1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _linux_POSIX_TIMERS_H
3 #define _linux_POSIX_TIMERS_H
5 #include <linux/alarmtimer.h>
6 #include <linux/list.h>
7 #include <linux/mutex.h>
8 #include <linux/posix-timers_types.h>
9 #include <linux/spinlock.h>
10 #include <linux/timerqueue.h>
12 struct kernel_siginfo;
15 static inline clockid_t make_process_cpuclock(const unsigned int pid,
16 const clockid_t clock)
18 return ((~pid) << 3) | clock;
20 static inline clockid_t make_thread_cpuclock(const unsigned int tid,
21 const clockid_t clock)
23 return make_process_cpuclock(tid, clock | CPUCLOCK_PERTHREAD_MASK);
26 static inline clockid_t fd_to_clockid(const int fd)
28 return make_process_cpuclock((unsigned int) fd, CLOCKFD);
31 static inline int clockid_to_fd(const clockid_t clk)
36 #ifdef CONFIG_POSIX_TIMERS
39 * cpu_timer - Posix CPU timer representation for k_itimer
40 * @node: timerqueue node to queue in the task/sig
41 * @head: timerqueue head on which this timer is queued
42 * @pid: Pointer to target task PID
43 * @elist: List head for the expiry list
44 * @firing: Timer is currently firing
45 * @handling: Pointer to the task which handles expiry
48 struct timerqueue_node node;
49 struct timerqueue_head *head;
51 struct list_head elist;
53 struct task_struct __rcu *handling;
56 static inline bool cpu_timer_enqueue(struct timerqueue_head *head,
57 struct cpu_timer *ctmr)
60 return timerqueue_add(head, &ctmr->node);
63 static inline bool cpu_timer_queued(struct cpu_timer *ctmr)
68 static inline bool cpu_timer_dequeue(struct cpu_timer *ctmr)
70 if (cpu_timer_queued(ctmr)) {
71 timerqueue_del(ctmr->head, &ctmr->node);
78 static inline u64 cpu_timer_getexpires(struct cpu_timer *ctmr)
80 return ctmr->node.expires;
83 static inline void cpu_timer_setexpires(struct cpu_timer *ctmr, u64 exp)
85 ctmr->node.expires = exp;
88 static inline void posix_cputimers_init(struct posix_cputimers *pct)
90 memset(pct, 0, sizeof(*pct));
91 pct->bases[0].nextevt = U64_MAX;
92 pct->bases[1].nextevt = U64_MAX;
93 pct->bases[2].nextevt = U64_MAX;
96 void posix_cputimers_group_init(struct posix_cputimers *pct, u64 cpu_limit);
98 static inline void posix_cputimers_rt_watchdog(struct posix_cputimers *pct,
101 pct->bases[CPUCLOCK_SCHED].nextevt = runtime;
104 /* Init task static initializer */
105 #define INIT_CPU_TIMERBASE(b) { \
106 .nextevt = U64_MAX, \
109 #define INIT_CPU_TIMERBASES(b) { \
110 INIT_CPU_TIMERBASE(b[0]), \
111 INIT_CPU_TIMERBASE(b[1]), \
112 INIT_CPU_TIMERBASE(b[2]), \
115 #define INIT_CPU_TIMERS(s) \
116 .posix_cputimers = { \
117 .bases = INIT_CPU_TIMERBASES(s.posix_cputimers.bases), \
120 struct cpu_timer { };
121 #define INIT_CPU_TIMERS(s)
122 static inline void posix_cputimers_init(struct posix_cputimers *pct) { }
123 static inline void posix_cputimers_group_init(struct posix_cputimers *pct,
127 #ifdef CONFIG_POSIX_CPU_TIMERS_TASK_WORK
128 void clear_posix_cputimers_work(struct task_struct *p);
129 void posix_cputimers_init_work(void);
131 static inline void clear_posix_cputimers_work(struct task_struct *p) { }
132 static inline void posix_cputimers_init_work(void) { }
135 #define REQUEUE_PENDING 1
138 * struct k_itimer - POSIX.1b interval timer structure.
139 * @list: List head for binding the timer to signals->posix_timers
140 * @t_hash: Entry in the posix timer hash table
141 * @it_lock: Lock protecting the timer
142 * @kclock: Pointer to the k_clock struct handling this timer
143 * @it_clock: The posix timer clock id
144 * @it_id: The posix timer id for identifying the timer
145 * @it_active: Marker that timer is active
146 * @it_overrun: The overrun counter for pending signals
147 * @it_overrun_last: The overrun at the time of the last delivered signal
148 * @it_requeue_pending: Indicator that timer waits for being requeued on
150 * @it_sigev_notify: The notify word of sigevent struct for signal delivery
151 * @it_interval: The interval for periodic timers
152 * @it_signal: Pointer to the creators signal struct
153 * @it_pid: The pid of the process/task targeted by the signal
154 * @it_process: The task to wakeup on clock_nanosleep (CPU timers)
155 * @sigq: Pointer to preallocated sigqueue
156 * @it: Union representing the various posix timer type
158 * @rcu: RCU head for freeing the timer.
161 struct list_head list;
162 struct hlist_node t_hash;
164 const struct k_clock *kclock;
170 int it_requeue_pending;
173 struct signal_struct *it_signal;
176 struct task_struct *it_process;
178 struct sigqueue *sigq;
181 struct hrtimer timer;
183 struct cpu_timer cpu;
185 struct alarm alarmtimer;
191 void run_posix_cpu_timers(void);
192 void posix_cpu_timers_exit(struct task_struct *task);
193 void posix_cpu_timers_exit_group(struct task_struct *task);
194 void set_process_cpu_timer(struct task_struct *task, unsigned int clock_idx,
195 u64 *newval, u64 *oldval);
197 int update_rlimit_cpu(struct task_struct *task, unsigned long rlim_new);
199 void posixtimer_rearm(struct kernel_siginfo *info);