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/sched.h>
8 #include <linux/timex.h>
9 #include <linux/alarmtimer.h>
10 
11 struct siginfo;
12 
13 struct cpu_timer_list {
14 	struct list_head entry;
15 	u64 expires, incr;
16 	struct task_struct *task;
17 	int firing;
18 };
19 
20 /*
21  * Bit fields within a clockid:
22  *
23  * The most significant 29 bits hold either a pid or a file descriptor.
24  *
25  * Bit 2 indicates whether a cpu clock refers to a thread or a process.
26  *
27  * Bits 1 and 0 give the type: PROF=0, VIRT=1, SCHED=2, or FD=3.
28  *
29  * A clockid is invalid if bits 2, 1, and 0 are all set.
30  */
31 #define CPUCLOCK_PID(clock)		((pid_t) ~((clock) >> 3))
32 #define CPUCLOCK_PERTHREAD(clock) \
33 	(((clock) & (clockid_t) CPUCLOCK_PERTHREAD_MASK) != 0)
34 
35 #define CPUCLOCK_PERTHREAD_MASK	4
36 #define CPUCLOCK_WHICH(clock)	((clock) & (clockid_t) CPUCLOCK_CLOCK_MASK)
37 #define CPUCLOCK_CLOCK_MASK	3
38 #define CPUCLOCK_PROF		0
39 #define CPUCLOCK_VIRT		1
40 #define CPUCLOCK_SCHED		2
41 #define CPUCLOCK_MAX		3
42 #define CLOCKFD			CPUCLOCK_MAX
43 #define CLOCKFD_MASK		(CPUCLOCK_PERTHREAD_MASK|CPUCLOCK_CLOCK_MASK)
44 
make_process_cpuclock(const unsigned int pid,const clockid_t clock)45 static inline clockid_t make_process_cpuclock(const unsigned int pid,
46 		const clockid_t clock)
47 {
48 	return ((~pid) << 3) | clock;
49 }
make_thread_cpuclock(const unsigned int tid,const clockid_t clock)50 static inline clockid_t make_thread_cpuclock(const unsigned int tid,
51 		const clockid_t clock)
52 {
53 	return make_process_cpuclock(tid, clock | CPUCLOCK_PERTHREAD_MASK);
54 }
55 
fd_to_clockid(const int fd)56 static inline clockid_t fd_to_clockid(const int fd)
57 {
58 	return make_process_cpuclock((unsigned int) fd, CLOCKFD);
59 }
60 
clockid_to_fd(const clockid_t clk)61 static inline int clockid_to_fd(const clockid_t clk)
62 {
63 	return ~(clk >> 3);
64 }
65 
66 #define REQUEUE_PENDING 1
67 
68 /**
69  * struct k_itimer - POSIX.1b interval timer structure.
70  * @list:		List head for binding the timer to signals->posix_timers
71  * @t_hash:		Entry in the posix timer hash table
72  * @it_lock:		Lock protecting the timer
73  * @kclock:		Pointer to the k_clock struct handling this timer
74  * @it_clock:		The posix timer clock id
75  * @it_id:		The posix timer id for identifying the timer
76  * @it_active:		Marker that timer is active
77  * @it_overrun:		The overrun counter for pending signals
78  * @it_overrun_last:	The overrun at the time of the last delivered signal
79  * @it_requeue_pending:	Indicator that timer waits for being requeued on
80  *			signal delivery
81  * @it_sigev_notify:	The notify word of sigevent struct for signal delivery
82  * @it_interval:	The interval for periodic timers
83  * @it_signal:		Pointer to the creators signal struct
84  * @it_pid:		The pid of the process/task targeted by the signal
85  * @it_process:		The task to wakeup on clock_nanosleep (CPU timers)
86  * @sigq:		Pointer to preallocated sigqueue
87  * @it:			Union representing the various posix timer type
88  *			internals. Also used for rcu freeing the timer.
89  */
90 struct k_itimer {
91 	struct list_head	list;
92 	struct hlist_node	t_hash;
93 	spinlock_t		it_lock;
94 	const struct k_clock	*kclock;
95 	clockid_t		it_clock;
96 	timer_t			it_id;
97 	int			it_active;
98 	s64			it_overrun;
99 	s64			it_overrun_last;
100 	int			it_requeue_pending;
101 	int			it_sigev_notify;
102 	ktime_t			it_interval;
103 	struct signal_struct	*it_signal;
104 	union {
105 		struct pid		*it_pid;
106 		struct task_struct	*it_process;
107 	};
108 	struct sigqueue		*sigq;
109 	union {
110 		struct {
111 			struct hrtimer	timer;
112 		} real;
113 		struct cpu_timer_list	cpu;
114 		struct {
115 			struct alarm	alarmtimer;
116 		} alarm;
117 		struct rcu_head		rcu;
118 	} it;
119 };
120 
121 void run_posix_cpu_timers(struct task_struct *task);
122 void posix_cpu_timers_exit(struct task_struct *task);
123 void posix_cpu_timers_exit_group(struct task_struct *task);
124 void set_process_cpu_timer(struct task_struct *task, unsigned int clock_idx,
125 			   u64 *newval, u64 *oldval);
126 
127 void update_rlimit_cpu(struct task_struct *task, unsigned long rlim_new);
128 
129 void posixtimer_rearm(struct siginfo *info);
130 #endif
131