-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrtgsync.patch
More file actions
2820 lines (2766 loc) · 72.6 KB
/
rtgsync.patch
File metadata and controls
2820 lines (2766 loc) · 72.6 KB
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
diff --git a/Makefile b/Makefile
index 44445f2..585dc2d 100644
--- a/Makefile
+++ b/Makefile
@@ -1,7 +1,7 @@
VERSION = 4
PATCHLEVEL = 4
SUBLEVEL = 38
-EXTRAVERSION =
+EXTRAVERSION = -RTGv3
NAME = Blurry Fish Butt
# *DOCUMENTATION*
diff --git a/include/linux/cgroup_subsys.h b/include/linux/cgroup_subsys.h
index 1a96fda..9235fae 100644
--- a/include/linux/cgroup_subsys.h
+++ b/include/linux/cgroup_subsys.h
@@ -84,3 +84,7 @@ SUBSYS(debug)
/*
* DO NOT ADD ANY SUBSYSTEM WITHOUT EXPLICIT ACKS FROM CGROUP MAINTAINERS.
*/
+
+#if IS_ENABLED(CONFIG_CGROUP_PALLOC)
+SUBSYS(palloc)
+#endif
diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h
index e23a9e7..1f718f0 100644
--- a/include/linux/mmzone.h
+++ b/include/linux/mmzone.h
@@ -69,6 +69,14 @@ enum {
# define is_migrate_cma(migratetype) false
#endif
+#ifdef CONFIG_CGROUP_PALLOC
+/* Determine the number of bins according to the bits required for
+ each component of the address */
+#define MAX_PALLOC_BITS 8
+#define MAX_PALLOC_BINS (1 << MAX_PALLOC_BITS)
+#define COLOR_BITMAP(name) DECLARE_BITMAP(name, MAX_PALLOC_BINS)
+#endif
+
#define for_each_migratetype_order(order, type) \
for (order = 0; order < MAX_ORDER; order++) \
for (type = 0; type < MIGRATE_TYPES; type++)
@@ -478,6 +486,14 @@ struct zone {
/* free areas of different sizes */
struct free_area free_area[MAX_ORDER];
+#ifdef CONFIG_CGROUP_PALLOC
+ /*
+ * Color page cache for movable type free pages of order-0
+ */
+ struct list_head color_list[MAX_PALLOC_BINS];
+ COLOR_BITMAP(color_bitmap);
+#endif
+
/* zone flags, see below */
unsigned long flags;
diff --git a/include/linux/palloc.h b/include/linux/palloc.h
new file mode 100644
index 0000000..7236e31
--- /dev/null
+++ b/include/linux/palloc.h
@@ -0,0 +1,33 @@
+#ifndef _LINUX_PALLOC_H
+#define _LINUX_PALLOC_H
+
+/*
+ * kernel/palloc.h
+ *
+ * Physical Memory Aware Allocator
+ */
+
+#include <linux/types.h>
+#include <linux/cgroup.h>
+#include <linux/kernel.h>
+#include <linux/mm.h>
+
+#ifdef CONFIG_CGROUP_PALLOC
+
+struct palloc {
+ struct cgroup_subsys_state css;
+ COLOR_BITMAP(cmap);
+};
+
+/* Retrieve the palloc group corresponding to this cgroup container */
+struct palloc *cgroup_ph(struct cgroup *cgrp);
+
+/* Retrieve the palloc group corresponding to this subsys */
+struct palloc *ph_from_subsys(struct cgroup_subsys_state *subsys);
+
+/* Return number of palloc bins */
+int palloc_bins(void);
+
+#endif /* CONFIG_CGROUP_PALLOC */
+
+#endif /* _LINUX_PALLOC_H */
diff --git a/include/linux/sched.h b/include/linux/sched.h
index 4d05a8d..f3d695b 100644
--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -126,6 +126,17 @@ struct sched_attr {
u64 sched_period;
};
+#ifdef CONFIG_SCHED_RTGANG
+struct rtg_resource_info {
+ int gid;
+ int rd_th;
+ int wr_th;
+ long unsigned bins;
+};
+
+#define GET_RTG_INFO(task) (&task->rtg_info)
+#endif
+
struct futex_pi_state;
struct robust_list_head;
struct bio_list;
@@ -1416,6 +1427,14 @@ struct task_struct {
#endif
struct sched_dl_entity dl;
+#ifdef CONFIG_SCHED_RTGANG
+ struct rtg_resource_info rtg_info;
+
+#ifdef CONFIG_CGROUP_PALLOC
+ COLOR_BITMAP(cmap);
+#endif
+#endif
+
#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 c2b66a2..9e4d738 100644
--- a/include/linux/syscalls.h
+++ b/include/linux/syscalls.h
@@ -889,4 +889,9 @@ asmlinkage long sys_membarrier(int cmd, int flags);
asmlinkage long sys_mlock2(unsigned long start, size_t len, int flags);
+#ifdef CONFIG_SCHED_RTGANG
+asmlinkage long sys_rtg_set_params(pid_t pid,
+ struct rtg_resource_info __user *info);
+#endif
+
#endif
diff --git a/include/uapi/asm-generic/unistd.h b/include/uapi/asm-generic/unistd.h
index 1324b02..a90c346 100644
--- a/include/uapi/asm-generic/unistd.h
+++ b/include/uapi/asm-generic/unistd.h
@@ -662,6 +662,11 @@ __SC_COMP(__NR_recvmmsg, sys_recvmmsg, compat_sys_recvmmsg)
*/
#define __NR_arch_specific_syscall 244
+#ifdef CONFIG_SCHED_RTGANG
+#define __NR_rtg_set_params 245
+__SYSCALL(__NR_rtg_set_params, sys_rtg_set_params)
+#endif
+
#define __NR_wait4 260
__SC_COMP(__NR_wait4, sys_wait4, compat_sys_wait4)
#define __NR_prlimit64 261
diff --git a/init/Kconfig b/init/Kconfig
index b4c3fc7..29d6901 100644
--- a/init/Kconfig
+++ b/init/Kconfig
@@ -1158,6 +1158,13 @@ config CGROUP_WRITEBACK
depends on MEMCG && BLK_CGROUP
default y
+config CGROUP_PALLOC
+ bool "Enable PALLOC"
+ help
+ Enable PALLOC. PALLOC is a color-aware page-based physical memory
+ allocator which replaces the buddy allocator for order-zero page
+ allocations.
+
endif # CGROUPS
config CHECKPOINT_RESTORE
@@ -1244,6 +1251,23 @@ config SCHED_AUTOGROUP
desktop applications. Task group autogeneration is currently based
upon task session.
+config SCHED_RTGANG
+ bool "Include RT_GANG_LOCK in scheduling features"
+ help
+ This option introduces the RT-Gang scheduling feature. Under RT-Gang,
+ only one (parallel) real-time task is allowed to execute on all
+ system cores at any given time. This resolves the problem of shared
+ resource contention among different real-time tasks and guarantees
+ complete performance isolation to the highest priority real-time
+ task.
+
+config SCHED_THROTTLE
+ bool "Enable best-effor task throttling support inside scheduler"
+ help
+ This option integrates a kernel level task throttling framework into
+ the scheduler. This framework can be used to limit the interference
+ from best-effort tasks to real-time tasks.
+
config SYSFS_DEPRECATED
bool "Enable deprecated sysfs features to support old userspace tools"
depends on SYSFS
diff --git a/kernel/sched/Makefile b/kernel/sched/Makefile
index 43fb319..8db3155 100644
--- a/kernel/sched/Makefile
+++ b/kernel/sched/Makefile
@@ -23,3 +23,5 @@ obj-$(CONFIG_SCHED_DEBUG) += debug.o
obj-$(CONFIG_CGROUP_CPUACCT) += cpuacct.o
obj-$(CONFIG_CPU_FREQ) += cpufreq.o
obj-$(CONFIG_CPU_FREQ_GOV_SCHEDUTIL) += cpufreq_schedutil.o
+obj-$(CONFIG_SCHED_RTGANG) += rtgang.o
+obj-$(CONFIG_SCHED_THROTTLE) += throttle.o
diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index 8d2cd2b..5667e5e 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -91,6 +91,10 @@
#define CREATE_TRACE_POINTS
#include <trace/events/sched.h>
+#ifdef CONFIG_SCHED_RTGANG
+#include "rtgang.h"
+#endif
+
DEFINE_MUTEX(sched_domains_mutex);
DEFINE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues);
@@ -607,6 +611,36 @@ void resched_cpu(int cpu)
raw_spin_unlock_irqrestore(&rq->lock, flags);
}
+#ifdef CONFIG_SCHED_RTGANG
+/*
+ * 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;
+
+ if (test_tsk_need_resched(curr))
+ return;
+
+ 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);
+}
+#endif
+
#ifdef CONFIG_SMP
#ifdef CONFIG_NO_HZ_COMMON
/*
@@ -3065,33 +3099,31 @@ static inline void schedule_debug(struct task_struct *prev)
static inline struct task_struct *
pick_next_task(struct rq *rq, struct task_struct *prev)
{
- const struct sched_class *class = &fair_sched_class;
+ const struct sched_class *class;
struct task_struct *p;
-
- /*
- * Optimization: we know that if all tasks are in
- * the fair class we can call that function directly:
- */
- if (likely(prev->sched_class == class &&
- rq->nr_running == rq->cfs.h_nr_running)) {
- p = fair_sched_class.pick_next_task(rq, prev);
- if (unlikely(p == RETRY_TASK))
- goto again;
-
- /* assumes fair_sched_class->next == idle_sched_class */
- if (unlikely(!p))
- p = idle_sched_class.pick_next_task(rq, prev);
-
- return p;
- }
+ bool skip_retry_flag = false;
again:
for_each_class(class) {
p = class->pick_next_task(rq, prev);
if (p) {
- if (unlikely(p == RETRY_TASK))
+ if (p == BLOCK_TASK) {
+ /*
+ * Do not honor the RETRY request from the fair
+ * class since blocking of task in RT class is
+ * being done on purpose.
+ */
+ skip_retry_flag = true;
+ continue;
+ }
+
+ if (p != RETRY_TASK)
+ /* We have a valid task. Return it! */
+ return p;
+
+ if (!skip_retry_flag && p == RETRY_TASK)
+ /* Restart the task picking loop */
goto again;
- return p;
}
}
@@ -4213,6 +4245,45 @@ err_size:
return -E2BIG;
}
+#ifdef CONFIG_SCHED_RTGANG
+/*
+ * sys_rtg_set_params - Update task parameters for RT-Gang
+ *
+ * @pid : pid of the target process.
+ * @info : Resource requirement information of the target process
+ *
+ * Return: Zero on success. An error code otherwise.
+ */
+SYSCALL_DEFINE2(rtg_set_params, pid_t, pid,
+ struct rtg_resource_info* __user, info)
+{
+ int c;
+ 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 || !(IS_RTC(p) || IS_EDF(p)))
+ return -EINVAL;
+
+ if (copy_from_user(GET_RTG_INFO(p), info,
+ sizeof(struct rtg_resource_info)))
+ return -EFAULT;
+
+#ifdef CONFIG_CGROUP_PALLOC
+ for_each_set_bit(c, &info->bins, sizeof(info->bins) * 8)
+ bitmap_set(p->cmap, c, 1);
+#endif
+
+ return 0;
+}
+#endif
+
/**
* sys_sched_setscheduler - set/change the scheduler policy and RT priority
* @pid: the pid in question.
diff --git a/kernel/sched/deadline.c b/kernel/sched/deadline.c
index b27f783..6e64991 100644
--- a/kernel/sched/deadline.c
+++ b/kernel/sched/deadline.c
@@ -15,6 +15,7 @@
* Fabio Checconi <fchecconi@gmail.com>
*/
#include "sched.h"
+#include "rtgang.h"
#include <linux/slab.h>
@@ -1161,6 +1162,7 @@ struct task_struct *pick_next_task_dl(struct rq *rq, struct task_struct *prev)
struct sched_dl_entity *dl_se;
struct task_struct *p;
struct dl_rq *dl_rq;
+ int ret;
dl_rq = &rq->dl;
@@ -1187,22 +1189,37 @@ struct task_struct *pick_next_task_dl(struct rq *rq, struct task_struct *prev)
* When prev is DL, we may throttle it in put_prev_task().
* So, we update time before we check for dl_nr_running.
*/
- if (prev->sched_class == &dl_sched_class)
+ if (prev->sched_class == &dl_sched_class) {
update_curr_dl(rq);
+#ifdef CONFIG_SCHED_RTGANG
+ if (sched_feat(RT_GANG_LOCK))
+ rtg_try_release_lock(prev);
+#endif
+ }
+
if (unlikely(!dl_rq->dl_nr_running))
return NULL;
- put_prev_task(rq, prev);
-
dl_se = pick_next_dl_entity(rq, dl_rq);
BUG_ON(!dl_se);
p = dl_task_of(dl_se);
+
+#ifdef CONFIG_SCHED_RTGANG
+ if (sched_feat(RT_GANG_LOCK)) {
+ ret = rtg_try_acquire_lock(p);
+
+ if (ret == RTG_BLOCK)
+ return BLOCK_TASK;
+ }
+#endif
+
+ put_prev_task(rq, prev);
p->se.exec_start = rq_clock_task(rq);
/* Running task will never be pushed. */
- dequeue_pushable_dl_task(rq, p);
+ dequeue_pushable_dl_task(rq, p);
if (hrtick_enabled(rq))
start_hrtick_dl(rq, p);
diff --git a/kernel/sched/features.h b/kernel/sched/features.h
index b91da5f..0234011 100644
--- a/kernel/sched/features.h
+++ b/kernel/sched/features.h
@@ -5,6 +5,16 @@
*/
SCHED_FEAT(GENTLE_FAIR_SLEEPERS, true)
+#ifdef CONFIG_SCHED_RTGANG
+/*
+ * 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)
+#endif
+
/*
* 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 6a839a4..9f8077f 100644
--- a/kernel/sched/rt.c
+++ b/kernel/sched/rt.c
@@ -4,6 +4,7 @@
*/
#include "sched.h"
+#include "rtgang.h"
#include <linux/slab.h>
#include <linux/irq_work.h>
@@ -1480,7 +1481,7 @@ static struct sched_rt_entity *pick_next_rt_entity(struct rq *rq,
return next;
}
-static struct task_struct *_pick_next_task_rt(struct rq *rq)
+static struct task_struct *__peek_next_task_rt(struct rq *rq)
{
struct sched_rt_entity *rt_se;
struct task_struct *p;
@@ -1493,7 +1494,6 @@ static struct task_struct *_pick_next_task_rt(struct rq *rq)
} while (rt_rq);
p = rt_task_of(rt_se);
- p->se.exec_start = rq_clock_task(rq);
return p;
}
@@ -1501,6 +1501,7 @@ static struct task_struct *_pick_next_task_rt(struct rq *rq)
static struct task_struct *
pick_next_task_rt(struct rq *rq, struct task_struct *prev)
{
+ int ret;
struct task_struct *p;
struct rt_rq *rt_rq = &rq->rt;
@@ -1528,19 +1529,34 @@ pick_next_task_rt(struct rq *rq, struct task_struct *prev)
* We may dequeue prev's rt_rq in put_prev_task().
* So, we update time before rt_nr_running check.
*/
- if (prev->sched_class == &rt_sched_class)
+ if (prev->sched_class == &rt_sched_class) {
update_curr_rt(rq);
+#ifdef CONFIG_SCHED_RTGANG
+ if (sched_feat(RT_GANG_LOCK))
+ rtg_try_release_lock(prev);
+#endif
+ }
+
if (!rt_rq->rt_queued)
return NULL;
- put_prev_task(rq, prev);
+ p = __peek_next_task_rt (rq);
- p = _pick_next_task_rt(rq);
+#ifdef CONFIG_SCHED_RTGANG
+ if (sched_feat(RT_GANG_LOCK) && RTG_FIFO_CHECK(p)) {
+ ret = rtg_try_acquire_lock(p);
+
+ if (ret == RTG_BLOCK)
+ return BLOCK_TASK;
+ }
+#endif
+
+ put_prev_task (rq, prev);
+ p->se.exec_start = rq_clock_task (rq);
/* The running task is never eligible for pushing */
dequeue_pushable_task(rq, p);
-
queue_push_tasks(rq);
return p;
diff --git a/kernel/sched/rtg_throttle.h b/kernel/sched/rtg_throttle.h
new file mode 100644
index 0000000..2749848
--- /dev/null
+++ b/kernel/sched/rtg_throttle.h
@@ -0,0 +1,27 @@
+#ifndef __RTG_THROTTLE_H__
+#define __RTG_THROTTLE_H__
+
+#if defined(CONFIG_SCHED_RTGANG) && defined(CONFIG_SCHED_THROTTLE)
+
+#define ID_TX2 (0x1)
+#define ID_PI (0x2)
+#define PLATFORM_ID ID_TX2
+
+#if (PLATFORM_ID == ID_TX2 || PLATFORM_ID == ID_PI)
+#define TH_RTG_EVT1_ID (0x17)
+#define TH_RTG_EVT2_ID (0x18)
+#else
+#error Platform not supported by throttling framework.
+#endif
+
+#define TH_RTG_EVT1_DEFAULT_BUDGET (16348LLU) /* 1000 MBps */
+#define TH_RTG_EVT2_DEFAULT_BUDGET (8192LLU) /* 500 MBps */
+#define TH_RTG_EVT1_MAX_BUDGET (1634800LLU) /* 100 GBps */
+#define TH_RTG_EVT2_MAX_BUDGET (819200LLU) /* 50 GBps */
+
+void th_rtg_create_event(int id, u64 budget);
+void th_rtg_update_budget(u64 evt1_budget, u64 evt2_budget);
+
+#endif /* defined(CONFIG_SCHED_RTGANG) && defined(CONFIG_SCHED_THROTTLE) */
+
+#endif /* __RTG_THROTTLE_H__ */
diff --git a/kernel/sched/rtgang.c b/kernel/sched/rtgang.c
new file mode 100644
index 0000000..5917263
--- /dev/null
+++ b/kernel/sched/rtgang.c
@@ -0,0 +1,275 @@
+/*
+ * kernel/sched/rtgang.c
+ *
+ * Real-Time Gang Scheduling Framework
+ *
+ * Copyright (C) 2019 CSL-KU
+ * 2019-03-28 Separation of RT-Gang from scheduler core
+ * 2019-03-29 Support EDF tasks (SCHED_DEADLINE)
+ * 2019-03-29 Conditionally compile RT-Gang into kernel
+ * 2019-03-30 Integrate with the throttling framework
+ */
+#include "sched.h"
+#include "rtgang.h"
+#include <linux/debugfs.h>
+
+#ifdef CONFIG_SCHED_THROTTLE
+#include "rtg_throttle.h"
+#endif
+
+/*
+ * Global variables
+ */
+struct rtgang_lock rtgang_lock;
+struct rtgang_lock *rtg_lock = &rtgang_lock;
+
+/*
+ * Current debug level
+ * Default: 0 (No debug messages)
+ */
+int rtg_debug_level = 0;
+
+/*
+ * gang_lock_cpu - Acquire RT-Gang lock on behalf of the thread
+ */
+static inline void gang_lock_cpu(struct task_struct *thread)
+{
+ int cpu = smp_processor_id();
+
+ cpumask_set_cpu(cpu, rtg_lock->locked_cores);
+ rtg_lock->gthreads [cpu] = thread;
+ rtg_debug(RTG_LEVEL_SUBSTATE, " rtg_lock_thread: comm=%s sched=%s "
+ "pid=%d tgid=%d rtgid=%d\n", thread->comm, PRINT_SCHED(thread),
+ thread->pid, thread->tgid, thread->rtgid);
+
+ return;
+}
+
+/*
+ * resched_cpus - Send rescheduling interrupt(s) to CPUs in mask
+ */
+static inline void resched_cpus(cpumask_var_t mask)
+{
+ int cpu;
+ int this_cpu = smp_processor_id();
+
+ for_each_cpu (cpu, mask) {
+ rtg_debug(RTG_LEVEL_ALL, " rtg_resched_cpu: cpu=%d\n",
+ cpu);
+
+ if (cpu == this_cpu)
+ continue;
+
+ resched_cpu_force(cpu);
+ }
+
+ return;
+}
+
+/*
+ * do_gang_preemption - Preempt currently running executing gang on behalf of
+ * 'next' gang
+ *
+ * Acquire RT-Gang lock on behalf of 'next' gang
+ */
+static inline void do_gang_preemption(struct task_struct *next)
+{
+ int cpu;
+ int this_cpu = smp_processor_id();
+
+ for_each_cpu (cpu, rtg_lock->locked_cores) {
+ WARN_ON(rtg_lock->gthreads [cpu] == NULL);
+
+ if (cpu != this_cpu)
+ resched_cpu_force(cpu);
+
+ rtg_debug(RTG_LEVEL_SUBSTATE, " rtg_preempt_thread: cpu=%d "
+ "comm=%s sched=%s pid=%d tgid=%d rtgid=%d\n", cpu,
+ rtg_lock->gthreads [cpu]->comm,
+ PRINT_SCHED(rtg_lock->gthreads [cpu]),
+ rtg_lock->gthreads [cpu]->pid,
+ rtg_lock->gthreads [cpu]->tgid,
+ rtg_lock->gthreads [cpu]->rtgid);
+ rtg_lock->gthreads [cpu] = NULL;
+ }
+
+ cpumask_clear(rtg_lock->locked_cores);
+ gang_lock_cpu(next);
+ rtg_lock->leader = next;
+
+ return;
+}
+
+/*
+ * try_glock_release - Release RT-Gang lock on behalf of 'thread'
+ *
+ * Send rescheduling interrupt to blocked CPUs if RT-Gang lock is now free.
+ */
+static inline void try_glock_release(struct task_struct *thread)
+{
+ int cpu;
+
+ WARN_ON(cpumask_weight(rtg_lock->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, rtg_lock->locked_cores) {
+ if (rtg_lock->gthreads [cpu] == thread) {
+ WARN_ON(!rt_prio(thread->prio));
+ cpumask_clear_cpu(cpu, rtg_lock->locked_cores);
+ rtg_debug(RTG_LEVEL_SUBSTATE, " rtg_unlock_thread: "
+ "cpu=%d comm=%s sched=%s pid=%d tgid=%d rtgid=%d\n", cpu,
+ thread->comm, PRINT_SCHED(thread),
+ thread->pid, thread->tgid, thread->rtgid);
+ }
+ }
+
+ if (cpumask_weight(rtg_lock->locked_cores) == 0) {
+ /* RT-Gang lock is now free. Reschedule blocked cores */
+ rtg_lock->leader = NULL;
+ rtg_lock->busy = false;
+ resched_cpus(rtg_lock->blocked_cores);
+ cpumask_clear(rtg_lock->blocked_cores);
+
+#ifdef CONFIG_SCHED_THROTTLE
+ th_rtg_update_budget(TH_RTG_EVT1_MAX_BUDGET,
+ TH_RTG_EVT2_MAX_BUDGET);
+#endif
+
+ rtg_log_event(RTG_LEVEL_STATE, thread, "release");
+ }
+
+ return;
+}
+
+/*
+ * rtg_try_release_lock - Interface function for releasing RT-Gang lock
+ *
+ * If the task going out of execution on this CPU is holding RT-Gang lock,
+ * release it on the task's behalf and perform necessary book keeping.
+ */
+void rtg_try_release_lock(struct task_struct *prev)
+{
+ /*
+ * If 'prev' is a member of the current RT gang, update the
+ * locked_cores mask and release the RT gang lock if necessary.
+ */
+ raw_spin_lock(&rtg_lock->access_lock);
+ if (rtg_lock->busy)
+ try_glock_release(prev);
+ raw_spin_unlock(&rtg_lock->access_lock);
+
+ return;
+}
+
+/*
+ * rtg_try_acquire_lock - Interface function for obtaining RT-Gang lock
+ *
+ * Check if the next task is eligibile to obtain RT-Gang lock. If not, block
+ * the task from executing on this CPU.
+ */
+int rtg_try_acquire_lock(struct task_struct *next)
+{
+ int this_cpu = smp_processor_id();
+ int ret = RTG_CONTINUE;
+
+ raw_spin_lock(&rtg_lock->access_lock);
+ if (!rtg_lock->busy) {
+ /* No RT gang exist currently; begin a new gang */
+ BUG_ON(cpumask_weight(rtg_lock->locked_cores) != 0);
+ BUG_ON(cpumask_weight(rtg_lock->blocked_cores) != 0);
+
+ rtg_log_event(RTG_LEVEL_STATE, next, "acquire");
+ gang_lock_cpu(next);
+ rtg_lock->busy = true;
+ rtg_lock->leader = next;
+
+#ifdef CONFIG_SCHED_THROTTLE
+ th_rtg_update_budget(GET_RTG_INFO(next)->rd_th,
+ GET_RTG_INFO(next)->wr_th);
+#endif
+
+ goto out;
+ }
+
+ BUG_ON(cpumask_weight(rtg_lock->locked_cores) == 0);
+ if (IS_GANG_MEMBER(next)) {
+ /* 'next' is part of the current RT gang */
+ rtg_log_event(RTG_LEVEL_SUBSTATE, next, " add");
+ gang_lock_cpu(next);
+ goto out;
+ }
+
+ /*
+ * Gang preemption conditions:
+ * 1. Current gang leader and 'next' task are of same scheduler type
+ * 1.1. EDF: 'next' has earlier deadline
+ * 1.2. FIFO: 'next' has higher priority
+ * 2. Current gang leader and 'next' task are of different scheduler
+ * type and next is an EDF task
+ */
+ if (((IS_SAME_CLASS(next, rtg_lock->leader)) &&
+ ((IS_EDF(next) && IS_EARLIER_EDF(next, rtg_lock->leader)) ||
+ (!IS_EDF(next) && IS_HIGHER_PRIO(next, rtg_lock->leader)))) ||
+ ((!IS_SAME_CLASS(next, rtg_lock->leader)) && IS_EDF(next))) {
+ rtg_log_event(RTG_LEVEL_STATE, next, "preemptor");
+ rtg_log_event(RTG_LEVEL_STATE, rtg_lock->leader, "preemptee");
+
+ do_gang_preemption(next);
+
+#ifdef CONFIG_SCHED_THROTTLE
+ th_rtg_update_budget(GET_RTG_INFO(next)->rd_th,
+ GET_RTG_INFO(next)->wr_th);
+#endif
+ } else {
+ /* 'p' has lower priority; blocked */
+ if (!cpumask_test_cpu(this_cpu, rtg_lock->blocked_cores)) {
+ cpumask_set_cpu(this_cpu, rtg_lock->blocked_cores);
+ rtg_log_event(RTG_LEVEL_STATE, next, "block");
+ }
+
+ ret = RTG_BLOCK;
+ }
+
+out:
+ raw_spin_unlock(&rtg_lock->access_lock);
+ return ret;
+}
+
+/*
+ * rtg_init_lock - Initialize RT-Gang data-structure and interface
+ *
+ * Called at the end of kernel initialization. Performs bare-minimum setup for
+ * using RT-Gang at runtime.
+ */
+static int __init rtg_init_lock(void)
+{
+ int i = 0;
+ struct dentry *dir;
+ umode_t mode = S_IFREG | S_IRUSR | S_IWUSR;
+
+ dir = debugfs_create_dir("rtgang", NULL);
+ if (!dir)
+ return PTR_ERR(dir);
+
+ if (!debugfs_create_u32("debug_level", mode, dir, &rtg_debug_level))
+ goto fail;
+
+ raw_spin_lock_init(&rtg_lock->access_lock);
+ rtg_lock->busy = false;
+ zalloc_cpumask_var(&rtg_lock->locked_cores, GFP_KERNEL);
+ zalloc_cpumask_var(&rtg_lock->blocked_cores, GFP_KERNEL);
+ rtg_lock->leader = NULL;
+
+ for (; i < NR_CPUS; i++)
+ rtg_lock->gthreads [i] = NULL;
+
+ return 0;
+fail:
+ debugfs_remove_recursive(dir);
+ return -ENOMEM;
+}
+
+late_initcall(rtg_init_lock);
diff --git a/kernel/sched/rtgang.h b/kernel/sched/rtgang.h
new file mode 100644
index 0000000..5ce1445
--- /dev/null
+++ b/kernel/sched/rtgang.h
@@ -0,0 +1,84 @@
+#ifndef __RTGANG_H__
+#define __RTGANG_H__
+
+#ifdef CONFIG_SCHED_RTGANG
+
+#define RTG_FIFO_PRIO_THRESHOLD (50)
+#define RTG_CONTINUE (0)
+#define RTG_BLOCK (1)
+
+#define RTG_FIFO_CHECK(p) \
+ (p->mm && p->prio > RTG_FIFO_PRIO_THRESHOLD)
+
+#define IS_REAL_GANG_MEMBER(p) \
+ (rtg_lock->leader->tgid == p->tgid)
+
+#define IS_VIRT_GANG_MEMBER(p) \
+ ((GET_RTG_INFO(rtg_lock->leader)->gid != 0) && \
+ (GET_RTG_INFO(rtg_lock->leader)->gid == GET_RTG_INFO(p)->gid))
+
+#define IS_GANG_MEMBER(p) \
+ (IS_REAL_GANG_MEMBER(p) || IS_VIRT_GANG_MEMBER(p))
+
+#define IS_SAME_CLASS(p, n) \
+ (p->sched_class == n->sched_class)
+
+#define IS_RTC(p) \
+ (p->sched_class == &rt_sched_class)
+
+#define IS_EDF(p) \
+ (p->sched_class == &dl_sched_class)
+
+#define IS_EARLIER_EDF(p, n) \
+ (dl_time_before(p->dl.deadline, n->dl.deadline))
+
+#define IS_HIGHER_PRIO(p, n) \
+ (p->prio < n->prio)
+
+#define PRINT_SCHED(p) \
+ (IS_EDF(p)? "EDF":"FIFO")
+
+#define PRINT_PRIO(p) \
+ (IS_EDF(p)? p->dl.deadline:(u64)p->prio)
+
+#undef RTG_DEBUG
+#ifdef RTG_DEBUG
+#define rtg_log_event(level, task, event) \
+do { \
+ rtg_debug(level, "rtg_%s: comm=%s rtgid=%d tgid=%d " \
+ "pid=%d prio=%d\n", event, task->comm, \
+ task->rtgid, task->tgid, task->pid, \
+ task->prio); \
+} while (0);
+
+#define rtg_debug(level, format, ...) \
+do { \
+ if (rtg_debug_level >= level) \
+ trace_printk(format, ##__VA_ARGS__); \
+} while (0);
+#else
+#define rtg_log_event(level, task, event)
+#define rtg_debug(level, format, ...)
+#endif
+
+/* Debug Levels */
+#define RTG_LEVEL_DISABLE (0)
+#define RTG_LEVEL_STATE (1)
+#define RTG_LEVEL_SUBSTATE (2)
+#define RTG_LEVEL_ALL (3)
+
+struct rtgang_lock {
+ bool busy;
+ raw_spinlock_t access_lock;
+ struct task_struct* leader;
+ struct task_struct* gthreads [NR_CPUS];
+ cpumask_var_t locked_cores;
+ cpumask_var_t blocked_cores;
+};
+
+void rtg_try_release_lock(struct task_struct *prev);
+int rtg_try_acquire_lock(struct task_struct *next);
+
+#endif /* CONFIG_SCHED_RTGANG */
+
+#endif /* __RTGANG_H__ */
diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h
index b654396..776b235 100644
--- a/kernel/sched/sched.h
+++ b/kernel/sched/sched.h
@@ -1179,6 +1179,7 @@ static const u32 prio_to_wmult[40] = {
#define DEQUEUE_SAVE 0x02
#define RETRY_TASK ((void *)-1UL)
+#define BLOCK_TASK ((void *)-2UL)
struct sched_class {
const struct sched_class *next;
@@ -1313,6 +1314,10 @@ extern void init_sched_fair_class(void);
extern void resched_curr(struct rq *rq);
extern void resched_cpu(int cpu);
+#ifdef CONFIG_SCHED_RTGANG
+extern void resched_cpu_force(int cpu);
+#endif
+
extern struct rt_bandwidth def_rt_bandwidth;
extern void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime);
diff --git a/kernel/sched/throttle.c b/kernel/sched/throttle.c
new file mode 100644
index 0000000..774eac2
--- /dev/null
+++ b/kernel/sched/throttle.c
@@ -0,0 +1,891 @@
+/*
+ * kernel/sched/throttle.c
+ *
+ * Best-Effort Task Throttling Framework
+ *
+ * Copyright (C) 2019 CSL-KU
+ * 2019-03-23 Integration of BWLOCK++ throttling framework into the scheduler
+ * 2019-03-25 Enable runtime selection of throttling event
+ * 2019-03-26 Support up-to 2 throttling events simultaneously
+ * 2019-03-27 Support variable number of throttling events
+ * 2019-03-27 Code refactoring and cleanup
+ * 2019-03-29 Further refactoring to create an internal (kernel) interface
+ * 2019-03-30 Integrate with the RT-Gang framework
+ * 2019-07-07 Create automatic regulation events for bandwidth throttling
+ */
+
+#include "sched.h"
+#include "throttle.h"
+
+#include <linux/perf_event.h>
+#include <linux/debugfs.h>
+#include <linux/uaccess.h>
+#include <linux/kthread.h>
+
+#ifdef CONFIG_SCHED_RTGANG
+#include "rtg_throttle.h"
+#endif
+
+/*
+ * Globals: Define various global variables
+ */
+struct th_core_info __percpu *th_core_info;
+
+/*
+ * Throttle fair scheduler punishment factor
+ * default: 0 (No TFS)
+ */
+static int th_tfs_factor = 0;
+
+/*
+ * Current debug level