Skip to contentSkip to conversation

printk: Enhance the condition check of msleep in pr_flush()

PATCH In review [email protected] 5 participants 13 messages Jul 19, 2021 – Aug 6, 2021 Printk

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 printk: Enhance the condition check of msleep in pr_flush()

From: Chao Qin <[email protected]>

There is msleep in pr_flush(). If call WARN() in the early boot stage such as in early_initcall, pr_flush() will run into msleep when process scheduler is not ready yet. And then the system will sleep forever.

Before the system_state is SYSTEM_RUNNING, make sure DO NOT sleep in pr_flush().

Fixes 63cf1e4b564a ("printk: add pr_flush()")
---
 kernel/printk/printk.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/printk/printk.c +2 -1
index 209d2392f0d8..a9b3afbdac39 100644
@@ -3620,7 +3620,8 @@ bool pr_flush(int timeout_ms, bool reset_on_progress)
 	u64 diff;
 	u64 seq;
 
-	may_sleep = (preemptible() && !in_softirq());
+	may_sleep = (preemptible() && !in_softirq()
+			&& (system_state >= SYSTEM_RUNNING));
 
 	seq = prb_next_seq(prb);
 

base-commit: 7e175e6b59975c8901ad370f7818937f68de45c1

Signature · 1 line
2.25.1
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 7 lines · On 2021-07-19, [email protected] wrote:

From: Chao Qin <[email protected]>

There is msleep in pr_flush(). If call WARN() in the early boot stage such as in early_initcall, pr_flush() will run into msleep when process scheduler is not ready yet. And then the system will sleep forever.

Quoted 7 lines

Before the system_state is SYSTEM_RUNNING, make sure DO NOT sleep in pr_flush().

Fixes 63cf1e4b564a ("printk: add pr_flush()")
Quoted 22 lines
---
 kernel/printk/printk.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/printk/printk.c +2 -1
index 209d2392f0d8..a9b3afbdac39 100644
@@ -3620,7 +3620,8 @@ bool pr_flush(int timeout_ms, bool reset_on_progress)
 	u64 diff;
 	u64 seq;
 
-	may_sleep = (preemptible() && !in_softirq());
+	may_sleep = (preemptible() && !in_softirq()
+			&& (system_state >= SYSTEM_RUNNING));
 
 	seq = prb_next_seq(prb);
 

base-commit: 7e175e6b59975c8901ad370f7818937f68de45c1

Signature · 1 line
2.25.1
#2 lore raw
Quoted 10 lines · John Ogness

kernel/printk/printk.c bool pr_flush(int timeout_ms, bool reset_on_progress) · line 3624

Quoted 9 lines · On 2021-07-19, [email protected] wrote:
kernel/printk/printk.c +2 -1
@@ -3620,7 +3620,8 @@ bool pr_flush(int timeout_ms, bool reset_on_progress)
 	u64 diff;
 	u64 seq;
 
-	may_sleep = (preemptible() && !in_softirq());
+	may_sleep = (preemptible() && !in_softirq()
+			&& (system_state >= SYSTEM_RUNNING));

I don't have more context but scheduling should work starting with SYSTEM_SCHEDULING.

Quoted 4 lines

kernel/printk/printk.c bool pr_flush(int timeout_ms, bool reset_on_progress)

Quoted 4 lines
kernel/printk/printk.c +0 -0
 	seq = prb_next_seq(prb);
 
 

Sebastian

maintainer
#3 lore raw
Quoted 8 lines · Sebastian Andrzej Siewior
Quoted 7 lines · John Ogness
Quoted 6 lines · On 2021-07-19, [email protected] wrote:
kernel/printk/printk.c +0 -0
@@ -3620,7 +3620,8 @@ bool pr_flush(int timeout_ms, bool reset_on_progress)
 	u64 diff;
 	u64 seq;
 
Quoted 6 lines
Quoted 3 lines

kernel/printk/printk.c bool pr_flush(int timeout_ms, bool reset_on_progress) · line 3624

Quoted 3 lines
kernel/printk/printk.c +2 -1
-	may_sleep = (preemptible() && !in_softirq());
+	may_sleep = (preemptible() && !in_softirq()
+			&& (system_state >= SYSTEM_RUNNING));

I don't have more context but scheduling should work starting with SYSTEM_SCHEDULING.

I also thought this, but a quick test shows that is not the case. For example, init/main.c:kernel_init() is called in preemptible context, but msleep() will hang if called at the beginning of that function.

John Ogness

author
#4 lore raw

As John Ogness commented, I also found it would hang if sleep when >= SYSTEM_SCHEDULING.

And as in commit https://lore.kernel.org/lkml/[email protected]/, it should enable sleep right when the scheduler starts working(>= SYSTEM_RUNNING).

-Chao.

-----Original Message-----
From: John Ogness <[email protected]>
Sent: Friday, July 30, 2021 10:47 PM
To: Sebastian Andrzej Siewior <[email protected]>
Cc: Qin, Chao <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Mei, Paul <[email protected]>; Li, Lili <[email protected]>
Subject: Re: [PREEMPT_RT][PATCH] printk: Enhance the condition check of msleep in pr_flush()

Quoted 8 lines · Sebastian Andrzej Siewior
Quoted 7 lines · John Ogness
Quoted 6 lines · On 2021-07-19, [email protected] wrote:
kernel/printk/printk.c +0 -0
@@ -3620,7 +3620,8 @@ bool pr_flush(int timeout_ms, bool reset_on_progress)
 	u64 diff;
 	u64 seq;
 
Quoted 6 lines
Quoted 3 lines

kernel/printk/printk.c bool pr_flush(int timeout_ms, bool reset_on_progress) · line 3624

Quoted 3 lines
kernel/printk/printk.c +2 -1
-	may_sleep = (preemptible() && !in_softirq());
+	may_sleep = (preemptible() && !in_softirq()
+			&& (system_state >= SYSTEM_RUNNING));

I don't have more context but scheduling should work starting with SYSTEM_SCHEDULING.

I also thought this, but a quick test shows that is not the case. For example, init/main.c:kernel_init() is called in preemptible context, but msleep() will hang if called at the beginning of that function.

John Ogness

#5 lore raw
In reply to John Ogness
Quoted 7 lines · John Ogness
Quoted 2 lines · Sebastian Andrzej Siewior

I don't have more context but scheduling should work starting with SYSTEM_SCHEDULING.

I also thought this, but a quick test shows that is not the case. For example, init/main.c:kernel_init() is called in preemptible context, but msleep() will hang if called at the beginning of that function.

hmm. So the timer device is missing. Everything else is fine.

Quoted 1 line

John Ogness

Sebastian

#6 lore raw
In reply to [email protected]
Quoted 9 lines · On Mon, 2021-07-19 at 10:26 +0800, [email protected] wrote:

From: Chao Qin <[email protected]>

There is msleep in pr_flush(). If call WARN() in the early boot stage such as in early_initcall, pr_flush() will run into msleep when process scheduler is not ready yet. And then the system will sleep forever.

Before the system_state is SYSTEM_RUNNING, make sure DO NOT sleep in pr_flush().

Makes sense, thanks.

kernel/printk/printk.c

Quoted 1 line
kernel/printk/printk.c +0 -0

[]

kernel/printk/printk.c bool pr_flush(int timeout_ms, bool reset_on_progress) · line 3624

Quoted 7 lines
kernel/printk/printk.c +2 -1
@@ -3620,7 +3620,8 @@ bool pr_flush(int timeout_ms, bool reset_on_progress)
  	u64 diff;
  	u64 seq;
 
-	may_sleep = (preemptible() && !in_softirq());
+	may_sleep = (preemptible() && !in_softirq()
+			&& (system_state >= SYSTEM_RUNNING));

trivial style note:

Logic continuations are typically at the end of the previous line. And there are few too many parentheses for my taste.

Maybe exceed 80 columns in a single line

	may_sleep = preemptible() && !in_softirq() && system_state >= SYSTEM_RUNNING;

or align the continuation

	may_sleep = (preemptible() && !in_softirq() &&
		     system_state >= SYSTEM_RUNNING);

or use individual lines

	may_sleep = (preemptible() &&
		     !in_softirq() &&
		     system_state >= SYSTEM_RUNNING);
maintainer
#7 lore raw
Quoted 9 lines · Joe Perches

Logic continuations are typically at the end of the previous line. And there are few too many parentheses for my taste.

Maybe exceed 80 columns in a single line

	may_sleep = preemptible() && !in_softirq() && system_state >= SYSTEM_RUNNING;

or align the continuation

Quoted 8 lines
	may_sleep = (preemptible() && !in_softirq() &&
		     system_state >= SYSTEM_RUNNING);

or use individual lines

	may_sleep = (preemptible() &&
		     !in_softirq() &&
		     system_state >= SYSTEM_RUNNING);

The kernel now has a 100-column policy, but I decided to go with this third variant for easy reading.

Chao Qin, your patch will be part of the next PREEMPT_RT release. Thank you.

John Ogness

author
#8 lore raw

Noted. Thanks for your review.

-Chao.

-----Original Message-----
From: John Ogness <[email protected]>
Sent: Tuesday, July 20, 2021 10:03 PM
To: Joe Perches <[email protected]>; Qin, Chao <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Cc: [email protected]; Mei, Paul <[email protected]>; Li, Lili <[email protected]>
Subject: Re: [PREEMPT_RT][PATCH] printk: Enhance the condition check of msleep in pr_flush()

Quoted 10 lines · Joe Perches

Logic continuations are typically at the end of the previous line. And there are few too many parentheses for my taste.

Maybe exceed 80 columns in a single line

	may_sleep = preemptible() && !in_softirq() && system_state >= 

SYSTEM_RUNNING;

or align the continuation

Quoted 8 lines
	may_sleep = (preemptible() && !in_softirq() &&
		     system_state >= SYSTEM_RUNNING);

or use individual lines

	may_sleep = (preemptible() &&
		     !in_softirq() &&
		     system_state >= SYSTEM_RUNNING);

The kernel now has a 100-column policy, but I decided to go with this third variant for easy reading.

Chao Qin, your patch will be part of the next PREEMPT_RT release. Thank you.

John Ogness

author
#9 lore raw
In reply to John Ogness

Hi John Ogness,

Do you have plan to backport this fix into v5.10.y-rt kernel?

Thanks,
Qin Chao.

-----Original Message-----
From: John Ogness <[email protected]>
Sent: Tuesday, July 20, 2021 10:03 PM
To: Joe Perches <[email protected]>; Qin, Chao <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Cc: [email protected]; Mei, Paul <[email protected]>; Li, Lili <[email protected]>
Subject: Re: [PREEMPT_RT][PATCH] printk: Enhance the condition check of msleep in pr_flush()

Quoted 10 lines · Joe Perches

Logic continuations are typically at the end of the previous line. And there are few too many parentheses for my taste.

Maybe exceed 80 columns in a single line

	may_sleep = preemptible() && !in_softirq() && system_state >= 

SYSTEM_RUNNING;

or align the continuation

Quoted 8 lines
	may_sleep = (preemptible() && !in_softirq() &&
		     system_state >= SYSTEM_RUNNING);

or use individual lines

	may_sleep = (preemptible() &&
		     !in_softirq() &&
		     system_state >= SYSTEM_RUNNING);

The kernel now has a 100-column policy, but I decided to go with this third variant for easy reading.

Chao Qin, your patch will be part of the next PREEMPT_RT release. Thank you.

John Ogness

maintainer Reviewed-by
#10 lore raw

Hello Steven,

Could you please cherry-pick 83e9288d9c42("printk: Enhance the condition check of msleep in pr_flush()") from linux-rt-devel.git (branch linux-5.14.y-rt-rebase) for the next stable v5.10-rt release?

The commit is provided below as well.

Thanks.

John Ogness

Quoted 1 line · Chao

Do you have plan to backport this fix into v5.10.y-rt kernel?

From 83e9288d9c4295d1195e9d780fcbc42c72ba4a83 Mon Sep 17 00:00:00 2001
From: Chao Qin <[email protected]>
Date: Mon, 19 Jul 2021 10:26:50 +0800
Subject: [PATCH] printk: Enhance the condition check of msleep in pr_flush()

There is msleep in pr_flush(). If call WARN() in the early boot stage such as in early_initcall, pr_flush() will run into msleep when process scheduler is not ready yet. And then the system will sleep forever.

Before the system_state is SYSTEM_RUNNING, make sure DO NOT sleep in pr_flush().

---
 kernel/printk/printk.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
kernel/printk/printk.c +3 -1
index e4085e2cafb5..500ae4b18864 100644
@@ -3661,7 +3661,9 @@ bool pr_flush(int timeout_ms, bool reset_on_progress)
 	u64 diff;
 	u64 seq;
 
-	may_sleep = (preemptible() && !in_softirq());
+	may_sleep = (preemptible() &&
+		     !in_softirq() &&
+		     system_state >= SYSTEM_RUNNING);
 
 	seq = prb_next_seq(prb);
 
Signature · 1 line
2.20.1
#11 lore raw
Quoted 8 lines · On Thu, 05 Aug 2021 14:27:25 +0206 John Ogness <[email protected]>…

Hello Steven,

Could you please cherry-pick 83e9288d9c42("printk: Enhance the condition check of msleep in pr_flush()") from linux-rt-devel.git (branch linux-5.14.y-rt-rebase) for the next stable v5.10-rt release?

The commit is provided below as well.

I'll add it to my queue for tomorrow.

Thanks!

-- Steve

#12 lore raw
Quoted 1 line · On Thu, 5 Aug 2021 12:12:00 -0400 Steven Rostedt <[email protected]> wrote:

I'll add it to my queue for tomorrow.

I'm currently testing 5.10.56-rt48 which only brings the 5.10 rt branch up to latest stable. After that is done and pushed out, I'll backport this patch and do a 5.10.56-rt49-rc release.

Letting you know in case you see the 5.10.56-rt48 and wonder where your patch is.

-- Steve