posix-timers.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028
  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. * Initialize everything, well, just everything in Posix clocks/timers ;)
  204. */
  205. static __init int init_posix_timers(void)
  206. {
  207. struct k_clock clock_realtime = {
  208. .clock_getres = hrtimer_get_res,
  209. };
  210. struct k_clock clock_monotonic = {
  211. .clock_getres = hrtimer_get_res,
  212. .clock_get = posix_ktime_get_ts,
  213. .clock_set = do_posix_clock_nosettime,
  214. };
  215. register_posix_clock(CLOCK_REALTIME, &clock_realtime);
  216. register_posix_clock(CLOCK_MONOTONIC, &clock_monotonic);
  217. posix_timers_cache = kmem_cache_create("posix_timers_cache",
  218. sizeof (struct k_itimer), 0, SLAB_PANIC,
  219. NULL);
  220. idr_init(&posix_timers_id);
  221. return 0;
  222. }
  223. __initcall(init_posix_timers);
  224. static void schedule_next_timer(struct k_itimer *timr)
  225. {
  226. struct hrtimer *timer = &timr->it.real.timer;
  227. if (timr->it.real.interval.tv64 == 0)
  228. return;
  229. timr->it_overrun += (unsigned int) hrtimer_forward(timer,
  230. timer->base->get_time(),
  231. timr->it.real.interval);
  232. timr->it_overrun_last = timr->it_overrun;
  233. timr->it_overrun = -1;
  234. ++timr->it_requeue_pending;
  235. hrtimer_restart(timer);
  236. }
  237. /*
  238. * This function is exported for use by the signal deliver code. It is
  239. * called just prior to the info block being released and passes that
  240. * block to us. It's function is to update the overrun entry AND to
  241. * restart the timer. It should only be called if the timer is to be
  242. * restarted (i.e. we have flagged this in the sys_private entry of the
  243. * info block).
  244. *
  245. * To protect aginst the timer going away while the interrupt is queued,
  246. * we require that the it_requeue_pending flag be set.
  247. */
  248. void do_schedule_next_timer(struct siginfo *info)
  249. {
  250. struct k_itimer *timr;
  251. unsigned long flags;
  252. timr = lock_timer(info->si_tid, &flags);
  253. if (timr && timr->it_requeue_pending == info->si_sys_private) {
  254. if (timr->it_clock < 0)
  255. posix_cpu_timer_schedule(timr);
  256. else
  257. schedule_next_timer(timr);
  258. info->si_overrun = timr->it_overrun_last;
  259. }
  260. if (timr)
  261. unlock_timer(timr, flags);
  262. }
  263. int posix_timer_event(struct k_itimer *timr,int si_private)
  264. {
  265. memset(&timr->sigq->info, 0, sizeof(siginfo_t));
  266. timr->sigq->info.si_sys_private = si_private;
  267. /* Send signal to the process that owns this timer.*/
  268. timr->sigq->info.si_signo = timr->it_sigev_signo;
  269. timr->sigq->info.si_errno = 0;
  270. timr->sigq->info.si_code = SI_TIMER;
  271. timr->sigq->info.si_tid = timr->it_id;
  272. timr->sigq->info.si_value = timr->it_sigev_value;
  273. if (timr->it_sigev_notify & SIGEV_THREAD_ID) {
  274. struct task_struct *leader;
  275. int ret = send_sigqueue(timr->sigq, timr->it_process, 0);
  276. if (likely(ret >= 0))
  277. return ret;
  278. timr->it_sigev_notify = SIGEV_SIGNAL;
  279. leader = timr->it_process->group_leader;
  280. put_task_struct(timr->it_process);
  281. timr->it_process = leader;
  282. }
  283. return send_sigqueue(timr->sigq, timr->it_process, 1);
  284. }
  285. EXPORT_SYMBOL_GPL(posix_timer_event);
  286. /*
  287. * This function gets called when a POSIX.1b interval timer expires. It
  288. * is used as a callback from the kernel internal timer. The
  289. * run_timer_list code ALWAYS calls with interrupts on.
  290. * This code is for CLOCK_REALTIME* and CLOCK_MONOTONIC* timers.
  291. */
  292. static enum hrtimer_restart posix_timer_fn(struct hrtimer *timer)
  293. {
  294. struct k_itimer *timr;
  295. unsigned long flags;
  296. int si_private = 0;
  297. enum hrtimer_restart ret = HRTIMER_NORESTART;
  298. timr = container_of(timer, struct k_itimer, it.real.timer);
  299. spin_lock_irqsave(&timr->it_lock, flags);
  300. if (timr->it.real.interval.tv64 != 0)
  301. si_private = ++timr->it_requeue_pending;
  302. if (posix_timer_event(timr, si_private)) {
  303. /*
  304. * signal was not sent because of sig_ignor
  305. * we will not get a call back to restart it AND
  306. * it should be restarted.
  307. */
  308. if (timr->it.real.interval.tv64 != 0) {
  309. ktime_t now = hrtimer_cb_get_time(timer);
  310. /*
  311. * FIXME: What we really want, is to stop this
  312. * timer completely and restart it in case the
  313. * SIG_IGN is removed. This is a non trivial
  314. * change which involves sighand locking
  315. * (sigh !), which we don't want to do late in
  316. * the release cycle.
  317. *
  318. * For now we just let timers with an interval
  319. * less than a jiffie expire every jiffie to
  320. * avoid softirq starvation in case of SIG_IGN
  321. * and a very small interval, which would put
  322. * the timer right back on the softirq pending
  323. * list. By moving now ahead of time we trick
  324. * hrtimer_forward() to expire the timer
  325. * later, while we still maintain the overrun
  326. * accuracy, but have some inconsistency in
  327. * the timer_gettime() case. This is at least
  328. * better than a starved softirq. A more
  329. * complex fix which solves also another related
  330. * inconsistency is already in the pipeline.
  331. */
  332. #ifdef CONFIG_HIGH_RES_TIMERS
  333. {
  334. ktime_t kj = ktime_set(0, NSEC_PER_SEC / HZ);
  335. if (timr->it.real.interval.tv64 < kj.tv64)
  336. now = ktime_add(now, kj);
  337. }
  338. #endif
  339. timr->it_overrun += (unsigned int)
  340. hrtimer_forward(timer, now,
  341. timr->it.real.interval);
  342. ret = HRTIMER_RESTART;
  343. ++timr->it_requeue_pending;
  344. }
  345. }
  346. unlock_timer(timr, flags);
  347. return ret;
  348. }
  349. static struct task_struct * good_sigevent(sigevent_t * event)
  350. {
  351. struct task_struct *rtn = current->group_leader;
  352. if ((event->sigev_notify & SIGEV_THREAD_ID ) &&
  353. (!(rtn = find_task_by_vpid(event->sigev_notify_thread_id)) ||
  354. !same_thread_group(rtn, current) ||
  355. (event->sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_SIGNAL))
  356. return NULL;
  357. if (((event->sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE) &&
  358. ((event->sigev_signo <= 0) || (event->sigev_signo > SIGRTMAX)))
  359. return NULL;
  360. return rtn;
  361. }
  362. void register_posix_clock(const clockid_t clock_id, struct k_clock *new_clock)
  363. {
  364. if ((unsigned) clock_id >= MAX_CLOCKS) {
  365. printk("POSIX clock register failed for clock_id %d\n",
  366. clock_id);
  367. return;
  368. }
  369. posix_clocks[clock_id] = *new_clock;
  370. }
  371. EXPORT_SYMBOL_GPL(register_posix_clock);
  372. static struct k_itimer * alloc_posix_timer(void)
  373. {
  374. struct k_itimer *tmr;
  375. tmr = kmem_cache_zalloc(posix_timers_cache, GFP_KERNEL);
  376. if (!tmr)
  377. return tmr;
  378. if (unlikely(!(tmr->sigq = sigqueue_alloc()))) {
  379. kmem_cache_free(posix_timers_cache, tmr);
  380. tmr = NULL;
  381. }
  382. return tmr;
  383. }
  384. #define IT_ID_SET 1
  385. #define IT_ID_NOT_SET 0
  386. static void release_posix_timer(struct k_itimer *tmr, int it_id_set)
  387. {
  388. if (it_id_set) {
  389. unsigned long flags;
  390. spin_lock_irqsave(&idr_lock, flags);
  391. idr_remove(&posix_timers_id, tmr->it_id);
  392. spin_unlock_irqrestore(&idr_lock, flags);
  393. }
  394. sigqueue_free(tmr->sigq);
  395. if (unlikely(tmr->it_process) &&
  396. tmr->it_sigev_notify == (SIGEV_SIGNAL|SIGEV_THREAD_ID))
  397. put_task_struct(tmr->it_process);
  398. kmem_cache_free(posix_timers_cache, tmr);
  399. }
  400. /* Create a POSIX.1b interval timer. */
  401. asmlinkage long
  402. sys_timer_create(const clockid_t which_clock,
  403. struct sigevent __user *timer_event_spec,
  404. timer_t __user * created_timer_id)
  405. {
  406. int error = 0;
  407. struct k_itimer *new_timer = NULL;
  408. int new_timer_id;
  409. struct task_struct *process = NULL;
  410. unsigned long flags;
  411. sigevent_t event;
  412. int it_id_set = IT_ID_NOT_SET;
  413. if (invalid_clockid(which_clock))
  414. return -EINVAL;
  415. new_timer = alloc_posix_timer();
  416. if (unlikely(!new_timer))
  417. return -EAGAIN;
  418. spin_lock_init(&new_timer->it_lock);
  419. retry:
  420. if (unlikely(!idr_pre_get(&posix_timers_id, GFP_KERNEL))) {
  421. error = -EAGAIN;
  422. goto out;
  423. }
  424. spin_lock_irq(&idr_lock);
  425. error = idr_get_new(&posix_timers_id, (void *) new_timer,
  426. &new_timer_id);
  427. spin_unlock_irq(&idr_lock);
  428. if (error == -EAGAIN)
  429. goto retry;
  430. else if (error) {
  431. /*
  432. * Weird looking, but we return EAGAIN if the IDR is
  433. * full (proper POSIX return value for this)
  434. */
  435. error = -EAGAIN;
  436. goto out;
  437. }
  438. it_id_set = IT_ID_SET;
  439. new_timer->it_id = (timer_t) new_timer_id;
  440. new_timer->it_clock = which_clock;
  441. new_timer->it_overrun = -1;
  442. error = CLOCK_DISPATCH(which_clock, timer_create, (new_timer));
  443. if (error)
  444. goto out;
  445. /*
  446. * return the timer_id now. The next step is hard to
  447. * back out if there is an error.
  448. */
  449. if (copy_to_user(created_timer_id,
  450. &new_timer_id, sizeof (new_timer_id))) {
  451. error = -EFAULT;
  452. goto out;
  453. }
  454. if (timer_event_spec) {
  455. if (copy_from_user(&event, timer_event_spec, sizeof (event))) {
  456. error = -EFAULT;
  457. goto out;
  458. }
  459. new_timer->it_sigev_notify = event.sigev_notify;
  460. new_timer->it_sigev_signo = event.sigev_signo;
  461. new_timer->it_sigev_value = event.sigev_value;
  462. read_lock(&tasklist_lock);
  463. if ((process = good_sigevent(&event))) {
  464. /*
  465. * We may be setting up this process for another
  466. * thread. It may be exiting. To catch this
  467. * case the we check the PF_EXITING flag. If
  468. * the flag is not set, the siglock will catch
  469. * him before it is too late (in exit_itimers).
  470. *
  471. * The exec case is a bit more invloved but easy
  472. * to code. If the process is in our thread
  473. * group (and it must be or we would not allow
  474. * it here) and is doing an exec, it will cause
  475. * us to be killed. In this case it will wait
  476. * for us to die which means we can finish this
  477. * linkage with our last gasp. I.e. no code :)
  478. */
  479. spin_lock_irqsave(&process->sighand->siglock, flags);
  480. if (!(process->flags & PF_EXITING)) {
  481. new_timer->it_process = process;
  482. list_add(&new_timer->list,
  483. &process->signal->posix_timers);
  484. if (new_timer->it_sigev_notify == (SIGEV_SIGNAL|SIGEV_THREAD_ID))
  485. get_task_struct(process);
  486. spin_unlock_irqrestore(&process->sighand->siglock, flags);
  487. } else {
  488. spin_unlock_irqrestore(&process->sighand->siglock, flags);
  489. process = NULL;
  490. }
  491. }
  492. read_unlock(&tasklist_lock);
  493. if (!process) {
  494. error = -EINVAL;
  495. goto out;
  496. }
  497. } else {
  498. new_timer->it_sigev_notify = SIGEV_SIGNAL;
  499. new_timer->it_sigev_signo = SIGALRM;
  500. new_timer->it_sigev_value.sival_int = new_timer->it_id;
  501. process = current->group_leader;
  502. spin_lock_irqsave(&process->sighand->siglock, flags);
  503. new_timer->it_process = process;
  504. list_add(&new_timer->list, &process->signal->posix_timers);
  505. spin_unlock_irqrestore(&process->sighand->siglock, flags);
  506. }
  507. /*
  508. * In the case of the timer belonging to another task, after
  509. * the task is unlocked, the timer is owned by the other task
  510. * and may cease to exist at any time. Don't use or modify
  511. * new_timer after the unlock call.
  512. */
  513. out:
  514. if (error)
  515. release_posix_timer(new_timer, it_id_set);
  516. return error;
  517. }
  518. /*
  519. * Locking issues: We need to protect the result of the id look up until
  520. * we get the timer locked down so it is not deleted under us. The
  521. * removal is done under the idr spinlock so we use that here to bridge
  522. * the find to the timer lock. To avoid a dead lock, the timer id MUST
  523. * be release with out holding the timer lock.
  524. */
  525. static struct k_itimer * lock_timer(timer_t timer_id, unsigned long *flags)
  526. {
  527. struct k_itimer *timr;
  528. /*
  529. * Watch out here. We do a irqsave on the idr_lock and pass the
  530. * flags part over to the timer lock. Must not let interrupts in
  531. * while we are moving the lock.
  532. */
  533. spin_lock_irqsave(&idr_lock, *flags);
  534. timr = (struct k_itimer *) idr_find(&posix_timers_id, (int) timer_id);
  535. if (timr) {
  536. spin_lock(&timr->it_lock);
  537. if ((timr->it_id != timer_id) || !(timr->it_process) ||
  538. !same_thread_group(timr->it_process, current)) {
  539. spin_unlock(&timr->it_lock);
  540. spin_unlock_irqrestore(&idr_lock, *flags);
  541. timr = NULL;
  542. } else
  543. spin_unlock(&idr_lock);
  544. } else
  545. spin_unlock_irqrestore(&idr_lock, *flags);
  546. return timr;
  547. }
  548. /*
  549. * Get the time remaining on a POSIX.1b interval timer. This function
  550. * is ALWAYS called with spin_lock_irq on the timer, thus it must not
  551. * mess with irq.
  552. *
  553. * We have a couple of messes to clean up here. First there is the case
  554. * of a timer that has a requeue pending. These timers should appear to
  555. * be in the timer list with an expiry as if we were to requeue them
  556. * now.
  557. *
  558. * The second issue is the SIGEV_NONE timer which may be active but is
  559. * not really ever put in the timer list (to save system resources).
  560. * This timer may be expired, and if so, we will do it here. Otherwise
  561. * it is the same as a requeue pending timer WRT to what we should
  562. * report.
  563. */
  564. static void
  565. common_timer_get(struct k_itimer *timr, struct itimerspec *cur_setting)
  566. {
  567. ktime_t now, remaining, iv;
  568. struct hrtimer *timer = &timr->it.real.timer;
  569. memset(cur_setting, 0, sizeof(struct itimerspec));
  570. iv = timr->it.real.interval;
  571. /* interval timer ? */
  572. if (iv.tv64)
  573. cur_setting->it_interval = ktime_to_timespec(iv);
  574. else if (!hrtimer_active(timer) &&
  575. (timr->it_sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE)
  576. return;
  577. now = timer->base->get_time();
  578. /*
  579. * When a requeue is pending or this is a SIGEV_NONE
  580. * timer move the expiry time forward by intervals, so
  581. * expiry is > now.
  582. */
  583. if (iv.tv64 && (timr->it_requeue_pending & REQUEUE_PENDING ||
  584. (timr->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE))
  585. timr->it_overrun += (unsigned int) hrtimer_forward(timer, now, iv);
  586. remaining = ktime_sub(timer->expires, now);
  587. /* Return 0 only, when the timer is expired and not pending */
  588. if (remaining.tv64 <= 0) {
  589. /*
  590. * A single shot SIGEV_NONE timer must return 0, when
  591. * it is expired !
  592. */
  593. if ((timr->it_sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE)
  594. cur_setting->it_value.tv_nsec = 1;
  595. } else
  596. cur_setting->it_value = ktime_to_timespec(remaining);
  597. }
  598. /* Get the time remaining on a POSIX.1b interval timer. */
  599. asmlinkage long
  600. sys_timer_gettime(timer_t timer_id, struct itimerspec __user *setting)
  601. {
  602. struct k_itimer *timr;
  603. struct itimerspec cur_setting;
  604. unsigned long flags;
  605. timr = lock_timer(timer_id, &flags);
  606. if (!timr)
  607. return -EINVAL;
  608. CLOCK_DISPATCH(timr->it_clock, timer_get, (timr, &cur_setting));
  609. unlock_timer(timr, flags);
  610. if (copy_to_user(setting, &cur_setting, sizeof (cur_setting)))
  611. return -EFAULT;
  612. return 0;
  613. }
  614. /*
  615. * Get the number of overruns of a POSIX.1b interval timer. This is to
  616. * be the overrun of the timer last delivered. At the same time we are
  617. * accumulating overruns on the next timer. The overrun is frozen when
  618. * the signal is delivered, either at the notify time (if the info block
  619. * is not queued) or at the actual delivery time (as we are informed by
  620. * the call back to do_schedule_next_timer(). So all we need to do is
  621. * to pick up the frozen overrun.
  622. */
  623. asmlinkage long
  624. sys_timer_getoverrun(timer_t timer_id)
  625. {
  626. struct k_itimer *timr;
  627. int overrun;
  628. unsigned long flags;
  629. timr = lock_timer(timer_id, &flags);
  630. if (!timr)
  631. return -EINVAL;
  632. overrun = timr->it_overrun_last;
  633. unlock_timer(timr, flags);
  634. return overrun;
  635. }
  636. /* Set a POSIX.1b interval timer. */
  637. /* timr->it_lock is taken. */
  638. static int
  639. common_timer_set(struct k_itimer *timr, int flags,
  640. struct itimerspec *new_setting, struct itimerspec *old_setting)
  641. {
  642. struct hrtimer *timer = &timr->it.real.timer;
  643. enum hrtimer_mode mode;
  644. if (old_setting)
  645. common_timer_get(timr, old_setting);
  646. /* disable the timer */
  647. timr->it.real.interval.tv64 = 0;
  648. /*
  649. * careful here. If smp we could be in the "fire" routine which will
  650. * be spinning as we hold the lock. But this is ONLY an SMP issue.
  651. */
  652. if (hrtimer_try_to_cancel(timer) < 0)
  653. return TIMER_RETRY;
  654. timr->it_requeue_pending = (timr->it_requeue_pending + 2) &
  655. ~REQUEUE_PENDING;
  656. timr->it_overrun_last = 0;
  657. /* switch off the timer when it_value is zero */
  658. if (!new_setting->it_value.tv_sec && !new_setting->it_value.tv_nsec)
  659. return 0;
  660. mode = flags & TIMER_ABSTIME ? HRTIMER_MODE_ABS : HRTIMER_MODE_REL;
  661. hrtimer_init(&timr->it.real.timer, timr->it_clock, mode);
  662. timr->it.real.timer.function = posix_timer_fn;
  663. timer->expires = timespec_to_ktime(new_setting->it_value);
  664. /* Convert interval */
  665. timr->it.real.interval = timespec_to_ktime(new_setting->it_interval);
  666. /* SIGEV_NONE timers are not queued ! See common_timer_get */
  667. if (((timr->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE)) {
  668. /* Setup correct expiry time for relative timers */
  669. if (mode == HRTIMER_MODE_REL) {
  670. timer->expires =
  671. ktime_add_safe(timer->expires,
  672. timer->base->get_time());
  673. }
  674. return 0;
  675. }
  676. hrtimer_start(timer, timer->expires, mode);
  677. return 0;
  678. }
  679. /* Set a POSIX.1b interval timer */
  680. asmlinkage long
  681. sys_timer_settime(timer_t timer_id, int flags,
  682. const struct itimerspec __user *new_setting,
  683. struct itimerspec __user *old_setting)
  684. {
  685. struct k_itimer *timr;
  686. struct itimerspec new_spec, old_spec;
  687. int error = 0;
  688. unsigned long flag;
  689. struct itimerspec *rtn = old_setting ? &old_spec : NULL;
  690. if (!new_setting)
  691. return -EINVAL;
  692. if (copy_from_user(&new_spec, new_setting, sizeof (new_spec)))
  693. return -EFAULT;
  694. if (!timespec_valid(&new_spec.it_interval) ||
  695. !timespec_valid(&new_spec.it_value))
  696. return -EINVAL;
  697. retry:
  698. timr = lock_timer(timer_id, &flag);
  699. if (!timr)
  700. return -EINVAL;
  701. error = CLOCK_DISPATCH(timr->it_clock, timer_set,
  702. (timr, flags, &new_spec, rtn));
  703. unlock_timer(timr, flag);
  704. if (error == TIMER_RETRY) {
  705. rtn = NULL; // We already got the old time...
  706. goto retry;
  707. }
  708. if (old_setting && !error &&
  709. copy_to_user(old_setting, &old_spec, sizeof (old_spec)))
  710. error = -EFAULT;
  711. return error;
  712. }
  713. static inline int common_timer_del(struct k_itimer *timer)
  714. {
  715. timer->it.real.interval.tv64 = 0;
  716. if (hrtimer_try_to_cancel(&timer->it.real.timer) < 0)
  717. return TIMER_RETRY;
  718. return 0;
  719. }
  720. static inline int timer_delete_hook(struct k_itimer *timer)
  721. {
  722. return CLOCK_DISPATCH(timer->it_clock, timer_del, (timer));
  723. }
  724. /* Delete a POSIX.1b interval timer. */
  725. asmlinkage long
  726. sys_timer_delete(timer_t timer_id)
  727. {
  728. struct k_itimer *timer;
  729. unsigned long flags;
  730. retry_delete:
  731. timer = lock_timer(timer_id, &flags);
  732. if (!timer)
  733. return -EINVAL;
  734. if (timer_delete_hook(timer) == TIMER_RETRY) {
  735. unlock_timer(timer, flags);
  736. goto retry_delete;
  737. }
  738. spin_lock(&current->sighand->siglock);
  739. list_del(&timer->list);
  740. spin_unlock(&current->sighand->siglock);
  741. /*
  742. * This keeps any tasks waiting on the spin lock from thinking
  743. * they got something (see the lock code above).
  744. */
  745. if (timer->it_process) {
  746. if (timer->it_sigev_notify == (SIGEV_SIGNAL|SIGEV_THREAD_ID))
  747. put_task_struct(timer->it_process);
  748. timer->it_process = NULL;
  749. }
  750. unlock_timer(timer, flags);
  751. release_posix_timer(timer, IT_ID_SET);
  752. return 0;
  753. }
  754. /*
  755. * return timer owned by the process, used by exit_itimers
  756. */
  757. static void itimer_delete(struct k_itimer *timer)
  758. {
  759. unsigned long flags;
  760. retry_delete:
  761. spin_lock_irqsave(&timer->it_lock, flags);
  762. if (timer_delete_hook(timer) == TIMER_RETRY) {
  763. unlock_timer(timer, flags);
  764. goto retry_delete;
  765. }
  766. list_del(&timer->list);
  767. /*
  768. * This keeps any tasks waiting on the spin lock from thinking
  769. * they got something (see the lock code above).
  770. */
  771. if (timer->it_process) {
  772. if (timer->it_sigev_notify == (SIGEV_SIGNAL|SIGEV_THREAD_ID))
  773. put_task_struct(timer->it_process);
  774. timer->it_process = NULL;
  775. }
  776. unlock_timer(timer, flags);
  777. release_posix_timer(timer, IT_ID_SET);
  778. }
  779. /*
  780. * This is called by do_exit or de_thread, only when there are no more
  781. * references to the shared signal_struct.
  782. */
  783. void exit_itimers(struct signal_struct *sig)
  784. {
  785. struct k_itimer *tmr;
  786. while (!list_empty(&sig->posix_timers)) {
  787. tmr = list_entry(sig->posix_timers.next, struct k_itimer, list);
  788. itimer_delete(tmr);
  789. }
  790. }
  791. /* Not available / possible... functions */
  792. int do_posix_clock_nosettime(const clockid_t clockid, struct timespec *tp)
  793. {
  794. return -EINVAL;
  795. }
  796. EXPORT_SYMBOL_GPL(do_posix_clock_nosettime);
  797. int do_posix_clock_nonanosleep(const clockid_t clock, int flags,
  798. struct timespec *t, struct timespec __user *r)
  799. {
  800. #ifndef ENOTSUP
  801. return -EOPNOTSUPP; /* aka ENOTSUP in userland for POSIX */
  802. #else /* parisc does define it separately. */
  803. return -ENOTSUP;
  804. #endif
  805. }
  806. EXPORT_SYMBOL_GPL(do_posix_clock_nonanosleep);
  807. asmlinkage long sys_clock_settime(const clockid_t which_clock,
  808. const struct timespec __user *tp)
  809. {
  810. struct timespec new_tp;
  811. if (invalid_clockid(which_clock))
  812. return -EINVAL;
  813. if (copy_from_user(&new_tp, tp, sizeof (*tp)))
  814. return -EFAULT;
  815. return CLOCK_DISPATCH(which_clock, clock_set, (which_clock, &new_tp));
  816. }
  817. asmlinkage long
  818. sys_clock_gettime(const clockid_t which_clock, struct timespec __user *tp)
  819. {
  820. struct timespec kernel_tp;
  821. int error;
  822. if (invalid_clockid(which_clock))
  823. return -EINVAL;
  824. error = CLOCK_DISPATCH(which_clock, clock_get,
  825. (which_clock, &kernel_tp));
  826. if (!error && copy_to_user(tp, &kernel_tp, sizeof (kernel_tp)))
  827. error = -EFAULT;
  828. return error;
  829. }
  830. asmlinkage long
  831. sys_clock_getres(const clockid_t which_clock, struct timespec __user *tp)
  832. {
  833. struct timespec rtn_tp;
  834. int error;
  835. if (invalid_clockid(which_clock))
  836. return -EINVAL;
  837. error = CLOCK_DISPATCH(which_clock, clock_getres,
  838. (which_clock, &rtn_tp));
  839. if (!error && tp && copy_to_user(tp, &rtn_tp, sizeof (rtn_tp))) {
  840. error = -EFAULT;
  841. }
  842. return error;
  843. }
  844. /*
  845. * nanosleep for monotonic and realtime clocks
  846. */
  847. static int common_nsleep(const clockid_t which_clock, int flags,
  848. struct timespec *tsave, struct timespec __user *rmtp)
  849. {
  850. return hrtimer_nanosleep(tsave, rmtp, flags & TIMER_ABSTIME ?
  851. HRTIMER_MODE_ABS : HRTIMER_MODE_REL,
  852. which_clock);
  853. }
  854. asmlinkage long
  855. sys_clock_nanosleep(const clockid_t which_clock, int flags,
  856. const struct timespec __user *rqtp,
  857. struct timespec __user *rmtp)
  858. {
  859. struct timespec t;
  860. if (invalid_clockid(which_clock))
  861. return -EINVAL;
  862. if (copy_from_user(&t, rqtp, sizeof (struct timespec)))
  863. return -EFAULT;
  864. if (!timespec_valid(&t))
  865. return -EINVAL;
  866. return CLOCK_DISPATCH(which_clock, nsleep,
  867. (which_clock, flags, &t, rmtp));
  868. }
  869. /*
  870. * nanosleep_restart for monotonic and realtime clocks
  871. */
  872. static int common_nsleep_restart(struct restart_block *restart_block)
  873. {
  874. return hrtimer_nanosleep_restart(restart_block);
  875. }
  876. /*
  877. * This will restart clock_nanosleep. This is required only by
  878. * compat_clock_nanosleep_restart for now.
  879. */
  880. long
  881. clock_nanosleep_restart(struct restart_block *restart_block)
  882. {
  883. clockid_t which_clock = restart_block->arg0;
  884. return CLOCK_DISPATCH(which_clock, nsleep_restart,
  885. (restart_block));
  886. }