From patchwork Mon Feb 5 14:26:12 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: 13545641 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.16]) (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 0DF172C1A8; Mon, 5 Feb 2024 14:26:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.16 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707143188; cv=none; b=R46QM5RqzFif66z227iOX4mIyKOCQBe/pmTvMYFbw55YBA1LBbyGg1sLM9bQCMg7VyxE5sgB3MCrhDeQstAzEAEKW4bNH//UGHRrfL6UTOzxIXQdq/roMwoQSyXpX0UbHOu6m39+T5XoRE1AJ1iIVKUPiJW+H/e0Mf4UfvQ+z+c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707143188; c=relaxed/simple; bh=um9Vx8F751cSpatd8MK+ZXL+bVntzFGdFOdjUXLJBqg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ZGVIfXEliShsjCw682ahaXt0WPepX2DibaCoV0pLvpkrgt1WeOGOXTPqg9y9aXSvPRKpLVcHc1CjdZiKoVmx0AUnSuP3VIjj0E+pvL3J8G0jPgL8tJ/pcbvaaODdjo4JE8hQVf7GiaJcDn+XAXMr00+TLyx6vQs3i8+hsD8/RgE= 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=beHC2N0g; arc=none smtp.client-ip=192.198.163.16 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="beHC2N0g" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1707143187; x=1738679187; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=um9Vx8F751cSpatd8MK+ZXL+bVntzFGdFOdjUXLJBqg=; b=beHC2N0gV7fYZXUuf54+uLUjNrekzE6YuC+rTX+9vMDO7vN6SkkxfGS3 BlkIv0dZJWUdOGnaQuxhE1L/0IyHz+87icibP0nwKYxAG4+2usOAkc7Zt bm9hDPJvzS7p62kcU4kTRG2WjkBBQHd/Tv3JzHjMn7k+f4CHSm7DQsq1W iPYXMbTCaHRbVx+m58WtgzZrTXZDYwWyqxgakZv62YnCX05tiIkNUK7XD W8uRkydQroJ8KxKultIo2PZiehbbCXDdd3XtmDnP+U+BVU2HS2ev+8vvd ELnDiJsQbOj4du3J48YxsUPC4YO7R+sGVp1gufaSryVVO4jj+KBW0j3QS w==; X-IronPort-AV: E=McAfee;i="6600,9927,10974"; a="787668" X-IronPort-AV: E=Sophos;i="6.05,245,1701158400"; d="scan'208";a="787668" Received: from orviesa004.jf.intel.com ([10.64.159.144]) by fmvoesa110.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Feb 2024 06:26:26 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.05,245,1701158400"; d="scan'208";a="5473011" Received: from fdefranc-mobl3.ger.corp.intel.com (HELO fdefranc-mobl3.intel.com) ([10.213.21.219]) by orviesa004-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Feb 2024 06:26:23 -0800 From: "Fabio M. De Francesco" To: Peter Zijlstra , Dan Williams , linux-kernel@vger.kernel.org Cc: linux-cxl@vger.kernel.org, Ingo Molnar , Jonathan Cameron , "Fabio M. De Francesco" , Ira Weiny Subject: [PATCH 1/2 v2] cleanup: Add cond_guard() to conditional guards Date: Mon, 5 Feb 2024 15:26:12 +0100 Message-ID: <20240205142613.23914-2-fabio.maria.de.francesco@linux.intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240205142613.23914-1-fabio.maria.de.francesco@linux.intel.com> References: <20240205142613.23914-1-fabio.maria.de.francesco@linux.intel.com> Precedence: bulk X-Mailing-List: linux-cxl@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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 more statements in a block) that is passed to its second argument. That statement (or block) is executed if waiting for a lock is interrupted or if a _trylock() fails in case of contention. Usage example: cond_guard(rwsem_read_try, { printk(...); return 0; }, &semaphore); Consistently with the other guards, locks are unlocked at the exit of the scope where cond_guard() is called. Cc: Peter Zijlstra Suggested-by: Dan Williams Suggested-by: Ira Weiny Signed-off-by: Fabio M. De Francesco Reviewed-by: Jonathan Cameron Reviewed-by: Dave Jiang --- include/linux/cleanup.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/include/linux/cleanup.h b/include/linux/cleanup.h index c2d09bc4f976..88af56600325 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' are one or more + * statements that are executed when waiting for a lock is interrupted or + * when a _trylock() fails in case of contention. + * + * Example: + * + * cond_guard(rwsem_read_try, { printk(...); return 0; }, &semaphore); + * * 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,10 @@ 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 + #define scoped_guard(_name, args...) \ for (CLASS(_name, scope)(args), \ *done = NULL; __guard_ptr(_name)(&scope) && !done; done = (void *)1) From patchwork Mon Feb 5 14:26:13 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: 13545642 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.16]) (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 B593B2CCB4; Mon, 5 Feb 2024 14:26:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.16 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707143192; cv=none; b=XvKZ/nN4VcgYdULTp7VqUQ1rBpNnCpqawAp7d7sgfDh/8sl72WoXTuRP9qJMdt3ooGBKDN2riPPoIzeypftMFbH+kCR717Gso6Ox/Nu0Syb5pGi/UHd/gLn9C1tcCve22UfM4aUz31CU81IGINDMSxPo9PKyi2WlMK52+JMQTOk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707143192; c=relaxed/simple; bh=4ANXw9Eev2vDRjyUFpbniMCxexDvyqi2W5ezLzeVHOk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=VDU2Qxq910u2KRGDXODTVo428sA/fpDKLJ2PYBdkMFbMZPyRHsBuQmNUVoagIFQBRKCuazNxj8o/LbOkWrYGltT4LkE0I6olVkOYpgC55ROpoE3w71ta/B4Y2yWdAOagNIPXxkfu3TMrN21SRXH5xrw8WH1iCdgrLGZ38Sr+tMU= 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=ipme4T/P; arc=none smtp.client-ip=192.198.163.16 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="ipme4T/P" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1707143190; x=1738679190; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=4ANXw9Eev2vDRjyUFpbniMCxexDvyqi2W5ezLzeVHOk=; b=ipme4T/P5QSwK3TFYBMPutdyv+6TqVGZ+5HlW24qMba/xHlUxcaWO/qb KQQSHm02NZQgXc+NjHBjdfg2faRRfNokx2/7tnqqJVy3kcuamqM54DVbS HBO2iQyUVOFqfViZEeydMlCdX03dZDMdBSoavKExNPdUCuVHwvMGhyOhG rrsN893Ncd6nduTS6ChEdDpbofcMef+0Y4JllGbf0UhukxsEbS1dyug6x OMxIh4QPccQUEy1SZjMxjXnHfzMzzPyBAhKXF2iLYLWFfeIacs5GChgDa Iujw1IqQMf1l3WYmQG371DH/YQdXLzgY52YVIy9JXLnxf63EE2bNwvT2b w==; X-IronPort-AV: E=McAfee;i="6600,9927,10974"; a="787675" X-IronPort-AV: E=Sophos;i="6.05,245,1701158400"; d="scan'208";a="787675" Received: from orviesa004.jf.intel.com ([10.64.159.144]) by fmvoesa110.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Feb 2024 06:26:30 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.05,245,1701158400"; d="scan'208";a="5473019" Received: from fdefranc-mobl3.ger.corp.intel.com (HELO fdefranc-mobl3.intel.com) ([10.213.21.219]) by orviesa004-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Feb 2024 06:26:27 -0800 From: "Fabio M. De Francesco" To: Peter Zijlstra , Dan Williams , linux-kernel@vger.kernel.org Cc: linux-cxl@vger.kernel.org, Ingo Molnar , Jonathan Cameron , "Fabio M. De Francesco" , Ira Weiny Subject: [PATCH 2/2 v2] cxl/region: Use cond_guard() in show_targetN() Date: Mon, 5 Feb 2024 15:26:13 +0100 Message-ID: <20240205142613.23914-3-fabio.maria.de.francesco@linux.intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240205142613.23914-1-fabio.maria.de.francesco@linux.intel.com> References: <20240205142613.23914-1-fabio.maria.de.francesco@linux.intel.com> Precedence: bulk X-Mailing-List: linux-cxl@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Use cond_guard() in show_target() to not open code an up_read() in an 'out' block. If the down_read_interruptible() fails, the statement passed to the second argument of cond_guard() returns -EINTR. Cc: Peter Zijlstra Suggested-by: Dan Williams Suggested-by: Ira Weiny Reviewed-by: Jonathan Cameron Signed-off-by: Fabio M. De Francesco --- drivers/cxl/core/region.c | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/drivers/cxl/core/region.c b/drivers/cxl/core/region.c index 0f05692bfec3..bd3236786a25 100644 --- a/drivers/cxl/core/region.c +++ b/drivers/cxl/core/region.c @@ -666,28 +666,20 @@ static size_t show_targetN(struct cxl_region *cxlr, char *buf, int pos) { struct cxl_region_params *p = &cxlr->params; struct cxl_endpoint_decoder *cxled; - int rc; - rc = down_read_interruptible(&cxl_region_rwsem); - if (rc) - return rc; + cond_guard(rwsem_read_intr, return -EINTR, &cxl_region_rwsem); 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)