Files
laptops-kernel/drivers/vhost/vhost.h
T

370 lines
11 KiB
C
Raw Normal View History

/* SPDX-License-Identifier: GPL-2.0 */
2010-01-14 06:17:27 +00:00
#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>
2011-07-26 16:09:06 -07:00
#include <linux/atomic.h>
2020-03-26 22:01:19 +08:00
#include <linux/vhost_iotlb.h>
2020-07-31 14:55:28 +08:00
#include <linux/irqbypass.h>
2010-01-14 06:17:27 +00:00
struct vhost_work;
2023-03-10 16:03:32 -06:00
struct vhost_task;
typedef void (*vhost_work_fn_t)(struct vhost_work *work);
2016-04-25 22:14:33 -04:00
#define VHOST_WORK_QUEUED 1
struct vhost_work {
2021-05-25 12:47:33 -05:00
struct llist_node node;
vhost_work_fn_t fn;
unsigned long flags;
};
struct vhost_worker;
struct vhost_dev;
struct vhost_worker_ops {
int (*create)(struct vhost_worker *worker, struct vhost_dev *dev,
const char *name);
void (*stop)(struct vhost_worker *worker);
void (*wakeup)(struct vhost_worker *worker);
};
struct vhost_worker {
struct task_struct *kthread_task;
2023-03-10 16:03:32 -06:00
struct vhost_task *vtsk;
struct vhost_dev *dev;
/* Used to serialize device wide flushing with worker swapping. */
struct mutex mutex;
struct llist_head work_list;
u64 kcov_handle;
u32 id;
2023-06-26 18:23:05 -05:00
int attachment_cnt;
bool killed;
const struct vhost_worker_ops *ops;
};
2010-01-14 06:17:27 +00:00
/* Poll a file (eventfd or socket) */
/* Note: there's nothing vhost specific about this structure. */
struct vhost_poll {
2021-05-25 12:47:32 -05:00
poll_table table;
wait_queue_head_t *wqh;
wait_queue_entry_t wait;
struct vhost_work work;
__poll_t mask;
struct vhost_dev *dev;
2023-06-26 18:22:57 -05:00
struct vhost_virtqueue *vq;
2010-01-14 06:17:27 +00:00
};
void vhost_poll_init(struct vhost_poll *poll, vhost_work_fn_t fn,
2023-06-26 18:22:57 -05:00
__poll_t mask, struct vhost_dev *dev,
struct vhost_virtqueue *vq);
int vhost_poll_start(struct vhost_poll *poll, struct file *file);
2010-01-14 06:17:27 +00:00
void vhost_poll_stop(struct vhost_poll *poll);
void vhost_poll_queue(struct vhost_poll *poll);
2023-06-26 18:23:02 -05:00
void vhost_work_init(struct vhost_work *work, vhost_work_fn_t fn);
2022-05-17 13:08:50 -05:00
void vhost_dev_flush(struct vhost_dev *dev);
2010-01-14 06:17:27 +00:00
struct vhost_log {
u64 addr;
u64 len;
};
2017-02-28 17:56:02 +08:00
enum vhost_uaddr_type {
VHOST_ADDR_DESC = 0,
VHOST_ADDR_AVAIL = 1,
VHOST_ADDR_USED = 2,
VHOST_NUM_ADDRS = 3,
};
2020-07-31 14:55:28 +08:00
struct vhost_vring_call {
struct eventfd_ctx *ctx;
struct irq_bypass_producer producer;
};
2010-01-14 06:17:27 +00:00
/* The virtqueue structure describes a queue attached to a device. */
struct vhost_virtqueue {
struct vhost_dev *dev;
struct vhost_worker __rcu *worker;
2010-01-14 06:17:27 +00:00
/* The actual ring of buffers. */
struct mutex mutex;
unsigned int num;
vring_desc_t __user *desc;
vring_avail_t __user *avail;
vring_used_t __user *used;
2020-03-26 22:01:19 +08:00
const struct vhost_iotlb_map *meta_iotlb[VHOST_NUM_ADDRS];
2010-01-14 06:17:27 +00:00
struct file *kick;
2020-07-31 14:55:28 +08:00
struct vhost_vring_call call_ctx;
2010-01-14 06:17:27 +00:00
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. */
vhost_work_fn_t handle_kick;
2010-01-14 06:17:27 +00:00
/* Last available index we saw.
* Values are limited to 0x7fff, and the high bit is used as
* a wrap counter when using VIRTIO_F_RING_PACKED. */
2010-01-14 06:17:27 +00:00
u16 last_avail_idx;
2025-07-14 16:47:54 +08:00
/* Next avail ring head when VIRTIO_F_IN_ORDER is negoitated */
u16 next_avail_head;
2010-01-14 06:17:27 +00:00
/* Caches available index value from user. */
u16 avail_idx;
/* Last index we used.
* Values are limited to 0x7fff, and the high bit is used as
* a wrap counter when using VIRTIO_F_RING_PACKED. */
2010-01-14 06:17:27 +00:00
u16 last_used_idx;
/* Used flags */
u16 used_flags;
2011-05-20 02:10:54 +03:00
/* Last used index value we have signalled on */
u16 signalled_used;
/* Last used index value we have signalled on */
bool signalled_used_valid;
2010-01-14 06:17:27 +00:00
/* Log writes to used structure. */
bool log_used;
u64 log_addr;
2010-09-14 23:53:05 +08:00
struct iovec iov[UIO_MAXIOV];
2016-06-23 02:04:32 -04:00
struct iovec iotlb_iov[64];
2010-09-14 23:53:05 +08:00
struct iovec *indirect;
struct vring_used_elem *heads;
2025-07-14 16:47:54 +08:00
u16 *nheads;
2013-05-07 14:54:36 +08:00
/* Protected by virtqueue mutex. */
2020-03-26 22:01:19 +08:00
struct vhost_iotlb *umem;
struct vhost_iotlb *iotlb;
2013-05-07 14:54:36 +08:00
void *private_data;
VIRTIO_DECLARE_FEATURES(acked_features);
2018-08-06 11:17:47 +08:00
u64 acked_backend_features;
2010-01-14 06:17:27 +00:00
/* Log write descriptors */
void __user *log_base;
2010-09-14 23:53:05 +08:00
struct vhost_log *log;
2020-09-15 02:08:09 +08:00
struct iovec log_iov[64];
/* 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
2016-03-04 06:24:53 -05:00
u32 busyloop_timeout;
2010-01-14 06:17:27 +00:00
};
2016-06-23 02:04:32 -04:00
struct vhost_msg_node {
2018-08-06 11:17:47 +08:00
union {
struct vhost_msg msg;
struct vhost_msg_v2 msg_v2;
};
2016-06-23 02:04:32 -04:00
struct vhost_virtqueue *vq;
struct list_head node;
};
2010-01-14 06:17:27 +00:00
struct vhost_dev {
struct mm_struct *mm;
struct mutex mutex;
2013-04-27 11:16:48 +08:00
struct vhost_virtqueue **vqs;
2010-01-14 06:17:27 +00:00
int nvqs;
struct eventfd_ctx *log_ctx;
2020-03-26 22:01:19 +08:00
struct vhost_iotlb *umem;
struct vhost_iotlb *iotlb;
2016-06-23 02:04:32 -04:00
spinlock_t iotlb_lock;
struct list_head read_list;
struct list_head pending_list;
wait_queue_head_t wait;
2019-01-28 15:05:05 +08:00
int iov_limit;
2019-05-17 00:29:49 -04:00
int weight;
int byte_weight;
struct xarray worker_xa;
bool use_worker;
/*
* If fork_owner is true we use vhost_tasks to create
* the worker so all settings/limits like cgroups, NPROC,
* scheduler, etc are inherited from the owner. If false,
* we use kthreads and only attach to the same cgroups
* as the owner for compat with older kernels.
* here we use true as default value.
* The default value is set by fork_from_owner_default
*/
bool fork_owner;
2022-03-30 23:33:49 +05:30
int (*msg_handler)(struct vhost_dev *dev, u32 asid,
2020-03-26 22:01:18 +08:00
struct vhost_iotlb_msg *msg);
2010-01-14 06:17:27 +00:00
};
2019-05-17 00:29:49 -04:00
bool vhost_exceeds_weight(struct vhost_virtqueue *vq, int pkts, int total_len);
2019-01-28 15:05:05 +08:00
void vhost_dev_init(struct vhost_dev *, struct vhost_virtqueue **vqs,
2020-03-26 22:01:18 +08:00
int nvqs, int iov_limit, int weight, int byte_weight,
bool use_worker,
2022-03-30 23:33:49 +05:30
int (*msg_handler)(struct vhost_dev *dev, u32 asid,
2020-03-26 22:01:18 +08:00
struct vhost_iotlb_msg *msg));
2013-05-06 11:15:59 +08:00
long vhost_dev_set_owner(struct vhost_dev *dev);
bool vhost_dev_has_owner(struct vhost_dev *dev);
2010-01-14 06:17:27 +00:00
long vhost_dev_check_owner(struct vhost_dev *);
2020-03-26 22:01:19 +08:00
struct vhost_iotlb *vhost_dev_reset_owner_prepare(void);
void vhost_dev_reset_owner(struct vhost_dev *dev, struct vhost_iotlb *iotlb);
void vhost_dev_cleanup(struct vhost_dev *);
2012-11-01 09:16:46 +00:00
void vhost_dev_stop(struct vhost_dev *);
2012-12-06 14:03:34 +02:00
long vhost_dev_ioctl(struct vhost_dev *, unsigned int ioctl, void __user *argp);
long vhost_vring_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp);
2023-06-26 18:23:05 -05:00
long vhost_worker_ioctl(struct vhost_dev *dev, unsigned int ioctl,
void __user *argp);
bool vhost_vq_access_ok(struct vhost_virtqueue *vq);
bool vhost_log_access_ok(struct vhost_dev *);
void vhost_clear_msg(struct vhost_dev *dev);
2010-01-14 06:17:27 +00:00
2014-06-05 15:20:27 +03:00
int vhost_get_vq_desc(struct vhost_virtqueue *,
struct iovec iov[], unsigned int iov_size,
2010-06-24 16:59:59 +03:00
unsigned int *out_num, unsigned int *in_num,
struct vhost_log *log, unsigned int *log_num);
2010-07-27 18:52:21 +03:00
void vhost_discard_vq_desc(struct vhost_virtqueue *, int n);
2010-01-14 06:17:27 +00:00
bool vhost_vq_work_queue(struct vhost_virtqueue *vq, struct vhost_work *work);
bool vhost_vq_has_work(struct vhost_virtqueue *vq);
bool vhost_vq_is_setup(struct vhost_virtqueue *vq);
2016-02-16 15:59:44 +01:00
int vhost_vq_init_access(struct vhost_virtqueue *);
2010-01-14 06:17:27 +00:00
int vhost_add_used(struct vhost_virtqueue *, unsigned int head, int len);
2010-07-27 18:52:21 +03:00
int vhost_add_used_n(struct vhost_virtqueue *, struct vring_used_elem *heads,
2025-07-14 16:47:54 +08:00
u16 *nheads, unsigned count);
2010-01-14 06:17:27 +00:00
void vhost_add_used_and_signal(struct vhost_dev *, struct vhost_virtqueue *,
2010-07-27 18:52:21 +03:00
unsigned int id, int len);
void vhost_add_used_and_signal_n(struct vhost_dev *, struct vhost_virtqueue *,
2025-07-14 16:47:54 +08:00
struct vring_used_elem *heads, u16 *nheads,
unsigned count);
2010-07-27 18:52:21 +03:00
void vhost_signal(struct vhost_dev *, struct vhost_virtqueue *);
2011-05-20 02:10:54 +03:00
void vhost_disable_notify(struct vhost_dev *, struct vhost_virtqueue *);
2016-03-04 06:24:52 -05:00
bool vhost_vq_avail_empty(struct vhost_dev *, struct vhost_virtqueue *);
2011-05-20 02:10:54 +03:00
bool vhost_enable_notify(struct vhost_dev *, struct vhost_virtqueue *);
2010-01-14 06:17:27 +00:00
int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log,
2019-01-16 16:54:42 +08:00
unsigned int log_num, u64 len,
struct iovec *iov, int count);
int vq_meta_prefetch(struct vhost_virtqueue *vq);
2016-06-23 02:04:32 -04:00
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);
void vhost_set_backend_features(struct vhost_dev *dev, u64 features);
__poll_t vhost_chr_poll(struct file *file, struct vhost_dev *dev,
2016-06-23 02:04:32 -04:00
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);
2023-01-10 10:44:45 +08:00
int vhost_init_device_iotlb(struct vhost_dev *d);
2010-01-14 06:17:27 +00:00
2020-03-26 22:01:19 +08:00
void vhost_iotlb_map_free(struct vhost_iotlb *iotlb,
struct vhost_iotlb_map *map);
2010-01-14 06:17:27 +00:00
#define vq_err(vq, fmt, ...) do { \
pr_debug(pr_fmt(fmt), ##__VA_ARGS__); \
if ((vq)->error_ctx) \
2023-11-22 13:48:23 +01:00
eventfd_signal((vq)->error_ctx);\
2010-01-14 06:17:27 +00:00
} while (0)
enum {
2011-05-20 02:10:54 +03:00
VHOST_FEATURES = (1ULL << VIRTIO_F_NOTIFY_ON_EMPTY) |
(1ULL << VIRTIO_RING_F_INDIRECT_DESC) |
(1ULL << VIRTIO_RING_F_EVENT_IDX) |
2015-09-09 22:24:56 +03:00
(1ULL << VHOST_F_LOG_ALL) |
(1ULL << VIRTIO_F_ANY_LAYOUT) |
(1ULL << VIRTIO_F_VERSION_1)
2010-01-14 06:17:27 +00:00
};
/**
* 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;
}
2014-10-24 14:08:18 +03:00
static inline bool vhost_has_feature(struct vhost_virtqueue *vq, int bit)
2010-01-14 06:17:27 +00:00
{
return virtio_features_test_bit(vq->acked_features_array, bit);
2010-01-14 06:17:27 +00:00
}
2014-10-24 11:48:17 +03:00
2018-08-06 11:17:47 +08:00
static inline bool vhost_backend_has_feature(struct vhost_virtqueue *vq, int bit)
{
return vq->acked_backend_features & (1ULL << bit);
}
2015-10-27 11:37:39 +02:00
#ifdef CONFIG_VHOST_CROSS_ENDIAN_LEGACY
static inline bool vhost_is_little_endian(struct vhost_virtqueue *vq)
{
return vq->is_le;
}
2015-10-27 11:37:39 +02:00
#else
static inline bool vhost_is_little_endian(struct vhost_virtqueue *vq)
{
return virtio_legacy_is_little_endian() || vq->is_le;
}
#endif
2014-10-24 11:48:17 +03:00
/* Memory accessors */
static inline u16 vhost16_to_cpu(struct vhost_virtqueue *vq, __virtio16 val)
{
return __virtio16_to_cpu(vhost_is_little_endian(vq), val);
2014-10-24 11:48:17 +03:00
}
static inline __virtio16 cpu_to_vhost16(struct vhost_virtqueue *vq, u16 val)
{
return __cpu_to_virtio16(vhost_is_little_endian(vq), val);
2014-10-24 11:48:17 +03:00
}
static inline u32 vhost32_to_cpu(struct vhost_virtqueue *vq, __virtio32 val)
{
return __virtio32_to_cpu(vhost_is_little_endian(vq), val);
2014-10-24 11:48:17 +03:00
}
static inline __virtio32 cpu_to_vhost32(struct vhost_virtqueue *vq, u32 val)
{
return __cpu_to_virtio32(vhost_is_little_endian(vq), val);
2014-10-24 11:48:17 +03:00
}
static inline u64 vhost64_to_cpu(struct vhost_virtqueue *vq, __virtio64 val)
{
return __virtio64_to_cpu(vhost_is_little_endian(vq), val);
2014-10-24 11:48:17 +03:00
}
static inline __virtio64 cpu_to_vhost64(struct vhost_virtqueue *vq, u64 val)
{
return __cpu_to_virtio64(vhost_is_little_endian(vq), val);
2014-10-24 11:48:17 +03:00
}
2010-01-14 06:17:27 +00:00
#endif