From patchwork Tue Oct 12 13:15:23 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Leon Romanovsky X-Patchwork-Id: 12552501 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 77E8CC433EF for ; Tue, 12 Oct 2021 13:15:59 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 5D0F161050 for ; Tue, 12 Oct 2021 13:15:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236724AbhJLNR4 (ORCPT ); Tue, 12 Oct 2021 09:17:56 -0400 Received: from mail.kernel.org ([198.145.29.99]:45534 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236732AbhJLNRw (ORCPT ); Tue, 12 Oct 2021 09:17:52 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 8960F610E7; Tue, 12 Oct 2021 13:15:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1634044551; bh=Qx5MznPq238UMGCL2ja414plE7+8hqf3yMF4dqZ9S1A=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=WsTgSd7MeDb06DbjJqPfEITyj4M6nrJE2xXojOpDOCfgegEpbRY7jXGtDzbQ1v4Oz FrLU84pEhS/jLDpfpqxTEAQXO7+ZwOtJzcmnReltTUsOvWQRLZBbBbbkPLtJ6I/S/Q LKxomWedVvIdd1At2XCGaKz+yDr+/F9AMdS2B7EiTvHg06iEKNTCXC8f4ZojI1yM/C J6bE4NFjBaIhGZh7zt44Ma746pOI17KKhKNY9awkhLlO1RUfMqu6PPk9dA4bdd4nxd NAfMpa6OuIFs29e0G/AgRlpH3lz9j96RODrAmUl/VlUz/47PvmmxLHC9iHaWNUn2Jp +cBnHbLqWrrbQ== From: Leon Romanovsky To: "David S . Miller" , Jakub Kicinski Cc: Leon Romanovsky , Ido Schimmel , Ingo Molnar , Jiri Pirko , linux-kernel@vger.kernel.org, linux-rdma@vger.kernel.org, mlxsw@nvidia.com, Moshe Shemesh , netdev@vger.kernel.org, Saeed Mahameed , Salil Mehta , Shay Drory , Steven Rostedt , Tariq Toukan , Yisen Zhuang Subject: [PATCH net-next v4 3/6] devlink: Annotate devlink API calls Date: Tue, 12 Oct 2021 16:15:23 +0300 Message-Id: <039bc02fef0eefc713d3640473dbbd4b7770f103.1634044267.git.leonro@nvidia.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-rdma@vger.kernel.org From: Leon Romanovsky Initial annotation patch to separate calls that needs to be executed before or after devlink_register(). Signed-off-by: Leon Romanovsky --- net/core/devlink.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/net/core/devlink.c b/net/core/devlink.c index e9e908c4cfb4..276e1e421eb4 100644 --- a/net/core/devlink.c +++ b/net/core/devlink.c @@ -152,6 +152,22 @@ static const struct nla_policy devlink_function_nl_policy[DEVLINK_PORT_FUNCTION_ static DEFINE_XARRAY_FLAGS(devlinks, XA_FLAGS_ALLOC); #define DEVLINK_REGISTERED XA_MARK_1 +/* devlink instances are open to the access from the user space after + * devlink_register() call. Such logical barrier allows us to have certain + * expectations related to locking. + * + * Before *_register() - we are in initialization stage and no parallel + * access possible to the devlink instance. All drivers perform that phase + * by implicitly holding device_lock. + * + * After *_register() - users and driver can access devlink instance at + * the same time. + */ +#define ASSERT_DEVLINK_REGISTERED(d) \ + WARN_ON_ONCE(!xa_get_mark(&devlinks, (d)->index, DEVLINK_REGISTERED)) +#define ASSERT_DEVLINK_NOT_REGISTERED(d) \ + WARN_ON_ONCE(xa_get_mark(&devlinks, (d)->index, DEVLINK_REGISTERED)) + /* devlink_mutex * * An overall lock guarding every operation coming from userspace. @@ -9113,6 +9129,10 @@ static void devlink_notify_unregister(struct devlink *devlink) */ void devlink_register(struct devlink *devlink) { + ASSERT_DEVLINK_NOT_REGISTERED(devlink); + /* Make sure that we are in .probe() routine */ + device_lock_assert(devlink->dev); + mutex_lock(&devlink_mutex); xa_set_mark(&devlinks, devlink->index, DEVLINK_REGISTERED); devlink_notify_register(devlink); @@ -9127,6 +9147,10 @@ EXPORT_SYMBOL_GPL(devlink_register); */ void devlink_unregister(struct devlink *devlink) { + ASSERT_DEVLINK_REGISTERED(devlink); + /* Make sure that we are in .remove() routine */ + device_lock_assert(devlink->dev); + devlink_put(devlink); wait_for_completion(&devlink->comp); @@ -9181,6 +9205,8 @@ EXPORT_SYMBOL_GPL(devlink_reload_disable); */ void devlink_free(struct devlink *devlink) { + ASSERT_DEVLINK_NOT_REGISTERED(devlink); + mutex_destroy(&devlink->reporters_lock); mutex_destroy(&devlink->lock); WARN_ON(!list_empty(&devlink->trap_policer_list));