From nobody Wed Dec 17 10:56:22 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4F229EE49A6 for ; Mon, 21 Aug 2023 22:20:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231304AbjHUWUa (ORCPT ); Mon, 21 Aug 2023 18:20:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60088 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231229AbjHUWU2 (ORCPT ); Mon, 21 Aug 2023 18:20:28 -0400 Received: from mail-wm1-x329.google.com (mail-wm1-x329.google.com [IPv6:2a00:1450:4864:20::329]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B7C66131 for ; Mon, 21 Aug 2023 15:20:22 -0700 (PDT) Received: by mail-wm1-x329.google.com with SMTP id 5b1f17b1804b1-3fbd33a57b6so36692495e9.2 for ; Mon, 21 Aug 2023 15:20:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=layalina-io.20221208.gappssmtp.com; s=20221208; t=1692656421; x=1693261221; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=OGGulz8Xs9VGIJsh2Gg1EO/IDYOGhZjqwi+HXfBv8GA=; b=FBr8hsVZUyCDzPHYEf5mMiSSLRqgKLX1EYON2WMn/vRtRI2B2klIQgNYSR+hQeZ5WO VEkAesn4ov+TvdOjlA/l7SOLVbEUtSruWRJkEYWZQrR+vv3QmmhphbP4HUylTTUyVoHo ikpAdF+Gx82kToB7LJp03Mej1idR2XpVaVLnC34kLerw2JI1rO9xsOp0QowJC84pKUPQ pDYZu8GU5e8lnZ3/i0tVP77t4lEFIQp84qYA3tfWjGmZneWYh1EWjWyzOeXkuyngUw3/ 5M6Qm0vKWhHb5jrMREYMcSqZjEXI5puMwoRPk9OxIbl0XTPmBXEbW1XHc+88JFf0QXB0 /vxw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692656421; x=1693261221; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=OGGulz8Xs9VGIJsh2Gg1EO/IDYOGhZjqwi+HXfBv8GA=; b=aYzVCt+S8eEKmZ9bGfiaoaminSaZUiG4txEX1tPDc6fnHenwdUX7LkwOfVH+bGP+06 SxnpXV/ZG9yujbarJTrDBCiwRsoiyc6GNPs1S/rgifF+aJj73roVlyXeNbe3eB5jJw5O I2rQurbenxhvLONhUoafOAIorMkW4c0YpGUDG6AwfS1dRZffV868L1exhGoafN67XHQY 05TywPmzDduv4cbKenpS2u/b85YF6vw8qoJyvkKv9fmJdoi+RH5+CN4oanCkxAEaphJh 9h7AS00VD3pN+wdoP1j+nVqiZrynuqro87EZgSjOVQWu8GSsy05kXhb+cZW1TyCW21Cg yeDA== X-Gm-Message-State: AOJu0Yy3AgF38At1tS3VzEuWs6fLIn1dZxZiZApzInwG27St8nqMULpD lKBHrjjLBL0JpRrqigBDJtIX5Q== X-Google-Smtp-Source: AGHT+IHpf1bgg9D4wRey2I8bxhP8ECfcpt/gVPFSFZ2pM8DF9wttV9D+MospCMe2HTqqga+l1No1Ag== X-Received: by 2002:a1c:7508:0:b0:3fe:dd91:b5b6 with SMTP id o8-20020a1c7508000000b003fedd91b5b6mr6085684wmc.25.1692656421239; Mon, 21 Aug 2023 15:20:21 -0700 (PDT) Received: from airbuntu.. (host109-151-228-137.range109-151.btcentralplus.com. [109.151.228.137]) by smtp.gmail.com with ESMTPSA id hn40-20020a05600ca3a800b003fe61c33df5sm17751154wmb.3.2023.08.21.15.20.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 21 Aug 2023 15:20:20 -0700 (PDT) From: Qais Yousef To: stable@vger.kernel.org Cc: Juri Lelli , Waiman Long , Tejun Heo , Dietmar Eggemann , Peter Zijlstra , Vincent Guittot , Ingo Molnar , Hao Luo , John Stultz , cgroups@vger.kernel.org, linux-kernel@vger.kernel.org, Qais Yousef Subject: [PATCH 1/6] cgroup/cpuset: Rename functions dealing with DEADLINE accounting Date: Mon, 21 Aug 2023 23:19:51 +0100 Message-Id: <20230821221956.698117-2-qyousef@layalina.io> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230821221956.698117-1-qyousef@layalina.io> References: <20230821221956.698117-1-qyousef@layalina.io> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Juri Lelli commit ad3a557daf6915296a43ef97a3e9c48e076c9dd8 upstream. rebuild_root_domains() and update_tasks_root_domain() have neutral names, but actually deal with DEADLINE bandwidth accounting. Rename them to use 'dl_' prefix so that intent is more clear. No functional change. Suggested-by: Qais Yousef (Google) Signed-off-by: Juri Lelli Reviewed-by: Waiman Long Signed-off-by: Tejun Heo (cherry picked from commit ad3a557daf6915296a43ef97a3e9c48e076c9dd8) Signed-off-by: Qais Yousef (Google) --- kernel/cgroup/cpuset.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/kernel/cgroup/cpuset.c b/kernel/cgroup/cpuset.c index e4ca2dd2b764..428ab46291e2 100644 --- a/kernel/cgroup/cpuset.c +++ b/kernel/cgroup/cpuset.c @@ -1066,7 +1066,7 @@ static int generate_sched_domains(cpumask_var_t **dom= ains, return ndoms; } =20 -static void update_tasks_root_domain(struct cpuset *cs) +static void dl_update_tasks_root_domain(struct cpuset *cs) { struct css_task_iter it; struct task_struct *task; @@ -1079,7 +1079,7 @@ static void update_tasks_root_domain(struct cpuset *c= s) css_task_iter_end(&it); } =20 -static void rebuild_root_domains(void) +static void dl_rebuild_rd_accounting(void) { struct cpuset *cs =3D NULL; struct cgroup_subsys_state *pos_css; @@ -1107,7 +1107,7 @@ static void rebuild_root_domains(void) =20 rcu_read_unlock(); =20 - update_tasks_root_domain(cs); + dl_update_tasks_root_domain(cs); =20 rcu_read_lock(); css_put(&cs->css); @@ -1121,7 +1121,7 @@ partition_and_rebuild_sched_domains(int ndoms_new, cp= umask_var_t doms_new[], { mutex_lock(&sched_domains_mutex); partition_sched_domains_locked(ndoms_new, doms_new, dattr_new); - rebuild_root_domains(); + dl_rebuild_rd_accounting(); mutex_unlock(&sched_domains_mutex); } =20 --=20 2.34.1 From nobody Wed Dec 17 10:56:22 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3A319EE49A8 for ; Mon, 21 Aug 2023 22:20:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231331AbjHUWUj (ORCPT ); Mon, 21 Aug 2023 18:20:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44782 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231322AbjHUWUb (ORCPT ); Mon, 21 Aug 2023 18:20:31 -0400 Received: from mail-wm1-x32d.google.com (mail-wm1-x32d.google.com [IPv6:2a00:1450:4864:20::32d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D9D68137 for ; Mon, 21 Aug 2023 15:20:23 -0700 (PDT) Received: by mail-wm1-x32d.google.com with SMTP id 5b1f17b1804b1-3fe9c20f449so37187225e9.3 for ; Mon, 21 Aug 2023 15:20:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=layalina-io.20221208.gappssmtp.com; s=20221208; t=1692656422; x=1693261222; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=vpGjJfq2A5e/X8d46OpXuARncKFR8DhE1r7cPJGjpx4=; b=rAFNwqoY4bRodYJUStwJPrMc2Rhr1zW3fNK6IM9tEe/wXNPO9+NhisW451IOq2jvGr 9f4WCZ9i0KDk4D1dUimsaqCoWrRMd6QvE64G3wZzu0To0atYPknlCc/7oM7l2Qi/CuDl 8+yvgFs5E9xqpUsppYVEOXSKLeIW5YzS+MwFeiA74dp6Oz/43RNrmmRvTcQd3/NdIvhE xp9yyMbgSEgD7J54DxqdociS7YyokCCgPAdsFnz41+G3+yGOyrulTw0Gzo+wwq3FxxVx YLctCIC0FnPMZbdvuWExO/MqsxvwS7wjnPd1JWtmWgzR570cRLOET966Ly4/CjLjj7XC GvCg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692656422; x=1693261222; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=vpGjJfq2A5e/X8d46OpXuARncKFR8DhE1r7cPJGjpx4=; b=Vm6xt890lthQZFSBv7iMEy1Jdldoz63Jnd+qW8J6CBWa9Zpg+kga7Aohs+zu7D6aXo YvV9hW77j+dSI1f25kI3JOCJ9LltyrEYKWUgUQCkpCxCcQs4JbKPvVz2IgxNjuMbpTON PEoG8VUpM+vPirIRh/KiG6bj+bxUll/xiudVbKEMPCCU3Q+pAPyfy8irT1905I0uBAmq 56j6FcGRkuvdlClP22UXwFAJqDw+nLFuAzKoPtxUvlwFKKf1TTMuw9o4IP2vXPM0TruC Iz2CtiOhFGdVqBWxbw7rPh3FgOUrdbbeVUKERjvB3mDt9mehp6yH0fRtXb303zeXt6sj +9ug== X-Gm-Message-State: AOJu0YxY1DE9CIGB221w6RkElo3LTwK1dCR3/XrOvRcSM6yoZPjBI2PX 0loM9/i+j348m8xi0u76/pSa9Q== X-Google-Smtp-Source: AGHT+IGJ4z12t08PyGUU45S7zGTQoTjObR5R6efH4aUo4Z9bdm7KWxX9ksN4gZXW0dathImLK51SjQ== X-Received: by 2002:a1c:7705:0:b0:3fb:d1db:5454 with SMTP id t5-20020a1c7705000000b003fbd1db5454mr6285051wmi.35.1692656422306; Mon, 21 Aug 2023 15:20:22 -0700 (PDT) Received: from airbuntu.. (host109-151-228-137.range109-151.btcentralplus.com. [109.151.228.137]) by smtp.gmail.com with ESMTPSA id hn40-20020a05600ca3a800b003fe61c33df5sm17751154wmb.3.2023.08.21.15.20.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 21 Aug 2023 15:20:21 -0700 (PDT) From: Qais Yousef To: stable@vger.kernel.org Cc: Juri Lelli , Waiman Long , Tejun Heo , Dietmar Eggemann , Peter Zijlstra , Vincent Guittot , Ingo Molnar , Hao Luo , John Stultz , cgroups@vger.kernel.org, linux-kernel@vger.kernel.org, Qais Yousef Subject: [PATCH 2/6] sched/cpuset: Bring back cpuset_mutex Date: Mon, 21 Aug 2023 23:19:52 +0100 Message-Id: <20230821221956.698117-3-qyousef@layalina.io> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230821221956.698117-1-qyousef@layalina.io> References: <20230821221956.698117-1-qyousef@layalina.io> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Juri Lelli commit 111cd11bbc54850f24191c52ff217da88a5e639b upstream. Turns out percpu_cpuset_rwsem - commit 1243dc518c9d ("cgroup/cpuset: Convert cpuset_mutex to percpu_rwsem") - wasn't such a brilliant idea, as it has been reported to cause slowdowns in workloads that need to change cpuset configuration frequently and it is also not implementing priority inheritance (which causes troubles with realtime workloads). Convert percpu_cpuset_rwsem back to regular cpuset_mutex. Also grab it only for SCHED_DEADLINE tasks (other policies don't care about stable cpusets anyway). Signed-off-by: Juri Lelli Reviewed-by: Waiman Long Signed-off-by: Tejun Heo (cherry picked from commit 111cd11bbc54850f24191c52ff217da88a5e639b) Signed-off-by: Qais Yousef (Google) --- include/linux/cpuset.h | 8 +-- kernel/cgroup/cpuset.c | 159 +++++++++++++++++++++-------------------- kernel/sched/core.c | 22 ++++-- 3 files changed, 99 insertions(+), 90 deletions(-) diff --git a/include/linux/cpuset.h b/include/linux/cpuset.h index 980b76a1237e..f90e6325d707 100644 --- a/include/linux/cpuset.h +++ b/include/linux/cpuset.h @@ -71,8 +71,8 @@ extern void cpuset_init_smp(void); extern void cpuset_force_rebuild(void); extern void cpuset_update_active_cpus(void); extern void cpuset_wait_for_hotplug(void); -extern void cpuset_read_lock(void); -extern void cpuset_read_unlock(void); +extern void cpuset_lock(void); +extern void cpuset_unlock(void); extern void cpuset_cpus_allowed(struct task_struct *p, struct cpumask *mas= k); extern bool cpuset_cpus_allowed_fallback(struct task_struct *p); extern nodemask_t cpuset_mems_allowed(struct task_struct *p); @@ -189,8 +189,8 @@ static inline void cpuset_update_active_cpus(void) =20 static inline void cpuset_wait_for_hotplug(void) { } =20 -static inline void cpuset_read_lock(void) { } -static inline void cpuset_read_unlock(void) { } +static inline void cpuset_lock(void) { } +static inline void cpuset_unlock(void) { } =20 static inline void cpuset_cpus_allowed(struct task_struct *p, struct cpumask *mask) diff --git a/kernel/cgroup/cpuset.c b/kernel/cgroup/cpuset.c index 428ab46291e2..041c0809adaf 100644 --- a/kernel/cgroup/cpuset.c +++ b/kernel/cgroup/cpuset.c @@ -366,22 +366,23 @@ static struct cpuset top_cpuset =3D { if (is_cpuset_online(((des_cs) =3D css_cs((pos_css))))) =20 /* - * There are two global locks guarding cpuset structures - cpuset_rwsem and + * There are two global locks guarding cpuset structures - cpuset_mutex and * callback_lock. We also require taking task_lock() when dereferencing a * task's cpuset pointer. See "The task_lock() exception", at the end of t= his - * comment. The cpuset code uses only cpuset_rwsem write lock. Other - * kernel subsystems can use cpuset_read_lock()/cpuset_read_unlock() to - * prevent change to cpuset structures. + * comment. The cpuset code uses only cpuset_mutex. Other kernel subsyste= ms + * can use cpuset_lock()/cpuset_unlock() to prevent change to cpuset + * structures. Note that cpuset_mutex needs to be a mutex as it is used in + * paths that rely on priority inheritance (e.g. scheduler - on RT) for + * correctness. * * A task must hold both locks to modify cpusets. If a task holds - * cpuset_rwsem, it blocks others wanting that rwsem, ensuring that it - * is the only task able to also acquire callback_lock and be able to - * modify cpusets. It can perform various checks on the cpuset structure - * first, knowing nothing will change. It can also allocate memory while - * just holding cpuset_rwsem. While it is performing these checks, various - * callback routines can briefly acquire callback_lock to query cpusets. - * Once it is ready to make the changes, it takes callback_lock, blocking - * everyone else. + * cpuset_mutex, it blocks others, ensuring that it is the only task able = to + * also acquire callback_lock and be able to modify cpusets. It can perfo= rm + * various checks on the cpuset structure first, knowing nothing will chan= ge. + * It can also allocate memory while just holding cpuset_mutex. While it = is + * performing these checks, various callback routines can briefly acquire + * callback_lock to query cpusets. Once it is ready to make the changes, = it + * takes callback_lock, blocking everyone else. * * Calls to the kernel memory allocator can not be made while holding * callback_lock, as that would risk double tripping on callback_lock @@ -403,16 +404,16 @@ static struct cpuset top_cpuset =3D { * guidelines for accessing subsystem state in kernel/cgroup.c */ =20 -DEFINE_STATIC_PERCPU_RWSEM(cpuset_rwsem); +static DEFINE_MUTEX(cpuset_mutex); =20 -void cpuset_read_lock(void) +void cpuset_lock(void) { - percpu_down_read(&cpuset_rwsem); + mutex_lock(&cpuset_mutex); } =20 -void cpuset_read_unlock(void) +void cpuset_unlock(void) { - percpu_up_read(&cpuset_rwsem); + mutex_unlock(&cpuset_mutex); } =20 static DEFINE_SPINLOCK(callback_lock); @@ -496,7 +497,7 @@ static inline bool partition_is_populated(struct cpuset= *cs, * One way or another, we guarantee to return some non-empty subset * of cpu_online_mask. * - * Call with callback_lock or cpuset_rwsem held. + * Call with callback_lock or cpuset_mutex held. */ static void guarantee_online_cpus(struct task_struct *tsk, struct cpumask *pmask) @@ -538,7 +539,7 @@ static void guarantee_online_cpus(struct task_struct *t= sk, * One way or another, we guarantee to return some non-empty subset * of node_states[N_MEMORY]. * - * Call with callback_lock or cpuset_rwsem held. + * Call with callback_lock or cpuset_mutex held. */ static void guarantee_online_mems(struct cpuset *cs, nodemask_t *pmask) { @@ -550,7 +551,7 @@ static void guarantee_online_mems(struct cpuset *cs, no= demask_t *pmask) /* * update task's spread flag if cpuset's page/slab spread flag is set * - * Call with callback_lock or cpuset_rwsem held. The check can be skipped + * Call with callback_lock or cpuset_mutex held. The check can be skipped * if on default hierarchy. */ static void cpuset_update_task_spread_flags(struct cpuset *cs, @@ -575,7 +576,7 @@ static void cpuset_update_task_spread_flags(struct cpus= et *cs, * * One cpuset is a subset of another if all its allowed CPUs and * Memory Nodes are a subset of the other, and its exclusive flags - * are only set if the other's are set. Call holding cpuset_rwsem. + * are only set if the other's are set. Call holding cpuset_mutex. */ =20 static int is_cpuset_subset(const struct cpuset *p, const struct cpuset *q) @@ -713,7 +714,7 @@ static int validate_change_legacy(struct cpuset *cur, s= truct cpuset *trial) * If we replaced the flag and mask values of the current cpuset * (cur) with those values in the trial cpuset (trial), would * our various subset and exclusive rules still be valid? Presumes - * cpuset_rwsem held. + * cpuset_mutex held. * * 'cur' is the address of an actual, in-use cpuset. Operations * such as list traversal that depend on the actual address of the @@ -829,7 +830,7 @@ static void update_domain_attr_tree(struct sched_domain= _attr *dattr, rcu_read_unlock(); } =20 -/* Must be called with cpuset_rwsem held. */ +/* Must be called with cpuset_mutex held. */ static inline int nr_cpusets(void) { /* jump label reference count + the top-level cpuset */ @@ -855,7 +856,7 @@ static inline int nr_cpusets(void) * domains when operating in the severe memory shortage situations * that could cause allocation failures below. * - * Must be called with cpuset_rwsem held. + * Must be called with cpuset_mutex held. * * The three key local variables below are: * cp - cpuset pointer, used (together with pos_css) to perform a @@ -1084,7 +1085,7 @@ static void dl_rebuild_rd_accounting(void) struct cpuset *cs =3D NULL; struct cgroup_subsys_state *pos_css; =20 - percpu_rwsem_assert_held(&cpuset_rwsem); + lockdep_assert_held(&cpuset_mutex); lockdep_assert_cpus_held(); lockdep_assert_held(&sched_domains_mutex); =20 @@ -1134,7 +1135,7 @@ partition_and_rebuild_sched_domains(int ndoms_new, cp= umask_var_t doms_new[], * 'cpus' is removed, then call this routine to rebuild the * scheduler's dynamic sched domains. * - * Call with cpuset_rwsem held. Takes cpus_read_lock(). + * Call with cpuset_mutex held. Takes cpus_read_lock(). */ static void rebuild_sched_domains_locked(void) { @@ -1145,7 +1146,7 @@ static void rebuild_sched_domains_locked(void) int ndoms; =20 lockdep_assert_cpus_held(); - percpu_rwsem_assert_held(&cpuset_rwsem); + lockdep_assert_held(&cpuset_mutex); =20 /* * If we have raced with CPU hotplug, return early to avoid @@ -1196,9 +1197,9 @@ static void rebuild_sched_domains_locked(void) void rebuild_sched_domains(void) { cpus_read_lock(); - percpu_down_write(&cpuset_rwsem); + mutex_lock(&cpuset_mutex); rebuild_sched_domains_locked(); - percpu_up_write(&cpuset_rwsem); + mutex_unlock(&cpuset_mutex); cpus_read_unlock(); } =20 @@ -1208,7 +1209,7 @@ void rebuild_sched_domains(void) * @new_cpus: the temp variable for the new effective_cpus mask * * Iterate through each task of @cs updating its cpus_allowed to the - * effective cpuset's. As this function is called with cpuset_rwsem held, + * effective cpuset's. As this function is called with cpuset_mutex held, * cpuset membership stays stable. For top_cpuset, task_cpu_possible_mask() * is used instead of effective_cpus to make sure all offline CPUs are also * included as hotplug code won't update cpumasks for tasks in top_cpuset. @@ -1322,7 +1323,7 @@ static int update_parent_subparts_cpumask(struct cpus= et *cs, int cmd, int old_prs, new_prs; int part_error =3D PERR_NONE; /* Partition error? */ =20 - percpu_rwsem_assert_held(&cpuset_rwsem); + lockdep_assert_held(&cpuset_mutex); =20 /* * The parent must be a partition root. @@ -1545,7 +1546,7 @@ static int update_parent_subparts_cpumask(struct cpus= et *cs, int cmd, * * On legacy hierarchy, effective_cpus will be the same with cpu_allowed. * - * Called with cpuset_rwsem held + * Called with cpuset_mutex held */ static void update_cpumasks_hier(struct cpuset *cs, struct tmpmasks *tmp, bool force) @@ -1705,7 +1706,7 @@ static void update_sibling_cpumasks(struct cpuset *pa= rent, struct cpuset *cs, struct cpuset *sibling; struct cgroup_subsys_state *pos_css; =20 - percpu_rwsem_assert_held(&cpuset_rwsem); + lockdep_assert_held(&cpuset_mutex); =20 /* * Check all its siblings and call update_cpumasks_hier() @@ -1955,12 +1956,12 @@ static void *cpuset_being_rebound; * @cs: the cpuset in which each task's mems_allowed mask needs to be chan= ged * * Iterate through each task of @cs updating its mems_allowed to the - * effective cpuset's. As this function is called with cpuset_rwsem held, + * effective cpuset's. As this function is called with cpuset_mutex held, * cpuset membership stays stable. */ static void update_tasks_nodemask(struct cpuset *cs) { - static nodemask_t newmems; /* protected by cpuset_rwsem */ + static nodemask_t newmems; /* protected by cpuset_mutex */ struct css_task_iter it; struct task_struct *task; =20 @@ -1973,7 +1974,7 @@ static void update_tasks_nodemask(struct cpuset *cs) * take while holding tasklist_lock. Forks can happen - the * mpol_dup() cpuset_being_rebound check will catch such forks, * and rebind their vma mempolicies too. Because we still hold - * the global cpuset_rwsem, we know that no other rebind effort + * the global cpuset_mutex, we know that no other rebind effort * will be contending for the global variable cpuset_being_rebound. * It's ok if we rebind the same mm twice; mpol_rebind_mm() * is idempotent. Also migrate pages in each mm to new nodes. @@ -2019,7 +2020,7 @@ static void update_tasks_nodemask(struct cpuset *cs) * * On legacy hierarchy, effective_mems will be the same with mems_allowed. * - * Called with cpuset_rwsem held + * Called with cpuset_mutex held */ static void update_nodemasks_hier(struct cpuset *cs, nodemask_t *new_mems) { @@ -2072,7 +2073,7 @@ static void update_nodemasks_hier(struct cpuset *cs, = nodemask_t *new_mems) * mempolicies and if the cpuset is marked 'memory_migrate', * migrate the tasks pages to the new memory. * - * Call with cpuset_rwsem held. May take callback_lock during call. + * Call with cpuset_mutex held. May take callback_lock during call. * Will take tasklist_lock, scan tasklist for tasks in cpuset cs, * lock each such tasks mm->mmap_lock, scan its vma's and rebind * their mempolicies to the cpusets new mems_allowed. @@ -2164,7 +2165,7 @@ static int update_relax_domain_level(struct cpuset *c= s, s64 val) * @cs: the cpuset in which each task's spread flags needs to be changed * * Iterate through each task of @cs updating its spread flags. As this - * function is called with cpuset_rwsem held, cpuset membership stays + * function is called with cpuset_mutex held, cpuset membership stays * stable. */ static void update_tasks_flags(struct cpuset *cs) @@ -2184,7 +2185,7 @@ static void update_tasks_flags(struct cpuset *cs) * cs: the cpuset to update * turning_on: whether the flag is being set or cleared * - * Call with cpuset_rwsem held. + * Call with cpuset_mutex held. */ =20 static int update_flag(cpuset_flagbits_t bit, struct cpuset *cs, @@ -2234,7 +2235,7 @@ static int update_flag(cpuset_flagbits_t bit, struct = cpuset *cs, * @new_prs: new partition root state * Return: 0 if successful, !=3D 0 if error * - * Call with cpuset_rwsem held. + * Call with cpuset_mutex held. */ static int update_prstate(struct cpuset *cs, int new_prs) { @@ -2472,7 +2473,7 @@ static int cpuset_can_attach_check(struct cpuset *cs) return 0; } =20 -/* Called by cgroups to determine if a cpuset is usable; cpuset_rwsem held= */ +/* Called by cgroups to determine if a cpuset is usable; cpuset_mutex held= */ static int cpuset_can_attach(struct cgroup_taskset *tset) { struct cgroup_subsys_state *css; @@ -2484,7 +2485,7 @@ static int cpuset_can_attach(struct cgroup_taskset *t= set) cpuset_attach_old_cs =3D task_cs(cgroup_taskset_first(tset, &css)); cs =3D css_cs(css); =20 - percpu_down_write(&cpuset_rwsem); + mutex_lock(&cpuset_mutex); =20 /* Check to see if task is allowed in the cpuset */ ret =3D cpuset_can_attach_check(cs); @@ -2506,7 +2507,7 @@ static int cpuset_can_attach(struct cgroup_taskset *t= set) */ cs->attach_in_progress++; out_unlock: - percpu_up_write(&cpuset_rwsem); + mutex_unlock(&cpuset_mutex); return ret; } =20 @@ -2518,15 +2519,15 @@ static void cpuset_cancel_attach(struct cgroup_task= set *tset) cgroup_taskset_first(tset, &css); cs =3D css_cs(css); =20 - percpu_down_write(&cpuset_rwsem); + mutex_lock(&cpuset_mutex); cs->attach_in_progress--; if (!cs->attach_in_progress) wake_up(&cpuset_attach_wq); - percpu_up_write(&cpuset_rwsem); + mutex_unlock(&cpuset_mutex); } =20 /* - * Protected by cpuset_rwsem. cpus_attach is used only by cpuset_attach_ta= sk() + * Protected by cpuset_mutex. cpus_attach is used only by cpuset_attach_ta= sk() * but we can't allocate it dynamically there. Define it global and * allocate from cpuset_init(). */ @@ -2535,7 +2536,7 @@ static nodemask_t cpuset_attach_nodemask_to; =20 static void cpuset_attach_task(struct cpuset *cs, struct task_struct *task) { - percpu_rwsem_assert_held(&cpuset_rwsem); + lockdep_assert_held(&cpuset_mutex); =20 if (cs !=3D &top_cpuset) guarantee_online_cpus(task, cpus_attach); @@ -2565,7 +2566,7 @@ static void cpuset_attach(struct cgroup_taskset *tset) cs =3D css_cs(css); =20 lockdep_assert_cpus_held(); /* see cgroup_attach_lock() */ - percpu_down_write(&cpuset_rwsem); + mutex_lock(&cpuset_mutex); cpus_updated =3D !cpumask_equal(cs->effective_cpus, oldcs->effective_cpus); mems_updated =3D !nodes_equal(cs->effective_mems, oldcs->effective_mems); @@ -2626,7 +2627,7 @@ static void cpuset_attach(struct cgroup_taskset *tset) if (!cs->attach_in_progress) wake_up(&cpuset_attach_wq); =20 - percpu_up_write(&cpuset_rwsem); + mutex_unlock(&cpuset_mutex); } =20 /* The various types of files and directories in a cpuset file system */ @@ -2658,7 +2659,7 @@ static int cpuset_write_u64(struct cgroup_subsys_stat= e *css, struct cftype *cft, int retval =3D 0; =20 cpus_read_lock(); - percpu_down_write(&cpuset_rwsem); + mutex_lock(&cpuset_mutex); if (!is_cpuset_online(cs)) { retval =3D -ENODEV; goto out_unlock; @@ -2694,7 +2695,7 @@ static int cpuset_write_u64(struct cgroup_subsys_stat= e *css, struct cftype *cft, break; } out_unlock: - percpu_up_write(&cpuset_rwsem); + mutex_unlock(&cpuset_mutex); cpus_read_unlock(); return retval; } @@ -2707,7 +2708,7 @@ static int cpuset_write_s64(struct cgroup_subsys_stat= e *css, struct cftype *cft, int retval =3D -ENODEV; =20 cpus_read_lock(); - percpu_down_write(&cpuset_rwsem); + mutex_lock(&cpuset_mutex); if (!is_cpuset_online(cs)) goto out_unlock; =20 @@ -2720,7 +2721,7 @@ static int cpuset_write_s64(struct cgroup_subsys_stat= e *css, struct cftype *cft, break; } out_unlock: - percpu_up_write(&cpuset_rwsem); + mutex_unlock(&cpuset_mutex); cpus_read_unlock(); return retval; } @@ -2753,7 +2754,7 @@ static ssize_t cpuset_write_resmask(struct kernfs_ope= n_file *of, * operation like this one can lead to a deadlock through kernfs * active_ref protection. Let's break the protection. Losing the * protection is okay as we check whether @cs is online after - * grabbing cpuset_rwsem anyway. This only happens on the legacy + * grabbing cpuset_mutex anyway. This only happens on the legacy * hierarchies. */ css_get(&cs->css); @@ -2761,7 +2762,7 @@ static ssize_t cpuset_write_resmask(struct kernfs_ope= n_file *of, flush_work(&cpuset_hotplug_work); =20 cpus_read_lock(); - percpu_down_write(&cpuset_rwsem); + mutex_lock(&cpuset_mutex); if (!is_cpuset_online(cs)) goto out_unlock; =20 @@ -2785,7 +2786,7 @@ static ssize_t cpuset_write_resmask(struct kernfs_ope= n_file *of, =20 free_cpuset(trialcs); out_unlock: - percpu_up_write(&cpuset_rwsem); + mutex_unlock(&cpuset_mutex); cpus_read_unlock(); kernfs_unbreak_active_protection(of->kn); css_put(&cs->css); @@ -2933,13 +2934,13 @@ static ssize_t sched_partition_write(struct kernfs_= open_file *of, char *buf, =20 css_get(&cs->css); cpus_read_lock(); - percpu_down_write(&cpuset_rwsem); + mutex_lock(&cpuset_mutex); if (!is_cpuset_online(cs)) goto out_unlock; =20 retval =3D update_prstate(cs, val); out_unlock: - percpu_up_write(&cpuset_rwsem); + mutex_unlock(&cpuset_mutex); cpus_read_unlock(); css_put(&cs->css); return retval ?: nbytes; @@ -3156,7 +3157,7 @@ static int cpuset_css_online(struct cgroup_subsys_sta= te *css) return 0; =20 cpus_read_lock(); - percpu_down_write(&cpuset_rwsem); + mutex_lock(&cpuset_mutex); =20 set_bit(CS_ONLINE, &cs->flags); if (is_spread_page(parent)) @@ -3207,7 +3208,7 @@ static int cpuset_css_online(struct cgroup_subsys_sta= te *css) cpumask_copy(cs->effective_cpus, parent->cpus_allowed); spin_unlock_irq(&callback_lock); out_unlock: - percpu_up_write(&cpuset_rwsem); + mutex_unlock(&cpuset_mutex); cpus_read_unlock(); return 0; } @@ -3228,7 +3229,7 @@ static void cpuset_css_offline(struct cgroup_subsys_s= tate *css) struct cpuset *cs =3D css_cs(css); =20 cpus_read_lock(); - percpu_down_write(&cpuset_rwsem); + mutex_lock(&cpuset_mutex); =20 if (is_partition_valid(cs)) update_prstate(cs, 0); @@ -3247,7 +3248,7 @@ static void cpuset_css_offline(struct cgroup_subsys_s= tate *css) cpuset_dec(); clear_bit(CS_ONLINE, &cs->flags); =20 - percpu_up_write(&cpuset_rwsem); + mutex_unlock(&cpuset_mutex); cpus_read_unlock(); } =20 @@ -3260,7 +3261,7 @@ static void cpuset_css_free(struct cgroup_subsys_stat= e *css) =20 static void cpuset_bind(struct cgroup_subsys_state *root_css) { - percpu_down_write(&cpuset_rwsem); + mutex_lock(&cpuset_mutex); spin_lock_irq(&callback_lock); =20 if (is_in_v2_mode()) { @@ -3273,7 +3274,7 @@ static void cpuset_bind(struct cgroup_subsys_state *r= oot_css) } =20 spin_unlock_irq(&callback_lock); - percpu_up_write(&cpuset_rwsem); + mutex_unlock(&cpuset_mutex); } =20 /* @@ -3294,7 +3295,7 @@ static int cpuset_can_fork(struct task_struct *task, = struct css_set *cset) return 0; =20 lockdep_assert_held(&cgroup_mutex); - percpu_down_write(&cpuset_rwsem); + mutex_lock(&cpuset_mutex); =20 /* Check to see if task is allowed in the cpuset */ ret =3D cpuset_can_attach_check(cs); @@ -3315,7 +3316,7 @@ static int cpuset_can_fork(struct task_struct *task, = struct css_set *cset) */ cs->attach_in_progress++; out_unlock: - percpu_up_write(&cpuset_rwsem); + mutex_unlock(&cpuset_mutex); return ret; } =20 @@ -3331,11 +3332,11 @@ static void cpuset_cancel_fork(struct task_struct *= task, struct css_set *cset) if (same_cs) return; =20 - percpu_down_write(&cpuset_rwsem); + mutex_lock(&cpuset_mutex); cs->attach_in_progress--; if (!cs->attach_in_progress) wake_up(&cpuset_attach_wq); - percpu_up_write(&cpuset_rwsem); + mutex_unlock(&cpuset_mutex); } =20 /* @@ -3363,7 +3364,7 @@ static void cpuset_fork(struct task_struct *task) } =20 /* CLONE_INTO_CGROUP */ - percpu_down_write(&cpuset_rwsem); + mutex_lock(&cpuset_mutex); guarantee_online_mems(cs, &cpuset_attach_nodemask_to); cpuset_attach_task(cs, task); =20 @@ -3371,7 +3372,7 @@ static void cpuset_fork(struct task_struct *task) if (!cs->attach_in_progress) wake_up(&cpuset_attach_wq); =20 - percpu_up_write(&cpuset_rwsem); + mutex_unlock(&cpuset_mutex); } =20 struct cgroup_subsys cpuset_cgrp_subsys =3D { @@ -3472,7 +3473,7 @@ hotplug_update_tasks_legacy(struct cpuset *cs, is_empty =3D cpumask_empty(cs->cpus_allowed) || nodes_empty(cs->mems_allowed); =20 - percpu_up_write(&cpuset_rwsem); + mutex_unlock(&cpuset_mutex); =20 /* * Move tasks to the nearest ancestor with execution resources, @@ -3482,7 +3483,7 @@ hotplug_update_tasks_legacy(struct cpuset *cs, if (is_empty) remove_tasks_in_empty_cpuset(cs); =20 - percpu_down_write(&cpuset_rwsem); + mutex_lock(&cpuset_mutex); } =20 static void @@ -3533,14 +3534,14 @@ static void cpuset_hotplug_update_tasks(struct cpus= et *cs, struct tmpmasks *tmp) retry: wait_event(cpuset_attach_wq, cs->attach_in_progress =3D=3D 0); =20 - percpu_down_write(&cpuset_rwsem); + mutex_lock(&cpuset_mutex); =20 /* * We have raced with task attaching. We wait until attaching * is finished, so we won't attach a task to an empty cpuset. */ if (cs->attach_in_progress) { - percpu_up_write(&cpuset_rwsem); + mutex_unlock(&cpuset_mutex); goto retry; } =20 @@ -3637,7 +3638,7 @@ static void cpuset_hotplug_update_tasks(struct cpuset= *cs, struct tmpmasks *tmp) cpus_updated, mems_updated); =20 unlock: - percpu_up_write(&cpuset_rwsem); + mutex_unlock(&cpuset_mutex); } =20 /** @@ -3667,7 +3668,7 @@ static void cpuset_hotplug_workfn(struct work_struct = *work) if (on_dfl && !alloc_cpumasks(NULL, &tmp)) ptmp =3D &tmp; =20 - percpu_down_write(&cpuset_rwsem); + mutex_lock(&cpuset_mutex); =20 /* fetch the available cpus/mems and find out which changed how */ cpumask_copy(&new_cpus, cpu_active_mask); @@ -3724,7 +3725,7 @@ static void cpuset_hotplug_workfn(struct work_struct = *work) update_tasks_nodemask(&top_cpuset); } =20 - percpu_up_write(&cpuset_rwsem); + mutex_unlock(&cpuset_mutex); =20 /* if cpus or mems changed, we need to propagate to descendants */ if (cpus_updated || mems_updated) { @@ -4155,7 +4156,7 @@ void __cpuset_memory_pressure_bump(void) * - Used for /proc//cpuset. * - No need to task_lock(tsk) on this tsk->cpuset reference, as it * doesn't really matter if tsk->cpuset changes after we read it, - * and we take cpuset_rwsem, keeping cpuset_attach() from changing it + * and we take cpuset_mutex, keeping cpuset_attach() from changing it * anyway. */ int proc_cpuset_show(struct seq_file *m, struct pid_namespace *ns, diff --git a/kernel/sched/core.c b/kernel/sched/core.c index a68d1276bab0..72cd267e3821 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -7590,6 +7590,7 @@ static int __sched_setscheduler(struct task_struct *p, int reset_on_fork; int queue_flags =3D DEQUEUE_SAVE | DEQUEUE_MOVE | DEQUEUE_NOCLOCK; struct rq *rq; + bool cpuset_locked =3D false; =20 /* The pi code expects interrupts enabled */ BUG_ON(pi && in_interrupt()); @@ -7639,8 +7640,14 @@ static int __sched_setscheduler(struct task_struct *= p, return retval; } =20 - if (pi) - cpuset_read_lock(); + /* + * SCHED_DEADLINE bandwidth accounting relies on stable cpusets + * information. + */ + if (dl_policy(policy) || dl_policy(p->policy)) { + cpuset_locked =3D true; + cpuset_lock(); + } =20 /* * Make sure no PI-waiters arrive (or leave) while we are @@ -7716,8 +7723,8 @@ static int __sched_setscheduler(struct task_struct *p, if (unlikely(oldpolicy !=3D -1 && oldpolicy !=3D p->policy)) { policy =3D oldpolicy =3D -1; task_rq_unlock(rq, p, &rf); - if (pi) - cpuset_read_unlock(); + if (cpuset_locked) + cpuset_unlock(); goto recheck; } =20 @@ -7784,7 +7791,8 @@ static int __sched_setscheduler(struct task_struct *p, task_rq_unlock(rq, p, &rf); =20 if (pi) { - cpuset_read_unlock(); + if (cpuset_locked) + cpuset_unlock(); rt_mutex_adjust_pi(p); } =20 @@ -7796,8 +7804,8 @@ static int __sched_setscheduler(struct task_struct *p, =20 unlock: task_rq_unlock(rq, p, &rf); - if (pi) - cpuset_read_unlock(); + if (cpuset_locked) + cpuset_unlock(); return retval; } =20 --=20 2.34.1 From nobody Wed Dec 17 10:56:22 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B2640EE49A6 for ; Mon, 21 Aug 2023 22:20:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231360AbjHUWUd (ORCPT ); Mon, 21 Aug 2023 18:20:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44774 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231309AbjHUWUa (ORCPT ); Mon, 21 Aug 2023 18:20:30 -0400 Received: from mail-wm1-x331.google.com (mail-wm1-x331.google.com [IPv6:2a00:1450:4864:20::331]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9C06E185 for ; Mon, 21 Aug 2023 15:20:24 -0700 (PDT) Received: by mail-wm1-x331.google.com with SMTP id 5b1f17b1804b1-3fe1e1142caso38237125e9.0 for ; Mon, 21 Aug 2023 15:20:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=layalina-io.20221208.gappssmtp.com; s=20221208; t=1692656423; x=1693261223; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=O1kw3WQnL3iTlge0NF0vy4UNO9daWogE09VXqwnAfts=; b=BAcTJuLbERccCSX58gUF2qlzd148tForaVKzS7/AZgPw3f/bo93KbgiSY9E8hHkz/P 2uG86XuARo8+zszuhW9wMSPgMTRv3zIVMc9RUhzdV7sqqCXD2HxY9iD9bBSPXSM9GlOg r/1LQjxM7kSTszSwOKfMDLj2VZfn6/lW+AyrGGoGlthmWVcrhavIqn0d2Cpn6FbLc4u4 0m/CPfbOsgW27s7wQdio1OvSSTFdr7R5hJFWqQxq+O3plzcetrLyXimfRFqEOH43rKz0 GaGoYE10PQVzjMxeLWSgy29AIod43SM/90aWC1Id4WG4TONLQqzMqNi7j/Cmrt8bBMey gYIw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692656423; x=1693261223; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=O1kw3WQnL3iTlge0NF0vy4UNO9daWogE09VXqwnAfts=; b=gxrK9Gf1kIvE5K1D5AcC9FSCpMWt8UYViK46oCzhnaDVZNwOZ1ibfdBhBuzenj7T88 5geUH/VWUag4Swxw0BOqqlKiGcCnEJ819t+q8TT5z9gQTsH+0WPdV/e7XATSznZUwt1M 4+dmd/nYtnSMuksJjm+Y5p8mCW+/kdzXwnzPG3rhdgVcrRQC70SwxCyWl+oBrUUX0XcA dJhtfpIWNkz5Ig1tVs8R5/WQeNZD3GFYmf0TQSoN3HAX1F42o6HplmWr98y/c22pHhzo whFuW7F1PyXRSm8SHS7av9WbQ7nkiKyTJxW9+5jxsfc/1zYkVlwLY0jNU468k9zou4wz SO/Q== X-Gm-Message-State: AOJu0YxmOmKRbLWgbG9VGNJE1iRTOYj5AGt2+BbZwuLJZfMJIr56JRhg 35YcqZ+ptrrxj2rmnpCFrmZq0w== X-Google-Smtp-Source: AGHT+IH+9hDALfPhzelzQbjkDqgI2PpKvWy9rHvbOio8t53VyYd2Vcqbq7J27glZwxaSLHENX2EsTw== X-Received: by 2002:a1c:4c17:0:b0:3fa:95c7:e891 with SMTP id z23-20020a1c4c17000000b003fa95c7e891mr5956246wmf.35.1692656423231; Mon, 21 Aug 2023 15:20:23 -0700 (PDT) Received: from airbuntu.. (host109-151-228-137.range109-151.btcentralplus.com. [109.151.228.137]) by smtp.gmail.com with ESMTPSA id hn40-20020a05600ca3a800b003fe61c33df5sm17751154wmb.3.2023.08.21.15.20.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 21 Aug 2023 15:20:22 -0700 (PDT) From: Qais Yousef To: stable@vger.kernel.org Cc: Juri Lelli , Waiman Long , Tejun Heo , Dietmar Eggemann , Peter Zijlstra , Vincent Guittot , Ingo Molnar , Hao Luo , John Stultz , cgroups@vger.kernel.org, linux-kernel@vger.kernel.org, Qais Yousef Subject: [PATCH 3/6] sched/cpuset: Keep track of SCHED_DEADLINE task in cpusets Date: Mon, 21 Aug 2023 23:19:53 +0100 Message-Id: <20230821221956.698117-4-qyousef@layalina.io> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230821221956.698117-1-qyousef@layalina.io> References: <20230821221956.698117-1-qyousef@layalina.io> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Juri Lelli commit 6c24849f5515e4966d94fa5279bdff4acf2e9489 upstream. Qais reported that iterating over all tasks when rebuilding root domains for finding out which ones are DEADLINE and need their bandwidth correctly restored on such root domains can be a costly operation (10+ ms delays on suspend-resume). To fix the problem keep track of the number of DEADLINE tasks belonging to each cpuset and then use this information (followup patch) to only perform the above iteration if DEADLINE tasks are actually present in the cpuset for which a corresponding root domain is being rebuilt. Reported-by: Qais Yousef (Google) Link: https://lore.kernel.org/lkml/20230206221428.2125324-1-qyousef@layalin= a.io/ Signed-off-by: Juri Lelli Reviewed-by: Waiman Long Signed-off-by: Tejun Heo (cherry picked from commit 6c24849f5515e4966d94fa5279bdff4acf2e9489) Signed-off-by: Qais Yousef (Google) --- include/linux/cpuset.h | 4 ++++ kernel/cgroup/cgroup.c | 4 ++++ kernel/cgroup/cpuset.c | 25 +++++++++++++++++++++++++ kernel/sched/deadline.c | 14 ++++++++++++++ 4 files changed, 47 insertions(+) diff --git a/include/linux/cpuset.h b/include/linux/cpuset.h index f90e6325d707..d629094fac6e 100644 --- a/include/linux/cpuset.h +++ b/include/linux/cpuset.h @@ -71,6 +71,8 @@ extern void cpuset_init_smp(void); extern void cpuset_force_rebuild(void); extern void cpuset_update_active_cpus(void); extern void cpuset_wait_for_hotplug(void); +extern void inc_dl_tasks_cs(struct task_struct *task); +extern void dec_dl_tasks_cs(struct task_struct *task); extern void cpuset_lock(void); extern void cpuset_unlock(void); extern void cpuset_cpus_allowed(struct task_struct *p, struct cpumask *mas= k); @@ -189,6 +191,8 @@ static inline void cpuset_update_active_cpus(void) =20 static inline void cpuset_wait_for_hotplug(void) { } =20 +static inline void inc_dl_tasks_cs(struct task_struct *task) { } +static inline void dec_dl_tasks_cs(struct task_struct *task) { } static inline void cpuset_lock(void) { } static inline void cpuset_unlock(void) { } =20 diff --git a/kernel/cgroup/cgroup.c b/kernel/cgroup/cgroup.c index 3299ec69ce0d..5b9648c940b4 100644 --- a/kernel/cgroup/cgroup.c +++ b/kernel/cgroup/cgroup.c @@ -57,6 +57,7 @@ #include #include #include +#include #include #include =20 @@ -6696,6 +6697,9 @@ void cgroup_exit(struct task_struct *tsk) list_add_tail(&tsk->cg_list, &cset->dying_tasks); cset->nr_tasks--; =20 + if (dl_task(tsk)) + dec_dl_tasks_cs(tsk); + WARN_ON_ONCE(cgroup_task_frozen(tsk)); if (unlikely(!(tsk->flags & PF_KTHREAD) && test_bit(CGRP_FREEZE, &task_dfl_cgroup(tsk)->flags))) diff --git a/kernel/cgroup/cpuset.c b/kernel/cgroup/cpuset.c index 041c0809adaf..ca195ff8b298 100644 --- a/kernel/cgroup/cpuset.c +++ b/kernel/cgroup/cpuset.c @@ -193,6 +193,12 @@ struct cpuset { int use_parent_ecpus; int child_ecpus_count; =20 + /* + * number of SCHED_DEADLINE tasks attached to this cpuset, so that we + * know when to rebuild associated root domain bandwidth information. + */ + int nr_deadline_tasks; + /* Invalid partition error code, not lock protected */ enum prs_errcode prs_err; =20 @@ -245,6 +251,20 @@ static inline struct cpuset *parent_cs(struct cpuset *= cs) return css_cs(cs->css.parent); } =20 +void inc_dl_tasks_cs(struct task_struct *p) +{ + struct cpuset *cs =3D task_cs(p); + + cs->nr_deadline_tasks++; +} + +void dec_dl_tasks_cs(struct task_struct *p) +{ + struct cpuset *cs =3D task_cs(p); + + cs->nr_deadline_tasks--; +} + /* bits in struct cpuset flags field */ typedef enum { CS_ONLINE, @@ -2499,6 +2519,11 @@ static int cpuset_can_attach(struct cgroup_taskset *= tset) ret =3D security_task_setscheduler(task); if (ret) goto out_unlock; + + if (dl_task(task)) { + cs->nr_deadline_tasks++; + cpuset_attach_old_cs->nr_deadline_tasks--; + } } =20 /* diff --git a/kernel/sched/deadline.c b/kernel/sched/deadline.c index 5a9a4b81c972..e11de074a6fd 100644 --- a/kernel/sched/deadline.c +++ b/kernel/sched/deadline.c @@ -16,6 +16,8 @@ * Fabio Checconi */ =20 +#include + /* * Default limits for DL period; on the top end we guard against small util * tasks still getting ridiculously long effective runtimes, on the bottom= end we @@ -2596,6 +2598,12 @@ static void switched_from_dl(struct rq *rq, struct t= ask_struct *p) if (task_on_rq_queued(p) && p->dl.dl_runtime) task_non_contending(p); =20 + /* + * In case a task is setscheduled out from SCHED_DEADLINE we need to + * keep track of that on its cpuset (for correct bandwidth tracking). + */ + dec_dl_tasks_cs(p); + if (!task_on_rq_queued(p)) { /* * Inactive timer is armed. However, p is leaving DEADLINE and @@ -2636,6 +2644,12 @@ static void switched_to_dl(struct rq *rq, struct tas= k_struct *p) if (hrtimer_try_to_cancel(&p->dl.inactive_timer) =3D=3D 1) put_task_struct(p); =20 + /* + * In case a task is setscheduled to SCHED_DEADLINE we need to keep + * track of that on its cpuset (for correct bandwidth tracking). + */ + inc_dl_tasks_cs(p); + /* If p is not queued we will update its parameters at next wakeup. */ if (!task_on_rq_queued(p)) { add_rq_bw(&p->dl, &rq->dl); --=20 2.34.1 From nobody Wed Dec 17 10:56:22 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 185DAEE49A6 for ; Mon, 21 Aug 2023 22:20:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231501AbjHUWUn (ORCPT ); Mon, 21 Aug 2023 18:20:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44844 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231368AbjHUWUi (ORCPT ); Mon, 21 Aug 2023 18:20:38 -0400 Received: from mail-wm1-x332.google.com (mail-wm1-x332.google.com [IPv6:2a00:1450:4864:20::332]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7B161188 for ; Mon, 21 Aug 2023 15:20:25 -0700 (PDT) Received: by mail-wm1-x332.google.com with SMTP id 5b1f17b1804b1-3fe1e1142caso38237245e9.0 for ; Mon, 21 Aug 2023 15:20:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=layalina-io.20221208.gappssmtp.com; s=20221208; t=1692656424; x=1693261224; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=zv3/KXztnL5KoChNf1Fo3z4E8w03N3Vz74hqaKz4DNE=; b=BpLPwm/4qMtYQnJZdL/KNRwP7phYdTF/YWVH8K9pnFsM+Q5zE6QnM3lHDFdGixEgkK v9CC/qdXTeYberXAtZHhgv1f4FthPxkH4H5GCMD3xctFGhsQw3lZ1O76tK7SjuRJcNNF +LvwpBst3BGysD2QvvtOYZMwAAoipc0lx0GZIegv4Xt6Hs8Ho3z/IKqEd+s3muOAvGdz jqmRE31xvlWiQCYgNUPoTCFaqwS4vE3AyUoob04X41XCqGWTv0vERBz5s7NZFnTCxfTQ MN5Qy0DbKmEVlA/yR0gWNAkpkMB5lM8drWnV6Bk+GXgAZfe73+VUgxiCRT2ddDASzsdw 3ODg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692656424; x=1693261224; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=zv3/KXztnL5KoChNf1Fo3z4E8w03N3Vz74hqaKz4DNE=; b=i6/J54pK6m0JJzkr5x2po8wWP1L0ytqQ2O8KOgTM+sR1bLZ40PbxIBmeX7agBEWry/ JHc5PBG5ufwBTQ9csYCvpHTL6+MZ2nW37q7HI3jCh62B7kQ+Ozr+lYe294qoNnzpQhPr Wiaokh7zsjxUO9AKwDrWDU157hgkpXStheGS8lN26oaAx6CXE9Vg/gCSkvkL2sg0OJWd wle6GeZczanHwsbVZXJ8EMZbtqnvvKjNCKtmUspBevd4ZYtwGka97W4ag4MFccXmfFzr 4Qpow8eSVSWQWGTjVYWd9tI8OvOL57VoDkLPYMBjcFb1yhHOdUO570KikRToKyohVkex 4P3Q== X-Gm-Message-State: AOJu0YxNHmukj/t9RwCO573WHTKiE2MzL52FQlpO+rlisHeiUwkK4lc4 T6Uj+5oMYJ2VGVEP+sBnepKc2g== X-Google-Smtp-Source: AGHT+IH+42oA+v5gZ66xF6eh/lfH7VJgolpwEE78hT93/Yh/YPgkaMG+Tx/AoHmev2F7Tp/vH2gpAw== X-Received: by 2002:a05:600c:2307:b0:3fe:687a:abad with SMTP id 7-20020a05600c230700b003fe687aabadmr5792357wmo.20.1692656424104; Mon, 21 Aug 2023 15:20:24 -0700 (PDT) Received: from airbuntu.. (host109-151-228-137.range109-151.btcentralplus.com. [109.151.228.137]) by smtp.gmail.com with ESMTPSA id hn40-20020a05600ca3a800b003fe61c33df5sm17751154wmb.3.2023.08.21.15.20.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 21 Aug 2023 15:20:23 -0700 (PDT) From: Qais Yousef To: stable@vger.kernel.org Cc: Juri Lelli , Waiman Long , Tejun Heo , Dietmar Eggemann , Peter Zijlstra , Vincent Guittot , Ingo Molnar , Hao Luo , John Stultz , cgroups@vger.kernel.org, linux-kernel@vger.kernel.org, Qais Yousef Subject: [PATCH 4/6] cgroup/cpuset: Iterate only if DEADLINE tasks are present Date: Mon, 21 Aug 2023 23:19:54 +0100 Message-Id: <20230821221956.698117-5-qyousef@layalina.io> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230821221956.698117-1-qyousef@layalina.io> References: <20230821221956.698117-1-qyousef@layalina.io> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Juri Lelli commit c0f78fd5edcf29b2822ac165f9248a6c165e8554 upstream. update_tasks_root_domain currently iterates over all tasks even if no DEADLINE task is present on the cpuset/root domain for which bandwidth accounting is being rebuilt. This has been reported to introduce 10+ ms delays on suspend-resume operations. Skip the costly iteration for cpusets that don't contain DEADLINE tasks. Reported-by: Qais Yousef (Google) Link: https://lore.kernel.org/lkml/20230206221428.2125324-1-qyousef@layalin= a.io/ Signed-off-by: Juri Lelli Reviewed-by: Waiman Long Signed-off-by: Tejun Heo (cherry picked from commit c0f78fd5edcf29b2822ac165f9248a6c165e8554) Signed-off-by: Qais Yousef (Google) --- kernel/cgroup/cpuset.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/kernel/cgroup/cpuset.c b/kernel/cgroup/cpuset.c index ca195ff8b298..b7168970fff2 100644 --- a/kernel/cgroup/cpuset.c +++ b/kernel/cgroup/cpuset.c @@ -1092,6 +1092,9 @@ static void dl_update_tasks_root_domain(struct cpuset= *cs) struct css_task_iter it; struct task_struct *task; =20 + if (cs->nr_deadline_tasks =3D=3D 0) + return; + css_task_iter_start(&cs->css, 0, &it); =20 while ((task =3D css_task_iter_next(&it))) --=20 2.34.1 From nobody Wed Dec 17 10:56:22 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C374AEE49A0 for ; Mon, 21 Aug 2023 22:20:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231487AbjHUWUk (ORCPT ); Mon, 21 Aug 2023 18:20:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44828 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229841AbjHUWUh (ORCPT ); Mon, 21 Aug 2023 18:20:37 -0400 Received: from mail-wm1-x32e.google.com (mail-wm1-x32e.google.com [IPv6:2a00:1450:4864:20::32e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 96E56193 for ; Mon, 21 Aug 2023 15:20:26 -0700 (PDT) Received: by mail-wm1-x32e.google.com with SMTP id 5b1f17b1804b1-3fee843b44cso20955375e9.1 for ; Mon, 21 Aug 2023 15:20:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=layalina-io.20221208.gappssmtp.com; s=20221208; t=1692656425; x=1693261225; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=am444WNrS3h8wlVCx2H3MlRIk5MPSUP4lUnh4kSEX7U=; b=BPDNlUnLuVmEXdh393SwJy7fbkTa6LxPE5FyC53TuAC/nNeerqqlrPUU2Ql2mFOKge tVisnNDs67BQkQelL5BH3xDB6qlAxIsauKSdCP1u9RbVtQwR23XLMyLlcpNGXB6Iy+9V 6h5c6qltYh+VvFXjhEhMAVMkoOH0kzg/O/VFN9flkcaesv4gVW663QWu5QmKnZxZelhW VxA2ruZokQ/13Y4W1iK9D7QTZPDstoYSMzJe7Ut6tGpbcqCR8J9/BzhJDaPnbWSYALlO 0wLQY1QKd5mVg7H0581ixiyR8HIbQcWET5YJk7fGoy/pG71Z81LUP2b0nT4PvguFw6cc 5C1w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692656425; x=1693261225; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=am444WNrS3h8wlVCx2H3MlRIk5MPSUP4lUnh4kSEX7U=; b=ZCsuJnjjHMwGMuF/DE12/VTHoVuY4YDLOEHMG2uCv51GpfEHZ22qZ/hbFlL/edwAY0 pzLdnq2Hx7MmsQZ/PONCLOTDtELNjnuP670ke4eGd6ojhTAV7zZLBEA4ZQb32t8vII7V fng+NMiDCWU4t0pR8bmEUQLjgzUfOM5iqy5nFiCa/U94mesh7+2EvPLLTCSXgpC8MQOK 5I/T89+Kol6NDsHIFlYJ3TihBIrerpTiI+M5S5s9cV9XbF3YIZchP6oo1dDE0fCJxgJa rNTxmZaQ1cjs+s0jSDnuieMwfuEp0aqb9i9c6YDzAyWgc6MIcOwl8wOqhsLE3H9yZyb+ 1gTw== X-Gm-Message-State: AOJu0YzYRyAqBWrg+GYvnyBafQT696iHy3Grl/caiF7vvoKEwDdf4iZs 3zR7V3DToBcuqhOsvrSGyXzN8A== X-Google-Smtp-Source: AGHT+IE7nTgt2xWXl939ycCQCouQu+JbBrOJvWeo+TRBWSL8R/kRvPsatTTyirJH2mgjFLatJZcvYQ== X-Received: by 2002:a7b:cb95:0:b0:3fe:1967:447c with SMTP id m21-20020a7bcb95000000b003fe1967447cmr5850301wmi.9.1692656425088; Mon, 21 Aug 2023 15:20:25 -0700 (PDT) Received: from airbuntu.. (host109-151-228-137.range109-151.btcentralplus.com. [109.151.228.137]) by smtp.gmail.com with ESMTPSA id hn40-20020a05600ca3a800b003fe61c33df5sm17751154wmb.3.2023.08.21.15.20.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 21 Aug 2023 15:20:24 -0700 (PDT) From: Qais Yousef To: stable@vger.kernel.org Cc: Juri Lelli , Waiman Long , Tejun Heo , Dietmar Eggemann , Peter Zijlstra , Vincent Guittot , Ingo Molnar , Hao Luo , John Stultz , cgroups@vger.kernel.org, linux-kernel@vger.kernel.org, Qais Yousef Subject: [PATCH 5/6] sched/deadline: Create DL BW alloc, free & check overflow interface Date: Mon, 21 Aug 2023 23:19:55 +0100 Message-Id: <20230821221956.698117-6-qyousef@layalina.io> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230821221956.698117-1-qyousef@layalina.io> References: <20230821221956.698117-1-qyousef@layalina.io> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Dietmar Eggemann commit 85989106feb734437e2d598b639991b9185a43a6 upstream. While moving a set of tasks between exclusive cpusets, cpuset_can_attach() -> task_can_attach() calls dl_cpu_busy(..., p) for DL BW overflow checking and per-task DL BW allocation on the destination root_domain for the DL tasks in this set. This approach has the issue of not freeing already allocated DL BW in the following error cases: (1) The set of tasks includes multiple DL tasks and DL BW overflow checking fails for one of the subsequent DL tasks. (2) Another controller next to the cpuset controller which is attached to the same cgroup fails in its can_attach(). To address this problem rework dl_cpu_busy(): (1) Split it into dl_bw_check_overflow() & dl_bw_alloc() and add a dedicated dl_bw_free(). (2) dl_bw_alloc() & dl_bw_free() take a `u64 dl_bw` parameter instead of a `struct task_struct *p` used in dl_cpu_busy(). This allows to allocate DL BW for a set of tasks too rather than only for a single task. Signed-off-by: Dietmar Eggemann Signed-off-by: Juri Lelli Signed-off-by: Tejun Heo (cherry picked from commit 85989106feb734437e2d598b639991b9185a43a6) Signed-off-by: Qais Yousef (Google) --- include/linux/sched.h | 2 ++ kernel/sched/core.c | 4 ++-- kernel/sched/deadline.c | 53 +++++++++++++++++++++++++++++++---------- kernel/sched/sched.h | 2 +- 4 files changed, 45 insertions(+), 16 deletions(-) diff --git a/include/linux/sched.h b/include/linux/sched.h index eed5d65b8d1f..0bee06542450 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -1853,6 +1853,8 @@ current_restore_flags(unsigned long orig_flags, unsig= ned long flags) =20 extern int cpuset_cpumask_can_shrink(const struct cpumask *cur, const stru= ct cpumask *trial); extern int task_can_attach(struct task_struct *p, const struct cpumask *cs= _effective_cpus); +extern int dl_bw_alloc(int cpu, u64 dl_bw); +extern void dl_bw_free(int cpu, u64 dl_bw); #ifdef CONFIG_SMP extern void do_set_cpus_allowed(struct task_struct *p, const struct cpumas= k *new_mask); extern int set_cpus_allowed_ptr(struct task_struct *p, const struct cpumas= k *new_mask); diff --git a/kernel/sched/core.c b/kernel/sched/core.c index 72cd267e3821..50fab8114260 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -9319,7 +9319,7 @@ int task_can_attach(struct task_struct *p, =20 if (unlikely(cpu >=3D nr_cpu_ids)) return -EINVAL; - ret =3D dl_cpu_busy(cpu, p); + ret =3D dl_bw_alloc(cpu, p->dl.dl_bw); } =20 out: @@ -9604,7 +9604,7 @@ static void cpuset_cpu_active(void) static int cpuset_cpu_inactive(unsigned int cpu) { if (!cpuhp_tasks_frozen) { - int ret =3D dl_cpu_busy(cpu, NULL); + int ret =3D dl_bw_check_overflow(cpu); =20 if (ret) return ret; diff --git a/kernel/sched/deadline.c b/kernel/sched/deadline.c index e11de074a6fd..166c3e6eae61 100644 --- a/kernel/sched/deadline.c +++ b/kernel/sched/deadline.c @@ -3058,26 +3058,38 @@ int dl_cpuset_cpumask_can_shrink(const struct cpuma= sk *cur, return ret; } =20 -int dl_cpu_busy(int cpu, struct task_struct *p) +enum dl_bw_request { + dl_bw_req_check_overflow =3D 0, + dl_bw_req_alloc, + dl_bw_req_free +}; + +static int dl_bw_manage(enum dl_bw_request req, int cpu, u64 dl_bw) { - unsigned long flags, cap; + unsigned long flags; struct dl_bw *dl_b; - bool overflow; + bool overflow =3D 0; =20 rcu_read_lock_sched(); dl_b =3D dl_bw_of(cpu); raw_spin_lock_irqsave(&dl_b->lock, flags); - cap =3D dl_bw_capacity(cpu); - overflow =3D __dl_overflow(dl_b, cap, 0, p ? p->dl.dl_bw : 0); =20 - if (!overflow && p) { - /* - * We reserve space for this task in the destination - * root_domain, as we can't fail after this point. - * We will free resources in the source root_domain - * later on (see set_cpus_allowed_dl()). - */ - __dl_add(dl_b, p->dl.dl_bw, dl_bw_cpus(cpu)); + if (req =3D=3D dl_bw_req_free) { + __dl_sub(dl_b, dl_bw, dl_bw_cpus(cpu)); + } else { + unsigned long cap =3D dl_bw_capacity(cpu); + + overflow =3D __dl_overflow(dl_b, cap, 0, dl_bw); + + if (req =3D=3D dl_bw_req_alloc && !overflow) { + /* + * We reserve space in the destination + * root_domain, as we can't fail after this point. + * We will free resources in the source root_domain + * later on (see set_cpus_allowed_dl()). + */ + __dl_add(dl_b, dl_bw, dl_bw_cpus(cpu)); + } } =20 raw_spin_unlock_irqrestore(&dl_b->lock, flags); @@ -3085,6 +3097,21 @@ int dl_cpu_busy(int cpu, struct task_struct *p) =20 return overflow ? -EBUSY : 0; } + +int dl_bw_check_overflow(int cpu) +{ + return dl_bw_manage(dl_bw_req_check_overflow, cpu, 0); +} + +int dl_bw_alloc(int cpu, u64 dl_bw) +{ + return dl_bw_manage(dl_bw_req_alloc, cpu, dl_bw); +} + +void dl_bw_free(int cpu, u64 dl_bw) +{ + dl_bw_manage(dl_bw_req_free, cpu, dl_bw); +} #endif =20 #ifdef CONFIG_SCHED_DEBUG diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h index 81ac605b9cd5..ead91c1fbe75 100644 --- a/kernel/sched/sched.h +++ b/kernel/sched/sched.h @@ -330,7 +330,7 @@ extern void __getparam_dl(struct task_struct *p, struct= sched_attr *attr); extern bool __checkparam_dl(const struct sched_attr *attr); extern bool dl_param_changed(struct task_struct *p, const struct sched_att= r *attr); extern int dl_cpuset_cpumask_can_shrink(const struct cpumask *cur, const = struct cpumask *trial); -extern int dl_cpu_busy(int cpu, struct task_struct *p); +extern int dl_bw_check_overflow(int cpu); =20 #ifdef CONFIG_CGROUP_SCHED =20 --=20 2.34.1 From nobody Wed Dec 17 10:56:22 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 06F66EE49A0 for ; Mon, 21 Aug 2023 22:20:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231509AbjHUWUp (ORCPT ); Mon, 21 Aug 2023 18:20:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44860 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231376AbjHUWUi (ORCPT ); Mon, 21 Aug 2023 18:20:38 -0400 Received: from mail-wm1-x32a.google.com (mail-wm1-x32a.google.com [IPv6:2a00:1450:4864:20::32a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A4743DB for ; Mon, 21 Aug 2023 15:20:27 -0700 (PDT) Received: by mail-wm1-x32a.google.com with SMTP id 5b1f17b1804b1-3fbd33a57b6so36693075e9.2 for ; Mon, 21 Aug 2023 15:20:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=layalina-io.20221208.gappssmtp.com; s=20221208; t=1692656426; x=1693261226; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=ddy6FCEY5hzeS0746oHXREKMbN9QVr6NPEwblklvNZo=; b=4LGFXD8A8ENlsep9RQwGl10vPxO88NsvzMgXD+9eNRZ/7DkjwMT1Zi45Y77ZHu5j35 fAR/SWi67fTJGy6DPHA5dGI0BYGOP9hZRMzlgCTW8qYqB9Ph1w4Hecrw80DG03EL+91s KANRNANdrqCmfVVCsebcxcdAsgsG82MH/roQ1LK+1P2+qhUN+FO31C/FheHaizkfb1Lj EB7ETBvwnb85T1LCOCXiKyHbjloZva22BLI3QZ5anM/mQ6CY4lpl+j0BzBzOZWPDxbMm p0AnclTj2DwBoi/mLAlySk0v3njaL+AL0rpvxPoGFmFSd/hq2dKwRHgy0p4VXMoPEwIz d4Mg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692656426; x=1693261226; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ddy6FCEY5hzeS0746oHXREKMbN9QVr6NPEwblklvNZo=; b=PLpmv/fQMmkgzD5dkK3UWA24gF1WAI8RSyeR5GTqwV9oy+WSopdQ0gg14fgZhoej/U 3WGRRFsbb+TazPiaOSI4rfyWQmADDbuwNhbtFyzI0EAR7/T+KbNtmzfP+FIz5HEV64lw y8QDi+1U1ZRUhFmotNU6wnpuirMpGHvvqiJynY2HTsm+v56zZ2Dd5HBTJ7uqois+I0zs ZQWw2QAVuJyqW3+BvDFNIIA3QQ1MGtzU57UP7f5jwZ0fp+JmYFNuvrzbQm1A2jBmrpPy K8GYnDMvIEDzo+exHEHGFGfzUTLhhgCIOeBOctDqjZHHg5q2X7ik/a8XqWlxlrtG5mpO i7dg== X-Gm-Message-State: AOJu0YwZSFvIUzwJS/0NYoo2/Tth9Vd2JdTR74Xh6uxxV4C2WR2MM5RV /z1NcxxEE9VCkXOPrBNNwLJZJw== X-Google-Smtp-Source: AGHT+IGuL4lJ6a+Hcvj9IHHW0ZedYrdkuqW75STmYvkP3MfMFqU7cHV/ivmQuIUtxDCkFoP1IOnzQg== X-Received: by 2002:a05:600c:214a:b0:3fc:25:ced6 with SMTP id v10-20020a05600c214a00b003fc0025ced6mr6358964wml.13.1692656426229; Mon, 21 Aug 2023 15:20:26 -0700 (PDT) Received: from airbuntu.. (host109-151-228-137.range109-151.btcentralplus.com. [109.151.228.137]) by smtp.gmail.com with ESMTPSA id hn40-20020a05600ca3a800b003fe61c33df5sm17751154wmb.3.2023.08.21.15.20.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 21 Aug 2023 15:20:25 -0700 (PDT) From: Qais Yousef To: stable@vger.kernel.org Cc: Juri Lelli , Waiman Long , Tejun Heo , Dietmar Eggemann , Peter Zijlstra , Vincent Guittot , Ingo Molnar , Hao Luo , John Stultz , cgroups@vger.kernel.org, linux-kernel@vger.kernel.org, Qais Yousef Subject: [PATCH 6/6] cgroup/cpuset: Free DL BW in case can_attach() fails Date: Mon, 21 Aug 2023 23:19:56 +0100 Message-Id: <20230821221956.698117-7-qyousef@layalina.io> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230821221956.698117-1-qyousef@layalina.io> References: <20230821221956.698117-1-qyousef@layalina.io> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Dietmar Eggemann commit 2ef269ef1ac006acf974793d975539244d77b28f upstream. cpuset_can_attach() can fail. Postpone DL BW allocation until all tasks have been checked. DL BW is not allocated per-task but as a sum over all DL tasks migrating. If multiple controllers are attached to the cgroup next to the cpuset controller a non-cpuset can_attach() can fail. In this case free DL BW in cpuset_cancel_attach(). Finally, update cpuset DL task count (nr_deadline_tasks) only in cpuset_attach(). Suggested-by: Waiman Long Signed-off-by: Dietmar Eggemann Signed-off-by: Juri Lelli Reviewed-by: Waiman Long Signed-off-by: Tejun Heo (cherry picked from commit 2ef269ef1ac006acf974793d975539244d77b28f) Signed-off-by: Qais Yousef (Google) --- include/linux/sched.h | 2 +- kernel/cgroup/cpuset.c | 53 ++++++++++++++++++++++++++++++++++++++---- kernel/sched/core.c | 17 ++------------ 3 files changed, 51 insertions(+), 21 deletions(-) diff --git a/include/linux/sched.h b/include/linux/sched.h index 0bee06542450..2553918f0b61 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -1852,7 +1852,7 @@ current_restore_flags(unsigned long orig_flags, unsig= ned long flags) } =20 extern int cpuset_cpumask_can_shrink(const struct cpumask *cur, const stru= ct cpumask *trial); -extern int task_can_attach(struct task_struct *p, const struct cpumask *cs= _effective_cpus); +extern int task_can_attach(struct task_struct *p); extern int dl_bw_alloc(int cpu, u64 dl_bw); extern void dl_bw_free(int cpu, u64 dl_bw); #ifdef CONFIG_SMP diff --git a/kernel/cgroup/cpuset.c b/kernel/cgroup/cpuset.c index b7168970fff2..2c76fcd9f0bc 100644 --- a/kernel/cgroup/cpuset.c +++ b/kernel/cgroup/cpuset.c @@ -198,6 +198,8 @@ struct cpuset { * know when to rebuild associated root domain bandwidth information. */ int nr_deadline_tasks; + int nr_migrate_dl_tasks; + u64 sum_migrate_dl_bw; =20 /* Invalid partition error code, not lock protected */ enum prs_errcode prs_err; @@ -2496,16 +2498,23 @@ static int cpuset_can_attach_check(struct cpuset *c= s) return 0; } =20 +static void reset_migrate_dl_data(struct cpuset *cs) +{ + cs->nr_migrate_dl_tasks =3D 0; + cs->sum_migrate_dl_bw =3D 0; +} + /* Called by cgroups to determine if a cpuset is usable; cpuset_mutex held= */ static int cpuset_can_attach(struct cgroup_taskset *tset) { struct cgroup_subsys_state *css; - struct cpuset *cs; + struct cpuset *cs, *oldcs; struct task_struct *task; int ret; =20 /* used later by cpuset_attach() */ cpuset_attach_old_cs =3D task_cs(cgroup_taskset_first(tset, &css)); + oldcs =3D cpuset_attach_old_cs; cs =3D css_cs(css); =20 mutex_lock(&cpuset_mutex); @@ -2516,7 +2525,7 @@ static int cpuset_can_attach(struct cgroup_taskset *t= set) goto out_unlock; =20 cgroup_taskset_for_each(task, css, tset) { - ret =3D task_can_attach(task, cs->effective_cpus); + ret =3D task_can_attach(task); if (ret) goto out_unlock; ret =3D security_task_setscheduler(task); @@ -2524,11 +2533,31 @@ static int cpuset_can_attach(struct cgroup_taskset = *tset) goto out_unlock; =20 if (dl_task(task)) { - cs->nr_deadline_tasks++; - cpuset_attach_old_cs->nr_deadline_tasks--; + cs->nr_migrate_dl_tasks++; + cs->sum_migrate_dl_bw +=3D task->dl.dl_bw; } } =20 + if (!cs->nr_migrate_dl_tasks) + goto out_success; + + if (!cpumask_intersects(oldcs->effective_cpus, cs->effective_cpus)) { + int cpu =3D cpumask_any_and(cpu_active_mask, cs->effective_cpus); + + if (unlikely(cpu >=3D nr_cpu_ids)) { + reset_migrate_dl_data(cs); + ret =3D -EINVAL; + goto out_unlock; + } + + ret =3D dl_bw_alloc(cpu, cs->sum_migrate_dl_bw); + if (ret) { + reset_migrate_dl_data(cs); + goto out_unlock; + } + } + +out_success: /* * Mark attach is in progress. This makes validate_change() fail * changes which zero cpus/mems_allowed. @@ -2551,6 +2580,14 @@ static void cpuset_cancel_attach(struct cgroup_tasks= et *tset) cs->attach_in_progress--; if (!cs->attach_in_progress) wake_up(&cpuset_attach_wq); + + if (cs->nr_migrate_dl_tasks) { + int cpu =3D cpumask_any(cs->effective_cpus); + + dl_bw_free(cpu, cs->sum_migrate_dl_bw); + reset_migrate_dl_data(cs); + } + mutex_unlock(&cpuset_mutex); } =20 @@ -2651,6 +2688,12 @@ static void cpuset_attach(struct cgroup_taskset *tse= t) out: cs->old_mems_allowed =3D cpuset_attach_nodemask_to; =20 + if (cs->nr_migrate_dl_tasks) { + cs->nr_deadline_tasks +=3D cs->nr_migrate_dl_tasks; + oldcs->nr_deadline_tasks -=3D cs->nr_migrate_dl_tasks; + reset_migrate_dl_data(cs); + } + cs->attach_in_progress--; if (!cs->attach_in_progress) wake_up(&cpuset_attach_wq); @@ -3330,7 +3373,7 @@ static int cpuset_can_fork(struct task_struct *task, = struct css_set *cset) if (ret) goto out_unlock; =20 - ret =3D task_can_attach(task, cs->effective_cpus); + ret =3D task_can_attach(task); if (ret) goto out_unlock; =20 diff --git a/kernel/sched/core.c b/kernel/sched/core.c index 50fab8114260..90005760003f 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -9294,8 +9294,7 @@ int cpuset_cpumask_can_shrink(const struct cpumask *c= ur, return ret; } =20 -int task_can_attach(struct task_struct *p, - const struct cpumask *cs_effective_cpus) +int task_can_attach(struct task_struct *p) { int ret =3D 0; =20 @@ -9308,21 +9307,9 @@ int task_can_attach(struct task_struct *p, * success of set_cpus_allowed_ptr() on all attached tasks * before cpus_mask may be changed. */ - if (p->flags & PF_NO_SETAFFINITY) { + if (p->flags & PF_NO_SETAFFINITY) ret =3D -EINVAL; - goto out; - } - - if (dl_task(p) && !cpumask_intersects(task_rq(p)->rd->span, - cs_effective_cpus)) { - int cpu =3D cpumask_any_and(cpu_active_mask, cs_effective_cpus); =20 - if (unlikely(cpu >=3D nr_cpu_ids)) - return -EINVAL; - ret =3D dl_bw_alloc(cpu, p->dl.dl_bw); - } - -out: return ret; } =20 --=20 2.34.1