From patchwork Mon Sep 14 17:24:49 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wei Wang X-Patchwork-Id: 260906 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.5 required=3.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_INVALID, DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 00CB2C433E2 for ; Mon, 14 Sep 2020 17:27:09 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id B1CB7208E4 for ; Mon, 14 Sep 2020 17:27:08 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=google.com header.i=@google.com header.b="a5opZK4r" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726038AbgINR0r (ORCPT ); Mon, 14 Sep 2020 13:26:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37140 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726258AbgINR0N (ORCPT ); Mon, 14 Sep 2020 13:26:13 -0400 Received: from mail-qk1-x74a.google.com (mail-qk1-x74a.google.com [IPv6:2607:f8b0:4864:20::74a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4F9B2C061788 for ; Mon, 14 Sep 2020 10:26:08 -0700 (PDT) Received: by mail-qk1-x74a.google.com with SMTP id r128so656354qkc.9 for ; Mon, 14 Sep 2020 10:26:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=sender:date:in-reply-to:message-id:mime-version:references:subject :from:to:cc; bh=RgCsCaqgYmgC1Mfvq5pQLQQYxiHHLMC+4ovtFO74twY=; b=a5opZK4rIQ7YDjDbYJ+2QVaMYI2KAbXkyKoDpN6EhfLpxLKrOpgJxGArBJKzRP9TgP fqWGwi7Zs16jcFSADA/YMarv6KYd7COZqI3/myEXOutxOInstfoWvosR7JDphj88/ZVs R9dDVAISE0Y8/kX0Jj1mR52jbLDLnIA/oJHnUYT5t/pjL/abM7O3Pen3yuWEfweHxAEe WDKDlZnXLjFRxhAsUMh0GLkoW+xD9dScICQoBCB8itkqQOqHQdxel0TbxX7QR3ijj5Qc 4imAOfPNrmteUL40AYJNW5VQ6YZln6xUuSv6mEzntBDZXIcCWT9y9dkgY+3wnmvJY7/t gsvA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=RgCsCaqgYmgC1Mfvq5pQLQQYxiHHLMC+4ovtFO74twY=; b=bTzw3+/47jUU7L31vs782OUO5dE55bKEk9YRw2oQ8uKerEXrm3oKY+NFfOqmsOMU4G ymOHW+iKMyjVcOg8RVQA32Gh4QDIGMEp/l8Js4CJElfr2fVxUpm6JI6ec65GAWnbmx6Z XkBpnIzDxqvTgONdT24SOZJZkSnOe5ZLy1XSMLJaYgiYLgZfuqqVpFHZMvd07CaZb2Yg O2QOpfELBtDE0JcwI4Apf1N0asDa2JGJuG2ZSugUIC+mKQSOyZhxM0b1FfGO4xR873ms O1ctnMX4bxX7xbnImAhPxeQUnZv+x9mbBoqVWVDKHbeGzDJWbSKTYbFx6LF81faOFMXI DQZg== X-Gm-Message-State: AOAM530HZiA6ICWBv1y5hHFEvL2WVmHBrhUwiNe6RXVwj0YJmmeNz70O JQIod3LoNw0nCxglcgtyvVDxWudXLxo= X-Google-Smtp-Source: ABdhPJzSc5vtgLiMS/r8RgBBjpYhyUOUivM35PDt0VyyHE24tGqdkgNAw14ZgJccRp+aJMqLyaPxT1elDS0= X-Received: from weiwan.svl.corp.google.com ([2620:15c:2c4:201:1ea0:b8ff:fe75:cf08]) (user=weiwan job=sendgmr) by 2002:a0c:c390:: with SMTP id o16mr14569352qvi.0.1600104367439; Mon, 14 Sep 2020 10:26:07 -0700 (PDT) Date: Mon, 14 Sep 2020 10:24:49 -0700 In-Reply-To: <20200914172453.1833883-1-weiwan@google.com> Message-Id: <20200914172453.1833883-3-weiwan@google.com> Mime-Version: 1.0 References: <20200914172453.1833883-1-weiwan@google.com> X-Mailer: git-send-email 2.28.0.618.gf4bc123cb7-goog Subject: [RFC PATCH net-next 2/6] net: add sysfs attribute to control napi threaded mode From: Wei Wang To: "David S . Miller" , netdev@vger.kernel.org Cc: Jakub Kicinski , Eric Dumazet , Paolo Abeni , Hannes Frederic Sowa , Felix Fietkau , Wei Wang Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org From: Paolo Abeni this patch adds a new sysfs attribute to the network device class. Said attribute is a bitmask that allows controlling the threaded mode for all the napi instances of the given network device. The threaded mode can be switched only if related network device is down. Signed-off-by: Paolo Abeni Signed-off-by: Hannes Frederic Sowa Signed-off-by: Wei Wang --- include/linux/netdevice.h | 1 + net/core/net-sysfs.c | 102 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+) diff --git a/include/linux/netdevice.h b/include/linux/netdevice.h index 6797eb356e2e..37941d6a911d 100644 --- a/include/linux/netdevice.h +++ b/include/linux/netdevice.h @@ -1965,6 +1965,7 @@ struct net_device { spinlock_t addr_list_lock; unsigned char name_assign_type; bool uc_promisc; + bool napi_threaded; struct netdev_hw_addr_list uc; struct netdev_hw_addr_list mc; struct netdev_hw_addr_list dev_addrs; diff --git a/net/core/net-sysfs.c b/net/core/net-sysfs.c index efec66fa78b7..0172457a1bfe 100644 --- a/net/core/net-sysfs.c +++ b/net/core/net-sysfs.c @@ -538,6 +538,107 @@ static ssize_t phys_switch_id_show(struct device *dev, } static DEVICE_ATTR_RO(phys_switch_id); +unsigned long *__alloc_thread_bitmap(struct net_device *netdev, int *bits) +{ + struct napi_struct *n; + + *bits = 0; + list_for_each_entry(n, &netdev->napi_list, dev_list) + (*bits)++; + + return kmalloc_array(BITS_TO_LONGS(*bits), sizeof(unsigned long), + GFP_ATOMIC | __GFP_ZERO); +} + +static ssize_t threaded_show(struct device *dev, + struct device_attribute *attr, char *buf) +{ + struct net_device *netdev = to_net_dev(dev); + struct napi_struct *n; + unsigned long *bmap; + size_t count = 0; + int i, bits; + + if (!rtnl_trylock()) + return restart_syscall(); + + if (!dev_isalive(netdev)) + goto unlock; + + bmap = __alloc_thread_bitmap(netdev, &bits); + if (!bmap) { + count = -ENOMEM; + goto unlock; + } + + i = 0; + list_for_each_entry(n, &netdev->napi_list, dev_list) { + if (test_bit(NAPI_STATE_THREADED, &n->state)) + set_bit(i, bmap); + i++; + } + + count = bitmap_print_to_pagebuf(true, buf, bmap, bits); + kfree(bmap); + +unlock: + rtnl_unlock(); + + return count; +} + +static ssize_t threaded_store(struct device *dev, + struct device_attribute *attr, + const char *buf, size_t len) +{ + struct net_device *netdev = to_net_dev(dev); + struct napi_struct *n; + unsigned long *bmap; + int i, bits; + size_t ret; + + if (!capable(CAP_NET_ADMIN)) + return -EPERM; + + if (!rtnl_trylock()) + return restart_syscall(); + + if (!dev_isalive(netdev)) { + ret = len; + goto unlock; + } + + if (netdev->flags & IFF_UP) { + ret = -EBUSY; + goto unlock; + } + + bmap = __alloc_thread_bitmap(netdev, &bits); + if (!bmap) { + ret = -ENOMEM; + goto unlock; + } + + ret = bitmap_parselist(buf, bmap, bits); + if (ret) + goto free_unlock; + + i = 0; + list_for_each_entry(n, &netdev->napi_list, dev_list) { + napi_set_threaded(n, test_bit(i, bmap)); + i++; + } + ret = len; + +free_unlock: + kfree(bmap); + +unlock: + rtnl_unlock(); + return ret; +} +static DEVICE_ATTR_RW(threaded); + static struct attribute *net_class_attrs[] __ro_after_init = { &dev_attr_netdev_group.attr, &dev_attr_type.attr, @@ -570,6 +671,7 @@ static struct attribute *net_class_attrs[] __ro_after_init = { &dev_attr_proto_down.attr, &dev_attr_carrier_up_count.attr, &dev_attr_carrier_down_count.attr, + &dev_attr_threaded.attr, NULL, }; ATTRIBUTE_GROUPS(net_class); From patchwork Mon Sep 14 17:24:51 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wei Wang X-Patchwork-Id: 260905 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.6 required=3.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_INVALID, DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 796E4C43461 for ; Mon, 14 Sep 2020 17:27:57 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 3BFD320771 for ; Mon, 14 Sep 2020 17:27:57 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=google.com header.i=@google.com header.b="O+YSkcEp" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726056AbgINR1k (ORCPT ); Mon, 14 Sep 2020 13:27:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37194 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726306AbgINR00 (ORCPT ); Mon, 14 Sep 2020 13:26:26 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1715EC06174A for ; Mon, 14 Sep 2020 10:26:26 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id e190so433325ybf.18 for ; Mon, 14 Sep 2020 10:26:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=sender:date:in-reply-to:message-id:mime-version:references:subject :from:to:cc; bh=LYZ1+juOAoa/emu0bMvEoA7YWrA+PmRXyRdY+YEq8Tw=; b=O+YSkcEpuAZmFQfPD/KixyL1MdeWGfam/Ed3HgnOtiD19nRsM+q4hIJNZ8iYDCMXO2 OJhS/UQ9yAQKswOrSJE1+layYfB8EHSM0LZCBEFM0qe7R+8cA8FCTTyqDQ6K5q7fmrvv 1cIgi5hBumdLaHswaWxlWz2ErltL0lngGk8vM/jEeqnUXFvU8ibUfY9kBg2qoPv2BT6H 6Z53xZdqI92Ng9VOmihDhUg9pELtMpnjrjKQZhx2lVOjGpoyCSz0q/GUaSHMMD8y6Ljx 2ee1aDgP9frXEOGveJv4p3HePBWaE7qa2C3lCG+tZl1myP6jlZ4w64dITo90N/JDZVQU NQ8Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=LYZ1+juOAoa/emu0bMvEoA7YWrA+PmRXyRdY+YEq8Tw=; b=rvfio7kuGitLYBEYmVJ50iG/dzYKWXdzWdBHpgVPyn9vB7c6QKhEdalklI6COMu7z1 dnThRUHh+BCzTWFJ0VhvsbL3zUV0YM2CMq+zxhuM2I4ZrX9iSj6aLzlxOMuV4EaNaABr NPUUs42QwfmfbdEDEoFomfMdHboDu1Sxm2R4CQ4pQWDkyq/hQ4LagGzMR1Ycv1bShyB3 3rEIkz/v7QSuEisEsV91vAw/2Tt5F/u+4LEm+96uSSeIbuHwvg709i/8hwpd2e8qYbf4 wjQTXJ35ciIlOabU6TUfcXNV6o/by6lvSfT4MZbSNafGi2zie4fkSiYllBz+TqKjZnsp f5Vg== X-Gm-Message-State: AOAM5326S/2RiJIhieq4MpqxWn2L3oviE0YgyKO/RIphMpsKATVYEKuX oB3aPdxzCPDk7ZZkxz8NpjFpDl/qBps= X-Google-Smtp-Source: ABdhPJzEDpRe07jBYHx33+e1peTFcjdq7HiVHjsAcCt51s3ColGJm2b58l6xOeggCjSPidNXsF0UTCaMdp8= X-Received: from weiwan.svl.corp.google.com ([2620:15c:2c4:201:1ea0:b8ff:fe75:cf08]) (user=weiwan job=sendgmr) by 2002:a25:f20d:: with SMTP id i13mr20998124ybe.152.1600104385278; Mon, 14 Sep 2020 10:26:25 -0700 (PDT) Date: Mon, 14 Sep 2020 10:24:51 -0700 In-Reply-To: <20200914172453.1833883-1-weiwan@google.com> Message-Id: <20200914172453.1833883-5-weiwan@google.com> Mime-Version: 1.0 References: <20200914172453.1833883-1-weiwan@google.com> X-Mailer: git-send-email 2.28.0.618.gf4bc123cb7-goog Subject: [RFC PATCH net-next 4/6] net: modify kthread handler to use __napi_poll() From: Wei Wang To: "David S . Miller" , netdev@vger.kernel.org Cc: Jakub Kicinski , Eric Dumazet , Paolo Abeni , Hannes Frederic Sowa , Felix Fietkau , Wei Wang Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org From: Jakub Kicinski The current kthread handler calls napi_poll() and has to pass a dummy repoll list to the function, which seems redundent. The new proposed kthread handler calls the newly proposed __napi_poll(), and respects napi->weight as before. If repoll is needed, cond_resched() is called first to give other tasks a chance to run before repolling. This change is proposed by Jakub Kicinski on top of the previous patch. Signed-off-by: Jakub Kicinski Signed-off-by: Wei Wang --- net/core/dev.c | 62 +++++++++++++++++++------------------------------- 1 file changed, 24 insertions(+), 38 deletions(-) diff --git a/net/core/dev.c b/net/core/dev.c index bc2a7681b239..be676c21bdc4 100644 --- a/net/core/dev.c +++ b/net/core/dev.c @@ -6763,6 +6763,15 @@ static int __napi_poll(struct napi_struct *n, bool *repoll) gro_normal_list(n); + /* Some drivers may have called napi_schedule + * prior to exhausting their budget. + */ + if (unlikely(!list_empty(&n->poll_list))) { + pr_warn_once("%s: Budget exhausted after napi rescheduled\n", + n->dev ? n->dev->name : "backlog"); + return work; + } + *repoll = true; return work; @@ -6783,15 +6792,6 @@ static int napi_poll(struct napi_struct *n, struct list_head *repoll) if (!do_repoll) goto out_unlock; - /* Some drivers may have called napi_schedule - * prior to exhausting their budget. - */ - if (unlikely(!list_empty(&n->poll_list))) { - pr_warn_once("%s: Budget exhausted after napi rescheduled\n", - n->dev ? n->dev->name : "backlog"); - goto out_unlock; - } - list_add_tail(&n->poll_list, repoll); out_unlock: @@ -6820,40 +6820,26 @@ static int napi_thread_wait(struct napi_struct *napi) static int napi_threaded_poll(void *data) { struct napi_struct *napi = data; + void *have; while (!napi_thread_wait(napi)) { - struct list_head dummy_repoll; - int budget = netdev_budget; - unsigned long time_limit; - bool again = true; + for (;;) { + bool repoll = false; - INIT_LIST_HEAD(&dummy_repoll); - local_bh_disable(); - time_limit = jiffies + 2; - do { - /* ensure that the poll list is not empty */ - if (list_empty(&dummy_repoll)) - list_add(&napi->poll_list, &dummy_repoll); - - budget -= napi_poll(napi, &dummy_repoll); - if (unlikely(budget <= 0 || - time_after_eq(jiffies, time_limit))) { - cond_resched(); - - /* refresh the budget */ - budget = netdev_budget; - __kfree_skb_flush(); - time_limit = jiffies + 2; - } + local_bh_disable(); - if (napi_disable_pending(napi)) - again = false; - else if (!test_bit(NAPI_STATE_SCHED, &napi->state)) - again = false; - } while (again); + have = netpoll_poll_lock(napi); + __napi_poll(napi, &repoll); + netpoll_poll_unlock(have); - __kfree_skb_flush(); - local_bh_enable(); + __kfree_skb_flush(); + local_bh_enable(); + + if (!repoll) + break; + + cond_resched(); + } } return 0; }