kernel/bpf/bpf_local_storage.c
Source file repositories/reference/linux-study-clean/kernel/bpf/bpf_local_storage.c
File Facts
- System
- Linux kernel
- Corpus path
kernel/bpf/bpf_local_storage.c- Extension
.c- Size
- 25350 bytes
- Lines
- 878
- 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/rculist.hlinux/list.hlinux/hash.hlinux/types.hlinux/spinlock.hlinux/bpf.hlinux/btf_ids.hlinux/bpf_local_storage.hnet/sock.huapi/linux/sock_diag.huapi/linux/btf.hlinux/rcupdate.hlinux/rcupdate_trace.hlinux/rcupdate_wait.h
Detected Declarations
function select_bucketfunction mem_chargefunction mem_unchargefunction owner_storagefunction selem_linked_to_storage_locklessfunction selem_linked_to_storagefunction selem_linked_to_mapfunction bpf_selem_allocfunction bpf_local_storage_free_trace_rcufunction bpf_local_storage_freefunction bpf_selem_free_trace_rcufunction bpf_selem_freefunction bpf_selem_free_listfunction bpf_selem_unlink_storage_nolock_miscfunction bpf_selem_unlink_storage_nolockfunction bpf_selem_link_storage_nolockfunction bpf_selem_unlink_mapfunction bpf_selem_unlink_map_nolockfunction bpf_selem_link_mapfunction bpf_selem_link_map_nolockfunction bpf_selem_unlinkfunction bpf_local_storage_destroyfunction __bpf_local_storage_insert_cachefunction check_flagsfunction bpf_local_storage_allocfunction bpf_local_storage_updatefunction bpf_local_storage_cache_idx_getfunction bpf_local_storage_cache_idx_freefunction bpf_local_storage_map_alloc_checkfunction bpf_local_storage_map_check_btffunction bpf_local_storage_destroyfunction bpf_local_storage_map_mem_usagefunction bpf_local_storage_map_allocfunction bpf_local_storage_map_freefunction hlist_for_each_entry_rcu
Annotated Snippet
if (value) {
/* No need to call check_and_init_map_value as memory is zero init */
copy_map_value(&smap->map, SDATA(selem)->data, value);
if (swap_uptrs)
bpf_obj_swap_uptrs(smap->map.record, SDATA(selem)->data, value);
}
return selem;
}
mem_uncharge(smap, owner, smap->elem_size);
return NULL;
}
static void bpf_local_storage_free_trace_rcu(struct rcu_head *rcu)
{
struct bpf_local_storage *local_storage;
/*
* RCU Tasks Trace grace period implies RCU grace period, do
* kfree() directly.
*/
local_storage = container_of(rcu, struct bpf_local_storage, rcu);
kfree(local_storage);
}
static void bpf_local_storage_free(struct bpf_local_storage *local_storage,
bool reuse_now)
{
if (!local_storage)
return;
if (reuse_now) {
kfree_rcu(local_storage, rcu);
return;
}
call_rcu_tasks_trace(&local_storage->rcu,
bpf_local_storage_free_trace_rcu);
}
static void bpf_selem_free_trace_rcu(struct rcu_head *rcu)
{
struct bpf_local_storage_elem *selem;
struct bpf_local_storage_map *smap;
selem = container_of(rcu, struct bpf_local_storage_elem, rcu);
/* The bpf_local_storage_map_free will wait for rcu_barrier */
smap = rcu_dereference_check(SDATA(selem)->smap, 1);
if (smap)
bpf_obj_free_fields(smap->map.record, SDATA(selem)->data);
/*
* RCU Tasks Trace grace period implies RCU grace period, do
* kfree() directly.
*/
kfree(selem);
}
void bpf_selem_free(struct bpf_local_storage_elem *selem,
bool reuse_now)
{
struct bpf_local_storage_map *smap;
smap = rcu_dereference_check(SDATA(selem)->smap, 1);
if (reuse_now) {
if (smap)
bpf_obj_free_fields(smap->map.record, SDATA(selem)->data);
kfree_rcu(selem, rcu);
return;
}
call_rcu_tasks_trace(&selem->rcu, bpf_selem_free_trace_rcu);
}
static void bpf_selem_free_list(struct hlist_head *list, bool reuse_now)
{
struct bpf_local_storage_elem *selem;
struct hlist_node *n;
/* The "_safe" iteration is needed.
* The loop is not removing the selem from the list
* but bpf_selem_free will use the selem->rcu_head
* which is union-ized with the selem->free_node.
*/
hlist_for_each_entry_safe(selem, n, list, free_node)
bpf_selem_free(selem, reuse_now);
}
Annotation
- Immediate include surface: `linux/rculist.h`, `linux/list.h`, `linux/hash.h`, `linux/types.h`, `linux/spinlock.h`, `linux/bpf.h`, `linux/btf_ids.h`, `linux/bpf_local_storage.h`.
- Detected declarations: `function select_bucket`, `function mem_charge`, `function mem_uncharge`, `function owner_storage`, `function selem_linked_to_storage_lockless`, `function selem_linked_to_storage`, `function selem_linked_to_map`, `function bpf_selem_alloc`, `function bpf_local_storage_free_trace_rcu`, `function bpf_local_storage_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.