blob: 42a8c2a13ab1b1fc5b90f3f6b43f69572fa242ac [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 Wang7f466032019-05-24 04:12:18 -040015#include <linux/pagemap.h>
16#include <linux/mmu_notifier.h>
17#include <asm/cacheflush.h>
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000018
Tejun Heoc23f34452010-06-02 20:40:00 +020019struct vhost_work;
20typedef void (*vhost_work_fn_t)(struct vhost_work *work);
21
Jason Wang04b96e52016-04-25 22:14:33 -040022#define VHOST_WORK_QUEUED 1
Tejun Heoc23f34452010-06-02 20:40:00 +020023struct vhost_work {
Jason Wang04b96e52016-04-25 22:14:33 -040024 struct llist_node node;
Tejun Heoc23f34452010-06-02 20:40:00 +020025 vhost_work_fn_t fn;
Jason Wang04b96e52016-04-25 22:14:33 -040026 unsigned long flags;
Tejun Heoc23f34452010-06-02 20:40:00 +020027};
28
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000029/* Poll a file (eventfd or socket) */
30/* Note: there's nothing vhost specific about this structure. */
31struct vhost_poll {
32 poll_table table;
33 wait_queue_head_t *wqh;
Ingo Molnarac6424b2017-06-20 12:06:13 +020034 wait_queue_entry_t wait;
Tejun Heoc23f34452010-06-02 20:40:00 +020035 struct vhost_work work;
Al Viro58e3b602017-07-03 23:50:40 -040036 __poll_t mask;
Tejun Heoc23f34452010-06-02 20:40:00 +020037 struct vhost_dev *dev;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000038};
39
Stefan Hajnoczi163049a2012-07-21 06:55:37 +000040void vhost_work_init(struct vhost_work *work, vhost_work_fn_t fn);
41void vhost_work_queue(struct vhost_dev *dev, struct vhost_work *work);
Jason Wang526d3e72016-03-04 06:24:51 -050042bool vhost_has_work(struct vhost_dev *dev);
Stefan Hajnoczi163049a2012-07-21 06:55:37 +000043
Tejun Heoc23f34452010-06-02 20:40:00 +020044void vhost_poll_init(struct vhost_poll *poll, vhost_work_fn_t fn,
Al Viro58e3b602017-07-03 23:50:40 -040045 __poll_t mask, struct vhost_dev *dev);
Jason Wang2b8b3282013-01-28 01:05:18 +000046int vhost_poll_start(struct vhost_poll *poll, struct file *file);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000047void vhost_poll_stop(struct vhost_poll *poll);
48void vhost_poll_flush(struct vhost_poll *poll);
49void vhost_poll_queue(struct vhost_poll *poll);
Asias He6ac1afb2013-05-06 16:38:21 +080050void vhost_work_flush(struct vhost_dev *dev, struct vhost_work *work);
Sonny Rao26b36602018-03-14 10:05:06 -070051long vhost_vring_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000052
53struct vhost_log {
54 u64 addr;
55 u64 len;
56};
57
Jason Wanga9709d62016-06-23 02:04:31 -040058#define START(node) ((node)->start)
59#define LAST(node) ((node)->last)
60
61struct 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;
Jason Wang6b1e6cc2016-06-23 02:04:32 -040068 __u32 perm;
69 __u32 flags_padding;
Jason Wanga9709d62016-06-23 02:04:31 -040070 __u64 __subtree_last;
71};
72
73struct vhost_umem {
Davidlohr Buesof808c132017-09-08 16:15:08 -070074 struct rb_root_cached umem_tree;
Jason Wanga9709d62016-06-23 02:04:31 -040075 struct list_head umem_list;
Jason Wang6b1e6cc2016-06-23 02:04:32 -040076 int numem;
Jason Wanga9709d62016-06-23 02:04:31 -040077};
78
Jason Wangf8894912017-02-28 17:56:02 +080079enum 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
Jason Wang7f466032019-05-24 04:12:18 -040086struct vhost_map {
87 int npages;
88 void *addr;
89 struct page **pages;
90};
91
92struct vhost_uaddr {
93 unsigned long uaddr;
94 size_t size;
95 bool write;
96};
97
Michael S. Tsirkin0b4a7092019-06-06 17:17:36 -040098#if defined(CONFIG_MMU_NOTIFIER) && ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE == 0
Michael S. Tsirkin73f628e2019-07-26 07:49:29 -040099#define VHOST_ARCH_CAN_ACCEL_UACCESS 0
Michael S. Tsirkin0b4a7092019-06-06 17:17:36 -0400100#else
101#define VHOST_ARCH_CAN_ACCEL_UACCESS 0
102#endif
Jason Wang7f466032019-05-24 04:12:18 -0400103
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000104/* The virtqueue structure describes a queue attached to a device. */
105struct 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;
Jason Wang7f466032019-05-24 04:12:18 -0400114
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
Jason Wangf8894912017-02-28 17:56:02 +0800128 const struct vhost_umem_node *meta_iotlb[VHOST_NUM_ADDRS];
Jason Wang7f466032019-05-24 04:12:18 -0400129
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000130 struct file *kick;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000131 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. */
Tejun Heoc23f34452010-06-02 20:40:00 +0200138 vhost_work_fn_t handle_kick;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000139
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
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300152 /* 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
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000158 /* Log writes to used structure. */
159 bool log_used;
160 u64 log_addr;
161
Jason Wange0e9b402010-09-14 23:53:05 +0800162 struct iovec iov[UIO_MAXIOV];
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400163 struct iovec iotlb_iov[64];
Jason Wange0e9b402010-09-14 23:53:05 +0800164 struct iovec *indirect;
Jason Wange0e9b402010-09-14 23:53:05 +0800165 struct vring_used_elem *heads;
Asias He22fa90c2013-05-07 14:54:36 +0800166 /* Protected by virtqueue mutex. */
Jason Wanga9709d62016-06-23 02:04:31 -0400167 struct vhost_umem *umem;
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400168 struct vhost_umem *iotlb;
Asias He22fa90c2013-05-07 14:54:36 +0800169 void *private_data;
Michael S. Tsirkinbd827522014-10-24 14:08:18 +0300170 u64 acked_features;
Jason Wang429711a2018-08-06 11:17:47 +0800171 u64 acked_backend_features;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000172 /* Log write descriptors */
173 void __user *log_base;
Jason Wange0e9b402010-09-14 23:53:05 +0800174 struct vhost_log *log;
Greg Kurz2751c982015-04-24 14:27:24 +0200175
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
Jason Wang03088132016-03-04 06:24:53 -0500183 u32 busyloop_timeout;
Jason Wang7f466032019-05-24 04:12:18 -0400184 spinlock_t mmu_lock;
185 int invalidate_count;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000186};
187
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400188struct vhost_msg_node {
Jason Wang429711a2018-08-06 11:17:47 +0800189 union {
190 struct vhost_msg msg;
191 struct vhost_msg_v2 msg_v2;
192 };
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400193 struct vhost_virtqueue *vq;
194 struct list_head node;
195};
196
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000197struct vhost_dev {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000198 struct mm_struct *mm;
Jason Wang7f466032019-05-24 04:12:18 -0400199#ifdef CONFIG_MMU_NOTIFIER
200 struct mmu_notifier mmu_notifier;
201#endif
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000202 struct mutex mutex;
Asias He3ab2e422013-04-27 11:16:48 +0800203 struct vhost_virtqueue **vqs;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000204 int nvqs;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000205 struct eventfd_ctx *log_ctx;
Jason Wang04b96e52016-04-25 22:14:33 -0400206 struct llist_head work_list;
Tejun Heoc23f34452010-06-02 20:40:00 +0200207 struct task_struct *worker;
Jason Wanga9709d62016-06-23 02:04:31 -0400208 struct vhost_umem *umem;
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400209 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;
Jason Wangb46a0bf2019-01-28 15:05:05 +0800214 int iov_limit;
Jason Wange82b9b02019-05-17 00:29:49 -0400215 int weight;
216 int byte_weight;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000217};
218
Jason Wange82b9b02019-05-17 00:29:49 -0400219bool vhost_exceeds_weight(struct vhost_virtqueue *vq, int pkts, int total_len);
Jason Wangb46a0bf2019-01-28 15:05:05 +0800220void vhost_dev_init(struct vhost_dev *, struct vhost_virtqueue **vqs,
Jason Wange82b9b02019-05-17 00:29:49 -0400221 int nvqs, int iov_limit, int weight, int byte_weight);
Asias He54db63c2013-05-06 11:15:59 +0800222long vhost_dev_set_owner(struct vhost_dev *dev);
Michael S. Tsirkin05c05352013-06-06 15:20:39 +0300223bool vhost_dev_has_owner(struct vhost_dev *dev);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000224long vhost_dev_check_owner(struct vhost_dev *);
Jason Wanga9709d62016-06-23 02:04:31 -0400225struct vhost_umem *vhost_dev_reset_owner_prepare(void);
226void vhost_dev_reset_owner(struct vhost_dev *, struct vhost_umem *);
夷则(Caspar)f6f93f72017-12-25 00:08:58 +0800227void vhost_dev_cleanup(struct vhost_dev *);
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000228void vhost_dev_stop(struct vhost_dev *);
Michael S. Tsirkin935cdee2012-12-06 14:03:34 +0200229long vhost_dev_ioctl(struct vhost_dev *, unsigned int ioctl, void __user *argp);
Sonny Rao26b36602018-03-14 10:05:06 -0700230long vhost_vring_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp);
Stefan Hajnocziddd3d402018-04-11 10:35:41 +0800231bool vhost_vq_access_ok(struct vhost_virtqueue *vq);
232bool vhost_log_access_ok(struct vhost_dev *);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000233
Michael S. Tsirkin47283be2014-06-05 15:20:27 +0300234int vhost_get_vq_desc(struct vhost_virtqueue *,
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +0300235 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);
David Stevens8dd014a2010-07-27 18:52:21 +0300238void vhost_discard_vq_desc(struct vhost_virtqueue *, int n);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000239
Greg Kurz80f7d032016-02-16 15:59:44 +0100240int vhost_vq_init_access(struct vhost_virtqueue *);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000241int vhost_add_used(struct vhost_virtqueue *, unsigned int head, int len);
David Stevens8dd014a2010-07-27 18:52:21 +0300242int vhost_add_used_n(struct vhost_virtqueue *, struct vring_used_elem *heads,
243 unsigned count);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000244void vhost_add_used_and_signal(struct vhost_dev *, struct vhost_virtqueue *,
David Stevens8dd014a2010-07-27 18:52:21 +0300245 unsigned int id, int len);
246void vhost_add_used_and_signal_n(struct vhost_dev *, struct vhost_virtqueue *,
247 struct vring_used_elem *heads, unsigned count);
248void vhost_signal(struct vhost_dev *, struct vhost_virtqueue *);
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300249void vhost_disable_notify(struct vhost_dev *, struct vhost_virtqueue *);
Jason Wangd4a60602016-03-04 06:24:52 -0500250bool vhost_vq_avail_empty(struct vhost_dev *, struct vhost_virtqueue *);
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300251bool vhost_enable_notify(struct vhost_dev *, struct vhost_virtqueue *);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000252
253int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log,
Jason Wangcc5e7102019-01-16 16:54:42 +0800254 unsigned int log_num, u64 len,
255 struct iovec *iov, int count);
Jason Wang9b5e8302019-05-24 04:12:15 -0400256int vq_meta_prefetch(struct vhost_virtqueue *vq);
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400257
258struct vhost_msg_node *vhost_new_msg(struct vhost_virtqueue *vq, int type);
259void vhost_enqueue_msg(struct vhost_dev *dev,
260 struct list_head *head,
261 struct vhost_msg_node *node);
262struct vhost_msg_node *vhost_dequeue_msg(struct vhost_dev *dev,
263 struct list_head *head);
Al Viroafc9a422017-07-03 06:39:46 -0400264__poll_t vhost_chr_poll(struct file *file, struct vhost_dev *dev,
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400265 poll_table *wait);
266ssize_t vhost_chr_read_iter(struct vhost_dev *dev, struct iov_iter *to,
267 int noblock);
268ssize_t vhost_chr_write_iter(struct vhost_dev *dev,
269 struct iov_iter *from);
270int vhost_init_device_iotlb(struct vhost_dev *d, bool enabled);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000271
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000272#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
278enum {
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300279 VHOST_FEATURES = (1ULL << VIRTIO_F_NOTIFY_ON_EMPTY) |
280 (1ULL << VIRTIO_RING_F_INDIRECT_DESC) |
281 (1ULL << VIRTIO_RING_F_EVENT_IDX) |
Michael S. Tsirkin4e9fa502015-09-09 22:24:56 +0300282 (1ULL << VHOST_F_LOG_ALL) |
283 (1ULL << VIRTIO_F_ANY_LAYOUT) |
284 (1ULL << VIRTIO_F_VERSION_1)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000285};
286
Michael S. Tsirkinbd827522014-10-24 14:08:18 +0300287static inline bool vhost_has_feature(struct vhost_virtqueue *vq, int bit)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000288{
Michael S. Tsirkinbd827522014-10-24 14:08:18 +0300289 return vq->acked_features & (1ULL << bit);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000290}
Michael S. Tsirkine05fd122014-10-24 11:48:17 +0300291
Jason Wang429711a2018-08-06 11:17:47 +0800292static inline bool vhost_backend_has_feature(struct vhost_virtqueue *vq, int bit)
293{
294 return vq->acked_backend_features & (1ULL << bit);
295}
296
Michael S. Tsirkine407f392015-10-27 11:37:39 +0200297#ifdef CONFIG_VHOST_CROSS_ENDIAN_LEGACY
Greg Kurzab27c072015-04-24 14:25:12 +0200298static inline bool vhost_is_little_endian(struct vhost_virtqueue *vq)
299{
Greg Kurz2751c982015-04-24 14:27:24 +0200300 return vq->is_le;
Greg Kurzab27c072015-04-24 14:25:12 +0200301}
Michael S. Tsirkine407f392015-10-27 11:37:39 +0200302#else
303static inline bool vhost_is_little_endian(struct vhost_virtqueue *vq)
304{
305 return virtio_legacy_is_little_endian() || vq->is_le;
306}
307#endif
Greg Kurzab27c072015-04-24 14:25:12 +0200308
Michael S. Tsirkine05fd122014-10-24 11:48:17 +0300309/* Memory accessors */
310static inline u16 vhost16_to_cpu(struct vhost_virtqueue *vq, __virtio16 val)
311{
Greg Kurzab27c072015-04-24 14:25:12 +0200312 return __virtio16_to_cpu(vhost_is_little_endian(vq), val);
Michael S. Tsirkine05fd122014-10-24 11:48:17 +0300313}
314
315static inline __virtio16 cpu_to_vhost16(struct vhost_virtqueue *vq, u16 val)
316{
Greg Kurzab27c072015-04-24 14:25:12 +0200317 return __cpu_to_virtio16(vhost_is_little_endian(vq), val);
Michael S. Tsirkine05fd122014-10-24 11:48:17 +0300318}
319
320static inline u32 vhost32_to_cpu(struct vhost_virtqueue *vq, __virtio32 val)
321{
Greg Kurzab27c072015-04-24 14:25:12 +0200322 return __virtio32_to_cpu(vhost_is_little_endian(vq), val);
Michael S. Tsirkine05fd122014-10-24 11:48:17 +0300323}
324
325static inline __virtio32 cpu_to_vhost32(struct vhost_virtqueue *vq, u32 val)
326{
Greg Kurzab27c072015-04-24 14:25:12 +0200327 return __cpu_to_virtio32(vhost_is_little_endian(vq), val);
Michael S. Tsirkine05fd122014-10-24 11:48:17 +0300328}
329
330static inline u64 vhost64_to_cpu(struct vhost_virtqueue *vq, __virtio64 val)
331{
Greg Kurzab27c072015-04-24 14:25:12 +0200332 return __virtio64_to_cpu(vhost_is_little_endian(vq), val);
Michael S. Tsirkine05fd122014-10-24 11:48:17 +0300333}
334
335static inline __virtio64 cpu_to_vhost64(struct vhost_virtqueue *vq, u64 val)
336{
Greg Kurzab27c072015-04-24 14:25:12 +0200337 return __cpu_to_virtio64(vhost_is_little_endian(vq), val);
Michael S. Tsirkine05fd122014-10-24 11:48:17 +0300338}
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000339#endif