From patchwork Tue Feb 27 16:48:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dan Williams X-Patchwork-Id: 207320 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:a81b:b0:108:e6aa:91d0 with SMTP id bq27csp2822391dyb; Tue, 27 Feb 2024 08:49:38 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCU7eVqtzZkIlyI+jVl9QoBAEwZrfUMcSNLTXKDh2Fnp+qPJZJBg+IcIzRNDQfnzmLLqoDRQpgsLKYsOrSHXN5wgGL8pRA== X-Google-Smtp-Source: AGHT+IH4vjH22rBcggW5xRNuek1ZP58T4A7rQdExSdh6hmSCakNRmuS8C1iKeLV9A58Q7MlUoYXt X-Received: by 2002:a17:902:e88a:b0:1dc:abe6:238 with SMTP id w10-20020a170902e88a00b001dcabe60238mr5626632plg.15.1709052578083; Tue, 27 Feb 2024 08:49:38 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709052578; cv=pass; d=google.com; s=arc-20160816; b=spV43jcZ4ngxxFPp2eboZ9OslWjP4B6Rp9YZcYigQAERko191MD6MNJaQcMd1pDS8Y Age2GMRo730KgZVHeQbqMm+IZm1ZYO59WMakfcllXqLiHhjra1Su1fRgekyJ4hC+WUUU FpBuBmIlFExtZylewEjYj5itYutOLSMbx+mL6FRAyb19+M7NjJ9SdNE0Zf4QKLAZANT7 6lzaLooG4aHCNaf1AaAJNKTQndo3KhrUkWbG2dIKs3EqPiLOEXG/QLF+fC1B8NNPUtjl wu2lXJ8Xq/16H5iIY6VYf8xnCwCgUPLzCTHysyanLxdyrKBoxTR4y7utnLlR5px7JX22 4Y4g== 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:user-agent:references:in-reply-to :message-id:date:cc:to:from:subject:dkim-signature; bh=dYIPOGqD/CKFk/Yo/OoXGLbCQOXBgG0fvxpGI3WX/8s=; fh=m3BpWLAONYEKdlzALB2w8yDVnxnPNGBx9VUtILVXH2U=; b=c233o8tsgqB1xZ/etmJP95kMfyz7i93voA7VQaJgOWLph2l/iWhcVfbMikhDEZ4XGt vyx/wR5U3jzdmIJw2aDNIvgLL8XX+WF5uNAifgXk6XYCAGTGlSP/y0v+0oGT+LZPJEfV 9aQYKNuxuIWYyz+VNC5EN/Mr2wU4UQEs9H22vvbD4CGA+65h45tK94zqfrFEMfQJxVvL B8/d6xLH5W8RhUwBFHrDRI5phVZ6R0B87pqD8HlyYQoZ9lhZ+LbOm8Hg8nOQd+x4h9CF iJn48FoMRW6ksyYwTBgYkM+Uj8tlRGcUh5KpPPkAZt4sCwR9XldZB2N1fvupiyRrNDj2 c8ew==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=HD90jN7Q; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-83648-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-83648-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id h18-20020a170902f71200b001da1d2ad541si1639385plo.572.2024.02.27.08.49.37 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Feb 2024 08:49:38 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-83648-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=HD90jN7Q; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-83648-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-83648-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 E0479283205 for ; Tue, 27 Feb 2024 16:49:37 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7F28E4DA0C; Tue, 27 Feb 2024 16:48:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="HD90jN7Q" Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.8]) (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 B02A8487A5; Tue, 27 Feb 2024 16:48:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.8 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709052536; cv=none; b=eDV9cmQ2MhkLLj/WUr1YD8E9h4Z7YZy1kc8b9xiHgL9SwVvkZK9z2E7p2z8Vu7sJtxijKqf78h/m+b8pJrcfV8q2WNYe9mlTxwm/JLimwq6/gQdj02MVh/nHaj0ShW7xMfzi2wauhoxaN3ka7kwmvzEj+4uEmyYnFzc0KdFHR1g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709052536; c=relaxed/simple; bh=6DJ8zNycy4XqO4nhbI6pzWZYZ6PRv0j+XW33IcU7Q/k=; h=Subject:From:To:Cc:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Yyp3TiitmmJQh95HTmKZoa6Jfbb0XRSlViOAgEvNt7ZEpk+YL3SW7u70DPPtkMNf78Vq96TxBlkaPQ9YYgj++hn4zWxxfuIP9Y57reSMFKvnjBadw7vF8BtV4N02FQY+s1V6wvNtQUNEA5G+wT+y+XAg4SNOBF8BelJxMZB4OLA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=HD90jN7Q; arc=none smtp.client-ip=192.198.163.8 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1709052535; x=1740588535; h=subject:from:to:cc:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=6DJ8zNycy4XqO4nhbI6pzWZYZ6PRv0j+XW33IcU7Q/k=; b=HD90jN7QyymSen/a5MM+f/Y+2wjP/BQi+UrWr3lnZDFdi6JDSY9EwTSn VOuFDfhG3QleuUtJlsLyaeFA8ONFA1SLhe5lMrpJNqZsAGYeWaMOglTb+ mfCenKCyPTexOZTiSoQyWFGoNX1WbCJyf1Sokgw2ctQwuIm219BvoszYu 3yQFkM8jQNuQWJNkx4GbAoHfkJ7r2QfFTLFduYe/Urt+N9JaFL4gk2tBH 5TIImFSLvEGW+rbN+vt/wj4op44jTJaKoOeC0wwZCP3ZaeFHxMqSU5hvq JmYTVtxFL9RlUcCGEJX6dJdf0xA9Sg7QJBFVZZfjm9uJQqKHvLG36vLnR g==; X-IronPort-AV: E=McAfee;i="6600,9927,10996"; a="20948825" X-IronPort-AV: E=Sophos;i="6.06,188,1705392000"; d="scan'208";a="20948825" Received: from orviesa006.jf.intel.com ([10.64.159.146]) by fmvoesa102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Feb 2024 08:48:54 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.06,188,1705392000"; d="scan'208";a="7536097" Received: from sshaik-mobl1.amr.corp.intel.com (HELO dwillia2-xfh.jf.intel.com) ([10.209.88.67]) by orviesa006-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Feb 2024 08:48:54 -0800 Subject: [PATCH 1/3] cleanup: Add cond_guard() to conditional guards From: Dan Williams To: torvalds@linux-foundation.org, peterz@infradead.org, gregkh@linuxfoundation.org Cc: Dave Jiang , Ira Weiny , Jonathan Cameron , "Fabio M. De Francesco" , Jonathan Cameron , Dave Jiang , linux-kernel@vger.kernel.org, linux-cxl@vger.kernel.org Date: Tue, 27 Feb 2024 08:48:53 -0800 Message-ID: <170905253339.2268463.9376907713092612237.stgit@dwillia2-xfh.jf.intel.com> In-Reply-To: <170905252721.2268463.6714121678946763402.stgit@dwillia2-xfh.jf.intel.com> References: <170905252721.2268463.6714121678946763402.stgit@dwillia2-xfh.jf.intel.com> User-Agent: StGit/0.18-3-g996c 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: 1792071515852197332 X-GMAIL-MSGID: 1792071515852197332 From: Fabio M. De Francesco 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 Reviewed-by: Jonathan Cameron Reviewed-by: Dave Jiang Signed-off-by: Dan Williams --- 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)