Skip to contentSkip to conversation

sched: set TIF_NEED_RESCHED before calling __trace_set_need_resched()

PATCH Applied Sechang Lim 4 participants 6 messages Jun 30 – Oct 2 Scheduler

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 sched: set TIF_NEED_RESCHED before calling __trace_set_need_resched()

set_tsk_need_resched() tests TIF_NEED_RESCHED, calls __trace_set_need_resched() if the flag is clear, then sets it via set_tsk_thread_flag(). A BPF raw_tp program attached to sched_set_need_resched executes synchronously inside __bpf_trace_run(). On return, __bpf_trace_run() drops the RCU lock with rcu_read_unlock_migrate(), which on the preempt-or-BH-disabled path calls set_need_resched_current() -> set_tsk_need_resched() again.

set_tsk_thread_flag() follows the tracepoint call, so every re-entrant frame sees TIF_NEED_RESCHED clear and calls __trace_set_need_resched() again:

  BUG: TASK stack guard page was hit at ffffc9001224ff98
  Oops: stack guard page: 0000 [#1] SMP KASAN PTI
  RIP: 0010:__bpf_trace_sched_set_need_resched_tp+0x1c/0x190
  Call Trace:
   trace_sched_set_need_resched_tp+0x110/0x130
   set_tsk_need_resched include/linux/sched.h:2076
   set_need_resched_current include/linux/sched.h:2094
   rcu_read_unlock_special+0x43a/0x440
   __rcu_read_unlock+0x9e/0x120
   rcu_read_unlock_migrate+0xa9/0x240
   __bpf_trace_run+0x131/0x180
   bpf_trace_run3+0x333/0x430
   __bpf_trace_sched_set_need_resched_tp+0x13a/0x190
   trace_sched_set_need_resched_tp+0x110/0x130
   set_tsk_need_resched include/linux/sched.h:2076
   ...

__resched_curr() has the same ordering, firing the tracepoint before setting the flag via set_ti_thread_flag() or set_nr_and_not_polling(). Fix it for consistency.

Replace the separate test_tsk_thread_flag() + set_tsk_thread_flag() pair in set_tsk_need_resched() with test_and_set_tsk_thread_flag(). In __resched_curr(), move the tracepoint call after the flag is set in each path.

Fixes adcc3bfa8806 ("sched: Adapt sched tracepoints for RV task model")

---
v3:
- reorder need_ipi variable. (K Prateek Nayak)

v2:
- https://lore.kernel.org/all/[email protected]/

v1:
- https://lore.kernel.org/all/[email protected]/

 include/linux/sched.h | 5 ++---
 kernel/sched/core.c   | 7 +++++--
 2 files changed, 7 insertions(+), 5 deletions(-)
include/linux/sched.h +2 -3
index ee06cba5c6f5..c9efd08dae92 100644
@@ -2071,10 +2071,9 @@ static inline int test_tsk_thread_flag(struct task_struct *tsk, int flag)
 
 static inline void set_tsk_need_resched(struct task_struct *tsk)
 {
-	if (tracepoint_enabled(sched_set_need_resched_tp) &&
-	    !test_tsk_thread_flag(tsk, TIF_NEED_RESCHED))
+	if (!test_and_set_tsk_thread_flag(tsk, TIF_NEED_RESCHED) &&
+	    tracepoint_enabled(sched_set_need_resched_tp))
 		__trace_set_need_resched(tsk, TIF_NEED_RESCHED);
-	set_tsk_thread_flag(tsk,TIF_NEED_RESCHED);
 }
 
 static inline void clear_tsk_need_resched(struct task_struct *tsk)
kernel/sched/core.c +5 -2
index b8871449d3c6..19de28f0d85a 100644
@@ -1171,6 +1171,7 @@ static void __resched_curr(struct rq *rq, int tif)
 {
 	struct task_struct *curr = rq->curr;
 	struct thread_info *cti = task_thread_info(curr);
+	bool need_ipi;
 	int cpu;
 
 	lockdep_assert_rq_held(rq);
@@ -1187,15 +1188,17 @@ static void __resched_curr(struct rq *rq, int tif)
 
 	cpu = cpu_of(rq);
 
-	trace_sched_set_need_resched_tp(curr, cpu, tif);
 	if (cpu == smp_processor_id()) {
 		set_ti_thread_flag(cti, tif);
 		if (tif == TIF_NEED_RESCHED)
 			set_preempt_need_resched();
+		trace_sched_set_need_resched_tp(curr, cpu, tif);
 		return;
 	}
 
-	if (set_nr_and_not_polling(cti, tif)) {
+	need_ipi = set_nr_and_not_polling(cti, tif);
+	trace_sched_set_need_resched_tp(curr, cpu, tif);
+	if (need_ipi) {
 		if (tif == TIF_NEED_RESCHED)
 			smp_send_reschedule(cpu);
 	} else {
Signature · 1 line
2.43.0
Acked-by on v3 1/1
#1 lore raw
Quoted 33 lines · Sechang Lim

set_tsk_need_resched() tests TIF_NEED_RESCHED, calls __trace_set_need_resched() if the flag is clear, then sets it via set_tsk_thread_flag().  A BPF raw_tp program attached to sched_set_need_resched executes synchronously inside __bpf_trace_run(). On return, __bpf_trace_run() drops the RCU lock with rcu_read_unlock_migrate(), which on the preempt-or-BH-disabled path calls set_need_resched_current() -> set_tsk_need_resched() again.

set_tsk_thread_flag() follows the tracepoint call, so every re-entrant frame sees TIF_NEED_RESCHED clear and calls __trace_set_need_resched() again:

  BUG: TASK stack guard page was hit at ffffc9001224ff98
  Oops: stack guard page: 0000 [#1] SMP KASAN PTI
  RIP: 0010:__bpf_trace_sched_set_need_resched_tp+0x1c/0x190
  Call Trace:
   trace_sched_set_need_resched_tp+0x110/0x130
   set_tsk_need_resched include/linux/sched.h:2076
   set_need_resched_current include/linux/sched.h:2094
   rcu_read_unlock_special+0x43a/0x440
   __rcu_read_unlock+0x9e/0x120
   rcu_read_unlock_migrate+0xa9/0x240
   __bpf_trace_run+0x131/0x180
   bpf_trace_run3+0x333/0x430
   __bpf_trace_sched_set_need_resched_tp+0x13a/0x190
   trace_sched_set_need_resched_tp+0x110/0x130
   set_tsk_need_resched include/linux/sched.h:2076

   ...

__resched_curr() has the same ordering, firing the tracepoint before setting the flag via set_ti_thread_flag() or set_nr_and_not_polling(). Fix it for consistency.

Quoted 8 lines

Replace the separate test_tsk_thread_flag() + set_tsk_thread_flag() pair in set_tsk_need_resched() with test_and_set_tsk_thread_flag().  In __resched_curr(), move the tracepoint call after the flag is set in each path.

Fixes adcc3bfa8806 ("sched: Adapt sched tracepoints for RV task model")

---

Works fine with the existing rv monitors, thanks!

Quoted 63 lines

v3:
 - reorder need_ipi variable. (K Prateek Nayak)

v2:
 - https://lore.kernel.org/all/[email protected]/

v1:
 - https://lore.kernel.org/all/[email protected]/

 include/linux/sched.h | 5 ++---
 kernel/sched/core.c   | 7 +++++--

 2 files changed, 7 insertions(+), 5 deletions(-)
include/linux/sched.h +0 -0
index ee06cba5c6f5..c9efd08dae92 100644
@@ -2071,10 +2071,9 @@ static inline int test_tsk_thread_flag(struct
 task_struct *tsk, int flag)


 static inline void set_tsk_need_resched(struct task_struct *tsk) {

include/linux/sched.h +2 -2
-	if (tracepoint_enabled(sched_set_need_resched_tp) &&
-	    !test_tsk_thread_flag(tsk, TIF_NEED_RESCHED))
+	if (!test_and_set_tsk_thread_flag(tsk, TIF_NEED_RESCHED) &&
+	    tracepoint_enabled(sched_set_need_resched_tp))

  __trace_set_need_resched(tsk, TIF_NEED_RESCHED);

include/linux/sched.h +0 -1
-	set_tsk_thread_flag(tsk,TIF_NEED_RESCHED);

 }

 static inline void clear_tsk_need_resched(struct task_struct *tsk)

kernel/sched/core.c +1 -0
index b8871449d3c6..19de28f0d85a 100644
@@ -1171,6 +1171,7 @@ static void __resched_curr(struct rq *rq, int tif)
  {
  	struct task_struct *curr = rq->curr;
  	struct thread_info *cti = task_thread_info(curr);
+	bool need_ipi;
  	int cpu;


  lockdep_assert_rq_held(rq);

kernel/sched/core.c +0 -0
@@ -1187,15 +1188,17 @@ static void __resched_curr(struct rq *rq, int tif)


  cpu = cpu_of(rq);

kernel/sched/core.c +0 -1
-	trace_sched_set_need_resched_tp(curr, cpu, tif);

  if (cpu == smp_processor_id()) { set_ti_thread_flag(cti, tif); if (tif == TIF_NEED_RESCHED) set_preempt_need_resched();

kernel/sched/core.c +1 -0
+		trace_sched_set_need_resched_tp(curr, cpu, tif);

  return;
  }

kernel/sched/core.c +3 -1
-	if (set_nr_and_not_polling(cti, tif)) {
+	need_ipi = set_nr_and_not_polling(cti, tif);
+	trace_sched_set_need_resched_tp(curr, cpu, tif);
+	if (need_ipi) {

  if (tif == TIF_NEED_RESCHED)
  smp_send_reschedule(cpu);
  } else {

#2 lore raw
In reply to Sechang Lim

Hi Sechang,

Quoted 29 lines · Sechang Lim

set_tsk_need_resched() tests TIF_NEED_RESCHED, calls __trace_set_need_resched() if the flag is clear, then sets it via set_tsk_thread_flag(). A BPF raw_tp program attached to sched_set_need_resched executes synchronously inside __bpf_trace_run(). On return, __bpf_trace_run() drops the RCU lock with rcu_read_unlock_migrate(), which on the preempt-or-BH-disabled path calls set_need_resched_current() -> set_tsk_need_resched() again.

set_tsk_thread_flag() follows the tracepoint call, so every re-entrant frame sees TIF_NEED_RESCHED clear and calls __trace_set_need_resched() again:

  BUG: TASK stack guard page was hit at ffffc9001224ff98
  Oops: stack guard page: 0000 [#1] SMP KASAN PTI
  RIP: 0010:__bpf_trace_sched_set_need_resched_tp+0x1c/0x190
  Call Trace:
   trace_sched_set_need_resched_tp+0x110/0x130
   set_tsk_need_resched include/linux/sched.h:2076
   set_need_resched_current include/linux/sched.h:2094
   rcu_read_unlock_special+0x43a/0x440
   __rcu_read_unlock+0x9e/0x120
   rcu_read_unlock_migrate+0xa9/0x240
   __bpf_trace_run+0x131/0x180
   bpf_trace_run3+0x333/0x430
   __bpf_trace_sched_set_need_resched_tp+0x13a/0x190
   trace_sched_set_need_resched_tp+0x110/0x130
   set_tsk_need_resched include/linux/sched.h:2076
   ...
Quoted 8 lines

__resched_curr() has the same ordering, firing the tracepoint before setting the flag via set_ti_thread_flag() or set_nr_and_not_polling(). Fix it for consistency.

Replace the separate test_tsk_thread_flag() + set_tsk_thread_flag() pair in set_tsk_need_resched() with test_and_set_tsk_thread_flag(). In __resched_curr(), move the tracepoint call after the flag is set in each path.

This leaves the race Prateek described in the v2 discussion: a remote CPU sets TIF_NEED_RESCHED under rq->lock, but the target CPU can emit sched_entry_tp() before acquiring that lock, ahead of the need-resched tracepoint.

I reproduced it with this applied to tip/master. With the RV nrp monitor enabled, 300 runs of "perf bench sched messaging -g 20 -l 100" produced:

rv: monitor nrp does not allow event schedule_entry_preempt on state any_thread_running

Applying the following on top fixed the ordering, the same 300-run test then completed without an RV violation. If it makes sense, could you fold this change into v4?

Thanks,
-Andrea

kernel/sched/core.c +3 -3
index 639b5df7cf130..91a15a4234280 100644
@@ -7143,9 +7143,6 @@ static void __sched notrace __schedule(int sched_mode)
 	struct rq *rq;
 	int cpu;
 
-	/* Trace preemptions consistently with task switches */
-	trace_sched_entry_tp(sched_mode == SM_PREEMPT);
-
 	cpu = smp_processor_id();
 	rq = cpu_rq(cpu);
 	prev = rq->curr;
@@ -7178,6 +7175,9 @@ static void __sched notrace __schedule(int sched_mode)
 	rq_lock(rq, &rf);
 	smp_mb__after_spinlock();
 
+	/* Trace preemptions consistently with task switches */
+	trace_sched_entry_tp(sched_mode == SM_PREEMPT);
+
 	hrtick_schedule_enter(rq);
 
 	/* Promote REQ to ACT */
Quoted 69 lines
Fixes adcc3bfa8806 ("sched: Adapt sched tracepoints for RV task model")

---
v3:
- reorder need_ipi variable. (K Prateek Nayak)

v2:
- https://lore.kernel.org/all/[email protected]/

v1:
- https://lore.kernel.org/all/[email protected]/

 include/linux/sched.h | 5 ++---
 kernel/sched/core.c   | 7 +++++--
 2 files changed, 7 insertions(+), 5 deletions(-)
include/linux/sched.h +2 -3
index ee06cba5c6f5..c9efd08dae92 100644
@@ -2071,10 +2071,9 @@ static inline int test_tsk_thread_flag(struct task_struct *tsk, int flag)
 
 static inline void set_tsk_need_resched(struct task_struct *tsk)
 {
-	if (tracepoint_enabled(sched_set_need_resched_tp) &&
-	    !test_tsk_thread_flag(tsk, TIF_NEED_RESCHED))
+	if (!test_and_set_tsk_thread_flag(tsk, TIF_NEED_RESCHED) &&
+	    tracepoint_enabled(sched_set_need_resched_tp))
 		__trace_set_need_resched(tsk, TIF_NEED_RESCHED);
-	set_tsk_thread_flag(tsk,TIF_NEED_RESCHED);
 }
 
 static inline void clear_tsk_need_resched(struct task_struct *tsk)
kernel/sched/core.c +5 -2
index b8871449d3c6..19de28f0d85a 100644
@@ -1171,6 +1171,7 @@ static void __resched_curr(struct rq *rq, int tif)
 {
 	struct task_struct *curr = rq->curr;
 	struct thread_info *cti = task_thread_info(curr);
+	bool need_ipi;
 	int cpu;
 
 	lockdep_assert_rq_held(rq);
@@ -1187,15 +1188,17 @@ static void __resched_curr(struct rq *rq, int tif)
 
 	cpu = cpu_of(rq);
 
-	trace_sched_set_need_resched_tp(curr, cpu, tif);
 	if (cpu == smp_processor_id()) {
 		set_ti_thread_flag(cti, tif);
 		if (tif == TIF_NEED_RESCHED)
 			set_preempt_need_resched();
+		trace_sched_set_need_resched_tp(curr, cpu, tif);
 		return;
 	}
 
-	if (set_nr_and_not_polling(cti, tif)) {
+	need_ipi = set_nr_and_not_polling(cti, tif);
+	trace_sched_set_need_resched_tp(curr, cpu, tif);
+	if (need_ipi) {
 		if (tif == TIF_NEED_RESCHED)
 			smp_send_reschedule(cpu);
 	} else {
Signature · 1 line
2.43.0
#3 lore raw

Hi Andrea,

Quoted 10 lines · Andrea Righi

This leaves the race Prateek described in the v2 discussion: a remote CPU sets TIF_NEED_RESCHED under rq->lock, but the target CPU can emit sched_entry_tp() before acquiring that lock, ahead of the need-resched tracepoint.

I reproduced it with this applied to tip/master. With the RV nrp monitor enabled, 300 runs of "perf bench sched messaging -g 20 -l 100" produced:

  rv: monitor nrp does not allow event schedule_entry_preempt on state any_thread_running

Quoted 6 lines

Applying the following on top fixed the ordering, the same 300-run test then completed without an RV violation. If it makes sense, could you fold this change into v4?

Thanks,
-Andrea

thanks for looking into this. Is this change required for anything else besides fixing the nrp monitor after changing the order with need_resched?

I believe this would break the sts monitor which expects sched_entry before disabling interrupts.

Both monitors could be adapted to either case, but if your change is just for the sake of nrp, I think it's easier to just allow this race, since nrp is already allowing the race with interrupts.
I haven't tested yet, but something like allowing a sched_entry_preempt without need_resched set but provided it's going to be set before sched_exit would probably do (it'd allow also independent need_resched there, but those will then need their own preemption too).

I'd say if you don't need to move sched_entry for other reasons we can wait to apply this change until I see what's better for the models.

Thanks,
Gabriele

Quoted 97 lines
kernel/sched/core.c +0 -0
index 639b5df7cf130..91a15a4234280 100644
@@ -7143,9 +7143,6 @@ static void __sched notrace __schedule(int sched_mode)
  	struct rq *rq;
  	int cpu;

kernel/sched/core.c +0 -3
-	/* Trace preemptions consistently with task switches */
-	trace_sched_entry_tp(sched_mode == SM_PREEMPT);
-

  cpu = smp_processor_id();
  rq = cpu_rq(cpu);
  prev = rq->curr;

kernel/sched/core.c +0 -0
@@ -7178,6 +7175,9 @@ static void __sched notrace __schedule(int sched_mode)
  	rq_lock(rq, &rf);
  	smp_mb__after_spinlock();

kernel/sched/core.c +3 -0
+	/* Trace preemptions consistently with task switches */
+	trace_sched_entry_tp(sched_mode == SM_PREEMPT);
+

  hrtick_schedule_enter(rq);

  /* Promote REQ to ACT */

Quoted 70 lines
Fixes adcc3bfa8806 ("sched: Adapt sched tracepoints for RV task model")

---
v3:
 - reorder need_ipi variable. (K Prateek Nayak)

v2:
 - https://lore.kernel.org/all/[email protected]/

v1:
 - https://lore.kernel.org/all/[email protected]/

 include/linux/sched.h | 5 ++---
 kernel/sched/core.c   | 7 +++++--

 2 files changed, 7 insertions(+), 5 deletions(-)
include/linux/sched.h +0 -0
index ee06cba5c6f5..c9efd08dae92 100644
@@ -2071,10 +2071,9 @@ static inline int test_tsk_thread_flag(struct
 task_struct *tsk, int flag)


 static inline void set_tsk_need_resched(struct task_struct *tsk) {

include/linux/sched.h +2 -2
-	if (tracepoint_enabled(sched_set_need_resched_tp) &&
-	    !test_tsk_thread_flag(tsk, TIF_NEED_RESCHED))
+	if (!test_and_set_tsk_thread_flag(tsk, TIF_NEED_RESCHED) &&
+	    tracepoint_enabled(sched_set_need_resched_tp))

  __trace_set_need_resched(tsk, TIF_NEED_RESCHED);

include/linux/sched.h +0 -1
-	set_tsk_thread_flag(tsk,TIF_NEED_RESCHED);

 }

 static inline void clear_tsk_need_resched(struct task_struct *tsk)

kernel/sched/core.c +1 -0
index b8871449d3c6..19de28f0d85a 100644
@@ -1171,6 +1171,7 @@ static void __resched_curr(struct rq *rq, int tif)
  {
  	struct task_struct *curr = rq->curr;
  	struct thread_info *cti = task_thread_info(curr);
+	bool need_ipi;
  	int cpu;


  lockdep_assert_rq_held(rq);

kernel/sched/core.c +0 -0
@@ -1187,15 +1188,17 @@ static void __resched_curr(struct rq *rq, int tif)


  cpu = cpu_of(rq);

kernel/sched/core.c +0 -1
-	trace_sched_set_need_resched_tp(curr, cpu, tif);

  if (cpu == smp_processor_id()) { set_ti_thread_flag(cti, tif); if (tif == TIF_NEED_RESCHED) set_preempt_need_resched();

kernel/sched/core.c +1 -0
+		trace_sched_set_need_resched_tp(curr, cpu, tif);

  return;
  }

kernel/sched/core.c +3 -1
-	if (set_nr_and_not_polling(cti, tif)) {
+	need_ipi = set_nr_and_not_polling(cti, tif);
+	trace_sched_set_need_resched_tp(curr, cpu, tif);
+	if (need_ipi) {

  if (tif == TIF_NEED_RESCHED)
  smp_send_reschedule(cpu);
  } else {

Signature · 1 line
2.43.0
Reviewed-by
#4 lore raw

Hi Gabriele,

Quoted 17 lines · Gabriele Monaco

Hi Andrea,

Quoted 14 lines · Andrea Righi

This leaves the race Prateek described in the v2 discussion: a remote CPU sets TIF_NEED_RESCHED under rq->lock, but the target CPU can emit sched_entry_tp() before acquiring that lock, ahead of the need-resched tracepoint.

I reproduced it with this applied to tip/master. With the RV nrp monitor enabled, 300 runs of "perf bench sched messaging -g 20 -l 100" produced:

  rv: monitor nrp does not allow event schedule_entry_preempt on state any_thread_running

Applying the following on top fixed the ordering, the same 300-run test then completed without an RV violation. If it makes sense, could you fold this change into v4?

Quoted 5 lines
Quoted 2 lines

Thanks,
-Andrea

thanks for looking into this. Is this change required for anything else besides fixing the nrp monitor after changing the order with need_resched?

No, I was mainly trying to help get this fix upstream: a sched_ext kselftest I'm working on hits that issue and I'm holding off on submitting the test until the fix lands. Moving sched_entry was only my attempt to address the nrp warning I saw while testing the fix.

Quoted 12 lines

I believe this would break the sts monitor which expects sched_entry before disabling interrupts.

Both monitors could be adapted to either case, but if your change is just for the sake of nrp, I think it's easier to just allow this race, since nrp is already allowing the race with interrupts.
I haven't tested yet, but something like allowing a sched_entry_preempt without need_resched set but provided it's going to be set before sched_exit would probably do (it'd allow also independent need_resched there, but those will then need their own preemption too).

Quoted 2 lines

I'd say if you don't need to move sched_entry for other reasons we can wait to apply this change until I see what's better for the models.

That makes sense, we can ignore the sched_entry change for now. With that, Sechang's fix looks good to me.

Thanks,
-Andrea

merge bot Applied
#5 lore raw

[tip: sched/core] sched: Set TIF_NEED_RESCHED before calling __trace_set_need_resched()

In reply to Sechang Lim
The following commit has been merged into the sched/core branch of tip:

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

Commit-ID:     53bc5c556b82a3ca32b62ba349c4511f91526b9f
Gitweb:        https://git.kernel.org/tip/53bc5c556b82a3ca32b62ba349c4511f91526b9f
Author:        Sechang Lim <[email protected]>
AuthorDate:    Tue, 30 Jun 2026 08:47:37 
Committer:     Peter Zijlstra <[email protected]>
CommitterDate: Thu, 01 Oct 2026 14:00:34 +02:00

sched: Set TIF_NEED_RESCHED before calling __trace_set_need_resched()

set_tsk_need_resched() tests TIF_NEED_RESCHED, calls __trace_set_need_resched() if the flag is clear, then sets it via set_tsk_thread_flag(). A BPF raw_tp program attached to sched_set_need_resched executes synchronously inside __bpf_trace_run(). On return, __bpf_trace_run() drops the RCU lock with rcu_read_unlock_migrate(), which on the preempt-or-BH-disabled path calls set_need_resched_current() -> set_tsk_need_resched() again.

set_tsk_thread_flag() follows the tracepoint call, so every re-entrant frame sees TIF_NEED_RESCHED clear and calls __trace_set_need_resched() again:

  BUG: TASK stack guard page was hit at ffffc9001224ff98
  Oops: stack guard page: 0000 [#1] SMP KASAN PTI
  RIP: 0010:__bpf_trace_sched_set_need_resched_tp+0x1c/0x190
  Call Trace:
   trace_sched_set_need_resched_tp+0x110/0x130
   set_tsk_need_resched include/linux/sched.h:2076
   set_need_resched_current include/linux/sched.h:2094
   rcu_read_unlock_special+0x43a/0x440
   __rcu_read_unlock+0x9e/0x120
   rcu_read_unlock_migrate+0xa9/0x240
   __bpf_trace_run+0x131/0x180
   bpf_trace_run3+0x333/0x430
   __bpf_trace_sched_set_need_resched_tp+0x13a/0x190
   trace_sched_set_need_resched_tp+0x110/0x130
   set_tsk_need_resched include/linux/sched.h:2076
   ...

__resched_curr() has the same ordering, firing the tracepoint before setting the flag via set_ti_thread_flag() or set_nr_and_not_polling(). Fix it for consistency.

Replace the separate test_tsk_thread_flag() + set_tsk_thread_flag() pair in set_tsk_need_resched() with test_and_set_tsk_thread_flag(). In __resched_curr(), move the tracepoint call after the flag is set in each path.

---
 include/linux/sched.h | 5 ++---
 kernel/sched/core.c   | 7 +++++--
 2 files changed, 7 insertions(+), 5 deletions(-)
include/linux/sched.h +2 -3
index 48639b0..7b91cae 100644
@@ -2105,10 +2105,9 @@ static inline int test_tsk_thread_flag(struct task_struct *tsk, int flag)
 
 static inline void set_tsk_need_resched(struct task_struct *tsk)
 {
-	if (tracepoint_enabled(sched_set_need_resched_tp) &&
-	    !test_tsk_thread_flag(tsk, TIF_NEED_RESCHED))
+	if (!test_and_set_tsk_thread_flag(tsk, TIF_NEED_RESCHED) &&
+	    tracepoint_enabled(sched_set_need_resched_tp))
 		__trace_set_need_resched(tsk, TIF_NEED_RESCHED);
-	set_tsk_thread_flag(tsk,TIF_NEED_RESCHED);
 }
 
 static inline void clear_tsk_need_resched(struct task_struct *tsk)
kernel/sched/core.c +5 -2
index 23677d7..71d3c94 100644
@@ -1196,6 +1196,7 @@ static void __resched_curr(struct rq *rq, int tif)
 {
 	struct task_struct *curr = rq->curr;
 	struct thread_info *cti = task_thread_info(curr);
+	bool need_ipi;
 	int cpu;
 
 	lockdep_assert_rq_held(rq);
@@ -1212,15 +1213,17 @@ static void __resched_curr(struct rq *rq, int tif)
 
 	cpu = cpu_of(rq);
 
-	trace_sched_set_need_resched_tp(curr, cpu, tif);
 	if (cpu == smp_processor_id()) {
 		set_ti_thread_flag(cti, tif);
 		if (tif == TIF_NEED_RESCHED)
 			set_preempt_need_resched();
+		trace_sched_set_need_resched_tp(curr, cpu, tif);
 		return;
 	}
 
-	if (set_nr_and_not_polling(cti, tif)) {
+	need_ipi = set_nr_and_not_polling(cti, tif);
+	trace_sched_set_need_resched_tp(curr, cpu, tif);
+	if (need_ipi) {
 		if (tif == TIF_NEED_RESCHED)
 			smp_send_reschedule(cpu);
 	} else {