Skip to contentSkip to conversation

bpf: Fix possible memleak when updating hash maps

PATCH Superseded Leon Hwang 1 participant 4 messages Oct 16, 2025

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Leon Hwang

author
#0 lore raw

cover bpf: Fix possible memleak when updating hash maps

In the discussion thread
"[PATCH bpf-next v9 0/7] bpf: Introduce BPF_F_CPU and BPF_F_ALL_CPUS flags for percpu maps"[1], it was pointed out that missing calls to bpf_obj_free_fields() could lead to memory leaks.

A selftest was added to confirm that this is indeed a real issue - the memory referenced by BPF_KPTR_{REF,PERCPU} fields is not freed when bpf_obj_free_fields() is missing after copy_map_value[,_long]().

Further inspection of copy_map_value[,_long]() call sites revealed two locations affected by this issue:

1. pcpu_copy_value()
2. htab_map_update_elem() when used with BPF_F_LOCK

This series fixes the leaks by properly calling bpf_obj_free_fields() (or check_and_free_fields()) after copy_map_value[,_long]() and adds two selftests to verify the fix.

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

Leon Hwang (3):
bpf: Fix possible memleak in [lru_,]percpu_hash map update
bpf: Fix possible memleak when updating hash maps with BPF_F_LOCK selftests/bpf: Add test to verify no memleak when updating hash maps

 kernel/bpf/hashtab.c                          |   4 +
 .../bpf/prog_tests/refcounted_kptr.c          |  93 ++++++++++++++++
 .../selftests/bpf/progs/refcounted_kptr.c     | 101 ++++++++++++++++++
 3 files changed, 198 insertions(+)

--
2.51.0

author
#1 lore raw

PATCH 1/3 bpf: Fix possible memleak in [lru_,]percpu_hash map update

As [lru_,]percpu_hash maps support BPF_KPTR_{REF,PERCPU}, missing calls to 'bpf_obj_free_fields()' in 'pcpu_copy_value()' can leak memory referenced by BPF_KPTR_{REF,PERCPU} fields.

Fix this by calling 'bpf_obj_free_fields()' after 'copy_map_value[,_long]()' in 'pcpu_copy_value()'.

Fixes 65334e64a493 ("bpf: Support kptrs in percpu hashmap and percpu LRU hashmap")
---
 kernel/bpf/hashtab.c | 2 ++
 1 file changed, 2 insertions(+)
kernel/bpf/hashtab.c +2 -0
index c2fcd0cd51e51..26308adc9ccb3 100644
@@ -950,12 +950,14 @@ static void pcpu_copy_value(struct bpf_htab *htab, void __percpu *pptr,
 	if (!onallcpus) {
 		/* copy true value_size bytes */
 		copy_map_value(&htab->map, this_cpu_ptr(pptr), value);
+		bpf_obj_free_fields(htab->map.record, this_cpu_ptr(pptr));
 	} else {
 		u32 size = round_up(htab->map.value_size, 8);
 		int off = 0, cpu;
 
 		for_each_possible_cpu(cpu) {
 			copy_map_value_long(&htab->map, per_cpu_ptr(pptr, cpu), value + off);
+			bpf_obj_free_fields(htab->map.record, per_cpu_ptr(pptr, cpu));
 			off += size;
 		}
 	}
Signature · 1 line
2.51.0
author
#2 lore raw

PATCH 2/3 bpf: Fix possible memleak when updating hash maps with BPF_F_LOCK

In reply to Leon Hwang

When updating hash maps with BPF_F_LOCK, the special fields were not freed after being replaced. This could cause memory referenced by BPF_KPTR_{REF,PERCPU} fields to leak.

Fix this by calling 'check_and_free_fields()' after 'copy_map_value_locked()' to properly release the old fields.

Fixes 14a324f6a67e ("bpf: Wire up freeing of referenced kptr")
---
 kernel/bpf/hashtab.c | 2 ++
 1 file changed, 2 insertions(+)
kernel/bpf/hashtab.c +2 -0
index 26308adc9ccb3..65009ea3e9379 100644
@@ -1124,6 +1124,7 @@ static long htab_map_update_elem(struct bpf_map *map, void *key, void *value,
 			copy_map_value_locked(map,
 					      htab_elem_value(l_old, key_size),
 					      value, false);
+			check_and_free_fields(htab, l_old);
 			return 0;
 		}
 		/* fall through, grab the bucket lock and lookup again.
@@ -1152,6 +1153,7 @@ static long htab_map_update_elem(struct bpf_map *map, void *key, void *value,
 		copy_map_value_locked(map,
 				      htab_elem_value(l_old, key_size),
 				      value, false);
+		check_and_free_fields(htab, l_old);
 		ret = 0;
 		goto err;
 	}
Signature · 1 line
2.51.0
author
#3 lore raw

PATCH 3/3 selftests/bpf: Add test to verify no memleak when updating hash maps

In reply to Leon Hwang

Add two tests to verify that updating hash maps does not leak memory when BPF_KPTR_REF objects are involved.

The test performs the following steps:

1. Call update_elem() to insert an initial value.
2. Use bpf_refcount_acquire() to increment the refcount.
3. Store the node pointer in the map value.
4. Add the node to a linked list.
5. Probe-read the refcount and verify it is *2*.
6. Call update_elem() again to trigger refcount decrement.
7. Verify that the field has been reset.

---
 .../bpf/prog_tests/refcounted_kptr.c          |  93 ++++++++++++++++
 .../selftests/bpf/progs/refcounted_kptr.c     | 101 ++++++++++++++++++
 2 files changed, 194 insertions(+)
tools/testing/selftests/bpf/prog_tests/refcounted_kptr.c +93 -0
index d6bd5e16e6372..9dc9a425cc65d 100644
@@ -44,3 +44,96 @@ void test_refcounted_kptr_wrong_owner(void)
 	ASSERT_OK(opts.retval, "rbtree_wrong_owner_remove_fail_a2 retval");
 	refcounted_kptr__destroy(skel);
 }
+
+static void test_refcnt_leak(void *values, size_t values_sz, u64 flags, bool lock_hash)
+{
+	struct refcounted_kptr *skel;
+	int ret, fd, key = 0;
+	struct bpf_map *map;
+	LIBBPF_OPTS(bpf_test_run_opts, opts,
+		    .data_in = &pkt_v4,
+		    .data_size_in = sizeof(pkt_v4),
+		    .repeat = 1,
+	);
+
+	skel = refcounted_kptr__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "refcounted_kptr__open_and_load"))
+		return;
+
+	map = skel->maps.pcpu_hash;
+	if (lock_hash)
+		map = skel->maps.lock_hash;
+
+	ret = bpf_map__update_elem(map, &key, sizeof(key), values, values_sz, flags);
+	if (!ASSERT_OK(ret, "bpf_map__update_elem first"))
+		goto out;
+
+	fd = bpf_program__fd(skel->progs.pcpu_hash_refcount_leak);
+	if (lock_hash)
+		fd = bpf_program__fd(skel->progs.hash_lock_refcount_leak);
+
+	ret = bpf_prog_test_run_opts(fd, &opts);
+	if (!ASSERT_OK(ret, "test_run_opts"))
+		goto out;
+	if (!ASSERT_EQ(opts.retval, 2, "retval refcount"))
+		goto out;
+
+	ret = bpf_map__update_elem(map, &key, sizeof(key), values, values_sz, flags);
+	if (!ASSERT_OK(ret, "bpf_map__update_elem second"))
+		goto out;
+
+	fd = bpf_program__fd(skel->progs.check_pcpu_hash_refcount);
+	if (lock_hash)
+		fd = bpf_program__fd(skel->progs.check_hash_lock_refcount);
+
+	ret = bpf_prog_test_run_opts(fd, &opts);
+	if (!ASSERT_OK(ret, "test_run_opts"))
+		goto out;
+	if (!ASSERT_EQ(opts.retval, 1, "retval"))
+		goto out;
+
+out:
+	refcounted_kptr__destroy(skel);
+}
+
+static void test_percpu_hash_refcount_leak(void)
+{
+	size_t values_sz;
+	u64 *values;
+	int cpu_nr;
+
+	cpu_nr = libbpf_num_possible_cpus();
+	if (!ASSERT_GT(cpu_nr, 0, "libbpf_num_possible_cpus"))
+		return;
+
+	values = calloc(cpu_nr, sizeof(u64));
+	if (!ASSERT_OK_PTR(values, "calloc values"))
+		return;
+
+	values_sz = cpu_nr * sizeof(u64);
+	memset(values, 0, values_sz);
+
+	test_refcnt_leak(values, values_sz, 0, false);
+
+	free(values);
+}
+
+struct hash_lock_value {
+	struct bpf_spin_lock lock;
+	u64 node;
+};
+
+static void test_hash_lock_refcount_leak(void)
+{
+	struct hash_lock_value value = {};
+
+	test_refcnt_leak(&value, sizeof(value), BPF_F_LOCK, true);
+}
+
+void test_refcount_leak(void)
+{
+	if (test__start_subtest("percpu_hash_refcount_leak"))
+		test_percpu_hash_refcount_leak();
+	if (test__start_subtest("hash_lock_refcount_leak"))
+		test_hash_lock_refcount_leak();
+}
tools/testing/selftests/bpf/progs/refcounted_kptr.c +101 -0

105 lines not on this page · raw message

Signature · 1 line
2.51.0