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