vhost.h 9.5 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>
J
Jason Wang 已提交
15
#include <linux/vhost_iotlb.h>
Z
Zhu Lingshan 已提交
16
#include <linux/irqbypass.h>
17

18 19 20
struct vhost_work;
typedef void (*vhost_work_fn_t)(struct vhost_work *work);

J
Jason Wang 已提交
21
#define VHOST_WORK_QUEUED 1
22
struct vhost_work {
J
Jason Wang 已提交
23
	struct llist_node	  node;
24
	vhost_work_fn_t		  fn;
J
Jason Wang 已提交
25
	unsigned long		  flags;
26 27
};

28 29 30 31 32
/* 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;
33
	wait_queue_entry_t              wait;
34
	struct vhost_work	  work;
A
Al Viro 已提交
35
	__poll_t		  mask;
36
	struct vhost_dev	 *dev;
37 38
};

39 40
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 已提交
41
bool vhost_has_work(struct vhost_dev *dev);
42

43
void vhost_poll_init(struct vhost_poll *poll, vhost_work_fn_t fn,
A
Al Viro 已提交
44
		     __poll_t mask, struct vhost_dev *dev);
45
int vhost_poll_start(struct vhost_poll *poll, struct file *file);
46 47 48
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 已提交
49
void vhost_work_flush(struct vhost_dev *dev, struct vhost_work *work);
50
long vhost_vring_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp);
51 52 53 54 55 56

struct vhost_log {
	u64 addr;
	u64 len;
};

57 58 59 60 61 62 63
enum vhost_uaddr_type {
	VHOST_ADDR_DESC = 0,
	VHOST_ADDR_AVAIL = 1,
	VHOST_ADDR_USED = 2,
	VHOST_NUM_ADDRS = 3,
};

Z
Zhu Lingshan 已提交
64 65 66 67 68
struct vhost_vring_call {
	struct eventfd_ctx *ctx;
	struct irq_bypass_producer producer;
};

69 70 71 72 73 74 75
/* 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;
76 77 78
	vring_desc_t __user *desc;
	vring_avail_t __user *avail;
	vring_used_t __user *used;
J
Jason Wang 已提交
79
	const struct vhost_iotlb_map *meta_iotlb[VHOST_NUM_ADDRS];
80
	struct file *kick;
Z
Zhu Lingshan 已提交
81
	struct vhost_vring_call call_ctx;
82 83 84 85 86 87
	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. */
88
	vhost_work_fn_t handle_kick;
89 90 91 92 93 94 95 96 97 98 99 100 101

	/* 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 已提交
102 103 104 105 106 107
	/* Last used index value we have signalled on */
	u16 signalled_used;

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

108 109 110 111
	/* Log writes to used structure. */
	bool log_used;
	u64 log_addr;

J
Jason Wang 已提交
112
	struct iovec iov[UIO_MAXIOV];
J
Jason Wang 已提交
113
	struct iovec iotlb_iov[64];
J
Jason Wang 已提交
114 115
	struct iovec *indirect;
	struct vring_used_elem *heads;
A
Asias He 已提交
116
	/* Protected by virtqueue mutex. */
J
Jason Wang 已提交
117 118
	struct vhost_iotlb *umem;
	struct vhost_iotlb *iotlb;
A
Asias He 已提交
119
	void *private_data;
M
Michael S. Tsirkin 已提交
120
	u64 acked_features;
121
	u64 acked_backend_features;
122 123
	/* Log write descriptors */
	void __user *log_base;
J
Jason Wang 已提交
124
	struct vhost_log *log;
L
Li Wang 已提交
125
	struct iovec log_iov[64];
126 127 128 129 130 131 132 133

	/* 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 已提交
134
	u32 busyloop_timeout;
135 136
};

J
Jason Wang 已提交
137
struct vhost_msg_node {
138 139 140 141
  union {
	  struct vhost_msg msg;
	  struct vhost_msg_v2 msg_v2;
  };
J
Jason Wang 已提交
142 143 144 145
  struct vhost_virtqueue *vq;
  struct list_head node;
};

146 147 148
struct vhost_dev {
	struct mm_struct *mm;
	struct mutex mutex;
149
	struct vhost_virtqueue **vqs;
150 151
	int nvqs;
	struct eventfd_ctx *log_ctx;
J
Jason Wang 已提交
152
	struct llist_head work_list;
153
	struct task_struct *worker;
J
Jason Wang 已提交
154 155
	struct vhost_iotlb *umem;
	struct vhost_iotlb *iotlb;
J
Jason Wang 已提交
156 157 158 159
	spinlock_t iotlb_lock;
	struct list_head read_list;
	struct list_head pending_list;
	wait_queue_head_t wait;
J
Jason Wang 已提交
160
	int iov_limit;
161 162
	int weight;
	int byte_weight;
163
	u64 kcov_handle;
164
	bool use_worker;
165 166
	int (*msg_handler)(struct vhost_dev *dev,
			   struct vhost_iotlb_msg *msg);
167 168
};

169
bool vhost_exceeds_weight(struct vhost_virtqueue *vq, int pkts, int total_len);
J
Jason Wang 已提交
170
void vhost_dev_init(struct vhost_dev *, struct vhost_virtqueue **vqs,
171
		    int nvqs, int iov_limit, int weight, int byte_weight,
172
		    bool use_worker,
173 174
		    int (*msg_handler)(struct vhost_dev *dev,
				       struct vhost_iotlb_msg *msg));
A
Asias He 已提交
175
long vhost_dev_set_owner(struct vhost_dev *dev);
176
bool vhost_dev_has_owner(struct vhost_dev *dev);
177
long vhost_dev_check_owner(struct vhost_dev *);
J
Jason Wang 已提交
178 179
struct vhost_iotlb *vhost_dev_reset_owner_prepare(void);
void vhost_dev_reset_owner(struct vhost_dev *dev, struct vhost_iotlb *iotlb);
180
void vhost_dev_cleanup(struct vhost_dev *);
181
void vhost_dev_stop(struct vhost_dev *);
182
long vhost_dev_ioctl(struct vhost_dev *, unsigned int ioctl, void __user *argp);
183
long vhost_vring_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp);
184 185
bool vhost_vq_access_ok(struct vhost_virtqueue *vq);
bool vhost_log_access_ok(struct vhost_dev *);
186

187
int vhost_get_vq_desc(struct vhost_virtqueue *,
188 189 190
		      struct iovec iov[], unsigned int iov_count,
		      unsigned int *out_num, unsigned int *in_num,
		      struct vhost_log *log, unsigned int *log_num);
191
void vhost_discard_vq_desc(struct vhost_virtqueue *, int n);
192

193
bool vhost_vq_is_setup(struct vhost_virtqueue *vq);
G
Greg Kurz 已提交
194
int vhost_vq_init_access(struct vhost_virtqueue *);
195
int vhost_add_used(struct vhost_virtqueue *, unsigned int head, int len);
196 197
int vhost_add_used_n(struct vhost_virtqueue *, struct vring_used_elem *heads,
		     unsigned count);
198
void vhost_add_used_and_signal(struct vhost_dev *, struct vhost_virtqueue *,
199 200 201 202
			       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 已提交
203
void vhost_disable_notify(struct vhost_dev *, struct vhost_virtqueue *);
204
bool vhost_vq_avail_empty(struct vhost_dev *, struct vhost_virtqueue *);
M
Michael S. Tsirkin 已提交
205
bool vhost_enable_notify(struct vhost_dev *, struct vhost_virtqueue *);
206 207

int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log,
J
Jason Wang 已提交
208 209
		    unsigned int log_num, u64 len,
		    struct iovec *iov, int count);
210
int vq_meta_prefetch(struct vhost_virtqueue *vq);
J
Jason Wang 已提交
211 212 213 214 215 216 217

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);
218 219
void vhost_set_backend_features(struct vhost_dev *dev, u64 features);

220
__poll_t vhost_chr_poll(struct file *file, struct vhost_dev *dev,
J
Jason Wang 已提交
221 222 223 224 225 226
			    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

J
Jason Wang 已提交
228 229 230
void vhost_iotlb_map_free(struct vhost_iotlb *iotlb,
			  struct vhost_iotlb_map *map);

231 232 233 234 235 236 237
#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 已提交
238 239 240
	VHOST_FEATURES = (1ULL << VIRTIO_F_NOTIFY_ON_EMPTY) |
			 (1ULL << VIRTIO_RING_F_INDIRECT_DESC) |
			 (1ULL << VIRTIO_RING_F_EVENT_IDX) |
241 242 243
			 (1ULL << VHOST_F_LOG_ALL) |
			 (1ULL << VIRTIO_F_ANY_LAYOUT) |
			 (1ULL << VIRTIO_F_VERSION_1)
244 245
};

246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
/**
 * vhost_vq_set_backend - Set backend.
 *
 * @vq            Virtqueue.
 * @private_data  The private data.
 *
 * Context: Need to call with vq->mutex acquired.
 */
static inline void vhost_vq_set_backend(struct vhost_virtqueue *vq,
					void *private_data)
{
	vq->private_data = private_data;
}

/**
 * vhost_vq_get_backend - Get backend.
 *
 * @vq            Virtqueue.
 *
 * Context: Need to call with vq->mutex acquired.
 * Return: Private data previously set with vhost_vq_set_backend.
 */
static inline void *vhost_vq_get_backend(struct vhost_virtqueue *vq)
{
	return vq->private_data;
}

M
Michael S. Tsirkin 已提交
273
static inline bool vhost_has_feature(struct vhost_virtqueue *vq, int bit)
274
{
M
Michael S. Tsirkin 已提交
275
	return vq->acked_features & (1ULL << bit);
276
}
277

278 279 280 281 282
static inline bool vhost_backend_has_feature(struct vhost_virtqueue *vq, int bit)
{
	return vq->acked_backend_features & (1ULL << bit);
}

283
#ifdef CONFIG_VHOST_CROSS_ENDIAN_LEGACY
284 285
static inline bool vhost_is_little_endian(struct vhost_virtqueue *vq)
{
286
	return vq->is_le;
287
}
288 289 290 291 292 293
#else
static inline bool vhost_is_little_endian(struct vhost_virtqueue *vq)
{
	return virtio_legacy_is_little_endian() || vq->is_le;
}
#endif
294

295 296 297
/* Memory accessors */
static inline u16 vhost16_to_cpu(struct vhost_virtqueue *vq, __virtio16 val)
{
298
	return __virtio16_to_cpu(vhost_is_little_endian(vq), val);
299 300 301 302
}

static inline __virtio16 cpu_to_vhost16(struct vhost_virtqueue *vq, u16 val)
{
303
	return __cpu_to_virtio16(vhost_is_little_endian(vq), val);
304 305 306 307
}

static inline u32 vhost32_to_cpu(struct vhost_virtqueue *vq, __virtio32 val)
{
308
	return __virtio32_to_cpu(vhost_is_little_endian(vq), val);
309 310 311 312
}

static inline __virtio32 cpu_to_vhost32(struct vhost_virtqueue *vq, u32 val)
{
313
	return __cpu_to_virtio32(vhost_is_little_endian(vq), val);
314 315 316 317
}

static inline u64 vhost64_to_cpu(struct vhost_virtqueue *vq, __virtio64 val)
{
318
	return __virtio64_to_cpu(vhost_is_little_endian(vq), val);
319 320 321 322
}

static inline __virtio64 cpu_to_vhost64(struct vhost_virtqueue *vq, u64 val)
{
323
	return __cpu_to_virtio64(vhost_is_little_endian(vq), val);
324
}
325
#endif