3ea920e8fe7f299091d584ee29a77e8b278ab501
[sfrench/cifs-2.6.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/alarmtimer.h>
8
9 struct kernel_siginfo;
10 struct task_struct;
11
12 struct cpu_timer_list {
13         struct list_head entry;
14         u64 expires;
15         struct task_struct *task;
16         int firing;
17 };
18
19 /*
20  * Bit fields within a clockid:
21  *
22  * The most significant 29 bits hold either a pid or a file descriptor.
23  *
24  * Bit 2 indicates whether a cpu clock refers to a thread or a process.
25  *
26  * Bits 1 and 0 give the type: PROF=0, VIRT=1, SCHED=2, or FD=3.
27  *
28  * A clockid is invalid if bits 2, 1, and 0 are all set.
29  */
30 #define CPUCLOCK_PID(clock)             ((pid_t) ~((clock) >> 3))
31 #define CPUCLOCK_PERTHREAD(clock) \
32         (((clock) & (clockid_t) CPUCLOCK_PERTHREAD_MASK) != 0)
33
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)
43
44 static inline clockid_t make_process_cpuclock(const unsigned int pid,
45                 const clockid_t clock)
46 {
47         return ((~pid) << 3) | clock;
48 }
49 static inline clockid_t make_thread_cpuclock(const unsigned int tid,
50                 const clockid_t clock)
51 {
52         return make_process_cpuclock(tid, clock | CPUCLOCK_PERTHREAD_MASK);
53 }
54
55 static inline clockid_t fd_to_clockid(const int fd)
56 {
57         return make_process_cpuclock((unsigned int) fd, CLOCKFD);
58 }
59
60 static inline int clockid_to_fd(const clockid_t clk)
61 {
62         return ~(clk >> 3);
63 }
64
65 #ifdef CONFIG_POSIX_TIMERS
66
67 /**
68  * posix_cputimer_base - Container per posix CPU clock
69  * @nextevt:            Earliest-expiration cache
70  * @cpu_timers:         List heads to queue posix CPU timers
71  */
72 struct posix_cputimer_base {
73         u64                     nextevt;
74         struct list_head        cpu_timers;
75 };
76
77 /**
78  * posix_cputimers - Container for posix CPU timer related data
79  * @bases:              Base container for posix CPU clocks
80  *
81  * Used in task_struct and signal_struct
82  */
83 struct posix_cputimers {
84         struct posix_cputimer_base      bases[CPUCLOCK_MAX];
85 };
86
87 static inline void posix_cputimers_init(struct posix_cputimers *pct)
88 {
89         pct->bases[0].nextevt = U64_MAX;
90         pct->bases[1].nextevt = U64_MAX;
91         pct->bases[2].nextevt = U64_MAX;
92         INIT_LIST_HEAD(&pct->bases[0].cpu_timers);
93         INIT_LIST_HEAD(&pct->bases[1].cpu_timers);
94         INIT_LIST_HEAD(&pct->bases[2].cpu_timers);
95 }
96
97 void posix_cputimers_group_init(struct posix_cputimers *pct, u64 cpu_limit);
98
99 static inline void posix_cputimers_rt_watchdog(struct posix_cputimers *pct,
100                                                u64 runtime)
101 {
102         pct->bases[CPUCLOCK_SCHED].nextevt = runtime;
103 }
104
105 /* Init task static initializer */
106 #define INIT_CPU_TIMERBASE(b) {                                         \
107         .nextevt        = U64_MAX,                                      \
108         .cpu_timers     = LIST_HEAD_INIT(b.cpu_timers),                 \
109 }
110
111 #define INIT_CPU_TIMERBASES(b) {                                        \
112         INIT_CPU_TIMERBASE(b[0]),                                       \
113         INIT_CPU_TIMERBASE(b[1]),                                       \
114         INIT_CPU_TIMERBASE(b[2]),                                       \
115 }
116
117 #define INIT_CPU_TIMERS(s)                                              \
118         .posix_cputimers = {                                            \
119                 .bases = INIT_CPU_TIMERBASES(s.posix_cputimers.bases),  \
120         },
121 #else
122 struct posix_cputimers { };
123 #define INIT_CPU_TIMERS(s)
124 static inline void posix_cputimers_init(struct posix_cputimers *pct) { }
125 static inline void posix_cputimers_group_init(struct posix_cputimers *pct,
126                                               u64 cpu_limit) { }
127 #endif
128
129 #define REQUEUE_PENDING 1
130
131 /**
132  * struct k_itimer - POSIX.1b interval timer structure.
133  * @list:               List head for binding the timer to signals->posix_timers
134  * @t_hash:             Entry in the posix timer hash table
135  * @it_lock:            Lock protecting the timer
136  * @kclock:             Pointer to the k_clock struct handling this timer
137  * @it_clock:           The posix timer clock id
138  * @it_id:              The posix timer id for identifying the timer
139  * @it_active:          Marker that timer is active
140  * @it_overrun:         The overrun counter for pending signals
141  * @it_overrun_last:    The overrun at the time of the last delivered signal
142  * @it_requeue_pending: Indicator that timer waits for being requeued on
143  *                      signal delivery
144  * @it_sigev_notify:    The notify word of sigevent struct for signal delivery
145  * @it_interval:        The interval for periodic timers
146  * @it_signal:          Pointer to the creators signal struct
147  * @it_pid:             The pid of the process/task targeted by the signal
148  * @it_process:         The task to wakeup on clock_nanosleep (CPU timers)
149  * @sigq:               Pointer to preallocated sigqueue
150  * @it:                 Union representing the various posix timer type
151  *                      internals.
152  * @rcu:                RCU head for freeing the timer.
153  */
154 struct k_itimer {
155         struct list_head        list;
156         struct hlist_node       t_hash;
157         spinlock_t              it_lock;
158         const struct k_clock    *kclock;
159         clockid_t               it_clock;
160         timer_t                 it_id;
161         int                     it_active;
162         s64                     it_overrun;
163         s64                     it_overrun_last;
164         int                     it_requeue_pending;
165         int                     it_sigev_notify;
166         ktime_t                 it_interval;
167         struct signal_struct    *it_signal;
168         union {
169                 struct pid              *it_pid;
170                 struct task_struct      *it_process;
171         };
172         struct sigqueue         *sigq;
173         union {
174                 struct {
175                         struct hrtimer  timer;
176                 } real;
177                 struct cpu_timer_list   cpu;
178                 struct {
179                         struct alarm    alarmtimer;
180                 } alarm;
181         } it;
182         struct rcu_head         rcu;
183 };
184
185 void run_posix_cpu_timers(void);
186 void posix_cpu_timers_exit(struct task_struct *task);
187 void posix_cpu_timers_exit_group(struct task_struct *task);
188 void set_process_cpu_timer(struct task_struct *task, unsigned int clock_idx,
189                            u64 *newval, u64 *oldval);
190
191 void update_rlimit_cpu(struct task_struct *task, unsigned long rlim_new);
192
193 void posixtimer_rearm(struct kernel_siginfo *info);
194 #endif