Skip to contentSkip to conversation

locking: Detect includes rwlock.h outside of spinlock.h

PATCH Applied Sebastian Andrzej Siewior 4 participants 6 messages Aug 25, 2022 – Sep 15, 2022 Locking primitives

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 locking: Detect includes rwlock.h outside of spinlock.h

From: Michael S. Tsirkin <[email protected]>

The check for __LINUX_SPINLOCK_H within rwlock.h (and other files) detects the direct include of the header file if it is at the very beginning of the include section.
If it is listed later then chances are high that spinlock.h was already included (including rwlock.h) and the additional listing of rwlock.h will not cause any failure.

On PREEMPT_RT this additional rwlock.h will lead to compile failures since it uses a different rwlock implementation.

Add __LINUX_INSIDE_SPINLOCK_H to spinlock.h and check for this instead of __LINUX_SPINLOCK_H to detect wrong includes. This will help detect direct includes of rwlock.h with without PREEMPT_RT enabled.

[ bigeasy: add remaining __LINUX_SPINLOCK_H user and rewrite
commit description. ]

---

This is to avoid patches like
https://lkml.kernel.org/r/[email protected]

in the future which is not the first I sent…

 include/linux/rwlock.h           |    2 +-
 include/linux/spinlock.h         |    2 ++
 include/linux/spinlock_api_smp.h |    2 +-
 include/linux/spinlock_api_up.h  |    2 +-
 include/linux/spinlock_rt.h      |    2 +-
 include/linux/spinlock_up.h      |    2 +-
 6 files changed, 7 insertions(+), 5 deletions(-)
include/linux/rwlock.h +1 -1
@@ -1,7 +1,7 @@
 #ifndef __LINUX_RWLOCK_H
 #define __LINUX_RWLOCK_H
 
-#ifndef __LINUX_SPINLOCK_H
+#ifndef __LINUX_INSIDE_SPINLOCK_H
 # error "please don't include this file directly"
 #endif
 
include/linux/spinlock.h +2 -0
@@ -1,6 +1,7 @@
 /* SPDX-License-Identifier: GPL-2.0 */
 #ifndef __LINUX_SPINLOCK_H
 #define __LINUX_SPINLOCK_H
+#define __LINUX_INSIDE_SPINLOCK_H
 
 /*
  * include/linux/spinlock.h - generic spinlock/rwlock declarations
@@ -492,4 +493,5 @@ int __alloc_bucket_spinlocks(spinlock_t
 
 void free_bucket_spinlocks(spinlock_t *locks);
 
+#undef __LINUX_INSIDE_SPINLOCK_H
 #endif /* __LINUX_SPINLOCK_H */
include/linux/spinlock_api_smp.h +1 -1
@@ -1,7 +1,7 @@
 #ifndef __LINUX_SPINLOCK_API_SMP_H
 #define __LINUX_SPINLOCK_API_SMP_H
 
-#ifndef __LINUX_SPINLOCK_H
+#ifndef __LINUX_INSIDE_SPINLOCK_H
 # error "please don't include this file directly"
 #endif
 
include/linux/spinlock_api_up.h +1 -1
@@ -1,7 +1,7 @@
 #ifndef __LINUX_SPINLOCK_API_UP_H
 #define __LINUX_SPINLOCK_API_UP_H
 
-#ifndef __LINUX_SPINLOCK_H
+#ifndef __LINUX_INSIDE_SPINLOCK_H
 # error "please don't include this file directly"
 #endif
 
include/linux/spinlock_rt.h +1 -1
@@ -2,7 +2,7 @@
 #ifndef __LINUX_SPINLOCK_RT_H
 #define __LINUX_SPINLOCK_RT_H
 
-#ifndef __LINUX_SPINLOCK_H
+#ifndef __LINUX_INSIDE_SPINLOCK_H
 #error Do not include directly. Use spinlock.h
 #endif
 
include/linux/spinlock_up.h +1 -1
@@ -1,7 +1,7 @@
 #ifndef __LINUX_SPINLOCK_UP_H
 #define __LINUX_SPINLOCK_UP_H
 
-#ifndef __LINUX_SPINLOCK_H
+#ifndef __LINUX_INSIDE_SPINLOCK_H
 # error "please don't include this file directly"
 #endif
 
#1 lore raw
Quoted 19 lines · On 2022-08-25 18:08:59 [+0200], To [email protected] wrote:

From: Michael S. Tsirkin <[email protected]>

The check for __LINUX_SPINLOCK_H within rwlock.h (and other files) detects the direct include of the header file if it is at the very beginning of the include section.
If it is listed later then chances are high that spinlock.h was already included (including rwlock.h) and the additional listing of rwlock.h will not cause any failure.

On PREEMPT_RT this additional rwlock.h will lead to compile failures since it uses a different rwlock implementation.

Add __LINUX_INSIDE_SPINLOCK_H to spinlock.h and check for this instead of __LINUX_SPINLOCK_H to detect wrong includes. This will help detect direct includes of rwlock.h with without PREEMPT_RT enabled.

[ bigeasy: add remaining __LINUX_SPINLOCK_H user and rewrite
commit description. ]

Quoted 8 lines

---

This is to avoid patches like
https://lkml.kernel.org/r/[email protected]

in the future which is not the first I sent…

polite *ping*

Sebastian

#2 lore raw
Quoted 24 lines · Sebastian Andrzej Siewior
Quoted 23 lines · On 2022-08-25 18:08:59 [+0200], To [email protected] wrote:

From: Michael S. Tsirkin <[email protected]>

The check for __LINUX_SPINLOCK_H within rwlock.h (and other files) detects the direct include of the header file if it is at the very beginning of the include section.
If it is listed later then chances are high that spinlock.h was already included (including rwlock.h) and the additional listing of rwlock.h will not cause any failure.

On PREEMPT_RT this additional rwlock.h will lead to compile failures since it uses a different rwlock implementation.

Add __LINUX_INSIDE_SPINLOCK_H to spinlock.h and check for this instead of __LINUX_SPINLOCK_H to detect wrong includes. This will help detect direct includes of rwlock.h with without PREEMPT_RT enabled.

[ bigeasy: add remaining __LINUX_SPINLOCK_H user and rewrite
commit description. ]

---

Quoted 8 lines
Quoted 4 lines

This is to avoid patches like
https://lkml.kernel.org/r/[email protected]

in the future which is not the first I sent…

polite *ping*

Sebastian

This needs to be merged by locking maintainers IMHO, not by me.

Signature · 1 line
MST
#3 lore raw
Quoted 2 lines · Michael S. Tsirkin

This needs to be merged by locking maintainers IMHO, not by me.

I'm aware. This was directed towards will/peterz :)

Sebastian

maintainer
#4 lore raw
In reply to Sebastian Andrzej Siewior
Quoted 16 lines · Sebastian Andrzej Siewior

From: Michael S. Tsirkin <[email protected]>

The check for __LINUX_SPINLOCK_H within rwlock.h (and other files) detects the direct include of the header file if it is at the very beginning of the include section.
If it is listed later then chances are high that spinlock.h was already included (including rwlock.h) and the additional listing of rwlock.h will not cause any failure.

On PREEMPT_RT this additional rwlock.h will lead to compile failures since it uses a different rwlock implementation.

Add __LINUX_INSIDE_SPINLOCK_H to spinlock.h and check for this instead of __LINUX_SPINLOCK_H to detect wrong includes. This will help detect direct includes of rwlock.h with without PREEMPT_RT enabled.

Quoted 5 lines

[ bigeasy: add remaining __LINUX_SPINLOCK_H user and rewrite
commit description. ]

Thanks!

#5 lore raw
In reply to Sebastian Andrzej Siewior
The following commit has been merged into the locking/core branch of tip:

The following commit has been merged into the locking/core branch of tip:

Commit-ID:     2747b93ebbede2af2d7bb088b9ddae3193ceede8
Gitweb:        https://git.kernel.org/tip/2747b93ebbede2af2d7bb088b9ddae3193ceede8
Author:        Sebastian Andrzej Siewior <[email protected]>
AuthorDate:    Thu, 25 Aug 2022 18:08:56 +02:00
Committer:     Peter Zijlstra <[email protected]>
CommitterDate: Thu, 15 Sep 2022 16:14:02 +02:00

locking: Detect includes rwlock.h outside of spinlock.h

From: Michael S. Tsirkin <[email protected]>

The check for __LINUX_SPINLOCK_H within rwlock.h (and other files) detects the direct include of the header file if it is at the very beginning of the include section.
If it is listed later then chances are high that spinlock.h was already included (including rwlock.h) and the additional listing of rwlock.h will not cause any failure.

On PREEMPT_RT this additional rwlock.h will lead to compile failures since it uses a different rwlock implementation.

Add __LINUX_INSIDE_SPINLOCK_H to spinlock.h and check for this instead of __LINUX_SPINLOCK_H to detect wrong includes. This will help detect direct includes of rwlock.h with without PREEMPT_RT enabled.

[ bigeasy: add remaining __LINUX_SPINLOCK_H user and rewrite
commit description. ]

---
 include/linux/rwlock.h           | 2 +-
 include/linux/spinlock.h         | 2 ++
 include/linux/spinlock_api_smp.h | 2 +-
 include/linux/spinlock_api_up.h  | 2 +-
 include/linux/spinlock_rt.h      | 2 +-
 include/linux/spinlock_up.h      | 2 +-
 6 files changed, 7 insertions(+), 5 deletions(-)
include/linux/rwlock.h +1 -1
index 8f416c5..c0ef596 100644
@@ -1,7 +1,7 @@
 #ifndef __LINUX_RWLOCK_H
 #define __LINUX_RWLOCK_H
 
-#ifndef __LINUX_SPINLOCK_H
+#ifndef __LINUX_INSIDE_SPINLOCK_H
 # error "please don't include this file directly"
 #endif
 
include/linux/spinlock.h +2 -0
index 5c0c517..1341f7d 100644
@@ -1,6 +1,7 @@
 /* SPDX-License-Identifier: GPL-2.0 */
 #ifndef __LINUX_SPINLOCK_H
 #define __LINUX_SPINLOCK_H
+#define __LINUX_INSIDE_SPINLOCK_H
 
 /*
  * include/linux/spinlock.h - generic spinlock/rwlock declarations
@@ -492,4 +493,5 @@ int __alloc_bucket_spinlocks(spinlock_t **locks, unsigned int *lock_mask,
 
 void free_bucket_spinlocks(spinlock_t *locks);
 
+#undef __LINUX_INSIDE_SPINLOCK_H
 #endif /* __LINUX_SPINLOCK_H */
include/linux/spinlock_api_smp.h +1 -1
index 51fa0da..89eb6f4 100644
@@ -1,7 +1,7 @@
 #ifndef __LINUX_SPINLOCK_API_SMP_H
 #define __LINUX_SPINLOCK_API_SMP_H
 
-#ifndef __LINUX_SPINLOCK_H
+#ifndef __LINUX_INSIDE_SPINLOCK_H
 # error "please don't include this file directly"
 #endif
 
include/linux/spinlock_api_up.h +1 -1
index b8ba00c..819aeba 100644
@@ -1,7 +1,7 @@
 #ifndef __LINUX_SPINLOCK_API_UP_H
 #define __LINUX_SPINLOCK_API_UP_H
 
-#ifndef __LINUX_SPINLOCK_H
+#ifndef __LINUX_INSIDE_SPINLOCK_H
 # error "please don't include this file directly"
 #endif
 
include/linux/spinlock_rt.h +1 -1
index 835aeda..61c49b1 100644
@@ -2,7 +2,7 @@
 #ifndef __LINUX_SPINLOCK_RT_H
 #define __LINUX_SPINLOCK_RT_H
 
-#ifndef __LINUX_SPINLOCK_H
+#ifndef __LINUX_INSIDE_SPINLOCK_H
 #error Do not include directly. Use spinlock.h
 #endif
 
include/linux/spinlock_up.h +1 -1
index 1652107..c872042 100644
@@ -1,7 +1,7 @@
 #ifndef __LINUX_SPINLOCK_UP_H
 #define __LINUX_SPINLOCK_UP_H
 
-#ifndef __LINUX_SPINLOCK_H
+#ifndef __LINUX_INSIDE_SPINLOCK_H
 # error "please don't include this file directly"
 #endif