1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 */
2*4882a593Smuzhiyun #ifndef _LINUX_POLL_H
3*4882a593Smuzhiyun #define _LINUX_POLL_H
4*4882a593Smuzhiyun
5*4882a593Smuzhiyun
6*4882a593Smuzhiyun #include <linux/compiler.h>
7*4882a593Smuzhiyun #include <linux/ktime.h>
8*4882a593Smuzhiyun #include <linux/wait.h>
9*4882a593Smuzhiyun #include <linux/string.h>
10*4882a593Smuzhiyun #include <linux/fs.h>
11*4882a593Smuzhiyun #include <linux/sysctl.h>
12*4882a593Smuzhiyun #include <linux/uaccess.h>
13*4882a593Smuzhiyun #include <uapi/linux/poll.h>
14*4882a593Smuzhiyun #include <uapi/linux/eventpoll.h>
15*4882a593Smuzhiyun
16*4882a593Smuzhiyun extern struct ctl_table epoll_table[]; /* for sysctl */
17*4882a593Smuzhiyun /* ~832 bytes of stack space used max in sys_select/sys_poll before allocating
18*4882a593Smuzhiyun additional memory. */
19*4882a593Smuzhiyun #ifdef __clang__
20*4882a593Smuzhiyun #define MAX_STACK_ALLOC 768
21*4882a593Smuzhiyun #else
22*4882a593Smuzhiyun #define MAX_STACK_ALLOC 832
23*4882a593Smuzhiyun #endif
24*4882a593Smuzhiyun #define FRONTEND_STACK_ALLOC 256
25*4882a593Smuzhiyun #define SELECT_STACK_ALLOC FRONTEND_STACK_ALLOC
26*4882a593Smuzhiyun #define POLL_STACK_ALLOC FRONTEND_STACK_ALLOC
27*4882a593Smuzhiyun #define WQUEUES_STACK_ALLOC (MAX_STACK_ALLOC - FRONTEND_STACK_ALLOC)
28*4882a593Smuzhiyun #define N_INLINE_POLL_ENTRIES (WQUEUES_STACK_ALLOC / sizeof(struct poll_table_entry))
29*4882a593Smuzhiyun
30*4882a593Smuzhiyun #define DEFAULT_POLLMASK (EPOLLIN | EPOLLOUT | EPOLLRDNORM | EPOLLWRNORM)
31*4882a593Smuzhiyun
32*4882a593Smuzhiyun struct poll_table_struct;
33*4882a593Smuzhiyun
34*4882a593Smuzhiyun /*
35*4882a593Smuzhiyun * structures and helpers for f_op->poll implementations
36*4882a593Smuzhiyun */
37*4882a593Smuzhiyun typedef void (*poll_queue_proc)(struct file *, wait_queue_head_t *, struct poll_table_struct *);
38*4882a593Smuzhiyun
39*4882a593Smuzhiyun /*
40*4882a593Smuzhiyun * Do not touch the structure directly, use the access functions
41*4882a593Smuzhiyun * poll_does_not_wait() and poll_requested_events() instead.
42*4882a593Smuzhiyun */
43*4882a593Smuzhiyun typedef struct poll_table_struct {
44*4882a593Smuzhiyun poll_queue_proc _qproc;
45*4882a593Smuzhiyun __poll_t _key;
46*4882a593Smuzhiyun } poll_table;
47*4882a593Smuzhiyun
poll_wait(struct file * filp,wait_queue_head_t * wait_address,poll_table * p)48*4882a593Smuzhiyun static inline void poll_wait(struct file * filp, wait_queue_head_t * wait_address, poll_table *p)
49*4882a593Smuzhiyun {
50*4882a593Smuzhiyun if (p && p->_qproc && wait_address)
51*4882a593Smuzhiyun p->_qproc(filp, wait_address, p);
52*4882a593Smuzhiyun }
53*4882a593Smuzhiyun
54*4882a593Smuzhiyun /*
55*4882a593Smuzhiyun * Return true if it is guaranteed that poll will not wait. This is the case
56*4882a593Smuzhiyun * if the poll() of another file descriptor in the set got an event, so there
57*4882a593Smuzhiyun * is no need for waiting.
58*4882a593Smuzhiyun */
poll_does_not_wait(const poll_table * p)59*4882a593Smuzhiyun static inline bool poll_does_not_wait(const poll_table *p)
60*4882a593Smuzhiyun {
61*4882a593Smuzhiyun return p == NULL || p->_qproc == NULL;
62*4882a593Smuzhiyun }
63*4882a593Smuzhiyun
64*4882a593Smuzhiyun /*
65*4882a593Smuzhiyun * Return the set of events that the application wants to poll for.
66*4882a593Smuzhiyun * This is useful for drivers that need to know whether a DMA transfer has
67*4882a593Smuzhiyun * to be started implicitly on poll(). You typically only want to do that
68*4882a593Smuzhiyun * if the application is actually polling for POLLIN and/or POLLOUT.
69*4882a593Smuzhiyun */
poll_requested_events(const poll_table * p)70*4882a593Smuzhiyun static inline __poll_t poll_requested_events(const poll_table *p)
71*4882a593Smuzhiyun {
72*4882a593Smuzhiyun return p ? p->_key : ~(__poll_t)0;
73*4882a593Smuzhiyun }
74*4882a593Smuzhiyun
init_poll_funcptr(poll_table * pt,poll_queue_proc qproc)75*4882a593Smuzhiyun static inline void init_poll_funcptr(poll_table *pt, poll_queue_proc qproc)
76*4882a593Smuzhiyun {
77*4882a593Smuzhiyun pt->_qproc = qproc;
78*4882a593Smuzhiyun pt->_key = ~(__poll_t)0; /* all events enabled */
79*4882a593Smuzhiyun }
80*4882a593Smuzhiyun
file_can_poll(struct file * file)81*4882a593Smuzhiyun static inline bool file_can_poll(struct file *file)
82*4882a593Smuzhiyun {
83*4882a593Smuzhiyun return file->f_op->poll;
84*4882a593Smuzhiyun }
85*4882a593Smuzhiyun
vfs_poll(struct file * file,struct poll_table_struct * pt)86*4882a593Smuzhiyun static inline __poll_t vfs_poll(struct file *file, struct poll_table_struct *pt)
87*4882a593Smuzhiyun {
88*4882a593Smuzhiyun if (unlikely(!file->f_op->poll))
89*4882a593Smuzhiyun return DEFAULT_POLLMASK;
90*4882a593Smuzhiyun return file->f_op->poll(file, pt);
91*4882a593Smuzhiyun }
92*4882a593Smuzhiyun
93*4882a593Smuzhiyun struct poll_table_entry {
94*4882a593Smuzhiyun struct file *filp;
95*4882a593Smuzhiyun __poll_t key;
96*4882a593Smuzhiyun wait_queue_entry_t wait;
97*4882a593Smuzhiyun wait_queue_head_t *wait_address;
98*4882a593Smuzhiyun };
99*4882a593Smuzhiyun
100*4882a593Smuzhiyun /*
101*4882a593Smuzhiyun * Structures and helpers for select/poll syscall
102*4882a593Smuzhiyun */
103*4882a593Smuzhiyun struct poll_wqueues {
104*4882a593Smuzhiyun poll_table pt;
105*4882a593Smuzhiyun struct poll_table_page *table;
106*4882a593Smuzhiyun struct task_struct *polling_task;
107*4882a593Smuzhiyun int triggered;
108*4882a593Smuzhiyun int error;
109*4882a593Smuzhiyun int inline_index;
110*4882a593Smuzhiyun struct poll_table_entry inline_entries[N_INLINE_POLL_ENTRIES];
111*4882a593Smuzhiyun };
112*4882a593Smuzhiyun
113*4882a593Smuzhiyun extern void poll_initwait(struct poll_wqueues *pwq);
114*4882a593Smuzhiyun extern void poll_freewait(struct poll_wqueues *pwq);
115*4882a593Smuzhiyun extern u64 select_estimate_accuracy(struct timespec64 *tv);
116*4882a593Smuzhiyun
117*4882a593Smuzhiyun #define MAX_INT64_SECONDS (((s64)(~((u64)0)>>1)/HZ)-1)
118*4882a593Smuzhiyun
119*4882a593Smuzhiyun extern int core_sys_select(int n, fd_set __user *inp, fd_set __user *outp,
120*4882a593Smuzhiyun fd_set __user *exp, struct timespec64 *end_time);
121*4882a593Smuzhiyun
122*4882a593Smuzhiyun extern int poll_select_set_timeout(struct timespec64 *to, time64_t sec,
123*4882a593Smuzhiyun long nsec);
124*4882a593Smuzhiyun
125*4882a593Smuzhiyun #define __MAP(v, from, to) \
126*4882a593Smuzhiyun (from < to ? (v & from) * (to/from) : (v & from) / (from/to))
127*4882a593Smuzhiyun
mangle_poll(__poll_t val)128*4882a593Smuzhiyun static inline __u16 mangle_poll(__poll_t val)
129*4882a593Smuzhiyun {
130*4882a593Smuzhiyun __u16 v = (__force __u16)val;
131*4882a593Smuzhiyun #define M(X) __MAP(v, (__force __u16)EPOLL##X, POLL##X)
132*4882a593Smuzhiyun return M(IN) | M(OUT) | M(PRI) | M(ERR) | M(NVAL) |
133*4882a593Smuzhiyun M(RDNORM) | M(RDBAND) | M(WRNORM) | M(WRBAND) |
134*4882a593Smuzhiyun M(HUP) | M(RDHUP) | M(MSG);
135*4882a593Smuzhiyun #undef M
136*4882a593Smuzhiyun }
137*4882a593Smuzhiyun
demangle_poll(u16 val)138*4882a593Smuzhiyun static inline __poll_t demangle_poll(u16 val)
139*4882a593Smuzhiyun {
140*4882a593Smuzhiyun #define M(X) (__force __poll_t)__MAP(val, POLL##X, (__force __u16)EPOLL##X)
141*4882a593Smuzhiyun return M(IN) | M(OUT) | M(PRI) | M(ERR) | M(NVAL) |
142*4882a593Smuzhiyun M(RDNORM) | M(RDBAND) | M(WRNORM) | M(WRBAND) |
143*4882a593Smuzhiyun M(HUP) | M(RDHUP) | M(MSG);
144*4882a593Smuzhiyun #undef M
145*4882a593Smuzhiyun }
146*4882a593Smuzhiyun #undef __MAP
147*4882a593Smuzhiyun
148*4882a593Smuzhiyun
149*4882a593Smuzhiyun #endif /* _LINUX_POLL_H */
150