posix-timers.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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/mutex.h>
  7. #include <linux/alarmtimer.h>
  8. #include <linux/timerqueue.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. * @pid: Pointer to target task PID
  58. * @elist: List head for the expiry list
  59. * @firing: Timer is currently firing
  60. * @handling: Pointer to the task which handles expiry
  61. */
  62. struct cpu_timer {
  63. struct timerqueue_node node;
  64. struct timerqueue_head *head;
  65. struct pid *pid;
  66. struct list_head elist;
  67. int firing;
  68. struct task_struct __rcu *handling;
  69. };
  70. static inline bool cpu_timer_enqueue(struct timerqueue_head *head,
  71. struct cpu_timer *ctmr)
  72. {
  73. ctmr->head = head;
  74. return timerqueue_add(head, &ctmr->node);
  75. }
  76. static inline bool cpu_timer_queued(struct cpu_timer *ctmr)
  77. {
  78. return !!ctmr->head;
  79. }
  80. static inline bool cpu_timer_dequeue(struct cpu_timer *ctmr)
  81. {
  82. if (cpu_timer_queued(ctmr)) {
  83. timerqueue_del(ctmr->head, &ctmr->node);
  84. ctmr->head = NULL;
  85. return true;
  86. }
  87. return false;
  88. }
  89. static inline u64 cpu_timer_getexpires(struct cpu_timer *ctmr)
  90. {
  91. return ctmr->node.expires;
  92. }
  93. static inline void cpu_timer_setexpires(struct cpu_timer *ctmr, u64 exp)
  94. {
  95. ctmr->node.expires = exp;
  96. }
  97. /**
  98. * posix_cputimer_base - Container per posix CPU clock
  99. * @nextevt: Earliest-expiration cache
  100. * @tqhead: timerqueue head for cpu_timers
  101. */
  102. struct posix_cputimer_base {
  103. u64 nextevt;
  104. struct timerqueue_head tqhead;
  105. };
  106. /**
  107. * posix_cputimers - Container for posix CPU timer related data
  108. * @bases: Base container for posix CPU clocks
  109. * @timers_active: Timers are queued.
  110. * @expiry_active: Timer expiry is active. Used for
  111. * process wide timers to avoid multiple
  112. * task trying to handle expiry concurrently
  113. *
  114. * Used in task_struct and signal_struct
  115. */
  116. struct posix_cputimers {
  117. struct posix_cputimer_base bases[CPUCLOCK_MAX];
  118. unsigned int timers_active;
  119. unsigned int expiry_active;
  120. };
  121. /**
  122. * posix_cputimers_work - Container for task work based posix CPU timer expiry
  123. * @work: The task work to be scheduled
  124. * @mutex: Mutex held around expiry in context of this task work
  125. * @scheduled: @work has been scheduled already, no further processing
  126. */
  127. struct posix_cputimers_work {
  128. struct callback_head work;
  129. struct mutex mutex;
  130. unsigned int scheduled;
  131. };
  132. static inline void posix_cputimers_init(struct posix_cputimers *pct)
  133. {
  134. memset(pct, 0, sizeof(*pct));
  135. pct->bases[0].nextevt = U64_MAX;
  136. pct->bases[1].nextevt = U64_MAX;
  137. pct->bases[2].nextevt = U64_MAX;
  138. }
  139. void posix_cputimers_group_init(struct posix_cputimers *pct, u64 cpu_limit);
  140. static inline void posix_cputimers_rt_watchdog(struct posix_cputimers *pct,
  141. u64 runtime)
  142. {
  143. pct->bases[CPUCLOCK_SCHED].nextevt = runtime;
  144. }
  145. /* Init task static initializer */
  146. #define INIT_CPU_TIMERBASE(b) { \
  147. .nextevt = U64_MAX, \
  148. }
  149. #define INIT_CPU_TIMERBASES(b) { \
  150. INIT_CPU_TIMERBASE(b[0]), \
  151. INIT_CPU_TIMERBASE(b[1]), \
  152. INIT_CPU_TIMERBASE(b[2]), \
  153. }
  154. #define INIT_CPU_TIMERS(s) \
  155. .posix_cputimers = { \
  156. .bases = INIT_CPU_TIMERBASES(s.posix_cputimers.bases), \
  157. },
  158. #else
  159. struct posix_cputimers { };
  160. struct cpu_timer { };
  161. #define INIT_CPU_TIMERS(s)
  162. static inline void posix_cputimers_init(struct posix_cputimers *pct) { }
  163. static inline void posix_cputimers_group_init(struct posix_cputimers *pct,
  164. u64 cpu_limit) { }
  165. #endif
  166. #ifdef CONFIG_POSIX_CPU_TIMERS_TASK_WORK
  167. void clear_posix_cputimers_work(struct task_struct *p);
  168. void posix_cputimers_init_work(void);
  169. #else
  170. static inline void clear_posix_cputimers_work(struct task_struct *p) { }
  171. static inline void posix_cputimers_init_work(void) { }
  172. #endif
  173. #define REQUEUE_PENDING 1
  174. /**
  175. * struct k_itimer - POSIX.1b interval timer structure.
  176. * @list: List head for binding the timer to signals->posix_timers
  177. * @t_hash: Entry in the posix timer hash table
  178. * @it_lock: Lock protecting the timer
  179. * @kclock: Pointer to the k_clock struct handling this timer
  180. * @it_clock: The posix timer clock id
  181. * @it_id: The posix timer id for identifying the timer
  182. * @it_active: Marker that timer is active
  183. * @it_overrun: The overrun counter for pending signals
  184. * @it_overrun_last: The overrun at the time of the last delivered signal
  185. * @it_requeue_pending: Indicator that timer waits for being requeued on
  186. * signal delivery
  187. * @it_sigev_notify: The notify word of sigevent struct for signal delivery
  188. * @it_interval: The interval for periodic timers
  189. * @it_signal: Pointer to the creators signal struct
  190. * @it_pid: The pid of the process/task targeted by the signal
  191. * @it_process: The task to wakeup on clock_nanosleep (CPU timers)
  192. * @sigq: Pointer to preallocated sigqueue
  193. * @it: Union representing the various posix timer type
  194. * internals.
  195. * @rcu: RCU head for freeing the timer.
  196. */
  197. struct k_itimer {
  198. struct list_head list;
  199. struct hlist_node t_hash;
  200. spinlock_t it_lock;
  201. const struct k_clock *kclock;
  202. clockid_t it_clock;
  203. timer_t it_id;
  204. int it_active;
  205. s64 it_overrun;
  206. s64 it_overrun_last;
  207. int it_requeue_pending;
  208. int it_sigev_notify;
  209. ktime_t it_interval;
  210. struct signal_struct *it_signal;
  211. union {
  212. struct pid *it_pid;
  213. struct task_struct *it_process;
  214. };
  215. struct sigqueue *sigq;
  216. union {
  217. struct {
  218. struct hrtimer timer;
  219. } real;
  220. struct cpu_timer cpu;
  221. struct {
  222. struct alarm alarmtimer;
  223. } alarm;
  224. } it;
  225. struct rcu_head rcu;
  226. };
  227. void run_posix_cpu_timers(void);
  228. void posix_cpu_timers_exit(struct task_struct *task);
  229. void posix_cpu_timers_exit_group(struct task_struct *task);
  230. void set_process_cpu_timer(struct task_struct *task, unsigned int clock_idx,
  231. u64 *newval, u64 *oldval);
  232. int update_rlimit_cpu(struct task_struct *task, unsigned long rlim_new);
  233. void posixtimer_rearm(struct kernel_siginfo *info);
  234. #endif