From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pg1-f181.google.com (mail-pg1-f181.google.com [209.85.215.181]) (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 A1E123F4852 for ; Fri, 17 Jul 2026 12:55:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.181 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784292925; cv=none; b=BJbzpjopGlYJpoGvWbAaHLayy/G6Q6zrkw+5rlpnvRrckIiQ934mLanUsYMgq4fCs06EU9vfvj5SykGg9Hpj27NsdJ/Amo+GsUkcpyrzeGPZKzcNgnY8VR8dIVdHrTxhCmrqdur0jRb5A1BAIfc2j5lWUUtLk2O99ZK8nPx55CA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784292925; c=relaxed/simple; bh=tLwYsfhph1B4flzTU5fZWVqNoNPKtRUdoOfJ/QexRPg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=AOBnwAtvzI+7EoecZdAbLFyYcLFBZj9NA1ESc6xJxABbyyn03QdV9oMBBSH18/HzCpRpX72whl/KZQJgJTNcJrDbtku52XApFQ9zPdWi79vBDalNJjIrgsnXbmnI/7NEf4smi/Enin8zmCU+b80WtRSxwxNT6T4m6Qj2HKpiWu0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=CE7Vf5ev; arc=none smtp.client-ip=209.85.215.181 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="CE7Vf5ev" Received: by mail-pg1-f181.google.com with SMTP id 41be03b00d2f7-c88a4d79ba5so5281146a12.2 for ; Fri, 17 Jul 2026 05:55:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1784292923; x=1784897723; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=RT5HTLdD9zgx6KswEGGYDXfzampdR0sWmHYJQRGhq9E=; b=CE7Vf5evRuA1KMGeiGtlA0Oy1UU/aQxusjQFT9f4ZR2P2kj/EFDzemnlnOf9wxs7Qy S4WNtOzjvNt3E2VgqRLe3r63CKjmwmEB19WRGq6faCrNj0OZ5A35tmwdgzNJZk6E8lUD EpL/dfHX/Afylkid/pOift/HFVHtTIt8RAUk3BuDmRrVz+IH1CP3BkQM8AidI6ZQXYc3 e29QdAUlKspL6LAg8dht58hrKGFIbYzdjCIeZLS5tZD8F4kPdVk7J6lw4/wvmnpsBUyE PUSkoZ9yGklnSlmVjc9jScsIWqSRK2VFRYT3M4q9yHDtimLG/DuczZmWqLJBnBuT2NHB rXYQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1784292923; x=1784897723; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=RT5HTLdD9zgx6KswEGGYDXfzampdR0sWmHYJQRGhq9E=; b=Z/4nlPJaxyTFao8jM5EU4V0dkzBTsifv8zl2yOatWb/P/dYEc7GdVu5Tsj+rNyKC7w MzfHnEfVD2ucEFkZRb0F89fWdRvBuBd+JV2kTqd3dirOvEqzkVwFWyQdqrmgvbRDz7gE 6ej4EY7aGlRbg+cAVLWPSxH5+JRXqFNBlPzLNaWLziZCfIudEGGR6Y3J/apRFDb98me5 sdW6tf4+t6ThiiBx0TLuFiwoQMSxlFYNplil2LJ72pVz7F9UKx9kXZpwde+moSfP9UJT bFubicuMHDpIPvTfeP/vtUzpYIu9ICYxQP+tkSSXubLPqYZdyxeBanc/1iI1sHQyFjha MNjw== X-Forwarded-Encrypted: i=1; AHgh+Rpk7ea9520QfJYe8G04RvQp9zayoip/XF8kjwMAjdfe38VSxF40QxKdM9x5/25yoC37ZYHUc/YWiLPQNCg=@vger.kernel.org X-Gm-Message-State: AOJu0Yy5QSoq6fsHjaIpRvpe8ZrsBJfepyn4V7CX9GJDYReo1xHlgcte YwVYQ5zEuynd5jGRP1yBUPFk9Fy5TM5dbTS5PQrOxyx1zPb7dsjAzsPs X-Gm-Gg: AfdE7cng09I6FALzdBwZFr2+ak+JhDbAGP2eqy5BJ+Bv1yOGIhoeK3gQ17sSVAL55wD S6MiHUxbtz0JuscyIHCE7gTYsob2JSewBw4zxALOggA3o9mCA8kJxk4/UaSB0EEXUifpxPwules FIZ45vhb3+cgU7E60dux6gsL8MNiCtAJMLPE+WU4dxnnkmm47HfcUUiHMhWrDBlf5AIwEXiNwZj vwYWKcwm+x92m04uh1+Jr4XX1c6ajIffzr9/Pv0RcF7jrlzVxxFQeTNk/VJ/dY2Li94/4ynAnNM /uHVC99fCNRSb48hkIlHDap/gZRWREkoYb+w67p9AT829Xfk/JEZ0segRYLMQ4HwIijWr1JLQ7g kz06/siM7iarO5fU5t+DD3hXxxP0uOpwTGZ7pOGDi0Gt3cq+0BOZ4j5pHI3QTK0r+/BjpPOhrO3 ouKvh5g0y7FdcsFxKuMGloeJxX4eVuzJ/QbL0yRYj+7BJTRmZWYKEiAQ== X-Received: by 2002:a05:6a20:d81b:b0:3bf:d0c8:2aa4 with SMTP id adf61e73a8af0-3c3ad5d5b4amr2864882637.8.1784292921338; Fri, 17 Jul 2026 05:55:21 -0700 (PDT) Received: from mi-HP-ProDesk-680-G6-PCI-Microtower-PC.mioffice.cn ([43.224.245.226]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-cb519de2fb2sm806564a12.24.2026.07.17.05.55.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 17 Jul 2026 05:55:21 -0700 (PDT) From: "zhidao su (Xiaomi)" To: K Prateek Nayak , John Stultz Cc: Peter Zijlstra , Ingo Molnar , Juri Lelli , Vincent Guittot , Dietmar Eggemann , Steven Rostedt , Ben Segall , Mel Gorman , Valentin Schneider , linux-kernel@vger.kernel.org, "zhidao su (Xiaomi)" Subject: [PATCH v4] sched/proxy_exec: Detect cycles in proxy walks Date: Fri, 17 Jul 2026 20:55:12 +0800 Message-ID: <20260717125512.865145-1-soolaugust@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <3ab1c896-7b33-4d39-85cd-3ab78f3a5bf8@amd.com> References: <3ab1c896-7b33-4d39-85cd-3ab78f3a5bf8@amd.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit find_proxy_task() can keep walking the same blocked_on chain if the chain contains a cycle. This happens with a simple A->B->A mutex deadlock under proxy execution, and can leave the CPU spinning in __schedule() with the rq lock held. Use the rq pick sequence as a per-walk marker. Mark each task visited by the current proxy walk. If the walk sees the same marker again, break the cycle by clearing the blocked_on state at the detection point and deactivating that task. The marker is only consumed while holding the rq lock. Clear it when a task is activated, so stale state from an earlier pick or another rq is not carried into the next queued lifetime. Tested with a PE cycle reproducer in virtme-ng: buggy kernel: vng timed out without returning fixed kernel: WARN_ONCE "sched/pe: deadlock cycle detected" Signed-off-by: zhidao su (Xiaomi) --- Changes since v3: - Use a single per-task proxy_pick_seq marker instead of storing both CPU and sequence. - Clear stale proxy-walk state from activate_task() rather than from the proxy migration path only. - Drop fork-time marker initialization. - Drop READ_ONCE()/WRITE_ONCE() around marker accesses; the walk is under rq lock. - Drop the u64 wraparound guard. include/linux/sched.h | 3 +++ kernel/sched/core.c | 28 ++++++++++++++++++++++++++-- kernel/sched/sched.h | 1 + 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/include/linux/sched.h b/include/linux/sched.h index 968b18a7f4702..b1c46e21bc268 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -1256,6 +1256,9 @@ struct task_struct { * under preempt_disable(). */ struct task_struct *blocked_donor; +#ifdef CONFIG_SCHED_PROXY_EXEC + u64 proxy_pick_seq; +#endif #ifdef CONFIG_DETECT_HUNG_TASK_BLOCKER /* diff --git a/kernel/sched/core.c b/kernel/sched/core.c index 2e7cde033a319..1a9d3decd66e0 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -2221,6 +2221,9 @@ void activate_task(struct rq *rq, struct task_struct *p, int flags) if (task_on_rq_migrating(p)) flags |= ENQUEUE_MIGRATED; +#ifdef CONFIG_SCHED_PROXY_EXEC + p->proxy_pick_seq = 0; +#endif enqueue_task(rq, p, flags); WRITE_ONCE(p->on_rq, TASK_ON_RQ_QUEUED); @@ -6724,6 +6727,7 @@ static bool try_to_block_task(struct rq *rq, struct task_struct *p, } #ifdef CONFIG_SCHED_PROXY_EXEC + static inline void proxy_set_task_cpu(struct task_struct *p, int cpu) { unsigned int wake_cpu; @@ -6839,14 +6843,14 @@ static void proxy_migrate_task(struct rq *rq, struct rq_flags *rf, } /* - * Find runnable lock owner to proxy for mutex blocked donor + * Find runnable lock owner to proxy for a blocked donor * * Follow the blocked-on relation: * * ,-> task * | | blocked-on * | v - * blocked_donor | mutex + * blocked_donor | blocking primitive * | | owner * | v * `-- task @@ -6874,6 +6878,8 @@ find_proxy_task(struct rq *rq, struct task_struct *donor, struct rq_flags *rf) struct task_struct *p; int owner_cpu; + rq->proxy_pick_seq++; + /* Follow blocked_on chain. */ for (p = donor; p->is_blocked; p = owner) { /* if its PROXY_WAKING, do return migration or run if current */ @@ -6905,6 +6911,14 @@ find_proxy_task(struct rq *rq, struct task_struct *donor, struct rq_flags *rf) return NULL; } + if (p->proxy_pick_seq == rq->proxy_pick_seq) { + WARN_ONCE(1, "sched/pe: deadlock cycle detected, pid %d\n", + p->pid); + __clear_task_blocked_on(p, NULL); + goto deactivate; + } + p->proxy_pick_seq = rq->proxy_pick_seq; + if (task_current(rq, p)) curr_in_chain = true; @@ -6990,6 +7004,13 @@ find_proxy_task(struct rq *rq, struct task_struct *donor, struct rq_flags *rf) */ return proxy_resched_idle(rq); } + + if (owner->proxy_pick_seq == rq->proxy_pick_seq) { + WARN_ONCE(1, "sched/pe: deadlock cycle detected, pid %d\n", + p->pid); + __clear_task_blocked_on(p, NULL); + goto deactivate; + } /* * OK, now we're absolutely sure @owner is on this * rq, therefore holding @rq->lock is sufficient to @@ -9057,6 +9078,9 @@ void __init sched_init(void) raw_spin_lock_init(&rq->cpu_epoch_lock); rq->cpu_epoch_next = jiffies; #endif +#ifdef CONFIG_SCHED_PROXY_EXEC + rq->proxy_pick_seq = 1; +#endif zalloc_cpumask_var_node(&rq->scratch_mask, GFP_KERNEL, cpu_to_node(i)); } diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h index 26ae13c86b699..26d8d2788e0cd 100644 --- a/kernel/sched/sched.h +++ b/kernel/sched/sched.h @@ -1154,6 +1154,7 @@ struct rq { #ifdef CONFIG_SCHED_PROXY_EXEC struct task_struct __rcu *donor; /* Scheduling context */ struct task_struct __rcu *curr; /* Execution context */ + u64 proxy_pick_seq; #else union { struct task_struct __rcu *donor; /* Scheduler context */ base-commit: 04998aa54848f15332202d0bea008d2ca1ed1713 -- 2.43.0