From patchwork Thu Feb 1 13:10:33 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: 195395 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:2719:b0:106:209c:c626 with SMTP id hl25csp291226dyb; Thu, 1 Feb 2024 08:52:31 -0800 (PST) X-Google-Smtp-Source: AGHT+IFxEFYoDtrlzb5oImASK3Lg3c5mgqQEy3XYUl92+l4ZOwpSOF8ZUUgLWnXkWpFpziKPIWfI X-Received: by 2002:a05:6830:1101:b0:6e0:ee9a:ee97 with SMTP id w1-20020a056830110100b006e0ee9aee97mr3278361otq.27.1706806350809; Thu, 01 Feb 2024 08:52:30 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706806350; cv=pass; d=google.com; s=arc-20160816; b=Oz+plKbv8egBQ6bofsrFXKtummiFaFfLIXkkvi/EDVaNNteofJMU9IbQ3mJTcsF2HI eBsgIBCMJJ0x485Dp+zrkxzbHrOwhuYaRWYRUWf9oHOHy/6/cGcd6tMVHIsxS1sVCukj Bui2RXYvO0Pybvd6yfBqznTbLzAZBurL0C+C21miIlNSjipe0mUI9BxClL+Woe0TkB4J xNjuzKk6seSy0Qak/tQB9yONxyOcyQPuXT2z85+NMuYDKA+mvwUp7BlEpFk3aF9p05kY mgz9TPuDdOJfOu1UVwO5aMQLAOJ2vd3G+Fm+WXuLdFoA5BLVZhnpKWYQIuMVOjl6DWrn C6+w== 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=SpCbI5lUVnZ4kMuYYmxJwwyH7t08FQScJR1WOEdTzwA=; fh=AG9ntMij6CW3HYavBNYMXdPOtA6jZgnsUu51CwNg1vE=; b=UYD9MjgdFk5haQRErSYYpenLh+fPbn1zFCTRe/Ar5/vg9+HZU9A5INU6tx2ttYVIit amKtwd+kTQr4UXkOsAXaVpdURUlF7IKXtI/GOsdQMuzJnSecWUM073+YtCNVuPISr5Rb 9xZ/+NGf4v5c6z2gVr4P22JOMV/prSsmyh4jTWtgsqkWGkNMcU0PsacUckqEfrp+6jZM dzzMmdRbIaKrC+03ueKoN1/eR67AvONSKwZASAjQlbkYy0TSwLAIWPdHjU1ZspDw+UDY 3naOocciBTh6q7CHvNNx1oXcJNDS76NsFmdV9Y9coRuSOQCUg3Z/MFlSuq1dkLI2GJgZ QiHw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=jhNcqcKZ; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-48201-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-48201-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com X-Forwarded-Encrypted: i=1; AJvYcCU5+lI5rCd0CDhkBa4XELIl/6PZO7pVn3ZhMcwCuntr+XXYXZEakF6GGIY3x8+JK9kH5LZ+0MSaWm8AUshoYFbAdTgt+Q== Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id n29-20020a63721d000000b005c6b59c91bbsi12490962pgc.654.2024.02.01.08.52.30 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 01 Feb 2024 08:52:30 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-48201-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=jhNcqcKZ; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-48201-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-48201-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 B931C2879C7 for ; Thu, 1 Feb 2024 13:12:00 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 342345B696; Thu, 1 Feb 2024 13:10:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="jhNcqcKZ" Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.17]) (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 934585338C; Thu, 1 Feb 2024 13:10:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.17 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706793047; cv=none; b=kEuH1pXEGoJZ9dS3ozU3VoEeF37F5x1eexb7le62RPyEBo+oVcXg4zC/aIuIbXgJque+XD7e/+gDoRLyPEeXDNsdGxtx8vfKuOZTKeorS2aI5TyWEfOv9RfjcT8HvRegk2OAP85eH6o/xJGIl/NH0fs8/L8s9cTl+ny0MXd9gAk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706793047; c=relaxed/simple; bh=5AhN+56yyPZ/UPaJHZIX7QR4zt1xAGI4R1FdhndMxKE=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=DVqs1Tjmkv9rsYSN5QuBMJl6nbCNTZQFEd+3+/x8DNcmgfjxTHzMLUczv90dyl3r0wrC4hPeqvxumc7mgGNZVNFQZlKUCyQ5G0yQJwHW9y2yl7ZBo/Abu0XFCr4AcFzh8/BU95pj/Z9DUIEfO050aQ0aTDSmHbK2ksfkMuDH7k8= 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=jhNcqcKZ; arc=none smtp.client-ip=198.175.65.17 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=1706793046; x=1738329046; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=5AhN+56yyPZ/UPaJHZIX7QR4zt1xAGI4R1FdhndMxKE=; b=jhNcqcKZtBLY+hFklhzSQ4o4H01ULXLIr1hdDHASj5P0Th/xC9t06aSH WryG8G4kjfRYu+5BsuhC8kEOw3bkkBITuGcKC2xEFkOy4zymaUSowaL0b Qb5pu0p5O/nDfWwd+1aq+b6MCugIpdQGXJXM1tVLgVmgAdsklmYn3AKOn ySPEIPCjdkBkTxLoO3AN1yjmBgbNka5WI+bDUPuaXD4NvqnkSU3NaApct hT90bDwnqcKys4di/e3Swu1YHwq6hBJ2x0cxqZCU/Ru0ts2JcxW08+OsW 2j9DYZSncdZV4ijhaf4qLhcNB9rcQgQil6B3S8sn5KaG8M+g5Bhce/1HM w==; X-IronPort-AV: E=McAfee;i="6600,9927,10969"; a="71086" X-IronPort-AV: E=Sophos;i="6.05,234,1701158400"; d="scan'208";a="71086" Received: from fmviesa006.fm.intel.com ([10.60.135.146]) by orvoesa109.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Feb 2024 05:10:45 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.05,234,1701158400"; d="scan'208";a="79456" Received: from fdefranc-mobl3.ger.corp.intel.com (HELO fdefranc-mobl3.intel.com) ([10.213.1.220]) by fmviesa006-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Feb 2024 05:10:42 -0800 From: "Fabio M. De Francesco" To: Peter Zijlstra , Ingo Molnar , Dan Williams , linux-kernel@vger.kernel.org Cc: linux-cxl@vger.kernel.org, "Fabio M. De Francesco" , Ira Weiny Subject: [RFC PATCH v5] cleanup: Add cond_guard() to conditional guards Date: Thu, 1 Feb 2024 14:10:33 +0100 Message-ID: <20240201131033.9850-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: 1789716175913271270 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 that is given to its second argument. In case of success it stores the third argument, whereas in case of failure it stores the fourth. The returned value can be checked to act accordingly. cond_guard() always stores either success or failure codes because certain functions, e.g. down_read_trylock(), return 1 on success and 0 on contention. By storing 1 to ret, an 'if (!ret)' after a successful down_read_trylock() will correctly evaluate false and so the failure path is not executed. 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 different syntax. The changes to the CXL code are provided only to show the use of this macro. If consensus is reached on this macro, the cleanup of show_targetN() will be submitted later with a separate patch. Cc: Peter Zijlstra Suggested-by: Dan Williams Suggested-by: Ira Weiny Signed-off-by: Fabio M. De Francesco --- Changes from v1-4: Addressed Dan's requests (thanks) to change cond_guard() interface. drivers/cxl/core/region.c | 13 ++++--------- include/linux/cleanup.h | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/drivers/cxl/core/region.c b/drivers/cxl/core/region.c index 0f05692bfec3..15e03edce7af 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, 0, -EINTR, &cxl_region_rwsem); if (rc) return rc; 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..63d7d5bc374f 100644 --- a/include/linux/cleanup.h +++ b/include/linux/cleanup.h @@ -134,6 +134,21 @@ 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, scs, err, args...): + * for conditional locks like mutex_trylock() or down_read_interruptible(). + * 'ret' is a variable where this macro stores 'scs' on success and 'err' + * on failure to acquire a lock. + * + * Example: + * + * 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; + * } + * * 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 +180,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, _scs, _err, args...) \ + CLASS(_name, scope)(args); \ + if (!__guard_ptr(_name)(&scope)) _ret = _err; \ + else _ret = _scs + #define scoped_guard(_name, args...) \ for (CLASS(_name, scope)(args), \ *done = NULL; __guard_ptr(_name)(&scope) && !done; done = (void *)1)