Received: by 2002:ab2:2994:0:b0:1ef:ca3e:3cd5 with SMTP id n20csp549648lqb; Thu, 14 Mar 2024 21:41:00 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVQxRazH+fpAUJ+8qaf5QDH9dpOrtLlwsCbPeoxkk18trIWA0jlv9NAsrRQwtwz6U5BecRKsRg2p+KSI4KEnuvNuC2h4JoMA/5WjVOqWw== X-Google-Smtp-Source: AGHT+IFqj2BuuyeBCe0i+kwMg6K/Wgs/EyMGYZtbpE2+EsdpokKdSd/nDtpcHoW3MHBmOik71qRY X-Received: by 2002:a17:902:d4c1:b0:1dd:b67b:21c0 with SMTP id o1-20020a170902d4c100b001ddb67b21c0mr2728843plg.2.1710477660079; Thu, 14 Mar 2024 21:41:00 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1710477660; cv=pass; d=google.com; s=arc-20160816; b=SAEsJHPUwFC781/BrQeONg+sbiGkV0R7w+zIeQp2U08Hkvxd4mbFdc4eSRqkE+n8Qf V75+ShN9aZnS91W1ZoET6aYDtX8JINsmplvD0a6wGmIw+Bm96GoPQShoRIjcPuY1+dTB I8XmwGIKL4x3CITRvscDxoXcdWGkZgpiGJd2Fb+QYiXqb2dPsqGbBKknm9q9+Rzd79RQ nD9exz7hGmOflA2gvsmNjWC1Lk9VVpbqX2MCd06m2z4v1M2Q/3k/ljiqi/sErSDhaVKg 1FxWcukYg8S6DVG1V9DwlKJ0IoyQS1ohztvEaJEXZRZ010J2dFYKRCmU52FTwgdIlLi2 vRqQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :dkim-signature; bh=wPFpAa5+VGKsgJgb1JT18RP9N5QBHmzAN+rZSPB/N9U=; fh=y8KJUBnrwQA+5PtWX4i2qO6O4Uc2yYtIrVYD0yjhUrk=; b=ynMznzYR7v8qqKV6i4tL6FXy1Aq3JLFu2QFhhSr7qSSnITYlD3S4HEdJxi0+iZJjIX jStZySLXd01A+JEcG+nP1RX5e/x4IgsbIvNI7/4P28jE2+W+XMCJ5hJIccWXrjrsqOFY SXOQys7vt4jLuVMtXwGck0XPtEhY/6Bo0T6eFvEwUky+illWRoYMyxOKDlS9Cpmv3iCo wMMFDQ4YtAG0aurJZP3EFQK/CL/aTrRWG8EsQCRsa53Q1bDi0Wc7EG5OZTtqJCJGCeAY eJitjGk04Mgi+lcugrKou5We137zR4WVT43/zs198gE4Zd1kf9VBLhOI0T7lSkizTlDw qekw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=fGptibbk; arc=pass (i=1 spf=pass spfdomain=flex--jstultz.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-104049-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-104049-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id h15-20020a170902ac8f00b001dda3fa4180si2669426plr.618.2024.03.14.21.40.59 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Mar 2024 21:41:00 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-104049-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=fGptibbk; arc=pass (i=1 spf=pass spfdomain=flex--jstultz.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-104049-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-104049-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 7DA42B22285 for ; Fri, 15 Mar 2024 04:40:50 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 15469125C9; Fri, 15 Mar 2024 04:40:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="fGptibbk" Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EF8F6F9DE for ; Fri, 15 Mar 2024 04:40:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710477626; cv=none; b=blispntDP4z7VBDuwJRNry+EQJBpq6N8YiWlnwatxtb920R0pRh1bUH5H/jjzM8RBxqU0kUUs3MlDyKM5lyKAYtzeBGgQvvEOq0LwHtSIbF1p1BeGbF24oijkvmZgwsZv2ti6Yfw+oTL6SsbotE07xZIQxAugrCqsOJqplF37qg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710477626; c=relaxed/simple; bh=2FiN2H+43D3sx0OR6+I4fJnMUbMIax6oO3HYblar+8k=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Wv/tY0vhNQpORzifHvYZkf3P3Z/Vy2lKTiWxQZvv9YBvW8H3PO3sjmEcjvn8+03MOOw/sIj/rLb6v+z5biVhmfKZ80W0sNAWu3YxDR7VGGwngbEkQkWFvc+gl+SLFP5feuQLHr2G/yHJOlV8zODXt/vzKBiakHjyTeclGCzXAsw= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--jstultz.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=fGptibbk; arc=none smtp.client-ip=209.85.128.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--jstultz.bounces.google.com Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-60a2b82039bso31911597b3.1 for ; Thu, 14 Mar 2024 21:40:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1710477624; x=1711082424; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=wPFpAa5+VGKsgJgb1JT18RP9N5QBHmzAN+rZSPB/N9U=; b=fGptibbkGCzPDTJTQiyif4UEiWSx7yP0wVtA1Z5Jfh6KSmZEGkl8xU54drxHv4YBMP mBvSNcnzPlOOSiVuMjGo1UrikjrGZepvGaYCcNbEzhDX5u/Xzjhne9sI0iYMTQz4vqlG qSqnr2e0zRzaXpD3Q4LmkYaZloNJlrMXtQYWzeL3svcVXNOyJn/a+aC1C5OYddDdQZ6p O40ryN69+NxebDTrCaYLDQYSZ6QqQSWN/cie3L10LPdkGAleeEKdbuxB2ApiNCj4VV5L werbK/Qf6CYsDNuDv7bvjUgRbP/MjVdy5Goa+zZrph5Os62YmKcvlQvHdxTXXfA/P3u5 1sdA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710477624; x=1711082424; 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=wPFpAa5+VGKsgJgb1JT18RP9N5QBHmzAN+rZSPB/N9U=; b=FmyFGnkiQAE6dzQAHTKhIIkimlOB9omB8ohJr1zNeKAuewWuMea6jrAFMIUDJwomli ehrKkax7H1jDdETgyEggzCOXpmJXawvs1XCi7iH66q3By1d8Yv2U2AW/gXJNps2rWDPK NJ0GRxJJiaIeAp91yOUUxYGgo/gVUikqE0Z32V9eaYe8SxrEO3BLaC9ZlY4o1ejxaCZO Hx/6coqMTnsbvP5zZanVOGQLqpNwD27l+NYAlKGggR48JMFsWJNgiLU9CcdaCoH6SvNI WlpE5cG/mjjBQ1Vt+TMf4P6lfMGl9QRbxw+qCoKk9X7MG9nShWso/GEWUwD4VMme4cH3 yBjQ== X-Gm-Message-State: AOJu0YxnnG3em9okRxsHsW+JuiEvDNSZVzZD+p7Oll5AykYGCKDuFpw1 bMulNu/jh2Gj8cZAZV+CpDMqaFgshCZVmqUDcvMwz5LiYXoIecbK01Z6+VyFFQYEQqi9YAZwsdx 2Q7+BRSjALAVgEog9pa1cdF1iBxlNkyluz0BKYsa3SXw1LMwQk5H7tqbPsiz0cQvoIbi+tEuWT8 XdkJLl5FsjNPXl89RAxTY+P7CwjJQ49O6eZadFYDW0hra9 X-Received: from jstultz-noogler2.c.googlers.com ([fda3:e722:ac3:cc00:24:72f4:c0a8:600]) (user=jstultz job=sendgmr) by 2002:a05:6902:2748:b0:dc6:ebd4:cca2 with SMTP id ea8-20020a056902274800b00dc6ebd4cca2mr216205ybb.11.1710477622683; Thu, 14 Mar 2024 21:40:22 -0700 (PDT) Date: Thu, 14 Mar 2024 21:39:47 -0700 In-Reply-To: <20240315044007.2778856-1-jstultz@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240315044007.2778856-1-jstultz@google.com> X-Mailer: git-send-email 2.44.0.291.gc1ea87d7ee-goog Message-ID: <20240315044007.2778856-3-jstultz@google.com> Subject: [PATCH v9 2/7] 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 , Youssef Esmat , Mel Gorman , Daniel Bristot de Oliveira , Will Deacon , Waiman Long , Boqun Feng , "Paul E. McKenney" , Metin Kaya , Xuewen Yan , K Prateek Nayak , Thomas Gleixner , kernel-team@android.com, "Connor O'Brien" , John Stultz 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: Youssef Esmat Cc: Mel Gorman Cc: Daniel Bristot de Oliveira Cc: Will Deacon Cc: Waiman Long Cc: Boqun Feng Cc: "Paul E. McKenney" Cc: Metin Kaya Cc: Xuewen Yan Cc: K Prateek Nayak Cc: Thomas Gleixner Cc: kernel-team@android.com 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. v7: * Fix function definition for PREEMPT_RT case, as pointed out by Metin Kaya. * Fix incorrect flags handling in PREEMPT_RT case as found by Metin Kaya --- kernel/locking/mutex.c | 18 ++++++++++-------- kernel/locking/ww_mutex.h | 22 +++++++++++----------- 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/kernel/locking/mutex.c b/kernel/locking/mutex.c index 980ce630232c..7de72c610c65 100644 --- a/kernel/locking/mutex.c +++ b/kernel/locking/mutex.c @@ -578,6 +578,7 @@ __mutex_lock_common(struct mutex *lock, unsigned int state, unsigned int subclas DEFINE_WAKE_Q(wake_q); struct mutex_waiter waiter; struct ww_mutex *ww; + unsigned long flags; int ret; if (!use_ww_ctx) @@ -620,7 +621,7 @@ __mutex_lock_common(struct mutex *lock, unsigned int state, unsigned int subclas return 0; } - raw_spin_lock(&lock->wait_lock); + raw_spin_lock_irqsave(&lock->wait_lock, flags); /* * After waiting to acquire the wait_lock, try again. */ @@ -681,7 +682,7 @@ __mutex_lock_common(struct mutex *lock, unsigned int state, unsigned int subclas goto err; } - raw_spin_unlock(&lock->wait_lock); + raw_spin_unlock_irqrestore(&lock->wait_lock, flags); /* Make sure we do wakeups before calling schedule */ if (!wake_q_empty(&wake_q)) { wake_up_q(&wake_q); @@ -707,9 +708,9 @@ __mutex_lock_common(struct mutex *lock, unsigned int state, unsigned int subclas trace_contention_begin(lock, LCB_F_MUTEX); } - 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); @@ -735,7 +736,7 @@ __mutex_lock_common(struct mutex *lock, unsigned int state, unsigned int subclas if (ww_ctx) ww_mutex_lock_acquired(ww, ww_ctx); - raw_spin_unlock(&lock->wait_lock); + raw_spin_unlock_irqrestore(&lock->wait_lock, flags); wake_up_q(&wake_q); preempt_enable(); return 0; @@ -745,7 +746,7 @@ __mutex_lock_common(struct mutex *lock, unsigned int state, 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); wake_up_q(&wake_q); @@ -916,6 +917,7 @@ static noinline void __sched __mutex_unlock_slowpath(struct mutex *lock, unsigne struct task_struct *next = NULL; DEFINE_WAKE_Q(wake_q); unsigned long owner; + unsigned long flags; mutex_release(&lock->dep_map, ip); @@ -943,7 +945,7 @@ static noinline void __sched __mutex_unlock_slowpath(struct mutex *lock, unsigne } preempt_disable(); - 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: */ @@ -960,7 +962,7 @@ static noinline void __sched __mutex_unlock_slowpath(struct mutex *lock, unsigne if (owner & MUTEX_FLAG_HANDOFF) __mutex_handoff(lock, next); - raw_spin_unlock(&lock->wait_lock); + raw_spin_unlock_irqrestore(&lock->wait_lock, flags); wake_up_q(&wake_q); preempt_enable(); } diff --git a/kernel/locking/ww_mutex.h b/kernel/locking/ww_mutex.h index 7189c6631d90..9facc0ddfdd3 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; } -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); } -static inline void unlock_wait_lock(struct mutex *lock) +static inline void unlock_wait_lock(struct mutex *lock, unsigned long *flags) { - raw_spin_unlock(&lock->wait_lock); + raw_spin_unlock_irqrestore(&lock->wait_lock, *flags); } 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); } -static inline void lock_wait_lock(struct rt_mutex *lock) +static inline void lock_wait_lock(struct rt_mutex *lock, unsigned long *flags) { - raw_spin_lock(&lock->rtmutex.wait_lock); + raw_spin_lock_irqsave(&lock->rtmutex.wait_lock, *flags); } -static inline void unlock_wait_lock(struct rt_mutex *lock) +static inline void unlock_wait_lock(struct rt_mutex *lock, unsigned long *flags) { - raw_spin_unlock(&lock->rtmutex.wait_lock); + raw_spin_unlock_irqrestore(&lock->rtmutex.wait_lock, *flags); } static inline void lockdep_assert_wait_lock_held(struct rt_mutex *lock) @@ -380,6 +380,7 @@ static __always_inline void ww_mutex_set_context_fastpath(struct ww_mutex *lock, struct ww_acquire_ctx *ctx) { DEFINE_WAKE_Q(wake_q); + unsigned long flags; ww_mutex_lock_acquired(lock, ctx); @@ -408,10 +409,9 @@ ww_mutex_set_context_fastpath(struct ww_mutex *lock, struct 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, &wake_q); - unlock_wait_lock(&lock->base); - + unlock_wait_lock(&lock->base, &flags); wake_up_q(&wake_q); } -- 2.44.0.291.gc1ea87d7ee-goog