timer.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. /*
  2. * Copyright (c) 2017-2019 Richard Braun.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. *
  18. * This implementation is based on "Hashed and Hierarchical Timing Wheels:
  19. * Efficient Data Structures for Implementing a Timer Facility" by George
  20. * Varghese and Tony Lauck. Specifically, it implements scheme 6.1.2.
  21. *
  22. * TODO Analyse hash parameters.
  23. */
  24. #include <assert.h>
  25. #include <stdalign.h>
  26. #include <stdbool.h>
  27. #include <stddef.h>
  28. #include <stdint.h>
  29. #include <kern/atomic.h>
  30. #include <kern/clock.h>
  31. #include <kern/error.h>
  32. #include <kern/init.h>
  33. #include <kern/hlist.h>
  34. #include <kern/macros.h>
  35. #include <kern/panic.h>
  36. #include <kern/percpu.h>
  37. #include <kern/spinlock.h>
  38. #include <kern/thread.h>
  39. #include <kern/timer.h>
  40. #include <kern/timer_i.h>
  41. #include <kern/work.h>
  42. #include <machine/boot.h>
  43. #include <machine/cpu.h>
  44. /*
  45. * Timer states.
  46. */
  47. #define TIMER_TS_READY 1
  48. #define TIMER_TS_SCHEDULED 2
  49. #define TIMER_TS_RUNNING 3
  50. #define TIMER_TS_DONE 4
  51. /*
  52. * Timer flags.
  53. */
  54. #define TIMER_TF_DETACHED 0x1
  55. #define TIMER_TF_INTR 0x2
  56. #define TIMER_TF_HIGH_PRIO 0x4
  57. #define TIMER_TF_CANCELED 0x8
  58. #define TIMER_INVALID_CPU ((unsigned int)-1)
  59. #define TIMER_HTABLE_SIZE 2048
  60. #if !ISP2(TIMER_HTABLE_SIZE)
  61. #error "hash table size must be a power of two"
  62. #endif /* !ISP2(TIMER_HTABLE_SIZE) */
  63. #define TIMER_HTABLE_MASK (TIMER_HTABLE_SIZE - 1)
  64. struct timer_bucket {
  65. struct hlist timers;
  66. };
  67. /*
  68. * The hash table bucket matching the last time member has already been
  69. * processed, and the next periodic event resumes from the next bucket.
  70. *
  71. * Locking order: interrupts -> timer_cpu_data.
  72. */
  73. struct timer_cpu_data {
  74. unsigned int cpu;
  75. struct spinlock lock;
  76. uint64_t last_time;
  77. struct timer_bucket htable[TIMER_HTABLE_SIZE];
  78. };
  79. static struct timer_cpu_data timer_cpu_data __percpu;
  80. static struct timer_cpu_data *
  81. timer_cpu_data_acquire(unsigned long *flags)
  82. {
  83. struct timer_cpu_data *cpu_data;
  84. thread_preempt_disable();
  85. cpu_data = cpu_local_ptr(timer_cpu_data);
  86. spinlock_lock_intr_save(&cpu_data->lock, flags);
  87. thread_preempt_enable_no_resched();
  88. return cpu_data;
  89. }
  90. static struct timer_cpu_data *
  91. timer_lock_cpu_data(struct timer *timer, unsigned long *flags)
  92. {
  93. struct timer_cpu_data *cpu_data;
  94. unsigned int cpu;
  95. for (;;) {
  96. cpu = atomic_load(&timer->cpu, ATOMIC_RELAXED);
  97. if (cpu == TIMER_INVALID_CPU) {
  98. return NULL;
  99. }
  100. cpu_data = percpu_ptr(timer_cpu_data, cpu);
  101. spinlock_lock_intr_save(&cpu_data->lock, flags);
  102. if (cpu == atomic_load(&timer->cpu, ATOMIC_RELAXED)) {
  103. return cpu_data;
  104. }
  105. spinlock_unlock_intr_restore(&cpu_data->lock, *flags);
  106. }
  107. }
  108. static void
  109. timer_unlock_cpu_data(struct timer_cpu_data *cpu_data, unsigned long flags)
  110. {
  111. spinlock_unlock_intr_restore(&cpu_data->lock, flags);
  112. }
  113. /*
  114. * Timer state functions.
  115. */
  116. static bool
  117. timer_ready(const struct timer *timer)
  118. {
  119. return timer->state == TIMER_TS_READY;
  120. }
  121. static void
  122. timer_set_ready(struct timer *timer)
  123. {
  124. timer->state = TIMER_TS_READY;
  125. }
  126. static bool
  127. timer_scheduled(const struct timer *timer)
  128. {
  129. return timer->state == TIMER_TS_SCHEDULED;
  130. }
  131. static void
  132. timer_set_scheduled(struct timer *timer, unsigned int cpu)
  133. {
  134. atomic_store(&timer->cpu, cpu, ATOMIC_RELAXED);
  135. timer->state = TIMER_TS_SCHEDULED;
  136. }
  137. static bool
  138. timer_running(const struct timer *timer)
  139. {
  140. return timer->state == TIMER_TS_RUNNING;
  141. }
  142. static void
  143. timer_set_running(struct timer *timer)
  144. {
  145. timer->state = TIMER_TS_RUNNING;
  146. }
  147. static bool
  148. timer_done(const struct timer *timer)
  149. {
  150. return timer->state == TIMER_TS_DONE;
  151. }
  152. static void
  153. timer_set_done(struct timer *timer)
  154. {
  155. timer->state = TIMER_TS_DONE;
  156. }
  157. /*
  158. * Timer flags functions.
  159. */
  160. static bool
  161. timer_detached(const struct timer *timer)
  162. {
  163. return timer->flags & TIMER_TF_DETACHED;
  164. }
  165. static void
  166. timer_set_detached(struct timer *timer)
  167. {
  168. timer->flags |= TIMER_TF_DETACHED;
  169. }
  170. static bool
  171. timer_is_intr(const struct timer *timer)
  172. {
  173. return timer->flags & TIMER_TF_INTR;
  174. }
  175. static void
  176. timer_set_intr(struct timer *timer)
  177. {
  178. timer->flags |= TIMER_TF_INTR;
  179. }
  180. static bool
  181. timer_is_high_prio(const struct timer *timer)
  182. {
  183. return timer->flags & TIMER_TF_HIGH_PRIO;
  184. }
  185. static void
  186. timer_set_high_prio(struct timer *timer)
  187. {
  188. timer->flags |= TIMER_TF_HIGH_PRIO;
  189. }
  190. static bool
  191. timer_canceled(const struct timer *timer)
  192. {
  193. return timer->flags & TIMER_TF_CANCELED;
  194. }
  195. static void
  196. timer_set_canceled(struct timer *timer)
  197. {
  198. timer->flags |= TIMER_TF_CANCELED;
  199. }
  200. static void
  201. timer_set_time(struct timer *timer, uint64_t ticks)
  202. {
  203. timer->ticks = ticks;
  204. }
  205. static bool
  206. timer_occurred(const struct timer *timer, uint64_t ref)
  207. {
  208. return clock_time_occurred(timer_get_time(timer), ref);
  209. }
  210. static uintptr_t
  211. timer_hash(uint64_t ticks)
  212. {
  213. return ticks;
  214. }
  215. static void
  216. timer_run(struct timer *timer)
  217. {
  218. struct timer_cpu_data *cpu_data;
  219. unsigned long cpu_flags;
  220. assert(timer_running(timer));
  221. timer->fn(timer);
  222. if (timer_detached(timer)) {
  223. return;
  224. }
  225. cpu_data = timer_lock_cpu_data(timer, &cpu_flags);
  226. /*
  227. * The timer handler may have :
  228. * - rescheduled itself
  229. * - been canceled
  230. * - none of the above
  231. *
  232. * If the handler didn't call a timer function, or if the timer was
  233. * canceled, set the state to done and wake up the joiner, if any.
  234. *
  235. * If the handler rescheduled the timer, nothing must be done. This
  236. * is also true if the timer was canceled after being rescheduled by
  237. * the handler (in this case, cancellation won't wait for a signal).
  238. * These cases can be identified by checking if the timer state is
  239. * different from running.
  240. */
  241. if (timer_running(timer)) {
  242. timer_set_done(timer);
  243. thread_wakeup(timer->joiner);
  244. }
  245. timer_unlock_cpu_data(cpu_data, cpu_flags);
  246. }
  247. static void
  248. timer_run_work(struct work *work)
  249. {
  250. struct timer *timer;
  251. timer = structof(work, struct timer, work);
  252. timer_run(timer);
  253. }
  254. static void
  255. timer_process(struct timer *timer)
  256. {
  257. int work_flags;
  258. if (timer_is_intr(timer)) {
  259. timer_run(timer);
  260. return;
  261. }
  262. if (timer_is_high_prio(timer)) {
  263. work_flags = WORK_HIGHPRIO;
  264. } else {
  265. work_flags = 0;
  266. }
  267. work_init(&timer->work, timer_run_work);
  268. work_schedule(&timer->work, work_flags);
  269. }
  270. static void
  271. timer_bucket_init(struct timer_bucket *bucket)
  272. {
  273. hlist_init(&bucket->timers);
  274. }
  275. static void
  276. timer_bucket_add(struct timer_bucket *bucket, struct timer *timer)
  277. {
  278. hlist_insert_head(&bucket->timers, &timer->node);
  279. }
  280. static void
  281. timer_bucket_remove(struct timer_bucket *bucket, struct timer *timer)
  282. {
  283. (void)bucket;
  284. hlist_remove(&timer->node);
  285. }
  286. static void
  287. timer_cpu_data_init(struct timer_cpu_data *cpu_data, unsigned int cpu)
  288. {
  289. cpu_data->cpu = cpu;
  290. spinlock_init(&cpu_data->lock);
  291. /* See periodic event handling */
  292. cpu_data->last_time = clock_get_time() - 1;
  293. for (size_t i = 0; i < ARRAY_SIZE(cpu_data->htable); i++) {
  294. timer_bucket_init(&cpu_data->htable[i]);
  295. }
  296. }
  297. static struct timer_bucket *
  298. timer_cpu_data_get_bucket(struct timer_cpu_data *cpu_data, uint64_t ticks)
  299. {
  300. uintptr_t index;
  301. index = timer_hash(ticks) & TIMER_HTABLE_MASK;
  302. assert(index < ARRAY_SIZE(cpu_data->htable));
  303. return &cpu_data->htable[index];
  304. }
  305. static void
  306. timer_cpu_data_add(struct timer_cpu_data *cpu_data, struct timer *timer)
  307. {
  308. struct timer_bucket *bucket;
  309. assert(timer_ready(timer));
  310. bucket = timer_cpu_data_get_bucket(cpu_data, timer->ticks);
  311. timer_bucket_add(bucket, timer);
  312. }
  313. static void
  314. timer_cpu_data_remove(struct timer_cpu_data *cpu_data, struct timer *timer)
  315. {
  316. struct timer_bucket *bucket;
  317. assert(timer_scheduled(timer));
  318. bucket = timer_cpu_data_get_bucket(cpu_data, timer->ticks);
  319. timer_bucket_remove(bucket, timer);
  320. }
  321. static void
  322. timer_bucket_filter(struct timer_bucket *bucket, uint64_t now,
  323. struct hlist *timers)
  324. {
  325. struct timer *timer, *tmp;
  326. hlist_for_each_entry_safe(&bucket->timers, timer, tmp, node) {
  327. assert(timer_scheduled(timer));
  328. if (!timer_occurred(timer, now)) {
  329. continue;
  330. }
  331. hlist_remove(&timer->node);
  332. timer_set_running(timer);
  333. hlist_insert_head(timers, &timer->node);
  334. }
  335. }
  336. static int __init
  337. timer_bootstrap(void)
  338. {
  339. timer_cpu_data_init(cpu_local_ptr(timer_cpu_data), 0);
  340. return 0;
  341. }
  342. INIT_OP_DEFINE(timer_bootstrap,
  343. INIT_OP_DEP(cpu_setup, true),
  344. INIT_OP_DEP(spinlock_setup, true));
  345. static int __init
  346. timer_setup(void)
  347. {
  348. for (unsigned int cpu = 1; cpu < cpu_count(); cpu++) {
  349. timer_cpu_data_init(percpu_ptr(timer_cpu_data, cpu), cpu);
  350. }
  351. return 0;
  352. }
  353. INIT_OP_DEFINE(timer_setup,
  354. INIT_OP_DEP(cpu_mp_probe, true),
  355. INIT_OP_DEP(spinlock_setup, true));
  356. void
  357. timer_init(struct timer *timer, timer_fn_t fn, int flags)
  358. {
  359. timer->fn = fn;
  360. timer->cpu = TIMER_INVALID_CPU;
  361. timer->state = TIMER_TS_READY;
  362. timer->flags = 0;
  363. timer->joiner = NULL;
  364. if (flags & TIMER_DETACHED) {
  365. timer_set_detached(timer);
  366. }
  367. if (flags & TIMER_INTR) {
  368. timer_set_intr(timer);
  369. } else if (flags & TIMER_HIGH_PRIO) {
  370. timer_set_high_prio(timer);
  371. }
  372. }
  373. void
  374. timer_schedule(struct timer *timer, uint64_t ticks)
  375. {
  376. struct timer_cpu_data *cpu_data;
  377. unsigned long cpu_flags;
  378. cpu_data = timer_lock_cpu_data(timer, &cpu_flags);
  379. if (cpu_data == NULL) {
  380. cpu_data = timer_cpu_data_acquire(&cpu_flags);
  381. } else {
  382. if (timer_canceled(timer)) {
  383. goto out;
  384. }
  385. /*
  386. * If called from the handler, the timer is running. If rescheduled
  387. * after completion, it's done.
  388. */
  389. if (timer_running(timer) || timer_done(timer)) {
  390. timer_set_ready(timer);
  391. }
  392. }
  393. timer_set_time(timer, ticks);
  394. if (timer_occurred(timer, cpu_data->last_time)) {
  395. ticks = cpu_data->last_time + 1;
  396. }
  397. timer_cpu_data_add(cpu_data, timer);
  398. timer_set_scheduled(timer, cpu_data->cpu);
  399. out:
  400. timer_unlock_cpu_data(cpu_data, cpu_flags);
  401. }
  402. void
  403. timer_cancel(struct timer *timer)
  404. {
  405. struct timer_cpu_data *cpu_data;
  406. unsigned long cpu_flags;
  407. assert(!timer_detached(timer));
  408. cpu_data = timer_lock_cpu_data(timer, &cpu_flags);
  409. assert(timer->joiner == NULL);
  410. timer_set_canceled(timer);
  411. if (timer_scheduled(timer)) {
  412. timer_cpu_data_remove(cpu_data, timer);
  413. } else {
  414. timer->joiner = thread_self();
  415. while (!timer_done(timer)) {
  416. if (timer_is_intr(timer)) {
  417. timer_unlock_cpu_data(cpu_data, cpu_flags);
  418. cpu_pause();
  419. cpu_data = timer_lock_cpu_data(timer, &cpu_flags);
  420. } else {
  421. thread_sleep(&cpu_data->lock, timer, "tmr_cncl");
  422. }
  423. }
  424. assert(timer_done(timer));
  425. timer->joiner = NULL;
  426. }
  427. timer_set_ready(timer);
  428. timer_unlock_cpu_data(cpu_data, cpu_flags);
  429. }
  430. void
  431. timer_report_periodic_event(void)
  432. {
  433. struct timer_cpu_data *cpu_data;
  434. struct timer_bucket *bucket;
  435. struct timer *timer;
  436. struct hlist timers;
  437. uint64_t ticks, now;
  438. assert(thread_check_intr_context());
  439. now = clock_get_time();
  440. hlist_init(&timers);
  441. cpu_data = cpu_local_ptr(timer_cpu_data);
  442. spinlock_lock(&cpu_data->lock);
  443. for (ticks = cpu_data->last_time + 1;
  444. clock_time_occurred(ticks, now);
  445. ticks++) {
  446. bucket = timer_cpu_data_get_bucket(cpu_data, ticks);
  447. timer_bucket_filter(bucket, now, &timers);
  448. }
  449. cpu_data->last_time = now;
  450. spinlock_unlock(&cpu_data->lock);
  451. while (!hlist_empty(&timers)) {
  452. timer = hlist_first_entry(&timers, struct timer, node);
  453. hlist_remove(&timer->node);
  454. timer_process(timer);
  455. }
  456. }