hc
2023-12-09 b22da3d8526a935aa31e086e63f60ff3246cb61c
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,46 @@
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
+ cpu_timer_dequeue(ctmr);
786
+ list_add_tail(&ctmr->elist, firing);
793787 }
794788
795
- return 0;
789
+ return U64_MAX;
790
+}
791
+
792
+static void collect_posix_cputimers(struct posix_cputimers *pct, u64 *samples,
793
+ struct list_head *firing)
794
+{
795
+ struct posix_cputimer_base *base = pct->bases;
796
+ int i;
797
+
798
+ for (i = 0; i < CPUCLOCK_MAX; i++, base++) {
799
+ base->nextevt = collect_timerqueue(&base->tqhead, firing,
800
+ samples[i]);
801
+ }
796802 }
797803
798804 static inline void check_dl_overrun(struct task_struct *tsk)
....@@ -803,6 +809,20 @@
803809 }
804810 }
805811
812
+static bool check_rlimit(u64 time, u64 limit, int signo, bool rt, bool hard)
813
+{
814
+ if (time < limit)
815
+ return false;
816
+
817
+ if (print_fatal_signals) {
818
+ pr_info("%s Watchdog Timeout (%s): %s[%d]\n",
819
+ rt ? "RT" : "CPU", hard ? "hard" : "soft",
820
+ current->comm, task_pid_nr(current));
821
+ }
822
+ __group_send_sig_info(signo, SEND_SIG_PRIV, current);
823
+ return true;
824
+}
825
+
806826 /*
807827 * Check for any per-thread CPU timers that have fired and move them off
808828 * the tsk->cpu_timers[N] list onto the firing list. Here we update the
....@@ -811,76 +831,50 @@
811831 static void check_thread_timers(struct task_struct *tsk,
812832 struct list_head *firing)
813833 {
814
- struct list_head *timers = tsk->cpu_timers;
815
- struct task_cputime *tsk_expires = &tsk->cputime_expires;
816
- u64 expires;
834
+ struct posix_cputimers *pct = &tsk->posix_cputimers;
835
+ u64 samples[CPUCLOCK_MAX];
817836 unsigned long soft;
818837
819838 if (dl_task(tsk))
820839 check_dl_overrun(tsk);
821840
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))
841
+ if (expiry_cache_is_inactive(pct))
827842 return;
828843
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);
844
+ task_sample_cputime(tsk, samples);
845
+ collect_posix_cputimers(pct, samples, firing);
837846
838847 /*
839848 * Check for the special case thread timers.
840849 */
841850 soft = task_rlimit(tsk, RLIMIT_RTTIME);
842851 if (soft != RLIM_INFINITY) {
852
+ /* Task RT timeout is accounted in jiffies. RTTIME is usec */
853
+ unsigned long rttime = tsk->rt.timeout * (USEC_PER_SEC / HZ);
843854 unsigned long hard = task_rlimit_max(tsk, RLIMIT_RTTIME);
844855
856
+ /* At the hard limit, send SIGKILL. No further action. */
845857 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);
858
+ check_rlimit(rttime, hard, SIGKILL, true, true))
856859 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);
860
+
861
+ /* At the soft limit, send a SIGXCPU every second */
862
+ if (check_rlimit(rttime, soft, SIGXCPU, true, false)) {
863
+ soft += USEC_PER_SEC;
864
+ tsk->signal->rlim[RLIMIT_RTTIME].rlim_cur = soft;
872865 }
873866 }
874
- if (task_cputime_zero(tsk_expires))
867
+
868
+ if (expiry_cache_is_inactive(pct))
875869 tick_dep_clear_task(tsk, TICK_DEP_BIT_POSIX_TIMER);
876870 }
877871
878872 static inline void stop_process_timers(struct signal_struct *sig)
879873 {
880
- struct thread_group_cputimer *cputimer = &sig->cputimer;
874
+ struct posix_cputimers *pct = &sig->posix_cputimers;
881875
882
- /* Turn off cputimer->running. This is done without locking. */
883
- WRITE_ONCE(cputimer->running, false);
876
+ /* Turn off the active flag. This is done without locking. */
877
+ WRITE_ONCE(pct->timers_active, false);
884878 tick_dep_clear_signal(sig, TICK_DEP_BIT_POSIX_TIMER);
885879 }
886880
....@@ -902,7 +896,7 @@
902896 __group_send_sig_info(signo, SEND_SIG_PRIV, tsk);
903897 }
904898
905
- if (it->expires && (!*expires || it->expires < *expires))
899
+ if (it->expires && it->expires < *expires)
906900 *expires = it->expires;
907901 }
908902
....@@ -915,90 +909,69 @@
915909 struct list_head *firing)
916910 {
917911 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;
912
+ struct posix_cputimers *pct = &sig->posix_cputimers;
913
+ u64 samples[CPUCLOCK_MAX];
922914 unsigned long soft;
923915
924
- if (dl_task(tsk))
925
- check_dl_overrun(tsk);
926
-
927916 /*
928
- * If cputimer is not running, then there are no active
929
- * process wide timers (POSIX 1.b, itimers, RLIMIT_CPU).
917
+ * If there are no active process wide timers (POSIX 1.b, itimers,
918
+ * RLIMIT_CPU) nothing to check. Also skip the process wide timer
919
+ * processing when there is already another task handling them.
930920 */
931
- if (!READ_ONCE(tsk->signal->cputimer.running))
921
+ if (!READ_ONCE(pct->timers_active) || pct->expiry_active)
932922 return;
933923
934
- /*
924
+ /*
935925 * Signify that a thread is checking for process timers.
936926 * Write access to this field is protected by the sighand lock.
937927 */
938
- sig->cputimer.checking_timer = true;
928
+ pct->expiry_active = true;
939929
940930 /*
941
- * Collect the current process totals.
931
+ * Collect the current process totals. Group accounting is active
932
+ * so the sample can be taken directly.
942933 */
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);
934
+ proc_sample_cputime_atomic(&sig->cputimer.cputime_atomic, samples);
935
+ collect_posix_cputimers(pct, samples, firing);
951936
952937 /*
953938 * Check for the special case process timers.
954939 */
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);
940
+ check_cpu_itimer(tsk, &sig->it[CPUCLOCK_PROF],
941
+ &pct->bases[CPUCLOCK_PROF].nextevt,
942
+ samples[CPUCLOCK_PROF], SIGPROF);
943
+ check_cpu_itimer(tsk, &sig->it[CPUCLOCK_VIRT],
944
+ &pct->bases[CPUCLOCK_VIRT].nextevt,
945
+ samples[CPUCLOCK_VIRT], SIGVTALRM);
946
+
959947 soft = task_rlimit(tsk, RLIMIT_CPU);
960948 if (soft != RLIM_INFINITY) {
961
- unsigned long psecs = div_u64(ptime, NSEC_PER_SEC);
949
+ /* RLIMIT_CPU is in seconds. Samples are nanoseconds */
962950 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);
951
+ u64 ptime = samples[CPUCLOCK_PROF];
952
+ u64 softns = (u64)soft * NSEC_PER_SEC;
953
+ u64 hardns = (u64)hard * NSEC_PER_SEC;
954
+
955
+ /* At the hard limit, send SIGKILL. No further action. */
956
+ if (hard != RLIM_INFINITY &&
957
+ check_rlimit(ptime, hardns, SIGKILL, false, true))
974958 return;
959
+
960
+ /* At the soft limit, send a SIGXCPU every second */
961
+ if (check_rlimit(ptime, softns, SIGXCPU, false, false)) {
962
+ sig->rlim[RLIMIT_CPU].rlim_cur = soft + 1;
963
+ softns += NSEC_PER_SEC;
975964 }
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;
965
+
966
+ /* Update the expiry cache */
967
+ if (softns < pct->bases[CPUCLOCK_PROF].nextevt)
968
+ pct->bases[CPUCLOCK_PROF].nextevt = softns;
993969 }
994970
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))
971
+ if (expiry_cache_is_inactive(pct))
999972 stop_process_timers(sig);
1000973
1001
- sig->cputimer.checking_timer = false;
974
+ pct->expiry_active = false;
1002975 }
1003976
1004977 /*
....@@ -1007,78 +980,60 @@
1007980 */
1008981 static void posix_cpu_timer_rearm(struct k_itimer *timer)
1009982 {
1010
- struct task_struct *p = timer->it.cpu.task;
983
+ clockid_t clkid = CPUCLOCK_WHICH(timer->it_clock);
984
+ struct task_struct *p;
1011985 struct sighand_struct *sighand;
1012986 unsigned long flags;
1013987 u64 now;
1014988
1015
- if (WARN_ON_ONCE(!p))
1016
- return;
989
+ rcu_read_lock();
990
+ p = cpu_timer_task_rcu(timer);
991
+ if (!p)
992
+ goto out;
993
+
994
+ /* Protect timer list r/w in arm_timer() */
995
+ sighand = lock_task_sighand(p, &flags);
996
+ if (unlikely(sighand == NULL))
997
+ goto out;
1017998
1018999 /*
10191000 * Fetch the current sample and update the timer's expiry time.
10201001 */
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;
1002
+ if (CPUCLOCK_PERTHREAD(timer->it_clock))
1003
+ now = cpu_clock_sample(clkid, p);
1004
+ else
1005
+ now = cpu_clock_sample_group(clkid, p, true);
10261006
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
- }
1007
+ bump_cpu_timer(timer, now);
10521008
10531009 /*
10541010 * Now re-arm for the new expiry time.
10551011 */
1056
- arm_timer(timer);
1057
-unlock:
1012
+ arm_timer(timer, p);
10581013 unlock_task_sighand(p, &flags);
1014
+out:
1015
+ rcu_read_unlock();
10591016 }
10601017
10611018 /**
1062
- * task_cputime_expired - Compare two task_cputime entities.
1019
+ * task_cputimers_expired - Check whether posix CPU timers are expired
10631020 *
1064
- * @sample: The task_cputime structure to be checked for expiration.
1065
- * @expires: Expiration times, against which @sample will be checked.
1021
+ * @samples: Array of current samples for the CPUCLOCK clocks
1022
+ * @pct: Pointer to a posix_cputimers container
10661023 *
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.
1024
+ * Returns true if any member of @samples is greater than the corresponding
1025
+ * member of @pct->bases[CLK].nextevt. False otherwise
10701026 */
1071
-static inline int task_cputime_expired(const struct task_cputime *sample,
1072
- const struct task_cputime *expires)
1027
+static inline bool
1028
+task_cputimers_expired(const u64 *samples, struct posix_cputimers *pct)
10731029 {
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;
1030
+ int i;
1031
+
1032
+ for (i = 0; i < CPUCLOCK_MAX; i++) {
1033
+ if (samples[i] >= pct->bases[i].nextevt)
1034
+ return true;
1035
+ }
1036
+ return false;
10821037 }
10831038
10841039 /**
....@@ -1091,83 +1046,224 @@
10911046 * timers and compare them with the corresponding expiration times. Return
10921047 * true if a timer has expired, else return false.
10931048 */
1094
-static inline int fastpath_timer_check(struct task_struct *tsk)
1049
+static inline bool fastpath_timer_check(struct task_struct *tsk)
10951050 {
1051
+ struct posix_cputimers *pct = &tsk->posix_cputimers;
10961052 struct signal_struct *sig;
10971053
1098
- if (!task_cputime_zero(&tsk->cputime_expires)) {
1099
- struct task_cputime task_sample;
1054
+ if (!expiry_cache_is_inactive(pct)) {
1055
+ u64 samples[CPUCLOCK_MAX];
11001056
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;
1057
+ task_sample_cputime(tsk, samples);
1058
+ if (task_cputimers_expired(samples, pct))
1059
+ return true;
11051060 }
11061061
11071062 sig = tsk->signal;
1063
+ pct = &sig->posix_cputimers;
11081064 /*
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.
1065
+ * Check if thread group timers expired when timers are active and
1066
+ * no other thread in the group is already handling expiry for
1067
+ * thread group cputimers. These fields are read without the
1068
+ * sighand lock. However, this is fine because this is meant to be
1069
+ * a fastpath heuristic to determine whether we should try to
1070
+ * acquire the sighand lock to handle timer expiry.
11151071 *
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.
1072
+ * In the worst case scenario, if concurrently timers_active is set
1073
+ * or expiry_active is cleared, but the current thread doesn't see
1074
+ * the change yet, the timer checks are delayed until the next
1075
+ * thread in the group gets a scheduler interrupt to handle the
1076
+ * timer. This isn't an issue in practice because these types of
1077
+ * delays with signals actually getting sent are expected.
11211078 */
1122
- if (READ_ONCE(sig->cputimer.running) &&
1123
- !READ_ONCE(sig->cputimer.checking_timer)) {
1124
- struct task_cputime group_sample;
1079
+ if (READ_ONCE(pct->timers_active) && !READ_ONCE(pct->expiry_active)) {
1080
+ u64 samples[CPUCLOCK_MAX];
11251081
1126
- sample_cputime_atomic(&group_sample, &sig->cputimer.cputime_atomic);
1082
+ proc_sample_cputime_atomic(&sig->cputimer.cputime_atomic,
1083
+ samples);
11271084
1128
- if (task_cputime_expired(&group_sample, &sig->cputime_expires))
1129
- return 1;
1085
+ if (task_cputimers_expired(samples, pct))
1086
+ return true;
11301087 }
11311088
11321089 if (dl_task(tsk) && tsk->dl.dl_overrun)
1133
- return 1;
1090
+ return true;
11341091
1135
- return 0;
1092
+ return false;
1093
+}
1094
+
1095
+static void handle_posix_cpu_timers(struct task_struct *tsk);
1096
+
1097
+#ifdef CONFIG_POSIX_CPU_TIMERS_TASK_WORK
1098
+static void posix_cpu_timers_work(struct callback_head *work)
1099
+{
1100
+ handle_posix_cpu_timers(current);
11361101 }
11371102
11381103 /*
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.
1104
+ * Clear existing posix CPU timers task work.
11421105 */
1143
-void run_posix_cpu_timers(struct task_struct *tsk)
1106
+void clear_posix_cputimers_work(struct task_struct *p)
11441107 {
1145
- LIST_HEAD(firing);
1146
- struct k_itimer *timer, *next;
1147
- unsigned long flags;
1108
+ /*
1109
+ * A copied work entry from the old task is not meaningful, clear it.
1110
+ * N.B. init_task_work will not do this.
1111
+ */
1112
+ memset(&p->posix_cputimers_work.work, 0,
1113
+ sizeof(p->posix_cputimers_work.work));
1114
+ init_task_work(&p->posix_cputimers_work.work,
1115
+ posix_cpu_timers_work);
1116
+ p->posix_cputimers_work.scheduled = false;
1117
+}
11481118
1149
- lockdep_assert_irqs_disabled();
1119
+/*
1120
+ * Initialize posix CPU timers task work in init task. Out of line to
1121
+ * keep the callback static and to avoid header recursion hell.
1122
+ */
1123
+void __init posix_cputimers_init_work(void)
1124
+{
1125
+ clear_posix_cputimers_work(current);
1126
+}
1127
+
1128
+/*
1129
+ * Note: All operations on tsk->posix_cputimer_work.scheduled happen either
1130
+ * in hard interrupt context or in task context with interrupts
1131
+ * disabled. Aside of that the writer/reader interaction is always in the
1132
+ * context of the current task, which means they are strict per CPU.
1133
+ */
1134
+static inline bool posix_cpu_timers_work_scheduled(struct task_struct *tsk)
1135
+{
1136
+ return tsk->posix_cputimers_work.scheduled;
1137
+}
1138
+
1139
+static inline void __run_posix_cpu_timers(struct task_struct *tsk)
1140
+{
1141
+ if (WARN_ON_ONCE(tsk->posix_cputimers_work.scheduled))
1142
+ return;
1143
+
1144
+ /* Schedule task work to actually expire the timers */
1145
+ tsk->posix_cputimers_work.scheduled = true;
1146
+ task_work_add(tsk, &tsk->posix_cputimers_work.work, TWA_RESUME);
1147
+}
1148
+
1149
+static inline bool posix_cpu_timers_enable_work(struct task_struct *tsk,
1150
+ unsigned long start)
1151
+{
1152
+ bool ret = true;
11501153
11511154 /*
1152
- * The fast path checks that there are no expired thread or thread
1153
- * group timers. If that's so, just return.
1155
+ * On !RT kernels interrupts are disabled while collecting expired
1156
+ * timers, so no tick can happen and the fast path check can be
1157
+ * reenabled without further checks.
11541158 */
1155
- if (!fastpath_timer_check(tsk))
1156
- return;
1159
+ if (!IS_ENABLED(CONFIG_PREEMPT_RT)) {
1160
+ tsk->posix_cputimers_work.scheduled = false;
1161
+ return true;
1162
+ }
1163
+
1164
+ /*
1165
+ * On RT enabled kernels ticks can happen while the expired timers
1166
+ * are collected under sighand lock. But any tick which observes
1167
+ * the CPUTIMERS_WORK_SCHEDULED bit set, does not run the fastpath
1168
+ * checks. So reenabling the tick work has do be done carefully:
1169
+ *
1170
+ * Disable interrupts and run the fast path check if jiffies have
1171
+ * advanced since the collecting of expired timers started. If
1172
+ * jiffies have not advanced or the fast path check did not find
1173
+ * newly expired timers, reenable the fast path check in the timer
1174
+ * interrupt. If there are newly expired timers, return false and
1175
+ * let the collection loop repeat.
1176
+ */
1177
+ local_irq_disable();
1178
+ if (start != jiffies && fastpath_timer_check(tsk))
1179
+ ret = false;
1180
+ else
1181
+ tsk->posix_cputimers_work.scheduled = false;
1182
+ local_irq_enable();
1183
+
1184
+ return ret;
1185
+}
1186
+#else /* CONFIG_POSIX_CPU_TIMERS_TASK_WORK */
1187
+static inline void __run_posix_cpu_timers(struct task_struct *tsk)
1188
+{
1189
+ lockdep_posixtimer_enter();
1190
+ handle_posix_cpu_timers(tsk);
1191
+ lockdep_posixtimer_exit();
1192
+}
1193
+
1194
+static inline bool posix_cpu_timers_work_scheduled(struct task_struct *tsk)
1195
+{
1196
+ return false;
1197
+}
1198
+
1199
+static inline bool posix_cpu_timers_enable_work(struct task_struct *tsk,
1200
+ unsigned long start)
1201
+{
1202
+ return true;
1203
+}
1204
+#endif /* CONFIG_POSIX_CPU_TIMERS_TASK_WORK */
1205
+
1206
+static void handle_posix_cpu_timers(struct task_struct *tsk)
1207
+{
1208
+ struct k_itimer *timer, *next;
1209
+ unsigned long flags, start;
1210
+ LIST_HEAD(firing);
11571211
11581212 if (!lock_task_sighand(tsk, &flags))
11591213 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);
11661214
1167
- check_process_timers(tsk, &firing);
1215
+ do {
1216
+ /*
1217
+ * On RT locking sighand lock does not disable interrupts,
1218
+ * so this needs to be careful vs. ticks. Store the current
1219
+ * jiffies value.
1220
+ */
1221
+ start = READ_ONCE(jiffies);
1222
+ barrier();
1223
+
1224
+ /*
1225
+ * Here we take off tsk->signal->cpu_timers[N] and
1226
+ * tsk->cpu_timers[N] all the timers that are firing, and
1227
+ * put them on the firing list.
1228
+ */
1229
+ check_thread_timers(tsk, &firing);
1230
+
1231
+ check_process_timers(tsk, &firing);
1232
+
1233
+ /*
1234
+ * The above timer checks have updated the exipry cache and
1235
+ * because nothing can have queued or modified timers after
1236
+ * sighand lock was taken above it is guaranteed to be
1237
+ * consistent. So the next timer interrupt fastpath check
1238
+ * will find valid data.
1239
+ *
1240
+ * If timer expiry runs in the timer interrupt context then
1241
+ * the loop is not relevant as timers will be directly
1242
+ * expired in interrupt context. The stub function below
1243
+ * returns always true which allows the compiler to
1244
+ * optimize the loop out.
1245
+ *
1246
+ * If timer expiry is deferred to task work context then
1247
+ * the following rules apply:
1248
+ *
1249
+ * - On !RT kernels no tick can have happened on this CPU
1250
+ * after sighand lock was acquired because interrupts are
1251
+ * disabled. So reenabling task work before dropping
1252
+ * sighand lock and reenabling interrupts is race free.
1253
+ *
1254
+ * - On RT kernels ticks might have happened but the tick
1255
+ * work ignored posix CPU timer handling because the
1256
+ * CPUTIMERS_WORK_SCHEDULED bit is set. Reenabling work
1257
+ * must be done very carefully including a check whether
1258
+ * ticks have happened since the start of the timer
1259
+ * expiry checks. posix_cpu_timers_enable_work() takes
1260
+ * care of that and eventually lets the expiry checks
1261
+ * run again.
1262
+ */
1263
+ } while (!posix_cpu_timers_enable_work(tsk, start));
11681264
11691265 /*
1170
- * We must release these locks before taking any timer's lock.
1266
+ * We must release sighand lock before taking any timer's lock.
11711267 * There is a potential race with timer deletion here, as the
11721268 * siglock now protects our private firing list. We have set
11731269 * the firing flag in each timer, so that a deletion attempt
....@@ -1182,11 +1278,18 @@
11821278 * each timer's lock before clearing its firing flag, so no
11831279 * timer call will interfere.
11841280 */
1185
- list_for_each_entry_safe(timer, next, &firing, it.cpu.entry) {
1281
+ list_for_each_entry_safe(timer, next, &firing, it.cpu.elist) {
11861282 int cpu_firing;
11871283
1284
+ /*
1285
+ * spin_lock() is sufficient here even independent of the
1286
+ * expiry context. If expiry happens in hard interrupt
1287
+ * context it's obvious. For task work context it's safe
1288
+ * because all other operations on timer::it_lock happen in
1289
+ * task context (syscall or exit).
1290
+ */
11881291 spin_lock(&timer->it_lock);
1189
- list_del_init(&timer->it.cpu.entry);
1292
+ list_del_init(&timer->it.cpu.elist);
11901293 cpu_firing = timer->it.cpu.firing;
11911294 timer->it.cpu.firing = 0;
11921295 /*
....@@ -1201,21 +1304,49 @@
12011304 }
12021305
12031306 /*
1307
+ * This is called from the timer interrupt handler. The irq handler has
1308
+ * already updated our counts. We need to check if any timers fire now.
1309
+ * Interrupts are disabled.
1310
+ */
1311
+void run_posix_cpu_timers(void)
1312
+{
1313
+ struct task_struct *tsk = current;
1314
+
1315
+ lockdep_assert_irqs_disabled();
1316
+
1317
+ /*
1318
+ * If the actual expiry is deferred to task work context and the
1319
+ * work is already scheduled there is no point to do anything here.
1320
+ */
1321
+ if (posix_cpu_timers_work_scheduled(tsk))
1322
+ return;
1323
+
1324
+ /*
1325
+ * The fast path checks that there are no expired thread or thread
1326
+ * group timers. If that's so, just return.
1327
+ */
1328
+ if (!fastpath_timer_check(tsk))
1329
+ return;
1330
+
1331
+ __run_posix_cpu_timers(tsk);
1332
+}
1333
+
1334
+/*
12041335 * Set one of the process-wide special case CPU timers or RLIMIT_CPU.
12051336 * The tsk->sighand->siglock must be held by the caller.
12061337 */
1207
-void set_process_cpu_timer(struct task_struct *tsk, unsigned int clock_idx,
1338
+void set_process_cpu_timer(struct task_struct *tsk, unsigned int clkid,
12081339 u64 *newval, u64 *oldval)
12091340 {
1210
- u64 now;
1211
- int ret;
1341
+ u64 now, *nextevt;
12121342
1213
- if (WARN_ON_ONCE(clock_idx >= CPUCLOCK_SCHED))
1343
+ if (WARN_ON_ONCE(clkid >= CPUCLOCK_SCHED))
12141344 return;
12151345
1216
- ret = cpu_timer_sample_group(clock_idx, tsk, &now);
1346
+ nextevt = &tsk->signal->posix_cputimers.bases[clkid].nextevt;
1347
+ now = cpu_clock_sample_group(clkid, tsk, true);
12171348
1218
- if (oldval && ret != -EINVAL) {
1349
+ if (oldval) {
12191350 /*
12201351 * We are setting itimer. The *oldval is absolute and we update
12211352 * it to be relative, *newval argument is relative and we update
....@@ -1236,19 +1367,11 @@
12361367 }
12371368
12381369 /*
1239
- * Update expiration cache if we are the earliest timer, or eventually
1240
- * RLIMIT_CPU limit is earlier than prof_exp cpu timer expire.
1370
+ * Update expiration cache if this is the earliest timer. CPUCLOCK_PROF
1371
+ * expiry cache is also used by RLIMIT_CPU!.
12411372 */
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
- }
1373
+ if (*newval < *nextevt)
1374
+ *nextevt = *newval;
12521375
12531376 tick_dep_set_signal(tsk->signal, TICK_DEP_BIT_POSIX_TIMER);
12541377 }
....@@ -1270,6 +1393,7 @@
12701393 timer.it_overrun = -1;
12711394 error = posix_cpu_timer_create(&timer);
12721395 timer.it_process = current;
1396
+
12731397 if (!error) {
12741398 static struct itimerspec64 zero_it;
12751399 struct restart_block *restart;
....@@ -1285,7 +1409,7 @@
12851409 }
12861410
12871411 while (!signal_pending(current)) {
1288
- if (timer.it.cpu.expires == 0) {
1412
+ if (!cpu_timer_getexpires(&timer.it.cpu)) {
12891413 /*
12901414 * Our timer fired and was reset, below
12911415 * deletion can not fail.
....@@ -1307,7 +1431,7 @@
13071431 /*
13081432 * We were interrupted by a signal.
13091433 */
1310
- expires = timer.it.cpu.expires;
1434
+ expires = cpu_timer_getexpires(&timer.it.cpu);
13111435 error = posix_cpu_timer_set(&timer, 0, &zero_it, &it);
13121436 if (!error) {
13131437 /*
....@@ -1427,26 +1551,26 @@
14271551 }
14281552
14291553 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,
1554
+ .clock_getres = posix_cpu_clock_getres,
1555
+ .clock_set = posix_cpu_clock_set,
1556
+ .clock_get_timespec = posix_cpu_clock_get,
1557
+ .timer_create = posix_cpu_timer_create,
1558
+ .nsleep = posix_cpu_nsleep,
1559
+ .timer_set = posix_cpu_timer_set,
1560
+ .timer_del = posix_cpu_timer_del,
1561
+ .timer_get = posix_cpu_timer_get,
1562
+ .timer_rearm = posix_cpu_timer_rearm,
14391563 };
14401564
14411565 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,
1566
+ .clock_getres = process_cpu_clock_getres,
1567
+ .clock_get_timespec = process_cpu_clock_get,
1568
+ .timer_create = process_cpu_timer_create,
1569
+ .nsleep = process_cpu_nsleep,
14461570 };
14471571
14481572 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,
1573
+ .clock_getres = thread_cpu_clock_getres,
1574
+ .clock_get_timespec = thread_cpu_clock_get,
1575
+ .timer_create = thread_cpu_timer_create,
14521576 };