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
|
From 2328e7500823151cb2f119b847454e63e9da0f64 Mon Sep 17 00:00:00 2001
From: Masahito S <firelzrd@gmail.com>
Date: Thu, 3 Oct 2024 17:33:57 +0900
Subject: [PATCH] linux6.11.y-bore5.6.0
---
include/linux/sched.h | 20 +-
include/linux/sched/bore.h | 37 ++++
init/Kconfig | 17 ++
kernel/Kconfig.hz | 17 ++
kernel/fork.c | 5 +
kernel/sched/Makefile | 1 +
kernel/sched/bore.c | 380 +++++++++++++++++++++++++++++++++++++
kernel/sched/core.c | 7 +
kernel/sched/debug.c | 60 +++++-
kernel/sched/fair.c | 89 ++++++++-
kernel/sched/features.h | 4 +
kernel/sched/sched.h | 7 +
12 files changed, 639 insertions(+), 5 deletions(-)
create mode 100644 include/linux/sched/bore.h
create mode 100644 kernel/sched/bore.c
diff --git a/include/linux/sched.h b/include/linux/sched.h
index f8d150343d..2481cf0125 100644
--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -535,6 +535,14 @@ struct sched_statistics {
#endif /* CONFIG_SCHEDSTATS */
} ____cacheline_aligned;
+#ifdef CONFIG_SCHED_BORE
+struct sched_burst_cache {
+ u8 score;
+ u32 count;
+ u64 timestamp;
+};
+#endif // CONFIG_SCHED_BORE
+
struct sched_entity {
/* For load-balancing: */
struct load_weight load;
@@ -543,12 +551,22 @@ struct sched_entity {
u64 min_vruntime;
struct list_head group_node;
- unsigned int on_rq;
+ unsigned char on_rq;
+ unsigned char rel_deadline;
u64 exec_start;
u64 sum_exec_runtime;
u64 prev_sum_exec_runtime;
u64 vruntime;
+#ifdef CONFIG_SCHED_BORE
+ u64 burst_time;
+ u8 prev_burst_penalty;
+ u8 curr_burst_penalty;
+ u8 burst_penalty;
+ u8 burst_score;
+ struct sched_burst_cache child_burst;
+ struct sched_burst_cache group_burst;
+#endif // CONFIG_SCHED_BORE
s64 vlag;
u64 slice;
diff --git a/include/linux/sched/bore.h b/include/linux/sched/bore.h
new file mode 100644
index 0000000000..12a613a94f
--- /dev/null
+++ b/include/linux/sched/bore.h
@@ -0,0 +1,37 @@
+
+#include <linux/sched.h>
+#include <linux/sched/cputime.h>
+
+#ifndef _LINUX_SCHED_BORE_H
+#define _LINUX_SCHED_BORE_H
+
+#ifdef CONFIG_SCHED_BORE
+extern u8 __read_mostly sched_bore;
+extern u8 __read_mostly sched_burst_exclude_kthreads;
+extern u8 __read_mostly sched_burst_smoothness_long;
+extern u8 __read_mostly sched_burst_smoothness_short;
+extern u8 __read_mostly sched_burst_fork_atavistic;
+extern u8 __read_mostly sched_burst_parity_threshold;
+extern u8 __read_mostly sched_burst_penalty_offset;
+extern uint __read_mostly sched_burst_penalty_scale;
+extern uint __read_mostly sched_burst_cache_lifetime;
+extern uint __read_mostly sched_deadline_boost_mask;
+
+extern void update_burst_score(struct sched_entity *se);
+extern void update_burst_penalty(struct sched_entity *se);
+
+extern void restart_burst(struct sched_entity *se);
+extern void restart_burst_rescale_deadline(struct sched_entity *se);
+
+extern int sched_bore_update_handler(const struct ctl_table *table, int write,
+ void __user *buffer, size_t *lenp, loff_t *ppos);
+
+extern void sched_clone_bore(
+ struct task_struct *p, struct task_struct *parent, u64 clone_flags);
+
+extern void init_task_bore(struct task_struct *p);
+
+extern void reweight_entity(
+ struct cfs_rq *cfs_rq, struct sched_entity *se, unsigned long weight);
+#endif // CONFIG_SCHED_BORE
+#endif // _LINUX_SCHED_BORE_H
diff --git a/init/Kconfig b/init/Kconfig
index 5783a0b875..b648ed538c 100644
--- a/init/Kconfig
+++ b/init/Kconfig
@@ -1297,6 +1297,23 @@ config CHECKPOINT_RESTORE
If unsure, say N here.
+config SCHED_BORE
+ bool "Burst-Oriented Response Enhancer"
+ default y
+ help
+ In Desktop and Mobile computing, one might prefer interactive
+ tasks to keep responsive no matter what they run in the background.
+
+ Enabling this kernel feature modifies the scheduler to discriminate
+ tasks by their burst time (runtime since it last went sleeping or
+ yielding state) and prioritize those that run less bursty.
+ Such tasks usually include window compositor, widgets backend,
+ terminal emulator, video playback, games and so on.
+ With a little impact to scheduling fairness, it may improve
+ responsiveness especially under heavy background workload.
+
+ If unsure, say Y here.
+
config SCHED_AUTOGROUP
bool "Automatic process group scheduling"
select CGROUPS
diff --git a/kernel/Kconfig.hz b/kernel/Kconfig.hz
index 38ef6d0688..253c566b59 100644
--- a/kernel/Kconfig.hz
+++ b/kernel/Kconfig.hz
@@ -55,5 +55,22 @@ config HZ
default 300 if HZ_300
default 1000 if HZ_1000
+config MIN_BASE_SLICE_NS
+ int "Default value for min_base_slice_ns"
+ default 2000000
+ help
+ The BORE Scheduler automatically calculates the optimal base
+ slice for the configured HZ using the following equation:
+
+ base_slice_ns =
+ 1000000000/HZ * DIV_ROUNDUP(min_base_slice_ns, 1000000000/HZ)
+
+ This option sets the default lower bound limit of the base slice
+ to prevent the loss of task throughput due to overscheduling.
+
+ Setting this value too high can cause the system to boot with
+ an unnecessarily large base slice, resulting in high scheduling
+ latency and poor system responsiveness.
+
config SCHED_HRTICK
def_bool HIGH_RES_TIMERS
diff --git a/kernel/fork.c b/kernel/fork.c
index cc760491f2..179b884da3 100644
--- a/kernel/fork.c
+++ b/kernel/fork.c
@@ -111,6 +111,8 @@
#include <asm/cacheflush.h>
#include <asm/tlbflush.h>
+#include <linux/sched/bore.h>
+
#include <trace/events/sched.h>
#define CREATE_TRACE_POINTS
@@ -2344,6 +2346,9 @@ __latent_entropy struct task_struct *copy_process(
retval = sched_fork(clone_flags, p);
if (retval)
goto bad_fork_cleanup_policy;
+#ifdef CONFIG_SCHED_BORE
+ sched_clone_bore(p, current, clone_flags);
+#endif // CONFIG_SCHED_BORE
retval = perf_event_init_task(p, clone_flags);
if (retval)
diff --git a/kernel/sched/Makefile b/kernel/sched/Makefile
index 976092b7bd..293aad6754 100644
--- a/kernel/sched/Makefile
+++ b/kernel/sched/Makefile
@@ -32,3 +32,4 @@ obj-y += core.o
obj-y += fair.o
obj-y += build_policy.o
obj-y += build_utility.o
+obj-y += bore.o
diff --git a/kernel/sched/bore.c b/kernel/sched/bore.c
new file mode 100644
index 0000000000..62a0191a32
--- /dev/null
+++ b/kernel/sched/bore.c
@@ -0,0 +1,380 @@
+/*
+ * Burst-Oriented Response Enhancer (BORE) CPU Scheduler
+ * Copyright (C) 2021-2024 Masahito Suzuki <firelzrd@gmail.com>
+ */
+#include <linux/cpuset.h>
+#include <linux/sched/bore.h>
+#include "sched.h"
+
+#ifdef CONFIG_SCHED_BORE
+u8 __read_mostly sched_bore = 1;
+u8 __read_mostly sched_burst_exclude_kthreads = 1;
+u8 __read_mostly sched_burst_smoothness_long = 1;
+u8 __read_mostly sched_burst_smoothness_short = 0;
+u8 __read_mostly sched_burst_fork_atavistic = 2;
+u8 __read_mostly sched_burst_parity_threshold = 2;
+u8 __read_mostly sched_burst_penalty_offset = 24;
+uint __read_mostly sched_burst_penalty_scale = 1280;
+uint __read_mostly sched_burst_cache_lifetime = 60000000;
+uint __read_mostly sched_deadline_boost_mask = ENQUEUE_INITIAL
+ | ENQUEUE_WAKEUP;
+static int __maybe_unused sixty_four = 64;
+static int __maybe_unused maxval_u8 = 255;
+static int __maybe_unused maxval_12_bits = 4095;
+
+#define MAX_BURST_PENALTY (39U <<2)
+
+static inline u32 log2plus1_u64_u32f8(u64 v) {
+ u32 integral = fls64(v);
+ u8 fractional = v << (64 - integral) >> 55;
+ return integral << 8 | fractional;
+}
+
+static inline u32 calc_burst_penalty(u64 burst_time) {
+ u32 greed, tolerance, penalty, scaled_penalty;
+
+ greed = log2plus1_u64_u32f8(burst_time);
+ tolerance = sched_burst_penalty_offset << 8;
+ penalty = max(0, (s32)(greed - tolerance));
+ scaled_penalty = penalty * sched_burst_penalty_scale >> 16;
+
+ return min(MAX_BURST_PENALTY, scaled_penalty);
+}
+
+static inline u64 __scale_slice(u64 delta, u8 score)
+{return mul_u64_u32_shr(delta, sched_prio_to_wmult[score], 22);}
+
+static inline u64 __unscale_slice(u64 delta, u8 score)
+{return mul_u64_u32_shr(delta, sched_prio_to_weight[score], 10);}
+
+static void reweight_task_by_prio(struct task_struct *p, int prio) {
+ struct sched_entity *se = &p->se;
+ unsigned long weight = scale_load(sched_prio_to_weight[prio]);
+
+ reweight_entity(cfs_rq_of(se), se, weight);
+ se->load.inv_weight = sched_prio_to_wmult[prio];
+}
+
+static inline u8 effective_prio(struct task_struct *p) {
+ u8 prio = p->static_prio - MAX_RT_PRIO;
+ if (likely(sched_bore))
+ prio += p->se.burst_score;
+ return min(39, prio);
+}
+
+void update_burst_score(struct sched_entity *se) {
+ if (!entity_is_task(se)) return;
+ struct task_struct *p = task_of(se);
+ u8 prev_prio = effective_prio(p);
+
+ u8 burst_score = 0;
+ if (!((p->flags & PF_KTHREAD) && likely(sched_burst_exclude_kthreads)))
+ burst_score = se->burst_penalty >> 2;
+ se->burst_score = burst_score;
+
+ u8 new_prio = effective_prio(p);
+ if (new_prio != prev_prio)
+ reweight_task_by_prio(p, new_prio);
+}
+
+void update_burst_penalty(struct sched_entity *se) {
+ se->curr_burst_penalty = calc_burst_penalty(se->burst_time);
+ se->burst_penalty = max(se->prev_burst_penalty, se->curr_burst_penalty);
+ update_burst_score(se);
+}
+
+static inline u32 binary_smooth(u32 new, u32 old) {
+ int increment = new - old;
+ return (0 <= increment)?
+ old + ( increment >> (int)sched_burst_smoothness_long):
+ old - (-increment >> (int)sched_burst_smoothness_short);
+}
+
+static void revolve_burst_penalty(struct sched_entity *se) {
+ se->prev_burst_penalty =
+ binary_smooth(se->curr_burst_penalty, se->prev_burst_penalty);
+ se->burst_time = 0;
+ se->curr_burst_penalty = 0;
+}
+
+inline void restart_burst(struct sched_entity *se) {
+ revolve_burst_penalty(se);
+ se->burst_penalty = se->prev_burst_penalty;
+ update_burst_score(se);
+}
+
+void restart_burst_rescale_deadline(struct sched_entity *se) {
+ s64 vscaled, wremain, vremain = se->deadline - se->vruntime;
+ struct task_struct *p = task_of(se);
+ u8 prev_prio = effective_prio(p);
+ restart_burst(se);
+ u8 new_prio = effective_prio(p);
+ if (prev_prio > new_prio) {
+ wremain = __unscale_slice(abs(vremain), prev_prio);
+ vscaled = __scale_slice(wremain, new_prio);
+ if (unlikely(vremain < 0))
+ vscaled = -vscaled;
+ se->deadline = se->vruntime + vscaled;
+ }
+}
+
+static void reset_task_weights_bore(void) {
+ struct task_struct *task;
+ struct rq *rq;
+ struct rq_flags rf;
+
+ write_lock_irq(&tasklist_lock);
+ for_each_process(task) {
+ rq = task_rq(task);
+ rq_lock_irqsave(rq, &rf);
+ reweight_task_by_prio(task, effective_prio(task));
+ rq_unlock_irqrestore(rq, &rf);
+ }
+ write_unlock_irq(&tasklist_lock);
+}
+
+int sched_bore_update_handler(const struct ctl_table *table, int write,
+ void __user *buffer, size_t *lenp, loff_t *ppos) {
+ int ret = proc_dou8vec_minmax(table, write, buffer, lenp, ppos);
+ if (ret || !write)
+ return ret;
+
+ reset_task_weights_bore();
+
+ return 0;
+}
+
+static u32 count_child_tasks(struct task_struct *p) {
+ struct task_struct *child;
+ u32 cnt = 0;
+ list_for_each_entry(child, &p->children, sibling) {cnt++;}
+ return cnt;
+}
+
+static inline bool task_is_bore_eligible(struct task_struct *p)
+{return p->sched_class == &fair_sched_class;}
+
+static inline bool burst_cache_expired(struct sched_burst_cache *bc, u64 now)
+{return (s64)(bc->timestamp + sched_burst_cache_lifetime - now) < 0;}
+
+static void update_burst_cache(struct sched_burst_cache *bc,
+ struct task_struct *p, u32 cnt, u32 sum, u64 now) {
+ u8 avg = cnt ? sum / cnt : 0;
+ bc->score = max(avg, p->se.burst_penalty);
+ bc->count = cnt;
+ bc->timestamp = now;
+}
+
+static inline void update_child_burst_direct(struct task_struct *p, u64 now) {
+ u32 cnt = 0, sum = 0;
+ struct task_struct *child;
+
+ list_for_each_entry(child, &p->children, sibling) {
+ if (!task_is_bore_eligible(child)) continue;
+ cnt++;
+ sum += child->se.burst_penalty;
+ }
+
+ update_burst_cache(&p->se.child_burst, p, cnt, sum, now);
+}
+
+static inline u8 inherit_burst_direct(struct task_struct *p, u64 now) {
+ struct task_struct *parent = p;
+ if (burst_cache_expired(&parent->se.child_burst, now))
+ update_child_burst_direct(parent, now);
+
+ return parent->se.child_burst.score;
+}
+
+static void update_child_burst_topological(
+ struct task_struct *p, u64 now, u32 depth, u32 *acnt, u32 *asum) {
+ u32 cnt = 0, dcnt = 0, sum = 0;
+ struct task_struct *child, *dec;
+
+ list_for_each_entry(child, &p->children, sibling) {
+ dec = child;
+ while ((dcnt = count_child_tasks(dec)) == 1)
+ dec = list_first_entry(&dec->children, struct task_struct, sibling);
+
+ if (!dcnt || !depth) {
+ if (!task_is_bore_eligible(dec)) continue;
+ cnt++;
+ sum += dec->se.burst_penalty;
+ continue;
+ }
+ if (!burst_cache_expired(&dec->se.child_burst, now)) {
+ cnt += dec->se.child_burst.count;
+ sum += (u32)dec->se.child_burst.score * dec->se.child_burst.count;
+ continue;
+ }
+ update_child_burst_topological(dec, now, depth - 1, &cnt, &sum);
+ }
+
+ update_burst_cache(&p->se.child_burst, p, cnt, sum, now);
+ *acnt += cnt;
+ *asum += sum;
+}
+
+static inline u8 inherit_burst_topological(struct task_struct *p, u64 now) {
+ struct task_struct *anc = p;
+ u32 cnt = 0, sum = 0;
+
+ while (anc->real_parent != anc && count_child_tasks(anc) == 1)
+ anc = anc->real_parent;
+
+ if (burst_cache_expired(&anc->se.child_burst, now))
+ update_child_burst_topological(
+ anc, now, sched_burst_fork_atavistic - 1, &cnt, &sum);
+
+ return anc->se.child_burst.score;
+}
+
+static inline void update_tg_burst(struct task_struct *p, u64 now) {
+ struct task_struct *task;
+ u32 cnt = 0, sum = 0;
+
+ for_each_thread(p, task) {
+ if (!task_is_bore_eligible(task)) continue;
+ cnt++;
+ sum += task->se.burst_penalty;
+ }
+
+ update_burst_cache(&p->se.group_burst, p, cnt, sum, now);
+}
+
+static inline u8 inherit_burst_tg(struct task_struct *p, u64 now) {
+ struct task_struct *parent = p->group_leader;
+ if (burst_cache_expired(&parent->se.group_burst, now))
+ update_tg_burst(parent, now);
+
+ return parent->se.group_burst.score;
+}
+
+void sched_clone_bore(
+ struct task_struct *p, struct task_struct *parent, u64 clone_flags) {
+ if (!task_is_bore_eligible(p)) return;
+
+ u64 now = ktime_get_ns();
+ read_lock(&tasklist_lock);
+ u8 penalty = (clone_flags & CLONE_THREAD) ?
+ inherit_burst_tg(parent, now) :
+ likely(sched_burst_fork_atavistic) ?
+ inherit_burst_topological(parent, now):
+ inherit_burst_direct(parent, now);
+ read_unlock(&tasklist_lock);
+
+ struct sched_entity *se = &p->se;
+ revolve_burst_penalty(se);
+ se->burst_penalty = se->prev_burst_penalty =
+ max(se->prev_burst_penalty, penalty);
+ se->child_burst.timestamp = 0;
+ se->group_burst.timestamp = 0;
+}
+
+void init_task_bore(struct task_struct *p) {
+ p->se.burst_time = 0;
+ p->se.prev_burst_penalty = 0;
+ p->se.curr_burst_penalty = 0;
+ p->se.burst_penalty = 0;
+ p->se.burst_score = 0;
+ memset(&p->se.child_burst, 0, sizeof(struct sched_burst_cache));
+ memset(&p->se.group_burst, 0, sizeof(struct sched_burst_cache));
+}
+
+#ifdef CONFIG_SYSCTL
+static struct ctl_table sched_bore_sysctls[] = {
+ {
+ .procname = "sched_bore",
+ .data = &sched_bore,
+ .maxlen = sizeof(u8),
+ .mode = 0644,
+ .proc_handler = sched_bore_update_handler,
+ .extra1 = SYSCTL_ZERO,
+ .extra2 = SYSCTL_ONE,
+ },
+ {
+ .procname = "sched_burst_exclude_kthreads",
+ .data = &sched_burst_exclude_kthreads,
+ .maxlen = sizeof(u8),
+ .mode = 0644,
+ .proc_handler = proc_dou8vec_minmax,
+ .extra1 = SYSCTL_ZERO,
+ .extra2 = SYSCTL_ONE,
+ },
+ {
+ .procname = "sched_burst_smoothness_long",
+ .data = &sched_burst_smoothness_long,
+ .maxlen = sizeof(u8),
+ .mode = 0644,
+ .proc_handler = proc_dou8vec_minmax,
+ .extra1 = SYSCTL_ZERO,
+ .extra2 = SYSCTL_ONE,
+ },
+ {
+ .procname = "sched_burst_smoothness_short",
+ .data = &sched_burst_smoothness_short,
+ .maxlen = sizeof(u8),
+ .mode = 0644,
+ .proc_handler = proc_dou8vec_minmax,
+ .extra1 = SYSCTL_ZERO,
+ .extra2 = SYSCTL_ONE,
+ },
+ {
+ .procname = "sched_burst_fork_atavistic",
+ .data = &sched_burst_fork_atavistic,
+ .maxlen = sizeof(u8),
+ .mode = 0644,
+ .proc_handler = proc_dou8vec_minmax,
+ .extra1 = SYSCTL_ZERO,
+ .extra2 = SYSCTL_THREE,
+ },
+ {
+ .procname = "sched_burst_parity_threshold",
+ .data = &sched_burst_parity_threshold,
+ .maxlen = sizeof(u8),
+ .mode = 0644,
+ .proc_handler = proc_dou8vec_minmax,
+ .extra1 = SYSCTL_ZERO,
+ .extra2 = &maxval_u8,
+ },
+ {
+ .procname = "sched_burst_penalty_offset",
+ .data = &sched_burst_penalty_offset,
+ .maxlen = sizeof(u8),
+ .mode = 0644,
+ .proc_handler = proc_dou8vec_minmax,
+ .extra1 = SYSCTL_ZERO,
+ .extra2 = &sixty_four,
+ },
+ {
+ .procname = "sched_burst_penalty_scale",
+ .data = &sched_burst_penalty_scale,
+ .maxlen = sizeof(uint),
+ .mode = 0644,
+ .proc_handler = proc_douintvec_minmax,
+ .extra1 = SYSCTL_ZERO,
+ .extra2 = &maxval_12_bits,
+ },
+ {
+ .procname = "sched_burst_cache_lifetime",
+ .data = &sched_burst_cache_lifetime,
+ .maxlen = sizeof(uint),
+ .mode = 0644,
+ .proc_handler = proc_douintvec,
+ },
+ {
+ .procname = "sched_deadline_boost_mask",
+ .data = &sched_deadline_boost_mask,
+ .maxlen = sizeof(uint),
+ .mode = 0644,
+ .proc_handler = proc_douintvec,
+ },
+};
+
+static int __init sched_bore_sysctl_init(void) {
+ register_sysctl_init("kernel", sched_bore_sysctls);
+ return 0;
+}
+late_initcall(sched_bore_sysctl_init);
+#endif // CONFIG_SYSCTL
+#endif // CONFIG_SCHED_BORE
diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index f3951e4a55..9264e542c9 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -97,6 +97,8 @@
#include "../../io_uring/io-wq.h"
#include "../smpboot.h"
+#include <linux/sched/bore.h>
+
EXPORT_TRACEPOINT_SYMBOL_GPL(ipi_send_cpu);
EXPORT_TRACEPOINT_SYMBOL_GPL(ipi_send_cpumask);
@@ -8197,6 +8199,11 @@ void __init sched_init(void)
BUG_ON(&dl_sched_class != &stop_sched_class + 1);
#endif
+#ifdef CONFIG_SCHED_BORE
+ printk(KERN_INFO "BORE (Burst-Oriented Response Enhancer) CPU Scheduler modification 5.6.0 by Masahito Suzuki");
+ init_task_bore(&init_task);
+#endif // CONFIG_SCHED_BORE
+
wait_bit_init();
#ifdef CONFIG_FAIR_GROUP_SCHED
diff --git a/kernel/sched/debug.c b/kernel/sched/debug.c
index c1eb9a1afd..e2da8d7738 100644
--- a/kernel/sched/debug.c
+++ b/kernel/sched/debug.c
@@ -167,7 +167,52 @@ static const struct file_operations sched_feat_fops = {
};
#ifdef CONFIG_SMP
+#ifdef CONFIG_SCHED_BORE
+static ssize_t sched_min_base_slice_write(struct file *filp, const char __user *ubuf,
+ size_t cnt, loff_t *ppos)
+{
+ char buf[16];
+ unsigned int value;
+
+ if (cnt > 15)
+ cnt = 15;
+
+ if (copy_from_user(&buf, ubuf, cnt))
+ return -EFAULT;
+ buf[cnt] = '\0';
+
+ if (kstrtouint(buf, 10, &value))
+ return -EINVAL;
+ if (!value)
+ return -EINVAL;
+
+ sysctl_sched_min_base_slice = value;
+ sched_update_min_base_slice();
+
+ *ppos += cnt;
+ return cnt;
+}
+
+static int sched_min_base_slice_show(struct seq_file *m, void *v)
+{
+ seq_printf(m, "%d\n", sysctl_sched_min_base_slice);
+ return 0;
+}
+
+static int sched_min_base_slice_open(struct inode *inode, struct file *filp)
+{
+ return single_open(filp, sched_min_base_slice_show, NULL);
+}
+
+static const struct file_operations sched_min_base_slice_fops = {
+ .open = sched_min_base_slice_open,
+ .write = sched_min_base_slice_write,
+ .read = seq_read,
+ .llseek = seq_lseek,
+ .release = single_release,
+};
+#else // !CONFIG_SCHED_BORE
static ssize_t sched_scaling_write(struct file *filp, const char __user *ubuf,
size_t cnt, loff_t *ppos)
{
@@ -213,7 +258,7 @@ static const struct file_operations sched_scaling_fops = {
.llseek = seq_lseek,
.release = single_release,
};
-
+#endif // CONFIG_SCHED_BORE
#endif /* SMP */
#ifdef CONFIG_PREEMPT_DYNAMIC
@@ -347,13 +392,20 @@ static __init int sched_init_debug(void)
debugfs_create_file("preempt", 0644, debugfs_sched, NULL, &sched_dynamic_fops);
#endif
+#ifdef CONFIG_SCHED_BORE
+ debugfs_create_file("min_base_slice_ns", 0644, debugfs_sched, NULL, &sched_min_base_slice_fops);
+ debugfs_create_u32("base_slice_ns", 0400, debugfs_sched, &sysctl_sched_base_slice);
+#else // !CONFIG_SCHED_BORE
debugfs_create_u32("base_slice_ns", 0644, debugfs_sched, &sysctl_sched_base_slice);
+#endif // CONFIG_SCHED_BORE
debugfs_create_u32("latency_warn_ms", 0644, debugfs_sched, &sysctl_resched_latency_warn_ms);
debugfs_create_u32("latency_warn_once", 0644, debugfs_sched, &sysctl_resched_latency_warn_once);
#ifdef CONFIG_SMP
+#if !defined(CONFIG_SCHED_BORE)
debugfs_create_file("tunable_scaling", 0644, debugfs_sched, NULL, &sched_scaling_fops);
+#endif // CONFIG_SCHED_BORE
debugfs_create_u32("migration_cost_ns", 0644, debugfs_sched, &sysctl_sched_migration_cost);
debugfs_create_u32("nr_migrate", 0644, debugfs_sched, &sysctl_sched_nr_migrate);
@@ -596,6 +648,9 @@ print_task(struct seq_file *m, struct rq *rq, struct task_struct *p)
SPLIT_NS(schedstat_val_or_zero(p->stats.sum_sleep_runtime)),
SPLIT_NS(schedstat_val_or_zero(p->stats.sum_block_runtime)));
+#ifdef CONFIG_SCHED_BORE
+ SEQ_printf(m, " %2d", p->se.burst_score);
+#endif // CONFIG_SCHED_BORE
#ifdef CONFIG_NUMA_BALANCING
SEQ_printf(m, " %d %d", task_node(p), task_numa_group_id(p));
#endif
@@ -1069,6 +1124,9 @@ void proc_sched_show_task(struct task_struct *p, struct pid_namespace *ns,
P(se.load.weight);
#ifdef CONFIG_SMP
+#ifdef CONFIG_SCHED_BORE
+ P(se.burst_score);
+#endif // CONFIG_SCHED_BORE
P(se.avg.load_sum);
P(se.avg.runnable_sum);
P(se.avg.util_sum);
diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
index 9057584ec0..465d2626ee 100644
--- a/kernel/sched/fair.c
+++ b/kernel/sched/fair.c
@@ -55,6 +55,8 @@
#include "stats.h"
#include "autogroup.h"
+#include <linux/sched/bore.h>
+
/*
* The initial- and re-scaling of tunables is configurable
*
@@ -64,17 +66,29 @@
* SCHED_TUNABLESCALING_LOG - scaled logarithmically, *1+ilog(ncpus)
* SCHED_TUNABLESCALING_LINEAR - scaled linear, *ncpus
*
- * (default SCHED_TUNABLESCALING_LOG = *(1+ilog(ncpus))
+ * (BORE default SCHED_TUNABLESCALING_NONE = *1 constant)
+ * (EEVDF default SCHED_TUNABLESCALING_LOG = *(1+ilog(ncpus))
*/
+#ifdef CONFIG_SCHED_BORE
+unsigned int sysctl_sched_tunable_scaling = SCHED_TUNABLESCALING_NONE;
+#else // !CONFIG_SCHED_BORE
unsigned int sysctl_sched_tunable_scaling = SCHED_TUNABLESCALING_LOG;
+#endif // CONFIG_SCHED_BORE
/*
* Minimal preemption granularity for CPU-bound tasks:
*
- * (default: 0.75 msec * (1 + ilog(ncpus)), units: nanoseconds)
+ * (BORE default: max(1 sec / HZ, min_base_slice) constant, units: nanoseconds)
+ * (EEVDF default: 0.75 msec * (1 + ilog(ncpus)), units: nanoseconds)
*/
+#ifdef CONFIG_SCHED_BORE
+unsigned int sysctl_sched_base_slice = 1000000000ULL / HZ;
+static unsigned int configured_sched_base_slice = 1000000000ULL / HZ;
+unsigned int sysctl_sched_min_base_slice = CONFIG_MIN_BASE_SLICE_NS;
+#else // !CONFIG_SCHED_BORE
unsigned int sysctl_sched_base_slice = 750000ULL;
static unsigned int normalized_sysctl_sched_base_slice = 750000ULL;
+#endif // CONFIG_SCHED_BORE
const_debug unsigned int sysctl_sched_migration_cost = 500000UL;
@@ -188,6 +202,18 @@ static inline void update_load_set(struct load_weight *lw, unsigned long w)
*
* This idea comes from the SD scheduler of Con Kolivas:
*/
+#ifdef CONFIG_SCHED_BORE
+static void update_sysctl(void) {
+ unsigned int base_slice = configured_sched_base_slice;
+ unsigned int min_base_slice = sysctl_sched_min_base_slice;
+
+ if (min_base_slice)
+ base_slice *= DIV_ROUND_UP(min_base_slice, base_slice);
+
+ sysctl_sched_base_slice = base_slice;
+}
+void sched_update_min_base_slice(void) { update_sysctl(); }
+#else // !CONFIG_SCHED_BORE
static unsigned int get_update_sysctl_factor(void)
{
unsigned int cpus = min_t(unsigned int, num_online_cpus(), 8);
@@ -218,6 +244,7 @@ static void update_sysctl(void)
SET_SYSCTL(sched_base_slice);
#undef SET_SYSCTL
}
+#endif // CONFIG_SCHED_BORE
void __init sched_init_granularity(void)
{
@@ -695,6 +722,9 @@ static s64 entity_lag(u64 avruntime, struct sched_entity *se)
vlag = avruntime - se->vruntime;
limit = calc_delta_fair(max_t(u64, 2*se->slice, TICK_NSEC), se);
+#ifdef CONFIG_SCHED_BORE
+ limit >>= !!sched_bore;
+#endif // CONFIG_SCHED_BORE
return clamp(vlag, -limit, limit);
}
@@ -896,6 +926,10 @@ static struct sched_entity *pick_eevdf(struct cfs_rq *cfs_rq)
* until it gets a new slice. See the HACK in set_next_entity().
*/
if (sched_feat(RUN_TO_PARITY) && curr && curr->vlag == curr->deadline)
+#ifdef CONFIG_SCHED_BORE
+ if (!(likely(sched_bore) && likely(sched_burst_parity_threshold) &&
+ sched_burst_parity_threshold < cfs_rq->nr_running))
+#endif // CONFIG_SCHED_BORE
return curr;
/* Pick the leftmost entity if it's eligible */
@@ -954,6 +988,7 @@ struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq)
* Scheduling class statistics methods:
*/
#ifdef CONFIG_SMP
+#if !defined(CONFIG_SCHED_BORE)
int sched_update_scaling(void)
{
unsigned int factor = get_update_sysctl_factor();
@@ -965,6 +1000,7 @@ int sched_update_scaling(void)
return 0;
}
+#endif // CONFIG_SCHED_BORE
#endif
#endif
@@ -1165,6 +1201,10 @@ static void update_curr(struct cfs_rq *cfs_rq)
if (unlikely(delta_exec <= 0))
return;
+#ifdef CONFIG_SCHED_BORE
+ curr->burst_time += delta_exec;
+ update_burst_penalty(curr);
+#endif // CONFIG_SCHED_BORE
curr->vruntime += calc_delta_fair(delta_exec, curr);
update_deadline(cfs_rq, curr);
update_min_vruntime(cfs_rq);
@@ -3782,7 +3822,7 @@ static void reweight_eevdf(struct sched_entity *se, u64 avruntime,
se->deadline = avruntime + vslice;
}
-static void reweight_entity(struct cfs_rq *cfs_rq, struct sched_entity *se,
+void reweight_entity(struct cfs_rq *cfs_rq, struct sched_entity *se,
unsigned long weight)
{
bool curr = cfs_rq->curr == se;
@@ -5189,6 +5229,9 @@ place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
*
* EEVDF: placement strategy #1 / #2
*/
+#ifdef CONFIG_SCHED_BORE
+ if (se->vlag)
+#endif // CONFIG_SCHED_BORE
if (sched_feat(PLACE_LAG) && cfs_rq->nr_running) {
struct sched_entity *curr = cfs_rq->curr;
unsigned long load;
@@ -5259,6 +5302,16 @@ place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
se->vruntime = vruntime - lag;
+ if (sched_feat(PLACE_REL_DEADLINE) && se->rel_deadline) {
+ se->deadline += se->vruntime;
+ se->rel_deadline = 0;
+ return;
+ }
+#ifdef CONFIG_SCHED_BORE
+ else if (likely(sched_bore))
+ vslice >>= !!(flags & sched_deadline_boost_mask);
+ else
+#endif // CONFIG_SCHED_BORE
/*
* When joining the competition; the existing tasks will be,
* on average, halfway through their slice, as such start tasks
@@ -5368,6 +5421,7 @@ static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq);
static void
dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
{
+ bool sleep = flags & DEQUEUE_SLEEP;
int action = UPDATE_TG;
if (entity_is_task(se) && task_on_rq_migrating(task_of(se)))
@@ -5395,6 +5449,11 @@ dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
clear_buddies(cfs_rq, se);
update_entity_lag(cfs_rq, se);
+ if (sched_feat(PLACE_REL_DEADLINE) && !sleep) {
+ se->deadline -= se->vruntime;
+ se->rel_deadline = 1;
+ }
+
if (se != cfs_rq->curr)
__dequeue_entity(cfs_rq, se);
se->on_rq = 0;
@@ -6846,6 +6905,14 @@ static void dequeue_task_fair(struct rq *rq, struct task_struct *p, int flags)
bool was_sched_idle = sched_idle_rq(rq);
util_est_dequeue(&rq->cfs, p);
+#ifdef CONFIG_SCHED_BORE
+ if (task_sleep) {
+ cfs_rq = cfs_rq_of(se);
+ if (cfs_rq->curr == se)
+ update_curr(cfs_rq);
+ restart_burst(se);
+ }
+#endif // CONFIG_SCHED_BORE
for_each_sched_entity(se) {
cfs_rq = cfs_rq_of(se);
@@ -8632,16 +8699,25 @@ static void yield_task_fair(struct rq *rq)
/*
* Are we the only task in the tree?
*/
+#if !defined(CONFIG_SCHED_BORE)
if (unlikely(rq->nr_running == 1))
return;
clear_buddies(cfs_rq, se);
+#endif // CONFIG_SCHED_BORE
update_rq_clock(rq);
/*
* Update run-time statistics of the 'current'.
*/
update_curr(cfs_rq);
+#ifdef CONFIG_SCHED_BORE
+ restart_burst_rescale_deadline(se);
+ if (unlikely(rq->nr_running == 1))
+ return;
+
+ clear_buddies(cfs_rq, se);
+#endif // CONFIG_SCHED_BORE
/*
* Tell update_rq_clock() that we've just updated,
* so we don't do microscopic update in schedule()
@@ -12716,6 +12792,9 @@ static void task_fork_fair(struct task_struct *p)
curr = cfs_rq->curr;
if (curr)
update_curr(cfs_rq);
+#ifdef CONFIG_SCHED_BORE
+ update_burst_score(se);
+#endif // CONFIG_SCHED_BORE
place_entity(cfs_rq, se, ENQUEUE_INITIAL);
rq_unlock(rq, &rf);
}
@@ -12828,6 +12907,10 @@ static void attach_task_cfs_rq(struct task_struct *p)
static void switched_from_fair(struct rq *rq, struct task_struct *p)
{
+ p->se.rel_deadline = 0;
+#ifdef CONFIG_SCHED_BORE
+ init_task_bore(p);
+#endif // CONFIG_SCHED_BORE
detach_task_cfs_rq(p);
}
diff --git a/kernel/sched/features.h b/kernel/sched/features.h
index 143f55df89..e97b7b68bd 100644
--- a/kernel/sched/features.h
+++ b/kernel/sched/features.h
@@ -6,6 +6,10 @@
*/
SCHED_FEAT(PLACE_LAG, true)
SCHED_FEAT(PLACE_DEADLINE_INITIAL, true)
+/*
+ * Preserve relative virtual deadline on 'migration'.
+ */
+SCHED_FEAT(PLACE_REL_DEADLINE, true)
SCHED_FEAT(RUN_TO_PARITY, true)
/*
diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h
index 4c36cc6803..cc18ad228e 100644
--- a/kernel/sched/sched.h
+++ b/kernel/sched/sched.h
@@ -1984,7 +1984,11 @@ static inline void update_sched_domain_debugfs(void) { }
static inline void dirty_sched_domain_sysctl(int cpu) { }
#endif
+#ifdef CONFIG_SCHED_BORE
+extern void sched_update_min_base_slice(void);
+#else // !CONFIG_SCHED_BORE
extern int sched_update_scaling(void);
+#endif // CONFIG_SCHED_BORE
static inline const struct cpumask *task_user_cpus(struct task_struct *p)
{
@@ -2601,6 +2605,9 @@ extern const_debug unsigned int sysctl_sched_nr_migrate;
extern const_debug unsigned int sysctl_sched_migration_cost;
extern unsigned int sysctl_sched_base_slice;
+#ifdef CONFIG_SCHED_BORE
+extern unsigned int sysctl_sched_min_base_slice;
+#endif // CONFIG_SCHED_BORE
#ifdef CONFIG_SCHED_DEBUG
extern int sysctl_resched_latency_warn_ms;
--
2.34.1
|