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/alarmtimer.h>
12 struct cpu_timer_list {
13 struct list_head entry;
15 struct task_struct *task;
20 * Bit fields within a clockid:
22 * The most significant 29 bits hold either a pid or a file descriptor.
24 * Bit 2 indicates whether a cpu clock refers to a thread or a process.
26 * Bits 1 and 0 give the type: PROF=0, VIRT=1, SCHED=2, or FD=3.
28 * A clockid is invalid if bits 2, 1, and 0 are all set.
30 #define CPUCLOCK_PID(clock) ((pid_t) ~((clock) >> 3))
31 #define CPUCLOCK_PERTHREAD(clock) \
32 (((clock) & (clockid_t) CPUCLOCK_PERTHREAD_MASK) != 0)
34 #define CPUCLOCK_PERTHREAD_MASK 4
35 #define CPUCLOCK_WHICH(clock) ((clock) & (clockid_t) CPUCLOCK_CLOCK_MASK)
36 #define CPUCLOCK_CLOCK_MASK 3
37 #define CPUCLOCK_PROF 0
38 #define CPUCLOCK_VIRT 1
39 #define CPUCLOCK_SCHED 2
40 #define CPUCLOCK_MAX 3
41 #define CLOCKFD CPUCLOCK_MAX
42 #define CLOCKFD_MASK (CPUCLOCK_PERTHREAD_MASK|CPUCLOCK_CLOCK_MASK)
44 static inline clockid_t make_process_cpuclock(const unsigned int pid,
45 const clockid_t clock)
47 return ((~pid) << 3) | clock;
49 static inline clockid_t make_thread_cpuclock(const unsigned int tid,
50 const clockid_t clock)
52 return make_process_cpuclock(tid, clock | CPUCLOCK_PERTHREAD_MASK);
55 static inline clockid_t fd_to_clockid(const int fd)
57 return make_process_cpuclock((unsigned int) fd, CLOCKFD);
60 static inline int clockid_to_fd(const clockid_t clk)
65 #ifdef CONFIG_POSIX_TIMERS
68 * posix_cputimer_base - Container per posix CPU clock
69 * @nextevt: Earliest-expiration cache
70 * @cpu_timers: List heads to queue posix CPU timers
72 struct posix_cputimer_base {
74 struct list_head cpu_timers;
78 * posix_cputimers - Container for posix CPU timer related data
79 * @bases: Base container for posix CPU clocks
81 * Used in task_struct and signal_struct
83 struct posix_cputimers {
84 struct posix_cputimer_base bases[CPUCLOCK_MAX];
87 static inline void posix_cputimers_init(struct posix_cputimers *pct)
89 pct->bases[0].nextevt = U64_MAX;
90 pct->bases[1].nextevt = U64_MAX;
91 pct->bases[2].nextevt = U64_MAX;
92 INIT_LIST_HEAD(&pct->bases[0].cpu_timers);
93 INIT_LIST_HEAD(&pct->bases[1].cpu_timers);
94 INIT_LIST_HEAD(&pct->bases[2].cpu_timers);
97 void posix_cputimers_group_init(struct posix_cputimers *pct, u64 cpu_limit);
99 static inline void posix_cputimers_rt_watchdog(struct posix_cputimers *pct,
102 pct->bases[CPUCLOCK_SCHED].nextevt = runtime;
105 /* Init task static initializer */
106 #define INIT_CPU_TIMERBASE(b) { \
107 .nextevt = U64_MAX, \
108 .cpu_timers = LIST_HEAD_INIT(b.cpu_timers), \
111 #define INIT_CPU_TIMERBASES(b) { \
112 INIT_CPU_TIMERBASE(b[0]), \
113 INIT_CPU_TIMERBASE(b[1]), \
114 INIT_CPU_TIMERBASE(b[2]), \
117 #define INIT_CPU_TIMERS(s) \
118 .posix_cputimers = { \
119 .bases = INIT_CPU_TIMERBASES(s.posix_cputimers.bases), \
122 struct posix_cputimers { };
123 #define INIT_CPU_TIMERS(s)
124 static inline void posix_cputimers_init(struct posix_cputimers *pct) { }
125 static inline void posix_cputimers_group_init(struct posix_cputimers *pct,
129 #define REQUEUE_PENDING 1
132 * struct k_itimer - POSIX.1b interval timer structure.
133 * @list: List head for binding the timer to signals->posix_timers
134 * @t_hash: Entry in the posix timer hash table
135 * @it_lock: Lock protecting the timer
136 * @kclock: Pointer to the k_clock struct handling this timer
137 * @it_clock: The posix timer clock id
138 * @it_id: The posix timer id for identifying the timer
139 * @it_active: Marker that timer is active
140 * @it_overrun: The overrun counter for pending signals
141 * @it_overrun_last: The overrun at the time of the last delivered signal
142 * @it_requeue_pending: Indicator that timer waits for being requeued on
144 * @it_sigev_notify: The notify word of sigevent struct for signal delivery
145 * @it_interval: The interval for periodic timers
146 * @it_signal: Pointer to the creators signal struct
147 * @it_pid: The pid of the process/task targeted by the signal
148 * @it_process: The task to wakeup on clock_nanosleep (CPU timers)
149 * @sigq: Pointer to preallocated sigqueue
150 * @it: Union representing the various posix timer type
152 * @rcu: RCU head for freeing the timer.
155 struct list_head list;
156 struct hlist_node t_hash;
158 const struct k_clock *kclock;
164 int it_requeue_pending;
167 struct signal_struct *it_signal;
170 struct task_struct *it_process;
172 struct sigqueue *sigq;
175 struct hrtimer timer;
177 struct cpu_timer_list cpu;
179 struct alarm alarmtimer;
185 void run_posix_cpu_timers(void);
186 void posix_cpu_timers_exit(struct task_struct *task);
187 void posix_cpu_timers_exit_group(struct task_struct *task);
188 void set_process_cpu_timer(struct task_struct *task, unsigned int clock_idx,
189 u64 *newval, u64 *oldval);
191 void update_rlimit_cpu(struct task_struct *task, unsigned long rlim_new);
193 void posixtimer_rearm(struct kernel_siginfo *info);