posix-timers.c 28 KB

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