Skip to contentSkip to conversation

x86/alternatives: Add cond_resched() to text_poke_bp_batch()

PATCH Superseded Steven Rostedt 3 participants 6 messages May 28, 2023 – May 31, 2023 x86

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/alternatives: Add cond_resched() to text_poke_bp_batch()

From: "Steven Rostedt (Google)" <[email protected]>

Debugging in the kernel has started slowing down the kernel by a noticeable amount. The ftrace start up tests are triggering the softlockup watchdog on some boxes. This is caused by the start up tests that enable function and function graph tracing several times. Sprinkling cond_resched() just in the start up test code was not enough to stop the softlockup from triggering. It would sometimes trigger in the text_poke_bp_batch() code.

The text_poke_bp_batch() is run in schedulable context. Add cond_resched() between each phase (adding the int3, updating the code, and removing the int3). This keeps the softlockup from triggering in the start up tests.

---
 arch/x86/kernel/alternative.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
arch/x86/kernel/alternative.c +12 -1
index f615e0cb6d93..e024eddd457f 100644
@@ -1953,6 +1953,14 @@ static void text_poke_bp_batch(struct text_poke_loc *tp, unsigned int nr_entries
 	 */
 	atomic_set_release(&bp_desc.refs, 1);
 
+	/*
+	 * Function tracing can enable thousands of places that need to be
+	 * updated. This can take quite some time, and with full kernel debugging
+	 * enabled, this could cause the softlockup watchdog to trigger.
+	 * Add cond_resched() calls to each phase.
+	 */
+	cond_resched();
+
 	/*
 	 * Corresponding read barrier in int3 notifier for making sure the
 	 * nr_entries and handler are correctly ordered wrt. patching.
@@ -2030,6 +2038,7 @@ static void text_poke_bp_batch(struct text_poke_loc *tp, unsigned int nr_entries
 		 * better safe than sorry (plus there's not only Intel).
 		 */
 		text_poke_sync();
+		cond_resched();
 	}
 
 	/*
@@ -2049,8 +2058,10 @@ static void text_poke_bp_batch(struct text_poke_loc *tp, unsigned int nr_entries
 		do_sync++;
 	}
 
-	if (do_sync)
+	if (do_sync) {
 		text_poke_sync();
+		cond_resched();
+	}
 
 	/*
 	 * Remove and wait for refs to be zero.
Signature · 1 line
2.39.2
#1 lore raw
Quoted 25 lines · On Sun, 28 May 2023 08:46:52 -0400 Steven Rostedt <[email protected]> wrote:

From: "Steven Rostedt (Google)" <[email protected]>

Debugging in the kernel has started slowing down the kernel by a noticeable amount. The ftrace start up tests are triggering the softlockup watchdog on some boxes. This is caused by the start up tests that enable function and function graph tracing several times. Sprinkling cond_resched() just in the start up test code was not enough to stop the softlockup from triggering. It would sometimes trigger in the text_poke_bp_batch() code.

The text_poke_bp_batch() is run in schedulable context. Add cond_resched() between each phase (adding the int3, updating the code, and removing the int3). This keeps the softlockup from triggering in the start up tests.

---
 arch/x86/kernel/alternative.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
arch/x86/kernel/alternative.c +0 -0
index f615e0cb6d93..e024eddd457f 100644

arch/x86/kernel/alternative.c static void text_poke_bp_batch(struct text_poke_loc *tp, unsigned int nr_entries · line 1962

Quoted 10 lines
arch/x86/kernel/alternative.c +7 -0
@@ -1953,6 +1953,14 @@ static void text_poke_bp_batch(struct text_poke_loc *tp, unsigned int nr_entries
 	 */
 	atomic_set_release(&bp_desc.refs, 1);
 
+	/*
+	 * Function tracing can enable thousands of places that need to be
+	 * updated. This can take quite some time, and with full kernel debugging
+	 * enabled, this could cause the softlockup watchdog to trigger.
+	 * Add cond_resched() calls to each phase.
+	 */
+	cond_resched();

Hmm, why don't you put this between the first step (put int3) and the second step (put other bytes)? I guess those would takes more time.

Thank you,

Quoted 27 lines
arch/x86/kernel/alternative.c +5 -1
+
 	/*
 	 * Corresponding read barrier in int3 notifier for making sure the
 	 * nr_entries and handler are correctly ordered wrt. patching.
@@ -2030,6 +2038,7 @@ static void text_poke_bp_batch(struct text_poke_loc *tp, unsigned int nr_entries
 		 * better safe than sorry (plus there's not only Intel).
 		 */
 		text_poke_sync();
+		cond_resched();
 	}
 
 	/*
@@ -2049,8 +2058,10 @@ static void text_poke_bp_batch(struct text_poke_loc *tp, unsigned int nr_entries
 		do_sync++;
 	}
 
-	if (do_sync)
+	if (do_sync) {
 		text_poke_sync();
+		cond_resched();
+	}
 
 	/*
 	 * Remove and wait for refs to be zero.
Signature · 1 line
2.39.2
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
#2 lore raw
Quoted 10 lines · On Mon, 29 May 2023 11:52:46 +0900 Masami Hiramatsu (Google)…

arch/x86/kernel/alternative.c static void text_poke_bp_batch(struct text_poke_loc *tp, unsigned int nr_entries · line 1962

Quoted 7 lines
arch/x86/kernel/alternative.c +7 -0
+	/*
+	 * Function tracing can enable thousands of places that need to be
+	 * updated. This can take quite some time, and with full kernel debugging
+	 * enabled, this could cause the softlockup watchdog to trigger.
+	 * Add cond_resched() calls to each phase.
+	 */
+	cond_resched();  

Hmm, why don't you put this between the first step (put int3) and the second step (put other bytes)? I guess those would takes more time.

Ah you're right. I still want this here to clear the 'need resched' flag before we start the process, but I did miss one after the first loop.

Thanks for the review!

-- Steve

#3 lore raw
In reply to Steven Rostedt
Quoted 25 lines · Steven Rostedt

From: "Steven Rostedt (Google)" <[email protected]>

Debugging in the kernel has started slowing down the kernel by a noticeable amount. The ftrace start up tests are triggering the softlockup watchdog on some boxes. This is caused by the start up tests that enable function and function graph tracing several times. Sprinkling cond_resched() just in the start up test code was not enough to stop the softlockup from triggering. It would sometimes trigger in the text_poke_bp_batch() code.

The text_poke_bp_batch() is run in schedulable context. Add cond_resched() between each phase (adding the int3, updating the code, and removing the int3). This keeps the softlockup from triggering in the start up tests.

---
 arch/x86/kernel/alternative.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
arch/x86/kernel/alternative.c +0 -0
index f615e0cb6d93..e024eddd457f 100644

arch/x86/kernel/alternative.c static void text_poke_bp_batch(struct text_poke_loc *tp, unsigned int nr_entries · line 1962

Quoted 10 lines
arch/x86/kernel/alternative.c +7 -0
@@ -1953,6 +1953,14 @@ static void text_poke_bp_batch(struct text_poke_loc *tp, unsigned int nr_entries
 	 */
 	atomic_set_release(&bp_desc.refs, 1);
 
+	/*
+	 * Function tracing can enable thousands of places that need to be
+	 * updated. This can take quite some time, and with full kernel debugging
+	 * enabled, this could cause the softlockup watchdog to trigger.
+	 * Add cond_resched() calls to each phase.
+	 */
+	cond_resched();

But but but... you can only have TP_VEC_MAX pokes queued, which is 256 on normal setups.

Please explain how this leads to problems and why you need _3_ reschedule points here.

#4 lore raw
Quoted 20 lines · On Tue, 30 May 2023 14:01:48 +0200 Peter Zijlstra <[email protected]> wrote:
Quoted 5 lines · Steven Rostedt
arch/x86/kernel/alternative.c +0 -0
index f615e0cb6d93..e024eddd457f 100644

arch/x86/kernel/alternative.c static void text_poke_bp_batch(struct text_poke_loc *tp, unsigned int nr_entries · line 1962

Quoted 10 lines
arch/x86/kernel/alternative.c +7 -0
@@ -1953,6 +1953,14 @@ static void text_poke_bp_batch(struct text_poke_loc *tp, unsigned int nr_entries
 	 */
 	atomic_set_release(&bp_desc.refs, 1);
 
+	/*
+	 * Function tracing can enable thousands of places that need to be
+	 * updated. This can take quite some time, and with full kernel debugging
+	 * enabled, this could cause the softlockup watchdog to trigger.
+	 * Add cond_resched() calls to each phase.
+	 */
+	cond_resched();  

But but but... you can only have TP_VEC_MAX pokes queued, which is 256 on normal setups.

Quoted 2 lines

Please explain how this leads to problems and why you need _3_ reschedule points here.

Maybe this was me being overly paranoid (and thinking, it doesn't hurt).

I could try it with just adding one (to make sure it happens every flush), and see if it doesn't trigger the softlock up.

-- Steve

#5 lore raw
In reply to Peter Zijlstra
Quoted 19 lines · On Tue, 30 May 2023 14:01:48 +0200 Peter Zijlstra <[email protected]> wrote:
Quoted 5 lines
arch/x86/kernel/alternative.c +0 -0
index f615e0cb6d93..e024eddd457f 100644

arch/x86/kernel/alternative.c static void text_poke_bp_batch(struct text_poke_loc *tp, unsigned int nr_entries · line 1962

Quoted 10 lines
arch/x86/kernel/alternative.c +7 -0
@@ -1953,6 +1953,14 @@ static void text_poke_bp_batch(struct text_poke_loc *tp, unsigned int nr_entries
 	 */
 	atomic_set_release(&bp_desc.refs, 1);
 
+	/*
+	 * Function tracing can enable thousands of places that need to be
+	 * updated. This can take quite some time, and with full kernel debugging
+	 * enabled, this could cause the softlockup watchdog to trigger.
+	 * Add cond_resched() calls to each phase.
+	 */
+	cond_resched();  

But but but... you can only have TP_VEC_MAX pokes queued, which is 256 on normal setups.

Quoted 2 lines

Please explain how this leads to problems and why you need _3_ reschedule points here.

I removed all but this first one and it works fine.

I'll resend with the update.

-- Steve