Received: by 2002:a25:31c3:0:0:0:0:0 with SMTP id x186csp117460ybx; Wed, 30 Oct 2019 12:14:01 -0700 (PDT) X-Google-Smtp-Source: APXvYqzKQYGZ163H2w8EPs452Lz+IeRz8JzH4YYTI1ZJsaZQ6YURzs2n++miXWgMdX6nCgHs8WyC X-Received: by 2002:aa7:c942:: with SMTP id h2mr1573545edt.238.1572462840994; Wed, 30 Oct 2019 12:14:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1572462840; cv=none; d=google.com; s=arc-20160816; b=xSokEfKsbLPRTqNY2sWyrfH76q1ui+tVvcx2P9PzfJSsuwR1zzJVKH/Fj93kvupHwy gONB7Ujp0Tx2V+nwb/AVIBwWIznoe/4FtZ4WxFuu92fHO0a00qKzfIMK+qyFwjUmW8e1 oXVFskgDpzZFhjJF52KBb7LWB5eT+3haHdjMoyp+MonXupzpJaX+UJJ9Pj8cHrBJmdkh 40lCK90w9jh8V3PcOrw3QQmfu77nwbiThYS4udsUT2goYmhW1KEZ4o4fNJWCEVXitNMf p3ODwyWHS41ER/Xm9IdKjsc9Jkb0XvsbPJfd8WvKQgy0Z/CmWK4oCHjZvU9K5TMfAm3L Yxdg== 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=zHCANWKkTeb6Y1hhG8NL8+V3n0Ipmi5CGrrLZMsqGAc=; b=cPSf9BWM4fzTswPLiSUFhr+8Fh5uVcZIRS41F4emzmYUGZLOLSQ6WcMIjl6YZa24FB ZntdSDcUcSrBciaw9Kzou5e1zeqU57/0episz2B4p6Amoop5P/jL1dLmsPHZ6CDP+ZNa soH8zF3bBUPkEKh6r8wyvl/SQSmJ5JNylWD52tijEWyPHIhTyhyEtx92ENi+ZvoWw0kA 72SxCPsekP7TBjITGUnEKFCwHuxYnLguM4m4nRrGAVV4kPjpGylYmKEs5VqQd/VXa+rP 4M+Ehl/ZXIZGflms25I7yM1MR5iNEORAbHrEbQG4frXRPnmwTOjmlvcqYr3WC3JunswR SeSw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@digitalocean.com header.s=google header.b=LqEm6i8i; 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 l5si2141907edb.341.2019.10.30.12.13.37; Wed, 30 Oct 2019 12:14:00 -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=LqEm6i8i; 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 S1728104AbfJ3SeY (ORCPT + 99 others); Wed, 30 Oct 2019 14:34:24 -0400 Received: from mail-yw1-f66.google.com ([209.85.161.66]:33349 "EHLO mail-yw1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728063AbfJ3SeT (ORCPT ); Wed, 30 Oct 2019 14:34:19 -0400 Received: by mail-yw1-f66.google.com with SMTP id v5so1207019ywd.0 for ; Wed, 30 Oct 2019 11:34:18 -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=zHCANWKkTeb6Y1hhG8NL8+V3n0Ipmi5CGrrLZMsqGAc=; b=LqEm6i8izQhrhdxhwwHDbtbCqHcG3Kx9IsUHZHfsQvk+Bj5zbTnyqzRrsg8BFHkjQD hTPMSzidg/JeQyYHKmQAq+XlFv0hilB/ZuVTX6/qgvHzGDYUPQtBvsw1QRjKUrwFRn/A Ta1cSgQ8Pcna++kIypLKkFGXIjRqFbmUfBbsI= 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=zHCANWKkTeb6Y1hhG8NL8+V3n0Ipmi5CGrrLZMsqGAc=; b=ip8QX0FbXdAXOU1Ku1+BWtTzjaQeoFvujo+fkdEyb6cdif9xXSE8+j3Ki/y7rQq51i bDwhVguXzLpg3xtbGnE7jxKiHogpmnZWCAR1PTqmIdtpTKUJiax3DYj9qQc69xf4Gpzl cDj0SlGbpK85IdJ8GbSm2IOZyktlR4T114hYh/qKMETIz+hSxju626DNoNK0FxvQah12 Nz2KRx+paQ6MNGGEtALeU9nLZ0nePrdPZig2KL9efSutYAZT2rAScRIh8yiuamg9WTtY mnhyekPSXqNiCfjwbC/z1AKjx5SRf2nWT0G9pb80Z8CKnHLnWGvvjda+BNaJZi0Il5Fn SmhQ== X-Gm-Message-State: APjAAAWK5VG8STkMVjEoqw7iRUCMrddJFHaF/N0DY/UN7TmMUZwR8u+0 JR33sJM2YQw8CAUjgtI11nYtkw== X-Received: by 2002:a81:6288:: with SMTP id w130mr846602ywb.434.1572460458387; Wed, 30 Oct 2019 11:34:18 -0700 (PDT) Received: from vpillai-dev.sfo2.internal.digitalocean.com ([138.68.32.68]) by smtp.gmail.com with ESMTPSA id a141sm544846ywe.88.2019.10.30.11.34.17 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 30 Oct 2019 11:34:18 -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, Dario Faggioli , fweisbec@gmail.com, keescook@chromium.org, kerrnel@google.com, Phil Auld , Aaron Lu , Aubrey Li , Valentin Schneider , Mel Gorman , Pawan Gupta , Paolo Bonzini , Vineeth Remanan Pillai Subject: [RFC PATCH v4 10/19] sched: Core-wide rq->lock Date: Wed, 30 Oct 2019 18:33:23 +0000 Message-Id: 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 Introduce the basic infrastructure to have a core wide rq->lock. Signed-off-by: Peter Zijlstra (Intel) Signed-off-by: Julien Desfossez Signed-off-by: Vineeth Remanan Pillai --- kernel/Kconfig.preempt | 6 +++ kernel/sched/core.c | 113 ++++++++++++++++++++++++++++++++++++++++- kernel/sched/sched.h | 31 +++++++++++ 3 files changed, 148 insertions(+), 2 deletions(-) diff --git a/kernel/Kconfig.preempt b/kernel/Kconfig.preempt index deff97217496..63493a1b9ff6 100644 --- a/kernel/Kconfig.preempt +++ b/kernel/Kconfig.preempt @@ -80,3 +80,9 @@ config PREEMPT_COUNT config PREEMPTION bool select PREEMPT_COUNT + +config SCHED_CORE + bool + default y + depends on SCHED_SMT + diff --git a/kernel/sched/core.c b/kernel/sched/core.c index 1f40211ff29a..0427cf610d6a 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -72,6 +72,70 @@ __read_mostly int scheduler_running; */ int sysctl_sched_rt_runtime = 950000; +#ifdef CONFIG_SCHED_CORE + +DEFINE_STATIC_KEY_FALSE(__sched_core_enabled); + +/* + * The static-key + stop-machine variable are needed such that: + * + * spin_lock(rq_lockp(rq)); + * ... + * spin_unlock(rq_lockp(rq)); + * + * ends up locking and unlocking the _same_ lock, and all CPUs + * always agree on what rq has what lock. + * + * XXX entirely possible to selectively enable cores, don't bother for now. + */ +static int __sched_core_stopper(void *data) +{ + bool enabled = !!(unsigned long)data; + int cpu; + + for_each_online_cpu(cpu) + cpu_rq(cpu)->core_enabled = enabled; + + return 0; +} + +static DEFINE_MUTEX(sched_core_mutex); +static int sched_core_count; + +static void __sched_core_enable(void) +{ + // XXX verify there are no cookie tasks (yet) + + static_branch_enable(&__sched_core_enabled); + stop_machine(__sched_core_stopper, (void *)true, NULL); +} + +static void __sched_core_disable(void) +{ + // XXX verify there are no cookie tasks (left) + + stop_machine(__sched_core_stopper, (void *)false, NULL); + static_branch_disable(&__sched_core_enabled); +} + +void sched_core_get(void) +{ + mutex_lock(&sched_core_mutex); + if (!sched_core_count++) + __sched_core_enable(); + mutex_unlock(&sched_core_mutex); +} + +void sched_core_put(void) +{ + mutex_lock(&sched_core_mutex); + if (!--sched_core_count) + __sched_core_disable(); + mutex_unlock(&sched_core_mutex); +} + +#endif /* CONFIG_SCHED_CORE */ + /* * __task_rq_lock - lock the rq @p resides on. */ @@ -6210,8 +6274,15 @@ int sched_cpu_activate(unsigned int cpu) /* * When going up, increment the number of cores with SMT present. */ - if (cpumask_weight(cpu_smt_mask(cpu)) == 2) + if (cpumask_weight(cpu_smt_mask(cpu)) == 2) { static_branch_inc_cpuslocked(&sched_smt_present); +#ifdef CONFIG_SCHED_CORE + if (static_branch_unlikely(&__sched_core_enabled)) { + rq->core_enabled = true; + } +#endif + } + #endif set_cpu_active(cpu, true); @@ -6259,8 +6330,16 @@ int sched_cpu_deactivate(unsigned int cpu) /* * When going down, decrement the number of cores with SMT present. */ - if (cpumask_weight(cpu_smt_mask(cpu)) == 2) + if (cpumask_weight(cpu_smt_mask(cpu)) == 2) { +#ifdef CONFIG_SCHED_CORE + struct rq *rq = cpu_rq(cpu); + if (static_branch_unlikely(&__sched_core_enabled)) { + rq->core_enabled = false; + } +#endif static_branch_dec_cpuslocked(&sched_smt_present); + + } #endif if (!sched_smp_initialized) @@ -6285,6 +6364,28 @@ static void sched_rq_cpu_starting(unsigned int cpu) int sched_cpu_starting(unsigned int cpu) { +#ifdef CONFIG_SCHED_CORE + const struct cpumask *smt_mask = cpu_smt_mask(cpu); + struct rq *rq, *core_rq = NULL; + int i; + + for_each_cpu(i, smt_mask) { + rq = cpu_rq(i); + if (rq->core && rq->core == rq) + core_rq = rq; + } + + if (!core_rq) + core_rq = cpu_rq(cpu); + + for_each_cpu(i, smt_mask) { + rq = cpu_rq(i); + + WARN_ON_ONCE(rq->core && rq->core != core_rq); + rq->core = core_rq; + } +#endif /* CONFIG_SCHED_CORE */ + sched_rq_cpu_starting(cpu); sched_tick_start(cpu); return 0; @@ -6313,6 +6414,9 @@ int sched_cpu_dying(unsigned int cpu) update_max_interval(); nohz_balance_exit_idle(rq); hrtick_clear(rq); +#ifdef CONFIG_SCHED_CORE + rq->core = NULL; +#endif return 0; } #endif @@ -6507,6 +6611,11 @@ void __init sched_init(void) #endif /* CONFIG_SMP */ hrtick_rq_init(rq); atomic_set(&rq->nr_iowait, 0); + +#ifdef CONFIG_SCHED_CORE + rq->core = NULL; + rq->core_enabled = 0; +#endif } set_load_weight(&init_task, false); diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h index 44f45aad3f97..2ba68ab093ee 100644 --- a/kernel/sched/sched.h +++ b/kernel/sched/sched.h @@ -991,6 +991,12 @@ struct rq { /* Must be inspected within a rcu lock section */ struct cpuidle_state *idle_state; #endif + +#ifdef CONFIG_SCHED_CORE + /* per rq */ + struct rq *core; + unsigned int core_enabled; +#endif }; #ifdef CONFIG_FAIR_GROUP_SCHED @@ -1018,11 +1024,36 @@ static inline int cpu_of(struct rq *rq) #endif } +#ifdef CONFIG_SCHED_CORE +DECLARE_STATIC_KEY_FALSE(__sched_core_enabled); + +static inline bool sched_core_enabled(struct rq *rq) +{ + return static_branch_unlikely(&__sched_core_enabled) && rq->core_enabled; +} + +static inline raw_spinlock_t *rq_lockp(struct rq *rq) +{ + if (sched_core_enabled(rq)) + return &rq->core->__lock; + + return &rq->__lock; +} + +#else /* !CONFIG_SCHED_CORE */ + +static inline bool sched_core_enabled(struct rq *rq) +{ + return false; +} + static inline raw_spinlock_t *rq_lockp(struct rq *rq) { return &rq->__lock; } +#endif /* CONFIG_SCHED_CORE */ + #ifdef CONFIG_SCHED_SMT extern void __update_idle_core(struct rq *rq); -- 2.17.1