Merge tag 'apparmor-pr-2024-01-18' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6-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
53d31ba8 5#include <linux/alarmtimer.h>
1da177e4 6#include <linux/list.h>
f7abf14f 7#include <linux/mutex.h>
53d31ba8
KO
8#include <linux/posix-timers_types.h>
9#include <linux/spinlock.h>
60bda037 10#include <linux/timerqueue.h>
1da177e4 11
ce03f613
TG
12struct kernel_siginfo;
13struct task_struct;
55ccb616 14
29f1b2b0
ND
15static inline clockid_t make_process_cpuclock(const unsigned int pid,
16 const clockid_t clock)
17{
18 return ((~pid) << 3) | clock;
19}
20static inline clockid_t make_thread_cpuclock(const unsigned int tid,
21 const clockid_t clock)
22{
23 return make_process_cpuclock(tid, clock | CPUCLOCK_PERTHREAD_MASK);
24}
1da177e4 25
29f1b2b0
ND
26static inline clockid_t fd_to_clockid(const int fd)
27{
28 return make_process_cpuclock((unsigned int) fd, CLOCKFD);
29}
30
31static inline int clockid_to_fd(const clockid_t clk)
32{
33 return ~(clk >> 3);
34}
0606f422 35
2b69942f 36#ifdef CONFIG_POSIX_TIMERS
87dc6448 37
60bda037
TG
38/**
39 * cpu_timer - Posix CPU timer representation for k_itimer
40 * @node: timerqueue node to queue in the task/sig
41 * @head: timerqueue head on which this timer is queued
f7abf14f 42 * @pid: Pointer to target task PID
60bda037
TG
43 * @elist: List head for the expiry list
44 * @firing: Timer is currently firing
f7abf14f 45 * @handling: Pointer to the task which handles expiry
60bda037
TG
46 */
47struct cpu_timer {
f7abf14f
TG
48 struct timerqueue_node node;
49 struct timerqueue_head *head;
50 struct pid *pid;
51 struct list_head elist;
52 int firing;
53 struct task_struct __rcu *handling;
60bda037
TG
54};
55
60bda037
TG
56static inline bool cpu_timer_enqueue(struct timerqueue_head *head,
57 struct cpu_timer *ctmr)
58{
59 ctmr->head = head;
60 return timerqueue_add(head, &ctmr->node);
61}
62
ee375328
FW
63static inline bool cpu_timer_queued(struct cpu_timer *ctmr)
64{
65 return !!ctmr->head;
66}
67
175cc3ab 68static inline bool cpu_timer_dequeue(struct cpu_timer *ctmr)
60bda037 69{
ee375328 70 if (cpu_timer_queued(ctmr)) {
60bda037 71 timerqueue_del(ctmr->head, &ctmr->node);
00d9e47f 72 ctmr->head = NULL;
175cc3ab 73 return true;
00d9e47f 74 }
175cc3ab 75 return false;
60bda037
TG
76}
77
78static inline u64 cpu_timer_getexpires(struct cpu_timer *ctmr)
79{
80 return ctmr->node.expires;
81}
82
83static inline void cpu_timer_setexpires(struct cpu_timer *ctmr, u64 exp)
84{
85 ctmr->node.expires = exp;
86}
87
2b69942f
TG
88static inline void posix_cputimers_init(struct posix_cputimers *pct)
89{
60bda037 90 memset(pct, 0, sizeof(*pct));
2bbdbdae
TG
91 pct->bases[0].nextevt = U64_MAX;
92 pct->bases[1].nextevt = U64_MAX;
93 pct->bases[2].nextevt = U64_MAX;
2b69942f
TG
94}
95
3a245c0f
TG
96void posix_cputimers_group_init(struct posix_cputimers *pct, u64 cpu_limit);
97
98static inline void posix_cputimers_rt_watchdog(struct posix_cputimers *pct,
99 u64 runtime)
100{
87dc6448 101 pct->bases[CPUCLOCK_SCHED].nextevt = runtime;
3a245c0f
TG
102}
103
2b69942f 104/* Init task static initializer */
87dc6448 105#define INIT_CPU_TIMERBASE(b) { \
2bbdbdae 106 .nextevt = U64_MAX, \
87dc6448
TG
107}
108
109#define INIT_CPU_TIMERBASES(b) { \
110 INIT_CPU_TIMERBASE(b[0]), \
111 INIT_CPU_TIMERBASE(b[1]), \
112 INIT_CPU_TIMERBASE(b[2]), \
2b69942f
TG
113}
114
115#define INIT_CPU_TIMERS(s) \
116 .posix_cputimers = { \
87dc6448 117 .bases = INIT_CPU_TIMERBASES(s.posix_cputimers.bases), \
2b69942f
TG
118 },
119#else
8f2edb4a 120struct cpu_timer { };
2b69942f 121#define INIT_CPU_TIMERS(s)
3a245c0f
TG
122static inline void posix_cputimers_init(struct posix_cputimers *pct) { }
123static inline void posix_cputimers_group_init(struct posix_cputimers *pct,
124 u64 cpu_limit) { }
2b69942f
TG
125#endif
126
1fb497dd 127#ifdef CONFIG_POSIX_CPU_TIMERS_TASK_WORK
ca7752ca 128void clear_posix_cputimers_work(struct task_struct *p);
1fb497dd
TG
129void posix_cputimers_init_work(void);
130#else
ca7752ca 131static inline void clear_posix_cputimers_work(struct task_struct *p) { }
1fb497dd
TG
132static inline void posix_cputimers_init_work(void) { }
133#endif
134
1da177e4 135#define REQUEUE_PENDING 1
03676b41
TG
136
137/**
138 * struct k_itimer - POSIX.1b interval timer structure.
139 * @list: List head for binding the timer to signals->posix_timers
140 * @t_hash: Entry in the posix timer hash table
141 * @it_lock: Lock protecting the timer
d97bb75d 142 * @kclock: Pointer to the k_clock struct handling this timer
03676b41
TG
143 * @it_clock: The posix timer clock id
144 * @it_id: The posix timer id for identifying the timer
21e55c1f 145 * @it_active: Marker that timer is active
03676b41
TG
146 * @it_overrun: The overrun counter for pending signals
147 * @it_overrun_last: The overrun at the time of the last delivered signal
148 * @it_requeue_pending: Indicator that timer waits for being requeued on
149 * signal delivery
150 * @it_sigev_notify: The notify word of sigevent struct for signal delivery
80105cd0 151 * @it_interval: The interval for periodic timers
03676b41
TG
152 * @it_signal: Pointer to the creators signal struct
153 * @it_pid: The pid of the process/task targeted by the signal
154 * @it_process: The task to wakeup on clock_nanosleep (CPU timers)
155 * @sigq: Pointer to preallocated sigqueue
156 * @it: Union representing the various posix timer type
5d99b32a
SAS
157 * internals.
158 * @rcu: RCU head for freeing the timer.
03676b41
TG
159 */
160struct k_itimer {
161 struct list_head list;
162 struct hlist_node t_hash;
163 spinlock_t it_lock;
d97bb75d 164 const struct k_clock *kclock;
03676b41
TG
165 clockid_t it_clock;
166 timer_t it_id;
21e55c1f 167 int it_active;
78c9c4df
TG
168 s64 it_overrun;
169 s64 it_overrun_last;
03676b41
TG
170 int it_requeue_pending;
171 int it_sigev_notify;
80105cd0 172 ktime_t it_interval;
03676b41 173 struct signal_struct *it_signal;
27af4245 174 union {
03676b41
TG
175 struct pid *it_pid;
176 struct task_struct *it_process;
27af4245 177 };
03676b41 178 struct sigqueue *sigq;
1da177e4
LT
179 union {
180 struct {
03676b41 181 struct hrtimer timer;
1da177e4 182 } real;
60bda037 183 struct cpu_timer cpu;
9e264762 184 struct {
03676b41 185 struct alarm alarmtimer;
9e264762 186 } alarm;
1da177e4 187 } it;
5d99b32a 188 struct rcu_head rcu;
1da177e4
LT
189};
190
dce3e8fd 191void run_posix_cpu_timers(void);
2a698971
TG
192void posix_cpu_timers_exit(struct task_struct *task);
193void posix_cpu_timers_exit_group(struct task_struct *task);
2a698971 194void set_process_cpu_timer(struct task_struct *task, unsigned int clock_idx,
858cf3a8 195 u64 *newval, u64 *oldval);
1da177e4 196
18c91bb2 197int update_rlimit_cpu(struct task_struct *task, unsigned long rlim_new);
f06febc9 198
ae7795bc 199void posixtimer_rearm(struct kernel_siginfo *info);
1da177e4 200#endif