posix-timers.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _linux_POSIX_TIMERS_H
  3. #define _linux_POSIX_TIMERS_H
  4. #include <linux/spinlock.h>
  5. #include <linux/list.h>
  6. #include <linux/alarmtimer.h>
  7. #include <linux/timerqueue.h>
  8. #include <linux/task_work.h>
  9. struct kernel_siginfo;
  10. struct task_struct;
  11. /*
  12. * Bit fields within a clockid:
  13. *
  14. * The most significant 29 bits hold either a pid or a file descriptor.
  15. *
  16. * Bit 2 indicates whether a cpu clock refers to a thread or a process.
  17. *
  18. * Bits 1 and 0 give the type: PROF=0, VIRT=1, SCHED=2, or FD=3.
  19. *
  20. * A clockid is invalid if bits 2, 1, and 0 are all set.
  21. */
  22. #define CPUCLOCK_PID(clock) ((pid_t) ~((clock) >> 3))
  23. #define CPUCLOCK_PERTHREAD(clock) \
  24. (((clock) & (clockid_t) CPUCLOCK_PERTHREAD_MASK) != 0)
  25. #define CPUCLOCK_PERTHREAD_MASK 4
  26. #define CPUCLOCK_WHICH(clock) ((clock) & (clockid_t) CPUCLOCK_CLOCK_MASK)
  27. #define CPUCLOCK_CLOCK_MASK 3
  28. #define CPUCLOCK_PROF 0
  29. #define CPUCLOCK_VIRT 1
  30. #define CPUCLOCK_SCHED 2
  31. #define CPUCLOCK_MAX 3
  32. #define CLOCKFD CPUCLOCK_MAX
  33. #define CLOCKFD_MASK (CPUCLOCK_PERTHREAD_MASK|CPUCLOCK_CLOCK_MASK)
  34. static inline clockid_t make_process_cpuclock(const unsigned int pid,
  35. const clockid_t clock)
  36. {
  37. return ((~pid) << 3) | clock;
  38. }
  39. static inline clockid_t make_thread_cpuclock(const unsigned int tid,
  40. const clockid_t clock)
  41. {
  42. return make_process_cpuclock(tid, clock | CPUCLOCK_PERTHREAD_MASK);
  43. }
  44. static inline clockid_t fd_to_clockid(const int fd)
  45. {
  46. return make_process_cpuclock((unsigned int) fd, CLOCKFD);
  47. }
  48. static inline int clockid_to_fd(const clockid_t clk)
  49. {
  50. return ~(clk >> 3);
  51. }
  52. #ifdef CONFIG_POSIX_TIMERS
  53. /**
  54. * cpu_timer - Posix CPU timer representation for k_itimer
  55. * @node: timerqueue node to queue in the task/sig
  56. * @head: timerqueue head on which this timer is queued
  57. * @task: Pointer to target task
  58. * @elist: List head for the expiry list
  59. * @firing: Timer is currently firing
  60. */
  61. struct cpu_timer {
  62. struct timerqueue_node node;
  63. struct timerqueue_head *head;
  64. struct pid *pid;
  65. struct list_head elist;
  66. int firing;
  67. };
  68. static inline bool cpu_timer_enqueue(struct timerqueue_head *head,
  69. struct cpu_timer *ctmr)
  70. {
  71. ctmr->head = head;
  72. return timerqueue_add(head, &ctmr->node);
  73. }
  74. static inline void cpu_timer_dequeue(struct cpu_timer *ctmr)
  75. {
  76. if (ctmr->head) {
  77. timerqueue_del(ctmr->head, &ctmr->node);
  78. ctmr->head = NULL;
  79. }
  80. }
  81. static inline u64 cpu_timer_getexpires(struct cpu_timer *ctmr)
  82. {
  83. return ctmr->node.expires;
  84. }
  85. static inline void cpu_timer_setexpires(struct cpu_timer *ctmr, u64 exp)
  86. {
  87. ctmr->node.expires = exp;
  88. }
  89. /**
  90. * posix_cputimer_base - Container per posix CPU clock
  91. * @nextevt: Earliest-expiration cache
  92. * @tqhead: timerqueue head for cpu_timers
  93. */
  94. struct posix_cputimer_base {
  95. u64 nextevt;
  96. struct timerqueue_head tqhead;
  97. };
  98. /**
  99. * posix_cputimers - Container for posix CPU timer related data
  100. * @bases: Base container for posix CPU clocks
  101. * @timers_active: Timers are queued.
  102. * @expiry_active: Timer expiry is active. Used for
  103. * process wide timers to avoid multiple
  104. * task trying to handle expiry concurrently
  105. *
  106. * Used in task_struct and signal_struct
  107. */
  108. struct posix_cputimers {
  109. struct posix_cputimer_base bases[CPUCLOCK_MAX];
  110. unsigned int timers_active;
  111. unsigned int expiry_active;
  112. };
  113. /**
  114. * posix_cputimers_work - Container for task work based posix CPU timer expiry
  115. * @work: The task work to be scheduled
  116. * @scheduled: @work has been scheduled already, no further processing
  117. */
  118. struct posix_cputimers_work {
  119. struct callback_head work;
  120. unsigned int scheduled;
  121. };
  122. static inline void posix_cputimers_init(struct posix_cputimers *pct)
  123. {
  124. memset(pct, 0, sizeof(*pct));
  125. pct->bases[0].nextevt = U64_MAX;
  126. pct->bases[1].nextevt = U64_MAX;
  127. pct->bases[2].nextevt = U64_MAX;
  128. }
  129. void posix_cputimers_group_init(struct posix_cputimers *pct, u64 cpu_limit);
  130. static inline void posix_cputimers_rt_watchdog(struct posix_cputimers *pct,
  131. u64 runtime)
  132. {
  133. pct->bases[CPUCLOCK_SCHED].nextevt = runtime;
  134. }
  135. /* Init task static initializer */
  136. #define INIT_CPU_TIMERBASE(b) { \
  137. .nextevt = U64_MAX, \
  138. }
  139. #define INIT_CPU_TIMERBASES(b) { \
  140. INIT_CPU_TIMERBASE(b[0]), \
  141. INIT_CPU_TIMERBASE(b[1]), \
  142. INIT_CPU_TIMERBASE(b[2]), \
  143. }
  144. #define INIT_CPU_TIMERS(s) \
  145. .posix_cputimers = { \
  146. .bases = INIT_CPU_TIMERBASES(s.posix_cputimers.bases), \
  147. },
  148. #else
  149. struct posix_cputimers { };
  150. struct cpu_timer { };
  151. #define INIT_CPU_TIMERS(s)
  152. static inline void posix_cputimers_init(struct posix_cputimers *pct) { }
  153. static inline void posix_cputimers_group_init(struct posix_cputimers *pct,
  154. u64 cpu_limit) { }
  155. #endif
  156. #ifdef CONFIG_POSIX_CPU_TIMERS_TASK_WORK
  157. void clear_posix_cputimers_work(struct task_struct *p);
  158. void posix_cputimers_init_work(void);
  159. #else
  160. static inline void clear_posix_cputimers_work(struct task_struct *p) { }
  161. static inline void posix_cputimers_init_work(void) { }
  162. #endif
  163. #define REQUEUE_PENDING 1
  164. /**
  165. * struct k_itimer - POSIX.1b interval timer structure.
  166. * @list: List head for binding the timer to signals->posix_timers
  167. * @t_hash: Entry in the posix timer hash table
  168. * @it_lock: Lock protecting the timer
  169. * @kclock: Pointer to the k_clock struct handling this timer
  170. * @it_clock: The posix timer clock id
  171. * @it_id: The posix timer id for identifying the timer
  172. * @it_active: Marker that timer is active
  173. * @it_overrun: The overrun counter for pending signals
  174. * @it_overrun_last: The overrun at the time of the last delivered signal
  175. * @it_requeue_pending: Indicator that timer waits for being requeued on
  176. * signal delivery
  177. * @it_sigev_notify: The notify word of sigevent struct for signal delivery
  178. * @it_interval: The interval for periodic timers
  179. * @it_signal: Pointer to the creators signal struct
  180. * @it_pid: The pid of the process/task targeted by the signal
  181. * @it_process: The task to wakeup on clock_nanosleep (CPU timers)
  182. * @sigq: Pointer to preallocated sigqueue
  183. * @it: Union representing the various posix timer type
  184. * internals.
  185. * @rcu: RCU head for freeing the timer.
  186. */
  187. struct k_itimer {
  188. struct list_head list;
  189. struct hlist_node t_hash;
  190. spinlock_t it_lock;
  191. const struct k_clock *kclock;
  192. clockid_t it_clock;
  193. timer_t it_id;
  194. int it_active;
  195. s64 it_overrun;
  196. s64 it_overrun_last;
  197. int it_requeue_pending;
  198. int it_sigev_notify;
  199. ktime_t it_interval;
  200. struct signal_struct *it_signal;
  201. union {
  202. struct pid *it_pid;
  203. struct task_struct *it_process;
  204. };
  205. struct sigqueue *sigq;
  206. union {
  207. struct {
  208. struct hrtimer timer;
  209. } real;
  210. struct cpu_timer cpu;
  211. struct {
  212. struct alarm alarmtimer;
  213. } alarm;
  214. } it;
  215. struct rcu_head rcu;
  216. };
  217. void run_posix_cpu_timers(void);
  218. void posix_cpu_timers_exit(struct task_struct *task);
  219. void posix_cpu_timers_exit_group(struct task_struct *task);
  220. void set_process_cpu_timer(struct task_struct *task, unsigned int clock_idx,
  221. u64 *newval, u64 *oldval);
  222. void update_rlimit_cpu(struct task_struct *task, unsigned long rlim_new);
  223. void posixtimer_rearm(struct kernel_siginfo *info);
  224. #endif