Skip to contentSkip to conversation

tun: Ignore tun in netdev_lock_pos().

PATCH In review Kuniyuki Iwashima 4 participants 10 messages Mar 27 – Mar 30

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tun: Ignore tun in netdev_lock_pos().

dev->type could be any value on tun due to TUNSETLINK.

Let's not warn about it in netdev_lock_pos().

---
 drivers/net/tun.c      |  2 +-
 include/linux/if_tun.h | 12 ++++++++++++
 net/core/dev.c         | 22 +++++++++++++---------
 3 files changed, 26 insertions(+), 10 deletions(-)
drivers/net/tun.c +1 -1
index c492fda6fc15..48194e567656 100644
@@ -2338,7 +2338,7 @@ static int tun_fill_info(struct sk_buff *skb, const struct net_device *dev)
 	return -EMSGSIZE;
 }
 
-static struct rtnl_link_ops tun_link_ops __read_mostly = {
+struct rtnl_link_ops tun_link_ops __read_mostly = {
 	.kind		= DRV_NAME,
 	.priv_size	= sizeof(struct tun_struct),
 	.setup		= tun_setup,
include/linux/if_tun.h +12 -0
index 80166eb62f41..89d9b7b0a044 100644
@@ -20,6 +20,13 @@ struct tun_msg_ctl {
 };
 
 #if defined(CONFIG_TUN) || defined(CONFIG_TUN_MODULE)
+extern struct rtnl_link_ops tun_link_ops;
+
+static inline bool netdev_is_tun(const struct net_device *dev)
+{
+	return dev->rtnl_link_ops == &tun_link_ops;
+}
+
 struct socket *tun_get_socket(struct file *);
 struct ptr_ring *tun_get_tx_ring(struct file *file);
 
@@ -45,6 +52,11 @@ void tun_ptr_free(void *ptr);
 struct file;
 struct socket;
 
+static inline bool netdev_is_tun(const struct net_device *dev)
+{
+	return false;
+}
+
 static inline struct socket *tun_get_socket(struct file *f)
 {
 	return ERR_PTR(-EINVAL);
net/core/dev.c +13 -9
index 200d44883fc1..bd91e0a3dbdf 100644
@@ -89,6 +89,7 @@
 #include <linux/errno.h>
 #include <linux/interrupt.h>
 #include <linux/if_ether.h>
+#include <linux/if_tun.h>
 #include <linux/netdevice.h>
 #include <linux/etherdevice.h>
 #include <linux/ethtool.h>
@@ -526,24 +527,27 @@ static const char *const netdev_lock_name[] = {
 static struct lock_class_key netdev_xmit_lock_key[ARRAY_SIZE(netdev_lock_type)];
 static struct lock_class_key netdev_addr_lock_key[ARRAY_SIZE(netdev_lock_type)];
 
-static inline unsigned short netdev_lock_pos(unsigned short dev_type)
+static inline unsigned short netdev_lock_pos(const struct net_device *dev)
 {
+	unsigned short dev_type = dev->type;
 	int i;
 
 	for (i = 0; i < ARRAY_SIZE(netdev_lock_type); i++)
 		if (netdev_lock_type[i] == dev_type)
 			return i;
+
 	/* the last key is used by default */
-	WARN_ONCE(1, "netdev_lock_pos() could not find dev_type=%u\n", dev_type);
+	WARN_ONCE(!netdev_is_tun(dev),
+		  "netdev_lock_pos() could not find dev_type=%u\n", dev_type);
 	return ARRAY_SIZE(netdev_lock_type) - 1;
 }
 
-static inline void netdev_set_xmit_lockdep_class(spinlock_t *lock,
-						 unsigned short dev_type)
+static inline void netdev_set_xmit_lockdep_class(const struct net_device *dev,
+						 spinlock_t *lock)
 {
 	int i;
 
-	i = netdev_lock_pos(dev_type);
+	i = netdev_lock_pos(dev);
 	lockdep_set_class_and_name(lock, &netdev_xmit_lock_key[i],
 				   netdev_lock_name[i]);
 }
@@ -552,14 +556,14 @@ static inline void netdev_set_addr_lockdep_class(struct net_device *dev)
 {
 	int i;
 
-	i = netdev_lock_pos(dev->type);
+	i = netdev_lock_pos(dev);
 	lockdep_set_class_and_name(&dev->addr_list_lock,
 				   &netdev_addr_lock_key[i],
 				   netdev_lock_name[i]);
 }
 #else
-static inline void netdev_set_xmit_lockdep_class(spinlock_t *lock,
-						 unsigned short dev_type)
+static inline void netdev_set_xmit_lockdep_class(const struct net_device *dev,
+						 spinlock_t *lock)
 {
 }
 
@@ -11210,7 +11214,7 @@ static void netdev_init_one_queue(struct net_device *dev,
 {
 	/* Initialize queue lock */
 	spin_lock_init(&queue->_xmit_lock);
-	netdev_set_xmit_lockdep_class(&queue->_xmit_lock, dev->type);
+	netdev_set_xmit_lockdep_class(dev, &queue->_xmit_lock);
 	queue->xmit_lock_owner = -1;
 	netdev_queue_numa_node_write(queue, NUMA_NO_NODE);
 	queue->dev = dev;
Signature · 1 line
2.53.0.1018.g2bb0e51243-goog
#1 lore raw
Quoted 6 lines · Kuniyuki Iwashima

dev->type could be any value on tun due to TUNSETLINK.

Let's not warn about it in netdev_lock_pos().

Hmmm... should we instead have a list of supported types ?

And reject user space requests for unsupported ones ?

#2 lore raw
Quoted 5 lines · Eric Dumazet
Quoted 3 lines · Kuniyuki Iwashima

dev->type could be any value on tun due to TUNSETLINK.

Quoted 7 lines
Quoted 3 lines

Let's not warn about it in netdev_lock_pos().

Hmmm... should we instead have a list of supported types ?

And reject user space requests for unsupported ones ?

I chose a safer way but maybe we could reuse tun_get_addr_len() and define the default case there as unsupported ?

I guess it's okay even if it breaks userspace because we can just add a single line to restore the support.

maintainer
#3 lore raw
Quoted 12 lines · Kuniyuki Iwashima
Quoted 11 lines · Eric Dumazet
Quoted 6 lines · Kuniyuki Iwashima

dev->type could be any value on tun due to TUNSETLINK.

Let's not warn about it in netdev_lock_pos().

Hmmm... should we instead have a list of supported types ?

Quoted 7 lines
Quoted 1 line

And reject user space requests for unsupported ones ?

I chose a safer way but maybe we could reuse tun_get_addr_len() and define the default case there as unsupported ?

I guess it's okay even if it breaks userspace because we can just add a single line to restore the support.

We shouldn't risk breaking userspace, over such a sanity check.

Presumably any truly newly added type will have a non-zero addr_len. While tun sets this to zero for weird inputs. Maybe just filter on that in the WARN_ONCE?

#4 lore raw
Quoted 16 lines · Willem de Bruijn
Quoted 14 lines · Kuniyuki Iwashima
Quoted 12 lines · Eric Dumazet
Quoted 6 lines · Kuniyuki Iwashima

dev->type could be any value on tun due to TUNSETLINK.

Let's not warn about it in netdev_lock_pos().

Hmmm... should we instead have a list of supported types ?

And reject user space requests for unsupported ones ?

Quoted 7 lines
Quoted 5 lines

I chose a safer way but maybe we could reuse tun_get_addr_len() and define the default case there as unsupported ?

I guess it's okay even if it breaks userspace because we can just add a single line to restore the support.

We shouldn't risk breaking userspace, over such a sanity check.

The thing is : this check makes sure that each type has a different lockdep class.

Quoted 4 lines

Presumably any truly newly added type will have a non-zero addr_len. While tun sets this to zero for weird inputs. Maybe just filter on that in the WARN_ONCE?

Then we might hit some lockdep issues...

Perhaps use lockdep_register_key() on each tun device...

maintainer
#5 lore raw
Quoted 30 lines · Eric Dumazet
Quoted 16 lines · Willem de Bruijn
Quoted 14 lines · Kuniyuki Iwashima
Quoted 12 lines · Eric Dumazet
Quoted 6 lines · Kuniyuki Iwashima

dev->type could be any value on tun due to TUNSETLINK.

Let's not warn about it in netdev_lock_pos().

Hmmm... should we instead have a list of supported types ?

And reject user space requests for unsupported ones ?

Quoted 7 lines
Quoted 5 lines

I chose a safer way but maybe we could reuse tun_get_addr_len() and define the default case there as unsupported ?

I guess it's okay even if it breaks userspace because we can just add a single line to restore the support.

We shouldn't risk breaking userspace, over such a sanity check.

The thing is : this check makes sure that each type has a different lockdep class.

Quoted 1 line
Quoted 5 lines
Quoted 3 lines

Presumably any truly newly added type will have a non-zero addr_len. While tun sets this to zero for weird inputs. Maybe just filter on that in the WARN_ONCE?

Then we might hit some lockdep issues...

Warnings due to stacked devices wouldn't apply to tun.

Can this be used to set up (false positive) lock inversion deadline reports?

Quoted 1 line

Perhaps use lockdep_register_key() on each tun device...

In tun_set_iff replace the xmit_lockdep_class previously set by netdev_init_one_queue?

If keeping the approach from v1, perhaps a tun device can already be identified in core code without having to export tun_link_ops. From the init in tun_set_iff, dev->sysfs_groups[0] will have an attr "tun_flags". There might be something more elegant. Just the first I spotted.

#6 lore raw
In reply to Kuniyuki Iwashima
kernel 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/tun-Ignore-tun-in-netdev_lock_pos/20260328-220059
base:   net-next/main
patch link:    https://lore.kernel.org/r/20260327213441.3781433-1-kuniyu%40google.com
patch subject: [PATCH v1 net-next] tun: Ignore tun in netdev_lock_pos().
config: arm64-allnoconfig-bpf (https://download.01.org/0day-ci/archive/20260329/[email protected]/config)
compiler: aarch64-linux-gnu-gcc (Debian 14.2.0-19) 14.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260329/[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 >>):

aarch64-linux-gnu-ld: net/core/dev.o: in function `netdev_lock_pos':
net/core/dev.c:540:(.text+0x8088): undefined reference to `tun_link_ops'
aarch64-linux-gnu-ld: net/core/dev.o: relocation R_AARCH64_ADR_PREL_PG_HI21 against symbol `tun_link_ops' which may bind externally can not be used when making a shared object; recompile with -fPIC

>> net/core/dev.c:540:(.text+0x8088): dangerous relocation: unsupported relocation
>> aarch64-linux-gnu-ld: net/core/dev.c:540:(.text+0x808c): undefined reference to `tun_link_ops'

aarch64-linux-gnu-ld: net/core/dev.c:540:(.text+0x29d70): undefined reference to `tun_link_ops'
aarch64-linux-gnu-ld: net/core/dev.o: relocation R_AARCH64_ADR_PREL_PG_HI21 against symbol `tun_link_ops' which may bind externally can not be used when making a shared object; recompile with -fPIC net/core/dev.c:540:(.text+0x29d70): dangerous relocation: unsupported relocation
aarch64-linux-gnu-ld: net/core/dev.c:540:(.text+0x29d74): undefined reference to `tun_link_ops'

vim +540 net/core/dev.c

529
530 static inline unsigned short netdev_lock_pos(const struct net_device *dev)
531 {
532 unsigned short dev_type = dev->type;
533 int i;
534
535 for (i = 0; i < ARRAY_SIZE(netdev_lock_type); i++)
536 if (netdev_lock_type[i] == dev_type)
537 return i;
538
539 /* the last key is used by default */

 > 540		WARN_ONCE(!netdev_is_tun(dev),

541 "netdev_lock_pos() could not find dev_type=%u\n", dev_type);
542 return ARRAY_SIZE(netdev_lock_type) - 1;
543 }
544

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#7 lore raw
In reply to Kuniyuki Iwashima
kernel 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/tun-Ignore-tun-in-netdev_lock_pos/20260328-220059
base:   net-next/main
patch link:    https://lore.kernel.org/r/20260327213441.3781433-1-kuniyu%40google.com
patch subject: [PATCH v1 net-next] tun: Ignore tun in netdev_lock_pos().
config: s390-allnoconfig-bpf (https://download.01.org/0day-ci/archive/20260329/[email protected]/config)
compiler: s390x-linux-gnu-gcc (Debian 14.2.0-19) 14.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260329/[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 >>):

s390x-linux-gnu-ld: net/core/dev.o: in function `netdev_lock_pos':
net/core/dev.c:540:(.text+0x86a6): undefined reference to `tun_link_ops'

>> s390x-linux-gnu-ld: net/core/dev.c:540:(.text+0x2b5ac): undefined reference to `tun_link_ops'
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#8 lore raw
In reply to Kuniyuki Iwashima
kernel 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/tun-Ignore-tun-in-netdev_lock_pos/20260328-220059
base:   net-next/main
patch link:    https://lore.kernel.org/r/20260327213441.3781433-1-kuniyu%40google.com
patch subject: [PATCH v1 net-next] tun: Ignore tun in netdev_lock_pos().
config: x86_64-rhel-9.4-kselftests (https://download.01.org/0day-ci/archive/20260329/[email protected]/config)
compiler: gcc-14 (Debian 14.2.0-19) 14.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260329/[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 >>):

ld: vmlinux.o: in function `netdev_lock_pos':

>> net/core/dev.c:540:(.text+0x24fe910): undefined reference to `tun_link_ops'
>> ld: net/core/dev.c:540:(.text+0x254084c): undefined reference to `tun_link_ops'

vim +540 net/core/dev.c

529
530 static inline unsigned short netdev_lock_pos(const struct net_device *dev)
531 {
532 unsigned short dev_type = dev->type;
533 int i;
534
535 for (i = 0; i < ARRAY_SIZE(netdev_lock_type); i++)
536 if (netdev_lock_type[i] == dev_type)
537 return i;
538
539 /* the last key is used by default */

 > 540		WARN_ONCE(!netdev_is_tun(dev),

541 "netdev_lock_pos() could not find dev_type=%u\n", dev_type);
542 return ARRAY_SIZE(netdev_lock_type) - 1;
543 }
544

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#9 lore raw
In reply to Kuniyuki Iwashima
kernel 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/tun-Ignore-tun-in-netdev_lock_pos/20260328-220059
base:   net-next/main
patch link:    https://lore.kernel.org/r/20260327213441.3781433-1-kuniyu%40google.com
patch subject: [PATCH v1 net-next] tun: Ignore tun in netdev_lock_pos().
config: x86_64-randconfig-016-20260329 (https://download.01.org/0day-ci/archive/20260329/[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/20260329/[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 >>):

>> ld.lld: error: undefined symbol: tun_link_ops
   >>> referenced by if_tun.h:27 (include/linux/if_tun.h:27)
   >>>               vmlinux.o:(register_netdevice)
   >>> referenced by if_tun.h:27 (include/linux/if_tun.h:27)
   >>>               vmlinux.o:(alloc_netdev_mqs)
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki