posix-timers.c 30 KB

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