xref: /linux/include/linux/posix-timers.h (revision 3fd6c59042dbba50391e30862beac979491145fe)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _linux_POSIX_TIMERS_H
3 #define _linux_POSIX_TIMERS_H
4 
5 #include <linux/alarmtimer.h>
6 #include <linux/list.h>
7 #include <linux/mutex.h>
8 #include <linux/pid.h>
9 #include <linux/posix-timers_types.h>
10 #include <linux/rcuref.h>
11 #include <linux/spinlock.h>
12 #include <linux/timerqueue.h>
13 
14 struct kernel_siginfo;
15 struct task_struct;
16 struct sigqueue;
17 struct k_itimer;
18 
make_process_cpuclock(const unsigned int pid,const clockid_t clock)19 static inline clockid_t make_process_cpuclock(const unsigned int pid,
20 		const clockid_t clock)
21 {
22 	return ((~pid) << 3) | clock;
23 }
make_thread_cpuclock(const unsigned int tid,const clockid_t clock)24 static inline clockid_t make_thread_cpuclock(const unsigned int tid,
25 		const clockid_t clock)
26 {
27 	return make_process_cpuclock(tid, clock | CPUCLOCK_PERTHREAD_MASK);
28 }
29 
fd_to_clockid(const int fd)30 static inline clockid_t fd_to_clockid(const int fd)
31 {
32 	return make_process_cpuclock((unsigned int) fd, CLOCKFD);
33 }
34 
clockid_to_fd(const clockid_t clk)35 static inline int clockid_to_fd(const clockid_t clk)
36 {
37 	return ~(clk >> 3);
38 }
39 
40 #ifdef CONFIG_POSIX_TIMERS
41 
42 #include <linux/signal_types.h>
43 
44 /**
45  * cpu_timer - Posix CPU timer representation for k_itimer
46  * @node:	timerqueue node to queue in the task/sig
47  * @head:	timerqueue head on which this timer is queued
48  * @pid:	Pointer to target task PID
49  * @elist:	List head for the expiry list
50  * @firing:	Timer is currently firing
51  * @nanosleep:	Timer is used for nanosleep and is not a regular posix-timer
52  * @handling:	Pointer to the task which handles expiry
53  */
54 struct cpu_timer {
55 	struct timerqueue_node		node;
56 	struct timerqueue_head		*head;
57 	struct pid			*pid;
58 	struct list_head		elist;
59 	bool				firing;
60 	bool				nanosleep;
61 	struct task_struct __rcu	*handling;
62 };
63 
cpu_timer_enqueue(struct timerqueue_head * head,struct cpu_timer * ctmr)64 static inline bool cpu_timer_enqueue(struct timerqueue_head *head,
65 				     struct cpu_timer *ctmr)
66 {
67 	ctmr->head = head;
68 	return timerqueue_add(head, &ctmr->node);
69 }
70 
cpu_timer_queued(struct cpu_timer * ctmr)71 static inline bool cpu_timer_queued(struct cpu_timer *ctmr)
72 {
73 	return !!ctmr->head;
74 }
75 
cpu_timer_dequeue(struct cpu_timer * ctmr)76 static inline bool cpu_timer_dequeue(struct cpu_timer *ctmr)
77 {
78 	if (cpu_timer_queued(ctmr)) {
79 		timerqueue_del(ctmr->head, &ctmr->node);
80 		ctmr->head = NULL;
81 		return true;
82 	}
83 	return false;
84 }
85 
cpu_timer_getexpires(struct cpu_timer * ctmr)86 static inline u64 cpu_timer_getexpires(struct cpu_timer *ctmr)
87 {
88 	return ctmr->node.expires;
89 }
90 
cpu_timer_setexpires(struct cpu_timer * ctmr,u64 exp)91 static inline void cpu_timer_setexpires(struct cpu_timer *ctmr, u64 exp)
92 {
93 	ctmr->node.expires = exp;
94 }
95 
posix_cputimers_init(struct posix_cputimers * pct)96 static inline void posix_cputimers_init(struct posix_cputimers *pct)
97 {
98 	memset(pct, 0, sizeof(*pct));
99 	pct->bases[0].nextevt = U64_MAX;
100 	pct->bases[1].nextevt = U64_MAX;
101 	pct->bases[2].nextevt = U64_MAX;
102 }
103 
104 void posix_cputimers_group_init(struct posix_cputimers *pct, u64 cpu_limit);
105 
posix_cputimers_rt_watchdog(struct posix_cputimers * pct,u64 runtime)106 static inline void posix_cputimers_rt_watchdog(struct posix_cputimers *pct,
107 					       u64 runtime)
108 {
109 	pct->bases[CPUCLOCK_SCHED].nextevt = runtime;
110 }
111 
112 void posixtimer_rearm_itimer(struct task_struct *p);
113 bool posixtimer_init_sigqueue(struct sigqueue *q);
114 void posixtimer_send_sigqueue(struct k_itimer *tmr);
115 bool posixtimer_deliver_signal(struct kernel_siginfo *info, struct sigqueue *timer_sigq);
116 void posixtimer_free_timer(struct k_itimer *timer);
117 
118 /* Init task static initializer */
119 #define INIT_CPU_TIMERBASE(b) {						\
120 	.nextevt	= U64_MAX,					\
121 }
122 
123 #define INIT_CPU_TIMERBASES(b) {					\
124 	INIT_CPU_TIMERBASE(b[0]),					\
125 	INIT_CPU_TIMERBASE(b[1]),					\
126 	INIT_CPU_TIMERBASE(b[2]),					\
127 }
128 
129 #define INIT_CPU_TIMERS(s)						\
130 	.posix_cputimers = {						\
131 		.bases = INIT_CPU_TIMERBASES(s.posix_cputimers.bases),	\
132 	},
133 #else
134 struct cpu_timer { };
135 #define INIT_CPU_TIMERS(s)
posix_cputimers_init(struct posix_cputimers * pct)136 static inline void posix_cputimers_init(struct posix_cputimers *pct) { }
posix_cputimers_group_init(struct posix_cputimers * pct,u64 cpu_limit)137 static inline void posix_cputimers_group_init(struct posix_cputimers *pct,
138 					      u64 cpu_limit) { }
posixtimer_rearm_itimer(struct task_struct * p)139 static inline void posixtimer_rearm_itimer(struct task_struct *p) { }
posixtimer_deliver_signal(struct kernel_siginfo * info,struct sigqueue * timer_sigq)140 static inline bool posixtimer_deliver_signal(struct kernel_siginfo *info,
141 					     struct sigqueue *timer_sigq) { return false; }
posixtimer_free_timer(struct k_itimer * timer)142 static inline void posixtimer_free_timer(struct k_itimer *timer) { }
143 #endif
144 
145 #ifdef CONFIG_POSIX_CPU_TIMERS_TASK_WORK
146 void clear_posix_cputimers_work(struct task_struct *p);
147 void posix_cputimers_init_work(void);
148 #else
clear_posix_cputimers_work(struct task_struct * p)149 static inline void clear_posix_cputimers_work(struct task_struct *p) { }
posix_cputimers_init_work(void)150 static inline void posix_cputimers_init_work(void) { }
151 #endif
152 
153 /**
154  * struct k_itimer - POSIX.1b interval timer structure.
155  * @list:		List node for binding the timer to tsk::signal::posix_timers
156  * @ignored_list:	List node for tracking ignored timers in tsk::signal::ignored_posix_timers
157  * @t_hash:		Entry in the posix timer hash table
158  * @it_lock:		Lock protecting the timer
159  * @kclock:		Pointer to the k_clock struct handling this timer
160  * @it_clock:		The posix timer clock id
161  * @it_id:		The posix timer id for identifying the timer
162  * @it_status:		The status of the timer
163  * @it_sig_periodic:	The periodic status at signal delivery
164  * @it_overrun:		The overrun counter for pending signals
165  * @it_overrun_last:	The overrun at the time of the last delivered signal
166  * @it_signal_seq:	Sequence count to control signal delivery
167  * @it_sigqueue_seq:	The sequence count at the point where the signal was queued
168  * @it_sigev_notify:	The notify word of sigevent struct for signal delivery
169  * @it_interval:	The interval for periodic timers
170  * @it_signal:		Pointer to the creators signal struct
171  * @it_pid:		The pid of the process/task targeted by the signal
172  * @it_process:		The task to wakeup on clock_nanosleep (CPU timers)
173  * @rcuref:		Reference count for life time management
174  * @sigq:		Embedded sigqueue
175  * @it:			Union representing the various posix timer type
176  *			internals.
177  * @rcu:		RCU head for freeing the timer.
178  */
179 struct k_itimer {
180 	struct hlist_node	list;
181 	struct hlist_node	ignored_list;
182 	struct hlist_node	t_hash;
183 	spinlock_t		it_lock;
184 	const struct k_clock	*kclock;
185 	clockid_t		it_clock;
186 	timer_t			it_id;
187 	int			it_status;
188 	bool			it_sig_periodic;
189 	s64			it_overrun;
190 	s64			it_overrun_last;
191 	unsigned int		it_signal_seq;
192 	unsigned int		it_sigqueue_seq;
193 	int			it_sigev_notify;
194 	enum pid_type		it_pid_type;
195 	ktime_t			it_interval;
196 	struct signal_struct	*it_signal;
197 	union {
198 		struct pid		*it_pid;
199 		struct task_struct	*it_process;
200 	};
201 	struct sigqueue		sigq;
202 	rcuref_t		rcuref;
203 	union {
204 		struct {
205 			struct hrtimer	timer;
206 		} real;
207 		struct cpu_timer	cpu;
208 		struct {
209 			struct alarm	alarmtimer;
210 		} alarm;
211 	} it;
212 	struct rcu_head		rcu;
213 };
214 
215 void run_posix_cpu_timers(void);
216 void posix_cpu_timers_exit(struct task_struct *task);
217 void posix_cpu_timers_exit_group(struct task_struct *task);
218 void set_process_cpu_timer(struct task_struct *task, unsigned int clock_idx,
219 			   u64 *newval, u64 *oldval);
220 
221 int update_rlimit_cpu(struct task_struct *task, unsigned long rlim_new);
222 
223 #ifdef CONFIG_POSIX_TIMERS
posixtimer_putref(struct k_itimer * tmr)224 static inline void posixtimer_putref(struct k_itimer *tmr)
225 {
226 	if (rcuref_put(&tmr->rcuref))
227 		posixtimer_free_timer(tmr);
228 }
229 
posixtimer_sigqueue_getref(struct sigqueue * q)230 static inline void posixtimer_sigqueue_getref(struct sigqueue *q)
231 {
232 	struct k_itimer *tmr = container_of(q, struct k_itimer, sigq);
233 
234 	WARN_ON_ONCE(!rcuref_get(&tmr->rcuref));
235 }
236 
posixtimer_sigqueue_putref(struct sigqueue * q)237 static inline void posixtimer_sigqueue_putref(struct sigqueue *q)
238 {
239 	struct k_itimer *tmr = container_of(q, struct k_itimer, sigq);
240 
241 	posixtimer_putref(tmr);
242 }
243 #else  /* CONFIG_POSIX_TIMERS */
posixtimer_sigqueue_getref(struct sigqueue * q)244 static inline void posixtimer_sigqueue_getref(struct sigqueue *q) { }
posixtimer_sigqueue_putref(struct sigqueue * q)245 static inline void posixtimer_sigqueue_putref(struct sigqueue *q) { }
246 #endif /* !CONFIG_POSIX_TIMERS */
247 
248 #endif
249