Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp3772048yba; Tue, 23 Apr 2019 09:20:36 -0700 (PDT) X-Google-Smtp-Source: APXvYqwd7zY69Bz2CWbOsuclaWnI/cqSZums2Ch8Fm24PQRtLN9wO/lDcvcfnvJV035HzS3Px5+k X-Received: by 2002:a63:bd52:: with SMTP id d18mr25808868pgp.52.1556036436308; Tue, 23 Apr 2019 09:20:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1556036436; cv=none; d=google.com; s=arc-20160816; b=Y91RiP6iuozjS8tcir42XCA3EmoOSdknq4nmSMsG3RNDQfTtldJtRk3gggyrp0aFaB 49iUhLBQLLxbKbazJFEoIpXD6UgJGtWyvRZDXeOXjrIuawe30msbzRyS6roW/Gnn00nl TA0IqjXd4yhC5a1vDBWiH2tfIxeR68eM2PK6S0cB3tO4ifUHLnlQy9KQSEDYBz+R973q eTpAuhHlxzYoLp/c9qrvPtFE3XnqgnxgATOU08jyoKmL/ulHQ9e42MtF3/FPOquR4aMF wijMF5d1g+Gyv0Pw5TslqdzucyFDiA33Z5jo+3IAARChcCYEeZu0yHsi+ekbOUHBW56e J7og== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:dkim-signature; bh=R44A5/YkLGkeY9si7Vswv49s16/buRIZLRAf2l2d/Vc=; b=c/lE9N294enBzCDq6i9wnDUIygLdrK7jlL62mjpgZgFpvwE1p7ntxvbSnxZj0PJZ0i Y27NqhpazSsnmBDMIBDgCjhMiGrhFtIxu69TLKLH3BesyON4MbvmcByS5yfzEifDgttT 6LQCUH19usjANn2TfYn/NR7P5iRf1eseDPz2cBi9Z4pKm1MqxNtgRNG8KVyMcyxSdDev 8Q+EEbnzZs8uJDKQnuBSoSy7RPzOgcWX3QKjEyf/qxjlNwkIrmap0QSeSFHt/jaVqVk3 OKJ4mH66DKCwf/a01uArCp7tNm2eaYUgvWrzw8KGv8oPLilhOIFf9roBzwMCgoUuEFRJ 4uQw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@digitalocean.com header.s=google header.b=SgfnErvW; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=digitalocean.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id q1si16186822pfh.125.2019.04.23.09.20.21; Tue, 23 Apr 2019 09:20:36 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@digitalocean.com header.s=google header.b=SgfnErvW; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=digitalocean.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728774AbfDWQTB (ORCPT + 99 others); Tue, 23 Apr 2019 12:19:01 -0400 Received: from mail-io1-f66.google.com ([209.85.166.66]:45630 "EHLO mail-io1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728740AbfDWQS5 (ORCPT ); Tue, 23 Apr 2019 12:18:57 -0400 Received: by mail-io1-f66.google.com with SMTP id e8so798860ioe.12 for ; Tue, 23 Apr 2019 09:18:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=digitalocean.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :in-reply-to:references; bh=R44A5/YkLGkeY9si7Vswv49s16/buRIZLRAf2l2d/Vc=; b=SgfnErvWNi4dnKwE0F9wsbTY7BqXNr2Q84AudXiI5tAK6QkjeRAd/o8rvAA5VYjTcw SVmJy5mvrRFsIIwP1Y4IRBRUDWJor1nJgpXFPqrHzOFxnOGpQZwP+b8tvbiiH05d9wyJ SDEKLeC9h1RLvFJdBvYHbnv041wC1R9h35RVg= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:in-reply-to:references; bh=R44A5/YkLGkeY9si7Vswv49s16/buRIZLRAf2l2d/Vc=; b=eksQM+ITkZMty5TVV2bbz1c3FHaykuqAj+pfRdMsL6PU9YWMMI0qCGuZKEMyU49Xgy XAh94E1yAzXqhHOAkUEfCIUyyDZNWL8pR8x9qrwYKSV5cYOmO3uSYqfvmZ4WoGvWLDIX lBh1TWN8GPlNtRV3+udTaPVV5nUJzFecRfHrFOTPfmZPwO2ISXSFF8/dWq7faMvIA+FR N0fQUs6UIUZtQwTPa3nABV/VgD0U0RuYxWEyL/NuWReu5+dtG1Ds+cYiw79Q81VH3I1n 4O2mMWpHXdi9t/jyI/2kzgnSY+F6ABh/18puDncnX1UqDR2PYIM5ovdKSRntPLm7Ya25 5l5g== X-Gm-Message-State: APjAAAU9jRn7lnAs9D+jOBzdodEoSunxKDiJFqDZEScafKAiY5fnpbkL Wvly2AHacdfQYjCsEUt8zTyJUw== X-Received: by 2002:a5d:948d:: with SMTP id v13mr7854789ioj.59.1556036336055; Tue, 23 Apr 2019 09:18:56 -0700 (PDT) Received: from swap-tester ([178.128.225.14]) by smtp.gmail.com with ESMTPSA id y22sm5404570ioa.47.2019.04.23.09.18.55 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 23 Apr 2019 09:18:55 -0700 (PDT) From: Vineeth Remanan Pillai To: Nishanth Aravamudan , Julien Desfossez , Peter Zijlstra , Tim Chen , mingo@kernel.org, tglx@linutronix.de, pjt@google.com, torvalds@linux-foundation.org Cc: linux-kernel@vger.kernel.org, subhra.mazumdar@oracle.com, fweisbec@gmail.com, keescook@chromium.org, kerrnel@google.com, Phil Auld , Aaron Lu , Aubrey Li , Valentin Schneider , Mel Gorman , Pawan Gupta , Paolo Bonzini Subject: [RFC PATCH v2 15/17] sched: Trivial forced-newidle balancer Date: Tue, 23 Apr 2019 16:18:20 +0000 Message-Id: <002304fa58577c7926abe9f4cdc8039945985b3d.1556025155.git.vpillai@digitalocean.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: References: In-Reply-To: References: Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Peter Zijlstra (Intel) When a sibling is forced-idle to match the core-cookie; search for matching tasks to fill the core. Signed-off-by: Peter Zijlstra (Intel) --- include/linux/sched.h | 1 + kernel/sched/core.c | 131 +++++++++++++++++++++++++++++++++++++++++- kernel/sched/idle.c | 1 + kernel/sched/sched.h | 6 ++ 4 files changed, 138 insertions(+), 1 deletion(-) diff --git a/include/linux/sched.h b/include/linux/sched.h index a4b39a28236f..1a309e8546cd 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -641,6 +641,7 @@ struct task_struct { #ifdef CONFIG_SCHED_CORE struct rb_node core_node; unsigned long core_cookie; + unsigned int core_occupation; #endif #ifdef CONFIG_CGROUP_SCHED diff --git a/kernel/sched/core.c b/kernel/sched/core.c index 9e6e90c6f9b9..e8f5ec641d0a 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -217,6 +217,21 @@ struct task_struct *sched_core_find(struct rq *rq, unsigned long cookie) return match; } +struct task_struct *sched_core_next(struct task_struct *p, unsigned long cookie) +{ + struct rb_node *node = &p->core_node; + + node = rb_next(node); + if (!node) + return NULL; + + p = container_of(node, struct task_struct, core_node); + if (p->core_cookie != cookie) + return NULL; + + return p; +} + /* * The static-key + stop-machine variable are needed such that: * @@ -3672,7 +3687,7 @@ pick_next_task(struct rq *rq, struct task_struct *prev, struct rq_flags *rf) struct task_struct *next, *max = NULL; const struct sched_class *class; const struct cpumask *smt_mask; - int i, j, cpu; + int i, j, cpu, occ = 0; if (!sched_core_enabled(rq)) return __pick_next_task(rq, prev, rf); @@ -3763,6 +3778,9 @@ pick_next_task(struct rq *rq, struct task_struct *prev, struct rq_flags *rf) goto done; } + if (!is_idle_task(p)) + occ++; + rq_i->core_pick = p; /* @@ -3786,6 +3804,7 @@ pick_next_task(struct rq *rq, struct task_struct *prev, struct rq_flags *rf) cpu_rq(j)->core_pick = NULL; } + occ = 1; goto again; } } @@ -3808,6 +3827,8 @@ next_class:; WARN_ON_ONCE(!rq_i->core_pick); + rq_i->core_pick->core_occupation = occ; + if (i == cpu) continue; @@ -3823,6 +3844,114 @@ next_class:; return next; } +static bool try_steal_cookie(int this, int that) +{ + struct rq *dst = cpu_rq(this), *src = cpu_rq(that); + struct task_struct *p; + unsigned long cookie; + bool success = false; + + local_irq_disable(); + double_rq_lock(dst, src); + + cookie = dst->core->core_cookie; + if (!cookie) + goto unlock; + + if (dst->curr != dst->idle) + goto unlock; + + p = sched_core_find(src, cookie); + if (p == src->idle) + goto unlock; + + do { + if (p == src->core_pick || p == src->curr) + goto next; + + if (!cpumask_test_cpu(this, &p->cpus_allowed)) + goto next; + + if (p->core_occupation > dst->idle->core_occupation) + goto next; + + p->on_rq = TASK_ON_RQ_MIGRATING; + deactivate_task(src, p, 0); + set_task_cpu(p, this); + activate_task(dst, p, 0); + p->on_rq = TASK_ON_RQ_QUEUED; + + resched_curr(dst); + + success = true; + break; + +next: + p = sched_core_next(p, cookie); + } while (p); + +unlock: + double_rq_unlock(dst, src); + local_irq_enable(); + + return success; +} + +static bool steal_cookie_task(int cpu, struct sched_domain *sd) +{ + int i; + + for_each_cpu_wrap(i, sched_domain_span(sd), cpu) { + if (i == cpu) + continue; + + if (need_resched()) + break; + + if (try_steal_cookie(cpu, i)) + return true; + } + + return false; +} + +static void sched_core_balance(struct rq *rq) +{ + struct sched_domain *sd; + int cpu = cpu_of(rq); + + rcu_read_lock(); + raw_spin_unlock_irq(rq_lockp(rq)); + for_each_domain(cpu, sd) { + if (!(sd->flags & SD_LOAD_BALANCE)) + break; + + if (need_resched()) + break; + + if (steal_cookie_task(cpu, sd)) + break; + } + raw_spin_lock_irq(rq_lockp(rq)); + rcu_read_unlock(); +} + +static DEFINE_PER_CPU(struct callback_head, core_balance_head); + +void queue_core_balance(struct rq *rq) +{ + if (!sched_core_enabled(rq)) + return; + + if (!rq->core->core_cookie) + return; + + if (!rq->nr_running) /* not forced idle */ + return; + + queue_balance_callback(rq, &per_cpu(core_balance_head, rq->cpu), sched_core_balance); +} + #else /* !CONFIG_SCHED_CORE */ static struct task_struct * diff --git a/kernel/sched/idle.c b/kernel/sched/idle.c index e7f38da60373..44decdcccba1 100644 --- a/kernel/sched/idle.c +++ b/kernel/sched/idle.c @@ -387,6 +387,7 @@ static void set_next_task_idle(struct rq *rq, struct task_struct *next) { update_idle_core(rq); schedstat_inc(rq->sched_goidle); + queue_core_balance(rq); } static struct task_struct * diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h index 4cfde289610d..2a5f5a6b11ae 100644 --- a/kernel/sched/sched.h +++ b/kernel/sched/sched.h @@ -1013,6 +1013,8 @@ static inline raw_spinlock_t *rq_lockp(struct rq *rq) return &rq->__lock; } +extern void queue_core_balance(struct rq *rq); + #else /* !CONFIG_SCHED_CORE */ static inline bool sched_core_enabled(struct rq *rq) @@ -1025,6 +1027,10 @@ static inline raw_spinlock_t *rq_lockp(struct rq *rq) return &rq->__lock; } +static inline void queue_core_balance(struct rq *rq) +{ +} + #endif /* CONFIG_SCHED_CORE */ #ifdef CONFIG_SCHED_SMT -- 2.17.1