posix-timers.c 28 KB

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