vhost.h 8.4 KB
Newer Older
1
/* SPDX-License-Identifier: GPL-2.0 */
2 3 4 5 6 7 8 9 10 11 12 13
#ifndef _VHOST_H
#define _VHOST_H

#include <linux/eventfd.h>
#include <linux/vhost.h>
#include <linux/mm.h>
#include <linux/mutex.h>
#include <linux/poll.h>
#include <linux/file.h>
#include <linux/uio.h>
#include <linux/virtio_config.h>
#include <linux/virtio_ring.h>
A
Arun Sharma 已提交
14
#include <linux/atomic.h>
15

16 17 18
struct vhost_work;
typedef void (*vhost_work_fn_t)(struct vhost_work *work);

J
Jason Wang 已提交
19
#define VHOST_WORK_QUEUED 1
20
struct vhost_work {
J
Jason Wang 已提交
21
	struct llist_node	  node;
22 23 24 25 26
	vhost_work_fn_t		  fn;
	wait_queue_head_t	  done;
	int			  flushing;
	unsigned		  queue_seq;
	unsigned		  done_seq;
J
Jason Wang 已提交
27
	unsigned long		  flags;
28 29
};

30 31 32 33 34
/* Poll a file (eventfd or socket) */
/* Note: there's nothing vhost specific about this structure. */
struct vhost_poll {
	poll_table                table;
	wait_queue_head_t        *wqh;
35
	wait_queue_entry_t              wait;
36
	struct vhost_work	  work;
A
Al Viro 已提交
37
	__poll_t		  mask;
38
	struct vhost_dev	 *dev;
39 40
};

41 42
void vhost_work_init(struct vhost_work *work, vhost_work_fn_t fn);
void vhost_work_queue(struct vhost_dev *dev, struct vhost_work *work);
J
Jason Wang 已提交
43
bool vhost_has_work(struct vhost_dev *dev);
44

45
void vhost_poll_init(struct vhost_poll *poll, vhost_work_fn_t fn,
A
Al Viro 已提交
46
		     __poll_t mask, struct vhost_dev *dev);
47
int vhost_poll_start(struct vhost_poll *poll, struct file *file);
48 49 50
void vhost_poll_stop(struct vhost_poll *poll);
void vhost_poll_flush(struct vhost_poll *poll);
void vhost_poll_queue(struct vhost_poll *poll);
A
Asias He 已提交
51 52
void vhost_work_flush(struct vhost_dev *dev, struct vhost_work *work);
long vhost_vring_ioctl(struct vhost_dev *d, int ioctl, void __user *argp);
53 54 55 56 57 58

struct vhost_log {
	u64 addr;
	u64 len;
};

59 60 61 62 63 64 65 66 67 68
#define START(node) ((node)->start)
#define LAST(node) ((node)->last)

struct vhost_umem_node {
	struct rb_node rb;
	struct list_head link;
	__u64 start;
	__u64 last;
	__u64 size;
	__u64 userspace_addr;
J
Jason Wang 已提交
69 70
	__u32 perm;
	__u32 flags_padding;
71 72 73 74
	__u64 __subtree_last;
};

struct vhost_umem {
75
	struct rb_root_cached umem_tree;
76
	struct list_head umem_list;
J
Jason Wang 已提交
77
	int numem;
78 79
};

80 81 82 83 84 85 86
enum vhost_uaddr_type {
	VHOST_ADDR_DESC = 0,
	VHOST_ADDR_AVAIL = 1,
	VHOST_ADDR_USED = 2,
	VHOST_NUM_ADDRS = 3,
};

87 88 89 90 91 92 93 94 95 96
/* The virtqueue structure describes a queue attached to a device. */
struct vhost_virtqueue {
	struct vhost_dev *dev;

	/* The actual ring of buffers. */
	struct mutex mutex;
	unsigned int num;
	struct vring_desc __user *desc;
	struct vring_avail __user *avail;
	struct vring_used __user *used;
97
	const struct vhost_umem_node *meta_iotlb[VHOST_NUM_ADDRS];
98 99 100 101 102 103 104 105 106 107
	struct file *kick;
	struct file *call;
	struct file *error;
	struct eventfd_ctx *call_ctx;
	struct eventfd_ctx *error_ctx;
	struct eventfd_ctx *log_ctx;

	struct vhost_poll poll;

	/* The routine to call when the Guest pings us, or timeout. */
108
	vhost_work_fn_t handle_kick;
109 110 111 112 113 114 115 116 117 118 119 120 121

	/* Last available index we saw. */
	u16 last_avail_idx;

	/* Caches available index value from user. */
	u16 avail_idx;

	/* Last index we used. */
	u16 last_used_idx;

	/* Used flags */
	u16 used_flags;

M
Michael S. Tsirkin 已提交
122 123 124 125 126 127
	/* Last used index value we have signalled on */
	u16 signalled_used;

	/* Last used index value we have signalled on */
	bool signalled_used_valid;

128 129 130 131
	/* Log writes to used structure. */
	bool log_used;
	u64 log_addr;

J
Jason Wang 已提交
132
	struct iovec iov[UIO_MAXIOV];
J
Jason Wang 已提交
133
	struct iovec iotlb_iov[64];
J
Jason Wang 已提交
134 135
	struct iovec *indirect;
	struct vring_used_elem *heads;
A
Asias He 已提交
136
	/* Protected by virtqueue mutex. */
137
	struct vhost_umem *umem;
J
Jason Wang 已提交
138
	struct vhost_umem *iotlb;
A
Asias He 已提交
139
	void *private_data;
M
Michael S. Tsirkin 已提交
140
	u64 acked_features;
141 142
	/* Log write descriptors */
	void __user *log_base;
J
Jason Wang 已提交
143
	struct vhost_log *log;
144 145 146 147 148 149 150 151

	/* Ring endianness. Defaults to legacy native endianness.
	 * Set to true when starting a modern virtio device. */
	bool is_le;
#ifdef CONFIG_VHOST_CROSS_ENDIAN_LEGACY
	/* Ring endianness requested by userspace for cross-endian support. */
	bool user_be;
#endif
J
Jason Wang 已提交
152
	u32 busyloop_timeout;
153 154
};

J
Jason Wang 已提交
155 156 157 158 159 160
struct vhost_msg_node {
  struct vhost_msg msg;
  struct vhost_virtqueue *vq;
  struct list_head node;
};

161 162 163
struct vhost_dev {
	struct mm_struct *mm;
	struct mutex mutex;
164
	struct vhost_virtqueue **vqs;
165 166 167
	int nvqs;
	struct file *log_file;
	struct eventfd_ctx *log_ctx;
J
Jason Wang 已提交
168
	struct llist_head work_list;
169
	struct task_struct *worker;
170
	struct vhost_umem *umem;
J
Jason Wang 已提交
171 172 173 174 175
	struct vhost_umem *iotlb;
	spinlock_t iotlb_lock;
	struct list_head read_list;
	struct list_head pending_list;
	wait_queue_head_t wait;
176 177
};

Z
Zhi Yong Wu 已提交
178
void vhost_dev_init(struct vhost_dev *, struct vhost_virtqueue **vqs, int nvqs);
A
Asias He 已提交
179
long vhost_dev_set_owner(struct vhost_dev *dev);
180
bool vhost_dev_has_owner(struct vhost_dev *dev);
181
long vhost_dev_check_owner(struct vhost_dev *);
182 183
struct vhost_umem *vhost_dev_reset_owner_prepare(void);
void vhost_dev_reset_owner(struct vhost_dev *, struct vhost_umem *);
184
void vhost_dev_cleanup(struct vhost_dev *, bool locked);
185
void vhost_dev_stop(struct vhost_dev *);
186 187
long vhost_dev_ioctl(struct vhost_dev *, unsigned int ioctl, void __user *argp);
long vhost_vring_ioctl(struct vhost_dev *d, int ioctl, void __user *argp);
188 189 190
int vhost_vq_access_ok(struct vhost_virtqueue *vq);
int vhost_log_access_ok(struct vhost_dev *);

191
int vhost_get_vq_desc(struct vhost_virtqueue *,
192 193 194
		      struct iovec iov[], unsigned int iov_count,
		      unsigned int *out_num, unsigned int *in_num,
		      struct vhost_log *log, unsigned int *log_num);
195
void vhost_discard_vq_desc(struct vhost_virtqueue *, int n);
196

G
Greg Kurz 已提交
197
int vhost_vq_init_access(struct vhost_virtqueue *);
198
int vhost_add_used(struct vhost_virtqueue *, unsigned int head, int len);
199 200
int vhost_add_used_n(struct vhost_virtqueue *, struct vring_used_elem *heads,
		     unsigned count);
201
void vhost_add_used_and_signal(struct vhost_dev *, struct vhost_virtqueue *,
202 203 204 205
			       unsigned int id, int len);
void vhost_add_used_and_signal_n(struct vhost_dev *, struct vhost_virtqueue *,
			       struct vring_used_elem *heads, unsigned count);
void vhost_signal(struct vhost_dev *, struct vhost_virtqueue *);
M
Michael S. Tsirkin 已提交
206
void vhost_disable_notify(struct vhost_dev *, struct vhost_virtqueue *);
207
bool vhost_vq_avail_empty(struct vhost_dev *, struct vhost_virtqueue *);
M
Michael S. Tsirkin 已提交
208
bool vhost_enable_notify(struct vhost_dev *, struct vhost_virtqueue *);
209 210 211

int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log,
		    unsigned int log_num, u64 len);
J
Jason Wang 已提交
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
int vq_iotlb_prefetch(struct vhost_virtqueue *vq);

struct vhost_msg_node *vhost_new_msg(struct vhost_virtqueue *vq, int type);
void vhost_enqueue_msg(struct vhost_dev *dev,
		       struct list_head *head,
		       struct vhost_msg_node *node);
struct vhost_msg_node *vhost_dequeue_msg(struct vhost_dev *dev,
					 struct list_head *head);
unsigned int vhost_chr_poll(struct file *file, struct vhost_dev *dev,
			    poll_table *wait);
ssize_t vhost_chr_read_iter(struct vhost_dev *dev, struct iov_iter *to,
			    int noblock);
ssize_t vhost_chr_write_iter(struct vhost_dev *dev,
			     struct iov_iter *from);
int vhost_init_device_iotlb(struct vhost_dev *d, bool enabled);
227 228 229 230 231 232 233 234

#define vq_err(vq, fmt, ...) do {                                  \
		pr_debug(pr_fmt(fmt), ##__VA_ARGS__);       \
		if ((vq)->error_ctx)                               \
				eventfd_signal((vq)->error_ctx, 1);\
	} while (0)

enum {
M
Michael S. Tsirkin 已提交
235 236 237
	VHOST_FEATURES = (1ULL << VIRTIO_F_NOTIFY_ON_EMPTY) |
			 (1ULL << VIRTIO_RING_F_INDIRECT_DESC) |
			 (1ULL << VIRTIO_RING_F_EVENT_IDX) |
238 239 240
			 (1ULL << VHOST_F_LOG_ALL) |
			 (1ULL << VIRTIO_F_ANY_LAYOUT) |
			 (1ULL << VIRTIO_F_VERSION_1)
241 242
};

M
Michael S. Tsirkin 已提交
243
static inline bool vhost_has_feature(struct vhost_virtqueue *vq, int bit)
244
{
M
Michael S. Tsirkin 已提交
245
	return vq->acked_features & (1ULL << bit);
246
}
247

248
#ifdef CONFIG_VHOST_CROSS_ENDIAN_LEGACY
249 250
static inline bool vhost_is_little_endian(struct vhost_virtqueue *vq)
{
251
	return vq->is_le;
252
}
253 254 255 256 257 258
#else
static inline bool vhost_is_little_endian(struct vhost_virtqueue *vq)
{
	return virtio_legacy_is_little_endian() || vq->is_le;
}
#endif
259

260 261 262
/* Memory accessors */
static inline u16 vhost16_to_cpu(struct vhost_virtqueue *vq, __virtio16 val)
{
263
	return __virtio16_to_cpu(vhost_is_little_endian(vq), val);
264 265 266 267
}

static inline __virtio16 cpu_to_vhost16(struct vhost_virtqueue *vq, u16 val)
{
268
	return __cpu_to_virtio16(vhost_is_little_endian(vq), val);
269 270 271 272
}

static inline u32 vhost32_to_cpu(struct vhost_virtqueue *vq, __virtio32 val)
{
273
	return __virtio32_to_cpu(vhost_is_little_endian(vq), val);
274 275 276 277
}

static inline __virtio32 cpu_to_vhost32(struct vhost_virtqueue *vq, u32 val)
{
278
	return __cpu_to_virtio32(vhost_is_little_endian(vq), val);
279 280 281 282
}

static inline u64 vhost64_to_cpu(struct vhost_virtqueue *vq, __virtio64 val)
{
283
	return __virtio64_to_cpu(vhost_is_little_endian(vq), val);
284 285 286 287
}

static inline __virtio64 cpu_to_vhost64(struct vhost_virtqueue *vq, u64 val)
{
288
	return __cpu_to_virtio64(vhost_is_little_endian(vq), val);
289
}
290
#endif