blob: e016cd3fa02f788096906e3aba3fd53da4b409a9 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Michael S. Tsirkin3a4d5c94e2010-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. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00009#include <linux/poll.h>
10#include <linux/file.h>
Michael S. Tsirkin3a4d5c94e2010-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>
Zhu Lingshan265a0ad2020-07-31 14:55:28 +080016#include <linux/irqbypass.h>
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +000017
Tejun Heoc23f34452010-06-02 20:40:00 +020018struct vhost_work;
19typedef void (*vhost_work_fn_t)(struct vhost_work *work);
20
Jason Wang04b96e52016-04-25 22:14:33 -040021#define VHOST_WORK_QUEUED 1
Tejun Heoc23f34452010-06-02 20:40:00 +020022struct vhost_work {
Jason Wang04b96e52016-04-25 22:14:33 -040023 struct llist_node node;
Tejun Heoc23f34452010-06-02 20:40:00 +020024 vhost_work_fn_t fn;
Jason Wang04b96e52016-04-25 22:14:33 -040025 unsigned long flags;
Tejun Heoc23f34452010-06-02 20:40:00 +020026};
27
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +000028/* Poll a file (eventfd or socket) */
29/* Note: there's nothing vhost specific about this structure. */
30struct vhost_poll {
31 poll_table table;
32 wait_queue_head_t *wqh;
Ingo Molnarac6424b2017-06-20 12:06:13 +020033 wait_queue_entry_t wait;
Tejun Heoc23f34452010-06-02 20:40:00 +020034 struct vhost_work work;
Al Viro58e3b602017-07-03 23:50:40 -040035 __poll_t mask;
Tejun Heoc23f34452010-06-02 20:40:00 +020036 struct vhost_dev *dev;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +000037};
38
Stefan Hajnoczi163049a2012-07-21 06:55:37 +000039void vhost_work_init(struct vhost_work *work, vhost_work_fn_t fn);
40void vhost_work_queue(struct vhost_dev *dev, struct vhost_work *work);
Jason Wang526d3e72016-03-04 06:24:51 -050041bool vhost_has_work(struct vhost_dev *dev);
Stefan Hajnoczi163049a2012-07-21 06:55:37 +000042
Tejun Heoc23f34452010-06-02 20:40:00 +020043void vhost_poll_init(struct vhost_poll *poll, vhost_work_fn_t fn,
Al Viro58e3b602017-07-03 23:50:40 -040044 __poll_t mask, struct vhost_dev *dev);
Jason Wang2b8b3282013-01-28 01:05:18 +000045int vhost_poll_start(struct vhost_poll *poll, struct file *file);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +000046void vhost_poll_stop(struct vhost_poll *poll);
47void vhost_poll_flush(struct vhost_poll *poll);
48void vhost_poll_queue(struct vhost_poll *poll);
Asias He6ac1afb2013-05-06 16:38:21 +080049void vhost_work_flush(struct vhost_dev *dev, struct vhost_work *work);
Sonny Rao26b36602018-03-14 10:05:06 -070050long vhost_vring_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +000051
52struct vhost_log {
53 u64 addr;
54 u64 len;
55};
56
Jason Wangf8894912017-02-28 17:56:02 +080057enum vhost_uaddr_type {
58 VHOST_ADDR_DESC = 0,
59 VHOST_ADDR_AVAIL = 1,
60 VHOST_ADDR_USED = 2,
61 VHOST_NUM_ADDRS = 3,
62};
63
Zhu Lingshan265a0ad2020-07-31 14:55:28 +080064struct vhost_vring_call {
65 struct eventfd_ctx *ctx;
66 struct irq_bypass_producer producer;
Zhu Lingshan265a0ad2020-07-31 14:55:28 +080067};
68
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +000069/* The virtqueue structure describes a queue attached to a device. */
70struct vhost_virtqueue {
71 struct vhost_dev *dev;
72
73 /* The actual ring of buffers. */
74 struct mutex mutex;
75 unsigned int num;
Michael S. Tsirkina865e422020-04-06 08:42:55 -040076 vring_desc_t __user *desc;
77 vring_avail_t __user *avail;
78 vring_used_t __user *used;
Jason Wang0bbe3062020-03-26 22:01:19 +080079 const struct vhost_iotlb_map *meta_iotlb[VHOST_NUM_ADDRS];
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +000080 struct file *kick;
Zhu Lingshan265a0ad2020-07-31 14:55:28 +080081 struct vhost_vring_call call_ctx;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +000082 struct eventfd_ctx *error_ctx;
83 struct eventfd_ctx *log_ctx;
84
85 struct vhost_poll poll;
86
87 /* The routine to call when the Guest pings us, or timeout. */
Tejun Heoc23f34452010-06-02 20:40:00 +020088 vhost_work_fn_t handle_kick;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +000089
90 /* Last available index we saw. */
91 u16 last_avail_idx;
92
93 /* Caches available index value from user. */
94 u16 avail_idx;
95
96 /* Last index we used. */
97 u16 last_used_idx;
98
99 /* Used flags */
100 u16 used_flags;
101
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300102 /* Last used index value we have signalled on */
103 u16 signalled_used;
104
105 /* Last used index value we have signalled on */
106 bool signalled_used_valid;
107
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000108 /* Log writes to used structure. */
109 bool log_used;
110 u64 log_addr;
111
Jason Wange0e9b402010-09-14 23:53:05 +0800112 struct iovec iov[UIO_MAXIOV];
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400113 struct iovec iotlb_iov[64];
Jason Wange0e9b402010-09-14 23:53:05 +0800114 struct iovec *indirect;
Jason Wange0e9b402010-09-14 23:53:05 +0800115 struct vring_used_elem *heads;
Asias He22fa90c2013-05-07 14:54:36 +0800116 /* Protected by virtqueue mutex. */
Jason Wang0bbe3062020-03-26 22:01:19 +0800117 struct vhost_iotlb *umem;
118 struct vhost_iotlb *iotlb;
Asias He22fa90c2013-05-07 14:54:36 +0800119 void *private_data;
Michael S. Tsirkinbd827522014-10-24 14:08:18 +0300120 u64 acked_features;
Jason Wang429711a2018-08-06 11:17:47 +0800121 u64 acked_backend_features;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000122 /* Log write descriptors */
123 void __user *log_base;
Jason Wange0e9b402010-09-14 23:53:05 +0800124 struct vhost_log *log;
Li Wang5e5e8732020-09-15 02:08:09 +0800125 struct iovec log_iov[64];
Greg Kurz2751c982015-04-24 14:27:24 +0200126
127 /* Ring endianness. Defaults to legacy native endianness.
128 * Set to true when starting a modern virtio device. */
129 bool is_le;
130#ifdef CONFIG_VHOST_CROSS_ENDIAN_LEGACY
131 /* Ring endianness requested by userspace for cross-endian support. */
132 bool user_be;
133#endif
Jason Wang03088132016-03-04 06:24:53 -0500134 u32 busyloop_timeout;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000135};
136
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400137struct vhost_msg_node {
Jason Wang429711a2018-08-06 11:17:47 +0800138 union {
139 struct vhost_msg msg;
140 struct vhost_msg_v2 msg_v2;
141 };
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400142 struct vhost_virtqueue *vq;
143 struct list_head node;
144};
145
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000146struct vhost_dev {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000147 struct mm_struct *mm;
148 struct mutex mutex;
Asias He3ab2e422013-04-27 11:16:48 +0800149 struct vhost_virtqueue **vqs;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000150 int nvqs;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000151 struct eventfd_ctx *log_ctx;
Jason Wang04b96e52016-04-25 22:14:33 -0400152 struct llist_head work_list;
Tejun Heoc23f34452010-06-02 20:40:00 +0200153 struct task_struct *worker;
Jason Wang0bbe3062020-03-26 22:01:19 +0800154 struct vhost_iotlb *umem;
155 struct vhost_iotlb *iotlb;
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400156 spinlock_t iotlb_lock;
157 struct list_head read_list;
158 struct list_head pending_list;
159 wait_queue_head_t wait;
Jason Wangb46a0bf2019-01-28 15:05:05 +0800160 int iov_limit;
Jason Wange82b9b02019-05-17 00:29:49 -0400161 int weight;
162 int byte_weight;
Andrey Konovalov8f6a7f92019-12-04 16:52:50 -0800163 u64 kcov_handle;
Jason Wang01fcb1c2020-05-29 16:02:58 +0800164 bool use_worker;
Jason Wang792a4f22020-03-26 22:01:18 +0800165 int (*msg_handler)(struct vhost_dev *dev,
166 struct vhost_iotlb_msg *msg);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000167};
168
Jason Wange82b9b02019-05-17 00:29:49 -0400169bool vhost_exceeds_weight(struct vhost_virtqueue *vq, int pkts, int total_len);
Jason Wangb46a0bf2019-01-28 15:05:05 +0800170void vhost_dev_init(struct vhost_dev *, struct vhost_virtqueue **vqs,
Jason Wang792a4f22020-03-26 22:01:18 +0800171 int nvqs, int iov_limit, int weight, int byte_weight,
Jason Wang01fcb1c2020-05-29 16:02:58 +0800172 bool use_worker,
Jason Wang792a4f22020-03-26 22:01:18 +0800173 int (*msg_handler)(struct vhost_dev *dev,
174 struct vhost_iotlb_msg *msg));
Asias He54db63c2013-05-06 11:15:59 +0800175long vhost_dev_set_owner(struct vhost_dev *dev);
Michael S. Tsirkin05c05352013-06-06 15:20:39 +0300176bool vhost_dev_has_owner(struct vhost_dev *dev);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000177long vhost_dev_check_owner(struct vhost_dev *);
Jason Wang0bbe3062020-03-26 22:01:19 +0800178struct vhost_iotlb *vhost_dev_reset_owner_prepare(void);
179void vhost_dev_reset_owner(struct vhost_dev *dev, struct vhost_iotlb *iotlb);
夷则(Caspar)f6f93f72017-12-25 00:08:58 +0800180void vhost_dev_cleanup(struct vhost_dev *);
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000181void vhost_dev_stop(struct vhost_dev *);
Michael S. Tsirkin935cdee2012-12-06 14:03:34 +0200182long vhost_dev_ioctl(struct vhost_dev *, unsigned int ioctl, void __user *argp);
Sonny Rao26b36602018-03-14 10:05:06 -0700183long vhost_vring_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp);
Stefan Hajnocziddd3d402018-04-11 10:35:41 +0800184bool vhost_vq_access_ok(struct vhost_virtqueue *vq);
185bool vhost_log_access_ok(struct vhost_dev *);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000186
Michael S. Tsirkin47283be2014-06-05 15:20:27 +0300187int vhost_get_vq_desc(struct vhost_virtqueue *,
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +0300188 struct iovec iov[], unsigned int iov_count,
189 unsigned int *out_num, unsigned int *in_num,
190 struct vhost_log *log, unsigned int *log_num);
David Stevens8dd014a2010-07-27 18:52:21 +0300191void vhost_discard_vq_desc(struct vhost_virtqueue *, int n);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000192
Greg Kurz80f7d032016-02-16 15:59:44 +0100193int vhost_vq_init_access(struct vhost_virtqueue *);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000194int vhost_add_used(struct vhost_virtqueue *, unsigned int head, int len);
David Stevens8dd014a2010-07-27 18:52:21 +0300195int vhost_add_used_n(struct vhost_virtqueue *, struct vring_used_elem *heads,
196 unsigned count);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000197void vhost_add_used_and_signal(struct vhost_dev *, struct vhost_virtqueue *,
David Stevens8dd014a2010-07-27 18:52:21 +0300198 unsigned int id, int len);
199void vhost_add_used_and_signal_n(struct vhost_dev *, struct vhost_virtqueue *,
200 struct vring_used_elem *heads, unsigned count);
201void vhost_signal(struct vhost_dev *, struct vhost_virtqueue *);
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300202void vhost_disable_notify(struct vhost_dev *, struct vhost_virtqueue *);
Jason Wangd4a60602016-03-04 06:24:52 -0500203bool vhost_vq_avail_empty(struct vhost_dev *, struct vhost_virtqueue *);
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300204bool vhost_enable_notify(struct vhost_dev *, struct vhost_virtqueue *);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000205
206int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log,
Jason Wangcc5e7102019-01-16 16:54:42 +0800207 unsigned int log_num, u64 len,
208 struct iovec *iov, int count);
Jason Wang9b5e8302019-05-24 04:12:15 -0400209int vq_meta_prefetch(struct vhost_virtqueue *vq);
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400210
211struct vhost_msg_node *vhost_new_msg(struct vhost_virtqueue *vq, int type);
212void vhost_enqueue_msg(struct vhost_dev *dev,
213 struct list_head *head,
214 struct vhost_msg_node *node);
215struct vhost_msg_node *vhost_dequeue_msg(struct vhost_dev *dev,
216 struct list_head *head);
Jason Wang460f7ce2020-08-04 19:20:38 +0300217void vhost_set_backend_features(struct vhost_dev *dev, u64 features);
218
Al Viroafc9a422017-07-03 06:39:46 -0400219__poll_t vhost_chr_poll(struct file *file, struct vhost_dev *dev,
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400220 poll_table *wait);
221ssize_t vhost_chr_read_iter(struct vhost_dev *dev, struct iov_iter *to,
222 int noblock);
223ssize_t vhost_chr_write_iter(struct vhost_dev *dev,
224 struct iov_iter *from);
225int vhost_init_device_iotlb(struct vhost_dev *d, bool enabled);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000226
Jason Wang0bbe3062020-03-26 22:01:19 +0800227void vhost_iotlb_map_free(struct vhost_iotlb *iotlb,
228 struct vhost_iotlb_map *map);
229
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000230#define vq_err(vq, fmt, ...) do { \
231 pr_debug(pr_fmt(fmt), ##__VA_ARGS__); \
232 if ((vq)->error_ctx) \
233 eventfd_signal((vq)->error_ctx, 1);\
234 } while (0)
235
236enum {
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300237 VHOST_FEATURES = (1ULL << VIRTIO_F_NOTIFY_ON_EMPTY) |
238 (1ULL << VIRTIO_RING_F_INDIRECT_DESC) |
239 (1ULL << VIRTIO_RING_F_EVENT_IDX) |
Michael S. Tsirkin4e9fa502015-09-09 22:24:56 +0300240 (1ULL << VHOST_F_LOG_ALL) |
241 (1ULL << VIRTIO_F_ANY_LAYOUT) |
242 (1ULL << VIRTIO_F_VERSION_1)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000243};
244
Eugenio Pérez247643f2020-03-31 21:27:57 +0200245/**
246 * vhost_vq_set_backend - Set backend.
247 *
248 * @vq Virtqueue.
249 * @private_data The private data.
250 *
251 * Context: Need to call with vq->mutex acquired.
252 */
253static inline void vhost_vq_set_backend(struct vhost_virtqueue *vq,
254 void *private_data)
255{
256 vq->private_data = private_data;
257}
258
259/**
260 * vhost_vq_get_backend - Get backend.
261 *
262 * @vq Virtqueue.
263 *
264 * Context: Need to call with vq->mutex acquired.
265 * Return: Private data previously set with vhost_vq_set_backend.
266 */
267static inline void *vhost_vq_get_backend(struct vhost_virtqueue *vq)
268{
269 return vq->private_data;
270}
271
Michael S. Tsirkinbd827522014-10-24 14:08:18 +0300272static inline bool vhost_has_feature(struct vhost_virtqueue *vq, int bit)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000273{
Michael S. Tsirkinbd827522014-10-24 14:08:18 +0300274 return vq->acked_features & (1ULL << bit);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000275}
Michael S. Tsirkine05fd122014-10-24 11:48:17 +0300276
Jason Wang429711a2018-08-06 11:17:47 +0800277static inline bool vhost_backend_has_feature(struct vhost_virtqueue *vq, int bit)
278{
279 return vq->acked_backend_features & (1ULL << bit);
280}
281
Michael S. Tsirkine407f392015-10-27 11:37:39 +0200282#ifdef CONFIG_VHOST_CROSS_ENDIAN_LEGACY
Greg Kurzab27c072015-04-24 14:25:12 +0200283static inline bool vhost_is_little_endian(struct vhost_virtqueue *vq)
284{
Greg Kurz2751c982015-04-24 14:27:24 +0200285 return vq->is_le;
Greg Kurzab27c072015-04-24 14:25:12 +0200286}
Michael S. Tsirkine407f392015-10-27 11:37:39 +0200287#else
288static inline bool vhost_is_little_endian(struct vhost_virtqueue *vq)
289{
290 return virtio_legacy_is_little_endian() || vq->is_le;
291}
292#endif
Greg Kurzab27c072015-04-24 14:25:12 +0200293
Michael S. Tsirkine05fd122014-10-24 11:48:17 +0300294/* Memory accessors */
295static inline u16 vhost16_to_cpu(struct vhost_virtqueue *vq, __virtio16 val)
296{
Greg Kurzab27c072015-04-24 14:25:12 +0200297 return __virtio16_to_cpu(vhost_is_little_endian(vq), val);
Michael S. Tsirkine05fd122014-10-24 11:48:17 +0300298}
299
300static inline __virtio16 cpu_to_vhost16(struct vhost_virtqueue *vq, u16 val)
301{
Greg Kurzab27c072015-04-24 14:25:12 +0200302 return __cpu_to_virtio16(vhost_is_little_endian(vq), val);
Michael S. Tsirkine05fd122014-10-24 11:48:17 +0300303}
304
305static inline u32 vhost32_to_cpu(struct vhost_virtqueue *vq, __virtio32 val)
306{
Greg Kurzab27c072015-04-24 14:25:12 +0200307 return __virtio32_to_cpu(vhost_is_little_endian(vq), val);
Michael S. Tsirkine05fd122014-10-24 11:48:17 +0300308}
309
310static inline __virtio32 cpu_to_vhost32(struct vhost_virtqueue *vq, u32 val)
311{
Greg Kurzab27c072015-04-24 14:25:12 +0200312 return __cpu_to_virtio32(vhost_is_little_endian(vq), val);
Michael S. Tsirkine05fd122014-10-24 11:48:17 +0300313}
314
315static inline u64 vhost64_to_cpu(struct vhost_virtqueue *vq, __virtio64 val)
316{
Greg Kurzab27c072015-04-24 14:25:12 +0200317 return __virtio64_to_cpu(vhost_is_little_endian(vq), val);
Michael S. Tsirkine05fd122014-10-24 11:48:17 +0300318}
319
320static inline __virtio64 cpu_to_vhost64(struct vhost_virtqueue *vq, u64 val)
321{
Greg Kurzab27c072015-04-24 14:25:12 +0200322 return __cpu_to_virtio64(vhost_is_little_endian(vq), val);
Michael S. Tsirkine05fd122014-10-24 11:48:17 +0300323}
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000324#endif