xref: /linux/drivers/vhost/vhost.h (revision 821c9e515db512904250e1d460109a1dc4c7ef6b)
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 void vhost_discard_vq_desc(struct vhost_virtqueue *, int n);
234 
235 bool vhost_vq_work_queue(struct vhost_virtqueue *vq, struct vhost_work *work);
236 bool vhost_vq_has_work(struct vhost_virtqueue *vq);
237 bool vhost_vq_is_setup(struct vhost_virtqueue *vq);
238 int vhost_vq_init_access(struct vhost_virtqueue *);
239 int vhost_add_used(struct vhost_virtqueue *, unsigned int head, int len);
240 int vhost_add_used_n(struct vhost_virtqueue *, struct vring_used_elem *heads,
241 		     u16 *nheads, unsigned count);
242 void vhost_add_used_and_signal(struct vhost_dev *, struct vhost_virtqueue *,
243 			       unsigned int id, int len);
244 void vhost_add_used_and_signal_n(struct vhost_dev *, struct vhost_virtqueue *,
245 				 struct vring_used_elem *heads, u16 *nheads,
246 				 unsigned count);
247 void vhost_signal(struct vhost_dev *, struct vhost_virtqueue *);
248 void vhost_disable_notify(struct vhost_dev *, struct vhost_virtqueue *);
249 bool vhost_vq_avail_empty(struct vhost_dev *, struct vhost_virtqueue *);
250 bool vhost_enable_notify(struct vhost_dev *, struct vhost_virtqueue *);
251 
252 int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log,
253 		    unsigned int log_num, u64 len,
254 		    struct iovec *iov, int count);
255 int vq_meta_prefetch(struct vhost_virtqueue *vq);
256 
257 struct vhost_msg_node *vhost_new_msg(struct vhost_virtqueue *vq, int type);
258 void vhost_enqueue_msg(struct vhost_dev *dev,
259 		       struct list_head *head,
260 		       struct vhost_msg_node *node);
261 struct vhost_msg_node *vhost_dequeue_msg(struct vhost_dev *dev,
262 					 struct list_head *head);
263 void vhost_set_backend_features(struct vhost_dev *dev, u64 features);
264 
265 __poll_t vhost_chr_poll(struct file *file, struct vhost_dev *dev,
266 			    poll_table *wait);
267 ssize_t vhost_chr_read_iter(struct vhost_dev *dev, struct iov_iter *to,
268 			    int noblock);
269 ssize_t vhost_chr_write_iter(struct vhost_dev *dev,
270 			     struct iov_iter *from);
271 int vhost_init_device_iotlb(struct vhost_dev *d);
272 
273 void vhost_iotlb_map_free(struct vhost_iotlb *iotlb,
274 			  struct vhost_iotlb_map *map);
275 
276 #define vq_err(vq, fmt, ...) do {                                  \
277 		pr_debug(pr_fmt(fmt), ##__VA_ARGS__);       \
278 		if ((vq)->error_ctx)                               \
279 				eventfd_signal((vq)->error_ctx);\
280 	} while (0)
281 
282 enum {
283 	VHOST_FEATURES = (1ULL << VIRTIO_F_NOTIFY_ON_EMPTY) |
284 			 (1ULL << VIRTIO_RING_F_INDIRECT_DESC) |
285 			 (1ULL << VIRTIO_RING_F_EVENT_IDX) |
286 			 (1ULL << VHOST_F_LOG_ALL) |
287 			 (1ULL << VIRTIO_F_ANY_LAYOUT) |
288 			 (1ULL << VIRTIO_F_VERSION_1)
289 };
290 
291 /**
292  * vhost_vq_set_backend - Set backend.
293  *
294  * @vq            Virtqueue.
295  * @private_data  The private data.
296  *
297  * Context: Need to call with vq->mutex acquired.
298  */
vhost_vq_set_backend(struct vhost_virtqueue * vq,void * private_data)299 static inline void vhost_vq_set_backend(struct vhost_virtqueue *vq,
300 					void *private_data)
301 {
302 	vq->private_data = private_data;
303 }
304 
305 /**
306  * vhost_vq_get_backend - Get backend.
307  *
308  * @vq            Virtqueue.
309  *
310  * Context: Need to call with vq->mutex acquired.
311  * Return: Private data previously set with vhost_vq_set_backend.
312  */
vhost_vq_get_backend(struct vhost_virtqueue * vq)313 static inline void *vhost_vq_get_backend(struct vhost_virtqueue *vq)
314 {
315 	return vq->private_data;
316 }
317 
vhost_has_feature(struct vhost_virtqueue * vq,int bit)318 static inline bool vhost_has_feature(struct vhost_virtqueue *vq, int bit)
319 {
320 	return virtio_features_test_bit(vq->acked_features_array, bit);
321 }
322 
vhost_backend_has_feature(struct vhost_virtqueue * vq,int bit)323 static inline bool vhost_backend_has_feature(struct vhost_virtqueue *vq, int bit)
324 {
325 	return vq->acked_backend_features & (1ULL << bit);
326 }
327 
328 #ifdef CONFIG_VHOST_CROSS_ENDIAN_LEGACY
vhost_is_little_endian(struct vhost_virtqueue * vq)329 static inline bool vhost_is_little_endian(struct vhost_virtqueue *vq)
330 {
331 	return vq->is_le;
332 }
333 #else
vhost_is_little_endian(struct vhost_virtqueue * vq)334 static inline bool vhost_is_little_endian(struct vhost_virtqueue *vq)
335 {
336 	return virtio_legacy_is_little_endian() || vq->is_le;
337 }
338 #endif
339 
340 /* Memory accessors */
vhost16_to_cpu(struct vhost_virtqueue * vq,__virtio16 val)341 static inline u16 vhost16_to_cpu(struct vhost_virtqueue *vq, __virtio16 val)
342 {
343 	return __virtio16_to_cpu(vhost_is_little_endian(vq), val);
344 }
345 
cpu_to_vhost16(struct vhost_virtqueue * vq,u16 val)346 static inline __virtio16 cpu_to_vhost16(struct vhost_virtqueue *vq, u16 val)
347 {
348 	return __cpu_to_virtio16(vhost_is_little_endian(vq), val);
349 }
350 
vhost32_to_cpu(struct vhost_virtqueue * vq,__virtio32 val)351 static inline u32 vhost32_to_cpu(struct vhost_virtqueue *vq, __virtio32 val)
352 {
353 	return __virtio32_to_cpu(vhost_is_little_endian(vq), val);
354 }
355 
cpu_to_vhost32(struct vhost_virtqueue * vq,u32 val)356 static inline __virtio32 cpu_to_vhost32(struct vhost_virtqueue *vq, u32 val)
357 {
358 	return __cpu_to_virtio32(vhost_is_little_endian(vq), val);
359 }
360 
vhost64_to_cpu(struct vhost_virtqueue * vq,__virtio64 val)361 static inline u64 vhost64_to_cpu(struct vhost_virtqueue *vq, __virtio64 val)
362 {
363 	return __virtio64_to_cpu(vhost_is_little_endian(vq), val);
364 }
365 
cpu_to_vhost64(struct vhost_virtqueue * vq,u64 val)366 static inline __virtio64 cpu_to_vhost64(struct vhost_virtqueue *vq, u64 val)
367 {
368 	return __cpu_to_virtio64(vhost_is_little_endian(vq), val);
369 }
370 #endif
371