blob: f9fbb4c620c1ecdee0e1c28cd64d897196975071 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002#ifndef _linux_POSIX_TIMERS_H
3#define _linux_POSIX_TIMERS_H
4
5#include <linux/spinlock.h>
6#include <linux/list.h>
John Stultz9a7adcf52011-01-11 09:54:33 -08007#include <linux/alarmtimer.h>
Thomas Gleixner60bda032019-08-27 21:31:02 +02008#include <linux/timerqueue.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009
Thomas Gleixnerce03f612019-08-19 16:31:42 +020010struct kernel_siginfo;
11struct task_struct;
Frederic Weisbecker55ccb612013-06-28 00:06:42 +000012
Richard Cochran81e294c2011-02-01 13:52:32 +000013/*
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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#define CPUCLOCK_PID(clock) ((pid_t) ~((clock) >> 3))
25#define CPUCLOCK_PERTHREAD(clock) \
26 (((clock) & (clockid_t) CPUCLOCK_PERTHREAD_MASK) != 0)
Richard Cochran0606f422011-02-01 13:52:35 +000027
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#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
Richard Cochran81e294c2011-02-01 13:52:32 +000035#define CLOCKFD CPUCLOCK_MAX
36#define CLOCKFD_MASK (CPUCLOCK_PERTHREAD_MASK|CPUCLOCK_CLOCK_MASK)
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Nick Desaulniers29f1b2b2017-12-28 22:11:36 -050038static 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}
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Nick Desaulniers29f1b2b2017-12-28 22:11:36 -050049static 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}
Richard Cochran0606f422011-02-01 13:52:35 +000058
Thomas Gleixner2b699422019-08-21 21:09:04 +020059#ifdef CONFIG_POSIX_TIMERS
Thomas Gleixner87dc6442019-08-26 20:22:24 +020060
61/**
Thomas Gleixner60bda032019-08-27 21:31:02 +020062 * 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;
72 struct task_struct *task;
73 struct list_head elist;
74 int firing;
75};
76
77static inline bool cpu_timer_requeue(struct cpu_timer *ctmr)
78{
79 return timerqueue_add(ctmr->head, &ctmr->node);
80}
81
82static inline bool cpu_timer_enqueue(struct timerqueue_head *head,
83 struct cpu_timer *ctmr)
84{
85 ctmr->head = head;
86 return timerqueue_add(head, &ctmr->node);
87}
88
89static inline void cpu_timer_dequeue(struct cpu_timer *ctmr)
90{
91 if (!RB_EMPTY_NODE(&ctmr->node.node))
92 timerqueue_del(ctmr->head, &ctmr->node);
93}
94
95static inline u64 cpu_timer_getexpires(struct cpu_timer *ctmr)
96{
97 return ctmr->node.expires;
98}
99
100static inline void cpu_timer_setexpires(struct cpu_timer *ctmr, u64 exp)
101{
102 ctmr->node.expires = exp;
103}
104
105/**
Thomas Gleixner87dc6442019-08-26 20:22:24 +0200106 * posix_cputimer_base - Container per posix CPU clock
107 * @nextevt: Earliest-expiration cache
Thomas Gleixner60bda032019-08-27 21:31:02 +0200108 * @tqhead: timerqueue head for cpu_timers
Thomas Gleixner87dc6442019-08-26 20:22:24 +0200109 */
110struct posix_cputimer_base {
111 u64 nextevt;
Thomas Gleixner60bda032019-08-27 21:31:02 +0200112 struct timerqueue_head tqhead;
Thomas Gleixner87dc6442019-08-26 20:22:24 +0200113};
114
Thomas Gleixner2b699422019-08-21 21:09:04 +0200115/**
116 * posix_cputimers - Container for posix CPU timer related data
Thomas Gleixner87dc6442019-08-26 20:22:24 +0200117 * @bases: Base container for posix CPU clocks
Thomas Gleixner244d49e2019-08-21 21:09:24 +0200118 * @timers_active: Timers are queued.
119 * @expiry_active: Timer expiry is active. Used for
120 * process wide timers to avoid multiple
121 * task trying to handle expiry concurrently
Thomas Gleixner2b699422019-08-21 21:09:04 +0200122 *
123 * Used in task_struct and signal_struct
124 */
125struct posix_cputimers {
Thomas Gleixner87dc6442019-08-26 20:22:24 +0200126 struct posix_cputimer_base bases[CPUCLOCK_MAX];
Thomas Gleixner244d49e2019-08-21 21:09:24 +0200127 unsigned int timers_active;
128 unsigned int expiry_active;
Thomas Gleixner2b699422019-08-21 21:09:04 +0200129};
130
131static inline void posix_cputimers_init(struct posix_cputimers *pct)
132{
Thomas Gleixner60bda032019-08-27 21:31:02 +0200133 memset(pct, 0, sizeof(*pct));
Thomas Gleixner2bbdbda2019-08-21 21:09:19 +0200134 pct->bases[0].nextevt = U64_MAX;
135 pct->bases[1].nextevt = U64_MAX;
136 pct->bases[2].nextevt = U64_MAX;
Thomas Gleixner2b699422019-08-21 21:09:04 +0200137}
138
Thomas Gleixner3a245c02019-08-21 21:09:06 +0200139void posix_cputimers_group_init(struct posix_cputimers *pct, u64 cpu_limit);
140
141static inline void posix_cputimers_rt_watchdog(struct posix_cputimers *pct,
142 u64 runtime)
143{
Thomas Gleixner87dc6442019-08-26 20:22:24 +0200144 pct->bases[CPUCLOCK_SCHED].nextevt = runtime;
Thomas Gleixner3a245c02019-08-21 21:09:06 +0200145}
146
Thomas Gleixner2b699422019-08-21 21:09:04 +0200147/* Init task static initializer */
Thomas Gleixner87dc6442019-08-26 20:22:24 +0200148#define INIT_CPU_TIMERBASE(b) { \
Thomas Gleixner2bbdbda2019-08-21 21:09:19 +0200149 .nextevt = U64_MAX, \
Thomas Gleixner87dc6442019-08-26 20:22:24 +0200150}
151
152#define INIT_CPU_TIMERBASES(b) { \
153 INIT_CPU_TIMERBASE(b[0]), \
154 INIT_CPU_TIMERBASE(b[1]), \
155 INIT_CPU_TIMERBASE(b[2]), \
Thomas Gleixner2b699422019-08-21 21:09:04 +0200156}
157
158#define INIT_CPU_TIMERS(s) \
159 .posix_cputimers = { \
Thomas Gleixner87dc6442019-08-26 20:22:24 +0200160 .bases = INIT_CPU_TIMERBASES(s.posix_cputimers.bases), \
Thomas Gleixner2b699422019-08-21 21:09:04 +0200161 },
162#else
163struct posix_cputimers { };
164#define INIT_CPU_TIMERS(s)
Thomas Gleixner3a245c02019-08-21 21:09:06 +0200165static inline void posix_cputimers_init(struct posix_cputimers *pct) { }
166static inline void posix_cputimers_group_init(struct posix_cputimers *pct,
167 u64 cpu_limit) { }
Thomas Gleixner2b699422019-08-21 21:09:04 +0200168#endif
169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170#define REQUEUE_PENDING 1
Thomas Gleixner03676b41a2017-05-30 23:15:40 +0200171
172/**
173 * struct k_itimer - POSIX.1b interval timer structure.
174 * @list: List head for binding the timer to signals->posix_timers
175 * @t_hash: Entry in the posix timer hash table
176 * @it_lock: Lock protecting the timer
Thomas Gleixnerd97bb752017-05-30 23:15:44 +0200177 * @kclock: Pointer to the k_clock struct handling this timer
Thomas Gleixner03676b41a2017-05-30 23:15:40 +0200178 * @it_clock: The posix timer clock id
179 * @it_id: The posix timer id for identifying the timer
Thomas Gleixner21e55c12017-05-30 23:15:48 +0200180 * @it_active: Marker that timer is active
Thomas Gleixner03676b41a2017-05-30 23:15:40 +0200181 * @it_overrun: The overrun counter for pending signals
182 * @it_overrun_last: The overrun at the time of the last delivered signal
183 * @it_requeue_pending: Indicator that timer waits for being requeued on
184 * signal delivery
185 * @it_sigev_notify: The notify word of sigevent struct for signal delivery
Thomas Gleixner80105cd2017-05-30 23:15:43 +0200186 * @it_interval: The interval for periodic timers
Thomas Gleixner03676b41a2017-05-30 23:15:40 +0200187 * @it_signal: Pointer to the creators signal struct
188 * @it_pid: The pid of the process/task targeted by the signal
189 * @it_process: The task to wakeup on clock_nanosleep (CPU timers)
190 * @sigq: Pointer to preallocated sigqueue
191 * @it: Union representing the various posix timer type
Sebastian Andrzej Siewior5d99b322019-07-31 00:33:54 +0200192 * internals.
193 * @rcu: RCU head for freeing the timer.
Thomas Gleixner03676b41a2017-05-30 23:15:40 +0200194 */
195struct k_itimer {
196 struct list_head list;
197 struct hlist_node t_hash;
198 spinlock_t it_lock;
Thomas Gleixnerd97bb752017-05-30 23:15:44 +0200199 const struct k_clock *kclock;
Thomas Gleixner03676b41a2017-05-30 23:15:40 +0200200 clockid_t it_clock;
201 timer_t it_id;
Thomas Gleixner21e55c12017-05-30 23:15:48 +0200202 int it_active;
Thomas Gleixner78c9c4d2018-06-26 15:21:32 +0200203 s64 it_overrun;
204 s64 it_overrun_last;
Thomas Gleixner03676b41a2017-05-30 23:15:40 +0200205 int it_requeue_pending;
206 int it_sigev_notify;
Thomas Gleixner80105cd2017-05-30 23:15:43 +0200207 ktime_t it_interval;
Thomas Gleixner03676b41a2017-05-30 23:15:40 +0200208 struct signal_struct *it_signal;
Oleg Nesterov27af4242008-12-01 14:18:13 -0800209 union {
Thomas Gleixner03676b41a2017-05-30 23:15:40 +0200210 struct pid *it_pid;
211 struct task_struct *it_process;
Oleg Nesterov27af4242008-12-01 14:18:13 -0800212 };
Thomas Gleixner03676b41a2017-05-30 23:15:40 +0200213 struct sigqueue *sigq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 union {
215 struct {
Thomas Gleixner03676b41a2017-05-30 23:15:40 +0200216 struct hrtimer timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 } real;
Thomas Gleixner60bda032019-08-27 21:31:02 +0200218 struct cpu_timer cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 struct {
Thomas Gleixner03676b41a2017-05-30 23:15:40 +0200220 struct alarm alarmtimer;
John Stultz9e264762011-08-10 12:09:24 -0700221 } alarm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 } it;
Sebastian Andrzej Siewior5d99b322019-07-31 00:33:54 +0200223 struct rcu_head rcu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224};
225
Thomas Gleixnerdce3e8fd2019-08-19 16:31:47 +0200226void run_posix_cpu_timers(void);
Thomas Gleixner2a698972006-01-09 20:52:28 -0800227void posix_cpu_timers_exit(struct task_struct *task);
228void posix_cpu_timers_exit_group(struct task_struct *task);
Thomas Gleixner2a698972006-01-09 20:52:28 -0800229void set_process_cpu_timer(struct task_struct *task, unsigned int clock_idx,
Frederic Weisbecker858cf3a2017-01-31 04:09:35 +0100230 u64 *newval, u64 *oldval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Jiri Slaby5ab46b32009-08-28 14:05:12 +0200232void update_rlimit_cpu(struct task_struct *task, unsigned long rlim_new);
Frank Mayharf06febc2008-09-12 09:54:39 -0700233
Eric W. Biedermanae7795b2018-09-25 11:27:20 +0200234void posixtimer_rearm(struct kernel_siginfo *info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235#endif