xref: /linux/include/linux/poll.h (revision 79790b6818e96c58fe2bffee1b418c16e64e7b80)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
21da177e4SLinus Torvalds #ifndef _LINUX_POLL_H
31da177e4SLinus Torvalds #define _LINUX_POLL_H
41da177e4SLinus Torvalds 
51da177e4SLinus Torvalds 
61da177e4SLinus Torvalds #include <linux/compiler.h>
7a99bbaf5SAlexey Dobriyan #include <linux/ktime.h>
81da177e4SLinus Torvalds #include <linux/wait.h>
91da177e4SLinus Torvalds #include <linux/string.h>
10f23f6e08SAl Viro #include <linux/fs.h>
117c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
12607ca46eSDavid Howells #include <uapi/linux/poll.h>
13e78cd95bSAl Viro #include <uapi/linux/eventpoll.h>
141da177e4SLinus Torvalds 
1570674f95SAndi Kleen /* ~832 bytes of stack space used max in sys_select/sys_poll before allocating
1670674f95SAndi Kleen    additional memory. */
1770674f95SAndi Kleen #define MAX_STACK_ALLOC 832
1870674f95SAndi Kleen #define FRONTEND_STACK_ALLOC	256
1970674f95SAndi Kleen #define SELECT_STACK_ALLOC	FRONTEND_STACK_ALLOC
2070674f95SAndi Kleen #define POLL_STACK_ALLOC	FRONTEND_STACK_ALLOC
2170674f95SAndi Kleen #define WQUEUES_STACK_ALLOC	(MAX_STACK_ALLOC - FRONTEND_STACK_ALLOC)
2270674f95SAndi Kleen #define N_INLINE_POLL_ENTRIES	(WQUEUES_STACK_ALLOC / sizeof(struct poll_table_entry))
2370674f95SAndi Kleen 
24e78cd95bSAl Viro #define DEFAULT_POLLMASK (EPOLLIN | EPOLLOUT | EPOLLRDNORM | EPOLLWRNORM)
25dd23aae4SAlexey Dobriyan 
261da177e4SLinus Torvalds struct poll_table_struct;
271da177e4SLinus Torvalds 
281da177e4SLinus Torvalds /*
291da177e4SLinus Torvalds  * structures and helpers for f_op->poll implementations
301da177e4SLinus Torvalds  */
311da177e4SLinus Torvalds typedef void (*poll_queue_proc)(struct file *, wait_queue_head_t *, struct poll_table_struct *);
321da177e4SLinus Torvalds 
33626cf236SHans Verkuil /*
34626cf236SHans Verkuil  * Do not touch the structure directly, use the access functions
35626cf236SHans Verkuil  * poll_does_not_wait() and poll_requested_events() instead.
36626cf236SHans Verkuil  */
371da177e4SLinus Torvalds typedef struct poll_table_struct {
38626cf236SHans Verkuil 	poll_queue_proc _qproc;
3901699437SAl Viro 	__poll_t _key;
401da177e4SLinus Torvalds } poll_table;
411da177e4SLinus Torvalds 
poll_wait(struct file * filp,wait_queue_head_t * wait_address,poll_table * p)421da177e4SLinus Torvalds static inline void poll_wait(struct file * filp, wait_queue_head_t * wait_address, poll_table *p)
431da177e4SLinus Torvalds {
44626cf236SHans Verkuil 	if (p && p->_qproc && wait_address)
45626cf236SHans Verkuil 		p->_qproc(filp, wait_address, p);
46626cf236SHans Verkuil }
47626cf236SHans Verkuil 
48626cf236SHans Verkuil /*
49626cf236SHans Verkuil  * Return true if it is guaranteed that poll will not wait. This is the case
50626cf236SHans Verkuil  * if the poll() of another file descriptor in the set got an event, so there
51626cf236SHans Verkuil  * is no need for waiting.
52626cf236SHans Verkuil  */
poll_does_not_wait(const poll_table * p)53626cf236SHans Verkuil static inline bool poll_does_not_wait(const poll_table *p)
54626cf236SHans Verkuil {
55626cf236SHans Verkuil 	return p == NULL || p->_qproc == NULL;
56626cf236SHans Verkuil }
57626cf236SHans Verkuil 
58626cf236SHans Verkuil /*
59626cf236SHans Verkuil  * Return the set of events that the application wants to poll for.
60626cf236SHans Verkuil  * This is useful for drivers that need to know whether a DMA transfer has
61626cf236SHans Verkuil  * to be started implicitly on poll(). You typically only want to do that
62626cf236SHans Verkuil  * if the application is actually polling for POLLIN and/or POLLOUT.
63626cf236SHans Verkuil  */
poll_requested_events(const poll_table * p)6401699437SAl Viro static inline __poll_t poll_requested_events(const poll_table *p)
65626cf236SHans Verkuil {
6601699437SAl Viro 	return p ? p->_key : ~(__poll_t)0;
671da177e4SLinus Torvalds }
681da177e4SLinus Torvalds 
init_poll_funcptr(poll_table * pt,poll_queue_proc qproc)691da177e4SLinus Torvalds static inline void init_poll_funcptr(poll_table *pt, poll_queue_proc qproc)
701da177e4SLinus Torvalds {
71626cf236SHans Verkuil 	pt->_qproc = qproc;
7201699437SAl Viro 	pt->_key   = ~(__poll_t)0; /* all events enabled */
731da177e4SLinus Torvalds }
741da177e4SLinus Torvalds 
file_can_poll(struct file * file)753deb642fSChristoph Hellwig static inline bool file_can_poll(struct file *file)
769965ed17SChristoph Hellwig {
77*a11e1d43SLinus Torvalds 	return file->f_op->poll;
789965ed17SChristoph Hellwig }
799965ed17SChristoph Hellwig 
vfs_poll(struct file * file,struct poll_table_struct * pt)80*a11e1d43SLinus Torvalds static inline __poll_t vfs_poll(struct file *file, struct poll_table_struct *pt)
81*a11e1d43SLinus Torvalds {
82*a11e1d43SLinus Torvalds 	if (unlikely(!file->f_op->poll))
83*a11e1d43SLinus Torvalds 		return DEFAULT_POLLMASK;
84*a11e1d43SLinus Torvalds 	return file->f_op->poll(file, pt);
85*a11e1d43SLinus Torvalds }
863deb642fSChristoph Hellwig 
8770674f95SAndi Kleen struct poll_table_entry {
8870674f95SAndi Kleen 	struct file *filp;
89ddc0505fSAl Viro 	__poll_t key;
90ac6424b9SIngo Molnar 	wait_queue_entry_t wait;
9170674f95SAndi Kleen 	wait_queue_head_t *wait_address;
9270674f95SAndi Kleen };
9370674f95SAndi Kleen 
941da177e4SLinus Torvalds /*
95dac36dd8SNamhyung Kim  * Structures and helpers for select/poll syscall
961da177e4SLinus Torvalds  */
971da177e4SLinus Torvalds struct poll_wqueues {
981da177e4SLinus Torvalds 	poll_table pt;
991da177e4SLinus Torvalds 	struct poll_table_page *table;
1005f820f64STejun Heo 	struct task_struct *polling_task;
1015f820f64STejun Heo 	int triggered;
1021da177e4SLinus Torvalds 	int error;
10370674f95SAndi Kleen 	int inline_index;
10470674f95SAndi Kleen 	struct poll_table_entry inline_entries[N_INLINE_POLL_ENTRIES];
1051da177e4SLinus Torvalds };
1061da177e4SLinus Torvalds 
1071da177e4SLinus Torvalds extern void poll_initwait(struct poll_wqueues *pwq);
1081da177e4SLinus Torvalds extern void poll_freewait(struct poll_wqueues *pwq);
109766b9f92SDeepa Dinamani extern u64 select_estimate_accuracy(struct timespec64 *tv);
11095aac7b1SShawn Bohrer 
1119f72949fSDavid Woodhouse #define MAX_INT64_SECONDS (((s64)(~((u64)0)>>1)/HZ)-1)
1129f72949fSDavid Woodhouse 
113a2dcb44cSAl Viro extern int core_sys_select(int n, fd_set __user *inp, fd_set __user *outp,
114766b9f92SDeepa Dinamani 			   fd_set __user *exp, struct timespec64 *end_time);
1151da177e4SLinus Torvalds 
116766b9f92SDeepa Dinamani extern int poll_select_set_timeout(struct timespec64 *to, time64_t sec,
117766b9f92SDeepa Dinamani 				   long nsec);
118b773ad40SThomas Gleixner 
1197a163b21SAl Viro #define __MAP(v, from, to) \
1207a163b21SAl Viro 	(from < to ? (v & from) * (to/from) : (v & from) / (from/to))
1217a163b21SAl Viro 
mangle_poll(__poll_t val)1227a163b21SAl Viro static inline __u16 mangle_poll(__poll_t val)
1237a163b21SAl Viro {
1247a163b21SAl Viro 	__u16 v = (__force __u16)val;
1257a163b21SAl Viro #define M(X) __MAP(v, (__force __u16)EPOLL##X, POLL##X)
1267a163b21SAl Viro 	return M(IN) | M(OUT) | M(PRI) | M(ERR) | M(NVAL) |
1277a163b21SAl Viro 		M(RDNORM) | M(RDBAND) | M(WRNORM) | M(WRBAND) |
1287a163b21SAl Viro 		M(HUP) | M(RDHUP) | M(MSG);
1297a163b21SAl Viro #undef M
1307a163b21SAl Viro }
1317a163b21SAl Viro 
demangle_poll(u16 val)1327a163b21SAl Viro static inline __poll_t demangle_poll(u16 val)
1337a163b21SAl Viro {
1347a163b21SAl Viro #define M(X) (__force __poll_t)__MAP(val, POLL##X, (__force __u16)EPOLL##X)
1357a163b21SAl Viro 	return M(IN) | M(OUT) | M(PRI) | M(ERR) | M(NVAL) |
1367a163b21SAl Viro 		M(RDNORM) | M(RDBAND) | M(WRNORM) | M(WRBAND) |
1377a163b21SAl Viro 		M(HUP) | M(RDHUP) | M(MSG);
1387a163b21SAl Viro #undef M
1397a163b21SAl Viro }
1407a163b21SAl Viro #undef __MAP
1417a163b21SAl Viro 
1427a163b21SAl Viro 
1431da177e4SLinus Torvalds #endif /* _LINUX_POLL_H */
144