kernel/bpf/local_storage.c
Source file repositories/reference/linux-study-clean/kernel/bpf/local_storage.c
File Facts
- System
- Linux kernel
- Corpus path
kernel/bpf/local_storage.c- Extension
.c- Size
- 15315 bytes
- Lines
- 619
- Domain
- Core OS
- Bucket
- Scheduler, Processes, Timers, Sync, And Syscalls
- Inferred role
- Core OS: implementation source
- Status
- source implementation candidate
Why This File Exists
Core operating-system implementation surface: boot, tasks, memory, VFS, syscall-facing interfaces, synchronization, credentials, and isolation.
- Core operating-system implementation surface: boot, tasks, memory, VFS, syscall-facing interfaces, synchronization, credentials, and isolation.
- 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/bpf-cgroup.hlinux/bpf.hlinux/bpf_local_storage.hlinux/btf.hlinux/bug.hlinux/filter.hlinux/mm.hlinux/rbtree.hlinux/slab.huapi/linux/btf.hlinux/btf_ids.h../cgroup/cgroup-internal.h
Detected Declarations
struct bpf_cgroup_storage_mapfunction attach_type_isolatedfunction bpf_cgroup_storage_key_cmpfunction cgroup_storage_lookupfunction cgroup_storage_insertfunction cgroup_storage_update_elemfunction bpf_percpu_cgroup_storage_copyfunction bpf_percpu_cgroup_storage_updatefunction cgroup_storage_get_next_keyfunction cgroup_storage_map_freefunction list_for_each_entry_safefunction cgroup_storage_delete_elemfunction cgroup_storage_check_btffunction cgroup_storage_seq_show_elemfunction for_each_possible_cpufunction cgroup_storage_map_usagefunction bpf_cgroup_storage_assignfunction bpf_cgroup_storage_calculate_sizefunction free_shared_cgroup_storage_rcufunction free_percpu_cgroup_storage_rcufunction bpf_cgroup_storage_freefunction bpf_cgroup_storage_linkfunction bpf_cgroup_storage_unlink
Annotated Snippet
struct bpf_cgroup_storage_map {
struct bpf_map map;
spinlock_t lock;
struct rb_root root;
struct list_head list;
};
static struct bpf_cgroup_storage_map *map_to_storage(struct bpf_map *map)
{
return container_of(map, struct bpf_cgroup_storage_map, map);
}
static bool attach_type_isolated(const struct bpf_map *map)
{
return map->key_size == sizeof(struct bpf_cgroup_storage_key);
}
static int bpf_cgroup_storage_key_cmp(const struct bpf_cgroup_storage_map *map,
const void *_key1, const void *_key2)
{
if (attach_type_isolated(&map->map)) {
const struct bpf_cgroup_storage_key *key1 = _key1;
const struct bpf_cgroup_storage_key *key2 = _key2;
if (key1->cgroup_inode_id < key2->cgroup_inode_id)
return -1;
else if (key1->cgroup_inode_id > key2->cgroup_inode_id)
return 1;
else if (key1->attach_type < key2->attach_type)
return -1;
else if (key1->attach_type > key2->attach_type)
return 1;
} else {
const __u64 *cgroup_inode_id1 = _key1;
const __u64 *cgroup_inode_id2 = _key2;
if (*cgroup_inode_id1 < *cgroup_inode_id2)
return -1;
else if (*cgroup_inode_id1 > *cgroup_inode_id2)
return 1;
}
return 0;
}
struct bpf_cgroup_storage *
cgroup_storage_lookup(struct bpf_cgroup_storage_map *map,
void *key, bool locked)
{
struct rb_root *root = &map->root;
struct rb_node *node;
if (!locked)
spin_lock_bh(&map->lock);
node = root->rb_node;
while (node) {
struct bpf_cgroup_storage *storage;
storage = container_of(node, struct bpf_cgroup_storage, node);
switch (bpf_cgroup_storage_key_cmp(map, key, &storage->key)) {
case -1:
node = node->rb_left;
break;
case 1:
node = node->rb_right;
break;
default:
if (!locked)
spin_unlock_bh(&map->lock);
return storage;
}
}
if (!locked)
spin_unlock_bh(&map->lock);
return NULL;
}
static int cgroup_storage_insert(struct bpf_cgroup_storage_map *map,
struct bpf_cgroup_storage *storage)
{
struct rb_root *root = &map->root;
struct rb_node **new = &(root->rb_node), *parent = NULL;
while (*new) {
struct bpf_cgroup_storage *this;
Annotation
- Immediate include surface: `linux/bpf-cgroup.h`, `linux/bpf.h`, `linux/bpf_local_storage.h`, `linux/btf.h`, `linux/bug.h`, `linux/filter.h`, `linux/mm.h`, `linux/rbtree.h`.
- Detected declarations: `struct bpf_cgroup_storage_map`, `function attach_type_isolated`, `function bpf_cgroup_storage_key_cmp`, `function cgroup_storage_lookup`, `function cgroup_storage_insert`, `function cgroup_storage_update_elem`, `function bpf_percpu_cgroup_storage_copy`, `function bpf_percpu_cgroup_storage_update`, `function cgroup_storage_get_next_key`, `function cgroup_storage_map_free`.
- Atlas domain: Core OS / Scheduler, Processes, Timers, Sync, And Syscalls.
- 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.