Skip to content

Commit 46c66d9

Browse files
Waiman-Longgregkh
authored andcommitted
locking/semaphore: Use wake_q to wake up processes outside lock critical section
[ Upstream commit 85b2b9c16d053364e2004883140538e73b333cdb ] A circular lock dependency splat has been seen involving down_trylock(): ====================================================== WARNING: possible circular locking dependency detected 6.12.0-41.el10.s390x+debug ------------------------------------------------------ dd/32479 is trying to acquire lock: 0015a20accd0d4f8 ((console_sem).lock){-.-.}-{2:2}, at: down_trylock+0x26/0x90 but task is already holding lock: 000000017e461698 (&zone->lock){-.-.}-{2:2}, at: rmqueue_bulk+0xac/0x8f0 the existing dependency chain (in reverse order) is: -> #4 (&zone->lock){-.-.}-{2:2}: -> #3 (hrtimer_bases.lock){-.-.}-{2:2}: -> #2 (&rq->__lock){-.-.}-{2:2}: -> #1 (&p->pi_lock){-.-.}-{2:2}: -> #0 ((console_sem).lock){-.-.}-{2:2}: The console_sem -> pi_lock dependency is due to calling try_to_wake_up() while holding the console_sem raw_spinlock. This dependency can be broken by using wake_q to do the wakeup instead of calling try_to_wake_up() under the console_sem lock. This will also make the semaphore's raw_spinlock become a terminal lock without taking any further locks underneath it. The hrtimer_bases.lock is a raw_spinlock while zone->lock is a spinlock. The hrtimer_bases.lock -> zone->lock dependency happens via the debug_objects_fill_pool() helper function in the debugobjects code. -> #4 (&zone->lock){-.-.}-{2:2}: __lock_acquire+0xe86/0x1cc0 lock_acquire.part.0+0x258/0x630 lock_acquire+0xb8/0xe0 _raw_spin_lock_irqsave+0xb4/0x120 rmqueue_bulk+0xac/0x8f0 __rmqueue_pcplist+0x580/0x830 rmqueue_pcplist+0xfc/0x470 rmqueue.isra.0+0xdec/0x11b0 get_page_from_freelist+0x2ee/0xeb0 __alloc_pages_noprof+0x2c2/0x520 alloc_pages_mpol_noprof+0x1fc/0x4d0 alloc_pages_noprof+0x8c/0xe0 allocate_slab+0x320/0x460 ___slab_alloc+0xa58/0x12b0 __slab_alloc.isra.0+0x42/0x60 kmem_cache_alloc_noprof+0x304/0x350 fill_pool+0xf6/0x450 debug_object_activate+0xfe/0x360 enqueue_hrtimer+0x34/0x190 __run_hrtimer+0x3c8/0x4c0 __hrtimer_run_queues+0x1b2/0x260 hrtimer_interrupt+0x316/0x760 do_IRQ+0x9a/0xe0 do_irq_async+0xf6/0x160 Normally a raw_spinlock to spinlock dependency is not legitimate and will be warned if CONFIG_PROVE_RAW_LOCK_NESTING is enabled, but debug_objects_fill_pool() is an exception as it explicitly allows this dependency for non-PREEMPT_RT kernel without causing PROVE_RAW_LOCK_NESTING lockdep splat. As a result, this dependency is legitimate and not a bug. Anyway, semaphore is the only locking primitive left that is still using try_to_wake_up() to do wakeup inside critical section, all the other locking primitives had been migrated to use wake_q to do wakeup outside of the critical section. It is also possible that there are other circular locking dependencies involving printk/console_sem or other existing/new semaphores lurking somewhere which may show up in the future. Let just do the migration now to wake_q to avoid headache like this. Reported-by: yzbot+ed801a886dfdbfe7136d@syzkaller.appspotmail.com Signed-off-by: Waiman Long <longman@redhat.com> Signed-off-by: Boqun Feng <boqun.feng@gmail.com> Signed-off-by: Ingo Molnar <mingo@kernel.org> Cc: Linus Torvalds <torvalds@linux-foundation.org> Link: https://lore.kernel.org/r/20250307232717.1759087-3-boqun.feng@gmail.com Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 26d4d84 commit 46c66d9

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

kernel/locking/semaphore.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <linux/export.h>
3030
#include <linux/sched.h>
3131
#include <linux/sched/debug.h>
32+
#include <linux/sched/wake_q.h>
3233
#include <linux/semaphore.h>
3334
#include <linux/spinlock.h>
3435
#include <linux/ftrace.h>
@@ -37,7 +38,7 @@ static noinline void __down(struct semaphore *sem);
3738
static noinline int __down_interruptible(struct semaphore *sem);
3839
static noinline int __down_killable(struct semaphore *sem);
3940
static noinline int __down_timeout(struct semaphore *sem, long timeout);
40-
static noinline void __up(struct semaphore *sem);
41+
static noinline void __up(struct semaphore *sem, struct wake_q_head *wake_q);
4142

4243
/**
4344
* down - acquire the semaphore
@@ -182,13 +183,16 @@ EXPORT_SYMBOL(down_timeout);
182183
void up(struct semaphore *sem)
183184
{
184185
unsigned long flags;
186+
DEFINE_WAKE_Q(wake_q);
185187

186188
raw_spin_lock_irqsave(&sem->lock, flags);
187189
if (likely(list_empty(&sem->wait_list)))
188190
sem->count++;
189191
else
190-
__up(sem);
192+
__up(sem, &wake_q);
191193
raw_spin_unlock_irqrestore(&sem->lock, flags);
194+
if (!wake_q_empty(&wake_q))
195+
wake_up_q(&wake_q);
192196
}
193197
EXPORT_SYMBOL(up);
194198

@@ -256,11 +260,12 @@ static noinline int __sched __down_timeout(struct semaphore *sem, long timeout)
256260
return __down_common(sem, TASK_UNINTERRUPTIBLE, timeout);
257261
}
258262

259-
static noinline void __sched __up(struct semaphore *sem)
263+
static noinline void __sched __up(struct semaphore *sem,
264+
struct wake_q_head *wake_q)
260265
{
261266
struct semaphore_waiter *waiter = list_first_entry(&sem->wait_list,
262267
struct semaphore_waiter, list);
263268
list_del(&waiter->list);
264269
waiter->up = true;
265-
wake_up_process(waiter->task);
270+
wake_q_add(wake_q, waiter->task);
266271
}

0 commit comments

Comments
 (0)