tools/testing/selftests/bpf/progs/local_kptr_stash.c
Source file repositories/reference/linux-study-clean/tools/testing/selftests/bpf/progs/local_kptr_stash.c
File Facts
- System
- Linux kernel
- Corpus path
tools/testing/selftests/bpf/progs/local_kptr_stash.c- Extension
.c- Size
- 5686 bytes
- Lines
- 286
- 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.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
vmlinux.hbpf/bpf_tracing.hbpf/bpf_helpers.hbpf/bpf_core_read.h../bpf_experimental.h../test_kmods/bpf_testmod_kfunc.h
Detected Declarations
struct plain_localstruct node_datastruct refcounted_nodestruct stashstruct plain_localstruct local_with_rootstruct map_valuefunction lessfunction create_and_stashfunction stash_rb_nodesfunction stash_plainfunction stash_local_with_rootfunction unstash_rb_nodefunction stash_test_ref_kfuncfunction refcount_acquire_without_unstashfunction stash_refcounted_node
Annotated Snippet
struct node_data {
long key;
long data;
struct plain_local __kptr * stashed_in_local_kptr;
struct bpf_rb_node node;
};
struct refcounted_node {
long data;
struct bpf_rb_node rb_node;
struct bpf_refcount refcount;
};
struct stash {
struct bpf_spin_lock l;
struct refcounted_node __kptr *stashed;
};
struct {
__uint(type, BPF_MAP_TYPE_ARRAY);
__type(key, int);
__type(value, struct stash);
__uint(max_entries, 10);
} refcounted_node_stash SEC(".maps");
struct plain_local {
long key;
long data;
};
struct local_with_root {
long key;
struct bpf_spin_lock l;
struct bpf_rb_root r __contains(node_data, node);
};
struct map_value {
struct prog_test_ref_kfunc *not_kptr;
struct prog_test_ref_kfunc __kptr *val;
struct node_data __kptr *node;
struct plain_local __kptr *plain;
struct local_with_root __kptr *local_root;
};
/* This is necessary so that LLVM generates BTF for node_data struct
* If it's not included, a fwd reference for node_data will be generated but
* no struct. Example BTF of "node" field in map_value when not included:
*
* [10] PTR '(anon)' type_id=35
* [34] FWD 'node_data' fwd_kind=struct
* [35] TYPE_TAG 'kptr_ref' type_id=34
*
* (with no node_data struct defined)
* Had to do the same w/ bpf_kfunc_call_test_release below
*/
struct node_data *just_here_because_btf_bug;
struct refcounted_node *just_here_because_btf_bug2;
struct {
__uint(type, BPF_MAP_TYPE_ARRAY);
__type(key, int);
__type(value, struct map_value);
__uint(max_entries, 2);
} some_nodes SEC(".maps");
static bool less(struct bpf_rb_node *a, const struct bpf_rb_node *b)
{
struct node_data *node_a;
struct node_data *node_b;
node_a = container_of(a, struct node_data, node);
node_b = container_of(b, struct node_data, node);
return node_a->key < node_b->key;
}
static int create_and_stash(int idx, int val)
{
struct plain_local *inner_local_kptr;
struct map_value *mapval;
struct node_data *res;
mapval = bpf_map_lookup_elem(&some_nodes, &idx);
if (!mapval)
return 1;
inner_local_kptr = bpf_obj_new(typeof(*inner_local_kptr));
if (!inner_local_kptr)
return 2;
Annotation
- Immediate include surface: `vmlinux.h`, `bpf/bpf_tracing.h`, `bpf/bpf_helpers.h`, `bpf/bpf_core_read.h`, `../bpf_experimental.h`, `../test_kmods/bpf_testmod_kfunc.h`.
- Detected declarations: `struct plain_local`, `struct node_data`, `struct refcounted_node`, `struct stash`, `struct plain_local`, `struct local_with_root`, `struct map_value`, `function less`, `function create_and_stash`, `function stash_rb_nodes`.
- 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.