Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * Implement CPU time clocks for the POSIX clock interface. | |
3 | */ | |
4 | ||
5 | #include <linux/sched.h> | |
6 | #include <linux/posix-timers.h> | |
1da177e4 | 7 | #include <linux/errno.h> |
f8bd2258 RZ |
8 | #include <linux/math64.h> |
9 | #include <asm/uaccess.h> | |
bb34d92f | 10 | #include <linux/kernel_stat.h> |
3f0a525e | 11 | #include <trace/events/timer.h> |
61337054 | 12 | #include <linux/random.h> |
a8572160 FW |
13 | #include <linux/tick.h> |
14 | #include <linux/workqueue.h> | |
1da177e4 | 15 | |
f06febc9 | 16 | /* |
f55db609 SG |
17 | * Called after updating RLIMIT_CPU to run cpu timer and update |
18 | * tsk->signal->cputime_expires expiration cache if necessary. Needs | |
19 | * siglock protection since other code may update expiration cache as | |
20 | * well. | |
f06febc9 | 21 | */ |
5ab46b34 | 22 | void update_rlimit_cpu(struct task_struct *task, unsigned long rlim_new) |
f06febc9 | 23 | { |
42c4ab41 | 24 | cputime_t cputime = secs_to_cputime(rlim_new); |
f06febc9 | 25 | |
5ab46b34 JS |
26 | spin_lock_irq(&task->sighand->siglock); |
27 | set_process_cpu_timer(task, CPUCLOCK_PROF, &cputime, NULL); | |
28 | spin_unlock_irq(&task->sighand->siglock); | |
f06febc9 FM |
29 | } |
30 | ||
a924b04d | 31 | static int check_clock(const clockid_t which_clock) |
1da177e4 LT |
32 | { |
33 | int error = 0; | |
34 | struct task_struct *p; | |
35 | const pid_t pid = CPUCLOCK_PID(which_clock); | |
36 | ||
37 | if (CPUCLOCK_WHICH(which_clock) >= CPUCLOCK_MAX) | |
38 | return -EINVAL; | |
39 | ||
40 | if (pid == 0) | |
41 | return 0; | |
42 | ||
c0deae8c | 43 | rcu_read_lock(); |
8dc86af0 | 44 | p = find_task_by_vpid(pid); |
bac0abd6 | 45 | if (!p || !(CPUCLOCK_PERTHREAD(which_clock) ? |
c0deae8c | 46 | same_thread_group(p, current) : has_group_leader_pid(p))) { |
1da177e4 LT |
47 | error = -EINVAL; |
48 | } | |
c0deae8c | 49 | rcu_read_unlock(); |
1da177e4 LT |
50 | |
51 | return error; | |
52 | } | |
53 | ||
54 | static inline union cpu_time_count | |
a924b04d | 55 | timespec_to_sample(const clockid_t which_clock, const struct timespec *tp) |
1da177e4 LT |
56 | { |
57 | union cpu_time_count ret; | |
58 | ret.sched = 0; /* high half always zero when .cpu used */ | |
59 | if (CPUCLOCK_WHICH(which_clock) == CPUCLOCK_SCHED) { | |
ee500f27 | 60 | ret.sched = (unsigned long long)tp->tv_sec * NSEC_PER_SEC + tp->tv_nsec; |
1da177e4 LT |
61 | } else { |
62 | ret.cpu = timespec_to_cputime(tp); | |
63 | } | |
64 | return ret; | |
65 | } | |
66 | ||
a924b04d | 67 | static void sample_to_timespec(const clockid_t which_clock, |
1da177e4 LT |
68 | union cpu_time_count cpu, |
69 | struct timespec *tp) | |
70 | { | |
f8bd2258 RZ |
71 | if (CPUCLOCK_WHICH(which_clock) == CPUCLOCK_SCHED) |
72 | *tp = ns_to_timespec(cpu.sched); | |
73 | else | |
1da177e4 | 74 | cputime_to_timespec(cpu.cpu, tp); |
1da177e4 LT |
75 | } |
76 | ||
a924b04d | 77 | static inline int cpu_time_before(const clockid_t which_clock, |
1da177e4 LT |
78 | union cpu_time_count now, |
79 | union cpu_time_count then) | |
80 | { | |
81 | if (CPUCLOCK_WHICH(which_clock) == CPUCLOCK_SCHED) { | |
82 | return now.sched < then.sched; | |
83 | } else { | |
64861634 | 84 | return now.cpu < then.cpu; |
1da177e4 LT |
85 | } |
86 | } | |
a924b04d | 87 | static inline void cpu_time_add(const clockid_t which_clock, |
1da177e4 LT |
88 | union cpu_time_count *acc, |
89 | union cpu_time_count val) | |
90 | { | |
91 | if (CPUCLOCK_WHICH(which_clock) == CPUCLOCK_SCHED) { | |
92 | acc->sched += val.sched; | |
93 | } else { | |
64861634 | 94 | acc->cpu += val.cpu; |
1da177e4 LT |
95 | } |
96 | } | |
a924b04d | 97 | static inline union cpu_time_count cpu_time_sub(const clockid_t which_clock, |
1da177e4 LT |
98 | union cpu_time_count a, |
99 | union cpu_time_count b) | |
100 | { | |
101 | if (CPUCLOCK_WHICH(which_clock) == CPUCLOCK_SCHED) { | |
102 | a.sched -= b.sched; | |
103 | } else { | |
64861634 | 104 | a.cpu -= b.cpu; |
1da177e4 LT |
105 | } |
106 | return a; | |
107 | } | |
108 | ||
109 | /* | |
110 | * Update expiry time from increment, and increase overrun count, | |
111 | * given the current clock sample. | |
112 | */ | |
7a4ed937 | 113 | static void bump_cpu_timer(struct k_itimer *timer, |
1da177e4 LT |
114 | union cpu_time_count now) |
115 | { | |
116 | int i; | |
117 | ||
118 | if (timer->it.cpu.incr.sched == 0) | |
119 | return; | |
120 | ||
121 | if (CPUCLOCK_WHICH(timer->it_clock) == CPUCLOCK_SCHED) { | |
122 | unsigned long long delta, incr; | |
123 | ||
124 | if (now.sched < timer->it.cpu.expires.sched) | |
125 | return; | |
126 | incr = timer->it.cpu.incr.sched; | |
127 | delta = now.sched + incr - timer->it.cpu.expires.sched; | |
128 | /* Don't use (incr*2 < delta), incr*2 might overflow. */ | |
129 | for (i = 0; incr < delta - incr; i++) | |
130 | incr = incr << 1; | |
131 | for (; i >= 0; incr >>= 1, i--) { | |
7a4ed937 | 132 | if (delta < incr) |
1da177e4 LT |
133 | continue; |
134 | timer->it.cpu.expires.sched += incr; | |
135 | timer->it_overrun += 1 << i; | |
136 | delta -= incr; | |
137 | } | |
138 | } else { | |
139 | cputime_t delta, incr; | |
140 | ||
64861634 | 141 | if (now.cpu < timer->it.cpu.expires.cpu) |
1da177e4 LT |
142 | return; |
143 | incr = timer->it.cpu.incr.cpu; | |
64861634 | 144 | delta = now.cpu + incr - timer->it.cpu.expires.cpu; |
1da177e4 | 145 | /* Don't use (incr*2 < delta), incr*2 might overflow. */ |
64861634 MS |
146 | for (i = 0; incr < delta - incr; i++) |
147 | incr += incr; | |
148 | for (; i >= 0; incr = incr >> 1, i--) { | |
149 | if (delta < incr) | |
1da177e4 | 150 | continue; |
64861634 | 151 | timer->it.cpu.expires.cpu += incr; |
1da177e4 | 152 | timer->it_overrun += 1 << i; |
64861634 | 153 | delta -= incr; |
1da177e4 LT |
154 | } |
155 | } | |
156 | } | |
157 | ||
158 | static inline cputime_t prof_ticks(struct task_struct *p) | |
159 | { | |
6fac4829 FW |
160 | cputime_t utime, stime; |
161 | ||
162 | task_cputime(p, &utime, &stime); | |
163 | ||
164 | return utime + stime; | |
1da177e4 LT |
165 | } |
166 | static inline cputime_t virt_ticks(struct task_struct *p) | |
167 | { | |
6fac4829 FW |
168 | cputime_t utime; |
169 | ||
170 | task_cputime(p, &utime, NULL); | |
171 | ||
172 | return utime; | |
1da177e4 | 173 | } |
1da177e4 | 174 | |
bc2c8ea4 TG |
175 | static int |
176 | posix_cpu_clock_getres(const clockid_t which_clock, struct timespec *tp) | |
1da177e4 LT |
177 | { |
178 | int error = check_clock(which_clock); | |
179 | if (!error) { | |
180 | tp->tv_sec = 0; | |
181 | tp->tv_nsec = ((NSEC_PER_SEC + HZ - 1) / HZ); | |
182 | if (CPUCLOCK_WHICH(which_clock) == CPUCLOCK_SCHED) { | |
183 | /* | |
184 | * If sched_clock is using a cycle counter, we | |
185 | * don't have any idea of its true resolution | |
186 | * exported, but it is much more than 1s/HZ. | |
187 | */ | |
188 | tp->tv_nsec = 1; | |
189 | } | |
190 | } | |
191 | return error; | |
192 | } | |
193 | ||
bc2c8ea4 TG |
194 | static int |
195 | posix_cpu_clock_set(const clockid_t which_clock, const struct timespec *tp) | |
1da177e4 LT |
196 | { |
197 | /* | |
198 | * You can never reset a CPU clock, but we check for other errors | |
199 | * in the call before failing with EPERM. | |
200 | */ | |
201 | int error = check_clock(which_clock); | |
202 | if (error == 0) { | |
203 | error = -EPERM; | |
204 | } | |
205 | return error; | |
206 | } | |
207 | ||
208 | ||
209 | /* | |
210 | * Sample a per-thread clock for the given task. | |
211 | */ | |
a924b04d | 212 | static int cpu_clock_sample(const clockid_t which_clock, struct task_struct *p, |
1da177e4 LT |
213 | union cpu_time_count *cpu) |
214 | { | |
215 | switch (CPUCLOCK_WHICH(which_clock)) { | |
216 | default: | |
217 | return -EINVAL; | |
218 | case CPUCLOCK_PROF: | |
219 | cpu->cpu = prof_ticks(p); | |
220 | break; | |
221 | case CPUCLOCK_VIRT: | |
222 | cpu->cpu = virt_ticks(p); | |
223 | break; | |
224 | case CPUCLOCK_SCHED: | |
c5f8d995 | 225 | cpu->sched = task_sched_runtime(p); |
1da177e4 LT |
226 | break; |
227 | } | |
228 | return 0; | |
229 | } | |
230 | ||
4da94d49 PZ |
231 | static void update_gt_cputime(struct task_cputime *a, struct task_cputime *b) |
232 | { | |
64861634 | 233 | if (b->utime > a->utime) |
4da94d49 PZ |
234 | a->utime = b->utime; |
235 | ||
64861634 | 236 | if (b->stime > a->stime) |
4da94d49 PZ |
237 | a->stime = b->stime; |
238 | ||
239 | if (b->sum_exec_runtime > a->sum_exec_runtime) | |
240 | a->sum_exec_runtime = b->sum_exec_runtime; | |
241 | } | |
242 | ||
243 | void thread_group_cputimer(struct task_struct *tsk, struct task_cputime *times) | |
244 | { | |
245 | struct thread_group_cputimer *cputimer = &tsk->signal->cputimer; | |
246 | struct task_cputime sum; | |
247 | unsigned long flags; | |
248 | ||
4da94d49 | 249 | if (!cputimer->running) { |
4da94d49 PZ |
250 | /* |
251 | * The POSIX timer interface allows for absolute time expiry | |
252 | * values through the TIMER_ABSTIME flag, therefore we have | |
253 | * to synchronize the timer to the clock every time we start | |
254 | * it. | |
255 | */ | |
256 | thread_group_cputime(tsk, &sum); | |
3cfef952 | 257 | raw_spin_lock_irqsave(&cputimer->lock, flags); |
bcd5cff7 | 258 | cputimer->running = 1; |
4da94d49 | 259 | update_gt_cputime(&cputimer->cputime, &sum); |
bcd5cff7 | 260 | } else |
3cfef952 | 261 | raw_spin_lock_irqsave(&cputimer->lock, flags); |
4da94d49 | 262 | *times = cputimer->cputime; |
ee30a7b2 | 263 | raw_spin_unlock_irqrestore(&cputimer->lock, flags); |
4da94d49 PZ |
264 | } |
265 | ||
1da177e4 LT |
266 | /* |
267 | * Sample a process (thread group) clock for the given group_leader task. | |
268 | * Must be called with tasklist_lock held for reading. | |
1da177e4 | 269 | */ |
bb34d92f FM |
270 | static int cpu_clock_sample_group(const clockid_t which_clock, |
271 | struct task_struct *p, | |
272 | union cpu_time_count *cpu) | |
1da177e4 | 273 | { |
f06febc9 FM |
274 | struct task_cputime cputime; |
275 | ||
eccdaeaf | 276 | switch (CPUCLOCK_WHICH(which_clock)) { |
1da177e4 LT |
277 | default: |
278 | return -EINVAL; | |
279 | case CPUCLOCK_PROF: | |
c5f8d995 | 280 | thread_group_cputime(p, &cputime); |
64861634 | 281 | cpu->cpu = cputime.utime + cputime.stime; |
1da177e4 LT |
282 | break; |
283 | case CPUCLOCK_VIRT: | |
c5f8d995 | 284 | thread_group_cputime(p, &cputime); |
f06febc9 | 285 | cpu->cpu = cputime.utime; |
1da177e4 LT |
286 | break; |
287 | case CPUCLOCK_SCHED: | |
d670ec13 PZ |
288 | thread_group_cputime(p, &cputime); |
289 | cpu->sched = cputime.sum_exec_runtime; | |
1da177e4 LT |
290 | break; |
291 | } | |
292 | return 0; | |
293 | } | |
294 | ||
1da177e4 | 295 | |
bc2c8ea4 | 296 | static int posix_cpu_clock_get(const clockid_t which_clock, struct timespec *tp) |
1da177e4 LT |
297 | { |
298 | const pid_t pid = CPUCLOCK_PID(which_clock); | |
299 | int error = -EINVAL; | |
300 | union cpu_time_count rtn; | |
301 | ||
302 | if (pid == 0) { | |
303 | /* | |
304 | * Special case constant value for our own clocks. | |
305 | * We don't have to do any lookup to find ourselves. | |
306 | */ | |
307 | if (CPUCLOCK_PERTHREAD(which_clock)) { | |
308 | /* | |
309 | * Sampling just ourselves we can do with no locking. | |
310 | */ | |
311 | error = cpu_clock_sample(which_clock, | |
312 | current, &rtn); | |
313 | } else { | |
314 | read_lock(&tasklist_lock); | |
315 | error = cpu_clock_sample_group(which_clock, | |
316 | current, &rtn); | |
317 | read_unlock(&tasklist_lock); | |
318 | } | |
319 | } else { | |
320 | /* | |
321 | * Find the given PID, and validate that the caller | |
322 | * should be able to see it. | |
323 | */ | |
324 | struct task_struct *p; | |
1f2ea083 | 325 | rcu_read_lock(); |
8dc86af0 | 326 | p = find_task_by_vpid(pid); |
1da177e4 LT |
327 | if (p) { |
328 | if (CPUCLOCK_PERTHREAD(which_clock)) { | |
bac0abd6 | 329 | if (same_thread_group(p, current)) { |
1da177e4 LT |
330 | error = cpu_clock_sample(which_clock, |
331 | p, &rtn); | |
332 | } | |
1f2ea083 PM |
333 | } else { |
334 | read_lock(&tasklist_lock); | |
d30fda35 | 335 | if (thread_group_leader(p) && p->sighand) { |
1f2ea083 PM |
336 | error = |
337 | cpu_clock_sample_group(which_clock, | |
338 | p, &rtn); | |
339 | } | |
340 | read_unlock(&tasklist_lock); | |
1da177e4 LT |
341 | } |
342 | } | |
1f2ea083 | 343 | rcu_read_unlock(); |
1da177e4 LT |
344 | } |
345 | ||
346 | if (error) | |
347 | return error; | |
348 | sample_to_timespec(which_clock, rtn, tp); | |
349 | return 0; | |
350 | } | |
351 | ||
352 | ||
353 | /* | |
354 | * Validate the clockid_t for a new CPU-clock timer, and initialize the timer. | |
ba5ea951 SG |
355 | * This is called from sys_timer_create() and do_cpu_nanosleep() with the |
356 | * new timer already all-zeros initialized. | |
1da177e4 | 357 | */ |
bc2c8ea4 | 358 | static int posix_cpu_timer_create(struct k_itimer *new_timer) |
1da177e4 LT |
359 | { |
360 | int ret = 0; | |
361 | const pid_t pid = CPUCLOCK_PID(new_timer->it_clock); | |
362 | struct task_struct *p; | |
363 | ||
364 | if (CPUCLOCK_WHICH(new_timer->it_clock) >= CPUCLOCK_MAX) | |
365 | return -EINVAL; | |
366 | ||
367 | INIT_LIST_HEAD(&new_timer->it.cpu.entry); | |
1da177e4 | 368 | |
c0deae8c | 369 | rcu_read_lock(); |
1da177e4 LT |
370 | if (CPUCLOCK_PERTHREAD(new_timer->it_clock)) { |
371 | if (pid == 0) { | |
372 | p = current; | |
373 | } else { | |
8dc86af0 | 374 | p = find_task_by_vpid(pid); |
bac0abd6 | 375 | if (p && !same_thread_group(p, current)) |
1da177e4 LT |
376 | p = NULL; |
377 | } | |
378 | } else { | |
379 | if (pid == 0) { | |
380 | p = current->group_leader; | |
381 | } else { | |
8dc86af0 | 382 | p = find_task_by_vpid(pid); |
c0deae8c | 383 | if (p && !has_group_leader_pid(p)) |
1da177e4 LT |
384 | p = NULL; |
385 | } | |
386 | } | |
387 | new_timer->it.cpu.task = p; | |
388 | if (p) { | |
389 | get_task_struct(p); | |
390 | } else { | |
391 | ret = -EINVAL; | |
392 | } | |
c0deae8c | 393 | rcu_read_unlock(); |
1da177e4 LT |
394 | |
395 | return ret; | |
396 | } | |
397 | ||
398 | /* | |
399 | * Clean up a CPU-clock timer that is about to be destroyed. | |
400 | * This is called from timer deletion with the timer already locked. | |
401 | * If we return TIMER_RETRY, it's necessary to release the timer's lock | |
402 | * and try again. (This happens when the timer is in the middle of firing.) | |
403 | */ | |
bc2c8ea4 | 404 | static int posix_cpu_timer_del(struct k_itimer *timer) |
1da177e4 LT |
405 | { |
406 | struct task_struct *p = timer->it.cpu.task; | |
108150ea | 407 | int ret = 0; |
1da177e4 | 408 | |
108150ea | 409 | if (likely(p != NULL)) { |
9465bee8 | 410 | read_lock(&tasklist_lock); |
d30fda35 | 411 | if (unlikely(p->sighand == NULL)) { |
9465bee8 LT |
412 | /* |
413 | * We raced with the reaping of the task. | |
414 | * The deletion should have cleared us off the list. | |
415 | */ | |
416 | BUG_ON(!list_empty(&timer->it.cpu.entry)); | |
417 | } else { | |
9465bee8 | 418 | spin_lock(&p->sighand->siglock); |
108150ea ON |
419 | if (timer->it.cpu.firing) |
420 | ret = TIMER_RETRY; | |
421 | else | |
422 | list_del(&timer->it.cpu.entry); | |
9465bee8 LT |
423 | spin_unlock(&p->sighand->siglock); |
424 | } | |
425 | read_unlock(&tasklist_lock); | |
108150ea ON |
426 | |
427 | if (!ret) | |
428 | put_task_struct(p); | |
1da177e4 | 429 | } |
1da177e4 | 430 | |
108150ea | 431 | return ret; |
1da177e4 LT |
432 | } |
433 | ||
434 | /* | |
435 | * Clean out CPU timers still ticking when a thread exited. The task | |
436 | * pointer is cleared, and the expiry time is replaced with the residual | |
437 | * time for later timer_gettime calls to return. | |
438 | * This must be called with the siglock held. | |
439 | */ | |
440 | static void cleanup_timers(struct list_head *head, | |
441 | cputime_t utime, cputime_t stime, | |
41b86e9c | 442 | unsigned long long sum_exec_runtime) |
1da177e4 LT |
443 | { |
444 | struct cpu_timer_list *timer, *next; | |
64861634 | 445 | cputime_t ptime = utime + stime; |
1da177e4 LT |
446 | |
447 | list_for_each_entry_safe(timer, next, head, entry) { | |
1da177e4 | 448 | list_del_init(&timer->entry); |
64861634 MS |
449 | if (timer->expires.cpu < ptime) { |
450 | timer->expires.cpu = 0; | |
1da177e4 | 451 | } else { |
64861634 | 452 | timer->expires.cpu -= ptime; |
1da177e4 LT |
453 | } |
454 | } | |
455 | ||
456 | ++head; | |
457 | list_for_each_entry_safe(timer, next, head, entry) { | |
1da177e4 | 458 | list_del_init(&timer->entry); |
64861634 MS |
459 | if (timer->expires.cpu < utime) { |
460 | timer->expires.cpu = 0; | |
1da177e4 | 461 | } else { |
64861634 | 462 | timer->expires.cpu -= utime; |
1da177e4 LT |
463 | } |
464 | } | |
465 | ||
466 | ++head; | |
467 | list_for_each_entry_safe(timer, next, head, entry) { | |
1da177e4 | 468 | list_del_init(&timer->entry); |
41b86e9c | 469 | if (timer->expires.sched < sum_exec_runtime) { |
1da177e4 LT |
470 | timer->expires.sched = 0; |
471 | } else { | |
41b86e9c | 472 | timer->expires.sched -= sum_exec_runtime; |
1da177e4 LT |
473 | } |
474 | } | |
475 | } | |
476 | ||
477 | /* | |
478 | * These are both called with the siglock held, when the current thread | |
479 | * is being reaped. When the final (leader) thread in the group is reaped, | |
480 | * posix_cpu_timers_exit_group will be called after posix_cpu_timers_exit. | |
481 | */ | |
482 | void posix_cpu_timers_exit(struct task_struct *tsk) | |
483 | { | |
6fac4829 FW |
484 | cputime_t utime, stime; |
485 | ||
61337054 NK |
486 | add_device_randomness((const void*) &tsk->se.sum_exec_runtime, |
487 | sizeof(unsigned long long)); | |
6fac4829 | 488 | task_cputime(tsk, &utime, &stime); |
1da177e4 | 489 | cleanup_timers(tsk->cpu_timers, |
6fac4829 | 490 | utime, stime, tsk->se.sum_exec_runtime); |
1da177e4 LT |
491 | |
492 | } | |
493 | void posix_cpu_timers_exit_group(struct task_struct *tsk) | |
494 | { | |
17d42c1c | 495 | struct signal_struct *const sig = tsk->signal; |
6fac4829 | 496 | cputime_t utime, stime; |
ca531a0a | 497 | |
6fac4829 | 498 | task_cputime(tsk, &utime, &stime); |
f06febc9 | 499 | cleanup_timers(tsk->signal->cpu_timers, |
6fac4829 | 500 | utime + sig->utime, stime + sig->stime, |
17d42c1c | 501 | tsk->se.sum_exec_runtime + sig->sum_sched_runtime); |
1da177e4 LT |
502 | } |
503 | ||
504 | static void clear_dead_task(struct k_itimer *timer, union cpu_time_count now) | |
505 | { | |
506 | /* | |
507 | * That's all for this thread or process. | |
508 | * We leave our residual in expires to be reported. | |
509 | */ | |
510 | put_task_struct(timer->it.cpu.task); | |
511 | timer->it.cpu.task = NULL; | |
512 | timer->it.cpu.expires = cpu_time_sub(timer->it_clock, | |
513 | timer->it.cpu.expires, | |
514 | now); | |
515 | } | |
516 | ||
d1e3b6d1 SG |
517 | static inline int expires_gt(cputime_t expires, cputime_t new_exp) |
518 | { | |
64861634 | 519 | return expires == 0 || expires > new_exp; |
d1e3b6d1 SG |
520 | } |
521 | ||
1da177e4 LT |
522 | /* |
523 | * Insert the timer on the appropriate list before any timers that | |
524 | * expire later. This must be called with the tasklist_lock held | |
c2873937 | 525 | * for reading, interrupts disabled and p->sighand->siglock taken. |
1da177e4 | 526 | */ |
5eb9aa64 | 527 | static void arm_timer(struct k_itimer *timer) |
1da177e4 LT |
528 | { |
529 | struct task_struct *p = timer->it.cpu.task; | |
530 | struct list_head *head, *listpos; | |
5eb9aa64 | 531 | struct task_cputime *cputime_expires; |
1da177e4 LT |
532 | struct cpu_timer_list *const nt = &timer->it.cpu; |
533 | struct cpu_timer_list *next; | |
1da177e4 | 534 | |
5eb9aa64 SG |
535 | if (CPUCLOCK_PERTHREAD(timer->it_clock)) { |
536 | head = p->cpu_timers; | |
537 | cputime_expires = &p->cputime_expires; | |
538 | } else { | |
539 | head = p->signal->cpu_timers; | |
540 | cputime_expires = &p->signal->cputime_expires; | |
541 | } | |
1da177e4 LT |
542 | head += CPUCLOCK_WHICH(timer->it_clock); |
543 | ||
1da177e4 | 544 | listpos = head; |
5eb9aa64 SG |
545 | list_for_each_entry(next, head, entry) { |
546 | if (cpu_time_before(timer->it_clock, nt->expires, next->expires)) | |
547 | break; | |
548 | listpos = &next->entry; | |
1da177e4 LT |
549 | } |
550 | list_add(&nt->entry, listpos); | |
551 | ||
552 | if (listpos == head) { | |
5eb9aa64 SG |
553 | union cpu_time_count *exp = &nt->expires; |
554 | ||
1da177e4 | 555 | /* |
5eb9aa64 SG |
556 | * We are the new earliest-expiring POSIX 1.b timer, hence |
557 | * need to update expiration cache. Take into account that | |
558 | * for process timers we share expiration cache with itimers | |
559 | * and RLIMIT_CPU and for thread timers with RLIMIT_RTTIME. | |
1da177e4 LT |
560 | */ |
561 | ||
5eb9aa64 SG |
562 | switch (CPUCLOCK_WHICH(timer->it_clock)) { |
563 | case CPUCLOCK_PROF: | |
564 | if (expires_gt(cputime_expires->prof_exp, exp->cpu)) | |
565 | cputime_expires->prof_exp = exp->cpu; | |
566 | break; | |
567 | case CPUCLOCK_VIRT: | |
568 | if (expires_gt(cputime_expires->virt_exp, exp->cpu)) | |
569 | cputime_expires->virt_exp = exp->cpu; | |
570 | break; | |
571 | case CPUCLOCK_SCHED: | |
572 | if (cputime_expires->sched_exp == 0 || | |
573 | cputime_expires->sched_exp > exp->sched) | |
574 | cputime_expires->sched_exp = exp->sched; | |
575 | break; | |
1da177e4 LT |
576 | } |
577 | } | |
1da177e4 LT |
578 | } |
579 | ||
580 | /* | |
581 | * The timer is locked, fire it and arrange for its reload. | |
582 | */ | |
583 | static void cpu_timer_fire(struct k_itimer *timer) | |
584 | { | |
1f169f84 SG |
585 | if ((timer->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE) { |
586 | /* | |
587 | * User don't want any signal. | |
588 | */ | |
589 | timer->it.cpu.expires.sched = 0; | |
590 | } else if (unlikely(timer->sigq == NULL)) { | |
1da177e4 LT |
591 | /* |
592 | * This a special case for clock_nanosleep, | |
593 | * not a normal timer from sys_timer_create. | |
594 | */ | |
595 | wake_up_process(timer->it_process); | |
596 | timer->it.cpu.expires.sched = 0; | |
597 | } else if (timer->it.cpu.incr.sched == 0) { | |
598 | /* | |
599 | * One-shot timer. Clear it as soon as it's fired. | |
600 | */ | |
601 | posix_timer_event(timer, 0); | |
602 | timer->it.cpu.expires.sched = 0; | |
603 | } else if (posix_timer_event(timer, ++timer->it_requeue_pending)) { | |
604 | /* | |
605 | * The signal did not get queued because the signal | |
606 | * was ignored, so we won't get any callback to | |
607 | * reload the timer. But we need to keep it | |
608 | * ticking in case the signal is deliverable next time. | |
609 | */ | |
610 | posix_cpu_timer_schedule(timer); | |
611 | } | |
612 | } | |
613 | ||
3997ad31 PZ |
614 | /* |
615 | * Sample a process (thread group) timer for the given group_leader task. | |
616 | * Must be called with tasklist_lock held for reading. | |
617 | */ | |
618 | static int cpu_timer_sample_group(const clockid_t which_clock, | |
619 | struct task_struct *p, | |
620 | union cpu_time_count *cpu) | |
621 | { | |
622 | struct task_cputime cputime; | |
623 | ||
624 | thread_group_cputimer(p, &cputime); | |
625 | switch (CPUCLOCK_WHICH(which_clock)) { | |
626 | default: | |
627 | return -EINVAL; | |
628 | case CPUCLOCK_PROF: | |
64861634 | 629 | cpu->cpu = cputime.utime + cputime.stime; |
3997ad31 PZ |
630 | break; |
631 | case CPUCLOCK_VIRT: | |
632 | cpu->cpu = cputime.utime; | |
633 | break; | |
634 | case CPUCLOCK_SCHED: | |
635 | cpu->sched = cputime.sum_exec_runtime + task_delta_exec(p); | |
636 | break; | |
637 | } | |
638 | return 0; | |
639 | } | |
640 | ||
a8572160 FW |
641 | #ifdef CONFIG_NO_HZ_FULL |
642 | static void nohz_kick_work_fn(struct work_struct *work) | |
643 | { | |
644 | tick_nohz_full_kick_all(); | |
645 | } | |
646 | ||
647 | static DECLARE_WORK(nohz_kick_work, nohz_kick_work_fn); | |
648 | ||
649 | /* | |
650 | * We need the IPIs to be sent from sane process context. | |
651 | * The posix cpu timers are always set with irqs disabled. | |
652 | */ | |
653 | static void posix_cpu_timer_kick_nohz(void) | |
654 | { | |
655 | schedule_work(&nohz_kick_work); | |
656 | } | |
657 | #else | |
658 | static inline void posix_cpu_timer_kick_nohz(void) { } | |
659 | #endif | |
660 | ||
1da177e4 LT |
661 | /* |
662 | * Guts of sys_timer_settime for CPU timers. | |
663 | * This is called with the timer locked and interrupts disabled. | |
664 | * If we return TIMER_RETRY, it's necessary to release the timer's lock | |
665 | * and try again. (This happens when the timer is in the middle of firing.) | |
666 | */ | |
bc2c8ea4 TG |
667 | static int posix_cpu_timer_set(struct k_itimer *timer, int flags, |
668 | struct itimerspec *new, struct itimerspec *old) | |
1da177e4 LT |
669 | { |
670 | struct task_struct *p = timer->it.cpu.task; | |
ae1a78ee | 671 | union cpu_time_count old_expires, new_expires, old_incr, val; |
1da177e4 LT |
672 | int ret; |
673 | ||
674 | if (unlikely(p == NULL)) { | |
675 | /* | |
676 | * Timer refers to a dead task's clock. | |
677 | */ | |
678 | return -ESRCH; | |
679 | } | |
680 | ||
681 | new_expires = timespec_to_sample(timer->it_clock, &new->it_value); | |
682 | ||
683 | read_lock(&tasklist_lock); | |
684 | /* | |
685 | * We need the tasklist_lock to protect against reaping that | |
d30fda35 | 686 | * clears p->sighand. If p has just been reaped, we can no |
1da177e4 LT |
687 | * longer get any information about it at all. |
688 | */ | |
d30fda35 | 689 | if (unlikely(p->sighand == NULL)) { |
1da177e4 LT |
690 | read_unlock(&tasklist_lock); |
691 | put_task_struct(p); | |
692 | timer->it.cpu.task = NULL; | |
693 | return -ESRCH; | |
694 | } | |
695 | ||
696 | /* | |
697 | * Disarm any old timer after extracting its expiry time. | |
698 | */ | |
699 | BUG_ON(!irqs_disabled()); | |
a69ac4a7 ON |
700 | |
701 | ret = 0; | |
ae1a78ee | 702 | old_incr = timer->it.cpu.incr; |
1da177e4 LT |
703 | spin_lock(&p->sighand->siglock); |
704 | old_expires = timer->it.cpu.expires; | |
a69ac4a7 ON |
705 | if (unlikely(timer->it.cpu.firing)) { |
706 | timer->it.cpu.firing = -1; | |
707 | ret = TIMER_RETRY; | |
708 | } else | |
709 | list_del_init(&timer->it.cpu.entry); | |
1da177e4 LT |
710 | |
711 | /* | |
712 | * We need to sample the current value to convert the new | |
713 | * value from to relative and absolute, and to convert the | |
714 | * old value from absolute to relative. To set a process | |
715 | * timer, we need a sample to balance the thread expiry | |
716 | * times (in arm_timer). With an absolute time, we must | |
717 | * check if it's already passed. In short, we need a sample. | |
718 | */ | |
719 | if (CPUCLOCK_PERTHREAD(timer->it_clock)) { | |
720 | cpu_clock_sample(timer->it_clock, p, &val); | |
721 | } else { | |
3997ad31 | 722 | cpu_timer_sample_group(timer->it_clock, p, &val); |
1da177e4 LT |
723 | } |
724 | ||
725 | if (old) { | |
726 | if (old_expires.sched == 0) { | |
727 | old->it_value.tv_sec = 0; | |
728 | old->it_value.tv_nsec = 0; | |
729 | } else { | |
730 | /* | |
731 | * Update the timer in case it has | |
732 | * overrun already. If it has, | |
733 | * we'll report it as having overrun | |
734 | * and with the next reloaded timer | |
735 | * already ticking, though we are | |
736 | * swallowing that pending | |
737 | * notification here to install the | |
738 | * new setting. | |
739 | */ | |
740 | bump_cpu_timer(timer, val); | |
741 | if (cpu_time_before(timer->it_clock, val, | |
742 | timer->it.cpu.expires)) { | |
743 | old_expires = cpu_time_sub( | |
744 | timer->it_clock, | |
745 | timer->it.cpu.expires, val); | |
746 | sample_to_timespec(timer->it_clock, | |
747 | old_expires, | |
748 | &old->it_value); | |
749 | } else { | |
750 | old->it_value.tv_nsec = 1; | |
751 | old->it_value.tv_sec = 0; | |
752 | } | |
753 | } | |
754 | } | |
755 | ||
a69ac4a7 | 756 | if (unlikely(ret)) { |
1da177e4 LT |
757 | /* |
758 | * We are colliding with the timer actually firing. | |
759 | * Punt after filling in the timer's old value, and | |
760 | * disable this firing since we are already reporting | |
761 | * it as an overrun (thanks to bump_cpu_timer above). | |
762 | */ | |
c2873937 | 763 | spin_unlock(&p->sighand->siglock); |
1da177e4 | 764 | read_unlock(&tasklist_lock); |
1da177e4 LT |
765 | goto out; |
766 | } | |
767 | ||
768 | if (new_expires.sched != 0 && !(flags & TIMER_ABSTIME)) { | |
769 | cpu_time_add(timer->it_clock, &new_expires, val); | |
770 | } | |
771 | ||
772 | /* | |
773 | * Install the new expiry time (or zero). | |
774 | * For a timer with no notification action, we don't actually | |
775 | * arm the timer (we'll just fake it for timer_gettime). | |
776 | */ | |
777 | timer->it.cpu.expires = new_expires; | |
778 | if (new_expires.sched != 0 && | |
1da177e4 | 779 | cpu_time_before(timer->it_clock, val, new_expires)) { |
5eb9aa64 | 780 | arm_timer(timer); |
1da177e4 LT |
781 | } |
782 | ||
c2873937 | 783 | spin_unlock(&p->sighand->siglock); |
1da177e4 LT |
784 | read_unlock(&tasklist_lock); |
785 | ||
786 | /* | |
787 | * Install the new reload setting, and | |
788 | * set up the signal and overrun bookkeeping. | |
789 | */ | |
790 | timer->it.cpu.incr = timespec_to_sample(timer->it_clock, | |
791 | &new->it_interval); | |
792 | ||
793 | /* | |
794 | * This acts as a modification timestamp for the timer, | |
795 | * so any automatic reload attempt will punt on seeing | |
796 | * that we have reset the timer manually. | |
797 | */ | |
798 | timer->it_requeue_pending = (timer->it_requeue_pending + 2) & | |
799 | ~REQUEUE_PENDING; | |
800 | timer->it_overrun_last = 0; | |
801 | timer->it_overrun = -1; | |
802 | ||
803 | if (new_expires.sched != 0 && | |
1da177e4 LT |
804 | !cpu_time_before(timer->it_clock, val, new_expires)) { |
805 | /* | |
806 | * The designated time already passed, so we notify | |
807 | * immediately, even if the thread never runs to | |
808 | * accumulate more time on this clock. | |
809 | */ | |
810 | cpu_timer_fire(timer); | |
811 | } | |
812 | ||
813 | ret = 0; | |
814 | out: | |
815 | if (old) { | |
816 | sample_to_timespec(timer->it_clock, | |
ae1a78ee | 817 | old_incr, &old->it_interval); |
1da177e4 | 818 | } |
a8572160 FW |
819 | if (!ret) |
820 | posix_cpu_timer_kick_nohz(); | |
1da177e4 LT |
821 | return ret; |
822 | } | |
823 | ||
bc2c8ea4 | 824 | static void posix_cpu_timer_get(struct k_itimer *timer, struct itimerspec *itp) |
1da177e4 LT |
825 | { |
826 | union cpu_time_count now; | |
827 | struct task_struct *p = timer->it.cpu.task; | |
828 | int clear_dead; | |
829 | ||
830 | /* | |
831 | * Easy part: convert the reload time. | |
832 | */ | |
833 | sample_to_timespec(timer->it_clock, | |
834 | timer->it.cpu.incr, &itp->it_interval); | |
835 | ||
836 | if (timer->it.cpu.expires.sched == 0) { /* Timer not armed at all. */ | |
837 | itp->it_value.tv_sec = itp->it_value.tv_nsec = 0; | |
838 | return; | |
839 | } | |
840 | ||
841 | if (unlikely(p == NULL)) { | |
842 | /* | |
843 | * This task already died and the timer will never fire. | |
844 | * In this case, expires is actually the dead value. | |
845 | */ | |
846 | dead: | |
847 | sample_to_timespec(timer->it_clock, timer->it.cpu.expires, | |
848 | &itp->it_value); | |
849 | return; | |
850 | } | |
851 | ||
852 | /* | |
853 | * Sample the clock to take the difference with the expiry time. | |
854 | */ | |
855 | if (CPUCLOCK_PERTHREAD(timer->it_clock)) { | |
856 | cpu_clock_sample(timer->it_clock, p, &now); | |
857 | clear_dead = p->exit_state; | |
858 | } else { | |
859 | read_lock(&tasklist_lock); | |
d30fda35 | 860 | if (unlikely(p->sighand == NULL)) { |
1da177e4 LT |
861 | /* |
862 | * The process has been reaped. | |
863 | * We can't even collect a sample any more. | |
864 | * Call the timer disarmed, nothing else to do. | |
865 | */ | |
866 | put_task_struct(p); | |
867 | timer->it.cpu.task = NULL; | |
868 | timer->it.cpu.expires.sched = 0; | |
869 | read_unlock(&tasklist_lock); | |
870 | goto dead; | |
871 | } else { | |
3997ad31 | 872 | cpu_timer_sample_group(timer->it_clock, p, &now); |
1da177e4 LT |
873 | clear_dead = (unlikely(p->exit_state) && |
874 | thread_group_empty(p)); | |
875 | } | |
876 | read_unlock(&tasklist_lock); | |
877 | } | |
878 | ||
1da177e4 LT |
879 | if (unlikely(clear_dead)) { |
880 | /* | |
881 | * We've noticed that the thread is dead, but | |
882 | * not yet reaped. Take this opportunity to | |
883 | * drop our task ref. | |
884 | */ | |
885 | clear_dead_task(timer, now); | |
886 | goto dead; | |
887 | } | |
888 | ||
889 | if (cpu_time_before(timer->it_clock, now, timer->it.cpu.expires)) { | |
890 | sample_to_timespec(timer->it_clock, | |
891 | cpu_time_sub(timer->it_clock, | |
892 | timer->it.cpu.expires, now), | |
893 | &itp->it_value); | |
894 | } else { | |
895 | /* | |
896 | * The timer should have expired already, but the firing | |
897 | * hasn't taken place yet. Say it's just about to expire. | |
898 | */ | |
899 | itp->it_value.tv_nsec = 1; | |
900 | itp->it_value.tv_sec = 0; | |
901 | } | |
902 | } | |
903 | ||
904 | /* | |
905 | * Check for any per-thread CPU timers that have fired and move them off | |
906 | * the tsk->cpu_timers[N] list onto the firing list. Here we update the | |
907 | * tsk->it_*_expires values to reflect the remaining thread CPU timers. | |
908 | */ | |
909 | static void check_thread_timers(struct task_struct *tsk, | |
910 | struct list_head *firing) | |
911 | { | |
e80eda94 | 912 | int maxfire; |
1da177e4 | 913 | struct list_head *timers = tsk->cpu_timers; |
78f2c7db | 914 | struct signal_struct *const sig = tsk->signal; |
d4bb5274 | 915 | unsigned long soft; |
1da177e4 | 916 | |
e80eda94 | 917 | maxfire = 20; |
64861634 | 918 | tsk->cputime_expires.prof_exp = 0; |
1da177e4 | 919 | while (!list_empty(timers)) { |
b5e61818 | 920 | struct cpu_timer_list *t = list_first_entry(timers, |
1da177e4 LT |
921 | struct cpu_timer_list, |
922 | entry); | |
64861634 | 923 | if (!--maxfire || prof_ticks(tsk) < t->expires.cpu) { |
f06febc9 | 924 | tsk->cputime_expires.prof_exp = t->expires.cpu; |
1da177e4 LT |
925 | break; |
926 | } | |
927 | t->firing = 1; | |
928 | list_move_tail(&t->entry, firing); | |
929 | } | |
930 | ||
931 | ++timers; | |
e80eda94 | 932 | maxfire = 20; |
64861634 | 933 | tsk->cputime_expires.virt_exp = 0; |
1da177e4 | 934 | while (!list_empty(timers)) { |
b5e61818 | 935 | struct cpu_timer_list *t = list_first_entry(timers, |
1da177e4 LT |
936 | struct cpu_timer_list, |
937 | entry); | |
64861634 | 938 | if (!--maxfire || virt_ticks(tsk) < t->expires.cpu) { |
f06febc9 | 939 | tsk->cputime_expires.virt_exp = t->expires.cpu; |
1da177e4 LT |
940 | break; |
941 | } | |
942 | t->firing = 1; | |
943 | list_move_tail(&t->entry, firing); | |
944 | } | |
945 | ||
946 | ++timers; | |
e80eda94 | 947 | maxfire = 20; |
f06febc9 | 948 | tsk->cputime_expires.sched_exp = 0; |
1da177e4 | 949 | while (!list_empty(timers)) { |
b5e61818 | 950 | struct cpu_timer_list *t = list_first_entry(timers, |
1da177e4 LT |
951 | struct cpu_timer_list, |
952 | entry); | |
41b86e9c | 953 | if (!--maxfire || tsk->se.sum_exec_runtime < t->expires.sched) { |
f06febc9 | 954 | tsk->cputime_expires.sched_exp = t->expires.sched; |
1da177e4 LT |
955 | break; |
956 | } | |
957 | t->firing = 1; | |
958 | list_move_tail(&t->entry, firing); | |
959 | } | |
78f2c7db PZ |
960 | |
961 | /* | |
962 | * Check for the special case thread timers. | |
963 | */ | |
78d7d407 | 964 | soft = ACCESS_ONCE(sig->rlim[RLIMIT_RTTIME].rlim_cur); |
d4bb5274 | 965 | if (soft != RLIM_INFINITY) { |
78d7d407 JS |
966 | unsigned long hard = |
967 | ACCESS_ONCE(sig->rlim[RLIMIT_RTTIME].rlim_max); | |
78f2c7db | 968 | |
5a52dd50 PZ |
969 | if (hard != RLIM_INFINITY && |
970 | tsk->rt.timeout > DIV_ROUND_UP(hard, USEC_PER_SEC/HZ)) { | |
78f2c7db PZ |
971 | /* |
972 | * At the hard limit, we just die. | |
973 | * No need to calculate anything else now. | |
974 | */ | |
975 | __group_send_sig_info(SIGKILL, SEND_SIG_PRIV, tsk); | |
976 | return; | |
977 | } | |
d4bb5274 | 978 | if (tsk->rt.timeout > DIV_ROUND_UP(soft, USEC_PER_SEC/HZ)) { |
78f2c7db PZ |
979 | /* |
980 | * At the soft limit, send a SIGXCPU every second. | |
981 | */ | |
d4bb5274 JS |
982 | if (soft < hard) { |
983 | soft += USEC_PER_SEC; | |
984 | sig->rlim[RLIMIT_RTTIME].rlim_cur = soft; | |
78f2c7db | 985 | } |
81d50bb2 HS |
986 | printk(KERN_INFO |
987 | "RT Watchdog Timeout: %s[%d]\n", | |
988 | tsk->comm, task_pid_nr(tsk)); | |
78f2c7db PZ |
989 | __group_send_sig_info(SIGXCPU, SEND_SIG_PRIV, tsk); |
990 | } | |
991 | } | |
1da177e4 LT |
992 | } |
993 | ||
15365c10 | 994 | static void stop_process_timers(struct signal_struct *sig) |
3fccfd67 | 995 | { |
15365c10 | 996 | struct thread_group_cputimer *cputimer = &sig->cputimer; |
3fccfd67 PZ |
997 | unsigned long flags; |
998 | ||
ee30a7b2 | 999 | raw_spin_lock_irqsave(&cputimer->lock, flags); |
3fccfd67 | 1000 | cputimer->running = 0; |
ee30a7b2 | 1001 | raw_spin_unlock_irqrestore(&cputimer->lock, flags); |
3fccfd67 PZ |
1002 | } |
1003 | ||
8356b5f9 SG |
1004 | static u32 onecputick; |
1005 | ||
42c4ab41 SG |
1006 | static void check_cpu_itimer(struct task_struct *tsk, struct cpu_itimer *it, |
1007 | cputime_t *expires, cputime_t cur_time, int signo) | |
1008 | { | |
64861634 | 1009 | if (!it->expires) |
42c4ab41 SG |
1010 | return; |
1011 | ||
64861634 MS |
1012 | if (cur_time >= it->expires) { |
1013 | if (it->incr) { | |
1014 | it->expires += it->incr; | |
8356b5f9 SG |
1015 | it->error += it->incr_error; |
1016 | if (it->error >= onecputick) { | |
64861634 | 1017 | it->expires -= cputime_one_jiffy; |
8356b5f9 SG |
1018 | it->error -= onecputick; |
1019 | } | |
3f0a525e | 1020 | } else { |
64861634 | 1021 | it->expires = 0; |
3f0a525e | 1022 | } |
42c4ab41 | 1023 | |
3f0a525e XG |
1024 | trace_itimer_expire(signo == SIGPROF ? |
1025 | ITIMER_PROF : ITIMER_VIRTUAL, | |
1026 | tsk->signal->leader_pid, cur_time); | |
42c4ab41 SG |
1027 | __group_send_sig_info(signo, SEND_SIG_PRIV, tsk); |
1028 | } | |
1029 | ||
64861634 | 1030 | if (it->expires && (!*expires || it->expires < *expires)) { |
42c4ab41 SG |
1031 | *expires = it->expires; |
1032 | } | |
1033 | } | |
1034 | ||
29f87b79 SG |
1035 | /** |
1036 | * task_cputime_zero - Check a task_cputime struct for all zero fields. | |
1037 | * | |
1038 | * @cputime: The struct to compare. | |
1039 | * | |
1040 | * Checks @cputime to see if all fields are zero. Returns true if all fields | |
1041 | * are zero, false if any field is nonzero. | |
1042 | */ | |
1043 | static inline int task_cputime_zero(const struct task_cputime *cputime) | |
1044 | { | |
64861634 | 1045 | if (!cputime->utime && !cputime->stime && !cputime->sum_exec_runtime) |
29f87b79 SG |
1046 | return 1; |
1047 | return 0; | |
1048 | } | |
1049 | ||
1da177e4 LT |
1050 | /* |
1051 | * Check for any per-thread CPU timers that have fired and move them | |
1052 | * off the tsk->*_timers list onto the firing list. Per-thread timers | |
1053 | * have already been taken off. | |
1054 | */ | |
1055 | static void check_process_timers(struct task_struct *tsk, | |
1056 | struct list_head *firing) | |
1057 | { | |
e80eda94 | 1058 | int maxfire; |
1da177e4 | 1059 | struct signal_struct *const sig = tsk->signal; |
f06febc9 | 1060 | cputime_t utime, ptime, virt_expires, prof_expires; |
41b86e9c | 1061 | unsigned long long sum_sched_runtime, sched_expires; |
1da177e4 | 1062 | struct list_head *timers = sig->cpu_timers; |
f06febc9 | 1063 | struct task_cputime cputime; |
d4bb5274 | 1064 | unsigned long soft; |
1da177e4 | 1065 | |
1da177e4 LT |
1066 | /* |
1067 | * Collect the current process totals. | |
1068 | */ | |
4cd4c1b4 | 1069 | thread_group_cputimer(tsk, &cputime); |
f06febc9 | 1070 | utime = cputime.utime; |
64861634 | 1071 | ptime = utime + cputime.stime; |
f06febc9 | 1072 | sum_sched_runtime = cputime.sum_exec_runtime; |
e80eda94 | 1073 | maxfire = 20; |
64861634 | 1074 | prof_expires = 0; |
1da177e4 | 1075 | while (!list_empty(timers)) { |
ee7dd205 | 1076 | struct cpu_timer_list *tl = list_first_entry(timers, |
1da177e4 LT |
1077 | struct cpu_timer_list, |
1078 | entry); | |
64861634 | 1079 | if (!--maxfire || ptime < tl->expires.cpu) { |
ee7dd205 | 1080 | prof_expires = tl->expires.cpu; |
1da177e4 LT |
1081 | break; |
1082 | } | |
ee7dd205 WC |
1083 | tl->firing = 1; |
1084 | list_move_tail(&tl->entry, firing); | |
1da177e4 LT |
1085 | } |
1086 | ||
1087 | ++timers; | |
e80eda94 | 1088 | maxfire = 20; |
64861634 | 1089 | virt_expires = 0; |
1da177e4 | 1090 | while (!list_empty(timers)) { |
ee7dd205 | 1091 | struct cpu_timer_list *tl = list_first_entry(timers, |
1da177e4 LT |
1092 | struct cpu_timer_list, |
1093 | entry); | |
64861634 | 1094 | if (!--maxfire || utime < tl->expires.cpu) { |
ee7dd205 | 1095 | virt_expires = tl->expires.cpu; |
1da177e4 LT |
1096 | break; |
1097 | } | |
ee7dd205 WC |
1098 | tl->firing = 1; |
1099 | list_move_tail(&tl->entry, firing); | |
1da177e4 LT |
1100 | } |
1101 | ||
1102 | ++timers; | |
e80eda94 | 1103 | maxfire = 20; |
1da177e4 LT |
1104 | sched_expires = 0; |
1105 | while (!list_empty(timers)) { | |
ee7dd205 | 1106 | struct cpu_timer_list *tl = list_first_entry(timers, |
1da177e4 LT |
1107 | struct cpu_timer_list, |
1108 | entry); | |
ee7dd205 WC |
1109 | if (!--maxfire || sum_sched_runtime < tl->expires.sched) { |
1110 | sched_expires = tl->expires.sched; | |
1da177e4 LT |
1111 | break; |
1112 | } | |
ee7dd205 WC |
1113 | tl->firing = 1; |
1114 | list_move_tail(&tl->entry, firing); | |
1da177e4 LT |
1115 | } |
1116 | ||
1117 | /* | |
1118 | * Check for the special case process timers. | |
1119 | */ | |
42c4ab41 SG |
1120 | check_cpu_itimer(tsk, &sig->it[CPUCLOCK_PROF], &prof_expires, ptime, |
1121 | SIGPROF); | |
1122 | check_cpu_itimer(tsk, &sig->it[CPUCLOCK_VIRT], &virt_expires, utime, | |
1123 | SIGVTALRM); | |
78d7d407 | 1124 | soft = ACCESS_ONCE(sig->rlim[RLIMIT_CPU].rlim_cur); |
d4bb5274 | 1125 | if (soft != RLIM_INFINITY) { |
1da177e4 | 1126 | unsigned long psecs = cputime_to_secs(ptime); |
78d7d407 JS |
1127 | unsigned long hard = |
1128 | ACCESS_ONCE(sig->rlim[RLIMIT_CPU].rlim_max); | |
1da177e4 | 1129 | cputime_t x; |
d4bb5274 | 1130 | if (psecs >= hard) { |
1da177e4 LT |
1131 | /* |
1132 | * At the hard limit, we just die. | |
1133 | * No need to calculate anything else now. | |
1134 | */ | |
1135 | __group_send_sig_info(SIGKILL, SEND_SIG_PRIV, tsk); | |
1136 | return; | |
1137 | } | |
d4bb5274 | 1138 | if (psecs >= soft) { |
1da177e4 LT |
1139 | /* |
1140 | * At the soft limit, send a SIGXCPU every second. | |
1141 | */ | |
1142 | __group_send_sig_info(SIGXCPU, SEND_SIG_PRIV, tsk); | |
d4bb5274 JS |
1143 | if (soft < hard) { |
1144 | soft++; | |
1145 | sig->rlim[RLIMIT_CPU].rlim_cur = soft; | |
1da177e4 LT |
1146 | } |
1147 | } | |
d4bb5274 | 1148 | x = secs_to_cputime(soft); |
64861634 | 1149 | if (!prof_expires || x < prof_expires) { |
1da177e4 LT |
1150 | prof_expires = x; |
1151 | } | |
1152 | } | |
1153 | ||
29f87b79 SG |
1154 | sig->cputime_expires.prof_exp = prof_expires; |
1155 | sig->cputime_expires.virt_exp = virt_expires; | |
1156 | sig->cputime_expires.sched_exp = sched_expires; | |
1157 | if (task_cputime_zero(&sig->cputime_expires)) | |
1158 | stop_process_timers(sig); | |
1da177e4 LT |
1159 | } |
1160 | ||
1161 | /* | |
1162 | * This is called from the signal code (via do_schedule_next_timer) | |
1163 | * when the last timer signal was delivered and we have to reload the timer. | |
1164 | */ | |
1165 | void posix_cpu_timer_schedule(struct k_itimer *timer) | |
1166 | { | |
1167 | struct task_struct *p = timer->it.cpu.task; | |
1168 | union cpu_time_count now; | |
1169 | ||
1170 | if (unlikely(p == NULL)) | |
1171 | /* | |
1172 | * The task was cleaned up already, no future firings. | |
1173 | */ | |
708f430d | 1174 | goto out; |
1da177e4 LT |
1175 | |
1176 | /* | |
1177 | * Fetch the current sample and update the timer's expiry time. | |
1178 | */ | |
1179 | if (CPUCLOCK_PERTHREAD(timer->it_clock)) { | |
1180 | cpu_clock_sample(timer->it_clock, p, &now); | |
1181 | bump_cpu_timer(timer, now); | |
1182 | if (unlikely(p->exit_state)) { | |
1183 | clear_dead_task(timer, now); | |
708f430d | 1184 | goto out; |
1da177e4 LT |
1185 | } |
1186 | read_lock(&tasklist_lock); /* arm_timer needs it. */ | |
c2873937 | 1187 | spin_lock(&p->sighand->siglock); |
1da177e4 LT |
1188 | } else { |
1189 | read_lock(&tasklist_lock); | |
d30fda35 | 1190 | if (unlikely(p->sighand == NULL)) { |
1da177e4 LT |
1191 | /* |
1192 | * The process has been reaped. | |
1193 | * We can't even collect a sample any more. | |
1194 | */ | |
1195 | put_task_struct(p); | |
1196 | timer->it.cpu.task = p = NULL; | |
1197 | timer->it.cpu.expires.sched = 0; | |
708f430d | 1198 | goto out_unlock; |
1da177e4 LT |
1199 | } else if (unlikely(p->exit_state) && thread_group_empty(p)) { |
1200 | /* | |
1201 | * We've noticed that the thread is dead, but | |
1202 | * not yet reaped. Take this opportunity to | |
1203 | * drop our task ref. | |
1204 | */ | |
1205 | clear_dead_task(timer, now); | |
708f430d | 1206 | goto out_unlock; |
1da177e4 | 1207 | } |
c2873937 | 1208 | spin_lock(&p->sighand->siglock); |
3997ad31 | 1209 | cpu_timer_sample_group(timer->it_clock, p, &now); |
1da177e4 LT |
1210 | bump_cpu_timer(timer, now); |
1211 | /* Leave the tasklist_lock locked for the call below. */ | |
1212 | } | |
1213 | ||
1214 | /* | |
1215 | * Now re-arm for the new expiry time. | |
1216 | */ | |
c2873937 | 1217 | BUG_ON(!irqs_disabled()); |
5eb9aa64 | 1218 | arm_timer(timer); |
c2873937 | 1219 | spin_unlock(&p->sighand->siglock); |
1da177e4 | 1220 | |
708f430d | 1221 | out_unlock: |
1da177e4 | 1222 | read_unlock(&tasklist_lock); |
708f430d RM |
1223 | |
1224 | out: | |
1225 | timer->it_overrun_last = timer->it_overrun; | |
1226 | timer->it_overrun = -1; | |
1227 | ++timer->it_requeue_pending; | |
1da177e4 LT |
1228 | } |
1229 | ||
f06febc9 FM |
1230 | /** |
1231 | * task_cputime_expired - Compare two task_cputime entities. | |
1232 | * | |
1233 | * @sample: The task_cputime structure to be checked for expiration. | |
1234 | * @expires: Expiration times, against which @sample will be checked. | |
1235 | * | |
1236 | * Checks @sample against @expires to see if any field of @sample has expired. | |
1237 | * Returns true if any field of the former is greater than the corresponding | |
1238 | * field of the latter if the latter field is set. Otherwise returns false. | |
1239 | */ | |
1240 | static inline int task_cputime_expired(const struct task_cputime *sample, | |
1241 | const struct task_cputime *expires) | |
1242 | { | |
64861634 | 1243 | if (expires->utime && sample->utime >= expires->utime) |
f06febc9 | 1244 | return 1; |
64861634 | 1245 | if (expires->stime && sample->utime + sample->stime >= expires->stime) |
f06febc9 FM |
1246 | return 1; |
1247 | if (expires->sum_exec_runtime != 0 && | |
1248 | sample->sum_exec_runtime >= expires->sum_exec_runtime) | |
1249 | return 1; | |
1250 | return 0; | |
1251 | } | |
1252 | ||
1253 | /** | |
1254 | * fastpath_timer_check - POSIX CPU timers fast path. | |
1255 | * | |
1256 | * @tsk: The task (thread) being checked. | |
f06febc9 | 1257 | * |
bb34d92f FM |
1258 | * Check the task and thread group timers. If both are zero (there are no |
1259 | * timers set) return false. Otherwise snapshot the task and thread group | |
1260 | * timers and compare them with the corresponding expiration times. Return | |
1261 | * true if a timer has expired, else return false. | |
f06febc9 | 1262 | */ |
bb34d92f | 1263 | static inline int fastpath_timer_check(struct task_struct *tsk) |
f06febc9 | 1264 | { |
ad133ba3 | 1265 | struct signal_struct *sig; |
6fac4829 FW |
1266 | cputime_t utime, stime; |
1267 | ||
1268 | task_cputime(tsk, &utime, &stime); | |
bb34d92f | 1269 | |
bb34d92f FM |
1270 | if (!task_cputime_zero(&tsk->cputime_expires)) { |
1271 | struct task_cputime task_sample = { | |
6fac4829 FW |
1272 | .utime = utime, |
1273 | .stime = stime, | |
bb34d92f FM |
1274 | .sum_exec_runtime = tsk->se.sum_exec_runtime |
1275 | }; | |
1276 | ||
1277 | if (task_cputime_expired(&task_sample, &tsk->cputime_expires)) | |
1278 | return 1; | |
1279 | } | |
ad133ba3 ON |
1280 | |
1281 | sig = tsk->signal; | |
29f87b79 | 1282 | if (sig->cputimer.running) { |
bb34d92f FM |
1283 | struct task_cputime group_sample; |
1284 | ||
ee30a7b2 | 1285 | raw_spin_lock(&sig->cputimer.lock); |
8d1f431c | 1286 | group_sample = sig->cputimer.cputime; |
ee30a7b2 | 1287 | raw_spin_unlock(&sig->cputimer.lock); |
8d1f431c | 1288 | |
bb34d92f FM |
1289 | if (task_cputime_expired(&group_sample, &sig->cputime_expires)) |
1290 | return 1; | |
1291 | } | |
37bebc70 | 1292 | |
f55db609 | 1293 | return 0; |
f06febc9 FM |
1294 | } |
1295 | ||
1da177e4 LT |
1296 | /* |
1297 | * This is called from the timer interrupt handler. The irq handler has | |
1298 | * already updated our counts. We need to check if any timers fire now. | |
1299 | * Interrupts are disabled. | |
1300 | */ | |
1301 | void run_posix_cpu_timers(struct task_struct *tsk) | |
1302 | { | |
1303 | LIST_HEAD(firing); | |
1304 | struct k_itimer *timer, *next; | |
0bdd2ed4 | 1305 | unsigned long flags; |
1da177e4 LT |
1306 | |
1307 | BUG_ON(!irqs_disabled()); | |
1308 | ||
1da177e4 | 1309 | /* |
f06febc9 | 1310 | * The fast path checks that there are no expired thread or thread |
bb34d92f | 1311 | * group timers. If that's so, just return. |
1da177e4 | 1312 | */ |
bb34d92f | 1313 | if (!fastpath_timer_check(tsk)) |
f06febc9 | 1314 | return; |
5ce73a4a | 1315 | |
0bdd2ed4 ON |
1316 | if (!lock_task_sighand(tsk, &flags)) |
1317 | return; | |
bb34d92f FM |
1318 | /* |
1319 | * Here we take off tsk->signal->cpu_timers[N] and | |
1320 | * tsk->cpu_timers[N] all the timers that are firing, and | |
1321 | * put them on the firing list. | |
1322 | */ | |
1323 | check_thread_timers(tsk, &firing); | |
29f87b79 SG |
1324 | /* |
1325 | * If there are any active process wide timers (POSIX 1.b, itimers, | |
1326 | * RLIMIT_CPU) cputimer must be running. | |
1327 | */ | |
1328 | if (tsk->signal->cputimer.running) | |
1329 | check_process_timers(tsk, &firing); | |
1da177e4 | 1330 | |
bb34d92f FM |
1331 | /* |
1332 | * We must release these locks before taking any timer's lock. | |
1333 | * There is a potential race with timer deletion here, as the | |
1334 | * siglock now protects our private firing list. We have set | |
1335 | * the firing flag in each timer, so that a deletion attempt | |
1336 | * that gets the timer lock before we do will give it up and | |
1337 | * spin until we've taken care of that timer below. | |
1338 | */ | |
0bdd2ed4 | 1339 | unlock_task_sighand(tsk, &flags); |
1da177e4 LT |
1340 | |
1341 | /* | |
1342 | * Now that all the timers on our list have the firing flag, | |
25985edc | 1343 | * no one will touch their list entries but us. We'll take |
1da177e4 LT |
1344 | * each timer's lock before clearing its firing flag, so no |
1345 | * timer call will interfere. | |
1346 | */ | |
1347 | list_for_each_entry_safe(timer, next, &firing, it.cpu.entry) { | |
6e85c5ba HS |
1348 | int cpu_firing; |
1349 | ||
1da177e4 LT |
1350 | spin_lock(&timer->it_lock); |
1351 | list_del_init(&timer->it.cpu.entry); | |
6e85c5ba | 1352 | cpu_firing = timer->it.cpu.firing; |
1da177e4 LT |
1353 | timer->it.cpu.firing = 0; |
1354 | /* | |
1355 | * The firing flag is -1 if we collided with a reset | |
1356 | * of the timer, which already reported this | |
1357 | * almost-firing as an overrun. So don't generate an event. | |
1358 | */ | |
6e85c5ba | 1359 | if (likely(cpu_firing >= 0)) |
1da177e4 | 1360 | cpu_timer_fire(timer); |
1da177e4 LT |
1361 | spin_unlock(&timer->it_lock); |
1362 | } | |
a8572160 FW |
1363 | |
1364 | /* | |
1365 | * In case some timers were rescheduled after the queue got emptied, | |
1366 | * wake up full dynticks CPUs. | |
1367 | */ | |
1368 | if (tsk->signal->cputimer.running) | |
1369 | posix_cpu_timer_kick_nohz(); | |
1da177e4 LT |
1370 | } |
1371 | ||
1372 | /* | |
f55db609 | 1373 | * Set one of the process-wide special case CPU timers or RLIMIT_CPU. |
f06febc9 | 1374 | * The tsk->sighand->siglock must be held by the caller. |
1da177e4 LT |
1375 | */ |
1376 | void set_process_cpu_timer(struct task_struct *tsk, unsigned int clock_idx, | |
1377 | cputime_t *newval, cputime_t *oldval) | |
1378 | { | |
1379 | union cpu_time_count now; | |
1da177e4 LT |
1380 | |
1381 | BUG_ON(clock_idx == CPUCLOCK_SCHED); | |
4cd4c1b4 | 1382 | cpu_timer_sample_group(clock_idx, tsk, &now); |
1da177e4 LT |
1383 | |
1384 | if (oldval) { | |
f55db609 SG |
1385 | /* |
1386 | * We are setting itimer. The *oldval is absolute and we update | |
1387 | * it to be relative, *newval argument is relative and we update | |
1388 | * it to be absolute. | |
1389 | */ | |
64861634 MS |
1390 | if (*oldval) { |
1391 | if (*oldval <= now.cpu) { | |
1da177e4 | 1392 | /* Just about to fire. */ |
a42548a1 | 1393 | *oldval = cputime_one_jiffy; |
1da177e4 | 1394 | } else { |
64861634 | 1395 | *oldval -= now.cpu; |
1da177e4 LT |
1396 | } |
1397 | } | |
1398 | ||
64861634 | 1399 | if (!*newval) |
a8572160 | 1400 | goto out; |
64861634 | 1401 | *newval += now.cpu; |
1da177e4 LT |
1402 | } |
1403 | ||
1404 | /* | |
f55db609 SG |
1405 | * Update expiration cache if we are the earliest timer, or eventually |
1406 | * RLIMIT_CPU limit is earlier than prof_exp cpu timer expire. | |
1da177e4 | 1407 | */ |
f55db609 SG |
1408 | switch (clock_idx) { |
1409 | case CPUCLOCK_PROF: | |
1410 | if (expires_gt(tsk->signal->cputime_expires.prof_exp, *newval)) | |
f06febc9 | 1411 | tsk->signal->cputime_expires.prof_exp = *newval; |
f55db609 SG |
1412 | break; |
1413 | case CPUCLOCK_VIRT: | |
1414 | if (expires_gt(tsk->signal->cputime_expires.virt_exp, *newval)) | |
f06febc9 | 1415 | tsk->signal->cputime_expires.virt_exp = *newval; |
f55db609 | 1416 | break; |
1da177e4 | 1417 | } |
a8572160 FW |
1418 | out: |
1419 | posix_cpu_timer_kick_nohz(); | |
1da177e4 LT |
1420 | } |
1421 | ||
e4b76555 TA |
1422 | static int do_cpu_nanosleep(const clockid_t which_clock, int flags, |
1423 | struct timespec *rqtp, struct itimerspec *it) | |
1da177e4 | 1424 | { |
1da177e4 LT |
1425 | struct k_itimer timer; |
1426 | int error; | |
1427 | ||
1da177e4 LT |
1428 | /* |
1429 | * Set up a temporary timer and then wait for it to go off. | |
1430 | */ | |
1431 | memset(&timer, 0, sizeof timer); | |
1432 | spin_lock_init(&timer.it_lock); | |
1433 | timer.it_clock = which_clock; | |
1434 | timer.it_overrun = -1; | |
1435 | error = posix_cpu_timer_create(&timer); | |
1436 | timer.it_process = current; | |
1437 | if (!error) { | |
1da177e4 | 1438 | static struct itimerspec zero_it; |
e4b76555 TA |
1439 | |
1440 | memset(it, 0, sizeof *it); | |
1441 | it->it_value = *rqtp; | |
1da177e4 LT |
1442 | |
1443 | spin_lock_irq(&timer.it_lock); | |
e4b76555 | 1444 | error = posix_cpu_timer_set(&timer, flags, it, NULL); |
1da177e4 LT |
1445 | if (error) { |
1446 | spin_unlock_irq(&timer.it_lock); | |
1447 | return error; | |
1448 | } | |
1449 | ||
1450 | while (!signal_pending(current)) { | |
1451 | if (timer.it.cpu.expires.sched == 0) { | |
1452 | /* | |
e6c42c29 SG |
1453 | * Our timer fired and was reset, below |
1454 | * deletion can not fail. | |
1da177e4 | 1455 | */ |
e6c42c29 | 1456 | posix_cpu_timer_del(&timer); |
1da177e4 LT |
1457 | spin_unlock_irq(&timer.it_lock); |
1458 | return 0; | |
1459 | } | |
1460 | ||
1461 | /* | |
1462 | * Block until cpu_timer_fire (or a signal) wakes us. | |
1463 | */ | |
1464 | __set_current_state(TASK_INTERRUPTIBLE); | |
1465 | spin_unlock_irq(&timer.it_lock); | |
1466 | schedule(); | |
1467 | spin_lock_irq(&timer.it_lock); | |
1468 | } | |
1469 | ||
1470 | /* | |
1471 | * We were interrupted by a signal. | |
1472 | */ | |
1473 | sample_to_timespec(which_clock, timer.it.cpu.expires, rqtp); | |
e6c42c29 SG |
1474 | error = posix_cpu_timer_set(&timer, 0, &zero_it, it); |
1475 | if (!error) { | |
1476 | /* | |
1477 | * Timer is now unarmed, deletion can not fail. | |
1478 | */ | |
1479 | posix_cpu_timer_del(&timer); | |
1480 | } | |
1da177e4 LT |
1481 | spin_unlock_irq(&timer.it_lock); |
1482 | ||
e6c42c29 SG |
1483 | while (error == TIMER_RETRY) { |
1484 | /* | |
1485 | * We need to handle case when timer was or is in the | |
1486 | * middle of firing. In other cases we already freed | |
1487 | * resources. | |
1488 | */ | |
1489 | spin_lock_irq(&timer.it_lock); | |
1490 | error = posix_cpu_timer_del(&timer); | |
1491 | spin_unlock_irq(&timer.it_lock); | |
1492 | } | |
1493 | ||
e4b76555 | 1494 | if ((it->it_value.tv_sec | it->it_value.tv_nsec) == 0) { |
1da177e4 LT |
1495 | /* |
1496 | * It actually did fire already. | |
1497 | */ | |
1498 | return 0; | |
1499 | } | |
1500 | ||
e4b76555 TA |
1501 | error = -ERESTART_RESTARTBLOCK; |
1502 | } | |
1503 | ||
1504 | return error; | |
1505 | } | |
1506 | ||
bc2c8ea4 TG |
1507 | static long posix_cpu_nsleep_restart(struct restart_block *restart_block); |
1508 | ||
1509 | static int posix_cpu_nsleep(const clockid_t which_clock, int flags, | |
1510 | struct timespec *rqtp, struct timespec __user *rmtp) | |
e4b76555 TA |
1511 | { |
1512 | struct restart_block *restart_block = | |
3751f9f2 | 1513 | ¤t_thread_info()->restart_block; |
e4b76555 TA |
1514 | struct itimerspec it; |
1515 | int error; | |
1516 | ||
1517 | /* | |
1518 | * Diagnose required errors first. | |
1519 | */ | |
1520 | if (CPUCLOCK_PERTHREAD(which_clock) && | |
1521 | (CPUCLOCK_PID(which_clock) == 0 || | |
1522 | CPUCLOCK_PID(which_clock) == current->pid)) | |
1523 | return -EINVAL; | |
1524 | ||
1525 | error = do_cpu_nanosleep(which_clock, flags, rqtp, &it); | |
1526 | ||
1527 | if (error == -ERESTART_RESTARTBLOCK) { | |
1528 | ||
3751f9f2 | 1529 | if (flags & TIMER_ABSTIME) |
e4b76555 | 1530 | return -ERESTARTNOHAND; |
1da177e4 | 1531 | /* |
3751f9f2 TG |
1532 | * Report back to the user the time still remaining. |
1533 | */ | |
1534 | if (rmtp && copy_to_user(rmtp, &it.it_value, sizeof *rmtp)) | |
1da177e4 LT |
1535 | return -EFAULT; |
1536 | ||
1711ef38 | 1537 | restart_block->fn = posix_cpu_nsleep_restart; |
ab8177bc | 1538 | restart_block->nanosleep.clockid = which_clock; |
3751f9f2 TG |
1539 | restart_block->nanosleep.rmtp = rmtp; |
1540 | restart_block->nanosleep.expires = timespec_to_ns(rqtp); | |
1da177e4 | 1541 | } |
1da177e4 LT |
1542 | return error; |
1543 | } | |
1544 | ||
bc2c8ea4 | 1545 | static long posix_cpu_nsleep_restart(struct restart_block *restart_block) |
1da177e4 | 1546 | { |
ab8177bc | 1547 | clockid_t which_clock = restart_block->nanosleep.clockid; |
97735f25 | 1548 | struct timespec t; |
e4b76555 TA |
1549 | struct itimerspec it; |
1550 | int error; | |
97735f25 | 1551 | |
3751f9f2 | 1552 | t = ns_to_timespec(restart_block->nanosleep.expires); |
97735f25 | 1553 | |
e4b76555 TA |
1554 | error = do_cpu_nanosleep(which_clock, TIMER_ABSTIME, &t, &it); |
1555 | ||
1556 | if (error == -ERESTART_RESTARTBLOCK) { | |
3751f9f2 | 1557 | struct timespec __user *rmtp = restart_block->nanosleep.rmtp; |
e4b76555 | 1558 | /* |
3751f9f2 TG |
1559 | * Report back to the user the time still remaining. |
1560 | */ | |
1561 | if (rmtp && copy_to_user(rmtp, &it.it_value, sizeof *rmtp)) | |
e4b76555 TA |
1562 | return -EFAULT; |
1563 | ||
3751f9f2 | 1564 | restart_block->nanosleep.expires = timespec_to_ns(&t); |
e4b76555 TA |
1565 | } |
1566 | return error; | |
1567 | ||
1da177e4 LT |
1568 | } |
1569 | ||
1da177e4 LT |
1570 | #define PROCESS_CLOCK MAKE_PROCESS_CPUCLOCK(0, CPUCLOCK_SCHED) |
1571 | #define THREAD_CLOCK MAKE_THREAD_CPUCLOCK(0, CPUCLOCK_SCHED) | |
1572 | ||
a924b04d TG |
1573 | static int process_cpu_clock_getres(const clockid_t which_clock, |
1574 | struct timespec *tp) | |
1da177e4 LT |
1575 | { |
1576 | return posix_cpu_clock_getres(PROCESS_CLOCK, tp); | |
1577 | } | |
a924b04d TG |
1578 | static int process_cpu_clock_get(const clockid_t which_clock, |
1579 | struct timespec *tp) | |
1da177e4 LT |
1580 | { |
1581 | return posix_cpu_clock_get(PROCESS_CLOCK, tp); | |
1582 | } | |
1583 | static int process_cpu_timer_create(struct k_itimer *timer) | |
1584 | { | |
1585 | timer->it_clock = PROCESS_CLOCK; | |
1586 | return posix_cpu_timer_create(timer); | |
1587 | } | |
a924b04d | 1588 | static int process_cpu_nsleep(const clockid_t which_clock, int flags, |
97735f25 TG |
1589 | struct timespec *rqtp, |
1590 | struct timespec __user *rmtp) | |
1da177e4 | 1591 | { |
97735f25 | 1592 | return posix_cpu_nsleep(PROCESS_CLOCK, flags, rqtp, rmtp); |
1da177e4 | 1593 | } |
1711ef38 TA |
1594 | static long process_cpu_nsleep_restart(struct restart_block *restart_block) |
1595 | { | |
1596 | return -EINVAL; | |
1597 | } | |
a924b04d TG |
1598 | static int thread_cpu_clock_getres(const clockid_t which_clock, |
1599 | struct timespec *tp) | |
1da177e4 LT |
1600 | { |
1601 | return posix_cpu_clock_getres(THREAD_CLOCK, tp); | |
1602 | } | |
a924b04d TG |
1603 | static int thread_cpu_clock_get(const clockid_t which_clock, |
1604 | struct timespec *tp) | |
1da177e4 LT |
1605 | { |
1606 | return posix_cpu_clock_get(THREAD_CLOCK, tp); | |
1607 | } | |
1608 | static int thread_cpu_timer_create(struct k_itimer *timer) | |
1609 | { | |
1610 | timer->it_clock = THREAD_CLOCK; | |
1611 | return posix_cpu_timer_create(timer); | |
1612 | } | |
1da177e4 | 1613 | |
1976945e TG |
1614 | struct k_clock clock_posix_cpu = { |
1615 | .clock_getres = posix_cpu_clock_getres, | |
1616 | .clock_set = posix_cpu_clock_set, | |
1617 | .clock_get = posix_cpu_clock_get, | |
1618 | .timer_create = posix_cpu_timer_create, | |
1619 | .nsleep = posix_cpu_nsleep, | |
1620 | .nsleep_restart = posix_cpu_nsleep_restart, | |
1621 | .timer_set = posix_cpu_timer_set, | |
1622 | .timer_del = posix_cpu_timer_del, | |
1623 | .timer_get = posix_cpu_timer_get, | |
1624 | }; | |
1625 | ||
1da177e4 LT |
1626 | static __init int init_posix_cpu_timers(void) |
1627 | { | |
1628 | struct k_clock process = { | |
2fd1f040 TG |
1629 | .clock_getres = process_cpu_clock_getres, |
1630 | .clock_get = process_cpu_clock_get, | |
2fd1f040 TG |
1631 | .timer_create = process_cpu_timer_create, |
1632 | .nsleep = process_cpu_nsleep, | |
1633 | .nsleep_restart = process_cpu_nsleep_restart, | |
1da177e4 LT |
1634 | }; |
1635 | struct k_clock thread = { | |
2fd1f040 TG |
1636 | .clock_getres = thread_cpu_clock_getres, |
1637 | .clock_get = thread_cpu_clock_get, | |
2fd1f040 | 1638 | .timer_create = thread_cpu_timer_create, |
1da177e4 | 1639 | }; |
8356b5f9 | 1640 | struct timespec ts; |
1da177e4 | 1641 | |
52708737 TG |
1642 | posix_timers_register_clock(CLOCK_PROCESS_CPUTIME_ID, &process); |
1643 | posix_timers_register_clock(CLOCK_THREAD_CPUTIME_ID, &thread); | |
1da177e4 | 1644 | |
a42548a1 | 1645 | cputime_to_timespec(cputime_one_jiffy, &ts); |
8356b5f9 SG |
1646 | onecputick = ts.tv_nsec; |
1647 | WARN_ON(ts.tv_sec != 0); | |
1648 | ||
1da177e4 LT |
1649 | return 0; |
1650 | } | |
1651 | __initcall(init_posix_cpu_timers); |