vhost.h 9.5 KB

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