From patchwork Tue Apr 10 17:08:11 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Saeed Mahameed X-Patchwork-Id: 896831 X-Patchwork-Delegate: davem@davemloft.net Return-Path: X-Original-To: patchwork-incoming-netdev@ozlabs.org Delivered-To: patchwork-incoming-netdev@ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=netdev-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=mellanox.com Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 40LDCb6spVz9s2S for ; Wed, 11 Apr 2018 03:08:43 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751825AbeDJRIl (ORCPT ); Tue, 10 Apr 2018 13:08:41 -0400 Received: from mail-il-dmz.mellanox.com ([193.47.165.129]:52776 "EHLO mellanox.co.il" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1751490AbeDJRIk (ORCPT ); Tue, 10 Apr 2018 13:08:40 -0400 Received: from Internal Mail-Server by MTLPINE1 (envelope-from saeedm@mellanox.com) with ESMTPS (AES256-SHA encrypted); 10 Apr 2018 20:09:48 +0300 Received: from stpd.mtl.com ([172.16.5.27]) by labmailer.mlnx (8.13.8/8.13.8) with ESMTP id w3AH8bWu005725; Tue, 10 Apr 2018 20:08:38 +0300 From: Saeed Mahameed To: netdev@vger.kernel.org Cc: Saeed Mahameed Subject: [RFC net-next 1/2] net: net-porcfs: Reduce rcu lock critical section Date: Tue, 10 Apr 2018 10:08:11 -0700 Message-Id: <20180410170812.18905-1-saeedm@mellanox.com> X-Mailer: git-send-email 2.14.3 Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org The current net proc fs sequence file implementation to show current namespace netdevs list statistics and mc lists holds the rcu lock throughout the whole process, from dev seq start up to dev seq stop. This is really greedy and demanding from device drivers since ndo_get_stats64 called from dev_seq_show while the rcu lock is held. The rcu lock is needed to guarantee that device chain is not modified while the dev sequence file is walking through it and handling the netdev in the same time. To minimize this critical section and drastically reduce the time rcu lock is being held, all we need is to grab the rcu lock only for the brief moment where we are looking for the next netdev to handle, if found, dev_hold it to guarantee it kept alive while accessed later in seq show callback and release the rcu lock immediately. The current netdev being handled will be released "dev_put" when the seq next callback is called or dev seq stop is called. Signed-off-by: Saeed Mahameed --- net/core/net-procfs.c | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/net/core/net-procfs.c b/net/core/net-procfs.c index 9737302907b1..9d5ce6a203d2 100644 --- a/net/core/net-procfs.c +++ b/net/core/net-procfs.c @@ -31,19 +31,24 @@ static inline struct net_device *dev_from_same_bucket(struct seq_file *seq, loff static inline struct net_device *dev_from_bucket(struct seq_file *seq, loff_t *pos) { - struct net_device *dev; + struct net_device *dev = NULL; unsigned int bucket; + rcu_read_lock(); do { dev = dev_from_same_bucket(seq, pos); - if (dev) - return dev; + if (dev) { + dev_hold(dev); + goto unlock; + } bucket = get_bucket(*pos) + 1; *pos = set_bucket_offset(bucket, 1); } while (bucket < NETDEV_HASHENTRIES); - return NULL; +unlock: + rcu_read_unlock(); + return dev; } /* @@ -51,9 +56,7 @@ static inline struct net_device *dev_from_bucket(struct seq_file *seq, loff_t *p * in detail. */ static void *dev_seq_start(struct seq_file *seq, loff_t *pos) - __acquires(RCU) { - rcu_read_lock(); if (!*pos) return SEQ_START_TOKEN; @@ -66,13 +69,15 @@ static void *dev_seq_start(struct seq_file *seq, loff_t *pos) static void *dev_seq_next(struct seq_file *seq, void *v, loff_t *pos) { ++*pos; + if (v && v != SEQ_START_TOKEN) + dev_put(v); return dev_from_bucket(seq, pos); } static void dev_seq_stop(struct seq_file *seq, void *v) - __releases(RCU) { - rcu_read_unlock(); + if (v && v != SEQ_START_TOKEN) + dev_put(v); } static void dev_seq_printf_stats(struct seq_file *seq, struct net_device *dev) From patchwork Tue Apr 10 17:08:12 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Saeed Mahameed X-Patchwork-Id: 896832 X-Patchwork-Delegate: davem@davemloft.net Return-Path: X-Original-To: patchwork-incoming-netdev@ozlabs.org Delivered-To: patchwork-incoming-netdev@ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=netdev-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=mellanox.com Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 40LDCh5ck3z9s2S for ; Wed, 11 Apr 2018 03:08:48 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752041AbeDJRIq (ORCPT ); Tue, 10 Apr 2018 13:08:46 -0400 Received: from mail-il-dmz.mellanox.com ([193.47.165.129]:52797 "EHLO mellanox.co.il" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1751845AbeDJRIp (ORCPT ); Tue, 10 Apr 2018 13:08:45 -0400 Received: from Internal Mail-Server by MTLPINE1 (envelope-from saeedm@mellanox.com) with ESMTPS (AES256-SHA encrypted); 10 Apr 2018 20:09:50 +0300 Received: from stpd.mtl.com ([172.16.5.27]) by labmailer.mlnx (8.13.8/8.13.8) with ESMTP id w3AH8bWv005725; Tue, 10 Apr 2018 20:08:39 +0300 From: Saeed Mahameed To: netdev@vger.kernel.org Cc: Saeed Mahameed Subject: [RFC net-next 2/2] net: net-sysfs: Reduce netstat_show read_lock critical section Date: Tue, 10 Apr 2018 10:08:12 -0700 Message-Id: <20180410170812.18905-2-saeedm@mellanox.com> X-Mailer: git-send-email 2.14.3 In-Reply-To: <20180410170812.18905-1-saeedm@mellanox.com> References: <20180410170812.18905-1-saeedm@mellanox.com> Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org Instead of holding the device chain read_lock also while calling dev_get_stats just hold it only to check dev_isalive, if the dev is alive, hold that dev via dev_hold then release the read_lock. When done handling the device, dev_put it. Signed-off-by: Saeed Mahameed --- net/core/net-sysfs.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/net/core/net-sysfs.c b/net/core/net-sysfs.c index c476f0794132..ee6f9fed43df 100644 --- a/net/core/net-sysfs.c +++ b/net/core/net-sysfs.c @@ -563,13 +563,20 @@ static ssize_t netstat_show(const struct device *d, offset % sizeof(u64) != 0); read_lock(&dev_base_lock); - if (dev_isalive(dev)) { + if (dev_isalive(dev)) + dev_hold(dev); + else + dev = NULL; + read_unlock(&dev_base_lock); + + if (dev) { struct rtnl_link_stats64 temp; const struct rtnl_link_stats64 *stats = dev_get_stats(dev, &temp); + dev_put(dev); ret = sprintf(buf, fmt_u64, *(u64 *)(((u8 *)stats) + offset)); } - read_unlock(&dev_base_lock); + return ret; }