poll.h 3.9 KB

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