xref: /openbmc/linux/drivers/vhost/vhost.h (revision 7ce2e76a)
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/pagemap.h>
16 #include <linux/mmu_notifier.h>
17 #include <asm/cacheflush.h>
18 
19 struct vhost_work;
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 /* Poll a file (eventfd or socket) */
30 /* Note: there's nothing vhost specific about this structure. */
31 struct vhost_poll {
32 	poll_table                table;
33 	wait_queue_head_t        *wqh;
34 	wait_queue_entry_t              wait;
35 	struct vhost_work	  work;
36 	__poll_t		  mask;
37 	struct vhost_dev	 *dev;
38 };
39 
40 void vhost_work_init(struct vhost_work *work, vhost_work_fn_t fn);
41 void vhost_work_queue(struct vhost_dev *dev, struct vhost_work *work);
42 bool vhost_has_work(struct vhost_dev *dev);
43 
44 void vhost_poll_init(struct vhost_poll *poll, vhost_work_fn_t fn,
45 		     __poll_t mask, struct vhost_dev *dev);
46 int vhost_poll_start(struct vhost_poll *poll, struct file *file);
47 void vhost_poll_stop(struct vhost_poll *poll);
48 void vhost_poll_flush(struct vhost_poll *poll);
49 void vhost_poll_queue(struct vhost_poll *poll);
50 void vhost_work_flush(struct vhost_dev *dev, struct vhost_work *work);
51 long vhost_vring_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp);
52 
53 struct vhost_log {
54 	u64 addr;
55 	u64 len;
56 };
57 
58 #define START(node) ((node)->start)
59 #define LAST(node) ((node)->last)
60 
61 struct vhost_umem_node {
62 	struct rb_node rb;
63 	struct list_head link;
64 	__u64 start;
65 	__u64 last;
66 	__u64 size;
67 	__u64 userspace_addr;
68 	__u32 perm;
69 	__u32 flags_padding;
70 	__u64 __subtree_last;
71 };
72 
73 struct vhost_umem {
74 	struct rb_root_cached umem_tree;
75 	struct list_head umem_list;
76 	int numem;
77 };
78 
79 enum vhost_uaddr_type {
80 	VHOST_ADDR_DESC = 0,
81 	VHOST_ADDR_AVAIL = 1,
82 	VHOST_ADDR_USED = 2,
83 	VHOST_NUM_ADDRS = 3,
84 };
85 
86 struct vhost_map {
87 	int npages;
88 	void *addr;
89 	struct page **pages;
90 };
91 
92 struct vhost_uaddr {
93 	unsigned long uaddr;
94 	size_t size;
95 	bool write;
96 };
97 
98 #if defined(CONFIG_MMU_NOTIFIER) && ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE == 0
99 #define VHOST_ARCH_CAN_ACCEL_UACCESS 1
100 #else
101 #define VHOST_ARCH_CAN_ACCEL_UACCESS 0
102 #endif
103 
104 /* The virtqueue structure describes a queue attached to a device. */
105 struct vhost_virtqueue {
106 	struct vhost_dev *dev;
107 
108 	/* The actual ring of buffers. */
109 	struct mutex mutex;
110 	unsigned int num;
111 	struct vring_desc __user *desc;
112 	struct vring_avail __user *avail;
113 	struct vring_used __user *used;
114 
115 #if VHOST_ARCH_CAN_ACCEL_UACCESS
116 	/* Read by memory accessors, modified by meta data
117 	 * prefetching, MMU notifier and vring ioctl().
118 	 * Synchonrized through mmu_lock (writers) and RCU (writers
119 	 * and readers).
120 	 */
121 	struct vhost_map __rcu *maps[VHOST_NUM_ADDRS];
122 	/* Read by MMU notifier, modified by vring ioctl(),
123 	 * synchronized through MMU notifier
124 	 * registering/unregistering.
125 	 */
126 	struct vhost_uaddr uaddrs[VHOST_NUM_ADDRS];
127 #endif
128 	const struct vhost_umem_node *meta_iotlb[VHOST_NUM_ADDRS];
129 
130 	struct file *kick;
131 	struct eventfd_ctx *call_ctx;
132 	struct eventfd_ctx *error_ctx;
133 	struct eventfd_ctx *log_ctx;
134 
135 	struct vhost_poll poll;
136 
137 	/* The routine to call when the Guest pings us, or timeout. */
138 	vhost_work_fn_t handle_kick;
139 
140 	/* Last available index we saw. */
141 	u16 last_avail_idx;
142 
143 	/* Caches available index value from user. */
144 	u16 avail_idx;
145 
146 	/* Last index we used. */
147 	u16 last_used_idx;
148 
149 	/* Used flags */
150 	u16 used_flags;
151 
152 	/* Last used index value we have signalled on */
153 	u16 signalled_used;
154 
155 	/* Last used index value we have signalled on */
156 	bool signalled_used_valid;
157 
158 	/* Log writes to used structure. */
159 	bool log_used;
160 	u64 log_addr;
161 
162 	struct iovec iov[UIO_MAXIOV];
163 	struct iovec iotlb_iov[64];
164 	struct iovec *indirect;
165 	struct vring_used_elem *heads;
166 	/* Protected by virtqueue mutex. */
167 	struct vhost_umem *umem;
168 	struct vhost_umem *iotlb;
169 	void *private_data;
170 	u64 acked_features;
171 	u64 acked_backend_features;
172 	/* Log write descriptors */
173 	void __user *log_base;
174 	struct vhost_log *log;
175 
176 	/* Ring endianness. Defaults to legacy native endianness.
177 	 * Set to true when starting a modern virtio device. */
178 	bool is_le;
179 #ifdef CONFIG_VHOST_CROSS_ENDIAN_LEGACY
180 	/* Ring endianness requested by userspace for cross-endian support. */
181 	bool user_be;
182 #endif
183 	u32 busyloop_timeout;
184 	spinlock_t mmu_lock;
185 	int invalidate_count;
186 };
187 
188 struct vhost_msg_node {
189   union {
190 	  struct vhost_msg msg;
191 	  struct vhost_msg_v2 msg_v2;
192   };
193   struct vhost_virtqueue *vq;
194   struct list_head node;
195 };
196 
197 struct vhost_dev {
198 	struct mm_struct *mm;
199 #ifdef CONFIG_MMU_NOTIFIER
200 	struct mmu_notifier mmu_notifier;
201 #endif
202 	struct mutex mutex;
203 	struct vhost_virtqueue **vqs;
204 	int nvqs;
205 	struct eventfd_ctx *log_ctx;
206 	struct llist_head work_list;
207 	struct task_struct *worker;
208 	struct vhost_umem *umem;
209 	struct vhost_umem *iotlb;
210 	spinlock_t iotlb_lock;
211 	struct list_head read_list;
212 	struct list_head pending_list;
213 	wait_queue_head_t wait;
214 	int iov_limit;
215 	int weight;
216 	int byte_weight;
217 };
218 
219 bool vhost_exceeds_weight(struct vhost_virtqueue *vq, int pkts, int total_len);
220 void vhost_dev_init(struct vhost_dev *, struct vhost_virtqueue **vqs,
221 		    int nvqs, int iov_limit, int weight, int byte_weight);
222 long vhost_dev_set_owner(struct vhost_dev *dev);
223 bool vhost_dev_has_owner(struct vhost_dev *dev);
224 long vhost_dev_check_owner(struct vhost_dev *);
225 struct vhost_umem *vhost_dev_reset_owner_prepare(void);
226 void vhost_dev_reset_owner(struct vhost_dev *, struct vhost_umem *);
227 void vhost_dev_cleanup(struct vhost_dev *);
228 void vhost_dev_stop(struct vhost_dev *);
229 long vhost_dev_ioctl(struct vhost_dev *, unsigned int ioctl, void __user *argp);
230 long vhost_vring_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp);
231 bool vhost_vq_access_ok(struct vhost_virtqueue *vq);
232 bool vhost_log_access_ok(struct vhost_dev *);
233 
234 int vhost_get_vq_desc(struct vhost_virtqueue *,
235 		      struct iovec iov[], unsigned int iov_count,
236 		      unsigned int *out_num, unsigned int *in_num,
237 		      struct vhost_log *log, unsigned int *log_num);
238 void vhost_discard_vq_desc(struct vhost_virtqueue *, int n);
239 
240 int vhost_vq_init_access(struct vhost_virtqueue *);
241 int vhost_add_used(struct vhost_virtqueue *, unsigned int head, int len);
242 int vhost_add_used_n(struct vhost_virtqueue *, struct vring_used_elem *heads,
243 		     unsigned count);
244 void vhost_add_used_and_signal(struct vhost_dev *, struct vhost_virtqueue *,
245 			       unsigned int id, int len);
246 void vhost_add_used_and_signal_n(struct vhost_dev *, struct vhost_virtqueue *,
247 			       struct vring_used_elem *heads, unsigned count);
248 void vhost_signal(struct vhost_dev *, struct vhost_virtqueue *);
249 void vhost_disable_notify(struct vhost_dev *, struct vhost_virtqueue *);
250 bool vhost_vq_avail_empty(struct vhost_dev *, struct vhost_virtqueue *);
251 bool vhost_enable_notify(struct vhost_dev *, struct vhost_virtqueue *);
252 
253 int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log,
254 		    unsigned int log_num, u64 len,
255 		    struct iovec *iov, int count);
256 int vq_meta_prefetch(struct vhost_virtqueue *vq);
257 
258 struct vhost_msg_node *vhost_new_msg(struct vhost_virtqueue *vq, int type);
259 void vhost_enqueue_msg(struct vhost_dev *dev,
260 		       struct list_head *head,
261 		       struct vhost_msg_node *node);
262 struct vhost_msg_node *vhost_dequeue_msg(struct vhost_dev *dev,
263 					 struct list_head *head);
264 __poll_t vhost_chr_poll(struct file *file, struct vhost_dev *dev,
265 			    poll_table *wait);
266 ssize_t vhost_chr_read_iter(struct vhost_dev *dev, struct iov_iter *to,
267 			    int noblock);
268 ssize_t vhost_chr_write_iter(struct vhost_dev *dev,
269 			     struct iov_iter *from);
270 int vhost_init_device_iotlb(struct vhost_dev *d, bool enabled);
271 
272 #define vq_err(vq, fmt, ...) do {                                  \
273 		pr_debug(pr_fmt(fmt), ##__VA_ARGS__);       \
274 		if ((vq)->error_ctx)                               \
275 				eventfd_signal((vq)->error_ctx, 1);\
276 	} while (0)
277 
278 enum {
279 	VHOST_FEATURES = (1ULL << VIRTIO_F_NOTIFY_ON_EMPTY) |
280 			 (1ULL << VIRTIO_RING_F_INDIRECT_DESC) |
281 			 (1ULL << VIRTIO_RING_F_EVENT_IDX) |
282 			 (1ULL << VHOST_F_LOG_ALL) |
283 			 (1ULL << VIRTIO_F_ANY_LAYOUT) |
284 			 (1ULL << VIRTIO_F_VERSION_1)
285 };
286 
287 static inline bool vhost_has_feature(struct vhost_virtqueue *vq, int bit)
288 {
289 	return vq->acked_features & (1ULL << bit);
290 }
291 
292 static inline bool vhost_backend_has_feature(struct vhost_virtqueue *vq, int bit)
293 {
294 	return vq->acked_backend_features & (1ULL << bit);
295 }
296 
297 #ifdef CONFIG_VHOST_CROSS_ENDIAN_LEGACY
298 static inline bool vhost_is_little_endian(struct vhost_virtqueue *vq)
299 {
300 	return vq->is_le;
301 }
302 #else
303 static inline bool vhost_is_little_endian(struct vhost_virtqueue *vq)
304 {
305 	return virtio_legacy_is_little_endian() || vq->is_le;
306 }
307 #endif
308 
309 /* Memory accessors */
310 static inline u16 vhost16_to_cpu(struct vhost_virtqueue *vq, __virtio16 val)
311 {
312 	return __virtio16_to_cpu(vhost_is_little_endian(vq), val);
313 }
314 
315 static inline __virtio16 cpu_to_vhost16(struct vhost_virtqueue *vq, u16 val)
316 {
317 	return __cpu_to_virtio16(vhost_is_little_endian(vq), val);
318 }
319 
320 static inline u32 vhost32_to_cpu(struct vhost_virtqueue *vq, __virtio32 val)
321 {
322 	return __virtio32_to_cpu(vhost_is_little_endian(vq), val);
323 }
324 
325 static inline __virtio32 cpu_to_vhost32(struct vhost_virtqueue *vq, u32 val)
326 {
327 	return __cpu_to_virtio32(vhost_is_little_endian(vq), val);
328 }
329 
330 static inline u64 vhost64_to_cpu(struct vhost_virtqueue *vq, __virtio64 val)
331 {
332 	return __virtio64_to_cpu(vhost_is_little_endian(vq), val);
333 }
334 
335 static inline __virtio64 cpu_to_vhost64(struct vhost_virtqueue *vq, u64 val)
336 {
337 	return __cpu_to_virtio64(vhost_is_little_endian(vq), val);
338 }
339 #endif
340