explicit netdev refs
Jakub Kicinski
maintainercover explicit netdev refs
syzbot keeps accumulating netdev ref leak errors. These are notoriously hard to debug. We can't do much about all the old code at this point, this set tries to provide an API for the new code to use.
It basically adds a explicitly reference counted pointer (there's probably a better name for this). It's a structure which wraps the netdev pointer and helps us doing sanity checking.
There isn't much that's netdev-specific here, but we probably still want to keep our own wrappers even if the main struct gets generalized, so that we can keep the helpers typed.
Sending as an RFC because vlan refcounting has a bug which needs to be fixed first [1]. Explicit refs catch it and spew warnings appropriately.
[1] https://lore.kernel.org/all/[email protected]/
Jakub Kicinski (2):
net: add netdev_refs debug
vlan: use new netdev_refs infra
MAINTAINERS | 1 + drivers/net/ethernet/broadcom/bnxt/bnxt_tc.c | 3 +- include/linux/if_vlan.h | 11 +- include/linux/netdev_refs.h | 104 +++++++++++++++++++ lib/Kconfig.debug | 7 ++ net/8021q/vlan.c | 13 +-- net/8021q/vlan_core.c | 4 +- net/8021q/vlan_dev.c | 63 +++++------ net/8021q/vlan_gvrp.c | 5 +- net/8021q/vlan_mvrp.c | 5 +- net/8021q/vlan_netlink.c | 4 +- net/8021q/vlanproc.c | 6 +- net/core/dev.c | 8 ++ 13 files changed, 186 insertions(+), 48 deletions(-) create mode 100644 include/linux/netdev_refs.h
Signature · 1 line
2.31.1
Jakub Kicinski
maintainerRFC 1/2 net: add netdev_refs debug
Debugging netdev ref leaks is still pretty hard. Eric added optional use of a normal refcount which is useful for tracking abuse of existing users.
For new code, however, it'd be great if we could actually track the refs per-user. Allowing us to detect leaks where they happen. This patch introduces a netdev_ref type and uses the debug_objects infra to track refs being lost or misused.
In the future we can extend this structure to also catch those who fail to release the ref on unregistering notification.
--- MAINTAINERS | 1 + include/linux/netdev_refs.h | 104 ++++++++++++++++++++++++++++++++++++ lib/Kconfig.debug | 7 +++ net/core/dev.c | 8 +++ 4 files changed, 120 insertions(+) create mode 100644 include/linux/netdev_refs.h
MAINTAINERS +1 -0
@@ -18482,6 +18482,7 @@ F: include/uapi/linux/pkt_sched.h F: include/uapi/linux/tc_act/ F: include/uapi/linux/tc_ematch/ F: net/sched/ +F: tools/testing/selftests/tc-testing/ TC90522 MEDIA DRIVER M: Akihiro Tsukada <[email protected]>
include/linux/netdev_refs.h new file +104 -0
104 lines not on this page · raw message
lib/Kconfig.debug +7 -0
@@ -655,6 +655,13 @@ config DEBUG_OBJECTS_PERCPU_COUNTER percpu counter routines to track the life time of percpu counter objects and validate the percpu counter operations. +config DEBUG_OBJECTS_NETDEV_REFS + bool "Debug net_device references" + depends on DEBUG_OBJECTS + help + If you say Y here, additional code will be inserted into the + net_device reference routines to catch incorrect use. + config DEBUG_OBJECTS_ENABLE_DEFAULT int "debug_objects bootup default value (0-1)" range 0 1
net/core/dev.c +8 -0
@@ -150,6 +150,7 @@#include <linux/pm_runtime.h> #include <linux/prandom.h> #include <linux/once_lite.h> +#include <linux/netdev_refs.h> #include "net-sysfs.h"
@@ -158,6 +159,13 @@ static DEFINE_SPINLOCK(ptype_lock);struct list_head ptype_base[PTYPE_HASH_SIZE ]__read_mostly ;struct list_head ptype_all__read_mostly ;/* Taps */ +const struct debug_obj_descr netdev_ref_debug_descr = { + .name ="netdev_ref" , +}; +#ifdef CONFIG_DEBUG_OBJECTS_NETDEV_REFS +EXPORT_SYMBOL (netdev_ref_debug_descr); +#endif +static int netif_rx_internal (struct sk_buff *skb);static int call_netdevice_notifiers_info (unsigned long val,struct netdev_notifier_info *info);
Signature · 1 line
2.31.1
Jakub Kicinski
maintainerRFC 2/2 vlan: use new netdev_refs infra
In reply to Jakub KicinskiExample user of the new netdev_refs infra.
VLAN is not the simplest - the ref ptr is stored on the IOCTL path and netlink path but only taken during netdev registration.
No changes to the assembly output with debug disabled, save for the few places where explicit temporary variables were added.
--- drivers/net/ethernet/broadcom/bnxt/bnxt_tc.c | 3 +- include/linux/if_vlan.h | 11 +++- net/8021q/vlan.c | 13 ++-- net/8021q/vlan_core.c | 4 +- net/8021q/vlan_dev.c | 63 ++++++++++---------- net/8021q/vlan_gvrp.c | 5 +- net/8021q/vlan_mvrp.c | 5 +- net/8021q/vlan_netlink.c | 4 +- net/8021q/vlanproc.c | 6 +- 9 files changed, 66 insertions(+), 48 deletions(-)
drivers/net/ethernet/broadcom/bnxt/bnxt_tc.c +2 -1
@@ -1250,9 +1250,10 @@ static int bnxt_tc_resolve_tunnel_hdrs(struct bnxt *bp, dst_dev = rt->dst.dev; if (is_vlan_dev(dst_dev)) { #if IS_ENABLED(CONFIG_VLAN_8021Q) + struct net_device *real_dev = __vlan_dev_real_dev(dst_dev); struct vlan_dev_priv *vlan = vlan_dev_priv(dst_dev); - if (vlan->real_dev != real_dst_dev) { + if (real_dev != real_dst_dev) { netdev_info(bp->dev, "dst_dev(%s) doesn't use PF-if(%s)\n", netdev_name(dst_dev),
include/linux/if_vlan.h +10 -1
@@ -8,6 +8,7 @@#define _LINUX_IF_VLAN_H_#include <linux/netdevice.h> +#include <linux/netdev_refs.h> #include <linux/etherdevice.h> #include <linux/rtnetlink.h> #include <linux/bug.h>
@@ -176,7 +177,7 @@ struct vlan_dev_priv {u16 vlan_id;u16 flags; -struct net_device *real_dev; +struct netdev_ref real_dev;unsigned char real_dev_addr[ETH_ALEN ];struct proc_dir_entry *dent;
@@ -191,6 +192,14 @@ static inline struct vlan_dev_priv *vlan_dev_priv(const struct net_device *dev)return netdev_priv (dev); } +static inline struct net_device * +__vlan_dev_real_dev (const struct net_device *dev) +{ +struct vlan_dev_priv *vlan =vlan_dev_priv (dev); + +return netdev_ref_ptr (&vlan->real_dev); +} +static inline u16 vlan_dev_get_egress_qos_mask (struct net_device *dev,u32 skprio) {
net/8021q/vlan.c +7 -6
@@ -89,7 +89,7 @@ static void vlan_stacked_transfer_operstate(const struct net_device *rootdev,void unregister_vlan_dev (struct net_device *dev,struct list_head *head) {struct vlan_dev_priv *vlan =vlan_dev_priv (dev); -struct net_device *real_dev = vlan->real_dev; +struct net_device *real_dev =netdev_ref_ptr (&vlan->real_dev);struct vlan_info *vlan_info;struct vlan_group *grp;u16 vlan_id = vlan->vlan_id;
@@ -148,7 +148,7 @@ int vlan_check_real_dev(struct net_device *real_dev,int register_vlan_dev (struct net_device *dev,struct netlink_ext_ack *extack) {struct vlan_dev_priv *vlan =vlan_dev_priv (dev); -struct net_device *real_dev = vlan->real_dev; +struct net_device *real_dev =__netdev_ref_ptr (&vlan->real_dev);u16 vlan_id = vlan->vlan_id;struct vlan_info *vlan_info;struct vlan_group *grp;
@@ -185,7 +185,7 @@ int register_vlan_dev(struct net_device *dev, struct netlink_ext_ack *extack) goto out_unregister_netdev;/* Account for reference in struct vlan_dev_priv */ -dev_hold (real_dev); +__netdev_hold_stored (&vlan->real_dev);vlan_stacked_transfer_operstate (real_dev, dev, vlan);linkwatch_fire_event (dev);/* _MUST_ call rfc2863_policy() */
@@ -272,7 +272,7 @@ static int register_vlan_device(struct net_device *real_dev, u16 vlan_id) vlan =vlan_dev_priv (new_dev); vlan->vlan_proto =htons (ETH_P_8021Q ); vlan->vlan_id = vlan_id; - vlan->real_dev = real_dev; +__netdev_ref_store (&vlan->real_dev, real_dev); vlan->dent =NULL ; vlan->flags =VLAN_FLAG_REORDER_HDR ;
@@ -321,6 +321,7 @@ static void vlan_transfer_features(struct net_device *dev, struct net_device *vlandev) {struct vlan_dev_priv *vlan =vlan_dev_priv (vlandev); +struct net_device *real_dev =netdev_ref_ptr (&vlan->real_dev); vlandev->gso_max_size = dev->gso_max_size; vlandev->gso_max_segs = dev->gso_max_segs;
@@ -335,8 +336,8 @@ static void vlan_transfer_features(struct net_device *dev,#endif vlandev->priv_flags &= ~IFF_XMIT_DST_RELEASE ; - vlandev->priv_flags |= (vlan->real_dev->priv_flags &IFF_XMIT_DST_RELEASE ); - vlandev->hw_enc_features =vlan_tnl_features (vlan->real_dev); + vlandev->priv_flags |= (real_dev->priv_flags &IFF_XMIT_DST_RELEASE ); + vlandev->hw_enc_features =vlan_tnl_features (real_dev);netdev_update_features (vlandev); }
net/8021q/vlan_core.c +2 -2
@@ -101,10 +101,10 @@ EXPORT_SYMBOL(__vlan_find_dev_deep_rcu);struct net_device *vlan_dev_real_dev (const struct net_device *dev) { -struct net_device *ret =vlan_dev_priv (dev)->real_dev; +struct net_device *ret =__vlan_dev_real_dev (dev);while (is_vlan_dev (ret)) - ret =vlan_dev_priv (ret)->real_dev; + ret =__vlan_dev_real_dev (ret);return ret; }
net/8021q/vlan_dev.c +33 -30
224 lines not on this page · raw message
net/8021q/vlan_gvrp.c +3 -2
@@ -31,7 +31,8 @@ int vlan_gvrp_request_join(const struct net_device *dev)if (vlan->vlan_proto !=htons (ETH_P_8021Q ))return 0 ; -return garp_request_join (vlan->real_dev, &vlan_gvrp_app, +return garp_request_join (netdev_ref_ptr (&vlan->real_dev), + &vlan_gvrp_app, &vlan_id,sizeof (vlan_id),GVRP_ATTR_VID ); }
@@ -42,7 +43,7 @@ void vlan_gvrp_request_leave(const struct net_device *dev)if (vlan->vlan_proto !=htons (ETH_P_8021Q ))return ; -garp_request_leave (vlan->real_dev, &vlan_gvrp_app, +garp_request_leave (netdev_ref_ptr (&vlan->real_dev), &vlan_gvrp_app, &vlan_id,sizeof (vlan_id),GVRP_ATTR_VID ); }
net/8021q/vlan_mvrp.c +3 -2
@@ -37,7 +37,8 @@ int vlan_mvrp_request_join(const struct net_device *dev)if (vlan->vlan_proto !=htons (ETH_P_8021Q ))return 0 ; -return mrp_request_join (vlan->real_dev, &vlan_mrp_app, +return mrp_request_join (netdev_ref_ptr (&vlan->real_dev), + &vlan_mrp_app, &vlan_id,sizeof (vlan_id),MVRP_ATTR_VID ); }
@@ -48,7 +49,7 @@ void vlan_mvrp_request_leave(const struct net_device *dev)if (vlan->vlan_proto !=htons (ETH_P_8021Q ))return ; -mrp_request_leave (vlan->real_dev, &vlan_mrp_app, +mrp_request_leave (netdev_ref_ptr (&vlan->real_dev), &vlan_mrp_app, &vlan_id,sizeof (vlan_id),MVRP_ATTR_VID ); }
net/8021q/vlan_netlink.c +2 -2
@@ -166,7 +166,7 @@ static int vlan_newlink(struct net *src_net, struct net_device *dev, vlan->vlan_proto = proto; vlan->vlan_id =nla_get_u16 (data[IFLA_VLAN_ID ]); - vlan->real_dev = real_dev; +__netdev_ref_store (&vlan->real_dev, real_dev); dev->priv_flags |= (real_dev->priv_flags &IFF_XMIT_DST_RELEASE ); vlan->flags =VLAN_FLAG_REORDER_HDR ;
@@ -274,7 +274,7 @@ static int vlan_fill_info(struct sk_buff *skb, const struct net_device *dev)static struct net *vlan_get_link_net (const struct net_device *dev) { -struct net_device *real_dev =vlan_dev_priv (dev)->real_dev; +struct net_device *real_dev =__vlan_dev_real_dev (dev);return dev_net (real_dev); }
net/8021q/vlanproc.c +4 -2
@@ -231,9 +231,10 @@ static int vlan_seq_show(struct seq_file *seq, void *v) }else {const struct net_device *vlandev = v;const struct vlan_dev_priv *vlan =vlan_dev_priv (vlandev); +struct net_device *real_dev =netdev_ref_ptr (&vlan->real_dev);seq_printf (seq,"%-15s| %d | %s\n" , vlandev->name, - vlan->vlan_id, vlan->real_dev->name); + vlan->vlan_id, real_dev->name); }return 0 ; }
@@ -242,6 +243,7 @@ static int vlandev_seq_show(struct seq_file *seq, void *offset) {struct net_device *vlandev = (struct net_device *) seq->private;const struct vlan_dev_priv *vlan =vlan_dev_priv (vlandev); +struct net_device *real_dev =netdev_ref_ptr (&vlan->real_dev);struct rtnl_link_stats64 temp;const struct rtnl_link_stats64 *stats;static const char fmt64[] ="%30s %12llu\n" ;
@@ -262,7 +264,7 @@ static int vlandev_seq_show(struct seq_file *seq, void *offset)seq_puts (seq,"\n" );seq_printf (seq, fmt64,"total frames transmitted" , stats->tx_packets);seq_printf (seq, fmt64,"total bytes transmitted" , stats->tx_bytes); -seq_printf (seq,"Device: %s" , vlan->real_dev->name); +seq_printf (seq,"Device: %s" , real_dev->name);/* now show all PRIORITY mappings relating to this VLAN */ seq_printf(seq, "\nINGRESS priority mappings: " "0:%u 1:%u 2:%u 3:%u 4:%u 5:%u 6:%u 7:%u\n",
Signature · 1 line
2.31.1
Re: [RFC net-next 1/2] net: add netdev_refs debug
In reply to Jakub KicinskiQuoted 61 lines · Jakub Kicinski
Debugging netdev ref leaks is still pretty hard. Eric added optional use of a normal refcount which is useful for tracking abuse of existing users.
For new code, however, it'd be great if we could actually track the refs per-user. Allowing us to detect leaks where they happen. This patch introduces a
netdev_reftype and uses thedebug_objectsinfra to track refs being lost or misused.In the future we can extend this structure to also catch those who fail to release the ref on unregistering notification.
Signed-off-by Jakub Kicinski <[email protected]>--- MAINTAINERS | 1 + include/linux/netdev_refs.h | 104 ++++++++++++++++++++++++++++++++++++ lib/Kconfig.debug | 7 +++ net/core/dev.c | 8 +++ 4 files changed, 120 insertions(+) create mode 100644 include/linux/netdev_refs.hMAINTAINERS +1
-0@@ -18482,6 +18482,7 @@ F: include/uapi/linux/pkt_sched.h F: include/uapi/linux/tc_act/ F: include/uapi/linux/tc_ematch/ F: net/sched/ +F: tools/testing/selftests/tc-testing/ TC90522 MEDIA DRIVER M: Akihiro Tsukada <[email protected]>include/linux/netdev_refs.h new file +22
-0@@ -0,0 +1,104 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ + +#ifndef _LINUX_NETDEV_REFS_H +#define _LINUX_NETDEV_REFS_H + +#include <linux/debugobjects.h> +#include <linux/netdevice.h> + +/* Explicit netdevice references +* struct netdev_ref is a storage for a reference. It's equivalent +* to a netdev pointer, but when debug is enabled it performs extra checks. +* Most users will want to take a reference with netdev_hold(), access it +* via netdev_ref_ptr() and release with netdev_put(). +*/ + +struct netdev_ref { +struct net_device *dev; +#ifdef CONFIG_DEBUG_OBJECTS_NETDEV_REFS +refcount_t cnt; +#endif +}; +
include/linux/netdev_refs.h line 32
Quoted 10 lines
include/linux/netdev_refs.h new file +10
-0+extern const struct debug_obj_descr netdev_ref_debug_descr; + +/* Store a raw, unprotected pointer */ +static inline void __netdev_ref_store (struct netdev_ref *ref, +struct net_device *dev) +{ + ref->dev = dev; + +#ifdef CONFIG_DEBUG_OBJECTS_NETDEV_REFS +refcount_set (&ref->cnt,0 );
This is very uncommon pattern. I would expect that first pointer access will start from 1, like all refcount_t users. If you still prefer to start from 0, i suggest you to use atomic_t.
IMHO, much better will be to use kref for this type of reference counting.
Thanks
Jakub Kicinski
maintainerRe: [RFC net-next 1/2] net: add netdev_refs debug
Quoted 4 lines · On Wed, 17 Nov 2021 20:24:17 +0200 Leon Romanovsky wrote:
Quoted 4 lines
include/linux/netdev_refs.h new file +4
-0+/* Store a raw, unprotected pointer */ +static inline void __netdev_ref_store (struct netdev_ref *ref, +struct net_device *dev) +{
Quoted 8 lines
include/linux/netdev_refs.h line 32
Quoted 4 lines
include/linux/netdev_refs.h new file +4
-0+ ref->dev = dev; + +#ifdef CONFIG_DEBUG_OBJECTS_NETDEV_REFS +refcount_set (&ref->cnt,0 );This is very uncommon pattern. I would expect that first pointer access will start from 1, like all
refcount_tusers. If you still prefer to start from 0, i suggest you to useatomic_t.
It's not really "starting from 0", it's more of a "setting the count to invalid". It can't escape from this state with a simple inc.
Quoted 1 line
IMHO, much better will be to use kref for this type of reference counting.
I'm not really sure if the netdev_ref_{get,put}() part is necessary.
Main pattern I'm trying to replace is a pointer which is optionally holding a reference (0, 1 references). Rather than a pointer which is ref counted (1..n references).
IOW __netdev_ref_store() is much more likely to be used than netdev_ref_get(), that's also why netdev_put() does not invalidate the pointer itself.
Re: [RFC net-next 1/2] net: add netdev_refs debug
In reply to Jakub KicinskiQuoted 10 lines · Jakub Kicinski
Debugging netdev ref leaks is still pretty hard. Eric added optional use of a normal refcount which is useful for tracking abuse of existing users.
For new code, however, it'd be great if we could actually track the refs per-user. Allowing us to detect leaks where they happen. This patch introduces a
netdev_reftype and uses thedebug_objectsinfra to track refs being lost or misused.
Quoted 5 lines
In the future we can extend this structure to also catch those who fail to release the ref on unregistering notification.
Signed-off-by Jakub Kicinski <[email protected]>---
Interesting, I was working on something very different, let me send the RFC (it is absolutely not complete at this point)
Re: [RFC net-next 1/2] net: add netdev_refs debug
In reply to Jakub KicinskiQuoted 14 lines · Jakub Kicinski
Quoted 4 lines · On Wed, 17 Nov 2021 20:24:17 +0200 Leon Romanovsky wrote:
Quoted 4 lines
include/linux/netdev_refs.h new file +4
-0+/* Store a raw, unprotected pointer */ +static inline void __netdev_ref_store (struct netdev_ref *ref, +struct net_device *dev) +{Quoted 8 lines
include/linux/netdev_refs.h line 32
Quoted 4 lines
include/linux/netdev_refs.h new file +4
-0+ ref->dev = dev; + +#ifdef CONFIG_DEBUG_OBJECTS_NETDEV_REFS +refcount_set (&ref->cnt,0 );This is very uncommon pattern. I would expect that first pointer access will start from 1, like all
refcount_tusers. If you still prefer to start from 0, i suggest you to useatomic_t.
Quoted 2 lines
It's not really "starting from 0", it's more of a "setting the count to invalid". It can't escape from this state with a simple inc.
I understand it and this is what raises eyebrows. The refcount_t type has very clear semantics which you are stretching too far.
Let's see what Eric had in mind for his RFC.
Thanks