Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp3772595yba; Tue, 23 Apr 2019 09:21:06 -0700 (PDT) X-Google-Smtp-Source: APXvYqxVXArGktV5lFfeCeTNFh02wC75Je5QmcUCc3sDoFogzJQPbroWbq3drgwZRhCe9v3KgHlu X-Received: by 2002:a17:902:9b83:: with SMTP id y3mr27087121plp.165.1556036466238; Tue, 23 Apr 2019 09:21:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1556036466; cv=none; d=google.com; s=arc-20160816; b=UpOe0SDPP0bgzU6cwW/53hNSCAqo5lQpXkjX7JauijwQ060vIThZBNm9WKQOtae1G+ Xsb7Q639xeG1V6tSq4T61mMcjXrnyFl/qF6NQHVMkw0OLxPGCf9/pQbenl6zkfyHZovy vx2ujuJE0uVqzljtaBCMbhw17hAtmiwzj2tmOSndRySlgJusDA1CNYjPyOFRJSRGQrcK BnT/C8FeBBe478BNRQFrhfi1LRzkmJAH2UZJa0Fu5wMeRlm09mwmFr1M5hxlDsQEY7ER wcjcim50adAhdFTMootx1vyaOxk6YMHESaXlp/aIAaENAmXqX3ahBGnhO3DhQzl/6ZMn xhlQ== 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=wQ0LDsK3bHeBUH7iKyQ0azS+t5FzDsZhSPpY0xVR21M=; b=py9xVV8fmgc3oykgGYCjbRC5F7eOUrB/4S6N1M5B73vccrjq5Sx+5655PrzK+1jMHm pJu44nNZWuqKYPG2ZnCmOrPoroQ6CagDZV/kUJ6RAlNqHIvXqE9KjqBoV01d95xtJkJ0 LuaiSwcgfVQSHXR9i8R/exRi222GniqJBMIzKWo7aYxcD6yS+NY2G6QSQcsI4V9vT7d2 KXlf3gVm1RwoxA4soIdW3gbm0IIZT5muGTEGnfVIWqkXBH5NT/lLHRFuXnoyDOKgByrz YxAKNQLDniTxkQiOMkhJyqjL4GTcNe19si77RetfQ8BrTb9EnMhX6GV5tjN/nOONp3B2 KOwg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@digitalocean.com header.s=google header.b=LdsdrWfd; 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 d6si17025019pfh.177.2019.04.23.09.20.50; Tue, 23 Apr 2019 09:21:06 -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=LdsdrWfd; 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 S1728825AbfDWQTa (ORCPT + 99 others); Tue, 23 Apr 2019 12:19:30 -0400 Received: from mail-it1-f195.google.com ([209.85.166.195]:39451 "EHLO mail-it1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728658AbfDWQSx (ORCPT ); Tue, 23 Apr 2019 12:18:53 -0400 Received: by mail-it1-f195.google.com with SMTP id e13so1098291itk.4 for ; Tue, 23 Apr 2019 09:18:52 -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=wQ0LDsK3bHeBUH7iKyQ0azS+t5FzDsZhSPpY0xVR21M=; b=LdsdrWfdePHVziSXPo4uXNlFsr0/MpFgAXe2qPVjmMy9KlXreSM9+ACbvf8kVG74+3 oM7Z2wquZ00teHE9uN0xIOzgzdg2TeEAGJ8luW0CrGLGsnBBb7iKGVEQbDiP6wqNp4Zq fcqD7yGmN/yMEHhwyDKzJz8NAPfmOELIDZCdE= 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=wQ0LDsK3bHeBUH7iKyQ0azS+t5FzDsZhSPpY0xVR21M=; b=Y5CzplQJCjD5X0VHNWzNFESEZnCDobppiGYAYqKrwH8qo9RgbLfrQwnB7O7geyyg3D SyqHGxjz35+beWoYNdaww21YhgucCXFtBJ+yilYsxFH32BrHc7o9NtckAwyB1i8bRcVR pgUpJXVJIbprcDlOLM/FJjtXZlMjTVeTKwbWFaHtV6i8W70lHluGHUlofz4jkt4La3q1 WUnxJMXrZp8LtCDHYiH/FFfTaGGvvYN9RbljqiQDhhS0qn5Bbnly3KMIbp1TRJTNmyVQ 6LVneRpgSmfwuK5z8uL+V2z6r9Gj+WWjATarP9Wi/6h4wy6MKGZmz28CDrX7UpbfrxJ3 +4fQ== X-Gm-Message-State: APjAAAUCahaYsH+AlUNAoCN27nRGuQHTvisa+2UA1kzsqhK3+11qgioF dHgs6mx/ZcvPaC8ZuyJVcC+WfA== X-Received: by 2002:a02:6019:: with SMTP id i25mr17900810jac.66.1556036331966; Tue, 23 Apr 2019 09:18:51 -0700 (PDT) Received: from swap-tester ([178.128.225.14]) by smtp.gmail.com with ESMTPSA id 125sm7946972itx.21.2019.04.23.09.18.51 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 23 Apr 2019 09:18:51 -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 , Vineeth Remanan Pillai Subject: [RFC PATCH v2 11/17] sched: Basic tracking of matching tasks Date: Tue, 23 Apr 2019 16:18:16 +0000 Message-Id: <2364f2b65bf50826d881c84d7634b6565dfee527.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) Introduce task_struct::core_cookie as an opaque identifier for core scheduling. When enabled; core scheduling will only allow matching task to be on the core; where idle matches everything. When task_struct::core_cookie is set (and core scheduling is enabled) these tasks are indexed in a second RB-tree, first on cookie value then on scheduling function, such that matching task selection always finds the most elegible match. NOTE: *shudder* at the overhead... NOTE: *sigh*, a 3rd copy of the scheduling function; the alternative is per class tracking of cookies and that just duplicates a lot of stuff for no raisin (the 2nd copy lives in the rt-mutex PI code). Signed-off-by: Peter Zijlstra (Intel) Signed-off-by: Vineeth Remanan Pillai Signed-off-by: Julien Desfossez --- Changes in v2 ------------- - Improves the priority comparison logic between processes in different cpus. - Peter Zijlstra - Aaron Lu --- include/linux/sched.h | 8 ++- kernel/sched/core.c | 164 ++++++++++++++++++++++++++++++++++++++++++ kernel/sched/sched.h | 4 ++ 3 files changed, 175 insertions(+), 1 deletion(-) diff --git a/include/linux/sched.h b/include/linux/sched.h index 1549584a1538..a4b39a28236f 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -636,10 +636,16 @@ struct task_struct { const struct sched_class *sched_class; struct sched_entity se; struct sched_rt_entity rt; + struct sched_dl_entity dl; + +#ifdef CONFIG_SCHED_CORE + struct rb_node core_node; + unsigned long core_cookie; +#endif + #ifdef CONFIG_CGROUP_SCHED struct task_group *sched_task_group; #endif - struct sched_dl_entity dl; #ifdef CONFIG_PREEMPT_NOTIFIERS /* List of struct preempt_notifier: */ diff --git a/kernel/sched/core.c b/kernel/sched/core.c index 2f559d706b8e..5066a1493acf 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -64,6 +64,159 @@ int sysctl_sched_rt_runtime = 950000; DEFINE_STATIC_KEY_FALSE(__sched_core_enabled); +/* kernel prio, less is more */ +static inline int __task_prio(struct task_struct *p) +{ + if (p->sched_class == &stop_sched_class) /* trumps deadline */ + return -2; + + if (rt_prio(p->prio)) /* includes deadline */ + return p->prio; /* [-1, 99] */ + + if (p->sched_class == &idle_sched_class) + return MAX_RT_PRIO + NICE_WIDTH; /* 140 */ + + return MAX_RT_PRIO + MAX_NICE; /* 120, squash fair */ +} + +// FIXME: This is copied from fair.c. Needs only single copy. +#ifdef CONFIG_FAIR_GROUP_SCHED +static inline struct cfs_rq *task_cfs_rq(struct task_struct *p) +{ + return p->se.cfs_rq; +} +#else +static inline struct cfs_rq *task_cfs_rq(struct task_struct *p) +{ + return &task_rq(p)->cfs; +} +#endif + +/* + * l(a,b) + * le(a,b) := !l(b,a) + * g(a,b) := l(b,a) + * ge(a,b) := !l(a,b) + */ + +/* real prio, less is less */ +static inline bool __prio_less(struct task_struct *a, struct task_struct *b, bool core_cmp) +{ + u64 vruntime; + + int pa = __task_prio(a), pb = __task_prio(b); + + if (-pa < -pb) + return true; + + if (-pb < -pa) + return false; + + if (pa == -1) /* dl_prio() doesn't work because of stop_class above */ + return !dl_time_before(a->dl.deadline, b->dl.deadline); + + vruntime = b->se.vruntime; + if (core_cmp) { + vruntime -= task_cfs_rq(b)->min_vruntime; + vruntime += task_cfs_rq(a)->min_vruntime; + } + if (pa == MAX_RT_PRIO + MAX_NICE) /* fair */ + return !((s64)(a->se.vruntime - vruntime) <= 0); + + return false; +} + +static inline bool cpu_prio_less(struct task_struct *a, struct task_struct *b) +{ + return __prio_less(a, b, false); +} + +static inline bool core_prio_less(struct task_struct *a, struct task_struct *b) +{ + return __prio_less(a, b, true); +} + +static inline bool __sched_core_less(struct task_struct *a, struct task_struct *b) +{ + if (a->core_cookie < b->core_cookie) + return true; + + if (a->core_cookie > b->core_cookie) + return false; + + /* flip prio, so high prio is leftmost */ + if (cpu_prio_less(b, a)) + return true; + + return false; +} + +void sched_core_enqueue(struct rq *rq, struct task_struct *p) +{ + struct rb_node *parent, **node; + struct task_struct *node_task; + + rq->core->core_task_seq++; + + if (!p->core_cookie) + return; + + node = &rq->core_tree.rb_node; + parent = *node; + + while (*node) { + node_task = container_of(*node, struct task_struct, core_node); + parent = *node; + + if (__sched_core_less(p, node_task)) + node = &parent->rb_left; + else + node = &parent->rb_right; + } + + rb_link_node(&p->core_node, parent, node); + rb_insert_color(&p->core_node, &rq->core_tree); +} + +void sched_core_dequeue(struct rq *rq, struct task_struct *p) +{ + rq->core->core_task_seq++; + + if (!p->core_cookie) + return; + + rb_erase(&p->core_node, &rq->core_tree); +} + +/* + * Find left-most (aka, highest priority) task matching @cookie. + */ +struct task_struct *sched_core_find(struct rq *rq, unsigned long cookie) +{ + struct rb_node *node = rq->core_tree.rb_node; + struct task_struct *node_task, *match; + + /* + * The idle task always matches any cookie! + */ + match = idle_sched_class.pick_task(rq); + + while (node) { + node_task = container_of(node, struct task_struct, core_node); + + if (node_task->core_cookie < cookie) { + node = node->rb_left; + } else if (node_task->core_cookie > cookie) { + node = node->rb_right; + } else { + match = node_task; + node = node->rb_left; + } + } + + return match; +} + /* * The static-key + stop-machine variable are needed such that: * @@ -122,6 +275,11 @@ void sched_core_put(void) mutex_unlock(&sched_core_mutex); } +#else /* !CONFIG_SCHED_CORE */ + +static inline void sched_core_enqueue(struct rq *rq, struct task_struct *p) { } +static inline void sched_core_dequeue(struct rq *rq, struct task_struct *p) { } + #endif /* CONFIG_SCHED_CORE */ /* @@ -826,6 +984,9 @@ static void set_load_weight(struct task_struct *p, bool update_load) static inline void enqueue_task(struct rq *rq, struct task_struct *p, int flags) { + if (sched_core_enabled(rq)) + sched_core_enqueue(rq, p); + if (!(flags & ENQUEUE_NOCLOCK)) update_rq_clock(rq); @@ -839,6 +1000,9 @@ static inline void enqueue_task(struct rq *rq, struct task_struct *p, int flags) static inline void dequeue_task(struct rq *rq, struct task_struct *p, int flags) { + if (sched_core_enabled(rq)) + sched_core_dequeue(rq, p); + if (!(flags & DEQUEUE_NOCLOCK)) update_rq_clock(rq); diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h index eb38063221d0..42dd620797d7 100644 --- a/kernel/sched/sched.h +++ b/kernel/sched/sched.h @@ -957,6 +957,10 @@ struct rq { /* per rq */ struct rq *core; unsigned int core_enabled; + struct rb_root core_tree; + + /* shared state */ + unsigned int core_task_seq; #endif }; -- 2.17.1