posix-timers.c 29 KB

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