dev: Pass netdevice_tracker to dev_get_by_flags_rcu().
Subsystems (3)
PATCH 1/1 dev: Pass netdevice_tracker to dev_get_by_flags_rcu().
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.
--- include/linux/netdevice.h | 4 ++-- net/core/dev.c | 8 ++++---- net/ipv6/anycast.c | 7 ++++--- 3 files changed, 10 insertions(+), 9 deletions(-)
include/linux/netdevice.h +2 -2
@@ -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);
net/core/dev.c +4 -4
@@ -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; } }
net/ipv6/anycast.c +4 -3
@@ -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));
Signature · 1 line
2.50.0.727.gbf7dc18ff4-goog
Jakub Kicinski
maintainerQuoted 24 lines · On Wed, 9 Jul 2025 19:01:32 +0000 Kuniyuki Iwashima wrote:
net/ipv6/anycast.c +3
-2@@ -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 (); }
net/ipv6/anycast.c int ipv6_sock_ac_join(struct sock *sk, int ifindex, const struct in6_addr *addr) · line 163
Quoted 5 lines
net/ipv6/anycast.c +1
-1@@ -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?
Signature · 1 line
pw-bot: cr
Quoted 28 lines · Jakub Kicinski
Quoted 26 lines · On Wed, 9 Jul 2025 19:01:32 +0000 Kuniyuki Iwashima wrote:
net/ipv6/anycast.c +3
-2@@ -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);
Quoted 8 lines
net/ipv6/anycast.c int ipv6_sock_ac_join(struct sock *sk, int ifindex, const struct in6_addr *addr) · line 163
Quoted 3 lines
net/ipv6/anycast.c +1
-1error: -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 baredev_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.
net/ipv6/anycast.c +0 -0
@@ -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();
kernel test robot
build botkernel test robot noticed the following build warnings:
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/[email protected]/config) compiler: or1k-linux-gcc (GCC) 15.1.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250711/[email protected]/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 <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All warnings (new ones prefixed by >>):
>> Warning: net/core/dev.c:1281 function parameter 'tracker' not described in 'netdev_get_by_flags_rcu'
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
kernel test robot
build botkernel test robot noticed the following build errors:
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/[email protected]/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/[email protected]/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 <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
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
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki