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: <redacted>
Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
Link: https://patch.msgid.link/20261117-can-bcmlock-memcg-v6-2-6826a6227d36@hartkopp.net (local)
Cc: Vincent Mailhol <mailhol@kernel.org>
Cc: Marc Kleine-Budde <mkl@pengutronix.de>
Cc: Oleksij Rempel <o.rempel@pengutronix.de>
Cc: Zhu Ling <redacted>
Cc: <redacted>
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