Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp3996469pxu; Wed, 9 Dec 2020 06:02:48 -0800 (PST) X-Google-Smtp-Source: ABdhPJzYbKOo/nO3oKH8GVf0Bj42lWGx/SwHH0FoUK+8+Ktqv7r/K5Z6LrbzrjFY71IHFKchBNKm X-Received: by 2002:a17:907:2070:: with SMTP id qp16mr2126956ejb.503.1607522568277; Wed, 09 Dec 2020 06:02:48 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1607522568; cv=none; d=google.com; s=arc-20160816; b=ROGeMJzM+0+XeMcEN5BPHDSeFgwe/utSmO54XSfSQqL9M4lgBEkFTRFIguMkKEzCKe 11kke7jtK+WPPLp1A2fjqh0JR4LoOawyCbmodbCmJ9yp6vnuHLd0IfIebQsQbCHk1a/0 QCwN4USZOyIlhM818PEDEe3QGfTxBJE06YaKu6irYBBvRY4HTmGxdDlD1gsBHb2F5VHw d99j2D5uqU/R5PWwMQGLlJvR9IQ+AC0o8ry0pVlmPOrqvQQuA74PZ8P7KsN2tWAHJCdb MEC80FtEpaYZxffNYp9dTUn07T7b/JAMvIfzL5+5tuxt5sMyAzNGet6o22aaWQT8JiMG NpZg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=+EBAMKQfu9uxdGLL7ad9C++uCGmU6KMz6ZVwSzSDIvo=; b=agmgWz2l/LY864YRhT0K63CpqN2kVB0ncxeEy5/xz5dFWJ7ws1RSdhF+mZzj9eiOfZ UE9s08QggYxaVGNXIuvseaD138wSjUk0Shg3cY8PJvQN2d4vrXJ22w/+DrhMdk53K9l4 6on4dQSAVdBssXhafDDXR90/gM6bFarTswdYEKq7h2TweO7JBe7ZY/dZZznsp2GNl6Pe f1vZp28Mqh1EG1H6Lf8wnGfxyZwff5BnqD2dzNSwfEYze3JFYPgsAo3a0DUd+CVbKTH0 GWZdSHbmJDB2mXOVvcrlnwxZx8g7F3JFDciQsTsPDLdKEljSDu0CZEAMpZRg2ex6YdrS a5bg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="rcQ/GRT3"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id k9si1012629edv.493.2020.12.09.06.02.25; Wed, 09 Dec 2020 06:02:48 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="rcQ/GRT3"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726738AbgLIIQ1 (ORCPT + 99 others); Wed, 9 Dec 2020 03:16:27 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35238 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725765AbgLIIQ0 (ORCPT ); Wed, 9 Dec 2020 03:16:26 -0500 Received: from mail-wm1-x343.google.com (mail-wm1-x343.google.com [IPv6:2a00:1450:4864:20::343]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2646EC0613CF for ; Wed, 9 Dec 2020 00:15:46 -0800 (PST) Received: by mail-wm1-x343.google.com with SMTP id g185so680302wmf.3 for ; Wed, 09 Dec 2020 00:15:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:content-transfer-encoding:in-reply-to :user-agent; bh=+EBAMKQfu9uxdGLL7ad9C++uCGmU6KMz6ZVwSzSDIvo=; b=rcQ/GRT3SQTlkVYKjzsg24YfDcevyGzwRysGfd55uIxz2gS2K0/WR7Q2EUkwOBrfzx 3G9NyaKB38jASjtZlkMlRlSU1VRRPxUOahkaMW3KO5DY3/Uz/VbvzRzFRJlOIX9v6IAw qcJb1jR11UoljoQ2cJQG1XasPO3U0l+xsnCcQ3CHafmeW38QjpHMpQQNeoEVo5CLdFmX ZBzR6UGg84xfCXmtOgN+Ia1FpHDSNuKIiax9EpKL1P0X8/GXx9l3fRTwaQDdIoksC+KB ciqWaHDNRjZVIXIerNPi1zmu4sPszQC7FNQ4FGp3DDT5o0olcyPgcbN++D5779+AHeOj ljlQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:content-transfer-encoding :in-reply-to:user-agent; bh=+EBAMKQfu9uxdGLL7ad9C++uCGmU6KMz6ZVwSzSDIvo=; b=rSSL41S/w1B+Eg4EpyUXzdeHQ+isruKnyvRCgMSbhQfIhWGncVk/oOFGhKcB6K5olG LnFDE+BAL0Bawyqq7KnRWKs2/hasipw4Mxvm9dXb79+rit4Evhm0seV64trYtv4POxoF P8dWl0wN6mSQTQu9lfUzZAbhFvVqCFRbCpDTWnTJ/EGgB8p8Unl4QdYcC+5xuQfYep2E eMQ337q3X8Sv9dvQl6LAbGpKAoxkNf164xvf8yvf6HjsY6IVNkD0rsk8Tyw1DQCvzlfo /rCrfKxkLRcGyNW0JhuJL0FYqMyhw57P7e6eQSHo2MfgFgz3zgtyzYzbVljMQJeXssR5 h4Mw== X-Gm-Message-State: AOAM5336U0hs75R616yXE77HT3ti/wzYH3n2II5RGY1jQ4FwzETTGob+ zf3dHbCfaJSLqgfrI5Kl22Kuzw== X-Received: by 2002:a1c:c2d4:: with SMTP id s203mr1427836wmf.58.1607501744668; Wed, 09 Dec 2020 00:15:44 -0800 (PST) Received: from vingu-book ([2a01:e0a:f:6020:2095:66e9:5a3f:72dd]) by smtp.gmail.com with ESMTPSA id g184sm1823079wma.16.2020.12.09.00.15.43 (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Wed, 09 Dec 2020 00:15:43 -0800 (PST) Date: Wed, 9 Dec 2020 09:15:41 +0100 From: Vincent Guittot To: Aubrey Li Cc: mingo@redhat.com, peterz@infradead.org, juri.lelli@redhat.com, mgorman@techsingularity.net, valentin.schneider@arm.com, qais.yousef@arm.com, dietmar.eggemann@arm.com, rostedt@goodmis.org, bsegall@google.com, tim.c.chen@linux.intel.com, linux-kernel@vger.kernel.org, Mel Gorman , Jiang Biao Subject: Re: [RFC PATCH v7] sched/fair: select idle cpu from idle cpumask for task wakeup Message-ID: <20201209081541.GA5071@vingu-book> References: <20201209062404.175565-1-aubrey.li@linux.intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20201209062404.175565-1-aubrey.li@linux.intel.com> User-Agent: Mutt/1.9.4 (2018-02-28) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Le mercredi 09 d?c. 2020 ? 14:24:04 (+0800), Aubrey Li a ?crit : > Add idle cpumask to track idle cpus in sched domain. Every time > a CPU enters idle, the CPU is set in idle cpumask to be a wakeup > target. And if the CPU is not in idle, the CPU is cleared in idle > cpumask during scheduler tick to ratelimit idle cpumask update. > > When a task wakes up to select an idle cpu, scanning idle cpumask > has lower cost than scanning all the cpus in last level cache domain, > especially when the system is heavily loaded. > > Benchmarks including hackbench, schbench, uperf, sysbench mysql > and kbuild were tested on a x86 4 socket system with 24 cores per > socket and 2 hyperthreads per core, total 192 CPUs, no regression > found. > > v6->v7: > - place the whole idle cpumask mechanism under CONFIG_SMP. > > v5->v6: > - decouple idle cpumask update from stop_tick signal, set idle CPU > in idle cpumask every time the CPU enters idle > > v4->v5: > - add update_idle_cpumask for s2idle case > - keep the same ordering of tick_nohz_idle_stop_tick() and update_ > idle_cpumask() everywhere > > v3->v4: > - change setting idle cpumask from every idle entry to tickless idle > if cpu driver is available. > - move clearing idle cpumask to scheduler_tick to decouple nohz mode. > > v2->v3: > - change setting idle cpumask to every idle entry, otherwise schbench > has a regression of 99th percentile latency. > - change clearing idle cpumask to nohz_balancer_kick(), so updating > idle cpumask is ratelimited in the idle exiting path. > - set SCHED_IDLE cpu in idle cpumask to allow it as a wakeup target. > > v1->v2: > - idle cpumask is updated in the nohz routines, by initializing idle > cpumask with sched_domain_span(sd), nohz=off case remains the original > behavior. > > Cc: Peter Zijlstra > Cc: Mel Gorman > Cc: Vincent Guittot > Cc: Qais Yousef > Cc: Valentin Schneider > Cc: Jiang Biao > Cc: Tim Chen > Signed-off-by: Aubrey Li > --- > include/linux/sched/topology.h | 13 +++++++++ > kernel/sched/core.c | 2 ++ > kernel/sched/fair.c | 51 +++++++++++++++++++++++++++++++++- > kernel/sched/idle.c | 5 ++++ > kernel/sched/sched.h | 4 +++ > kernel/sched/topology.c | 3 +- > 6 files changed, 76 insertions(+), 2 deletions(-) > > diff --git a/include/linux/sched/topology.h b/include/linux/sched/topology.h > index 820511289857..b47b85163607 100644 > --- a/include/linux/sched/topology.h > +++ b/include/linux/sched/topology.h > @@ -65,8 +65,21 @@ struct sched_domain_shared { > atomic_t ref; > atomic_t nr_busy_cpus; > int has_idle_cores; > + /* > + * Span of all idle CPUs in this domain. > + * > + * NOTE: this field is variable length. (Allocated dynamically > + * by attaching extra space to the end of the structure, > + * depending on how many CPUs the kernel has booted up with) > + */ > + unsigned long idle_cpus_span[]; > }; > > +static inline struct cpumask *sds_idle_cpus(struct sched_domain_shared *sds) > +{ > + return to_cpumask(sds->idle_cpus_span); > +} > + > struct sched_domain { > /* These fields must be setup */ > struct sched_domain __rcu *parent; /* top domain must be null terminated */ > diff --git a/kernel/sched/core.c b/kernel/sched/core.c > index c4da7e17b906..c4c51ff3402a 100644 > --- a/kernel/sched/core.c > +++ b/kernel/sched/core.c > @@ -4011,6 +4011,7 @@ void scheduler_tick(void) > > #ifdef CONFIG_SMP > rq->idle_balance = idle_cpu(cpu); > + update_idle_cpumask(cpu, false); Test rq->idle_balance here instead of adding the test in update_idle_cpumask which is only relevant for this situation. if (!rq->idle_balance) update_idle_cpumask(cpu, false); > trigger_load_balance(rq); > #endif > } > @@ -7186,6 +7187,7 @@ void __init sched_init(void) > rq->idle_stamp = 0; > rq->avg_idle = 2*sysctl_sched_migration_cost; > rq->max_idle_balance_cost = sysctl_sched_migration_cost; > + rq->last_idle_state = 1; > > INIT_LIST_HEAD(&rq->cfs_tasks); > > diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c > index c0c4d9ad7da8..7306f8886120 100644 > --- a/kernel/sched/fair.c > +++ b/kernel/sched/fair.c > @@ -6146,7 +6146,12 @@ static int select_idle_cpu(struct task_struct *p, struct sched_domain *sd, int t > > time = cpu_clock(this); > > - cpumask_and(cpus, sched_domain_span(sd), p->cpus_ptr); > + /* > + * sched_domain_shared is set only at shared cache level, > + * this works only because select_idle_cpu is called with > + * sd_llc. > + */ > + cpumask_and(cpus, sds_idle_cpus(sd->shared), p->cpus_ptr); > > for_each_cpu_wrap(cpu, cpus, target) { > if (!--nr) > @@ -6806,6 +6811,50 @@ balance_fair(struct rq *rq, struct task_struct *prev, struct rq_flags *rf) > > return newidle_balance(rq, rf) != 0; > } > + > +/* > + * Update cpu idle state and record this information > + * in sd_llc_shared->idle_cpus_span. > + */ > +void update_idle_cpumask(int cpu, bool set_idle) > +{ > + struct sched_domain *sd; > + struct rq *rq = cpu_rq(cpu); > + int idle_state; > + > + /* > + * If called from scheduler tick, only update > + * idle cpumask if the CPU is busy, as idle > + * cpumask is also updated on idle entry. > + * > + */ > + if (!set_idle && rq->idle_balance) > + return; > + /* > + * Also set SCHED_IDLE cpu in idle cpumask to > + * allow SCHED_IDLE cpu as a wakeup target > + */ > + idle_state = set_idle || sched_idle_cpu(cpu); > + /* > + * No need to update idle cpumask if the state > + * does not change. > + */ > + if (rq->last_idle_state == idle_state) > + return; > + /* > + * Called with irq disabled, rcu_read_lock() is not needed. > + */ > + sd = rcu_dereference(per_cpu(sd_llc, cpu)); > + if (unlikely(!sd)) > + return; > + > + if (idle_state) > + cpumask_set_cpu(cpu, sds_idle_cpus(sd->shared)); > + else > + cpumask_clear_cpu(cpu, sds_idle_cpus(sd->shared)); > + > + rq->last_idle_state = idle_state; > +} > #endif /* CONFIG_SMP */ > > static unsigned long wakeup_gran(struct sched_entity *se) > diff --git a/kernel/sched/idle.c b/kernel/sched/idle.c > index f324dc36fc43..2c517d6a061a 100644 > --- a/kernel/sched/idle.c > +++ b/kernel/sched/idle.c > @@ -257,6 +257,11 @@ static void do_idle(void) > cpuhp_report_idle_dead(); > arch_cpu_idle_dead(); > } > + /* > + * The CPU is about to go idle, set it in idle cpumask > + * to be a wake up target. > + */ > + update_idle_cpumask(cpu, true); > > arch_cpu_idle_enter(); > > diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h > index 8d1ca65db3b0..2167ca48f3aa 100644 > --- a/kernel/sched/sched.h > +++ b/kernel/sched/sched.h > @@ -976,6 +976,7 @@ struct rq { > > unsigned char nohz_idle_balance; > unsigned char idle_balance; > + unsigned char last_idle_state; > > unsigned long misfit_task_load; > > @@ -1516,6 +1517,8 @@ static inline unsigned int group_first_cpu(struct sched_group *group) > > extern int group_balance_cpu(struct sched_group *sg); > > +void update_idle_cpumask(int cpu, bool set_idle); > + > #if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SYSCTL) > void register_sched_domain_sysctl(void); > void dirty_sched_domain_sysctl(int cpu); > @@ -1536,6 +1539,7 @@ extern void flush_smp_call_function_from_idle(void); > > #else /* !CONFIG_SMP: */ > static inline void flush_smp_call_function_from_idle(void) { } > +static inline void update_idle_cpumask(int cpu, bool set_idle) { } > #endif > > #include "stats.h" > diff --git a/kernel/sched/topology.c b/kernel/sched/topology.c > index 1bd7e3af904f..541bd3a7de4d 100644 > --- a/kernel/sched/topology.c > +++ b/kernel/sched/topology.c > @@ -1407,6 +1407,7 @@ sd_init(struct sched_domain_topology_level *tl, > sd->shared = *per_cpu_ptr(sdd->sds, sd_id); > atomic_inc(&sd->shared->ref); > atomic_set(&sd->shared->nr_busy_cpus, sd_weight); > + cpumask_copy(sds_idle_cpus(sd->shared), sched_domain_span(sd)); > } > > sd->private = sdd; > @@ -1769,7 +1770,7 @@ static int __sdt_alloc(const struct cpumask *cpu_map) > > *per_cpu_ptr(sdd->sd, j) = sd; > > - sds = kzalloc_node(sizeof(struct sched_domain_shared), > + sds = kzalloc_node(sizeof(struct sched_domain_shared) + cpumask_size(), > GFP_KERNEL, cpu_to_node(j)); > if (!sds) > return -ENOMEM; > -- > 2.25.1 >