Received: by 2002:a05:7412:3b8b:b0:fc:a2b0:25d7 with SMTP id nd11csp295707rdb; Thu, 8 Feb 2024 06:20:36 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCX5rNJmnT1zhnwClkaywqD33jMStyR1jhZvcmLXf7NwsYytG4Og2wjrHdp8OAWdKbeLIRoeDD/znxiCcf75g7MyGVPscESV3UU5PoYQpA== X-Google-Smtp-Source: AGHT+IH7OKq+V6NKDdWVPEEpFKhMl7Iqa2XEbyS7Kx3+/7VGD98PVO9dUeYZmHImgRD6TQXrFiHu X-Received: by 2002:a17:90b:3e8d:b0:296:286c:4dd6 with SMTP id rj13-20020a17090b3e8d00b00296286c4dd6mr5786102pjb.46.1707402036120; Thu, 08 Feb 2024 06:20:36 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707402036; cv=pass; d=google.com; s=arc-20160816; b=Il2xSUr6oBO/6DkENM71T5W88jsLGcV2VGm1YRMBWh7RW2/OsDJclgoX7+ekN3iWeh e30m4eOxKAZsvP/xYG9nyL13ZmMHVE3UjX3XLm5a8SIykwQlhehZFDciyiRCO/p0vYWA LdUnV24b4gV7TPnw2Yn52q7NR6v8UrYaWmjfrIj3MYCQ5Dz+d221eDNhqzomiyxjfiw1 qHgL0FelRcKo5f4wnj3zqOlI/OOqzyP8IOjSsa65TCfnRPN5LwGZbItnk8LXtg4lNP+j rjan5t8bfGhwyLsGPfzkplnFgS/jtj9JOhW6l62nQw6l1dcRf55WvwTkuOnG1uC4Yrt0 w8tA== 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=jb55GFKrkYMPi3e6JYN0dcxc3CKeQ/NXeQOya1ug+G8=; fh=pRSE7RLakXFB0CHAMYr3zfvtSL+k0CtRFt73YLKCLug=; b=WYCBr/0wlc8K4DaQuYofokLn8HHdz/fulZQdq7XCX6ciD+QACzfq4wCibE6e+MM4EW DRB/gpPqD/iP6HD3SXW52hPhQpCMo292YVwDYGypQkWDYC7eY5XXcZmG1PhopwFawOEZ fzes+TNzRIn1sd0w7YYZ2+hCXrl3dwC+0OehYRAwl/5TCbhkL2BSAPmSz4sapyeO6nI5 S/N36SgZ7t3gJhKbmdjFRuJrYRnT6hMIovMv3G0bHBk0TeJW/tjK4+VCc896XIeghpIt Xl+QRQong2BShCr9CkIAXdQs2io36H8Ddl6ID2Xl92Po0U/lbl8tCPH2nZJ61B5lXslK 1cVQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=mx+aWCBT; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-58104-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-58104-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com X-Forwarded-Encrypted: i=2; AJvYcCXovs3Sy46KLte2+xAABqbK9JtmfI0x8WQOOUs0cB+1XS7y/PNqf0i/YXjNFa1vIbJFNNRkp/tGfvKgotWWRbxu7SUr19h7Lpjvq8Bv4w== Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id x17-20020a17090aca1100b00296e95152c9si1465887pjt.171.2024.02.08.06.20.35 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Feb 2024 06:20:36 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-58104-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=mx+aWCBT; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-58104-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-58104-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 sy.mirrors.kernel.org (Postfix) with ESMTPS id 19253B25119 for ; Thu, 8 Feb 2024 13:05:04 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 49FB376414; Thu, 8 Feb 2024 13:04:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="mx+aWCBT" Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.18]) (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 CFB74763E4; Thu, 8 Feb 2024 13:04:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.18 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707397479; cv=none; b=GNIcQOfsl8B/cYq18mQFzByP9Nu5EDOCY8hL6x00jZOmZYG7yukJQAIp50XC+4RPyHLTRmiu9tbTMsEWu4+BPwtSSP86gxiUN4tKbpBxwDXFcX78wYMGWhp85ZUiS10zyTpkGHWKj1vSSC98z1AN2KD35JMmqlnzRre2+EWy0VI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707397479; c=relaxed/simple; bh=3nWZ+AvAgVTv540Fkbwwjic5kaTrkMjBgX1KoIMMW1Q=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=OPPpyuKg+isfndO3LMaEI8USeL78xHkH1A8H+t5A05wTPjF3B9tOlvlK8YEgQihzl3Zp3ZzycOe91qLI+jWIxjgwrQBW9TtnNn+lbpLDLNbFgYFZPSxOFV4Oj4lCaxN1LoWwpRTCM57NXrc1ruDfphlniSCg6aSPqbHN/yzuT+k= 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=mx+aWCBT; arc=none smtp.client-ip=198.175.65.18 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=1707397478; x=1738933478; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=3nWZ+AvAgVTv540Fkbwwjic5kaTrkMjBgX1KoIMMW1Q=; b=mx+aWCBTC8hOCtoDOM2eNnhBsChuiOtAUb4y8Skro4HM016hMhOM+tG+ GCM6ekEcYFaR0wuqXKHLIyGv8XmO4qZpoGiRRH5JLLMqojdSozdlxlcNC /VvPYuaOxb3tZWHQvfnClAhgZ3ZHMeazDYegiZxcfqVl9aQNJ3AW6uunR UWBlpjsUvpycg4OhHVHrlcBQVjOAk4V2dvRL16ur/KA0lcr8XjA3MH5lE 20mb787Mjzn4w0o03U5s12CYS9SuAl6/qZaopKjv1WwFE4fVIaY2YfJJz k4RwHhJI1npnk51cDNT9uIeJjdFRs0QY7645UX5Gk2xutJ+evp+ghyk1R w==; X-IronPort-AV: E=McAfee;i="6600,9927,10977"; a="1354950" X-IronPort-AV: E=Sophos;i="6.05,253,1701158400"; d="scan'208";a="1354950" Received: from fmviesa008.fm.intel.com ([10.60.135.148]) by orvoesa110.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Feb 2024 05:04:37 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.05,253,1701158400"; d="scan'208";a="1884034" Received: from fdefranc-mobl3.ger.corp.intel.com (HELO fdefranc-mobl3.intel.com) ([10.213.2.200]) by fmviesa008-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Feb 2024 05:04:33 -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/2 v4] cleanup: Add cond_guard() to conditional guards Date: Thu, 8 Feb 2024 14:04:23 +0100 Message-ID: <20240208130424.59568-2-fabio.maria.de.francesco@linux.intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240208130424.59568-1-fabio.maria.de.francesco@linux.intel.com> References: <20240208130424.59568-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. Cc: Jonathan Cameron Cc: Peter Zijlstra Suggested-by: Dan Williams Suggested-by: Ira Weiny Reviewed-by: Dave Jiang Reviewed-by: Ira Weiny Signed-off-by: Fabio M. De Francesco --- include/linux/cleanup.h | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/include/linux/cleanup.h b/include/linux/cleanup.h index c2d09bc4f976..7b54ee996414 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) -- 2.43.0