printk: Update @console_may_schedule in console_trylock_spinning()

Message ID 875xybmo2z.fsf@jogness.linutronix.de
State New
Headers
Series printk: Update @console_may_schedule in console_trylock_spinning() |

Commit Message

John Ogness Feb. 26, 2024, 12:01 p.m. UTC
  console_trylock_spinning() may takeover the console lock from a
schedulable context. Update @console_may_schedule to make sure it
reflects a trylock acquire.

Reported-by: Mukesh Ojha <quic_mojha@quicinc.com>
Link: https://lore.kernel.org/lkml/20240222090538.23017-1-quic_mojha@quicinc.com
Fixes: dbdda842fe96 ("printk: Add console owner and waiter logic to load balance console writes")
Signed-off-by: John Ogness <john.ogness@linutronix.de>
---
 kernel/printk/printk.c | 6 ++++++
 1 file changed, 6 insertions(+)


base-commit: e7081d5a9d976b84f61f497316d7c940a4a2e67a
  

Comments

Mukesh Ojha Feb. 26, 2024, 12:17 p.m. UTC | #1
On 2/26/2024 5:31 PM, John Ogness wrote:
> console_trylock_spinning() may takeover the console lock from a
> schedulable context. Update @console_may_schedule to make sure it
> reflects a trylock acquire.
> 
> Reported-by: Mukesh Ojha <quic_mojha@quicinc.com>
> Link: https://lore.kernel.org/lkml/20240222090538.23017-1-quic_mojha@quicinc.com
> Fixes: dbdda842fe96 ("printk: Add console owner and waiter logic to load balance console writes")
> Signed-off-by: John Ogness <john.ogness@linutronix.de>

Thanks for prompt response..

Yes, this looks fine..
As spinning code runs with preemption disabled context
and should reset the console_may_schedule to 0 .

what if console_trylock_spinning() gets the lock which makes 
console_may_schedule =1 and it is still schedulable ?

-Mukesh

> ---
>   kernel/printk/printk.c | 6 ++++++
>   1 file changed, 6 insertions(+)
> 
> diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
> index 1685a71f3f71..1612b50b2374 100644
> --- a/kernel/printk/printk.c
> +++ b/kernel/printk/printk.c
> @@ -2020,6 +2020,12 @@ static int console_trylock_spinning(void)
>   	 */
>   	mutex_acquire(&console_lock_dep_map, 0, 1, _THIS_IP_);
>   
> +	/*
> +	 * Update @console_may_schedule for trylock because the previous
> +	 * owner may have been schedulable.
> +	 */
> +	console_may_schedule = 0;
> +
>   	return 1;
>   }
>   
> 
> base-commit: e7081d5a9d976b84f61f497316d7c940a4a2e67a
  
John Ogness Feb. 26, 2024, 1:02 p.m. UTC | #2
On 2024-02-26, Mukesh Ojha <quic_mojha@quicinc.com> wrote:
> what if console_trylock_spinning() gets the lock which makes 
> console_may_schedule =1 and it is still schedulable ?

I am afraid I do not understand the question.

console_trylock_spinning() is only called from the printk caller
context. In this context, console_may_schedule is always set to 0.

Only if another context acquires the console lock per sleeping wait,
console_lock(), can console_may_schedule be set to 1.

Note that the value of console_may_schedule is only relevant for the
console lock owner when console_unlock() is called. That is why its
value is set when locking the console (or, with this patch, when
transferring console lock ownerhip).

John
  
Mukesh Ojha Feb. 27, 2024, 4:37 p.m. UTC | #3
On 2/26/2024 6:32 PM, John Ogness wrote:
> On 2024-02-26, Mukesh Ojha <quic_mojha@quicinc.com> wrote:
>> what if console_trylock_spinning() gets the lock which makes
>> console_may_schedule =1 and it is still schedulable ?
> 
> I am afraid I do not understand the question.
> 
> console_trylock_spinning() is only called from the printk caller
> context. In this context, console_may_schedule is always set to 0.
> 
> Only if another context acquires the console lock per sleeping wait,
> console_lock(), can console_may_schedule be set to 1.
> 
> Note that the value of console_may_schedule is only relevant for the
> console lock owner when console_unlock() is called. That is why its
> value is set when locking the console (or, with this patch, when
> transferring console lock ownerhip).

I overlooked it, thanks.
Patch LGTM.

Reviewed-by: Mukesh Ojha <quic_mojha@quicinc.com>

-Mukesh
> 
> John
  

Patch

diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
index 1685a71f3f71..1612b50b2374 100644
--- a/kernel/printk/printk.c
+++ b/kernel/printk/printk.c
@@ -2020,6 +2020,12 @@  static int console_trylock_spinning(void)
 	 */
 	mutex_acquire(&console_lock_dep_map, 0, 1, _THIS_IP_);
 
+	/*
+	 * Update @console_may_schedule for trylock because the previous
+	 * owner may have been schedulable.
+	 */
+	console_may_schedule = 0;
+
 	return 1;
 }