-
Notifications
You must be signed in to change notification settings - Fork 6
/
baby-dl-5.14.patch
3333 lines (3312 loc) · 86.4 KB
/
baby-dl-5.14.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
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/include/linux/sched.h b/include/linux/sched.h
index f6935787e7e8..caad916e704b 100644
--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -462,6 +462,18 @@ struct sched_statistics {
#endif
};
+#ifdef CONFIG_BS_SCHED
+struct bs_node {
+ struct bs_node* next;
+ struct bs_node* prev;
+ u64 deadline;
+ u64 prev_bursts;
+ u64 bursts;
+ u64 start_time;
+
+};
+#endif
+
struct sched_entity {
/* For load-balancing: */
struct load_weight load;
@@ -469,6 +481,10 @@ struct sched_entity {
struct list_head group_node;
unsigned int on_rq;
+#ifdef CONFIG_BS_SCHED
+ struct bs_node bs_node;
+#endif
+
u64 exec_start;
u64 sum_exec_runtime;
u64 vruntime;
diff --git a/init/Kconfig b/init/Kconfig
index 55f9f7738ebb..0646b73952f7 100644
--- a/init/Kconfig
+++ b/init/Kconfig
@@ -105,6 +105,15 @@ config THREAD_INFO_IN_TASK
One subtle change that will be needed is to use try_get_task_stack()
and put_task_stack() in save_thread_stack_tsk() and get_wchan().
+config BS_SCHED
+ bool "Baby Scheduler"
+ default y
+ select TICK_CPU_ACCOUNTING
+ select PREEMPT
+ help
+ This is a Deadline version of Baby Scheduler.
+
+
menu "General setup"
config BROKEN
@@ -789,6 +798,7 @@ menu "Scheduler features"
config UCLAMP_TASK
bool "Enable utilization clamping for RT/FAIR tasks"
depends on CPU_FREQ_GOV_SCHEDUTIL
+ depends on !BS_SCHED
help
This feature enables the scheduler to track the clamped utilization
of each CPU based on RUNNABLE tasks scheduled on that CPU.
@@ -954,6 +964,7 @@ config CGROUP_WRITEBACK
menuconfig CGROUP_SCHED
bool "CPU controller"
+ depends on !BS_SCHED
default n
help
This feature lets CPU scheduler recognize task groups and control CPU
@@ -1231,6 +1242,8 @@ config CHECKPOINT_RESTORE
config SCHED_AUTOGROUP
bool "Automatic process group scheduling"
+ default n
+ depends on !BS_SCHED
select CGROUPS
select CGROUP_SCHED
select FAIR_GROUP_SCHED
@@ -1420,6 +1433,7 @@ config BPF
menuconfig EXPERT
bool "Configure standard kernel features (expert users)"
# Unhide debug options, to make the on-by-default options visible
+ depends on !BS_SCHED
select DEBUG_KERNEL
help
This option allows certain base kernel options and settings
diff --git a/kernel/Kconfig.hz b/kernel/Kconfig.hz
index 38ef6d06888e..f1efcc6ba603 100644
--- a/kernel/Kconfig.hz
+++ b/kernel/Kconfig.hz
@@ -5,7 +5,7 @@
choice
prompt "Timer frequency"
- default HZ_250
+ default HZ_803
help
Allows the configuration of the timer frequency. It is customary
to have the timer interrupt run at 1000 Hz but 100 Hz may be more
@@ -40,6 +40,11 @@ choice
on SMP and NUMA systems and exactly dividing by both PAL and
NTSC frame rates for video and multimedia work.
+ config HZ_803
+ bool "803 HZ"
+ help
+ 803 Hz is the default for Baby (dl).
+
config HZ_1000
bool "1000 HZ"
help
@@ -53,6 +58,7 @@ config HZ
default 100 if HZ_100
default 250 if HZ_250
default 300 if HZ_300
+ default 803 if HZ_803
default 1000 if HZ_1000
config SCHED_HRTICK
diff --git a/kernel/Kconfig.preempt b/kernel/Kconfig.preempt
index 5876e30c5740..f5a195470121 100644
--- a/kernel/Kconfig.preempt
+++ b/kernel/Kconfig.preempt
@@ -2,7 +2,7 @@
choice
prompt "Preemption Model"
- default PREEMPT_NONE
+ default PREEMPT
config PREEMPT_NONE
bool "No Forced Preemption (Server)"
@@ -103,6 +103,7 @@ config PREEMPT_DYNAMIC
config SCHED_CORE
bool "Core Scheduling for SMT"
depends on SCHED_SMT
+ depends on !BS_SCHED
help
This option permits Core Scheduling, a means of coordinated task
selection across SMT siblings. When enabled -- see
diff --git a/kernel/sched/Makefile b/kernel/sched/Makefile
index 978fcfca5871..464b134de739 100644
--- a/kernel/sched/Makefile
+++ b/kernel/sched/Makefile
@@ -23,7 +23,7 @@ CFLAGS_core.o := $(PROFILING) -fno-omit-frame-pointer
endif
obj-y += core.o loadavg.o clock.o cputime.o
-obj-y += idle.o fair.o rt.o deadline.o
+obj-y += idle.o bs.o rt.o deadline.o
obj-y += wait.o wait_bit.o swait.o completion.o
obj-$(CONFIG_SMP) += cpupri.o cpudeadline.o topology.o stop_task.o pelt.o
diff --git a/kernel/sched/bs.c b/kernel/sched/bs.c
new file mode 100644
index 000000000000..b78dcf504202
--- /dev/null
+++ b/kernel/sched/bs.c
@@ -0,0 +1,961 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Baby Scheduler (BS) Class (SCHED_NORMAL/SCHED_BATCH)
+ *
+ * Copyright (C) 2021, Hamad Al Marri <hamad.s.almarri@gmail.com>
+ */
+#include "sched.h"
+#include "pelt.h"
+#include "fair_numa.h"
+#include "bs.h"
+
+#define TIME_PERIOD 200000000ULL
+#define TO_COMPLETE 5000000ULL
+
+#define MIN_DEADLINE_NS 590000ULL
+/* MIN_DEADLINE_NS * 2 */
+#define DEADLINE_NS 1180000ULL
+
+const s64 prio_factor[40] = {
+ /* -20 */ -1080000L, -980000L, -880000L, -780000L, -680000L,
+ /* -15 */ -580000L, -480000L, -380000L, -280000L, -180000L,
+ /* -10 */ -80000L, -79000L, -78000L, -77000L, -76000L,
+ /* -5 */ -75000L, -74000L, -73000L, -72000L, -71000L,
+ /* 0 */ 0L, 1080000L, 1090000L, 1100000L, 1110000L,
+ /* 5 */ 1120000L, 1130000L, 1140000L, 1150000L, 1160000L,
+ /* 10 */ 1170000L, 1180000L, 1190000L, 1200000L, 1210000L,
+ /* 15 */ 1220000L, 1230000L, 1240000L, 1250000L, 1260000L
+};
+
+#define YIELD_MARK(bsn) ((bsn)->deadline |= 0x8000000000000000ULL)
+#define YIELD_UNMARK(bsn) ((bsn)->deadline &= 0x7FFFFFFFFFFFFFFFULL)
+
+static inline void
+calc_bursts(struct sched_entity *se, u64 burst, u64 now)
+{
+ struct bs_node *bsn = &se->bs_node;
+
+ if ((s64) now - bsn->start_time >= TIME_PERIOD) {
+ bsn->start_time = now;
+ bsn->prev_bursts = bsn->bursts;
+ bsn->bursts = 0ULL;
+ }
+
+ bsn->bursts += burst;
+}
+
+static inline u64
+calc_deadline(struct cfs_rq *cfs_rq, struct sched_entity *se)
+{
+ struct task_struct *p = task_of(se);
+ s64 prio_diff;
+ u64 now = rq_clock(rq_of(cfs_rq));
+ u64 deadline = now + DEADLINE_NS;
+
+ if (PRIO_TO_NICE(p->prio) == 0)
+ return deadline;
+
+ prio_diff = prio_factor[PRIO_TO_NICE(p->prio) + 20];
+
+ return deadline + prio_diff;
+}
+
+static inline bool
+reached_deadline(struct cfs_rq *cfs_rq, struct sched_entity *se)
+{
+ u64 now = rq_clock(rq_of(cfs_rq));
+ s64 delta = se->bs_node.deadline - now;
+
+ return (delta <= 0);
+}
+
+static void update_curr(struct cfs_rq *cfs_rq)
+{
+ struct sched_entity *curr = cfs_rq->curr;
+ u64 now = rq_clock_task(rq_of(cfs_rq));
+ u64 delta_exec;
+
+ if (unlikely(!curr))
+ return;
+
+ delta_exec = now - curr->exec_start;
+ if (unlikely((s64)delta_exec <= 0))
+ return;
+
+ curr->exec_start = now;
+ curr->sum_exec_runtime += delta_exec;
+
+ calc_bursts(curr, delta_exec, now);
+ if (reached_deadline(cfs_rq, curr))
+ curr->bs_node.deadline = calc_deadline(cfs_rq, curr);
+}
+
+static void update_curr_fair(struct rq *rq)
+{
+ update_curr(cfs_rq_of(&rq->curr->se));
+}
+
+/**
+ * Does a have earlier deadline than b?
+ */
+static inline bool
+entity_before(struct bs_node *a, struct bs_node *b)
+{
+ return (s64)(a->deadline - b->deadline) < 0;
+}
+
+static inline bool
+can_resched_curr(struct sched_entity *curr)
+{
+ struct bs_node *bsn = &curr->bs_node;
+ u64 bursts = bsn->prev_bursts;
+ u64 curr_bursts = bsn->bursts;
+ u64 delta = 25000000ULL;
+
+ if (!bursts)
+ return true;
+ else if ((s64)(TIME_PERIOD - (bursts + delta)) <= 0)
+ return true;
+ else if ((s64)(curr_bursts - bursts) >= 0)
+ return true;
+ else if ((s64)(bursts - (curr_bursts + TO_COMPLETE)) > 0)
+ return true;
+
+ return false;
+}
+
+static inline void
+try_resched_curr(struct rq *rq, struct sched_entity *curr)
+{
+ if (can_resched_curr(curr))
+ resched_curr(rq);
+}
+
+#ifdef CONFIG_SCHED_HRTICK
+
+#define DIFF_DL(a, b) ( (s64)((a)->bs_node.deadline - (b)->bs_node.deadline) )
+#define DIFF_DL_NOW(a, now) ( (s64)((a)->bs_node.deadline - (now)) )
+
+static struct sched_entity *
+pick_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *curr);
+
+/*
+ * cs cm cd
+ * | min dl | |
+ * |<-------->| |
+ * n1 | n1 | n2 | n3
+ * @@@|@@@@@@@@@@|##########|****
+ * | c1 | |
+ * | ^ | c2 |
+ * |now | ^ |
+ * | | now |
+ *
+ * n1 & c1: hr(cm - c1)
+ * n2 & c1: hr(cd - c1)
+ * n3 & c1: hr(n3 - c1)
+ *
+ * n1 & c2: resched_curr
+ * n2 & c2: hr(cd - c2)
+ * n3 & c2: hr(n3 - c2)
+ */
+static void hrtick_start_fair(struct rq *rq, struct task_struct *pcurr)
+{
+ struct sched_entity *curr = &pcurr->se;
+ struct bs_node *c_bsn = &curr->bs_node;
+ struct sched_entity *next;
+ u64 now = rq_clock(rq);
+ s64 cm, cd, c_ran;
+
+ if (rq->cfs.h_nr_running < 2)
+ return;
+
+ if (!can_resched_curr(curr))
+ return;
+
+ next = pick_next_entity(&rq->cfs, NULL);
+
+ if (!next)
+ return;
+
+ c_ran = curr->sum_exec_runtime - curr->prev_sum_exec_runtime;
+ cm = MIN_DEADLINE_NS - c_ran;
+ cd = DIFF_DL_NOW(curr, now);
+
+ // if c1 (if 590us >= current ran)
+ if (cm >= 0) {
+ if (entity_before(&next->bs_node, c_bsn)) {
+ // if n1
+ if (DIFF_DL(curr, next) - MIN_DEADLINE_NS > 0)
+ hrtick_start(rq, cm);
+ // if n2
+ else
+ hrtick_start(rq, cd);
+ }
+ // if n3
+ else {
+ hrtick_start(rq, DIFF_DL_NOW(next, now));
+ }
+ }
+ // if c2
+ else {
+ if (entity_before(&next->bs_node, c_bsn)) {
+ // if n1
+ if (DIFF_DL(curr, next) - MIN_DEADLINE_NS > 0)
+ try_resched_curr(rq, curr);
+ // if n2
+ else
+ hrtick_start(rq, cd);
+ }
+ // if n3
+ else {
+ hrtick_start(rq, DIFF_DL_NOW(next, now));
+ }
+ }
+}
+
+/*
+ * called from enqueue/dequeue and updates the hrtick when the
+ * current task is from our class.
+ */
+static void hrtick_update(struct rq *rq)
+{
+ struct task_struct *curr = rq->curr;
+
+ if (!hrtick_enabled_fair(rq) || curr->sched_class != &fair_sched_class)
+ return;
+
+ hrtick_start_fair(rq, curr);
+}
+#else /* !CONFIG_SCHED_HRTICK */
+static inline void
+hrtick_start_fair(struct rq *rq, struct task_struct *curr)
+{
+}
+
+static inline void hrtick_update(struct rq *rq)
+{
+}
+#endif
+
+static void __enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
+{
+ struct bs_node *bsn = &se->bs_node;
+
+ bsn->next = bsn->prev = NULL;
+
+ // if empty
+ if (!cfs_rq->head) {
+ cfs_rq->head = bsn;
+ }
+ else {
+ bsn->next = cfs_rq->head;
+ cfs_rq->head->prev = bsn;
+ cfs_rq->head = bsn;
+ }
+}
+
+static void __dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
+{
+ struct bs_node *bsn = &se->bs_node;
+ struct bs_node *prev, *next;
+
+ // if only one se in rq
+ if (cfs_rq->head->next == NULL) {
+ cfs_rq->head = NULL;
+ }
+ // if it is the head
+ else if (bsn == cfs_rq->head) {
+ cfs_rq->head = cfs_rq->head->next;
+ cfs_rq->head->prev = NULL;
+ }
+ // if in the middle
+ else {
+ prev = bsn->prev;
+ next = bsn->next;
+
+ prev->next = next;
+ if (next)
+ next->prev = prev;
+ }
+}
+
+static void
+enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
+{
+ bool curr = cfs_rq->curr == se;
+ bool renorm = !(flags & ENQUEUE_WAKEUP) || (flags & ENQUEUE_MIGRATED);
+
+ update_curr(cfs_rq);
+
+ /*
+ * Renormalise, such that we're placed at the current
+ * moment in time, instead of some random moment in the past. Being
+ * placed in the past could significantly boost this task to the
+ * fairness detriment of existing tasks.
+ */
+ if (renorm && !curr)
+ se->bs_node.deadline = calc_deadline(cfs_rq, se);
+
+ account_entity_enqueue(cfs_rq, se);
+
+ if (!curr)
+ __enqueue_entity(cfs_rq, se);
+
+ se->on_rq = 1;
+}
+
+static void
+dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
+{
+ int task_sleep = flags & DEQUEUE_SLEEP;
+ struct task_struct *p = task_of(se);
+
+ /*
+ * Check if didn't reach its deadline, then
+ * rise its priority
+ */
+ if (task_sleep && !reached_deadline(cfs_rq, se) &&
+ PRIO_TO_NICE(p->prio) > -20)
+ {
+ p->prio--;
+ }
+
+ update_curr(cfs_rq);
+
+ if (se != cfs_rq->curr)
+ __dequeue_entity(cfs_rq, se);
+
+ se->on_rq = 0;
+ account_entity_dequeue(cfs_rq, se);
+}
+
+static void
+enqueue_task_fair(struct rq *rq, struct task_struct *p, int flags)
+{
+ struct sched_entity *se = &p->se;
+ struct cfs_rq *cfs_rq = cfs_rq_of(se);
+ int idle_h_nr_running = task_has_idle_policy(p);
+
+ if (!se->on_rq) {
+ enqueue_entity(cfs_rq, se, flags);
+ cfs_rq->h_nr_running++;
+ cfs_rq->idle_h_nr_running += idle_h_nr_running;
+ }
+
+ add_nr_running(rq, 1);
+ hrtick_update(rq);
+}
+
+static void dequeue_task_fair(struct rq *rq, struct task_struct *p, int flags)
+{
+ struct sched_entity *se = &p->se;
+ struct cfs_rq *cfs_rq = cfs_rq_of(se);
+ int idle_h_nr_running = task_has_idle_policy(p);
+
+ dequeue_entity(cfs_rq, se, flags);
+
+ cfs_rq->h_nr_running--;
+ cfs_rq->idle_h_nr_running -= idle_h_nr_running;
+
+ sub_nr_running(rq, 1);
+ hrtick_update(rq);
+}
+
+static void yield_task_fair(struct rq *rq)
+{
+ struct task_struct *curr = rq->curr;
+ struct cfs_rq *cfs_rq = task_cfs_rq(curr);
+
+ YIELD_MARK(&curr->se.bs_node);
+
+ /*
+ * Are we the only task in the tree?
+ */
+ if (unlikely(rq->nr_running == 1))
+ return;
+
+ if (curr->policy != SCHED_BATCH) {
+ update_rq_clock(rq);
+ /*
+ * Update run-time statistics of the 'current'.
+ */
+ update_curr(cfs_rq);
+ /*
+ * Tell update_rq_clock() that we've just updated,
+ * so we don't do microscopic update in schedule()
+ * and double the fastpath cost.
+ */
+ rq_clock_skip_update(rq);
+ }
+}
+
+static bool yield_to_task_fair(struct rq *rq, struct task_struct *p)
+{
+ yield_task_fair(rq);
+ return true;
+}
+
+static void
+set_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
+{
+ int nice = PRIO_TO_NICE(task_of(se)->prio);
+
+ if (se->on_rq)
+ __dequeue_entity(cfs_rq, se);
+
+ se->exec_start = rq_clock_task(rq_of(cfs_rq));
+ cfs_rq->curr = se;
+ se->prev_sum_exec_runtime = se->sum_exec_runtime;
+
+ if (nice >= 0 && reached_deadline(cfs_rq, se))
+ se->bs_node.deadline = calc_deadline(cfs_rq, se);
+}
+
+static struct sched_entity *
+pick_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *curr)
+{
+ struct bs_node *bsn = cfs_rq->head;
+ struct bs_node *next;
+
+ if (!bsn)
+ return curr;
+
+ next = bsn->next;
+ while (next) {
+ if (entity_before(next, bsn))
+ bsn = next;
+
+ next = next->next;
+ }
+
+ if (curr && entity_before(&curr->bs_node, bsn))
+ return curr;
+
+ return se_of(bsn);
+}
+
+struct task_struct *
+pick_next_task_fair(struct rq *rq, struct task_struct *prev, struct rq_flags *rf)
+{
+ struct cfs_rq *cfs_rq = &rq->cfs;
+ struct sched_entity *se;
+ struct task_struct *p;
+ int new_tasks;
+
+again:
+ if (!sched_fair_runnable(rq))
+ goto idle;
+
+ if (prev)
+ put_prev_task(rq, prev);
+
+ se = pick_next_entity(cfs_rq, NULL);
+ set_next_entity(cfs_rq, se);
+
+ p = task_of(se);
+
+ if (prev)
+ YIELD_UNMARK(&prev->se.bs_node);
+
+done: __maybe_unused;
+#ifdef CONFIG_SMP
+ /*
+ * Move the next running task to the front of
+ * the list, so our cfs_tasks list becomes MRU
+ * one.
+ */
+ list_move(&p->se.group_node, &rq->cfs_tasks);
+#endif
+
+ if (hrtick_enabled_fair(rq))
+ hrtick_start_fair(rq, p);
+
+ return p;
+
+idle:
+ if (!rf)
+ return NULL;
+
+ new_tasks = newidle_balance(rq, rf);
+
+ /*
+ * Because newidle_balance() releases (and re-acquires) rq->lock, it is
+ * possible for any higher priority task to appear. In that case we
+ * must re-start the pick_next_entity() loop.
+ */
+ if (new_tasks < 0)
+ return RETRY_TASK;
+
+ if (new_tasks > 0)
+ goto again;
+
+ /*
+ * rq is about to be idle, check if we need to update the
+ * lost_idle_time of clock_pelt
+ */
+ update_idle_rq_clock_pelt(rq);
+
+ return NULL;
+}
+
+static struct task_struct *__pick_next_task_fair(struct rq *rq)
+{
+ return pick_next_task_fair(rq, NULL, NULL);
+}
+
+#ifdef CONFIG_SMP
+static struct task_struct *pick_task_fair(struct rq *rq)
+{
+ struct sched_entity *se;
+ struct cfs_rq *cfs_rq = &rq->cfs;
+ struct sched_entity *curr = cfs_rq->curr;
+
+ if (!cfs_rq->nr_running)
+ return NULL;
+
+ /* When we pick for a remote RQ, we'll not have done put_prev_entity() */
+ if (curr) {
+ if (curr->on_rq)
+ update_curr(cfs_rq);
+ else
+ curr = NULL;
+ }
+
+ se = pick_next_entity(cfs_rq, curr);
+
+ return task_of(se);
+}
+#endif
+
+static void put_prev_entity(struct cfs_rq *cfs_rq, struct sched_entity *prev)
+{
+ /*
+ * If still on the runqueue then deactivate_task()
+ * was not called and update_curr() has to be done:
+ */
+ if (prev->on_rq)
+ update_curr(cfs_rq);
+
+ if (prev->on_rq)
+ __enqueue_entity(cfs_rq, prev);
+
+ cfs_rq->curr = NULL;
+}
+
+static void put_prev_task_fair(struct rq *rq, struct task_struct *prev)
+{
+ struct sched_entity *se = &prev->se;
+
+ put_prev_entity(cfs_rq_of(se), se);
+}
+
+static void set_next_task_fair(struct rq *rq, struct task_struct *p, bool first)
+{
+ struct sched_entity *se = &p->se;
+ struct cfs_rq *cfs_rq = cfs_rq_of(se);
+
+#ifdef CONFIG_SMP
+ if (task_on_rq_queued(p)) {
+ /*
+ * Move the next running task to the front of the list, so our
+ * cfs_tasks list becomes MRU one.
+ */
+ list_move(&se->group_node, &rq->cfs_tasks);
+ }
+#endif
+
+ set_next_entity(cfs_rq, se);
+}
+
+static void
+check_preempt_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr)
+{
+ if (pick_next_entity(cfs_rq, curr) != curr)
+ try_resched_curr(rq_of(cfs_rq), curr);
+}
+
+static void
+entity_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr, int queued)
+{
+ update_curr(cfs_rq);
+
+#ifdef CONFIG_SCHED_HRTICK
+ /*
+ * queued ticks are scheduled to match the slice, so don't bother
+ * validating it and just reschedule.
+ */
+ if (queued) {
+ try_resched_curr(rq_of(cfs_rq), curr);
+ return;
+ }
+
+ /*
+ * don't let the period tick interfere with the hrtick preemption
+ */
+ if (!sched_feat(DOUBLE_TICK) &&
+ hrtimer_active(&rq_of(cfs_rq)->hrtick_timer))
+ return;
+#endif
+
+ if (cfs_rq->nr_running > 1)
+ check_preempt_tick(cfs_rq, curr);
+}
+
+static void check_preempt_wakeup(struct rq *rq, struct task_struct *p, int wake_flags)
+{
+ struct task_struct *curr = rq->curr;
+ struct sched_entity *se = &curr->se, *wse = &p->se;
+
+ if (unlikely(se == wse))
+ return;
+
+ if (test_tsk_need_resched(curr))
+ return;
+
+ /* Idle tasks are by definition preempted by non-idle tasks. */
+ if (unlikely(task_has_idle_policy(curr)) &&
+ likely(!task_has_idle_policy(p)))
+ goto preempt;
+
+ /*
+ * Batch and idle tasks do not preempt non-idle tasks (their preemption
+ * is driven by the tick):
+ */
+ if (unlikely(p->policy != SCHED_NORMAL) || !sched_feat(WAKEUP_PREEMPTION))
+ return;
+
+ /*
+ * Lower priority tasks do not preempt higher ones
+ */
+ if (p->prio > curr->prio)
+ return;
+
+ update_curr(cfs_rq_of(se));
+
+ if (entity_before(&wse->bs_node, &se->bs_node))
+ goto preempt;
+
+ return;
+
+preempt:
+ try_resched_curr(rq, se);
+}
+
+#ifdef CONFIG_SMP
+static int
+balance_fair(struct rq *rq, struct task_struct *prev, struct rq_flags *rf)
+{
+ if (rq->nr_running)
+ return 1;
+
+ return newidle_balance(rq, rf) != 0;
+}
+
+static int
+select_task_rq_fair(struct task_struct *p, int prev_cpu, int wake_flags)
+{
+ struct rq *rq = cpu_rq(prev_cpu);
+ unsigned int min_this = rq->nr_running;
+ unsigned int min = rq->nr_running;
+ int cpu, new_cpu = prev_cpu;
+
+ for_each_online_cpu(cpu) {
+ if (cpu_rq(cpu)->nr_running < min) {
+ new_cpu = cpu;
+ min = cpu_rq(cpu)->nr_running;
+ }
+ }
+
+ if (min == min_this)
+ return prev_cpu;
+
+ return new_cpu;
+}
+
+static int
+can_migrate_task(struct task_struct *p, int dst_cpu, struct rq *src_rq)
+{
+ if (task_running(src_rq, p))
+ return 0;
+
+ /* Disregard pcpu kthreads; they are where they need to be. */
+ if (kthread_is_per_cpu(p))
+ return 0;
+
+ if (!cpumask_test_cpu(dst_cpu, p->cpus_ptr))
+ return 0;
+
+ return 1;
+}
+
+static void pull_from(struct rq *dist_rq,
+ struct rq *src_rq,
+ struct rq_flags *src_rf,
+ struct task_struct *p)
+{
+ struct rq_flags rf;
+
+ // detach task
+ deactivate_task(src_rq, p, DEQUEUE_NOCLOCK);
+ set_task_cpu(p, cpu_of(dist_rq));
+
+ // unlock src rq
+ rq_unlock(src_rq, src_rf);
+
+ // lock dist rq
+ rq_lock(dist_rq, &rf);
+ update_rq_clock(dist_rq);
+
+ activate_task(dist_rq, p, ENQUEUE_NOCLOCK);
+ check_preempt_curr(dist_rq, p, 0);
+
+ // unlock dist rq
+ rq_unlock(dist_rq, &rf);
+
+ local_irq_restore(src_rf->flags);
+}
+
+static int move_task(struct rq *dist_rq, struct rq *src_rq,
+ struct rq_flags *src_rf)
+{
+ struct cfs_rq *src_cfs_rq = &src_rq->cfs;
+ struct task_struct *p;
+ struct bs_node *bsn = src_cfs_rq->head;
+
+ while (bsn) {
+ p = task_of(se_of(bsn));
+ if (can_migrate_task(p, cpu_of(dist_rq), src_rq)) {
+ pull_from(dist_rq, src_rq, src_rf, p);
+ return 1;
+ }
+
+ bsn = bsn->next;
+ }
+
+ /*
+ * Here we know we have not migrated any task,
+ * thus, we need to unlock and return 0
+ * Note: the pull_from does the unlocking for us.
+ */
+ rq_unlock(src_rq, src_rf);
+ local_irq_restore(src_rf->flags);
+
+ return 0;
+}
+
+static int newidle_balance(struct rq *this_rq, struct rq_flags *rf)
+{
+ int this_cpu = this_rq->cpu;
+ struct rq *src_rq;
+ int src_cpu = -1, cpu;
+ int pulled_task = 0;
+ unsigned int max = 0;
+ struct rq_flags src_rf;
+
+ /*
+ * We must set idle_stamp _before_ calling idle_balance(), such that we
+ * measure the duration of idle_balance() as idle time.
+ */
+ this_rq->idle_stamp = rq_clock(this_rq);
+
+ /*
+ * Do not pull tasks towards !active CPUs...
+ */
+ if (!cpu_active(this_cpu))
+ return 0;
+
+ rq_unpin_lock(this_rq, rf);
+ raw_spin_unlock(&this_rq->__lock);
+
+ for_each_online_cpu(cpu) {
+ /*
+ * Stop searching for tasks to pull if there are
+ * now runnable tasks on this rq.
+ */
+ if (this_rq->nr_running > 0)
+ goto out;
+
+ if (cpu == this_cpu)
+ continue;
+
+ src_rq = cpu_rq(cpu);
+
+ if (src_rq->nr_running < 2)
+ continue;
+
+ if (src_rq->nr_running > max) {
+ max = src_rq->nr_running;
+ src_cpu = cpu;
+ }
+ }
+
+ if (src_cpu != -1) {
+ src_rq = cpu_rq(src_cpu);
+
+ rq_lock_irqsave(src_rq, &src_rf);
+ update_rq_clock(src_rq);
+
+ if (src_rq->nr_running < 2) {
+ rq_unlock(src_rq, &src_rf);
+ local_irq_restore(src_rf.flags);
+ } else {
+ pulled_task = move_task(this_rq, src_rq, &src_rf);
+ }
+ }
+
+out:
+ raw_spin_lock(&this_rq->__lock);
+
+ /*
+ * While browsing the domains, we released the rq lock, a task could
+ * have been enqueued in the meantime. Since we're not going idle,
+ * pretend we pulled a task.
+ */
+ if (this_rq->cfs.h_nr_running && !pulled_task)
+ pulled_task = 1;
+
+ /* Is there a task of a high priority class? */
+ if (this_rq->nr_running != this_rq->cfs.h_nr_running)
+ pulled_task = -1;
+
+ if (pulled_task)
+ this_rq->idle_stamp = 0;
+
+ rq_repin_lock(this_rq, rf);
+
+ return pulled_task;
+}
+
+static inline int on_null_domain(struct rq *rq)
+{
+ return unlikely(!rcu_dereference_sched(rq->sd));
+}
+
+void trigger_load_balance(struct rq *this_rq)
+{
+ int this_cpu = cpu_of(this_rq);
+ int cpu;
+ unsigned int max, min;
+ struct rq *max_rq, *min_rq, *c_rq;
+ struct rq_flags src_rf;