posix-timers.c 29 KB

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