vhost.h 12 KB

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