2024-06-05 09:52:03

by John Ogness

[permalink] [raw]
Subject: [PATCH linux-next] printk: nbcon: Fix nbcon_cpu_emergency_flush() when preemptible

nbcon_cpu_emergency_flush() can be called in a preemptible
context. In that case the CPU is not in an emergency state.
However, in order to see that the CPU is not in an emergency
state (accessing the per-cpu variable), preemption must be
disabled.

Disable preemption when checking the CPU state.

Reported-by: Juri Lelli <[email protected]>
Closes: https://lore.kernel.org/aqkcpca4vgadxc3yzcu74xwq3grslj5m43f3eb5fcs23yo2gy4@gcsnqcts5tos
Fixes: 46a1379208b7 ("printk: nbcon: Implement emergency sections")
Signed-off-by: John Ogness <[email protected]>
---
kernel/printk/nbcon.c | 13 ++++++++++++-
1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/kernel/printk/nbcon.c b/kernel/printk/nbcon.c
index 19f0db6945e4..39eb369880d9 100644
--- a/kernel/printk/nbcon.c
+++ b/kernel/printk/nbcon.c
@@ -1443,8 +1443,19 @@ void nbcon_cpu_emergency_exit(void)
*/
void nbcon_cpu_emergency_flush(void)
{
+ bool is_emergency;
+
+ /*
+ * If this context is not an emergency context, preemption might be
+ * enabled. To be sure, disable preemption when checking if this is
+ * an emergency context.
+ */
+ preempt_disable();
+ is_emergency = (*nbcon_get_cpu_emergency_nesting() != 0);
+ preempt_enable();
+
/* The explicit flush is needed only in the emergency context. */
- if (*(nbcon_get_cpu_emergency_nesting()) == 0)
+ if (!is_emergency)
return;

nbcon_atomic_flush_pending();
--
2.30.2


2024-06-05 10:11:03

by Petr Mladek

[permalink] [raw]
Subject: Re: [PATCH linux-next] printk: nbcon: Fix nbcon_cpu_emergency_flush() when preemptible

On Wed 2024-06-05 11:57:34, John Ogness wrote:
> nbcon_cpu_emergency_flush() can be called in a preemptible
> context. In that case the CPU is not in an emergency state.
> However, in order to see that the CPU is not in an emergency
> state (accessing the per-cpu variable), preemption must be
> disabled.
>
> Disable preemption when checking the CPU state.
>
> Reported-by: Juri Lelli <[email protected]>
> Closes: https://lore.kernel.org/aqkcpca4vgadxc3yzcu74xwq3grslj5m43f3eb5fcs23yo2gy4@gcsnqcts5tos
> Fixes: 46a1379208b7 ("printk: nbcon: Implement emergency sections")
> Signed-off-by: John Ogness <[email protected]>

Great catch!

Reviewed-by: Petr Mladek <[email protected]>

I am going to push it...

Best Regards,
Petr

2024-06-05 10:15:19

by Petr Mladek

[permalink] [raw]
Subject: Re: [PATCH linux-next] printk: nbcon: Fix nbcon_cpu_emergency_flush() when preemptible

On Wed 2024-06-05 12:10:50, Petr Mladek wrote:
> On Wed 2024-06-05 11:57:34, John Ogness wrote:
> > nbcon_cpu_emergency_flush() can be called in a preemptible
> > context. In that case the CPU is not in an emergency state.
> > However, in order to see that the CPU is not in an emergency
> > state (accessing the per-cpu variable), preemption must be
> > disabled.
> >
> > Disable preemption when checking the CPU state.
> >
> > Reported-by: Juri Lelli <[email protected]>
> > Closes: https://lore.kernel.org/aqkcpca4vgadxc3yzcu74xwq3grslj5m43f3eb5fcs23yo2gy4@gcsnqcts5tos
> > Fixes: 46a1379208b7 ("printk: nbcon: Implement emergency sections")
> > Signed-off-by: John Ogness <[email protected]>
>
> Great catch!
>
> Reviewed-by: Petr Mladek <[email protected]>
>
> I am going to push it...

Juri, should/could I add your Tested-by ?

Best Regards,
Petr

2024-06-05 13:00:42

by Juri Lelli

[permalink] [raw]
Subject: Re: [PATCH linux-next] printk: nbcon: Fix nbcon_cpu_emergency_flush() when preemptible

On 05/06/24 12:14, Petr Mladek wrote:
> On Wed 2024-06-05 12:10:50, Petr Mladek wrote:
> > On Wed 2024-06-05 11:57:34, John Ogness wrote:
> > > nbcon_cpu_emergency_flush() can be called in a preemptible
> > > context. In that case the CPU is not in an emergency state.
> > > However, in order to see that the CPU is not in an emergency
> > > state (accessing the per-cpu variable), preemption must be
> > > disabled.
> > >
> > > Disable preemption when checking the CPU state.
> > >
> > > Reported-by: Juri Lelli <[email protected]>
> > > Closes: https://lore.kernel.org/aqkcpca4vgadxc3yzcu74xwq3grslj5m43f3eb5fcs23yo2gy4@gcsnqcts5tos
> > > Fixes: 46a1379208b7 ("printk: nbcon: Implement emergency sections")
> > > Signed-off-by: John Ogness <[email protected]>
> >
> > Great catch!
> >
> > Reviewed-by: Petr Mladek <[email protected]>
> >
> > I am going to push it...
>
> Juri, should/could I add your Tested-by ?

A yes please if you like. Thanks!

Tested-by: Juri Lelli <[email protected]>

You guys were so quick to act on it! :)

Best,
Juri


2024-06-05 17:02:09

by Petr Mladek

[permalink] [raw]
Subject: Re: [PATCH linux-next] printk: nbcon: Fix nbcon_cpu_emergency_flush() when preemptible

On Wed 2024-06-05 11:57:34, John Ogness wrote:
> nbcon_cpu_emergency_flush() can be called in a preemptible
> context. In that case the CPU is not in an emergency state.
> However, in order to see that the CPU is not in an emergency
> state (accessing the per-cpu variable), preemption must be
> disabled.
>
> Disable preemption when checking the CPU state.
>
> Reported-by: Juri Lelli <[email protected]>
> Closes: https://lore.kernel.org/aqkcpca4vgadxc3yzcu74xwq3grslj5m43f3eb5fcs23yo2gy4@gcsnqcts5tos
> Fixes: 46a1379208b7 ("printk: nbcon: Implement emergency sections")
> Signed-off-by: John Ogness <[email protected]>

JFYI, the patch has been comitted into printk/linux.git,
branch rework/write-atomic.

Best Regards,
Petr