Received: by 2002:a05:7412:8521:b0:e2:908c:2ebd with SMTP id t33csp2357471rdf; Mon, 6 Nov 2023 11:37:32 -0800 (PST) X-Google-Smtp-Source: AGHT+IHJw+ef+H813c/z90jwlalS3zav7ZBwpb7YhbKEODVMOaAxVW/zeQnAnzpRqrntTIllFqSv X-Received: by 2002:a05:6a00:10c6:b0:690:c887:8cd8 with SMTP id d6-20020a056a0010c600b00690c8878cd8mr26581157pfu.32.1699299452580; Mon, 06 Nov 2023 11:37:32 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699299452; cv=none; d=google.com; s=arc-20160816; b=iWyuXk4RO3WuG+1TyYZ7lFj+rHgkGRsUTv8iY52ZzLP2uMoK/24EHmwf+M5XybUjjB +ynQ+78+OSk30FSk4TsRoDB65EZzYaCGCeaVdcd0a/OpTV15tQE4qKMjF5LhkaA8Hj6h jmIEya2Ex8hpKGRmCAGwtDmUQU2sOiC5ocYPgcuoLhOAdg6FmGlYNJJ2PSuVt81KCENA wnwKCZneSbjjeMZ6tenDUlk/FLyFzRBaa2J3zp7LILZ/UlR3wz7Elh5rzenATcine6kP Ptjj6woa2fWpsn1g0dD1Y+dJvqk8rYjrIXU4U05Ervbso/fn0ajdpWgmmOAdDl/Y61sL Ch9A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=jkWqsamzbkDYVkOMHZ9o6Mm5vyOWDIPUHoqOezvBTm8=; fh=UoUedsFOp4RBNYBZdfJu/lWCoRphcxbsDxun+uNZnAU=; b=zds9Y2CxSj698pXF7eDmfsRNWkrYPgJgA+zGklry7cuaAUiahLSd93JP8RgpZp8pOG E1VAl3qFKZ+Fg8Kn7vO0/lvsjgGA9C+DvOrmIcNQ9AuL9caf94iMY22MLwWpOGa9NoYh aEJqWgSyKKUHTpYcf7wn2PIpKnxJYnQaPpa1eob99BN2863cFFPDeW3RL7/XZwYxAWz4 aHbq6/RdazfujrlkNX4YNCzZ8N9wp6v9q1tyMgrHTTAF4gaT3ZzBppYkrADpigZAcYF5 rzMhPkRn9ETjzxeCoh9qQBtxEfhH6huupP+H515k1t6N2/8an/QG5ER8q2sfOao/7fq4 c7+Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=n61RZnkX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from agentk.vger.email (agentk.vger.email. [2620:137:e000::3:2]) by mx.google.com with ESMTPS id y17-20020aa78551000000b006bd82e2d31dsi8349537pfn.384.2023.11.06.11.37.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Nov 2023 11:37:32 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) client-ip=2620:137:e000::3:2; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=n61RZnkX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id 18D6580C59BE; Mon, 6 Nov 2023 11:37:23 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232982AbjKFTgK (ORCPT + 99 others); Mon, 6 Nov 2023 14:36:10 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52300 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232939AbjKFTfz (ORCPT ); Mon, 6 Nov 2023 14:35:55 -0500 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9E67010CF for ; Mon, 6 Nov 2023 11:35:47 -0800 (PST) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-5b59662ff67so26976577b3.0 for ; Mon, 06 Nov 2023 11:35:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1699299347; x=1699904147; 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=jkWqsamzbkDYVkOMHZ9o6Mm5vyOWDIPUHoqOezvBTm8=; b=n61RZnkXf160k8YCjVauYORtV1bmgd2KwyBt/E1hJRXs8M0Kbz137QgFTd1+99tPEl bkdHoQOc6/wRV7xTdN1o4NF1C2IJSEBhFR87CAyUjrpftjVXYqJ/xtltXMWXxJaY6/bk mPTlcqd+rGoYo41LZnTCDb4jZ2u3nXCZKeVoQswRGFua6cEJzcnQR9vbPdMoCUPic9Pa MJ0bQLEa8MWWJPRO7xz7O6gmWoRpUoFbkmW1m7BTS9D0iPHPP8GBjDuiHaoe80vef1Fa YY7xLEoG8HO1MuRQoWeciLp5h2ZjBJBM8mjzYycSHE1AneeL+Fe4AlurF0hPp1+ePBEh gGhQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699299347; x=1699904147; 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=jkWqsamzbkDYVkOMHZ9o6Mm5vyOWDIPUHoqOezvBTm8=; b=UA2E2bnLrfAt7GK4glFglhYvkyklYOJ8n8jeQbr2ldPL5/w22X4TeNzbQIJRWWy7gQ W85WVZz5GbOnaH5bzipnkZUa8ad8t5HfnbrWDIWwRFKFH2jEvXBtdmTzQqB+kUSRmuJ3 FAbGIaGZdmstgX8JL79Vc/lOpCYr4lihAtF1/mJjpnfms0o1g2qC4VZPFJnEGWKpY+45 gLzH9/jQqNb3wk54FPPTj5bBlRTCThSKzVzTlNzp35HwSGvfMjFw+D+64iYCK9EinbcT mu5cX7NmjgiBbLEtDBe+nrG4EYJVyhlYb3206DPxAwNyQIOelPSwSsAswKRCnIBwgYon eftQ== X-Gm-Message-State: AOJu0Yxoj717uG5TmplFPx2NCx9GpddXCm4ZT855GzyiY1+mZ4Uhxx8B ekGZx4GUgSZrN+qKqnO0jpDkjKv1IwyeYb2HMG7Ap90QBUBsE9kDMa3mO2PL5kaMEN7WOTtvQGo BAWDJHcxAM278ld14yOYifV4tE5f38BB6+NH3DjYVaJxp3jqkPiKenKgLMIKbKTDQSo/EXbw= X-Received: from jstultz-noogler2.c.googlers.com ([fda3:e722:ac3:cc00:24:72f4:c0a8:600]) (user=jstultz job=sendgmr) by 2002:a25:d28e:0:b0:da0:37e1:558e with SMTP id j136-20020a25d28e000000b00da037e1558emr13029ybg.6.1699299346350; Mon, 06 Nov 2023 11:35:46 -0800 (PST) Date: Mon, 6 Nov 2023 19:34:48 +0000 In-Reply-To: <20231106193524.866104-1-jstultz@google.com> Mime-Version: 1.0 References: <20231106193524.866104-1-jstultz@google.com> X-Mailer: git-send-email 2.42.0.869.gea05f2083d-goog Message-ID: <20231106193524.866104-6-jstultz@google.com> Subject: [PATCH v6 05/20] locking/mutex: Rework task_struct::blocked_on From: John Stultz To: LKML Cc: Peter Zijlstra , Joel Fernandes , Qais Yousef , Ingo Molnar , Juri Lelli , 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" , kernel-team@android.com, "Connor O'Brien" , John Stultz Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (agentk.vger.email [0.0.0.0]); Mon, 06 Nov 2023 11:37:23 -0800 (PST) From: Peter Zijlstra Track the blocked-on relation for mutexes, this allows following this relation at schedule time. task | blocked-on v mutex | owner v task 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: kernel-team@android.com Signed-off-by: Peter Zijlstra (Intel) [minor changes while rebasing] Signed-off-by: Juri Lelli Signed-off-by: Peter Zijlstra (Intel) Signed-off-by: Connor O'Brien [jstultz: Fix blocked_on tracking in __mutex_lock_common in error paths] Signed-off-by: John Stultz --- v2: * Fixed blocked_on tracking in error paths that was causing crashes v4: * Ensure we clear blocked_on when waking ww_mutexes to die or wound. This is critical so we don't get ciruclar blocked_on relationships that can't be resolved. v5: * Fix potential bug where the skip_wait path might clear blocked_on when that path never set it * Slight tweaks to where we set blocked_on to make it consistent, along with extra WARN_ON correctness checking * Minor comment changes --- include/linux/sched.h | 5 +---- kernel/fork.c | 3 +-- kernel/locking/mutex-debug.c | 9 +++++---- kernel/locking/mutex.c | 10 ++++++++++ kernel/locking/ww_mutex.h | 17 +++++++++++++++-- 5 files changed, 32 insertions(+), 12 deletions(-) diff --git a/include/linux/sched.h b/include/linux/sched.h index 4f5b0710c0f1..22a6ac47d5fb 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -1145,10 +1145,7 @@ struct task_struct { struct rt_mutex_waiter *pi_blocked_on; #endif -#ifdef CONFIG_DEBUG_MUTEXES - /* Mutex deadlock detection: */ - struct mutex_waiter *blocked_on; -#endif + struct mutex *blocked_on; /* lock we're blocked on */ #ifdef CONFIG_DEBUG_ATOMIC_SLEEP int non_block_count; diff --git a/kernel/fork.c b/kernel/fork.c index 3b6d20dfb9a8..1c3f7eaa9239 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -2455,9 +2455,8 @@ __latent_entropy struct task_struct *copy_process( lockdep_init_task(p); #endif -#ifdef CONFIG_DEBUG_MUTEXES p->blocked_on = NULL; /* not blocked yet */ -#endif + #ifdef CONFIG_BCACHE p->sequential_io = 0; p->sequential_io_avg = 0; diff --git a/kernel/locking/mutex-debug.c b/kernel/locking/mutex-debug.c index bc8abb8549d2..7228909c3e62 100644 --- a/kernel/locking/mutex-debug.c +++ b/kernel/locking/mutex-debug.c @@ -52,17 +52,18 @@ void debug_mutex_add_waiter(struct mutex *lock, struct mutex_waiter *waiter, { lockdep_assert_held(&lock->wait_lock); - /* Mark the current thread as blocked on the lock: */ - task->blocked_on = waiter; + /* Current thread can't be already blocked (since it's executing!) */ + DEBUG_LOCKS_WARN_ON(task->blocked_on); } void debug_mutex_remove_waiter(struct mutex *lock, struct mutex_waiter *waiter, struct task_struct *task) { + struct mutex *blocked_on = READ_ONCE(task->blocked_on); + DEBUG_LOCKS_WARN_ON(list_empty(&waiter->list)); DEBUG_LOCKS_WARN_ON(waiter->task != task); - DEBUG_LOCKS_WARN_ON(task->blocked_on != waiter); - task->blocked_on = NULL; + DEBUG_LOCKS_WARN_ON(blocked_on && blocked_on != lock); INIT_LIST_HEAD(&waiter->list); waiter->task = NULL; diff --git a/kernel/locking/mutex.c b/kernel/locking/mutex.c index 2c5d1a9cf767..73064e4865b7 100644 --- a/kernel/locking/mutex.c +++ b/kernel/locking/mutex.c @@ -622,6 +622,7 @@ __mutex_lock_common(struct mutex *lock, unsigned int state, unsigned int subclas goto err_early_kill; } + current->blocked_on = lock; set_current_state(state); trace_contention_begin(lock, LCB_F_MUTEX); for (;;) { @@ -662,6 +663,10 @@ __mutex_lock_common(struct mutex *lock, unsigned int state, unsigned int subclas first = __mutex_waiter_is_first(lock, &waiter); + /* + * Gets reset by unlock path(). + */ + current->blocked_on = lock; set_current_state(state); /* * Here we order against unlock; we must either see it change @@ -682,6 +687,7 @@ __mutex_lock_common(struct mutex *lock, unsigned int state, unsigned int subclas } raw_spin_lock_irqsave(&lock->wait_lock, flags); acquired: + current->blocked_on = NULL; __set_current_state(TASK_RUNNING); if (ww_ctx) { @@ -712,9 +718,11 @@ __mutex_lock_common(struct mutex *lock, unsigned int state, unsigned int subclas return 0; err: + current->blocked_on = NULL; __set_current_state(TASK_RUNNING); __mutex_remove_waiter(lock, &waiter); err_early_kill: + WARN_ON(current->blocked_on); trace_contention_end(lock, ret); raw_spin_unlock_irqrestore(&lock->wait_lock, flags); debug_mutex_free_waiter(&waiter); @@ -926,6 +934,8 @@ static noinline void __sched __mutex_unlock_slowpath(struct mutex *lock, unsigne next = waiter->task; debug_mutex_wake_waiter(lock, waiter); + WARN_ON(next->blocked_on != lock); + next->blocked_on = NULL; wake_q_add(&wake_q, next); } diff --git a/kernel/locking/ww_mutex.h b/kernel/locking/ww_mutex.h index 8b94f4b89e74..8bb334491732 100644 --- a/kernel/locking/ww_mutex.h +++ b/kernel/locking/ww_mutex.h @@ -284,6 +284,13 @@ __ww_mutex_die(struct MUTEX *lock, struct MUTEX_WAITER *waiter, #ifndef WW_RT debug_mutex_wake_waiter(lock, waiter); #endif + /* + * When waking up the task to die, be sure to clear the + * blocked_on pointer. Otherwise we can see circular + * blocked_on relationships that can't resolve. + */ + WARN_ON(waiter->task->blocked_on != lock); + waiter->task->blocked_on = NULL; wake_q_add(wake_q, waiter->task); } @@ -331,9 +338,15 @@ static bool __ww_mutex_wound(struct MUTEX *lock, * it's wounded in __ww_mutex_check_kill() or has a * wakeup pending to re-read the wounded state. */ - if (owner != current) + if (owner != current) { + /* + * When waking up the task to wound, be sure to clear the + * blocked_on pointer. Otherwise we can see circular + * blocked_on relationships that can't resolve. + */ + owner->blocked_on = NULL; wake_q_add(wake_q, owner); - + } return true; } -- 2.42.0.869.gea05f2083d-goog