From nobody Tue Oct 7 07:07:36 2025 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.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 D522C136352 for ; Sat, 12 Jul 2025 03:34:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752291257; cv=none; b=fBkEUmtaJhA64vcX586EfkHCrbI41Iz6DYpucbWwOP8+Zd+oA4HeLnOu1+9XzxcDogyNJ7/dXAzF7/W8o+IVFmyIQXXUqjjF3lwnRjsxedhv2NzK/M7TwG2g9mKLGB0QCG0qEfdZE2NDf09bDTvKc63vke7/jvFRDIQpc6YRr94= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752291257; c=relaxed/simple; bh=VWgHxyi7LDDBO4Gn93TVM5lfgdvjoH0TRNH9C5pGwss=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=onQ7NEWXpUOjL78f1T1+Q5Np1PBIxqexrayKOCDDVmiGaIct3fdTGkmgwIg6H5IwZK00r+Vd8jieFL7KZluP45fGI9w+f41Y43eyku19PL4KZFutTp8xJeo0+XS+KV2p5KaiqPBw0iqT4B0Pu6UsvervMHBkTmrM8qE1BrL6vU8= 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=tYGf6GYA; arc=none smtp.client-ip=209.85.214.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="tYGf6GYA" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-23536f7c2d7so42005205ad.2 for ; Fri, 11 Jul 2025 20:34:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1752291255; x=1752896055; 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=ivjBPFJqY0Lz+cvcJfjKk19gbOyQS173V3105eKSA3c=; b=tYGf6GYA86poNT1Gj69D1jv+QeAMIJOYVo9ZWnM2jR2I7B42yg1jFrrJoErSAn4Iko hu/AgIpjabOnM5/WmJD9o9nL3Yfwvi4bUOoclfSCSjx29Xwn9BTdBSjb8UzuVpEN2j1t 72XtmzFZE2LdRb+Sz4hZJcmVbrJI6PzqFFzg1T/V29rxRiRiqujrpm4RnBxva5BEfL4M 8cjaGtjzdb+JtV6oRw18QrD0RV/OUGqVeXhywiurvLNQKt6XJ+s/9w2C4RkUrABzUS1j MMISITc0/NuDnoIlo5lN0LQsx36aFABAVISYBnDOIK5/QL5QdP/FCasM/MMstdcm23l1 L7yQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1752291255; x=1752896055; 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=ivjBPFJqY0Lz+cvcJfjKk19gbOyQS173V3105eKSA3c=; b=tlU8xYk/dkL8EaXIn67o8SFKQw9/h7iBiYiuhdEzD9wsRzGQyBkV+8QaWGJmFbT9iz /DdU2kcJa8i8uZh1gDeiEyTtwbTyyJ4gziHfeDYbgnfUz0lvomGBBXYuYiRyA/EEpZrX K2pZjE4c7I8eGl4ZVRMKP6Uco+C45PXB8UAs/I7LIXqShR64rizN+Zn1qhOcVs5PngYR i8wX1askQsAW2r5Xz2yxrhzGAK0WYBWZTgavoZU4pfSljN8DPPUwZ+5ieO7SDGdrGI6p gjRhR+mXxf0T589K12QHq9FmDednfdHgFqYzAAz/1J6XVXWYJkzLHfQEpPgaWsOlun/B YpjA== X-Gm-Message-State: AOJu0YzfHLQ7C+4LzNc1f45PpAllo6ByHAD5CZbWU7Kkey2zDJjQc7mU Q08LcvXbZOOxhlEMeD6ELUrAdCbZiDfSYwEi+EXlrGmPrj5rgZh/v/wXPxJ1u3I3KfQefu+vivb wZC+xz9XJFvwQjiPw64/+kBzvSDJlwy2C8fEnGk3sA4p7h9dUhUvJKEOoWd9QadCIeZKiaAFOb7 dgGIDTqdiMO4cwnIIlU9ictK/praAvv/OiqIdA9AXzZLxODWQZ X-Google-Smtp-Source: AGHT+IEzvGypPpwTdLR61tX5Qreu93vLW0bjmeM1IQ2Kfy1C9LpbI714yZ0CwfmQcI6QWmkA0AWo9I14suZf X-Received: from pjvf13.prod.google.com ([2002:a17:90a:da8d:b0:314:3153:5650]) (user=jstultz job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:dacd:b0:23d:dcf5:47e1 with SMTP id d9443c01a7336-23dee237ff0mr70391925ad.31.1752291255055; Fri, 11 Jul 2025 20:34:15 -0700 (PDT) Date: Sat, 12 Jul 2025 03:33:42 +0000 In-Reply-To: <20250712033407.2383110-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: <20250712033407.2383110-1-jstultz@google.com> X-Mailer: git-send-email 2.50.0.727.gbf7dc18ff4-goog Message-ID: <20250712033407.2383110-2-jstultz@google.com> Subject: [PATCH v19 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 --- 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 666783eb50abd..5c2b036c85a9d 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 ec68fc686bd74..45daa0e5b27d3 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 07:07:36 2025 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (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 41FA31EF38E for ; Sat, 12 Jul 2025 03:34:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752291259; cv=none; b=hztnh1FTbUIPhcq4vpXh4N/xJRmCHbFyUg8kcoZf+OXe2pXrgzQaH/T+zMFd+TR9WrO5UK8V+6Z9hl0bVewRACV18qlzwNyQvCHwUGeEsE3SgAT6XqwyXG8I/p2OfVGqB009KXuaCqzx4gutQO4nNPhTSc2ALSXrrEu/O35IJDQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752291259; c=relaxed/simple; bh=uOykFACp3NbUBmPSMCaDEu9zlSnIbC8VBbKQTciMjpM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=HMvyPiVfKHo2SNpE+mCPmM0WyV6ax2if9RyhIJNqL+VA8iCimmuaAJiIxT1hsr4JaxmMNm/EA6SCmXrF+PzdvS8SxvwEjZr+/YlkKq9av7VTNrzE+ZdQFdF48w9IvV1R2o8wIOx5CGTMtdGIbi26Bcu3bEQ2Yx3mts00aPGc5Zc= 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=1BaQnH+7; arc=none smtp.client-ip=209.85.216.73 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="1BaQnH+7" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-3141f9ce4e2so4221193a91.1 for ; Fri, 11 Jul 2025 20:34:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1752291257; x=1752896057; 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=RcsDbgmCNCPDTwLfou023Ud0plRNGaZ+zadqPA7d6qM=; b=1BaQnH+77hr3rkr8/FxZtZwTL+vy4IOysif4j7mxZPAODoEQsEU1wDsaCop3Pey1dX Sh4U83v8lmuVjAIXyWHm8rZMLMw/XYJuAYGUF/mwfr9eQyzrVsqEBr08sjhs5HSBCnQa eqgg37S99w4xejJ3og6zRFcLH79xpYejQ+6NzTnWJjXIhV5AdN+C6Mn5EHoq4d3LZqVa nAtfN++RqJ/YbcVP6WEXnonwaRu8JKi/jcxi/JEiWn3raYIpfheBQwVT352EkA9Xb2Hq tqG6swYGp5vgFvap+a1gTbr0uhX31TRqL9LfpvmJBE3nJu1dAgwUjGHGahq/EUJBLGsI P3pA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1752291257; x=1752896057; 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=RcsDbgmCNCPDTwLfou023Ud0plRNGaZ+zadqPA7d6qM=; b=aIgfcI93Fgiy3CDQmYWt1+mQ/+NgRbkjgZunPLPiucLSkcdm4MGfZyEQpmnCJpOzLk tn5XUw0/lQnP/sVVb3VKReiVgqsdOU2ttgHeNb0je38NvxK9UNKvddKBkC7zsfgIY/IF TV06DN3v6kuaevt4RL3KFVnJrIhxCxqgM7hNVFcg8/11/C9RHT8rTqczT78IlqJL81dg EAshb77JTDXXBMnB4G9AbwEBpO8+ulACFz1GOsuuNCjMqOntR4y3Ah/D2SzSDK7n7Emu UIYolnzKWWKYO+Nhi6FzJj9IQ/dZNOf6QDKzVLK2w4lNAoY7U0hc4HmGyg3EFdp0YxbB wr/w== X-Gm-Message-State: AOJu0Yz3MwVGJcwxdYlW+4EY/U6Wmo7CqyNfAS4hJ8iESQB7DqdS8ns4 wZQpS7eUBu3Jvzq1oCldvtXzfJetkjvZhLQf5orKlnwYthC5N7nY90E0zVMGhiHSYnzFA66m1yH 4usxdH0gkIFOTkNGJsWGinJU2OXgLDFx4mSxHA4t3HdWUvy10wx4F17/r6+2YfxR8GL8fHHK+c8 8NaAuxDwSWMLm77qRz8bBAQ658V4pmUd6EJYdUWisG42cOWCYn X-Google-Smtp-Source: AGHT+IHzIBXtohB8qukap9wqMJzeQbi2viJq7t4aockOpxtbi7Ph7vlyq29vA1m/lTtwhH9n6G136WYREuKy X-Received: from pjbqo12.prod.google.com ([2002:a17:90b:3dcc:b0:312:ea08:fa64]) (user=jstultz job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:48cf:b0:31c:260e:55e9 with SMTP id 98e67ed59e1d1-31c4f591cebmr7852591a91.24.1752291256255; Fri, 11 Jul 2025 20:34:16 -0700 (PDT) Date: Sat, 12 Jul 2025 03:33:43 +0000 In-Reply-To: <20250712033407.2383110-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: <20250712033407.2383110-1-jstultz@google.com> X-Mailer: git-send-email 2.50.0.727.gbf7dc18ff4-goog Message-ID: <20250712033407.2383110-3-jstultz@google.com> Subject: [PATCH v19 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 --- 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 v19: * Build fixup for PREEMPT_RT suggested 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 --- 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..45fe05e51db18 100644 --- a/kernel/locking/ww_mutex.h +++ b/kernel/locking/ww_mutex.h @@ -283,7 +283,15 @@ __ww_mutex_die(struct MUTEX *lock, struct MUTEX_WAITER= *waiter, if (waiter->ww_ctx->acquired > 0 && __ww_ctx_less(waiter->ww_ctx, ww_ctx)= ) { #ifndef WW_RT debug_mutex_wake_waiter(lock, waiter); + /* + * 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); #endif + 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 07:07:36 2025 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (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 5F0361F418B for ; Sat, 12 Jul 2025 03:34:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752291261; cv=none; b=fzUb7vQw11s6BM8zB+Hl7n96ULsLUlJ7SFJTgoPNAluVI3oWwB/A8rzc4+B7H/Wg3k0eC0aXbhD0OXgcSBD/F2PCq/R5KPDyxneUsDFUoX23tEDYu/X0fXIHD3495b65opAHh3GaXLLmX5+eNjRAhJcMoZv9tP5zPsTcvtjKdp8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752291261; c=relaxed/simple; bh=2nPjTJu8eSyI/g8kurX6AAHLKzbj+e9wpsH8hqHA4yQ=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=n2ebHc2L9kR3diWtawD/FPCeMX+4ZN9iXf3VTISmuuAWW/ma7OMgSuvqgYCzk8TqU4Iz6OzdUhrcGm6VYh+Y8LLsFSyjB5sAZ7oO8+mRCSkd1W9Y9hrDSWZe4qyz/GcBaxznReCFCIDJUyw/pfceWhZKa2kjPw/y9eu44TYJE/M= 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=ysYifSfu; arc=none smtp.client-ip=209.85.216.73 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="ysYifSfu" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-31366819969so2789259a91.0 for ; Fri, 11 Jul 2025 20:34:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1752291258; x=1752896058; 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=rADpeiK38k62j2Z07JxedtjCmAXRqeaTKkWyWraJECg=; b=ysYifSfunq147a4Dxz20um7ltAkf8gq4JBqA6BvyNX1gJbjq3GWvrBkDEnsknosCjS hXOajkTvR/Or2wXn3mV5pHXzxvjSxKzEmWhqjqMrpfgiBMcisWMtEs0F8fE3aRXlcrMn azIWOfbUOX/dlLXM9lHnafNkAsZvtqtJJz9HGIk6/ESdP29q3EUjXVFf7dGZH/TVkoZ+ rsH+rtUgbUBWYfg8ahifqLSZLHQ5fKOMOeuwgv0UgpXNIgeX6kZ4h402QsXpW5NwCemx z6PrIAg13bkIIswg6ll0zIELbo2xbbWzUo2+2J/gacre6oWobPlassQBIlmPvbTuR6/k OltA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1752291258; x=1752896058; 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=rADpeiK38k62j2Z07JxedtjCmAXRqeaTKkWyWraJECg=; b=Z4NAZErMjbKikrtvCfV46tmYr69Fz9I3MNk5oZ9Fgnlgz+ZAIER4r1r1Nbvny47W2L efrAK0K87hTycX/cHKxUMeZ9J8HyE0kqnGi8oFcd1eY6IZCKqRcirBCFQPXURF2TEas2 OX4074NskhAIfR78/Qzam/hJvoI+zg6h6TxY4p+mCW7NP5pfs1bkYw6zkj8bQVaD+kCX uaQlEGmZV0mUYqq0TEAbS3td0FAbOIhBnX+4owoSapTnUFZ4daA7KuGbpJMzUML0YpYp A953+GsIS5D0VUmPJoq6sbJdagZztlK0O3FaSuzlaxjCJ8RsIpDml1EkX8Hbeo9AxHTu gMAw== X-Gm-Message-State: AOJu0YxhW0HNL9D1zica40oOZzcVdHfy51AYeKADYMVBe6PZdfX8efTQ uoJofv+Nvvlize5pIj2ZKHcVWFgx2748L9sg8/Ut7bG/aH7dfFJMoJ0PCN1TOYE2wnMWWr60f8P 8vjyi2H3ZPbW5akKbs2Z75bwYFTE9zB8KQVdvxTdWBPlqirWU0kE/m0eKtapRZb9nwp8TtHvyIn kWpVlQNQvm5s79FYPm8Cz1SrGiRdFC+YIC12+N4Y8HE/vWxwBd X-Google-Smtp-Source: AGHT+IHC4vHzOkZXP7DZJpTAxmQ+h6ghQXTW6mkQUH74GTUPdYbq2Eh+xc5XQaAb9NVOlo2Dm/n2s0RqqsvX X-Received: from pjf14.prod.google.com ([2002:a17:90b:3f0e:b0:31c:160d:e3be]) (user=jstultz job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:2b88:b0:301:9f62:a944 with SMTP id 98e67ed59e1d1-31c50e4661cmr7157525a91.33.1752291257794; Fri, 11 Jul 2025 20:34:17 -0700 (PDT) Date: Sat, 12 Jul 2025 03:33:44 +0000 In-Reply-To: <20250712033407.2383110-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: <20250712033407.2383110-1-jstultz@google.com> X-Mailer: git-send-email 2.50.0.727.gbf7dc18ff4-goog Message-ID: <20250712033407.2383110-4-jstultz@google.com> Subject: [PATCH v19 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 --- 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 v19: * PREEMPT_RT build fixes suggested by K Prateek Nayak * Slight re-arrangement of functions to clean up later patches 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 | 64 ++++++++++++++++++++++++++++++++++-- kernel/locking/mutex-debug.c | 4 +-- kernel/locking/mutex.c | 32 ++++++++---------- kernel/locking/ww_mutex.h | 8 ++--- 4 files changed, 81 insertions(+), 27 deletions(-) diff --git a/include/linux/sched.h b/include/linux/sched.h index aa7966141a090..675f971b18e99 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,67 @@ extern int __cond_resched_rwlock_write(rwlock_t *loc= k); __cond_resched_rwlock_write(lock); \ }) =20 +#ifndef CONFIG_PREEMPT_RT +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 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); +} +#else +static inline void __clear_task_blocked_on(struct task_struct *p, struct r= t_mutex *m) +{ +} + +static inline void clear_task_blocked_on(struct task_struct *p, struct rt_= mutex *m) +{ +} +#endif /* !CONFIG_PREEMPT_RT */ + static __always_inline bool need_resched(void) { return unlikely(tif_need_resched()); @@ -2216,8 +2278,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 45fe05e51db18..086fd5487ca77 100644 --- a/kernel/locking/ww_mutex.h +++ b/kernel/locking/ww_mutex.h @@ -283,15 +283,13 @@ __ww_mutex_die(struct MUTEX *lock, struct MUTEX_WAITE= R *waiter, if (waiter->ww_ctx->acquired > 0 && __ww_ctx_less(waiter->ww_ctx, ww_ctx)= ) { #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); -#endif - 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 07:07:36 2025 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (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 4B2801F7575 for ; Sat, 12 Jul 2025 03:34:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752291262; cv=none; b=XzoCV+wG1rrZYtgPpz2TxqUDtc8ujefwc5dqiKiooDIbBGBIilMIOwWmh1dfjfCG1lcyRWRGICEZLrY8zD1PKaCnPPin3M6DdkQQMTKjJMmjm0CIo73ZL9tC/Gt8b/e02T7SEQLw4TkV4D7nsyjBF9xk5xZCUgoeNPJbwTUgOxM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752291262; c=relaxed/simple; bh=E9BRbJXMe0/nYQ1bgCddiiXCeZLlpdEjEZgz4SOKuY4=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=hG8rSC8QBzQsNCQv+8nbZLy8TysNci3k+Epi8p0t+Kae3lN/JhfDe3OPXRAjIur7BgFSfYLiZv+7YcLc6vtF6KhI/vf0tilJ2BMFpO9B8mNYU9w/DgHrirYGKLgz8wc5CxLFbwaEs/1KgeMSCo0qiv3ifr1JmLLUny3FrL3EnlE= 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=u4X20kRN; arc=none smtp.client-ip=209.85.216.73 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="u4X20kRN" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-313f702d37fso2852240a91.3 for ; Fri, 11 Jul 2025 20:34:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1752291259; x=1752896059; 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=u4X20kRNQWlOQ0Ho1ORe5pgH6mulrRztHuJ8P4CjVM7S8HQmQxqUrtXWQA/HJwHx0Z CClohshgvdaTnYzjN7QOxlZewnovaGeJbwPKUmT50FK1nAxVAF2e+3rRZreXnqpFUuGX kbi9AkdRIPgUYKV0sEOxn1U33k03FXXXpEwH3+6hLG8yCSBrRQ4p57lxG3IHF17c1I3I b5SIuxX13cSP0D1jMcroiQlc/IufyEZdt64SdgB/tzXd8VQe2MJ3XkguFmkZT9jek53r n4dOwT1o5lFPU/CqNjYDM2KDALHhDOLNT3Ps8N6pb5J0pJ2UUxVV3hwDe2mxrMvcT27H 8AOA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1752291259; x=1752896059; 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=SjT0tec1I+q6HSFD3W3BcL+gEZcytPpRSRcB1wO4A1Zrmwybbrug/m0PCk9Mdw8YmT KnRTGp28FigtJKdKkgVJNLGBJvDBms/ZpOi0jdw7GorcnWDIr/pVsPLaCzIC+KAFkq8S oayowdYsNgEp1rr0VOivYpDgpNQHqEgGo2+3bQue5SnA1GepTHqwH14hj4K4g7Yp1r14 /jnea3tFSCFPnNvNmMz30kU2Lh/nUwpC+6+YI3BVGjikJfFGWjDGSInbQfp3ODB5Pr9M SWNg8JTUEpQ/Mb29TmznS/MLOVT1IB4Oo+UwlT3fqNUGkf4lAWs3DEPkJN2URZ1PNNIj B2oQ== X-Gm-Message-State: AOJu0YziKbT4pYcGa1XtRnffvNK691Kvc1Ts5AD5Y93/XzMRId2IKCZJ UOpjcYq4r1PHzeQ8TqDkq5sB4Ymn6S8w55ErKIdCcE0BXS5v364XCl6CPpV5CvcgNcaaHg3Zc6j udpac+mXmTCM/Gd8VWNgRVvQh0s+MLObYdsVPXXk+8D4pxTP/4h+ooeDxCekDn9dEvylNM/T5eF TQhbOQ6Ewwtr5a9INBpea3+BDH9Q7iIATqmUqvndF7ymS5I1yR X-Google-Smtp-Source: AGHT+IHA6gpxwJ68POuktr0YtPoE3QpMcvDqvrNo/5dRbr0PMMwnya6OZ7i4kbzGZRNpBuM9BofZQTDp2zsM X-Received: from pjbqx4.prod.google.com ([2002:a17:90b:3e44:b0:315:f140:91a5]) (user=jstultz job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:3d89:b0:312:f650:c795 with SMTP id 98e67ed59e1d1-31c4f512ab0mr6831733a91.21.1752291259457; Fri, 11 Jul 2025 20:34:19 -0700 (PDT) Date: Sat, 12 Jul 2025 03:33:45 +0000 In-Reply-To: <20250712033407.2383110-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: <20250712033407.2383110-1-jstultz@google.com> X-Mailer: git-send-email 2.50.0.727.gbf7dc18ff4-goog Message-ID: <20250712033407.2383110-5-jstultz@google.com> Subject: [PATCH v19 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 --- 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 07:07:36 2025 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (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 9F9DB1FCCF8 for ; Sat, 12 Jul 2025 03:34:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752291263; cv=none; b=rj/0lihvJaaXILmySVhvyQ8xrCaR6eXcqMIiXr63BGpgQht3U7VWrjNarB6pE+icymvwh//pUec8IYevREPRrW8bg8jBsO8AXItfGahz310wFilYkJYzcleoe9KbeOmPHZ1gIQ81T9xEl+nlzLr/eX7/+aQrk1/rNCd+PmEA1FE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752291263; c=relaxed/simple; bh=w/5Z05JWQxDCXH3VP+Bseb+TW5gj6HkFGljwFSWGQl0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=QHTWSvCQSZZnzw9+L2us2Yg4Aof/ePwa1uOLsKBFGgyH9+2iQXW3ke2xG9qHF1CqyEfyfkqylDoXhiOusnJziKvTa7kucZrsc1WQT1LyVjfQMDLcxsPO9vBRbZH2ivAJbETdA33XeKz2q2b6mgM4gLBq6Ms5PiN/2FJpUkkCre8= 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=2QXmz++u; arc=none smtp.client-ip=209.85.216.73 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="2QXmz++u" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-31202bbaafaso2810851a91.1 for ; Fri, 11 Jul 2025 20:34:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1752291261; x=1752896061; 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=92EYSjhNLkWOuubLSKggwNqEDGryRbhYkmkTsTaH1Kw=; b=2QXmz++utp15+mP6G4lQXuHnSdPHS5KlTHwvBxocQbArbgjH+xb+SLilH2Wih89+P5 vNxBFpgnzbAjPzTWPGPqhX1GkXrl/nxqpjRiGq7UR+Nou14qTYUadFkofQ8WkBAuy0i0 qkDDzNyxw4DMwWE/pN3Ryn/rQPMSAEBbC5A/G/QMhxRsNStF9BhdfJw7GwhIhouyzctm 0lQ+cAO87asmgVxioC+CsFdCTEKvln36bj2NO2o64EMLOlQwuchBEjT+jpXNZFApUq20 PsFHNCew2D2rR3HSxJp1DF2ockAcnwoZLUsGIJaOBijKgfHOIXi+apoGMgjcYQxV1r4Q uidQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1752291261; x=1752896061; 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=92EYSjhNLkWOuubLSKggwNqEDGryRbhYkmkTsTaH1Kw=; b=qZyE2Ml0S7k3EIbvSYDWuL91UJNw5oygQqQOgoCogOZ5bHrnPSFEOvapuPkrAV6PI1 mHlY5lvwSQEjI++CmHgRaXXj2P0oTjaKA31RxzvjktRcb+9Or2OOULlX+51GhF9JhSb2 DMXZw25ptzUvhqC1sNwLeRjn7mR9vOD9HusJlfBxfK5ZBnsRw7oFVwgQZ3zZbwFrLftu vOqQIkRlt3DBptasb/kdCZ9hEZSXCn/vhbKGtUtjWE1NmaDb9uK+i3TNuGymoYRG6bI0 FLO3WANA6J76NykjtskbrW7tYEKa1smWqVbP63f3LGURbvohgI1+Z+Mlv4rAK/lVkwsq fkKA== X-Gm-Message-State: AOJu0Yw8LCth0dyO/N3Wi8ywvanLBse0Fh8sMkEUfF2A6PPyHjcC44q/ xEsMQohtY9DTGtI3Wl5wf6j8W+w1tsAu8OH2L8Hs7X8TP1TE4h5iAmLEApnD7EuV9CUMtVgbC6n dkAu6EVCSpKscDFyUV7tjm8Bxqpm363ok/0eHwpY1azXv0/qfE6ooCRfE9zJgQBYhgbmWA07ka+ tK/ZWOG6Rw60kTtTZUkBtwZq8eKYBXKH4UOAXVdonndKLFKknc X-Google-Smtp-Source: AGHT+IEyCWYlpG1/Tgz+vYCZ/3nxG4C07Dgg7oG1e2xNdH6yULt54Lsf7nmd7r2hC2LmLQXfahDXdtiPBXOG X-Received: from pjm3.prod.google.com ([2002:a17:90b:2fc3:b0:311:8076:14f1]) (user=jstultz job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:5830:b0:311:df4b:4b7a with SMTP id 98e67ed59e1d1-31c4f563387mr6124589a91.29.1752291261004; Fri, 11 Jul 2025 20:34:21 -0700 (PDT) Date: Sat, 12 Jul 2025 03:33:46 +0000 In-Reply-To: <20250712033407.2383110-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: <20250712033407.2383110-1-jstultz@google.com> X-Mailer: git-send-email 2.50.0.727.gbf7dc18ff4-goog Message-ID: <20250712033407.2383110-6-jstultz@google.com> Subject: [PATCH v19 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 --- 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. v19: * Slight tweak further simplifying update_curr_common() 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/fair.c | 42 ++++++++++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c index edcc7d59ecc3b..068002fe15dd8 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)); } @@ -1211,9 +1221,7 @@ static inline bool do_preempt_short(struct cfs_rq *cf= s_rq, */ s64 update_curr_common(struct rq *rq) { - struct task_struct *donor =3D rq->donor; - - return update_curr_se(rq, &donor->se); + return update_se(rq, &rq->donor->se); } =20 /* @@ -1221,6 +1229,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 +1243,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 07:07:36 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 264B81EE035 for ; Sat, 12 Jul 2025 03:34:23 +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=1752291265; cv=none; b=i13VCJVwDLByLFeYfW837K6FN90wy7cNmhSifKtwvSThTvwnmbjhLJXtAxt+gPqEwZI0xJNaW33NhsSMM2oPmC/FeQDREG5WfR7RbakifBdfgdhQ7Xw8QFdCOAGqUmsmHeypU/HtpCTUqSFeMFHgdcWKl3z7DXZGYPkAROFmKv0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752291265; c=relaxed/simple; bh=pQLEqGv10drwEZ00T5QPwAmL48sm4LEDQUpwgsNdmhc=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=SqQRE4sEL9sVjJ6cSZHhJ+QIcS5OBTTY8ckMMRRS/z1hRrphKwv0x4M/fP2RBYMB8cQE9UvDO2N3A8kPRaruv+hlIuMktFivL5PTSPTgK+rAJ8bi9P463qcwbiDK98oE4Pnkhf2d3bNVSmkB+JmMhZCLtvw9nifMTp15Ewp1B0g= 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=da4CHAN1; 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="da4CHAN1" Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-b362d101243so2259994a12.0 for ; Fri, 11 Jul 2025 20:34:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1752291262; x=1752896062; 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=aoruQA2UpeX5unKgudWbiIQrcP/pcmo3uYDj7LS2F2c=; b=da4CHAN1F3Hi0txy1+hh5gWSrywm/HksLQA977LBFrDKAk1dNnFA9JIeWHI0TQAtJU bukCpL/iJDV/xAaQcHainflP4YGL1Qbd8WQ8cqrwQJdNQCvr1ah08yu5/qGVXZPFBLUa JlA+CCVOiqLvy42Db5qoeeuJKZaIxYp5YdpBdzl68xOtTQbkb30Rx7lUiSYykjtbFWyI s1rLwTAElx2K+07rWQRyLkv+JMtZOrAlg+ZfpSmTj8fUR5UA8zTKH4s9jS7nenK08bim 7QBWvadGNSRsj5wSRTEJCbOelzRX7ck6v7wGYGMMCdqRdpMXBlNw5g3Q36lXj0zng9mR SfiQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1752291262; x=1752896062; 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=aoruQA2UpeX5unKgudWbiIQrcP/pcmo3uYDj7LS2F2c=; b=hHaDTivdnRg4fMMCt2h3klK/LIXZ4TZRqfnfMdKrlIGZVyf6+rdNDJWrjzZ8wVleCc nur7pD0Mvf8xn/PB8QUxHPry/pQlFtW8WvQuB1XpVsFfwckLBj9NwFs3zrT0RhKbssyu QjtGsRC+pISIiGowY65nFPqJRG0No0AQyHwfAXuvmM1dqSitBaYrc/52lE68p0dOZzoJ dnYiyTUfVILdoSa2v4HWbRRC0VclgnnqItNzoCzhEtOWvKy2bXvXv6tt5UO4SlPhtxB1 jWusO+xXXDVXRxRPyLqMaXeBp+/qW33Pma3N1SG0550VkLHvplYxIRdEvWch26aQRbOi m3Vg== X-Gm-Message-State: AOJu0YylutLmusZYaHX0hAgQp33r+kcuQtOMAWSzPKxZlPFj03fHMo7/ 4u25wH7tkjaA3jrOgxbUMe2Bs7HcSWZ1HanSh/jat415p6Xaqj8wmTAjUNwRUcIDau60BTnsS7d Dtfm5MhfDiNaCL+deTmAwuyHeg0PhLrdW4JvcG385lq8Re59ER+KVIue7Cu4NoUxFVFtml1l+uk 5JXR5plQ+UYVsgQx3GYchx2yMF7yl3em1vGSGtltYKkV5M7c9I X-Google-Smtp-Source: AGHT+IGksVXHKFqWwfnzN/rCh663Qoqe/aig3kHrjsV1J2czoE641NdPYL1bqIPiJSAarSuwNjYenovyWHpc X-Received: from pgbcj11.prod.google.com ([2002:a05:6a02:208b:b0:b39:b9d7:2921]) (user=jstultz job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a21:9206:b0:220:6a52:bfa9 with SMTP id adf61e73a8af0-2313504f65fmr8741809637.11.1752291262304; Fri, 11 Jul 2025 20:34:22 -0700 (PDT) Date: Sat, 12 Jul 2025 03:33:47 +0000 In-Reply-To: <20250712033407.2383110-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: <20250712033407.2383110-1-jstultz@google.com> X-Mailer: git-send-email 2.50.0.727.gbf7dc18ff4-goog Message-ID: <20250712033407.2383110-7-jstultz@google.com> Subject: [PATCH v19 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 --- 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 v19: * Rename try_to_block_task() arguments and add additional comments 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 | 117 +++++++++++++++++++++++++++++++++++++++++-- kernel/sched/sched.h | 10 +++- 2 files changed, 121 insertions(+), 6 deletions(-) diff --git a/kernel/sched/core.c b/kernel/sched/core.c index 45daa0e5b27d3..917043f815335 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -6615,11 +6615,13 @@ pick_next_task(struct rq *rq, struct task_struct *p= rev, struct rq_flags *rf) /* * Helper function for __schedule() * - * If a task does not have signals pending, deactivate it - * Otherwise marks the task's __state as RUNNING + * Tries to deactivate the task, unless the should_block arg + * is false or if a signal is pending. In the case a signal + * is pending, marks the task's __state as RUNNING (and clear + * blocked_on). */ static bool try_to_block_task(struct rq *rq, struct task_struct *p, - unsigned long *task_state_p) + unsigned long *task_state_p, bool should_block) { unsigned long task_state =3D *task_state_p; int flags =3D DEQUEUE_NOCLOCK; @@ -6630,6 +6632,16 @@ static bool try_to_block_task(struct rq *rq, struct = task_struct *p, return false; } =20 + /* + * We check should_block after signal_pending because we + * will want to wake the task in that case. But if + * should_block is false, its likely due to the task being + * blocked on a mutex, and we want to keep it on the runqueue + * to be selectable for proxy-execution. + */ + if (!should_block) + return false; + p->sched_contributes_to_load =3D (task_state & TASK_UNINTERRUPTIBLE) && !(task_state & TASK_NOLOAD) && @@ -6653,6 +6665,88 @@ static bool try_to_block_task(struct rq *rq, struct = task_struct *p, return true; } =20 +#ifdef CONFIG_SCHED_PROXY_EXEC +static inline void 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); +} + +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. * @@ -6765,12 +6859,25 @@ static void __sched notrace __schedule(int sched_mo= de) goto picked; } } else if (!preempt && prev_state) { - try_to_block_task(rq, prev, &prev_state); + /* + * We pass task_is_blocked() as the should_block arg + * in order to keep mutex-blocked tasks on the runqueue + * for slection with proxy-exec (without proxy-exec + * task_is_blocked() will always be false). + */ + 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 07:07:36 2025 Received: from mail-pf1-f201.google.com (mail-pf1-f201.google.com [209.85.210.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 A3CED219A72 for ; Sat, 12 Jul 2025 03:34:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752291266; cv=none; b=qs3WEgUTihyo5/pKc/71kSbNjLO/q4x9hxdDUDYH6vfBI+Ireoa/cCaGNlD/HOaddM7booDYY69JtLdZtlQFm6nVKxgy07TcFXPv5pFgG37EOzagS9RPWCLYRA9SqpBscAl/+gng+MqE+MaT5bsdFss1vHJKDC2o1wvworBbT24= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752291266; c=relaxed/simple; bh=MehgenUigdp3sAgb3bYrAt2AlAJ5FRx+0d1kZs5FhHo=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=NBERr2goxGR3dn8qqWWJUNPCian10hjBTxGg9zmm3sD68tVKIcKuSM7HLiS2lpqgvDzPOsDKA61bT0v3/QED92OHtV0ucz09HONc0Z30JqHfSXC+1+Gs9g5zzSgHvXLzcPrTRKDnQ0nEyScEexd6L9wui8u+sr+QWgvDf5gGmuI= 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=CEwH35ds; arc=none smtp.client-ip=209.85.210.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="CEwH35ds" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-740774348f6so2250900b3a.1 for ; Fri, 11 Jul 2025 20:34:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1752291264; x=1752896064; 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=q6K3DxGTztrWhfpK7W+fWPHaX3cUV0DOD5CR/NRGfQs=; b=CEwH35dsvehxfw0/5aJ6SEw3EU35B3Ap4Ucwf4QDko0VTgHY9tFlT++lpVfvN/FO5E STY8rTPhKyJg3OzrJsKfaqvis34cn1SFdF+LcUKtxf6jJ/ByI1TPJ1leEz6nmAO1y0zO 6UZ0CseKNBEVfzakstaVDk5/Pia2FRXCT2nS6wKY4ewsYBG+zLn5Ho4Ar+33F0ZLeRpj fPQOFmpEa5KZsafk3XQ1W9DCqJRoxR8HMLtj+DUtSASemjsN+H3m7i3N9m0yX3dQtOE3 PWC+duo0b96yLCV8EvzV8KHe2/pFsmEJer/5gcw761EYi0Hosv41sXkdwuCw9Oii6tzA ve/Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1752291264; x=1752896064; 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=q6K3DxGTztrWhfpK7W+fWPHaX3cUV0DOD5CR/NRGfQs=; b=kO29bSMHU8cUsliECG1s52pk8r89QKRNZmwXn99r3wdXny3VtjMS88bLk6tlX/mgW0 V5sT8MmxKQvK+plwF85tcLy0fsKm+IlxmA8U5buupPYx4hYJ+rQ61knYkpdBBGyHIpXs c2rhNidHJp6pmQoQ4x30gOyW/HV0VhgV5Jx8zhZOlsK0pVHD3CCE7lKgVHUTwQILnxva YOrWK1TUCOcvTENJN3qTYYLNUR6UIdFcvRvHiB4H4kDbnGMtAD1+if4I+FRF0DWjhLmL Su01MC2rvyJhcZi4CSh7J40ZPboeYMW2aSOTACfvToD3KaS6OId+cDHcwhp6WnfPoXtd ij1Q== X-Gm-Message-State: AOJu0YyRSw6l4Ey0seqA9dSXKhqdXdieyc/kqDOJXh1zDBcHG0Akq510 zYkxWnO0ag1qUdSNak2dBxY14phVJZnYjxa1ApBjI5OJypnjI3UFKUyhFZ4/ZcZaD6WllgzqHF9 bIvO+vGJALsLRKKch7y+apBOTsy/KSuV2g9HVB0ccvrkodfkzcc1x9fEnJd93lcFqYDlv5C51wR cQKGnfwKBxHi19WKym4gFgsulOxibYiZ7jvIcwwaCEpOhL+Hty X-Google-Smtp-Source: AGHT+IHsOYKcABoOwN/fx5Bf4QB/gk/JWtKxibLqFHSyZPn8gxWaFaGGBukOeFXpQ4Zt8J3hPB7k5sJNyeHf X-Received: from pfbbj10.prod.google.com ([2002:a05:6a00:318a:b0:748:da37:7e37]) (user=jstultz job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:856:b0:748:fcfa:8bd5 with SMTP id d2e1a72fcca58-74ee0bb022amr8522844b3a.3.1752291263833; Fri, 11 Jul 2025 20:34:23 -0700 (PDT) Date: Sat, 12 Jul 2025 03:33:48 +0000 In-Reply-To: <20250712033407.2383110-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: <20250712033407.2383110-1-jstultz@google.com> X-Mailer: git-send-email 2.50.0.727.gbf7dc18ff4-goog Message-ID: <20250712033407.2383110-8-jstultz@google.com> Subject: [PATCH v19 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 --- 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 917043f815335..adb4818ee7f7d 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -6747,6 +6747,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. * @@ -6891,6 +6908,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 @@ -6925,6 +6946,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 89019a1408264..82ec60a713d7f 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 07:07:36 2025 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.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 3455D21FF50 for ; Sat, 12 Jul 2025 03:34:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752291268; cv=none; b=puXq5htyK4PYyZeVTmpisLTwfxGuiFHnpLUYMIJo42KkhZBavHAMl7L4R8gUw8VdAXKn33aEU3uHobygLBj0tq77EEZFKNJ0tDxKLHRWyFE3ow6cTMr1Yh/PqSeNlkvBToz2OgMiMBh+0T10Ls7C8SoWqqNCqp3+KYg8Fx9Ntn8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752291268; c=relaxed/simple; bh=1He4md2PBEaZm22dmRAM4Du69ImTrpl86oEZmUCYsbI=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Ordz7Pj2cphcfwalywJ8ZyS/UxFSz3a81Zd8TGTyqNB1eKJjIHAf9WsSZ/BXaIUhli9QW52pkhjrOggU3Kud0ehkUoiB0PfvjaTX/JDFRC2FlZx3iiNUZfSvUviw8aoiB+ehgC7J4bia/vrB3w3RY7+aaXBAdof5O+wZBbDiF44= 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=wR4ebqBp; arc=none smtp.client-ip=209.85.214.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="wR4ebqBp" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-23692793178so24113925ad.0 for ; Fri, 11 Jul 2025 20:34:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1752291265; x=1752896065; 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=XeGgKboECPvpWcyxHj1mn9UMzK3Dqy5k3OXMc6d+LZU=; b=wR4ebqBp/4YwxHHXI1Z8fj+djG0tkXS82JGPpSIdNuWCEITWxzmQLGDLyflAALGvJ0 Uojmp1WX4HhZt6DNpaovMfwlo2MHh2wabfB8n33qC5DIBXSEeDUqqOa0BuRmwe977MR6 L4jjirUmDr2OjOUaOjSdUzTCWyK1TEope2WAdkvSPx1Y32EVRVdh+4XGdr+FOQHGxxjQ ATGIzcLJRBsP2bzkkz1EMC8w0rfaGoNaD3jdgf3D4SF2NGjets9D1RCg8qJ2bkJIwDpV xFksi/58KgK5oiGjduUO0ZCuzApxRnPq6SyMrHKXvRB6OxJHbcIdPiSvUK9j3CSg2BkA oF2Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1752291265; x=1752896065; 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=XeGgKboECPvpWcyxHj1mn9UMzK3Dqy5k3OXMc6d+LZU=; b=uUnQPwcnTsOqVzvh4ria5O0MIMuGbpro6B3QdH3IQ7GCEy3Pdf/PXy1iXE/rR2P7nH T8Zk6RN56JkaqxsOFh9FvIINGxyNX5f2JRY/J0839jrGfdMglCXJQZB4udcHnnhHkBEl oS8aQIfiwEOa75LrMpzBQJL7F6TEiJZW06jh+5VFWxpg0gPQyIbqtgjoPJerL67Dgrh7 VwL6fHBo9pNPflfUfzvGEoSqqrB/44TtouMinfYHVj/8NDu0C+78ovSlPWCuPEcDE+0l Idylbg2XZcNhDcMOxQ/dGC03NVegyVsHrAWjbY5sizTqEYeR6h+y3z9Z+rdqlmzHDnZ+ fs8g== X-Gm-Message-State: AOJu0YyA/7djyZdWQc12FIIIJTojbAdDLGSUB1dgtk4c/U+CsbaMhA6c yB/K3JIkSED3IxyBLeEov6yEx+fhhuiUQ+ODnwMJ1Zcqt91wZp3QkTTzD64+PFMldx2DFDjokYT ntDtoyxcDiePJAPl9hLGF3E8GLfv4yQ5ykTfFClGDxedmrs8b32EabcVXoXyb7FBTb9GOy4QX4t VOq/VFTtUekFGu/nX608tTuA99Dadf7iNGxC3Lj7M+1QdPzKNW X-Google-Smtp-Source: AGHT+IFHErj3xmug/4poAg/MELCmQ3bB11/QOP4ZW2zFoP/nOeA8DiNwvjdogbR3Yt/OssLCBQKkZl8DgiKI X-Received: from pjbsl11.prod.google.com ([2002:a17:90b:2e0b:b0:311:2058:21e7]) (user=jstultz job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:dac5:b0:234:c549:d9dd with SMTP id d9443c01a7336-23dedea537cmr74136505ad.48.1752291265275; Fri, 11 Jul 2025 20:34:25 -0700 (PDT) Date: Sat, 12 Jul 2025 03:33:49 +0000 In-Reply-To: <20250712033407.2383110-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: <20250712033407.2383110-1-jstultz@google.com> X-Mailer: git-send-email 2.50.0.727.gbf7dc18ff4-goog Message-ID: <20250712033407.2383110-9-jstultz@google.com> Subject: [PATCH v19 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 --- 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. v19: * Push proxy_resched_idle() returning rq->idle to this later patch in the series, as 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/locking/mutex.h | 3 +- kernel/sched/core.c | 146 ++++++++++++++++++++++++++++++++++------- kernel/sched/fair.c | 12 +++- 3 files changed, 135 insertions(+), 26 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 adb4818ee7f7d..d86407765388b 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) && @@ -6666,11 +6674,12 @@ static bool try_to_block_task(struct rq *rq, struct= task_struct *p, } =20 #ifdef CONFIG_SCHED_PROXY_EXEC -static inline void proxy_resched_idle(struct rq *rq) +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; } =20 static bool __proxy_deactivate(struct rq *rq, struct task_struct *donor) @@ -6707,36 +6716,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 * @@ -6894,10 +6991,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 068002fe15dd8..9ec5e813bb677 100644 --- a/kernel/sched/fair.c +++ b/kernel/sched/fair.c @@ -9413,7 +9413,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; @@ -9435,6 +9436,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 @@ -9470,7 +9474,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; } @@ -9514,6 +9519,9 @@ static void detach_task(struct task_struct *p, struct= lb_env *env) schedstat_inc(p->stats.nr_forced_migrations); } =20 + WARN_ON(task_current(env->src_rq, p)); + WARN_ON(task_current_donor(env->src_rq, p)); + deactivate_task(env->src_rq, p, DEQUEUE_NOCLOCK); set_task_cpu(p, env->dst_cpu); } --=20 2.50.0.727.gbf7dc18ff4-goog