posix-timers.c 41 KB

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