posix-timers.c 31 KB

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