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/posix-timers_types.h>
9#include <linux/spinlock.h>
10#include <linux/timerqueue.h>
11
12struct kernel_siginfo;
13struct task_struct;
14
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}
25
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}
35
36#ifdef CONFIG_POSIX_TIMERS
37
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
42 * @pid:	Pointer to target task PID
43 * @elist:	List head for the expiry list
44 * @firing:	Timer is currently firing
45 * @handling:	Pointer to the task which handles expiry
46 */
47struct cpu_timer {
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;
54};
55
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
63static inline bool cpu_timer_queued(struct cpu_timer *ctmr)
64{
65	return !!ctmr->head;
66}
67
68static inline bool cpu_timer_dequeue(struct cpu_timer *ctmr)
69{
70	if (cpu_timer_queued(ctmr)) {
71		timerqueue_del(ctmr->head, &ctmr->node);
72		ctmr->head = NULL;
73		return true;
74	}
75	return false;
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
88static inline void posix_cputimers_init(struct posix_cputimers *pct)
89{
90	memset(pct, 0, sizeof(*pct));
91	pct->bases[0].nextevt = U64_MAX;
92	pct->bases[1].nextevt = U64_MAX;
93	pct->bases[2].nextevt = U64_MAX;
94}
95
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{
101	pct->bases[CPUCLOCK_SCHED].nextevt = runtime;
102}
103
104/* Init task static initializer */
105#define INIT_CPU_TIMERBASE(b) {						\
106	.nextevt	= U64_MAX,					\
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]),					\
113}
114
115#define INIT_CPU_TIMERS(s)						\
116	.posix_cputimers = {						\
117		.bases = INIT_CPU_TIMERBASES(s.posix_cputimers.bases),	\
118	},
119#else
120struct cpu_timer { };
121#define INIT_CPU_TIMERS(s)
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) { }
125#endif
126
127#ifdef CONFIG_POSIX_CPU_TIMERS_TASK_WORK
128void clear_posix_cputimers_work(struct task_struct *p);
129void posix_cputimers_init_work(void);
130#else
131static inline void clear_posix_cputimers_work(struct task_struct *p) { }
132static inline void posix_cputimers_init_work(void) { }
133#endif
134
135#define REQUEUE_PENDING 1
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
142 * @kclock:		Pointer to the k_clock struct handling this timer
143 * @it_clock:		The posix timer clock id
144 * @it_id:		The posix timer id for identifying the timer
145 * @it_active:		Marker that timer is active
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
151 * @it_interval:	The interval for periodic timers
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
157 *			internals.
158 * @rcu:		RCU head for freeing the timer.
159 */
160struct k_itimer {
161	struct list_head	list;
162	struct hlist_node	t_hash;
163	spinlock_t		it_lock;
164	const struct k_clock	*kclock;
165	clockid_t		it_clock;
166	timer_t			it_id;
167	int			it_active;
168	s64			it_overrun;
169	s64			it_overrun_last;
170	int			it_requeue_pending;
171	int			it_sigev_notify;
172	ktime_t			it_interval;
173	struct signal_struct	*it_signal;
174	union {
175		struct pid		*it_pid;
176		struct task_struct	*it_process;
177	};
178	struct sigqueue		*sigq;
179	union {
180		struct {
181			struct hrtimer	timer;
182		} real;
183		struct cpu_timer	cpu;
184		struct {
185			struct alarm	alarmtimer;
186		} alarm;
187	} it;
188	struct rcu_head		rcu;
189};
190
191void run_posix_cpu_timers(void);
192void posix_cpu_timers_exit(struct task_struct *task);
193void posix_cpu_timers_exit_group(struct task_struct *task);
194void set_process_cpu_timer(struct task_struct *task, unsigned int clock_idx,
195			   u64 *newval, u64 *oldval);
196
197int update_rlimit_cpu(struct task_struct *task, unsigned long rlim_new);
198
199void posixtimer_rearm(struct kernel_siginfo *info);
200#endif
201