Received: by 2002:a05:7412:cfc7:b0:fc:a2b0:25d7 with SMTP id by7csp151794rdb; Sat, 17 Feb 2024 02:59:41 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCX0pwELQYnRHeVa6VzPBaZtCuhM2/t3Yph27WPoF82UWGOckXaWg5jNwu8YC1u8jlHHeSYd/J8w06lzWrqRdHqxvEZnf+Y1YuK6/TDo+g== X-Google-Smtp-Source: AGHT+IHqsoW1yfxuTJu9UFHrJirbgeCSWFHSUn4CvZUoT/SZROPQ5a6Clq1rjKN+awLUeywDBFgo X-Received: by 2002:a05:622a:1b8d:b0:42c:4e93:ac11 with SMTP id bp13-20020a05622a1b8d00b0042c4e93ac11mr8763275qtb.28.1708167581176; Sat, 17 Feb 2024 02:59:41 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708167581; cv=pass; d=google.com; s=arc-20160816; b=NGaJTIO9yKFddDqBf74QfL5ikrTfePZ6VvSd+PmZ+SiiNqnu2sgR6CS7w/oS4C9TM5 NUrO2trqMLU9XesKhrRQNXbO4x0GvMVew1yEILhRMGk61fq7kX1MkBSFl3yXH4MnDvJh PbFwO2lx9JavIUCqIWrCwZ7BLjHPwIzJuWuYqhtLoq7zfP7dCZm5Dwg4/7nQCxarCGDW MxwbgWpOXrts3cgjPwSuQxtAy7MGaFiY+YwcfuFGtx7G9V2yFf7c+6TK5dnBFhLjP3zN Lv9TGQNm0ElXM5Ubdc5uDqUxKT8i1KMvRO9iFU/Wl8W5ulLgXIMDfcPxPrcqoDN8XDMH /7dA== 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=FuVs7w7psiYm3JbOf6KpBwEhtyPW9YkqGGni1RCFiac=; fh=tt9aLUCPtZBDntIGGgm0DfYgLXsZUYzWtYgxP3QUZ1A=; b=gzJylNdIxpJd3mn8+Z5wJhAsLGTjVeLwINVBkT3CB5s6inPcPozzDHhEeD0e1U4SB9 nrpWrLCez/M44l0HSS6s8Cii/Hhr15Rv33mjhVi54wclOVcs0UnQEpOyGz7qImkVHqiS 2r7xoJEUtl8ckJBX41dBOzXesHFWlbzwtiJGHPEsZBaO8gkEABAjKx/k94F9FKSaDIWW eMZgbXHKxnY4pbj3v4aS9mTqz6/fggMfvjMB2V7iukVw+G6U58F7PEaGsLjEIKCStxa1 ce9LaH+3KKsh29JeW5LrN/PL2A+Wr/LCZ90pQPUPgYwSBBQCpTcvTLLepdhaPpWog3Bf 523A==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=H+MpTSB+; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-69815-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-69815-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id o7-20020ac86987000000b0042dfc34b70esi256832qtq.528.2024.02.17.02.59.41 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 17 Feb 2024 02:59:41 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-69815-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=H+MpTSB+; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-69815-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-69815-linux.lists.archive=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 ny.mirrors.kernel.org (Postfix) with ESMTPS id E80EC1C21390 for ; Sat, 17 Feb 2024 10:59:40 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 311CE5D72D; Sat, 17 Feb 2024 10:59:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="H+MpTSB+" Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.7]) (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 941A159B6C; Sat, 17 Feb 2024 10:59:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.7 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708167563; cv=none; b=iJbEN3xHJlwZHTY/CWgi50MhBnQsRmf70SkDiGe3N/ZJ1l4E1wyekyG21aom99LrzIkJ7iCjQPtIO/hNi95Au43ujVnITKvkBykwpeoRRVYjPMZOOxMFoks03bv9ivz+9alNaVhPyxjwcICDzr7uYCmUoHamVuuYaagQi40h3R0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708167563; c=relaxed/simple; bh=XF+WjLg4KIqsngigqVAPUzNcgTjwiRifxdEA0ayezwg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=AyJxdjDF+6rh2PUWKw0s7z0WMEPYjeKnlkXvTL++MUecfcJQH1aAyMX3UrgPfx1uNqR/WAjoRujnnQXoxouVlom1ZBhhaSaRq7cUqG11NRXmd1YtByGYDJq+/fGjHyBSyRBumFS60LaC1o9p81ZbZa7I9Yrhu5pSqng+fmIpHPo= 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=H+MpTSB+; arc=none smtp.client-ip=192.198.163.7 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=1708167561; x=1739703561; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=XF+WjLg4KIqsngigqVAPUzNcgTjwiRifxdEA0ayezwg=; b=H+MpTSB+GsqBssNOGC8aQYTz+CJXwIXdmpFEWVwO5mhPP2PMpBlrL2Wj QrmtPbIPtiJSwlkZUELavPjaTbth9Fx2J6uoZC49B9/BLuk8Djp+3Sog9 w/WsyVmjY07/ir3wnjRtRlsvEC5/srBFfnW6yEr4kkCpv/9pwgEUVitaA EkRQYfZ5M+ClGgor4wR3Z/P65YBXNgo71tZIsy7M2wEIkH27E+utmGzRr Vk7xIfgcI3f7stLND9DtTWvQq6inn/ZBqR3+mgQdkWs1Cz2ipcZQeBdAI OonjokmdGz0zlmQLD+cbK57iQWjw8t0R/J2mAGTmRje92PnyEmB3HHFYh g==; X-IronPort-AV: E=McAfee;i="6600,9927,10986"; a="27746704" X-IronPort-AV: E=Sophos;i="6.06,166,1705392000"; d="scan'208";a="27746704" Received: from fmviesa006.fm.intel.com ([10.60.135.146]) by fmvoesa101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Feb 2024 02:59:21 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.06,166,1705392000"; d="scan'208";a="4348712" Received: from fdefranc-mobl3.ger.corp.intel.com (HELO fdefranc-mobl3.Hitronhub.home) ([10.213.3.209]) by fmviesa006-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Feb 2024 02:59:18 -0800 From: "Fabio M. De Francesco" To: Peter Zijlstra , Dan Williams , linux-kernel@vger.kernel.org Cc: linux-cxl@vger.kernel.org, Ingo Molnar , Dave Jiang , Jonathan Cameron , Ira Weiny , "Fabio M. De Francesco" Subject: [PATCH 1/3 v5] cleanup: Add cond_guard() to conditional guards Date: Sat, 17 Feb 2024 11:59:02 +0100 Message-ID: <20240217105904.1912368-2-fabio.maria.de.francesco@linux.intel.com> X-Mailer: git-send-email 2.43.2 In-Reply-To: <20240217105904.1912368-1-fabio.maria.de.francesco@linux.intel.com> References: <20240217105904.1912368-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 Content-Transfer-Encoding: 8bit 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. This macro can be called multiple times in the same scope. Cc: Dave Jiang Cc: Peter Zijlstra Suggested-by: Dan Williams Suggested-by: Ira Weiny Suggested-by: Jonathan Cameron Signed-off-by: Fabio M. De Francesco --- include/linux/cleanup.h | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/include/linux/cleanup.h b/include/linux/cleanup.h index c2d09bc4f976..602afb85da34 100644 --- a/include/linux/cleanup.h +++ b/include/linux/cleanup.h @@ -134,6 +134,19 @@ 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); + * + * This macro can be called multiple times in the same scope, for it + * declares unique instances of type 'name'. + * * 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 +178,13 @@ static inline class_##_name##_t class_##_name##ext##_constructor(_init_args) \ #define __guard_ptr(_name) class_##_name##_lock_ptr +#define __cond_guard(__unique, _name, _fail, args...) \ + CLASS(_name, __unique)(args); \ + if (!__guard_ptr(_name)(&__unique)) _fail; \ + else { } +#define cond_guard(_name, _fail, args...) \ + __cond_guard(__UNIQUE_ID(scope), _name, _fail, args) + #define scoped_guard(_name, args...) \ for (CLASS(_name, scope)(args), \ *done = NULL; __guard_ptr(_name)(&scope) && !done; done = (void *)1) -- 2.43.2