From nobody Tue Oct 7 18:28:34 2025 Received: from mail-pg1-f202.google.com (mail-pg1-f202.google.com [209.85.215.202]) (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 E77F71E8322 for ; Mon, 7 Jul 2025 20:44:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751921059; cv=none; b=YlGzfuNs1gfa6OsbfneXsYeTxk/bS1dwhnBJxRDfB+100y2CVVp7X63KE1kjl7/1Auw1EIn7UF886ZSWw/TJ54vyM5Dtf3K4+y4UIwPFLj5bIst3AjU8DevvjjA6pNYvlXtzMDQGAuznmnq/Xf+fkPkDuCACzCrQc/FvBzy4EH8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751921059; c=relaxed/simple; bh=vtzyhv9a/esCi91sq/GucpYona1JPGP894PNVA+Aruw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Kbfi7iyf7S5Uq/X6kvpE+Daux/uzJs+BLUQDAdidZzRbBdUng7PaPIyTnq7XqNQ0Xb365My/dYqvMTNSPdWKx8l3esp5mMVoLX2fvVfjdu5+W7//UbN5hcH3VFQ7C/NuugpINj14kOmgG8NDiot7hMwXvQUS/77MPu+ECtvw6vE= 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=tdlMng6J; arc=none smtp.client-ip=209.85.215.202 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="tdlMng6J" Received: by mail-pg1-f202.google.com with SMTP id 41be03b00d2f7-b2fb347b3e6so4348319a12.1 for ; Mon, 07 Jul 2025 13:44:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1751921057; x=1752525857; 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=GtamtensSo2O4q2/DkAjxE9NIlfjTtUqP5/1W59cAKQ=; b=tdlMng6JPjQMn2friiwjg01ItE+73Gdjdjw8E6TezqrTvxdtZRDUJjEZTuM16dRlVl fkhXrxkrUDKmMYlxZ34VLqVleeQRrwHleytTTpRIfiel5yjE7ect50O3xrNu2GqqzQ5M ztrFnAuUB4/V5CGMI0IsQbEVZvnAxYfaKGF6OWvEP0UpZXWKWhks8TexkmR+hEGEqAlg MwvwLE76AfqvrNTKMtan33DHjnHt5rgA/0JSuLaeMJT3uFFh/ZLabTz+Jui/DL683ayK V7VlKabpqLSY74FukhRBJSbUHfU1rEtO5SZaLM7tglHuFGbjnyNxIGdIkY3SsIlt4cxC Jd2A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1751921057; x=1752525857; 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=GtamtensSo2O4q2/DkAjxE9NIlfjTtUqP5/1W59cAKQ=; b=ALmr7gfsdbw4dnpTb2tPoN4zuz4/jRiSGg+vRg3lotjEg2Uu3/LraEakpByai4Y04g qPRpXztx3OpD1M4bT3kLhFg7DGuc81JdtZjbfK7Ge3ZCyi7l6wtQO2MQZGNSOUmgBliO cXy8n+QHpsj25xcAtk25MMHz2JpypUq9qYharwW3OOa2cn0jsQRYWVPCsVY8/NwVh4iz 5oxPwglAXAbwcNKIVA+rI34DO3WV7+5dZCPcOgh+QKcKdGb/mEIDG04bQTD5mq8sS4p8 qzsoarPfHd/CD3ALsARKt6JAuQy5TWtebiCM82DnnXEN1x1fOmPp+VYbObFlpzDSFScp EIfw== X-Gm-Message-State: AOJu0YzTBbuZGuHkU8ayKRQ1aAjTlEJdX3LKzyEvbPbfmw2qguiPB/HL 1dHGqmyAF9h/KJlg3DNwZOkObKFiwB7n2tgdoHl6f0osd/3yu5j7Shjks4eIpIjoa4J8JhDcpWp nygliLbgDstd5uiUYD82v0J1s9B7bEzoz8VWl5mzKjlua/fbd78dZFfvKftq/AwFAscaNL5Fq8C gOME3h6jX4/un85cTW9CNbbGoalEW3t+jCwOhT19LWjpdYyfSb X-Google-Smtp-Source: AGHT+IG4FN6+gTahsP4YSBFaxpNQCv9V8b+i4y0+uAiReg1PMGVLD93sWSc6lTngqfi/r0t5ViTPJL40vo9c X-Received: from pgbda5.prod.google.com ([2002:a05:6a02:2385:b0:b2e:beba:356]) (user=jstultz job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a21:b98:b0:215:fb74:2dc2 with SMTP id adf61e73a8af0-22b4214ddabmr855529637.11.1751921057122; Mon, 07 Jul 2025 13:44:17 -0700 (PDT) Date: Mon, 7 Jul 2025 20:43:48 +0000 In-Reply-To: <20250707204409.1028494-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: <20250707204409.1028494-1-jstultz@google.com> X-Mailer: git-send-email 2.50.0.727.gbf7dc18ff4-goog Message-ID: <20250707204409.1028494-2-jstultz@google.com> Subject: [RESEND][PATCH v18 1/8] sched: Add CONFIG_SCHED_PROXY_EXEC & boot argument to enable/disable From: John Stultz To: LKML Cc: John Stultz , K Prateek Nayak , Joel Fernandes , Qais Yousef , Ingo Molnar , Peter Zijlstra , Juri Lelli , Vincent Guittot , Dietmar Eggemann , Valentin Schneider , Steven Rostedt , Ben Segall , Zimuzo Ezeozue , Mel Gorman , Will Deacon , Waiman Long , Boqun Feng , "Paul E. McKenney" , Metin Kaya , Xuewen Yan , Thomas Gleixner , Daniel Lezcano , Suleiman Souhlal , kuyo chang , hupu , kernel-team@android.com Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add a CONFIG_SCHED_PROXY_EXEC option, along with a boot argument sched_proxy_exec=3D that can be used to disable the feature at boot time if CONFIG_SCHED_PROXY_EXEC was enabled. Also uses this option to allow the rq->donor to be different from rq->curr. Tested-by: K Prateek Nayak Signed-off-by: John Stultz Tested-by tags. --- v7: * Switch to CONFIG_SCHED_PROXY_EXEC/sched_proxy_exec=3D as suggested by Metin Kaya. * Switch boot arg from =3Ddisable/enable to use kstrtobool(), which supports =3Dyes|no|1|0|true|false|on|off, as also suggested by Metin Kaya, and print a message when a boot argument is used. v8: * Move CONFIG_SCHED_PROXY_EXEC under Scheduler Features as Suggested by Metin * Minor rework reordering with split sched contexts patch v12: * Rework for selected -> donor renaming v14: * Depend on !PREEMPT_RT to avoid build issues for now v15: * Depend on EXPERT while patch series upstreaming is in progress. v16: * Allow "sched_proxy_exec" without "=3Dtrue" to enable proxy-execution at boot time, in addition to the "sched_proxy_exec=3Dtrue" or "sched_proxy_exec=3Dfalse" options as suggested by Steven * Drop the "default n" in Kconfig as suggested by Steven * Add !SCHED_CLASS_EXT dependency until I can investigate if sched_ext can understand split contexts, as suggested by Peter v17: * Expanded the commit message a bit to clarify that the option allows the split contexts (rq->donor and rq->curr) to be different tasks, from feedback from Juri Lelli 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: Mel Gorman 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 --- .../admin-guide/kernel-parameters.txt | 5 ++++ include/linux/sched.h | 13 +++++++++ init/Kconfig | 12 ++++++++ kernel/sched/core.c | 29 +++++++++++++++++++ kernel/sched/sched.h | 12 ++++++++ 5 files changed, 71 insertions(+) diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentatio= n/admin-guide/kernel-parameters.txt index f1f2c0874da9d..5b89464ca570a 100644 --- a/Documentation/admin-guide/kernel-parameters.txt +++ b/Documentation/admin-guide/kernel-parameters.txt @@ -6387,6 +6387,11 @@ sa1100ir [NET] See drivers/net/irda/sa1100_ir.c. =20 + sched_proxy_exec=3D [KNL] + Enables or disables "proxy execution" style + solution to mutex-based priority inversion. + Format: + sched_verbose [KNL,EARLY] Enables verbose scheduler debug messages. =20 schedstats=3D [KNL,X86] Enable or disable scheduled statistics. diff --git a/include/linux/sched.h b/include/linux/sched.h index 4f78a64beb52c..ba2a02ead8c7e 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -1664,6 +1664,19 @@ struct task_struct { randomized_struct_fields_end } __attribute__ ((aligned (64))); =20 +#ifdef CONFIG_SCHED_PROXY_EXEC +DECLARE_STATIC_KEY_TRUE(__sched_proxy_exec); +static inline bool sched_proxy_exec(void) +{ + return static_branch_likely(&__sched_proxy_exec); +} +#else +static inline bool sched_proxy_exec(void) +{ + return false; +} +#endif + #define TASK_REPORT_IDLE (TASK_REPORT + 1) #define TASK_REPORT_MAX (TASK_REPORT_IDLE << 1) =20 diff --git a/init/Kconfig b/init/Kconfig index af4c2f0854554..6844b25303913 100644 --- a/init/Kconfig +++ b/init/Kconfig @@ -875,6 +875,18 @@ config UCLAMP_BUCKETS_COUNT =20 If in doubt, use the default value. =20 +config SCHED_PROXY_EXEC + bool "Proxy Execution" + # Avoid some build failures w/ PREEMPT_RT until it can be fixed + depends on !PREEMPT_RT + # Need to investigate how to inform sched_ext of split contexts + depends on !SCHED_CLASS_EXT + # Not particularly useful until we get to multi-rq proxying + depends on EXPERT + help + This option enables proxy execution, a mechanism for mutex-owning + tasks to inherit the scheduling context of higher priority waiters. + endmenu =20 # diff --git a/kernel/sched/core.c b/kernel/sched/core.c index 8988d38d46a38..853157b27f384 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -119,6 +119,35 @@ EXPORT_TRACEPOINT_SYMBOL_GPL(sched_compute_energy_tp); =20 DEFINE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues); =20 +#ifdef CONFIG_SCHED_PROXY_EXEC +DEFINE_STATIC_KEY_TRUE(__sched_proxy_exec); +static int __init setup_proxy_exec(char *str) +{ + bool proxy_enable =3D true; + + if (*str && kstrtobool(str + 1, &proxy_enable)) { + pr_warn("Unable to parse sched_proxy_exec=3D\n"); + return 0; + } + + if (proxy_enable) { + pr_info("sched_proxy_exec enabled via boot arg\n"); + static_branch_enable(&__sched_proxy_exec); + } else { + pr_info("sched_proxy_exec disabled via boot arg\n"); + static_branch_disable(&__sched_proxy_exec); + } + return 1; +} +#else +static int __init setup_proxy_exec(char *str) +{ + pr_warn("CONFIG_SCHED_PROXY_EXEC=3Dn, so it cannot be enabled or disabled= at boot time\n"); + return 0; +} +#endif +__setup("sched_proxy_exec", setup_proxy_exec); + /* * Debugging: various feature bits * diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h index 475bb5998295e..6b61e0c7f6e78 100644 --- a/kernel/sched/sched.h +++ b/kernel/sched/sched.h @@ -1151,10 +1151,15 @@ struct rq { */ unsigned int nr_uninterruptible; =20 +#ifdef CONFIG_SCHED_PROXY_EXEC + struct task_struct __rcu *donor; /* Scheduling context */ + struct task_struct __rcu *curr; /* Execution context */ +#else union { struct task_struct __rcu *donor; /* Scheduler context */ struct task_struct __rcu *curr; /* Execution context */ }; +#endif struct sched_dl_entity *dl_server; struct task_struct *idle; struct task_struct *stop; @@ -1349,10 +1354,17 @@ DECLARE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues= ); #define cpu_curr(cpu) (cpu_rq(cpu)->curr) #define raw_rq() raw_cpu_ptr(&runqueues) =20 +#ifdef CONFIG_SCHED_PROXY_EXEC +static inline void rq_set_donor(struct rq *rq, struct task_struct *t) +{ + rcu_assign_pointer(rq->donor, t); +} +#else static inline void rq_set_donor(struct rq *rq, struct task_struct *t) { /* Do nothing */ } +#endif =20 #ifdef CONFIG_SCHED_CORE static inline struct cpumask *sched_group_span(struct sched_group *sg); --=20 2.50.0.727.gbf7dc18ff4-goog From nobody Tue Oct 7 18:28:34 2025 Received: from mail-pg1-f201.google.com (mail-pg1-f201.google.com [209.85.215.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 70064221F01 for ; Mon, 7 Jul 2025 20:44:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751921061; cv=none; b=ZfVt5ilg/V6lFbsnJraU4/y6zmGJTyCH/UPFsr3pOAEQ1IdPe4Mu/rl072oMtoVBjzl4B/+bPor/8o/IyZplfO6dS5JeIuDM2z+pOEUG69EXYJ7CfUW8yNvV8MO/DXRx1VnHT3GZHlWNQYXdhCBZQexaBURhrzRtMrjOdeCoDdw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751921061; c=relaxed/simple; bh=lCYOFYCQ5X/O9LF0+RaTNfvNehIilJ5msdeFK7VN4CI=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=pGCMlA9GeGojSNaDEEpj3OGyTU1jMzRezdyyBULVTQ75y8W4IzsjVx26gd3de0HnzQpxvsk4OH4FxaYKl6gUL8vlyknVMeBIB+5UuDhggcghUK9CuXs3KPsfSkRtLnzeMiViU5X3d0uuH6Mcvo0LHemebE1t63eZZvDeknd/AAw= 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=mvnsGMyi; arc=none smtp.client-ip=209.85.215.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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="mvnsGMyi" Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-b2c37558eccso2975756a12.1 for ; Mon, 07 Jul 2025 13:44:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1751921059; x=1752525859; 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=Q+W26I4UoVcgtTG2t49WkryD3K6H1+lyiZhKPVWcdyM=; b=mvnsGMyiFSaF5MyGcxHOxvCsI4mQEGFrufsV/0kAbn9l9/xWuxu3CIiDg6z8SaL1GK MxV8VpKGf++un1O/MjkxVXGMTyubEqtM69uPsPyFG2TgpfRa6FKha0tEmqM298s9R1Zl vWY17FTyPQHv/oqZVd73ftzwDzbhvUSmMw2Aj+knwP8eG1DR25whDdVjyOF6D0dzBZfa JP/En8Lc/mE+FenoOnycDGvI7LeVukQ9apjJufr+owlA2pP+Oh6U5Kp7kasCFuuL97F8 EoDI+dBbeZhsR3sxwrjscRaO1UJ3xX5pkIs5XLsW2HHap5lMqJgKbMdsaezd3un0I/k9 /L+Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1751921059; x=1752525859; 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=Q+W26I4UoVcgtTG2t49WkryD3K6H1+lyiZhKPVWcdyM=; b=hqp8FX7jaYotGfA3XjcQgOetaeKcHE+9wQRtU0l1mQSCyyNBPgD5YpL2BsoOv215Jc VWHvscFmeHaeqzC5K+qZ1ufBGAdXvg4p+fAWiVfl8KIJSfWDPdQo8kKHpxDgS4NmFGQf yHimWIdyqxpRFE5msMcUs4li89Ga972cCLcPEgd83yMAmfOqDrvJyo4at8yMHkF6J1Uz qnHitxd/y6wszNRzn+8HrFa1zwg+vy+Czz0MIS1msn9gDxDy6koQ4ybiZnToI7/DaTgR BoGBY5BCK5GSGOxXbFtL8cf6RghRgc94ssrRaYyVV6iKdNL2Q64fRjbFYbc6fQrA7gn7 4GGQ== X-Gm-Message-State: AOJu0YxT1XbNAjiTa1ezUZBbzk0/tzBdOnR3/eK7+sxvtHOLENyZUZjY Beg0eqCOo4uxnHt+j/P2JIjA75W0cJdq6T68sEWCnbbrmN1W7za3t7tEHMNTOTlaiHsYyUWTd8N m6F4Hv5rSgKeG4sdcZ9l/xFkwKbHJrC8NcdLY8nzUd2YKjV7H6aEBS9Egfsloq6G18noonZbqiy Uy2JrQpeZaQZ6XfyG7XYl9/Gll8MuancHThdtyCv/Ct+jPZfX/ X-Google-Smtp-Source: AGHT+IEh2nqsVZ8R4lb/7cnmZFdEFkJb3LTSruJ4cuFRbvE3mEQxIjclK8OFefnprMHPKh6PWBpRIWFt70hi X-Received: from pgbdn3.prod.google.com ([2002:a05:6a02:e03:b0:b31:e758:d80]) (user=jstultz job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a21:6da0:b0:226:d285:302d with SMTP id adf61e73a8af0-22b4252ae70mr850302637.4.1751921058337; Mon, 07 Jul 2025 13:44:18 -0700 (PDT) Date: Mon, 7 Jul 2025 20:43:49 +0000 In-Reply-To: <20250707204409.1028494-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: <20250707204409.1028494-1-jstultz@google.com> X-Mailer: git-send-email 2.50.0.727.gbf7dc18ff4-goog Message-ID: <20250707204409.1028494-3-jstultz@google.com> Subject: [RESEND][PATCH v18 2/8] locking/mutex: Rework task_struct::blocked_on From: John Stultz To: LKML Cc: Peter Zijlstra , K Prateek Nayak , Juri Lelli , "Connor O'Brien" , John Stultz , Joel Fernandes , Qais Yousef , Ingo Molnar , Vincent Guittot , Dietmar Eggemann , Valentin Schneider , Steven Rostedt , Ben Segall , Zimuzo Ezeozue , Mel Gorman , Will Deacon , Waiman Long , Boqun Feng , "Paul E. McKenney" , Metin Kaya , Xuewen Yan , Thomas Gleixner , Daniel Lezcano , Suleiman Souhlal , kuyo chang , hupu , kernel-team@android.com Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Peter Zijlstra Track the blocked-on relation for mutexes, to allow following this relation at schedule time. task | blocked-on v mutex | owner v task This all will be used for tracking blocked-task/mutex chains with the prox-execution patch in a similar fashion to how priority inheritance is done with rt_mutexes. For serialization, blocked-on is only set by the task itself (current). And both when setting or clearing (potentially by others), is done while holding the mutex::wait_lock. Tested-by: K Prateek Nayak 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 Tested-by tags. --- 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 circular 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 v7: * Minor commit message change suggested by Metin Kaya * Fix WARN_ON conditionals in unlock path (as blocked_on might already be cleared), found while looking at issue Metin Kaya raised. * Minor tweaks to be consistent in what we do under the blocked_on lock, also tweaked variable name to avoid confusion with label, and comment typos, as suggested by Metin Kaya * Minor tweak for CONFIG_SCHED_PROXY_EXEC name change * Moved unused block of code to later in the series, as suggested by Metin Kaya * Switch to a tri-state to be able to distinguish from waking and runnable so we can later safely do return migration from ttwu * Folded together with related blocked_on changes v8: * Fix issue leaving task BO_BLOCKED when calling into optimistic spinning path. * Include helper to better handle BO_BLOCKED->BO_WAKING transitions v9: * Typo fixup pointed out by Metin * Cleanup BO_WAKING->BO_RUNNABLE transitions for the !proxy case * Many cleanups and simplifications suggested by Metin v11: * Whitespace fixup pointed out by Metin v13: * Refactor set_blocked_on helpers clean things up a bit v14: * Small build fixup with PREEMPT_RT v15: * Improve consistency of names for functions that assume blocked_lock is held, as suggested by Peter * Use guard instead of separate spinlock/unlock calls, also suggested by Peter * Drop blocked_on_state tri-state for now, as its not needed until later in the series, when we get to proxy-migration and return- migration. v16: * Clear blocked on before optimistic spinning 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: Mel Gorman 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 --- include/linux/sched.h | 5 +---- kernel/fork.c | 3 +-- kernel/locking/mutex-debug.c | 9 +++++---- kernel/locking/mutex.c | 22 ++++++++++++++++++++++ kernel/locking/ww_mutex.h | 18 ++++++++++++++++-- 5 files changed, 45 insertions(+), 12 deletions(-) diff --git a/include/linux/sched.h b/include/linux/sched.h index ba2a02ead8c7e..aa7966141a090 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -1238,10 +1238,7 @@ struct task_struct { struct rt_mutex_waiter *pi_blocked_on; #endif =20 -#ifdef CONFIG_DEBUG_MUTEXES - /* Mutex deadlock detection: */ - struct mutex_waiter *blocked_on; -#endif + struct mutex *blocked_on; /* lock we're blocked on */ =20 #ifdef CONFIG_DETECT_HUNG_TASK_BLOCKER /* diff --git a/kernel/fork.c b/kernel/fork.c index 1ee8eb11f38ba..5f87f05aff4a0 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -2123,9 +2123,8 @@ __latent_entropy struct task_struct *copy_process( lockdep_init_task(p); #endif =20 -#ifdef CONFIG_DEBUG_MUTEXES p->blocked_on =3D NULL; /* not blocked yet */ -#endif + #ifdef CONFIG_BCACHE p->sequential_io =3D 0; p->sequential_io_avg =3D 0; diff --git a/kernel/locking/mutex-debug.c b/kernel/locking/mutex-debug.c index 6e6f6071cfa27..758b7a6792b0c 100644 --- a/kernel/locking/mutex-debug.c +++ b/kernel/locking/mutex-debug.c @@ -53,17 +53,18 @@ void debug_mutex_add_waiter(struct mutex *lock, struct = mutex_waiter *waiter, { lockdep_assert_held(&lock->wait_lock); =20 - /* Mark the current thread as blocked on the lock: */ - task->blocked_on =3D waiter; + /* Current thread can't be already blocked (since it's executing!) */ + DEBUG_LOCKS_WARN_ON(task->blocked_on); } =20 void debug_mutex_remove_waiter(struct mutex *lock, struct mutex_waiter *wa= iter, struct task_struct *task) { + struct mutex *blocked_on =3D READ_ONCE(task->blocked_on); + DEBUG_LOCKS_WARN_ON(list_empty(&waiter->list)); DEBUG_LOCKS_WARN_ON(waiter->task !=3D task); - DEBUG_LOCKS_WARN_ON(task->blocked_on !=3D waiter); - task->blocked_on =3D NULL; + DEBUG_LOCKS_WARN_ON(blocked_on && blocked_on !=3D lock); =20 INIT_LIST_HEAD(&waiter->list); waiter->task =3D NULL; diff --git a/kernel/locking/mutex.c b/kernel/locking/mutex.c index a39ecccbd106a..e2f59863a866e 100644 --- a/kernel/locking/mutex.c +++ b/kernel/locking/mutex.c @@ -644,6 +644,8 @@ __mutex_lock_common(struct mutex *lock, unsigned int st= ate, unsigned int subclas goto err_early_kill; } =20 + WARN_ON(current->blocked_on); + current->blocked_on =3D lock; set_current_state(state); trace_contention_begin(lock, LCB_F_MUTEX); for (;;) { @@ -680,6 +682,12 @@ __mutex_lock_common(struct mutex *lock, unsigned int s= tate, unsigned int subclas =20 first =3D __mutex_waiter_is_first(lock, &waiter); =20 + /* + * As we likely have been woken up by task + * that has cleared our blocked_on state, re-set + * it to the lock we are trying to aquire. + */ + current->blocked_on =3D lock; set_current_state(state); /* * Here we order against unlock; we must either see it change @@ -691,8 +699,11 @@ __mutex_lock_common(struct mutex *lock, unsigned int s= tate, unsigned int subclas =20 if (first) { trace_contention_begin(lock, LCB_F_MUTEX | LCB_F_SPIN); + /* clear blocked_on as mutex_optimistic_spin may schedule() */ + current->blocked_on =3D NULL; if (mutex_optimistic_spin(lock, ww_ctx, &waiter)) break; + current->blocked_on =3D lock; trace_contention_begin(lock, LCB_F_MUTEX); } =20 @@ -700,6 +711,7 @@ __mutex_lock_common(struct mutex *lock, unsigned int st= ate, unsigned int subclas } raw_spin_lock_irqsave(&lock->wait_lock, flags); acquired: + current->blocked_on =3D NULL; __set_current_state(TASK_RUNNING); =20 if (ww_ctx) { @@ -729,9 +741,11 @@ __mutex_lock_common(struct mutex *lock, unsigned int s= tate, unsigned int subclas return 0; =20 err: + current->blocked_on =3D 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_wake(&lock->wait_lock, flags, &wake_q); debug_mutex_free_waiter(&waiter); @@ -942,6 +956,14 @@ static noinline void __sched __mutex_unlock_slowpath(s= truct mutex *lock, unsigne next =3D waiter->task; =20 debug_mutex_wake_waiter(lock, waiter); + /* + * Unlock wakeups can be happening in parallel + * (when optimistic spinners steal and release + * the lock), so blocked_on may already be + * cleared here. + */ + WARN_ON(next->blocked_on && next->blocked_on !=3D lock); + next->blocked_on =3D NULL; wake_q_add(&wake_q, next); } =20 diff --git a/kernel/locking/ww_mutex.h b/kernel/locking/ww_mutex.h index 37f025a096c9d..00db40946328e 100644 --- a/kernel/locking/ww_mutex.h +++ b/kernel/locking/ww_mutex.h @@ -284,6 +284,14 @@ __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 && + waiter->task->blocked_on !=3D lock); + waiter->task->blocked_on =3D NULL; wake_q_add(wake_q, waiter->task); } =20 @@ -331,9 +339,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 !=3D current) + if (owner !=3D 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 =3D NULL; wake_q_add(wake_q, owner); - + } return true; } =20 --=20 2.50.0.727.gbf7dc18ff4-goog From nobody Tue Oct 7 18:28:34 2025 Received: from mail-pg1-f201.google.com (mail-pg1-f201.google.com [209.85.215.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 892C322422A for ; Mon, 7 Jul 2025 20:44:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751921062; cv=none; b=q6+s/Ij1Z8FLQhI6Er3EbtnQ/NhjQNMrlJio26cx9KvHzRYvj6oUp8gKNR4QeyqD4Yfqd01eNR7BS7dVQBHHbs1K0SRHtzLf3YTUoYnXVljQi9T5cUytavWc52jPDc0S9QvdkmksmFLsVhwu9fqRblM4nclqr7XtnKv0Kg1ZAqo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751921062; c=relaxed/simple; bh=By0nbPfTOcXYPbjtB4bCxl3LcCaPQcULGVBaxYXufTg=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=QpzMz9LIicx04XZE74kWA5+NUvxOTYEQvq4C6DHlbPwT78iAB5cdw5r9ZvL2m8waCwYOR1egWhWzlCuuAcacxSj+4Bpeg9XeHU/GkPcGqG7aKSsL329NLujbWwTWzJZG8O3eSN94gMM+Tt8AQDhwc+XpQ0u+Qb6u4WB+yP9dqbU= 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=Nes1nGnC; arc=none smtp.client-ip=209.85.215.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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Nes1nGnC" Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-b34ca003f1cso2398458a12.2 for ; Mon, 07 Jul 2025 13:44:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1751921060; x=1752525860; 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=jtBQet3ao/se13H1IhwJdvBga7baKePSjfnpu6TJKGA=; b=Nes1nGnCF29K/OZqh0XbPwnuro5QYk+fCL7A0CwJ9EPywoEdTv3O7rK8zfwcEOIGA4 V55bzU9CxXJbAsJ9vcT/0l6MpJwXRXsp2n/ts6KiRZOtuXg0RFZPlyf8VqIMCvS3Sbc5 cd846JMjwsr+CxtWnP8DfOB06IrCfdiJk9L9RYIk8HSYzK+vu6VQzCXakW4+ztikiiiU VWjH3xj4NI+VovB0/rziZnf9O7rpAoweRP9UyF4xlee5e/QdJI2DIT6+bF/TrbuPLyp3 TnGXRSWMtn/tZ9x5zyAiavx9vy2Ek2yix8dkTraPAyUXwnYcY0QrqdOD1vzm0VGgXTBe /Hsg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1751921060; x=1752525860; 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=jtBQet3ao/se13H1IhwJdvBga7baKePSjfnpu6TJKGA=; b=Fr8EJ7yjAmlJOgpTLK3SzejSGpeEfHWOoEglLXSJGRjeoQk1oqM3/XOYnMDzluTSK5 SHDK/BGtp1bAm0TTD99G5apKdCsFPJRnfyCCCxjZMVemTLzTictBQs4aKVsl1/UoYM49 /jnOi2SRm4FygT2RfymwttMkSiCWlOso4RgXMaImGOD2cGORZrA1t6gEBKwpz0fBA1dA RLzaVxa132VFqnhmSm+jUy0lk5ViLaBfx/+ortLzI+JHaSox2o+CgMEBhjqAIlbWDhBO 7cQ8JpYNICexsvSTu/t1wzVPxUA2+xoG5ivrpnkixVSFC+SyAHs0p0JXxXvGPtE1DSpQ rLog== X-Gm-Message-State: AOJu0Yy+hzNaVubi+tDqf/zGz0aJlxNl4ZVxC/4ZqQpDBH5W0oJ5hUTX tXLDXhLiHZHXwN/UOncahD+Xf3KwYbb0DM4SWewhaZ/hBy9/D3n7D0oDakJtQYSniF1l+sIHMVs g8cA67glbsA7m/v4W3PRyYvnGPS15ZgSPM8rGykZdCzatsS7VxBzhZYc56de6yWNYHabnjHOu8E usf0E2I22H0lc8wjEMndZlNCR+Jgr9vGpVhc6KEZ2H+njmxf24 X-Google-Smtp-Source: AGHT+IE6Stgyv8V51A6xi7yNUlB8wlRvRck1wD5Syl1/svcfov1FCfa8c3Jfm7KV3dcThVNirb87xyltQgy1 X-Received: from pjbee15.prod.google.com ([2002:a17:90a:fc4f:b0:313:2d44:397b]) (user=jstultz job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:2ed0:b0:311:f30b:c21 with SMTP id 98e67ed59e1d1-31aadd9fd46mr16216617a91.26.1751921059785; Mon, 07 Jul 2025 13:44:19 -0700 (PDT) Date: Mon, 7 Jul 2025 20:43:50 +0000 In-Reply-To: <20250707204409.1028494-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: <20250707204409.1028494-1-jstultz@google.com> X-Mailer: git-send-email 2.50.0.727.gbf7dc18ff4-goog Message-ID: <20250707204409.1028494-4-jstultz@google.com> Subject: [RESEND][PATCH v18 3/8] locking/mutex: Add p->blocked_on wrappers for correctness checks From: John Stultz To: LKML Cc: Valentin Schneider , K Prateek Nayak , "Connor O'Brien" , John Stultz , Joel Fernandes , Qais Yousef , Ingo Molnar , Peter Zijlstra , Juri Lelli , Vincent Guittot , Dietmar Eggemann , Valentin Schneider , Steven Rostedt , Ben Segall , Zimuzo Ezeozue , Mel Gorman , Will Deacon , Waiman Long , Boqun Feng , "Paul E. McKenney" , Metin Kaya , Xuewen Yan , Thomas Gleixner , Daniel Lezcano , Suleiman Souhlal , kuyo chang , hupu , kernel-team@android.com Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Valentin Schneider This lets us assert mutex::wait_lock is held whenever we access p->blocked_on, as well as warn us for unexpected state changes. Tested-by: K Prateek Nayak Signed-off-by: Valentin Schneider [fix conflicts, call in more places] Signed-off-by: Connor O'Brien [jstultz: tweaked commit subject, reworked a good bit] Signed-off-by: John Stultz Tested-by tags. --- v2: * Added get_task_blocked_on() accessor v4: * Address READ_ONCE usage that was dropped in v2 * Reordered to be a later add on to the main patch series as Peter was unhappy with similar wrappers in other patches. v5: * Added some extra correctness checking in wrappers v7: * Tweaks to reorder this change in the patch series * Minor cleanup to set_task_blocked_on() suggested by Metin Kaya v15: * Split out into its own patch again. * Further improve assumption checks in helpers. v16: * Fix optimistic spin case that can call schedule() v17: * Fix typos caught by Metin Kaya * Add lockdep_assert_held_once and drop the READ_ONCE in __get_task_blocked_on(), as suggested by Juri Lelli 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: Mel Gorman 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 --- include/linux/sched.h | 54 ++++++++++++++++++++++++++++++++++-- kernel/locking/mutex-debug.c | 4 +-- kernel/locking/mutex.c | 32 ++++++++++----------- kernel/locking/ww_mutex.h | 6 ++-- 4 files changed, 70 insertions(+), 26 deletions(-) diff --git a/include/linux/sched.h b/include/linux/sched.h index aa7966141a090..1d7f625adbb5e 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -34,6 +34,7 @@ #include #include #include +#include #include #include #include @@ -2177,6 +2178,57 @@ extern int __cond_resched_rwlock_write(rwlock_t *loc= k); __cond_resched_rwlock_write(lock); \ }) =20 +static inline void __set_task_blocked_on(struct task_struct *p, struct mut= ex *m) +{ + WARN_ON_ONCE(!m); + /* The task should only be setting itself as blocked */ + WARN_ON_ONCE(p !=3D current); + /* Currently we serialize blocked_on under the mutex::wait_lock */ + lockdep_assert_held_once(&m->wait_lock); + /* + * Check ensure we don't overwrite existing mutex value + * with a different mutex. Note, setting it to the same + * lock repeatedly is ok. + */ + WARN_ON_ONCE(p->blocked_on && p->blocked_on !=3D m); + p->blocked_on =3D m; +} + +static inline void set_task_blocked_on(struct task_struct *p, struct mutex= *m) +{ + guard(raw_spinlock_irqsave)(&m->wait_lock); + __set_task_blocked_on(p, m); +} + +static inline void __clear_task_blocked_on(struct task_struct *p, struct m= utex *m) +{ + WARN_ON_ONCE(!m); + /* Currently we serialize blocked_on under the mutex::wait_lock */ + lockdep_assert_held_once(&m->wait_lock); + /* + * There may be cases where we re-clear already cleared + * blocked_on relationships, but make sure we are not + * clearing the relationship with a different lock. + */ + WARN_ON_ONCE(m && p->blocked_on && p->blocked_on !=3D m); + p->blocked_on =3D NULL; +} + +static inline void clear_task_blocked_on(struct task_struct *p, struct mut= ex *m) +{ + guard(raw_spinlock_irqsave)(&m->wait_lock); + __clear_task_blocked_on(p, m); +} + +static inline struct mutex *__get_task_blocked_on(struct task_struct *p) +{ + struct mutex *m =3D p->blocked_on; + + if (m) + lockdep_assert_held_once(&m->wait_lock); + return m; +} + static __always_inline bool need_resched(void) { return unlikely(tif_need_resched()); @@ -2216,8 +2268,6 @@ extern bool sched_task_on_rq(struct task_struct *p); extern unsigned long get_wchan(struct task_struct *p); extern struct task_struct *cpu_curr_snapshot(int cpu); =20 -#include - /* * In order to reduce various lock holder preemption latencies provide an * interface to see if a vCPU is currently running or not. diff --git a/kernel/locking/mutex-debug.c b/kernel/locking/mutex-debug.c index 758b7a6792b0c..949103fd8e9b5 100644 --- a/kernel/locking/mutex-debug.c +++ b/kernel/locking/mutex-debug.c @@ -54,13 +54,13 @@ void debug_mutex_add_waiter(struct mutex *lock, struct = mutex_waiter *waiter, lockdep_assert_held(&lock->wait_lock); =20 /* Current thread can't be already blocked (since it's executing!) */ - DEBUG_LOCKS_WARN_ON(task->blocked_on); + DEBUG_LOCKS_WARN_ON(__get_task_blocked_on(task)); } =20 void debug_mutex_remove_waiter(struct mutex *lock, struct mutex_waiter *wa= iter, struct task_struct *task) { - struct mutex *blocked_on =3D READ_ONCE(task->blocked_on); + struct mutex *blocked_on =3D __get_task_blocked_on(task); =20 DEBUG_LOCKS_WARN_ON(list_empty(&waiter->list)); DEBUG_LOCKS_WARN_ON(waiter->task !=3D task); diff --git a/kernel/locking/mutex.c b/kernel/locking/mutex.c index e2f59863a866e..80d778fedd605 100644 --- a/kernel/locking/mutex.c +++ b/kernel/locking/mutex.c @@ -644,8 +644,7 @@ __mutex_lock_common(struct mutex *lock, unsigned int st= ate, unsigned int subclas goto err_early_kill; } =20 - WARN_ON(current->blocked_on); - current->blocked_on =3D lock; + __set_task_blocked_on(current, lock); set_current_state(state); trace_contention_begin(lock, LCB_F_MUTEX); for (;;) { @@ -685,9 +684,9 @@ __mutex_lock_common(struct mutex *lock, unsigned int st= ate, unsigned int subclas /* * As we likely have been woken up by task * that has cleared our blocked_on state, re-set - * it to the lock we are trying to aquire. + * it to the lock we are trying to acquire. */ - current->blocked_on =3D lock; + set_task_blocked_on(current, lock); set_current_state(state); /* * Here we order against unlock; we must either see it change @@ -699,11 +698,15 @@ __mutex_lock_common(struct mutex *lock, unsigned int = state, unsigned int subclas =20 if (first) { trace_contention_begin(lock, LCB_F_MUTEX | LCB_F_SPIN); - /* clear blocked_on as mutex_optimistic_spin may schedule() */ - current->blocked_on =3D NULL; + /* + * mutex_optimistic_spin() can call schedule(), so + * clear blocked on so we don't become unselectable + * to run. + */ + clear_task_blocked_on(current, lock); if (mutex_optimistic_spin(lock, ww_ctx, &waiter)) break; - current->blocked_on =3D lock; + set_task_blocked_on(current, lock); trace_contention_begin(lock, LCB_F_MUTEX); } =20 @@ -711,7 +714,7 @@ __mutex_lock_common(struct mutex *lock, unsigned int st= ate, unsigned int subclas } raw_spin_lock_irqsave(&lock->wait_lock, flags); acquired: - current->blocked_on =3D NULL; + __clear_task_blocked_on(current, lock); __set_current_state(TASK_RUNNING); =20 if (ww_ctx) { @@ -741,11 +744,11 @@ __mutex_lock_common(struct mutex *lock, unsigned int = state, unsigned int subclas return 0; =20 err: - current->blocked_on =3D NULL; + __clear_task_blocked_on(current, lock); __set_current_state(TASK_RUNNING); __mutex_remove_waiter(lock, &waiter); err_early_kill: - WARN_ON(current->blocked_on); + WARN_ON(__get_task_blocked_on(current)); trace_contention_end(lock, ret); raw_spin_unlock_irqrestore_wake(&lock->wait_lock, flags, &wake_q); debug_mutex_free_waiter(&waiter); @@ -956,14 +959,7 @@ static noinline void __sched __mutex_unlock_slowpath(s= truct mutex *lock, unsigne next =3D waiter->task; =20 debug_mutex_wake_waiter(lock, waiter); - /* - * Unlock wakeups can be happening in parallel - * (when optimistic spinners steal and release - * the lock), so blocked_on may already be - * cleared here. - */ - WARN_ON(next->blocked_on && next->blocked_on !=3D lock); - next->blocked_on =3D NULL; + __clear_task_blocked_on(next, lock); wake_q_add(&wake_q, next); } =20 diff --git a/kernel/locking/ww_mutex.h b/kernel/locking/ww_mutex.h index 00db40946328e..086fd5487ca77 100644 --- a/kernel/locking/ww_mutex.h +++ b/kernel/locking/ww_mutex.h @@ -289,9 +289,7 @@ __ww_mutex_die(struct MUTEX *lock, struct MUTEX_WAITER = *waiter, * blocked_on pointer. Otherwise we can see circular * blocked_on relationships that can't resolve. */ - WARN_ON(waiter->task->blocked_on && - waiter->task->blocked_on !=3D lock); - waiter->task->blocked_on =3D NULL; + __clear_task_blocked_on(waiter->task, lock); wake_q_add(wake_q, waiter->task); } =20 @@ -345,7 +343,7 @@ static bool __ww_mutex_wound(struct MUTEX *lock, * blocked_on pointer. Otherwise we can see circular * blocked_on relationships that can't resolve. */ - owner->blocked_on =3D NULL; + __clear_task_blocked_on(owner, lock); wake_q_add(wake_q, owner); } return true; --=20 2.50.0.727.gbf7dc18ff4-goog From nobody Tue Oct 7 18:28:34 2025 Received: from mail-pf1-f202.google.com (mail-pf1-f202.google.com [209.85.210.202]) (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 3CE6322F75B for ; Mon, 7 Jul 2025 20:44:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751921063; cv=none; b=fGshN9JshYVGDV+x6ITnV/Es2v+hhbLyeuoa7+mBYz+06iTXCIzBFMsiRrgaZI2YyuEqSoEo2gKnhvc2f+vN2qLf7WwQuxRUHZxj7lozkRkNS2cErOJtTVmkjgftG7ifvpr/aPOzd00feiW8Ojy5S2SdlIaQOaEQLc3Vi5O/PiA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751921063; c=relaxed/simple; bh=E9BRbJXMe0/nYQ1bgCddiiXCeZLlpdEjEZgz4SOKuY4=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=mGtbyvFz6lIy7ARM/YKQX8lXMm8ckoiakcI1ygrmATm+jG40e0p3XhOEhpssEW36dlo/uneAr03uX5Ugkoc7pDobcWopWYRJ8lCot0X7ZIY6A/eH/OVDsWmUJJFhZBy0su8ymNJI4fg5Hr/l6f3fdIG5nkEY+xEiBzxqX0DaJ+8= 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=mpyxPpVu; arc=none smtp.client-ip=209.85.210.202 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="mpyxPpVu" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-748cf01de06so5863778b3a.3 for ; Mon, 07 Jul 2025 13:44:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1751921061; x=1752525861; 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=+AbZUBhL4AUdhsW/eOFIK7IUkWM1FRN3cRsd+jlZ1ow=; b=mpyxPpVupKcrocJ+Vth09O9u2homMR6uWkW2V1L2wHw5dF53rW6i1TI22Rha42Qohu xxFK3qn76Fh2IL+9VZK8ZClQDW2LlX4LMSw/t3cmVBf4s8PoXus+3zL7/M2GOJJIrim9 zmVDW7dGYZuf91YBdHV00p5OQpmgejFfm89I6X7Jjj0I582ucWnxoTL+4wpRq3vkRLrs dIIsiDTUBzzvISAJlzgAWzVCAjxB2zx42X57JoIrlrGuuyFanNNrQ/gc9cIbE7rkEjqo GeQ5SP0KAByBfnn/gJGZ83a+EBZ4QUjEfuKmQLtphTYxZGirbiQKxaljZmzvNrA2Liz0 rhyQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1751921061; x=1752525861; 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=+AbZUBhL4AUdhsW/eOFIK7IUkWM1FRN3cRsd+jlZ1ow=; b=IlKcx2SWlezx3cqheBxmPmX9TQfE0VPFyVc7AqM+hInKXrjTRslAqNReuRRFAf8uxE YeNPcSxpv8o8IgEKLhvoLX8TfVgqwxH5uOy0PWVvXzW9NxlJIHLWDtwKKxx/rEgrWyJw P8nSKDDbZNoPlAtXxOnyosvMf8cVAIQEOGCZWGQZHrztrz4exZMyqWB82Ff3soZBrbz8 KrJKX5qvn20W+E79L7tr/pMSZOPyFqUmRUSYfeIjQbt95Rr7Q82hMS/1P0fUGH/BGHRy u5AkMUlDY3DWqR7WYZ/McnOl4ezev0LyEpLmA9qzT3tELKz4aZzI2lrglKsbydxw2Uji rkkw== X-Gm-Message-State: AOJu0Ywm+YwYytvKqoixA+livLj02fqdGyAssB28J3i2lAkoTiLhlfX+ 8p7rrDTaZfr9trh3KGlgme0e48eKGQUYp+LFsTSZSReHJ7N/p6KB0Bjm13m3wQjQkfZdwYcmeyW bqCRcu6QrsHtlZMmWq2f2neJ/yKrDkWpW1NIcMrYjZyJhGEAV+zLzbUuJYkUDYElzozE4mZ0jWj FSkSzzibPZelCj/dpwa39jMDuY+K6CsUi0+tngS7Tch3rb5MHj X-Google-Smtp-Source: AGHT+IH8AysgDbvr2jlirLawkTwfcXO2Tis4TXmiByHOmfCB95ums6tA2sAk201MUw+plz37ikslD1NFSWIc X-Received: from pfbhg10.prod.google.com ([2002:a05:6a00:860a:b0:749:1e51:d39a]) (user=jstultz job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:2d94:b0:748:311a:8aef with SMTP id d2e1a72fcca58-74ce66697admr20248541b3a.12.1751921061334; Mon, 07 Jul 2025 13:44:21 -0700 (PDT) Date: Mon, 7 Jul 2025 20:43:51 +0000 In-Reply-To: <20250707204409.1028494-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: <20250707204409.1028494-1-jstultz@google.com> X-Mailer: git-send-email 2.50.0.727.gbf7dc18ff4-goog Message-ID: <20250707204409.1028494-5-jstultz@google.com> Subject: [RESEND][PATCH v18 4/8] sched: Move update_curr_task logic into update_curr_se From: John Stultz To: LKML Cc: John Stultz , K Prateek Nayak , Joel Fernandes , Qais Yousef , Ingo Molnar , Peter Zijlstra , Juri Lelli , Vincent Guittot , Dietmar Eggemann , Valentin Schneider , Steven Rostedt , Ben Segall , Zimuzo Ezeozue , Mel Gorman , Will Deacon , Waiman Long , Boqun Feng , "Paul E. McKenney" , Metin Kaya , Xuewen Yan , Thomas Gleixner , Daniel Lezcano , Suleiman Souhlal , kuyo chang , hupu , kernel-team@android.com Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Absorb update_curr_task() into update_curr_se(), and in the process simplify update_curr_common(). This will make the next step a bit easier. Tested-by: K Prateek Nayak Signed-off-by: John Stultz Tested-by tags. --- 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: Mel Gorman 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/sched/fair.c | 26 +++++++++----------------- 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c index 7a14da5396fb2..edcc7d59ecc3b 100644 --- a/kernel/sched/fair.c +++ b/kernel/sched/fair.c @@ -1155,6 +1155,14 @@ static s64 update_curr_se(struct rq *rq, struct sche= d_entity *curr) curr->exec_start =3D now; curr->sum_exec_runtime +=3D delta_exec; =20 + if (entity_is_task(curr)) { + struct task_struct *p =3D task_of(curr); + + trace_sched_stat_runtime(p, delta_exec); + account_group_exec_runtime(p, delta_exec); + cgroup_account_cputime(p, delta_exec); + } + if (schedstat_enabled()) { struct sched_statistics *stats; =20 @@ -1166,13 +1174,6 @@ static s64 update_curr_se(struct rq *rq, struct sche= d_entity *curr) return delta_exec; } =20 -static inline void update_curr_task(struct task_struct *p, s64 delta_exec) -{ - trace_sched_stat_runtime(p, delta_exec); - account_group_exec_runtime(p, delta_exec); - cgroup_account_cputime(p, delta_exec); -} - static inline bool did_preempt_short(struct cfs_rq *cfs_rq, struct sched_e= ntity *curr) { if (!sched_feat(PREEMPT_SHORT)) @@ -1211,13 +1212,8 @@ static inline bool do_preempt_short(struct cfs_rq *c= fs_rq, s64 update_curr_common(struct rq *rq) { struct task_struct *donor =3D rq->donor; - s64 delta_exec; =20 - delta_exec =3D update_curr_se(rq, &donor->se); - if (likely(delta_exec > 0)) - update_curr_task(donor, delta_exec); - - return delta_exec; + return update_curr_se(rq, &donor->se); } =20 /* @@ -1242,10 +1238,6 @@ static void update_curr(struct cfs_rq *cfs_rq) update_min_vruntime(cfs_rq); =20 if (entity_is_task(curr)) { - struct task_struct *p =3D task_of(curr); - - update_curr_task(p, delta_exec); - /* * If the fair_server is active, we need to account for the * fair_server time whether or not the task is running on --=20 2.50.0.727.gbf7dc18ff4-goog From nobody Tue Oct 7 18:28:34 2025 Received: from mail-pg1-f202.google.com (mail-pg1-f202.google.com [209.85.215.202]) (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 E39A8262FFF for ; Mon, 7 Jul 2025 20:44:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751921065; cv=none; b=t+ajNGJlhjuyisiM3ri2ssSvf78wmTCmUEimtds3A0L2xBpaAv8QTr35AN9zSub9/PoBbgzI559/9hJlSAxfNuMce901Loac4trclMFxvS7JTaK3NDQWXrHupRtlBE0kt8YQdmBoOSD3tHJjqgaMtdFOCb6//QMMuvCdwt4ehUE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751921065; c=relaxed/simple; bh=lqAQZVltZS+lvkWKjTT0msQEex9/pUi+VcnFxgyd7tc=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=GWMeR62TxEjlysPAT25sUqrf4/6H6nMg9lbH/4HaZxhAq9ePKJ/nF4YlszFJQZgAv621M02hZJERrME2C7NRVXYKVhnEJ5ftRXxDcSEylA+uNjNji+ZwgKHIzaY6TDRCT5e8n/zkvX//gHNVEZ3z9Uom2wkBFJzF7al+WZkCPv8= 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=BPx5bRj2; arc=none smtp.client-ip=209.85.215.202 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="BPx5bRj2" Received: by mail-pg1-f202.google.com with SMTP id 41be03b00d2f7-b00e4358a34so2187948a12.0 for ; Mon, 07 Jul 2025 13:44:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1751921063; x=1752525863; 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=aHue3Nd9RiQZ9pLuaob+NG3Hrx0YWPuPvJ+cxebN6WE=; b=BPx5bRj28bZGHpRbLRvhJ3bMh8lZH5L/1PSUkWySx6RugOCslQq9QISN+rGfrRXks1 FK1p2h0xlIvoqfL2l6usFNLAO+7/G5xtHvIgU5F/4yNUzFfCFn8b4mFt9KUhIuGRzU24 XqftFV/dq+qrRnaKiEIz5hEfL8TpB01JkAret6fmDZZWOoICHrtAMOpBxMaOz3Ih/O4V /kzLck3dflJ1ktPPHJHLc5pdn8mujZbd8NztzchIm6NJ+FU8xBHNcUsbf+sxdTXt+etX eDz+GzX8n70ztP1nzAXuCqyEYcM/vNuli3uEYb+1htkef/I7El7uh17iYU+PNMGZdzh7 BhSw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1751921063; x=1752525863; 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=aHue3Nd9RiQZ9pLuaob+NG3Hrx0YWPuPvJ+cxebN6WE=; b=X7qJYtX7DFZxZk7fR/wCvrukKWKN7NRVY1ZUfej28HdlQxLplzrXRUkLcoNblsmI0C xyr08Xm/tj7xGC8lW0dJQP7bNw0jMr8fJ4Kf3wjwFUSqWyFzkP5MQortaPe6l2Wo/uh3 vUxVdLzBiowRZj8ct1IgXrQGOv2c0j+8x9g70Ruy8Lj5KSQn5kgaj2R4VLgbqsmMLzp2 rBr/V9dM1bP2JFJUx91tQWU0/AOcVa1foHR3eDazCmmWkPS9DY03wnBRqQOqk3D/L3DL 6kHtrLbSoDYqyorRPrx+WqV0gVvssG8a6YSVQBiu0+E/sNT4T0Z0bhnSdhN31xXra9MA fBSA== X-Gm-Message-State: AOJu0YzVTc3xZvu7EMwSoBSqntcQDjsQ5zPcqQLJ5SPDV010NVdj8UMU JeZ0ZYi7fRbXfSBPdC8clLA8U8pnG5Ftc4rqRXrZEW6qNSFKo7JMT0JghAIeQsXv8bhjb3DE1ug Z7f49Sg+0azD0azF2kEZPuCzlNGd322X1qHuf9EjQ2GplrDo+nOKQPCz3djWfbmMbMgy21+KXMg BJXYmSuUmspR7ghVEbuabEV+vgqqyQqexVMFrba6QkZIMZjkdp X-Google-Smtp-Source: AGHT+IEx5x4iZCDdsZic7GyPUoMuUfWYSVOK7+h6vDSyUXV5CkzHltFTs3EkzA0DtyGgC8tGfTk1Hns49q5r X-Received: from pgac12.prod.google.com ([2002:a05:6a02:294c:b0:b34:c32a:5da8]) (user=jstultz job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a21:9998:b0:218:9b3e:e8bd with SMTP id adf61e73a8af0-2260998b100mr20934369637.10.1751921062916; Mon, 07 Jul 2025 13:44:22 -0700 (PDT) Date: Mon, 7 Jul 2025 20:43:52 +0000 In-Reply-To: <20250707204409.1028494-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: <20250707204409.1028494-1-jstultz@google.com> X-Mailer: git-send-email 2.50.0.727.gbf7dc18ff4-goog Message-ID: <20250707204409.1028494-6-jstultz@google.com> Subject: [RESEND][PATCH v18 5/8] sched: Fix runtime accounting w/ split exec & sched contexts From: John Stultz To: LKML Cc: John Stultz , K Prateek Nayak , Joel Fernandes , Qais Yousef , Ingo Molnar , Peter Zijlstra , Juri Lelli , Vincent Guittot , Dietmar Eggemann , Valentin Schneider , Steven Rostedt , Ben Segall , Zimuzo Ezeozue , Mel Gorman , Will Deacon , Waiman Long , Boqun Feng , "Paul E. McKenney" , Metin Kaya , Xuewen Yan , Thomas Gleixner , Daniel Lezcano , Suleiman Souhlal , kuyo chang , hupu , kernel-team@android.com Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Without proxy-exec, we normally charge the "current" task for both its vruntime as well as its sum_exec_runtime. With proxy, however, we have two "current" contexts: the scheduler context and the execution context. We want to charge the execution context rq->curr (ie: proxy/lock holder) execution time to its sum_exec_runtime (so it's clear to userland the rq->curr task *is* running), as well as its thread group. However the rest of the time accounting (such a vruntime and cgroup accounting), we charge against the scheduler context (rq->donor) task, because it is from that task that the time is being "donated". If the donor and curr tasks are the same, then it's the same as without proxy. Tested-by: K Prateek Nayak Signed-off-by: John Stultz Tested-by tags. --- v16: * Renamed update_curr_se to update_se_times, as suggested by Steven Rostedt. * Reworded the commit message as suggested by Steven Rostedt v17: * Renamed update_se_times to update_se, as suggested by Peter * Added comment calrifying cfs_rq->curr being rq->donor.se as suggested by Peter * Reworked to fix issue Peter pointed out with thread group accounting being done on the donor, rather then the running execution context. * Slight rewording of the commit message to further clairifiy things. 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: Mel Gorman 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/sched/fair.c | 40 ++++++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c index edcc7d59ecc3b..c34e0891193a7 100644 --- a/kernel/sched/fair.c +++ b/kernel/sched/fair.c @@ -1143,30 +1143,40 @@ static void update_tg_load_avg(struct cfs_rq *cfs_r= q) } #endif /* CONFIG_SMP */ =20 -static s64 update_curr_se(struct rq *rq, struct sched_entity *curr) +static s64 update_se(struct rq *rq, struct sched_entity *se) { u64 now =3D rq_clock_task(rq); s64 delta_exec; =20 - delta_exec =3D now - curr->exec_start; + delta_exec =3D now - se->exec_start; if (unlikely(delta_exec <=3D 0)) return delta_exec; =20 - curr->exec_start =3D now; - curr->sum_exec_runtime +=3D delta_exec; + se->exec_start =3D now; + if (entity_is_task(se)) { + struct task_struct *donor =3D task_of(se); + struct task_struct *running =3D rq->curr; + /* + * If se is a task, we account the time against the running + * task, as w/ proxy-exec they may not be the same. + */ + running->se.exec_start =3D now; + running->se.sum_exec_runtime +=3D delta_exec; =20 - if (entity_is_task(curr)) { - struct task_struct *p =3D task_of(curr); + trace_sched_stat_runtime(running, delta_exec); + account_group_exec_runtime(running, delta_exec); =20 - trace_sched_stat_runtime(p, delta_exec); - account_group_exec_runtime(p, delta_exec); - cgroup_account_cputime(p, delta_exec); + /* cgroup time is always accounted against the donor */ + cgroup_account_cputime(donor, delta_exec); + } else { + /* If not task, account the time against donor se */ + se->sum_exec_runtime +=3D delta_exec; } =20 if (schedstat_enabled()) { struct sched_statistics *stats; =20 - stats =3D __schedstats_from_se(curr); + stats =3D __schedstats_from_se(se); __schedstat_set(stats->exec_max, max(delta_exec, stats->exec_max)); } @@ -1213,7 +1223,7 @@ s64 update_curr_common(struct rq *rq) { struct task_struct *donor =3D rq->donor; =20 - return update_curr_se(rq, &donor->se); + return update_se(rq, &donor->se); } =20 /* @@ -1221,6 +1231,12 @@ s64 update_curr_common(struct rq *rq) */ static void update_curr(struct cfs_rq *cfs_rq) { + /* + * Note: cfs_rq->curr corresponds to the task picked to + * run (ie: rq->donor.se) which due to proxy-exec may + * not necessarily be the actual task running + * (rq->curr.se). This is easy to confuse! + */ struct sched_entity *curr =3D cfs_rq->curr; struct rq *rq =3D rq_of(cfs_rq); s64 delta_exec; @@ -1229,7 +1245,7 @@ static void update_curr(struct cfs_rq *cfs_rq) if (unlikely(!curr)) return; =20 - delta_exec =3D update_curr_se(rq, curr); + delta_exec =3D update_se(rq, curr); if (unlikely(delta_exec <=3D 0)) return; =20 --=20 2.50.0.727.gbf7dc18ff4-goog From nobody Tue Oct 7 18:28:34 2025 Received: from mail-pg1-f202.google.com (mail-pg1-f202.google.com [209.85.215.202]) (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 20D5026562C for ; Mon, 7 Jul 2025 20:44:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751921069; cv=none; b=LeJBU5zh2ljVDIqfqwNJ891abr+uL8XrR11Qk96H+9b1KVXv7YBeJRl7wHv4udjs1CfZqv8pOzPQFn3TokZeYO0H9QhdpO53tZQBCuFe6CdhaXN5gxY/proaAsl3FjNu8i4RyYH8xlP8Q7bzpkzmrTuaF8eNz5HKFSkwOJm2B30= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751921069; c=relaxed/simple; bh=65aTOo3/PoW85wXoc+sQcKL+wkQ2DIJV8Su0O+nklrs=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=cE4RPIO41VO8+/Om82rgHuIMY2Gz/SwFH+pQwk0k4J+9pHVfWLF+igyiUHqrC6WRXgIpbxvNXD8/mNdlFJTqlFJnDOrLsa4xNp5mfifP/HgH0O+F0iSnF7Kuks2e++Gb6bX6jCkghw6pUR0Dk3N8Z8SVriM2gGemHMe8CNG6nxw= 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=EW4obEXD; arc=none smtp.client-ip=209.85.215.202 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="EW4obEXD" Received: by mail-pg1-f202.google.com with SMTP id 41be03b00d2f7-b38ec062983so2330245a12.3 for ; Mon, 07 Jul 2025 13:44:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1751921064; x=1752525864; 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=Wl1xJj/1Ciw5/8ZxYlRHCnf/dLxcgLYG//PTqoDrKQo=; b=EW4obEXDk5IC7J3LGlEbnTG5RgxAxjYF/os6ZUnw2m4tUFLFNZLvXNeM0MSyjgE/ct 4Xoj/WHYtqO1N5SNBKznoPlBbF5vczm5dZMYUa57Dhc//W3xc1huabPsGIYhWTsscbZJ imlMSlyL59/TuGS0JrNGiyOmAFvJ3AM/XNwykqVgPBWSSnej3z3XEwaSQWjTTB4LVXXp s+X8Y4ggKlrALuMdaJXqrBb1bAI6iy/Gyvvv215ylr19VuMLNPRdbYdAWy19Fn+B+XVk gy6rMlm/bOPHyWgpN7F26FMHNiXkZdysazBZbgFnwbeZSlbAHvTy+EtRxF2v7nfFE5vH x9Dw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1751921064; x=1752525864; 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=Wl1xJj/1Ciw5/8ZxYlRHCnf/dLxcgLYG//PTqoDrKQo=; b=qgJbUeLGVcqvLXc8FrW1fye+bwSKrsnfmQYP+4OSx3NPUZDsR2nASDQT2rRk9PPo9F oa1dhR3J1MuJhoAlCk5QW/0IklAuM4oRoFvH7AJFsYeY7RnhE+nR5Cez72L461Z4C7In ih65D7/WEUdTh7infAlaZ4DD4B91lsIAO0uHVbo+HWf7lPUByirTybvEELwn0Iey/+sB CCpjqa8HFJb67tk4QTWeik9yN0No9z/posp0t1JdOfF7JJ9Qi9ZlfoNOrveAcbO8remR yfJTgpqaD1/upTpEi2cTFRtrItrNMOzZ+REON+6+gFj5JSfl5EnV3ugQQtW34dN/3l3E YRNg== X-Gm-Message-State: AOJu0Yx4xtKAwRIqFERk3rF4ry+B3EZqT6kzYsfFZJJ7PDazGYSimvkm w8B+FrlJGYOe7iFT00h6hm9dijygsjow8g2/zYdNjWKHzWzRoa2dqQu06YRsysaqixn8D9kG+vn 4rUq2igQoBZj6T1wmJLN7sd61tQ29TXl14Fx+krwkYEzLbdXTEnwbLJYcJ6lbHfOpMlrjv68aJ1 Sih5S5be+AIhjc1SuyYVxqR/GGyHh8b92osOwqVFWIxbpprNLH X-Google-Smtp-Source: AGHT+IG6a6yF/dP7r64wCMyq6s87oym3hIRvT2d4PAwKXLMFQOII6D+5XqBU4TKeo13VMyezyhkIA5nR2Z9K X-Received: from pgcv10.prod.google.com ([2002:a05:6a02:530a:b0:b2e:c15e:3eb7]) (user=jstultz job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a21:4612:b0:220:3024:3d05 with SMTP id adf61e73a8af0-22b1bc58967mr1529266637.16.1751921064381; Mon, 07 Jul 2025 13:44:24 -0700 (PDT) Date: Mon, 7 Jul 2025 20:43:53 +0000 In-Reply-To: <20250707204409.1028494-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: <20250707204409.1028494-1-jstultz@google.com> X-Mailer: git-send-email 2.50.0.727.gbf7dc18ff4-goog Message-ID: <20250707204409.1028494-7-jstultz@google.com> Subject: [RESEND][PATCH v18 6/8] sched: Add an initial sketch of the find_proxy_task() function From: John Stultz To: LKML Cc: John Stultz , K Prateek Nayak , Joel Fernandes , Qais Yousef , Ingo Molnar , Peter Zijlstra , Juri Lelli , Vincent Guittot , Dietmar Eggemann , Valentin Schneider , Steven Rostedt , Ben Segall , Zimuzo Ezeozue , Mel Gorman , Will Deacon , Waiman Long , Boqun Feng , "Paul E. McKenney" , Metin Kaya , Xuewen Yan , Thomas Gleixner , Daniel Lezcano , Suleiman Souhlal , kuyo chang , hupu , kernel-team@android.com Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add a find_proxy_task() function which doesn't do much. When we select a blocked task to run, we will just deactivate it and pick again. The exception being if it has become unblocked after find_proxy_task() was called. This allows us to validate keeping blocked tasks on the runqueue and later deactivating them is working ok, stressing the failure cases for when a proxy isn't found. Greatly simplified from patch by: Peter Zijlstra (Intel) Juri Lelli Valentin Schneider Connor O'Brien Tested-by: K Prateek Nayak [jstultz: Split out from larger proxy patch and simplified for review and testing.] Signed-off-by: John Stultz Tested-by tags. --- v5: * Split out from larger proxy patch v7: * Fixed unused function arguments, spelling nits, and tweaks for clarity, pointed out by Metin Kaya * Fix build warning Reported-by: kernel test robot Closes: https://lore.kernel.org/oe-kbuild-all/202311081028.yDLmCWgr-lkp@i= ntel.com/ v8: * Fixed case where we might return a blocked task from find_proxy_task() * Continued tweaks to handle avoiding returning blocked tasks v9: * Add zap_balance_callbacks helper to unwind balance_callbacks when we will re-call pick_next_task() again. * Add extra comment suggested by Metin * Typo fixes from Metin * Moved adding proxy_resched_idle earlier in the series, as suggested by Metin * Fix to call proxy_resched_idle() *prior* to deactivating next, to avoid crashes caused by stale references to next * s/PROXY/SCHED_PROXY_EXEC/ as suggested by Metin * Number of tweaks and cleanups suggested by Metin * Simplify proxy_deactivate as suggested by Metin v11: * Tweaks for earlier simplification in try_to_deactivate_task v13: * Rename rename "next" to "donor" in find_proxy_task() for clarity * Similarly use "donor" instead of next in proxy_deactivate * Refactor/simplify proxy_resched_idle * Moved up a needed fix from later in the series v15: * Tweaked some comments to better explain the initial sketch of find_proxy_task(), suggested by Qais * Build fixes for !CONFIG_SMP * Slight rework for blocked_on_state being added later in the series. * Move the zap_balance_callbacks to later in the patch series v16: * Move the enqueue_task_rt() out to later in the series, as suggested by K Prateek Nayak * Fixup whitespace error pointed out by K Prateek Nayak * Use put_prev_set_next_task as suggested by K Prateek Nayak * Try to rework find_proxy_task() locking to use guard and proxy_deactivate_task() in the way Peter suggested. v17: * Slightly simplified variable names per suggestion from Juri Lelli * Minor comment and commit message tweaks suggested by Peter 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: Mel Gorman 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/sched/core.c | 100 +++++++++++++++++++++++++++++++++++++++++-- kernel/sched/sched.h | 10 ++++- 2 files changed, 106 insertions(+), 4 deletions(-) diff --git a/kernel/sched/core.c b/kernel/sched/core.c index 853157b27f384..dc82d9b8bee2c 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -6614,7 +6614,8 @@ pick_next_task(struct rq *rq, struct task_struct *pre= v, struct rq_flags *rf) * Otherwise marks the task's __state as RUNNING */ static bool try_to_block_task(struct rq *rq, struct task_struct *p, - unsigned long *task_state_p) + unsigned long *task_state_p, + bool deactivate_cond) { unsigned long task_state =3D *task_state_p; int flags =3D DEQUEUE_NOCLOCK; @@ -6625,6 +6626,9 @@ static bool try_to_block_task(struct rq *rq, struct t= ask_struct *p, return false; } =20 + if (!deactivate_cond) + return false; + p->sched_contributes_to_load =3D (task_state & TASK_UNINTERRUPTIBLE) && !(task_state & TASK_NOLOAD) && @@ -6648,6 +6652,89 @@ static bool try_to_block_task(struct rq *rq, struct = task_struct *p, return true; } =20 +#ifdef CONFIG_SCHED_PROXY_EXEC +static inline struct task_struct *proxy_resched_idle(struct rq *rq) +{ + put_prev_set_next_task(rq, rq->donor, rq->idle); + rq_set_donor(rq, rq->idle); + set_tsk_need_resched(rq->idle); + return rq->idle; +} + +static bool __proxy_deactivate(struct rq *rq, struct task_struct *donor) +{ + unsigned long state =3D READ_ONCE(donor->__state); + + /* Don't deactivate if the state has been changed to TASK_RUNNING */ + if (state =3D=3D TASK_RUNNING) + return false; + /* + * Because we got donor from pick_next_task(), it is *crucial* + * that we call proxy_resched_idle() before we deactivate it. + * As once we deactivate donor, donor->on_rq is set to zero, + * which allows ttwu() to immediately try to wake the task on + * another rq. So we cannot use *any* references to donor + * after that point. So things like cfs_rq->curr or rq->donor + * need to be changed from next *before* we deactivate. + */ + proxy_resched_idle(rq); + return try_to_block_task(rq, donor, &state, true); +} + +static struct task_struct *proxy_deactivate(struct rq *rq, struct task_str= uct *donor) +{ + if (!__proxy_deactivate(rq, donor)) { + /* + * XXX: For now, if deactivation failed, set donor + * as unblocked, as we aren't doing proxy-migrations + * yet (more logic will be needed then). + */ + donor->blocked_on =3D NULL; + } + return NULL; +} + +/* + * Initial simple sketch that just deactivates the blocked task + * chosen by pick_next_task() so we can then pick something that + * isn't blocked. + */ +static struct task_struct * +find_proxy_task(struct rq *rq, struct task_struct *donor, struct rq_flags = *rf) +{ + struct mutex *mutex; + + mutex =3D donor->blocked_on; + /* Something changed in the chain, so pick again */ + if (!mutex) + return NULL; + /* + * By taking mutex->wait_lock we hold off concurrent mutex_unlock() + * and ensure @owner sticks around. + */ + guard(raw_spinlock)(&mutex->wait_lock); + + /* Check again that donor is blocked with blocked_lock held */ + if (!task_is_blocked(donor) || mutex !=3D __get_task_blocked_on(donor)) { + /* + * Something changed in the blocked_on chain and + * we don't know if only at this level. So, let's + * just bail out completely and let __schedule() + * figure things out (pick_again loop). + */ + return NULL; /* do pick_next_task() again */ + } + return proxy_deactivate(rq, donor); +} +#else /* SCHED_PROXY_EXEC */ +static struct task_struct * +find_proxy_task(struct rq *rq, struct task_struct *donor, struct rq_flags = *rf) +{ + WARN_ONCE(1, "This should never be called in the !SCHED_PROXY_EXEC case\n= "); + return donor; +} +#endif /* SCHED_PROXY_EXEC */ + /* * __schedule() is the main scheduler function. * @@ -6760,12 +6847,19 @@ static void __sched notrace __schedule(int sched_mo= de) goto picked; } } else if (!preempt && prev_state) { - try_to_block_task(rq, prev, &prev_state); + try_to_block_task(rq, prev, &prev_state, + !task_is_blocked(prev)); switch_count =3D &prev->nvcsw; } =20 - next =3D pick_next_task(rq, prev, &rf); +pick_again: + next =3D pick_next_task(rq, rq->donor, &rf); rq_set_donor(rq, next); + if (unlikely(task_is_blocked(next))) { + next =3D find_proxy_task(rq, next, &rf); + if (!next) + goto pick_again; + } picked: clear_tsk_need_resched(prev); clear_preempt_need_resched(); diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h index 6b61e0c7f6e78..590a44c0215fb 100644 --- a/kernel/sched/sched.h +++ b/kernel/sched/sched.h @@ -2290,6 +2290,14 @@ static inline int task_current_donor(struct rq *rq, = struct task_struct *p) return rq->donor =3D=3D p; } =20 +static inline bool task_is_blocked(struct task_struct *p) +{ + if (!sched_proxy_exec()) + return false; + + return !!p->blocked_on; +} + static inline int task_on_cpu(struct rq *rq, struct task_struct *p) { #ifdef CONFIG_SMP @@ -2499,7 +2507,7 @@ static inline void put_prev_set_next_task(struct rq *= rq, struct task_struct *prev, struct task_struct *next) { - WARN_ON_ONCE(rq->curr !=3D prev); + WARN_ON_ONCE(rq->donor !=3D prev); =20 __put_prev_set_next_dl_server(rq, prev, next); =20 --=20 2.50.0.727.gbf7dc18ff4-goog From nobody Tue Oct 7 18:28:34 2025 Received: from mail-pf1-f202.google.com (mail-pf1-f202.google.com [209.85.210.202]) (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 DB609267B74 for ; Mon, 7 Jul 2025 20:44:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751921068; cv=none; b=szuqVNTAUoXvOifpffy8JkvO3MfFZMzddq0OK+xGuKX56AnHoVCDCpEgA8efIbpqwOB1GeTK1VPp869IBmCqwZOYeDS+1D3pawlveLhCBeSHbV6+fE7ri26thx1A9Fmo84XxofTdcFltLJcMZI+TLivk1d5zt0HAEdH4Wb+HyOE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751921068; c=relaxed/simple; bh=u/y5/Gw7ZLh2MeaqyrXP30V3g6GshyA2dah4uiCZBoM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=iX8ZBUqtad1q5n2p9Nb49v/cPsvCMvUXgYJT2xGVj+exHrixGIKHM2eluOCNahsAUyMV5gBbvdQjQDCqWXsgjR0qGHJOw5l65t3/91LIdV+TH4542andMLJe0YkRJKOoU0XIG2ae0EARId4Nvl1xjgIeAN6VGvkZV+P6dvMrd3Y= 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=Rr6SWeF3; arc=none smtp.client-ip=209.85.210.202 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="Rr6SWeF3" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-74ad608d60aso2649174b3a.1 for ; Mon, 07 Jul 2025 13:44:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1751921066; x=1752525866; 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=LeChAIRUP8/BaHB2cXuv3ihMNbQP7hRMMG0hSAPxSR4=; b=Rr6SWeF36TsAXSHdqplvKgVLYoUbP2W5msKhs2v3p6cYEkvSNKNdNdV42RUYA+s+Pz rdBF/uVKUB7W1ilW0wYyhuQDRH7R2lvGOR74Ph0TvKs0bXXtC5yv1bjOIUqQYn/cCFF8 LVnwPeGzTzoIcuWZdS0sBI0r2tRTIVcQlN3uuF7v+Ccuv3zKfR9vyvOY7JZ/7AtqsoF1 PhiIME1T/retxP0Q6cVWlF77RyfTutne2UmVnpucFICazXf4KjMFUYRufBqYsEmuAFzb tC65cl6LypsCR/TLrrm+FpPqS4BsrI/RKHzzfYqw/LaswFj6w3rDzvQstZpRWXoMDLTL 77pQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1751921066; x=1752525866; 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=LeChAIRUP8/BaHB2cXuv3ihMNbQP7hRMMG0hSAPxSR4=; b=X2YH2n6Qq1M4WoXJ/A4untRrxZj1zqd1+3MtlIPfvirSNOAEf7Mljmr+k+qFMgsN/X YD4Y+Z3761T7t++vq0Rvr+gv+g5/hgJbtSbXDbIrTV7VEUlaMs7xscALtFKpf4GvL0II 3Dz5bKE2LJRmKI00wVxbJzFEV6q2+/menIQMVDb3pRk4PK5klpybRPd1sJTfw2AFlg76 MafqHOlPrKEQlljI/wpy8qYJ6VaRtjtr4iCARCgVJNBOdUu6RT12T0xFI+lFKn0j0Lz3 QyMGnyp0rk2WDlJ1F4KP7x9eI5trCINmJ6b5Ws698cVL6qfUIfDz9RfyCdImoR1M21PA iIIg== X-Gm-Message-State: AOJu0YxIzjjj4/xXISMgOp/4iWpeQ5eyQjlU/E5h3PvZoT5+1uikRkH+ uJFyz8a5Yxhkb0f8HPlXf5hdYNpg0YLbNbYIXEDLMcKoL7OpSxod0VfxUyqLo8j6l904vTBEnYP KwuN4+tdUkKBMw4Y9+OI2mlqVbI7psS9jxOBLlpsjn0ES6CrfJseidZ2JEEnAM/JD+46sRIjLOG LVZeuy6zihR/iCaEdsJo5N2TGRc1V6sWI3RX0E9trEyRvoSwi0 X-Google-Smtp-Source: AGHT+IG+XDBs41hxSauq/yiNAwfjIjYo5GdA3sWEsLg99YPpVMVwNGPr94NGnfdtvxxOD6jzdpuGgqSn+GBq X-Received: from pfbcr14.prod.google.com ([2002:a05:6a00:f0e:b0:746:2e5d:3e6a]) (user=jstultz job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:218c:b0:72d:3b2e:fef9 with SMTP id d2e1a72fcca58-74ce65d7abamr20079658b3a.20.1751921065945; Mon, 07 Jul 2025 13:44:25 -0700 (PDT) Date: Mon, 7 Jul 2025 20:43:54 +0000 In-Reply-To: <20250707204409.1028494-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: <20250707204409.1028494-1-jstultz@google.com> X-Mailer: git-send-email 2.50.0.727.gbf7dc18ff4-goog Message-ID: <20250707204409.1028494-8-jstultz@google.com> Subject: [RESEND][PATCH v18 7/8] sched: Fix proxy/current (push,pull)ability From: John Stultz To: LKML Cc: Valentin Schneider , K Prateek Nayak , "Connor O'Brien" , John Stultz , Joel Fernandes , Qais Yousef , Ingo Molnar , Peter Zijlstra , Juri Lelli , Vincent Guittot , Dietmar Eggemann , Valentin Schneider , Steven Rostedt , Ben Segall , Zimuzo Ezeozue , Mel Gorman , Will Deacon , Waiman Long , Boqun Feng , "Paul E. McKenney" , Metin Kaya , Xuewen Yan , Thomas Gleixner , Daniel Lezcano , Suleiman Souhlal , kuyo chang , hupu , kernel-team@android.com Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Valentin Schneider Proxy execution forms atomic pairs of tasks: The waiting donor task (scheduling context) and a proxy (execution context). The donor task, along with the rest of the blocked chain, follows the proxy wrt CPU placement. They can be the same task, in which case push/pull doesn't need any modification. When they are different, however, FIFO1 & FIFO42: ,-> RT42 | | blocked-on | v blocked_donor | mutex | | owner | v `-- RT1 RT1 RT42 CPU0 CPU1 ^ ^ | | overloaded !overloaded rq prio =3D 42 rq prio =3D 0 RT1 is eligible to be pushed to CPU1, but should that happen it will "carry" RT42 along. Clearly here neither RT1 nor RT42 must be seen as push/pullable. Unfortunately, only the donor task is usually dequeued from the rq, and the proxy'ed execution context (rq->curr) remains on the rq. This can cause RT1 to be selected for migration from logic like the rt pushable_list. Thus, adda a dequeue/enqueue cycle on the proxy task before __schedule returns, which allows the sched class logic to avoid adding the now current task to the pushable_list. Furthermore, tasks becoming blocked on a mutex don't need an explicit dequeue/enqueue cycle to be made (push/pull)able: they have to be running to block on a mutex, thus they will eventually hit put_prev_task(). Tested-by: K Prateek Nayak Signed-off-by: Valentin Schneider Signed-off-by: Connor O'Brien Signed-off-by: John Stultz Tested-by tags. --- v3: * Tweaked comments & commit message v5: * Minor simplifications to utilize the fix earlier in the patch series. * Rework the wording of the commit message to match selected/ proxy terminology and expand a bit to make it more clear how it works. v6: * Dropped now-unused proxied value, to be re-added later in the series when it is used, as caught by Dietmar v7: * Unused function argument fixup * Commit message nit pointed out by Metin Kaya * Dropped unproven unlikely() and use sched_proxy_exec() in proxy_tag_curr, suggested by Metin Kaya v8: * More cleanups and typo fixes suggested by Metin Kaya v11: * Cleanup of comimt message suggested by Metin v12: * Rework for rq_selected -> rq->donor renaming v16: * Pulled logic from later patch in to avoid sched_balance migrating blocked tasks. * Moved enqueue_task_rt() logic from earlier into this patch as suggested by K Prateek Nayak * Simplified changes to enqueue_task_rt to match deadline's logic, as pointed out by Peter v17: * Add similar logic to put_prev_task_dl as pointed out by K Prateek Nayak 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: Mel Gorman 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/sched/core.c | 25 +++++++++++++++++++++++++ kernel/sched/deadline.c | 7 +++++++ kernel/sched/rt.c | 5 +++++ 3 files changed, 37 insertions(+) diff --git a/kernel/sched/core.c b/kernel/sched/core.c index dc82d9b8bee2c..98a802e0a3f36 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -6735,6 +6735,23 @@ find_proxy_task(struct rq *rq, struct task_struct *d= onor, struct rq_flags *rf) } #endif /* SCHED_PROXY_EXEC */ =20 +static inline void proxy_tag_curr(struct rq *rq, struct task_struct *owner) +{ + if (!sched_proxy_exec()) + return; + /* + * pick_next_task() calls set_next_task() on the chosen task + * at some point, which ensures it is not push/pullable. + * However, the chosen/donor task *and* the mutex owner form an + * atomic pair wrt push/pull. + * + * Make sure owner we run is not pushable. Unfortunately we can + * only deal with that by means of a dequeue/enqueue cycle. :-/ + */ + dequeue_task(rq, owner, DEQUEUE_NOCLOCK | DEQUEUE_SAVE); + enqueue_task(rq, owner, ENQUEUE_NOCLOCK | ENQUEUE_RESTORE); +} + /* * __schedule() is the main scheduler function. * @@ -6873,6 +6890,10 @@ static void __sched notrace __schedule(int sched_mod= e) * changes to task_struct made by pick_next_task(). */ RCU_INIT_POINTER(rq->curr, next); + + if (!task_current_donor(rq, next)) + proxy_tag_curr(rq, next); + /* * The membarrier system call requires each architecture * to have a full memory barrier after updating @@ -6907,6 +6928,10 @@ static void __sched notrace __schedule(int sched_mod= e) /* Also unlocks the rq: */ rq =3D context_switch(rq, prev, next, &rf); } else { + /* In case next was already curr but just got blocked_donor */ + if (!task_current_donor(rq, next)) + proxy_tag_curr(rq, next); + rq_unpin_lock(rq, &rf); __balance_callbacks(rq); raw_spin_rq_unlock_irq(rq); diff --git a/kernel/sched/deadline.c b/kernel/sched/deadline.c index ad45a8fea245e..4757c29d0979e 100644 --- a/kernel/sched/deadline.c +++ b/kernel/sched/deadline.c @@ -2166,6 +2166,9 @@ static void enqueue_task_dl(struct rq *rq, struct tas= k_struct *p, int flags) if (dl_server(&p->dl)) return; =20 + if (task_is_blocked(p)) + return; + if (!task_current(rq, p) && !p->dl.dl_throttled && p->nr_cpus_allowed > 1) enqueue_pushable_dl_task(rq, p); } @@ -2465,6 +2468,10 @@ static void put_prev_task_dl(struct rq *rq, struct t= ask_struct *p, struct task_s update_curr_dl(rq); =20 update_dl_rq_load_avg(rq_clock_pelt(rq), rq, 1); + + if (task_is_blocked(p)) + return; + if (on_dl_rq(&p->dl) && p->nr_cpus_allowed > 1) enqueue_pushable_dl_task(rq, p); } diff --git a/kernel/sched/rt.c b/kernel/sched/rt.c index e40422c370335..b1fc9e6cf4a60 100644 --- a/kernel/sched/rt.c +++ b/kernel/sched/rt.c @@ -1488,6 +1488,9 @@ enqueue_task_rt(struct rq *rq, struct task_struct *p,= int flags) =20 enqueue_rt_entity(rt_se, flags); =20 + if (task_is_blocked(p)) + return; + if (!task_current(rq, p) && p->nr_cpus_allowed > 1) enqueue_pushable_task(rq, p); } @@ -1768,6 +1771,8 @@ static void put_prev_task_rt(struct rq *rq, struct ta= sk_struct *p, struct task_s =20 update_rt_rq_load_avg(rq_clock_pelt(rq), rq, 1); =20 + if (task_is_blocked(p)) + return; /* * The previous task needs to be made eligible for pushing * if it is still active --=20 2.50.0.727.gbf7dc18ff4-goog From nobody Tue Oct 7 18:28:34 2025 Received: from mail-pf1-f202.google.com (mail-pf1-f202.google.com [209.85.210.202]) (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 7A134268688 for ; Mon, 7 Jul 2025 20:44:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751921070; cv=none; b=oeo52FfB4Zqg4FApYruNF79aq/OQSmDKv09oUjwFJ7JRQXe1YLW5mIDQsDb0LyP6GamCAMyyK4+OY+XHrtqC6IAXcnDrTWMsQ8EJfEIhdwuPp7oZkgPQfvECKiSR+L++BpuqEQYCdtrdT7/k2AhybQDU8N06Sz8M26KTxU/AHh0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751921070; c=relaxed/simple; bh=Hxz8uG4m5R/mzhVQ3Bo8z4NItlLHV7QW9xm/h3/v3EU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=SGieb4kTe7xpatUMitTilvpXfFbccTImhflZzxvCBnsl22jiboG71MqEFKBMbAUHR3vwoeW62pQnKhoOSbiSq7Bukf22DCvv+utwgujbHnC21WVqGpPPELGOPZ5PqKfcLqXckHmC3IfCEzjT8ul55u1P4izZ+T+DgrK3ZR8hq2o= 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=d3JE4nCk; arc=none smtp.client-ip=209.85.210.202 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="d3JE4nCk" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-74928291bc3so2651261b3a.0 for ; Mon, 07 Jul 2025 13:44:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1751921068; x=1752525868; 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=DJu+JtHA2rRmbqCAjoFs3m/1LJQvgxh9w2qROU6np4Y=; b=d3JE4nCkvx8UFP9jxZH6YSnTZ4pBGLfD0HWpR97USSz/sN0Liy5717wjjdkny8P9GR IgoeIT978hxqyzSIUy5cLKjy2ozTbhBaZ+5c41LhoFyO6Wr5CaFhh07DVthMroiLdZRS SCxxJeICDAKLWb+YmfsfgMgLgdGTct24u0yJMmGBD+d83dvBLunHfJwqKEAiq2EA9DLh sM3MZhJTNnmI3yHLzAoWj+aO3dDdLzqQiAD+vNZg+pa035Wi97Y4trtfr1a9Jt+hft+n nQsTWjrHxWDmKl9tpmEi1LiZhA8h5hVFDESYOhHkzZfh0wsGGBkYOsscUjamkfgL92CE xbNw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1751921068; x=1752525868; 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=DJu+JtHA2rRmbqCAjoFs3m/1LJQvgxh9w2qROU6np4Y=; b=myoexAAqLb4aSx4n1EPn3/5enzR7Sk7d9PlWC1Sz1jZ7zIG9WT1iOPTlHiyUVfuXcd VHbUgJyHkby88hjEiHyQJyf82BFwIeI1tKKAp8RNyl+Lsc5FolyFUffW55F/rGKKNgJU JGeKoXjkJ2532SBpJ5BdgOOBOb7oJIbuMTdCgXeDwBDLeqcRzRKPADp7hLgLd/2a8Jhm rV3guG+icukOrjgudiUuzMyXv9Yqb/BB9D5Gupb3QT7ITDUtsI0f+tPBE0/CazidlKle CVQiDL0C4y4uK1YGtyUdMMLyMnGKlhd5o0BzPS0bZRNkYlEe14iFUPiOIgmqx3DfS+7J yY9g== X-Gm-Message-State: AOJu0YwvjFhtJh862tIZ4suLb56nMDH5bb7ojUO3+33iIaD9H3sPKV2E V6ekbrxsUN1MxrRRz8W0C3E1JNym4vdml3jwpnfz2ceFYk8i0NLW/0ys4g619uS182vOOWeQgb+ gtfeSN0ZGrS9pM+sUdKqtYYd7xKcNLkDKUOqiPG8qJt/FMwhlyzkFLeUkiKFpWCa9XEqu832e1w N104+7SM/ZdYOZTkrmKt0CEtvzww5axzZ3Bjef/lIF3A//mAjM X-Google-Smtp-Source: AGHT+IEbt6q5/NDrIGflHjOrIVeescszMrLFojseR+515LGzg5VXIodSxfGL5jJgLxMXGtY5USOxeJnQWM4n X-Received: from pfop14.prod.google.com ([2002:a05:6a00:b4e:b0:748:ea16:c7ad]) (user=jstultz job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:10cd:b0:742:3fe0:8289 with SMTP id d2e1a72fcca58-74d2672c972mr97171b3a.20.1751921067550; Mon, 07 Jul 2025 13:44:27 -0700 (PDT) Date: Mon, 7 Jul 2025 20:43:55 +0000 In-Reply-To: <20250707204409.1028494-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: <20250707204409.1028494-1-jstultz@google.com> X-Mailer: git-send-email 2.50.0.727.gbf7dc18ff4-goog Message-ID: <20250707204409.1028494-9-jstultz@google.com> Subject: [RESEND][PATCH v18 8/8] sched: Start blocked_on chain processing in find_proxy_task() From: John Stultz To: LKML Cc: Peter Zijlstra , K Prateek Nayak , Juri Lelli , Valentin Schneider , "Connor O'Brien" , John Stultz , Joel Fernandes , Qais Yousef , Ingo Molnar , Vincent Guittot , Dietmar Eggemann , Valentin Schneider , Steven Rostedt , Ben Segall , Zimuzo Ezeozue , Mel Gorman , Will Deacon , Waiman Long , Boqun Feng , "Paul E. McKenney" , Metin Kaya , Xuewen Yan , Thomas Gleixner , Daniel Lezcano , Suleiman Souhlal , kuyo chang , hupu , kernel-team@android.com Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Peter Zijlstra Start to flesh out the real find_proxy_task() implementation, but avoid the migration cases for now, in those cases just deactivate the donor task and pick again. To ensure the donor task or other blocked tasks in the chain aren't migrated away while we're running the proxy, also tweak the fair class logic to avoid migrating donor or mutex blocked tasks. Tested-by: K Prateek Nayak Signed-off-by: Peter Zijlstra (Intel) Signed-off-by: Juri Lelli Signed-off-by: Valentin Schneider Signed-off-by: Connor O'Brien [jstultz: This change was split out from the larger proxy patch] Signed-off-by: John Stultz Tested-by tags. --- v5: * Split this out from larger proxy patch v7: * Minor refactoring of core find_proxy_task() function * Minor spelling and corrections suggested by Metin Kaya * Dropped an added BUG_ON that was frequently tripped v8: * Fix issue if proxy_deactivate fails, we don't leave task BO_BLOCKED * Switch to WARN_ON from BUG_ON checks v9: * Improve comments suggested by Metin * Minor cleanups v11: * Previously we checked next=3D=3Drq->idle && prev=3D=3Drq->idle, but I think we only really care if next=3D=3Drq->idle from find_proxy_task, as we will still want to resched regardless of what prev was. v12: * Commit message rework for selected -> donor rewording v13: * Address new delayed dequeue condition (deactivate donor for now) * Next to donor renaming in find_proxy_task * Improved comments for find_proxy_task * Rework for proxy_deactivate cleanup v14: * Fix build error from __mutex_owner() with CONFIG_PREEMPT_RT v15: * Reworks for moving blocked_on_state to later in the series v16: * Pull down fix from later in the series where a deactivated task could pass the (task_cpu(owner) =3D=3D this_cpu) check then have it be activated on a different cpu, so it passes the on_rq check. Thus double check the values in the opposite order to make sure nothing slips by. * Add resched_idle label to simplify common exit path * Get rid of preserve_need_resched flag and rework per Peter's suggestion * Rework find_proxy_task() to use guard to cleanup the exit gotos as Peter suggested. v18: * Minor tweak to order of logic in find_proxy_task() to enable an optimization later in the full series suggested by Suleiman * Use READ_ONCE calls to on_rq references as I hit a bug where the compiler was optimizing out some of the reads. 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: Mel Gorman 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.h | 3 +- kernel/sched/core.c | 143 ++++++++++++++++++++++++++++++++++------- kernel/sched/fair.c | 11 +++- 3 files changed, 132 insertions(+), 25 deletions(-) diff --git a/kernel/locking/mutex.h b/kernel/locking/mutex.h index cbff35b9b7ae3..2e8080a9bee37 100644 --- a/kernel/locking/mutex.h +++ b/kernel/locking/mutex.h @@ -6,7 +6,7 @@ * * Copyright (C) 2004, 2005, 2006 Red Hat, Inc., Ingo Molnar */ - +#ifndef CONFIG_PREEMPT_RT /* * This is the control structure for tasks blocked on mutex, which resides * on the blocked task's kernel stack: @@ -70,3 +70,4 @@ extern void debug_mutex_init(struct mutex *lock, const ch= ar *name, # define debug_mutex_unlock(lock) do { } while (0) # define debug_mutex_init(lock, name, key) do { } while (0) #endif /* !CONFIG_DEBUG_MUTEXES */ +#endif /* CONFIG_PREEMPT_RT */ diff --git a/kernel/sched/core.c b/kernel/sched/core.c index 98a802e0a3f36..1c7573d0cd312 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -96,6 +96,7 @@ #include "../workqueue_internal.h" #include "../../io_uring/io-wq.h" #include "../smpboot.h" +#include "../locking/mutex.h" =20 EXPORT_TRACEPOINT_SYMBOL_GPL(ipi_send_cpu); EXPORT_TRACEPOINT_SYMBOL_GPL(ipi_send_cpumask); @@ -2965,8 +2966,15 @@ static int affine_move_task(struct rq *rq, struct ta= sk_struct *p, struct rq_flag struct set_affinity_pending my_pending =3D { }, *pending =3D NULL; bool stop_pending, complete =3D false; =20 - /* Can the task run on the task's current CPU? If so, we're done */ - if (cpumask_test_cpu(task_cpu(p), &p->cpus_mask)) { + /* + * Can the task run on the task's current CPU? If so, we're done + * + * We are also done if the task is the current donor, boosting a lock- + * holding proxy, (and potentially has been migrated outside its + * current or previous affinity mask) + */ + if (cpumask_test_cpu(task_cpu(p), &p->cpus_mask) || + (task_current_donor(rq, p) && !task_current(rq, p))) { struct task_struct *push_task =3D NULL; =20 if ((flags & SCA_MIGRATE_ENABLE) && @@ -6695,36 +6703,124 @@ static struct task_struct *proxy_deactivate(struct= rq *rq, struct task_struct *d } =20 /* - * Initial simple sketch that just deactivates the blocked task - * chosen by pick_next_task() so we can then pick something that - * isn't blocked. + * Find runnable lock owner to proxy for mutex blocked donor + * + * Follow the blocked-on relation: + * task->blocked_on -> mutex->owner -> task... + * + * Lock order: + * + * p->pi_lock + * rq->lock + * mutex->wait_lock + * + * Returns the task that is going to be used as execution context (the one + * that is actually going to be run on cpu_of(rq)). */ static struct task_struct * find_proxy_task(struct rq *rq, struct task_struct *donor, struct rq_flags = *rf) { + struct task_struct *owner =3D NULL; + int this_cpu =3D cpu_of(rq); + struct task_struct *p; struct mutex *mutex; =20 - mutex =3D donor->blocked_on; - /* Something changed in the chain, so pick again */ - if (!mutex) - return NULL; - /* - * By taking mutex->wait_lock we hold off concurrent mutex_unlock() - * and ensure @owner sticks around. - */ - guard(raw_spinlock)(&mutex->wait_lock); + /* Follow blocked_on chain. */ + for (p =3D donor; task_is_blocked(p); p =3D owner) { + mutex =3D p->blocked_on; + /* Something changed in the chain, so pick again */ + if (!mutex) + return NULL; + /* + * By taking mutex->wait_lock we hold off concurrent mutex_unlock() + * and ensure @owner sticks around. + */ + guard(raw_spinlock)(&mutex->wait_lock); =20 - /* Check again that donor is blocked with blocked_lock held */ - if (!task_is_blocked(donor) || mutex !=3D __get_task_blocked_on(donor)) { + /* Check again that p is blocked with wait_lock held */ + if (mutex !=3D __get_task_blocked_on(p)) { + /* + * Something changed in the blocked_on chain and + * we don't know if only at this level. So, let's + * just bail out completely and let __schedule() + * figure things out (pick_again loop). + */ + return NULL; + } + + owner =3D __mutex_owner(mutex); + if (!owner) { + __clear_task_blocked_on(p, mutex); + return p; + } + + if (!READ_ONCE(owner->on_rq) || owner->se.sched_delayed) { + /* XXX Don't handle blocked owners/delayed dequeue yet */ + return proxy_deactivate(rq, donor); + } + + if (task_cpu(owner) !=3D this_cpu) { + /* XXX Don't handle migrations yet */ + return proxy_deactivate(rq, donor); + } + + if (task_on_rq_migrating(owner)) { + /* + * One of the chain of mutex owners is currently migrating to this + * CPU, but has not yet been enqueued because we are holding the + * rq lock. As a simple solution, just schedule rq->idle to give + * the migration a chance to complete. Much like the migrate_task + * case we should end up back in find_proxy_task(), this time + * hopefully with all relevant tasks already enqueued. + */ + return proxy_resched_idle(rq); + } + + /* + * Its possible to race where after we check owner->on_rq + * but before we check (owner_cpu !=3D this_cpu) that the + * task on another cpu was migrated back to this cpu. In + * that case it could slip by our checks. So double check + * we are still on this cpu and not migrating. If we get + * inconsistent results, try again. + */ + if (!task_on_rq_queued(owner) || task_cpu(owner) !=3D this_cpu) + return NULL; + + if (owner =3D=3D p) { + /* + * It's possible we interleave with mutex_unlock like: + * + * lock(&rq->lock); + * find_proxy_task() + * mutex_unlock() + * lock(&wait_lock); + * donor(owner) =3D current->blocked_donor; + * unlock(&wait_lock); + * + * wake_up_q(); + * ... + * ttwu_runnable() + * __task_rq_lock() + * lock(&wait_lock); + * owner =3D=3D p + * + * Which leaves us to finish the ttwu_runnable() and make it go. + * + * So schedule rq->idle so that ttwu_runnable() can get the rq + * lock and mark owner as running. + */ + return proxy_resched_idle(rq); + } /* - * Something changed in the blocked_on chain and - * we don't know if only at this level. So, let's - * just bail out completely and let __schedule() - * figure things out (pick_again loop). + * OK, now we're absolutely sure @owner is on this + * rq, therefore holding @rq->lock is sufficient to + * guarantee its existence, as per ttwu_remote(). */ - return NULL; /* do pick_next_task() again */ } - return proxy_deactivate(rq, donor); + + WARN_ON_ONCE(owner && !owner->on_rq); + return owner; } #else /* SCHED_PROXY_EXEC */ static struct task_struct * @@ -6876,10 +6972,13 @@ static void __sched notrace __schedule(int sched_mo= de) next =3D find_proxy_task(rq, next, &rf); if (!next) goto pick_again; + if (next =3D=3D rq->idle) + goto keep_resched; } picked: clear_tsk_need_resched(prev); clear_preempt_need_resched(); +keep_resched: rq->last_seen_need_resched_ns =3D 0; =20 is_switch =3D prev !=3D next; diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c index c34e0891193a7..ea4331ed6bc23 100644 --- a/kernel/sched/fair.c +++ b/kernel/sched/fair.c @@ -9415,7 +9415,8 @@ int can_migrate_task(struct task_struct *p, struct lb= _env *env) * 2) throttled_lb_pair, or * 3) cannot be migrated to this CPU due to cpus_ptr, or * 4) running (obviously), or - * 5) are cache-hot on their current CPU. + * 5) are cache-hot on their current CPU or + * 6) are blocked on mutexes (if SCHED_PROXY_EXEC is enabled) */ if ((p->se.sched_delayed) && (env->migration_type !=3D migrate_load)) return 0; @@ -9437,6 +9438,9 @@ int can_migrate_task(struct task_struct *p, struct lb= _env *env) if (kthread_is_per_cpu(p)) return 0; =20 + if (task_is_blocked(p)) + return 0; + if (!cpumask_test_cpu(env->dst_cpu, p->cpus_ptr)) { int cpu; =20 @@ -9472,7 +9476,8 @@ int can_migrate_task(struct task_struct *p, struct lb= _env *env) /* Record that we found at least one task that could run on dst_cpu */ env->flags &=3D ~LBF_ALL_PINNED; =20 - if (task_on_cpu(env->src_rq, p)) { + if (task_on_cpu(env->src_rq, p) || + task_current_donor(env->src_rq, p)) { schedstat_inc(p->stats.nr_failed_migrations_running); return 0; } @@ -9515,6 +9520,8 @@ static void detach_task(struct task_struct *p, struct= lb_env *env) schedstat_inc(env->sd->lb_hot_gained[env->idle]); schedstat_inc(p->stats.nr_forced_migrations); } + WARN_ON(task_current(env->src_rq, p)); + WARN_ON(task_current_donor(env->src_rq, p)); =20 deactivate_task(env->src_rq, p, DEQUEUE_NOCLOCK); set_task_cpu(p, env->dst_cpu); --=20 2.50.0.727.gbf7dc18ff4-goog