From patchwork Tue Feb 6 12:13:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Fabio M. De Francesco" X-Patchwork-Id: 197445 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:168b:b0:106:860b:bbdd with SMTP id ma11csp1533968dyb; Tue, 6 Feb 2024 05:24:56 -0800 (PST) X-Google-Smtp-Source: AGHT+IGtmkKKMdryJq3t8fDOoT9GAwEpfwVH5I0WbrefPe+9F/RVOSLYLYF2roxHvZdQNhXbVT8w X-Received: by 2002:a17:902:6803:b0:1d9:167b:8e6c with SMTP id h3-20020a170902680300b001d9167b8e6cmr1427509plk.46.1707225896433; Tue, 06 Feb 2024 05:24:56 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707225896; cv=pass; d=google.com; s=arc-20160816; b=qkIw3yjgu6+qPvtFzolsZC2MA3djsyJ42m7pqz3Kbd54ugp3nXw4p/4jM5QTSPzDN7 Z1ne3ZZnJvHzyEJuRV3Z/InPxH6mg7J82FES1lFXtgHM2gpDhad+UPtdaZMAG0nWvJ3B YwizQ77XstYnrenw6Hi/GdjjboiVVtiBH5KlCsp2seN0BSe+rFYStrwIcD7aE785HI5F F11ybJxwnvDXY+SOmYRpfFifB1b4c7kj/otrTkM/yII5AlfzLBiKPPqUvBSSkfGVu61D OZuYXJrdzlEiDYIAUD25IauGK22j8NGfFETt5fKzkq/Q8ViSJgL7o1eMX/+LsYQ3vhLP ZeHQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=RxuK4xw8pqh5yDcOGZC1UBLtUHIGb++cQtibGifwwYo=; fh=nhXwMRznLESpjwZnG8MoMyXpwITXjMxEr9hSbh7bjqM=; b=VWjBVsxzwNkfr7VmRLpQqbr1frW5RGbFETOQw7ES35qYWDrWRtLsRUZ5upfYYOKY0Y i3OQiWs7VFgo8d1N06NiYfMmNQP1wKbf5EB+mStpvStehBXJGZ/Qb0SXqTmi65VeWrbQ s1+tl0DkYdb2fhxR+eUQQViOUaUne/fuYbxkdxCOOVyR/dCjTzvRXg0E+B+lhaYcn2t+ ydWTQAXZOEcrxdrcOGyLIoanfcKvLoH/a/v9bHHsE0UHsIKrSWtFJDQr8dYmxKiELBtm 2mFUeY0mjPiWuLPUujsYFmu8PzrP+KMbIg9hHJW3EGIMGyOGxUCx01Bo4L/sP97cO9JI zYPw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=DJcRQtsp; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-54935-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-54935-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com X-Forwarded-Encrypted: i=1; AJvYcCWL40Cycj5JvKqOJVJj3Z5ngn00VKhVodjC/Q54hso3D7+WWzbVggDb+9v3bH02W5UUqMGGKjgEai+gEasDh8fAryGCOQ== Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id h2-20020a170902f7c200b001d51f6f28cesi1613706plw.142.2024.02.06.05.24.56 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Feb 2024 05:24:56 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-54935-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=DJcRQtsp; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-54935-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-54935-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id C0054286F49 for ; Tue, 6 Feb 2024 12:30:07 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C8D8E12FF9E; Tue, 6 Feb 2024 12:13:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="DJcRQtsp" Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.16]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B87781EA7A; Tue, 6 Feb 2024 12:13:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.16 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707221597; cv=none; b=qUyq1sCu/rfVhtfC0g40JKhxGoi+8tCeVQv/6LJrM+gfkXg9WujhUMYWeMO6f2xU7KoVwmFsoRg8mQDf5y6hU3j7koLZ4vFwYk0mHrG6Zt4Th46PD41RlrTxEKcBRRcMr4o5Lkt3GuLdO59ZN/4WUw9hgxSXQq3VGNOGeEqNGMs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707221597; c=relaxed/simple; bh=Wim/IsJK6tra2KU9v5LTN8HrI8504obd37/zUH9YMYQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Zp6Dia5sEQmi2QVEmDDVpYwSRNmsDDUC049G/6m8KfDV+wJ5hOqkUzJ6qamKgTuPnE9BJaK+xqSyRbC1p5+p6JzPh171A2AbrsZ9Ctg9Dm27BFux25PLOhCY1rHIqkImhTRfyihCQOfrNY9R5PKwSPntfSnixL5CbMUZz6YX0/w= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=DJcRQtsp; arc=none smtp.client-ip=192.198.163.16 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1707221594; x=1738757594; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Wim/IsJK6tra2KU9v5LTN8HrI8504obd37/zUH9YMYQ=; b=DJcRQtsp2VAhMnHEIJasEz7HbNTjQw7ejyL4S/48wsDW4T+WnexJ+RlE J+EBHHlLsur1d9H5jEb1+A0cU+3h8S/yJfcQLMd4MQ3tqUXtU2l/oatbX xYS1p4U+gSg/V+CFrNCj4JlQWfSzN7br67wclXhfXujPtMFus1HkLjWob Gmho7vb72Zdfk6nu3rFCIpV2q5aHF7TyqGdk9D5ovu2D6UJylZ9JkM7G1 AAmnCs/j+4EnSmib6htJ8HP5Wb1xkKkE1myOKCdoySQLLpX+W8tWikbMC NUPpE40X4J/0+N7KUmIYiHqjBSppwTohmA8SEv4Ji1X0E4vrc4SLk+2w8 g==; X-IronPort-AV: E=McAfee;i="6600,9927,10975"; a="998426" X-IronPort-AV: E=Sophos;i="6.05,247,1701158400"; d="scan'208";a="998426" Received: from fmviesa002.fm.intel.com ([10.60.135.142]) by fmvoesa110.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 06 Feb 2024 04:13:14 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.05,247,1701158400"; d="scan'208";a="24247467" Received: from fdefranc-mobl3.ger.corp.intel.com (HELO fdefranc-mobl3.Hitronhub.home) ([10.213.2.175]) by fmviesa002-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 06 Feb 2024 04:13:10 -0800 From: "Fabio M. De Francesco" To: Peter Zijlstra , Dan Williams , linux-kernel@vger.kernel.org Cc: linux-cxl@vger.kernel.org, Dave Jiang , Jonathan Cameron , Ingo Molnar , "Fabio M. De Francesco" , Ira Weiny Subject: [PATCH 1/2 v3] cleanup: Add cond_guard() to conditional guards Date: Tue, 6 Feb 2024 13:13:00 +0100 Message-ID: <20240206121301.7225-2-fabio.maria.de.francesco@linux.intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240206121301.7225-1-fabio.maria.de.francesco@linux.intel.com> References: <20240206121301.7225-1-fabio.maria.de.francesco@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790156101157032937 X-GMAIL-MSGID: 1790156101157032937 Add cond_guard() macro to conditional guards. cond_guard() is a guard to be used with the conditional variants of locks, like down_read_trylock() or mutex_lock_interruptible(). It takes a statement (or statement-expression) that is passed as its second argument. That statement (or statement-expression) is executed if waiting for a lock is interrupted or if a _trylock() fails in case of contention. Usage example: cond_guard(mutex_intr, return -EINTR, &mutex); Consistent with other usage of _guard(), locks are unlocked at the exit of the scope where cond_guard() is called. Cc: Dave Jiang Cc: Jonathan Cameron Cc: Peter Zijlstra Suggested-by: Dan Williams Suggested-by: Ira Weiny Signed-off-by: Fabio M. De Francesco Reviewed-by: Ira Weiny Reviewed-by: Dave Jiang --- include/linux/cleanup.h | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/include/linux/cleanup.h b/include/linux/cleanup.h index c2d09bc4f976..d70454e9f8dc 100644 --- a/include/linux/cleanup.h +++ b/include/linux/cleanup.h @@ -134,6 +134,16 @@ static inline class_##_name##_t class_##_name##ext##_constructor(_init_args) \ * an anonymous instance of the (guard) class, not recommended for * conditional locks. * + * cond_guard(name, fail, args...): + * a guard to be used with the conditional variants of locks, like + * down_read_trylock() or mutex_lock_interruptible. 'fail' is a + * statement or statement-expression that is executed if waiting for a + * lock is interrupted or if a _trylock() fails in case of contention. + * + * Example: + * + * cond_guard(mutex_intr, return -EINTR, &mutex); + * * scoped_guard (name, args...) { }: * similar to CLASS(name, scope)(args), except the variable (with the * explicit name 'scope') is declard in a for-loop such that its scope is @@ -165,6 +175,11 @@ static inline class_##_name##_t class_##_name##ext##_constructor(_init_args) \ #define __guard_ptr(_name) class_##_name##_lock_ptr +#define cond_guard(_name, _fail, args...) \ + CLASS(_name, scope)(args); \ + if (!__guard_ptr(_name)(&scope)) _fail; \ + else + #define scoped_guard(_name, args...) \ for (CLASS(_name, scope)(args), \ *done = NULL; __guard_ptr(_name)(&scope) && !done; done = (void *)1)