Received: by 2002:a05:6a11:4021:0:0:0:0 with SMTP id ky33csp470941pxb; Wed, 29 Sep 2021 03:20:00 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwFVEl7h+DVJXUgMRkr+p15Qsn8msJ+J4avFXmKoapdXDQvGk+Wz+tGotTp0ycU4p4yLBkk X-Received: by 2002:a50:8264:: with SMTP id 91mr13515508edf.333.1632910800578; Wed, 29 Sep 2021 03:20:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1632910800; cv=none; d=google.com; s=arc-20160816; b=VVBN1V4U3DqJPPBZCNviJXZ19NUSI/l+ONBY7k3Xl3TyeChtKSgHHc0MzSBueAryy3 KFOsSx/zjUwLCAGT5KvyRl9qV7Ur1Bk7cuwxqCrLRLPSKOe52JmI69mew2GMmyVkSFgt Yai57EJNsvTcUQR8zoRPJSw1okU/BqLuRaHrXdNlOjufow1EJmNaWMSNHNbfP2KhxRIW +NAh7R/R2DPQ9Aq7ucjWzjJrrCcl1U0VCsHw2tGj6pytrXgSgJvyOCtq/a4a0oFNQmmL 6AJZ2OLZmlMbhpfUr3tnc7vwR0kRR6/ynnSgGI4aIOUFKQpPzXxfPqxeseCCBaUx68rL a0Ew== 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=dSo+RjfsDM0+g/YZbhgVoiyZCUVAmfTI+yQkZiBP5SY=; b=edCSR1EDWdzvAmpxqvfaHOpraXSSxOHEWMo2dxQ6Dda7YHkszeKsWmg5gh1/XUlooW rDAQwov8r/OmyVqNeXRfWqmGKPd3huyg9eTnvIx6yMuApz+6SGfczbBpYMAGDpARAssu jg2XhvSa0/MkrvU9TFl2XSCvQr85kQC7RE5x1051Yy20qN7w4IAQB8yT5XZA0vFqGIso Bg2EPt/w8W7CLmPc/SalzvZqASO5hXDPYIWXJp6zPf/JFniTqUdYnx00hoazSSYi9Hs6 ecTTXPKRAQMY8au1ARqnRMVfcPxuY3/OFqwWccc+SKhETsIyylhp7KXQ0MlcIb7JQYrs shuw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=NSpJhV2+; 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 o8si2642664ejj.94.2021.09.29.03.19.35; Wed, 29 Sep 2021 03:20:00 -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=NSpJhV2+; 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 S245444AbhI2KSf (ORCPT + 99 others); Wed, 29 Sep 2021 06:18:35 -0400 Received: from mail.kernel.org ([198.145.29.99]:49302 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S245418AbhI2KSc (ORCPT ); Wed, 29 Sep 2021 06:18:32 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 646F4613A5; Wed, 29 Sep 2021 10:16:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1632910611; bh=lA8WUo/+NGUBXAShC+sjos7pew5KebdIN8DiTIW2984=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=NSpJhV2+J5T+2HEqPitvtj27o68uSnDt46b9mVGz5BBrgUjLbW1CQccH1ATFbj7OG 7QDWNjlnbCo4AuJbUYK3ZB0kuxPvqWNtszVUVWeSUJFu+xw2Les2D5rJvPQwMskRiw +Boj0k4X2Q8bhnw8VsSIO7wst9oAukH196yHabnu2a+NjpD05gu06Yk9LaWaguDtCq eYvWnp3roLSM8PRR0R6Fn98qOWKeMvjOl8XIaS/zYG7x6DQT3tDEhbjxO21swCG0aq tKQao7GzFXyJfS5U5CZhrPt9frLK3A5G/ID3h/aNFprO+biwuRw6feIloG2x3DCWiY Fai7FFCcDc7ag== From: Leon Romanovsky To: "David S . Miller" , Jakub Kicinski Cc: Leon Romanovsky , Alexandre Belloni , Andrew Lunn , Ariel Elior , Bin Luo , Claudiu Manoil , Coiby Xu , Derek Chickles , drivers@pensando.io, Eric Dumazet , Felix Manlunas , Florian Fainelli , Geetha sowjanya , Greg Kroah-Hartman , GR-everest-linux-l2@marvell.com, GR-Linux-NIC-Dev@marvell.com, hariprasad , Ido Schimmel , intel-wired-lan@lists.osuosl.org, Ioana Ciornei , Jerin Jacob , Jesse Brandeburg , Jiri Pirko , Jonathan Lemon , Linu Cherian , linux-kernel@vger.kernel.org, linux-omap@vger.kernel.org, linux-rdma@vger.kernel.org, linux-staging@lists.linux.dev, Manish Chopra , Michael Chan , Moshe Shemesh , netdev@vger.kernel.org, oss-drivers@corigine.com, Richard Cochran , Saeed Mahameed , Salil Mehta , Satanand Burla , Shannon Nelson , Shay Drory , Simon Horman , Subbaraya Sundeep , Sunil Goutham , Taras Chornyi , Tariq Toukan , Tony Nguyen , UNGLinuxDriver@microchip.com, Vadym Kochan , Vivien Didelot , Vladimir Oltean , Yisen Zhuang Subject: [PATCH net-next 3/5] devlink: Allow set specific ops callbacks dynamically Date: Wed, 29 Sep 2021 13:16:37 +0300 Message-Id: <4e99e3996118ce0e2da5367b8fc2a427095dfffd.1632909221.git.leonro@nvidia.com> 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 Introduce new devlink call to set specific ops callback during device initialization phase after devlink_alloc() is already called. This allows us to set reload_* specific ops based on device property which sometimes is known almost at the end of driver initialization. For the sake of simplicity, this API lacks any type of locking and needs to be called before devlink_register() to make sure that no parallel access to the ops is possible at this stage. Signed-off-by: Leon Romanovsky --- include/net/devlink.h | 1 + net/core/devlink.c | 41 +++++++++++++++++++++++++++++++++++++++-- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/include/net/devlink.h b/include/net/devlink.h index 317b09917c41..305be548ac21 100644 --- a/include/net/devlink.h +++ b/include/net/devlink.h @@ -1565,6 +1565,7 @@ static inline struct devlink *devlink_alloc(struct devlink_ops *ops, { return devlink_alloc_ns(ops, priv_size, &init_net, dev); } +void devlink_set_ops(struct devlink *devlink, struct devlink_ops *ops); void devlink_register(struct devlink *devlink); 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 cee2be47c40e..71d0c5671f43 100644 --- a/net/core/devlink.c +++ b/net/core/devlink.c @@ -8907,6 +8907,43 @@ static bool devlink_reload_actions_valid(struct devlink_ops *ops) return true; } +/** + * devlink_set_ops - Set devlink ops dynamically + * + * @devlink: devlink + * @ops: devlink ops to set + * + * This interface allows us to set ops based on device property + * which is known after devlink_alloc() was already called. For now, + * it is applicable for reload_* assignments only and all other + * callbacks are ignored. + * + * It should be called before devlink_register(), so doesn't have any + * protection from concurent access. + */ +void devlink_set_ops(struct devlink *devlink, struct devlink_ops *ops) +{ + struct devlink_ops *dev_ops = devlink->ops; + + WARN_ON(!devlink_reload_actions_valid(ops)); + +#define SET_DEVICE_OP(ptr, name) \ + do { \ + if (ops->name) \ + if (!((ptr)->name)) \ + (ptr)->name = ops->name; \ + } while (0) + + /* Keep sorted */ + SET_DEVICE_OP(dev_ops, reload_actions); + SET_DEVICE_OP(dev_ops, reload_down); + SET_DEVICE_OP(dev_ops, reload_limits); + SET_DEVICE_OP(dev_ops, reload_up); + +#undef SET_DEVICE_OP +} +EXPORT_SYMBOL_GPL(devlink_set_ops); + /** * devlink_alloc_ns - Allocate new devlink instance resources * in specific namespace @@ -8927,8 +8964,6 @@ struct devlink *devlink_alloc_ns(struct devlink_ops *ops, size_t priv_size, int ret; WARN_ON(!ops || !dev); - if (!devlink_reload_actions_valid(ops)) - return NULL; devlink = kzalloc(sizeof(*devlink) + priv_size, GFP_KERNEL); if (!devlink) @@ -8943,6 +8978,8 @@ struct devlink *devlink_alloc_ns(struct devlink_ops *ops, size_t priv_size, devlink->dev = dev; devlink->ops = ops; + /* To check validity of reload actions */ + devlink_set_ops(devlink, ops); xa_init_flags(&devlink->snapshot_ids, XA_FLAGS_ALLOC); write_pnet(&devlink->_net, net); INIT_LIST_HEAD(&devlink->port_list); -- 2.31.1