posix-timers.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555
  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. struct task_struct *leader;
  383. int ret = send_sigqueue(timr->it_sigev_signo, timr->sigq,
  384. timr->it_process);
  385. if (likely(ret >= 0))
  386. return ret;
  387. timr->it_sigev_notify = SIGEV_SIGNAL;
  388. leader = timr->it_process->group_leader;
  389. put_task_struct(timr->it_process);
  390. timr->it_process = leader;
  391. }
  392. return send_group_sigqueue(timr->it_sigev_signo, timr->sigq,
  393. timr->it_process);
  394. }
  395. EXPORT_SYMBOL_GPL(posix_timer_event);
  396. /*
  397. * This function gets called when a POSIX.1b interval timer expires. It
  398. * is used as a callback from the kernel internal timer. The
  399. * run_timer_list code ALWAYS calls with interrupts on.
  400. * This code is for CLOCK_REALTIME* and CLOCK_MONOTONIC* timers.
  401. */
  402. static void posix_timer_fn(unsigned long __data)
  403. {
  404. struct k_itimer *timr = (struct k_itimer *) __data;
  405. unsigned long flags;
  406. unsigned long seq;
  407. struct timespec delta, new_wall_to;
  408. u64 exp = 0;
  409. int do_notify = 1;
  410. spin_lock_irqsave(&timr->it_lock, flags);
  411. if (!list_empty(&timr->it.real.abs_timer_entry)) {
  412. spin_lock(&abs_list.lock);
  413. do {
  414. seq = read_seqbegin(&xtime_lock);
  415. new_wall_to = wall_to_monotonic;
  416. } while (read_seqretry(&xtime_lock, seq));
  417. set_normalized_timespec(&delta,
  418. new_wall_to.tv_sec -
  419. timr->it.real.wall_to_prev.tv_sec,
  420. new_wall_to.tv_nsec -
  421. timr->it.real.wall_to_prev.tv_nsec);
  422. if (likely((delta.tv_sec | delta.tv_nsec ) == 0)) {
  423. /* do nothing, timer is on time */
  424. } else if (delta.tv_sec < 0) {
  425. /* do nothing, timer is already late */
  426. } else {
  427. /* timer is early due to a clock set */
  428. tstojiffie(&delta,
  429. posix_clocks[timr->it_clock].res,
  430. &exp);
  431. timr->it.real.wall_to_prev = new_wall_to;
  432. timr->it.real.timer.expires += exp;
  433. add_timer(&timr->it.real.timer);
  434. do_notify = 0;
  435. }
  436. spin_unlock(&abs_list.lock);
  437. }
  438. if (do_notify) {
  439. int si_private=0;
  440. if (timr->it.real.incr)
  441. si_private = ++timr->it_requeue_pending;
  442. else {
  443. remove_from_abslist(timr);
  444. }
  445. if (posix_timer_event(timr, si_private))
  446. /*
  447. * signal was not sent because of sig_ignor
  448. * we will not get a call back to restart it AND
  449. * it should be restarted.
  450. */
  451. schedule_next_timer(timr);
  452. }
  453. unlock_timer(timr, flags); /* hold thru abs lock to keep irq off */
  454. }
  455. static inline struct task_struct * good_sigevent(sigevent_t * event)
  456. {
  457. struct task_struct *rtn = current->group_leader;
  458. if ((event->sigev_notify & SIGEV_THREAD_ID ) &&
  459. (!(rtn = find_task_by_pid(event->sigev_notify_thread_id)) ||
  460. rtn->tgid != current->tgid ||
  461. (event->sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_SIGNAL))
  462. return NULL;
  463. if (((event->sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE) &&
  464. ((event->sigev_signo <= 0) || (event->sigev_signo > SIGRTMAX)))
  465. return NULL;
  466. return rtn;
  467. }
  468. void register_posix_clock(clockid_t clock_id, struct k_clock *new_clock)
  469. {
  470. if ((unsigned) clock_id >= MAX_CLOCKS) {
  471. printk("POSIX clock register failed for clock_id %d\n",
  472. clock_id);
  473. return;
  474. }
  475. posix_clocks[clock_id] = *new_clock;
  476. }
  477. EXPORT_SYMBOL_GPL(register_posix_clock);
  478. static struct k_itimer * alloc_posix_timer(void)
  479. {
  480. struct k_itimer *tmr;
  481. tmr = kmem_cache_alloc(posix_timers_cache, GFP_KERNEL);
  482. if (!tmr)
  483. return tmr;
  484. memset(tmr, 0, sizeof (struct k_itimer));
  485. if (unlikely(!(tmr->sigq = sigqueue_alloc()))) {
  486. kmem_cache_free(posix_timers_cache, tmr);
  487. tmr = NULL;
  488. }
  489. return tmr;
  490. }
  491. #define IT_ID_SET 1
  492. #define IT_ID_NOT_SET 0
  493. static void release_posix_timer(struct k_itimer *tmr, int it_id_set)
  494. {
  495. if (it_id_set) {
  496. unsigned long flags;
  497. spin_lock_irqsave(&idr_lock, flags);
  498. idr_remove(&posix_timers_id, tmr->it_id);
  499. spin_unlock_irqrestore(&idr_lock, flags);
  500. }
  501. sigqueue_free(tmr->sigq);
  502. if (unlikely(tmr->it_process) &&
  503. tmr->it_sigev_notify == (SIGEV_SIGNAL|SIGEV_THREAD_ID))
  504. put_task_struct(tmr->it_process);
  505. kmem_cache_free(posix_timers_cache, tmr);
  506. }
  507. /* Create a POSIX.1b interval timer. */
  508. asmlinkage long
  509. sys_timer_create(clockid_t which_clock,
  510. struct sigevent __user *timer_event_spec,
  511. timer_t __user * created_timer_id)
  512. {
  513. int error = 0;
  514. struct k_itimer *new_timer = NULL;
  515. int new_timer_id;
  516. struct task_struct *process = NULL;
  517. unsigned long flags;
  518. sigevent_t event;
  519. int it_id_set = IT_ID_NOT_SET;
  520. if (invalid_clockid(which_clock))
  521. return -EINVAL;
  522. new_timer = alloc_posix_timer();
  523. if (unlikely(!new_timer))
  524. return -EAGAIN;
  525. spin_lock_init(&new_timer->it_lock);
  526. retry:
  527. if (unlikely(!idr_pre_get(&posix_timers_id, GFP_KERNEL))) {
  528. error = -EAGAIN;
  529. goto out;
  530. }
  531. spin_lock_irq(&idr_lock);
  532. error = idr_get_new(&posix_timers_id,
  533. (void *) new_timer,
  534. &new_timer_id);
  535. spin_unlock_irq(&idr_lock);
  536. if (error == -EAGAIN)
  537. goto retry;
  538. else if (error) {
  539. /*
  540. * Wierd looking, but we return EAGAIN if the IDR is
  541. * full (proper POSIX return value for this)
  542. */
  543. error = -EAGAIN;
  544. goto out;
  545. }
  546. it_id_set = IT_ID_SET;
  547. new_timer->it_id = (timer_t) new_timer_id;
  548. new_timer->it_clock = which_clock;
  549. new_timer->it_overrun = -1;
  550. error = CLOCK_DISPATCH(which_clock, timer_create, (new_timer));
  551. if (error)
  552. goto out;
  553. /*
  554. * return the timer_id now. The next step is hard to
  555. * back out if there is an error.
  556. */
  557. if (copy_to_user(created_timer_id,
  558. &new_timer_id, sizeof (new_timer_id))) {
  559. error = -EFAULT;
  560. goto out;
  561. }
  562. if (timer_event_spec) {
  563. if (copy_from_user(&event, timer_event_spec, sizeof (event))) {
  564. error = -EFAULT;
  565. goto out;
  566. }
  567. new_timer->it_sigev_notify = event.sigev_notify;
  568. new_timer->it_sigev_signo = event.sigev_signo;
  569. new_timer->it_sigev_value = event.sigev_value;
  570. read_lock(&tasklist_lock);
  571. if ((process = good_sigevent(&event))) {
  572. /*
  573. * We may be setting up this process for another
  574. * thread. It may be exiting. To catch this
  575. * case the we check the PF_EXITING flag. If
  576. * the flag is not set, the siglock will catch
  577. * him before it is too late (in exit_itimers).
  578. *
  579. * The exec case is a bit more invloved but easy
  580. * to code. If the process is in our thread
  581. * group (and it must be or we would not allow
  582. * it here) and is doing an exec, it will cause
  583. * us to be killed. In this case it will wait
  584. * for us to die which means we can finish this
  585. * linkage with our last gasp. I.e. no code :)
  586. */
  587. spin_lock_irqsave(&process->sighand->siglock, flags);
  588. if (!(process->flags & PF_EXITING)) {
  589. new_timer->it_process = process;
  590. list_add(&new_timer->list,
  591. &process->signal->posix_timers);
  592. spin_unlock_irqrestore(&process->sighand->siglock, flags);
  593. if (new_timer->it_sigev_notify == (SIGEV_SIGNAL|SIGEV_THREAD_ID))
  594. get_task_struct(process);
  595. } else {
  596. spin_unlock_irqrestore(&process->sighand->siglock, flags);
  597. process = NULL;
  598. }
  599. }
  600. read_unlock(&tasklist_lock);
  601. if (!process) {
  602. error = -EINVAL;
  603. goto out;
  604. }
  605. } else {
  606. new_timer->it_sigev_notify = SIGEV_SIGNAL;
  607. new_timer->it_sigev_signo = SIGALRM;
  608. new_timer->it_sigev_value.sival_int = new_timer->it_id;
  609. process = current->group_leader;
  610. spin_lock_irqsave(&process->sighand->siglock, flags);
  611. new_timer->it_process = process;
  612. list_add(&new_timer->list, &process->signal->posix_timers);
  613. spin_unlock_irqrestore(&process->sighand->siglock, flags);
  614. }
  615. /*
  616. * In the case of the timer belonging to another task, after
  617. * the task is unlocked, the timer is owned by the other task
  618. * and may cease to exist at any time. Don't use or modify
  619. * new_timer after the unlock call.
  620. */
  621. out:
  622. if (error)
  623. release_posix_timer(new_timer, it_id_set);
  624. return error;
  625. }
  626. /*
  627. * good_timespec
  628. *
  629. * This function checks the elements of a timespec structure.
  630. *
  631. * Arguments:
  632. * ts : Pointer to the timespec structure to check
  633. *
  634. * Return value:
  635. * If a NULL pointer was passed in, or the tv_nsec field was less than 0
  636. * or greater than NSEC_PER_SEC, or the tv_sec field was less than 0,
  637. * this function returns 0. Otherwise it returns 1.
  638. */
  639. static int good_timespec(const struct timespec *ts)
  640. {
  641. if ((!ts) || (ts->tv_sec < 0) ||
  642. ((unsigned) ts->tv_nsec >= NSEC_PER_SEC))
  643. return 0;
  644. return 1;
  645. }
  646. /*
  647. * Locking issues: We need to protect the result of the id look up until
  648. * we get the timer locked down so it is not deleted under us. The
  649. * removal is done under the idr spinlock so we use that here to bridge
  650. * the find to the timer lock. To avoid a dead lock, the timer id MUST
  651. * be release with out holding the timer lock.
  652. */
  653. static struct k_itimer * lock_timer(timer_t timer_id, unsigned long *flags)
  654. {
  655. struct k_itimer *timr;
  656. /*
  657. * Watch out here. We do a irqsave on the idr_lock and pass the
  658. * flags part over to the timer lock. Must not let interrupts in
  659. * while we are moving the lock.
  660. */
  661. spin_lock_irqsave(&idr_lock, *flags);
  662. timr = (struct k_itimer *) idr_find(&posix_timers_id, (int) timer_id);
  663. if (timr) {
  664. spin_lock(&timr->it_lock);
  665. spin_unlock(&idr_lock);
  666. if ((timr->it_id != timer_id) || !(timr->it_process) ||
  667. timr->it_process->tgid != current->tgid) {
  668. unlock_timer(timr, *flags);
  669. timr = NULL;
  670. }
  671. } else
  672. spin_unlock_irqrestore(&idr_lock, *flags);
  673. return timr;
  674. }
  675. /*
  676. * Get the time remaining on a POSIX.1b interval timer. This function
  677. * is ALWAYS called with spin_lock_irq on the timer, thus it must not
  678. * mess with irq.
  679. *
  680. * We have a couple of messes to clean up here. First there is the case
  681. * of a timer that has a requeue pending. These timers should appear to
  682. * be in the timer list with an expiry as if we were to requeue them
  683. * now.
  684. *
  685. * The second issue is the SIGEV_NONE timer which may be active but is
  686. * not really ever put in the timer list (to save system resources).
  687. * This timer may be expired, and if so, we will do it here. Otherwise
  688. * it is the same as a requeue pending timer WRT to what we should
  689. * report.
  690. */
  691. static void
  692. common_timer_get(struct k_itimer *timr, struct itimerspec *cur_setting)
  693. {
  694. unsigned long expires;
  695. struct now_struct now;
  696. do
  697. expires = timr->it.real.timer.expires;
  698. while ((volatile long) (timr->it.real.timer.expires) != expires);
  699. posix_get_now(&now);
  700. if (expires &&
  701. ((timr->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE) &&
  702. !timr->it.real.incr &&
  703. posix_time_before(&timr->it.real.timer, &now))
  704. timr->it.real.timer.expires = expires = 0;
  705. if (expires) {
  706. if (timr->it_requeue_pending & REQUEUE_PENDING ||
  707. (timr->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE) {
  708. posix_bump_timer(timr, now);
  709. expires = timr->it.real.timer.expires;
  710. }
  711. else
  712. if (!timer_pending(&timr->it.real.timer))
  713. expires = 0;
  714. if (expires)
  715. expires -= now.jiffies;
  716. }
  717. jiffies_to_timespec(expires, &cur_setting->it_value);
  718. jiffies_to_timespec(timr->it.real.incr, &cur_setting->it_interval);
  719. if (cur_setting->it_value.tv_sec < 0) {
  720. cur_setting->it_value.tv_nsec = 1;
  721. cur_setting->it_value.tv_sec = 0;
  722. }
  723. }
  724. /* Get the time remaining on a POSIX.1b interval timer. */
  725. asmlinkage long
  726. sys_timer_gettime(timer_t timer_id, struct itimerspec __user *setting)
  727. {
  728. struct k_itimer *timr;
  729. struct itimerspec cur_setting;
  730. unsigned long flags;
  731. timr = lock_timer(timer_id, &flags);
  732. if (!timr)
  733. return -EINVAL;
  734. CLOCK_DISPATCH(timr->it_clock, timer_get, (timr, &cur_setting));
  735. unlock_timer(timr, flags);
  736. if (copy_to_user(setting, &cur_setting, sizeof (cur_setting)))
  737. return -EFAULT;
  738. return 0;
  739. }
  740. /*
  741. * Get the number of overruns of a POSIX.1b interval timer. This is to
  742. * be the overrun of the timer last delivered. At the same time we are
  743. * accumulating overruns on the next timer. The overrun is frozen when
  744. * the signal is delivered, either at the notify time (if the info block
  745. * is not queued) or at the actual delivery time (as we are informed by
  746. * the call back to do_schedule_next_timer(). So all we need to do is
  747. * to pick up the frozen overrun.
  748. */
  749. asmlinkage long
  750. sys_timer_getoverrun(timer_t timer_id)
  751. {
  752. struct k_itimer *timr;
  753. int overrun;
  754. long flags;
  755. timr = lock_timer(timer_id, &flags);
  756. if (!timr)
  757. return -EINVAL;
  758. overrun = timr->it_overrun_last;
  759. unlock_timer(timr, flags);
  760. return overrun;
  761. }
  762. /*
  763. * Adjust for absolute time
  764. *
  765. * If absolute time is given and it is not CLOCK_MONOTONIC, we need to
  766. * adjust for the offset between the timer clock (CLOCK_MONOTONIC) and
  767. * what ever clock he is using.
  768. *
  769. * If it is relative time, we need to add the current (CLOCK_MONOTONIC)
  770. * time to it to get the proper time for the timer.
  771. */
  772. static int adjust_abs_time(struct k_clock *clock, struct timespec *tp,
  773. int abs, u64 *exp, struct timespec *wall_to)
  774. {
  775. struct timespec now;
  776. struct timespec oc = *tp;
  777. u64 jiffies_64_f;
  778. int rtn =0;
  779. if (abs) {
  780. /*
  781. * The mask pick up the 4 basic clocks
  782. */
  783. if (!((clock - &posix_clocks[0]) & ~CLOCKS_MASK)) {
  784. jiffies_64_f = do_posix_clock_monotonic_gettime_parts(
  785. &now, wall_to);
  786. /*
  787. * If we are doing a MONOTONIC clock
  788. */
  789. if((clock - &posix_clocks[0]) & CLOCKS_MONO){
  790. now.tv_sec += wall_to->tv_sec;
  791. now.tv_nsec += wall_to->tv_nsec;
  792. }
  793. } else {
  794. /*
  795. * Not one of the basic clocks
  796. */
  797. clock->clock_get(clock - posix_clocks, &now);
  798. jiffies_64_f = get_jiffies_64();
  799. }
  800. /*
  801. * Take away now to get delta and normalize
  802. */
  803. set_normalized_timespec(&oc, oc.tv_sec - now.tv_sec,
  804. oc.tv_nsec - now.tv_nsec);
  805. }else{
  806. jiffies_64_f = get_jiffies_64();
  807. }
  808. /*
  809. * Check if the requested time is prior to now (if so set now)
  810. */
  811. if (oc.tv_sec < 0)
  812. oc.tv_sec = oc.tv_nsec = 0;
  813. if (oc.tv_sec | oc.tv_nsec)
  814. set_normalized_timespec(&oc, oc.tv_sec,
  815. oc.tv_nsec + clock->res);
  816. tstojiffie(&oc, clock->res, exp);
  817. /*
  818. * Check if the requested time is more than the timer code
  819. * can handle (if so we error out but return the value too).
  820. */
  821. if (*exp > ((u64)MAX_JIFFY_OFFSET))
  822. /*
  823. * This is a considered response, not exactly in
  824. * line with the standard (in fact it is silent on
  825. * possible overflows). We assume such a large
  826. * value is ALMOST always a programming error and
  827. * try not to compound it by setting a really dumb
  828. * value.
  829. */
  830. rtn = -EINVAL;
  831. /*
  832. * return the actual jiffies expire time, full 64 bits
  833. */
  834. *exp += jiffies_64_f;
  835. return rtn;
  836. }
  837. /* Set a POSIX.1b interval timer. */
  838. /* timr->it_lock is taken. */
  839. static inline int
  840. common_timer_set(struct k_itimer *timr, int flags,
  841. struct itimerspec *new_setting, struct itimerspec *old_setting)
  842. {
  843. struct k_clock *clock = &posix_clocks[timr->it_clock];
  844. u64 expire_64;
  845. if (old_setting)
  846. common_timer_get(timr, old_setting);
  847. /* disable the timer */
  848. timr->it.real.incr = 0;
  849. /*
  850. * careful here. If smp we could be in the "fire" routine which will
  851. * be spinning as we hold the lock. But this is ONLY an SMP issue.
  852. */
  853. if (try_to_del_timer_sync(&timr->it.real.timer) < 0) {
  854. #ifdef CONFIG_SMP
  855. /*
  856. * It can only be active if on an other cpu. Since
  857. * we have cleared the interval stuff above, it should
  858. * clear once we release the spin lock. Of course once
  859. * we do that anything could happen, including the
  860. * complete melt down of the timer. So return with
  861. * a "retry" exit status.
  862. */
  863. return TIMER_RETRY;
  864. #endif
  865. }
  866. remove_from_abslist(timr);
  867. timr->it_requeue_pending = (timr->it_requeue_pending + 2) &
  868. ~REQUEUE_PENDING;
  869. timr->it_overrun_last = 0;
  870. timr->it_overrun = -1;
  871. /*
  872. *switch off the timer when it_value is zero
  873. */
  874. if (!new_setting->it_value.tv_sec && !new_setting->it_value.tv_nsec) {
  875. timr->it.real.timer.expires = 0;
  876. return 0;
  877. }
  878. if (adjust_abs_time(clock,
  879. &new_setting->it_value, flags & TIMER_ABSTIME,
  880. &expire_64, &(timr->it.real.wall_to_prev))) {
  881. return -EINVAL;
  882. }
  883. timr->it.real.timer.expires = (unsigned long)expire_64;
  884. tstojiffie(&new_setting->it_interval, clock->res, &expire_64);
  885. timr->it.real.incr = (unsigned long)expire_64;
  886. /*
  887. * We do not even queue SIGEV_NONE timers! But we do put them
  888. * in the abs list so we can do that right.
  889. */
  890. if (((timr->it_sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE))
  891. add_timer(&timr->it.real.timer);
  892. if (flags & TIMER_ABSTIME && clock->abs_struct) {
  893. spin_lock(&clock->abs_struct->lock);
  894. list_add_tail(&(timr->it.real.abs_timer_entry),
  895. &(clock->abs_struct->list));
  896. spin_unlock(&clock->abs_struct->lock);
  897. }
  898. return 0;
  899. }
  900. /* Set a POSIX.1b interval timer */
  901. asmlinkage long
  902. sys_timer_settime(timer_t timer_id, int flags,
  903. const struct itimerspec __user *new_setting,
  904. struct itimerspec __user *old_setting)
  905. {
  906. struct k_itimer *timr;
  907. struct itimerspec new_spec, old_spec;
  908. int error = 0;
  909. long flag;
  910. struct itimerspec *rtn = old_setting ? &old_spec : NULL;
  911. if (!new_setting)
  912. return -EINVAL;
  913. if (copy_from_user(&new_spec, new_setting, sizeof (new_spec)))
  914. return -EFAULT;
  915. if ((!good_timespec(&new_spec.it_interval)) ||
  916. (!good_timespec(&new_spec.it_value)))
  917. return -EINVAL;
  918. retry:
  919. timr = lock_timer(timer_id, &flag);
  920. if (!timr)
  921. return -EINVAL;
  922. error = CLOCK_DISPATCH(timr->it_clock, timer_set,
  923. (timr, flags, &new_spec, rtn));
  924. unlock_timer(timr, flag);
  925. if (error == TIMER_RETRY) {
  926. rtn = NULL; // We already got the old time...
  927. goto retry;
  928. }
  929. if (old_setting && !error && copy_to_user(old_setting,
  930. &old_spec, sizeof (old_spec)))
  931. error = -EFAULT;
  932. return error;
  933. }
  934. static inline int common_timer_del(struct k_itimer *timer)
  935. {
  936. timer->it.real.incr = 0;
  937. if (try_to_del_timer_sync(&timer->it.real.timer) < 0) {
  938. #ifdef CONFIG_SMP
  939. /*
  940. * It can only be active if on an other cpu. Since
  941. * we have cleared the interval stuff above, it should
  942. * clear once we release the spin lock. Of course once
  943. * we do that anything could happen, including the
  944. * complete melt down of the timer. So return with
  945. * a "retry" exit status.
  946. */
  947. return TIMER_RETRY;
  948. #endif
  949. }
  950. remove_from_abslist(timer);
  951. return 0;
  952. }
  953. static inline int timer_delete_hook(struct k_itimer *timer)
  954. {
  955. return CLOCK_DISPATCH(timer->it_clock, timer_del, (timer));
  956. }
  957. /* Delete a POSIX.1b interval timer. */
  958. asmlinkage long
  959. sys_timer_delete(timer_t timer_id)
  960. {
  961. struct k_itimer *timer;
  962. long flags;
  963. #ifdef CONFIG_SMP
  964. int error;
  965. retry_delete:
  966. #endif
  967. timer = lock_timer(timer_id, &flags);
  968. if (!timer)
  969. return -EINVAL;
  970. #ifdef CONFIG_SMP
  971. error = timer_delete_hook(timer);
  972. if (error == TIMER_RETRY) {
  973. unlock_timer(timer, flags);
  974. goto retry_delete;
  975. }
  976. #else
  977. timer_delete_hook(timer);
  978. #endif
  979. spin_lock(&current->sighand->siglock);
  980. list_del(&timer->list);
  981. spin_unlock(&current->sighand->siglock);
  982. /*
  983. * This keeps any tasks waiting on the spin lock from thinking
  984. * they got something (see the lock code above).
  985. */
  986. if (timer->it_process) {
  987. if (timer->it_sigev_notify == (SIGEV_SIGNAL|SIGEV_THREAD_ID))
  988. put_task_struct(timer->it_process);
  989. timer->it_process = NULL;
  990. }
  991. unlock_timer(timer, flags);
  992. release_posix_timer(timer, IT_ID_SET);
  993. return 0;
  994. }
  995. /*
  996. * return timer owned by the process, used by exit_itimers
  997. */
  998. static inline void itimer_delete(struct k_itimer *timer)
  999. {
  1000. unsigned long flags;
  1001. #ifdef CONFIG_SMP
  1002. int error;
  1003. retry_delete:
  1004. #endif
  1005. spin_lock_irqsave(&timer->it_lock, flags);
  1006. #ifdef CONFIG_SMP
  1007. error = timer_delete_hook(timer);
  1008. if (error == TIMER_RETRY) {
  1009. unlock_timer(timer, flags);
  1010. goto retry_delete;
  1011. }
  1012. #else
  1013. timer_delete_hook(timer);
  1014. #endif
  1015. list_del(&timer->list);
  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. }
  1028. /*
  1029. * This is called by __exit_signal, only when there are no more
  1030. * references to the shared signal_struct.
  1031. */
  1032. void exit_itimers(struct signal_struct *sig)
  1033. {
  1034. struct k_itimer *tmr;
  1035. while (!list_empty(&sig->posix_timers)) {
  1036. tmr = list_entry(sig->posix_timers.next, struct k_itimer, list);
  1037. itimer_delete(tmr);
  1038. }
  1039. }
  1040. /*
  1041. * And now for the "clock" calls
  1042. *
  1043. * These functions are called both from timer functions (with the timer
  1044. * spin_lock_irq() held and from clock calls with no locking. They must
  1045. * use the save flags versions of locks.
  1046. */
  1047. /*
  1048. * We do ticks here to avoid the irq lock ( they take sooo long).
  1049. * The seqlock is great here. Since we a reader, we don't really care
  1050. * if we are interrupted since we don't take lock that will stall us or
  1051. * any other cpu. Voila, no irq lock is needed.
  1052. *
  1053. */
  1054. static u64 do_posix_clock_monotonic_gettime_parts(
  1055. struct timespec *tp, struct timespec *mo)
  1056. {
  1057. u64 jiff;
  1058. unsigned int seq;
  1059. do {
  1060. seq = read_seqbegin(&xtime_lock);
  1061. getnstimeofday(tp);
  1062. *mo = wall_to_monotonic;
  1063. jiff = jiffies_64;
  1064. } while(read_seqretry(&xtime_lock, seq));
  1065. return jiff;
  1066. }
  1067. static int do_posix_clock_monotonic_get(clockid_t clock, struct timespec *tp)
  1068. {
  1069. struct timespec wall_to_mono;
  1070. do_posix_clock_monotonic_gettime_parts(tp, &wall_to_mono);
  1071. tp->tv_sec += wall_to_mono.tv_sec;
  1072. tp->tv_nsec += wall_to_mono.tv_nsec;
  1073. if ((tp->tv_nsec - NSEC_PER_SEC) > 0) {
  1074. tp->tv_nsec -= NSEC_PER_SEC;
  1075. tp->tv_sec++;
  1076. }
  1077. return 0;
  1078. }
  1079. int do_posix_clock_monotonic_gettime(struct timespec *tp)
  1080. {
  1081. return do_posix_clock_monotonic_get(CLOCK_MONOTONIC, tp);
  1082. }
  1083. int do_posix_clock_nosettime(clockid_t clockid, struct timespec *tp)
  1084. {
  1085. return -EINVAL;
  1086. }
  1087. EXPORT_SYMBOL_GPL(do_posix_clock_nosettime);
  1088. int do_posix_clock_notimer_create(struct k_itimer *timer)
  1089. {
  1090. return -EINVAL;
  1091. }
  1092. EXPORT_SYMBOL_GPL(do_posix_clock_notimer_create);
  1093. int do_posix_clock_nonanosleep(clockid_t clock, int flags, struct timespec *t)
  1094. {
  1095. #ifndef ENOTSUP
  1096. return -EOPNOTSUPP; /* aka ENOTSUP in userland for POSIX */
  1097. #else /* parisc does define it separately. */
  1098. return -ENOTSUP;
  1099. #endif
  1100. }
  1101. EXPORT_SYMBOL_GPL(do_posix_clock_nonanosleep);
  1102. asmlinkage long
  1103. sys_clock_settime(clockid_t which_clock, const struct timespec __user *tp)
  1104. {
  1105. struct timespec new_tp;
  1106. if (invalid_clockid(which_clock))
  1107. return -EINVAL;
  1108. if (copy_from_user(&new_tp, tp, sizeof (*tp)))
  1109. return -EFAULT;
  1110. return CLOCK_DISPATCH(which_clock, clock_set, (which_clock, &new_tp));
  1111. }
  1112. asmlinkage long
  1113. sys_clock_gettime(clockid_t which_clock, struct timespec __user *tp)
  1114. {
  1115. struct timespec kernel_tp;
  1116. int error;
  1117. if (invalid_clockid(which_clock))
  1118. return -EINVAL;
  1119. error = CLOCK_DISPATCH(which_clock, clock_get,
  1120. (which_clock, &kernel_tp));
  1121. if (!error && copy_to_user(tp, &kernel_tp, sizeof (kernel_tp)))
  1122. error = -EFAULT;
  1123. return error;
  1124. }
  1125. asmlinkage long
  1126. sys_clock_getres(clockid_t which_clock, struct timespec __user *tp)
  1127. {
  1128. struct timespec rtn_tp;
  1129. int error;
  1130. if (invalid_clockid(which_clock))
  1131. return -EINVAL;
  1132. error = CLOCK_DISPATCH(which_clock, clock_getres,
  1133. (which_clock, &rtn_tp));
  1134. if (!error && tp && copy_to_user(tp, &rtn_tp, sizeof (rtn_tp))) {
  1135. error = -EFAULT;
  1136. }
  1137. return error;
  1138. }
  1139. static void nanosleep_wake_up(unsigned long __data)
  1140. {
  1141. struct task_struct *p = (struct task_struct *) __data;
  1142. wake_up_process(p);
  1143. }
  1144. /*
  1145. * The standard says that an absolute nanosleep call MUST wake up at
  1146. * the requested time in spite of clock settings. Here is what we do:
  1147. * For each nanosleep call that needs it (only absolute and not on
  1148. * CLOCK_MONOTONIC* (as it can not be set)) we thread a little structure
  1149. * into the "nanosleep_abs_list". All we need is the task_struct pointer.
  1150. * When ever the clock is set we just wake up all those tasks. The rest
  1151. * is done by the while loop in clock_nanosleep().
  1152. *
  1153. * On locking, clock_was_set() is called from update_wall_clock which
  1154. * holds (or has held for it) a write_lock_irq( xtime_lock) and is
  1155. * called from the timer bh code. Thus we need the irq save locks.
  1156. *
  1157. * Also, on the call from update_wall_clock, that is done as part of a
  1158. * softirq thing. We don't want to delay the system that much (possibly
  1159. * long list of timers to fix), so we defer that work to keventd.
  1160. */
  1161. static DECLARE_WAIT_QUEUE_HEAD(nanosleep_abs_wqueue);
  1162. static DECLARE_WORK(clock_was_set_work, (void(*)(void*))clock_was_set, NULL);
  1163. static DECLARE_MUTEX(clock_was_set_lock);
  1164. void clock_was_set(void)
  1165. {
  1166. struct k_itimer *timr;
  1167. struct timespec new_wall_to;
  1168. LIST_HEAD(cws_list);
  1169. unsigned long seq;
  1170. if (unlikely(in_interrupt())) {
  1171. schedule_work(&clock_was_set_work);
  1172. return;
  1173. }
  1174. wake_up_all(&nanosleep_abs_wqueue);
  1175. /*
  1176. * Check if there exist TIMER_ABSTIME timers to correct.
  1177. *
  1178. * Notes on locking: This code is run in task context with irq
  1179. * on. We CAN be interrupted! All other usage of the abs list
  1180. * lock is under the timer lock which holds the irq lock as
  1181. * well. We REALLY don't want to scan the whole list with the
  1182. * interrupt system off, AND we would like a sequence lock on
  1183. * this code as well. Since we assume that the clock will not
  1184. * be set often, it seems ok to take and release the irq lock
  1185. * for each timer. In fact add_timer will do this, so this is
  1186. * not an issue. So we know when we are done, we will move the
  1187. * whole list to a new location. Then as we process each entry,
  1188. * we will move it to the actual list again. This way, when our
  1189. * copy is empty, we are done. We are not all that concerned
  1190. * about preemption so we will use a semaphore lock to protect
  1191. * aginst reentry. This way we will not stall another
  1192. * processor. It is possible that this may delay some timers
  1193. * that should have expired, given the new clock, but even this
  1194. * will be minimal as we will always update to the current time,
  1195. * even if it was set by a task that is waiting for entry to
  1196. * this code. Timers that expire too early will be caught by
  1197. * the expire code and restarted.
  1198. * Absolute timers that repeat are left in the abs list while
  1199. * waiting for the task to pick up the signal. This means we
  1200. * may find timers that are not in the "add_timer" list, but are
  1201. * in the abs list. We do the same thing for these, save
  1202. * putting them back in the "add_timer" list. (Note, these are
  1203. * left in the abs list mainly to indicate that they are
  1204. * ABSOLUTE timers, a fact that is used by the re-arm code, and
  1205. * for which we have no other flag.)
  1206. */
  1207. down(&clock_was_set_lock);
  1208. spin_lock_irq(&abs_list.lock);
  1209. list_splice_init(&abs_list.list, &cws_list);
  1210. spin_unlock_irq(&abs_list.lock);
  1211. do {
  1212. do {
  1213. seq = read_seqbegin(&xtime_lock);
  1214. new_wall_to = wall_to_monotonic;
  1215. } while (read_seqretry(&xtime_lock, seq));
  1216. spin_lock_irq(&abs_list.lock);
  1217. if (list_empty(&cws_list)) {
  1218. spin_unlock_irq(&abs_list.lock);
  1219. break;
  1220. }
  1221. timr = list_entry(cws_list.next, struct k_itimer,
  1222. it.real.abs_timer_entry);
  1223. list_del_init(&timr->it.real.abs_timer_entry);
  1224. if (add_clockset_delta(timr, &new_wall_to) &&
  1225. del_timer(&timr->it.real.timer)) /* timer run yet? */
  1226. add_timer(&timr->it.real.timer);
  1227. list_add(&timr->it.real.abs_timer_entry, &abs_list.list);
  1228. spin_unlock_irq(&abs_list.lock);
  1229. } while (1);
  1230. up(&clock_was_set_lock);
  1231. }
  1232. long clock_nanosleep_restart(struct restart_block *restart_block);
  1233. asmlinkage long
  1234. sys_clock_nanosleep(clockid_t which_clock, int flags,
  1235. const struct timespec __user *rqtp,
  1236. struct timespec __user *rmtp)
  1237. {
  1238. struct timespec t;
  1239. struct restart_block *restart_block =
  1240. &(current_thread_info()->restart_block);
  1241. int ret;
  1242. if (invalid_clockid(which_clock))
  1243. return -EINVAL;
  1244. if (copy_from_user(&t, rqtp, sizeof (struct timespec)))
  1245. return -EFAULT;
  1246. if ((unsigned) t.tv_nsec >= NSEC_PER_SEC || t.tv_sec < 0)
  1247. return -EINVAL;
  1248. /*
  1249. * Do this here as nsleep function does not have the real address.
  1250. */
  1251. restart_block->arg1 = (unsigned long)rmtp;
  1252. ret = CLOCK_DISPATCH(which_clock, nsleep, (which_clock, flags, &t));
  1253. if ((ret == -ERESTART_RESTARTBLOCK) && rmtp &&
  1254. copy_to_user(rmtp, &t, sizeof (t)))
  1255. return -EFAULT;
  1256. return ret;
  1257. }
  1258. static int common_nsleep(clockid_t which_clock,
  1259. int flags, struct timespec *tsave)
  1260. {
  1261. struct timespec t, dum;
  1262. struct timer_list new_timer;
  1263. DECLARE_WAITQUEUE(abs_wqueue, current);
  1264. u64 rq_time = (u64)0;
  1265. s64 left;
  1266. int abs;
  1267. struct restart_block *restart_block =
  1268. &current_thread_info()->restart_block;
  1269. abs_wqueue.flags = 0;
  1270. init_timer(&new_timer);
  1271. new_timer.expires = 0;
  1272. new_timer.data = (unsigned long) current;
  1273. new_timer.function = nanosleep_wake_up;
  1274. abs = flags & TIMER_ABSTIME;
  1275. if (restart_block->fn == clock_nanosleep_restart) {
  1276. /*
  1277. * Interrupted by a non-delivered signal, pick up remaining
  1278. * time and continue. Remaining time is in arg2 & 3.
  1279. */
  1280. restart_block->fn = do_no_restart_syscall;
  1281. rq_time = restart_block->arg3;
  1282. rq_time = (rq_time << 32) + restart_block->arg2;
  1283. if (!rq_time)
  1284. return -EINTR;
  1285. left = rq_time - get_jiffies_64();
  1286. if (left <= (s64)0)
  1287. return 0; /* Already passed */
  1288. }
  1289. if (abs && (posix_clocks[which_clock].clock_get !=
  1290. posix_clocks[CLOCK_MONOTONIC].clock_get))
  1291. add_wait_queue(&nanosleep_abs_wqueue, &abs_wqueue);
  1292. do {
  1293. t = *tsave;
  1294. if (abs || !rq_time) {
  1295. adjust_abs_time(&posix_clocks[which_clock], &t, abs,
  1296. &rq_time, &dum);
  1297. }
  1298. left = rq_time - get_jiffies_64();
  1299. if (left >= (s64)MAX_JIFFY_OFFSET)
  1300. left = (s64)MAX_JIFFY_OFFSET;
  1301. if (left < (s64)0)
  1302. break;
  1303. new_timer.expires = jiffies + left;
  1304. __set_current_state(TASK_INTERRUPTIBLE);
  1305. add_timer(&new_timer);
  1306. schedule();
  1307. del_timer_sync(&new_timer);
  1308. left = rq_time - get_jiffies_64();
  1309. } while (left > (s64)0 && !test_thread_flag(TIF_SIGPENDING));
  1310. if (abs_wqueue.task_list.next)
  1311. finish_wait(&nanosleep_abs_wqueue, &abs_wqueue);
  1312. if (left > (s64)0) {
  1313. /*
  1314. * Always restart abs calls from scratch to pick up any
  1315. * clock shifting that happened while we are away.
  1316. */
  1317. if (abs)
  1318. return -ERESTARTNOHAND;
  1319. left *= TICK_NSEC;
  1320. tsave->tv_sec = div_long_long_rem(left,
  1321. NSEC_PER_SEC,
  1322. &tsave->tv_nsec);
  1323. /*
  1324. * Restart works by saving the time remaing in
  1325. * arg2 & 3 (it is 64-bits of jiffies). The other
  1326. * info we need is the clock_id (saved in arg0).
  1327. * The sys_call interface needs the users
  1328. * timespec return address which _it_ saves in arg1.
  1329. * Since we have cast the nanosleep call to a clock_nanosleep
  1330. * both can be restarted with the same code.
  1331. */
  1332. restart_block->fn = clock_nanosleep_restart;
  1333. restart_block->arg0 = which_clock;
  1334. /*
  1335. * Caller sets arg1
  1336. */
  1337. restart_block->arg2 = rq_time & 0xffffffffLL;
  1338. restart_block->arg3 = rq_time >> 32;
  1339. return -ERESTART_RESTARTBLOCK;
  1340. }
  1341. return 0;
  1342. }
  1343. /*
  1344. * This will restart clock_nanosleep.
  1345. */
  1346. long
  1347. clock_nanosleep_restart(struct restart_block *restart_block)
  1348. {
  1349. struct timespec t;
  1350. int ret = common_nsleep(restart_block->arg0, 0, &t);
  1351. if ((ret == -ERESTART_RESTARTBLOCK) && restart_block->arg1 &&
  1352. copy_to_user((struct timespec __user *)(restart_block->arg1), &t,
  1353. sizeof (t)))
  1354. return -EFAULT;
  1355. return ret;
  1356. }