vhost.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _VHOST_H
  3. #define _VHOST_H
  4. #include <linux/eventfd.h>
  5. #include <linux/vhost.h>
  6. #include <linux/mm.h>
  7. #include <linux/mutex.h>
  8. #include <linux/poll.h>
  9. #include <linux/file.h>
  10. #include <linux/uio.h>
  11. #include <linux/virtio_config.h>
  12. #include <linux/virtio_ring.h>
  13. #include <linux/atomic.h>
  14. struct vhost_work;
  15. typedef void (*vhost_work_fn_t)(struct vhost_work *work);
  16. #define VHOST_WORK_QUEUED 1
  17. struct vhost_work {
  18. struct llist_node node;
  19. vhost_work_fn_t fn;
  20. wait_queue_head_t done;
  21. int flushing;
  22. unsigned queue_seq;
  23. unsigned done_seq;
  24. unsigned long flags;
  25. };
  26. /* Poll a file (eventfd or socket) */
  27. /* Note: there's nothing vhost specific about this structure. */
  28. struct vhost_poll {
  29. poll_table table;
  30. wait_queue_head_t *wqh;
  31. wait_queue_entry_t wait;
  32. struct vhost_work work;
  33. unsigned long mask;
  34. struct vhost_dev *dev;
  35. };
  36. void vhost_work_init(struct vhost_work *work, vhost_work_fn_t fn);
  37. void vhost_work_queue(struct vhost_dev *dev, struct vhost_work *work);
  38. bool vhost_has_work(struct vhost_dev *dev);
  39. void vhost_poll_init(struct vhost_poll *poll, vhost_work_fn_t fn,
  40. unsigned long mask, struct vhost_dev *dev);
  41. int vhost_poll_start(struct vhost_poll *poll, struct file *file);
  42. void vhost_poll_stop(struct vhost_poll *poll);
  43. void vhost_poll_flush(struct vhost_poll *poll);
  44. void vhost_poll_queue(struct vhost_poll *poll);
  45. void vhost_work_flush(struct vhost_dev *dev, struct vhost_work *work);
  46. long vhost_vring_ioctl(struct vhost_dev *d, int ioctl, void __user *argp);
  47. struct vhost_log {
  48. u64 addr;
  49. u64 len;
  50. };
  51. #define START(node) ((node)->start)
  52. #define LAST(node) ((node)->last)
  53. struct vhost_umem_node {
  54. struct rb_node rb;
  55. struct list_head link;
  56. __u64 start;
  57. __u64 last;
  58. __u64 size;
  59. __u64 userspace_addr;
  60. __u32 perm;
  61. __u32 flags_padding;
  62. __u64 __subtree_last;
  63. };
  64. struct vhost_umem {
  65. struct rb_root_cached umem_tree;
  66. struct list_head umem_list;
  67. int numem;
  68. };
  69. enum vhost_uaddr_type {
  70. VHOST_ADDR_DESC = 0,
  71. VHOST_ADDR_AVAIL = 1,
  72. VHOST_ADDR_USED = 2,
  73. VHOST_NUM_ADDRS = 3,
  74. };
  75. /* The virtqueue structure describes a queue attached to a device. */
  76. struct vhost_virtqueue {
  77. struct vhost_dev *dev;
  78. /* The actual ring of buffers. */
  79. struct mutex mutex;
  80. unsigned int num;
  81. struct vring_desc __user *desc;
  82. struct vring_avail __user *avail;
  83. struct vring_used __user *used;
  84. const struct vhost_umem_node *meta_iotlb[VHOST_NUM_ADDRS];
  85. struct file *kick;
  86. struct file *call;
  87. struct file *error;
  88. struct eventfd_ctx *call_ctx;
  89. struct eventfd_ctx *error_ctx;
  90. struct eventfd_ctx *log_ctx;
  91. struct vhost_poll poll;
  92. /* The routine to call when the Guest pings us, or timeout. */
  93. vhost_work_fn_t handle_kick;
  94. /* Last available index we saw. */
  95. u16 last_avail_idx;
  96. /* Caches available index value from user. */
  97. u16 avail_idx;
  98. /* Last index we used. */
  99. u16 last_used_idx;
  100. /* Used flags */
  101. u16 used_flags;
  102. /* Last used index value we have signalled on */
  103. u16 signalled_used;
  104. /* Last used index value we have signalled on */
  105. bool signalled_used_valid;
  106. /* Log writes to used structure. */
  107. bool log_used;
  108. u64 log_addr;
  109. struct iovec iov[UIO_MAXIOV];
  110. struct iovec iotlb_iov[64];
  111. struct iovec *indirect;
  112. struct vring_used_elem *heads;
  113. /* Protected by virtqueue mutex. */
  114. struct vhost_umem *umem;
  115. struct vhost_umem *iotlb;
  116. void *private_data;
  117. u64 acked_features;
  118. /* Log write descriptors */
  119. void __user *log_base;
  120. struct vhost_log *log;
  121. /* Ring endianness. Defaults to legacy native endianness.
  122. * Set to true when starting a modern virtio device. */
  123. bool is_le;
  124. #ifdef CONFIG_VHOST_CROSS_ENDIAN_LEGACY
  125. /* Ring endianness requested by userspace for cross-endian support. */
  126. bool user_be;
  127. #endif
  128. u32 busyloop_timeout;
  129. };
  130. struct vhost_msg_node {
  131. struct vhost_msg msg;
  132. struct vhost_virtqueue *vq;
  133. struct list_head node;
  134. };
  135. struct vhost_dev {
  136. struct mm_struct *mm;
  137. struct mutex mutex;
  138. struct vhost_virtqueue **vqs;
  139. int nvqs;
  140. struct file *log_file;
  141. struct eventfd_ctx *log_ctx;
  142. struct llist_head work_list;
  143. struct task_struct *worker;
  144. struct vhost_umem *umem;
  145. struct vhost_umem *iotlb;
  146. spinlock_t iotlb_lock;
  147. struct list_head read_list;
  148. struct list_head pending_list;
  149. wait_queue_head_t wait;
  150. int weight;
  151. int byte_weight;
  152. u64 kcov_handle;
  153. };
  154. bool vhost_exceeds_weight(struct vhost_virtqueue *vq, int pkts, int total_len);
  155. void vhost_dev_init(struct vhost_dev *, struct vhost_virtqueue **vqs,
  156. int nvqs, int weight, int byte_weight);
  157. long vhost_dev_set_owner(struct vhost_dev *dev);
  158. bool vhost_dev_has_owner(struct vhost_dev *dev);
  159. long vhost_dev_check_owner(struct vhost_dev *);
  160. struct vhost_umem *vhost_dev_reset_owner_prepare(void);
  161. void vhost_dev_reset_owner(struct vhost_dev *, struct vhost_umem *);
  162. void vhost_dev_cleanup(struct vhost_dev *, bool locked);
  163. void vhost_dev_stop(struct vhost_dev *);
  164. long vhost_dev_ioctl(struct vhost_dev *, unsigned int ioctl, void __user *argp);
  165. long vhost_vring_ioctl(struct vhost_dev *d, int ioctl, void __user *argp);
  166. int vhost_vq_access_ok(struct vhost_virtqueue *vq);
  167. int vhost_log_access_ok(struct vhost_dev *);
  168. int vhost_get_vq_desc(struct vhost_virtqueue *,
  169. struct iovec iov[], unsigned int iov_count,
  170. unsigned int *out_num, unsigned int *in_num,
  171. struct vhost_log *log, unsigned int *log_num);
  172. void vhost_discard_vq_desc(struct vhost_virtqueue *, int n);
  173. int vhost_vq_init_access(struct vhost_virtqueue *);
  174. int vhost_add_used(struct vhost_virtqueue *, unsigned int head, int len);
  175. int vhost_add_used_n(struct vhost_virtqueue *, struct vring_used_elem *heads,
  176. unsigned count);
  177. void vhost_add_used_and_signal(struct vhost_dev *, struct vhost_virtqueue *,
  178. unsigned int id, int len);
  179. void vhost_add_used_and_signal_n(struct vhost_dev *, struct vhost_virtqueue *,
  180. struct vring_used_elem *heads, unsigned count);
  181. void vhost_signal(struct vhost_dev *, struct vhost_virtqueue *);
  182. void vhost_disable_notify(struct vhost_dev *, struct vhost_virtqueue *);
  183. bool vhost_vq_avail_empty(struct vhost_dev *, struct vhost_virtqueue *);
  184. bool vhost_enable_notify(struct vhost_dev *, struct vhost_virtqueue *);
  185. int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log,
  186. unsigned int log_num, u64 len,
  187. struct iovec *iov, int count);
  188. int vq_iotlb_prefetch(struct vhost_virtqueue *vq);
  189. struct vhost_msg_node *vhost_new_msg(struct vhost_virtqueue *vq, int type);
  190. void vhost_enqueue_msg(struct vhost_dev *dev,
  191. struct list_head *head,
  192. struct vhost_msg_node *node);
  193. struct vhost_msg_node *vhost_dequeue_msg(struct vhost_dev *dev,
  194. struct list_head *head);
  195. unsigned int vhost_chr_poll(struct file *file, struct vhost_dev *dev,
  196. poll_table *wait);
  197. ssize_t vhost_chr_read_iter(struct vhost_dev *dev, struct iov_iter *to,
  198. int noblock);
  199. ssize_t vhost_chr_write_iter(struct vhost_dev *dev,
  200. struct iov_iter *from);
  201. int vhost_init_device_iotlb(struct vhost_dev *d, bool enabled);
  202. #define vq_err(vq, fmt, ...) do { \
  203. pr_debug(pr_fmt(fmt), ##__VA_ARGS__); \
  204. if ((vq)->error_ctx) \
  205. eventfd_signal((vq)->error_ctx, 1);\
  206. } while (0)
  207. enum {
  208. VHOST_FEATURES = (1ULL << VIRTIO_F_NOTIFY_ON_EMPTY) |
  209. (1ULL << VIRTIO_RING_F_INDIRECT_DESC) |
  210. (1ULL << VIRTIO_RING_F_EVENT_IDX) |
  211. (1ULL << VHOST_F_LOG_ALL) |
  212. (1ULL << VIRTIO_F_ANY_LAYOUT) |
  213. (1ULL << VIRTIO_F_VERSION_1)
  214. };
  215. static inline bool vhost_has_feature(struct vhost_virtqueue *vq, int bit)
  216. {
  217. return vq->acked_features & (1ULL << bit);
  218. }
  219. #ifdef CONFIG_VHOST_CROSS_ENDIAN_LEGACY
  220. static inline bool vhost_is_little_endian(struct vhost_virtqueue *vq)
  221. {
  222. return vq->is_le;
  223. }
  224. #else
  225. static inline bool vhost_is_little_endian(struct vhost_virtqueue *vq)
  226. {
  227. return virtio_legacy_is_little_endian() || vq->is_le;
  228. }
  229. #endif
  230. /* Memory accessors */
  231. static inline u16 vhost16_to_cpu(struct vhost_virtqueue *vq, __virtio16 val)
  232. {
  233. return __virtio16_to_cpu(vhost_is_little_endian(vq), val);
  234. }
  235. static inline __virtio16 cpu_to_vhost16(struct vhost_virtqueue *vq, u16 val)
  236. {
  237. return __cpu_to_virtio16(vhost_is_little_endian(vq), val);
  238. }
  239. static inline u32 vhost32_to_cpu(struct vhost_virtqueue *vq, __virtio32 val)
  240. {
  241. return __virtio32_to_cpu(vhost_is_little_endian(vq), val);
  242. }
  243. static inline __virtio32 cpu_to_vhost32(struct vhost_virtqueue *vq, u32 val)
  244. {
  245. return __cpu_to_virtio32(vhost_is_little_endian(vq), val);
  246. }
  247. static inline u64 vhost64_to_cpu(struct vhost_virtqueue *vq, __virtio64 val)
  248. {
  249. return __virtio64_to_cpu(vhost_is_little_endian(vq), val);
  250. }
  251. static inline __virtio64 cpu_to_vhost64(struct vhost_virtqueue *vq, u64 val)
  252. {
  253. return __cpu_to_virtio64(vhost_is_little_endian(vq), val);
  254. }
  255. #endif