From patchwork Mon Feb 19 09:58:38 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kirill Tkhai X-Patchwork-Id: 875062 X-Patchwork-Delegate: davem@davemloft.net Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@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; dkim=pass (1024-bit key; unprotected) header.d=virtuozzo.com header.i=@virtuozzo.com header.b="NRAh4kSL"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 3zlK2Z36cgz9rxx for ; Mon, 19 Feb 2018 20:58:46 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752475AbeBSJ6o (ORCPT ); Mon, 19 Feb 2018 04:58:44 -0500 Received: from mail-he1eur01on0114.outbound.protection.outlook.com ([104.47.0.114]:63680 "EHLO EUR01-HE1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1752465AbeBSJ6m (ORCPT ); Mon, 19 Feb 2018 04:58:42 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=virtuozzo.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=A9Zn58SobvqfIQHuI5FuwKhb/kX6yNutViHluGGBBT8=; b=NRAh4kSLoQIR0niyK3vNcqXa6cE7a6MNSoDH+WnmoG2o/rumpuGqy0HODeBpaxwC8v5E2i53Bvieuszivxl3tnTSAz6MAOpVC6ZGo9pgzTxSV4p5OEkVvibpn9V14sL8iKvfQXyeMYB+Frwp+0mSPPu8LwP+ggTh4cn/zOki+N8= Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=ktkhai@virtuozzo.com; Received: from localhost.localdomain (195.214.232.6) by HE1PR0801MB1338.eurprd08.prod.outlook.com (2603:10a6:3:39::28) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P256) id 15.20.506.18; Mon, 19 Feb 2018 09:58:40 +0000 Subject: [PATCH 1/3] net: Kill net_mutex From: Kirill Tkhai To: davem@davemloft.net, nicolas.dichtel@6wind.com, vyasevic@redhat.com, ktkhai@virtuozzo.com, paulmck@linux.vnet.ibm.com, vyasevich@gmail.com, mark.rutland@arm.com, gregkh@linuxfoundation.org, leonro@mellanox.com, avagin@virtuozzo.com, fw@strlen.de, roman.kapl@sysgo.com, netdev@vger.kernel.org Date: Mon, 19 Feb 2018 12:58:38 +0300 Message-ID: <151903431795.8021.5069678488269142143.stgit@localhost.localdomain> In-Reply-To: <151903409491.8021.11032992295248447417.stgit@localhost.localdomain> References: <151903409491.8021.11032992295248447417.stgit@localhost.localdomain> User-Agent: StGit/0.18 MIME-Version: 1.0 X-Originating-IP: [195.214.232.6] X-ClientProxiedBy: HE1PR0701CA0076.eurprd07.prod.outlook.com (2603:10a6:3:64::20) To HE1PR0801MB1338.eurprd08.prod.outlook.com (2603:10a6:3:39::28) X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 03a899c1-9a8f-41e8-bb49-08d5777f5a6e X-Microsoft-Antispam: UriScan:; BCL:0; PCL:0; RULEID:(7020095)(4652020)(5600026)(4604075)(4534165)(7168020)(4627221)(201703031133081)(201702281549075)(2017052603307)(7153060)(7193020); SRVR:HE1PR0801MB1338; X-Microsoft-Exchange-Diagnostics: 1; HE1PR0801MB1338; 3:0ch9YToh38jFUqAC1RuOdIguqLXuGnZejk2dv+kLhhBQxTYf1uACTH+rD2wbNRoBrs390dTP0ULrVUDDSzg5bItpS/ExpRcWnTb7NDwJWEvLZoOP3sGrETOn4aRGnDDalIk6MORRbzuAvTmjCJ1GdyE2gHTA66AaIGIlZhtP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5riyiswqvNUAPpdCfmMQSokuMu8aR2zuIKBumHAzcrEzeSF2KLWFQabt3uJZB/P43BrdEeSGfeumZ6cpBHBld+BD0rkUfMCHrKv5oTtitlrH7+5u1GVdZrEv1Lo227wP1QvD41PGMwz80gWnj48Lua7CI7I3BrkGWzKtJYx3PC4+esI5klpE4pT; 4:RITB8fCeY6WhZ7bhaCeUu7CzStJZu9VvaX4o0sr1juYLn6trqTCoA1ZI+6tze1hKgqhJMcMwdNhpQ5nWMm/xX15x8lJOgnb0sb5aiTSh7ojwh1hvko2Azv7EvHPflM1h1DXWnyvd2r3YCuLDwAK2bzys656OuFfk62zSmxtN8CCx0Yfg+aHbmdMKqW6vMm6F6VJcFO+21ahE8xIut2bXs/auHes0qZJXkDzGOSk8J2TG6Xdfpqa4ytavzP0QR8Ibw4M/ZYoxfgTNmNX/BRIlTQ== X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:; X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(6040501)(2401047)(8121501046)(5005006)(10201501046)(93006095)(93001095)(3231101)(944501161)(3002001)(6041288)(201703131423095)(201702281528075)(20161123555045)(201703061421075)(201703061406153)(20161123560045)(20161123562045)(20161123558120)(20161123564045)(6072148)(201708071742011); SRVR:HE1PR0801MB1338; BCL:0; PCL:0; RULEID:; SRVR:HE1PR0801MB1338; X-Forefront-PRVS: 0588B2BD96 X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10019020)(6069001)(346002)(39380400002)(39840400004)(366004)(376002)(396003)(189003)(199004)(61506002)(47776003)(66066001)(39060400002)(106356001)(6116002)(3846002)(2906002)(8676002)(81156014)(81166006)(8936002)(7416002)(83506002)(7696005)(52116002)(25786009)(76176011)(50466002)(68736007)(58126008)(103116003)(305945005)(2486003)(16526019)(23676004)(7736002)(316002)(186003)(26005)(97736004)(59450400001)(33896004)(575784001)(86362001)(55236004)(6506007)(386003)(5660300001)(2950100002)(9686003)(478600001)(230700001)(53936002)(55016002)(105586002)(921003)(1121003); DIR:OUT; SFP:1102; SCL:1; SRVR:HE1PR0801MB1338; H:localhost.localdomain; FPR:; SPF:None; PTR:InfoNoRecords; MX:1; A:1; LANG:en; Received-SPF: None (protection.outlook.com: virtuozzo.com does not designate permitted sender hosts) X-Microsoft-Exchange-Diagnostics: =?utf-8?q?1=3BHE1PR0801MB1338=3B23=3ATr?= =?utf-8?q?lgvnqWBgMAEAUhYZLXI5KtGS9GItKjcT1i60G+xodUhtoQ3hVZn4RoiL?= =?utf-8?q?5/ZNDyzsf5tkxRc7I8jw8ZopldBOFtlsCDowigXU16LoTG8Xr4iO1dB3?= =?utf-8?q?s3mif9oBY5SVNUx/BLHTJLfiauRGWPFkA7xwcY/m0yaSoTHIoHj/SlLB?= =?utf-8?q?JK0FTMJLliUf78yxWr9xkoE7Kxe+a8PIpPGMSpPHmWDClBGtYGl8FQfs?= =?utf-8?q?Ivdk4ovA+KyPcZjtt5bUWYmeEC1eNWnxvMDcOXf/N/Z8L/PYT0wRb4nK?= =?utf-8?q?8ofVq5fqa9yme/PY7aDNghNuwLquPXiG3MeuaoWszvoshPGX12aRRyaM?= =?utf-8?q?lFb1frT00bAlwDOyt79FKeCQgvbYGKGjukMqA9FCPP1Od8aD32VYvFhZ?= =?utf-8?q?6QIXW/yjYvgUH7SQBEMiJ5D7O1lAwMVSOxjTGL/0KlbA1WOD5q8J+3Dj?= =?utf-8?q?FAVFV553YnHQQsb17QroNY3ySdyRxLy4X4P+sowNiVpOJD5SIF4LdqyI?= =?utf-8?q?ydebiO0P0S3tFYYR5r5iucIi8qVh21spqBuSHlAUOhyIFMkvOtvrvKYs?= =?utf-8?q?ztbTtSfWVBisxF5gBWe3ZjRgI5WteIsNHoFOwoXo3OkD8d4+1W/y0RTZ?= =?utf-8?q?wksaODYFOtwMDEXr+FEc4UhxkUgrhB0u6mYdL5rqPdMt6/Xbx9EktPyi?= =?utf-8?q?+B4/UGVkMtSayzsaGkeyREjE9SrzQKp61QnZUO+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wVKSNYQ24vnCWolQ/Frf7yHZYdM9o9I4YrMBfAUmBzFXh9I=; 5:d9whSd6211xzF8v+SNw94XiOBA+s29JfzflQhgm8ShnJ5tgAbbP5CW6gjvPIGHxrcoTGDsShMzOYYaMH3Kg+oMNDrkRXX2wHxYRKB920NQVaFrQufpe51nGGVpro6LRjFMoKmUt7TSGb3Nrp/QPd/2s9ghDLoAAin2lsVo0fkyY=; 24:07LgENFHSPeV4qjTa6ayHWfq7ArqrFhrKysnJKmi/+wcBXfo6bcLws6pacdgVB9XTNLscLji9ZEHhV+25AvmnhCuMMkAL68dDTuSVqZhFBs=; 7:wd25oZH3zGotX/xkOCVt4iJnWUTb7Ia567akeQNvo4s9LuiA9Vi+QaUPLnyod0sQm0/eOlYQ5D79Oopv6EJjDcjJ7r40NhyF01cYHWURv2AcJxEU9YGleJyLAf7+dAKZe7Jjj2IMDpGJ8nvMxDqWhhV4v6IIKQdpq+NUHXL+MLtA8yO0tq0YPLv5NWWYLofg3mJbeiP3JFhGYtIi7SXKafKSSgZVDFtF6PmJIjjUIhjGDxrkWTMYgfd7utMrBo57 SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1; HE1PR0801MB1338; 20:pYbVN9RwXqUc299/5GGx96IBnn9dudMxFVVWLcEb+eI60OAl7uFO72f3OBtNaVQ7n/vmqbOyk2v2Es4GTFUFzYyW1j6adOwUDJ2wsgN1ol4odhFZpGM7h04BlE6g4SV1RtuVb9MKdKIwp4GVlsYIVUWGNntzwvlfBlVrIf2WyKc= X-OriginatorOrg: virtuozzo.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 19 Feb 2018 09:58:40.0332 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 03a899c1-9a8f-41e8-bb49-08d5777f5a6e X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 0bc7f26d-0264-416e-a6fc-8352af79c58f X-MS-Exchange-Transport-CrossTenantHeadersStamped: HE1PR0801MB1338 Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org We take net_mutex, when there are !async pernet_operations registered, and read locking of net_sem is not enough. But we may get rid of taking the mutex, and just change the logic to write lock net_sem in such cases. This obviously reduces the number of lock operations, we do. Signed-off-by: Kirill Tkhai --- include/linux/rtnetlink.h | 1 - include/net/net_namespace.h | 11 ++++++--- net/core/net_namespace.c | 53 ++++++++++++++++++++++++++----------------- 3 files changed, 39 insertions(+), 26 deletions(-) diff --git a/include/linux/rtnetlink.h b/include/linux/rtnetlink.h index e9ee9ad0a681..3573b4bf2fdf 100644 --- a/include/linux/rtnetlink.h +++ b/include/linux/rtnetlink.h @@ -35,7 +35,6 @@ extern int rtnl_trylock(void); extern int rtnl_is_locked(void); extern wait_queue_head_t netdev_unregistering_wq; -extern struct mutex net_mutex; extern struct rw_semaphore net_sem; #ifdef CONFIG_PROVE_LOCKING diff --git a/include/net/net_namespace.h b/include/net/net_namespace.h index 9158ec1ad06f..115b01b92f4d 100644 --- a/include/net/net_namespace.h +++ b/include/net/net_namespace.h @@ -60,8 +60,11 @@ struct net { struct list_head list; /* list of network namespaces */ struct list_head cleanup_list; /* namespaces on death row */ - struct list_head exit_list; /* Use only net_mutex */ - + struct list_head exit_list; /* To linked to call pernet exit + * methods on dead net (net_sem + * read locked), or to unregister + * pernet ops (net_sem wr locked). + */ struct user_namespace *user_ns; /* Owning user namespace */ struct ucounts *ucounts; spinlock_t nsid_lock; @@ -89,7 +92,7 @@ struct net { /* core fib_rules */ struct list_head rules_ops; - struct list_head fib_notifier_ops; /* protected by net_mutex */ + struct list_head fib_notifier_ops; /* protected by net_sem */ struct net_device *loopback_dev; /* The loopback */ struct netns_core core; @@ -316,7 +319,7 @@ struct pernet_operations { /* * Indicates above methods are allowed to be executed in parallel * with methods of any other pernet_operations, i.e. they are not - * need synchronization via net_mutex. + * need write locked net_sem. */ bool async; }; diff --git a/net/core/net_namespace.c b/net/core/net_namespace.c index bcab9a938d6f..e89a516620dd 100644 --- a/net/core/net_namespace.c +++ b/net/core/net_namespace.c @@ -29,8 +29,6 @@ static LIST_HEAD(pernet_list); static struct list_head *first_device = &pernet_list; -/* Used only if there are !async pernet_operations registered */ -DEFINE_MUTEX(net_mutex); LIST_HEAD(net_namespace_list); EXPORT_SYMBOL_GPL(net_namespace_list); @@ -407,6 +405,7 @@ struct net *copy_net_ns(unsigned long flags, { struct ucounts *ucounts; struct net *net; + unsigned write; int rv; if (!(flags & CLONE_NEWNET)) @@ -424,20 +423,26 @@ struct net *copy_net_ns(unsigned long flags, refcount_set(&net->passive, 1); net->ucounts = ucounts; get_user_ns(user_ns); - - rv = down_read_killable(&net_sem); +again: + write = READ_ONCE(nr_sync_pernet_ops); + if (write) + rv = down_write_killable(&net_sem); + else + rv = down_read_killable(&net_sem); if (rv < 0) goto put_userns; - if (nr_sync_pernet_ops) { - rv = mutex_lock_killable(&net_mutex); - if (rv < 0) - goto up_read; + + if (!write && unlikely(READ_ONCE(nr_sync_pernet_ops))) { + up_read(&net_sem); + goto again; } rv = setup_net(net, user_ns); - if (nr_sync_pernet_ops) - mutex_unlock(&net_mutex); -up_read: - up_read(&net_sem); + + if (write) + up_write(&net_sem); + else + up_read(&net_sem); + if (rv < 0) { put_userns: put_user_ns(user_ns); @@ -485,15 +490,23 @@ static void cleanup_net(struct work_struct *work) struct net *net, *tmp, *last; struct list_head net_kill_list; LIST_HEAD(net_exit_list); + unsigned write; /* Atomically snapshot the list of namespaces to cleanup */ spin_lock_irq(&cleanup_list_lock); list_replace_init(&cleanup_list, &net_kill_list); spin_unlock_irq(&cleanup_list_lock); +again: + write = READ_ONCE(nr_sync_pernet_ops); + if (write) + down_write(&net_sem); + else + down_read(&net_sem); - down_read(&net_sem); - if (nr_sync_pernet_ops) - mutex_lock(&net_mutex); + if (!write && unlikely(READ_ONCE(nr_sync_pernet_ops))) { + up_read(&net_sem); + goto again; + } /* Don't let anyone else find us. */ rtnl_lock(); @@ -528,14 +541,14 @@ static void cleanup_net(struct work_struct *work) list_for_each_entry_reverse(ops, &pernet_list, list) ops_exit_list(ops, &net_exit_list); - if (nr_sync_pernet_ops) - mutex_unlock(&net_mutex); - /* Free the net generic variables */ list_for_each_entry_reverse(ops, &pernet_list, list) ops_free_list(ops, &net_exit_list); - up_read(&net_sem); + if (write) + up_write(&net_sem); + else + up_read(&net_sem); /* Ensure there are no outstanding rcu callbacks using this * network namespace. @@ -563,8 +576,6 @@ static void cleanup_net(struct work_struct *work) void net_ns_barrier(void) { down_write(&net_sem); - mutex_lock(&net_mutex); - mutex_unlock(&net_mutex); up_write(&net_sem); } EXPORT_SYMBOL(net_ns_barrier);