posix-timers.c 30 KB

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