1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22#include <linux/kernel_stat.h>
23#include <linux/export.h>
24#include <linux/interrupt.h>
25#include <linux/percpu.h>
26#include <linux/init.h>
27#include <linux/mm.h>
28#include <linux/swap.h>
29#include <linux/pid_namespace.h>
30#include <linux/notifier.h>
31#include <linux/thread_info.h>
32#include <linux/time.h>
33#include <linux/jiffies.h>
34#include <linux/posix-timers.h>
35#include <linux/cpu.h>
36#include <linux/syscalls.h>
37#include <linux/delay.h>
38#include <linux/tick.h>
39#include <linux/kallsyms.h>
40#include <linux/irq_work.h>
41#include <linux/sched.h>
42#include <linux/sched/sysctl.h>
43#include <linux/slab.h>
44#include <linux/compat.h>
45
46#include <linux/uaccess.h>
47#include <asm/unistd.h>
48#include <asm/div64.h>
49#include <asm/timex.h>
50#include <asm/io.h>
51
52#include "tick-internal.h"
53
54#define CREATE_TRACE_POINTS
55#include <trace/events/timer.h>
56
57__visible u64 jiffies_64 __cacheline_aligned_in_smp = INITIAL_JIFFIES;
58
59EXPORT_SYMBOL(jiffies_64);
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#define LVL_CLK_SHIFT 3
151#define LVL_CLK_DIV (1UL << LVL_CLK_SHIFT)
152#define LVL_CLK_MASK (LVL_CLK_DIV - 1)
153#define LVL_SHIFT(n) ((n) * LVL_CLK_SHIFT)
154#define LVL_GRAN(n) (1UL << LVL_SHIFT(n))
155
156
157
158
159
160#define LVL_START(n) ((LVL_SIZE - 1) << (((n) - 1) * LVL_CLK_SHIFT))
161
162
163#define LVL_BITS 6
164#define LVL_SIZE (1UL << LVL_BITS)
165#define LVL_MASK (LVL_SIZE - 1)
166#define LVL_OFFS(n) ((n) * LVL_SIZE)
167
168
169#if HZ > 100
170# define LVL_DEPTH 9
171# else
172# define LVL_DEPTH 8
173#endif
174
175
176#define WHEEL_TIMEOUT_CUTOFF (LVL_START(LVL_DEPTH))
177#define WHEEL_TIMEOUT_MAX (WHEEL_TIMEOUT_CUTOFF - LVL_GRAN(LVL_DEPTH - 1))
178
179
180
181
182
183#define WHEEL_SIZE (LVL_SIZE * LVL_DEPTH)
184
185#ifdef CONFIG_NO_HZ_COMMON
186# define NR_BASES 2
187# define BASE_STD 0
188# define BASE_DEF 1
189#else
190# define NR_BASES 1
191# define BASE_STD 0
192# define BASE_DEF 0
193#endif
194
195struct timer_base {
196 spinlock_t lock;
197 struct timer_list *running_timer;
198 unsigned long clk;
199 unsigned long next_expiry;
200 unsigned int cpu;
201 bool migration_enabled;
202 bool nohz_active;
203 bool is_idle;
204 DECLARE_BITMAP(pending_map, WHEEL_SIZE);
205 struct hlist_head vectors[WHEEL_SIZE];
206} ____cacheline_aligned;
207
208static DEFINE_PER_CPU(struct timer_base, timer_bases[NR_BASES]);
209
210#if defined(CONFIG_SMP) && defined(CONFIG_NO_HZ_COMMON)
211unsigned int sysctl_timer_migration = 1;
212
213void timers_update_migration(bool update_nohz)
214{
215 bool on = sysctl_timer_migration && tick_nohz_active;
216 unsigned int cpu;
217
218
219 if (this_cpu_read(timer_bases[BASE_STD].migration_enabled) == on)
220 return;
221
222 for_each_possible_cpu(cpu) {
223 per_cpu(timer_bases[BASE_STD].migration_enabled, cpu) = on;
224 per_cpu(timer_bases[BASE_DEF].migration_enabled, cpu) = on;
225 per_cpu(hrtimer_bases.migration_enabled, cpu) = on;
226 if (!update_nohz)
227 continue;
228 per_cpu(timer_bases[BASE_STD].nohz_active, cpu) = true;
229 per_cpu(timer_bases[BASE_DEF].nohz_active, cpu) = true;
230 per_cpu(hrtimer_bases.nohz_active, cpu) = true;
231 }
232}
233
234int timer_migration_handler(struct ctl_table *table, int write,
235 void __user *buffer, size_t *lenp,
236 loff_t *ppos)
237{
238 static DEFINE_MUTEX(mutex);
239 int ret;
240
241 mutex_lock(&mutex);
242 ret = proc_dointvec(table, write, buffer, lenp, ppos);
243 if (!ret && write)
244 timers_update_migration(false);
245 mutex_unlock(&mutex);
246 return ret;
247}
248#endif
249
250static unsigned long round_jiffies_common(unsigned long j, int cpu,
251 bool force_up)
252{
253 int rem;
254 unsigned long original = j;
255
256
257
258
259
260
261
262
263
264 j += cpu * 3;
265
266 rem = j % HZ;
267
268
269
270
271
272
273
274
275 if (rem < HZ/4 && !force_up)
276 j = j - rem;
277 else
278 j = j - rem + HZ;
279
280
281 j -= cpu * 3;
282
283
284
285
286
287 return time_is_after_jiffies(j) ? j : original;
288}
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310unsigned long __round_jiffies(unsigned long j, int cpu)
311{
312 return round_jiffies_common(j, cpu, false);
313}
314EXPORT_SYMBOL_GPL(__round_jiffies);
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336unsigned long __round_jiffies_relative(unsigned long j, int cpu)
337{
338 unsigned long j0 = jiffies;
339
340
341 return round_jiffies_common(j + j0, cpu, false) - j0;
342}
343EXPORT_SYMBOL_GPL(__round_jiffies_relative);
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360unsigned long round_jiffies(unsigned long j)
361{
362 return round_jiffies_common(j, raw_smp_processor_id(), false);
363}
364EXPORT_SYMBOL_GPL(round_jiffies);
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381unsigned long round_jiffies_relative(unsigned long j)
382{
383 return __round_jiffies_relative(j, raw_smp_processor_id());
384}
385EXPORT_SYMBOL_GPL(round_jiffies_relative);
386
387
388
389
390
391
392
393
394
395
396
397unsigned long __round_jiffies_up(unsigned long j, int cpu)
398{
399 return round_jiffies_common(j, cpu, true);
400}
401EXPORT_SYMBOL_GPL(__round_jiffies_up);
402
403
404
405
406
407
408
409
410
411
412
413unsigned long __round_jiffies_up_relative(unsigned long j, int cpu)
414{
415 unsigned long j0 = jiffies;
416
417
418 return round_jiffies_common(j + j0, cpu, true) - j0;
419}
420EXPORT_SYMBOL_GPL(__round_jiffies_up_relative);
421
422
423
424
425
426
427
428
429
430
431unsigned long round_jiffies_up(unsigned long j)
432{
433 return round_jiffies_common(j, raw_smp_processor_id(), true);
434}
435EXPORT_SYMBOL_GPL(round_jiffies_up);
436
437
438
439
440
441
442
443
444
445
446unsigned long round_jiffies_up_relative(unsigned long j)
447{
448 return __round_jiffies_up_relative(j, raw_smp_processor_id());
449}
450EXPORT_SYMBOL_GPL(round_jiffies_up_relative);
451
452
453static inline unsigned int timer_get_idx(struct timer_list *timer)
454{
455 return (timer->flags & TIMER_ARRAYMASK) >> TIMER_ARRAYSHIFT;
456}
457
458static inline void timer_set_idx(struct timer_list *timer, unsigned int idx)
459{
460 timer->flags = (timer->flags & ~TIMER_ARRAYMASK) |
461 idx << TIMER_ARRAYSHIFT;
462}
463
464
465
466
467
468static inline unsigned calc_index(unsigned expires, unsigned lvl)
469{
470 expires = (expires + LVL_GRAN(lvl)) >> LVL_SHIFT(lvl);
471 return LVL_OFFS(lvl) + (expires & LVL_MASK);
472}
473
474static int calc_wheel_index(unsigned long expires, unsigned long clk)
475{
476 unsigned long delta = expires - clk;
477 unsigned int idx;
478
479 if (delta < LVL_START(1)) {
480 idx = calc_index(expires, 0);
481 } else if (delta < LVL_START(2)) {
482 idx = calc_index(expires, 1);
483 } else if (delta < LVL_START(3)) {
484 idx = calc_index(expires, 2);
485 } else if (delta < LVL_START(4)) {
486 idx = calc_index(expires, 3);
487 } else if (delta < LVL_START(5)) {
488 idx = calc_index(expires, 4);
489 } else if (delta < LVL_START(6)) {
490 idx = calc_index(expires, 5);
491 } else if (delta < LVL_START(7)) {
492 idx = calc_index(expires, 6);
493 } else if (LVL_DEPTH > 8 && delta < LVL_START(8)) {
494 idx = calc_index(expires, 7);
495 } else if ((long) delta < 0) {
496 idx = clk & LVL_MASK;
497 } else {
498
499
500
501
502 if (expires >= WHEEL_TIMEOUT_CUTOFF)
503 expires = WHEEL_TIMEOUT_MAX;
504
505 idx = calc_index(expires, LVL_DEPTH - 1);
506 }
507 return idx;
508}
509
510
511
512
513
514static void enqueue_timer(struct timer_base *base, struct timer_list *timer,
515 unsigned int idx)
516{
517 hlist_add_head(&timer->entry, base->vectors + idx);
518 __set_bit(idx, base->pending_map);
519 timer_set_idx(timer, idx);
520}
521
522static void
523__internal_add_timer(struct timer_base *base, struct timer_list *timer)
524{
525 unsigned int idx;
526
527 idx = calc_wheel_index(timer->expires, base->clk);
528 enqueue_timer(base, timer, idx);
529}
530
531static void
532trigger_dyntick_cpu(struct timer_base *base, struct timer_list *timer)
533{
534 if (!IS_ENABLED(CONFIG_NO_HZ_COMMON) || !base->nohz_active)
535 return;
536
537
538
539
540
541 if (timer->flags & TIMER_DEFERRABLE) {
542 if (tick_nohz_full_cpu(base->cpu))
543 wake_up_nohz_cpu(base->cpu);
544 return;
545 }
546
547
548
549
550
551
552 if (!base->is_idle)
553 return;
554
555
556 if (time_after_eq(timer->expires, base->next_expiry))
557 return;
558
559
560
561
562
563 base->next_expiry = timer->expires;
564 wake_up_nohz_cpu(base->cpu);
565}
566
567static void
568internal_add_timer(struct timer_base *base, struct timer_list *timer)
569{
570 __internal_add_timer(base, timer);
571 trigger_dyntick_cpu(base, timer);
572}
573
574#ifdef CONFIG_TIMER_STATS
575void __timer_stats_timer_set_start_info(struct timer_list *timer, void *addr)
576{
577 if (timer->start_site)
578 return;
579
580 timer->start_site = addr;
581 memcpy(timer->start_comm, current->comm, TASK_COMM_LEN);
582 timer->start_pid = current->pid;
583}
584
585static void timer_stats_account_timer(struct timer_list *timer)
586{
587 void *site;
588
589
590
591
592
593 site = READ_ONCE(timer->start_site);
594 if (likely(!site))
595 return;
596
597 timer_stats_update_stats(timer, timer->start_pid, site,
598 timer->function, timer->start_comm,
599 timer->flags);
600}
601
602#else
603static void timer_stats_account_timer(struct timer_list *timer) {}
604#endif
605
606#ifdef CONFIG_DEBUG_OBJECTS_TIMERS
607
608static struct debug_obj_descr timer_debug_descr;
609
610static void *timer_debug_hint(void *addr)
611{
612 return ((struct timer_list *) addr)->function;
613}
614
615static bool timer_is_static_object(void *addr)
616{
617 struct timer_list *timer = addr;
618
619 return (timer->entry.pprev == NULL &&
620 timer->entry.next == TIMER_ENTRY_STATIC);
621}
622
623
624
625
626
627static bool timer_fixup_init(void *addr, enum debug_obj_state state)
628{
629 struct timer_list *timer = addr;
630
631 switch (state) {
632 case ODEBUG_STATE_ACTIVE:
633 del_timer_sync(timer);
634 debug_object_init(timer, &timer_debug_descr);
635 return true;
636 default:
637 return false;
638 }
639}
640
641
642static void stub_timer(unsigned long data)
643{
644 WARN_ON(1);
645}
646
647
648
649
650
651
652static bool timer_fixup_activate(void *addr, enum debug_obj_state state)
653{
654 struct timer_list *timer = addr;
655
656 switch (state) {
657 case ODEBUG_STATE_NOTAVAILABLE:
658 setup_timer(timer, stub_timer, 0);
659 return true;
660
661 case ODEBUG_STATE_ACTIVE:
662 WARN_ON(1);
663
664 default:
665 return false;
666 }
667}
668
669
670
671
672
673static bool timer_fixup_free(void *addr, enum debug_obj_state state)
674{
675 struct timer_list *timer = addr;
676
677 switch (state) {
678 case ODEBUG_STATE_ACTIVE:
679 del_timer_sync(timer);
680 debug_object_free(timer, &timer_debug_descr);
681 return true;
682 default:
683 return false;
684 }
685}
686
687
688
689
690
691static bool timer_fixup_assert_init(void *addr, enum debug_obj_state state)
692{
693 struct timer_list *timer = addr;
694
695 switch (state) {
696 case ODEBUG_STATE_NOTAVAILABLE:
697 setup_timer(timer, stub_timer, 0);
698 return true;
699 default:
700 return false;
701 }
702}
703
704static struct debug_obj_descr timer_debug_descr = {
705 .name = "timer_list",
706 .debug_hint = timer_debug_hint,
707 .is_static_object = timer_is_static_object,
708 .fixup_init = timer_fixup_init,
709 .fixup_activate = timer_fixup_activate,
710 .fixup_free = timer_fixup_free,
711 .fixup_assert_init = timer_fixup_assert_init,
712};
713
714static inline void debug_timer_init(struct timer_list *timer)
715{
716 debug_object_init(timer, &timer_debug_descr);
717}
718
719static inline void debug_timer_activate(struct timer_list *timer)
720{
721 debug_object_activate(timer, &timer_debug_descr);
722}
723
724static inline void debug_timer_deactivate(struct timer_list *timer)
725{
726 debug_object_deactivate(timer, &timer_debug_descr);
727}
728
729static inline void debug_timer_free(struct timer_list *timer)
730{
731 debug_object_free(timer, &timer_debug_descr);
732}
733
734static inline void debug_timer_assert_init(struct timer_list *timer)
735{
736 debug_object_assert_init(timer, &timer_debug_descr);
737}
738
739static void do_init_timer(struct timer_list *timer, unsigned int flags,
740 const char *name, struct lock_class_key *key);
741
742void init_timer_on_stack_key(struct timer_list *timer, unsigned int flags,
743 const char *name, struct lock_class_key *key)
744{
745 debug_object_init_on_stack(timer, &timer_debug_descr);
746 do_init_timer(timer, flags, name, key);
747}
748EXPORT_SYMBOL_GPL(init_timer_on_stack_key);
749
750void destroy_timer_on_stack(struct timer_list *timer)
751{
752 debug_object_free(timer, &timer_debug_descr);
753}
754EXPORT_SYMBOL_GPL(destroy_timer_on_stack);
755
756#else
757static inline void debug_timer_init(struct timer_list *timer) { }
758static inline void debug_timer_activate(struct timer_list *timer) { }
759static inline void debug_timer_deactivate(struct timer_list *timer) { }
760static inline void debug_timer_assert_init(struct timer_list *timer) { }
761#endif
762
763static inline void debug_init(struct timer_list *timer)
764{
765 debug_timer_init(timer);
766 trace_timer_init(timer);
767}
768
769static inline void
770debug_activate(struct timer_list *timer, unsigned long expires)
771{
772 debug_timer_activate(timer);
773 trace_timer_start(timer, expires, timer->flags);
774}
775
776static inline void debug_deactivate(struct timer_list *timer)
777{
778 debug_timer_deactivate(timer);
779 trace_timer_cancel(timer);
780}
781
782static inline void debug_assert_init(struct timer_list *timer)
783{
784 debug_timer_assert_init(timer);
785}
786
787static void do_init_timer(struct timer_list *timer, unsigned int flags,
788 const char *name, struct lock_class_key *key)
789{
790 timer->entry.pprev = NULL;
791 timer->flags = flags | raw_smp_processor_id();
792#ifdef CONFIG_TIMER_STATS
793 timer->start_site = NULL;
794 timer->start_pid = -1;
795 memset(timer->start_comm, 0, TASK_COMM_LEN);
796#endif
797 lockdep_init_map(&timer->lockdep_map, name, key, 0);
798}
799
800
801
802
803
804
805
806
807
808
809
810
811void init_timer_key(struct timer_list *timer, unsigned int flags,
812 const char *name, struct lock_class_key *key)
813{
814 debug_init(timer);
815 do_init_timer(timer, flags, name, key);
816}
817EXPORT_SYMBOL(init_timer_key);
818
819static inline void detach_timer(struct timer_list *timer, bool clear_pending)
820{
821 struct hlist_node *entry = &timer->entry;
822
823 debug_deactivate(timer);
824
825 __hlist_del(entry);
826 if (clear_pending)
827 entry->pprev = NULL;
828 entry->next = LIST_POISON2;
829}
830
831static int detach_if_pending(struct timer_list *timer, struct timer_base *base,
832 bool clear_pending)
833{
834 unsigned idx = timer_get_idx(timer);
835
836 if (!timer_pending(timer))
837 return 0;
838
839 if (hlist_is_singular_node(&timer->entry, base->vectors + idx))
840 __clear_bit(idx, base->pending_map);
841
842 detach_timer(timer, clear_pending);
843 return 1;
844}
845
846static inline struct timer_base *get_timer_cpu_base(u32 tflags, u32 cpu)
847{
848 struct timer_base *base = per_cpu_ptr(&timer_bases[BASE_STD], cpu);
849
850
851
852
853
854 if (IS_ENABLED(CONFIG_NO_HZ_COMMON) && base->nohz_active &&
855 (tflags & TIMER_DEFERRABLE))
856 base = per_cpu_ptr(&timer_bases[BASE_DEF], cpu);
857 return base;
858}
859
860static inline struct timer_base *get_timer_this_cpu_base(u32 tflags)
861{
862 struct timer_base *base = this_cpu_ptr(&timer_bases[BASE_STD]);
863
864
865
866
867
868 if (IS_ENABLED(CONFIG_NO_HZ_COMMON) && base->nohz_active &&
869 (tflags & TIMER_DEFERRABLE))
870 base = this_cpu_ptr(&timer_bases[BASE_DEF]);
871 return base;
872}
873
874static inline struct timer_base *get_timer_base(u32 tflags)
875{
876 return get_timer_cpu_base(tflags, tflags & TIMER_CPUMASK);
877}
878
879#ifdef CONFIG_NO_HZ_COMMON
880static inline struct timer_base *
881get_target_base(struct timer_base *base, unsigned tflags)
882{
883#ifdef CONFIG_SMP
884 if ((tflags & TIMER_PINNED) || !base->migration_enabled)
885 return get_timer_this_cpu_base(tflags);
886 return get_timer_cpu_base(tflags, get_nohz_timer_target());
887#else
888 return get_timer_this_cpu_base(tflags);
889#endif
890}
891
892static inline void forward_timer_base(struct timer_base *base)
893{
894 unsigned long jnow = READ_ONCE(jiffies);
895
896
897
898
899
900 if (!base->is_idle || (long) (jnow - base->clk) < 2)
901 return;
902
903
904
905
906
907 if (time_after(base->next_expiry, jnow))
908 base->clk = jnow;
909 else
910 base->clk = base->next_expiry;
911}
912#else
913static inline struct timer_base *
914get_target_base(struct timer_base *base, unsigned tflags)
915{
916 return get_timer_this_cpu_base(tflags);
917}
918
919static inline void forward_timer_base(struct timer_base *base) { }
920#endif
921
922
923
924
925
926
927
928
929
930
931
932
933
934static struct timer_base *lock_timer_base(struct timer_list *timer,
935 unsigned long *flags)
936 __acquires(timer->base->lock)
937{
938 for (;;) {
939 struct timer_base *base;
940 u32 tf;
941
942
943
944
945
946
947 tf = READ_ONCE(timer->flags);
948
949 if (!(tf & TIMER_MIGRATING)) {
950 base = get_timer_base(tf);
951 spin_lock_irqsave(&base->lock, *flags);
952 if (timer->flags == tf)
953 return base;
954 spin_unlock_irqrestore(&base->lock, *flags);
955 }
956 cpu_relax();
957 }
958}
959
960static inline int
961__mod_timer(struct timer_list *timer, unsigned long expires, bool pending_only)
962{
963 struct timer_base *base, *new_base;
964 unsigned int idx = UINT_MAX;
965 unsigned long clk = 0, flags;
966 int ret = 0;
967
968 BUG_ON(!timer->function);
969
970
971
972
973
974
975 if (timer_pending(timer)) {
976 if (timer->expires == expires)
977 return 1;
978
979
980
981
982
983
984
985 base = lock_timer_base(timer, &flags);
986
987 clk = base->clk;
988 idx = calc_wheel_index(expires, clk);
989
990
991
992
993
994
995 if (idx == timer_get_idx(timer)) {
996 timer->expires = expires;
997 ret = 1;
998 goto out_unlock;
999 }
1000 } else {
1001 base = lock_timer_base(timer, &flags);
1002 }
1003
1004 timer_stats_timer_set_start_info(timer);
1005
1006 ret = detach_if_pending(timer, base, false);
1007 if (!ret && pending_only)
1008 goto out_unlock;
1009
1010 debug_activate(timer, expires);
1011
1012 new_base = get_target_base(base, timer->flags);
1013
1014 if (base != new_base) {
1015
1016
1017
1018
1019
1020
1021
1022 if (likely(base->running_timer != timer)) {
1023
1024 timer->flags |= TIMER_MIGRATING;
1025
1026 spin_unlock(&base->lock);
1027 base = new_base;
1028 spin_lock(&base->lock);
1029 WRITE_ONCE(timer->flags,
1030 (timer->flags & ~TIMER_BASEMASK) | base->cpu);
1031 }
1032 }
1033
1034
1035 forward_timer_base(base);
1036
1037 timer->expires = expires;
1038
1039
1040
1041
1042
1043
1044
1045 if (idx != UINT_MAX && clk == base->clk) {
1046 enqueue_timer(base, timer, idx);
1047 trigger_dyntick_cpu(base, timer);
1048 } else {
1049 internal_add_timer(base, timer);
1050 }
1051
1052out_unlock:
1053 spin_unlock_irqrestore(&base->lock, flags);
1054
1055 return ret;
1056}
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068int mod_timer_pending(struct timer_list *timer, unsigned long expires)
1069{
1070 return __mod_timer(timer, expires, true);
1071}
1072EXPORT_SYMBOL(mod_timer_pending);
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094int mod_timer(struct timer_list *timer, unsigned long expires)
1095{
1096 return __mod_timer(timer, expires, false);
1097}
1098EXPORT_SYMBOL(mod_timer);
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114void add_timer(struct timer_list *timer)
1115{
1116 BUG_ON(timer_pending(timer));
1117 mod_timer(timer, timer->expires);
1118}
1119EXPORT_SYMBOL(add_timer);
1120
1121
1122
1123
1124
1125
1126
1127
1128void add_timer_on(struct timer_list *timer, int cpu)
1129{
1130 struct timer_base *new_base, *base;
1131 unsigned long flags;
1132
1133 timer_stats_timer_set_start_info(timer);
1134 BUG_ON(timer_pending(timer) || !timer->function);
1135
1136 new_base = get_timer_cpu_base(timer->flags, cpu);
1137
1138
1139
1140
1141
1142
1143 base = lock_timer_base(timer, &flags);
1144 if (base != new_base) {
1145 timer->flags |= TIMER_MIGRATING;
1146
1147 spin_unlock(&base->lock);
1148 base = new_base;
1149 spin_lock(&base->lock);
1150 WRITE_ONCE(timer->flags,
1151 (timer->flags & ~TIMER_BASEMASK) | cpu);
1152 }
1153
1154 debug_activate(timer, timer->expires);
1155 internal_add_timer(base, timer);
1156 spin_unlock_irqrestore(&base->lock, flags);
1157}
1158EXPORT_SYMBOL_GPL(add_timer_on);
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171int del_timer(struct timer_list *timer)
1172{
1173 struct timer_base *base;
1174 unsigned long flags;
1175 int ret = 0;
1176
1177 debug_assert_init(timer);
1178
1179 timer_stats_timer_clear_start_info(timer);
1180 if (timer_pending(timer)) {
1181 base = lock_timer_base(timer, &flags);
1182 ret = detach_if_pending(timer, base, true);
1183 spin_unlock_irqrestore(&base->lock, flags);
1184 }
1185
1186 return ret;
1187}
1188EXPORT_SYMBOL(del_timer);
1189
1190
1191
1192
1193
1194
1195
1196
1197int try_to_del_timer_sync(struct timer_list *timer)
1198{
1199 struct timer_base *base;
1200 unsigned long flags;
1201 int ret = -1;
1202
1203 debug_assert_init(timer);
1204
1205 base = lock_timer_base(timer, &flags);
1206
1207 if (base->running_timer != timer) {
1208 timer_stats_timer_clear_start_info(timer);
1209 ret = detach_if_pending(timer, base, true);
1210 }
1211 spin_unlock_irqrestore(&base->lock, flags);
1212
1213 return ret;
1214}
1215EXPORT_SYMBOL(try_to_del_timer_sync);
1216
1217#ifdef CONFIG_SMP
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254int del_timer_sync(struct timer_list *timer)
1255{
1256#ifdef CONFIG_LOCKDEP
1257 unsigned long flags;
1258
1259
1260
1261
1262
1263 local_irq_save(flags);
1264 lock_map_acquire(&timer->lockdep_map);
1265 lock_map_release(&timer->lockdep_map);
1266 local_irq_restore(flags);
1267#endif
1268
1269
1270
1271
1272 WARN_ON(in_irq() && !(timer->flags & TIMER_IRQSAFE));
1273 for (;;) {
1274 int ret = try_to_del_timer_sync(timer);
1275 if (ret >= 0)
1276 return ret;
1277 cpu_relax();
1278 }
1279}
1280EXPORT_SYMBOL(del_timer_sync);
1281#endif
1282
1283static void call_timer_fn(struct timer_list *timer, void (*fn)(unsigned long),
1284 unsigned long data)
1285{
1286 int count = preempt_count();
1287
1288#ifdef CONFIG_LOCKDEP
1289
1290
1291
1292
1293
1294
1295
1296 struct lockdep_map lockdep_map;
1297
1298 lockdep_copy_map(&lockdep_map, &timer->lockdep_map);
1299#endif
1300
1301
1302
1303
1304
1305 lock_map_acquire(&lockdep_map);
1306
1307 trace_timer_expire_entry(timer);
1308 fn(data);
1309 trace_timer_expire_exit(timer);
1310
1311 lock_map_release(&lockdep_map);
1312
1313 if (count != preempt_count()) {
1314 WARN_ONCE(1, "timer: %pF preempt leak: %08x -> %08x\n",
1315 fn, count, preempt_count());
1316
1317
1318
1319
1320
1321
1322 preempt_count_set(count);
1323 }
1324}
1325
1326static void expire_timers(struct timer_base *base, struct hlist_head *head)
1327{
1328 while (!hlist_empty(head)) {
1329 struct timer_list *timer;
1330 void (*fn)(unsigned long);
1331 unsigned long data;
1332
1333 timer = hlist_entry(head->first, struct timer_list, entry);
1334 timer_stats_account_timer(timer);
1335
1336 base->running_timer = timer;
1337 detach_timer(timer, true);
1338
1339 fn = timer->function;
1340 data = timer->data;
1341
1342 if (timer->flags & TIMER_IRQSAFE) {
1343 spin_unlock(&base->lock);
1344 call_timer_fn(timer, fn, data);
1345 spin_lock(&base->lock);
1346 } else {
1347 spin_unlock_irq(&base->lock);
1348 call_timer_fn(timer, fn, data);
1349 spin_lock_irq(&base->lock);
1350 }
1351 }
1352}
1353
1354static int __collect_expired_timers(struct timer_base *base,
1355 struct hlist_head *heads)
1356{
1357 unsigned long clk = base->clk;
1358 struct hlist_head *vec;
1359 int i, levels = 0;
1360 unsigned int idx;
1361
1362 for (i = 0; i < LVL_DEPTH; i++) {
1363 idx = (clk & LVL_MASK) + i * LVL_SIZE;
1364
1365 if (__test_and_clear_bit(idx, base->pending_map)) {
1366 vec = base->vectors + idx;
1367 hlist_move_list(vec, heads++);
1368 levels++;
1369 }
1370
1371 if (clk & LVL_CLK_MASK)
1372 break;
1373
1374 clk >>= LVL_CLK_SHIFT;
1375 }
1376 return levels;
1377}
1378
1379#ifdef CONFIG_NO_HZ_COMMON
1380
1381
1382
1383
1384
1385static int next_pending_bucket(struct timer_base *base, unsigned offset,
1386 unsigned clk)
1387{
1388 unsigned pos, start = offset + clk;
1389 unsigned end = offset + LVL_SIZE;
1390
1391 pos = find_next_bit(base->pending_map, end, start);
1392 if (pos < end)
1393 return pos - start;
1394
1395 pos = find_next_bit(base->pending_map, start, offset);
1396 return pos < start ? pos + LVL_SIZE - start : -1;
1397}
1398
1399
1400
1401
1402
1403static unsigned long __next_timer_interrupt(struct timer_base *base)
1404{
1405 unsigned long clk, next, adj;
1406 unsigned lvl, offset = 0;
1407
1408 next = base->clk + NEXT_TIMER_MAX_DELTA;
1409 clk = base->clk;
1410 for (lvl = 0; lvl < LVL_DEPTH; lvl++, offset += LVL_SIZE) {
1411 int pos = next_pending_bucket(base, offset, clk & LVL_MASK);
1412
1413 if (pos >= 0) {
1414 unsigned long tmp = clk + (unsigned long) pos;
1415
1416 tmp <<= LVL_SHIFT(lvl);
1417 if (time_before(tmp, next))
1418 next = tmp;
1419 }
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456 adj = clk & LVL_CLK_MASK ? 1 : 0;
1457 clk >>= LVL_CLK_SHIFT;
1458 clk += adj;
1459 }
1460 return next;
1461}
1462
1463
1464
1465
1466
1467static u64 cmp_next_hrtimer_event(u64 basem, u64 expires)
1468{
1469 u64 nextevt = hrtimer_get_next_event();
1470
1471
1472
1473
1474
1475 if (expires <= nextevt)
1476 return expires;
1477
1478
1479
1480
1481
1482 if (nextevt <= basem)
1483 return basem;
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493 return DIV_ROUND_UP_ULL(nextevt, TICK_NSEC) * TICK_NSEC;
1494}
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504u64 get_next_timer_interrupt(unsigned long basej, u64 basem)
1505{
1506 struct timer_base *base = this_cpu_ptr(&timer_bases[BASE_STD]);
1507 u64 expires = KTIME_MAX;
1508 unsigned long nextevt;
1509 bool is_max_delta;
1510
1511
1512
1513
1514
1515 if (cpu_is_offline(smp_processor_id()))
1516 return expires;
1517
1518 spin_lock(&base->lock);
1519 nextevt = __next_timer_interrupt(base);
1520 is_max_delta = (nextevt == base->clk + NEXT_TIMER_MAX_DELTA);
1521 base->next_expiry = nextevt;
1522
1523
1524
1525
1526
1527 if (time_after(basej, base->clk)) {
1528 if (time_after(nextevt, basej))
1529 base->clk = basej;
1530 else if (time_after(nextevt, base->clk))
1531 base->clk = nextevt;
1532 }
1533
1534 if (time_before_eq(nextevt, basej)) {
1535 expires = basem;
1536 base->is_idle = false;
1537 } else {
1538 if (!is_max_delta)
1539 expires = basem + (nextevt - basej) * TICK_NSEC;
1540
1541
1542
1543 if ((expires - basem) > TICK_NSEC)
1544 base->is_idle = true;
1545 }
1546 spin_unlock(&base->lock);
1547
1548 return cmp_next_hrtimer_event(basem, expires);
1549}
1550
1551
1552
1553
1554
1555
1556void timer_clear_idle(void)
1557{
1558 struct timer_base *base = this_cpu_ptr(&timer_bases[BASE_STD]);
1559
1560
1561
1562
1563
1564
1565
1566 base->is_idle = false;
1567}
1568
1569static int collect_expired_timers(struct timer_base *base,
1570 struct hlist_head *heads)
1571{
1572
1573
1574
1575
1576
1577 if ((long)(jiffies - base->clk) > 2) {
1578 unsigned long next = __next_timer_interrupt(base);
1579
1580
1581
1582
1583
1584 if (time_after(next, jiffies)) {
1585
1586 base->clk = jiffies - 1;
1587 return 0;
1588 }
1589 base->clk = next;
1590 }
1591 return __collect_expired_timers(base, heads);
1592}
1593#else
1594static inline int collect_expired_timers(struct timer_base *base,
1595 struct hlist_head *heads)
1596{
1597 return __collect_expired_timers(base, heads);
1598}
1599#endif
1600
1601
1602
1603
1604
1605void update_process_times(int user_tick)
1606{
1607 struct task_struct *p = current;
1608
1609
1610 account_process_tick(p, user_tick);
1611 run_local_timers();
1612 rcu_check_callbacks(user_tick);
1613#ifdef CONFIG_IRQ_WORK
1614 if (in_irq())
1615 irq_work_tick();
1616#endif
1617 scheduler_tick();
1618 if (IS_ENABLED(CONFIG_POSIX_TIMERS))
1619 run_posix_cpu_timers(p);
1620}
1621
1622
1623
1624
1625
1626static inline void __run_timers(struct timer_base *base)
1627{
1628 struct hlist_head heads[LVL_DEPTH];
1629 int levels;
1630
1631 if (!time_after_eq(jiffies, base->clk))
1632 return;
1633
1634 spin_lock_irq(&base->lock);
1635
1636 while (time_after_eq(jiffies, base->clk)) {
1637
1638 levels = collect_expired_timers(base, heads);
1639 base->clk++;
1640
1641 while (levels--)
1642 expire_timers(base, heads + levels);
1643 }
1644 base->running_timer = NULL;
1645 spin_unlock_irq(&base->lock);
1646}
1647
1648
1649
1650
1651static __latent_entropy void run_timer_softirq(struct softirq_action *h)
1652{
1653 struct timer_base *base = this_cpu_ptr(&timer_bases[BASE_STD]);
1654
1655 __run_timers(base);
1656 if (IS_ENABLED(CONFIG_NO_HZ_COMMON) && base->nohz_active)
1657 __run_timers(this_cpu_ptr(&timer_bases[BASE_DEF]));
1658}
1659
1660
1661
1662
1663void run_local_timers(void)
1664{
1665 struct timer_base *base = this_cpu_ptr(&timer_bases[BASE_STD]);
1666
1667 hrtimer_run_queues();
1668
1669 if (time_before(jiffies, base->clk)) {
1670 if (!IS_ENABLED(CONFIG_NO_HZ_COMMON) || !base->nohz_active)
1671 return;
1672
1673 base++;
1674 if (time_before(jiffies, base->clk))
1675 return;
1676 }
1677 raise_softirq(TIMER_SOFTIRQ);
1678}
1679
1680static void process_timeout(unsigned long __data)
1681{
1682 wake_up_process((struct task_struct *)__data);
1683}
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714signed long __sched schedule_timeout(signed long timeout)
1715{
1716 struct timer_list timer;
1717 unsigned long expire;
1718
1719 switch (timeout)
1720 {
1721 case MAX_SCHEDULE_TIMEOUT:
1722
1723
1724
1725
1726
1727
1728
1729 schedule();
1730 goto out;
1731 default:
1732
1733
1734
1735
1736
1737
1738
1739 if (timeout < 0) {
1740 printk(KERN_ERR "schedule_timeout: wrong timeout "
1741 "value %lx\n", timeout);
1742 dump_stack();
1743 current->state = TASK_RUNNING;
1744 goto out;
1745 }
1746 }
1747
1748 expire = timeout + jiffies;
1749
1750 setup_timer_on_stack(&timer, process_timeout, (unsigned long)current);
1751 __mod_timer(&timer, expire, false);
1752 schedule();
1753 del_singleshot_timer_sync(&timer);
1754
1755
1756 destroy_timer_on_stack(&timer);
1757
1758 timeout = expire - jiffies;
1759
1760 out:
1761 return timeout < 0 ? 0 : timeout;
1762}
1763EXPORT_SYMBOL(schedule_timeout);
1764
1765
1766
1767
1768
1769signed long __sched schedule_timeout_interruptible(signed long timeout)
1770{
1771 __set_current_state(TASK_INTERRUPTIBLE);
1772 return schedule_timeout(timeout);
1773}
1774EXPORT_SYMBOL(schedule_timeout_interruptible);
1775
1776signed long __sched schedule_timeout_killable(signed long timeout)
1777{
1778 __set_current_state(TASK_KILLABLE);
1779 return schedule_timeout(timeout);
1780}
1781EXPORT_SYMBOL(schedule_timeout_killable);
1782
1783signed long __sched schedule_timeout_uninterruptible(signed long timeout)
1784{
1785 __set_current_state(TASK_UNINTERRUPTIBLE);
1786 return schedule_timeout(timeout);
1787}
1788EXPORT_SYMBOL(schedule_timeout_uninterruptible);
1789
1790
1791
1792
1793
1794signed long __sched schedule_timeout_idle(signed long timeout)
1795{
1796 __set_current_state(TASK_IDLE);
1797 return schedule_timeout(timeout);
1798}
1799EXPORT_SYMBOL(schedule_timeout_idle);
1800
1801#ifdef CONFIG_HOTPLUG_CPU
1802static void migrate_timer_list(struct timer_base *new_base, struct hlist_head *head)
1803{
1804 struct timer_list *timer;
1805 int cpu = new_base->cpu;
1806
1807 while (!hlist_empty(head)) {
1808 timer = hlist_entry(head->first, struct timer_list, entry);
1809 detach_timer(timer, false);
1810 timer->flags = (timer->flags & ~TIMER_BASEMASK) | cpu;
1811 internal_add_timer(new_base, timer);
1812 }
1813}
1814
1815int timers_dead_cpu(unsigned int cpu)
1816{
1817 struct timer_base *old_base;
1818 struct timer_base *new_base;
1819 int b, i;
1820
1821 BUG_ON(cpu_online(cpu));
1822
1823 for (b = 0; b < NR_BASES; b++) {
1824 old_base = per_cpu_ptr(&timer_bases[b], cpu);
1825 new_base = get_cpu_ptr(&timer_bases[b]);
1826
1827
1828
1829
1830 spin_lock_irq(&new_base->lock);
1831 spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
1832
1833 BUG_ON(old_base->running_timer);
1834
1835 for (i = 0; i < WHEEL_SIZE; i++)
1836 migrate_timer_list(new_base, old_base->vectors + i);
1837
1838 spin_unlock(&old_base->lock);
1839 spin_unlock_irq(&new_base->lock);
1840 put_cpu_ptr(&timer_bases);
1841 }
1842 return 0;
1843}
1844
1845#endif
1846
1847static void __init init_timer_cpu(int cpu)
1848{
1849 struct timer_base *base;
1850 int i;
1851
1852 for (i = 0; i < NR_BASES; i++) {
1853 base = per_cpu_ptr(&timer_bases[i], cpu);
1854 base->cpu = cpu;
1855 spin_lock_init(&base->lock);
1856 base->clk = jiffies;
1857 }
1858}
1859
1860static void __init init_timer_cpus(void)
1861{
1862 int cpu;
1863
1864 for_each_possible_cpu(cpu)
1865 init_timer_cpu(cpu);
1866}
1867
1868void __init init_timers(void)
1869{
1870 init_timer_cpus();
1871 init_timer_stats();
1872 open_softirq(TIMER_SOFTIRQ, run_timer_softirq);
1873}
1874
1875
1876
1877
1878
1879void msleep(unsigned int msecs)
1880{
1881 unsigned long timeout = msecs_to_jiffies(msecs) + 1;
1882
1883 while (timeout)
1884 timeout = schedule_timeout_uninterruptible(timeout);
1885}
1886
1887EXPORT_SYMBOL(msleep);
1888
1889
1890
1891
1892
1893unsigned long msleep_interruptible(unsigned int msecs)
1894{
1895 unsigned long timeout = msecs_to_jiffies(msecs) + 1;
1896
1897 while (timeout && !signal_pending(current))
1898 timeout = schedule_timeout_interruptible(timeout);
1899 return jiffies_to_msecs(timeout);
1900}
1901
1902EXPORT_SYMBOL(msleep_interruptible);
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915void __sched usleep_range(unsigned long min, unsigned long max)
1916{
1917 ktime_t exp = ktime_add_us(ktime_get(), min);
1918 u64 delta = (u64)(max - min) * NSEC_PER_USEC;
1919
1920 for (;;) {
1921 __set_current_state(TASK_UNINTERRUPTIBLE);
1922
1923 if (!schedule_hrtimeout_range(&exp, delta, HRTIMER_MODE_ABS))
1924 break;
1925 }
1926}
1927EXPORT_SYMBOL(usleep_range);
1928