posix-timers.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584
  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/smp_lock.h>
  33. #include <linux/interrupt.h>
  34. #include <linux/slab.h>
  35. #include <linux/time.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. #ifndef div_long_long_rem
  48. #include <asm/div64.h>
  49. #define div_long_long_rem(dividend,divisor,remainder) ({ \
  50. u64 result = dividend; \
  51. *remainder = do_div(result,divisor); \
  52. result; })
  53. #endif
  54. #define CLOCK_REALTIME_RES TICK_NSEC /* In nano seconds. */
  55. static inline u64 mpy_l_X_l_ll(unsigned long mpy1,unsigned long mpy2)
  56. {
  57. return (u64)mpy1 * mpy2;
  58. }
  59. /*
  60. * Management arrays for POSIX timers. Timers are kept in slab memory
  61. * Timer ids are allocated by an external routine that keeps track of the
  62. * id and the timer. The external interface is:
  63. *
  64. * void *idr_find(struct idr *idp, int id); to find timer_id <id>
  65. * int idr_get_new(struct idr *idp, void *ptr); to get a new id and
  66. * related it to <ptr>
  67. * void idr_remove(struct idr *idp, int id); to release <id>
  68. * void idr_init(struct idr *idp); to initialize <idp>
  69. * which we supply.
  70. * The idr_get_new *may* call slab for more memory so it must not be
  71. * called under a spin lock. Likewise idr_remore may release memory
  72. * (but it may be ok to do this under a lock...).
  73. * idr_find is just a memory look up and is quite fast. A -1 return
  74. * indicates that the requested id does not exist.
  75. */
  76. /*
  77. * Lets keep our timers in a slab cache :-)
  78. */
  79. static kmem_cache_t *posix_timers_cache;
  80. static struct idr posix_timers_id;
  81. static DEFINE_SPINLOCK(idr_lock);
  82. /*
  83. * Just because the timer is not in the timer list does NOT mean it is
  84. * inactive. It could be in the "fire" routine getting a new expire time.
  85. */
  86. #define TIMER_INACTIVE 1
  87. #ifdef CONFIG_SMP
  88. # define timer_active(tmr) \
  89. ((tmr)->it.real.timer.entry.prev != (void *)TIMER_INACTIVE)
  90. # define set_timer_inactive(tmr) \
  91. do { \
  92. (tmr)->it.real.timer.entry.prev = (void *)TIMER_INACTIVE; \
  93. } while (0)
  94. #else
  95. # define timer_active(tmr) BARFY // error to use outside of SMP
  96. # define set_timer_inactive(tmr) do { } while (0)
  97. #endif
  98. /*
  99. * we assume that the new SIGEV_THREAD_ID shares no bits with the other
  100. * SIGEV values. Here we put out an error if this assumption fails.
  101. */
  102. #if SIGEV_THREAD_ID != (SIGEV_THREAD_ID & \
  103. ~(SIGEV_SIGNAL | SIGEV_NONE | SIGEV_THREAD))
  104. #error "SIGEV_THREAD_ID must not share bit with other SIGEV values!"
  105. #endif
  106. /*
  107. * The timer ID is turned into a timer address by idr_find().
  108. * Verifying a valid ID consists of:
  109. *
  110. * a) checking that idr_find() returns other than -1.
  111. * b) checking that the timer id matches the one in the timer itself.
  112. * c) that the timer owner is in the callers thread group.
  113. */
  114. /*
  115. * CLOCKs: The POSIX standard calls for a couple of clocks and allows us
  116. * to implement others. This structure defines the various
  117. * clocks and allows the possibility of adding others. We
  118. * provide an interface to add clocks to the table and expect
  119. * the "arch" code to add at least one clock that is high
  120. * resolution. Here we define the standard CLOCK_REALTIME as a
  121. * 1/HZ resolution clock.
  122. *
  123. * RESOLUTION: Clock resolution is used to round up timer and interval
  124. * times, NOT to report clock times, which are reported with as
  125. * much resolution as the system can muster. In some cases this
  126. * resolution may depend on the underlying clock hardware and
  127. * may not be quantifiable until run time, and only then is the
  128. * necessary code is written. The standard says we should say
  129. * something about this issue in the documentation...
  130. *
  131. * FUNCTIONS: The CLOCKs structure defines possible functions to handle
  132. * various clock functions. For clocks that use the standard
  133. * system timer code these entries should be NULL. This will
  134. * allow dispatch without the overhead of indirect function
  135. * calls. CLOCKS that depend on other sources (e.g. WWV or GPS)
  136. * must supply functions here, even if the function just returns
  137. * ENOSYS. The standard POSIX timer management code assumes the
  138. * following: 1.) The k_itimer struct (sched.h) is used for the
  139. * timer. 2.) The list, it_lock, it_clock, it_id and it_process
  140. * fields are not modified by timer code.
  141. *
  142. * At this time all functions EXCEPT clock_nanosleep can be
  143. * redirected by the CLOCKS structure. Clock_nanosleep is in
  144. * there, but the code ignores it.
  145. *
  146. * Permissions: It is assumed that the clock_settime() function defined
  147. * for each clock will take care of permission checks. Some
  148. * clocks may be set able by any user (i.e. local process
  149. * clocks) others not. Currently the only set able clock we
  150. * have is CLOCK_REALTIME and its high res counter part, both of
  151. * which we beg off on and pass to do_sys_settimeofday().
  152. */
  153. static struct k_clock posix_clocks[MAX_CLOCKS];
  154. /*
  155. * We only have one real clock that can be set so we need only one abs list,
  156. * even if we should want to have several clocks with differing resolutions.
  157. */
  158. static struct k_clock_abs abs_list = {.list = LIST_HEAD_INIT(abs_list.list),
  159. .lock = SPIN_LOCK_UNLOCKED};
  160. static void posix_timer_fn(unsigned long);
  161. static u64 do_posix_clock_monotonic_gettime_parts(
  162. struct timespec *tp, struct timespec *mo);
  163. int do_posix_clock_monotonic_gettime(struct timespec *tp);
  164. static int do_posix_clock_monotonic_get(clockid_t, struct timespec *tp);
  165. static struct k_itimer *lock_timer(timer_t timer_id, unsigned long *flags);
  166. static inline void unlock_timer(struct k_itimer *timr, unsigned long flags)
  167. {
  168. spin_unlock_irqrestore(&timr->it_lock, flags);
  169. }
  170. /*
  171. * Call the k_clock hook function if non-null, or the default function.
  172. */
  173. #define CLOCK_DISPATCH(clock, call, arglist) \
  174. ((clock) < 0 ? posix_cpu_##call arglist : \
  175. (posix_clocks[clock].call != NULL \
  176. ? (*posix_clocks[clock].call) arglist : common_##call arglist))
  177. /*
  178. * Default clock hook functions when the struct k_clock passed
  179. * to register_posix_clock leaves a function pointer null.
  180. *
  181. * The function common_CALL is the default implementation for
  182. * the function pointer CALL in struct k_clock.
  183. */
  184. static inline int common_clock_getres(clockid_t which_clock,
  185. struct timespec *tp)
  186. {
  187. tp->tv_sec = 0;
  188. tp->tv_nsec = posix_clocks[which_clock].res;
  189. return 0;
  190. }
  191. static inline int common_clock_get(clockid_t which_clock, struct timespec *tp)
  192. {
  193. getnstimeofday(tp);
  194. return 0;
  195. }
  196. static inline int common_clock_set(clockid_t which_clock, struct timespec *tp)
  197. {
  198. return do_sys_settimeofday(tp, NULL);
  199. }
  200. static inline int common_timer_create(struct k_itimer *new_timer)
  201. {
  202. INIT_LIST_HEAD(&new_timer->it.real.abs_timer_entry);
  203. init_timer(&new_timer->it.real.timer);
  204. new_timer->it.real.timer.data = (unsigned long) new_timer;
  205. new_timer->it.real.timer.function = posix_timer_fn;
  206. set_timer_inactive(new_timer);
  207. return 0;
  208. }
  209. /*
  210. * These ones are defined below.
  211. */
  212. static int common_nsleep(clockid_t, int flags, struct timespec *t);
  213. static void common_timer_get(struct k_itimer *, struct itimerspec *);
  214. static int common_timer_set(struct k_itimer *, int,
  215. struct itimerspec *, struct itimerspec *);
  216. static int common_timer_del(struct k_itimer *timer);
  217. /*
  218. * Return nonzero iff we know a priori this clockid_t value is bogus.
  219. */
  220. static inline int invalid_clockid(clockid_t which_clock)
  221. {
  222. if (which_clock < 0) /* CPU clock, posix_cpu_* will check it */
  223. return 0;
  224. if ((unsigned) which_clock >= MAX_CLOCKS)
  225. return 1;
  226. if (posix_clocks[which_clock].clock_getres != NULL)
  227. return 0;
  228. #ifndef CLOCK_DISPATCH_DIRECT
  229. if (posix_clocks[which_clock].res != 0)
  230. return 0;
  231. #endif
  232. return 1;
  233. }
  234. /*
  235. * Initialize everything, well, just everything in Posix clocks/timers ;)
  236. */
  237. static __init int init_posix_timers(void)
  238. {
  239. struct k_clock clock_realtime = {.res = CLOCK_REALTIME_RES,
  240. .abs_struct = &abs_list
  241. };
  242. struct k_clock clock_monotonic = {.res = CLOCK_REALTIME_RES,
  243. .abs_struct = NULL,
  244. .clock_get = do_posix_clock_monotonic_get,
  245. .clock_set = do_posix_clock_nosettime
  246. };
  247. register_posix_clock(CLOCK_REALTIME, &clock_realtime);
  248. register_posix_clock(CLOCK_MONOTONIC, &clock_monotonic);
  249. posix_timers_cache = kmem_cache_create("posix_timers_cache",
  250. sizeof (struct k_itimer), 0, 0, NULL, NULL);
  251. idr_init(&posix_timers_id);
  252. return 0;
  253. }
  254. __initcall(init_posix_timers);
  255. static void tstojiffie(struct timespec *tp, int res, u64 *jiff)
  256. {
  257. long sec = tp->tv_sec;
  258. long nsec = tp->tv_nsec + res - 1;
  259. if (nsec > NSEC_PER_SEC) {
  260. sec++;
  261. nsec -= NSEC_PER_SEC;
  262. }
  263. /*
  264. * The scaling constants are defined in <linux/time.h>
  265. * The difference between there and here is that we do the
  266. * res rounding and compute a 64-bit result (well so does that
  267. * but it then throws away the high bits).
  268. */
  269. *jiff = (mpy_l_X_l_ll(sec, SEC_CONVERSION) +
  270. (mpy_l_X_l_ll(nsec, NSEC_CONVERSION) >>
  271. (NSEC_JIFFIE_SC - SEC_JIFFIE_SC))) >> SEC_JIFFIE_SC;
  272. }
  273. /*
  274. * This function adjusts the timer as needed as a result of the clock
  275. * being set. It should only be called for absolute timers, and then
  276. * under the abs_list lock. It computes the time difference and sets
  277. * the new jiffies value in the timer. It also updates the timers
  278. * reference wall_to_monotonic value. It is complicated by the fact
  279. * that tstojiffies() only handles positive times and it needs to work
  280. * with both positive and negative times. Also, for negative offsets,
  281. * we need to defeat the res round up.
  282. *
  283. * Return is true if there is a new time, else false.
  284. */
  285. static long add_clockset_delta(struct k_itimer *timr,
  286. struct timespec *new_wall_to)
  287. {
  288. struct timespec delta;
  289. int sign = 0;
  290. u64 exp;
  291. set_normalized_timespec(&delta,
  292. new_wall_to->tv_sec -
  293. timr->it.real.wall_to_prev.tv_sec,
  294. new_wall_to->tv_nsec -
  295. timr->it.real.wall_to_prev.tv_nsec);
  296. if (likely(!(delta.tv_sec | delta.tv_nsec)))
  297. return 0;
  298. if (delta.tv_sec < 0) {
  299. set_normalized_timespec(&delta,
  300. -delta.tv_sec,
  301. 1 - delta.tv_nsec -
  302. posix_clocks[timr->it_clock].res);
  303. sign++;
  304. }
  305. tstojiffie(&delta, posix_clocks[timr->it_clock].res, &exp);
  306. timr->it.real.wall_to_prev = *new_wall_to;
  307. timr->it.real.timer.expires += (sign ? -exp : exp);
  308. return 1;
  309. }
  310. static void remove_from_abslist(struct k_itimer *timr)
  311. {
  312. if (!list_empty(&timr->it.real.abs_timer_entry)) {
  313. spin_lock(&abs_list.lock);
  314. list_del_init(&timr->it.real.abs_timer_entry);
  315. spin_unlock(&abs_list.lock);
  316. }
  317. }
  318. static void schedule_next_timer(struct k_itimer *timr)
  319. {
  320. struct timespec new_wall_to;
  321. struct now_struct now;
  322. unsigned long seq;
  323. /*
  324. * Set up the timer for the next interval (if there is one).
  325. * Note: this code uses the abs_timer_lock to protect
  326. * it.real.wall_to_prev and must hold it until exp is set, not exactly
  327. * obvious...
  328. * This function is used for CLOCK_REALTIME* and
  329. * CLOCK_MONOTONIC* timers. If we ever want to handle other
  330. * CLOCKs, the calling code (do_schedule_next_timer) would need
  331. * to pull the "clock" info from the timer and dispatch the
  332. * "other" CLOCKs "next timer" code (which, I suppose should
  333. * also be added to the k_clock structure).
  334. */
  335. if (!timr->it.real.incr)
  336. return;
  337. do {
  338. seq = read_seqbegin(&xtime_lock);
  339. new_wall_to = wall_to_monotonic;
  340. posix_get_now(&now);
  341. } while (read_seqretry(&xtime_lock, seq));
  342. if (!list_empty(&timr->it.real.abs_timer_entry)) {
  343. spin_lock(&abs_list.lock);
  344. add_clockset_delta(timr, &new_wall_to);
  345. posix_bump_timer(timr, now);
  346. spin_unlock(&abs_list.lock);
  347. } else {
  348. posix_bump_timer(timr, now);
  349. }
  350. timr->it_overrun_last = timr->it_overrun;
  351. timr->it_overrun = -1;
  352. ++timr->it_requeue_pending;
  353. add_timer(&timr->it.real.timer);
  354. }
  355. /*
  356. * This function is exported for use by the signal deliver code. It is
  357. * called just prior to the info block being released and passes that
  358. * block to us. It's function is to update the overrun entry AND to
  359. * restart the timer. It should only be called if the timer is to be
  360. * restarted (i.e. we have flagged this in the sys_private entry of the
  361. * info block).
  362. *
  363. * To protect aginst the timer going away while the interrupt is queued,
  364. * we require that the it_requeue_pending flag be set.
  365. */
  366. void do_schedule_next_timer(struct siginfo *info)
  367. {
  368. struct k_itimer *timr;
  369. unsigned long flags;
  370. timr = lock_timer(info->si_tid, &flags);
  371. if (!timr || timr->it_requeue_pending != info->si_sys_private)
  372. goto exit;
  373. if (timr->it_clock < 0) /* CPU clock */
  374. posix_cpu_timer_schedule(timr);
  375. else
  376. schedule_next_timer(timr);
  377. info->si_overrun = timr->it_overrun_last;
  378. exit:
  379. if (timr)
  380. unlock_timer(timr, flags);
  381. }
  382. int posix_timer_event(struct k_itimer *timr,int si_private)
  383. {
  384. memset(&timr->sigq->info, 0, sizeof(siginfo_t));
  385. timr->sigq->info.si_sys_private = si_private;
  386. /*
  387. * Send signal to the process that owns this timer.
  388. * This code assumes that all the possible abs_lists share the
  389. * same lock (there is only one list at this time). If this is
  390. * not the case, the CLOCK info would need to be used to find
  391. * the proper abs list lock.
  392. */
  393. timr->sigq->info.si_signo = timr->it_sigev_signo;
  394. timr->sigq->info.si_errno = 0;
  395. timr->sigq->info.si_code = SI_TIMER;
  396. timr->sigq->info.si_tid = timr->it_id;
  397. timr->sigq->info.si_value = timr->it_sigev_value;
  398. if (timr->it_sigev_notify & SIGEV_THREAD_ID) {
  399. if (unlikely(timr->it_process->flags & PF_EXITING)) {
  400. timr->it_sigev_notify = SIGEV_SIGNAL;
  401. put_task_struct(timr->it_process);
  402. timr->it_process = timr->it_process->group_leader;
  403. goto group;
  404. }
  405. return send_sigqueue(timr->it_sigev_signo, timr->sigq,
  406. timr->it_process);
  407. }
  408. else {
  409. group:
  410. return send_group_sigqueue(timr->it_sigev_signo, timr->sigq,
  411. timr->it_process);
  412. }
  413. }
  414. EXPORT_SYMBOL_GPL(posix_timer_event);
  415. /*
  416. * This function gets called when a POSIX.1b interval timer expires. It
  417. * is used as a callback from the kernel internal timer. The
  418. * run_timer_list code ALWAYS calls with interrupts on.
  419. * This code is for CLOCK_REALTIME* and CLOCK_MONOTONIC* timers.
  420. */
  421. static void posix_timer_fn(unsigned long __data)
  422. {
  423. struct k_itimer *timr = (struct k_itimer *) __data;
  424. unsigned long flags;
  425. unsigned long seq;
  426. struct timespec delta, new_wall_to;
  427. u64 exp = 0;
  428. int do_notify = 1;
  429. spin_lock_irqsave(&timr->it_lock, flags);
  430. set_timer_inactive(timr);
  431. if (!list_empty(&timr->it.real.abs_timer_entry)) {
  432. spin_lock(&abs_list.lock);
  433. do {
  434. seq = read_seqbegin(&xtime_lock);
  435. new_wall_to = wall_to_monotonic;
  436. } while (read_seqretry(&xtime_lock, seq));
  437. set_normalized_timespec(&delta,
  438. new_wall_to.tv_sec -
  439. timr->it.real.wall_to_prev.tv_sec,
  440. new_wall_to.tv_nsec -
  441. timr->it.real.wall_to_prev.tv_nsec);
  442. if (likely((delta.tv_sec | delta.tv_nsec ) == 0)) {
  443. /* do nothing, timer is on time */
  444. } else if (delta.tv_sec < 0) {
  445. /* do nothing, timer is already late */
  446. } else {
  447. /* timer is early due to a clock set */
  448. tstojiffie(&delta,
  449. posix_clocks[timr->it_clock].res,
  450. &exp);
  451. timr->it.real.wall_to_prev = new_wall_to;
  452. timr->it.real.timer.expires += exp;
  453. add_timer(&timr->it.real.timer);
  454. do_notify = 0;
  455. }
  456. spin_unlock(&abs_list.lock);
  457. }
  458. if (do_notify) {
  459. int si_private=0;
  460. if (timr->it.real.incr)
  461. si_private = ++timr->it_requeue_pending;
  462. else {
  463. remove_from_abslist(timr);
  464. }
  465. if (posix_timer_event(timr, si_private))
  466. /*
  467. * signal was not sent because of sig_ignor
  468. * we will not get a call back to restart it AND
  469. * it should be restarted.
  470. */
  471. schedule_next_timer(timr);
  472. }
  473. unlock_timer(timr, flags); /* hold thru abs lock to keep irq off */
  474. }
  475. static inline struct task_struct * good_sigevent(sigevent_t * event)
  476. {
  477. struct task_struct *rtn = current->group_leader;
  478. if ((event->sigev_notify & SIGEV_THREAD_ID ) &&
  479. (!(rtn = find_task_by_pid(event->sigev_notify_thread_id)) ||
  480. rtn->tgid != current->tgid ||
  481. (event->sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_SIGNAL))
  482. return NULL;
  483. if (((event->sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE) &&
  484. ((event->sigev_signo <= 0) || (event->sigev_signo > SIGRTMAX)))
  485. return NULL;
  486. return rtn;
  487. }
  488. void register_posix_clock(clockid_t clock_id, struct k_clock *new_clock)
  489. {
  490. if ((unsigned) clock_id >= MAX_CLOCKS) {
  491. printk("POSIX clock register failed for clock_id %d\n",
  492. clock_id);
  493. return;
  494. }
  495. posix_clocks[clock_id] = *new_clock;
  496. }
  497. EXPORT_SYMBOL_GPL(register_posix_clock);
  498. static struct k_itimer * alloc_posix_timer(void)
  499. {
  500. struct k_itimer *tmr;
  501. tmr = kmem_cache_alloc(posix_timers_cache, GFP_KERNEL);
  502. if (!tmr)
  503. return tmr;
  504. memset(tmr, 0, sizeof (struct k_itimer));
  505. if (unlikely(!(tmr->sigq = sigqueue_alloc()))) {
  506. kmem_cache_free(posix_timers_cache, tmr);
  507. tmr = NULL;
  508. }
  509. return tmr;
  510. }
  511. #define IT_ID_SET 1
  512. #define IT_ID_NOT_SET 0
  513. static void release_posix_timer(struct k_itimer *tmr, int it_id_set)
  514. {
  515. if (it_id_set) {
  516. unsigned long flags;
  517. spin_lock_irqsave(&idr_lock, flags);
  518. idr_remove(&posix_timers_id, tmr->it_id);
  519. spin_unlock_irqrestore(&idr_lock, flags);
  520. }
  521. sigqueue_free(tmr->sigq);
  522. if (unlikely(tmr->it_process) &&
  523. tmr->it_sigev_notify == (SIGEV_SIGNAL|SIGEV_THREAD_ID))
  524. put_task_struct(tmr->it_process);
  525. kmem_cache_free(posix_timers_cache, tmr);
  526. }
  527. /* Create a POSIX.1b interval timer. */
  528. asmlinkage long
  529. sys_timer_create(clockid_t which_clock,
  530. struct sigevent __user *timer_event_spec,
  531. timer_t __user * created_timer_id)
  532. {
  533. int error = 0;
  534. struct k_itimer *new_timer = NULL;
  535. int new_timer_id;
  536. struct task_struct *process = NULL;
  537. unsigned long flags;
  538. sigevent_t event;
  539. int it_id_set = IT_ID_NOT_SET;
  540. if (invalid_clockid(which_clock))
  541. return -EINVAL;
  542. new_timer = alloc_posix_timer();
  543. if (unlikely(!new_timer))
  544. return -EAGAIN;
  545. spin_lock_init(&new_timer->it_lock);
  546. retry:
  547. if (unlikely(!idr_pre_get(&posix_timers_id, GFP_KERNEL))) {
  548. error = -EAGAIN;
  549. goto out;
  550. }
  551. spin_lock_irq(&idr_lock);
  552. error = idr_get_new(&posix_timers_id,
  553. (void *) new_timer,
  554. &new_timer_id);
  555. spin_unlock_irq(&idr_lock);
  556. if (error == -EAGAIN)
  557. goto retry;
  558. else if (error) {
  559. /*
  560. * Wierd looking, but we return EAGAIN if the IDR is
  561. * full (proper POSIX return value for this)
  562. */
  563. error = -EAGAIN;
  564. goto out;
  565. }
  566. it_id_set = IT_ID_SET;
  567. new_timer->it_id = (timer_t) new_timer_id;
  568. new_timer->it_clock = which_clock;
  569. new_timer->it_overrun = -1;
  570. error = CLOCK_DISPATCH(which_clock, timer_create, (new_timer));
  571. if (error)
  572. goto out;
  573. /*
  574. * return the timer_id now. The next step is hard to
  575. * back out if there is an error.
  576. */
  577. if (copy_to_user(created_timer_id,
  578. &new_timer_id, sizeof (new_timer_id))) {
  579. error = -EFAULT;
  580. goto out;
  581. }
  582. if (timer_event_spec) {
  583. if (copy_from_user(&event, timer_event_spec, sizeof (event))) {
  584. error = -EFAULT;
  585. goto out;
  586. }
  587. new_timer->it_sigev_notify = event.sigev_notify;
  588. new_timer->it_sigev_signo = event.sigev_signo;
  589. new_timer->it_sigev_value = event.sigev_value;
  590. read_lock(&tasklist_lock);
  591. if ((process = good_sigevent(&event))) {
  592. /*
  593. * We may be setting up this process for another
  594. * thread. It may be exiting. To catch this
  595. * case the we check the PF_EXITING flag. If
  596. * the flag is not set, the siglock will catch
  597. * him before it is too late (in exit_itimers).
  598. *
  599. * The exec case is a bit more invloved but easy
  600. * to code. If the process is in our thread
  601. * group (and it must be or we would not allow
  602. * it here) and is doing an exec, it will cause
  603. * us to be killed. In this case it will wait
  604. * for us to die which means we can finish this
  605. * linkage with our last gasp. I.e. no code :)
  606. */
  607. spin_lock_irqsave(&process->sighand->siglock, flags);
  608. if (!(process->flags & PF_EXITING)) {
  609. new_timer->it_process = process;
  610. list_add(&new_timer->list,
  611. &process->signal->posix_timers);
  612. spin_unlock_irqrestore(&process->sighand->siglock, flags);
  613. if (new_timer->it_sigev_notify == (SIGEV_SIGNAL|SIGEV_THREAD_ID))
  614. get_task_struct(process);
  615. } else {
  616. spin_unlock_irqrestore(&process->sighand->siglock, flags);
  617. process = NULL;
  618. }
  619. }
  620. read_unlock(&tasklist_lock);
  621. if (!process) {
  622. error = -EINVAL;
  623. goto out;
  624. }
  625. } else {
  626. new_timer->it_sigev_notify = SIGEV_SIGNAL;
  627. new_timer->it_sigev_signo = SIGALRM;
  628. new_timer->it_sigev_value.sival_int = new_timer->it_id;
  629. process = current->group_leader;
  630. spin_lock_irqsave(&process->sighand->siglock, flags);
  631. new_timer->it_process = process;
  632. list_add(&new_timer->list, &process->signal->posix_timers);
  633. spin_unlock_irqrestore(&process->sighand->siglock, flags);
  634. }
  635. /*
  636. * In the case of the timer belonging to another task, after
  637. * the task is unlocked, the timer is owned by the other task
  638. * and may cease to exist at any time. Don't use or modify
  639. * new_timer after the unlock call.
  640. */
  641. out:
  642. if (error)
  643. release_posix_timer(new_timer, it_id_set);
  644. return error;
  645. }
  646. /*
  647. * good_timespec
  648. *
  649. * This function checks the elements of a timespec structure.
  650. *
  651. * Arguments:
  652. * ts : Pointer to the timespec structure to check
  653. *
  654. * Return value:
  655. * If a NULL pointer was passed in, or the tv_nsec field was less than 0
  656. * or greater than NSEC_PER_SEC, or the tv_sec field was less than 0,
  657. * this function returns 0. Otherwise it returns 1.
  658. */
  659. static int good_timespec(const struct timespec *ts)
  660. {
  661. if ((!ts) || (ts->tv_sec < 0) ||
  662. ((unsigned) ts->tv_nsec >= NSEC_PER_SEC))
  663. return 0;
  664. return 1;
  665. }
  666. /*
  667. * Locking issues: We need to protect the result of the id look up until
  668. * we get the timer locked down so it is not deleted under us. The
  669. * removal is done under the idr spinlock so we use that here to bridge
  670. * the find to the timer lock. To avoid a dead lock, the timer id MUST
  671. * be release with out holding the timer lock.
  672. */
  673. static struct k_itimer * lock_timer(timer_t timer_id, unsigned long *flags)
  674. {
  675. struct k_itimer *timr;
  676. /*
  677. * Watch out here. We do a irqsave on the idr_lock and pass the
  678. * flags part over to the timer lock. Must not let interrupts in
  679. * while we are moving the lock.
  680. */
  681. spin_lock_irqsave(&idr_lock, *flags);
  682. timr = (struct k_itimer *) idr_find(&posix_timers_id, (int) timer_id);
  683. if (timr) {
  684. spin_lock(&timr->it_lock);
  685. spin_unlock(&idr_lock);
  686. if ((timr->it_id != timer_id) || !(timr->it_process) ||
  687. timr->it_process->tgid != current->tgid) {
  688. unlock_timer(timr, *flags);
  689. timr = NULL;
  690. }
  691. } else
  692. spin_unlock_irqrestore(&idr_lock, *flags);
  693. return timr;
  694. }
  695. /*
  696. * Get the time remaining on a POSIX.1b interval timer. This function
  697. * is ALWAYS called with spin_lock_irq on the timer, thus it must not
  698. * mess with irq.
  699. *
  700. * We have a couple of messes to clean up here. First there is the case
  701. * of a timer that has a requeue pending. These timers should appear to
  702. * be in the timer list with an expiry as if we were to requeue them
  703. * now.
  704. *
  705. * The second issue is the SIGEV_NONE timer which may be active but is
  706. * not really ever put in the timer list (to save system resources).
  707. * This timer may be expired, and if so, we will do it here. Otherwise
  708. * it is the same as a requeue pending timer WRT to what we should
  709. * report.
  710. */
  711. static void
  712. common_timer_get(struct k_itimer *timr, struct itimerspec *cur_setting)
  713. {
  714. unsigned long expires;
  715. struct now_struct now;
  716. do
  717. expires = timr->it.real.timer.expires;
  718. while ((volatile long) (timr->it.real.timer.expires) != expires);
  719. posix_get_now(&now);
  720. if (expires &&
  721. ((timr->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE) &&
  722. !timr->it.real.incr &&
  723. posix_time_before(&timr->it.real.timer, &now))
  724. timr->it.real.timer.expires = expires = 0;
  725. if (expires) {
  726. if (timr->it_requeue_pending & REQUEUE_PENDING ||
  727. (timr->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE) {
  728. posix_bump_timer(timr, now);
  729. expires = timr->it.real.timer.expires;
  730. }
  731. else
  732. if (!timer_pending(&timr->it.real.timer))
  733. expires = 0;
  734. if (expires)
  735. expires -= now.jiffies;
  736. }
  737. jiffies_to_timespec(expires, &cur_setting->it_value);
  738. jiffies_to_timespec(timr->it.real.incr, &cur_setting->it_interval);
  739. if (cur_setting->it_value.tv_sec < 0) {
  740. cur_setting->it_value.tv_nsec = 1;
  741. cur_setting->it_value.tv_sec = 0;
  742. }
  743. }
  744. /* Get the time remaining on a POSIX.1b interval timer. */
  745. asmlinkage long
  746. sys_timer_gettime(timer_t timer_id, struct itimerspec __user *setting)
  747. {
  748. struct k_itimer *timr;
  749. struct itimerspec cur_setting;
  750. unsigned long flags;
  751. timr = lock_timer(timer_id, &flags);
  752. if (!timr)
  753. return -EINVAL;
  754. CLOCK_DISPATCH(timr->it_clock, timer_get, (timr, &cur_setting));
  755. unlock_timer(timr, flags);
  756. if (copy_to_user(setting, &cur_setting, sizeof (cur_setting)))
  757. return -EFAULT;
  758. return 0;
  759. }
  760. /*
  761. * Get the number of overruns of a POSIX.1b interval timer. This is to
  762. * be the overrun of the timer last delivered. At the same time we are
  763. * accumulating overruns on the next timer. The overrun is frozen when
  764. * the signal is delivered, either at the notify time (if the info block
  765. * is not queued) or at the actual delivery time (as we are informed by
  766. * the call back to do_schedule_next_timer(). So all we need to do is
  767. * to pick up the frozen overrun.
  768. */
  769. asmlinkage long
  770. sys_timer_getoverrun(timer_t timer_id)
  771. {
  772. struct k_itimer *timr;
  773. int overrun;
  774. long flags;
  775. timr = lock_timer(timer_id, &flags);
  776. if (!timr)
  777. return -EINVAL;
  778. overrun = timr->it_overrun_last;
  779. unlock_timer(timr, flags);
  780. return overrun;
  781. }
  782. /*
  783. * Adjust for absolute time
  784. *
  785. * If absolute time is given and it is not CLOCK_MONOTONIC, we need to
  786. * adjust for the offset between the timer clock (CLOCK_MONOTONIC) and
  787. * what ever clock he is using.
  788. *
  789. * If it is relative time, we need to add the current (CLOCK_MONOTONIC)
  790. * time to it to get the proper time for the timer.
  791. */
  792. static int adjust_abs_time(struct k_clock *clock, struct timespec *tp,
  793. int abs, u64 *exp, struct timespec *wall_to)
  794. {
  795. struct timespec now;
  796. struct timespec oc = *tp;
  797. u64 jiffies_64_f;
  798. int rtn =0;
  799. if (abs) {
  800. /*
  801. * The mask pick up the 4 basic clocks
  802. */
  803. if (!((clock - &posix_clocks[0]) & ~CLOCKS_MASK)) {
  804. jiffies_64_f = do_posix_clock_monotonic_gettime_parts(
  805. &now, wall_to);
  806. /*
  807. * If we are doing a MONOTONIC clock
  808. */
  809. if((clock - &posix_clocks[0]) & CLOCKS_MONO){
  810. now.tv_sec += wall_to->tv_sec;
  811. now.tv_nsec += wall_to->tv_nsec;
  812. }
  813. } else {
  814. /*
  815. * Not one of the basic clocks
  816. */
  817. clock->clock_get(clock - posix_clocks, &now);
  818. jiffies_64_f = get_jiffies_64();
  819. }
  820. /*
  821. * Take away now to get delta
  822. */
  823. oc.tv_sec -= now.tv_sec;
  824. oc.tv_nsec -= now.tv_nsec;
  825. /*
  826. * Normalize...
  827. */
  828. while ((oc.tv_nsec - NSEC_PER_SEC) >= 0) {
  829. oc.tv_nsec -= NSEC_PER_SEC;
  830. oc.tv_sec++;
  831. }
  832. while ((oc.tv_nsec) < 0) {
  833. oc.tv_nsec += NSEC_PER_SEC;
  834. oc.tv_sec--;
  835. }
  836. }else{
  837. jiffies_64_f = get_jiffies_64();
  838. }
  839. /*
  840. * Check if the requested time is prior to now (if so set now)
  841. */
  842. if (oc.tv_sec < 0)
  843. oc.tv_sec = oc.tv_nsec = 0;
  844. if (oc.tv_sec | oc.tv_nsec)
  845. set_normalized_timespec(&oc, oc.tv_sec,
  846. oc.tv_nsec + clock->res);
  847. tstojiffie(&oc, clock->res, exp);
  848. /*
  849. * Check if the requested time is more than the timer code
  850. * can handle (if so we error out but return the value too).
  851. */
  852. if (*exp > ((u64)MAX_JIFFY_OFFSET))
  853. /*
  854. * This is a considered response, not exactly in
  855. * line with the standard (in fact it is silent on
  856. * possible overflows). We assume such a large
  857. * value is ALMOST always a programming error and
  858. * try not to compound it by setting a really dumb
  859. * value.
  860. */
  861. rtn = -EINVAL;
  862. /*
  863. * return the actual jiffies expire time, full 64 bits
  864. */
  865. *exp += jiffies_64_f;
  866. return rtn;
  867. }
  868. /* Set a POSIX.1b interval timer. */
  869. /* timr->it_lock is taken. */
  870. static inline int
  871. common_timer_set(struct k_itimer *timr, int flags,
  872. struct itimerspec *new_setting, struct itimerspec *old_setting)
  873. {
  874. struct k_clock *clock = &posix_clocks[timr->it_clock];
  875. u64 expire_64;
  876. if (old_setting)
  877. common_timer_get(timr, old_setting);
  878. /* disable the timer */
  879. timr->it.real.incr = 0;
  880. /*
  881. * careful here. If smp we could be in the "fire" routine which will
  882. * be spinning as we hold the lock. But this is ONLY an SMP issue.
  883. */
  884. #ifdef CONFIG_SMP
  885. if (timer_active(timr) && !del_timer(&timr->it.real.timer))
  886. /*
  887. * It can only be active if on an other cpu. Since
  888. * we have cleared the interval stuff above, it should
  889. * clear once we release the spin lock. Of course once
  890. * we do that anything could happen, including the
  891. * complete melt down of the timer. So return with
  892. * a "retry" exit status.
  893. */
  894. return TIMER_RETRY;
  895. set_timer_inactive(timr);
  896. #else
  897. del_timer(&timr->it.real.timer);
  898. #endif
  899. remove_from_abslist(timr);
  900. timr->it_requeue_pending = (timr->it_requeue_pending + 2) &
  901. ~REQUEUE_PENDING;
  902. timr->it_overrun_last = 0;
  903. timr->it_overrun = -1;
  904. /*
  905. *switch off the timer when it_value is zero
  906. */
  907. if (!new_setting->it_value.tv_sec && !new_setting->it_value.tv_nsec) {
  908. timr->it.real.timer.expires = 0;
  909. return 0;
  910. }
  911. if (adjust_abs_time(clock,
  912. &new_setting->it_value, flags & TIMER_ABSTIME,
  913. &expire_64, &(timr->it.real.wall_to_prev))) {
  914. return -EINVAL;
  915. }
  916. timr->it.real.timer.expires = (unsigned long)expire_64;
  917. tstojiffie(&new_setting->it_interval, clock->res, &expire_64);
  918. timr->it.real.incr = (unsigned long)expire_64;
  919. /*
  920. * We do not even queue SIGEV_NONE timers! But we do put them
  921. * in the abs list so we can do that right.
  922. */
  923. if (((timr->it_sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE))
  924. add_timer(&timr->it.real.timer);
  925. if (flags & TIMER_ABSTIME && clock->abs_struct) {
  926. spin_lock(&clock->abs_struct->lock);
  927. list_add_tail(&(timr->it.real.abs_timer_entry),
  928. &(clock->abs_struct->list));
  929. spin_unlock(&clock->abs_struct->lock);
  930. }
  931. return 0;
  932. }
  933. /* Set a POSIX.1b interval timer */
  934. asmlinkage long
  935. sys_timer_settime(timer_t timer_id, int flags,
  936. const struct itimerspec __user *new_setting,
  937. struct itimerspec __user *old_setting)
  938. {
  939. struct k_itimer *timr;
  940. struct itimerspec new_spec, old_spec;
  941. int error = 0;
  942. long flag;
  943. struct itimerspec *rtn = old_setting ? &old_spec : NULL;
  944. if (!new_setting)
  945. return -EINVAL;
  946. if (copy_from_user(&new_spec, new_setting, sizeof (new_spec)))
  947. return -EFAULT;
  948. if ((!good_timespec(&new_spec.it_interval)) ||
  949. (!good_timespec(&new_spec.it_value)))
  950. return -EINVAL;
  951. retry:
  952. timr = lock_timer(timer_id, &flag);
  953. if (!timr)
  954. return -EINVAL;
  955. error = CLOCK_DISPATCH(timr->it_clock, timer_set,
  956. (timr, flags, &new_spec, rtn));
  957. unlock_timer(timr, flag);
  958. if (error == TIMER_RETRY) {
  959. rtn = NULL; // We already got the old time...
  960. goto retry;
  961. }
  962. if (old_setting && !error && copy_to_user(old_setting,
  963. &old_spec, sizeof (old_spec)))
  964. error = -EFAULT;
  965. return error;
  966. }
  967. static inline int common_timer_del(struct k_itimer *timer)
  968. {
  969. timer->it.real.incr = 0;
  970. #ifdef CONFIG_SMP
  971. if (timer_active(timer) && !del_timer(&timer->it.real.timer))
  972. /*
  973. * It can only be active if on an other cpu. Since
  974. * we have cleared the interval stuff above, it should
  975. * clear once we release the spin lock. Of course once
  976. * we do that anything could happen, including the
  977. * complete melt down of the timer. So return with
  978. * a "retry" exit status.
  979. */
  980. return TIMER_RETRY;
  981. #else
  982. del_timer(&timer->it.real.timer);
  983. #endif
  984. remove_from_abslist(timer);
  985. return 0;
  986. }
  987. static inline int timer_delete_hook(struct k_itimer *timer)
  988. {
  989. return CLOCK_DISPATCH(timer->it_clock, timer_del, (timer));
  990. }
  991. /* Delete a POSIX.1b interval timer. */
  992. asmlinkage long
  993. sys_timer_delete(timer_t timer_id)
  994. {
  995. struct k_itimer *timer;
  996. long flags;
  997. #ifdef CONFIG_SMP
  998. int error;
  999. retry_delete:
  1000. #endif
  1001. timer = lock_timer(timer_id, &flags);
  1002. if (!timer)
  1003. return -EINVAL;
  1004. #ifdef CONFIG_SMP
  1005. error = timer_delete_hook(timer);
  1006. if (error == TIMER_RETRY) {
  1007. unlock_timer(timer, flags);
  1008. goto retry_delete;
  1009. }
  1010. #else
  1011. timer_delete_hook(timer);
  1012. #endif
  1013. spin_lock(&current->sighand->siglock);
  1014. list_del(&timer->list);
  1015. spin_unlock(&current->sighand->siglock);
  1016. /*
  1017. * This keeps any tasks waiting on the spin lock from thinking
  1018. * they got something (see the lock code above).
  1019. */
  1020. if (timer->it_process) {
  1021. if (timer->it_sigev_notify == (SIGEV_SIGNAL|SIGEV_THREAD_ID))
  1022. put_task_struct(timer->it_process);
  1023. timer->it_process = NULL;
  1024. }
  1025. unlock_timer(timer, flags);
  1026. release_posix_timer(timer, IT_ID_SET);
  1027. return 0;
  1028. }
  1029. /*
  1030. * return timer owned by the process, used by exit_itimers
  1031. */
  1032. static inline void itimer_delete(struct k_itimer *timer)
  1033. {
  1034. unsigned long flags;
  1035. #ifdef CONFIG_SMP
  1036. int error;
  1037. retry_delete:
  1038. #endif
  1039. spin_lock_irqsave(&timer->it_lock, flags);
  1040. #ifdef CONFIG_SMP
  1041. error = timer_delete_hook(timer);
  1042. if (error == TIMER_RETRY) {
  1043. unlock_timer(timer, flags);
  1044. goto retry_delete;
  1045. }
  1046. #else
  1047. timer_delete_hook(timer);
  1048. #endif
  1049. list_del(&timer->list);
  1050. /*
  1051. * This keeps any tasks waiting on the spin lock from thinking
  1052. * they got something (see the lock code above).
  1053. */
  1054. if (timer->it_process) {
  1055. if (timer->it_sigev_notify == (SIGEV_SIGNAL|SIGEV_THREAD_ID))
  1056. put_task_struct(timer->it_process);
  1057. timer->it_process = NULL;
  1058. }
  1059. unlock_timer(timer, flags);
  1060. release_posix_timer(timer, IT_ID_SET);
  1061. }
  1062. /*
  1063. * This is called by __exit_signal, only when there are no more
  1064. * references to the shared signal_struct.
  1065. */
  1066. void exit_itimers(struct signal_struct *sig)
  1067. {
  1068. struct k_itimer *tmr;
  1069. while (!list_empty(&sig->posix_timers)) {
  1070. tmr = list_entry(sig->posix_timers.next, struct k_itimer, list);
  1071. itimer_delete(tmr);
  1072. }
  1073. }
  1074. /*
  1075. * And now for the "clock" calls
  1076. *
  1077. * These functions are called both from timer functions (with the timer
  1078. * spin_lock_irq() held and from clock calls with no locking. They must
  1079. * use the save flags versions of locks.
  1080. */
  1081. /*
  1082. * We do ticks here to avoid the irq lock ( they take sooo long).
  1083. * The seqlock is great here. Since we a reader, we don't really care
  1084. * if we are interrupted since we don't take lock that will stall us or
  1085. * any other cpu. Voila, no irq lock is needed.
  1086. *
  1087. */
  1088. static u64 do_posix_clock_monotonic_gettime_parts(
  1089. struct timespec *tp, struct timespec *mo)
  1090. {
  1091. u64 jiff;
  1092. unsigned int seq;
  1093. do {
  1094. seq = read_seqbegin(&xtime_lock);
  1095. getnstimeofday(tp);
  1096. *mo = wall_to_monotonic;
  1097. jiff = jiffies_64;
  1098. } while(read_seqretry(&xtime_lock, seq));
  1099. return jiff;
  1100. }
  1101. static int do_posix_clock_monotonic_get(clockid_t clock, struct timespec *tp)
  1102. {
  1103. struct timespec wall_to_mono;
  1104. do_posix_clock_monotonic_gettime_parts(tp, &wall_to_mono);
  1105. tp->tv_sec += wall_to_mono.tv_sec;
  1106. tp->tv_nsec += wall_to_mono.tv_nsec;
  1107. if ((tp->tv_nsec - NSEC_PER_SEC) > 0) {
  1108. tp->tv_nsec -= NSEC_PER_SEC;
  1109. tp->tv_sec++;
  1110. }
  1111. return 0;
  1112. }
  1113. int do_posix_clock_monotonic_gettime(struct timespec *tp)
  1114. {
  1115. return do_posix_clock_monotonic_get(CLOCK_MONOTONIC, tp);
  1116. }
  1117. int do_posix_clock_nosettime(clockid_t clockid, struct timespec *tp)
  1118. {
  1119. return -EINVAL;
  1120. }
  1121. EXPORT_SYMBOL_GPL(do_posix_clock_nosettime);
  1122. int do_posix_clock_notimer_create(struct k_itimer *timer)
  1123. {
  1124. return -EINVAL;
  1125. }
  1126. EXPORT_SYMBOL_GPL(do_posix_clock_notimer_create);
  1127. int do_posix_clock_nonanosleep(clockid_t clock, int flags, struct timespec *t)
  1128. {
  1129. #ifndef ENOTSUP
  1130. return -EOPNOTSUPP; /* aka ENOTSUP in userland for POSIX */
  1131. #else /* parisc does define it separately. */
  1132. return -ENOTSUP;
  1133. #endif
  1134. }
  1135. EXPORT_SYMBOL_GPL(do_posix_clock_nonanosleep);
  1136. asmlinkage long
  1137. sys_clock_settime(clockid_t which_clock, const struct timespec __user *tp)
  1138. {
  1139. struct timespec new_tp;
  1140. if (invalid_clockid(which_clock))
  1141. return -EINVAL;
  1142. if (copy_from_user(&new_tp, tp, sizeof (*tp)))
  1143. return -EFAULT;
  1144. return CLOCK_DISPATCH(which_clock, clock_set, (which_clock, &new_tp));
  1145. }
  1146. asmlinkage long
  1147. sys_clock_gettime(clockid_t which_clock, struct timespec __user *tp)
  1148. {
  1149. struct timespec kernel_tp;
  1150. int error;
  1151. if (invalid_clockid(which_clock))
  1152. return -EINVAL;
  1153. error = CLOCK_DISPATCH(which_clock, clock_get,
  1154. (which_clock, &kernel_tp));
  1155. if (!error && copy_to_user(tp, &kernel_tp, sizeof (kernel_tp)))
  1156. error = -EFAULT;
  1157. return error;
  1158. }
  1159. asmlinkage long
  1160. sys_clock_getres(clockid_t which_clock, struct timespec __user *tp)
  1161. {
  1162. struct timespec rtn_tp;
  1163. int error;
  1164. if (invalid_clockid(which_clock))
  1165. return -EINVAL;
  1166. error = CLOCK_DISPATCH(which_clock, clock_getres,
  1167. (which_clock, &rtn_tp));
  1168. if (!error && tp && copy_to_user(tp, &rtn_tp, sizeof (rtn_tp))) {
  1169. error = -EFAULT;
  1170. }
  1171. return error;
  1172. }
  1173. static void nanosleep_wake_up(unsigned long __data)
  1174. {
  1175. struct task_struct *p = (struct task_struct *) __data;
  1176. wake_up_process(p);
  1177. }
  1178. /*
  1179. * The standard says that an absolute nanosleep call MUST wake up at
  1180. * the requested time in spite of clock settings. Here is what we do:
  1181. * For each nanosleep call that needs it (only absolute and not on
  1182. * CLOCK_MONOTONIC* (as it can not be set)) we thread a little structure
  1183. * into the "nanosleep_abs_list". All we need is the task_struct pointer.
  1184. * When ever the clock is set we just wake up all those tasks. The rest
  1185. * is done by the while loop in clock_nanosleep().
  1186. *
  1187. * On locking, clock_was_set() is called from update_wall_clock which
  1188. * holds (or has held for it) a write_lock_irq( xtime_lock) and is
  1189. * called from the timer bh code. Thus we need the irq save locks.
  1190. *
  1191. * Also, on the call from update_wall_clock, that is done as part of a
  1192. * softirq thing. We don't want to delay the system that much (possibly
  1193. * long list of timers to fix), so we defer that work to keventd.
  1194. */
  1195. static DECLARE_WAIT_QUEUE_HEAD(nanosleep_abs_wqueue);
  1196. static DECLARE_WORK(clock_was_set_work, (void(*)(void*))clock_was_set, NULL);
  1197. static DECLARE_MUTEX(clock_was_set_lock);
  1198. void clock_was_set(void)
  1199. {
  1200. struct k_itimer *timr;
  1201. struct timespec new_wall_to;
  1202. LIST_HEAD(cws_list);
  1203. unsigned long seq;
  1204. if (unlikely(in_interrupt())) {
  1205. schedule_work(&clock_was_set_work);
  1206. return;
  1207. }
  1208. wake_up_all(&nanosleep_abs_wqueue);
  1209. /*
  1210. * Check if there exist TIMER_ABSTIME timers to correct.
  1211. *
  1212. * Notes on locking: This code is run in task context with irq
  1213. * on. We CAN be interrupted! All other usage of the abs list
  1214. * lock is under the timer lock which holds the irq lock as
  1215. * well. We REALLY don't want to scan the whole list with the
  1216. * interrupt system off, AND we would like a sequence lock on
  1217. * this code as well. Since we assume that the clock will not
  1218. * be set often, it seems ok to take and release the irq lock
  1219. * for each timer. In fact add_timer will do this, so this is
  1220. * not an issue. So we know when we are done, we will move the
  1221. * whole list to a new location. Then as we process each entry,
  1222. * we will move it to the actual list again. This way, when our
  1223. * copy is empty, we are done. We are not all that concerned
  1224. * about preemption so we will use a semaphore lock to protect
  1225. * aginst reentry. This way we will not stall another
  1226. * processor. It is possible that this may delay some timers
  1227. * that should have expired, given the new clock, but even this
  1228. * will be minimal as we will always update to the current time,
  1229. * even if it was set by a task that is waiting for entry to
  1230. * this code. Timers that expire too early will be caught by
  1231. * the expire code and restarted.
  1232. * Absolute timers that repeat are left in the abs list while
  1233. * waiting for the task to pick up the signal. This means we
  1234. * may find timers that are not in the "add_timer" list, but are
  1235. * in the abs list. We do the same thing for these, save
  1236. * putting them back in the "add_timer" list. (Note, these are
  1237. * left in the abs list mainly to indicate that they are
  1238. * ABSOLUTE timers, a fact that is used by the re-arm code, and
  1239. * for which we have no other flag.)
  1240. */
  1241. down(&clock_was_set_lock);
  1242. spin_lock_irq(&abs_list.lock);
  1243. list_splice_init(&abs_list.list, &cws_list);
  1244. spin_unlock_irq(&abs_list.lock);
  1245. do {
  1246. do {
  1247. seq = read_seqbegin(&xtime_lock);
  1248. new_wall_to = wall_to_monotonic;
  1249. } while (read_seqretry(&xtime_lock, seq));
  1250. spin_lock_irq(&abs_list.lock);
  1251. if (list_empty(&cws_list)) {
  1252. spin_unlock_irq(&abs_list.lock);
  1253. break;
  1254. }
  1255. timr = list_entry(cws_list.next, struct k_itimer,
  1256. it.real.abs_timer_entry);
  1257. list_del_init(&timr->it.real.abs_timer_entry);
  1258. if (add_clockset_delta(timr, &new_wall_to) &&
  1259. del_timer(&timr->it.real.timer)) /* timer run yet? */
  1260. add_timer(&timr->it.real.timer);
  1261. list_add(&timr->it.real.abs_timer_entry, &abs_list.list);
  1262. spin_unlock_irq(&abs_list.lock);
  1263. } while (1);
  1264. up(&clock_was_set_lock);
  1265. }
  1266. long clock_nanosleep_restart(struct restart_block *restart_block);
  1267. asmlinkage long
  1268. sys_clock_nanosleep(clockid_t which_clock, int flags,
  1269. const struct timespec __user *rqtp,
  1270. struct timespec __user *rmtp)
  1271. {
  1272. struct timespec t;
  1273. struct restart_block *restart_block =
  1274. &(current_thread_info()->restart_block);
  1275. int ret;
  1276. if (invalid_clockid(which_clock))
  1277. return -EINVAL;
  1278. if (copy_from_user(&t, rqtp, sizeof (struct timespec)))
  1279. return -EFAULT;
  1280. if ((unsigned) t.tv_nsec >= NSEC_PER_SEC || t.tv_sec < 0)
  1281. return -EINVAL;
  1282. /*
  1283. * Do this here as nsleep function does not have the real address.
  1284. */
  1285. restart_block->arg1 = (unsigned long)rmtp;
  1286. ret = CLOCK_DISPATCH(which_clock, nsleep, (which_clock, flags, &t));
  1287. if ((ret == -ERESTART_RESTARTBLOCK) && rmtp &&
  1288. copy_to_user(rmtp, &t, sizeof (t)))
  1289. return -EFAULT;
  1290. return ret;
  1291. }
  1292. static int common_nsleep(clockid_t which_clock,
  1293. int flags, struct timespec *tsave)
  1294. {
  1295. struct timespec t, dum;
  1296. struct timer_list new_timer;
  1297. DECLARE_WAITQUEUE(abs_wqueue, current);
  1298. u64 rq_time = (u64)0;
  1299. s64 left;
  1300. int abs;
  1301. struct restart_block *restart_block =
  1302. &current_thread_info()->restart_block;
  1303. abs_wqueue.flags = 0;
  1304. init_timer(&new_timer);
  1305. new_timer.expires = 0;
  1306. new_timer.data = (unsigned long) current;
  1307. new_timer.function = nanosleep_wake_up;
  1308. abs = flags & TIMER_ABSTIME;
  1309. if (restart_block->fn == clock_nanosleep_restart) {
  1310. /*
  1311. * Interrupted by a non-delivered signal, pick up remaining
  1312. * time and continue. Remaining time is in arg2 & 3.
  1313. */
  1314. restart_block->fn = do_no_restart_syscall;
  1315. rq_time = restart_block->arg3;
  1316. rq_time = (rq_time << 32) + restart_block->arg2;
  1317. if (!rq_time)
  1318. return -EINTR;
  1319. left = rq_time - get_jiffies_64();
  1320. if (left <= (s64)0)
  1321. return 0; /* Already passed */
  1322. }
  1323. if (abs && (posix_clocks[which_clock].clock_get !=
  1324. posix_clocks[CLOCK_MONOTONIC].clock_get))
  1325. add_wait_queue(&nanosleep_abs_wqueue, &abs_wqueue);
  1326. do {
  1327. t = *tsave;
  1328. if (abs || !rq_time) {
  1329. adjust_abs_time(&posix_clocks[which_clock], &t, abs,
  1330. &rq_time, &dum);
  1331. }
  1332. left = rq_time - get_jiffies_64();
  1333. if (left >= (s64)MAX_JIFFY_OFFSET)
  1334. left = (s64)MAX_JIFFY_OFFSET;
  1335. if (left < (s64)0)
  1336. break;
  1337. new_timer.expires = jiffies + left;
  1338. __set_current_state(TASK_INTERRUPTIBLE);
  1339. add_timer(&new_timer);
  1340. schedule();
  1341. del_timer_sync(&new_timer);
  1342. left = rq_time - get_jiffies_64();
  1343. } while (left > (s64)0 && !test_thread_flag(TIF_SIGPENDING));
  1344. if (abs_wqueue.task_list.next)
  1345. finish_wait(&nanosleep_abs_wqueue, &abs_wqueue);
  1346. if (left > (s64)0) {
  1347. /*
  1348. * Always restart abs calls from scratch to pick up any
  1349. * clock shifting that happened while we are away.
  1350. */
  1351. if (abs)
  1352. return -ERESTARTNOHAND;
  1353. left *= TICK_NSEC;
  1354. tsave->tv_sec = div_long_long_rem(left,
  1355. NSEC_PER_SEC,
  1356. &tsave->tv_nsec);
  1357. /*
  1358. * Restart works by saving the time remaing in
  1359. * arg2 & 3 (it is 64-bits of jiffies). The other
  1360. * info we need is the clock_id (saved in arg0).
  1361. * The sys_call interface needs the users
  1362. * timespec return address which _it_ saves in arg1.
  1363. * Since we have cast the nanosleep call to a clock_nanosleep
  1364. * both can be restarted with the same code.
  1365. */
  1366. restart_block->fn = clock_nanosleep_restart;
  1367. restart_block->arg0 = which_clock;
  1368. /*
  1369. * Caller sets arg1
  1370. */
  1371. restart_block->arg2 = rq_time & 0xffffffffLL;
  1372. restart_block->arg3 = rq_time >> 32;
  1373. return -ERESTART_RESTARTBLOCK;
  1374. }
  1375. return 0;
  1376. }
  1377. /*
  1378. * This will restart clock_nanosleep.
  1379. */
  1380. long
  1381. clock_nanosleep_restart(struct restart_block *restart_block)
  1382. {
  1383. struct timespec t;
  1384. int ret = common_nsleep(restart_block->arg0, 0, &t);
  1385. if ((ret == -ERESTART_RESTARTBLOCK) && restart_block->arg1 &&
  1386. copy_to_user((struct timespec __user *)(restart_block->arg1), &t,
  1387. sizeof (t)))
  1388. return -EFAULT;
  1389. return ret;
  1390. }