Received: by 2002:a05:6520:4d:b0:139:a872:a4c9 with SMTP id i13csp2568426lkm; Mon, 20 Sep 2021 18:56:54 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwPE5FRCVikpF2ixNW3uML9vEi5y1BWrHCqr9mG1PydWCF4ZKMAcvQhjwkcpyQ1Ht3OReds X-Received: by 2002:a5d:9145:: with SMTP id y5mr20699981ioq.200.1632188926959; Mon, 20 Sep 2021 18:48:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1632188926; cv=none; d=google.com; s=arc-20160816; b=x+wiXI/IYiKt8p6UrWkA5eWYaJS9HdOngWywWTe16TN1eu3Gv6e8gK2qX0QaOg9Iq/ abZ4k724x1t2/EUG8Pbo46lBtrE4vc6rO8Tsv1GWipNfqnQ8+/JVH9PRY/obrpti+pHy mum723bPgTI2+o67+Z2nQrFygjLaNMODm35wPE8N0BMMNPugIa7ZWm4Zof6wGeZ1rlnU NZdtU2qfn79Dy1A+yc1SlmLIPbPc+bZANtQeOkG38fVtv08RKKZyWUeDAZjjqqsPaaeV H9CwO458nlIeoKeNcUH8sbsKoGfDWiVqGvPVqfjuHdZQrNwzOGUVdcqBfSt1kfqP4Smq jGjQ== 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=qgpQbtbZtGJcMVDzrtWzG0ecOESLVbYB3HWENrzsXmM=; b=YkjwbS/T2FoelTLFfNn42u6L5L3SfHzeFMRkn1bcf0KLAs5ki0o4QHjOMQ6Wb8CRmp qC+RHlvezzkZkcLvLLNnCblVMaKQUsfAMyDBjP18010CGuEcBUx4sbKQSH7MTo/rp9Vm fhyMlzqTEMcnJGa1UROKBl8IJ688RlcN9q8TmGuLxQHe2eV3UOfrEpfGgPsSTmVtw6+D 0LbjCvr3xOXMxx82b6w4jCryFQ1olF3i2hgi1HqoJQaeSzk/BR0lZhrzugbG6zYHAqhw VYoyq53Oc169f1bsuylhi6z/M3NTCExIGh3k+vfjdY/ZbTWRA1zUb1r96yA44M/z1Iai Ukxw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=Xo8dXNKe; 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 a14si3239063ilc.22.2021.09.20.18.48.36; Mon, 20 Sep 2021 18:48:46 -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=Xo8dXNKe; 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 S1349423AbhITRqv (ORCPT + 99 others); Mon, 20 Sep 2021 13:46:51 -0400 Received: from mail.kernel.org ([198.145.29.99]:48048 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1353006AbhITRnS (ORCPT ); Mon, 20 Sep 2021 13:43:18 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id CFBB161B72; Mon, 20 Sep 2021 17:09:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1632157746; bh=GVDe6dytpR8MZgWC71Q+k/v2IWhxvAb5Tj1GIdvifOg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Xo8dXNKe6zSASi5faISY/y2QrjScBUsXCzqvDLB9dibMkOkRC5gaI1uMkq60bXkjm bEvqAOat91QKuHc98yd4IGt3dBysDvgdsKQXqWpr1uNhrKDuLZgf7Q0MNq4G5fstG2 CuiASYIFwsrZ8VdV0SsfHfusRU78EHvG7X6XaC3U= 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 4.19 105/293] netns: protect netns ID lookups with RCU Date: Mon, 20 Sep 2021 18:41:07 +0200 Message-Id: <20210920163936.858871708@linuxfoundation.org> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20210920163933.258815435@linuxfoundation.org> References: <20210920163933.258815435@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 | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) --- a/net/core/net_namespace.c +++ b/net/core/net_namespace.c @@ -192,9 +192,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) { @@ -218,7 +218,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; @@ -261,9 +261,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); @@ -837,6 +838,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; @@ -867,9 +869,9 @@ static int rtnl_net_dumpid(struct sk_buf .s_idx = cb->args[0], }; - spin_lock_bh(&net->nsid_lock); + rcu_read_lock(); idr_for_each(&net->netns_ids, rtnl_net_dumpid_one, &net_cb); - spin_unlock_bh(&net->nsid_lock); + rcu_read_unlock(); cb->args[0] = net_cb.idx; return skb->len;