From: sashiko-bot@kernel.org
To: "Jiayuan Chen" <jiayuan.chen@linux.dev>, bpf@vger.kernel.org
Subject: Re: [PATCH bpf 03/11] bpf, sockmap: Fix strparser reinit after the parser link is removed
Date: Sat, 21 Nov 2026 11:04:51 +0000 [thread overview]
Message-ID: <sashiko-outbox-226969@kernel.org> (raw)
In-Reply-To: <20261121103222.88209-4-jiayuan.chen@linux.dev>
Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [High] bpf, sockmap: active work_struct reinit on psock->rwork in
skmsg
- [High] bpf, sockmap: sk_psock_init_strp() rewinds psock->copied_seq
- [High] bpf, sockmap: softirq livelock in sock_map_destroy() on rwork
put
- [Medium] bpf, sockmap: race and missing wait in
sock_map_strp_done_wait()
--
commit d7a98bf12f6088d3395a10dfb32d450db8554b49
Author: Jiayuan Chen <jiayuan.chen@linux.dev>
bpf, sockmap: Fix strparser reinit after the parser link is removed
This commit tears down the strparser asynchronously via psock->rwork when
a parser link is removed while other sockmap links keep the psock alive,
and blocks attaching a new parser until teardown finishes.
> diff --git a/net/core/skmsg.c b/net/core/skmsg.c
> index 16f84dcb5acd9..43dc128841e98 100644
> --- a/net/core/skmsg.c
> +++ b/net/core/skmsg.c
> @@ -879,6 +879,18 @@ static void sk_psock_destroy(struct work_struct *work)
> {
> struct sk_psock *psock = container_of(to_rcu_work(work),
> struct sk_psock, rwork);
> +
> + if (sk_psock_test_state(psock, SK_PSOCK_STRP_DONE_PENDING)) {
> + /* Parser link gone, other links keep the psock alive */
> + sk_psock_done_strp(psock);
> + write_lock_bh(&psock->sk->sk_callback_lock);
> + sk_psock_clear_state(psock, SK_PSOCK_RX_STRP_ENABLED);
> + sk_psock_clear_state(psock, SK_PSOCK_STRP_DONE_PENDING);
> + write_unlock_bh(&psock->sk->sk_callback_lock);
> + sk_psock_put(psock->sk, psock);
> + return;
> + }
> +
> /* No sk_callback_lock since already detached. */
>
> sk_psock_done_strp(psock);
[Severity: High]
Does calling sk_psock_put() with bottom halves enabled here risk a
softirq livelock in sock_map_destroy()?
When the removed parser link was the last link on the socket,
sk_psock_queue_strp_done() holds a psock reference until rwork runs. If
the socket is closed in the meantime, sock_map_close() does not wait for
rwork and calls saved_close() while sk->sk_prot->destroy is still
sock_map_destroy. When rwork drops the last psock reference with BH
enabled, a softirq destroying the socket on the same CPU can interrupt
between refcount_dec_and_test() and sk_psock_restore_proto():
sk_psock_destroy()
write_unlock_bh(&sk->sk_callback_lock)
sk_psock_put(sk, psock)
refcount_dec_and_test(&psock->refcnt) -> true
<softirq>
tcp_v4_rcv()
...
sock_map_destroy()
sk_psock_get(sk) -> NULL
READ_ONCE(sk->sk_prot)->destroy == sock_map_destroy
goto retry; <-- spins forever
sk_psock_drop()
sk_psock_restore_proto()
The interrupted kworker cannot resume to restore sk->sk_prot, so the CPU
hard-locks in the retry loop.
> @@ -1208,6 +1220,18 @@ void sk_psock_stop_strp(struct sock *sk, struct sk_psock *psock)
> strp_stop(&psock->strp);
> }
>
> +/* Called with sk_callback_lock held while a link still holds the psock */
> +void sk_psock_queue_strp_done(struct sk_psock *psock)
> +{
> + if (!sk_psock_test_state(psock, SK_PSOCK_RX_STRP_ENABLED) ||
> + test_and_set_bit(SK_PSOCK_STRP_DONE_PENDING, &psock->state))
> + return;
> +
> + refcount_inc(&psock->refcnt);
> + INIT_WORK(&psock->rwork.work, sk_psock_destroy);
> + schedule_work(&psock->rwork.work);
> +}
> +
> static void sk_psock_done_strp(struct sk_psock *psock)
> {
> /* Parser has been stopped */
[Severity: High]
Can re-initializing psock->rwork here and in sk_psock_drop() corrupt the
work item while it is still executing or being flushed?
When sk_psock_destroy() finishes the SK_PSOCK_STRP_DONE_PENDING path and
calls sk_psock_put(), dropping the last reference calls sk_psock_drop() ->
INIT_RCU_WORK(&psock->rwork, sk_psock_destroy) from inside the running
psock->rwork.work callback. If another link kept the psock alive, a
concurrent parser attach and detach on another CPU can likewise call
sk_psock_queue_strp_done() -> INIT_WORK(&psock->rwork.work, ...) as soon
as sk_callback_lock is released, while the first worker is still in
process_one_work() or while another task is in flush_work():
CPU0 (kworker) CPU1
process_one_work(rwork.work)
sk_psock_destroy()
clear SK_PSOCK_STRP_DONE_PENDING
write_unlock_bh()
sock_map_del_link()
sk_psock_queue_strp_done()
INIT_WORK(&psock->rwork.work, ...)
schedule_work(&psock->rwork.work)
sk_psock_put()
sk_psock_drop()
INIT_RCU_WORK(&psock->rwork, ...)
queue_rcu_work(..., &psock->rwork)
process_one_work(rwork.work)
sk_psock_destroy()
kfree(psock) <-- use-after-free
INIT_WORK() and INIT_RCU_WORK() overwrite work->data (wiping the worker
pool ID needed by find_worker_executing_work() and start_flush_work())
and re-initialize work->lockdep_map. That lets __queue_work() run
sk_psock_destroy() on a second CPU concurrently while the first worker is
still finishing, and causes flush_work() in sock_map_strp_done_wait() to
see a NULL pool and return early without waiting. Could psock->rwork be
initialized once in sk_psock_init() instead?
[Severity: High]
When a surviving psock is later added to another parser map and goes
through sk_psock_init_strp() again, does resetting psock->copied_seq drop
subsequent incoming TCP data?
net/core/skmsg.c:sk_psock_init_strp() {
...
if (sk_is_tcp(sk)) {
psock->strp.cb.read_sock = tcp_bpf_strp_read_sock;
psock->copied_seq = tcp_sk(sk)->copied_seq;
}
...
}
In tcp_bpf_strp_read_sock(), tp->copied_seq is held behind
psock->copied_seq by the unread SK_PASS bytes sitting in
psock->ingress_msg. If unread SK_PASS messages from the first parser are
still queued in psock->ingress_msg when the second parser is initialized,
overwriting psock->copied_seq with tcp_sk(sk)->copied_seq rewinds
psock->copied_seq behind the next skb on sk->sk_receive_queue. On later
packets, tcp_recv_skb() computes offset = seq - TCP_SKB_CB(skb)->seq,
which underflows u32 to a value larger than skb->len and causes
__tcp_read_sock() to discard incoming skbs via tcp_eat_recv_skb().
> diff --git a/net/core/sock_map.c b/net/core/sock_map.c
> index 0d7a8d9405d19..8b14a338a0327 100644
> --- a/net/core/sock_map.c
> +++ b/net/core/sock_map.c
> @@ -115,6 +115,33 @@ int sock_map_prog_detach(const union bpf_attr *attr, enum bpf_prog_type ptype)
> return ret;
> }
>
> +/*
> + * Wait for the strparser of a removed parser link to be torn down, so that
> + * a new parser can be attached right after the old one was removed.
> + */
> +static void sock_map_strp_done_wait(struct bpf_map *map, struct sock *sk)
> +{
> + struct sk_psock *psock;
> +
> + if (!READ_ONCE(sock_map_progs(map)->stream_parser))
> + return;
> +
> + rcu_read_lock();
> + read_lock_bh(&sk->sk_callback_lock);
> + psock = sk_psock(sk);
> + if (!psock ||
> + !sk_psock_test_state(psock, SK_PSOCK_STRP_DONE_PENDING) ||
> + !refcount_inc_not_zero(&psock->refcnt))
> + psock = NULL;
> + read_unlock_bh(&sk->sk_callback_lock);
> + rcu_read_unlock();
> + if (!psock)
> + return;
> +
> + flush_work(&psock->rwork.work);
> + sk_psock_put(sk, psock);
> +}
[Severity: Medium]
Can a back-to-back map update still race with a dying zero-link psock or
attach to it before teardown finishes?
kworker CPU1
sk_psock_destroy()
write_lock_bh(&sk_callback_lock)
clear SK_PSOCK_STRP_DONE_PENDING
write_unlock_bh(&sk_callback_lock)
sock_map_update_elem_sys()
sock_map_strp_done_wait()
/* DONE_PENDING clear: no wait */
sock_map_link()
sock_map_psock_get_checked()
sk_psock_put()
refcount_dec_and_test() -> 0
refcount_inc_not_zero() -> 0
return ERR_PTR(-EBUSY)
sk_psock_drop()
rcu_assign_sk_user_data(sk, NULL)
Because sk_psock_queue_strp_done() takes a reference on psock when the
last link is removed, the zero-link psock stays in sk->sk_user_data until
rwork drops that reference. Clearing SK_PSOCK_STRP_DONE_PENDING before
sk_psock_put() opens a window where sock_map_strp_done_wait() does not
flush, and sock_map_psock_get_checked() either returns -EBUSY (if refcnt
just hit 0) or resurrects the dying psock with its old
psock->progs.msg_parser still installed.
Also, because sock_map_strp_done_wait() and the SK_PSOCK_RX_STRP_ENABLED
check in sock_map_link() both only trigger when the new map has a
stream_parser, adding the socket to a map without a stream_parser right
after removing its last link from a parser map skips the wait completely:
it can hit the same resurrection or -EBUSY, or call
sk_psock_start_verdict() while strp_done() is still pending.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261121103222.88209-1-jiayuan.chen@linux.dev?part=3
next prev parent reply other threads:[~2026-11-21 11:04 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-21 10:30 [PATCH bpf 00/11] fix misc sockmap issues Jiayuan Chen
2026-11-21 10:30 ` [PATCH bpf 01/11] bpf, sockmap: Fix copied_seq updates with strparser Jiayuan Chen
2026-11-21 10:59 ` sashiko-bot
2026-11-21 10:30 ` [PATCH bpf 02/11] selftests/bpf: Add test for unread SK_PASS data " Jiayuan Chen
2026-11-21 10:59 ` sashiko-bot
2026-11-21 10:30 ` [PATCH bpf 03/11] bpf, sockmap: Fix strparser reinit after the parser link is removed Jiayuan Chen
2026-11-21 11:04 ` sashiko-bot [this message]
2026-11-21 10:30 ` [PATCH bpf 04/11] bpf, sockmap: Fix empty sk_msg queued by ingress redirect Jiayuan Chen
2026-11-21 10:30 ` [PATCH bpf 05/11] bpf, sockmap: Fix concurrent senders on one psock Jiayuan Chen
2026-11-21 10:53 ` sashiko-bot
2026-11-21 10:30 ` [PATCH bpf 06/11] bpf, sockmap: Fix reconnect with a psock by rejecting disconnect Jiayuan Chen
2026-11-21 10:30 ` [PATCH bpf 07/11] bpf, sockmap: Fix psock refcnt taken in sk_psock_backlog() Jiayuan Chen
2026-11-21 10:30 ` [PATCH bpf 08/11] bpf, sockmap: Fix socket lock taken on a child before accept() Jiayuan Chen
2026-11-21 10:56 ` sashiko-bot
2026-11-21 10:30 ` [PATCH bpf 09/11] bpf, sockmap: Fix apply_bytes when a redirect sends only a part Jiayuan Chen
2026-11-21 11:11 ` bot+bpf-ci
2026-11-21 11:15 ` sashiko-bot
2026-11-21 10:30 ` [PATCH bpf 10/11] selftests/bpf: Fix errno expected by the wait send mem test of test_sockmap Jiayuan Chen
2026-11-21 10:30 ` [PATCH bpf 11/11] selftests/bpf: Add test for apply_bytes when a redirect sends a part Jiayuan Chen
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=sashiko-outbox-226969@kernel.org \
--to=sashiko-bot@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=jiayuan.chen@linux.dev \
--cc=sashiko-reviews@lists.linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.