tools/testing/selftests/bpf/progs/kmem_cache_iter.c
Source file repositories/reference/linux-study-clean/tools/testing/selftests/bpf/progs/kmem_cache_iter.c
File Facts
- System
- Linux kernel
- Corpus path
tools/testing/selftests/bpf/progs/kmem_cache_iter.c- Extension
.c- Size
- 2315 bytes
- Lines
- 109
- 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.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
vmlinux.hbpf/bpf_helpers.hbpf/bpf_tracing.hbpf_experimental.h
Detected Declarations
struct kmem_cache_resultfunction slab_info_collectorfunction BPF_PROGfunction open_coded_iterfunction bpf_for_each
Annotated Snippet
struct kmem_cache_result {
char name[SLAB_NAME_MAX];
long obj_size;
};
struct {
__uint(type, BPF_MAP_TYPE_HASH);
__uint(key_size, sizeof(void *));
__uint(value_size, SLAB_NAME_MAX);
__uint(max_entries, 1);
} slab_hash SEC(".maps");
struct {
__uint(type, BPF_MAP_TYPE_ARRAY);
__uint(key_size, sizeof(int));
__uint(value_size, sizeof(struct kmem_cache_result));
__uint(max_entries, 1024);
} slab_result SEC(".maps");
extern struct kmem_cache *bpf_get_kmem_cache(u64 addr) __ksym;
/* Result, will be checked by userspace */
int task_struct_found;
int kmem_cache_seen;
int open_coded_seen;
SEC("iter/kmem_cache")
int slab_info_collector(struct bpf_iter__kmem_cache *ctx)
{
struct seq_file *seq = ctx->meta->seq;
struct kmem_cache *s = ctx->s;
struct kmem_cache_result *r;
int idx;
if (s) {
/* To make sure if the slab_iter implements the seq interface
* properly and it's also useful for debugging.
*/
BPF_SEQ_PRINTF(seq, "%s: %u\n", s->name, s->size);
idx = kmem_cache_seen;
r = bpf_map_lookup_elem(&slab_result, &idx);
if (r == NULL)
return 0;
kmem_cache_seen++;
/* Save name and size to match /proc/slabinfo */
bpf_probe_read_kernel_str(r->name, sizeof(r->name), s->name);
r->obj_size = s->size;
if (!bpf_strncmp(r->name, 11, "task_struct"))
bpf_map_update_elem(&slab_hash, &s, r->name, BPF_NOEXIST);
}
return 0;
}
SEC("raw_tp/bpf_test_finish")
int BPF_PROG(check_task_struct)
{
u64 curr = bpf_get_current_task();
struct kmem_cache *s;
char *name;
s = bpf_get_kmem_cache(curr);
if (s == NULL) {
task_struct_found = -1;
return 0;
}
name = bpf_map_lookup_elem(&slab_hash, &s);
if (name && !bpf_strncmp(name, 11, "task_struct"))
task_struct_found = 1;
else
task_struct_found = -2;
return 0;
}
SEC("syscall")
int open_coded_iter(const void *ctx)
{
struct kmem_cache *s;
bpf_for_each(kmem_cache, s) {
struct kmem_cache_result *r;
r = bpf_map_lookup_elem(&slab_result, &open_coded_seen);
if (!r)
break;
Annotation
- Immediate include surface: `vmlinux.h`, `bpf/bpf_helpers.h`, `bpf/bpf_tracing.h`, `bpf_experimental.h`.
- Detected declarations: `struct kmem_cache_result`, `function slab_info_collector`, `function BPF_PROG`, `function open_coded_iter`, `function bpf_for_each`.
- Atlas domain: Support Tooling And Documentation / tools.
- Implementation status: source implementation candidate.
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.