Received: by 2002:a05:6602:18e:0:0:0:0 with SMTP id m14csp407015ioo; Sat, 21 May 2022 02:45:48 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzk34AZrEu/hB7TKqP9F3qGrqZh16rCCPnJE6LWcKi/JmsXld1adq8HzTlBGWnaVE283M/s X-Received: by 2002:a17:907:9628:b0:6f6:b20c:bd7f with SMTP id gb40-20020a170907962800b006f6b20cbd7fmr11719630ejc.719.1653126348040; Sat, 21 May 2022 02:45:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1653126348; cv=none; d=google.com; s=arc-20160816; b=PX4233GliVlrYf1+s51+IfDoLGWPeefFAvTXQaIsqmOPX0Q4cGNnBZZFK1Zk7wcqrb OtdbFd2IswphIhLx9sGW9GPRtLDPKknynRe3X+nDE/4MmGcUro4gCkEbX2wtnzARqd/2 p7TWA+UcIKXIsE7Q4FPfDkUrx0uFLlImsy4SQVodpDomCgY/bCUfp3gg/SE+8FzDM59F Q2sw3O6lTKatb4q8Mszr/azSovZUeJEBunPOHqY3utzMWPdSL8FavhifsU9xquCJzWql ci/dpkKtu76sWvUvNkYjkQxlE1tulPiabasEGWGF2EgVTG0OAdMFc5LgZ3m7hlrJPW9w 8Uhw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=OS8NR8i9uxRYeaJ37QNH5DDI1/tItl77uIiu4YdB1EA=; b=YJ/VasAYAPpTAWyUSbao/UE1NYoDFR1iUbtcL5IilqBZeE7nqupQm3NO+/wEG4ozGs seam/30to2m+yROjvdODga6G7RVpsXa6H9IOlZXmPVGxropKZjAlD5Sk4pw2prTzbGSQ oPY70jNWHFqErYqHyU7mgPLDEbQygqesDgXxAp+k2L+x4y3w4RGLHveHIhtiwXcrh9f4 LhS/LII2LZpstmCGtKdUFo0MTopzabpogZ7kwSzt+COp7dPrwsEBIrKquoFDQbl+S1d1 DSoyD+oIYYlop5loO/MIY7Ye0aRcRLeyo4AX5NnBRwHsISyARI/3kRW09UKdIYG1Y08n +0QA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id qa20-20020a170907869400b006f38baab73fsi11179616ejc.530.2022.05.21.02.45.14; Sat, 21 May 2022 02:45:48 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1348202AbiETKgP (ORCPT + 99 others); Fri, 20 May 2022 06:36:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40050 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1348246AbiETKgD (ORCPT ); Fri, 20 May 2022 06:36:03 -0400 Received: from outbound-smtp24.blacknight.com (outbound-smtp24.blacknight.com [81.17.249.192]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 25A873BA4D for ; Fri, 20 May 2022 03:36:01 -0700 (PDT) Received: from mail.blacknight.com (pemlinmail01.blacknight.ie [81.17.254.10]) by outbound-smtp24.blacknight.com (Postfix) with ESMTPS id 71C11C0BB4 for ; Fri, 20 May 2022 11:36:00 +0100 (IST) Received: (qmail 31180 invoked from network); 20 May 2022 10:36:00 -0000 Received: from unknown (HELO morpheus.112glenside.lan) (mgorman@techsingularity.net@[84.203.198.246]) by 81.17.254.9 with ESMTPA; 20 May 2022 10:36:00 -0000 From: Mel Gorman To: Peter Zijlstra Cc: Ingo Molnar , Vincent Guittot , Valentin Schneider , K Prateek Nayak , Aubrey Li , Ying Huang , LKML , Mel Gorman Subject: [PATCH 3/4] sched/numa: Apply imbalance limitations consistently Date: Fri, 20 May 2022 11:35:18 +0100 Message-Id: <20220520103519.1863-4-mgorman@techsingularity.net> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220520103519.1863-1-mgorman@techsingularity.net> References: <20220520103519.1863-1-mgorman@techsingularity.net> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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. [lkp@intel.com: Fix build breakage for arc-allyesconfig] Signed-off-by: Mel Gorman --- kernel/sched/fair.c | 81 +++++++++++++++++++++++++-------------------- 1 file changed, 45 insertions(+), 36 deletions(-) diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c index 03b1ad79d47d..0b3646be88b3 100644 --- a/kernel/sched/fair.c +++ b/kernel/sched/fair.c @@ -1043,6 +1043,33 @@ update_stats_curr_start(struct cfs_rq *cfs_rq, struct sched_entity *se) * Scheduling class queueing methods: */ +#ifdef CONFIG_NUMA +#define NUMA_IMBALANCE_MIN 2 + +static inline long +adjust_numa_imbalance(int imbalance, int dst_running, int imb_numa_nr) +{ + /* + * Allow a NUMA imbalance if busy CPUs is less than the maximum + * threshold. Above this threshold, individual tasks may be contending + * for both memory bandwidth and any shared HT resources. This is an + * approximation as the number of running tasks may not be related to + * the number of busy CPUs due to sched_setaffinity. + */ + if (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 <= NUMA_IMBALANCE_MIN) + return 0; + + return imbalance; +} +#endif /* CONFIG_NUMA */ + #ifdef CONFIG_NUMA_BALANCING /* * Approximate time to scan a full NUMA task in ms. The task scan period is @@ -1536,8 +1563,6 @@ struct task_numa_env { static unsigned long cpu_load(struct rq *rq); static unsigned long cpu_runnable(struct rq *rq); -static inline long adjust_numa_imbalance(int imbalance, - int dst_running, int imb_numa_nr); static inline enum numa_type numa_classify(unsigned int imbalance_pct, @@ -9098,16 +9123,6 @@ static bool update_pick_idlest(struct sched_group *idlest, return true; } -/* - * Allow a NUMA imbalance if busy CPUs is less than 25% of the domain. - * This is an approximation as the number of running tasks may not be - * related to the number of busy CPUs due to sched_setaffinity. - */ -static inline bool allow_numa_imbalance(int running, int imb_numa_nr) -{ - return running <= imb_numa_nr; -} - /* * find_idlest_group() finds and returns the least busy CPU group within the * domain. @@ -9224,6 +9239,7 @@ find_idlest_group(struct sched_domain *sd, struct task_struct *p, int this_cpu) break; case group_has_spare: +#ifdef CONFIG_NUMA if (sd->flags & SD_NUMA) { #ifdef CONFIG_NUMA_BALANCING int idlest_cpu; @@ -9237,7 +9253,7 @@ find_idlest_group(struct sched_domain *sd, struct task_struct *p, int this_cpu) idlest_cpu = cpumask_first(sched_group_span(idlest)); if (cpu_to_node(idlest_cpu) == p->numa_preferred_nid) return idlest; -#endif +#endif /* CONFIG_NUMA_BALANCING */ /* * Otherwise, keep the task close to the wakeup source * and improve locality if the number of running tasks @@ -9245,9 +9261,14 @@ find_idlest_group(struct sched_domain *sd, struct task_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 = 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; + } } +#endif /* CONFIG_NUMA */ /* * Select group with highest number of idle CPUs. We could also @@ -9334,24 +9355,6 @@ static inline void update_sd_lb_stats(struct lb_env *env, struct sd_lb_stats *sd } } -#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 <= 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 +9439,7 @@ static inline void calculate_imbalance(struct lb_env *env, struct sd_lb_stats *s */ env->migration_type = migrate_task; lsub_positive(&nr_diff, local->sum_nr_running); - env->imbalance = nr_diff >> 1; + env->imbalance = nr_diff; } else { /* @@ -9444,15 +9447,21 @@ static inline void calculate_imbalance(struct lb_env *env, struct sd_lb_stats *s * idle cpus. */ env->migration_type = migrate_task; - env->imbalance = max_t(long, 0, (local->idle_cpus - - busiest->idle_cpus) >> 1); + env->imbalance = max_t(long, 0, + (local->idle_cpus - busiest->idle_cpus)); } +#ifdef CONFIG_NUMA /* Consider allowing a small imbalance between NUMA groups */ if (env->sd->flags & SD_NUMA) { env->imbalance = adjust_numa_imbalance(env->imbalance, - local->sum_nr_running + 1, env->sd->imb_numa_nr); + local->sum_nr_running + 1, + env->sd->imb_numa_nr); } +#endif + + /* Number of tasks to move to restore balance */ + env->imbalance >>= 1; return; } -- 2.34.1