posix-timers.c 44 KB

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