diff mbox series

[net-next,2/5] net: track netdev refcnt with obj_cnt

Message ID e09681e82af58902bf50254a380c491cfd8d36e0.1638849511.git.lucien.xin@gmail.com (mailing list archive)
State RFC
Delegated to: Netdev Maintainers
Headers show
Series net: add refcnt tracking for some common objects | expand

Checks

Context Check Description
netdev/tree_selection success Clearly marked for net-next
netdev/fixes_present success Fixes tag not required for -next series
netdev/subject_prefix success Link
netdev/cover_letter success Series has a cover letter
netdev/patch_count success Link
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 4757 this patch: 4757
netdev/cc_maintainers success CCed 4 of 4 maintainers
netdev/build_clang success Errors and warnings before: 852 this patch: 852
netdev/module_param success Was 0 now: 0
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 4911 this patch: 4911
netdev/checkpatch warning WARNING: line length of 84 exceeds 80 columns
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

Xin Long Dec. 7, 2021, 4:02 a.m. UTC
Two types are added into obj_cnt to count dev_hold and dev_put,
and all it does is put obj_cnt_track_by_dev() into these two
functions.

Here is an example to track the refcnt of a netdev named dummy0:

  # sysctl -w obj_cnt.control="clear" # clear the old result

  # sysctl -w obj_cnt.type=0x3     # enable dev_hold/put track
  # sysctl -w obj_cnt.name=dummy0  # count dev_hold/put(dummy0)
  # sysctl -w obj_cnt.nr_entries=4 # save 4 frames' call trace

  # ip link add dummy0 type dummy
  # ip link set dummy0 up
  # ip link set dummy0 down
  # ip link del dummy0

  # sysctl -w obj_cnt.control="scan"  # print the new result
  # dmesg
  OBJ_CNT: obj_cnt_dump: obj: ffff894402397000, type: dev_put, cnt: 1,:
       in_dev_finish_destroy+0x6a/0x80
       rcu_do_batch+0x164/0x4b0
       rcu_core+0x249/0x350
       __do_softirq+0xf5/0x2ea
  OBJ_CNT: obj_cnt_dump: obj: ffff894402397000, type: dev_hold, cnt: 1,:
       inetdev_init+0xff/0x1c0
       inetdev_event+0x4b7/0x600
       raw_notifier_call_chain+0x41/0x50
       register_netdevice+0x481/0x580
  ...
  OBJ_CNT: obj_cnt_dump: obj: ffff894402397000, type: dev_put, cnt: 1,:
       rx_queue_release+0xa8/0xb0
       kobject_release+0x43/0x140
       net_rx_queue_update_kobjects+0x13c/0x190
       netdev_unregister_kobject+0x4a/0x80
  OBJ_CNT: obj_cnt_dump: obj: ffff894402397000, type: dev_put, cnt: 3,:
       fib_nh_common_release+0x10f/0x120
       fib6_info_destroy_rcu+0x73/0xc0
       rcu_do_batch+0x164/0x4b0
       rcu_core+0x249/0x350
  ...

Signed-off-by: Xin Long <lucien.xin@gmail.com>
---
 include/linux/netdevice.h | 11 +++++++++++
 include/linux/obj_cnt.h   |  2 ++
 lib/obj_cnt.c             |  2 ++
 3 files changed, 15 insertions(+)
diff mbox series

Patch

diff --git a/include/linux/netdevice.h b/include/linux/netdevice.h
index 69dca1edd5a6..4cbcd34829da 100644
--- a/include/linux/netdevice.h
+++ b/include/linux/netdevice.h
@@ -49,6 +49,7 @@ 
 #include <linux/hashtable.h>
 #include <linux/rbtree.h>
 #include <linux/ref_tracker.h>
+#include <linux/obj_cnt.h>
 
 struct netpoll_info;
 struct device;
@@ -3815,6 +3816,14 @@  extern unsigned int	netdev_budget_usecs;
 /* Called by rtnetlink.c:rtnl_unlock() */
 void netdev_run_todo(void);
 
+static inline void obj_cnt_track_by_dev(void *obj, struct net_device *dev, int type)
+{
+#ifdef CONFIG_OBJ_CNT
+	if (dev)
+		obj_cnt_track(type, dev->ifindex, dev->name, obj);
+#endif
+}
+
 /**
  *	dev_put - release reference to device
  *	@dev: network device
@@ -3825,6 +3834,7 @@  void netdev_run_todo(void);
 static inline void dev_put(struct net_device *dev)
 {
 	if (dev) {
+		obj_cnt_track_by_dev(dev, dev, OBJ_CNT_DEV_PUT);
 #ifdef CONFIG_PCPU_DEV_REFCNT
 		this_cpu_dec(*dev->pcpu_refcnt);
 #else
@@ -3843,6 +3853,7 @@  static inline void dev_put(struct net_device *dev)
 static inline void dev_hold(struct net_device *dev)
 {
 	if (dev) {
+		obj_cnt_track_by_dev(dev, dev, OBJ_CNT_DEV_HOLD);
 #ifdef CONFIG_PCPU_DEV_REFCNT
 		this_cpu_inc(*dev->pcpu_refcnt);
 #else
diff --git a/include/linux/obj_cnt.h b/include/linux/obj_cnt.h
index e5185f7022d1..bb2d37484a32 100644
--- a/include/linux/obj_cnt.h
+++ b/include/linux/obj_cnt.h
@@ -3,6 +3,8 @@ 
 #define _LINUX_OBJ_CNT_H
 
 enum {
+	OBJ_CNT_DEV_HOLD,
+	OBJ_CNT_DEV_PUT,
 	OBJ_CNT_TYPE_MAX
 };
 
diff --git a/lib/obj_cnt.c b/lib/obj_cnt.c
index 19ced2303452..12a1fdafd632 100644
--- a/lib/obj_cnt.c
+++ b/lib/obj_cnt.c
@@ -15,6 +15,8 @@  static unsigned int		obj_cnt_num;
 static spinlock_t		obj_cnt_lock;
 
 static char *obj_cnt_str[OBJ_CNT_TYPE_MAX] = {
+	"dev_hold",
+	"dev_put"
 };
 
 struct obj_cnt {