netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v1 net-next] dev: Pass netdevice_tracker to dev_get_by_flags_rcu().
@ 2025-07-09 19:01 Kuniyuki Iwashima
  2025-07-10  0:16 ` Jakub Kicinski
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Kuniyuki Iwashima @ 2025-07-09 19:01 UTC (permalink / raw)
  To: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	Andrew Lunn, David Ahern
  Cc: Simon Horman, Kuniyuki Iwashima, Kuniyuki Iwashima, netdev

This is a follow-up for commit eb1ac9ff6c4a5 ("ipv6: anycast: Don't
hold RTNL for IPV6_JOIN_ANYCAST.").

We should not add a new device lookup API without netdevice_tracker.

Let's pass netdevice_tracker to dev_get_by_flags_rcu() and rename it
with netdev_ prefix to match other newer APIs.

Note that we always use GFP_ATOMIC for netdev_hold() as it's expected
to be called under RCU.

Suggested-by: Jakub Kicinski <kuba@kernel.org>
Link: https://lore.kernel.org/netdev/20250708184053.102109f6@kernel.org/
Signed-off-by: Kuniyuki Iwashima <kuniyu@google.com>
---
 include/linux/netdevice.h | 4 ++--
 net/core/dev.c            | 8 ++++----
 net/ipv6/anycast.c        | 7 ++++---
 3 files changed, 10 insertions(+), 9 deletions(-)

diff --git a/include/linux/netdevice.h b/include/linux/netdevice.h
index a80d21a146123..ec23cee5245d7 100644
--- a/include/linux/netdevice.h
+++ b/include/linux/netdevice.h
@@ -3332,8 +3332,6 @@ int dev_get_iflink(const struct net_device *dev);
 int dev_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb);
 int dev_fill_forward_path(const struct net_device *dev, const u8 *daddr,
 			  struct net_device_path_stack *stack);
-struct net_device *dev_get_by_flags_rcu(struct net *net, unsigned short flags,
-					unsigned short mask);
 struct net_device *dev_get_by_name(struct net *net, const char *name);
 struct net_device *dev_get_by_name_rcu(struct net *net, const char *name);
 struct net_device *__dev_get_by_name(struct net *net, const char *name);
@@ -3396,6 +3394,8 @@ struct net_device *netdev_get_by_index(struct net *net, int ifindex,
 				       netdevice_tracker *tracker, gfp_t gfp);
 struct net_device *netdev_get_by_name(struct net *net, const char *name,
 				      netdevice_tracker *tracker, gfp_t gfp);
+struct net_device *netdev_get_by_flags_rcu(struct net *net, netdevice_tracker *tracker,
+					   unsigned short flags, unsigned short mask);
 struct net_device *dev_get_by_index_rcu(struct net *net, int ifindex);
 void netdev_copy_name(struct net_device *dev, char *name);
 
diff --git a/net/core/dev.c b/net/core/dev.c
index e365b099484ec..48fa8b9836016 100644
--- a/net/core/dev.c
+++ b/net/core/dev.c
@@ -1267,7 +1267,7 @@ struct net_device *dev_getfirstbyhwtype(struct net *net, unsigned short type)
 EXPORT_SYMBOL(dev_getfirstbyhwtype);
 
 /**
- * dev_get_by_flags_rcu - find any device with given flags
+ * netdev_get_by_flags_rcu - find any device with given flags
  * @net: the applicable net namespace
  * @if_flags: IFF_* values
  * @mask: bitmask of bits in if_flags to check
@@ -1277,14 +1277,14 @@ EXPORT_SYMBOL(dev_getfirstbyhwtype);
  * Context: rcu_read_lock() must be held.
  * Returns: NULL if a device is not found or a pointer to the device.
  */
-struct net_device *dev_get_by_flags_rcu(struct net *net, unsigned short if_flags,
-					unsigned short mask)
+struct net_device *netdev_get_by_flags_rcu(struct net *net, netdevice_tracker *tracker,
+					   unsigned short if_flags, unsigned short mask)
 {
 	struct net_device *dev;
 
 	for_each_netdev_rcu(net, dev) {
 		if (((READ_ONCE(dev->flags) ^ if_flags) & mask) == 0) {
-			dev_hold(dev);
+			netdev_hold(dev, tracker, GFP_ATOMIC);
 			return dev;
 		}
 	}
diff --git a/net/ipv6/anycast.c b/net/ipv6/anycast.c
index 53cf68e0242bf..fa7f0c22167b4 100644
--- a/net/ipv6/anycast.c
+++ b/net/ipv6/anycast.c
@@ -69,6 +69,7 @@ int ipv6_sock_ac_join(struct sock *sk, int ifindex, const struct in6_addr *addr)
 	struct ipv6_pinfo *np = inet6_sk(sk);
 	struct ipv6_ac_socklist *pac = NULL;
 	struct net *net = sock_net(sk);
+	netdevice_tracker dev_tracker;
 	struct net_device *dev = NULL;
 	struct inet6_dev *idev;
 	int err = 0, ishost;
@@ -112,8 +113,8 @@ int ipv6_sock_ac_join(struct sock *sk, int ifindex, const struct in6_addr *addr)
 			goto error;
 		} else {
 			/* router, no matching interface: just pick one */
-			dev = dev_get_by_flags_rcu(net, IFF_UP,
-						   IFF_UP | IFF_LOOPBACK);
+			dev = netdev_get_by_flags_rcu(net, &dev_tracker, IFF_UP,
+						      IFF_UP | IFF_LOOPBACK);
 		}
 		rcu_read_unlock();
 	}
@@ -159,7 +160,7 @@ int ipv6_sock_ac_join(struct sock *sk, int ifindex, const struct in6_addr *addr)
 error_idev:
 	in6_dev_put(idev);
 error:
-	dev_put(dev);
+	netdev_put(dev, &dev_tracker);
 
 	if (pac)
 		sock_kfree_s(sk, pac, sizeof(*pac));
-- 
2.50.0.727.gbf7dc18ff4-goog


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH v1 net-next] dev: Pass netdevice_tracker to dev_get_by_flags_rcu().
  2025-07-09 19:01 [PATCH v1 net-next] dev: Pass netdevice_tracker to dev_get_by_flags_rcu() Kuniyuki Iwashima
@ 2025-07-10  0:16 ` Jakub Kicinski
  2025-07-10  1:38   ` Kuniyuki Iwashima
  2025-07-10 21:51 ` kernel test robot
  2025-07-10 23:05 ` kernel test robot
  2 siblings, 1 reply; 5+ messages in thread
From: Jakub Kicinski @ 2025-07-10  0:16 UTC (permalink / raw)
  To: Kuniyuki Iwashima
  Cc: David S. Miller, Eric Dumazet, Paolo Abeni, Andrew Lunn,
	David Ahern, Simon Horman, Kuniyuki Iwashima, netdev

On Wed,  9 Jul 2025 19:01:32 +0000 Kuniyuki Iwashima wrote:
> diff --git a/net/ipv6/anycast.c b/net/ipv6/anycast.c
> index 53cf68e0242bf..fa7f0c22167b4 100644
> --- a/net/ipv6/anycast.c
> +++ b/net/ipv6/anycast.c
> @@ -69,6 +69,7 @@ int ipv6_sock_ac_join(struct sock *sk, int ifindex, const struct in6_addr *addr)
>  	struct ipv6_pinfo *np = inet6_sk(sk);
>  	struct ipv6_ac_socklist *pac = NULL;
>  	struct net *net = sock_net(sk);
> +	netdevice_tracker dev_tracker;
>  	struct net_device *dev = NULL;
>  	struct inet6_dev *idev;
>  	int err = 0, ishost;
> @@ -112,8 +113,8 @@ int ipv6_sock_ac_join(struct sock *sk, int ifindex, const struct in6_addr *addr)
>  			goto error;
>  		} else {
>  			/* router, no matching interface: just pick one */
> -			dev = dev_get_by_flags_rcu(net, IFF_UP,
> -						   IFF_UP | IFF_LOOPBACK);
> +			dev = netdev_get_by_flags_rcu(net, &dev_tracker, IFF_UP,
> +						      IFF_UP | IFF_LOOPBACK);
>  		}
>  		rcu_read_unlock();
>  	}
> @@ -159,7 +160,7 @@ int ipv6_sock_ac_join(struct sock *sk, int ifindex, const struct in6_addr *addr)
>  error_idev:
>  	in6_dev_put(idev);
>  error:
> -	dev_put(dev);
> +	netdev_put(dev, &dev_tracker);

Hmmm.. not sure this is legal.. We could have gotten the reference from
dev_get_by_index() or a bare dev_hold() -- I mean there are two other
ways of acquiring dev in this function. Either all or none of them
have to be tracker aware, we can't mix?
-- 
pw-bot: cr

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v1 net-next] dev: Pass netdevice_tracker to dev_get_by_flags_rcu().
  2025-07-10  0:16 ` Jakub Kicinski
@ 2025-07-10  1:38   ` Kuniyuki Iwashima
  0 siblings, 0 replies; 5+ messages in thread
From: Kuniyuki Iwashima @ 2025-07-10  1:38 UTC (permalink / raw)
  To: Jakub Kicinski
  Cc: David S. Miller, Eric Dumazet, Paolo Abeni, Andrew Lunn,
	David Ahern, Simon Horman, Kuniyuki Iwashima, netdev

On Wed, Jul 9, 2025 at 5:16 PM Jakub Kicinski <kuba@kernel.org> wrote:
>
> On Wed,  9 Jul 2025 19:01:32 +0000 Kuniyuki Iwashima wrote:
> > diff --git a/net/ipv6/anycast.c b/net/ipv6/anycast.c
> > index 53cf68e0242bf..fa7f0c22167b4 100644
> > --- a/net/ipv6/anycast.c
> > +++ b/net/ipv6/anycast.c
> > @@ -69,6 +69,7 @@ int ipv6_sock_ac_join(struct sock *sk, int ifindex, const struct in6_addr *addr)
> >       struct ipv6_pinfo *np = inet6_sk(sk);
> >       struct ipv6_ac_socklist *pac = NULL;
> >       struct net *net = sock_net(sk);
> > +     netdevice_tracker dev_tracker;
> >       struct net_device *dev = NULL;
> >       struct inet6_dev *idev;
> >       int err = 0, ishost;
> > @@ -112,8 +113,8 @@ int ipv6_sock_ac_join(struct sock *sk, int ifindex, const struct in6_addr *addr)
> >                       goto error;
> >               } else {
> >                       /* router, no matching interface: just pick one */
> > -                     dev = dev_get_by_flags_rcu(net, IFF_UP,
> > -                                                IFF_UP | IFF_LOOPBACK);
> > +                     dev = netdev_get_by_flags_rcu(net, &dev_tracker, IFF_UP,
> > +                                                   IFF_UP | IFF_LOOPBACK);
> >               }
> >               rcu_read_unlock();
> >       }
> > @@ -159,7 +160,7 @@ int ipv6_sock_ac_join(struct sock *sk, int ifindex, const struct in6_addr *addr)
> >  error_idev:
> >       in6_dev_put(idev);
> >  error:
> > -     dev_put(dev);
> > +     netdev_put(dev, &dev_tracker);
>
> Hmmm.. not sure this is legal.. We could have gotten the reference from
> dev_get_by_index() or a bare dev_hold() -- I mean there are two other
> ways of acquiring dev in this function. Either all or none of them
> have to be tracker aware, we can't mix?

Oh sorry, I totally forgot to update other parts..
Will squash this in v2.

diff --git a/net/ipv6/anycast.c b/net/ipv6/anycast.c
index fa7f0c22167b4..f8a8e46286b8e 100644
--- a/net/ipv6/anycast.c
+++ b/net/ipv6/anycast.c
@@ -80,7 +80,7 @@ int ipv6_sock_ac_join(struct sock *sk, int ifindex,
const struct in6_addr *addr)
  return -EINVAL;

  if (ifindex)
- dev = dev_get_by_index(net, ifindex);
+ dev = netdev_get_by_index(net, ifindex, &dev_tracker, GFP_KERNEL);

  if (ipv6_chk_addr_and_flags(net, addr, dev, true, 0, IFA_F_TENTATIVE)) {
  err = -EINVAL;
@@ -105,7 +105,7 @@ int ipv6_sock_ac_join(struct sock *sk, int
ifindex, const struct in6_addr *addr)
  rt = rt6_lookup(net, addr, NULL, 0, NULL, 0);
  if (rt) {
  dev = dst_dev(&rt->dst);
- dev_hold(dev);
+ netdev_hold(dev, &dev_tracker, GFP_ATOMIC);
  ip6_rt_put(rt);
  } else if (ishost) {
  rcu_read_unlock();

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH v1 net-next] dev: Pass netdevice_tracker to dev_get_by_flags_rcu().
  2025-07-09 19:01 [PATCH v1 net-next] dev: Pass netdevice_tracker to dev_get_by_flags_rcu() Kuniyuki Iwashima
  2025-07-10  0:16 ` Jakub Kicinski
@ 2025-07-10 21:51 ` kernel test robot
  2025-07-10 23:05 ` kernel test robot
  2 siblings, 0 replies; 5+ messages in thread
From: kernel test robot @ 2025-07-10 21:51 UTC (permalink / raw)
  To: Kuniyuki Iwashima, David S. Miller, Eric Dumazet, Jakub Kicinski,
	Paolo Abeni, Andrew Lunn, David Ahern
  Cc: oe-kbuild-all, netdev, Simon Horman, Kuniyuki Iwashima

Hi Kuniyuki,

kernel test robot noticed the following build warnings:

[auto build test WARNING on net-next/main]

url:    https://github.com/intel-lab-lkp/linux/commits/Kuniyuki-Iwashima/dev-Pass-netdevice_tracker-to-dev_get_by_flags_rcu/20250710-030425
base:   net-next/main
patch link:    https://lore.kernel.org/r/20250709190144.659194-1-kuniyu%40google.com
patch subject: [PATCH v1 net-next] dev: Pass netdevice_tracker to dev_get_by_flags_rcu().
config: openrisc-defconfig (https://download.01.org/0day-ci/archive/20250711/202507110521.NJvbOpJK-lkp@intel.com/config)
compiler: or1k-linux-gcc (GCC) 15.1.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250711/202507110521.NJvbOpJK-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202507110521.NJvbOpJK-lkp@intel.com/

All warnings (new ones prefixed by >>):

>> Warning: net/core/dev.c:1281 function parameter 'tracker' not described in 'netdev_get_by_flags_rcu'

-- 
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v1 net-next] dev: Pass netdevice_tracker to dev_get_by_flags_rcu().
  2025-07-09 19:01 [PATCH v1 net-next] dev: Pass netdevice_tracker to dev_get_by_flags_rcu() Kuniyuki Iwashima
  2025-07-10  0:16 ` Jakub Kicinski
  2025-07-10 21:51 ` kernel test robot
@ 2025-07-10 23:05 ` kernel test robot
  2 siblings, 0 replies; 5+ messages in thread
From: kernel test robot @ 2025-07-10 23:05 UTC (permalink / raw)
  To: Kuniyuki Iwashima, David S. Miller, Eric Dumazet, Jakub Kicinski,
	Paolo Abeni, Andrew Lunn, David Ahern
  Cc: llvm, oe-kbuild-all, netdev, Simon Horman, Kuniyuki Iwashima

Hi Kuniyuki,

kernel test robot noticed the following build errors:

[auto build test ERROR on net-next/main]

url:    https://github.com/intel-lab-lkp/linux/commits/Kuniyuki-Iwashima/dev-Pass-netdevice_tracker-to-dev_get_by_flags_rcu/20250710-030425
base:   net-next/main
patch link:    https://lore.kernel.org/r/20250709190144.659194-1-kuniyu%40google.com
patch subject: [PATCH v1 net-next] dev: Pass netdevice_tracker to dev_get_by_flags_rcu().
config: x86_64-randconfig-071-20250711 (https://download.01.org/0day-ci/archive/20250711/202507110651.qJgShsUO-lkp@intel.com/config)
compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250711/202507110651.qJgShsUO-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202507110651.qJgShsUO-lkp@intel.com/

All errors (new ones prefixed by >>):

>> net/core/dev.c:1294:17: error: use of undeclared identifier 'dev_get_by_flags_rcu'; did you mean 'netdev_get_by_flags_rcu'?
    1294 | EXPORT_IPV6_MOD(dev_get_by_flags_rcu);
         |                 ^~~~~~~~~~~~~~~~~~~~
         |                 netdev_get_by_flags_rcu
   include/net/ip.h:693:42: note: expanded from macro 'EXPORT_IPV6_MOD'
     693 | #define EXPORT_IPV6_MOD(X) EXPORT_SYMBOL(X)
         |                                          ^
   include/linux/export.h:89:44: note: expanded from macro 'EXPORT_SYMBOL'
      89 | #define EXPORT_SYMBOL(sym)              _EXPORT_SYMBOL(sym, "")
         |                                                        ^
   include/linux/export.h:86:54: note: expanded from macro '_EXPORT_SYMBOL'
      86 | #define _EXPORT_SYMBOL(sym, license)    __EXPORT_SYMBOL(sym, license, "")
         |                                                         ^
   include/linux/export.h:76:16: note: expanded from macro '__EXPORT_SYMBOL'
      76 |         extern typeof(sym) sym;                                 \
         |                       ^
   net/core/dev.c:1280:20: note: 'netdev_get_by_flags_rcu' declared here
    1280 | struct net_device *netdev_get_by_flags_rcu(struct net *net, netdevice_tracker *tracker,
         |                    ^
   1 error generated.


vim +1294 net/core/dev.c

^1da177e4c3f415 Linus Torvalds    2005-04-16  1268  
^1da177e4c3f415 Linus Torvalds    2005-04-16  1269  /**
700489140bac828 Kuniyuki Iwashima 2025-07-09  1270   * netdev_get_by_flags_rcu - find any device with given flags
c4ea43c552ecc9c Randy Dunlap      2007-10-12  1271   * @net: the applicable net namespace
^1da177e4c3f415 Linus Torvalds    2005-04-16  1272   * @if_flags: IFF_* values
^1da177e4c3f415 Linus Torvalds    2005-04-16  1273   * @mask: bitmask of bits in if_flags to check
^1da177e4c3f415 Linus Torvalds    2005-04-16  1274   *
eb1ac9ff6c4a572 Kuniyuki Iwashima 2025-07-02  1275   * Search for any interface with the given flags.
eb1ac9ff6c4a572 Kuniyuki Iwashima 2025-07-02  1276   *
eb1ac9ff6c4a572 Kuniyuki Iwashima 2025-07-02  1277   * Context: rcu_read_lock() must be held.
eb1ac9ff6c4a572 Kuniyuki Iwashima 2025-07-02  1278   * Returns: NULL if a device is not found or a pointer to the device.
^1da177e4c3f415 Linus Torvalds    2005-04-16  1279   */
700489140bac828 Kuniyuki Iwashima 2025-07-09  1280  struct net_device *netdev_get_by_flags_rcu(struct net *net, netdevice_tracker *tracker,
700489140bac828 Kuniyuki Iwashima 2025-07-09  1281  					   unsigned short if_flags, unsigned short mask)
^1da177e4c3f415 Linus Torvalds    2005-04-16  1282  {
eb1ac9ff6c4a572 Kuniyuki Iwashima 2025-07-02  1283  	struct net_device *dev;
6c555490e0ce885 WANG Cong         2014-09-11  1284  
eb1ac9ff6c4a572 Kuniyuki Iwashima 2025-07-02  1285  	for_each_netdev_rcu(net, dev) {
eb1ac9ff6c4a572 Kuniyuki Iwashima 2025-07-02  1286  		if (((READ_ONCE(dev->flags) ^ if_flags) & mask) == 0) {
700489140bac828 Kuniyuki Iwashima 2025-07-09  1287  			netdev_hold(dev, tracker, GFP_ATOMIC);
eb1ac9ff6c4a572 Kuniyuki Iwashima 2025-07-02  1288  			return dev;
^1da177e4c3f415 Linus Torvalds    2005-04-16  1289  		}
^1da177e4c3f415 Linus Torvalds    2005-04-16  1290  	}
eb1ac9ff6c4a572 Kuniyuki Iwashima 2025-07-02  1291  
eb1ac9ff6c4a572 Kuniyuki Iwashima 2025-07-02  1292  	return NULL;
^1da177e4c3f415 Linus Torvalds    2005-04-16  1293  }
eb1ac9ff6c4a572 Kuniyuki Iwashima 2025-07-02 @1294  EXPORT_IPV6_MOD(dev_get_by_flags_rcu);
^1da177e4c3f415 Linus Torvalds    2005-04-16  1295  

-- 
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2025-07-10 23:06 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-07-09 19:01 [PATCH v1 net-next] dev: Pass netdevice_tracker to dev_get_by_flags_rcu() Kuniyuki Iwashima
2025-07-10  0:16 ` Jakub Kicinski
2025-07-10  1:38   ` Kuniyuki Iwashima
2025-07-10 21:51 ` kernel test robot
2025-07-10 23:05 ` kernel test robot

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).