poll.h 4.0 KB

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