From: Eric Dumazet <edumazet@google.com>
To: Qingjie Xing <xqjcool@gmail.com>
Cc: davem@davemloft.net, kuba@kernel.org, pabeni@redhat.com,
keescook@chromium.org, johannes@sipsolutions.net,
pctammela@mojatatu.com, dhowells@redhat.com, fw@strlen.de,
kuniyu@amazon.com, netdev@vger.kernel.org,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH] netlink: Fix the netlink socket malfunction due to concurrency
Date: Fri, 18 Aug 2023 20:57:28 +0200 [thread overview]
Message-ID: <CANn89iJCDYteM_1SQ-h2=htUAE4FqrBAak0kHt_Z990XYZThzQ@mail.gmail.com> (raw)
In-Reply-To: <20230818183712.123455-1-xqjcool@gmail.com>
On Fri, Aug 18, 2023 at 8:42 PM Qingjie Xing <xqjcool@gmail.com> wrote:
>
> The concurrent Invocation of netlink_attachskb() and netlink_recvmsg()
> on different CPUs causes malfunction of netlink socket.
>
> The concurrent scenario of netlink_recvmsg() and netlink_attachskb()
> as following:
>
> CPU A CPU B
> ======== ========
> netlink_recvmsg() netlink_attachskb()
> [1]bit NETLINK_S_CONGESTED is set
> netlink_overrun()
> netlink_rcv_wake()
> [2]sk_receive_queue is empty
> clear bit NETLINK_S_CONGESTED
> [3]NETLINK_F_RECV_NO_ENOBUFS not set
> set bit NETLINK_S_CONGESTED
>
> In this scenario, the socket's receive queue is empty. Additionally,
> due to the NETLINK_S_CONGESTED flag being set, all packets sent to
> this socket are discarded.
>
> To prevent this situation, we need to introduce a check for whether
> the socket receive buffer is full before setting the NETLINK_S_CONGESTED
> flag.
>
> Signed-off-by: Qingjie Xing <xqjcool@gmail.com>
> ---
> net/netlink/af_netlink.c | 3 ++-
> 1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/net/netlink/af_netlink.c b/net/netlink/af_netlink.c
> index 383631873748..80bcce9acbfc 100644
> --- a/net/netlink/af_netlink.c
> +++ b/net/netlink/af_netlink.c
> @@ -352,7 +352,8 @@ static void netlink_overrun(struct sock *sk)
> struct netlink_sock *nlk = nlk_sk(sk);
>
> if (!(nlk->flags & NETLINK_F_RECV_NO_ENOBUFS)) {
> - if (!test_and_set_bit(NETLINK_S_CONGESTED,
> + if (atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf
> + && !test_and_set_bit(NETLINK_S_CONGESTED,
> &nlk_sk(sk)->state)) {
> sk->sk_err = ENOBUFS;
> sk_error_report(sk);
This does not look race-free to me.
sk->sk_rcvbuf can change anytime.
I wonder if we should instead remove all this CONGESTED thing and come
back to the fundamentals,
instead of having another bit mirroring some existing state.
Apparently part of it was added in
commit cd1df525da59c64244d27b4548ff5d132489488a
Author: Patrick McHardy <kaber@trash.net>
Date: Wed Apr 17 06:47:05 2013 +0000
netlink: add flow control for memory mapped I/O
Add flow control for memory mapped RX. Since user-space usually doesn't
invoke recvmsg() when using memory mapped I/O, flow control is performed
in netlink_poll(). Dumps are allowed to continue if at least half of the
ring frames are unused.
But I do not think we kept this memory mapped RX.
next prev parent reply other threads:[~2023-08-18 18:57 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-08-18 18:37 [PATCH] netlink: Fix the netlink socket malfunction due to concurrency Qingjie Xing
2023-08-18 18:57 ` Eric Dumazet [this message]
2023-08-19 18:04 ` Qingjie Xing
2023-08-19 18:24 ` Qingjie Xing
2023-08-19 20:17 ` Qingjie Xing
2023-08-23 16:43 ` Further Elaboration on this patch Qingjie Xing
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='CANn89iJCDYteM_1SQ-h2=htUAE4FqrBAak0kHt_Z990XYZThzQ@mail.gmail.com' \
--to=edumazet@google.com \
--cc=davem@davemloft.net \
--cc=dhowells@redhat.com \
--cc=fw@strlen.de \
--cc=johannes@sipsolutions.net \
--cc=keescook@chromium.org \
--cc=kuba@kernel.org \
--cc=kuniyu@amazon.com \
--cc=linux-kernel@vger.kernel.org \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=pctammela@mojatatu.com \
--cc=xqjcool@gmail.com \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).