Received: by 2002:a05:6a11:4021:0:0:0:0 with SMTP id ky33csp552353pxb; Wed, 29 Sep 2021 05:03:59 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzDiqkypg8KpcaGVHyfAMfIdJqU01ABW5e3oB3FG6+TxrPAYf5Jf6NUlmwFdlqBu+EwFk9W X-Received: by 2002:aa7:968e:0:b0:447:a593:926e with SMTP id f14-20020aa7968e000000b00447a593926emr10822791pfk.27.1632917039182; Wed, 29 Sep 2021 05:03:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1632917039; cv=none; d=google.com; s=arc-20160816; b=fBI4MKDol/zMt1fU3WfFz9iQtJ+vps+rGLue/Ajz2ymymm7jdoZAibQbbFBOTw7XXp DntgU+W7Cn0XIlfP64rYLCScIiJNw2LekAX/aCRDyVC8+rOtrbcNFE60+L7phvHdBwFq NrI1+9w5j6of1K901GfGA/WS9uTJhWTO+S91cxUovR8aLrATdySvYQnZzb5IT6+N8mYN byo5WlghG4AfE/38H/l5Dzoqy6zaj4G7oH8ZnwXoKBfWSl5bEOUhATYqkcXOfAVGM4m1 ztVwVSemsbMp3wNmKe3NmOCo375+NmLmHzsExbRSY9txW+1ioT7n/E/VUPyNTTIvTBqP Ai3A== 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=H6Y1dQ3FbOhvVuWwyKWcU5/SxcrmFM9O6gX36xeRwjc=; b=FygFSOpqKzXSbfhAW2s2EVeUHimS6WP8xrDdXu3JJ+qWnQY+tgcSYMBVhHnOlniuF6 fCh3PG7NEZCJPpbrlZnd57zCiYm0O7ZfQ7UqBk/PFy3b8h11ASydVa51MtnQzVVC14EK VRrUAlsyq5lBBjR717YZNPUp8KBFWLMxgNcPOjYT5LDNYVhB9sQs0aap0+PKnkmfNTih EDw1xZuD99M+PCcEX0QisRXZcwhgD/YbV2soeRJBGxHokJMEgqk46Zo7cqgnkp6rsMQA /VDZUbCLyowA5cSyzbFlsKcdeCmDMa0iTCMBH4zJWgtruP18gS39CTfj8Kq7ODLM7uzG V3tA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="rs/XvNYx"; 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 h9si2890037pfq.353.2021.09.29.05.03.46; Wed, 29 Sep 2021 05:03: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="rs/XvNYx"; 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 S1343614AbhI2MCl (ORCPT + 99 others); Wed, 29 Sep 2021 08:02:41 -0400 Received: from mail.kernel.org ([198.145.29.99]:52524 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1343628AbhI2MCj (ORCPT ); Wed, 29 Sep 2021 08:02:39 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 5F75561414; Wed, 29 Sep 2021 12:00:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1632916858; bh=sWOMc53F6fyjiBJQ3ELzEBwFAXedaQQqVi7cxDeJKLU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=rs/XvNYxmfzCfL9jh91wFmivkm84vokPpMn/jA+yIEPJXh+T0IRl7w5dJyY6tHPX8 Spof3xyYVDwokJtnoqfRfDVC35T8dDtJlCZ0CcXK3XoIcFVUDR1CYuUzaUJAjkecru 15O0f1m9pRiLpiZuMaU7nSSoSd5Yh8UzS4Ioy4/kIpTuxaa/aZEl92wcdYJWHlDwsS 8h2TCijNo0rJfapU0b+HI+hfEji8mA7wK+HwJqQLgiG9q71uRANLbC4nY+RqjENwFL OR1ZxPGIB4JYvN2pGlck1ysSDYcDwQh4wMkYJnGejtJlttb3ipc/7uPP+ppTXYrHJE 1axdAYORcnFLw== 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 v1 3/5] devlink: Allow set specific ops callbacks dynamically Date: Wed, 29 Sep 2021 15:00:44 +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 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 9ae38128d6e1..67a846d424b7 100644 --- a/net/core/devlink.c +++ b/net/core/devlink.c @@ -8906,6 +8906,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, op, name) \ + do { \ + if ((op)->name) \ + if (!((ptr)->name)) \ + (ptr)->name = (op)->name; \ + } while (0) + + /* Keep sorted */ + SET_DEVICE_OP(dev_ops, ops, reload_actions); + SET_DEVICE_OP(dev_ops, ops, reload_down); + SET_DEVICE_OP(dev_ops, ops, reload_limits); + SET_DEVICE_OP(dev_ops, ops, reload_up); + +#undef SET_DEVICE_OP +} +EXPORT_SYMBOL_GPL(devlink_set_ops); + /** * devlink_alloc_ns - Allocate new devlink instance resources * in specific namespace @@ -8926,8 +8963,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) @@ -8942,6 +8977,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