posix-timers.c 29 KB

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