The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [PATCH bpf-next] bpf: Add a retry after refilling the free list when unit_alloc() fails
@ 2025-02-12  8:48 Changwoo Min
  2025-02-12 18:33 ` Song Liu
  2025-02-13 17:45 ` Alexei Starovoitov
  0 siblings, 2 replies; 11+ messages in thread
From: Changwoo Min @ 2025-02-12  8:48 UTC (permalink / raw)
  To: ast, daniel, andrii, martin.lau, eddyz87, song, yonghong.song,
	john.fastabend, kpsingh, sdf, haoluo, jolsa
  Cc: tj, arighi, kernel-dev, bpf, linux-kernel, Changwoo Min

When there is no entry in the free list (c->free_llist), unit_alloc()
fails even when there is available memory in the system, causing allocation
failure in various BPF calls -- such as bpf_mem_alloc() and
bpf_cpumask_create().

Such allocation failure can happen, especially when a BPF program tries many
allocations -- more than a delta between high and low watermarks -- in an
IRQ-disabled context.

To address the problem, when there is no free entry, refill one entry on the
free list (alloc_bulk) and then retry the allocation procedure on the free
list. Note that since some callers of unit_alloc() do not allow to block
(e.g., bpf_cpumask_create), allocate the additional free entry in an atomic
manner (atomic = true in alloc_bulk).

Signed-off-by: Changwoo Min <changwoo@igalia.com>
---
 kernel/bpf/memalloc.c | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/kernel/bpf/memalloc.c b/kernel/bpf/memalloc.c
index 889374722d0a..22fe9cfb2b56 100644
--- a/kernel/bpf/memalloc.c
+++ b/kernel/bpf/memalloc.c
@@ -784,6 +784,7 @@ static void notrace *unit_alloc(struct bpf_mem_cache *c)
 	struct llist_node *llnode = NULL;
 	unsigned long flags;
 	int cnt = 0;
+	bool retry = false;
 
 	/* Disable irqs to prevent the following race for majority of prog types:
 	 * prog_A
@@ -795,6 +796,7 @@ static void notrace *unit_alloc(struct bpf_mem_cache *c)
 	 * Use per-cpu 'active' counter to order free_list access between
 	 * unit_alloc/unit_free/bpf_mem_refill.
 	 */
+retry_alloc:
 	local_irq_save(flags);
 	if (local_inc_return(&c->active) == 1) {
 		llnode = __llist_del_first(&c->free_llist);
@@ -815,6 +817,13 @@ static void notrace *unit_alloc(struct bpf_mem_cache *c)
 	 */
 	local_irq_restore(flags);
 
+	if (unlikely(!llnode && !retry)) {
+		int cpu = smp_processor_id();
+		alloc_bulk(c, 1, cpu_to_node(cpu), true);
+		retry = true;
+		goto retry_alloc;
+	}
+
 	return llnode;
 }
 
-- 
2.48.1


^ permalink raw reply related	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2025-02-17  2:20 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-02-12  8:48 [PATCH bpf-next] bpf: Add a retry after refilling the free list when unit_alloc() fails Changwoo Min
2025-02-12 18:33 ` Song Liu
2025-02-13  8:41   ` Changwoo Min
2025-02-13  9:05     ` Kumar Kartikeya Dwivedi
2025-02-13 10:11       ` Changwoo Min
2025-02-13 17:45 ` Alexei Starovoitov
2025-02-14  9:23   ` Changwoo Min
2025-02-15  3:51     ` Alexei Starovoitov
2025-02-15 15:16       ` Changwoo Min
2025-02-16 16:04         ` Changwoo Min
2025-02-17  2:19           ` Hou Tao

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox