Question: Can I call down() in an atomic context?

From: Xiaoming Ni
Date: Thu Aug 05 2021 - 22:36:00 EST


might_sleep() is called in kernel/locking/rwsem.c (API: down_read/down_write/...)
but not in kernel/locking/semaphore.c (API: down/down_timeout/..).
Was it designed so purposely, or was it missed by mistake?

After I added might_sleep() to kernel/locking/semaphore.c,
an alarm log was occasionally detected in my test environment:

BUG: sleeping function called from invalid context at kernel/locking/semaphore.c:163
in_atomic(): 1, irqs_disabled(): 128, pid: 0, name: swapper/8
...
Call trace:
dump_backtrace+0x0/0x3c
dump_backtrace+0x2c/0x3c
show_stack+0x24/0x34
dump_stack+0xb0/0xf0
___might_sleep+0x130/0x144
__might_sleep+0x78/0x88
down_timeout+0x40/0xc8
acpi_os_wait_semaphore+0x78/0xa0 drivers/acpi/osl.c#L1266
acpi_ut_acquire_mutex+0x50/0xb4 drivers/acpi/acpica/utmutex.c#L241
acpi_get_table+0x3c/0xc8 drivers/acpi/acpica/tbxface.c#L318
acpi_find_last_cache_level+0x7c/0x140 drivers/acpi/pptt.c#L602
_init_cache_level+0xd0/0xd8 arch/arm64/kernel/cacheinfo.c#L64
flush_smp_call_function_queue+0x138/0x160 kernel/smp.c#L561
generic_smp_call_function_single_interrupt+0x18/0x24
handle_IPI+0x1d0/0x50c
gic_handle_irq+0x13c/0x140
el1_irq+0xcc/0x180
arch_cpu_idle+0xc0/0x16c
default_idle_call+0x34/0x38
cpu_startup_entry+0x2b4/0x358
secondary_start_kernel+0x1a8/0x1dc

Does this mean that it is necessary to add might_sleep in kernel/locking/semaphore.c?


Thanks
Xiaoming Ni