Received: by 2002:a05:6a11:4021:0:0:0:0 with SMTP id ky33csp22368pxb; Mon, 13 Sep 2021 11:53:45 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx1R5mdHPKR+HdQHg3o+1S5KdGkhg2AgLnN0k/8lv42ppnmUDWq2Xf/AChj09aO5yLabplx X-Received: by 2002:a92:cd81:: with SMTP id r1mr9030097ilb.279.1631559225385; Mon, 13 Sep 2021 11:53:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1631559225; cv=none; d=google.com; s=arc-20160816; b=OBMuO43Yogi7Rq2NpCBincGYk3WHeKHql/K9b8+xGcsiNxnm/5lsSN2Lkq/JjkHO9p Lqdf5uP34B/enHCYHTTTyJJY+d6V/dClUn7v7sGyRkAQj6F5+TguHZhqNaDLTGsH4nNJ FwXm73TP55ShHyBt16iXRmy3xkNlDRXPC6XhSRDGm0iGNRFQAYT9OyYwXRz8/Z5kEKbm UpXBAj/ZinKlQcFISYW5cjDPs8GzCpFqhDy70CtW6hExKA1415H/lFFtR8mOLHy+1AjY Bg4AnaUEPdCDpqlQnfUbVQKUr44n9X0BAl7BcpLlIgmR4xBePeCb6LOsz/rOjODdXnhX iRIg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=2l6sr7OCX2eDlfk3h6Ej8u6NldC12DXGGdW00d8YS9I=; b=IZ1b17bx7iBgtszj4aOaLNNncuEtWBLOlNcFbsqwCjfZ4tLRrmBt9T8GiNGhymGc0V +9Vbd1EfKIsJFsdWUEeu5sLjxVAyZKKtTTdUQ4ZFl+tS060jjUPBrXCGu7C6A5+s+LR7 d658g2BFQYwCLMOrlC2F/HEkQxVFqYWNVxtg+LxKedm6wJXolOiwWoDi7DRPBh+sRHMi JbilBw0cmMu+ZTA7PTs12pUrp7v4YggYL+LwDgN5HA5KtfZLlwmIe0Hw3wMORNHFx3Bn zpt0SkV651k2M5JM6Aag67aZQfbBqYslkJ2cGo5Tk3MjrIeGrZ7jp90jOxzt49awg5u+ l24A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="vh9RA/su"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id x3si7744076ile.168.2021.09.13.11.53.33; Mon, 13 Sep 2021 11:53:45 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="vh9RA/su"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241972AbhIMN0X (ORCPT + 99 others); Mon, 13 Sep 2021 09:26:23 -0400 Received: from mail.kernel.org ([198.145.29.99]:34832 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240591AbhIMNWJ (ORCPT ); Mon, 13 Sep 2021 09:22:09 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id C55FC6113E; Mon, 13 Sep 2021 13:20:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1631539238; bh=eLJ9Sol2cQltCVDqOhPRp1+wi1Jp3Nz8GOsN49x/vds=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=vh9RA/suusRowSGVGYiRwyUgugJ4eL9Z20cXoXeG5VKND96toOhzbF7gYcpfwqV5q XGnUSirdX9qGGcgnH2tcdo/zmat94a8vNfL/2qPTszkdEYCKnq5OtL2KThX+mibZPM KuDQZgl5ybmgSO1dQqZpBH4p8dsJxD9qCWXQyupg= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Guillaume Nault , "David S. Miller" , =?UTF-8?q?H=C3=A5kon=20Bugge?= Subject: [PATCH 5.4 062/144] netns: protect netns ID lookups with RCU Date: Mon, 13 Sep 2021 15:14:03 +0200 Message-Id: <20210913131050.045276595@linuxfoundation.org> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20210913131047.974309396@linuxfoundation.org> References: <20210913131047.974309396@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Guillaume Nault commit 2dce224f469f060b9998a5a869151ef83c08ce77 upstream. __peernet2id() can be protected by RCU as it only calls idr_for_each(), which is RCU-safe, and never modifies the nsid table. rtnl_net_dumpid() can also do lockless lookups. It does two nested idr_for_each() calls on nsid tables (one direct call and one indirect call because of rtnl_net_dumpid_one() calling __peernet2id()). The netnsid tables are never updated. Therefore it is safe to not take the nsid_lock and run within an RCU-critical section instead. Signed-off-by: Guillaume Nault Signed-off-by: David S. Miller Signed-off-by: Greg Kroah-Hartman Signed-off-by: HÃ¥kon Bugge --- net/core/net_namespace.c | 28 ++++++++++------------------ 1 file changed, 10 insertions(+), 18 deletions(-) --- a/net/core/net_namespace.c +++ b/net/core/net_namespace.c @@ -211,9 +211,9 @@ static int net_eq_idr(int id, void *net, return 0; } -/* Should be called with nsid_lock held. If a new id is assigned, the bool alloc - * is set to true, thus the caller knows that the new id must be notified via - * rtnl. +/* Must be called from RCU-critical section or with nsid_lock held. If + * a new id is assigned, the bool alloc is set to true, thus the + * caller knows that the new id must be notified via rtnl. */ static int __peernet2id_alloc(struct net *net, struct net *peer, bool *alloc) { @@ -237,7 +237,7 @@ static int __peernet2id_alloc(struct net return NETNSA_NSID_NOT_ASSIGNED; } -/* should be called with nsid_lock held */ +/* Must be called from RCU-critical section or with nsid_lock held */ static int __peernet2id(struct net *net, struct net *peer) { bool no = false; @@ -281,9 +281,10 @@ int peernet2id(struct net *net, struct n { int id; - spin_lock_bh(&net->nsid_lock); + rcu_read_lock(); id = __peernet2id(net, peer); - spin_unlock_bh(&net->nsid_lock); + rcu_read_unlock(); + return id; } EXPORT_SYMBOL(peernet2id); @@ -962,6 +963,7 @@ struct rtnl_net_dump_cb { int s_idx; }; +/* Runs in RCU-critical section. */ static int rtnl_net_dumpid_one(int id, void *peer, void *data) { struct rtnl_net_dump_cb *net_cb = (struct rtnl_net_dump_cb *)data; @@ -1046,19 +1048,9 @@ static int rtnl_net_dumpid(struct sk_buf goto end; } - spin_lock_bh(&net_cb.tgt_net->nsid_lock); - if (net_cb.fillargs.add_ref && - !net_eq(net_cb.ref_net, net_cb.tgt_net) && - !spin_trylock_bh(&net_cb.ref_net->nsid_lock)) { - spin_unlock_bh(&net_cb.tgt_net->nsid_lock); - err = -EAGAIN; - goto end; - } + rcu_read_lock(); idr_for_each(&net_cb.tgt_net->netns_ids, rtnl_net_dumpid_one, &net_cb); - if (net_cb.fillargs.add_ref && - !net_eq(net_cb.ref_net, net_cb.tgt_net)) - spin_unlock_bh(&net_cb.ref_net->nsid_lock); - spin_unlock_bh(&net_cb.tgt_net->nsid_lock); + rcu_read_unlock(); cb->args[0] = net_cb.idx; end: