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
67 * posix_cputimers - Container for posix CPU timer related data
68 * @cputime_expires: Earliest-expiration cache task_cputime based
69 * @expiries: Earliest-expiration cache array based
70 * @cpu_timers: List heads to queue posix CPU timers
72 * Used in task_struct and signal_struct
74 struct posix_cputimers {
75 /* Temporary union until all users are cleaned up */
77 struct task_cputime cputime_expires;
78 u64 expiries[CPUCLOCK_MAX];
80 struct list_head cpu_timers[CPUCLOCK_MAX];
83 static inline void posix_cputimers_init(struct posix_cputimers *pct)
85 memset(&pct->expiries, 0, sizeof(pct->expiries));
86 INIT_LIST_HEAD(&pct->cpu_timers[0]);
87 INIT_LIST_HEAD(&pct->cpu_timers[1]);
88 INIT_LIST_HEAD(&pct->cpu_timers[2]);
91 void posix_cputimers_group_init(struct posix_cputimers *pct, u64 cpu_limit);
93 static inline void posix_cputimers_rt_watchdog(struct posix_cputimers *pct,
96 pct->expiries[CPUCLOCK_SCHED] = runtime;
99 /* Init task static initializer */
100 #define INIT_CPU_TIMERLISTS(c) { \
101 LIST_HEAD_INIT(c.cpu_timers[0]), \
102 LIST_HEAD_INIT(c.cpu_timers[1]), \
103 LIST_HEAD_INIT(c.cpu_timers[2]), \
106 #define INIT_CPU_TIMERS(s) \
107 .posix_cputimers = { \
108 .cpu_timers = INIT_CPU_TIMERLISTS(s.posix_cputimers), \
111 struct posix_cputimers { };
112 #define INIT_CPU_TIMERS(s)
113 static inline void posix_cputimers_init(struct posix_cputimers *pct) { }
114 static inline void posix_cputimers_group_init(struct posix_cputimers *pct,
118 #define REQUEUE_PENDING 1
121 * struct k_itimer - POSIX.1b interval timer structure.
122 * @list: List head for binding the timer to signals->posix_timers
123 * @t_hash: Entry in the posix timer hash table
124 * @it_lock: Lock protecting the timer
125 * @kclock: Pointer to the k_clock struct handling this timer
126 * @it_clock: The posix timer clock id
127 * @it_id: The posix timer id for identifying the timer
128 * @it_active: Marker that timer is active
129 * @it_overrun: The overrun counter for pending signals
130 * @it_overrun_last: The overrun at the time of the last delivered signal
131 * @it_requeue_pending: Indicator that timer waits for being requeued on
133 * @it_sigev_notify: The notify word of sigevent struct for signal delivery
134 * @it_interval: The interval for periodic timers
135 * @it_signal: Pointer to the creators signal struct
136 * @it_pid: The pid of the process/task targeted by the signal
137 * @it_process: The task to wakeup on clock_nanosleep (CPU timers)
138 * @sigq: Pointer to preallocated sigqueue
139 * @it: Union representing the various posix timer type
141 * @rcu: RCU head for freeing the timer.
144 struct list_head list;
145 struct hlist_node t_hash;
147 const struct k_clock *kclock;
153 int it_requeue_pending;
156 struct signal_struct *it_signal;
159 struct task_struct *it_process;
161 struct sigqueue *sigq;
164 struct hrtimer timer;
166 struct cpu_timer_list cpu;
168 struct alarm alarmtimer;
174 void run_posix_cpu_timers(void);
175 void posix_cpu_timers_exit(struct task_struct *task);
176 void posix_cpu_timers_exit_group(struct task_struct *task);
177 void set_process_cpu_timer(struct task_struct *task, unsigned int clock_idx,
178 u64 *newval, u64 *oldval);
180 void update_rlimit_cpu(struct task_struct *task, unsigned long rlim_new);
182 void posixtimer_rearm(struct kernel_siginfo *info);