From patchwork Sun Apr 26 17:10:57 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xunlei Pang X-Patchwork-Id: 47589 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-lb0-f197.google.com (mail-lb0-f197.google.com [209.85.217.197]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 5B31420553 for ; Sun, 26 Apr 2015 17:13:57 +0000 (UTC) Received: by lbcne10 with SMTP id ne10sf20343289lbc.1 for ; Sun, 26 Apr 2015 10:13:56 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:delivered-to:from:to:cc:subject :date:message-id:in-reply-to:references:sender:precedence:list-id :x-original-sender:x-original-authentication-results:mailing-list :list-post:list-help:list-archive:list-unsubscribe; bh=h1HTXBlfys5i1Us5lMAre19A6ECAyD1xRYAgXCy87fY=; b=YRLNdJymFmWlFOxxmzdn27gAk62983CaoC3HEAXMdUjXuhBQIM5nYCQPjlJj16EL7c Tf/Wukfm97NbywyzT1Leoq3H5C0wXrvX4M6XB+pyhdC/47oI0OurdspPiR10qe6H93T6 7xbaTokUO//gNW4yDNp0PfDVPAAazV8xyThpuEjEmzwwAWrvR0qP23jm1RdU8kTPccBy ZE1COrcFY/ReuH1Zqna0FeEIg6/uNNKliCL0Mfrn3jM+fjxHYVR5peRKhQgddrmkYzMP ROPc8btwgKRCcM7pdMg3pBVDRXmvk1NodHikqym0tGXW7qXk+4eS23qzSQkCCYoVUluW +VCA== X-Gm-Message-State: ALoCoQlCoC8Ts2CZLwBAid9EPmyO8Gd88aPVsdQfZy3pGx2TeLOEcWhvy43wSLBLdsq6aoP7kGwo X-Received: by 10.112.118.162 with SMTP id kn2mr5028863lbb.22.1430068436266; Sun, 26 Apr 2015 10:13:56 -0700 (PDT) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.152.43.111 with SMTP id v15ls681187lal.5.gmail; Sun, 26 Apr 2015 10:13:56 -0700 (PDT) X-Received: by 10.112.168.165 with SMTP id zx5mr6705922lbb.111.1430068436096; Sun, 26 Apr 2015 10:13:56 -0700 (PDT) Received: from mail-lb0-x22a.google.com (mail-lb0-x22a.google.com. [2a00:1450:4010:c04::22a]) by mx.google.com with ESMTPS id y8si11246311laj.161.2015.04.26.10.13.56 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 26 Apr 2015 10:13:56 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 2a00:1450:4010:c04::22a as permitted sender) client-ip=2a00:1450:4010:c04::22a; Received: by lbbzk7 with SMTP id zk7so67034257lbb.0 for ; Sun, 26 Apr 2015 10:13:56 -0700 (PDT) X-Received: by 10.112.199.133 with SMTP id jk5mr7045766lbc.32.1430068435989; Sun, 26 Apr 2015 10:13:55 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patch@linaro.org Received: by 10.112.67.65 with SMTP id l1csp868545lbt; Sun, 26 Apr 2015 10:13:54 -0700 (PDT) X-Received: by 10.68.233.71 with SMTP id tu7mr14976565pbc.106.1430068433814; Sun, 26 Apr 2015 10:13:53 -0700 (PDT) Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id ph4si14346292pdb.101.2015.04.26.10.13.52; Sun, 26 Apr 2015 10:13:53 -0700 (PDT) Received-SPF: none (google.com: linux-kernel-owner@vger.kernel.org does not designate permitted sender hosts) client-ip=209.132.180.67; Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752496AbbDZRNt (ORCPT + 27 others); Sun, 26 Apr 2015 13:13:49 -0400 Received: from m15-114.126.com ([220.181.15.114]:53269 "EHLO m15-114.126.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750914AbbDZRMX (ORCPT ); Sun, 26 Apr 2015 13:12:23 -0400 Received: from localhost.localdomain (unknown [220.166.221.63]) by smtp7 (Coremail) with SMTP id DsmowACX+G4iHD1V8BojAA--.830S6; Mon, 27 Apr 2015 01:11:09 +0800 (CST) From: Xunlei Pang To: linux-kernel@vger.kernel.org Cc: Peter Zijlstra , Steven Rostedt , Juri Lelli , Ingo Molnar , Xunlei Pang Subject: [RFC PATCH 5/6] sched/rt: Fix wrong SMP scheduler behavior for equal prio cases Date: Mon, 27 Apr 2015 01:10:57 +0800 Message-Id: <1430068258-1960-5-git-send-email-xlpang@126.com> X-Mailer: git-send-email 2.1.0 In-Reply-To: <1430068258-1960-1-git-send-email-xlpang@126.com> References: <1430068258-1960-1-git-send-email-xlpang@126.com> X-CM-TRANSID: DsmowACX+G4iHD1V8BojAA--.830S6 X-Coremail-Antispam: 1Uf129KBjvJXoW3AF1kuw17Gw13WF1xXry3XFb_yoWfKF4kpa 95Ww18Ja1ktay2grZ7Zr48ZrW5KwnYqa13Jr93KayrKr1rt34vgFnYyryayFs8ur109FWa yF4kKrZxGr1jqrJanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x07jMg4hUUUUU= X-Originating-IP: [220.166.221.63] X-CM-SenderInfo: p0ost0bj6rjloofrz/1tbipBTnv1GogZ1G-wAAs0 Sender: linux-kernel-owner@vger.kernel.org Precedence: list List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Original-Sender: xlpang@126.com X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 2a00:1450:4010:c04::22a as permitted sender) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org; dkim=neutral (body hash did not verify) header.i=@; dmarc=fail (p=NONE dis=NONE) header.from=126.com Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org X-Google-Group-Id: 836684582541 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , From: Xunlei Pang We know, there are two main queues each cpu for RT scheduler: Let's call them "run queue" and "pushable queue" respectively. For RT tasks, the scheduler uses "plist" to manage the pushable queue, so when there are multiple tasks queued at the same priority, they get queued in the strict FIFO order. Currently, when an rt task gets queued, it is put to the head or the tail of its "run queue" at the same priority according to different scenarios. Then if it is migratable, it will also and always be put to the tail of its "pushable queue" at the same priority. For one cpu, assuming initially it has some migratable tasks queued at the same priority as current(RT) both in "run queue" and "pushable queue" in the same order. At some time, when current gets preempted, it will be put behind these tasks in the "pushable queue", while it still stays ahead of these tasks in the "run queue". Afterwards, if there comes a pull from other cpu or a push from local cpu, the task behind current in the "run queue" will be removed from the "pushable queue" and gets running, as the global rt scheduler fetches tasks from the head of the "pushable queue" to do pulling or pushing. Obviously, to maintain the same order between the two queues, when current is preempted(not involving re-queue in the "run queue"), we want to put it ahead of all those tasks queued at the same priority in the "pushable queue". So, if a task is running and gets preempted by a higher priority task or even with same priority for migrating, this patch ensures that it is put ahead of any existing task with the same priority in the "pushable queue". The dealing logic used here: - Add a new variable named "rt_preempt"(define a new flag named RT_PREEMPT_QUEUEAHEAD for it) in task_struct, used by RT. - When doing preempted resched_curr() for current RT, set the flag. Create a new resched_curr_preempted_rt() for this function, and replace all the possible resched_curr() used for rt preemption with resched_curr_preempted_rt(). - In put_prev_task_rt(), test RT_PREEMPT_QUEUEAHEAD if set, enqueue the task ahead in the "pushable queue". Signed-off-by: Xunlei Pang --- include/linux/sched.h | 5 +++ include/linux/sched/rt.h | 16 ++++++++ kernel/sched/core.c | 6 ++- kernel/sched/rt.c | 96 ++++++++++++++++++++++++++++++++++++++++++------ 4 files changed, 110 insertions(+), 13 deletions(-) diff --git a/include/linux/sched.h b/include/linux/sched.h index f74d4cc..24e0f72 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -1321,6 +1321,11 @@ struct task_struct { const struct sched_class *sched_class; struct sched_entity se; struct sched_rt_entity rt; + +#ifdef CONFIG_SMP + unsigned long rt_preempt; /* Used by rt */ +#endif + #ifdef CONFIG_CGROUP_SCHED struct task_group *sched_task_group; #endif diff --git a/include/linux/sched/rt.h b/include/linux/sched/rt.h index 6341f5b..69e3c82 100644 --- a/include/linux/sched/rt.h +++ b/include/linux/sched/rt.h @@ -15,6 +15,22 @@ static inline int rt_task(struct task_struct *p) return rt_prio(p->prio); } +struct rq; + +#ifdef CONFIG_SMP +extern void resched_curr_preempted_rt(struct rq *rq); + +static inline void resched_curr_preempted(struct rq *rq) +{ + resched_curr_preempted_rt(rq); +} +#else +static inline void resched_curr_preempted(struct rq *rq) +{ + rsched_curr(rq); +} +#endif + #ifdef CONFIG_RT_MUTEXES extern int rt_mutex_getprio(struct task_struct *p); extern void rt_mutex_setprio(struct task_struct *p, int prio); diff --git a/kernel/sched/core.c b/kernel/sched/core.c index dc646a6..64a1603 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -1002,7 +1002,7 @@ void check_preempt_curr(struct rq *rq, struct task_struct *p, int flags) if (class == rq->curr->sched_class) break; if (class == p->sched_class) { - resched_curr(rq); + resched_curr_preempted(rq); break; } } @@ -1833,6 +1833,10 @@ static void __sched_fork(unsigned long clone_flags, struct task_struct *p) INIT_LIST_HEAD(&p->rt.run_list); +#ifdef CONFIG_SMP + p->rt_preempt = 0; +#endif + #ifdef CONFIG_PREEMPT_NOTIFIERS INIT_HLIST_HEAD(&p->preempt_notifiers); #endif diff --git a/kernel/sched/rt.c b/kernel/sched/rt.c index ddd5b19..e7d66eb 100644 --- a/kernel/sched/rt.c +++ b/kernel/sched/rt.c @@ -254,8 +254,33 @@ int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent) } #endif /* CONFIG_RT_GROUP_SCHED */ + #ifdef CONFIG_SMP +#define RT_PREEMPT_QUEUEAHEAD 1UL + +/* + * p(current) was preempted, and to be put ahead of + * any task with the same priority in pushable queue. + */ +static inline bool rt_preempted(struct task_struct *p) +{ + return !!(p->rt_preempt & RT_PREEMPT_QUEUEAHEAD); +} + +static inline void clear_rt_preempt(struct task_struct *p) +{ + p->rt_preempt = 0; +} + +void resched_curr_preempted_rt(struct rq *rq) +{ + if (rt_task(rq->curr)) + rq->curr->rt_preempt |= RT_PREEMPT_QUEUEAHEAD; + + resched_curr(rq); +} + static int pull_rt_task(struct rq *this_rq); static inline bool need_pull_rt_task(struct rq *rq, struct task_struct *prev) @@ -359,17 +384,32 @@ static inline void set_post_schedule(struct rq *rq) rq->post_schedule = has_pushable_tasks(rq); } -static void enqueue_pushable_task(struct rq *rq, struct task_struct *p) +static void +__enqueue_pushable_task(struct rq *rq, struct task_struct *p, bool head) { plist_del(&p->pushable_tasks, &rq->rt.pushable_tasks); plist_node_init(&p->pushable_tasks, p->prio); - plist_add(&p->pushable_tasks, &rq->rt.pushable_tasks); + if (head) + plist_add_head(&p->pushable_tasks, &rq->rt.pushable_tasks); + else + plist_add_tail(&p->pushable_tasks, &rq->rt.pushable_tasks); /* Update the highest prio pushable task */ if (p->prio < rq->rt.highest_prio.next) rq->rt.highest_prio.next = p->prio; } +static inline +void enqueue_pushable_task_preempted(struct rq *rq, struct task_struct *curr) +{ + __enqueue_pushable_task(rq, curr, true); +} + +static inline void enqueue_pushable_task(struct rq *rq, struct task_struct *p) +{ + __enqueue_pushable_task(rq, p, false); +} + static void dequeue_pushable_task(struct rq *rq, struct task_struct *p) { plist_del(&p->pushable_tasks, &rq->rt.pushable_tasks); @@ -385,6 +425,25 @@ static void dequeue_pushable_task(struct rq *rq, struct task_struct *p) #else +static inline bool rt_preempted(struct task_struct *p) +{ + return false; +} + +static inline void clear_rt_preempt(struct task_struct *p) +{ +} + +static inline void resched_curr_preempted_rt(struct rq *rq) +{ + resched_curr(rq); +} + +static inline +void enqueue_pushable_task_preempted(struct rq *rq, struct task_struct *p) +{ +} + static inline void enqueue_pushable_task(struct rq *rq, struct task_struct *p) { } @@ -489,7 +548,7 @@ static void sched_rt_rq_enqueue(struct rt_rq *rt_rq) enqueue_rt_entity(rt_se, false); if (rt_rq->highest_prio.curr < curr->prio) - resched_curr(rq); + resched_curr_preempted_rt(rq); } } @@ -967,7 +1026,7 @@ static void update_curr_rt(struct rq *rq) raw_spin_lock(&rt_rq->rt_runtime_lock); rt_rq->rt_time += delta_exec; if (sched_rt_runtime_exceeded(rt_rq)) - resched_curr(rq); + resched_curr_preempted_rt(rq); raw_spin_unlock(&rt_rq->rt_runtime_lock); } } @@ -1409,7 +1468,7 @@ static void check_preempt_equal_prio_common(struct rq *rq) * to try and push current away. */ requeue_task_rt(rq, next, 1); - resched_curr(rq); + resched_curr_preempted_rt(rq); } static inline @@ -1434,7 +1493,7 @@ void check_preempt_equal_prio(struct rq *rq, struct task_struct *p) static void check_preempt_curr_rt(struct rq *rq, struct task_struct *p, int flags) { if (p->prio < rq->curr->prio) { - resched_curr(rq); + resched_curr_preempted_rt(rq); return; } @@ -1544,8 +1603,21 @@ static void put_prev_task_rt(struct rq *rq, struct task_struct *p) * The previous task needs to be made eligible for pushing * if it is still active */ - if (on_rt_rq(&p->rt) && p->nr_cpus_allowed > 1) - enqueue_pushable_task(rq, p); + if (on_rt_rq(&p->rt) && p->nr_cpus_allowed > 1) { + /* + * When put_prev_task_rt() is called by + * pick_next_task_rt(), if the current rt task + * is being preempted, to maintain FIFO, it must + * stay ahead of any other task that is queued + * at the same priority. + */ + if (rt_preempted(p)) + enqueue_pushable_task_preempted(rq, p); + else + enqueue_pushable_task(rq, p); + } + + clear_rt_preempt(p); } #ifdef CONFIG_SMP @@ -1764,7 +1836,7 @@ retry: * just reschedule current. */ if (unlikely(next_task->prio < rq->curr->prio)) { - resched_curr(rq); + resched_curr_preempted_rt(rq); return 0; } @@ -1811,7 +1883,7 @@ retry: activate_task(lowest_rq, next_task, 0); ret = 1; - resched_curr(lowest_rq); + resched_curr_preempted_rt(lowest_rq); double_unlock_balance(rq, lowest_rq); @@ -2236,7 +2308,7 @@ static void switched_to_rt(struct rq *rq, struct task_struct *p) check_resched = 0; #endif /* CONFIG_SMP */ if (check_resched && p->prio < rq->curr->prio) - resched_curr(rq); + resched_curr_preempted_rt(rq); } } @@ -2278,7 +2350,7 @@ prio_changed_rt(struct rq *rq, struct task_struct *p, int oldprio) * then reschedule. */ if (p->prio < rq->curr->prio) - resched_curr(rq); + resched_curr_preempted_rt(rq); } }