vhost.h 11 KB

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