From patchwork Tue Oct 1 22:30:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Lechner X-Patchwork-Id: 13818815 Received: from mail-oi1-f174.google.com (mail-oi1-f174.google.com [209.85.167.174]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5728A1CF2A2 for ; Tue, 1 Oct 2024 22:30:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727821850; cv=none; b=BYcvS/crNuWLF1CTgim+Mx2WzasojL+XOR/2PKHcK9vM95AqCaIoGImqMmTuxf+iL1tv4EjaFlCD7+UQOFLWMxT3JMWpR6qM4iC4XcI/Myr00YsJosSwCHkB6tMN8ZBEUKi+nnAzEbZEVyzvnrTb419Djt1CbUF1S9iYvmmyRtA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727821850; c=relaxed/simple; bh=JGTFl5VoZdw/HMSx+xMjDWjUeDuNOPJLXAn6T5BdJYg=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=swLOsKtOgZHQvWu3j57xyzKsug2SKuCiAZ8JQdbW63IccUqRJVjutF9TODuTGmRSn1sHq74kvjN4f6C4jPylo1WDUghCtWA+ZY5iGMwKrW85kdx7Ikjc3YvjD6DWsANvdKycwRE+fLK9IuyQ1r7FJxtp+egfDcrGe6gC6HK1ka4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com; spf=pass smtp.mailfrom=baylibre.com; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b=npXPjI8S; arc=none smtp.client-ip=209.85.167.174 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=baylibre.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b="npXPjI8S" Received: by mail-oi1-f174.google.com with SMTP id 5614622812f47-3e045525719so3798849b6e.2 for ; Tue, 01 Oct 2024 15:30:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1727821847; x=1728426647; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=zQt8/ibZGmr7UQAMTn5RryHz2eZfh/xc9yurTJPgOaA=; b=npXPjI8SwU0So5l8m4ZN22qFUG5G5lrl0EmQJvjDGAMv1HD4W1YIGL+lxt7dk8ORkr +x5ztrLQI6JfDh/TTWOXO3gyT7ZHhRSFeoMG2dkmkDgv72S/JCG9NjYfPsP0cQEYjYcJ rjn4WFgKBtvDIHMN2h2LwDukfBnUbSTI9RwODUaA/6qu8vl6pfbSzW9WybkCdit2LAAs vcGfpZsqxryP/Kx8tkWrUipLgj8sQk7SYTYbqDTadMjcHjsoSwBGoDZd2x5daE18Zazl 55EG1RBU01GJqCAFBXuV5KmkEXNyS7cwC79go7xvJoWl3Q/JIESh0DIk/f/cJx38BB/d 8Dug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727821847; x=1728426647; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=zQt8/ibZGmr7UQAMTn5RryHz2eZfh/xc9yurTJPgOaA=; b=cZn940lxJWAwuMMqeC5nJE0k3lv0K+JWrbogM1KVAExYo7oSjzljPvShuGwjUj6OgK 7U8qBf7iw/JYjmgibubAzY5MTmBziNWOatHiyXyddOyKr6M6u1jVfXzR+AiSJannfIpC Dd6aH3Rc1Rk2eDJ1l3TGn1GZboHPIQGMWY8NkDNHM1qtdXG4+Ych3FrU/MpSOYqEeoGH qjlKtAnGUJWqL+4WLwmzdjHDPql9N3liUqNqyIflWMfl6xPoM6LKB3N0W2c5OLVC53rh ZWITcgcz1f2c093R7eFKn5YkCCPDbDu5/mbuIdqYGezdn+FMohO0TmGjpEXzQ4A2/izs j5Ag== X-Forwarded-Encrypted: i=1; AJvYcCXguzRy7X8EBAetczWXa5YEIzNro335ZND7qmAP9Nxdi8iU1Jjl6OzSuzMPkY2Bl2YXqKSUSshTI74=@vger.kernel.org X-Gm-Message-State: AOJu0YzRWeXmv+8R0uXHEqbKnWizmPYLwGiSJg4uqPdaolpyvrJ50DNV kLvuYLOc79oSxYykOCnF76VzLx+6TZc7oWfuSHgQOfx4vwsihzdVjCENP7XYDWM= X-Google-Smtp-Source: AGHT+IGhvVHHB1hJmRFs6zxZiSrw/UPrPWpHoFtdU1cOKU7mjPFozjX8w0AobKNYjmXrmqNGa3i2Pg== X-Received: by 2002:a05:6808:1493:b0:3e3:b10e:c2c7 with SMTP id 5614622812f47-3e3b41879eemr1066223b6e.35.1727821847469; Tue, 01 Oct 2024 15:30:47 -0700 (PDT) Received: from [127.0.1.1] (ip98-183-112-25.ok.ok.cox.net. [98.183.112.25]) by smtp.gmail.com with ESMTPSA id 006d021491bc7-5e770eaa056sm3002097eaf.31.2024.10.01.15.30.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Oct 2024 15:30:46 -0700 (PDT) From: David Lechner Date: Tue, 01 Oct 2024 17:30:18 -0500 Subject: [PATCH 1/3] cleanup: add conditional guard helper Precedence: bulk X-Mailing-List: linux-cxl@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20241001-cleanup-if_not_cond_guard-v1-1-7753810b0f7a@baylibre.com> References: <20241001-cleanup-if_not_cond_guard-v1-0-7753810b0f7a@baylibre.com> In-Reply-To: <20241001-cleanup-if_not_cond_guard-v1-0-7753810b0f7a@baylibre.com> To: Peter Zijlstra , Dan Williams , Linus Torvalds , Jonathan Cameron Cc: =?utf-8?q?Nuno_S=C3=A1?= , Michael Hennerich , linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, linux-cxl@vger.kernel.org, David Lechner X-Mailer: b4 0.14.1 Add a new if_not_cond_guard() macro to cleanup.h for handling conditional guards such as mutext_trylock(). This is more ergonomic than scoped_cond_guard() for most use cases. Instead of hiding the error handling statement in the macro args, it works like a normal if statement and allow the error path to be indented while the normal code flow path is not indented. And it avoid unwanted side-effect from hidden for loop in scoped_cond_guard(). Signed-off-by: David Lechner Signed-off-by: Fabio M. De Francesco Reviewed-by: Dan Williams --- include/linux/cleanup.h | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/include/linux/cleanup.h b/include/linux/cleanup.h index 038b2d523bf8..682bb3fadfc9 100644 --- a/include/linux/cleanup.h +++ b/include/linux/cleanup.h @@ -273,6 +273,10 @@ static inline class_##_name##_t class_##_name##ext##_constructor(_init_args) \ * an anonymous instance of the (guard) class, not recommended for * conditional locks. * + * if_not_cond_guard(name, args...) { }: + * convenience macro for conditional guards that calls the statement that + * follows only if the lock was not acquired (typically an error return). + * * 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 @@ -304,6 +308,13 @@ static inline class_##_name##_t class_##_name##ext##_constructor(_init_args) \ #define __guard_ptr(_name) class_##_name##_lock_ptr +#define __if_not_cond_guard(_name, _id, args...) \ + CLASS(_name, _id)(args); \ + if (!__guard_ptr(_name)(&_id)) + +#define if_not_cond_guard(_name, args...) \ + __if_not_cond_guard(_name, __UNIQUE_ID(guard), args) + #define scoped_guard(_name, args...) \ for (CLASS(_name, scope)(args), \ *done = NULL; __guard_ptr(_name)(&scope) && !done; done = (void *)1)