posix-timers.c 44 KB

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