From nobody Sat Feb 7 17:53:09 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.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 05E091E0E0A for ; Mon, 20 Jan 2025 19:36:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737401786; cv=none; b=hfj37gygDAOwRYsM3jCFRZAVc/Y2w8s46Rww7Bo1mZ5FCc8L2VH3UINw7TtgWZpFsP3bsRjuHu+x7IifH1DAdNxAWGtiFCw8xDvMar5soQ6D9YlPQp6nt6ba3GCsGMqm++tl8MXw6N5v755C15MuUE52UgOBU01+wiccnjQxIoo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737401786; c=relaxed/simple; bh=ImdET2rZSi/3WZoc7cjNzhRFFas8AaaANxEZQ3omCgI=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=OwqQ/xX4CYZW+oI+i8rvkv1fJopyNgayjvcUiEv62UTJxUvD6yW2pRZc2a1IDQKS0JHL6OGZzjqSzNJBY1w3EcIRVWCMxEjdaJLgDDZpKI1kU+D9XqMBQUbQeazPQYXouQ8xKNiPP8BjpUdbsy7FbbhsrQHxL2GVejfb+cqOtOY= 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=ZlEyfEit; arc=none smtp.client-ip=170.10.133.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="ZlEyfEit" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1737401783; 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=OFnZd5hMEAmVO3zU2sdUz3yFC5fUf8kbm/qECMtKUxk=; b=ZlEyfEitrtbBBTOsYNHlYTOunyzBv1JqaROYMSgZDRStUaPyIAlVTQtwmmVCFqEonEn5tp viCFc+wJtccmcaUCrTzM62yszv7k7piGcoeDcJ2+ud++Z5c3nA9z45KSVQPado1eOBFfmF oViBIFM1nZouvqhkjYyMQfBxla0h334= Received: from mx-prod-mc-01.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-394-oog5WaSSM6ihICXoFg9GnQ-1; Mon, 20 Jan 2025 14:36:20 -0500 X-MC-Unique: oog5WaSSM6ihICXoFg9GnQ-1 X-Mimecast-MFC-AGG-ID: oog5WaSSM6ihICXoFg9GnQ Received: from mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.15]) (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-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 9C9F31956050; Mon, 20 Jan 2025 19:36:18 +0000 (UTC) Received: from llong-thinkpadp16vgen1.westford.csb (unknown [10.22.65.167]) by mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 2B56D195608A; Mon, 20 Jan 2025 19:36:16 +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 raw_spin_trylock_irqsave() in down_trylock() Date: Mon, 20 Jan 2025 14:36:08 -0500 Message-ID: <20250120193608.2312690-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.15 Content-Type: text/plain; charset="utf-8" A circular lock dependency splat has been seen with 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 [ 4011.795644] [ 4011.795644] which lock already depends on the new lock. : [ 4011.796025] (console_sem).lock --> hrtimer_bases.lock --> &zone->lock [ 4011.796025] [ 4011.796029] Possible unsafe locking scenario: [ 4011.796029] [ 4011.796030] CPU0 [ 4011.796031] ---- [ 4011.796032] lock(&zone->lock); [ 4011.796034] lock(hrtimer_bases.lock); [ 4011.796036] lock(&zone->lock); [ 4011.796038] lock((console_sem).lock); [ 4011.796039] [ 4011.796039] *** DEADLOCK *** The calling sequence was rmqueue_pcplist() =3D> __rmqueue_pcplist() =3D> rmqueue_bulk() =3D> expand() =3D> __add_to_free_list() =3D> __warn_printk() =3D> ... =3D> console_trylock() =3D> __down_trylock_console_sem() =3D> down_trylock() =3D> _raw_spin_lock_irqsave() Normally, a trylock call should avoid this kind of circular lock dependency splat, but down_trylock() is an exception. Fix this problem by using raw_spin_trylock_irqsave() in down_trylock() to make it behave like the other trylock calls. Signed-off-by: Waiman Long --- kernel/locking/semaphore.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/kernel/locking/semaphore.c b/kernel/locking/semaphore.c index 34bfae72f295..cb27cbf5162f 100644 --- a/kernel/locking/semaphore.c +++ b/kernel/locking/semaphore.c @@ -127,6 +127,7 @@ EXPORT_SYMBOL(down_killable); * * NOTE: This return value is inverted from both spin_trylock and * mutex_trylock! Be careful about this when converting code. + * I.e. 0 on success, 1 on failure. * * Unlike mutex_trylock, this function can be used from interrupt context, * and the semaphore can be released by any task or interrupt. @@ -136,7 +137,8 @@ int __sched down_trylock(struct semaphore *sem) unsigned long flags; int count; =20 - raw_spin_lock_irqsave(&sem->lock, flags); + if (!raw_spin_trylock_irqsave(&sem->lock, flags)) + return 1; count =3D sem->count - 1; if (likely(count >=3D 0)) sem->count =3D count; --=20 2.47.1