From nobody Sun Jun 14 21:06:23 2026 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 8F421C433FE for ; Wed, 11 May 2022 14:31:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244797AbiEKOb3 (ORCPT ); Wed, 11 May 2022 10:31:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45768 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244746AbiEKObF (ORCPT ); Wed, 11 May 2022 10:31:05 -0400 Received: from outbound-smtp15.blacknight.com (outbound-smtp15.blacknight.com [46.22.139.232]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D56C527CF1 for ; Wed, 11 May 2022 07:31:02 -0700 (PDT) Received: from mail.blacknight.com (pemlinmail03.blacknight.ie [81.17.254.16]) by outbound-smtp15.blacknight.com (Postfix) with ESMTPS id D62141C4E62 for ; Wed, 11 May 2022 15:31:00 +0100 (IST) Received: (qmail 6634 invoked from network); 11 May 2022 14:31:00 -0000 Received: from unknown (HELO morpheus.112glenside.lan) (mgorman@techsingularity.net@[84.203.198.246]) by 81.17.254.9 with ESMTPA; 11 May 2022 14:31:00 -0000 From: Mel Gorman To: Peter Zijlstra Cc: Ingo Molnar , Vincent Guittot , Valentin Schneider , Aubrey Li , LKML , Mel Gorman Subject: [PATCH 1/4] sched/numa: Initialise numa_migrate_retry Date: Wed, 11 May 2022 15:30:35 +0100 Message-Id: <20220511143038.4620-2-mgorman@techsingularity.net> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220511143038.4620-1-mgorman@techsingularity.net> References: <20220511143038.4620-1-mgorman@techsingularity.net> 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" On clone, numa_migrate_retry is inherited from the parent which means that the first NUMA placement of a task is non-deterministic. This affects when load balancing recognises numa tasks and whether to migrate "regular", "remote" or "all" tasks between NUMA scheduler domains. Signed-off-by: Mel Gorman Tested-by: K Prateek Nayak --- kernel/sched/fair.c | 1 + 1 file changed, 1 insertion(+) diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c index d4bd299d67ab..867806a57119 100644 --- a/kernel/sched/fair.c +++ b/kernel/sched/fair.c @@ -2873,6 +2873,7 @@ void init_numa_balancing(unsigned long clone_flags, s= truct task_struct *p) p->node_stamp =3D 0; p->numa_scan_seq =3D mm ? mm->numa_scan_seq : 0; p->numa_scan_period =3D sysctl_numa_balancing_scan_delay; + p->numa_migrate_retry =3D 0; /* Protect against double add, see task_tick_numa and task_numa_work */ p->numa_work.next =3D &p->numa_work; p->numa_faults =3D NULL; --=20 2.34.1 From nobody Sun Jun 14 21:06:23 2026 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 57E4FC433EF for ; Wed, 11 May 2022 14:31:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244827AbiEKObh (ORCPT ); Wed, 11 May 2022 10:31:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46270 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244782AbiEKObP (ORCPT ); Wed, 11 May 2022 10:31:15 -0400 Received: from outbound-smtp60.blacknight.com (outbound-smtp60.blacknight.com [46.22.136.244]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 533F250E15 for ; Wed, 11 May 2022 07:31:12 -0700 (PDT) Received: from mail.blacknight.com (pemlinmail03.blacknight.ie [81.17.254.16]) by outbound-smtp60.blacknight.com (Postfix) with ESMTPS id 793EDFAC9E for ; Wed, 11 May 2022 15:31:11 +0100 (IST) Received: (qmail 7479 invoked from network); 11 May 2022 14:31:11 -0000 Received: from unknown (HELO morpheus.112glenside.lan) (mgorman@techsingularity.net@[84.203.198.246]) by 81.17.254.9 with ESMTPA; 11 May 2022 14:31:11 -0000 From: Mel Gorman To: Peter Zijlstra Cc: Ingo Molnar , Vincent Guittot , Valentin Schneider , Aubrey Li , LKML , Mel Gorman Subject: [PATCH 2/4] sched/numa: Do not swap tasks between nodes when spare capacity is available Date: Wed, 11 May 2022 15:30:36 +0100 Message-Id: <20220511143038.4620-3-mgorman@techsingularity.net> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220511143038.4620-1-mgorman@techsingularity.net> References: <20220511143038.4620-1-mgorman@techsingularity.net> 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" If a destination node has spare capacity but there is an imbalance then two tasks are selected for swapping. If the tasks have no numa group or are within the same NUMA group, it's simply shuffling tasks around without having any impact on the compute imbalance. Instead, it's just punishing one task to help another. Signed-off-by: Mel Gorman Tested-by: K Prateek Nayak --- kernel/sched/fair.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c index 867806a57119..03b1ad79d47d 100644 --- a/kernel/sched/fair.c +++ b/kernel/sched/fair.c @@ -1778,6 +1778,15 @@ static bool task_numa_compare(struct task_numa_env *= env, */ cur_ng =3D rcu_dereference(cur->numa_group); if (cur_ng =3D=3D p_ng) { + /* + * Do not swap within a group or between tasks that have + * no group if there is spare capacity. Swapping does + * not address the load imbalance and helps one task at + * the cost of punishing another. + */ + if (env->dst_stats.node_type =3D=3D node_has_spare) + goto unlock; + imp =3D taskimp + task_weight(cur, env->src_nid, dist) - task_weight(cur, env->dst_nid, dist); /* --=20 2.34.1 From nobody Sun Jun 14 21:06:23 2026 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 60DACC433EF for ; Wed, 11 May 2022 14:31:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244818AbiEKObo (ORCPT ); Wed, 11 May 2022 10:31:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47154 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244794AbiEKOb0 (ORCPT ); Wed, 11 May 2022 10:31:26 -0400 Received: from outbound-smtp41.blacknight.com (outbound-smtp41.blacknight.com [46.22.139.224]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3E19059943 for ; Wed, 11 May 2022 07:31:23 -0700 (PDT) Received: from mail.blacknight.com (pemlinmail03.blacknight.ie [81.17.254.16]) by outbound-smtp41.blacknight.com (Postfix) with ESMTPS id 0F56D2A6E for ; Wed, 11 May 2022 15:31:22 +0100 (IST) Received: (qmail 7989 invoked from network); 11 May 2022 14:31:21 -0000 Received: from unknown (HELO morpheus.112glenside.lan) (mgorman@techsingularity.net@[84.203.198.246]) by 81.17.254.9 with ESMTPA; 11 May 2022 14:31:21 -0000 From: Mel Gorman To: Peter Zijlstra Cc: Ingo Molnar , Vincent Guittot , Valentin Schneider , Aubrey Li , LKML , Mel Gorman Subject: [PATCH 3/4] sched/numa: Apply imbalance limitations consistently Date: Wed, 11 May 2022 15:30:37 +0100 Message-Id: <20220511143038.4620-4-mgorman@techsingularity.net> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220511143038.4620-1-mgorman@techsingularity.net> References: <20220511143038.4620-1-mgorman@techsingularity.net> 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" The imbalance limitations are applied inconsistently at fork time and at runtime. At fork, a new task can remain local until there are too many running tasks even if the degree of imbalance is larger than NUMA_IMBALANCE_MIN which is different to runtime. Secondly, the imbalance figure used during load balancing is different to the one used at NUMA placement. Load balancing uses the number of tasks that must move to restore imbalance where as NUMA balancing uses the total imbalance. In combination, it is possible for a parallel workload that uses a small number of CPUs without applying scheduler policies to have very variable run-to-run performance. Signed-off-by: Mel Gorman Reported-by: kernel test robot Tested-by: K Prateek Nayak --- kernel/sched/fair.c | 49 ++++++++++++++++++++++++++------------------- 1 file changed, 28 insertions(+), 21 deletions(-) diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c index 03b1ad79d47d..602c05b22805 100644 --- a/kernel/sched/fair.c +++ b/kernel/sched/fair.c @@ -9108,6 +9108,24 @@ static inline bool allow_numa_imbalance(int running,= int imb_numa_nr) return running <=3D imb_numa_nr; } =20 +#define NUMA_IMBALANCE_MIN 2 + +static inline long adjust_numa_imbalance(int imbalance, + int dst_running, int imb_numa_nr) +{ + if (!allow_numa_imbalance(dst_running, imb_numa_nr)) + return imbalance; + + /* + * Allow a small imbalance based on a simple pair of communicating + * tasks that remain local when the destination is lightly loaded. + */ + if (imbalance <=3D NUMA_IMBALANCE_MIN) + return 0; + + return imbalance; +} + /* * find_idlest_group() finds and returns the least busy CPU group within t= he * domain. @@ -9245,8 +9263,12 @@ find_idlest_group(struct sched_domain *sd, struct ta= sk_struct *p, int this_cpu) * allowed. If there is a real need of migration, * periodic load balance will take care of it. */ - if (allow_numa_imbalance(local_sgs.sum_nr_running + 1, sd->imb_numa_nr)) + imbalance =3D abs(local_sgs.idle_cpus - idlest_sgs.idle_cpus); + if (!adjust_numa_imbalance(imbalance, + local_sgs.sum_nr_running + 1, + sd->imb_numa_nr)) { return NULL; + } } =20 /* @@ -9334,24 +9356,6 @@ static inline void update_sd_lb_stats(struct lb_env = *env, struct sd_lb_stats *sd } } =20 -#define NUMA_IMBALANCE_MIN 2 - -static inline long adjust_numa_imbalance(int imbalance, - int dst_running, int imb_numa_nr) -{ - if (!allow_numa_imbalance(dst_running, imb_numa_nr)) - return imbalance; - - /* - * Allow a small imbalance based on a simple pair of communicating - * tasks that remain local when the destination is lightly loaded. - */ - if (imbalance <=3D NUMA_IMBALANCE_MIN) - return 0; - - return imbalance; -} - /** * calculate_imbalance - Calculate the amount of imbalance present within = the * groups of a given sched_domain during load balance. @@ -9436,7 +9440,7 @@ static inline void calculate_imbalance(struct lb_env = *env, struct sd_lb_stats *s */ env->migration_type =3D migrate_task; lsub_positive(&nr_diff, local->sum_nr_running); - env->imbalance =3D nr_diff >> 1; + env->imbalance =3D nr_diff; } else { =20 /* @@ -9445,7 +9449,7 @@ static inline void calculate_imbalance(struct lb_env = *env, struct sd_lb_stats *s */ env->migration_type =3D migrate_task; env->imbalance =3D max_t(long, 0, (local->idle_cpus - - busiest->idle_cpus) >> 1); + busiest->idle_cpus)); } =20 /* Consider allowing a small imbalance between NUMA groups */ @@ -9454,6 +9458,9 @@ static inline void calculate_imbalance(struct lb_env = *env, struct sd_lb_stats *s local->sum_nr_running + 1, env->sd->imb_numa_nr); } =20 + /* Number of tasks to move to restore balance */ + env->imbalance >>=3D 1; + return; } =20 --=20 2.34.1 From nobody Sun Jun 14 21:06:23 2026 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 7627FC433EF for ; Wed, 11 May 2022 14:32:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244682AbiEKOb6 (ORCPT ); Wed, 11 May 2022 10:31:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48084 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244830AbiEKObj (ORCPT ); Wed, 11 May 2022 10:31:39 -0400 Received: from outbound-smtp17.blacknight.com (outbound-smtp17.blacknight.com [46.22.139.234]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 294285DBFB for ; Wed, 11 May 2022 07:31:33 -0700 (PDT) Received: from mail.blacknight.com (pemlinmail03.blacknight.ie [81.17.254.16]) by outbound-smtp17.blacknight.com (Postfix) with ESMTPS id 857051C4F25 for ; Wed, 11 May 2022 15:31:32 +0100 (IST) Received: (qmail 8665 invoked from network); 11 May 2022 14:31:32 -0000 Received: from unknown (HELO morpheus.112glenside.lan) (mgorman@techsingularity.net@[84.203.198.246]) by 81.17.254.9 with ESMTPA; 11 May 2022 14:31:32 -0000 From: Mel Gorman To: Peter Zijlstra Cc: Ingo Molnar , Vincent Guittot , Valentin Schneider , Aubrey Li , LKML , Mel Gorman Subject: [PATCH 4/4] sched/numa: Adjust imb_numa_nr to a better approximation of memory channels Date: Wed, 11 May 2022 15:30:38 +0100 Message-Id: <20220511143038.4620-5-mgorman@techsingularity.net> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220511143038.4620-1-mgorman@techsingularity.net> References: <20220511143038.4620-1-mgorman@techsingularity.net> 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" For a single LLC per node, a NUMA imbalance is allowed up until 25% of CPUs sharing a node could be active. One intent of the cut-off is to avoid an imbalance of memory channels but there is no topological information based on active memory channels. Furthermore, there can be differences between nodes depending on the number of populated DIMMs. A cut-off of 25% was arbitrary but generally worked. It does have a severe corner cases though when an parallel workload is using 25% of all available CPUs over-saturates memory channels. This can happen due to the initial forking of tasks that get pulled more to one node after early wakeups (e.g. a barrier synchronisation) that is not quickly corrected by the load balancer. The LB may fail to act quickly as the parallel tasks are considered to be poor migrate candidates due to locality or cache hotness. On a range of modern Intel CPUs, 12.5% appears to be a better cut-off assuming all memory channels are populated and is used as the new cut-off point. A minimum of 1 is specified to allow a communicating pair to remain local even for CPUs with low numbers of cores. For modern AMDs, there are multiple LLCs and are not affected. Signed-off-by: Mel Gorman Tested-by: K Prateek Nayak --- kernel/sched/topology.c | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/kernel/sched/topology.c b/kernel/sched/topology.c index 810750e62118..2740e245cb37 100644 --- a/kernel/sched/topology.c +++ b/kernel/sched/topology.c @@ -2295,23 +2295,30 @@ build_sched_domains(const struct cpumask *cpu_map, = struct sched_domain_attr *att =20 /* * For a single LLC per node, allow an - * imbalance up to 25% of the node. This is an - * arbitrary cutoff based on SMT-2 to balance - * between memory bandwidth and avoiding - * premature sharing of HT resources and SMT-4 - * or SMT-8 *may* benefit from a different - * cutoff. + * imbalance up to 12.5% of the node. This is + * arbitrary cutoff based two factors -- SMT and + * memory channels. For SMT-2, the intent is to + * avoid premature sharing of HT resources but + * SMT-4 or SMT-8 *may* benefit from a different + * cutoff. For memory channels, this is a very + * rough estimate of how many channels may be + * active and is based on recent CPUs with + * many cores. * * For multiple LLCs, allow an imbalance * until multiple tasks would share an LLC * on one node while LLCs on another node - * remain idle. + * remain idle. This assumes that there are + * enough logical CPUs per LLC to avoid SMT + * factors and that there is a correlation + * between LLCs and memory channels. */ nr_llcs =3D sd->span_weight / child->span_weight; if (nr_llcs =3D=3D 1) - imb =3D sd->span_weight >> 2; + imb =3D sd->span_weight >> 3; else imb =3D nr_llcs; + imb =3D max(1U, imb); sd->imb_numa_nr =3D imb; =20 /* Set span based on the first NUMA domain. */ --=20 2.34.1