Avoid beyond bounds copy while caching ACL
[zen-stable.git] / arch / s390 / kernel / vtime.c
blobbb48977f54697bdb1d68b34da3cc239947659ea2
1 /*
2 * arch/s390/kernel/vtime.c
3 * Virtual cpu timer based timer functions.
5 * S390 version
6 * Copyright (C) 2004 IBM Deutschland Entwicklung GmbH, IBM Corporation
7 * Author(s): Jan Glauber <jan.glauber@de.ibm.com>
8 */
10 #include <linux/module.h>
11 #include <linux/kernel.h>
12 #include <linux/time.h>
13 #include <linux/delay.h>
14 #include <linux/init.h>
15 #include <linux/smp.h>
16 #include <linux/types.h>
17 #include <linux/timex.h>
18 #include <linux/notifier.h>
19 #include <linux/kernel_stat.h>
20 #include <linux/rcupdate.h>
21 #include <linux/posix-timers.h>
22 #include <linux/cpu.h>
23 #include <linux/kprobes.h>
25 #include <asm/timer.h>
26 #include <asm/irq_regs.h>
27 #include <asm/cputime.h>
28 #include <asm/irq.h>
30 static DEFINE_PER_CPU(struct vtimer_queue, virt_cpu_timer);
32 DEFINE_PER_CPU(struct s390_idle_data, s390_idle);
34 static inline __u64 get_vtimer(void)
36 __u64 timer;
38 asm volatile("STPT %0" : "=m" (timer));
39 return timer;
42 static inline void set_vtimer(__u64 expires)
44 __u64 timer;
46 asm volatile (" STPT %0\n" /* Store current cpu timer value */
47 " SPT %1" /* Set new value immediately afterwards */
48 : "=m" (timer) : "m" (expires) );
49 S390_lowcore.system_timer += S390_lowcore.last_update_timer - timer;
50 S390_lowcore.last_update_timer = expires;
54 * Update process times based on virtual cpu times stored by entry.S
55 * to the lowcore fields user_timer, system_timer & steal_clock.
57 static void do_account_vtime(struct task_struct *tsk, int hardirq_offset)
59 struct thread_info *ti = task_thread_info(tsk);
60 __u64 timer, clock, user, system, steal;
62 timer = S390_lowcore.last_update_timer;
63 clock = S390_lowcore.last_update_clock;
64 asm volatile (" STPT %0\n" /* Store current cpu timer value */
65 " STCK %1" /* Store current tod clock value */
66 : "=m" (S390_lowcore.last_update_timer),
67 "=m" (S390_lowcore.last_update_clock) );
68 S390_lowcore.system_timer += timer - S390_lowcore.last_update_timer;
69 S390_lowcore.steal_timer += S390_lowcore.last_update_clock - clock;
71 user = S390_lowcore.user_timer - ti->user_timer;
72 S390_lowcore.steal_timer -= user;
73 ti->user_timer = S390_lowcore.user_timer;
74 account_user_time(tsk, user, user);
76 system = S390_lowcore.system_timer - ti->system_timer;
77 S390_lowcore.steal_timer -= system;
78 ti->system_timer = S390_lowcore.system_timer;
79 account_system_time(tsk, hardirq_offset, system, system);
81 steal = S390_lowcore.steal_timer;
82 if ((s64) steal > 0) {
83 S390_lowcore.steal_timer = 0;
84 account_steal_time(steal);
88 void account_vtime(struct task_struct *prev, struct task_struct *next)
90 struct thread_info *ti;
92 do_account_vtime(prev, 0);
93 ti = task_thread_info(prev);
94 ti->user_timer = S390_lowcore.user_timer;
95 ti->system_timer = S390_lowcore.system_timer;
96 ti = task_thread_info(next);
97 S390_lowcore.user_timer = ti->user_timer;
98 S390_lowcore.system_timer = ti->system_timer;
101 void account_process_tick(struct task_struct *tsk, int user_tick)
103 do_account_vtime(tsk, HARDIRQ_OFFSET);
107 * Update process times based on virtual cpu times stored by entry.S
108 * to the lowcore fields user_timer, system_timer & steal_clock.
110 void account_system_vtime(struct task_struct *tsk)
112 struct thread_info *ti = task_thread_info(tsk);
113 __u64 timer, system;
115 timer = S390_lowcore.last_update_timer;
116 S390_lowcore.last_update_timer = get_vtimer();
117 S390_lowcore.system_timer += timer - S390_lowcore.last_update_timer;
119 system = S390_lowcore.system_timer - ti->system_timer;
120 S390_lowcore.steal_timer -= system;
121 ti->system_timer = S390_lowcore.system_timer;
122 account_system_time(tsk, 0, system, system);
124 EXPORT_SYMBOL_GPL(account_system_vtime);
126 void __kprobes vtime_start_cpu(__u64 int_clock, __u64 enter_timer)
128 struct s390_idle_data *idle = &__get_cpu_var(s390_idle);
129 struct vtimer_queue *vq = &__get_cpu_var(virt_cpu_timer);
130 __u64 idle_time, expires;
132 if (idle->idle_enter == 0ULL)
133 return;
135 /* Account time spent with enabled wait psw loaded as idle time. */
136 idle_time = int_clock - idle->idle_enter;
137 account_idle_time(idle_time);
138 S390_lowcore.steal_timer +=
139 idle->idle_enter - S390_lowcore.last_update_clock;
140 S390_lowcore.last_update_clock = int_clock;
142 /* Account system time spent going idle. */
143 S390_lowcore.system_timer += S390_lowcore.last_update_timer - vq->idle;
144 S390_lowcore.last_update_timer = enter_timer;
146 /* Restart vtime CPU timer */
147 if (vq->do_spt) {
148 /* Program old expire value but first save progress. */
149 expires = vq->idle - enter_timer;
150 expires += get_vtimer();
151 set_vtimer(expires);
152 } else {
153 /* Don't account the CPU timer delta while the cpu was idle. */
154 vq->elapsed -= vq->idle - enter_timer;
157 idle->sequence++;
158 smp_wmb();
159 idle->idle_time += idle_time;
160 idle->idle_enter = 0ULL;
161 idle->idle_count++;
162 smp_wmb();
163 idle->sequence++;
166 void __kprobes vtime_stop_cpu(void)
168 struct s390_idle_data *idle = &__get_cpu_var(s390_idle);
169 struct vtimer_queue *vq = &__get_cpu_var(virt_cpu_timer);
170 psw_t psw;
172 /* Wait for external, I/O or machine check interrupt. */
173 psw.mask = psw_kernel_bits | PSW_MASK_WAIT |
174 PSW_MASK_DAT | PSW_MASK_IO | PSW_MASK_EXT | PSW_MASK_MCHECK;
176 idle->nohz_delay = 0;
178 /* Check if the CPU timer needs to be reprogrammed. */
179 if (vq->do_spt) {
180 __u64 vmax = VTIMER_MAX_SLICE;
182 * The inline assembly is equivalent to
183 * vq->idle = get_cpu_timer();
184 * set_cpu_timer(VTIMER_MAX_SLICE);
185 * idle->idle_enter = get_clock();
186 * __load_psw_mask(psw_kernel_bits | PSW_MASK_WAIT |
187 * PSW_MASK_DAT | PSW_MASK_IO |
188 * PSW_MASK_EXT | PSW_MASK_MCHECK);
189 * The difference is that the inline assembly makes sure that
190 * the last three instruction are stpt, stck and lpsw in that
191 * order. This is done to increase the precision.
193 asm volatile(
194 #ifndef CONFIG_64BIT
195 " basr 1,0\n"
196 "0: ahi 1,1f-0b\n"
197 " st 1,4(%2)\n"
198 #else /* CONFIG_64BIT */
199 " larl 1,1f\n"
200 " stg 1,8(%2)\n"
201 #endif /* CONFIG_64BIT */
202 " stpt 0(%4)\n"
203 " spt 0(%5)\n"
204 " stck 0(%3)\n"
205 #ifndef CONFIG_64BIT
206 " lpsw 0(%2)\n"
207 #else /* CONFIG_64BIT */
208 " lpswe 0(%2)\n"
209 #endif /* CONFIG_64BIT */
210 "1:"
211 : "=m" (idle->idle_enter), "=m" (vq->idle)
212 : "a" (&psw), "a" (&idle->idle_enter),
213 "a" (&vq->idle), "a" (&vmax), "m" (vmax), "m" (psw)
214 : "memory", "cc", "1");
215 } else {
217 * The inline assembly is equivalent to
218 * vq->idle = get_cpu_timer();
219 * idle->idle_enter = get_clock();
220 * __load_psw_mask(psw_kernel_bits | PSW_MASK_WAIT |
221 * PSW_MASK_DAT | PSW_MASK_IO |
222 * PSW_MASK_EXT | PSW_MASK_MCHECK);
223 * The difference is that the inline assembly makes sure that
224 * the last three instruction are stpt, stck and lpsw in that
225 * order. This is done to increase the precision.
227 asm volatile(
228 #ifndef CONFIG_64BIT
229 " basr 1,0\n"
230 "0: ahi 1,1f-0b\n"
231 " st 1,4(%2)\n"
232 #else /* CONFIG_64BIT */
233 " larl 1,1f\n"
234 " stg 1,8(%2)\n"
235 #endif /* CONFIG_64BIT */
236 " stpt 0(%4)\n"
237 " stck 0(%3)\n"
238 #ifndef CONFIG_64BIT
239 " lpsw 0(%2)\n"
240 #else /* CONFIG_64BIT */
241 " lpswe 0(%2)\n"
242 #endif /* CONFIG_64BIT */
243 "1:"
244 : "=m" (idle->idle_enter), "=m" (vq->idle)
245 : "a" (&psw), "a" (&idle->idle_enter),
246 "a" (&vq->idle), "m" (psw)
247 : "memory", "cc", "1");
251 cputime64_t s390_get_idle_time(int cpu)
253 struct s390_idle_data *idle;
254 unsigned long long now, idle_time, idle_enter;
255 unsigned int sequence;
257 idle = &per_cpu(s390_idle, cpu);
259 now = get_clock();
260 repeat:
261 sequence = idle->sequence;
262 smp_rmb();
263 if (sequence & 1)
264 goto repeat;
265 idle_time = 0;
266 idle_enter = idle->idle_enter;
267 if (idle_enter != 0ULL && idle_enter < now)
268 idle_time = now - idle_enter;
269 smp_rmb();
270 if (idle->sequence != sequence)
271 goto repeat;
272 return idle_time;
276 * Sorted add to a list. List is linear searched until first bigger
277 * element is found.
279 static void list_add_sorted(struct vtimer_list *timer, struct list_head *head)
281 struct vtimer_list *event;
283 list_for_each_entry(event, head, entry) {
284 if (event->expires > timer->expires) {
285 list_add_tail(&timer->entry, &event->entry);
286 return;
289 list_add_tail(&timer->entry, head);
293 * Do the callback functions of expired vtimer events.
294 * Called from within the interrupt handler.
296 static void do_callbacks(struct list_head *cb_list)
298 struct vtimer_queue *vq;
299 struct vtimer_list *event, *tmp;
301 if (list_empty(cb_list))
302 return;
304 vq = &__get_cpu_var(virt_cpu_timer);
306 list_for_each_entry_safe(event, tmp, cb_list, entry) {
307 list_del_init(&event->entry);
308 (event->function)(event->data);
309 if (event->interval) {
310 /* Recharge interval timer */
311 event->expires = event->interval + vq->elapsed;
312 spin_lock(&vq->lock);
313 list_add_sorted(event, &vq->list);
314 spin_unlock(&vq->lock);
320 * Handler for the virtual CPU timer.
322 static void do_cpu_timer_interrupt(unsigned int ext_int_code,
323 unsigned int param32, unsigned long param64)
325 struct vtimer_queue *vq;
326 struct vtimer_list *event, *tmp;
327 struct list_head cb_list; /* the callback queue */
328 __u64 elapsed, next;
330 kstat_cpu(smp_processor_id()).irqs[EXTINT_TMR]++;
331 INIT_LIST_HEAD(&cb_list);
332 vq = &__get_cpu_var(virt_cpu_timer);
334 /* walk timer list, fire all expired events */
335 spin_lock(&vq->lock);
337 elapsed = vq->elapsed + (vq->timer - S390_lowcore.async_enter_timer);
338 BUG_ON((s64) elapsed < 0);
339 vq->elapsed = 0;
340 list_for_each_entry_safe(event, tmp, &vq->list, entry) {
341 if (event->expires < elapsed)
342 /* move expired timer to the callback queue */
343 list_move_tail(&event->entry, &cb_list);
344 else
345 event->expires -= elapsed;
347 spin_unlock(&vq->lock);
349 vq->do_spt = list_empty(&cb_list);
350 do_callbacks(&cb_list);
352 /* next event is first in list */
353 next = VTIMER_MAX_SLICE;
354 spin_lock(&vq->lock);
355 if (!list_empty(&vq->list)) {
356 event = list_first_entry(&vq->list, struct vtimer_list, entry);
357 next = event->expires;
358 } else
359 vq->do_spt = 0;
360 spin_unlock(&vq->lock);
362 * To improve precision add the time spent by the
363 * interrupt handler to the elapsed time.
364 * Note: CPU timer counts down and we got an interrupt,
365 * the current content is negative
367 elapsed = S390_lowcore.async_enter_timer - get_vtimer();
368 set_vtimer(next - elapsed);
369 vq->timer = next - elapsed;
370 vq->elapsed = elapsed;
373 void init_virt_timer(struct vtimer_list *timer)
375 timer->function = NULL;
376 INIT_LIST_HEAD(&timer->entry);
378 EXPORT_SYMBOL(init_virt_timer);
380 static inline int vtimer_pending(struct vtimer_list *timer)
382 return (!list_empty(&timer->entry));
386 * this function should only run on the specified CPU
388 static void internal_add_vtimer(struct vtimer_list *timer)
390 struct vtimer_queue *vq;
391 unsigned long flags;
392 __u64 left, expires;
394 vq = &per_cpu(virt_cpu_timer, timer->cpu);
395 spin_lock_irqsave(&vq->lock, flags);
397 BUG_ON(timer->cpu != smp_processor_id());
399 if (list_empty(&vq->list)) {
400 /* First timer on this cpu, just program it. */
401 list_add(&timer->entry, &vq->list);
402 set_vtimer(timer->expires);
403 vq->timer = timer->expires;
404 vq->elapsed = 0;
405 } else {
406 /* Check progress of old timers. */
407 expires = timer->expires;
408 left = get_vtimer();
409 if (likely((s64) expires < (s64) left)) {
410 /* The new timer expires before the current timer. */
411 set_vtimer(expires);
412 vq->elapsed += vq->timer - left;
413 vq->timer = expires;
414 } else {
415 vq->elapsed += vq->timer - left;
416 vq->timer = left;
418 /* Insert new timer into per cpu list. */
419 timer->expires += vq->elapsed;
420 list_add_sorted(timer, &vq->list);
423 spin_unlock_irqrestore(&vq->lock, flags);
424 /* release CPU acquired in prepare_vtimer or mod_virt_timer() */
425 put_cpu();
428 static inline void prepare_vtimer(struct vtimer_list *timer)
430 BUG_ON(!timer->function);
431 BUG_ON(!timer->expires || timer->expires > VTIMER_MAX_SLICE);
432 BUG_ON(vtimer_pending(timer));
433 timer->cpu = get_cpu();
437 * add_virt_timer - add an oneshot virtual CPU timer
439 void add_virt_timer(void *new)
441 struct vtimer_list *timer;
443 timer = (struct vtimer_list *)new;
444 prepare_vtimer(timer);
445 timer->interval = 0;
446 internal_add_vtimer(timer);
448 EXPORT_SYMBOL(add_virt_timer);
451 * add_virt_timer_int - add an interval virtual CPU timer
453 void add_virt_timer_periodic(void *new)
455 struct vtimer_list *timer;
457 timer = (struct vtimer_list *)new;
458 prepare_vtimer(timer);
459 timer->interval = timer->expires;
460 internal_add_vtimer(timer);
462 EXPORT_SYMBOL(add_virt_timer_periodic);
464 static int __mod_vtimer(struct vtimer_list *timer, __u64 expires, int periodic)
466 struct vtimer_queue *vq;
467 unsigned long flags;
468 int cpu;
470 BUG_ON(!timer->function);
471 BUG_ON(!expires || expires > VTIMER_MAX_SLICE);
473 if (timer->expires == expires && vtimer_pending(timer))
474 return 1;
476 cpu = get_cpu();
477 vq = &per_cpu(virt_cpu_timer, cpu);
479 /* disable interrupts before test if timer is pending */
480 spin_lock_irqsave(&vq->lock, flags);
482 /* if timer isn't pending add it on the current CPU */
483 if (!vtimer_pending(timer)) {
484 spin_unlock_irqrestore(&vq->lock, flags);
486 if (periodic)
487 timer->interval = expires;
488 else
489 timer->interval = 0;
490 timer->expires = expires;
491 timer->cpu = cpu;
492 internal_add_vtimer(timer);
493 return 0;
496 /* check if we run on the right CPU */
497 BUG_ON(timer->cpu != cpu);
499 list_del_init(&timer->entry);
500 timer->expires = expires;
501 if (periodic)
502 timer->interval = expires;
504 /* the timer can't expire anymore so we can release the lock */
505 spin_unlock_irqrestore(&vq->lock, flags);
506 internal_add_vtimer(timer);
507 return 1;
511 * If we change a pending timer the function must be called on the CPU
512 * where the timer is running on.
514 * returns whether it has modified a pending timer (1) or not (0)
516 int mod_virt_timer(struct vtimer_list *timer, __u64 expires)
518 return __mod_vtimer(timer, expires, 0);
520 EXPORT_SYMBOL(mod_virt_timer);
523 * If we change a pending timer the function must be called on the CPU
524 * where the timer is running on.
526 * returns whether it has modified a pending timer (1) or not (0)
528 int mod_virt_timer_periodic(struct vtimer_list *timer, __u64 expires)
530 return __mod_vtimer(timer, expires, 1);
532 EXPORT_SYMBOL(mod_virt_timer_periodic);
535 * delete a virtual timer
537 * returns whether the deleted timer was pending (1) or not (0)
539 int del_virt_timer(struct vtimer_list *timer)
541 unsigned long flags;
542 struct vtimer_queue *vq;
544 /* check if timer is pending */
545 if (!vtimer_pending(timer))
546 return 0;
548 vq = &per_cpu(virt_cpu_timer, timer->cpu);
549 spin_lock_irqsave(&vq->lock, flags);
551 /* we don't interrupt a running timer, just let it expire! */
552 list_del_init(&timer->entry);
554 spin_unlock_irqrestore(&vq->lock, flags);
555 return 1;
557 EXPORT_SYMBOL(del_virt_timer);
560 * Start the virtual CPU timer on the current CPU.
562 void init_cpu_vtimer(void)
564 struct vtimer_queue *vq;
566 /* initialize per cpu vtimer structure */
567 vq = &__get_cpu_var(virt_cpu_timer);
568 INIT_LIST_HEAD(&vq->list);
569 spin_lock_init(&vq->lock);
571 /* enable cpu timer interrupts */
572 __ctl_set_bit(0,10);
575 static int __cpuinit s390_nohz_notify(struct notifier_block *self,
576 unsigned long action, void *hcpu)
578 struct s390_idle_data *idle;
579 long cpu = (long) hcpu;
581 idle = &per_cpu(s390_idle, cpu);
582 switch (action) {
583 case CPU_DYING:
584 case CPU_DYING_FROZEN:
585 idle->nohz_delay = 0;
586 default:
587 break;
589 return NOTIFY_OK;
592 void __init vtime_init(void)
594 /* request the cpu timer external interrupt */
595 if (register_external_interrupt(0x1005, do_cpu_timer_interrupt))
596 panic("Couldn't request external interrupt 0x1005");
598 /* Enable cpu timer interrupts on the boot cpu. */
599 init_cpu_vtimer();
600 cpu_notifier(s390_nohz_notify, 0);