lib/scatterlist.c
Source file repositories/reference/linux-study-clean/lib/scatterlist.c
File Facts
- System
- Linux kernel
- Corpus path
lib/scatterlist.c- Extension
.c- Size
- 37850 bytes
- Lines
- 1439
- 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.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- 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/export.hlinux/slab.hlinux/scatterlist.hlinux/highmem.hlinux/kmemleak.hlinux/bvec.hlinux/uio.hlinux/folio_queue.h
Detected Declarations
function Copyrightfunction sg_nents_for_lenfunction sg_nents_for_dmafunction sg_init_tablefunction sg_init_onefunction sg_alloc_tablefunction sg_kfreefunction __sg_alloc_tablefunction sg_free_append_tablefunction sg_free_tablefunction __sg_free_tablefunction sg_alloc_tablefunction pages_are_mergeablefunction sg_free_append_tablefunction sg_alloc_table_from_pages_segmentfunction sgl_free_n_orderfunction for_each_sgfunction sgl_free_orderfunction sgl_freefunction __sg_page_iter_startfunction sg_page_countfunction __sg_page_iter_nextfunction sg_dma_page_countfunction __sg_page_iter_dma_nextfunction sg_miter_startfunction sg_miter_get_next_pagefunction sg_miter_skipfunction sg_miter_startfunction sg_miter_startfunction sg_copy_bufferfunction sg_copy_from_bufferfunction sg_copy_to_bufferfunction sg_pcopy_from_bufferfunction sg_pcopy_to_bufferfunction sg_zero_bufferfunction extract_user_to_sgfunction extract_bvec_to_sgfunction extract_kvec_to_sgfunction extract_folioq_to_sgfunction extract_xarray_to_sgfunction xas_for_eachfunction iov_iter_extract_modeexport sg_nentsexport sg_nents_for_lenexport sg_nents_for_dmaexport sg_lastexport sg_init_tableexport sg_init_one
Annotated Snippet
if (alloc_size > curr_max_ents) {
next = sg_chain_ptr(&sgl[curr_max_ents - 1]);
alloc_size = curr_max_ents;
sg_size = alloc_size - 1;
} else {
sg_size = alloc_size;
next = NULL;
}
num_ents -= sg_size;
if (nents_first_chunk)
nents_first_chunk = 0;
else
free_fn(sgl, alloc_size);
sgl = next;
curr_max_ents = max_ents;
}
table->sgl = NULL;
}
EXPORT_SYMBOL(__sg_free_table);
/**
* sg_free_append_table - Free a previously allocated append sg table.
* @table: The mapped sg append table header
*
**/
void sg_free_append_table(struct sg_append_table *table)
{
__sg_free_table(&table->sgt, SG_MAX_SINGLE_ALLOC, 0, sg_kfree,
table->total_nents);
}
EXPORT_SYMBOL(sg_free_append_table);
/**
* sg_free_table - Free a previously allocated sg table
* @table: The mapped sg table header
*
**/
void sg_free_table(struct sg_table *table)
{
__sg_free_table(table, SG_MAX_SINGLE_ALLOC, 0, sg_kfree,
table->orig_nents);
}
EXPORT_SYMBOL(sg_free_table);
/**
* __sg_alloc_table - Allocate and initialize an sg table with given allocator
* @table: The sg table header to use
* @nents: Number of entries in sg list
* @max_ents: The maximum number of entries the allocator returns per call
* @first_chunk: first SGL if preallocated (may be %NULL)
* @nents_first_chunk: Number of entries in the (preallocated) first
* scatterlist chunk, 0 means no such preallocated chunk provided by user
* @gfp_mask: GFP allocation mask
* @alloc_fn: Allocator to use
*
* Description:
* This function returns a @table @nents long. The allocator is
* defined to return scatterlist chunks of maximum size @max_ents.
* Thus if @nents is bigger than @max_ents, the scatterlists will be
* chained in units of @max_ents.
*
* Notes:
* If this function returns non-0 (eg failure), the caller must call
* __sg_free_table() to cleanup any leftover allocations.
*
**/
int __sg_alloc_table(struct sg_table *table, unsigned int nents,
unsigned int max_ents, struct scatterlist *first_chunk,
unsigned int nents_first_chunk, gfp_t gfp_mask,
sg_alloc_fn *alloc_fn)
{
struct scatterlist *sg, *prv;
unsigned int left;
unsigned curr_max_ents = nents_first_chunk ?: max_ents;
unsigned prv_max_ents;
memset(table, 0, sizeof(*table));
if (nents == 0)
return -EINVAL;
#ifdef CONFIG_ARCH_NO_SG_CHAIN
if (WARN_ON_ONCE(nents > max_ents))
return -EINVAL;
#endif
left = nents;
prv = NULL;
Annotation
- Immediate include surface: `linux/export.h`, `linux/slab.h`, `linux/scatterlist.h`, `linux/highmem.h`, `linux/kmemleak.h`, `linux/bvec.h`, `linux/uio.h`, `linux/folio_queue.h`.
- Detected declarations: `function Copyright`, `function sg_nents_for_len`, `function sg_nents_for_dma`, `function sg_init_table`, `function sg_init_one`, `function sg_alloc_table`, `function sg_kfree`, `function __sg_alloc_table`, `function sg_free_append_table`, `function sg_free_table`.
- Atlas domain: Kernel Services / lib.
- Implementation status: integration implementation candidate.
- Synchronization appears in or near this file; preserve lock ordering, sleepability, and interrupt-context constraints.
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.