Skip to contentSkip to conversation

sched: Drop the need_resched() loop from cond_resched()

PATCH In review Frederic Weisbecker 4 participants 19 messages Jul 10, 2009 Scheduler

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 sched: Drop the need_resched() loop from cond_resched()

The schedule() function is a loop that reschedules the current task while the TIF_NEED_RESCHED flag is set:

void schedule(void)
{
need_resched:

	/* schedule code */
	if (need_resched())
		goto need_resched;
}

And cond_resched() repeat this loop:

do {
	add_preempt_count(PREEMPT_ACTIVE);
	schedule();
	sub_preempt_count(PREEMPT_ACTIVE);
} while(need_resched());

This loop is needless because schedule() already did the check and nothing can set TIF_NEED_RESCHED between schedule() exit and the loop check in need_resched().

Then remove this needless loop.

---
 kernel/sched.c |    8 +++-----
 1 files changed, 3 insertions(+), 5 deletions(-)
kernel/sched.c +3 -5
index 4d1e387..87ecac1 100644
@@ -6613,11 +6613,9 @@ static void __cond_resched(void)
 	 * PREEMPT_ACTIVE, which could trigger a second
 	 * cond_resched() call.
 	 */
-	do {
-		add_preempt_count(PREEMPT_ACTIVE);
-		schedule();
-		sub_preempt_count(PREEMPT_ACTIVE);
-	} while (need_resched());
+	add_preempt_count(PREEMPT_ACTIVE);
+	schedule();
+	sub_preempt_count(PREEMPT_ACTIVE);
 }
 
 int __sched _cond_resched(void)
Signature · 1 line
1.6.2.3
#1 lore raw

PATCH 2/2 sched: Move the sleeping while atomic checks early in cond_resched()

might_sleep() is called lately in cond_resched(), after the need_resched()/preempt enabled/system running tests are checked.

It's better to check the sleeps while atomic earlier and not depend on some environment datas that reduce the chances to detect a problem.

---
 include/linux/sched.h |    2 ++
 kernel/sched.c        |    3 ---
 2 files changed, 2 insertions(+), 3 deletions(-)
include/linux/sched.h +2 -0
index 0cb0d8d..e357dc7 100644
@@ -2279,11 +2279,13 @@ extern int _cond_resched(void);
 #ifdef CONFIG_PREEMPT_BKL
 static inline int cond_resched(void)
 {
+	might_sleep();
 	return 0;
 }
 #else
 static inline int cond_resched(void)
 {
+	might_sleep();
 	return _cond_resched();
 }
 #endif
kernel/sched.c +0 -3
index 87ecac1..c22804b 100644
@@ -6605,9 +6605,6 @@ SYSCALL_DEFINE0(sched_yield)
 
 static void __cond_resched(void)
 {
-#ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
-	__might_sleep(__FILE__, __LINE__);
-#endif
 	/*
 	 * The BKS might be reacquired before we have dropped
 	 * PREEMPT_ACTIVE, which could trigger a second
Signature · 1 line
1.6.2.3
#2 lore raw

Re: [PATCH 2/2] sched: Move the sleeping while atomic checks early in cond_resched()

Quoted 7 lines · Frederic Weisbecker

index 0cb0d8d..e357dc7 100644

include/linux/sched.h +0 -0
@@ -2279,11 +2279,13 @@ extern int _cond_resched(void);
 #ifdef CONFIG_PREEMPT_BKL
 static inline int cond_resched(void)
 {

include/linux/sched.h extern int _cond_resched(void); · line 2291

Quoted 10 lines
include/linux/sched.h +2 -0
+	might_sleep();
 	return 0;
 }
 #else
 static inline int cond_resched(void)
 {
+	might_sleep();
 	return _cond_resched();
 }
 #endif
# define might_resched() _cond_resched()
# define might_sleep() \
	do { __might_sleep(__FILE__, __LINE__); might_resched(); } while (0)

Doesn't seem to make it any better that, but yeah, moving that __might_sleep() did occur to me earlier today when I touched that code.

Quoted 14 lines
kernel/sched.c +0 -3
index 87ecac1..c22804b 100644
@@ -6605,9 +6605,6 @@ SYSCALL_DEFINE0(sched_yield)
 
 static void __cond_resched(void)
 {
-#ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
-	__might_sleep(__FILE__, __LINE__);
-#endif
 	/*
 	 * The BKS might be reacquired before we have dropped
 	 * PREEMPT_ACTIVE, which could trigger a second
#3 lore raw

Re: [PATCH 2/2] sched: Move the sleeping while atomic checks early in cond_resched()

Quoted 13 lines · Peter Zijlstra
Quoted 11 lines · Frederic Weisbecker

index 0cb0d8d..e357dc7 100644

include/linux/sched.h +1 -0
@@ -2279,11 +2279,13 @@ extern int _cond_resched(void);
 #ifdef CONFIG_PREEMPT_BKL
 static inline int cond_resched(void)
 {
+	might_sleep();
 	return 0;
 }
 #else
Quoted 8 lines

include/linux/sched.h extern int _cond_resched(void); · line 2291

Quoted 6 lines
include/linux/sched.h +1 -0
 static inline int cond_resched(void)
 {
+	might_sleep();
 	return _cond_resched();
 }
 #endif
# define might_resched() _cond_resched()

Argh, indeed.
I thought might_sleep() only wrapped __might_sleep(__FILE__, __LINE__)

Quoted 6 lines
# define might_sleep() \
	do { __might_sleep(__FILE__, __LINE__); might_resched(); } while (0)

Doesn't seem to make it any better that, but yeah, moving that __might_sleep() did occur to me earlier today when I touched that code.

Ok.

Another idea: if cond_resched() was a macro and __might_sleep() was called inside, the given __FILE__ __LINE__ would be much more useful.

Only the backtraces would be useful in the current state, __FILE__ and __LINE__ point to sched.h, which is not exactly what is needed, right?

Thanks,
Frederic.

Quoted 17 lines
Quoted 14 lines
kernel/sched.c +0 -3
index 87ecac1..c22804b 100644
@@ -6605,9 +6605,6 @@ SYSCALL_DEFINE0(sched_yield)
 
 static void __cond_resched(void)
 {
-#ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
-	__might_sleep(__FILE__, __LINE__);
-#endif
 	/*
 	 * The BKS might be reacquired before we have dropped
 	 * PREEMPT_ACTIVE, which could trigger a second
#4 lore raw

Re: [PATCH 2/2] sched: Move the sleeping while atomic checks early in cond_resched()

Quoted 36 lines · Frederic Weisbecker
Quoted 13 lines · Peter Zijlstra
Quoted 11 lines · Frederic Weisbecker

index 0cb0d8d..e357dc7 100644

include/linux/sched.h +1 -0
@@ -2279,11 +2279,13 @@ extern int _cond_resched(void);
 #ifdef CONFIG_PREEMPT_BKL
 static inline int cond_resched(void)
 {
+	might_sleep();
 	return 0;
 }
 #else
Quoted 8 lines

include/linux/sched.h extern int _cond_resched(void); · line 2291

Quoted 6 lines
include/linux/sched.h +1 -0
 static inline int cond_resched(void)
 {
+	might_sleep();
 	return _cond_resched();
 }
 #endif
# define might_resched() _cond_resched()

Argh, indeed.
I thought might_sleep() only wrapped __might_sleep(__FILE__, __LINE__)

Quoted 6 lines
# define might_sleep() \
	do { __might_sleep(__FILE__, __LINE__); might_resched(); } while (0)

Doesn't seem to make it any better that, but yeah, moving that __might_sleep() did occur to me earlier today when I touched that code.

Quoted 8 lines

Ok.

Another idea: if cond_resched() was a macro and __might_sleep() was called inside, the given __FILE__ __LINE__ would be much more useful.

Only the backtraces would be useful in the current state, __FILE__ and __LINE__ point to sched.h, which is not exactly what is needed, right?

Right. There's some CONFIG_PREEMPT_BKL clutter in sched.h but I think we could largely fold might_sleep() and cond_resched().

Ingo?

#5 lore raw

Re: [PATCH 2/2] sched: Move the sleeping while atomic checks early in cond_resched()

Quoted 42 lines · Peter Zijlstra
Quoted 41 lines · Frederic Weisbecker
Quoted 13 lines · Peter Zijlstra
Quoted 11 lines · Frederic Weisbecker

index 0cb0d8d..e357dc7 100644

include/linux/sched.h +1 -0
@@ -2279,11 +2279,13 @@ extern int _cond_resched(void);
 #ifdef CONFIG_PREEMPT_BKL
 static inline int cond_resched(void)
 {
+	might_sleep();
 	return 0;
 }
 #else
Quoted 8 lines

include/linux/sched.h extern int _cond_resched(void); · line 2291

Quoted 6 lines
include/linux/sched.h +1 -0
 static inline int cond_resched(void)
 {
+	might_sleep();
 	return _cond_resched();
 }
 #endif
# define might_resched() _cond_resched()

Argh, indeed.
I thought might_sleep() only wrapped __might_sleep(__FILE__, __LINE__)

Quoted 6 lines
# define might_sleep() \
	do { __might_sleep(__FILE__, __LINE__); might_resched(); } while (0)

Doesn't seem to make it any better that, but yeah, moving that __might_sleep() did occur to me earlier today when I touched that code.

Ok.

Another idea: if cond_resched() was a macro and __might_sleep() was called inside, the given __FILE__ __LINE__ would be much more useful.

Quoted 8 lines
Quoted 3 lines

Only the backtraces would be useful in the current state, __FILE__ and __LINE__ point to sched.h, which is not exactly what is needed, right?

Right. There's some CONFIG_PREEMPT_BKL clutter in sched.h but I think we could largely fold might_sleep() and cond_resched().

Ingo?

Yep - CONFIG_PREEMPT_BKL wont ever come back so any legacy related to it should be removed. (That doesnt mean it wont crash or misbehave, this code is quite fragile.)

Ingo

#6 lore raw

PATCH 1/1 sched: Move the sleeping while atomic checks early in cond_resched()

might_sleep() is called lately in cond_resched(), after the need_resched()/preempt enabled/system running tests are checked.

It's better to check the sleeps while atomic earlier and not depend on some environment datas that reduce the chances to detect a problem.

Changes in v2:
- call __might_sleep() directly instead of might_sleep() which may call cond_resched()
- turn cond_resched() into a macro so that the file:line couple reported refers to the caller of cond_resched() and not __cond_resched() itself.
- drop the obsolete CONFIG_PREEMPT_BKL related zones

---
 include/linux/sched.h |   22 +++++++---------------
 kernel/sched.c        |    5 ++---
 2 files changed, 9 insertions(+), 18 deletions(-)
include/linux/sched.h +7 -15
index 0cb0d8d..737f569 100644
@@ -2276,23 +2276,15 @@ static inline int need_resched(void)
  * cond_resched_softirq() will enable bhs before scheduling.
  */
 extern int _cond_resched(void);
-#ifdef CONFIG_PREEMPT_BKL
-static inline int cond_resched(void)
-{
-	return 0;
-}
-#else
-static inline int cond_resched(void)
-{
-	return _cond_resched();
-}
-#endif
+#define cond_resched() ({			\
+	__might_sleep(__FILE__, __LINE__);	\
+	_cond_resched();			\
+})
+
 extern int cond_resched_lock(spinlock_t * lock);
 extern int cond_resched_softirq(void);
-static inline int cond_resched_bkl(void)
-{
-	return _cond_resched();
-}
+
+#define cond_resched_bkl()	cond_resched();
 
 /*
  * Does a critical section need to be broken due to another
kernel/sched.c +2 -3
index 87ecac1..649ec92 100644
@@ -6605,9 +6605,6 @@ SYSCALL_DEFINE0(sched_yield)
 
 static void __cond_resched(void)
 {
-#ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
-	__might_sleep(__FILE__, __LINE__);
-#endif
 	/*
 	 * The BKS might be reacquired before we have dropped
 	 * PREEMPT_ACTIVE, which could trigger a second
@@ -6644,6 +6641,7 @@ int cond_resched_lock(spinlock_t *lock)
 
 	if (spin_needbreak(lock) || resched) {
 		spin_unlock(lock);
+		__might_sleep(__FILE__, __LINE__);
 		if (resched && need_resched())
 			__cond_resched();
 		else
@@ -6661,6 +6659,7 @@ int __sched cond_resched_softirq(void)
 
 	if (need_resched() && system_state == SYSTEM_RUNNING) {
 		local_bh_enable();
+		__might_sleep(__FILE__, __LINE__);
 		__cond_resched();
 		local_bh_disable();
 		return 1;
Signature · 1 line
1.6.2.3
#7 lore raw

Re: [PATCH] sched: Move the sleeping while atomic checks early in cond_resched()

Quoted 36 lines · Frederic Weisbecker

might_sleep() is called lately in cond_resched(), after the need_resched()/preempt enabled/system running tests are checked.

It's better to check the sleeps while atomic earlier and not depend on some environment datas that reduce the chances to detect a problem.

Changes in v2:
- call __might_sleep() directly instead of might_sleep() which may call cond_resched()
- turn cond_resched() into a macro so that the file:line couple reported refers to the caller of cond_resched() and not __cond_resched() itself.
- drop the obsolete CONFIG_PREEMPT_BKL related zones

---
 include/linux/sched.h |   22 +++++++---------------
 kernel/sched.c        |    5 ++---
 2 files changed, 9 insertions(+), 18 deletions(-)
include/linux/sched.h +0 -5
index 0cb0d8d..737f569 100644
@@ -2276,23 +2276,15 @@ static inline int need_resched(void)
  * cond_resched_softirq() will enable bhs before scheduling.
  */
 extern int _cond_resched(void);
-#ifdef CONFIG_PREEMPT_BKL
-static inline int cond_resched(void)
-{
-	return 0;
-}

include/linux/sched.h static inline int need_resched(void) · line 2282

Quoted 10 lines
include/linux/sched.h +4 -6
-#else
-static inline int cond_resched(void)
-{
-	return _cond_resched();
-}
-#endif
+#define cond_resched() ({			\
+	__might_sleep(__FILE__, __LINE__);	\
+	_cond_resched();			\
+})

I don't think this will compile for !CONFIG_DEBUG_SPINLOCK_SLEEP.

include/linux/sched.h static inline int need_resched(void) · line 2287

Quoted 8 lines
include/linux/sched.h +2 -4
 extern int cond_resched_lock(spinlock_t * lock);
 extern int cond_resched_softirq(void);
-static inline int cond_resched_bkl(void)
-{
-	return _cond_resched();
-}
+
+#define cond_resched_bkl()	cond_resched();

We might as well convert the one user of this ;-)

Quoted 25 lines
include/linux/sched.h +0 -0
 /*
  * Does a critical section need to be broken due to another
kernel/sched.c +1 -3
index 87ecac1..649ec92 100644
@@ -6605,9 +6605,6 @@ SYSCALL_DEFINE0(sched_yield)
 
 static void __cond_resched(void)
 {
-#ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
-	__might_sleep(__FILE__, __LINE__);
-#endif
 	/*
 	 * The BKS might be reacquired before we have dropped
 	 * PREEMPT_ACTIVE, which could trigger a second
@@ -6644,6 +6641,7 @@ int cond_resched_lock(spinlock_t *lock)
 
 	if (spin_needbreak(lock) || resched) {
 		spin_unlock(lock);
+		__might_sleep(__FILE__, __LINE__);
 		if (resched && need_resched())
 			__cond_resched();
 		else

kernel/sched.c int __sched cond_resched_softirq(void) · line 6665

Quoted 7 lines
kernel/sched.c +1 -0
@@ -6661,6 +6659,7 @@ int __sched cond_resched_softirq(void)
 
 	if (need_resched() && system_state == SYSTEM_RUNNING) {
 		local_bh_enable();
+		__might_sleep(__FILE__, __LINE__);
 		__cond_resched();
 		local_bh_disable();
 		return 1;

Right, how about renaming these to _cond_resched_{lock,softirq}, and added a __might_sleep() definition for !DEBUG_SPINLOCK_SLEEP and add macro wrappers to sched.c for these two as well?

#8 lore raw

Re: [PATCH] sched: Move the sleeping while atomic checks early in cond_resched()

Quoted 41 lines · Peter Zijlstra
Quoted 40 lines · Frederic Weisbecker

might_sleep() is called lately in cond_resched(), after the need_resched()/preempt enabled/system running tests are checked.

It's better to check the sleeps while atomic earlier and not depend on some environment datas that reduce the chances to detect a problem.

Changes in v2:
- call __might_sleep() directly instead of might_sleep() which may call cond_resched()
- turn cond_resched() into a macro so that the file:line couple reported refers to the caller of cond_resched() and not __cond_resched() itself.
- drop the obsolete CONFIG_PREEMPT_BKL related zones

---
 include/linux/sched.h |   22 +++++++---------------
 kernel/sched.c        |    5 ++---
 2 files changed, 9 insertions(+), 18 deletions(-)
include/linux/sched.h +0 -9
index 0cb0d8d..737f569 100644
@@ -2276,23 +2276,15 @@ static inline int need_resched(void)
  * cond_resched_softirq() will enable bhs before scheduling.
  */
 extern int _cond_resched(void);
-#ifdef CONFIG_PREEMPT_BKL
-static inline int cond_resched(void)
-{
-	return 0;
-}
-#else
-static inline int cond_resched(void)
-{
-	return _cond_resched();
Quoted 8 lines

include/linux/sched.h static inline int need_resched(void) · line 2282

Quoted 6 lines
include/linux/sched.h +4 -2
-}
-#endif
+#define cond_resched() ({			\
+	__might_sleep(__FILE__, __LINE__);	\
+	_cond_resched();			\
+})

I don't think this will compile for !CONFIG_DEBUG_SPINLOCK_SLEEP.

Ahh, right.

Quoted 10 lines

include/linux/sched.h static inline int need_resched(void) · line 2287

Quoted 8 lines
include/linux/sched.h +2 -4
 extern int cond_resched_lock(spinlock_t * lock);
 extern int cond_resched_softirq(void);
-static inline int cond_resched_bkl(void)
-{
-	return _cond_resched();
-}
+
+#define cond_resched_bkl()	cond_resched();

We might as well convert the one user of this ;-)

Ok :)

Quoted 26 lines
Quoted 26 lines
include/linux/sched.h +0 -0
 /*
  * Does a critical section need to be broken due to another
kernel/sched.c +1 -3
index 87ecac1..649ec92 100644
@@ -6605,9 +6605,6 @@ SYSCALL_DEFINE0(sched_yield)
 
 static void __cond_resched(void)
 {
-#ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
-	__might_sleep(__FILE__, __LINE__);
-#endif
 	/*
 	 * The BKS might be reacquired before we have dropped
 	 * PREEMPT_ACTIVE, which could trigger a second
@@ -6644,6 +6641,7 @@ int cond_resched_lock(spinlock_t *lock)
 
 	if (spin_needbreak(lock) || resched) {
 		spin_unlock(lock);
+		__might_sleep(__FILE__, __LINE__);
 		if (resched && need_resched())
 			__cond_resched();
 		else
@@ -6661,6 +6659,7 @@ int __sched cond_resched_softirq(void)
 
Quoted 10 lines

kernel/sched.c int __sched cond_resched_softirq(void) · line 6665

Quoted 6 lines
kernel/sched.c +1 -0
 	if (need_resched() && system_state == SYSTEM_RUNNING) {
 		local_bh_enable();
+		__might_sleep(__FILE__, __LINE__);
 		__cond_resched();
 		local_bh_disable();
 		return 1;

Right, how about renaming these to _cond_resched_{lock,softirq}, and added a __might_sleep() definition for !DEBUG_SPINLOCK_SLEEP and add macro wrappers to sched.c for these two as well?

I did that first but thought that might_sleep() would fail in a spinlock held or softirq context, right?

#9 lore raw

Re: [PATCH] sched: Move the sleeping while atomic checks early in cond_resched()

Quoted 6 lines · Frederic Weisbecker
Quoted 3 lines

Right, how about renaming these to _cond_resched_{lock,softirq}, and added a __might_sleep() definition for !DEBUG_SPINLOCK_SLEEP and add macro wrappers to sched.c for these two as well?

I did that first but thought that might_sleep() would fail in a spinlock held or softirq context, right?

Ah, right.. maybe we can add a preempt_count_offset parameter to __might_sleep() such that it will compensate for the pending spin_unlock()/local_bh_enable().

#10 lore raw

Re: [PATCH] sched: Move the sleeping while atomic checks early in cond_resched()

Quoted 6 lines · Peter Zijlstra
Quoted 4 lines · Frederic Weisbecker
Quoted 3 lines

Right, how about renaming these to _cond_resched_{lock,softirq}, and added a __might_sleep() definition for !DEBUG_SPINLOCK_SLEEP and add macro wrappers to sched.c for these two as well?

Quoted 6 lines
Quoted 2 lines

I did that first but thought that might_sleep() would fail in a spinlock held or softirq context, right?

Ah, right.. maybe we can add a preempt_count_offset parameter to __might_sleep() such that it will compensate for the pending spin_unlock()/local_bh_enable().

Good idea, I'm trying that.

Acked-by on v1 1/2
#11 lore raw
In reply to Frederic Weisbecker
Quoted 19 lines · Frederic Weisbecker

The schedule() function is a loop that reschedules the current task while the TIF_NEED_RESCHED flag is set:

void schedule(void)
{
need_resched:

	/* schedule code */
	if (need_resched())
		goto need_resched;
}

And cond_resched() repeat this loop:

do {
	add_preempt_count(PREEMPT_ACTIVE);
	schedule();
	sub_preempt_count(PREEMPT_ACTIVE);
} while(need_resched());
Quoted 7 lines

This loop is needless because schedule() already did the check and nothing can set TIF_NEED_RESCHED between schedule() exit and the loop check in need_resched().

Then remove this needless loop.

Quoted 23 lines
---
 kernel/sched.c |    8 +++-----
 1 files changed, 3 insertions(+), 5 deletions(-)
kernel/sched.c +3 -5
index 4d1e387..87ecac1 100644
@@ -6613,11 +6613,9 @@ static void __cond_resched(void)
 	 * PREEMPT_ACTIVE, which could trigger a second
 	 * cond_resched() call.
 	 */
-	do {
-		add_preempt_count(PREEMPT_ACTIVE);
-		schedule();
-		sub_preempt_count(PREEMPT_ACTIVE);
-	} while (need_resched());
+	add_preempt_count(PREEMPT_ACTIVE);
+	schedule();
+	sub_preempt_count(PREEMPT_ACTIVE);
 }
 
 int __sched _cond_resched(void)
#12 lore raw
In reply to Frederic Weisbecker
Quoted 6 lines · Frederic Weisbecker
kernel/sched.c +0 -0
@@ -6613,11 +6613,9 @@ static void __cond_resched(void)
         * PREEMPT_ACTIVE, which could trigger a second
         * cond_resched() call.
         */

kernel/sched.c static void __cond_resched(void) · line 6620

Quoted 10 lines
kernel/sched.c +3 -5
-       do {
-               add_preempt_count(PREEMPT_ACTIVE);
-               schedule();
-               sub_preempt_count(PREEMPT_ACTIVE);
-       } while (need_resched());
+       add_preempt_count(PREEMPT_ACTIVE);
+       schedule();
+       sub_preempt_count(PREEMPT_ACTIVE);
 }
 

If you drop the loop, then you should also remove the comment that explains why it was put there.

	Arnd <><
#13 lore raw
Quoted 21 lines · Arnd Bergmann
Quoted 6 lines · Frederic Weisbecker
kernel/sched.c +0 -0
@@ -6613,11 +6613,9 @@ static void __cond_resched(void)
         * PREEMPT_ACTIVE, which could trigger a second
         * cond_resched() call.
         */

kernel/sched.c static void __cond_resched(void) · line 6620

Quoted 10 lines
kernel/sched.c +3 -5
-       do {
-               add_preempt_count(PREEMPT_ACTIVE);
-               schedule();
-               sub_preempt_count(PREEMPT_ACTIVE);
-       } while (need_resched());
+       add_preempt_count(PREEMPT_ACTIVE);
+       schedule();
+       sub_preempt_count(PREEMPT_ACTIVE);
 }
 

If you drop the loop, then you should also remove the comment that explains why it was put there.

Quoted 1 line
	Arnd <><

Hmm, these comments seem to actually explain why we do the PREEMPT_ACTIVE trick, which is to prevent from cond_resched() recursion, right?

#14 lore raw
Quoted 23 lines · Frederic Weisbecker
Quoted 12 lines · Arnd Bergmann
Quoted 11 lines · Frederic Weisbecker
kernel/sched.c +0 -5
@@ -6613,11 +6613,9 @@ static void __cond_resched(void)
         * PREEMPT_ACTIVE, which could trigger a second
         * cond_resched() call.
         */
-       do {
-               add_preempt_count(PREEMPT_ACTIVE);
-               schedule();
-               sub_preempt_count(PREEMPT_ACTIVE);
-       } while (need_resched());
Quoted 9 lines

kernel/sched.c static void __cond_resched(void) · line 6620

Quoted 5 lines
kernel/sched.c +3 -0
+       add_preempt_count(PREEMPT_ACTIVE);
+       schedule();
+       sub_preempt_count(PREEMPT_ACTIVE);
 }
 

If you drop the loop, then you should also remove the comment that explains why it was put there.

Quoted 3 lines

Hmm, these comments seem to actually explain why we do the PREEMPT_ACTIVE trick, which is to prevent from cond_resched() recursion, right?

I think we both misinterpreted the comment, which seemed to refer to older code added by Ingo in 5bbcfd900 "cond_resched(): fix bogus might_sleep() warning" and removed by Andrew in e7b384043e2 "cond_resched() fix".

The original code in Ingos version looked like

static inline void __cond_resched(void)

 {
       /*
        * The BKS might be reacquired before we have dropped
        * PREEMPT_ACTIVE, which could trigger a second
        * cond_resched() call.
        */
       if (unlikely(preempt_count()))
               return;
       do {
               add_preempt_count(PREEMPT_ACTIVE);
               schedule();
	...

So, it's got nothing to do with the loop, but should still be removed because the 'if (unlikely(preempt_count()))' is no longer there.

	Arnd <><
#15 lore raw
Quoted 50 lines · Arnd Bergmann
Quoted 23 lines · Frederic Weisbecker
Quoted 12 lines · Arnd Bergmann
Quoted 11 lines · Frederic Weisbecker
kernel/sched.c +0 -5
@@ -6613,11 +6613,9 @@ static void __cond_resched(void)
         * PREEMPT_ACTIVE, which could trigger a second
         * cond_resched() call.
         */
-       do {
-               add_preempt_count(PREEMPT_ACTIVE);
-               schedule();
-               sub_preempt_count(PREEMPT_ACTIVE);
-       } while (need_resched());
Quoted 9 lines

kernel/sched.c static void __cond_resched(void) · line 6620

Quoted 5 lines
kernel/sched.c +3 -0
+       add_preempt_count(PREEMPT_ACTIVE);
+       schedule();
+       sub_preempt_count(PREEMPT_ACTIVE);
 }
 

If you drop the loop, then you should also remove the comment that explains why it was put there.

Quoted 3 lines

Hmm, these comments seem to actually explain why we do the PREEMPT_ACTIVE trick, which is to prevent from cond_resched() recursion, right?

I think we both misinterpreted the comment, which seemed to refer to older code added by Ingo in 5bbcfd900 "cond_resched(): fix bogus might_sleep() warning" and removed by Andrew in e7b384043e2 "cond_resched() fix".

The original code in Ingos version looked like

static inline void __cond_resched(void)

 {
       /*
        * The BKS might be reacquired before we have dropped
        * PREEMPT_ACTIVE, which could trigger a second
        * cond_resched() call.
        */
       if (unlikely(preempt_count()))
               return;
       do {
               add_preempt_count(PREEMPT_ACTIVE);
               schedule();
	...
Quoted 2 lines

So, it's got nothing to do with the loop, but should still be removed because the 'if (unlikely(preempt_count()))' is no longer there.

Yeah, but the comment still fits the code after this patch, don't you think? :-)

#16 lore raw
Quoted 51 lines · Frederic Weisbecker
Quoted 50 lines · Arnd Bergmann
Quoted 23 lines · Frederic Weisbecker
Quoted 12 lines · Arnd Bergmann
Quoted 11 lines · Frederic Weisbecker
kernel/sched.c +0 -5
@@ -6613,11 +6613,9 @@ static void __cond_resched(void)
         * PREEMPT_ACTIVE, which could trigger a second
         * cond_resched() call.
         */
-       do {
-               add_preempt_count(PREEMPT_ACTIVE);
-               schedule();
-               sub_preempt_count(PREEMPT_ACTIVE);
-       } while (need_resched());
Quoted 9 lines

kernel/sched.c static void __cond_resched(void) · line 6620

Quoted 5 lines
kernel/sched.c +3 -0
+       add_preempt_count(PREEMPT_ACTIVE);
+       schedule();
+       sub_preempt_count(PREEMPT_ACTIVE);
 }
 

If you drop the loop, then you should also remove the comment that explains why it was put there.

Quoted 3 lines

Hmm, these comments seem to actually explain why we do the PREEMPT_ACTIVE trick, which is to prevent from cond_resched() recursion, right?

I think we both misinterpreted the comment, which seemed to refer to older code added by Ingo in 5bbcfd900 "cond_resched(): fix bogus might_sleep() warning" and removed by Andrew in e7b384043e2 "cond_resched() fix".

The original code in Ingos version looked like

static inline void __cond_resched(void)

 {
       /*
        * The BKS might be reacquired before we have dropped
        * PREEMPT_ACTIVE, which could trigger a second
        * cond_resched() call.
        */
       if (unlikely(preempt_count()))
               return;
       do {
               add_preempt_count(PREEMPT_ACTIVE);
               schedule();
	...
Quoted 6 lines
Quoted 2 lines

So, it's got nothing to do with the loop, but should still be removed because the 'if (unlikely(preempt_count()))' is no longer there.

Yeah, but the comment still fits the code after this patch, don't you think? :-)

... except that there's no Big Kernel Semaphore anymore ;-)

Ingo

#17 lore raw
Quoted 58 lines · Ingo Molnar
Quoted 55 lines · Frederic Weisbecker
Quoted 52 lines · Arnd Bergmann
Quoted 23 lines · Frederic Weisbecker
Quoted 12 lines · Arnd Bergmann
Quoted 11 lines · Frederic Weisbecker
kernel/sched.c +0 -5
@@ -6613,11 +6613,9 @@ static void __cond_resched(void)
         * PREEMPT_ACTIVE, which could trigger a second
         * cond_resched() call.
         */
-       do {
-               add_preempt_count(PREEMPT_ACTIVE);
-               schedule();
-               sub_preempt_count(PREEMPT_ACTIVE);
-       } while (need_resched());
Quoted 9 lines

kernel/sched.c static void __cond_resched(void) · line 6620

Quoted 5 lines
kernel/sched.c +3 -0
+       add_preempt_count(PREEMPT_ACTIVE);
+       schedule();
+       sub_preempt_count(PREEMPT_ACTIVE);
 }
 

If you drop the loop, then you should also remove the comment that explains why it was put there.

Quoted 3 lines

Hmm, these comments seem to actually explain why we do the PREEMPT_ACTIVE trick, which is to prevent from cond_resched() recursion, right?

I think we both misinterpreted the comment, which seemed to refer to older code added by Ingo in 5bbcfd900 "cond_resched(): fix bogus might_sleep() warning" and removed by Andrew in e7b384043e2 "cond_resched() fix".

The original code in Ingos version looked like

static inline void __cond_resched(void)

 {
       /*
        * The BKS might be reacquired before we have dropped
        * PREEMPT_ACTIVE, which could trigger a second
        * cond_resched() call.
        */
       if (unlikely(preempt_count()))
               return;
       do {
               add_preempt_count(PREEMPT_ACTIVE);
               schedule();
	...

So, it's got nothing to do with the loop, but should still be removed because the 'if (unlikely(preempt_count()))' is no longer there.

Quoted 6 lines
Quoted 2 lines

Yeah, but the comment still fits the code after this patch, don't you think? :-)

... except that there's no Big Kernel Semaphore anymore ;-)

Ingo

Ah, I lack some backgrounds about Linux heroic ages :) I thought it was a mispell of BKL.

I guess the comment should be removed anyway, while reading it more, it doesn't explain the code that follows it.

#18 lore raw

PATCH 1/1 sched: Remove obsolete comment in __cond_resched()

In reply to Ingo Molnar

Remove the outdated comment from __cond_resched() related to the now removed Big Kernel Semaphore.

---
 kernel/sched.c |    5 -----
 1 files changed, 0 insertions(+), 5 deletions(-)
kernel/sched.c +0 -5
index 649ec92..7b2ab88 100644
@@ -6605,11 +6605,6 @@ SYSCALL_DEFINE0(sched_yield)
 
 static void __cond_resched(void)
 {
-	/*
-	 * The BKS might be reacquired before we have dropped
-	 * PREEMPT_ACTIVE, which could trigger a second
-	 * cond_resched() call.
-	 */
 	add_preempt_count(PREEMPT_ACTIVE);
 	schedule();
 	sub_preempt_count(PREEMPT_ACTIVE);
Signature · 1 line
1.6.2.3