tools/testing/shared/linux.c
Source file repositories/reference/linux-study-clean/tools/testing/shared/linux.c
File Facts
- System
- Linux kernel
- Corpus path
tools/testing/shared/linux.c- Extension
.c- Size
- 8307 bytes
- Lines
- 377
- Domain
- Support Tooling And Documentation
- Bucket
- tools
- Inferred role
- Support Tooling And Documentation: implementation source
- Status
- source implementation candidate
Why This File Exists
Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- 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
stdlib.hstring.hmalloc.hpthread.hunistd.hassert.hlinux/gfp.hlinux/poison.hlinux/slab.hlinux/radix-tree.hurcu/uatomic.h
Detected Declarations
function kmem_cache_set_callbackfunction kmem_cache_set_privatefunction kmem_cache_set_non_kernelfunction kmem_cache_get_allocfunction kmem_cache_nr_allocatedfunction kmem_cache_nr_tallocatedfunction kmem_cache_zero_nr_tallocatedfunction __kmem_cache_free_lockedfunction kmem_cache_free_lockedfunction kmem_cache_freefunction kmem_cache_free_bulkfunction kmem_cache_shrinkfunction __kmem_cache_create_argsfunction kmem_cache_prefill_sheaffunction kmem_cache_refill_sheaffunction kmem_cache_return_sheaffunction kmem_cache_alloc_from_sheaffunction test_kmem_cache_bulk
Annotated Snippet
if (!cachep->non_kernel) {
if (cachep->callback)
cachep->exec_callback = true;
return NULL;
}
cachep->non_kernel--;
}
pthread_mutex_lock(&cachep->lock);
if (cachep->nr_objs) {
struct radix_tree_node *node = cachep->objs;
cachep->nr_objs--;
cachep->objs = node->parent;
pthread_mutex_unlock(&cachep->lock);
node->parent = NULL;
p = node;
} else {
pthread_mutex_unlock(&cachep->lock);
if (cachep->align) {
if (posix_memalign(&p, cachep->align, cachep->size) < 0)
return NULL;
} else {
p = malloc(cachep->size);
}
if (cachep->ctor)
cachep->ctor(p);
else if (gfp & __GFP_ZERO)
memset(p, 0, cachep->size);
}
uatomic_inc(&cachep->nr_allocated);
uatomic_inc(&nr_allocated);
uatomic_inc(&cachep->nr_tallocated);
if (kmalloc_verbose)
printf("Allocating %p from slab\n", p);
return p;
}
void __kmem_cache_free_locked(struct kmem_cache *cachep, void *objp)
{
assert(objp);
if (cachep->nr_objs > 10 || cachep->align) {
memset(objp, POISON_FREE, cachep->size);
free(objp);
} else {
struct radix_tree_node *node = objp;
cachep->nr_objs++;
node->parent = cachep->objs;
cachep->objs = node;
}
}
void kmem_cache_free_locked(struct kmem_cache *cachep, void *objp)
{
uatomic_dec(&nr_allocated);
uatomic_dec(&cachep->nr_allocated);
if (kmalloc_verbose)
printf("Freeing %p to slab\n", objp);
__kmem_cache_free_locked(cachep, objp);
}
void kmem_cache_free(struct kmem_cache *cachep, void *objp)
{
pthread_mutex_lock(&cachep->lock);
kmem_cache_free_locked(cachep, objp);
pthread_mutex_unlock(&cachep->lock);
}
void kmem_cache_free_bulk(struct kmem_cache *cachep, size_t size, void **list)
{
if (kmalloc_verbose)
pr_debug("Bulk free %p[0-%zu]\n", list, size - 1);
if (cachep->exec_callback) {
if (cachep->callback)
cachep->callback(cachep->private);
cachep->exec_callback = false;
}
pthread_mutex_lock(&cachep->lock);
for (int i = 0; i < size; i++)
kmem_cache_free_locked(cachep, list[i]);
pthread_mutex_unlock(&cachep->lock);
}
void kmem_cache_shrink(struct kmem_cache *cachep)
{
}
Annotation
- Immediate include surface: `stdlib.h`, `string.h`, `malloc.h`, `pthread.h`, `unistd.h`, `assert.h`, `linux/gfp.h`, `linux/poison.h`.
- Detected declarations: `function kmem_cache_set_callback`, `function kmem_cache_set_private`, `function kmem_cache_set_non_kernel`, `function kmem_cache_get_alloc`, `function kmem_cache_nr_allocated`, `function kmem_cache_nr_tallocated`, `function kmem_cache_zero_nr_tallocated`, `function __kmem_cache_free_locked`, `function kmem_cache_free_locked`, `function kmem_cache_free`.
- Atlas domain: Support Tooling And Documentation / tools.
- Implementation status: source 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.