From patchwork Thu Feb 1 01:09:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Fabio M. De Francesco" X-Patchwork-Id: 195169 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:693c:2685:b0:106:209c:c626 with SMTP id mn5csp244952dyc; Wed, 31 Jan 2024 22:22:06 -0800 (PST) X-Google-Smtp-Source: AGHT+IHbn2BkXxJ9VfXbVr9S4DGInBM74VYnXeY0BSgykpMabnSISSfL0zUEt9jb3AP5DM5WZnsj X-Received: by 2002:a2e:3004:0:b0:2d0:7981:17b0 with SMTP id w4-20020a2e3004000000b002d0798117b0mr312568ljw.44.1706768526061; Wed, 31 Jan 2024 22:22:06 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706768526; cv=pass; d=google.com; s=arc-20160816; b=1E018QRIBPTAcnla9hHVeW4Jjx6PlV2ADAXJxrL31kpfKuma05QgTIyt321jifK7+3 FRJukbzbwbEqJ8lE7uSkraRqjF7gY8WrkAx3mYSHdoYJnUWz1dfIHbxDmHCPg7eGpUm9 PKjbVrFj3bWaQcFALja7QTkr2rUgeI6XwgNO8FCH8nkdZA4l6QarUbDF+wLQcDBAUiLE DHJcpTdnJRVjYDzcA3adjVLVlEi9EiZ27VjCgfEkV8Hflx7oq6rw0DUZ/ROpzLL00vIq AUC9xr9Ro1JjRmtHWMb6Rv4JbpWHGRiulM7+Sx4kRB7pyx7KrYXi/d+YVsh18VBjkJVx Mmng== 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=3Ix9JvIFPVcKn/XkE9HIYUb7FnEZ4jFulAijYeDGQQ8=; fh=ixDbCrFpiJ8AMGmoBjGiiuRl8xEt1/OrQx7PNjgdmWM=; b=HzbKrXPIefNP9soNZ5Z8br6VguhzZyxbCxP7T8s0fzlmGX1ls5W2zSxw/2Pl81fbN/ znU73xC7/1TPHzg98QOaWOmoxABJbQog3cMSgyPus+JNeKGmuh3HDAEfXr2yyfbkqY7e nn43ZwSoEIAI6yPuQHx3N2e/5J5VxnjM7erhgQIsrfCpHNUtmgOZFFAA58Z6Rowm/1KE WpsbGaCWq4iobFW6Ifd3DP7rbxyWU787sVvMNbYiCKhYDszxSNcvvSf9R9/OQ699TuP1 K0GMEdJylH/6xAlGhC+S8q8U/98lZFE3QpWMamYcji0xCvFFJLwvRxV7KNtWGJOPsdc+ 4Vrw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=hUvzNQ+G; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-47481-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-47481-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com X-Forwarded-Encrypted: i=1; AJvYcCWAWMVv3Ndor+6O6R/+FP2/zB9ucNIYd8yYrBdG3470SaA626Ge4nzGyGBTMnENPcTh11LciEbQfAZcR97N3jFq0Q4s3g== Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id w18-20020a05640234d200b0055f0b2cf509si3803911edc.161.2024.01.31.22.22.05 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 31 Jan 2024 22:22:06 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-47481-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=hUvzNQ+G; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-47481-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-47481-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 am.mirrors.kernel.org (Postfix) with ESMTPS id 27D4D1F2C140 for ; Thu, 1 Feb 2024 01:13:33 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 861632C686; Thu, 1 Feb 2024 01:11:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="hUvzNQ+G" Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.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 009922C1A7; Thu, 1 Feb 2024 01:11:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.13 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706749867; cv=none; b=ej2+zB8sBi2NTjEd/fpcON8NTp1qEXx9BIcoRxR5s2cUxRsdVkzn5VQHfjArnJLTiUjvSLoUtorL/JbNDFSSQvxSr2MRdNAvYJ38lQPK9gnIfzdx5ot4JPhXaISIAPsfjQDNWLyIWrO95F4Z7NHnagVZnQ3Jb3ZIJwP6ve3DwdM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706749867; c=relaxed/simple; bh=L32ltv4g2uOGoYGmH9JgTrOWs6BXkXHAwaeCLPaMu+c=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=JLCT+M/uSQB8KIzD42EbqN0mR+hWjEiDY9bJgM4agVmGxTM1C8nrD18GCCQtDD+VYuXUpEiLhfau14GcDqZrcpDD84uQft+ziyiZcM5H0ZL59nk0oxKYMDQwjDu3dpM+cEUt9VLKkuuVGv5uRdDeFeQtxTbtDWg//73JF1dJm7E= 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=hUvzNQ+G; arc=none smtp.client-ip=192.198.163.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=1706749866; x=1738285866; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=L32ltv4g2uOGoYGmH9JgTrOWs6BXkXHAwaeCLPaMu+c=; b=hUvzNQ+G+ReD/LFTKej8cj8mxdNRKwdXETGwMzrhzWP8rvZo3TFcQjQU OyaPio2nwp7S1wo8yGB9+BIqQyGWiR1GdNgnBNsOcbTnxaoQ3V75Bt54Q rNGgkA3kaPU8PribeDGKkfTCQwjVkLCdkRgpmCwvmxT40p4OGoog2qzaV EHdP/RlXs0k5xEPQ6rcPewK2b8zxa6rrvDl2bb2o11ekT/gC07///kep0 3omGE26Uet1fYQxvAdbQBLEOMBGufVuhogai/riCypiEfUO1+a83Tnn6f vFoKWrgVk+unMLe0ME1PvhMLM2viVuV8V0chXL0zXY+l61MXZXT+M4qIs g==; X-IronPort-AV: E=McAfee;i="6600,9927,10969"; a="2714091" X-IronPort-AV: E=Sophos;i="6.05,233,1701158400"; d="scan'208";a="2714091" Received: from orsmga005.jf.intel.com ([10.7.209.41]) by fmvoesa107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 31 Jan 2024 17:11:02 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10969"; a="961770473" X-IronPort-AV: E=Sophos;i="6.05,233,1701158400"; d="scan'208";a="961770473" Received: from fdefranc-mobl3.ger.corp.intel.com (HELO fdefranc-mobl3.intel.com) ([10.213.21.45]) by orsmga005-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 31 Jan 2024 17:10:57 -0800 From: "Fabio M. De Francesco" To: Peter Zijlstra , dan.j.williams@intel.com, linux-kernel@vger.kernel.org, Ingo Molnar , linux-cxl@vger.kernel.org Cc: "Fabio M. De Francesco" , Ira Weiny Subject: [RFC PATCH v4] cleanup: Add cond_guard() to conditional guards Date: Thu, 1 Feb 2024 02:09:31 +0100 Message-ID: <20240201011052.474129-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: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1789464126089129097 X-GMAIL-MSGID: 1789676514008421910 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). It stores both success and failure to always overwrite any old value which is possibly contained in the return variable. 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. Furthermore, I've only been able to set the return variable by dereferencing its address. "_ret = _err" fails my tests. Any hint on why it doesn't work for me is welcome. 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 Cc: Dan Williams Suggested-by: Ira Weiny Signed-off-by: Fabio M. De Francesco --- Changes from v1 and v2: Addressed Dan's comments (thanks). Changes from v3: Fixed a grammar error in macro. 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..a72c52430665 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)