posix-timers.c 29 KB

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