netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Eric Dumazet <edumazet@google.com>
To: "David S . Miller" <davem@davemloft.net>,
	Jakub Kicinski <kuba@kernel.org>,
	 Paolo Abeni <pabeni@redhat.com>
Cc: netdev@vger.kernel.org, eric.dumazet@gmail.com,
	 Eric Dumazet <edumazet@google.com>
Subject: [PATCH v3 net-next 05/13] net-sysfs: convert netdev_show() to RCU
Date: Fri,  9 Feb 2024 20:34:20 +0000	[thread overview]
Message-ID: <20240209203428.307351-6-edumazet@google.com> (raw)
In-Reply-To: <20240209203428.307351-1-edumazet@google.com>

Make clear dev_isalive() can be called with RCU protection.

Then convert netdev_show() to RCU, to remove dev_base_lock
dependency.

Also add RCU to broadcast_show().

Signed-off-by: Eric Dumazet <edumazet@google.com>
---
 net/core/net-sysfs.c | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)

diff --git a/net/core/net-sysfs.c b/net/core/net-sysfs.c
index f4c2b82674951bbeefd880ca22c54e6a32c9f988..678e4be690821c5cdb933f5e91af2247ebecb830 100644
--- a/net/core/net-sysfs.c
+++ b/net/core/net-sysfs.c
@@ -34,10 +34,10 @@ static const char fmt_dec[] = "%d\n";
 static const char fmt_ulong[] = "%lu\n";
 static const char fmt_u64[] = "%llu\n";
 
-/* Caller holds RTNL or dev_base_lock */
+/* Caller holds RTNL, RCU or dev_base_lock */
 static inline int dev_isalive(const struct net_device *dev)
 {
-	return dev->reg_state <= NETREG_REGISTERED;
+	return READ_ONCE(dev->reg_state) <= NETREG_REGISTERED;
 }
 
 /* use same locking rules as GIF* ioctl's */
@@ -48,10 +48,10 @@ static ssize_t netdev_show(const struct device *dev,
 	struct net_device *ndev = to_net_dev(dev);
 	ssize_t ret = -EINVAL;
 
-	read_lock(&dev_base_lock);
+	rcu_read_lock();
 	if (dev_isalive(ndev))
 		ret = (*format)(ndev, buf);
-	read_unlock(&dev_base_lock);
+	rcu_read_unlock();
 
 	return ret;
 }
@@ -60,7 +60,7 @@ static ssize_t netdev_show(const struct device *dev,
 #define NETDEVICE_SHOW(field, format_string)				\
 static ssize_t format_##field(const struct net_device *dev, char *buf)	\
 {									\
-	return sysfs_emit(buf, format_string, dev->field);		\
+	return sysfs_emit(buf, format_string, READ_ONCE(dev->field));		\
 }									\
 static ssize_t field##_show(struct device *dev,				\
 			    struct device_attribute *attr, char *buf)	\
@@ -161,10 +161,13 @@ static ssize_t broadcast_show(struct device *dev,
 			      struct device_attribute *attr, char *buf)
 {
 	struct net_device *ndev = to_net_dev(dev);
+	int ret = -EINVAL;
 
+	rcu_read_lock();
 	if (dev_isalive(ndev))
-		return sysfs_format_mac(buf, ndev->broadcast, ndev->addr_len);
-	return -EINVAL;
+		ret = sysfs_format_mac(buf, ndev->broadcast, ndev->addr_len);
+	rcu_read_unlock();
+	return ret;
 }
 static DEVICE_ATTR_RO(broadcast);
 
-- 
2.43.0.687.g38aa6559b0-goog


  parent reply	other threads:[~2024-02-09 20:35 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-09 20:34 [PATCH v3 net-next 00/13] net: complete dev_base_lock removal Eric Dumazet
2024-02-09 20:34 ` [PATCH v3 net-next 01/13] net: annotate data-races around dev->name_assign_type Eric Dumazet
2024-02-09 20:34 ` [PATCH v3 net-next 02/13] ip_tunnel: annotate data-races around t->parms.link Eric Dumazet
2024-02-09 20:34 ` [PATCH v3 net-next 03/13] dev: annotate accesses to dev->link Eric Dumazet
2024-02-09 20:34 ` [PATCH v3 net-next 04/13] net: convert dev->reg_state to u8 Eric Dumazet
2024-02-09 20:34 ` Eric Dumazet [this message]
2024-02-09 20:34 ` [PATCH v3 net-next 06/13] net-sysfs: use dev_addr_sem to remove races in address_show() Eric Dumazet
2024-02-13  1:58   ` Jakub Kicinski
2024-02-13  6:08     ` Eric Dumazet
2024-02-09 20:34 ` [PATCH v3 net-next 07/13] net-sysfs: convert dev->operstate reads to lockless ones Eric Dumazet
2024-02-09 20:34 ` [PATCH v3 net-next 08/13] net-sysfs: convert netstat_show() to RCU Eric Dumazet
2024-02-09 20:34 ` [PATCH v3 net-next 09/13] net: remove stale mentions of dev_base_lock in comments Eric Dumazet
2024-02-09 20:34 ` [PATCH v3 net-next 10/13] net: add netdev_set_operstate() helper Eric Dumazet
2024-02-09 20:34 ` [PATCH v3 net-next 11/13] net: remove dev_base_lock from do_setlink() Eric Dumazet
2024-02-09 20:34 ` [PATCH v3 net-next 12/13] net: remove dev_base_lock from register_netdevice() and friends Eric Dumazet
2024-02-09 20:34 ` [PATCH v3 net-next 13/13] net: remove dev_base_lock Eric Dumazet

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=20240209203428.307351-6-edumazet@google.com \
    --to=edumazet@google.com \
    --cc=davem@davemloft.net \
    --cc=eric.dumazet@gmail.com \
    --cc=kuba@kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.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).