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