xref: /linux/kernel/bpf/memalloc.c (revision b81a677400755cf24c971d1342c4c53d81744d82)
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) - 1;
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 
102 	struct rcu_head rcu;
103 	struct llist_head free_by_rcu;
104 	struct llist_head waiting_for_gp;
105 	atomic_t call_rcu_in_progress;
106 };
107 
108 struct bpf_mem_caches {
109 	struct bpf_mem_cache cache[NUM_CACHES];
110 };
111 
112 static struct llist_node notrace *__llist_del_first(struct llist_head *head)
113 {
114 	struct llist_node *entry, *next;
115 
116 	entry = head->first;
117 	if (!entry)
118 		return NULL;
119 	next = entry->next;
120 	head->first = next;
121 	return entry;
122 }
123 
124 static void *__alloc(struct bpf_mem_cache *c, int node)
125 {
126 	/* Allocate, but don't deplete atomic reserves that typical
127 	 * GFP_ATOMIC would do. irq_work runs on this cpu and kmalloc
128 	 * will allocate from the current numa node which is what we
129 	 * want here.
130 	 */
131 	gfp_t flags = GFP_NOWAIT | __GFP_NOWARN | __GFP_ACCOUNT;
132 
133 	if (c->percpu_size) {
134 		void **obj = kmalloc_node(c->percpu_size, flags, node);
135 		void *pptr = __alloc_percpu_gfp(c->unit_size, 8, flags);
136 
137 		if (!obj || !pptr) {
138 			free_percpu(pptr);
139 			kfree(obj);
140 			return NULL;
141 		}
142 		obj[1] = pptr;
143 		return obj;
144 	}
145 
146 	return kmalloc_node(c->unit_size, flags, node);
147 }
148 
149 static struct mem_cgroup *get_memcg(const struct bpf_mem_cache *c)
150 {
151 #ifdef CONFIG_MEMCG_KMEM
152 	if (c->objcg)
153 		return get_mem_cgroup_from_objcg(c->objcg);
154 #endif
155 
156 #ifdef CONFIG_MEMCG
157 	return root_mem_cgroup;
158 #else
159 	return NULL;
160 #endif
161 }
162 
163 /* Mostly runs from irq_work except __init phase. */
164 static void alloc_bulk(struct bpf_mem_cache *c, int cnt, int node)
165 {
166 	struct mem_cgroup *memcg = NULL, *old_memcg;
167 	unsigned long flags;
168 	void *obj;
169 	int i;
170 
171 	memcg = get_memcg(c);
172 	old_memcg = set_active_memcg(memcg);
173 	for (i = 0; i < cnt; i++) {
174 		obj = __alloc(c, node);
175 		if (!obj)
176 			break;
177 		if (IS_ENABLED(CONFIG_PREEMPT_RT))
178 			/* In RT irq_work runs in per-cpu kthread, so disable
179 			 * interrupts to avoid preemption and interrupts and
180 			 * reduce the chance of bpf prog executing on this cpu
181 			 * when active counter is busy.
182 			 */
183 			local_irq_save(flags);
184 		/* alloc_bulk runs from irq_work which will not preempt a bpf
185 		 * program that does unit_alloc/unit_free since IRQs are
186 		 * disabled there. There is no race to increment 'active'
187 		 * counter. It protects free_llist from corruption in case NMI
188 		 * bpf prog preempted this loop.
189 		 */
190 		WARN_ON_ONCE(local_inc_return(&c->active) != 1);
191 		__llist_add(obj, &c->free_llist);
192 		c->free_cnt++;
193 		local_dec(&c->active);
194 		if (IS_ENABLED(CONFIG_PREEMPT_RT))
195 			local_irq_restore(flags);
196 	}
197 	set_active_memcg(old_memcg);
198 	mem_cgroup_put(memcg);
199 }
200 
201 static void free_one(struct bpf_mem_cache *c, void *obj)
202 {
203 	if (c->percpu_size) {
204 		free_percpu(((void **)obj)[1]);
205 		kfree(obj);
206 		return;
207 	}
208 
209 	kfree(obj);
210 }
211 
212 static void __free_rcu(struct rcu_head *head)
213 {
214 	struct bpf_mem_cache *c = container_of(head, struct bpf_mem_cache, rcu);
215 	struct llist_node *llnode = llist_del_all(&c->waiting_for_gp);
216 	struct llist_node *pos, *t;
217 
218 	llist_for_each_safe(pos, t, llnode)
219 		free_one(c, pos);
220 	atomic_set(&c->call_rcu_in_progress, 0);
221 }
222 
223 static void __free_rcu_tasks_trace(struct rcu_head *head)
224 {
225 	/* If RCU Tasks Trace grace period implies RCU grace period,
226 	 * there is no need to invoke call_rcu().
227 	 */
228 	if (rcu_trace_implies_rcu_gp())
229 		__free_rcu(head);
230 	else
231 		call_rcu(head, __free_rcu);
232 }
233 
234 static void enque_to_free(struct bpf_mem_cache *c, void *obj)
235 {
236 	struct llist_node *llnode = obj;
237 
238 	/* bpf_mem_cache is a per-cpu object. Freeing happens in irq_work.
239 	 * Nothing races to add to free_by_rcu list.
240 	 */
241 	__llist_add(llnode, &c->free_by_rcu);
242 }
243 
244 static void do_call_rcu(struct bpf_mem_cache *c)
245 {
246 	struct llist_node *llnode, *t;
247 
248 	if (atomic_xchg(&c->call_rcu_in_progress, 1))
249 		return;
250 
251 	WARN_ON_ONCE(!llist_empty(&c->waiting_for_gp));
252 	llist_for_each_safe(llnode, t, __llist_del_all(&c->free_by_rcu))
253 		/* There is no concurrent __llist_add(waiting_for_gp) access.
254 		 * It doesn't race with llist_del_all either.
255 		 * But there could be two concurrent llist_del_all(waiting_for_gp):
256 		 * from __free_rcu() and from drain_mem_cache().
257 		 */
258 		__llist_add(llnode, &c->waiting_for_gp);
259 	/* Use call_rcu_tasks_trace() to wait for sleepable progs to finish.
260 	 * If RCU Tasks Trace grace period implies RCU grace period, free
261 	 * these elements directly, else use call_rcu() to wait for normal
262 	 * progs to finish and finally do free_one() on each element.
263 	 */
264 	call_rcu_tasks_trace(&c->rcu, __free_rcu_tasks_trace);
265 }
266 
267 static void free_bulk(struct bpf_mem_cache *c)
268 {
269 	struct llist_node *llnode, *t;
270 	unsigned long flags;
271 	int cnt;
272 
273 	do {
274 		if (IS_ENABLED(CONFIG_PREEMPT_RT))
275 			local_irq_save(flags);
276 		WARN_ON_ONCE(local_inc_return(&c->active) != 1);
277 		llnode = __llist_del_first(&c->free_llist);
278 		if (llnode)
279 			cnt = --c->free_cnt;
280 		else
281 			cnt = 0;
282 		local_dec(&c->active);
283 		if (IS_ENABLED(CONFIG_PREEMPT_RT))
284 			local_irq_restore(flags);
285 		if (llnode)
286 			enque_to_free(c, llnode);
287 	} while (cnt > (c->high_watermark + c->low_watermark) / 2);
288 
289 	/* and drain free_llist_extra */
290 	llist_for_each_safe(llnode, t, llist_del_all(&c->free_llist_extra))
291 		enque_to_free(c, llnode);
292 	do_call_rcu(c);
293 }
294 
295 static void bpf_mem_refill(struct irq_work *work)
296 {
297 	struct bpf_mem_cache *c = container_of(work, struct bpf_mem_cache, refill_work);
298 	int cnt;
299 
300 	/* Racy access to free_cnt. It doesn't need to be 100% accurate */
301 	cnt = c->free_cnt;
302 	if (cnt < c->low_watermark)
303 		/* irq_work runs on this cpu and kmalloc will allocate
304 		 * from the current numa node which is what we want here.
305 		 */
306 		alloc_bulk(c, c->batch, NUMA_NO_NODE);
307 	else if (cnt > c->high_watermark)
308 		free_bulk(c);
309 }
310 
311 static void notrace irq_work_raise(struct bpf_mem_cache *c)
312 {
313 	irq_work_queue(&c->refill_work);
314 }
315 
316 /* For typical bpf map case that uses bpf_mem_cache_alloc and single bucket
317  * the freelist cache will be elem_size * 64 (or less) on each cpu.
318  *
319  * For bpf programs that don't have statically known allocation sizes and
320  * assuming (low_mark + high_mark) / 2 as an average number of elements per
321  * bucket and all buckets are used the total amount of memory in freelists
322  * on each cpu will be:
323  * 64*16 + 64*32 + 64*64 + 64*96 + 64*128 + 64*196 + 64*256 + 32*512 + 16*1024 + 8*2048 + 4*4096
324  * == ~ 116 Kbyte using below heuristic.
325  * Initialized, but unused bpf allocator (not bpf map specific one) will
326  * consume ~ 11 Kbyte per cpu.
327  * Typical case will be between 11K and 116K closer to 11K.
328  * bpf progs can and should share bpf_mem_cache when possible.
329  */
330 
331 static void prefill_mem_cache(struct bpf_mem_cache *c, int cpu)
332 {
333 	init_irq_work(&c->refill_work, bpf_mem_refill);
334 	if (c->unit_size <= 256) {
335 		c->low_watermark = 32;
336 		c->high_watermark = 96;
337 	} else {
338 		/* When page_size == 4k, order-0 cache will have low_mark == 2
339 		 * and high_mark == 6 with batch alloc of 3 individual pages at
340 		 * a time.
341 		 * 8k allocs and above low == 1, high == 3, batch == 1.
342 		 */
343 		c->low_watermark = max(32 * 256 / c->unit_size, 1);
344 		c->high_watermark = max(96 * 256 / c->unit_size, 3);
345 	}
346 	c->batch = max((c->high_watermark - c->low_watermark) / 4 * 3, 1);
347 
348 	/* To avoid consuming memory assume that 1st run of bpf
349 	 * prog won't be doing more than 4 map_update_elem from
350 	 * irq disabled region
351 	 */
352 	alloc_bulk(c, c->unit_size <= 256 ? 4 : 1, cpu_to_node(cpu));
353 }
354 
355 /* When size != 0 bpf_mem_cache for each cpu.
356  * This is typical bpf hash map use case when all elements have equal size.
357  *
358  * When size == 0 allocate 11 bpf_mem_cache-s for each cpu, then rely on
359  * kmalloc/kfree. Max allocation size is 4096 in this case.
360  * This is bpf_dynptr and bpf_kptr use case.
361  */
362 int bpf_mem_alloc_init(struct bpf_mem_alloc *ma, int size, bool percpu)
363 {
364 	static u16 sizes[NUM_CACHES] = {96, 192, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096};
365 	struct bpf_mem_caches *cc, __percpu *pcc;
366 	struct bpf_mem_cache *c, __percpu *pc;
367 	struct obj_cgroup *objcg = NULL;
368 	int cpu, i, unit_size, percpu_size = 0;
369 
370 	if (size) {
371 		pc = __alloc_percpu_gfp(sizeof(*pc), 8, GFP_KERNEL);
372 		if (!pc)
373 			return -ENOMEM;
374 
375 		if (percpu)
376 			/* room for llist_node and per-cpu pointer */
377 			percpu_size = LLIST_NODE_SZ + sizeof(void *);
378 		else
379 			size += LLIST_NODE_SZ; /* room for llist_node */
380 		unit_size = size;
381 
382 #ifdef CONFIG_MEMCG_KMEM
383 		objcg = get_obj_cgroup_from_current();
384 #endif
385 		for_each_possible_cpu(cpu) {
386 			c = per_cpu_ptr(pc, cpu);
387 			c->unit_size = unit_size;
388 			c->objcg = objcg;
389 			c->percpu_size = percpu_size;
390 			prefill_mem_cache(c, cpu);
391 		}
392 		ma->cache = pc;
393 		return 0;
394 	}
395 
396 	/* size == 0 && percpu is an invalid combination */
397 	if (WARN_ON_ONCE(percpu))
398 		return -EINVAL;
399 
400 	pcc = __alloc_percpu_gfp(sizeof(*cc), 8, GFP_KERNEL);
401 	if (!pcc)
402 		return -ENOMEM;
403 #ifdef CONFIG_MEMCG_KMEM
404 	objcg = get_obj_cgroup_from_current();
405 #endif
406 	for_each_possible_cpu(cpu) {
407 		cc = per_cpu_ptr(pcc, cpu);
408 		for (i = 0; i < NUM_CACHES; i++) {
409 			c = &cc->cache[i];
410 			c->unit_size = sizes[i];
411 			c->objcg = objcg;
412 			prefill_mem_cache(c, cpu);
413 		}
414 	}
415 	ma->caches = pcc;
416 	return 0;
417 }
418 
419 static void drain_mem_cache(struct bpf_mem_cache *c)
420 {
421 	struct llist_node *llnode, *t;
422 
423 	/* No progs are using this bpf_mem_cache, but htab_map_free() called
424 	 * bpf_mem_cache_free() for all remaining elements and they can be in
425 	 * free_by_rcu or in waiting_for_gp lists, so drain those lists now.
426 	 */
427 	llist_for_each_safe(llnode, t, __llist_del_all(&c->free_by_rcu))
428 		free_one(c, llnode);
429 	llist_for_each_safe(llnode, t, llist_del_all(&c->waiting_for_gp))
430 		free_one(c, llnode);
431 	llist_for_each_safe(llnode, t, llist_del_all(&c->free_llist))
432 		free_one(c, llnode);
433 	llist_for_each_safe(llnode, t, llist_del_all(&c->free_llist_extra))
434 		free_one(c, llnode);
435 }
436 
437 static void free_mem_alloc_no_barrier(struct bpf_mem_alloc *ma)
438 {
439 	free_percpu(ma->cache);
440 	free_percpu(ma->caches);
441 	ma->cache = NULL;
442 	ma->caches = NULL;
443 }
444 
445 static void free_mem_alloc(struct bpf_mem_alloc *ma)
446 {
447 	/* waiting_for_gp lists was drained, but __free_rcu might
448 	 * still execute. Wait for it now before we freeing percpu caches.
449 	 */
450 	rcu_barrier_tasks_trace();
451 	rcu_barrier();
452 	free_mem_alloc_no_barrier(ma);
453 }
454 
455 static void free_mem_alloc_deferred(struct work_struct *work)
456 {
457 	struct bpf_mem_alloc *ma = container_of(work, struct bpf_mem_alloc, work);
458 
459 	free_mem_alloc(ma);
460 	kfree(ma);
461 }
462 
463 static void destroy_mem_alloc(struct bpf_mem_alloc *ma, int rcu_in_progress)
464 {
465 	struct bpf_mem_alloc *copy;
466 
467 	if (!rcu_in_progress) {
468 		/* Fast path. No callbacks are pending, hence no need to do
469 		 * rcu_barrier-s.
470 		 */
471 		free_mem_alloc_no_barrier(ma);
472 		return;
473 	}
474 
475 	copy = kmalloc(sizeof(*ma), GFP_KERNEL);
476 	if (!copy) {
477 		/* Slow path with inline barrier-s */
478 		free_mem_alloc(ma);
479 		return;
480 	}
481 
482 	/* Defer barriers into worker to let the rest of map memory to be freed */
483 	copy->cache = ma->cache;
484 	ma->cache = NULL;
485 	copy->caches = ma->caches;
486 	ma->caches = NULL;
487 	INIT_WORK(&copy->work, free_mem_alloc_deferred);
488 	queue_work(system_unbound_wq, &copy->work);
489 }
490 
491 void bpf_mem_alloc_destroy(struct bpf_mem_alloc *ma)
492 {
493 	struct bpf_mem_caches *cc;
494 	struct bpf_mem_cache *c;
495 	int cpu, i, rcu_in_progress;
496 
497 	if (ma->cache) {
498 		rcu_in_progress = 0;
499 		for_each_possible_cpu(cpu) {
500 			c = per_cpu_ptr(ma->cache, cpu);
501 			drain_mem_cache(c);
502 			rcu_in_progress += atomic_read(&c->call_rcu_in_progress);
503 		}
504 		/* objcg is the same across cpus */
505 		if (c->objcg)
506 			obj_cgroup_put(c->objcg);
507 		destroy_mem_alloc(ma, rcu_in_progress);
508 	}
509 	if (ma->caches) {
510 		rcu_in_progress = 0;
511 		for_each_possible_cpu(cpu) {
512 			cc = per_cpu_ptr(ma->caches, cpu);
513 			for (i = 0; i < NUM_CACHES; i++) {
514 				c = &cc->cache[i];
515 				drain_mem_cache(c);
516 				rcu_in_progress += atomic_read(&c->call_rcu_in_progress);
517 			}
518 		}
519 		if (c->objcg)
520 			obj_cgroup_put(c->objcg);
521 		destroy_mem_alloc(ma, rcu_in_progress);
522 	}
523 }
524 
525 /* notrace is necessary here and in other functions to make sure
526  * bpf programs cannot attach to them and cause llist corruptions.
527  */
528 static void notrace *unit_alloc(struct bpf_mem_cache *c)
529 {
530 	struct llist_node *llnode = NULL;
531 	unsigned long flags;
532 	int cnt = 0;
533 
534 	/* Disable irqs to prevent the following race for majority of prog types:
535 	 * prog_A
536 	 *   bpf_mem_alloc
537 	 *      preemption or irq -> prog_B
538 	 *        bpf_mem_alloc
539 	 *
540 	 * but prog_B could be a perf_event NMI prog.
541 	 * Use per-cpu 'active' counter to order free_list access between
542 	 * unit_alloc/unit_free/bpf_mem_refill.
543 	 */
544 	local_irq_save(flags);
545 	if (local_inc_return(&c->active) == 1) {
546 		llnode = __llist_del_first(&c->free_llist);
547 		if (llnode)
548 			cnt = --c->free_cnt;
549 	}
550 	local_dec(&c->active);
551 	local_irq_restore(flags);
552 
553 	WARN_ON(cnt < 0);
554 
555 	if (cnt < c->low_watermark)
556 		irq_work_raise(c);
557 	return llnode;
558 }
559 
560 /* Though 'ptr' object could have been allocated on a different cpu
561  * add it to the free_llist of the current cpu.
562  * Let kfree() logic deal with it when it's later called from irq_work.
563  */
564 static void notrace unit_free(struct bpf_mem_cache *c, void *ptr)
565 {
566 	struct llist_node *llnode = ptr - LLIST_NODE_SZ;
567 	unsigned long flags;
568 	int cnt = 0;
569 
570 	BUILD_BUG_ON(LLIST_NODE_SZ > 8);
571 
572 	local_irq_save(flags);
573 	if (local_inc_return(&c->active) == 1) {
574 		__llist_add(llnode, &c->free_llist);
575 		cnt = ++c->free_cnt;
576 	} else {
577 		/* unit_free() cannot fail. Therefore add an object to atomic
578 		 * llist. free_bulk() will drain it. Though free_llist_extra is
579 		 * a per-cpu list we have to use atomic llist_add here, since
580 		 * it also can be interrupted by bpf nmi prog that does another
581 		 * unit_free() into the same free_llist_extra.
582 		 */
583 		llist_add(llnode, &c->free_llist_extra);
584 	}
585 	local_dec(&c->active);
586 	local_irq_restore(flags);
587 
588 	if (cnt > c->high_watermark)
589 		/* free few objects from current cpu into global kmalloc pool */
590 		irq_work_raise(c);
591 }
592 
593 /* Called from BPF program or from sys_bpf syscall.
594  * In both cases migration is disabled.
595  */
596 void notrace *bpf_mem_alloc(struct bpf_mem_alloc *ma, size_t size)
597 {
598 	int idx;
599 	void *ret;
600 
601 	if (!size)
602 		return ZERO_SIZE_PTR;
603 
604 	idx = bpf_mem_cache_idx(size + LLIST_NODE_SZ);
605 	if (idx < 0)
606 		return NULL;
607 
608 	ret = unit_alloc(this_cpu_ptr(ma->caches)->cache + idx);
609 	return !ret ? NULL : ret + LLIST_NODE_SZ;
610 }
611 
612 void notrace bpf_mem_free(struct bpf_mem_alloc *ma, void *ptr)
613 {
614 	int idx;
615 
616 	if (!ptr)
617 		return;
618 
619 	idx = bpf_mem_cache_idx(ksize(ptr - LLIST_NODE_SZ));
620 	if (idx < 0)
621 		return;
622 
623 	unit_free(this_cpu_ptr(ma->caches)->cache + idx, ptr);
624 }
625 
626 void notrace *bpf_mem_cache_alloc(struct bpf_mem_alloc *ma)
627 {
628 	void *ret;
629 
630 	ret = unit_alloc(this_cpu_ptr(ma->cache));
631 	return !ret ? NULL : ret + LLIST_NODE_SZ;
632 }
633 
634 void notrace bpf_mem_cache_free(struct bpf_mem_alloc *ma, void *ptr)
635 {
636 	if (!ptr)
637 		return;
638 
639 	unit_free(this_cpu_ptr(ma->cache), ptr);
640 }
641