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.

Dependency Surface

Detected Declarations

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

Implementation Notes