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