Skip to content

Commit 3f48a0f

Browse files
Bjoern A. ZeebBjoern A. Zeeb
authored andcommitted
LinuxKPI: add DEFINE_LOCK_GUARD_0 for rcu
This adds guard support for non-real-types like rcu locking meaning that we need to keep the lock state separately ourselves. _T is still special and needs to be updated. Given it may not be used it needs an __unused attribute (we are using the LinuxKPI __maybe_unused which indeed is more expressive in this case). Sponsored by: The FreeBSD Foundation (initially) MFC after: 3 days Reviewed by: dumbbell Differential Revision: https://reviews.freebsd.org/D52076
1 parent 39e9290 commit 3f48a0f

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

sys/compat/linuxkpi/common/include/linux/cleanup.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,39 @@
5555

5656
#define __free(_n) __cleanup(__free_##_n)
5757

58+
/*
59+
* Given this is a _0 version it should likely be broken up into parts.
60+
* But we have no idead what a _1, _2, ... version would do different
61+
* until we see a call.
62+
* This is used for a not-real-type (rcu). We use a bool to "simulate"
63+
* the lock held. Also _T still special, may not always be used, so tag
64+
* with __unused (or better the LinuxKPI __maybe_unused).
65+
*/
66+
#define DEFINE_LOCK_GUARD_0(_n, _lock, _unlock, ...) \
67+
\
68+
typedef struct { \
69+
bool lock; \
70+
__VA_ARGS__; \
71+
} guard_ ## _n ## _t; \
72+
\
73+
static inline void \
74+
guard_ ## _n ## _destroy(guard_ ## _n ## _t *_T) \
75+
{ \
76+
if (_T->lock) { \
77+
_unlock; \
78+
} \
79+
} \
80+
\
81+
static inline guard_ ## _n ## _t \
82+
guard_ ## _n ## _create(void) \
83+
{ \
84+
guard_ ## _n ## _t _tmp; \
85+
guard_ ## _n ## _t *_T __maybe_unused; \
86+
\
87+
_tmp.lock = true; \
88+
_T = &_tmp; \
89+
_lock; \
90+
return (_tmp); \
91+
}
92+
5893
#endif /* _LINUXKPI_LINUX_CLEANUP_H */

sys/compat/linuxkpi/common/include/linux/rcupdate.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*-
22
* Copyright (c) 2016-2017 Mellanox Technologies, Ltd.
33
* All rights reserved.
4-
* Copyright (c) 2024 The FreeBSD Foundation
4+
* Copyright (c) 2024-2025 The FreeBSD Foundation
55
*
66
* Portions of this software were developed by Björn Zeeb
77
* under sponsorship from the FreeBSD Foundation.
@@ -35,6 +35,7 @@
3535
#include <linux/compiler.h>
3636
#include <linux/types.h>
3737
#include <linux/kernel.h>
38+
#include <linux/cleanup.h>
3839

3940
#include <machine/atomic.h>
4041

@@ -162,4 +163,6 @@ void linux_synchronize_rcu(unsigned type);
162163
#define init_rcu_head_on_stack(...)
163164
#define destroy_rcu_head_on_stack(...)
164165

166+
DEFINE_LOCK_GUARD_0(rcu, rcu_read_lock(), rcu_read_unlock())
167+
165168
#endif /* _LINUXKPI_LINUX_RCUPDATE_H_ */

0 commit comments

Comments
 (0)