1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _linux_POSIX_TIMERS_H
3 #define _linux_POSIX_TIMERS_H
5 #include <linux/spinlock.h>
6 #include <linux/list.h>
7 #include <linux/mutex.h>
8 #include <linux/alarmtimer.h>
9 #include <linux/timerqueue.h>
11 struct kernel_siginfo;
15 * Bit fields within a clockid:
17 * The most significant 29 bits hold either a pid or a file descriptor.
19 * Bit 2 indicates whether a cpu clock refers to a thread or a process.
21 * Bits 1 and 0 give the type: PROF=0, VIRT=1, SCHED=2, or FD=3.
23 * A clockid is invalid if bits 2, 1, and 0 are all set.
25 #define CPUCLOCK_PID(clock) ((pid_t) ~((clock) >> 3))
26 #define CPUCLOCK_PERTHREAD(clock) \
27 (((clock) & (clockid_t) CPUCLOCK_PERTHREAD_MASK) != 0)
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)
39 static inline clockid_t make_process_cpuclock(const unsigned int pid,
40 const clockid_t clock)
42 return ((~pid) << 3) | clock;
44 static inline clockid_t make_thread_cpuclock(const unsigned int tid,
45 const clockid_t clock)
47 return make_process_cpuclock(tid, clock | CPUCLOCK_PERTHREAD_MASK);
50 static inline clockid_t fd_to_clockid(const int fd)
52 return make_process_cpuclock((unsigned int) fd, CLOCKFD);
55 static inline int clockid_to_fd(const clockid_t clk)
60 #ifdef CONFIG_POSIX_TIMERS
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 * @pid: Pointer to target task PID
67 * @elist: List head for the expiry list
68 * @firing: Timer is currently firing
69 * @handling: Pointer to the task which handles expiry
72 struct timerqueue_node node;
73 struct timerqueue_head *head;
75 struct list_head elist;
77 struct task_struct __rcu *handling;
80 static inline bool cpu_timer_enqueue(struct timerqueue_head *head,
81 struct cpu_timer *ctmr)
84 return timerqueue_add(head, &ctmr->node);
87 static inline bool cpu_timer_queued(struct cpu_timer *ctmr)
92 static inline bool cpu_timer_dequeue(struct cpu_timer *ctmr)
94 if (cpu_timer_queued(ctmr)) {
95 timerqueue_del(ctmr->head, &ctmr->node);
102 static inline u64 cpu_timer_getexpires(struct cpu_timer *ctmr)
104 return ctmr->node.expires;
107 static inline void cpu_timer_setexpires(struct cpu_timer *ctmr, u64 exp)
109 ctmr->node.expires = exp;
113 * posix_cputimer_base - Container per posix CPU clock
114 * @nextevt: Earliest-expiration cache
115 * @tqhead: timerqueue head for cpu_timers
117 struct posix_cputimer_base {
119 struct timerqueue_head tqhead;
123 * posix_cputimers - Container for posix CPU timer related data
124 * @bases: Base container for posix CPU clocks
125 * @timers_active: Timers are queued.
126 * @expiry_active: Timer expiry is active. Used for
127 * process wide timers to avoid multiple
128 * task trying to handle expiry concurrently
130 * Used in task_struct and signal_struct
132 struct posix_cputimers {
133 struct posix_cputimer_base bases[CPUCLOCK_MAX];
134 unsigned int timers_active;
135 unsigned int expiry_active;
139 * posix_cputimers_work - Container for task work based posix CPU timer expiry
140 * @work: The task work to be scheduled
141 * @mutex: Mutex held around expiry in context of this task work
142 * @scheduled: @work has been scheduled already, no further processing
144 struct posix_cputimers_work {
145 struct callback_head work;
147 unsigned int scheduled;
150 static inline void posix_cputimers_init(struct posix_cputimers *pct)
152 memset(pct, 0, sizeof(*pct));
153 pct->bases[0].nextevt = U64_MAX;
154 pct->bases[1].nextevt = U64_MAX;
155 pct->bases[2].nextevt = U64_MAX;
158 void posix_cputimers_group_init(struct posix_cputimers *pct, u64 cpu_limit);
160 static inline void posix_cputimers_rt_watchdog(struct posix_cputimers *pct,
163 pct->bases[CPUCLOCK_SCHED].nextevt = runtime;
166 /* Init task static initializer */
167 #define INIT_CPU_TIMERBASE(b) { \
168 .nextevt = U64_MAX, \
171 #define INIT_CPU_TIMERBASES(b) { \
172 INIT_CPU_TIMERBASE(b[0]), \
173 INIT_CPU_TIMERBASE(b[1]), \
174 INIT_CPU_TIMERBASE(b[2]), \
177 #define INIT_CPU_TIMERS(s) \
178 .posix_cputimers = { \
179 .bases = INIT_CPU_TIMERBASES(s.posix_cputimers.bases), \
182 struct posix_cputimers { };
183 struct cpu_timer { };
184 #define INIT_CPU_TIMERS(s)
185 static inline void posix_cputimers_init(struct posix_cputimers *pct) { }
186 static inline void posix_cputimers_group_init(struct posix_cputimers *pct,
190 #ifdef CONFIG_POSIX_CPU_TIMERS_TASK_WORK
191 void clear_posix_cputimers_work(struct task_struct *p);
192 void posix_cputimers_init_work(void);
194 static inline void clear_posix_cputimers_work(struct task_struct *p) { }
195 static inline void posix_cputimers_init_work(void) { }
198 #define REQUEUE_PENDING 1
201 * struct k_itimer - POSIX.1b interval timer structure.
202 * @list: List head for binding the timer to signals->posix_timers
203 * @t_hash: Entry in the posix timer hash table
204 * @it_lock: Lock protecting the timer
205 * @kclock: Pointer to the k_clock struct handling this timer
206 * @it_clock: The posix timer clock id
207 * @it_id: The posix timer id for identifying the timer
208 * @it_active: Marker that timer is active
209 * @it_overrun: The overrun counter for pending signals
210 * @it_overrun_last: The overrun at the time of the last delivered signal
211 * @it_requeue_pending: Indicator that timer waits for being requeued on
213 * @it_sigev_notify: The notify word of sigevent struct for signal delivery
214 * @it_interval: The interval for periodic timers
215 * @it_signal: Pointer to the creators signal struct
216 * @it_pid: The pid of the process/task targeted by the signal
217 * @it_process: The task to wakeup on clock_nanosleep (CPU timers)
218 * @sigq: Pointer to preallocated sigqueue
219 * @it: Union representing the various posix timer type
221 * @rcu: RCU head for freeing the timer.
224 struct list_head list;
225 struct hlist_node t_hash;
227 const struct k_clock *kclock;
233 int it_requeue_pending;
236 struct signal_struct *it_signal;
239 struct task_struct *it_process;
241 struct sigqueue *sigq;
244 struct hrtimer timer;
246 struct cpu_timer cpu;
248 struct alarm alarmtimer;
254 void run_posix_cpu_timers(void);
255 void posix_cpu_timers_exit(struct task_struct *task);
256 void posix_cpu_timers_exit_group(struct task_struct *task);
257 void set_process_cpu_timer(struct task_struct *task, unsigned int clock_idx,
258 u64 *newval, u64 *oldval);
260 int update_rlimit_cpu(struct task_struct *task, unsigned long rlim_new);
262 void posixtimer_rearm(struct kernel_siginfo *info);