Skip to contentSkip to conversation

bpf: Add warning to detect memory leak in bpf_selem_unlink_nofail()

PATCH Applied Amery Hung 3 participants 3 messages Mar 18 – Mar 19 BPF

lore
Share & tools
lore

Amery Hung

author
#0 lore raw

PATCH 1/1 bpf: Add warning to detect memory leak in bpf_selem_unlink_nofail()

While very unlikely, local storage theoretically may leak memory of the size of "struct bpf_local_storage" when destroy() fails to grab local_storage->lock and initializes selem->local_storage before other racing map_free() see it. Warn the user to allow debugging the issue instead of leaking the memory silently.

Note that test_maps in bpf selftests already stress tested bpf_selem_unlink_nofail() by creating 4096 sockets and then immediately destroying them in multiple threads. With 64 threads, 64 x 4096 socket local storages were created and destroyed during the test and no warning in the function were triggered.

---
 kernel/bpf/bpf_local_storage.c | 8 ++++++++
 1 file changed, 8 insertions(+)
kernel/bpf/bpf_local_storage.c +8 -0
index 9c96a4477f81..69bb2a7a71bc 100644
@@ -494,6 +494,14 @@ static void bpf_selem_unlink_nofail(struct bpf_local_storage_elem *selem,
 			}
 			raw_res_spin_unlock_irqrestore(&local_storage->lock, flags);
 		}
+		/*
+		 * Highly unlikely scenario: memory leak
+		 *
+		 * When destroy() fails to acqurire local_storage->lock and initializes
+		 * selem->local_storage to NULL before any racing map_free() sees the same
+		 * selem, no one will free the local storage.
+		 */
+		WARN_ON_ONCE(err && !in_map_free);
 		if (!err || !in_map_free)
 			RCU_INIT_POINTER(selem->local_storage, NULL);
 	}
Signature · 1 line
2.52.0
maintainer Acked-by on v1 1/1
#1 lore raw
Quoted 7 lines · Amery Hung

While very unlikely, local storage theoretically may leak memory of the size of "struct bpf_local_storage" when destroy() fails to grab local_storage->lock and initializes selem->local_storage before other racing map_free() see it. Warn the user to allow debugging the issue instead of leaking the memory silently.

Quoted 8 lines

Note that test_maps in bpf selftests already stress tested bpf_selem_unlink_nofail() by creating 4096 sockets and then immediately destroying them in multiple threads. With 64 threads, 64 x 4096 socket local storages were created and destroyed during the test and no warning in the function were triggered.

---

Quoted 25 lines
 kernel/bpf/bpf_local_storage.c | 8 ++++++++
 1 file changed, 8 insertions(+)
kernel/bpf/bpf_local_storage.c +8 -1
index 9c96a4477f81..69bb2a7a71bc 100644
@@ -494,6 +494,14 @@ static void bpf_selem_unlink_nofail(struct bpf_local_storage_elem *selem,
                        }
                        raw_res_spin_unlock_irqrestore(&local_storage->lock, flags);
                }
+               /*
+                * Highly unlikely scenario: memory leak
+                *
+                * When destroy() fails to acqurire local_storage->lock and initializes
+                * selem->local_storage to NULL before any racing map_free() sees the same
+                * selem, no one will free the local storage.
+                */
+               WARN_ON_ONCE(err && !in_map_free);
                if (!err || !in_map_free)
                        RCU_INIT_POINTER(selem->local_storage, NULL);
        }
--

2.52.0

#2 lore raw
In reply to Amery Hung
This patch was applied to bpf/bpf-next.git (master) by Martin KaFai Lau <[email protected]>:

Hello:

This patch was applied to bpf/bpf-next.git (master) by Martin KaFai Lau <[email protected]>:

Quoted 6 lines · On Wed, 18 Mar 2026 15:42:19 -0700 you wrote:

While very unlikely, local storage theoretically may leak memory of the size of "struct bpf_local_storage" when destroy() fails to grab local_storage->lock and initializes selem->local_storage before other racing map_free() see it. Warn the user to allow debugging the issue instead of leaking the memory silently.

Quoted 7 lines

Note that test_maps in bpf selftests already stress tested bpf_selem_unlink_nofail() by creating 4096 sockets and then immediately destroying them in multiple threads. With 64 threads, 64 x 4096 socket local storages were created and destroyed during the test and no warning in the function were triggered.

[...]

Here is the summary with links:
- [bpf-next,v1,1/1] bpf: Add warning to detect memory leak in bpf_selem_unlink_nofail()

    https://git.kernel.org/bpf/bpf-next/c/4b21ea502483

You are awesome, thank you!

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