lib/kfifo.c
Source file repositories/reference/linux-study-clean/lib/kfifo.c
File Facts
- System
- Linux kernel
- Corpus path
lib/kfifo.c- Extension
.c- Size
- 12666 bytes
- Lines
- 596
- Domain
- Kernel Services
- Bucket
- lib
- Inferred role
- Kernel Services: exported/initcall integration point
- Status
- integration implementation candidate
Why This File Exists
Shared kernel service surface used by multiple subsystems, including helpers, cryptography, virtualization support, and async I/O infrastructure.
- Shared kernel service surface used by multiple subsystems, including helpers, cryptography, virtualization support, and async I/O infrastructure.
- Exports symbols or registers init work; inspect boot/module ordering and who consumes the exported contract.
- Touches user memory; correctness depends on fault-safe copying and privilege boundary handling.
- Allocates kernel memory; connect allocation flags and lifetime to context constraints.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/dma-mapping.hlinux/err.hlinux/export.hlinux/kfifo.hlinux/log2.hlinux/scatterlist.hlinux/slab.hlinux/uaccess.h
Detected Declarations
function Copyrightfunction __kfifo_alloc_nodefunction __kfifo_freefunction __kfifo_initfunction kfifo_copy_infunction __kfifo_infunction kfifo_copy_outfunction __kfifo_out_peekfunction __kfifo_out_linearfunction __kfifo_outfunction kfifo_copy_from_userfunction __kfifo_from_userfunction kfifo_copy_to_userfunction __kfifo_to_userfunction setup_sgl_buffunction setup_sglfunction __kfifo_dma_in_preparefunction __kfifo_dma_out_preparefunction __kfifo_max_rfunction __kfifo_peek_nfunction __kfifo_poke_nfunction __kfifo_len_rfunction __kfifo_in_rfunction kfifo_out_copy_rfunction __kfifo_out_peek_rfunction __kfifo_out_linear_rfunction __kfifo_out_rfunction __kfifo_skip_rfunction __kfifo_from_user_rfunction __kfifo_to_user_rfunction __kfifo_dma_in_prepare_rfunction __kfifo_dma_in_finish_rfunction __kfifo_dma_out_prepare_rexport __kfifo_alloc_nodeexport __kfifo_freeexport __kfifo_initexport __kfifo_inexport __kfifo_out_peekexport __kfifo_out_linearexport __kfifo_outexport __kfifo_from_userexport __kfifo_to_userexport __kfifo_dma_in_prepareexport __kfifo_dma_out_prepareexport __kfifo_max_rexport __kfifo_len_rexport __kfifo_in_rexport __kfifo_out_peek_r
Annotated Snippet
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* A generic kernel FIFO implementation
*
* Copyright (C) 2009/2010 Stefani Seibold <stefani@seibold.net>
*/
#include <linux/dma-mapping.h>
#include <linux/err.h>
#include <linux/export.h>
#include <linux/kfifo.h>
#include <linux/log2.h>
#include <linux/scatterlist.h>
#include <linux/slab.h>
#include <linux/uaccess.h>
/*
* internal helper to calculate the unused elements in a fifo
*/
static inline unsigned int kfifo_unused(struct __kfifo *fifo)
{
return (fifo->mask + 1) - (fifo->in - fifo->out);
}
int __kfifo_alloc_node(struct __kfifo *fifo, unsigned int size,
size_t esize, gfp_t gfp_mask, int node)
{
/*
* round up to the next power of 2, since our 'let the indices
* wrap' technique works only in this case.
*/
size = roundup_pow_of_two(size);
fifo->in = 0;
fifo->out = 0;
fifo->esize = esize;
if (size < 2) {
fifo->data = NULL;
fifo->mask = 0;
return -EINVAL;
}
fifo->data = kmalloc_array_node(size, esize, gfp_mask, node);
if (!fifo->data) {
fifo->mask = 0;
return -ENOMEM;
}
fifo->mask = size - 1;
return 0;
}
EXPORT_SYMBOL(__kfifo_alloc_node);
void __kfifo_free(struct __kfifo *fifo)
{
kfree(fifo->data);
fifo->in = 0;
fifo->out = 0;
fifo->esize = 0;
fifo->data = NULL;
fifo->mask = 0;
}
EXPORT_SYMBOL(__kfifo_free);
int __kfifo_init(struct __kfifo *fifo, void *buffer,
unsigned int size, size_t esize)
{
size /= esize;
if (!is_power_of_2(size))
size = rounddown_pow_of_two(size);
fifo->in = 0;
fifo->out = 0;
fifo->esize = esize;
fifo->data = buffer;
if (size < 2) {
fifo->mask = 0;
return -EINVAL;
}
fifo->mask = size - 1;
return 0;
}
EXPORT_SYMBOL(__kfifo_init);
static void kfifo_copy_in(struct __kfifo *fifo, const void *src,
Annotation
- Immediate include surface: `linux/dma-mapping.h`, `linux/err.h`, `linux/export.h`, `linux/kfifo.h`, `linux/log2.h`, `linux/scatterlist.h`, `linux/slab.h`, `linux/uaccess.h`.
- Detected declarations: `function Copyright`, `function __kfifo_alloc_node`, `function __kfifo_free`, `function __kfifo_init`, `function kfifo_copy_in`, `function __kfifo_in`, `function kfifo_copy_out`, `function __kfifo_out_peek`, `function __kfifo_out_linear`, `function __kfifo_out`.
- Atlas domain: Kernel Services / lib.
- Implementation status: integration implementation candidate.
- This snippet crosses the user/kernel memory boundary; validate fault handling and access checks before translating the pattern.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.