Skip to contentSkip to conversation

Switch to kmalloc_nolock() in BPF local storage

RFC PATCH Superseded Amery Hung 2 participants 9 messages Nov 12, 2025 – Nov 13, 2025

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Amery Hung

author
#0 lore raw

cover Switch to kmalloc_nolock() in BPF local storage

Hi,

This patchset tries to simplify bpf_local_storage.c by switching to kmalloc_nolock() unconditionally. Currently, local storage adopted BPF memory allocator in task and cgroup local storage or when PREEMPT_RT is enabled to allow getting memory in different context without deadlock. However, due to performance reasons socket local storage did not switch. Using different memory allocators added a decent amount of complexity. Therefore, to make [1] and other future work in local storage simpler, this patchset consolidates the memory allocation/deallocation paths by switching to kmalloc_nolock() unconditionally.

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 losses some when switching from kzalloc() to kmalloc_nolock().

Socket local storage

memory alloc     batch  creation speed              creation speed diff
---------------  ----   ------------------                         ----
kzalloc           16    104.217 ± 0.974k/s  4.15 kmallocs/create
(before)          32    104.355 ± 0.606k/s  4.13 kmallocs/create
                  64    103.611 ± 0.707k/s  4.15 kmallocs/create
kmalloc_nolock    16    100.566 ± 0.560k/s  1.13 kmallocs/create  -3.5%
(after)           32     99.708 ± 0.684k/s  1.15 kmallocs/create  -4.5%
                  64     98.375 ± 1.757k/s  1.13 kmallocs/create  -5.1%

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.399 ± 0.142k/s  2.51 kmallocs/create  +3.0%
(after)           32     23.495 ± 1.285k/s  2.66 kmallocs/create  +2.6%
                  64     23.701 ± 0.207k/s  2.63 kmallocs/create  +5.1%

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

---

Amery Hung (2):
bpf: Always charge/uncharge memory when allocating/unlinking storage
elements
bpf: Use kmalloc_nolock() in local storage unconditionally

 include/linux/bpf_local_storage.h |  12 +-
 kernel/bpf/bpf_cgrp_storage.c     |   2 +-
 kernel/bpf/bpf_inode_storage.c    |   2 +-
 kernel/bpf/bpf_local_storage.c    | 283 +++++-------------------------
 kernel/bpf/bpf_task_storage.c     |   2 +-
 net/core/bpf_sk_storage.c         |   6 +-
 6 files changed, 53 insertions(+), 254 deletions(-)
Signature · 1 line
2.47.3
author
#1 lore raw

RFC PATCH 1/2 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
#2 lore raw

RFC PATCH 2/2 bpf: Use kmalloc_nolock() in local storage unconditionally

In reply to Amery Hung

Simplify local storage and selem memory alloc/free paths by switching to kmalloc_nolock unconditionally.

While it is okay to call bpf_obj_free_fields() immediately in bpf_selem_free() when reuse_now == true, it is kept only in bpf_selem_free_rcu() to simplify the code. This requires rcu_barrier() to be called unconditionally in bpf_local_storage_map_free() since bpf_obj_free_fields() may also be called from rcu callbacks.

In addition, remove the argument, smap, from bpf_selem_free() and rely on SDATA(selem)->smap for bpf_obj_free_fields(). This requires initializing SDATA(selem)->smap earlier during bpf_selem_alloc() as bpf_local_storage_update() can allocate an selem and free it without ever linking it to a map.

Finally, clean up and update comments. Note that, 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 |  10 +-
 kernel/bpf/bpf_cgrp_storage.c     |   2 +-
 kernel/bpf/bpf_inode_storage.c    |   2 +-
 kernel/bpf/bpf_local_storage.c    | 261 ++++--------------------------
 kernel/bpf/bpf_task_storage.c     |   2 +-
 net/core/bpf_sk_storage.c         |   4 +-
 6 files changed, 41 insertions(+), 240 deletions(-)
include/linux/bpf_local_storage.h +2 -8
index 3663eabcc3ff..3c35f0b9b86c 100644
@@ -53,9 +53,6 @@ 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;
 };
 
 struct bpf_local_storage_data {
@@ -129,8 +126,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);
+			    struct bpf_local_storage_cache *cache);
 
 void __bpf_local_storage_insert_cache(struct bpf_local_storage *local_storage,
 				      struct bpf_local_storage_map *smap,
@@ -186,9 +182,7 @@ struct bpf_local_storage_elem *
 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);
+void bpf_selem_free(struct bpf_local_storage_elem *selem, bool reuse_now);
 
 int
 bpf_local_storage_alloc(void *owner,
kernel/bpf/bpf_cgrp_storage.c +1 -1
index 0687a760974a..87d50b6b3673 100644
@@ -146,7 +146,7 @@ static int notsupp_get_next_key(struct bpf_map *map, void *key, void *next_key)
 
 static struct bpf_map *cgroup_storage_map_alloc(union bpf_attr *attr)
 {
-	return bpf_local_storage_map_alloc(attr, &cgroup_cache, true);
+	return bpf_local_storage_map_alloc(attr, &cgroup_cache);
 }
 
 static void cgroup_storage_map_free(struct bpf_map *map)
kernel/bpf/bpf_inode_storage.c +1 -1
index e54cce2b9175..30934f6a66a4 100644
@@ -181,7 +181,7 @@ static int notsupp_get_next_key(struct bpf_map *map, void *key,
 
 static struct bpf_map *inode_storage_map_alloc(union bpf_attr *attr)
 {
-	return bpf_local_storage_map_alloc(attr, &inode_cache, false);
+	return bpf_local_storage_map_alloc(attr, &inode_cache);
 }
 
 static void inode_storage_map_free(struct bpf_map *map)
kernel/bpf/bpf_local_storage.c +34 -227

434 lines not on this page · raw message

kernel/bpf/bpf_task_storage.c +1 -1
index a1dc1bf0848a..baf5de6104e2 100644
@@ -308,7 +308,7 @@ static int notsupp_get_next_key(struct bpf_map *map, void *key, void *next_key)
 
 static struct bpf_map *task_storage_map_alloc(union bpf_attr *attr)
 {
-	return bpf_local_storage_map_alloc(attr, &task_cache, true);
+	return bpf_local_storage_map_alloc(attr, &task_cache);
 }
 
 static void task_storage_map_free(struct bpf_map *map)
net/core/bpf_sk_storage.c +2 -2
index bd3c686edc0b..92b548246f04 100644
@@ -67,7 +67,7 @@ static void bpf_sk_storage_map_free(struct bpf_map *map)
 
 static struct bpf_map *bpf_sk_storage_map_alloc(union bpf_attr *attr)
 {
-	return bpf_local_storage_map_alloc(attr, &sk_cache, false);
+	return bpf_local_storage_map_alloc(attr, &sk_cache);
 }
 
 static int notsupp_get_next_key(struct bpf_map *map, void *key,
@@ -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
#3 lore raw

Re: [PATCH RFC bpf-next 2/2] bpf: Use kmalloc_nolock() in local storage unconditionally

Quoted 11 lines · Amery Hung
kernel/bpf/bpf_local_storage.c +0 -6
@@ -80,23 +80,12 @@ bpf_selem_alloc(struct bpf_local_storage_map *smap, void *owner,
        if (mem_charge(smap, owner, smap->elem_size))
                return NULL;
 
-       if (smap->bpf_ma) {
-               selem = bpf_mem_cache_alloc_flags(&smap->selem_ma, gfp_flags);
-               if (selem)
-                       /* Keep the original bpf_map_kzalloc behavior
-                        * before started using the bpf_mem_cache_alloc.
-                        *

kernel/bpf/bpf_local_storage.c bpf_selem_alloc(struct bpf_local_storage_map *smap, void *owner, · line 83

Quoted 10 lines
kernel/bpf/bpf_local_storage.c +1 -9
-                        * No need to use zero_map_value. The bpf_selem_free()
-                        * only does bpf_mem_cache_free when there is
-                        * no other bpf prog is using the selem.
-                        */
-                       memset(SDATA(selem)->data, 0, smap->map.value_size);
-       } else {
-               selem = bpf_map_kzalloc(&smap->map, smap->elem_size,
-                                       gfp_flags | __GFP_NOWARN);
-       }
+       selem = bpf_map_kmalloc_nolock(&smap->map, smap->elem_size, gfp_flags, NUMA_NO_NODE);

Pls enable CONFIG_DEBUG_VM=y then you'll see that the above triggers:
void *kmalloc_nolock_noprof(size_t size, gfp_t gfp_flags, int node)

{
        gfp_t alloc_gfp = __GFP_NOWARN | __GFP_NOMEMALLOC | gfp_flags;
...
        VM_WARN_ON_ONCE(gfp_flags & ~(__GFP_ACCOUNT | __GFP_ZERO |
                                      __GFP_NO_OBJ_EXT));

and benchmarking numbers have to be redone, since with unsupported gfp flags kmalloc_nolock() is likely doing something wrong.

author
#4 lore raw

Re: [PATCH RFC bpf-next 2/2] bpf: Use kmalloc_nolock() in local storage unconditionally

Quoted 27 lines · Alexei Starovoitov
Quoted 11 lines · Amery Hung
kernel/bpf/bpf_local_storage.c +0 -6
@@ -80,23 +80,12 @@ bpf_selem_alloc(struct bpf_local_storage_map *smap, void *owner,
        if (mem_charge(smap, owner, smap->elem_size))
                return NULL;
 
-       if (smap->bpf_ma) {
-               selem = bpf_mem_cache_alloc_flags(&smap->selem_ma, gfp_flags);
-               if (selem)
-                       /* Keep the original bpf_map_kzalloc behavior
-                        * before started using the bpf_mem_cache_alloc.
-                        *

kernel/bpf/bpf_local_storage.c bpf_selem_alloc(struct bpf_local_storage_map *smap, void *owner, · line 83

Quoted 10 lines
kernel/bpf/bpf_local_storage.c +1 -9
-                        * No need to use zero_map_value. The bpf_selem_free()
-                        * only does bpf_mem_cache_free when there is
-                        * no other bpf prog is using the selem.
-                        */
-                       memset(SDATA(selem)->data, 0, smap->map.value_size);
-       } else {
-               selem = bpf_map_kzalloc(&smap->map, smap->elem_size,
-                                       gfp_flags | __GFP_NOWARN);
-       }
+       selem = bpf_map_kmalloc_nolock(&smap->map, smap->elem_size, gfp_flags, NUMA_NO_NODE);

Pls enable CONFIG_DEBUG_VM=y then you'll see that the above triggers:
void *kmalloc_nolock_noprof(size_t size, gfp_t gfp_flags, int node)

Quoted 8 lines
{
        gfp_t alloc_gfp = __GFP_NOWARN | __GFP_NOMEMALLOC | gfp_flags;
...
        VM_WARN_ON_ONCE(gfp_flags & ~(__GFP_ACCOUNT | __GFP_ZERO |
                                      __GFP_NO_OBJ_EXT));

and benchmarking numbers have to be redone, since with unsupported gfp flags kmalloc_nolock() is likely doing something wrong.

I see. Thanks for pointing it out. Currently the verifier determines the flag and rewrites the program based on if the caller of storage_get helpers is sleepable. I will remove it and redo the benchmark.

Thanks,
Amery

#5 lore raw

Re: [PATCH RFC bpf-next 2/2] bpf: Use kmalloc_nolock() in local storage unconditionally

Quoted 36 lines · Amery Hung
Quoted 33 lines · Alexei Starovoitov
Quoted 11 lines · Amery Hung
kernel/bpf/bpf_local_storage.c +0 -6
@@ -80,23 +80,12 @@ bpf_selem_alloc(struct bpf_local_storage_map *smap, void *owner,
        if (mem_charge(smap, owner, smap->elem_size))
                return NULL;
 
-       if (smap->bpf_ma) {
-               selem = bpf_mem_cache_alloc_flags(&smap->selem_ma, gfp_flags);
-               if (selem)
-                       /* Keep the original bpf_map_kzalloc behavior
-                        * before started using the bpf_mem_cache_alloc.
-                        *

kernel/bpf/bpf_local_storage.c bpf_selem_alloc(struct bpf_local_storage_map *smap, void *owner, · line 83

Quoted 10 lines
kernel/bpf/bpf_local_storage.c +1 -9
-                        * No need to use zero_map_value. The bpf_selem_free()
-                        * only does bpf_mem_cache_free when there is
-                        * no other bpf prog is using the selem.
-                        */
-                       memset(SDATA(selem)->data, 0, smap->map.value_size);
-       } else {
-               selem = bpf_map_kzalloc(&smap->map, smap->elem_size,
-                                       gfp_flags | __GFP_NOWARN);
-       }
+       selem = bpf_map_kmalloc_nolock(&smap->map, smap->elem_size, gfp_flags, NUMA_NO_NODE);

Pls enable CONFIG_DEBUG_VM=y then you'll see that the above triggers:
void *kmalloc_nolock_noprof(size_t size, gfp_t gfp_flags, int node)

{
        gfp_t alloc_gfp = __GFP_NOWARN | __GFP_NOMEMALLOC | gfp_flags;
...
        VM_WARN_ON_ONCE(gfp_flags & ~(__GFP_ACCOUNT | __GFP_ZERO |
                                      __GFP_NO_OBJ_EXT));
Quoted 7 lines
Quoted 2 lines

and benchmarking numbers have to be redone, since with unsupported gfp flags kmalloc_nolock() is likely doing something wrong.

I see. Thanks for pointing it out. Currently the verifier determines the flag and rewrites the program based on if the caller of storage_get helpers is sleepable. I will remove it and redo the benchmark.

yes. that part of the verifier can be removed too.
First I would redo the benchmark numbers with s/gfp_flags/0 in the above line.

author
#6 lore raw

Re: [PATCH RFC bpf-next 2/2] bpf: Use kmalloc_nolock() in local storage unconditionally

Quoted 41 lines · Alexei Starovoitov
Quoted 39 lines · Amery Hung
Quoted 35 lines · Alexei Starovoitov
Quoted 11 lines · Amery Hung
kernel/bpf/bpf_local_storage.c +0 -6
@@ -80,23 +80,12 @@ bpf_selem_alloc(struct bpf_local_storage_map *smap, void *owner,
        if (mem_charge(smap, owner, smap->elem_size))
                return NULL;
 
-       if (smap->bpf_ma) {
-               selem = bpf_mem_cache_alloc_flags(&smap->selem_ma, gfp_flags);
-               if (selem)
-                       /* Keep the original bpf_map_kzalloc behavior
-                        * before started using the bpf_mem_cache_alloc.
-                        *

kernel/bpf/bpf_local_storage.c bpf_selem_alloc(struct bpf_local_storage_map *smap, void *owner, · line 83

Quoted 10 lines
kernel/bpf/bpf_local_storage.c +1 -9
-                        * No need to use zero_map_value. The bpf_selem_free()
-                        * only does bpf_mem_cache_free when there is
-                        * no other bpf prog is using the selem.
-                        */
-                       memset(SDATA(selem)->data, 0, smap->map.value_size);
-       } else {
-               selem = bpf_map_kzalloc(&smap->map, smap->elem_size,
-                                       gfp_flags | __GFP_NOWARN);
-       }
+       selem = bpf_map_kmalloc_nolock(&smap->map, smap->elem_size, gfp_flags, NUMA_NO_NODE);

Pls enable CONFIG_DEBUG_VM=y then you'll see that the above triggers:
void *kmalloc_nolock_noprof(size_t size, gfp_t gfp_flags, int node)

{
        gfp_t alloc_gfp = __GFP_NOWARN | __GFP_NOMEMALLOC | gfp_flags;
...
        VM_WARN_ON_ONCE(gfp_flags & ~(__GFP_ACCOUNT | __GFP_ZERO |
                                      __GFP_NO_OBJ_EXT));

and benchmarking numbers have to be redone, since with unsupported gfp flags kmalloc_nolock() is likely doing something wrong.

Quoted 7 lines
Quoted 4 lines

I see. Thanks for pointing it out. Currently the verifier determines the flag and rewrites the program based on if the caller of storage_get helpers is sleepable. I will remove it and redo the benchmark.

yes. that part of the verifier can be removed too.
First I would redo the benchmark numbers with s/gfp_flags/0 in the above line.

Here are the new numbers after setting gfp_flags = __GFP_ZERO and removing memset(0). BTW, the test is done on a physical machine. The numbers for sk local storage can change ±1-2. I will try to increase the test iteration or kill other unnecessary things running on the machine to see if the number fluctuates less.

Socket local storage

memory alloc     batch  creation speed              creation speed diff
---------------  ----   ------------------                         ----
kzalloc           16    104.217 ± 0.974k/s  4.15 kmallocs/create
(before)          32    104.355 ± 0.606k/s  4.13 kmallocs/create
                  64    103.611 ± 0.707k/s  4.15 kmallocs/create
kmalloc_nolock    16    100.402 ± 1.282k/s  1.11 kmallocs/create  -3.7%
(after)           32    101.592 ± 0.861k/s  1.07 kmallocs/create  -2.6%
                  64     98.995 ± 0.868k/s  1.07 kmallocs/create  -4.6%

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.659 ± 0.108k/s  2.51 kmallocs/create  +4.0%
(after)           32     23.723 ± 0.082k/s  2.54 kmallocs/create  +3.6%
                  64     23.359 ± 0.236k/s  2.48 kmallocs/create  +3.5%
#7 lore raw

Re: [PATCH RFC bpf-next 2/2] bpf: Use kmalloc_nolock() in local storage unconditionally

Quoted 49 lines · Amery Hung
Quoted 46 lines · Alexei Starovoitov
Quoted 43 lines · Amery Hung
Quoted 27 lines · Alexei Starovoitov
Quoted 11 lines · Amery Hung
kernel/bpf/bpf_local_storage.c +0 -6
@@ -80,23 +80,12 @@ bpf_selem_alloc(struct bpf_local_storage_map *smap, void *owner,
        if (mem_charge(smap, owner, smap->elem_size))
                return NULL;
 
-       if (smap->bpf_ma) {
-               selem = bpf_mem_cache_alloc_flags(&smap->selem_ma, gfp_flags);
-               if (selem)
-                       /* Keep the original bpf_map_kzalloc behavior
-                        * before started using the bpf_mem_cache_alloc.
-                        *

kernel/bpf/bpf_local_storage.c bpf_selem_alloc(struct bpf_local_storage_map *smap, void *owner, · line 83

Quoted 10 lines
kernel/bpf/bpf_local_storage.c +1 -9
-                        * No need to use zero_map_value. The bpf_selem_free()
-                        * only does bpf_mem_cache_free when there is
-                        * no other bpf prog is using the selem.
-                        */
-                       memset(SDATA(selem)->data, 0, smap->map.value_size);
-       } else {
-               selem = bpf_map_kzalloc(&smap->map, smap->elem_size,
-                                       gfp_flags | __GFP_NOWARN);
-       }
+       selem = bpf_map_kmalloc_nolock(&smap->map, smap->elem_size, gfp_flags, NUMA_NO_NODE);

Pls enable CONFIG_DEBUG_VM=y then you'll see that the above triggers:
void *kmalloc_nolock_noprof(size_t size, gfp_t gfp_flags, int node)

Quoted 8 lines
{
        gfp_t alloc_gfp = __GFP_NOWARN | __GFP_NOMEMALLOC | gfp_flags;
...
        VM_WARN_ON_ONCE(gfp_flags & ~(__GFP_ACCOUNT | __GFP_ZERO |
                                      __GFP_NO_OBJ_EXT));

and benchmarking numbers have to be redone, since with unsupported gfp flags kmalloc_nolock() is likely doing something wrong.

I see. Thanks for pointing it out. Currently the verifier determines the flag and rewrites the program based on if the caller of storage_get helpers is sleepable. I will remove it and redo the benchmark.

Quoted 8 lines
Quoted 2 lines

yes. that part of the verifier can be removed too.
First I would redo the benchmark numbers with s/gfp_flags/0 in the above line.

Here are the new numbers after setting gfp_flags = __GFP_ZERO and removing memset(0). BTW, the test is done on a physical machine. The numbers for sk local storage can change ±1-2. I will try to increase the test iteration or kill other unnecessary things running on the machine to see if the number fluctuates less.

fyi gfp_zero is pretty much the same memset().
Shouldn't be any difference between __GFP_ZERO vs manual memset() after alloc.

Quoted 10 lines

Socket local storage

memory alloc     batch  creation speed              creation speed diff
---------------  ----   ------------------                         ----
kzalloc           16    104.217 ± 0.974k/s  4.15 kmallocs/create
(before)          32    104.355 ± 0.606k/s  4.13 kmallocs/create
                  64    103.611 ± 0.707k/s  4.15 kmallocs/create
kmalloc_nolock    16    100.402 ± 1.282k/s  1.11 kmallocs/create  -3.7%
(after)           32    101.592 ± 0.861k/s  1.07 kmallocs/create  -2.6%
                  64     98.995 ± 0.868k/s  1.07 kmallocs/create  -4.6%

could you perf record/report both to see where the difference comes from? The only reason I could explain the difference is that kfree_nolock() is hitting defer_free() case, but that shouldn't happen
in this microbenchmark. But if you see free_deferred_objects() in perf report that would explain it.

author
#8 lore raw

Re: [PATCH RFC bpf-next 2/2] bpf: Use kmalloc_nolock() in local storage unconditionally

Quoted 74 lines · Alexei Starovoitov
Quoted 49 lines · Amery Hung
Quoted 46 lines · Alexei Starovoitov
Quoted 43 lines · Amery Hung
Quoted 27 lines · Alexei Starovoitov
Quoted 11 lines · Amery Hung
kernel/bpf/bpf_local_storage.c +0 -6
@@ -80,23 +80,12 @@ bpf_selem_alloc(struct bpf_local_storage_map *smap, void *owner,
        if (mem_charge(smap, owner, smap->elem_size))
                return NULL;
 
-       if (smap->bpf_ma) {
-               selem = bpf_mem_cache_alloc_flags(&smap->selem_ma, gfp_flags);
-               if (selem)
-                       /* Keep the original bpf_map_kzalloc behavior
-                        * before started using the bpf_mem_cache_alloc.
-                        *

kernel/bpf/bpf_local_storage.c bpf_selem_alloc(struct bpf_local_storage_map *smap, void *owner, · line 83

Quoted 10 lines
kernel/bpf/bpf_local_storage.c +1 -9
-                        * No need to use zero_map_value. The bpf_selem_free()
-                        * only does bpf_mem_cache_free when there is
-                        * no other bpf prog is using the selem.
-                        */
-                       memset(SDATA(selem)->data, 0, smap->map.value_size);
-       } else {
-               selem = bpf_map_kzalloc(&smap->map, smap->elem_size,
-                                       gfp_flags | __GFP_NOWARN);
-       }
+       selem = bpf_map_kmalloc_nolock(&smap->map, smap->elem_size, gfp_flags, NUMA_NO_NODE);

Pls enable CONFIG_DEBUG_VM=y then you'll see that the above triggers:
void *kmalloc_nolock_noprof(size_t size, gfp_t gfp_flags, int node)

Quoted 8 lines
{
        gfp_t alloc_gfp = __GFP_NOWARN | __GFP_NOMEMALLOC | gfp_flags;
...
        VM_WARN_ON_ONCE(gfp_flags & ~(__GFP_ACCOUNT | __GFP_ZERO |
                                      __GFP_NO_OBJ_EXT));

and benchmarking numbers have to be redone, since with unsupported gfp flags kmalloc_nolock() is likely doing something wrong.

I see. Thanks for pointing it out. Currently the verifier determines the flag and rewrites the program based on if the caller of storage_get helpers is sleepable. I will remove it and redo the benchmark.

Quoted 8 lines
Quoted 2 lines

yes. that part of the verifier can be removed too.
First I would redo the benchmark numbers with s/gfp_flags/0 in the above line.

Here are the new numbers after setting gfp_flags = __GFP_ZERO and removing memset(0). BTW, the test is done on a physical machine. The numbers for sk local storage can change ±1-2. I will try to increase the test iteration or kill other unnecessary things running on the machine to see if the number fluctuates less.

fyi gfp_zero is pretty much the same memset().
Shouldn't be any difference between __GFP_ZERO vs manual memset() after alloc.

Quoted 10 lines

Socket local storage

memory alloc     batch  creation speed              creation speed diff
---------------  ----   ------------------                         ----
kzalloc           16    104.217 ± 0.974k/s  4.15 kmallocs/create
(before)          32    104.355 ± 0.606k/s  4.13 kmallocs/create
                  64    103.611 ± 0.707k/s  4.15 kmallocs/create
kmalloc_nolock    16    100.402 ± 1.282k/s  1.11 kmallocs/create  -3.7%
(after)           32    101.592 ± 0.861k/s  1.07 kmallocs/create  -2.6%
                  64     98.995 ± 0.868k/s  1.07 kmallocs/create  -4.6%
Quoted 5 lines

could you perf record/report both to see where the difference comes from? The only reason I could explain the difference is that kfree_nolock() is hitting defer_free() case, but that shouldn't happen
in this microbenchmark. But if you see free_deferred_objects() in perf report that would explain it.

Updated numbers and next step:

A new perf result shows ~10% creation speed decrease (130k/s vs 142k/s) for socket local storage after stopping a service that is also creating it for every connection.

The performance hit did come from defer_free() and the loss of batching with kfree_rcu().

Hence, kmalloc_nolock() is not ready in this case to replace kzalloc(). The next respin will only replace BPF memory allocator in local storage with kmalloc_nolock().