1
2#ifndef _linux_POSIX_TIMERS_H
3#define _linux_POSIX_TIMERS_H
4
5#include <linux/spinlock.h>
6#include <linux/list.h>
7#include <linux/alarmtimer.h>
8#include <linux/timerqueue.h>
9#include <linux/task_work.h>
10
11struct kernel_siginfo;
12struct task_struct;
13
14
15
16
17
18
19
20
21
22
23
24
25#define CPUCLOCK_PID(clock) ((pid_t) ~((clock) >> 3))
26#define CPUCLOCK_PERTHREAD(clock) \
27 (((clock) & (clockid_t) CPUCLOCK_PERTHREAD_MASK) != 0)
28
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
36#define CLOCKFD CPUCLOCK_MAX
37#define CLOCKFD_MASK (CPUCLOCK_PERTHREAD_MASK|CPUCLOCK_CLOCK_MASK)
38
39static inline clockid_t make_process_cpuclock(const unsigned int pid,
40 const clockid_t clock)
41{
42 return ((~pid) << 3) | clock;
43}
44static 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}
49
50static inline clockid_t fd_to_clockid(const int fd)
51{
52 return make_process_cpuclock((unsigned int) fd, CLOCKFD);
53}
54
55static inline int clockid_to_fd(const clockid_t clk)
56{
57 return ~(clk >> 3);
58}
59
60#ifdef CONFIG_POSIX_TIMERS
61
62
63
64
65
66
67
68
69
70struct cpu_timer {
71 struct timerqueue_node node;
72 struct timerqueue_head *head;
73 struct pid *pid;
74 struct list_head elist;
75 int firing;
76};
77
78static 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
85static inline void cpu_timer_dequeue(struct cpu_timer *ctmr)
86{
87 if (ctmr->head) {
88 timerqueue_del(ctmr->head, &ctmr->node);
89 ctmr->head = NULL;
90 }
91}
92
93static inline u64 cpu_timer_getexpires(struct cpu_timer *ctmr)
94{
95 return ctmr->node.expires;
96}
97
98static inline void cpu_timer_setexpires(struct cpu_timer *ctmr, u64 exp)
99{
100 ctmr->node.expires = exp;
101}
102
103
104
105
106
107
108struct posix_cputimer_base {
109 u64 nextevt;
110 struct timerqueue_head tqhead;
111};
112
113
114
115
116
117
118
119
120
121
122
123struct posix_cputimers {
124 struct posix_cputimer_base bases[CPUCLOCK_MAX];
125 unsigned int timers_active;
126 unsigned int expiry_active;
127};
128
129
130
131
132
133
134struct posix_cputimers_work {
135 struct callback_head work;
136 unsigned int scheduled;
137};
138
139static inline void posix_cputimers_init(struct posix_cputimers *pct)
140{
141 memset(pct, 0, sizeof(*pct));
142 pct->bases[0].nextevt = U64_MAX;
143 pct->bases[1].nextevt = U64_MAX;
144 pct->bases[2].nextevt = U64_MAX;
145}
146
147void posix_cputimers_group_init(struct posix_cputimers *pct, u64 cpu_limit);
148
149static inline void posix_cputimers_rt_watchdog(struct posix_cputimers *pct,
150 u64 runtime)
151{
152 pct->bases[CPUCLOCK_SCHED].nextevt = runtime;
153}
154
155
156#define INIT_CPU_TIMERBASE(b) { \
157 .nextevt = U64_MAX, \
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]), \
164}
165
166#define INIT_CPU_TIMERS(s) \
167 .posix_cputimers = { \
168 .bases = INIT_CPU_TIMERBASES(s.posix_cputimers.bases), \
169 },
170#else
171struct posix_cputimers { };
172struct cpu_timer { };
173#define INIT_CPU_TIMERS(s)
174static inline void posix_cputimers_init(struct posix_cputimers *pct) { }
175static inline void posix_cputimers_group_init(struct posix_cputimers *pct,
176 u64 cpu_limit) { }
177#endif
178
179#ifdef CONFIG_POSIX_CPU_TIMERS_TASK_WORK
180void posix_cputimers_init_work(void);
181#else
182static inline void posix_cputimers_init_work(void) { }
183#endif
184
185#define REQUEUE_PENDING 1
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210struct k_itimer {
211 struct list_head list;
212 struct hlist_node t_hash;
213 spinlock_t it_lock;
214 const struct k_clock *kclock;
215 clockid_t it_clock;
216 timer_t it_id;
217 int it_active;
218 s64 it_overrun;
219 s64 it_overrun_last;
220 int it_requeue_pending;
221 int it_sigev_notify;
222 ktime_t it_interval;
223 struct signal_struct *it_signal;
224 union {
225 struct pid *it_pid;
226 struct task_struct *it_process;
227 };
228 struct sigqueue *sigq;
229 union {
230 struct {
231 struct hrtimer timer;
232 } real;
233 struct cpu_timer cpu;
234 struct {
235 struct alarm alarmtimer;
236 } alarm;
237 } it;
238 struct rcu_head rcu;
239};
240
241void run_posix_cpu_timers(void);
242void posix_cpu_timers_exit(struct task_struct *task);
243void posix_cpu_timers_exit_group(struct task_struct *task);
244void set_process_cpu_timer(struct task_struct *task, unsigned int clock_idx,
245 u64 *newval, u64 *oldval);
246
247void update_rlimit_cpu(struct task_struct *task, unsigned long rlim_new);
248
249void posixtimer_rearm(struct kernel_siginfo *info);
250#endif
251