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 E1DB73ECBFD for ; Mon, 27 Apr 2026 18:38:55 +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=1777315137; cv=none; b=F+zvLT9SR04soEU59NZHTvM7QCemHB0SpB0NpLuxAIPZSP8w6YMw6zQl5s3TAsV2qjmHTYxAaKZx+gA1mcXqua9VghlWQ4KHG5LI0W1vEpdU5MvLKIa9KC7nggBCeQGoYkDSIHRKqBYyHN27VCQCanbdV3lNQv3XLPvRy4o7JOI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777315137; 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=If4y3br8UDRbMbprM90o3KFwbsJaaLI0FqxxHYJ9Fk5zMNu7Rol4r5qkKxR/rnINhDBSkk1sE0anRt2dEbcxfcivjPT5+g7yMJbOh9MARsllKBmTL5gYvXG/QkRHg7bcd6T+81H7P0oQDR1sjGPvFHKnQDVoLyOVQt2QmsHm1z8= 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=rSPN8kZ7; 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="rSPN8kZ7" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-35e5791871fso12830125a91.3 for ; Mon, 27 Apr 2026 11:38:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1777315135; x=1777919935; 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=rSPN8kZ7M5zayFxtg/DhJYUQScMVzLxYipv+GNRL3i10jPlTkZDtAVUlkTqEeLifaw v3hxay7a8fLJ8CynYDenMMc3d3GfzQUdB7+Kj/BY0eiqTWR+eAKOpt8d/KeyKVO/Yklm vkgkizx7Rr0+MlcwDlflR9VvjtWrx3TALdFLX6uIuElo9SqhGw5zzrlWJOWuHWk2fPmU bmrqZ935HPV/aMmcLEDoV5f0uWIUMyOwnV587yKV60iasRt9qej7mgya/MRaUpVYeIAK 0amZQjo78XNYPvqWZIfLOHv2RoxWmpzWTMihESoDH3I8ubXN/qw3HlovJhwpZeaFvBae u7FA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1777315135; x=1777919935; 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=rT7NcmLieuXnCJPRcmec6V0eQ8jl5TLwfT75DkKVNFVfRgp/55TAaRGcDZT4lujWZS EgV+rUhlKOWelpAhavKFmmeQBGb6VWnNrT5to3wXg3/yXdNpxBgmGnrBXJoSdxwnVVQN A012RPdEkVGyqI5Z1v8v77QtCfh6PLskvKRva46+Gr8z4X6Ca9r5sPPlxnhvDiBDIT7l j1HIe9vQfM3UOqaDoZzH3qJJE1Wi6N9xcYHfgOR6Mc8EcLUI55tTus+faWrUHO3agiNz cCxzX4wCSqTD1zyybYBovR3fdagS+8APw9fcSr7gTpnVnJKS+orb6frl4oaC5Q3WW3bQ iBJg== X-Gm-Message-State: AOJu0YxSbdiBwx2noPoTW3ZCljB5750uj0yoSJqmz1kGyarzaHdDaVi9 rYWiWydZxi3XZ9+tbY8yW5YOZEnBd4E+CiLAHbveaNq+nxPfJleDuW3DqkTq6C/5xIb1bP8C2hC NXDTbpCnSM0cvcKweaQrTKpnYsMvBbz4Z4JOEL1h9MRIJfkWO/vzPNnht20tuus/jIvYTaIYKSI gZFF5dXALPZ5FkLM4f5bLNIyCAfWfCJh0QFjSVxdsLUUR+DyyV X-Received: from plti2.prod.google.com ([2002:a17:902:6ac2:b0:2b2:eb2c:c86e]) (user=jstultz job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:3d84:b0:35d:8f3d:c554 with SMTP id 98e67ed59e1d1-36490c6437cmr263197a91.13.1777315134844; Mon, 27 Apr 2026 11:38:54 -0700 (PDT) Date: Mon, 27 Apr 2026 18:38:41 +0000 In-Reply-To: <20260427183848.698551-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: <20260427183848.698551-1-jstultz@google.com> X-Mailer: git-send-email 2.54.0.545.g6539524ca2-goog Message-ID: <20260427183848.698551-3-jstultz@google.com> Subject: [PATCH 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