Commit 78885597 authored by Xie Yongji's avatar Xie Yongji Committed by Michael S. Tsirkin

vduse: Refactor allocation for vduse virtqueues

Allocate memory for vduse virtqueues one by one instead of
doing one allocation for all of them.

This is a preparation for adding sysfs interface for virtqueues.
Signed-off-by: default avatarXie Yongji <xieyongji@bytedance.com>
Acked-by: default avatarJason Wang <jasowang@redhat.com>
Message-Id: <20230323053043.35-5-xieyongji@bytedance.com>
Signed-off-by: default avatarMichael S. Tsirkin <mst@redhat.com>
parent 3dad5682
...@@ -76,7 +76,7 @@ struct vduse_umem { ...@@ -76,7 +76,7 @@ struct vduse_umem {
struct vduse_dev { struct vduse_dev {
struct vduse_vdpa *vdev; struct vduse_vdpa *vdev;
struct device *dev; struct device *dev;
struct vduse_virtqueue *vqs; struct vduse_virtqueue **vqs;
struct vduse_iova_domain *domain; struct vduse_iova_domain *domain;
char *name; char *name;
struct mutex lock; struct mutex lock;
...@@ -434,7 +434,7 @@ static void vduse_dev_reset(struct vduse_dev *dev) ...@@ -434,7 +434,7 @@ static void vduse_dev_reset(struct vduse_dev *dev)
flush_work(&dev->inject); flush_work(&dev->inject);
for (i = 0; i < dev->vq_num; i++) { for (i = 0; i < dev->vq_num; i++) {
struct vduse_virtqueue *vq = &dev->vqs[i]; struct vduse_virtqueue *vq = dev->vqs[i];
vq->ready = false; vq->ready = false;
vq->desc_addr = 0; vq->desc_addr = 0;
...@@ -466,7 +466,7 @@ static int vduse_vdpa_set_vq_address(struct vdpa_device *vdpa, u16 idx, ...@@ -466,7 +466,7 @@ static int vduse_vdpa_set_vq_address(struct vdpa_device *vdpa, u16 idx,
u64 device_area) u64 device_area)
{ {
struct vduse_dev *dev = vdpa_to_vduse(vdpa); struct vduse_dev *dev = vdpa_to_vduse(vdpa);
struct vduse_virtqueue *vq = &dev->vqs[idx]; struct vduse_virtqueue *vq = dev->vqs[idx];
vq->desc_addr = desc_area; vq->desc_addr = desc_area;
vq->driver_addr = driver_area; vq->driver_addr = driver_area;
...@@ -500,7 +500,7 @@ static void vduse_vq_kick_work(struct work_struct *work) ...@@ -500,7 +500,7 @@ static void vduse_vq_kick_work(struct work_struct *work)
static void vduse_vdpa_kick_vq(struct vdpa_device *vdpa, u16 idx) static void vduse_vdpa_kick_vq(struct vdpa_device *vdpa, u16 idx)
{ {
struct vduse_dev *dev = vdpa_to_vduse(vdpa); struct vduse_dev *dev = vdpa_to_vduse(vdpa);
struct vduse_virtqueue *vq = &dev->vqs[idx]; struct vduse_virtqueue *vq = dev->vqs[idx];
if (!eventfd_signal_allowed()) { if (!eventfd_signal_allowed()) {
schedule_work(&vq->kick); schedule_work(&vq->kick);
...@@ -513,7 +513,7 @@ static void vduse_vdpa_set_vq_cb(struct vdpa_device *vdpa, u16 idx, ...@@ -513,7 +513,7 @@ static void vduse_vdpa_set_vq_cb(struct vdpa_device *vdpa, u16 idx,
struct vdpa_callback *cb) struct vdpa_callback *cb)
{ {
struct vduse_dev *dev = vdpa_to_vduse(vdpa); struct vduse_dev *dev = vdpa_to_vduse(vdpa);
struct vduse_virtqueue *vq = &dev->vqs[idx]; struct vduse_virtqueue *vq = dev->vqs[idx];
spin_lock(&vq->irq_lock); spin_lock(&vq->irq_lock);
vq->cb.callback = cb->callback; vq->cb.callback = cb->callback;
...@@ -524,7 +524,7 @@ static void vduse_vdpa_set_vq_cb(struct vdpa_device *vdpa, u16 idx, ...@@ -524,7 +524,7 @@ static void vduse_vdpa_set_vq_cb(struct vdpa_device *vdpa, u16 idx,
static void vduse_vdpa_set_vq_num(struct vdpa_device *vdpa, u16 idx, u32 num) static void vduse_vdpa_set_vq_num(struct vdpa_device *vdpa, u16 idx, u32 num)
{ {
struct vduse_dev *dev = vdpa_to_vduse(vdpa); struct vduse_dev *dev = vdpa_to_vduse(vdpa);
struct vduse_virtqueue *vq = &dev->vqs[idx]; struct vduse_virtqueue *vq = dev->vqs[idx];
vq->num = num; vq->num = num;
} }
...@@ -533,7 +533,7 @@ static void vduse_vdpa_set_vq_ready(struct vdpa_device *vdpa, ...@@ -533,7 +533,7 @@ static void vduse_vdpa_set_vq_ready(struct vdpa_device *vdpa,
u16 idx, bool ready) u16 idx, bool ready)
{ {
struct vduse_dev *dev = vdpa_to_vduse(vdpa); struct vduse_dev *dev = vdpa_to_vduse(vdpa);
struct vduse_virtqueue *vq = &dev->vqs[idx]; struct vduse_virtqueue *vq = dev->vqs[idx];
vq->ready = ready; vq->ready = ready;
} }
...@@ -541,7 +541,7 @@ static void vduse_vdpa_set_vq_ready(struct vdpa_device *vdpa, ...@@ -541,7 +541,7 @@ static void vduse_vdpa_set_vq_ready(struct vdpa_device *vdpa,
static bool vduse_vdpa_get_vq_ready(struct vdpa_device *vdpa, u16 idx) static bool vduse_vdpa_get_vq_ready(struct vdpa_device *vdpa, u16 idx)
{ {
struct vduse_dev *dev = vdpa_to_vduse(vdpa); struct vduse_dev *dev = vdpa_to_vduse(vdpa);
struct vduse_virtqueue *vq = &dev->vqs[idx]; struct vduse_virtqueue *vq = dev->vqs[idx];
return vq->ready; return vq->ready;
} }
...@@ -550,7 +550,7 @@ static int vduse_vdpa_set_vq_state(struct vdpa_device *vdpa, u16 idx, ...@@ -550,7 +550,7 @@ static int vduse_vdpa_set_vq_state(struct vdpa_device *vdpa, u16 idx,
const struct vdpa_vq_state *state) const struct vdpa_vq_state *state)
{ {
struct vduse_dev *dev = vdpa_to_vduse(vdpa); struct vduse_dev *dev = vdpa_to_vduse(vdpa);
struct vduse_virtqueue *vq = &dev->vqs[idx]; struct vduse_virtqueue *vq = dev->vqs[idx];
if (dev->driver_features & BIT_ULL(VIRTIO_F_RING_PACKED)) { if (dev->driver_features & BIT_ULL(VIRTIO_F_RING_PACKED)) {
vq->state.packed.last_avail_counter = vq->state.packed.last_avail_counter =
...@@ -569,7 +569,7 @@ static int vduse_vdpa_get_vq_state(struct vdpa_device *vdpa, u16 idx, ...@@ -569,7 +569,7 @@ static int vduse_vdpa_get_vq_state(struct vdpa_device *vdpa, u16 idx,
struct vdpa_vq_state *state) struct vdpa_vq_state *state)
{ {
struct vduse_dev *dev = vdpa_to_vduse(vdpa); struct vduse_dev *dev = vdpa_to_vduse(vdpa);
struct vduse_virtqueue *vq = &dev->vqs[idx]; struct vduse_virtqueue *vq = dev->vqs[idx];
if (dev->driver_features & BIT_ULL(VIRTIO_F_RING_PACKED)) if (dev->driver_features & BIT_ULL(VIRTIO_F_RING_PACKED))
return vduse_dev_get_vq_state_packed(dev, vq, &state->packed); return vduse_dev_get_vq_state_packed(dev, vq, &state->packed);
...@@ -624,8 +624,8 @@ static u16 vduse_vdpa_get_vq_num_max(struct vdpa_device *vdpa) ...@@ -624,8 +624,8 @@ static u16 vduse_vdpa_get_vq_num_max(struct vdpa_device *vdpa)
int i; int i;
for (i = 0; i < dev->vq_num; i++) for (i = 0; i < dev->vq_num; i++)
if (num_max < dev->vqs[i].num_max) if (num_max < dev->vqs[i]->num_max)
num_max = dev->vqs[i].num_max; num_max = dev->vqs[i]->num_max;
return num_max; return num_max;
} }
...@@ -863,7 +863,7 @@ static int vduse_kickfd_setup(struct vduse_dev *dev, ...@@ -863,7 +863,7 @@ static int vduse_kickfd_setup(struct vduse_dev *dev,
return -EINVAL; return -EINVAL;
index = array_index_nospec(eventfd->index, dev->vq_num); index = array_index_nospec(eventfd->index, dev->vq_num);
vq = &dev->vqs[index]; vq = dev->vqs[index];
if (eventfd->fd >= 0) { if (eventfd->fd >= 0) {
ctx = eventfd_ctx_fdget(eventfd->fd); ctx = eventfd_ctx_fdget(eventfd->fd);
if (IS_ERR(ctx)) if (IS_ERR(ctx))
...@@ -889,7 +889,7 @@ static bool vduse_dev_is_ready(struct vduse_dev *dev) ...@@ -889,7 +889,7 @@ static bool vduse_dev_is_ready(struct vduse_dev *dev)
int i; int i;
for (i = 0; i < dev->vq_num; i++) for (i = 0; i < dev->vq_num; i++)
if (!dev->vqs[i].num_max) if (!dev->vqs[i]->num_max)
return false; return false;
return true; return true;
...@@ -1130,7 +1130,7 @@ static long vduse_dev_ioctl(struct file *file, unsigned int cmd, ...@@ -1130,7 +1130,7 @@ static long vduse_dev_ioctl(struct file *file, unsigned int cmd,
break; break;
index = array_index_nospec(config.index, dev->vq_num); index = array_index_nospec(config.index, dev->vq_num);
dev->vqs[index].num_max = config.max_size; dev->vqs[index]->num_max = config.max_size;
ret = 0; ret = 0;
break; break;
} }
...@@ -1148,7 +1148,7 @@ static long vduse_dev_ioctl(struct file *file, unsigned int cmd, ...@@ -1148,7 +1148,7 @@ static long vduse_dev_ioctl(struct file *file, unsigned int cmd,
break; break;
index = array_index_nospec(vq_info.index, dev->vq_num); index = array_index_nospec(vq_info.index, dev->vq_num);
vq = &dev->vqs[index]; vq = dev->vqs[index];
vq_info.desc_addr = vq->desc_addr; vq_info.desc_addr = vq->desc_addr;
vq_info.driver_addr = vq->driver_addr; vq_info.driver_addr = vq->driver_addr;
vq_info.device_addr = vq->device_addr; vq_info.device_addr = vq->device_addr;
...@@ -1198,7 +1198,7 @@ static long vduse_dev_ioctl(struct file *file, unsigned int cmd, ...@@ -1198,7 +1198,7 @@ static long vduse_dev_ioctl(struct file *file, unsigned int cmd,
break; break;
index = array_index_nospec(index, dev->vq_num); index = array_index_nospec(index, dev->vq_num);
ret = vduse_dev_queue_irq_work(dev, &dev->vqs[index].inject); ret = vduse_dev_queue_irq_work(dev, &dev->vqs[index]->inject);
break; break;
} }
case VDUSE_IOTLB_REG_UMEM: { case VDUSE_IOTLB_REG_UMEM: {
...@@ -1339,6 +1339,49 @@ static const struct file_operations vduse_dev_fops = { ...@@ -1339,6 +1339,49 @@ static const struct file_operations vduse_dev_fops = {
.llseek = noop_llseek, .llseek = noop_llseek,
}; };
static void vduse_dev_deinit_vqs(struct vduse_dev *dev)
{
int i;
if (!dev->vqs)
return;
for (i = 0; i < dev->vq_num; i++)
kfree(dev->vqs[i]);
kfree(dev->vqs);
}
static int vduse_dev_init_vqs(struct vduse_dev *dev, u32 vq_align, u32 vq_num)
{
int i;
dev->vq_align = vq_align;
dev->vq_num = vq_num;
dev->vqs = kcalloc(dev->vq_num, sizeof(*dev->vqs), GFP_KERNEL);
if (!dev->vqs)
return -ENOMEM;
for (i = 0; i < vq_num; i++) {
dev->vqs[i] = kzalloc(sizeof(*dev->vqs[i]), GFP_KERNEL);
if (!dev->vqs[i])
goto err;
dev->vqs[i]->index = i;
INIT_WORK(&dev->vqs[i]->inject, vduse_vq_irq_inject);
INIT_WORK(&dev->vqs[i]->kick, vduse_vq_kick_work);
spin_lock_init(&dev->vqs[i]->kick_lock);
spin_lock_init(&dev->vqs[i]->irq_lock);
}
return 0;
err:
while (i--)
kfree(dev->vqs[i]);
kfree(dev->vqs);
dev->vqs = NULL;
return -ENOMEM;
}
static struct vduse_dev *vduse_dev_create(void) static struct vduse_dev *vduse_dev_create(void)
{ {
struct vduse_dev *dev = kzalloc(sizeof(*dev), GFP_KERNEL); struct vduse_dev *dev = kzalloc(sizeof(*dev), GFP_KERNEL);
...@@ -1396,7 +1439,7 @@ static int vduse_destroy_dev(char *name) ...@@ -1396,7 +1439,7 @@ static int vduse_destroy_dev(char *name)
device_destroy(vduse_class, MKDEV(MAJOR(vduse_major), dev->minor)); device_destroy(vduse_class, MKDEV(MAJOR(vduse_major), dev->minor));
idr_remove(&vduse_idr, dev->minor); idr_remove(&vduse_idr, dev->minor);
kvfree(dev->config); kvfree(dev->config);
kfree(dev->vqs); vduse_dev_deinit_vqs(dev);
vduse_domain_destroy(dev->domain); vduse_domain_destroy(dev->domain);
kfree(dev->name); kfree(dev->name);
vduse_dev_destroy(dev); vduse_dev_destroy(dev);
...@@ -1486,7 +1529,7 @@ ATTRIBUTE_GROUPS(vduse_dev); ...@@ -1486,7 +1529,7 @@ ATTRIBUTE_GROUPS(vduse_dev);
static int vduse_create_dev(struct vduse_dev_config *config, static int vduse_create_dev(struct vduse_dev_config *config,
void *config_buf, u64 api_version) void *config_buf, u64 api_version)
{ {
int i, ret; int ret;
struct vduse_dev *dev; struct vduse_dev *dev;
ret = -EEXIST; ret = -EEXIST;
...@@ -1513,19 +1556,10 @@ static int vduse_create_dev(struct vduse_dev_config *config, ...@@ -1513,19 +1556,10 @@ static int vduse_create_dev(struct vduse_dev_config *config,
dev->config = config_buf; dev->config = config_buf;
dev->config_size = config->config_size; dev->config_size = config->config_size;
dev->vq_align = config->vq_align;
dev->vq_num = config->vq_num;
dev->vqs = kcalloc(dev->vq_num, sizeof(*dev->vqs), GFP_KERNEL);
if (!dev->vqs)
goto err_vqs;
for (i = 0; i < dev->vq_num; i++) { ret = vduse_dev_init_vqs(dev, config->vq_align, config->vq_num);
dev->vqs[i].index = i; if (ret)
INIT_WORK(&dev->vqs[i].inject, vduse_vq_irq_inject); goto err_vqs;
INIT_WORK(&dev->vqs[i].kick, vduse_vq_kick_work);
spin_lock_init(&dev->vqs[i].kick_lock);
spin_lock_init(&dev->vqs[i].irq_lock);
}
ret = idr_alloc(&vduse_idr, dev, 1, VDUSE_DEV_MAX, GFP_KERNEL); ret = idr_alloc(&vduse_idr, dev, 1, VDUSE_DEV_MAX, GFP_KERNEL);
if (ret < 0) if (ret < 0)
...@@ -1546,7 +1580,7 @@ static int vduse_create_dev(struct vduse_dev_config *config, ...@@ -1546,7 +1580,7 @@ static int vduse_create_dev(struct vduse_dev_config *config,
err_dev: err_dev:
idr_remove(&vduse_idr, dev->minor); idr_remove(&vduse_idr, dev->minor);
err_idr: err_idr:
kfree(dev->vqs); vduse_dev_deinit_vqs(dev);
err_vqs: err_vqs:
vduse_domain_destroy(dev->domain); vduse_domain_destroy(dev->domain);
err_domain: err_domain:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment