kernel/bpf/hashtab.c
Source file repositories/reference/linux-study-clean/kernel/bpf/hashtab.c
File Facts
- System
- Linux kernel
- Corpus path
kernel/bpf/hashtab.c- Extension
.c- Size
- 94221 bytes
- Lines
- 3536
- 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.
- Touches user memory; correctness depends on fault-safe copying and privilege boundary handling.
- 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.hlinux/btf.hlinux/jhash.hlinux/filter.hlinux/rculist_nulls.hlinux/rcupdate_wait.hlinux/random.hlinux/rhashtable.huapi/linux/btf.hlinux/rcupdate_trace.hlinux/btf_ids.hpercpu_freelist.hbpf_lru_list.hmap_in_map.hlinux/bpf_mem_alloc.hasm/rqspinlock.h
Detected Declarations
struct bucketstruct bpf_htabstruct htab_elemstruct htab_btf_recordstruct bpf_iter_seq_hash_map_infostruct rhtab_elemstruct bpf_rhtabstruct bpf_iter_seq_rhash_map_infofunction htab_is_preallocfunction htab_init_bucketsfunction htab_lock_bucketfunction htab_unlock_bucketfunction htab_is_lrufunction htab_is_percpufunction is_fd_htabfunction htab_elem_set_ptrfunction htab_has_extra_elemsfunction htab_free_prealloced_internal_structsfunction htab_free_prealloced_fieldsfunction for_each_possible_cpufunction htab_free_elemsfunction bpf_lru_pop_freefunction prealloc_initfunction prealloc_destroyfunction alloc_extra_elemsfunction for_each_possible_cpufunction htab_map_alloc_checkfunction htab_mem_dtorfunction htab_pcpu_mem_dtorfunction htab_dtor_ctx_freefunction bpf_ma_set_dtorfunction htab_map_check_btffunction htab_map_hashfunction bpf_map_lookup_elemfunction htab_map_gen_lookupfunction htab_lru_map_gen_lookupfunction check_and_cancel_fieldsfunction htab_lru_map_delete_nodefunction hlist_nulls_for_each_entry_rcufunction htab_map_get_next_keyfunction htab_elem_freefunction htab_put_fd_valuefunction is_map_fullfunction inc_elem_countfunction dec_elem_countfunction free_htab_elemfunction pcpu_copy_valuefunction for_each_possible_cpu
Annotated Snippet
struct bucket {
struct hlist_nulls_head head;
rqspinlock_t raw_lock;
};
struct bpf_htab {
struct bpf_map map;
struct bpf_mem_alloc ma;
struct bpf_mem_alloc pcpu_ma;
struct bucket *buckets;
void *elems;
union {
struct pcpu_freelist freelist;
struct bpf_lru lru;
};
struct htab_elem *__percpu *extra_elems;
/* number of elements in non-preallocated hashtable are kept
* in either pcount or count
*/
struct percpu_counter pcount;
atomic_t count;
bool use_percpu_counter;
u32 n_buckets; /* number of hash buckets */
u32 elem_size; /* size of each element in bytes */
u32 hashrnd;
};
/* each htab element is struct htab_elem + key + value */
struct htab_elem {
union {
struct hlist_nulls_node hash_node;
struct {
void *padding;
union {
struct pcpu_freelist_node fnode;
struct htab_elem *batch_flink;
};
};
};
union {
/* pointer to per-cpu pointer */
void *ptr_to_pptr;
struct bpf_lru_node lru_node;
};
u32 hash;
char key[] __aligned(8);
};
struct htab_btf_record {
struct btf_record *record;
u32 key_size;
};
static inline bool htab_is_prealloc(const struct bpf_htab *htab)
{
return !(htab->map.map_flags & BPF_F_NO_PREALLOC);
}
static void htab_init_buckets(struct bpf_htab *htab)
{
unsigned int i;
for (i = 0; i < htab->n_buckets; i++) {
INIT_HLIST_NULLS_HEAD(&htab->buckets[i].head, i);
raw_res_spin_lock_init(&htab->buckets[i].raw_lock);
cond_resched();
}
}
static inline int htab_lock_bucket(struct bucket *b, unsigned long *pflags)
{
unsigned long flags;
int ret;
ret = raw_res_spin_lock_irqsave(&b->raw_lock, flags);
if (ret)
return ret;
*pflags = flags;
return 0;
}
static inline void htab_unlock_bucket(struct bucket *b, unsigned long flags)
{
raw_res_spin_unlock_irqrestore(&b->raw_lock, flags);
}
static bool htab_lru_map_delete_node(void *arg, struct bpf_lru_node *node);
static bool htab_is_lru(const struct bpf_htab *htab)
{
Annotation
- Immediate include surface: `linux/bpf.h`, `linux/btf.h`, `linux/jhash.h`, `linux/filter.h`, `linux/rculist_nulls.h`, `linux/rcupdate_wait.h`, `linux/random.h`, `linux/rhashtable.h`.
- Detected declarations: `struct bucket`, `struct bpf_htab`, `struct htab_elem`, `struct htab_btf_record`, `struct bpf_iter_seq_hash_map_info`, `struct rhtab_elem`, `struct bpf_rhtab`, `struct bpf_iter_seq_rhash_map_info`, `function htab_is_prealloc`, `function htab_init_buckets`.
- Atlas domain: Core OS / Scheduler, Processes, Timers, Sync, And Syscalls.
- Implementation status: source implementation candidate.
- This snippet crosses the user/kernel memory boundary; validate fault handling and access checks before translating the pattern.
- 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.