From patchwork Tue Feb 4 12:49:02 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Maarten Lankhorst X-Patchwork-Id: 13959098 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 2407DC02196 for ; Tue, 4 Feb 2025 12:48:45 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 8549E10E339; Tue, 4 Feb 2025 12:48:40 +0000 (UTC) Received: from mblankhorst.nl (lankhorst.se [141.105.120.124]) by gabe.freedesktop.org (Postfix) with ESMTPS id A594C10E339; Tue, 4 Feb 2025 12:48:38 +0000 (UTC) From: Maarten Lankhorst To: intel-gfx@lists.freedesktop.org Cc: dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, Maarten Lankhorst , Ingo Molnar , David Lechner , Peter Zijlstra , Will Deacon , Waiman Long , Boqun Feng Subject: [PATCH 1/8] header/cleanup.h: Add _init_args to DEFINE_LOCK_GUARD_1(_COND) Date: Tue, 4 Feb 2025 13:49:02 +0100 Message-ID: <20250204124909.158315-2-dev@lankhorst.se> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250204124909.158315-1-dev@lankhorst.se> References: <20250204124909.158315-1-dev@lankhorst.se> MIME-Version: 1.0 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" This makes it possible to use the lock guards for guards that need extra arguments. I've been attempting to add a guard to xe_force_wake handling, but that required an extra argument specifying the domain. For nested spinlock handling, it could also be beneficial to be able to do something like this. For example: DEFINE_LOCK_GUARD_1_COND(spinlock_irqsave, _nested, spin_lock_irqsave_nested(_T->lock, _T->flags, nest), unsigned nest); guard(spinlock_irqsave_nested, &lock, SINGLE_DEPTH_NESTING); The first optional argument in DEFINE_LOCK_GUARD_1 is now used for the struct members, the remainder goes to init_args to allow the same usage in the base case.. I'm abusing the preprocessor to add an extra meaning to the first optional argument is done by creating a __DO_DEFINE_LOCK_GUARD_1, and passing __VA_ARGS__ not ##__VA_ARGS__ to it to ensure _struct_members is empty when not passed explicitly. Cc: Ingo Molnar Cc: David Lechner Cc: Peter Zijlstra Cc: Will Deacon Cc: Waiman Long Cc: Boqun Feng --- include/linux/cleanup.h | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/include/linux/cleanup.h b/include/linux/cleanup.h index ec00e3f7af2b3..dbaf02447f206 100644 --- a/include/linux/cleanup.h +++ b/include/linux/cleanup.h @@ -349,19 +349,23 @@ _label: \ * locks that don't have a native type (eg. RCU, preempt) or those that need a * 'fat' pointer (eg. spin_lock_irqsave). * - * DEFINE_LOCK_GUARD_0(name, lock, unlock, ...) - * DEFINE_LOCK_GUARD_1(name, type, lock, unlock, ...) - * DEFINE_LOCK_GUARD_1_COND(name, ext, condlock) + * DEFINE_LOCK_GUARD_0(name, lock, unlock, _lock_members...) + * DEFINE_LOCK_GUARD_1(name, type, lock, unlock, (opt)_lock_members, _init_args...) + * DEFINE_LOCK_GUARD_1_COND(name, ext, condlock, _init_args...) * * will result in the following type: * * typedef struct { * type *lock; // 'type := void' for the _0 variant - * __VA_ARGS__; + * _lock_members; // use ; as separator to add multiple members * } class_##name##_t; * * As above, both _lock and _unlock are statements, except this time '_T' will * be a pointer to the above struct. + * + * For DEFINE_LOCK_GUARD_1 and DEFINE_LOCK_GUARD_1_COND, it adds all + * _init_args as local variables available to the lock statement. + * They need to be passed to all guard() functions as extra argument. */ #define __DEFINE_UNLOCK_GUARD(_name, _type, _unlock, ...) \ @@ -381,8 +385,8 @@ static inline void *class_##_name##_lock_ptr(class_##_name##_t *_T) \ } -#define __DEFINE_LOCK_GUARD_1(_name, _type, _lock) \ -static inline class_##_name##_t class_##_name##_constructor(_type *l) \ +#define __DEFINE_LOCK_GUARD_1(_name, _type, _lock, ...) \ +static inline class_##_name##_t class_##_name##_constructor(_type *l, ##__VA_ARGS__) \ { \ class_##_name##_t _t = { .lock = l }, *_T = &_t; \ _lock; \ @@ -398,23 +402,27 @@ static inline class_##_name##_t class_##_name##_constructor(void) \ return _t; \ } -#define DEFINE_LOCK_GUARD_1(_name, _type, _lock, _unlock, ...) \ +#define __DO_DEFINE_LOCK_GUARD_1(_name, _type, _lock, _unlock, _lock_members, _init_args...) \ __DEFINE_CLASS_IS_CONDITIONAL(_name, false); \ -__DEFINE_UNLOCK_GUARD(_name, _type, _unlock, __VA_ARGS__) \ -__DEFINE_LOCK_GUARD_1(_name, _type, _lock) +__DEFINE_UNLOCK_GUARD(_name, _type, _unlock, _lock_members) \ +__DEFINE_LOCK_GUARD_1(_name, _type, _lock, ##_init_args) + +/* Call __DO_DEFINE_LOCK_GUARD_1 here because of the 2 optional arguments */ +#define DEFINE_LOCK_GUARD_1(_name, _type, _lock, _unlock, ...) \ + __DO_DEFINE_LOCK_GUARD_1(_name, _type, _lock, _unlock, __VA_ARGS__) #define DEFINE_LOCK_GUARD_0(_name, _lock, _unlock, ...) \ __DEFINE_CLASS_IS_CONDITIONAL(_name, false); \ __DEFINE_UNLOCK_GUARD(_name, void, _unlock, __VA_ARGS__) \ __DEFINE_LOCK_GUARD_0(_name, _lock) -#define DEFINE_LOCK_GUARD_1_COND(_name, _ext, _condlock) \ +#define DEFINE_LOCK_GUARD_1_COND(_name, _ext, _condlock, ...) \ __DEFINE_CLASS_IS_CONDITIONAL(_name##_ext, true); \ EXTEND_CLASS(_name, _ext, \ ({ class_##_name##_t _t = { .lock = l }, *_T = &_t;\ if (_T->lock && !(_condlock)) _T->lock = NULL; \ _t; }), \ - typeof_member(class_##_name##_t, lock) l) \ + typeof_member(class_##_name##_t, lock) l, ##__VA_ARGS__) \ static inline void * class_##_name##_ext##_lock_ptr(class_##_name##_t *_T) \ { return class_##_name##_lock_ptr(_T); }