xref: /openbmc/linux/kernel/bpf/memalloc.c (revision 801b27e8)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2022 Meta Platforms, Inc. and affiliates. */
3 #include <linux/mm.h>
4 #include <linux/llist.h>
5 #include <linux/bpf.h>
6 #include <linux/irq_work.h>
7 #include <linux/bpf_mem_alloc.h>
8 #include <linux/memcontrol.h>
9 #include <asm/local.h>
10 
11 /* Any context (including NMI) BPF specific memory allocator.
12  *
13  * Tracing BPF programs can attach to kprobe and fentry. Hence they
14  * run in unknown context where calling plain kmalloc() might not be safe.
15  *
16  * Front-end kmalloc() with per-cpu per-bucket cache of free elements.
17  * Refill this cache asynchronously from irq_work.
18  *
19  * CPU_0 buckets
20  * 16 32 64 96 128 196 256 512 1024 2048 4096
21  * ...
22  * CPU_N buckets
23  * 16 32 64 96 128 196 256 512 1024 2048 4096
24  *
25  * The buckets are prefilled at the start.
26  * BPF programs always run with migration disabled.
27  * It's safe to allocate from cache of the current cpu with irqs disabled.
28  * Free-ing is always done into bucket of the current cpu as well.
29  * irq_work trims extra free elements from buckets with kfree
30  * and refills them with kmalloc, so global kmalloc logic takes care
31  * of freeing objects allocated by one cpu and freed on another.
32  *
33  * Every allocated objected is padded with extra 8 bytes that contains
34  * struct llist_node.
35  */
36 #define LLIST_NODE_SZ sizeof(struct llist_node)
37 
38 /* similar to kmalloc, but sizeof == 8 bucket is gone */
39 static u8 size_index[24] __ro_after_init = {
40 	3,	/* 8 */
41 	3,	/* 16 */
42 	4,	/* 24 */
43 	4,	/* 32 */
44 	5,	/* 40 */
45 	5,	/* 48 */
46 	5,	/* 56 */
47 	5,	/* 64 */
48 	1,	/* 72 */
49 	1,	/* 80 */
50 	1,	/* 88 */
51 	1,	/* 96 */
52 	6,	/* 104 */
53 	6,	/* 112 */
54 	6,	/* 120 */
55 	6,	/* 128 */
56 	2,	/* 136 */
57 	2,	/* 144 */
58 	2,	/* 152 */
59 	2,	/* 160 */
60 	2,	/* 168 */
61 	2,	/* 176 */
62 	2,	/* 184 */
63 	2	/* 192 */
64 };
65 
66 static int bpf_mem_cache_idx(size_t size)
67 {
68 	if (!size || size > 4096)
69 		return -1;
70 
71 	if (size <= 192)
72 		return size_index[(size - 1) / 8] - 1;
73 
74 	return fls(size - 1) - 2;
75 }
76 
77 #define NUM_CACHES 11
78 
79 struct bpf_mem_cache {
80 	/* per-cpu list of free objects of size 'unit_size'.
81 	 * All accesses are done with interrupts disabled and 'active' counter
82 	 * protection with __llist_add() and __llist_del_first().
83 	 */
84 	struct llist_head free_llist;
85 	local_t active;
86 
87 	/* Operations on the free_list from unit_alloc/unit_free/bpf_mem_refill
88 	 * are sequenced by per-cpu 'active' counter. But unit_free() cannot
89 	 * fail. When 'active' is busy the unit_free() will add an object to
90 	 * free_llist_extra.
91 	 */
92 	struct llist_head free_llist_extra;
93 
94 	struct irq_work refill_work;
95 	struct obj_cgroup *objcg;
96 	int unit_size;
97 	/* count of objects in free_llist */
98 	int free_cnt;
99 	int low_watermark, high_watermark, batch;
100 	int percpu_size;
101 	bool draining;
102 	struct bpf_mem_cache *tgt;
103 
104 	/* list of objects to be freed after RCU GP */
105 	struct llist_head free_by_rcu;
106 	struct llist_node *free_by_rcu_tail;
107 	struct llist_head waiting_for_gp;
108 	struct llist_node *waiting_for_gp_tail;
109 	struct rcu_head rcu;
110 	atomic_t call_rcu_in_progress;
111 	struct llist_head free_llist_extra_rcu;
112 
113 	/* list of objects to be freed after RCU tasks trace GP */
114 	struct llist_head free_by_rcu_ttrace;
115 	struct llist_head waiting_for_gp_ttrace;
116 	struct rcu_head rcu_ttrace;
117 	atomic_t call_rcu_ttrace_in_progress;
118 };
119 
120 struct bpf_mem_caches {
121 	struct bpf_mem_cache cache[NUM_CACHES];
122 };
123 
124 static struct llist_node notrace *__llist_del_first(struct llist_head *head)
125 {
126 	struct llist_node *entry, *next;
127 
128 	entry = head->first;
129 	if (!entry)
130 		return NULL;
131 	next = entry->next;
132 	head->first = next;
133 	return entry;
134 }
135 
136 static void *__alloc(struct bpf_mem_cache *c, int node, gfp_t flags)
137 {
138 	if (c->percpu_size) {
139 		void **obj = kmalloc_node(c->percpu_size, flags, node);
140 		void *pptr = __alloc_percpu_gfp(c->unit_size, 8, flags);
141 
142 		if (!obj || !pptr) {
143 			free_percpu(pptr);
144 			kfree(obj);
145 			return NULL;
146 		}
147 		obj[1] = pptr;
148 		return obj;
149 	}
150 
151 	return kmalloc_node(c->unit_size, flags | __GFP_ZERO, node);
152 }
153 
154 static struct mem_cgroup *get_memcg(const struct bpf_mem_cache *c)
155 {
156 #ifdef CONFIG_MEMCG_KMEM
157 	if (c->objcg)
158 		return get_mem_cgroup_from_objcg(c->objcg);
159 #endif
160 
161 #ifdef CONFIG_MEMCG
162 	return root_mem_cgroup;
163 #else
164 	return NULL;
165 #endif
166 }
167 
168 static void inc_active(struct bpf_mem_cache *c, unsigned long *flags)
169 {
170 	if (IS_ENABLED(CONFIG_PREEMPT_RT))
171 		/* In RT irq_work runs in per-cpu kthread, so disable
172 		 * interrupts to avoid preemption and interrupts and
173 		 * reduce the chance of bpf prog executing on this cpu
174 		 * when active counter is busy.
175 		 */
176 		local_irq_save(*flags);
177 	/* alloc_bulk runs from irq_work which will not preempt a bpf
178 	 * program that does unit_alloc/unit_free since IRQs are
179 	 * disabled there. There is no race to increment 'active'
180 	 * counter. It protects free_llist from corruption in case NMI
181 	 * bpf prog preempted this loop.
182 	 */
183 	WARN_ON_ONCE(local_inc_return(&c->active) != 1);
184 }
185 
186 static void dec_active(struct bpf_mem_cache *c, unsigned long flags)
187 {
188 	local_dec(&c->active);
189 	if (IS_ENABLED(CONFIG_PREEMPT_RT))
190 		local_irq_restore(flags);
191 }
192 
193 static void add_obj_to_free_list(struct bpf_mem_cache *c, void *obj)
194 {
195 	unsigned long flags;
196 
197 	inc_active(c, &flags);
198 	__llist_add(obj, &c->free_llist);
199 	c->free_cnt++;
200 	dec_active(c, flags);
201 }
202 
203 /* Mostly runs from irq_work except __init phase. */
204 static void alloc_bulk(struct bpf_mem_cache *c, int cnt, int node)
205 {
206 	struct mem_cgroup *memcg = NULL, *old_memcg;
207 	void *obj;
208 	int i;
209 
210 	for (i = 0; i < cnt; i++) {
211 		/*
212 		 * For every 'c' llist_del_first(&c->free_by_rcu_ttrace); is
213 		 * done only by one CPU == current CPU. Other CPUs might
214 		 * llist_add() and llist_del_all() in parallel.
215 		 */
216 		obj = llist_del_first(&c->free_by_rcu_ttrace);
217 		if (!obj)
218 			break;
219 		add_obj_to_free_list(c, obj);
220 	}
221 	if (i >= cnt)
222 		return;
223 
224 	for (; i < cnt; i++) {
225 		obj = llist_del_first(&c->waiting_for_gp_ttrace);
226 		if (!obj)
227 			break;
228 		add_obj_to_free_list(c, obj);
229 	}
230 	if (i >= cnt)
231 		return;
232 
233 	memcg = get_memcg(c);
234 	old_memcg = set_active_memcg(memcg);
235 	for (; i < cnt; i++) {
236 		/* Allocate, but don't deplete atomic reserves that typical
237 		 * GFP_ATOMIC would do. irq_work runs on this cpu and kmalloc
238 		 * will allocate from the current numa node which is what we
239 		 * want here.
240 		 */
241 		obj = __alloc(c, node, GFP_NOWAIT | __GFP_NOWARN | __GFP_ACCOUNT);
242 		if (!obj)
243 			break;
244 		add_obj_to_free_list(c, obj);
245 	}
246 	set_active_memcg(old_memcg);
247 	mem_cgroup_put(memcg);
248 }
249 
250 static void free_one(void *obj, bool percpu)
251 {
252 	if (percpu) {
253 		free_percpu(((void **)obj)[1]);
254 		kfree(obj);
255 		return;
256 	}
257 
258 	kfree(obj);
259 }
260 
261 static int free_all(struct llist_node *llnode, bool percpu)
262 {
263 	struct llist_node *pos, *t;
264 	int cnt = 0;
265 
266 	llist_for_each_safe(pos, t, llnode) {
267 		free_one(pos, percpu);
268 		cnt++;
269 	}
270 	return cnt;
271 }
272 
273 static void __free_rcu(struct rcu_head *head)
274 {
275 	struct bpf_mem_cache *c = container_of(head, struct bpf_mem_cache, rcu_ttrace);
276 
277 	free_all(llist_del_all(&c->waiting_for_gp_ttrace), !!c->percpu_size);
278 	atomic_set(&c->call_rcu_ttrace_in_progress, 0);
279 }
280 
281 static void __free_rcu_tasks_trace(struct rcu_head *head)
282 {
283 	/* If RCU Tasks Trace grace period implies RCU grace period,
284 	 * there is no need to invoke call_rcu().
285 	 */
286 	if (rcu_trace_implies_rcu_gp())
287 		__free_rcu(head);
288 	else
289 		call_rcu(head, __free_rcu);
290 }
291 
292 static void enque_to_free(struct bpf_mem_cache *c, void *obj)
293 {
294 	struct llist_node *llnode = obj;
295 
296 	/* bpf_mem_cache is a per-cpu object. Freeing happens in irq_work.
297 	 * Nothing races to add to free_by_rcu_ttrace list.
298 	 */
299 	llist_add(llnode, &c->free_by_rcu_ttrace);
300 }
301 
302 static void do_call_rcu_ttrace(struct bpf_mem_cache *c)
303 {
304 	struct llist_node *llnode, *t;
305 
306 	if (atomic_xchg(&c->call_rcu_ttrace_in_progress, 1)) {
307 		if (unlikely(READ_ONCE(c->draining))) {
308 			llnode = llist_del_all(&c->free_by_rcu_ttrace);
309 			free_all(llnode, !!c->percpu_size);
310 		}
311 		return;
312 	}
313 
314 	WARN_ON_ONCE(!llist_empty(&c->waiting_for_gp_ttrace));
315 	llist_for_each_safe(llnode, t, llist_del_all(&c->free_by_rcu_ttrace))
316 		llist_add(llnode, &c->waiting_for_gp_ttrace);
317 
318 	if (unlikely(READ_ONCE(c->draining))) {
319 		__free_rcu(&c->rcu_ttrace);
320 		return;
321 	}
322 
323 	/* Use call_rcu_tasks_trace() to wait for sleepable progs to finish.
324 	 * If RCU Tasks Trace grace period implies RCU grace period, free
325 	 * these elements directly, else use call_rcu() to wait for normal
326 	 * progs to finish and finally do free_one() on each element.
327 	 */
328 	call_rcu_tasks_trace(&c->rcu_ttrace, __free_rcu_tasks_trace);
329 }
330 
331 static void free_bulk(struct bpf_mem_cache *c)
332 {
333 	struct bpf_mem_cache *tgt = c->tgt;
334 	struct llist_node *llnode, *t;
335 	unsigned long flags;
336 	int cnt;
337 
338 	WARN_ON_ONCE(tgt->unit_size != c->unit_size);
339 
340 	do {
341 		inc_active(c, &flags);
342 		llnode = __llist_del_first(&c->free_llist);
343 		if (llnode)
344 			cnt = --c->free_cnt;
345 		else
346 			cnt = 0;
347 		dec_active(c, flags);
348 		if (llnode)
349 			enque_to_free(tgt, llnode);
350 	} while (cnt > (c->high_watermark + c->low_watermark) / 2);
351 
352 	/* and drain free_llist_extra */
353 	llist_for_each_safe(llnode, t, llist_del_all(&c->free_llist_extra))
354 		enque_to_free(tgt, llnode);
355 	do_call_rcu_ttrace(tgt);
356 }
357 
358 static void __free_by_rcu(struct rcu_head *head)
359 {
360 	struct bpf_mem_cache *c = container_of(head, struct bpf_mem_cache, rcu);
361 	struct bpf_mem_cache *tgt = c->tgt;
362 	struct llist_node *llnode;
363 
364 	llnode = llist_del_all(&c->waiting_for_gp);
365 	if (!llnode)
366 		goto out;
367 
368 	llist_add_batch(llnode, c->waiting_for_gp_tail, &tgt->free_by_rcu_ttrace);
369 
370 	/* Objects went through regular RCU GP. Send them to RCU tasks trace */
371 	do_call_rcu_ttrace(tgt);
372 out:
373 	atomic_set(&c->call_rcu_in_progress, 0);
374 }
375 
376 static void check_free_by_rcu(struct bpf_mem_cache *c)
377 {
378 	struct llist_node *llnode, *t;
379 	unsigned long flags;
380 
381 	/* drain free_llist_extra_rcu */
382 	if (unlikely(!llist_empty(&c->free_llist_extra_rcu))) {
383 		inc_active(c, &flags);
384 		llist_for_each_safe(llnode, t, llist_del_all(&c->free_llist_extra_rcu))
385 			if (__llist_add(llnode, &c->free_by_rcu))
386 				c->free_by_rcu_tail = llnode;
387 		dec_active(c, flags);
388 	}
389 
390 	if (llist_empty(&c->free_by_rcu))
391 		return;
392 
393 	if (atomic_xchg(&c->call_rcu_in_progress, 1)) {
394 		/*
395 		 * Instead of kmalloc-ing new rcu_head and triggering 10k
396 		 * call_rcu() to hit rcutree.qhimark and force RCU to notice
397 		 * the overload just ask RCU to hurry up. There could be many
398 		 * objects in free_by_rcu list.
399 		 * This hint reduces memory consumption for an artificial
400 		 * benchmark from 2 Gbyte to 150 Mbyte.
401 		 */
402 		rcu_request_urgent_qs_task(current);
403 		return;
404 	}
405 
406 	WARN_ON_ONCE(!llist_empty(&c->waiting_for_gp));
407 
408 	inc_active(c, &flags);
409 	WRITE_ONCE(c->waiting_for_gp.first, __llist_del_all(&c->free_by_rcu));
410 	c->waiting_for_gp_tail = c->free_by_rcu_tail;
411 	dec_active(c, flags);
412 
413 	if (unlikely(READ_ONCE(c->draining))) {
414 		free_all(llist_del_all(&c->waiting_for_gp), !!c->percpu_size);
415 		atomic_set(&c->call_rcu_in_progress, 0);
416 	} else {
417 		call_rcu_hurry(&c->rcu, __free_by_rcu);
418 	}
419 }
420 
421 static void bpf_mem_refill(struct irq_work *work)
422 {
423 	struct bpf_mem_cache *c = container_of(work, struct bpf_mem_cache, refill_work);
424 	int cnt;
425 
426 	/* Racy access to free_cnt. It doesn't need to be 100% accurate */
427 	cnt = c->free_cnt;
428 	if (cnt < c->low_watermark)
429 		/* irq_work runs on this cpu and kmalloc will allocate
430 		 * from the current numa node which is what we want here.
431 		 */
432 		alloc_bulk(c, c->batch, NUMA_NO_NODE);
433 	else if (cnt > c->high_watermark)
434 		free_bulk(c);
435 
436 	check_free_by_rcu(c);
437 }
438 
439 static void notrace irq_work_raise(struct bpf_mem_cache *c)
440 {
441 	irq_work_queue(&c->refill_work);
442 }
443 
444 /* For typical bpf map case that uses bpf_mem_cache_alloc and single bucket
445  * the freelist cache will be elem_size * 64 (or less) on each cpu.
446  *
447  * For bpf programs that don't have statically known allocation sizes and
448  * assuming (low_mark + high_mark) / 2 as an average number of elements per
449  * bucket and all buckets are used the total amount of memory in freelists
450  * on each cpu will be:
451  * 64*16 + 64*32 + 64*64 + 64*96 + 64*128 + 64*196 + 64*256 + 32*512 + 16*1024 + 8*2048 + 4*4096
452  * == ~ 116 Kbyte using below heuristic.
453  * Initialized, but unused bpf allocator (not bpf map specific one) will
454  * consume ~ 11 Kbyte per cpu.
455  * Typical case will be between 11K and 116K closer to 11K.
456  * bpf progs can and should share bpf_mem_cache when possible.
457  */
458 
459 static void prefill_mem_cache(struct bpf_mem_cache *c, int cpu)
460 {
461 	init_irq_work(&c->refill_work, bpf_mem_refill);
462 	if (c->unit_size <= 256) {
463 		c->low_watermark = 32;
464 		c->high_watermark = 96;
465 	} else {
466 		/* When page_size == 4k, order-0 cache will have low_mark == 2
467 		 * and high_mark == 6 with batch alloc of 3 individual pages at
468 		 * a time.
469 		 * 8k allocs and above low == 1, high == 3, batch == 1.
470 		 */
471 		c->low_watermark = max(32 * 256 / c->unit_size, 1);
472 		c->high_watermark = max(96 * 256 / c->unit_size, 3);
473 	}
474 	c->batch = max((c->high_watermark - c->low_watermark) / 4 * 3, 1);
475 
476 	/* To avoid consuming memory assume that 1st run of bpf
477 	 * prog won't be doing more than 4 map_update_elem from
478 	 * irq disabled region
479 	 */
480 	alloc_bulk(c, c->unit_size <= 256 ? 4 : 1, cpu_to_node(cpu));
481 }
482 
483 /* When size != 0 bpf_mem_cache for each cpu.
484  * This is typical bpf hash map use case when all elements have equal size.
485  *
486  * When size == 0 allocate 11 bpf_mem_cache-s for each cpu, then rely on
487  * kmalloc/kfree. Max allocation size is 4096 in this case.
488  * This is bpf_dynptr and bpf_kptr use case.
489  */
490 int bpf_mem_alloc_init(struct bpf_mem_alloc *ma, int size, bool percpu)
491 {
492 	static u16 sizes[NUM_CACHES] = {96, 192, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096};
493 	struct bpf_mem_caches *cc, __percpu *pcc;
494 	struct bpf_mem_cache *c, __percpu *pc;
495 	struct obj_cgroup *objcg = NULL;
496 	int cpu, i, unit_size, percpu_size = 0;
497 
498 	if (size) {
499 		pc = __alloc_percpu_gfp(sizeof(*pc), 8, GFP_KERNEL);
500 		if (!pc)
501 			return -ENOMEM;
502 
503 		if (percpu)
504 			/* room for llist_node and per-cpu pointer */
505 			percpu_size = LLIST_NODE_SZ + sizeof(void *);
506 		else
507 			size += LLIST_NODE_SZ; /* room for llist_node */
508 		unit_size = size;
509 
510 #ifdef CONFIG_MEMCG_KMEM
511 		if (memcg_bpf_enabled())
512 			objcg = get_obj_cgroup_from_current();
513 #endif
514 		for_each_possible_cpu(cpu) {
515 			c = per_cpu_ptr(pc, cpu);
516 			c->unit_size = unit_size;
517 			c->objcg = objcg;
518 			c->percpu_size = percpu_size;
519 			c->tgt = c;
520 			prefill_mem_cache(c, cpu);
521 		}
522 		ma->cache = pc;
523 		return 0;
524 	}
525 
526 	/* size == 0 && percpu is an invalid combination */
527 	if (WARN_ON_ONCE(percpu))
528 		return -EINVAL;
529 
530 	pcc = __alloc_percpu_gfp(sizeof(*cc), 8, GFP_KERNEL);
531 	if (!pcc)
532 		return -ENOMEM;
533 #ifdef CONFIG_MEMCG_KMEM
534 	objcg = get_obj_cgroup_from_current();
535 #endif
536 	for_each_possible_cpu(cpu) {
537 		cc = per_cpu_ptr(pcc, cpu);
538 		for (i = 0; i < NUM_CACHES; i++) {
539 			c = &cc->cache[i];
540 			c->unit_size = sizes[i];
541 			c->objcg = objcg;
542 			c->tgt = c;
543 			prefill_mem_cache(c, cpu);
544 		}
545 	}
546 	ma->caches = pcc;
547 	return 0;
548 }
549 
550 static void drain_mem_cache(struct bpf_mem_cache *c)
551 {
552 	bool percpu = !!c->percpu_size;
553 
554 	/* No progs are using this bpf_mem_cache, but htab_map_free() called
555 	 * bpf_mem_cache_free() for all remaining elements and they can be in
556 	 * free_by_rcu_ttrace or in waiting_for_gp_ttrace lists, so drain those lists now.
557 	 *
558 	 * Except for waiting_for_gp_ttrace list, there are no concurrent operations
559 	 * on these lists, so it is safe to use __llist_del_all().
560 	 */
561 	free_all(llist_del_all(&c->free_by_rcu_ttrace), percpu);
562 	free_all(llist_del_all(&c->waiting_for_gp_ttrace), percpu);
563 	free_all(__llist_del_all(&c->free_llist), percpu);
564 	free_all(__llist_del_all(&c->free_llist_extra), percpu);
565 	free_all(__llist_del_all(&c->free_by_rcu), percpu);
566 	free_all(__llist_del_all(&c->free_llist_extra_rcu), percpu);
567 	free_all(llist_del_all(&c->waiting_for_gp), percpu);
568 }
569 
570 static void check_mem_cache(struct bpf_mem_cache *c)
571 {
572 	WARN_ON_ONCE(!llist_empty(&c->free_by_rcu_ttrace));
573 	WARN_ON_ONCE(!llist_empty(&c->waiting_for_gp_ttrace));
574 	WARN_ON_ONCE(!llist_empty(&c->free_llist));
575 	WARN_ON_ONCE(!llist_empty(&c->free_llist_extra));
576 	WARN_ON_ONCE(!llist_empty(&c->free_by_rcu));
577 	WARN_ON_ONCE(!llist_empty(&c->free_llist_extra_rcu));
578 	WARN_ON_ONCE(!llist_empty(&c->waiting_for_gp));
579 }
580 
581 static void check_leaked_objs(struct bpf_mem_alloc *ma)
582 {
583 	struct bpf_mem_caches *cc;
584 	struct bpf_mem_cache *c;
585 	int cpu, i;
586 
587 	if (ma->cache) {
588 		for_each_possible_cpu(cpu) {
589 			c = per_cpu_ptr(ma->cache, cpu);
590 			check_mem_cache(c);
591 		}
592 	}
593 	if (ma->caches) {
594 		for_each_possible_cpu(cpu) {
595 			cc = per_cpu_ptr(ma->caches, cpu);
596 			for (i = 0; i < NUM_CACHES; i++) {
597 				c = &cc->cache[i];
598 				check_mem_cache(c);
599 			}
600 		}
601 	}
602 }
603 
604 static void free_mem_alloc_no_barrier(struct bpf_mem_alloc *ma)
605 {
606 	check_leaked_objs(ma);
607 	free_percpu(ma->cache);
608 	free_percpu(ma->caches);
609 	ma->cache = NULL;
610 	ma->caches = NULL;
611 }
612 
613 static void free_mem_alloc(struct bpf_mem_alloc *ma)
614 {
615 	/* waiting_for_gp[_ttrace] lists were drained, but RCU callbacks
616 	 * might still execute. Wait for them.
617 	 *
618 	 * rcu_barrier_tasks_trace() doesn't imply synchronize_rcu_tasks_trace(),
619 	 * but rcu_barrier_tasks_trace() and rcu_barrier() below are only used
620 	 * to wait for the pending __free_rcu_tasks_trace() and __free_rcu(),
621 	 * so if call_rcu(head, __free_rcu) is skipped due to
622 	 * rcu_trace_implies_rcu_gp(), it will be OK to skip rcu_barrier() by
623 	 * using rcu_trace_implies_rcu_gp() as well.
624 	 */
625 	rcu_barrier(); /* wait for __free_by_rcu */
626 	rcu_barrier_tasks_trace(); /* wait for __free_rcu */
627 	if (!rcu_trace_implies_rcu_gp())
628 		rcu_barrier();
629 	free_mem_alloc_no_barrier(ma);
630 }
631 
632 static void free_mem_alloc_deferred(struct work_struct *work)
633 {
634 	struct bpf_mem_alloc *ma = container_of(work, struct bpf_mem_alloc, work);
635 
636 	free_mem_alloc(ma);
637 	kfree(ma);
638 }
639 
640 static void destroy_mem_alloc(struct bpf_mem_alloc *ma, int rcu_in_progress)
641 {
642 	struct bpf_mem_alloc *copy;
643 
644 	if (!rcu_in_progress) {
645 		/* Fast path. No callbacks are pending, hence no need to do
646 		 * rcu_barrier-s.
647 		 */
648 		free_mem_alloc_no_barrier(ma);
649 		return;
650 	}
651 
652 	copy = kmemdup(ma, sizeof(*ma), GFP_KERNEL);
653 	if (!copy) {
654 		/* Slow path with inline barrier-s */
655 		free_mem_alloc(ma);
656 		return;
657 	}
658 
659 	/* Defer barriers into worker to let the rest of map memory to be freed */
660 	memset(ma, 0, sizeof(*ma));
661 	INIT_WORK(&copy->work, free_mem_alloc_deferred);
662 	queue_work(system_unbound_wq, &copy->work);
663 }
664 
665 void bpf_mem_alloc_destroy(struct bpf_mem_alloc *ma)
666 {
667 	struct bpf_mem_caches *cc;
668 	struct bpf_mem_cache *c;
669 	int cpu, i, rcu_in_progress;
670 
671 	if (ma->cache) {
672 		rcu_in_progress = 0;
673 		for_each_possible_cpu(cpu) {
674 			c = per_cpu_ptr(ma->cache, cpu);
675 			WRITE_ONCE(c->draining, true);
676 			irq_work_sync(&c->refill_work);
677 			drain_mem_cache(c);
678 			rcu_in_progress += atomic_read(&c->call_rcu_ttrace_in_progress);
679 			rcu_in_progress += atomic_read(&c->call_rcu_in_progress);
680 		}
681 		/* objcg is the same across cpus */
682 		if (c->objcg)
683 			obj_cgroup_put(c->objcg);
684 		destroy_mem_alloc(ma, rcu_in_progress);
685 	}
686 	if (ma->caches) {
687 		rcu_in_progress = 0;
688 		for_each_possible_cpu(cpu) {
689 			cc = per_cpu_ptr(ma->caches, cpu);
690 			for (i = 0; i < NUM_CACHES; i++) {
691 				c = &cc->cache[i];
692 				WRITE_ONCE(c->draining, true);
693 				irq_work_sync(&c->refill_work);
694 				drain_mem_cache(c);
695 				rcu_in_progress += atomic_read(&c->call_rcu_ttrace_in_progress);
696 				rcu_in_progress += atomic_read(&c->call_rcu_in_progress);
697 			}
698 		}
699 		if (c->objcg)
700 			obj_cgroup_put(c->objcg);
701 		destroy_mem_alloc(ma, rcu_in_progress);
702 	}
703 }
704 
705 /* notrace is necessary here and in other functions to make sure
706  * bpf programs cannot attach to them and cause llist corruptions.
707  */
708 static void notrace *unit_alloc(struct bpf_mem_cache *c)
709 {
710 	struct llist_node *llnode = NULL;
711 	unsigned long flags;
712 	int cnt = 0;
713 
714 	/* Disable irqs to prevent the following race for majority of prog types:
715 	 * prog_A
716 	 *   bpf_mem_alloc
717 	 *      preemption or irq -> prog_B
718 	 *        bpf_mem_alloc
719 	 *
720 	 * but prog_B could be a perf_event NMI prog.
721 	 * Use per-cpu 'active' counter to order free_list access between
722 	 * unit_alloc/unit_free/bpf_mem_refill.
723 	 */
724 	local_irq_save(flags);
725 	if (local_inc_return(&c->active) == 1) {
726 		llnode = __llist_del_first(&c->free_llist);
727 		if (llnode) {
728 			cnt = --c->free_cnt;
729 			*(struct bpf_mem_cache **)llnode = c;
730 		}
731 	}
732 	local_dec(&c->active);
733 	local_irq_restore(flags);
734 
735 	WARN_ON(cnt < 0);
736 
737 	if (cnt < c->low_watermark)
738 		irq_work_raise(c);
739 	return llnode;
740 }
741 
742 /* Though 'ptr' object could have been allocated on a different cpu
743  * add it to the free_llist of the current cpu.
744  * Let kfree() logic deal with it when it's later called from irq_work.
745  */
746 static void notrace unit_free(struct bpf_mem_cache *c, void *ptr)
747 {
748 	struct llist_node *llnode = ptr - LLIST_NODE_SZ;
749 	unsigned long flags;
750 	int cnt = 0;
751 
752 	BUILD_BUG_ON(LLIST_NODE_SZ > 8);
753 
754 	/*
755 	 * Remember bpf_mem_cache that allocated this object.
756 	 * The hint is not accurate.
757 	 */
758 	c->tgt = *(struct bpf_mem_cache **)llnode;
759 
760 	local_irq_save(flags);
761 	if (local_inc_return(&c->active) == 1) {
762 		__llist_add(llnode, &c->free_llist);
763 		cnt = ++c->free_cnt;
764 	} else {
765 		/* unit_free() cannot fail. Therefore add an object to atomic
766 		 * llist. free_bulk() will drain it. Though free_llist_extra is
767 		 * a per-cpu list we have to use atomic llist_add here, since
768 		 * it also can be interrupted by bpf nmi prog that does another
769 		 * unit_free() into the same free_llist_extra.
770 		 */
771 		llist_add(llnode, &c->free_llist_extra);
772 	}
773 	local_dec(&c->active);
774 	local_irq_restore(flags);
775 
776 	if (cnt > c->high_watermark)
777 		/* free few objects from current cpu into global kmalloc pool */
778 		irq_work_raise(c);
779 }
780 
781 static void notrace unit_free_rcu(struct bpf_mem_cache *c, void *ptr)
782 {
783 	struct llist_node *llnode = ptr - LLIST_NODE_SZ;
784 	unsigned long flags;
785 
786 	c->tgt = *(struct bpf_mem_cache **)llnode;
787 
788 	local_irq_save(flags);
789 	if (local_inc_return(&c->active) == 1) {
790 		if (__llist_add(llnode, &c->free_by_rcu))
791 			c->free_by_rcu_tail = llnode;
792 	} else {
793 		llist_add(llnode, &c->free_llist_extra_rcu);
794 	}
795 	local_dec(&c->active);
796 	local_irq_restore(flags);
797 
798 	if (!atomic_read(&c->call_rcu_in_progress))
799 		irq_work_raise(c);
800 }
801 
802 /* Called from BPF program or from sys_bpf syscall.
803  * In both cases migration is disabled.
804  */
805 void notrace *bpf_mem_alloc(struct bpf_mem_alloc *ma, size_t size)
806 {
807 	int idx;
808 	void *ret;
809 
810 	if (!size)
811 		return ZERO_SIZE_PTR;
812 
813 	idx = bpf_mem_cache_idx(size + LLIST_NODE_SZ);
814 	if (idx < 0)
815 		return NULL;
816 
817 	ret = unit_alloc(this_cpu_ptr(ma->caches)->cache + idx);
818 	return !ret ? NULL : ret + LLIST_NODE_SZ;
819 }
820 
821 void notrace bpf_mem_free(struct bpf_mem_alloc *ma, void *ptr)
822 {
823 	int idx;
824 
825 	if (!ptr)
826 		return;
827 
828 	idx = bpf_mem_cache_idx(ksize(ptr - LLIST_NODE_SZ));
829 	if (idx < 0)
830 		return;
831 
832 	unit_free(this_cpu_ptr(ma->caches)->cache + idx, ptr);
833 }
834 
835 void notrace bpf_mem_free_rcu(struct bpf_mem_alloc *ma, void *ptr)
836 {
837 	int idx;
838 
839 	if (!ptr)
840 		return;
841 
842 	idx = bpf_mem_cache_idx(ksize(ptr - LLIST_NODE_SZ));
843 	if (idx < 0)
844 		return;
845 
846 	unit_free_rcu(this_cpu_ptr(ma->caches)->cache + idx, ptr);
847 }
848 
849 void notrace *bpf_mem_cache_alloc(struct bpf_mem_alloc *ma)
850 {
851 	void *ret;
852 
853 	ret = unit_alloc(this_cpu_ptr(ma->cache));
854 	return !ret ? NULL : ret + LLIST_NODE_SZ;
855 }
856 
857 void notrace bpf_mem_cache_free(struct bpf_mem_alloc *ma, void *ptr)
858 {
859 	if (!ptr)
860 		return;
861 
862 	unit_free(this_cpu_ptr(ma->cache), ptr);
863 }
864 
865 void notrace bpf_mem_cache_free_rcu(struct bpf_mem_alloc *ma, void *ptr)
866 {
867 	if (!ptr)
868 		return;
869 
870 	unit_free_rcu(this_cpu_ptr(ma->cache), ptr);
871 }
872 
873 /* Directly does a kfree() without putting 'ptr' back to the free_llist
874  * for reuse and without waiting for a rcu_tasks_trace gp.
875  * The caller must first go through the rcu_tasks_trace gp for 'ptr'
876  * before calling bpf_mem_cache_raw_free().
877  * It could be used when the rcu_tasks_trace callback does not have
878  * a hold on the original bpf_mem_alloc object that allocated the
879  * 'ptr'. This should only be used in the uncommon code path.
880  * Otherwise, the bpf_mem_alloc's free_llist cannot be refilled
881  * and may affect performance.
882  */
883 void bpf_mem_cache_raw_free(void *ptr)
884 {
885 	if (!ptr)
886 		return;
887 
888 	kfree(ptr - LLIST_NODE_SZ);
889 }
890 
891 /* When flags == GFP_KERNEL, it signals that the caller will not cause
892  * deadlock when using kmalloc. bpf_mem_cache_alloc_flags() will use
893  * kmalloc if the free_llist is empty.
894  */
895 void notrace *bpf_mem_cache_alloc_flags(struct bpf_mem_alloc *ma, gfp_t flags)
896 {
897 	struct bpf_mem_cache *c;
898 	void *ret;
899 
900 	c = this_cpu_ptr(ma->cache);
901 
902 	ret = unit_alloc(c);
903 	if (!ret && flags == GFP_KERNEL) {
904 		struct mem_cgroup *memcg, *old_memcg;
905 
906 		memcg = get_memcg(c);
907 		old_memcg = set_active_memcg(memcg);
908 		ret = __alloc(c, NUMA_NO_NODE, GFP_KERNEL | __GFP_NOWARN | __GFP_ACCOUNT);
909 		set_active_memcg(old_memcg);
910 		mem_cgroup_put(memcg);
911 	}
912 
913 	return !ret ? NULL : ret + LLIST_NODE_SZ;
914 }
915