Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2) /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  * Copyright (c) 2016 Facebook
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5) #include <linux/bpf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6) #include <linux/btf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7) #include <linux/jhash.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8) #include <linux/filter.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9) #include <linux/rculist_nulls.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10) #include <linux/random.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11) #include <uapi/linux/btf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12) #include <linux/rcupdate_trace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13) #include "percpu_freelist.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14) #include "bpf_lru_list.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15) #include "map_in_map.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17) #define HTAB_CREATE_FLAG_MASK						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18) 	(BPF_F_NO_PREALLOC | BPF_F_NO_COMMON_LRU | BPF_F_NUMA_NODE |	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19) 	 BPF_F_ACCESS_MASK | BPF_F_ZERO_SEED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) #define BATCH_OPS(_name)			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) 	.map_lookup_batch =			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) 	_name##_map_lookup_batch,		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) 	.map_lookup_and_delete_batch =		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) 	_name##_map_lookup_and_delete_batch,	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) 	.map_update_batch =			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) 	generic_map_update_batch,		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) 	.map_delete_batch =			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) 	generic_map_delete_batch
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32)  * The bucket lock has two protection scopes:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34)  * 1) Serializing concurrent operations from BPF programs on differrent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35)  *    CPUs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37)  * 2) Serializing concurrent operations from BPF programs and sys_bpf()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39)  * BPF programs can execute in any context including perf, kprobes and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40)  * tracing. As there are almost no limits where perf, kprobes and tracing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41)  * can be invoked from the lock operations need to be protected against
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42)  * deadlocks. Deadlocks can be caused by recursion and by an invocation in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43)  * the lock held section when functions which acquire this lock are invoked
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44)  * from sys_bpf(). BPF recursion is prevented by incrementing the per CPU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45)  * variable bpf_prog_active, which prevents BPF programs attached to perf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46)  * events, kprobes and tracing to be invoked before the prior invocation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47)  * from one of these contexts completed. sys_bpf() uses the same mechanism
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48)  * by pinning the task to the current CPU and incrementing the recursion
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49)  * protection accross the map operation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51)  * This has subtle implications on PREEMPT_RT. PREEMPT_RT forbids certain
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52)  * operations like memory allocations (even with GFP_ATOMIC) from atomic
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53)  * contexts. This is required because even with GFP_ATOMIC the memory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54)  * allocator calls into code pathes which acquire locks with long held lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55)  * sections. To ensure the deterministic behaviour these locks are regular
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56)  * spinlocks, which are converted to 'sleepable' spinlocks on RT. The only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57)  * true atomic contexts on an RT kernel are the low level hardware
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58)  * handling, scheduling, low level interrupt handling, NMIs etc. None of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59)  * these contexts should ever do memory allocations.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61)  * As regular device interrupt handlers and soft interrupts are forced into
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62)  * thread context, the existing code which does
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63)  *   spin_lock*(); alloc(GPF_ATOMIC); spin_unlock*();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64)  * just works.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66)  * In theory the BPF locks could be converted to regular spinlocks as well,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67)  * but the bucket locks and percpu_freelist locks can be taken from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68)  * arbitrary contexts (perf, kprobes, tracepoints) which are required to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69)  * atomic contexts even on RT. These mechanisms require preallocated maps,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70)  * so there is no need to invoke memory allocations within the lock held
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71)  * sections.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73)  * BPF maps which need dynamic allocation are only used from (forced)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74)  * thread context on RT and can therefore use regular spinlocks which in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75)  * turn allows to invoke memory allocations from the lock held section.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77)  * On a non RT kernel this distinction is neither possible nor required.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78)  * spinlock maps to raw_spinlock and the extra code is optimized out by the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79)  * compiler.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) struct bucket {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) 	struct hlist_nulls_head head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) 	union {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) 		raw_spinlock_t raw_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) 		spinlock_t     lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) struct bpf_htab {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) 	struct bpf_map map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) 	struct bucket *buckets;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) 	void *elems;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) 	union {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) 		struct pcpu_freelist freelist;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) 		struct bpf_lru lru;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) 	struct htab_elem *__percpu *extra_elems;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) 	atomic_t count;	/* number of elements in this hashtable */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) 	u32 n_buckets;	/* number of hash buckets */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) 	u32 elem_size;	/* size of each element in bytes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) 	u32 hashrnd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) /* each htab element is struct htab_elem + key + value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) struct htab_elem {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) 	union {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) 		struct hlist_nulls_node hash_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) 		struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) 			void *padding;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) 			union {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) 				struct bpf_htab *htab;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) 				struct pcpu_freelist_node fnode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) 				struct htab_elem *batch_flink;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) 			};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) 		};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) 	union {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) 		struct rcu_head rcu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) 		struct bpf_lru_node lru_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) 	u32 hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) 	char key[] __aligned(8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) static inline bool htab_is_prealloc(const struct bpf_htab *htab)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) 	return !(htab->map.map_flags & BPF_F_NO_PREALLOC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) static inline bool htab_use_raw_lock(const struct bpf_htab *htab)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 	return (!IS_ENABLED(CONFIG_PREEMPT_RT) || htab_is_prealloc(htab));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) static void htab_init_buckets(struct bpf_htab *htab)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) 	unsigned i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) 	for (i = 0; i < htab->n_buckets; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 		INIT_HLIST_NULLS_HEAD(&htab->buckets[i].head, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) 		if (htab_use_raw_lock(htab))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 			raw_spin_lock_init(&htab->buckets[i].raw_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) 			spin_lock_init(&htab->buckets[i].lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) static inline unsigned long htab_lock_bucket(const struct bpf_htab *htab,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) 					     struct bucket *b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) 	if (htab_use_raw_lock(htab))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) 		raw_spin_lock_irqsave(&b->raw_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) 		spin_lock_irqsave(&b->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 	return flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) static inline void htab_unlock_bucket(const struct bpf_htab *htab,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 				      struct bucket *b,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 				      unsigned long flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 	if (htab_use_raw_lock(htab))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) 		raw_spin_unlock_irqrestore(&b->raw_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) 		spin_unlock_irqrestore(&b->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) static bool htab_lru_map_delete_node(void *arg, struct bpf_lru_node *node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) static bool htab_is_lru(const struct bpf_htab *htab)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) 	return htab->map.map_type == BPF_MAP_TYPE_LRU_HASH ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) 		htab->map.map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) static bool htab_is_percpu(const struct bpf_htab *htab)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) 	return htab->map.map_type == BPF_MAP_TYPE_PERCPU_HASH ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 		htab->map.map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) static inline void htab_elem_set_ptr(struct htab_elem *l, u32 key_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 				     void __percpu *pptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 	*(void __percpu **)(l->key + key_size) = pptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) static inline void __percpu *htab_elem_get_ptr(struct htab_elem *l, u32 key_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) 	return *(void __percpu **)(l->key + key_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) static void *fd_htab_map_get_ptr(const struct bpf_map *map, struct htab_elem *l)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) 	return *(void **)(l->key + roundup(map->key_size, 8));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) static struct htab_elem *get_htab_elem(struct bpf_htab *htab, int i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 	return (struct htab_elem *) (htab->elems + i * htab->elem_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) static void htab_free_elems(struct bpf_htab *htab)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) 	if (!htab_is_percpu(htab))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) 		goto free_elems;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) 	for (i = 0; i < htab->map.max_entries; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) 		void __percpu *pptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) 		pptr = htab_elem_get_ptr(get_htab_elem(htab, i),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) 					 htab->map.key_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) 		free_percpu(pptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) 		cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) free_elems:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 	bpf_map_area_free(htab->elems);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) /* The LRU list has a lock (lru_lock). Each htab bucket has a lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225)  * (bucket_lock). If both locks need to be acquired together, the lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226)  * order is always lru_lock -> bucket_lock and this only happens in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227)  * bpf_lru_list.c logic. For example, certain code path of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228)  * bpf_lru_pop_free(), which is called by function prealloc_lru_pop(),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229)  * will acquire lru_lock first followed by acquiring bucket_lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231)  * In hashtab.c, to avoid deadlock, lock acquisition of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232)  * bucket_lock followed by lru_lock is not allowed. In such cases,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233)  * bucket_lock needs to be released first before acquiring lru_lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) static struct htab_elem *prealloc_lru_pop(struct bpf_htab *htab, void *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 					  u32 hash)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 	struct bpf_lru_node *node = bpf_lru_pop_free(&htab->lru, hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 	struct htab_elem *l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 	if (node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 		l = container_of(node, struct htab_elem, lru_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 		memcpy(l->key, key, htab->map.key_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 		return l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) static int prealloc_init(struct bpf_htab *htab)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 	u32 num_entries = htab->map.max_entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 	int err = -ENOMEM, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 	if (!htab_is_percpu(htab) && !htab_is_lru(htab))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 		num_entries += num_possible_cpus();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) 	htab->elems = bpf_map_area_alloc(htab->elem_size * num_entries,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) 					 htab->map.numa_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 	if (!htab->elems)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 	if (!htab_is_percpu(htab))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 		goto skip_percpu_elems;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 	for (i = 0; i < num_entries; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) 		u32 size = round_up(htab->map.value_size, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) 		void __percpu *pptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) 		pptr = __alloc_percpu_gfp(size, 8, GFP_USER | __GFP_NOWARN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 		if (!pptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 			goto free_elems;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 		htab_elem_set_ptr(get_htab_elem(htab, i), htab->map.key_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 				  pptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 		cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) skip_percpu_elems:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 	if (htab_is_lru(htab))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 		err = bpf_lru_init(&htab->lru,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 				   htab->map.map_flags & BPF_F_NO_COMMON_LRU,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 				   offsetof(struct htab_elem, hash) -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 				   offsetof(struct htab_elem, lru_node),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 				   htab_lru_map_delete_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 				   htab);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 		err = pcpu_freelist_init(&htab->freelist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 		goto free_elems;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 	if (htab_is_lru(htab))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) 		bpf_lru_populate(&htab->lru, htab->elems,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 				 offsetof(struct htab_elem, lru_node),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) 				 htab->elem_size, num_entries);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 		pcpu_freelist_populate(&htab->freelist,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 				       htab->elems + offsetof(struct htab_elem, fnode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 				       htab->elem_size, num_entries);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) free_elems:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 	htab_free_elems(htab);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) static void prealloc_destroy(struct bpf_htab *htab)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 	htab_free_elems(htab);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) 	if (htab_is_lru(htab))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 		bpf_lru_destroy(&htab->lru);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 		pcpu_freelist_destroy(&htab->freelist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) static int alloc_extra_elems(struct bpf_htab *htab)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 	struct htab_elem *__percpu *pptr, *l_new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 	struct pcpu_freelist_node *l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 	int cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 	pptr = __alloc_percpu_gfp(sizeof(struct htab_elem *), 8,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 				  GFP_USER | __GFP_NOWARN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 	if (!pptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 	for_each_possible_cpu(cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) 		l = pcpu_freelist_pop(&htab->freelist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 		/* pop will succeed, since prealloc_init()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 		 * preallocated extra num_possible_cpus elements
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 		l_new = container_of(l, struct htab_elem, fnode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 		*per_cpu_ptr(pptr, cpu) = l_new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 	htab->extra_elems = pptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) /* Called from syscall */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) static int htab_map_alloc_check(union bpf_attr *attr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 	bool percpu = (attr->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 		       attr->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 	bool lru = (attr->map_type == BPF_MAP_TYPE_LRU_HASH ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 		    attr->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 	/* percpu_lru means each cpu has its own LRU list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 	 * it is different from BPF_MAP_TYPE_PERCPU_HASH where
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 	 * the map's value itself is percpu.  percpu_lru has
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 	 * nothing to do with the map's value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 	bool percpu_lru = (attr->map_flags & BPF_F_NO_COMMON_LRU);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 	bool prealloc = !(attr->map_flags & BPF_F_NO_PREALLOC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 	bool zero_seed = (attr->map_flags & BPF_F_ZERO_SEED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 	int numa_node = bpf_map_attr_numa_node(attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 	BUILD_BUG_ON(offsetof(struct htab_elem, htab) !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 		     offsetof(struct htab_elem, hash_node.pprev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 	BUILD_BUG_ON(offsetof(struct htab_elem, fnode.next) !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 		     offsetof(struct htab_elem, hash_node.pprev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 	if (lru && !bpf_capable())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 		/* LRU implementation is much complicated than other
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 		 * maps.  Hence, limit to CAP_BPF.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 		return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 	if (zero_seed && !capable(CAP_SYS_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 		/* Guard against local DoS, and discourage production use. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 		return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 	if (attr->map_flags & ~HTAB_CREATE_FLAG_MASK ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) 	    !bpf_map_flags_access_ok(attr->map_flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 	if (!lru && percpu_lru)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) 	if (lru && !prealloc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 		return -ENOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 	if (numa_node != NUMA_NO_NODE && (percpu || percpu_lru))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 	/* check sanity of attributes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 	 * value_size == 0 may be allowed in the future to use map as a set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 	if (attr->max_entries == 0 || attr->key_size == 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 	    attr->value_size == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 	if (attr->key_size > MAX_BPF_STACK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 		/* eBPF programs initialize keys on stack, so they cannot be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 		 * larger than max stack size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 		return -E2BIG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 	if (attr->value_size >= KMALLOC_MAX_SIZE -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 	    MAX_BPF_STACK - sizeof(struct htab_elem))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 		/* if value_size is bigger, the user space won't be able to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 		 * access the elements via bpf syscall. This check also makes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 		 * sure that the elem_size doesn't overflow and it's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 		 * kmalloc-able later in htab_map_update_elem()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 		return -E2BIG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) static struct bpf_map *htab_map_alloc(union bpf_attr *attr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 	bool percpu = (attr->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 		       attr->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 	bool lru = (attr->map_type == BPF_MAP_TYPE_LRU_HASH ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 		    attr->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 	/* percpu_lru means each cpu has its own LRU list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 	 * it is different from BPF_MAP_TYPE_PERCPU_HASH where
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) 	 * the map's value itself is percpu.  percpu_lru has
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 	 * nothing to do with the map's value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 	bool percpu_lru = (attr->map_flags & BPF_F_NO_COMMON_LRU);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 	bool prealloc = !(attr->map_flags & BPF_F_NO_PREALLOC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 	struct bpf_htab *htab;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 	u64 cost;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 	htab = kzalloc(sizeof(*htab), GFP_USER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 	if (!htab)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 	bpf_map_init_from_attr(&htab->map, attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 	if (percpu_lru) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 		/* ensure each CPU's lru list has >=1 elements.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 		 * since we are at it, make each lru list has the same
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 		 * number of elements.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 		htab->map.max_entries = roundup(attr->max_entries,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 						num_possible_cpus());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 		if (htab->map.max_entries < attr->max_entries)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 			htab->map.max_entries = rounddown(attr->max_entries,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 							  num_possible_cpus());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 	/* hash table size must be power of 2 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 	htab->n_buckets = roundup_pow_of_two(htab->map.max_entries);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) 	htab->elem_size = sizeof(struct htab_elem) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) 			  round_up(htab->map.key_size, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 	if (percpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 		htab->elem_size += sizeof(void *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) 		htab->elem_size += round_up(htab->map.value_size, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) 	err = -E2BIG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 	/* prevent zero size kmalloc and check for u32 overflow */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 	if (htab->n_buckets == 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 	    htab->n_buckets > U32_MAX / sizeof(struct bucket))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 		goto free_htab;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 	cost = (u64) htab->n_buckets * sizeof(struct bucket) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 	       (u64) htab->elem_size * htab->map.max_entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 	if (percpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 		cost += (u64) round_up(htab->map.value_size, 8) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 			num_possible_cpus() * htab->map.max_entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 	       cost += (u64) htab->elem_size * num_possible_cpus();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 	/* if map size is larger than memlock limit, reject it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 	err = bpf_map_charge_init(&htab->map.memory, cost);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 		goto free_htab;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 	err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 	htab->buckets = bpf_map_area_alloc(htab->n_buckets *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 					   sizeof(struct bucket),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 					   htab->map.numa_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) 	if (!htab->buckets)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 		goto free_charge;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) 	if (htab->map.map_flags & BPF_F_ZERO_SEED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 		htab->hashrnd = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) 		htab->hashrnd = get_random_int();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 	htab_init_buckets(htab);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) 	if (prealloc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 		err = prealloc_init(htab);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 			goto free_buckets;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 		if (!percpu && !lru) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 			/* lru itself can remove the least used element, so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 			 * there is no need for an extra elem during map_update.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 			err = alloc_extra_elems(htab);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 			if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 				goto free_prealloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 	return &htab->map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) free_prealloc:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 	prealloc_destroy(htab);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) free_buckets:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 	bpf_map_area_free(htab->buckets);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) free_charge:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 	bpf_map_charge_finish(&htab->map.memory);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) free_htab:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 	kfree(htab);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 	return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) static inline u32 htab_map_hash(const void *key, u32 key_len, u32 hashrnd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 	return jhash(key, key_len, hashrnd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) static inline struct bucket *__select_bucket(struct bpf_htab *htab, u32 hash)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 	return &htab->buckets[hash & (htab->n_buckets - 1)];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) static inline struct hlist_nulls_head *select_bucket(struct bpf_htab *htab, u32 hash)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 	return &__select_bucket(htab, hash)->head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) /* this lookup function can only be called with bucket lock taken */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) static struct htab_elem *lookup_elem_raw(struct hlist_nulls_head *head, u32 hash,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 					 void *key, u32 key_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 	struct hlist_nulls_node *n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 	struct htab_elem *l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 	hlist_nulls_for_each_entry_rcu(l, n, head, hash_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 		if (l->hash == hash && !memcmp(&l->key, key, key_size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 			return l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) /* can be called without bucket lock. it will repeat the loop in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548)  * the unlikely event when elements moved from one bucket into another
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549)  * while link list is being walked
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) static struct htab_elem *lookup_nulls_elem_raw(struct hlist_nulls_head *head,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 					       u32 hash, void *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 					       u32 key_size, u32 n_buckets)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 	struct hlist_nulls_node *n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 	struct htab_elem *l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 	hlist_nulls_for_each_entry_rcu(l, n, head, hash_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 		if (l->hash == hash && !memcmp(&l->key, key, key_size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 			return l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 	if (unlikely(get_nulls_value(n) != (hash & (n_buckets - 1))))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 		goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) /* Called from syscall or from eBPF program directly, so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570)  * arguments have to match bpf_map_lookup_elem() exactly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571)  * The return value is adjusted by BPF instructions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572)  * in htab_map_gen_lookup().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) static void *__htab_map_lookup_elem(struct bpf_map *map, void *key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 	struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 	struct hlist_nulls_head *head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 	struct htab_elem *l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 	u32 hash, key_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 	WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_trace_held());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 	key_size = map->key_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 	hash = htab_map_hash(key, key_size, htab->hashrnd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 	head = select_bucket(htab, hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 	l = lookup_nulls_elem_raw(head, hash, key, key_size, htab->n_buckets);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 	return l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) static void *htab_map_lookup_elem(struct bpf_map *map, void *key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 	struct htab_elem *l = __htab_map_lookup_elem(map, key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 	if (l)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 		return l->key + round_up(map->key_size, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) /* inline bpf_map_lookup_elem() call.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605)  * Instead of:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606)  * bpf_prog
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607)  *   bpf_map_lookup_elem
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608)  *     map->ops->map_lookup_elem
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609)  *       htab_map_lookup_elem
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610)  *         __htab_map_lookup_elem
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611)  * do:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612)  * bpf_prog
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613)  *   __htab_map_lookup_elem
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) static int htab_map_gen_lookup(struct bpf_map *map, struct bpf_insn *insn_buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 	struct bpf_insn *insn = insn_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 	const int ret = BPF_REG_0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 	BUILD_BUG_ON(!__same_type(&__htab_map_lookup_elem,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 		     (void *(*)(struct bpf_map *map, void *key))NULL));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 	*insn++ = BPF_EMIT_CALL(BPF_CAST_CALL(__htab_map_lookup_elem));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 	*insn++ = BPF_JMP_IMM(BPF_JEQ, ret, 0, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 	*insn++ = BPF_ALU64_IMM(BPF_ADD, ret,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 				offsetof(struct htab_elem, key) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 				round_up(map->key_size, 8));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 	return insn - insn_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) static __always_inline void *__htab_lru_map_lookup_elem(struct bpf_map *map,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 							void *key, const bool mark)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 	struct htab_elem *l = __htab_map_lookup_elem(map, key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 	if (l) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 		if (mark)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 			bpf_lru_node_set_ref(&l->lru_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 		return l->key + round_up(map->key_size, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) static void *htab_lru_map_lookup_elem(struct bpf_map *map, void *key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 	return __htab_lru_map_lookup_elem(map, key, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) static void *htab_lru_map_lookup_elem_sys(struct bpf_map *map, void *key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 	return __htab_lru_map_lookup_elem(map, key, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) static int htab_lru_map_gen_lookup(struct bpf_map *map,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 				   struct bpf_insn *insn_buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 	struct bpf_insn *insn = insn_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 	const int ret = BPF_REG_0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 	const int ref_reg = BPF_REG_1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 	BUILD_BUG_ON(!__same_type(&__htab_map_lookup_elem,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 		     (void *(*)(struct bpf_map *map, void *key))NULL));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 	*insn++ = BPF_EMIT_CALL(BPF_CAST_CALL(__htab_map_lookup_elem));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 	*insn++ = BPF_JMP_IMM(BPF_JEQ, ret, 0, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 	*insn++ = BPF_LDX_MEM(BPF_B, ref_reg, ret,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 			      offsetof(struct htab_elem, lru_node) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 			      offsetof(struct bpf_lru_node, ref));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 	*insn++ = BPF_JMP_IMM(BPF_JNE, ref_reg, 0, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 	*insn++ = BPF_ST_MEM(BPF_B, ret,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 			     offsetof(struct htab_elem, lru_node) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 			     offsetof(struct bpf_lru_node, ref),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 			     1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 	*insn++ = BPF_ALU64_IMM(BPF_ADD, ret,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 				offsetof(struct htab_elem, key) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 				round_up(map->key_size, 8));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 	return insn - insn_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) /* It is called from the bpf_lru_list when the LRU needs to delete
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680)  * older elements from the htab.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) static bool htab_lru_map_delete_node(void *arg, struct bpf_lru_node *node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 	struct bpf_htab *htab = (struct bpf_htab *)arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 	struct htab_elem *l = NULL, *tgt_l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 	struct hlist_nulls_head *head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 	struct hlist_nulls_node *n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 	struct bucket *b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 	tgt_l = container_of(node, struct htab_elem, lru_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 	b = __select_bucket(htab, tgt_l->hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 	head = &b->head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 	flags = htab_lock_bucket(htab, b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 	hlist_nulls_for_each_entry_rcu(l, n, head, hash_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 		if (l == tgt_l) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 			hlist_nulls_del_rcu(&l->hash_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 	htab_unlock_bucket(htab, b, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 	return l == tgt_l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) /* Called from syscall */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) static int htab_map_get_next_key(struct bpf_map *map, void *key, void *next_key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 	struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) 	struct hlist_nulls_head *head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 	struct htab_elem *l, *next_l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 	u32 hash, key_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 	int i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 	WARN_ON_ONCE(!rcu_read_lock_held());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 	key_size = map->key_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 	if (!key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 		goto find_first_elem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 	hash = htab_map_hash(key, key_size, htab->hashrnd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 	head = select_bucket(htab, hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 	/* lookup the key */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 	l = lookup_nulls_elem_raw(head, hash, key, key_size, htab->n_buckets);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 	if (!l)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 		goto find_first_elem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 	/* key was found, get next key in the same bucket */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 	next_l = hlist_nulls_entry_safe(rcu_dereference_raw(hlist_nulls_next_rcu(&l->hash_node)),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 				  struct htab_elem, hash_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 	if (next_l) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 		/* if next elem in this hash list is non-zero, just return it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 		memcpy(next_key, next_l->key, key_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 	/* no more elements in this hash list, go to the next bucket */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 	i = hash & (htab->n_buckets - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 	i++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) find_first_elem:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 	/* iterate over buckets */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 	for (; i < htab->n_buckets; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 		head = select_bucket(htab, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 		/* pick first element in the bucket */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 		next_l = hlist_nulls_entry_safe(rcu_dereference_raw(hlist_nulls_first_rcu(head)),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 					  struct htab_elem, hash_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 		if (next_l) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 			/* if it's not empty, just return it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 			memcpy(next_key, next_l->key, key_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 	/* iterated over all buckets and all elements */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 	return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) static void htab_elem_free(struct bpf_htab *htab, struct htab_elem *l)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 	if (htab->map.map_type == BPF_MAP_TYPE_PERCPU_HASH)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 		free_percpu(htab_elem_get_ptr(l, htab->map.key_size));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 	kfree(l);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) static void htab_elem_free_rcu(struct rcu_head *head)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 	struct htab_elem *l = container_of(head, struct htab_elem, rcu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 	struct bpf_htab *htab = l->htab;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 	htab_elem_free(htab, l);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) static void htab_put_fd_value(struct bpf_htab *htab, struct htab_elem *l)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 	struct bpf_map *map = &htab->map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) 	void *ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 	if (map->ops->map_fd_put_ptr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 		ptr = fd_htab_map_get_ptr(map, l);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 		map->ops->map_fd_put_ptr(ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) static void free_htab_elem(struct bpf_htab *htab, struct htab_elem *l)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 	htab_put_fd_value(htab, l);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 	if (htab_is_prealloc(htab)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 		__pcpu_freelist_push(&htab->freelist, &l->fnode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 		atomic_dec(&htab->count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 		l->htab = htab;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 		call_rcu(&l->rcu, htab_elem_free_rcu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) static void pcpu_copy_value(struct bpf_htab *htab, void __percpu *pptr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 			    void *value, bool onallcpus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 	if (!onallcpus) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 		/* copy true value_size bytes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 		memcpy(this_cpu_ptr(pptr), value, htab->map.value_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 		u32 size = round_up(htab->map.value_size, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 		int off = 0, cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 		for_each_possible_cpu(cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 			bpf_long_memcpy(per_cpu_ptr(pptr, cpu),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 					value + off, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 			off += size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) static void pcpu_init_value(struct bpf_htab *htab, void __percpu *pptr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 			    void *value, bool onallcpus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 	/* When using prealloc and not setting the initial value on all cpus,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 	 * zero-fill element values for other cpus (just as what happens when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 	 * not using prealloc). Otherwise, bpf program has no way to ensure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 	 * known initial values for cpus other than current one
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 	 * (onallcpus=false always when coming from bpf prog).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 	if (htab_is_prealloc(htab) && !onallcpus) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 		u32 size = round_up(htab->map.value_size, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 		int current_cpu = raw_smp_processor_id();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 		int cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 		for_each_possible_cpu(cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 			if (cpu == current_cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 				bpf_long_memcpy(per_cpu_ptr(pptr, cpu), value,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 						size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 				memset(per_cpu_ptr(pptr, cpu), 0, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 		pcpu_copy_value(htab, pptr, value, onallcpus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) static bool fd_htab_map_needs_adjust(const struct bpf_htab *htab)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 	return htab->map.map_type == BPF_MAP_TYPE_HASH_OF_MAPS &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 	       BITS_PER_LONG == 64;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) static struct htab_elem *alloc_htab_elem(struct bpf_htab *htab, void *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 					 void *value, u32 key_size, u32 hash,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 					 bool percpu, bool onallcpus,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 					 struct htab_elem *old_elem)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 	u32 size = htab->map.value_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 	bool prealloc = htab_is_prealloc(htab);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 	struct htab_elem *l_new, **pl_new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 	void __percpu *pptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 	if (prealloc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 		if (old_elem) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 			/* if we're updating the existing element,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 			 * use per-cpu extra elems to avoid freelist_pop/push
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 			pl_new = this_cpu_ptr(htab->extra_elems);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 			l_new = *pl_new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 			htab_put_fd_value(htab, old_elem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 			*pl_new = old_elem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 			struct pcpu_freelist_node *l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 			l = __pcpu_freelist_pop(&htab->freelist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 			if (!l)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 				return ERR_PTR(-E2BIG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 			l_new = container_of(l, struct htab_elem, fnode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 		if (atomic_inc_return(&htab->count) > htab->map.max_entries)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 			if (!old_elem) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 				/* when map is full and update() is replacing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 				 * old element, it's ok to allocate, since
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 				 * old element will be freed immediately.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 				 * Otherwise return an error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 				 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 				l_new = ERR_PTR(-E2BIG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 				goto dec_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 		l_new = kmalloc_node(htab->elem_size, GFP_ATOMIC | __GFP_NOWARN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 				     htab->map.numa_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 		if (!l_new) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 			l_new = ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 			goto dec_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 		check_and_init_map_lock(&htab->map,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 					l_new->key + round_up(key_size, 8));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 	memcpy(l_new->key, key, key_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 	if (percpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 		size = round_up(size, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 		if (prealloc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 			pptr = htab_elem_get_ptr(l_new, key_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 			/* alloc_percpu zero-fills */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 			pptr = __alloc_percpu_gfp(size, 8,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 						  GFP_ATOMIC | __GFP_NOWARN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 			if (!pptr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 				kfree(l_new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 				l_new = ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 				goto dec_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 		pcpu_init_value(htab, pptr, value, onallcpus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 		if (!prealloc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 			htab_elem_set_ptr(l_new, key_size, pptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 	} else if (fd_htab_map_needs_adjust(htab)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 		size = round_up(size, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 		memcpy(l_new->key + round_up(key_size, 8), value, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 		copy_map_value(&htab->map,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 			       l_new->key + round_up(key_size, 8),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 			       value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 	l_new->hash = hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 	return l_new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) dec_count:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 	atomic_dec(&htab->count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 	return l_new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) static int check_flags(struct bpf_htab *htab, struct htab_elem *l_old,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 		       u64 map_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 	if (l_old && (map_flags & ~BPF_F_LOCK) == BPF_NOEXIST)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 		/* elem already exists */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 		return -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 	if (!l_old && (map_flags & ~BPF_F_LOCK) == BPF_EXIST)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 		/* elem doesn't exist, cannot update it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 		return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) /* Called from syscall or from eBPF program */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) static int htab_map_update_elem(struct bpf_map *map, void *key, void *value,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 				u64 map_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 	struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 	struct htab_elem *l_new = NULL, *l_old;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 	struct hlist_nulls_head *head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 	struct bucket *b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 	u32 key_size, hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 	if (unlikely((map_flags & ~BPF_F_LOCK) > BPF_EXIST))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 		/* unknown flags */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 	WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_trace_held());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 	key_size = map->key_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 	hash = htab_map_hash(key, key_size, htab->hashrnd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 	b = __select_bucket(htab, hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 	head = &b->head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 	if (unlikely(map_flags & BPF_F_LOCK)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 		if (unlikely(!map_value_has_spin_lock(map)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 		/* find an element without taking the bucket lock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 		l_old = lookup_nulls_elem_raw(head, hash, key, key_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 					      htab->n_buckets);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 		ret = check_flags(htab, l_old, map_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 		if (l_old) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 			/* grab the element lock and update value in place */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 			copy_map_value_locked(map,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 					      l_old->key + round_up(key_size, 8),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 					      value, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 		/* fall through, grab the bucket lock and lookup again.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 		 * 99.9% chance that the element won't be found,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 		 * but second lookup under lock has to be done.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 	flags = htab_lock_bucket(htab, b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 	l_old = lookup_elem_raw(head, hash, key, key_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 	ret = check_flags(htab, l_old, map_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 	if (unlikely(l_old && (map_flags & BPF_F_LOCK))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 		/* first lookup without the bucket lock didn't find the element,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 		 * but second lookup with the bucket lock found it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 		 * This case is highly unlikely, but has to be dealt with:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 		 * grab the element lock in addition to the bucket lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 		 * and update element in place
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 		copy_map_value_locked(map,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 				      l_old->key + round_up(key_size, 8),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 				      value, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 		ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 	l_new = alloc_htab_elem(htab, key, value, key_size, hash, false, false,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 				l_old);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 	if (IS_ERR(l_new)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 		/* all pre-allocated elements are in use or memory exhausted */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 		ret = PTR_ERR(l_new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 	/* add new element to the head of the list, so that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 	 * concurrent search will find it before old elem
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 	hlist_nulls_add_head_rcu(&l_new->hash_node, head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 	if (l_old) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 		hlist_nulls_del_rcu(&l_old->hash_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 		if (!htab_is_prealloc(htab))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 			free_htab_elem(htab, l_old);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 	ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 	htab_unlock_bucket(htab, b, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) static int htab_lru_map_update_elem(struct bpf_map *map, void *key, void *value,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 				    u64 map_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 	struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 	struct htab_elem *l_new, *l_old = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 	struct hlist_nulls_head *head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 	struct bucket *b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 	u32 key_size, hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 	if (unlikely(map_flags > BPF_EXIST))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 		/* unknown flags */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) 	WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_trace_held());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 	key_size = map->key_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 	hash = htab_map_hash(key, key_size, htab->hashrnd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) 	b = __select_bucket(htab, hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 	head = &b->head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 	/* For LRU, we need to alloc before taking bucket's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 	 * spinlock because getting free nodes from LRU may need
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 	 * to remove older elements from htab and this removal
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) 	 * operation will need a bucket lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) 	l_new = prealloc_lru_pop(htab, key, hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) 	if (!l_new)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 	memcpy(l_new->key + round_up(map->key_size, 8), value, map->value_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) 	flags = htab_lock_bucket(htab, b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 	l_old = lookup_elem_raw(head, hash, key, key_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 	ret = check_flags(htab, l_old, map_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 	/* add new element to the head of the list, so that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 	 * concurrent search will find it before old elem
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 	hlist_nulls_add_head_rcu(&l_new->hash_node, head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) 	if (l_old) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) 		bpf_lru_node_set_ref(&l_new->lru_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 		hlist_nulls_del_rcu(&l_old->hash_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) 	ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) 	htab_unlock_bucket(htab, b, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) 		bpf_lru_push_free(&htab->lru, &l_new->lru_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) 	else if (l_old)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 		bpf_lru_push_free(&htab->lru, &l_old->lru_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) static int __htab_percpu_map_update_elem(struct bpf_map *map, void *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) 					 void *value, u64 map_flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) 					 bool onallcpus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) 	struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 	struct htab_elem *l_new = NULL, *l_old;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 	struct hlist_nulls_head *head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) 	struct bucket *b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) 	u32 key_size, hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 	if (unlikely(map_flags > BPF_EXIST))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) 		/* unknown flags */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 	WARN_ON_ONCE(!rcu_read_lock_held());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) 	key_size = map->key_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) 	hash = htab_map_hash(key, key_size, htab->hashrnd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 	b = __select_bucket(htab, hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 	head = &b->head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) 	flags = htab_lock_bucket(htab, b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 	l_old = lookup_elem_raw(head, hash, key, key_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 	ret = check_flags(htab, l_old, map_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) 	if (l_old) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 		/* per-cpu hash map can update value in-place */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 		pcpu_copy_value(htab, htab_elem_get_ptr(l_old, key_size),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 				value, onallcpus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) 		l_new = alloc_htab_elem(htab, key, value, key_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 					hash, true, onallcpus, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 		if (IS_ERR(l_new)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) 			ret = PTR_ERR(l_new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) 			goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) 		hlist_nulls_add_head_rcu(&l_new->hash_node, head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 	ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 	htab_unlock_bucket(htab, b, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) static int __htab_lru_percpu_map_update_elem(struct bpf_map *map, void *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) 					     void *value, u64 map_flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) 					     bool onallcpus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) 	struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) 	struct htab_elem *l_new = NULL, *l_old;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) 	struct hlist_nulls_head *head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) 	struct bucket *b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) 	u32 key_size, hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) 	if (unlikely(map_flags > BPF_EXIST))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) 		/* unknown flags */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) 	WARN_ON_ONCE(!rcu_read_lock_held());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) 	key_size = map->key_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) 	hash = htab_map_hash(key, key_size, htab->hashrnd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) 	b = __select_bucket(htab, hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) 	head = &b->head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) 	/* For LRU, we need to alloc before taking bucket's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) 	 * spinlock because LRU's elem alloc may need
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) 	 * to remove older elem from htab and this removal
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) 	 * operation will need a bucket lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) 	if (map_flags != BPF_EXIST) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) 		l_new = prealloc_lru_pop(htab, key, hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) 		if (!l_new)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) 			return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) 	flags = htab_lock_bucket(htab, b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) 	l_old = lookup_elem_raw(head, hash, key, key_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) 	ret = check_flags(htab, l_old, map_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) 	if (l_old) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) 		bpf_lru_node_set_ref(&l_old->lru_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) 		/* per-cpu hash map can update value in-place */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) 		pcpu_copy_value(htab, htab_elem_get_ptr(l_old, key_size),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) 				value, onallcpus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) 		pcpu_init_value(htab, htab_elem_get_ptr(l_new, key_size),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) 				value, onallcpus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) 		hlist_nulls_add_head_rcu(&l_new->hash_node, head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) 		l_new = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) 	ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) 	htab_unlock_bucket(htab, b, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) 	if (l_new)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) 		bpf_lru_push_free(&htab->lru, &l_new->lru_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) static int htab_percpu_map_update_elem(struct bpf_map *map, void *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) 				       void *value, u64 map_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) 	return __htab_percpu_map_update_elem(map, key, value, map_flags, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) static int htab_lru_percpu_map_update_elem(struct bpf_map *map, void *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) 					   void *value, u64 map_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) 	return __htab_lru_percpu_map_update_elem(map, key, value, map_flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) 						 false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) /* Called from syscall or from eBPF program */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) static int htab_map_delete_elem(struct bpf_map *map, void *key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) 	struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) 	struct hlist_nulls_head *head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) 	struct bucket *b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) 	struct htab_elem *l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) 	u32 hash, key_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) 	int ret = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) 	WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_trace_held());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) 	key_size = map->key_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) 	hash = htab_map_hash(key, key_size, htab->hashrnd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) 	b = __select_bucket(htab, hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) 	head = &b->head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) 	flags = htab_lock_bucket(htab, b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) 	l = lookup_elem_raw(head, hash, key, key_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) 	if (l) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) 		hlist_nulls_del_rcu(&l->hash_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) 		free_htab_elem(htab, l);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) 		ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) 	htab_unlock_bucket(htab, b, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) static int htab_lru_map_delete_elem(struct bpf_map *map, void *key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) 	struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) 	struct hlist_nulls_head *head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) 	struct bucket *b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) 	struct htab_elem *l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) 	u32 hash, key_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) 	int ret = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) 	WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_trace_held());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) 	key_size = map->key_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) 	hash = htab_map_hash(key, key_size, htab->hashrnd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) 	b = __select_bucket(htab, hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) 	head = &b->head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) 	flags = htab_lock_bucket(htab, b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) 	l = lookup_elem_raw(head, hash, key, key_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) 	if (l) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) 		hlist_nulls_del_rcu(&l->hash_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) 		ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) 	htab_unlock_bucket(htab, b, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) 	if (l)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) 		bpf_lru_push_free(&htab->lru, &l->lru_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) static void delete_all_elements(struct bpf_htab *htab)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) 	for (i = 0; i < htab->n_buckets; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) 		struct hlist_nulls_head *head = select_bucket(htab, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) 		struct hlist_nulls_node *n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) 		struct htab_elem *l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) 		hlist_nulls_for_each_entry_safe(l, n, head, hash_node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) 			hlist_nulls_del_rcu(&l->hash_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) 			htab_elem_free(htab, l);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) /* Called when map->refcnt goes to zero, either from workqueue or from syscall */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) static void htab_map_free(struct bpf_map *map)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) 	struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) 	/* bpf_free_used_maps() or close(map_fd) will trigger this map_free callback.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) 	 * bpf_free_used_maps() is called after bpf prog is no longer executing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) 	 * There is no need to synchronize_rcu() here to protect map elements.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) 	/* some of free_htab_elem() callbacks for elements of this map may
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) 	 * not have executed. Wait for them.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) 	rcu_barrier();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) 	if (!htab_is_prealloc(htab))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) 		delete_all_elements(htab);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) 		prealloc_destroy(htab);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) 	free_percpu(htab->extra_elems);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) 	bpf_map_area_free(htab->buckets);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) 	kfree(htab);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) static void htab_map_seq_show_elem(struct bpf_map *map, void *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) 				   struct seq_file *m)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) 	void *value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) 	rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) 	value = htab_map_lookup_elem(map, key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) 	if (!value) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) 		rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) 	btf_type_seq_show(map->btf, map->btf_key_type_id, key, m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) 	seq_puts(m, ": ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) 	btf_type_seq_show(map->btf, map->btf_value_type_id, value, m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) 	seq_puts(m, "\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) 	rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) __htab_map_lookup_and_delete_batch(struct bpf_map *map,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) 				   const union bpf_attr *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) 				   union bpf_attr __user *uattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) 				   bool do_delete, bool is_lru_map,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) 				   bool is_percpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) 	struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) 	u32 bucket_cnt, total, key_size, value_size, roundup_key_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) 	void *keys = NULL, *values = NULL, *value, *dst_key, *dst_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) 	void __user *uvalues = u64_to_user_ptr(attr->batch.values);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) 	void __user *ukeys = u64_to_user_ptr(attr->batch.keys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) 	void *ubatch = u64_to_user_ptr(attr->batch.in_batch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) 	u32 batch, max_count, size, bucket_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) 	struct htab_elem *node_to_free = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) 	u64 elem_map_flags, map_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) 	struct hlist_nulls_head *head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) 	struct hlist_nulls_node *n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) 	unsigned long flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) 	bool locked = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) 	struct htab_elem *l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) 	struct bucket *b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) 	elem_map_flags = attr->batch.elem_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) 	if ((elem_map_flags & ~BPF_F_LOCK) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) 	    ((elem_map_flags & BPF_F_LOCK) && !map_value_has_spin_lock(map)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) 	map_flags = attr->batch.flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) 	if (map_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) 	max_count = attr->batch.count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) 	if (!max_count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) 	if (put_user(0, &uattr->batch.count))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) 	batch = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) 	if (ubatch && copy_from_user(&batch, ubatch, sizeof(batch)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) 	if (batch >= htab->n_buckets)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) 		return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) 	key_size = htab->map.key_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) 	roundup_key_size = round_up(htab->map.key_size, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) 	value_size = htab->map.value_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) 	size = round_up(value_size, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) 	if (is_percpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) 		value_size = size * num_possible_cpus();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) 	total = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) 	/* while experimenting with hash tables with sizes ranging from 10 to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) 	 * 1000, it was observed that a bucket can have upto 5 entries.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) 	bucket_size = 5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) alloc:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) 	/* We cannot do copy_from_user or copy_to_user inside
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) 	 * the rcu_read_lock. Allocate enough space here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) 	keys = kvmalloc_array(key_size, bucket_size, GFP_USER | __GFP_NOWARN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) 	values = kvmalloc_array(value_size, bucket_size, GFP_USER | __GFP_NOWARN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) 	if (!keys || !values) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) 		goto after_loop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) 	bpf_disable_instrumentation();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) 	rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) again_nocopy:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) 	dst_key = keys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) 	dst_val = values;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) 	b = &htab->buckets[batch];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) 	head = &b->head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) 	/* do not grab the lock unless need it (bucket_cnt > 0). */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) 	if (locked)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) 		flags = htab_lock_bucket(htab, b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) 	bucket_cnt = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) 	hlist_nulls_for_each_entry_rcu(l, n, head, hash_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) 		bucket_cnt++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) 	if (bucket_cnt && !locked) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) 		locked = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) 		goto again_nocopy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) 	if (bucket_cnt > (max_count - total)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) 		if (total == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) 			ret = -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) 		/* Note that since bucket_cnt > 0 here, it is implicit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) 		 * that the locked was grabbed, so release it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) 		htab_unlock_bucket(htab, b, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) 		rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) 		bpf_enable_instrumentation();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) 		goto after_loop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) 	if (bucket_cnt > bucket_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) 		bucket_size = bucket_cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) 		/* Note that since bucket_cnt > 0 here, it is implicit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) 		 * that the locked was grabbed, so release it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) 		htab_unlock_bucket(htab, b, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) 		rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) 		bpf_enable_instrumentation();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) 		kvfree(keys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) 		kvfree(values);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) 		goto alloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) 	/* Next block is only safe to run if you have grabbed the lock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) 	if (!locked)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) 		goto next_batch;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) 	hlist_nulls_for_each_entry_safe(l, n, head, hash_node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) 		memcpy(dst_key, l->key, key_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) 		if (is_percpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) 			int off = 0, cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) 			void __percpu *pptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) 			pptr = htab_elem_get_ptr(l, map->key_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) 			for_each_possible_cpu(cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) 				bpf_long_memcpy(dst_val + off,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) 						per_cpu_ptr(pptr, cpu), size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) 				off += size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) 			value = l->key + roundup_key_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) 			if (elem_map_flags & BPF_F_LOCK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) 				copy_map_value_locked(map, dst_val, value,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) 						      true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) 				copy_map_value(map, dst_val, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) 			check_and_init_map_lock(map, dst_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) 		if (do_delete) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) 			hlist_nulls_del_rcu(&l->hash_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) 			/* bpf_lru_push_free() will acquire lru_lock, which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) 			 * may cause deadlock. See comments in function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) 			 * prealloc_lru_pop(). Let us do bpf_lru_push_free()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) 			 * after releasing the bucket lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) 			if (is_lru_map) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) 				l->batch_flink = node_to_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) 				node_to_free = l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) 				free_htab_elem(htab, l);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) 		dst_key += key_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) 		dst_val += value_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) 	htab_unlock_bucket(htab, b, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) 	locked = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) 	while (node_to_free) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) 		l = node_to_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) 		node_to_free = node_to_free->batch_flink;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) 		bpf_lru_push_free(&htab->lru, &l->lru_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) next_batch:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) 	/* If we are not copying data, we can go to next bucket and avoid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) 	 * unlocking the rcu.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) 	if (!bucket_cnt && (batch + 1 < htab->n_buckets)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) 		batch++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) 		goto again_nocopy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) 	rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) 	bpf_enable_instrumentation();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) 	if (bucket_cnt && (copy_to_user(ukeys + total * key_size, keys,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) 	    key_size * bucket_cnt) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) 	    copy_to_user(uvalues + total * value_size, values,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) 	    value_size * bucket_cnt))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) 		ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) 		goto after_loop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) 	total += bucket_cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) 	batch++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) 	if (batch >= htab->n_buckets) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) 		ret = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) 		goto after_loop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) 	goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) after_loop:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) 	if (ret == -EFAULT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) 	/* copy # of entries and next batch */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) 	ubatch = u64_to_user_ptr(attr->batch.out_batch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) 	if (copy_to_user(ubatch, &batch, sizeof(batch)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) 	    put_user(total, &uattr->batch.count))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) 		ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) 	kvfree(keys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) 	kvfree(values);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) htab_percpu_map_lookup_batch(struct bpf_map *map, const union bpf_attr *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) 			     union bpf_attr __user *uattr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) 	return __htab_map_lookup_and_delete_batch(map, attr, uattr, false,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) 						  false, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) htab_percpu_map_lookup_and_delete_batch(struct bpf_map *map,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) 					const union bpf_attr *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) 					union bpf_attr __user *uattr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) 	return __htab_map_lookup_and_delete_batch(map, attr, uattr, true,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) 						  false, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) htab_map_lookup_batch(struct bpf_map *map, const union bpf_attr *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) 		      union bpf_attr __user *uattr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) 	return __htab_map_lookup_and_delete_batch(map, attr, uattr, false,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) 						  false, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) htab_map_lookup_and_delete_batch(struct bpf_map *map,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) 				 const union bpf_attr *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) 				 union bpf_attr __user *uattr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) 	return __htab_map_lookup_and_delete_batch(map, attr, uattr, true,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) 						  false, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) htab_lru_percpu_map_lookup_batch(struct bpf_map *map,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) 				 const union bpf_attr *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) 				 union bpf_attr __user *uattr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) 	return __htab_map_lookup_and_delete_batch(map, attr, uattr, false,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) 						  true, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) htab_lru_percpu_map_lookup_and_delete_batch(struct bpf_map *map,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) 					    const union bpf_attr *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) 					    union bpf_attr __user *uattr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) 	return __htab_map_lookup_and_delete_batch(map, attr, uattr, true,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) 						  true, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) htab_lru_map_lookup_batch(struct bpf_map *map, const union bpf_attr *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) 			  union bpf_attr __user *uattr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) 	return __htab_map_lookup_and_delete_batch(map, attr, uattr, false,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) 						  true, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) htab_lru_map_lookup_and_delete_batch(struct bpf_map *map,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) 				     const union bpf_attr *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) 				     union bpf_attr __user *uattr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) 	return __htab_map_lookup_and_delete_batch(map, attr, uattr, true,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) 						  true, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) struct bpf_iter_seq_hash_map_info {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) 	struct bpf_map *map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) 	struct bpf_htab *htab;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) 	void *percpu_value_buf; // non-zero means percpu hash
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) 	u32 bucket_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) 	u32 skip_elems;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) static struct htab_elem *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) bpf_hash_map_seq_find_next(struct bpf_iter_seq_hash_map_info *info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) 			   struct htab_elem *prev_elem)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) 	const struct bpf_htab *htab = info->htab;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) 	u32 skip_elems = info->skip_elems;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) 	u32 bucket_id = info->bucket_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) 	struct hlist_nulls_head *head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) 	struct hlist_nulls_node *n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) 	struct htab_elem *elem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) 	struct bucket *b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) 	u32 i, count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) 	if (bucket_id >= htab->n_buckets)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) 	/* try to find next elem in the same bucket */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) 	if (prev_elem) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) 		/* no update/deletion on this bucket, prev_elem should be still valid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674) 		 * and we won't skip elements.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) 		n = rcu_dereference_raw(hlist_nulls_next_rcu(&prev_elem->hash_node));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) 		elem = hlist_nulls_entry_safe(n, struct htab_elem, hash_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678) 		if (elem)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) 			return elem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) 		/* not found, unlock and go to the next bucket */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) 		b = &htab->buckets[bucket_id++];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) 		rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) 		skip_elems = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) 	for (i = bucket_id; i < htab->n_buckets; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688) 		b = &htab->buckets[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) 		rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) 		count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692) 		head = &b->head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) 		hlist_nulls_for_each_entry_rcu(elem, n, head, hash_node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) 			if (count >= skip_elems) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) 				info->bucket_id = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696) 				info->skip_elems = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) 				return elem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699) 			count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702) 		rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) 		skip_elems = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706) 	info->bucket_id = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707) 	info->skip_elems = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711) static void *bpf_hash_map_seq_start(struct seq_file *seq, loff_t *pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713) 	struct bpf_iter_seq_hash_map_info *info = seq->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714) 	struct htab_elem *elem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) 	elem = bpf_hash_map_seq_find_next(info, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717) 	if (!elem)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720) 	if (*pos == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721) 		++*pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722) 	return elem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725) static void *bpf_hash_map_seq_next(struct seq_file *seq, void *v, loff_t *pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727) 	struct bpf_iter_seq_hash_map_info *info = seq->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729) 	++*pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) 	++info->skip_elems;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731) 	return bpf_hash_map_seq_find_next(info, v);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734) static int __bpf_hash_map_seq_show(struct seq_file *seq, struct htab_elem *elem)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736) 	struct bpf_iter_seq_hash_map_info *info = seq->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) 	u32 roundup_key_size, roundup_value_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738) 	struct bpf_iter__bpf_map_elem ctx = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739) 	struct bpf_map *map = info->map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740) 	struct bpf_iter_meta meta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741) 	int ret = 0, off = 0, cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) 	struct bpf_prog *prog;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743) 	void __percpu *pptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745) 	meta.seq = seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) 	prog = bpf_iter_get_info(&meta, elem == NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747) 	if (prog) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) 		ctx.meta = &meta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749) 		ctx.map = info->map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750) 		if (elem) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751) 			roundup_key_size = round_up(map->key_size, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752) 			ctx.key = elem->key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753) 			if (!info->percpu_value_buf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754) 				ctx.value = elem->key + roundup_key_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) 				roundup_value_size = round_up(map->value_size, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757) 				pptr = htab_elem_get_ptr(elem, map->key_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758) 				for_each_possible_cpu(cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759) 					bpf_long_memcpy(info->percpu_value_buf + off,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760) 							per_cpu_ptr(pptr, cpu),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761) 							roundup_value_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762) 					off += roundup_value_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764) 				ctx.value = info->percpu_value_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767) 		ret = bpf_iter_run_prog(prog, &ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773) static int bpf_hash_map_seq_show(struct seq_file *seq, void *v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775) 	return __bpf_hash_map_seq_show(seq, v);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778) static void bpf_hash_map_seq_stop(struct seq_file *seq, void *v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780) 	if (!v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781) 		(void)__bpf_hash_map_seq_show(seq, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783) 		rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786) static int bpf_iter_init_hash_map(void *priv_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787) 				  struct bpf_iter_aux_info *aux)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789) 	struct bpf_iter_seq_hash_map_info *seq_info = priv_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790) 	struct bpf_map *map = aux->map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791) 	void *value_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792) 	u32 buf_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794) 	if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795) 	    map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796) 		buf_size = round_up(map->value_size, 8) * num_possible_cpus();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1797) 		value_buf = kmalloc(buf_size, GFP_USER | __GFP_NOWARN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1798) 		if (!value_buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1799) 			return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1800) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801) 		seq_info->percpu_value_buf = value_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804) 	seq_info->map = map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805) 	seq_info->htab = container_of(map, struct bpf_htab, map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1806) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1807) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1808) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1809) static void bpf_iter_fini_hash_map(void *priv_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1810) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1811) 	struct bpf_iter_seq_hash_map_info *seq_info = priv_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1812) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1813) 	kfree(seq_info->percpu_value_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1814) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1815) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1816) static const struct seq_operations bpf_hash_map_seq_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1817) 	.start	= bpf_hash_map_seq_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1818) 	.next	= bpf_hash_map_seq_next,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1819) 	.stop	= bpf_hash_map_seq_stop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1820) 	.show	= bpf_hash_map_seq_show,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1821) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1822) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1823) static const struct bpf_iter_seq_info iter_seq_info = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1824) 	.seq_ops		= &bpf_hash_map_seq_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1825) 	.init_seq_private	= bpf_iter_init_hash_map,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1826) 	.fini_seq_private	= bpf_iter_fini_hash_map,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1827) 	.seq_priv_size		= sizeof(struct bpf_iter_seq_hash_map_info),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1828) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1829) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1830) static int htab_map_btf_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1831) const struct bpf_map_ops htab_map_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1832) 	.map_meta_equal = bpf_map_meta_equal,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1833) 	.map_alloc_check = htab_map_alloc_check,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1834) 	.map_alloc = htab_map_alloc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1835) 	.map_free = htab_map_free,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1836) 	.map_get_next_key = htab_map_get_next_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1837) 	.map_lookup_elem = htab_map_lookup_elem,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1838) 	.map_update_elem = htab_map_update_elem,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1839) 	.map_delete_elem = htab_map_delete_elem,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1840) 	.map_gen_lookup = htab_map_gen_lookup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1841) 	.map_seq_show_elem = htab_map_seq_show_elem,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1842) 	BATCH_OPS(htab),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1843) 	.map_btf_name = "bpf_htab",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1844) 	.map_btf_id = &htab_map_btf_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1845) 	.iter_seq_info = &iter_seq_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1846) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1847) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1848) static int htab_lru_map_btf_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1849) const struct bpf_map_ops htab_lru_map_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1850) 	.map_meta_equal = bpf_map_meta_equal,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1851) 	.map_alloc_check = htab_map_alloc_check,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1852) 	.map_alloc = htab_map_alloc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1853) 	.map_free = htab_map_free,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1854) 	.map_get_next_key = htab_map_get_next_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1855) 	.map_lookup_elem = htab_lru_map_lookup_elem,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1856) 	.map_lookup_elem_sys_only = htab_lru_map_lookup_elem_sys,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1857) 	.map_update_elem = htab_lru_map_update_elem,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1858) 	.map_delete_elem = htab_lru_map_delete_elem,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1859) 	.map_gen_lookup = htab_lru_map_gen_lookup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1860) 	.map_seq_show_elem = htab_map_seq_show_elem,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1861) 	BATCH_OPS(htab_lru),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1862) 	.map_btf_name = "bpf_htab",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1863) 	.map_btf_id = &htab_lru_map_btf_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1864) 	.iter_seq_info = &iter_seq_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1865) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1866) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1867) /* Called from eBPF program */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1868) static void *htab_percpu_map_lookup_elem(struct bpf_map *map, void *key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1869) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1870) 	struct htab_elem *l = __htab_map_lookup_elem(map, key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1871) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1872) 	if (l)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1873) 		return this_cpu_ptr(htab_elem_get_ptr(l, map->key_size));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1874) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1875) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1876) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1877) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1878) static void *htab_lru_percpu_map_lookup_elem(struct bpf_map *map, void *key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1879) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1880) 	struct htab_elem *l = __htab_map_lookup_elem(map, key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1881) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1882) 	if (l) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1883) 		bpf_lru_node_set_ref(&l->lru_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1884) 		return this_cpu_ptr(htab_elem_get_ptr(l, map->key_size));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1885) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1886) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1887) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1888) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1889) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1890) int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1891) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1892) 	struct htab_elem *l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1893) 	void __percpu *pptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1894) 	int ret = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1895) 	int cpu, off = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1896) 	u32 size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1897) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1898) 	/* per_cpu areas are zero-filled and bpf programs can only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1899) 	 * access 'value_size' of them, so copying rounded areas
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1900) 	 * will not leak any kernel data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1901) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1902) 	size = round_up(map->value_size, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1903) 	rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1904) 	l = __htab_map_lookup_elem(map, key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1905) 	if (!l)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1906) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1907) 	/* We do not mark LRU map element here in order to not mess up
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1908) 	 * eviction heuristics when user space does a map walk.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1909) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1910) 	pptr = htab_elem_get_ptr(l, map->key_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1911) 	for_each_possible_cpu(cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1912) 		bpf_long_memcpy(value + off,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1913) 				per_cpu_ptr(pptr, cpu), size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1914) 		off += size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1915) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1916) 	ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1917) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1918) 	rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1919) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1920) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1921) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1922) int bpf_percpu_hash_update(struct bpf_map *map, void *key, void *value,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1923) 			   u64 map_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1924) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1925) 	struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1926) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1927) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1928) 	rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1929) 	if (htab_is_lru(htab))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1930) 		ret = __htab_lru_percpu_map_update_elem(map, key, value,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1931) 							map_flags, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1932) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1933) 		ret = __htab_percpu_map_update_elem(map, key, value, map_flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1934) 						    true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1935) 	rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1936) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1937) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1938) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1939) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1940) static void htab_percpu_map_seq_show_elem(struct bpf_map *map, void *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1941) 					  struct seq_file *m)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1942) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1943) 	struct htab_elem *l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1944) 	void __percpu *pptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1945) 	int cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1946) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1947) 	rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1948) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1949) 	l = __htab_map_lookup_elem(map, key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1950) 	if (!l) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1951) 		rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1952) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1953) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1954) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1955) 	btf_type_seq_show(map->btf, map->btf_key_type_id, key, m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1956) 	seq_puts(m, ": {\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1957) 	pptr = htab_elem_get_ptr(l, map->key_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1958) 	for_each_possible_cpu(cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1959) 		seq_printf(m, "\tcpu%d: ", cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1960) 		btf_type_seq_show(map->btf, map->btf_value_type_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1961) 				  per_cpu_ptr(pptr, cpu), m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1962) 		seq_puts(m, "\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1963) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1964) 	seq_puts(m, "}\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1965) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1966) 	rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1967) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1968) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1969) static int htab_percpu_map_btf_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1970) const struct bpf_map_ops htab_percpu_map_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1971) 	.map_meta_equal = bpf_map_meta_equal,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1972) 	.map_alloc_check = htab_map_alloc_check,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1973) 	.map_alloc = htab_map_alloc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1974) 	.map_free = htab_map_free,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1975) 	.map_get_next_key = htab_map_get_next_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1976) 	.map_lookup_elem = htab_percpu_map_lookup_elem,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1977) 	.map_update_elem = htab_percpu_map_update_elem,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1978) 	.map_delete_elem = htab_map_delete_elem,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1979) 	.map_seq_show_elem = htab_percpu_map_seq_show_elem,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1980) 	BATCH_OPS(htab_percpu),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1981) 	.map_btf_name = "bpf_htab",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1982) 	.map_btf_id = &htab_percpu_map_btf_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1983) 	.iter_seq_info = &iter_seq_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1984) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1985) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1986) static int htab_lru_percpu_map_btf_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1987) const struct bpf_map_ops htab_lru_percpu_map_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1988) 	.map_meta_equal = bpf_map_meta_equal,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1989) 	.map_alloc_check = htab_map_alloc_check,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1990) 	.map_alloc = htab_map_alloc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1991) 	.map_free = htab_map_free,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1992) 	.map_get_next_key = htab_map_get_next_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1993) 	.map_lookup_elem = htab_lru_percpu_map_lookup_elem,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1994) 	.map_update_elem = htab_lru_percpu_map_update_elem,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1995) 	.map_delete_elem = htab_lru_map_delete_elem,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1996) 	.map_seq_show_elem = htab_percpu_map_seq_show_elem,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1997) 	BATCH_OPS(htab_lru_percpu),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1998) 	.map_btf_name = "bpf_htab",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1999) 	.map_btf_id = &htab_lru_percpu_map_btf_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2000) 	.iter_seq_info = &iter_seq_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2001) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2002) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2003) static int fd_htab_map_alloc_check(union bpf_attr *attr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2004) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2005) 	if (attr->value_size != sizeof(u32))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2006) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2007) 	return htab_map_alloc_check(attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2008) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2009) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2010) static void fd_htab_map_free(struct bpf_map *map)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2011) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2012) 	struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2013) 	struct hlist_nulls_node *n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2014) 	struct hlist_nulls_head *head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2015) 	struct htab_elem *l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2016) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2017) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2018) 	for (i = 0; i < htab->n_buckets; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2019) 		head = select_bucket(htab, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2020) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2021) 		hlist_nulls_for_each_entry_safe(l, n, head, hash_node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2022) 			void *ptr = fd_htab_map_get_ptr(map, l);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2023) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2024) 			map->ops->map_fd_put_ptr(ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2025) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2026) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2027) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2028) 	htab_map_free(map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2029) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2030) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2031) /* only called from syscall */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2032) int bpf_fd_htab_map_lookup_elem(struct bpf_map *map, void *key, u32 *value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2033) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2034) 	void **ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2035) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2036) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2037) 	if (!map->ops->map_fd_sys_lookup_elem)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2038) 		return -ENOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2039) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2040) 	rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2041) 	ptr = htab_map_lookup_elem(map, key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2042) 	if (ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2043) 		*value = map->ops->map_fd_sys_lookup_elem(READ_ONCE(*ptr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2044) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2045) 		ret = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2046) 	rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2047) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2048) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2049) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2050) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2051) /* only called from syscall */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2052) int bpf_fd_htab_map_update_elem(struct bpf_map *map, struct file *map_file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2053) 				void *key, void *value, u64 map_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2054) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2055) 	void *ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2056) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2057) 	u32 ufd = *(u32 *)value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2058) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2059) 	ptr = map->ops->map_fd_get_ptr(map, map_file, ufd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2060) 	if (IS_ERR(ptr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2061) 		return PTR_ERR(ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2062) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2063) 	ret = htab_map_update_elem(map, key, &ptr, map_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2064) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2065) 		map->ops->map_fd_put_ptr(ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2066) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2067) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2068) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2069) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2070) static struct bpf_map *htab_of_map_alloc(union bpf_attr *attr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2071) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2072) 	struct bpf_map *map, *inner_map_meta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2073) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2074) 	inner_map_meta = bpf_map_meta_alloc(attr->inner_map_fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2075) 	if (IS_ERR(inner_map_meta))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2076) 		return inner_map_meta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2077) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2078) 	map = htab_map_alloc(attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2079) 	if (IS_ERR(map)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2080) 		bpf_map_meta_free(inner_map_meta);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2081) 		return map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2082) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2083) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2084) 	map->inner_map_meta = inner_map_meta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2085) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2086) 	return map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2087) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2088) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2089) static void *htab_of_map_lookup_elem(struct bpf_map *map, void *key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2090) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2091) 	struct bpf_map **inner_map  = htab_map_lookup_elem(map, key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2092) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2093) 	if (!inner_map)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2094) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2095) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2096) 	return READ_ONCE(*inner_map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2097) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2098) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2099) static int htab_of_map_gen_lookup(struct bpf_map *map,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2100) 				  struct bpf_insn *insn_buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2101) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2102) 	struct bpf_insn *insn = insn_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2103) 	const int ret = BPF_REG_0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2105) 	BUILD_BUG_ON(!__same_type(&__htab_map_lookup_elem,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2106) 		     (void *(*)(struct bpf_map *map, void *key))NULL));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2107) 	*insn++ = BPF_EMIT_CALL(BPF_CAST_CALL(__htab_map_lookup_elem));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2108) 	*insn++ = BPF_JMP_IMM(BPF_JEQ, ret, 0, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2109) 	*insn++ = BPF_ALU64_IMM(BPF_ADD, ret,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2110) 				offsetof(struct htab_elem, key) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2111) 				round_up(map->key_size, 8));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2112) 	*insn++ = BPF_LDX_MEM(BPF_DW, ret, ret, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2114) 	return insn - insn_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2117) static void htab_of_map_free(struct bpf_map *map)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2118) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2119) 	bpf_map_meta_free(map->inner_map_meta);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2120) 	fd_htab_map_free(map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2123) static int htab_of_maps_map_btf_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2124) const struct bpf_map_ops htab_of_maps_map_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2125) 	.map_alloc_check = fd_htab_map_alloc_check,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2126) 	.map_alloc = htab_of_map_alloc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2127) 	.map_free = htab_of_map_free,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2128) 	.map_get_next_key = htab_map_get_next_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2129) 	.map_lookup_elem = htab_of_map_lookup_elem,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2130) 	.map_delete_elem = htab_map_delete_elem,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2131) 	.map_fd_get_ptr = bpf_map_fd_get_ptr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2132) 	.map_fd_put_ptr = bpf_map_fd_put_ptr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2133) 	.map_fd_sys_lookup_elem = bpf_map_fd_sys_lookup_elem,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2134) 	.map_gen_lookup = htab_of_map_gen_lookup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2135) 	.map_check_btf = map_check_no_btf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2136) 	.map_btf_name = "bpf_htab",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2137) 	.map_btf_id = &htab_of_maps_map_btf_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2138) };