poll.h 4.1 KB

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