From nobody Wed Jan 22 08:52:04 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DA51E1B95B for ; Wed, 22 Jan 2025 01:13:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737508412; cv=none; b=gYsdTwNPzABjzm43HLkr2itfLFMqj4N9ZXMbvWUjLkZz5jF3CUAYgZgN4aCt7jXHpJJ2vZGjlMbrqyoPFuj56ytW0/yUHjut8xn3YA3ZGn3ncHURWgHSa3YirCdrf8Uj5RsvuAgHVFcN4sJfblcCvKtRU3Ua2Hfm1Pe6//rXJHI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737508412; c=relaxed/simple; bh=FDTfD2eVUHbciDO+vA9LwRpkTGF5GgfgvOnKGcP5nMw=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=f27D+CZtK757aPOq3aLpacqEEW/tipUT3UfF/tBSdecpfnNc1qd6DpGP/sgigOetJwqaCv4GH3ZaSfOMptAjOAbym3l+OuwVJqGdH/DKX6RcWTOsgqfEFFXGccWiZzJp5gb6T3ce1F6PrDNzDkdLCWeHYK1rdlhjs0soZsFkCzI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=DSALRmm9; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="DSALRmm9" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1737508408; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=NcBLyR00ChZFyF/JUk43gPUvbqC+qOyhCFWCsf8bJEw=; b=DSALRmm9PqOOHBuf6DWde5xRa0xEObZps+dCK5KR5lTx4DEQ/2uk6N5Cy/kbA7KrEt+ysg RGnYaz2Shr7VQrwD5n2WbaWbxAC3ELHZqe+e+emFaVLEZilPhOrcY1aB09sUEhCY650Wyh XtFWjuJYPmTwkSEaoM5x6/L9nuTrKTE= Received: from mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-147-xnihp4ReNGCPuAsPlh8N4A-1; Tue, 21 Jan 2025 20:13:25 -0500 X-MC-Unique: xnihp4ReNGCPuAsPlh8N4A-1 X-Mimecast-MFC-AGG-ID: xnihp4ReNGCPuAsPlh8N4A Received: from mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.40]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id BE0A51955F36; Wed, 22 Jan 2025 01:13:23 +0000 (UTC) Received: from llong-thinkpadp16vgen1.westford.csb (unknown [10.22.90.5]) by mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 5F8A619560AD; Wed, 22 Jan 2025 01:13:22 +0000 (UTC) From: Waiman Long To: Peter Zijlstra , Ingo Molnar , Will Deacon , Boqun Feng Cc: linux-kernel@vger.kernel.org, Waiman Long Subject: [PATCH] locking/semaphore: Use wake_q to wake up processes outside lock critical section Date: Tue, 21 Jan 2025 20:13:14 -0500 Message-ID: <20250122011314.2869715-1-longman@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.40 Content-Type: text/plain; charset="utf-8" A circular lock dependency splat has been seen involving down_trylock(). [ 4011.795602] =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D [ 4011.795603] WARNING: possible circular locking dependency detected [ 4011.795607] 6.12.0-41.el10.s390x+debug [ 4011.795612] ------------------------------------------------------ [ 4011.795613] dd/32479 is trying to acquire lock: [ 4011.795617] 0015a20accd0d4f8 ((console_sem).lock){-.-.}-{2:2}, at: down_= trylock+0x26/0x90 [ 4011.795636] [ 4011.795636] but task is already holding lock: [ 4011.795637] 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 last two are due to calling try_to_wake_up() while holding the console.sem raw_spinlock. Break this circular lock dependency by using wake_q to do the wakeup instead of calling try_to_wake_up() under the lock. By doing so, the semaphore's raw_spinlock becomes a terminal lock without taking any further raw_spinlock's underneath it. Signed-off-by: Waiman Long --- kernel/locking/semaphore.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/kernel/locking/semaphore.c b/kernel/locking/semaphore.c index cb27cbf5162f..751e3cbb899f 100644 --- a/kernel/locking/semaphore.c +++ b/kernel/locking/semaphore.c @@ -29,6 +29,7 @@ #include #include #include +#include #include #include #include @@ -38,7 +39,7 @@ static noinline void __down(struct semaphore *sem); static noinline int __down_interruptible(struct semaphore *sem); static noinline int __down_killable(struct semaphore *sem); static noinline int __down_timeout(struct semaphore *sem, long timeout); -static noinline void __up(struct semaphore *sem); +static noinline void __up(struct semaphore *sem, struct wake_q_head *wake_= q); =20 /** * down - acquire the semaphore @@ -185,13 +186,16 @@ EXPORT_SYMBOL(down_timeout); void __sched up(struct semaphore *sem) { unsigned long flags; + DEFINE_WAKE_Q(wake_q); =20 raw_spin_lock_irqsave(&sem->lock, flags); if (likely(list_empty(&sem->wait_list))) sem->count++; else - __up(sem); + __up(sem, &wake_q); raw_spin_unlock_irqrestore(&sem->lock, flags); + if (!wake_q_empty(&wake_q)) + wake_up_q(&wake_q); } EXPORT_SYMBOL(up); =20 @@ -271,11 +275,12 @@ static noinline int __sched __down_timeout(struct sem= aphore *sem, long timeout) return __down_common(sem, TASK_UNINTERRUPTIBLE, timeout); } =20 -static noinline void __sched __up(struct semaphore *sem) +static noinline void __sched __up(struct semaphore *sem, + struct wake_q_head *wake_q) { struct semaphore_waiter *waiter =3D list_first_entry(&sem->wait_list, struct semaphore_waiter, list); list_del(&waiter->list); waiter->up =3D true; - wake_up_process(waiter->task); + wake_q_add(wake_q, waiter->task); } --=20 2.47.1