posix-timers.c 30 KB

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