Skip to contentSkip to conversation

sched: Allow sleeping spinlocks on PREEMPT_RT within non_block_start()/end block.

PATCH Superseded Sebastian Andrzej Siewior 3 participants 4 messages Aug 21 – Sep 16

Subsystems (3)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Sebastian Andrzej Siewior

author maintainer
#0 lore raw

PATCH 1/1 sched: Allow sleeping spinlocks on PREEMPT_RT within non_block_start()/end block.

Commit 312364f3534cc ("kernel.h: Add non_block_start/end()") added non_block_star()/end(). The intention was to catch any callbacks which depend on locks or sleep able conditionals in order to ensure forward progress. According to the commit message spinlocks were excluded "because spinlocks can't have an indirect dependency upon the page allocator…".
On PREEMPT_RT this leads to a splat because spinlock_t and rwlock_t are turned into a sleeping spinlock which have a might_sleep() as they could schedule() on lock contention.

Besids mm, the other used of non_block_start() is pwm. Here the hrtimer can acnqure a spinlock_t on RT and I can't tell other user but I assume it is not atomic on RT since it wouldn't be able to acquire its own (spinlock_t) lock.

Since spinlock_t (and rwlock_t) are not problem, add a sleeping_lock argument to __might_resched() signaling if this is scheduling request is from a sleeping lock (true) or regular scheduling request (false). Add the `true' argument to rtlock_might_resched() which is used on PREEMPT_RT for both sleeping lock types.

Cc Michal Hocko <[email protected]>
Cc David Woodhouse <[email protected]>
Cc Sean Young <[email protected]>
Cc Thierry Reding <[email protected]>
---
 include/linux/kernel.h       |  5 +++--
 include/linux/sched.h        | 24 ++++++++++++------------
 kernel/locking/spinlock_rt.c |  2 +-
 kernel/sched/core.c          |  7 ++++---
 4 files changed, 20 insertions(+), 18 deletions(-)
include/linux/kernel.h +3 -2
index e5570a16cbb1a..fbafdfe9bdb78 100644
@@ -70,7 +70,8 @@ extern int dynamic_might_resched(void);
 #endif /* CONFIG_PREEMPT_* */
 
 #ifdef CONFIG_DEBUG_ATOMIC_SLEEP
-extern void __might_resched(const char *file, int line, unsigned int offsets);
+extern void __might_resched(const char *file, int line, unsigned int offsets,
+			    bool rt_sleeping_lock);
 extern void __might_sleep(const char *file, int line);
 extern void __cant_sleep(const char *file, int line, int preempt_offset);
 extern void __cant_migrate(const char *file, int line);
@@ -128,7 +129,7 @@ extern void __cant_migrate(const char *file, int line);
 # define non_block_end() WARN_ON(current->non_block_count-- == 0)
 #else
   static inline void __might_resched(const char *file, int line,
-				     unsigned int offsets) { }
+				     unsigned int offsets, bool rt_sleeping_lock) { }
 static inline void __might_sleep(const char *file, int line) { }
 # define might_sleep() do { might_resched(); } while (0)
 # define cant_sleep() do { } while (0)
include/linux/sched.h +12 -12
index 373bcc0598d10..650e42d55e1e8 100644
@@ -2158,9 +2158,9 @@ static inline int _cond_resched(void)
 
 #endif /* !CONFIG_PREEMPTION || CONFIG_PREEMPT_DYNAMIC */
 
-#define cond_resched() ({			\
-	__might_resched(__FILE__, __LINE__, 0);	\
-	_cond_resched();			\
+#define cond_resched() ({				\
+	__might_resched(__FILE__, __LINE__, 0, false);	\
+	_cond_resched();				\
 })
 
 extern int __cond_resched_lock(spinlock_t *lock) __must_hold(lock);
@@ -2186,19 +2186,19 @@ extern int __cond_resched_rwlock_write(rwlock_t *lock) __must_hold(lock);
 	(PREEMPT_LOCK_OFFSET + (1U << MIGHT_RESCHED_RCU_SHIFT))
 #endif
 
-#define cond_resched_lock(lock) ({						\
-	__might_resched(__FILE__, __LINE__, PREEMPT_LOCK_RESCHED_OFFSETS);	\
-	__cond_resched_lock(lock);						\
+#define cond_resched_lock(lock) ({							\
+	__might_resched(__FILE__, __LINE__, PREEMPT_LOCK_RESCHED_OFFSETS, false);	\
+	__cond_resched_lock(lock);							\
 })
 
-#define cond_resched_rwlock_read(lock) ({					\
-	__might_resched(__FILE__, __LINE__, PREEMPT_LOCK_RESCHED_OFFSETS);	\
-	__cond_resched_rwlock_read(lock);					\
+#define cond_resched_rwlock_read(lock) ({						\
+	__might_resched(__FILE__, __LINE__, PREEMPT_LOCK_RESCHED_OFFSETS, false);	\
+	__cond_resched_rwlock_read(lock);						\
 })
 
-#define cond_resched_rwlock_write(lock) ({					\
-	__might_resched(__FILE__, __LINE__, PREEMPT_LOCK_RESCHED_OFFSETS);	\
-	__cond_resched_rwlock_write(lock);					\
+#define cond_resched_rwlock_write(lock) ({						\
+	__might_resched(__FILE__, __LINE__, PREEMPT_LOCK_RESCHED_OFFSETS, false);	\
+	__cond_resched_rwlock_write(lock);						\
 })
 
 #ifndef CONFIG_PREEMPT_RT
kernel/locking/spinlock_rt.c +1 -1
index 1d5e1b3c60bfa..0a89e7f10af18 100644
@@ -33,7 +33,7 @@
 	(rcu_preempt_depth() << MIGHT_RESCHED_RCU_SHIFT)
 
 #define rtlock_might_resched()						\
-	__might_resched(__FILE__, __LINE__, RTLOCK_RESCHED_OFFSETS)
+	__might_resched(__FILE__, __LINE__, RTLOCK_RESCHED_OFFSETS, true)
 
 static __always_inline void rtlock_lock(struct rt_mutex_base *rtm)
 {
kernel/sched/core.c +4 -3
index 96226707c2f61..ee0fbb41fc61c 100644
@@ -9123,7 +9123,7 @@ void __might_sleep(const char *file, int line)
 			(void *)current->task_state_change,
 			(void *)current->task_state_change);
 
-	__might_resched(file, line, 0);
+	__might_resched(file, line, 0, false);
 }
 EXPORT_SYMBOL(__might_sleep);
 
@@ -9148,7 +9148,8 @@ static inline bool resched_offsets_ok(unsigned int offsets)
 	return nested == offsets;
 }
 
-void __might_resched(const char *file, int line, unsigned int offsets)
+void __might_resched(const char *file, int line, unsigned int offsets,
+		     bool rt_sleeping_lock)
 {
 	/* Ratelimiting timestamp: */
 	static unsigned long prev_jiffy;
@@ -9159,7 +9160,7 @@ void __might_resched(const char *file, int line, unsigned int offsets)
 	rcu_sleep_check();
 
 	if ((resched_offsets_ok(offsets) && !irqs_disabled() &&
-	     !is_idle_task(current) && !current->non_block_count) ||
+	     !is_idle_task(current) && (rt_sleeping_lock || !current->non_block_count)) ||
 	    system_state == SYSTEM_BOOTING || system_state > SYSTEM_RUNNING ||
 	    oops_in_progress)
 		return;
Signature · 1 line
2.55.0
#1 lore raw
Quoted 15 lines · Sebastian Andrzej Siewior

Commit 312364f3534cc ("kernel.h: Add non_block_start/end()") added non_block_star()/end(). The intention was to catch any callbacks which depend on locks or sleep able conditionals in order to ensure forward progress. According to the commit message spinlocks were excluded "because spinlocks can't have an indirect dependency upon the page allocator…".
On PREEMPT_RT this leads to a splat because spinlock_t and rwlock_t are turned into a sleeping spinlock which have a might_sleep() as they could schedule() on lock contention.

Besids mm, the other used of non_block_start() is pwm. Here the hrtimer can acnqure a spinlock_t on RT and I can't tell other user but I assume it is not atomic on RT since it wouldn't be able to acquire its own (spinlock_t) lock.

Quoted 5 lines

Since spinlock_t (and rwlock_t) are not problem, add a sleeping_lock argument to __might_resched() signaling if this is scheduling request is from a sleeping lock (true) or regular scheduling request (false). Add the `true' argument to rtlock_might_resched() which is used on PREEMPT_RT for both sleeping lock types.

This definitely makes a lot of sense to me. It looks like a straightforward solution for false positives. The patch looks good to me, I do not feel qualified to give you Acked-by though.

Thanks!

Quoted 128 lines
Cc Michal Hocko <[email protected]>
Cc David Woodhouse <[email protected]>
Cc Sean Young <[email protected]>
Cc Thierry Reding <[email protected]>
---
 include/linux/kernel.h       |  5 +++--
 include/linux/sched.h        | 24 ++++++++++++------------
 kernel/locking/spinlock_rt.c |  2 +-
 kernel/sched/core.c          |  7 ++++---
 4 files changed, 20 insertions(+), 18 deletions(-)
include/linux/kernel.h +3 -2
index e5570a16cbb1a..fbafdfe9bdb78 100644
@@ -70,7 +70,8 @@ extern int dynamic_might_resched(void);
 #endif /* CONFIG_PREEMPT_* */
 
 #ifdef CONFIG_DEBUG_ATOMIC_SLEEP
-extern void __might_resched(const char *file, int line, unsigned int offsets);
+extern void __might_resched(const char *file, int line, unsigned int offsets,
+			    bool rt_sleeping_lock);
 extern void __might_sleep(const char *file, int line);
 extern void __cant_sleep(const char *file, int line, int preempt_offset);
 extern void __cant_migrate(const char *file, int line);
@@ -128,7 +129,7 @@ extern void __cant_migrate(const char *file, int line);
 # define non_block_end() WARN_ON(current->non_block_count-- == 0)
 #else
   static inline void __might_resched(const char *file, int line,
-				     unsigned int offsets) { }
+				     unsigned int offsets, bool rt_sleeping_lock) { }
 static inline void __might_sleep(const char *file, int line) { }
 # define might_sleep() do { might_resched(); } while (0)
 # define cant_sleep() do { } while (0)
include/linux/sched.h +12 -12
index 373bcc0598d10..650e42d55e1e8 100644
@@ -2158,9 +2158,9 @@ static inline int _cond_resched(void)
 
 #endif /* !CONFIG_PREEMPTION || CONFIG_PREEMPT_DYNAMIC */
 
-#define cond_resched() ({			\
-	__might_resched(__FILE__, __LINE__, 0);	\
-	_cond_resched();			\
+#define cond_resched() ({				\
+	__might_resched(__FILE__, __LINE__, 0, false);	\
+	_cond_resched();				\
 })
 
 extern int __cond_resched_lock(spinlock_t *lock) __must_hold(lock);
@@ -2186,19 +2186,19 @@ extern int __cond_resched_rwlock_write(rwlock_t *lock) __must_hold(lock);
 	(PREEMPT_LOCK_OFFSET + (1U << MIGHT_RESCHED_RCU_SHIFT))
 #endif
 
-#define cond_resched_lock(lock) ({						\
-	__might_resched(__FILE__, __LINE__, PREEMPT_LOCK_RESCHED_OFFSETS);	\
-	__cond_resched_lock(lock);						\
+#define cond_resched_lock(lock) ({							\
+	__might_resched(__FILE__, __LINE__, PREEMPT_LOCK_RESCHED_OFFSETS, false);	\
+	__cond_resched_lock(lock);							\
 })
 
-#define cond_resched_rwlock_read(lock) ({					\
-	__might_resched(__FILE__, __LINE__, PREEMPT_LOCK_RESCHED_OFFSETS);	\
-	__cond_resched_rwlock_read(lock);					\
+#define cond_resched_rwlock_read(lock) ({						\
+	__might_resched(__FILE__, __LINE__, PREEMPT_LOCK_RESCHED_OFFSETS, false);	\
+	__cond_resched_rwlock_read(lock);						\
 })
 
-#define cond_resched_rwlock_write(lock) ({					\
-	__might_resched(__FILE__, __LINE__, PREEMPT_LOCK_RESCHED_OFFSETS);	\
-	__cond_resched_rwlock_write(lock);					\
+#define cond_resched_rwlock_write(lock) ({						\
+	__might_resched(__FILE__, __LINE__, PREEMPT_LOCK_RESCHED_OFFSETS, false);	\
+	__cond_resched_rwlock_write(lock);						\
 })
 
 #ifndef CONFIG_PREEMPT_RT
kernel/locking/spinlock_rt.c +1 -1
index 1d5e1b3c60bfa..0a89e7f10af18 100644
@@ -33,7 +33,7 @@
 	(rcu_preempt_depth() << MIGHT_RESCHED_RCU_SHIFT)
 
 #define rtlock_might_resched()						\
-	__might_resched(__FILE__, __LINE__, RTLOCK_RESCHED_OFFSETS)
+	__might_resched(__FILE__, __LINE__, RTLOCK_RESCHED_OFFSETS, true)
 
 static __always_inline void rtlock_lock(struct rt_mutex_base *rtm)
 {
kernel/sched/core.c +4 -3
index 96226707c2f61..ee0fbb41fc61c 100644
@@ -9123,7 +9123,7 @@ void __might_sleep(const char *file, int line)
 			(void *)current->task_state_change,
 			(void *)current->task_state_change);
 
-	__might_resched(file, line, 0);
+	__might_resched(file, line, 0, false);
 }
 EXPORT_SYMBOL(__might_sleep);
 
@@ -9148,7 +9148,8 @@ static inline bool resched_offsets_ok(unsigned int offsets)
 	return nested == offsets;
 }
 
-void __might_resched(const char *file, int line, unsigned int offsets)
+void __might_resched(const char *file, int line, unsigned int offsets,
+		     bool rt_sleeping_lock)
 {
 	/* Ratelimiting timestamp: */
 	static unsigned long prev_jiffy;
@@ -9159,7 +9160,7 @@ void __might_resched(const char *file, int line, unsigned int offsets)
 	rcu_sleep_check();
 
 	if ((resched_offsets_ok(offsets) && !irqs_disabled() &&
-	     !is_idle_task(current) && !current->non_block_count) ||
+	     !is_idle_task(current) && (rt_sleeping_lock || !current->non_block_count)) ||
 	    system_state == SYSTEM_BOOTING || system_state > SYSTEM_RUNNING ||
 	    oops_in_progress)
 		return;
Signature · 1 line
2.55.0
Signature · 2 lines
Michal Hocko
SUSE Labs
Acked-by on v1 1/1
#2 lore raw

Re: [PATCH] sched: Allow sleeping spinlocks on PREEMPT_RT within non_block_start() section

In reply to Sebastian Andrzej Siewior
Quoted 3 lines · Sebastian Andrzej Siewior

Since spinlock_t (and rwlock_t) are not problem, add a sleeping_lock argument to __might_resched() signaling if this is scheduling request is from a sleeping lock (true) or regular scheduling request (false).

Fixes 312364f3534c ("kernel.h: Add non_block_start/end()")
Reported-by [email protected]

I've prodded syzbot; let's see if we can have a Tested-by: from it too.

I kind of also want synchronize_srcu() to be permitted too, but I guess
https://lore.kernel.org/all/[email protected]/
would need to be resolved first.

Thanks.

author maintainer
#3 lore raw

PATCH v2 1/1 sched: Allow sleeping spinlocks on PREEMPT_RT within non_block_start()/end block.

In reply to Sebastian Andrzej Siewior

Commit 312364f3534cc ("kernel.h: Add non_block_start/end()") added non_block_star()/end(). The intention was to catch any callbacks which depend on locks or sleep able conditionals in order to ensure forward progress. According to the commit message spinlocks were excluded "because spinlocks can't have an indirect dependency upon the page allocator…".
On PREEMPT_RT this leads to a splat because spinlock_t and rwlock_t are turned into a sleeping spinlock which have a might_sleep() as they could schedule() on lock contention.

Besids mm, the other used of non_block_start() is pwm. Here the hrtimer can acnqure a spinlock_t on RT and I can't tell other user but I assume it is not atomic on RT since it wouldn't be able to acquire its own (spinlock_t) lock.

Since spinlock_t (and rwlock_t) are not problem, add a sleeping_lock argument to __might_resched() signaling if this is scheduling request is from a sleeping lock (true) or regular scheduling request (false). Add the `true' argument to rtlock_might_resched() which is used on PREEMPT_RT for both sleeping lock types.

Cc Michal Hocko <[email protected]>
Cc David Woodhouse <[email protected]>
Cc Sean Young <[email protected]>
Cc Thierry Reding <[email protected]>
Fixes 312364f3534c ("kernel.h: Add non_block_start/end()")
Reported-by [email protected]

---

v1…v2: https://lore.kernel.org/all/[email protected]/
- repost with collected tags
- Add Reported-by for syzbot

 include/linux/kernel.h       |  5 +++--
 include/linux/sched.h        | 24 ++++++++++++------------
 kernel/locking/spinlock_rt.c |  2 +-
 kernel/sched/core.c          |  7 ++++---
 4 files changed, 20 insertions(+), 18 deletions(-)
include/linux/kernel.h +3 -2
index 24414c79e59ab..c3937e081f18f 100644
@@ -70,7 +70,8 @@ extern int dynamic_might_resched(void);
 #endif /* CONFIG_PREEMPT_* */
 
 #ifdef CONFIG_DEBUG_ATOMIC_SLEEP
-extern void __might_resched(const char *file, int line, unsigned int offsets);
+extern void __might_resched(const char *file, int line, unsigned int offsets,
+			    bool rt_sleeping_lock);
 extern void __might_sleep(const char *file, int line);
 extern void __cant_sleep(const char *file, int line);
 extern void __cant_migrate(const char *file, int line);
@@ -128,7 +129,7 @@ extern void __cant_migrate(const char *file, int line);
 # define non_block_end() WARN_ON(current->non_block_count-- == 0)
 #else
   static inline void __might_resched(const char *file, int line,
-				     unsigned int offsets) { }
+				     unsigned int offsets, bool rt_sleeping_lock) { }
 static inline void __might_sleep(const char *file, int line) { }
 # define might_sleep() do { might_resched(); } while (0)
 # define cant_sleep() do { } while (0)
include/linux/sched.h +12 -12
index 705970d07614f..bb4cbca8f7d69 100644
@@ -2173,9 +2173,9 @@ static inline int _cond_resched(void)
 
 #endif /* !CONFIG_PREEMPTION || CONFIG_PREEMPT_DYNAMIC */
 
-#define cond_resched() ({			\
-	__might_resched(__FILE__, __LINE__, 0);	\
-	_cond_resched();			\
+#define cond_resched() ({				\
+	__might_resched(__FILE__, __LINE__, 0, false);	\
+	_cond_resched();				\
 })
 
 extern int __cond_resched_lock(spinlock_t *lock) __must_hold(lock);
@@ -2201,19 +2201,19 @@ extern int __cond_resched_rwlock_write(rwlock_t *lock) __must_hold(lock);
 	(PREEMPT_LOCK_OFFSET + (1U << MIGHT_RESCHED_RCU_SHIFT))
 #endif
 
-#define cond_resched_lock(lock) ({						\
-	__might_resched(__FILE__, __LINE__, PREEMPT_LOCK_RESCHED_OFFSETS);	\
-	__cond_resched_lock(lock);						\
+#define cond_resched_lock(lock) ({							\
+	__might_resched(__FILE__, __LINE__, PREEMPT_LOCK_RESCHED_OFFSETS, false);	\
+	__cond_resched_lock(lock);							\
 })
 
-#define cond_resched_rwlock_read(lock) ({					\
-	__might_resched(__FILE__, __LINE__, PREEMPT_LOCK_RESCHED_OFFSETS);	\
-	__cond_resched_rwlock_read(lock);					\
+#define cond_resched_rwlock_read(lock) ({						\
+	__might_resched(__FILE__, __LINE__, PREEMPT_LOCK_RESCHED_OFFSETS, false);	\
+	__cond_resched_rwlock_read(lock);						\
 })
 
-#define cond_resched_rwlock_write(lock) ({					\
-	__might_resched(__FILE__, __LINE__, PREEMPT_LOCK_RESCHED_OFFSETS);	\
-	__cond_resched_rwlock_write(lock);					\
+#define cond_resched_rwlock_write(lock) ({						\
+	__might_resched(__FILE__, __LINE__, PREEMPT_LOCK_RESCHED_OFFSETS, false);	\
+	__cond_resched_rwlock_write(lock);						\
 })
 
 #ifndef CONFIG_PREEMPT_RT
kernel/locking/spinlock_rt.c +1 -1
index 1d5e1b3c60bfa..0a89e7f10af18 100644
@@ -33,7 +33,7 @@
 	(rcu_preempt_depth() << MIGHT_RESCHED_RCU_SHIFT)
 
 #define rtlock_might_resched()						\
-	__might_resched(__FILE__, __LINE__, RTLOCK_RESCHED_OFFSETS)
+	__might_resched(__FILE__, __LINE__, RTLOCK_RESCHED_OFFSETS, true)
 
 static __always_inline void rtlock_lock(struct rt_mutex_base *rtm)
 {
kernel/sched/core.c +4 -3
index 7885ff76e69f2..ff2bdc5a86679 100644
@@ -9183,7 +9183,7 @@ void __might_sleep(const char *file, int line)
 			(void *)current->task_state_change,
 			(void *)current->task_state_change);
 
-	__might_resched(file, line, 0);
+	__might_resched(file, line, 0, false);
 }
 EXPORT_SYMBOL(__might_sleep);
 
@@ -9208,7 +9208,8 @@ static inline bool resched_offsets_ok(unsigned int offsets)
 	return nested == offsets;
 }
 
-void __might_resched(const char *file, int line, unsigned int offsets)
+void __might_resched(const char *file, int line, unsigned int offsets,
+		     bool rt_sleeping_lock)
 {
 	/* Ratelimiting timestamp: */
 	static unsigned long prev_jiffy;
@@ -9219,7 +9220,7 @@ void __might_resched(const char *file, int line, unsigned int offsets)
 	rcu_sleep_check();
 
 	if ((resched_offsets_ok(offsets) && !irqs_disabled() &&
-	     !is_idle_task(current) && !current->non_block_count) ||
+	     !is_idle_task(current) && (rt_sleeping_lock || !current->non_block_count)) ||
 	    system_state == SYSTEM_BOOTING || system_state > SYSTEM_RUNNING ||
 	    oops_in_progress)
 		return;
Signature · 1 line
2.55.0