-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy path0001-bore.patch
More file actions
1203 lines (1134 loc) · 36.9 KB
/
0001-bore.patch
File metadata and controls
1203 lines (1134 loc) · 36.9 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
From 95676cf0684b3fa23af168b2f3bec494fbaf3056 Mon Sep 17 00:00:00 2001
From: Piotr Gorski <lucjan.lucjanov@gmail.com>
Date: Mon, 2 Feb 2026 15:01:16 +0100
Subject: [PATCH] bore
Signed-off-by: Piotr Gorski <lucjan.lucjanov@gmail.com>
---
include/linux/sched.h | 34 +++
include/linux/sched/bore.h | 39 ++++
init/Kconfig | 17 ++
kernel/Kconfig.hz | 17 ++
kernel/exit.c | 2 +-
kernel/fork.c | 11 +-
kernel/futex/waitwake.c | 11 +
kernel/sched/Makefile | 1 +
kernel/sched/bore.c | 415 +++++++++++++++++++++++++++++++++++++
kernel/sched/core.c | 12 ++
kernel/sched/debug.c | 61 ++++++
kernel/sched/fair.c | 124 +++++++++--
kernel/sched/features.h | 3 +
kernel/sched/sched.h | 9 +
14 files changed, 742 insertions(+), 14 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 b469878de..ca23845c4 100644
--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -816,6 +816,37 @@ struct kmap_ctrl {
#endif
};
+#ifdef CONFIG_SCHED_BORE
+#define BORE_BC_TIMESTAMP_SHIFT 16
+
+struct bore_bc {
+ union {
+ struct {
+ u64 timestamp: 48;
+ u64 penalty: 16;
+ };
+ u64 value;
+ };
+};
+
+struct bore_ctx {
+ u64 burst_time;
+ u16 prev_penalty;
+ u16 curr_penalty;
+ union {
+ u16 penalty;
+ struct {
+ u8 _;
+ u8 score;
+ };
+ };
+ bool stop_update;
+ bool futex_waiting;
+ struct bore_bc subtree;
+ struct bore_bc group;
+} ____cacheline_aligned;
+#endif /* CONFIG_SCHED_BORE */
+
struct task_struct {
#ifdef CONFIG_THREAD_INFO_IN_TASK
/*
@@ -874,6 +905,9 @@ struct task_struct {
#ifdef CONFIG_SCHED_CLASS_EXT
struct sched_ext_entity scx;
#endif
+#ifdef CONFIG_SCHED_BORE
+ struct bore_ctx bore;
+#endif /* CONFIG_SCHED_BORE */
const struct sched_class *sched_class;
#ifdef CONFIG_SCHED_CORE
diff --git a/include/linux/sched/bore.h b/include/linux/sched/bore.h
new file mode 100644
index 000000000..85d14089c
--- /dev/null
+++ b/include/linux/sched/bore.h
@@ -0,0 +1,39 @@
+#ifndef _KERNEL_SCHED_BORE_H
+#define _KERNEL_SCHED_BORE_H
+
+#include <linux/sched.h>
+#include <linux/sched/cputime.h>
+#include <linux/atomic.h>
+#include <linux/list.h>
+#include <linux/rcupdate.h>
+
+#define SCHED_BORE_AUTHOR "Masahito Suzuki"
+#define SCHED_BORE_PROGNAME "BORE CPU Scheduler modification"
+
+#define SCHED_BORE_VERSION "6.6.1"
+
+extern u8 __read_mostly sched_bore;
+extern u8 __read_mostly sched_burst_inherit_type;
+extern u8 __read_mostly sched_burst_smoothness;
+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 u8 effective_prio_bore(struct task_struct *p);
+extern void update_curr_bore(struct task_struct *p, u64 delta_exec);
+extern void restart_burst_bore(struct task_struct *p);
+extern void restart_burst_rescale_deadline_bore(struct task_struct *p);
+extern void task_fork_bore(struct task_struct *p, struct task_struct *parent,
+ u64 clone_flags, u64 now);
+extern void sched_init_bore(void);
+extern void reset_task_bore(struct task_struct *p);
+
+extern int sched_bore_update_handler(const struct ctl_table *table,
+ int write, void __user *buffer, size_t *lenp, loff_t *ppos);
+extern int sched_burst_inherit_type_update_handler(const struct ctl_table *table,
+ int write, void __user *buffer, size_t *lenp, loff_t *ppos);
+
+extern void reweight_entity(
+ struct cfs_rq *cfs_rq, struct sched_entity *se, unsigned long weight);
+
+#endif /* _KERNEL_SCHED_BORE_H */
diff --git a/init/Kconfig b/init/Kconfig
index cab3ad28c..d821a5fbe 100644
--- a/init/Kconfig
+++ b/init/Kconfig
@@ -1423,6 +1423,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 ce1435cb0..9eee2005e 100644
--- a/kernel/Kconfig.hz
+++ b/kernel/Kconfig.hz
@@ -57,3 +57,20 @@ config HZ
config SCHED_HRTICK
def_bool HIGH_RES_TIMERS
+
+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.
diff --git a/kernel/exit.c b/kernel/exit.c
index 9f74e8f1c..ca564251f 100644
--- a/kernel/exit.c
+++ b/kernel/exit.c
@@ -147,7 +147,7 @@ static void __unhash_process(struct release_task_post *post, struct task_struct
detach_pid(post->pids, p, PIDTYPE_SID);
list_del_rcu(&p->tasks);
- list_del_init(&p->sibling);
+ list_del_rcu(&p->sibling);
__this_cpu_dec(process_counts);
}
list_del_rcu(&p->thread_node);
diff --git a/kernel/fork.c b/kernel/fork.c
index 3da0f0861..ca3da8984 100644
--- a/kernel/fork.c
+++ b/kernel/fork.c
@@ -116,6 +116,10 @@
/* For dup_mmap(). */
#include "../mm/internal.h"
+#ifdef CONFIG_SCHED_BORE
+#include <linux/sched/bore.h>
+#endif /* CONFIG_SCHED_BORE */
+
#include <trace/events/sched.h>
#define CREATE_TRACE_POINTS
@@ -2320,6 +2324,11 @@ __latent_entropy struct task_struct *copy_process(
p->start_time = ktime_get_ns();
p->start_boottime = ktime_get_boottime_ns();
+#ifdef CONFIG_SCHED_BORE
+ if (likely(p->pid))
+ task_fork_bore(p, current, clone_flags, p->start_time);
+#endif /* CONFIG_SCHED_BORE */
+
/*
* Make it visible to the rest of the system, but dont wake it up yet.
* Need tasklist lock for parent etc handling!
@@ -2393,7 +2402,7 @@ __latent_entropy struct task_struct *copy_process(
*/
p->signal->has_child_subreaper = p->real_parent->signal->has_child_subreaper ||
p->real_parent->signal->is_child_subreaper;
- list_add_tail(&p->sibling, &p->real_parent->children);
+ list_add_tail_rcu(&p->sibling, &p->real_parent->children);
list_add_tail_rcu(&p->tasks, &init_task.tasks);
attach_pid(p, PIDTYPE_TGID);
attach_pid(p, PIDTYPE_PGID);
diff --git a/kernel/futex/waitwake.c b/kernel/futex/waitwake.c
index e2bbe5509..6484ad583 100644
--- a/kernel/futex/waitwake.c
+++ b/kernel/futex/waitwake.c
@@ -4,6 +4,9 @@
#include <linux/sched/task.h>
#include <linux/sched/signal.h>
#include <linux/freezer.h>
+#ifdef CONFIG_SCHED_BORE
+#include <linux/sched/bore.h>
+#endif /* CONFIG_SCHED_BORE */
#include "futex.h"
@@ -355,7 +358,15 @@ void futex_do_wait(struct futex_q *q, struct hrtimer_sleeper *timeout)
* is no timeout, or if it has yet to expire.
*/
if (!timeout || timeout->task)
+#ifdef CONFIG_SCHED_BORE
+ {
+ current->bore.futex_waiting = true;
+#endif /* CONFIG_SCHED_BORE */
schedule();
+#ifdef CONFIG_SCHED_BORE
+ current->bore.futex_waiting = false;
+ }
+#endif /* CONFIG_SCHED_BORE */
}
__set_current_state(TASK_RUNNING);
}
diff --git a/kernel/sched/Makefile b/kernel/sched/Makefile
index 8ae86371d..b688084bc 100644
--- a/kernel/sched/Makefile
+++ b/kernel/sched/Makefile
@@ -37,3 +37,4 @@ obj-y += core.o
obj-y += fair.o
obj-y += build_policy.o
obj-y += build_utility.o
+obj-$(CONFIG_SCHED_BORE) += bore.o
diff --git a/kernel/sched/bore.c b/kernel/sched/bore.c
new file mode 100644
index 000000000..ac685f116
--- /dev/null
+++ b/kernel/sched/bore.c
@@ -0,0 +1,415 @@
+/*
+ * Burst-Oriented Response Enhancer (BORE) CPU Scheduler
+ * Copyright (C) 2021-2025 Masahito Suzuki <firelzrd@gmail.com>
+ */
+#include <linux/cpuset.h>
+#include <linux/sched/task.h>
+#include <linux/sched/bore.h>
+#include "sched.h"
+
+#ifdef CONFIG_SCHED_BORE
+u8 __read_mostly sched_bore = 1;
+u8 __read_mostly sched_burst_inherit_type = 2;
+u8 __read_mostly sched_burst_smoothness = 1;
+u8 __read_mostly sched_burst_penalty_offset = 24;
+uint __read_mostly sched_burst_penalty_scale = 1536;
+uint __read_mostly sched_burst_cache_lifetime = 75000000;
+static int __maybe_unused maxval_prio = 39;
+static int __maybe_unused maxval_6_bits = 63;
+static int __maybe_unused maxval_8_bits = 255;
+static int __maybe_unused maxval_12_bits = 4095;
+
+#define MAX_BURST_PENALTY ((40U << 8) - 1)
+#define BURST_CACHE_SAMPLE_LIMIT 63
+#define BURST_CACHE_SCAN_LIMIT (BURST_CACHE_SAMPLE_LIMIT * 2)
+
+static u32 bore_reciprocal_lut[BURST_CACHE_SAMPLE_LIMIT + 1];
+
+static u32 (*inherit_penalty_fn)(struct task_struct *, u64, u64);
+
+static inline u32 log2p1_u64_u32fp(u64 v, u8 fp) {
+ if (unlikely(!v)) return 0;
+ int clz = __builtin_clzll(v);
+ int exponent = 64 - clz;
+ u32 mantissa = (u32)((v << clz) << 1 >> (64 - fp));
+ return exponent << fp | mantissa;
+}
+
+static inline u32 calc_burst_penalty(u64 burst_time) {
+ u32 greed = log2p1_u64_u32fp(burst_time, 8),
+ tolerance = sched_burst_penalty_offset << 8;
+ s32 diff = (s32)(greed - tolerance);
+ u32 penalty = diff & ~(diff >> 31);
+ u32 scaled_penalty = penalty * sched_burst_penalty_scale >> 10;
+ s32 overflow = scaled_penalty - MAX_BURST_PENALTY;
+ return scaled_penalty - (overflow & ~(overflow >> 31));
+}
+
+static inline u64 rescale_slice(u64 delta, u8 old_prio, u8 new_prio) {
+ u64 unscaled, rescaled;
+ unscaled = mul_u64_u32_shr(delta , sched_prio_to_weight[old_prio], 10);
+ rescaled = mul_u64_u32_shr(unscaled, sched_prio_to_wmult [new_prio], 22);
+ return rescaled;
+}
+
+static inline u32 binary_smooth(u32 new, u32 old) {
+ u32 is_growing = (new > old);
+ u32 increment = (new - old) * is_growing;
+ u32 shift = sched_burst_smoothness;
+ u32 smoothed = old + ((increment + (1U << shift) - 1) >> shift);
+ return (new & ~(-is_growing)) | (smoothed & (-is_growing));
+}
+
+static void reweight_task_by_prio(struct task_struct *p, int prio) {
+ if (task_has_idle_policy(p)) return;
+
+ struct sched_entity *se = &p->se;
+ unsigned long weight = scale_load(sched_prio_to_weight[prio]);
+
+ if (se->on_rq) {
+ p->bore.stop_update = true;
+ reweight_entity(cfs_rq_of(se), se, weight);
+ p->bore.stop_update = false;
+ } else
+ se->load.weight = weight;
+ se->load.inv_weight = sched_prio_to_wmult[prio];
+}
+
+u8 effective_prio_bore(struct task_struct *p) {
+ int prio = p->static_prio - MAX_RT_PRIO;
+ prio += p->bore.score & -(s32)sched_bore;
+ prio &= ~(prio >> 31);
+ s32 diff = prio - maxval_prio;
+ prio -= (diff & ~(diff >> 31));
+ return (u8)prio;
+}
+
+static void update_penalty(struct task_struct *p) {
+ struct bore_ctx *ctx = &p->bore;
+
+ u8 prev_prio = effective_prio_bore(p);
+
+ s32 diff = (s32)ctx->curr_penalty - (s32)ctx->prev_penalty;
+ u16 max_val = ctx->curr_penalty - (diff & (diff >> 31));
+ u32 is_kthread = !!(p->flags & PF_KTHREAD);
+ ctx->penalty = max_val & -(s32)(!is_kthread);
+
+ u8 new_prio = effective_prio_bore(p);
+ if (new_prio != prev_prio)
+ reweight_task_by_prio(p, new_prio);
+}
+
+void update_curr_bore(struct task_struct *p, u64 delta_exec) {
+ struct bore_ctx *ctx = &p->bore;
+ if (ctx->stop_update) return;
+
+ ctx->burst_time += delta_exec;
+ u32 curr_penalty = ctx->curr_penalty = calc_burst_penalty(ctx->burst_time);
+
+ if (curr_penalty <= ctx->prev_penalty) return;
+ update_penalty(p);
+}
+
+void restart_burst_bore(struct task_struct *p) {
+ struct bore_ctx *ctx = &p->bore;
+ u32 new_penalty = binary_smooth(ctx->curr_penalty, ctx->prev_penalty);
+ ctx->prev_penalty = new_penalty;
+ ctx->curr_penalty = 0;
+ ctx->burst_time = 0;
+ update_penalty(p);
+}
+
+void restart_burst_rescale_deadline_bore(struct task_struct *p) {
+ struct sched_entity *se = &p->se;
+ s64 vscaled, vremain = se->deadline - se->vruntime;
+
+ u8 old_prio = effective_prio_bore(p);
+ restart_burst_bore(p);
+ u8 new_prio = effective_prio_bore(p);
+
+ if (old_prio > new_prio) {
+ vscaled = rescale_slice(abs(vremain), old_prio, new_prio);
+ if (unlikely(vremain < 0))
+ vscaled = -vscaled;
+ se->deadline = se->vruntime + vscaled;
+ }
+}
+
+static inline bool task_is_bore_eligible(struct task_struct *p)
+{return p && p->sched_class == &fair_sched_class && !p->exit_state;}
+
+#ifndef for_each_child_task
+#define for_each_child_task(p, t) \
+ list_for_each_entry_rcu(t, &(p)->children, sibling)
+#endif
+
+static inline u32 count_children_upto2(struct task_struct *p) {
+ struct list_head *head = &p->children;
+ struct list_head *next = head->next;
+ return (next != head) + (next->next != head);
+}
+
+static inline bool burst_cache_expired(struct bore_bc *bc, u64 now) {
+ struct bore_bc bc_val = { .value = READ_ONCE(bc->value) };
+ u64 timestamp = (u64)bc_val.timestamp << BORE_BC_TIMESTAMP_SHIFT;
+ return now - timestamp > (u64)sched_burst_cache_lifetime;
+}
+
+static void update_burst_cache(struct bore_bc *bc,
+ struct task_struct *p, u32 count, u32 total, u64 now) {
+ u32 average = (count == 1) ? total :
+ (u32)(((u64)total * bore_reciprocal_lut[count]) >> 32);
+
+ struct bore_bc new_bc = {
+ .penalty = max(average, p->bore.penalty),
+ .timestamp = now >> BORE_BC_TIMESTAMP_SHIFT
+ };
+ WRITE_ONCE(bc->value, new_bc.value);
+}
+
+static u32 inherit_none(struct task_struct *parent,
+ u64 clone_flags, u64 now)
+{ return 0; }
+
+static u32 inherit_from_parent(struct task_struct *parent,
+ u64 clone_flags, u64 now) {
+ struct bore_bc bc_val;
+
+ if (clone_flags & CLONE_PARENT)
+ parent = rcu_dereference(parent->real_parent);
+
+ struct bore_bc *bc = &parent->bore.subtree;
+
+ if (burst_cache_expired(bc, now)) {
+ struct task_struct *child;
+ u32 count = 0, total = 0, scan_count = 0;
+ for_each_child_task(parent, child) {
+ if (count >= BURST_CACHE_SAMPLE_LIMIT) break;
+ if (scan_count++ >= BURST_CACHE_SCAN_LIMIT) break;
+
+ if (!task_is_bore_eligible(child)) continue;
+ count++;
+ total += child->bore.penalty;
+ }
+
+ update_burst_cache(bc, parent, count, total, now);
+ }
+
+ bc_val.value = READ_ONCE(bc->value);
+ return (u32)bc_val.penalty;
+}
+
+static u32 inherit_from_ancestor_hub(struct task_struct *parent,
+ u64 clone_flags, u64 now) {
+ struct bore_bc bc_val;
+ struct task_struct *ancestor = parent;
+ u32 sole_child_count = 0;
+
+ if (clone_flags & CLONE_PARENT) {
+ ancestor = rcu_dereference(ancestor->real_parent);
+ sole_child_count = 1;
+ }
+
+ for (struct task_struct *next;
+ (next = rcu_dereference(ancestor->real_parent)) != ancestor &&
+ count_children_upto2(ancestor) <= sole_child_count;
+ ancestor = next, sole_child_count = 1) {}
+
+ struct bore_bc *bc = &ancestor->bore.subtree;
+
+ if (burst_cache_expired(bc, now)) {
+ struct task_struct *direct_child;
+ u32 count = 0, total = 0, scan_count = 0;
+ for_each_child_task(ancestor, direct_child) {
+ if (count >= BURST_CACHE_SAMPLE_LIMIT) break;
+ if (scan_count++ >= BURST_CACHE_SCAN_LIMIT) break;
+
+ struct task_struct *descendant = direct_child;
+ while (count_children_upto2(descendant) == 1)
+ descendant = list_first_entry(&descendant->children,
+ struct task_struct, sibling);
+
+ if (!task_is_bore_eligible(descendant)) continue;
+ count++;
+ total += descendant->bore.penalty;
+ }
+
+ update_burst_cache(bc, ancestor, count, total, now);
+ }
+
+ bc_val.value = READ_ONCE(bc->value);
+ return (u32)bc_val.penalty;
+}
+
+static u32 inherit_from_thread_group(struct task_struct *p, u64 now) {
+ struct bore_bc bc_val;
+ struct task_struct *leader = p->group_leader;
+ struct bore_bc *bc = &leader->bore.group;
+
+ if (burst_cache_expired(bc, now)) {
+ struct task_struct *sibling;
+ u32 count = 0, total = 0;
+
+ for_each_thread(leader, sibling) {
+ if (count >= BURST_CACHE_SAMPLE_LIMIT) break;
+
+ if (!task_is_bore_eligible(sibling)) continue;
+ count++;
+ total += sibling->bore.penalty;
+ }
+
+ update_burst_cache(bc, leader, count, total, now);
+ }
+
+ bc_val.value = READ_ONCE(bc->value);
+ return (u32)bc_val.penalty;
+}
+
+void task_fork_bore(struct task_struct *p,
+ struct task_struct *parent, u64 clone_flags, u64 now) {
+ if (!task_is_bore_eligible(p) || unlikely(!sched_bore)) return;
+
+ rcu_read_lock();
+ struct bore_ctx *ctx = &p->bore;
+ u32 inherited_penalty = (clone_flags & CLONE_THREAD)?
+ inherit_from_thread_group(parent, now):
+ inherit_penalty_fn(parent, clone_flags, now);
+
+ if (ctx->prev_penalty < inherited_penalty)
+ ctx->prev_penalty = inherited_penalty;
+ ctx->curr_penalty = 0;
+ ctx->burst_time = 0;
+ ctx->stop_update = false;
+ ctx->futex_waiting = false;
+ update_penalty(p);
+ rcu_read_unlock();
+}
+
+void reset_task_bore(struct task_struct *p)
+{ memset(&p->bore, 0, sizeof(struct bore_ctx)); }
+
+static void update_inherit_type(void) {
+ switch(sched_burst_inherit_type) {
+ case 1:
+ inherit_penalty_fn = inherit_from_parent;
+ break;
+ case 2:
+ inherit_penalty_fn = inherit_from_ancestor_hub;
+ break;
+ default:
+ inherit_penalty_fn = inherit_none;
+ }
+}
+
+void __init sched_init_bore(void) {
+ printk(KERN_INFO "%s %s by %s\n",
+ SCHED_BORE_PROGNAME, SCHED_BORE_VERSION, SCHED_BORE_AUTHOR);
+
+ for (int i = 1; i <= BURST_CACHE_SAMPLE_LIMIT; i++)
+ bore_reciprocal_lut[i] = (u32)((0xffffffffULL + i) / i);
+
+ reset_task_bore(&init_task);
+ update_inherit_type();
+}
+
+static void readjust_all_task_weights(void) {
+ struct task_struct *task;
+ struct rq *rq;
+ struct rq_flags rf;
+
+ scoped_guard(write_lock_irq, &tasklist_lock)
+ for_each_process(task) {
+ if (!task_is_bore_eligible(task)) continue;
+ rq = task_rq_lock(task, &rf);
+ update_rq_clock(rq);
+ reweight_task_by_prio(task, effective_prio_bore(task));
+ task_rq_unlock(rq, task, &rf);
+ }
+}
+
+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;
+
+ readjust_all_task_weights();
+
+ return 0;
+}
+
+int sched_burst_inherit_type_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;
+
+ update_inherit_type();
+
+ return 0;
+}
+
+#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_inherit_type",
+ .data = &sched_burst_inherit_type,
+ .maxlen = sizeof(u8),
+ .mode = 0644,
+ .proc_handler = sched_burst_inherit_type_update_handler,
+ .extra1 = SYSCTL_ZERO,
+ .extra2 = SYSCTL_TWO,
+ },
+ {
+ .procname = "sched_burst_smoothness",
+ .data = &sched_burst_smoothness,
+ .maxlen = sizeof(u8),
+ .mode = 0644,
+ .proc_handler = proc_dou8vec_minmax,
+ .extra1 = SYSCTL_ZERO,
+ .extra2 = SYSCTL_THREE,
+ },
+ {
+ .procname = "sched_burst_penalty_offset",
+ .data = &sched_burst_penalty_offset,
+ .maxlen = sizeof(u8),
+ .mode = 0644,
+ .proc_handler = proc_dou8vec_minmax,
+ .extra1 = SYSCTL_ZERO,
+ .extra2 = &maxval_6_bits,
+ },
+ {
+ .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,
+ },
+};
+
+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 e460c22de..15bf4857b 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -100,6 +100,10 @@
#include "../smpboot.h"
#include "../locking/mutex.h"
+#ifdef CONFIG_SCHED_BORE
+#include <linux/sched/bore.h>
+#endif /* CONFIG_SCHED_BORE */
+
EXPORT_TRACEPOINT_SYMBOL_GPL(ipi_send_cpu);
EXPORT_TRACEPOINT_SYMBOL_GPL(ipi_send_cpumask);
@@ -1432,7 +1436,11 @@ int tg_nop(struct task_group *tg, void *data)
void set_load_weight(struct task_struct *p, bool update_load)
{
+#ifdef CONFIG_SCHED_BORE
+ int prio = effective_prio_bore(p);
+#else /* !CONFIG_SCHED_BORE */
int prio = p->static_prio - MAX_RT_PRIO;
+#endif /* CONFIG_SCHED_BORE */
struct load_weight lw;
if (task_has_idle_policy(p)) {
@@ -8658,6 +8666,10 @@ void __init sched_init(void)
BUG_ON(!sched_class_above(&ext_sched_class, &idle_sched_class));
#endif
+#ifdef CONFIG_SCHED_BORE
+ sched_init_bore();
+#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 41caa22e0..1c5ace627 100644
--- a/kernel/sched/debug.c
+++ b/kernel/sched/debug.c
@@ -169,6 +169,53 @@ static const struct file_operations sched_feat_fops = {
.release = single_release,
};
+#ifdef CONFIG_SCHED_BORE
+#define DEFINE_SYSCTL_SCHED_FUNC(name, update_func) \
+static ssize_t sched_##name##_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; \
+\
+ sysctl_sched_##name = value; \
+ sched_update_##update_func(); \
+\
+ *ppos += cnt; \
+ return cnt; \
+} \
+\
+static int sched_##name##_show(struct seq_file *m, void *v) \
+{ \
+ seq_printf(m, "%d\n", sysctl_sched_##name); \
+ return 0; \
+} \
+\
+static int sched_##name##_open(struct inode *inode, struct file *filp) \
+{ \
+ return single_open(filp, sched_##name##_show, NULL); \
+} \
+\
+static const struct file_operations sched_##name##_fops = { \
+ .open = sched_##name##_open, \
+ .write = sched_##name##_write, \
+ .read = seq_read, \
+ .llseek = seq_lseek, \
+ .release = single_release, \
+};
+
+DEFINE_SYSCTL_SCHED_FUNC(min_base_slice, min_base_slice)
+
+#undef DEFINE_SYSCTL_SCHED_FUNC
+#else /* !CONFIG_SCHED_BORE */
static ssize_t sched_scaling_write(struct file *filp, const char __user *ubuf,
size_t cnt, loff_t *ppos)
{
@@ -214,6 +261,7 @@ static const struct file_operations sched_scaling_fops = {
.llseek = seq_lseek,
.release = single_release,
};
+#endif /* CONFIG_SCHED_BORE */
#ifdef CONFIG_PREEMPT_DYNAMIC
@@ -500,12 +548,19 @@ 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", 0444, 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);
+#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);
@@ -747,6 +802,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->bore.score);
+#endif /* CONFIG_SCHED_BORE */
#ifdef CONFIG_NUMA_BALANCING
SEQ_printf(m, " %d %d", task_node(p), task_numa_group_id(p));
#endif
@@ -1224,6 +1282,9 @@ void proc_sched_show_task(struct task_struct *p, struct pid_namespace *ns,
__PS("nr_involuntary_switches", p->nivcsw);
P(se.load.weight);
+#ifdef CONFIG_SCHED_BORE
+ P(bore.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 f0c7c9442..9757964d4 100644
--- a/kernel/sched/fair.c
+++ b/kernel/sched/fair.c
@@ -58,6 +58,10 @@
#include "stats.h"
#include "autogroup.h"
+#ifdef CONFIG_SCHED_BORE
+#include <linux/sched/bore.h>
+#endif /* CONFIG_SCHED_BORE */
+
/*
* The initial- and re-scaling of tunables is configurable
*
@@ -67,17 +71,30 @@
* 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.70 msec * (1 + ilog(ncpus)), units: nanoseconds)
+ * BORE : base_slice = minimum multiple of nsecs_per_tick >= min_base_slice
+ * (default min_base_slice = 2000000 constant, units: nanoseconds)
+ * EEVDF: default 0.70 msec * (1 + ilog(ncpus)), units: nanoseconds
*/
+#ifdef CONFIG_SCHED_BORE
+static const unsigned int nsecs_per_tick = 1000000000ULL / HZ;
+unsigned int sysctl_sched_min_base_slice = CONFIG_MIN_BASE_SLICE_NS;
+__read_mostly uint sysctl_sched_base_slice = nsecs_per_tick;
+#else /* !CONFIG_SCHED_BORE */
unsigned int sysctl_sched_base_slice = 700000ULL;
static unsigned int normalized_sysctl_sched_base_slice = 700000ULL;
+#endif /* CONFIG_SCHED_BORE */
__read_mostly unsigned int sysctl_sched_migration_cost = 500000UL;
@@ -189,6 +206,13 @@ 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) {
+ sysctl_sched_base_slice = nsecs_per_tick *
+ max(1UL, DIV_ROUND_UP(sysctl_sched_min_base_slice, nsecs_per_tick));
+}
+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);
@@ -219,6 +243,7 @@ static void update_sysctl(void)
SET_SYSCTL(sched_base_slice);
#undef SET_SYSCTL
}
+#endif /* CONFIG_SCHED_BORE */
void __init sched_init_granularity(void)
{
@@ -698,6 +723,9 @@ static void update_entity_lag(struct cfs_rq *cfs_rq, struct sched_entity *se)
vlag = avg_vruntime(cfs_rq) - 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 */
se->vlag = clamp(vlag, -limit, limit);
}
@@ -865,10 +893,17 @@ struct sched_entity *__pick_first_entity(struct cfs_rq *cfs_rq)
*/
static inline void set_protect_slice(struct cfs_rq *cfs_rq, struct sched_entity *se)
{
+#ifdef CONFIG_SCHED_BORE
+ u64 slice = sysctl_sched_base_slice;
+ bool run_to_parity = likely(sched_bore) ?
+ sched_feat(RUN_TO_PARITY_BORE) : sched_feat(RUN_TO_PARITY);
+#else /* CONFIG_SCHED_BORE */
u64 slice = normalized_sysctl_sched_base_slice;
+ bool run_to_parity = sched_feat(RUN_TO_PARITY);
+#endif /* CONFIG_SCHED_BORE */
u64 vprot = se->deadline;
- if (sched_feat(RUN_TO_PARITY))
+ if (run_to_parity)
slice = cfs_rq_min_slice(cfs_rq);
slice = min(slice, se->slice);
@@ -933,6 +968,11 @@ static struct sched_entity *__pick_eevdf(struct cfs_rq *cfs_rq, bool protect)
curr = NULL;
if (curr && protect && protect_slice(curr))
+#ifdef CONFIG_SCHED_BORE
+ if (!entity_is_task(curr) ||
+ !task_of(curr)->bore.futex_waiting ||
+ unlikely(!sched_bore))
+#endif /* CONFIG_SCHED_BORE */
return curr;
/* Pick the leftmost entity if it's eligible */
@@ -994,6 +1034,7 @@ struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq)
/**************************************************************
* Scheduling class statistics methods:
*/
+#if !defined(CONFIG_SCHED_BORE)
int sched_update_scaling(void)
{
unsigned int factor = get_update_sysctl_factor();
@@ -1005,6 +1046,7 @@ int sched_update_scaling(void)
return 0;
}
+#endif /* CONFIG_SCHED_BORE */
static void clear_buddies(struct cfs_rq *cfs_rq, struct sched_entity *se);
@@ -1202,6 +1244,11 @@ static void update_curr(struct cfs_rq *cfs_rq)
resched = update_deadline(cfs_rq, curr);
if (entity_is_task(curr)) {
+#ifdef CONFIG_SCHED_BORE
+ struct task_struct *p = task_of(curr);
+ update_curr_bore(p, delta_exec);
+#endif /* CONFIG_SCHED_BORE */
+
/*
* If the fair_server is active, we need to account for the
* fair_server time whether or not the task is running on
@@ -3740,7 +3787,7 @@ dequeue_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se)
static void place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags);
-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;
@@ -5091,12 +5138,11 @@ void __setparam_fair(struct task_struct *p, const struct sched_attr *attr)
static void
place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
{
- u64 vslice, vruntime = avg_vruntime(cfs_rq);
+ u64 vslice = 0, vruntime = avg_vruntime(cfs_rq);
s64 lag = 0;
if (!se->custom_slice)
se->slice = sysctl_sched_base_slice;
- vslice = calc_delta_fair(se->slice, se);
/*
* Due to how V is constructed as the weighted average of entities,
@@ -5181,7 +5227,18 @@ place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
se->rel_deadline = 0;
return;
}
-
+#ifdef CONFIG_SCHED_BORE
+ if (entity_is_task(se) &&
+ likely(sched_bore) &&
+ task_of(se)->bore.futex_waiting)
+ goto vslice_found;