Re: [PATCH 3/3] printk: Avoid all wakeups from printk

Ingo Molnar
2011-06-09T13:55:56Z

* Peter Zijlstra <a.p.zijlstra@chello.nl> wrote:

> +static void __console_flush(void);

I'd suggest moving this function to the right place, so that no
prototypes are necessary.

> +/*
> + * Special 'atomic' semaphore operations that mimmick down_trylock() + up(),

mimic

> + * except they don't release the semaphore internal lock and optimize the
> + * sem->count fiddling away.
> + *
> + * The advantage is that this construct doesn't generate wakeups on atomic_up()
> + * since any contending semaphore acuiqisition will still be spinning on the

acquisition.

> + * internal lock, instead of having gotten queued on the wait_list.
> + *
> + * printk() uses this to avoid generating wakeups, which would make it unsafe
> + * to use in certain contexts (avoids lock inversion or lock recursion with
> + * the scheduler locks).
> + *
> + * Assumes IRQs are disabled.

I'd add:

* Note: We emphatically do *not* want this function exported. Ever.

and maybe:

* Note2: Even asking for that will likely buy you a nasty response.

> + */
> +static int atomic_down_trylock(struct semaphore *sem)
> +{
> + spin_lock(&sem->lock);
> + if (sem->count > 0)
> + return 0;
> +
> + spin_unlock(&sem->lock);
> + return 1;
> +}
> +
> +static void atomic_up(struct semaphore *sem)
> +{
> + spin_unlock(&sem->lock);
> +}
> +
> asmlinkage int vprintk(const char *fmt, va_list args)
> {
> int printed_len = 0;
> @@ -943,19 +939,14 @@ asmlinkage int vprintk(const char *fmt,
> if (*p == '\n')
> new_text_line = 1;
> }
> + printk_cpu = UINT_MAX;
> + spin_unlock(&logbuf_lock);

Hm, is that printk_cpu setting inside the critical section safe? What
happens if we get an NMI on this CPU in that window?

Thanks,

Ingo