🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.kernel.netdev
Article #5601

Re: [PATCH net-next 11/11] can: protect the receive lists with a per-device lock

#5601
From: netdev-bot+sashiko
Date: Tue, 20 Oct 2026 17:11
197 lines
15051 bytes
Thank you for your contribution! Sashiko AI review found 3 potential
issue(s) to consider.

Critical: 0 · High: 0 · Medium: 1 · Low: 2

If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).

- [Medium] raw_bind() no longer holds RTNL, so the socket can miss a
  NETDEV_DOWN that arrives while it is binding.
- [Low] The commit message says "the callers pass the namespace that has
  been used for can_rx_register()", and the new kernel-doc of…
- [Low] The new comment on netns_can.rcvlists_lock in
  include/net/netns/can.h says it "protects rcv_lists_stats".

--

can: protect the receive lists with a per-device lock

This commit adds a spinlock to struct can_dev_rcv_lists. The receive lists
of a CAN device are now protected per device instead of by the per-netns
rcvlists_lock, so can_rx_unregister() still finds subscriptions after the
device has moved to another namespace. It also drops RTNL from raw_bind(),
raw_setsockopt(), raw_release(), isotp_bind() and isotp_release(), and
relies on lock_sock() plus a device pointer published inside the RCU
lookup to serialize with the netdev notifiers.

Link: https://lore.kernel.org/linux-can/CEA6A38A-2646-4ADA-95B4-CBAE2F301A8E@doyensec.com/
Link: https://patch.msgid.link/20261019152741.54854-6-socketcan@hartkopp.net

> diff --git a/include/net/netns/can.h b/include/net/netns/can.h
> index 48b79f7e6236..ef7aefdf7b23 100644
> --- a/include/net/netns/can.h
> +++ b/include/net/netns/can.h
> @@ -29,7 +29,7 @@ struct netns_can {
>  
>  	/* receive filters subscribed for 'all' CAN devices */
>  	struct can_dev_rcv_lists *rx_alldev_list;
> -	spinlock_t rcvlists_lock;
> +	spinlock_t rcvlists_lock; /* protects rcv_lists_stats */

[Severity: Low]
This isn't a bug, but is this comment accurate? The lock only serializes
the rcv_entries and rcv_entries_max updates in can_rx_register() and
can_rx_unregister().

can_init_stats() in net/can/proc.c updates other fields of the same
structure without taking rcvlists_lock. It runs from the stats timer, or
from the reset_stats proc read when the timer is disabled:

net/can/proc.c:can_init_stats() {
    ...
	rcv_lists_stats->stats_reset++;

	if (user_reset) {
		user_reset = 0;
		rcv_lists_stats->user_reset++;
	}
}

can_stats_proc_show() also reads these counters without the lock.

Could the comment name rcv_entries and rcv_entries_max specifically?

[ ... ]

> diff --git a/net/can/af_can.c b/net/can/af_can.c
> index d97f85fc3232..bf612c4b983d 100644
> --- a/net/can/af_can.c
> +++ b/net/can/af_can.c

[ ... ]

> @@ -509,6 +513,13 @@ static void can_rx_delete_receiver(struct rcu_head *rp)
>   *
>   * Description:
>   *  Removes subscription entry depending on given (subscription) values.
> + *  The receive lists of a CAN device are protected by a per-device lock,
> + *  so the subscription is found even when the device has been moved to
> + *  another network namespace after can_rx_register(). The caller has to
> + *  ensure that @dev stays valid, e.g. by holding a reference to @dev or
> + *  RTNL. @net is the namespace that has been used for can_rx_register().
> + *  It selects the 'any' CAN device lists for @dev == NULL and is used for
> + *  the receive list statistics.
>   */

[Severity: Low]
The new kernel-doc says @net is the namespace that was used for
can_rx_register(). The commit message says the same: "the callers pass
the namespace that has been used for can_rx_register()".

Does j1939 follow this? It reads dev_net(ndev) again at unregister time:

net/can/j1939/main.c:j1939_can_rx_register() {
    ...
	ret = can_rx_register(dev_net(ndev), ndev, J1939_CAN_ID, J1939_CAN_MASK,
			      j1939_can_recv, priv, 0, NULL);
    ...
}

net/can/j1939/main.c:j1939_can_rx_unregister() {
    ...
	can_rx_unregister(dev_net(ndev), ndev, J1939_CAN_ID, J1939_CAN_MASK,
			  j1939_can_recv, priv);
    ...
}

A j1939 registration can outlive a namespace move. j1939_sk_bind() does
not hold RTNL. It calls j1939_netdev_start(), which registers with the old
namespace, before it calls j1939_jsk_add().

Suppose the NETDEV_UNREGISTER from __dev_change_net_namespace() runs
j1939_sk_netdev_event_unregister() in that window. The socket is not
J1939_SOCK_BOUND yet, so it is skipped and the registration survives the
move. The final release then passes the new namespace:

j1939_netdev_stop()
  __j1939_rx_release()
    j1939_can_rx_unregister()
      can_rx_unregister(dev_net(ndev), ...)

The receiver is still found through the per-device lists. However,
rcv_entries is decremented in the new namespace's rcv_lists_stats
(clamped at 0) and stays elevated in the old one. This accounting matches
the behaviour before the patch.

Should j1939_priv store the registration namespace (with a netns
reference)? Or should the kernel-doc and commit message describe a looser
rule?

[ ... ]

> diff --git a/net/can/raw.c b/net/can/raw.c
> index dfea48768b23..0d8edfa772fb 100644
> --- a/net/can/raw.c
> +++ b/net/can/raw.c

[ ... ]

> @@ -305,16 +309,28 @@ static void raw_notify(struct raw_sock *ro, unsigned long msg,
>  		       struct net_device *dev)
>  {
>  	struct sock *sk = &ro->sk;
> +	struct net_device *bind_dev;
>  
>  	if (!net_eq(dev_net(dev), sock_net(sk)))
>  		return;
>  
> -	if (ro->dev != dev)
> +	/* Lockless fast path, ro->bind_dev covers a concurrent bind().
> +	 * Read it before ro->dev, pairs with smp_store_release() in
> +	 * raw_bind(). A stale ro->bind_dev only costs a lock_sock().
> +	 */
> +	bind_dev = smp_load_acquire(&ro->bind_dev);
> +	if (READ_ONCE(ro->dev) != dev &&
> +	    (msg != NETDEV_UNREGISTER || bind_dev != dev))
>  		return;

[Severity: Medium]
Can a NETDEV_DOWN be lost while raw_bind() is running? The bind_dev hint
is only checked for NETDEV_UNREGISTER, and raw_bind() no longer holds
RTNL.

raw_bind() now runs these steps in order:

    smp_store_release(&ro->bind_dev, dev);
    ...
    if (!(READ_ONCE(dev->flags) & IFF_UP))   /* device still up here */
        notify_enetdown = 1;
    ...
    err = raw_enable_allfilters(sock_net(sk), dev, sk);  /* cond_resched() */
    ...
    WRITE_ONCE(ro->dev, dev);

Suppose dev_close() runs after the IFF_UP check but before ro->dev is
published. raw_notifier()->raw_notify() then sees ro->dev != dev with
msg == NETDEV_DOWN, so it returns at the check above and never reaches
case NETDEV_DOWN. raw_bind() finishes with notify_enetdown == 0.

The socket ends up bound to a down interface, and sk_err = ENETDOWN /
sk_error_report() is never raised. Before this patch, rtnl_lock() in
raw_bind() kept dev_close() out of this window.

isotp_bind() publishes so->dev before it reads IFF_UP, so it does not seem
to have this problem.

Could raw_notify() also check bind_dev for NETDEV_DOWN? Alternatively,
raw_bind() could publish ro->dev before the IFF_UP check.

[ ... ]

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261019170834.1727150-2-mkl%40pengutronix.de
Technical Details
Message-ID: <179251629957.3135.180755777057921325@kernel.org>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-349417-linux-kernel-netdev=dewy.ru
References: <20261019170834.1727150-12-mkl@pengutronix.de>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-349417-linux-kernel-netdev=dewy.ru
From: netdev-bot+sashiko@kernel.org
Newsgroups: linux.kernel.netdev
Subject: Re: [PATCH net-next 11/11] can: protect the receive lists with a
 per-device lock
Date: Tue, 20 Oct 2026 17:11:39 +0000
Organization: Usenet Network
Message-ID: <179251629957.3135.180755777057921325@kernel.org>
References: <20261019170834.1727150-12-mkl@pengutronix.de>
MIME-Version: 1.0
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 8bit
Injection-Info: usenet.network;
	logging-data="3906407"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:K3WCV43ym10TrzqCoXYEm6oHYPk= sha256:gxyZUxIHqzMdLndxSBSdeV3HcV4f46hsatgwhBhdMIg=
	sha1:j/TTHqpgFETWGq+G4tzzmsbQOV4= sha256:nGMz3A4z+GVxUAN3elbijKHcW+/18WbFFEfB31fMsFc=
List-ID: <netdev.vger.kernel.org>
X-Mailpost-Empty-Hdrs: X-Spam-Level
X-Received: from localhost (localhost [127.0.0.1])
	by mail.dewy.ru (Postfix) with ESMTP id D6E21110DA1; Tue, 20 Oct 2026 20:23:39 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -3.729
X-Spam-Status: No, score=-3.729 tagged_above=-9999 required=6.31
	tests=[DCC_REPUT_70_89=0.1, DKIMWL_WL_HIGH=-0.329, DKIM_SIGNED=0.1,
	DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1,
	MAILING_LIST_MULTI=-1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001,
	SPF_PASS=-0.001] autolearn=ham autolearn_force=no
X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (2048-bit key)
	header.d=kernel.org
X-Received: from mail.dewy.ru ([127.0.0.1])
	by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024)
	with ESMTP id DdPto99stKZo;
	Tue, 20 Oct 2026 20:23:32 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=104.64.211.4; helo=sin.lore.kernel.org; envelope-from=netdev+bounces-349417-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> 
X-Authentication-Results: OpenDMARC; dmarc=pass (p=quarantine dis=none) header.from=kernel.org
X-Received: from sin.lore.kernel.org (sin.lore.kernel.org [104.64.211.4])
	by mail.dewy.ru (Postfix) with ESMTPS id 89C7F110D9E; Tue, 20 Oct 2026 20:23:23 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92])
	by sin.lore.kernel.org (Postfix) with ESMTP id 2C97E31907; Tue, 20 Oct 2026 17:12:26 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass;
	t=1792516334;
	b=UkFoeLARCl9HZ3w+RKYdyxYA2EHzCBQmfULgsmtdQvClybUD8XBKWSP3UBJCmNHjN57X
	 CPnwHjdQvFy6yLVvGAT+Os3EGZGZgfJOdDisQ2i+Jtd/oFTqnNrOIaQTxe1cdpv0cIJ+m
	 2aLTB8rKOilhEit3cQQhID4NlvVLOCJhcoVpTtSY19AlWVgtZ4l/X7ygeYdNcRC8PaNlr
	 EQ0RJn0clrXSHMXzY3HOXQ66qnVwbfnUmN4AjgSR8dMY3ZqNWN45/wPpmNvrEGfmvuvdC
	 LE3BAUwWOBNu1n4OA5tPy+hd/7IbKTYZV/NwZVCg2f/DMnaxbK8teKNoKL2VClZHDdQ==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1792516334;
	h=X-Original-To:Received:Received:DKIM-Signature:Subject:From:To:Cc:
	 Date:Message-ID:In-Reply-To:References:X-sashiko-severity:
	 Content-Type:Content-Transfer-Encoding:X-Mailing-List:List-Id:
	 List-Post:List-Help:List-Subscribe:List-Unsubscribe:List-Owner:
	 Precedence:MIME-Version;
	bh=o3WJRJcePloVZq4AExePZBL3lYY5STJ5wC5g0j9QDrc=;
	b=QTADbaLdxaPf/fxUvQZMJMdXuAkAiriQRd6An7va58u02s+ycWCvzFJYdFJmp94IZjJK
	 Wiih1mahJXazigBTAKfghtjCCfkzIDZUU7EIzkyOUtVyhYFvl2CgnIxR6+qeSQmib4lsZ
	 cXc57IPSX03TKkwOsYf1b0ubBJABFN7SXQilOx7dRNNvKjvSHENmQrDq7hHbeUFUM0D2y
	 3/oa52eKVgxa86wWuZg2yXlm8K3QwmhID2vO/9kIZwWYn+VV50VD1R4Dh86kFLZZLHejx
	 KLkEsUyiV+xunoe34pU9vykzmTydEgO5vZizTAJQlRbrsoZDVuEBgLXmgnpq1pFMlfw==
X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; 
	dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=YfuCXTLE;
	arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-Received: from localhost.localdomain (localhost [127.0.0.1])
	by smtp.subspace.kernel.org (Postfix) with ESMTP id DE91C60A262D;
	Tue, 20 Oct 2026 17:12:13 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org;
	dkim=pass (2048-bit key, unprotected) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=YfuCXTLE
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18
X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none;
	t=1792516328;
	b=h1ZFgARaf5trEpPC+MYgxUhL0BSs/22e7ndGzYwfc5TCW52lXThN0uDCoNWuKY7qmbXO
	 WR5NewXP6tkeXyTiG0IsB3Jactk6TaTCzkxhMMN9QZX3qVBi2CzDGYAfGukBsqjgIamdE
	 ntHDws8K30uWHLhVtvgrC97RLW/LtUnUso2YXES/67d5QU4602Z5JAvXaaLEun/IvkbIK
	 4IRmyA8UmixfwA3h3VfV7yo6DDlcMYyzPw8c8DiyalixxD42gd8M93ccUduCHtMrsAQ7f
	 Jcbl+v3mso6/P6hs93aWqluwiM/Hqsqaa1YpNZCMQwhWPPmJ+S9qUMvfFTOrmPflGVg==
X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1792516328;
	h=Received:DKIM-Signature:Subject:From:To:Cc:Date:Message-ID:
	 In-Reply-To:References:X-sashiko-severity:Content-Type:
	 Content-Transfer-Encoding:MIME-Version;
	bh=o3WJRJcePloVZq4AExePZBL3lYY5STJ5wC5g0j9QDrc=;
	b=nPH45SKAPuy04uGJusyarTeDPqIZ1dp1/sSv4VTYKBkxourv0RhjDUxhLkLyTOD4pKpE
	 UdNWKSQexJtLmbTvyGqS1kvQpJSc6TK7Quq4PpZ+7PXH35oMI0x2C/A4Wc4pzSJA8BHa1
	 wUIrWmq4k8PBiMJaiXCw6ORCAe05g1/LWiOY5BPqLvsOvXoPzxZcYpsRVTfPmsyGW3itc
	 ns7lCMhq798NgLnGa5aGr1p4kBd8ovBPipqec0E8vIrNRLrt3OyTETcHvxWufUqzl/5pb
	 b/vEBer5OCGpplLnpGleW+52Vk9eTXOQZI2igsTewErk2lCVCo1r3JCw/qttKAUF3dQ==
X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; 
	dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=YfuCXTLE;
	arc=none smtp.remote-ip=100.103.45.18
X-Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange X25519 server-signature ECDSA (prime256v1))
	(No client certificate requested)
	by smtp.subspace.kernel.org (Postfix) with ESMTPS id B9088606A336;
	Tue, 20 Oct 2026 17:12:07 +0000 (UTC)
X-Received: by smtp.kernel.org (Postfix) with ESMTPSA id DE63A1F000C9;
	Tue, 20 Oct 2026 17:12:06 +0000 (UTC)
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org;
	s=k20260515; t=1792516327;
	bh=o3WJRJcePloVZq4AExePZBL3lYY5STJ5wC5g0j9QDrc=;
	h=Subject:From:To:Cc:Date:In-Reply-To:References;
	b=YfuCXTLEJo5Auvh+BJQePp4Hi1Q1e+SQiXY4c0RnpEqIJUvljLBA59Dmej+qkrTCn
	 uyxr6l5IXgexqoM2kH5TKf0oYulDUTooMyfTaEuY35IAmjH5ckZktU0RkRR2fGGtXM
	 yg/+cWAjwcIbVANWF7+5rs6b5VrUmkma+CEYh4tlVEc+1uwkh9T8XMj8565mJIm5nP
	 MGwz+WkB3DRpGc7EvoMaWf3VdMqK7DWesZpUV2k8QX/aFfusO02zFtUIB/UFcC6YUl
	 QGsQO5Eumtonm1722ohiW8bEsm6bWBDbun5BWM2+7DH7QH4uNyG7/PAYw2Ogsh00AD
	 PRrVJURMxz+vA==
X-Cc: netdev@vger.kernel.org, davem@davemloft.net, kuba@kernel.org,
 linux-can@vger.kernel.org, kernel@pengutronix.de, 1579567540@qq.com,
 stable@vger.kernel.org, mailhol@kernel.org
X-In-Reply-To: <20261019170834.1727150-12-mkl@pengutronix.de>
X-sashiko-severity: Medium
X-Mailing-List: netdev@vger.kernel.org
X-List-Post: <mailto:netdev@vger.kernel.org>
X-List-Help: <mailto:netdev+help@vger.kernel.org>
X-List-Subscribe: <mailto:netdev+subscribe@vger.kernel.org>
X-List-Unsubscribe: <mailto:netdev+unsubscribe@vger.kernel.org>
X-List-Owner: <mailto:netdev+owner@vger.kernel.org>
X-Precedence: list