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 v2 net-next 04/13] net: convert dev->reg_state to standard enum
Date: Thu,  8 Feb 2024 14:11:45 +0000	[thread overview]
Message-ID: <20240208141154.1131622-5-edumazet@google.com> (raw)
In-Reply-To: <20240208141154.1131622-1-edumazet@google.com>

Prepares things so that dev->reg_state reads can be lockless,
by adding WRITE_ONCE() on write side.

READ_ONCE()/WRITE_ONCE() do not support bitfields.

v2: use a standard enum, so that try_cmpxchg() added
    in a followup patch will work on all arches.
    ( https://lore.kernel.org/oe-kbuild-all/202402081918.OLyGaea3-lkp@intel.com/ )

Signed-off-by: Eric Dumazet <edumazet@google.com>
---
 include/linux/netdevice.h | 25 +++++++++++++++----------
 net/core/dev.c            |  8 ++++----
 2 files changed, 19 insertions(+), 14 deletions(-)

diff --git a/include/linux/netdevice.h b/include/linux/netdevice.h
index 07cefa32eafa93dd1a4602de892d0ee1cbf2e22b..5f138025d5f189cd5ab7bb0434205b66d30e60e7 100644
--- a/include/linux/netdevice.h
+++ b/include/linux/netdevice.h
@@ -1815,6 +1815,15 @@ enum netdev_stat_type {
 	NETDEV_PCPU_STAT_DSTATS, /* struct pcpu_dstats */
 };
 
+enum netdev_reg_state {
+	NETREG_UNINITIALIZED = 0,
+	NETREG_REGISTERED,	/* completed register_netdevice */
+	NETREG_UNREGISTERING,	/* called unregister_netdevice */
+	NETREG_UNREGISTERED,	/* completed unregister todo */
+	NETREG_RELEASED,	/* called free_netdev */
+	NETREG_DUMMY,		/* dummy device for NAPI poll */
+};
+
 /**
  *	struct net_device - The DEVICE structure.
  *
@@ -2372,22 +2381,16 @@ struct net_device {
 
 	struct list_head	link_watch_list;
 
-	enum { NETREG_UNINITIALIZED=0,
-	       NETREG_REGISTERED,	/* completed register_netdevice */
-	       NETREG_UNREGISTERING,	/* called unregister_netdevice */
-	       NETREG_UNREGISTERED,	/* completed unregister todo */
-	       NETREG_RELEASED,		/* called free_netdev */
-	       NETREG_DUMMY,		/* dummy device for NAPI poll */
-	} reg_state:8;
+	enum netdev_reg_state reg_state;
 
 	bool dismantle;
+	bool needs_free_netdev;
 
 	enum {
 		RTNL_LINK_INITIALIZED,
 		RTNL_LINK_INITIALIZING,
 	} rtnl_link_state:16;
 
-	bool needs_free_netdev;
 	void (*priv_destructor)(struct net_device *dev);
 
 	/* mid-layer private */
@@ -5254,7 +5257,9 @@ static inline const char *netdev_name(const struct net_device *dev)
 
 static inline const char *netdev_reg_state(const struct net_device *dev)
 {
-	switch (dev->reg_state) {
+	enum netdev_reg_state reg_state = READ_ONCE(dev->reg_state);
+
+	switch (reg_state) {
 	case NETREG_UNINITIALIZED: return " (uninitialized)";
 	case NETREG_REGISTERED: return "";
 	case NETREG_UNREGISTERING: return " (unregistering)";
@@ -5263,7 +5268,7 @@ static inline const char *netdev_reg_state(const struct net_device *dev)
 	case NETREG_DUMMY: return " (dummy)";
 	}
 
-	WARN_ONCE(1, "%s: unknown reg_state %d\n", dev->name, dev->reg_state);
+	WARN_ONCE(1, "%s: unknown reg_state %d\n", dev->name, reg_state);
 	return " (unknown)";
 }
 
diff --git a/net/core/dev.c b/net/core/dev.c
index c0db7408fa9f9a3ae9b5b83d34b50663604a5f99..89dbba691c9db83102d15074b6ea96312689be10 100644
--- a/net/core/dev.c
+++ b/net/core/dev.c
@@ -10268,7 +10268,7 @@ int register_netdevice(struct net_device *dev)
 
 	ret = netdev_register_kobject(dev);
 	write_lock(&dev_base_lock);
-	dev->reg_state = ret ? NETREG_UNREGISTERED : NETREG_REGISTERED;
+	WRITE_ONCE(dev->reg_state, ret ? NETREG_UNREGISTERED : NETREG_REGISTERED);
 	write_unlock(&dev_base_lock);
 	if (ret)
 		goto err_uninit_notify;
@@ -10559,7 +10559,7 @@ void netdev_run_todo(void)
 		}
 
 		write_lock(&dev_base_lock);
-		dev->reg_state = NETREG_UNREGISTERED;
+		WRITE_ONCE(dev->reg_state, NETREG_UNREGISTERED);
 		write_unlock(&dev_base_lock);
 		linkwatch_sync_dev(dev);
 	}
@@ -10979,7 +10979,7 @@ void free_netdev(struct net_device *dev)
 	}
 
 	BUG_ON(dev->reg_state != NETREG_UNREGISTERED);
-	dev->reg_state = NETREG_RELEASED;
+	WRITE_ONCE(dev->reg_state, NETREG_RELEASED);
 
 	/* will free via device release */
 	put_device(&dev->dev);
@@ -11069,7 +11069,7 @@ void unregister_netdevice_many_notify(struct list_head *head,
 		/* And unlink it from device chain. */
 		write_lock(&dev_base_lock);
 		unlist_netdevice(dev, false);
-		dev->reg_state = NETREG_UNREGISTERING;
+		WRITE_ONCE(dev->reg_state, NETREG_UNREGISTERING);
 		write_unlock(&dev_base_lock);
 	}
 	flush_all_backlogs();
-- 
2.43.0.594.gd9cf4e227d-goog


  parent reply	other threads:[~2024-02-08 14:12 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-08 14:11 [PATCH v2 net-next 00/13] net: complete dev_base_lock removal Eric Dumazet
2024-02-08 14:11 ` [PATCH v2 net-next 01/13] net: annotate data-races around dev->name_assign_type Eric Dumazet
2024-02-08 14:11 ` [PATCH v2 net-next 02/13] ip_tunnel: annotate data-races around t->parms.link Eric Dumazet
2024-02-08 14:11 ` [PATCH v2 net-next 03/13] dev: annotate accesses to dev->link Eric Dumazet
2024-02-08 14:11 ` Eric Dumazet [this message]
2024-02-08 14:11 ` [PATCH v2 net-next 05/13] net-sysfs: convert netdev_show() to RCU Eric Dumazet
2024-02-08 14:11 ` [PATCH v2 net-next 06/13] net-sysfs: use dev_addr_sem to remove races in address_show() Eric Dumazet
2024-02-08 14:11 ` [PATCH v2 net-next 07/13] net-sysfs: convert dev->operstate reads to lockless ones Eric Dumazet
2024-02-08 14:11 ` [PATCH v2 net-next 08/13] net-sysfs: convert netstat_show() to RCU Eric Dumazet
2024-02-08 14:11 ` [PATCH v2 net-next 09/13] net: remove stale mentions of dev_base_lock in comments Eric Dumazet
2024-02-08 14:11 ` [PATCH v2 net-next 10/13] net: add netdev_set_operstate() helper Eric Dumazet
2024-02-09 19:13   ` kernel test robot
2024-02-09 19:58     ` Eric Dumazet
2024-02-08 14:11 ` [PATCH v2 net-next 11/13] net: remove dev_base_lock from do_setlink() Eric Dumazet
2024-02-08 14:11 ` [PATCH v2 net-next 12/13] net: remove dev_base_lock from register_netdevice() and friends Eric Dumazet
2024-02-08 14:11 ` [PATCH v2 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=20240208141154.1131622-5-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).