-
Notifications
You must be signed in to change notification settings - Fork 8
/
rtgang-v5.4.83.patch
392 lines (376 loc) · 11.1 KB
/
rtgang-v5.4.83.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
diff --git a/include/linux/sched.h b/include/linux/sched.h
index 5710b80f8..a956024a9 100644
--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -687,6 +687,9 @@ struct task_struct {
struct uclamp_se uclamp[UCLAMP_CNT];
#endif
+ /* Throttling related fields */
+ int corun_threshold_events;
+
#ifdef CONFIG_PREEMPT_NOTIFIERS
/* List of struct preempt_notifier: */
struct hlist_head preempt_notifiers;
diff --git a/include/linux/syscalls.h b/include/linux/syscalls.h
index f7c561c4d..d3cacc4c6 100644
--- a/include/linux/syscalls.h
+++ b/include/linux/syscalls.h
@@ -946,6 +946,7 @@ asmlinkage long sys_process_vm_writev(pid_t pid,
const struct iovec __user *rvec,
unsigned long riovcnt,
unsigned long flags);
+asmlinkage long sys_bwlock(pid_t pid, int cte);
asmlinkage long sys_kcmp(pid_t pid1, pid_t pid2, int type,
unsigned long idx1, unsigned long idx2);
asmlinkage long sys_finit_module(int fd, const char __user *uargs, int flags);
diff --git a/include/uapi/asm-generic/unistd.h b/include/uapi/asm-generic/unistd.h
index 1fc8faa6e..74cf9a277 100644
--- a/include/uapi/asm-generic/unistd.h
+++ b/include/uapi/asm-generic/unistd.h
@@ -702,6 +702,9 @@ __SC_COMP_3264(__NR_recvmmsg, sys_recvmmsg_time32, sys_recvmmsg, compat_sys_recv
*/
#define __NR_arch_specific_syscall 244
+#define __NR_bwlock 255
+__SYSCALL(__NR_bwlock, sys_bwlock)
+
#if defined(__ARCH_WANT_TIME32_SYSCALLS) || __BITS_PER_LONG != 32
#define __NR_wait4 260
__SC_COMP(__NR_wait4, sys_wait4, compat_sys_wait4)
diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index 4511532b0..8701182be 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -539,6 +539,31 @@ void resched_cpu(int cpu)
raw_spin_unlock_irqrestore(&rq->lock, flags);
}
+/*
+ * The purpose of this function is to force rescheduling of a target cpu under
+ * all circumstances. For this reason, this function does not acquire the
+ * target CPU's rq lock and sends a rescheduling interrupt without protection
+ * if need be. It is used exclusively in RT-Gang related code.
+ */
+void resched_cpu_force (int cpu)
+{
+ struct rq *rq = cpu_rq(cpu);
+ struct task_struct *curr = rq->curr;
+
+ cpu = cpu_of(rq);
+
+ if (cpu == smp_processor_id()) {
+ set_tsk_need_resched(curr);
+ set_preempt_need_resched();
+ return;
+ }
+
+ if (set_nr_and_not_polling(curr))
+ smp_send_reschedule(cpu);
+ else
+ trace_sched_wake_idle_without_ipi(cpu);
+}
+
#ifdef CONFIG_SMP
#ifdef CONFIG_NO_HZ_COMMON
/*
@@ -4037,6 +4062,8 @@ pick_next_task(struct rq *rq, struct task_struct *prev, struct rq_flags *rf)
for_each_class(class) {
p = class->pick_next_task(rq, NULL, NULL);
+ if (p == BLOCK_TASK)
+ continue;
if (p)
return p;
}
@@ -5243,6 +5270,34 @@ static int sched_copy_attr(struct sched_attr __user *uattr, struct sched_attr *a
return -E2BIG;
}
+/*
+ * sys_bwlock - Memory bandwidth control lock. Provides exclusive access to
+ * main memory to the holder. Holder must be a real-time task
+ *
+ * @pid : pid of the process which wants to hold bandwidth lock
+ * @cte : Safe memory usage threshold for corunning tasks
+ */
+SYSCALL_DEFINE2(bwlock, pid_t, pid, int, cte)
+{
+ struct task_struct *p;
+
+ /* Obtain the task structure associated with the process
+ referenced by pid */
+ if (pid == 0 || current->pid == pid)
+ p = current;
+ else
+ p = find_process_by_pid (pid);
+
+ /* Process does not exist or it is not a real-time process */
+ if (!p || !rt_task (p))
+ return -1;
+
+ p->corun_threshold_events = cte;
+
+ /* Return with success */
+ return 0;
+}
+
/**
* sys_sched_setscheduler - set/change the scheduler policy and RT priority
* @pid: the pid in question.
diff --git a/kernel/sched/features.h b/kernel/sched/features.h
index 2410db5e9..9088068d6 100644
--- a/kernel/sched/features.h
+++ b/kernel/sched/features.h
@@ -6,6 +6,14 @@
*/
SCHED_FEAT(GENTLE_FAIR_SLEEPERS, true)
+/*
+ * Enable real-time gang scheduling framework (RT-Gang). RT-Gang allows
+ * execution of a single (multi-threaded) real-time task (i.e., gang) at any
+ * giving time across all system cores.
+ * NOTE: This feature is disabled by default.
+ */
+SCHED_FEAT(RT_GANG_LOCK, false)
+
/*
* Place new tasks ahead so that they do not starve already running
* tasks
diff --git a/kernel/sched/rt.c b/kernel/sched/rt.c
index 5b04bba45..1fd138401 100644
--- a/kernel/sched/rt.c
+++ b/kernel/sched/rt.c
@@ -7,6 +7,11 @@
#include "pelt.h"
+rt_gang_lock_t rt_gang_lock;
+rt_gang_lock_t *rt_glock = &rt_gang_lock;
+int be_mem_threshold = SYS_MAX_LLC_EVENTS;
+EXPORT_SYMBOL(be_mem_threshold);
+
int sched_rr_timeslice = RR_TIMESLICE;
int sysctl_sched_rr_timeslice = (MSEC_PER_SEC / HZ) * RR_TIMESLICE;
/* More than 4 hours if BW_SHIFT equals 20. */
@@ -1580,6 +1585,49 @@ pick_next_task_rt(struct rq *rq, struct task_struct *prev, struct rq_flags *rf)
return NULL;
p = _pick_next_task_rt(rq);
+
+ /* Do not apply RT gang to high-priority kernel threads */
+ if (sched_feat(RT_GANG_LOCK) && p->mm &&
+ (p->prio > RT_SYS_PRIO_THRESHOLD))
+ {
+ raw_spin_lock(&rt_glock->lock);
+ if (!rt_glock->lock_held) {
+ /* No RT gang exist currently; begin a new gang */
+ BUG_ON(cpumask_weight(rt_glock->locked_cores) != 0);
+ BUG_ON(cpumask_weight(rt_glock->blocked_cores) != 0);
+
+ TRACER(p, "Acquiring lock");
+ rt_glock->prio = p->prio;
+ gang_lock_cpu(p);
+ rt_glock->lock_held = true;
+ update_mem_threshold(p);
+ } else {
+ BUG_ON(cpumask_weight(rt_glock->locked_cores) == 0);
+ if (rt_glock->prio > p->prio) {
+ /* 'p' has higher priority; preempt */
+ TRACER(p, "Preempted by gang");
+ do_gang_preemption();
+ rt_glock->prio = p->prio;
+ gang_lock_cpu(p);
+ update_mem_threshold(p);
+ } else if (p->prio == rt_glock->prio) {
+ /* 'p' is part of the current RT gang */
+ gang_lock_cpu(p);
+ } else {
+ int this_cpu = smp_processor_id();
+ /* 'p' has lower priority; blocked */
+ if (!cpumask_test_cpu(this_cpu,
+ rt_glock->blocked_cores)) {
+ TRACER(p, "Blocking gang");
+ cpumask_set_cpu(this_cpu,
+ rt_glock->blocked_cores);
+ }
+ raw_spin_unlock(&rt_glock->lock);
+ return BLOCK_TASK;
+ }
+ }
+ raw_spin_unlock(&rt_glock->lock);
+ }
set_next_task_rt(rq, p, true);
return p;
}
@@ -1588,6 +1636,15 @@ static void put_prev_task_rt(struct rq *rq, struct task_struct *p)
{
update_curr_rt(rq);
+ /* If 'prev' is a member of the current RT gang, update the
+ * locked_cores mask and release the RT gang lock if necessary. */
+ if (sched_feat(RT_GANG_LOCK))
+ {
+ raw_spin_lock(&rt_glock->lock);
+ if (rt_glock->lock_held)
+ try_glock_release(p);
+ raw_spin_unlock(&rt_glock->lock);
+ }
update_rt_rq_load_avg(rq_clock_pelt(rq), rq, 1);
/*
@@ -2210,6 +2267,8 @@ void __init init_sched_rt_class(void)
zalloc_cpumask_var_node(&per_cpu(local_cpu_mask, i),
GFP_KERNEL, cpu_to_node(i));
}
+
+ INIT_GANG_LOCK();
}
#endif /* CONFIG_SMP */
diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h
index 3e7590813..346ff4cc7 100644
--- a/kernel/sched/sched.h
+++ b/kernel/sched/sched.h
@@ -1713,6 +1713,7 @@ extern const u32 sched_prio_to_wmult[40];
#endif
#define RETRY_TASK ((void *)-1UL)
+#define BLOCK_TASK ((void *)-2UL)
struct sched_class {
const struct sched_class *next;
@@ -1885,6 +1886,7 @@ extern void reweight_task(struct task_struct *p, int prio);
extern void resched_curr(struct rq *rq);
extern void resched_cpu(int cpu);
+extern void resched_cpu_force(int cpu);
extern struct rt_bandwidth def_rt_bandwidth;
extern void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime);
@@ -2554,3 +2556,141 @@ static inline void membarrier_switch_mm(struct rq *rq,
{
}
#endif
+
+/*
+ * GANG SCHEDULING RELATED DECLARATIONS
+ */
+typedef struct rt_gang_lock {
+ raw_spinlock_t lock;
+ bool lock_held;
+ cpumask_var_t locked_cores;
+ cpumask_var_t blocked_cores;
+ int prio;
+ struct task_struct* gthreads [NR_CPUS];
+} rt_gang_lock_t;
+
+extern int be_mem_threshold;
+extern rt_gang_lock_t *rt_glock;
+
+#define RT_SYS_PRIO_THRESHOLD (50)
+#define INIT_GANG_LOCK() \
+do { \
+ int i = 0; \
+ raw_spin_lock_init(&rt_glock->lock); \
+ rt_glock->lock_held = false; \
+ zalloc_cpumask_var(&rt_glock->locked_cores, GFP_KERNEL); \
+ zalloc_cpumask_var(&rt_glock->blocked_cores, GFP_KERNEL); \
+ rt_glock->prio = INT_MAX; \
+ for (; i < NR_CPUS; i++) \
+ rt_glock->gthreads [i] = NULL; \
+} while (0);
+
+/*
+ * Default memory usage threshold for best-effort tasks. On a system with
+ * 64-Byte cache line size, this equals 100 GBytes/sec i.e., no throttling.
+ */
+#define SYS_MAX_LLC_EVENTS (1638400)
+
+/*
+ * The following budget (for using main memory) is applied by default to all
+ * best-effort tasks on a per-core basis while a real-time task is executing.
+ * On a system with 64-Byte cache line size, the value specified below comes
+ * out to be 100 MBytes/sec.
+ * The goal here is to throttle 'aggressively' by default so that the
+ * best-effort tasks are not able to interfere with the real-time tasks.
+ */
+#define SYS_DEFAULT_LLC_EVENTS (1638)
+
+#define RT_GANG_DEBUG
+#ifdef RT_GANG_DEBUG
+#define TRACER(task, msg) trace_printk("[G:] %s\n", msg)
+#else
+#define TRACER(task, msg)
+#endif
+
+static inline void gang_lock_cpu(struct task_struct *thread)
+{
+ int cpu = smp_processor_id();
+
+ TRACER(thread, "Adding new gang member");
+ cpumask_set_cpu(cpu, rt_glock->locked_cores);
+ rt_glock->gthreads [cpu] = thread;
+
+ return;
+}
+
+static inline void resched_cpus(cpumask_var_t mask)
+{
+ int cpu;
+ int this_cpu = smp_processor_id();
+
+ for_each_cpu (cpu, mask) {
+ if (cpu == this_cpu)
+ continue;
+
+ resched_cpu_force(cpu);
+ }
+ return;
+}
+
+static inline void do_gang_preemption(void)
+{
+ int cpu;
+ int this_cpu = smp_processor_id();
+
+ for_each_cpu (cpu, rt_glock->locked_cores) {
+ WARN_ON(rt_glock->gthreads[cpu] == NULL);
+ TRACER(rt_glock->gthreads[cpu], "Preempting thread");
+
+ set_tsk_need_resched(rt_glock->gthreads[cpu]);
+
+ rt_glock->gthreads[cpu] = NULL;
+ if (cpu != this_cpu)
+ resched_cpu_force(cpu);
+ }
+
+ cpumask_clear(rt_glock->locked_cores);
+
+ return;
+}
+
+static inline void try_glock_release(struct task_struct *thread)
+{
+ int cpu;
+
+ WARN_ON(cpumask_weight(rt_glock->locked_cores) == 0);
+
+ /*
+ * Release RT-Gang lock of 'prev' task on all cores it may have ran on.
+ * Migrated tasks can hold lock on multiple cores.
+ */
+ for_each_cpu (cpu, rt_glock->locked_cores) {
+ if (rt_glock->gthreads [cpu] == thread) {
+ TRACER(thread, "Releasing lock");
+ WARN_ON(!rt_prio(thread->prio));
+ cpumask_clear_cpu(cpu, rt_glock->locked_cores);
+ }
+ }
+
+ if (cpumask_weight(rt_glock->locked_cores) == 0) {
+ /* RT-Gang lock is now free. Reschedule blocked cores. */
+ TRACER(thread, "Lock free");
+ rt_glock->prio = INT_MAX;
+ rt_glock->lock_held = false;
+ be_mem_threshold = SYS_MAX_LLC_EVENTS;
+ resched_cpus(rt_glock->blocked_cores);
+ cpumask_clear(rt_glock->blocked_cores);
+ }
+
+ return;
+}
+
+static inline void update_mem_threshold(struct task_struct *thread)
+{
+ if (thread->corun_threshold_events)
+ be_mem_threshold = thread->corun_threshold_events;
+ else
+ be_mem_threshold = SYS_DEFAULT_LLC_EVENTS;
+
+ return;
+}