From: Kees Cook <keescook@chromium.org>
To: Peter Zijlstra <peterz@infradead.org>
Cc: torvalds@linux-foundation.org, gregkh@linuxfoundation.org,
pbonzini@redhat.com, linux-kernel@vger.kernel.org,
ojeda@kernel.org, ndesaulniers@google.com, mingo@redhat.com,
will@kernel.org, longman@redhat.com, boqun.feng@gmail.com,
juri.lelli@redhat.com, vincent.guittot@linaro.org,
dietmar.eggemann@arm.com, rostedt@goodmis.org,
bsegall@google.com, mgorman@suse.de, bristot@redhat.com,
vschneid@redhat.com, paulmck@kernel.org, frederic@kernel.org,
quic_neeraju@quicinc.com, joel@joelfernandes.org,
josh@joshtriplett.org, mathieu.desnoyers@efficios.com,
jiangshanlai@gmail.com, qiang1.zhang@intel.com,
rcu@vger.kernel.org, tj@kernel.org, tglx@linutronix.de
Subject: Re: [RFC][PATCH 1/2] locking: Introduce __cleanup__ based guards
Date: Fri, 26 May 2023 10:05:41 -0700 [thread overview]
Message-ID: <202305260939.D33FE435D2@keescook> (raw)
In-Reply-To: <20230526151946.960406324@infradead.org>
On Fri, May 26, 2023 at 05:05:50PM +0200, Peter Zijlstra wrote:
> Use __attribute__((__cleanup__(func))) to buid various guards:
>
> - ptr_guard()
> - void_guard() / void_scope()
> - lock_guard() / lock_scope()
> - double_lock_guard() / double_lock_scope()
>
> Where the _guard thingies are variables with scope-based cleanup and
> the _scope thingies are basically do-once for-loops with the same.
This makes things much easier to deal with, rather than forcing loops
into separate functions, etc, and hoping to get the cleanup right.
>
> The CPP is rather impenetrable -- but I'll attempt to write proper
> comments if/when people think this is worth pursuing.
Yes please. Comments would help a lot. I was scratching my head over _G
for a bit before I realized what was happening. :)
>
> Actual usage in the next patch
>
> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
> ---
> include/linux/compiler_attributes.h | 2
> include/linux/irqflags.h | 7 ++
> include/linux/guards.h | 118 ++++++++++++++++++++++++++++++++++++
> include/linux/mutex.h | 5 +
> include/linux/preempt.h | 4 +
> include/linux/rcupdate.h | 3
> include/linux/sched/task.h | 2
> include/linux/spinlock.h | 23 +++++++
> 8 files changed, 164 insertions(+)
>
> --- a/include/linux/compiler_attributes.h
> +++ b/include/linux/compiler_attributes.h
> @@ -366,4 +366,6 @@
> */
> #define __fix_address noinline __noclone
>
> +#define __cleanup(func) __attribute__((__cleanup__(func)))
> +
> #endif /* __LINUX_COMPILER_ATTRIBUTES_H */
nitpick: sorting. This needs to be moved up alphabetically; the comment
at the start of the file says:
...
* This file is meant to be sorted (by actual attribute name,
* not by #define identifier). ...
> [...]
> +#define DEFINE_VOID_GUARD(_type, _Lock, _Unlock, ...) \
> +typedef struct { \
> + __VA_ARGS__ \
> +} void_guard_##_type##_t; \
> + \
> [...]
> +DEFINE_VOID_GUARD(irq, local_irq_disable(), local_irq_enable())
> +DEFINE_VOID_GUARD(irqsave,
> + local_irq_save(_G->flags),
> + local_irq_restore(_G->flags),
> + unsigned long flags;)
Yeah, good trick for defining 0-or-more members to the guard struct. I
expect the common cases to be 0 or 1, so perhaps move the final ";" to
after __VA_ARGS__ to avoid needing it in the DEFINEs? (And even in this
initial patch, there's only 1 non-empty argument...)
> [...]
> --- /dev/null
> +++ b/include/linux/guards.h
> @@ -0,0 +1,118 @@
> +/* SPDX-License-Identifier: GPL-2.0 */
> +#ifndef __LINUX_GUARDS_H
> +#define __LINUX_GUARDS_H
> +
> +#include <linux/compiler_attributes.h>
> +
> +/* Pointer Guard */
> +
> +#define DEFINE_PTR_GUARD(_type, _Type, _Put) \
> +typedef _Type *ptr_guard_##_type##_t; \
> +static inline void ptr_guard_##_type##_cleanup(_Type **_ptr) \
> +{ \
> + _Type *_G = *_ptr; \
> + if (_G) \
> + _Put(_G); \
> +}
*loud forehead-smacking noise* __cleanup with inlines! I love it!
> [...]
> +#define void_scope(_type) \
> + for (struct { void_guard_##_type##_t guard; bool done; } _scope \
> + __cleanup(void_guard_##_type##_cleanup) = \
> + { .guard = void_guard_##_type##_init() }; !_scope.done; \
> + _scope.done = true)
Heh, yes, that'll work for a forced scope, and I bet compiler
optimizations will collapse a bunch of this into a very clean execution
path.
> [...]
> +DEFINE_VOID_GUARD(preempt, preempt_disable(), preempt_enable())
> +DEFINE_VOID_GUARD(migrate, migrate_disable(), migrate_enable())
> [...]
> +DEFINE_VOID_GUARD(rcu, rcu_read_lock(), rcu_read_unlock())
> [...]
> +DEFINE_PTR_GUARD(put_task, struct task_struct, put_task_struct)
> [...]
It seems like there are some _really_ common code patterns you're
targeting here, and I bet we could do some mechanical treewide changes
with Coccinelle to remove a ton of boilerplate code.
I like this API, and the CPP isn't very obfuscated at all, compared to
some stuff we've already got in the tree. :)
-Kees
--
Kees Cook
next prev parent reply other threads:[~2023-05-26 17:05 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-05-26 15:05 [RFC][PATCH 0/2] Lock and Pointer guards Peter Zijlstra
2023-05-26 15:05 ` [RFC][PATCH 1/2] locking: Introduce __cleanup__ based guards Peter Zijlstra
2023-05-26 17:05 ` Kees Cook [this message]
2023-05-26 18:39 ` Miguel Ojeda
2023-05-26 18:22 ` Linus Torvalds
2023-05-26 19:10 ` Peter Zijlstra
2023-05-26 18:49 ` Waiman Long
2023-05-26 18:58 ` Mathieu Desnoyers
2023-05-26 19:04 ` Waiman Long
2023-05-26 15:05 ` [RFC][PATCH 2/2] sched: Use fancy new guards Peter Zijlstra
2023-05-26 16:25 ` Greg KH
2023-05-26 16:27 ` Mathieu Desnoyers
2023-05-26 16:43 ` Peter Zijlstra
2023-05-26 17:08 ` Kees Cook
2023-05-26 17:41 ` Miguel Ojeda
2023-05-26 16:41 ` Peter Zijlstra
2023-05-29 11:29 ` [RFC][PATCH 0/2] Lock and Pointer guards Paolo Bonzini
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=202305260939.D33FE435D2@keescook \
--to=keescook@chromium.org \
--cc=boqun.feng@gmail.com \
--cc=bristot@redhat.com \
--cc=bsegall@google.com \
--cc=dietmar.eggemann@arm.com \
--cc=frederic@kernel.org \
--cc=gregkh@linuxfoundation.org \
--cc=jiangshanlai@gmail.com \
--cc=joel@joelfernandes.org \
--cc=josh@joshtriplett.org \
--cc=juri.lelli@redhat.com \
--cc=linux-kernel@vger.kernel.org \
--cc=longman@redhat.com \
--cc=mathieu.desnoyers@efficios.com \
--cc=mgorman@suse.de \
--cc=mingo@redhat.com \
--cc=ndesaulniers@google.com \
--cc=ojeda@kernel.org \
--cc=paulmck@kernel.org \
--cc=pbonzini@redhat.com \
--cc=peterz@infradead.org \
--cc=qiang1.zhang@intel.com \
--cc=quic_neeraju@quicinc.com \
--cc=rcu@vger.kernel.org \
--cc=rostedt@goodmis.org \
--cc=tglx@linutronix.de \
--cc=tj@kernel.org \
--cc=torvalds@linux-foundation.org \
--cc=vincent.guittot@linaro.org \
--cc=vschneid@redhat.com \
--cc=will@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox