bpf: Fixes for bpf_sk_storage
Subsystems (5)
Martin KaFai Lau
reviewercover bpf: Fixes for bpf_sk_storage
From: Martin KaFai Lau <[email protected]>
This set has two fixes for bpf_sk_storage. Please see the individual patch for details.
Martin KaFai Lau (3):
bpf: bpf_sk_storage: Fix invalid wait context lockdep report
bpf: bpf_sk_storage: Fix the missing uncharge in sk_omem_alloc selftests/bpf: Check bpf_sk_storage has uncharged sk_omem_alloc
kernel/bpf/bpf_local_storage.c | 49 +++++---------- .../bpf/prog_tests/sk_storage_omem_uncharge.c | 56 +++++++++++++++++ .../selftests/bpf/progs/bpf_tracing_net.h | 1 + .../bpf/progs/sk_storage_omem_uncharge.c | 61 +++++++++++++++++++ 4 files changed, 133 insertions(+), 34 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/sk_storage_omem_uncharge.c create mode 100644 tools/testing/selftests/bpf/progs/sk_storage_omem_uncharge.c
Signature · 1 line
2.34.1
Martin KaFai Lau
reviewerPATCH 1/3 bpf: bpf_sk_storage: Fix invalid wait context lockdep report
From: Martin KaFai Lau <[email protected]>
'./test_progs -t test_local_storage' reported a splat:
[ 27.137569] =============================
[ 27.138122] [ BUG: Invalid wait context ]
[ 27.138650] 6.5.0-03980-gd11ae1b16b0a #247 Tainted: G O
[ 27.139542] -----------------------------
[ 27.140106] test_progs/1729 is trying to lock:
[ 27.140713] ffff8883ef047b88 (stock_lock){-.-.}-{3:3}, at: local_lock_acquire+0x9/0x130
[ 27.141834] other info that might help us debug this:
[ 27.142437] context-{5:5}
[ 27.142856] 2 locks held by test_progs/1729:
[ 27.143352] #0: ffffffff84bcd9c0 (rcu_read_lock){....}-{1:3}, at: rcu_lock_acquire+0x4/0x40
[ 27.144492] #1: ffff888107deb2c0 (&storage->lock){..-.}-{2:2}, at: bpf_local_storage_update+0x39e/0x8e0
[ 27.145855] stack backtrace:
[ 27.146274] CPU: 0 PID: 1729 Comm: test_progs Tainted: G O 6.5.0-03980-gd11ae1b16b0a #247
[ 27.147550] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS rel-1.14.0-0-g155821a1990b-prebuilt.qemu.org 04/01/2014
[ 27.149127] Call Trace:
[ 27.149490] <TASK>
[ 27.149867] dump_stack_lvl+0x130/0x1d0
[ 27.152609] dump_stack+0x14/0x20
[ 27.153131] __lock_acquire+0x1657/0x2220
[ 27.153677] lock_acquire+0x1b8/0x510
[ 27.157908] local_lock_acquire+0x29/0x130
[ 27.159048] obj_cgroup_charge+0xf4/0x3c0
[ 27.160794] slab_pre_alloc_hook+0x28e/0x2b0
[ 27.161931] __kmem_cache_alloc_node+0x51/0x210
[ 27.163557] __kmalloc+0xaa/0x210
[ 27.164593] bpf_map_kzalloc+0xbc/0x170
[ 27.165147] bpf_selem_alloc+0x130/0x510
[ 27.166295] bpf_local_storage_update+0x5aa/0x8e0
[ 27.167042] bpf_fd_sk_storage_update_elem+0xdb/0x1a0
[ 27.169199] bpf_map_update_value+0x415/0x4f0
[ 27.169871] map_update_elem+0x413/0x550
[ 27.170330] __sys_bpf+0x5e9/0x640
[ 27.174065] __x64_sys_bpf+0x80/0x90
[ 27.174568] do_syscall_64+0x48/0xa0
[ 27.175201] entry_SYSCALL_64_after_hwframe+0x6e/0xd8
[ 27.175932] RIP: 0033:0x7effb40e41ad
[ 27.176357] Code: ff c3 66 2e 0f 1f 84 00 00 00 00 00 90 f3 0f 1e fa 48 89 f8 48 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 8b 0d8
[ 27.179028] RSP: 002b:00007ffe64c21fc8 EFLAGS: 00000202 ORIG_RAX: 0000000000000141
[ 27.180088] RAX: ffffffffffffffda RBX: 00007ffe64c22768 RCX: 00007effb40e41ad
[ 27.181082] RDX: 0000000000000020 RSI: 00007ffe64c22008 RDI: 0000000000000002
[ 27.182030] RBP: 00007ffe64c21ff0 R08: 0000000000000000 R09: 00007ffe64c22788
[ 27.183038] R10: 0000000000000064 R11: 0000000000000202 R12: 0000000000000000
[ 27.184006] R13: 00007ffe64c22788 R14: 00007effb42a1000 R15: 0000000000000000
[ 27.184958] </TASK>It complains about acquiring a local_lock while holding a raw_spin_lock. It means it should not allocate memory while holding a raw_spin_lock since it is not safe for RT.
raw_spin_lock is needed because bpf_local_storage supports tracing context. In particular for task local storage, it is easy to get a "current" task PTR_TO_BTF_ID in tracing bpf prog. However, task (and cgroup) local storage has already been moved to bpf mem allocator which can be used after raw_spin_lock.
The splat is for the sk storage. For sk (and inode) storage, it has not been moved to bpf mem allocator. Using raw_spin_lock or not, kzalloc(GFP_ATOMIC) could theoretically be unsafe in tracing context. However, the local storage helper requires a verifier accepted sk pointer (PTR_TO_BTF_ID), it is hypothetical if that (mean running a bpf prog in a kzalloc unsafe context and also able to hold a verifier accepted sk pointer) could happen.
This patch avoids kzalloc after raw_spin_lock to silent the splat. There is an existing kzalloc before the raw_spin_lock. At that point, a kzalloc is very likely required because a lookup has just been done before. Thus, this patch always does the kzalloc before acquiring the raw_spin_lock and remove the later kzalloc usage after the raw_spin_lock. After this change, it will have a charge and then uncharge during the syscall bpf_map_update_elem() code path. This patch opts for simplicity and not continue the old optimization to save one charge and uncharge.
This issue is dated back to the very first commit of bpf_sk_storage which had been refactored multiple times to create task, inode, and cgroup storage. This patch uses a Fixes tag with a more recent commit that should be easier to do backport.
--- kernel/bpf/bpf_local_storage.c | 47 ++++++++++------------------------ 1 file changed, 14 insertions(+), 33 deletions(-)
kernel/bpf/bpf_local_storage.c +14 -33
@@ -553,7 +553,7 @@ bpf_local_storage_update(void *owner, struct bpf_local_storage_map *smap, void *value, u64 map_flags, gfp_t gfp_flags) { struct bpf_local_storage_data *old_sdata = NULL; -struct bpf_local_storage_elem *selem =NULL ; +struct bpf_local_storage_elem *alloc_selem, *selem =NULL ;struct bpf_local_storage *local_storage;unsigned long flags;int err;
@@ -607,11 +607,12 @@ bpf_local_storage_update(void *owner, struct bpf_local_storage_map *smap, } } -if (gfp_flags ==GFP_KERNEL ) { - selem =bpf_selem_alloc (smap, owner, value, true, gfp_flags); -if (!selem) -return ERR_PTR (-ENOMEM ); - } +/* 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, gfp_flags); +if (!alloc_selem) +return ERR_PTR (-ENOMEM );raw_spin_lock_irqsave (&local_storage->lock, flags);
@@ -623,13 +624,13 @@ bpf_local_storage_update(void *owner, struct bpf_local_storage_map *smap,* simple. */ err = -EAGAIN ; - goto unlock_err; + goto unlock; } old_sdata =bpf_local_storage_lookup (local_storage, smap, false); err =check_flags (old_sdata, map_flags);if (err) - goto unlock_err; + goto unlock;if (old_sdata && (map_flags &BPF_F_LOCK )) { copy_map_value_locked(&smap->map, old_sdata->data, value,
@@ -638,23 +639,7 @@ bpf_local_storage_update(void *owner, struct bpf_local_storage_map *smap, goto unlock; } -if (gfp_flags !=GFP_KERNEL ) { -/* local_storage->lock is held. Hence, we are sure -* we can unlink and uncharge the old_sdata successfully -* later. Hence, instead of charging the new selem now -* and then uncharge the old selem later (which may cause -* a potential but unnecessary charge failure), avoid taking -* a charge at all here (the "!old_sdata" check) and the -* old_sdata will not be uncharged later during -* bpf_selem_unlink_storage_nolock(). -*/ - selem =bpf_selem_alloc (smap, owner, value, !old_sdata, gfp_flags); -if (!selem) { - err = -ENOMEM ; - goto unlock_err; - } - } - + alloc_selem =NULL ;/* First, link the new selem to the map */ bpf_selem_link_map (smap, selem);
@@ -665,20 +650,16 @@ 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), - false, false); + true, false); } unlock:raw_spin_unlock_irqrestore (&local_storage->lock, flags); -return SDATA (selem); - -unlock_err: -raw_spin_unlock_irqrestore (&local_storage->lock, flags); -if (selem) { +if (alloc_selem) {mem_uncharge (smap, owner, smap->elem_size); -bpf_selem_free (selem, smap, true); +bpf_selem_free (alloc_selem, smap, true); } -return ERR_PTR (err); +return err ?ERR_PTR (err) :SDATA (selem); }static u16 bpf_local_storage_cache_idx_get (struct bpf_local_storage_cache *cache)
Signature · 1 line
2.34.1
Martin KaFai Lau
reviewerPATCH 2/3 bpf: bpf_sk_storage: Fix the missing uncharge in sk_omem_alloc
In reply to Martin KaFai LauFrom: Martin KaFai Lau <[email protected]>
The commit c83597fa5dc6 ("bpf: Refactor some inode/task/sk storage functions for reuse"), refactored the bpf_{sk,task,inode}_storage_free() into bpf_local_storage_unlink_nolock() which then later renamed
to bpf_local_storage_destroy(). The commit accidentally passed the
"bool uncharge_mem = false" argument to bpf_selem_unlink_storage_nolock() which then stopped the uncharge from happening to the sk->sk_omem_alloc.
This missing uncharge only happens when the sk is going away (during __sk_destruct).
This patch fixes it by always passing "uncharge_mem = true". It is a noop to the task/inode/cgroup storage because they do not have the map_local_storage_(un)charge enabled in the map_ops. A followup patch will be done in bpf-next to remove the uncharge_mem argument.
A selftest is added in the next patch.
--- kernel/bpf/bpf_local_storage.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/bpf/bpf_local_storage.c +1 -1
@@ -760,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, false, true); + local_storage, selem, true, true); }raw_spin_unlock_irqrestore (&local_storage->lock, flags);
Signature · 1 line
2.34.1
Martin KaFai Lau
reviewerPATCH 3/3 selftests/bpf: Check bpf_sk_storage has uncharged sk_omem_alloc
In reply to Martin KaFai LauFrom: Martin KaFai Lau <[email protected]>
This patch checks the sk_omem_alloc has been uncharged by bpf_sk_storage during the __sk_destruct.
--- .../bpf/prog_tests/sk_storage_omem_uncharge.c | 56 +++++++++++++++++ .../selftests/bpf/progs/bpf_tracing_net.h | 1 + .../bpf/progs/sk_storage_omem_uncharge.c | 61 +++++++++++++++++++ 3 files changed, 118 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/sk_storage_omem_uncharge.c create mode 100644 tools/testing/selftests/bpf/progs/sk_storage_omem_uncharge.c
tools/testing/selftests/bpf/prog_tests/sk_storage_omem_uncharge.c new file +56 -0
@@ -0,0 +1,56 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2023 Facebook */ +#include <test_progs.h> +#include <bpf/libbpf.h> +#include <sys/types.h> +#include <sys/socket.h> +#include "sk_storage_omem_uncharge.skel.h" + +void test_sk_storage_omem_uncharge (void ) +{ +struct sk_storage_omem_uncharge *skel; +int sk_fd =-1 , map_fd, err, value; +socklen_t optlen; + + skel =sk_storage_omem_uncharge__open_and_load (); +if (!ASSERT_OK_PTR (skel,"skel open_and_load" )) +return ; + map_fd =bpf_map__fd (skel->maps.sk_storage); + +/* A standalone socket not binding to addr:port, +* so nentns is not needed. +*/ + sk_fd =socket (AF_INET6 ,SOCK_STREAM ,0 ); +if (!ASSERT_GE (sk_fd,0 ,"socket" )) + goto done; + + optlen =sizeof (skel->bss->cookie); + err =getsockopt (sk_fd,SOL_SOCKET ,SO_COOKIE , &skel->bss->cookie, &optlen); +if (!ASSERT_OK (err,"getsockopt(SO_COOKIE)" )) + goto done; + + value =0 ; + err =bpf_map_update_elem (map_fd, &sk_fd, &value,0 ); +if (!ASSERT_OK (err,"bpf_map_update_elem(value=0)" )) + goto done; + + value =0xdeadbeef ; + err =bpf_map_update_elem (map_fd, &sk_fd, &value,0 ); +if (!ASSERT_OK (err,"bpf_map_update_elem(value=0xdeadbeef)" )) + goto done; + + err =sk_storage_omem_uncharge__attach (skel); +if (!ASSERT_OK (err,"attach" )) + goto done; + +close (sk_fd); + sk_fd =-1 ; + +ASSERT_EQ (skel->bss->cookie_found,2 ,"cookie_found" ); +ASSERT_EQ (skel->bss->omem,0 ,"omem" ); + +done: +sk_storage_omem_uncharge__destroy (skel); +if (sk_fd !=-1 ) +close (sk_fd); +}
tools/testing/selftests/bpf/progs/bpf_tracing_net.h +1 -0
@@ -88,6 +88,7 @@#define sk_v6_rcv_saddr __sk_common.skc_v6_rcv_saddr#define sk_flags __sk_common.skc_flags#define sk_reuse __sk_common.skc_reuse +#define sk_cookie __sk_common.skc_cookie#define s6_addr32 in6_u.u6_addr32
tools/testing/selftests/bpf/progs/sk_storage_omem_uncharge.c new file +61 -0
61 lines not on this page · raw message
Signature · 1 line
2.34.1
This series was applied to bpf/bpf.git (master) by Daniel Borkmann <[email protected]>:
Hello:
This series was applied to bpf/bpf.git (master)
by Daniel Borkmann <[email protected]>:
Quoted 11 lines · On Fri, 1 Sep 2023 16:11:26 -0700 you wrote:
From: Martin KaFai Lau <[email protected]>
This set has two fixes for
bpf_sk_storage. Please see the individual patch for details.Martin KaFai Lau (3):
bpf:bpf_sk_storage: Fix invalid wait context lockdep report
bpf:bpf_sk_storage: Fix the missing uncharge insk_omem_allocselftests/bpf: Checkbpf_sk_storagehas unchargedsk_omem_alloc[...]
Here is the summary with links:
- [bpf,1/3] bpf: bpf_sk_storage: Fix invalid wait context lockdep report
https://git.kernel.org/bpf/bpf/c/a96a44aba556 - [bpf,2/3] bpf: bpf_sk_storage: Fix the missing uncharge in sk_omem_alloc https://git.kernel.org/bpf/bpf/c/55d49f750b1c - [bpf,3/3] selftests/bpf: Check bpf_sk_storage has uncharged sk_omem_alloc https://git.kernel.org/bpf/bpf/c/a96d1cfb2da0
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html