| .. | .. |
|---|
| 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 | |
|---|
| .. | .. |
|---|
| 128 | 114 | struct iovec *indirect; |
|---|
| 129 | 115 | struct vring_used_elem *heads; |
|---|
| 130 | 116 | /* Protected by virtqueue mutex. */ |
|---|
| 131 | | - struct vhost_umem *umem; |
|---|
| 132 | | - struct vhost_umem *iotlb; |
|---|
| 117 | + struct vhost_iotlb *umem; |
|---|
| 118 | + struct vhost_iotlb *iotlb; |
|---|
| 133 | 119 | void *private_data; |
|---|
| 134 | 120 | u64 acked_features; |
|---|
| 135 | 121 | u64 acked_backend_features; |
|---|
| 136 | 122 | /* Log write descriptors */ |
|---|
| 137 | 123 | void __user *log_base; |
|---|
| 138 | 124 | struct vhost_log *log; |
|---|
| 125 | + struct iovec log_iov[64]; |
|---|
| 139 | 126 | |
|---|
| 140 | 127 | /* Ring endianness. Defaults to legacy native endianness. |
|---|
| 141 | 128 | * Set to true when starting a modern virtio device. */ |
|---|
| .. | .. |
|---|
| 164 | 151 | struct eventfd_ctx *log_ctx; |
|---|
| 165 | 152 | struct llist_head work_list; |
|---|
| 166 | 153 | struct task_struct *worker; |
|---|
| 167 | | - struct vhost_umem *umem; |
|---|
| 168 | | - struct vhost_umem *iotlb; |
|---|
| 154 | + struct vhost_iotlb *umem; |
|---|
| 155 | + struct vhost_iotlb *iotlb; |
|---|
| 169 | 156 | spinlock_t iotlb_lock; |
|---|
| 170 | 157 | struct list_head read_list; |
|---|
| 171 | 158 | struct list_head pending_list; |
|---|
| .. | .. |
|---|
| 174 | 161 | int weight; |
|---|
| 175 | 162 | int byte_weight; |
|---|
| 176 | 163 | u64 kcov_handle; |
|---|
| 164 | + bool use_worker; |
|---|
| 165 | + int (*msg_handler)(struct vhost_dev *dev, |
|---|
| 166 | + struct vhost_iotlb_msg *msg); |
|---|
| 177 | 167 | }; |
|---|
| 178 | 168 | |
|---|
| 179 | 169 | bool vhost_exceeds_weight(struct vhost_virtqueue *vq, int pkts, int total_len); |
|---|
| 180 | 170 | void vhost_dev_init(struct vhost_dev *, struct vhost_virtqueue **vqs, |
|---|
| 181 | | - int nvqs, int iov_limit, int weight, int byte_weight); |
|---|
| 171 | + int nvqs, int iov_limit, int weight, int byte_weight, |
|---|
| 172 | + bool use_worker, |
|---|
| 173 | + int (*msg_handler)(struct vhost_dev *dev, |
|---|
| 174 | + struct vhost_iotlb_msg *msg)); |
|---|
| 182 | 175 | long vhost_dev_set_owner(struct vhost_dev *dev); |
|---|
| 183 | 176 | bool vhost_dev_has_owner(struct vhost_dev *dev); |
|---|
| 184 | 177 | 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 *); |
|---|
| 178 | +struct vhost_iotlb *vhost_dev_reset_owner_prepare(void); |
|---|
| 179 | +void vhost_dev_reset_owner(struct vhost_dev *dev, struct vhost_iotlb *iotlb); |
|---|
| 187 | 180 | void vhost_dev_cleanup(struct vhost_dev *); |
|---|
| 188 | 181 | void vhost_dev_stop(struct vhost_dev *); |
|---|
| 189 | 182 | long vhost_dev_ioctl(struct vhost_dev *, unsigned int ioctl, void __user *argp); |
|---|
| .. | .. |
|---|
| 197 | 190 | struct vhost_log *log, unsigned int *log_num); |
|---|
| 198 | 191 | void vhost_discard_vq_desc(struct vhost_virtqueue *, int n); |
|---|
| 199 | 192 | |
|---|
| 193 | +bool vhost_vq_is_setup(struct vhost_virtqueue *vq); |
|---|
| 200 | 194 | int vhost_vq_init_access(struct vhost_virtqueue *); |
|---|
| 201 | 195 | int vhost_add_used(struct vhost_virtqueue *, unsigned int head, int len); |
|---|
| 202 | 196 | int vhost_add_used_n(struct vhost_virtqueue *, struct vring_used_elem *heads, |
|---|
| .. | .. |
|---|
| 213 | 207 | int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log, |
|---|
| 214 | 208 | unsigned int log_num, u64 len, |
|---|
| 215 | 209 | struct iovec *iov, int count); |
|---|
| 216 | | -int vq_iotlb_prefetch(struct vhost_virtqueue *vq); |
|---|
| 210 | +int vq_meta_prefetch(struct vhost_virtqueue *vq); |
|---|
| 217 | 211 | |
|---|
| 218 | 212 | struct vhost_msg_node *vhost_new_msg(struct vhost_virtqueue *vq, int type); |
|---|
| 219 | 213 | void vhost_enqueue_msg(struct vhost_dev *dev, |
|---|
| .. | .. |
|---|
| 221 | 215 | struct vhost_msg_node *node); |
|---|
| 222 | 216 | struct vhost_msg_node *vhost_dequeue_msg(struct vhost_dev *dev, |
|---|
| 223 | 217 | struct list_head *head); |
|---|
| 218 | +void vhost_set_backend_features(struct vhost_dev *dev, u64 features); |
|---|
| 219 | + |
|---|
| 224 | 220 | __poll_t vhost_chr_poll(struct file *file, struct vhost_dev *dev, |
|---|
| 225 | 221 | poll_table *wait); |
|---|
| 226 | 222 | ssize_t vhost_chr_read_iter(struct vhost_dev *dev, struct iov_iter *to, |
|---|
| .. | .. |
|---|
| 228 | 224 | ssize_t vhost_chr_write_iter(struct vhost_dev *dev, |
|---|
| 229 | 225 | struct iov_iter *from); |
|---|
| 230 | 226 | int vhost_init_device_iotlb(struct vhost_dev *d, bool enabled); |
|---|
| 227 | + |
|---|
| 228 | +void vhost_iotlb_map_free(struct vhost_iotlb *iotlb, |
|---|
| 229 | + struct vhost_iotlb_map *map); |
|---|
| 231 | 230 | |
|---|
| 232 | 231 | #define vq_err(vq, fmt, ...) do { \ |
|---|
| 233 | 232 | pr_debug(pr_fmt(fmt), ##__VA_ARGS__); \ |
|---|
| .. | .. |
|---|
| 244 | 243 | (1ULL << VIRTIO_F_VERSION_1) |
|---|
| 245 | 244 | }; |
|---|
| 246 | 245 | |
|---|
| 246 | +/** |
|---|
| 247 | + * vhost_vq_set_backend - Set backend. |
|---|
| 248 | + * |
|---|
| 249 | + * @vq Virtqueue. |
|---|
| 250 | + * @private_data The private data. |
|---|
| 251 | + * |
|---|
| 252 | + * Context: Need to call with vq->mutex acquired. |
|---|
| 253 | + */ |
|---|
| 254 | +static inline void vhost_vq_set_backend(struct vhost_virtqueue *vq, |
|---|
| 255 | + void *private_data) |
|---|
| 256 | +{ |
|---|
| 257 | + vq->private_data = private_data; |
|---|
| 258 | +} |
|---|
| 259 | + |
|---|
| 260 | +/** |
|---|
| 261 | + * vhost_vq_get_backend - Get backend. |
|---|
| 262 | + * |
|---|
| 263 | + * @vq Virtqueue. |
|---|
| 264 | + * |
|---|
| 265 | + * Context: Need to call with vq->mutex acquired. |
|---|
| 266 | + * Return: Private data previously set with vhost_vq_set_backend. |
|---|
| 267 | + */ |
|---|
| 268 | +static inline void *vhost_vq_get_backend(struct vhost_virtqueue *vq) |
|---|
| 269 | +{ |
|---|
| 270 | + return vq->private_data; |
|---|
| 271 | +} |
|---|
| 272 | + |
|---|
| 247 | 273 | static inline bool vhost_has_feature(struct vhost_virtqueue *vq, int bit) |
|---|
| 248 | 274 | { |
|---|
| 249 | 275 | return vq->acked_features & (1ULL << bit); |
|---|