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 <asm/uaccess.h>
47#include <asm/unistd.h>
48#include <asm/div64.h>
49#include <asm/timex.h>
50#include <asm/io.h>
51
52#define CREATE_TRACE_POINTS
53#include <trace/events/timer.h>
54
55u64 jiffies_64 __cacheline_aligned_in_smp = INITIAL_JIFFIES;
56
57EXPORT_SYMBOL(jiffies_64);
58
59
60
61
62#define TVN_BITS (CONFIG_BASE_SMALL ? 4 : 6)
63#define TVR_BITS (CONFIG_BASE_SMALL ? 6 : 8)
64#define TVN_SIZE (1 << TVN_BITS)
65#define TVR_SIZE (1 << TVR_BITS)
66#define TVN_MASK (TVN_SIZE - 1)
67#define TVR_MASK (TVR_SIZE - 1)
68#define MAX_TVAL ((unsigned long)((1ULL << (TVR_BITS + 4*TVN_BITS)) - 1))
69
70struct tvec {
71 struct list_head vec[TVN_SIZE];
72};
73
74struct tvec_root {
75 struct list_head vec[TVR_SIZE];
76};
77
78struct tvec_base {
79 spinlock_t lock;
80 struct timer_list *running_timer;
81 unsigned long timer_jiffies;
82 unsigned long next_timer;
83 unsigned long active_timers;
84 struct tvec_root tv1;
85 struct tvec tv2;
86 struct tvec tv3;
87 struct tvec tv4;
88 struct tvec tv5;
89} ____cacheline_aligned;
90
91struct tvec_base boot_tvec_bases;
92EXPORT_SYMBOL(boot_tvec_bases);
93static DEFINE_PER_CPU(struct tvec_base *, tvec_bases) = &boot_tvec_bases;
94
95
96static inline unsigned int tbase_get_deferrable(struct tvec_base *base)
97{
98 return ((unsigned int)(unsigned long)base & TIMER_DEFERRABLE);
99}
100
101static inline unsigned int tbase_get_irqsafe(struct tvec_base *base)
102{
103 return ((unsigned int)(unsigned long)base & TIMER_IRQSAFE);
104}
105
106static inline struct tvec_base *tbase_get_base(struct tvec_base *base)
107{
108 return ((struct tvec_base *)((unsigned long)base & ~TIMER_FLAG_MASK));
109}
110
111static inline void
112timer_set_base(struct timer_list *timer, struct tvec_base *new_base)
113{
114 unsigned long flags = (unsigned long)timer->base & TIMER_FLAG_MASK;
115
116 timer->base = (struct tvec_base *)((unsigned long)(new_base) | flags);
117}
118
119static unsigned long round_jiffies_common(unsigned long j, int cpu,
120 bool force_up)
121{
122 int rem;
123 unsigned long original = j;
124
125
126
127
128
129
130
131
132
133 j += cpu * 3;
134
135 rem = j % HZ;
136
137
138
139
140
141
142
143
144 if (rem < HZ/4 && !force_up)
145 j = j - rem;
146 else
147 j = j - rem + HZ;
148
149
150 j -= cpu * 3;
151
152
153
154
155
156 return time_is_after_jiffies(j) ? j : original;
157}
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179unsigned long __round_jiffies(unsigned long j, int cpu)
180{
181 return round_jiffies_common(j, cpu, false);
182}
183EXPORT_SYMBOL_GPL(__round_jiffies);
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205unsigned long __round_jiffies_relative(unsigned long j, int cpu)
206{
207 unsigned long j0 = jiffies;
208
209
210 return round_jiffies_common(j + j0, cpu, false) - j0;
211}
212EXPORT_SYMBOL_GPL(__round_jiffies_relative);
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229unsigned long round_jiffies(unsigned long j)
230{
231 return round_jiffies_common(j, raw_smp_processor_id(), false);
232}
233EXPORT_SYMBOL_GPL(round_jiffies);
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250unsigned long round_jiffies_relative(unsigned long j)
251{
252 return __round_jiffies_relative(j, raw_smp_processor_id());
253}
254EXPORT_SYMBOL_GPL(round_jiffies_relative);
255
256
257
258
259
260
261
262
263
264
265
266unsigned long __round_jiffies_up(unsigned long j, int cpu)
267{
268 return round_jiffies_common(j, cpu, true);
269}
270EXPORT_SYMBOL_GPL(__round_jiffies_up);
271
272
273
274
275
276
277
278
279
280
281
282unsigned long __round_jiffies_up_relative(unsigned long j, int cpu)
283{
284 unsigned long j0 = jiffies;
285
286
287 return round_jiffies_common(j + j0, cpu, true) - j0;
288}
289EXPORT_SYMBOL_GPL(__round_jiffies_up_relative);
290
291
292
293
294
295
296
297
298
299
300unsigned long round_jiffies_up(unsigned long j)
301{
302 return round_jiffies_common(j, raw_smp_processor_id(), true);
303}
304EXPORT_SYMBOL_GPL(round_jiffies_up);
305
306
307
308
309
310
311
312
313
314
315unsigned long round_jiffies_up_relative(unsigned long j)
316{
317 return __round_jiffies_up_relative(j, raw_smp_processor_id());
318}
319EXPORT_SYMBOL_GPL(round_jiffies_up_relative);
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334void set_timer_slack(struct timer_list *timer, int slack_hz)
335{
336 timer->slack = slack_hz;
337}
338EXPORT_SYMBOL_GPL(set_timer_slack);
339
340static void
341__internal_add_timer(struct tvec_base *base, struct timer_list *timer)
342{
343 unsigned long expires = timer->expires;
344 unsigned long idx = expires - base->timer_jiffies;
345 struct list_head *vec;
346
347 if (idx < TVR_SIZE) {
348 int i = expires & TVR_MASK;
349 vec = base->tv1.vec + i;
350 } else if (idx < 1 << (TVR_BITS + TVN_BITS)) {
351 int i = (expires >> TVR_BITS) & TVN_MASK;
352 vec = base->tv2.vec + i;
353 } else if (idx < 1 << (TVR_BITS + 2 * TVN_BITS)) {
354 int i = (expires >> (TVR_BITS + TVN_BITS)) & TVN_MASK;
355 vec = base->tv3.vec + i;
356 } else if (idx < 1 << (TVR_BITS + 3 * TVN_BITS)) {
357 int i = (expires >> (TVR_BITS + 2 * TVN_BITS)) & TVN_MASK;
358 vec = base->tv4.vec + i;
359 } else if ((signed long) idx < 0) {
360
361
362
363
364 vec = base->tv1.vec + (base->timer_jiffies & TVR_MASK);
365 } else {
366 int i;
367
368
369
370
371 if (idx > MAX_TVAL) {
372 idx = MAX_TVAL;
373 expires = idx + base->timer_jiffies;
374 }
375 i = (expires >> (TVR_BITS + 3 * TVN_BITS)) & TVN_MASK;
376 vec = base->tv5.vec + i;
377 }
378
379
380
381 list_add_tail(&timer->entry, vec);
382}
383
384static void internal_add_timer(struct tvec_base *base, struct timer_list *timer)
385{
386 __internal_add_timer(base, timer);
387
388
389
390 if (!tbase_get_deferrable(timer->base)) {
391 if (time_before(timer->expires, base->next_timer))
392 base->next_timer = timer->expires;
393 base->active_timers++;
394 }
395}
396
397#ifdef CONFIG_TIMER_STATS
398void __timer_stats_timer_set_start_info(struct timer_list *timer, void *addr)
399{
400 if (timer->start_site)
401 return;
402
403 timer->start_site = addr;
404 memcpy(timer->start_comm, current->comm, TASK_COMM_LEN);
405 timer->start_pid = current->pid;
406}
407
408static void timer_stats_account_timer(struct timer_list *timer)
409{
410 unsigned int flag = 0;
411
412 if (likely(!timer->start_site))
413 return;
414 if (unlikely(tbase_get_deferrable(timer->base)))
415 flag |= TIMER_STATS_FLAG_DEFERRABLE;
416
417 timer_stats_update_stats(timer, timer->start_pid, timer->start_site,
418 timer->function, timer->start_comm, flag);
419}
420
421#else
422static void timer_stats_account_timer(struct timer_list *timer) {}
423#endif
424
425#ifdef CONFIG_DEBUG_OBJECTS_TIMERS
426
427static struct debug_obj_descr timer_debug_descr;
428
429static void *timer_debug_hint(void *addr)
430{
431 return ((struct timer_list *) addr)->function;
432}
433
434
435
436
437
438static int timer_fixup_init(void *addr, enum debug_obj_state state)
439{
440 struct timer_list *timer = addr;
441
442 switch (state) {
443 case ODEBUG_STATE_ACTIVE:
444 del_timer_sync(timer);
445 debug_object_init(timer, &timer_debug_descr);
446 return 1;
447 default:
448 return 0;
449 }
450}
451
452
453static void stub_timer(unsigned long data)
454{
455 WARN_ON(1);
456}
457
458
459
460
461
462
463static int timer_fixup_activate(void *addr, enum debug_obj_state state)
464{
465 struct timer_list *timer = addr;
466
467 switch (state) {
468
469 case ODEBUG_STATE_NOTAVAILABLE:
470
471
472
473
474
475 if (timer->entry.next == NULL &&
476 timer->entry.prev == TIMER_ENTRY_STATIC) {
477 debug_object_init(timer, &timer_debug_descr);
478 debug_object_activate(timer, &timer_debug_descr);
479 return 0;
480 } else {
481 setup_timer(timer, stub_timer, 0);
482 return 1;
483 }
484 return 0;
485
486 case ODEBUG_STATE_ACTIVE:
487 WARN_ON(1);
488
489 default:
490 return 0;
491 }
492}
493
494
495
496
497
498static int timer_fixup_free(void *addr, enum debug_obj_state state)
499{
500 struct timer_list *timer = addr;
501
502 switch (state) {
503 case ODEBUG_STATE_ACTIVE:
504 del_timer_sync(timer);
505 debug_object_free(timer, &timer_debug_descr);
506 return 1;
507 default:
508 return 0;
509 }
510}
511
512
513
514
515
516static int timer_fixup_assert_init(void *addr, enum debug_obj_state state)
517{
518 struct timer_list *timer = addr;
519
520 switch (state) {
521 case ODEBUG_STATE_NOTAVAILABLE:
522 if (timer->entry.prev == TIMER_ENTRY_STATIC) {
523
524
525
526
527
528 debug_object_init(timer, &timer_debug_descr);
529 return 0;
530 } else {
531 setup_timer(timer, stub_timer, 0);
532 return 1;
533 }
534 default:
535 return 0;
536 }
537}
538
539static struct debug_obj_descr timer_debug_descr = {
540 .name = "timer_list",
541 .debug_hint = timer_debug_hint,
542 .fixup_init = timer_fixup_init,
543 .fixup_activate = timer_fixup_activate,
544 .fixup_free = timer_fixup_free,
545 .fixup_assert_init = timer_fixup_assert_init,
546};
547
548static inline void debug_timer_init(struct timer_list *timer)
549{
550 debug_object_init(timer, &timer_debug_descr);
551}
552
553static inline void debug_timer_activate(struct timer_list *timer)
554{
555 debug_object_activate(timer, &timer_debug_descr);
556}
557
558static inline void debug_timer_deactivate(struct timer_list *timer)
559{
560 debug_object_deactivate(timer, &timer_debug_descr);
561}
562
563static inline void debug_timer_free(struct timer_list *timer)
564{
565 debug_object_free(timer, &timer_debug_descr);
566}
567
568static inline void debug_timer_assert_init(struct timer_list *timer)
569{
570 debug_object_assert_init(timer, &timer_debug_descr);
571}
572
573static void do_init_timer(struct timer_list *timer, unsigned int flags,
574 const char *name, struct lock_class_key *key);
575
576void init_timer_on_stack_key(struct timer_list *timer, unsigned int flags,
577 const char *name, struct lock_class_key *key)
578{
579 debug_object_init_on_stack(timer, &timer_debug_descr);
580 do_init_timer(timer, flags, name, key);
581}
582EXPORT_SYMBOL_GPL(init_timer_on_stack_key);
583
584void destroy_timer_on_stack(struct timer_list *timer)
585{
586 debug_object_free(timer, &timer_debug_descr);
587}
588EXPORT_SYMBOL_GPL(destroy_timer_on_stack);
589
590#else
591static inline void debug_timer_init(struct timer_list *timer) { }
592static inline void debug_timer_activate(struct timer_list *timer) { }
593static inline void debug_timer_deactivate(struct timer_list *timer) { }
594static inline void debug_timer_assert_init(struct timer_list *timer) { }
595#endif
596
597static inline void debug_init(struct timer_list *timer)
598{
599 debug_timer_init(timer);
600 trace_timer_init(timer);
601}
602
603static inline void
604debug_activate(struct timer_list *timer, unsigned long expires)
605{
606 debug_timer_activate(timer);
607 trace_timer_start(timer, expires);
608}
609
610static inline void debug_deactivate(struct timer_list *timer)
611{
612 debug_timer_deactivate(timer);
613 trace_timer_cancel(timer);
614}
615
616static inline void debug_assert_init(struct timer_list *timer)
617{
618 debug_timer_assert_init(timer);
619}
620
621static void do_init_timer(struct timer_list *timer, unsigned int flags,
622 const char *name, struct lock_class_key *key)
623{
624 struct tvec_base *base = __raw_get_cpu_var(tvec_bases);
625
626 timer->entry.next = NULL;
627 timer->base = (void *)((unsigned long)base | flags);
628 timer->slack = -1;
629#ifdef CONFIG_TIMER_STATS
630 timer->start_site = NULL;
631 timer->start_pid = -1;
632 memset(timer->start_comm, 0, TASK_COMM_LEN);
633#endif
634 lockdep_init_map(&timer->lockdep_map, name, key, 0);
635}
636
637
638
639
640
641
642
643
644
645
646
647
648void init_timer_key(struct timer_list *timer, unsigned int flags,
649 const char *name, struct lock_class_key *key)
650{
651 debug_init(timer);
652 do_init_timer(timer, flags, name, key);
653}
654EXPORT_SYMBOL(init_timer_key);
655
656static inline void detach_timer(struct timer_list *timer, bool clear_pending)
657{
658 struct list_head *entry = &timer->entry;
659
660 debug_deactivate(timer);
661
662 __list_del(entry->prev, entry->next);
663 if (clear_pending)
664 entry->next = NULL;
665 entry->prev = LIST_POISON2;
666}
667
668static inline void
669detach_expired_timer(struct timer_list *timer, struct tvec_base *base)
670{
671 detach_timer(timer, true);
672 if (!tbase_get_deferrable(timer->base))
673 base->active_timers--;
674}
675
676static int detach_if_pending(struct timer_list *timer, struct tvec_base *base,
677 bool clear_pending)
678{
679 if (!timer_pending(timer))
680 return 0;
681
682 detach_timer(timer, clear_pending);
683 if (!tbase_get_deferrable(timer->base)) {
684 base->active_timers--;
685 if (timer->expires == base->next_timer)
686 base->next_timer = base->timer_jiffies;
687 }
688 return 1;
689}
690
691
692
693
694
695
696
697
698
699
700
701
702
703static struct tvec_base *lock_timer_base(struct timer_list *timer,
704 unsigned long *flags)
705 __acquires(timer->base->lock)
706{
707 struct tvec_base *base;
708
709 for (;;) {
710 struct tvec_base *prelock_base = timer->base;
711 base = tbase_get_base(prelock_base);
712 if (likely(base != NULL)) {
713 spin_lock_irqsave(&base->lock, *flags);
714 if (likely(prelock_base == timer->base))
715 return base;
716
717 spin_unlock_irqrestore(&base->lock, *flags);
718 }
719 cpu_relax();
720 }
721}
722
723static inline int
724__mod_timer(struct timer_list *timer, unsigned long expires,
725 bool pending_only, int pinned)
726{
727 struct tvec_base *base, *new_base;
728 unsigned long flags;
729 int ret = 0 , cpu;
730
731 timer_stats_timer_set_start_info(timer);
732 BUG_ON(!timer->function);
733
734 base = lock_timer_base(timer, &flags);
735
736 ret = detach_if_pending(timer, base, false);
737 if (!ret && pending_only)
738 goto out_unlock;
739
740 debug_activate(timer, expires);
741
742 cpu = smp_processor_id();
743
744#if defined(CONFIG_NO_HZ_COMMON) && defined(CONFIG_SMP)
745 if (!pinned && get_sysctl_timer_migration() && idle_cpu(cpu))
746 cpu = get_nohz_timer_target();
747#endif
748 new_base = per_cpu(tvec_bases, cpu);
749
750 if (base != new_base) {
751
752
753
754
755
756
757
758 if (likely(base->running_timer != timer)) {
759
760 timer_set_base(timer, NULL);
761 spin_unlock(&base->lock);
762 base = new_base;
763 spin_lock(&base->lock);
764 timer_set_base(timer, base);
765 }
766 }
767
768 timer->expires = expires;
769 internal_add_timer(base, timer);
770
771out_unlock:
772 spin_unlock_irqrestore(&base->lock, flags);
773
774 return ret;
775}
776
777
778
779
780
781
782
783
784
785
786
787int mod_timer_pending(struct timer_list *timer, unsigned long expires)
788{
789 return __mod_timer(timer, expires, true, TIMER_NOT_PINNED);
790}
791EXPORT_SYMBOL(mod_timer_pending);
792
793
794
795
796
797
798
799
800
801
802
803static inline
804unsigned long apply_slack(struct timer_list *timer, unsigned long expires)
805{
806 unsigned long expires_limit, mask;
807 int bit;
808
809 if (timer->slack >= 0) {
810 expires_limit = expires + timer->slack;
811 } else {
812 long delta = expires - jiffies;
813
814 if (delta < 256)
815 return expires;
816
817 expires_limit = expires + delta / 256;
818 }
819 mask = expires ^ expires_limit;
820 if (mask == 0)
821 return expires;
822
823 bit = find_last_bit(&mask, BITS_PER_LONG);
824
825 mask = (1 << bit) - 1;
826
827 expires_limit = expires_limit & ~(mask);
828
829 return expires_limit;
830}
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852int mod_timer(struct timer_list *timer, unsigned long expires)
853{
854 expires = apply_slack(timer, expires);
855
856
857
858
859
860
861 if (timer_pending(timer) && timer->expires == expires)
862 return 1;
863
864 return __mod_timer(timer, expires, false, TIMER_NOT_PINNED);
865}
866EXPORT_SYMBOL(mod_timer);
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887int mod_timer_pinned(struct timer_list *timer, unsigned long expires)
888{
889 if (timer->expires == expires && timer_pending(timer))
890 return 1;
891
892 return __mod_timer(timer, expires, false, TIMER_PINNED);
893}
894EXPORT_SYMBOL(mod_timer_pinned);
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910void add_timer(struct timer_list *timer)
911{
912 BUG_ON(timer_pending(timer));
913 mod_timer(timer, timer->expires);
914}
915EXPORT_SYMBOL(add_timer);
916
917
918
919
920
921
922
923
924void add_timer_on(struct timer_list *timer, int cpu)
925{
926 struct tvec_base *base = per_cpu(tvec_bases, cpu);
927 unsigned long flags;
928
929 timer_stats_timer_set_start_info(timer);
930 BUG_ON(timer_pending(timer) || !timer->function);
931 spin_lock_irqsave(&base->lock, flags);
932 timer_set_base(timer, base);
933 debug_activate(timer, timer->expires);
934 internal_add_timer(base, timer);
935
936
937
938
939
940
941
942
943 wake_up_nohz_cpu(cpu);
944 spin_unlock_irqrestore(&base->lock, flags);
945}
946EXPORT_SYMBOL_GPL(add_timer_on);
947
948
949
950
951
952
953
954
955
956
957
958
959int del_timer(struct timer_list *timer)
960{
961 struct tvec_base *base;
962 unsigned long flags;
963 int ret = 0;
964
965 debug_assert_init(timer);
966
967 timer_stats_timer_clear_start_info(timer);
968 if (timer_pending(timer)) {
969 base = lock_timer_base(timer, &flags);
970 ret = detach_if_pending(timer, base, true);
971 spin_unlock_irqrestore(&base->lock, flags);
972 }
973
974 return ret;
975}
976EXPORT_SYMBOL(del_timer);
977
978
979
980
981
982
983
984
985int try_to_del_timer_sync(struct timer_list *timer)
986{
987 struct tvec_base *base;
988 unsigned long flags;
989 int ret = -1;
990
991 debug_assert_init(timer);
992
993 base = lock_timer_base(timer, &flags);
994
995 if (base->running_timer != timer) {
996 timer_stats_timer_clear_start_info(timer);
997 ret = detach_if_pending(timer, base, true);
998 }
999 spin_unlock_irqrestore(&base->lock, flags);
1000
1001 return ret;
1002}
1003EXPORT_SYMBOL(try_to_del_timer_sync);
1004
1005#ifdef CONFIG_SMP
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042int del_timer_sync(struct timer_list *timer)
1043{
1044#ifdef CONFIG_LOCKDEP
1045 unsigned long flags;
1046
1047
1048
1049
1050
1051 local_irq_save(flags);
1052 lock_map_acquire(&timer->lockdep_map);
1053 lock_map_release(&timer->lockdep_map);
1054 local_irq_restore(flags);
1055#endif
1056
1057
1058
1059
1060 WARN_ON(in_irq() && !tbase_get_irqsafe(timer->base));
1061 for (;;) {
1062 int ret = try_to_del_timer_sync(timer);
1063 if (ret >= 0)
1064 return ret;
1065 cpu_relax();
1066 }
1067}
1068EXPORT_SYMBOL(del_timer_sync);
1069#endif
1070
1071static int cascade(struct tvec_base *base, struct tvec *tv, int index)
1072{
1073
1074 struct timer_list *timer, *tmp;
1075 struct list_head tv_list;
1076
1077 list_replace_init(tv->vec + index, &tv_list);
1078
1079
1080
1081
1082
1083 list_for_each_entry_safe(timer, tmp, &tv_list, entry) {
1084 BUG_ON(tbase_get_base(timer->base) != base);
1085
1086 __internal_add_timer(base, timer);
1087 }
1088
1089 return index;
1090}
1091
1092static void call_timer_fn(struct timer_list *timer, void (*fn)(unsigned long),
1093 unsigned long data)
1094{
1095 int count = preempt_count();
1096
1097#ifdef CONFIG_LOCKDEP
1098
1099
1100
1101
1102
1103
1104
1105 struct lockdep_map lockdep_map;
1106
1107 lockdep_copy_map(&lockdep_map, &timer->lockdep_map);
1108#endif
1109
1110
1111
1112
1113
1114 lock_map_acquire(&lockdep_map);
1115
1116 trace_timer_expire_entry(timer);
1117 fn(data);
1118 trace_timer_expire_exit(timer);
1119
1120 lock_map_release(&lockdep_map);
1121
1122 if (count != preempt_count()) {
1123 WARN_ONCE(1, "timer: %pF preempt leak: %08x -> %08x\n",
1124 fn, count, preempt_count());
1125
1126
1127
1128
1129
1130
1131 preempt_count_set(count);
1132 }
1133}
1134
1135#define INDEX(N) ((base->timer_jiffies >> (TVR_BITS + (N) * TVN_BITS)) & TVN_MASK)
1136
1137
1138
1139
1140
1141
1142
1143
1144static inline void __run_timers(struct tvec_base *base)
1145{
1146 struct timer_list *timer;
1147
1148 spin_lock_irq(&base->lock);
1149 while (time_after_eq(jiffies, base->timer_jiffies)) {
1150 struct list_head work_list;
1151 struct list_head *head = &work_list;
1152 int index = base->timer_jiffies & TVR_MASK;
1153
1154
1155
1156
1157 if (!index &&
1158 (!cascade(base, &base->tv2, INDEX(0))) &&
1159 (!cascade(base, &base->tv3, INDEX(1))) &&
1160 !cascade(base, &base->tv4, INDEX(2)))
1161 cascade(base, &base->tv5, INDEX(3));
1162 ++base->timer_jiffies;
1163 list_replace_init(base->tv1.vec + index, &work_list);
1164 while (!list_empty(head)) {
1165 void (*fn)(unsigned long);
1166 unsigned long data;
1167 bool irqsafe;
1168
1169 timer = list_first_entry(head, struct timer_list,entry);
1170 fn = timer->function;
1171 data = timer->data;
1172 irqsafe = tbase_get_irqsafe(timer->base);
1173
1174 timer_stats_account_timer(timer);
1175
1176 base->running_timer = timer;
1177 detach_expired_timer(timer, base);
1178
1179 if (irqsafe) {
1180 spin_unlock(&base->lock);
1181 call_timer_fn(timer, fn, data);
1182 spin_lock(&base->lock);
1183 } else {
1184 spin_unlock_irq(&base->lock);
1185 call_timer_fn(timer, fn, data);
1186 spin_lock_irq(&base->lock);
1187 }
1188 }
1189 }
1190 base->running_timer = NULL;
1191 spin_unlock_irq(&base->lock);
1192}
1193
1194#ifdef CONFIG_NO_HZ_COMMON
1195
1196
1197
1198
1199
1200static unsigned long __next_timer_interrupt(struct tvec_base *base)
1201{
1202 unsigned long timer_jiffies = base->timer_jiffies;
1203 unsigned long expires = timer_jiffies + NEXT_TIMER_MAX_DELTA;
1204 int index, slot, array, found = 0;
1205 struct timer_list *nte;
1206 struct tvec *varray[4];
1207
1208
1209 index = slot = timer_jiffies & TVR_MASK;
1210 do {
1211 list_for_each_entry(nte, base->tv1.vec + slot, entry) {
1212 if (tbase_get_deferrable(nte->base))
1213 continue;
1214
1215 found = 1;
1216 expires = nte->expires;
1217
1218 if (!index || slot < index)
1219 goto cascade;
1220 return expires;
1221 }
1222 slot = (slot + 1) & TVR_MASK;
1223 } while (slot != index);
1224
1225cascade:
1226
1227 if (index)
1228 timer_jiffies += TVR_SIZE - index;
1229 timer_jiffies >>= TVR_BITS;
1230
1231
1232 varray[0] = &base->tv2;
1233 varray[1] = &base->tv3;
1234 varray[2] = &base->tv4;
1235 varray[3] = &base->tv5;
1236
1237 for (array = 0; array < 4; array++) {
1238 struct tvec *varp = varray[array];
1239
1240 index = slot = timer_jiffies & TVN_MASK;
1241 do {
1242 list_for_each_entry(nte, varp->vec + slot, entry) {
1243 if (tbase_get_deferrable(nte->base))
1244 continue;
1245
1246 found = 1;
1247 if (time_before(nte->expires, expires))
1248 expires = nte->expires;
1249 }
1250
1251
1252
1253
1254 if (found) {
1255
1256 if (!index || slot < index)
1257 break;
1258 return expires;
1259 }
1260 slot = (slot + 1) & TVN_MASK;
1261 } while (slot != index);
1262
1263 if (index)
1264 timer_jiffies += TVN_SIZE - index;
1265 timer_jiffies >>= TVN_BITS;
1266 }
1267 return expires;
1268}
1269
1270
1271
1272
1273
1274static unsigned long cmp_next_hrtimer_event(unsigned long now,
1275 unsigned long expires)
1276{
1277 ktime_t hr_delta = hrtimer_get_next_event();
1278 struct timespec tsdelta;
1279 unsigned long delta;
1280
1281 if (hr_delta.tv64 == KTIME_MAX)
1282 return expires;
1283
1284
1285
1286
1287 if (hr_delta.tv64 <= 0)
1288 return now + 1;
1289
1290 tsdelta = ktime_to_timespec(hr_delta);
1291 delta = timespec_to_jiffies(&tsdelta);
1292
1293
1294
1295
1296
1297 if (delta > NEXT_TIMER_MAX_DELTA)
1298 delta = NEXT_TIMER_MAX_DELTA;
1299
1300
1301
1302
1303
1304
1305
1306 if (delta < 1)
1307 delta = 1;
1308 now += delta;
1309 if (time_before(now, expires))
1310 return now;
1311 return expires;
1312}
1313
1314
1315
1316
1317
1318unsigned long get_next_timer_interrupt(unsigned long now)
1319{
1320 struct tvec_base *base = __this_cpu_read(tvec_bases);
1321 unsigned long expires = now + NEXT_TIMER_MAX_DELTA;
1322
1323
1324
1325
1326
1327 if (cpu_is_offline(smp_processor_id()))
1328 return expires;
1329
1330 spin_lock(&base->lock);
1331 if (base->active_timers) {
1332 if (time_before_eq(base->next_timer, base->timer_jiffies))
1333 base->next_timer = __next_timer_interrupt(base);
1334 expires = base->next_timer;
1335 }
1336 spin_unlock(&base->lock);
1337
1338 if (time_before_eq(expires, now))
1339 return now;
1340
1341 return cmp_next_hrtimer_event(now, expires);
1342}
1343#endif
1344
1345
1346
1347
1348
1349void update_process_times(int user_tick)
1350{
1351 struct task_struct *p = current;
1352 int cpu = smp_processor_id();
1353
1354
1355 account_process_tick(p, user_tick);
1356 run_local_timers();
1357 rcu_check_callbacks(cpu, user_tick);
1358#ifdef CONFIG_IRQ_WORK
1359 if (in_irq())
1360 irq_work_run();
1361#endif
1362 scheduler_tick();
1363 run_posix_cpu_timers(p);
1364}
1365
1366
1367
1368
1369static void run_timer_softirq(struct softirq_action *h)
1370{
1371 struct tvec_base *base = __this_cpu_read(tvec_bases);
1372
1373 hrtimer_run_pending();
1374
1375 if (time_after_eq(jiffies, base->timer_jiffies))
1376 __run_timers(base);
1377}
1378
1379
1380
1381
1382void run_local_timers(void)
1383{
1384 hrtimer_run_queues();
1385 raise_softirq(TIMER_SOFTIRQ);
1386}
1387
1388#ifdef __ARCH_WANT_SYS_ALARM
1389
1390
1391
1392
1393
1394SYSCALL_DEFINE1(alarm, unsigned int, seconds)
1395{
1396 return alarm_setitimer(seconds);
1397}
1398
1399#endif
1400
1401static void process_timeout(unsigned long __data)
1402{
1403 wake_up_process((struct task_struct *)__data);
1404}
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432signed long __sched schedule_timeout(signed long timeout)
1433{
1434 struct timer_list timer;
1435 unsigned long expire;
1436
1437 switch (timeout)
1438 {
1439 case MAX_SCHEDULE_TIMEOUT:
1440
1441
1442
1443
1444
1445
1446
1447 schedule();
1448 goto out;
1449 default:
1450
1451
1452
1453
1454
1455
1456
1457 if (timeout < 0) {
1458 printk(KERN_ERR "schedule_timeout: wrong timeout "
1459 "value %lx\n", timeout);
1460 dump_stack();
1461 current->state = TASK_RUNNING;
1462 goto out;
1463 }
1464 }
1465
1466 expire = timeout + jiffies;
1467
1468 setup_timer_on_stack(&timer, process_timeout, (unsigned long)current);
1469 __mod_timer(&timer, expire, false, TIMER_NOT_PINNED);
1470 schedule();
1471 del_singleshot_timer_sync(&timer);
1472
1473
1474 destroy_timer_on_stack(&timer);
1475
1476 timeout = expire - jiffies;
1477
1478 out:
1479 return timeout < 0 ? 0 : timeout;
1480}
1481EXPORT_SYMBOL(schedule_timeout);
1482
1483
1484
1485
1486
1487signed long __sched schedule_timeout_interruptible(signed long timeout)
1488{
1489 __set_current_state(TASK_INTERRUPTIBLE);
1490 return schedule_timeout(timeout);
1491}
1492EXPORT_SYMBOL(schedule_timeout_interruptible);
1493
1494signed long __sched schedule_timeout_killable(signed long timeout)
1495{
1496 __set_current_state(TASK_KILLABLE);
1497 return schedule_timeout(timeout);
1498}
1499EXPORT_SYMBOL(schedule_timeout_killable);
1500
1501signed long __sched schedule_timeout_uninterruptible(signed long timeout)
1502{
1503 __set_current_state(TASK_UNINTERRUPTIBLE);
1504 return schedule_timeout(timeout);
1505}
1506EXPORT_SYMBOL(schedule_timeout_uninterruptible);
1507
1508static int init_timers_cpu(int cpu)
1509{
1510 int j;
1511 struct tvec_base *base;
1512 static char tvec_base_done[NR_CPUS];
1513
1514 if (!tvec_base_done[cpu]) {
1515 static char boot_done;
1516
1517 if (boot_done) {
1518
1519
1520
1521 base = kzalloc_node(sizeof(*base), GFP_KERNEL,
1522 cpu_to_node(cpu));
1523 if (!base)
1524 return -ENOMEM;
1525
1526
1527 if (tbase_get_deferrable(base)) {
1528 WARN_ON(1);
1529 kfree(base);
1530 return -ENOMEM;
1531 }
1532 per_cpu(tvec_bases, cpu) = base;
1533 } else {
1534
1535
1536
1537
1538
1539
1540 boot_done = 1;
1541 base = &boot_tvec_bases;
1542 }
1543 spin_lock_init(&base->lock);
1544 tvec_base_done[cpu] = 1;
1545 } else {
1546 base = per_cpu(tvec_bases, cpu);
1547 }
1548
1549
1550 for (j = 0; j < TVN_SIZE; j++) {
1551 INIT_LIST_HEAD(base->tv5.vec + j);
1552 INIT_LIST_HEAD(base->tv4.vec + j);
1553 INIT_LIST_HEAD(base->tv3.vec + j);
1554 INIT_LIST_HEAD(base->tv2.vec + j);
1555 }
1556 for (j = 0; j < TVR_SIZE; j++)
1557 INIT_LIST_HEAD(base->tv1.vec + j);
1558
1559 base->timer_jiffies = jiffies;
1560 base->next_timer = base->timer_jiffies;
1561 base->active_timers = 0;
1562 return 0;
1563}
1564
1565#ifdef CONFIG_HOTPLUG_CPU
1566static void migrate_timer_list(struct tvec_base *new_base, struct list_head *head)
1567{
1568 struct timer_list *timer;
1569
1570 while (!list_empty(head)) {
1571 timer = list_first_entry(head, struct timer_list, entry);
1572
1573 detach_timer(timer, false);
1574 timer_set_base(timer, new_base);
1575 internal_add_timer(new_base, timer);
1576 }
1577}
1578
1579static void migrate_timers(int cpu)
1580{
1581 struct tvec_base *old_base;
1582 struct tvec_base *new_base;
1583 int i;
1584
1585 BUG_ON(cpu_online(cpu));
1586 old_base = per_cpu(tvec_bases, cpu);
1587 new_base = get_cpu_var(tvec_bases);
1588
1589
1590
1591
1592 spin_lock_irq(&new_base->lock);
1593 spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
1594
1595 BUG_ON(old_base->running_timer);
1596
1597 for (i = 0; i < TVR_SIZE; i++)
1598 migrate_timer_list(new_base, old_base->tv1.vec + i);
1599 for (i = 0; i < TVN_SIZE; i++) {
1600 migrate_timer_list(new_base, old_base->tv2.vec + i);
1601 migrate_timer_list(new_base, old_base->tv3.vec + i);
1602 migrate_timer_list(new_base, old_base->tv4.vec + i);
1603 migrate_timer_list(new_base, old_base->tv5.vec + i);
1604 }
1605
1606 spin_unlock(&old_base->lock);
1607 spin_unlock_irq(&new_base->lock);
1608 put_cpu_var(tvec_bases);
1609}
1610#endif
1611
1612static int timer_cpu_notify(struct notifier_block *self,
1613 unsigned long action, void *hcpu)
1614{
1615 long cpu = (long)hcpu;
1616 int err;
1617
1618 switch(action) {
1619 case CPU_UP_PREPARE:
1620 case CPU_UP_PREPARE_FROZEN:
1621 err = init_timers_cpu(cpu);
1622 if (err < 0)
1623 return notifier_from_errno(err);
1624 break;
1625#ifdef CONFIG_HOTPLUG_CPU
1626 case CPU_DEAD:
1627 case CPU_DEAD_FROZEN:
1628 migrate_timers(cpu);
1629 break;
1630#endif
1631 default:
1632 break;
1633 }
1634 return NOTIFY_OK;
1635}
1636
1637static struct notifier_block timers_nb = {
1638 .notifier_call = timer_cpu_notify,
1639};
1640
1641
1642void __init init_timers(void)
1643{
1644 int err;
1645
1646
1647 BUILD_BUG_ON(__alignof__(struct tvec_base) & TIMER_FLAG_MASK);
1648
1649 err = timer_cpu_notify(&timers_nb, (unsigned long)CPU_UP_PREPARE,
1650 (void *)(long)smp_processor_id());
1651 init_timer_stats();
1652
1653 BUG_ON(err != NOTIFY_OK);
1654 register_cpu_notifier(&timers_nb);
1655 open_softirq(TIMER_SOFTIRQ, run_timer_softirq);
1656}
1657
1658
1659
1660
1661
1662void msleep(unsigned int msecs)
1663{
1664 unsigned long timeout = msecs_to_jiffies(msecs) + 1;
1665
1666 while (timeout)
1667 timeout = schedule_timeout_uninterruptible(timeout);
1668}
1669
1670EXPORT_SYMBOL(msleep);
1671
1672
1673
1674
1675
1676unsigned long msleep_interruptible(unsigned int msecs)
1677{
1678 unsigned long timeout = msecs_to_jiffies(msecs) + 1;
1679
1680 while (timeout && !signal_pending(current))
1681 timeout = schedule_timeout_interruptible(timeout);
1682 return jiffies_to_msecs(timeout);
1683}
1684
1685EXPORT_SYMBOL(msleep_interruptible);
1686
1687static int __sched do_usleep_range(unsigned long min, unsigned long max)
1688{
1689 ktime_t kmin;
1690 unsigned long delta;
1691
1692 kmin = ktime_set(0, min * NSEC_PER_USEC);
1693 delta = (max - min) * NSEC_PER_USEC;
1694 return schedule_hrtimeout_range(&kmin, delta, HRTIMER_MODE_REL);
1695}
1696
1697
1698
1699
1700
1701
1702void usleep_range(unsigned long min, unsigned long max)
1703{
1704 __set_current_state(TASK_UNINTERRUPTIBLE);
1705 do_usleep_range(min, max);
1706}
1707EXPORT_SYMBOL(usleep_range);
1708