From nobody Wed Feb 11 19:42:32 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D9CB0C76196 for ; Tue, 11 Apr 2023 04:25:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230114AbjDKEZd (ORCPT ); Tue, 11 Apr 2023 00:25:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40864 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230088AbjDKEZY (ORCPT ); Tue, 11 Apr 2023 00:25:24 -0400 Received: from mail-pf1-x449.google.com (mail-pf1-x449.google.com [IPv6:2607:f8b0:4864:20::449]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AE38610E for ; Mon, 10 Apr 2023 21:25:23 -0700 (PDT) Received: by mail-pf1-x449.google.com with SMTP id d2e1a72fcca58-632cf80572bso616520b3a.3 for ; Mon, 10 Apr 2023 21:25:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1681187123; x=1683779123; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=D11BkT/by/KTrics5kACR4e3C7wu73LGrBohPqnvbDM=; b=a/8h6h0U18RoU6lWNBts1y/Xzg9Z20KAdpvIJ/qhPltdInUHLLAQJQy6ixAXv1mjb1 6RhdONDB2S/hncpRejyEtOMjxGgN9UbZ4Gcj3Rpjs2J7+/qOVGU/vqWKnSz2lIgvnrU7 DQgcer+PFG7VOc6LR5J0jqwzYfqpyrdwGjpYuHF3terfFA/nfSWVvKaSOAFYYimDGr5X TUufRKic2F+HDcm80RvhbNxP6pHVdFJGRtEDjaEgYMw93ET9mCEOHNTnrnX9qCVZE430 kD0Sp7XElrnLAsgHIaBE/mJQ2BCnRLN0FwFAbc3cZPeDRYjtTzRamp7EcC0xQCex3SgY U6PQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1681187123; x=1683779123; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=D11BkT/by/KTrics5kACR4e3C7wu73LGrBohPqnvbDM=; b=l704giov0w+SuLsxBI12ogpY9ZZeuH7yMHZpt7JtK+/BeMDv0Va2kucE1XLLA8HGMf xT4PNd2yYLzp8zaALRiDtTOY5vjRNbdsmMRqnbd89MSjeJ8N4SyZW/s+8TxgrW5J9DX/ omVqqgnds/ZGXL9PoNItvcMuCDUFzrp11ktt9HGB/hq/+FJ9JQuaGlAKQXFrzpt8y2ca Yoo2zdiXBL5Yu/41wgNK+0uUuNlCI3kId+srwzW2BOTrqZq08Goutm38JfOrQHyRiZHl u+gdLmRrZlOaSIQ20NCBmDMjGl+GU3VR1BzGWZG00TIfy7i7mp2GL0jlKQ5aQbbYUn4g Wypg== X-Gm-Message-State: AAQBX9fQ7nv23S62Grr1Z4eJPhAu8yI6mx8Fdmyvcy6gfCV/vsBHjU4L meusCa25/+GaAYM0GNotyxxsvg+EyqcLzvZNLf42PHILZzbEnyd6+6U2v7Tlkj/Q/u2bc0AyeCY 260p/3KhrVic4DQ1Jj2nw9mhO+KPImeJeTAlFvWU0yItpeK+NHXgV7mKKXZuqFiJM7ZI6uD8= X-Google-Smtp-Source: AKy350bLfk3lZjV3uXu/HKmTd6O9B4gIlEG5TOe1lGSMQDkfVUgDSf42OlNaT7CZ3Jf3m2A9fvf0ixchCeFe X-Received: from jstultz-noogler2.c.googlers.com ([fda3:e722:ac3:cc00:24:72f4:c0a8:600]) (user=jstultz job=sendgmr) by 2002:a05:6a00:2387:b0:632:1d87:688b with SMTP id f7-20020a056a00238700b006321d87688bmr4479902pfc.0.1681187122894; Mon, 10 Apr 2023 21:25:22 -0700 (PDT) Date: Tue, 11 Apr 2023 04:24:59 +0000 In-Reply-To: <20230411042511.1606592-1-jstultz@google.com> Mime-Version: 1.0 References: <20230411042511.1606592-1-jstultz@google.com> X-Mailer: git-send-email 2.40.0.577.gac1e443424-goog Message-ID: <20230411042511.1606592-3-jstultz@google.com> Subject: [PATCH v3 02/14] locking/mutex: make mutex::wait_lock irq safe From: John Stultz To: LKML Cc: Juri Lelli , Joel Fernandes , Qais Yousef , Ingo Molnar , Peter Zijlstra , Vincent Guittot , Dietmar Eggemann , Valentin Schneider , Steven Rostedt , Ben Segall , Zimuzo Ezeozue , Mel Gorman , Daniel Bristot de Oliveira , Will Deacon , Waiman Long , Boqun Feng , "Paul E . McKenney" , "Connor O'Brien" , John Stultz Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Juri Lelli mutex::wait_lock might be nested under rq->lock. Make it irq safe then. Cc: Joel Fernandes Cc: Qais Yousef Cc: Ingo Molnar Cc: Peter Zijlstra Cc: Juri Lelli Cc: Vincent Guittot Cc: Dietmar Eggemann Cc: Valentin Schneider Cc: Steven Rostedt Cc: Ben Segall Cc: Zimuzo Ezeozue Cc: Mel Gorman Cc: Daniel Bristot de Oliveira Cc: Will Deacon Cc: Waiman Long Cc: Boqun Feng Cc: "Paul E . McKenney" Signed-off-by: Juri Lelli Signed-off-by: Peter Zijlstra (Intel) [rebase & fix {un,}lock_wait_lock helpers in ww_mutex.h] Signed-off-by: Connor O'Brien Signed-off-by: John Stultz --- v3: * Re-added this patch after it was dropped in v2 which caused lockdep warnings to trip. --- kernel/locking/mutex.c | 18 ++++++++++-------- kernel/locking/ww_mutex.h | 22 ++++++++++++---------- 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/kernel/locking/mutex.c b/kernel/locking/mutex.c index 1582756914df..a528e7f42caa 100644 --- a/kernel/locking/mutex.c +++ b/kernel/locking/mutex.c @@ -572,6 +572,7 @@ __mutex_lock_common(struct mutex *lock, unsigned int st= ate, unsigned int subclas { struct mutex_waiter waiter; struct ww_mutex *ww; + unsigned long flags; int ret; =20 if (!use_ww_ctx) @@ -614,7 +615,7 @@ __mutex_lock_common(struct mutex *lock, unsigned int st= ate, unsigned int subclas return 0; } =20 - raw_spin_lock(&lock->wait_lock); + raw_spin_lock_irqsave(&lock->wait_lock, flags); /* * After waiting to acquire the wait_lock, try again. */ @@ -675,7 +676,7 @@ __mutex_lock_common(struct mutex *lock, unsigned int st= ate, unsigned int subclas goto err; } =20 - raw_spin_unlock(&lock->wait_lock); + raw_spin_unlock_irqrestore(&lock->wait_lock, flags); if (ww_ctx) ww_ctx_wake(ww_ctx); schedule_preempt_disabled(); @@ -698,9 +699,9 @@ __mutex_lock_common(struct mutex *lock, unsigned int st= ate, unsigned int subclas trace_contention_begin(lock, LCB_F_MUTEX); } =20 - raw_spin_lock(&lock->wait_lock); + raw_spin_lock_irqsave(&lock->wait_lock, flags); } - raw_spin_lock(&lock->wait_lock); + raw_spin_lock_irqsave(&lock->wait_lock, flags); acquired: __set_current_state(TASK_RUNNING); =20 @@ -726,7 +727,7 @@ __mutex_lock_common(struct mutex *lock, unsigned int st= ate, unsigned int subclas if (ww_ctx) ww_mutex_lock_acquired(ww, ww_ctx); =20 - raw_spin_unlock(&lock->wait_lock); + raw_spin_unlock_irqrestore(&lock->wait_lock, flags); if (ww_ctx) ww_ctx_wake(ww_ctx); preempt_enable(); @@ -737,7 +738,7 @@ __mutex_lock_common(struct mutex *lock, unsigned int st= ate, unsigned int subclas __mutex_remove_waiter(lock, &waiter); err_early_kill: trace_contention_end(lock, ret); - raw_spin_unlock(&lock->wait_lock); + raw_spin_unlock_irqrestore(&lock->wait_lock, flags); debug_mutex_free_waiter(&waiter); mutex_release(&lock->dep_map, ip); if (ww_ctx) @@ -909,6 +910,7 @@ static noinline void __sched __mutex_unlock_slowpath(st= ruct mutex *lock, unsigne struct task_struct *next =3D NULL; DEFINE_WAKE_Q(wake_q); unsigned long owner; + unsigned long flags; =20 mutex_release(&lock->dep_map, ip); =20 @@ -935,7 +937,7 @@ static noinline void __sched __mutex_unlock_slowpath(st= ruct mutex *lock, unsigne } } =20 - raw_spin_lock(&lock->wait_lock); + raw_spin_lock_irqsave(&lock->wait_lock, flags); debug_mutex_unlock(lock); if (!list_empty(&lock->wait_list)) { /* get the first entry from the wait-list: */ @@ -953,7 +955,7 @@ static noinline void __sched __mutex_unlock_slowpath(st= ruct mutex *lock, unsigne __mutex_handoff(lock, next); =20 preempt_disable(); - raw_spin_unlock(&lock->wait_lock); + raw_spin_unlock_irqrestore(&lock->wait_lock, flags); =20 wake_up_q(&wake_q); preempt_enable(); diff --git a/kernel/locking/ww_mutex.h b/kernel/locking/ww_mutex.h index e49ea5336473..984a4e0bff36 100644 --- a/kernel/locking/ww_mutex.h +++ b/kernel/locking/ww_mutex.h @@ -70,14 +70,14 @@ __ww_mutex_has_waiters(struct mutex *lock) return atomic_long_read(&lock->owner) & MUTEX_FLAG_WAITERS; } =20 -static inline void lock_wait_lock(struct mutex *lock) +static inline void lock_wait_lock(struct mutex *lock, unsigned long *flags) { - raw_spin_lock(&lock->wait_lock); + raw_spin_lock_irqsave(&lock->wait_lock, *flags); } =20 -static inline void unlock_wait_lock(struct mutex *lock) +static inline void unlock_wait_lock(struct mutex *lock, unsigned long flag= s) { - raw_spin_unlock(&lock->wait_lock); + raw_spin_unlock_irqrestore(&lock->wait_lock, flags); } =20 static inline void lockdep_assert_wait_lock_held(struct mutex *lock) @@ -144,14 +144,14 @@ __ww_mutex_has_waiters(struct rt_mutex *lock) return rt_mutex_has_waiters(&lock->rtmutex); } =20 -static inline void lock_wait_lock(struct rt_mutex *lock) +static inline void lock_wait_lock(struct rt_mutex *lock, unsigned long *fl= ags) { - raw_spin_lock(&lock->rtmutex.wait_lock); + raw_spin_lock_irqsave(&lock->rtmutex.wait_lock, *flags); } =20 -static inline void unlock_wait_lock(struct rt_mutex *lock) +static inline void unlock_wait_lock(struct rt_mutex *lock, flags) { - raw_spin_unlock(&lock->rtmutex.wait_lock); + raw_spin_unlock_irqrestore(&lock->rtmutex.wait_lock, flags); } =20 static inline void lockdep_assert_wait_lock_held(struct rt_mutex *lock) @@ -383,6 +383,8 @@ __ww_mutex_check_waiters(struct MUTEX *lock, struct ww_= acquire_ctx *ww_ctx) static __always_inline void ww_mutex_set_context_fastpath(struct ww_mutex *lock, struct ww_acquire_ctx= *ctx) { + unsigned long flags; + ww_mutex_lock_acquired(lock, ctx); =20 /* @@ -410,9 +412,9 @@ ww_mutex_set_context_fastpath(struct ww_mutex *lock, st= ruct ww_acquire_ctx *ctx) * Uh oh, we raced in fastpath, check if any of the waiters need to * die or wound us. */ - lock_wait_lock(&lock->base); + lock_wait_lock(&lock->base, &flags); __ww_mutex_check_waiters(&lock->base, ctx); - unlock_wait_lock(&lock->base); + unlock_wait_lock(&lock->base, flags); } =20 static __always_inline int --=20 2.40.0.577.gac1e443424-goog