Received: by 2002:a05:6a10:1287:0:0:0:0 with SMTP id d7csp5912745pxv; Thu, 29 Jul 2021 01:19:59 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyPmKrR4MJD/AyX+SMK2zFYU6qBvs/o1PxPEWPrxfO69kfiMw6zTPZ/Ug9PbLY1ZEeyjpoZ X-Received: by 2002:a05:6402:1b06:: with SMTP id by6mr4576115edb.95.1627546799228; Thu, 29 Jul 2021 01:19:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1627546799; cv=none; d=google.com; s=arc-20160816; b=BQqxbS03RV+jYuHe6ERj2Q4S7EyfORZixkf1BER1g7HIw+uCqo+tQo8cXpOzv+CvZM +L7qHzr6YbZZUDVMex49is9t2nV7fQVg275pQ3T1jV6/b11f6aAuQbQVJn83d/MlTxH/ w0PGzsnNftB5VFTybou2IjUVG9ma7zUMPnMgxnUetc6KSAGgzXPNqIcj9rXUuxhR5dph grEFr92Q/VSEWPSRM6/QDw8lLPf39zeFAVfp0/mZatKltlgPOhhQ9onT6MvduSbkbyXl Ii+Gu/LvTRjqkmHY9iO/XzSsHqcBAYHksDf/Spd1V6x9xBCo96bbF6lRuKiAj17uRwj0 6iBA== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=vmrJCAcSm5MZs6uEWQVPTFHgoCJ52DxZExhZWyMQjTk=; b=A9z4sHI9SqvfHOIKK3R1muQko2TfKfVyahWvyIGvNctsgDPkLklw3vTnOe2gb9zL8f iu12KuD8yeOzRgwzfZbgbHILqr3ePlxdu9H4b+cIDzr4GwqRs3VSDj99gjs6wJG92/Yd bRaggso4D/rlWj+hpe6Hm/jdxy9E8J1Y8vt4L9UqU1C9gZYxh8i6/FMseavnXi+Z/15j q1CtUZ0rGS8ci+6D/+TCLZPtQV5053ITiOc0zjGp/fsAXT55Udh2NGxeQsnXN94yoUMm orT1X84DsGY6mWjucsfZNcadqX0dBghR8DlWG4YG8vqpoAb7IQsRN0ZlhVuu/6Kd8Fos 243w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=iwa2im8E; 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=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id k11si2067819ejg.307.2021.07.29.01.19.36; Thu, 29 Jul 2021 01:19:59 -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=@kernel.org header.s=k20201202 header.b=iwa2im8E; 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=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234995AbhG2IPm (ORCPT + 99 others); Thu, 29 Jul 2021 04:15:42 -0400 Received: from mail.kernel.org ([198.145.29.99]:57002 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234917AbhG2IPi (ORCPT ); Thu, 29 Jul 2021 04:15:38 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id C87D260EBB; Thu, 29 Jul 2021 08:15:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1627546535; bh=KyjLRDfscOh8u1K820uzrlgW4POL7rThGF5tlhgKuH4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=iwa2im8EISOGCfmNsdltwRUWe2+ACsNeKYK0HqjAj0Jdn+ur4uwBsVaAjnXDSnxAs AITRGDnQfIH3WHiA1vtge3fxTN6iR/rnvb5YoLMjrFwISWjkeu5RyY+DZXbEwe9jYg qra/Jgp6zFMhLHZUYV7u4wtx+hxyeLfO6/eUAk2mHE3fo0S0INZ+tBYdlfqoIIOuz5 /nJMAI9ozZ2bn2qH/F75nLpbfktLtNIqkJy25L5dSoiO6PGMIat9gR0QlMk8uroape R/hMZk9xeAC2oRKeAI8LysW/YsT+bCtaVWoHcPNyRCAJF6MACmKjg5BGsg71SaCNlX SN6mdfTQwdfvw== From: Leon Romanovsky To: "David S. Miller" , Jakub Kicinski , Jiri Pirko Cc: Leon Romanovsky , linux-kernel@vger.kernel.org, netdev@vger.kernel.org, Parav Pandit Subject: [PATCH net-next 2/2] devlink: Allocate devlink directly in requested net namespace Date: Thu, 29 Jul 2021 11:15:26 +0300 Message-Id: X-Mailer: git-send-email 2.31.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Leon Romanovsky There is no need in extra call indirection and check from impossible flow where someone tries to set namespace without prior call to devlink_alloc(). Instead of this extra logic and additional EXPORT_SYMBOL, use specialized devlink allocation function that receives net namespace as an argument. Such specialized API allows clear view when devlink initialized in wrong net namespace and/or kernel users don't try to change devlink namespace under the hood. Signed-off-by: Leon Romanovsky --- drivers/net/netdevsim/dev.c | 4 ++-- include/net/devlink.h | 14 ++++++++++++-- net/core/devlink.c | 26 ++++++++------------------ 3 files changed, 22 insertions(+), 22 deletions(-) diff --git a/drivers/net/netdevsim/dev.c b/drivers/net/netdevsim/dev.c index 6348307bfa84..d538a39d4225 100644 --- a/drivers/net/netdevsim/dev.c +++ b/drivers/net/netdevsim/dev.c @@ -1431,10 +1431,10 @@ int nsim_dev_probe(struct nsim_bus_dev *nsim_bus_dev) struct devlink *devlink; int err; - devlink = devlink_alloc(&nsim_dev_devlink_ops, sizeof(*nsim_dev)); + devlink = devlink_alloc_ns(&nsim_dev_devlink_ops, sizeof(*nsim_dev), + nsim_bus_dev->initial_net); if (!devlink) return -ENOMEM; - devlink_net_set(devlink, nsim_bus_dev->initial_net); nsim_dev = devlink_priv(devlink); nsim_dev->nsim_bus_dev = nsim_bus_dev; nsim_dev->switch_id.id_len = sizeof(nsim_dev->switch_id.id); diff --git a/include/net/devlink.h b/include/net/devlink.h index e48a62320407..b4691c40320f 100644 --- a/include/net/devlink.h +++ b/include/net/devlink.h @@ -1540,8 +1540,18 @@ static inline struct devlink *netdev_to_devlink(struct net_device *dev) struct ib_device; struct net *devlink_net(const struct devlink *devlink); -void devlink_net_set(struct devlink *devlink, struct net *net); -struct devlink *devlink_alloc(const struct devlink_ops *ops, size_t priv_size); +/* This RAW call is intended for software devices that can + * create devlink instance in other namespaces than init_net. + * + * Drivers that operate on real HW must use devlink_alloc() instead. + */ +struct devlink *devlink_alloc_ns(const struct devlink_ops *ops, + size_t priv_size, struct net *net); +static inline struct devlink *devlink_alloc(const struct devlink_ops *ops, + size_t priv_size) +{ + return devlink_alloc_ns(ops, priv_size, &init_net); +} int devlink_register(struct devlink *devlink, struct device *dev); void devlink_unregister(struct devlink *devlink); void devlink_reload_enable(struct devlink *devlink); diff --git a/net/core/devlink.c b/net/core/devlink.c index 54e2a0375539..b419b7a6ec40 100644 --- a/net/core/devlink.c +++ b/net/core/devlink.c @@ -108,19 +108,6 @@ struct net *devlink_net(const struct devlink *devlink) } EXPORT_SYMBOL_GPL(devlink_net); -static void __devlink_net_set(struct devlink *devlink, struct net *net) -{ - write_pnet(&devlink->_net, net); -} - -void devlink_net_set(struct devlink *devlink, struct net *net) -{ - if (WARN_ON(devlink->dev)) - return; - __devlink_net_set(devlink, net); -} -EXPORT_SYMBOL_GPL(devlink_net_set); - static struct devlink *devlink_get_from_attrs(struct net *net, struct nlattr **attrs) { @@ -3920,7 +3907,7 @@ static int devlink_reload(struct devlink *devlink, struct net *dest_net, return err; if (dest_net && !net_eq(dest_net, curr_net)) - __devlink_net_set(devlink, dest_net); + write_pnet(&devlink->_net, dest_net); err = devlink->ops->reload_up(devlink, action, limit, actions_performed, extack); devlink_reload_failed_set(devlink, !!err); @@ -8776,15 +8763,18 @@ static bool devlink_reload_actions_valid(const struct devlink_ops *ops) } /** - * devlink_alloc - Allocate new devlink instance resources + * devlink_alloc_ns - Allocate new devlink instance resources + * in specific namespace * * @ops: ops * @priv_size: size of user private data + * @net: net namespace * * Allocate new devlink instance resources, including devlink index * and name. */ -struct devlink *devlink_alloc(const struct devlink_ops *ops, size_t priv_size) +struct devlink *devlink_alloc_ns(const struct devlink_ops *ops, + size_t priv_size, struct net *net) { struct devlink *devlink; @@ -8799,7 +8789,7 @@ struct devlink *devlink_alloc(const struct devlink_ops *ops, size_t priv_size) return NULL; devlink->ops = ops; xa_init_flags(&devlink->snapshot_ids, XA_FLAGS_ALLOC); - __devlink_net_set(devlink, &init_net); + write_pnet(&devlink->_net, net); INIT_LIST_HEAD(&devlink->port_list); INIT_LIST_HEAD(&devlink->rate_list); INIT_LIST_HEAD(&devlink->sb_list); @@ -8815,7 +8805,7 @@ struct devlink *devlink_alloc(const struct devlink_ops *ops, size_t priv_size) mutex_init(&devlink->reporters_lock); return devlink; } -EXPORT_SYMBOL_GPL(devlink_alloc); +EXPORT_SYMBOL_GPL(devlink_alloc_ns); /** * devlink_register - Register devlink instance -- 2.31.1