1 /* SPDX-License-Identifier: GPL-2.0 */
5 #include <linux/eventfd.h>
6 #include <linux/vhost.h>
8 #include <linux/mutex.h>
9 #include <linux/poll.h>
10 #include <linux/file.h>
11 #include <linux/uio.h>
12 #include <linux/virtio_config.h>
13 #include <linux/virtio_ring.h>
14 #include <linux/atomic.h>
15 #include <linux/pagemap.h>
16 #include <linux/mmu_notifier.h>
17 #include <asm/cacheflush.h>
20 typedef void (*vhost_work_fn_t
)(struct vhost_work
*work
);
22 #define VHOST_WORK_QUEUED 1
24 struct llist_node node
;
29 /* Poll a file (eventfd or socket) */
30 /* Note: there's nothing vhost specific about this structure. */
33 wait_queue_head_t
*wqh
;
34 wait_queue_entry_t wait
;
35 struct vhost_work work
;
37 struct vhost_dev
*dev
;
40 void vhost_work_init(struct vhost_work
*work
, vhost_work_fn_t fn
);
41 void vhost_work_queue(struct vhost_dev
*dev
, struct vhost_work
*work
);
42 bool vhost_has_work(struct vhost_dev
*dev
);
44 void vhost_poll_init(struct vhost_poll
*poll
, vhost_work_fn_t fn
,
45 __poll_t mask
, struct vhost_dev
*dev
);
46 int vhost_poll_start(struct vhost_poll
*poll
, struct file
*file
);
47 void vhost_poll_stop(struct vhost_poll
*poll
);
48 void vhost_poll_flush(struct vhost_poll
*poll
);
49 void vhost_poll_queue(struct vhost_poll
*poll
);
50 void vhost_work_flush(struct vhost_dev
*dev
, struct vhost_work
*work
);
51 long vhost_vring_ioctl(struct vhost_dev
*d
, unsigned int ioctl
, void __user
*argp
);
58 #define START(node) ((node)->start)
59 #define LAST(node) ((node)->last)
61 struct vhost_umem_node
{
63 struct list_head link
;
74 struct rb_root_cached umem_tree
;
75 struct list_head umem_list
;
79 enum vhost_uaddr_type
{
98 #if defined(CONFIG_MMU_NOTIFIER) && ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE == 0
99 #define VHOST_ARCH_CAN_ACCEL_UACCESS 0
101 #define VHOST_ARCH_CAN_ACCEL_UACCESS 0
104 /* The virtqueue structure describes a queue attached to a device. */
105 struct vhost_virtqueue
{
106 struct vhost_dev
*dev
;
108 /* The actual ring of buffers. */
111 struct vring_desc __user
*desc
;
112 struct vring_avail __user
*avail
;
113 struct vring_used __user
*used
;
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
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.
126 struct vhost_uaddr uaddrs
[VHOST_NUM_ADDRS
];
128 const struct vhost_umem_node
*meta_iotlb
[VHOST_NUM_ADDRS
];
131 struct eventfd_ctx
*call_ctx
;
132 struct eventfd_ctx
*error_ctx
;
133 struct eventfd_ctx
*log_ctx
;
135 struct vhost_poll poll
;
137 /* The routine to call when the Guest pings us, or timeout. */
138 vhost_work_fn_t handle_kick
;
140 /* Last available index we saw. */
143 /* Caches available index value from user. */
146 /* Last index we used. */
152 /* Last used index value we have signalled on */
155 /* Last used index value we have signalled on */
156 bool signalled_used_valid
;
158 /* Log writes to used structure. */
162 struct iovec iov
[UIO_MAXIOV
];
163 struct iovec iotlb_iov
[64];
164 struct iovec
*indirect
;
165 struct vring_used_elem
*heads
;
166 /* Protected by virtqueue mutex. */
167 struct vhost_umem
*umem
;
168 struct vhost_umem
*iotlb
;
171 u64 acked_backend_features
;
172 /* Log write descriptors */
173 void __user
*log_base
;
174 struct vhost_log
*log
;
176 /* Ring endianness. Defaults to legacy native endianness.
177 * Set to true when starting a modern virtio device. */
179 #ifdef CONFIG_VHOST_CROSS_ENDIAN_LEGACY
180 /* Ring endianness requested by userspace for cross-endian support. */
183 u32 busyloop_timeout
;
185 int invalidate_count
;
188 struct vhost_msg_node
{
190 struct vhost_msg msg
;
191 struct vhost_msg_v2 msg_v2
;
193 struct vhost_virtqueue
*vq
;
194 struct list_head node
;
198 struct mm_struct
*mm
;
199 #ifdef CONFIG_MMU_NOTIFIER
200 struct mmu_notifier mmu_notifier
;
203 struct vhost_virtqueue
**vqs
;
205 struct eventfd_ctx
*log_ctx
;
206 struct llist_head work_list
;
207 struct task_struct
*worker
;
208 struct vhost_umem
*umem
;
209 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
;
219 bool vhost_exceeds_weight(struct vhost_virtqueue
*vq
, int pkts
, int total_len
);
220 void vhost_dev_init(struct vhost_dev
*, struct vhost_virtqueue
**vqs
,
221 int nvqs
, int iov_limit
, int weight
, int byte_weight
);
222 long vhost_dev_set_owner(struct vhost_dev
*dev
);
223 bool vhost_dev_has_owner(struct vhost_dev
*dev
);
224 long vhost_dev_check_owner(struct vhost_dev
*);
225 struct vhost_umem
*vhost_dev_reset_owner_prepare(void);
226 void vhost_dev_reset_owner(struct vhost_dev
*, struct vhost_umem
*);
227 void vhost_dev_cleanup(struct vhost_dev
*);
228 void vhost_dev_stop(struct vhost_dev
*);
229 long vhost_dev_ioctl(struct vhost_dev
*, unsigned int ioctl
, void __user
*argp
);
230 long vhost_vring_ioctl(struct vhost_dev
*d
, unsigned int ioctl
, void __user
*argp
);
231 bool vhost_vq_access_ok(struct vhost_virtqueue
*vq
);
232 bool vhost_log_access_ok(struct vhost_dev
*);
234 int vhost_get_vq_desc(struct vhost_virtqueue
*,
235 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
);
238 void vhost_discard_vq_desc(struct vhost_virtqueue
*, int n
);
240 int vhost_vq_init_access(struct vhost_virtqueue
*);
241 int vhost_add_used(struct vhost_virtqueue
*, unsigned int head
, int len
);
242 int vhost_add_used_n(struct vhost_virtqueue
*, struct vring_used_elem
*heads
,
244 void vhost_add_used_and_signal(struct vhost_dev
*, struct vhost_virtqueue
*,
245 unsigned int id
, int len
);
246 void vhost_add_used_and_signal_n(struct vhost_dev
*, struct vhost_virtqueue
*,
247 struct vring_used_elem
*heads
, unsigned count
);
248 void vhost_signal(struct vhost_dev
*, struct vhost_virtqueue
*);
249 void vhost_disable_notify(struct vhost_dev
*, struct vhost_virtqueue
*);
250 bool vhost_vq_avail_empty(struct vhost_dev
*, struct vhost_virtqueue
*);
251 bool vhost_enable_notify(struct vhost_dev
*, struct vhost_virtqueue
*);
253 int vhost_log_write(struct vhost_virtqueue
*vq
, struct vhost_log
*log
,
254 unsigned int log_num
, u64 len
,
255 struct iovec
*iov
, int count
);
256 int vq_meta_prefetch(struct vhost_virtqueue
*vq
);
258 struct vhost_msg_node
*vhost_new_msg(struct vhost_virtqueue
*vq
, int type
);
259 void vhost_enqueue_msg(struct vhost_dev
*dev
,
260 struct list_head
*head
,
261 struct vhost_msg_node
*node
);
262 struct vhost_msg_node
*vhost_dequeue_msg(struct vhost_dev
*dev
,
263 struct list_head
*head
);
264 __poll_t
vhost_chr_poll(struct file
*file
, struct vhost_dev
*dev
,
266 ssize_t
vhost_chr_read_iter(struct vhost_dev
*dev
, struct iov_iter
*to
,
268 ssize_t
vhost_chr_write_iter(struct vhost_dev
*dev
,
269 struct iov_iter
*from
);
270 int vhost_init_device_iotlb(struct vhost_dev
*d
, bool enabled
);
272 #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);\
279 VHOST_FEATURES
= (1ULL << VIRTIO_F_NOTIFY_ON_EMPTY
) |
280 (1ULL << VIRTIO_RING_F_INDIRECT_DESC
) |
281 (1ULL << VIRTIO_RING_F_EVENT_IDX
) |
282 (1ULL << VHOST_F_LOG_ALL
) |
283 (1ULL << VIRTIO_F_ANY_LAYOUT
) |
284 (1ULL << VIRTIO_F_VERSION_1
)
287 static inline bool vhost_has_feature(struct vhost_virtqueue
*vq
, int bit
)
289 return vq
->acked_features
& (1ULL << bit
);
292 static inline bool vhost_backend_has_feature(struct vhost_virtqueue
*vq
, int bit
)
294 return vq
->acked_backend_features
& (1ULL << bit
);
297 #ifdef CONFIG_VHOST_CROSS_ENDIAN_LEGACY
298 static inline bool vhost_is_little_endian(struct vhost_virtqueue
*vq
)
303 static inline bool vhost_is_little_endian(struct vhost_virtqueue
*vq
)
305 return virtio_legacy_is_little_endian() || vq
->is_le
;
309 /* Memory accessors */
310 static inline u16
vhost16_to_cpu(struct vhost_virtqueue
*vq
, __virtio16 val
)
312 return __virtio16_to_cpu(vhost_is_little_endian(vq
), val
);
315 static inline __virtio16
cpu_to_vhost16(struct vhost_virtqueue
*vq
, u16 val
)
317 return __cpu_to_virtio16(vhost_is_little_endian(vq
), val
);
320 static inline u32
vhost32_to_cpu(struct vhost_virtqueue
*vq
, __virtio32 val
)
322 return __virtio32_to_cpu(vhost_is_little_endian(vq
), val
);
325 static inline __virtio32
cpu_to_vhost32(struct vhost_virtqueue
*vq
, u32 val
)
327 return __cpu_to_virtio32(vhost_is_little_endian(vq
), val
);
330 static inline u64
vhost64_to_cpu(struct vhost_virtqueue
*vq
, __virtio64 val
)
332 return __virtio64_to_cpu(vhost_is_little_endian(vq
), val
);
335 static inline __virtio64
cpu_to_vhost64(struct vhost_virtqueue
*vq
, u64 val
)
337 return __cpu_to_virtio64(vhost_is_little_endian(vq
), val
);