Skip to contentSkip to conversation

Replace BPF memory allocator with kmalloc_nolock() in local storage

PATCH Applied Amery Hung 6 participants 17 messages Nov 14, 2025 – Apr 10

Subsystems (4)
lore
Share & tools
lore

Amery Hung

author
#0 lore raw

v2 cover Replace BPF memory allocator with kmalloc_nolock() in local storage

Hi,

This patchset tries to simplify bpf_local_storage.c by adopting kmalloc_nolock(). This removes memory preallocation and reduces the dependency of smap in bpf_selem_free() and bpf_local_storage_free(). The later will simplify a future refactor that replaces local_storage->lock and b->lock [1].

RFC v1 tried to switch to kmalloc_nolock() unconditionally. However, as there is substantial performance loss in socket local storage due to
1) defer_free() in kfree_nolock() and 2) no kfree_rcu() batching, replacing kzalloc() is postponed until necessary improvements in mm land.

Benchmark

./bench -p 1 local-storage-create --storage-type <socket,task> \
--batch-size <16,32,64>

The benchmark is a microbenchmark stress-testing how fast local storage can be created. For task local storage, switching from BPF memory allocator to kmalloc_nolock() yields a small amount of improvement. For socket local storage, it remains roughly the same as nothing has changed.

Socket local storage

memory alloc     batch  creation speed              creation speed diff
---------------  ----   ------------------                         ----
kzalloc           16    144.149 ± 0.642k/s  3.10 kmallocs/create
(before)          32    144.379 ± 1.070k/s  3.08 kmallocs/create
                  64    144.491 ± 0.818k/s  3.13 kmallocs/create
kzalloc           16    146.180 ± 1.403k/s  3.10 kmallocs/create  +1.4%
(not changed)     32    146.245 ± 1.272k/s  3.10 kmallocs/create  +1.3%
                  64    145.012 ± 1.545k/s  3.10 kmallocs/create  +0.4%

Task local storage

memory alloc     batch  creation speed              creation speed diff
---------------  ----   ------------------                         ----
BPF memory        16     24.668 ± 0.121k/s  2.54 kmallocs/create
allocator         32     22.899 ± 0.097k/s  2.67 kmallocs/create
(before)          64     22.559 ± 0.076k/s  2.56 kmallocs/create
kmalloc_nolock    16     25.796 ± 0.059k/s  2.52 kmallocs/create  +4.6%
(after)           32     23.412 ± 0.069k/s  2.50 kmallocs/create  +2.2%
                  64     23.717 ± 0.108k/s  2.60 kmallocs/create  +5.1%

[1] https://lore.kernel.org/bpf/[email protected]/

v1 -> v2
- Only replace BPF memory allocator with kmalloc_nolock()

---

Amery Hung (4):
bpf: Always charge/uncharge memory when allocating/unlinking storage
elements
bpf: Remove smap argument from bpf_selem_free()
bpf: Save memory alloction info in bpf_local_storage
bpf: Replace bpf memory allocator with kmalloc_nolock() in local
storage

 include/linux/bpf_local_storage.h |  10 +-
 kernel/bpf/bpf_local_storage.c    | 235 +++++++++---------------------
 net/core/bpf_sk_storage.c         |   4 +-
 3 files changed, 74 insertions(+), 175 deletions(-)
Signature · 1 line
2.47.3
author
#1 lore raw

PATCH v2 1/4 bpf: Always charge/uncharge memory when allocating/unlinking storage elements

Since commit a96a44aba556 ("bpf: bpf_sk_storage: Fix invalid wait context lockdep report"), {charge,uncharge}_mem are always true when allocating a bpf_local_storage_elem or unlinking a bpf_local_storage_elem from local storage, so drop these arguments. No functional change.

---
 include/linux/bpf_local_storage.h |  2 +-
 kernel/bpf/bpf_local_storage.c    | 22 ++++++++++------------
 net/core/bpf_sk_storage.c         |  2 +-
 3 files changed, 12 insertions(+), 14 deletions(-)
include/linux/bpf_local_storage.h +1 -1
index 782f58feea35..3663eabcc3ff 100644
@@ -184,7 +184,7 @@ void bpf_selem_link_map(struct bpf_local_storage_map *smap,
 
 struct bpf_local_storage_elem *
 bpf_selem_alloc(struct bpf_local_storage_map *smap, void *owner, void *value,
-		bool charge_mem, bool swap_uptrs, gfp_t gfp_flags);
+		bool swap_uptrs, gfp_t gfp_flags);
 
 void bpf_selem_free(struct bpf_local_storage_elem *selem,
 		    struct bpf_local_storage_map *smap,
kernel/bpf/bpf_local_storage.c +10 -12
index b931fbceb54d..400bdf8a3eb2 100644
@@ -73,11 +73,11 @@ static bool selem_linked_to_map(const struct bpf_local_storage_elem *selem)
 
 struct bpf_local_storage_elem *
 bpf_selem_alloc(struct bpf_local_storage_map *smap, void *owner,
-		void *value, bool charge_mem, bool swap_uptrs, gfp_t gfp_flags)
+		void *value, bool swap_uptrs, gfp_t gfp_flags)
 {
 	struct bpf_local_storage_elem *selem;
 
-	if (charge_mem && mem_charge(smap, owner, smap->elem_size))
+	if (mem_charge(smap, owner, smap->elem_size))
 		return NULL;
 
 	if (smap->bpf_ma) {
@@ -106,8 +106,7 @@ bpf_selem_alloc(struct bpf_local_storage_map *smap, void *owner,
 		return selem;
 	}
 
-	if (charge_mem)
-		mem_uncharge(smap, owner, smap->elem_size);
+	mem_uncharge(smap, owner, smap->elem_size);
 
 	return NULL;
 }
@@ -284,7 +283,7 @@ static void bpf_selem_free_list(struct hlist_head *list, bool reuse_now)
  */
 static bool bpf_selem_unlink_storage_nolock(struct bpf_local_storage *local_storage,
 					    struct bpf_local_storage_elem *selem,
-					    bool uncharge_mem, struct hlist_head *free_selem_list)
+					    struct hlist_head *free_selem_list)
 {
 	struct bpf_local_storage_map *smap;
 	bool free_local_storage;
@@ -297,8 +296,7 @@ static bool bpf_selem_unlink_storage_nolock(struct bpf_local_storage *local_stor
 	 * The owner may be freed once the last selem is unlinked
 	 * from local_storage.
 	 */
-	if (uncharge_mem)
-		mem_uncharge(smap, owner, smap->elem_size);
+	mem_uncharge(smap, owner, smap->elem_size);
 
 	free_local_storage = hlist_is_singular_node(&selem->snode,
 						    &local_storage->list);
@@ -393,7 +391,7 @@ static void bpf_selem_unlink_storage(struct bpf_local_storage_elem *selem,
 	raw_spin_lock_irqsave(&local_storage->lock, flags);
 	if (likely(selem_linked_to_storage(selem)))
 		free_local_storage = bpf_selem_unlink_storage_nolock(
-			local_storage, selem, true, &selem_free_list);
+			local_storage, selem, &selem_free_list);
 	raw_spin_unlock_irqrestore(&local_storage->lock, flags);
 
 	bpf_selem_free_list(&selem_free_list, reuse_now);
@@ -582,7 +580,7 @@ bpf_local_storage_update(void *owner, struct bpf_local_storage_map *smap,
 		if (err)
 			return ERR_PTR(err);
 
-		selem = bpf_selem_alloc(smap, owner, value, true, swap_uptrs, gfp_flags);
+		selem = bpf_selem_alloc(smap, owner, value, swap_uptrs, gfp_flags);
 		if (!selem)
 			return ERR_PTR(-ENOMEM);
 
@@ -616,7 +614,7 @@ bpf_local_storage_update(void *owner, struct bpf_local_storage_map *smap,
 	/* A lookup has just been done before and concluded a new selem is
 	 * needed. The chance of an unnecessary alloc is unlikely.
 	 */
-	alloc_selem = selem = bpf_selem_alloc(smap, owner, value, true, swap_uptrs, gfp_flags);
+	alloc_selem = selem = bpf_selem_alloc(smap, owner, value, swap_uptrs, gfp_flags);
 	if (!alloc_selem)
 		return ERR_PTR(-ENOMEM);
 
@@ -656,7 +654,7 @@ bpf_local_storage_update(void *owner, struct bpf_local_storage_map *smap,
 	if (old_sdata) {
 		bpf_selem_unlink_map(SELEM(old_sdata));
 		bpf_selem_unlink_storage_nolock(local_storage, SELEM(old_sdata),
-						true, &old_selem_free_list);
+						&old_selem_free_list);
 	}
 
 unlock:
@@ -762,7 +760,7 @@ void bpf_local_storage_destroy(struct bpf_local_storage *local_storage)
 		 * of the loop will set the free_cgroup_storage to true.
 		 */
 		free_storage = bpf_selem_unlink_storage_nolock(
-			local_storage, selem, true, &free_selem_list);
+			local_storage, selem, &free_selem_list);
 	}
 	raw_spin_unlock_irqrestore(&local_storage->lock, flags);
 
net/core/bpf_sk_storage.c +1 -1
index d3fbaf89a698..bd3c686edc0b 100644
@@ -136,7 +136,7 @@ bpf_sk_storage_clone_elem(struct sock *newsk,
 {
 	struct bpf_local_storage_elem *copy_selem;
 
-	copy_selem = bpf_selem_alloc(smap, newsk, NULL, true, false, GFP_ATOMIC);
+	copy_selem = bpf_selem_alloc(smap, newsk, NULL, false, GFP_ATOMIC);
 	if (!copy_selem)
 		return NULL;
 
Signature · 1 line
2.47.3
author
#3 lore raw

PATCH v2 2/4 bpf: Remove smap argument from bpf_selem_free()

In reply to Amery Hung

Since selem already saves a pointer to smap, use it instead of an additional argument in bpf_selem_free(). This requires moving the SDATA(selem)->smap assignment from bpf_selem_link_map() to bpf_selem_alloc() since bpf_selem_free() may be called without the selem being linked to smap in bpf_local_storage_update().

---
 include/linux/bpf_local_storage.h |  1 -
 kernel/bpf/bpf_local_storage.c    | 19 ++++++++++---------
 net/core/bpf_sk_storage.c         |  2 +-
 3 files changed, 11 insertions(+), 11 deletions(-)
include/linux/bpf_local_storage.h +0 -1
index 3663eabcc3ff..4ab137e75f33 100644
@@ -187,7 +187,6 @@ bpf_selem_alloc(struct bpf_local_storage_map *smap, void *owner, void *value,
 		bool swap_uptrs, gfp_t gfp_flags);
 
 void bpf_selem_free(struct bpf_local_storage_elem *selem,
-		    struct bpf_local_storage_map *smap,
 		    bool reuse_now);
 
 int
kernel/bpf/bpf_local_storage.c +10 -9
index 400bdf8a3eb2..95a5ea618cc5 100644
@@ -97,6 +97,8 @@ bpf_selem_alloc(struct bpf_local_storage_map *smap, void *owner,
 	}
 
 	if (selem) {
+		RCU_INIT_POINTER(SDATA(selem)->smap, smap);
+
 		if (value) {
 			/* No need to call check_and_init_map_value as memory is zero init */
 			copy_map_value(&smap->map, SDATA(selem)->data, value);
@@ -227,9 +229,12 @@ static void bpf_selem_free_trace_rcu(struct rcu_head *rcu)
 }
 
 void bpf_selem_free(struct bpf_local_storage_elem *selem,
-		    struct bpf_local_storage_map *smap,
 		    bool reuse_now)
 {
+	struct bpf_local_storage_map *smap;
+
+	smap = rcu_dereference_check(SDATA(selem)->smap, bpf_rcu_lock_held());
+
 	if (!smap->bpf_ma) {
 		/* Only task storage has uptrs and task storage
 		 * has moved to bpf_mem_alloc. Meaning smap->bpf_ma == true
@@ -263,7 +268,6 @@ void bpf_selem_free(struct bpf_local_storage_elem *selem,
 static void bpf_selem_free_list(struct hlist_head *list, bool reuse_now)
 {
 	struct bpf_local_storage_elem *selem;
-	struct bpf_local_storage_map *smap;
 	struct hlist_node *n;
 
 	/* The "_safe" iteration is needed.
@@ -271,10 +275,8 @@ static void bpf_selem_free_list(struct hlist_head *list, bool reuse_now)
 	 * but bpf_selem_free will use the selem->rcu_head
 	 * which is union-ized with the selem->free_node.
 	 */
-	hlist_for_each_entry_safe(selem, n, list, free_node) {
-		smap = rcu_dereference_check(SDATA(selem)->smap, bpf_rcu_lock_held());
-		bpf_selem_free(selem, smap, reuse_now);
-	}
+	hlist_for_each_entry_safe(selem, n, list, free_node)
+		bpf_selem_free(selem, reuse_now);
 }
 
 /* local_storage->lock must be held and selem->local_storage == local_storage.
@@ -432,7 +434,6 @@ void bpf_selem_link_map(struct bpf_local_storage_map *smap,
 	unsigned long flags;
 
 	raw_spin_lock_irqsave(&b->lock, flags);
-	RCU_INIT_POINTER(SDATA(selem)->smap, smap);
 	hlist_add_head_rcu(&selem->map_node, &b->list);
 	raw_spin_unlock_irqrestore(&b->lock, flags);
 }
@@ -586,7 +587,7 @@ bpf_local_storage_update(void *owner, struct bpf_local_storage_map *smap,
 
 		err = bpf_local_storage_alloc(owner, smap, selem, gfp_flags);
 		if (err) {
-			bpf_selem_free(selem, smap, true);
+			bpf_selem_free(selem, true);
 			mem_uncharge(smap, owner, smap->elem_size);
 			return ERR_PTR(err);
 		}
@@ -662,7 +663,7 @@ bpf_local_storage_update(void *owner, struct bpf_local_storage_map *smap,
 	bpf_selem_free_list(&old_selem_free_list, false);
 	if (alloc_selem) {
 		mem_uncharge(smap, owner, smap->elem_size);
-		bpf_selem_free(alloc_selem, smap, true);
+		bpf_selem_free(alloc_selem, true);
 	}
 	return err ? ERR_PTR(err) : SDATA(selem);
 }
net/core/bpf_sk_storage.c +1 -1
index bd3c686edc0b..850dd736ccd1 100644
@@ -196,7 +196,7 @@ int bpf_sk_storage_clone(const struct sock *sk, struct sock *newsk)
 		} else {
 			ret = bpf_local_storage_alloc(newsk, smap, copy_selem, GFP_ATOMIC);
 			if (ret) {
-				bpf_selem_free(copy_selem, smap, true);
+				bpf_selem_free(copy_selem, true);
 				atomic_sub(smap->elem_size,
 					   &newsk->sk_omem_alloc);
 				bpf_map_put(map);
Signature · 1 line
2.47.3
author
#5 lore raw

PATCH v2 3/4 bpf: Save memory alloction info in bpf_local_storage

In reply to Amery Hung

Save the memory allocation method used for bpf_local_storage in the struct explicitly so that we don't need to go through the hassle to find out the info. When a later patch replaces BPF memory allocator with kmalloc_noloc(), bpf_local_storage_free() will no longer need smap->storage_ma to return the memory and completely remove the dependency on smap in bpf_local_storage_free().

---
 include/linux/bpf_local_storage.h |  1 +
 kernel/bpf/bpf_local_storage.c    | 52 +++++--------------------------
 2 files changed, 9 insertions(+), 44 deletions(-)
include/linux/bpf_local_storage.h +1 -0
index 4ab137e75f33..7fef0cec8340 100644
@@ -97,6 +97,7 @@ struct bpf_local_storage {
 				 */
 	struct rcu_head rcu;
 	raw_spinlock_t lock;	/* Protect adding/removing from the "list" */
+	bool bpf_ma;
 };
 
 /* U16_MAX is much more than enough for sk local storage
kernel/bpf/bpf_local_storage.c +8 -44

114 lines not on this page · raw message

Signature · 1 line
2.47.3
author
#7 lore raw

PATCH v2 4/4 bpf: Replace bpf memory allocator with kmalloc_nolock() in local storage

In reply to Amery Hung

Replace bpf memory allocator with kmalloc_nolock() to reduce memory wastage due to preallocation.

In bpf_selem_free(), an selem now needs to wait for a RCU grace period before being freed when reuse_now == true. Therefore, rcu_barrier() should be always be called in bpf_local_storage_map_free().

In bpf_local_storage_free(), since smap->storage_ma is no longer needed to return the memory, the function is now independent from smap.

Remove the outdated comment in bpf_local_storage_alloc(). We already free selem after an RCU grace period in bpf_local_storage_update() when bpf_local_storage_alloc() failed the cmpxchg since commit c0d63f309186 ("bpf: Add bpf_selem_free()").

---
 include/linux/bpf_local_storage.h |   8 +-
 kernel/bpf/bpf_local_storage.c    | 152 +++++++++---------------------
 2 files changed, 48 insertions(+), 112 deletions(-)
include/linux/bpf_local_storage.h +3 -5
index 7fef0cec8340..66432248cd81 100644
@@ -53,9 +53,7 @@ struct bpf_local_storage_map {
 	u32 bucket_log;
 	u16 elem_size;
 	u16 cache_idx;
-	struct bpf_mem_alloc selem_ma;
-	struct bpf_mem_alloc storage_ma;
-	bool bpf_ma;
+	bool use_kmalloc_nolock;
 };
 
 struct bpf_local_storage_data {
@@ -97,7 +95,7 @@ struct bpf_local_storage {
 				 */
 	struct rcu_head rcu;
 	raw_spinlock_t lock;	/* Protect adding/removing from the "list" */
-	bool bpf_ma;
+	bool use_kmalloc_nolock;
 };
 
 /* U16_MAX is much more than enough for sk local storage
@@ -131,7 +129,7 @@ int bpf_local_storage_map_alloc_check(union bpf_attr *attr);
 struct bpf_map *
 bpf_local_storage_map_alloc(union bpf_attr *attr,
 			    struct bpf_local_storage_cache *cache,
-			    bool bpf_ma);
+			    bool use_kmalloc_nolock);
 
 void __bpf_local_storage_insert_cache(struct bpf_local_storage *local_storage,
 				      struct bpf_local_storage_map *smap,
kernel/bpf/bpf_local_storage.c +45 -107

303 lines not on this page · raw message

Signature · 1 line
2.47.3
#8 lore raw

Re: [PATCH v2 bpf-next 4/4] bpf: Replace bpf memory allocator with kmalloc_nolock() in local storage

kernel/bpf/bpf_local_storage.c void bpf_local_storage_map_free(struct bpf_map *map, · line 821

Quoted 10 lines · Amery Hung
kernel/bpf/bpf_local_storage.c +2 -5
-       if (smap->bpf_ma) {
+       if (smap->use_kmalloc_nolock) {
                rcu_barrier_tasks_trace();
-               if (!rcu_trace_implies_rcu_gp())
-                       rcu_barrier();
-               bpf_mem_alloc_destroy(&smap->selem_ma);
-               bpf_mem_alloc_destroy(&smap->storage_ma);
+               rcu_barrier();

Why unconditional rcu_barrier() ? It's implied in rcu_barrier_tasks_trace(). What am I missing?

The rest looks good.
If that's the only issue, I can fix it up while applying.

reviewer Reviewed-by on v2 1/4
#2 lore raw

Re: [PATCH v2 bpf-next 1/4] bpf: Always charge/uncharge memory when allocating/unlinking storage elements

In reply to Amery Hung
Quoted 4 lines · Amery Hung

Since commit a96a44aba556 ("bpf: bpf_sk_storage: Fix invalid wait context lockdep report"), {charge,uncharge}_mem are always true when allocating a bpf_local_storage_elem or unlinking a bpf_local_storage_elem from local storage, so drop these arguments. No functional change.

reviewer Reviewed-by on v2 2/4
#4 lore raw

Re: [PATCH v2 bpf-next 2/4] bpf: Remove smap argument from bpf_selem_free()

In reply to Amery Hung
Quoted 5 lines · Amery Hung

Since selem already saves a pointer to smap, use it instead of an additional argument in bpf_selem_free(). This requires moving the SDATA(selem)->smap assignment from bpf_selem_link_map() to bpf_selem_alloc() since bpf_selem_free() may be called without the selem being linked to smap in bpf_local_storage_update().

reviewer Reviewed-by on v2 3/4
#6 lore raw

Re: [PATCH v2 bpf-next 3/4] bpf: Save memory alloction info in bpf_local_storage

In reply to Amery Hung
Quoted 6 lines · Amery Hung

Save the memory allocation method used for bpf_local_storage in the struct explicitly so that we don't need to go through the hassle to find out the info. When a later patch replaces BPF memory allocator with kmalloc_noloc(), bpf_local_storage_free() will no longer need smap->storage_ma to return the memory and completely remove the dependency on smap in bpf_local_storage_free().

reviewer Reviewed-by
#9 lore raw

Re: [PATCH v2 bpf-next 4/4] bpf: Replace bpf memory allocator with kmalloc_nolock() in local storage

In reply to Alexei Starovoitov
Quoted 12 lines · Alexei Starovoitov

kernel/bpf/bpf_local_storage.c void bpf_local_storage_map_free(struct bpf_map *map, · line 821

Quoted 10 lines · Amery Hung
kernel/bpf/bpf_local_storage.c +2 -5
-       if (smap->bpf_ma) {
+       if (smap->use_kmalloc_nolock) {
                 rcu_barrier_tasks_trace();
-               if (!rcu_trace_implies_rcu_gp())
-                       rcu_barrier();
-               bpf_mem_alloc_destroy(&smap->selem_ma);
-               bpf_mem_alloc_destroy(&smap->storage_ma);
+               rcu_barrier();
Quoted 3 lines

Why unconditional rcu_barrier() ?
It's implied in rcu_barrier_tasks_trace().
What am I missing?

Amery probably can confirm. I think the bpf_obj_free_fields() may only need to wait for a rcu gp without going through a rcu_tasks_trace gp and the tasks_trace cb, so it needs to ensure all rcu callbacks has finished.

@@ -247,18 +231,11 @@ void bpf_selem_free(struct bpf_local_storage_elem *selem,
}

if (reuse_now) {
- /* reuse_now == true only happens when the storage owner
- * (e.g. task_struct) is being destructed or the map itself
- * is being destructed (ie map_free). In both cases,
- * no bpf prog can have a hold on the selem. It is
- * safe to unpin the uptrs and free the selem now.
- */
- bpf_obj_free_fields(smap->map.record, SDATA(selem)->data);
- /* Instead of using the vanilla call_rcu(),
- * bpf_mem_cache_free will be able to reuse selem
- * immediately.
+ /*
+ * While it is okay to call bpf_obj_free_fields() that unpins uptr when
+ * reuse_now == true, keep it in bpf_selem_free_rcu() for simplicity.
*/
- bpf_mem_cache_free(&smap->selem_ma, selem);
+ call_rcu(&selem->rcu, bpf_selem_free_rcu);
return;
}

Others lgtm also,

author
#10 lore raw

Re: [PATCH v2 bpf-next 4/4] bpf: Replace bpf memory allocator with kmalloc_nolock() in local storage

In reply to Alexei Starovoitov
Quoted 13 lines · Alexei Starovoitov

kernel/bpf/bpf_local_storage.c void bpf_local_storage_map_free(struct bpf_map *map, · line 821

Quoted 10 lines · Amery Hung
kernel/bpf/bpf_local_storage.c +2 -5
-       if (smap->bpf_ma) {
+       if (smap->use_kmalloc_nolock) {
                rcu_barrier_tasks_trace();
-               if (!rcu_trace_implies_rcu_gp())
-                       rcu_barrier();
-               bpf_mem_alloc_destroy(&smap->selem_ma);
-               bpf_mem_alloc_destroy(&smap->storage_ma);
+               rcu_barrier();
Quoted 2 lines

Why unconditional rcu_barrier() ?
It's implied in rcu_barrier_tasks_trace().

Hmm, I am not sure.

Quoted 1 line

What am I missing?

I hit a UAF in v1 in bpf_selem_free_rcu() when running selftests and making rcu_barrier() unconditional addressed it. I think the bug was due to map_free() not waiting for bpf_selem_free_rcu() (an RCU callback) to finish.

Looking at rcu_barrier() and rcu_barrier_tasks_trace(), they pass different rtp to rcu_barrier_tasks_generic() so I think both are needed to make sure in-flight RCU and RCU tasks trace callbacks are done.

Not an expert in RCU so I might be wrong and it was something else.

Quoted 3 lines

The rest looks good.
If that's the only issue, I can fix it up while applying.

#11 lore raw

Re: [PATCH v2 bpf-next 4/4] bpf: Replace bpf memory allocator with kmalloc_nolock() in local storage

Quoted 28 lines · Amery Hung
Quoted 13 lines · Alexei Starovoitov

kernel/bpf/bpf_local_storage.c void bpf_local_storage_map_free(struct bpf_map *map, · line 821

Quoted 10 lines · Amery Hung
kernel/bpf/bpf_local_storage.c +2 -5
-       if (smap->bpf_ma) {
+       if (smap->use_kmalloc_nolock) {
                rcu_barrier_tasks_trace();
-               if (!rcu_trace_implies_rcu_gp())
-                       rcu_barrier();
-               bpf_mem_alloc_destroy(&smap->selem_ma);
-               bpf_mem_alloc_destroy(&smap->storage_ma);
+               rcu_barrier();
Quoted 2 lines

Why unconditional rcu_barrier() ?
It's implied in rcu_barrier_tasks_trace().

Hmm, I am not sure.

Quoted 1 line

What am I missing?

I hit a UAF in v1 in bpf_selem_free_rcu() when running selftests and making rcu_barrier() unconditional addressed it. I think the bug was due to map_free() not waiting for bpf_selem_free_rcu() (an RCU callback) to finish.

Quoted 6 lines

Looking at rcu_barrier() and rcu_barrier_tasks_trace(), they pass different rtp to rcu_barrier_tasks_generic() so I think both are needed to make sure in-flight RCU and RCU tasks trace callbacks are done.

Not an expert in RCU so I might be wrong and it was something else.

Paul,

Please help us here.
Does rcu_barrier_tasks_trace() imply rcu_barrier() ?

#12 lore raw

Re: [PATCH v2 bpf-next 4/4] bpf: Replace bpf memory allocator with kmalloc_nolock() in local storage

Quoted 34 lines · Alexei Starovoitov
Quoted 33 lines · Amery Hung
Quoted 13 lines · Alexei Starovoitov

kernel/bpf/bpf_local_storage.c void bpf_local_storage_map_free(struct bpf_map *map, · line 821

Quoted 10 lines · Amery Hung
kernel/bpf/bpf_local_storage.c +2 -5
-       if (smap->bpf_ma) {
+       if (smap->use_kmalloc_nolock) {
                rcu_barrier_tasks_trace();
-               if (!rcu_trace_implies_rcu_gp())
-                       rcu_barrier();
-               bpf_mem_alloc_destroy(&smap->selem_ma);
-               bpf_mem_alloc_destroy(&smap->storage_ma);
+               rcu_barrier();
Quoted 2 lines

Why unconditional rcu_barrier() ?
It's implied in rcu_barrier_tasks_trace().

Hmm, I am not sure.

Quoted 1 line

What am I missing?

I hit a UAF in v1 in bpf_selem_free_rcu() when running selftests and making rcu_barrier() unconditional addressed it. I think the bug was due to map_free() not waiting for bpf_selem_free_rcu() (an RCU callback) to finish.

Looking at rcu_barrier() and rcu_barrier_tasks_trace(), they pass different rtp to rcu_barrier_tasks_generic() so I think both are needed to make sure in-flight RCU and RCU tasks trace callbacks are done.

Quoted 6 lines
Quoted 1 line

Not an expert in RCU so I might be wrong and it was something else.

Paul,

Please help us here.
Does rcu_barrier_tasks_trace() imply rcu_barrier() ?

I am sorry, but no, it does not.

If latency proves to be an issue, one approach is to invoke rcu_barrier() and rcu_barrier_tasks_trace() each in its own workqueue handler. But as always, I suggest invoking them one after the other to see if a latency problem really exists before adding complexity.

Except that rcu_barrier_tasks_trace() is never invoked by rcu_barrier(), only rcu_barrier_tasks() and rcu_barrier_tasks_trace(). So do you really mean rcu_barrier()? Or rcu_barrier_tasks()?

Either way, rcu_barrier_tasks() and rcu_barrier_tasks_trace() are also independent of each other in the sense that if you need tw wait on callbacks from both call_rcu_tasks() and call_rcu_tasks_trace(), you need both rcu_barrier_tasks() and rcu_barrier_tasks_trace() to be invoked.

Thanx, Paul

author
#13 lore raw

Re: [PATCH v2 bpf-next 4/4] bpf: Replace bpf memory allocator with kmalloc_nolock() in local storage

Quoted 36 lines · Paul E. McKenney
Quoted 34 lines · Alexei Starovoitov
Quoted 33 lines · Amery Hung
Quoted 13 lines · Alexei Starovoitov

kernel/bpf/bpf_local_storage.c void bpf_local_storage_map_free(struct bpf_map *map, · line 821

Quoted 10 lines · Amery Hung
kernel/bpf/bpf_local_storage.c +2 -5
-       if (smap->bpf_ma) {
+       if (smap->use_kmalloc_nolock) {
                rcu_barrier_tasks_trace();
-               if (!rcu_trace_implies_rcu_gp())
-                       rcu_barrier();
-               bpf_mem_alloc_destroy(&smap->selem_ma);
-               bpf_mem_alloc_destroy(&smap->storage_ma);
+               rcu_barrier();
Quoted 2 lines

Why unconditional rcu_barrier() ?
It's implied in rcu_barrier_tasks_trace().

Hmm, I am not sure.

Quoted 1 line

What am I missing?

I hit a UAF in v1 in bpf_selem_free_rcu() when running selftests and making rcu_barrier() unconditional addressed it. I think the bug was due to map_free() not waiting for bpf_selem_free_rcu() (an RCU callback) to finish.

Looking at rcu_barrier() and rcu_barrier_tasks_trace(), they pass different rtp to rcu_barrier_tasks_generic() so I think both are needed to make sure in-flight RCU and RCU tasks trace callbacks are done.

Quoted 8 lines
Quoted 6 lines
Quoted 1 line

Not an expert in RCU so I might be wrong and it was something else.

Paul,

Please help us here.
Does rcu_barrier_tasks_trace() imply rcu_barrier() ?

I am sorry, but no, it does not.

Thanks for the clarification, Paul!

Quoted 9 lines

If latency proves to be an issue, one approach is to invoke rcu_barrier() and rcu_barrier_tasks_trace() each in its own workqueue handler. But as always, I suggest invoking them one after the other to see if a latency problem really exists before adding complexity.

Except that rcu_barrier_tasks_trace() is never invoked by rcu_barrier(), only rcu_barrier_tasks() and rcu_barrier_tasks_trace(). So do you really mean rcu_barrier()? Or rcu_barrier_tasks()?

Sorry for the confusion. I misread the code. I was trying to say that rcu_barrier() and rcu_barrier_tasks_trace() seem to wait on different callacks but then referring to rcu_barrier_tasks() implementation wrongly.

Quoted 7 lines

Either way, rcu_barrier_tasks() and rcu_barrier_tasks_trace() are also independent of each other in the sense that if you need tw wait on callbacks from both call_rcu_tasks() and call_rcu_tasks_trace(), you need both rcu_barrier_tasks() and rcu_barrier_tasks_trace() to be invoked.

Thanx, Paul

#14 lore raw

Re: [PATCH v2 bpf-next 4/4] bpf: Replace bpf memory allocator with kmalloc_nolock() in local storage

Quoted 39 lines · Amery Hung
Quoted 38 lines · Paul E. McKenney
Quoted 36 lines · Alexei Starovoitov
Quoted 34 lines · Amery Hung
Quoted 13 lines · Alexei Starovoitov

kernel/bpf/bpf_local_storage.c void bpf_local_storage_map_free(struct bpf_map *map, · line 821

Quoted 10 lines · Amery Hung
kernel/bpf/bpf_local_storage.c +2 -5
-       if (smap->bpf_ma) {
+       if (smap->use_kmalloc_nolock) {
                rcu_barrier_tasks_trace();
-               if (!rcu_trace_implies_rcu_gp())
-                       rcu_barrier();
-               bpf_mem_alloc_destroy(&smap->selem_ma);
-               bpf_mem_alloc_destroy(&smap->storage_ma);
+               rcu_barrier();
Quoted 2 lines

Why unconditional rcu_barrier() ?
It's implied in rcu_barrier_tasks_trace().

Hmm, I am not sure.

Quoted 1 line

What am I missing?

I hit a UAF in v1 in bpf_selem_free_rcu() when running selftests and making rcu_barrier() unconditional addressed it. I think the bug was due to map_free() not waiting for bpf_selem_free_rcu() (an RCU callback) to finish.

Looking at rcu_barrier() and rcu_barrier_tasks_trace(), they pass different rtp to rcu_barrier_tasks_generic() so I think both are needed to make sure in-flight RCU and RCU tasks trace callbacks are done.

Not an expert in RCU so I might be wrong and it was something else.

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines

Paul,

Please help us here.
Does rcu_barrier_tasks_trace() imply rcu_barrier() ?

I am sorry, but no, it does not.

Thanks for the clarification, Paul!

No problem!

Quoted 5 lines
Quoted 5 lines

If latency proves to be an issue, one approach is to invoke rcu_barrier() and rcu_barrier_tasks_trace() each in its own workqueue handler. But as always, I suggest invoking them one after the other to see if a latency problem really exists before adding complexity.

Quoted 8 lines
Quoted 3 lines

Except that rcu_barrier_tasks_trace() is never invoked by rcu_barrier(), only rcu_barrier_tasks() and rcu_barrier_tasks_trace(). So do you really mean rcu_barrier()? Or rcu_barrier_tasks()?

Sorry for the confusion. I misread the code. I was trying to say that rcu_barrier() and rcu_barrier_tasks_trace() seem to wait on different callacks but then referring to rcu_barrier_tasks() implementation wrongly.

Well, you did reach the correct conclusion, even if by dubious means. ;-)

Thanx, Paul

Quoted 6 lines
Quoted 6 lines

Either way, rcu_barrier_tasks() and rcu_barrier_tasks_trace() are also independent of each other in the sense that if you need tw wait on callbacks from both call_rcu_tasks() and call_rcu_tasks_trace(), you need both rcu_barrier_tasks() and rcu_barrier_tasks_trace() to be invoked.

Thanx, Paul

#16 lore raw
In reply to Amery Hung
This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:

Hello:

This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:

Quoted 9 lines · On Fri, 14 Nov 2025 12:13:22 -0800 you wrote:

Hi,

This patchset tries to simplify bpf_local_storage.c by adopting kmalloc_nolock(). This removes memory preallocation and reduces the dependency of smap in bpf_selem_free() and bpf_local_storage_free(). The later will simplify a future refactor that replaces local_storage->lock and b->lock [1].

[...]

Here is the summary with links:
- [v2,bpf-next,1/4] bpf: Always charge/uncharge memory when allocating/unlinking storage elements

    https://git.kernel.org/bpf/bpf-next/c/0e854e553569
  - [v2,bpf-next,2/4] bpf: Remove smap argument from bpf_selem_free()
    https://git.kernel.org/bpf/bpf-next/c/e76a33e1c718
  - [v2,bpf-next,3/4] bpf: Save memory alloction info in bpf_local_storage
    https://git.kernel.org/bpf/bpf-next/c/39a460c4253e
  - [v2,bpf-next,4/4] bpf: Replace bpf memory allocator with kmalloc_nolock() in local storage
    https://git.kernel.org/bpf/bpf-next/c/f484f4a3e058

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
#15 lore raw

Re: [PATCH v2 bpf-next 4/4] bpf: Replace bpf memory allocator with kmalloc_nolock() in local storage

In reply to Amery Hung

I found that this patch restricts task storage value allocation to KMALLOC_MAX_CACHE_SIZE on any system regardless of CONFIG_PREEMPT_RT, which is 8KB on many systems, as use_kmalloc_nolock is set to true always for task storage by task_storage_map_alloc. Before this patch there was no such restriction and task storage supported at least 64KB allocation for value, restricted only by BPF_LOCAL_STORAGE_MAX_VALUE_SIZE. Was this KMALLOC_MAX_CACHE_SIZE restriction added deliberately by this patch?