Received: by 2002:a05:7412:d1aa:b0:fc:a2b0:25d7 with SMTP id ba42csp577656rdb; Mon, 29 Jan 2024 11:01:18 -0800 (PST) X-Google-Smtp-Source: AGHT+IGJGrNXF+q8zzqdMLELLDQ34jnacaj88zikzy8GrMvfgmSD3rcbpbAXijQxofQ1jS8yEDiY X-Received: by 2002:a17:902:e809:b0:1d7:3100:9f8a with SMTP id u9-20020a170902e80900b001d731009f8amr6871811plg.43.1706554878323; Mon, 29 Jan 2024 11:01:18 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706554878; cv=pass; d=google.com; s=arc-20160816; b=mrLF9EiLJgmycTCE9p/SrkkWk+4e/Qqrqc00JU+nmVnT3z5A2xChxOX5Hqn5ubBDE2 rRJS/aBiQKx+gaVNIyezWvn+0UaFzSZYc9gSW+R2NJ1lgdPcPLm/SjorTs7JBOXQEBC7 QkFLlZLbCx+PCXTarYNA5TkypOZ+WhmPw4Ihfx0glwAK7ZcuSz/sv5MU+1bnndc/0wfa B16362BH//6LFqQM/bYjzeQAyjkmRZBEtcpFzG2rH0jVCQlaFHq6eeUoMsBCLQhor+7o HplW7GkASLtf3p4gY6gXAF/V9TNPaTcGCjiV+Jeri1DfNgWaEHWyISD1/EHoLZubtxFn wPYA== 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=vvOOxVw0SPVZJQ2WE3A4z4JQfzWUce8OKkYJ5cp0pDo=; fh=GrBwOHLB7EHEQSs6t0XwLoHd4RKAbGScgfFQM/jONsE=; b=kdn8zCBeIU88KH9OJqIrJiIxFR1q/GUUV4xwl78gErMVWAG0HNWREPWJkq1b/QEiA2 bPl+2vlad4SJYzlsBb5Oho7f5AWOc8MzwfQHV9U5vt0dVTdauhOpLeN3Lrg64dM5xo2n s5zGoXvU8ofDfyIRMIr9QMYOnAOKT4Pk23LDca7E00U2vt6WTAsThddmGKw48nmKsRiW 6YlfCGJcl0I/EiBtlp5CEF4FDeoi/94kyx8Wpjw70ncQlZDizExgvs0wwKIoLmlhQ37G kzudpyWzvaVQ8t8Pg+duEKzQKJggR4bNnYK24BX7Uu0K0vOobKzG4CBIQhD8wnXndV4A tw/w== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=Uz4fF7tL; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-43380-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-43380-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id d14-20020a170903230e00b001d8e91b8b62si1755824plh.617.2024.01.29.11.01.18 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 29 Jan 2024 11:01:18 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-43380-linux.lists.archive=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=Uz4fF7tL; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-43380-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-43380-linux.lists.archive=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 EA79728484E for ; Mon, 29 Jan 2024 19:01:17 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id F041031755; Mon, 29 Jan 2024 19:01:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="Uz4fF7tL" Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.88]) (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 750C4101CF for ; Mon, 29 Jan 2024 19:01:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.55.52.88 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706554871; cv=none; b=JYe1wqAcrMsGuo7FGhPPjOOAJFYgrSgBa3Gy0bSVYIXKgZv2T6mBCNwg14ozI4cfAXpDpazzYDxINj9UiuBnM3v3ofxUZhP0Mko9HA2/jE30vIbuJQ/rkRH4ZUqJNPxV1lrIoauxO1QEF6q8fbiVY3C7mmVR2kkUZ6bihebsaYU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706554871; c=relaxed/simple; bh=EGwiQ4p1vYieugVbWg78iLa7GA3A68534GyysP3s9mI=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=Cr4TRMRju7YL0hzELVc0bMAqqetpvmoxLrA5v5UHGzWVQUFWcEI+OfOfS/TaFOCk8QvVrN6HrSVPiQCp5Zy74x1K+4/a3xcU1JuFGxle7xa1e4OgMYfw9QzHYqlwOTAjgowZ/6vzabG6mzsJCqxXRHDj49/ZnQA3hybpDXpKfZ8= 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=Uz4fF7tL; arc=none smtp.client-ip=192.55.52.88 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=1706554869; x=1738090869; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=EGwiQ4p1vYieugVbWg78iLa7GA3A68534GyysP3s9mI=; b=Uz4fF7tL7foOnKUjtQ39nsL6GqkudEttDVvslhejqSfOCr/9VBzG+YGX cKgxwU97BC7D1029etFO88nclt3O8ESfKChwwrh1orE0GReXKabCayGG6 zInSgfjZ63XPWStSzLqFKg5G5Yck9cP/TCPVDtF/gNZPmO/N7meTSZNmO A59dHbG8TZqcbcHFwUGzi+Nnv3rrdpgCq2VDF1mAksDEyfQell1s4ELoH ZicRvbhWze1icrRekkl6m5KILgiLUzRiEztYoJd1lcOWBLVL5mQJdyaPk rLYixsDPREaysIXoxmrdnE8nt5y/rWZHKk71n5R0aizOOXzgrV6SO6Qnd w==; X-IronPort-AV: E=McAfee;i="6600,9927,10968"; a="434233556" X-IronPort-AV: E=Sophos;i="6.05,227,1701158400"; d="scan'208";a="434233556" Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Jan 2024 11:01:08 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10968"; a="858194431" X-IronPort-AV: E=Sophos;i="6.05,227,1701158400"; d="scan'208";a="858194431" Received: from fdefranc-mobl3.ger.corp.intel.com (HELO fdefranc-mobl3.intel.com) ([10.213.21.23]) by fmsmga004-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Jan 2024 11:01:06 -0800 From: "Fabio M. De Francesco" To: linux-kernel@vger.kernel.org Cc: "Fabio M. De Francesco" , Peter Zijlstra , Dan Williams , Ira Weiny Subject: [RFC PATCH] cleanup: Add cond_guard() to conditional guards Date: Mon, 29 Jan 2024 20:00:55 +0100 Message-ID: <20240129190100.329791-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 Content-Transfer-Encoding: 8bit Add cond_guard() to conditional guards. cond_guard() stores the return value from _interruptible(), _killable(), and _try versions of locks to a variable whose address is passed as the second parameter, so that the value can later be checked and an action be taken accordingly (e.g., calling printk() in case of failure). As the other guards, it avoids to open code the release of the lock after a goto to an 'out' label. This is an RFC at least for two reasons... 1) I'm not sure that this guard is needed unless we want to avoid the use of scoped_cond_guard() with its necessary indentation of the code in the successful path and/or we want to check the return value of the conditional lock. 2) By using cond_guard() it is not possible to release the lock before calling other functions from the current one. The lock is held until the current function exits. This is not always desirable. The changes for cxl/region are only added to show a possible use case for cond_guard(). Cc: Peter Zijlstra Cc: Dan Williams Suggested-by: Ira Weiny Signed-off-by: Fabio M. De Francesco --- drivers/cxl/core/region.c | 7 ++----- include/linux/cleanup.h | 9 +++++++++ 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/drivers/cxl/core/region.c b/drivers/cxl/core/region.c index 0f05692bfec3..4a72a8d161f0 100644 --- a/drivers/cxl/core/region.c +++ b/drivers/cxl/core/region.c @@ -668,15 +668,14 @@ 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; 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]; @@ -684,8 +683,6 @@ static size_t show_targetN(struct cxl_region *cxlr, char *buf, int pos) rc = sysfs_emit(buf, "\n"); else rc = sysfs_emit(buf, "%s\n", dev_name(&cxled->cxld.dev)); -out: - up_read(&cxl_region_rwsem); return rc; } diff --git a/include/linux/cleanup.h b/include/linux/cleanup.h index c2d09bc4f976..550d21a425d3 100644 --- a/include/linux/cleanup.h +++ b/include/linux/cleanup.h @@ -134,6 +134,11 @@ 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, _retp, args...): + * this assigns *_retp with the return values from conditional locks like + * down_read_trylock() or mutex_lock_interruptible(). *_retp can then be + * checked and actions can be taken accordingly. + * * 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 +170,10 @@ 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); \ + *_ret = (!__guard_ptr(_name)(&scope)) + #define scoped_guard(_name, args...) \ for (CLASS(_name, scope)(args), \ *done = NULL; __guard_ptr(_name)(&scope) && !done; done = (void *)1) -- 2.43.0