posix-timers.c 28 KB

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