xref: /openbmc/linux/kernel/bpf/hashtab.c (revision 278002edb19bce2c628fafb0af936e77000f3a5b)
15b497af4SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
20f8e4bd8SAlexei Starovoitov /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
36c905981SAlexei Starovoitov  * Copyright (c) 2016 Facebook
40f8e4bd8SAlexei Starovoitov  */
50f8e4bd8SAlexei Starovoitov #include <linux/bpf.h>
6699c86d6SYonghong Song #include <linux/btf.h>
70f8e4bd8SAlexei Starovoitov #include <linux/jhash.h>
80f8e4bd8SAlexei Starovoitov #include <linux/filter.h>
94fe84359SAlexei Starovoitov #include <linux/rculist_nulls.h>
10c0203475SDaniel Borkmann #include <linux/random.h>
11699c86d6SYonghong Song #include <uapi/linux/btf.h>
121e6c62a8SAlexei Starovoitov #include <linux/rcupdate_trace.h>
13c317ab71SMenglong Dong #include <linux/btf_ids.h>
146c905981SAlexei Starovoitov #include "percpu_freelist.h"
1529ba732aSMartin KaFai Lau #include "bpf_lru_list.h"
16bcc6b1b7SMartin KaFai Lau #include "map_in_map.h"
17fba1a1c6SAlexei Starovoitov #include <linux/bpf_mem_alloc.h>
180f8e4bd8SAlexei Starovoitov 
1996eabe7aSMartin KaFai Lau #define HTAB_CREATE_FLAG_MASK						\
206e71b04aSChenbo Feng 	(BPF_F_NO_PREALLOC | BPF_F_NO_COMMON_LRU | BPF_F_NUMA_NODE |	\
21591fe988SDaniel Borkmann 	 BPF_F_ACCESS_MASK | BPF_F_ZERO_SEED)
2296eabe7aSMartin KaFai Lau 
2305799638SYonghong Song #define BATCH_OPS(_name)			\
2405799638SYonghong Song 	.map_lookup_batch =			\
2505799638SYonghong Song 	_name##_map_lookup_batch,		\
2605799638SYonghong Song 	.map_lookup_and_delete_batch =		\
2705799638SYonghong Song 	_name##_map_lookup_and_delete_batch,	\
2805799638SYonghong Song 	.map_update_batch =			\
2905799638SYonghong Song 	generic_map_update_batch,		\
3005799638SYonghong Song 	.map_delete_batch =			\
3105799638SYonghong Song 	generic_map_delete_batch
3205799638SYonghong Song 
33dbca151cSThomas Gleixner /*
34dbca151cSThomas Gleixner  * The bucket lock has two protection scopes:
35dbca151cSThomas Gleixner  *
366bd45f2eSLiu xuzhi  * 1) Serializing concurrent operations from BPF programs on different
37dbca151cSThomas Gleixner  *    CPUs
38dbca151cSThomas Gleixner  *
39dbca151cSThomas Gleixner  * 2) Serializing concurrent operations from BPF programs and sys_bpf()
40dbca151cSThomas Gleixner  *
41dbca151cSThomas Gleixner  * BPF programs can execute in any context including perf, kprobes and
42dbca151cSThomas Gleixner  * tracing. As there are almost no limits where perf, kprobes and tracing
43dbca151cSThomas Gleixner  * can be invoked from the lock operations need to be protected against
44dbca151cSThomas Gleixner  * deadlocks. Deadlocks can be caused by recursion and by an invocation in
45dbca151cSThomas Gleixner  * the lock held section when functions which acquire this lock are invoked
46dbca151cSThomas Gleixner  * from sys_bpf(). BPF recursion is prevented by incrementing the per CPU
47dbca151cSThomas Gleixner  * variable bpf_prog_active, which prevents BPF programs attached to perf
48dbca151cSThomas Gleixner  * events, kprobes and tracing to be invoked before the prior invocation
49dbca151cSThomas Gleixner  * from one of these contexts completed. sys_bpf() uses the same mechanism
50dbca151cSThomas Gleixner  * by pinning the task to the current CPU and incrementing the recursion
518fb33b60SZhen Lei  * protection across the map operation.
527f805d17SThomas Gleixner  *
537f805d17SThomas Gleixner  * This has subtle implications on PREEMPT_RT. PREEMPT_RT forbids certain
547f805d17SThomas Gleixner  * operations like memory allocations (even with GFP_ATOMIC) from atomic
557f805d17SThomas Gleixner  * contexts. This is required because even with GFP_ATOMIC the memory
568fb33b60SZhen Lei  * allocator calls into code paths which acquire locks with long held lock
577f805d17SThomas Gleixner  * sections. To ensure the deterministic behaviour these locks are regular
587f805d17SThomas Gleixner  * spinlocks, which are converted to 'sleepable' spinlocks on RT. The only
597f805d17SThomas Gleixner  * true atomic contexts on an RT kernel are the low level hardware
607f805d17SThomas Gleixner  * handling, scheduling, low level interrupt handling, NMIs etc. None of
617f805d17SThomas Gleixner  * these contexts should ever do memory allocations.
627f805d17SThomas Gleixner  *
637f805d17SThomas Gleixner  * As regular device interrupt handlers and soft interrupts are forced into
647f805d17SThomas Gleixner  * thread context, the existing code which does
65ace2bee8SYafang Shao  *   spin_lock*(); alloc(GFP_ATOMIC); spin_unlock*();
667f805d17SThomas Gleixner  * just works.
677f805d17SThomas Gleixner  *
687f805d17SThomas Gleixner  * In theory the BPF locks could be converted to regular spinlocks as well,
697f805d17SThomas Gleixner  * but the bucket locks and percpu_freelist locks can be taken from
707f805d17SThomas Gleixner  * arbitrary contexts (perf, kprobes, tracepoints) which are required to be
711d8b82c6SHou Tao  * atomic contexts even on RT. Before the introduction of bpf_mem_alloc,
721d8b82c6SHou Tao  * it is only safe to use raw spinlock for preallocated hash map on a RT kernel,
731d8b82c6SHou Tao  * because there is no memory allocation within the lock held sections. However
741d8b82c6SHou Tao  * after hash map was fully converted to use bpf_mem_alloc, there will be
751d8b82c6SHou Tao  * non-synchronous memory allocation for non-preallocated hash map, so it is
761d8b82c6SHou Tao  * safe to always use raw spinlock for bucket lock.
77dbca151cSThomas Gleixner  */
78688ecfe6Stom.leiming@gmail.com struct bucket {
794fe84359SAlexei Starovoitov 	struct hlist_nulls_head head;
807f805d17SThomas Gleixner 	raw_spinlock_t raw_lock;
81688ecfe6Stom.leiming@gmail.com };
82688ecfe6Stom.leiming@gmail.com 
8320b6cc34SSong Liu #define HASHTAB_MAP_LOCK_COUNT 8
8420b6cc34SSong Liu #define HASHTAB_MAP_LOCK_MASK (HASHTAB_MAP_LOCK_COUNT - 1)
8520b6cc34SSong Liu 
860f8e4bd8SAlexei Starovoitov struct bpf_htab {
870f8e4bd8SAlexei Starovoitov 	struct bpf_map map;
88fba1a1c6SAlexei Starovoitov 	struct bpf_mem_alloc ma;
89ee4ed53cSAlexei Starovoitov 	struct bpf_mem_alloc pcpu_ma;
90688ecfe6Stom.leiming@gmail.com 	struct bucket *buckets;
916c905981SAlexei Starovoitov 	void *elems;
9229ba732aSMartin KaFai Lau 	union {
936c905981SAlexei Starovoitov 		struct pcpu_freelist freelist;
9429ba732aSMartin KaFai Lau 		struct bpf_lru lru;
9529ba732aSMartin KaFai Lau 	};
968c290e60SAlexei Starovoitov 	struct htab_elem *__percpu *extra_elems;
9786fe28f7SAlexei Starovoitov 	/* number of elements in non-preallocated hashtable are kept
9886fe28f7SAlexei Starovoitov 	 * in either pcount or count
9986fe28f7SAlexei Starovoitov 	 */
10086fe28f7SAlexei Starovoitov 	struct percpu_counter pcount;
10186fe28f7SAlexei Starovoitov 	atomic_t count;
10286fe28f7SAlexei Starovoitov 	bool use_percpu_counter;
1030f8e4bd8SAlexei Starovoitov 	u32 n_buckets;	/* number of hash buckets */
1040f8e4bd8SAlexei Starovoitov 	u32 elem_size;	/* size of each element in bytes */
105c0203475SDaniel Borkmann 	u32 hashrnd;
106c50eb518SSong Liu 	struct lock_class_key lockdep_key;
10720b6cc34SSong Liu 	int __percpu *map_locked[HASHTAB_MAP_LOCK_COUNT];
1080f8e4bd8SAlexei Starovoitov };
1090f8e4bd8SAlexei Starovoitov 
1100f8e4bd8SAlexei Starovoitov /* each htab element is struct htab_elem + key + value */
1110f8e4bd8SAlexei Starovoitov struct htab_elem {
112824bd0ceSAlexei Starovoitov 	union {
1134fe84359SAlexei Starovoitov 		struct hlist_nulls_node hash_node;
1149f691549SAlexei Starovoitov 		struct {
1159f691549SAlexei Starovoitov 			void *padding;
1169f691549SAlexei Starovoitov 			union {
1176c905981SAlexei Starovoitov 				struct pcpu_freelist_node fnode;
118b9aff38dSYonghong Song 				struct htab_elem *batch_flink;
119824bd0ceSAlexei Starovoitov 			};
1209f691549SAlexei Starovoitov 		};
1219f691549SAlexei Starovoitov 	};
122a6ed3ea6SAlexei Starovoitov 	union {
123ee4ed53cSAlexei Starovoitov 		/* pointer to per-cpu pointer */
124ee4ed53cSAlexei Starovoitov 		void *ptr_to_pptr;
12529ba732aSMartin KaFai Lau 		struct bpf_lru_node lru_node;
126a6ed3ea6SAlexei Starovoitov 	};
1276c905981SAlexei Starovoitov 	u32 hash;
128d7f10df8SGustavo A. R. Silva 	char key[] __aligned(8);
1290f8e4bd8SAlexei Starovoitov };
1300f8e4bd8SAlexei Starovoitov 
htab_is_prealloc(const struct bpf_htab * htab)1317f805d17SThomas Gleixner static inline bool htab_is_prealloc(const struct bpf_htab *htab)
1327f805d17SThomas Gleixner {
1337f805d17SThomas Gleixner 	return !(htab->map.map_flags & BPF_F_NO_PREALLOC);
1347f805d17SThomas Gleixner }
1357f805d17SThomas Gleixner 
htab_init_buckets(struct bpf_htab * htab)136d01f9b19SThomas Gleixner static void htab_init_buckets(struct bpf_htab *htab)
137d01f9b19SThomas Gleixner {
1389263dddcSTakshak Chahande 	unsigned int i;
139d01f9b19SThomas Gleixner 
140d01f9b19SThomas Gleixner 	for (i = 0; i < htab->n_buckets; i++) {
141d01f9b19SThomas Gleixner 		INIT_HLIST_NULLS_HEAD(&htab->buckets[i].head, i);
1427f805d17SThomas Gleixner 		raw_spin_lock_init(&htab->buckets[i].raw_lock);
143c50eb518SSong Liu 		lockdep_set_class(&htab->buckets[i].raw_lock,
144c50eb518SSong Liu 					  &htab->lockdep_key);
145e7e51805SEric Dumazet 		cond_resched();
146d01f9b19SThomas Gleixner 	}
147d01f9b19SThomas Gleixner }
148d01f9b19SThomas Gleixner 
htab_lock_bucket(const struct bpf_htab * htab,struct bucket * b,u32 hash,unsigned long * pflags)14920b6cc34SSong Liu static inline int htab_lock_bucket(const struct bpf_htab *htab,
15020b6cc34SSong Liu 				   struct bucket *b, u32 hash,
15120b6cc34SSong Liu 				   unsigned long *pflags)
152d01f9b19SThomas Gleixner {
153d01f9b19SThomas Gleixner 	unsigned long flags;
154d01f9b19SThomas Gleixner 
1559f907439STonghao Zhang 	hash = hash & min_t(u32, HASHTAB_MAP_LOCK_MASK, htab->n_buckets - 1);
15620b6cc34SSong Liu 
1572775da21SHou Tao 	preempt_disable();
1586e6dffbbSSong Liu 	local_irq_save(flags);
15920b6cc34SSong Liu 	if (unlikely(__this_cpu_inc_return(*(htab->map_locked[hash])) != 1)) {
16020b6cc34SSong Liu 		__this_cpu_dec(*(htab->map_locked[hash]));
1616e6dffbbSSong Liu 		local_irq_restore(flags);
1622775da21SHou Tao 		preempt_enable();
16320b6cc34SSong Liu 		return -EBUSY;
16420b6cc34SSong Liu 	}
16520b6cc34SSong Liu 
1666e6dffbbSSong Liu 	raw_spin_lock(&b->raw_lock);
16720b6cc34SSong Liu 	*pflags = flags;
16820b6cc34SSong Liu 
16920b6cc34SSong Liu 	return 0;
170d01f9b19SThomas Gleixner }
171d01f9b19SThomas Gleixner 
htab_unlock_bucket(const struct bpf_htab * htab,struct bucket * b,u32 hash,unsigned long flags)172d01f9b19SThomas Gleixner static inline void htab_unlock_bucket(const struct bpf_htab *htab,
17320b6cc34SSong Liu 				      struct bucket *b, u32 hash,
174d01f9b19SThomas Gleixner 				      unsigned long flags)
175d01f9b19SThomas Gleixner {
1769f907439STonghao Zhang 	hash = hash & min_t(u32, HASHTAB_MAP_LOCK_MASK, htab->n_buckets - 1);
1776e6dffbbSSong Liu 	raw_spin_unlock(&b->raw_lock);
17820b6cc34SSong Liu 	__this_cpu_dec(*(htab->map_locked[hash]));
1796e6dffbbSSong Liu 	local_irq_restore(flags);
1802775da21SHou Tao 	preempt_enable();
181d01f9b19SThomas Gleixner }
182d01f9b19SThomas Gleixner 
18329ba732aSMartin KaFai Lau static bool htab_lru_map_delete_node(void *arg, struct bpf_lru_node *node);
18429ba732aSMartin KaFai Lau 
htab_is_lru(const struct bpf_htab * htab)18529ba732aSMartin KaFai Lau static bool htab_is_lru(const struct bpf_htab *htab)
18629ba732aSMartin KaFai Lau {
1878f844938SMartin KaFai Lau 	return htab->map.map_type == BPF_MAP_TYPE_LRU_HASH ||
1888f844938SMartin KaFai Lau 		htab->map.map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH;
1898f844938SMartin KaFai Lau }
1908f844938SMartin KaFai Lau 
htab_is_percpu(const struct bpf_htab * htab)1918f844938SMartin KaFai Lau static bool htab_is_percpu(const struct bpf_htab *htab)
1928f844938SMartin KaFai Lau {
1938f844938SMartin KaFai Lau 	return htab->map.map_type == BPF_MAP_TYPE_PERCPU_HASH ||
1948f844938SMartin KaFai Lau 		htab->map.map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH;
19529ba732aSMartin KaFai Lau }
19629ba732aSMartin KaFai Lau 
htab_elem_set_ptr(struct htab_elem * l,u32 key_size,void __percpu * pptr)1976c905981SAlexei Starovoitov static inline void htab_elem_set_ptr(struct htab_elem *l, u32 key_size,
1986c905981SAlexei Starovoitov 				     void __percpu *pptr)
1996c905981SAlexei Starovoitov {
2006c905981SAlexei Starovoitov 	*(void __percpu **)(l->key + key_size) = pptr;
2016c905981SAlexei Starovoitov }
2026c905981SAlexei Starovoitov 
htab_elem_get_ptr(struct htab_elem * l,u32 key_size)2036c905981SAlexei Starovoitov static inline void __percpu *htab_elem_get_ptr(struct htab_elem *l, u32 key_size)
2046c905981SAlexei Starovoitov {
2056c905981SAlexei Starovoitov 	return *(void __percpu **)(l->key + key_size);
2066c905981SAlexei Starovoitov }
2076c905981SAlexei Starovoitov 
fd_htab_map_get_ptr(const struct bpf_map * map,struct htab_elem * l)208bcc6b1b7SMartin KaFai Lau static void *fd_htab_map_get_ptr(const struct bpf_map *map, struct htab_elem *l)
209bcc6b1b7SMartin KaFai Lau {
210bcc6b1b7SMartin KaFai Lau 	return *(void **)(l->key + roundup(map->key_size, 8));
211bcc6b1b7SMartin KaFai Lau }
212bcc6b1b7SMartin KaFai Lau 
get_htab_elem(struct bpf_htab * htab,int i)2136c905981SAlexei Starovoitov static struct htab_elem *get_htab_elem(struct bpf_htab *htab, int i)
2146c905981SAlexei Starovoitov {
215e1868b9eSEric Dumazet 	return (struct htab_elem *) (htab->elems + i * (u64)htab->elem_size);
2166c905981SAlexei Starovoitov }
2176c905981SAlexei Starovoitov 
htab_has_extra_elems(struct bpf_htab * htab)21868134668SAlexei Starovoitov static bool htab_has_extra_elems(struct bpf_htab *htab)
21968134668SAlexei Starovoitov {
22068134668SAlexei Starovoitov 	return !htab_is_percpu(htab) && !htab_is_lru(htab);
22168134668SAlexei Starovoitov }
22268134668SAlexei Starovoitov 
htab_free_prealloced_timers(struct bpf_htab * htab)22368134668SAlexei Starovoitov static void htab_free_prealloced_timers(struct bpf_htab *htab)
22468134668SAlexei Starovoitov {
22568134668SAlexei Starovoitov 	u32 num_entries = htab->map.max_entries;
22668134668SAlexei Starovoitov 	int i;
22768134668SAlexei Starovoitov 
228db559117SKumar Kartikeya Dwivedi 	if (!btf_record_has_field(htab->map.record, BPF_TIMER))
22968134668SAlexei Starovoitov 		return;
23068134668SAlexei Starovoitov 	if (htab_has_extra_elems(htab))
23168134668SAlexei Starovoitov 		num_entries += num_possible_cpus();
23268134668SAlexei Starovoitov 
23368134668SAlexei Starovoitov 	for (i = 0; i < num_entries; i++) {
23468134668SAlexei Starovoitov 		struct htab_elem *elem;
23568134668SAlexei Starovoitov 
23668134668SAlexei Starovoitov 		elem = get_htab_elem(htab, i);
237db559117SKumar Kartikeya Dwivedi 		bpf_obj_free_timer(htab->map.record, elem->key + round_up(htab->map.key_size, 8));
23868134668SAlexei Starovoitov 		cond_resched();
23968134668SAlexei Starovoitov 	}
24068134668SAlexei Starovoitov }
24168134668SAlexei Starovoitov 
htab_free_prealloced_fields(struct bpf_htab * htab)242aa3496acSKumar Kartikeya Dwivedi static void htab_free_prealloced_fields(struct bpf_htab *htab)
24314a324f6SKumar Kartikeya Dwivedi {
24414a324f6SKumar Kartikeya Dwivedi 	u32 num_entries = htab->map.max_entries;
24514a324f6SKumar Kartikeya Dwivedi 	int i;
24614a324f6SKumar Kartikeya Dwivedi 
247aa3496acSKumar Kartikeya Dwivedi 	if (IS_ERR_OR_NULL(htab->map.record))
24814a324f6SKumar Kartikeya Dwivedi 		return;
24914a324f6SKumar Kartikeya Dwivedi 	if (htab_has_extra_elems(htab))
25014a324f6SKumar Kartikeya Dwivedi 		num_entries += num_possible_cpus();
25114a324f6SKumar Kartikeya Dwivedi 	for (i = 0; i < num_entries; i++) {
25214a324f6SKumar Kartikeya Dwivedi 		struct htab_elem *elem;
25314a324f6SKumar Kartikeya Dwivedi 
25414a324f6SKumar Kartikeya Dwivedi 		elem = get_htab_elem(htab, i);
25565334e64SKumar Kartikeya Dwivedi 		if (htab_is_percpu(htab)) {
25665334e64SKumar Kartikeya Dwivedi 			void __percpu *pptr = htab_elem_get_ptr(elem, htab->map.key_size);
25765334e64SKumar Kartikeya Dwivedi 			int cpu;
25865334e64SKumar Kartikeya Dwivedi 
25965334e64SKumar Kartikeya Dwivedi 			for_each_possible_cpu(cpu) {
26065334e64SKumar Kartikeya Dwivedi 				bpf_obj_free_fields(htab->map.record, per_cpu_ptr(pptr, cpu));
26165334e64SKumar Kartikeya Dwivedi 				cond_resched();
26265334e64SKumar Kartikeya Dwivedi 			}
26365334e64SKumar Kartikeya Dwivedi 		} else {
264aa3496acSKumar Kartikeya Dwivedi 			bpf_obj_free_fields(htab->map.record, elem->key + round_up(htab->map.key_size, 8));
26514a324f6SKumar Kartikeya Dwivedi 			cond_resched();
26614a324f6SKumar Kartikeya Dwivedi 		}
26765334e64SKumar Kartikeya Dwivedi 		cond_resched();
26865334e64SKumar Kartikeya Dwivedi 	}
26914a324f6SKumar Kartikeya Dwivedi }
27014a324f6SKumar Kartikeya Dwivedi 
htab_free_elems(struct bpf_htab * htab)2716c905981SAlexei Starovoitov static void htab_free_elems(struct bpf_htab *htab)
2726c905981SAlexei Starovoitov {
2736c905981SAlexei Starovoitov 	int i;
2746c905981SAlexei Starovoitov 
2758f844938SMartin KaFai Lau 	if (!htab_is_percpu(htab))
2766c905981SAlexei Starovoitov 		goto free_elems;
2776c905981SAlexei Starovoitov 
2786c905981SAlexei Starovoitov 	for (i = 0; i < htab->map.max_entries; i++) {
2796c905981SAlexei Starovoitov 		void __percpu *pptr;
2806c905981SAlexei Starovoitov 
2816c905981SAlexei Starovoitov 		pptr = htab_elem_get_ptr(get_htab_elem(htab, i),
2826c905981SAlexei Starovoitov 					 htab->map.key_size);
2836c905981SAlexei Starovoitov 		free_percpu(pptr);
2849147efcbSEric Dumazet 		cond_resched();
2856c905981SAlexei Starovoitov 	}
2866c905981SAlexei Starovoitov free_elems:
287d407bd25SDaniel Borkmann 	bpf_map_area_free(htab->elems);
2886c905981SAlexei Starovoitov }
2896c905981SAlexei Starovoitov 
290b9aff38dSYonghong Song /* The LRU list has a lock (lru_lock). Each htab bucket has a lock
291b9aff38dSYonghong Song  * (bucket_lock). If both locks need to be acquired together, the lock
292b9aff38dSYonghong Song  * order is always lru_lock -> bucket_lock and this only happens in
293b9aff38dSYonghong Song  * bpf_lru_list.c logic. For example, certain code path of
294b9aff38dSYonghong Song  * bpf_lru_pop_free(), which is called by function prealloc_lru_pop(),
295b9aff38dSYonghong Song  * will acquire lru_lock first followed by acquiring bucket_lock.
296b9aff38dSYonghong Song  *
297b9aff38dSYonghong Song  * In hashtab.c, to avoid deadlock, lock acquisition of
298b9aff38dSYonghong Song  * bucket_lock followed by lru_lock is not allowed. In such cases,
299b9aff38dSYonghong Song  * bucket_lock needs to be released first before acquiring lru_lock.
300b9aff38dSYonghong Song  */
prealloc_lru_pop(struct bpf_htab * htab,void * key,u32 hash)30129ba732aSMartin KaFai Lau static struct htab_elem *prealloc_lru_pop(struct bpf_htab *htab, void *key,
30229ba732aSMartin KaFai Lau 					  u32 hash)
30329ba732aSMartin KaFai Lau {
30429ba732aSMartin KaFai Lau 	struct bpf_lru_node *node = bpf_lru_pop_free(&htab->lru, hash);
30529ba732aSMartin KaFai Lau 	struct htab_elem *l;
30629ba732aSMartin KaFai Lau 
30729ba732aSMartin KaFai Lau 	if (node) {
3089bc421b6SAnton Protopopov 		bpf_map_inc_elem_count(&htab->map);
30929ba732aSMartin KaFai Lau 		l = container_of(node, struct htab_elem, lru_node);
310275c30bcSKumar Kartikeya Dwivedi 		memcpy(l->key, key, htab->map.key_size);
31129ba732aSMartin KaFai Lau 		return l;
31229ba732aSMartin KaFai Lau 	}
31329ba732aSMartin KaFai Lau 
31429ba732aSMartin KaFai Lau 	return NULL;
31529ba732aSMartin KaFai Lau }
31629ba732aSMartin KaFai Lau 
prealloc_init(struct bpf_htab * htab)31729ba732aSMartin KaFai Lau static int prealloc_init(struct bpf_htab *htab)
3186c905981SAlexei Starovoitov {
3198c290e60SAlexei Starovoitov 	u32 num_entries = htab->map.max_entries;
3206c905981SAlexei Starovoitov 	int err = -ENOMEM, i;
3216c905981SAlexei Starovoitov 
32268134668SAlexei Starovoitov 	if (htab_has_extra_elems(htab))
3238c290e60SAlexei Starovoitov 		num_entries += num_possible_cpus();
3248c290e60SAlexei Starovoitov 
325e1868b9eSEric Dumazet 	htab->elems = bpf_map_area_alloc((u64)htab->elem_size * num_entries,
32696eabe7aSMartin KaFai Lau 					 htab->map.numa_node);
3276c905981SAlexei Starovoitov 	if (!htab->elems)
3286c905981SAlexei Starovoitov 		return -ENOMEM;
3296c905981SAlexei Starovoitov 
3308f844938SMartin KaFai Lau 	if (!htab_is_percpu(htab))
3316c905981SAlexei Starovoitov 		goto skip_percpu_elems;
3326c905981SAlexei Starovoitov 
3338c290e60SAlexei Starovoitov 	for (i = 0; i < num_entries; i++) {
3346c905981SAlexei Starovoitov 		u32 size = round_up(htab->map.value_size, 8);
3356c905981SAlexei Starovoitov 		void __percpu *pptr;
3366c905981SAlexei Starovoitov 
33788145681SRoman Gushchin 		pptr = bpf_map_alloc_percpu(&htab->map, size, 8,
33888145681SRoman Gushchin 					    GFP_USER | __GFP_NOWARN);
3396c905981SAlexei Starovoitov 		if (!pptr)
3406c905981SAlexei Starovoitov 			goto free_elems;
3416c905981SAlexei Starovoitov 		htab_elem_set_ptr(get_htab_elem(htab, i), htab->map.key_size,
3426c905981SAlexei Starovoitov 				  pptr);
3439147efcbSEric Dumazet 		cond_resched();
3446c905981SAlexei Starovoitov 	}
3456c905981SAlexei Starovoitov 
3466c905981SAlexei Starovoitov skip_percpu_elems:
34729ba732aSMartin KaFai Lau 	if (htab_is_lru(htab))
34829ba732aSMartin KaFai Lau 		err = bpf_lru_init(&htab->lru,
34929ba732aSMartin KaFai Lau 				   htab->map.map_flags & BPF_F_NO_COMMON_LRU,
35029ba732aSMartin KaFai Lau 				   offsetof(struct htab_elem, hash) -
35129ba732aSMartin KaFai Lau 				   offsetof(struct htab_elem, lru_node),
35229ba732aSMartin KaFai Lau 				   htab_lru_map_delete_node,
35329ba732aSMartin KaFai Lau 				   htab);
35429ba732aSMartin KaFai Lau 	else
3556c905981SAlexei Starovoitov 		err = pcpu_freelist_init(&htab->freelist);
35629ba732aSMartin KaFai Lau 
3576c905981SAlexei Starovoitov 	if (err)
3586c905981SAlexei Starovoitov 		goto free_elems;
3596c905981SAlexei Starovoitov 
36029ba732aSMartin KaFai Lau 	if (htab_is_lru(htab))
36129ba732aSMartin KaFai Lau 		bpf_lru_populate(&htab->lru, htab->elems,
36229ba732aSMartin KaFai Lau 				 offsetof(struct htab_elem, lru_node),
3638c290e60SAlexei Starovoitov 				 htab->elem_size, num_entries);
36429ba732aSMartin KaFai Lau 	else
3659f691549SAlexei Starovoitov 		pcpu_freelist_populate(&htab->freelist,
3669f691549SAlexei Starovoitov 				       htab->elems + offsetof(struct htab_elem, fnode),
3678c290e60SAlexei Starovoitov 				       htab->elem_size, num_entries);
36829ba732aSMartin KaFai Lau 
3696c905981SAlexei Starovoitov 	return 0;
3706c905981SAlexei Starovoitov 
3716c905981SAlexei Starovoitov free_elems:
3726c905981SAlexei Starovoitov 	htab_free_elems(htab);
3736c905981SAlexei Starovoitov 	return err;
3746c905981SAlexei Starovoitov }
3756c905981SAlexei Starovoitov 
prealloc_destroy(struct bpf_htab * htab)37629ba732aSMartin KaFai Lau static void prealloc_destroy(struct bpf_htab *htab)
37729ba732aSMartin KaFai Lau {
37829ba732aSMartin KaFai Lau 	htab_free_elems(htab);
37929ba732aSMartin KaFai Lau 
38029ba732aSMartin KaFai Lau 	if (htab_is_lru(htab))
38129ba732aSMartin KaFai Lau 		bpf_lru_destroy(&htab->lru);
38229ba732aSMartin KaFai Lau 	else
38329ba732aSMartin KaFai Lau 		pcpu_freelist_destroy(&htab->freelist);
38429ba732aSMartin KaFai Lau }
38529ba732aSMartin KaFai Lau 
alloc_extra_elems(struct bpf_htab * htab)386a6ed3ea6SAlexei Starovoitov static int alloc_extra_elems(struct bpf_htab *htab)
387a6ed3ea6SAlexei Starovoitov {
3888c290e60SAlexei Starovoitov 	struct htab_elem *__percpu *pptr, *l_new;
3898c290e60SAlexei Starovoitov 	struct pcpu_freelist_node *l;
390a6ed3ea6SAlexei Starovoitov 	int cpu;
391a6ed3ea6SAlexei Starovoitov 
39288145681SRoman Gushchin 	pptr = bpf_map_alloc_percpu(&htab->map, sizeof(struct htab_elem *), 8,
3938c290e60SAlexei Starovoitov 				    GFP_USER | __GFP_NOWARN);
394a6ed3ea6SAlexei Starovoitov 	if (!pptr)
395a6ed3ea6SAlexei Starovoitov 		return -ENOMEM;
396a6ed3ea6SAlexei Starovoitov 
397a6ed3ea6SAlexei Starovoitov 	for_each_possible_cpu(cpu) {
3988c290e60SAlexei Starovoitov 		l = pcpu_freelist_pop(&htab->freelist);
3998c290e60SAlexei Starovoitov 		/* pop will succeed, since prealloc_init()
4008c290e60SAlexei Starovoitov 		 * preallocated extra num_possible_cpus elements
4018c290e60SAlexei Starovoitov 		 */
4028c290e60SAlexei Starovoitov 		l_new = container_of(l, struct htab_elem, fnode);
4038c290e60SAlexei Starovoitov 		*per_cpu_ptr(pptr, cpu) = l_new;
404a6ed3ea6SAlexei Starovoitov 	}
405a6ed3ea6SAlexei Starovoitov 	htab->extra_elems = pptr;
406a6ed3ea6SAlexei Starovoitov 	return 0;
407a6ed3ea6SAlexei Starovoitov }
408a6ed3ea6SAlexei Starovoitov 
4090f8e4bd8SAlexei Starovoitov /* Called from syscall */
htab_map_alloc_check(union bpf_attr * attr)4109328e0d1SJakub Kicinski static int htab_map_alloc_check(union bpf_attr *attr)
4119328e0d1SJakub Kicinski {
4129328e0d1SJakub Kicinski 	bool percpu = (attr->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
4139328e0d1SJakub Kicinski 		       attr->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH);
4149328e0d1SJakub Kicinski 	bool lru = (attr->map_type == BPF_MAP_TYPE_LRU_HASH ||
4159328e0d1SJakub Kicinski 		    attr->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH);
4169328e0d1SJakub Kicinski 	/* percpu_lru means each cpu has its own LRU list.
4179328e0d1SJakub Kicinski 	 * it is different from BPF_MAP_TYPE_PERCPU_HASH where
4189328e0d1SJakub Kicinski 	 * the map's value itself is percpu.  percpu_lru has
4199328e0d1SJakub Kicinski 	 * nothing to do with the map's value.
4209328e0d1SJakub Kicinski 	 */
4219328e0d1SJakub Kicinski 	bool percpu_lru = (attr->map_flags & BPF_F_NO_COMMON_LRU);
4229328e0d1SJakub Kicinski 	bool prealloc = !(attr->map_flags & BPF_F_NO_PREALLOC);
42396b3b6c9SLorenz Bauer 	bool zero_seed = (attr->map_flags & BPF_F_ZERO_SEED);
4249328e0d1SJakub Kicinski 	int numa_node = bpf_map_attr_numa_node(attr);
4259328e0d1SJakub Kicinski 
4269328e0d1SJakub Kicinski 	BUILD_BUG_ON(offsetof(struct htab_elem, fnode.next) !=
4279328e0d1SJakub Kicinski 		     offsetof(struct htab_elem, hash_node.pprev));
4289328e0d1SJakub Kicinski 
42996b3b6c9SLorenz Bauer 	if (zero_seed && !capable(CAP_SYS_ADMIN))
43096b3b6c9SLorenz Bauer 		/* Guard against local DoS, and discourage production use. */
43196b3b6c9SLorenz Bauer 		return -EPERM;
43296b3b6c9SLorenz Bauer 
433591fe988SDaniel Borkmann 	if (attr->map_flags & ~HTAB_CREATE_FLAG_MASK ||
434591fe988SDaniel Borkmann 	    !bpf_map_flags_access_ok(attr->map_flags))
4359328e0d1SJakub Kicinski 		return -EINVAL;
4369328e0d1SJakub Kicinski 
4379328e0d1SJakub Kicinski 	if (!lru && percpu_lru)
4389328e0d1SJakub Kicinski 		return -EINVAL;
4399328e0d1SJakub Kicinski 
4409328e0d1SJakub Kicinski 	if (lru && !prealloc)
4419328e0d1SJakub Kicinski 		return -ENOTSUPP;
4429328e0d1SJakub Kicinski 
4439328e0d1SJakub Kicinski 	if (numa_node != NUMA_NO_NODE && (percpu || percpu_lru))
4449328e0d1SJakub Kicinski 		return -EINVAL;
4459328e0d1SJakub Kicinski 
4469328e0d1SJakub Kicinski 	/* check sanity of attributes.
4479328e0d1SJakub Kicinski 	 * value_size == 0 may be allowed in the future to use map as a set
4489328e0d1SJakub Kicinski 	 */
4499328e0d1SJakub Kicinski 	if (attr->max_entries == 0 || attr->key_size == 0 ||
4509328e0d1SJakub Kicinski 	    attr->value_size == 0)
4519328e0d1SJakub Kicinski 		return -EINVAL;
4529328e0d1SJakub Kicinski 
453c6bde958SFlorian Lehner 	if ((u64)attr->key_size + attr->value_size >= KMALLOC_MAX_SIZE -
454c6bde958SFlorian Lehner 	   sizeof(struct htab_elem))
455c6bde958SFlorian Lehner 		/* if key_size + value_size is bigger, the user space won't be
456c6bde958SFlorian Lehner 		 * able to access the elements via bpf syscall. This check
457c6bde958SFlorian Lehner 		 * also makes sure that the elem_size doesn't overflow and it's
4589328e0d1SJakub Kicinski 		 * kmalloc-able later in htab_map_update_elem()
4599328e0d1SJakub Kicinski 		 */
4609328e0d1SJakub Kicinski 		return -E2BIG;
461c43622d6STao Chen 	/* percpu map value size is bound by PCPU_MIN_UNIT_SIZE */
462c43622d6STao Chen 	if (percpu && round_up(attr->value_size, 8) > PCPU_MIN_UNIT_SIZE)
463c43622d6STao Chen 		return -E2BIG;
4649328e0d1SJakub Kicinski 
4659328e0d1SJakub Kicinski 	return 0;
4669328e0d1SJakub Kicinski }
4679328e0d1SJakub Kicinski 
htab_map_alloc(union bpf_attr * attr)4680f8e4bd8SAlexei Starovoitov static struct bpf_map *htab_map_alloc(union bpf_attr *attr)
4690f8e4bd8SAlexei Starovoitov {
4708f844938SMartin KaFai Lau 	bool percpu = (attr->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
4718f844938SMartin KaFai Lau 		       attr->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH);
4728f844938SMartin KaFai Lau 	bool lru = (attr->map_type == BPF_MAP_TYPE_LRU_HASH ||
4738f844938SMartin KaFai Lau 		    attr->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH);
47429ba732aSMartin KaFai Lau 	/* percpu_lru means each cpu has its own LRU list.
47529ba732aSMartin KaFai Lau 	 * it is different from BPF_MAP_TYPE_PERCPU_HASH where
47629ba732aSMartin KaFai Lau 	 * the map's value itself is percpu.  percpu_lru has
47729ba732aSMartin KaFai Lau 	 * nothing to do with the map's value.
47829ba732aSMartin KaFai Lau 	 */
47929ba732aSMartin KaFai Lau 	bool percpu_lru = (attr->map_flags & BPF_F_NO_COMMON_LRU);
48029ba732aSMartin KaFai Lau 	bool prealloc = !(attr->map_flags & BPF_F_NO_PREALLOC);
4810f8e4bd8SAlexei Starovoitov 	struct bpf_htab *htab;
48220b6cc34SSong Liu 	int err, i;
4830f8e4bd8SAlexei Starovoitov 
48473cf09a3SYafang Shao 	htab = bpf_map_area_alloc(sizeof(*htab), NUMA_NO_NODE);
4850f8e4bd8SAlexei Starovoitov 	if (!htab)
4860f8e4bd8SAlexei Starovoitov 		return ERR_PTR(-ENOMEM);
4870f8e4bd8SAlexei Starovoitov 
4888aaeed81SEric Dumazet 	lockdep_register_key(&htab->lockdep_key);
4898aaeed81SEric Dumazet 
490bd475643SJakub Kicinski 	bpf_map_init_from_attr(&htab->map, attr);
4910f8e4bd8SAlexei Starovoitov 
49229ba732aSMartin KaFai Lau 	if (percpu_lru) {
49329ba732aSMartin KaFai Lau 		/* ensure each CPU's lru list has >=1 elements.
49429ba732aSMartin KaFai Lau 		 * since we are at it, make each lru list has the same
49529ba732aSMartin KaFai Lau 		 * number of elements.
49629ba732aSMartin KaFai Lau 		 */
49729ba732aSMartin KaFai Lau 		htab->map.max_entries = roundup(attr->max_entries,
49829ba732aSMartin KaFai Lau 						num_possible_cpus());
49929ba732aSMartin KaFai Lau 		if (htab->map.max_entries < attr->max_entries)
50029ba732aSMartin KaFai Lau 			htab->map.max_entries = rounddown(attr->max_entries,
50129ba732aSMartin KaFai Lau 							  num_possible_cpus());
50229ba732aSMartin KaFai Lau 	}
50329ba732aSMartin KaFai Lau 
5048435f096SToke Høiland-Jørgensen 	/* hash table size must be power of 2; roundup_pow_of_two() can overflow
5058435f096SToke Høiland-Jørgensen 	 * into UB on 32-bit arches, so check that first
5068435f096SToke Høiland-Jørgensen 	 */
5078435f096SToke Høiland-Jørgensen 	err = -E2BIG;
5088435f096SToke Høiland-Jørgensen 	if (htab->map.max_entries > 1UL << 31)
5098435f096SToke Høiland-Jørgensen 		goto free_htab;
5108435f096SToke Høiland-Jørgensen 
5110f8e4bd8SAlexei Starovoitov 	htab->n_buckets = roundup_pow_of_two(htab->map.max_entries);
5120f8e4bd8SAlexei Starovoitov 
51301b3f521SAlexei Starovoitov 	htab->elem_size = sizeof(struct htab_elem) +
514824bd0ceSAlexei Starovoitov 			  round_up(htab->map.key_size, 8);
515824bd0ceSAlexei Starovoitov 	if (percpu)
516824bd0ceSAlexei Starovoitov 		htab->elem_size += sizeof(void *);
517824bd0ceSAlexei Starovoitov 	else
5186c905981SAlexei Starovoitov 		htab->elem_size += round_up(htab->map.value_size, 8);
51901b3f521SAlexei Starovoitov 
5208435f096SToke Høiland-Jørgensen 	/* check for u32 overflow */
5218435f096SToke Høiland-Jørgensen 	if (htab->n_buckets > U32_MAX / sizeof(struct bucket))
522daaf427cSAlexei Starovoitov 		goto free_htab;
523daaf427cSAlexei Starovoitov 
5249bc421b6SAnton Protopopov 	err = bpf_map_init_elem_count(&htab->map);
5259bc421b6SAnton Protopopov 	if (err)
5269bc421b6SAnton Protopopov 		goto free_htab;
5279bc421b6SAnton Protopopov 
52801b3f521SAlexei Starovoitov 	err = -ENOMEM;
529d407bd25SDaniel Borkmann 	htab->buckets = bpf_map_area_alloc(htab->n_buckets *
53096eabe7aSMartin KaFai Lau 					   sizeof(struct bucket),
53196eabe7aSMartin KaFai Lau 					   htab->map.numa_node);
5320f8e4bd8SAlexei Starovoitov 	if (!htab->buckets)
5339bc421b6SAnton Protopopov 		goto free_elem_count;
5340f8e4bd8SAlexei Starovoitov 
53520b6cc34SSong Liu 	for (i = 0; i < HASHTAB_MAP_LOCK_COUNT; i++) {
53688145681SRoman Gushchin 		htab->map_locked[i] = bpf_map_alloc_percpu(&htab->map,
53788145681SRoman Gushchin 							   sizeof(int),
53888145681SRoman Gushchin 							   sizeof(int),
53988145681SRoman Gushchin 							   GFP_USER);
54020b6cc34SSong Liu 		if (!htab->map_locked[i])
54120b6cc34SSong Liu 			goto free_map_locked;
54220b6cc34SSong Liu 	}
54320b6cc34SSong Liu 
54496b3b6c9SLorenz Bauer 	if (htab->map.map_flags & BPF_F_ZERO_SEED)
54596b3b6c9SLorenz Bauer 		htab->hashrnd = 0;
54696b3b6c9SLorenz Bauer 	else
547a251c17aSJason A. Donenfeld 		htab->hashrnd = get_random_u32();
54896b3b6c9SLorenz Bauer 
549d01f9b19SThomas Gleixner 	htab_init_buckets(htab);
5500f8e4bd8SAlexei Starovoitov 
55186fe28f7SAlexei Starovoitov /* compute_batch_value() computes batch value as num_online_cpus() * 2
55286fe28f7SAlexei Starovoitov  * and __percpu_counter_compare() needs
55386fe28f7SAlexei Starovoitov  * htab->max_entries - cur_number_of_elems to be more than batch * num_online_cpus()
55486fe28f7SAlexei Starovoitov  * for percpu_counter to be faster than atomic_t. In practice the average bpf
55586fe28f7SAlexei Starovoitov  * hash map size is 10k, which means that a system with 64 cpus will fill
55686fe28f7SAlexei Starovoitov  * hashmap to 20% of 10k before percpu_counter becomes ineffective. Therefore
55786fe28f7SAlexei Starovoitov  * define our own batch count as 32 then 10k hash map can be filled up to 80%:
55886fe28f7SAlexei Starovoitov  * 10k - 8k > 32 _batch_ * 64 _cpus_
55986fe28f7SAlexei Starovoitov  * and __percpu_counter_compare() will still be fast. At that point hash map
56086fe28f7SAlexei Starovoitov  * collisions will dominate its performance anyway. Assume that hash map filled
56186fe28f7SAlexei Starovoitov  * to 50+% isn't going to be O(1) and use the following formula to choose
56286fe28f7SAlexei Starovoitov  * between percpu_counter and atomic_t.
56386fe28f7SAlexei Starovoitov  */
56486fe28f7SAlexei Starovoitov #define PERCPU_COUNTER_BATCH 32
56586fe28f7SAlexei Starovoitov 	if (attr->max_entries / 2 > num_online_cpus() * PERCPU_COUNTER_BATCH)
56686fe28f7SAlexei Starovoitov 		htab->use_percpu_counter = true;
56786fe28f7SAlexei Starovoitov 
56886fe28f7SAlexei Starovoitov 	if (htab->use_percpu_counter) {
56986fe28f7SAlexei Starovoitov 		err = percpu_counter_init(&htab->pcount, 0, GFP_KERNEL);
57086fe28f7SAlexei Starovoitov 		if (err)
57186fe28f7SAlexei Starovoitov 			goto free_map_locked;
57286fe28f7SAlexei Starovoitov 	}
57386fe28f7SAlexei Starovoitov 
5748c290e60SAlexei Starovoitov 	if (prealloc) {
5758c290e60SAlexei Starovoitov 		err = prealloc_init(htab);
5768c290e60SAlexei Starovoitov 		if (err)
57720b6cc34SSong Liu 			goto free_map_locked;
5788c290e60SAlexei Starovoitov 
57929ba732aSMartin KaFai Lau 		if (!percpu && !lru) {
58029ba732aSMartin KaFai Lau 			/* lru itself can remove the least used element, so
58129ba732aSMartin KaFai Lau 			 * there is no need for an extra elem during map_update.
58229ba732aSMartin KaFai Lau 			 */
583a6ed3ea6SAlexei Starovoitov 			err = alloc_extra_elems(htab);
5846c905981SAlexei Starovoitov 			if (err)
5858c290e60SAlexei Starovoitov 				goto free_prealloc;
5866c905981SAlexei Starovoitov 		}
587fba1a1c6SAlexei Starovoitov 	} else {
5884ab67149SAlexei Starovoitov 		err = bpf_mem_alloc_init(&htab->ma, htab->elem_size, false);
589fba1a1c6SAlexei Starovoitov 		if (err)
590fba1a1c6SAlexei Starovoitov 			goto free_map_locked;
591ee4ed53cSAlexei Starovoitov 		if (percpu) {
592ee4ed53cSAlexei Starovoitov 			err = bpf_mem_alloc_init(&htab->pcpu_ma,
593ee4ed53cSAlexei Starovoitov 						 round_up(htab->map.value_size, 8), true);
594ee4ed53cSAlexei Starovoitov 			if (err)
595ee4ed53cSAlexei Starovoitov 				goto free_map_locked;
596ee4ed53cSAlexei Starovoitov 		}
597a6ed3ea6SAlexei Starovoitov 	}
598a6ed3ea6SAlexei Starovoitov 
5990f8e4bd8SAlexei Starovoitov 	return &htab->map;
6000f8e4bd8SAlexei Starovoitov 
6018c290e60SAlexei Starovoitov free_prealloc:
6028c290e60SAlexei Starovoitov 	prealloc_destroy(htab);
60320b6cc34SSong Liu free_map_locked:
604cf7de6a5STetsuo Handa 	if (htab->use_percpu_counter)
605cf7de6a5STetsuo Handa 		percpu_counter_destroy(&htab->pcount);
60620b6cc34SSong Liu 	for (i = 0; i < HASHTAB_MAP_LOCK_COUNT; i++)
60720b6cc34SSong Liu 		free_percpu(htab->map_locked[i]);
608d407bd25SDaniel Borkmann 	bpf_map_area_free(htab->buckets);
609ee4ed53cSAlexei Starovoitov 	bpf_mem_alloc_destroy(&htab->pcpu_ma);
610fba1a1c6SAlexei Starovoitov 	bpf_mem_alloc_destroy(&htab->ma);
6119bc421b6SAnton Protopopov free_elem_count:
6129bc421b6SAnton Protopopov 	bpf_map_free_elem_count(&htab->map);
6130f8e4bd8SAlexei Starovoitov free_htab:
6148aaeed81SEric Dumazet 	lockdep_unregister_key(&htab->lockdep_key);
61573cf09a3SYafang Shao 	bpf_map_area_free(htab);
6160f8e4bd8SAlexei Starovoitov 	return ERR_PTR(err);
6170f8e4bd8SAlexei Starovoitov }
6180f8e4bd8SAlexei Starovoitov 
htab_map_hash(const void * key,u32 key_len,u32 hashrnd)619c0203475SDaniel Borkmann static inline u32 htab_map_hash(const void *key, u32 key_len, u32 hashrnd)
6200f8e4bd8SAlexei Starovoitov {
6215b85575aSAnton Protopopov 	if (likely(key_len % 4 == 0))
6225b85575aSAnton Protopopov 		return jhash2(key, key_len / 4, hashrnd);
623c0203475SDaniel Borkmann 	return jhash(key, key_len, hashrnd);
6240f8e4bd8SAlexei Starovoitov }
6250f8e4bd8SAlexei Starovoitov 
__select_bucket(struct bpf_htab * htab,u32 hash)626688ecfe6Stom.leiming@gmail.com static inline struct bucket *__select_bucket(struct bpf_htab *htab, u32 hash)
6270f8e4bd8SAlexei Starovoitov {
6280f8e4bd8SAlexei Starovoitov 	return &htab->buckets[hash & (htab->n_buckets - 1)];
6290f8e4bd8SAlexei Starovoitov }
6300f8e4bd8SAlexei Starovoitov 
select_bucket(struct bpf_htab * htab,u32 hash)6314fe84359SAlexei Starovoitov static inline struct hlist_nulls_head *select_bucket(struct bpf_htab *htab, u32 hash)
632688ecfe6Stom.leiming@gmail.com {
633688ecfe6Stom.leiming@gmail.com 	return &__select_bucket(htab, hash)->head;
634688ecfe6Stom.leiming@gmail.com }
635688ecfe6Stom.leiming@gmail.com 
6364fe84359SAlexei Starovoitov /* this lookup function can only be called with bucket lock taken */
lookup_elem_raw(struct hlist_nulls_head * head,u32 hash,void * key,u32 key_size)6374fe84359SAlexei Starovoitov static struct htab_elem *lookup_elem_raw(struct hlist_nulls_head *head, u32 hash,
6380f8e4bd8SAlexei Starovoitov 					 void *key, u32 key_size)
6390f8e4bd8SAlexei Starovoitov {
6404fe84359SAlexei Starovoitov 	struct hlist_nulls_node *n;
6410f8e4bd8SAlexei Starovoitov 	struct htab_elem *l;
6420f8e4bd8SAlexei Starovoitov 
6434fe84359SAlexei Starovoitov 	hlist_nulls_for_each_entry_rcu(l, n, head, hash_node)
6440f8e4bd8SAlexei Starovoitov 		if (l->hash == hash && !memcmp(&l->key, key, key_size))
6450f8e4bd8SAlexei Starovoitov 			return l;
6460f8e4bd8SAlexei Starovoitov 
6470f8e4bd8SAlexei Starovoitov 	return NULL;
6480f8e4bd8SAlexei Starovoitov }
6490f8e4bd8SAlexei Starovoitov 
6504fe84359SAlexei Starovoitov /* can be called without bucket lock. it will repeat the loop in
6514fe84359SAlexei Starovoitov  * the unlikely event when elements moved from one bucket into another
6524fe84359SAlexei Starovoitov  * while link list is being walked
6534fe84359SAlexei Starovoitov  */
lookup_nulls_elem_raw(struct hlist_nulls_head * head,u32 hash,void * key,u32 key_size,u32 n_buckets)6544fe84359SAlexei Starovoitov static struct htab_elem *lookup_nulls_elem_raw(struct hlist_nulls_head *head,
6554fe84359SAlexei Starovoitov 					       u32 hash, void *key,
6564fe84359SAlexei Starovoitov 					       u32 key_size, u32 n_buckets)
6574fe84359SAlexei Starovoitov {
6584fe84359SAlexei Starovoitov 	struct hlist_nulls_node *n;
6594fe84359SAlexei Starovoitov 	struct htab_elem *l;
6604fe84359SAlexei Starovoitov 
6614fe84359SAlexei Starovoitov again:
6624fe84359SAlexei Starovoitov 	hlist_nulls_for_each_entry_rcu(l, n, head, hash_node)
6634fe84359SAlexei Starovoitov 		if (l->hash == hash && !memcmp(&l->key, key, key_size))
6644fe84359SAlexei Starovoitov 			return l;
6654fe84359SAlexei Starovoitov 
6664fe84359SAlexei Starovoitov 	if (unlikely(get_nulls_value(n) != (hash & (n_buckets - 1))))
6674fe84359SAlexei Starovoitov 		goto again;
6684fe84359SAlexei Starovoitov 
6694fe84359SAlexei Starovoitov 	return NULL;
6704fe84359SAlexei Starovoitov }
6714fe84359SAlexei Starovoitov 
6729015d2f5SAlexei Starovoitov /* Called from syscall or from eBPF program directly, so
6739015d2f5SAlexei Starovoitov  * arguments have to match bpf_map_lookup_elem() exactly.
6749015d2f5SAlexei Starovoitov  * The return value is adjusted by BPF instructions
6759015d2f5SAlexei Starovoitov  * in htab_map_gen_lookup().
6769015d2f5SAlexei Starovoitov  */
__htab_map_lookup_elem(struct bpf_map * map,void * key)677824bd0ceSAlexei Starovoitov static void *__htab_map_lookup_elem(struct bpf_map *map, void *key)
6780f8e4bd8SAlexei Starovoitov {
6790f8e4bd8SAlexei Starovoitov 	struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
6804fe84359SAlexei Starovoitov 	struct hlist_nulls_head *head;
6810f8e4bd8SAlexei Starovoitov 	struct htab_elem *l;
6820f8e4bd8SAlexei Starovoitov 	u32 hash, key_size;
6830f8e4bd8SAlexei Starovoitov 
684694cea39SToke Høiland-Jørgensen 	WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_trace_held() &&
685694cea39SToke Høiland-Jørgensen 		     !rcu_read_lock_bh_held());
6860f8e4bd8SAlexei Starovoitov 
6870f8e4bd8SAlexei Starovoitov 	key_size = map->key_size;
6880f8e4bd8SAlexei Starovoitov 
689c0203475SDaniel Borkmann 	hash = htab_map_hash(key, key_size, htab->hashrnd);
6900f8e4bd8SAlexei Starovoitov 
6910f8e4bd8SAlexei Starovoitov 	head = select_bucket(htab, hash);
6920f8e4bd8SAlexei Starovoitov 
6934fe84359SAlexei Starovoitov 	l = lookup_nulls_elem_raw(head, hash, key, key_size, htab->n_buckets);
6940f8e4bd8SAlexei Starovoitov 
695824bd0ceSAlexei Starovoitov 	return l;
696824bd0ceSAlexei Starovoitov }
697824bd0ceSAlexei Starovoitov 
htab_map_lookup_elem(struct bpf_map * map,void * key)698824bd0ceSAlexei Starovoitov static void *htab_map_lookup_elem(struct bpf_map *map, void *key)
699824bd0ceSAlexei Starovoitov {
700824bd0ceSAlexei Starovoitov 	struct htab_elem *l = __htab_map_lookup_elem(map, key);
701824bd0ceSAlexei Starovoitov 
7020f8e4bd8SAlexei Starovoitov 	if (l)
7030f8e4bd8SAlexei Starovoitov 		return l->key + round_up(map->key_size, 8);
7040f8e4bd8SAlexei Starovoitov 
7050f8e4bd8SAlexei Starovoitov 	return NULL;
7060f8e4bd8SAlexei Starovoitov }
7070f8e4bd8SAlexei Starovoitov 
7089015d2f5SAlexei Starovoitov /* inline bpf_map_lookup_elem() call.
7099015d2f5SAlexei Starovoitov  * Instead of:
7109015d2f5SAlexei Starovoitov  * bpf_prog
7119015d2f5SAlexei Starovoitov  *   bpf_map_lookup_elem
7129015d2f5SAlexei Starovoitov  *     map->ops->map_lookup_elem
7139015d2f5SAlexei Starovoitov  *       htab_map_lookup_elem
7149015d2f5SAlexei Starovoitov  *         __htab_map_lookup_elem
7159015d2f5SAlexei Starovoitov  * do:
7169015d2f5SAlexei Starovoitov  * bpf_prog
7179015d2f5SAlexei Starovoitov  *   __htab_map_lookup_elem
7189015d2f5SAlexei Starovoitov  */
htab_map_gen_lookup(struct bpf_map * map,struct bpf_insn * insn_buf)7194a8f87e6SDaniel Borkmann static int htab_map_gen_lookup(struct bpf_map *map, struct bpf_insn *insn_buf)
7209015d2f5SAlexei Starovoitov {
7219015d2f5SAlexei Starovoitov 	struct bpf_insn *insn = insn_buf;
7229015d2f5SAlexei Starovoitov 	const int ret = BPF_REG_0;
7239015d2f5SAlexei Starovoitov 
72409772d92SDaniel Borkmann 	BUILD_BUG_ON(!__same_type(&__htab_map_lookup_elem,
72509772d92SDaniel Borkmann 		     (void *(*)(struct bpf_map *map, void *key))NULL));
7263d717fadSKees Cook 	*insn++ = BPF_EMIT_CALL(__htab_map_lookup_elem);
7279015d2f5SAlexei Starovoitov 	*insn++ = BPF_JMP_IMM(BPF_JEQ, ret, 0, 1);
7289015d2f5SAlexei Starovoitov 	*insn++ = BPF_ALU64_IMM(BPF_ADD, ret,
7299015d2f5SAlexei Starovoitov 				offsetof(struct htab_elem, key) +
7309015d2f5SAlexei Starovoitov 				round_up(map->key_size, 8));
7319015d2f5SAlexei Starovoitov 	return insn - insn_buf;
7329015d2f5SAlexei Starovoitov }
7339015d2f5SAlexei Starovoitov 
__htab_lru_map_lookup_elem(struct bpf_map * map,void * key,const bool mark)73450b045a8SDaniel Borkmann static __always_inline void *__htab_lru_map_lookup_elem(struct bpf_map *map,
73550b045a8SDaniel Borkmann 							void *key, const bool mark)
73629ba732aSMartin KaFai Lau {
73729ba732aSMartin KaFai Lau 	struct htab_elem *l = __htab_map_lookup_elem(map, key);
73829ba732aSMartin KaFai Lau 
73929ba732aSMartin KaFai Lau 	if (l) {
74050b045a8SDaniel Borkmann 		if (mark)
74129ba732aSMartin KaFai Lau 			bpf_lru_node_set_ref(&l->lru_node);
74229ba732aSMartin KaFai Lau 		return l->key + round_up(map->key_size, 8);
74329ba732aSMartin KaFai Lau 	}
74429ba732aSMartin KaFai Lau 
74529ba732aSMartin KaFai Lau 	return NULL;
74629ba732aSMartin KaFai Lau }
74729ba732aSMartin KaFai Lau 
htab_lru_map_lookup_elem(struct bpf_map * map,void * key)74850b045a8SDaniel Borkmann static void *htab_lru_map_lookup_elem(struct bpf_map *map, void *key)
74950b045a8SDaniel Borkmann {
75050b045a8SDaniel Borkmann 	return __htab_lru_map_lookup_elem(map, key, true);
75150b045a8SDaniel Borkmann }
75250b045a8SDaniel Borkmann 
htab_lru_map_lookup_elem_sys(struct bpf_map * map,void * key)75350b045a8SDaniel Borkmann static void *htab_lru_map_lookup_elem_sys(struct bpf_map *map, void *key)
75450b045a8SDaniel Borkmann {
75550b045a8SDaniel Borkmann 	return __htab_lru_map_lookup_elem(map, key, false);
75650b045a8SDaniel Borkmann }
75750b045a8SDaniel Borkmann 
htab_lru_map_gen_lookup(struct bpf_map * map,struct bpf_insn * insn_buf)7584a8f87e6SDaniel Borkmann static int htab_lru_map_gen_lookup(struct bpf_map *map,
759cc555421SMartin KaFai Lau 				   struct bpf_insn *insn_buf)
760cc555421SMartin KaFai Lau {
761cc555421SMartin KaFai Lau 	struct bpf_insn *insn = insn_buf;
762cc555421SMartin KaFai Lau 	const int ret = BPF_REG_0;
763bb9b9f88SMartin KaFai Lau 	const int ref_reg = BPF_REG_1;
764cc555421SMartin KaFai Lau 
76509772d92SDaniel Borkmann 	BUILD_BUG_ON(!__same_type(&__htab_map_lookup_elem,
76609772d92SDaniel Borkmann 		     (void *(*)(struct bpf_map *map, void *key))NULL));
7673d717fadSKees Cook 	*insn++ = BPF_EMIT_CALL(__htab_map_lookup_elem);
768bb9b9f88SMartin KaFai Lau 	*insn++ = BPF_JMP_IMM(BPF_JEQ, ret, 0, 4);
769bb9b9f88SMartin KaFai Lau 	*insn++ = BPF_LDX_MEM(BPF_B, ref_reg, ret,
770bb9b9f88SMartin KaFai Lau 			      offsetof(struct htab_elem, lru_node) +
771bb9b9f88SMartin KaFai Lau 			      offsetof(struct bpf_lru_node, ref));
772bb9b9f88SMartin KaFai Lau 	*insn++ = BPF_JMP_IMM(BPF_JNE, ref_reg, 0, 1);
773cc555421SMartin KaFai Lau 	*insn++ = BPF_ST_MEM(BPF_B, ret,
774cc555421SMartin KaFai Lau 			     offsetof(struct htab_elem, lru_node) +
775cc555421SMartin KaFai Lau 			     offsetof(struct bpf_lru_node, ref),
776cc555421SMartin KaFai Lau 			     1);
777cc555421SMartin KaFai Lau 	*insn++ = BPF_ALU64_IMM(BPF_ADD, ret,
778cc555421SMartin KaFai Lau 				offsetof(struct htab_elem, key) +
779cc555421SMartin KaFai Lau 				round_up(map->key_size, 8));
780cc555421SMartin KaFai Lau 	return insn - insn_buf;
781cc555421SMartin KaFai Lau }
782cc555421SMartin KaFai Lau 
check_and_free_fields(struct bpf_htab * htab,struct htab_elem * elem)78314a324f6SKumar Kartikeya Dwivedi static void check_and_free_fields(struct bpf_htab *htab,
78414a324f6SKumar Kartikeya Dwivedi 				  struct htab_elem *elem)
78568134668SAlexei Starovoitov {
78665334e64SKumar Kartikeya Dwivedi 	if (htab_is_percpu(htab)) {
78765334e64SKumar Kartikeya Dwivedi 		void __percpu *pptr = htab_elem_get_ptr(elem, htab->map.key_size);
78865334e64SKumar Kartikeya Dwivedi 		int cpu;
78965334e64SKumar Kartikeya Dwivedi 
79065334e64SKumar Kartikeya Dwivedi 		for_each_possible_cpu(cpu)
79165334e64SKumar Kartikeya Dwivedi 			bpf_obj_free_fields(htab->map.record, per_cpu_ptr(pptr, cpu));
79265334e64SKumar Kartikeya Dwivedi 	} else {
79314a324f6SKumar Kartikeya Dwivedi 		void *map_value = elem->key + round_up(htab->map.key_size, 8);
79414a324f6SKumar Kartikeya Dwivedi 
795aa3496acSKumar Kartikeya Dwivedi 		bpf_obj_free_fields(htab->map.record, map_value);
79668134668SAlexei Starovoitov 	}
79765334e64SKumar Kartikeya Dwivedi }
79868134668SAlexei Starovoitov 
79929ba732aSMartin KaFai Lau /* It is called from the bpf_lru_list when the LRU needs to delete
80029ba732aSMartin KaFai Lau  * older elements from the htab.
80129ba732aSMartin KaFai Lau  */
htab_lru_map_delete_node(void * arg,struct bpf_lru_node * node)80229ba732aSMartin KaFai Lau static bool htab_lru_map_delete_node(void *arg, struct bpf_lru_node *node)
80329ba732aSMartin KaFai Lau {
804241d50ecSYu Zhe 	struct bpf_htab *htab = arg;
8054fe84359SAlexei Starovoitov 	struct htab_elem *l = NULL, *tgt_l;
8064fe84359SAlexei Starovoitov 	struct hlist_nulls_head *head;
8074fe84359SAlexei Starovoitov 	struct hlist_nulls_node *n;
80829ba732aSMartin KaFai Lau 	unsigned long flags;
80929ba732aSMartin KaFai Lau 	struct bucket *b;
81020b6cc34SSong Liu 	int ret;
81129ba732aSMartin KaFai Lau 
81229ba732aSMartin KaFai Lau 	tgt_l = container_of(node, struct htab_elem, lru_node);
81329ba732aSMartin KaFai Lau 	b = __select_bucket(htab, tgt_l->hash);
81429ba732aSMartin KaFai Lau 	head = &b->head;
81529ba732aSMartin KaFai Lau 
81620b6cc34SSong Liu 	ret = htab_lock_bucket(htab, b, tgt_l->hash, &flags);
81720b6cc34SSong Liu 	if (ret)
81820b6cc34SSong Liu 		return false;
81929ba732aSMartin KaFai Lau 
8204fe84359SAlexei Starovoitov 	hlist_nulls_for_each_entry_rcu(l, n, head, hash_node)
82129ba732aSMartin KaFai Lau 		if (l == tgt_l) {
8224fe84359SAlexei Starovoitov 			hlist_nulls_del_rcu(&l->hash_node);
82314a324f6SKumar Kartikeya Dwivedi 			check_and_free_fields(htab, l);
8249bc421b6SAnton Protopopov 			bpf_map_dec_elem_count(&htab->map);
82529ba732aSMartin KaFai Lau 			break;
82629ba732aSMartin KaFai Lau 		}
82729ba732aSMartin KaFai Lau 
82820b6cc34SSong Liu 	htab_unlock_bucket(htab, b, tgt_l->hash, flags);
82929ba732aSMartin KaFai Lau 
83029ba732aSMartin KaFai Lau 	return l == tgt_l;
83129ba732aSMartin KaFai Lau }
83229ba732aSMartin KaFai Lau 
8330f8e4bd8SAlexei Starovoitov /* Called from syscall */
htab_map_get_next_key(struct bpf_map * map,void * key,void * next_key)8340f8e4bd8SAlexei Starovoitov static int htab_map_get_next_key(struct bpf_map *map, void *key, void *next_key)
8350f8e4bd8SAlexei Starovoitov {
8360f8e4bd8SAlexei Starovoitov 	struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
8374fe84359SAlexei Starovoitov 	struct hlist_nulls_head *head;
8380f8e4bd8SAlexei Starovoitov 	struct htab_elem *l, *next_l;
8390f8e4bd8SAlexei Starovoitov 	u32 hash, key_size;
8408fe45924STeng Qin 	int i = 0;
8410f8e4bd8SAlexei Starovoitov 
8420f8e4bd8SAlexei Starovoitov 	WARN_ON_ONCE(!rcu_read_lock_held());
8430f8e4bd8SAlexei Starovoitov 
8440f8e4bd8SAlexei Starovoitov 	key_size = map->key_size;
8450f8e4bd8SAlexei Starovoitov 
8468fe45924STeng Qin 	if (!key)
8478fe45924STeng Qin 		goto find_first_elem;
8488fe45924STeng Qin 
849c0203475SDaniel Borkmann 	hash = htab_map_hash(key, key_size, htab->hashrnd);
8500f8e4bd8SAlexei Starovoitov 
8510f8e4bd8SAlexei Starovoitov 	head = select_bucket(htab, hash);
8520f8e4bd8SAlexei Starovoitov 
8530f8e4bd8SAlexei Starovoitov 	/* lookup the key */
8544fe84359SAlexei Starovoitov 	l = lookup_nulls_elem_raw(head, hash, key, key_size, htab->n_buckets);
8550f8e4bd8SAlexei Starovoitov 
8568fe45924STeng Qin 	if (!l)
8570f8e4bd8SAlexei Starovoitov 		goto find_first_elem;
8580f8e4bd8SAlexei Starovoitov 
8590f8e4bd8SAlexei Starovoitov 	/* key was found, get next key in the same bucket */
8604fe84359SAlexei Starovoitov 	next_l = hlist_nulls_entry_safe(rcu_dereference_raw(hlist_nulls_next_rcu(&l->hash_node)),
8610f8e4bd8SAlexei Starovoitov 				  struct htab_elem, hash_node);
8620f8e4bd8SAlexei Starovoitov 
8630f8e4bd8SAlexei Starovoitov 	if (next_l) {
8640f8e4bd8SAlexei Starovoitov 		/* if next elem in this hash list is non-zero, just return it */
8650f8e4bd8SAlexei Starovoitov 		memcpy(next_key, next_l->key, key_size);
8660f8e4bd8SAlexei Starovoitov 		return 0;
8670f8e4bd8SAlexei Starovoitov 	}
8680f8e4bd8SAlexei Starovoitov 
8690f8e4bd8SAlexei Starovoitov 	/* no more elements in this hash list, go to the next bucket */
8700f8e4bd8SAlexei Starovoitov 	i = hash & (htab->n_buckets - 1);
8710f8e4bd8SAlexei Starovoitov 	i++;
8720f8e4bd8SAlexei Starovoitov 
8730f8e4bd8SAlexei Starovoitov find_first_elem:
8740f8e4bd8SAlexei Starovoitov 	/* iterate over buckets */
8750f8e4bd8SAlexei Starovoitov 	for (; i < htab->n_buckets; i++) {
8760f8e4bd8SAlexei Starovoitov 		head = select_bucket(htab, i);
8770f8e4bd8SAlexei Starovoitov 
8780f8e4bd8SAlexei Starovoitov 		/* pick first element in the bucket */
8794fe84359SAlexei Starovoitov 		next_l = hlist_nulls_entry_safe(rcu_dereference_raw(hlist_nulls_first_rcu(head)),
8800f8e4bd8SAlexei Starovoitov 					  struct htab_elem, hash_node);
8810f8e4bd8SAlexei Starovoitov 		if (next_l) {
8820f8e4bd8SAlexei Starovoitov 			/* if it's not empty, just return it */
8830f8e4bd8SAlexei Starovoitov 			memcpy(next_key, next_l->key, key_size);
8840f8e4bd8SAlexei Starovoitov 			return 0;
8850f8e4bd8SAlexei Starovoitov 		}
8860f8e4bd8SAlexei Starovoitov 	}
8870f8e4bd8SAlexei Starovoitov 
8886c905981SAlexei Starovoitov 	/* iterated over all buckets and all elements */
8890f8e4bd8SAlexei Starovoitov 	return -ENOENT;
8900f8e4bd8SAlexei Starovoitov }
8910f8e4bd8SAlexei Starovoitov 
htab_elem_free(struct bpf_htab * htab,struct htab_elem * l)8926c905981SAlexei Starovoitov static void htab_elem_free(struct bpf_htab *htab, struct htab_elem *l)
893824bd0ceSAlexei Starovoitov {
89465334e64SKumar Kartikeya Dwivedi 	check_and_free_fields(htab, l);
895*10e8a2deSHou Tao 
896*10e8a2deSHou Tao 	migrate_disable();
8976c905981SAlexei Starovoitov 	if (htab->map.map_type == BPF_MAP_TYPE_PERCPU_HASH)
898ee4ed53cSAlexei Starovoitov 		bpf_mem_cache_free(&htab->pcpu_ma, l->ptr_to_pptr);
899fba1a1c6SAlexei Starovoitov 	bpf_mem_cache_free(&htab->ma, l);
900*10e8a2deSHou Tao 	migrate_enable();
901824bd0ceSAlexei Starovoitov }
902824bd0ceSAlexei Starovoitov 
htab_put_fd_value(struct bpf_htab * htab,struct htab_elem * l)9031d4e1eabSAndrii Nakryiko static void htab_put_fd_value(struct bpf_htab *htab, struct htab_elem *l)
904824bd0ceSAlexei Starovoitov {
905bcc6b1b7SMartin KaFai Lau 	struct bpf_map *map = &htab->map;
9061d4e1eabSAndrii Nakryiko 	void *ptr;
907bcc6b1b7SMartin KaFai Lau 
908bcc6b1b7SMartin KaFai Lau 	if (map->ops->map_fd_put_ptr) {
9091d4e1eabSAndrii Nakryiko 		ptr = fd_htab_map_get_ptr(map, l);
9101c40ec6bSHou Tao 		map->ops->map_fd_put_ptr(map, ptr, true);
911bcc6b1b7SMartin KaFai Lau 	}
9121d4e1eabSAndrii Nakryiko }
9131d4e1eabSAndrii Nakryiko 
is_map_full(struct bpf_htab * htab)91486fe28f7SAlexei Starovoitov static bool is_map_full(struct bpf_htab *htab)
91586fe28f7SAlexei Starovoitov {
91686fe28f7SAlexei Starovoitov 	if (htab->use_percpu_counter)
91786fe28f7SAlexei Starovoitov 		return __percpu_counter_compare(&htab->pcount, htab->map.max_entries,
91886fe28f7SAlexei Starovoitov 						PERCPU_COUNTER_BATCH) >= 0;
91986fe28f7SAlexei Starovoitov 	return atomic_read(&htab->count) >= htab->map.max_entries;
92086fe28f7SAlexei Starovoitov }
92186fe28f7SAlexei Starovoitov 
inc_elem_count(struct bpf_htab * htab)92286fe28f7SAlexei Starovoitov static void inc_elem_count(struct bpf_htab *htab)
92386fe28f7SAlexei Starovoitov {
9249bc421b6SAnton Protopopov 	bpf_map_inc_elem_count(&htab->map);
9259bc421b6SAnton Protopopov 
92686fe28f7SAlexei Starovoitov 	if (htab->use_percpu_counter)
92786fe28f7SAlexei Starovoitov 		percpu_counter_add_batch(&htab->pcount, 1, PERCPU_COUNTER_BATCH);
92886fe28f7SAlexei Starovoitov 	else
92986fe28f7SAlexei Starovoitov 		atomic_inc(&htab->count);
93086fe28f7SAlexei Starovoitov }
93186fe28f7SAlexei Starovoitov 
dec_elem_count(struct bpf_htab * htab)93286fe28f7SAlexei Starovoitov static void dec_elem_count(struct bpf_htab *htab)
93386fe28f7SAlexei Starovoitov {
9349bc421b6SAnton Protopopov 	bpf_map_dec_elem_count(&htab->map);
9359bc421b6SAnton Protopopov 
93686fe28f7SAlexei Starovoitov 	if (htab->use_percpu_counter)
93786fe28f7SAlexei Starovoitov 		percpu_counter_add_batch(&htab->pcount, -1, PERCPU_COUNTER_BATCH);
93886fe28f7SAlexei Starovoitov 	else
93986fe28f7SAlexei Starovoitov 		atomic_dec(&htab->count);
94086fe28f7SAlexei Starovoitov }
94186fe28f7SAlexei Starovoitov 
94286fe28f7SAlexei Starovoitov 
free_htab_elem(struct bpf_htab * htab,struct htab_elem * l)9431d4e1eabSAndrii Nakryiko static void free_htab_elem(struct bpf_htab *htab, struct htab_elem *l)
9441d4e1eabSAndrii Nakryiko {
9451d4e1eabSAndrii Nakryiko 	htab_put_fd_value(htab, l);
946bcc6b1b7SMartin KaFai Lau 
9478c290e60SAlexei Starovoitov 	if (htab_is_prealloc(htab)) {
9489bc421b6SAnton Protopopov 		bpf_map_dec_elem_count(&htab->map);
94914a324f6SKumar Kartikeya Dwivedi 		check_and_free_fields(htab, l);
950*10e8a2deSHou Tao 		pcpu_freelist_push(&htab->freelist, &l->fnode);
951824bd0ceSAlexei Starovoitov 	} else {
95286fe28f7SAlexei Starovoitov 		dec_elem_count(htab);
9530fd7c5d4SAlexei Starovoitov 		htab_elem_free(htab, l);
9540fd7c5d4SAlexei Starovoitov 	}
955824bd0ceSAlexei Starovoitov }
956824bd0ceSAlexei Starovoitov 
pcpu_copy_value(struct bpf_htab * htab,void __percpu * pptr,void * value,bool onallcpus)957fd91de7bSMartin KaFai Lau static void pcpu_copy_value(struct bpf_htab *htab, void __percpu *pptr,
958fd91de7bSMartin KaFai Lau 			    void *value, bool onallcpus)
959fd91de7bSMartin KaFai Lau {
960fd91de7bSMartin KaFai Lau 	if (!onallcpus) {
961fd91de7bSMartin KaFai Lau 		/* copy true value_size bytes */
96265334e64SKumar Kartikeya Dwivedi 		copy_map_value(&htab->map, this_cpu_ptr(pptr), value);
963fd91de7bSMartin KaFai Lau 	} else {
964fd91de7bSMartin KaFai Lau 		u32 size = round_up(htab->map.value_size, 8);
965fd91de7bSMartin KaFai Lau 		int off = 0, cpu;
966fd91de7bSMartin KaFai Lau 
967fd91de7bSMartin KaFai Lau 		for_each_possible_cpu(cpu) {
96865334e64SKumar Kartikeya Dwivedi 			copy_map_value_long(&htab->map, per_cpu_ptr(pptr, cpu), value + off);
969fd91de7bSMartin KaFai Lau 			off += size;
970fd91de7bSMartin KaFai Lau 		}
971fd91de7bSMartin KaFai Lau 	}
972fd91de7bSMartin KaFai Lau }
973fd91de7bSMartin KaFai Lau 
pcpu_init_value(struct bpf_htab * htab,void __percpu * pptr,void * value,bool onallcpus)974d3bec013SDavid Verbeiren static void pcpu_init_value(struct bpf_htab *htab, void __percpu *pptr,
975d3bec013SDavid Verbeiren 			    void *value, bool onallcpus)
976d3bec013SDavid Verbeiren {
977ee4ed53cSAlexei Starovoitov 	/* When not setting the initial value on all cpus, zero-fill element
978ee4ed53cSAlexei Starovoitov 	 * values for other cpus. Otherwise, bpf program has no way to ensure
979d3bec013SDavid Verbeiren 	 * known initial values for cpus other than current one
980d3bec013SDavid Verbeiren 	 * (onallcpus=false always when coming from bpf prog).
981d3bec013SDavid Verbeiren 	 */
982ee4ed53cSAlexei Starovoitov 	if (!onallcpus) {
983d3bec013SDavid Verbeiren 		int current_cpu = raw_smp_processor_id();
984d3bec013SDavid Verbeiren 		int cpu;
985d3bec013SDavid Verbeiren 
986d3bec013SDavid Verbeiren 		for_each_possible_cpu(cpu) {
987d3bec013SDavid Verbeiren 			if (cpu == current_cpu)
98865334e64SKumar Kartikeya Dwivedi 				copy_map_value_long(&htab->map, per_cpu_ptr(pptr, cpu), value);
98965334e64SKumar Kartikeya Dwivedi 			else /* Since elem is preallocated, we cannot touch special fields */
99065334e64SKumar Kartikeya Dwivedi 				zero_map_value(&htab->map, per_cpu_ptr(pptr, cpu));
991d3bec013SDavid Verbeiren 		}
992d3bec013SDavid Verbeiren 	} else {
993d3bec013SDavid Verbeiren 		pcpu_copy_value(htab, pptr, value, onallcpus);
994d3bec013SDavid Verbeiren 	}
995d3bec013SDavid Verbeiren }
996d3bec013SDavid Verbeiren 
fd_htab_map_needs_adjust(const struct bpf_htab * htab)997cd36c3a2SDaniel Borkmann static bool fd_htab_map_needs_adjust(const struct bpf_htab *htab)
998cd36c3a2SDaniel Borkmann {
999cd36c3a2SDaniel Borkmann 	return htab->map.map_type == BPF_MAP_TYPE_HASH_OF_MAPS &&
1000cd36c3a2SDaniel Borkmann 	       BITS_PER_LONG == 64;
1001cd36c3a2SDaniel Borkmann }
1002cd36c3a2SDaniel Borkmann 
alloc_htab_elem(struct bpf_htab * htab,void * key,void * value,u32 key_size,u32 hash,bool percpu,bool onallcpus,struct htab_elem * old_elem)1003824bd0ceSAlexei Starovoitov static struct htab_elem *alloc_htab_elem(struct bpf_htab *htab, void *key,
1004824bd0ceSAlexei Starovoitov 					 void *value, u32 key_size, u32 hash,
1005a6ed3ea6SAlexei Starovoitov 					 bool percpu, bool onallcpus,
10068c290e60SAlexei Starovoitov 					 struct htab_elem *old_elem)
1007824bd0ceSAlexei Starovoitov {
1008d83525caSAlexei Starovoitov 	u32 size = htab->map.value_size;
10098c290e60SAlexei Starovoitov 	bool prealloc = htab_is_prealloc(htab);
10108c290e60SAlexei Starovoitov 	struct htab_elem *l_new, **pl_new;
1011824bd0ceSAlexei Starovoitov 	void __percpu *pptr;
1012824bd0ceSAlexei Starovoitov 
10136c905981SAlexei Starovoitov 	if (prealloc) {
10148c290e60SAlexei Starovoitov 		if (old_elem) {
10158c290e60SAlexei Starovoitov 			/* if we're updating the existing element,
10168c290e60SAlexei Starovoitov 			 * use per-cpu extra elems to avoid freelist_pop/push
10178c290e60SAlexei Starovoitov 			 */
10188c290e60SAlexei Starovoitov 			pl_new = this_cpu_ptr(htab->extra_elems);
10198c290e60SAlexei Starovoitov 			l_new = *pl_new;
10208c290e60SAlexei Starovoitov 			*pl_new = old_elem;
10218c290e60SAlexei Starovoitov 		} else {
10229f691549SAlexei Starovoitov 			struct pcpu_freelist_node *l;
10239f691549SAlexei Starovoitov 
1024a89fac57SAlexei Starovoitov 			l = __pcpu_freelist_pop(&htab->freelist);
10259f691549SAlexei Starovoitov 			if (!l)
10268c290e60SAlexei Starovoitov 				return ERR_PTR(-E2BIG);
10279f691549SAlexei Starovoitov 			l_new = container_of(l, struct htab_elem, fnode);
10289bc421b6SAnton Protopopov 			bpf_map_inc_elem_count(&htab->map);
10298c290e60SAlexei Starovoitov 		}
10306c905981SAlexei Starovoitov 	} else {
103186fe28f7SAlexei Starovoitov 		if (is_map_full(htab))
103286fe28f7SAlexei Starovoitov 			if (!old_elem)
10338c290e60SAlexei Starovoitov 				/* when map is full and update() is replacing
10348c290e60SAlexei Starovoitov 				 * old element, it's ok to allocate, since
10358c290e60SAlexei Starovoitov 				 * old element will be freed immediately.
10368c290e60SAlexei Starovoitov 				 * Otherwise return an error
10378c290e60SAlexei Starovoitov 				 */
103886fe28f7SAlexei Starovoitov 				return ERR_PTR(-E2BIG);
103986fe28f7SAlexei Starovoitov 		inc_elem_count(htab);
1040fba1a1c6SAlexei Starovoitov 		l_new = bpf_mem_cache_alloc(&htab->ma);
1041ed2b82c0SMauricio Vasquez B 		if (!l_new) {
1042ed2b82c0SMauricio Vasquez B 			l_new = ERR_PTR(-ENOMEM);
1043ed2b82c0SMauricio Vasquez B 			goto dec_count;
1044ed2b82c0SMauricio Vasquez B 		}
10456c905981SAlexei Starovoitov 	}
1046824bd0ceSAlexei Starovoitov 
1047824bd0ceSAlexei Starovoitov 	memcpy(l_new->key, key, key_size);
1048824bd0ceSAlexei Starovoitov 	if (percpu) {
10496c905981SAlexei Starovoitov 		if (prealloc) {
10506c905981SAlexei Starovoitov 			pptr = htab_elem_get_ptr(l_new, key_size);
10516c905981SAlexei Starovoitov 		} else {
1052824bd0ceSAlexei Starovoitov 			/* alloc_percpu zero-fills */
1053ee4ed53cSAlexei Starovoitov 			pptr = bpf_mem_cache_alloc(&htab->pcpu_ma);
1054824bd0ceSAlexei Starovoitov 			if (!pptr) {
1055fba1a1c6SAlexei Starovoitov 				bpf_mem_cache_free(&htab->ma, l_new);
1056ed2b82c0SMauricio Vasquez B 				l_new = ERR_PTR(-ENOMEM);
1057ed2b82c0SMauricio Vasquez B 				goto dec_count;
10586c905981SAlexei Starovoitov 			}
1059ee4ed53cSAlexei Starovoitov 			l_new->ptr_to_pptr = pptr;
1060ee4ed53cSAlexei Starovoitov 			pptr = *(void **)pptr;
1061824bd0ceSAlexei Starovoitov 		}
1062824bd0ceSAlexei Starovoitov 
1063d3bec013SDavid Verbeiren 		pcpu_init_value(htab, pptr, value, onallcpus);
106415a07b33SAlexei Starovoitov 
10656c905981SAlexei Starovoitov 		if (!prealloc)
1066824bd0ceSAlexei Starovoitov 			htab_elem_set_ptr(l_new, key_size, pptr);
1067d83525caSAlexei Starovoitov 	} else if (fd_htab_map_needs_adjust(htab)) {
1068d83525caSAlexei Starovoitov 		size = round_up(size, 8);
1069824bd0ceSAlexei Starovoitov 		memcpy(l_new->key + round_up(key_size, 8), value, size);
1070d83525caSAlexei Starovoitov 	} else {
1071d83525caSAlexei Starovoitov 		copy_map_value(&htab->map,
1072d83525caSAlexei Starovoitov 			       l_new->key + round_up(key_size, 8),
1073d83525caSAlexei Starovoitov 			       value);
1074824bd0ceSAlexei Starovoitov 	}
1075824bd0ceSAlexei Starovoitov 
1076824bd0ceSAlexei Starovoitov 	l_new->hash = hash;
1077824bd0ceSAlexei Starovoitov 	return l_new;
1078ed2b82c0SMauricio Vasquez B dec_count:
107986fe28f7SAlexei Starovoitov 	dec_elem_count(htab);
1080ed2b82c0SMauricio Vasquez B 	return l_new;
1081824bd0ceSAlexei Starovoitov }
1082824bd0ceSAlexei Starovoitov 
check_flags(struct bpf_htab * htab,struct htab_elem * l_old,u64 map_flags)1083824bd0ceSAlexei Starovoitov static int check_flags(struct bpf_htab *htab, struct htab_elem *l_old,
1084824bd0ceSAlexei Starovoitov 		       u64 map_flags)
1085824bd0ceSAlexei Starovoitov {
108696049f3aSAlexei Starovoitov 	if (l_old && (map_flags & ~BPF_F_LOCK) == BPF_NOEXIST)
1087824bd0ceSAlexei Starovoitov 		/* elem already exists */
1088824bd0ceSAlexei Starovoitov 		return -EEXIST;
1089824bd0ceSAlexei Starovoitov 
109096049f3aSAlexei Starovoitov 	if (!l_old && (map_flags & ~BPF_F_LOCK) == BPF_EXIST)
1091824bd0ceSAlexei Starovoitov 		/* elem doesn't exist, cannot update it */
1092824bd0ceSAlexei Starovoitov 		return -ENOENT;
1093824bd0ceSAlexei Starovoitov 
1094824bd0ceSAlexei Starovoitov 	return 0;
1095824bd0ceSAlexei Starovoitov }
1096824bd0ceSAlexei Starovoitov 
10970f8e4bd8SAlexei Starovoitov /* Called from syscall or from eBPF program */
htab_map_update_elem(struct bpf_map * map,void * key,void * value,u64 map_flags)1098d7ba4cc9SJP Kobryn static long htab_map_update_elem(struct bpf_map *map, void *key, void *value,
10990f8e4bd8SAlexei Starovoitov 				 u64 map_flags)
11000f8e4bd8SAlexei Starovoitov {
11010f8e4bd8SAlexei Starovoitov 	struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1102824bd0ceSAlexei Starovoitov 	struct htab_elem *l_new = NULL, *l_old;
11034fe84359SAlexei Starovoitov 	struct hlist_nulls_head *head;
11040f8e4bd8SAlexei Starovoitov 	unsigned long flags;
1105*10e8a2deSHou Tao 	void *old_map_ptr;
1106824bd0ceSAlexei Starovoitov 	struct bucket *b;
1107824bd0ceSAlexei Starovoitov 	u32 key_size, hash;
11080f8e4bd8SAlexei Starovoitov 	int ret;
11090f8e4bd8SAlexei Starovoitov 
111096049f3aSAlexei Starovoitov 	if (unlikely((map_flags & ~BPF_F_LOCK) > BPF_EXIST))
11110f8e4bd8SAlexei Starovoitov 		/* unknown flags */
11120f8e4bd8SAlexei Starovoitov 		return -EINVAL;
11130f8e4bd8SAlexei Starovoitov 
1114694cea39SToke Høiland-Jørgensen 	WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_trace_held() &&
1115694cea39SToke Høiland-Jørgensen 		     !rcu_read_lock_bh_held());
11160f8e4bd8SAlexei Starovoitov 
1117824bd0ceSAlexei Starovoitov 	key_size = map->key_size;
1118824bd0ceSAlexei Starovoitov 
1119c0203475SDaniel Borkmann 	hash = htab_map_hash(key, key_size, htab->hashrnd);
1120824bd0ceSAlexei Starovoitov 
1121824bd0ceSAlexei Starovoitov 	b = __select_bucket(htab, hash);
1122688ecfe6Stom.leiming@gmail.com 	head = &b->head;
11230f8e4bd8SAlexei Starovoitov 
112496049f3aSAlexei Starovoitov 	if (unlikely(map_flags & BPF_F_LOCK)) {
1125db559117SKumar Kartikeya Dwivedi 		if (unlikely(!btf_record_has_field(map->record, BPF_SPIN_LOCK)))
112696049f3aSAlexei Starovoitov 			return -EINVAL;
112796049f3aSAlexei Starovoitov 		/* find an element without taking the bucket lock */
112896049f3aSAlexei Starovoitov 		l_old = lookup_nulls_elem_raw(head, hash, key, key_size,
112996049f3aSAlexei Starovoitov 					      htab->n_buckets);
113096049f3aSAlexei Starovoitov 		ret = check_flags(htab, l_old, map_flags);
113196049f3aSAlexei Starovoitov 		if (ret)
113296049f3aSAlexei Starovoitov 			return ret;
113396049f3aSAlexei Starovoitov 		if (l_old) {
113496049f3aSAlexei Starovoitov 			/* grab the element lock and update value in place */
113596049f3aSAlexei Starovoitov 			copy_map_value_locked(map,
113696049f3aSAlexei Starovoitov 					      l_old->key + round_up(key_size, 8),
113796049f3aSAlexei Starovoitov 					      value, false);
113896049f3aSAlexei Starovoitov 			return 0;
113996049f3aSAlexei Starovoitov 		}
114096049f3aSAlexei Starovoitov 		/* fall through, grab the bucket lock and lookup again.
114196049f3aSAlexei Starovoitov 		 * 99.9% chance that the element won't be found,
114296049f3aSAlexei Starovoitov 		 * but second lookup under lock has to be done.
114396049f3aSAlexei Starovoitov 		 */
114496049f3aSAlexei Starovoitov 	}
114596049f3aSAlexei Starovoitov 
114620b6cc34SSong Liu 	ret = htab_lock_bucket(htab, b, hash, &flags);
114720b6cc34SSong Liu 	if (ret)
114820b6cc34SSong Liu 		return ret;
11490f8e4bd8SAlexei Starovoitov 
1150824bd0ceSAlexei Starovoitov 	l_old = lookup_elem_raw(head, hash, key, key_size);
11510f8e4bd8SAlexei Starovoitov 
1152824bd0ceSAlexei Starovoitov 	ret = check_flags(htab, l_old, map_flags);
1153824bd0ceSAlexei Starovoitov 	if (ret)
11540f8e4bd8SAlexei Starovoitov 		goto err;
11550f8e4bd8SAlexei Starovoitov 
115696049f3aSAlexei Starovoitov 	if (unlikely(l_old && (map_flags & BPF_F_LOCK))) {
115796049f3aSAlexei Starovoitov 		/* first lookup without the bucket lock didn't find the element,
115896049f3aSAlexei Starovoitov 		 * but second lookup with the bucket lock found it.
115996049f3aSAlexei Starovoitov 		 * This case is highly unlikely, but has to be dealt with:
116096049f3aSAlexei Starovoitov 		 * grab the element lock in addition to the bucket lock
116196049f3aSAlexei Starovoitov 		 * and update element in place
116296049f3aSAlexei Starovoitov 		 */
116396049f3aSAlexei Starovoitov 		copy_map_value_locked(map,
116496049f3aSAlexei Starovoitov 				      l_old->key + round_up(key_size, 8),
116596049f3aSAlexei Starovoitov 				      value, false);
116696049f3aSAlexei Starovoitov 		ret = 0;
116796049f3aSAlexei Starovoitov 		goto err;
116896049f3aSAlexei Starovoitov 	}
116996049f3aSAlexei Starovoitov 
1170a6ed3ea6SAlexei Starovoitov 	l_new = alloc_htab_elem(htab, key, value, key_size, hash, false, false,
11718c290e60SAlexei Starovoitov 				l_old);
11726c905981SAlexei Starovoitov 	if (IS_ERR(l_new)) {
11736c905981SAlexei Starovoitov 		/* all pre-allocated elements are in use or memory exhausted */
11746c905981SAlexei Starovoitov 		ret = PTR_ERR(l_new);
11756c905981SAlexei Starovoitov 		goto err;
11766c905981SAlexei Starovoitov 	}
11776c905981SAlexei Starovoitov 
1178824bd0ceSAlexei Starovoitov 	/* add new element to the head of the list, so that
1179824bd0ceSAlexei Starovoitov 	 * concurrent search will find it before old elem
11800f8e4bd8SAlexei Starovoitov 	 */
11814fe84359SAlexei Starovoitov 	hlist_nulls_add_head_rcu(&l_new->hash_node, head);
11820f8e4bd8SAlexei Starovoitov 	if (l_old) {
11834fe84359SAlexei Starovoitov 		hlist_nulls_del_rcu(&l_old->hash_node);
1184*10e8a2deSHou Tao 
1185*10e8a2deSHou Tao 		/* l_old has already been stashed in htab->extra_elems, free
1186*10e8a2deSHou Tao 		 * its special fields before it is available for reuse. Also
1187*10e8a2deSHou Tao 		 * save the old map pointer in htab of maps before unlock
1188*10e8a2deSHou Tao 		 * and release it after unlock.
1189*10e8a2deSHou Tao 		 */
1190*10e8a2deSHou Tao 		old_map_ptr = NULL;
1191*10e8a2deSHou Tao 		if (htab_is_prealloc(htab)) {
1192*10e8a2deSHou Tao 			if (map->ops->map_fd_put_ptr)
1193*10e8a2deSHou Tao 				old_map_ptr = fd_htab_map_get_ptr(map, l_old);
119414a324f6SKumar Kartikeya Dwivedi 			check_and_free_fields(htab, l_old);
11950f8e4bd8SAlexei Starovoitov 		}
1196*10e8a2deSHou Tao 	}
1197*10e8a2deSHou Tao 	htab_unlock_bucket(htab, b, hash, flags);
1198*10e8a2deSHou Tao 	if (l_old) {
1199*10e8a2deSHou Tao 		if (old_map_ptr)
1200*10e8a2deSHou Tao 			map->ops->map_fd_put_ptr(map, old_map_ptr, true);
1201*10e8a2deSHou Tao 		if (!htab_is_prealloc(htab))
1202*10e8a2deSHou Tao 			free_htab_elem(htab, l_old);
1203*10e8a2deSHou Tao 	}
1204*10e8a2deSHou Tao 	return 0;
12050f8e4bd8SAlexei Starovoitov err:
120620b6cc34SSong Liu 	htab_unlock_bucket(htab, b, hash, flags);
12070f8e4bd8SAlexei Starovoitov 	return ret;
12080f8e4bd8SAlexei Starovoitov }
12090f8e4bd8SAlexei Starovoitov 
htab_lru_push_free(struct bpf_htab * htab,struct htab_elem * elem)121068134668SAlexei Starovoitov static void htab_lru_push_free(struct bpf_htab *htab, struct htab_elem *elem)
121168134668SAlexei Starovoitov {
121214a324f6SKumar Kartikeya Dwivedi 	check_and_free_fields(htab, elem);
12139bc421b6SAnton Protopopov 	bpf_map_dec_elem_count(&htab->map);
121468134668SAlexei Starovoitov 	bpf_lru_push_free(&htab->lru, &elem->lru_node);
121568134668SAlexei Starovoitov }
121668134668SAlexei Starovoitov 
htab_lru_map_update_elem(struct bpf_map * map,void * key,void * value,u64 map_flags)1217d7ba4cc9SJP Kobryn static long htab_lru_map_update_elem(struct bpf_map *map, void *key, void *value,
121829ba732aSMartin KaFai Lau 				     u64 map_flags)
121929ba732aSMartin KaFai Lau {
122029ba732aSMartin KaFai Lau 	struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
122129ba732aSMartin KaFai Lau 	struct htab_elem *l_new, *l_old = NULL;
12224fe84359SAlexei Starovoitov 	struct hlist_nulls_head *head;
122329ba732aSMartin KaFai Lau 	unsigned long flags;
122429ba732aSMartin KaFai Lau 	struct bucket *b;
122529ba732aSMartin KaFai Lau 	u32 key_size, hash;
122629ba732aSMartin KaFai Lau 	int ret;
122729ba732aSMartin KaFai Lau 
122829ba732aSMartin KaFai Lau 	if (unlikely(map_flags > BPF_EXIST))
122929ba732aSMartin KaFai Lau 		/* unknown flags */
123029ba732aSMartin KaFai Lau 		return -EINVAL;
123129ba732aSMartin KaFai Lau 
1232694cea39SToke Høiland-Jørgensen 	WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_trace_held() &&
1233694cea39SToke Høiland-Jørgensen 		     !rcu_read_lock_bh_held());
123429ba732aSMartin KaFai Lau 
123529ba732aSMartin KaFai Lau 	key_size = map->key_size;
123629ba732aSMartin KaFai Lau 
1237c0203475SDaniel Borkmann 	hash = htab_map_hash(key, key_size, htab->hashrnd);
123829ba732aSMartin KaFai Lau 
123929ba732aSMartin KaFai Lau 	b = __select_bucket(htab, hash);
124029ba732aSMartin KaFai Lau 	head = &b->head;
124129ba732aSMartin KaFai Lau 
124229ba732aSMartin KaFai Lau 	/* For LRU, we need to alloc before taking bucket's
124329ba732aSMartin KaFai Lau 	 * spinlock because getting free nodes from LRU may need
124429ba732aSMartin KaFai Lau 	 * to remove older elements from htab and this removal
124529ba732aSMartin KaFai Lau 	 * operation will need a bucket lock.
124629ba732aSMartin KaFai Lau 	 */
124729ba732aSMartin KaFai Lau 	l_new = prealloc_lru_pop(htab, key, hash);
124829ba732aSMartin KaFai Lau 	if (!l_new)
124929ba732aSMartin KaFai Lau 		return -ENOMEM;
125068134668SAlexei Starovoitov 	copy_map_value(&htab->map,
125168134668SAlexei Starovoitov 		       l_new->key + round_up(map->key_size, 8), value);
125229ba732aSMartin KaFai Lau 
125320b6cc34SSong Liu 	ret = htab_lock_bucket(htab, b, hash, &flags);
125420b6cc34SSong Liu 	if (ret)
1255b34ffb0cSAnton Protopopov 		goto err_lock_bucket;
125629ba732aSMartin KaFai Lau 
125729ba732aSMartin KaFai Lau 	l_old = lookup_elem_raw(head, hash, key, key_size);
125829ba732aSMartin KaFai Lau 
125929ba732aSMartin KaFai Lau 	ret = check_flags(htab, l_old, map_flags);
126029ba732aSMartin KaFai Lau 	if (ret)
126129ba732aSMartin KaFai Lau 		goto err;
126229ba732aSMartin KaFai Lau 
126329ba732aSMartin KaFai Lau 	/* add new element to the head of the list, so that
126429ba732aSMartin KaFai Lau 	 * concurrent search will find it before old elem
126529ba732aSMartin KaFai Lau 	 */
12664fe84359SAlexei Starovoitov 	hlist_nulls_add_head_rcu(&l_new->hash_node, head);
126729ba732aSMartin KaFai Lau 	if (l_old) {
126829ba732aSMartin KaFai Lau 		bpf_lru_node_set_ref(&l_new->lru_node);
12694fe84359SAlexei Starovoitov 		hlist_nulls_del_rcu(&l_old->hash_node);
127029ba732aSMartin KaFai Lau 	}
127129ba732aSMartin KaFai Lau 	ret = 0;
127229ba732aSMartin KaFai Lau 
127329ba732aSMartin KaFai Lau err:
127420b6cc34SSong Liu 	htab_unlock_bucket(htab, b, hash, flags);
127529ba732aSMartin KaFai Lau 
1276b34ffb0cSAnton Protopopov err_lock_bucket:
127729ba732aSMartin KaFai Lau 	if (ret)
127868134668SAlexei Starovoitov 		htab_lru_push_free(htab, l_new);
127929ba732aSMartin KaFai Lau 	else if (l_old)
128068134668SAlexei Starovoitov 		htab_lru_push_free(htab, l_old);
128129ba732aSMartin KaFai Lau 
128229ba732aSMartin KaFai Lau 	return ret;
128329ba732aSMartin KaFai Lau }
128429ba732aSMartin KaFai Lau 
__htab_percpu_map_update_elem(struct bpf_map * map,void * key,void * value,u64 map_flags,bool onallcpus)1285d7ba4cc9SJP Kobryn static long __htab_percpu_map_update_elem(struct bpf_map *map, void *key,
128615a07b33SAlexei Starovoitov 					  void *value, u64 map_flags,
128715a07b33SAlexei Starovoitov 					  bool onallcpus)
1288824bd0ceSAlexei Starovoitov {
1289824bd0ceSAlexei Starovoitov 	struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1290824bd0ceSAlexei Starovoitov 	struct htab_elem *l_new = NULL, *l_old;
12914fe84359SAlexei Starovoitov 	struct hlist_nulls_head *head;
1292824bd0ceSAlexei Starovoitov 	unsigned long flags;
1293824bd0ceSAlexei Starovoitov 	struct bucket *b;
1294824bd0ceSAlexei Starovoitov 	u32 key_size, hash;
1295824bd0ceSAlexei Starovoitov 	int ret;
1296824bd0ceSAlexei Starovoitov 
1297824bd0ceSAlexei Starovoitov 	if (unlikely(map_flags > BPF_EXIST))
1298824bd0ceSAlexei Starovoitov 		/* unknown flags */
1299824bd0ceSAlexei Starovoitov 		return -EINVAL;
1300824bd0ceSAlexei Starovoitov 
1301694cea39SToke Høiland-Jørgensen 	WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_trace_held() &&
1302694cea39SToke Høiland-Jørgensen 		     !rcu_read_lock_bh_held());
1303824bd0ceSAlexei Starovoitov 
1304824bd0ceSAlexei Starovoitov 	key_size = map->key_size;
1305824bd0ceSAlexei Starovoitov 
1306c0203475SDaniel Borkmann 	hash = htab_map_hash(key, key_size, htab->hashrnd);
1307824bd0ceSAlexei Starovoitov 
1308824bd0ceSAlexei Starovoitov 	b = __select_bucket(htab, hash);
1309824bd0ceSAlexei Starovoitov 	head = &b->head;
1310824bd0ceSAlexei Starovoitov 
131120b6cc34SSong Liu 	ret = htab_lock_bucket(htab, b, hash, &flags);
131220b6cc34SSong Liu 	if (ret)
131320b6cc34SSong Liu 		return ret;
1314824bd0ceSAlexei Starovoitov 
1315824bd0ceSAlexei Starovoitov 	l_old = lookup_elem_raw(head, hash, key, key_size);
1316824bd0ceSAlexei Starovoitov 
1317824bd0ceSAlexei Starovoitov 	ret = check_flags(htab, l_old, map_flags);
1318824bd0ceSAlexei Starovoitov 	if (ret)
1319824bd0ceSAlexei Starovoitov 		goto err;
1320824bd0ceSAlexei Starovoitov 
1321824bd0ceSAlexei Starovoitov 	if (l_old) {
1322824bd0ceSAlexei Starovoitov 		/* per-cpu hash map can update value in-place */
1323fd91de7bSMartin KaFai Lau 		pcpu_copy_value(htab, htab_elem_get_ptr(l_old, key_size),
1324fd91de7bSMartin KaFai Lau 				value, onallcpus);
1325824bd0ceSAlexei Starovoitov 	} else {
1326824bd0ceSAlexei Starovoitov 		l_new = alloc_htab_elem(htab, key, value, key_size,
13278c290e60SAlexei Starovoitov 					hash, true, onallcpus, NULL);
13286c905981SAlexei Starovoitov 		if (IS_ERR(l_new)) {
13296c905981SAlexei Starovoitov 			ret = PTR_ERR(l_new);
1330824bd0ceSAlexei Starovoitov 			goto err;
1331824bd0ceSAlexei Starovoitov 		}
13324fe84359SAlexei Starovoitov 		hlist_nulls_add_head_rcu(&l_new->hash_node, head);
1333824bd0ceSAlexei Starovoitov 	}
1334824bd0ceSAlexei Starovoitov 	ret = 0;
1335824bd0ceSAlexei Starovoitov err:
133620b6cc34SSong Liu 	htab_unlock_bucket(htab, b, hash, flags);
1337824bd0ceSAlexei Starovoitov 	return ret;
1338824bd0ceSAlexei Starovoitov }
1339824bd0ceSAlexei Starovoitov 
__htab_lru_percpu_map_update_elem(struct bpf_map * map,void * key,void * value,u64 map_flags,bool onallcpus)1340d7ba4cc9SJP Kobryn static long __htab_lru_percpu_map_update_elem(struct bpf_map *map, void *key,
13418f844938SMartin KaFai Lau 					      void *value, u64 map_flags,
13428f844938SMartin KaFai Lau 					      bool onallcpus)
13438f844938SMartin KaFai Lau {
13448f844938SMartin KaFai Lau 	struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
13458f844938SMartin KaFai Lau 	struct htab_elem *l_new = NULL, *l_old;
13464fe84359SAlexei Starovoitov 	struct hlist_nulls_head *head;
13478f844938SMartin KaFai Lau 	unsigned long flags;
13488f844938SMartin KaFai Lau 	struct bucket *b;
13498f844938SMartin KaFai Lau 	u32 key_size, hash;
13508f844938SMartin KaFai Lau 	int ret;
13518f844938SMartin KaFai Lau 
13528f844938SMartin KaFai Lau 	if (unlikely(map_flags > BPF_EXIST))
13538f844938SMartin KaFai Lau 		/* unknown flags */
13548f844938SMartin KaFai Lau 		return -EINVAL;
13558f844938SMartin KaFai Lau 
1356694cea39SToke Høiland-Jørgensen 	WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_trace_held() &&
1357694cea39SToke Høiland-Jørgensen 		     !rcu_read_lock_bh_held());
13588f844938SMartin KaFai Lau 
13598f844938SMartin KaFai Lau 	key_size = map->key_size;
13608f844938SMartin KaFai Lau 
1361c0203475SDaniel Borkmann 	hash = htab_map_hash(key, key_size, htab->hashrnd);
13628f844938SMartin KaFai Lau 
13638f844938SMartin KaFai Lau 	b = __select_bucket(htab, hash);
13648f844938SMartin KaFai Lau 	head = &b->head;
13658f844938SMartin KaFai Lau 
13668f844938SMartin KaFai Lau 	/* For LRU, we need to alloc before taking bucket's
13678f844938SMartin KaFai Lau 	 * spinlock because LRU's elem alloc may need
13688f844938SMartin KaFai Lau 	 * to remove older elem from htab and this removal
13698f844938SMartin KaFai Lau 	 * operation will need a bucket lock.
13708f844938SMartin KaFai Lau 	 */
13718f844938SMartin KaFai Lau 	if (map_flags != BPF_EXIST) {
13728f844938SMartin KaFai Lau 		l_new = prealloc_lru_pop(htab, key, hash);
13738f844938SMartin KaFai Lau 		if (!l_new)
13748f844938SMartin KaFai Lau 			return -ENOMEM;
13758f844938SMartin KaFai Lau 	}
13768f844938SMartin KaFai Lau 
137720b6cc34SSong Liu 	ret = htab_lock_bucket(htab, b, hash, &flags);
137820b6cc34SSong Liu 	if (ret)
1379b34ffb0cSAnton Protopopov 		goto err_lock_bucket;
13808f844938SMartin KaFai Lau 
13818f844938SMartin KaFai Lau 	l_old = lookup_elem_raw(head, hash, key, key_size);
13828f844938SMartin KaFai Lau 
13838f844938SMartin KaFai Lau 	ret = check_flags(htab, l_old, map_flags);
13848f844938SMartin KaFai Lau 	if (ret)
13858f844938SMartin KaFai Lau 		goto err;
13868f844938SMartin KaFai Lau 
13878f844938SMartin KaFai Lau 	if (l_old) {
13888f844938SMartin KaFai Lau 		bpf_lru_node_set_ref(&l_old->lru_node);
13898f844938SMartin KaFai Lau 
13908f844938SMartin KaFai Lau 		/* per-cpu hash map can update value in-place */
13918f844938SMartin KaFai Lau 		pcpu_copy_value(htab, htab_elem_get_ptr(l_old, key_size),
13928f844938SMartin KaFai Lau 				value, onallcpus);
13938f844938SMartin KaFai Lau 	} else {
1394d3bec013SDavid Verbeiren 		pcpu_init_value(htab, htab_elem_get_ptr(l_new, key_size),
13958f844938SMartin KaFai Lau 				value, onallcpus);
13964fe84359SAlexei Starovoitov 		hlist_nulls_add_head_rcu(&l_new->hash_node, head);
13978f844938SMartin KaFai Lau 		l_new = NULL;
13988f844938SMartin KaFai Lau 	}
13998f844938SMartin KaFai Lau 	ret = 0;
14008f844938SMartin KaFai Lau err:
140120b6cc34SSong Liu 	htab_unlock_bucket(htab, b, hash, flags);
1402b34ffb0cSAnton Protopopov err_lock_bucket:
14039bc421b6SAnton Protopopov 	if (l_new) {
14049bc421b6SAnton Protopopov 		bpf_map_dec_elem_count(&htab->map);
14058f844938SMartin KaFai Lau 		bpf_lru_push_free(&htab->lru, &l_new->lru_node);
14069bc421b6SAnton Protopopov 	}
14078f844938SMartin KaFai Lau 	return ret;
14088f844938SMartin KaFai Lau }
14098f844938SMartin KaFai Lau 
htab_percpu_map_update_elem(struct bpf_map * map,void * key,void * value,u64 map_flags)1410d7ba4cc9SJP Kobryn static long htab_percpu_map_update_elem(struct bpf_map *map, void *key,
141115a07b33SAlexei Starovoitov 					void *value, u64 map_flags)
141215a07b33SAlexei Starovoitov {
141315a07b33SAlexei Starovoitov 	return __htab_percpu_map_update_elem(map, key, value, map_flags, false);
141415a07b33SAlexei Starovoitov }
141515a07b33SAlexei Starovoitov 
htab_lru_percpu_map_update_elem(struct bpf_map * map,void * key,void * value,u64 map_flags)1416d7ba4cc9SJP Kobryn static long htab_lru_percpu_map_update_elem(struct bpf_map *map, void *key,
14178f844938SMartin KaFai Lau 					    void *value, u64 map_flags)
14188f844938SMartin KaFai Lau {
14198f844938SMartin KaFai Lau 	return __htab_lru_percpu_map_update_elem(map, key, value, map_flags,
14208f844938SMartin KaFai Lau 						 false);
14218f844938SMartin KaFai Lau }
14228f844938SMartin KaFai Lau 
14230f8e4bd8SAlexei Starovoitov /* Called from syscall or from eBPF program */
htab_map_delete_elem(struct bpf_map * map,void * key)1424d7ba4cc9SJP Kobryn static long htab_map_delete_elem(struct bpf_map *map, void *key)
14250f8e4bd8SAlexei Starovoitov {
14260f8e4bd8SAlexei Starovoitov 	struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
14274fe84359SAlexei Starovoitov 	struct hlist_nulls_head *head;
1428688ecfe6Stom.leiming@gmail.com 	struct bucket *b;
14290f8e4bd8SAlexei Starovoitov 	struct htab_elem *l;
14300f8e4bd8SAlexei Starovoitov 	unsigned long flags;
14310f8e4bd8SAlexei Starovoitov 	u32 hash, key_size;
143220b6cc34SSong Liu 	int ret;
14330f8e4bd8SAlexei Starovoitov 
1434694cea39SToke Høiland-Jørgensen 	WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_trace_held() &&
1435694cea39SToke Høiland-Jørgensen 		     !rcu_read_lock_bh_held());
14360f8e4bd8SAlexei Starovoitov 
14370f8e4bd8SAlexei Starovoitov 	key_size = map->key_size;
14380f8e4bd8SAlexei Starovoitov 
1439c0203475SDaniel Borkmann 	hash = htab_map_hash(key, key_size, htab->hashrnd);
1440688ecfe6Stom.leiming@gmail.com 	b = __select_bucket(htab, hash);
1441688ecfe6Stom.leiming@gmail.com 	head = &b->head;
14420f8e4bd8SAlexei Starovoitov 
144320b6cc34SSong Liu 	ret = htab_lock_bucket(htab, b, hash, &flags);
144420b6cc34SSong Liu 	if (ret)
144520b6cc34SSong Liu 		return ret;
14460f8e4bd8SAlexei Starovoitov 
14470f8e4bd8SAlexei Starovoitov 	l = lookup_elem_raw(head, hash, key, key_size);
1448*10e8a2deSHou Tao 	if (l)
14494fe84359SAlexei Starovoitov 		hlist_nulls_del_rcu(&l->hash_node);
1450*10e8a2deSHou Tao 	else
145120b6cc34SSong Liu 		ret = -ENOENT;
14520f8e4bd8SAlexei Starovoitov 
145320b6cc34SSong Liu 	htab_unlock_bucket(htab, b, hash, flags);
1454*10e8a2deSHou Tao 
1455*10e8a2deSHou Tao 	if (l)
1456*10e8a2deSHou Tao 		free_htab_elem(htab, l);
14570f8e4bd8SAlexei Starovoitov 	return ret;
14580f8e4bd8SAlexei Starovoitov }
14590f8e4bd8SAlexei Starovoitov 
htab_lru_map_delete_elem(struct bpf_map * map,void * key)1460d7ba4cc9SJP Kobryn static long htab_lru_map_delete_elem(struct bpf_map *map, void *key)
146129ba732aSMartin KaFai Lau {
146229ba732aSMartin KaFai Lau 	struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
14634fe84359SAlexei Starovoitov 	struct hlist_nulls_head *head;
146429ba732aSMartin KaFai Lau 	struct bucket *b;
146529ba732aSMartin KaFai Lau 	struct htab_elem *l;
146629ba732aSMartin KaFai Lau 	unsigned long flags;
146729ba732aSMartin KaFai Lau 	u32 hash, key_size;
146820b6cc34SSong Liu 	int ret;
146929ba732aSMartin KaFai Lau 
1470694cea39SToke Høiland-Jørgensen 	WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_trace_held() &&
1471694cea39SToke Høiland-Jørgensen 		     !rcu_read_lock_bh_held());
147229ba732aSMartin KaFai Lau 
147329ba732aSMartin KaFai Lau 	key_size = map->key_size;
147429ba732aSMartin KaFai Lau 
1475c0203475SDaniel Borkmann 	hash = htab_map_hash(key, key_size, htab->hashrnd);
147629ba732aSMartin KaFai Lau 	b = __select_bucket(htab, hash);
147729ba732aSMartin KaFai Lau 	head = &b->head;
147829ba732aSMartin KaFai Lau 
147920b6cc34SSong Liu 	ret = htab_lock_bucket(htab, b, hash, &flags);
148020b6cc34SSong Liu 	if (ret)
148120b6cc34SSong Liu 		return ret;
148229ba732aSMartin KaFai Lau 
148329ba732aSMartin KaFai Lau 	l = lookup_elem_raw(head, hash, key, key_size);
148429ba732aSMartin KaFai Lau 
148520b6cc34SSong Liu 	if (l)
14864fe84359SAlexei Starovoitov 		hlist_nulls_del_rcu(&l->hash_node);
148720b6cc34SSong Liu 	else
148820b6cc34SSong Liu 		ret = -ENOENT;
148929ba732aSMartin KaFai Lau 
149020b6cc34SSong Liu 	htab_unlock_bucket(htab, b, hash, flags);
149129ba732aSMartin KaFai Lau 	if (l)
149268134668SAlexei Starovoitov 		htab_lru_push_free(htab, l);
149329ba732aSMartin KaFai Lau 	return ret;
149429ba732aSMartin KaFai Lau }
149529ba732aSMartin KaFai Lau 
delete_all_elements(struct bpf_htab * htab)14960f8e4bd8SAlexei Starovoitov static void delete_all_elements(struct bpf_htab *htab)
14970f8e4bd8SAlexei Starovoitov {
14980f8e4bd8SAlexei Starovoitov 	int i;
14990f8e4bd8SAlexei Starovoitov 
1500fba1a1c6SAlexei Starovoitov 	/* It's called from a worker thread, so disable migration here,
1501fba1a1c6SAlexei Starovoitov 	 * since bpf_mem_cache_free() relies on that.
1502fba1a1c6SAlexei Starovoitov 	 */
1503fba1a1c6SAlexei Starovoitov 	migrate_disable();
15040f8e4bd8SAlexei Starovoitov 	for (i = 0; i < htab->n_buckets; i++) {
15054fe84359SAlexei Starovoitov 		struct hlist_nulls_head *head = select_bucket(htab, i);
15064fe84359SAlexei Starovoitov 		struct hlist_nulls_node *n;
15070f8e4bd8SAlexei Starovoitov 		struct htab_elem *l;
15080f8e4bd8SAlexei Starovoitov 
15094fe84359SAlexei Starovoitov 		hlist_nulls_for_each_entry_safe(l, n, head, hash_node) {
15104fe84359SAlexei Starovoitov 			hlist_nulls_del_rcu(&l->hash_node);
15116c905981SAlexei Starovoitov 			htab_elem_free(htab, l);
15120f8e4bd8SAlexei Starovoitov 		}
15130f8e4bd8SAlexei Starovoitov 	}
1514fba1a1c6SAlexei Starovoitov 	migrate_enable();
15150f8e4bd8SAlexei Starovoitov }
1516bcc6b1b7SMartin KaFai Lau 
htab_free_malloced_timers(struct bpf_htab * htab)151768134668SAlexei Starovoitov static void htab_free_malloced_timers(struct bpf_htab *htab)
151868134668SAlexei Starovoitov {
151968134668SAlexei Starovoitov 	int i;
152068134668SAlexei Starovoitov 
152168134668SAlexei Starovoitov 	rcu_read_lock();
152268134668SAlexei Starovoitov 	for (i = 0; i < htab->n_buckets; i++) {
152368134668SAlexei Starovoitov 		struct hlist_nulls_head *head = select_bucket(htab, i);
152468134668SAlexei Starovoitov 		struct hlist_nulls_node *n;
152568134668SAlexei Starovoitov 		struct htab_elem *l;
152668134668SAlexei Starovoitov 
152714a324f6SKumar Kartikeya Dwivedi 		hlist_nulls_for_each_entry(l, n, head, hash_node) {
1528db559117SKumar Kartikeya Dwivedi 			/* We only free timer on uref dropping to zero */
1529db559117SKumar Kartikeya Dwivedi 			bpf_obj_free_timer(htab->map.record, l->key + round_up(htab->map.key_size, 8));
153014a324f6SKumar Kartikeya Dwivedi 		}
153168134668SAlexei Starovoitov 		cond_resched_rcu();
153268134668SAlexei Starovoitov 	}
153368134668SAlexei Starovoitov 	rcu_read_unlock();
153468134668SAlexei Starovoitov }
153568134668SAlexei Starovoitov 
htab_map_free_timers(struct bpf_map * map)153668134668SAlexei Starovoitov static void htab_map_free_timers(struct bpf_map *map)
153768134668SAlexei Starovoitov {
153868134668SAlexei Starovoitov 	struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
153968134668SAlexei Starovoitov 
1540db559117SKumar Kartikeya Dwivedi 	/* We only free timer on uref dropping to zero */
1541db559117SKumar Kartikeya Dwivedi 	if (!btf_record_has_field(htab->map.record, BPF_TIMER))
154268134668SAlexei Starovoitov 		return;
154368134668SAlexei Starovoitov 	if (!htab_is_prealloc(htab))
154468134668SAlexei Starovoitov 		htab_free_malloced_timers(htab);
154568134668SAlexei Starovoitov 	else
154668134668SAlexei Starovoitov 		htab_free_prealloced_timers(htab);
154768134668SAlexei Starovoitov }
154868134668SAlexei Starovoitov 
15490f8e4bd8SAlexei Starovoitov /* Called when map->refcnt goes to zero, either from workqueue or from syscall */
htab_map_free(struct bpf_map * map)15500f8e4bd8SAlexei Starovoitov static void htab_map_free(struct bpf_map *map)
15510f8e4bd8SAlexei Starovoitov {
15520f8e4bd8SAlexei Starovoitov 	struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
155320b6cc34SSong Liu 	int i;
15540f8e4bd8SAlexei Starovoitov 
1555bba1dc0bSAlexei Starovoitov 	/* bpf_free_used_maps() or close(map_fd) will trigger this map_free callback.
1556bba1dc0bSAlexei Starovoitov 	 * bpf_free_used_maps() is called after bpf prog is no longer executing.
1557bba1dc0bSAlexei Starovoitov 	 * There is no need to synchronize_rcu() here to protect map elements.
15580f8e4bd8SAlexei Starovoitov 	 */
15590f8e4bd8SAlexei Starovoitov 
15609f2c6e96SAlexei Starovoitov 	/* htab no longer uses call_rcu() directly. bpf_mem_alloc does it
15619f2c6e96SAlexei Starovoitov 	 * underneath and is reponsible for waiting for callbacks to finish
15629f2c6e96SAlexei Starovoitov 	 * during bpf_mem_alloc_destroy().
15630f8e4bd8SAlexei Starovoitov 	 */
156414a324f6SKumar Kartikeya Dwivedi 	if (!htab_is_prealloc(htab)) {
15650f8e4bd8SAlexei Starovoitov 		delete_all_elements(htab);
156614a324f6SKumar Kartikeya Dwivedi 	} else {
1567aa3496acSKumar Kartikeya Dwivedi 		htab_free_prealloced_fields(htab);
156829ba732aSMartin KaFai Lau 		prealloc_destroy(htab);
156914a324f6SKumar Kartikeya Dwivedi 	}
157029ba732aSMartin KaFai Lau 
15719bc421b6SAnton Protopopov 	bpf_map_free_elem_count(map);
1572a6ed3ea6SAlexei Starovoitov 	free_percpu(htab->extra_elems);
1573d407bd25SDaniel Borkmann 	bpf_map_area_free(htab->buckets);
1574ee4ed53cSAlexei Starovoitov 	bpf_mem_alloc_destroy(&htab->pcpu_ma);
1575fba1a1c6SAlexei Starovoitov 	bpf_mem_alloc_destroy(&htab->ma);
157686fe28f7SAlexei Starovoitov 	if (htab->use_percpu_counter)
157786fe28f7SAlexei Starovoitov 		percpu_counter_destroy(&htab->pcount);
157820b6cc34SSong Liu 	for (i = 0; i < HASHTAB_MAP_LOCK_COUNT; i++)
157920b6cc34SSong Liu 		free_percpu(htab->map_locked[i]);
15808aaeed81SEric Dumazet 	lockdep_unregister_key(&htab->lockdep_key);
158173cf09a3SYafang Shao 	bpf_map_area_free(htab);
15820f8e4bd8SAlexei Starovoitov }
15830f8e4bd8SAlexei Starovoitov 
htab_map_seq_show_elem(struct bpf_map * map,void * key,struct seq_file * m)1584699c86d6SYonghong Song static void htab_map_seq_show_elem(struct bpf_map *map, void *key,
1585699c86d6SYonghong Song 				   struct seq_file *m)
1586699c86d6SYonghong Song {
1587699c86d6SYonghong Song 	void *value;
1588699c86d6SYonghong Song 
1589699c86d6SYonghong Song 	rcu_read_lock();
1590699c86d6SYonghong Song 
1591699c86d6SYonghong Song 	value = htab_map_lookup_elem(map, key);
1592699c86d6SYonghong Song 	if (!value) {
1593699c86d6SYonghong Song 		rcu_read_unlock();
1594699c86d6SYonghong Song 		return;
1595699c86d6SYonghong Song 	}
1596699c86d6SYonghong Song 
1597699c86d6SYonghong Song 	btf_type_seq_show(map->btf, map->btf_key_type_id, key, m);
1598699c86d6SYonghong Song 	seq_puts(m, ": ");
1599699c86d6SYonghong Song 	btf_type_seq_show(map->btf, map->btf_value_type_id, value, m);
1600699c86d6SYonghong Song 	seq_puts(m, "\n");
1601699c86d6SYonghong Song 
1602699c86d6SYonghong Song 	rcu_read_unlock();
1603699c86d6SYonghong Song }
1604699c86d6SYonghong Song 
__htab_map_lookup_and_delete_elem(struct bpf_map * map,void * key,void * value,bool is_lru_map,bool is_percpu,u64 flags)16053e87f192SDenis Salopek static int __htab_map_lookup_and_delete_elem(struct bpf_map *map, void *key,
16063e87f192SDenis Salopek 					     void *value, bool is_lru_map,
16073e87f192SDenis Salopek 					     bool is_percpu, u64 flags)
16083e87f192SDenis Salopek {
16093e87f192SDenis Salopek 	struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
16103e87f192SDenis Salopek 	struct hlist_nulls_head *head;
16113e87f192SDenis Salopek 	unsigned long bflags;
16123e87f192SDenis Salopek 	struct htab_elem *l;
16133e87f192SDenis Salopek 	u32 hash, key_size;
16143e87f192SDenis Salopek 	struct bucket *b;
16153e87f192SDenis Salopek 	int ret;
16163e87f192SDenis Salopek 
16173e87f192SDenis Salopek 	key_size = map->key_size;
16183e87f192SDenis Salopek 
16193e87f192SDenis Salopek 	hash = htab_map_hash(key, key_size, htab->hashrnd);
16203e87f192SDenis Salopek 	b = __select_bucket(htab, hash);
16213e87f192SDenis Salopek 	head = &b->head;
16223e87f192SDenis Salopek 
16233e87f192SDenis Salopek 	ret = htab_lock_bucket(htab, b, hash, &bflags);
16243e87f192SDenis Salopek 	if (ret)
16253e87f192SDenis Salopek 		return ret;
16263e87f192SDenis Salopek 
16273e87f192SDenis Salopek 	l = lookup_elem_raw(head, hash, key, key_size);
16283e87f192SDenis Salopek 	if (!l) {
16293e87f192SDenis Salopek 		ret = -ENOENT;
16303e87f192SDenis Salopek 	} else {
16313e87f192SDenis Salopek 		if (is_percpu) {
16323e87f192SDenis Salopek 			u32 roundup_value_size = round_up(map->value_size, 8);
16333e87f192SDenis Salopek 			void __percpu *pptr;
16343e87f192SDenis Salopek 			int off = 0, cpu;
16353e87f192SDenis Salopek 
16363e87f192SDenis Salopek 			pptr = htab_elem_get_ptr(l, key_size);
16373e87f192SDenis Salopek 			for_each_possible_cpu(cpu) {
163865334e64SKumar Kartikeya Dwivedi 				copy_map_value_long(&htab->map, value + off, per_cpu_ptr(pptr, cpu));
163965334e64SKumar Kartikeya Dwivedi 				check_and_init_map_value(&htab->map, value + off);
16403e87f192SDenis Salopek 				off += roundup_value_size;
16413e87f192SDenis Salopek 			}
16423e87f192SDenis Salopek 		} else {
16433e87f192SDenis Salopek 			u32 roundup_key_size = round_up(map->key_size, 8);
16443e87f192SDenis Salopek 
16453e87f192SDenis Salopek 			if (flags & BPF_F_LOCK)
16463e87f192SDenis Salopek 				copy_map_value_locked(map, value, l->key +
16473e87f192SDenis Salopek 						      roundup_key_size,
16483e87f192SDenis Salopek 						      true);
16493e87f192SDenis Salopek 			else
16503e87f192SDenis Salopek 				copy_map_value(map, value, l->key +
16513e87f192SDenis Salopek 					       roundup_key_size);
1652997849c4SHou Tao 			/* Zeroing special fields in the temp buffer */
165368134668SAlexei Starovoitov 			check_and_init_map_value(map, value);
16543e87f192SDenis Salopek 		}
16553e87f192SDenis Salopek 
16563e87f192SDenis Salopek 		hlist_nulls_del_rcu(&l->hash_node);
16573e87f192SDenis Salopek 		if (!is_lru_map)
16583e87f192SDenis Salopek 			free_htab_elem(htab, l);
16593e87f192SDenis Salopek 	}
16603e87f192SDenis Salopek 
16613e87f192SDenis Salopek 	htab_unlock_bucket(htab, b, hash, bflags);
16623e87f192SDenis Salopek 
16633e87f192SDenis Salopek 	if (is_lru_map && l)
166468134668SAlexei Starovoitov 		htab_lru_push_free(htab, l);
16653e87f192SDenis Salopek 
16663e87f192SDenis Salopek 	return ret;
16673e87f192SDenis Salopek }
16683e87f192SDenis Salopek 
htab_map_lookup_and_delete_elem(struct bpf_map * map,void * key,void * value,u64 flags)16693e87f192SDenis Salopek static int htab_map_lookup_and_delete_elem(struct bpf_map *map, void *key,
16703e87f192SDenis Salopek 					   void *value, u64 flags)
16713e87f192SDenis Salopek {
16723e87f192SDenis Salopek 	return __htab_map_lookup_and_delete_elem(map, key, value, false, false,
16733e87f192SDenis Salopek 						 flags);
16743e87f192SDenis Salopek }
16753e87f192SDenis Salopek 
htab_percpu_map_lookup_and_delete_elem(struct bpf_map * map,void * key,void * value,u64 flags)16763e87f192SDenis Salopek static int htab_percpu_map_lookup_and_delete_elem(struct bpf_map *map,
16773e87f192SDenis Salopek 						  void *key, void *value,
16783e87f192SDenis Salopek 						  u64 flags)
16793e87f192SDenis Salopek {
16803e87f192SDenis Salopek 	return __htab_map_lookup_and_delete_elem(map, key, value, false, true,
16813e87f192SDenis Salopek 						 flags);
16823e87f192SDenis Salopek }
16833e87f192SDenis Salopek 
htab_lru_map_lookup_and_delete_elem(struct bpf_map * map,void * key,void * value,u64 flags)16843e87f192SDenis Salopek static int htab_lru_map_lookup_and_delete_elem(struct bpf_map *map, void *key,
16853e87f192SDenis Salopek 					       void *value, u64 flags)
16863e87f192SDenis Salopek {
16873e87f192SDenis Salopek 	return __htab_map_lookup_and_delete_elem(map, key, value, true, false,
16883e87f192SDenis Salopek 						 flags);
16893e87f192SDenis Salopek }
16903e87f192SDenis Salopek 
htab_lru_percpu_map_lookup_and_delete_elem(struct bpf_map * map,void * key,void * value,u64 flags)16913e87f192SDenis Salopek static int htab_lru_percpu_map_lookup_and_delete_elem(struct bpf_map *map,
16923e87f192SDenis Salopek 						      void *key, void *value,
16933e87f192SDenis Salopek 						      u64 flags)
16943e87f192SDenis Salopek {
16953e87f192SDenis Salopek 	return __htab_map_lookup_and_delete_elem(map, key, value, true, true,
16963e87f192SDenis Salopek 						 flags);
16973e87f192SDenis Salopek }
16983e87f192SDenis Salopek 
169905799638SYonghong Song static int
__htab_map_lookup_and_delete_batch(struct bpf_map * map,const union bpf_attr * attr,union bpf_attr __user * uattr,bool do_delete,bool is_lru_map,bool is_percpu)170005799638SYonghong Song __htab_map_lookup_and_delete_batch(struct bpf_map *map,
170105799638SYonghong Song 				   const union bpf_attr *attr,
170205799638SYonghong Song 				   union bpf_attr __user *uattr,
170305799638SYonghong Song 				   bool do_delete, bool is_lru_map,
170405799638SYonghong Song 				   bool is_percpu)
170505799638SYonghong Song {
170605799638SYonghong Song 	struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
170705799638SYonghong Song 	u32 bucket_cnt, total, key_size, value_size, roundup_key_size;
170805799638SYonghong Song 	void *keys = NULL, *values = NULL, *value, *dst_key, *dst_val;
170905799638SYonghong Song 	void __user *uvalues = u64_to_user_ptr(attr->batch.values);
171005799638SYonghong Song 	void __user *ukeys = u64_to_user_ptr(attr->batch.keys);
17112f4b0319SLukas Bulwahn 	void __user *ubatch = u64_to_user_ptr(attr->batch.in_batch);
17129263dddcSTakshak Chahande 	u32 batch, max_count, size, bucket_size, map_id;
1713b9aff38dSYonghong Song 	struct htab_elem *node_to_free = NULL;
171405799638SYonghong Song 	u64 elem_map_flags, map_flags;
171505799638SYonghong Song 	struct hlist_nulls_head *head;
171605799638SYonghong Song 	struct hlist_nulls_node *n;
1717492e0d0dSBrian Vazquez 	unsigned long flags = 0;
1718492e0d0dSBrian Vazquez 	bool locked = false;
171905799638SYonghong Song 	struct htab_elem *l;
172005799638SYonghong Song 	struct bucket *b;
172105799638SYonghong Song 	int ret = 0;
172205799638SYonghong Song 
172305799638SYonghong Song 	elem_map_flags = attr->batch.elem_flags;
172405799638SYonghong Song 	if ((elem_map_flags & ~BPF_F_LOCK) ||
1725db559117SKumar Kartikeya Dwivedi 	    ((elem_map_flags & BPF_F_LOCK) && !btf_record_has_field(map->record, BPF_SPIN_LOCK)))
172605799638SYonghong Song 		return -EINVAL;
172705799638SYonghong Song 
172805799638SYonghong Song 	map_flags = attr->batch.flags;
172905799638SYonghong Song 	if (map_flags)
173005799638SYonghong Song 		return -EINVAL;
173105799638SYonghong Song 
173205799638SYonghong Song 	max_count = attr->batch.count;
173305799638SYonghong Song 	if (!max_count)
173405799638SYonghong Song 		return 0;
173505799638SYonghong Song 
173605799638SYonghong Song 	if (put_user(0, &uattr->batch.count))
173705799638SYonghong Song 		return -EFAULT;
173805799638SYonghong Song 
173905799638SYonghong Song 	batch = 0;
174005799638SYonghong Song 	if (ubatch && copy_from_user(&batch, ubatch, sizeof(batch)))
174105799638SYonghong Song 		return -EFAULT;
174205799638SYonghong Song 
174305799638SYonghong Song 	if (batch >= htab->n_buckets)
174405799638SYonghong Song 		return -ENOENT;
174505799638SYonghong Song 
174605799638SYonghong Song 	key_size = htab->map.key_size;
174705799638SYonghong Song 	roundup_key_size = round_up(htab->map.key_size, 8);
174805799638SYonghong Song 	value_size = htab->map.value_size;
174905799638SYonghong Song 	size = round_up(value_size, 8);
175005799638SYonghong Song 	if (is_percpu)
175105799638SYonghong Song 		value_size = size * num_possible_cpus();
175205799638SYonghong Song 	total = 0;
175305799638SYonghong Song 	/* while experimenting with hash tables with sizes ranging from 10 to
175405799638SYonghong Song 	 * 1000, it was observed that a bucket can have up to 5 entries.
175505799638SYonghong Song 	 */
175605799638SYonghong Song 	bucket_size = 5;
175705799638SYonghong Song 
175805799638SYonghong Song alloc:
175905799638SYonghong Song 	/* We cannot do copy_from_user or copy_to_user inside
176005799638SYonghong Song 	 * the rcu_read_lock. Allocate enough space here.
176105799638SYonghong Song 	 */
1762c4eb1f40STatsuhiko Yasumatsu 	keys = kvmalloc_array(key_size, bucket_size, GFP_USER | __GFP_NOWARN);
1763c4eb1f40STatsuhiko Yasumatsu 	values = kvmalloc_array(value_size, bucket_size, GFP_USER | __GFP_NOWARN);
176405799638SYonghong Song 	if (!keys || !values) {
176505799638SYonghong Song 		ret = -ENOMEM;
176605799638SYonghong Song 		goto after_loop;
176705799638SYonghong Song 	}
176805799638SYonghong Song 
176905799638SYonghong Song again:
1770085fee1aSThomas Gleixner 	bpf_disable_instrumentation();
177105799638SYonghong Song 	rcu_read_lock();
177205799638SYonghong Song again_nocopy:
177305799638SYonghong Song 	dst_key = keys;
177405799638SYonghong Song 	dst_val = values;
177505799638SYonghong Song 	b = &htab->buckets[batch];
177605799638SYonghong Song 	head = &b->head;
1777492e0d0dSBrian Vazquez 	/* do not grab the lock unless need it (bucket_cnt > 0). */
177820b6cc34SSong Liu 	if (locked) {
177920b6cc34SSong Liu 		ret = htab_lock_bucket(htab, b, batch, &flags);
178066a7a92eSHou Tao 		if (ret) {
178166a7a92eSHou Tao 			rcu_read_unlock();
178266a7a92eSHou Tao 			bpf_enable_instrumentation();
178366a7a92eSHou Tao 			goto after_loop;
178466a7a92eSHou Tao 		}
178520b6cc34SSong Liu 	}
178605799638SYonghong Song 
178705799638SYonghong Song 	bucket_cnt = 0;
178805799638SYonghong Song 	hlist_nulls_for_each_entry_rcu(l, n, head, hash_node)
178905799638SYonghong Song 		bucket_cnt++;
179005799638SYonghong Song 
1791492e0d0dSBrian Vazquez 	if (bucket_cnt && !locked) {
1792492e0d0dSBrian Vazquez 		locked = true;
1793492e0d0dSBrian Vazquez 		goto again_nocopy;
1794492e0d0dSBrian Vazquez 	}
1795492e0d0dSBrian Vazquez 
179605799638SYonghong Song 	if (bucket_cnt > (max_count - total)) {
179705799638SYonghong Song 		if (total == 0)
179805799638SYonghong Song 			ret = -ENOSPC;
1799492e0d0dSBrian Vazquez 		/* Note that since bucket_cnt > 0 here, it is implicit
1800492e0d0dSBrian Vazquez 		 * that the locked was grabbed, so release it.
1801492e0d0dSBrian Vazquez 		 */
180220b6cc34SSong Liu 		htab_unlock_bucket(htab, b, batch, flags);
180305799638SYonghong Song 		rcu_read_unlock();
1804085fee1aSThomas Gleixner 		bpf_enable_instrumentation();
180505799638SYonghong Song 		goto after_loop;
180605799638SYonghong Song 	}
180705799638SYonghong Song 
180805799638SYonghong Song 	if (bucket_cnt > bucket_size) {
180905799638SYonghong Song 		bucket_size = bucket_cnt;
1810492e0d0dSBrian Vazquez 		/* Note that since bucket_cnt > 0 here, it is implicit
1811492e0d0dSBrian Vazquez 		 * that the locked was grabbed, so release it.
1812492e0d0dSBrian Vazquez 		 */
181320b6cc34SSong Liu 		htab_unlock_bucket(htab, b, batch, flags);
181405799638SYonghong Song 		rcu_read_unlock();
1815085fee1aSThomas Gleixner 		bpf_enable_instrumentation();
181605799638SYonghong Song 		kvfree(keys);
181705799638SYonghong Song 		kvfree(values);
181805799638SYonghong Song 		goto alloc;
181905799638SYonghong Song 	}
182005799638SYonghong Song 
1821492e0d0dSBrian Vazquez 	/* Next block is only safe to run if you have grabbed the lock */
1822492e0d0dSBrian Vazquez 	if (!locked)
1823492e0d0dSBrian Vazquez 		goto next_batch;
1824492e0d0dSBrian Vazquez 
182505799638SYonghong Song 	hlist_nulls_for_each_entry_safe(l, n, head, hash_node) {
182605799638SYonghong Song 		memcpy(dst_key, l->key, key_size);
182705799638SYonghong Song 
182805799638SYonghong Song 		if (is_percpu) {
182905799638SYonghong Song 			int off = 0, cpu;
183005799638SYonghong Song 			void __percpu *pptr;
183105799638SYonghong Song 
183205799638SYonghong Song 			pptr = htab_elem_get_ptr(l, map->key_size);
183305799638SYonghong Song 			for_each_possible_cpu(cpu) {
183465334e64SKumar Kartikeya Dwivedi 				copy_map_value_long(&htab->map, dst_val + off, per_cpu_ptr(pptr, cpu));
183565334e64SKumar Kartikeya Dwivedi 				check_and_init_map_value(&htab->map, dst_val + off);
183605799638SYonghong Song 				off += size;
183705799638SYonghong Song 			}
183805799638SYonghong Song 		} else {
183905799638SYonghong Song 			value = l->key + roundup_key_size;
18409263dddcSTakshak Chahande 			if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) {
18419263dddcSTakshak Chahande 				struct bpf_map **inner_map = value;
18429263dddcSTakshak Chahande 
18439263dddcSTakshak Chahande 				 /* Actual value is the id of the inner map */
18449263dddcSTakshak Chahande 				map_id = map->ops->map_fd_sys_lookup_elem(*inner_map);
18459263dddcSTakshak Chahande 				value = &map_id;
18469263dddcSTakshak Chahande 			}
18479263dddcSTakshak Chahande 
184805799638SYonghong Song 			if (elem_map_flags & BPF_F_LOCK)
184905799638SYonghong Song 				copy_map_value_locked(map, dst_val, value,
185005799638SYonghong Song 						      true);
185105799638SYonghong Song 			else
185205799638SYonghong Song 				copy_map_value(map, dst_val, value);
1853997849c4SHou Tao 			/* Zeroing special fields in the temp buffer */
185468134668SAlexei Starovoitov 			check_and_init_map_value(map, dst_val);
185505799638SYonghong Song 		}
185605799638SYonghong Song 		if (do_delete) {
185705799638SYonghong Song 			hlist_nulls_del_rcu(&l->hash_node);
1858b9aff38dSYonghong Song 
1859b9aff38dSYonghong Song 			/* bpf_lru_push_free() will acquire lru_lock, which
1860b9aff38dSYonghong Song 			 * may cause deadlock. See comments in function
1861b9aff38dSYonghong Song 			 * prealloc_lru_pop(). Let us do bpf_lru_push_free()
1862b9aff38dSYonghong Song 			 * after releasing the bucket lock.
1863*10e8a2deSHou Tao 			 *
1864*10e8a2deSHou Tao 			 * For htab of maps, htab_put_fd_value() in
1865*10e8a2deSHou Tao 			 * free_htab_elem() may acquire a spinlock with bucket
1866*10e8a2deSHou Tao 			 * lock being held and it violates the lock rule, so
1867*10e8a2deSHou Tao 			 * invoke free_htab_elem() after unlock as well.
1868b9aff38dSYonghong Song 			 */
1869b9aff38dSYonghong Song 			l->batch_flink = node_to_free;
1870b9aff38dSYonghong Song 			node_to_free = l;
1871b9aff38dSYonghong Song 		}
187205799638SYonghong Song 		dst_key += key_size;
187305799638SYonghong Song 		dst_val += value_size;
187405799638SYonghong Song 	}
187505799638SYonghong Song 
187620b6cc34SSong Liu 	htab_unlock_bucket(htab, b, batch, flags);
1877492e0d0dSBrian Vazquez 	locked = false;
1878b9aff38dSYonghong Song 
1879b9aff38dSYonghong Song 	while (node_to_free) {
1880b9aff38dSYonghong Song 		l = node_to_free;
1881b9aff38dSYonghong Song 		node_to_free = node_to_free->batch_flink;
1882*10e8a2deSHou Tao 		if (is_lru_map)
188368134668SAlexei Starovoitov 			htab_lru_push_free(htab, l);
1884*10e8a2deSHou Tao 		else
1885*10e8a2deSHou Tao 			free_htab_elem(htab, l);
1886b9aff38dSYonghong Song 	}
1887b9aff38dSYonghong Song 
1888492e0d0dSBrian Vazquez next_batch:
188905799638SYonghong Song 	/* If we are not copying data, we can go to next bucket and avoid
189005799638SYonghong Song 	 * unlocking the rcu.
189105799638SYonghong Song 	 */
189205799638SYonghong Song 	if (!bucket_cnt && (batch + 1 < htab->n_buckets)) {
189305799638SYonghong Song 		batch++;
189405799638SYonghong Song 		goto again_nocopy;
189505799638SYonghong Song 	}
189605799638SYonghong Song 
189705799638SYonghong Song 	rcu_read_unlock();
1898085fee1aSThomas Gleixner 	bpf_enable_instrumentation();
189905799638SYonghong Song 	if (bucket_cnt && (copy_to_user(ukeys + total * key_size, keys,
190005799638SYonghong Song 	    key_size * bucket_cnt) ||
190105799638SYonghong Song 	    copy_to_user(uvalues + total * value_size, values,
190205799638SYonghong Song 	    value_size * bucket_cnt))) {
190305799638SYonghong Song 		ret = -EFAULT;
190405799638SYonghong Song 		goto after_loop;
190505799638SYonghong Song 	}
190605799638SYonghong Song 
190705799638SYonghong Song 	total += bucket_cnt;
190805799638SYonghong Song 	batch++;
190905799638SYonghong Song 	if (batch >= htab->n_buckets) {
191005799638SYonghong Song 		ret = -ENOENT;
191105799638SYonghong Song 		goto after_loop;
191205799638SYonghong Song 	}
191305799638SYonghong Song 	goto again;
191405799638SYonghong Song 
191505799638SYonghong Song after_loop:
191605799638SYonghong Song 	if (ret == -EFAULT)
191705799638SYonghong Song 		goto out;
191805799638SYonghong Song 
191905799638SYonghong Song 	/* copy # of entries and next batch */
192005799638SYonghong Song 	ubatch = u64_to_user_ptr(attr->batch.out_batch);
192105799638SYonghong Song 	if (copy_to_user(ubatch, &batch, sizeof(batch)) ||
192205799638SYonghong Song 	    put_user(total, &uattr->batch.count))
192305799638SYonghong Song 		ret = -EFAULT;
192405799638SYonghong Song 
192505799638SYonghong Song out:
192605799638SYonghong Song 	kvfree(keys);
192705799638SYonghong Song 	kvfree(values);
192805799638SYonghong Song 	return ret;
192905799638SYonghong Song }
193005799638SYonghong Song 
193105799638SYonghong Song static int
htab_percpu_map_lookup_batch(struct bpf_map * map,const union bpf_attr * attr,union bpf_attr __user * uattr)193205799638SYonghong Song htab_percpu_map_lookup_batch(struct bpf_map *map, const union bpf_attr *attr,
193305799638SYonghong Song 			     union bpf_attr __user *uattr)
193405799638SYonghong Song {
193505799638SYonghong Song 	return __htab_map_lookup_and_delete_batch(map, attr, uattr, false,
193605799638SYonghong Song 						  false, true);
193705799638SYonghong Song }
193805799638SYonghong Song 
193905799638SYonghong Song static int
htab_percpu_map_lookup_and_delete_batch(struct bpf_map * map,const union bpf_attr * attr,union bpf_attr __user * uattr)194005799638SYonghong Song htab_percpu_map_lookup_and_delete_batch(struct bpf_map *map,
194105799638SYonghong Song 					const union bpf_attr *attr,
194205799638SYonghong Song 					union bpf_attr __user *uattr)
194305799638SYonghong Song {
194405799638SYonghong Song 	return __htab_map_lookup_and_delete_batch(map, attr, uattr, true,
194505799638SYonghong Song 						  false, true);
194605799638SYonghong Song }
194705799638SYonghong Song 
194805799638SYonghong Song static int
htab_map_lookup_batch(struct bpf_map * map,const union bpf_attr * attr,union bpf_attr __user * uattr)194905799638SYonghong Song htab_map_lookup_batch(struct bpf_map *map, const union bpf_attr *attr,
195005799638SYonghong Song 		      union bpf_attr __user *uattr)
195105799638SYonghong Song {
195205799638SYonghong Song 	return __htab_map_lookup_and_delete_batch(map, attr, uattr, false,
195305799638SYonghong Song 						  false, false);
195405799638SYonghong Song }
195505799638SYonghong Song 
195605799638SYonghong Song static int
htab_map_lookup_and_delete_batch(struct bpf_map * map,const union bpf_attr * attr,union bpf_attr __user * uattr)195705799638SYonghong Song htab_map_lookup_and_delete_batch(struct bpf_map *map,
195805799638SYonghong Song 				 const union bpf_attr *attr,
195905799638SYonghong Song 				 union bpf_attr __user *uattr)
196005799638SYonghong Song {
196105799638SYonghong Song 	return __htab_map_lookup_and_delete_batch(map, attr, uattr, true,
196205799638SYonghong Song 						  false, false);
196305799638SYonghong Song }
196405799638SYonghong Song 
196505799638SYonghong Song static int
htab_lru_percpu_map_lookup_batch(struct bpf_map * map,const union bpf_attr * attr,union bpf_attr __user * uattr)196605799638SYonghong Song htab_lru_percpu_map_lookup_batch(struct bpf_map *map,
196705799638SYonghong Song 				 const union bpf_attr *attr,
196805799638SYonghong Song 				 union bpf_attr __user *uattr)
196905799638SYonghong Song {
197005799638SYonghong Song 	return __htab_map_lookup_and_delete_batch(map, attr, uattr, false,
197105799638SYonghong Song 						  true, true);
197205799638SYonghong Song }
197305799638SYonghong Song 
197405799638SYonghong Song static int
htab_lru_percpu_map_lookup_and_delete_batch(struct bpf_map * map,const union bpf_attr * attr,union bpf_attr __user * uattr)197505799638SYonghong Song htab_lru_percpu_map_lookup_and_delete_batch(struct bpf_map *map,
197605799638SYonghong Song 					    const union bpf_attr *attr,
197705799638SYonghong Song 					    union bpf_attr __user *uattr)
197805799638SYonghong Song {
197905799638SYonghong Song 	return __htab_map_lookup_and_delete_batch(map, attr, uattr, true,
198005799638SYonghong Song 						  true, true);
198105799638SYonghong Song }
198205799638SYonghong Song 
198305799638SYonghong Song static int
htab_lru_map_lookup_batch(struct bpf_map * map,const union bpf_attr * attr,union bpf_attr __user * uattr)198405799638SYonghong Song htab_lru_map_lookup_batch(struct bpf_map *map, const union bpf_attr *attr,
198505799638SYonghong Song 			  union bpf_attr __user *uattr)
198605799638SYonghong Song {
198705799638SYonghong Song 	return __htab_map_lookup_and_delete_batch(map, attr, uattr, false,
198805799638SYonghong Song 						  true, false);
198905799638SYonghong Song }
199005799638SYonghong Song 
199105799638SYonghong Song static int
htab_lru_map_lookup_and_delete_batch(struct bpf_map * map,const union bpf_attr * attr,union bpf_attr __user * uattr)199205799638SYonghong Song htab_lru_map_lookup_and_delete_batch(struct bpf_map *map,
199305799638SYonghong Song 				     const union bpf_attr *attr,
199405799638SYonghong Song 				     union bpf_attr __user *uattr)
199505799638SYonghong Song {
199605799638SYonghong Song 	return __htab_map_lookup_and_delete_batch(map, attr, uattr, true,
199705799638SYonghong Song 						  true, false);
199805799638SYonghong Song }
199905799638SYonghong Song 
2000d6c4503cSYonghong Song struct bpf_iter_seq_hash_map_info {
2001d6c4503cSYonghong Song 	struct bpf_map *map;
2002d6c4503cSYonghong Song 	struct bpf_htab *htab;
2003d6c4503cSYonghong Song 	void *percpu_value_buf; // non-zero means percpu hash
2004d6c4503cSYonghong Song 	u32 bucket_id;
2005d6c4503cSYonghong Song 	u32 skip_elems;
2006d6c4503cSYonghong Song };
2007d6c4503cSYonghong Song 
2008d6c4503cSYonghong Song static struct htab_elem *
bpf_hash_map_seq_find_next(struct bpf_iter_seq_hash_map_info * info,struct htab_elem * prev_elem)2009d6c4503cSYonghong Song bpf_hash_map_seq_find_next(struct bpf_iter_seq_hash_map_info *info,
2010d6c4503cSYonghong Song 			   struct htab_elem *prev_elem)
2011d6c4503cSYonghong Song {
2012d6c4503cSYonghong Song 	const struct bpf_htab *htab = info->htab;
2013d6c4503cSYonghong Song 	u32 skip_elems = info->skip_elems;
2014d6c4503cSYonghong Song 	u32 bucket_id = info->bucket_id;
2015d6c4503cSYonghong Song 	struct hlist_nulls_head *head;
2016d6c4503cSYonghong Song 	struct hlist_nulls_node *n;
2017d6c4503cSYonghong Song 	struct htab_elem *elem;
2018d6c4503cSYonghong Song 	struct bucket *b;
2019d6c4503cSYonghong Song 	u32 i, count;
2020d6c4503cSYonghong Song 
2021d6c4503cSYonghong Song 	if (bucket_id >= htab->n_buckets)
2022d6c4503cSYonghong Song 		return NULL;
2023d6c4503cSYonghong Song 
2024d6c4503cSYonghong Song 	/* try to find next elem in the same bucket */
2025d6c4503cSYonghong Song 	if (prev_elem) {
2026d6c4503cSYonghong Song 		/* no update/deletion on this bucket, prev_elem should be still valid
2027d6c4503cSYonghong Song 		 * and we won't skip elements.
2028d6c4503cSYonghong Song 		 */
2029d6c4503cSYonghong Song 		n = rcu_dereference_raw(hlist_nulls_next_rcu(&prev_elem->hash_node));
2030d6c4503cSYonghong Song 		elem = hlist_nulls_entry_safe(n, struct htab_elem, hash_node);
2031d6c4503cSYonghong Song 		if (elem)
2032d6c4503cSYonghong Song 			return elem;
2033d6c4503cSYonghong Song 
2034d6c4503cSYonghong Song 		/* not found, unlock and go to the next bucket */
2035d6c4503cSYonghong Song 		b = &htab->buckets[bucket_id++];
2036dc0988bbSYonghong Song 		rcu_read_unlock();
2037d6c4503cSYonghong Song 		skip_elems = 0;
2038d6c4503cSYonghong Song 	}
2039d6c4503cSYonghong Song 
2040d6c4503cSYonghong Song 	for (i = bucket_id; i < htab->n_buckets; i++) {
2041d6c4503cSYonghong Song 		b = &htab->buckets[i];
2042dc0988bbSYonghong Song 		rcu_read_lock();
2043d6c4503cSYonghong Song 
2044d6c4503cSYonghong Song 		count = 0;
2045d6c4503cSYonghong Song 		head = &b->head;
2046d6c4503cSYonghong Song 		hlist_nulls_for_each_entry_rcu(elem, n, head, hash_node) {
2047d6c4503cSYonghong Song 			if (count >= skip_elems) {
2048d6c4503cSYonghong Song 				info->bucket_id = i;
2049d6c4503cSYonghong Song 				info->skip_elems = count;
2050d6c4503cSYonghong Song 				return elem;
2051d6c4503cSYonghong Song 			}
2052d6c4503cSYonghong Song 			count++;
2053d6c4503cSYonghong Song 		}
2054d6c4503cSYonghong Song 
2055dc0988bbSYonghong Song 		rcu_read_unlock();
2056d6c4503cSYonghong Song 		skip_elems = 0;
2057d6c4503cSYonghong Song 	}
2058d6c4503cSYonghong Song 
2059d6c4503cSYonghong Song 	info->bucket_id = i;
2060d6c4503cSYonghong Song 	info->skip_elems = 0;
2061d6c4503cSYonghong Song 	return NULL;
2062d6c4503cSYonghong Song }
2063d6c4503cSYonghong Song 
bpf_hash_map_seq_start(struct seq_file * seq,loff_t * pos)2064d6c4503cSYonghong Song static void *bpf_hash_map_seq_start(struct seq_file *seq, loff_t *pos)
2065d6c4503cSYonghong Song {
2066d6c4503cSYonghong Song 	struct bpf_iter_seq_hash_map_info *info = seq->private;
2067d6c4503cSYonghong Song 	struct htab_elem *elem;
2068d6c4503cSYonghong Song 
2069d6c4503cSYonghong Song 	elem = bpf_hash_map_seq_find_next(info, NULL);
2070d6c4503cSYonghong Song 	if (!elem)
2071d6c4503cSYonghong Song 		return NULL;
2072d6c4503cSYonghong Song 
2073d6c4503cSYonghong Song 	if (*pos == 0)
2074d6c4503cSYonghong Song 		++*pos;
2075d6c4503cSYonghong Song 	return elem;
2076d6c4503cSYonghong Song }
2077d6c4503cSYonghong Song 
bpf_hash_map_seq_next(struct seq_file * seq,void * v,loff_t * pos)2078d6c4503cSYonghong Song static void *bpf_hash_map_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2079d6c4503cSYonghong Song {
2080d6c4503cSYonghong Song 	struct bpf_iter_seq_hash_map_info *info = seq->private;
2081d6c4503cSYonghong Song 
2082d6c4503cSYonghong Song 	++*pos;
2083d6c4503cSYonghong Song 	++info->skip_elems;
2084d6c4503cSYonghong Song 	return bpf_hash_map_seq_find_next(info, v);
2085d6c4503cSYonghong Song }
2086d6c4503cSYonghong Song 
__bpf_hash_map_seq_show(struct seq_file * seq,struct htab_elem * elem)2087d6c4503cSYonghong Song static int __bpf_hash_map_seq_show(struct seq_file *seq, struct htab_elem *elem)
2088d6c4503cSYonghong Song {
2089d6c4503cSYonghong Song 	struct bpf_iter_seq_hash_map_info *info = seq->private;
2090d6c4503cSYonghong Song 	u32 roundup_key_size, roundup_value_size;
2091d6c4503cSYonghong Song 	struct bpf_iter__bpf_map_elem ctx = {};
2092d6c4503cSYonghong Song 	struct bpf_map *map = info->map;
2093d6c4503cSYonghong Song 	struct bpf_iter_meta meta;
2094d6c4503cSYonghong Song 	int ret = 0, off = 0, cpu;
2095d6c4503cSYonghong Song 	struct bpf_prog *prog;
2096d6c4503cSYonghong Song 	void __percpu *pptr;
2097d6c4503cSYonghong Song 
2098d6c4503cSYonghong Song 	meta.seq = seq;
2099d6c4503cSYonghong Song 	prog = bpf_iter_get_info(&meta, elem == NULL);
2100d6c4503cSYonghong Song 	if (prog) {
2101d6c4503cSYonghong Song 		ctx.meta = &meta;
2102d6c4503cSYonghong Song 		ctx.map = info->map;
2103d6c4503cSYonghong Song 		if (elem) {
2104d6c4503cSYonghong Song 			roundup_key_size = round_up(map->key_size, 8);
2105d6c4503cSYonghong Song 			ctx.key = elem->key;
2106d6c4503cSYonghong Song 			if (!info->percpu_value_buf) {
2107d6c4503cSYonghong Song 				ctx.value = elem->key + roundup_key_size;
2108d6c4503cSYonghong Song 			} else {
2109d6c4503cSYonghong Song 				roundup_value_size = round_up(map->value_size, 8);
2110d6c4503cSYonghong Song 				pptr = htab_elem_get_ptr(elem, map->key_size);
2111d6c4503cSYonghong Song 				for_each_possible_cpu(cpu) {
211265334e64SKumar Kartikeya Dwivedi 					copy_map_value_long(map, info->percpu_value_buf + off,
211365334e64SKumar Kartikeya Dwivedi 							    per_cpu_ptr(pptr, cpu));
211465334e64SKumar Kartikeya Dwivedi 					check_and_init_map_value(map, info->percpu_value_buf + off);
2115d6c4503cSYonghong Song 					off += roundup_value_size;
2116d6c4503cSYonghong Song 				}
2117d6c4503cSYonghong Song 				ctx.value = info->percpu_value_buf;
2118d6c4503cSYonghong Song 			}
2119d6c4503cSYonghong Song 		}
2120d6c4503cSYonghong Song 		ret = bpf_iter_run_prog(prog, &ctx);
2121d6c4503cSYonghong Song 	}
2122d6c4503cSYonghong Song 
2123d6c4503cSYonghong Song 	return ret;
2124d6c4503cSYonghong Song }
2125d6c4503cSYonghong Song 
bpf_hash_map_seq_show(struct seq_file * seq,void * v)2126d6c4503cSYonghong Song static int bpf_hash_map_seq_show(struct seq_file *seq, void *v)
2127d6c4503cSYonghong Song {
2128d6c4503cSYonghong Song 	return __bpf_hash_map_seq_show(seq, v);
2129d6c4503cSYonghong Song }
2130d6c4503cSYonghong Song 
bpf_hash_map_seq_stop(struct seq_file * seq,void * v)2131d6c4503cSYonghong Song static void bpf_hash_map_seq_stop(struct seq_file *seq, void *v)
2132d6c4503cSYonghong Song {
2133d6c4503cSYonghong Song 	if (!v)
2134d6c4503cSYonghong Song 		(void)__bpf_hash_map_seq_show(seq, NULL);
2135d6c4503cSYonghong Song 	else
2136dc0988bbSYonghong Song 		rcu_read_unlock();
2137d6c4503cSYonghong Song }
2138d6c4503cSYonghong Song 
bpf_iter_init_hash_map(void * priv_data,struct bpf_iter_aux_info * aux)2139d6c4503cSYonghong Song static int bpf_iter_init_hash_map(void *priv_data,
2140d6c4503cSYonghong Song 				  struct bpf_iter_aux_info *aux)
2141d6c4503cSYonghong Song {
2142d6c4503cSYonghong Song 	struct bpf_iter_seq_hash_map_info *seq_info = priv_data;
2143d6c4503cSYonghong Song 	struct bpf_map *map = aux->map;
2144d6c4503cSYonghong Song 	void *value_buf;
2145d6c4503cSYonghong Song 	u32 buf_size;
2146d6c4503cSYonghong Song 
2147d6c4503cSYonghong Song 	if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
2148d6c4503cSYonghong Song 	    map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
2149d6c4503cSYonghong Song 		buf_size = round_up(map->value_size, 8) * num_possible_cpus();
2150d6c4503cSYonghong Song 		value_buf = kmalloc(buf_size, GFP_USER | __GFP_NOWARN);
2151d6c4503cSYonghong Song 		if (!value_buf)
2152d6c4503cSYonghong Song 			return -ENOMEM;
2153d6c4503cSYonghong Song 
2154d6c4503cSYonghong Song 		seq_info->percpu_value_buf = value_buf;
2155d6c4503cSYonghong Song 	}
2156d6c4503cSYonghong Song 
2157ef1e93d2SHou Tao 	bpf_map_inc_with_uref(map);
2158d6c4503cSYonghong Song 	seq_info->map = map;
2159d6c4503cSYonghong Song 	seq_info->htab = container_of(map, struct bpf_htab, map);
2160d6c4503cSYonghong Song 	return 0;
2161d6c4503cSYonghong Song }
2162d6c4503cSYonghong Song 
bpf_iter_fini_hash_map(void * priv_data)2163d6c4503cSYonghong Song static void bpf_iter_fini_hash_map(void *priv_data)
2164d6c4503cSYonghong Song {
2165d6c4503cSYonghong Song 	struct bpf_iter_seq_hash_map_info *seq_info = priv_data;
2166d6c4503cSYonghong Song 
2167ef1e93d2SHou Tao 	bpf_map_put_with_uref(seq_info->map);
2168d6c4503cSYonghong Song 	kfree(seq_info->percpu_value_buf);
2169d6c4503cSYonghong Song }
2170d6c4503cSYonghong Song 
2171d6c4503cSYonghong Song static const struct seq_operations bpf_hash_map_seq_ops = {
2172d6c4503cSYonghong Song 	.start	= bpf_hash_map_seq_start,
2173d6c4503cSYonghong Song 	.next	= bpf_hash_map_seq_next,
2174d6c4503cSYonghong Song 	.stop	= bpf_hash_map_seq_stop,
2175d6c4503cSYonghong Song 	.show	= bpf_hash_map_seq_show,
2176d6c4503cSYonghong Song };
2177d6c4503cSYonghong Song 
2178d6c4503cSYonghong Song static const struct bpf_iter_seq_info iter_seq_info = {
2179d6c4503cSYonghong Song 	.seq_ops		= &bpf_hash_map_seq_ops,
2180d6c4503cSYonghong Song 	.init_seq_private	= bpf_iter_init_hash_map,
2181d6c4503cSYonghong Song 	.fini_seq_private	= bpf_iter_fini_hash_map,
2182d6c4503cSYonghong Song 	.seq_priv_size		= sizeof(struct bpf_iter_seq_hash_map_info),
2183d6c4503cSYonghong Song };
2184d6c4503cSYonghong Song 
bpf_for_each_hash_elem(struct bpf_map * map,bpf_callback_t callback_fn,void * callback_ctx,u64 flags)2185d7ba4cc9SJP Kobryn static long bpf_for_each_hash_elem(struct bpf_map *map, bpf_callback_t callback_fn,
2186314ee05eSYonghong Song 				   void *callback_ctx, u64 flags)
2187314ee05eSYonghong Song {
2188314ee05eSYonghong Song 	struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
2189314ee05eSYonghong Song 	struct hlist_nulls_head *head;
2190314ee05eSYonghong Song 	struct hlist_nulls_node *n;
2191314ee05eSYonghong Song 	struct htab_elem *elem;
2192314ee05eSYonghong Song 	u32 roundup_key_size;
2193314ee05eSYonghong Song 	int i, num_elems = 0;
2194314ee05eSYonghong Song 	void __percpu *pptr;
2195314ee05eSYonghong Song 	struct bucket *b;
2196314ee05eSYonghong Song 	void *key, *val;
2197314ee05eSYonghong Song 	bool is_percpu;
2198314ee05eSYonghong Song 	u64 ret = 0;
2199314ee05eSYonghong Song 
2200314ee05eSYonghong Song 	if (flags != 0)
2201314ee05eSYonghong Song 		return -EINVAL;
2202314ee05eSYonghong Song 
2203314ee05eSYonghong Song 	is_percpu = htab_is_percpu(htab);
2204314ee05eSYonghong Song 
2205314ee05eSYonghong Song 	roundup_key_size = round_up(map->key_size, 8);
2206314ee05eSYonghong Song 	/* disable migration so percpu value prepared here will be the
2207314ee05eSYonghong Song 	 * same as the one seen by the bpf program with bpf_map_lookup_elem().
2208314ee05eSYonghong Song 	 */
2209314ee05eSYonghong Song 	if (is_percpu)
2210314ee05eSYonghong Song 		migrate_disable();
2211314ee05eSYonghong Song 	for (i = 0; i < htab->n_buckets; i++) {
2212314ee05eSYonghong Song 		b = &htab->buckets[i];
2213314ee05eSYonghong Song 		rcu_read_lock();
2214314ee05eSYonghong Song 		head = &b->head;
2215314ee05eSYonghong Song 		hlist_nulls_for_each_entry_rcu(elem, n, head, hash_node) {
2216314ee05eSYonghong Song 			key = elem->key;
2217314ee05eSYonghong Song 			if (is_percpu) {
2218314ee05eSYonghong Song 				/* current cpu value for percpu map */
2219314ee05eSYonghong Song 				pptr = htab_elem_get_ptr(elem, map->key_size);
2220314ee05eSYonghong Song 				val = this_cpu_ptr(pptr);
2221314ee05eSYonghong Song 			} else {
2222314ee05eSYonghong Song 				val = elem->key + roundup_key_size;
2223314ee05eSYonghong Song 			}
2224314ee05eSYonghong Song 			num_elems++;
2225102acbacSKees Cook 			ret = callback_fn((u64)(long)map, (u64)(long)key,
2226102acbacSKees Cook 					  (u64)(long)val, (u64)(long)callback_ctx, 0);
2227314ee05eSYonghong Song 			/* return value: 0 - continue, 1 - stop and return */
2228314ee05eSYonghong Song 			if (ret) {
2229314ee05eSYonghong Song 				rcu_read_unlock();
2230314ee05eSYonghong Song 				goto out;
2231314ee05eSYonghong Song 			}
2232314ee05eSYonghong Song 		}
2233314ee05eSYonghong Song 		rcu_read_unlock();
2234314ee05eSYonghong Song 	}
2235314ee05eSYonghong Song out:
2236314ee05eSYonghong Song 	if (is_percpu)
2237314ee05eSYonghong Song 		migrate_enable();
2238314ee05eSYonghong Song 	return num_elems;
2239314ee05eSYonghong Song }
2240314ee05eSYonghong Song 
htab_map_mem_usage(const struct bpf_map * map)2241304849a2SYafang Shao static u64 htab_map_mem_usage(const struct bpf_map *map)
2242304849a2SYafang Shao {
2243304849a2SYafang Shao 	struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
2244304849a2SYafang Shao 	u32 value_size = round_up(htab->map.value_size, 8);
2245304849a2SYafang Shao 	bool prealloc = htab_is_prealloc(htab);
2246304849a2SYafang Shao 	bool percpu = htab_is_percpu(htab);
2247304849a2SYafang Shao 	bool lru = htab_is_lru(htab);
2248304849a2SYafang Shao 	u64 num_entries;
2249304849a2SYafang Shao 	u64 usage = sizeof(struct bpf_htab);
2250304849a2SYafang Shao 
2251304849a2SYafang Shao 	usage += sizeof(struct bucket) * htab->n_buckets;
2252304849a2SYafang Shao 	usage += sizeof(int) * num_possible_cpus() * HASHTAB_MAP_LOCK_COUNT;
2253304849a2SYafang Shao 	if (prealloc) {
2254304849a2SYafang Shao 		num_entries = map->max_entries;
2255304849a2SYafang Shao 		if (htab_has_extra_elems(htab))
2256304849a2SYafang Shao 			num_entries += num_possible_cpus();
2257304849a2SYafang Shao 
2258304849a2SYafang Shao 		usage += htab->elem_size * num_entries;
2259304849a2SYafang Shao 
2260304849a2SYafang Shao 		if (percpu)
2261304849a2SYafang Shao 			usage += value_size * num_possible_cpus() * num_entries;
2262304849a2SYafang Shao 		else if (!lru)
2263304849a2SYafang Shao 			usage += sizeof(struct htab_elem *) * num_possible_cpus();
2264304849a2SYafang Shao 	} else {
2265304849a2SYafang Shao #define LLIST_NODE_SZ sizeof(struct llist_node)
2266304849a2SYafang Shao 
2267304849a2SYafang Shao 		num_entries = htab->use_percpu_counter ?
2268304849a2SYafang Shao 					  percpu_counter_sum(&htab->pcount) :
2269304849a2SYafang Shao 					  atomic_read(&htab->count);
2270304849a2SYafang Shao 		usage += (htab->elem_size + LLIST_NODE_SZ) * num_entries;
2271304849a2SYafang Shao 		if (percpu) {
2272304849a2SYafang Shao 			usage += (LLIST_NODE_SZ + sizeof(void *)) * num_entries;
2273304849a2SYafang Shao 			usage += value_size * num_possible_cpus() * num_entries;
2274304849a2SYafang Shao 		}
2275304849a2SYafang Shao 	}
2276304849a2SYafang Shao 	return usage;
2277304849a2SYafang Shao }
2278304849a2SYafang Shao 
2279c317ab71SMenglong Dong BTF_ID_LIST_SINGLE(htab_map_btf_ids, struct, bpf_htab)
228040077e0cSJohannes Berg const struct bpf_map_ops htab_map_ops = {
2281f4d05259SMartin KaFai Lau 	.map_meta_equal = bpf_map_meta_equal,
22829328e0d1SJakub Kicinski 	.map_alloc_check = htab_map_alloc_check,
22830f8e4bd8SAlexei Starovoitov 	.map_alloc = htab_map_alloc,
22840f8e4bd8SAlexei Starovoitov 	.map_free = htab_map_free,
22850f8e4bd8SAlexei Starovoitov 	.map_get_next_key = htab_map_get_next_key,
228668134668SAlexei Starovoitov 	.map_release_uref = htab_map_free_timers,
22870f8e4bd8SAlexei Starovoitov 	.map_lookup_elem = htab_map_lookup_elem,
22883e87f192SDenis Salopek 	.map_lookup_and_delete_elem = htab_map_lookup_and_delete_elem,
22890f8e4bd8SAlexei Starovoitov 	.map_update_elem = htab_map_update_elem,
22900f8e4bd8SAlexei Starovoitov 	.map_delete_elem = htab_map_delete_elem,
22919015d2f5SAlexei Starovoitov 	.map_gen_lookup = htab_map_gen_lookup,
2292699c86d6SYonghong Song 	.map_seq_show_elem = htab_map_seq_show_elem,
2293314ee05eSYonghong Song 	.map_set_for_each_callback_args = map_set_for_each_callback_args,
2294314ee05eSYonghong Song 	.map_for_each_callback = bpf_for_each_hash_elem,
2295304849a2SYafang Shao 	.map_mem_usage = htab_map_mem_usage,
229605799638SYonghong Song 	BATCH_OPS(htab),
2297c317ab71SMenglong Dong 	.map_btf_id = &htab_map_btf_ids[0],
2298d6c4503cSYonghong Song 	.iter_seq_info = &iter_seq_info,
22990f8e4bd8SAlexei Starovoitov };
23000f8e4bd8SAlexei Starovoitov 
230140077e0cSJohannes Berg const struct bpf_map_ops htab_lru_map_ops = {
2302f4d05259SMartin KaFai Lau 	.map_meta_equal = bpf_map_meta_equal,
23039328e0d1SJakub Kicinski 	.map_alloc_check = htab_map_alloc_check,
230429ba732aSMartin KaFai Lau 	.map_alloc = htab_map_alloc,
230529ba732aSMartin KaFai Lau 	.map_free = htab_map_free,
230629ba732aSMartin KaFai Lau 	.map_get_next_key = htab_map_get_next_key,
230768134668SAlexei Starovoitov 	.map_release_uref = htab_map_free_timers,
230829ba732aSMartin KaFai Lau 	.map_lookup_elem = htab_lru_map_lookup_elem,
23093e87f192SDenis Salopek 	.map_lookup_and_delete_elem = htab_lru_map_lookup_and_delete_elem,
231050b045a8SDaniel Borkmann 	.map_lookup_elem_sys_only = htab_lru_map_lookup_elem_sys,
231129ba732aSMartin KaFai Lau 	.map_update_elem = htab_lru_map_update_elem,
231229ba732aSMartin KaFai Lau 	.map_delete_elem = htab_lru_map_delete_elem,
2313cc555421SMartin KaFai Lau 	.map_gen_lookup = htab_lru_map_gen_lookup,
2314699c86d6SYonghong Song 	.map_seq_show_elem = htab_map_seq_show_elem,
2315314ee05eSYonghong Song 	.map_set_for_each_callback_args = map_set_for_each_callback_args,
2316314ee05eSYonghong Song 	.map_for_each_callback = bpf_for_each_hash_elem,
2317304849a2SYafang Shao 	.map_mem_usage = htab_map_mem_usage,
231805799638SYonghong Song 	BATCH_OPS(htab_lru),
2319c317ab71SMenglong Dong 	.map_btf_id = &htab_map_btf_ids[0],
2320d6c4503cSYonghong Song 	.iter_seq_info = &iter_seq_info,
232129ba732aSMartin KaFai Lau };
232229ba732aSMartin KaFai Lau 
2323824bd0ceSAlexei Starovoitov /* Called from eBPF program */
htab_percpu_map_lookup_elem(struct bpf_map * map,void * key)2324824bd0ceSAlexei Starovoitov static void *htab_percpu_map_lookup_elem(struct bpf_map *map, void *key)
2325824bd0ceSAlexei Starovoitov {
2326824bd0ceSAlexei Starovoitov 	struct htab_elem *l = __htab_map_lookup_elem(map, key);
2327824bd0ceSAlexei Starovoitov 
2328824bd0ceSAlexei Starovoitov 	if (l)
2329824bd0ceSAlexei Starovoitov 		return this_cpu_ptr(htab_elem_get_ptr(l, map->key_size));
2330824bd0ceSAlexei Starovoitov 	else
2331824bd0ceSAlexei Starovoitov 		return NULL;
2332824bd0ceSAlexei Starovoitov }
2333824bd0ceSAlexei Starovoitov 
htab_percpu_map_lookup_percpu_elem(struct bpf_map * map,void * key,u32 cpu)233407343110SFeng Zhou static void *htab_percpu_map_lookup_percpu_elem(struct bpf_map *map, void *key, u32 cpu)
233507343110SFeng Zhou {
233607343110SFeng Zhou 	struct htab_elem *l;
233707343110SFeng Zhou 
233807343110SFeng Zhou 	if (cpu >= nr_cpu_ids)
233907343110SFeng Zhou 		return NULL;
234007343110SFeng Zhou 
234107343110SFeng Zhou 	l = __htab_map_lookup_elem(map, key);
234207343110SFeng Zhou 	if (l)
234307343110SFeng Zhou 		return per_cpu_ptr(htab_elem_get_ptr(l, map->key_size), cpu);
234407343110SFeng Zhou 	else
234507343110SFeng Zhou 		return NULL;
234607343110SFeng Zhou }
234707343110SFeng Zhou 
htab_lru_percpu_map_lookup_elem(struct bpf_map * map,void * key)23488f844938SMartin KaFai Lau static void *htab_lru_percpu_map_lookup_elem(struct bpf_map *map, void *key)
23498f844938SMartin KaFai Lau {
23508f844938SMartin KaFai Lau 	struct htab_elem *l = __htab_map_lookup_elem(map, key);
23518f844938SMartin KaFai Lau 
23528f844938SMartin KaFai Lau 	if (l) {
23538f844938SMartin KaFai Lau 		bpf_lru_node_set_ref(&l->lru_node);
23548f844938SMartin KaFai Lau 		return this_cpu_ptr(htab_elem_get_ptr(l, map->key_size));
23558f844938SMartin KaFai Lau 	}
23568f844938SMartin KaFai Lau 
23578f844938SMartin KaFai Lau 	return NULL;
23588f844938SMartin KaFai Lau }
23598f844938SMartin KaFai Lau 
htab_lru_percpu_map_lookup_percpu_elem(struct bpf_map * map,void * key,u32 cpu)236007343110SFeng Zhou static void *htab_lru_percpu_map_lookup_percpu_elem(struct bpf_map *map, void *key, u32 cpu)
236107343110SFeng Zhou {
236207343110SFeng Zhou 	struct htab_elem *l;
236307343110SFeng Zhou 
236407343110SFeng Zhou 	if (cpu >= nr_cpu_ids)
236507343110SFeng Zhou 		return NULL;
236607343110SFeng Zhou 
236707343110SFeng Zhou 	l = __htab_map_lookup_elem(map, key);
236807343110SFeng Zhou 	if (l) {
236907343110SFeng Zhou 		bpf_lru_node_set_ref(&l->lru_node);
237007343110SFeng Zhou 		return per_cpu_ptr(htab_elem_get_ptr(l, map->key_size), cpu);
237107343110SFeng Zhou 	}
237207343110SFeng Zhou 
237307343110SFeng Zhou 	return NULL;
237407343110SFeng Zhou }
237507343110SFeng Zhou 
bpf_percpu_hash_copy(struct bpf_map * map,void * key,void * value)237615a07b33SAlexei Starovoitov int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value)
237715a07b33SAlexei Starovoitov {
237815a07b33SAlexei Starovoitov 	struct htab_elem *l;
237915a07b33SAlexei Starovoitov 	void __percpu *pptr;
238015a07b33SAlexei Starovoitov 	int ret = -ENOENT;
238115a07b33SAlexei Starovoitov 	int cpu, off = 0;
238215a07b33SAlexei Starovoitov 	u32 size;
238315a07b33SAlexei Starovoitov 
238415a07b33SAlexei Starovoitov 	/* per_cpu areas are zero-filled and bpf programs can only
238515a07b33SAlexei Starovoitov 	 * access 'value_size' of them, so copying rounded areas
238615a07b33SAlexei Starovoitov 	 * will not leak any kernel data
238715a07b33SAlexei Starovoitov 	 */
238815a07b33SAlexei Starovoitov 	size = round_up(map->value_size, 8);
238915a07b33SAlexei Starovoitov 	rcu_read_lock();
239015a07b33SAlexei Starovoitov 	l = __htab_map_lookup_elem(map, key);
239115a07b33SAlexei Starovoitov 	if (!l)
239215a07b33SAlexei Starovoitov 		goto out;
239350b045a8SDaniel Borkmann 	/* We do not mark LRU map element here in order to not mess up
239450b045a8SDaniel Borkmann 	 * eviction heuristics when user space does a map walk.
239550b045a8SDaniel Borkmann 	 */
239615a07b33SAlexei Starovoitov 	pptr = htab_elem_get_ptr(l, map->key_size);
239715a07b33SAlexei Starovoitov 	for_each_possible_cpu(cpu) {
239865334e64SKumar Kartikeya Dwivedi 		copy_map_value_long(map, value + off, per_cpu_ptr(pptr, cpu));
239965334e64SKumar Kartikeya Dwivedi 		check_and_init_map_value(map, value + off);
240015a07b33SAlexei Starovoitov 		off += size;
240115a07b33SAlexei Starovoitov 	}
240215a07b33SAlexei Starovoitov 	ret = 0;
240315a07b33SAlexei Starovoitov out:
240415a07b33SAlexei Starovoitov 	rcu_read_unlock();
240515a07b33SAlexei Starovoitov 	return ret;
240615a07b33SAlexei Starovoitov }
240715a07b33SAlexei Starovoitov 
bpf_percpu_hash_update(struct bpf_map * map,void * key,void * value,u64 map_flags)240815a07b33SAlexei Starovoitov int bpf_percpu_hash_update(struct bpf_map *map, void *key, void *value,
240915a07b33SAlexei Starovoitov 			   u64 map_flags)
241015a07b33SAlexei Starovoitov {
24118f844938SMartin KaFai Lau 	struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
24126bbd9a05SSasha Levin 	int ret;
24136bbd9a05SSasha Levin 
24146bbd9a05SSasha Levin 	rcu_read_lock();
24158f844938SMartin KaFai Lau 	if (htab_is_lru(htab))
24168f844938SMartin KaFai Lau 		ret = __htab_lru_percpu_map_update_elem(map, key, value,
24178f844938SMartin KaFai Lau 							map_flags, true);
24188f844938SMartin KaFai Lau 	else
24198f844938SMartin KaFai Lau 		ret = __htab_percpu_map_update_elem(map, key, value, map_flags,
24208f844938SMartin KaFai Lau 						    true);
24216bbd9a05SSasha Levin 	rcu_read_unlock();
24226bbd9a05SSasha Levin 
24236bbd9a05SSasha Levin 	return ret;
242415a07b33SAlexei Starovoitov }
242515a07b33SAlexei Starovoitov 
htab_percpu_map_seq_show_elem(struct bpf_map * map,void * key,struct seq_file * m)2426c7b27c37SYonghong Song static void htab_percpu_map_seq_show_elem(struct bpf_map *map, void *key,
2427c7b27c37SYonghong Song 					  struct seq_file *m)
2428c7b27c37SYonghong Song {
2429c7b27c37SYonghong Song 	struct htab_elem *l;
2430c7b27c37SYonghong Song 	void __percpu *pptr;
2431c7b27c37SYonghong Song 	int cpu;
2432c7b27c37SYonghong Song 
2433c7b27c37SYonghong Song 	rcu_read_lock();
2434c7b27c37SYonghong Song 
2435c7b27c37SYonghong Song 	l = __htab_map_lookup_elem(map, key);
2436c7b27c37SYonghong Song 	if (!l) {
2437c7b27c37SYonghong Song 		rcu_read_unlock();
2438c7b27c37SYonghong Song 		return;
2439c7b27c37SYonghong Song 	}
2440c7b27c37SYonghong Song 
2441c7b27c37SYonghong Song 	btf_type_seq_show(map->btf, map->btf_key_type_id, key, m);
2442c7b27c37SYonghong Song 	seq_puts(m, ": {\n");
2443c7b27c37SYonghong Song 	pptr = htab_elem_get_ptr(l, map->key_size);
2444c7b27c37SYonghong Song 	for_each_possible_cpu(cpu) {
2445c7b27c37SYonghong Song 		seq_printf(m, "\tcpu%d: ", cpu);
2446c7b27c37SYonghong Song 		btf_type_seq_show(map->btf, map->btf_value_type_id,
2447c7b27c37SYonghong Song 				  per_cpu_ptr(pptr, cpu), m);
2448c7b27c37SYonghong Song 		seq_puts(m, "\n");
2449c7b27c37SYonghong Song 	}
2450c7b27c37SYonghong Song 	seq_puts(m, "}\n");
2451c7b27c37SYonghong Song 
2452c7b27c37SYonghong Song 	rcu_read_unlock();
2453c7b27c37SYonghong Song }
2454c7b27c37SYonghong Song 
245540077e0cSJohannes Berg const struct bpf_map_ops htab_percpu_map_ops = {
2456f4d05259SMartin KaFai Lau 	.map_meta_equal = bpf_map_meta_equal,
24579328e0d1SJakub Kicinski 	.map_alloc_check = htab_map_alloc_check,
2458824bd0ceSAlexei Starovoitov 	.map_alloc = htab_map_alloc,
2459824bd0ceSAlexei Starovoitov 	.map_free = htab_map_free,
2460824bd0ceSAlexei Starovoitov 	.map_get_next_key = htab_map_get_next_key,
2461824bd0ceSAlexei Starovoitov 	.map_lookup_elem = htab_percpu_map_lookup_elem,
24623e87f192SDenis Salopek 	.map_lookup_and_delete_elem = htab_percpu_map_lookup_and_delete_elem,
2463824bd0ceSAlexei Starovoitov 	.map_update_elem = htab_percpu_map_update_elem,
2464824bd0ceSAlexei Starovoitov 	.map_delete_elem = htab_map_delete_elem,
246507343110SFeng Zhou 	.map_lookup_percpu_elem = htab_percpu_map_lookup_percpu_elem,
2466c7b27c37SYonghong Song 	.map_seq_show_elem = htab_percpu_map_seq_show_elem,
2467314ee05eSYonghong Song 	.map_set_for_each_callback_args = map_set_for_each_callback_args,
2468314ee05eSYonghong Song 	.map_for_each_callback = bpf_for_each_hash_elem,
2469304849a2SYafang Shao 	.map_mem_usage = htab_map_mem_usage,
247005799638SYonghong Song 	BATCH_OPS(htab_percpu),
2471c317ab71SMenglong Dong 	.map_btf_id = &htab_map_btf_ids[0],
2472d6c4503cSYonghong Song 	.iter_seq_info = &iter_seq_info,
2473824bd0ceSAlexei Starovoitov };
2474824bd0ceSAlexei Starovoitov 
247540077e0cSJohannes Berg const struct bpf_map_ops htab_lru_percpu_map_ops = {
2476f4d05259SMartin KaFai Lau 	.map_meta_equal = bpf_map_meta_equal,
24779328e0d1SJakub Kicinski 	.map_alloc_check = htab_map_alloc_check,
24788f844938SMartin KaFai Lau 	.map_alloc = htab_map_alloc,
24798f844938SMartin KaFai Lau 	.map_free = htab_map_free,
24808f844938SMartin KaFai Lau 	.map_get_next_key = htab_map_get_next_key,
24818f844938SMartin KaFai Lau 	.map_lookup_elem = htab_lru_percpu_map_lookup_elem,
24823e87f192SDenis Salopek 	.map_lookup_and_delete_elem = htab_lru_percpu_map_lookup_and_delete_elem,
24838f844938SMartin KaFai Lau 	.map_update_elem = htab_lru_percpu_map_update_elem,
24848f844938SMartin KaFai Lau 	.map_delete_elem = htab_lru_map_delete_elem,
248507343110SFeng Zhou 	.map_lookup_percpu_elem = htab_lru_percpu_map_lookup_percpu_elem,
2486c7b27c37SYonghong Song 	.map_seq_show_elem = htab_percpu_map_seq_show_elem,
2487314ee05eSYonghong Song 	.map_set_for_each_callback_args = map_set_for_each_callback_args,
2488314ee05eSYonghong Song 	.map_for_each_callback = bpf_for_each_hash_elem,
2489304849a2SYafang Shao 	.map_mem_usage = htab_map_mem_usage,
249005799638SYonghong Song 	BATCH_OPS(htab_lru_percpu),
2491c317ab71SMenglong Dong 	.map_btf_id = &htab_map_btf_ids[0],
2492d6c4503cSYonghong Song 	.iter_seq_info = &iter_seq_info,
24938f844938SMartin KaFai Lau };
24948f844938SMartin KaFai Lau 
fd_htab_map_alloc_check(union bpf_attr * attr)24959328e0d1SJakub Kicinski static int fd_htab_map_alloc_check(union bpf_attr *attr)
2496bcc6b1b7SMartin KaFai Lau {
2497bcc6b1b7SMartin KaFai Lau 	if (attr->value_size != sizeof(u32))
24989328e0d1SJakub Kicinski 		return -EINVAL;
24999328e0d1SJakub Kicinski 	return htab_map_alloc_check(attr);
2500bcc6b1b7SMartin KaFai Lau }
2501bcc6b1b7SMartin KaFai Lau 
fd_htab_map_free(struct bpf_map * map)2502bcc6b1b7SMartin KaFai Lau static void fd_htab_map_free(struct bpf_map *map)
2503bcc6b1b7SMartin KaFai Lau {
2504bcc6b1b7SMartin KaFai Lau 	struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
2505bcc6b1b7SMartin KaFai Lau 	struct hlist_nulls_node *n;
2506bcc6b1b7SMartin KaFai Lau 	struct hlist_nulls_head *head;
2507bcc6b1b7SMartin KaFai Lau 	struct htab_elem *l;
2508bcc6b1b7SMartin KaFai Lau 	int i;
2509bcc6b1b7SMartin KaFai Lau 
2510bcc6b1b7SMartin KaFai Lau 	for (i = 0; i < htab->n_buckets; i++) {
2511bcc6b1b7SMartin KaFai Lau 		head = select_bucket(htab, i);
2512bcc6b1b7SMartin KaFai Lau 
2513bcc6b1b7SMartin KaFai Lau 		hlist_nulls_for_each_entry_safe(l, n, head, hash_node) {
2514bcc6b1b7SMartin KaFai Lau 			void *ptr = fd_htab_map_get_ptr(map, l);
2515bcc6b1b7SMartin KaFai Lau 
25161c40ec6bSHou Tao 			map->ops->map_fd_put_ptr(map, ptr, false);
2517bcc6b1b7SMartin KaFai Lau 		}
2518bcc6b1b7SMartin KaFai Lau 	}
2519bcc6b1b7SMartin KaFai Lau 
2520bcc6b1b7SMartin KaFai Lau 	htab_map_free(map);
2521bcc6b1b7SMartin KaFai Lau }
2522bcc6b1b7SMartin KaFai Lau 
2523bcc6b1b7SMartin KaFai Lau /* only called from syscall */
bpf_fd_htab_map_lookup_elem(struct bpf_map * map,void * key,u32 * value)252414dc6f04SMartin KaFai Lau int bpf_fd_htab_map_lookup_elem(struct bpf_map *map, void *key, u32 *value)
252514dc6f04SMartin KaFai Lau {
252614dc6f04SMartin KaFai Lau 	void **ptr;
252714dc6f04SMartin KaFai Lau 	int ret = 0;
252814dc6f04SMartin KaFai Lau 
252914dc6f04SMartin KaFai Lau 	if (!map->ops->map_fd_sys_lookup_elem)
253014dc6f04SMartin KaFai Lau 		return -ENOTSUPP;
253114dc6f04SMartin KaFai Lau 
253214dc6f04SMartin KaFai Lau 	rcu_read_lock();
253314dc6f04SMartin KaFai Lau 	ptr = htab_map_lookup_elem(map, key);
253414dc6f04SMartin KaFai Lau 	if (ptr)
253514dc6f04SMartin KaFai Lau 		*value = map->ops->map_fd_sys_lookup_elem(READ_ONCE(*ptr));
253614dc6f04SMartin KaFai Lau 	else
253714dc6f04SMartin KaFai Lau 		ret = -ENOENT;
253814dc6f04SMartin KaFai Lau 	rcu_read_unlock();
253914dc6f04SMartin KaFai Lau 
254014dc6f04SMartin KaFai Lau 	return ret;
254114dc6f04SMartin KaFai Lau }
254214dc6f04SMartin KaFai Lau 
254314dc6f04SMartin KaFai Lau /* only called from syscall */
bpf_fd_htab_map_update_elem(struct bpf_map * map,struct file * map_file,void * key,void * value,u64 map_flags)2544bcc6b1b7SMartin KaFai Lau int bpf_fd_htab_map_update_elem(struct bpf_map *map, struct file *map_file,
2545bcc6b1b7SMartin KaFai Lau 				void *key, void *value, u64 map_flags)
2546bcc6b1b7SMartin KaFai Lau {
2547bcc6b1b7SMartin KaFai Lau 	void *ptr;
2548bcc6b1b7SMartin KaFai Lau 	int ret;
2549bcc6b1b7SMartin KaFai Lau 	u32 ufd = *(u32 *)value;
2550bcc6b1b7SMartin KaFai Lau 
2551bcc6b1b7SMartin KaFai Lau 	ptr = map->ops->map_fd_get_ptr(map, map_file, ufd);
2552bcc6b1b7SMartin KaFai Lau 	if (IS_ERR(ptr))
2553bcc6b1b7SMartin KaFai Lau 		return PTR_ERR(ptr);
2554bcc6b1b7SMartin KaFai Lau 
2555bcc6b1b7SMartin KaFai Lau 	ret = htab_map_update_elem(map, key, &ptr, map_flags);
2556bcc6b1b7SMartin KaFai Lau 	if (ret)
25571c40ec6bSHou Tao 		map->ops->map_fd_put_ptr(map, ptr, false);
2558bcc6b1b7SMartin KaFai Lau 
2559bcc6b1b7SMartin KaFai Lau 	return ret;
2560bcc6b1b7SMartin KaFai Lau }
2561bcc6b1b7SMartin KaFai Lau 
htab_of_map_alloc(union bpf_attr * attr)2562bcc6b1b7SMartin KaFai Lau static struct bpf_map *htab_of_map_alloc(union bpf_attr *attr)
2563bcc6b1b7SMartin KaFai Lau {
2564bcc6b1b7SMartin KaFai Lau 	struct bpf_map *map, *inner_map_meta;
2565bcc6b1b7SMartin KaFai Lau 
2566bcc6b1b7SMartin KaFai Lau 	inner_map_meta = bpf_map_meta_alloc(attr->inner_map_fd);
2567bcc6b1b7SMartin KaFai Lau 	if (IS_ERR(inner_map_meta))
2568bcc6b1b7SMartin KaFai Lau 		return inner_map_meta;
2569bcc6b1b7SMartin KaFai Lau 
25709328e0d1SJakub Kicinski 	map = htab_map_alloc(attr);
2571bcc6b1b7SMartin KaFai Lau 	if (IS_ERR(map)) {
2572bcc6b1b7SMartin KaFai Lau 		bpf_map_meta_free(inner_map_meta);
2573bcc6b1b7SMartin KaFai Lau 		return map;
2574bcc6b1b7SMartin KaFai Lau 	}
2575bcc6b1b7SMartin KaFai Lau 
2576bcc6b1b7SMartin KaFai Lau 	map->inner_map_meta = inner_map_meta;
2577bcc6b1b7SMartin KaFai Lau 
2578bcc6b1b7SMartin KaFai Lau 	return map;
2579bcc6b1b7SMartin KaFai Lau }
2580bcc6b1b7SMartin KaFai Lau 
htab_of_map_lookup_elem(struct bpf_map * map,void * key)2581bcc6b1b7SMartin KaFai Lau static void *htab_of_map_lookup_elem(struct bpf_map *map, void *key)
2582bcc6b1b7SMartin KaFai Lau {
2583bcc6b1b7SMartin KaFai Lau 	struct bpf_map **inner_map  = htab_map_lookup_elem(map, key);
2584bcc6b1b7SMartin KaFai Lau 
2585bcc6b1b7SMartin KaFai Lau 	if (!inner_map)
2586bcc6b1b7SMartin KaFai Lau 		return NULL;
2587bcc6b1b7SMartin KaFai Lau 
2588bcc6b1b7SMartin KaFai Lau 	return READ_ONCE(*inner_map);
2589bcc6b1b7SMartin KaFai Lau }
2590bcc6b1b7SMartin KaFai Lau 
htab_of_map_gen_lookup(struct bpf_map * map,struct bpf_insn * insn_buf)25914a8f87e6SDaniel Borkmann static int htab_of_map_gen_lookup(struct bpf_map *map,
25927b0c2a05SDaniel Borkmann 				  struct bpf_insn *insn_buf)
25937b0c2a05SDaniel Borkmann {
25947b0c2a05SDaniel Borkmann 	struct bpf_insn *insn = insn_buf;
25957b0c2a05SDaniel Borkmann 	const int ret = BPF_REG_0;
25967b0c2a05SDaniel Borkmann 
259709772d92SDaniel Borkmann 	BUILD_BUG_ON(!__same_type(&__htab_map_lookup_elem,
259809772d92SDaniel Borkmann 		     (void *(*)(struct bpf_map *map, void *key))NULL));
25993d717fadSKees Cook 	*insn++ = BPF_EMIT_CALL(__htab_map_lookup_elem);
26007b0c2a05SDaniel Borkmann 	*insn++ = BPF_JMP_IMM(BPF_JEQ, ret, 0, 2);
26017b0c2a05SDaniel Borkmann 	*insn++ = BPF_ALU64_IMM(BPF_ADD, ret,
26027b0c2a05SDaniel Borkmann 				offsetof(struct htab_elem, key) +
26037b0c2a05SDaniel Borkmann 				round_up(map->key_size, 8));
26047b0c2a05SDaniel Borkmann 	*insn++ = BPF_LDX_MEM(BPF_DW, ret, ret, 0);
26057b0c2a05SDaniel Borkmann 
26067b0c2a05SDaniel Borkmann 	return insn - insn_buf;
26077b0c2a05SDaniel Borkmann }
26087b0c2a05SDaniel Borkmann 
htab_of_map_free(struct bpf_map * map)2609bcc6b1b7SMartin KaFai Lau static void htab_of_map_free(struct bpf_map *map)
2610bcc6b1b7SMartin KaFai Lau {
2611bcc6b1b7SMartin KaFai Lau 	bpf_map_meta_free(map->inner_map_meta);
2612bcc6b1b7SMartin KaFai Lau 	fd_htab_map_free(map);
2613bcc6b1b7SMartin KaFai Lau }
2614bcc6b1b7SMartin KaFai Lau 
261540077e0cSJohannes Berg const struct bpf_map_ops htab_of_maps_map_ops = {
26169328e0d1SJakub Kicinski 	.map_alloc_check = fd_htab_map_alloc_check,
2617bcc6b1b7SMartin KaFai Lau 	.map_alloc = htab_of_map_alloc,
2618bcc6b1b7SMartin KaFai Lau 	.map_free = htab_of_map_free,
2619bcc6b1b7SMartin KaFai Lau 	.map_get_next_key = htab_map_get_next_key,
2620bcc6b1b7SMartin KaFai Lau 	.map_lookup_elem = htab_of_map_lookup_elem,
2621bcc6b1b7SMartin KaFai Lau 	.map_delete_elem = htab_map_delete_elem,
2622bcc6b1b7SMartin KaFai Lau 	.map_fd_get_ptr = bpf_map_fd_get_ptr,
2623bcc6b1b7SMartin KaFai Lau 	.map_fd_put_ptr = bpf_map_fd_put_ptr,
262414dc6f04SMartin KaFai Lau 	.map_fd_sys_lookup_elem = bpf_map_fd_sys_lookup_elem,
26257b0c2a05SDaniel Borkmann 	.map_gen_lookup = htab_of_map_gen_lookup,
2626e8d2bec0SDaniel Borkmann 	.map_check_btf = map_check_no_btf,
2627304849a2SYafang Shao 	.map_mem_usage = htab_map_mem_usage,
26289263dddcSTakshak Chahande 	BATCH_OPS(htab),
2629c317ab71SMenglong Dong 	.map_btf_id = &htab_map_btf_ids[0],
2630bcc6b1b7SMartin KaFai Lau };
2631