hc
2024-01-05 071106ecf68c401173c58808b1cf5f68cc50d390
kernel/kernel/time/posix-cpu-timers.c
....@@ -20,11 +20,20 @@
2020
2121 static void posix_cpu_timer_rearm(struct k_itimer *timer);
2222
23
+void posix_cputimers_group_init(struct posix_cputimers *pct, u64 cpu_limit)
24
+{
25
+ posix_cputimers_init(pct);
26
+ if (cpu_limit != RLIM_INFINITY) {
27
+ pct->bases[CPUCLOCK_PROF].nextevt = cpu_limit * NSEC_PER_SEC;
28
+ pct->timers_active = true;
29
+ }
30
+}
31
+
2332 /*
2433 * Called after updating RLIMIT_CPU to run cpu timer and update
25
- * tsk->signal->cputime_expires expiration cache if necessary. Needs
26
- * siglock protection since other code may update expiration cache as
27
- * well.
34
+ * tsk->signal->posix_cputimers.bases[clock].nextevt expiration cache if
35
+ * necessary. Needs siglock protection since other code may update the
36
+ * expiration cache as well.
2837 */
2938 void update_rlimit_cpu(struct task_struct *task, unsigned long rlim_new)
3039 {
....@@ -35,46 +44,87 @@
3544 spin_unlock_irq(&task->sighand->siglock);
3645 }
3746
38
-static int check_clock(const clockid_t which_clock)
47
+/*
48
+ * Functions for validating access to tasks.
49
+ */
50
+static struct pid *pid_for_clock(const clockid_t clock, bool gettime)
3951 {
40
- int error = 0;
41
- struct task_struct *p;
42
- const pid_t pid = CPUCLOCK_PID(which_clock);
52
+ const bool thread = !!CPUCLOCK_PERTHREAD(clock);
53
+ const pid_t upid = CPUCLOCK_PID(clock);
54
+ struct pid *pid;
4355
44
- if (CPUCLOCK_WHICH(which_clock) >= CPUCLOCK_MAX)
45
- return -EINVAL;
56
+ if (CPUCLOCK_WHICH(clock) >= CPUCLOCK_MAX)
57
+ return NULL;
4658
47
- if (pid == 0)
48
- return 0;
59
+ /*
60
+ * If the encoded PID is 0, then the timer is targeted at current
61
+ * or the process to which current belongs.
62
+ */
63
+ if (upid == 0)
64
+ return thread ? task_pid(current) : task_tgid(current);
65
+
66
+ pid = find_vpid(upid);
67
+ if (!pid)
68
+ return NULL;
69
+
70
+ if (thread) {
71
+ struct task_struct *tsk = pid_task(pid, PIDTYPE_PID);
72
+ return (tsk && same_thread_group(tsk, current)) ? pid : NULL;
73
+ }
74
+
75
+ /*
76
+ * For clock_gettime(PROCESS) allow finding the process by
77
+ * with the pid of the current task. The code needs the tgid
78
+ * of the process so that pid_task(pid, PIDTYPE_TGID) can be
79
+ * used to find the process.
80
+ */
81
+ if (gettime && (pid == task_pid(current)))
82
+ return task_tgid(current);
83
+
84
+ /*
85
+ * For processes require that pid identifies a process.
86
+ */
87
+ return pid_has_task(pid, PIDTYPE_TGID) ? pid : NULL;
88
+}
89
+
90
+static inline int validate_clock_permissions(const clockid_t clock)
91
+{
92
+ int ret;
4993
5094 rcu_read_lock();
51
- p = find_task_by_vpid(pid);
52
- if (!p || !(CPUCLOCK_PERTHREAD(which_clock) ?
53
- same_thread_group(p, current) : has_group_leader_pid(p))) {
54
- error = -EINVAL;
55
- }
95
+ ret = pid_for_clock(clock, false) ? 0 : -EINVAL;
5696 rcu_read_unlock();
5797
58
- return error;
98
+ return ret;
99
+}
100
+
101
+static inline enum pid_type clock_pid_type(const clockid_t clock)
102
+{
103
+ return CPUCLOCK_PERTHREAD(clock) ? PIDTYPE_PID : PIDTYPE_TGID;
104
+}
105
+
106
+static inline struct task_struct *cpu_timer_task_rcu(struct k_itimer *timer)
107
+{
108
+ return pid_task(timer->it.cpu.pid, clock_pid_type(timer->it_clock));
59109 }
60110
61111 /*
62112 * Update expiry time from increment, and increase overrun count,
63113 * given the current clock sample.
64114 */
65
-static void bump_cpu_timer(struct k_itimer *timer, u64 now)
115
+static u64 bump_cpu_timer(struct k_itimer *timer, u64 now)
66116 {
117
+ u64 delta, incr, expires = timer->it.cpu.node.expires;
67118 int i;
68
- u64 delta, incr;
69119
70
- if (timer->it.cpu.incr == 0)
71
- return;
120
+ if (!timer->it_interval)
121
+ return expires;
72122
73
- if (now < timer->it.cpu.expires)
74
- return;
123
+ if (now < expires)
124
+ return expires;
75125
76
- incr = timer->it.cpu.incr;
77
- delta = now + incr - timer->it.cpu.expires;
126
+ incr = timer->it_interval;
127
+ delta = now + incr - expires;
78128
79129 /* Don't use (incr*2 < delta), incr*2 might overflow. */
80130 for (i = 0; incr < delta - incr; i++)
....@@ -84,48 +134,26 @@
84134 if (delta < incr)
85135 continue;
86136
87
- timer->it.cpu.expires += incr;
137
+ timer->it.cpu.node.expires += incr;
88138 timer->it_overrun += 1LL << i;
89139 delta -= incr;
90140 }
141
+ return timer->it.cpu.node.expires;
91142 }
92143
93
-/**
94
- * task_cputime_zero - Check a task_cputime struct for all zero fields.
95
- *
96
- * @cputime: The struct to compare.
97
- *
98
- * Checks @cputime to see if all fields are zero. Returns true if all fields
99
- * are zero, false if any field is nonzero.
100
- */
101
-static inline int task_cputime_zero(const struct task_cputime *cputime)
144
+/* Check whether all cache entries contain U64_MAX, i.e. eternal expiry time */
145
+static inline bool expiry_cache_is_inactive(const struct posix_cputimers *pct)
102146 {
103
- if (!cputime->utime && !cputime->stime && !cputime->sum_exec_runtime)
104
- return 1;
105
- return 0;
106
-}
107
-
108
-static inline u64 prof_ticks(struct task_struct *p)
109
-{
110
- u64 utime, stime;
111
-
112
- task_cputime(p, &utime, &stime);
113
-
114
- return utime + stime;
115
-}
116
-static inline u64 virt_ticks(struct task_struct *p)
117
-{
118
- u64 utime, stime;
119
-
120
- task_cputime(p, &utime, &stime);
121
-
122
- return utime;
147
+ return !(~pct->bases[CPUCLOCK_PROF].nextevt |
148
+ ~pct->bases[CPUCLOCK_VIRT].nextevt |
149
+ ~pct->bases[CPUCLOCK_SCHED].nextevt);
123150 }
124151
125152 static int
126153 posix_cpu_clock_getres(const clockid_t which_clock, struct timespec64 *tp)
127154 {
128
- int error = check_clock(which_clock);
155
+ int error = validate_clock_permissions(which_clock);
156
+
129157 if (!error) {
130158 tp->tv_sec = 0;
131159 tp->tv_nsec = ((NSEC_PER_SEC + HZ - 1) / HZ);
....@@ -142,40 +170,64 @@
142170 }
143171
144172 static int
145
-posix_cpu_clock_set(const clockid_t which_clock, const struct timespec64 *tp)
173
+posix_cpu_clock_set(const clockid_t clock, const struct timespec64 *tp)
146174 {
175
+ int error = validate_clock_permissions(clock);
176
+
147177 /*
148178 * You can never reset a CPU clock, but we check for other errors
149179 * in the call before failing with EPERM.
150180 */
151
- int error = check_clock(which_clock);
152
- if (error == 0) {
153
- error = -EPERM;
154
- }
155
- return error;
181
+ return error ? : -EPERM;
156182 }
157183
158
-
159184 /*
160
- * Sample a per-thread clock for the given task.
185
+ * Sample a per-thread clock for the given task. clkid is validated.
161186 */
162
-static int cpu_clock_sample(const clockid_t which_clock,
163
- struct task_struct *p, u64 *sample)
187
+static u64 cpu_clock_sample(const clockid_t clkid, struct task_struct *p)
164188 {
165
- switch (CPUCLOCK_WHICH(which_clock)) {
166
- default:
167
- return -EINVAL;
189
+ u64 utime, stime;
190
+
191
+ if (clkid == CPUCLOCK_SCHED)
192
+ return task_sched_runtime(p);
193
+
194
+ task_cputime(p, &utime, &stime);
195
+
196
+ switch (clkid) {
168197 case CPUCLOCK_PROF:
169
- *sample = prof_ticks(p);
170
- break;
198
+ return utime + stime;
171199 case CPUCLOCK_VIRT:
172
- *sample = virt_ticks(p);
173
- break;
174
- case CPUCLOCK_SCHED:
175
- *sample = task_sched_runtime(p);
176
- break;
200
+ return utime;
201
+ default:
202
+ WARN_ON_ONCE(1);
177203 }
178204 return 0;
205
+}
206
+
207
+static inline void store_samples(u64 *samples, u64 stime, u64 utime, u64 rtime)
208
+{
209
+ samples[CPUCLOCK_PROF] = stime + utime;
210
+ samples[CPUCLOCK_VIRT] = utime;
211
+ samples[CPUCLOCK_SCHED] = rtime;
212
+}
213
+
214
+static void task_sample_cputime(struct task_struct *p, u64 *samples)
215
+{
216
+ u64 stime, utime;
217
+
218
+ task_cputime(p, &utime, &stime);
219
+ store_samples(samples, stime, utime, p->se.sum_exec_runtime);
220
+}
221
+
222
+static void proc_sample_cputime_atomic(struct task_cputime_atomic *at,
223
+ u64 *samples)
224
+{
225
+ u64 stime, utime, rtime;
226
+
227
+ utime = atomic64_read(&at->utime);
228
+ stime = atomic64_read(&at->stime);
229
+ rtime = atomic64_read(&at->sum_exec_runtime);
230
+ store_samples(samples, stime, utime, rtime);
179231 }
180232
181233 /*
....@@ -193,29 +245,56 @@
193245 }
194246 }
195247
196
-static void update_gt_cputime(struct task_cputime_atomic *cputime_atomic, struct task_cputime *sum)
248
+static void update_gt_cputime(struct task_cputime_atomic *cputime_atomic,
249
+ struct task_cputime *sum)
197250 {
198251 __update_gt_cputime(&cputime_atomic->utime, sum->utime);
199252 __update_gt_cputime(&cputime_atomic->stime, sum->stime);
200253 __update_gt_cputime(&cputime_atomic->sum_exec_runtime, sum->sum_exec_runtime);
201254 }
202255
203
-/* Sample task_cputime_atomic values in "atomic_timers", store results in "times". */
204
-static inline void sample_cputime_atomic(struct task_cputime *times,
205
- struct task_cputime_atomic *atomic_times)
206
-{
207
- times->utime = atomic64_read(&atomic_times->utime);
208
- times->stime = atomic64_read(&atomic_times->stime);
209
- times->sum_exec_runtime = atomic64_read(&atomic_times->sum_exec_runtime);
210
-}
211
-
212
-void thread_group_cputimer(struct task_struct *tsk, struct task_cputime *times)
256
+/**
257
+ * thread_group_sample_cputime - Sample cputime for a given task
258
+ * @tsk: Task for which cputime needs to be started
259
+ * @samples: Storage for time samples
260
+ *
261
+ * Called from sys_getitimer() to calculate the expiry time of an active
262
+ * timer. That means group cputime accounting is already active. Called
263
+ * with task sighand lock held.
264
+ *
265
+ * Updates @times with an uptodate sample of the thread group cputimes.
266
+ */
267
+void thread_group_sample_cputime(struct task_struct *tsk, u64 *samples)
213268 {
214269 struct thread_group_cputimer *cputimer = &tsk->signal->cputimer;
215
- struct task_cputime sum;
270
+ struct posix_cputimers *pct = &tsk->signal->posix_cputimers;
271
+
272
+ WARN_ON_ONCE(!pct->timers_active);
273
+
274
+ proc_sample_cputime_atomic(&cputimer->cputime_atomic, samples);
275
+}
276
+
277
+/**
278
+ * thread_group_start_cputime - Start cputime and return a sample
279
+ * @tsk: Task for which cputime needs to be started
280
+ * @samples: Storage for time samples
281
+ *
282
+ * The thread group cputime accouting is avoided when there are no posix
283
+ * CPU timers armed. Before starting a timer it's required to check whether
284
+ * the time accounting is active. If not, a full update of the atomic
285
+ * accounting store needs to be done and the accounting enabled.
286
+ *
287
+ * Updates @times with an uptodate sample of the thread group cputimes.
288
+ */
289
+static void thread_group_start_cputime(struct task_struct *tsk, u64 *samples)
290
+{
291
+ struct thread_group_cputimer *cputimer = &tsk->signal->cputimer;
292
+ struct posix_cputimers *pct = &tsk->signal->posix_cputimers;
216293
217294 /* Check if cputimer isn't running. This is accessed without locking. */
218
- if (!READ_ONCE(cputimer->running)) {
295
+ if (!READ_ONCE(pct->timers_active)) {
296
+ struct task_cputime sum;
297
+
219298 /*
220299 * The POSIX timer interface allows for absolute time expiry
221300 * values through the TIMER_ABSTIME flag, therefore we have
....@@ -225,94 +304,70 @@
225304 update_gt_cputime(&cputimer->cputime_atomic, &sum);
226305
227306 /*
228
- * We're setting cputimer->running without a lock. Ensure
229
- * this only gets written to in one operation. We set
230
- * running after update_gt_cputime() as a small optimization,
231
- * but barriers are not required because update_gt_cputime()
307
+ * We're setting timers_active without a lock. Ensure this
308
+ * only gets written to in one operation. We set it after
309
+ * update_gt_cputime() as a small optimization, but
310
+ * barriers are not required because update_gt_cputime()
232311 * can handle concurrent updates.
233312 */
234
- WRITE_ONCE(cputimer->running, true);
313
+ WRITE_ONCE(pct->timers_active, true);
235314 }
236
- sample_cputime_atomic(times, &cputimer->cputime_atomic);
315
+ proc_sample_cputime_atomic(&cputimer->cputime_atomic, samples);
316
+}
317
+
318
+static void __thread_group_cputime(struct task_struct *tsk, u64 *samples)
319
+{
320
+ struct task_cputime ct;
321
+
322
+ thread_group_cputime(tsk, &ct);
323
+ store_samples(samples, ct.stime, ct.utime, ct.sum_exec_runtime);
237324 }
238325
239326 /*
240
- * Sample a process (thread group) clock for the given group_leader task.
241
- * Must be called with task sighand lock held for safe while_each_thread()
242
- * traversal.
327
+ * Sample a process (thread group) clock for the given task clkid. If the
328
+ * group's cputime accounting is already enabled, read the atomic
329
+ * store. Otherwise a full update is required. clkid is already validated.
243330 */
244
-static int cpu_clock_sample_group(const clockid_t which_clock,
245
- struct task_struct *p,
246
- u64 *sample)
331
+static u64 cpu_clock_sample_group(const clockid_t clkid, struct task_struct *p,
332
+ bool start)
247333 {
248
- struct task_cputime cputime;
334
+ struct thread_group_cputimer *cputimer = &p->signal->cputimer;
335
+ struct posix_cputimers *pct = &p->signal->posix_cputimers;
336
+ u64 samples[CPUCLOCK_MAX];
249337
250
- switch (CPUCLOCK_WHICH(which_clock)) {
251
- default:
252
- return -EINVAL;
253
- case CPUCLOCK_PROF:
254
- thread_group_cputime(p, &cputime);
255
- *sample = cputime.utime + cputime.stime;
256
- break;
257
- case CPUCLOCK_VIRT:
258
- thread_group_cputime(p, &cputime);
259
- *sample = cputime.utime;
260
- break;
261
- case CPUCLOCK_SCHED:
262
- thread_group_cputime(p, &cputime);
263
- *sample = cputime.sum_exec_runtime;
264
- break;
265
- }
266
- return 0;
267
-}
268
-
269
-static int posix_cpu_clock_get_task(struct task_struct *tsk,
270
- const clockid_t which_clock,
271
- struct timespec64 *tp)
272
-{
273
- int err = -EINVAL;
274
- u64 rtn;
275
-
276
- if (CPUCLOCK_PERTHREAD(which_clock)) {
277
- if (same_thread_group(tsk, current))
278
- err = cpu_clock_sample(which_clock, tsk, &rtn);
338
+ if (!READ_ONCE(pct->timers_active)) {
339
+ if (start)
340
+ thread_group_start_cputime(p, samples);
341
+ else
342
+ __thread_group_cputime(p, samples);
279343 } else {
280
- if (tsk == current || thread_group_leader(tsk))
281
- err = cpu_clock_sample_group(which_clock, tsk, &rtn);
344
+ proc_sample_cputime_atomic(&cputimer->cputime_atomic, samples);
282345 }
283346
284
- if (!err)
285
- *tp = ns_to_timespec64(rtn);
286
-
287
- return err;
347
+ return samples[clkid];
288348 }
289349
290
-
291
-static int posix_cpu_clock_get(const clockid_t which_clock, struct timespec64 *tp)
350
+static int posix_cpu_clock_get(const clockid_t clock, struct timespec64 *tp)
292351 {
293
- const pid_t pid = CPUCLOCK_PID(which_clock);
294
- int err = -EINVAL;
352
+ const clockid_t clkid = CPUCLOCK_WHICH(clock);
353
+ struct task_struct *tsk;
354
+ u64 t;
295355
296
- if (pid == 0) {
297
- /*
298
- * Special case constant value for our own clocks.
299
- * We don't have to do any lookup to find ourselves.
300
- */
301
- err = posix_cpu_clock_get_task(current, which_clock, tp);
302
- } else {
303
- /*
304
- * Find the given PID, and validate that the caller
305
- * should be able to see it.
306
- */
307
- struct task_struct *p;
308
- rcu_read_lock();
309
- p = find_task_by_vpid(pid);
310
- if (p)
311
- err = posix_cpu_clock_get_task(p, which_clock, tp);
356
+ rcu_read_lock();
357
+ tsk = pid_task(pid_for_clock(clock, true), clock_pid_type(clock));
358
+ if (!tsk) {
312359 rcu_read_unlock();
360
+ return -EINVAL;
313361 }
314362
315
- return err;
363
+ if (CPUCLOCK_PERTHREAD(clock))
364
+ t = cpu_clock_sample(clkid, tsk);
365
+ else
366
+ t = cpu_clock_sample_group(clkid, tsk, false);
367
+ rcu_read_unlock();
368
+
369
+ *tp = ns_to_timespec64(t);
370
+ return 0;
316371 }
317372
318373 /*
....@@ -322,44 +377,32 @@
322377 */
323378 static int posix_cpu_timer_create(struct k_itimer *new_timer)
324379 {
325
- int ret = 0;
326
- const pid_t pid = CPUCLOCK_PID(new_timer->it_clock);
327
- struct task_struct *p;
328
-
329
- if (CPUCLOCK_WHICH(new_timer->it_clock) >= CPUCLOCK_MAX)
330
- return -EINVAL;
331
-
332
- new_timer->kclock = &clock_posix_cpu;
333
-
334
- INIT_LIST_HEAD(&new_timer->it.cpu.entry);
380
+ static struct lock_class_key posix_cpu_timers_key;
381
+ struct pid *pid;
335382
336383 rcu_read_lock();
337
- if (CPUCLOCK_PERTHREAD(new_timer->it_clock)) {
338
- if (pid == 0) {
339
- p = current;
340
- } else {
341
- p = find_task_by_vpid(pid);
342
- if (p && !same_thread_group(p, current))
343
- p = NULL;
344
- }
345
- } else {
346
- if (pid == 0) {
347
- p = current->group_leader;
348
- } else {
349
- p = find_task_by_vpid(pid);
350
- if (p && !has_group_leader_pid(p))
351
- p = NULL;
352
- }
384
+ pid = pid_for_clock(new_timer->it_clock, false);
385
+ if (!pid) {
386
+ rcu_read_unlock();
387
+ return -EINVAL;
353388 }
354
- new_timer->it.cpu.task = p;
355
- if (p) {
356
- get_task_struct(p);
357
- } else {
358
- ret = -EINVAL;
359
- }
360
- rcu_read_unlock();
361389
362
- return ret;
390
+ /*
391
+ * If posix timer expiry is handled in task work context then
392
+ * timer::it_lock can be taken without disabling interrupts as all
393
+ * other locking happens in task context. This requires a seperate
394
+ * lock class key otherwise regular posix timer expiry would record
395
+ * the lock class being taken in interrupt context and generate a
396
+ * false positive warning.
397
+ */
398
+ if (IS_ENABLED(CONFIG_POSIX_CPU_TIMERS_TASK_WORK))
399
+ lockdep_set_class(&new_timer->it_lock, &posix_cpu_timers_key);
400
+
401
+ new_timer->kclock = &clock_posix_cpu;
402
+ timerqueue_init(&new_timer->it.cpu.node);
403
+ new_timer->it.cpu.pid = get_pid(pid);
404
+ rcu_read_unlock();
405
+ return 0;
363406 }
364407
365408 /*
....@@ -370,13 +413,16 @@
370413 */
371414 static int posix_cpu_timer_del(struct k_itimer *timer)
372415 {
373
- int ret = 0;
374
- unsigned long flags;
416
+ struct cpu_timer *ctmr = &timer->it.cpu;
375417 struct sighand_struct *sighand;
376
- struct task_struct *p = timer->it.cpu.task;
418
+ struct task_struct *p;
419
+ unsigned long flags;
420
+ int ret = 0;
377421
378
- if (WARN_ON_ONCE(!p))
379
- return -EINVAL;
422
+ rcu_read_lock();
423
+ p = cpu_timer_task_rcu(timer);
424
+ if (!p)
425
+ goto out;
380426
381427 /*
382428 * Protect against sighand release/switch in exit/exec and process/
....@@ -385,44 +431,51 @@
385431 sighand = lock_task_sighand(p, &flags);
386432 if (unlikely(sighand == NULL)) {
387433 /*
388
- * We raced with the reaping of the task.
389
- * The deletion should have cleared us off the list.
434
+ * This raced with the reaping of the task. The exit cleanup
435
+ * should have removed this timer from the timer queue.
390436 */
391
- WARN_ON_ONCE(!list_empty(&timer->it.cpu.entry));
437
+ WARN_ON_ONCE(ctmr->head || timerqueue_node_queued(&ctmr->node));
392438 } else {
393439 if (timer->it.cpu.firing)
394440 ret = TIMER_RETRY;
395441 else
396
- list_del(&timer->it.cpu.entry);
442
+ cpu_timer_dequeue(ctmr);
397443
398444 unlock_task_sighand(p, &flags);
399445 }
400446
447
+out:
448
+ rcu_read_unlock();
401449 if (!ret)
402
- put_task_struct(p);
450
+ put_pid(ctmr->pid);
403451
404452 return ret;
405453 }
406454
407
-static void cleanup_timers_list(struct list_head *head)
455
+static void cleanup_timerqueue(struct timerqueue_head *head)
408456 {
409
- struct cpu_timer_list *timer, *next;
457
+ struct timerqueue_node *node;
458
+ struct cpu_timer *ctmr;
410459
411
- list_for_each_entry_safe(timer, next, head, entry)
412
- list_del_init(&timer->entry);
460
+ while ((node = timerqueue_getnext(head))) {
461
+ timerqueue_del(head, node);
462
+ ctmr = container_of(node, struct cpu_timer, node);
463
+ ctmr->head = NULL;
464
+ }
413465 }
414466
415467 /*
416
- * Clean out CPU timers still ticking when a thread exited. The task
417
- * pointer is cleared, and the expiry time is replaced with the residual
418
- * time for later timer_gettime calls to return.
468
+ * Clean out CPU timers which are still armed when a thread exits. The
469
+ * timers are only removed from the list. No other updates are done. The
470
+ * corresponding posix timers are still accessible, but cannot be rearmed.
471
+ *
419472 * This must be called with the siglock held.
420473 */
421
-static void cleanup_timers(struct list_head *head)
474
+static void cleanup_timers(struct posix_cputimers *pct)
422475 {
423
- cleanup_timers_list(head);
424
- cleanup_timers_list(++head);
425
- cleanup_timers_list(++head);
476
+ cleanup_timerqueue(&pct->bases[CPUCLOCK_PROF].tqhead);
477
+ cleanup_timerqueue(&pct->bases[CPUCLOCK_VIRT].tqhead);
478
+ cleanup_timerqueue(&pct->bases[CPUCLOCK_SCHED].tqhead);
426479 }
427480
428481 /*
....@@ -432,76 +485,45 @@
432485 */
433486 void posix_cpu_timers_exit(struct task_struct *tsk)
434487 {
435
- cleanup_timers(tsk->cpu_timers);
488
+ cleanup_timers(&tsk->posix_cputimers);
436489 }
437490 void posix_cpu_timers_exit_group(struct task_struct *tsk)
438491 {
439
- cleanup_timers(tsk->signal->cpu_timers);
440
-}
441
-
442
-static inline int expires_gt(u64 expires, u64 new_exp)
443
-{
444
- return expires == 0 || expires > new_exp;
492
+ cleanup_timers(&tsk->signal->posix_cputimers);
445493 }
446494
447495 /*
448496 * Insert the timer on the appropriate list before any timers that
449497 * expire later. This must be called with the sighand lock held.
450498 */
451
-static void arm_timer(struct k_itimer *timer)
499
+static void arm_timer(struct k_itimer *timer, struct task_struct *p)
452500 {
453
- struct task_struct *p = timer->it.cpu.task;
454
- struct list_head *head, *listpos;
455
- struct task_cputime *cputime_expires;
456
- struct cpu_timer_list *const nt = &timer->it.cpu;
457
- struct cpu_timer_list *next;
501
+ int clkidx = CPUCLOCK_WHICH(timer->it_clock);
502
+ struct cpu_timer *ctmr = &timer->it.cpu;
503
+ u64 newexp = cpu_timer_getexpires(ctmr);
504
+ struct posix_cputimer_base *base;
458505
459
- if (CPUCLOCK_PERTHREAD(timer->it_clock)) {
460
- head = p->cpu_timers;
461
- cputime_expires = &p->cputime_expires;
462
- } else {
463
- head = p->signal->cpu_timers;
464
- cputime_expires = &p->signal->cputime_expires;
465
- }
466
- head += CPUCLOCK_WHICH(timer->it_clock);
506
+ if (CPUCLOCK_PERTHREAD(timer->it_clock))
507
+ base = p->posix_cputimers.bases + clkidx;
508
+ else
509
+ base = p->signal->posix_cputimers.bases + clkidx;
467510
468
- listpos = head;
469
- list_for_each_entry(next, head, entry) {
470
- if (nt->expires < next->expires)
471
- break;
472
- listpos = &next->entry;
473
- }
474
- list_add(&nt->entry, listpos);
511
+ if (!cpu_timer_enqueue(&base->tqhead, ctmr))
512
+ return;
475513
476
- if (listpos == head) {
477
- u64 exp = nt->expires;
514
+ /*
515
+ * We are the new earliest-expiring POSIX 1.b timer, hence
516
+ * need to update expiration cache. Take into account that
517
+ * for process timers we share expiration cache with itimers
518
+ * and RLIMIT_CPU and for thread timers with RLIMIT_RTTIME.
519
+ */
520
+ if (newexp < base->nextevt)
521
+ base->nextevt = newexp;
478522
479
- /*
480
- * We are the new earliest-expiring POSIX 1.b timer, hence
481
- * need to update expiration cache. Take into account that
482
- * for process timers we share expiration cache with itimers
483
- * and RLIMIT_CPU and for thread timers with RLIMIT_RTTIME.
484
- */
485
-
486
- switch (CPUCLOCK_WHICH(timer->it_clock)) {
487
- case CPUCLOCK_PROF:
488
- if (expires_gt(cputime_expires->prof_exp, exp))
489
- cputime_expires->prof_exp = exp;
490
- break;
491
- case CPUCLOCK_VIRT:
492
- if (expires_gt(cputime_expires->virt_exp, exp))
493
- cputime_expires->virt_exp = exp;
494
- break;
495
- case CPUCLOCK_SCHED:
496
- if (expires_gt(cputime_expires->sched_exp, exp))
497
- cputime_expires->sched_exp = exp;
498
- break;
499
- }
500
- if (CPUCLOCK_PERTHREAD(timer->it_clock))
501
- tick_dep_set_task(p, TICK_DEP_BIT_POSIX_TIMER);
502
- else
503
- tick_dep_set_signal(p->signal, TICK_DEP_BIT_POSIX_TIMER);
504
- }
523
+ if (CPUCLOCK_PERTHREAD(timer->it_clock))
524
+ tick_dep_set_task(p, TICK_DEP_BIT_POSIX_TIMER);
525
+ else
526
+ tick_dep_set_signal(p->signal, TICK_DEP_BIT_POSIX_TIMER);
505527 }
506528
507529 /*
....@@ -509,24 +531,26 @@
509531 */
510532 static void cpu_timer_fire(struct k_itimer *timer)
511533 {
534
+ struct cpu_timer *ctmr = &timer->it.cpu;
535
+
512536 if ((timer->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE) {
513537 /*
514538 * User don't want any signal.
515539 */
516
- timer->it.cpu.expires = 0;
540
+ cpu_timer_setexpires(ctmr, 0);
517541 } else if (unlikely(timer->sigq == NULL)) {
518542 /*
519543 * This a special case for clock_nanosleep,
520544 * not a normal timer from sys_timer_create.
521545 */
522546 wake_up_process(timer->it_process);
523
- timer->it.cpu.expires = 0;
524
- } else if (timer->it.cpu.incr == 0) {
547
+ cpu_timer_setexpires(ctmr, 0);
548
+ } else if (!timer->it_interval) {
525549 /*
526550 * One-shot timer. Clear it as soon as it's fired.
527551 */
528552 posix_timer_event(timer, 0);
529
- timer->it.cpu.expires = 0;
553
+ cpu_timer_setexpires(ctmr, 0);
530554 } else if (posix_timer_event(timer, ++timer->it_requeue_pending)) {
531555 /*
532556 * The signal did not get queued because the signal
....@@ -540,33 +564,6 @@
540564 }
541565
542566 /*
543
- * Sample a process (thread group) timer for the given group_leader task.
544
- * Must be called with task sighand lock held for safe while_each_thread()
545
- * traversal.
546
- */
547
-static int cpu_timer_sample_group(const clockid_t which_clock,
548
- struct task_struct *p, u64 *sample)
549
-{
550
- struct task_cputime cputime;
551
-
552
- thread_group_cputimer(p, &cputime);
553
- switch (CPUCLOCK_WHICH(which_clock)) {
554
- default:
555
- return -EINVAL;
556
- case CPUCLOCK_PROF:
557
- *sample = cputime.utime + cputime.stime;
558
- break;
559
- case CPUCLOCK_VIRT:
560
- *sample = cputime.utime;
561
- break;
562
- case CPUCLOCK_SCHED:
563
- *sample = cputime.sum_exec_runtime;
564
- break;
565
- }
566
- return 0;
567
-}
568
-
569
-/*
570567 * Guts of sys_timer_settime for CPU timers.
571568 * This is called with the timer locked and interrupts disabled.
572569 * If we return TIMER_RETRY, it's necessary to release the timer's lock
....@@ -575,14 +572,24 @@
575572 static int posix_cpu_timer_set(struct k_itimer *timer, int timer_flags,
576573 struct itimerspec64 *new, struct itimerspec64 *old)
577574 {
578
- unsigned long flags;
579
- struct sighand_struct *sighand;
580
- struct task_struct *p = timer->it.cpu.task;
575
+ clockid_t clkid = CPUCLOCK_WHICH(timer->it_clock);
581576 u64 old_expires, new_expires, old_incr, val;
582
- int ret;
577
+ struct cpu_timer *ctmr = &timer->it.cpu;
578
+ struct sighand_struct *sighand;
579
+ struct task_struct *p;
580
+ unsigned long flags;
581
+ int ret = 0;
583582
584
- if (WARN_ON_ONCE(!p))
585
- return -EINVAL;
583
+ rcu_read_lock();
584
+ p = cpu_timer_task_rcu(timer);
585
+ if (!p) {
586
+ /*
587
+ * If p has just been reaped, we can no
588
+ * longer get any information about it at all.
589
+ */
590
+ rcu_read_unlock();
591
+ return -ESRCH;
592
+ }
586593
587594 /*
588595 * Use the to_ktime conversion because that clamps the maximum
....@@ -600,21 +607,22 @@
600607 * longer get any information about it at all.
601608 */
602609 if (unlikely(sighand == NULL)) {
610
+ rcu_read_unlock();
603611 return -ESRCH;
604612 }
605613
606614 /*
607615 * Disarm any old timer after extracting its expiry time.
608616 */
617
+ old_incr = timer->it_interval;
618
+ old_expires = cpu_timer_getexpires(ctmr);
609619
610
- ret = 0;
611
- old_incr = timer->it.cpu.incr;
612
- old_expires = timer->it.cpu.expires;
613620 if (unlikely(timer->it.cpu.firing)) {
614621 timer->it.cpu.firing = -1;
615622 ret = TIMER_RETRY;
616
- } else
617
- list_del_init(&timer->it.cpu.entry);
623
+ } else {
624
+ cpu_timer_dequeue(ctmr);
625
+ }
618626
619627 /*
620628 * We need to sample the current value to convert the new
....@@ -624,11 +632,10 @@
624632 * times (in arm_timer). With an absolute time, we must
625633 * check if it's already passed. In short, we need a sample.
626634 */
627
- if (CPUCLOCK_PERTHREAD(timer->it_clock)) {
628
- cpu_clock_sample(timer->it_clock, p, &val);
629
- } else {
630
- cpu_timer_sample_group(timer->it_clock, p, &val);
631
- }
635
+ if (CPUCLOCK_PERTHREAD(timer->it_clock))
636
+ val = cpu_clock_sample(clkid, p);
637
+ else
638
+ val = cpu_clock_sample_group(clkid, p, true);
632639
633640 if (old) {
634641 if (old_expires == 0) {
....@@ -636,18 +643,16 @@
636643 old->it_value.tv_nsec = 0;
637644 } else {
638645 /*
639
- * Update the timer in case it has
640
- * overrun already. If it has,
641
- * we'll report it as having overrun
642
- * and with the next reloaded timer
643
- * already ticking, though we are
644
- * swallowing that pending
645
- * notification here to install the
646
- * new setting.
646
+ * Update the timer in case it has overrun already.
647
+ * If it has, we'll report it as having overrun and
648
+ * with the next reloaded timer already ticking,
649
+ * though we are swallowing that pending
650
+ * notification here to install the new setting.
647651 */
648
- bump_cpu_timer(timer, val);
649
- if (val < timer->it.cpu.expires) {
650
- old_expires = timer->it.cpu.expires - val;
652
+ u64 exp = bump_cpu_timer(timer, val);
653
+
654
+ if (val < exp) {
655
+ old_expires = exp - val;
651656 old->it_value = ns_to_timespec64(old_expires);
652657 } else {
653658 old->it_value.tv_nsec = 1;
....@@ -676,9 +681,9 @@
676681 * For a timer with no notification action, we don't actually
677682 * arm the timer (we'll just fake it for timer_gettime).
678683 */
679
- timer->it.cpu.expires = new_expires;
684
+ cpu_timer_setexpires(ctmr, new_expires);
680685 if (new_expires != 0 && val < new_expires) {
681
- arm_timer(timer);
686
+ arm_timer(timer, p);
682687 }
683688
684689 unlock_task_sighand(p, &flags);
....@@ -686,8 +691,7 @@
686691 * Install the new reload setting, and
687692 * set up the signal and overrun bookkeeping.
688693 */
689
- timer->it.cpu.incr = timespec64_to_ns(&new->it_interval);
690
- timer->it_interval = ns_to_ktime(timer->it.cpu.incr);
694
+ timer->it_interval = timespec64_to_ktime(new->it_interval);
691695
692696 /*
693697 * This acts as a modification timestamp for the timer,
....@@ -710,6 +714,7 @@
710714
711715 ret = 0;
712716 out:
717
+ rcu_read_unlock();
713718 if (old)
714719 old->it_interval = ns_to_timespec64(old_incr);
715720
....@@ -718,51 +723,34 @@
718723
719724 static void posix_cpu_timer_get(struct k_itimer *timer, struct itimerspec64 *itp)
720725 {
721
- struct task_struct *p = timer->it.cpu.task;
722
- u64 now;
726
+ clockid_t clkid = CPUCLOCK_WHICH(timer->it_clock);
727
+ struct cpu_timer *ctmr = &timer->it.cpu;
728
+ u64 now, expires = cpu_timer_getexpires(ctmr);
729
+ struct task_struct *p;
723730
724
- if (WARN_ON_ONCE(!p))
725
- return;
731
+ rcu_read_lock();
732
+ p = cpu_timer_task_rcu(timer);
733
+ if (!p)
734
+ goto out;
726735
727736 /*
728737 * Easy part: convert the reload time.
729738 */
730
- itp->it_interval = ns_to_timespec64(timer->it.cpu.incr);
739
+ itp->it_interval = ktime_to_timespec64(timer->it_interval);
731740
732
- if (!timer->it.cpu.expires)
733
- return;
741
+ if (!expires)
742
+ goto out;
734743
735744 /*
736745 * Sample the clock to take the difference with the expiry time.
737746 */
738
- if (CPUCLOCK_PERTHREAD(timer->it_clock)) {
739
- cpu_clock_sample(timer->it_clock, p, &now);
740
- } else {
741
- struct sighand_struct *sighand;
742
- unsigned long flags;
747
+ if (CPUCLOCK_PERTHREAD(timer->it_clock))
748
+ now = cpu_clock_sample(clkid, p);
749
+ else
750
+ now = cpu_clock_sample_group(clkid, p, false);
743751
744
- /*
745
- * Protect against sighand release/switch in exit/exec and
746
- * also make timer sampling safe if it ends up calling
747
- * thread_group_cputime().
748
- */
749
- sighand = lock_task_sighand(p, &flags);
750
- if (unlikely(sighand == NULL)) {
751
- /*
752
- * The process has been reaped.
753
- * We can't even collect a sample any more.
754
- * Call the timer disarmed, nothing else to do.
755
- */
756
- timer->it.cpu.expires = 0;
757
- return;
758
- } else {
759
- cpu_timer_sample_group(timer->it_clock, p, &now);
760
- unlock_task_sighand(p, &flags);
761
- }
762
- }
763
-
764
- if (now < timer->it.cpu.expires) {
765
- itp->it_value = ns_to_timespec64(timer->it.cpu.expires - now);
752
+ if (now < expires) {
753
+ itp->it_value = ns_to_timespec64(expires - now);
766754 } else {
767755 /*
768756 * The timer should have expired already, but the firing
....@@ -771,28 +759,48 @@
771759 itp->it_value.tv_nsec = 1;
772760 itp->it_value.tv_sec = 0;
773761 }
762
+out:
763
+ rcu_read_unlock();
774764 }
775765
776
-static unsigned long long
777
-check_timers_list(struct list_head *timers,
778
- struct list_head *firing,
779
- unsigned long long curr)
766
+#define MAX_COLLECTED 20
767
+
768
+static u64 collect_timerqueue(struct timerqueue_head *head,
769
+ struct list_head *firing, u64 now)
780770 {
781
- int maxfire = 20;
771
+ struct timerqueue_node *next;
772
+ int i = 0;
782773
783
- while (!list_empty(timers)) {
784
- struct cpu_timer_list *t;
774
+ while ((next = timerqueue_getnext(head))) {
775
+ struct cpu_timer *ctmr;
776
+ u64 expires;
785777
786
- t = list_first_entry(timers, struct cpu_timer_list, entry);
778
+ ctmr = container_of(next, struct cpu_timer, node);
779
+ expires = cpu_timer_getexpires(ctmr);
780
+ /* Limit the number of timers to expire at once */
781
+ if (++i == MAX_COLLECTED || now < expires)
782
+ return expires;
787783
788
- if (!--maxfire || curr < t->expires)
789
- return t->expires;
790
-
791
- t->firing = 1;
792
- list_move_tail(&t->entry, firing);
784
+ ctmr->firing = 1;
785
+ /* See posix_cpu_timer_wait_running() */
786
+ rcu_assign_pointer(ctmr->handling, current);
787
+ cpu_timer_dequeue(ctmr);
788
+ list_add_tail(&ctmr->elist, firing);
793789 }
794790
795
- return 0;
791
+ return U64_MAX;
792
+}
793
+
794
+static void collect_posix_cputimers(struct posix_cputimers *pct, u64 *samples,
795
+ struct list_head *firing)
796
+{
797
+ struct posix_cputimer_base *base = pct->bases;
798
+ int i;
799
+
800
+ for (i = 0; i < CPUCLOCK_MAX; i++, base++) {
801
+ base->nextevt = collect_timerqueue(&base->tqhead, firing,
802
+ samples[i]);
803
+ }
796804 }
797805
798806 static inline void check_dl_overrun(struct task_struct *tsk)
....@@ -803,6 +811,20 @@
803811 }
804812 }
805813
814
+static bool check_rlimit(u64 time, u64 limit, int signo, bool rt, bool hard)
815
+{
816
+ if (time < limit)
817
+ return false;
818
+
819
+ if (print_fatal_signals) {
820
+ pr_info("%s Watchdog Timeout (%s): %s[%d]\n",
821
+ rt ? "RT" : "CPU", hard ? "hard" : "soft",
822
+ current->comm, task_pid_nr(current));
823
+ }
824
+ __group_send_sig_info(signo, SEND_SIG_PRIV, current);
825
+ return true;
826
+}
827
+
806828 /*
807829 * Check for any per-thread CPU timers that have fired and move them off
808830 * the tsk->cpu_timers[N] list onto the firing list. Here we update the
....@@ -811,76 +833,50 @@
811833 static void check_thread_timers(struct task_struct *tsk,
812834 struct list_head *firing)
813835 {
814
- struct list_head *timers = tsk->cpu_timers;
815
- struct task_cputime *tsk_expires = &tsk->cputime_expires;
816
- u64 expires;
836
+ struct posix_cputimers *pct = &tsk->posix_cputimers;
837
+ u64 samples[CPUCLOCK_MAX];
817838 unsigned long soft;
818839
819840 if (dl_task(tsk))
820841 check_dl_overrun(tsk);
821842
822
- /*
823
- * If cputime_expires is zero, then there are no active
824
- * per thread CPU timers.
825
- */
826
- if (task_cputime_zero(&tsk->cputime_expires))
843
+ if (expiry_cache_is_inactive(pct))
827844 return;
828845
829
- expires = check_timers_list(timers, firing, prof_ticks(tsk));
830
- tsk_expires->prof_exp = expires;
831
-
832
- expires = check_timers_list(++timers, firing, virt_ticks(tsk));
833
- tsk_expires->virt_exp = expires;
834
-
835
- tsk_expires->sched_exp = check_timers_list(++timers, firing,
836
- tsk->se.sum_exec_runtime);
846
+ task_sample_cputime(tsk, samples);
847
+ collect_posix_cputimers(pct, samples, firing);
837848
838849 /*
839850 * Check for the special case thread timers.
840851 */
841852 soft = task_rlimit(tsk, RLIMIT_RTTIME);
842853 if (soft != RLIM_INFINITY) {
854
+ /* Task RT timeout is accounted in jiffies. RTTIME is usec */
855
+ unsigned long rttime = tsk->rt.timeout * (USEC_PER_SEC / HZ);
843856 unsigned long hard = task_rlimit_max(tsk, RLIMIT_RTTIME);
844857
858
+ /* At the hard limit, send SIGKILL. No further action. */
845859 if (hard != RLIM_INFINITY &&
846
- tsk->rt.timeout > DIV_ROUND_UP(hard, USEC_PER_SEC/HZ)) {
847
- /*
848
- * At the hard limit, we just die.
849
- * No need to calculate anything else now.
850
- */
851
- if (print_fatal_signals) {
852
- pr_info("CPU Watchdog Timeout (hard): %s[%d]\n",
853
- tsk->comm, task_pid_nr(tsk));
854
- }
855
- __group_send_sig_info(SIGKILL, SEND_SIG_PRIV, tsk);
860
+ check_rlimit(rttime, hard, SIGKILL, true, true))
856861 return;
857
- }
858
- if (tsk->rt.timeout > DIV_ROUND_UP(soft, USEC_PER_SEC/HZ)) {
859
- /*
860
- * At the soft limit, send a SIGXCPU every second.
861
- */
862
- if (soft < hard) {
863
- soft += USEC_PER_SEC;
864
- tsk->signal->rlim[RLIMIT_RTTIME].rlim_cur =
865
- soft;
866
- }
867
- if (print_fatal_signals) {
868
- pr_info("RT Watchdog Timeout (soft): %s[%d]\n",
869
- tsk->comm, task_pid_nr(tsk));
870
- }
871
- __group_send_sig_info(SIGXCPU, SEND_SIG_PRIV, tsk);
862
+
863
+ /* At the soft limit, send a SIGXCPU every second */
864
+ if (check_rlimit(rttime, soft, SIGXCPU, true, false)) {
865
+ soft += USEC_PER_SEC;
866
+ tsk->signal->rlim[RLIMIT_RTTIME].rlim_cur = soft;
872867 }
873868 }
874
- if (task_cputime_zero(tsk_expires))
869
+
870
+ if (expiry_cache_is_inactive(pct))
875871 tick_dep_clear_task(tsk, TICK_DEP_BIT_POSIX_TIMER);
876872 }
877873
878874 static inline void stop_process_timers(struct signal_struct *sig)
879875 {
880
- struct thread_group_cputimer *cputimer = &sig->cputimer;
876
+ struct posix_cputimers *pct = &sig->posix_cputimers;
881877
882
- /* Turn off cputimer->running. This is done without locking. */
883
- WRITE_ONCE(cputimer->running, false);
878
+ /* Turn off the active flag. This is done without locking. */
879
+ WRITE_ONCE(pct->timers_active, false);
884880 tick_dep_clear_signal(sig, TICK_DEP_BIT_POSIX_TIMER);
885881 }
886882
....@@ -902,7 +898,7 @@
902898 __group_send_sig_info(signo, SEND_SIG_PRIV, tsk);
903899 }
904900
905
- if (it->expires && (!*expires || it->expires < *expires))
901
+ if (it->expires && it->expires < *expires)
906902 *expires = it->expires;
907903 }
908904
....@@ -915,90 +911,69 @@
915911 struct list_head *firing)
916912 {
917913 struct signal_struct *const sig = tsk->signal;
918
- u64 utime, ptime, virt_expires, prof_expires;
919
- u64 sum_sched_runtime, sched_expires;
920
- struct list_head *timers = sig->cpu_timers;
921
- struct task_cputime cputime;
914
+ struct posix_cputimers *pct = &sig->posix_cputimers;
915
+ u64 samples[CPUCLOCK_MAX];
922916 unsigned long soft;
923917
924
- if (dl_task(tsk))
925
- check_dl_overrun(tsk);
926
-
927918 /*
928
- * If cputimer is not running, then there are no active
929
- * process wide timers (POSIX 1.b, itimers, RLIMIT_CPU).
919
+ * If there are no active process wide timers (POSIX 1.b, itimers,
920
+ * RLIMIT_CPU) nothing to check. Also skip the process wide timer
921
+ * processing when there is already another task handling them.
930922 */
931
- if (!READ_ONCE(tsk->signal->cputimer.running))
923
+ if (!READ_ONCE(pct->timers_active) || pct->expiry_active)
932924 return;
933925
934
- /*
926
+ /*
935927 * Signify that a thread is checking for process timers.
936928 * Write access to this field is protected by the sighand lock.
937929 */
938
- sig->cputimer.checking_timer = true;
930
+ pct->expiry_active = true;
939931
940932 /*
941
- * Collect the current process totals.
933
+ * Collect the current process totals. Group accounting is active
934
+ * so the sample can be taken directly.
942935 */
943
- thread_group_cputimer(tsk, &cputime);
944
- utime = cputime.utime;
945
- ptime = utime + cputime.stime;
946
- sum_sched_runtime = cputime.sum_exec_runtime;
947
-
948
- prof_expires = check_timers_list(timers, firing, ptime);
949
- virt_expires = check_timers_list(++timers, firing, utime);
950
- sched_expires = check_timers_list(++timers, firing, sum_sched_runtime);
936
+ proc_sample_cputime_atomic(&sig->cputimer.cputime_atomic, samples);
937
+ collect_posix_cputimers(pct, samples, firing);
951938
952939 /*
953940 * Check for the special case process timers.
954941 */
955
- check_cpu_itimer(tsk, &sig->it[CPUCLOCK_PROF], &prof_expires, ptime,
956
- SIGPROF);
957
- check_cpu_itimer(tsk, &sig->it[CPUCLOCK_VIRT], &virt_expires, utime,
958
- SIGVTALRM);
942
+ check_cpu_itimer(tsk, &sig->it[CPUCLOCK_PROF],
943
+ &pct->bases[CPUCLOCK_PROF].nextevt,
944
+ samples[CPUCLOCK_PROF], SIGPROF);
945
+ check_cpu_itimer(tsk, &sig->it[CPUCLOCK_VIRT],
946
+ &pct->bases[CPUCLOCK_VIRT].nextevt,
947
+ samples[CPUCLOCK_VIRT], SIGVTALRM);
948
+
959949 soft = task_rlimit(tsk, RLIMIT_CPU);
960950 if (soft != RLIM_INFINITY) {
961
- unsigned long psecs = div_u64(ptime, NSEC_PER_SEC);
951
+ /* RLIMIT_CPU is in seconds. Samples are nanoseconds */
962952 unsigned long hard = task_rlimit_max(tsk, RLIMIT_CPU);
963
- u64 x;
964
- if (psecs >= hard) {
965
- /*
966
- * At the hard limit, we just die.
967
- * No need to calculate anything else now.
968
- */
969
- if (print_fatal_signals) {
970
- pr_info("RT Watchdog Timeout (hard): %s[%d]\n",
971
- tsk->comm, task_pid_nr(tsk));
972
- }
973
- __group_send_sig_info(SIGKILL, SEND_SIG_PRIV, tsk);
953
+ u64 ptime = samples[CPUCLOCK_PROF];
954
+ u64 softns = (u64)soft * NSEC_PER_SEC;
955
+ u64 hardns = (u64)hard * NSEC_PER_SEC;
956
+
957
+ /* At the hard limit, send SIGKILL. No further action. */
958
+ if (hard != RLIM_INFINITY &&
959
+ check_rlimit(ptime, hardns, SIGKILL, false, true))
974960 return;
961
+
962
+ /* At the soft limit, send a SIGXCPU every second */
963
+ if (check_rlimit(ptime, softns, SIGXCPU, false, false)) {
964
+ sig->rlim[RLIMIT_CPU].rlim_cur = soft + 1;
965
+ softns += NSEC_PER_SEC;
975966 }
976
- if (psecs >= soft) {
977
- /*
978
- * At the soft limit, send a SIGXCPU every second.
979
- */
980
- if (print_fatal_signals) {
981
- pr_info("CPU Watchdog Timeout (soft): %s[%d]\n",
982
- tsk->comm, task_pid_nr(tsk));
983
- }
984
- __group_send_sig_info(SIGXCPU, SEND_SIG_PRIV, tsk);
985
- if (soft < hard) {
986
- soft++;
987
- sig->rlim[RLIMIT_CPU].rlim_cur = soft;
988
- }
989
- }
990
- x = soft * NSEC_PER_SEC;
991
- if (!prof_expires || x < prof_expires)
992
- prof_expires = x;
967
+
968
+ /* Update the expiry cache */
969
+ if (softns < pct->bases[CPUCLOCK_PROF].nextevt)
970
+ pct->bases[CPUCLOCK_PROF].nextevt = softns;
993971 }
994972
995
- sig->cputime_expires.prof_exp = prof_expires;
996
- sig->cputime_expires.virt_exp = virt_expires;
997
- sig->cputime_expires.sched_exp = sched_expires;
998
- if (task_cputime_zero(&sig->cputime_expires))
973
+ if (expiry_cache_is_inactive(pct))
999974 stop_process_timers(sig);
1000975
1001
- sig->cputimer.checking_timer = false;
976
+ pct->expiry_active = false;
1002977 }
1003978
1004979 /*
....@@ -1007,78 +982,60 @@
1007982 */
1008983 static void posix_cpu_timer_rearm(struct k_itimer *timer)
1009984 {
1010
- struct task_struct *p = timer->it.cpu.task;
985
+ clockid_t clkid = CPUCLOCK_WHICH(timer->it_clock);
986
+ struct task_struct *p;
1011987 struct sighand_struct *sighand;
1012988 unsigned long flags;
1013989 u64 now;
1014990
1015
- if (WARN_ON_ONCE(!p))
1016
- return;
991
+ rcu_read_lock();
992
+ p = cpu_timer_task_rcu(timer);
993
+ if (!p)
994
+ goto out;
995
+
996
+ /* Protect timer list r/w in arm_timer() */
997
+ sighand = lock_task_sighand(p, &flags);
998
+ if (unlikely(sighand == NULL))
999
+ goto out;
10171000
10181001 /*
10191002 * Fetch the current sample and update the timer's expiry time.
10201003 */
1021
- if (CPUCLOCK_PERTHREAD(timer->it_clock)) {
1022
- cpu_clock_sample(timer->it_clock, p, &now);
1023
- bump_cpu_timer(timer, now);
1024
- if (unlikely(p->exit_state))
1025
- return;
1004
+ if (CPUCLOCK_PERTHREAD(timer->it_clock))
1005
+ now = cpu_clock_sample(clkid, p);
1006
+ else
1007
+ now = cpu_clock_sample_group(clkid, p, true);
10261008
1027
- /* Protect timer list r/w in arm_timer() */
1028
- sighand = lock_task_sighand(p, &flags);
1029
- if (!sighand)
1030
- return;
1031
- } else {
1032
- /*
1033
- * Protect arm_timer() and timer sampling in case of call to
1034
- * thread_group_cputime().
1035
- */
1036
- sighand = lock_task_sighand(p, &flags);
1037
- if (unlikely(sighand == NULL)) {
1038
- /*
1039
- * The process has been reaped.
1040
- * We can't even collect a sample any more.
1041
- */
1042
- timer->it.cpu.expires = 0;
1043
- return;
1044
- } else if (unlikely(p->exit_state) && thread_group_empty(p)) {
1045
- /* If the process is dying, no need to rearm */
1046
- goto unlock;
1047
- }
1048
- cpu_timer_sample_group(timer->it_clock, p, &now);
1049
- bump_cpu_timer(timer, now);
1050
- /* Leave the sighand locked for the call below. */
1051
- }
1009
+ bump_cpu_timer(timer, now);
10521010
10531011 /*
10541012 * Now re-arm for the new expiry time.
10551013 */
1056
- arm_timer(timer);
1057
-unlock:
1014
+ arm_timer(timer, p);
10581015 unlock_task_sighand(p, &flags);
1016
+out:
1017
+ rcu_read_unlock();
10591018 }
10601019
10611020 /**
1062
- * task_cputime_expired - Compare two task_cputime entities.
1021
+ * task_cputimers_expired - Check whether posix CPU timers are expired
10631022 *
1064
- * @sample: The task_cputime structure to be checked for expiration.
1065
- * @expires: Expiration times, against which @sample will be checked.
1023
+ * @samples: Array of current samples for the CPUCLOCK clocks
1024
+ * @pct: Pointer to a posix_cputimers container
10661025 *
1067
- * Checks @sample against @expires to see if any field of @sample has expired.
1068
- * Returns true if any field of the former is greater than the corresponding
1069
- * field of the latter if the latter field is set. Otherwise returns false.
1026
+ * Returns true if any member of @samples is greater than the corresponding
1027
+ * member of @pct->bases[CLK].nextevt. False otherwise
10701028 */
1071
-static inline int task_cputime_expired(const struct task_cputime *sample,
1072
- const struct task_cputime *expires)
1029
+static inline bool
1030
+task_cputimers_expired(const u64 *samples, struct posix_cputimers *pct)
10731031 {
1074
- if (expires->utime && sample->utime >= expires->utime)
1075
- return 1;
1076
- if (expires->stime && sample->utime + sample->stime >= expires->stime)
1077
- return 1;
1078
- if (expires->sum_exec_runtime != 0 &&
1079
- sample->sum_exec_runtime >= expires->sum_exec_runtime)
1080
- return 1;
1081
- return 0;
1032
+ int i;
1033
+
1034
+ for (i = 0; i < CPUCLOCK_MAX; i++) {
1035
+ if (samples[i] >= pct->bases[i].nextevt)
1036
+ return true;
1037
+ }
1038
+ return false;
10821039 }
10831040
10841041 /**
....@@ -1091,83 +1048,279 @@
10911048 * timers and compare them with the corresponding expiration times. Return
10921049 * true if a timer has expired, else return false.
10931050 */
1094
-static inline int fastpath_timer_check(struct task_struct *tsk)
1051
+static inline bool fastpath_timer_check(struct task_struct *tsk)
10951052 {
1053
+ struct posix_cputimers *pct = &tsk->posix_cputimers;
10961054 struct signal_struct *sig;
10971055
1098
- if (!task_cputime_zero(&tsk->cputime_expires)) {
1099
- struct task_cputime task_sample;
1056
+ if (!expiry_cache_is_inactive(pct)) {
1057
+ u64 samples[CPUCLOCK_MAX];
11001058
1101
- task_cputime(tsk, &task_sample.utime, &task_sample.stime);
1102
- task_sample.sum_exec_runtime = tsk->se.sum_exec_runtime;
1103
- if (task_cputime_expired(&task_sample, &tsk->cputime_expires))
1104
- return 1;
1059
+ task_sample_cputime(tsk, samples);
1060
+ if (task_cputimers_expired(samples, pct))
1061
+ return true;
11051062 }
11061063
11071064 sig = tsk->signal;
1065
+ pct = &sig->posix_cputimers;
11081066 /*
1109
- * Check if thread group timers expired when the cputimer is
1110
- * running and no other thread in the group is already checking
1111
- * for thread group cputimers. These fields are read without the
1112
- * sighand lock. However, this is fine because this is meant to
1113
- * be a fastpath heuristic to determine whether we should try to
1114
- * acquire the sighand lock to check/handle timers.
1067
+ * Check if thread group timers expired when timers are active and
1068
+ * no other thread in the group is already handling expiry for
1069
+ * thread group cputimers. These fields are read without the
1070
+ * sighand lock. However, this is fine because this is meant to be
1071
+ * a fastpath heuristic to determine whether we should try to
1072
+ * acquire the sighand lock to handle timer expiry.
11151073 *
1116
- * In the worst case scenario, if 'running' or 'checking_timer' gets
1117
- * set but the current thread doesn't see the change yet, we'll wait
1118
- * until the next thread in the group gets a scheduler interrupt to
1119
- * handle the timer. This isn't an issue in practice because these
1120
- * types of delays with signals actually getting sent are expected.
1074
+ * In the worst case scenario, if concurrently timers_active is set
1075
+ * or expiry_active is cleared, but the current thread doesn't see
1076
+ * the change yet, the timer checks are delayed until the next
1077
+ * thread in the group gets a scheduler interrupt to handle the
1078
+ * timer. This isn't an issue in practice because these types of
1079
+ * delays with signals actually getting sent are expected.
11211080 */
1122
- if (READ_ONCE(sig->cputimer.running) &&
1123
- !READ_ONCE(sig->cputimer.checking_timer)) {
1124
- struct task_cputime group_sample;
1081
+ if (READ_ONCE(pct->timers_active) && !READ_ONCE(pct->expiry_active)) {
1082
+ u64 samples[CPUCLOCK_MAX];
11251083
1126
- sample_cputime_atomic(&group_sample, &sig->cputimer.cputime_atomic);
1084
+ proc_sample_cputime_atomic(&sig->cputimer.cputime_atomic,
1085
+ samples);
11271086
1128
- if (task_cputime_expired(&group_sample, &sig->cputime_expires))
1129
- return 1;
1087
+ if (task_cputimers_expired(samples, pct))
1088
+ return true;
11301089 }
11311090
11321091 if (dl_task(tsk) && tsk->dl.dl_overrun)
1133
- return 1;
1092
+ return true;
11341093
1135
- return 0;
1094
+ return false;
1095
+}
1096
+
1097
+static void handle_posix_cpu_timers(struct task_struct *tsk);
1098
+
1099
+#ifdef CONFIG_POSIX_CPU_TIMERS_TASK_WORK
1100
+static void posix_cpu_timers_work(struct callback_head *work)
1101
+{
1102
+ struct posix_cputimers_work *cw = container_of(work, typeof(*cw), work);
1103
+
1104
+ mutex_lock(&cw->mutex);
1105
+ handle_posix_cpu_timers(current);
1106
+ mutex_unlock(&cw->mutex);
11361107 }
11371108
11381109 /*
1139
- * This is called from the timer interrupt handler. The irq handler has
1140
- * already updated our counts. We need to check if any timers fire now.
1141
- * Interrupts are disabled.
1110
+ * Invoked from the posix-timer core when a cancel operation failed because
1111
+ * the timer is marked firing. The caller holds rcu_read_lock(), which
1112
+ * protects the timer and the task which is expiring it from being freed.
11421113 */
1143
-void run_posix_cpu_timers(struct task_struct *tsk)
1114
+static void posix_cpu_timer_wait_running(struct k_itimer *timr)
11441115 {
1145
- LIST_HEAD(firing);
1146
- struct k_itimer *timer, *next;
1147
- unsigned long flags;
1116
+ struct task_struct *tsk = rcu_dereference(timr->it.cpu.handling);
11481117
1149
- lockdep_assert_irqs_disabled();
1118
+ /* Has the handling task completed expiry already? */
1119
+ if (!tsk)
1120
+ return;
1121
+
1122
+ /* Ensure that the task cannot go away */
1123
+ get_task_struct(tsk);
1124
+ /* Now drop the RCU protection so the mutex can be locked */
1125
+ rcu_read_unlock();
1126
+ /* Wait on the expiry mutex */
1127
+ mutex_lock(&tsk->posix_cputimers_work.mutex);
1128
+ /* Release it immediately again. */
1129
+ mutex_unlock(&tsk->posix_cputimers_work.mutex);
1130
+ /* Drop the task reference. */
1131
+ put_task_struct(tsk);
1132
+ /* Relock RCU so the callsite is balanced */
1133
+ rcu_read_lock();
1134
+}
1135
+
1136
+static void posix_cpu_timer_wait_running_nsleep(struct k_itimer *timr)
1137
+{
1138
+ /* Ensure that timr->it.cpu.handling task cannot go away */
1139
+ rcu_read_lock();
1140
+ spin_unlock_irq(&timr->it_lock);
1141
+ posix_cpu_timer_wait_running(timr);
1142
+ rcu_read_unlock();
1143
+ /* @timr is on stack and is valid */
1144
+ spin_lock_irq(&timr->it_lock);
1145
+}
1146
+
1147
+/*
1148
+ * Clear existing posix CPU timers task work.
1149
+ */
1150
+void clear_posix_cputimers_work(struct task_struct *p)
1151
+{
1152
+ /*
1153
+ * A copied work entry from the old task is not meaningful, clear it.
1154
+ * N.B. init_task_work will not do this.
1155
+ */
1156
+ memset(&p->posix_cputimers_work.work, 0,
1157
+ sizeof(p->posix_cputimers_work.work));
1158
+ init_task_work(&p->posix_cputimers_work.work,
1159
+ posix_cpu_timers_work);
1160
+ mutex_init(&p->posix_cputimers_work.mutex);
1161
+ p->posix_cputimers_work.scheduled = false;
1162
+}
1163
+
1164
+/*
1165
+ * Initialize posix CPU timers task work in init task. Out of line to
1166
+ * keep the callback static and to avoid header recursion hell.
1167
+ */
1168
+void __init posix_cputimers_init_work(void)
1169
+{
1170
+ clear_posix_cputimers_work(current);
1171
+}
1172
+
1173
+/*
1174
+ * Note: All operations on tsk->posix_cputimer_work.scheduled happen either
1175
+ * in hard interrupt context or in task context with interrupts
1176
+ * disabled. Aside of that the writer/reader interaction is always in the
1177
+ * context of the current task, which means they are strict per CPU.
1178
+ */
1179
+static inline bool posix_cpu_timers_work_scheduled(struct task_struct *tsk)
1180
+{
1181
+ return tsk->posix_cputimers_work.scheduled;
1182
+}
1183
+
1184
+static inline void __run_posix_cpu_timers(struct task_struct *tsk)
1185
+{
1186
+ if (WARN_ON_ONCE(tsk->posix_cputimers_work.scheduled))
1187
+ return;
1188
+
1189
+ /* Schedule task work to actually expire the timers */
1190
+ tsk->posix_cputimers_work.scheduled = true;
1191
+ task_work_add(tsk, &tsk->posix_cputimers_work.work, TWA_RESUME);
1192
+}
1193
+
1194
+static inline bool posix_cpu_timers_enable_work(struct task_struct *tsk,
1195
+ unsigned long start)
1196
+{
1197
+ bool ret = true;
11501198
11511199 /*
1152
- * The fast path checks that there are no expired thread or thread
1153
- * group timers. If that's so, just return.
1200
+ * On !RT kernels interrupts are disabled while collecting expired
1201
+ * timers, so no tick can happen and the fast path check can be
1202
+ * reenabled without further checks.
11541203 */
1155
- if (!fastpath_timer_check(tsk))
1156
- return;
1204
+ if (!IS_ENABLED(CONFIG_PREEMPT_RT)) {
1205
+ tsk->posix_cputimers_work.scheduled = false;
1206
+ return true;
1207
+ }
1208
+
1209
+ /*
1210
+ * On RT enabled kernels ticks can happen while the expired timers
1211
+ * are collected under sighand lock. But any tick which observes
1212
+ * the CPUTIMERS_WORK_SCHEDULED bit set, does not run the fastpath
1213
+ * checks. So reenabling the tick work has do be done carefully:
1214
+ *
1215
+ * Disable interrupts and run the fast path check if jiffies have
1216
+ * advanced since the collecting of expired timers started. If
1217
+ * jiffies have not advanced or the fast path check did not find
1218
+ * newly expired timers, reenable the fast path check in the timer
1219
+ * interrupt. If there are newly expired timers, return false and
1220
+ * let the collection loop repeat.
1221
+ */
1222
+ local_irq_disable();
1223
+ if (start != jiffies && fastpath_timer_check(tsk))
1224
+ ret = false;
1225
+ else
1226
+ tsk->posix_cputimers_work.scheduled = false;
1227
+ local_irq_enable();
1228
+
1229
+ return ret;
1230
+}
1231
+#else /* CONFIG_POSIX_CPU_TIMERS_TASK_WORK */
1232
+static inline void __run_posix_cpu_timers(struct task_struct *tsk)
1233
+{
1234
+ lockdep_posixtimer_enter();
1235
+ handle_posix_cpu_timers(tsk);
1236
+ lockdep_posixtimer_exit();
1237
+}
1238
+
1239
+static void posix_cpu_timer_wait_running(struct k_itimer *timr)
1240
+{
1241
+ cpu_relax();
1242
+}
1243
+
1244
+static void posix_cpu_timer_wait_running_nsleep(struct k_itimer *timr)
1245
+{
1246
+ spin_unlock_irq(&timr->it_lock);
1247
+ cpu_relax();
1248
+ spin_lock_irq(&timr->it_lock);
1249
+}
1250
+
1251
+static inline bool posix_cpu_timers_work_scheduled(struct task_struct *tsk)
1252
+{
1253
+ return false;
1254
+}
1255
+
1256
+static inline bool posix_cpu_timers_enable_work(struct task_struct *tsk,
1257
+ unsigned long start)
1258
+{
1259
+ return true;
1260
+}
1261
+#endif /* CONFIG_POSIX_CPU_TIMERS_TASK_WORK */
1262
+
1263
+static void handle_posix_cpu_timers(struct task_struct *tsk)
1264
+{
1265
+ struct k_itimer *timer, *next;
1266
+ unsigned long flags, start;
1267
+ LIST_HEAD(firing);
11571268
11581269 if (!lock_task_sighand(tsk, &flags))
11591270 return;
1160
- /*
1161
- * Here we take off tsk->signal->cpu_timers[N] and
1162
- * tsk->cpu_timers[N] all the timers that are firing, and
1163
- * put them on the firing list.
1164
- */
1165
- check_thread_timers(tsk, &firing);
11661271
1167
- check_process_timers(tsk, &firing);
1272
+ do {
1273
+ /*
1274
+ * On RT locking sighand lock does not disable interrupts,
1275
+ * so this needs to be careful vs. ticks. Store the current
1276
+ * jiffies value.
1277
+ */
1278
+ start = READ_ONCE(jiffies);
1279
+ barrier();
1280
+
1281
+ /*
1282
+ * Here we take off tsk->signal->cpu_timers[N] and
1283
+ * tsk->cpu_timers[N] all the timers that are firing, and
1284
+ * put them on the firing list.
1285
+ */
1286
+ check_thread_timers(tsk, &firing);
1287
+
1288
+ check_process_timers(tsk, &firing);
1289
+
1290
+ /*
1291
+ * The above timer checks have updated the exipry cache and
1292
+ * because nothing can have queued or modified timers after
1293
+ * sighand lock was taken above it is guaranteed to be
1294
+ * consistent. So the next timer interrupt fastpath check
1295
+ * will find valid data.
1296
+ *
1297
+ * If timer expiry runs in the timer interrupt context then
1298
+ * the loop is not relevant as timers will be directly
1299
+ * expired in interrupt context. The stub function below
1300
+ * returns always true which allows the compiler to
1301
+ * optimize the loop out.
1302
+ *
1303
+ * If timer expiry is deferred to task work context then
1304
+ * the following rules apply:
1305
+ *
1306
+ * - On !RT kernels no tick can have happened on this CPU
1307
+ * after sighand lock was acquired because interrupts are
1308
+ * disabled. So reenabling task work before dropping
1309
+ * sighand lock and reenabling interrupts is race free.
1310
+ *
1311
+ * - On RT kernels ticks might have happened but the tick
1312
+ * work ignored posix CPU timer handling because the
1313
+ * CPUTIMERS_WORK_SCHEDULED bit is set. Reenabling work
1314
+ * must be done very carefully including a check whether
1315
+ * ticks have happened since the start of the timer
1316
+ * expiry checks. posix_cpu_timers_enable_work() takes
1317
+ * care of that and eventually lets the expiry checks
1318
+ * run again.
1319
+ */
1320
+ } while (!posix_cpu_timers_enable_work(tsk, start));
11681321
11691322 /*
1170
- * We must release these locks before taking any timer's lock.
1323
+ * We must release sighand lock before taking any timer's lock.
11711324 * There is a potential race with timer deletion here, as the
11721325 * siglock now protects our private firing list. We have set
11731326 * the firing flag in each timer, so that a deletion attempt
....@@ -1182,11 +1335,18 @@
11821335 * each timer's lock before clearing its firing flag, so no
11831336 * timer call will interfere.
11841337 */
1185
- list_for_each_entry_safe(timer, next, &firing, it.cpu.entry) {
1338
+ list_for_each_entry_safe(timer, next, &firing, it.cpu.elist) {
11861339 int cpu_firing;
11871340
1341
+ /*
1342
+ * spin_lock() is sufficient here even independent of the
1343
+ * expiry context. If expiry happens in hard interrupt
1344
+ * context it's obvious. For task work context it's safe
1345
+ * because all other operations on timer::it_lock happen in
1346
+ * task context (syscall or exit).
1347
+ */
11881348 spin_lock(&timer->it_lock);
1189
- list_del_init(&timer->it.cpu.entry);
1349
+ list_del_init(&timer->it.cpu.elist);
11901350 cpu_firing = timer->it.cpu.firing;
11911351 timer->it.cpu.firing = 0;
11921352 /*
....@@ -1196,26 +1356,56 @@
11961356 */
11971357 if (likely(cpu_firing >= 0))
11981358 cpu_timer_fire(timer);
1359
+ /* See posix_cpu_timer_wait_running() */
1360
+ rcu_assign_pointer(timer->it.cpu.handling, NULL);
11991361 spin_unlock(&timer->it_lock);
12001362 }
1363
+}
1364
+
1365
+/*
1366
+ * This is called from the timer interrupt handler. The irq handler has
1367
+ * already updated our counts. We need to check if any timers fire now.
1368
+ * Interrupts are disabled.
1369
+ */
1370
+void run_posix_cpu_timers(void)
1371
+{
1372
+ struct task_struct *tsk = current;
1373
+
1374
+ lockdep_assert_irqs_disabled();
1375
+
1376
+ /*
1377
+ * If the actual expiry is deferred to task work context and the
1378
+ * work is already scheduled there is no point to do anything here.
1379
+ */
1380
+ if (posix_cpu_timers_work_scheduled(tsk))
1381
+ return;
1382
+
1383
+ /*
1384
+ * The fast path checks that there are no expired thread or thread
1385
+ * group timers. If that's so, just return.
1386
+ */
1387
+ if (!fastpath_timer_check(tsk))
1388
+ return;
1389
+
1390
+ __run_posix_cpu_timers(tsk);
12011391 }
12021392
12031393 /*
12041394 * Set one of the process-wide special case CPU timers or RLIMIT_CPU.
12051395 * The tsk->sighand->siglock must be held by the caller.
12061396 */
1207
-void set_process_cpu_timer(struct task_struct *tsk, unsigned int clock_idx,
1397
+void set_process_cpu_timer(struct task_struct *tsk, unsigned int clkid,
12081398 u64 *newval, u64 *oldval)
12091399 {
1210
- u64 now;
1211
- int ret;
1400
+ u64 now, *nextevt;
12121401
1213
- if (WARN_ON_ONCE(clock_idx >= CPUCLOCK_SCHED))
1402
+ if (WARN_ON_ONCE(clkid >= CPUCLOCK_SCHED))
12141403 return;
12151404
1216
- ret = cpu_timer_sample_group(clock_idx, tsk, &now);
1405
+ nextevt = &tsk->signal->posix_cputimers.bases[clkid].nextevt;
1406
+ now = cpu_clock_sample_group(clkid, tsk, true);
12171407
1218
- if (oldval && ret != -EINVAL) {
1408
+ if (oldval) {
12191409 /*
12201410 * We are setting itimer. The *oldval is absolute and we update
12211411 * it to be relative, *newval argument is relative and we update
....@@ -1236,19 +1426,11 @@
12361426 }
12371427
12381428 /*
1239
- * Update expiration cache if we are the earliest timer, or eventually
1240
- * RLIMIT_CPU limit is earlier than prof_exp cpu timer expire.
1429
+ * Update expiration cache if this is the earliest timer. CPUCLOCK_PROF
1430
+ * expiry cache is also used by RLIMIT_CPU!.
12411431 */
1242
- switch (clock_idx) {
1243
- case CPUCLOCK_PROF:
1244
- if (expires_gt(tsk->signal->cputime_expires.prof_exp, *newval))
1245
- tsk->signal->cputime_expires.prof_exp = *newval;
1246
- break;
1247
- case CPUCLOCK_VIRT:
1248
- if (expires_gt(tsk->signal->cputime_expires.virt_exp, *newval))
1249
- tsk->signal->cputime_expires.virt_exp = *newval;
1250
- break;
1251
- }
1432
+ if (*newval < *nextevt)
1433
+ *nextevt = *newval;
12521434
12531435 tick_dep_set_signal(tsk->signal, TICK_DEP_BIT_POSIX_TIMER);
12541436 }
....@@ -1270,6 +1452,7 @@
12701452 timer.it_overrun = -1;
12711453 error = posix_cpu_timer_create(&timer);
12721454 timer.it_process = current;
1455
+
12731456 if (!error) {
12741457 static struct itimerspec64 zero_it;
12751458 struct restart_block *restart;
....@@ -1285,7 +1468,7 @@
12851468 }
12861469
12871470 while (!signal_pending(current)) {
1288
- if (timer.it.cpu.expires == 0) {
1471
+ if (!cpu_timer_getexpires(&timer.it.cpu)) {
12891472 /*
12901473 * Our timer fired and was reset, below
12911474 * deletion can not fail.
....@@ -1307,26 +1490,19 @@
13071490 /*
13081491 * We were interrupted by a signal.
13091492 */
1310
- expires = timer.it.cpu.expires;
1493
+ expires = cpu_timer_getexpires(&timer.it.cpu);
13111494 error = posix_cpu_timer_set(&timer, 0, &zero_it, &it);
13121495 if (!error) {
1313
- /*
1314
- * Timer is now unarmed, deletion can not fail.
1315
- */
1496
+ /* Timer is now unarmed, deletion can not fail. */
13161497 posix_cpu_timer_del(&timer);
1498
+ } else {
1499
+ while (error == TIMER_RETRY) {
1500
+ posix_cpu_timer_wait_running_nsleep(&timer);
1501
+ error = posix_cpu_timer_del(&timer);
1502
+ }
13171503 }
1318
- spin_unlock_irq(&timer.it_lock);
13191504
1320
- while (error == TIMER_RETRY) {
1321
- /*
1322
- * We need to handle case when timer was or is in the
1323
- * middle of firing. In other cases we already freed
1324
- * resources.
1325
- */
1326
- spin_lock_irq(&timer.it_lock);
1327
- error = posix_cpu_timer_del(&timer);
1328
- spin_unlock_irq(&timer.it_lock);
1329
- }
1505
+ spin_unlock_irq(&timer.it_lock);
13301506
13311507 if ((it.it_value.tv_sec | it.it_value.tv_nsec) == 0) {
13321508 /*
....@@ -1427,26 +1603,27 @@
14271603 }
14281604
14291605 const struct k_clock clock_posix_cpu = {
1430
- .clock_getres = posix_cpu_clock_getres,
1431
- .clock_set = posix_cpu_clock_set,
1432
- .clock_get = posix_cpu_clock_get,
1433
- .timer_create = posix_cpu_timer_create,
1434
- .nsleep = posix_cpu_nsleep,
1435
- .timer_set = posix_cpu_timer_set,
1436
- .timer_del = posix_cpu_timer_del,
1437
- .timer_get = posix_cpu_timer_get,
1438
- .timer_rearm = posix_cpu_timer_rearm,
1606
+ .clock_getres = posix_cpu_clock_getres,
1607
+ .clock_set = posix_cpu_clock_set,
1608
+ .clock_get_timespec = posix_cpu_clock_get,
1609
+ .timer_create = posix_cpu_timer_create,
1610
+ .nsleep = posix_cpu_nsleep,
1611
+ .timer_set = posix_cpu_timer_set,
1612
+ .timer_del = posix_cpu_timer_del,
1613
+ .timer_get = posix_cpu_timer_get,
1614
+ .timer_rearm = posix_cpu_timer_rearm,
1615
+ .timer_wait_running = posix_cpu_timer_wait_running,
14391616 };
14401617
14411618 const struct k_clock clock_process = {
1442
- .clock_getres = process_cpu_clock_getres,
1443
- .clock_get = process_cpu_clock_get,
1444
- .timer_create = process_cpu_timer_create,
1445
- .nsleep = process_cpu_nsleep,
1619
+ .clock_getres = process_cpu_clock_getres,
1620
+ .clock_get_timespec = process_cpu_clock_get,
1621
+ .timer_create = process_cpu_timer_create,
1622
+ .nsleep = process_cpu_nsleep,
14461623 };
14471624
14481625 const struct k_clock clock_thread = {
1449
- .clock_getres = thread_cpu_clock_getres,
1450
- .clock_get = thread_cpu_clock_get,
1451
- .timer_create = thread_cpu_timer_create,
1626
+ .clock_getres = thread_cpu_clock_getres,
1627
+ .clock_get_timespec = thread_cpu_clock_get,
1628
+ .timer_create = thread_cpu_timer_create,
14521629 };