blob: c8e96a095d3b85f80023001b1dcdf793a437da57 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002#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>
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00009#include <linux/poll.h>
10#include <linux/file.h>
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000011#include <linux/uio.h>
12#include <linux/virtio_config.h>
13#include <linux/virtio_ring.h>
Arun Sharma600634972011-07-26 16:09:06 -070014#include <linux/atomic.h>
Jason Wang0bbe3062020-03-26 22:01:19 +080015#include <linux/vhost_iotlb.h>
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000016
Tejun Heoc23f34452010-06-02 20:40:00 +020017struct vhost_work;
18typedef void (*vhost_work_fn_t)(struct vhost_work *work);
19
Jason Wang04b96e52016-04-25 22:14:33 -040020#define VHOST_WORK_QUEUED 1
Tejun Heoc23f34452010-06-02 20:40:00 +020021struct vhost_work {
Jason Wang04b96e52016-04-25 22:14:33 -040022 struct llist_node node;
Tejun Heoc23f34452010-06-02 20:40:00 +020023 vhost_work_fn_t fn;
Jason Wang04b96e52016-04-25 22:14:33 -040024 unsigned long flags;
Tejun Heoc23f34452010-06-02 20:40:00 +020025};
26
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000027/* Poll a file (eventfd or socket) */
28/* Note: there's nothing vhost specific about this structure. */
29struct vhost_poll {
30 poll_table table;
31 wait_queue_head_t *wqh;
Ingo Molnarac6424b2017-06-20 12:06:13 +020032 wait_queue_entry_t wait;
Tejun Heoc23f34452010-06-02 20:40:00 +020033 struct vhost_work work;
Al Viro58e3b602017-07-03 23:50:40 -040034 __poll_t mask;
Tejun Heoc23f34452010-06-02 20:40:00 +020035 struct vhost_dev *dev;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000036};
37
Stefan Hajnoczi163049a2012-07-21 06:55:37 +000038void vhost_work_init(struct vhost_work *work, vhost_work_fn_t fn);
39void vhost_work_queue(struct vhost_dev *dev, struct vhost_work *work);
Jason Wang526d3e72016-03-04 06:24:51 -050040bool vhost_has_work(struct vhost_dev *dev);
Stefan Hajnoczi163049a2012-07-21 06:55:37 +000041
Tejun Heoc23f34452010-06-02 20:40:00 +020042void vhost_poll_init(struct vhost_poll *poll, vhost_work_fn_t fn,
Al Viro58e3b602017-07-03 23:50:40 -040043 __poll_t mask, struct vhost_dev *dev);
Jason Wang2b8b3282013-01-28 01:05:18 +000044int vhost_poll_start(struct vhost_poll *poll, struct file *file);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000045void vhost_poll_stop(struct vhost_poll *poll);
46void vhost_poll_flush(struct vhost_poll *poll);
47void vhost_poll_queue(struct vhost_poll *poll);
Asias He6ac1afb2013-05-06 16:38:21 +080048void vhost_work_flush(struct vhost_dev *dev, struct vhost_work *work);
Sonny Rao26b36602018-03-14 10:05:06 -070049long vhost_vring_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000050
51struct vhost_log {
52 u64 addr;
53 u64 len;
54};
55
Jason Wangf8894912017-02-28 17:56:02 +080056enum vhost_uaddr_type {
57 VHOST_ADDR_DESC = 0,
58 VHOST_ADDR_AVAIL = 1,
59 VHOST_ADDR_USED = 2,
60 VHOST_NUM_ADDRS = 3,
61};
62
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000063/* The virtqueue structure describes a queue attached to a device. */
64struct vhost_virtqueue {
65 struct vhost_dev *dev;
66
67 /* The actual ring of buffers. */
68 struct mutex mutex;
69 unsigned int num;
Michael S. Tsirkina865e422020-04-06 08:42:55 -040070 vring_desc_t __user *desc;
71 vring_avail_t __user *avail;
72 vring_used_t __user *used;
Jason Wang0bbe3062020-03-26 22:01:19 +080073 const struct vhost_iotlb_map *meta_iotlb[VHOST_NUM_ADDRS];
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000074 struct file *kick;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000075 struct eventfd_ctx *call_ctx;
76 struct eventfd_ctx *error_ctx;
77 struct eventfd_ctx *log_ctx;
78
79 struct vhost_poll poll;
80
81 /* The routine to call when the Guest pings us, or timeout. */
Tejun Heoc23f34452010-06-02 20:40:00 +020082 vhost_work_fn_t handle_kick;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000083
84 /* Last available index we saw. */
85 u16 last_avail_idx;
86
87 /* Caches available index value from user. */
88 u16 avail_idx;
89
90 /* Last index we used. */
91 u16 last_used_idx;
92
93 /* Used flags */
94 u16 used_flags;
95
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +030096 /* Last used index value we have signalled on */
97 u16 signalled_used;
98
99 /* Last used index value we have signalled on */
100 bool signalled_used_valid;
101
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000102 /* Log writes to used structure. */
103 bool log_used;
104 u64 log_addr;
105
Jason Wange0e9b402010-09-14 23:53:05 +0800106 struct iovec iov[UIO_MAXIOV];
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400107 struct iovec iotlb_iov[64];
Jason Wange0e9b402010-09-14 23:53:05 +0800108 struct iovec *indirect;
Jason Wange0e9b402010-09-14 23:53:05 +0800109 struct vring_used_elem *heads;
Asias He22fa90c2013-05-07 14:54:36 +0800110 /* Protected by virtqueue mutex. */
Jason Wang0bbe3062020-03-26 22:01:19 +0800111 struct vhost_iotlb *umem;
112 struct vhost_iotlb *iotlb;
Asias He22fa90c2013-05-07 14:54:36 +0800113 void *private_data;
Michael S. Tsirkinbd827522014-10-24 14:08:18 +0300114 u64 acked_features;
Jason Wang429711a2018-08-06 11:17:47 +0800115 u64 acked_backend_features;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000116 /* Log write descriptors */
117 void __user *log_base;
Jason Wange0e9b402010-09-14 23:53:05 +0800118 struct vhost_log *log;
Greg Kurz2751c982015-04-24 14:27:24 +0200119
120 /* Ring endianness. Defaults to legacy native endianness.
121 * Set to true when starting a modern virtio device. */
122 bool is_le;
123#ifdef CONFIG_VHOST_CROSS_ENDIAN_LEGACY
124 /* Ring endianness requested by userspace for cross-endian support. */
125 bool user_be;
126#endif
Jason Wang03088132016-03-04 06:24:53 -0500127 u32 busyloop_timeout;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000128};
129
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400130struct vhost_msg_node {
Jason Wang429711a2018-08-06 11:17:47 +0800131 union {
132 struct vhost_msg msg;
133 struct vhost_msg_v2 msg_v2;
134 };
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400135 struct vhost_virtqueue *vq;
136 struct list_head node;
137};
138
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000139struct vhost_dev {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000140 struct mm_struct *mm;
141 struct mutex mutex;
Asias He3ab2e422013-04-27 11:16:48 +0800142 struct vhost_virtqueue **vqs;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000143 int nvqs;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000144 struct eventfd_ctx *log_ctx;
Jason Wang04b96e52016-04-25 22:14:33 -0400145 struct llist_head work_list;
Tejun Heoc23f34452010-06-02 20:40:00 +0200146 struct task_struct *worker;
Jason Wang0bbe3062020-03-26 22:01:19 +0800147 struct vhost_iotlb *umem;
148 struct vhost_iotlb *iotlb;
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400149 spinlock_t iotlb_lock;
150 struct list_head read_list;
151 struct list_head pending_list;
152 wait_queue_head_t wait;
Jason Wangb46a0bf2019-01-28 15:05:05 +0800153 int iov_limit;
Jason Wange82b9b02019-05-17 00:29:49 -0400154 int weight;
155 int byte_weight;
Andrey Konovalov8f6a7f92019-12-04 16:52:50 -0800156 u64 kcov_handle;
Jason Wang01fcb1c2020-05-29 16:02:58 +0800157 bool use_worker;
Jason Wang792a4f22020-03-26 22:01:18 +0800158 int (*msg_handler)(struct vhost_dev *dev,
159 struct vhost_iotlb_msg *msg);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000160};
161
Jason Wange82b9b02019-05-17 00:29:49 -0400162bool vhost_exceeds_weight(struct vhost_virtqueue *vq, int pkts, int total_len);
Jason Wangb46a0bf2019-01-28 15:05:05 +0800163void vhost_dev_init(struct vhost_dev *, struct vhost_virtqueue **vqs,
Jason Wang792a4f22020-03-26 22:01:18 +0800164 int nvqs, int iov_limit, int weight, int byte_weight,
Jason Wang01fcb1c2020-05-29 16:02:58 +0800165 bool use_worker,
Jason Wang792a4f22020-03-26 22:01:18 +0800166 int (*msg_handler)(struct vhost_dev *dev,
167 struct vhost_iotlb_msg *msg));
Asias He54db63c2013-05-06 11:15:59 +0800168long vhost_dev_set_owner(struct vhost_dev *dev);
Michael S. Tsirkin05c05352013-06-06 15:20:39 +0300169bool vhost_dev_has_owner(struct vhost_dev *dev);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000170long vhost_dev_check_owner(struct vhost_dev *);
Jason Wang0bbe3062020-03-26 22:01:19 +0800171struct vhost_iotlb *vhost_dev_reset_owner_prepare(void);
172void vhost_dev_reset_owner(struct vhost_dev *dev, struct vhost_iotlb *iotlb);
夷则(Caspar)f6f93f72017-12-25 00:08:58 +0800173void vhost_dev_cleanup(struct vhost_dev *);
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000174void vhost_dev_stop(struct vhost_dev *);
Michael S. Tsirkin935cdee2012-12-06 14:03:34 +0200175long vhost_dev_ioctl(struct vhost_dev *, unsigned int ioctl, void __user *argp);
Sonny Rao26b36602018-03-14 10:05:06 -0700176long vhost_vring_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp);
Stefan Hajnocziddd3d402018-04-11 10:35:41 +0800177bool vhost_vq_access_ok(struct vhost_virtqueue *vq);
178bool vhost_log_access_ok(struct vhost_dev *);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000179
Michael S. Tsirkin47283be2014-06-05 15:20:27 +0300180int vhost_get_vq_desc(struct vhost_virtqueue *,
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +0300181 struct iovec iov[], unsigned int iov_count,
182 unsigned int *out_num, unsigned int *in_num,
183 struct vhost_log *log, unsigned int *log_num);
David Stevens8dd014a2010-07-27 18:52:21 +0300184void vhost_discard_vq_desc(struct vhost_virtqueue *, int n);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000185
Greg Kurz80f7d032016-02-16 15:59:44 +0100186int vhost_vq_init_access(struct vhost_virtqueue *);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000187int vhost_add_used(struct vhost_virtqueue *, unsigned int head, int len);
David Stevens8dd014a2010-07-27 18:52:21 +0300188int vhost_add_used_n(struct vhost_virtqueue *, struct vring_used_elem *heads,
189 unsigned count);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000190void vhost_add_used_and_signal(struct vhost_dev *, struct vhost_virtqueue *,
David Stevens8dd014a2010-07-27 18:52:21 +0300191 unsigned int id, int len);
192void vhost_add_used_and_signal_n(struct vhost_dev *, struct vhost_virtqueue *,
193 struct vring_used_elem *heads, unsigned count);
194void vhost_signal(struct vhost_dev *, struct vhost_virtqueue *);
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300195void vhost_disable_notify(struct vhost_dev *, struct vhost_virtqueue *);
Jason Wangd4a60602016-03-04 06:24:52 -0500196bool vhost_vq_avail_empty(struct vhost_dev *, struct vhost_virtqueue *);
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300197bool vhost_enable_notify(struct vhost_dev *, struct vhost_virtqueue *);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000198
199int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log,
Jason Wangcc5e7102019-01-16 16:54:42 +0800200 unsigned int log_num, u64 len,
201 struct iovec *iov, int count);
Jason Wang9b5e8302019-05-24 04:12:15 -0400202int vq_meta_prefetch(struct vhost_virtqueue *vq);
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400203
204struct vhost_msg_node *vhost_new_msg(struct vhost_virtqueue *vq, int type);
205void vhost_enqueue_msg(struct vhost_dev *dev,
206 struct list_head *head,
207 struct vhost_msg_node *node);
208struct vhost_msg_node *vhost_dequeue_msg(struct vhost_dev *dev,
209 struct list_head *head);
Al Viroafc9a422017-07-03 06:39:46 -0400210__poll_t vhost_chr_poll(struct file *file, struct vhost_dev *dev,
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400211 poll_table *wait);
212ssize_t vhost_chr_read_iter(struct vhost_dev *dev, struct iov_iter *to,
213 int noblock);
214ssize_t vhost_chr_write_iter(struct vhost_dev *dev,
215 struct iov_iter *from);
216int vhost_init_device_iotlb(struct vhost_dev *d, bool enabled);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000217
Jason Wang0bbe3062020-03-26 22:01:19 +0800218void vhost_iotlb_map_free(struct vhost_iotlb *iotlb,
219 struct vhost_iotlb_map *map);
220
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000221#define vq_err(vq, fmt, ...) do { \
222 pr_debug(pr_fmt(fmt), ##__VA_ARGS__); \
223 if ((vq)->error_ctx) \
224 eventfd_signal((vq)->error_ctx, 1);\
225 } while (0)
226
227enum {
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300228 VHOST_FEATURES = (1ULL << VIRTIO_F_NOTIFY_ON_EMPTY) |
229 (1ULL << VIRTIO_RING_F_INDIRECT_DESC) |
230 (1ULL << VIRTIO_RING_F_EVENT_IDX) |
Michael S. Tsirkin4e9fa502015-09-09 22:24:56 +0300231 (1ULL << VHOST_F_LOG_ALL) |
232 (1ULL << VIRTIO_F_ANY_LAYOUT) |
233 (1ULL << VIRTIO_F_VERSION_1)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000234};
235
Eugenio Pérez247643f2020-03-31 21:27:57 +0200236/**
237 * vhost_vq_set_backend - Set backend.
238 *
239 * @vq Virtqueue.
240 * @private_data The private data.
241 *
242 * Context: Need to call with vq->mutex acquired.
243 */
244static inline void vhost_vq_set_backend(struct vhost_virtqueue *vq,
245 void *private_data)
246{
247 vq->private_data = private_data;
248}
249
250/**
251 * vhost_vq_get_backend - Get backend.
252 *
253 * @vq Virtqueue.
254 *
255 * Context: Need to call with vq->mutex acquired.
256 * Return: Private data previously set with vhost_vq_set_backend.
257 */
258static inline void *vhost_vq_get_backend(struct vhost_virtqueue *vq)
259{
260 return vq->private_data;
261}
262
Michael S. Tsirkinbd827522014-10-24 14:08:18 +0300263static inline bool vhost_has_feature(struct vhost_virtqueue *vq, int bit)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000264{
Michael S. Tsirkinbd827522014-10-24 14:08:18 +0300265 return vq->acked_features & (1ULL << bit);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000266}
Michael S. Tsirkine05fd122014-10-24 11:48:17 +0300267
Jason Wang429711a2018-08-06 11:17:47 +0800268static inline bool vhost_backend_has_feature(struct vhost_virtqueue *vq, int bit)
269{
270 return vq->acked_backend_features & (1ULL << bit);
271}
272
Michael S. Tsirkine407f392015-10-27 11:37:39 +0200273#ifdef CONFIG_VHOST_CROSS_ENDIAN_LEGACY
Greg Kurzab27c072015-04-24 14:25:12 +0200274static inline bool vhost_is_little_endian(struct vhost_virtqueue *vq)
275{
Greg Kurz2751c982015-04-24 14:27:24 +0200276 return vq->is_le;
Greg Kurzab27c072015-04-24 14:25:12 +0200277}
Michael S. Tsirkine407f392015-10-27 11:37:39 +0200278#else
279static inline bool vhost_is_little_endian(struct vhost_virtqueue *vq)
280{
281 return virtio_legacy_is_little_endian() || vq->is_le;
282}
283#endif
Greg Kurzab27c072015-04-24 14:25:12 +0200284
Michael S. Tsirkine05fd122014-10-24 11:48:17 +0300285/* Memory accessors */
286static inline u16 vhost16_to_cpu(struct vhost_virtqueue *vq, __virtio16 val)
287{
Greg Kurzab27c072015-04-24 14:25:12 +0200288 return __virtio16_to_cpu(vhost_is_little_endian(vq), val);
Michael S. Tsirkine05fd122014-10-24 11:48:17 +0300289}
290
291static inline __virtio16 cpu_to_vhost16(struct vhost_virtqueue *vq, u16 val)
292{
Greg Kurzab27c072015-04-24 14:25:12 +0200293 return __cpu_to_virtio16(vhost_is_little_endian(vq), val);
Michael S. Tsirkine05fd122014-10-24 11:48:17 +0300294}
295
296static inline u32 vhost32_to_cpu(struct vhost_virtqueue *vq, __virtio32 val)
297{
Greg Kurzab27c072015-04-24 14:25:12 +0200298 return __virtio32_to_cpu(vhost_is_little_endian(vq), val);
Michael S. Tsirkine05fd122014-10-24 11:48:17 +0300299}
300
301static inline __virtio32 cpu_to_vhost32(struct vhost_virtqueue *vq, u32 val)
302{
Greg Kurzab27c072015-04-24 14:25:12 +0200303 return __cpu_to_virtio32(vhost_is_little_endian(vq), val);
Michael S. Tsirkine05fd122014-10-24 11:48:17 +0300304}
305
306static inline u64 vhost64_to_cpu(struct vhost_virtqueue *vq, __virtio64 val)
307{
Greg Kurzab27c072015-04-24 14:25:12 +0200308 return __virtio64_to_cpu(vhost_is_little_endian(vq), val);
Michael S. Tsirkine05fd122014-10-24 11:48:17 +0300309}
310
311static inline __virtio64 cpu_to_vhost64(struct vhost_virtqueue *vq, u64 val)
312{
Greg Kurzab27c072015-04-24 14:25:12 +0200313 return __cpu_to_virtio64(vhost_is_little_endian(vq), val);
Michael S. Tsirkine05fd122014-10-24 11:48:17 +0300314}
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000315#endif