posix-timers.c 30 KB

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