From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (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 2DDCB3542D4 for ; Thu, 30 Apr 2026 21:51:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777585876; cv=none; b=dj7EtjtyI3HMmlmnpxlYcwVMU9StXXbcK/Nwqs4D3pt+VoCTr/a9rIPD0I96UPEIgD2V38bFODEPmBnAVDj+0FTmQfQrI56WfA15IOHWa7L/hDEeL0lxMolC2xx8nrCKLIxyyfetBdqH+4a4IZSy0zCzlgpoTuyTLKRvkE7PiSQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777585876; c=relaxed/simple; bh=x/Wh1D+yXamvOjv3wRMZFBY9yDBFNOH7/v8a82glpZg=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=kovaAZ7lTBf9kcLHllPhFn/CxspEbk0igAdhUO9TBLji2cKuYBqpeirEfZENZA/CzprPgCasny/FD3WYUqrThlfZ/LvU7+pqY4d+bFYtNXugDo0/qlUb5tw7wIjxtDBm9znbe9RWYI2zCadvD4CHOFDGUpnE94rB6sI3btCuNMM= 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=p4SvThaz; arc=none smtp.client-ip=209.85.216.74 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="p4SvThaz" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-35d9e67f6dcso3106967a91.1 for ; Thu, 30 Apr 2026 14:51:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1777585874; x=1778190674; 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=LCFOqcRmwNjHX26koROMD9CdgEZKlERSoYkhoq5pjSA=; b=p4SvThazKt2XEauxuxo6Ll6s5gzl5bE80s7j3Ss/XRsvNhQgs4FT23wXcvNI98Y122 T+6NucZ9OdtWQlkyjDDIEF8NxdXgqWOgAZZMxwEgRiwaFjBHzvar0AueEle52ndX+57F epDFvEYS7/Sp/1Kah46e4ts8rVGI6xeQMBLTOtJk9AX2QUMWaUQab+xOWmPUztkm8Jch BfSF5dC0O6/Dcmq1Ovhekr7jFKMLyji9zHL8IknXUbfRTpC92VI/27vulNxrL8tgLpuh hqJhzT25mK9JbZVQEcRonUFUhcnpHUvIXhSEB3zDgbcRcmeVlJyFFF26hGGW8l4dOnHc 0cqQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1777585874; x=1778190674; 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=LCFOqcRmwNjHX26koROMD9CdgEZKlERSoYkhoq5pjSA=; b=eUbBNDRW2Bnsyf3P1gMUZxhvolgTfZnUB6ZNC4zs4BA8D3QW4ByA6MNaKBwK4b1xci Xivnd+m682AWj2Hrd0Wj1/vWo1bI//P3OFpmFvaIGeNss90t15+0fBjpmIwqsdkbFHX7 orc2AK+AJCYSwfIZrWecccGKDl1/pJMPrUefvm0TkFsNLhxqAi2g68WtT2kFcIxLeDij FAh9MaxoqF/a0Q2EClsXlP03BIedVs6/ADoMWclNJWaaMfYdOgrRaUjwcP2nyzycDRTY xZZh+Ile+BlxQQ6CbCR0vpNy2tBjfiKQwKeG9LEbfGAyRR3hW6rb4iA+Y+cZ1Ln3+3+R ajrQ== X-Gm-Message-State: AOJu0Yylf5jLU/3A2oI0S2q3wy1YZ4EMzHHHtjVq8zbSCb9AA816lMj7 Waba5p5MXjX9BdMupMnI0BzXplSDFMi6CSKIdZiyX/hFQ5oqS0viSyY0RU9DCcy9U2UVm07CJWf qt/Sjazm404JBZ4aDyKuMuAOU+AQJkZ6ZDB2TS/nl9BL+L614a7sntTGs3Sc82lfvOX6h095c8q GRW5zSQKb/7zavOh4aS75AEUh0tKQFfrivFLniEJ/4vi6qCleC X-Received: from pgid17.prod.google.com ([2002:a63:ed11:0:b0:c79:85be:4c5c]) (user=jstultz job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:1f86:b0:359:1130:1047 with SMTP id 98e67ed59e1d1-364c304ad91mr5572237a91.17.1777585874320; Thu, 30 Apr 2026 14:51:14 -0700 (PDT) Date: Thu, 30 Apr 2026 21:50:47 +0000 In-Reply-To: <20260430215103.2978955-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: <20260430215103.2978955-1-jstultz@google.com> X-Mailer: git-send-email 2.54.0.545.g6539524ca2-goog Message-ID: <20260430215103.2978955-3-jstultz@google.com> Subject: [PATCH v2 2/2] locking: mutex: Fix proxy-exec potentially deactivating tasks marked TASK_RUNNING From: John Stultz To: LKML Cc: John Stultz , Vineeth Pillai , Sonam Sanju , Sean Christopherson , Kunwu Chan , Tejun Heo , Joel Fernandes , Qais Yousef , Ingo Molnar , Peter Zijlstra , Juri Lelli , Vincent Guittot , Dietmar Eggemann , Valentin Schneider , Steven Rostedt , Will Deacon , Waiman Long , Boqun Feng , "Paul E. McKenney" , Metin Kaya , Xuewen Yan , K Prateek Nayak , Thomas Gleixner , Daniel Lezcano , Suleiman Souhlal , kuyo chang , hupu , kernel-team@android.com Content-Type: text/plain; charset="UTF-8" Vineeth found came up with a test driver that could trip up workqueue stalls. After fixing one issue this test found, Vineeth reported the test was still failing. Greatly simplified, a task that tries to take a mutex already owned by another task that is sleeping, can hit a edge case in the mutex_lock_common() case. If the task fails to get the lock, calls into schedule, but gets a spurious wakeup, it will find that it is first waiter, and go into the mutex_optimistic_spin() logic. Though before calling mutex_optimistic_spin(), we clear task blocked_on state, since mutex_optimistic_spin() may call schedule() if need_resched() is set. After mutex_optimistic_spin() fails, we set blocked_on again, restart the main mutex loop, try to take the lock and call into schedule_preempt_disabled(). >From there, with proxy-execution, we'll see the task is blocked_on, follow the chain, see the owner is sleeping and dequeue the waiting task from the runqueue. This all sounds fine and reasonable. But what I had missed is that in mutex_optimistic_spin(), not only do we call schedule() but we set TASK_RUNNABLE right before doing so. This is ok for that invocation of schedule(). But when we come back we re-set the blocked_on we had just cleared, but we do not re-set the task state to TASK_INTERRUPTIBLE/UNINTERRUPTIBLE. This means we have a task that is blocked_on & TASK_RUNNABLE, so when the proxy execution code dequeues the task, we are in trouble since future wakeups will be shortcut by the ttwu_state_match() check. Thus, to avoid this, after mutex_optimistic_spin(), set the task state back when we set blocked_on. Many many thanks again to Vineeth for his very useful testing driver that uncovered this long hidden bug, that I hadn't tripped in all my testing! Very impressed with the problems he's uncovered! Reported-by: Vineeth Pillai Tested-by: Vineeth Pillai Signed-off-by: John Stultz --- Cc: Vineeth Pillai Cc: Sonam Sanju Cc: Sean Christopherson Cc: Kunwu Chan Cc: Tejun Heo 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: 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: Daniel Lezcano Cc: Suleiman Souhlal Cc: kuyo chang Cc: hupu Cc: kernel-team@android.com --- kernel/locking/mutex.c | 1 + 1 file changed, 1 insertion(+) diff --git a/kernel/locking/mutex.c b/kernel/locking/mutex.c index 09534628dc01a..a93d4c6bee1a3 100644 --- a/kernel/locking/mutex.c +++ b/kernel/locking/mutex.c @@ -763,6 +763,7 @@ __mutex_lock_common(struct mutex *lock, unsigned int state, unsigned int subclas raw_spin_lock_irqsave(&lock->wait_lock, flags); raw_spin_lock(¤t->blocked_lock); __set_task_blocked_on(current, lock); + set_current_state(state); if (opt_acquired) break; -- 2.54.0.545.g6539524ca2-goog