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