Skip to contentSkip to conversation

x86/entry: Tiny idtentry_exit_cond_resched() cleanup

PATCH Superseded Sebastian Andrzej Siewior 2 participants 8 messages Jun 30, 2020 – Jul 1, 2020

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover x86/entry: Tiny idtentry_exit_cond_resched() cleanup

Tiny two patch series against idtentry_exit_cond_resched() after I noticed it.

Sebastian

#1 lore raw

PATCH 1/2 x86/entry: Use should_resched() in idtentry_exit_cond_resched()

The TIF_NEED_RESCHED bit is inlined on x86 into the preemption counter. So instead checking the preemption counter against zero via preempt_count() and later checking the TIF_NEED_RESCHED bit via need_resched() we could use should_resched() which does both checks in one go.
The functional difference is that we don't enter the if statement with preempt_count == 0 and TIF_NEED_RESCHED not set.

Use should_resched() instead need_resched() + preempt_count().

---
 arch/x86/entry/common.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
arch/x86/entry/common.c +2 -3
index bd3f14175193c..212382f61b8e4 100644
@@ -612,13 +612,12 @@ bool noinstr idtentry_enter_cond_rcu(struct pt_regs *regs)
 
 static void idtentry_exit_cond_resched(struct pt_regs *regs, bool may_sched)
 {
-	if (may_sched && !preempt_count()) {
+	if (may_sched && should_resched(0)) {
 		/* Sanity check RCU and thread stack */
 		rcu_irq_exit_check_preempt();
 		if (IS_ENABLED(CONFIG_DEBUG_ENTRY))
 			WARN_ON_ONCE(!on_thread_stack());
-		if (need_resched())
-			preempt_schedule_irq();
+		preempt_schedule_irq();
 	}
 	/* Covers both tracing and lockdep */
 	trace_hardirqs_on();
Signature · 1 line
2.27.0
#2 lore raw

Re: [PATCH 1/2] x86/entry: Use should_resched() in idtentry_exit_cond_resched()

Quoted 20 lines · Sebastian Andrzej Siewior

The TIF_NEED_RESCHED bit is inlined on x86 into the preemption counter. So instead checking the preemption counter against zero via preempt_count() and later checking the TIF_NEED_RESCHED bit via need_resched() we could use should_resched() which does both checks in one go.
The functional difference is that we don't enter the if statement with preempt_count == 0 and TIF_NEED_RESCHED not set.

Use should_resched() instead need_resched() + preempt_count().

---
 arch/x86/entry/common.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
arch/x86/entry/common.c +0 -0
index bd3f14175193c..212382f61b8e4 100644

arch/x86/entry/common.c bool noinstr idtentry_enter_cond_rcu(struct pt_regs *regs) · line 619

Quoted 9 lines
arch/x86/entry/common.c +1 -1
@@ -612,13 +612,12 @@ bool noinstr idtentry_enter_cond_rcu(struct pt_regs *regs)
 
 static void idtentry_exit_cond_resched(struct pt_regs *regs, bool may_sched)
 {
-	if (may_sched && !preempt_count()) {
+	if (may_sched && should_resched(0)) {
 		/* Sanity check RCU and thread stack */
 		rcu_irq_exit_check_preempt();
 		if (IS_ENABLED(CONFIG_DEBUG_ENTRY))
 			WARN_ON_ONCE(!on_thread_stack());

This was done on purpose, your change avoids hitting this WARN.

The thing is, if we could preempt (but not nessecarily have to) we want to validate we're on the thread stack.

Quoted 9 lines
arch/x86/entry/common.c +1 -2
-		if (need_resched())
-			preempt_schedule_irq();
+		preempt_schedule_irq();
 	}
 	/* Covers both tracing and lockdep */
 	trace_hardirqs_on();
Signature · 1 line
2.27.0
#3 lore raw

Re: [PATCH 1/2] x86/entry: Use should_resched() in idtentry_exit_cond_resched()

Quoted 15 lines · Peter Zijlstra

arch/x86/entry/common.c bool noinstr idtentry_enter_cond_rcu(struct pt_regs *regs) · line 619

Quoted 12 lines
arch/x86/entry/common.c +1 -1
@@ -612,13 +612,12 @@ bool noinstr idtentry_enter_cond_rcu(struct pt_regs *regs)
 
 static void idtentry_exit_cond_resched(struct pt_regs *regs, bool may_sched)
 {
-	if (may_sched && !preempt_count()) {
+	if (may_sched && should_resched(0)) {
 		/* Sanity check RCU and thread stack */
 		rcu_irq_exit_check_preempt();
 		if (IS_ENABLED(CONFIG_DEBUG_ENTRY))
 			WARN_ON_ONCE(!on_thread_stack());

This was done on purpose, your change avoids hitting this WARN.

Quoted 2 lines

The thing is, if we could preempt (but not nessecarily have to) we want to validate we're on the thread stack.

Okay then.

Sebastian

#4 lore raw

PATCH v2 1/1 x86/entry: Use should_resched() in idtentry_exit_cond_resched()

The TIF_NEED_RESCHED bit is inlined on x86 into the preemption counter. By using should_resched(0) instead of need_resched() the same check can be performed which uses the same variable as 'preempt_count()` which was issued before.

Use should_resched(0) instead need_resched().

---
v1…v2: Use should_resched() but keep the !preempt_count() check.

 arch/x86/entry/common.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/entry/common.c +1 -1
index bd3f14175193c..0bb6966f62738 100644
@@ -615,11 +615,11 @@ static void idtentry_exit_cond_resched(struct pt_regs *regs, bool may_sched)
 	if (may_sched && !preempt_count()) {
 		/* Sanity check RCU and thread stack */
 		rcu_irq_exit_check_preempt();
 		if (IS_ENABLED(CONFIG_DEBUG_ENTRY))
 			WARN_ON_ONCE(!on_thread_stack());
-		if (need_resched())
+		if (should_resched(0))
 			preempt_schedule_irq();
 	}
 	/* Covers both tracing and lockdep */
 	trace_hardirqs_on();
 }
Signature · 1 line
2.27.0
#5 lore raw

PATCH 2/2 x86/entry: Remove `regs' parameter from idtentry_exit_cond_resched()

In reply to Sebastian Andrzej Siewior

The `regs' parameter is not used in idtentry_exit_cond_resched() and can be removed.

Remove the `regs' parameter.

---
 arch/x86/entry/common.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
arch/x86/entry/common.c +3 -3
index 212382f61b8e4..652dce37643b4 100644
@@ -610,7 +610,7 @@ bool noinstr idtentry_enter_cond_rcu(struct pt_regs *regs)
 	return false;
 }
 
-static void idtentry_exit_cond_resched(struct pt_regs *regs, bool may_sched)
+static void idtentry_exit_cond_resched(bool may_sched)
 {
 	if (may_sched && should_resched(0)) {
 		/* Sanity check RCU and thread stack */
@@ -664,7 +664,7 @@ void noinstr idtentry_exit_cond_rcu(struct pt_regs *regs, bool rcu_exit)
 		}
 
 		instrumentation_begin();
-		idtentry_exit_cond_resched(regs, IS_ENABLED(CONFIG_PREEMPTION));
+		idtentry_exit_cond_resched(IS_ENABLED(CONFIG_PREEMPTION));
 		instrumentation_end();
 	} else {
 		/*
@@ -770,7 +770,7 @@ __visible noinstr void xen_pv_evtchn_do_upcall(struct pt_regs *regs)
 	inhcall = get_and_clear_inhcall();
 	if (inhcall && !WARN_ON_ONCE(rcu_exit)) {
 		instrumentation_begin();
-		idtentry_exit_cond_resched(regs, true);
+		idtentry_exit_cond_resched(true);
 		instrumentation_end();
 		restore_inhcall(inhcall);
 	} else {
Signature · 1 line
2.27.0
#6 lore raw

Re: [PATCH 2/2] x86/entry: Remove `regs' parameter from idtentry_exit_cond_resched()

Quoted 15 lines · Sebastian Andrzej Siewior

The `regs' parameter is not used in idtentry_exit_cond_resched() and can be removed.

Remove the `regs' parameter.

---
 arch/x86/entry/common.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
arch/x86/entry/common.c +0 -0
index 212382f61b8e4..652dce37643b4 100644

arch/x86/entry/common.c bool noinstr idtentry_enter_cond_rcu(struct pt_regs *regs) · line 613

Quoted 5 lines
arch/x86/entry/common.c +1 -1
@@ -610,7 +610,7 @@ bool noinstr idtentry_enter_cond_rcu(struct pt_regs *regs)
 	return false;
 }
 
-static void idtentry_exit_cond_resched(struct pt_regs *regs, bool may_sched)
+static void idtentry_exit_cond_resched(bool may_sched)

This is a style question; so far all idtentry_*() functions have regs passed. In the lockdep-vs-nmi series I introduce idtentry_{enter,exit}_nmi() both having an unused regs argument.

Not sure which way around I feel on this.

#7 lore raw

Re: [PATCH 2/2] x86/entry: Remove `regs' parameter from idtentry_exit_cond_resched()

Quoted 8 lines · Peter Zijlstra

arch/x86/entry/common.c bool noinstr idtentry_enter_cond_rcu(struct pt_regs *regs) · line 613

Quoted 2 lines
arch/x86/entry/common.c +1 -1
-static void idtentry_exit_cond_resched(struct pt_regs *regs, bool may_sched)
+static void idtentry_exit_cond_resched(bool may_sched)

This is a style question; so far all idtentry_*() functions have regs passed. In the lockdep-vs-nmi series I introduce idtentry_{enter,exit}_nmi() both having an unused regs argument.

Not sure which way around I feel on this.

Do as you are pleased. I was more attached to the previous patch that to this one :)

Sebastian