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