Message ID | 20210204172230.85853-9-sgarzare@redhat.com (mailing list archive) |
---|---|
State | New, archived |
Headers | show |
Series | vdpa: add vdpa simulator for block device | expand |
Hi Stefano, I love your patch! Yet something to improve: [auto build test ERROR on vhost/linux-next] [also build test ERROR on linus/master v5.11-rc6 next-20210125] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in https://git-scm.com/docs/git-format-patch] url: https://github.com/0day-ci/linux/commits/Stefano-Garzarella/vdpa-add-vdpa-simulator-for-block-device/20210205-020448 base: https://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost.git linux-next config: parisc-randconfig-r005-20210204 (attached as .config) compiler: hppa-linux-gcc (GCC) 9.3.0 reproduce (this is a W=1 build): wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross # https://github.com/0day-ci/linux/commit/17cf2b1e6be083a27f43414cc0f2524cf81fff60 git remote add linux-review https://github.com/0day-ci/linux git fetch --no-tags linux-review Stefano-Garzarella/vdpa-add-vdpa-simulator-for-block-device/20210205-020448 git checkout 17cf2b1e6be083a27f43414cc0f2524cf81fff60 # save the attached .config to linux build tree COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-9.3.0 make.cross ARCH=parisc If you fix the issue, kindly add following tag as appropriate Reported-by: kernel test robot <lkp@intel.com> All errors (new ones prefixed by >>): drivers/vdpa/mlx5/net/mlx5_vnet.c: In function 'mlx5_vdpa_get_config': >> drivers/vdpa/mlx5/net/mlx5_vnet.c:1810:10: error: expected ';' before '}' token 1810 | return 0 | ^ | ; 1811 | } | ~ vim +1810 drivers/vdpa/mlx5/net/mlx5_vnet.c 1798 1799 static int mlx5_vdpa_get_config(struct vdpa_device *vdev, unsigned int offset, void *buf, 1800 unsigned int len) 1801 { 1802 struct mlx5_vdpa_dev *mvdev = to_mvdev(vdev); 1803 struct mlx5_vdpa_net *ndev = to_mlx5_vdpa_ndev(mvdev); 1804 1805 if (offset + len > sizeof(struct virtio_net_config)) 1806 return -EINVAL; 1807 1808 memcpy(buf, (u8 *)&ndev->config + offset, len); 1809 > 1810 return 0 1811 } 1812 --- 0-DAY CI Kernel Test Service, Intel Corporation https://lists.01.org/hyperkitty/list/kbuild-all@lists.01.org
On Fri, Feb 05, 2021 at 06:31:20AM +0800, kernel test robot wrote: >Hi Stefano, > >I love your patch! Yet something to improve: > >[auto build test ERROR on vhost/linux-next] >[also build test ERROR on linus/master v5.11-rc6 next-20210125] >[If your patch is applied to the wrong git tree, kindly drop us a note. >And when submitting patch, we suggest to use '--base' as documented in >https://git-scm.com/docs/git-format-patch] > >url: https://github.com/0day-ci/linux/commits/Stefano-Garzarella/vdpa-add-vdpa-simulator-for-block-device/20210205-020448 >base: https://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost.git linux-next >config: parisc-randconfig-r005-20210204 (attached as .config) >compiler: hppa-linux-gcc (GCC) 9.3.0 >reproduce (this is a W=1 build): > wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross > chmod +x ~/bin/make.cross > # https://github.com/0day-ci/linux/commit/17cf2b1e6be083a27f43414cc0f2524cf81fff60 > git remote add linux-review https://github.com/0day-ci/linux > git fetch --no-tags linux-review Stefano-Garzarella/vdpa-add-vdpa-simulator-for-block-device/20210205-020448 > git checkout 17cf2b1e6be083a27f43414cc0f2524cf81fff60 > # save the attached .config to linux build tree > COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-9.3.0 make.cross ARCH=parisc > >If you fix the issue, kindly add following tag as appropriate >Reported-by: kernel test robot <lkp@intel.com> > >All errors (new ones prefixed by >>): > > drivers/vdpa/mlx5/net/mlx5_vnet.c: In function 'mlx5_vdpa_get_config': >>> drivers/vdpa/mlx5/net/mlx5_vnet.c:1810:10: error: expected ';' before '}' token > 1810 | return 0 > | ^ > | ; > 1811 | } > | ~ Ooops, I forgot to add mlx5_vnet.c on my .config. Sorry for that, I'll fix in the next release and I'll build all vDPA related stuff. Stefano
On 2021/2/5 上午1:22, Stefano Garzarella wrote: > All implementations of these callbacks already validate inputs. > > Let's return an error from these callbacks, so the caller doesn't > need to validate the input anymore. > > We update all implementations to return -EINVAL in case of invalid > input. > > Signed-off-by: Stefano Garzarella <sgarzare@redhat.com> > --- > include/linux/vdpa.h | 18 ++++++++++-------- > drivers/vdpa/ifcvf/ifcvf_main.c | 24 ++++++++++++++++-------- > drivers/vdpa/mlx5/net/mlx5_vnet.c | 17 +++++++++++------ > drivers/vdpa/vdpa_sim/vdpa_sim.c | 16 ++++++++++------ > 4 files changed, 47 insertions(+), 28 deletions(-) > > diff --git a/include/linux/vdpa.h b/include/linux/vdpa.h > index 4ab5494503a8..0e0cbd5fb41b 100644 > --- a/include/linux/vdpa.h > +++ b/include/linux/vdpa.h > @@ -157,6 +157,7 @@ struct vdpa_iova_range { > * @buf: buffer used to read to > * @len: the length to read from > * configuration space > + * Returns integer: success (0) or error (< 0) > * @set_config: Write to device specific configuration space > * @vdev: vdpa device > * @offset: offset from the beginning of > @@ -164,6 +165,7 @@ struct vdpa_iova_range { > * @buf: buffer used to write from > * @len: the length to write to > * configuration space > + * Returns integer: success (0) or error (< 0) > * @get_generation: Get device config generation (optional) > * @vdev: vdpa device > * Returns u32: device generation > @@ -231,10 +233,10 @@ struct vdpa_config_ops { > u32 (*get_vendor_id)(struct vdpa_device *vdev); > u8 (*get_status)(struct vdpa_device *vdev); > void (*set_status)(struct vdpa_device *vdev, u8 status); > - void (*get_config)(struct vdpa_device *vdev, unsigned int offset, > - void *buf, unsigned int len); > - void (*set_config)(struct vdpa_device *vdev, unsigned int offset, > - const void *buf, unsigned int len); > + int (*get_config)(struct vdpa_device *vdev, unsigned int offset, > + void *buf, unsigned int len); > + int (*set_config)(struct vdpa_device *vdev, unsigned int offset, > + const void *buf, unsigned int len); > u32 (*get_generation)(struct vdpa_device *vdev); > struct vdpa_iova_range (*get_iova_range)(struct vdpa_device *vdev); > > @@ -329,8 +331,8 @@ static inline int vdpa_set_features(struct vdpa_device *vdev, u64 features) > } > > > -static inline void vdpa_get_config(struct vdpa_device *vdev, unsigned offset, > - void *buf, unsigned int len) > +static inline int vdpa_get_config(struct vdpa_device *vdev, unsigned offset, > + void *buf, unsigned int len) > { > const struct vdpa_config_ops *ops = vdev->config; > > @@ -339,8 +341,8 @@ static inline void vdpa_get_config(struct vdpa_device *vdev, unsigned offset, > * If it does happen we assume a legacy guest. > */ > if (!vdev->features_valid) > - vdpa_set_features(vdev, 0); > - ops->get_config(vdev, offset, buf, len); > + return vdpa_set_features(vdev, 0); > + return ops->get_config(vdev, offset, buf, len); > } > > /** > diff --git a/drivers/vdpa/ifcvf/ifcvf_main.c b/drivers/vdpa/ifcvf/ifcvf_main.c > index 7c8bbfcf6c3e..f5e6a90d8114 100644 > --- a/drivers/vdpa/ifcvf/ifcvf_main.c > +++ b/drivers/vdpa/ifcvf/ifcvf_main.c > @@ -332,24 +332,32 @@ static u32 ifcvf_vdpa_get_vq_align(struct vdpa_device *vdpa_dev) > return IFCVF_QUEUE_ALIGNMENT; > } > > -static void ifcvf_vdpa_get_config(struct vdpa_device *vdpa_dev, > - unsigned int offset, > - void *buf, unsigned int len) > +static int ifcvf_vdpa_get_config(struct vdpa_device *vdpa_dev, > + unsigned int offset, > + void *buf, unsigned int len) > { > struct ifcvf_hw *vf = vdpa_to_vf(vdpa_dev); > > - WARN_ON(offset + len > sizeof(struct virtio_net_config)); > + if (offset + len > sizeof(struct virtio_net_config)) > + return -EINVAL; > + > ifcvf_read_net_config(vf, offset, buf, len); > + > + return 0; > } > > -static void ifcvf_vdpa_set_config(struct vdpa_device *vdpa_dev, > - unsigned int offset, const void *buf, > - unsigned int len) > +static int ifcvf_vdpa_set_config(struct vdpa_device *vdpa_dev, > + unsigned int offset, const void *buf, > + unsigned int len) > { > struct ifcvf_hw *vf = vdpa_to_vf(vdpa_dev); > > - WARN_ON(offset + len > sizeof(struct virtio_net_config)); > + if (offset + len > sizeof(struct virtio_net_config)) > + return -EINVAL; > + > ifcvf_write_net_config(vf, offset, buf, len); > + > + return 0; > } > > static void ifcvf_vdpa_set_config_cb(struct vdpa_device *vdpa_dev, > diff --git a/drivers/vdpa/mlx5/net/mlx5_vnet.c b/drivers/vdpa/mlx5/net/mlx5_vnet.c > index 029822060017..9323b5ff7988 100644 > --- a/drivers/vdpa/mlx5/net/mlx5_vnet.c > +++ b/drivers/vdpa/mlx5/net/mlx5_vnet.c > @@ -1796,20 +1796,25 @@ static void mlx5_vdpa_set_status(struct vdpa_device *vdev, u8 status) > ndev->mvdev.status |= VIRTIO_CONFIG_S_FAILED; > } > > -static void mlx5_vdpa_get_config(struct vdpa_device *vdev, unsigned int offset, void *buf, > - unsigned int len) > +static int mlx5_vdpa_get_config(struct vdpa_device *vdev, unsigned int offset, void *buf, > + unsigned int len) > { > struct mlx5_vdpa_dev *mvdev = to_mvdev(vdev); > struct mlx5_vdpa_net *ndev = to_mlx5_vdpa_ndev(mvdev); > > - if (offset + len < sizeof(struct virtio_net_config)) > - memcpy(buf, (u8 *)&ndev->config + offset, len); > + if (offset + len > sizeof(struct virtio_net_config)) > + return -EINVAL; It looks to me we should use ">=" here? Thanks > + > + memcpy(buf, (u8 *)&ndev->config + offset, len); > + > + return 0 > } > > -static void mlx5_vdpa_set_config(struct vdpa_device *vdev, unsigned int offset, const void *buf, > - unsigned int len) > +static int mlx5_vdpa_set_config(struct vdpa_device *vdev, unsigned int offset, const void *buf, > + unsigned int len) > { > /* not supported */ > + return 0; > } > > static u32 mlx5_vdpa_get_generation(struct vdpa_device *vdev) > diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim.c b/drivers/vdpa/vdpa_sim/vdpa_sim.c > index a7aeb5d01c3e..3808b01ac703 100644 > --- a/drivers/vdpa/vdpa_sim/vdpa_sim.c > +++ b/drivers/vdpa/vdpa_sim/vdpa_sim.c > @@ -462,32 +462,36 @@ static void vdpasim_set_status(struct vdpa_device *vdpa, u8 status) > spin_unlock(&vdpasim->lock); > } > > -static void vdpasim_get_config(struct vdpa_device *vdpa, unsigned int offset, > - void *buf, unsigned int len) > +static int vdpasim_get_config(struct vdpa_device *vdpa, unsigned int offset, > + void *buf, unsigned int len) > { > struct vdpasim *vdpasim = vdpa_to_sim(vdpa); > > if (offset + len > vdpasim->dev_attr.config_size) > - return; > + return -EINVAL; > > if (vdpasim->dev_attr.get_config) > vdpasim->dev_attr.get_config(vdpasim, vdpasim->config); > > memcpy(buf, vdpasim->config + offset, len); > + > + return 0; > } > > -static void vdpasim_set_config(struct vdpa_device *vdpa, unsigned int offset, > - const void *buf, unsigned int len) > +static int vdpasim_set_config(struct vdpa_device *vdpa, unsigned int offset, > + const void *buf, unsigned int len) > { > struct vdpasim *vdpasim = vdpa_to_sim(vdpa); > > if (offset + len > vdpasim->dev_attr.config_size) > - return; > + return -EINVAL; > > memcpy(vdpasim->config + offset, buf, len); > > if (vdpasim->dev_attr.set_config) > vdpasim->dev_attr.set_config(vdpasim, vdpasim->config); > + > + return 0; > } > > static u32 vdpasim_get_generation(struct vdpa_device *vdpa)
Adding Eli in the loop. On Fri, Feb 05, 2021 at 11:20:11AM +0800, Jason Wang wrote: > >On 2021/2/5 上午1:22, Stefano Garzarella wrote: >>All implementations of these callbacks already validate inputs. >> >>Let's return an error from these callbacks, so the caller doesn't >>need to validate the input anymore. >> >>We update all implementations to return -EINVAL in case of invalid >>input. >> >>Signed-off-by: Stefano Garzarella <sgarzare@redhat.com> >>--- >> include/linux/vdpa.h | 18 ++++++++++-------- >> drivers/vdpa/ifcvf/ifcvf_main.c | 24 ++++++++++++++++-------- >> drivers/vdpa/mlx5/net/mlx5_vnet.c | 17 +++++++++++------ >> drivers/vdpa/vdpa_sim/vdpa_sim.c | 16 ++++++++++------ >> 4 files changed, 47 insertions(+), 28 deletions(-) >> >>diff --git a/include/linux/vdpa.h b/include/linux/vdpa.h >>index 4ab5494503a8..0e0cbd5fb41b 100644 >>--- a/include/linux/vdpa.h >>+++ b/include/linux/vdpa.h >>@@ -157,6 +157,7 @@ struct vdpa_iova_range { >> * @buf: buffer used to read to >> * @len: the length to read from >> * configuration space >>+ * Returns integer: success (0) or error (< 0) >> * @set_config: Write to device specific configuration space >> * @vdev: vdpa device >> * @offset: offset from the beginning of >>@@ -164,6 +165,7 @@ struct vdpa_iova_range { >> * @buf: buffer used to write from >> * @len: the length to write to >> * configuration space >>+ * Returns integer: success (0) or error (< 0) >> * @get_generation: Get device config generation (optional) >> * @vdev: vdpa device >> * Returns u32: device generation >>@@ -231,10 +233,10 @@ struct vdpa_config_ops { >> u32 (*get_vendor_id)(struct vdpa_device *vdev); >> u8 (*get_status)(struct vdpa_device *vdev); >> void (*set_status)(struct vdpa_device *vdev, u8 status); >>- void (*get_config)(struct vdpa_device *vdev, unsigned int offset, >>- void *buf, unsigned int len); >>- void (*set_config)(struct vdpa_device *vdev, unsigned int offset, >>- const void *buf, unsigned int len); >>+ int (*get_config)(struct vdpa_device *vdev, unsigned int offset, >>+ void *buf, unsigned int len); >>+ int (*set_config)(struct vdpa_device *vdev, unsigned int offset, >>+ const void *buf, unsigned int len); >> u32 (*get_generation)(struct vdpa_device *vdev); >> struct vdpa_iova_range (*get_iova_range)(struct vdpa_device *vdev); >>@@ -329,8 +331,8 @@ static inline int vdpa_set_features(struct vdpa_device *vdev, u64 features) >> } >>-static inline void vdpa_get_config(struct vdpa_device *vdev, unsigned offset, >>- void *buf, unsigned int len) >>+static inline int vdpa_get_config(struct vdpa_device *vdev, unsigned offset, >>+ void *buf, unsigned int len) >> { >> const struct vdpa_config_ops *ops = vdev->config; >>@@ -339,8 +341,8 @@ static inline void vdpa_get_config(struct vdpa_device *vdev, unsigned offset, >> * If it does happen we assume a legacy guest. >> */ >> if (!vdev->features_valid) >>- vdpa_set_features(vdev, 0); >>- ops->get_config(vdev, offset, buf, len); >>+ return vdpa_set_features(vdev, 0); >>+ return ops->get_config(vdev, offset, buf, len); >> } >> /** >>diff --git a/drivers/vdpa/ifcvf/ifcvf_main.c b/drivers/vdpa/ifcvf/ifcvf_main.c >>index 7c8bbfcf6c3e..f5e6a90d8114 100644 >>--- a/drivers/vdpa/ifcvf/ifcvf_main.c >>+++ b/drivers/vdpa/ifcvf/ifcvf_main.c >>@@ -332,24 +332,32 @@ static u32 ifcvf_vdpa_get_vq_align(struct vdpa_device *vdpa_dev) >> return IFCVF_QUEUE_ALIGNMENT; >> } >>-static void ifcvf_vdpa_get_config(struct vdpa_device *vdpa_dev, >>- unsigned int offset, >>- void *buf, unsigned int len) >>+static int ifcvf_vdpa_get_config(struct vdpa_device *vdpa_dev, >>+ unsigned int offset, >>+ void *buf, unsigned int len) >> { >> struct ifcvf_hw *vf = vdpa_to_vf(vdpa_dev); >>- WARN_ON(offset + len > sizeof(struct virtio_net_config)); >>+ if (offset + len > sizeof(struct virtio_net_config)) >>+ return -EINVAL; >>+ >> ifcvf_read_net_config(vf, offset, buf, len); >>+ >>+ return 0; >> } >>-static void ifcvf_vdpa_set_config(struct vdpa_device *vdpa_dev, >>- unsigned int offset, const void *buf, >>- unsigned int len) >>+static int ifcvf_vdpa_set_config(struct vdpa_device *vdpa_dev, >>+ unsigned int offset, const void *buf, >>+ unsigned int len) >> { >> struct ifcvf_hw *vf = vdpa_to_vf(vdpa_dev); >>- WARN_ON(offset + len > sizeof(struct virtio_net_config)); >>+ if (offset + len > sizeof(struct virtio_net_config)) >>+ return -EINVAL; >>+ >> ifcvf_write_net_config(vf, offset, buf, len); >>+ >>+ return 0; >> } >> static void ifcvf_vdpa_set_config_cb(struct vdpa_device *vdpa_dev, >>diff --git a/drivers/vdpa/mlx5/net/mlx5_vnet.c b/drivers/vdpa/mlx5/net/mlx5_vnet.c >>index 029822060017..9323b5ff7988 100644 >>--- a/drivers/vdpa/mlx5/net/mlx5_vnet.c >>+++ b/drivers/vdpa/mlx5/net/mlx5_vnet.c >>@@ -1796,20 +1796,25 @@ static void mlx5_vdpa_set_status(struct vdpa_device *vdev, u8 status) >> ndev->mvdev.status |= VIRTIO_CONFIG_S_FAILED; >> } >>-static void mlx5_vdpa_get_config(struct vdpa_device *vdev, unsigned int offset, void *buf, >>- unsigned int len) >>+static int mlx5_vdpa_get_config(struct vdpa_device *vdev, unsigned int offset, void *buf, >>+ unsigned int len) >> { >> struct mlx5_vdpa_dev *mvdev = to_mvdev(vdev); >> struct mlx5_vdpa_net *ndev = to_mlx5_vdpa_ndev(mvdev); >>- if (offset + len < sizeof(struct virtio_net_config)) >>- memcpy(buf, (u8 *)&ndev->config + offset, len); >>+ if (offset + len > sizeof(struct virtio_net_config)) >>+ return -EINVAL; > > >It looks to me we should use ">=" here? Ehmm, I think it was wrong before this patch. If 'offset + len' is equal to 'sizeof(struct virtio_net_config)', should be okay to copy, no? I think it's one of the rare cases where the copy and paste went well :-) Should I fix this in a separate patch? Thanks, Stefano
On Fri, Feb 05, 2021 at 09:48:47AM +0100, Stefano Garzarella wrote: > Adding Eli in the loop. > > On Fri, Feb 05, 2021 at 11:20:11AM +0800, Jason Wang wrote: > > > > On 2021/2/5 上午1:22, Stefano Garzarella wrote: > > > All implementations of these callbacks already validate inputs. > > > > > > Let's return an error from these callbacks, so the caller doesn't > > > need to validate the input anymore. > > > > > > We update all implementations to return -EINVAL in case of invalid > > > input. > > > > > > Signed-off-by: Stefano Garzarella <sgarzare@redhat.com> > > > --- > > > include/linux/vdpa.h | 18 ++++++++++-------- > > > drivers/vdpa/ifcvf/ifcvf_main.c | 24 ++++++++++++++++-------- > > > drivers/vdpa/mlx5/net/mlx5_vnet.c | 17 +++++++++++------ > > > drivers/vdpa/vdpa_sim/vdpa_sim.c | 16 ++++++++++------ > > > 4 files changed, 47 insertions(+), 28 deletions(-) > > > > > > diff --git a/include/linux/vdpa.h b/include/linux/vdpa.h > > > index 4ab5494503a8..0e0cbd5fb41b 100644 > > > --- a/include/linux/vdpa.h > > > +++ b/include/linux/vdpa.h > > > @@ -157,6 +157,7 @@ struct vdpa_iova_range { > > > * @buf: buffer used to read to > > > * @len: the length to read from > > > * configuration space > > > + * Returns integer: success (0) or error (< 0) > > > * @set_config: Write to device specific configuration space > > > * @vdev: vdpa device > > > * @offset: offset from the beginning of > > > @@ -164,6 +165,7 @@ struct vdpa_iova_range { > > > * @buf: buffer used to write from > > > * @len: the length to write to > > > * configuration space > > > + * Returns integer: success (0) or error (< 0) > > > * @get_generation: Get device config generation (optional) > > > * @vdev: vdpa device > > > * Returns u32: device generation > > > @@ -231,10 +233,10 @@ struct vdpa_config_ops { > > > u32 (*get_vendor_id)(struct vdpa_device *vdev); > > > u8 (*get_status)(struct vdpa_device *vdev); > > > void (*set_status)(struct vdpa_device *vdev, u8 status); > > > - void (*get_config)(struct vdpa_device *vdev, unsigned int offset, > > > - void *buf, unsigned int len); > > > - void (*set_config)(struct vdpa_device *vdev, unsigned int offset, > > > - const void *buf, unsigned int len); > > > + int (*get_config)(struct vdpa_device *vdev, unsigned int offset, > > > + void *buf, unsigned int len); > > > + int (*set_config)(struct vdpa_device *vdev, unsigned int offset, > > > + const void *buf, unsigned int len); > > > u32 (*get_generation)(struct vdpa_device *vdev); > > > struct vdpa_iova_range (*get_iova_range)(struct vdpa_device *vdev); > > > @@ -329,8 +331,8 @@ static inline int vdpa_set_features(struct vdpa_device *vdev, u64 features) > > > } > > > -static inline void vdpa_get_config(struct vdpa_device *vdev, unsigned offset, > > > - void *buf, unsigned int len) > > > +static inline int vdpa_get_config(struct vdpa_device *vdev, unsigned offset, > > > + void *buf, unsigned int len) > > > { > > > const struct vdpa_config_ops *ops = vdev->config; > > > @@ -339,8 +341,8 @@ static inline void vdpa_get_config(struct vdpa_device *vdev, unsigned offset, > > > * If it does happen we assume a legacy guest. > > > */ > > > if (!vdev->features_valid) > > > - vdpa_set_features(vdev, 0); > > > - ops->get_config(vdev, offset, buf, len); > > > + return vdpa_set_features(vdev, 0); > > > + return ops->get_config(vdev, offset, buf, len); > > > } > > > /** > > > diff --git a/drivers/vdpa/ifcvf/ifcvf_main.c b/drivers/vdpa/ifcvf/ifcvf_main.c > > > index 7c8bbfcf6c3e..f5e6a90d8114 100644 > > > --- a/drivers/vdpa/ifcvf/ifcvf_main.c > > > +++ b/drivers/vdpa/ifcvf/ifcvf_main.c > > > @@ -332,24 +332,32 @@ static u32 ifcvf_vdpa_get_vq_align(struct vdpa_device *vdpa_dev) > > > return IFCVF_QUEUE_ALIGNMENT; > > > } > > > -static void ifcvf_vdpa_get_config(struct vdpa_device *vdpa_dev, > > > - unsigned int offset, > > > - void *buf, unsigned int len) > > > +static int ifcvf_vdpa_get_config(struct vdpa_device *vdpa_dev, > > > + unsigned int offset, > > > + void *buf, unsigned int len) > > > { > > > struct ifcvf_hw *vf = vdpa_to_vf(vdpa_dev); > > > - WARN_ON(offset + len > sizeof(struct virtio_net_config)); > > > + if (offset + len > sizeof(struct virtio_net_config)) > > > + return -EINVAL; > > > + > > > ifcvf_read_net_config(vf, offset, buf, len); > > > + > > > + return 0; > > > } > > > -static void ifcvf_vdpa_set_config(struct vdpa_device *vdpa_dev, > > > - unsigned int offset, const void *buf, > > > - unsigned int len) > > > +static int ifcvf_vdpa_set_config(struct vdpa_device *vdpa_dev, > > > + unsigned int offset, const void *buf, > > > + unsigned int len) > > > { > > > struct ifcvf_hw *vf = vdpa_to_vf(vdpa_dev); > > > - WARN_ON(offset + len > sizeof(struct virtio_net_config)); > > > + if (offset + len > sizeof(struct virtio_net_config)) > > > + return -EINVAL; > > > + > > > ifcvf_write_net_config(vf, offset, buf, len); > > > + > > > + return 0; > > > } > > > static void ifcvf_vdpa_set_config_cb(struct vdpa_device *vdpa_dev, > > > diff --git a/drivers/vdpa/mlx5/net/mlx5_vnet.c b/drivers/vdpa/mlx5/net/mlx5_vnet.c > > > index 029822060017..9323b5ff7988 100644 > > > --- a/drivers/vdpa/mlx5/net/mlx5_vnet.c > > > +++ b/drivers/vdpa/mlx5/net/mlx5_vnet.c > > > @@ -1796,20 +1796,25 @@ static void mlx5_vdpa_set_status(struct vdpa_device *vdev, u8 status) > > > ndev->mvdev.status |= VIRTIO_CONFIG_S_FAILED; > > > } > > > -static void mlx5_vdpa_get_config(struct vdpa_device *vdev, unsigned int offset, void *buf, > > > - unsigned int len) > > > +static int mlx5_vdpa_get_config(struct vdpa_device *vdev, unsigned int offset, void *buf, > > > + unsigned int len) > > > { > > > struct mlx5_vdpa_dev *mvdev = to_mvdev(vdev); > > > struct mlx5_vdpa_net *ndev = to_mlx5_vdpa_ndev(mvdev); > > > - if (offset + len < sizeof(struct virtio_net_config)) > > > - memcpy(buf, (u8 *)&ndev->config + offset, len); > > > + if (offset + len > sizeof(struct virtio_net_config)) > > > + return -EINVAL; > > > > > > It looks to me we should use ">=" here? > > > Ehmm, I think it was wrong before this patch. If 'offset + len' is equal to > 'sizeof(struct virtio_net_config)', should be okay to copy, no? > > I think it's one of the rare cases where the copy and paste went well :-) > > Should I fix this in a separate patch? > > Thanks, > Stefano Sure.
On Fri, Feb 05, 2021 at 09:11:26AM -0500, Michael S. Tsirkin wrote: >On Fri, Feb 05, 2021 at 09:48:47AM +0100, Stefano Garzarella wrote: >> Adding Eli in the loop. >> >> On Fri, Feb 05, 2021 at 11:20:11AM +0800, Jason Wang wrote: >> > >> > On 2021/2/5 上午1:22, Stefano Garzarella wrote: >> > > All implementations of these callbacks already validate inputs. >> > > >> > > Let's return an error from these callbacks, so the caller doesn't >> > > need to validate the input anymore. >> > > >> > > We update all implementations to return -EINVAL in case of invalid >> > > input. >> > > >> > > Signed-off-by: Stefano Garzarella <sgarzare@redhat.com> >> > > --- >> > > include/linux/vdpa.h | 18 ++++++++++-------- >> > > drivers/vdpa/ifcvf/ifcvf_main.c | 24 ++++++++++++++++-------- >> > > drivers/vdpa/mlx5/net/mlx5_vnet.c | 17 +++++++++++------ >> > > drivers/vdpa/vdpa_sim/vdpa_sim.c | 16 ++++++++++------ >> > > 4 files changed, 47 insertions(+), 28 deletions(-) >> > > >> > > diff --git a/include/linux/vdpa.h b/include/linux/vdpa.h >> > > index 4ab5494503a8..0e0cbd5fb41b 100644 >> > > --- a/include/linux/vdpa.h >> > > +++ b/include/linux/vdpa.h >> > > @@ -157,6 +157,7 @@ struct vdpa_iova_range { >> > > * @buf: buffer used to read to >> > > * @len: the length to read from >> > > * configuration space >> > > + * Returns integer: success (0) or error (< 0) >> > > * @set_config: Write to device specific configuration space >> > > * @vdev: vdpa device >> > > * @offset: offset from the beginning of >> > > @@ -164,6 +165,7 @@ struct vdpa_iova_range { >> > > * @buf: buffer used to write from >> > > * @len: the length to write to >> > > * configuration space >> > > + * Returns integer: success (0) or error (< 0) >> > > * @get_generation: Get device config generation (optional) >> > > * @vdev: vdpa device >> > > * Returns u32: device generation >> > > @@ -231,10 +233,10 @@ struct vdpa_config_ops { >> > > u32 (*get_vendor_id)(struct vdpa_device *vdev); >> > > u8 (*get_status)(struct vdpa_device *vdev); >> > > void (*set_status)(struct vdpa_device *vdev, u8 status); >> > > - void (*get_config)(struct vdpa_device *vdev, unsigned int offset, >> > > - void *buf, unsigned int len); >> > > - void (*set_config)(struct vdpa_device *vdev, unsigned int offset, >> > > - const void *buf, unsigned int len); >> > > + int (*get_config)(struct vdpa_device *vdev, unsigned int offset, >> > > + void *buf, unsigned int len); >> > > + int (*set_config)(struct vdpa_device *vdev, unsigned int offset, >> > > + const void *buf, unsigned int len); >> > > u32 (*get_generation)(struct vdpa_device *vdev); >> > > struct vdpa_iova_range (*get_iova_range)(struct vdpa_device *vdev); >> > > @@ -329,8 +331,8 @@ static inline int vdpa_set_features(struct vdpa_device *vdev, u64 features) >> > > } >> > > -static inline void vdpa_get_config(struct vdpa_device *vdev, unsigned offset, >> > > - void *buf, unsigned int len) >> > > +static inline int vdpa_get_config(struct vdpa_device *vdev, unsigned offset, >> > > + void *buf, unsigned int len) >> > > { >> > > const struct vdpa_config_ops *ops = vdev->config; >> > > @@ -339,8 +341,8 @@ static inline void vdpa_get_config(struct vdpa_device *vdev, unsigned offset, >> > > * If it does happen we assume a legacy guest. >> > > */ >> > > if (!vdev->features_valid) >> > > - vdpa_set_features(vdev, 0); >> > > - ops->get_config(vdev, offset, buf, len); >> > > + return vdpa_set_features(vdev, 0); >> > > + return ops->get_config(vdev, offset, buf, len); >> > > } >> > > /** >> > > diff --git a/drivers/vdpa/ifcvf/ifcvf_main.c b/drivers/vdpa/ifcvf/ifcvf_main.c >> > > index 7c8bbfcf6c3e..f5e6a90d8114 100644 >> > > --- a/drivers/vdpa/ifcvf/ifcvf_main.c >> > > +++ b/drivers/vdpa/ifcvf/ifcvf_main.c >> > > @@ -332,24 +332,32 @@ static u32 ifcvf_vdpa_get_vq_align(struct vdpa_device *vdpa_dev) >> > > return IFCVF_QUEUE_ALIGNMENT; >> > > } >> > > -static void ifcvf_vdpa_get_config(struct vdpa_device *vdpa_dev, >> > > - unsigned int offset, >> > > - void *buf, unsigned int len) >> > > +static int ifcvf_vdpa_get_config(struct vdpa_device *vdpa_dev, >> > > + unsigned int offset, >> > > + void *buf, unsigned int len) >> > > { >> > > struct ifcvf_hw *vf = vdpa_to_vf(vdpa_dev); >> > > - WARN_ON(offset + len > sizeof(struct virtio_net_config)); >> > > + if (offset + len > sizeof(struct virtio_net_config)) >> > > + return -EINVAL; >> > > + >> > > ifcvf_read_net_config(vf, offset, buf, len); >> > > + >> > > + return 0; >> > > } >> > > -static void ifcvf_vdpa_set_config(struct vdpa_device *vdpa_dev, >> > > - unsigned int offset, const void *buf, >> > > - unsigned int len) >> > > +static int ifcvf_vdpa_set_config(struct vdpa_device *vdpa_dev, >> > > + unsigned int offset, const void *buf, >> > > + unsigned int len) >> > > { >> > > struct ifcvf_hw *vf = vdpa_to_vf(vdpa_dev); >> > > - WARN_ON(offset + len > sizeof(struct virtio_net_config)); >> > > + if (offset + len > sizeof(struct virtio_net_config)) >> > > + return -EINVAL; >> > > + >> > > ifcvf_write_net_config(vf, offset, buf, len); >> > > + >> > > + return 0; >> > > } >> > > static void ifcvf_vdpa_set_config_cb(struct vdpa_device *vdpa_dev, >> > > diff --git a/drivers/vdpa/mlx5/net/mlx5_vnet.c b/drivers/vdpa/mlx5/net/mlx5_vnet.c >> > > index 029822060017..9323b5ff7988 100644 >> > > --- a/drivers/vdpa/mlx5/net/mlx5_vnet.c >> > > +++ b/drivers/vdpa/mlx5/net/mlx5_vnet.c >> > > @@ -1796,20 +1796,25 @@ static void mlx5_vdpa_set_status(struct vdpa_device *vdev, u8 status) >> > > ndev->mvdev.status |= VIRTIO_CONFIG_S_FAILED; >> > > } >> > > -static void mlx5_vdpa_get_config(struct vdpa_device *vdev, unsigned int offset, void *buf, >> > > - unsigned int len) >> > > +static int mlx5_vdpa_get_config(struct vdpa_device *vdev, unsigned int offset, void *buf, >> > > + unsigned int len) >> > > { >> > > struct mlx5_vdpa_dev *mvdev = to_mvdev(vdev); >> > > struct mlx5_vdpa_net *ndev = to_mlx5_vdpa_ndev(mvdev); >> > > - if (offset + len < sizeof(struct virtio_net_config)) >> > > - memcpy(buf, (u8 *)&ndev->config + offset, len); >> > > + if (offset + len > sizeof(struct virtio_net_config)) >> > > + return -EINVAL; >> > >> > >> > It looks to me we should use ">=" here? >> >> >> Ehmm, I think it was wrong before this patch. If 'offset + len' is equal to >> 'sizeof(struct virtio_net_config)', should be okay to copy, no? >> >> I think it's one of the rare cases where the copy and paste went well :-) >> >> Should I fix this in a separate patch? >> >> Thanks, >> Stefano > >Sure. > I'll do it. Thanks, Stefano
diff --git a/include/linux/vdpa.h b/include/linux/vdpa.h index 4ab5494503a8..0e0cbd5fb41b 100644 --- a/include/linux/vdpa.h +++ b/include/linux/vdpa.h @@ -157,6 +157,7 @@ struct vdpa_iova_range { * @buf: buffer used to read to * @len: the length to read from * configuration space + * Returns integer: success (0) or error (< 0) * @set_config: Write to device specific configuration space * @vdev: vdpa device * @offset: offset from the beginning of @@ -164,6 +165,7 @@ struct vdpa_iova_range { * @buf: buffer used to write from * @len: the length to write to * configuration space + * Returns integer: success (0) or error (< 0) * @get_generation: Get device config generation (optional) * @vdev: vdpa device * Returns u32: device generation @@ -231,10 +233,10 @@ struct vdpa_config_ops { u32 (*get_vendor_id)(struct vdpa_device *vdev); u8 (*get_status)(struct vdpa_device *vdev); void (*set_status)(struct vdpa_device *vdev, u8 status); - void (*get_config)(struct vdpa_device *vdev, unsigned int offset, - void *buf, unsigned int len); - void (*set_config)(struct vdpa_device *vdev, unsigned int offset, - const void *buf, unsigned int len); + int (*get_config)(struct vdpa_device *vdev, unsigned int offset, + void *buf, unsigned int len); + int (*set_config)(struct vdpa_device *vdev, unsigned int offset, + const void *buf, unsigned int len); u32 (*get_generation)(struct vdpa_device *vdev); struct vdpa_iova_range (*get_iova_range)(struct vdpa_device *vdev); @@ -329,8 +331,8 @@ static inline int vdpa_set_features(struct vdpa_device *vdev, u64 features) } -static inline void vdpa_get_config(struct vdpa_device *vdev, unsigned offset, - void *buf, unsigned int len) +static inline int vdpa_get_config(struct vdpa_device *vdev, unsigned offset, + void *buf, unsigned int len) { const struct vdpa_config_ops *ops = vdev->config; @@ -339,8 +341,8 @@ static inline void vdpa_get_config(struct vdpa_device *vdev, unsigned offset, * If it does happen we assume a legacy guest. */ if (!vdev->features_valid) - vdpa_set_features(vdev, 0); - ops->get_config(vdev, offset, buf, len); + return vdpa_set_features(vdev, 0); + return ops->get_config(vdev, offset, buf, len); } /** diff --git a/drivers/vdpa/ifcvf/ifcvf_main.c b/drivers/vdpa/ifcvf/ifcvf_main.c index 7c8bbfcf6c3e..f5e6a90d8114 100644 --- a/drivers/vdpa/ifcvf/ifcvf_main.c +++ b/drivers/vdpa/ifcvf/ifcvf_main.c @@ -332,24 +332,32 @@ static u32 ifcvf_vdpa_get_vq_align(struct vdpa_device *vdpa_dev) return IFCVF_QUEUE_ALIGNMENT; } -static void ifcvf_vdpa_get_config(struct vdpa_device *vdpa_dev, - unsigned int offset, - void *buf, unsigned int len) +static int ifcvf_vdpa_get_config(struct vdpa_device *vdpa_dev, + unsigned int offset, + void *buf, unsigned int len) { struct ifcvf_hw *vf = vdpa_to_vf(vdpa_dev); - WARN_ON(offset + len > sizeof(struct virtio_net_config)); + if (offset + len > sizeof(struct virtio_net_config)) + return -EINVAL; + ifcvf_read_net_config(vf, offset, buf, len); + + return 0; } -static void ifcvf_vdpa_set_config(struct vdpa_device *vdpa_dev, - unsigned int offset, const void *buf, - unsigned int len) +static int ifcvf_vdpa_set_config(struct vdpa_device *vdpa_dev, + unsigned int offset, const void *buf, + unsigned int len) { struct ifcvf_hw *vf = vdpa_to_vf(vdpa_dev); - WARN_ON(offset + len > sizeof(struct virtio_net_config)); + if (offset + len > sizeof(struct virtio_net_config)) + return -EINVAL; + ifcvf_write_net_config(vf, offset, buf, len); + + return 0; } static void ifcvf_vdpa_set_config_cb(struct vdpa_device *vdpa_dev, diff --git a/drivers/vdpa/mlx5/net/mlx5_vnet.c b/drivers/vdpa/mlx5/net/mlx5_vnet.c index 029822060017..9323b5ff7988 100644 --- a/drivers/vdpa/mlx5/net/mlx5_vnet.c +++ b/drivers/vdpa/mlx5/net/mlx5_vnet.c @@ -1796,20 +1796,25 @@ static void mlx5_vdpa_set_status(struct vdpa_device *vdev, u8 status) ndev->mvdev.status |= VIRTIO_CONFIG_S_FAILED; } -static void mlx5_vdpa_get_config(struct vdpa_device *vdev, unsigned int offset, void *buf, - unsigned int len) +static int mlx5_vdpa_get_config(struct vdpa_device *vdev, unsigned int offset, void *buf, + unsigned int len) { struct mlx5_vdpa_dev *mvdev = to_mvdev(vdev); struct mlx5_vdpa_net *ndev = to_mlx5_vdpa_ndev(mvdev); - if (offset + len < sizeof(struct virtio_net_config)) - memcpy(buf, (u8 *)&ndev->config + offset, len); + if (offset + len > sizeof(struct virtio_net_config)) + return -EINVAL; + + memcpy(buf, (u8 *)&ndev->config + offset, len); + + return 0 } -static void mlx5_vdpa_set_config(struct vdpa_device *vdev, unsigned int offset, const void *buf, - unsigned int len) +static int mlx5_vdpa_set_config(struct vdpa_device *vdev, unsigned int offset, const void *buf, + unsigned int len) { /* not supported */ + return 0; } static u32 mlx5_vdpa_get_generation(struct vdpa_device *vdev) diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim.c b/drivers/vdpa/vdpa_sim/vdpa_sim.c index a7aeb5d01c3e..3808b01ac703 100644 --- a/drivers/vdpa/vdpa_sim/vdpa_sim.c +++ b/drivers/vdpa/vdpa_sim/vdpa_sim.c @@ -462,32 +462,36 @@ static void vdpasim_set_status(struct vdpa_device *vdpa, u8 status) spin_unlock(&vdpasim->lock); } -static void vdpasim_get_config(struct vdpa_device *vdpa, unsigned int offset, - void *buf, unsigned int len) +static int vdpasim_get_config(struct vdpa_device *vdpa, unsigned int offset, + void *buf, unsigned int len) { struct vdpasim *vdpasim = vdpa_to_sim(vdpa); if (offset + len > vdpasim->dev_attr.config_size) - return; + return -EINVAL; if (vdpasim->dev_attr.get_config) vdpasim->dev_attr.get_config(vdpasim, vdpasim->config); memcpy(buf, vdpasim->config + offset, len); + + return 0; } -static void vdpasim_set_config(struct vdpa_device *vdpa, unsigned int offset, - const void *buf, unsigned int len) +static int vdpasim_set_config(struct vdpa_device *vdpa, unsigned int offset, + const void *buf, unsigned int len) { struct vdpasim *vdpasim = vdpa_to_sim(vdpa); if (offset + len > vdpasim->dev_attr.config_size) - return; + return -EINVAL; memcpy(vdpasim->config + offset, buf, len); if (vdpasim->dev_attr.set_config) vdpasim->dev_attr.set_config(vdpasim, vdpasim->config); + + return 0; } static u32 vdpasim_get_generation(struct vdpa_device *vdpa)
All implementations of these callbacks already validate inputs. Let's return an error from these callbacks, so the caller doesn't need to validate the input anymore. We update all implementations to return -EINVAL in case of invalid input. Signed-off-by: Stefano Garzarella <sgarzare@redhat.com> --- include/linux/vdpa.h | 18 ++++++++++-------- drivers/vdpa/ifcvf/ifcvf_main.c | 24 ++++++++++++++++-------- drivers/vdpa/mlx5/net/mlx5_vnet.c | 17 +++++++++++------ drivers/vdpa/vdpa_sim/vdpa_sim.c | 16 ++++++++++------ 4 files changed, 47 insertions(+), 28 deletions(-)