From patchwork Tue Feb 27 16:48:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dan Williams X-Patchwork-Id: 207320 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:a81b:b0:108:e6aa:91d0 with SMTP id bq27csp2822391dyb; Tue, 27 Feb 2024 08:49:38 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCU7eVqtzZkIlyI+jVl9QoBAEwZrfUMcSNLTXKDh2Fnp+qPJZJBg+IcIzRNDQfnzmLLqoDRQpgsLKYsOrSHXN5wgGL8pRA== X-Google-Smtp-Source: AGHT+IH4vjH22rBcggW5xRNuek1ZP58T4A7rQdExSdh6hmSCakNRmuS8C1iKeLV9A58Q7MlUoYXt X-Received: by 2002:a17:902:e88a:b0:1dc:abe6:238 with SMTP id w10-20020a170902e88a00b001dcabe60238mr5626632plg.15.1709052578083; Tue, 27 Feb 2024 08:49:38 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709052578; cv=pass; d=google.com; s=arc-20160816; b=spV43jcZ4ngxxFPp2eboZ9OslWjP4B6Rp9YZcYigQAERko191MD6MNJaQcMd1pDS8Y Age2GMRo730KgZVHeQbqMm+IZm1ZYO59WMakfcllXqLiHhjra1Su1fRgekyJ4hC+WUUU FpBuBmIlFExtZylewEjYj5itYutOLSMbx+mL6FRAyb19+M7NjJ9SdNE0Zf4QKLAZANT7 6lzaLooG4aHCNaf1AaAJNKTQndo3KhrUkWbG2dIKs3EqPiLOEXG/QLF+fC1B8NNPUtjl wu2lXJ8Xq/16H5iIY6VYf8xnCwCgUPLzCTHysyanLxdyrKBoxTR4y7utnLlR5px7JX22 4Y4g== 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:user-agent:references:in-reply-to :message-id:date:cc:to:from:subject:dkim-signature; bh=dYIPOGqD/CKFk/Yo/OoXGLbCQOXBgG0fvxpGI3WX/8s=; fh=m3BpWLAONYEKdlzALB2w8yDVnxnPNGBx9VUtILVXH2U=; b=c233o8tsgqB1xZ/etmJP95kMfyz7i93voA7VQaJgOWLph2l/iWhcVfbMikhDEZ4XGt vyx/wR5U3jzdmIJw2aDNIvgLL8XX+WF5uNAifgXk6XYCAGTGlSP/y0v+0oGT+LZPJEfV 9aQYKNuxuIWYyz+VNC5EN/Mr2wU4UQEs9H22vvbD4CGA+65h45tK94zqfrFEMfQJxVvL B8/d6xLH5W8RhUwBFHrDRI5phVZ6R0B87pqD8HlyYQoZ9lhZ+LbOm8Hg8nOQd+x4h9CF iJn48FoMRW6ksyYwTBgYkM+Uj8tlRGcUh5KpPPkAZt4sCwR9XldZB2N1fvupiyRrNDj2 c8ew==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=HD90jN7Q; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-83648-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-83648-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id h18-20020a170902f71200b001da1d2ad541si1639385plo.572.2024.02.27.08.49.37 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Feb 2024 08:49:38 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-83648-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=HD90jN7Q; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-83648-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-83648-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 E0479283205 for ; Tue, 27 Feb 2024 16:49:37 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7F28E4DA0C; Tue, 27 Feb 2024 16:48:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="HD90jN7Q" Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.8]) (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 B02A8487A5; Tue, 27 Feb 2024 16:48:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.8 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709052536; cv=none; b=eDV9cmQ2MhkLLj/WUr1YD8E9h4Z7YZy1kc8b9xiHgL9SwVvkZK9z2E7p2z8Vu7sJtxijKqf78h/m+b8pJrcfV8q2WNYe9mlTxwm/JLimwq6/gQdj02MVh/nHaj0ShW7xMfzi2wauhoxaN3ka7kwmvzEj+4uEmyYnFzc0KdFHR1g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709052536; c=relaxed/simple; bh=6DJ8zNycy4XqO4nhbI6pzWZYZ6PRv0j+XW33IcU7Q/k=; h=Subject:From:To:Cc:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Yyp3TiitmmJQh95HTmKZoa6Jfbb0XRSlViOAgEvNt7ZEpk+YL3SW7u70DPPtkMNf78Vq96TxBlkaPQ9YYgj++hn4zWxxfuIP9Y57reSMFKvnjBadw7vF8BtV4N02FQY+s1V6wvNtQUNEA5G+wT+y+XAg4SNOBF8BelJxMZB4OLA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=HD90jN7Q; arc=none smtp.client-ip=192.198.163.8 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1709052535; x=1740588535; h=subject:from:to:cc:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=6DJ8zNycy4XqO4nhbI6pzWZYZ6PRv0j+XW33IcU7Q/k=; b=HD90jN7QyymSen/a5MM+f/Y+2wjP/BQi+UrWr3lnZDFdi6JDSY9EwTSn VOuFDfhG3QleuUtJlsLyaeFA8ONFA1SLhe5lMrpJNqZsAGYeWaMOglTb+ mfCenKCyPTexOZTiSoQyWFGoNX1WbCJyf1Sokgw2ctQwuIm219BvoszYu 3yQFkM8jQNuQWJNkx4GbAoHfkJ7r2QfFTLFduYe/Urt+N9JaFL4gk2tBH 5TIImFSLvEGW+rbN+vt/wj4op44jTJaKoOeC0wwZCP3ZaeFHxMqSU5hvq JmYTVtxFL9RlUcCGEJX6dJdf0xA9Sg7QJBFVZZfjm9uJQqKHvLG36vLnR g==; X-IronPort-AV: E=McAfee;i="6600,9927,10996"; a="20948825" X-IronPort-AV: E=Sophos;i="6.06,188,1705392000"; d="scan'208";a="20948825" Received: from orviesa006.jf.intel.com ([10.64.159.146]) by fmvoesa102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Feb 2024 08:48:54 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.06,188,1705392000"; d="scan'208";a="7536097" Received: from sshaik-mobl1.amr.corp.intel.com (HELO dwillia2-xfh.jf.intel.com) ([10.209.88.67]) by orviesa006-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Feb 2024 08:48:54 -0800 Subject: [PATCH 1/3] cleanup: Add cond_guard() to conditional guards From: Dan Williams To: torvalds@linux-foundation.org, peterz@infradead.org, gregkh@linuxfoundation.org Cc: Dave Jiang , Ira Weiny , Jonathan Cameron , "Fabio M. De Francesco" , Jonathan Cameron , Dave Jiang , linux-kernel@vger.kernel.org, linux-cxl@vger.kernel.org Date: Tue, 27 Feb 2024 08:48:53 -0800 Message-ID: <170905253339.2268463.9376907713092612237.stgit@dwillia2-xfh.jf.intel.com> In-Reply-To: <170905252721.2268463.6714121678946763402.stgit@dwillia2-xfh.jf.intel.com> References: <170905252721.2268463.6714121678946763402.stgit@dwillia2-xfh.jf.intel.com> User-Agent: StGit/0.18-3-g996c 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: 1792071515852197332 X-GMAIL-MSGID: 1792071515852197332 From: Fabio M. De Francesco 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. This macro can be called multiple times in the same scope. Cc: Dave Jiang Cc: Peter Zijlstra Suggested-by: Dan Williams Suggested-by: Ira Weiny Suggested-by: Jonathan Cameron Signed-off-by: Fabio M. De Francesco Reviewed-by: Jonathan Cameron Reviewed-by: Dave Jiang Signed-off-by: Dan Williams --- include/linux/cleanup.h | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/include/linux/cleanup.h b/include/linux/cleanup.h index c2d09bc4f976..602afb85da34 100644 --- a/include/linux/cleanup.h +++ b/include/linux/cleanup.h @@ -134,6 +134,19 @@ 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); + * + * This macro can be called multiple times in the same scope, for it + * declares unique instances of type 'name'. + * * 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 +178,13 @@ static inline class_##_name##_t class_##_name##ext##_constructor(_init_args) \ #define __guard_ptr(_name) class_##_name##_lock_ptr +#define __cond_guard(__unique, _name, _fail, args...) \ + CLASS(_name, __unique)(args); \ + if (!__guard_ptr(_name)(&__unique)) _fail; \ + else { } +#define cond_guard(_name, _fail, args...) \ + __cond_guard(__UNIQUE_ID(scope), _name, _fail, args) + #define scoped_guard(_name, args...) \ for (CLASS(_name, scope)(args), \ *done = NULL; __guard_ptr(_name)(&scope) && !done; done = (void *)1) From patchwork Tue Feb 27 16:48:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dan Williams X-Patchwork-Id: 207321 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:a81b:b0:108:e6aa:91d0 with SMTP id bq27csp2822526dyb; Tue, 27 Feb 2024 08:49:53 -0800 (PST) X-Forwarded-Encrypted: i=2; AJvYcCUggo7m98gR8IlHGduGZCjGJUJmXLCxJDwxleYmzZywvvGb47ATqqCOnBZBpsXXjpthUHggw+2S+vtynqAJWOHfnWNzMQ== X-Google-Smtp-Source: AGHT+IHe/5wgz8MrlQRq4YX/Oig/QQNP5y7hOjcQQP+mThVC74SNtGOlft0M4QL0shbg3oOCkZdU X-Received: by 2002:aa7:d80e:0:b0:566:d15:1458 with SMTP id v14-20020aa7d80e000000b005660d151458mr3244017edq.21.1709052593507; Tue, 27 Feb 2024 08:49:53 -0800 (PST) Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id r18-20020aa7d152000000b00565ff51668fsi844525edo.369.2024.02.27.08.49.53 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Feb 2024 08:49:53 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-83649-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@intel.com header.s=Intel header.b=C8VI4qwy; arc=fail (body hash mismatch); spf=pass (google.com: domain of linux-kernel+bounces-83649-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-83649-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=fail (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 26AB11F268E7 for ; Tue, 27 Feb 2024 16:49:53 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id CE4DE4EB34; Tue, 27 Feb 2024 16:49:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=intel.com header.i=@intel.com header.b="C8VI4qwy" Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.8]) (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 AEB5E4DA11; Tue, 27 Feb 2024 16:48:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.8 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709052542; cv=none; b=KdnjMZPFVsUhDTajleaucKbVXdBixZOZUi3Eicni3zdGPdlxkRZCQbtcrzJ1pkRAAWZ4BX0VuqWqwpFyHVo7v77BmwYxYsjZbI3EYPy2lJvM+cEoN14Cw30nOrs/ymSPpTTB6r+TN7Yswp0kMxPjXTDsL0BuVoSp8k2Khsy7f5Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709052542; c=relaxed/simple; bh=t/V8GK7RDHUwml/a0vpcMATYt5kJT3OlTetC2L+ouw4=; h=Subject:From:To:Cc:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=rfwXkm9Y0k2axf9zZstx0cWOYN6EY8naLrtCAukqBUBlevFiNDYID11c/BD1nA6CZ/kydlCWci1LaqnjKfE78bPiUvjTKl5rVhb5TETMvUqExsm1zbsoDNhQ18F9GnJ8xC9jzKl0Kgh24MNy6iPhPkLv9Y9CmVs0mH2ydlPMfp0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=C8VI4qwy; arc=none smtp.client-ip=192.198.163.8 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1709052540; x=1740588540; h=subject:from:to:cc:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=t/V8GK7RDHUwml/a0vpcMATYt5kJT3OlTetC2L+ouw4=; b=C8VI4qwy/r37qbvdSrK5gTmjjbXey5sgA+uPM0BKvD/dnGeSdeKq/8P2 xDUecFLSVExXRp32aONwVHv2AUohhJXUAKePyC0WLN9arRM+Ag4LH/zTb sneUMerhMNIBf7Bxt5k/8EJo2Hv3y2PL3FR5paBdF1P1lHTIioK8435Pr 28o8Xkmy6Et1IKXRA4nfxLtKCl6h52MriG34mc5nR501jKoWVZzQsmmi3 lasOTJbNTqe3bmYZ4qH64Yi+6Hxl5LeEZMGXg+j+IzDsCB4WZH5vRbCIo Pspzkg0+1jqhaKE+FTRA+GPfeLOBD2HV/puennYv305tPjgSR7rBsWy4a Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10996"; a="20948834" X-IronPort-AV: E=Sophos;i="6.06,188,1705392000"; d="scan'208";a="20948834" Received: from orviesa006.jf.intel.com ([10.64.159.146]) by fmvoesa102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Feb 2024 08:48:59 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.06,188,1705392000"; d="scan'208";a="7536108" Received: from sshaik-mobl1.amr.corp.intel.com (HELO dwillia2-xfh.jf.intel.com) ([10.209.88.67]) by orviesa006-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Feb 2024 08:49:00 -0800 Subject: [PATCH 2/3] cleanup: Introduce cond_no_free_ptr() From: Dan Williams To: torvalds@linux-foundation.org, peterz@infradead.org, gregkh@linuxfoundation.org Cc: Jonathan Cameron , linux-kernel@vger.kernel.org, linux-cxl@vger.kernel.org Date: Tue, 27 Feb 2024 08:48:59 -0800 Message-ID: <170905253897.2268463.13371523233762430828.stgit@dwillia2-xfh.jf.intel.com> In-Reply-To: <170905252721.2268463.6714121678946763402.stgit@dwillia2-xfh.jf.intel.com> References: <170905252721.2268463.6714121678946763402.stgit@dwillia2-xfh.jf.intel.com> User-Agent: StGit/0.18-3-g996c 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: 1792071532330850190 X-GMAIL-MSGID: 1792071532330850190 The no_free_ptr() helper cancels automatic cleanup for cases where assigning the pointer transfers ownership for freeing it. However, it gets awkward to use when multiple allocations need to be cancelled in response to one registration call. For example: 1/ name = kasprintf(...); 2/ res = kmalloc(...); 3/ res->name = name; 4/ rc = insert_resource(..., res); 5/ if (rc) return rc; no_free_ptr() cannot be used for 3 since insert_resource() does not cleanup on failure. no_free_ptr() could be used at 4, but if insert_resource() fails, the no_free_ptr() was premature. After 5 is when it is known that it is safe to free @res and @name. However, no_free_ptr() is awkward there as well because of __must_check(). The options are: * Just open code @res = NULL and @name = NULL, but that is a non-idiomatic way to use the cleanup helpers. * Introduce a no_free_ptr() variant that drops the __must_check, but that defeats the purpose of mandating that the caller understands that responsibility for freeing has been handed off. * Introduce a new helper that combines a condition check to supersede the __must_check of no_free_ptr() So, per that last option, line 5/ from the example becomes: 5/ cond_no_free_ptr(rc == 0, return rc, res, name); ..and that handles calling no_free_ptr() while also mandating the negative condition be handled. It is inspired by scoped_cond_guard() which also takes a statement for the negative condition case. Cc: Peter Zijlstra Cc: Jonathan Cameron Signed-off-by: Dan Williams --- include/linux/cleanup.h | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/include/linux/cleanup.h b/include/linux/cleanup.h index 602afb85da34..a6d593a60611 100644 --- a/include/linux/cleanup.h +++ b/include/linux/cleanup.h @@ -77,6 +77,28 @@ const volatile void * __must_check_fn(const volatile void *val) #define return_ptr(p) return no_free_ptr(p) +#define __cond_no_free_ptrs(p) ({__auto_type __always_unused __ptr = no_free_ptr(p);}) +#define __cond_no_free_ptrs1(p, ...) __cond_no_free_ptrs(p) +#define __cond_no_free_ptrs2(p, ...) \ + __cond_no_free_ptrs(p), __cond_no_free_ptrs1(__VA_ARGS__) +#define __cond_no_free_ptrs3(p, ...) \ + __cond_no_free_ptrs(p), __cond_no_free_ptrs2(__VA_ARGS__) + +/* + * When an object is built up by an amalgamation of multiple allocations + * each of those need to be cleaned up on error, but there are occasions + * where once the object is registered all of those cleanups can be + * cancelled. cond_no_free_ptr() arranges to call no_free_ptr() on all + * its arguments (up to 3) if @condition is true and runs @_fail + * otherwise (typically to return and trigger auto-cleanup). + */ +#define cond_no_free_ptr(condition, _fail, ...) \ + if (condition) { \ + CONCATENATE(__cond_no_free_ptrs, COUNT_ARGS(__VA_ARGS__)) \ + (__VA_ARGS__); \ + } else { \ + _fail; \ + } /* * DEFINE_CLASS(name, type, exit, init, init_args...): From patchwork Tue Feb 27 16:49:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dan Williams X-Patchwork-Id: 207322 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:a81b:b0:108:e6aa:91d0 with SMTP id bq27csp2822649dyb; Tue, 27 Feb 2024 08:50:07 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUfkfEGVK1MC+v87wN7Egg5JTi3GUXZEG1+kH56RF7pDNs06nDawfeu3rxaj4M1fELPtlQ+Sk9bANQu9ncRZJ2LjVfP4g== X-Google-Smtp-Source: AGHT+IGfDhFU9MF81y+RvYIBVy+HdmIPiC0Oc94sosiO7nhZVs17rZgR4uoXJll+1hj1ffGGIY92 X-Received: by 2002:a9d:7cc4:0:b0:6e4:80c3:af76 with SMTP id r4-20020a9d7cc4000000b006e480c3af76mr10259774otn.17.1709052606755; Tue, 27 Feb 2024 08:50:06 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709052606; cv=pass; d=google.com; s=arc-20160816; b=KT/0BNjQfI7KtyiX4ylspb62hmCNHVsfagk7OraiEh+Y4EphvB+mEodb+9X/0hr8t/ qs2Zm+IPJvkrCTc7abvnO/Reb8h8thnf/5mMSaBh8TW32g6UEQdSNQXd7zdz/jPW9RrC QF81f3B8pHV/IaeSfzqmr/x4LVqDYsT9lcbyyWRdvrPIvy1YKVGLt7GHGyfyjTksHq0v 6nkWEy++0vvBnFl+zIdqbJ04+VjHWUd+R6cn3ol6Jh9iHYPH+ZwkY6SnNxcGecWFC/9o Fqh0NNOYq59fUZyNtOJ5Dghiuiy/wDo/kbHFfvLKQuUPo4uos7x2gcxGVpucZP9763h9 n90g== 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:user-agent:references:in-reply-to :message-id:date:cc:to:from:subject:dkim-signature; bh=QxkknGacMFhDHeGPAHuUP/JH0dbE4m2kP6y6jjyYCsY=; fh=J+y8fwVqf69pkg13Fh9pTaAnYpohIzxQcHuTScC561I=; b=xDwv7d4JhtwcVl2gbyG6UZXtuoUFaTf1uM2lFEnXHSGgcFSUoJpyOf7GTyvzHQ+aqE RyvmbS+T5QQ/Rh8a9Yj5Rna+7VZhfRrZCsTlCGlUoXrTzg9BCcMkKBCJQiIPw7yhtT02 FCer8PuIcx1i9bDlMC3LXJhZFr7WNJ/uH1eQrdOoahPPEPYvIuSGx8ILzdGtENbad97N +nqGPltl8XMe8WNWxIeWyV89c/pcfouk9rO3Ht10WvaF//sdq8s7bHiDJCdtszvwcGUX Tj/lSWOjiMnx+IywSlJNkhXH2zusYYGpqWOi5a1zANzQYBhYzMT7zFNLBUOTOIWm9wKH sa7Q==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="mixHy/oz"; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-83650-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-83650-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id 14-20020a63174e000000b005d66232593dsi5640544pgx.868.2024.02.27.08.50.06 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Feb 2024 08:50:06 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-83650-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="mixHy/oz"; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-83650-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-83650-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 505BE2861AC for ; Tue, 27 Feb 2024 16:50:06 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 802F94EB4F; Tue, 27 Feb 2024 16:49:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="mixHy/oz" Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.8]) (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 73DD74EB43; Tue, 27 Feb 2024 16:49:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.8 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709052547; cv=none; b=lpMbV7OdAbvYSC1kAB1s7tUbgOVphC2gWcM3M+Cx+81Tnw3CZPb17U6PEOtzR6iMC60Clb8DwXcmFSiHKcENBHHbzBBa9neX9L1Kcxhg+vV1MU1FHgbnGOwsvsc7kbEsuFsLzgrJPZwUN9xuiWEIz4Z0VVIa0rZrC8iQ2kIP270= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709052547; c=relaxed/simple; bh=uM/i0Rm9q3ig1FjMY9O1+2SHrbYFfbfJXmzB9UyHcMg=; h=Subject:From:To:Cc:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=rxatextoHO4lzHWBCV9l/r4EIDXFx345nJ+nfF696rpp/f8JvUbaaX9nhQ/qlIQoVtPzYED9UDFPaEAxB71WQoJ4s0KmpMv4UAWy/7c904aQcjRnIhyIA0heI8rhpEhai+GytETYMwbvaSGUZgg3k1C/RA8tDC/rQB+AwnmXA4E= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=mixHy/oz; arc=none smtp.client-ip=192.198.163.8 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1709052545; x=1740588545; h=subject:from:to:cc:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=uM/i0Rm9q3ig1FjMY9O1+2SHrbYFfbfJXmzB9UyHcMg=; b=mixHy/oz9ftJbGz9Kc9p3G02My0GRO1kXjM/b1mU2bKlklqCvnYUYlNN jRB1VSZ6+8V7IGdg09KIFfPG4O6b0jQ0CIrWtdy4S+SZqNYq8Lxp2hCXU VDDR7WEyAUEa1jJXGEukffFRP7WsfIwClDLkeQEamjui21uoJ7RyDTTkH nlS5Gex+bV2NYEck0uW6wQdQQUUGqk2HPAvGcla4JzJz5BAjUbJbpaK40 osxy5l+jwYyO8gyHNC6gMudixGJFzvZfadfWHuz4UY6YzzcQt+nsTEEPW wFSUVi4xFc93aOVAUNcCPlylyV35+YbMdh+Va4sOM4vkt5+zF5ZKNiCu+ Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10996"; a="20948840" X-IronPort-AV: E=Sophos;i="6.06,188,1705392000"; d="scan'208";a="20948840" Received: from orviesa006.jf.intel.com ([10.64.159.146]) by fmvoesa102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Feb 2024 08:49:05 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.06,188,1705392000"; d="scan'208";a="7536117" Received: from sshaik-mobl1.amr.corp.intel.com (HELO dwillia2-xfh.jf.intel.com) ([10.209.88.67]) by orviesa006-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Feb 2024 08:49:05 -0800 Subject: [PATCH 3/3] cxl/region: Use cond_guard() in show_targetN() From: Dan Williams To: torvalds@linux-foundation.org, peterz@infradead.org, gregkh@linuxfoundation.org Cc: Ira Weiny , Dave Jiang , Ira Weiny , Jonathan Cameron , "Fabio M. De Francesco" , linux-kernel@vger.kernel.org, linux-cxl@vger.kernel.org Date: Tue, 27 Feb 2024 08:49:04 -0800 Message-ID: <170905254443.2268463.935306988251313983.stgit@dwillia2-xfh.jf.intel.com> In-Reply-To: <170905252721.2268463.6714121678946763402.stgit@dwillia2-xfh.jf.intel.com> References: <170905252721.2268463.6714121678946763402.stgit@dwillia2-xfh.jf.intel.com> User-Agent: StGit/0.18-3-g996c 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: 1792071546374792413 X-GMAIL-MSGID: 1792071546374792413 From: Fabio M. De Francesco 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: Dave Jiang Reviewed-by: Ira Weiny Reviewed-by: Jonathan Cameron Signed-off-by: Fabio M. De Francesco Signed-off-by: Dan Williams --- 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 ce0e2d82bb2b..704102f75c14 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"); - else - rc = sysfs_emit(buf, "%s\n", dev_name(&cxled->cxld.dev)); -out: - up_read(&cxl_region_rwsem); + return sysfs_emit(buf, "\n"); - return rc; + return sysfs_emit(buf, "%s\n", dev_name(&cxled->cxld.dev)); } static int match_free_decoder(struct device *dev, void *data)