Re: [PATCH printk v1 11/13] printk: reimplement console_lock for proper kthread support

From: John Ogness
Date: Fri Feb 18 2022 - 17:03:08 EST


On 2022-02-18, John Ogness <john.ogness@xxxxxxxxxxxxx> wrote:
>> This is another complexity. An easier solution would be to do:
>>
>> console_trylock()
>> {
>> if (down_trylock(&console_sem))
>> return failed;
>>
>> for_each_console(con) {
>> if (mutext_trylock(&con->lock))
>> goto fail;
>> }
>>
>> // sucess
>>
>> fail:
>> for_each_console(con_fail) {
>> if (con_fail == con)
>> break;
>> mutex_unlock(&con->lock);
>> }
>
> No, this cannot work. Semaphores are the _only_ synchonization object in
> the kernel that is allowed to perform a trylock from any context
> (because they don't have owners).

Sorry, I need to clarify this bold statement.

Semaphores are the only synchonization object in the kernel that is
allowed to perform a trylock from any context and does not disable
preemption upon locking. (This is important because console lock holders
expect preemption to be unaffected when taking the lock.)

John