Blame view

kernel/time/posix-cpu-timers.c 42.4 KB
b24413180   Greg Kroah-Hartman   License cleanup: ...
1
  // SPDX-License-Identifier: GPL-2.0
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2
3
4
  /*
   * Implement CPU time clocks for the POSIX clock interface.
   */
3f07c0144   Ingo Molnar   sched/headers: Pr...
5
  #include <linux/sched/signal.h>
32ef5517c   Ingo Molnar   sched/headers: Pr...
6
  #include <linux/sched/cputime.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
7
  #include <linux/posix-timers.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
8
  #include <linux/errno.h>
f8bd2258e   Roman Zippel   remove div_long_l...
9
  #include <linux/math64.h>
7c0f6ba68   Linus Torvalds   Replace <asm/uacc...
10
  #include <linux/uaccess.h>
bb34d92f6   Frank Mayhar   timers: fix itime...
11
  #include <linux/kernel_stat.h>
3f0a525eb   Xiao Guangrong   itimers: Add trac...
12
  #include <trace/events/timer.h>
a85721601   Frederic Weisbecker   posix_timers: Kic...
13
14
  #include <linux/tick.h>
  #include <linux/workqueue.h>
edbeda463   Al Viro   time/posix-timers...
15
  #include <linux/compat.h>
34be39305   Juri Lelli   sched/deadline: I...
16
  #include <linux/sched/deadline.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
17

bab0aae9d   Thomas Gleixner   posix-timers: Mov...
18
  #include "posix-timers.h"
f37fb0aa4   Thomas Gleixner   posix-timers: Use...
19
  static void posix_cpu_timer_rearm(struct k_itimer *timer);
3a245c0f1   Thomas Gleixner   posix-cpu-timers:...
20
21
22
  void posix_cputimers_group_init(struct posix_cputimers *pct, u64 cpu_limit)
  {
  	posix_cputimers_init(pct);
244d49e30   Thomas Gleixner   posix-cpu-timers:...
23
  	if (cpu_limit != RLIM_INFINITY) {
87dc64480   Thomas Gleixner   posix-cpu-timers:...
24
  		pct->bases[CPUCLOCK_PROF].nextevt = cpu_limit * NSEC_PER_SEC;
244d49e30   Thomas Gleixner   posix-cpu-timers:...
25
26
  		pct->timers_active = true;
  	}
3a245c0f1   Thomas Gleixner   posix-cpu-timers:...
27
  }
f06febc96   Frank Mayhar   timers: fix itime...
28
  /*
f55db6090   Stanislaw Gruszka   cpu-timers: Simpl...
29
   * Called after updating RLIMIT_CPU to run cpu timer and update
87dc64480   Thomas Gleixner   posix-cpu-timers:...
30
31
   * tsk->signal->posix_cputimers.bases[clock].nextevt expiration cache if
   * necessary. Needs siglock protection since other code may update the
3a245c0f1   Thomas Gleixner   posix-cpu-timers:...
32
   * expiration cache as well.
f06febc96   Frank Mayhar   timers: fix itime...
33
   */
5ab46b345   Jiri Slaby   rlimits: add task...
34
  void update_rlimit_cpu(struct task_struct *task, unsigned long rlim_new)
f06febc96   Frank Mayhar   timers: fix itime...
35
  {
858cf3a8c   Frederic Weisbecker   timers/itimer: Co...
36
  	u64 nsecs = rlim_new * NSEC_PER_SEC;
f06febc96   Frank Mayhar   timers: fix itime...
37

5ab46b345   Jiri Slaby   rlimits: add task...
38
  	spin_lock_irq(&task->sighand->siglock);
858cf3a8c   Frederic Weisbecker   timers/itimer: Co...
39
  	set_process_cpu_timer(task, CPUCLOCK_PROF, &nsecs, NULL);
5ab46b345   Jiri Slaby   rlimits: add task...
40
  	spin_unlock_irq(&task->sighand->siglock);
f06febc96   Frank Mayhar   timers: fix itime...
41
  }
6ae40e3fd   Thomas Gleixner   posix-cpu-timers:...
42
43
44
  /*
   * Functions for validating access to tasks.
   */
964987738   Eric W. Biederman   posix-cpu-timers:...
45
  static struct pid *pid_for_clock(const clockid_t clock, bool gettime)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
46
  {
964987738   Eric W. Biederman   posix-cpu-timers:...
47
48
49
50
51
52
  	const bool thread = !!CPUCLOCK_PERTHREAD(clock);
  	const pid_t upid = CPUCLOCK_PID(clock);
  	struct pid *pid;
  
  	if (CPUCLOCK_WHICH(clock) >= CPUCLOCK_MAX)
  		return NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
53

77b4b5420   Thomas Gleixner   posix-cpu-timers:...
54
55
56
57
  	/*
  	 * If the encoded PID is 0, then the timer is targeted at current
  	 * or the process to which current belongs.
  	 */
964987738   Eric W. Biederman   posix-cpu-timers:...
58
59
  	if (upid == 0)
  		return thread ? task_pid(current) : task_tgid(current);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
60

964987738   Eric W. Biederman   posix-cpu-timers:...
61
62
63
  	pid = find_vpid(upid);
  	if (!pid)
  		return NULL;
77b4b5420   Thomas Gleixner   posix-cpu-timers:...
64

964987738   Eric W. Biederman   posix-cpu-timers:...
65
66
67
68
  	if (thread) {
  		struct task_struct *tsk = pid_task(pid, PIDTYPE_PID);
  		return (tsk && same_thread_group(tsk, current)) ? pid : NULL;
  	}
77b4b5420   Thomas Gleixner   posix-cpu-timers:...
69

c7f519405   Eric W. Biederman   posix-cpu-timer: ...
70
  	/*
964987738   Eric W. Biederman   posix-cpu-timers:...
71
72
73
74
  	 * For clock_gettime(PROCESS) allow finding the process by
  	 * with the pid of the current task.  The code needs the tgid
  	 * of the process so that pid_task(pid, PIDTYPE_TGID) can be
  	 * used to find the process.
c7f519405   Eric W. Biederman   posix-cpu-timer: ...
75
  	 */
964987738   Eric W. Biederman   posix-cpu-timers:...
76
77
  	if (gettime && (pid == task_pid(current)))
  		return task_tgid(current);
77b4b5420   Thomas Gleixner   posix-cpu-timers:...
78
79
  
  	/*
964987738   Eric W. Biederman   posix-cpu-timers:...
80
  	 * For processes require that pid identifies a process.
77b4b5420   Thomas Gleixner   posix-cpu-timers:...
81
  	 */
964987738   Eric W. Biederman   posix-cpu-timers:...
82
  	return pid_has_task(pid, PIDTYPE_TGID) ? pid : NULL;
6ae40e3fd   Thomas Gleixner   posix-cpu-timers:...
83
84
85
86
  }
  
  static inline int validate_clock_permissions(const clockid_t clock)
  {
9bf7c3240   Eric W. Biederman   posix-cpu-timers:...
87
88
89
  	int ret;
  
  	rcu_read_lock();
964987738   Eric W. Biederman   posix-cpu-timers:...
90
  	ret = pid_for_clock(clock, false) ? 0 : -EINVAL;
9bf7c3240   Eric W. Biederman   posix-cpu-timers:...
91
92
93
  	rcu_read_unlock();
  
  	return ret;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
94
  }
fece98260   Eric W. Biederman   posix-cpu-timers:...
95
  static inline enum pid_type clock_pid_type(const clockid_t clock)
55e8c8eb2   Eric W. Biederman   posix-cpu-timers:...
96
  {
fece98260   Eric W. Biederman   posix-cpu-timers:...
97
  	return CPUCLOCK_PERTHREAD(clock) ? PIDTYPE_PID : PIDTYPE_TGID;
55e8c8eb2   Eric W. Biederman   posix-cpu-timers:...
98
99
100
101
  }
  
  static inline struct task_struct *cpu_timer_task_rcu(struct k_itimer *timer)
  {
fece98260   Eric W. Biederman   posix-cpu-timers:...
102
  	return pid_task(timer->it.cpu.pid, clock_pid_type(timer->it_clock));
55e8c8eb2   Eric W. Biederman   posix-cpu-timers:...
103
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
104
105
106
107
  /*
   * Update expiry time from increment, and increase overrun count,
   * given the current clock sample.
   */
60bda037f   Thomas Gleixner   posix-cpu-timers:...
108
  static u64 bump_cpu_timer(struct k_itimer *timer, u64 now)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
109
  {
60bda037f   Thomas Gleixner   posix-cpu-timers:...
110
  	u64 delta, incr, expires = timer->it.cpu.node.expires;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
111
  	int i;
16118794e   Thomas Gleixner   posix-cpu-timers:...
112
  	if (!timer->it_interval)
60bda037f   Thomas Gleixner   posix-cpu-timers:...
113
  		return expires;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
114

60bda037f   Thomas Gleixner   posix-cpu-timers:...
115
116
  	if (now < expires)
  		return expires;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
117

16118794e   Thomas Gleixner   posix-cpu-timers:...
118
  	incr = timer->it_interval;
60bda037f   Thomas Gleixner   posix-cpu-timers:...
119
  	delta = now + incr - expires;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
120

55ccb616a   Frederic Weisbecker   posix_cpu_timer: ...
121
122
123
124
125
126
127
  	/* Don't use (incr*2 < delta), incr*2 might overflow. */
  	for (i = 0; incr < delta - incr; i++)
  		incr = incr << 1;
  
  	for (; i >= 0; incr >>= 1, i--) {
  		if (delta < incr)
  			continue;
60bda037f   Thomas Gleixner   posix-cpu-timers:...
128
  		timer->it.cpu.node.expires += incr;
78c9c4dfb   Thomas Gleixner   posix-timers: San...
129
  		timer->it_overrun += 1LL << i;
55ccb616a   Frederic Weisbecker   posix_cpu_timer: ...
130
  		delta -= incr;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
131
  	}
60bda037f   Thomas Gleixner   posix-cpu-timers:...
132
  	return timer->it.cpu.node.expires;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
133
  }
2bbdbdae0   Thomas Gleixner   posix-cpu-timers:...
134
135
  /* Check whether all cache entries contain U64_MAX, i.e. eternal expiry time */
  static inline bool expiry_cache_is_inactive(const struct posix_cputimers *pct)
555347f6c   Frederic Weisbecker   posix_timers: New...
136
  {
2bbdbdae0   Thomas Gleixner   posix-cpu-timers:...
137
138
139
  	return !(~pct->bases[CPUCLOCK_PROF].nextevt |
  		 ~pct->bases[CPUCLOCK_VIRT].nextevt |
  		 ~pct->bases[CPUCLOCK_SCHED].nextevt);
555347f6c   Frederic Weisbecker   posix_timers: New...
140
  }
bc2c8ea48   Thomas Gleixner   posix-timers: Mak...
141
  static int
d2e3e0ca5   Deepa Dinamani   time: Change k_cl...
142
  posix_cpu_clock_getres(const clockid_t which_clock, struct timespec64 *tp)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
143
  {
6ae40e3fd   Thomas Gleixner   posix-cpu-timers:...
144
  	int error = validate_clock_permissions(which_clock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
145
146
147
148
149
150
151
152
153
154
155
156
157
158
  	if (!error) {
  		tp->tv_sec = 0;
  		tp->tv_nsec = ((NSEC_PER_SEC + HZ - 1) / HZ);
  		if (CPUCLOCK_WHICH(which_clock) == CPUCLOCK_SCHED) {
  			/*
  			 * If sched_clock is using a cycle counter, we
  			 * don't have any idea of its true resolution
  			 * exported, but it is much more than 1s/HZ.
  			 */
  			tp->tv_nsec = 1;
  		}
  	}
  	return error;
  }
bc2c8ea48   Thomas Gleixner   posix-timers: Mak...
159
  static int
6ae40e3fd   Thomas Gleixner   posix-cpu-timers:...
160
  posix_cpu_clock_set(const clockid_t clock, const struct timespec64 *tp)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
161
  {
6ae40e3fd   Thomas Gleixner   posix-cpu-timers:...
162
  	int error = validate_clock_permissions(clock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
163
164
165
166
  	/*
  	 * You can never reset a CPU clock, but we check for other errors
  	 * in the call before failing with EPERM.
  	 */
6ae40e3fd   Thomas Gleixner   posix-cpu-timers:...
167
  	return error ? : -EPERM;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
168
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
169
  /*
2092c1d4f   Thomas Gleixner   posix-cpu-timers:...
170
   * Sample a per-thread clock for the given task. clkid is validated.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
171
   */
8c2d74f03   Thomas Gleixner   posix-cpu-timers:...
172
  static u64 cpu_clock_sample(const clockid_t clkid, struct task_struct *p)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
173
  {
ab693c5a5   Thomas Gleixner   posix-cpu-timers:...
174
175
176
177
178
179
  	u64 utime, stime;
  
  	if (clkid == CPUCLOCK_SCHED)
  		return task_sched_runtime(p);
  
  	task_cputime(p, &utime, &stime);
2092c1d4f   Thomas Gleixner   posix-cpu-timers:...
180
  	switch (clkid) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
181
  	case CPUCLOCK_PROF:
ab693c5a5   Thomas Gleixner   posix-cpu-timers:...
182
  		return utime + stime;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
183
  	case CPUCLOCK_VIRT:
ab693c5a5   Thomas Gleixner   posix-cpu-timers:...
184
  		return utime;
2092c1d4f   Thomas Gleixner   posix-cpu-timers:...
185
186
  	default:
  		WARN_ON_ONCE(1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
187
  	}
8c2d74f03   Thomas Gleixner   posix-cpu-timers:...
188
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
189
  }
b0d524f77   Thomas Gleixner   posix-cpu-timers:...
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
  static inline void store_samples(u64 *samples, u64 stime, u64 utime, u64 rtime)
  {
  	samples[CPUCLOCK_PROF] = stime + utime;
  	samples[CPUCLOCK_VIRT] = utime;
  	samples[CPUCLOCK_SCHED] = rtime;
  }
  
  static void task_sample_cputime(struct task_struct *p, u64 *samples)
  {
  	u64 stime, utime;
  
  	task_cputime(p, &utime, &stime);
  	store_samples(samples, stime, utime, p->se.sum_exec_runtime);
  }
  
  static void proc_sample_cputime_atomic(struct task_cputime_atomic *at,
  				       u64 *samples)
  {
  	u64 stime, utime, rtime;
  
  	utime = atomic64_read(&at->utime);
  	stime = atomic64_read(&at->stime);
  	rtime = atomic64_read(&at->sum_exec_runtime);
  	store_samples(samples, stime, utime, rtime);
  }
1018016c7   Jason Low   sched, timer: Rep...
215
216
217
218
219
  /*
   * Set cputime to sum_cputime if sum_cputime > cputime. Use cmpxchg
   * to avoid race conditions with concurrent updates to cputime.
   */
  static inline void __update_gt_cputime(atomic64_t *cputime, u64 sum_cputime)
4da94d49b   Peter Zijlstra   timers: fix TIMER...
220
  {
1018016c7   Jason Low   sched, timer: Rep...
221
222
223
224
225
226
227
228
  	u64 curr_cputime;
  retry:
  	curr_cputime = atomic64_read(cputime);
  	if (sum_cputime > curr_cputime) {
  		if (atomic64_cmpxchg(cputime, curr_cputime, sum_cputime) != curr_cputime)
  			goto retry;
  	}
  }
4da94d49b   Peter Zijlstra   timers: fix TIMER...
229

b7be4ef13   Thomas Gleixner   posix-cpu-timers:...
230
231
  static void update_gt_cputime(struct task_cputime_atomic *cputime_atomic,
  			      struct task_cputime *sum)
1018016c7   Jason Low   sched, timer: Rep...
232
  {
711074451   Jason Low   sched, timer: Use...
233
234
235
  	__update_gt_cputime(&cputime_atomic->utime, sum->utime);
  	__update_gt_cputime(&cputime_atomic->stime, sum->stime);
  	__update_gt_cputime(&cputime_atomic->sum_exec_runtime, sum->sum_exec_runtime);
1018016c7   Jason Low   sched, timer: Rep...
236
  }
4da94d49b   Peter Zijlstra   timers: fix TIMER...
237

19298fbf4   Thomas Gleixner   posix-cpu-timers:...
238
239
240
  /**
   * thread_group_sample_cputime - Sample cputime for a given task
   * @tsk:	Task for which cputime needs to be started
7f2cbcbca   Yi Wang   posix-cpu-timers:...
241
   * @samples:	Storage for time samples
19298fbf4   Thomas Gleixner   posix-cpu-timers:...
242
243
244
245
246
247
248
   *
   * Called from sys_getitimer() to calculate the expiry time of an active
   * timer. That means group cputime accounting is already active. Called
   * with task sighand lock held.
   *
   * Updates @times with an uptodate sample of the thread group cputimes.
   */
b7be4ef13   Thomas Gleixner   posix-cpu-timers:...
249
  void thread_group_sample_cputime(struct task_struct *tsk, u64 *samples)
19298fbf4   Thomas Gleixner   posix-cpu-timers:...
250
251
  {
  	struct thread_group_cputimer *cputimer = &tsk->signal->cputimer;
244d49e30   Thomas Gleixner   posix-cpu-timers:...
252
  	struct posix_cputimers *pct = &tsk->signal->posix_cputimers;
19298fbf4   Thomas Gleixner   posix-cpu-timers:...
253

244d49e30   Thomas Gleixner   posix-cpu-timers:...
254
  	WARN_ON_ONCE(!pct->timers_active);
19298fbf4   Thomas Gleixner   posix-cpu-timers:...
255

b7be4ef13   Thomas Gleixner   posix-cpu-timers:...
256
  	proc_sample_cputime_atomic(&cputimer->cputime_atomic, samples);
19298fbf4   Thomas Gleixner   posix-cpu-timers:...
257
  }
c506bef42   Thomas Gleixner   posix-cpu-timers:...
258
259
260
  /**
   * thread_group_start_cputime - Start cputime and return a sample
   * @tsk:	Task for which cputime needs to be started
b7be4ef13   Thomas Gleixner   posix-cpu-timers:...
261
   * @samples:	Storage for time samples
c506bef42   Thomas Gleixner   posix-cpu-timers:...
262
263
264
265
266
267
268
269
   *
   * The thread group cputime accouting is avoided when there are no posix
   * CPU timers armed. Before starting a timer it's required to check whether
   * the time accounting is active. If not, a full update of the atomic
   * accounting store needs to be done and the accounting enabled.
   *
   * Updates @times with an uptodate sample of the thread group cputimes.
   */
b7be4ef13   Thomas Gleixner   posix-cpu-timers:...
270
  static void thread_group_start_cputime(struct task_struct *tsk, u64 *samples)
4da94d49b   Peter Zijlstra   timers: fix TIMER...
271
272
  {
  	struct thread_group_cputimer *cputimer = &tsk->signal->cputimer;
244d49e30   Thomas Gleixner   posix-cpu-timers:...
273
  	struct posix_cputimers *pct = &tsk->signal->posix_cputimers;
4da94d49b   Peter Zijlstra   timers: fix TIMER...
274

1018016c7   Jason Low   sched, timer: Rep...
275
  	/* Check if cputimer isn't running. This is accessed without locking. */
244d49e30   Thomas Gleixner   posix-cpu-timers:...
276
  	if (!READ_ONCE(pct->timers_active)) {
b7be4ef13   Thomas Gleixner   posix-cpu-timers:...
277
  		struct task_cputime sum;
4da94d49b   Peter Zijlstra   timers: fix TIMER...
278
279
280
  		/*
  		 * The POSIX timer interface allows for absolute time expiry
  		 * values through the TIMER_ABSTIME flag, therefore we have
1018016c7   Jason Low   sched, timer: Rep...
281
  		 * to synchronize the timer to the clock every time we start it.
4da94d49b   Peter Zijlstra   timers: fix TIMER...
282
  		 */
ebd7e7fc4   Frederic Weisbecker   timers/posix-time...
283
  		thread_group_cputime(tsk, &sum);
711074451   Jason Low   sched, timer: Use...
284
  		update_gt_cputime(&cputimer->cputime_atomic, &sum);
1018016c7   Jason Low   sched, timer: Rep...
285
286
  
  		/*
244d49e30   Thomas Gleixner   posix-cpu-timers:...
287
288
289
290
  		 * We're setting timers_active without a lock. Ensure this
  		 * only gets written to in one operation. We set it after
  		 * update_gt_cputime() as a small optimization, but
  		 * barriers are not required because update_gt_cputime()
1018016c7   Jason Low   sched, timer: Rep...
291
292
  		 * can handle concurrent updates.
  		 */
244d49e30   Thomas Gleixner   posix-cpu-timers:...
293
  		WRITE_ONCE(pct->timers_active, true);
1018016c7   Jason Low   sched, timer: Rep...
294
  	}
b7be4ef13   Thomas Gleixner   posix-cpu-timers:...
295
296
297
298
299
300
301
302
303
  	proc_sample_cputime_atomic(&cputimer->cputime_atomic, samples);
  }
  
  static void __thread_group_cputime(struct task_struct *tsk, u64 *samples)
  {
  	struct task_cputime ct;
  
  	thread_group_cputime(tsk, &ct);
  	store_samples(samples, ct.stime, ct.utime, ct.sum_exec_runtime);
4da94d49b   Peter Zijlstra   timers: fix TIMER...
304
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
305
  /*
24ab7f5a7   Thomas Gleixner   posix-cpu-timers:...
306
307
   * Sample a process (thread group) clock for the given task clkid. If the
   * group's cputime accounting is already enabled, read the atomic
a2efdbf4f   Eric W. Biederman   posix-cpu-timers:...
308
   * store. Otherwise a full update is required.  clkid is already validated.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
309
   */
8c2d74f03   Thomas Gleixner   posix-cpu-timers:...
310
311
  static u64 cpu_clock_sample_group(const clockid_t clkid, struct task_struct *p,
  				  bool start)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
312
  {
24ab7f5a7   Thomas Gleixner   posix-cpu-timers:...
313
  	struct thread_group_cputimer *cputimer = &p->signal->cputimer;
244d49e30   Thomas Gleixner   posix-cpu-timers:...
314
  	struct posix_cputimers *pct = &p->signal->posix_cputimers;
b7be4ef13   Thomas Gleixner   posix-cpu-timers:...
315
  	u64 samples[CPUCLOCK_MAX];
f06febc96   Frank Mayhar   timers: fix itime...
316

244d49e30   Thomas Gleixner   posix-cpu-timers:...
317
  	if (!READ_ONCE(pct->timers_active)) {
24ab7f5a7   Thomas Gleixner   posix-cpu-timers:...
318
  		if (start)
b7be4ef13   Thomas Gleixner   posix-cpu-timers:...
319
  			thread_group_start_cputime(p, samples);
24ab7f5a7   Thomas Gleixner   posix-cpu-timers:...
320
  		else
b7be4ef13   Thomas Gleixner   posix-cpu-timers:...
321
  			__thread_group_cputime(p, samples);
24ab7f5a7   Thomas Gleixner   posix-cpu-timers:...
322
  	} else {
b7be4ef13   Thomas Gleixner   posix-cpu-timers:...
323
  		proc_sample_cputime_atomic(&cputimer->cputime_atomic, samples);
24ab7f5a7   Thomas Gleixner   posix-cpu-timers:...
324
  	}
b7be4ef13   Thomas Gleixner   posix-cpu-timers:...
325
  	return samples[clkid];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
326
  }
bfcf3e92c   Thomas Gleixner   posix-cpu-timers:...
327
  static int posix_cpu_clock_get(const clockid_t clock, struct timespec64 *tp)
33ab0fec3   Frederic Weisbecker   posix-timers: Con...
328
  {
bfcf3e92c   Thomas Gleixner   posix-cpu-timers:...
329
330
331
  	const clockid_t clkid = CPUCLOCK_WHICH(clock);
  	struct task_struct *tsk;
  	u64 t;
33ab0fec3   Frederic Weisbecker   posix-timers: Con...
332

9bf7c3240   Eric W. Biederman   posix-cpu-timers:...
333
  	rcu_read_lock();
964987738   Eric W. Biederman   posix-cpu-timers:...
334
  	tsk = pid_task(pid_for_clock(clock, true), clock_pid_type(clock));
9bf7c3240   Eric W. Biederman   posix-cpu-timers:...
335
336
  	if (!tsk) {
  		rcu_read_unlock();
bfcf3e92c   Thomas Gleixner   posix-cpu-timers:...
337
  		return -EINVAL;
9bf7c3240   Eric W. Biederman   posix-cpu-timers:...
338
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
339

bfcf3e92c   Thomas Gleixner   posix-cpu-timers:...
340
  	if (CPUCLOCK_PERTHREAD(clock))
8c2d74f03   Thomas Gleixner   posix-cpu-timers:...
341
  		t = cpu_clock_sample(clkid, tsk);
bfcf3e92c   Thomas Gleixner   posix-cpu-timers:...
342
  	else
8c2d74f03   Thomas Gleixner   posix-cpu-timers:...
343
  		t = cpu_clock_sample_group(clkid, tsk, false);
9bf7c3240   Eric W. Biederman   posix-cpu-timers:...
344
  	rcu_read_unlock();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
345

bfcf3e92c   Thomas Gleixner   posix-cpu-timers:...
346
347
  	*tp = ns_to_timespec64(t);
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
348
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
349
350
  /*
   * Validate the clockid_t for a new CPU-clock timer, and initialize the timer.
ba5ea951d   Stanislaw Gruszka   posix-cpu-timers:...
351
352
   * This is called from sys_timer_create() and do_cpu_nanosleep() with the
   * new timer already all-zeros initialized.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
353
   */
bc2c8ea48   Thomas Gleixner   posix-timers: Mak...
354
  static int posix_cpu_timer_create(struct k_itimer *new_timer)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
355
  {
1fb497dd0   Thomas Gleixner   posix-cpu-timers:...
356
  	static struct lock_class_key posix_cpu_timers_key;
964987738   Eric W. Biederman   posix-cpu-timers:...
357
  	struct pid *pid;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
358

9bf7c3240   Eric W. Biederman   posix-cpu-timers:...
359
  	rcu_read_lock();
964987738   Eric W. Biederman   posix-cpu-timers:...
360
361
  	pid = pid_for_clock(new_timer->it_clock, false);
  	if (!pid) {
9bf7c3240   Eric W. Biederman   posix-cpu-timers:...
362
  		rcu_read_unlock();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
363
  		return -EINVAL;
9bf7c3240   Eric W. Biederman   posix-cpu-timers:...
364
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
365

1fb497dd0   Thomas Gleixner   posix-cpu-timers:...
366
367
368
369
370
371
372
373
374
375
  	/*
  	 * If posix timer expiry is handled in task work context then
  	 * timer::it_lock can be taken without disabling interrupts as all
  	 * other locking happens in task context. This requires a seperate
  	 * lock class key otherwise regular posix timer expiry would record
  	 * the lock class being taken in interrupt context and generate a
  	 * false positive warning.
  	 */
  	if (IS_ENABLED(CONFIG_POSIX_CPU_TIMERS_TASK_WORK))
  		lockdep_set_class(&new_timer->it_lock, &posix_cpu_timers_key);
d97bb75dd   Thomas Gleixner   posix-timers: Sto...
376
  	new_timer->kclock = &clock_posix_cpu;
60bda037f   Thomas Gleixner   posix-cpu-timers:...
377
  	timerqueue_init(&new_timer->it.cpu.node);
964987738   Eric W. Biederman   posix-cpu-timers:...
378
  	new_timer->it.cpu.pid = get_pid(pid);
9bf7c3240   Eric W. Biederman   posix-cpu-timers:...
379
  	rcu_read_unlock();
e5a8b65b4   Thomas Gleixner   posix-cpu-timers:...
380
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
381
382
383
384
385
386
387
388
  }
  
  /*
   * Clean up a CPU-clock timer that is about to be destroyed.
   * This is called from timer deletion with the timer already locked.
   * If we return TIMER_RETRY, it's necessary to release the timer's lock
   * and try again.  (This happens when the timer is in the middle of firing.)
   */
bc2c8ea48   Thomas Gleixner   posix-timers: Mak...
389
  static int posix_cpu_timer_del(struct k_itimer *timer)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
390
  {
60bda037f   Thomas Gleixner   posix-cpu-timers:...
391
  	struct cpu_timer *ctmr = &timer->it.cpu;
3d7a1427e   Frederic Weisbecker   posix-timers: Use...
392
  	struct sighand_struct *sighand;
55e8c8eb2   Eric W. Biederman   posix-cpu-timers:...
393
  	struct task_struct *p;
60bda037f   Thomas Gleixner   posix-cpu-timers:...
394
395
  	unsigned long flags;
  	int ret = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
396

55e8c8eb2   Eric W. Biederman   posix-cpu-timers:...
397
398
399
400
  	rcu_read_lock();
  	p = cpu_timer_task_rcu(timer);
  	if (!p)
  		goto out;
108150ea7   Oleg Nesterov   [PATCH] posix-tim...
401

3d7a1427e   Frederic Weisbecker   posix-timers: Use...
402
403
404
405
406
407
  	/*
  	 * Protect against sighand release/switch in exit/exec and process/
  	 * thread timer list entry concurrent read/writes.
  	 */
  	sighand = lock_task_sighand(p, &flags);
  	if (unlikely(sighand == NULL)) {
a3222f88f   Frederic Weisbecker   posix-timers: Rem...
408
  		/*
60bda037f   Thomas Gleixner   posix-cpu-timers:...
409
410
  		 * This raced with the reaping of the task. The exit cleanup
  		 * should have removed this timer from the timer queue.
a3222f88f   Frederic Weisbecker   posix-timers: Rem...
411
  		 */
60bda037f   Thomas Gleixner   posix-cpu-timers:...
412
  		WARN_ON_ONCE(ctmr->head || timerqueue_node_queued(&ctmr->node));
a3222f88f   Frederic Weisbecker   posix-timers: Rem...
413
  	} else {
a3222f88f   Frederic Weisbecker   posix-timers: Rem...
414
415
416
  		if (timer->it.cpu.firing)
  			ret = TIMER_RETRY;
  		else
60bda037f   Thomas Gleixner   posix-cpu-timers:...
417
  			cpu_timer_dequeue(ctmr);
3d7a1427e   Frederic Weisbecker   posix-timers: Use...
418
419
  
  		unlock_task_sighand(p, &flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
420
  	}
a3222f88f   Frederic Weisbecker   posix-timers: Rem...
421

55e8c8eb2   Eric W. Biederman   posix-cpu-timers:...
422
423
  out:
  	rcu_read_unlock();
a3222f88f   Frederic Weisbecker   posix-timers: Rem...
424
  	if (!ret)
55e8c8eb2   Eric W. Biederman   posix-cpu-timers:...
425
  		put_pid(ctmr->pid);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
426

108150ea7   Oleg Nesterov   [PATCH] posix-tim...
427
  	return ret;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
428
  }
60bda037f   Thomas Gleixner   posix-cpu-timers:...
429
  static void cleanup_timerqueue(struct timerqueue_head *head)
1a7fa510b   Frederic Weisbecker   posix_cpu_timers:...
430
  {
60bda037f   Thomas Gleixner   posix-cpu-timers:...
431
432
  	struct timerqueue_node *node;
  	struct cpu_timer *ctmr;
1a7fa510b   Frederic Weisbecker   posix_cpu_timers:...
433

60bda037f   Thomas Gleixner   posix-cpu-timers:...
434
435
436
437
438
  	while ((node = timerqueue_getnext(head))) {
  		timerqueue_del(head, node);
  		ctmr = container_of(node, struct cpu_timer, node);
  		ctmr->head = NULL;
  	}
1a7fa510b   Frederic Weisbecker   posix_cpu_timers:...
439
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
440
  /*
7cb9a94c1   Thomas Gleixner   posix-cpu-timers:...
441
442
443
444
   * Clean out CPU timers which are still armed when a thread exits. The
   * timers are only removed from the list. No other updates are done. The
   * corresponding posix timers are still accessible, but cannot be rearmed.
   *
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
445
446
   * This must be called with the siglock held.
   */
2b69942f9   Thomas Gleixner   posix-cpu-timers:...
447
  static void cleanup_timers(struct posix_cputimers *pct)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
448
  {
60bda037f   Thomas Gleixner   posix-cpu-timers:...
449
450
451
  	cleanup_timerqueue(&pct->bases[CPUCLOCK_PROF].tqhead);
  	cleanup_timerqueue(&pct->bases[CPUCLOCK_VIRT].tqhead);
  	cleanup_timerqueue(&pct->bases[CPUCLOCK_SCHED].tqhead);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
452
453
454
455
456
457
458
459
460
  }
  
  /*
   * These are both called with the siglock held, when the current thread
   * is being reaped.  When the final (leader) thread in the group is reaped,
   * posix_cpu_timers_exit_group will be called after posix_cpu_timers_exit.
   */
  void posix_cpu_timers_exit(struct task_struct *tsk)
  {
2b69942f9   Thomas Gleixner   posix-cpu-timers:...
461
  	cleanup_timers(&tsk->posix_cputimers);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
462
463
464
  }
  void posix_cpu_timers_exit_group(struct task_struct *tsk)
  {
2b69942f9   Thomas Gleixner   posix-cpu-timers:...
465
  	cleanup_timers(&tsk->signal->posix_cputimers);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
466
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
467
468
  /*
   * Insert the timer on the appropriate list before any timers that
e73d84e33   Frederic Weisbecker   posix-timers: Rem...
469
   * expire later.  This must be called with the sighand lock held.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
470
   */
beb41d9cb   Eric W. Biederman   posix-cpu-timers:...
471
  static void arm_timer(struct k_itimer *timer, struct task_struct *p)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
472
  {
3b495b22d   Thomas Gleixner   posix-cpu-timers:...
473
  	int clkidx = CPUCLOCK_WHICH(timer->it_clock);
60bda037f   Thomas Gleixner   posix-cpu-timers:...
474
475
  	struct cpu_timer *ctmr = &timer->it.cpu;
  	u64 newexp = cpu_timer_getexpires(ctmr);
87dc64480   Thomas Gleixner   posix-cpu-timers:...
476
  	struct posix_cputimer_base *base;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
477

87dc64480   Thomas Gleixner   posix-cpu-timers:...
478
479
480
481
  	if (CPUCLOCK_PERTHREAD(timer->it_clock))
  		base = p->posix_cputimers.bases + clkidx;
  	else
  		base = p->signal->posix_cputimers.bases + clkidx;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
482

60bda037f   Thomas Gleixner   posix-cpu-timers:...
483
  	if (!cpu_timer_enqueue(&base->tqhead, ctmr))
3b495b22d   Thomas Gleixner   posix-cpu-timers:...
484
  		return;
5eb9aa641   Stanislaw Gruszka   cpu-timers: Clean...
485

3b495b22d   Thomas Gleixner   posix-cpu-timers:...
486
487
488
489
490
491
  	/*
  	 * We are the new earliest-expiring POSIX 1.b timer, hence
  	 * need to update expiration cache. Take into account that
  	 * for process timers we share expiration cache with itimers
  	 * and RLIMIT_CPU and for thread timers with RLIMIT_RTTIME.
  	 */
2bbdbdae0   Thomas Gleixner   posix-cpu-timers:...
492
  	if (newexp < base->nextevt)
87dc64480   Thomas Gleixner   posix-cpu-timers:...
493
  		base->nextevt = newexp;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
494

3b495b22d   Thomas Gleixner   posix-cpu-timers:...
495
496
497
498
  	if (CPUCLOCK_PERTHREAD(timer->it_clock))
  		tick_dep_set_task(p, TICK_DEP_BIT_POSIX_TIMER);
  	else
  		tick_dep_set_signal(p->signal, TICK_DEP_BIT_POSIX_TIMER);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
499
500
501
502
503
504
505
  }
  
  /*
   * The timer is locked, fire it and arrange for its reload.
   */
  static void cpu_timer_fire(struct k_itimer *timer)
  {
60bda037f   Thomas Gleixner   posix-cpu-timers:...
506
  	struct cpu_timer *ctmr = &timer->it.cpu;
1f169f84d   Stanislaw Gruszka   cpu-timers: Chang...
507
508
509
510
  	if ((timer->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE) {
  		/*
  		 * User don't want any signal.
  		 */
60bda037f   Thomas Gleixner   posix-cpu-timers:...
511
  		cpu_timer_setexpires(ctmr, 0);
1f169f84d   Stanislaw Gruszka   cpu-timers: Chang...
512
  	} else if (unlikely(timer->sigq == NULL)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
513
514
515
516
517
  		/*
  		 * This a special case for clock_nanosleep,
  		 * not a normal timer from sys_timer_create.
  		 */
  		wake_up_process(timer->it_process);
60bda037f   Thomas Gleixner   posix-cpu-timers:...
518
  		cpu_timer_setexpires(ctmr, 0);
16118794e   Thomas Gleixner   posix-cpu-timers:...
519
  	} else if (!timer->it_interval) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
520
521
522
523
  		/*
  		 * One-shot timer.  Clear it as soon as it's fired.
  		 */
  		posix_timer_event(timer, 0);
60bda037f   Thomas Gleixner   posix-cpu-timers:...
524
  		cpu_timer_setexpires(ctmr, 0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
525
526
527
528
529
530
531
  	} else if (posix_timer_event(timer, ++timer->it_requeue_pending)) {
  		/*
  		 * The signal did not get queued because the signal
  		 * was ignored, so we won't get any callback to
  		 * reload the timer.  But we need to keep it
  		 * ticking in case the signal is deliverable next time.
  		 */
f37fb0aa4   Thomas Gleixner   posix-timers: Use...
532
  		posix_cpu_timer_rearm(timer);
af888d677   Thomas Gleixner   posix-timers: Uni...
533
  		++timer->it_requeue_pending;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
534
535
536
537
538
539
540
541
542
  	}
  }
  
  /*
   * Guts of sys_timer_settime for CPU timers.
   * This is called with the timer locked and interrupts disabled.
   * If we return TIMER_RETRY, it's necessary to release the timer's lock
   * and try again.  (This happens when the timer is in the middle of firing.)
   */
e73d84e33   Frederic Weisbecker   posix-timers: Rem...
543
  static int posix_cpu_timer_set(struct k_itimer *timer, int timer_flags,
5f252b325   Deepa Dinamani   time: Change k_cl...
544
  			       struct itimerspec64 *new, struct itimerspec64 *old)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
545
  {
c7a37c6f4   Thomas Gleixner   posix-cpu-timers:...
546
  	clockid_t clkid = CPUCLOCK_WHICH(timer->it_clock);
ebd7e7fc4   Frederic Weisbecker   timers/posix-time...
547
  	u64 old_expires, new_expires, old_incr, val;
60bda037f   Thomas Gleixner   posix-cpu-timers:...
548
  	struct cpu_timer *ctmr = &timer->it.cpu;
c7a37c6f4   Thomas Gleixner   posix-cpu-timers:...
549
  	struct sighand_struct *sighand;
55e8c8eb2   Eric W. Biederman   posix-cpu-timers:...
550
  	struct task_struct *p;
c7a37c6f4   Thomas Gleixner   posix-cpu-timers:...
551
  	unsigned long flags;
60bda037f   Thomas Gleixner   posix-cpu-timers:...
552
  	int ret = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
553

55e8c8eb2   Eric W. Biederman   posix-cpu-timers:...
554
555
556
557
558
559
560
561
562
563
  	rcu_read_lock();
  	p = cpu_timer_task_rcu(timer);
  	if (!p) {
  		/*
  		 * If p has just been reaped, we can no
  		 * longer get any information about it at all.
  		 */
  		rcu_read_unlock();
  		return -ESRCH;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
564

098b0e01a   Thomas Gleixner   posix-cpu-timers:...
565
566
567
568
569
  	/*
  	 * Use the to_ktime conversion because that clamps the maximum
  	 * value to KTIME_MAX and avoid multiplication overflows.
  	 */
  	new_expires = ktime_to_ns(timespec64_to_ktime(new->it_value));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
570

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
571
  	/*
e73d84e33   Frederic Weisbecker   posix-timers: Rem...
572
573
574
575
576
577
  	 * Protect against sighand release/switch in exit/exec and p->cpu_timers
  	 * and p->signal->cpu_timers read/write in arm_timer()
  	 */
  	sighand = lock_task_sighand(p, &flags);
  	/*
  	 * If p has just been reaped, we can no
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
578
579
  	 * longer get any information about it at all.
  	 */
55e8c8eb2   Eric W. Biederman   posix-cpu-timers:...
580
581
  	if (unlikely(sighand == NULL)) {
  		rcu_read_unlock();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
582
  		return -ESRCH;
55e8c8eb2   Eric W. Biederman   posix-cpu-timers:...
583
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
584
585
586
587
  
  	/*
  	 * Disarm any old timer after extracting its expiry time.
  	 */
16118794e   Thomas Gleixner   posix-cpu-timers:...
588
  	old_incr = timer->it_interval;
60bda037f   Thomas Gleixner   posix-cpu-timers:...
589
  	old_expires = cpu_timer_getexpires(ctmr);
a69ac4a78   Oleg Nesterov   [PATCH] posix-tim...
590
591
592
  	if (unlikely(timer->it.cpu.firing)) {
  		timer->it.cpu.firing = -1;
  		ret = TIMER_RETRY;
60bda037f   Thomas Gleixner   posix-cpu-timers:...
593
594
595
  	} else {
  		cpu_timer_dequeue(ctmr);
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
596
597
598
599
600
601
602
603
604
  
  	/*
  	 * We need to sample the current value to convert the new
  	 * value from to relative and absolute, and to convert the
  	 * old value from absolute to relative.  To set a process
  	 * timer, we need a sample to balance the thread expiry
  	 * times (in arm_timer).  With an absolute time, we must
  	 * check if it's already passed.  In short, we need a sample.
  	 */
8c2d74f03   Thomas Gleixner   posix-cpu-timers:...
605
606
607
608
  	if (CPUCLOCK_PERTHREAD(timer->it_clock))
  		val = cpu_clock_sample(clkid, p);
  	else
  		val = cpu_clock_sample_group(clkid, p, true);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
609
610
  
  	if (old) {
55ccb616a   Frederic Weisbecker   posix_cpu_timer: ...
611
  		if (old_expires == 0) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
612
613
614
615
  			old->it_value.tv_sec = 0;
  			old->it_value.tv_nsec = 0;
  		} else {
  			/*
60bda037f   Thomas Gleixner   posix-cpu-timers:...
616
617
618
619
620
  			 * Update the timer in case it has overrun already.
  			 * If it has, we'll report it as having overrun and
  			 * with the next reloaded timer already ticking,
  			 * though we are swallowing that pending
  			 * notification here to install the new setting.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
621
  			 */
60bda037f   Thomas Gleixner   posix-cpu-timers:...
622
623
624
625
  			u64 exp = bump_cpu_timer(timer, val);
  
  			if (val < exp) {
  				old_expires = exp - val;
5f252b325   Deepa Dinamani   time: Change k_cl...
626
  				old->it_value = ns_to_timespec64(old_expires);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
627
628
629
630
631
632
  			} else {
  				old->it_value.tv_nsec = 1;
  				old->it_value.tv_sec = 0;
  			}
  		}
  	}
a69ac4a78   Oleg Nesterov   [PATCH] posix-tim...
633
  	if (unlikely(ret)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
634
635
636
637
638
639
  		/*
  		 * We are colliding with the timer actually firing.
  		 * Punt after filling in the timer's old value, and
  		 * disable this firing since we are already reporting
  		 * it as an overrun (thanks to bump_cpu_timer above).
  		 */
e73d84e33   Frederic Weisbecker   posix-timers: Rem...
640
  		unlock_task_sighand(p, &flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
641
642
  		goto out;
  	}
e73d84e33   Frederic Weisbecker   posix-timers: Rem...
643
  	if (new_expires != 0 && !(timer_flags & TIMER_ABSTIME)) {
55ccb616a   Frederic Weisbecker   posix_cpu_timer: ...
644
  		new_expires += val;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
645
646
647
648
649
650
651
  	}
  
  	/*
  	 * Install the new expiry time (or zero).
  	 * For a timer with no notification action, we don't actually
  	 * arm the timer (we'll just fake it for timer_gettime).
  	 */
60bda037f   Thomas Gleixner   posix-cpu-timers:...
652
  	cpu_timer_setexpires(ctmr, new_expires);
55ccb616a   Frederic Weisbecker   posix_cpu_timer: ...
653
  	if (new_expires != 0 && val < new_expires) {
beb41d9cb   Eric W. Biederman   posix-cpu-timers:...
654
  		arm_timer(timer, p);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
655
  	}
e73d84e33   Frederic Weisbecker   posix-timers: Rem...
656
  	unlock_task_sighand(p, &flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
657
658
659
660
  	/*
  	 * Install the new reload setting, and
  	 * set up the signal and overrun bookkeeping.
  	 */
16118794e   Thomas Gleixner   posix-cpu-timers:...
661
  	timer->it_interval = timespec64_to_ktime(new->it_interval);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
662
663
664
665
666
667
668
669
670
671
  
  	/*
  	 * This acts as a modification timestamp for the timer,
  	 * so any automatic reload attempt will punt on seeing
  	 * that we have reset the timer manually.
  	 */
  	timer->it_requeue_pending = (timer->it_requeue_pending + 2) &
  		~REQUEUE_PENDING;
  	timer->it_overrun_last = 0;
  	timer->it_overrun = -1;
55ccb616a   Frederic Weisbecker   posix_cpu_timer: ...
672
  	if (new_expires != 0 && !(val < new_expires)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
673
674
675
676
677
678
679
680
681
682
  		/*
  		 * The designated time already passed, so we notify
  		 * immediately, even if the thread never runs to
  		 * accumulate more time on this clock.
  		 */
  		cpu_timer_fire(timer);
  	}
  
  	ret = 0;
   out:
55e8c8eb2   Eric W. Biederman   posix-cpu-timers:...
683
  	rcu_read_unlock();
ebd7e7fc4   Frederic Weisbecker   timers/posix-time...
684
  	if (old)
5f252b325   Deepa Dinamani   time: Change k_cl...
685
  		old->it_interval = ns_to_timespec64(old_incr);
b78783000   Frederic Weisbecker   posix-cpu-timers:...
686

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
687
688
  	return ret;
  }
5f252b325   Deepa Dinamani   time: Change k_cl...
689
  static void posix_cpu_timer_get(struct k_itimer *timer, struct itimerspec64 *itp)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
690
  {
99093c5b8   Thomas Gleixner   posix-cpu-timers:...
691
  	clockid_t clkid = CPUCLOCK_WHICH(timer->it_clock);
60bda037f   Thomas Gleixner   posix-cpu-timers:...
692
693
  	struct cpu_timer *ctmr = &timer->it.cpu;
  	u64 now, expires = cpu_timer_getexpires(ctmr);
55e8c8eb2   Eric W. Biederman   posix-cpu-timers:...
694
  	struct task_struct *p;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
695

55e8c8eb2   Eric W. Biederman   posix-cpu-timers:...
696
697
698
699
  	rcu_read_lock();
  	p = cpu_timer_task_rcu(timer);
  	if (!p)
  		goto out;
a3222f88f   Frederic Weisbecker   posix-timers: Rem...
700

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
701
702
703
  	/*
  	 * Easy part: convert the reload time.
  	 */
16118794e   Thomas Gleixner   posix-cpu-timers:...
704
  	itp->it_interval = ktime_to_timespec64(timer->it_interval);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
705

60bda037f   Thomas Gleixner   posix-cpu-timers:...
706
  	if (!expires)
55e8c8eb2   Eric W. Biederman   posix-cpu-timers:...
707
  		goto out;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
708

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
709
710
711
  	/*
  	 * Sample the clock to take the difference with the expiry time.
  	 */
60f2ceaa8   Eric W. Biederman   posix-cpu-timers:...
712
  	if (CPUCLOCK_PERTHREAD(timer->it_clock))
8c2d74f03   Thomas Gleixner   posix-cpu-timers:...
713
  		now = cpu_clock_sample(clkid, p);
60f2ceaa8   Eric W. Biederman   posix-cpu-timers:...
714
715
  	else
  		now = cpu_clock_sample_group(clkid, p, false);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
716

60bda037f   Thomas Gleixner   posix-cpu-timers:...
717
718
  	if (now < expires) {
  		itp->it_value = ns_to_timespec64(expires - now);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
719
720
721
722
723
724
725
726
  	} else {
  		/*
  		 * The timer should have expired already, but the firing
  		 * hasn't taken place yet.  Say it's just about to expire.
  		 */
  		itp->it_value.tv_nsec = 1;
  		itp->it_value.tv_sec = 0;
  	}
55e8c8eb2   Eric W. Biederman   posix-cpu-timers:...
727
728
  out:
  	rcu_read_unlock();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
729
  }
60bda037f   Thomas Gleixner   posix-cpu-timers:...
730
  #define MAX_COLLECTED	20
2473f3e7a   Frederic Weisbecker   posix_cpu_timers:...
731

60bda037f   Thomas Gleixner   posix-cpu-timers:...
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
  static u64 collect_timerqueue(struct timerqueue_head *head,
  			      struct list_head *firing, u64 now)
  {
  	struct timerqueue_node *next;
  	int i = 0;
  
  	while ((next = timerqueue_getnext(head))) {
  		struct cpu_timer *ctmr;
  		u64 expires;
  
  		ctmr = container_of(next, struct cpu_timer, node);
  		expires = cpu_timer_getexpires(ctmr);
  		/* Limit the number of timers to expire at once */
  		if (++i == MAX_COLLECTED || now < expires)
  			return expires;
  
  		ctmr->firing = 1;
  		cpu_timer_dequeue(ctmr);
  		list_add_tail(&ctmr->elist, firing);
2473f3e7a   Frederic Weisbecker   posix_cpu_timers:...
751
  	}
2bbdbdae0   Thomas Gleixner   posix-cpu-timers:...
752
  	return U64_MAX;
2473f3e7a   Frederic Weisbecker   posix_cpu_timers:...
753
  }
60bda037f   Thomas Gleixner   posix-cpu-timers:...
754
755
  static void collect_posix_cputimers(struct posix_cputimers *pct, u64 *samples,
  				    struct list_head *firing)
1cd07c0b9   Thomas Gleixner   posix-cpu-timers:...
756
757
758
759
760
  {
  	struct posix_cputimer_base *base = pct->bases;
  	int i;
  
  	for (i = 0; i < CPUCLOCK_MAX; i++, base++) {
60bda037f   Thomas Gleixner   posix-cpu-timers:...
761
762
  		base->nextevt = collect_timerqueue(&base->tqhead, firing,
  						    samples[i]);
1cd07c0b9   Thomas Gleixner   posix-cpu-timers:...
763
764
  	}
  }
34be39305   Juri Lelli   sched/deadline: I...
765
766
767
768
769
770
771
  static inline void check_dl_overrun(struct task_struct *tsk)
  {
  	if (tsk->dl.dl_overrun) {
  		tsk->dl.dl_overrun = 0;
  		__group_send_sig_info(SIGXCPU, SEND_SIG_PRIV, tsk);
  	}
  }
8991afe26   Thomas Gleixner   posix-cpu-timers:...
772
773
774
775
776
777
778
779
780
781
782
783
784
785
  static bool check_rlimit(u64 time, u64 limit, int signo, bool rt, bool hard)
  {
  	if (time < limit)
  		return false;
  
  	if (print_fatal_signals) {
  		pr_info("%s Watchdog Timeout (%s): %s[%d]
  ",
  			rt ? "RT" : "CPU", hard ? "hard" : "soft",
  			current->comm, task_pid_nr(current));
  	}
  	__group_send_sig_info(signo, SEND_SIG_PRIV, current);
  	return true;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
786
787
788
789
790
791
792
793
  /*
   * Check for any per-thread CPU timers that have fired and move them off
   * the tsk->cpu_timers[N] list onto the firing list.  Here we update the
   * tsk->it_*_expires values to reflect the remaining thread CPU timers.
   */
  static void check_thread_timers(struct task_struct *tsk,
  				struct list_head *firing)
  {
1cd07c0b9   Thomas Gleixner   posix-cpu-timers:...
794
795
  	struct posix_cputimers *pct = &tsk->posix_cputimers;
  	u64 samples[CPUCLOCK_MAX];
d4bb52743   Jiri Slaby   posix-cpu-timers:...
796
  	unsigned long soft;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
797

34be39305   Juri Lelli   sched/deadline: I...
798
799
  	if (dl_task(tsk))
  		check_dl_overrun(tsk);
1cd07c0b9   Thomas Gleixner   posix-cpu-timers:...
800
  	if (expiry_cache_is_inactive(pct))
934715a19   Jason Low   posix_cpu_timer: ...
801
  		return;
1cd07c0b9   Thomas Gleixner   posix-cpu-timers:...
802
803
  	task_sample_cputime(tsk, samples);
  	collect_posix_cputimers(pct, samples, firing);
78f2c7db6   Peter Zijlstra   sched: SCHED_FIFO...
804
805
806
807
  
  	/*
  	 * Check for the special case thread timers.
  	 */
3cf294962   Krzysztof Opasiak   posix-cpu-timers:...
808
  	soft = task_rlimit(tsk, RLIMIT_RTTIME);
d4bb52743   Jiri Slaby   posix-cpu-timers:...
809
  	if (soft != RLIM_INFINITY) {
8ea1de90a   Thomas Gleixner   posix-cpu-timers:...
810
  		/* Task RT timeout is accounted in jiffies. RTTIME is usec */
8991afe26   Thomas Gleixner   posix-cpu-timers:...
811
  		unsigned long rttime = tsk->rt.timeout * (USEC_PER_SEC / HZ);
3cf294962   Krzysztof Opasiak   posix-cpu-timers:...
812
  		unsigned long hard = task_rlimit_max(tsk, RLIMIT_RTTIME);
78f2c7db6   Peter Zijlstra   sched: SCHED_FIFO...
813

8991afe26   Thomas Gleixner   posix-cpu-timers:...
814
815
816
  		/* At the hard limit, send SIGKILL. No further action. */
  		if (hard != RLIM_INFINITY &&
  		    check_rlimit(rttime, hard, SIGKILL, true, true))
78f2c7db6   Peter Zijlstra   sched: SCHED_FIFO...
817
  			return;
dd6702241   Thomas Gleixner   posix-cpu-timers:...
818

8991afe26   Thomas Gleixner   posix-cpu-timers:...
819
820
  		/* At the soft limit, send a SIGXCPU every second */
  		if (check_rlimit(rttime, soft, SIGXCPU, true, false)) {
dd6702241   Thomas Gleixner   posix-cpu-timers:...
821
822
  			soft += USEC_PER_SEC;
  			tsk->signal->rlim[RLIMIT_RTTIME].rlim_cur = soft;
78f2c7db6   Peter Zijlstra   sched: SCHED_FIFO...
823
824
  		}
  	}
c02b078e6   Thomas Gleixner   posix-cpu-timers:...
825

1cd07c0b9   Thomas Gleixner   posix-cpu-timers:...
826
  	if (expiry_cache_is_inactive(pct))
b78783000   Frederic Weisbecker   posix-cpu-timers:...
827
  		tick_dep_clear_task(tsk, TICK_DEP_BIT_POSIX_TIMER);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
828
  }
1018016c7   Jason Low   sched, timer: Rep...
829
  static inline void stop_process_timers(struct signal_struct *sig)
3fccfd67d   Peter Zijlstra   timers: split pro...
830
  {
244d49e30   Thomas Gleixner   posix-cpu-timers:...
831
  	struct posix_cputimers *pct = &sig->posix_cputimers;
3fccfd67d   Peter Zijlstra   timers: split pro...
832

244d49e30   Thomas Gleixner   posix-cpu-timers:...
833
834
  	/* Turn off the active flag. This is done without locking. */
  	WRITE_ONCE(pct->timers_active, false);
b78783000   Frederic Weisbecker   posix-cpu-timers:...
835
  	tick_dep_clear_signal(sig, TICK_DEP_BIT_POSIX_TIMER);
3fccfd67d   Peter Zijlstra   timers: split pro...
836
  }
42c4ab41a   Stanislaw Gruszka   itimers: Merge IT...
837
  static void check_cpu_itimer(struct task_struct *tsk, struct cpu_itimer *it,
ebd7e7fc4   Frederic Weisbecker   timers/posix-time...
838
  			     u64 *expires, u64 cur_time, int signo)
42c4ab41a   Stanislaw Gruszka   itimers: Merge IT...
839
  {
648616343   Martin Schwidefsky   [S390] cputime: a...
840
  	if (!it->expires)
42c4ab41a   Stanislaw Gruszka   itimers: Merge IT...
841
  		return;
858cf3a8c   Frederic Weisbecker   timers/itimer: Co...
842
843
  	if (cur_time >= it->expires) {
  		if (it->incr)
648616343   Martin Schwidefsky   [S390] cputime: a...
844
  			it->expires += it->incr;
858cf3a8c   Frederic Weisbecker   timers/itimer: Co...
845
  		else
648616343   Martin Schwidefsky   [S390] cputime: a...
846
  			it->expires = 0;
42c4ab41a   Stanislaw Gruszka   itimers: Merge IT...
847

3f0a525eb   Xiao Guangrong   itimers: Add trac...
848
849
  		trace_itimer_expire(signo == SIGPROF ?
  				    ITIMER_PROF : ITIMER_VIRTUAL,
6883f81aa   Eric W. Biederman   pid: Implement PI...
850
  				    task_tgid(tsk), cur_time);
42c4ab41a   Stanislaw Gruszka   itimers: Merge IT...
851
852
  		__group_send_sig_info(signo, SEND_SIG_PRIV, tsk);
  	}
2bbdbdae0   Thomas Gleixner   posix-cpu-timers:...
853
  	if (it->expires && it->expires < *expires)
858cf3a8c   Frederic Weisbecker   timers/itimer: Co...
854
  		*expires = it->expires;
42c4ab41a   Stanislaw Gruszka   itimers: Merge IT...
855
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
856
857
858
859
860
861
862
863
864
  /*
   * Check for any per-thread CPU timers that have fired and move them
   * off the tsk->*_timers list onto the firing list.  Per-thread timers
   * have already been taken off.
   */
  static void check_process_timers(struct task_struct *tsk,
  				 struct list_head *firing)
  {
  	struct signal_struct *const sig = tsk->signal;
1cd07c0b9   Thomas Gleixner   posix-cpu-timers:...
865
866
  	struct posix_cputimers *pct = &sig->posix_cputimers;
  	u64 samples[CPUCLOCK_MAX];
d4bb52743   Jiri Slaby   posix-cpu-timers:...
867
  	unsigned long soft;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
868
869
  
  	/*
244d49e30   Thomas Gleixner   posix-cpu-timers:...
870
  	 * If there are no active process wide timers (POSIX 1.b, itimers,
a2ed4fd68   Thomas Gleixner   posix-cpu-timers:...
871
872
  	 * RLIMIT_CPU) nothing to check. Also skip the process wide timer
  	 * processing when there is already another task handling them.
934715a19   Jason Low   posix_cpu_timer: ...
873
  	 */
a2ed4fd68   Thomas Gleixner   posix-cpu-timers:...
874
  	if (!READ_ONCE(pct->timers_active) || pct->expiry_active)
934715a19   Jason Low   posix_cpu_timer: ...
875
  		return;
a2ed4fd68   Thomas Gleixner   posix-cpu-timers:...
876
  	/*
c8d75aa47   Jason Low   posix_cpu_timer: ...
877
878
879
  	 * Signify that a thread is checking for process timers.
  	 * Write access to this field is protected by the sighand lock.
  	 */
a2ed4fd68   Thomas Gleixner   posix-cpu-timers:...
880
  	pct->expiry_active = true;
c8d75aa47   Jason Low   posix_cpu_timer: ...
881

934715a19   Jason Low   posix_cpu_timer: ...
882
  	/*
a324956fa   Thomas Gleixner   posix-cpu-timers:...
883
884
  	 * Collect the current process totals. Group accounting is active
  	 * so the sample can be taken directly.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
885
  	 */
b7be4ef13   Thomas Gleixner   posix-cpu-timers:...
886
  	proc_sample_cputime_atomic(&sig->cputimer.cputime_atomic, samples);
1cd07c0b9   Thomas Gleixner   posix-cpu-timers:...
887
  	collect_posix_cputimers(pct, samples, firing);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
888
889
890
891
  
  	/*
  	 * Check for the special case process timers.
  	 */
1cd07c0b9   Thomas Gleixner   posix-cpu-timers:...
892
893
  	check_cpu_itimer(tsk, &sig->it[CPUCLOCK_PROF],
  			 &pct->bases[CPUCLOCK_PROF].nextevt,
b7be4ef13   Thomas Gleixner   posix-cpu-timers:...
894
  			 samples[CPUCLOCK_PROF], SIGPROF);
1cd07c0b9   Thomas Gleixner   posix-cpu-timers:...
895
896
897
  	check_cpu_itimer(tsk, &sig->it[CPUCLOCK_VIRT],
  			 &pct->bases[CPUCLOCK_VIRT].nextevt,
  			 samples[CPUCLOCK_VIRT], SIGVTALRM);
b7be4ef13   Thomas Gleixner   posix-cpu-timers:...
898

3cf294962   Krzysztof Opasiak   posix-cpu-timers:...
899
  	soft = task_rlimit(tsk, RLIMIT_CPU);
d4bb52743   Jiri Slaby   posix-cpu-timers:...
900
  	if (soft != RLIM_INFINITY) {
8ea1de90a   Thomas Gleixner   posix-cpu-timers:...
901
  		/* RLIMIT_CPU is in seconds. Samples are nanoseconds */
3cf294962   Krzysztof Opasiak   posix-cpu-timers:...
902
  		unsigned long hard = task_rlimit_max(tsk, RLIMIT_CPU);
8ea1de90a   Thomas Gleixner   posix-cpu-timers:...
903
904
905
  		u64 ptime = samples[CPUCLOCK_PROF];
  		u64 softns = (u64)soft * NSEC_PER_SEC;
  		u64 hardns = (u64)hard * NSEC_PER_SEC;
b7be4ef13   Thomas Gleixner   posix-cpu-timers:...
906

8991afe26   Thomas Gleixner   posix-cpu-timers:...
907
908
909
  		/* At the hard limit, send SIGKILL. No further action. */
  		if (hard != RLIM_INFINITY &&
  		    check_rlimit(ptime, hardns, SIGKILL, false, true))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
910
  			return;
dd6702241   Thomas Gleixner   posix-cpu-timers:...
911

8991afe26   Thomas Gleixner   posix-cpu-timers:...
912
913
  		/* At the soft limit, send a SIGXCPU every second */
  		if (check_rlimit(ptime, softns, SIGXCPU, false, false)) {
dd6702241   Thomas Gleixner   posix-cpu-timers:...
914
915
  			sig->rlim[RLIMIT_CPU].rlim_cur = soft + 1;
  			softns += NSEC_PER_SEC;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
916
  		}
8ea1de90a   Thomas Gleixner   posix-cpu-timers:...
917
918
  
  		/* Update the expiry cache */
1cd07c0b9   Thomas Gleixner   posix-cpu-timers:...
919
920
  		if (softns < pct->bases[CPUCLOCK_PROF].nextevt)
  			pct->bases[CPUCLOCK_PROF].nextevt = softns;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
921
  	}
1cd07c0b9   Thomas Gleixner   posix-cpu-timers:...
922
  	if (expiry_cache_is_inactive(pct))
29f87b793   Stanislaw Gruszka   posix-cpu-timers:...
923
  		stop_process_timers(sig);
c8d75aa47   Jason Low   posix_cpu_timer: ...
924

244d49e30   Thomas Gleixner   posix-cpu-timers:...
925
  	pct->expiry_active = false;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
926
927
928
  }
  
  /*
96fe3b072   Thomas Gleixner   posix-timers: Ren...
929
   * This is called from the signal code (via posixtimer_rearm)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
930
931
   * when the last timer signal was delivered and we have to reload the timer.
   */
f37fb0aa4   Thomas Gleixner   posix-timers: Use...
932
  static void posix_cpu_timer_rearm(struct k_itimer *timer)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
933
  {
da020ce40   Thomas Gleixner   posix-cpu-timers:...
934
  	clockid_t clkid = CPUCLOCK_WHICH(timer->it_clock);
55e8c8eb2   Eric W. Biederman   posix-cpu-timers:...
935
  	struct task_struct *p;
e73d84e33   Frederic Weisbecker   posix-timers: Rem...
936
937
  	struct sighand_struct *sighand;
  	unsigned long flags;
ebd7e7fc4   Frederic Weisbecker   timers/posix-time...
938
  	u64 now;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
939

55e8c8eb2   Eric W. Biederman   posix-cpu-timers:...
940
941
942
943
  	rcu_read_lock();
  	p = cpu_timer_task_rcu(timer);
  	if (!p)
  		goto out;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
944
945
946
947
  
  	/*
  	 * Fetch the current sample and update the timer's expiry time.
  	 */
60f2ceaa8   Eric W. Biederman   posix-cpu-timers:...
948
  	if (CPUCLOCK_PERTHREAD(timer->it_clock))
8c2d74f03   Thomas Gleixner   posix-cpu-timers:...
949
  		now = cpu_clock_sample(clkid, p);
60f2ceaa8   Eric W. Biederman   posix-cpu-timers:...
950
  	else
8c2d74f03   Thomas Gleixner   posix-cpu-timers:...
951
  		now = cpu_clock_sample_group(clkid, p, true);
60f2ceaa8   Eric W. Biederman   posix-cpu-timers:...
952
953
954
955
956
957
  
  	bump_cpu_timer(timer, now);
  
  	/* Protect timer list r/w in arm_timer() */
  	sighand = lock_task_sighand(p, &flags);
  	if (unlikely(sighand == NULL))
55e8c8eb2   Eric W. Biederman   posix-cpu-timers:...
958
  		goto out;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
959
960
961
962
  
  	/*
  	 * Now re-arm for the new expiry time.
  	 */
beb41d9cb   Eric W. Biederman   posix-cpu-timers:...
963
  	arm_timer(timer, p);
e73d84e33   Frederic Weisbecker   posix-timers: Rem...
964
  	unlock_task_sighand(p, &flags);
55e8c8eb2   Eric W. Biederman   posix-cpu-timers:...
965
966
  out:
  	rcu_read_unlock();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
967
  }
f06febc96   Frank Mayhar   timers: fix itime...
968
  /**
87dc64480   Thomas Gleixner   posix-cpu-timers:...
969
   * task_cputimers_expired - Check whether posix CPU timers are expired
f06febc96   Frank Mayhar   timers: fix itime...
970
   *
001f79714   Thomas Gleixner   posix-cpu-timers:...
971
   * @samples:	Array of current samples for the CPUCLOCK clocks
87dc64480   Thomas Gleixner   posix-cpu-timers:...
972
   * @pct:	Pointer to a posix_cputimers container
f06febc96   Frank Mayhar   timers: fix itime...
973
   *
87dc64480   Thomas Gleixner   posix-cpu-timers:...
974
975
   * Returns true if any member of @samples is greater than the corresponding
   * member of @pct->bases[CLK].nextevt. False otherwise
f06febc96   Frank Mayhar   timers: fix itime...
976
   */
87dc64480   Thomas Gleixner   posix-cpu-timers:...
977
  static inline bool
7f2cbcbca   Yi Wang   posix-cpu-timers:...
978
  task_cputimers_expired(const u64 *samples, struct posix_cputimers *pct)
f06febc96   Frank Mayhar   timers: fix itime...
979
  {
001f79714   Thomas Gleixner   posix-cpu-timers:...
980
981
982
  	int i;
  
  	for (i = 0; i < CPUCLOCK_MAX; i++) {
7f2cbcbca   Yi Wang   posix-cpu-timers:...
983
  		if (samples[i] >= pct->bases[i].nextevt)
001f79714   Thomas Gleixner   posix-cpu-timers:...
984
985
986
  			return true;
  	}
  	return false;
f06febc96   Frank Mayhar   timers: fix itime...
987
988
989
990
991
992
  }
  
  /**
   * fastpath_timer_check - POSIX CPU timers fast path.
   *
   * @tsk:	The task (thread) being checked.
f06febc96   Frank Mayhar   timers: fix itime...
993
   *
bb34d92f6   Frank Mayhar   timers: fix itime...
994
995
996
997
   * Check the task and thread group timers.  If both are zero (there are no
   * timers set) return false.  Otherwise snapshot the task and thread group
   * timers and compare them with the corresponding expiration times.  Return
   * true if a timer has expired, else return false.
f06febc96   Frank Mayhar   timers: fix itime...
998
   */
001f79714   Thomas Gleixner   posix-cpu-timers:...
999
  static inline bool fastpath_timer_check(struct task_struct *tsk)
f06febc96   Frank Mayhar   timers: fix itime...
1000
  {
244d49e30   Thomas Gleixner   posix-cpu-timers:...
1001
  	struct posix_cputimers *pct = &tsk->posix_cputimers;
ad133ba3d   Oleg Nesterov   sched, signals: f...
1002
  	struct signal_struct *sig;
bb34d92f6   Frank Mayhar   timers: fix itime...
1003

244d49e30   Thomas Gleixner   posix-cpu-timers:...
1004
  	if (!expiry_cache_is_inactive(pct)) {
001f79714   Thomas Gleixner   posix-cpu-timers:...
1005
  		u64 samples[CPUCLOCK_MAX];
bb34d92f6   Frank Mayhar   timers: fix itime...
1006

001f79714   Thomas Gleixner   posix-cpu-timers:...
1007
  		task_sample_cputime(tsk, samples);
244d49e30   Thomas Gleixner   posix-cpu-timers:...
1008
  		if (task_cputimers_expired(samples, pct))
001f79714   Thomas Gleixner   posix-cpu-timers:...
1009
  			return true;
bb34d92f6   Frank Mayhar   timers: fix itime...
1010
  	}
ad133ba3d   Oleg Nesterov   sched, signals: f...
1011
1012
  
  	sig = tsk->signal;
244d49e30   Thomas Gleixner   posix-cpu-timers:...
1013
  	pct = &sig->posix_cputimers;
c8d75aa47   Jason Low   posix_cpu_timer: ...
1014
  	/*
244d49e30   Thomas Gleixner   posix-cpu-timers:...
1015
1016
1017
1018
1019
1020
  	 * Check if thread group timers expired when timers are active and
  	 * no other thread in the group is already handling expiry for
  	 * thread group cputimers. These fields are read without the
  	 * sighand lock. However, this is fine because this is meant to be
  	 * a fastpath heuristic to determine whether we should try to
  	 * acquire the sighand lock to handle timer expiry.
c8d75aa47   Jason Low   posix_cpu_timer: ...
1021
  	 *
244d49e30   Thomas Gleixner   posix-cpu-timers:...
1022
1023
1024
1025
1026
1027
  	 * In the worst case scenario, if concurrently timers_active is set
  	 * or expiry_active is cleared, but the current thread doesn't see
  	 * the change yet, the timer checks are delayed until the next
  	 * thread in the group gets a scheduler interrupt to handle the
  	 * timer. This isn't an issue in practice because these types of
  	 * delays with signals actually getting sent are expected.
c8d75aa47   Jason Low   posix_cpu_timer: ...
1028
  	 */
244d49e30   Thomas Gleixner   posix-cpu-timers:...
1029
  	if (READ_ONCE(pct->timers_active) && !READ_ONCE(pct->expiry_active)) {
001f79714   Thomas Gleixner   posix-cpu-timers:...
1030
  		u64 samples[CPUCLOCK_MAX];
bb34d92f6   Frank Mayhar   timers: fix itime...
1031

001f79714   Thomas Gleixner   posix-cpu-timers:...
1032
1033
  		proc_sample_cputime_atomic(&sig->cputimer.cputime_atomic,
  					   samples);
8d1f431cb   Oleg Nesterov   sched: Fix the ra...
1034

244d49e30   Thomas Gleixner   posix-cpu-timers:...
1035
  		if (task_cputimers_expired(samples, pct))
001f79714   Thomas Gleixner   posix-cpu-timers:...
1036
  			return true;
bb34d92f6   Frank Mayhar   timers: fix itime...
1037
  	}
37bebc70d   Oleg Nesterov   posix timers: fix...
1038

34be39305   Juri Lelli   sched/deadline: I...
1039
  	if (dl_task(tsk) && tsk->dl.dl_overrun)
001f79714   Thomas Gleixner   posix-cpu-timers:...
1040
  		return true;
34be39305   Juri Lelli   sched/deadline: I...
1041

001f79714   Thomas Gleixner   posix-cpu-timers:...
1042
  	return false;
f06febc96   Frank Mayhar   timers: fix itime...
1043
  }
1fb497dd0   Thomas Gleixner   posix-cpu-timers:...
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
  static void handle_posix_cpu_timers(struct task_struct *tsk);
  
  #ifdef CONFIG_POSIX_CPU_TIMERS_TASK_WORK
  static void posix_cpu_timers_work(struct callback_head *work)
  {
  	handle_posix_cpu_timers(current);
  }
  
  /*
   * Initialize posix CPU timers task work in init task. Out of line to
   * keep the callback static and to avoid header recursion hell.
   */
  void __init posix_cputimers_init_work(void)
  {
  	init_task_work(&current->posix_cputimers_work.work,
  		       posix_cpu_timers_work);
  }
  
  /*
   * Note: All operations on tsk->posix_cputimer_work.scheduled happen either
   * in hard interrupt context or in task context with interrupts
   * disabled. Aside of that the writer/reader interaction is always in the
   * context of the current task, which means they are strict per CPU.
   */
  static inline bool posix_cpu_timers_work_scheduled(struct task_struct *tsk)
  {
  	return tsk->posix_cputimers_work.scheduled;
  }
  
  static inline void __run_posix_cpu_timers(struct task_struct *tsk)
  {
  	if (WARN_ON_ONCE(tsk->posix_cputimers_work.scheduled))
  		return;
  
  	/* Schedule task work to actually expire the timers */
  	tsk->posix_cputimers_work.scheduled = true;
  	task_work_add(tsk, &tsk->posix_cputimers_work.work, TWA_RESUME);
  }
  
  static inline bool posix_cpu_timers_enable_work(struct task_struct *tsk,
  						unsigned long start)
  {
  	bool ret = true;
  
  	/*
  	 * On !RT kernels interrupts are disabled while collecting expired
  	 * timers, so no tick can happen and the fast path check can be
  	 * reenabled without further checks.
  	 */
  	if (!IS_ENABLED(CONFIG_PREEMPT_RT)) {
  		tsk->posix_cputimers_work.scheduled = false;
  		return true;
  	}
  
  	/*
  	 * On RT enabled kernels ticks can happen while the expired timers
  	 * are collected under sighand lock. But any tick which observes
  	 * the CPUTIMERS_WORK_SCHEDULED bit set, does not run the fastpath
  	 * checks. So reenabling the tick work has do be done carefully:
  	 *
  	 * Disable interrupts and run the fast path check if jiffies have
  	 * advanced since the collecting of expired timers started. If
  	 * jiffies have not advanced or the fast path check did not find
  	 * newly expired timers, reenable the fast path check in the timer
  	 * interrupt. If there are newly expired timers, return false and
  	 * let the collection loop repeat.
  	 */
  	local_irq_disable();
  	if (start != jiffies && fastpath_timer_check(tsk))
  		ret = false;
  	else
  		tsk->posix_cputimers_work.scheduled = false;
  	local_irq_enable();
  
  	return ret;
  }
  #else /* CONFIG_POSIX_CPU_TIMERS_TASK_WORK */
  static inline void __run_posix_cpu_timers(struct task_struct *tsk)
  {
  	lockdep_posixtimer_enter();
  	handle_posix_cpu_timers(tsk);
  	lockdep_posixtimer_exit();
  }
  
  static inline bool posix_cpu_timers_work_scheduled(struct task_struct *tsk)
  {
  	return false;
  }
  
  static inline bool posix_cpu_timers_enable_work(struct task_struct *tsk,
  						unsigned long start)
  {
  	return true;
  }
  #endif /* CONFIG_POSIX_CPU_TIMERS_TASK_WORK */
  
  static void handle_posix_cpu_timers(struct task_struct *tsk)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1141
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1142
  	struct k_itimer *timer, *next;
1fb497dd0   Thomas Gleixner   posix-cpu-timers:...
1143
  	unsigned long flags, start;
dce3e8fd0   Thomas Gleixner   posix-cpu-timers:...
1144
  	LIST_HEAD(firing);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1145

820903c78   Thomas Gleixner   posix-cpu-timers:...
1146
  	if (!lock_task_sighand(tsk, &flags))
f06febc96   Frank Mayhar   timers: fix itime...
1147
  		return;
5ce73a4a5   Ingo Molnar   timers: fix itime...
1148

1fb497dd0   Thomas Gleixner   posix-cpu-timers:...
1149
1150
1151
1152
1153
1154
1155
1156
  	do {
  		/*
  		 * On RT locking sighand lock does not disable interrupts,
  		 * so this needs to be careful vs. ticks. Store the current
  		 * jiffies value.
  		 */
  		start = READ_ONCE(jiffies);
  		barrier();
934715a19   Jason Low   posix_cpu_timer: ...
1157

1fb497dd0   Thomas Gleixner   posix-cpu-timers:...
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
  		/*
  		 * Here we take off tsk->signal->cpu_timers[N] and
  		 * tsk->cpu_timers[N] all the timers that are firing, and
  		 * put them on the firing list.
  		 */
  		check_thread_timers(tsk, &firing);
  
  		check_process_timers(tsk, &firing);
  
  		/*
  		 * The above timer checks have updated the exipry cache and
  		 * because nothing can have queued or modified timers after
  		 * sighand lock was taken above it is guaranteed to be
  		 * consistent. So the next timer interrupt fastpath check
  		 * will find valid data.
  		 *
  		 * If timer expiry runs in the timer interrupt context then
  		 * the loop is not relevant as timers will be directly
  		 * expired in interrupt context. The stub function below
  		 * returns always true which allows the compiler to
  		 * optimize the loop out.
  		 *
  		 * If timer expiry is deferred to task work context then
  		 * the following rules apply:
  		 *
  		 * - On !RT kernels no tick can have happened on this CPU
  		 *   after sighand lock was acquired because interrupts are
  		 *   disabled. So reenabling task work before dropping
  		 *   sighand lock and reenabling interrupts is race free.
  		 *
  		 * - On RT kernels ticks might have happened but the tick
  		 *   work ignored posix CPU timer handling because the
  		 *   CPUTIMERS_WORK_SCHEDULED bit is set. Reenabling work
  		 *   must be done very carefully including a check whether
  		 *   ticks have happened since the start of the timer
  		 *   expiry checks. posix_cpu_timers_enable_work() takes
  		 *   care of that and eventually lets the expiry checks
  		 *   run again.
  		 */
  	} while (!posix_cpu_timers_enable_work(tsk, start));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1198

bb34d92f6   Frank Mayhar   timers: fix itime...
1199
  	/*
1fb497dd0   Thomas Gleixner   posix-cpu-timers:...
1200
  	 * We must release sighand lock before taking any timer's lock.
bb34d92f6   Frank Mayhar   timers: fix itime...
1201
1202
1203
1204
1205
1206
  	 * There is a potential race with timer deletion here, as the
  	 * siglock now protects our private firing list.  We have set
  	 * the firing flag in each timer, so that a deletion attempt
  	 * that gets the timer lock before we do will give it up and
  	 * spin until we've taken care of that timer below.
  	 */
0bdd2ed41   Oleg Nesterov   sched: run_posix_...
1207
  	unlock_task_sighand(tsk, &flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1208
1209
1210
  
  	/*
  	 * Now that all the timers on our list have the firing flag,
25985edce   Lucas De Marchi   Fix common misspe...
1211
  	 * no one will touch their list entries but us.  We'll take
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1212
1213
1214
  	 * each timer's lock before clearing its firing flag, so no
  	 * timer call will interfere.
  	 */
60bda037f   Thomas Gleixner   posix-cpu-timers:...
1215
  	list_for_each_entry_safe(timer, next, &firing, it.cpu.elist) {
6e85c5ba7   H Hartley Sweeten   kernel/posix-cpu-...
1216
  		int cpu_firing;
1fb497dd0   Thomas Gleixner   posix-cpu-timers:...
1217
1218
1219
1220
1221
1222
1223
  		/*
  		 * spin_lock() is sufficient here even independent of the
  		 * expiry context. If expiry happens in hard interrupt
  		 * context it's obvious. For task work context it's safe
  		 * because all other operations on timer::it_lock happen in
  		 * task context (syscall or exit).
  		 */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1224
  		spin_lock(&timer->it_lock);
60bda037f   Thomas Gleixner   posix-cpu-timers:...
1225
  		list_del_init(&timer->it.cpu.elist);
6e85c5ba7   H Hartley Sweeten   kernel/posix-cpu-...
1226
  		cpu_firing = timer->it.cpu.firing;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1227
1228
1229
1230
1231
1232
  		timer->it.cpu.firing = 0;
  		/*
  		 * The firing flag is -1 if we collided with a reset
  		 * of the timer, which already reported this
  		 * almost-firing as an overrun.  So don't generate an event.
  		 */
6e85c5ba7   H Hartley Sweeten   kernel/posix-cpu-...
1233
  		if (likely(cpu_firing >= 0))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1234
  			cpu_timer_fire(timer);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1235
1236
  		spin_unlock(&timer->it_lock);
  	}
820903c78   Thomas Gleixner   posix-cpu-timers:...
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
  }
  
  /*
   * This is called from the timer interrupt handler.  The irq handler has
   * already updated our counts.  We need to check if any timers fire now.
   * Interrupts are disabled.
   */
  void run_posix_cpu_timers(void)
  {
  	struct task_struct *tsk = current;
  
  	lockdep_assert_irqs_disabled();
  
  	/*
1fb497dd0   Thomas Gleixner   posix-cpu-timers:...
1251
1252
1253
1254
1255
1256
1257
  	 * If the actual expiry is deferred to task work context and the
  	 * work is already scheduled there is no point to do anything here.
  	 */
  	if (posix_cpu_timers_work_scheduled(tsk))
  		return;
  
  	/*
820903c78   Thomas Gleixner   posix-cpu-timers:...
1258
1259
1260
1261
1262
  	 * The fast path checks that there are no expired thread or thread
  	 * group timers.  If that's so, just return.
  	 */
  	if (!fastpath_timer_check(tsk))
  		return;
820903c78   Thomas Gleixner   posix-cpu-timers:...
1263
  	__run_posix_cpu_timers(tsk);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1264
1265
1266
  }
  
  /*
f55db6090   Stanislaw Gruszka   cpu-timers: Simpl...
1267
   * Set one of the process-wide special case CPU timers or RLIMIT_CPU.
f06febc96   Frank Mayhar   timers: fix itime...
1268
   * The tsk->sighand->siglock must be held by the caller.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1269
   */
1b0dd96d0   Thomas Gleixner   posix-cpu-timers:...
1270
  void set_process_cpu_timer(struct task_struct *tsk, unsigned int clkid,
858cf3a8c   Frederic Weisbecker   timers/itimer: Co...
1271
  			   u64 *newval, u64 *oldval)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1272
  {
87dc64480   Thomas Gleixner   posix-cpu-timers:...
1273
  	u64 now, *nextevt;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1274

1b0dd96d0   Thomas Gleixner   posix-cpu-timers:...
1275
  	if (WARN_ON_ONCE(clkid >= CPUCLOCK_SCHED))
692117c1f   Thomas Gleixner   posix-cpu-timers:...
1276
  		return;
87dc64480   Thomas Gleixner   posix-cpu-timers:...
1277
  	nextevt = &tsk->signal->posix_cputimers.bases[clkid].nextevt;
1b0dd96d0   Thomas Gleixner   posix-cpu-timers:...
1278
  	now = cpu_clock_sample_group(clkid, tsk, true);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1279

5405d0051   Thomas Gleixner   posix-cpu-timers:...
1280
  	if (oldval) {
f55db6090   Stanislaw Gruszka   cpu-timers: Simpl...
1281
1282
1283
1284
1285
  		/*
  		 * We are setting itimer. The *oldval is absolute and we update
  		 * it to be relative, *newval argument is relative and we update
  		 * it to be absolute.
  		 */
648616343   Martin Schwidefsky   [S390] cputime: a...
1286
  		if (*oldval) {
858cf3a8c   Frederic Weisbecker   timers/itimer: Co...
1287
  			if (*oldval <= now) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1288
  				/* Just about to fire. */
858cf3a8c   Frederic Weisbecker   timers/itimer: Co...
1289
  				*oldval = TICK_NSEC;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1290
  			} else {
858cf3a8c   Frederic Weisbecker   timers/itimer: Co...
1291
  				*oldval -= now;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1292
1293
  			}
  		}
648616343   Martin Schwidefsky   [S390] cputime: a...
1294
  		if (!*newval)
b78783000   Frederic Weisbecker   posix-cpu-timers:...
1295
  			return;
858cf3a8c   Frederic Weisbecker   timers/itimer: Co...
1296
  		*newval += now;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1297
1298
1299
  	}
  
  	/*
1b0dd96d0   Thomas Gleixner   posix-cpu-timers:...
1300
1301
  	 * Update expiration cache if this is the earliest timer. CPUCLOCK_PROF
  	 * expiry cache is also used by RLIMIT_CPU!.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1302
  	 */
2bbdbdae0   Thomas Gleixner   posix-cpu-timers:...
1303
  	if (*newval < *nextevt)
87dc64480   Thomas Gleixner   posix-cpu-timers:...
1304
  		*nextevt = *newval;
b78783000   Frederic Weisbecker   posix-cpu-timers:...
1305
1306
  
  	tick_dep_set_signal(tsk->signal, TICK_DEP_BIT_POSIX_TIMER);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1307
  }
e4b765551   Toyo Abe   [PATCH] posix-tim...
1308
  static int do_cpu_nanosleep(const clockid_t which_clock, int flags,
343d8fc20   Thomas Gleixner   posix-cpu-timers:...
1309
  			    const struct timespec64 *rqtp)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1310
  {
86a9c446c   Al Viro   posix-cpu-timers:...
1311
  	struct itimerspec64 it;
343d8fc20   Thomas Gleixner   posix-cpu-timers:...
1312
1313
  	struct k_itimer timer;
  	u64 expires;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1314
1315
1316
  	int error;
  
  	/*
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1317
1318
1319
1320
1321
1322
1323
1324
  	 * Set up a temporary timer and then wait for it to go off.
  	 */
  	memset(&timer, 0, sizeof timer);
  	spin_lock_init(&timer.it_lock);
  	timer.it_clock = which_clock;
  	timer.it_overrun = -1;
  	error = posix_cpu_timer_create(&timer);
  	timer.it_process = current;
60bda037f   Thomas Gleixner   posix-cpu-timers:...
1325

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1326
  	if (!error) {
5f252b325   Deepa Dinamani   time: Change k_cl...
1327
  		static struct itimerspec64 zero_it;
edbeda463   Al Viro   time/posix-timers...
1328
  		struct restart_block *restart;
e4b765551   Toyo Abe   [PATCH] posix-tim...
1329

edbeda463   Al Viro   time/posix-timers...
1330
  		memset(&it, 0, sizeof(it));
86a9c446c   Al Viro   posix-cpu-timers:...
1331
  		it.it_value = *rqtp;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1332
1333
  
  		spin_lock_irq(&timer.it_lock);
86a9c446c   Al Viro   posix-cpu-timers:...
1334
  		error = posix_cpu_timer_set(&timer, flags, &it, NULL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1335
1336
1337
1338
1339
1340
  		if (error) {
  			spin_unlock_irq(&timer.it_lock);
  			return error;
  		}
  
  		while (!signal_pending(current)) {
60bda037f   Thomas Gleixner   posix-cpu-timers:...
1341
  			if (!cpu_timer_getexpires(&timer.it.cpu)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1342
  				/*
e6c42c295   Stanislaw Gruszka   posix-cpu-timers:...
1343
1344
  				 * Our timer fired and was reset, below
  				 * deletion can not fail.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1345
  				 */
e6c42c295   Stanislaw Gruszka   posix-cpu-timers:...
1346
  				posix_cpu_timer_del(&timer);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
  				spin_unlock_irq(&timer.it_lock);
  				return 0;
  			}
  
  			/*
  			 * Block until cpu_timer_fire (or a signal) wakes us.
  			 */
  			__set_current_state(TASK_INTERRUPTIBLE);
  			spin_unlock_irq(&timer.it_lock);
  			schedule();
  			spin_lock_irq(&timer.it_lock);
  		}
  
  		/*
  		 * We were interrupted by a signal.
  		 */
60bda037f   Thomas Gleixner   posix-cpu-timers:...
1363
  		expires = cpu_timer_getexpires(&timer.it.cpu);
86a9c446c   Al Viro   posix-cpu-timers:...
1364
  		error = posix_cpu_timer_set(&timer, 0, &zero_it, &it);
e6c42c295   Stanislaw Gruszka   posix-cpu-timers:...
1365
1366
1367
1368
1369
1370
  		if (!error) {
  			/*
  			 * Timer is now unarmed, deletion can not fail.
  			 */
  			posix_cpu_timer_del(&timer);
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1371
  		spin_unlock_irq(&timer.it_lock);
e6c42c295   Stanislaw Gruszka   posix-cpu-timers:...
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
  		while (error == TIMER_RETRY) {
  			/*
  			 * We need to handle case when timer was or is in the
  			 * middle of firing. In other cases we already freed
  			 * resources.
  			 */
  			spin_lock_irq(&timer.it_lock);
  			error = posix_cpu_timer_del(&timer);
  			spin_unlock_irq(&timer.it_lock);
  		}
86a9c446c   Al Viro   posix-cpu-timers:...
1382
  		if ((it.it_value.tv_sec | it.it_value.tv_nsec) == 0) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1383
1384
1385
1386
1387
  			/*
  			 * It actually did fire already.
  			 */
  			return 0;
  		}
e4b765551   Toyo Abe   [PATCH] posix-tim...
1388
  		error = -ERESTART_RESTARTBLOCK;
86a9c446c   Al Viro   posix-cpu-timers:...
1389
1390
1391
  		/*
  		 * Report back to the user the time still remaining.
  		 */
edbeda463   Al Viro   time/posix-timers...
1392
  		restart = &current->restart_block;
343d8fc20   Thomas Gleixner   posix-cpu-timers:...
1393
  		restart->nanosleep.expires = expires;
c0edd7c9a   Deepa Dinamani   nanosleep: Use ge...
1394
1395
  		if (restart->nanosleep.type != TT_NONE)
  			error = nanosleep_copyout(restart, &it.it_value);
e4b765551   Toyo Abe   [PATCH] posix-tim...
1396
1397
1398
1399
  	}
  
  	return error;
  }
bc2c8ea48   Thomas Gleixner   posix-timers: Mak...
1400
1401
1402
  static long posix_cpu_nsleep_restart(struct restart_block *restart_block);
  
  static int posix_cpu_nsleep(const clockid_t which_clock, int flags,
938e7cf2d   Thomas Gleixner   posix-timers: Mak...
1403
  			    const struct timespec64 *rqtp)
e4b765551   Toyo Abe   [PATCH] posix-tim...
1404
  {
f56141e3e   Andy Lutomirski   all arches, signa...
1405
  	struct restart_block *restart_block = &current->restart_block;
e4b765551   Toyo Abe   [PATCH] posix-tim...
1406
1407
1408
1409
1410
1411
1412
  	int error;
  
  	/*
  	 * Diagnose required errors first.
  	 */
  	if (CPUCLOCK_PERTHREAD(which_clock) &&
  	    (CPUCLOCK_PID(which_clock) == 0 ||
01a219748   Eric W. Biederman   posix-timers: Cor...
1413
  	     CPUCLOCK_PID(which_clock) == task_pid_vnr(current)))
e4b765551   Toyo Abe   [PATCH] posix-tim...
1414
  		return -EINVAL;
86a9c446c   Al Viro   posix-cpu-timers:...
1415
  	error = do_cpu_nanosleep(which_clock, flags, rqtp);
e4b765551   Toyo Abe   [PATCH] posix-tim...
1416
1417
  
  	if (error == -ERESTART_RESTARTBLOCK) {
3751f9f29   Thomas Gleixner   posix-timers: Cle...
1418
  		if (flags & TIMER_ABSTIME)
e4b765551   Toyo Abe   [PATCH] posix-tim...
1419
  			return -ERESTARTNOHAND;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1420

1711ef386   Toyo Abe   [PATCH] posix-tim...
1421
  		restart_block->fn = posix_cpu_nsleep_restart;
ab8177bc5   Thomas Gleixner   hrtimers: Avoid t...
1422
  		restart_block->nanosleep.clockid = which_clock;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1423
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1424
1425
  	return error;
  }
bc2c8ea48   Thomas Gleixner   posix-timers: Mak...
1426
  static long posix_cpu_nsleep_restart(struct restart_block *restart_block)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1427
  {
ab8177bc5   Thomas Gleixner   hrtimers: Avoid t...
1428
  	clockid_t which_clock = restart_block->nanosleep.clockid;
ad1963846   Deepa Dinamani   time: Change k_cl...
1429
  	struct timespec64 t;
97735f25d   Thomas Gleixner   [PATCH] hrtimer: ...
1430

ad1963846   Deepa Dinamani   time: Change k_cl...
1431
  	t = ns_to_timespec64(restart_block->nanosleep.expires);
97735f25d   Thomas Gleixner   [PATCH] hrtimer: ...
1432

86a9c446c   Al Viro   posix-cpu-timers:...
1433
  	return do_cpu_nanosleep(which_clock, TIMER_ABSTIME, &t);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1434
  }
29f1b2b0f   Nick Desaulniers   posix-timers: Pre...
1435
1436
  #define PROCESS_CLOCK	make_process_cpuclock(0, CPUCLOCK_SCHED)
  #define THREAD_CLOCK	make_thread_cpuclock(0, CPUCLOCK_SCHED)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1437

a924b04dd   Thomas Gleixner   [PATCH] hrtimer: ...
1438
  static int process_cpu_clock_getres(const clockid_t which_clock,
d2e3e0ca5   Deepa Dinamani   time: Change k_cl...
1439
  				    struct timespec64 *tp)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1440
1441
1442
  {
  	return posix_cpu_clock_getres(PROCESS_CLOCK, tp);
  }
a924b04dd   Thomas Gleixner   [PATCH] hrtimer: ...
1443
  static int process_cpu_clock_get(const clockid_t which_clock,
3c9c12f4b   Deepa Dinamani   time: Change k_cl...
1444
  				 struct timespec64 *tp)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1445
1446
1447
1448
1449
1450
1451
1452
  {
  	return posix_cpu_clock_get(PROCESS_CLOCK, tp);
  }
  static int process_cpu_timer_create(struct k_itimer *timer)
  {
  	timer->it_clock = PROCESS_CLOCK;
  	return posix_cpu_timer_create(timer);
  }
a924b04dd   Thomas Gleixner   [PATCH] hrtimer: ...
1453
  static int process_cpu_nsleep(const clockid_t which_clock, int flags,
938e7cf2d   Thomas Gleixner   posix-timers: Mak...
1454
  			      const struct timespec64 *rqtp)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1455
  {
99e6c0e6e   Al Viro   posix-timers: Sto...
1456
  	return posix_cpu_nsleep(PROCESS_CLOCK, flags, rqtp);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1457
  }
a924b04dd   Thomas Gleixner   [PATCH] hrtimer: ...
1458
  static int thread_cpu_clock_getres(const clockid_t which_clock,
d2e3e0ca5   Deepa Dinamani   time: Change k_cl...
1459
  				   struct timespec64 *tp)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1460
1461
1462
  {
  	return posix_cpu_clock_getres(THREAD_CLOCK, tp);
  }
a924b04dd   Thomas Gleixner   [PATCH] hrtimer: ...
1463
  static int thread_cpu_clock_get(const clockid_t which_clock,
3c9c12f4b   Deepa Dinamani   time: Change k_cl...
1464
  				struct timespec64 *tp)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1465
1466
1467
1468
1469
1470
1471
1472
  {
  	return posix_cpu_clock_get(THREAD_CLOCK, tp);
  }
  static int thread_cpu_timer_create(struct k_itimer *timer)
  {
  	timer->it_clock = THREAD_CLOCK;
  	return posix_cpu_timer_create(timer);
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1473

d3ba5a9a3   Christoph Hellwig   posix-timers: Mak...
1474
  const struct k_clock clock_posix_cpu = {
819a95fe3   Andrei Vagin   posix-clocks: Ren...
1475
1476
1477
1478
1479
1480
1481
1482
1483
  	.clock_getres		= posix_cpu_clock_getres,
  	.clock_set		= posix_cpu_clock_set,
  	.clock_get_timespec	= posix_cpu_clock_get,
  	.timer_create		= posix_cpu_timer_create,
  	.nsleep			= posix_cpu_nsleep,
  	.timer_set		= posix_cpu_timer_set,
  	.timer_del		= posix_cpu_timer_del,
  	.timer_get		= posix_cpu_timer_get,
  	.timer_rearm		= posix_cpu_timer_rearm,
1976945ee   Thomas Gleixner   posix-timers: Int...
1484
  };
d3ba5a9a3   Christoph Hellwig   posix-timers: Mak...
1485
  const struct k_clock clock_process = {
819a95fe3   Andrei Vagin   posix-clocks: Ren...
1486
1487
1488
1489
  	.clock_getres		= process_cpu_clock_getres,
  	.clock_get_timespec	= process_cpu_clock_get,
  	.timer_create		= process_cpu_timer_create,
  	.nsleep			= process_cpu_nsleep,
d3ba5a9a3   Christoph Hellwig   posix-timers: Mak...
1490
  };
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1491

d3ba5a9a3   Christoph Hellwig   posix-timers: Mak...
1492
  const struct k_clock clock_thread = {
819a95fe3   Andrei Vagin   posix-clocks: Ren...
1493
1494
1495
  	.clock_getres		= thread_cpu_clock_getres,
  	.clock_get_timespec	= thread_cpu_clock_get,
  	.timer_create		= thread_cpu_timer_create,
d3ba5a9a3   Christoph Hellwig   posix-timers: Mak...
1496
  };