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