Re: [RFC][PATCH 1/2] locking: Introduce __cleanup__ based guards

From: Waiman Long
Date: Fri May 26 2023 - 14:50:32 EST


On 5/26/23 11:05, 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.

The CPP is rather impenetrable -- but I'll attempt to write proper
comments if/when people think this is worth pursuing.

Actual usage in the next patch

Signed-off-by: Peter Zijlstra (Intel) <peterz@xxxxxxxxxxxxx>
---
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(+)

That is an interesting idea and may help to simplify some of the common code patterns that we have in the kernel. The macros are a bit hard to read and understand though I thought I got a rough idea of what they are trying to do.

BTW, do we have a use case for double_lock_guard/double_lock_scope? I can envision a nested lock_scope inside a lock_scope, but taking 2 auto locks of the same type at init time and then unlock them at exit just doesn't make sense to me.

Cheers,
Longman