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