Re: [PATCH 1/2] printk/panic: Access the main printk log in panic() only when safe

From: Sergey Senozhatsky
Date: Thu Jul 18 2019 - 06:12:00 EST


On (07/16/19 09:28), Petr Mladek wrote:
[..]
> +int printk_bust_lock_safe(void)
> +{
> + if (!raw_spin_is_locked(&logbuf_lock))
> + return 0;
> +
> + if (num_online_cpus() == 1) {
> + debug_locks_off();
> + raw_spin_lock_init(&logbuf_lock);
> + return 0;
> + }
> +
> + return -EWOULDBLOCK;
> +}

A side note:

I'd say that we also need to clear console_owner and re-init
console_owner_lock spin_lock, panic CPU can spin forever
otherwise; and I think it would also be reasonable to re-init
console_sem's spin_lock, yet another lock on which panic CPU
can spin forever. (Assuming that one of remote CPUs that we
have NMI killed had owned any of those locks).

Console drivers' locks are mostly OK, as long as drivers check
oops_in_progress in ->write() path and act accordingly, but
printk() has to deal with more than one lock (logbuf_lock)
before it invokes console drivers.

-ss