public inbox for netdev@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v1 net-next] tun: Ignore tun in netdev_lock_pos().
@ 2026-03-27 21:34 Kuniyuki Iwashima
  2026-03-27 21:48 ` Eric Dumazet
  0 siblings, 1 reply; 5+ messages in thread
From: Kuniyuki Iwashima @ 2026-03-27 21:34 UTC (permalink / raw)
  To: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	Willem de Bruijn, Jason Wang, Andrew Lunn
  Cc: Simon Horman, Kuniyuki Iwashima, Kuniyuki Iwashima, netdev

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

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

Signed-off-by: Kuniyuki Iwashima <kuniyu@google.com>
---
 drivers/net/tun.c      |  2 +-
 include/linux/if_tun.h | 12 ++++++++++++
 net/core/dev.c         | 22 +++++++++++++---------
 3 files changed, 26 insertions(+), 10 deletions(-)

diff --git a/drivers/net/tun.c b/drivers/net/tun.c
index c492fda6fc15..48194e567656 100644
--- a/drivers/net/tun.c
+++ b/drivers/net/tun.c
@@ -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,
diff --git a/include/linux/if_tun.h b/include/linux/if_tun.h
index 80166eb62f41..89d9b7b0a044 100644
--- a/include/linux/if_tun.h
+++ b/include/linux/if_tun.h
@@ -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);
diff --git a/net/core/dev.c b/net/core/dev.c
index 200d44883fc1..bd91e0a3dbdf 100644
--- a/net/core/dev.c
+++ b/net/core/dev.c
@@ -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;
-- 
2.53.0.1018.g2bb0e51243-goog


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

* Re: [PATCH v1 net-next] tun: Ignore tun in netdev_lock_pos().
  2026-03-27 21:34 [PATCH v1 net-next] tun: Ignore tun in netdev_lock_pos() Kuniyuki Iwashima
@ 2026-03-27 21:48 ` Eric Dumazet
  2026-03-27 22:22   ` Kuniyuki Iwashima
  0 siblings, 1 reply; 5+ messages in thread
From: Eric Dumazet @ 2026-03-27 21:48 UTC (permalink / raw)
  To: Kuniyuki Iwashima
  Cc: David S. Miller, Jakub Kicinski, Paolo Abeni, Willem de Bruijn,
	Jason Wang, Andrew Lunn, Simon Horman, Kuniyuki Iwashima, netdev

On Fri, Mar 27, 2026 at 2:34 PM Kuniyuki Iwashima <kuniyu@google.com> wrote:
>
> dev->type could be any value on tun due to TUNSETLINK.
>
> Let's not warn about it in netdev_lock_pos().
>
> Signed-off-by: Kuniyuki Iwashima <kuniyu@google.com>

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

And reject user space requests for unsupported ones ?

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

* Re: [PATCH v1 net-next] tun: Ignore tun in netdev_lock_pos().
  2026-03-27 21:48 ` Eric Dumazet
@ 2026-03-27 22:22   ` Kuniyuki Iwashima
  2026-03-27 23:16     ` Willem de Bruijn
  0 siblings, 1 reply; 5+ messages in thread
From: Kuniyuki Iwashima @ 2026-03-27 22:22 UTC (permalink / raw)
  To: Eric Dumazet
  Cc: David S. Miller, Jakub Kicinski, Paolo Abeni, Willem de Bruijn,
	Jason Wang, Andrew Lunn, Simon Horman, Kuniyuki Iwashima, netdev

On Fri, Mar 27, 2026 at 2:48 PM Eric Dumazet <edumazet@google.com> wrote:
>
> On Fri, Mar 27, 2026 at 2:34 PM Kuniyuki Iwashima <kuniyu@google.com> wrote:
> >
> > dev->type could be any value on tun due to TUNSETLINK.
> >
> > Let's not warn about it in netdev_lock_pos().
> >
> > Signed-off-by: Kuniyuki Iwashima <kuniyu@google.com>
>
> 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.

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

* Re: [PATCH v1 net-next] tun: Ignore tun in netdev_lock_pos().
  2026-03-27 22:22   ` Kuniyuki Iwashima
@ 2026-03-27 23:16     ` Willem de Bruijn
  2026-03-28  0:19       ` Eric Dumazet
  0 siblings, 1 reply; 5+ messages in thread
From: Willem de Bruijn @ 2026-03-27 23:16 UTC (permalink / raw)
  To: Kuniyuki Iwashima, Eric Dumazet
  Cc: David S. Miller, Jakub Kicinski, Paolo Abeni, Willem de Bruijn,
	Jason Wang, Andrew Lunn, Simon Horman, Kuniyuki Iwashima, netdev

Kuniyuki Iwashima wrote:
> On Fri, Mar 27, 2026 at 2:48 PM Eric Dumazet <edumazet@google.com> wrote:
> >
> > On Fri, Mar 27, 2026 at 2:34 PM Kuniyuki Iwashima <kuniyu@google.com> wrote:
> > >
> > > dev->type could be any value on tun due to TUNSETLINK.
> > >
> > > Let's not warn about it in netdev_lock_pos().
> > >
> > > Signed-off-by: Kuniyuki Iwashima <kuniyu@google.com>
> >
> > 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.

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?

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

* Re: [PATCH v1 net-next] tun: Ignore tun in netdev_lock_pos().
  2026-03-27 23:16     ` Willem de Bruijn
@ 2026-03-28  0:19       ` Eric Dumazet
  0 siblings, 0 replies; 5+ messages in thread
From: Eric Dumazet @ 2026-03-28  0:19 UTC (permalink / raw)
  To: Willem de Bruijn
  Cc: Kuniyuki Iwashima, David S. Miller, Jakub Kicinski, Paolo Abeni,
	Jason Wang, Andrew Lunn, Simon Horman, Kuniyuki Iwashima, netdev

On Fri, Mar 27, 2026 at 4:16 PM Willem de Bruijn
<willemdebruijn.kernel@gmail.com> wrote:
>
> Kuniyuki Iwashima wrote:
> > On Fri, Mar 27, 2026 at 2:48 PM Eric Dumazet <edumazet@google.com> wrote:
> > >
> > > On Fri, Mar 27, 2026 at 2:34 PM Kuniyuki Iwashima <kuniyu@google.com> wrote:
> > > >
> > > > dev->type could be any value on tun due to TUNSETLINK.
> > > >
> > > > Let's not warn about it in netdev_lock_pos().
> > > >
> > > > Signed-off-by: Kuniyuki Iwashima <kuniyu@google.com>
> > >
> > > 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.
>
> 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.

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

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

end of thread, other threads:[~2026-03-28  0:20 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-03-27 21:34 [PATCH v1 net-next] tun: Ignore tun in netdev_lock_pos() Kuniyuki Iwashima
2026-03-27 21:48 ` Eric Dumazet
2026-03-27 22:22   ` Kuniyuki Iwashima
2026-03-27 23:16     ` Willem de Bruijn
2026-03-28  0:19       ` Eric Dumazet

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox