Received: by 2002:a25:31c3:0:0:0:0:0 with SMTP id x186csp6243811ybx; Mon, 11 Nov 2019 06:15:12 -0800 (PST) X-Google-Smtp-Source: APXvYqxY3O5jsumFULofQjz4fhk9sL2LPF2Fio/BWSTltKxsonCD1NHvZt/nd3gOEkz8/DrOyy0h X-Received: by 2002:a17:906:4697:: with SMTP id a23mr22502118ejr.322.1573481712792; Mon, 11 Nov 2019 06:15:12 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1573481712; cv=none; d=google.com; s=arc-20160816; b=NFQ5j+vogV5pSO1aNtP26uXlocOgmtJ83oq/DZi75OaKPmb8iZBGA3Du/BCbw2hjPR NOC+DDkjKo1Oti83mfSUpRXgA4ssDvfxgBoKB/pZGEOHGjv56Wvm+DOdYJ0fQ63XXzVF QoA0379KYmspsdTjU586D4/rx40+mnu2Cwxor7IwmcqwBqkmKR7E+PScljprnnYyTwAG IX+9dBC3wQZ8VXq+SiE3UDxZ/vTjZ7rwIufcNU/2boXPt+wOLoTBzLgqqteYdssAAXBh u0prY/H1O/pmmrjVy7osDRkQ42EttqHXAje8Putai0vLyyFFa5iHEQRuN5mV3FqiVHrc FrRQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from; bh=ajklYA93s6LBBCx150LOOmcUV8Jvdm0ILXQmYeNAElA=; b=SbkZmJYi+s7op4JsHBYSkhzdvHVuYGxFbeYEP/JhjMlOHFsqH0BJTLIVdVCrwNY1Iw wCGuq85DeRntI49hUmxfYp/Kr8VhRWJRWZpTjNIZpDDpkcvtfd0seHa1paPQgagp2N09 eUwChuwSxRSWdcc3R5BwuwWXTK9wWlFhBQa1caA9oadtaxs0OGUIdcTGc9M2IncWMsHm CE3xcYirzIdzDigVjB66nx1m43x9FT+yeeDOS9NXajFshtF7hBd5Yr+8YdDQOgQHBNYs 05O84QkpFhLLJeQiMc4LULJKTl7ssVU6pp05H5NFUSMz+0BV60Z8ilD/VybGekskAIez UdEA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id d10si11518739edb.226.2019.11.11.06.14.48; Mon, 11 Nov 2019 06:15:12 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726946AbfKKOMN (ORCPT + 99 others); Mon, 11 Nov 2019 09:12:13 -0500 Received: from out30-130.freemail.mail.aliyun.com ([115.124.30.130]:39595 "EHLO out30-130.freemail.mail.aliyun.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726832AbfKKOMN (ORCPT ); Mon, 11 Nov 2019 09:12:13 -0500 X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R171e4;CH=green;DM=||false|;FP=0|-1|-1|-1|0|-1|-1|-1;HT=e01e04395;MF=tonylu@linux.alibaba.com;NM=1;PH=DS;RN=4;SR=0;TI=SMTPD_---0Tho9KK0_1573481523; Received: from localhost(mailfrom:tonylu@linux.alibaba.com fp:SMTPD_---0Tho9KK0_1573481523) by smtp.aliyun-inc.com(127.0.0.1); Mon, 11 Nov 2019 22:12:03 +0800 From: Tony Lu To: davem@davemloft.net Cc: shemminger@osdl.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH] net: remove static inline from dev_put/dev_hold Date: Mon, 11 Nov 2019 22:05:03 +0800 Message-Id: <20191111140502.17541-1-tonylu@linux.alibaba.com> X-Mailer: git-send-email 2.24.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This patch removes static inline from dev_put/dev_hold in order to help trace the pcpu_refcnt leak of net_device. We have sufferred this kind of issue for several times during manipulating NIC between different net namespaces. It prints this log in dmesg: unregister_netdevice: waiting for eth0 to become free. Usage count = 1 However, it is hard to find out who called and leaked refcnt in time. It only left the crime scene but few evidence. Once leaked, it is not safe to fix it up on the running host. We can't trace dev_put/dev_hold directly, for the functions are inlined and used wildly amoung modules. And this issue is common, there are tens of patches fix net_device refcnt leak for various causes. To trace the refcnt manipulating, this patch removes static inline from dev_put/dev_hold. We can use handy tools, such as eBPF with kprobe, to find out who holds but forgets to put refcnt. This will not be called frequently, so the overhead is limited. Signed-off-by: Tony Lu --- include/linux/netdevice.h | 24 ++++-------------------- net/core/dev.c | 24 ++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 20 deletions(-) diff --git a/include/linux/netdevice.h b/include/linux/netdevice.h index c20f190b4c18..872d266c6da5 100644 --- a/include/linux/netdevice.h +++ b/include/linux/netdevice.h @@ -3720,27 +3720,11 @@ extern unsigned int netdev_budget_usecs; /* Called by rtnetlink.c:rtnl_unlock() */ void netdev_run_todo(void); -/** - * dev_put - release reference to device - * @dev: network device - * - * Release reference to device to allow it to be freed. - */ -static inline void dev_put(struct net_device *dev) -{ - this_cpu_dec(*dev->pcpu_refcnt); -} +/* Release reference to device to allow it to be freed. */ +void dev_put(struct net_device *dev); -/** - * dev_hold - get reference to device - * @dev: network device - * - * Hold reference to device to keep it from being freed. - */ -static inline void dev_hold(struct net_device *dev) -{ - this_cpu_inc(*dev->pcpu_refcnt); -} +/* Hold reference to device to keep it from being freed. */ +void dev_hold(struct net_device *dev); /* Carrier loss detection, dial on demand. The functions netif_carrier_on * and _off may be called from IRQ context, but it is caller diff --git a/net/core/dev.c b/net/core/dev.c index 99ac84ff398f..620fb3d6718a 100644 --- a/net/core/dev.c +++ b/net/core/dev.c @@ -1294,6 +1294,30 @@ void netdev_notify_peers(struct net_device *dev) } EXPORT_SYMBOL(netdev_notify_peers); +/** + * dev_put - release reference to device + * @dev: network device + * + * Release reference to device to allow it to be freed. + */ +void dev_put(struct net_device *dev) +{ + this_cpu_dec(*dev->pcpu_refcnt); +} +EXPORT_SYMBOL(dev_put); + +/** + * dev_hold - get reference to device + * @dev: network device + * + * Hold reference to device to keep it from being freed. + */ +void dev_hold(struct net_device *dev) +{ + this_cpu_inc(*dev->pcpu_refcnt); +} +EXPORT_SYMBOL(dev_hold); + static int __dev_open(struct net_device *dev, struct netlink_ext_ack *extack) { const struct net_device_ops *ops = dev->netdev_ops; -- 2.24.0