| .. | .. |
|---|
| 12 | 12 | #include <linux/virtio_config.h> |
|---|
| 13 | 13 | #include <linux/virtio_ring.h> |
|---|
| 14 | 14 | #include <linux/atomic.h> |
|---|
| 15 | +#include <linux/vhost_iotlb.h> |
|---|
| 16 | +#include <linux/irqbypass.h> |
|---|
| 15 | 17 | |
|---|
| 16 | 18 | struct vhost_work; |
|---|
| 17 | 19 | typedef void (*vhost_work_fn_t)(struct vhost_work *work); |
|---|
| .. | .. |
|---|
| 52 | 54 | u64 len; |
|---|
| 53 | 55 | }; |
|---|
| 54 | 56 | |
|---|
| 55 | | -#define START(node) ((node)->start) |
|---|
| 56 | | -#define LAST(node) ((node)->last) |
|---|
| 57 | | - |
|---|
| 58 | | -struct vhost_umem_node { |
|---|
| 59 | | - struct rb_node rb; |
|---|
| 60 | | - struct list_head link; |
|---|
| 61 | | - __u64 start; |
|---|
| 62 | | - __u64 last; |
|---|
| 63 | | - __u64 size; |
|---|
| 64 | | - __u64 userspace_addr; |
|---|
| 65 | | - __u32 perm; |
|---|
| 66 | | - __u32 flags_padding; |
|---|
| 67 | | - __u64 __subtree_last; |
|---|
| 68 | | -}; |
|---|
| 69 | | - |
|---|
| 70 | | -struct vhost_umem { |
|---|
| 71 | | - struct rb_root_cached umem_tree; |
|---|
| 72 | | - struct list_head umem_list; |
|---|
| 73 | | - int numem; |
|---|
| 74 | | -}; |
|---|
| 75 | | - |
|---|
| 76 | 57 | enum vhost_uaddr_type { |
|---|
| 77 | 58 | VHOST_ADDR_DESC = 0, |
|---|
| 78 | 59 | VHOST_ADDR_AVAIL = 1, |
|---|
| 79 | 60 | VHOST_ADDR_USED = 2, |
|---|
| 80 | 61 | VHOST_NUM_ADDRS = 3, |
|---|
| 62 | +}; |
|---|
| 63 | + |
|---|
| 64 | +struct vhost_vring_call { |
|---|
| 65 | + struct eventfd_ctx *ctx; |
|---|
| 66 | + struct irq_bypass_producer producer; |
|---|
| 81 | 67 | }; |
|---|
| 82 | 68 | |
|---|
| 83 | 69 | /* The virtqueue structure describes a queue attached to a device. */ |
|---|
| .. | .. |
|---|
| 87 | 73 | /* The actual ring of buffers. */ |
|---|
| 88 | 74 | struct mutex mutex; |
|---|
| 89 | 75 | unsigned int num; |
|---|
| 90 | | - struct vring_desc __user *desc; |
|---|
| 91 | | - struct vring_avail __user *avail; |
|---|
| 92 | | - struct vring_used __user *used; |
|---|
| 93 | | - const struct vhost_umem_node *meta_iotlb[VHOST_NUM_ADDRS]; |
|---|
| 76 | + vring_desc_t __user *desc; |
|---|
| 77 | + vring_avail_t __user *avail; |
|---|
| 78 | + vring_used_t __user *used; |
|---|
| 79 | + const struct vhost_iotlb_map *meta_iotlb[VHOST_NUM_ADDRS]; |
|---|
| 94 | 80 | struct file *kick; |
|---|
| 95 | | - struct eventfd_ctx *call_ctx; |
|---|
| 81 | + struct vhost_vring_call call_ctx; |
|---|
| 96 | 82 | struct eventfd_ctx *error_ctx; |
|---|
| 97 | 83 | struct eventfd_ctx *log_ctx; |
|---|
| 98 | 84 | |
|---|
| .. | .. |
|---|
| 101 | 87 | /* The routine to call when the Guest pings us, or timeout. */ |
|---|
| 102 | 88 | vhost_work_fn_t handle_kick; |
|---|
| 103 | 89 | |
|---|
| 104 | | - /* Last available index we saw. */ |
|---|
| 90 | + /* Last available index we saw. |
|---|
| 91 | + * Values are limited to 0x7fff, and the high bit is used as |
|---|
| 92 | + * a wrap counter when using VIRTIO_F_RING_PACKED. */ |
|---|
| 105 | 93 | u16 last_avail_idx; |
|---|
| 106 | 94 | |
|---|
| 107 | 95 | /* Caches available index value from user. */ |
|---|
| 108 | 96 | u16 avail_idx; |
|---|
| 109 | 97 | |
|---|
| 110 | | - /* Last index we used. */ |
|---|
| 98 | + /* Last index we used. |
|---|
| 99 | + * Values are limited to 0x7fff, and the high bit is used as |
|---|
| 100 | + * a wrap counter when using VIRTIO_F_RING_PACKED. */ |
|---|
| 111 | 101 | u16 last_used_idx; |
|---|
| 112 | 102 | |
|---|
| 113 | 103 | /* Used flags */ |
|---|
| .. | .. |
|---|
| 128 | 118 | struct iovec *indirect; |
|---|
| 129 | 119 | struct vring_used_elem *heads; |
|---|
| 130 | 120 | /* Protected by virtqueue mutex. */ |
|---|
| 131 | | - struct vhost_umem *umem; |
|---|
| 132 | | - struct vhost_umem *iotlb; |
|---|
| 121 | + struct vhost_iotlb *umem; |
|---|
| 122 | + struct vhost_iotlb *iotlb; |
|---|
| 133 | 123 | void *private_data; |
|---|
| 134 | 124 | u64 acked_features; |
|---|
| 135 | 125 | u64 acked_backend_features; |
|---|
| 136 | 126 | /* Log write descriptors */ |
|---|
| 137 | 127 | void __user *log_base; |
|---|
| 138 | 128 | struct vhost_log *log; |
|---|
| 129 | + struct iovec log_iov[64]; |
|---|
| 139 | 130 | |
|---|
| 140 | 131 | /* Ring endianness. Defaults to legacy native endianness. |
|---|
| 141 | 132 | * Set to true when starting a modern virtio device. */ |
|---|
| .. | .. |
|---|
| 164 | 155 | struct eventfd_ctx *log_ctx; |
|---|
| 165 | 156 | struct llist_head work_list; |
|---|
| 166 | 157 | struct task_struct *worker; |
|---|
| 167 | | - struct vhost_umem *umem; |
|---|
| 168 | | - struct vhost_umem *iotlb; |
|---|
| 158 | + struct vhost_iotlb *umem; |
|---|
| 159 | + struct vhost_iotlb *iotlb; |
|---|
| 169 | 160 | spinlock_t iotlb_lock; |
|---|
| 170 | 161 | struct list_head read_list; |
|---|
| 171 | 162 | struct list_head pending_list; |
|---|
| .. | .. |
|---|
| 174 | 165 | int weight; |
|---|
| 175 | 166 | int byte_weight; |
|---|
| 176 | 167 | u64 kcov_handle; |
|---|
| 168 | + bool use_worker; |
|---|
| 169 | + int (*msg_handler)(struct vhost_dev *dev, |
|---|
| 170 | + struct vhost_iotlb_msg *msg); |
|---|
| 177 | 171 | }; |
|---|
| 178 | 172 | |
|---|
| 179 | 173 | bool vhost_exceeds_weight(struct vhost_virtqueue *vq, int pkts, int total_len); |
|---|
| 180 | 174 | void vhost_dev_init(struct vhost_dev *, struct vhost_virtqueue **vqs, |
|---|
| 181 | | - int nvqs, int iov_limit, int weight, int byte_weight); |
|---|
| 175 | + int nvqs, int iov_limit, int weight, int byte_weight, |
|---|
| 176 | + bool use_worker, |
|---|
| 177 | + int (*msg_handler)(struct vhost_dev *dev, |
|---|
| 178 | + struct vhost_iotlb_msg *msg)); |
|---|
| 182 | 179 | long vhost_dev_set_owner(struct vhost_dev *dev); |
|---|
| 183 | 180 | bool vhost_dev_has_owner(struct vhost_dev *dev); |
|---|
| 184 | 181 | long vhost_dev_check_owner(struct vhost_dev *); |
|---|
| 185 | | -struct vhost_umem *vhost_dev_reset_owner_prepare(void); |
|---|
| 186 | | -void vhost_dev_reset_owner(struct vhost_dev *, struct vhost_umem *); |
|---|
| 182 | +struct vhost_iotlb *vhost_dev_reset_owner_prepare(void); |
|---|
| 183 | +void vhost_dev_reset_owner(struct vhost_dev *dev, struct vhost_iotlb *iotlb); |
|---|
| 187 | 184 | void vhost_dev_cleanup(struct vhost_dev *); |
|---|
| 188 | 185 | void vhost_dev_stop(struct vhost_dev *); |
|---|
| 189 | 186 | long vhost_dev_ioctl(struct vhost_dev *, unsigned int ioctl, void __user *argp); |
|---|
| 190 | 187 | long vhost_vring_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp); |
|---|
| 191 | 188 | bool vhost_vq_access_ok(struct vhost_virtqueue *vq); |
|---|
| 192 | 189 | bool vhost_log_access_ok(struct vhost_dev *); |
|---|
| 190 | +void vhost_clear_msg(struct vhost_dev *dev); |
|---|
| 193 | 191 | |
|---|
| 194 | 192 | int vhost_get_vq_desc(struct vhost_virtqueue *, |
|---|
| 195 | 193 | struct iovec iov[], unsigned int iov_count, |
|---|
| .. | .. |
|---|
| 197 | 195 | struct vhost_log *log, unsigned int *log_num); |
|---|
| 198 | 196 | void vhost_discard_vq_desc(struct vhost_virtqueue *, int n); |
|---|
| 199 | 197 | |
|---|
| 198 | +bool vhost_vq_is_setup(struct vhost_virtqueue *vq); |
|---|
| 200 | 199 | int vhost_vq_init_access(struct vhost_virtqueue *); |
|---|
| 201 | 200 | int vhost_add_used(struct vhost_virtqueue *, unsigned int head, int len); |
|---|
| 202 | 201 | int vhost_add_used_n(struct vhost_virtqueue *, struct vring_used_elem *heads, |
|---|
| .. | .. |
|---|
| 213 | 212 | int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log, |
|---|
| 214 | 213 | unsigned int log_num, u64 len, |
|---|
| 215 | 214 | struct iovec *iov, int count); |
|---|
| 216 | | -int vq_iotlb_prefetch(struct vhost_virtqueue *vq); |
|---|
| 215 | +int vq_meta_prefetch(struct vhost_virtqueue *vq); |
|---|
| 217 | 216 | |
|---|
| 218 | 217 | struct vhost_msg_node *vhost_new_msg(struct vhost_virtqueue *vq, int type); |
|---|
| 219 | 218 | void vhost_enqueue_msg(struct vhost_dev *dev, |
|---|
| .. | .. |
|---|
| 221 | 220 | struct vhost_msg_node *node); |
|---|
| 222 | 221 | struct vhost_msg_node *vhost_dequeue_msg(struct vhost_dev *dev, |
|---|
| 223 | 222 | struct list_head *head); |
|---|
| 223 | +void vhost_set_backend_features(struct vhost_dev *dev, u64 features); |
|---|
| 224 | + |
|---|
| 224 | 225 | __poll_t vhost_chr_poll(struct file *file, struct vhost_dev *dev, |
|---|
| 225 | 226 | poll_table *wait); |
|---|
| 226 | 227 | ssize_t vhost_chr_read_iter(struct vhost_dev *dev, struct iov_iter *to, |
|---|
| .. | .. |
|---|
| 228 | 229 | ssize_t vhost_chr_write_iter(struct vhost_dev *dev, |
|---|
| 229 | 230 | struct iov_iter *from); |
|---|
| 230 | 231 | int vhost_init_device_iotlb(struct vhost_dev *d, bool enabled); |
|---|
| 232 | + |
|---|
| 233 | +void vhost_iotlb_map_free(struct vhost_iotlb *iotlb, |
|---|
| 234 | + struct vhost_iotlb_map *map); |
|---|
| 231 | 235 | |
|---|
| 232 | 236 | #define vq_err(vq, fmt, ...) do { \ |
|---|
| 233 | 237 | pr_debug(pr_fmt(fmt), ##__VA_ARGS__); \ |
|---|
| .. | .. |
|---|
| 244 | 248 | (1ULL << VIRTIO_F_VERSION_1) |
|---|
| 245 | 249 | }; |
|---|
| 246 | 250 | |
|---|
| 251 | +/** |
|---|
| 252 | + * vhost_vq_set_backend - Set backend. |
|---|
| 253 | + * |
|---|
| 254 | + * @vq Virtqueue. |
|---|
| 255 | + * @private_data The private data. |
|---|
| 256 | + * |
|---|
| 257 | + * Context: Need to call with vq->mutex acquired. |
|---|
| 258 | + */ |
|---|
| 259 | +static inline void vhost_vq_set_backend(struct vhost_virtqueue *vq, |
|---|
| 260 | + void *private_data) |
|---|
| 261 | +{ |
|---|
| 262 | + vq->private_data = private_data; |
|---|
| 263 | +} |
|---|
| 264 | + |
|---|
| 265 | +/** |
|---|
| 266 | + * vhost_vq_get_backend - Get backend. |
|---|
| 267 | + * |
|---|
| 268 | + * @vq Virtqueue. |
|---|
| 269 | + * |
|---|
| 270 | + * Context: Need to call with vq->mutex acquired. |
|---|
| 271 | + * Return: Private data previously set with vhost_vq_set_backend. |
|---|
| 272 | + */ |
|---|
| 273 | +static inline void *vhost_vq_get_backend(struct vhost_virtqueue *vq) |
|---|
| 274 | +{ |
|---|
| 275 | + return vq->private_data; |
|---|
| 276 | +} |
|---|
| 277 | + |
|---|
| 247 | 278 | static inline bool vhost_has_feature(struct vhost_virtqueue *vq, int bit) |
|---|
| 248 | 279 | { |
|---|
| 249 | 280 | return vq->acked_features & (1ULL << bit); |
|---|