🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.kernel.netdev
Article #16601

[PATCH net 2/9] can: bcm: do not remove the procfs entry in bcm_notify()

#16601
From: Marc Kleine-Budde
Date: Sun, 15 Nov 2026 09:54
129 lines
15654 bytes
From: Oliver Hartkopp <socketcan@hartkopp.net>

bcm_notify() removes the procfs entry of a CAN_BCM socket that is bound
to a vanishing CAN device with remove_proc_entry() while holding RTNL.
remove_proc_entry() waits for all running readers of this entry. A
reader of /proc/net/can-bcm/<inode> that stalls in its copy to user
space, e.g. on a FUSE backed mapping, therefore blocks RTNL on the
unregistration or namespace change of the bound CAN device.

The entry has been removed in bcm_notify() so that a later
bcm_connect() of the unbound socket can register it again without a
"proc_dir_entry already registered" warning.

The CAN_BCM socket itself stays usable after a NETDEV_UNREGISTER:
bcm_notify() only unbinds it and reports ENODEV, so the application can
connect() the socket again, e.g. to a re-created CAN device. The name
of the procfs entry is the inode number of the socket, which does not
change until the socket is closed.

Keep the procfs entry until bcm_release() instead, which removes it
after the socket has left the notifier list and without holding RTNL.
bcm_connect() only creates the entry, if it does not exist yet.
bcm_proc_show() reports an unbound socket as "unbound" instead of
showing the cleared ifindex as "any".

As bcm_proc_show() can now run while the socket is bound or unbound, it
reads bo->ifindex locklessly with READ_ONCE() and the writers use
WRITE_ONCE(). The NETDEV_DOWN handling in bcm_notify(), which runs
without lock_sock(), reads bo->bound and bo->ifindex with READ_ONCE()
as well. bo->ifindex is still only modified under lock_sock(). A
concurrent bind or unbind may show an intermediate state in the procfs
output, which is acceptable for this debug information.

Fixes: 76fe372ccb81 ("can: bcm: Remove proc entry when dev is unregistered.")
Cc: stable@vger.kernel.org
Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
Link: https://patch.msgid.link/20261113-can-bcmlock-memcg-v5-2-634c0e04bc72@hartkopp.net
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
---
 net/can/bcm.c | 33 +++++++++++++++------------------
 1 file changed, 15 insertions(+), 18 deletions(-)

diff --git a/net/can/bcm.c b/net/can/bcm.c
index 3f4c8d828337..1595b7720c98 100644
--- a/net/can/bcm.c
+++ b/net/can/bcm.c
@@ -219,11 +219,16 @@ static int bcm_proc_show(struct seq_file *m, void *v)
 	struct net *net = m->private;
 	struct sock *sk = (struct sock *)pde_data(m->file->f_inode);
 	struct bcm_sock *bo = bcm_sk(sk);
+	int ifindex = READ_ONCE(bo->ifindex);
 	struct bcm_op *op;
 
 	seq_printf(m, ">>> sock inode %s", bo->procname);
 	seq_printf(m, " / dropped %lu", bo->dropped_usr_msgs);
-	seq_printf(m, " / bound %s", bcm_proc_getifname(net, ifname, bo->ifindex));
+	if (READ_ONCE(bo->bound))
+		seq_printf(m, " / bound %s",
+			   bcm_proc_getifname(net, ifname, ifindex));
+	else
+		seq_puts(m, " / unbound");
 	seq_printf(m, " <<<\n");
 
 	rcu_read_lock();
@@ -1866,19 +1871,9 @@ static void bcm_notify(struct bcm_sock *bo, unsigned long msg,
 
 		/* remove device reference, if this is our bound device */
 		if (bo->bound && bo->ifindex == dev->ifindex) {
-#if IS_ENABLED(CONFIG_PROC_FS)
-			if (sock_net(sk)->can.bcmproc_dir && bo->bcm_proc_read) {
-				remove_proc_entry(bo->procname, sock_net(sk)->can.bcmproc_dir);
-				bo->bcm_proc_read = NULL;
-			}
-#endif
-			/* Paired with the lockless fast-path check in
-			 * bcm_sendmsg(); bo->ifindex itself is only ever
-			 * accessed under lock_sock() so it needs no
-			 * annotation.
-			 */
+			/* pairs with the lockless check in bcm_sendmsg() */
 			WRITE_ONCE(bo->bound, 0);
-			bo->ifindex = 0;
+			WRITE_ONCE(bo->ifindex, 0);
 			notify_enodev = 1;
 		}
 
@@ -1892,7 +1887,8 @@ static void bcm_notify(struct bcm_sock *bo, unsigned long msg,
 		break;
 
 	case NETDEV_DOWN:
-		if (bo->bound && bo->ifindex == dev->ifindex) {
+		if (READ_ONCE(bo->bound) &&
+		    READ_ONCE(bo->ifindex) == dev->ifindex) {
 			sk->sk_err = ENETDOWN;
 			if (!sock_flag(sk, SOCK_DEAD))
 				sk_error_report(sk);
@@ -2013,7 +2009,7 @@ static int bcm_release(struct socket *sock)
 	/* remove device reference */
 	if (bo->bound) {
 		WRITE_ONCE(bo->bound, 0);
-		bo->ifindex = 0;
+		WRITE_ONCE(bo->ifindex, 0);
 	}
 
 	sock_orphan(sk);
@@ -2060,16 +2056,17 @@ static int bcm_connect(struct socket *sock, struct sockaddr_unsized *uaddr, int
 			goto fail;
 		}
 
-		bo->ifindex = dev->ifindex;
+		WRITE_ONCE(bo->ifindex, dev->ifindex);
 		dev_put(dev);
 
 	} else {
 		/* no interface reference for ifindex = 0 ('any' CAN device) */
-		bo->ifindex = 0;
+		WRITE_ONCE(bo->ifindex, 0);
 	}
 
 #if IS_ENABLED(CONFIG_PROC_FS)
-	if (net->can.bcmproc_dir) {
+	/* create the procfs entry once, it is removed in bcm_release() */
+	if (net->can.bcmproc_dir && !bo->bcm_proc_read) {
 		/* use unique socket inode number as filename */
 		snprintf(bo->procname, sizeof(bo->procname),
 			 "%llu", sock_i_ino(sk));
-- 
2.53.0
Technical Details
Message-ID: <20261115095746.709371-3-mkl@pengutronix.de>
Path: mbox-mo1.pugleaf.net!news-lax.pugleaf.net!srl.newsdeef.eu!news.mb-net.net!open-news-network.org!2.eu.feeder.erje.net!feeder.erje.net!feeder1.feed.ams11.usenet.farm!feed.usenet.farm!peer03.ams4!peer.am4.highwinds-media.com!news.highwinds-media.com!feeder.usenetexpress.com!tr3.eu1.usenetexpress.com!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-363643-linux-kernel-netdev=dewy.ru
References: <20261115095746.709371-1-mkl@pengutronix.de>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-lax.pugleaf.net!srl.newsdeef.eu!news.mb-net.net!open-news-network.org!2.eu.feeder.erje.net!feeder.erje.net!feeder1.feed.ams11.usenet.farm!feed.usenet.farm!peer03.ams4!peer.am4.highwinds-media.com!news.highwinds-media.com!feeder.usenetexpress.com!tr3.eu1.usenetexpress.com!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-363643-linux-kernel-netdev=dewy.ru
From: mkl@pengutronix.de (Marc Kleine-Budde)
Newsgroups: linux.kernel.netdev
Subject: [PATCH net 2/9] can: bcm: do not remove the procfs entry in bcm_notify()
Date: Sun, 15 Nov 2026 10:54:53 +0100
Organization: Usenet Network
Message-ID: <20261115095746.709371-3-mkl@pengutronix.de>
References: <20261115095746.709371-1-mkl@pengutronix.de>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Injection-Info: usenet.network; logging-data="1767559"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:MaPyBsBAcxf5vJWFEFpZZBmEilg= sha256:gWnbcAClygD0UD0R3DjAzDiilNlwgFngVFZW1EVljlw= sha1:EBDXNfF//r6toyYkI9Ebh2kHwjQ= sha256:BDIkBLuwtej/f+TwBRjb5wj0iZZgvTdQnx0qg29ndFI=
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 2885A11498F; Sun, 15 Nov 2026 12:58:22 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -3.051
X-Spam-Status: No, score=-3.051 tagged_above=-9999 required=6.31 tests=[DCC_REPUT_70_89=0.1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, HEADER_FROM_DIFFERENT_DOMAINS=0.249, 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=pengutronix.de
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 kkURS9OPT20P; Sun, 15 Nov 2026 12:58:18 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.234.253.10; helo=sea.lore.kernel.org; envelope-from=netdev+bounces-363643-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN>
X-Authentication-Results: OpenDMARC; dmarc=none (p=none dis=none) header.from=pengutronix.de
X-Received: from sea.lore.kernel.org (sea.lore.kernel.org [172.234.253.10]) by mail.dewy.ru (Postfix) with ESMTPS id B05BF11498C; Sun, 15 Nov 2026 12:58:08 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92]) by sea.lore.kernel.org (Postfix) with ESMTP id 45D7F2D2D1; Sun, 15 Nov 2026 09:57:57 +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=3; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1794736674; b=QRU6w1unSfFsc2e+aRAxNpuOOh8ErD+cFOdGcQbjO2wiUroZqI6LQJsZJ8Rz9rXZmr7H kfPjIaRPjpzfnlYKZLptEbFjsKX7iHR+pZrOGo/u4dOY5vn5Exdei370F5cpQc4Z2g0hQ uiu52syfV2Vs9ndOMOKVw7APTRU2gacNkMHojrTfQxOEMBAhOwVFvkYPWAtnnnW8sE7m6 ZfVbFTVBZeQYFk4/7hUZE1bJjX41xwVulV5GvIkME40UghLjHM/pYiYdP52pErJ29gaU+ LRKckQwoDzj8I5ntHNTducgVHaQuabuR4pZSwPbO30wKXXeTpeb28ScYY3MjCKnlRvg==
X-ARC-Message-Signature: i=3; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1794736674; h=X-Original-To:Received:DMARC-Filter:Received:DKIM-Signature:Received: Received:From:To:Cc:Subject:Date:Message-ID:X-Mailer:In-Reply-To: References:X-Mailing-List:List-Id:List-Post:List-Help:List-Subscribe: List-Unsubscribe:List-Owner:Precedence:MIME-Version: Content-Transfer-Encoding; bh=Jasd+7eTfCu8M5HIID8UopD0ImkUg5IwkLLHNVAPX/U=; b=rQ5B6rqPEgq36kD13fuyJXQ3V4AKo3MNMRU+vS1RIj+s+sg0n0Qg7AL47gChkNdv2O4X DGd6Hp/LXZPpgWD3glDIJeR1/I+AjB9XPG7hu7IQWwzAeCXT/GVNA5IZecIHIM64uZ9E/ pK+hHIE2hpxQO1ht5YmSmSrf+wAWj5m+RjqlmASPP206eB/h4Gmcy/AqLh0dj+XWiZFKo 5hsXlxFF563WrKDA49KAJY9a/hxqWBRuNquQBxMc90n5N0mjIhjiiUNEDZlPouL0Tlgj1 QI3IhMl/N9rbv4W7u0FldROmHz+h8h5N+0QVPl1UMn0KksK6/2SoV5gaTBURbDLqkag==
X-ARC-Authentication-Results: i=3; smtp.subspace.kernel.org; dkim=pass header.d=pengutronix.de header.i=@pengutronix.de header.a=rsa-sha256 header.s=20260414 header.b=OE7L21AX; dmarc=none header.from=pengutronix.de; spf=pass smtp.mailfrom=pengutronix.de; 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 AB6D5606A88F; Sun, 15 Nov 2026 09:57:54 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pengutronix.de header.i=@pengutronix.de header.a=rsa-sha256 header.s=20260414 header.b=OE7L21AX
X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=185.203.200.13
X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1794736673; b=bB7WC0Ldiq1MjjrdWRpm8BXdyRSZr9XKOWzkOxphplBak2DLXmXSNpHx5rJNHbZlo+Zv +fZafwEblvVCjfbqVhL3Q1EQNef2FnjNBCIC2NL2JAOMrD8ovoz9C+nFFelF+Cvl3VdJh h5judKPMyt+sxV7QcXlPaOz05ViEq80KdepDB7sOKG1VZYGjldy3wHX7q/ywV0ncbMCRF TosyIRARBLulqd5fqdEoB+oYvVSdSwjZzdmsKY674wp1nDa7L066Io5RrwJlDZN720HC0 lemmv+LpMxtNyvApFEvlT9IzpdaERz7hBtBppKwq49hPBT2vMde97xCRf+j72dQ79gg==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1794736673; h=DMARC-Filter:Received:DKIM-Signature:Received:Received:From:To:Cc: Subject:Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=Jasd+7eTfCu8M5HIID8UopD0ImkUg5IwkLLHNVAPX/U=; b=tfBZ7yxm0Ii1vm0/MuslThrWIRP7+pxNxZDlqbCVFNPYRZkMUdGwuAM5RwgxV7phbyuU 4fIphZC/nFUYihk37FOSX4QRtsNGHAyMN4knt7XK9ZavLhhVhX0YX6TwDp2jQk3jPDI0A larChRb1d6mPnAwGj0PciYHt/R2TjL1xFXVT+V+TN1UNXP2nxPoAB2AIx3kTUVxT1IWNJ H9VKfrK+YgtiPx1A3kcIOoAFO71hus8bBUvv/iKNAZX8qF04blRFZoURVZTTau4TiB0AF QEOT4bHucIUhkjAzaodFRDQD5AlUAatVGnTxujEugU9qyF818g3p+ZbW/aHCKtl9PWQ==
X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=none header.from=pengutronix.de; spf=pass smtp.mailfrom=pengutronix.de; dkim=pass header.d=pengutronix.de header.i=@pengutronix.de header.a=rsa-sha256 header.s=20260414 header.b=OE7L21AX; arc=pass header.oldest-pass=0 smtp.remote-ip=185.203.200.13
X-Received: from mx1.white.stw.pengutronix.de (mx1.white.stw.pengutronix.de [185.203.200.13]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 68EC7606A31D; Sun, 15 Nov 2026 09:57:52 +0000 (UTC)
X-DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 68EC7606A31D
X-Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de
X-Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pengutronix.de
X-Received: from drehscheibe.grey.stw.pengutronix.de (drehscheibe.grey.stw.pengutronix.de [IPv6:2a0a:edc0:0:c01:1d::a2]) (Authenticated sender: relay-from-drehscheibe.grey.stw.pengutronix.de) by mx1.white.stw.pengutronix.de (Postfix) with ESMTPSA id 5FA922059F2; Sun, 15 Nov 2026 10:57:50 +0100 (CET)
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pengutronix.de; s=20260414; t=1794736670; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Jasd+7eTfCu8M5HIID8UopD0ImkUg5IwkLLHNVAPX/U=; b=OE7L21AXfLKapkD8TcKOm7FjJUpuRZ7PjjRmDiGYUpUt4UaJ9tSmXxwyW/oWyguoi+2VXj RCEXFLt+noW4IoNOz4a3siTM3xUrXjZfmAQ8nTncmlAjEF61P44bb9IGU9eVHc12IaMEfX 0MN806sGQ9C37Hi1iVO+I5TmRm/fD6oQFR/SwtOpLmZtUe4C/tUYjsyqIm6Q+TTNiodjio YZZLThTrXgqtSuQH2Yt7EEqnSCS3eBouPOCABNR+twUrpvE17CVg47HHSdek0zhgU35rNM tr+xAdKYkB7EmvzOAZ8LoThVFy81VCX2/Dze8dTD74x7oZuJcUgC3DY6L91iNw==
X-ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=pengutronix.de; s=20260414; t=1794736670; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Jasd+7eTfCu8M5HIID8UopD0ImkUg5IwkLLHNVAPX/U=; b=Ur7IH2iCFIkFyhz6msxlVBDuR+jnoVFO9VSSwNhozA+ULusbnxXX0Mu9qR8D4ZodC812PY mWarZR/397XDQ+JzkXEzakO1JONPuTZCA3wQVQBWwvJIbvoBXwCiBFwwSdHEuH+AuK4MIr 5X9UKRaO6cKPUKZjV3WL5vJ3b8RstCkQbBT541dxl4C7ONTyAhLKJqituCKWG7qZQ6dELi SZgzJlTM6mDncNX2WhVHqJtMudLDX/CMcyNdxLCbfhXLtWN8B9YHLioJ6J3nITlZJtkx/s uLfTtoxeQD0hsbUE0ZYhd+mmO0rp5Xq75NBm6y6Dnet7FxiQsOiaykzS9Txx5w==
X-ARC-Seal: i=1; s=20260414; d=pengutronix.de; t=1794736670; a=rsa-sha256; cv=none; b=JFzkMM8n3fkuLEpfoZRJMlArS107VKxywZxBRRXZO0nOUlLruzLCJVdK9nyn5aIxBvxg2S QXkQbsNKbUJrviz5r6qZ2ea/9LpcfZgHEc/1Vkxgwa1xYv2x6Ct6hqxbiaYx6lClbaZRmK ejo2Lf0Tik3K2Al8FNJudBtpQTtI1VqiWNBv9/G5XJVc7LprxVPZ6b7NvBnUt3W0w3m0aS O2eCFlLMhQAjByWi+Kz0D6NubmhNyGnuBHwpjEucptxsxUjZ4PeEElj050Lw8ulO+UBZ9D +l0Kk3fcq6o6bAOEzU2n36i+u0Y0xueN8kRrjb4DsuvuUeZ6JrHh/8Y+/FiStA==
X-ARC-Authentication-Results: i=1; ORIGINATING; auth=pass smtp.auth=relay-from-drehscheibe.grey.stw.pengutronix.de smtp.mailfrom=mkl@pengutronix.de
X-Received: from moin.white.stw.pengutronix.de ([2a0a:edc0:0:b01:1d::7b] helo=bjornoya.blackshift.org) by drehscheibe.grey.stw.pengutronix.de with esmtps  (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from <mkl@pengutronix.de>) id 1xSWze-004GWH-0q; Sun, 15 Nov 2026 10:57:50 +0100
X-Received: from blackshift.org (p4ffb23c7.dip0.t-ipconnect.de [79.251.35.199]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519MLKEM768 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) (Authenticated sender: mkl-all@blackshift.org) by smtp.blackshift.org (Postfix) with ESMTPSA id 03D105EC24E; Sun, 15 Nov 2026 09:57:50 +0000 (UTC)
X-Cc: davem@davemloft.net, kuba@kernel.org, linux-can@vger.kernel.org, kernel@pengutronix.de, Oliver Hartkopp <socketcan@hartkopp.net>, stable@vger.kernel.org, Marc Kleine-Budde <mkl@pengutronix.de>
X-Mailer: git-send-email 2.53.0
X-In-Reply-To: <20261115095746.709371-1-mkl@pengutronix.de>
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
X-Received-Bytes: 15368