posix-timers.c 29 KB

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