.. | .. |
---|
1 | 1 | // SPDX-License-Identifier: GPL-2.0 |
---|
2 | 2 | /* |
---|
3 | | - * linux/kernel/itimer.c |
---|
4 | | - * |
---|
5 | 3 | * Copyright (C) 1992 Darren Senn |
---|
6 | 4 | */ |
---|
7 | 5 | |
---|
.. | .. |
---|
28 | 26 | * Returns the delta between the expiry time and now, which can be |
---|
29 | 27 | * less than zero or 1usec for an pending expired timer |
---|
30 | 28 | */ |
---|
31 | | -static struct timeval itimer_get_remtime(struct hrtimer *timer) |
---|
| 29 | +static struct timespec64 itimer_get_remtime(struct hrtimer *timer) |
---|
32 | 30 | { |
---|
33 | 31 | ktime_t rem = __hrtimer_get_remaining(timer, true); |
---|
34 | 32 | |
---|
.. | .. |
---|
43 | 41 | } else |
---|
44 | 42 | rem = 0; |
---|
45 | 43 | |
---|
46 | | - return ktime_to_timeval(rem); |
---|
| 44 | + return ktime_to_timespec64(rem); |
---|
47 | 45 | } |
---|
48 | 46 | |
---|
49 | 47 | static void get_cpu_itimer(struct task_struct *tsk, unsigned int clock_id, |
---|
50 | | - struct itimerval *const value) |
---|
| 48 | + struct itimerspec64 *const value) |
---|
51 | 49 | { |
---|
52 | 50 | u64 val, interval; |
---|
53 | 51 | struct cpu_itimer *it = &tsk->signal->it[clock_id]; |
---|
.. | .. |
---|
57 | 55 | val = it->expires; |
---|
58 | 56 | interval = it->incr; |
---|
59 | 57 | if (val) { |
---|
60 | | - struct task_cputime cputime; |
---|
61 | | - u64 t; |
---|
| 58 | + u64 t, samples[CPUCLOCK_MAX]; |
---|
62 | 59 | |
---|
63 | | - thread_group_cputimer(tsk, &cputime); |
---|
64 | | - if (clock_id == CPUCLOCK_PROF) |
---|
65 | | - t = cputime.utime + cputime.stime; |
---|
66 | | - else |
---|
67 | | - /* CPUCLOCK_VIRT */ |
---|
68 | | - t = cputime.utime; |
---|
| 60 | + thread_group_sample_cputime(tsk, samples); |
---|
| 61 | + t = samples[clock_id]; |
---|
69 | 62 | |
---|
70 | 63 | if (val < t) |
---|
71 | 64 | /* about to fire */ |
---|
.. | .. |
---|
76 | 69 | |
---|
77 | 70 | spin_unlock_irq(&tsk->sighand->siglock); |
---|
78 | 71 | |
---|
79 | | - value->it_value = ns_to_timeval(val); |
---|
80 | | - value->it_interval = ns_to_timeval(interval); |
---|
| 72 | + value->it_value = ns_to_timespec64(val); |
---|
| 73 | + value->it_interval = ns_to_timespec64(interval); |
---|
81 | 74 | } |
---|
82 | 75 | |
---|
83 | | -int do_getitimer(int which, struct itimerval *value) |
---|
| 76 | +static int do_getitimer(int which, struct itimerspec64 *value) |
---|
84 | 77 | { |
---|
85 | 78 | struct task_struct *tsk = current; |
---|
86 | 79 | |
---|
.. | .. |
---|
89 | 82 | spin_lock_irq(&tsk->sighand->siglock); |
---|
90 | 83 | value->it_value = itimer_get_remtime(&tsk->signal->real_timer); |
---|
91 | 84 | value->it_interval = |
---|
92 | | - ktime_to_timeval(tsk->signal->it_real_incr); |
---|
| 85 | + ktime_to_timespec64(tsk->signal->it_real_incr); |
---|
93 | 86 | spin_unlock_irq(&tsk->sighand->siglock); |
---|
94 | 87 | break; |
---|
95 | 88 | case ITIMER_VIRTUAL: |
---|
.. | .. |
---|
104 | 97 | return 0; |
---|
105 | 98 | } |
---|
106 | 99 | |
---|
107 | | -SYSCALL_DEFINE2(getitimer, int, which, struct itimerval __user *, value) |
---|
| 100 | +static int put_itimerval(struct __kernel_old_itimerval __user *o, |
---|
| 101 | + const struct itimerspec64 *i) |
---|
108 | 102 | { |
---|
109 | | - int error = -EFAULT; |
---|
110 | | - struct itimerval get_buffer; |
---|
| 103 | + struct __kernel_old_itimerval v; |
---|
111 | 104 | |
---|
112 | | - if (value) { |
---|
113 | | - error = do_getitimer(which, &get_buffer); |
---|
114 | | - if (!error && |
---|
115 | | - copy_to_user(value, &get_buffer, sizeof(get_buffer))) |
---|
116 | | - error = -EFAULT; |
---|
117 | | - } |
---|
| 105 | + v.it_interval.tv_sec = i->it_interval.tv_sec; |
---|
| 106 | + v.it_interval.tv_usec = i->it_interval.tv_nsec / NSEC_PER_USEC; |
---|
| 107 | + v.it_value.tv_sec = i->it_value.tv_sec; |
---|
| 108 | + v.it_value.tv_usec = i->it_value.tv_nsec / NSEC_PER_USEC; |
---|
| 109 | + return copy_to_user(o, &v, sizeof(struct __kernel_old_itimerval)) ? -EFAULT : 0; |
---|
| 110 | +} |
---|
| 111 | + |
---|
| 112 | + |
---|
| 113 | +SYSCALL_DEFINE2(getitimer, int, which, struct __kernel_old_itimerval __user *, value) |
---|
| 114 | +{ |
---|
| 115 | + struct itimerspec64 get_buffer; |
---|
| 116 | + int error = do_getitimer(which, &get_buffer); |
---|
| 117 | + |
---|
| 118 | + if (!error && put_itimerval(value, &get_buffer)) |
---|
| 119 | + error = -EFAULT; |
---|
118 | 120 | return error; |
---|
119 | 121 | } |
---|
120 | 122 | |
---|
121 | | -#ifdef CONFIG_COMPAT |
---|
122 | | -COMPAT_SYSCALL_DEFINE2(getitimer, int, which, |
---|
123 | | - struct compat_itimerval __user *, it) |
---|
124 | | -{ |
---|
125 | | - struct itimerval kit; |
---|
126 | | - int error = do_getitimer(which, &kit); |
---|
| 123 | +#if defined(CONFIG_COMPAT) || defined(CONFIG_ALPHA) |
---|
| 124 | +struct old_itimerval32 { |
---|
| 125 | + struct old_timeval32 it_interval; |
---|
| 126 | + struct old_timeval32 it_value; |
---|
| 127 | +}; |
---|
127 | 128 | |
---|
128 | | - if (!error && put_compat_itimerval(it, &kit)) |
---|
| 129 | +static int put_old_itimerval32(struct old_itimerval32 __user *o, |
---|
| 130 | + const struct itimerspec64 *i) |
---|
| 131 | +{ |
---|
| 132 | + struct old_itimerval32 v32; |
---|
| 133 | + |
---|
| 134 | + v32.it_interval.tv_sec = i->it_interval.tv_sec; |
---|
| 135 | + v32.it_interval.tv_usec = i->it_interval.tv_nsec / NSEC_PER_USEC; |
---|
| 136 | + v32.it_value.tv_sec = i->it_value.tv_sec; |
---|
| 137 | + v32.it_value.tv_usec = i->it_value.tv_nsec / NSEC_PER_USEC; |
---|
| 138 | + return copy_to_user(o, &v32, sizeof(struct old_itimerval32)) ? -EFAULT : 0; |
---|
| 139 | +} |
---|
| 140 | + |
---|
| 141 | +COMPAT_SYSCALL_DEFINE2(getitimer, int, which, |
---|
| 142 | + struct old_itimerval32 __user *, value) |
---|
| 143 | +{ |
---|
| 144 | + struct itimerspec64 get_buffer; |
---|
| 145 | + int error = do_getitimer(which, &get_buffer); |
---|
| 146 | + |
---|
| 147 | + if (!error && put_old_itimerval32(value, &get_buffer)) |
---|
129 | 148 | error = -EFAULT; |
---|
130 | 149 | return error; |
---|
131 | 150 | } |
---|
132 | 151 | #endif |
---|
133 | | - |
---|
134 | 152 | |
---|
135 | 153 | /* |
---|
136 | 154 | * The timer is automagically restarted, when interval != 0 |
---|
.. | .. |
---|
148 | 166 | } |
---|
149 | 167 | |
---|
150 | 168 | static void set_cpu_itimer(struct task_struct *tsk, unsigned int clock_id, |
---|
151 | | - const struct itimerval *const value, |
---|
152 | | - struct itimerval *const ovalue) |
---|
| 169 | + const struct itimerspec64 *const value, |
---|
| 170 | + struct itimerspec64 *const ovalue) |
---|
153 | 171 | { |
---|
154 | 172 | u64 oval, nval, ointerval, ninterval; |
---|
155 | 173 | struct cpu_itimer *it = &tsk->signal->it[clock_id]; |
---|
156 | 174 | |
---|
157 | | - nval = ktime_to_ns(timeval_to_ktime(value->it_value)); |
---|
158 | | - ninterval = ktime_to_ns(timeval_to_ktime(value->it_interval)); |
---|
| 175 | + nval = timespec64_to_ns(&value->it_value); |
---|
| 176 | + ninterval = timespec64_to_ns(&value->it_interval); |
---|
159 | 177 | |
---|
160 | 178 | spin_lock_irq(&tsk->sighand->siglock); |
---|
161 | 179 | |
---|
.. | .. |
---|
174 | 192 | spin_unlock_irq(&tsk->sighand->siglock); |
---|
175 | 193 | |
---|
176 | 194 | if (ovalue) { |
---|
177 | | - ovalue->it_value = ns_to_timeval(oval); |
---|
178 | | - ovalue->it_interval = ns_to_timeval(ointerval); |
---|
| 195 | + ovalue->it_value = ns_to_timespec64(oval); |
---|
| 196 | + ovalue->it_interval = ns_to_timespec64(ointerval); |
---|
179 | 197 | } |
---|
180 | 198 | } |
---|
181 | 199 | |
---|
.. | .. |
---|
185 | 203 | #define timeval_valid(t) \ |
---|
186 | 204 | (((t)->tv_sec >= 0) && (((unsigned long) (t)->tv_usec) < USEC_PER_SEC)) |
---|
187 | 205 | |
---|
188 | | -int do_setitimer(int which, struct itimerval *value, struct itimerval *ovalue) |
---|
| 206 | +static int do_setitimer(int which, struct itimerspec64 *value, |
---|
| 207 | + struct itimerspec64 *ovalue) |
---|
189 | 208 | { |
---|
190 | 209 | struct task_struct *tsk = current; |
---|
191 | 210 | struct hrtimer *timer; |
---|
192 | 211 | ktime_t expires; |
---|
193 | | - |
---|
194 | | - /* |
---|
195 | | - * Validate the timevals in value. |
---|
196 | | - */ |
---|
197 | | - if (!timeval_valid(&value->it_value) || |
---|
198 | | - !timeval_valid(&value->it_interval)) |
---|
199 | | - return -EINVAL; |
---|
200 | 212 | |
---|
201 | 213 | switch (which) { |
---|
202 | 214 | case ITIMER_REAL: |
---|
.. | .. |
---|
206 | 218 | if (ovalue) { |
---|
207 | 219 | ovalue->it_value = itimer_get_remtime(timer); |
---|
208 | 220 | ovalue->it_interval |
---|
209 | | - = ktime_to_timeval(tsk->signal->it_real_incr); |
---|
| 221 | + = ktime_to_timespec64(tsk->signal->it_real_incr); |
---|
210 | 222 | } |
---|
211 | 223 | /* We are sharing ->siglock with it_real_fn() */ |
---|
212 | 224 | if (hrtimer_try_to_cancel(timer) < 0) { |
---|
213 | 225 | spin_unlock_irq(&tsk->sighand->siglock); |
---|
| 226 | + hrtimer_cancel_wait_running(timer); |
---|
214 | 227 | goto again; |
---|
215 | 228 | } |
---|
216 | | - expires = timeval_to_ktime(value->it_value); |
---|
| 229 | + expires = timespec64_to_ktime(value->it_value); |
---|
217 | 230 | if (expires != 0) { |
---|
218 | 231 | tsk->signal->it_real_incr = |
---|
219 | | - timeval_to_ktime(value->it_interval); |
---|
| 232 | + timespec64_to_ktime(value->it_interval); |
---|
220 | 233 | hrtimer_start(timer, expires, HRTIMER_MODE_REL); |
---|
221 | 234 | } else |
---|
222 | 235 | tsk->signal->it_real_incr = 0; |
---|
.. | .. |
---|
236 | 249 | return 0; |
---|
237 | 250 | } |
---|
238 | 251 | |
---|
| 252 | +#ifdef CONFIG_SECURITY_SELINUX |
---|
| 253 | +void clear_itimer(void) |
---|
| 254 | +{ |
---|
| 255 | + struct itimerspec64 v = {}; |
---|
| 256 | + int i; |
---|
| 257 | + |
---|
| 258 | + for (i = 0; i < 3; i++) |
---|
| 259 | + do_setitimer(i, &v, NULL); |
---|
| 260 | +} |
---|
| 261 | +#endif |
---|
| 262 | + |
---|
239 | 263 | #ifdef __ARCH_WANT_SYS_ALARM |
---|
240 | 264 | |
---|
241 | 265 | /** |
---|
.. | .. |
---|
252 | 276 | */ |
---|
253 | 277 | static unsigned int alarm_setitimer(unsigned int seconds) |
---|
254 | 278 | { |
---|
255 | | - struct itimerval it_new, it_old; |
---|
| 279 | + struct itimerspec64 it_new, it_old; |
---|
256 | 280 | |
---|
257 | 281 | #if BITS_PER_LONG < 64 |
---|
258 | 282 | if (seconds > INT_MAX) |
---|
259 | 283 | seconds = INT_MAX; |
---|
260 | 284 | #endif |
---|
261 | 285 | it_new.it_value.tv_sec = seconds; |
---|
262 | | - it_new.it_value.tv_usec = 0; |
---|
263 | | - it_new.it_interval.tv_sec = it_new.it_interval.tv_usec = 0; |
---|
| 286 | + it_new.it_value.tv_nsec = 0; |
---|
| 287 | + it_new.it_interval.tv_sec = it_new.it_interval.tv_nsec = 0; |
---|
264 | 288 | |
---|
265 | 289 | do_setitimer(ITIMER_REAL, &it_new, &it_old); |
---|
266 | 290 | |
---|
.. | .. |
---|
268 | 292 | * We can't return 0 if we have an alarm pending ... And we'd |
---|
269 | 293 | * better return too much than too little anyway |
---|
270 | 294 | */ |
---|
271 | | - if ((!it_old.it_value.tv_sec && it_old.it_value.tv_usec) || |
---|
272 | | - it_old.it_value.tv_usec >= 500000) |
---|
| 295 | + if ((!it_old.it_value.tv_sec && it_old.it_value.tv_nsec) || |
---|
| 296 | + it_old.it_value.tv_nsec >= (NSEC_PER_SEC / 2)) |
---|
273 | 297 | it_old.it_value.tv_sec++; |
---|
274 | 298 | |
---|
275 | 299 | return it_old.it_value.tv_sec; |
---|
.. | .. |
---|
286 | 310 | |
---|
287 | 311 | #endif |
---|
288 | 312 | |
---|
289 | | -SYSCALL_DEFINE3(setitimer, int, which, struct itimerval __user *, value, |
---|
290 | | - struct itimerval __user *, ovalue) |
---|
| 313 | +static int get_itimerval(struct itimerspec64 *o, const struct __kernel_old_itimerval __user *i) |
---|
291 | 314 | { |
---|
292 | | - struct itimerval set_buffer, get_buffer; |
---|
| 315 | + struct __kernel_old_itimerval v; |
---|
| 316 | + |
---|
| 317 | + if (copy_from_user(&v, i, sizeof(struct __kernel_old_itimerval))) |
---|
| 318 | + return -EFAULT; |
---|
| 319 | + |
---|
| 320 | + /* Validate the timevals in value. */ |
---|
| 321 | + if (!timeval_valid(&v.it_value) || |
---|
| 322 | + !timeval_valid(&v.it_interval)) |
---|
| 323 | + return -EINVAL; |
---|
| 324 | + |
---|
| 325 | + o->it_interval.tv_sec = v.it_interval.tv_sec; |
---|
| 326 | + o->it_interval.tv_nsec = v.it_interval.tv_usec * NSEC_PER_USEC; |
---|
| 327 | + o->it_value.tv_sec = v.it_value.tv_sec; |
---|
| 328 | + o->it_value.tv_nsec = v.it_value.tv_usec * NSEC_PER_USEC; |
---|
| 329 | + return 0; |
---|
| 330 | +} |
---|
| 331 | + |
---|
| 332 | +SYSCALL_DEFINE3(setitimer, int, which, struct __kernel_old_itimerval __user *, value, |
---|
| 333 | + struct __kernel_old_itimerval __user *, ovalue) |
---|
| 334 | +{ |
---|
| 335 | + struct itimerspec64 set_buffer, get_buffer; |
---|
293 | 336 | int error; |
---|
294 | 337 | |
---|
295 | 338 | if (value) { |
---|
296 | | - if(copy_from_user(&set_buffer, value, sizeof(set_buffer))) |
---|
297 | | - return -EFAULT; |
---|
| 339 | + error = get_itimerval(&set_buffer, value); |
---|
| 340 | + if (error) |
---|
| 341 | + return error; |
---|
298 | 342 | } else { |
---|
299 | 343 | memset(&set_buffer, 0, sizeof(set_buffer)); |
---|
300 | 344 | printk_once(KERN_WARNING "%s calls setitimer() with new_value NULL pointer." |
---|
.. | .. |
---|
306 | 350 | if (error || !ovalue) |
---|
307 | 351 | return error; |
---|
308 | 352 | |
---|
309 | | - if (copy_to_user(ovalue, &get_buffer, sizeof(get_buffer))) |
---|
| 353 | + if (put_itimerval(ovalue, &get_buffer)) |
---|
310 | 354 | return -EFAULT; |
---|
311 | 355 | return 0; |
---|
312 | 356 | } |
---|
313 | 357 | |
---|
314 | | -#ifdef CONFIG_COMPAT |
---|
315 | | -COMPAT_SYSCALL_DEFINE3(setitimer, int, which, |
---|
316 | | - struct compat_itimerval __user *, in, |
---|
317 | | - struct compat_itimerval __user *, out) |
---|
| 358 | +#if defined(CONFIG_COMPAT) || defined(CONFIG_ALPHA) |
---|
| 359 | +static int get_old_itimerval32(struct itimerspec64 *o, const struct old_itimerval32 __user *i) |
---|
318 | 360 | { |
---|
319 | | - struct itimerval kin, kout; |
---|
| 361 | + struct old_itimerval32 v32; |
---|
| 362 | + |
---|
| 363 | + if (copy_from_user(&v32, i, sizeof(struct old_itimerval32))) |
---|
| 364 | + return -EFAULT; |
---|
| 365 | + |
---|
| 366 | + /* Validate the timevals in value. */ |
---|
| 367 | + if (!timeval_valid(&v32.it_value) || |
---|
| 368 | + !timeval_valid(&v32.it_interval)) |
---|
| 369 | + return -EINVAL; |
---|
| 370 | + |
---|
| 371 | + o->it_interval.tv_sec = v32.it_interval.tv_sec; |
---|
| 372 | + o->it_interval.tv_nsec = v32.it_interval.tv_usec * NSEC_PER_USEC; |
---|
| 373 | + o->it_value.tv_sec = v32.it_value.tv_sec; |
---|
| 374 | + o->it_value.tv_nsec = v32.it_value.tv_usec * NSEC_PER_USEC; |
---|
| 375 | + return 0; |
---|
| 376 | +} |
---|
| 377 | + |
---|
| 378 | +COMPAT_SYSCALL_DEFINE3(setitimer, int, which, |
---|
| 379 | + struct old_itimerval32 __user *, value, |
---|
| 380 | + struct old_itimerval32 __user *, ovalue) |
---|
| 381 | +{ |
---|
| 382 | + struct itimerspec64 set_buffer, get_buffer; |
---|
320 | 383 | int error; |
---|
321 | 384 | |
---|
322 | | - if (in) { |
---|
323 | | - if (get_compat_itimerval(&kin, in)) |
---|
324 | | - return -EFAULT; |
---|
| 385 | + if (value) { |
---|
| 386 | + error = get_old_itimerval32(&set_buffer, value); |
---|
| 387 | + if (error) |
---|
| 388 | + return error; |
---|
325 | 389 | } else { |
---|
326 | | - memset(&kin, 0, sizeof(kin)); |
---|
| 390 | + memset(&set_buffer, 0, sizeof(set_buffer)); |
---|
| 391 | + printk_once(KERN_WARNING "%s calls setitimer() with new_value NULL pointer." |
---|
| 392 | + " Misfeature support will be removed\n", |
---|
| 393 | + current->comm); |
---|
327 | 394 | } |
---|
328 | 395 | |
---|
329 | | - error = do_setitimer(which, &kin, out ? &kout : NULL); |
---|
330 | | - if (error || !out) |
---|
| 396 | + error = do_setitimer(which, &set_buffer, ovalue ? &get_buffer : NULL); |
---|
| 397 | + if (error || !ovalue) |
---|
331 | 398 | return error; |
---|
332 | | - if (put_compat_itimerval(out, &kout)) |
---|
| 399 | + if (put_old_itimerval32(ovalue, &get_buffer)) |
---|
333 | 400 | return -EFAULT; |
---|
334 | 401 | return 0; |
---|
335 | 402 | } |
---|