Received: by 2002:a05:7412:3b8b:b0:fc:a2b0:25d7 with SMTP id nd11csp283441rdb; Thu, 8 Feb 2024 06:04:20 -0800 (PST) X-Google-Smtp-Source: AGHT+IHKzepORH6/BJN2dNIyUQ48p/GHHFjnsXvdmvGsfDACVQHYvjEjc6t5AD7bAgFn9kg8jZtH X-Received: by 2002:a05:6214:2465:b0:68c:aa25:c0ce with SMTP id im5-20020a056214246500b0068caa25c0cemr11531863qvb.30.1707401060275; Thu, 08 Feb 2024 06:04:20 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707401060; cv=pass; d=google.com; s=arc-20160816; b=jyrUzbd1Nh7/QlxEE+h2ittvAK7u39HHYU7J5L4GHO+u0uD8VsMpQS9Ei4Giz4tCme PeiXQxD/7RN9nafnDKj1asxSWyjzWxfnvJbJaTjtyMULAeWB3ezlM1M5IU/xJPXa+EUb HKXIoEjqQ931wpAShfLs/gykm5ssZjaeHFiCiBHJ8acz6L+WUD/J8D1j7s6iW1o9cgB8 YSzLe0MFIIC4ymEi/QJz/kXHnFsz+tKxN6RyOgJLLRGeBbsfD7uQcSM5492w6YcHrM9a rk8IZyO++eUhJEx76oqTT9VOY6PmmIxVn8xdSWCeGQK6kZo/BsDNbdLOykzYjKHLdDkd V0zg== 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:organization:references :in-reply-to:message-id:subject:cc:to:from:date; bh=95hyj5Fj51vUDE0tPpU+aB0lCTp6NonbahNGin29VLA=; fh=IxE2Kz8rybDIiAjh0uaPlDymC9YD8Dh8yfpao5XJ0KY=; b=ytiZC9o3L+tUtfS39X7hHNLWXR3qCkVPgHjsun59Hlf2ruk/mj41vMQrsEvpPi7IQJ zqq+1YBjYlXhYeAmHsziQdwtogiFqjEsxc+A4VgpY/2rSqwqlEOioSORuJ2yx6sF5kGq tXXvA0m6h2/78Y9WWRTKtG29/De6wL08fW8ZsE+2CMcU2nj8ZVSb2fT14fEV2uaL0lq+ g5E2wQAZ4xCnXbTch1HZa3mLBiMoFoL+KjO55J3PvJerKr/qqd5ud3GDxQKCHbVT8WW1 t27/C4dIij+Luoo7ZYcVm9VAwUZxRbSfu5Ikr6buq7+v6brjB6QgE4UQ99cpFAgoWC6Q L/0Q==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=huawei.com dmarc=pass fromdomain=huawei.com); spf=pass (google.com: domain of linux-kernel+bounces-58210-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-58210-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com X-Forwarded-Encrypted: i=2; AJvYcCWb/vvcCA48KTq3kbkSZxlsNno3H6fgNd1Shwl1Byi5PLcQBUCB6ba4VPLfxuSD0mQZhCWqpY8erBGNxz2k2sIJz6TEE0Av3QNpI8HLHw== Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id u9-20020ad45aa9000000b0068c7e7a77acsi45391qvg.605.2024.02.08.06.04.20 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Feb 2024 06:04:20 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-58210-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; arc=pass (i=1 spf=pass spfdomain=huawei.com dmarc=pass fromdomain=huawei.com); spf=pass (google.com: domain of linux-kernel+bounces-58210-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-58210-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.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 EDCED1C23B7B for ; Thu, 8 Feb 2024 14:04:19 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6E83A78696; Thu, 8 Feb 2024 14:04:13 +0000 (UTC) Received: from frasgout.his.huawei.com (frasgout.his.huawei.com [185.176.79.56]) (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 D9C4A77F33; Thu, 8 Feb 2024 14:04:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.176.79.56 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707401052; cv=none; b=c4Q5ak3z4EdP6ReTqCoSwZMPwtAoqLCbixzGuqLL//7G6TGsGJjlEzMnt/3TOSjPK/WQ2I5CdEhqlIgCHxs5XdbsTELyrkjGEpgbZ2Plpq6m7YTSZn+rKEhZnreKORE13xft2MV3EbhsEmoRbJkXY+S0PP5HhVihi87kTm8ARpc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707401052; c=relaxed/simple; bh=4kvYFrZx1wBuKLgLt/zTMYUAP5dUk4VX1HzeYWC4XaM=; h=Date:From:To:CC:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=ZO1fUtjQd5JwnGAs7HUNRrXvFK6ywBEyGNPftE2L3T1lGNTVTqUHvdByMiWvsRJLDNUBvHz9XSwAzWctgWOKJf5EBAuIi8rJ7ONK1vy1URkMCzIFWoBi9SDdnvNAVM2qPAoIpldE4gQ9fudNpgFnpelCg5CtqzVQJueJyzTbqOI= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=Huawei.com; spf=pass smtp.mailfrom=huawei.com; arc=none smtp.client-ip=185.176.79.56 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=Huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huawei.com Received: from mail.maildlp.com (unknown [172.18.186.31]) by frasgout.his.huawei.com (SkyGuard) with ESMTP id 4TVzC91vLnz6JB44; Thu, 8 Feb 2024 22:00:21 +0800 (CST) Received: from lhrpeml500005.china.huawei.com (unknown [7.191.163.240]) by mail.maildlp.com (Postfix) with ESMTPS id 564BC140595; Thu, 8 Feb 2024 22:04:04 +0800 (CST) Received: from localhost (10.202.227.76) by lhrpeml500005.china.huawei.com (7.191.163.240) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.35; Thu, 8 Feb 2024 14:04:03 +0000 Date: Thu, 8 Feb 2024 14:04:03 +0000 From: Jonathan Cameron To: "Fabio M. De Francesco" CC: Peter Zijlstra , Dan Williams , , , Ingo Molnar , Dave Jiang , Ira Weiny Subject: Re: [PATCH 1/2 v4] cleanup: Add cond_guard() to conditional guards Message-ID: <20240208140403.0000238f@Huawei.com> In-Reply-To: <20240208130424.59568-2-fabio.maria.de.francesco@linux.intel.com> References: <20240208130424.59568-1-fabio.maria.de.francesco@linux.intel.com> <20240208130424.59568-2-fabio.maria.de.francesco@linux.intel.com> Organization: Huawei Technologies Research and Development (UK) Ltd. X-Mailer: Claws Mail 4.1.0 (GTK 3.24.33; x86_64-w64-mingw32) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: 7bit X-ClientProxiedBy: lhrpeml500004.china.huawei.com (7.191.163.9) To lhrpeml500005.china.huawei.com (7.191.163.240) On Thu, 8 Feb 2024 14:04:23 +0100 "Fabio M. De Francesco" wrote: > 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 I like the defensive else {} Reviewed-by: 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)