Allow individual simulator to customize the allocation size.
Signed-off-by: Jason Wang <jasowang@redhat.com>
---
drivers/vdpa/vdpa_sim/vdpa_sim.c | 8 ++++++--
drivers/vdpa/vdpa_sim/vdpa_sim.h | 1 +
drivers/vdpa/vdpa_sim/vdpa_sim_blk.c | 1 +
drivers/vdpa/vdpa_sim/vdpa_sim_net.c | 1 +
4 files changed, 9 insertions(+), 2 deletions(-)
diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim.c b/drivers/vdpa/vdpa_sim/vdpa_sim.c
index 757afef86ba0..55aaa023a6e2 100644
--- a/drivers/vdpa/vdpa_sim/vdpa_sim.c
+++ b/drivers/vdpa/vdpa_sim/vdpa_sim.c
@@ -253,7 +253,10 @@ struct vdpasim *vdpasim_create(struct vdpasim_dev_attr *dev_attr,
struct vdpa_device *vdpa;
struct vdpasim *vdpasim;
struct device *dev;
- int i, ret = -ENOMEM;
+ int i, ret = -EINVAL;
+
+ if (!dev_attr->alloc_size)
+ goto err_alloc;
if (config->mask & BIT_ULL(VDPA_ATTR_DEV_FEATURES)) {
if (config->device_features &
@@ -268,9 +271,10 @@ struct vdpasim *vdpasim_create(struct vdpasim_dev_attr *dev_attr,
else
ops = &vdpasim_config_ops;
+ ret = -ENOMEM;
vdpa = __vdpa_alloc_device(NULL, ops,
dev_attr->ngroups, dev_attr->nas,
- sizeof(struct vdpasim),
+ dev_attr->alloc_size,
dev_attr->name, false);
if (IS_ERR(vdpa)) {
ret = PTR_ERR(vdpa);
diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim.h b/drivers/vdpa/vdpa_sim/vdpa_sim.h
index 0e78737dcc16..51c070a543f1 100644
--- a/drivers/vdpa/vdpa_sim/vdpa_sim.h
+++ b/drivers/vdpa/vdpa_sim/vdpa_sim.h
@@ -37,6 +37,7 @@ struct vdpasim_dev_attr {
struct vdpa_mgmt_dev *mgmt_dev;
const char *name;
u64 supported_features;
+ size_t alloc_size;
size_t config_size;
size_t buffer_size;
int nvqs;
diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim_blk.c b/drivers/vdpa/vdpa_sim/vdpa_sim_blk.c
index c6db1a1baf76..4f7c35f59aa5 100644
--- a/drivers/vdpa/vdpa_sim/vdpa_sim_blk.c
+++ b/drivers/vdpa/vdpa_sim/vdpa_sim_blk.c
@@ -378,6 +378,7 @@ static int vdpasim_blk_dev_add(struct vdpa_mgmt_dev *mdev, const char *name,
dev_attr.nvqs = VDPASIM_BLK_VQ_NUM;
dev_attr.ngroups = VDPASIM_BLK_GROUP_NUM;
dev_attr.nas = VDPASIM_BLK_AS_NUM;
+ dev_attr.alloc_size = sizeof(struct vdpasim);
dev_attr.config_size = sizeof(struct virtio_blk_config);
dev_attr.get_config = vdpasim_blk_get_config;
dev_attr.work_fn = vdpasim_blk_work;
diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim_net.c b/drivers/vdpa/vdpa_sim/vdpa_sim_net.c
index c3cb225ea469..20cd5cdff919 100644
--- a/drivers/vdpa/vdpa_sim/vdpa_sim_net.c
+++ b/drivers/vdpa/vdpa_sim/vdpa_sim_net.c
@@ -249,6 +249,7 @@ static int vdpasim_net_dev_add(struct vdpa_mgmt_dev *mdev, const char *name,
dev_attr.nvqs = VDPASIM_NET_VQ_NUM;
dev_attr.ngroups = VDPASIM_NET_GROUP_NUM;
dev_attr.nas = VDPASIM_NET_AS_NUM;
+ dev_attr.alloc_size = sizeof(struct vdpasim);
dev_attr.config_size = sizeof(struct virtio_net_config);
dev_attr.get_config = vdpasim_net_get_config;
dev_attr.work_fn = vdpasim_net_work;
--
2.25.1
On Thu, Dec 22, 2022 at 6:01 AM Jason Wang <jasowang@redhat.com> wrote: > > Allow individual simulator to customize the allocation size. > > Signed-off-by: Jason Wang <jasowang@redhat.com> > --- > drivers/vdpa/vdpa_sim/vdpa_sim.c | 8 ++++++-- > drivers/vdpa/vdpa_sim/vdpa_sim.h | 1 + > drivers/vdpa/vdpa_sim/vdpa_sim_blk.c | 1 + > drivers/vdpa/vdpa_sim/vdpa_sim_net.c | 1 + > 4 files changed, 9 insertions(+), 2 deletions(-) > > diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim.c b/drivers/vdpa/vdpa_sim/vdpa_sim.c > index 757afef86ba0..55aaa023a6e2 100644 > --- a/drivers/vdpa/vdpa_sim/vdpa_sim.c > +++ b/drivers/vdpa/vdpa_sim/vdpa_sim.c > @@ -253,7 +253,10 @@ struct vdpasim *vdpasim_create(struct vdpasim_dev_attr *dev_attr, > struct vdpa_device *vdpa; > struct vdpasim *vdpasim; > struct device *dev; > - int i, ret = -ENOMEM; > + int i, ret = -EINVAL; > + > + if (!dev_attr->alloc_size) > + goto err_alloc; It's weird that we need an error goto here and the next chunk of code may return ERR_PTR(-EINVAL) directly. It's better to return directly here in my opinion. > > if (config->mask & BIT_ULL(VDPA_ATTR_DEV_FEATURES)) { > if (config->device_features & > @@ -268,9 +271,10 @@ struct vdpasim *vdpasim_create(struct vdpasim_dev_attr *dev_attr, > else > ops = &vdpasim_config_ops; > > + ret = -ENOMEM; Nitpicking again, To set ret = -ENOMEM here is weird to me, since the next failure on __vdpa_alloc_device will override it. I'd set it right before dma_set_mask_and_coherent, closer to its actual usage. Actually, I'd propagate dma_set_mask_and_coherent error and set ret = -ENOMEM right before *alloc functions, but it wasn't done that way before this series, so not a reason to NAK to me. Thanks! > vdpa = __vdpa_alloc_device(NULL, ops, > dev_attr->ngroups, dev_attr->nas, > - sizeof(struct vdpasim), > + dev_attr->alloc_size, > dev_attr->name, false); > if (IS_ERR(vdpa)) { > ret = PTR_ERR(vdpa); > diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim.h b/drivers/vdpa/vdpa_sim/vdpa_sim.h > index 0e78737dcc16..51c070a543f1 100644 > --- a/drivers/vdpa/vdpa_sim/vdpa_sim.h > +++ b/drivers/vdpa/vdpa_sim/vdpa_sim.h > @@ -37,6 +37,7 @@ struct vdpasim_dev_attr { > struct vdpa_mgmt_dev *mgmt_dev; > const char *name; > u64 supported_features; > + size_t alloc_size; > size_t config_size; > size_t buffer_size; > int nvqs; > diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim_blk.c b/drivers/vdpa/vdpa_sim/vdpa_sim_blk.c > index c6db1a1baf76..4f7c35f59aa5 100644 > --- a/drivers/vdpa/vdpa_sim/vdpa_sim_blk.c > +++ b/drivers/vdpa/vdpa_sim/vdpa_sim_blk.c > @@ -378,6 +378,7 @@ static int vdpasim_blk_dev_add(struct vdpa_mgmt_dev *mdev, const char *name, > dev_attr.nvqs = VDPASIM_BLK_VQ_NUM; > dev_attr.ngroups = VDPASIM_BLK_GROUP_NUM; > dev_attr.nas = VDPASIM_BLK_AS_NUM; > + dev_attr.alloc_size = sizeof(struct vdpasim); > dev_attr.config_size = sizeof(struct virtio_blk_config); > dev_attr.get_config = vdpasim_blk_get_config; > dev_attr.work_fn = vdpasim_blk_work; > diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim_net.c b/drivers/vdpa/vdpa_sim/vdpa_sim_net.c > index c3cb225ea469..20cd5cdff919 100644 > --- a/drivers/vdpa/vdpa_sim/vdpa_sim_net.c > +++ b/drivers/vdpa/vdpa_sim/vdpa_sim_net.c > @@ -249,6 +249,7 @@ static int vdpasim_net_dev_add(struct vdpa_mgmt_dev *mdev, const char *name, > dev_attr.nvqs = VDPASIM_NET_VQ_NUM; > dev_attr.ngroups = VDPASIM_NET_GROUP_NUM; > dev_attr.nas = VDPASIM_NET_AS_NUM; > + dev_attr.alloc_size = sizeof(struct vdpasim); > dev_attr.config_size = sizeof(struct virtio_net_config); > dev_attr.get_config = vdpasim_net_get_config; > dev_attr.work_fn = vdpasim_net_work; > -- > 2.25.1 >
On Thu, Dec 22, 2022 at 4:22 PM Eugenio Perez Martin <eperezma@redhat.com> wrote: > > On Thu, Dec 22, 2022 at 6:01 AM Jason Wang <jasowang@redhat.com> wrote: > > > > Allow individual simulator to customize the allocation size. > > > > Signed-off-by: Jason Wang <jasowang@redhat.com> > > --- > > drivers/vdpa/vdpa_sim/vdpa_sim.c | 8 ++++++-- > > drivers/vdpa/vdpa_sim/vdpa_sim.h | 1 + > > drivers/vdpa/vdpa_sim/vdpa_sim_blk.c | 1 + > > drivers/vdpa/vdpa_sim/vdpa_sim_net.c | 1 + > > 4 files changed, 9 insertions(+), 2 deletions(-) > > > > diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim.c b/drivers/vdpa/vdpa_sim/vdpa_sim.c > > index 757afef86ba0..55aaa023a6e2 100644 > > --- a/drivers/vdpa/vdpa_sim/vdpa_sim.c > > +++ b/drivers/vdpa/vdpa_sim/vdpa_sim.c > > @@ -253,7 +253,10 @@ struct vdpasim *vdpasim_create(struct vdpasim_dev_attr *dev_attr, > > struct vdpa_device *vdpa; > > struct vdpasim *vdpasim; > > struct device *dev; > > - int i, ret = -ENOMEM; > > + int i, ret = -EINVAL; > > + > > + if (!dev_attr->alloc_size) > > + goto err_alloc; > > It's weird that we need an error goto here and the next chunk of code > may return ERR_PTR(-EINVAL) directly. It's better to return directly > here in my opinion. Right, let me fix it. > > > > > if (config->mask & BIT_ULL(VDPA_ATTR_DEV_FEATURES)) { > > if (config->device_features & > > @@ -268,9 +271,10 @@ struct vdpasim *vdpasim_create(struct vdpasim_dev_attr *dev_attr, > > else > > ops = &vdpasim_config_ops; > > > > + ret = -ENOMEM; > > Nitpicking again, > > To set ret = -ENOMEM here is weird to me, since the next failure on > __vdpa_alloc_device will override it. I'd set it right before > dma_set_mask_and_coherent, closer to its actual usage. Yes. > > Actually, I'd propagate dma_set_mask_and_coherent error and set ret = > -ENOMEM right before *alloc functions, but it wasn't done that way > before this series, so not a reason to NAK to me. Yes and I think this needs a separate fix. Thanks > > Thanks! > > > vdpa = __vdpa_alloc_device(NULL, ops, > > dev_attr->ngroups, dev_attr->nas, > > - sizeof(struct vdpasim), > > + dev_attr->alloc_size, > > dev_attr->name, false); > > if (IS_ERR(vdpa)) { > > ret = PTR_ERR(vdpa); > > diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim.h b/drivers/vdpa/vdpa_sim/vdpa_sim.h > > index 0e78737dcc16..51c070a543f1 100644 > > --- a/drivers/vdpa/vdpa_sim/vdpa_sim.h > > +++ b/drivers/vdpa/vdpa_sim/vdpa_sim.h > > @@ -37,6 +37,7 @@ struct vdpasim_dev_attr { > > struct vdpa_mgmt_dev *mgmt_dev; > > const char *name; > > u64 supported_features; > > + size_t alloc_size; > > size_t config_size; > > size_t buffer_size; > > int nvqs; > > diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim_blk.c b/drivers/vdpa/vdpa_sim/vdpa_sim_blk.c > > index c6db1a1baf76..4f7c35f59aa5 100644 > > --- a/drivers/vdpa/vdpa_sim/vdpa_sim_blk.c > > +++ b/drivers/vdpa/vdpa_sim/vdpa_sim_blk.c > > @@ -378,6 +378,7 @@ static int vdpasim_blk_dev_add(struct vdpa_mgmt_dev *mdev, const char *name, > > dev_attr.nvqs = VDPASIM_BLK_VQ_NUM; > > dev_attr.ngroups = VDPASIM_BLK_GROUP_NUM; > > dev_attr.nas = VDPASIM_BLK_AS_NUM; > > + dev_attr.alloc_size = sizeof(struct vdpasim); > > dev_attr.config_size = sizeof(struct virtio_blk_config); > > dev_attr.get_config = vdpasim_blk_get_config; > > dev_attr.work_fn = vdpasim_blk_work; > > diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim_net.c b/drivers/vdpa/vdpa_sim/vdpa_sim_net.c > > index c3cb225ea469..20cd5cdff919 100644 > > --- a/drivers/vdpa/vdpa_sim/vdpa_sim_net.c > > +++ b/drivers/vdpa/vdpa_sim/vdpa_sim_net.c > > @@ -249,6 +249,7 @@ static int vdpasim_net_dev_add(struct vdpa_mgmt_dev *mdev, const char *name, > > dev_attr.nvqs = VDPASIM_NET_VQ_NUM; > > dev_attr.ngroups = VDPASIM_NET_GROUP_NUM; > > dev_attr.nas = VDPASIM_NET_AS_NUM; > > + dev_attr.alloc_size = sizeof(struct vdpasim); > > dev_attr.config_size = sizeof(struct virtio_net_config); > > dev_attr.get_config = vdpasim_net_get_config; > > dev_attr.work_fn = vdpasim_net_work; > > -- > > 2.25.1 > > >
© 2016 - 2025 Red Hat, Inc.