Skip to contentSkip to conversation

sched: SCHED_DEADLINE v4

RFC PATCH In review Juri Lelli 10 participants 129 messages Apr 6, 2012 – May 15, 2012 1 message not in the archive

Subsystems (12)
lore
Share & tools
lore

Juri Lelli

author
#0 lore raw

cover sched: SCHED_DEADLINE v4

Hello everyone,

This is the take 4 for the SCHED_DEADLINE patchset.

Just to recap, the patchset introduces a new deadline based real-time task scheduling policy --called SCHED_DEADLINE-- with bandwidth isolation (aka "resource reservation") capabilities. It now supports global/clustered multiprocessor scheduling through dynamic task migrations.

From the previous releases[1]:
- all the comments and the fixes coming from the reviews we got have

    been considered and applied;

- better handling of rq selection for dynamic task migration, by means of a cpupri equivalent for -deadline tasks (cpudl). The mechanism is simple and straightforward, but showed nice performance figures[2].
- this time we sit on top of PREEMPT_RT (3.2.13-rt23); we continue to aim at mainline inclusion, but we also see -rt folks as immediate and interested users.

Still missing/incomplete:
- (c)group based bandwidth management, and maybe scheduling. It seems some more discussion on what precisely we want is *really* needed for this point;
- bandwidth inheritance (to replace deadline/priority inheritance). What's in the patchset is just very few more than a simple placeholder. More discussion on the right way to go is needed here. Some work has already been done, but it is still not ready for submission.

The development is taking place at:
https://github.com/jlelli/sched-deadline

Check the repositories frequently if you're interested, and feel free to e-mail me for any issue you run into.

Furthermore, we developed an application that you can use to test this patchset:
https://github.com/gbagnoli/rt-app

We also set up a development mailing list: linux-dl[3]. You can subscribe from here:
http://feanor.sssup.it/mailman/listinfo/linux-dl
or via e-mail (send a message to [email protected] with just the word `help' as subject or in the body to receive info).

As already discussed we are planning to merge this work with the EDF throttling patches [https://lkml.org/lkml/2010/2/23/239] but we still are in the preliminary phases of the merge and we intend to use the feedback to this post to help us decide on the direction it should take.

As said, patchset is on top of PREEMPT_RT (as of today). However, Insop Song (from Ericsson) is maintaining a parallel branch for the current tip/master (https://github.com/insop/sched-deadline2). Ericsson is in fact evaluating the use of SCHED_DEADLINE for CPE (Customer Premise Equipment) devices in order to reserve CPU bandwidth to processes.

The code was being jointly developed by ReTiS Lab (http://retis.sssup.it)
and Evidence S.r.l (http://www.evidence.eu.com) in the context of the ACTORS EU-funded project (http://www.actors-project.eu). It is now also supported by the S(o)OS EU-funded project (http://www.soos-project.eu/).
It has also some users, both in academic and applied research. Even if our last release dates back almost a year we continued to get feedback from Ericsson (see above), Wind River, Porto (ISEP), Trento and Malardalen universities :-).

As usual, any kind of feedback is welcome and appreciated.

Thanks in advice and regards,

- Juri

[1] http://lwn.net/Articles/376502, http://lwn.net/Articles/353797,

    http://lwn.net/Articles/412410

[2] http://retis.sssup.it/~jlelli/papers/Ospert11Lelli.pdf
[3] from the first linux-dl message:
-linux-dl should become the place where discussions about real-time
deadline scheduling on Linux take place (not only SCHED_DEADLINE). We felt the lack of a place where we can keep in touch with each other; we are all working on the same things, but probably from different viewpoints, and this is surely a point of strength. Anyway, our efforts need to be organized in some way, or at least it is important to know on what everyone is currently working as to not end up with "duplicate-efforts".-

Dario Faggioli (10):
sched: add extended scheduling interface.
sched: SCHED_DEADLINE data structures.
sched: SCHED_DEADLINE policy implementation.
sched: SCHED_DEADLINE avg_update accounting.
sched: add schedstats for -deadline tasks.
sched: add resource limits for -deadline tasks.
sched: add latency tracing for -deadline tasks.
sched: drafted deadline inheritance logic.
sched: add bandwidth management for sched_dl.
sched: add sched_dl documentation.

Juri Lelli (3):
sched: SCHED_DEADLINE SMP-related data structures.
sched: SCHED_DEADLINE push and pull logic
sched: speed up -dl pushes with a push-heap.

Harald Gustafsson (1):
sched: add period support for -deadline tasks.

Peter Zijlstra (1):
rtmutex: turn the plist into an rb-tree.

 Documentation/scheduler/sched-deadline.txt |  147 +++
 arch/arm/include/asm/unistd.h              |    3 +
 arch/arm/kernel/calls.S                    |    3 +
 arch/x86/ia32/ia32entry.S                  |    3 +
 arch/x86/include/asm/unistd_32.h           |    5 +-
 arch/x86/include/asm/unistd_64.h           |    6 +
 arch/x86/kernel/syscall_table_32.S         |    3 +
 include/asm-generic/resource.h             |    7 +-
 include/linux/init_task.h                  |   10 +
 include/linux/rtmutex.h                    |   18 +-
 include/linux/sched.h                      |  154 +++-
 include/linux/syscalls.h                   |    7 +
 kernel/Makefile                            |    1 +
 kernel/fork.c                              |    8 +-
 kernel/hrtimer.c                           |    2 +-
 kernel/rtmutex-debug.c                     |   10 +-
 kernel/rtmutex.c                           |  162 +++-
 kernel/rtmutex_common.h                    |   24 +-
 kernel/sched.c                             |  866 ++++++++++++++-
 kernel/sched_cpudl.c                       |  208 ++++
 kernel/sched_cpudl.h                       |   34 +
 kernel/sched_debug.c                       |   43 +
 kernel/sched_dl.c                          | 1585 ++++++++++++++++++++++++++++
 kernel/sched_rt.c                          |    3 +-
 kernel/sched_stoptask.c                    |    2 +-
 kernel/sysctl.c                            |   14 +
 kernel/trace/trace_sched_wakeup.c          |   41 +-
 kernel/trace/trace_selftest.c              |   30 +-
 28 files changed, 3266 insertions(+), 133 deletions(-)
author
#5 lore raw

PATCH 3/16 sched: SCHED_DEADLINE data structures.

From: Dario Faggioli <[email protected]>

Introduce the data structures, constants and symbols needed for SCHED_DEADLINE implementation.

Core data structure of SCHED_DEADLINE are defined, along with their initializers. Hooks for checking if a task belong to the new policy are also added where they are needed.

---
 include/linux/sched.h |   68 +++++++++++++++++++++++++++++++++++++++++++++-
 kernel/hrtimer.c      |    2 +-
 kernel/sched.c        |   73 +++++++++++++++++++++++++++++++++++++++++++------
 3 files changed, 132 insertions(+), 11 deletions(-)
include/linux/sched.h +67 -1
index 3e67d30..a7a4276 100644
@@ -39,6 +39,7 @@
 #define SCHED_BATCH		3
 /* SCHED_ISO: reserved but not implemented yet */
 #define SCHED_IDLE		5
+#define SCHED_DEADLINE		6
 /* Can be ORed in to make sure the process is reverted back to SCHED_NORMAL on fork */
 #define SCHED_RESET_ON_FORK     0x40000000
 
@@ -133,6 +134,10 @@ struct sched_param {
  * timing constraints.
  *
  * @__unused		padding to allow future expansion without ABI issues
+ *
+ * As of now, the SCHED_DEADLINE policy (sched_dl scheduling class) is the
+ * only user of this new interface. More information about the algorithm
+ * available in the scheduling class file or in Documentation/.
  */
 struct sched_param2 {
 	int sched_priority;
@@ -1130,6 +1135,7 @@ struct sched_domain;
 #else
 #define ENQUEUE_WAKING		0
 #endif
+#define ENQUEUE_REPLENISH	8
 
 #define DEQUEUE_SLEEP		1
 
@@ -1261,6 +1267,47 @@ struct sched_rt_entity {
 #endif
 };
 
+struct sched_dl_entity {
+	struct rb_node	rb_node;
+	int nr_cpus_allowed;
+
+	/*
+	 * Original scheduling parameters. Copied here from sched_param2
+	 * during sched_setscheduler2(), they will remain the same until
+	 * the next sched_setscheduler2().
+	 */
+	u64 dl_runtime;		/* maximum runtime for each instance	*/
+	u64 dl_deadline;	/* relative deadline of each instance	*/
+
+	/*
+	 * Actual scheduling parameters. Initialized with the values above,
+	 * they are continously updated during task execution. Note that
+	 * the remaining runtime could be < 0 in case we are in overrun.
+	 */
+	s64 runtime;		/* remaining runtime for this instance	*/
+	u64 deadline;		/* absolute deadline for this instance	*/
+	unsigned int flags;	/* specifying the scheduler behaviour	*/
+
+	/*
+	 * Some bool flags:
+	 *
+	 * @dl_throttled tells if we exhausted the runtime. If so, the
+	 * task has to wait for a replenishment to be performed at the
+	 * next firing of dl_timer.
+	 *
+	 * @dl_new tells if a new instance arrived. If so we must
+	 * start executing it with full runtime and reset its absolute
+	 * deadline;
+	 */
+	int dl_throttled, dl_new;
+
+	/*
+	 * Bandwidth enforcement timer. Each -deadline task has its
+	 * own bandwidth to be enforced, thus we need one timer per task.
+	 */
+	struct hrtimer dl_timer;
+};
+
 struct rcu_node;
 
 enum perf_event_task_context {
@@ -1289,6 +1336,7 @@ 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_PREEMPT_NOTIFIERS
 	/* list of struct preempt_notifier: */
@@ -1681,6 +1729,10 @@ static inline bool pagefault_disabled(void)
  * user-space.  This allows kernel threads to set their
  * priority to a value higher than any user task. Note:
  * MAX_RT_PRIO must not be smaller than MAX_USER_RT_PRIO.
+ *
+ * SCHED_DEADLINE tasks has negative priorities, reflecting
+ * the fact that any of them has higher prio than RT and
+ * NORMAL/BATCH tasks.
  */
 
 #define MAX_USER_RT_PRIO	100
@@ -1689,9 +1741,23 @@ static inline bool pagefault_disabled(void)
 #define MAX_PRIO		(MAX_RT_PRIO + 40)
 #define DEFAULT_PRIO		(MAX_RT_PRIO + 20)
 
+#define MAX_DL_PRIO		0
+
+static inline int dl_prio(int prio)
+{
+	if (unlikely(prio < MAX_DL_PRIO))
+		return 1;
+	return 0;
+}
+
+static inline int dl_task(struct task_struct *p)
+{
+	return dl_prio(p->prio);
+}
+
 static inline int rt_prio(int prio)
 {
-	if (unlikely(prio < MAX_RT_PRIO))
+	if (unlikely(prio >= MAX_DL_PRIO && prio < MAX_RT_PRIO))
 		return 1;
 	return 0;
 }
kernel/hrtimer.c +1 -1
index 3991464..246842e 100644
@@ -1764,7 +1764,7 @@ long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
 	unsigned long slack;
 
 	slack = current->timer_slack_ns;
-	if (rt_task(current))
+	if (dl_task(current) || rt_task(current))
 		slack = 0;
 
 	hrtimer_init_on_stack(&t.timer, clockid, mode);
kernel/sched.c +64 -9

175 lines not on this page · raw message

Signature · 1 line
1.7.5.4
author
#4 lore raw

PATCH 2/16 sched: add extended scheduling interface.

In reply to Juri Lelli

From: Dario Faggioli <[email protected]>

Add the interface bits needed for supporting scheduling algorithms with extended parameters (e.g., SCHED_DEADLINE).

In general, it makes possible to specify a periodic/sporadic task, that executes for a given amount of runtime at each instance, and is scheduled according to the urgency of their own timing constraints, i.e.:
- a (maximum/typical) instance execution time,
- a minimum interval between consecutive instances,
- a time constraint by which each instance must be completed.

Thus, both the data structure that holds the scheduling parameters of the tasks and the system calls dealing with it must be extended. Unfortunately, modifying the existing struct sched_param would break the ABI and result in potentially serious compatibility issues with legacy binaries.

For these reasons, this patch:
- defines the new struct sched_param2, containing all the fields that are necessary for specifying a task in the computational model described above;
- defines and implements the new scheduling related syscalls that manipulate it, i.e., sched_setscheduler2(), sched_setparam2() and sched_getparam2().

Syscalls are introduced for x86 (32 and 64 bits) and ARM only, as a proof of concept and for developing and testing purposes. Making them available on other architectures is straightforward.

Since no "user" for these new parameters is introduced in this patch, the implementation of the new system calls is just identical to their already existing counterpart. Future patches that implement scheduling policies able to exploit the new data structure must also take care of modifying the *2() calls accordingly with their own purposes.

---
 arch/arm/include/asm/unistd.h      |    3 +
 arch/arm/kernel/calls.S            |    3 +
 arch/x86/ia32/ia32entry.S          |    3 +
 arch/x86/include/asm/unistd_32.h   |    5 +-
 arch/x86/include/asm/unistd_64.h   |    6 ++
 arch/x86/kernel/syscall_table_32.S |    3 +
 include/linux/sched.h              |   50 ++++++++++++++++
 include/linux/syscalls.h           |    7 ++
 kernel/sched.c                     |  110 +++++++++++++++++++++++++++++++++++-
 9 files changed, 186 insertions(+), 4 deletions(-)
arch/arm/include/asm/unistd.h +3 -0
index 4a11237..66fb25b 100644
@@ -404,6 +404,9 @@
 #define __NR_setns			(__NR_SYSCALL_BASE+375)
 #define __NR_process_vm_readv		(__NR_SYSCALL_BASE+376)
 #define __NR_process_vm_writev		(__NR_SYSCALL_BASE+377)
+#define __NR_sched_setscheduler2	(__NR_SYSCALL_BASE+378)
+#define __NR_sched_setparam2		(__NR_SYSCALL_BASE+379)
+#define __NR_sched_getparam2		(__NR_SYSCALL_BASE+380)
 
 /*
  * The following SWIs are ARM private.
arch/arm/kernel/calls.S +3 -0
index 463ff4a..be174eb 100644
@@ -387,6 +387,9 @@
 /* 375 */	CALL(sys_setns)
 		CALL(sys_process_vm_readv)
 		CALL(sys_process_vm_writev)
+		CALL(sys_sched_setscheduler2)
+		CALL(sys_sched_setparam2)
+/* 380 */	CALL(sys_sched_getparam2)
 #ifndef syscalls_counted
 .equ syscalls_padding, ((NR_syscalls + 3) & ~3) - NR_syscalls
 #define syscalls_counted
arch/x86/ia32/ia32entry.S +3 -0
index a6253ec..7a502fd 100644
@@ -852,4 +852,7 @@ ia32_sys_call_table:
 	.quad sys_setns
 	.quad compat_sys_process_vm_readv
 	.quad compat_sys_process_vm_writev
+	.quad sys_sched_setscheduler2
+	.quad sys_sched_setparam2	/* 350 */
+	.quad sys_sched_getparam2
 ia32_syscall_end:
arch/x86/include/asm/unistd_32.h +4 -1
index 599c77d..5658de0 100644
@@ -354,10 +354,13 @@
 #define __NR_setns		346
 #define __NR_process_vm_readv	347
 #define __NR_process_vm_writev	348
+#define __NR_sched_setscheduler2	349
+#define __NR_sched_setparam2		350
+#define __NR_sched_getparam2		351
 
 #ifdef __KERNEL__
 
-#define NR_syscalls 349
+#define NR_syscalls 352
 
 #define __ARCH_WANT_IPC_PARSE_VERSION
 #define __ARCH_WANT_OLD_READDIR
arch/x86/include/asm/unistd_64.h +6 -0
index 0431f19..b7a4347 100644
@@ -686,6 +686,12 @@ __SYSCALL(__NR_getcpu, sys_getcpu)
 __SYSCALL(__NR_process_vm_readv, sys_process_vm_readv)
 #define __NR_process_vm_writev			311
 __SYSCALL(__NR_process_vm_writev, sys_process_vm_writev)
+#define __NR_sched_setscheduler2		312
+__SYSCALL(__NR_sched_setscheduler2, sys_sched_setscheduler2)
+#define __NR_sched_setparam2			313
+__SYSCALL(__NR_sched_setparam2, sys_sched_setparam2)
+#define __NR_sched_getparam2			314
+__SYSCALL(__NR_sched_getparam2, sys_sched_getparam2)
 
 #ifndef __NO_STUBS
 #define __ARCH_WANT_OLD_READDIR
arch/x86/kernel/syscall_table_32.S +3 -0
index 9a0e312..b9b37b5 100644
@@ -348,3 +348,6 @@ ENTRY(sys_call_table)
 	.long sys_setns
 	.long sys_process_vm_readv
 	.long sys_process_vm_writev
+	.long sys_sched_setscheduler2
+	.long sys_sched_setparam2	/* 350 */
+	.long sys_sched_getparam2
include/linux/sched.h +50 -0

62 lines not on this page · raw message

include/linux/syscalls.h +7 -0
index 86a24b1..11449ae 100644
@@ -38,6 +38,7 @@ struct rlimit;
 struct rlimit64;
 struct rusage;
 struct sched_param;
+struct sched_param2;
 struct sel_arg_struct;
 struct semaphore;
 struct sembuf;
@@ -327,11 +328,17 @@ asmlinkage long sys_clock_nanosleep(clockid_t which_clock, int flags,
 asmlinkage long sys_nice(int increment);
 asmlinkage long sys_sched_setscheduler(pid_t pid, int policy,
 					struct sched_param __user *param);
+asmlinkage long sys_sched_setscheduler2(pid_t pid, int policy,
+					struct sched_param2 __user *param);
 asmlinkage long sys_sched_setparam(pid_t pid,
 					struct sched_param __user *param);
+asmlinkage long sys_sched_setparam2(pid_t pid,
+					struct sched_param2 __user *param);
 asmlinkage long sys_sched_getscheduler(pid_t pid);
 asmlinkage long sys_sched_getparam(pid_t pid,
 					struct sched_param __user *param);
+asmlinkage long sys_sched_getparam2(pid_t pid,
+					struct sched_param2 __user *param);
 asmlinkage long sys_sched_setaffinity(pid_t pid, unsigned int len,
 					unsigned long __user *user_mask_ptr);
 asmlinkage long sys_sched_getaffinity(pid_t pid, unsigned int len,
kernel/sched.c +107 -3

155 lines not on this page · raw message

Signature · 1 line
1.7.5.4
author
#63 lore raw

PATCH 6/16 sched: SCHED_DEADLINE push and pull logic

In reply to Juri Lelli

Add dynamic migrations to SCHED_DEADLINE, so that tasks can be moved among CPUs when necessary. It is also possible to bind a task to a (set of) CPU(s), thus restricting its capability of migrating, or forbidding migrations at all.

The very same approach used in sched_rt is utilised:
- -deadline tasks are kept into CPU-specific runqueues,
- -deadline tasks are migrated among runqueues to achieve the following:
* on an M-CPU system the M earliest deadline ready tasks are always running;
* affinity/cpusets settings of all the -deadline tasks is always respected.

Therefore, this very special form of "load balancing" is done with an active method, i.e., the scheduler pushes or pulls tasks between runqueues when they are woken up and/or (de)scheduled.
IOW, every time a preemption occurs, the descheduled task might be sent to some other CPU (depending on its deadline) to continue executing (push). On the other hand, every time a CPU becomes idle, it might pull the second earliest deadline ready task from some other CPU.

To enforce this, a pull operation is always attempted before taking any scheduling decision (pre_schedule()), as well as a push one after each scheduling decision (post_schedule()). In addition, when a task arrives or wakes up, the best CPU where to resume it is selected taking into account its affinity mask, the system topology, but also its deadline. E.g., from the scheduling point of view, the best CPU where to wake up (and also where to push) a task is the one which is running the task with the latest deadline among the M executing ones.

In order to facilitate these decisions, per-runqueue "caching" of the deadlines of the currently running and of the first ready task is used. Queued but not running tasks are also parked in another rb-tree to speed-up pushes.

---
 kernel/sched_dl.c |  912 +++++++++++++++++++++++++++++++++++++++++++++++++++--
 kernel/sched_rt.c |    2 +-
 2 files changed, 889 insertions(+), 25 deletions(-)
kernel/sched_dl.c +888 -24
index 604e2bc..38edefa 100644
@@ -10,6 +10,7 @@
  * miss some of their deadlines), and won't affect any other task.
  *
  * Copyright (C) 2010 Dario Faggioli <[email protected]>,
+ *                    Juri Lelli <[email protected]>,
  *                    Michael Trimarchi <[email protected]>,
  *                    Fabio Checconi <[email protected]>
  */
@@ -20,6 +21,15 @@ static inline int dl_time_before(u64 a, u64 b)
 	return (s64)(a - b) < 0;
 }
 
+/*
+ * Tells if entity @a should preempt entity @b.
+ */
+static inline
+int dl_entity_preempt(struct sched_dl_entity *a, struct sched_dl_entity *b)
+{
+	return dl_time_before(a->deadline, b->deadline);
+}
+
 static inline struct task_struct *dl_task_of(struct sched_dl_entity *dl_se)
 {
 	return container_of(dl_se, struct task_struct, dl);
@@ -50,6 +60,153 @@ static inline int is_leftmost(struct task_struct *p, struct dl_rq *dl_rq)
 	return dl_rq->rb_leftmost == &dl_se->rb_node;
 }
 
+#ifdef CONFIG_SMP
+
+static inline int dl_overloaded(struct rq *rq)
+{
+	return atomic_read(&rq->rd->dlo_count);
+}
+
+static inline void dl_set_overload(struct rq *rq)
+{
+	if (!rq->online)
+		return;
+
+	cpumask_set_cpu(rq->cpu, rq->rd->dlo_mask);
+	/*
+	 * Must be visible before the overload count is
+	 * set (as in sched_rt.c).
+	 */
+	wmb();
+	atomic_inc(&rq->rd->dlo_count);
+}
+
+static inline void dl_clear_overload(struct rq *rq)
+{
+	if (!rq->online)
+		return;
+
+	atomic_dec(&rq->rd->dlo_count);
+	cpumask_clear_cpu(rq->cpu, rq->rd->dlo_mask);
+}
+
+static void update_dl_migration(struct dl_rq *dl_rq)
+{
+	if (dl_rq->dl_nr_migratory && dl_rq->dl_nr_total > 1) {
+		if (!dl_rq->overloaded) {
+			dl_set_overload(rq_of_dl_rq(dl_rq));
+			dl_rq->overloaded = 1;
+		}
+	} else if (dl_rq->overloaded) {
+		dl_clear_overload(rq_of_dl_rq(dl_rq));
+		dl_rq->overloaded = 0;
+	}
+}
+
+static void inc_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
+{
+	dl_rq = &rq_of_dl_rq(dl_rq)->dl;
+
+	dl_rq->dl_nr_total++;
+	if (dl_se->nr_cpus_allowed > 1)
+		dl_rq->dl_nr_migratory++;
+
+	update_dl_migration(dl_rq);
+}
+
+static void dec_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
+{
+	dl_rq = &rq_of_dl_rq(dl_rq)->dl;
+
+	dl_rq->dl_nr_total--;
+	if (dl_se->nr_cpus_allowed > 1)
+		dl_rq->dl_nr_migratory--;
+
+	update_dl_migration(dl_rq);
+}
+
+/*
+ * The list of pushable -deadline task is not a plist, like in
+ * sched_rt.c, it is an rb-tree with tasks ordered by deadline.
+ */
+static void enqueue_pushable_dl_task(struct rq *rq, struct task_struct *p)
+{
+	struct dl_rq *dl_rq = &rq->dl;
+	struct rb_node **link = &dl_rq->pushable_dl_tasks_root.rb_node;
+	struct rb_node *parent = NULL;
+	struct task_struct *entry;
+	int leftmost = 1;
+
+	BUG_ON(!RB_EMPTY_NODE(&p->pushable_dl_tasks));
+
+	while (*link) {
+		parent = *link;
+		entry = rb_entry(parent, struct task_struct,
+				 pushable_dl_tasks);
+		if (!dl_entity_preempt(&entry->dl, &p->dl))
+			link = &parent->rb_left;
+		else {
+			link = &parent->rb_right;
+			leftmost = 0;
+		}
+	}
+
+	if (leftmost)
+		dl_rq->pushable_dl_tasks_leftmost = &p->pushable_dl_tasks;
+
+	rb_link_node(&p->pushable_dl_tasks, parent, link);
+	rb_insert_color(&p->pushable_dl_tasks, &dl_rq->pushable_dl_tasks_root);
+}
+
+static void dequeue_pushable_dl_task(struct rq *rq, struct task_struct *p)
+{
+	struct dl_rq *dl_rq = &rq->dl;
+
+	if (RB_EMPTY_NODE(&p->pushable_dl_tasks))
+		return;
+
+	if (dl_rq->pushable_dl_tasks_leftmost == &p->pushable_dl_tasks) {
+		struct rb_node *next_node;
+
+		next_node = rb_next(&p->pushable_dl_tasks);
+		dl_rq->pushable_dl_tasks_leftmost = next_node;
+	}
+
+	rb_erase(&p->pushable_dl_tasks, &dl_rq->pushable_dl_tasks_root);
+	RB_CLEAR_NODE(&p->pushable_dl_tasks);
+}
+
+static inline int has_pushable_dl_tasks(struct rq *rq)
+{
+	return !RB_EMPTY_ROOT(&rq->dl.pushable_dl_tasks_root);
+}
+
+static int push_dl_task(struct rq *rq);
+
+#else
+
+static inline
+void enqueue_pushable_dl_task(struct rq *rq, struct task_struct *p)
+{
+}
+
+static inline
+void dequeue_pushable_dl_task(struct rq *rq, struct task_struct *p)
+{
+}
+
+static inline
+void inc_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
+{
+}
+
+static inline
+void dec_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
+{
+}
+
+#endif /* CONFIG_SMP */
+
 static void enqueue_task_dl(struct rq *rq, struct task_struct *p, int flags);
 static void __dequeue_task_dl(struct rq *rq, struct task_struct *p, int flags);
 static void check_preempt_curr_dl(struct rq *rq, struct task_struct *p,
@@ -276,6 +433,14 @@ static enum hrtimer_restart dl_task_timer(struct hrtimer *timer)
 			check_preempt_curr_dl(rq, p, 0);
 		else
 			resched_task(rq->curr);
+#ifdef CONFIG_SMP
+		/*
+		 * Queueing this task back might have overloaded rq,
+		 * check if we need to kick someone away.
+		 */
+		if (rq->dl.overloaded)
+			push_dl_task(rq);
+#endif
 	}
 unlock:
 	task_rq_unlock(rq, p, &flags);
@@ -359,6 +524,100 @@ static void update_curr_dl(struct rq *rq)
 	}
 }
 
+#ifdef CONFIG_SMP
+
+static struct task_struct *pick_next_earliest_dl_task(struct rq *rq, int cpu);
+
+static inline int next_deadline(struct rq *rq)
+{
+	struct task_struct *next = pick_next_earliest_dl_task(rq, rq->cpu);
+
+	if (next && dl_prio(next->prio))
+		return next->dl.deadline;
+	else
+		return 0;
+}
+
+static void inc_dl_deadline(struct dl_rq *dl_rq, u64 deadline)
+{
+	struct rq *rq = rq_of_dl_rq(dl_rq);
+
+	if (dl_rq->earliest_dl.curr == 0 ||
+	    dl_time_before(deadline, dl_rq->earliest_dl.curr)) {
+		/*
+		 * If the dl_rq had no -deadline tasks, or if the new task
+		 * has shorter deadline than the current one on dl_rq, we
+		 * know that the previous earliest becomes our next earliest,
+		 * as the new task becomes the earliest itself.
+		 */
+		dl_rq->earliest_dl.next = dl_rq->earliest_dl.curr;
+		dl_rq->earliest_dl.curr = deadline;
+	} else if (dl_rq->earliest_dl.next == 0 ||
+		   dl_time_before(deadline, dl_rq->earliest_dl.next)) {
+		/*
+		 * On the other hand, if the new -deadline task has a
+		 * a later deadline than the earliest one on dl_rq, but
+		 * it is earlier than the next (if any), we must
+		 * recompute the next-earliest.
+		 */
+		dl_rq->earliest_dl.next = next_deadline(rq);
+	}
+}
+
+static void dec_dl_deadline(struct dl_rq *dl_rq, u64 deadline)
+{
+	struct rq *rq = rq_of_dl_rq(dl_rq);
+
+	/*
+	 * Since we may have removed our earliest (and/or next earliest)
+	 * task we must recompute them.
+	 */
+	if (!dl_rq->dl_nr_running) {
+		dl_rq->earliest_dl.curr = 0;
+		dl_rq->earliest_dl.next = 0;
+	} else {
+		struct rb_node *leftmost = dl_rq->rb_leftmost;
+		struct sched_dl_entity *entry;
+
+		entry = rb_entry(leftmost, struct sched_dl_entity, rb_node);
+		dl_rq->earliest_dl.curr = entry->deadline;
+		dl_rq->earliest_dl.next = next_deadline(rq);
+	}
+}
+
+#else
+
+static inline void inc_dl_deadline(struct dl_rq *dl_rq, u64 deadline) {}
+static inline void dec_dl_deadline(struct dl_rq *dl_rq, u64 deadline) {}
+
+#endif /* CONFIG_SMP */
+
+static inline
+void inc_dl_tasks(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
+{
+	int prio = dl_task_of(dl_se)->prio;
+	u64 deadline = dl_se->deadline;
+
+	WARN_ON(!dl_prio(prio));
+	dl_rq->dl_nr_running++;
+
+	inc_dl_deadline(dl_rq, deadline);
+	inc_dl_migration(dl_se, dl_rq);
+}
+
+static inline
+void dec_dl_tasks(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
+{
+	int prio = dl_task_of(dl_se)->prio;
+
+	WARN_ON(!dl_prio(prio));
+	WARN_ON(!dl_rq->dl_nr_running);
+	dl_rq->dl_nr_running--;
+
+	dec_dl_deadline(dl_rq, dl_se->deadline);
+	dec_dl_migration(dl_se, dl_rq);
+}
+
 static void __enqueue_dl_entity(struct sched_dl_entity *dl_se)
 {
 	struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
@@ -386,7 +645,7 @@ static void __enqueue_dl_entity(struct sched_dl_entity *dl_se)
 	rb_link_node(&dl_se->rb_node, parent, link);
 	rb_insert_color(&dl_se->rb_node, &dl_rq->rb_root);
 
-	dl_rq->dl_nr_running++;
+	inc_dl_tasks(dl_se, dl_rq);
 }
 
 static void __dequeue_dl_entity(struct sched_dl_entity *dl_se)
@@ -406,7 +665,7 @@ static void __dequeue_dl_entity(struct sched_dl_entity *dl_se)
 	rb_erase(&dl_se->rb_node, &dl_rq->rb_root);
 	RB_CLEAR_NODE(&dl_se->rb_node);
 
-	dl_rq->dl_nr_running--;
+	dec_dl_tasks(dl_se, dl_rq);
 }
 
 static void
@@ -444,11 +703,15 @@ static void enqueue_task_dl(struct rq *rq, struct task_struct *p, int flags)
 		return;
 
 	enqueue_dl_entity(&p->dl, flags);
+
+	if (!task_current(rq, p) && p->dl.nr_cpus_allowed > 1)
+		enqueue_pushable_dl_task(rq, p);
 }
 
 static void __dequeue_task_dl(struct rq *rq, struct task_struct *p, int flags)
 {
 	dequeue_dl_entity(&p->dl);
+	dequeue_pushable_dl_task(rq, p);
 }
 
 static void dequeue_task_dl(struct rq *rq, struct task_struct *p, int flags)
@@ -480,6 +743,75 @@ static void yield_task_dl(struct rq *rq)
 	update_curr_dl(rq);
 }
 
+#ifdef CONFIG_SMP
+
+static int find_later_rq(struct task_struct *task);
+static int latest_cpu_find(struct cpumask *span,
+			   struct task_struct *task,
+			   struct cpumask *later_mask);
+
+static int
+select_task_rq_dl(struct task_struct *p, int sd_flag, int flags)
+{
+	struct task_struct *curr;
+	struct rq *rq;
+	int cpu;
+
+	if (sd_flag != SD_BALANCE_WAKE)
+		return smp_processor_id();
+
+	cpu = task_cpu(p);
+	rq = cpu_rq(cpu);
+
+	rcu_read_lock();
+	curr = ACCESS_ONCE(rq->curr); /* unlocked access */
+
+	/*
+	 * If we are dealing with a -deadline task, we must
+	 * decide where to wake it up.
+	 * If it has a later deadline and the current task
+	 * on this rq can't move (provided the waking task
+	 * can!) we prefer to send it somewhere else. On the
+	 * other hand, if it has a shorter deadline, we
+	 * try to make it stay here, it might be important.
+	 */
+	if (unlikely(dl_task(rq->curr)) &&
+	    (rq->curr->dl.nr_cpus_allowed < 2 ||
+	     dl_entity_preempt(&rq->curr->dl, &p->dl)) &&
+	    (p->dl.nr_cpus_allowed > 1)) {
+		int target = find_later_rq(p);
+
+		if (target != -1)
+			cpu = target;
+	}
+	rcu_read_unlock();
+
+	return cpu;
+}
+
+static void check_preempt_equal_dl(struct rq *rq, struct task_struct *p)
+{
+	/*
+	 * Current can't be migrated, useles to reschedule,
+	 * let's hope p can move out.
+	 */
+	if (rq->curr->dl.nr_cpus_allowed == 1 ||
+	    latest_cpu_find(rq->rd->span, rq->curr, NULL) == -1)
+		return;
+
+	/*
+	 * p is migratable, so let's not schedule it and
+	 * see if it is pushed or pulled somewhere else.
+	 */
+	if (p->dl.nr_cpus_allowed != 1 &&
+	    latest_cpu_find(rq->rd->span, p, NULL) != -1)
+		return;
+
+	resched_task(rq->curr);
+}
+
+#endif /* CONFIG_SMP */
+
 /*
  * Only called when both the current and waking task are -deadline
  * tasks.
@@ -487,8 +819,20 @@ static void yield_task_dl(struct rq *rq)
 static void check_preempt_curr_dl(struct rq *rq, struct task_struct *p,
 				  int flags)
 {
-	if (dl_time_before(p->dl.deadline, rq->curr->dl.deadline))
+	if (dl_time_before(p->dl.deadline, rq->curr->dl.deadline)) {
 		resched_task(rq->curr);
+		return;
+	}
+
+#ifdef CONFIG_SMP
+	/*
+	 * In the unlikely case current and p have the same deadline
+	 * let us try to decide what's the best thing to do...
+	 */
+	if ((s64)(p->dl.deadline - rq->curr->dl.deadline) == 0 &&
+	    !need_resched())
+		check_preempt_equal_dl(rq, p);
+#endif /* CONFIG_SMP */
 }
 
 #ifdef CONFIG_SCHED_HRTICK
@@ -532,10 +876,20 @@ struct task_struct *pick_next_task_dl(struct rq *rq)
 
 	p = dl_task_of(dl_se);
 	p->se.exec_start = rq->clock;
+
+	/* Running task will never be pushed. */
+	if (p)
+		dequeue_pushable_dl_task(rq, p);
+
 #ifdef CONFIG_SCHED_HRTICK
 	if (hrtick_enabled(rq))
 		start_hrtick_dl(rq, p);
 #endif
+
+#ifdef CONFIG_SMP
+	rq->post_schedule = has_pushable_dl_tasks(rq);
+#endif /* CONFIG_SMP */
+
 	return p;
 }
 
@@ -543,6 +897,9 @@ static void put_prev_task_dl(struct rq *rq, struct task_struct *p)
 {
 	update_curr_dl(rq);
 	p->se.exec_start = 0;
+
+	if (on_dl_rq(&p->dl) && p->dl.nr_cpus_allowed > 1)
+		enqueue_pushable_dl_task(rq, p);
 }
 
 static void task_tick_dl(struct rq *rq, struct task_struct *p, int queued)
@@ -576,43 +933,410 @@ static void set_curr_task_dl(struct rq *rq)
 	struct task_struct *p = rq->curr;
 
 	p->se.exec_start = rq->clock;
+
+	/* You can't push away the running task */
+	dequeue_pushable_dl_task(rq, p);
 }
 
-static void switched_from_dl(struct rq *rq, struct task_struct *p)
+#ifdef CONFIG_SMP
+
+/* Only try algorithms three times */
+#define DL_MAX_TRIES 3
+
+static int pick_dl_task(struct rq *rq, struct task_struct *p, int cpu)
 {
-	if (hrtimer_active(&p->dl.dl_timer))
-		hrtimer_try_to_cancel(&p->dl.dl_timer);
+	if (!task_running(rq, p) &&
+	    (cpu < 0 || cpumask_test_cpu(cpu, &p->cpus_allowed)) &&
+	    (p->dl.nr_cpus_allowed > 1))
+		return 1;
+
+	return 0;
 }
 
-static void switched_to_dl(struct rq *rq, struct task_struct *p)
+/* Returns the second earliest -deadline task, NULL otherwise */
+static struct task_struct *pick_next_earliest_dl_task(struct rq *rq, int cpu)
+{
+	struct rb_node *next_node = rq->dl.rb_leftmost;
+	struct sched_dl_entity *dl_se;
+	struct task_struct *p = NULL;
+
+next_node:
+	next_node = rb_next(next_node);
+	if (next_node) {
+		dl_se = rb_entry(next_node, struct sched_dl_entity, rb_node);
+		p = dl_task_of(dl_se);
+
+		if (pick_dl_task(rq, p, cpu))
+			return p;
+
+		goto next_node;
+	}
+
+	return NULL;
+}
+
+static int latest_cpu_find(struct cpumask *span,
+			   struct task_struct *task,
+			   struct cpumask *later_mask)
 {
+	const struct sched_dl_entity *dl_se = &task->dl;
+	int cpu, found = -1, best = 0;
+	u64 max_dl = 0;
+
+	for_each_cpu(cpu, span) {
+		struct rq *rq = cpu_rq(cpu);
+		struct dl_rq *dl_rq = &rq->dl;
+
+		if (cpumask_test_cpu(cpu, &task->cpus_allowed) &&
+		    (!dl_rq->dl_nr_running || dl_time_before(dl_se->deadline,
+		     dl_rq->earliest_dl.curr))) {
+			if (later_mask)
+				cpumask_set_cpu(cpu, later_mask);
+			if (!best && !dl_rq->dl_nr_running) {
+				best = 1;
+				found = cpu;
+			} else if (!best &&
+				   dl_time_before(max_dl,
+						  dl_rq->earliest_dl.curr)) {
+				max_dl = dl_rq->earliest_dl.curr;
+				found = cpu;
+			}
+		} else if (later_mask)
+			cpumask_clear_cpu(cpu, later_mask);
+	}
+
+	return found;
+}
+
+static DEFINE_PER_CPU(cpumask_var_t, local_cpu_mask_dl);
+
+static int find_later_rq(struct task_struct *task)
+{
+	struct sched_domain *sd;
+	struct cpumask *later_mask = __get_cpu_var(local_cpu_mask_dl);
+	int this_cpu = smp_processor_id();
+	int best_cpu, cpu = task_cpu(task);
+
+	if (task->dl.nr_cpus_allowed == 1)
+		return -1;
+
+	best_cpu = latest_cpu_find(task_rq(task)->rd->span, task, later_mask);
+	if (best_cpu == -1)
+		return -1;
+
 	/*
-	 * If p is throttled, don't consider the possibility
-	 * of preempting rq->curr, the check will be done right
-	 * after its runtime will get replenished.
+	 * If we are here, some target has been found,
+	 * the most suitable of which is cached in best_cpu.
+	 * This is, among the runqueues where the current tasks
+	 * have later deadlines than the task's one, the rq
+	 * with the latest possible one.
+	 *
+	 * Now we check how well this matches with task's
+	 * affinity and system topology.
+	 *
+	 * The last cpu where the task run is our first
+	 * guess, since it is most likely cache-hot there.
 	 */
-	if (unlikely(p->dl.dl_throttled))
-		return;
+	if (cpumask_test_cpu(cpu, later_mask))
+		return cpu;
+	/*
+	 * Check if this_cpu is to be skipped (i.e., it is
+	 * not in the mask) or not.
+	 */
+	if (!cpumask_test_cpu(this_cpu, later_mask))
+		this_cpu = -1;
+
+	rcu_read_lock();
+	for_each_domain(cpu, sd) {
+		if (sd->flags & SD_WAKE_AFFINE) {
+
+			/*
+			 * If possible, preempting this_cpu is
+			 * cheaper than migrating.
+			 */
+			if (this_cpu != -1 &&
+			    cpumask_test_cpu(this_cpu, sched_domain_span(sd)))
+				return this_cpu;
+
+			/*
+			 * Last chance: if best_cpu is valid and is
+			 * in the mask, that becomes our choice.
+			 */
+			if (best_cpu < nr_cpu_ids &&
+			    cpumask_test_cpu(best_cpu, sched_domain_span(sd)))
+				return best_cpu;
+		}
+	}
+	rcu_read_unlock();
 
-	if (!p->on_rq || rq->curr != p) {
-		if (task_has_dl_policy(rq->curr))
-			check_preempt_curr_dl(rq, p, 0);
-		else
-			resched_task(rq->curr);
+	/*
+	 * At this point, all our guesses failed, we just return
+	 * 'something', and let the caller sort the things out.
+	 */
+	if (this_cpu != -1)
+		return this_cpu;
+
+	cpu = cpumask_any(later_mask);
+	if (cpu < nr_cpu_ids)
+		return cpu;
+
+	return -1;
+}
+
+/* Locks the rq it finds */
+static struct rq *find_lock_later_rq(struct task_struct *task, struct rq *rq)
+{
+	struct rq *later_rq = NULL;
+	int tries;
+	int cpu;
+
+	for (tries = 0; tries < DL_MAX_TRIES; tries++) {
+		cpu = find_later_rq(task);
+
+		if ((cpu == -1) || (cpu == rq->cpu))
+			break;
+
+		later_rq = cpu_rq(cpu);
+
+		/* Retry if something changed. */
+		if (double_lock_balance(rq, later_rq)) {
+			if (unlikely(task_rq(task) != rq ||
+				     !cpumask_test_cpu(later_rq->cpu,
+						       &task->cpus_allowed) ||
+				     task_running(rq, task) ||
+				     !task->se.on_rq)) {
+				raw_spin_unlock(&later_rq->lock);
+				later_rq = NULL;
+				break;
+			}
+		}
+
+		/*
+		 * If the rq we found has no -deadline task, or
+		 * its earliest one has a later deadline than our
+		 * task, the rq is a good one.
+		 */
+		if (!later_rq->dl.dl_nr_running ||
+		    dl_time_before(task->dl.deadline,
+				   later_rq->dl.earliest_dl.curr))
+			break;
+
+		/* Otherwise we try again. */
+		double_unlock_balance(rq, later_rq);
+		later_rq = NULL;
 	}
+
+	return later_rq;
 }
 
-static void prio_changed_dl(struct rq *rq, struct task_struct *p,
-			    int oldprio)
+static struct task_struct *pick_next_pushable_dl_task(struct rq *rq)
 {
-	switched_to_dl(rq, p);
+	struct task_struct *p;
+
+	if (!has_pushable_dl_tasks(rq))
+		return NULL;
+
+	p = rb_entry(rq->dl.pushable_dl_tasks_leftmost,
+		     struct task_struct, pushable_dl_tasks);
+
+	BUG_ON(rq->cpu != task_cpu(p));
+	BUG_ON(task_current(rq, p));
+	BUG_ON(p->dl.nr_cpus_allowed <= 1);
+
+	BUG_ON(!p->se.on_rq);
+	BUG_ON(!dl_task(p));
+
+	return p;
 }
 
-#ifdef CONFIG_SMP
-static int
-select_task_rq_dl(struct task_struct *p, int sd_flag, int flags)
+/*
+ * See if the non running -deadline tasks on this rq
+ * can be sent to some other CPU where they can preempt
+ * and start executing.
+ */
+static int push_dl_task(struct rq *rq)
 {
-	return task_cpu(p);
+	struct task_struct *next_task;
+	struct rq *later_rq;
+
+	if (!rq->dl.overloaded)
+		return 0;
+
+	next_task = pick_next_pushable_dl_task(rq);
+	if (!next_task)
+		return 0;
+
+retry:
+	if (unlikely(next_task == rq->curr)) {
+		WARN_ON(1);
+		return 0;
+	}
+
+	/*
+	 * If next_task preempts rq->curr, and rq->curr
+	 * can move away, it makes sense to just reschedule
+	 * without going further in pushing next_task.
+	 */
+	if (dl_task(rq->curr) &&
+	    dl_time_before(next_task->dl.deadline, rq->curr->dl.deadline) &&
+	    rq->curr->dl.nr_cpus_allowed > 1) {
+		resched_task(rq->curr);
+		return 0;
+	}
+
+	/* We might release rq lock */
+	get_task_struct(next_task);
+
+	/* Will lock the rq it'll find */
+	later_rq = find_lock_later_rq(next_task, rq);
+	if (!later_rq) {
+		struct task_struct *task;
+
+		/*
+		 * We must check all this again, since
+		 * find_lock_later_rq releases rq->lock and it is
+		 * then possible that next_task has migrated.
+		 */
+		task = pick_next_pushable_dl_task(rq);
+		if (task_cpu(next_task) == rq->cpu && task == next_task) {
+			/*
+			 * The task is still there. We don't try
+			 * again, some other cpu will pull it when ready.
+			 */
+			dequeue_pushable_dl_task(rq, next_task);
+			goto out;
+		}
+
+		if (!task)
+			/* No more tasks */
+			goto out;
+
+		put_task_struct(next_task);
+		next_task = task;
+		goto retry;
+	}
+
+	deactivate_task(rq, next_task, 0);
+	set_task_cpu(next_task, later_rq->cpu);
+	activate_task(later_rq, next_task, 0);
+
+	resched_task(later_rq->curr);
+
+	double_unlock_balance(rq, later_rq);
+
+out:
+	put_task_struct(next_task);
+
+	return 1;
+}
+
+static void push_dl_tasks(struct rq *rq)
+{
+	/* Terminates as it moves a -deadline task */
+	while (push_dl_task(rq))
+		;
+}
+
+static int pull_dl_task(struct rq *this_rq)
+{
+	int this_cpu = this_rq->cpu, ret = 0, cpu;
+	struct task_struct *p;
+	struct rq *src_rq;
+	u64 dmin = LONG_MAX;
+
+	if (likely(!dl_overloaded(this_rq)))
+		return 0;
+
+	for_each_cpu(cpu, this_rq->rd->dlo_mask) {
+		if (this_cpu == cpu)
+			continue;
+
+		src_rq = cpu_rq(cpu);
+
+		/*
+		 * It looks racy, abd it is! However, as in sched_rt.c,
+		 * we are fine with this.
+		 */
+		if (this_rq->dl.dl_nr_running &&
+		    dl_time_before(this_rq->dl.earliest_dl.curr,
+				   src_rq->dl.earliest_dl.next))
+			continue;
+
+		/* Might drop this_rq->lock */
+		double_lock_balance(this_rq, src_rq);
+
+		/*
+		 * If there are no more pullable tasks on the
+		 * rq, we're done with it.
+		 */
+		if (src_rq->dl.dl_nr_running <= 1)
+			goto skip;
+
+		p = pick_next_earliest_dl_task(src_rq, this_cpu);
+
+		/*
+		 * We found a task to be pulled if:
+		 *  - it preempts our current (if there's one),
+		 *  - it will preempt the last one we pulled (if any).
+		 */
+		if (p && dl_time_before(p->dl.deadline, dmin) &&
+		    (!this_rq->dl.dl_nr_running ||
+		     dl_time_before(p->dl.deadline,
+				    this_rq->dl.earliest_dl.curr))) {
+			WARN_ON(p == src_rq->curr);
+			WARN_ON(!p->se.on_rq);
+
+			/*
+			 * Then we pull iff p has actually an earlier
+			 * deadline than the current task of its runqueue.
+			 */
+			if (dl_time_before(p->dl.deadline,
+					   src_rq->curr->dl.deadline))
+				goto skip;
+
+			ret = 1;
+
+			deactivate_task(src_rq, p, 0);
+			set_task_cpu(p, this_cpu);
+			activate_task(this_rq, p, 0);
+			dmin = p->dl.deadline;
+
+			/* Is there any other task even earlier? */
+		}
+skip:
+		double_unlock_balance(this_rq, src_rq);
+	}
+
+	return ret;
+}
+
+static void pre_schedule_dl(struct rq *rq, struct task_struct *prev)
+{
+	/* Try to pull other tasks here */
+	if (dl_task(prev))
+		pull_dl_task(rq);
+}
+
+static void post_schedule_dl(struct rq *rq)
+{
+	push_dl_tasks(rq);
+}
+
+/*
+ * Since the task is not running and a reschedule is not going to happen
+ * anytime soon on its runqueue, we try pushing it away now.
+ */
+static void task_woken_dl(struct rq *rq, struct task_struct *p)
+{
+	if (!task_running(rq, p) &&
+	    !test_tsk_need_resched(rq->curr) &&
+	    has_pushable_dl_tasks(rq) &&
+	    p->dl.nr_cpus_allowed > 1 &&
+	    dl_task(rq->curr) &&
+	    (rq->curr->dl.nr_cpus_allowed < 2 ||
+	     dl_entity_preempt(&rq->curr->dl, &p->dl))) {
+		push_dl_tasks(rq);
+	}
 }
 
 static void set_cpus_allowed_dl(struct task_struct *p,
@@ -622,10 +1346,145 @@ static void set_cpus_allowed_dl(struct task_struct *p,
 
 	BUG_ON(!dl_task(p));
 
+	/*
+	 * Update only if the task is actually running (i.e.,
+	 * it is on the rq AND it is not throttled).
+	 */
+	if (on_dl_rq(&p->dl) && (weight != p->dl.nr_cpus_allowed)) {
+		struct rq *rq = task_rq(p);
+
+		if (!task_current(rq, p)) {
+			/*
+			 * If the task was on the pushable list,
+			 * make sure it stays there only if the new
+			 * mask allows that.
+			 */
+			if (p->dl.nr_cpus_allowed > 1)
+				dequeue_pushable_dl_task(rq, p);
+
+			if (weight > 1)
+				enqueue_pushable_dl_task(rq, p);
+		}
+
+		if ((p->dl.nr_cpus_allowed <= 1) && (weight > 1)) {
+			rq->dl.dl_nr_migratory++;
+		} else if ((p->dl.nr_cpus_allowed > 1) && (weight <= 1)) {
+			BUG_ON(!rq->dl.dl_nr_migratory);
+			rq->dl.dl_nr_migratory--;
+		}
+
+		update_dl_migration(&rq->dl);
+	}
+
 	cpumask_copy(&p->cpus_allowed, new_mask);
 	p->dl.nr_cpus_allowed = weight;
 }
+
+/* Assumes rq->lock is held */
+static void rq_online_dl(struct rq *rq)
+{
+	if (rq->dl.overloaded)
+		dl_set_overload(rq);
+}
+
+/* Assumes rq->lock is held */
+static void rq_offline_dl(struct rq *rq)
+{
+	if (rq->dl.overloaded)
+		dl_clear_overload(rq);
+}
+
+static inline void init_sched_dl_class(void)
+{
+	unsigned int i;
+
+	for_each_possible_cpu(i)
+		zalloc_cpumask_var_node(&per_cpu(local_cpu_mask_dl, i),
+					GFP_KERNEL, cpu_to_node(i));
+}
+
+#endif /* CONFIG_SMP */
+
+static void switched_from_dl(struct rq *rq, struct task_struct *p)
+{
+	if (hrtimer_active(&p->dl.dl_timer) && !dl_policy(p->policy))
+		hrtimer_try_to_cancel(&p->dl.dl_timer);
+
+#ifdef CONFIG_SMP
+	/*
+	 * Since this might be the only -deadline task on the rq,
+	 * this is the right place to try to pull some other one
+	 * from an overloaded cpu, if any.
+	 */
+	if (!rq->dl.dl_nr_running)
+		pull_dl_task(rq);
 #endif
+}
+
+/*
+ * When switching to -deadline, we may overload the rq, then
+ * we try to push someone off, if possible.
+ */
+static void switched_to_dl(struct rq *rq, struct task_struct *p)
+{
+	int check_resched = 1;
+
+	/*
+	 * If p is throttled, don't consider the possibility
+	 * of preempting rq->curr, the check will be done right
+	 * after its runtime will get replenished.
+	 */
+	if (unlikely(p->dl.dl_throttled))
+		return;
+
+	if (!p->on_rq || rq->curr != p) {
+#ifdef CONFIG_SMP
+		if (rq->dl.overloaded && push_dl_task(rq) && rq != task_rq(p))
+			/* Only reschedule if pushing failed */
+			check_resched = 0;
+#endif /* CONFIG_SMP */
+		if (check_resched && task_has_dl_policy(rq->curr))
+			check_preempt_curr_dl(rq, p, 0);
+	}
+}
+
+/*
+ * If the scheduling parameters of a -deadline task changed,
+ * a push or pull operation might be needed.
+ */
+static void prio_changed_dl(struct rq *rq, struct task_struct *p,
+			    int oldprio)
+{
+	if (p->on_rq || rq->curr == p) {
+#ifdef CONFIG_SMP
+		/*
+		 * This might be too much, but unfortunately
+		 * we don't have the old deadline value, and
+		 * we can't argue if the task is increasing
+		 * or lowering its prio, so...
+		 */
+		if (!rq->dl.overloaded)
+			pull_dl_task(rq);
+
+		/*
+		 * If we now have a earlier deadline task than p,
+		 * then reschedule, provided p is still on this
+		 * runqueue.
+		 */
+		if (dl_time_before(rq->dl.earliest_dl.curr, p->dl.deadline) &&
+		    rq->curr == p)
+			resched_task(p);
+#else
+		/*
+		 * Again, we don't know if p has a earlier
+		 * or later deadline, so let's blindly set a
+		 * (maybe not needed) rescheduling point.
+		 */
+		resched_task(p);
+#endif /* CONFIG_SMP */
+	} else
+		switched_to_dl(rq, p);
+}
 
 static const struct sched_class dl_sched_class = {
 	.next			= &rt_sched_class,
@@ -642,6 +1501,11 @@ static const struct sched_class dl_sched_class = {
 	.select_task_rq		= select_task_rq_dl,
 
 	.set_cpus_allowed       = set_cpus_allowed_dl,
+	.rq_online              = rq_online_dl,
+	.rq_offline             = rq_offline_dl,
+	.pre_schedule		= pre_schedule_dl,
+	.post_schedule		= post_schedule_dl,
+	.task_woken		= task_woken_dl,
 #endif
 
 	.set_curr_task		= set_curr_task_dl,
kernel/sched_rt.c +1 -1
index 4b09704..7b609bc 100644
@@ -1590,7 +1590,7 @@ static void task_woken_rt(struct rq *rq, struct task_struct *p)
 	    !test_tsk_need_resched(rq->curr) &&
 	    has_pushable_tasks(rq) &&
 	    p->rt.nr_cpus_allowed > 1 &&
-	    rt_task(rq->curr) &&
+	    (dl_task(rq->curr) || rt_task(rq->curr)) &&
 	    (rq->curr->rt.nr_cpus_allowed < 2 ||
 	     rq->curr->prio <= p->prio))
 		push_rt_tasks(rq);
Signature · 1 line
1.7.5.4
author
#93 lore raw

PATCH 7/16 sched: SCHED_DEADLINE avg_update accounting.

In reply to Juri Lelli

From: Dario Faggioli <[email protected]>

Make the core scheduler and load balancer aware of the load produced by -deadline tasks, by updating the moving average like for sched_rt.

---
 kernel/sched_dl.c |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)
kernel/sched_dl.c +2 -0
index 38edefa..11c9b8d 100644
@@ -511,6 +511,8 @@ static void update_curr_dl(struct rq *rq)
 	curr->se.exec_start = rq->clock;
 	cpuacct_charge(curr, delta_exec);
 
+	sched_rt_avg_update(rq, delta_exec);
+
 	dl_se->runtime -= delta_exec;
 	if (dl_runtime_exceeded(rq, dl_se)) {
 		__dequeue_task_dl(rq, curr, 0);
Signature · 1 line
1.7.5.4
author
#101 lore raw

PATCH 10/16 sched: add resource limits for -deadline tasks.

In reply to Juri Lelli

From: Dario Faggioli <[email protected]>

Add resource limits for non-root tasks in using the SCHED_DEADLINE policy, very similarly to what already exists for RT policies.

In fact, this patch:
- adds the resource limit RLIMIT_DLDLINE, which is the minimum value a user task can use as its own deadline;
- adds the resource limit RLIMIT_DLRTIME, which is the maximum value a user task can use as it own runtime.

Notice that to exploit these, a modified version of the ulimit utility and a modified resource.h header file are needed. They both will be available on the website of the project.

---
 include/asm-generic/resource.h |    7 ++++++-
 kernel/sched.c                 |   25 +++++++++++++++++++++++++
 2 files changed, 31 insertions(+), 1 deletions(-)
include/asm-generic/resource.h +6 -1
index 61fa862..a682848 100644
@@ -45,7 +45,10 @@
 					   0-39 for nice level 19 .. -20 */
 #define RLIMIT_RTPRIO		14	/* maximum realtime priority */
 #define RLIMIT_RTTIME		15	/* timeout for RT tasks in us */
-#define RLIM_NLIMITS		16
+
+#define RLIMIT_DLDLINE		16	/* minimum deadline in us */
+#define RLIMIT_DLRTIME		17	/* maximum runtime in us */
+#define RLIM_NLIMITS		18
 
 /*
  * SuS says limits have to be unsigned.
@@ -87,6 +90,8 @@
 	[RLIMIT_NICE]		= { 0, 0 },				\
 	[RLIMIT_RTPRIO]		= { 0, 0 },				\
 	[RLIMIT_RTTIME]		= {  RLIM_INFINITY,  RLIM_INFINITY },	\
+	[RLIMIT_DLDLINE]	= { ULONG_MAX, ULONG_MAX },		\
+	[RLIMIT_DLRTIME]	= { 0, 0 },				\
 }
 
 #endif	/* __KERNEL__ */
kernel/sched.c +25 -0
index ea4787a..92d5e26 100644
@@ -5593,6 +5593,31 @@ recheck:
 	 * Allow unprivileged RT tasks to decrease priority:
 	 */
 	if (user && !capable(CAP_SYS_NICE)) {
+		if (dl_policy(policy)) {
+			u64 rlim_dline, rlim_rtime;
+			u64 dline, rtime;
+
+			if (!lock_task_sighand(p, &flags))
+				return -ESRCH;
+			rlim_dline = p->signal->rlim[RLIMIT_DLDLINE].rlim_cur;
+			rlim_rtime = p->signal->rlim[RLIMIT_DLRTIME].rlim_cur;
+			unlock_task_sighand(p, &flags);
+
+			/* can't set/change -deadline policy */
+			if (policy != p->policy && !rlim_rtime)
+				return -EPERM;
+
+			/* can't decrease the deadline */
+			rlim_dline *= NSEC_PER_USEC;
+			dline = param->sched_deadline;
+			if (dline < p->dl.dl_deadline && dline < rlim_dline)
+				return -EPERM;
+			/* can't increase the runtime */
+			rlim_rtime *= NSEC_PER_USEC;
+			rtime = param->sched_runtime;
+			if (rtime > p->dl.dl_runtime && rtime > rlim_rtime)
+				return -EPERM;
+		}
 		if (rt_policy(policy)) {
 			unsigned long rlim_rtprio =
 					task_rlimit(p, RLIMIT_RTPRIO);
Signature · 1 line
1.7.5.4
author
#106 lore raw

PATCH 11/16 sched: add latency tracing for -deadline tasks.

In reply to Juri Lelli

From: Dario Faggioli <[email protected]>

It is very likely that systems that wants/needs to use the new SCHED_DEADLINE policy also want to have the scheduling latency of the -deadline tasks under control.

For this reason a new version of the scheduling wakeup latency, called "wakeup_dl", is introduced.

As a consequence of applying this patch there will be three wakeup latency tracer:
* "wakeup", that deals with all tasks in the system;
* "wakeup_rt", that deals with -rt and -deadline tasks only;
* "wakeup_dl", that deals with -deadline tasks only.

---
 kernel/trace/trace_sched_wakeup.c |   41 ++++++++++++++++++++++++++++++++++++-
 kernel/trace/trace_selftest.c     |   30 ++++++++++++++++----------
 2 files changed, 58 insertions(+), 13 deletions(-)
kernel/trace/trace_sched_wakeup.c +40 -1
index e4a70c0..9c9b1be 100644
@@ -27,6 +27,7 @@ static int			wakeup_cpu;
 static int			wakeup_current_cpu;
 static unsigned			wakeup_prio = -1;
 static int			wakeup_rt;
+static int			wakeup_dl;
 
 static arch_spinlock_t wakeup_lock =
 	(arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
@@ -420,6 +421,17 @@ probe_wakeup(void *ignore, struct task_struct *p, int success)
 	if ((wakeup_rt && !rt_task(p)) ||
 			p->prio >= wakeup_prio ||
 			p->prio >= current->prio)
+	/*
+	 * Semantic is like this:
+	 *  - wakeup tracer handles all tasks in the system, independently
+	 *    from their scheduling class;
+	 *  - wakeup_rt tracer handles tasks belonging to sched_dl and
+	 *    sched_rt class;
+	 *  - wakeup_dl handles tasks belonging to sched_dl class only.
+	 */
+	if ((wakeup_dl && !dl_task(p)) ||
+	    (wakeup_rt && !dl_task(p) && !rt_task(p)) ||
+	    (p->prio >= wakeup_prio || p->prio >= current->prio))
 		return;
 
 	pc = preempt_count();
@@ -431,7 +443,7 @@ probe_wakeup(void *ignore, struct task_struct *p, int success)
 	arch_spin_lock(&wakeup_lock);
 
 	/* check for races. */
-	if (!tracer_enabled || p->prio >= wakeup_prio)
+	if (!tracer_enabled || (!dl_task(p) && p->prio >= wakeup_prio))
 		goto out_locked;
 
 	/* reset the trace */
@@ -539,16 +551,25 @@ static int __wakeup_tracer_init(struct trace_array *tr)
 
 static int wakeup_tracer_init(struct trace_array *tr)
 {
+	wakeup_dl = 0;
 	wakeup_rt = 0;
 	return __wakeup_tracer_init(tr);
 }
 
 static int wakeup_rt_tracer_init(struct trace_array *tr)
 {
+	wakeup_dl = 0;
 	wakeup_rt = 1;
 	return __wakeup_tracer_init(tr);
 }
 
+static int wakeup_dl_tracer_init(struct trace_array *tr)
+{
+	wakeup_dl = 1;
+	wakeup_rt = 0;
+	return __wakeup_tracer_init(tr);
+}
+
 static void wakeup_tracer_reset(struct trace_array *tr)
 {
 	stop_wakeup_tracer(tr);
@@ -611,6 +632,20 @@ static struct tracer wakeup_rt_tracer __read_mostly =
 	.use_max_tr	= 1,
 };
 
+static struct tracer wakeup_dl_tracer __read_mostly =
+{
+	.name		= "wakeup_dl",
+	.init		= wakeup_dl_tracer_init,
+	.reset		= wakeup_tracer_reset,
+	.start		= wakeup_tracer_start,
+	.stop		= wakeup_tracer_stop,
+	.wait_pipe	= poll_wait_pipe,
+	.print_max	= 1,
+#ifdef CONFIG_FTRACE_SELFTEST
+	.selftest    = trace_selftest_startup_wakeup,
+#endif
+};
+
 __init static int init_wakeup_tracer(void)
 {
 	int ret;
@@ -623,6 +658,10 @@ __init static int init_wakeup_tracer(void)
 	if (ret)
 		return ret;
 
+	ret = register_tracer(&wakeup_dl_tracer);
+	if (ret)
+		return ret;
+
 	return 0;
 }
 device_initcall(init_wakeup_tracer);
kernel/trace/trace_selftest.c +18 -12
index 288541f..849063e 100644
@@ -765,11 +765,17 @@ trace_selftest_startup_nop(struct tracer *trace, struct trace_array *tr)
 #ifdef CONFIG_SCHED_TRACER
 static int trace_wakeup_test_thread(void *data)
 {
-	/* Make this a RT thread, doesn't need to be too high */
-	static const struct sched_param param = { .sched_priority = 5 };
+	/* Make this a -deadline thread */
+	struct sched_param2 paramx = {
+		.sched_priority = 0,
+		.sched_runtime = 100000ULL,
+		.sched_deadline = 10000000ULL,
+		.sched_period = 10000000ULL
+		.sched_flags = 0
+	};
 	struct completion *x = data;
 
-	sched_setscheduler(current, SCHED_FIFO, &param);
+	sched_setscheduler2(current, SCHED_DEADLINE, &paramx);
 
 	/* Make it know we have a new prio */
 	complete(x);
@@ -781,8 +787,8 @@ static int trace_wakeup_test_thread(void *data)
 	/* we are awake, now wait to disappear */
 	while (!kthread_should_stop()) {
 		/*
-		 * This is an RT task, do short sleeps to let
-		 * others run.
+		 * This will likely be the system top priority
+		 * task, do short sleeps to let others run.
 		 */
 		msleep(100);
 	}
@@ -795,21 +801,21 @@ trace_selftest_startup_wakeup(struct tracer *trace, struct trace_array *tr)
 {
 	unsigned long save_max = tracing_max_latency;
 	struct task_struct *p;
-	struct completion isrt;
+	struct completion is_ready;
 	unsigned long count;
 	int ret;
 
-	init_completion(&isrt);
+	init_completion(&is_ready);
 
-	/* create a high prio thread */
-	p = kthread_run(trace_wakeup_test_thread, &isrt, "ftrace-test");
+	/* create a -deadline thread */
+	p = kthread_run(trace_wakeup_test_thread, &is_ready, "ftrace-test");
 	if (IS_ERR(p)) {
 		printk(KERN_CONT "Failed to create ftrace wakeup test thread ");
 		return -1;
 	}
 
-	/* make sure the thread is running at an RT prio */
-	wait_for_completion(&isrt);
+	/* make sure the thread is running at -deadline policy */
+	wait_for_completion(&is_ready);
 
 	/* start the tracing */
 	ret = tracer_init(trace, tr);
@@ -821,7 +827,7 @@ trace_selftest_startup_wakeup(struct tracer *trace, struct trace_array *tr)
 	/* reset the max latency */
 	tracing_max_latency = 0;
 
-	/* sleep to let the RT thread sleep too */
+	/* sleep to let the thread sleep too */
 	msleep(100);
 
 	/*
Signature · 1 line
1.7.5.4
author
#117 lore raw

PATCH 13/16 sched: drafted deadline inheritance logic.

In reply to Juri Lelli

From: Dario Faggioli <[email protected]>

Some method to deal with rt-mutexes and make sched_dl interact with the current PI-coded is needed, raising all but trivial issues, that needs (according to us) to be solved with some restructuring of the pi-code (i.e., going toward a proxy execution-ish implementation).

This is under development, in the meanwhile, as a temporary solution, what this commits does is:
- ensure a pi-lock owner with waiters is never throttled down. Instead, when it runs out of runtime, it immediately gets replenished and it's deadline is postponed;
- the scheduling parameters (relative deadline and default runtime) used for that replenishments --during the whole period it holds the pi-lock-- are the ones of the waiting task with earliest deadline.

Acting this way, we provide some kind of boosting to the lock-owner, still by using the existing (actually, slightly modified by the previous commit) pi-architecture.

We would stress the fact that this is only a surely needed, all but clean solution to the problem. In the end it's only a way to re-start discussion within the community. So, as always, comments, ideas, rants, etc.. are welcome! :-)

---
 include/linux/sched.h |    9 +++++-
 kernel/fork.c         |    1 +
 kernel/rtmutex.c      |   13 +++++++-
 kernel/sched.c        |   34 ++++++++++++++++++---
 kernel/sched_dl.c     |   77 +++++++++++++++++++++++++++++++++---------------
 5 files changed, 102 insertions(+), 32 deletions(-)
include/linux/sched.h +8 -1
index 5ef7bb6..ca45db4 100644
@@ -1309,8 +1309,12 @@ struct sched_dl_entity {
 	 * @dl_new tells if a new instance arrived. If so we must
 	 * start executing it with full runtime and reset its absolute
 	 * deadline;
+	 *
+	 * @dl_boosted tells if we are boosted due to DI. If so we are
+	 * outside bandwidth enforcement mechanism (but only until we
+	 * exit the critical section).
 	 */
-	int dl_throttled, dl_new;
+	int dl_throttled, dl_new, dl_boosted;
 
 	/*
 	 * Bandwidth enforcement timer. Each -deadline task has its
@@ -1556,6 +1560,8 @@ struct task_struct {
 	struct rb_node *pi_waiters_leftmost;
 	/* Deadlock detection and priority inheritance handling */
 	struct rt_mutex_waiter *pi_blocked_on;
+	/* Top pi_waiters task */
+	struct task_struct *pi_top_task;
 #endif
 
 #ifdef CONFIG_DEBUG_MUTEXES
@@ -2236,6 +2242,7 @@ extern unsigned int sysctl_sched_cfs_bandwidth_slice;
 #ifdef CONFIG_RT_MUTEXES
 extern int rt_mutex_getprio(struct task_struct *p);
 extern void rt_mutex_setprio(struct task_struct *p, int prio);
+extern struct task_struct *rt_mutex_get_top_task(struct task_struct *task);
 extern void rt_mutex_adjust_pi(struct task_struct *p);
 static inline bool tsk_is_pi_blocked(struct task_struct *tsk)
 {
kernel/fork.c +1 -0
index 344e0b4..b8b0dff 100644
@@ -1036,6 +1036,7 @@ static void rt_mutex_init_task(struct task_struct *p)
 	p->pi_waiters = RB_ROOT;
 	p->pi_waiters_leftmost = NULL;
 	p->pi_blocked_on = NULL;
+	p->pi_top_task = NULL;
 #endif
 }
 
kernel/rtmutex.c +11 -2
index 6234c0e..cf155ba 100644
@@ -217,6 +217,14 @@ int rt_mutex_getprio(struct task_struct *task)
 		   task->normal_prio);
 }
 
+struct task_struct *rt_mutex_get_top_task(struct task_struct *task)
+{
+	if (likely(!task_has_pi_waiters(task)))
+		return NULL;
+
+	return task_top_pi_waiter(task)->task;
+}
+
 /*
  * Adjust the priority of a task, after its pi_waiters got modified.
  *
@@ -226,7 +234,7 @@ static void __rt_mutex_adjust_prio(struct task_struct *task)
 {
 	int prio = rt_mutex_getprio(task);
 
-	if (task->prio != prio)
+	if (task->prio != prio || dl_prio(prio))
 		rt_mutex_setprio(task, prio);
 }
 
@@ -712,7 +720,8 @@ void rt_mutex_adjust_pi(struct task_struct *task)
 
 	waiter = task->pi_blocked_on;
 	if (!rt_mutex_real_waiter(waiter) ||
-	    waiter->task->prio == task->prio) {
+	    (waiter->task->prio == task->prio &&
+	    !dl_prio(task->prio))) {
 		raw_spin_unlock_irqrestore(&task->pi_lock, flags);
 		return;
 	}
kernel/sched.c +29 -5
index 00e69a0..3963e4e 100644
@@ -5238,7 +5238,7 @@ EXPORT_SYMBOL(sleep_on_timeout);
  */
 void rt_mutex_setprio(struct task_struct *p, int prio)
 {
-	int oldprio, on_rq, running;
+	int oldprio, on_rq, running, enqueue_flag = 0;
 	struct rq *rq;
 	const struct sched_class *prev_class;
 
@@ -5265,6 +5265,7 @@ void rt_mutex_setprio(struct task_struct *p, int prio)
 	}
 
 	trace_sched_pi_setprio(p, prio);
+	p->pi_top_task = rt_mutex_get_top_task(p);
 	oldprio = p->prio;
 	prev_class = p->sched_class;
 	on_rq = p->on_rq;
@@ -5274,19 +5275,42 @@ void rt_mutex_setprio(struct task_struct *p, int prio)
 	if (running)
 		p->sched_class->put_prev_task(rq, p);
 
-	if (dl_prio(prio))
+	/*
+	 * Boosting condition are:
+	 * 1. -rt task is running and holds mutex A
+	 *      --> -dl task blocks on mutex A
+	 *
+	 * 2. -dl task is running and holds mutex A
+	 *      --> -dl task blocks on mutex A and could preempt the
+	 *          running task
+	 */
+	if (dl_prio(prio)) {
+		if (!dl_prio(p->normal_prio) || (p->pi_top_task &&
+			dl_entity_preempt(&p->pi_top_task->dl, &p->dl))) {
+			p->dl.dl_boosted = 1;
+			p->dl.dl_throttled = 0;
+			enqueue_flag = ENQUEUE_REPLENISH;
+		} else
+			p->dl.dl_boosted = 0;
 		p->sched_class = &dl_sched_class;
-	else if (rt_prio(prio))
+	} else if (rt_prio(prio)) {
+		if (dl_prio(oldprio))
+			p->dl.dl_boosted = 0;
+		if (oldprio < prio)
+			enqueue_flag = ENQUEUE_HEAD;
 		p->sched_class = &rt_sched_class;
-	else
+	} else {
+		if (dl_prio(oldprio))
+			p->dl.dl_boosted = 0;
 		p->sched_class = &fair_sched_class;
+	}
 
 	p->prio = prio;
 
 	if (running)
 		p->sched_class->set_curr_task(rq);
 	if (on_rq)
-		enqueue_task(rq, p, oldprio < prio ? ENQUEUE_HEAD : 0);
+		enqueue_task(rq, p, enqueue_flag);
 
 	check_class_changed(rq, p, prev_class, oldprio);
 out_unlock:
kernel/sched_dl.c +53 -24

188 lines not on this page · raw message

Signature · 1 line
1.7.5.4
author
#122 lore raw

PATCH 15/16 sched: speed up -dl pushes with a push-heap.

In reply to Juri Lelli

Data from tests confirmed that the original active load balancing logic didn't scale neither in the number of CPU nor in the number of tasks (as sched_rt does).

Here we provide a global data structure to keep track of deadlines of the running tasks in the system. The structure is composed by a bitmask showing the free CPUs and a max-heap, needed when the system is heavily loaded.

The implementation and concurrent access scheme are kept simple by design. However, our measurements show that we can compete with sched_rt on large multi-CPUs machines [1].

Only the push path is addressed, the extension to use this structure also for pull decisions is straightforward. However, we are currently evaluating different (in order to decrease/avoid contention) data structures to solve possibly both problems. We are also going to re-run tests considering recent changes inside cpupri [2].

[1] http://retis.sssup.it/~jlelli/papers/Ospert11Lelli.pdf
[2] http://www.spinics.net/lists/linux-rt-users/msg06778.html

---
 kernel/Makefile      |    1 +
 kernel/sched.c       |    5 +
 kernel/sched_cpudl.c |  208 ++++++++++++++++++++++++++++++++++++++++++++++++++
 kernel/sched_cpudl.h |   34 ++++++++
 kernel/sched_dl.c    |   52 +++---------
 5 files changed, 261 insertions(+), 39 deletions(-)
 create mode 100644 kernel/sched_cpudl.c
 create mode 100644 kernel/sched_cpudl.h
kernel/Makefile +1 -0
index c961d3a..0aca455 100644
@@ -105,6 +105,7 @@ obj-$(CONFIG_X86_DS) += trace/
 obj-$(CONFIG_RING_BUFFER) += trace/
 obj-$(CONFIG_TRACEPOINTS) += trace/
 obj-$(CONFIG_SMP) += sched_cpupri.o
+obj-$(CONFIG_SMP) += sched_cpudl.o
 obj-$(CONFIG_IRQ_WORK) += irq_work.o
 obj-$(CONFIG_CPU_PM) += cpu_pm.o
 
kernel/sched.c +5 -0
index d5403c8..f8a857a 100644
@@ -81,6 +81,7 @@
 #endif
 
 #include "sched_cpupri.h"
+#include "sched_cpudl.h"
 #include "workqueue_sched.h"
 #include "sched_autogroup.h"
 
@@ -707,6 +708,7 @@ struct root_domain {
 	cpumask_var_t dlo_mask;
 	atomic_t dlo_count;
 	struct dl_bw dl_bw;
+	struct cpudl cpudl;
 
 	/*
 	 * The "RT overload" flag: it gets set if a CPU has more than
@@ -7683,6 +7685,7 @@ static void free_rootdomain(struct rcu_head *rcu)
 	struct root_domain *rd = container_of(rcu, struct root_domain, rcu);
 
 	cpupri_cleanup(&rd->cpupri);
+	cpudl_cleanup(&rd->cpudl);
 	free_cpumask_var(rd->dlo_mask);
 	free_cpumask_var(rd->rto_mask);
 	free_cpumask_var(rd->online);
@@ -7741,6 +7744,8 @@ static int init_rootdomain(struct root_domain *rd)
 		goto free_dlo_mask;
 
 	init_dl_bw(&rd->dl_bw);
+	if (cpudl_init(&rd->cpudl) != 0)
+		goto free_dlo_mask;
 
 	if (cpupri_init(&rd->cpupri) != 0)
 		goto free_rto_mask;
kernel/sched_cpudl.c new file +208 -0

208 lines not on this page · raw message

kernel/sched_cpudl.h new file +34 -0
new file mode 100644
index 0000000..9285dc4
@@ -0,0 +1,34 @@
+#ifndef _LINUX_CPUDL_H
+#define _LINUX_CPUDL_H
+
+#include <linux/sched.h>
+
+#define IDX_INVALID     -1
+
+struct array_item {
+	u64 dl;
+	int cpu;
+};
+
+struct cpudl {
+	raw_spinlock_t lock;
+	int size;
+	int cpu_to_idx[NR_CPUS];
+	struct array_item elements[NR_CPUS];
+	cpumask_var_t free_cpus;
+};
+
+
+#ifdef CONFIG_SMP
+int cpudl_find(struct cpudl *cp, struct cpumask *dlo_mask,
+		struct task_struct *p, struct cpumask *later_mask);
+void cpudl_set(struct cpudl *cp, int cpu, u64 dl, int is_valid);
+int cpudl_init(struct cpudl *cp);
+void cpudl_cleanup(struct cpudl *cp);
+#else
+#define cpudl_set(cp, cpu, dl) do { } while (0)
+#define cpudl_init() do { } while (0)
+#endif /* CONFIG_SMP */
+
+#endif /* _LINUX_CPUDL_H */
+
kernel/sched_dl.c +13 -39

112 lines not on this page · raw message

Signature · 1 line
1.7.5.4
author
#123 lore raw

PATCH 16/16 sched: add sched_dl documentation.

In reply to Juri Lelli

From: Dario Faggioli <[email protected]>

Add in Documentation/scheduler/ some hints about the design choices, the usage and the future possible developments of the sched_dl scheduling class and of the SCHED_DEADLINE policy.

---
 Documentation/scheduler/sched-deadline.txt |  147 ++++++++++++++++++++++++++++
 1 files changed, 147 insertions(+), 0 deletions(-)
 create mode 100644 Documentation/scheduler/sched-deadline.txt
Documentation/scheduler/sched-deadline.txt new file +147 -0
new file mode 100644
index 0000000..6c94194
@@ -0,0 +1,147 @@
+			Deadline Task and Group Scheduling
+			----------------------------------
+
+CONTENTS
+========
+
+0. WARNING
+1. Overview
+2. Task scheduling
+2. The Interface
+3. Bandwidth management
+  3.1 System-wide settings
+  2.2 Task interface
+  2.4 Default behavior
+3. Future plans
+
+
+0. WARNING
+==========
+
+ Fiddling with these settings can result in an unpredictable or even unstable
+ system behavior. As for -rt (group) scheduling, it is assumed that root users
+ know what they're doing.
+
+
+1. Overview
+===========
+
+ The SCHED_DEADLINE policy contained inside the sched_dl scheduling class is
+ basically an implementation of the Earliest Deadline First (EDF) scheduling
+ algorithm, augmented with a mechanism (called Constant Bandwidth Server, CBS)
+ that makes it possible to isolate the behavior of tasks between each other.
+
+
+2. Task scheduling
+==================
+
+ The typical -deadline task will be made up of a computation phase (instance)
+ which is activated on a periodic or sporadic fashion. The expected (maximum)
+ duration of such computation is called the task's runtime; the time interval
+ by which each instance needs to be completed is called the task's relative
+ deadline. The task's absolute deadline is dynamically calculated as the
+ time instant a task (better, an instance) activates plus the relative
+ deadline.
+
+ The EDF algorithm selects the task with the smallest absolute deadline as
+ the one to be executed first, while the CBS ensures that each task runs for
+ at most its runtime every period, avoiding any interference between different
+ tasks (bandwidth isolation).
+ Thanks to this feature, also tasks that do not strictly comply with the
+ computational model described above can effectively use the new policy.
+ IOW, there are no limitations on what kind of task can exploit this new
+ scheduling discipline, even if it must be said that it is particularly
+ suited for periodic or sporadic tasks that need guarantees on their
+ timing behavior, e.g., multimedia, streaming, control applications, etc.
+
+
+3. Bandwidth management
+=======================
+
+ In order for the -deadline scheduling to be effective and useful, it is
+ important to have some method to keep the allocation of the available CPU
+ bandwidth to the tasks under control.
+ This is usually called "admission control" and if it is not performed at all,
+ no guarantee can be given on the actual scheduling of the -deadline tasks.
+
+ Since when RT-throttling has been introduced each task group has a bandwidth
+ associated, calculated as a certain amount of runtime over a period.
+ Moreover, to make it possible to manipulate such bandwidth, readable/writable
+ controls have been added to both procfs (for system wide settings) and cgroupfs
+ (for per-group settings).
+ Therefore, the same interface is being used for controlling the bandwidth
+ distrubution to -deadline tasks and task groups, i.e., new controls but with
+ similar names, equivalent meaning and with the same usage paradigm are added.
+
+ However, more discussion is needed in order to figure out how we want to manage
+ SCHED_DEADLINE bandwidth at the task group level. Therefore, SCHED_DEADLINE
+ uses (for now) a less sophisticated, but actually very sensible, mechanism to
+ ensure that a certain utilization cap is not overcome per each root_domain.
+
+ Another main difference between deadline bandwidth management and RT-throttling
+ is that -deadline tasks have bandwidth on their own (while -rt ones don't!),
+ and thus we don't need an higher level throttling mechanism to enforce the
+ desired bandwidth.
+
+3.1 System wide settings
+------------------------
+
+The system wide settings are configured under the /proc virtual file system:
+
+ The per-group controls that are added to the cgroupfs virtual file system are:
+  * /proc/sys/kernel/sched_dl_runtime_us,
+  * /proc/sys/kernel/sched_dl_period_us,
+
+ They accept (if written) and provides (if read) the new runtime and period,
+ respectively, for each CPU in each root_domain.
+
+ This means that, for a root_domain comprising M CPUs, -deadline tasks
+ can be created until the sum of their bandwidths stay below:
+
+   M * (sched_dl_runtime_us / sched_dl_period_us)
+
+ It is also possible to disable this bandwidth management logic, and
+ be thus free of oversubscribing the system up to any arbitrary level.
+ This is done by writing -1 in /proc/sys/kernel/sched_dl_runtime_us.
+
+
+2.2 Task interface
+------------------
+
+ Specifying a periodic/sporadic task that executes for a given amount of
+ runtime at each instance, and that is scheduled according to the urgency of
+ its own timing constraints needs, in general, a way of declaring:
+  - a (maximum/typical) instance execution time,
+  - a minimum interval between consecutive instances,
+  - a time constraint by which each instance must be completed.
+
+ Therefore:
+  * a new struct sched_param2, containing all the necessary fields is
+    provided;
+  * the new scheduling related syscalls that manipulate it, i.e.,
+    sched_setscheduler2(), sched_setparam2() and sched_getparam2()
+    are implemented.
+
+
+2.4 Default behavior
+---------------------
+
+The default values for SCHED_DEADLINE bandwidth is to have dl_runtime and
+dl_period equal to 500000 and 1000000, respectively. This means -deadline
+tasks can use at most 5%, multiplied by the number of CPUs that compose the
+root_domain, for each root_domain.
+
+When a -deadline task fork a child, its dl_runtime is set to 0, which means
+someone must call sched_setscheduler2() on it, or it won't even start.
+
+
+3. Future plans
+===============
+
+Still Missing:
+
+ - refinements to deadline inheritance, especially regarding the possibility
+   of retaining bandwidth isolation among non-interacting tasks. This is
+   being studied from both theoretical and practical point of views, and
+   hopefully we can have some demonstrative code soon.
+
Signature · 1 line
1.7.5.4
author
#121 lore raw

PATCH 14/16 sched: add bandwidth management for sched_dl.

In reply to Juri Lelli

From: Dario Faggioli <[email protected]>

In order of -deadline scheduling to be effective and useful, it is important that some method of having the allocation of the available CPU bandwidth to tasks and task groups under control.
This is usually called "admission control" and if it is not performed at all, no guarantee can be given on the actual scheduling of the -deadline tasks.

Since when RT-throttling has been introduced each task group have a bandwidth associated to itself, calculated as a certain amount of runtime over a period. Moreover, to make it possible to manipulate such bandwidth, readable/writable controls have been added to both procfs (for system wide settings) and cgroupfs (for per-group settings).
Therefore, the same interface is being used for controlling the bandwidth distrubution to -deadline tasks and task groups, i.e., new controls but with similar names, equivalent meaning and with the same usage paradigm are added.

However, more discussion is needed in order to figure out how we want to manage SCHED_DEADLINE bandwidth at the task group level. Therefore, this patch adds a less sophisticated, but actually very sensible, mechanism to ensure that a certain utilization cap is not overcome per each root_domain (the single rq for !SMP configurations).

Another main difference between deadline bandwidth management and RT-throttling is that -deadline tasks have bandwidth on their own (while -rt ones doesn't!), and thus we don't need an higher level throttling mechanism to enforce the desired bandwidth.

This patch, therefore:
- adds system wide deadline bandwidth management by means of:
* /proc/sys/kernel/sched_dl_runtime_us,
* /proc/sys/kernel/sched_dl_period_us,
that determine (i.e., runtime / period) the total bandwidth available on each CPU of each root_domain for -deadline tasks;
- couples the RT and deadline bandwidth management, i.e., enforces that the sum of how much bandwidth is being devoted to -rt -deadline tasks to stay below 100%.

This means that, for a root_domain comprising M CPUs, -deadline tasks can be created until the sum of their bandwidths stay below:

M * (sched_dl_runtime_us / sched_dl_period_us)

It is also possible to disable this bandwidth management logic, and be thus free of oversubscribing the system up to any arbitrary level.

---
 include/linux/sched.h |    8 +
 kernel/sched.c        |  497 ++++++++++++++++++++++++++++++++++++++++++++++---
 kernel/sched_dl.c     |   18 ++-
 kernel/sysctl.c       |   14 ++
 4 files changed, 512 insertions(+), 25 deletions(-)
include/linux/sched.h +8 -0
index ca45db4..534f099 100644
@@ -1289,6 +1289,7 @@ struct sched_dl_entity {
 	u64 dl_runtime;		/* maximum runtime for each instance	*/
 	u64 dl_deadline;	/* relative deadline of each instance	*/
 	u64 dl_period;		/* separation of two instances (period) */
+	u64 dl_bw;		/* dl_runtime / dl_deadline		*/
 
 	/*
 	 * Actual scheduling parameters. Initialized with the values above,
@@ -2217,6 +2218,13 @@ int sched_rt_handler(struct ctl_table *table, int write,
 		void __user *buffer, size_t *lenp,
 		loff_t *ppos);
 
+extern unsigned int sysctl_sched_dl_period;
+extern int sysctl_sched_dl_runtime;
+
+int sched_dl_handler(struct ctl_table *table, int write,
+		void __user *buffer, size_t *lenp,
+		loff_t *ppos);
+
 #ifdef CONFIG_SCHED_AUTOGROUP
 extern unsigned int sysctl_sched_autogroup_enabled;
 
kernel/sched.c +475 -22

644 lines not on this page · raw message

kernel/sched_dl.c +15 -3
index e393292..fee9434 100644
@@ -991,6 +991,18 @@ static void task_fork_dl(struct task_struct *p)
 static void task_dead_dl(struct task_struct *p)
 {
 	struct hrtimer *timer = &p->dl.dl_timer;
+#ifdef CONFIG_SMP
+	struct dl_bw *dl_b = &task_rq(p)->rd->dl_bw;
+#else
+	struct dl_bw *dl_b = &task_rq(p)->dl.dl_bw;
+#endif
+
+	/*
+	 * Since we are TASK_DEAD we won't slip out of the domain!
+	 */
+	raw_spin_lock_irq(&dl_b->lock);
+	dl_b->total_bw -= p->dl.dl_bw;
+	raw_spin_unlock_irq(&dl_b->lock);
 
 	hrtimer_try_to_cancel(timer);
 }
@@ -1171,7 +1183,7 @@ static struct rq *find_lock_later_rq(struct task_struct *task, struct rq *rq)
 				     !cpumask_test_cpu(later_rq->cpu,
 						       &task->cpus_allowed) ||
 				     task_running(rq, task) ||
-				     !task->se.on_rq)) {
+				     !task->on_rq)) {
 				raw_spin_unlock(&later_rq->lock);
 				later_rq = NULL;
 				break;
@@ -1210,7 +1222,7 @@ static struct task_struct *pick_next_pushable_dl_task(struct rq *rq)
 	BUG_ON(task_current(rq, p));
 	BUG_ON(p->dl.nr_cpus_allowed <= 1);
 
-	BUG_ON(!p->se.on_rq);
+	BUG_ON(!p->on_rq);
 	BUG_ON(!dl_task(p));
 
 	return p;
@@ -1351,7 +1363,7 @@ static int pull_dl_task(struct rq *this_rq)
 		     dl_time_before(p->dl.deadline,
 				    this_rq->dl.earliest_dl.curr))) {
 			WARN_ON(p == src_rq->curr);
-			WARN_ON(!p->se.on_rq);
+			WARN_ON(!p->on_rq);
 
 			/*
 			 * Then we pull iff p has actually an earlier
kernel/sysctl.c +14 -0
index ae27196..9ab6c9d 100644
@@ -362,6 +362,20 @@ static struct ctl_table kern_table[] = {
 		.mode		= 0644,
 		.proc_handler	= sched_rt_handler,
 	},
+	{
+		.procname	= "sched_dl_period_us",
+		.data		= &sysctl_sched_dl_period,
+		.maxlen		= sizeof(unsigned int),
+		.mode		= 0644,
+		.proc_handler	= sched_dl_handler,
+	},
+	{
+		.procname	= "sched_dl_runtime_us",
+		.data		= &sysctl_sched_dl_runtime,
+		.maxlen		= sizeof(int),
+		.mode		= 0644,
+		.proc_handler	= sched_dl_handler,
+	},
 #ifdef CONFIG_SCHED_AUTOGROUP
 	{
 		.procname	= "sched_autogroup_enabled",
Signature · 1 line
1.7.5.4
author
#112 lore raw

PATCH 12/16 rtmutex: turn the plist into an rb-tree.

In reply to Juri Lelli

From: Peter Zijlstra <[email protected]>

Turn the pi-chains from plist to rb-tree, in the rt_mutex code, and provide a proper comparison function for -deadline and -priority tasks.

This is done mainly because:
- classical prio field of the plist is just an int, which might not be enough for representing a deadline;
- manipulating such a list would become O(nr_deadline_tasks), which might be to much, as the number of -deadline task increases.

Therefore, an rb-tree is used, and tasks are queued in it according to the following logic:
- among two -priority (i.e., SCHED_BATCH/OTHER/RR/FIFO) tasks, the one with the higher (lower, actually!) prio wins;
- among a -priority and a -deadline task, the latter always wins;
- among two -deadline tasks, the one with the earliest deadline wins.

Queueing and dequeueing functions are changed accordingly, for both the list of a task's pi-waiters and the list of tasks blocked on a pi-lock.

---
 include/linux/init_task.h |   10 +++
 include/linux/rtmutex.h   |   18 ++----
 include/linux/sched.h     |    4 +-
 kernel/fork.c             |    3 +-
 kernel/rtmutex-debug.c    |   10 +--
 kernel/rtmutex.c          |  151 +++++++++++++++++++++++++++++++++++----------
 kernel/rtmutex_common.h   |   24 ++++---
 kernel/sched.c            |    4 -
 8 files changed, 157 insertions(+), 67 deletions(-)
include/linux/init_task.h +10 -0
index cfd9f8d..7a2da88 100644
@@ -10,6 +10,7 @@
 #include <linux/pid_namespace.h>
 #include <linux/user_namespace.h>
 #include <linux/securebits.h>
+#include <linux/rbtree.h>
 #include <net/net_namespace.h>
 
 #ifdef CONFIG_SMP
@@ -134,6 +135,14 @@ extern struct cred init_cred;
 
 #define INIT_TASK_COMM "swapper"
 
+#ifdef CONFIG_RT_MUTEXES
+# define INIT_RT_MUTEXES						\
+	.pi_waiters = RB_ROOT,						\
+	.pi_waiters_leftmost = NULL,
+#else
+# define INIT_RT_MUTEXES
+#endif
+
 /*
  *  INIT_TASK is used to set up the first task table, touch at
  * your own risk!. Base=0, limit=0x1fffff (=2MB)
@@ -200,6 +209,7 @@ extern struct cred init_cred;
 	INIT_FTRACE_GRAPH						\
 	INIT_TRACE_RECURSION						\
 	INIT_TASK_RCU_PREEMPT(tsk)					\
+	INIT_RT_MUTEXES							\
 }
 
 
include/linux/rtmutex.h +6 -12
index 5ebd0bb..2344d71 100644
@@ -13,7 +13,7 @@
 #define __LINUX_RT_MUTEX_H
 
 #include <linux/linkage.h>
-#include <linux/plist.h>
+#include <linux/rbtree.h>
 #include <linux/spinlock_types_raw.h>
 
 extern int max_lock_depth; /* for sysctl */
@@ -22,12 +22,14 @@ extern int max_lock_depth; /* for sysctl */
  * The rt_mutex structure
  *
  * @wait_lock:	spinlock to protect the structure
- * @wait_list:	pilist head to enqueue waiters in priority order
+ * @waiters:	rbtree root to enqueue waiters in priority order
+ * @waiters_leftmost: top waiter
  * @owner:	the mutex owner
  */
 struct rt_mutex {
 	raw_spinlock_t		wait_lock;
-	struct plist_head	wait_list;
+	struct rb_root          waiters;
+	struct rb_node          *waiters_leftmost;
 	struct task_struct	*owner;
 	int			save_state;
 #ifdef CONFIG_DEBUG_RT_MUTEXES
@@ -79,7 +81,7 @@ struct hrtimer_sleeper;
 
 #define __RT_MUTEX_INITIALIZER_PLAIN(mutexname) \
 	.wait_lock = __RAW_SPIN_LOCK_UNLOCKED(mutexname.wait_lock) \
-	, .wait_list = PLIST_HEAD_INIT(mutexname.wait_list) \
+	, .waiters = RB_ROOT \
 	, .owner = NULL \
 	__DEBUG_RT_MUTEX_INITIALIZER(mutexname)
 
@@ -120,12 +122,4 @@ extern int rt_mutex_trylock(struct rt_mutex *lock);
 
 extern void rt_mutex_unlock(struct rt_mutex *lock);
 
-#ifdef CONFIG_RT_MUTEXES
-# define INIT_RT_MUTEXES(tsk)						\
-	.pi_waiters	= PLIST_HEAD_INIT(tsk.pi_waiters),	\
-	INIT_RT_MUTEX_DEBUG(tsk)
-#else
-# define INIT_RT_MUTEXES(tsk)
-#endif
-
 #endif
include/linux/sched.h +3 -1
index 23cca57..5ef7bb6 100644
@@ -57,6 +57,7 @@ struct sched_param {
 #include <linux/types.h>
 #include <linux/timex.h>
 #include <linux/jiffies.h>
+#include <linux/plist.h>
 #include <linux/rbtree.h>
 #include <linux/thread_info.h>
 #include <linux/cpumask.h>
@@ -1551,7 +1552,8 @@ struct task_struct {
 
 #ifdef CONFIG_RT_MUTEXES
 	/* PI waiters blocked on a rt_mutex held by this task */
-	struct plist_head pi_waiters;
+	struct rb_root pi_waiters;
+	struct rb_node *pi_waiters_leftmost;
 	/* Deadlock detection and priority inheritance handling */
 	struct rt_mutex_waiter *pi_blocked_on;
 #endif
kernel/fork.c +2 -1
index b263c69..344e0b4 100644
@@ -1033,7 +1033,8 @@ static void rt_mutex_init_task(struct task_struct *p)
 {
 	raw_spin_lock_init(&p->pi_lock);
 #ifdef CONFIG_RT_MUTEXES
-	plist_head_init(&p->pi_waiters);
+	p->pi_waiters = RB_ROOT;
+	p->pi_waiters_leftmost = NULL;
 	p->pi_blocked_on = NULL;
 #endif
 }
kernel/rtmutex-debug.c +4 -6
index 8eafd1b..e80670d 100644
@@ -23,7 +23,7 @@
 #include <linux/kallsyms.h>
 #include <linux/syscalls.h>
 #include <linux/interrupt.h>
-#include <linux/plist.h>
+#include <linux/rbtree.h>
 #include <linux/fs.h>
 #include <linux/debug_locks.h>
 
@@ -56,7 +56,7 @@ static void printk_lock(struct rt_mutex *lock, int print_owner)
 
 void rt_mutex_debug_task_free(struct task_struct *task)
 {
-	DEBUG_LOCKS_WARN_ON(!plist_head_empty(&task->pi_waiters));
+	DEBUG_LOCKS_WARN_ON(!RB_EMPTY_ROOT(&task->pi_waiters));
 	DEBUG_LOCKS_WARN_ON(task->pi_blocked_on);
 }
 
@@ -152,16 +152,14 @@ void debug_rt_mutex_proxy_unlock(struct rt_mutex *lock)
 void debug_rt_mutex_init_waiter(struct rt_mutex_waiter *waiter)
 {
 	memset(waiter, 0x11, sizeof(*waiter));
-	plist_node_init(&waiter->list_entry, MAX_PRIO);
-	plist_node_init(&waiter->pi_list_entry, MAX_PRIO);
+	RB_CLEAR_NODE(&waiter->pi_tree_entry);
+	RB_CLEAR_NODE(&waiter->tree_entry);
 	waiter->deadlock_task_pid = NULL;
 }
 
 void debug_rt_mutex_free_waiter(struct rt_mutex_waiter *waiter)
 {
 	put_pid(waiter->deadlock_task_pid);
-	DEBUG_LOCKS_WARN_ON(!plist_node_empty(&waiter->list_entry));
-	DEBUG_LOCKS_WARN_ON(!plist_node_empty(&waiter->pi_list_entry));
 	memset(waiter, 0x22, sizeof(*waiter));
 }
 
kernel/rtmutex.c +119 -32

267 lines not on this page · raw message

kernel/rtmutex_common.h +13 -11
index 6ec3dc1..aa2be42 100644
@@ -40,13 +40,13 @@ extern void schedule_rt_mutex_test(struct rt_mutex *lock);
  * This is the control structure for tasks blocked on a rt_mutex,
  * which is allocated on the kernel stack on of the blocked task.
  *
- * @list_entry:		pi node to enqueue into the mutex waiters list
- * @pi_list_entry:	pi node to enqueue into the mutex owner waiters list
+ * @tree_entry:		pi node to enqueue into the mutex waiters tree
+ * @pi_tree_entry:	pi node to enqueue into the mutex owner waiters tree
  * @task:		task reference to the blocked task
  */
 struct rt_mutex_waiter {
-	struct plist_node	list_entry;
-	struct plist_node	pi_list_entry;
+	struct rb_node          tree_entry;
+	struct rb_node          pi_tree_entry;
 	struct task_struct	*task;
 	struct rt_mutex		*lock;
 	bool			savestate;
@@ -58,11 +58,11 @@ struct rt_mutex_waiter {
 };
 
 /*
- * Various helpers to access the waiters-plist:
+ * Various helpers to access the waiters-tree:
  */
 static inline int rt_mutex_has_waiters(struct rt_mutex *lock)
 {
-	return !plist_head_empty(&lock->wait_list);
+	return !RB_EMPTY_ROOT(&lock->waiters);
 }
 
 static inline struct rt_mutex_waiter *
@@ -70,8 +70,8 @@ rt_mutex_top_waiter(struct rt_mutex *lock)
 {
 	struct rt_mutex_waiter *w;
 
-	w = plist_first_entry(&lock->wait_list, struct rt_mutex_waiter,
-			       list_entry);
+	w = rb_entry(lock->waiters_leftmost, struct rt_mutex_waiter,
+		     tree_entry);
 	BUG_ON(w->lock != lock);
 
 	return w;
@@ -79,14 +79,14 @@ rt_mutex_top_waiter(struct rt_mutex *lock)
 
 static inline int task_has_pi_waiters(struct task_struct *p)
 {
-	return !plist_head_empty(&p->pi_waiters);
+	return !RB_EMPTY_ROOT(&p->pi_waiters);
 }
 
 static inline struct rt_mutex_waiter *
 task_top_pi_waiter(struct task_struct *p)
 {
-	return plist_first_entry(&p->pi_waiters, struct rt_mutex_waiter,
-				  pi_list_entry);
+	return rb_entry(p->pi_waiters_leftmost, struct rt_mutex_waiter,
+			pi_tree_entry);
 }
 
 /*
@@ -133,6 +133,8 @@ rt_mutex_init_waiter(struct rt_mutex_waiter *waiter, bool savestate)
 	debug_rt_mutex_init_waiter(waiter);
 	waiter->task = NULL;
 	waiter->savestate = savestate;
+	rb_init_node(&waiter->tree_entry);
+	rb_init_node(&waiter->pi_tree_entry);
 }
 
 #endif
kernel/sched.c +0 -4
index 92d5e26..00e69a0 100644
@@ -8824,10 +8824,6 @@ void __init sched_init(void)
 	open_softirq(SCHED_SOFTIRQ, run_rebalance_domains);
 #endif
 
-#ifdef CONFIG_RT_MUTEXES
-	plist_head_init(&init_task.pi_waiters);
-#endif
-
 	/*
 	 * The boot idle thread does lazy MMU switching as well:
 	 */
Signature · 1 line
1.7.5.4
author
#100 lore raw

PATCH 9/16 sched: add schedstats for -deadline tasks.

In reply to Juri Lelli

From: Dario Faggioli <[email protected]>

Add some typical sched-debug output to dl_rq(s) and some schedstats to -deadline tasks.

---
 include/linux/sched.h |   13 +++++++++++++
 kernel/sched.c        |    2 ++
 kernel/sched_debug.c  |   43 +++++++++++++++++++++++++++++++++++++++++++
 kernel/sched_dl.c     |   45 +++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 103 insertions(+), 0 deletions(-)
include/linux/sched.h +13 -0
index 5961592..23cca57 100644
@@ -1267,6 +1267,15 @@ struct sched_rt_entity {
 #endif
 };
 
+#ifdef CONFIG_SCHEDSTATS
+struct sched_stats_dl {
+	u64			last_dmiss;
+	u64			last_rorun;
+	u64			dmiss_max;
+	u64			rorun_max;
+};
+#endif
+
 struct sched_dl_entity {
 	struct rb_node	rb_node;
 	int nr_cpus_allowed;
@@ -1307,6 +1316,10 @@ struct sched_dl_entity {
 	 * own bandwidth to be enforced, thus we need one timer per task.
 	 */
 	struct hrtimer dl_timer;
+
+#ifdef CONFIG_SCHEDSTATS
+	struct sched_stats_dl stats;
+#endif
 };
 
 struct rcu_node;
kernel/sched.c +2 -0
index 9461958..ea4787a 100644
@@ -569,6 +569,8 @@ struct dl_rq {
 
 	unsigned long dl_nr_running;
 
+	u64 exec_clock;
+
 #ifdef CONFIG_SMP
 	/*
 	 * Deadline values of the currently executing and the
kernel/sched_debug.c +43 -0

61 lines not on this page · raw message

kernel/sched_dl.c +45 -0

66 lines not on this page · raw message

Signature · 1 line
1.7.5.4
author
#94 lore raw

PATCH 8/16 sched: add period support for -deadline tasks.

In reply to Juri Lelli

From: Harald Gustafsson <[email protected]>

Make it possible to specify a period (different or equal than deadline) for -deadline tasks.

---
 include/linux/sched.h |    1 +
 kernel/sched.c        |   15 ++++++++++++---
 kernel/sched_dl.c     |   10 +++++++---
 3 files changed, 20 insertions(+), 6 deletions(-)
include/linux/sched.h +1 -0
index 416ce99..5961592 100644
@@ -1278,6 +1278,7 @@ struct sched_dl_entity {
 	 */
 	u64 dl_runtime;		/* maximum runtime for each instance	*/
 	u64 dl_deadline;	/* relative deadline of each instance	*/
+	u64 dl_period;		/* separation of two instances (period) */
 
 	/*
 	 * Actual scheduling parameters. Initialized with the values above,
kernel/sched.c +12 -3
index 1a38ad1..9461958 100644
@@ -3024,6 +3024,7 @@ static void __sched_fork(struct task_struct *p)
 	hrtimer_init(&p->dl.dl_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
 	p->dl.dl_runtime = p->dl.runtime = 0;
 	p->dl.dl_deadline = p->dl.deadline = 0;
+	p->dl.dl_period = 0;
 	p->dl.flags = 0;
 
 	INIT_LIST_HEAD(&p->rt.run_list);
@@ -5490,6 +5491,10 @@ __setparam_dl(struct task_struct *p, const struct sched_param2 *param2)
 	init_dl_task_timer(dl_se);
 	dl_se->dl_runtime = param2->sched_runtime;
 	dl_se->dl_deadline = param2->sched_deadline;
+	if (param2->sched_period != 0)
+		dl_se->dl_period = param2->sched_period;
+	else
+		dl_se->dl_period = dl_se->dl_deadline;
 	dl_se->flags = param2->sched_flags;
 	dl_se->dl_throttled = 0;
 	dl_se->dl_new = 1;
@@ -5503,19 +5508,23 @@ __getparam_dl(struct task_struct *p, struct sched_param2 *param2)
 	param2->sched_priority = p->rt_priority;
 	param2->sched_runtime = dl_se->dl_runtime;
 	param2->sched_deadline = dl_se->dl_deadline;
+	param2->sched_period = dl_se->dl_period;
 	param2->sched_flags = dl_se->flags;
 }
 
 /*
  * This function validates the new parameters of a -deadline task.
  * We ask for the deadline not being zero, and greater or equal
- * than the runtime.
+ * than the runtime, as well as the period of being zero or
+ * greater than deadline.
  */
 static bool
 __checkparam_dl(const struct sched_param2 *prm)
 {
-	return prm && (&prm->sched_deadline) != 0 &&
-	       (s64)(&prm->sched_deadline - &prm->sched_runtime) >= 0;
+	return prm && prm->sched_deadline != 0 &&
+	       (prm->sched_period == 0 ||
+		(s64)(prm->sched_period - prm->sched_deadline) >= 0) &&
+	       (s64)(prm->sched_deadline - prm->sched_runtime) >= 0;
 }
 
 /*
kernel/sched_dl.c +7 -3
index 11c9b8d..8682ee2 100644
@@ -266,7 +266,7 @@ static void replenish_dl_entity(struct sched_dl_entity *dl_se)
 	 * arbitrary large.
 	 */
 	while (dl_se->runtime <= 0) {
-		dl_se->deadline += dl_se->dl_deadline;
+		dl_se->deadline += dl_se->dl_period;
 		dl_se->runtime += dl_se->dl_runtime;
 	}
 
@@ -293,7 +293,11 @@ static void replenish_dl_entity(struct sched_dl_entity *dl_se)
  * assigned (function returns true if it can).
  *
  * For this to hold, we must check if:
- *   runtime / (deadline - t) < dl_runtime / dl_deadline .
+ *   runtime / (deadline - t) < dl_runtime / dl_period .
+ *
+ * Notice that the bandwidth check is done against the period. For
+ * task with deadline equal to period this is the same of using
+ * dl_deadline instead of dl_period in the equation above.
  */
 static bool dl_entity_overflow(struct sched_dl_entity *dl_se, u64 t)
 {
@@ -312,7 +316,7 @@ static bool dl_entity_overflow(struct sched_dl_entity *dl_se, u64 t)
 	 * to the (absolute) deadline. Therefore, overflowing the u64
 	 * type is very unlikely to occur in both cases.
 	 */
-	left = dl_se->dl_deadline * dl_se->runtime;
+	left = dl_se->dl_period * dl_se->runtime;
 	right = (dl_se->deadline - t) * dl_se->dl_runtime;
 
 	return dl_time_before(right, left);
Signature · 1 line
1.7.5.4
author
#21 lore raw

PATCH 5/16 sched: SCHED_DEADLINE policy implementation.

In reply to Juri Lelli

From: Dario Faggioli <[email protected]>

Add a scheduling class, in sched_dl.c and a new policy called SCHED_DEADLINE. It is an implementation of the Earliest Deadline First (EDF) scheduling algorithm, augmented with a mechanism (called Constant Bandwidth Server, CBS) that makes it possible to isolate the behaviour of tasks between each other.

The typical -deadline task will be made up of a computation phase (instance) which is activated on a periodic or sporadic fashion. The expected (maximum) duration of such computation is called the task's runtime; the time interval by which each instance need to be completed is called the task's relative deadline. The task's absolute deadline is dynamically calculated as the time instant a task (better, an instance) activates plus the relative deadline.

The EDF algorithms selects the task with the smallest absolute deadline as the one to be executed first, while the CBS ensures each task to run for at most its runtime every (relative) deadline length time interval, avoiding any interference between different tasks (bandwidth isolation).
Thanks to this feature, also tasks that do not strictly comply with the computational model sketched above can effectively use the new policy.

This patch:
- implements the core logic of the scheduling algorithm in the new scheduling class file;
- provides all the glue code between the new scheduling class and the core scheduler and refines the interactions between sched_dl and the other existing scheduling classes.

---
 include/linux/sched.h   |    2 +-
 kernel/fork.c           |    4 +-
 kernel/sched.c          |   67 +++++-
 kernel/sched_dl.c       |  655 +++++++++++++++++++++++++++++++++++++++++++++++
 kernel/sched_rt.c       |    1 +
 kernel/sched_stoptask.c |    2 +-
 6 files changed, 719 insertions(+), 12 deletions(-)
 create mode 100644 kernel/sched_dl.c
include/linux/sched.h +1 -1
index 6eb72b6..416ce99 100644
@@ -2323,7 +2323,7 @@ extern void wake_up_new_task(struct task_struct *tsk);
 #else
  static inline void kick_process(struct task_struct *tsk) { }
 #endif
-extern void sched_fork(struct task_struct *p);
+extern int sched_fork(struct task_struct *p);
 extern void sched_dead(struct task_struct *p);
 
 extern void proc_caches_init(void);
kernel/fork.c +3 -1
index e3db0cb..b263c69 100644
@@ -1241,7 +1241,9 @@ static struct task_struct *copy_process(unsigned long clone_flags,
 #endif
 
 	/* Perform scheduler related setup. Assign this task to a CPU. */
-	sched_fork(p);
+	retval = sched_fork(p);
+	if (retval)
+		goto bad_fork_cleanup_policy;
 
 	retval = perf_event_init_task(p);
 	if (retval)
kernel/sched.c +58 -9

141 lines not on this page · raw message

kernel/sched_dl.c new file +655 -0

655 lines not on this page · raw message

kernel/sched_rt.c +1 -0
index c108b9c..4b09704 100644
@@ -2,6 +2,7 @@
  * Real-Time Scheduling Class (mapped to the SCHED_FIFO and SCHED_RR
  * policies)
  */
+static const struct sched_class rt_sched_class;
 
 #ifdef CONFIG_RT_GROUP_SCHED
 
kernel/sched_stoptask.c +1 -1
index 8b44e7f..4270a36 100644
@@ -81,7 +81,7 @@ get_rr_interval_stop(struct rq *rq, struct task_struct *task)
  * Simple, special scheduling class for the per-CPU stop tasks:
  */
 static const struct sched_class stop_sched_class = {
-	.next			= &rt_sched_class,
+	.next			= &dl_sched_class,
 
 	.enqueue_task		= enqueue_task_stop,
 	.dequeue_task		= dequeue_task_stop,
Signature · 1 line
1.7.5.4
author
#20 lore raw

PATCH 4/16 sched: SCHED_DEADLINE SMP-related data structures.

In reply to Juri Lelli

Introduce data structures relevant for implementing dynamic migration of -deadline tasks.

Mainly, this is the logic for checking if runqueues are overloaded with -deadline tasks and for choosing where a task should migrate, when it is the case.

---
 include/linux/sched.h |    1 +
 kernel/sched.c        |   50 ++++++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 50 insertions(+), 1 deletions(-)
include/linux/sched.h +1 -0
index a7a4276..6eb72b6 100644
@@ -1384,6 +1384,7 @@ struct task_struct {
 	struct list_head tasks;
 #ifdef CONFIG_SMP
 	struct plist_node pushable_tasks;
+	struct rb_node pushable_dl_tasks;
 #endif
 
 	struct mm_struct *mm, *active_mm;
kernel/sched.c +49 -1

99 lines not on this page · raw message

Signature · 1 line
1.7.5.4
author
#1 lore raw

PATCH 1/16 sched: add sched_class->task_dead.

In reply to Juri Lelli

From: Dario Faggioli <[email protected]>

Add a new function to the scheduling class interface. It is called at the end of a context switch, if the prev task is in TASK_DEAD state.

It might be useful for the scheduling classes that want to be notified when one of their task dies, e.g. to perform some cleanup actions.

---
 include/linux/sched.h |    1 +
 kernel/sched.c        |    3 +++
 2 files changed, 4 insertions(+), 0 deletions(-)
include/linux/sched.h +1 -0
index 1f6b11a..ddfc4dc 100644
@@ -1116,6 +1116,7 @@ struct sched_class {
 	void (*set_curr_task) (struct rq *rq);
 	void (*task_tick) (struct rq *rq, struct task_struct *p, int queued);
 	void (*task_fork) (struct task_struct *p);
+	void (*task_dead) (struct task_struct *p);
 
 	void (*switched_from) (struct rq *this_rq, struct task_struct *task);
 	void (*switched_to) (struct rq *this_rq, struct task_struct *task);
kernel/sched.c +3 -0
index 1cc706d..4c8d1c3 100644
@@ -3219,6 +3219,9 @@ static void finish_task_switch(struct rq *rq, struct task_struct *prev)
 	if (mm)
 		mmdrop_delayed(mm);
 	if (unlikely(prev_state == TASK_DEAD)) {
+		if (prev->sched_class->task_dead)
+			prev->sched_class->task_dead(prev);
+
 		/*
 		 * Remove function-return probe instances associated with this
 		 * task and put them back on the free list.
Signature · 1 line
1.7.5.4
#124 lore raw
In reply to Juri Lelli

Hi all,

Quoted 3 lines · Juri Lelli

Hello everyone,

This is the take 4 for the SCHED_DEADLINE patchset.

[...]

Quoted 9 lines

Still missing/incomplete:
- (c)group based bandwidth management, and maybe scheduling. It seems some more discussion on what precisely we want is *really* needed for this point;
- bandwidth inheritance (to replace deadline/priority inheritance). What's in the patchset is just very few more than a simple placeholder. More discussion on the right way to go is needed here. Some work has already been done, but it is still not ready for submission.

About BWI... A student of mine (added in cc) implemented a prototypal bandwidth inheritance (based on an old version of SCHED_DEADLINE). It is here:
https://github.com/eus/cbs_inheritance
(Tadeus, please correct me if I pointed to the wrong repository).

It is not for inclusion yet (it is based on an old version, it is UP only, and it probably needs some cleanups), but it worked fine in our tests. Note that in this patch the BWI mechanism is not bound to rtmutexes, but inheritance is controlled through 2 syscalls (because we used BWI for client/server interactions).
Anyway, I hope that the BWI code developed by Tadeus can be useful (or can be directly re-used) for implementing BWI in SCHED_DEADLINE.

BTW, happy Easter to everyone!

Luca

#126 lore raw
In reply to Juri Lelli
Quoted 2 lines · Juri Lelli

Hello everyone,

Hi from here too,

Quoted 2 lines

This is the take 4 for the SCHED_DEADLINE patchset.

Yeah, we did it, at last! :-P

Actually, Juri did --entirely, as I'm now busy with a completely different kind of stuff! I therefore really what to thank him for having over the project, applying the review comments, updating it to latest branches and finally, posting it to see if you guys are still interested in it.

Quoted 2 lines

As usual, any kind of feedback is welcome and appreciated.

Yep, let us know what you think. :-)

Thanks and Regards,
Dario

Signature · 4 lines
<<This happens because I choose it to happen!>> (Raistlin Majere)
-----------------------------------------------------------------
Dario Faggioli, Ph.D, http://retis.sssup.it/people/faggioli
Senior Software Engineer, Citrix Systems R&D Ltd., Cambridge (UK)
#64 lore raw

Re: [PATCH 06/16] sched: SCHED_DEADLINE push and pull logic

In reply to Juri Lelli

Hello

Quoted 117 lines · Juri Lelli

Add dynamic migrations to SCHED_DEADLINE, so that tasks can be moved among CPUs when necessary. It is also possible to bind a task to a (set of) CPU(s), thus restricting its capability of migrating, or forbidding migrations at all.

The very same approach used in sched_rt is utilised:
 - -deadline tasks are kept into CPU-specific runqueues,
 - -deadline tasks are migrated among runqueues to achieve the following:
   * on an M-CPU system the M earliest deadline ready tasks are always running;
   * affinity/cpusets settings of all the -deadline tasks is always respected.

Therefore, this very special form of "load balancing" is done with an active method, i.e., the scheduler pushes or pulls tasks between runqueues when they are woken up and/or (de)scheduled.
IOW, every time a preemption occurs, the descheduled task might be sent to some other CPU (depending on its deadline) to continue executing (push). On the other hand, every time a CPU becomes idle, it might pull the second earliest deadline ready task from some other CPU.

To enforce this, a pull operation is always attempted before taking any scheduling decision (pre_schedule()), as well as a push one after each scheduling decision (post_schedule()). In addition, when a task arrives or wakes up, the best CPU where to resume it is selected taking into account its affinity mask, the system topology, but also its deadline. E.g., from the scheduling point of view, the best CPU where to wake up (and also where to push) a task is the one which is running the task with the latest deadline among the M executing ones.

In order to facilitate these decisions, per-runqueue "caching" of the deadlines of the currently running and of the first ready task is used. Queued but not running tasks are also parked in another rb-tree to speed-up pushes.

---
 kernel/sched_dl.c |  912 +++++++++++++++++++++++++++++++++++++++++++++++++++-- kernel/sched_rt.c |    2 +-

 2 files changed, 889 insertions(+), 25 deletions(-)
kernel/sched_dl.c +52 -0
index 604e2bc..38edefa 100644
@@ -10,6 +10,7 @@
  * miss some of their deadlines), and won't affect any other task.
  *
  * Copyright (C) 2010 Dario Faggioli <[email protected]>,
+ *                    Juri Lelli <[email protected]>,
  *                    Michael Trimarchi <[email protected]>,
  *                    Fabio Checconi <[email protected]>
  */
@@ -20,6 +21,15 @@ static inline int dl_time_before(u64 a, u64 b)
        return (s64)(a - b) < 0;
  }
 
+/*
+ * Tells if entity @a should preempt entity @b.
+ */
+static inline
+int dl_entity_preempt(struct sched_dl_entity *a, struct sched_dl_entity *b)
+{
+       return dl_time_before(a->deadline, b->deadline);
+}
+
  static inline struct task_struct *dl_task_of(struct sched_dl_entity *dl_se)
  {
        return container_of(dl_se, struct task_struct, dl);
@@ -50,6 +60,153 @@ static inline int is_leftmost(struct task_struct *p, struct dl_rq *dl_rq)
        return dl_rq->rb_leftmost == &dl_se->rb_node;
  }
 
+#ifdef CONFIG_SMP
+
+static inline int dl_overloaded(struct rq *rq)
+{
+       return atomic_read(&rq->rd->dlo_count);
+}
+
+static inline void dl_set_overload(struct rq *rq)
+{
+       if (!rq->online)
+               return;
+
+       cpumask_set_cpu(rq->cpu, rq->rd->dlo_mask);
+       /*
+        * Must be visible before the overload count is
+        * set (as in sched_rt.c).
+        */
+       wmb();
+       atomic_inc(&rq->rd->dlo_count);
+}
+
+static inline void dl_clear_overload(struct rq *rq)
+{
+       if (!rq->online)
+               return;
+
+       atomic_dec(&rq->rd->dlo_count);
+       cpumask_clear_cpu(rq->cpu, rq->rd->dlo_mask);
+}
+
+static void update_dl_migration(struct dl_rq *dl_rq)
+{
+       if (dl_rq->dl_nr_migratory && dl_rq->dl_nr_total > 1) {
+               if (!dl_rq->overloaded) {
+                       dl_set_overload(rq_of_dl_rq(dl_rq));
+                       dl_rq->overloaded = 1;
+               }
+       } else if (dl_rq->overloaded) {
+               dl_clear_overload(rq_of_dl_rq(dl_rq));
+               dl_rq->overloaded = 0;
+       }
+}

kernel/sched_dl.c static inline int is_leftmost(struct task_struct *p, struct dl_rq *dl_rq) · line 114

Quoted 10 lines
kernel/sched_dl.c +10 -0
+
+static void inc_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
+{
+       dl_rq = &rq_of_dl_rq(dl_rq)->dl;
+
+       dl_rq->dl_nr_total++;
+       if (dl_se->nr_cpus_allowed > 1)
+               dl_rq->dl_nr_migratory++;
+
+       update_dl_migration(dl_rq);
	if (dl_se->nr_cpus_allowed > 1) {
		dl_rq->dl_nr_migratory++;
		/* No change in migratory, no update of migration */
		update_dl_migration(dl_rq);
	}

kernel/sched_dl.c static inline int is_leftmost(struct task_struct *p, struct dl_rq *dl_rq) · line 125

Quoted 11 lines
kernel/sched_dl.c +11 -0
+}
+
+static void dec_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
+{
+       dl_rq = &rq_of_dl_rq(dl_rq)->dl;
+
+       dl_rq->dl_nr_total--;
+       if (dl_se->nr_cpus_allowed > 1)
+               dl_rq->dl_nr_migratory--;
+
+       update_dl_migration(dl_rq);

ditto

Quoted 11 lines
kernel/sched_dl.c +11 -0
+}
+
+/*
+ * The list of pushable -deadline task is not a plist, like in
+ * sched_rt.c, it is an rb-tree with tasks ordered by deadline.
+ */
+static void enqueue_pushable_dl_task(struct rq *rq, struct task_struct *p)
+{
+       struct dl_rq *dl_rq = &rq->dl;
+       struct rb_node **link = &dl_rq->pushable_dl_tasks_root.rb_node;
+       struct rb_node *parent = NULL;

kernel/sched_dl.c static inline int is_leftmost(struct task_struct *p, struct dl_rq *dl_rq) · line 146

Quoted 10 lines
kernel/sched_dl.c +10 -0
+       struct task_struct *entry;
+       int leftmost = 1;
+
+       BUG_ON(!RB_EMPTY_NODE(&p->pushable_dl_tasks));
+
+       while (*link) {
+               parent = *link;
+               entry = rb_entry(parent, struct task_struct,
+                                pushable_dl_tasks);
+               if (!dl_entity_preempt(&entry->dl, &p->dl))
		if (dl_entity_preempt(&p->dl, &entry->dl))
Quoted 16 lines
kernel/sched_dl.c +16 -0
+                       link = &parent->rb_left;
+               else {
+                       link = &parent->rb_right;
+                       leftmost = 0;
+               }
+       }
+
+       if (leftmost)
+               dl_rq->pushable_dl_tasks_leftmost = &p->pushable_dl_tasks;
+
+       rb_link_node(&p->pushable_dl_tasks, parent, link);
+       rb_insert_color(&p->pushable_dl_tasks, &dl_rq->pushable_dl_tasks_root);
+}
+
+static void dequeue_pushable_dl_task(struct rq *rq, struct task_struct *p)
+{

kernel/sched_dl.c static inline int is_leftmost(struct task_struct *p, struct dl_rq *dl_rq) · line 172

Quoted 10 lines
kernel/sched_dl.c +10 -0
+       struct dl_rq *dl_rq = &rq->dl;
+
+       if (RB_EMPTY_NODE(&p->pushable_dl_tasks))
+               return;
+
+       if (dl_rq->pushable_dl_tasks_leftmost == &p->pushable_dl_tasks) {
+               struct rb_node *next_node;
+
+               next_node = rb_next(&p->pushable_dl_tasks);
+               dl_rq->pushable_dl_tasks_leftmost = next_node;
		dl_rq->pushable_dl_tasks_leftmost =
					rb_next(&p->pushable_dl_tasks);

kernel/sched_dl.c static inline int is_leftmost(struct task_struct *p, struct dl_rq *dl_rq) · line 181

Quoted 9 lines
kernel/sched_dl.c +9 -0
+       }
+
+       rb_erase(&p->pushable_dl_tasks, &dl_rq->pushable_dl_tasks_root);
+       RB_CLEAR_NODE(&p->pushable_dl_tasks);
+}
+
+static inline int has_pushable_dl_tasks(struct rq *rq)
+{
+       return !RB_EMPTY_ROOT(&rq->dl.pushable_dl_tasks_root);
	return rq->dl.pushable_dl_tasks_leftmost != NULL;
Quoted 33 lines
kernel/sched_dl.c +28 -0
+}
+
+static int push_dl_task(struct rq *rq);
+
+#else
+
+static inline
+void enqueue_pushable_dl_task(struct rq *rq, struct task_struct *p)
+{
+}
+
+static inline
+void dequeue_pushable_dl_task(struct rq *rq, struct task_struct *p)
+{
+}
+
+static inline
+void inc_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
+{
+}
+
+static inline
+void dec_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
+{
+}
+
+#endif /* CONFIG_SMP */
+
  static void enqueue_task_dl(struct rq *rq, struct task_struct *p, int flags);
  static void __dequeue_task_dl(struct rq *rq, struct task_struct *p, int flags);
  static void check_preempt_curr_dl(struct rq *rq, struct task_struct *p,
@@ -276,6 +433,14 @@ static enum hrtimer_restart dl_task_timer(struct hrtimer *timer)
                        check_preempt_curr_dl(rq, p, 0);
Quoted 8 lines

               else
                       resched_task(rq->curr);
+#ifdef CONFIG_SMP
+               /*
+                * Queueing this task back might have overloaded rq,
+                * check if we need to kick someone away.
+                */
+               if (rq->dl.overloaded)

if (has_pushable_dl_tasks(rq))

Quoted 6 lines

+                       push_dl_task(rq);
+#endif
       }
 unlock:
       task_rq_unlock(rq, p, &flags);

kernel/sched_dl.c static void update_curr_dl(struct rq *rq) · line 531

Quoted 8 lines
kernel/sched_dl.c +5 -0
@@ -359,6 +524,100 @@ static void update_curr_dl(struct rq *rq)
        }
  }
 
+#ifdef CONFIG_SMP
+
+static struct task_struct *pick_next_earliest_dl_task(struct rq *rq, int cpu);
+
+static inline int next_deadline(struct rq *rq)

static inline typeof(next->dl.deadline) next_deadline(struct rq *rq) ?

Quoted 55 lines
kernel/sched_dl.c +55 -0
+{
+       struct task_struct *next = pick_next_earliest_dl_task(rq, rq->cpu);
+
+       if (next && dl_prio(next->prio))
+               return next->dl.deadline;
+       else
+               return 0;
+}
+
+static void inc_dl_deadline(struct dl_rq *dl_rq, u64 deadline)
+{
+       struct rq *rq = rq_of_dl_rq(dl_rq);
+
+       if (dl_rq->earliest_dl.curr == 0 ||
+           dl_time_before(deadline, dl_rq->earliest_dl.curr)) {
+               /*
+                * If the dl_rq had no -deadline tasks, or if the new task
+                * has shorter deadline than the current one on dl_rq, we
+                * know that the previous earliest becomes our next earliest,
+                * as the new task becomes the earliest itself.
+                */
+               dl_rq->earliest_dl.next = dl_rq->earliest_dl.curr;
+               dl_rq->earliest_dl.curr = deadline;
+       } else if (dl_rq->earliest_dl.next == 0 ||
+                  dl_time_before(deadline, dl_rq->earliest_dl.next)) {
+               /*
+                * On the other hand, if the new -deadline task has a
+                * a later deadline than the earliest one on dl_rq, but
+                * it is earlier than the next (if any), we must
+                * recompute the next-earliest.
+                */
+               dl_rq->earliest_dl.next = next_deadline(rq);
+       }
+}
+
+static void dec_dl_deadline(struct dl_rq *dl_rq, u64 deadline)
+{
+       struct rq *rq = rq_of_dl_rq(dl_rq);
+
+       /*
+        * Since we may have removed our earliest (and/or next earliest)
+        * task we must recompute them.
+        */
+       if (!dl_rq->dl_nr_running) {
+               dl_rq->earliest_dl.curr = 0;
+               dl_rq->earliest_dl.next = 0;
+       } else {
+               struct rb_node *leftmost = dl_rq->rb_leftmost;
+               struct sched_dl_entity *entry;
+
+               entry = rb_entry(leftmost, struct sched_dl_entity, rb_node);
+               dl_rq->earliest_dl.curr = entry->deadline;
+               dl_rq->earliest_dl.next = next_deadline(rq);
+       }
+}

kernel/sched_dl.c static void update_curr_dl(struct rq *rq) · line 596

Quoted 10 lines
kernel/sched_dl.c +10 -0
+
+#else
+
+static inline void inc_dl_deadline(struct dl_rq *dl_rq, u64 deadline) {}
+static inline void dec_dl_deadline(struct dl_rq *dl_rq, u64 deadline) {}
+
+#endif /* CONFIG_SMP */
+
+static inline
+void inc_dl_tasks(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)

void inc_dl_task(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)

kernel/sched_dl.c static void update_curr_dl(struct rq *rq) · line 609

Quoted 13 lines
kernel/sched_dl.c +13 -0
+{
+       int prio = dl_task_of(dl_se)->prio;
+       u64 deadline = dl_se->deadline;
+
+       WARN_ON(!dl_prio(prio));
+       dl_rq->dl_nr_running++;
+
+       inc_dl_deadline(dl_rq, deadline);
+       inc_dl_migration(dl_se, dl_rq);
+}
+
+static inline
+void dec_dl_tasks(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)

void dec_dl_task(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)

Quoted 57 lines
kernel/sched_dl.c +13 -2
+{
+       int prio = dl_task_of(dl_se)->prio;
+
+       WARN_ON(!dl_prio(prio));
+       WARN_ON(!dl_rq->dl_nr_running);
+       dl_rq->dl_nr_running--;
+
+       dec_dl_deadline(dl_rq, dl_se->deadline);
+       dec_dl_migration(dl_se, dl_rq);
+}
+
  static void __enqueue_dl_entity(struct sched_dl_entity *dl_se)
  {
        struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
@@ -386,7 +645,7 @@ static void __enqueue_dl_entity(struct sched_dl_entity *dl_se)
        rb_link_node(&dl_se->rb_node, parent, link);
        rb_insert_color(&dl_se->rb_node, &dl_rq->rb_root);
 
-       dl_rq->dl_nr_running++;
+       inc_dl_tasks(dl_se, dl_rq);
  }
 
  static void __dequeue_dl_entity(struct sched_dl_entity *dl_se)
@@ -406,7 +665,7 @@ static void __dequeue_dl_entity(struct sched_dl_entity *dl_se)
        rb_erase(&dl_se->rb_node, &dl_rq->rb_root);
        RB_CLEAR_NODE(&dl_se->rb_node);
 
-       dl_rq->dl_nr_running--;
+       dec_dl_tasks(dl_se, dl_rq);
  }
 

 static void

kernel/sched_dl.c +9 -0
@@ -444,11 +703,15 @@ static void enqueue_task_dl(struct rq *rq, struct task_struct *p, int flags)
                return;
 
        enqueue_dl_entity(&p->dl, flags);
+
+       if (!task_current(rq, p) && p->dl.nr_cpus_allowed > 1)
+               enqueue_pushable_dl_task(rq, p);
  }
 
  static void __dequeue_task_dl(struct rq *rq, struct task_struct *p, int flags)
  {
        dequeue_dl_entity(&p->dl);
+       dequeue_pushable_dl_task(rq, p);
  }
 
  static void dequeue_task_dl(struct rq *rq, struct task_struct *p, int flags)
@@ -480,6 +743,75 @@ static void yield_task_dl(struct rq *rq)
        update_curr_dl(rq);
  }
 
+#ifdef CONFIG_SMP
+
+static int find_later_rq(struct task_struct *task);
+static int latest_cpu_find(struct cpumask *span,
+                          struct task_struct *task,

kernel/sched_dl.c static void yield_task_dl(struct rq *rq) · line 760

Quoted 10 lines
kernel/sched_dl.c +10 -0
+                          struct cpumask *later_mask);
+
+static int
+select_task_rq_dl(struct task_struct *p, int sd_flag, int flags)
+{
+       struct task_struct *curr;
+       struct rq *rq;
+       int cpu;
+
+       if (sd_flag != SD_BALANCE_WAKE)

why is task_cpu(p) not eligible?

Quoted 8 lines
kernel/sched_dl.c +8 -0
+               return smp_processor_id();
+
+       cpu = task_cpu(p);
+       rq = cpu_rq(cpu);
+
+       rcu_read_lock();
+       curr = ACCESS_ONCE(rq->curr); /* unlocked access */
+

kernel/sched_dl.c static void yield_task_dl(struct rq *rq) · line 778

Quoted 10 lines
kernel/sched_dl.c +10 -0
+       /*
+        * If we are dealing with a -deadline task, we must
+        * decide where to wake it up.
+        * If it has a later deadline and the current task
+        * on this rq can't move (provided the waking task
+        * can!) we prefer to send it somewhere else. On the
+        * other hand, if it has a shorter deadline, we
+        * try to make it stay here, it might be important.
+        */
+       if (unlikely(dl_task(rq->curr)) &&

the above ACCESS_ONCE for what?

kernel/sched_dl.c static void yield_task_dl(struct rq *rq) · line 780

Quoted 2 lines
kernel/sched_dl.c +2 -0
+           (rq->curr->dl.nr_cpus_allowed < 2 ||
+            dl_entity_preempt(&rq->curr->dl, &p->dl)) &&
		!dl_entity_preempt(&p->dl, &rq->curr->dl)) &&
Quoted 38 lines
kernel/sched_dl.c +34 -0
+           (p->dl.nr_cpus_allowed > 1)) {
+               int target = find_later_rq(p);
+
+               if (target != -1)
+                       cpu = target;
+       }
+       rcu_read_unlock();
+
+       return cpu;
+}
+
+static void check_preempt_equal_dl(struct rq *rq, struct task_struct *p)
+{
+       /*
+        * Current can't be migrated, useles to reschedule,
+        * let's hope p can move out.
+        */
+       if (rq->curr->dl.nr_cpus_allowed == 1 ||
+           latest_cpu_find(rq->rd->span, rq->curr, NULL) == -1)
+               return;
+
+       /*
+        * p is migratable, so let's not schedule it and
+        * see if it is pushed or pulled somewhere else.
+        */
+       if (p->dl.nr_cpus_allowed != 1 &&
+           latest_cpu_find(rq->rd->span, p, NULL) != -1)
+               return;
+
+       resched_task(rq->curr);
+}
+
+#endif /* CONFIG_SMP */
+
  /*
  * Only called when both the current and waking task are -deadline
  * tasks.

kernel/sched_dl.c static void yield_task_dl(struct rq *rq) · line 822

Quoted 5 lines
kernel/sched_dl.c +1 -1
@@ -487,8 +819,20 @@ static void yield_task_dl(struct rq *rq)
  static void check_preempt_curr_dl(struct rq *rq, struct task_struct *p,
                                  int flags)
  {
-       if (dl_time_before(p->dl.deadline, rq->curr->dl.deadline))
+       if (dl_time_before(p->dl.deadline, rq->curr->dl.deadline)) {
	if (dl_entity_preempt(&p->dl, &rq->curr->dl))

kernel/sched_dl.c static void yield_task_dl(struct rq *rq) · line 833

Quoted 11 lines
kernel/sched_dl.c +10 -0
                resched_task(rq->curr);
+               return;
+       }
+
+#ifdef CONFIG_SMP
+       /*
+        * In the unlikely case current and p have the same deadline
+        * let us try to decide what's the best thing to do...
+        */
+       if ((s64)(p->dl.deadline - rq->curr->dl.deadline) == 0 &&
+           !need_resched())

please recheck !need_resched(), say rq->curr need reschedule?

Quoted 7 lines
kernel/sched_dl.c +2 -0
+               check_preempt_equal_dl(rq, p);
+#endif /* CONFIG_SMP */
  }
 
  #ifdef CONFIG_SCHED_HRTICK
@@ -532,10 +876,20 @@ struct task_struct *pick_next_task_dl(struct rq *rq)
 

kernel/sched_dl.c struct task_struct *pick_next_task_dl(struct rq *rq) · line 886

Quoted 10 lines
kernel/sched_dl.c +5 -0
        p = dl_task_of(dl_se);
        p->se.exec_start = rq->clock;
+
+       /* Running task will never be pushed. */
+       if (p)
+               dequeue_pushable_dl_task(rq, p);
+
  #ifdef CONFIG_SCHED_HRTICK
        if (hrtick_enabled(rq))
                start_hrtick_dl(rq, p);

need to check p is valid?

kernel/sched_dl.c static void put_prev_task_dl(struct rq *rq, struct task_struct *p) · line 899

Quoted 13 lines
kernel/sched_dl.c +5 -0
  #endif
+
+#ifdef CONFIG_SMP
+       rq->post_schedule = has_pushable_dl_tasks(rq);
+#endif /* CONFIG_SMP */
+
        return p;
  }
 
@@ -543,6 +897,9 @@ static void put_prev_task_dl(struct rq *rq, struct task_struct *p)
  {
        update_curr_dl(rq);
        p->se.exec_start = 0;

why seset exec_start?

Quoted 21 lines
kernel/sched_dl.c +11 -1
+
+       if (on_dl_rq(&p->dl) && p->dl.nr_cpus_allowed > 1)
+               enqueue_pushable_dl_task(rq, p);
  }
 
  static void task_tick_dl(struct rq *rq, struct task_struct *p, int queued)
@@ -576,43 +933,410 @@ static void set_curr_task_dl(struct rq *rq)
        struct task_struct *p = rq->curr;
 
        p->se.exec_start = rq->clock;
+
+       /* You can't push away the running task */
+       dequeue_pushable_dl_task(rq, p);
  }
 
-static void switched_from_dl(struct rq *rq, struct task_struct *p)
+#ifdef CONFIG_SMP
+
+/* Only try algorithms three times */
+#define DL_MAX_TRIES 3
+

kernel/sched_dl.c static void set_curr_task_dl(struct rq *rq) · line 953

Quoted 10 lines
kernel/sched_dl.c +7 -2
+static int pick_dl_task(struct rq *rq, struct task_struct *p, int cpu)
  {
-       if (hrtimer_active(&p->dl.dl_timer))
-               hrtimer_try_to_cancel(&p->dl.dl_timer);
+       if (!task_running(rq, p) &&
+           (cpu < 0 || cpumask_test_cpu(cpu, &p->cpus_allowed)) &&
+           (p->dl.nr_cpus_allowed > 1))
+               return 1;
+
+       return 0;
	if (task_running(rq, p))
		return 0;
	return cpumask_test_cpu(cpu, &p->cpus_allowed);
	that is all:)
Quoted 24 lines
kernel/sched_dl.c +21 -1
  }
 
-static void switched_to_dl(struct rq *rq, struct task_struct *p)
+/* Returns the second earliest -deadline task, NULL otherwise */
+static struct task_struct *pick_next_earliest_dl_task(struct rq *rq, int cpu)
+{
+       struct rb_node *next_node = rq->dl.rb_leftmost;
+       struct sched_dl_entity *dl_se;
+       struct task_struct *p = NULL;
+
+next_node:
+       next_node = rb_next(next_node);
+       if (next_node) {
+               dl_se = rb_entry(next_node, struct sched_dl_entity, rb_node);
+               p = dl_task_of(dl_se);
+
+               if (pick_dl_task(rq, p, cpu))
+                       return p;
+
+               goto next_node;
+       }
+
+       return NULL;
+}

kernel/sched_dl.c static void set_curr_task_dl(struct rq *rq) · line 986

Quoted 10 lines
kernel/sched_dl.c +9 -0
+
+static int latest_cpu_find(struct cpumask *span,
+                          struct task_struct *task,
+                          struct cpumask *later_mask)
  {
+       const struct sched_dl_entity *dl_se = &task->dl;
+       int cpu, found = -1, best = 0;
+       u64 max_dl = 0;
+
+       for_each_cpu(cpu, span) {
	for_each_cpu_and(cpu, span, &task->cpus_allowed) {

kernel/sched_dl.c static void set_curr_task_dl(struct rq *rq) · line 992

Quoted 6 lines
kernel/sched_dl.c +6 -0
+               struct rq *rq = cpu_rq(cpu);
+               struct dl_rq *dl_rq = &rq->dl;
+
+               if (cpumask_test_cpu(cpu, &task->cpus_allowed) &&
+                   (!dl_rq->dl_nr_running || dl_time_before(dl_se->deadline,
+                    dl_rq->earliest_dl.curr))) {

please use dl_entity_preempt()

Quoted 14 lines
kernel/sched_dl.c +14 -0
+                       if (later_mask)
+                               cpumask_set_cpu(cpu, later_mask);
+                       if (!best && !dl_rq->dl_nr_running) {
+                               best = 1;
+                               found = cpu;
+                       } else if (!best &&
+                                  dl_time_before(max_dl,
+                                                 dl_rq->earliest_dl.curr)) {
+                               max_dl = dl_rq->earliest_dl.curr;
+                               found = cpu;
+                       }
+               } else if (later_mask)
+                       cpumask_clear_cpu(cpu, later_mask);
+       }

kernel/sched_dl.c static void set_curr_task_dl(struct rq *rq) · line 1016

Quoted 10 lines
kernel/sched_dl.c +10 -0
+
+       return found;
+}
+
+static DEFINE_PER_CPU(cpumask_var_t, local_cpu_mask_dl);
+
+static int find_later_rq(struct task_struct *task)
+{
+       struct sched_domain *sd;
+       struct cpumask *later_mask = __get_cpu_var(local_cpu_mask_dl);

please check is local_cpu_mask_dl valid

Quoted 508 lines
kernel/sched_dl.c +453 -17
+       int this_cpu = smp_processor_id();
+       int best_cpu, cpu = task_cpu(task);
+
+       if (task->dl.nr_cpus_allowed == 1)
+               return -1;
+
+       best_cpu = latest_cpu_find(task_rq(task)->rd->span, task, later_mask);
+       if (best_cpu == -1)
+               return -1;
+
        /*
-        * If p is throttled, don't consider the possibility
-        * of preempting rq->curr, the check will be done right
-        * after its runtime will get replenished.
+        * If we are here, some target has been found,
+        * the most suitable of which is cached in best_cpu.
+        * This is, among the runqueues where the current tasks
+        * have later deadlines than the task's one, the rq
+        * with the latest possible one.
+        *
+        * Now we check how well this matches with task's
+        * affinity and system topology.
+        *
+        * The last cpu where the task run is our first
+        * guess, since it is most likely cache-hot there.
         */
-       if (unlikely(p->dl.dl_throttled))
-               return;
+       if (cpumask_test_cpu(cpu, later_mask))
+               return cpu;
+       /*
+        * Check if this_cpu is to be skipped (i.e., it is
+        * not in the mask) or not.
+        */
+       if (!cpumask_test_cpu(this_cpu, later_mask))
+               this_cpu = -1;
+
+       rcu_read_lock();
+       for_each_domain(cpu, sd) {
+               if (sd->flags & SD_WAKE_AFFINE) {
+
+                       /*
+                        * If possible, preempting this_cpu is
+                        * cheaper than migrating.
+                        */
+                       if (this_cpu != -1 &&
+                           cpumask_test_cpu(this_cpu, sched_domain_span(sd)))
+                               return this_cpu;
+
+                       /*
+                        * Last chance: if best_cpu is valid and is
+                        * in the mask, that becomes our choice.
+                        */
+                       if (best_cpu < nr_cpu_ids &&
+                           cpumask_test_cpu(best_cpu, sched_domain_span(sd)))
+                               return best_cpu;
+               }
+       }
+       rcu_read_unlock();
 
-       if (!p->on_rq || rq->curr != p) {
-               if (task_has_dl_policy(rq->curr))
-                       check_preempt_curr_dl(rq, p, 0);
-               else
-                       resched_task(rq->curr);
+       /*
+        * At this point, all our guesses failed, we just return
+        * 'something', and let the caller sort the things out.
+        */
+       if (this_cpu != -1)
+               return this_cpu;
+
+       cpu = cpumask_any(later_mask);
+       if (cpu < nr_cpu_ids)
+               return cpu;
+
+       return -1;
+}
+
+/* Locks the rq it finds */
+static struct rq *find_lock_later_rq(struct task_struct *task, struct rq *rq)
+{
+       struct rq *later_rq = NULL;
+       int tries;
+       int cpu;
+
+       for (tries = 0; tries < DL_MAX_TRIES; tries++) {
+               cpu = find_later_rq(task);
+
+               if ((cpu == -1) || (cpu == rq->cpu))
+                       break;
+
+               later_rq = cpu_rq(cpu);
+
+               /* Retry if something changed. */
+               if (double_lock_balance(rq, later_rq)) {
+                       if (unlikely(task_rq(task) != rq ||
+                                    !cpumask_test_cpu(later_rq->cpu,
+                                                      &task->cpus_allowed) ||
+                                    task_running(rq, task) ||
+                                    !task->se.on_rq)) {
+                               raw_spin_unlock(&later_rq->lock);
+                               later_rq = NULL;
+                               break;
+                       }
+               }
+
+               /*
+                * If the rq we found has no -deadline task, or
+                * its earliest one has a later deadline than our
+                * task, the rq is a good one.
+                */
+               if (!later_rq->dl.dl_nr_running ||
+                   dl_time_before(task->dl.deadline,
+                                  later_rq->dl.earliest_dl.curr))
+                       break;
+
+               /* Otherwise we try again. */
+               double_unlock_balance(rq, later_rq);
+               later_rq = NULL;
        }
+
+       return later_rq;
  }
 
-static void prio_changed_dl(struct rq *rq, struct task_struct *p,
-                           int oldprio)
+static struct task_struct *pick_next_pushable_dl_task(struct rq *rq)
  {
-       switched_to_dl(rq, p);
+       struct task_struct *p;
+
+       if (!has_pushable_dl_tasks(rq))
+               return NULL;
+
+       p = rb_entry(rq->dl.pushable_dl_tasks_leftmost,
+                    struct task_struct, pushable_dl_tasks);
+
+       BUG_ON(rq->cpu != task_cpu(p));
+       BUG_ON(task_current(rq, p));
+       BUG_ON(p->dl.nr_cpus_allowed <= 1);
+
+       BUG_ON(!p->se.on_rq);
+       BUG_ON(!dl_task(p));
+
+       return p;
  }
 
-#ifdef CONFIG_SMP
-static int
-select_task_rq_dl(struct task_struct *p, int sd_flag, int flags)
+/*
+ * See if the non running -deadline tasks on this rq
+ * can be sent to some other CPU where they can preempt
+ * and start executing.
+ */
+static int push_dl_task(struct rq *rq)
  {
-       return task_cpu(p);
+       struct task_struct *next_task;
+       struct rq *later_rq;
+
+       if (!rq->dl.overloaded)
+               return 0;
+
+       next_task = pick_next_pushable_dl_task(rq);
+       if (!next_task)
+               return 0;
+
+retry:
+       if (unlikely(next_task == rq->curr)) {
+               WARN_ON(1);
+               return 0;
+       }
+
+       /*
+        * If next_task preempts rq->curr, and rq->curr
+        * can move away, it makes sense to just reschedule
+        * without going further in pushing next_task.
+        */
+       if (dl_task(rq->curr) &&
+           dl_time_before(next_task->dl.deadline, rq->curr->dl.deadline) &&
+           rq->curr->dl.nr_cpus_allowed > 1) {
+               resched_task(rq->curr);
+               return 0;
+       }
+
+       /* We might release rq lock */
+       get_task_struct(next_task);
+
+       /* Will lock the rq it'll find */
+       later_rq = find_lock_later_rq(next_task, rq);
+       if (!later_rq) {
+               struct task_struct *task;
+
+               /*
+                * We must check all this again, since
+                * find_lock_later_rq releases rq->lock and it is
+                * then possible that next_task has migrated.
+                */
+               task = pick_next_pushable_dl_task(rq);
+               if (task_cpu(next_task) == rq->cpu && task == next_task) {
+                       /*
+                        * The task is still there. We don't try
+                        * again, some other cpu will pull it when ready.
+                        */
+                       dequeue_pushable_dl_task(rq, next_task);
+                       goto out;
+               }
+
+               if (!task)
+                       /* No more tasks */
+                       goto out;
+
+               put_task_struct(next_task);
+               next_task = task;
+               goto retry;
+       }
+
+       deactivate_task(rq, next_task, 0);
+       set_task_cpu(next_task, later_rq->cpu);
+       activate_task(later_rq, next_task, 0);
+
+       resched_task(later_rq->curr);
+
+       double_unlock_balance(rq, later_rq);
+
+out:
+       put_task_struct(next_task);
+
+       return 1;
+}
+
+static void push_dl_tasks(struct rq *rq)
+{
+       /* Terminates as it moves a -deadline task */
+       while (push_dl_task(rq))
+               ;
+}
+
+static int pull_dl_task(struct rq *this_rq)
+{
+       int this_cpu = this_rq->cpu, ret = 0, cpu;
+       struct task_struct *p;
+       struct rq *src_rq;
+       u64 dmin = LONG_MAX;
+
+       if (likely(!dl_overloaded(this_rq)))
+               return 0;
+
+       for_each_cpu(cpu, this_rq->rd->dlo_mask) {
+               if (this_cpu == cpu)
+                       continue;
+
+               src_rq = cpu_rq(cpu);
+
+               /*
+                * It looks racy, abd it is! However, as in sched_rt.c,
+                * we are fine with this.
+                */
+               if (this_rq->dl.dl_nr_running &&
+                   dl_time_before(this_rq->dl.earliest_dl.curr,
+                                  src_rq->dl.earliest_dl.next))
+                       continue;
+
+               /* Might drop this_rq->lock */
+               double_lock_balance(this_rq, src_rq);
+
+               /*
+                * If there are no more pullable tasks on the
+                * rq, we're done with it.
+                */
+               if (src_rq->dl.dl_nr_running <= 1)
+                       goto skip;
+
+               p = pick_next_earliest_dl_task(src_rq, this_cpu);
+
+               /*
+                * We found a task to be pulled if:
+                *  - it preempts our current (if there's one),
+                *  - it will preempt the last one we pulled (if any).
+                */
+               if (p && dl_time_before(p->dl.deadline, dmin) &&
+                   (!this_rq->dl.dl_nr_running ||
+                    dl_time_before(p->dl.deadline,
+                                   this_rq->dl.earliest_dl.curr))) {
+                       WARN_ON(p == src_rq->curr);
+                       WARN_ON(!p->se.on_rq);
+
+                       /*
+                        * Then we pull iff p has actually an earlier
+                        * deadline than the current task of its runqueue.
+                        */
+                       if (dl_time_before(p->dl.deadline,
+                                          src_rq->curr->dl.deadline))
+                               goto skip;
+
+                       ret = 1;
+
+                       deactivate_task(src_rq, p, 0);
+                       set_task_cpu(p, this_cpu);
+                       activate_task(this_rq, p, 0);
+                       dmin = p->dl.deadline;
+
+                       /* Is there any other task even earlier? */
+               }
+skip:
+               double_unlock_balance(this_rq, src_rq);
+       }
+
+       return ret;
+}
+
+static void pre_schedule_dl(struct rq *rq, struct task_struct *prev)
+{
+       /* Try to pull other tasks here */
+       if (dl_task(prev))
+               pull_dl_task(rq);
+}
+
+static void post_schedule_dl(struct rq *rq)
+{
+       push_dl_tasks(rq);
+}
+
+/*
+ * Since the task is not running and a reschedule is not going to happen
+ * anytime soon on its runqueue, we try pushing it away now.
+ */
+static void task_woken_dl(struct rq *rq, struct task_struct *p)
+{
+       if (!task_running(rq, p) &&
+           !test_tsk_need_resched(rq->curr) &&
+           has_pushable_dl_tasks(rq) &&
+           p->dl.nr_cpus_allowed > 1 &&
+           dl_task(rq->curr) &&
+           (rq->curr->dl.nr_cpus_allowed < 2 ||
+            dl_entity_preempt(&rq->curr->dl, &p->dl))) {
+               push_dl_tasks(rq);
+       }
  }
 
  static void set_cpus_allowed_dl(struct task_struct *p,
@@ -622,10 +1346,145 @@ static void set_cpus_allowed_dl(struct task_struct *p,
 
        BUG_ON(!dl_task(p));
 
+       /*
+        * Update only if the task is actually running (i.e.,
+        * it is on the rq AND it is not throttled).
+        */
+       if (on_dl_rq(&p->dl) && (weight != p->dl.nr_cpus_allowed)) {
+               struct rq *rq = task_rq(p);
+
+               if (!task_current(rq, p)) {
+                       /*
+                        * If the task was on the pushable list,
+                        * make sure it stays there only if the new
+                        * mask allows that.
+                        */
+                       if (p->dl.nr_cpus_allowed > 1)
+                               dequeue_pushable_dl_task(rq, p);
+
+                       if (weight > 1)
+                               enqueue_pushable_dl_task(rq, p);
+               }
+
+               if ((p->dl.nr_cpus_allowed <= 1) && (weight > 1)) {
+                       rq->dl.dl_nr_migratory++;
+               } else if ((p->dl.nr_cpus_allowed > 1) && (weight <= 1)) {
+                       BUG_ON(!rq->dl.dl_nr_migratory);
+                       rq->dl.dl_nr_migratory--;
+               }
+
+               update_dl_migration(&rq->dl);
+       }
+
        cpumask_copy(&p->cpus_allowed, new_mask);
        p->dl.nr_cpus_allowed = weight;
  }
+
+/* Assumes rq->lock is held */
+static void rq_online_dl(struct rq *rq)
+{
+       if (rq->dl.overloaded)
+               dl_set_overload(rq);
+}
+
+/* Assumes rq->lock is held */
+static void rq_offline_dl(struct rq *rq)
+{
+       if (rq->dl.overloaded)
+               dl_clear_overload(rq);
+}
+
+static inline void init_sched_dl_class(void)
+{
+       unsigned int i;
+
+       for_each_possible_cpu(i)
+               zalloc_cpumask_var_node(&per_cpu(local_cpu_mask_dl, i),
+                                       GFP_KERNEL, cpu_to_node(i));
+}
+
+#endif /* CONFIG_SMP */
+
+static void switched_from_dl(struct rq *rq, struct task_struct *p)
+{
+       if (hrtimer_active(&p->dl.dl_timer) && !dl_policy(p->policy))
+               hrtimer_try_to_cancel(&p->dl.dl_timer);
+
+#ifdef CONFIG_SMP
+       /*
+        * Since this might be the only -deadline task on the rq,
+        * this is the right place to try to pull some other one
+        * from an overloaded cpu, if any.
+        */
+       if (!rq->dl.dl_nr_running)
+               pull_dl_task(rq);
  #endif
+}
+
+/*
+ * When switching to -deadline, we may overload the rq, then
+ * we try to push someone off, if possible.
+ */
+static void switched_to_dl(struct rq *rq, struct task_struct *p)
+{
+       int check_resched = 1;
+
+       /*
+        * If p is throttled, don't consider the possibility
+        * of preempting rq->curr, the check will be done right
+        * after its runtime will get replenished.
+        */
+       if (unlikely(p->dl.dl_throttled))
+               return;
+
+       if (!p->on_rq || rq->curr != p) {
+#ifdef CONFIG_SMP
+               if (rq->dl.overloaded && push_dl_task(rq) && rq != task_rq(p))
+                       /* Only reschedule if pushing failed */
+                       check_resched = 0;
+#endif /* CONFIG_SMP */
+               if (check_resched && task_has_dl_policy(rq->curr))
+                       check_preempt_curr_dl(rq, p, 0);
+       }
+}
+
+/*
+ * If the scheduling parameters of a -deadline task changed,
+ * a push or pull operation might be needed.
+ */
+static void prio_changed_dl(struct rq *rq, struct task_struct *p,
+                           int oldprio)
+{
+       if (p->on_rq || rq->curr == p) {
+#ifdef CONFIG_SMP
+               /*
+                * This might be too much, but unfortunately
+                * we don't have the old deadline value, and
+                * we can't argue if the task is increasing
+                * or lowering its prio, so...
+                */
+               if (!rq->dl.overloaded)
+                       pull_dl_task(rq);
+
+               /*
+                * If we now have a earlier deadline task than p,
+                * then reschedule, provided p is still on this
+                * runqueue.
+                */
+               if (dl_time_before(rq->dl.earliest_dl.curr, p->dl.deadline) &&
+                   rq->curr == p)
+                       resched_task(p);
+#else
+               /*
+                * Again, we don't know if p has a earlier
+                * or later deadline, so let's blindly set a
+                * (maybe not needed) rescheduling point.
+                */
+               resched_task(p);
+#endif /* CONFIG_SMP */
+       } else
+               switched_to_dl(rq, p);
+}
 
  static const struct sched_class dl_sched_class = {
        .next                   = &rt_sched_class,
@@ -642,6 +1501,11 @@ static const struct sched_class dl_sched_class = {
        .select_task_rq         = select_task_rq_dl,
 
        .set_cpus_allowed       = set_cpus_allowed_dl,
+       .rq_online              = rq_online_dl,
+       .rq_offline             = rq_offline_dl,
+       .pre_schedule           = pre_schedule_dl,
+       .post_schedule          = post_schedule_dl,
+       .task_woken             = task_woken_dl,
  #endif
 
        .set_curr_task          = set_curr_task_dl,
kernel/sched_rt.c +0 -0
index 4b09704..7b609bc 100644
@@ -1590,7 +1590,7 @@ static void task_woken_rt(struct rq *rq, struct task_struct *p)

           !test_tsk_need_resched(rq->curr) &&
           has_pushable_tasks(rq) &&

Quoted 9 lines

           p->rt.nr_cpus_allowed > 1 &&
-           rt_task(rq->curr) &&
+           (dl_task(rq->curr) || rt_task(rq->curr)) && (rq->curr->rt.nr_cpus_allowed < 2 || rq->curr->prio <= p->prio)) push_rt_tasks(rq);
--
1.7.5.4

Would you please mail me, in attachment, a monolithic patch of this work?

Thanks
-hd ���{.n�+�������+%�����ݶ��w��{.n�+����{��G�����{ay�ʇڙ�,j��f���h��������z_�(�階�ݢj"���m������G������?����&���~��iO��z��v�^�m���� ������?�I�

author
#65 lore raw

Re: [PATCH 06/16] sched: SCHED_DEADLINE push and pull logic

Hi,

Quoted 128 lines · Hillf Danton

Hello

Quoted 125 lines · Juri Lelli

Add dynamic migrations to SCHED_DEADLINE, so that tasks can be moved among CPUs when necessary. It is also possible to bind a task to a (set of) CPU(s), thus restricting its capability of migrating, or forbidding migrations at all.

The very same approach used in sched_rt is utilised:
- -deadline tasks are kept into CPU-specific runqueues,
- -deadline tasks are migrated among runqueues to achieve the following:
* on an M-CPU system the M earliest deadline ready tasks are always running;
* affinity/cpusets settings of all the -deadline tasks is always respected.

Therefore, this very special form of "load balancing" is done with an active method, i.e., the scheduler pushes or pulls tasks between runqueues when they are woken up and/or (de)scheduled.
IOW, every time a preemption occurs, the descheduled task might be sent to some other CPU (depending on its deadline) to continue executing (push). On the other hand, every time a CPU becomes idle, it might pull the second earliest deadline ready task from some other CPU.

To enforce this, a pull operation is always attempted before taking any scheduling decision (pre_schedule()), as well as a push one after each scheduling decision (post_schedule()). In addition, when a task arrives or wakes up, the best CPU where to resume it is selected taking into account its affinity mask, the system topology, but also its deadline. E.g., from the scheduling point of view, the best CPU where to wake up (and also where to push) a task is the one which is running the task with the latest deadline among the M executing ones.

In order to facilitate these decisions, per-runqueue "caching" of the deadlines of the currently running and of the first ready task is used. Queued but not running tasks are also parked in another rb-tree to speed-up pushes.

---
  kernel/sched_dl.c |  912 +++++++++++++++++++++++++++++++++++++++++++++++++++--
  kernel/sched_rt.c |    2 +-
  2 files changed, 889 insertions(+), 25 deletions(-)
kernel/sched_dl.c +60 -0
index 604e2bc..38edefa 100644
@@ -10,6 +10,7 @@
  * miss some of their deadlines), and won't affect any other task.
  *
  * Copyright (C) 2010 Dario Faggioli<[email protected]>,
+ *                    Juri Lelli<[email protected]>,
  *                    Michael Trimarchi<[email protected]>,
  *                    Fabio Checconi<[email protected]>
  */
@@ -20,6 +21,15 @@ static inline int dl_time_before(u64 a, u64 b)
        return (s64)(a - b)<  0;
  }
 
+/*
+ * Tells if entity @a should preempt entity @b.
+ */
+static inline
+int dl_entity_preempt(struct sched_dl_entity *a, struct sched_dl_entity *b)
+{
+       return dl_time_before(a->deadline, b->deadline);
+}
+
  static inline struct task_struct *dl_task_of(struct sched_dl_entity *dl_se)
  {
        return container_of(dl_se, struct task_struct, dl);
@@ -50,6 +60,153 @@ static inline int is_leftmost(struct task_struct *p, struct dl_rq *dl_rq)
        return dl_rq->rb_leftmost ==&dl_se->rb_node;
  }
 
+#ifdef CONFIG_SMP
+
+static inline int dl_overloaded(struct rq *rq)
+{
+       return atomic_read(&rq->rd->dlo_count);
+}
+
+static inline void dl_set_overload(struct rq *rq)
+{
+       if (!rq->online)
+               return;
+
+       cpumask_set_cpu(rq->cpu, rq->rd->dlo_mask);
+       /*
+        * Must be visible before the overload count is
+        * set (as in sched_rt.c).
+        */
+       wmb();
+       atomic_inc(&rq->rd->dlo_count);
+}
+
+static inline void dl_clear_overload(struct rq *rq)
+{
+       if (!rq->online)
+               return;
+
+       atomic_dec(&rq->rd->dlo_count);
+       cpumask_clear_cpu(rq->cpu, rq->rd->dlo_mask);
+}
+
+static void update_dl_migration(struct dl_rq *dl_rq)
+{
+       if (dl_rq->dl_nr_migratory&&  dl_rq->dl_nr_total>  1) {
+               if (!dl_rq->overloaded) {
+                       dl_set_overload(rq_of_dl_rq(dl_rq));
+                       dl_rq->overloaded = 1;
+               }
+       } else if (dl_rq->overloaded) {
+               dl_clear_overload(rq_of_dl_rq(dl_rq));
+               dl_rq->overloaded = 0;
+       }
+}
+
+static void inc_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
+{
+       dl_rq =&rq_of_dl_rq(dl_rq)->dl;
+
+       dl_rq->dl_nr_total++;
+       if (dl_se->nr_cpus_allowed>  1)
+               dl_rq->dl_nr_migratory++;
Quoted 9 lines

kernel/sched_dl.c static inline int is_leftmost(struct task_struct *p, struct dl_rq *dl_rq) · line 114

Quoted 2 lines
kernel/sched_dl.c +2 -0
+
+       update_dl_migration(dl_rq);
	if (dl_se->nr_cpus_allowed>  1) {
		dl_rq->dl_nr_migratory++;
		/* No change in migratory, no update of migration */
		update_dl_migration(dl_rq);
	}

I think the code is probably cleaner as it is written now and aligned with sched_rt.

Quoted 5 lines
Quoted 5 lines
kernel/sched_dl.c +5 -0
+}
+
+static void dec_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
+{
+       dl_rq =&rq_of_dl_rq(dl_rq)->dl;
Quoted 9 lines

kernel/sched_dl.c static inline int is_leftmost(struct task_struct *p, struct dl_rq *dl_rq) · line 125

Quoted 6 lines
kernel/sched_dl.c +6 -0
+
+       dl_rq->dl_nr_total--;
+       if (dl_se->nr_cpus_allowed>  1)
+               dl_rq->dl_nr_migratory--;
+
+       update_dl_migration(dl_rq);

ditto

As above.

Quoted 15 lines
Quoted 15 lines
kernel/sched_dl.c +15 -0
+}
+
+/*
+ * The list of pushable -deadline task is not a plist, like in
+ * sched_rt.c, it is an rb-tree with tasks ordered by deadline.
+ */
+static void enqueue_pushable_dl_task(struct rq *rq, struct task_struct *p)
+{
+       struct dl_rq *dl_rq =&rq->dl;
+       struct rb_node **link =&dl_rq->pushable_dl_tasks_root.rb_node;
+       struct rb_node *parent = NULL;
+       struct task_struct *entry;
+       int leftmost = 1;
+
+       BUG_ON(!RB_EMPTY_NODE(&p->pushable_dl_tasks));
Quoted 9 lines

kernel/sched_dl.c static inline int is_leftmost(struct task_struct *p, struct dl_rq *dl_rq)

Quoted 6 lines
kernel/sched_dl.c +6 -0
+
+       while (*link) {
+               parent = *link;
+               entry = rb_entry(parent, struct task_struct,
+                                pushable_dl_tasks);
+               if (!dl_entity_preempt(&entry->dl,&p->dl))
		if (dl_entity_preempt(&p->dl,&entry->dl))

Any specific reason to reverse the condition?

Quoted 21 lines
Quoted 21 lines
kernel/sched_dl.c +21 -0
+                       link =&parent->rb_left;
+               else {
+                       link =&parent->rb_right;
+                       leftmost = 0;
+               }
+       }
+
+       if (leftmost)
+               dl_rq->pushable_dl_tasks_leftmost =&p->pushable_dl_tasks;
+
+       rb_link_node(&p->pushable_dl_tasks, parent, link);
+       rb_insert_color(&p->pushable_dl_tasks,&dl_rq->pushable_dl_tasks_root);
+}
+
+static void dequeue_pushable_dl_task(struct rq *rq, struct task_struct *p)
+{
+       struct dl_rq *dl_rq =&rq->dl;
+
+       if (RB_EMPTY_NODE(&p->pushable_dl_tasks))
+               return;
+
Quoted 9 lines

kernel/sched_dl.c static inline int is_leftmost(struct task_struct *p, struct dl_rq *dl_rq) · line 172

Quoted 5 lines
kernel/sched_dl.c +5 -0
+       if (dl_rq->pushable_dl_tasks_leftmost ==&p->pushable_dl_tasks) {
+               struct rb_node *next_node;
+
+               next_node = rb_next(&p->pushable_dl_tasks);
+               dl_rq->pushable_dl_tasks_leftmost = next_node;
		dl_rq->pushable_dl_tasks_leftmost =
					rb_next(&p->pushable_dl_tasks);

Yes, but mine is probably cleaner :-).

Quoted 11 lines

kernel/sched_dl.c static inline int is_leftmost(struct task_struct *p, struct dl_rq *dl_rq) · line 181

Quoted 9 lines
kernel/sched_dl.c +9 -0
+       }
+
+       rb_erase(&p->pushable_dl_tasks,&dl_rq->pushable_dl_tasks_root);
+       RB_CLEAR_NODE(&p->pushable_dl_tasks);
+}
+
+static inline int has_pushable_dl_tasks(struct rq *rq)
+{
+       return !RB_EMPTY_ROOT(&rq->dl.pushable_dl_tasks_root);
	return rq->dl.pushable_dl_tasks_leftmost != NULL;

Equivalent?

Quoted 34 lines
Quoted 34 lines
kernel/sched_dl.c +28 -0
+}
+
+static int push_dl_task(struct rq *rq);
+
+#else
+
+static inline
+void enqueue_pushable_dl_task(struct rq *rq, struct task_struct *p)
+{
+}
+
+static inline
+void dequeue_pushable_dl_task(struct rq *rq, struct task_struct *p)
+{
+}
+
+static inline
+void inc_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
+{
+}
+
+static inline
+void dec_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
+{
+}
+
+#endif /* CONFIG_SMP */
+
  static void enqueue_task_dl(struct rq *rq, struct task_struct *p, int flags);
  static void __dequeue_task_dl(struct rq *rq, struct task_struct *p, int flags);
  static void check_preempt_curr_dl(struct rq *rq, struct task_struct *p,
@@ -276,6 +433,14 @@ static enum hrtimer_restart dl_task_timer(struct hrtimer *timer)
                        check_preempt_curr_dl(rq, p, 0);
                else
Quoted 9 lines

kernel/sched_dl.c static enum hrtimer_restart dl_task_timer(struct hrtimer *timer) · line 441

Quoted 7 lines
kernel/sched_dl.c +6 -0
                        resched_task(rq->curr);
+#ifdef CONFIG_SMP
+               /*
+                * Queueing this task back might have overloaded rq,
+                * check if we need to kick someone away.
+                */
+               if (rq->dl.overloaded)

if (has_pushable_dl_tasks(rq))

Ok, better.

Quoted 9 lines
Quoted 9 lines
kernel/sched_dl.c +2 -0
+                       push_dl_task(rq);
+#endif
        }
  unlock:
        task_rq_unlock(rq, p,&flags);
@@ -359,6 +524,100 @@ static void update_curr_dl(struct rq *rq)
        }
  }
 
Quoted 7 lines

kernel/sched_dl.c static void update_curr_dl(struct rq *rq) · line 531

Quoted 5 lines
kernel/sched_dl.c +5 -0
+#ifdef CONFIG_SMP
+
+static struct task_struct *pick_next_earliest_dl_task(struct rq *rq, int cpu);
+
+static inline int next_deadline(struct rq *rq)

static inline typeof(next->dl.deadline) next_deadline(struct rq *rq) ?

Right!
static inline u64 next_deadline(struct rq *rq)

Quoted 58 lines
Quoted 58 lines
kernel/sched_dl.c +58 -0
+{
+       struct task_struct *next = pick_next_earliest_dl_task(rq, rq->cpu);
+
+       if (next&&  dl_prio(next->prio))
+               return next->dl.deadline;
+       else
+               return 0;
+}
+
+static void inc_dl_deadline(struct dl_rq *dl_rq, u64 deadline)
+{
+       struct rq *rq = rq_of_dl_rq(dl_rq);
+
+       if (dl_rq->earliest_dl.curr == 0 ||
+           dl_time_before(deadline, dl_rq->earliest_dl.curr)) {
+               /*
+                * If the dl_rq had no -deadline tasks, or if the new task
+                * has shorter deadline than the current one on dl_rq, we
+                * know that the previous earliest becomes our next earliest,
+                * as the new task becomes the earliest itself.
+                */
+               dl_rq->earliest_dl.next = dl_rq->earliest_dl.curr;
+               dl_rq->earliest_dl.curr = deadline;
+       } else if (dl_rq->earliest_dl.next == 0 ||
+                  dl_time_before(deadline, dl_rq->earliest_dl.next)) {
+               /*
+                * On the other hand, if the new -deadline task has a
+                * a later deadline than the earliest one on dl_rq, but
+                * it is earlier than the next (if any), we must
+                * recompute the next-earliest.
+                */
+               dl_rq->earliest_dl.next = next_deadline(rq);
+       }
+}
+
+static void dec_dl_deadline(struct dl_rq *dl_rq, u64 deadline)
+{
+       struct rq *rq = rq_of_dl_rq(dl_rq);
+
+       /*
+        * Since we may have removed our earliest (and/or next earliest)
+        * task we must recompute them.
+        */
+       if (!dl_rq->dl_nr_running) {
+               dl_rq->earliest_dl.curr = 0;
+               dl_rq->earliest_dl.next = 0;
+       } else {
+               struct rb_node *leftmost = dl_rq->rb_leftmost;
+               struct sched_dl_entity *entry;
+
+               entry = rb_entry(leftmost, struct sched_dl_entity, rb_node);
+               dl_rq->earliest_dl.curr = entry->deadline;
+               dl_rq->earliest_dl.next = next_deadline(rq);
+       }
+}
+
+#else
+
Quoted 9 lines

kernel/sched_dl.c static void update_curr_dl(struct rq *rq) · line 596

Quoted 7 lines
kernel/sched_dl.c +7 -0
+static inline void inc_dl_deadline(struct dl_rq *dl_rq, u64 deadline) {}
+static inline void dec_dl_deadline(struct dl_rq *dl_rq, u64 deadline) {}
+
+#endif /* CONFIG_SMP */
+
+static inline
+void inc_dl_tasks(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)

void inc_dl_task(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)

Well, I see this as "increment the number of -dl tasks"..

Quoted 6 lines
Quoted 6 lines
kernel/sched_dl.c +6 -0
+{
+       int prio = dl_task_of(dl_se)->prio;
+       u64 deadline = dl_se->deadline;
+
+       WARN_ON(!dl_prio(prio));
+       dl_rq->dl_nr_running++;
Quoted 9 lines

kernel/sched_dl.c static void update_curr_dl(struct rq *rq) · line 609

Quoted 7 lines
kernel/sched_dl.c +7 -0
+
+       inc_dl_deadline(dl_rq, deadline);
+       inc_dl_migration(dl_se, dl_rq);
+}
+
+static inline
+void dec_dl_tasks(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)

void dec_dl_task(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)

Like above.

Quoted 52 lines
Quoted 52 lines
kernel/sched_dl.c +17 -2
+{
+       int prio = dl_task_of(dl_se)->prio;
+
+       WARN_ON(!dl_prio(prio));
+       WARN_ON(!dl_rq->dl_nr_running);
+       dl_rq->dl_nr_running--;
+
+       dec_dl_deadline(dl_rq, dl_se->deadline);
+       dec_dl_migration(dl_se, dl_rq);
+}
+
  static void __enqueue_dl_entity(struct sched_dl_entity *dl_se)
  {
        struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
@@ -386,7 +645,7 @@ static void __enqueue_dl_entity(struct sched_dl_entity *dl_se)
        rb_link_node(&dl_se->rb_node, parent, link);
        rb_insert_color(&dl_se->rb_node,&dl_rq->rb_root);
 
-       dl_rq->dl_nr_running++;
+       inc_dl_tasks(dl_se, dl_rq);
  }
 
  static void __dequeue_dl_entity(struct sched_dl_entity *dl_se)
@@ -406,7 +665,7 @@ static void __dequeue_dl_entity(struct sched_dl_entity *dl_se)
        rb_erase(&dl_se->rb_node,&dl_rq->rb_root);
        RB_CLEAR_NODE(&dl_se->rb_node);
 
-       dl_rq->dl_nr_running--;
+       dec_dl_tasks(dl_se, dl_rq);
  }
 
  static void
@@ -444,11 +703,15 @@ static void enqueue_task_dl(struct rq *rq, struct task_struct *p, int flags)
                return;
 
        enqueue_dl_entity(&p->dl, flags);
+
+       if (!task_current(rq, p)&&  p->dl.nr_cpus_allowed>  1)
+               enqueue_pushable_dl_task(rq, p);
  }
 
  static void __dequeue_task_dl(struct rq *rq, struct task_struct *p, int flags)
  {
        dequeue_dl_entity(&p->dl);
+       dequeue_pushable_dl_task(rq, p);
  }
 
  static void dequeue_task_dl(struct rq *rq, struct task_struct *p, int flags)
@@ -480,6 +743,75 @@ static void yield_task_dl(struct rq *rq)
        update_curr_dl(rq);
  }
 
Quoted 17 lines
Quoted 5 lines
kernel/sched_dl.c +5 -0
+#ifdef CONFIG_SMP
+
+static int find_later_rq(struct task_struct *task);
+static int latest_cpu_find(struct cpumask *span,
+                          struct task_struct *task,

kernel/sched_dl.c static void yield_task_dl(struct rq *rq) · line 760

Quoted 10 lines
kernel/sched_dl.c +10 -0
+                          struct cpumask *later_mask);
+
+static int
+select_task_rq_dl(struct task_struct *p, int sd_flag, int flags)
+{
+       struct task_struct *curr;
+       struct rq *rq;
+       int cpu;
+
+       if (sd_flag != SD_BALANCE_WAKE)

why is task_cpu(p) not eligible?

Right, I'll change this.

Quoted 11 lines
Quoted 11 lines
kernel/sched_dl.c +11 -0
+               return smp_processor_id();
+
+       cpu = task_cpu(p);
+       rq = cpu_rq(cpu);
+
+       rcu_read_lock();
+       curr = ACCESS_ONCE(rq->curr); /* unlocked access */
+
+       /*
+        * If we are dealing with a -deadline task, we must
+        * decide where to wake it up.
Quoted 8 lines

kernel/sched_dl.c static void yield_task_dl(struct rq *rq)

Quoted 7 lines
kernel/sched_dl.c +7 -0
+        * If it has a later deadline and the current task
+        * on this rq can't move (provided the waking task
+        * can!) we prefer to send it somewhere else. On the
+        * other hand, if it has a shorter deadline, we
+        * try to make it stay here, it might be important.
+        */
+       if (unlikely(dl_task(rq->curr))&&

the above ACCESS_ONCE for what?

Yes, I'll change this.

Quoted 3 lines

kernel/sched_dl.c static void yield_task_dl(struct rq *rq) · line 780

Quoted 2 lines
kernel/sched_dl.c +2 -0
+           (rq->curr->dl.nr_cpus_allowed<  2 ||
+            dl_entity_preempt(&rq->curr->dl,&p->dl))&&
		!dl_entity_preempt(&p->dl,&rq->curr->dl))&&

As above?

Quoted 36 lines
Quoted 36 lines
kernel/sched_dl.c +34 -0
+           (p->dl.nr_cpus_allowed>  1)) {
+               int target = find_later_rq(p);
+
+               if (target != -1)
+                       cpu = target;
+       }
+       rcu_read_unlock();
+
+       return cpu;
+}
+
+static void check_preempt_equal_dl(struct rq *rq, struct task_struct *p)
+{
+       /*
+        * Current can't be migrated, useles to reschedule,
+        * let's hope p can move out.
+        */
+       if (rq->curr->dl.nr_cpus_allowed == 1 ||
+           latest_cpu_find(rq->rd->span, rq->curr, NULL) == -1)
+               return;
+
+       /*
+        * p is migratable, so let's not schedule it and
+        * see if it is pushed or pulled somewhere else.
+        */
+       if (p->dl.nr_cpus_allowed != 1&&
+           latest_cpu_find(rq->rd->span, p, NULL) != -1)
+               return;
+
+       resched_task(rq->curr);
+}
+
+#endif /* CONFIG_SMP */
+
  /*
  * Only called when both the current and waking task are -deadline
Quoted 8 lines

kernel/sched_dl.c static void yield_task_dl(struct rq *rq) · line 822

Quoted 7 lines
kernel/sched_dl.c +1 -1
  * tasks.
@@ -487,8 +819,20 @@ static void yield_task_dl(struct rq *rq)
  static void check_preempt_curr_dl(struct rq *rq, struct task_struct *p,
                                  int flags)
  {
-       if (dl_time_before(p->dl.deadline, rq->curr->dl.deadline))
+       if (dl_time_before(p->dl.deadline, rq->curr->dl.deadline)) {
	if (dl_entity_preempt(&p->dl,&rq->curr->dl))

Ok.

Quoted 4 lines
Quoted 4 lines
kernel/sched_dl.c +3 -0
                resched_task(rq->curr);
+               return;
+       }
+
Quoted 8 lines

kernel/sched_dl.c static void yield_task_dl(struct rq *rq) · line 833

Quoted 7 lines
kernel/sched_dl.c +7 -0
+#ifdef CONFIG_SMP
+       /*
+        * In the unlikely case current and p have the same deadline
+        * let us try to decide what's the best thing to do...
+        */
+       if ((s64)(p->dl.deadline - rq->curr->dl.deadline) == 0&&
+           !need_resched())

please recheck !need_resched(), say rq->curr need reschedule?

Sorry, I don't get this..

Quoted 7 lines
Quoted 7 lines
kernel/sched_dl.c +2 -0
+               check_preempt_equal_dl(rq, p);
+#endif /* CONFIG_SMP */
  }
 
  #ifdef CONFIG_SCHED_HRTICK
@@ -532,10 +876,20 @@ struct task_struct *pick_next_task_dl(struct rq *rq)
 
Quoted 11 lines

kernel/sched_dl.c struct task_struct *pick_next_task_dl(struct rq *rq) · line 886

Quoted 10 lines
kernel/sched_dl.c +5 -0
        p = dl_task_of(dl_se);
        p->se.exec_start = rq->clock;
+
+       /* Running task will never be pushed. */
+       if (p)
+               dequeue_pushable_dl_task(rq, p);
+
  #ifdef CONFIG_SCHED_HRTICK
        if (hrtick_enabled(rq))
                start_hrtick_dl(rq, p);

need to check p is valid?

I'll restructure pick_next_task_ as in sched_rt.

Quoted 9 lines
Quoted 9 lines
kernel/sched_dl.c +5 -0
  #endif
+
+#ifdef CONFIG_SMP
+       rq->post_schedule = has_pushable_dl_tasks(rq);
+#endif /* CONFIG_SMP */
+
        return p;
  }
 
Quoted 5 lines

kernel/sched_dl.c static void put_prev_task_dl(struct rq *rq, struct task_struct *p) · line 899

Quoted 4 lines
kernel/sched_dl.c +0 -0
@@ -543,6 +897,9 @@ static void put_prev_task_dl(struct rq *rq, struct task_struct *p)
  {
        update_curr_dl(rq);
        p->se.exec_start = 0;

why seset exec_start?

Good point, I'll probably remove this.

Quoted 28 lines
Quoted 28 lines
kernel/sched_dl.c +15 -3
+
+       if (on_dl_rq(&p->dl)&&  p->dl.nr_cpus_allowed>  1)
+               enqueue_pushable_dl_task(rq, p);
  }
 
  static void task_tick_dl(struct rq *rq, struct task_struct *p, int queued)
@@ -576,43 +933,410 @@ static void set_curr_task_dl(struct rq *rq)
        struct task_struct *p = rq->curr;
 
        p->se.exec_start = rq->clock;
+
+       /* You can't push away the running task */
+       dequeue_pushable_dl_task(rq, p);
  }
 
-static void switched_from_dl(struct rq *rq, struct task_struct *p)
+#ifdef CONFIG_SMP
+
+/* Only try algorithms three times */
+#define DL_MAX_TRIES 3
+
+static int pick_dl_task(struct rq *rq, struct task_struct *p, int cpu)
  {
-       if (hrtimer_active(&p->dl.dl_timer))
-               hrtimer_try_to_cancel(&p->dl.dl_timer);
+       if (!task_running(rq, p)&&
+           (cpu<  0 || cpumask_test_cpu(cpu,&p->cpus_allowed))&&
+           (p->dl.nr_cpus_allowed>  1))
Quoted 8 lines

kernel/sched_dl.c static void set_curr_task_dl(struct rq *rq) · line 953

Quoted 3 lines
kernel/sched_dl.c +3 -0
+               return 1;
+
+       return 0;
	if (task_running(rq, p))
		return 0;
	return cpumask_test_cpu(cpu,&p->cpus_allowed);
	that is all:)

We use this inside pull_dl_task. Since we are searching for a task to pull, you must be sure that the found task can actually migrate checking nr_cpus_allowed > 1.

Quoted 35 lines
Quoted 24 lines
kernel/sched_dl.c +21 -1
  }
 
-static void switched_to_dl(struct rq *rq, struct task_struct *p)
+/* Returns the second earliest -deadline task, NULL otherwise */
+static struct task_struct *pick_next_earliest_dl_task(struct rq *rq, int cpu)
+{
+       struct rb_node *next_node = rq->dl.rb_leftmost;
+       struct sched_dl_entity *dl_se;
+       struct task_struct *p = NULL;
+
+next_node:
+       next_node = rb_next(next_node);
+       if (next_node) {
+               dl_se = rb_entry(next_node, struct sched_dl_entity, rb_node);
+               p = dl_task_of(dl_se);
+
+               if (pick_dl_task(rq, p, cpu))
+                       return p;
+
+               goto next_node;
+       }
+
+       return NULL;
+}

kernel/sched_dl.c static void set_curr_task_dl(struct rq *rq) · line 986

Quoted 10 lines
kernel/sched_dl.c +9 -0
+
+static int latest_cpu_find(struct cpumask *span,
+                          struct task_struct *task,
+                          struct cpumask *later_mask)
  {
+       const struct sched_dl_entity *dl_se =&task->dl;
+       int cpu, found = -1, best = 0;
+       u64 max_dl = 0;
+
+       for_each_cpu(cpu, span) {
	for_each_cpu_and(cpu, span,&task->cpus_allowed) {
Quoted 7 lines

kernel/sched_dl.c static void set_curr_task_dl(struct rq *rq) · line 992

Quoted 6 lines
kernel/sched_dl.c +6 -0
+               struct rq *rq = cpu_rq(cpu);
+               struct dl_rq *dl_rq =&rq->dl;
+
+               if (cpumask_test_cpu(cpu,&task->cpus_allowed)&&
+                   (!dl_rq->dl_nr_running || dl_time_before(dl_se->deadline,
+                    dl_rq->earliest_dl.curr))) {

please use dl_entity_preempt()

Well, ok with this and above. Anyway this code is completely removed in 15/16.

Quoted 17 lines
Quoted 17 lines
kernel/sched_dl.c +17 -0
+                       if (later_mask)
+                               cpumask_set_cpu(cpu, later_mask);
+                       if (!best&&  !dl_rq->dl_nr_running) {
+                               best = 1;
+                               found = cpu;
+                       } else if (!best&&
+                                  dl_time_before(max_dl,
+                                                 dl_rq->earliest_dl.curr)) {
+                               max_dl = dl_rq->earliest_dl.curr;
+                               found = cpu;
+                       }
+               } else if (later_mask)
+                       cpumask_clear_cpu(cpu, later_mask);
+       }
+
+       return found;
+}
Quoted 9 lines

kernel/sched_dl.c static void set_curr_task_dl(struct rq *rq) · line 1016

Quoted 7 lines
kernel/sched_dl.c +7 -0
+
+static DEFINE_PER_CPU(cpumask_var_t, local_cpu_mask_dl);
+
+static int find_later_rq(struct task_struct *task)
+{
+       struct sched_domain *sd;
+       struct cpumask *later_mask = __get_cpu_var(local_cpu_mask_dl);

please check is local_cpu_mask_dl valid

Could you explain more why should I check for validity?

Quoted 511 lines
Quoted 511 lines
kernel/sched_dl.c +453 -17
+       int this_cpu = smp_processor_id();
+       int best_cpu, cpu = task_cpu(task);
+
+       if (task->dl.nr_cpus_allowed == 1)
+               return -1;
+
+       best_cpu = latest_cpu_find(task_rq(task)->rd->span, task, later_mask);
+       if (best_cpu == -1)
+               return -1;
+
        /*
-        * If p is throttled, don't consider the possibility
-        * of preempting rq->curr, the check will be done right
-        * after its runtime will get replenished.
+        * If we are here, some target has been found,
+        * the most suitable of which is cached in best_cpu.
+        * This is, among the runqueues where the current tasks
+        * have later deadlines than the task's one, the rq
+        * with the latest possible one.
+        *
+        * Now we check how well this matches with task's
+        * affinity and system topology.
+        *
+        * The last cpu where the task run is our first
+        * guess, since it is most likely cache-hot there.
         */
-       if (unlikely(p->dl.dl_throttled))
-               return;
+       if (cpumask_test_cpu(cpu, later_mask))
+               return cpu;
+       /*
+        * Check if this_cpu is to be skipped (i.e., it is
+        * not in the mask) or not.
+        */
+       if (!cpumask_test_cpu(this_cpu, later_mask))
+               this_cpu = -1;
+
+       rcu_read_lock();
+       for_each_domain(cpu, sd) {
+               if (sd->flags&  SD_WAKE_AFFINE) {
+
+                       /*
+                        * If possible, preempting this_cpu is
+                        * cheaper than migrating.
+                        */
+                       if (this_cpu != -1&&
+                           cpumask_test_cpu(this_cpu, sched_domain_span(sd)))
+                               return this_cpu;
+
+                       /*
+                        * Last chance: if best_cpu is valid and is
+                        * in the mask, that becomes our choice.
+                        */
+                       if (best_cpu<  nr_cpu_ids&&
+                           cpumask_test_cpu(best_cpu, sched_domain_span(sd)))
+                               return best_cpu;
+               }
+       }
+       rcu_read_unlock();
 
-       if (!p->on_rq || rq->curr != p) {
-               if (task_has_dl_policy(rq->curr))
-                       check_preempt_curr_dl(rq, p, 0);
-               else
-                       resched_task(rq->curr);
+       /*
+        * At this point, all our guesses failed, we just return
+        * 'something', and let the caller sort the things out.
+        */
+       if (this_cpu != -1)
+               return this_cpu;
+
+       cpu = cpumask_any(later_mask);
+       if (cpu<  nr_cpu_ids)
+               return cpu;
+
+       return -1;
+}
+
+/* Locks the rq it finds */
+static struct rq *find_lock_later_rq(struct task_struct *task, struct rq *rq)
+{
+       struct rq *later_rq = NULL;
+       int tries;
+       int cpu;
+
+       for (tries = 0; tries<  DL_MAX_TRIES; tries++) {
+               cpu = find_later_rq(task);
+
+               if ((cpu == -1) || (cpu == rq->cpu))
+                       break;
+
+               later_rq = cpu_rq(cpu);
+
+               /* Retry if something changed. */
+               if (double_lock_balance(rq, later_rq)) {
+                       if (unlikely(task_rq(task) != rq ||
+                                    !cpumask_test_cpu(later_rq->cpu,
+&task->cpus_allowed) ||
+                                    task_running(rq, task) ||
+                                    !task->se.on_rq)) {
+                               raw_spin_unlock(&later_rq->lock);
+                               later_rq = NULL;
+                               break;
+                       }
+               }
+
+               /*
+                * If the rq we found has no -deadline task, or
+                * its earliest one has a later deadline than our
+                * task, the rq is a good one.
+                */
+               if (!later_rq->dl.dl_nr_running ||
+                   dl_time_before(task->dl.deadline,
+                                  later_rq->dl.earliest_dl.curr))
+                       break;
+
+               /* Otherwise we try again. */
+               double_unlock_balance(rq, later_rq);
+               later_rq = NULL;
        }
+
+       return later_rq;
  }
 
-static void prio_changed_dl(struct rq *rq, struct task_struct *p,
-                           int oldprio)
+static struct task_struct *pick_next_pushable_dl_task(struct rq *rq)
  {
-       switched_to_dl(rq, p);
+       struct task_struct *p;
+
+       if (!has_pushable_dl_tasks(rq))
+               return NULL;
+
+       p = rb_entry(rq->dl.pushable_dl_tasks_leftmost,
+                    struct task_struct, pushable_dl_tasks);
+
+       BUG_ON(rq->cpu != task_cpu(p));
+       BUG_ON(task_current(rq, p));
+       BUG_ON(p->dl.nr_cpus_allowed<= 1);
+
+       BUG_ON(!p->se.on_rq);
+       BUG_ON(!dl_task(p));
+
+       return p;
  }
 
-#ifdef CONFIG_SMP
-static int
-select_task_rq_dl(struct task_struct *p, int sd_flag, int flags)
+/*
+ * See if the non running -deadline tasks on this rq
+ * can be sent to some other CPU where they can preempt
+ * and start executing.
+ */
+static int push_dl_task(struct rq *rq)
  {
-       return task_cpu(p);
+       struct task_struct *next_task;
+       struct rq *later_rq;
+
+       if (!rq->dl.overloaded)
+               return 0;
+
+       next_task = pick_next_pushable_dl_task(rq);
+       if (!next_task)
+               return 0;
+
+retry:
+       if (unlikely(next_task == rq->curr)) {
+               WARN_ON(1);
+               return 0;
+       }
+
+       /*
+        * If next_task preempts rq->curr, and rq->curr
+        * can move away, it makes sense to just reschedule
+        * without going further in pushing next_task.
+        */
+       if (dl_task(rq->curr)&&
+           dl_time_before(next_task->dl.deadline, rq->curr->dl.deadline)&&
+           rq->curr->dl.nr_cpus_allowed>  1) {
+               resched_task(rq->curr);
+               return 0;
+       }
+
+       /* We might release rq lock */
+       get_task_struct(next_task);
+
+       /* Will lock the rq it'll find */
+       later_rq = find_lock_later_rq(next_task, rq);
+       if (!later_rq) {
+               struct task_struct *task;
+
+               /*
+                * We must check all this again, since
+                * find_lock_later_rq releases rq->lock and it is
+                * then possible that next_task has migrated.
+                */
+               task = pick_next_pushable_dl_task(rq);
+               if (task_cpu(next_task) == rq->cpu&&  task == next_task) {
+                       /*
+                        * The task is still there. We don't try
+                        * again, some other cpu will pull it when ready.
+                        */
+                       dequeue_pushable_dl_task(rq, next_task);
+                       goto out;
+               }
+
+               if (!task)
+                       /* No more tasks */
+                       goto out;
+
+               put_task_struct(next_task);
+               next_task = task;
+               goto retry;
+       }
+
+       deactivate_task(rq, next_task, 0);
+       set_task_cpu(next_task, later_rq->cpu);
+       activate_task(later_rq, next_task, 0);
+
+       resched_task(later_rq->curr);
+
+       double_unlock_balance(rq, later_rq);
+
+out:
+       put_task_struct(next_task);
+
+       return 1;
+}
+
+static void push_dl_tasks(struct rq *rq)
+{
+       /* Terminates as it moves a -deadline task */
+       while (push_dl_task(rq))
+               ;
+}
+
+static int pull_dl_task(struct rq *this_rq)
+{
+       int this_cpu = this_rq->cpu, ret = 0, cpu;
+       struct task_struct *p;
+       struct rq *src_rq;
+       u64 dmin = LONG_MAX;
+
+       if (likely(!dl_overloaded(this_rq)))
+               return 0;
+
+       for_each_cpu(cpu, this_rq->rd->dlo_mask) {
+               if (this_cpu == cpu)
+                       continue;
+
+               src_rq = cpu_rq(cpu);
+
+               /*
+                * It looks racy, abd it is! However, as in sched_rt.c,
+                * we are fine with this.
+                */
+               if (this_rq->dl.dl_nr_running&&
+                   dl_time_before(this_rq->dl.earliest_dl.curr,
+                                  src_rq->dl.earliest_dl.next))
+                       continue;
+
+               /* Might drop this_rq->lock */
+               double_lock_balance(this_rq, src_rq);
+
+               /*
+                * If there are no more pullable tasks on the
+                * rq, we're done with it.
+                */
+               if (src_rq->dl.dl_nr_running<= 1)
+                       goto skip;
+
+               p = pick_next_earliest_dl_task(src_rq, this_cpu);
+
+               /*
+                * We found a task to be pulled if:
+                *  - it preempts our current (if there's one),
+                *  - it will preempt the last one we pulled (if any).
+                */
+               if (p&&  dl_time_before(p->dl.deadline, dmin)&&
+                   (!this_rq->dl.dl_nr_running ||
+                    dl_time_before(p->dl.deadline,
+                                   this_rq->dl.earliest_dl.curr))) {
+                       WARN_ON(p == src_rq->curr);
+                       WARN_ON(!p->se.on_rq);
+
+                       /*
+                        * Then we pull iff p has actually an earlier
+                        * deadline than the current task of its runqueue.
+                        */
+                       if (dl_time_before(p->dl.deadline,
+                                          src_rq->curr->dl.deadline))
+                               goto skip;
+
+                       ret = 1;
+
+                       deactivate_task(src_rq, p, 0);
+                       set_task_cpu(p, this_cpu);
+                       activate_task(this_rq, p, 0);
+                       dmin = p->dl.deadline;
+
+                       /* Is there any other task even earlier? */
+               }
+skip:
+               double_unlock_balance(this_rq, src_rq);
+       }
+
+       return ret;
+}
+
+static void pre_schedule_dl(struct rq *rq, struct task_struct *prev)
+{
+       /* Try to pull other tasks here */
+       if (dl_task(prev))
+               pull_dl_task(rq);
+}
+
+static void post_schedule_dl(struct rq *rq)
+{
+       push_dl_tasks(rq);
+}
+
+/*
+ * Since the task is not running and a reschedule is not going to happen
+ * anytime soon on its runqueue, we try pushing it away now.
+ */
+static void task_woken_dl(struct rq *rq, struct task_struct *p)
+{
+       if (!task_running(rq, p)&&
+           !test_tsk_need_resched(rq->curr)&&
+           has_pushable_dl_tasks(rq)&&
+           p->dl.nr_cpus_allowed>  1&&
+           dl_task(rq->curr)&&
+           (rq->curr->dl.nr_cpus_allowed<  2 ||
+            dl_entity_preempt(&rq->curr->dl,&p->dl))) {
+               push_dl_tasks(rq);
+       }
  }
 
  static void set_cpus_allowed_dl(struct task_struct *p,
@@ -622,10 +1346,145 @@ static void set_cpus_allowed_dl(struct task_struct *p,
 
        BUG_ON(!dl_task(p));
 
+       /*
+        * Update only if the task is actually running (i.e.,
+        * it is on the rq AND it is not throttled).
+        */
+       if (on_dl_rq(&p->dl)&&  (weight != p->dl.nr_cpus_allowed)) {
+               struct rq *rq = task_rq(p);
+
+               if (!task_current(rq, p)) {
+                       /*
+                        * If the task was on the pushable list,
+                        * make sure it stays there only if the new
+                        * mask allows that.
+                        */
+                       if (p->dl.nr_cpus_allowed>  1)
+                               dequeue_pushable_dl_task(rq, p);
+
+                       if (weight>  1)
+                               enqueue_pushable_dl_task(rq, p);
+               }
+
+               if ((p->dl.nr_cpus_allowed<= 1)&&  (weight>  1)) {
+                       rq->dl.dl_nr_migratory++;
+               } else if ((p->dl.nr_cpus_allowed>  1)&&  (weight<= 1)) {
+                       BUG_ON(!rq->dl.dl_nr_migratory);
+                       rq->dl.dl_nr_migratory--;
+               }
+
+               update_dl_migration(&rq->dl);
+       }
+
        cpumask_copy(&p->cpus_allowed, new_mask);
        p->dl.nr_cpus_allowed = weight;
  }
+
+/* Assumes rq->lock is held */
+static void rq_online_dl(struct rq *rq)
+{
+       if (rq->dl.overloaded)
+               dl_set_overload(rq);
+}
+
+/* Assumes rq->lock is held */
+static void rq_offline_dl(struct rq *rq)
+{
+       if (rq->dl.overloaded)
+               dl_clear_overload(rq);
+}
+
+static inline void init_sched_dl_class(void)
+{
+       unsigned int i;
+
+       for_each_possible_cpu(i)
+               zalloc_cpumask_var_node(&per_cpu(local_cpu_mask_dl, i),
+                                       GFP_KERNEL, cpu_to_node(i));
+}
+
+#endif /* CONFIG_SMP */
+
+static void switched_from_dl(struct rq *rq, struct task_struct *p)
+{
+       if (hrtimer_active(&p->dl.dl_timer)&&  !dl_policy(p->policy))
+               hrtimer_try_to_cancel(&p->dl.dl_timer);
+
+#ifdef CONFIG_SMP
+       /*
+        * Since this might be the only -deadline task on the rq,
+        * this is the right place to try to pull some other one
+        * from an overloaded cpu, if any.
+        */
+       if (!rq->dl.dl_nr_running)
+               pull_dl_task(rq);
  #endif
+}
+
+/*
+ * When switching to -deadline, we may overload the rq, then
+ * we try to push someone off, if possible.
+ */
+static void switched_to_dl(struct rq *rq, struct task_struct *p)
+{
+       int check_resched = 1;
+
+       /*
+        * If p is throttled, don't consider the possibility
+        * of preempting rq->curr, the check will be done right
+        * after its runtime will get replenished.
+        */
+       if (unlikely(p->dl.dl_throttled))
+               return;
+
+       if (!p->on_rq || rq->curr != p) {
+#ifdef CONFIG_SMP
+               if (rq->dl.overloaded&&  push_dl_task(rq)&&  rq != task_rq(p))
+                       /* Only reschedule if pushing failed */
+                       check_resched = 0;
+#endif /* CONFIG_SMP */
+               if (check_resched&&  task_has_dl_policy(rq->curr))
+                       check_preempt_curr_dl(rq, p, 0);
+       }
+}
+
+/*
+ * If the scheduling parameters of a -deadline task changed,
+ * a push or pull operation might be needed.
+ */
+static void prio_changed_dl(struct rq *rq, struct task_struct *p,
+                           int oldprio)
+{
+       if (p->on_rq || rq->curr == p) {
+#ifdef CONFIG_SMP
+               /*
+                * This might be too much, but unfortunately
+                * we don't have the old deadline value, and
+                * we can't argue if the task is increasing
+                * or lowering its prio, so...
+                */
+               if (!rq->dl.overloaded)
+                       pull_dl_task(rq);
+
+               /*
+                * If we now have a earlier deadline task than p,
+                * then reschedule, provided p is still on this
+                * runqueue.
+                */
+               if (dl_time_before(rq->dl.earliest_dl.curr, p->dl.deadline)&&
+                   rq->curr == p)
+                       resched_task(p);
+#else
+               /*
+                * Again, we don't know if p has a earlier
+                * or later deadline, so let's blindly set a
+                * (maybe not needed) rescheduling point.
+                */
+               resched_task(p);
+#endif /* CONFIG_SMP */
+       } else
+               switched_to_dl(rq, p);
+}
 
  static const struct sched_class dl_sched_class = {
        .next                   =&rt_sched_class,
@@ -642,6 +1501,11 @@ static const struct sched_class dl_sched_class = {
        .select_task_rq         = select_task_rq_dl,
 
        .set_cpus_allowed       = set_cpus_allowed_dl,
+       .rq_online              = rq_online_dl,
+       .rq_offline             = rq_offline_dl,
+       .pre_schedule           = pre_schedule_dl,
+       .post_schedule          = post_schedule_dl,
+       .task_woken             = task_woken_dl,
  #endif
 
        .set_curr_task          = set_curr_task_dl,
kernel/sched_rt.c +1 -1
index 4b09704..7b609bc 100644
@@ -1590,7 +1590,7 @@ static void task_woken_rt(struct rq *rq, struct task_struct *p)
            !test_tsk_need_resched(rq->curr)&&
            has_pushable_tasks(rq)&&
            p->rt.nr_cpus_allowed>  1&&
-           rt_task(rq->curr)&&
+           (dl_task(rq->curr) || rt_task(rq->curr))&&
Quoted 9 lines
Quoted 6 lines
kernel/sched_rt.c +0 -1
            (rq->curr->rt.nr_cpus_allowed<  2 ||
             rq->curr->prio<= p->prio))
                push_rt_tasks(rq);
--

1.7.5.4

Would you please mail me, in attachment, a monolithic patch of this work?

Ok, I'll prepare the monolithic patch and probably store it somewhere so that it can be downloaded also by others.

Thanks a lot for the immediate review :-)!

Best Regards,

- Juri

#66 lore raw

Re: [PATCH 06/16] sched: SCHED_DEADLINE push and pull logic

Quoted 3 lines · Juri Lelli
Quoted 3 lines
Quoted 3 lines

 kernel/sched_dl.c |  912
 kernel/sched_rt.c |    2 +-

You are working on 2.6.3x, x <= 8 ?
If so, what is the reason(just curious)?
Already planned to add in 3.3 and above?

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

+               if (!dl_entity_preempt(&entry->dl,&p->dl))

               if (dl_entity_preempt(&p->dl,&entry->dl))

Any specific reason to reverse the condition?

Just for easing readers.

Quoted 11 lines
Quoted 10 lines

kernel/sched_dl.c static void yield_task_dl(struct rq *rq) · line 760

Quoted 7 lines
kernel/sched_dl.c +7 -0
+select_task_rq_dl(struct task_struct *p, int sd_flag, int flags)
+{
+       struct task_struct *curr;
+       struct rq *rq;
+       int cpu;
+
+       if (sd_flag != SD_BALANCE_WAKE)

               why is task_cpu(p) not eligible?

Quoted 2 lines

Right, I'll change this.

No, you will first IMO sort out clear answer to the question.

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

+           (rq->curr->dl.nr_cpus_allowed<  2 ||
+            dl_entity_preempt(&rq->curr->dl,&p->dl))&&

               !dl_entity_preempt(&p->dl,&rq->curr->dl))&&

As above?

Just for easing reader.

Quoted 8 lines
Quoted 8 lines
Quoted 7 lines

+#ifdef CONFIG_SMP
+       /*
+        * In the unlikely case current and p have the same deadline
+        * let us try to decide what's the best thing to do...
+        */
+       if ((s64)(p->dl.deadline - rq->curr->dl.deadline) == 0&&
+           !need_resched())

Quoted 4 lines
Quoted 1 line

please recheck !need_resched(), say rq->curr need reschedule?

Sorry, I don't get this..

Perhaps smp_processor_id() != rq->cpu

Quoted 9 lines
Quoted 4 lines

       if (task_running(rq, p))
               return 0;
       return cpumask_test_cpu(cpu, &p->cpus_allowed);

We use this inside pull_dl_task. Since we are searching for a task to pull, you must be sure that the found task can actually migrate checking nr_cpus_allowed > 1.

If cpu is certainly allowed for task to run, but nr_cpus_allowed is no more than one, which is corrupted?

Quoted 4 lines

Well, ok with this and above. Anyway this code is completely removed in 15/16.

Yup, another reason for monolith.

Quoted 11 lines
Quoted 10 lines

kernel/sched_dl.c static void set_curr_task_dl(struct rq *rq) · line 1016

Quoted 7 lines
kernel/sched_dl.c +7 -0
+
+static DEFINE_PER_CPU(cpumask_var_t, local_cpu_mask_dl);
+
+static int find_later_rq(struct task_struct *task)
+{
+       struct sched_domain *sd;
+       struct cpumask *later_mask = __get_cpu_var(local_cpu_mask_dl);

       please check is local_cpu_mask_dl valid

Quoted 2 lines

Could you explain more why should I check for validity?

Only for the case that something comes in before it is initialized, IIRC encountered by Steven.

Quoted 4 lines

Ok, I'll prepare the monolithic patch and probably store it somewhere so that it can be downloaded also by others.

Info Hillf once it is ready, thanks.

Good Weekend
-hd

#67 lore raw

Re: [PATCH 06/16] sched: SCHED_DEADLINE push and pull logic

Quoted 7 lines · Hillf Danton
Quoted 3 lines · Juri Lelli
Quoted 3 lines
Quoted 3 lines
 kernel/sched_dl.c |  912
 kernel/sched_rt.c |    2 +-

You are working on 2.6.3x, x <= 8 ?

Not really. Why saying so? :-O

Quoted 3 lines

If so, what is the reason(just curious)?
Already planned to add in 3.3 and above?

The n.0 mail of this series "[RFC][PATCH 00/16] sched: SCHED_DEADLINE v4" says:
<< - this time we sit on top of PREEMPT_RT (3.2.13-rt23); we continue
to aim at mainline inclusion, but we also see -rt folks as immediate and interested users.

...

As said, patchset is on top of PREEMPT_RT (as of today). However, Insop Song (from Ericsson) is maintaining a parallel branch for the current tip/master (https://github.com/insop/sched-deadline2).>>

Thanks and Regards,
Dario

Signature · 4 lines
<<This happens because I choose it to happen!>> (Raistlin Majere)
-----------------------------------------------------------------
Dario Faggioli, Ph.D, http://retis.sssup.it/people/faggioli
Senior Software Engineer, Citrix Systems R&D Ltd., Cambridge (UK)
author
#127 lore raw

SCHED_DEADLINE v4

In reply to Juri Lelli

Hello everyone,

This is to inform you that a new version of the SCHED_DEADLINE patchset is out (v4). What follows is an excerpt of the overview (00/16) messsage sent on LKML, you can find the thread here:
https://lkml.org/lkml/2012/4/6/39

[...]
Just to recap, the patchset introduces a new deadline based real-time task scheduling policy --called SCHED_DEADLINE-- with bandwidth isolation (aka "resource reservation") capabilities. It now supports global/clustered multiprocessor scheduling through dynamic task migrations.

From the previous releases[1]:
- all the comments and the fixes coming from the reviews we got have

    been considered and applied;

- better handling of rq selection for dynamic task migration, by means of a cpupri equivalent for -deadline tasks (cpudl). The mechanism is simple and straightforward, but showed nice performance figures[2].
- this time we sit on top of PREEMPT_RT (3.2.13-rt23); we continue to aim at mainline inclusion, but we also see -rt folks as immediate and interested users.

Still missing/incomplete:
- (c)group based bandwidth management, and maybe scheduling. It seems some more discussion on what precisely we want is *really* needed for this point;
- bandwidth inheritance (to replace deadline/priority inheritance). What's in the patchset is just very few more than a simple placeholder. More discussion on the right way to go is needed here. Some work has already been done, but it is still not ready for submission.

The development is taking place at:
https://github.com/jlelli/sched-deadline

Check the repositories frequently if you're interested, and feel free to e-mail me for any issue you run into.

Furthermore, we developed an application that you can use to test this patchset:
https://github.com/gbagnoli/rt-app

We also set up a development mailing list: linux-dl[3]. You can subscribe from here:
http://feanor.sssup.it/mailman/listinfo/linux-dl
or via e-mail (send a message to [email protected] with just the word `help' as subject or in the body to receive info).

As already discussed we are planning to merge this work with the EDF throttling patches [https://lkml.org/lkml/2010/2/23/239] but we still are in the preliminary phases of the merge and we intend to use the feedback to this post to help us decide on the direction it should take.

As said, patchset is on top of PREEMPT_RT (as of today). However, Insop Song (from Ericsson) is maintaining a parallel branch for the current tip/master (https://github.com/insop/sched-deadline2). Ericsson is in fact evaluating the use of SCHED_DEADLINE for CPE (Customer Premise Equipment) devices in order to reserve CPU bandwidth to processes.

The code was being jointly developed by ReTiS Lab (http://retis.sssup.it)
and Evidence S.r.l (http://www.evidence.eu.com) in the context of the ACTORS EU-funded project (http://www.actors-project.eu). It is now also supported by the S(o)OS EU-funded project (http://www.soos-project.eu/).
It has also some users, both in academic and applied research. Even if our last release dates back almost a year we continued to get feedback from Ericsson (see above), Wind River, Porto (ISEP), Trento and Malardalen universities :-).

As usual, any kind of feedback is welcome and appreciated.

Thanks in advice and regards,

- Juri

[1] http://lwn.net/Articles/376502, http://lwn.net/Articles/353797,

    http://lwn.net/Articles/412410

[2] http://retis.sssup.it/~jlelli/papers/Ospert11Lelli.pdf
[3] from the first linux-dl message:
-linux-dl should become the place where discussions about real-time
deadline scheduling on Linux take place (not only SCHED_DEADLINE). We felt the lack of a place where we can keep in touch with each other; we are all working on the same things, but probably from different viewpoints, and this is surely a point of strength. Anyway, our efforts need to be organized in some way, or at least it is important to know on what everyone is currently working as to not end up with "duplicate-efforts".-

Dario Faggioli (10):
sched: add extended scheduling interface.
sched: SCHED_DEADLINE data structures.
sched: SCHED_DEADLINE policy implementation.
sched: SCHED_DEADLINE avg_update accounting.
sched: add schedstats for -deadline tasks.
sched: add resource limits for -deadline tasks.
sched: add latency tracing for -deadline tasks.
sched: drafted deadline inheritance logic.
sched: add bandwidth management for sched_dl.
sched: add sched_dl documentation.

Juri Lelli (3):
sched: SCHED_DEADLINE SMP-related data structures.
sched: SCHED_DEADLINE push and pull logic
sched: speed up -dl pushes with a push-heap.

Harald Gustafsson (1):
sched: add period support for -deadline tasks.

Peter Zijlstra (1):
rtmutex: turn the plist into an rb-tree.
[...]

#125 lore raw
In reply to Luca Abeni

Hi everyone!

On Fri, 2012-04-06 at 10:25 +0200, Luca Abeni wrote:

[...]

Quoted 5 lines

About BWI... A student of mine (added in cc) implemented a prototypal bandwidth inheritance (based on an old version of SCHED_DEADLINE). It is here:
https://github.com/eus/cbs_inheritance
(Tadeus, please correct me if I pointed to the wrong repository).

The repository is correct.

Quoted 7 lines

It is not for inclusion yet (it is based on an old version, it is UP only, and it probably needs some cleanups), but it worked fine in our tests. Note that in this patch the BWI mechanism is not bound to rtmutexes, but inheritance is controlled through 2 syscalls (because we used BWI for client/server interactions).
Anyway, I hope that the BWI code developed by Tadeus can be useful (or can be directly re-used) for implementing BWI in SCHED_DEADLINE.

For inclusion, I think the interaction between the two system calls and RT-mutex subsystem needs some thoughts especially when multi-cores are involved. I haven't given further thought on this matter upon the completion of my thesis work, especially in taking into account Dario's M-BWI approach.

For UP, the BWI syscalls implementation in the aforementioned branch does not have Dario's BWI implementation for mutexes. And interaction between the BWI syscalls and RT-Mutex has not been taken into account.

Happy Easter to all of you who celebrate it!

Quoted 1 line

Luca

Signature · 2 lines
Sincerely yours,
Tadeus Prastowo
#2 lore raw

Re: [PATCH 01/16] sched: add sched_class->task_dead.

In reply to Juri Lelli

kernel/sched.c static void finish_task_switch(struct rq *rq, struct task_struct *prev) · line 3224

Quoted 10 lines · Juri Lelli
kernel/sched.c +3 -0
@@ -3219,6 +3219,9 @@ static void finish_task_switch(struct rq *rq, struct task_struct *prev)
 	if (mm)
 		mmdrop_delayed(mm);
 	if (unlikely(prev_state == TASK_DEAD)) {
+		if (prev->sched_class->task_dead)
+			prev->sched_class->task_dead(prev);
+

And 5/16 adds

+static void task_dead_dl(struct task_struct *p) +{
+ struct hrtimer *timer = &p->dl.dl_timer;
+
+ if (hrtimer_active(timer))
+ hrtimer_try_to_cancel(timer);
+}

This looks suspicious. finish_task_switch() does put_task_struct() after that, it is quite possible this actually frees the memory.

What if hrtimer_try_to_cancel() fails because the timer is running? In this case __run_hrtimer() can play with the freed timer. Say, to clear HRTIMER_STATE_CALLBACK. Not to mention dl_task_timer() itself.

Oleg.

author
#3 lore raw

Re: [PATCH 01/16] sched: add sched_class->task_dead.

Quoted 22 lines · Oleg Nesterov

kernel/sched.c static void finish_task_switch(struct rq *rq, struct task_struct *prev) · line 3224

Quoted 10 lines · Juri Lelli
kernel/sched.c +3 -0
@@ -3219,6 +3219,9 @@ static void finish_task_switch(struct rq *rq, struct task_struct *prev)
  	if (mm)
  		mmdrop_delayed(mm);
  	if (unlikely(prev_state == TASK_DEAD)) {
+		if (prev->sched_class->task_dead)
+			prev->sched_class->task_dead(prev);
+

And 5/16 adds

+static void task_dead_dl(struct task_struct *p) +{
+ struct hrtimer *timer =&p->dl.dl_timer;
+
+ if (hrtimer_active(timer))
+ hrtimer_try_to_cancel(timer);
+}

Quoted 9 lines

This looks suspicious. finish_task_switch() does put_task_struct() after that, it is quite possible this actually frees the memory.

What if hrtimer_try_to_cancel() fails because the timer is running? In this case __run_hrtimer() can play with the freed timer. Say, to clear HRTIMER_STATE_CALLBACK. Not to mention dl_task_timer() itself.

Oleg.

Right, hrtimer_cancel(timer) looks way better.

Thanks!

- Juri

author
#68 lore raw

Re: [PATCH 06/16] sched: SCHED_DEADLINE push and pull logic

In reply to Hillf Danton
Quoted 9 lines · Hillf Danton
Quoted 3 lines · Juri Lelli
Quoted 3 lines
Quoted 3 lines
  kernel/sched_dl.c |  912
  kernel/sched_rt.c |    2 +-

You are working on 2.6.3x, x<= 8 ?
If so, what is the reason(just curious)?
Already planned to add in 3.3 and above?

Dario answered on this :-).

Quoted 9 lines
Quoted 7 lines
Quoted 4 lines
Quoted 1 line

+ if (!dl_entity_preempt(&entry->dl,&p->dl))

                if (dl_entity_preempt(&p->dl,&entry->dl))

Any specific reason to reverse the condition?

Just for easing readers.

Ok, reasonable. Here and below.

Quoted 11 lines
Quoted 11 lines
Quoted 10 lines

kernel/sched_dl.c static void yield_task_dl(struct rq *rq) · line 760

Quoted 7 lines
kernel/sched_dl.c +7 -0
+select_task_rq_dl(struct task_struct *p, int sd_flag, int flags)
+{
+       struct task_struct *curr;
+       struct rq *rq;
+       int cpu;
+
+       if (sd_flag != SD_BALANCE_WAKE)

why is task_cpu(p) not eligible?

Quoted 4 lines
Quoted 2 lines

Right, I'll change this.

No, you will first IMO sort out clear answer to the question.

task_cpu(p) is eligible and will be returned if sd_flag != SD_BALANCE_WAKE && sd_flag != SD_BALANCE_FORK as in sched_rt. I changed the code accordingly.

Quoted 17 lines
Quoted 7 lines
Quoted 4 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+           (rq->curr->dl.nr_cpus_allowed<    2 ||
+            dl_entity_preempt(&rq->curr->dl,&p->dl))&&
                !dl_entity_preempt(&p->dl,&rq->curr->dl))&&

As above?

Just for easing reader.

Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
kernel/sched_dl.c +7 -0
+#ifdef CONFIG_SMP
+       /*
+        * In the unlikely case current and p have the same deadline
+        * let us try to decide what's the best thing to do...
+        */
+       if ((s64)(p->dl.deadline - rq->curr->dl.deadline) == 0&&
+           !need_resched())
Quoted 6 lines
Quoted 4 lines
Quoted 1 line

please recheck !need_resched(), say rq->curr need reschedule?

Sorry, I don't get this..

Perhaps smp_processor_id() != rq->cpu

need_resched is actually checked...

Quoted 9 lines
Quoted 9 lines
Quoted 4 lines
        if (task_running(rq, p))
                return 0;
        return cpumask_test_cpu(cpu,&p->cpus_allowed);

We use this inside pull_dl_task. Since we are searching for a task to pull, you must be sure that the found task can actually migrate checking nr_cpus_allowed> 1.

Quoted 9 lines

If cpu is certainly allowed for task to run, but nr_cpus_allowed is no more than one, which is corrupted?

Quoted 4 lines

Well, ok with this and above. Anyway this code is completely removed in 15/16.

Yup, another reason for monolith.

Monolithic is below. Anyway, please check the github repo for bug fixes/new features. ;-)

Quoted 11 lines
Quoted 11 lines
Quoted 10 lines

kernel/sched_dl.c static void set_curr_task_dl(struct rq *rq) · line 1016

Quoted 7 lines
kernel/sched_dl.c +7 -0
+
+static DEFINE_PER_CPU(cpumask_var_t, local_cpu_mask_dl);
+
+static int find_later_rq(struct task_struct *task)
+{
+       struct sched_domain *sd;
+       struct cpumask *later_mask = __get_cpu_var(local_cpu_mask_dl);

please check is local_cpu_mask_dl valid

Quoted 5 lines
Quoted 2 lines

Could you explain more why should I check for validity?

Only for the case that something comes in before it is initialized, IIRC encountered by Steven.

Do you mean at kernel_init time?
Could you be more precise about the problem Steven encountered?

Quoted 6 lines
Quoted 4 lines

Ok, I'll prepare the monolithic patch and probably store it somewhere so that it can be downloaded also by others.

Info Hillf once it is ready, thanks.

Here we go:
https://github.com/downloads/jlelli/sched-deadline/sched-dl-V4.patch

I noticed that the Cc list is changed... something went wrong? Anyway, I restored it to the original one. :-)

Thanks and Regards,

- Juri

#69 lore raw

Re: [PATCH 06/16] sched: SCHED_DEADLINE push and pull logic

Quoted 4 lines · Juri Lelli

Do you mean at kernel_init time?
Could you be more precise about the problem Steven encountered?

After change from kernel/sched_rt.c to kernel/sched/rt.c, I could not find the git history of that fix, related to RCU IIRC, by Steven in mainline. Steven, please give a link.

Quoted 4 lines

Here we go:
https://github.com/downloads/jlelli/sched-deadline/sched-dl-V4.patch

Thanks:)

Quoted 2 lines

I noticed that the Cc list is changed... something went wrong?

I forget what was changed, simply because the mail agent beeps that one mail address is not reachable.

-hd

author
#70 lore raw

Re: [PATCH 06/16] sched: SCHED_DEADLINE push and pull logic

Quoted 9 lines · Hillf Danton
Quoted 4 lines · Juri Lelli

Do you mean at kernel_init time?
Could you be more precise about the problem Steven encountered?

After change from kernel/sched_rt.c to kernel/sched/rt.c, I could not find the git history of that fix, related to RCU IIRC, by Steven in mainline. Steven, please give a link.

Ok, found!
https://lkml.org/lkml/2011/6/14/366

Quoted 4 lines
Quoted 4 lines

Here we go:
https://github.com/downloads/jlelli/sched-deadline/sched-dl-V4.patch

Quoted 8 lines

Thanks:)

Quoted 2 lines

I noticed that the Cc list is changed... something went wrong?

I forget what was changed, simply because the mail agent beeps that one mail address is not reachable.

-hd

Thanks and Regards,

- Juri

maintainer
#22 lore raw

Re: [PATCH 05/16] sched: SCHED_DEADLINE policy implementation.

In reply to Juri Lelli
Quoted 8 lines · Juri Lelli

+/*
+ * Pure Earliest Deadline First (EDF) scheduling does not deal with the
+ * possibility of a entity lasting more than what it declared, and thus
+ * exhausting its runtime.
+ *
+ * Here we are interested in making runtime overrun possible, but we do
+ * not want a entity which is misbehaving to affect the scheduling of all
+ * other entities.

Quoted 8 lines

+ * Therefore, a budgeting strategy called Constant Bandwidth Server (CBS)
+ * is used, in order to confine each entity within its own bandwidth.
+ *
+ * This function deals exactly with that, and ensures that when the runtime
+ * of a entity is replenished, its deadline is also postponed. That ensures
+ * the overrunning entity can't interfere with other entity in the system and
+ * can't make them miss their deadlines. Reasons why this kind of overruns
+ * could happen are, typically, a entity voluntarily trying to overcume its

s/overcume/overcome/

-- Steve

Quoted 5 lines

+ * runtime, or it just underestimated it during sched_setscheduler_ex().
+ */

kernel/sched_dl.c new file +3 -0
+static void replenish_dl_entity(struct sched_dl_entity *dl_se)
+{
+
author
#23 lore raw

Re: [PATCH 05/16] sched: SCHED_DEADLINE policy implementation.

Quoted 21 lines · Steven Rostedt
Quoted 8 lines · Juri Lelli

+/*
+ * Pure Earliest Deadline First (EDF) scheduling does not deal with the
+ * possibility of a entity lasting more than what it declared, and thus
+ * exhausting its runtime.
+ *
+ * Here we are interested in making runtime overrun possible, but we do
+ * not want a entity which is misbehaving to affect the scheduling of all
+ * other entities.

Quoted 8 lines

+ * Therefore, a budgeting strategy called Constant Bandwidth Server (CBS)
+ * is used, in order to confine each entity within its own bandwidth.
+ *
+ * This function deals exactly with that, and ensures that when the runtime
+ * of a entity is replenished, its deadline is also postponed. That ensures
+ * the overrunning entity can't interfere with other entity in the system and
+ * can't make them miss their deadlines. Reasons why this kind of overruns
+ * could happen are, typically, a entity voluntarily trying to overcume its

s/overcume/overcome/

Quoted 8 lines

-- Steve

kernel/sched_dl.c line 104

Quoted 5 lines

+ * runtime, or it just underestimated it during sched_setscheduler_ex().
+ */

kernel/sched_dl.c new file +3 -0
+static void replenish_dl_entity(struct sched_dl_entity *dl_se)
+{
+

Thanks!

- Juri

maintainer
#24 lore raw

Re: [PATCH 05/16] sched: SCHED_DEADLINE policy implementation.

In reply to Juri Lelli

kernel/sched_dl.c line 106

Quoted 7 lines · Juri Lelli
kernel/sched_dl.c new file +7 -0
+static void replenish_dl_entity(struct sched_dl_entity *dl_se)
+{
+	struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
+	struct rq *rq = rq_of_dl_rq(dl_rq);
+
+	/*
+	 * We Keep moving the deadline away until we get some

s/Keep/keep/

Quoted 15 lines
kernel/sched_dl.c new file +15 -0
+	 * available runtime for the entity. This ensures correct
+	 * handling of situations where the runtime overrun is
+	 * arbitrary large.
+	 */
+	while (dl_se->runtime <= 0) {
+		dl_se->deadline += dl_se->dl_deadline;
+		dl_se->runtime += dl_se->dl_runtime;
+	}
+
+	/*
+	 * At this point, the deadline really should be "in
+	 * the future" with respect to rq->clock. If it's
+	 * not, we are, for some reason, lagging too much!
+	 * Anyway, after having warn userspace abut that,
+	 * we still try to keep the things running by

kernel/sched_dl.c line 131

Quoted 10 lines
kernel/sched_dl.c new file +10 -0
+	 * resetting the deadline and the budget of the
+	 * entity.
+	 */
+	if (dl_time_before(dl_se->deadline, rq->clock)) {
+		WARN_ON_ONCE(1);
+		dl_se->deadline = rq->clock + dl_se->dl_deadline;
+		dl_se->runtime = dl_se->dl_runtime;
+	}
+}
+

I just finished reviewing patches 1-5, and have yet to find anything wrong with them (except for these typos). I'll continue my review, and then I'll start testing them.

Good work (so far ;-)

-- Steve

author
#25 lore raw

Re: [PATCH 05/16] sched: SCHED_DEADLINE policy implementation.

Quoted 42 lines · Steven Rostedt

kernel/sched_dl.c line 106

Quoted 7 lines · Juri Lelli
kernel/sched_dl.c new file +7 -0
+static void replenish_dl_entity(struct sched_dl_entity *dl_se)
+{
+	struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
+	struct rq *rq = rq_of_dl_rq(dl_rq);
+
+	/*
+	 * We Keep moving the deadline away until we get some

s/Keep/keep/

Quoted 15 lines
kernel/sched_dl.c new file +15 -0
+	 * available runtime for the entity. This ensures correct
+	 * handling of situations where the runtime overrun is
+	 * arbitrary large.
+	 */
+	while (dl_se->runtime<= 0) {
+		dl_se->deadline += dl_se->dl_deadline;
+		dl_se->runtime += dl_se->dl_runtime;
+	}
+
+	/*
+	 * At this point, the deadline really should be "in
+	 * the future" with respect to rq->clock. If it's
+	 * not, we are, for some reason, lagging too much!
+	 * Anyway, after having warn userspace abut that,
+	 * we still try to keep the things running by

kernel/sched_dl.c line 131

Quoted 10 lines
kernel/sched_dl.c new file +10 -0
+	 * resetting the deadline and the budget of the
+	 * entity.
+	 */
+	if (dl_time_before(dl_se->deadline, rq->clock)) {
+		WARN_ON_ONCE(1);
+		dl_se->deadline = rq->clock + dl_se->dl_deadline;
+		dl_se->runtime = dl_se->dl_runtime;
+	}
+}
+

I just finished reviewing patches 1-5, and have yet to find anything wrong with them (except for these typos). I'll continue my review, and then I'll start testing them.

Quoted 5 lines

Good work (so far ;-)

-- Steve

Well, I tried my best to not spoil to much the work made by Dario & Co. :-).

Anyway, thanks!

- Juri

maintainer
#74 lore raw

Re: [PATCH 06/16] sched: SCHED_DEADLINE push and pull logic

In reply to Hillf Danton
Quoted 5 lines · Hillf Danton
Quoted 5 lines
kernel/sched_dl.c +5 -0
+static void inc_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
+{
+       dl_rq = &rq_of_dl_rq(dl_rq)->dl;
+
+       dl_rq->dl_nr_total++;
Quoted 8 lines

kernel/sched_dl.c static inline int is_leftmost(struct task_struct *p, struct dl_rq *dl_rq) · line 114

Quoted 4 lines
kernel/sched_dl.c +4 -0
+       if (dl_se->nr_cpus_allowed > 1)
+               dl_rq->dl_nr_migratory++;
+
+       update_dl_migration(dl_rq);
	if (dl_se->nr_cpus_allowed > 1) {
		dl_rq->dl_nr_migratory++;
		/* No change in migratory, no update of migration */

This is not true. As dl_nr_total changed. If there was only one dl task queued that can migrate, and then another dl task is queued but this task can not migrate, the update_dl_migration still needs to be called. As dl_nr_migratory would be 1, but now dl_nr_total > 1. This means we are now overloaded.

Quoted 16 lines
		update_dl_migration(dl_rq);
	}

kernel/sched_dl.c static inline int is_leftmost(struct task_struct *p, struct dl_rq *dl_rq) · line 125

Quoted 11 lines
kernel/sched_dl.c +11 -0
+}
+
+static void dec_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
+{
+       dl_rq = &rq_of_dl_rq(dl_rq)->dl;
+
+       dl_rq->dl_nr_total--;
+       if (dl_se->nr_cpus_allowed > 1)
+               dl_rq->dl_nr_migratory--;
+
+       update_dl_migration(dl_rq);

ditto

ditto.

Quoted 6 lines

kernel/sched_dl.c static inline int is_leftmost(struct task_struct *p, struct dl_rq *dl_rq) · line 130

Quoted 6 lines
kernel/sched_dl.c +5 -0
+}
+
+/*
+ * The list of pushable -deadline task is not a plist, like in
+ * sched_rt.c, it is an rb-tree with tasks ordered by deadline.

-- Steve

author
#129 lore raw
Quoted 11 lines · Steven Rostedt
Quoted 6 lines · Juri Lelli

The development is taking place at:
https://github.com/jlelli/sched-deadline

Check the repositories frequently if you're interested, and feel free to e-mail me for any issue you run into.

BTW, which branch should I be looking at?

* sched-dl-V4 contains the patchset as posted on LKML
* 3.2.13-rt23-dl is the "development" one with commits not merged in bigger patches

Currently, they are equivalent for testing purposes.

I most probably have to simplify and clean the whole repo..

Thanks and Regards,

- Juri

maintainer
#71 lore raw

Re: [PATCH 06/16] sched: SCHED_DEADLINE push and pull logic

In reply to Juri Lelli
Quoted 6 lines · Juri Lelli
Quoted 5 lines · Hillf Danton
Quoted 4 lines · Juri Lelli

Do you mean at kernel_init time?
Could you be more precise about the problem Steven encountered?

Quoted 8 lines
Quoted 4 lines

After change from kernel/sched_rt.c to kernel/sched/rt.c, I could not find the git history of that fix, related to RCU IIRC, by Steven in mainline. Steven, please give a link.

Ok, found!
https://lkml.org/lkml/2011/6/14/366

I doubt you'll hit this same bug, but it doesn't hurt to add it. If a boot time kernel thread starts using edf, then it would be required. And you never know who may do that ;-)

-- Steve

maintainer
#72 lore raw

Re: [PATCH 06/16] sched: SCHED_DEADLINE push and pull logic

In reply to Juri Lelli
Quoted 9 lines · Juri Lelli
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
kernel/sched_dl.c +7 -0
+#ifdef CONFIG_SMP
+       /*
+        * In the unlikely case current and p have the same deadline
+        * let us try to decide what's the best thing to do...
+        */
+       if ((s64)(p->dl.deadline - rq->curr->dl.deadline) == 0&&
+           !need_resched())
Quoted 9 lines
Quoted 6 lines
Quoted 4 lines
Quoted 1 line

please recheck !need_resched(), say rq->curr need reschedule?

Sorry, I don't get this..

Perhaps smp_processor_id() != rq->cpu

need_resched is actually checked...

I guess what Hillf is trying to say is,

s/!need_resched()/!test_tsk_need_resched(rq->curr)/

-- Steve

maintainer
#80 lore raw

Re: [PATCH 06/16] sched: SCHED_DEADLINE push and pull logic

In reply to Juri Lelli

kernel/sched_dl.c static void yield_task_dl(struct rq *rq) · line 795

Quoted 4 lines · Juri Lelli
kernel/sched_dl.c +4 -0
+static void check_preempt_equal_dl(struct rq *rq, struct task_struct *p)
+{
+	/*
+	 * Current can't be migrated, useles to reschedule,

s/useles/useless/

I feel so useles by only adding typo fixes ;-)

-- Steve

Quoted 19 lines
kernel/sched_dl.c +19 -0
+	 * let's hope p can move out.
+	 */
+	if (rq->curr->dl.nr_cpus_allowed == 1 ||
+	    latest_cpu_find(rq->rd->span, rq->curr, NULL) == -1)
+		return;
+
+	/*
+	 * p is migratable, so let's not schedule it and
+	 * see if it is pushed or pulled somewhere else.
+	 */
+	if (p->dl.nr_cpus_allowed != 1 &&
+	    latest_cpu_find(rq->rd->span, p, NULL) != -1)
+		return;
+
+	resched_task(rq->curr);
+}
+
+#endif /* CONFIG_SMP */
+
author
#73 lore raw

Re: [PATCH 06/16] sched: SCHED_DEADLINE push and pull logic

In reply to Steven Rostedt
Quoted 17 lines · Steven Rostedt
Quoted 16 lines · Juri Lelli
Quoted 15 lines
Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
kernel/sched_dl.c +7 -0
+#ifdef CONFIG_SMP
+       /*
+        * In the unlikely case current and p have the same deadline
+        * let us try to decide what's the best thing to do...
+        */
+       if ((s64)(p->dl.deadline - rq->curr->dl.deadline) == 0&&
+           !need_resched())
Quoted 4 lines
Quoted 1 line

please recheck !need_resched(), say rq->curr need reschedule?

Sorry, I don't get this..

Perhaps smp_processor_id() != rq->cpu

Quoted 7 lines
Quoted 2 lines

need_resched is actually checked...

I guess what Hillf is trying to say is,

s/!need_resched()/!test_tsk_need_resched(rq->curr)/

Yep, I finally got (and changed) it ;-).

Thanks,

- Juri

author
#81 lore raw

Re: [PATCH 06/16] sched: SCHED_DEADLINE push and pull logic

In reply to Steven Rostedt
Quoted 11 lines · Steven Rostedt

kernel/sched_dl.c static void yield_task_dl(struct rq *rq) · line 795

Quoted 4 lines · Juri Lelli
kernel/sched_dl.c +4 -0
+static void check_preempt_equal_dl(struct rq *rq, struct task_struct *p)
+{
+	/*
+	 * Current can't be migrated, useles to reschedule,

s/useles/useless/

I feel so useles by only adding typo fixes ;-)

Don't know why.. but this instead sounds good to me :-P.

Quoted 11 lines
Quoted 11 lines
kernel/sched_dl.c +11 -0
+	 * let's hope p can move out.
+	 */
+	if (rq->curr->dl.nr_cpus_allowed == 1 ||
+	    latest_cpu_find(rq->rd->span, rq->curr, NULL) == -1)
+		return;
+
+	/*
+	 * p is migratable, so let's not schedule it and
+	 * see if it is pushed or pulled somewhere else.
+	 */
+	if (p->dl.nr_cpus_allowed != 1&&
Quoted 9 lines

kernel/sched_dl.c static void yield_task_dl(struct rq *rq) · line 814

Quoted 8 lines
kernel/sched_dl.c +8 -0
+	    latest_cpu_find(rq->rd->span, p, NULL) != -1)
+		return;
+
+	resched_task(rq->curr);
+}
+
+#endif /* CONFIG_SMP */
+

Thanks,

- Juri

maintainer
#82 lore raw

Re: [PATCH 06/16] sched: SCHED_DEADLINE push and pull logic

In reply to Juri Lelli
Quoted 8 lines · Juri Lelli
kernel/sched_dl.c +7 -0
+static int latest_cpu_find(struct cpumask *span,
+			   struct task_struct *task,
+			   struct cpumask *later_mask)
 {
+	const struct sched_dl_entity *dl_se = &task->dl;
+	int cpu, found = -1, best = 0;
+	u64 max_dl = 0;
+

kernel/sched_dl.c static void set_curr_task_dl(struct rq *rq) · line 995

Quoted 10 lines
kernel/sched_dl.c +10 -0
+	for_each_cpu(cpu, span) {
+		struct rq *rq = cpu_rq(cpu);
+		struct dl_rq *dl_rq = &rq->dl;
+
+		if (cpumask_test_cpu(cpu, &task->cpus_allowed) &&
+		    (!dl_rq->dl_nr_running || dl_time_before(dl_se->deadline,
+		     dl_rq->earliest_dl.curr))) {
+			if (later_mask)
+				cpumask_set_cpu(cpu, later_mask);
+			if (!best && !dl_rq->dl_nr_running) {

I hate to say this (and I also have yet to look at the patches after this) but we should really take into account the RT tasks. It would suck to preempt a normal RT task when a non RT task is running on another CPU.

-- Steve

Quoted 14 lines
kernel/sched_dl.c +14 -0
+				best = 1;
+				found = cpu;
+			} else if (!best &&
+				   dl_time_before(max_dl,
+						  dl_rq->earliest_dl.curr)) {
+				max_dl = dl_rq->earliest_dl.curr;
+				found = cpu;
+			}
+		} else if (later_mask)
+			cpumask_clear_cpu(cpu, later_mask);
+	}
+
+	return found;
+}
maintainer
#84 lore raw

Re: [PATCH 06/16] sched: SCHED_DEADLINE push and pull logic

In reply to Juri Lelli
Quoted 10 lines · Juri Lelli

/*
* Only called when both the current and waking task are -deadline
* tasks.

kernel/sched_dl.c +1 -1
@@ -487,8 +819,20 @@ static void yield_task_dl(struct rq *rq)
 static void check_preempt_curr_dl(struct rq *rq, struct task_struct *p,
 				  int flags)
 {
-	if (dl_time_before(p->dl.deadline, rq->curr->dl.deadline))
+	if (dl_time_before(p->dl.deadline, rq->curr->dl.deadline)) {
 		resched_task(rq->curr);

kernel/sched_dl.c static void yield_task_dl(struct rq *rq) · line 833

Quoted 10 lines
kernel/sched_dl.c +10 -0
+		return;
+	}
+
+#ifdef CONFIG_SMP
+	/*
+	 * In the unlikely case current and p have the same deadline
+	 * let us try to decide what's the best thing to do...
+	 */
+	if ((s64)(p->dl.deadline - rq->curr->dl.deadline) == 0 &&
+	    !need_resched())

OK, maybe I'm thick. But how is:

(s64)(p->dl.deadline - rq->curr->dl.deadline) == 0

Better than:

p->dl.deadline == rq->curr->dl.deadline

?

-- Steve

Quoted 4 lines
kernel/sched_dl.c +2 -0
+		check_preempt_equal_dl(rq, p);
+#endif /* CONFIG_SMP */
 }
 
author
#85 lore raw

Re: [PATCH 06/16] sched: SCHED_DEADLINE push and pull logic

Quoted 25 lines · Steven Rostedt
Quoted 10 lines · Juri Lelli

/*
* Only called when both the current and waking task are -deadline
* tasks.

kernel/sched_dl.c +1 -1
@@ -487,8 +819,20 @@ static void yield_task_dl(struct rq *rq)
  static void check_preempt_curr_dl(struct rq *rq, struct task_struct *p,
  				  int flags)
  {
-	if (dl_time_before(p->dl.deadline, rq->curr->dl.deadline))
+	if (dl_time_before(p->dl.deadline, rq->curr->dl.deadline)) {
  		resched_task(rq->curr);

kernel/sched_dl.c static void yield_task_dl(struct rq *rq) · line 833

Quoted 10 lines
kernel/sched_dl.c +10 -0
+		return;
+	}
+
+#ifdef CONFIG_SMP
+	/*
+	 * In the unlikely case current and p have the same deadline
+	 * let us try to decide what's the best thing to do...
+	 */
+	if ((s64)(p->dl.deadline - rq->curr->dl.deadline) == 0&&
+	    !need_resched())

OK, maybe I'm thick. But how is:

Quoted 8 lines
   (s64)(p->dl.deadline - rq->curr->dl.deadline) == 0

Better than:

   p->dl.deadline == rq->curr->dl.deadline

?

I agree, will change.

Quoted 7 lines

kernel/sched_dl.c static void yield_task_dl(struct rq *rq) · line 837

Quoted 5 lines
kernel/sched_dl.c +2 -0
+		check_preempt_equal_dl(rq, p);
+#endif /* CONFIG_SMP */
  }
 

Thanks,

- Juri

maintainer
#86 lore raw

Re: [PATCH 06/16] sched: SCHED_DEADLINE push and pull logic

In reply to Juri Lelli

kernel/sched_dl.c static void put_prev_task_dl(struct rq *rq, struct task_struct *p) · line 903

Quoted 9 lines · Juri Lelli
kernel/sched_dl.c +3 -0
@@ -543,6 +897,9 @@ static void put_prev_task_dl(struct rq *rq, struct task_struct *p)
 {
 	update_curr_dl(rq);
 	p->se.exec_start = 0;
+
+	if (on_dl_rq(&p->dl) && p->dl.nr_cpus_allowed > 1)
+		enqueue_pushable_dl_task(rq, p);
 }

Ouch! We need to fix this. This has nothing to do with your patch series, but if you look at schedule():

	put_prev_task(rq, prev);
	next = pick_next_task(rq);

We put the prev task and then pick the next task. If we call schedule for some reason when we don't need to really schedule, then we just added and removed from the pushable rb tree the same task. That is, we did the rb manipulation twice, for no good reason.

Not sure how to fix this. But it will require a generic change.

-- Steve

maintainer
#91 lore raw

Re: [PATCH 06/16] sched: SCHED_DEADLINE push and pull logic

In reply to Juri Lelli
Quoted 24 lines · Juri Lelli

static void set_cpus_allowed_dl(struct task_struct *p,

kernel/sched_dl.c +18 -0
@@ -622,10 +1346,145 @@ static void set_cpus_allowed_dl(struct task_struct *p,
 
 	BUG_ON(!dl_task(p));
 
+	/*
+	 * Update only if the task is actually running (i.e.,
+	 * it is on the rq AND it is not throttled).
+	 */
+	if (on_dl_rq(&p->dl) && (weight != p->dl.nr_cpus_allowed)) {
+		struct rq *rq = task_rq(p);
+
+		if (!task_current(rq, p)) {
+			/*
+			 * If the task was on the pushable list,
+			 * make sure it stays there only if the new
+			 * mask allows that.
+			 */
+			if (p->dl.nr_cpus_allowed > 1)
+				dequeue_pushable_dl_task(rq, p);
+
+			if (weight > 1)
+				enqueue_pushable_dl_task(rq, p);

kernel/sched_dl.c static void set_cpus_allowed_dl(struct task_struct *p, · line 1376

Quoted 10 lines
kernel/sched_dl.c +10 -0
+		}
+
+		if ((p->dl.nr_cpus_allowed <= 1) && (weight > 1)) {
+			rq->dl.dl_nr_migratory++;
+		} else if ((p->dl.nr_cpus_allowed > 1) && (weight <= 1)) {
+			BUG_ON(!rq->dl.dl_nr_migratory);
+			rq->dl.dl_nr_migratory--;
+		}
+
+		update_dl_migration(&rq->dl);

Note, I'm in the process of testing this patch:

https://lkml.org/lkml/2012/4/11/7

Just giving you a heads up, as this looks like you can benefit from this change as well.

-- Steve

Quoted 6 lines
kernel/sched_dl.c +3 -0
+	}
+
 	cpumask_copy(&p->cpus_allowed, new_mask);
 	p->dl.nr_cpus_allowed = weight;
 }
+
author
#92 lore raw

Re: [PATCH 06/16] sched: SCHED_DEADLINE push and pull logic

Quoted 38 lines · Steven Rostedt
Quoted 24 lines · Juri Lelli

static void set_cpus_allowed_dl(struct task_struct *p,

kernel/sched_dl.c +18 -0
@@ -622,10 +1346,145 @@ static void set_cpus_allowed_dl(struct task_struct *p,
 
  	BUG_ON(!dl_task(p));
 
+	/*
+	 * Update only if the task is actually running (i.e.,
+	 * it is on the rq AND it is not throttled).
+	 */
+	if (on_dl_rq(&p->dl)&&  (weight != p->dl.nr_cpus_allowed)) {
+		struct rq *rq = task_rq(p);
+
+		if (!task_current(rq, p)) {
+			/*
+			 * If the task was on the pushable list,
+			 * make sure it stays there only if the new
+			 * mask allows that.
+			 */
+			if (p->dl.nr_cpus_allowed>  1)
+				dequeue_pushable_dl_task(rq, p);
+
+			if (weight>  1)
+				enqueue_pushable_dl_task(rq, p);

kernel/sched_dl.c static void set_cpus_allowed_dl(struct task_struct *p, · line 1376

Quoted 10 lines
kernel/sched_dl.c +10 -0
+		}
+
+		if ((p->dl.nr_cpus_allowed<= 1)&&  (weight>  1)) {
+			rq->dl.dl_nr_migratory++;
+		} else if ((p->dl.nr_cpus_allowed>  1)&&  (weight<= 1)) {
+			BUG_ON(!rq->dl.dl_nr_migratory);
+			rq->dl.dl_nr_migratory--;
+		}
+
+		update_dl_migration(&rq->dl);

Note, I'm in the process of testing this patch:

Quoted 5 lines

https://lkml.org/lkml/2012/4/11/7

Just giving you a heads up, as this looks like you can benefit from this change as well.

Sure! I noticed it just today and started wondering how it would apply in my case.

Quoted 9 lines

kernel/sched_dl.c static void set_cpus_allowed_dl(struct task_struct *p, · line 1382

Quoted 7 lines
kernel/sched_dl.c +3 -0
+	}
+
  	cpumask_copy(&p->cpus_allowed, new_mask);
  	p->dl.nr_cpus_allowed = weight;
  }
+

Thanks,

- Juri

maintainer
#95 lore raw

Re: [PATCH 08/16] sched: add period support for -deadline tasks.

In reply to Juri Lelli

kernel/sched_dl.c static void replenish_dl_entity(struct sched_dl_entity *dl_se) · line 300

Quoted 10 lines · Juri Lelli
kernel/sched_dl.c +5 -1
@@ -293,7 +293,11 @@ static void replenish_dl_entity(struct sched_dl_entity *dl_se)
  * assigned (function returns true if it can).
  *
  * For this to hold, we must check if:
- *   runtime / (deadline - t) < dl_runtime / dl_deadline .
+ *   runtime / (deadline - t) < dl_runtime / dl_period .
+ *
+ * Notice that the bandwidth check is done against the period. For
+ * task with deadline equal to period this is the same of using
+ * dl_deadline instead of dl_period in the equation above.

First, it seems that the function returns true if:

dl_runtime / dl_period < runtime / (deadline - t)

I'm a little confused by this. We are comparing the ratio of runtime left and deadline left, to the ratio of total runtime to period.

I'm actually confused by this premise anyway. What's the purpose of comparing the ratio? If runtime < (deadline - t) wouldn't it not be able to complete anyway? Or are we thinking that the runtime will be interrupted proportionally by other tasks?

-- Steve

Quoted 12 lines
kernel/sched_dl.c +1 -1
  */
 static bool dl_entity_overflow(struct sched_dl_entity *dl_se, u64 t)
 {
@@ -312,7 +316,7 @@ static bool dl_entity_overflow(struct sched_dl_entity *dl_se, u64 t)
 	 * to the (absolute) deadline. Therefore, overflowing the u64
 	 * type is very unlikely to occur in both cases.
 	 */
-	left = dl_se->dl_deadline * dl_se->runtime;
+	left = dl_se->dl_period * dl_se->runtime;
 	right = (dl_se->deadline - t) * dl_se->dl_runtime;
 
 	return dl_time_before(right, left);
maintainer
#107 lore raw

Re: [PATCH 11/16] sched: add latency tracing for -deadline tasks.

In reply to Juri Lelli
Quoted 35 lines · Juri Lelli

From: Dario Faggioli <[email protected]>

It is very likely that systems that wants/needs to use the new SCHED_DEADLINE policy also want to have the scheduling latency of the -deadline tasks under control.

For this reason a new version of the scheduling wakeup latency, called "wakeup_dl", is introduced.

As a consequence of applying this patch there will be three wakeup latency tracer:
* "wakeup", that deals with all tasks in the system;
* "wakeup_rt", that deals with -rt and -deadline tasks only;
* "wakeup_dl", that deals with -deadline tasks only.

---
 kernel/trace/trace_sched_wakeup.c |   41 ++++++++++++++++++++++++++++++++++++-
 kernel/trace/trace_selftest.c     |   30 ++++++++++++++++----------
 2 files changed, 58 insertions(+), 13 deletions(-)
kernel/trace/trace_sched_wakeup.c +1 -0
index e4a70c0..9c9b1be 100644
@@ -27,6 +27,7 @@ static int			wakeup_cpu;
 static int			wakeup_current_cpu;
 static unsigned			wakeup_prio = -1;
 static int			wakeup_rt;
+static int			wakeup_dl;
 
 static arch_spinlock_t wakeup_lock =
 	(arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;

kernel/trace/trace_sched_wakeup.c probe_wakeup(void *ignore, struct task_struct *p, int success) · line 423

Quoted 3 lines
kernel/trace/trace_sched_wakeup.c +0 -0
@@ -420,6 +421,17 @@ probe_wakeup(void *ignore, struct task_struct *p, int success)
 	if ((wakeup_rt && !rt_task(p)) ||
 			p->prio >= wakeup_prio ||
 			p->prio >= current->prio)

I don't think you meant to keep both if statements. Look above and below ;-)

kernel/trace/trace_sched_wakeup.c probe_wakeup(void *ignore, struct task_struct *p, int success) · line 435

Quoted 12 lines
kernel/trace/trace_sched_wakeup.c +11 -0
+	/*
+	 * Semantic is like this:
+	 *  - wakeup tracer handles all tasks in the system, independently
+	 *    from their scheduling class;
+	 *  - wakeup_rt tracer handles tasks belonging to sched_dl and
+	 *    sched_rt class;
+	 *  - wakeup_dl handles tasks belonging to sched_dl class only.
+	 */
+	if ((wakeup_dl && !dl_task(p)) ||
+	    (wakeup_rt && !dl_task(p) && !rt_task(p)) ||
+	    (p->prio >= wakeup_prio || p->prio >= current->prio))
 		return;

Anyway, perhaps this should be broken up, as we don't want the double test, that is, wakeup_rt and wakeup_dl are both checked. Perhaps do:

	if (wakeup_dl && !dl_task(p))
		return;
	else if (wakeup_rt && !dl_task(p) && !rt_task(p))
		return;

	if (p->prio >= wakeup_prio || p->prio >= current->prio)
		return;

-- Steve

Quoted 13 lines
kernel/trace/trace_sched_wakeup.c +1 -1
 	pc = preempt_count();
@@ -431,7 +443,7 @@ probe_wakeup(void *ignore, struct task_struct *p, int success)
 	arch_spin_lock(&wakeup_lock);
 
 	/* check for races. */
-	if (!tracer_enabled || p->prio >= wakeup_prio)
+	if (!tracer_enabled || (!dl_task(p) && p->prio >= wakeup_prio))
 		goto out_locked;
 
 	/* reset the trace */
@@ -539,16 +551,25 @@ static int __wakeup_tracer_init(struct trace_array *tr)
 
maintainer
#113 lore raw

Re: [PATCH 12/16] rtmutex: turn the plist into an rb-tree.

In reply to Juri Lelli
Quoted 5 lines · Juri Lelli

From: Peter Zijlstra <[email protected]>

Turn the pi-chains from plist to rb-tree, in the rt_mutex code, and provide a proper comparison function for -deadline and -priority tasks.

I have to ask. Why not just add a rbtree with a plist? That is, add all deadline tasks to the rbtree and all others to the plist. As plist has a O(1) operation, and rbtree does not. We are making all RT tasks suffer the overhead of the rbtree.

As deadline tasks always win, the two may stay agnostic from each other. Check first the rbtree, if it is empty, then check the plist.

This will become more predominant with the -rt tree as it converts most the locks in the kernel to pi mutexes.

-- Steve

Quoted 22 lines

This is done mainly because:
- classical prio field of the plist is just an int, which might not be enough for representing a deadline;
- manipulating such a list would become O(nr_deadline_tasks), which might be to much, as the number of -deadline task increases.

Therefore, an rb-tree is used, and tasks are queued in it according to the following logic:
- among two -priority (i.e., SCHED_BATCH/OTHER/RR/FIFO) tasks, the one with the higher (lower, actually!) prio wins;
- among a -priority and a -deadline task, the latter always wins;
- among two -deadline tasks, the one with the earliest deadline wins.

Queueing and dequeueing functions are changed accordingly, for both the list of a task's pi-waiters and the list of tasks blocked on a pi-lock.

author
#96 lore raw

Re: [PATCH 08/16] sched: add period support for -deadline tasks.

In reply to Steven Rostedt
Quoted 15 lines · Steven Rostedt

kernel/sched_dl.c static void replenish_dl_entity(struct sched_dl_entity *dl_se) · line 300

Quoted 10 lines · Juri Lelli
kernel/sched_dl.c +5 -1
@@ -293,7 +293,11 @@ static void replenish_dl_entity(struct sched_dl_entity *dl_se)
   * assigned (function returns true if it can).
   *
   * For this to hold, we must check if:
- *   runtime / (deadline - t)<  dl_runtime / dl_deadline .
+ *   runtime / (deadline - t)<  dl_runtime / dl_period .
+ *
+ * Notice that the bandwidth check is done against the period. For
+ * task with deadline equal to period this is the same of using
+ * dl_deadline instead of dl_period in the equation above.

First, it seems that the function returns true if:

Quoted 2 lines

dl_runtime / dl_period< runtime / (deadline - t)

Right, the comment is wrong! Just reverse the inequality as you did.

Quoted 9 lines

I'm a little confused by this. We are comparing the ratio of runtime left and deadline left, to the ratio of total runtime to period.

I'm actually confused by this premise anyway. What's the purpose of comparing the ratio? If runtime< (deadline - t) wouldn't it not be able to complete anyway? Or are we thinking that the runtime will be interrupted proportionally by other tasks?

We are actually applying one of the CBS rules. We want to be able to "slow down" a deadline task if it is going to exceed its reserved bandwidth. We are in fact checking here that the bandwidth this task will consume from t to its deadline is no more than its reserved one.

Quoted 13 lines
Quoted 13 lines
kernel/sched_dl.c +1 -1
   */
  static bool dl_entity_overflow(struct sched_dl_entity *dl_se, u64 t)
  {
@@ -312,7 +316,7 @@ static bool dl_entity_overflow(struct sched_dl_entity *dl_se, u64 t)
  	 * to the (absolute) deadline. Therefore, overflowing the u64
  	 * type is very unlikely to occur in both cases.
  	 */
-	left = dl_se->dl_deadline * dl_se->runtime;
+	left = dl_se->dl_period * dl_se->runtime;
  	right = (dl_se->deadline - t) * dl_se->dl_runtime;
 
Quoted 3 lines

kernel/sched_dl.c static bool dl_entity_overflow(struct sched_dl_entity *dl_se, u64 t) · line 322

Quoted 1 line
kernel/sched_dl.c +0 -0
  	return dl_time_before(right, left);

Thanks,

- Juri

#97 lore raw

Re: [PATCH 08/16] sched: add period support for -deadline tasks.

In reply to Steven Rostedt

Il 11/04/2012 21:32, Steven Rostedt ha scritto:

Quoted 17 lines · Juri Lelli

kernel/sched_dl.c static void replenish_dl_entity(struct sched_dl_entity *dl_se) · line 300

Quoted 11 lines
kernel/sched_dl.c +5 -1
@@ -293,7 +293,11 @@ static void replenish_dl_entity(struct sched_dl_entity *dl_se)
   * assigned (function returns true if it can).
   *
   * For this to hold, we must check if:
- *   runtime / (deadline - t)<  dl_runtime / dl_deadline .
+ *   runtime / (deadline - t)<  dl_runtime / dl_period .
+ *
+ * Notice that the bandwidth check is done against the period. For
+ * task with deadline equal to period this is the same of using
+ * dl_deadline instead of dl_period in the equation above.

First, it seems that the function returns true if:

dl_runtime / dl_period< runtime / (deadline - t)

Quoted 7 lines

I'm a little confused by this. We are comparing the ratio of runtime left and deadline left, to the ratio of total runtime to period.

I'm actually confused by this premise anyway. What's the purpose of comparing the ratio? If runtime< (deadline - t) wouldn't it not be able to complete anyway? Or are we thinking that the runtime will be interrupted proportionally by other tasks?

That's a well-known property of the CBS scheduling algorithm:
the "unblock rule" says that, when a task wakes up, if the residual budget over residual deadline fits within the allocated "bandwidth", then we can keep the current (abs) deadline and residual budget without disrupting the schedulability of the system (i.e., ability of other admitted tasks to meet their deadlines).
Otherwise, we should reset the status, i.e., refill budget and set the deadline a period in the future, because keeping the current abs deadline of the task would result in breaking guarantees promised to other tasks.
Imagine a task going to sleep and waking
too close to its deadline: its deadline would be the closest in the system, and it wouldn't allow anyone else to run. However, if its residual budget is also big, then this is going to not allow anyone to run for too much. Now, when doing the classical easy admission test for single-CPU EDF (sum of budgets over periods <= 1), we hadn't accounted for such a scenario with tasks blocking (which can actually be accounted for by using far more complex tests knowing for how long each task will block etc.). However, using the CBS, we can keep the easy test and add the simple cut-off rule at task wake-up which adds "temporal isolation", i.e., the capability to not disrupt others' guarantees if I sleep for too much. Said this, I guess/hope the rule is implemented right :-)

For a formal proof, I think you can refer to the Abeni's paper(s):

-) Integrating Multimedia Applications in Hard Real-Time Systems, RTSS '98

    www.cis.upenn.edu/~lee/01cis642/papers/AB98.pdf

which redirects on the Technical Report:
-) Server Mechanisms for Multimedia Applications

Hope this helps,

    T.
Signature · 4 lines
Tommaso Cucinotta, Computer Engineering PhD, Researcher
ReTiS Lab, Scuola Superiore Sant'Anna, Pisa, Italy
Tel +39 050 882 024, Fax +39 050 882 003
http://retis.sssup.it/people/tommaso
maintainer
#98 lore raw

Re: [PATCH 08/16] sched: add period support for -deadline tasks.

Quoted 9 lines · Tommaso Cucinotta

For a formal proof, I think you can refer to the Abeni's paper(s):

-) Integrating Multimedia Applications in Hard Real-Time Systems, RTSS '98

     www.cis.upenn.edu/~lee/01cis642/papers/AB98.pdf

which redirects on the Technical Report:
-) Server Mechanisms for Multimedia Applications

Hope this helps,

Thanks, I'll read up on it. Honestly, it's been 5 years since I worked with rate monotonic algorithms. I'm happy to get back into it ;-)

-- Steve

maintainer
#118 lore raw

Re: [PATCH 13/16] sched: drafted deadline inheritance logic.

In reply to Juri Lelli
Quoted 6 lines · Juri Lelli

From: Dario Faggioli <[email protected]>

Some method to deal with rt-mutexes and make sched_dl interact with the current PI-coded is needed, raising all but trivial issues, that needs (according to us) to be solved with some restructuring of the pi-code (i.e., going toward a proxy execution-ish implementation).

I also would like to point out that this is probably only for -rt? Or is this for PI futexes as well?

Note, non-proxy version of this code may be an issue. Could a process force more bandwidth than was allowed by rlimit if the process used PI futexes forced a behavior that PI waiters were waiting on another process while it held the PI futex? The process that held the futex may get more bandwidth if it never lets go of the futex, right?

Quoted 9 lines

This is under development, in the meanwhile, as a temporary solution, what this commits does is:
- ensure a pi-lock owner with waiters is never throttled down. Instead, when it runs out of runtime, it immediately gets replenished and it's deadline is postponed;
- the scheduling parameters (relative deadline and default runtime) used for that replenishments --during the whole period it holds the pi-lock-- are the ones of the waiting task with earliest deadline.

This sounds similar to what I implemented for a company back in 2005.

Quoted 25 lines

Acting this way, we provide some kind of boosting to the lock-owner, still by using the existing (actually, slightly modified by the previous commit) pi-architecture.

We would stress the fact that this is only a surely needed, all but clean solution to the problem. In the end it's only a way to re-start discussion within the community. So, as always, comments, ideas, rants, etc.. are welcome! :-)

---
 include/linux/sched.h |    9 +++++-
 kernel/fork.c         |    1 +
 kernel/rtmutex.c      |   13 +++++++-
 kernel/sched.c        |   34 ++++++++++++++++++---
 kernel/sched_dl.c     |   77 +++++++++++++++++++++++++++++++++---------------
 5 files changed, 102 insertions(+), 32 deletions(-)
include/linux/sched.h +0 -0
index 5ef7bb6..ca45db4 100644

include/linux/sched.h struct sched_dl_entity { · line 1313

Quoted 5 lines
include/linux/sched.h +2 -0
@@ -1309,8 +1309,12 @@ struct sched_dl_entity {
 	 * @dl_new tells if a new instance arrived. If so we must
 	 * start executing it with full runtime and reset its absolute
 	 * deadline;
+	 *
+	 * @dl_boosted tells if we are boosted due to DI. If so we are

DI?

-- Steve

Quoted 16 lines
include/linux/sched.h +5 -1
+	 * outside bandwidth enforcement mechanism (but only until we
+	 * exit the critical section).
 	 */
-	int dl_throttled, dl_new;
+	int dl_throttled, dl_new, dl_boosted;
 
 	/*
 	 * Bandwidth enforcement timer. Each -deadline task has its
@@ -1556,6 +1560,8 @@ struct task_struct {
 	struct rb_node *pi_waiters_leftmost;
 	/* Deadlock detection and priority inheritance handling */
 	struct rt_mutex_waiter *pi_blocked_on;
+	/* Top pi_waiters task */
+	struct task_struct *pi_top_task;
 #endif
 
#99 lore raw

Re: [PATCH 08/16] sched: add period support for -deadline tasks.

In reply to Steven Rostedt

Hi Steven,

Quoted 18 lines · Steven Rostedt

kernel/sched_dl.c static void replenish_dl_entity(struct sched_dl_entity *dl_se) · line 300

Quoted 10 lines · Juri Lelli
kernel/sched_dl.c +5 -1
@@ -293,7 +293,11 @@ static void replenish_dl_entity(struct sched_dl_entity *dl_se)
   * assigned (function returns true if it can).
   *
   * For this to hold, we must check if:
- *   runtime / (deadline - t)<  dl_runtime / dl_deadline .
+ *   runtime / (deadline - t)<  dl_runtime / dl_period .
+ *
+ * Notice that the bandwidth check is done against the period. For
+ * task with deadline equal to period this is the same of using
+ * dl_deadline instead of dl_period in the equation above.

First, it seems that the function returns true if:

dl_runtime / dl_period< runtime / (deadline - t)

Quoted 7 lines

I'm a little confused by this. We are comparing the ratio of runtime left and deadline left, to the ratio of total runtime to period.

I'm actually confused by this premise anyway. What's the purpose of comparing the ratio? If runtime< (deadline - t) wouldn't it not be able to complete anyway? Or are we thinking that the runtime will be interrupted proportionally by other tasks?

I see that other people have been faster than me to reply (and Tommaso's reply is pretty good).

I just add that the original CBS algorithm (which introduced this kind of check) assumed "relative deadline = reservation period". So, if you are wondering why
dl_runtime / dl_deadline
is changed into
dl_runtime / dl_period
I agree that this is not too clear... In my opinion, a more correct (but probably too pessimistic) check should use
dl_runtime / min(dl_deadline, dl_period)

Also, note that this check is presented as

	c_s >= (d_{s,k} - r_{i,j})U_s

(where U_s = Q_s/T_s = dl_runtime / dl_period - with dl_period = dl_deadline) in the original paper (see the "When a job J_{i,j} arrives and the server is idle..." item at the end of the first column of the third page of the paper).

Also, regarding the "Server mechanisms for multimedia applications" technical report mentioned by Tommaso, you can download it from here:
http://xoomer.virgilio.it/lucabe72/pubs/tr-98-01.ps

Luca

author
#108 lore raw

Re: [PATCH 11/16] sched: add latency tracing for -deadline tasks.

In reply to Steven Rostedt
Quoted 33 lines · Steven Rostedt
Quoted 32 lines · Juri Lelli

From: Dario Faggioli<[email protected]>

It is very likely that systems that wants/needs to use the new SCHED_DEADLINE policy also want to have the scheduling latency of the -deadline tasks under control.

For this reason a new version of the scheduling wakeup latency, called "wakeup_dl", is introduced.

As a consequence of applying this patch there will be three wakeup latency tracer:
* "wakeup", that deals with all tasks in the system;
* "wakeup_rt", that deals with -rt and -deadline tasks only;
* "wakeup_dl", that deals with -deadline tasks only.

---
  kernel/trace/trace_sched_wakeup.c |   41 ++++++++++++++++++++++++++++++++++++-
  kernel/trace/trace_selftest.c     |   30 ++++++++++++++++----------
  2 files changed, 58 insertions(+), 13 deletions(-)
kernel/trace/trace_sched_wakeup.c +1 -0
index e4a70c0..9c9b1be 100644
@@ -27,6 +27,7 @@ static int			wakeup_cpu;
  static int			wakeup_current_cpu;
  static unsigned			wakeup_prio = -1;
  static int			wakeup_rt;
+static int			wakeup_dl;
 
Quoted 10 lines

kernel/trace/trace_sched_wakeup.c probe_wakeup(void *ignore, struct task_struct *p, int success) · line 423

Quoted 6 lines
kernel/trace/trace_sched_wakeup.c +0 -0
  static arch_spinlock_t wakeup_lock =
  	(arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
@@ -420,6 +421,17 @@ probe_wakeup(void *ignore, struct task_struct *p, int success)
  	if ((wakeup_rt&&  !rt_task(p)) ||
  			p->prio>= wakeup_prio ||
  			p->prio>= current->prio)

I don't think you meant to keep both if statements. Look above and below ;-)

Ouch! Forgot to cut something! :-(

Quoted 16 lines

kernel/trace/trace_sched_wakeup.c probe_wakeup(void *ignore, struct task_struct *p, int success) · line 435

Quoted 12 lines
kernel/trace/trace_sched_wakeup.c +11 -0
+	/*
+	 * Semantic is like this:
+	 *  - wakeup tracer handles all tasks in the system, independently
+	 *    from their scheduling class;
+	 *  - wakeup_rt tracer handles tasks belonging to sched_dl and
+	 *    sched_rt class;
+	 *  - wakeup_dl handles tasks belonging to sched_dl class only.
+	 */
+	if ((wakeup_dl&&  !dl_task(p)) ||
+	    (wakeup_rt&&  !dl_task(p)&&  !rt_task(p)) ||
+	    (p->prio>= wakeup_prio || p->prio>= current->prio))
  		return;

Anyway, perhaps this should be broken up, as we don't want the double test, that is, wakeup_rt and wakeup_dl are both checked. Perhaps do:

Quoted 7 lines
	if (wakeup_dl&&  !dl_task(p))
		return;
	else if (wakeup_rt&&  !dl_task(p)&&  !rt_task(p))
		return;

	if (p->prio>= wakeup_prio || p->prio>= current->prio)
		return;

Yes, way better.

Thanks!

- Juri

Quoted 15 lines
Quoted 13 lines
kernel/trace/trace_sched_wakeup.c +1 -1
  	pc = preempt_count();
@@ -431,7 +443,7 @@ probe_wakeup(void *ignore, struct task_struct *p, int success)
  	arch_spin_lock(&wakeup_lock);
 
  	/* check for races. */
-	if (!tracer_enabled || p->prio>= wakeup_prio)
+	if (!tracer_enabled || (!dl_task(p)&&  p->prio>= wakeup_prio))
  		goto out_locked;
 
  	/* reset the trace */
@@ -539,16 +551,25 @@ static int __wakeup_tracer_init(struct trace_array *tr)
 
#75 lore raw

Re: [PATCH 06/16] sched: SCHED_DEADLINE push and pull logic

In reply to Steven Rostedt
Quoted 16 lines · Steven Rostedt
Quoted 5 lines · Hillf Danton
Quoted 5 lines
kernel/sched_dl.c +5 -0
+static void inc_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
+{
+       dl_rq = &rq_of_dl_rq(dl_rq)->dl;
+
+       dl_rq->dl_nr_total++;
Quoted 8 lines
Quoted 4 lines

+       if (dl_se->nr_cpus_allowed > 1)
+               dl_rq->dl_nr_migratory++;
+
+       update_dl_migration(dl_rq);

      if (dl_se->nr_cpus_allowed > 1) { dl_rq->dl_nr_migratory++;
              /* No change in migratory, no update of migration */

Quoted 6 lines

This is not true. As dl_nr_total changed. If there was only one dl task queued that can migrate, and then another dl task is queued but this task can not migrate, the update_dl_migration still needs to be called. As dl_nr_migratory would be 1, but now dl_nr_total > 1. This means we are now overloaded.

	if (2 == dl_nr_migratory + dl_nr_total)
		rq was not overloaded;

	after enqueuing a deadline task that is not migratory,

	if (current task is not preempted)
		rq remains not overloaded;

	else if (current task is not pushed out) {
		if (rq is not overloaded)
			maintenance of overloaded is __corrupted__;
	}

btw, same behavior in RTS?

maintainer
#76 lore raw

Re: [PATCH 06/16] sched: SCHED_DEADLINE push and pull logic

Quoted 17 lines · Hillf Danton
Quoted 16 lines · Steven Rostedt
Quoted 13 lines · Hillf Danton

kernel/sched_dl.c static inline int is_leftmost(struct task_struct *p, struct dl_rq *dl_rq) · line 114

Quoted 9 lines
kernel/sched_dl.c +9 -0
+static void inc_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
+{
+       dl_rq = &rq_of_dl_rq(dl_rq)->dl;
+
+       dl_rq->dl_nr_total++;
+       if (dl_se->nr_cpus_allowed > 1)
+               dl_rq->dl_nr_migratory++;
+
+       update_dl_migration(dl_rq);
      if (dl_se->nr_cpus_allowed > 1) {
              dl_rq->dl_nr_migratory++;
              /* No change in migratory, no update of migration */
Quoted 6 lines
Quoted 6 lines

This is not true. As dl_nr_total changed. If there was only one dl task queued that can migrate, and then another dl task is queued but this task can not migrate, the update_dl_migration still needs to be called. As dl_nr_migratory would be 1, but now dl_nr_total > 1. This means we are now overloaded.

You're speaking in riddles.

Quoted 4 lines
	if (2 == dl_nr_migratory + dl_nr_total)
		rq was not overloaded;

	after enqueuing a deadline task that is not migratory,

Now rq would be overloaded because:

	dl_nr_migratory + dl_nr_total == 3
Quoted 3 lines
	if (current task is not preempted)
		rq remains not overloaded;

s/not//

Quoted 7 lines
	else if (current task is not pushed out) {
		if (rq is not overloaded)
			maintenance of overloaded is __corrupted__;
	}

btw, same behavior in RTS?

I still don't understand what you are saying.

I can see your scenario happening with the change you are suggesting though.

-- Steve

#77 lore raw

Re: [PATCH 06/16] sched: SCHED_DEADLINE push and pull logic

Quoted 27 lines · Steven Rostedt
Quoted 17 lines · Hillf Danton
Quoted 16 lines · Steven Rostedt
Quoted 13 lines · Hillf Danton

kernel/sched_dl.c static inline int is_leftmost(struct task_struct *p, struct dl_rq *dl_rq) · line 114

Quoted 9 lines
kernel/sched_dl.c +9 -0
+static void inc_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
+{
+       dl_rq = &rq_of_dl_rq(dl_rq)->dl;
+
+       dl_rq->dl_nr_total++;
+       if (dl_se->nr_cpus_allowed > 1)
+               dl_rq->dl_nr_migratory++;
+
+       update_dl_migration(dl_rq);

      if (dl_se->nr_cpus_allowed > 1) { dl_rq->dl_nr_migratory++;
              /* No change in migratory, no update of migration */

Quoted 6 lines
Quoted 6 lines

This is not true. As dl_nr_total changed. If there was only one dl task queued that can migrate, and then another dl task is queued but this task can not migrate, the update_dl_migration still needs to be called. As dl_nr_migratory would be 1, but now dl_nr_total > 1. This means we are now overloaded.

You're speaking in riddles.

Quoted 9 lines
Quoted 4 lines

      if (2 == dl_nr_migratory + dl_nr_total)
              rq was not overloaded;

      after enqueuing a deadline task that is not migratory,

Now rq would be overloaded because:

       dl_nr_migratory + dl_nr_total == 3

Which is the current task, and where?

Quoted 23 lines
Quoted 4 lines

      if (current task is not preempted)
              rq remains not overloaded;

s/not//

Quoted 7 lines

      else if (current task is not pushed out) {
              if (rq is not overloaded)
                      maintenance of overloaded is __corrupted__; }

btw, same behavior in RTS?

I still don't understand what you are saying.

I can see your scenario happening with the change you are suggesting though.

-- Steve

maintainer
#78 lore raw

Re: [PATCH 06/16] sched: SCHED_DEADLINE push and pull logic

Quoted 11 lines · Hillf Danton
Quoted 9 lines
Quoted 4 lines
      if (2 == dl_nr_migratory + dl_nr_total)
              rq was not overloaded;

      after enqueuing a deadline task that is not migratory,

Now rq would be overloaded because:

       dl_nr_migratory + dl_nr_total == 3

Which is the current task, and where?

More riddles?

The current task is the one executing on the CPU.

-- Steve

Quoted 24 lines
Quoted 24 lines
Quoted 5 lines

if (current task is not preempted)
rq remains not overloaded;

s/not//

Quoted 7 lines
      else if (current task is not pushed out) {
              if (rq is not overloaded)
                      maintenance of overloaded is __corrupted__;
      }

btw, same behavior in RTS?

I still don't understand what you are saying.

I can see your scenario happening with the change you are suggesting though.

-- Steve

#79 lore raw

Re: [PATCH 06/16] sched: SCHED_DEADLINE push and pull logic

Quoted 7 lines · Steven Rostedt
Quoted 5 lines · Hillf Danton
Quoted 5 lines
Quoted 4 lines

      if (2 == dl_nr_migratory + dl_nr_total)
              rq was not overloaded;

      after enqueuing a deadline task that is not migratory,

Quoted 9 lines
Quoted 6 lines
Quoted 4 lines

Now rq would be overloaded because:

       dl_nr_migratory + dl_nr_total == 3

Which is the current task, and where?

More riddles?

As always:)

Quoted 3 lines

The current task is the one executing on the CPU.

I will see rt tomorrow, and report if any new finding.

Best Regards
Hillf

#109 lore raw

Re: [PATCH 11/16] sched: add latency tracing for -deadline tasks.

In reply to Steven Rostedt

Hi,

Quoted 21 lines · Steven Rostedt
Quoted 4 lines

+     /*
+      * Semantic is like this:
+      *  - wakeup tracer handles all tasks in the system, independently
+      *    from their scheduling class;

Quoted 8 lines

+      *  - wakeup_rt tracer handles tasks belonging to sched_dl and
+      *    sched_rt class;
+      *  - wakeup_dl handles tasks belonging to sched_dl class only.
+      */
+     if ((wakeup_dl && !dl_task(p)) ||
+         (wakeup_rt && !dl_task(p) && !rt_task(p)) ||
+         (p->prio >= wakeup_prio || p->prio >= current->prio)) return;

Anyway, perhaps this should be broken up, as we don't want the double test, that is, wakeup_rt and wakeup_dl are both checked. Perhaps do:

       if (wakeup_dl && !dl_task(p))
               return;
       else if (wakeup_rt && !dl_task(p) && !rt_task(p)) return;

Quoted 5 lines

       if (p->prio >= wakeup_prio || p->prio >= current->prio) return;

-- Steve

sorry for the question, I'm obviously missing something here but what is the logic behind this rewrite? In both cases my gcc generates the same code for me.

nX

maintainer
#110 lore raw

Re: [PATCH 11/16] sched: add latency tracing for -deadline tasks.

Quoted 3 lines · Daniel Vacek

sorry for the question, I'm obviously missing something here but what is the logic behind this rewrite? In both cases my gcc generates the same code for me.

Yeah, I noticed that later. I thought it was doing something slightly different, but after a good nights rest, and re-reading what I wrote in the morning, it was obviously the same functionality.

But that said. The final result is much easier to read. And as you stated, it doesn't make a difference in the final outcome, it ended up being a good fix (more readable code means less bugs).

-- Steve

#111 lore raw

Re: [PATCH 11/16] sched: add latency tracing for -deadline tasks.

Quoted 10 lines · Steven Rostedt
Quoted 3 lines · Daniel Vacek

sorry for the question, I'm obviously missing something here but what is the logic behind this rewrite? In both cases my gcc generates the same code for me.

Yeah, I noticed that later. I thought it was doing something slightly different, but after a good nights rest, and re-reading what I wrote in the morning, it was obviously the same functionality.

Quoted 5 lines

But that said. The final result is much easier to read. And as you stated, it doesn't make a difference in the final outcome, it ended up being a good fix (more readable code means less bugs).

-- Steve

That's exactly why I reacted in the first place. I would say the original code was cleaner and more readable IMHO.

And I twisted my brain hardly in getting what's the difference I can't see for this ugly change ;-) So at the end I compiled both versions and then asked what I'm doing wrong :-)

Glad to hear I was not wrong. So it is up to you (or Juri) which version is the 'right' one.

-- nX

author
#83 lore raw

Re: [PATCH 06/16] sched: SCHED_DEADLINE push and pull logic

In reply to Steven Rostedt
Quoted 17 lines · Steven Rostedt
Quoted 15 lines · Juri Lelli
kernel/sched_dl.c +14 -0
+static int latest_cpu_find(struct cpumask *span,
+			   struct task_struct *task,
+			   struct cpumask *later_mask)
  {
+	const struct sched_dl_entity *dl_se =&task->dl;
+	int cpu, found = -1, best = 0;
+	u64 max_dl = 0;
+
+	for_each_cpu(cpu, span) {
+		struct rq *rq = cpu_rq(cpu);
+		struct dl_rq *dl_rq =&rq->dl;
+
+		if (cpumask_test_cpu(cpu,&task->cpus_allowed)&&
+		    (!dl_rq->dl_nr_running || dl_time_before(dl_se->deadline,
+		     dl_rq->earliest_dl.curr))) {
Quoted 8 lines

kernel/sched_dl.c static void set_curr_task_dl(struct rq *rq)

Quoted 3 lines
kernel/sched_dl.c +3 -0
+			if (later_mask)
+				cpumask_set_cpu(cpu, later_mask);
+			if (!best&&  !dl_rq->dl_nr_running) {

I hate to say this (and I also have yet to look at the patches after this) but we should really take into account the RT tasks. It would suck to preempt a normal RT task when a non RT task is running on another CPU.

Well, this changes in 15/16, but your point remains, and I see it :-). We are currently reworking the push/pull mechanism (15/16 will most probably change as well) to speed it up. I agree that it would be nice to take into accont RT tasks as well, so we'll surely think about it.

Quoted 6 lines
Quoted 6 lines
kernel/sched_dl.c +6 -0
+				best = 1;
+				found = cpu;
+			} else if (!best&&
+				   dl_time_before(max_dl,
+						  dl_rq->earliest_dl.curr)) {
+				max_dl = dl_rq->earliest_dl.curr;
Quoted 10 lines

kernel/sched_dl.c static void set_curr_task_dl(struct rq *rq) · line 1009

Quoted 8 lines
kernel/sched_dl.c +8 -0
+				found = cpu;
+			}
+		} else if (later_mask)
+			cpumask_clear_cpu(cpu, later_mask);
+	}
+
+	return found;
+}

Thanks,

- Juri

author
#119 lore raw

Re: [PATCH 13/16] sched: drafted deadline inheritance logic.

In reply to Steven Rostedt
Quoted 11 lines · Steven Rostedt
Quoted 6 lines · Juri Lelli

From: Dario Faggioli<[email protected]>

Some method to deal with rt-mutexes and make sched_dl interact with the current PI-coded is needed, raising all but trivial issues, that needs (according to us) to be solved with some restructuring of the pi-code (i.e., going toward a proxy execution-ish implementation).

I also would like to point out that this is probably only for -rt? Or is this for PI futexes as well?

Well, this directly affects rtmutex.c only. But, as soon as a PI-enabled user-space mutex fails the fastpath all returns to rtmutex machinery, right? So, I would say that this apply to PI futexes as well :-|.

Quoted 6 lines

Note, non-proxy version of this code may be an issue. Could a process force more bandwidth than was allowed by rlimit if the process used PI futexes forced a behavior that PI waiters were waiting on another process while it held the PI futex? The process that held the futex may get more bandwidth if it never lets go of the futex, right?

Yes, I would remark that this is a needed _temporary_ solution (just to not delay further the release post). I tried to remain simple as to not prevent any fast shift in the way we want to resolve this problem. Anyway, it would be great if some discussion on this point could re-start :-).

Quoted 32 lines
Quoted 9 lines

This is under development, in the meanwhile, as a temporary solution, what this commits does is:
- ensure a pi-lock owner with waiters is never throttled down. Instead, when it runs out of runtime, it immediately gets replenished and it's deadline is postponed;
- the scheduling parameters (relative deadline and default runtime) used for that replenishments --during the whole period it holds the pi-lock-- are the ones of the waiting task with earliest deadline.

This sounds similar to what I implemented for a company back in 2005.

Quoted 20 lines

Acting this way, we provide some kind of boosting to the lock-owner, still by using the existing (actually, slightly modified by the previous commit) pi-architecture.

We would stress the fact that this is only a surely needed, all but clean solution to the problem. In the end it's only a way to re-start discussion within the community. So, as always, comments, ideas, rants, etc.. are welcome! :-)

---
  include/linux/sched.h |    9 +++++-
  kernel/fork.c         |    1 +
  kernel/rtmutex.c      |   13 +++++++-
  kernel/sched.c        |   34 ++++++++++++++++++---
  kernel/sched_dl.c     |   77 +++++++++++++++++++++++++++++++++---------------
  5 files changed, 102 insertions(+), 32 deletions(-)
Quoted 13 lines

include/linux/sched.h struct sched_dl_entity { · line 1313

Quoted 10 lines
include/linux/sched.h +2 -0
index 5ef7bb6..ca45db4 100644
@@ -1309,8 +1309,12 @@ struct sched_dl_entity {
  	 * @dl_new tells if a new instance arrived. If so we must
  	 * start executing it with full runtime and reset its absolute
  	 * deadline;
+	 *
+	 * @dl_boosted tells if we are boosted due to DI. If so we are

DI?

DI = Deadline Inheritance. Even if not strictly correct in the definition, when you're boosted you inherit highest priority waiter's parameters, among which deadline. In fact you inherit its budget.

Quoted 6 lines
Quoted 6 lines
include/linux/sched.h +3 -1
+	 * outside bandwidth enforcement mechanism (but only until we
+	 * exit the critical section).
  	 */
-	int dl_throttled, dl_new;
+	int dl_throttled, dl_new, dl_boosted;
 
Quoted 10 lines

include/linux/sched.h struct task_struct { · line 1566

Quoted 10 lines
include/linux/sched.h +2 -0
  	/*
  	 * Bandwidth enforcement timer. Each -deadline task has its
@@ -1556,6 +1560,8 @@ struct task_struct {
  	struct rb_node *pi_waiters_leftmost;
  	/* Deadlock detection and priority inheritance handling */
  	struct rt_mutex_waiter *pi_blocked_on;
+	/* Top pi_waiters task */
+	struct task_struct *pi_top_task;
  #endif
 

Sorry for the delayed reply, I was really busy for the last two weeks :-\.

Thanks and Regards,

- Juri

author
#114 lore raw

Re: [PATCH 12/16] rtmutex: turn the plist into an rb-tree.

In reply to Steven Rostedt
Quoted 12 lines · Steven Rostedt
Quoted 5 lines · Juri Lelli

From: Peter Zijlstra<[email protected]>

Turn the pi-chains from plist to rb-tree, in the rt_mutex code, and provide a proper comparison function for -deadline and -priority tasks.

I have to ask. Why not just add a rbtree with a plist? That is, add all deadline tasks to the rbtree and all others to the plist. As plist has a O(1) operation, and rbtree does not. We are making all RT tasks suffer the overhead of the rbtree.

I basically got this patch from the v3 patchset and, since it applied perfectly and came from Peter, I assumed it was the right way to go ;-).

Quoted 6 lines

As deadline tasks always win, the two may stay agnostic from each other. Check first the rbtree, if it is empty, then check the plist.

This will become more predominant with the -rt tree as it converts most the locks in the kernel to pi mutexes.

I see your point, but I'm not yet convinced that in the end the plist + rbtree implementation would win. AFAIK, the only O(1) plist operation is removal, beeing addition O(K) [K RT priorities]. Now, we have O(logn) [n elements] operations for rbtrees and we speed-up search with the leftmost pointer.
So, are we sure that add complexity (and related checks) is needed here? I'm not against your point, I'm only asking :-).

Thanks a lot,

- Juri

Quoted 24 lines
Quoted 22 lines

This is done mainly because:
- classical prio field of the plist is just an int, which might

    not be enough for representing a deadline;

- manipulating such a list would become O(nr_deadline_tasks), which might be to much, as the number of -deadline task increases.

Therefore, an rb-tree is used, and tasks are queued in it according to the following logic:
- among two -priority (i.e., SCHED_BATCH/OTHER/RR/FIFO) tasks, the

    one with the higher (lower, actually!) prio wins;

- among a -priority and a -deadline task, the latter always wins;
- among two -deadline tasks, the one with the earliest deadline wins.

Queueing and dequeueing functions are changed accordingly, for both the list of a task's pi-waiters and the list of tasks blocked on a pi-lock.

maintainer
#115 lore raw

Re: [PATCH 12/16] rtmutex: turn the plist into an rb-tree.

In reply to Steven Rostedt
Quoted 5 lines · Steven Rostedt

I have to ask. Why not just add a rbtree with a plist? That is, add all deadline tasks to the rbtree and all others to the plist. As plist has a O(1) operation, and rbtree does not. We are making all RT tasks suffer the overhead of the rbtree.

You always love to add complexity to stuff before making it work, don't you ;-)

I'm not quite convinced the plist stuff is _that_ much faster, anyway, its all a stop-gap measure until we can do proper BWI which would wipe all this code anyway.

maintainer
#120 lore raw

Re: [PATCH 13/16] sched: drafted deadline inheritance logic.

In reply to Steven Rostedt
Quoted 2 lines · Steven Rostedt

I also would like to point out that this is probably only for -rt?

Nope, upstream carries kernel/rtmutex.c as well.

Quoted 1 line

Or is this for PI futexes as well?

Quite so.

maintainer
#6 lore raw

Re: [PATCH 03/16] sched: SCHED_DEADLINE data structures.

In reply to Juri Lelli

include/linux/sched.h struct sched_rt_entity { · line 1272

Quoted 3 lines · Juri Lelli
include/linux/sched.h +3 -0
+struct sched_dl_entity {
+       struct rb_node  rb_node;
+       int nr_cpus_allowed; 

I think it would be all-round best to move sched_rt_entity::nr_cpus_allowed out next to cpus_allowed.

maintainer
#11 lore raw

Re: [PATCH 03/16] sched: SCHED_DEADLINE data structures.

In reply to Juri Lelli
Quoted 1 line · Juri Lelli

+ if (unlikely(prio >= MAX_DL_PRIO && prio < MAX_RT_PRIO))

You could write that as:

if ((unsigned)prio < MAX_RT_PRIO)

Although GCC might be smart enough to do that already, also I'm not sure people in general are willing to put up with such 'fun' stuff :-)

author
#12 lore raw

Re: [PATCH 03/16] sched: SCHED_DEADLINE data structures.

Quoted 7 lines · Peter Zijlstra
Quoted 1 line · Juri Lelli

+ if (unlikely(prio>= MAX_DL_PRIO&& prio< MAX_RT_PRIO))

You could write that as:

if ((unsigned)prio< MAX_RT_PRIO)

Right, I'll do :-).

Thanks,

- Juri

maintainer
#13 lore raw

Re: [PATCH 03/16] sched: SCHED_DEADLINE data structures.

In reply to Juri Lelli

kernel/sched.c __setscheduler(struct rq *rq, struct task_struct *p, int policy, int prio) · line 5449

Quoted 11 lines · Juri Lelli

/*
+ * This function validates the new parameters of a -deadline task.
+ * We ask for the deadline not being zero, and greater or equal
+ * than the runtime.
+ */

kernel/sched.c +6 -0
+static bool
+__checkparam_dl(const struct sched_param2 *prm)
+{
+       return prm && (&prm->sched_deadline) != 0 &&
+              (s64)(&prm->sched_deadline - &prm->sched_runtime) >= 0;
+} 

Shouldn't this also include deadline == period for now?

maintainer
#16 lore raw

Re: [PATCH 03/16] sched: SCHED_DEADLINE data structures.

In reply to Juri Lelli
Quoted 1 line · Juri Lelli

+ p->sched_class = &dl_sched_class;

Does this patch actually compile? I've only seen a fwd declaration of this variable but here you take an actual reference to it.

author
#14 lore raw

Re: [PATCH 03/16] sched: SCHED_DEADLINE data structures.

In reply to Peter Zijlstra
Quoted 14 lines · Peter Zijlstra

kernel/sched.c __setscheduler(struct rq *rq, struct task_struct *p, int policy, int prio) · line 5449

Quoted 11 lines · Juri Lelli

/*
+ * This function validates the new parameters of a -deadline task.
+ * We ask for the deadline not being zero, and greater or equal
+ * than the runtime.
+ */

kernel/sched.c +6 -0
+static bool
+__checkparam_dl(const struct sched_param2 *prm)
+{
+       return prm&&  (&prm->sched_deadline) != 0&&
+              (s64)(&prm->sched_deadline -&prm->sched_runtime)>= 0;
+}

Shouldn't this also include deadline == period for now?

No notion of period in 03/16. It is inserted in 08/16 and checkparam changed accordingly.

Thanks,

- Juri

maintainer
#15 lore raw

Re: [PATCH 03/16] sched: SCHED_DEADLINE data structures.

Quoted 16 lines · Juri Lelli
Quoted 14 lines · Peter Zijlstra

kernel/sched.c __setscheduler(struct rq *rq, struct task_struct *p, int policy, int prio) · line 5449

Quoted 11 lines · Juri Lelli

/*
+ * This function validates the new parameters of a -deadline task.
+ * We ask for the deadline not being zero, and greater or equal
+ * than the runtime.
+ */

kernel/sched.c +6 -0
+static bool
+__checkparam_dl(const struct sched_param2 *prm)
+{
+       return prm&&  (&prm->sched_deadline) != 0&&
+              (s64)(&prm->sched_deadline -&prm->sched_runtime)>= 0;
+}

Shouldn't this also include deadline == period for now?

Quoted 2 lines

No notion of period in 03/16. It is inserted in 08/16 and checkparam changed accordingly.

Ah, ok. got confused by 2/ having sched_period.

author
#7 lore raw

Re: [PATCH 03/16] sched: SCHED_DEADLINE data structures.

In reply to Peter Zijlstra
Quoted 7 lines · Peter Zijlstra

include/linux/sched.h struct sched_rt_entity { · line 1272

Quoted 3 lines · Juri Lelli
include/linux/sched.h +3 -0
+struct sched_dl_entity {
+       struct rb_node  rb_node;
+       int nr_cpus_allowed;

I think it would be all-round best to move sched_rt_entity::nr_cpus_allowed out next to cpus_allowed.

You mean unify them: a single nr_cpus_allowed after task_struct::cpus_allowed, right?

Thanks,

- Juri

maintainer
#8 lore raw

Re: [PATCH 03/16] sched: SCHED_DEADLINE data structures.

Quoted 11 lines · Juri Lelli
Quoted 7 lines · Peter Zijlstra

include/linux/sched.h struct sched_rt_entity { · line 1272

Quoted 3 lines · Juri Lelli
include/linux/sched.h +3 -0
+struct sched_dl_entity {
+       struct rb_node  rb_node;
+       int nr_cpus_allowed;

I think it would be all-round best to move sched_rt_entity::nr_cpus_allowed out next to cpus_allowed.

You mean unify them: a single nr_cpus_allowed after task_struct::cpus_allowed, right?

Yes, no point in keeping that one value twice and in fact there's a usage of p->rt.nr_cpus_allowed in sched/fair.c so its past time its moved out of that rt specific thing.

author
#9 lore raw

Re: [PATCH 03/16] sched: SCHED_DEADLINE data structures.

Quoted 10 lines · Peter Zijlstra
Quoted 9 lines · Juri Lelli
Quoted 7 lines · Peter Zijlstra

include/linux/sched.h struct sched_rt_entity { · line 1272

Quoted 3 lines · Juri Lelli
include/linux/sched.h +3 -0
+struct sched_dl_entity {
+       struct rb_node  rb_node;
+       int nr_cpus_allowed;

I think it would be all-round best to move sched_rt_entity::nr_cpus_allowed out next to cpus_allowed.

Quoted 6 lines
Quoted 2 lines

You mean unify them: a single nr_cpus_allowed after task_struct::cpus_allowed, right?

Yes, no point in keeping that one value twice and in fact there's a usage of p->rt.nr_cpus_allowed in sched/fair.c so its past time its moved out of that rt specific thing.

Sure. Since this is a small change, probably not strictly related to this patchset, may I wait to see the change in mainline?

Thanks,

- Juri

maintainer
#10 lore raw

Re: [PATCH 03/16] sched: SCHED_DEADLINE data structures.

Quoted 14 lines · Juri Lelli
Quoted 13 lines · Peter Zijlstra
Quoted 11 lines · Juri Lelli
Quoted 7 lines · Peter Zijlstra

Review

Quoted 3 lines · Juri Lelli
(quoted diff) +3 -0
+struct sched_dl_entity {
+       struct rb_node  rb_node;
+       int nr_cpus_allowed;

I think it would be all-round best to move sched_rt_entity::nr_cpus_allowed out next to cpus_allowed.

You mean unify them: a single nr_cpus_allowed after task_struct::cpus_allowed, right?

Quoted 6 lines
Quoted 3 lines

Yes, no point in keeping that one value twice and in fact there's a usage of p->rt.nr_cpus_allowed in sched/fair.c so its past time its moved out of that rt specific thing.

Sure. Since this is a small change, probably not strictly related to this patchset, may I wait to see the change in mainline?

Ok..

---
Subject: sched: Move nr_cpus_allowed out of sched_rt_entity
From: Peter Zijlstra <[email protected]>
Date: Mon Apr 23 12:11:21 CEST 2012

Since nr_cpus_allowed is used outside of sched/rt.c and wants to be used outside of there more, move it to a more natural site.

---
 arch/blackfin/kernel/process.c |    2 +-
 include/linux/init_task.h      |    2 +-
 include/linux/sched.h          |    2 +-
 kernel/sched/core.c            |    2 +-
 kernel/sched/fair.c            |    2 +-
 kernel/sched/rt.c              |   36 +++++++++++++++++++++---------------
 6 files changed, 26 insertions(+), 20 deletions(-)
arch/blackfin/kernel/process.c +1 -1
@@ -171,7 +171,7 @@ asmlinkage int bfin_clone(struct pt_regs
 	unsigned long newsp;
 
 #ifdef __ARCH_SYNC_CORE_DCACHE
-	if (current->rt.nr_cpus_allowed == num_possible_cpus())
+	if (current->nr_cpus_allowed == num_possible_cpus())
 		set_cpus_allowed_ptr(current, cpumask_of(smp_processor_id()));
 #endif
 
include/linux/init_task.h +1 -1
@@ -149,6 +149,7 @@ extern struct cred init_cred;
 	.normal_prio	= MAX_PRIO-20,					\
 	.policy		= SCHED_NORMAL,					\
 	.cpus_allowed	= CPU_MASK_ALL,					\
+	.nr_cpus_allowed= NR_CPUS,					\
 	.mm		= NULL,						\
 	.active_mm	= &init_mm,					\
 	.se		= {						\
@@ -157,7 +158,6 @@ extern struct cred init_cred;
 	.rt		= {						\
 		.run_list	= LIST_HEAD_INIT(tsk.rt.run_list),	\
 		.time_slice	= RR_TIMESLICE,				\
-		.nr_cpus_allowed = NR_CPUS,				\
 	},								\
 	.tasks		= LIST_HEAD_INIT(tsk.tasks),			\
 	INIT_PUSHABLE_TASKS(tsk)					\
include/linux/sched.h +1 -1
@@ -1234,7 +1234,6 @@ struct sched_rt_entity {
 	struct list_head run_list;
 	unsigned long timeout;
 	unsigned int time_slice;
-	int nr_cpus_allowed;
 
 	struct sched_rt_entity *back;
 #ifdef CONFIG_RT_GROUP_SCHED
@@ -1299,6 +1298,7 @@ struct task_struct {
 #endif
 
 	unsigned int policy;
+	int nr_cpus_allowed;
 	cpumask_t cpus_allowed;
 
 #ifdef CONFIG_PREEMPT_RCU
kernel/sched/core.c +1 -1
@@ -4957,7 +4957,7 @@ void do_set_cpus_allowed(struct task_str
 		p->sched_class->set_cpus_allowed(p, new_mask);
 
 	cpumask_copy(&p->cpus_allowed, new_mask);
-	p->rt.nr_cpus_allowed = cpumask_weight(new_mask);
+	p->nr_cpus_allowed = cpumask_weight(new_mask);
 }
 
 /*
kernel/sched/fair.c +1 -1
@@ -2703,7 +2703,7 @@ select_task_rq_fair(struct task_struct *
 	int want_sd = 1;
 	int sync = wake_flags & WF_SYNC;
 
-	if (p->rt.nr_cpus_allowed == 1)
+	if (p->nr_cpus_allowed == 1)
 		return prev_cpu;
 
 	if (sd_flag & SD_BALANCE_WAKE) {
kernel/sched/rt.c +21 -15
@@ -274,13 +274,16 @@ static void update_rt_migration(struct r
 
 static void inc_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
 {
+	struct task_struct *p;
+
 	if (!rt_entity_is_task(rt_se))
 		return;
 
+	p = rt_task_of(rt_se);
 	rt_rq = &rq_of_rt_rq(rt_rq)->rt;
 
 	rt_rq->rt_nr_total++;
-	if (rt_se->nr_cpus_allowed > 1)
+	if (p->nr_cpus_allowed > 1)
 		rt_rq->rt_nr_migratory++;
 
 	update_rt_migration(rt_rq);
@@ -288,13 +291,16 @@ static void inc_rt_migration(struct sche
 
 static void dec_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
 {
+	struct task_struct *p;
+
 	if (!rt_entity_is_task(rt_se))
 		return;
 
+	p = rt_task_of(rt_se);
 	rt_rq = &rq_of_rt_rq(rt_rq)->rt;
 
 	rt_rq->rt_nr_total--;
-	if (rt_se->nr_cpus_allowed > 1)
+	if (p->nr_cpus_allowed > 1)
 		rt_rq->rt_nr_migratory--;
 
 	update_rt_migration(rt_rq);
@@ -1161,7 +1167,7 @@ enqueue_task_rt(struct rq *rq, struct ta
 
 	enqueue_rt_entity(rt_se, flags & ENQUEUE_HEAD);
 
-	if (!task_current(rq, p) && p->rt.nr_cpus_allowed > 1)
+	if (!task_current(rq, p) && p->nr_cpus_allowed > 1)
 		enqueue_pushable_task(rq, p);
 
 	inc_nr_running(rq);
@@ -1225,7 +1231,7 @@ select_task_rq_rt(struct task_struct *p,
 
 	cpu = task_cpu(p);
 
-	if (p->rt.nr_cpus_allowed == 1)
+	if (p->nr_cpus_allowed == 1)
 		goto out;
 
 	/* For anything but wake ups, just return the task_cpu */
@@ -1260,9 +1266,9 @@ select_task_rq_rt(struct task_struct *p,
 	 * will have to sort it out.
 	 */
 	if (curr && unlikely(rt_task(curr)) &&
-	    (curr->rt.nr_cpus_allowed < 2 ||
+	    (curr->nr_cpus_allowed < 2 ||
 	     curr->prio <= p->prio) &&
-	    (p->rt.nr_cpus_allowed > 1)) {
+	    (p->nr_cpus_allowed > 1)) {
 		int target = find_lowest_rq(p);
 
 		if (target != -1)
@@ -1276,10 +1282,10 @@ select_task_rq_rt(struct task_struct *p,
 
 static void check_preempt_equal_prio(struct rq *rq, struct task_struct *p)
 {
-	if (rq->curr->rt.nr_cpus_allowed == 1)
+	if (rq->curr->nr_cpus_allowed == 1)
 		return;
 
-	if (p->rt.nr_cpus_allowed != 1
+	if (p->nr_cpus_allowed != 1
 	    && cpupri_find(&rq->rd->cpupri, p, NULL))
 		return;
 
@@ -1395,7 +1401,7 @@ static void put_prev_task_rt(struct rq *
 	 * The previous task needs to be made eligible for pushing
 	 * if it is still active
 	 */
-	if (on_rt_rq(&p->rt) && p->rt.nr_cpus_allowed > 1)
+	if (on_rt_rq(&p->rt) && p->nr_cpus_allowed > 1)
 		enqueue_pushable_task(rq, p);
 }
 
@@ -1408,7 +1414,7 @@ static int pick_rt_task(struct rq *rq, s
 {
 	if (!task_running(rq, p) &&
 	    (cpu < 0 || cpumask_test_cpu(cpu, tsk_cpus_allowed(p))) &&
-	    (p->rt.nr_cpus_allowed > 1))
+	    (p->nr_cpus_allowed > 1))
 		return 1;
 	return 0;
 }
@@ -1464,7 +1470,7 @@ static int find_lowest_rq(struct task_st
 	if (unlikely(!lowest_mask))
 		return -1;
 
-	if (task->rt.nr_cpus_allowed == 1)
+	if (task->nr_cpus_allowed == 1)
 		return -1; /* No other targets possible */
 
 	if (!cpupri_find(&task_rq(task)->rd->cpupri, task, lowest_mask))
@@ -1586,7 +1592,7 @@ static struct task_struct *pick_next_pus
 
 	BUG_ON(rq->cpu != task_cpu(p));
 	BUG_ON(task_current(rq, p));
-	BUG_ON(p->rt.nr_cpus_allowed <= 1);
+	BUG_ON(p->nr_cpus_allowed <= 1);
 
 	BUG_ON(!p->on_rq);
 	BUG_ON(!rt_task(p));
@@ -1793,9 +1799,9 @@ static void task_woken_rt(struct rq *rq,
 	if (!task_running(rq, p) &&
 	    !test_tsk_need_resched(rq->curr) &&
 	    has_pushable_tasks(rq) &&
-	    p->rt.nr_cpus_allowed > 1 &&
+	    p->nr_cpus_allowed > 1 &&
 	    rt_task(rq->curr) &&
-	    (rq->curr->rt.nr_cpus_allowed < 2 ||
+	    (rq->curr->nr_cpus_allowed < 2 ||
 	     rq->curr->prio <= p->prio))
 		push_rt_tasks(rq);
 }
@@ -1817,7 +1823,7 @@ static void set_cpus_allowed_rt(struct t
 	 * Only update if the process changes its state from whether it
 	 * can migrate or not.
 	 */
-	if ((p->rt.nr_cpus_allowed > 1) == (weight > 1))
+	if ((p->nr_cpus_allowed > 1) == (weight > 1))
 		return;
 
 	rq = task_rq(p);
author
#17 lore raw

Re: [PATCH 03/16] sched: SCHED_DEADLINE data structures.

In reply to Peter Zijlstra
Quoted 5 lines · Peter Zijlstra
Quoted 1 line · Juri Lelli

+ p->sched_class =&dl_sched_class;

Does this patch actually compile? I've only seen a fwd declaration of this variable but here you take an actual reference to it.

The patch compiles (just tested), although this sounds strange to me too..

Thanks,

- Juri

maintainer
#18 lore raw

Re: [PATCH 03/16] sched: SCHED_DEADLINE data structures.

In reply to Juri Lelli
Quoted 9 lines · Juri Lelli
Quoted 5 lines · Peter Zijlstra
Quoted 1 line · Juri Lelli

+ p->sched_class =&dl_sched_class;

Does this patch actually compile? I've only seen a fwd declaration of this variable but here you take an actual reference to it.

The patch compiles (just tested), although this sounds strange to me too..

I just realized that its a proper definition without an initializer. So no harm done, just a brain that still needs to wake up or so.. :-)

maintainer
#28 lore raw

Re: [PATCH 05/16] sched: SCHED_DEADLINE policy implementation.

In reply to Juri Lelli
Quoted 1 line · Juri Lelli

+ dl_se->deadline = rq->clock + dl_se->dl_deadline;

You might want to use rq->clock_task, this clock excludes times spend in hardirq context and steal-time (when paravirt).

Then again, it might not want to use that.. but its something you might want to consider and make explicit by means of a comment.

author
#19 lore raw

Re: [PATCH 03/16] sched: SCHED_DEADLINE data structures.

In reply to Peter Zijlstra
Quoted 5 lines · Peter Zijlstra
Quoted 4 lines · Juri Lelli
Quoted 3 lines · Peter Zijlstra
Quoted 1 line · Juri Lelli

+ p->sched_class =&dl_sched_class;

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Does this patch actually compile? I've only seen a fwd declaration of this variable but here you take an actual reference to it.

The patch compiles (just tested), although this sounds strange to me too..

I just realized that its a proper definition without an initializer. So no harm done, just a brain that still needs to wake up or so.. :-)

Ouch.. right! A struct sched_class after all! Regarding brains, mine should be enough woken up at this time :-|.

Thanks,

- Juri

author
#29 lore raw

Re: [PATCH 05/16] sched: SCHED_DEADLINE policy implementation.

In reply to Peter Zijlstra
Quoted 8 lines · Peter Zijlstra
Quoted 1 line · Juri Lelli

+ dl_se->deadline = rq->clock + dl_se->dl_deadline;

You might want to use rq->clock_task, this clock excludes times spend in hardirq context and steal-time (when paravirt).

Then again, it might not want to use that.. but its something you might want to consider and make explicit by means of a comment.

Yes, I planned a consistency check for the use of clock/clock_task throughout the code, but it seems I then forgot it. Planned for the next iteration :-).

Thanks,

- Juri

maintainer
#35 lore raw

Re: [PATCH 05/16] sched: SCHED_DEADLINE policy implementation.

In reply to Juri Lelli

kernel/sched_dl.c line 114

Quoted 10 lines · Juri Lelli
kernel/sched_dl.c new file +10 -0
+       /*
+        * We Keep moving the deadline away until we get some
+        * available runtime for the entity. This ensures correct
+        * handling of situations where the runtime overrun is
+        * arbitrary large.
+        */
+       while (dl_se->runtime <= 0) {
+               dl_se->deadline += dl_se->dl_deadline;
+               dl_se->runtime += dl_se->dl_runtime;
+       } 

Does gcc 'optimize' that into a division? If so, it might need special glue to make it not do that.

maintainer
#40 lore raw

Re: [PATCH 05/16] sched: SCHED_DEADLINE policy implementation.

In reply to Juri Lelli

kernel/sched_dl.c line 126

Quoted 11 lines · Juri Lelli
kernel/sched_dl.c new file +11 -0
+       /*
+        * At this point, the deadline really should be "in
+        * the future" with respect to rq->clock. If it's
+        * not, we are, for some reason, lagging too much!
+        * Anyway, after having warn userspace abut that,
+        * we still try to keep the things running by
+        * resetting the deadline and the budget of the
+        * entity.
+        */
+       if (dl_time_before(dl_se->deadline, rq->clock)) {
+               WARN_ON_ONCE(1);

Doing printk() and friends from scheduler context isn't actually safe and can lock up your machine.. there's a printk_sched() that maybe-sorta-kinda can get your complaints out..

Quoted 3 lines
kernel/sched_dl.c new file +3 -0
+               dl_se->deadline = rq->clock + dl_se->dl_deadline;
+               dl_se->runtime = dl_se->dl_runtime;
+       } 
maintainer
#116 lore raw

Re: [PATCH 12/16] rtmutex: turn the plist into an rb-tree.

In reply to Peter Zijlstra
Quoted 9 lines · Peter Zijlstra
Quoted 5 lines · Steven Rostedt

I have to ask. Why not just add a rbtree with a plist? That is, add all deadline tasks to the rbtree and all others to the plist. As plist has a O(1) operation, and rbtree does not. We are making all RT tasks suffer the overhead of the rbtree.

You always love to add complexity to stuff before making it work, don't you ;-)

Nah, I like the make it work first, then optimize. But I wanted to bring this up as a concern. After benchmarks, it may not be an issue anyway. I just don't want to forget about doing the benchmarks.

-- Steve

maintainer
#42 lore raw

Re: [PATCH 05/16] sched: SCHED_DEADLINE policy implementation.

In reply to Juri Lelli
Quoted 8 lines · Juri Lelli

+/*
+ * Here we check if --at time t-- an entity (which is probably being
+ * [re]activated or, in general, enqueued) can use its remaining runtime
+ * and its current deadline _without_ exceeding the bandwidth it is
+ * assigned (function returns true if it can).
+ *
+ * For this to hold, we must check if:
+ * runtime / (deadline - t) < dl_runtime / dl_deadline .

It might be good to put a few words in as to why that is.. I know I always forget (but know where to find it by now), also might be good to refer those papers Tommaso listed when Steven asked this a while back.

Quoted 22 lines

+ */

kernel/sched_dl.c new file +19 -0
+static bool dl_entity_overflow(struct sched_dl_entity *dl_se, u64 t)
+{
+       u64 left, right;
+
+       /*
+        * left and right are the two sides of the equation above,
+        * after a bit of shuffling to use multiplications instead
+        * of divisions.
+        *
+        * Note that none of the time values involved in the two
+        * multiplications are absolute: dl_deadline and dl_runtime
+        * are the relative deadline and the maximum runtime of each
+        * instance, runtime is the runtime left for the last instance
+        * and (deadline - t), since t is rq->clock, is the time left
+        * to the (absolute) deadline. Therefore, overflowing the u64
+        * type is very unlikely to occur in both cases.
+        */
+       left = dl_se->dl_deadline * dl_se->runtime;
+       right = (dl_se->deadline - t) * dl_se->dl_runtime;
Quoted 1 line

From what I can see there are no constraints on the values in

__setparam_dl() so the above left term can be constructed to be an overflow.

Ideally we'd use u128 here, but I don't think people will let us :/

Quoted 2 lines
(quoted diff) +2 -0
+       return dl_time_before(right, left);
+} 
author
#41 lore raw

Re: [PATCH 05/16] sched: SCHED_DEADLINE policy implementation.

In reply to Peter Zijlstra
Quoted 13 lines · Peter Zijlstra

kernel/sched_dl.c line 126

Quoted 11 lines · Juri Lelli
kernel/sched_dl.c new file +11 -0
+       /*
+        * At this point, the deadline really should be "in
+        * the future" with respect to rq->clock. If it's
+        * not, we are, for some reason, lagging too much!
+        * Anyway, after having warn userspace abut that,
+        * we still try to keep the things running by
+        * resetting the deadline and the budget of the
+        * entity.
+        */
+       if (dl_time_before(dl_se->deadline, rq->clock)) {
+               WARN_ON_ONCE(1);
Quoted 4 lines

Doing printk() and friends from scheduler context isn't actually safe and can lock up your machine.. there's a printk_sched() that maybe-sorta-kinda can get your complaints out..

Thanks! I'll look at it.

Quoted 3 lines

kernel/sched_dl.c line 80

Quoted 3 lines
kernel/sched_dl.c new file +3 -0
+               dl_se->deadline = rq->clock + dl_se->dl_deadline;
+               dl_se->runtime = dl_se->dl_runtime;
+       }

- Juri

author
#36 lore raw

Re: [PATCH 05/16] sched: SCHED_DEADLINE policy implementation.

In reply to Peter Zijlstra
Quoted 12 lines · Peter Zijlstra

kernel/sched_dl.c line 114

Quoted 10 lines · Juri Lelli
kernel/sched_dl.c new file +10 -0
+       /*
+        * We Keep moving the deadline away until we get some
+        * available runtime for the entity. This ensures correct
+        * handling of situations where the runtime overrun is
+        * arbitrary large.
+        */
+       while (dl_se->runtime<= 0) {
+               dl_se->deadline += dl_se->dl_deadline;
+               dl_se->runtime += dl_se->dl_runtime;
+       }
Quoted 2 lines

Does gcc 'optimize' that into a division? If so, it might need special glue to make it not do that.

I got two adds and a jle, no div here..

Thanks,

- Juri

maintainer
#37 lore raw

Re: [PATCH 05/16] sched: SCHED_DEADLINE policy implementation.

Quoted 13 lines · Juri Lelli
Quoted 12 lines · Peter Zijlstra
Quoted 10 lines · Juri Lelli
kernel/sched_dl.c new file +10 -0
+       /*
+        * We Keep moving the deadline away until we get some
+        * available runtime for the entity. This ensures correct
+        * handling of situations where the runtime overrun is
+        * arbitrary large.
+        */
+       while (dl_se->runtime<= 0) {
+               dl_se->deadline += dl_se->dl_deadline;
+               dl_se->runtime += dl_se->dl_runtime;
+       }
Quoted 4 lines
Quoted 2 lines

Does gcc 'optimize' that into a division? If so, it might need special glue to make it not do that.

I got two adds and a jle, no div here..

Gcc is known to change such loops to something like:

if (runtime <= 0) {
tmp = 1 - runtime / dl_runtime;
deadline += tmp * dl_deadline;
runtime += tmp * dl_runtime;
}

author
#38 lore raw

Re: [PATCH 05/16] sched: SCHED_DEADLINE policy implementation.

Quoted 19 lines · Peter Zijlstra
Quoted 13 lines · Juri Lelli
Quoted 12 lines · Peter Zijlstra
Quoted 10 lines · Juri Lelli
kernel/sched_dl.c new file +10 -0
+       /*
+        * We Keep moving the deadline away until we get some
+        * available runtime for the entity. This ensures correct
+        * handling of situations where the runtime overrun is
+        * arbitrary large.
+        */
+       while (dl_se->runtime<= 0) {
+               dl_se->deadline += dl_se->dl_deadline;
+               dl_se->runtime += dl_se->dl_runtime;
+       }
Quoted 4 lines
Quoted 2 lines

Does gcc 'optimize' that into a division? If so, it might need special glue to make it not do that.

I got two adds and a jle, no div here..

Quoted 9 lines

Gcc is known to change such loops to something like:

  if (runtime<= 0) {
    tmp = 1 - runtime / dl_runtime;
    deadline += tmp * dl_deadline;
    runtime += tmp * dl_runtime;
  }

This is what I got for that snippet:

ffffffff81062826 <enqueue_task_dl>:
[...]

ffffffff81062885:       49 03 44 24 20          add    0x20(%r12),%rax
ffffffff8106288a:       49 8b 54 24 28          mov    0x28(%r12),%rdx
ffffffff8106288f:       49 01 54 24 38          add    %rdx,0x38(%r12)
ffffffff81062894:       49 89 44 24 30          mov    %rax,0x30(%r12)
ffffffff81062899:       49 8b 44 24 30          mov    0x30(%r12),%rax
ffffffff8106289e:       48 85 c0                test   %rax,%rax
ffffffff810628a1:       7e e2                   jle    ffffffff81062885 <enqueue_task_dl+0x5f>

So it seems we are fine in this case, right?
It is anyway better to enforce this Gcc behaviour, just to be on the safe side?

Thanks,

- Juri

maintainer
#39 lore raw

Re: [PATCH 05/16] sched: SCHED_DEADLINE policy implementation.

Quoted 13 lines · Juri Lelli

This is what I got for that snippet:

ffffffff81062826 <enqueue_task_dl>:
[...]

ffffffff81062885:       49 03 44 24 20          add    0x20(%r12),%rax
ffffffff8106288a:       49 8b 54 24 28          mov    0x28(%r12),%rdx
ffffffff8106288f:       49 01 54 24 38          add    %rdx,0x38(%r12)
ffffffff81062894:       49 89 44 24 30          mov    %rax,0x30(%r12)
ffffffff81062899:       49 8b 44 24 30          mov    0x30(%r12),%rax
ffffffff8106289e:       48 85 c0                test   %rax,%rax
ffffffff810628a1:       7e e2                   jle    ffffffff81062885 <enqueue_task_dl+0x5f>
Quoted 1 line

So it seems we are fine in this case, right?

Yep.

Quoted 2 lines

It is anyway better to enforce this Gcc behaviour, just to be on the safe side?

Dunno, the 'fix' is somewhat hideous (although we could make it suck less), we've only ever bothered with it if caused problems, so I guess we'll just wait and see until it breaks.

---
Subject: kernel,sched,time: Clean up gcc work-arounds
From: Peter Zijlstra <[email protected]>
Date: Mon Apr 23 15:55:48 CEST 2012

We've grown various copies of a particular gcc work-around, consolidate them into one and add a larger comment.

---
 include/linux/compiler.h |   12 ++++++++++++
 include/linux/math64.h   |    4 +---
 kernel/sched/core.c      |    8 ++------
 kernel/sched/fair.c      |    8 ++------
 kernel/time.c            |   11 ++++-------
 5 files changed, 21 insertions(+), 22 deletions(-)
include/linux/compiler.h +12 -0
@@ -310,4 +310,16 @@ void ftrace_likely_update(struct ftrace_
  */
 #define ACCESS_ONCE(x) (*(volatile typeof(x) *)&(x))
 
+/*
+ * Avoid gcc loop optimization by clobbering a variable, forcing a reload
+ * and invalidating the optimization.
+ *
+ * The optimization in question transforms various loops into divisions/modulo
+ * operations, this is a problem when either the resulting operation generates
+ * unimplemented libgcc functions (u64 divisions for example) or the loop is
+ * known not to contain a lot of iterations and the division is in fact more
+ * expensive.
+ */
+#define __gcc_dont_optimize_loop(var) asm("" "+rm" (var))
+
 #endif /* __LINUX_COMPILER_H */
include/linux/math64.h +1 -3
@@ -105,9 +105,7 @@ __iter_div_u64_rem(u64 dividend, u32 div
 	u32 ret = 0;
 
 	while (dividend >= divisor) {
-		/* The following asm() prevents the compiler from
-		   optimising this loop into a modulo operation.  */
-		asm("" : "+rm"(dividend));
+		__gcc_dont_optimize_loop(dividend);
 
 		dividend -= divisor;
 		ret++;
kernel/sched/core.c +2 -6
@@ -628,12 +628,8 @@ void sched_avg_update(struct rq *rq)
 	s64 period = sched_avg_period();
 
 	while ((s64)(rq->clock - rq->age_stamp) > period) {
-		/*
-		 * Inline assembly required to prevent the compiler
-		 * optimising this loop into a divmod call.
-		 * See __iter_div_u64_rem() for another example of this.
-		 */
-		asm("" : "+rm" (rq->age_stamp));
+		__gcc_dont_optimize_loop(rq->age_stamp);
+
 		rq->age_stamp += period;
 		rq->rt_avg /= 2;
 	}
kernel/sched/fair.c +2 -6
@@ -853,12 +853,8 @@ static void update_cfs_load(struct cfs_r
 		update_cfs_rq_load_contribution(cfs_rq, global_update);
 
 	while (cfs_rq->load_period > period) {
-		/*
-		 * Inline assembly required to prevent the compiler
-		 * optimising this loop into a divmod call.
-		 * See __iter_div_u64_rem() for another example of this.
-		 */
-		asm("" : "+rm" (cfs_rq->load_period));
+		__gcc_dont_optimize_loop(cfs_rq->load_period);
+
 		cfs_rq->load_period /= 2;
 		cfs_rq->load_avg /= 2;
 	}
kernel/time.c +4 -7
@@ -349,17 +349,14 @@ EXPORT_SYMBOL(mktime);
 void set_normalized_timespec(struct timespec *ts, time_t sec, s64 nsec)
 {
 	while (nsec >= NSEC_PER_SEC) {
-		/*
-		 * The following asm() prevents the compiler from
-		 * optimising this loop into a modulo operation. See
-		 * also __iter_div_u64_rem() in include/linux/time.h
-		 */
-		asm("" : "+rm"(nsec));
+		__gcc_dont_optimize_loop(nsec);
+
 		nsec -= NSEC_PER_SEC;
 		++sec;
 	}
 	while (nsec < 0) {
-		asm("" : "+rm"(nsec));
+		__gcc_dont_optimize_loop(nsec);
+
 		nsec += NSEC_PER_SEC;
 		--sec;
 	}
maintainer
#50 lore raw

Re: [PATCH 05/16] sched: SCHED_DEADLINE policy implementation.

In reply to Juri Lelli
Quoted 8 lines · Juri Lelli
kernel/sched_dl.c new file +8 -0
+static int start_dl_timer(struct sched_dl_entity *dl_se)
+{
+       struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
+       struct rq *rq = rq_of_dl_rq(dl_rq);
+       ktime_t now, act;
+       ktime_t soft, hard;
+       unsigned long range;
+       s64 delta;

kernel/sched_dl.c line 221

Quoted 10 lines
kernel/sched_dl.c new file +10 -0
+
+       /*
+        * We want the timer to fire at the deadline, but considering
+        * that it is actually coming from rq->clock and not from
+        * hrtimer's time base reading.
+        */
+       act = ns_to_ktime(dl_se->deadline);
+       now = hrtimer_cb_get_time(&dl_se->dl_timer);
+       delta = ktime_to_ns(now) - rq->clock;
+       act = ktime_add_ns(act, delta);

Right, this all is very sad.. but I guess we'll have to like live with it. The only other option is adding another timer base that tries to keep itself in sync with rq->clock but that all sounds very painful indeed.

Keeping up with rq->clock_task would be even more painful since it slows the clock down in random fashion making the timer fire early. Compensating that is going to be both fun and expensive.

Quoted 8 lines
kernel/sched_dl.c new file +8 -0
+       /*
+        * If the expiry time already passed, e.g., because the value
+        * chosen as the deadline is too small, don't even try to
+        * start the timer in the past!
+        */
+       if (ktime_us_delta(act, now) < 0)
+               return 0;
+

kernel/sched_dl.c line 240

Quoted 10 lines
kernel/sched_dl.c new file +10 -0
+       hrtimer_set_expires(&dl_se->dl_timer, act);
+
+       soft = hrtimer_get_softexpires(&dl_se->dl_timer);
+       hard = hrtimer_get_expires(&dl_se->dl_timer);
+       range = ktime_to_ns(ktime_sub(hard, soft));
+       __hrtimer_start_range_ns(&dl_se->dl_timer, soft,
+                                range, HRTIMER_MODE_ABS, 0);
+
+       return hrtimer_active(&dl_se->dl_timer);
+} 

/me reminds himself to make __hrtimer_start_range_ns() return -ETIME.

maintainer
#51 lore raw

Re: [PATCH 05/16] sched: SCHED_DEADLINE policy implementation.

In reply to Juri Lelli
Quoted 4 lines · Juri Lelli

+/*
+ * This is the bandwidth enforcement timer callback. If here, we know
+ * a task is not on its dl_rq, since the fact that the timer was running
+ * means the task is throttled and needs a runtime replenishment.

Quoted 8 lines

+ *
+ * However, what we actually do depends on the fact the task is active,
+ * (it is on its rq) or has been removed from there by a call to
+ * dequeue_task_dl(). In the former case we must issue the runtime
+ * replenishment and add the task back to the dl_rq; in the latter, we just
+ * do nothing but clearing dl_throttled, so that runtime and deadline
+ * updating (and the queueing back to dl_rq) will be done by the
+ * next call to enqueue_task_dl().

OK, so that comment isn't entirely clear to me, how can that timer still be active when the task isn't? You start the timer when you throttle it, at that point it cannot in fact dequeue itself anymore.

The only possibility I see is the one mentioned with the dl_task() check below, that someone else called sched_setscheduler() on it.

Quoted 16 lines

+ */

kernel/sched_dl.c new file +15 -0
+static enum hrtimer_restart dl_task_timer(struct hrtimer *timer)
+{
+       unsigned long flags;
+       struct sched_dl_entity *dl_se = container_of(timer,
+                                                    struct sched_dl_entity,
+                                                    dl_timer);
+       struct task_struct *p = dl_task_of(dl_se);
+       struct rq *rq = task_rq_lock(p, &flags);
+
+       /*
+        * We need to take care of a possible races here. In fact, the
+        * task might have changed its scheduling policy to something
+        * different from SCHED_DEADLINE (through sched_setscheduler()).
+        */
+       if (!dl_task(p))

kernel/sched_dl.c line 279

Quoted 10 lines
kernel/sched_dl.c new file +10 -0
+               goto unlock;
+
+       dl_se->dl_throttled = 0;
+       if (p->on_rq) {
+               enqueue_task_dl(rq, p, ENQUEUE_REPLENISH);
+               if (task_has_dl_policy(rq->curr))
+                       check_preempt_curr_dl(rq, p, 0);
+               else
+                       resched_task(rq->curr);
+       }

So I can't see how that cannot be true.

Quoted 5 lines
kernel/sched_dl.c new file +5 -0
+unlock:
+       task_rq_unlock(rq, p, &flags);
+
+       return HRTIMER_NORESTART;
+} 
maintainer
#53 lore raw

Re: [PATCH 05/16] sched: SCHED_DEADLINE policy implementation.

In reply to Juri Lelli

kernel/sched_dl.c line 293

Quoted 8 lines · Juri Lelli
kernel/sched_dl.c new file +8 -0
+static void init_dl_task_timer(struct sched_dl_entity *dl_se)
+{
+       struct hrtimer *timer = &dl_se->dl_timer;
+
+       if (hrtimer_active(timer)) {
+               hrtimer_try_to_cancel(timer);
+               return;
+       }

Same question I guess, how can it be active here? Also, just letting it run doesn't seem like the best way out..

Quoted 5 lines
kernel/sched_dl.c new file +5 -0
+
+       hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
+       timer->function = dl_task_timer;
+       timer->irqsafe = 1;
+} 
author
#43 lore raw

Re: [PATCH 05/16] sched: SCHED_DEADLINE policy implementation.

In reply to Peter Zijlstra
Quoted 10 lines · Peter Zijlstra
Quoted 8 lines · Juri Lelli

+/*
+ * Here we check if --at time t-- an entity (which is probably being
+ * [re]activated or, in general, enqueued) can use its remaining runtime
+ * and its current deadline _without_ exceeding the bandwidth it is
+ * assigned (function returns true if it can).
+ *
+ * For this to hold, we must check if:
+ * runtime / (deadline - t)< dl_runtime / dl_deadline .

Quoted 4 lines

It might be good to put a few words in as to why that is.. I know I always forget (but know where to find it by now), also might be good to refer those papers Tommaso listed when Steven asked this a while back.

Ok, I'll fix the comment, extend it and add T.'s references in the Documentation.

Quoted 17 lines
Quoted 17 lines

+ */

kernel/sched_dl.c new file +16 -0
+static bool dl_entity_overflow(struct sched_dl_entity *dl_se, u64 t)
+{
+       u64 left, right;
+
+       /*
+        * left and right are the two sides of the equation above,
+        * after a bit of shuffling to use multiplications instead
+        * of divisions.
+        *
+        * Note that none of the time values involved in the two
+        * multiplications are absolute: dl_deadline and dl_runtime
+        * are the relative deadline and the maximum runtime of each
+        * instance, runtime is the runtime left for the last instance
+        * and (deadline - t), since t is rq->clock, is the time left
+        * to the (absolute) deadline. Therefore, overflowing the u64
+        * type is very unlikely to occur in both cases.
Quoted 9 lines

kernel/sched_dl.c line 159

Quoted 3 lines
kernel/sched_dl.c new file +3 -0
+        */
+       left = dl_se->dl_deadline * dl_se->runtime;
+       right = (dl_se->deadline - t) * dl_se->dl_runtime;

From what I can see there are no constraints on the values in
__setparam_dl() so the above left term can be constructed to be an overflow.

Yes, could happen :-\.

Quoted 2 lines

Ideally we'd use u128 here, but I don't think people will let us :/

Do we need to do something about that? If we cannot go for bigger space probably limit dl_deadline (or warn the user)..

Quoted 2 lines
Quoted 2 lines
(quoted diff) +2 -0
+       return dl_time_before(right, left);
+}
maintainer
#44 lore raw

Re: [PATCH 05/16] sched: SCHED_DEADLINE policy implementation.

Quoted 6 lines · Juri Lelli
Quoted 5 lines

From what I can see there are no constraints on the values in
__setparam_dl() so the above left term can be constructed to be an overflow.

Quoted 7 lines

Yes, could happen :-\.

Quoted 2 lines

Ideally we'd use u128 here, but I don't think people will let us :/

Do we need to do something about that? If we cannot go for bigger space probably limit dl_deadline (or warn the user)..

Depends on what happens, if only this task gets screwy, no real problem, they supplied funny input, they get funny output. If OTOH it affects other tasks we should do something.

Ideally we'd avoid the situation by some clever maths, second best would be rejecting the parameters up front.

maintainer
#61 lore raw

Re: [PATCH 05/16] sched: SCHED_DEADLINE policy implementation.

In reply to Juri Lelli
Quoted 9 lines · Juri Lelli
kernel/sched_dl.c new file +9 -0
+static
+int dl_runtime_exceeded(struct rq *rq, struct sched_dl_entity *dl_se)
+{
+       int dmiss = dl_time_before(dl_se->deadline, rq->clock);
+       int rorun = dl_se->runtime <= 0;
+
+       if (!rorun && !dmiss)
+               return 0;
+

kernel/sched_dl.c line 318

Quoted 10 lines
kernel/sched_dl.c new file +10 -0
+       /*
+        * If we are beyond our current deadline and we are still
+        * executing, then we have already used some of the runtime of
+        * the next instance. Thus, if we do not account that, we are
+        * stealing bandwidth from the system at each deadline miss!
+        */
+       if (dmiss) {
+               dl_se->runtime = rorun ? dl_se->runtime : 0;
+               dl_se->runtime -= rq->clock - dl_se->deadline;
+       }

So ideally this can't happen, but since we already leak time from the system through means of hardirq / kstop / context-switch-overhead / clock-jitter etc.. we avoid the error accumulating?

Quoted 3 lines
kernel/sched_dl.c new file +3 -0
+
+       return 1;
+} 
author
#52 lore raw

Re: [PATCH 05/16] sched: SCHED_DEADLINE policy implementation.

In reply to Peter Zijlstra
Quoted 14 lines · Peter Zijlstra
Quoted 4 lines · Juri Lelli

+/*
+ * This is the bandwidth enforcement timer callback. If here, we know
+ * a task is not on its dl_rq, since the fact that the timer was running
+ * means the task is throttled and needs a runtime replenishment.

Quoted 8 lines

+ *
+ * However, what we actually do depends on the fact the task is active,
+ * (it is on its rq) or has been removed from there by a call to
+ * dequeue_task_dl(). In the former case we must issue the runtime
+ * replenishment and add the task back to the dl_rq; in the latter, we just
+ * do nothing but clearing dl_throttled, so that runtime and deadline
+ * updating (and the queueing back to dl_rq) will be done by the
+ * next call to enqueue_task_dl().

Quoted 7 lines

OK, so that comment isn't entirely clear to me, how can that timer still be active when the task isn't? You start the timer when you throttle it, at that point it cannot in fact dequeue itself anymore.

The only possibility I see is the one mentioned with the dl_task() check below, that someone else called sched_setscheduler() on it.

Ok, I was also stuck at this point when I first reviewed v3. Then I convinced myself that, even if probably always true, the p->on_rq check would prevent weird situations like for
example: by the time I block on a mutex, go to sleep or whatever, I am throttled, then the dl_timer fires and I'm still !on_rq. But I didn't see this happening ever actually...

Quoted 34 lines
Quoted 16 lines

+ */

kernel/sched_dl.c new file +15 -0
+static enum hrtimer_restart dl_task_timer(struct hrtimer *timer)
+{
+       unsigned long flags;
+       struct sched_dl_entity *dl_se = container_of(timer,
+                                                    struct sched_dl_entity,
+                                                    dl_timer);
+       struct task_struct *p = dl_task_of(dl_se);
+       struct rq *rq = task_rq_lock(p,&flags);
+
+       /*
+        * We need to take care of a possible races here. In fact, the
+        * task might have changed its scheduling policy to something
+        * different from SCHED_DEADLINE (through sched_setscheduler()).
+        */
+       if (!dl_task(p))

kernel/sched_dl.c line 279

Quoted 10 lines
kernel/sched_dl.c new file +10 -0
+               goto unlock;
+
+       dl_se->dl_throttled = 0;
+       if (p->on_rq) {
+               enqueue_task_dl(rq, p, ENQUEUE_REPLENISH);
+               if (task_has_dl_policy(rq->curr))
+                       check_preempt_curr_dl(rq, p, 0);
+               else
+                       resched_task(rq->curr);
+       }

So I can't see how that cannot be true.

Quoted 5 lines
kernel/sched_dl.c new file +5 -0
+unlock:
+       task_rq_unlock(rq, p,&flags);
+
+       return HRTIMER_NORESTART;
+}
author
#62 lore raw

Re: [PATCH 05/16] sched: SCHED_DEADLINE policy implementation.

In reply to Peter Zijlstra
Quoted 16 lines · Peter Zijlstra
Quoted 15 lines · Juri Lelli
kernel/sched_dl.c new file +15 -0
+static
+int dl_runtime_exceeded(struct rq *rq, struct sched_dl_entity *dl_se)
+{
+       int dmiss = dl_time_before(dl_se->deadline, rq->clock);
+       int rorun = dl_se->runtime<= 0;
+
+       if (!rorun&&  !dmiss)
+               return 0;
+
+       /*
+        * If we are beyond our current deadline and we are still
+        * executing, then we have already used some of the runtime of
+        * the next instance. Thus, if we do not account that, we are
+        * stealing bandwidth from the system at each deadline miss!
+        */
Quoted 9 lines

kernel/sched_dl.c line 318

Quoted 4 lines
kernel/sched_dl.c new file +4 -0
+       if (dmiss) {
+               dl_se->runtime = rorun ? dl_se->runtime : 0;
+               dl_se->runtime -= rq->clock - dl_se->deadline;
+       }

So ideally this can't happen, but since we already leak time from the system through means of hardirq / kstop / context-switch-overhead / clock-jitter etc.. we avoid the error accumulating?

Yep, seems fair :-).

Quoted 5 lines

kernel/sched_dl.c line 321

Quoted 3 lines
kernel/sched_dl.c new file +3 -0
+
+       return 1;
+}

Thanks,

- Juri

author
#54 lore raw

Re: [PATCH 05/16] sched: SCHED_DEADLINE policy implementation.

In reply to Peter Zijlstra
Quoted 13 lines · Peter Zijlstra

kernel/sched_dl.c line 293

Quoted 8 lines · Juri Lelli
kernel/sched_dl.c new file +8 -0
+static void init_dl_task_timer(struct sched_dl_entity *dl_se)
+{
+       struct hrtimer *timer =&dl_se->dl_timer;
+
+       if (hrtimer_active(timer)) {
+               hrtimer_try_to_cancel(timer);
+               return;
+       }

Same question I guess, how can it be active here? Also, just letting it run doesn't seem like the best way out..

Probably s/hrtimer_try_to_cancel/hrtimer_cancel is better.

Quoted 5 lines

kernel/sched_dl.c line 298

Quoted 5 lines
kernel/sched_dl.c new file +5 -0
+
+       hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
+       timer->function = dl_task_timer;
+       timer->irqsafe = 1;
+}

Thanks,

- Juri

maintainer
#55 lore raw

Re: [PATCH 05/16] sched: SCHED_DEADLINE policy implementation.

Quoted 15 lines · Juri Lelli
Quoted 13 lines · Peter Zijlstra

kernel/sched_dl.c line 293

Quoted 8 lines · Juri Lelli
kernel/sched_dl.c new file +8 -0
+static void init_dl_task_timer(struct sched_dl_entity *dl_se)
+{
+       struct hrtimer *timer =&dl_se->dl_timer;
+
+       if (hrtimer_active(timer)) {
+               hrtimer_try_to_cancel(timer);
+               return;
+       }

Same question I guess, how can it be active here? Also, just letting it run doesn't seem like the best way out..

Quoted 1 line

Probably s/hrtimer_try_to_cancel/hrtimer_cancel is better.

Yeah, not sure you can do hrtimer_cancel() there though, you're holding ->pi_lock and rq->lock and have IRQs disabled. That sounds like asking for trouble.

Anyway, if it can't happen, we don't have to fix it.. so lets answer that first ;-)

author
#56 lore raw

Re: [PATCH 05/16] sched: SCHED_DEADLINE policy implementation.

Quoted 16 lines · Peter Zijlstra
Quoted 15 lines · Juri Lelli
Quoted 13 lines · Peter Zijlstra

kernel/sched_dl.c line 293

Quoted 8 lines · Juri Lelli
kernel/sched_dl.c new file +8 -0
+static void init_dl_task_timer(struct sched_dl_entity *dl_se)
+{
+       struct hrtimer *timer =&dl_se->dl_timer;
+
+       if (hrtimer_active(timer)) {
+               hrtimer_try_to_cancel(timer);
+               return;
+       }

Same question I guess, how can it be active here? Also, just letting it run doesn't seem like the best way out..

Quoted 8 lines
Quoted 1 line

Probably s/hrtimer_try_to_cancel/hrtimer_cancel is better.

Yeah, not sure you can do hrtimer_cancel() there though, you're holding ->pi_lock and rq->lock and have IRQs disabled. That sounds like asking for trouble.

Anyway, if it can't happen, we don't have to fix it.. so lets answer that first ;-)

The user could call __setparam_dl on a throttled task through __sched_setscheduler.

BTW, I noticed that we should change this (inside __sched_setscheduler):

        /*
         * If not changing anything there's no need to proceed further
         */
        if (unlikely(policy == p->policy && (!rt_policy(policy) ||
                        param->sched_priority == p->rt_priority))) {

                __task_rq_unlock(rq);
                raw_spin_unlock_irqrestore(&p->pi_lock, flags);
                return 0;
        }

to something like this:

	if (unlikely(policy == p->policy && (!rt_policy(policy) ||
                        param->sched_priority == p->rt_priority) &&
			!dl_policy(policy)))

Thanks,

- Juri

#30 lore raw

Re: [PATCH 05/16] sched: SCHED_DEADLINE policy implementation.

In reply to Juri Lelli

Il 23/04/2012 11:37, Juri Lelli ha scritto:

Quoted 7 lines · Peter Zijlstra
Quoted 6 lines · Juri Lelli
Quoted 1 line

+ dl_se->deadline = rq->clock + dl_se->dl_deadline;

You might want to use rq->clock_task, this clock excludes times spend in hardirq context and steal-time (when paravirt).

Quoted 6 lines
Quoted 2 lines

Then again, it might not want to use that.. but its something you might want to consider and make explicit by means of a comment.

Yes, I planned a consistency check for the use of clock/clock_task throughout the code, but it seems I then forgot it. Planned for the next iteration :-).

unless I'm mistaken, there are 3 repetitions of this block in 05/16:

+ dl_se->deadline = rq->clock + dl_se->dl_deadline;
+ dl_se->runtime = dl_se->dl_runtime;

perhaps enclosing them into a function (e.g., reset_from_now() or similar) may help to keep consistency...

Another thing: I cannot get the real difference between rq->clock and rq->task_clock.
If task_clock is a kind of CLOCK_MONOTONIC thing that increases only when the task (or any task) is scheduled, then you don't want to use that here. Here you need to set the new ->deadline to an absolute time, so I guess the regular rq->clock is what you need, isn't it ?

Hope I didn't say too much nonsense.

    T.
Signature · 4 lines
Tommaso Cucinotta, Computer Engineering PhD, Researcher
ReTiS Lab, Scuola Superiore Sant'Anna, Pisa, Italy
Tel +39 050 882 024, Fax +39 050 882 003
http://retis.sssup.it/people/tommaso
maintainer
#31 lore raw

Re: [PATCH 05/16] sched: SCHED_DEADLINE policy implementation.

Quoted 1 line · Tommaso Cucinotta

I cannot get the real difference between rq->clock and rq->task_clock.

One runs at wall-time (rq->clock) the other excludes time in irq-context and steal-time (rq->clock_task).

The idea is that ->clock_task gives the time as observed by schedulable tasks and excludes other muck.

#45 lore raw

Re: [PATCH 05/16] sched: SCHED_DEADLINE policy implementation.

In reply to Peter Zijlstra

Il 23/04/2012 12:55, Peter Zijlstra ha scritto:

Quoted 38 lines · Juri Lelli
Quoted 8 lines

+/*
+ * Here we check if --at time t-- an entity (which is probably being
+ * [re]activated or, in general, enqueued) can use its remaining runtime
+ * and its current deadline _without_ exceeding the bandwidth it is
+ * assigned (function returns true if it can).
+ *
+ * For this to hold, we must check if:
+ * runtime / (deadline - t)< dl_runtime / dl_deadline .

It might be good to put a few words in as to why that is.. I know I always forget (but know where to find it by now), also might be good to refer those papers Tommaso listed when Steven asked this a while back.

Quoted 10 lines

+ */

kernel/sched_dl.c new file +9 -0
+static bool dl_entity_overflow(struct sched_dl_entity *dl_se, u64 t)
+{
+       u64 left, right;
+
+       /*
+        * left and right are the two sides of the equation above,
+        * after a bit of shuffling to use multiplications instead
+        * of divisions.
+        *

kernel/sched_dl.c line 159

Quoted 10 lines
kernel/sched_dl.c new file +10 -0
+        * Note that none of the time values involved in the two
+        * multiplications are absolute: dl_deadline and dl_runtime
+        * are the relative deadline and the maximum runtime of each
+        * instance, runtime is the runtime left for the last instance
+        * and (deadline - t), since t is rq->clock, is the time left
+        * to the (absolute) deadline. Therefore, overflowing the u64
+        * type is very unlikely to occur in both cases.
+        */
+       left = dl_se->dl_deadline * dl_se->runtime;
+       right = (dl_se->deadline - t) * dl_se->dl_runtime;

From what I can see there are no constraints on the values in
__setparam_dl() so the above left term can be constructed to be an overflow.

Quoted 1 line

Ideally we'd use u128 here, but I don't think people will let us :/

why not write this straight in asm, i.e., multiply 64*64 then divide by 64 keeping the intermediate result on 128 bits?
Something straightforward to write in asm, but not that easy to let gcc understand that I don't want to multiply 128*128 :-).... a few years ago I had a similar issue; perhaps it was a 32/64 version of this problem, and gcc was not optimizing properly the C code with -O3, so I had used asm segments. In this case, if avoiding the division is a major requirement, then we could multiply twice 64*64 in asm, then compare the two results on 128 bits ? Again, a few assembly lines on architectures supporting the 64*64 and 128-bits comparison.

    T.
Signature · 4 lines
Tommaso Cucinotta, Computer Engineering PhD, Researcher
ReTiS Lab, Scuola Superiore Sant'Anna, Pisa, Italy
Tel +39 050 882 024, Fax +39 050 882 003
http://retis.sssup.it/people/tommaso
maintainer
#46 lore raw

Re: [PATCH 05/16] sched: SCHED_DEADLINE policy implementation.

Quoted 2 lines · Tommaso Cucinotta

why not write this straight in asm, i.e., multiply 64*64 then divide by 64 keeping the intermediate result on 128 bits?

If you know of a way to do this for all 30 odd architectures supported by our beloved kernel, do let me know ;-)

Yes I can do it for x86_64, but people tend to get mighty upset if you break the compile for all other arches...

#47 lore raw

Re: [PATCH 05/16] sched: SCHED_DEADLINE policy implementation.

Il 23/04/2012 22:58, Peter Zijlstra ha scritto:

Quoted 5 lines · Tommaso Cucinotta
Quoted 2 lines

why not write this straight in asm, i.e., multiply 64*64 then divide by 64 keeping the intermediate result on 128 bits?

If you know of a way to do this for all 30 odd architectures supported by our beloved kernel, do let me know ;-)

:-)

Quoted 2 lines

Yes I can do it for x86_64, but people tend to get mighty upset if you break the compile for all other arches...

rather than breaking compile, I was thinking more of using the optimization for a more accurate comparison on archs that have 64-bit mul and 128-bit cmp, and leaving the overflow on other archs. Though, that would imply a difference in behavior on those borderline cases (very big periods I guess).

However, I'm also puzzled from what would happen by compiling the current code on mostly 16-bit micros which have very limited 32-bit operations...

    T.
Signature · 4 lines
Tommaso Cucinotta, Computer Engineering PhD, Researcher
ReTiS Lab, Scuola Superiore Sant'Anna, Pisa, Italy
Tel +39 050 882 024, Fax +39 050 882 003
http://retis.sssup.it/people/tommaso
#32 lore raw

Re: [PATCH 05/16] sched: SCHED_DEADLINE policy implementation.

In reply to Peter Zijlstra

Il 23/04/2012 22:45, Peter Zijlstra ha scritto:

Quoted 7 lines · Tommaso Cucinotta
Quoted 1 line

I cannot get the real difference between rq->clock and rq->task_clock.

One runs at wall-time (rq->clock) the other excludes time in irq-context and steal-time (rq->clock_task).

The idea is that ->clock_task gives the time as observed by schedulable tasks and excludes other muck.

so clock_task might be better to compute the consumed budget at task deschedule, but for setting deadlines one period ahead in the future guess the regular wall-time rq->clock is the one to be used?

Thx,

    T.
Signature · 4 lines
Tommaso Cucinotta, Computer Engineering PhD, Researcher
ReTiS Lab, Scuola Superiore Sant'Anna, Pisa, Italy
Tel +39 050 882 024, Fax +39 050 882 003
http://retis.sssup.it/people/tommaso
maintainer
#49 lore raw

Re: [PATCH 05/16] sched: SCHED_DEADLINE policy implementation.

In reply to Peter Zijlstra
Quoted 9 lines · Peter Zijlstra
Quoted 2 lines · Tommaso Cucinotta

why not write this straight in asm, i.e., multiply 64*64 then divide by 64 keeping the intermediate result on 128 bits?

If you know of a way to do this for all 30 odd architectures supported by our beloved kernel, do let me know ;-)

Yes I can do it for x86_64, but people tend to get mighty upset if you break the compile for all other arches...

Use the draconian method. Make SCHED_DEADLINE dependent on "ARCH_HAS_128_MULT" and any arch maintainer that wants SCHED_DEADLINE for their arch will be responsible for implementing it ;-)

-- Steve

#33 lore raw

Re: [PATCH 05/16] sched: SCHED_DEADLINE policy implementation.

In reply to Tommaso Cucinotta
Quoted 7 lines · Tommaso Cucinotta
Quoted 2 lines

The idea is that ->clock_task gives the time as observed by schedulable tasks and excludes other muck.

so clock_task might be better to compute the consumed budget at task deschedule, but for setting deadlines one period ahead in the future guess the regular wall-time rq->clock is the one to be used?

Yep, that was the idea, unless my recollection has completely gone flaky! :-P

Perhaps adding a comment saying right this thing above, as Peter suggested?

Regards,
Dario

Signature · 4 lines
<<This happens because I choose it to happen!>> (Raistlin Majere)
-----------------------------------------------------------------
Dario Faggioli, Ph.D, http://retis.sssup.it/people/faggioli
Senior Software Engineer, Citrix Systems R&D Ltd., Cambridge (UK)
author
#34 lore raw

Re: [PATCH 05/16] sched: SCHED_DEADLINE policy implementation.

Quoted 8 lines · Dario Faggioli
Quoted 7 lines · Tommaso Cucinotta
Quoted 2 lines

The idea is that ->clock_task gives the time as observed by schedulable tasks and excludes other muck.

so clock_task might be better to compute the consumed budget at task deschedule, but for setting deadlines one period ahead in the future guess the regular wall-time rq->clock is the one to be used?

Quoted 6 lines

Yep, that was the idea, unless my recollection has completely gone flaky! :-P

Perhaps adding a comment saying right this thing above, as Peter suggested?

Sure! TODO for the next release :-).

Thanks,

- Juri

author
#58 lore raw

Re: [PATCH 05/16] sched: SCHED_DEADLINE policy implementation.

In reply to a message not in the archive
Quoted 10 lines · Tommaso Cucinotta

Il 23/04/2012 17:41, Juri Lelli ha scritto:

Quoted 2 lines

The user could call __setparam_dl on a throttled task through __sched_setscheduler.

in case it can be related: a scenario that used to break isolation (in the old aquosa crap): 1) create a deadline task 2) (actively) wait till it's just about to be throttled 3) remove reservation (i.e., return the task to the normal system policy and destroy reservation info in the kernel) 4) reserve it again

Yes, this is very similar to what I thought just after I've sent the email (ouch! :-)).

Quoted 8 lines

Assuming the borderline condition of a nearly fully saturated system, if 3)-4) manage to happen sufficiently close to each other and right after 2), now the task budget is refilled with a deadline which is where it should not be, according to the admission control rules. In other words, we may break guarantees of other tasks by a properly misbehaving task. Something relevant when considering misbehaviour and admission control from a security perspective [1].

Thanks for the ref., I'll read it!

Quoted 8 lines

At that time, I was persuaded that the right way to avoid this would be to avoid to free system cpu bw immediately when a reservation is destroyed, but rather wait till its current abs deadline, then "free" the bandwidth. A new task trying to re-create the reservation too early, i.e., at step 4) above, would be rejected by the system as it would still see a fully occupied cpu bw. Never implemented of course :-)...

A kind of "two steps" approach. It would work, I just have to think how to implement it (and let the system survive ;-)). Then create some bench to test it.

Quoted 7 lines

And also, from a security perspective, a misbehaving (sched_other) task might thrash the system with useless nansosleeps forcing the OS to continuously schedule/deschedule it. Equivalently, with a deadline scheduler, you could try to set a very small period/deadline. That's why in [1], among the configurable variables, there was a minimum allowed reservation period.

Yes, this should be easily controlled at admission time.

Quoted 3 lines

Nothing really urgent, just something you might want to keep in mind for the future, I thought.

Well, depends on how much effort will this turn to require. I personally would prefer to be able to come out with a new release ASAP. Just to continue the discussion with the most of the comments addressed and a more updated code (I also have a mainline version of the patchset quite ready).

Thanks a lot,

- Juri

maintainer
#59 lore raw

Re: [PATCH 05/16] sched: SCHED_DEADLINE policy implementation.

Quoted 5 lines · Juri Lelli

Well, depends on how much effort will this turn to require. I personally would prefer to be able to come out with a new release ASAP. Just to continue the discussion with the most of the comments addressed and a more updated code (I also have a mainline version of the patchset quite ready).

Right, one thing we can initially do is require root for using SCHED_DEADLINE and then when later work closes all the holes and we've added user bandwidth controls we can allow everybody in.

maintainer
#48 lore raw

Re: [PATCH 05/16] sched: SCHED_DEADLINE policy implementation.

In reply to Tommaso Cucinotta
Quoted 9 lines · Tommaso Cucinotta
Quoted 2 lines

Yes I can do it for x86_64, but people tend to get mighty upset if you break the compile for all other arches...

rather than breaking compile, I was thinking more of using the optimization for a more accurate comparison on archs that have 64-bit mul and 128-bit cmp, and leaving the overflow on other archs. Though, that would imply a difference in behavior on those borderline cases (very big periods I guess).

Quoted 3 lines

However, I'm also puzzled from what would happen by compiling the current code on mostly 16-bit micros which have very limited 32-bit operations...

We don't support 16bit archs, 32bit is almost useless as it is :-)

Anyway, how about something like this, I guess archs can go wild and add asm/math128.h if they want etc..

Completely untested, hasn't even seen a compiler..

---
Subject: math128: Add {add,mult,cmp}_u128
From: Peter Zijlstra <[email protected]>
Date: Tue Apr 24 11:47:12 CEST 2012

---
 include/linux/math128.h |   75 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 75 insertions(+)
include/linux/math128.h new file +75 -0
@@ -0,0 +1,75 @@
+#ifndef _LINUX_MATH128_H
+#define _LINUX_MATH128_H
+
+#include <linux/types.h>
+
+typedef struct {
+	u64 hi, lo;
+} u128;
+
+u128 add_u128(u128 a, u128 b)
+{
+	u128 res;
+
+	res.hi = a.hi + b.hi;
+	res.lo = a.lo + b.lo;
+
+	if (res.lo < a.lo || res.lo < b.lo)
+		res.hi++;
+
+	return res;
+}
+
+/*
+ * a * b = (ah * 2^32 + al) * (bh * 2^32 + bl) =
+ *   ah*bh * 2^64 + (ah*bl + bh*al) * 2^32 + al*bl
+ */
+u128 mult_u128(u64 a, u64 b)
+{
+	u128 res;
+	u64 ah, al;
+	u64 bh, bl;
+	u128 t1, t2, t3, t4;
+
+	ah = a >> 32;
+	al = a & ((1ULL << 32) - 1);
+
+	bh = b >> 32;
+	bl = b & ((1ULL << 32) - 1);
+
+	t1.lo = 0;
+	t1.hi = ah * bh;
+
+	t2.lo = ah * bl;
+	t2.hi = t2.lo >> 32;
+	t2.lo <<= 32;
+
+	t3.lo = al * bh;
+	t3.hi = t3.lo >> 32;
+	t3.lo <<= 32;
+
+	t4.lo = al * bl;
+	t4.hi = 0;
+
+	res = add_u128(t1, t2);
+	res = add_u128(res, t3);
+	res = add_u128(res, t4);
+
+	return res;
+}
+
+int cmp_u128(u128 a, u128 b)
+{
+	if (a.hi > b.hi)
+		return 1;
+	if (a.hi < b.hi)
+		return -1;
+	if (a.lo > b.lo)
+		return 1;
+	if (a.lo < b.lo)
+		return -1;
+
+	return 0;
+}
+
+#endif /* _LINUX_MATH128_H */
maintainer
#87 lore raw

Re: [PATCH 06/16] sched: SCHED_DEADLINE push and pull logic

In reply to Steven Rostedt
Quoted 18 lines · Steven Rostedt

kernel/sched_dl.c static void put_prev_task_dl(struct rq *rq, struct task_struct *p) · line 903

Quoted 9 lines · Juri Lelli
kernel/sched_dl.c +3 -0
@@ -543,6 +897,9 @@ static void put_prev_task_dl(struct rq *rq, struct task_struct *p)
 {
 	update_curr_dl(rq);
 	p->se.exec_start = 0;
+
+	if (on_dl_rq(&p->dl) && p->dl.nr_cpus_allowed > 1)
+		enqueue_pushable_dl_task(rq, p);
 }

Ouch! We need to fix this. This has nothing to do with your patch series, but if you look at schedule():

	put_prev_task(rq, prev);
	next = pick_next_task(rq);
Quoted 6 lines

We put the prev task and then pick the next task. If we call schedule for some reason when we don't need to really schedule, then we just added and removed from the pushable rb tree the same task. That is, we did the rb manipulation twice, for no good reason.

Not sure how to fix this. But it will require a generic change.

Something like so: https://lkml.org/lkml/2012/2/16/487 ?

maintainer
#102 lore raw

Re: [PATCH 10/16] sched: add resource limits for -deadline tasks.

In reply to Juri Lelli
Quoted 11 lines · Juri Lelli

From: Dario Faggioli <[email protected]>

Add resource limits for non-root tasks in using the SCHED_DEADLINE policy, very similarly to what already exists for RT policies.

In fact, this patch:
- adds the resource limit RLIMIT_DLDLINE, which is the minimum value a user task can use as its own deadline;
- adds the resource limit RLIMIT_DLRTIME, which is the maximum value a user task can use as it own runtime.

Quoted 6 lines

Notice that to exploit these, a modified version of the ulimit utility and a modified resource.h header file are needed. They both will be available on the website of the project.

I'm not sure this is the right way to go.. those existing things aren't entirely as useful/sane as one might hope either.

The DLDLINE minimum is ok I guess, the DLRTIME one doesn't really do anything, by spawning multiple tasks one can still saturate the cpu and thus we have no effective control for unpriv users.

Ideally DLRTIME would be a utilization cap per user and tracked in user_struct such that we can enforce a max utilization per user.

This also needs a global (and possibly per-cgroup) user limit too to cap the total utilization of all users (excluding root) so that multiple users cannot combine their efforts in order to bring down the machine.

In light of these latter controls the per-user control might be considered optional, furthermore I don't particularly like the rlimit infrastructure but I guess its the best we have for per-user like things if indeed we want to go there.

author
#103 lore raw

Re: [PATCH 10/16] sched: add resource limits for -deadline tasks.

Quoted 29 lines · Peter Zijlstra
Quoted 11 lines · Juri Lelli

From: Dario Faggioli<[email protected]>

Add resource limits for non-root tasks in using the SCHED_DEADLINE policy, very similarly to what already exists for RT policies.

In fact, this patch:
- adds the resource limit RLIMIT_DLDLINE, which is the minimum value

    a user task can use as its own deadline;

- adds the resource limit RLIMIT_DLRTIME, which is the maximum value a user task can use as it own runtime.

Quoted 6 lines

Notice that to exploit these, a modified version of the ulimit utility and a modified resource.h header file are needed. They both will be available on the website of the project.

I'm not sure this is the right way to go.. those existing things aren't entirely as useful/sane as one might hope either.

The DLDLINE minimum is ok I guess, the DLRTIME one doesn't really do anything, by spawning multiple tasks one can still saturate the cpu and thus we have no effective control for unpriv users.

Ideally DLRTIME would be a utilization cap per user and tracked in user_struct such that we can enforce a max utilization per user.

Quoted 8 lines

This also needs a global (and possibly per-cgroup) user limit too to cap the total utilization of all users (excluding root) so that multiple users cannot combine their efforts in order to bring down the machine.

In light of these latter controls the per-user control might be considered optional, furthermore I don't particularly like the rlimit infrastructure but I guess its the best we have for per-user like things if indeed we want to go there.

Ok, but considering what you said regarding setscheduler security problems:

Quoted 9 lines · Peter Zijlstra
Quoted 5 lines · Juri Lelli
Quoted 5 lines

Well, depends on how much effort will this turn to require. I personally would prefer to be able to come out with a new release ASAP. Just to continue the discussion with the most of the comments addressed and a more updated code (I also have a mainline version of the patchset quite ready).

Right, one thing we can initially do is require root for using SCHED_DEADLINE and then when later work closes all the holes and we've added user bandwidth controls we can allow everybody in.

Are you suggesting to drop/postpone this to some later time?

Thanks,

- Juri

maintainer
#104 lore raw

Re: [PATCH 10/16] sched: add resource limits for -deadline tasks.

Quoted 3 lines · Juri Lelli

Ok, but considering what you said regarding setscheduler security problems:

Are you suggesting to drop/postpone this to some later time?

Yeah, I think we can put this on the end of the queue and try and get some of the basic infrastructure merged first.

author
#105 lore raw

Re: [PATCH 10/16] sched: add resource limits for -deadline tasks.

Quoted 8 lines · Peter Zijlstra
Quoted 3 lines · Juri Lelli

Ok, but considering what you said regarding setscheduler security problems:

Are you suggesting to drop/postpone this to some later time?

Yeah, I think we can put this on the end of the queue and try and get some of the basic infrastructure merged first.

Perfect, no rants on my side ;-).

Thanks,

- Juri

maintainer
#88 lore raw

Re: [PATCH 06/16] sched: SCHED_DEADLINE push and pull logic

In reply to Peter Zijlstra
Quoted 24 lines · Peter Zijlstra
Quoted 23 lines · Steven Rostedt

kernel/sched_dl.c static void put_prev_task_dl(struct rq *rq, struct task_struct *p) · line 903

Quoted 9 lines · Juri Lelli
kernel/sched_dl.c +3 -0
@@ -543,6 +897,9 @@ static void put_prev_task_dl(struct rq *rq, struct task_struct *p)
 {
 	update_curr_dl(rq);
 	p->se.exec_start = 0;
+
+	if (on_dl_rq(&p->dl) && p->dl.nr_cpus_allowed > 1)
+		enqueue_pushable_dl_task(rq, p);
 }

Ouch! We need to fix this. This has nothing to do with your patch series, but if you look at schedule():

	put_prev_task(rq, prev);
	next = pick_next_task(rq);

We put the prev task and then pick the next task. If we call schedule for some reason when we don't need to really schedule, then we just added and removed from the pushable rb tree the same task. That is, we did the rb manipulation twice, for no good reason.

Quoted 4 lines
Quoted 1 line

Not sure how to fix this. But it will require a generic change.

Something like so: https://lkml.org/lkml/2012/2/16/487 ?

But it still does the same thing:

+static struct task_struct * +pick_next_task_rt(struct rq *rq, struct task_struct *prev)
{
- struct task_struct *p = _pick_next_task_rt(rq);
+ struct task_struct *p;
+ struct rt_rq *rt_rq = &rq->rt;
+
+ if (!rt_rq->rt_nr_running)
+ return NULL;
+
+ if (rt_rq_throttled(rt_rq))
+ return NULL;
+
+ if (prev)
+ prev->sched_class->put_prev_task(rq, prev);
+
+ p = _pick_next_task_rt(rq);

Now if we can do the _pick_next_task_rt() before put_prev_task(), and only do the put_prev_task() if p != prev, then that would be something.

-- Steve

maintainer
#89 lore raw

Re: [PATCH 06/16] sched: SCHED_DEADLINE push and pull logic

Quoted 22 lines · Steven Rostedt
Quoted 1 line

Something like so: https://lkml.org/lkml/2012/2/16/487 ?

But it still does the same thing:

(quoted diff) +15 -1
+static struct task_struct *
+pick_next_task_rt(struct rq *rq, struct task_struct *prev)
 {
-       struct task_struct *p = _pick_next_task_rt(rq);
+       struct task_struct *p;
+       struct rt_rq *rt_rq = &rq->rt;
+
+       if (!rt_rq->rt_nr_running)
+               return NULL;
+
+       if (rt_rq_throttled(rt_rq))
+               return NULL;
+
+       if (prev)
+               prev->sched_class->put_prev_task(rq, prev);
+
+       p = _pick_next_task_rt(rq);
Quoted 2 lines

Now if we can do the _pick_next_task_rt() before put_prev_task(), and only do the put_prev_task() if p != prev, then that would be something.

Well that's up to the implementation of pick_next_task_rt() that conversion is just a minimal make it work thing.

But the generic changes that patch carries should allow you to do what you want, right?

maintainer
#90 lore raw

Re: [PATCH 06/16] sched: SCHED_DEADLINE push and pull logic

Quoted 5 lines · Peter Zijlstra

Well that's up to the implementation of pick_next_task_rt() that conversion is just a minimal make it work thing.

But the generic changes that patch carries should allow you to do what you want, right?

Yep. It should.

-- Steve

author
#60 lore raw

Re: [PATCH 05/16] sched: SCHED_DEADLINE policy implementation.

In reply to Peter Zijlstra
Quoted 16 lines · Peter Zijlstra
Quoted 15 lines · Juri Lelli
Quoted 13 lines · Peter Zijlstra

kernel/sched_dl.c line 293

Quoted 8 lines · Juri Lelli
kernel/sched_dl.c new file +8 -0
+static void init_dl_task_timer(struct sched_dl_entity *dl_se)
+{
+       struct hrtimer *timer =&dl_se->dl_timer;
+
+       if (hrtimer_active(timer)) {
+               hrtimer_try_to_cancel(timer);
+               return;
+       }

Same question I guess, how can it be active here? Also, just letting it run doesn't seem like the best way out..

Quoted 8 lines
Quoted 1 line

Probably s/hrtimer_try_to_cancel/hrtimer_cancel is better.

Yeah, not sure you can do hrtimer_cancel() there though, you're holding ->pi_lock and rq->lock and have IRQs disabled. That sounds like asking for trouble.

Anyway, if it can't happen, we don't have to fix it.. so lets answer that first ;-)

Even if I dropped the bits for allowing !root users, this critical point still remains.
What if I leave this like it is and instead I do the following?

@@ -488,9 +488,10 @@ static enum hrtimer_restart dl_task_timer(struct hrtimer *timer)
/*
* We need to take care of a possible races here. In fact, the
* task might have changed its scheduling policy to something

-        * different from SCHED_DEADLINE (through sched_setscheduler()).
+        * different from SCHED_DEADLINE or changed its reservation
+        * parameters (through sched_{setscheduler(),setscheduler2()}).
         */
-       if (!dl_task(p))
+       if (!dl_task(p) || dl_se->dl_new)
                goto unlock;
 
        dl_se->dl_throttled = 0;

The idea is that hrtimer_try_to_cancel should fail only if the callback routine is running. If, meanwhile, I set up new parameters, I can try to recognize this situation through dl_new (set to 1 during __setparam_dl).

BTW, I'd have a new version ready (also rebased on the current tip/master). It address all the comments excluding your gcc work-around, math128 and nr_cpus_allowed shift (patches are ready but those changes not yet mainline, right?). Anyway, do you think would be fine to post it?

Thanks and Regards,

- Juri