Received: by 2002:a05:6a10:d5a5:0:0:0:0 with SMTP id gn37csp1085518pxb; Wed, 6 Oct 2021 23:57:26 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxp/14zTc2vzOFDSpiU8icEmjl4j2taWWCBXJGGX7Mkhm+WeIYx1gUCPP+RgKRa9qaPXk0Y X-Received: by 2002:a50:ccc4:: with SMTP id b4mr4003148edj.83.1633589845807; Wed, 06 Oct 2021 23:57:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1633589845; cv=none; d=google.com; s=arc-20160816; b=T5iodnDOXcooTj1CW/f1Xfh+bgg1pqBbQowaHXjxMjeytsjv0I0bQ7oFZV+CCZdva7 4FRNIl4fZFuO4iYNnfZ1KMaKRpp4txxKR9oKvd+0PG+Kqo5M6abqIYznXowXTvuBTu8y RvVg3aS/SqWM9i3a+9JNdTetawqr8SYKEs9yZ9QrwJ+Nk5bgSJ2V0WvVDo472z/8Ng8T 37GVaOnrmJx1bVyt3lMF+2yHEUHZowKICjMViMrUamrpgIApYowmUVPlDnq5tqFz2xGx 6cB8gDFrsL/D6VoOs+y6JuRwZa4KjH8/VCPJTX39F0S281LrnudZpjx94s9MH/BtiEpg bzeA== 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=KLdiZdvX9Wpt6i/lfKY3Po8YdL6RC5eGoUV438OTryc=; b=P05EcTacyJ2HwuBw/w7PAvePSuXZ4aOCxLhOelcA5C1JkSXdDglsl2wNeF9LLb0N7s /A3fCSbhudY4nKjSCeCmmXboTUSKcidtnleFe578CMDTgDzCRfxjYVUwKdLptZaNy/BA XSG1Q6begiiNCaVcAXAk8B9dwENYm9XmGhw2yO2oa7chElGyDAGKXnrMrfkSZVVVU3VD hdieIBDA9jXVHi8UiNJtftG47ZabGjV9JDhCeROcgQ2iCdbx8NU6p4SqO6G95Lj7+9/K 0M+/Uz6/5ZGdKfrGf/yWfUfC0jBF3WxRmSA/SdKiPAnG0dUw5hQw1O7VEOXWy5e3tIf2 TGYg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=S+qO3gFa; 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 h24si23986527ejt.376.2021.10.06.23.57.02; Wed, 06 Oct 2021 23:57:25 -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=S+qO3gFa; 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 S240297AbhJGG5X (ORCPT + 99 others); Thu, 7 Oct 2021 02:57:23 -0400 Received: from mail.kernel.org ([198.145.29.99]:35568 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240233AbhJGG5W (ORCPT ); Thu, 7 Oct 2021 02:57:22 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 521E661261; Thu, 7 Oct 2021 06:55:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1633589728; bh=ubBfXkJlfWk9vB5diL1cDnPrfyxhDRFVopndxkSNLHo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=S+qO3gFaIC2YiK+qF0Li+9ZLcXO5JK2oRoxGvU8Dp6vd3MYz864zj/jNvHnC0TBQc 6D5nX//OJnLvnlcSDarDsges/HgnNZXZ0ZrslsgmjsqORpacvBv67daSVDJtOJGe6V rDtV7GaTYRRQ3noWqpGjQqer4T7cMPpN1xAU7tnlkTw9+pM0xl+0b9LK6/B2InB/dD uCCwf2QaDp+QOiJBM60xWgIvOVvEOYCEfFSZdu14j4xRzowQppuMitMTdsqDI0Qq1p jR+FckUd5bkOQ0/FY17jNa2AkzOmcEriGCm+snkSVncgmrF2fGy2Uyv4a0g7vBuvp+ wx8ZST14rvh0g== 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 v3 2/5] devlink: Annotate devlink API calls Date: Thu, 7 Oct 2021 09:55:16 +0300 Message-Id: <19f798d89eae110593055db5e15eb3bbf07adb2c.1633589385.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 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 9642429cec65..4e484afeadea 100644 --- a/net/core/devlink.c +++ b/net/core/devlink.c @@ -154,6 +154,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. @@ -9115,6 +9131,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); @@ -9129,6 +9149,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); @@ -9183,6 +9207,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)); -- 2.31.1