bpf, sockmap: use READ_ONCE/WRITE_ONCE for saved_data_ready in the verdict path
Subsystems (5)
PATCH 1/1 bpf, sockmap: use READ_ONCE/WRITE_ONCE for saved_data_ready in the verdict path
sk_psock_start_verdict() and sk_psock_stop_verdict() access psock->saved_data_ready with plain loads and stores, while the sibling sk_psock_stop_strp() uses WRITE_ONCE for the same field. sock_map_link() / sock_map_unref() touch the same psock from another CPU without the lock, so KCSAN reports the race:
BUG: KCSAN: data-race in sock_map_link / sock_map_unref BUG: KCSAN: data-race in sk_psock_start_verdict / sock_map_unref
Use READ_ONCE() for the reads and WRITE_ONCE() for the writes, matching the strp sibling.
---
Tested base
-----------
Detected on Linux 7.3-rc4 (commit 93f51579e7df), arm64 QEMU guest. Re-checked against mainline f47d223627f8 (v7.3-341): the affected code is unchanged between the two, so the bug is still present there. Checked against linux-next next-20261021 (commit 9a482184d910): the affected code is unchanged there as well, so the bug is still present in linux-next too.
Reproducer status
-----------------
A reproducer was written and executed on an arm64 QEMU guest (KCSAN); it reported the race. The reproducer source is not posted here per Documentation/process/security-bugs.rst; it is available on request.
Triggering conditions
---------------------CAP_BPF / the ability to update sockmap and attach verdict programs.
Notes
-----
Built and checkpatch-clean: this patch was applied on top of 7.3-rc4 (commit 93f51579e7df) and a full arm64 kernel (defconfig) was built with it -- no new warnings or errors. scripts/checkpatch.pl reports 0 errors, 0 warnings.
net/core/skmsg.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
net/core/skmsg.c +4 -4
@@ -1260,10 +1260,10 @@ static void sk_psock_verdict_data_ready(struct sock *sk)void sk_psock_start_verdict (struct sock *sk,struct sk_psock *psock) { -if (psock->saved_data_ready) +if (READ_ONCE (psock->saved_data_ready))return ; - psock->saved_data_ready = sk->sk_data_ready; +WRITE_ONCE (psock->saved_data_ready, sk->sk_data_ready);WRITE_ONCE (sk->sk_data_ready, sk_psock_verdict_data_ready);WRITE_ONCE (sk->sk_write_space, sk_psock_write_space); }
@@ -1273,9 +1273,9 @@ void sk_psock_stop_verdict(struct sock *sk, struct sk_psock *psock)psock_set_prog (&psock->progs.stream_verdict,NULL );psock_set_prog (&psock->progs.skb_verdict,NULL ); -if (!psock->saved_data_ready) +if (!READ_ONCE (psock->saved_data_ready))return ;WRITE_ONCE (sk->sk_data_ready, psock->saved_data_ready); - psock->saved_data_ready =NULL ; +WRITE_ONCE (psock->saved_data_ready,NULL ); }
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] bpf, sockmap: incomplete data-race fix in sk_psock_start_verdict() and sock_map_unref() --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] bpf, sockmap: incomplete data-race fix in sk_psock_start_verdict() and sock_map_unref() --
commit 3aa9e245b4ee20f4b50e92e1de66b46757cb8a03
Author: Dudu Lu <[email protected]>
bpf, sockmap: use READ_ONCE/WRITE_ONCE for saved_data_ready in the verdict path
This commit annotates accesses to psock->saved_data_ready in sk_psock_start_verdict() and sk_psock_stop_verdict() to address data races with sock_map_link() and sock_map_unref().
Quoted 19 lines
net/core/skmsg.c +2
-2@@ -1260,10 +1260,10 @@ static void sk_psock_verdict_data_ready(struct sock *sk)void sk_psock_start_verdict (struct sock *sk,struct sk_psock *psock) { -if (READ_ONCE (psock->saved_data_ready)) +if (READ_ONCE (psock->saved_data_ready))return ; - psock->saved_data_ready = sk->sk_data_ready; +WRITE_ONCE (psock->saved_data_ready, sk->sk_data_ready);WRITE_ONCE (sk->sk_data_ready, sk_psock_verdict_data_ready);WRITE_ONCE (sk->sk_write_space, sk_psock_write_space); }@@ -1273,9 +1273,9 @@ void sk_psock_stop_verdict(struct sock *sk, struct sk_psock *psock)psock_set_prog (&psock->progs.stream_verdict,NULL );
net/core/skmsg.c void sk_psock_stop_verdict(struct sock *sk, struct sk_psock *psock) · line 1281
Quoted 10 lines
net/core/skmsg.c +2
-2psock_set_prog (&psock->progs.skb_verdict,NULL ); -if (!psock->saved_data_ready) +if (!READ_ONCE (psock->saved_data_ready))return ;WRITE_ONCE (sk->sk_data_ready, psock->saved_data_ready); - psock->saved_data_ready =NULL ; +WRITE_ONCE (psock->saved_data_ready,NULL ); }
[Severity: Medium]
When a socket is concurrently added to and removed from sockmaps via sock_map_update_elem() -> sock_map_link() and sock_map_delete_elem() -> sock_map_unref() -> sock_map_del_link(), does this change actually resolve the reported data races?
Every caller of sk_psock_start_verdict(), sk_psock_stop_verdict(), sk_psock_start_strp(), and sk_psock_stop_strp() (namely sock_map_link() and sock_map_del_link() in net/core/sock_map.c, and sk_psock_drop() in net/core/skmsg.c) already holds write_lock_bh(&sk->sk_callback_lock). Because all writes to psock->saved_data_ready are serialized by sk->sk_callback_lock, the reads in sk_psock_start_verdict() and sk_psock_stop_verdict() cannot race with a concurrent writer.
Meanwhile, sk_psock_stop_verdict() in net/core/skmsg.c still reads psock->saved_data_ready with an unannotated plain load when restoring sk->sk_data_ready:
if (!READ_ONCE(psock->saved_data_ready)) return; WRITE_ONCE(sk->sk_data_ready, psock->saved_data_ready);
And the sibling functions sk_psock_start_strp() and sk_psock_stop_strp() in net/core/skmsg.c, as well as sock_map_link() in net/core/sock_map.c, also use plain loads and stores for psock->saved_data_ready:
net/core/skmsg.c:sk_psock_start_strp() {
if (psock->saved_data_ready)
return;
psock->saved_data_ready = sk->sk_data_ready;
...
}More importantly, the unlocked reads of psock->saved_data_ready that race with sk_psock_start_verdict() happen in sock_map_unref() -> sock_map_del_link() in net/core/sock_map.c under spin_lock_bh(&psock->link_lock), before write_lock_bh(&sk->sk_callback_lock) is acquired:
net/core/sock_map.c:sock_map_del_link() {
...
spin_lock_bh(&psock->link_lock);
list_for_each_entry_safe(link, tmp, &psock->link, list) {
if (link->link_raw == link_raw) {
struct bpf_map *map = link->map;
struct sk_psock_progs *progs = sock_map_progs(map);
if (psock->saved_data_ready && progs->stream_parser)
strp_stop = true;
if (psock->saved_data_ready && progs->stream_verdict)
verdict_stop = true;
if (psock->saved_data_ready && progs->skb_verdict)
verdict_stop = true;
...
}
}
spin_unlock_bh(&psock->link_lock);
if (strp_stop || verdict_stop) {
write_lock_bh(&sk->sk_callback_lock);
if (strp_stop)
sk_psock_stop_strp(sk, psock);
if (verdict_stop)
sk_psock_stop_verdict(sk, psock);
...
write_unlock_bh(&sk->sk_callback_lock);
}
}Because those reads in sock_map_del_link() remain unannotated plain loads outside sk->sk_callback_lock, won't the data race between sk_psock_start_verdict() and sock_map_unref() still trigger? In addition, if sock_map_del_link() checks psock->saved_data_ready before taking write_lock_bh(&sk->sk_callback_lock), can it observe psock->saved_data_ready newly set by a concurrent sock_map_link() and then call sk_psock_stop_verdict(), clearing psock->progs.stream_verdict and psock->progs.skb_verdict and restoring sk->sk_data_ready for the newly added link?
Finally, regarding the race between sock_map_link() and sock_map_unref() mentioned in the commit description, doesn't that race occur in net/core/sock_map.c where sock_map_link() -> sock_map_init_proto() writes psock->psock_update_sk_prot without sk->sk_callback_lock while sock_map_unref() -> sock_map_del_link() reads it under sk->sk_callback_lock:
net/core/sock_map.c:sock_map_init_proto() {
if (!sk->sk_prot->psock_update_sk_prot)
return -EINVAL;
psock->psock_update_sk_prot = sk->sk_prot->psock_update_sk_prot;
return sk->sk_prot->psock_update_sk_prot(sk, psock, false);
}which is not touched by this patch?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
[email protected]
AI reviewTL;DR: Commit message only: the Fixes: tag may point at the wrong commit; 5a685cd94b21 may be the better target (medium confidence), though 2ef2b20cf4e0 may be deliberate for stable backporting. The…
Quoted 1 line
bpf, sockmap: use READ_ONCE/WRITE_ONCE for
saved_data_readyin the verdict path
TL;DR: Commit message only: the Fixes: tag may point at the wrong commit; 5a685cd94b21 may be the better target (medium confidence), though 2ef2b20cf4e0 may be deliberate for stable backporting. The code change itself is not criticized.
Quoted 11 lines
bpf, sockmap: use READ_ONCE/WRITE_ONCE for
saved_data_readyin the verdict path
sk_psock_start_verdict()andsk_psock_stop_verdict()accesspsock->saved_data_readywith plain loads and stores, while the siblingsk_psock_stop_strp()usesWRITE_ONCEfor the same field.sock_map_link()/sock_map_unref()touch the same psock from another CPU without the lock, so KCSAN reports the race:BUG: KCSAN: data-race in
sock_map_link/sock_map_unref
BUG: KCSAN: data-race insk_psock_start_verdict/sock_map_unref
Quoted 6 lines
Use
READ_ONCE()for the reads andWRITE_ONCE()for the writes, matching the strp sibling.Fixes 2ef2b20cf4e0 ("net: annotate data-races around sk->sk_{data_ready,write_space}")Assisted-by LLMSigned-off-by Dudu Lu <[email protected]>
Is the Fixes: tag pointing at the right commit?
2ef2b20cf4e0 only annotated the sk->sk_data_ready and sk->sk_write_space accesses, plus the saved_data_ready store in sk_psock_stop_strp(). Its parent already had plain loads and stores of psock->saved_data_ready in sk_psock_start_verdict() and sk_psock_stop_verdict(), so it left the verdict path unannotated rather than introducing the accesses changed here.
The psock->saved_data_ready field, and the lockless reads of it that the KCSAN reports are about, came from 5a685cd94b21 ("skmsg: Get rid of struct sk_psock_parser"), which folded parser.saved_data_ready and parser.enabled into psock->saved_data_ready. Should the tag be:
The verdict start/stop code itself dates back to ef5659280eb1, but there the unlocked reader tested parser.enabled rather than saved_data_ready, so this attribution is a medium confidence suggestion. It is also possible that 2ef2b20cf4e0 was chosen deliberately, since the context lines of this patch depend on it for stable backporting.
---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md
CI run summary: https://github.com/kernel-patches/bpf/actions/runs/39631503837
Quoted 6 lines · Dudu Lu
sk_psock_stop_strp()usesWRITE_ONCEfor the same field.sock_map_link()/sock_map_unref()touch the same psock from another CPU without the lock, so KCSAN reports the race:BUG: KCSAN: data-race in sock_map_link / sock_map_unref BUG: KCSAN: data-race in sk_psock_start_verdict / sock_map_unref
sashiko is right. KCSAN will print both with this patch applied.
pw-bot: cr
Jakub Sitnicki
maintainerQuoted 50 lines · Dudu Lu
sk_psock_start_verdict()andsk_psock_stop_verdict()accesspsock->saved_data_readywith plain loads and stores, while the siblingsk_psock_stop_strp()usesWRITE_ONCEfor the same field.sock_map_link()/sock_map_unref()touch the same psock from another CPU without the lock, so KCSAN reports the race:BUG: KCSAN: data-race in sock_map_link / sock_map_unref BUG: KCSAN: data-race in sk_psock_start_verdict / sock_map_unrefUse
READ_ONCE()for the reads andWRITE_ONCE()for the writes, matching the strp sibling.Fixes 2ef2b20cf4e0 ("net: annotate data-races around sk->sk_{data_ready,write_space}")Assisted-by LLMSigned-off-by Dudu Lu <[email protected]>---
Tested base
-----------
Detected on Linux 7.3-rc4 (commit93f51579e7df), arm64 QEMU guest. Re-checked against mainlinef47d223627f8(v7.3-341): the affected code is unchanged between the two, so the bug is still present there. Checked against linux-next next-20261021 (commit9a482184d910): the affected code is unchanged there as well, so the bug is still present in linux-next too.Reproducer status
-----------------
A reproducer was written and executed on an arm64 QEMU guest (KCSAN); it reported the race. The reproducer source is not posted here perDocumentation/process/security-bugs.rst; it is available on request.Triggering conditions
---------------------CAP_BPF/ the ability to update sockmap and attach verdict programs.Notes
-----
Built and checkpatch-clean: this patch was applied on top of 7.3-rc4 (commit93f51579e7df) and a full arm64 kernel (defconfig) was built with it -- no new warnings or errors.scripts/checkpatch.plreports 0 errors, 0 warnings.net/core/skmsg.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)net/core/skmsg.c +0
-0
net/core/skmsg.c static void sk_psock_verdict_data_ready(struct sock *sk) · line 1264
Quoted 6 lines
net/core/skmsg.c +1
-1@@ -1260,10 +1260,10 @@ static void sk_psock_verdict_data_ready(struct sock *sk)void sk_psock_start_verdict (struct sock *sk,struct sk_psock *psock) { -if (psock->saved_data_ready) +if (READ_ONCE (psock->saved_data_ready))return ;
Okay, but if we're going down that route, now sk_psock_stop_strp() is missing a READ_ONCE around psock->saved_data_ready, or rather we should do it once and use it through the whole function:
void sk_psock_stop_strp(struct sock *sk, struct sk_psock *psock)
{
psock_set_prog(&psock->progs.stream_parser, NULL);
if (!psock->saved_data_ready)
return;
WRITE_ONCE(sk->sk_data_ready, psock->saved_data_ready);
WRITE_ONCE(psock->saved_data_ready, NULL);
strp_stop(&psock->strp);
}net/core/skmsg.c static void sk_psock_verdict_data_ready(struct sock *sk) · line 1266
Quoted 3 lines
net/core/skmsg.c +1
-1- psock->saved_data_ready = sk->sk_data_ready; +WRITE_ONCE (psock->saved_data_ready, sk->sk_data_ready);
I belive you also need to READ_ONCE(sk->sk_data_ready).
Quoted 15 lines
net/core/skmsg.c +2
-2WRITE_ONCE (sk->sk_data_ready, sk_psock_verdict_data_ready);WRITE_ONCE (sk->sk_write_space, sk_psock_write_space); }@@ -1273,9 +1273,9 @@ void sk_psock_stop_verdict(struct sock *sk, struct sk_psock *psock)psock_set_prog (&psock->progs.stream_verdict,NULL );psock_set_prog (&psock->progs.skb_verdict,NULL ); -if (!psock->saved_data_ready) +if (!READ_ONCE (psock->saved_data_ready))return ;WRITE_ONCE (sk->sk_data_ready, psock->saved_data_ready); - psock->saved_data_ready =NULL ; +WRITE_ONCE (psock->saved_data_ready,NULL ); }
Jiayuan Chen
maintainerQuoted 6 lines · Dudu Lu
sk_psock_start_verdict()andsk_psock_stop_verdict()accesspsock->saved_data_readywith plain loads and stores, while the siblingsk_psock_stop_strp()usesWRITE_ONCEfor the same field.sock_map_link()/sock_map_unref()touch the same psock from another CPU without the lock, so KCSAN reports the race:
Quoted 7 lines
BUG: KCSAN: data-race in sock_map_link / sock_map_unref BUG: KCSAN: data-race in sk_psock_start_verdict / sock_map_unrefUse
READ_ONCE()for the reads andWRITE_ONCE()for the writes, matching the strp sibling.Fixes 2ef2b20cf4e0 ("net: annotate data-races around sk->sk_{data_ready,write_space}")
I think it's just an incomplete fix which you shouldn't blame.
PATCH v2 1/1 bpf, sockmap: annotate the psock teardown data races
In reply to Dudu LuThe sockmap link/unlink paths touch three psock fields from one CPU while another CPU touches them under a different lock. A KCSAN run with two SOCKMAPs sharing one socket reports all three:
BUG: KCSAN: data-race in sock_map_link / sock_map_unref BUG: KCSAN: data-race in sock_map_link / unix_dgram_bpf_update_proto BUG: KCSAN: data-race in sk_psock_start_verdict / sock_map_unref
- psock->psock_update_sk_prot: written in sock_map_init_proto() without
sk->sk_callback_lock, read in sock_map_del_link() under it.
- psock->saved_data_ready: written by sk_psock_start_verdict() under
sk->sk_callback_lock, read in sock_map_del_link() under
psock->link_lock -- a different lock instance. - sk->sk_prot: replaced by sock_replace_proto() (via the protocol's psock_update_sk_prot()) under sk->sk_callback_lock, read by sock_map_link() / sock_map_sk_is_suitable() outside it.
Annotate every one of these accesses with READ_ONCE()/WRITE_ONCE(), matching the existing convention for the same fields: sk->sk_data_ready and sk->sk_write_space were already annotated this way, as were the sk_psock_stop_strp() accesses to saved_data_ready. The indirect calls read the pointer once into a local, so the guard and the call cannot see different values.
No Fixes: tag: the three fields were introduced by three different commits, so no single one is the origin. psock->saved_data_ready was folded into struct sk_psock by commit 5a685cd94b21 ("skmsg: Get rid of struct sk_psock_parser"); the psock->psock_update_sk_prot read in sock_map_del_link() came with commit c0d95d3380ee ("bpf, sockmap:
Re-evaluate proto ops when psock is removed from sockmap"); the lockless sk->sk_prot reads are older still.
---
v2:
- Drop the Fixes: tag; the three fields came from three different commits, so explain the provenance in the message instead (bpf-ci).
- Read the function pointer once into a local before the guard and the call, so the two cannot see different values: psock->saved_data_ready in sk_psock_stop_strp() and sk_psock_stop_verdict(), and psock->psock_update_sk_prot in sk_psock_restore_proto(), sock_map_del_link() and sock_map_init_proto() (Jakub).
- READ_ONCE() the source sk->sk_data_ready in sk_psock_start_strp() and sk_psock_start_verdict() (Jakub).
- Annotate the sock_map.c accesses that KCSAN actually reports:
sock_map_init_proto(), sock_map_del_link(), sock_map_link(), sock_map_psock_get_checked() and sock_map_sk_is_suitable() (Alexei).
Tested base
-----------
Detected on Linux 7.3-rc4 (commit 93f51579e7df), arm64 QEMU guest. Re-checked against mainline f47d223627f8 (v7.3-341): the affected code is unchanged between the two, so the bug is still present there. Checked against linux-next next-20261021 (commit 9a482184d910): the affected code is unchanged there as well, so the bug is still present in linux-next too.
Reproducer status
-----------------
A reproducer was written and executed on an arm64 QEMU guest (KCSAN); it reported the races above. The reproducer source is not posted here per Documentation/process/security-bugs.rst; it is available on request.
Post-fix verification
---------------------
Tested: the patch was built for arm64 and booted in a QEMU guest with KCSAN; the reproducer passes. Concretely, the reproducer was re-run on the guest, once on the base tree and once with this patch applied. On the base tree the sockmap-path races are five -- the three listed above, plus
BUG: KCSAN: data-race in sock_map_update_elem_sys / unix_dgram_bpf_update_proto BUG: KCSAN: data-race in sk_psock_stop_verdict / sock_map_unref
With this patch all five are gone and no new race is introduced. The map operations complete identically in both runs (same op counts, no failures, no KASAN memory errors), so the annotation does not change behaviour.
The reproducer also reports four unrelated races that this patch does not touch and that fire the same way with or without it: three are the socket-lock fast path (the 8-byte try_cmpxchg on sk_lock.combined against the slock-protected sk_lock.owned write, by design), and one is a plain sk->sk_prot read in generic release_sock(), which is a different scope from the sockmap paths annotated here.
Triggering conditions
---------------------CAP_BPF / the ability to update sockmap and attach verdict programs. The two writers run on separate CPUs (the guest has 2), one cycling a map with a verdict program, the other a bare map, on the same socket.
Notes
-----
Built and checkpatch-clean: this patch was applied on top of 7.3-rc4 (commit 93f51579e7df) and the affected objects were rebuilt for arm64 with the KCSAN config -- no new warnings or errors. scripts/checkpatch.pl reports 0 errors, 1 warning (its 'BUG: KCSAN:' commit-message heuristic, i.e. it is asking for a Fixes: tag, which we deliberately omit).
include/linux/skmsg.h | 7 +++++-- net/core/skmsg.c | 24 +++++++++++++++--------- net/core/sock_map.c | 34 +++++++++++++++++++++------------- 3 files changed, 41 insertions(+), 24 deletions(-)
include/linux/skmsg.h +5 -2
@@ -488,8 +488,11 @@ static inline void sk_psock_cork_free(struct sk_psock *psock)static inline void sk_psock_restore_proto (struct sock *sk,struct sk_psock *psock) { -if (psock->psock_update_sk_prot) - psock->psock_update_sk_prot (sk, psock, true); +int (*update_proto)(struct sock *sk,struct sk_psock *psock,bool restore); + + update_proto =READ_ONCE (psock->psock_update_sk_prot); +if (update_proto) +update_proto (sk, psock, true); }static inline struct sk_psock *sk_psock_get (struct sock *sk)
net/core/skmsg.c +15 -9
@@ -1167,22 +1167,25 @@ int sk_psock_init_strp(struct sock *sk, struct sk_psock *psock)void sk_psock_start_strp (struct sock *sk,struct sk_psock *psock) { -if (psock->saved_data_ready) +if (READ_ONCE (psock->saved_data_ready))return ; - psock->saved_data_ready = sk->sk_data_ready; +WRITE_ONCE (psock->saved_data_ready,READ_ONCE (sk->sk_data_ready));WRITE_ONCE (sk->sk_data_ready, sk_psock_strp_data_ready);WRITE_ONCE (sk->sk_write_space, sk_psock_write_space); }void sk_psock_stop_strp (struct sock *sk,struct sk_psock *psock) { +void (*saved_data_ready)(struct sock *sk); +psock_set_prog (&psock->progs.stream_parser,NULL ); -if (!psock->saved_data_ready) + saved_data_ready =READ_ONCE (psock->saved_data_ready); +if (!saved_data_ready)return ; -WRITE_ONCE (sk->sk_data_ready, psock->saved_data_ready); +WRITE_ONCE (sk->sk_data_ready, saved_data_ready);WRITE_ONCE (psock->saved_data_ready,NULL );strp_stop (&psock->strp); }
@@ -1260,22 +1263,25 @@ static void sk_psock_verdict_data_ready(struct sock *sk)void sk_psock_start_verdict (struct sock *sk,struct sk_psock *psock) { -if (psock->saved_data_ready) +if (READ_ONCE (psock->saved_data_ready))return ; - psock->saved_data_ready = sk->sk_data_ready; +WRITE_ONCE (psock->saved_data_ready,READ_ONCE (sk->sk_data_ready));WRITE_ONCE (sk->sk_data_ready, sk_psock_verdict_data_ready);WRITE_ONCE (sk->sk_write_space, sk_psock_write_space); }void sk_psock_stop_verdict (struct sock *sk,struct sk_psock *psock) { +void (*saved_data_ready)(struct sock *sk); +psock_set_prog (&psock->progs.stream_verdict,NULL );psock_set_prog (&psock->progs.skb_verdict,NULL ); -if (!psock->saved_data_ready) + saved_data_ready =READ_ONCE (psock->saved_data_ready); +if (!saved_data_ready)return ; -WRITE_ONCE (sk->sk_data_ready, psock->saved_data_ready); - psock->saved_data_ready =NULL ; +WRITE_ONCE (sk->sk_data_ready, saved_data_ready); +WRITE_ONCE (psock->saved_data_ready,NULL ); }
net/core/sock_map.c +21 -13
@@ -145,6 +145,7 @@ static void sock_map_del_link(struct sock *sk, {bool strp_stop = false, verdict_stop = false;struct sk_psock_link *link, *tmp; +int (*update_proto)(struct sock *sk,struct sk_psock *psock,bool restore);spin_lock_bh (&psock->link_lock);list_for_each_entry_safe (link, tmp, &psock->link, list) {
@@ -152,11 +153,11 @@ static void sock_map_del_link(struct sock *sk,struct bpf_map *map = link->map;struct sk_psock_progs *progs =sock_map_progs (map); -if (psock->saved_data_ready && progs->stream_parser) +if (READ_ONCE (psock->saved_data_ready) && progs->stream_parser) strp_stop = true; -if (psock->saved_data_ready && progs->stream_verdict) +if (READ_ONCE (psock->saved_data_ready) && progs->stream_verdict) verdict_stop = true; -if (psock->saved_data_ready && progs->skb_verdict) +if (READ_ONCE (psock->saved_data_ready) && progs->skb_verdict) verdict_stop = true;list_del (&link->list);sk_psock_free_link (link);
@@ -171,8 +172,9 @@ static void sock_map_del_link(struct sock *sk,if (verdict_stop)sk_psock_stop_verdict (sk, psock); -if (psock->psock_update_sk_prot) - psock->psock_update_sk_prot (sk, psock, false); + update_proto =READ_ONCE (psock->psock_update_sk_prot); +if (update_proto) +update_proto (sk, psock, false);write_unlock_bh (&sk->sk_callback_lock); } }
@@ -189,10 +191,13 @@ static void sock_map_unref(struct sock *sk, void *link_raw)static int sock_map_init_proto (struct sock *sk,struct sk_psock *psock) { -if (!sk->sk_prot->psock_update_sk_prot) +int (*update_proto)(struct sock *sk,struct sk_psock *psock,bool restore); + + update_proto =READ_ONCE (sk->sk_prot)->psock_update_sk_prot; +if (!update_proto)return -EINVAL ; - psock->psock_update_sk_prot = sk->sk_prot->psock_update_sk_prot; -return sk->sk_prot->psock_update_sk_prot (sk, psock, false); +WRITE_ONCE (psock->psock_update_sk_prot, update_proto); +return update_proto (sk, psock, false); }static struct sk_psock *sock_map_psock_get_checked (struct sock *sk)
@@ -202,7 +207,7 @@ static struct sk_psock *sock_map_psock_get_checked(struct sock *sk)rcu_read_lock (); psock =sk_psock (sk);if (psock) { -if (sk->sk_prot->close != sock_map_close) { +if (READ_ONCE (sk->sk_prot)->close != sock_map_close) { psock =ERR_PTR (-EBUSY ); goto out; }
@@ -303,7 +308,8 @@ static int sock_map_link(struct bpf_map *map, struct sock *sk) }write_lock_bh (&sk->sk_callback_lock); -if (stream_parser && stream_verdict && !psock->saved_data_ready) { +if (stream_parser && stream_verdict && + !READ_ONCE (psock->saved_data_ready)) {if (sk_is_tcp (sk)) ret =sk_psock_init_strp (sk, psock);else
@@ -314,9 +320,11 @@ static int sock_map_link(struct bpf_map *map, struct sock *sk) goto out; }sk_psock_start_strp (sk, psock); - }else if (!stream_parser && stream_verdict && !psock->saved_data_ready) { + }else if (!stream_parser && stream_verdict && + !READ_ONCE (psock->saved_data_ready)) {sk_psock_start_verdict (sk,psock); - }else if (!stream_verdict && skb_verdict && !psock->saved_data_ready) { + }else if (!stream_verdict && skb_verdict && + !READ_ONCE (psock->saved_data_ready)) {sk_psock_start_verdict (sk, psock); }write_unlock_bh (&sk->sk_callback_lock);
@@ -536,7 +544,7 @@ static bool sock_map_redirect_allowed(const struct sock *sk)static bool sock_map_sk_is_suitable (const struct sock *sk) { -return !!sk->sk_prot->psock_update_sk_prot; +return !!READ_ONCE (sk->sk_prot)->psock_update_sk_prot; }static bool sock_map_sk_state_allowed (const struct sock *sk)
Re: [PATCH bpf v2] bpf, sockmap: annotate the psock teardown data races
Quoted 2 lines · Dudu Lu
Annotate every one of these accesses with READ_ONCE()/WRITE_ONCE(), matching the existing convention for the same fields:
sk->sk_data_ready
This makes KCSAN quiet and leaves the race where it is.
sock_map_link() runs under the sock lock. sock_map_delete_elem() doesn't take it. sk_callback_lock is the only lock they share and sock_map_link() takes it after psock_set_prog() and sock_map_init_proto().
So two tcp_bpf_update_proto(sk, psock, false) can run in parallel:
cpu0: sock_map_link() for a map with msg_parser sees
psock->progs.stream_verdict and picks TCP_BPF_TXRX
cpu1: sock_map_del_link() stops the verdict and stores TCP_BPF_TX
cpu0: stores TCP_BPF_TXRX
The socket is left with tcp_bpf_recvmsg_parser() and no verdict prog. That's what commit c0d95d3380ee fixed.
Take the lock earlier. Then psock_update_sk_prot needs no annotation.
All accesses to saved_data_ready are under sk_callback_lock except the three reads in sock_map_del_link(). Move that check under the lock instead of annotating it. No need to touch skmsg.c and skmsg.h.
Only the two lockless reads of sk->sk_prot need READ_ONCE().
Something like this (compile tested only):
net/core/sock_map.c +13 -9
@@ -152,11 +152,11 @@ static void sock_map_del_link(struct sock *sk,struct bpf_map *map = link->map;struct sk_psock_progs *progs =sock_map_progs (map); -if (psock->saved_data_ready && progs->stream_parser) +if (progs->stream_parser) strp_stop = true; -if (psock->saved_data_ready && progs->stream_verdict) +if (progs->stream_verdict) verdict_stop = true; -if (psock->saved_data_ready && progs->skb_verdict) +if (progs->skb_verdict) verdict_stop = true;list_del (&link->list);sk_psock_free_link (link);
@@ -164,8 +164,11 @@ static void sock_map_del_link(struct sock *sk, } }spin_unlock_bh (&psock->link_lock); -if (strp_stop || verdict_stop) { -write_lock_bh (&sk->sk_callback_lock); +if (!strp_stop && !verdict_stop) +return ; + +write_lock_bh (&sk->sk_callback_lock); +if (psock->saved_data_ready) {if (strp_stop)sk_psock_stop_strp (sk, psock);if (verdict_stop)
@@ -173,8 +176,8 @@ static void sock_map_del_link(struct sock *sk,if (psock->psock_update_sk_prot) psock->psock_update_sk_prot (sk, psock, false); -write_unlock_bh (&sk->sk_callback_lock); } +write_unlock_bh (&sk->sk_callback_lock); }static void sock_map_unref (struct sock *sk,void *link_raw)
@@ -202,7 +205,7 @@ static struct sk_psock *sock_map_psock_get_checked(struct sock *sk)rcu_read_lock (); psock =sk_psock (sk);if (psock) { -if (sk->sk_prot->close != sock_map_close) { +if (READ_ONCE (sk->sk_prot)->close != sock_map_close) { psock =ERR_PTR (-EBUSY ); goto out; }
@@ -284,6 +287,7 @@ static int sock_map_link(struct bpf_map *map, struct sock *sk) } } +write_lock_bh (&sk->sk_callback_lock);if (msg_parser)psock_set_prog (&psock->progs.msg_parser, msg_parser);if (stream_parser)
@@ -298,11 +302,11 @@ static int sock_map_link(struct bpf_map *map, struct sock *sk)*/ ret =sock_map_init_proto (sk, psock);if (ret <0 ) { +write_unlock_bh (&sk->sk_callback_lock);sk_psock_put (sk, psock); goto out; } -write_lock_bh (&sk->sk_callback_lock);if (stream_parser && stream_verdict && !psock->saved_data_ready) {if (sk_is_tcp (sk)) ret =sk_psock_init_strp (sk, psock);
@@ -536,7 +540,7 @@ static bool sock_map_redirect_allowed(const struct sock *sk)static bool sock_map_sk_is_suitable (const struct sock *sk) { -return !!sk->sk_prot->psock_update_sk_prot; +return !!READ_ONCE (sk->sk_prot)->psock_update_sk_prot; }static bool sock_map_sk_state_allowed (const struct sock *sk)
Pls run your reproducer on it, post the reproducer with the respin and target bpf-next.
pw-bot: cr
Jakub Sitnicki
maintainerRe: [PATCH bpf v2] bpf, sockmap: annotate the psock teardown data races
Quoted 6 lines · Alexei Starovoitov
Quoted 2 lines · Dudu Lu
Annotate every one of these accesses with READ_ONCE()/WRITE_ONCE(), matching the existing convention for the same fields:
sk->sk_data_readyThis makes KCSAN quiet and leaves the race where it is.
Quoted 8 lines
sock_map_link()runs under the sock lock.sock_map_delete_elem()doesn't take it.sk_callback_lockis the only lock they share andsock_map_link()takes it afterpsock_set_prog()andsock_map_init_proto().
So twotcp_bpf_update_proto(sk, psock, false)can run in parallel:
cpu0:sock_map_link()for a map withmsg_parserseespsock->progs.stream_verdict and picks TCP_BPF_TXRXcpu1:
sock_map_del_link()stops the verdict and storesTCP_BPF_TX
cpu0: storesTCP_BPF_TXRX
Right, so:
- map_update_elem syscall vs close(fd) - OK (lock_sock protected),
- map_update_elem syscall vs map_delete_elem syscall - RACY,
- map_update_elem syscall vs TCP RST (destroy) - RACY,
- map_update_elem from BPF vs close(fd) - RACY.
Quoted 86 lines
The socket is left with
tcp_bpf_recvmsg_parser()and no verdict prog. That's what commitc0d95d3380eefixed.
Take the lock earlier. Thenpsock_update_sk_protneeds no annotation.All accesses to
saved_data_readyare undersk_callback_lockexcept the three reads insock_map_del_link(). Move that check under the lock instead of annotating it. No need to touchskmsg.candskmsg.h.Only the two lockless reads of
sk->sk_protneedREAD_ONCE().Something like this (compile tested only):
net/core/sock_map.c +12
-8@@ -152,11 +152,11 @@ static void sock_map_del_link(struct sock *sk,struct bpf_map *map = link->map;struct sk_psock_progs *progs =sock_map_progs (map); -if (psock->saved_data_ready && progs->stream_parser) +if (progs->stream_parser) strp_stop = true; -if (psock->saved_data_ready && progs->stream_verdict) +if (progs->stream_verdict) verdict_stop = true; -if (psock->saved_data_ready && progs->skb_verdict) +if (progs->skb_verdict) verdict_stop = true;list_del (&link->list);sk_psock_free_link (link);@@ -164,8 +164,11 @@ static void sock_map_del_link(struct sock *sk, } }spin_unlock_bh (&psock->link_lock); -if (strp_stop || verdict_stop) { -write_lock_bh (&sk->sk_callback_lock); +if (!strp_stop && !verdict_stop) +return ; + +write_lock_bh (&sk->sk_callback_lock); +if (psock->saved_data_ready) {if (strp_stop)sk_psock_stop_strp (sk, psock);if (verdict_stop)@@ -173,8 +176,8 @@ static void sock_map_del_link(struct sock *sk,if (psock->psock_update_sk_prot) psock->psock_update_sk_prot (sk, psock, false); -write_unlock_bh (&sk->sk_callback_lock); } +write_unlock_bh (&sk->sk_callback_lock); }static void sock_map_unref (struct sock *sk,void *link_raw)@@ -202,7 +205,7 @@ static struct sk_psock *sock_map_psock_get_checked(struct sock *sk)rcu_read_lock (); psock =sk_psock (sk);if (psock) { -if (sk->sk_prot->close != sock_map_close) { +if (READ_ONCE (sk->sk_prot)->close != sock_map_close) { psock =ERR_PTR (-EBUSY ); goto out; }@@ -284,6 +287,7 @@ static int sock_map_link(struct bpf_map *map, struct sock *sk) } } +write_lock_bh (&sk->sk_callback_lock);if (msg_parser)psock_set_prog (&psock->progs.msg_parser, msg_parser);if (stream_parser)@@ -298,11 +302,11 @@ static int sock_map_link(struct bpf_map *map, struct sock *sk)*/ ret =sock_map_init_proto (sk, psock);if (ret <0 ) { +write_unlock_bh (&sk->sk_callback_lock);sk_psock_put (sk, psock); goto out; } -write_lock_bh (&sk->sk_callback_lock);if (stream_parser && stream_verdict && !psock->saved_data_ready) {if (sk_is_tcp (sk)) ret =sk_psock_init_strp (sk, psock);
net/core/sock_map.c static bool sock_map_redirect_allowed(const struct sock *sk) · line 546
Quoted 8 lines
net/core/sock_map.c +1
-1@@ -536,7 +540,7 @@ static bool sock_map_redirect_allowed(const struct sock *sk)static bool sock_map_sk_is_suitable (const struct sock *sk) { -return !!sk->sk_prot->psock_update_sk_prot; +return !!READ_ONCE (sk->sk_prot)->psock_update_sk_prot; }static bool sock_map_sk_state_allowed (const struct sock *sk)
Code LGTM
Re: [PATCH bpf v2] bpf, sockmap: annotate the psock teardown data races
Quoted 5 lines · Jakub Sitnicki
Quoted 3 lines
static bool
sock_map_sk_state_allowed(const struct sock *sk)Code LGTM
Jakub,
Could you commandeer it ?
Dudu is clearly a bot and doesn't care.
Jakub Sitnicki
maintainerRe: [PATCH bpf v2] bpf, sockmap: annotate the psock teardown data races
Quoted 10 lines · Alexei Starovoitov
Quoted 5 lines · Jakub Sitnicki
Quoted 3 lines
static bool
sock_map_sk_state_allowed(const struct sock *sk)Code LGTM
Jakub,
Could you commandeer it ?
Dudu is clearly a bot and doesn't care.
Ack. Will drive this.
Re: [PATCH bpf v2] bpf, sockmap: annotate the psock teardown data races
Hi Jakub, Alexei,
Sorry for the long silence — my laptop died right after v2 and took the local mail and the working tree with it. Agreed that the ordering fix is the right one; please go ahead with the commandeer, Jakub.
I do still have the reproducer (a KCSAN selftest for this race, verified in QEMU). Say the word and I will post it, or send it to you to fold into your respin.
Thanks,
Dudu
Quoted 14 lines · Jakub Sitnicki
Quoted 10 lines · Alexei Starovoitov
Quoted 5 lines · Jakub Sitnicki
Quoted 3 lines
static bool
sock_map_sk_state_allowed(const struct sock *sk)Code LGTM
Jakub,
Could you commandeer it ?
Dudu is clearly a bot and doesn't care.Ack. Will drive this.
Jakub Sitnicki
maintainerRe: [PATCH bpf v2] bpf, sockmap: annotate the psock teardown data races
Quoted 7 lines · phx
Sorry for the long silence — my laptop died right after v2 and took the local mail and the working tree with it. Agreed that the ordering fix is the right one; please go ahead with the commandeer, Jakub.
I do still have the reproducer (a KCSAN selftest for this race, verified in QEMU). Say the word and I will post it, or send it to you to fold into your respin.
Feel free to iterate on the patch once you get your dev env restored.
We would also need selftest coverage for the race, if it reproduces relatively fast. You can take a look at test_race_insert_listen for an example.
Re: [PATCH bpf v2] bpf, sockmap: annotate the psock teardown data races
Quoted 10 lines · Jakub Sitnicki
Quoted 7 lines · phx
Sorry for the long silence — my laptop died right after v2 and took the local mail and the working tree with it. Agreed that the ordering fix is the right one; please go ahead with the commandeer, Jakub.
I do still have the reproducer (a KCSAN selftest for this race, verified in QEMU). Say the word and I will post it, or send it to you to fold into your respin.
Feel free to iterate on the patch once you get your dev env restored.
I very much prefer if you Jakub will take it over. 'phx' is a clueless bot that sends spam across all subsystem.
Jakub Sitnicki
maintainerRe: [PATCH bpf v2] bpf, sockmap: annotate the psock teardown data races
Quoted 6 lines · Alexei Starovoitov
Quoted 5 lines · Jakub Sitnicki
Quoted 4 lines · phx
Sorry for the long silence — my laptop died right after v2 and took the local mail and the working tree with it. Agreed that the ordering fix is the right one; please go ahead with the commandeer, Jakub.
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
I do still have the reproducer (a KCSAN selftest for this race, verified in QEMU). Say the word and I will post it, or send it to you to fold into your respin.
Feel free to iterate on the patch once you get your dev env restored.
I very much prefer if you Jakub will take it over. 'phx' is a clueless bot that sends spam across all subsystem.
Got me fooled. Thanks. I will respin it myself.
Jakub Sitnicki
maintainerRe: [PATCH bpf v2] bpf, sockmap: annotate the psock teardown data races
In reply to Alexei StarovoitovQuoted 6 lines · Alexei Starovoitov
Quoted 5 lines · Jakub Sitnicki
Quoted 4 lines · phx
Sorry for the long silence — my laptop died right after v2 and took the local mail and the working tree with it. Agreed that the ordering fix is the right one; please go ahead with the commandeer, Jakub.
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
I do still have the reproducer (a KCSAN selftest for this race, verified in QEMU). Say the word and I will post it, or send it to you to fold into your respin.
Feel free to iterate on the patch once you get your dev env restored.
I very much prefer if you Jakub will take it over. 'phx' is a clueless bot that sends spam across all subsystem.
Fix now posted: