posix-timers.c 29 KB

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