From nobody Sun Dec 14 21:42:31 2025 Received: from mblankhorst.nl (lankhorst.se [141.105.120.124]) (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 B4779218ADB for ; Tue, 4 Feb 2025 13:21:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=141.105.120.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738675319; cv=none; b=eL4tFMR2kqZ5d0szLzFOqGbsfxA7LtR1LtgMsEcpzUUI8ITJWVG9Nn51tsDq8vDkIvcOsxN3AHPLCf1kjOdxeTtMWun4ACywZtpJ5OSmRb+oFDabIFKF2B9QPNs2t8nxMLe1QLCxbOaFJwqdDZ57evkjyvs3HkqxUDNpGmT6J8w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738675319; c=relaxed/simple; bh=lLY7isZ2WSpKh9qUL9fRA9iy+6gQ/Zd09FdN8Dnhj9c=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=EmgziAKrvMmXvF8MrMrpLtC1a3FkVPeUx9Ag0D8sWVNEM0Gt8Q4CEC0vf8McvdQkg5JERmE4ejbf9+18I502OIXvfaAOJTRgpBFOJxVuWsB2N6Is/fM736zpiC2hVsyOlGsagg7LsMXNOPkl2IsnqmWQirtqNtpi1prMIkM4GnU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=lankhorst.se; spf=none smtp.mailfrom=mblankhorst.nl; arc=none smtp.client-ip=141.105.120.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=lankhorst.se Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=mblankhorst.nl From: Maarten Lankhorst To: intel-xe@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-resent-to-correct-ml 1/8] header/cleanup.h: Add _init_args to DEFINE_LOCK_GUARD_1(_COND) Date: Tue, 4 Feb 2025 14:22:30 +0100 Message-ID: <20250204132238.162608-2-dev@lankhorst.se> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250204132238.162608-1-dev@lankhorst.se> References: <20250204132238.162608-1-dev@lankhorst.se> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 stru= ct 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 ne= ed 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 :=3D 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. */ =20 #define __DEFINE_UNLOCK_GUARD(_name, _type, _unlock, ...) \ @@ -381,8 +385,8 @@ static inline void *class_##_name##_lock_ptr(class_##_n= ame##_t *_T) \ } =20 =20 -#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 =3D { .lock =3D l }, *_T =3D &_t; \ _lock; \ @@ -398,23 +402,27 @@ static inline class_##_name##_t class_##_name##_const= ructor(void) \ return _t; \ } =20 -#define DEFINE_LOCK_GUARD_1(_name, _type, _lock, _unlock, ...) \ +#define __DO_DEFINE_LOCK_GUARD_1(_name, _type, _lock, _unlock, _lock_membe= rs, _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__) =20 #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) =20 -#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 =3D { .lock =3D l }, *_T =3D &_t;\ if (_T->lock && !(_condlock)) _T->lock =3D 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); } =20 --=20 2.47.1