posix-timers.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131
  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. static void schedule_next_timer(struct k_itimer *timr)
  318. {
  319. struct hrtimer *timer = &timr->it.real.timer;
  320. if (timr->it.real.interval.tv64 == 0)
  321. return;
  322. timr->it_overrun += (unsigned int) hrtimer_forward(timer,
  323. timer->base->get_time(),
  324. timr->it.real.interval);
  325. timr->it_overrun_last = timr->it_overrun;
  326. timr->it_overrun = -1;
  327. ++timr->it_requeue_pending;
  328. hrtimer_restart(timer);
  329. }
  330. /*
  331. * This function is exported for use by the signal deliver code. It is
  332. * called just prior to the info block being released and passes that
  333. * block to us. It's function is to update the overrun entry AND to
  334. * restart the timer. It should only be called if the timer is to be
  335. * restarted (i.e. we have flagged this in the sys_private entry of the
  336. * info block).
  337. *
  338. * To protect against the timer going away while the interrupt is queued,
  339. * we require that the it_requeue_pending flag be set.
  340. */
  341. void do_schedule_next_timer(struct siginfo *info)
  342. {
  343. struct k_itimer *timr;
  344. unsigned long flags;
  345. timr = lock_timer(info->si_tid, &flags);
  346. if (timr && timr->it_requeue_pending == info->si_sys_private) {
  347. if (timr->it_clock < 0)
  348. posix_cpu_timer_schedule(timr);
  349. else
  350. schedule_next_timer(timr);
  351. info->si_overrun += timr->it_overrun_last;
  352. }
  353. if (timr)
  354. unlock_timer(timr, flags);
  355. }
  356. int posix_timer_event(struct k_itimer *timr, int si_private)
  357. {
  358. struct task_struct *task;
  359. int shared, ret = -1;
  360. /*
  361. * FIXME: if ->sigq is queued we can race with
  362. * dequeue_signal()->do_schedule_next_timer().
  363. *
  364. * If dequeue_signal() sees the "right" value of
  365. * si_sys_private it calls do_schedule_next_timer().
  366. * We re-queue ->sigq and drop ->it_lock().
  367. * do_schedule_next_timer() locks the timer
  368. * and re-schedules it while ->sigq is pending.
  369. * Not really bad, but not that we want.
  370. */
  371. timr->sigq->info.si_sys_private = si_private;
  372. rcu_read_lock();
  373. task = pid_task(timr->it_pid, PIDTYPE_PID);
  374. if (task) {
  375. shared = !(timr->it_sigev_notify & SIGEV_THREAD_ID);
  376. ret = send_sigqueue(timr->sigq, task, shared);
  377. }
  378. rcu_read_unlock();
  379. /* If we failed to send the signal the timer stops. */
  380. return ret > 0;
  381. }
  382. EXPORT_SYMBOL_GPL(posix_timer_event);
  383. /*
  384. * This function gets called when a POSIX.1b interval timer expires. It
  385. * is used as a callback from the kernel internal timer. The
  386. * run_timer_list code ALWAYS calls with interrupts on.
  387. * This code is for CLOCK_REALTIME* and CLOCK_MONOTONIC* timers.
  388. */
  389. static enum hrtimer_restart posix_timer_fn(struct hrtimer *timer)
  390. {
  391. struct k_itimer *timr;
  392. unsigned long flags;
  393. int si_private = 0;
  394. enum hrtimer_restart ret = HRTIMER_NORESTART;
  395. timr = container_of(timer, struct k_itimer, it.real.timer);
  396. spin_lock_irqsave(&timr->it_lock, flags);
  397. if (timr->it.real.interval.tv64 != 0)
  398. si_private = ++timr->it_requeue_pending;
  399. if (posix_timer_event(timr, si_private)) {
  400. /*
  401. * signal was not sent because of sig_ignor
  402. * we will not get a call back to restart it AND
  403. * it should be restarted.
  404. */
  405. if (timr->it.real.interval.tv64 != 0) {
  406. ktime_t now = hrtimer_cb_get_time(timer);
  407. /*
  408. * FIXME: What we really want, is to stop this
  409. * timer completely and restart it in case the
  410. * SIG_IGN is removed. This is a non trivial
  411. * change which involves sighand locking
  412. * (sigh !), which we don't want to do late in
  413. * the release cycle.
  414. *
  415. * For now we just let timers with an interval
  416. * less than a jiffie expire every jiffie to
  417. * avoid softirq starvation in case of SIG_IGN
  418. * and a very small interval, which would put
  419. * the timer right back on the softirq pending
  420. * list. By moving now ahead of time we trick
  421. * hrtimer_forward() to expire the timer
  422. * later, while we still maintain the overrun
  423. * accuracy, but have some inconsistency in
  424. * the timer_gettime() case. This is at least
  425. * better than a starved softirq. A more
  426. * complex fix which solves also another related
  427. * inconsistency is already in the pipeline.
  428. */
  429. #ifdef CONFIG_HIGH_RES_TIMERS
  430. {
  431. ktime_t kj = ktime_set(0, NSEC_PER_SEC / HZ);
  432. if (timr->it.real.interval.tv64 < kj.tv64)
  433. now = ktime_add(now, kj);
  434. }
  435. #endif
  436. timr->it_overrun += (unsigned int)
  437. hrtimer_forward(timer, now,
  438. timr->it.real.interval);
  439. ret = HRTIMER_RESTART;
  440. ++timr->it_requeue_pending;
  441. }
  442. }
  443. unlock_timer(timr, flags);
  444. return ret;
  445. }
  446. static struct pid *good_sigevent(sigevent_t * event)
  447. {
  448. struct task_struct *rtn = current->group_leader;
  449. if ((event->sigev_notify & SIGEV_THREAD_ID ) &&
  450. (!(rtn = find_task_by_vpid(event->sigev_notify_thread_id)) ||
  451. !same_thread_group(rtn, current) ||
  452. (event->sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_SIGNAL))
  453. return NULL;
  454. if (((event->sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE) &&
  455. ((event->sigev_signo <= 0) || (event->sigev_signo > SIGRTMAX)))
  456. return NULL;
  457. return task_pid(rtn);
  458. }
  459. void posix_timers_register_clock(const clockid_t clock_id,
  460. struct k_clock *new_clock)
  461. {
  462. if ((unsigned) clock_id >= MAX_CLOCKS) {
  463. printk(KERN_WARNING "POSIX clock register failed for clock_id %d\n",
  464. clock_id);
  465. return;
  466. }
  467. if (!new_clock->clock_get) {
  468. printk(KERN_WARNING "POSIX clock id %d lacks clock_get()\n",
  469. clock_id);
  470. return;
  471. }
  472. if (!new_clock->clock_getres) {
  473. printk(KERN_WARNING "POSIX clock id %d lacks clock_getres()\n",
  474. clock_id);
  475. return;
  476. }
  477. posix_clocks[clock_id] = *new_clock;
  478. }
  479. EXPORT_SYMBOL_GPL(posix_timers_register_clock);
  480. static struct k_itimer * alloc_posix_timer(void)
  481. {
  482. struct k_itimer *tmr;
  483. tmr = kmem_cache_zalloc(posix_timers_cache, GFP_KERNEL);
  484. if (!tmr)
  485. return tmr;
  486. if (unlikely(!(tmr->sigq = sigqueue_alloc()))) {
  487. kmem_cache_free(posix_timers_cache, tmr);
  488. return NULL;
  489. }
  490. memset(&tmr->sigq->info, 0, sizeof(siginfo_t));
  491. return tmr;
  492. }
  493. static void k_itimer_rcu_free(struct rcu_head *head)
  494. {
  495. struct k_itimer *tmr = container_of(head, struct k_itimer, it.rcu);
  496. kmem_cache_free(posix_timers_cache, tmr);
  497. }
  498. #define IT_ID_SET 1
  499. #define IT_ID_NOT_SET 0
  500. static void release_posix_timer(struct k_itimer *tmr, int it_id_set)
  501. {
  502. if (it_id_set) {
  503. unsigned long flags;
  504. spin_lock_irqsave(&hash_lock, flags);
  505. hlist_del_rcu(&tmr->t_hash);
  506. spin_unlock_irqrestore(&hash_lock, flags);
  507. }
  508. put_pid(tmr->it_pid);
  509. sigqueue_free(tmr->sigq);
  510. call_rcu(&tmr->it.rcu, k_itimer_rcu_free);
  511. }
  512. static struct k_clock *clockid_to_kclock(const clockid_t id)
  513. {
  514. if (id < 0)
  515. return (id & CLOCKFD_MASK) == CLOCKFD ?
  516. &clock_posix_dynamic : &clock_posix_cpu;
  517. if (id >= MAX_CLOCKS || !posix_clocks[id].clock_getres)
  518. return NULL;
  519. return &posix_clocks[id];
  520. }
  521. static int common_timer_create(struct k_itimer *new_timer)
  522. {
  523. hrtimer_init(&new_timer->it.real.timer, new_timer->it_clock, 0);
  524. return 0;
  525. }
  526. /* Create a POSIX.1b interval timer. */
  527. SYSCALL_DEFINE3(timer_create, const clockid_t, which_clock,
  528. struct sigevent __user *, timer_event_spec,
  529. timer_t __user *, created_timer_id)
  530. {
  531. struct k_clock *kc = clockid_to_kclock(which_clock);
  532. struct k_itimer *new_timer;
  533. int error, new_timer_id;
  534. sigevent_t event;
  535. int it_id_set = IT_ID_NOT_SET;
  536. if (!kc)
  537. return -EINVAL;
  538. if (!kc->timer_create)
  539. return -EOPNOTSUPP;
  540. new_timer = alloc_posix_timer();
  541. if (unlikely(!new_timer))
  542. return -EAGAIN;
  543. spin_lock_init(&new_timer->it_lock);
  544. new_timer_id = posix_timer_add(new_timer);
  545. if (new_timer_id < 0) {
  546. error = new_timer_id;
  547. goto out;
  548. }
  549. it_id_set = IT_ID_SET;
  550. new_timer->it_id = (timer_t) new_timer_id;
  551. new_timer->it_clock = which_clock;
  552. new_timer->it_overrun = -1;
  553. if (timer_event_spec) {
  554. if (copy_from_user(&event, timer_event_spec, sizeof (event))) {
  555. error = -EFAULT;
  556. goto out;
  557. }
  558. rcu_read_lock();
  559. new_timer->it_pid = get_pid(good_sigevent(&event));
  560. rcu_read_unlock();
  561. if (!new_timer->it_pid) {
  562. error = -EINVAL;
  563. goto out;
  564. }
  565. } else {
  566. memset(&event.sigev_value, 0, sizeof(event.sigev_value));
  567. event.sigev_notify = SIGEV_SIGNAL;
  568. event.sigev_signo = SIGALRM;
  569. event.sigev_value.sival_int = new_timer->it_id;
  570. new_timer->it_pid = get_pid(task_tgid(current));
  571. }
  572. new_timer->it_sigev_notify = event.sigev_notify;
  573. new_timer->sigq->info.si_signo = event.sigev_signo;
  574. new_timer->sigq->info.si_value = event.sigev_value;
  575. new_timer->sigq->info.si_tid = new_timer->it_id;
  576. new_timer->sigq->info.si_code = SI_TIMER;
  577. if (copy_to_user(created_timer_id,
  578. &new_timer_id, sizeof (new_timer_id))) {
  579. error = -EFAULT;
  580. goto out;
  581. }
  582. error = kc->timer_create(new_timer);
  583. if (error)
  584. goto out;
  585. spin_lock_irq(&current->sighand->siglock);
  586. new_timer->it_signal = current->signal;
  587. list_add(&new_timer->list, &current->signal->posix_timers);
  588. spin_unlock_irq(&current->sighand->siglock);
  589. return 0;
  590. /*
  591. * In the case of the timer belonging to another task, after
  592. * the task is unlocked, the timer is owned by the other task
  593. * and may cease to exist at any time. Don't use or modify
  594. * new_timer after the unlock call.
  595. */
  596. out:
  597. release_posix_timer(new_timer, it_id_set);
  598. return error;
  599. }
  600. /*
  601. * Locking issues: We need to protect the result of the id look up until
  602. * we get the timer locked down so it is not deleted under us. The
  603. * removal is done under the idr spinlock so we use that here to bridge
  604. * the find to the timer lock. To avoid a dead lock, the timer id MUST
  605. * be release with out holding the timer lock.
  606. */
  607. static struct k_itimer *__lock_timer(timer_t timer_id, unsigned long *flags)
  608. {
  609. struct k_itimer *timr;
  610. /*
  611. * timer_t could be any type >= int and we want to make sure any
  612. * @timer_id outside positive int range fails lookup.
  613. */
  614. if ((unsigned long long)timer_id > INT_MAX)
  615. return NULL;
  616. rcu_read_lock();
  617. timr = posix_timer_by_id(timer_id);
  618. if (timr) {
  619. spin_lock_irqsave(&timr->it_lock, *flags);
  620. if (timr->it_signal == current->signal) {
  621. rcu_read_unlock();
  622. return timr;
  623. }
  624. spin_unlock_irqrestore(&timr->it_lock, *flags);
  625. }
  626. rcu_read_unlock();
  627. return NULL;
  628. }
  629. /*
  630. * Get the time remaining on a POSIX.1b interval timer. This function
  631. * is ALWAYS called with spin_lock_irq on the timer, thus it must not
  632. * mess with irq.
  633. *
  634. * We have a couple of messes to clean up here. First there is the case
  635. * of a timer that has a requeue pending. These timers should appear to
  636. * be in the timer list with an expiry as if we were to requeue them
  637. * now.
  638. *
  639. * The second issue is the SIGEV_NONE timer which may be active but is
  640. * not really ever put in the timer list (to save system resources).
  641. * This timer may be expired, and if so, we will do it here. Otherwise
  642. * it is the same as a requeue pending timer WRT to what we should
  643. * report.
  644. */
  645. static void
  646. common_timer_get(struct k_itimer *timr, struct itimerspec *cur_setting)
  647. {
  648. ktime_t now, remaining, iv;
  649. struct hrtimer *timer = &timr->it.real.timer;
  650. memset(cur_setting, 0, sizeof(struct itimerspec));
  651. iv = timr->it.real.interval;
  652. /* interval timer ? */
  653. if (iv.tv64)
  654. cur_setting->it_interval = ktime_to_timespec(iv);
  655. else if (!hrtimer_active(timer) &&
  656. (timr->it_sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE)
  657. return;
  658. now = timer->base->get_time();
  659. /*
  660. * When a requeue is pending or this is a SIGEV_NONE
  661. * timer move the expiry time forward by intervals, so
  662. * expiry is > now.
  663. */
  664. if (iv.tv64 && (timr->it_requeue_pending & REQUEUE_PENDING ||
  665. (timr->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE))
  666. timr->it_overrun += (unsigned int) hrtimer_forward(timer, now, iv);
  667. remaining = __hrtimer_expires_remaining_adjusted(timer, now);
  668. /* Return 0 only, when the timer is expired and not pending */
  669. if (remaining.tv64 <= 0) {
  670. /*
  671. * A single shot SIGEV_NONE timer must return 0, when
  672. * it is expired !
  673. */
  674. if ((timr->it_sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE)
  675. cur_setting->it_value.tv_nsec = 1;
  676. } else
  677. cur_setting->it_value = ktime_to_timespec(remaining);
  678. }
  679. /* Get the time remaining on a POSIX.1b interval timer. */
  680. SYSCALL_DEFINE2(timer_gettime, timer_t, timer_id,
  681. struct itimerspec __user *, setting)
  682. {
  683. struct itimerspec cur_setting;
  684. struct k_itimer *timr;
  685. struct k_clock *kc;
  686. unsigned long flags;
  687. int ret = 0;
  688. timr = lock_timer(timer_id, &flags);
  689. if (!timr)
  690. return -EINVAL;
  691. kc = clockid_to_kclock(timr->it_clock);
  692. if (WARN_ON_ONCE(!kc || !kc->timer_get))
  693. ret = -EINVAL;
  694. else
  695. kc->timer_get(timr, &cur_setting);
  696. unlock_timer(timr, flags);
  697. if (!ret && copy_to_user(setting, &cur_setting, sizeof (cur_setting)))
  698. return -EFAULT;
  699. return ret;
  700. }
  701. /*
  702. * Get the number of overruns of a POSIX.1b interval timer. This is to
  703. * be the overrun of the timer last delivered. At the same time we are
  704. * accumulating overruns on the next timer. The overrun is frozen when
  705. * the signal is delivered, either at the notify time (if the info block
  706. * is not queued) or at the actual delivery time (as we are informed by
  707. * the call back to do_schedule_next_timer(). So all we need to do is
  708. * to pick up the frozen overrun.
  709. */
  710. SYSCALL_DEFINE1(timer_getoverrun, timer_t, timer_id)
  711. {
  712. struct k_itimer *timr;
  713. int overrun;
  714. unsigned long flags;
  715. timr = lock_timer(timer_id, &flags);
  716. if (!timr)
  717. return -EINVAL;
  718. overrun = timr->it_overrun_last;
  719. unlock_timer(timr, flags);
  720. return overrun;
  721. }
  722. /* Set a POSIX.1b interval timer. */
  723. /* timr->it_lock is taken. */
  724. static int
  725. common_timer_set(struct k_itimer *timr, int flags,
  726. struct itimerspec *new_setting, struct itimerspec *old_setting)
  727. {
  728. struct hrtimer *timer = &timr->it.real.timer;
  729. enum hrtimer_mode mode;
  730. if (old_setting)
  731. common_timer_get(timr, old_setting);
  732. /* disable the timer */
  733. timr->it.real.interval.tv64 = 0;
  734. /*
  735. * careful here. If smp we could be in the "fire" routine which will
  736. * be spinning as we hold the lock. But this is ONLY an SMP issue.
  737. */
  738. if (hrtimer_try_to_cancel(timer) < 0)
  739. return TIMER_RETRY;
  740. timr->it_requeue_pending = (timr->it_requeue_pending + 2) &
  741. ~REQUEUE_PENDING;
  742. timr->it_overrun_last = 0;
  743. /* switch off the timer when it_value is zero */
  744. if (!new_setting->it_value.tv_sec && !new_setting->it_value.tv_nsec)
  745. return 0;
  746. mode = flags & TIMER_ABSTIME ? HRTIMER_MODE_ABS : HRTIMER_MODE_REL;
  747. hrtimer_init(&timr->it.real.timer, timr->it_clock, mode);
  748. timr->it.real.timer.function = posix_timer_fn;
  749. hrtimer_set_expires(timer, timespec_to_ktime(new_setting->it_value));
  750. /* Convert interval */
  751. timr->it.real.interval = timespec_to_ktime(new_setting->it_interval);
  752. /* SIGEV_NONE timers are not queued ! See common_timer_get */
  753. if (((timr->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE)) {
  754. /* Setup correct expiry time for relative timers */
  755. if (mode == HRTIMER_MODE_REL) {
  756. hrtimer_add_expires(timer, timer->base->get_time());
  757. }
  758. return 0;
  759. }
  760. hrtimer_start_expires(timer, mode);
  761. return 0;
  762. }
  763. /* Set a POSIX.1b interval timer */
  764. SYSCALL_DEFINE4(timer_settime, timer_t, timer_id, int, flags,
  765. const struct itimerspec __user *, new_setting,
  766. struct itimerspec __user *, old_setting)
  767. {
  768. struct k_itimer *timr;
  769. struct itimerspec new_spec, old_spec;
  770. int error = 0;
  771. unsigned long flag;
  772. struct itimerspec *rtn = old_setting ? &old_spec : NULL;
  773. struct k_clock *kc;
  774. if (!new_setting)
  775. return -EINVAL;
  776. if (copy_from_user(&new_spec, new_setting, sizeof (new_spec)))
  777. return -EFAULT;
  778. if (!timespec_valid(&new_spec.it_interval) ||
  779. !timespec_valid(&new_spec.it_value))
  780. return -EINVAL;
  781. retry:
  782. timr = lock_timer(timer_id, &flag);
  783. if (!timr)
  784. return -EINVAL;
  785. kc = clockid_to_kclock(timr->it_clock);
  786. if (WARN_ON_ONCE(!kc || !kc->timer_set))
  787. error = -EINVAL;
  788. else
  789. error = kc->timer_set(timr, flags, &new_spec, rtn);
  790. unlock_timer(timr, flag);
  791. if (error == TIMER_RETRY) {
  792. rtn = NULL; // We already got the old time...
  793. goto retry;
  794. }
  795. if (old_setting && !error &&
  796. copy_to_user(old_setting, &old_spec, sizeof (old_spec)))
  797. error = -EFAULT;
  798. return error;
  799. }
  800. static int common_timer_del(struct k_itimer *timer)
  801. {
  802. timer->it.real.interval.tv64 = 0;
  803. if (hrtimer_try_to_cancel(&timer->it.real.timer) < 0)
  804. return TIMER_RETRY;
  805. return 0;
  806. }
  807. static inline int timer_delete_hook(struct k_itimer *timer)
  808. {
  809. struct k_clock *kc = clockid_to_kclock(timer->it_clock);
  810. if (WARN_ON_ONCE(!kc || !kc->timer_del))
  811. return -EINVAL;
  812. return kc->timer_del(timer);
  813. }
  814. /* Delete a POSIX.1b interval timer. */
  815. SYSCALL_DEFINE1(timer_delete, timer_t, timer_id)
  816. {
  817. struct k_itimer *timer;
  818. unsigned long flags;
  819. retry_delete:
  820. timer = lock_timer(timer_id, &flags);
  821. if (!timer)
  822. return -EINVAL;
  823. if (timer_delete_hook(timer) == TIMER_RETRY) {
  824. unlock_timer(timer, flags);
  825. goto retry_delete;
  826. }
  827. spin_lock(&current->sighand->siglock);
  828. list_del(&timer->list);
  829. spin_unlock(&current->sighand->siglock);
  830. /*
  831. * This keeps any tasks waiting on the spin lock from thinking
  832. * they got something (see the lock code above).
  833. */
  834. timer->it_signal = NULL;
  835. unlock_timer(timer, flags);
  836. release_posix_timer(timer, IT_ID_SET);
  837. return 0;
  838. }
  839. /*
  840. * return timer owned by the process, used by exit_itimers
  841. */
  842. static void itimer_delete(struct k_itimer *timer)
  843. {
  844. unsigned long flags;
  845. retry_delete:
  846. spin_lock_irqsave(&timer->it_lock, flags);
  847. if (timer_delete_hook(timer) == TIMER_RETRY) {
  848. unlock_timer(timer, flags);
  849. goto retry_delete;
  850. }
  851. list_del(&timer->list);
  852. /*
  853. * This keeps any tasks waiting on the spin lock from thinking
  854. * they got something (see the lock code above).
  855. */
  856. timer->it_signal = NULL;
  857. unlock_timer(timer, flags);
  858. release_posix_timer(timer, IT_ID_SET);
  859. }
  860. /*
  861. * This is called by do_exit or de_thread, only when there are no more
  862. * references to the shared signal_struct.
  863. */
  864. void exit_itimers(struct signal_struct *sig)
  865. {
  866. struct k_itimer *tmr;
  867. while (!list_empty(&sig->posix_timers)) {
  868. tmr = list_entry(sig->posix_timers.next, struct k_itimer, list);
  869. itimer_delete(tmr);
  870. }
  871. }
  872. SYSCALL_DEFINE2(clock_settime, const clockid_t, which_clock,
  873. const struct timespec __user *, tp)
  874. {
  875. struct k_clock *kc = clockid_to_kclock(which_clock);
  876. struct timespec new_tp;
  877. if (!kc || !kc->clock_set)
  878. return -EINVAL;
  879. if (copy_from_user(&new_tp, tp, sizeof (*tp)))
  880. return -EFAULT;
  881. return kc->clock_set(which_clock, &new_tp);
  882. }
  883. SYSCALL_DEFINE2(clock_gettime, const clockid_t, which_clock,
  884. struct timespec __user *,tp)
  885. {
  886. struct k_clock *kc = clockid_to_kclock(which_clock);
  887. struct timespec kernel_tp;
  888. int error;
  889. if (!kc)
  890. return -EINVAL;
  891. error = kc->clock_get(which_clock, &kernel_tp);
  892. if (!error && copy_to_user(tp, &kernel_tp, sizeof (kernel_tp)))
  893. error = -EFAULT;
  894. return error;
  895. }
  896. SYSCALL_DEFINE2(clock_adjtime, const clockid_t, which_clock,
  897. struct timex __user *, utx)
  898. {
  899. struct k_clock *kc = clockid_to_kclock(which_clock);
  900. struct timex ktx;
  901. int err;
  902. if (!kc)
  903. return -EINVAL;
  904. if (!kc->clock_adj)
  905. return -EOPNOTSUPP;
  906. if (copy_from_user(&ktx, utx, sizeof(ktx)))
  907. return -EFAULT;
  908. err = kc->clock_adj(which_clock, &ktx);
  909. if (err >= 0 && copy_to_user(utx, &ktx, sizeof(ktx)))
  910. return -EFAULT;
  911. return err;
  912. }
  913. SYSCALL_DEFINE2(clock_getres, const clockid_t, which_clock,
  914. struct timespec __user *, tp)
  915. {
  916. struct k_clock *kc = clockid_to_kclock(which_clock);
  917. struct timespec rtn_tp;
  918. int error;
  919. if (!kc)
  920. return -EINVAL;
  921. error = kc->clock_getres(which_clock, &rtn_tp);
  922. if (!error && tp && copy_to_user(tp, &rtn_tp, sizeof (rtn_tp)))
  923. error = -EFAULT;
  924. return error;
  925. }
  926. /*
  927. * nanosleep for monotonic and realtime clocks
  928. */
  929. static int common_nsleep(const clockid_t which_clock, int flags,
  930. struct timespec *tsave, struct timespec __user *rmtp)
  931. {
  932. return hrtimer_nanosleep(tsave, rmtp, flags & TIMER_ABSTIME ?
  933. HRTIMER_MODE_ABS : HRTIMER_MODE_REL,
  934. which_clock);
  935. }
  936. SYSCALL_DEFINE4(clock_nanosleep, const clockid_t, which_clock, int, flags,
  937. const struct timespec __user *, rqtp,
  938. struct timespec __user *, rmtp)
  939. {
  940. struct k_clock *kc = clockid_to_kclock(which_clock);
  941. struct timespec t;
  942. if (!kc)
  943. return -EINVAL;
  944. if (!kc->nsleep)
  945. return -ENANOSLEEP_NOTSUP;
  946. if (copy_from_user(&t, rqtp, sizeof (struct timespec)))
  947. return -EFAULT;
  948. if (!timespec_valid(&t))
  949. return -EINVAL;
  950. return kc->nsleep(which_clock, flags, &t, rmtp);
  951. }
  952. /*
  953. * This will restart clock_nanosleep. This is required only by
  954. * compat_clock_nanosleep_restart for now.
  955. */
  956. long clock_nanosleep_restart(struct restart_block *restart_block)
  957. {
  958. clockid_t which_clock = restart_block->nanosleep.clockid;
  959. struct k_clock *kc = clockid_to_kclock(which_clock);
  960. if (WARN_ON_ONCE(!kc || !kc->nsleep_restart))
  961. return -EINVAL;
  962. return kc->nsleep_restart(restart_block);
  963. }