posix-timers.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039
  1. /*
  2. * linux/kernel/posix-timers.c
  3. *
  4. *
  5. * 2002-10-15 Posix Clocks & timers
  6. * by George Anzinger george@mvista.com
  7. *
  8. * Copyright (C) 2002 2003 by MontaVista Software.
  9. *
  10. * 2004-06-01 Fix CLOCK_REALTIME clock/timer TIMER_ABSTIME bug.
  11. * Copyright (C) 2004 Boris Hu
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or (at
  16. * your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful, but
  19. * WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  21. * General Public License for more details.
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  25. *
  26. * MontaVista Software | 1237 East Arques Avenue | Sunnyvale | CA 94085 | USA
  27. */
  28. /* These are all the functions necessary to implement
  29. * POSIX clocks & timers
  30. */
  31. #include <linux/mm.h>
  32. #include <linux/interrupt.h>
  33. #include <linux/slab.h>
  34. #include <linux/time.h>
  35. #include <linux/mutex.h>
  36. #include <asm/uaccess.h>
  37. #include <linux/list.h>
  38. #include <linux/init.h>
  39. #include <linux/compiler.h>
  40. #include <linux/idr.h>
  41. #include <linux/posix-timers.h>
  42. #include <linux/syscalls.h>
  43. #include <linux/wait.h>
  44. #include <linux/workqueue.h>
  45. #include <linux/module.h>
  46. /*
  47. * Management arrays for POSIX timers. Timers are kept in slab memory
  48. * Timer ids are allocated by an external routine that keeps track of the
  49. * id and the timer. The external interface is:
  50. *
  51. * void *idr_find(struct idr *idp, int id); to find timer_id <id>
  52. * int idr_get_new(struct idr *idp, void *ptr); to get a new id and
  53. * related it to <ptr>
  54. * void idr_remove(struct idr *idp, int id); to release <id>
  55. * void idr_init(struct idr *idp); to initialize <idp>
  56. * which we supply.
  57. * The idr_get_new *may* call slab for more memory so it must not be
  58. * called under a spin lock. Likewise idr_remore may release memory
  59. * (but it may be ok to do this under a lock...).
  60. * idr_find is just a memory look up and is quite fast. A -1 return
  61. * indicates that the requested id does not exist.
  62. */
  63. /*
  64. * Lets keep our timers in a slab cache :-)
  65. */
  66. static struct kmem_cache *posix_timers_cache;
  67. static struct idr posix_timers_id;
  68. static DEFINE_SPINLOCK(idr_lock);
  69. /*
  70. * we assume that the new SIGEV_THREAD_ID shares no bits with the other
  71. * SIGEV values. Here we put out an error if this assumption fails.
  72. */
  73. #if SIGEV_THREAD_ID != (SIGEV_THREAD_ID & \
  74. ~(SIGEV_SIGNAL | SIGEV_NONE | SIGEV_THREAD))
  75. #error "SIGEV_THREAD_ID must not share bit with other SIGEV values!"
  76. #endif
  77. /*
  78. * parisc wants ENOTSUP instead of EOPNOTSUPP
  79. */
  80. #ifndef ENOTSUP
  81. # define ENANOSLEEP_NOTSUP EOPNOTSUPP
  82. #else
  83. # define ENANOSLEEP_NOTSUP ENOTSUP
  84. #endif
  85. /*
  86. * The timer ID is turned into a timer address by idr_find().
  87. * Verifying a valid ID consists of:
  88. *
  89. * a) checking that idr_find() returns other than -1.
  90. * b) checking that the timer id matches the one in the timer itself.
  91. * c) that the timer owner is in the callers thread group.
  92. */
  93. /*
  94. * CLOCKs: The POSIX standard calls for a couple of clocks and allows us
  95. * to implement others. This structure defines the various
  96. * clocks and allows the possibility of adding others. We
  97. * provide an interface to add clocks to the table and expect
  98. * the "arch" code to add at least one clock that is high
  99. * resolution. Here we define the standard CLOCK_REALTIME as a
  100. * 1/HZ resolution clock.
  101. *
  102. * RESOLUTION: Clock resolution is used to round up timer and interval
  103. * times, NOT to report clock times, which are reported with as
  104. * much resolution as the system can muster. In some cases this
  105. * resolution may depend on the underlying clock hardware and
  106. * may not be quantifiable until run time, and only then is the
  107. * necessary code is written. The standard says we should say
  108. * something about this issue in the documentation...
  109. *
  110. * FUNCTIONS: The CLOCKs structure defines possible functions to handle
  111. * various clock functions. For clocks that use the standard
  112. * system timer code these entries should be NULL. This will
  113. * allow dispatch without the overhead of indirect function
  114. * calls. CLOCKS that depend on other sources (e.g. WWV or GPS)
  115. * must supply functions here, even if the function just returns
  116. * ENOSYS. The standard POSIX timer management code assumes the
  117. * following: 1.) The k_itimer struct (sched.h) is used for the
  118. * timer. 2.) The list, it_lock, it_clock, it_id and it_pid
  119. * fields are not modified by timer code.
  120. *
  121. * At this time all functions EXCEPT clock_nanosleep can be
  122. * redirected by the CLOCKS structure. Clock_nanosleep is in
  123. * there, but the code ignores it.
  124. *
  125. * Permissions: It is assumed that the clock_settime() function defined
  126. * for each clock will take care of permission checks. Some
  127. * clocks may be set able by any user (i.e. local process
  128. * clocks) others not. Currently the only set able clock we
  129. * have is CLOCK_REALTIME and its high res counter part, both of
  130. * which we beg off on and pass to do_sys_settimeofday().
  131. */
  132. static struct k_clock posix_clocks[MAX_CLOCKS];
  133. /*
  134. * These ones are defined below.
  135. */
  136. static int common_nsleep(const clockid_t, int flags, struct timespec *t,
  137. struct timespec __user *rmtp);
  138. static void common_timer_get(struct k_itimer *, struct itimerspec *);
  139. static int common_timer_set(struct k_itimer *, int,
  140. struct itimerspec *, struct itimerspec *);
  141. static int common_timer_del(struct k_itimer *timer);
  142. static enum hrtimer_restart posix_timer_fn(struct hrtimer *data);
  143. static struct k_itimer *__lock_timer(timer_t timer_id, unsigned long *flags);
  144. #define lock_timer(tid, flags) \
  145. ({ struct k_itimer *__timr; \
  146. __cond_lock(&__timr->it_lock, __timr = __lock_timer(tid, flags)); \
  147. __timr; \
  148. })
  149. static inline void unlock_timer(struct k_itimer *timr, unsigned long flags)
  150. {
  151. spin_unlock_irqrestore(&timr->it_lock, flags);
  152. }
  153. /*
  154. * Call the k_clock hook function if non-null, or the default function.
  155. */
  156. #define CLOCK_DISPATCH(clock, call, arglist) \
  157. ((clock) < 0 ? posix_cpu_##call arglist : \
  158. (posix_clocks[clock].call != NULL \
  159. ? (*posix_clocks[clock].call) arglist : common_##call arglist))
  160. /*
  161. * Default clock hook functions when the struct k_clock passed
  162. * to register_posix_clock leaves a function pointer null.
  163. *
  164. * The function common_CALL is the default implementation for
  165. * the function pointer CALL in struct k_clock.
  166. */
  167. static inline int common_clock_getres(const clockid_t which_clock,
  168. struct timespec *tp)
  169. {
  170. tp->tv_sec = 0;
  171. tp->tv_nsec = posix_clocks[which_clock].res;
  172. return 0;
  173. }
  174. static int common_timer_create(struct k_itimer *new_timer)
  175. {
  176. hrtimer_init(&new_timer->it.real.timer, new_timer->it_clock, 0);
  177. return 0;
  178. }
  179. static int no_timer_create(struct k_itimer *new_timer)
  180. {
  181. return -EOPNOTSUPP;
  182. }
  183. /*
  184. * Return nonzero if we know a priori this clockid_t value is bogus.
  185. */
  186. static inline int invalid_clockid(const clockid_t which_clock)
  187. {
  188. if (which_clock < 0) /* CPU clock, posix_cpu_* will check it */
  189. return 0;
  190. if ((unsigned) which_clock >= MAX_CLOCKS)
  191. return 1;
  192. if (posix_clocks[which_clock].clock_getres != NULL)
  193. return 0;
  194. if (posix_clocks[which_clock].res != 0)
  195. return 0;
  196. return 1;
  197. }
  198. /* Get clock_realtime */
  199. static int posix_clock_realtime_get(clockid_t which_clock, struct timespec *tp)
  200. {
  201. ktime_get_real_ts(tp);
  202. return 0;
  203. }
  204. /* Set clock_realtime */
  205. static int posix_clock_realtime_set(const clockid_t which_clock,
  206. const struct timespec *tp)
  207. {
  208. return do_sys_settimeofday(tp, NULL);
  209. }
  210. /*
  211. * Get monotonic time for posix timers
  212. */
  213. static int posix_ktime_get_ts(clockid_t which_clock, struct timespec *tp)
  214. {
  215. ktime_get_ts(tp);
  216. return 0;
  217. }
  218. /*
  219. * Get monotonic time for posix timers
  220. */
  221. static int posix_get_monotonic_raw(clockid_t which_clock, struct timespec *tp)
  222. {
  223. getrawmonotonic(tp);
  224. return 0;
  225. }
  226. static int posix_get_realtime_coarse(clockid_t which_clock, struct timespec *tp)
  227. {
  228. *tp = current_kernel_time();
  229. return 0;
  230. }
  231. static int posix_get_monotonic_coarse(clockid_t which_clock,
  232. struct timespec *tp)
  233. {
  234. *tp = get_monotonic_coarse();
  235. return 0;
  236. }
  237. static int posix_get_coarse_res(const clockid_t which_clock, struct timespec *tp)
  238. {
  239. *tp = ktime_to_timespec(KTIME_LOW_RES);
  240. return 0;
  241. }
  242. /*
  243. * Initialize everything, well, just everything in Posix clocks/timers ;)
  244. */
  245. static __init int init_posix_timers(void)
  246. {
  247. struct k_clock clock_realtime = {
  248. .clock_getres = hrtimer_get_res,
  249. .clock_get = posix_clock_realtime_get,
  250. .clock_set = posix_clock_realtime_set,
  251. .nsleep = common_nsleep,
  252. .nsleep_restart = hrtimer_nanosleep_restart,
  253. };
  254. struct k_clock clock_monotonic = {
  255. .clock_getres = hrtimer_get_res,
  256. .clock_get = posix_ktime_get_ts,
  257. .nsleep = common_nsleep,
  258. .nsleep_restart = hrtimer_nanosleep_restart,
  259. };
  260. struct k_clock clock_monotonic_raw = {
  261. .clock_getres = hrtimer_get_res,
  262. .clock_get = posix_get_monotonic_raw,
  263. .timer_create = no_timer_create,
  264. };
  265. struct k_clock clock_realtime_coarse = {
  266. .clock_getres = posix_get_coarse_res,
  267. .clock_get = posix_get_realtime_coarse,
  268. .timer_create = no_timer_create,
  269. };
  270. struct k_clock clock_monotonic_coarse = {
  271. .clock_getres = posix_get_coarse_res,
  272. .clock_get = posix_get_monotonic_coarse,
  273. .timer_create = no_timer_create,
  274. };
  275. register_posix_clock(CLOCK_REALTIME, &clock_realtime);
  276. register_posix_clock(CLOCK_MONOTONIC, &clock_monotonic);
  277. register_posix_clock(CLOCK_MONOTONIC_RAW, &clock_monotonic_raw);
  278. register_posix_clock(CLOCK_REALTIME_COARSE, &clock_realtime_coarse);
  279. register_posix_clock(CLOCK_MONOTONIC_COARSE, &clock_monotonic_coarse);
  280. posix_timers_cache = kmem_cache_create("posix_timers_cache",
  281. sizeof (struct k_itimer), 0, SLAB_PANIC,
  282. NULL);
  283. idr_init(&posix_timers_id);
  284. return 0;
  285. }
  286. __initcall(init_posix_timers);
  287. static void schedule_next_timer(struct k_itimer *timr)
  288. {
  289. struct hrtimer *timer = &timr->it.real.timer;
  290. if (timr->it.real.interval.tv64 == 0)
  291. return;
  292. timr->it_overrun += (unsigned int) hrtimer_forward(timer,
  293. timer->base->get_time(),
  294. timr->it.real.interval);
  295. timr->it_overrun_last = timr->it_overrun;
  296. timr->it_overrun = -1;
  297. ++timr->it_requeue_pending;
  298. hrtimer_restart(timer);
  299. }
  300. /*
  301. * This function is exported for use by the signal deliver code. It is
  302. * called just prior to the info block being released and passes that
  303. * block to us. It's function is to update the overrun entry AND to
  304. * restart the timer. It should only be called if the timer is to be
  305. * restarted (i.e. we have flagged this in the sys_private entry of the
  306. * info block).
  307. *
  308. * To protect aginst the timer going away while the interrupt is queued,
  309. * we require that the it_requeue_pending flag be set.
  310. */
  311. void do_schedule_next_timer(struct siginfo *info)
  312. {
  313. struct k_itimer *timr;
  314. unsigned long flags;
  315. timr = lock_timer(info->si_tid, &flags);
  316. if (timr && timr->it_requeue_pending == info->si_sys_private) {
  317. if (timr->it_clock < 0)
  318. posix_cpu_timer_schedule(timr);
  319. else
  320. schedule_next_timer(timr);
  321. info->si_overrun += timr->it_overrun_last;
  322. }
  323. if (timr)
  324. unlock_timer(timr, flags);
  325. }
  326. int posix_timer_event(struct k_itimer *timr, int si_private)
  327. {
  328. struct task_struct *task;
  329. int shared, ret = -1;
  330. /*
  331. * FIXME: if ->sigq is queued we can race with
  332. * dequeue_signal()->do_schedule_next_timer().
  333. *
  334. * If dequeue_signal() sees the "right" value of
  335. * si_sys_private it calls do_schedule_next_timer().
  336. * We re-queue ->sigq and drop ->it_lock().
  337. * do_schedule_next_timer() locks the timer
  338. * and re-schedules it while ->sigq is pending.
  339. * Not really bad, but not that we want.
  340. */
  341. timr->sigq->info.si_sys_private = si_private;
  342. rcu_read_lock();
  343. task = pid_task(timr->it_pid, PIDTYPE_PID);
  344. if (task) {
  345. shared = !(timr->it_sigev_notify & SIGEV_THREAD_ID);
  346. ret = send_sigqueue(timr->sigq, task, shared);
  347. }
  348. rcu_read_unlock();
  349. /* If we failed to send the signal the timer stops. */
  350. return ret > 0;
  351. }
  352. EXPORT_SYMBOL_GPL(posix_timer_event);
  353. /*
  354. * This function gets called when a POSIX.1b interval timer expires. It
  355. * is used as a callback from the kernel internal timer. The
  356. * run_timer_list code ALWAYS calls with interrupts on.
  357. * This code is for CLOCK_REALTIME* and CLOCK_MONOTONIC* timers.
  358. */
  359. static enum hrtimer_restart posix_timer_fn(struct hrtimer *timer)
  360. {
  361. struct k_itimer *timr;
  362. unsigned long flags;
  363. int si_private = 0;
  364. enum hrtimer_restart ret = HRTIMER_NORESTART;
  365. timr = container_of(timer, struct k_itimer, it.real.timer);
  366. spin_lock_irqsave(&timr->it_lock, flags);
  367. if (timr->it.real.interval.tv64 != 0)
  368. si_private = ++timr->it_requeue_pending;
  369. if (posix_timer_event(timr, si_private)) {
  370. /*
  371. * signal was not sent because of sig_ignor
  372. * we will not get a call back to restart it AND
  373. * it should be restarted.
  374. */
  375. if (timr->it.real.interval.tv64 != 0) {
  376. ktime_t now = hrtimer_cb_get_time(timer);
  377. /*
  378. * FIXME: What we really want, is to stop this
  379. * timer completely and restart it in case the
  380. * SIG_IGN is removed. This is a non trivial
  381. * change which involves sighand locking
  382. * (sigh !), which we don't want to do late in
  383. * the release cycle.
  384. *
  385. * For now we just let timers with an interval
  386. * less than a jiffie expire every jiffie to
  387. * avoid softirq starvation in case of SIG_IGN
  388. * and a very small interval, which would put
  389. * the timer right back on the softirq pending
  390. * list. By moving now ahead of time we trick
  391. * hrtimer_forward() to expire the timer
  392. * later, while we still maintain the overrun
  393. * accuracy, but have some inconsistency in
  394. * the timer_gettime() case. This is at least
  395. * better than a starved softirq. A more
  396. * complex fix which solves also another related
  397. * inconsistency is already in the pipeline.
  398. */
  399. #ifdef CONFIG_HIGH_RES_TIMERS
  400. {
  401. ktime_t kj = ktime_set(0, NSEC_PER_SEC / HZ);
  402. if (timr->it.real.interval.tv64 < kj.tv64)
  403. now = ktime_add(now, kj);
  404. }
  405. #endif
  406. timr->it_overrun += (unsigned int)
  407. hrtimer_forward(timer, now,
  408. timr->it.real.interval);
  409. ret = HRTIMER_RESTART;
  410. ++timr->it_requeue_pending;
  411. }
  412. }
  413. unlock_timer(timr, flags);
  414. return ret;
  415. }
  416. static struct pid *good_sigevent(sigevent_t * event)
  417. {
  418. struct task_struct *rtn = current->group_leader;
  419. if ((event->sigev_notify & SIGEV_THREAD_ID ) &&
  420. (!(rtn = find_task_by_vpid(event->sigev_notify_thread_id)) ||
  421. !same_thread_group(rtn, current) ||
  422. (event->sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_SIGNAL))
  423. return NULL;
  424. if (((event->sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE) &&
  425. ((event->sigev_signo <= 0) || (event->sigev_signo > SIGRTMAX)))
  426. return NULL;
  427. return task_pid(rtn);
  428. }
  429. void register_posix_clock(const clockid_t clock_id, struct k_clock *new_clock)
  430. {
  431. if ((unsigned) clock_id >= MAX_CLOCKS) {
  432. printk("POSIX clock register failed for clock_id %d\n",
  433. clock_id);
  434. return;
  435. }
  436. posix_clocks[clock_id] = *new_clock;
  437. }
  438. EXPORT_SYMBOL_GPL(register_posix_clock);
  439. static struct k_itimer * alloc_posix_timer(void)
  440. {
  441. struct k_itimer *tmr;
  442. tmr = kmem_cache_zalloc(posix_timers_cache, GFP_KERNEL);
  443. if (!tmr)
  444. return tmr;
  445. if (unlikely(!(tmr->sigq = sigqueue_alloc()))) {
  446. kmem_cache_free(posix_timers_cache, tmr);
  447. return NULL;
  448. }
  449. memset(&tmr->sigq->info, 0, sizeof(siginfo_t));
  450. return tmr;
  451. }
  452. #define IT_ID_SET 1
  453. #define IT_ID_NOT_SET 0
  454. static void release_posix_timer(struct k_itimer *tmr, int it_id_set)
  455. {
  456. if (it_id_set) {
  457. unsigned long flags;
  458. spin_lock_irqsave(&idr_lock, flags);
  459. idr_remove(&posix_timers_id, tmr->it_id);
  460. spin_unlock_irqrestore(&idr_lock, flags);
  461. }
  462. put_pid(tmr->it_pid);
  463. sigqueue_free(tmr->sigq);
  464. kmem_cache_free(posix_timers_cache, tmr);
  465. }
  466. static struct k_clock *clockid_to_kclock(const clockid_t id)
  467. {
  468. if (id < 0)
  469. return &clock_posix_cpu;
  470. if (id >= MAX_CLOCKS || !posix_clocks[id].clock_getres)
  471. return NULL;
  472. return &posix_clocks[id];
  473. }
  474. /* Create a POSIX.1b interval timer. */
  475. SYSCALL_DEFINE3(timer_create, const clockid_t, which_clock,
  476. struct sigevent __user *, timer_event_spec,
  477. timer_t __user *, created_timer_id)
  478. {
  479. struct k_itimer *new_timer;
  480. int error, new_timer_id;
  481. sigevent_t event;
  482. int it_id_set = IT_ID_NOT_SET;
  483. if (invalid_clockid(which_clock))
  484. return -EINVAL;
  485. new_timer = alloc_posix_timer();
  486. if (unlikely(!new_timer))
  487. return -EAGAIN;
  488. spin_lock_init(&new_timer->it_lock);
  489. retry:
  490. if (unlikely(!idr_pre_get(&posix_timers_id, GFP_KERNEL))) {
  491. error = -EAGAIN;
  492. goto out;
  493. }
  494. spin_lock_irq(&idr_lock);
  495. error = idr_get_new(&posix_timers_id, new_timer, &new_timer_id);
  496. spin_unlock_irq(&idr_lock);
  497. if (error) {
  498. if (error == -EAGAIN)
  499. goto retry;
  500. /*
  501. * Weird looking, but we return EAGAIN if the IDR is
  502. * full (proper POSIX return value for this)
  503. */
  504. error = -EAGAIN;
  505. goto out;
  506. }
  507. it_id_set = IT_ID_SET;
  508. new_timer->it_id = (timer_t) new_timer_id;
  509. new_timer->it_clock = which_clock;
  510. new_timer->it_overrun = -1;
  511. if (timer_event_spec) {
  512. if (copy_from_user(&event, timer_event_spec, sizeof (event))) {
  513. error = -EFAULT;
  514. goto out;
  515. }
  516. rcu_read_lock();
  517. new_timer->it_pid = get_pid(good_sigevent(&event));
  518. rcu_read_unlock();
  519. if (!new_timer->it_pid) {
  520. error = -EINVAL;
  521. goto out;
  522. }
  523. } else {
  524. event.sigev_notify = SIGEV_SIGNAL;
  525. event.sigev_signo = SIGALRM;
  526. event.sigev_value.sival_int = new_timer->it_id;
  527. new_timer->it_pid = get_pid(task_tgid(current));
  528. }
  529. new_timer->it_sigev_notify = event.sigev_notify;
  530. new_timer->sigq->info.si_signo = event.sigev_signo;
  531. new_timer->sigq->info.si_value = event.sigev_value;
  532. new_timer->sigq->info.si_tid = new_timer->it_id;
  533. new_timer->sigq->info.si_code = SI_TIMER;
  534. if (copy_to_user(created_timer_id,
  535. &new_timer_id, sizeof (new_timer_id))) {
  536. error = -EFAULT;
  537. goto out;
  538. }
  539. error = CLOCK_DISPATCH(which_clock, timer_create, (new_timer));
  540. if (error)
  541. goto out;
  542. spin_lock_irq(&current->sighand->siglock);
  543. new_timer->it_signal = current->signal;
  544. list_add(&new_timer->list, &current->signal->posix_timers);
  545. spin_unlock_irq(&current->sighand->siglock);
  546. return 0;
  547. /*
  548. * In the case of the timer belonging to another task, after
  549. * the task is unlocked, the timer is owned by the other task
  550. * and may cease to exist at any time. Don't use or modify
  551. * new_timer after the unlock call.
  552. */
  553. out:
  554. release_posix_timer(new_timer, it_id_set);
  555. return error;
  556. }
  557. /*
  558. * Locking issues: We need to protect the result of the id look up until
  559. * we get the timer locked down so it is not deleted under us. The
  560. * removal is done under the idr spinlock so we use that here to bridge
  561. * the find to the timer lock. To avoid a dead lock, the timer id MUST
  562. * be release with out holding the timer lock.
  563. */
  564. static struct k_itimer *__lock_timer(timer_t timer_id, unsigned long *flags)
  565. {
  566. struct k_itimer *timr;
  567. /*
  568. * Watch out here. We do a irqsave on the idr_lock and pass the
  569. * flags part over to the timer lock. Must not let interrupts in
  570. * while we are moving the lock.
  571. */
  572. spin_lock_irqsave(&idr_lock, *flags);
  573. timr = idr_find(&posix_timers_id, (int)timer_id);
  574. if (timr) {
  575. spin_lock(&timr->it_lock);
  576. if (timr->it_signal == current->signal) {
  577. spin_unlock(&idr_lock);
  578. return timr;
  579. }
  580. spin_unlock(&timr->it_lock);
  581. }
  582. spin_unlock_irqrestore(&idr_lock, *flags);
  583. return NULL;
  584. }
  585. /*
  586. * Get the time remaining on a POSIX.1b interval timer. This function
  587. * is ALWAYS called with spin_lock_irq on the timer, thus it must not
  588. * mess with irq.
  589. *
  590. * We have a couple of messes to clean up here. First there is the case
  591. * of a timer that has a requeue pending. These timers should appear to
  592. * be in the timer list with an expiry as if we were to requeue them
  593. * now.
  594. *
  595. * The second issue is the SIGEV_NONE timer which may be active but is
  596. * not really ever put in the timer list (to save system resources).
  597. * This timer may be expired, and if so, we will do it here. Otherwise
  598. * it is the same as a requeue pending timer WRT to what we should
  599. * report.
  600. */
  601. static void
  602. common_timer_get(struct k_itimer *timr, struct itimerspec *cur_setting)
  603. {
  604. ktime_t now, remaining, iv;
  605. struct hrtimer *timer = &timr->it.real.timer;
  606. memset(cur_setting, 0, sizeof(struct itimerspec));
  607. iv = timr->it.real.interval;
  608. /* interval timer ? */
  609. if (iv.tv64)
  610. cur_setting->it_interval = ktime_to_timespec(iv);
  611. else if (!hrtimer_active(timer) &&
  612. (timr->it_sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE)
  613. return;
  614. now = timer->base->get_time();
  615. /*
  616. * When a requeue is pending or this is a SIGEV_NONE
  617. * timer move the expiry time forward by intervals, so
  618. * expiry is > now.
  619. */
  620. if (iv.tv64 && (timr->it_requeue_pending & REQUEUE_PENDING ||
  621. (timr->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE))
  622. timr->it_overrun += (unsigned int) hrtimer_forward(timer, now, iv);
  623. remaining = ktime_sub(hrtimer_get_expires(timer), now);
  624. /* Return 0 only, when the timer is expired and not pending */
  625. if (remaining.tv64 <= 0) {
  626. /*
  627. * A single shot SIGEV_NONE timer must return 0, when
  628. * it is expired !
  629. */
  630. if ((timr->it_sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE)
  631. cur_setting->it_value.tv_nsec = 1;
  632. } else
  633. cur_setting->it_value = ktime_to_timespec(remaining);
  634. }
  635. /* Get the time remaining on a POSIX.1b interval timer. */
  636. SYSCALL_DEFINE2(timer_gettime, timer_t, timer_id,
  637. struct itimerspec __user *, setting)
  638. {
  639. struct k_itimer *timr;
  640. struct itimerspec cur_setting;
  641. unsigned long flags;
  642. timr = lock_timer(timer_id, &flags);
  643. if (!timr)
  644. return -EINVAL;
  645. CLOCK_DISPATCH(timr->it_clock, timer_get, (timr, &cur_setting));
  646. unlock_timer(timr, flags);
  647. if (copy_to_user(setting, &cur_setting, sizeof (cur_setting)))
  648. return -EFAULT;
  649. return 0;
  650. }
  651. /*
  652. * Get the number of overruns of a POSIX.1b interval timer. This is to
  653. * be the overrun of the timer last delivered. At the same time we are
  654. * accumulating overruns on the next timer. The overrun is frozen when
  655. * the signal is delivered, either at the notify time (if the info block
  656. * is not queued) or at the actual delivery time (as we are informed by
  657. * the call back to do_schedule_next_timer(). So all we need to do is
  658. * to pick up the frozen overrun.
  659. */
  660. SYSCALL_DEFINE1(timer_getoverrun, timer_t, timer_id)
  661. {
  662. struct k_itimer *timr;
  663. int overrun;
  664. unsigned long flags;
  665. timr = lock_timer(timer_id, &flags);
  666. if (!timr)
  667. return -EINVAL;
  668. overrun = timr->it_overrun_last;
  669. unlock_timer(timr, flags);
  670. return overrun;
  671. }
  672. /* Set a POSIX.1b interval timer. */
  673. /* timr->it_lock is taken. */
  674. static int
  675. common_timer_set(struct k_itimer *timr, int flags,
  676. struct itimerspec *new_setting, struct itimerspec *old_setting)
  677. {
  678. struct hrtimer *timer = &timr->it.real.timer;
  679. enum hrtimer_mode mode;
  680. if (old_setting)
  681. common_timer_get(timr, old_setting);
  682. /* disable the timer */
  683. timr->it.real.interval.tv64 = 0;
  684. /*
  685. * careful here. If smp we could be in the "fire" routine which will
  686. * be spinning as we hold the lock. But this is ONLY an SMP issue.
  687. */
  688. if (hrtimer_try_to_cancel(timer) < 0)
  689. return TIMER_RETRY;
  690. timr->it_requeue_pending = (timr->it_requeue_pending + 2) &
  691. ~REQUEUE_PENDING;
  692. timr->it_overrun_last = 0;
  693. /* switch off the timer when it_value is zero */
  694. if (!new_setting->it_value.tv_sec && !new_setting->it_value.tv_nsec)
  695. return 0;
  696. mode = flags & TIMER_ABSTIME ? HRTIMER_MODE_ABS : HRTIMER_MODE_REL;
  697. hrtimer_init(&timr->it.real.timer, timr->it_clock, mode);
  698. timr->it.real.timer.function = posix_timer_fn;
  699. hrtimer_set_expires(timer, timespec_to_ktime(new_setting->it_value));
  700. /* Convert interval */
  701. timr->it.real.interval = timespec_to_ktime(new_setting->it_interval);
  702. /* SIGEV_NONE timers are not queued ! See common_timer_get */
  703. if (((timr->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE)) {
  704. /* Setup correct expiry time for relative timers */
  705. if (mode == HRTIMER_MODE_REL) {
  706. hrtimer_add_expires(timer, timer->base->get_time());
  707. }
  708. return 0;
  709. }
  710. hrtimer_start_expires(timer, mode);
  711. return 0;
  712. }
  713. /* Set a POSIX.1b interval timer */
  714. SYSCALL_DEFINE4(timer_settime, timer_t, timer_id, int, flags,
  715. const struct itimerspec __user *, new_setting,
  716. struct itimerspec __user *, old_setting)
  717. {
  718. struct k_itimer *timr;
  719. struct itimerspec new_spec, old_spec;
  720. int error = 0;
  721. unsigned long flag;
  722. struct itimerspec *rtn = old_setting ? &old_spec : NULL;
  723. if (!new_setting)
  724. return -EINVAL;
  725. if (copy_from_user(&new_spec, new_setting, sizeof (new_spec)))
  726. return -EFAULT;
  727. if (!timespec_valid(&new_spec.it_interval) ||
  728. !timespec_valid(&new_spec.it_value))
  729. return -EINVAL;
  730. retry:
  731. timr = lock_timer(timer_id, &flag);
  732. if (!timr)
  733. return -EINVAL;
  734. error = CLOCK_DISPATCH(timr->it_clock, timer_set,
  735. (timr, flags, &new_spec, rtn));
  736. unlock_timer(timr, flag);
  737. if (error == TIMER_RETRY) {
  738. rtn = NULL; // We already got the old time...
  739. goto retry;
  740. }
  741. if (old_setting && !error &&
  742. copy_to_user(old_setting, &old_spec, sizeof (old_spec)))
  743. error = -EFAULT;
  744. return error;
  745. }
  746. static inline int common_timer_del(struct k_itimer *timer)
  747. {
  748. timer->it.real.interval.tv64 = 0;
  749. if (hrtimer_try_to_cancel(&timer->it.real.timer) < 0)
  750. return TIMER_RETRY;
  751. return 0;
  752. }
  753. static inline int timer_delete_hook(struct k_itimer *timer)
  754. {
  755. return CLOCK_DISPATCH(timer->it_clock, timer_del, (timer));
  756. }
  757. /* Delete a POSIX.1b interval timer. */
  758. SYSCALL_DEFINE1(timer_delete, timer_t, timer_id)
  759. {
  760. struct k_itimer *timer;
  761. unsigned long flags;
  762. retry_delete:
  763. timer = lock_timer(timer_id, &flags);
  764. if (!timer)
  765. return -EINVAL;
  766. if (timer_delete_hook(timer) == TIMER_RETRY) {
  767. unlock_timer(timer, flags);
  768. goto retry_delete;
  769. }
  770. spin_lock(&current->sighand->siglock);
  771. list_del(&timer->list);
  772. spin_unlock(&current->sighand->siglock);
  773. /*
  774. * This keeps any tasks waiting on the spin lock from thinking
  775. * they got something (see the lock code above).
  776. */
  777. timer->it_signal = NULL;
  778. unlock_timer(timer, flags);
  779. release_posix_timer(timer, IT_ID_SET);
  780. return 0;
  781. }
  782. /*
  783. * return timer owned by the process, used by exit_itimers
  784. */
  785. static void itimer_delete(struct k_itimer *timer)
  786. {
  787. unsigned long flags;
  788. retry_delete:
  789. spin_lock_irqsave(&timer->it_lock, flags);
  790. if (timer_delete_hook(timer) == TIMER_RETRY) {
  791. unlock_timer(timer, flags);
  792. goto retry_delete;
  793. }
  794. list_del(&timer->list);
  795. /*
  796. * This keeps any tasks waiting on the spin lock from thinking
  797. * they got something (see the lock code above).
  798. */
  799. timer->it_signal = NULL;
  800. unlock_timer(timer, flags);
  801. release_posix_timer(timer, IT_ID_SET);
  802. }
  803. /*
  804. * This is called by do_exit or de_thread, only when there are no more
  805. * references to the shared signal_struct.
  806. */
  807. void exit_itimers(struct signal_struct *sig)
  808. {
  809. struct k_itimer *tmr;
  810. while (!list_empty(&sig->posix_timers)) {
  811. tmr = list_entry(sig->posix_timers.next, struct k_itimer, list);
  812. itimer_delete(tmr);
  813. }
  814. }
  815. SYSCALL_DEFINE2(clock_settime, const clockid_t, which_clock,
  816. const struct timespec __user *, tp)
  817. {
  818. struct k_clock *kc = clockid_to_kclock(which_clock);
  819. struct timespec new_tp;
  820. if (!kc || !kc->clock_set)
  821. return -EINVAL;
  822. if (copy_from_user(&new_tp, tp, sizeof (*tp)))
  823. return -EFAULT;
  824. return kc->clock_set(which_clock, &new_tp);
  825. }
  826. SYSCALL_DEFINE2(clock_gettime, const clockid_t, which_clock,
  827. struct timespec __user *,tp)
  828. {
  829. struct k_clock *kc = clockid_to_kclock(which_clock);
  830. struct timespec kernel_tp;
  831. int error;
  832. if (!kc)
  833. return -EINVAL;
  834. if (!kc->clock_get)
  835. return -EOPNOTSUPP;
  836. error = kc->clock_get(which_clock, &kernel_tp);
  837. if (!error && copy_to_user(tp, &kernel_tp, sizeof (kernel_tp)))
  838. error = -EFAULT;
  839. return error;
  840. }
  841. SYSCALL_DEFINE2(clock_getres, const clockid_t, which_clock,
  842. struct timespec __user *, tp)
  843. {
  844. struct timespec rtn_tp;
  845. int error;
  846. if (invalid_clockid(which_clock))
  847. return -EINVAL;
  848. error = CLOCK_DISPATCH(which_clock, clock_getres,
  849. (which_clock, &rtn_tp));
  850. if (!error && tp && copy_to_user(tp, &rtn_tp, sizeof (rtn_tp))) {
  851. error = -EFAULT;
  852. }
  853. return error;
  854. }
  855. /*
  856. * nanosleep for monotonic and realtime clocks
  857. */
  858. static int common_nsleep(const clockid_t which_clock, int flags,
  859. struct timespec *tsave, struct timespec __user *rmtp)
  860. {
  861. return hrtimer_nanosleep(tsave, rmtp, flags & TIMER_ABSTIME ?
  862. HRTIMER_MODE_ABS : HRTIMER_MODE_REL,
  863. which_clock);
  864. }
  865. SYSCALL_DEFINE4(clock_nanosleep, const clockid_t, which_clock, int, flags,
  866. const struct timespec __user *, rqtp,
  867. struct timespec __user *, rmtp)
  868. {
  869. struct k_clock *kc = clockid_to_kclock(which_clock);
  870. struct timespec t;
  871. if (!kc)
  872. return -EINVAL;
  873. if (!kc->nsleep)
  874. return -ENANOSLEEP_NOTSUP;
  875. if (copy_from_user(&t, rqtp, sizeof (struct timespec)))
  876. return -EFAULT;
  877. if (!timespec_valid(&t))
  878. return -EINVAL;
  879. return kc->nsleep(which_clock, flags, &t, rmtp);
  880. }
  881. /*
  882. * This will restart clock_nanosleep. This is required only by
  883. * compat_clock_nanosleep_restart for now.
  884. */
  885. long clock_nanosleep_restart(struct restart_block *restart_block)
  886. {
  887. clockid_t which_clock = restart_block->nanosleep.index;
  888. struct k_clock *kc = clockid_to_kclock(which_clock);
  889. if (WARN_ON_ONCE(!kc || !kc->nsleep_restart))
  890. return -EINVAL;
  891. return kc->nsleep_restart(restart_block);
  892. }