Message ID | 20240131134108.423258-1-fabio.maria.de.francesco@linux.intel.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel+bounces-46520-ouuuleilei=gmail.com@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:2087:b0:106:209c:c626 with SMTP id gs7csp2001124dyb; Wed, 31 Jan 2024 08:20:44 -0800 (PST) X-Google-Smtp-Source: AGHT+IHON3o4aNrl9XBDeiEtlxunXB7XwpKsKiceOUhEBAK0bK13jTyW3V38+jfbfVZO3udCtC+T X-Received: by 2002:a05:620a:ce3:b0:783:fdc2:51e8 with SMTP id c3-20020a05620a0ce300b00783fdc251e8mr1994115qkj.6.1706718043973; Wed, 31 Jan 2024 08:20:43 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706718043; cv=pass; d=google.com; s=arc-20160816; b=bOP8izupcmSOOyaBZU589jdvhOVCQTrR1xf/FnmR2igEtJD9m1MnE8QV8TM4MtFDNO LCmrKbSf6AdtSYqLqxYGbA86NGsYv32BWjlDFYQAS6OfAgB1EKf9pL86+dy+4MMPXIXJ /2D0BQsuvUS8m4KRyzYFdjqt2iT3ZDNY8gZJP3e/Au1bFRoiGpQ4Vae6VChgP9lhKsLj RcsNCg2n+8IeXyCL2vJ66qgxvu9H7KD5eXVk69sd+1byDs4hoWKERa+zfTc21Q22jOqx U2vMiHprcUWgM4gWxqJV5wY7X6Gl6M+Zxa2wpSix/LNjL8V/p0E3uCf/KSOxeNghQ/8R 6PSg== 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:message-id:date:subject:cc:to :from:dkim-signature; bh=k+x1S4JeVP6z14wyhvb8rQnwo5d5R6sYkkwyZDyIxAw=; fh=yAFRnn8aMAefBkif+9cgjzWGHuSkY8sQ54WcCtfnabs=; b=pqv45oGUaD8MeDZ7r8++A0qDfuh01P7D7xDW4IB3dVORYBlSnYbolLLF/kRP7lgoVe Jd7UKzu9TjlOfyGyjvUPIJEiXBGJp+1gNV6+RHE+YtVONaZi7AajntcYa+jQf6ykQl+9 r0gm01Ww5/ugawHlwZkt/QcOCmOVGMJzUdulA9J/te0VFrjWAji5woCu2KXGluNNn47H ONUPcmU1Uul8ysXsic8fs2mFELTlWIxrCSf2weM/mdONB0mwT+TSQiI6MUUjZQzM45cF EpVyzicCJSzYKuUT3KcMunvym30FSZKZ94QvbngfJAVCMK3fbFPbc2SNiPi49veIVEqB 7qnA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=YoJeecfU; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-46520-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-46520-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com X-Forwarded-Encrypted: i=1; AJvYcCUqunOWgdNRbZaVK2H5ifZfsDY7qGt58zJoVguOgP7cRg7WExwZ8qg5Xrc9aWXr34xqMKVdK/iHbOSBOD5cwqcqVEaTOQ== Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id cz51-20020a05620a36f300b007814f7972f9si12269949qkb.542.2024.01.31.08.20.43 for <ouuuleilei@gmail.com> (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 31 Jan 2024 08:20:43 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-46520-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=YoJeecfU; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-46520-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-46520-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id D09291C25E8C for <ouuuleilei@gmail.com>; Wed, 31 Jan 2024 13:42:03 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id BD7AC80BED; Wed, 31 Jan 2024 13:41:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="YoJeecfU" Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.13]) (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 0CC3880BF3; Wed, 31 Jan 2024 13:41:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.13 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706708479; cv=none; b=uohBbg2OvlfaorCJX6DFcLG6JzX2LM23pJ7NHkGuO1/i9T1el4zrp46wkigirhV+a4xINS9VnNHkke69uzBV6lhHnhGGxVk0tpQgx02XKl55JMNrBYiFeBxEm4dq8nHA+mzcyVNPaMxmnz6gDS93aklq6gHEjSXzaSRE6NSKs7M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706708479; c=relaxed/simple; bh=kkYLtuM+W9SeG0+q5Jd57SqlE6TQt9i1wFutcTGM+js=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=ku7OxnUnc3DSJvyhH+dMG69fdKbq0qbebh3oxWS2IR+SskhjPsxgj1hkbCoWkjS2IHVnqFKHU5E0wEabhP+BRty4mF8ste658r1LBF3EQDP6dwToZMEbRPBwZafxiEVM4dd8Q/N5zf8LYY6TpB4AtsYY+8xTND/IMfHN3L0SOpI= 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=YoJeecfU; arc=none smtp.client-ip=198.175.65.13 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=1706708478; x=1738244478; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=kkYLtuM+W9SeG0+q5Jd57SqlE6TQt9i1wFutcTGM+js=; b=YoJeecfUBTj+iSrEd3jm2Fy/4tWbPYwSx8YueC9kcrLBELCc9dKgycJ9 /GV5zo140KwDtYtfGMh93X3ksoPU6i2JKRAvNKO7uGJOtFty1+Pzy8RSp XaMaQcMaLUZGEUqe9kn1982UtESmSaJeknDtSQUDrLWuT8o7cZCshRq2F o7pNNBKe86tP1ZrFQD7Z1+UOeHSvNsqiKBZ8+CwKeDaxNTezYe3HHcn39 3yObPfyPLKtoM5MUhHLUJudZg9FmtL5fTQ98H+kmHFO0YsslRBVlNeRHx MggPdhRP//V6jqpoPwCLkK1MOfROx3QuBS5q7tZ+8HwZPkmHb9iU9UI0n g==; X-IronPort-AV: E=McAfee;i="6600,9927,10969"; a="10703186" X-IronPort-AV: E=Sophos;i="6.05,231,1701158400"; d="scan'208";a="10703186" Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by orvoesa105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 31 Jan 2024 05:41:17 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10969"; a="878785276" X-IronPort-AV: E=Sophos;i="6.05,231,1701158400"; d="scan'208";a="878785276" Received: from fdefranc-mobl3.ger.corp.intel.com (HELO fdefranc-mobl3.intel.com) ([10.213.21.82]) by fmsmga003-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 31 Jan 2024 05:41:14 -0800 From: "Fabio M. De Francesco" <fabio.maria.de.francesco@linux.intel.com> To: Peter Zijlstra <peterz@infradead.org>, dan.j.williams@intel.com, linux-kernel@vger.kernel.org, Ingo Molnar <mingo@kernel.org>, linux-cxl@vger.kernel.org Cc: "Fabio M. De Francesco" <fabio.maria.de.francesco@linux.intel.com>, Ira Weiny <ira.weiny@intel.com> Subject: [RFC PATCH v3] cleanup: Add cond_guard() to conditional guards Date: Wed, 31 Jan 2024 14:37:35 +0100 Message-ID: <20240131134108.423258-1-fabio.maria.de.francesco@linux.intel.com> X-Mailer: git-send-email 2.43.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: <linux-kernel.vger.kernel.org> List-Subscribe: <mailto:linux-kernel+subscribe@vger.kernel.org> List-Unsubscribe: <mailto:linux-kernel+unsubscribe@vger.kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1789464126089129097 X-GMAIL-MSGID: 1789623579421515772 |
Series |
[RFC,v3] cleanup: Add cond_guard() to conditional guards
|
|
Commit Message
Fabio M. De Francesco
Jan. 31, 2024, 1:37 p.m. UTC
Add cond_guard() to conditional guards.
cond_guard() is used for the _interruptible(), _killable(), and _try
versions of locks.
It stores a return value to a variable whose address is given to its
second argument, that is either '-1' on failure or '0' on success to
acquire a lock. The returned value can be checked to act accordingly
(e.g., to call printk() and return -EINTR in case of failure of an
_interruptible() variant)
As the other guards, it avoids to open code the release of the lock
after a goto to an 'out' label.
This remains an RFC because Dan suggested a slightly different syntax.
The changes to the CXL code are provided only to show the use of this
macro. If consensus is gathered on this macro, the cleanup of
show_targetN() will be submitted later in a separate patch.
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Dan Williams <dan.j.williams@intel.com>
Suggested-by: Ira Weiny <ira.weiny@intel.com>
Signed-off-by: Fabio M. De Francesco <fabio.maria.de.francesco@linux.intel.com>
---
Changes from v1 and v2:
I've taken into account Dan's comments (thanks).
drivers/cxl/core/region.c | 15 +++++----------
include/linux/cleanup.h | 19 +++++++++++++++++++
2 files changed, 24 insertions(+), 10 deletions(-)
Comments
I just noticed that this is not the final version. It misses a semicolon. Please discard this v3. I'm sending v4. Fabio On Wednesday, 31 January 2024 14:37:35 CET Fabio M. De Francesco wrote: > Add cond_guard() to conditional guards. > > cond_guard() is used for the _interruptible(), _killable(), and _try > versions of locks. > > It stores a return value to a variable whose address is given to its > second argument, that is either '-1' on failure or '0' on success to > acquire a lock. The returned value can be checked to act accordingly > (e.g., to call printk() and return -EINTR in case of failure of an > _interruptible() variant) > > As the other guards, it avoids to open code the release of the lock > after a goto to an 'out' label. > > This remains an RFC because Dan suggested a slightly different syntax. > > The changes to the CXL code are provided only to show the use of this > macro. If consensus is gathered on this macro, the cleanup of > show_targetN() will be submitted later in a separate patch. > > Cc: Peter Zijlstra <peterz@infradead.org> > Cc: Dan Williams <dan.j.williams@intel.com> > Suggested-by: Ira Weiny <ira.weiny@intel.com> > Signed-off-by: Fabio M. De Francesco > <fabio.maria.de.francesco@linux.intel.com> --- > > Changes from v1 and v2: > I've taken into account Dan's comments (thanks). > > drivers/cxl/core/region.c | 15 +++++---------- > include/linux/cleanup.h | 19 +++++++++++++++++++ > 2 files changed, 24 insertions(+), 10 deletions(-) > > diff --git a/drivers/cxl/core/region.c b/drivers/cxl/core/region.c > index 0f05692bfec3..560f25bdfd11 100644 > --- a/drivers/cxl/core/region.c > +++ b/drivers/cxl/core/region.c > @@ -668,26 +668,21 @@ static size_t show_targetN(struct cxl_region *cxlr, > char *buf, int pos) struct cxl_endpoint_decoder *cxled; > int rc; > > - rc = down_read_interruptible(&cxl_region_rwsem); > + cond_guard(rwsem_read_intr, &rc, &cxl_region_rwsem); > if (rc) > - return rc; > + return -EINTR; > > if (pos >= p->interleave_ways) { > dev_dbg(&cxlr->dev, "position %d out of range %d\n", pos, > p->interleave_ways); > - rc = -ENXIO; > - goto out; > + return -ENXIO; > } > > cxled = p->targets[pos]; > if (!cxled) > - rc = sysfs_emit(buf, "\n"); > + return sysfs_emit(buf, "\n"); > else > - rc = sysfs_emit(buf, "%s\n", dev_name(&cxled->cxld.dev)); > -out: > - up_read(&cxl_region_rwsem); > - > - return rc; > + return sysfs_emit(buf, "%s\n", dev_name(&cxled->cxld.dev)); > } > > static int match_free_decoder(struct device *dev, void *data) > diff --git a/include/linux/cleanup.h b/include/linux/cleanup.h > index c2d09bc4f976..a4b40d511f9e 100644 > --- a/include/linux/cleanup.h > +++ b/include/linux/cleanup.h > @@ -134,6 +134,20 @@ 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, ret, args...): > + * for conditional locks like mutex_trylock() or > down_read_interruptible(). + * 'ret' is a pointer to a variable where this > macro stores 0 on success + * and -1 on failure to acquire a lock. > + * > + * Example: > + * > + * int ret; > + * cond_guard(rwsem_read_try, &ret, &sem); > + * if (ret) { > + * dev_dbg("down_read_trylock() failed to down 'sem')\n"); > + * return 0; // down_read_trylock() returns 0 on contention > + * } > + * > * 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 +179,11 @@ static inline class_##_name##_t > class_##_name##ext##_constructor(_init_args) \ > > #define __guard_ptr(_name) class_##_name##_lock_ptr > > +#define cond_guard(_name, _ret, args...) \ > + CLASS(_name, scope)(args); \ > + if (!__guard_ptr(_name)(&scope)) *_ret = -1 \ > + else *_ret = 0 > + > #define scoped_guard(_name, args...) \ > for (CLASS(_name, scope)(args), \ > *done = NULL; __guard_ptr(_name)(&scope) && !done; done = (void *)1)
Fabio M. De Francesco wrote: > I just noticed that this is not the final version. It misses a semicolon. > Please discard this v3. I'm sending v4. Ok, but do please copy the aspect of scoped_conf_guard() to take a "_fail" statement argument. Passing a return code collector variable by reference just feels a bit too magical. I like the explicitness of passing the statement directly.
On Thursday, 1 February 2024 02:12:12 CET Dan Williams wrote: > Fabio M. De Francesco wrote: > > I just noticed that this is not the final version. It misses a semicolon. > > Please discard this v3. I'm sending v4. > > Ok, but do please copy the aspect of scoped_conf_guard() to take a > "_fail" statement argument. Passing a return code collector variable by > reference just feels a bit too magical. I like the explicitness of > passing the statement directly. I'm sorry I haven't been clear. The following call convention fails my tests: cond_guard(..., rc = -EINTR, ...); It always returns -EINTR, regardless of the success of down_read_interuptible(). There must be a reason that I can't see. It works only if we immediaely return an error code: cond_guard(..., return -EINTR, ...); But this is not what we want since we want to check 'rc'. Fabio
On Thursday, 1 February 2024 02:12:12 CET Dan Williams wrote: > Fabio M. De Francesco wrote: > > I just noticed that this is not the final version. It misses a semicolon. > > Please discard this v3. I'm sending v4. > > Ok, but do please copy the aspect of scoped_conf_guard() to take a > "_fail" statement argument. Passing a return code collector variable by > reference just feels a bit too magical. I like the explicitness of > passing the statement directly. I had introduced a bug in my tests that made me see failures when there were not. Now I fixed it and tests don't fail. I'm sending a new version that passes the return variable directly, not as a reference, similar but not equal to: cond_guard(..., rc, -EINTR, ...); Actually, I'm doing this: cond_guard(..., rc, 0, -EINTR, ...); I'm not passing 'rc = -EINTR' because I want to take into account the possibility that rc contains values different than 0 from previous assignments. I'm passing rc, so that the macro can assign either a success code or a failure error to this variable. Any value from previous assignments must be always overwritten: #define cond_guard(_name, _ret, _scs, _err, args...) \ CLASS(_name, scope)(args); \ if (!__guard_ptr(_name)(&scope)) _ret = _err; \ else _ret = _scs; I should have seen long ago that my tests were failing because of a missing 'else' when passing a statement in 'cond_guard(..., rc = -EINTR, ...);'. It had nothing to do with how to pass 'rc'. Sorry for that confusion. Fabio Fabio
On Thu, 01 Feb 2024 09:16:59 +0100 "Fabio M. De Francesco" <fabio.maria.de.francesco@linux.intel.com> wrote: > On Thursday, 1 February 2024 02:12:12 CET Dan Williams wrote: > > Fabio M. De Francesco wrote: > > > I just noticed that this is not the final version. It misses a semicolon. > > > Please discard this v3. I'm sending v4. > > > > Ok, but do please copy the aspect of scoped_conf_guard() to take a > > "_fail" statement argument. Passing a return code collector variable by > > reference just feels a bit too magical. I like the explicitness of > > passing the statement directly. > > I had introduced a bug in my tests that made me see failures when there were > not. Now I fixed it and tests don't fail. > > I'm sending a new version that passes the return variable directly, not as a > reference, similar but not equal to: > > cond_guard(..., rc, -EINTR, ...); > > Actually, I'm doing this: > > cond_guard(..., rc, 0, -EINTR, ...); Can we not works some magic to do. cond_guard(..., return -EINTR, ...) and not have an rc at all if we don't want to. Something like #define cond_guard(_name, _fail, args...) \ CLASS(_name, scope)(args); \ if (!__guard_ptr(_name)(&scope)) _fail Completely untested so I'm probably missing some subtleties. Jonathan > > I'm not passing 'rc = -EINTR' because I want to take into account the > possibility that rc contains values different than 0 from previous assignments. > I'm passing rc, so that the macro can assign either a success code or a > failure error to this variable. Any value from previous assignments must be > always overwritten: > > #define cond_guard(_name, _ret, _scs, _err, args...) \ > CLASS(_name, scope)(args); \ > if (!__guard_ptr(_name)(&scope)) _ret = _err; \ > else _ret = _scs; > > I should have seen long ago that my tests were failing because of a missing > 'else' when passing a statement in 'cond_guard(..., rc = -EINTR, ...);'. It > had nothing to do with how to pass 'rc'. Sorry for that confusion. > > Fabio > > Fabio > > >
On Thursday, 1 February 2024 12:36:12 CET Jonathan Cameron wrote: > On Thu, 01 Feb 2024 09:16:59 +0100 > > "Fabio M. De Francesco" <fabio.maria.de.francesco@linux.intel.com> wrote: > > > > [snip] > > > > Actually, I'm doing this: > > cond_guard(..., rc, 0, -EINTR, ...); > > Can we not works some magic to do. > cond_guard(..., return -EINTR, ...) > > and not have an rc at all if we don't want to. > > Something like > > #define cond_guard(_name, _fail, args...) \ > CLASS(_name, scope)(args); \ > if (!__guard_ptr(_name)(&scope)) _fail > > Completely untested so I'm probably missing some subtleties. > > Jonathan > Jonathan, Can you please comment on the v5 of this RFC? It is at https://lore.kernel.org/all/20240201131033.9850-1-fabio.maria.de.francesco@linux.intel.com/ The macro introduced in v5 has the following, more general, use case: * * int ret; + * // down_read_trylock() returns 1 on success, 0 on contention + * cond_guard(rwsem_read_try, ret, 1, 0, &sem); + * if (!ret) { + * dev_dbg("down_read_trylock() failed to down 'sem')\n"); + * return ret; + * } The text above has been copy-pasted from the RFC Patch v5. Please notice that we need to provide both the success and the failure code to make it work also with the _trylock() variants (more details in the patch). If we simply do something like: cond_guard(..., ret = 0, ...) to be able store in 'ret' the code of the contended case, that is 0. Since down_read_trylock() returns 1 on down semaphore, when we later check 'ret' with "if (!ret) <failure path>;" we always enter in that failure path even if the semaphore is down because we didn't store the success code in ret (and ret is still probably 0). This is why, I think, we need a five arguments cond_guard(). This can manage also the _interruptible() and _killable() cases as: cond_guard(..., ret, 0, -EINTR, ...) In this case we don't need 5 arguments, but we have a general use case, one only macro, that can work with all the three variants of locks. Fabio
On Thursday, 1 February 2024 16:13:34 CET Fabio M. De Francesco wrote: > On Thursday, 1 February 2024 12:36:12 CET Jonathan Cameron wrote: > > On Thu, 01 Feb 2024 09:16:59 +0100 > > > > "Fabio M. De Francesco" <fabio.maria.de.francesco@linux.intel.com> wrote: > > > [snip] > > > > > > Actually, I'm doing this: > > > cond_guard(..., rc, 0, -EINTR, ...); > > > > Can we not works some magic to do. > > > > cond_guard(..., return -EINTR, ...) > > > > and not have an rc at all if we don't want to. > > > > Something like > > > > #define cond_guard(_name, _fail, args...) \ > > > > CLASS(_name, scope)(args); \ > > if (!__guard_ptr(_name)(&scope)) _fail > > > > Completely untested so I'm probably missing some subtleties. > > > > Jonathan > > Jonathan, > > Can you please comment on the v5 of this RFC? > It is at > https://lore.kernel.org/all/20240201131033.9850-1-fabio.maria.de.francesco@ > linux.intel.com/ > > The macro introduced in v5 has the following, more general, use case: > > * * int ret; > + * // down_read_trylock() returns 1 on success, 0 on contention > + * cond_guard(rwsem_read_try, ret, 1, 0, &sem); > + * if (!ret) { > + * dev_dbg("down_read_trylock() failed to down 'sem')\n"); > + * return ret; > + * } > > The text above has been copy-pasted from the RFC Patch v5. > > Please notice that we need to provide both the success and the failure code > to make it work also with the _trylock() variants (more details in the > patch). The next three lines have been messed up by a copy-paste. They are: If we simply do something like: cond_guard(..., ret = 0, ...) We won't store the success (that is 1) in ret and it would still contain 0, that is the code of the contended case. > If we simply do something like: > > cond_guard(..., ret = 0, ...) > > to be able store in 'ret' the code of the contended case, that is 0. > > Since down_read_trylock() returns 1 on down semaphore, when we later check > 'ret' with "if (!ret) <failure path>;" we always enter in that failure path > even if the semaphore is down because we didn't store the success code in > ret (and ret is still probably 0). > > This is why, I think, we need a five arguments cond_guard(). This can manage > also the _interruptible() and _killable() cases as: > > cond_guard(..., ret, 0, -EINTR, ...) > > In this case we don't need 5 arguments, but we have a general use case, one > only macro, that can work with all the three variants of locks. > > Fabio
On Thu, 01 Feb 2024 16:32:25 +0100 "Fabio M. De Francesco" <fabio.maria.de.francesco@linux.intel.com> wrote: > On Thursday, 1 February 2024 16:13:34 CET Fabio M. De Francesco wrote: > > On Thursday, 1 February 2024 12:36:12 CET Jonathan Cameron wrote: > > > On Thu, 01 Feb 2024 09:16:59 +0100 > > > > > > "Fabio M. De Francesco" <fabio.maria.de.francesco@linux.intel.com> wrote: > > > > [snip] > > > > > > > > Actually, I'm doing this: > > > > cond_guard(..., rc, 0, -EINTR, ...); > > > > > > Can we not works some magic to do. > > > > > > cond_guard(..., return -EINTR, ...) > > > > > > and not have an rc at all if we don't want to. > > > > > > Something like > > > > > > #define cond_guard(_name, _fail, args...) \ > > > > > > CLASS(_name, scope)(args); \ > > > if (!__guard_ptr(_name)(&scope)) _fail > > > > > > Completely untested so I'm probably missing some subtleties. > > > > > > Jonathan > > > > Jonathan, > > > > Can you please comment on the v5 of this RFC? Would lose context of this discussion. > > It is at > > https://lore.kernel.org/all/20240201131033.9850-1-fabio.maria.de.francesco@ > > linux.intel.com/ > > > > The macro introduced in v5 has the following, more general, use case: > > > > * * int ret; > > + * // down_read_trylock() returns 1 on success, 0 on contention > > + * cond_guard(rwsem_read_try, ret, 1, 0, &sem); > > + * if (!ret) { > > + * dev_dbg("down_read_trylock() failed to down 'sem')\n"); > > + * return ret; > > + * } > > > > The text above has been copy-pasted from the RFC Patch v5. > > > > Please notice that we need to provide both the success and the failure code > > to make it work also with the _trylock() variants (more details in the > > patch). > > The next three lines have been messed up by a copy-paste. > They are: > > If we simply do something like: > > cond_guard(..., ret = 0, ...) > > We won't store the success (that is 1) in ret and it would still contain 0, > that is the code of the contended case. If there are cases that need to do different things in the two paths the define full conditions for success and failure. #define cond_guard(_name, _fail, _success, args...) \ CLASS(_name, scope)(args); \ if (!__guard_ptr(_name)(&scope)) _fail; \ else _success However I'm not sure that additional complexity is worth while. Maybe just handling failure is all we need. This should allow cond_guard(rwsem_read_try, return -EINVAL, , lock); or cond_guard(rwsem_read_try, rc = 1, rc = 0, lock); So similar to scoped_cond_guard() there is no need to have a local variable if all you want to do is return on failure. > > > If we simply do something like: > > > > cond_guard(..., ret = 0, ...) > > > > to be able store in 'ret' the code of the contended case, that is 0. > > > > Since down_read_trylock() returns 1 on down semaphore, when we later check > > 'ret' with "if (!ret) <failure path>;" we always enter in that failure path > > even if the semaphore is down because we didn't store the success code in > > ret (and ret is still probably 0). > > > > This is why, I think, we need a five arguments cond_guard(). This can manage > > also the _interruptible() and _killable() cases as: > > > > cond_guard(..., ret, 0, -EINTR, ...) > > > > In this case we don't need 5 arguments, but we have a general use case, one > > only macro, that can work with all the three variants of locks. > > > > Fabio > > > >
diff --git a/drivers/cxl/core/region.c b/drivers/cxl/core/region.c index 0f05692bfec3..560f25bdfd11 100644 --- a/drivers/cxl/core/region.c +++ b/drivers/cxl/core/region.c @@ -668,26 +668,21 @@ static size_t show_targetN(struct cxl_region *cxlr, char *buf, int pos) struct cxl_endpoint_decoder *cxled; int rc; - rc = down_read_interruptible(&cxl_region_rwsem); + cond_guard(rwsem_read_intr, &rc, &cxl_region_rwsem); if (rc) - return rc; + return -EINTR; if (pos >= p->interleave_ways) { dev_dbg(&cxlr->dev, "position %d out of range %d\n", pos, p->interleave_ways); - rc = -ENXIO; - goto out; + return -ENXIO; } cxled = p->targets[pos]; if (!cxled) - rc = sysfs_emit(buf, "\n"); + return sysfs_emit(buf, "\n"); else - rc = sysfs_emit(buf, "%s\n", dev_name(&cxled->cxld.dev)); -out: - up_read(&cxl_region_rwsem); - - return rc; + return sysfs_emit(buf, "%s\n", dev_name(&cxled->cxld.dev)); } static int match_free_decoder(struct device *dev, void *data) diff --git a/include/linux/cleanup.h b/include/linux/cleanup.h index c2d09bc4f976..a4b40d511f9e 100644 --- a/include/linux/cleanup.h +++ b/include/linux/cleanup.h @@ -134,6 +134,20 @@ 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, ret, args...): + * for conditional locks like mutex_trylock() or down_read_interruptible(). + * 'ret' is a pointer to a variable where this macro stores 0 on success + * and -1 on failure to acquire a lock. + * + * Example: + * + * int ret; + * cond_guard(rwsem_read_try, &ret, &sem); + * if (ret) { + * dev_dbg("down_read_trylock() failed to down 'sem')\n"); + * return 0; // down_read_trylock() returns 0 on contention + * } + * * 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 +179,11 @@ static inline class_##_name##_t class_##_name##ext##_constructor(_init_args) \ #define __guard_ptr(_name) class_##_name##_lock_ptr +#define cond_guard(_name, _ret, args...) \ + CLASS(_name, scope)(args); \ + if (!__guard_ptr(_name)(&scope)) *_ret = -1 \ + else *_ret = 0 + #define scoped_guard(_name, args...) \ for (CLASS(_name, scope)(args), \ *done = NULL; __guard_ptr(_name)(&scope) && !done; done = (void *)1)