GNU Linux-libre 5.15.131-gnu
[releases.git] / include / linux / posix-timers.h
1 /* SPDX-License-Identifier: GPL-2.0 */
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/mutex.h>
8 #include <linux/alarmtimer.h>
9 #include <linux/timerqueue.h>
10 #include <linux/task_work.h>
11
12 struct kernel_siginfo;
13 struct task_struct;
14
15 /*
16  * Bit fields within a clockid:
17  *
18  * The most significant 29 bits hold either a pid or a file descriptor.
19  *
20  * Bit 2 indicates whether a cpu clock refers to a thread or a process.
21  *
22  * Bits 1 and 0 give the type: PROF=0, VIRT=1, SCHED=2, or FD=3.
23  *
24  * A clockid is invalid if bits 2, 1, and 0 are all set.
25  */
26 #define CPUCLOCK_PID(clock)             ((pid_t) ~((clock) >> 3))
27 #define CPUCLOCK_PERTHREAD(clock) \
28         (((clock) & (clockid_t) CPUCLOCK_PERTHREAD_MASK) != 0)
29
30 #define CPUCLOCK_PERTHREAD_MASK 4
31 #define CPUCLOCK_WHICH(clock)   ((clock) & (clockid_t) CPUCLOCK_CLOCK_MASK)
32 #define CPUCLOCK_CLOCK_MASK     3
33 #define CPUCLOCK_PROF           0
34 #define CPUCLOCK_VIRT           1
35 #define CPUCLOCK_SCHED          2
36 #define CPUCLOCK_MAX            3
37 #define CLOCKFD                 CPUCLOCK_MAX
38 #define CLOCKFD_MASK            (CPUCLOCK_PERTHREAD_MASK|CPUCLOCK_CLOCK_MASK)
39
40 static inline clockid_t make_process_cpuclock(const unsigned int pid,
41                 const clockid_t clock)
42 {
43         return ((~pid) << 3) | clock;
44 }
45 static inline clockid_t make_thread_cpuclock(const unsigned int tid,
46                 const clockid_t clock)
47 {
48         return make_process_cpuclock(tid, clock | CPUCLOCK_PERTHREAD_MASK);
49 }
50
51 static inline clockid_t fd_to_clockid(const int fd)
52 {
53         return make_process_cpuclock((unsigned int) fd, CLOCKFD);
54 }
55
56 static inline int clockid_to_fd(const clockid_t clk)
57 {
58         return ~(clk >> 3);
59 }
60
61 #ifdef CONFIG_POSIX_TIMERS
62
63 /**
64  * cpu_timer - Posix CPU timer representation for k_itimer
65  * @node:       timerqueue node to queue in the task/sig
66  * @head:       timerqueue head on which this timer is queued
67  * @pid:        Pointer to target task PID
68  * @elist:      List head for the expiry list
69  * @firing:     Timer is currently firing
70  * @handling:   Pointer to the task which handles expiry
71  */
72 struct cpu_timer {
73         struct timerqueue_node          node;
74         struct timerqueue_head          *head;
75         struct pid                      *pid;
76         struct list_head                elist;
77         int                             firing;
78         struct task_struct __rcu        *handling;
79 };
80
81 static inline bool cpu_timer_enqueue(struct timerqueue_head *head,
82                                      struct cpu_timer *ctmr)
83 {
84         ctmr->head = head;
85         return timerqueue_add(head, &ctmr->node);
86 }
87
88 static inline bool cpu_timer_queued(struct cpu_timer *ctmr)
89 {
90         return !!ctmr->head;
91 }
92
93 static inline bool cpu_timer_dequeue(struct cpu_timer *ctmr)
94 {
95         if (cpu_timer_queued(ctmr)) {
96                 timerqueue_del(ctmr->head, &ctmr->node);
97                 ctmr->head = NULL;
98                 return true;
99         }
100         return false;
101 }
102
103 static inline u64 cpu_timer_getexpires(struct cpu_timer *ctmr)
104 {
105         return ctmr->node.expires;
106 }
107
108 static inline void cpu_timer_setexpires(struct cpu_timer *ctmr, u64 exp)
109 {
110         ctmr->node.expires = exp;
111 }
112
113 /**
114  * posix_cputimer_base - Container per posix CPU clock
115  * @nextevt:            Earliest-expiration cache
116  * @tqhead:             timerqueue head for cpu_timers
117  */
118 struct posix_cputimer_base {
119         u64                     nextevt;
120         struct timerqueue_head  tqhead;
121 };
122
123 /**
124  * posix_cputimers - Container for posix CPU timer related data
125  * @bases:              Base container for posix CPU clocks
126  * @timers_active:      Timers are queued.
127  * @expiry_active:      Timer expiry is active. Used for
128  *                      process wide timers to avoid multiple
129  *                      task trying to handle expiry concurrently
130  *
131  * Used in task_struct and signal_struct
132  */
133 struct posix_cputimers {
134         struct posix_cputimer_base      bases[CPUCLOCK_MAX];
135         unsigned int                    timers_active;
136         unsigned int                    expiry_active;
137 };
138
139 /**
140  * posix_cputimers_work - Container for task work based posix CPU timer expiry
141  * @work:       The task work to be scheduled
142  * @mutex:      Mutex held around expiry in context of this task work
143  * @scheduled:  @work has been scheduled already, no further processing
144  */
145 struct posix_cputimers_work {
146         struct callback_head    work;
147         struct mutex            mutex;
148         unsigned int            scheduled;
149 };
150
151 static inline void posix_cputimers_init(struct posix_cputimers *pct)
152 {
153         memset(pct, 0, sizeof(*pct));
154         pct->bases[0].nextevt = U64_MAX;
155         pct->bases[1].nextevt = U64_MAX;
156         pct->bases[2].nextevt = U64_MAX;
157 }
158
159 void posix_cputimers_group_init(struct posix_cputimers *pct, u64 cpu_limit);
160
161 static inline void posix_cputimers_rt_watchdog(struct posix_cputimers *pct,
162                                                u64 runtime)
163 {
164         pct->bases[CPUCLOCK_SCHED].nextevt = runtime;
165 }
166
167 /* Init task static initializer */
168 #define INIT_CPU_TIMERBASE(b) {                                         \
169         .nextevt        = U64_MAX,                                      \
170 }
171
172 #define INIT_CPU_TIMERBASES(b) {                                        \
173         INIT_CPU_TIMERBASE(b[0]),                                       \
174         INIT_CPU_TIMERBASE(b[1]),                                       \
175         INIT_CPU_TIMERBASE(b[2]),                                       \
176 }
177
178 #define INIT_CPU_TIMERS(s)                                              \
179         .posix_cputimers = {                                            \
180                 .bases = INIT_CPU_TIMERBASES(s.posix_cputimers.bases),  \
181         },
182 #else
183 struct posix_cputimers { };
184 struct cpu_timer { };
185 #define INIT_CPU_TIMERS(s)
186 static inline void posix_cputimers_init(struct posix_cputimers *pct) { }
187 static inline void posix_cputimers_group_init(struct posix_cputimers *pct,
188                                               u64 cpu_limit) { }
189 #endif
190
191 #ifdef CONFIG_POSIX_CPU_TIMERS_TASK_WORK
192 void clear_posix_cputimers_work(struct task_struct *p);
193 void posix_cputimers_init_work(void);
194 #else
195 static inline void clear_posix_cputimers_work(struct task_struct *p) { }
196 static inline void posix_cputimers_init_work(void) { }
197 #endif
198
199 #define REQUEUE_PENDING 1
200
201 /**
202  * struct k_itimer - POSIX.1b interval timer structure.
203  * @list:               List head for binding the timer to signals->posix_timers
204  * @t_hash:             Entry in the posix timer hash table
205  * @it_lock:            Lock protecting the timer
206  * @kclock:             Pointer to the k_clock struct handling this timer
207  * @it_clock:           The posix timer clock id
208  * @it_id:              The posix timer id for identifying the timer
209  * @it_active:          Marker that timer is active
210  * @it_overrun:         The overrun counter for pending signals
211  * @it_overrun_last:    The overrun at the time of the last delivered signal
212  * @it_requeue_pending: Indicator that timer waits for being requeued on
213  *                      signal delivery
214  * @it_sigev_notify:    The notify word of sigevent struct for signal delivery
215  * @it_interval:        The interval for periodic timers
216  * @it_signal:          Pointer to the creators signal struct
217  * @it_pid:             The pid of the process/task targeted by the signal
218  * @it_process:         The task to wakeup on clock_nanosleep (CPU timers)
219  * @sigq:               Pointer to preallocated sigqueue
220  * @it:                 Union representing the various posix timer type
221  *                      internals.
222  * @rcu:                RCU head for freeing the timer.
223  */
224 struct k_itimer {
225         struct list_head        list;
226         struct hlist_node       t_hash;
227         spinlock_t              it_lock;
228         const struct k_clock    *kclock;
229         clockid_t               it_clock;
230         timer_t                 it_id;
231         int                     it_active;
232         s64                     it_overrun;
233         s64                     it_overrun_last;
234         int                     it_requeue_pending;
235         int                     it_sigev_notify;
236         ktime_t                 it_interval;
237         struct signal_struct    *it_signal;
238         union {
239                 struct pid              *it_pid;
240                 struct task_struct      *it_process;
241         };
242         struct sigqueue         *sigq;
243         union {
244                 struct {
245                         struct hrtimer  timer;
246                 } real;
247                 struct cpu_timer        cpu;
248                 struct {
249                         struct alarm    alarmtimer;
250                 } alarm;
251         } it;
252         struct rcu_head         rcu;
253 };
254
255 void run_posix_cpu_timers(void);
256 void posix_cpu_timers_exit(struct task_struct *task);
257 void posix_cpu_timers_exit_group(struct task_struct *task);
258 void set_process_cpu_timer(struct task_struct *task, unsigned int clock_idx,
259                            u64 *newval, u64 *oldval);
260
261 void update_rlimit_cpu(struct task_struct *task, unsigned long rlim_new);
262
263 void posixtimer_rearm(struct kernel_siginfo *info);
264 #endif