2018-10-17 19:20:55

by Wenwen Wang

[permalink] [raw]
Subject: [PATCH v4] drivers/vfio: Fix a redundant copy bug

In vfio_spapr_iommu_eeh_ioctl(), if the ioctl command is VFIO_EEH_PE_OP,
the user-space buffer 'arg' is copied to the kernel object 'op' and the
'argsz' and 'flags' fields of 'op' are checked. If the check fails, an
error code EINVAL is returned. Otherwise, 'op.op' is further checked
through a switch statement to invoke related handlers. If 'op.op' is
VFIO_EEH_PE_INJECT_ERR, the whole user-space buffer 'arg' is copied again
to 'op' to obtain the err information. However, in the following execution
of this case, the fields of 'op', except the field 'err', are actually not
used. That is, the second copy has a redundant part. Therefore, for
performance consideration, the redundant part of the second copy should be
removed.

This patch removes such a part in the second copy. It only copies from
'err.type' to 'err.mask', which is exactly required by the
VFIO_EEH_PE_INJECT_ERR op.

Signed-off-by: Wenwen Wang <[email protected]>
---
drivers/vfio/vfio_spapr_eeh.c | 9 ++++++---
1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/drivers/vfio/vfio_spapr_eeh.c b/drivers/vfio/vfio_spapr_eeh.c
index 38edeb4..66634c6 100644
--- a/drivers/vfio/vfio_spapr_eeh.c
+++ b/drivers/vfio/vfio_spapr_eeh.c
@@ -37,6 +37,7 @@ long vfio_spapr_iommu_eeh_ioctl(struct iommu_group *group,
struct eeh_pe *pe;
struct vfio_eeh_pe_op op;
unsigned long minsz;
+ unsigned long start, end;
long ret = -EINVAL;

switch (cmd) {
@@ -86,10 +87,12 @@ long vfio_spapr_iommu_eeh_ioctl(struct iommu_group *group,
ret = eeh_pe_configure(pe);
break;
case VFIO_EEH_PE_INJECT_ERR:
- minsz = offsetofend(struct vfio_eeh_pe_op, err.mask);
- if (op.argsz < minsz)
+ start = offsetof(struct vfio_eeh_pe_op, err.type);
+ end = offsetofend(struct vfio_eeh_pe_op, err.mask);
+ if (op.argsz < end)
return -EINVAL;
- if (copy_from_user(&op, (void __user *)arg, minsz))
+ if (copy_from_user(&op.err, (char __user *)arg +
+ start, end - start))
return -EFAULT;

ret = eeh_pe_inject_err(pe, op.err.type, op.err.func,
--
2.7.4



2018-10-29 18:58:15

by Wenwen Wang

[permalink] [raw]
Subject: Re: [PATCH v4] drivers/vfio: Fix a redundant copy bug

Hello,

Could you please apply this patch? Thanks!

Wenwen

On Wed, Oct 17, 2018 at 2:18 PM Wenwen Wang <[email protected]> wrote:
>
> In vfio_spapr_iommu_eeh_ioctl(), if the ioctl command is VFIO_EEH_PE_OP,
> the user-space buffer 'arg' is copied to the kernel object 'op' and the
> 'argsz' and 'flags' fields of 'op' are checked. If the check fails, an
> error code EINVAL is returned. Otherwise, 'op.op' is further checked
> through a switch statement to invoke related handlers. If 'op.op' is
> VFIO_EEH_PE_INJECT_ERR, the whole user-space buffer 'arg' is copied again
> to 'op' to obtain the err information. However, in the following execution
> of this case, the fields of 'op', except the field 'err', are actually not
> used. That is, the second copy has a redundant part. Therefore, for
> performance consideration, the redundant part of the second copy should be
> removed.
>
> This patch removes such a part in the second copy. It only copies from
> 'err.type' to 'err.mask', which is exactly required by the
> VFIO_EEH_PE_INJECT_ERR op.
>
> Signed-off-by: Wenwen Wang <[email protected]>
> ---
> drivers/vfio/vfio_spapr_eeh.c | 9 ++++++---
> 1 file changed, 6 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/vfio/vfio_spapr_eeh.c b/drivers/vfio/vfio_spapr_eeh.c
> index 38edeb4..66634c6 100644
> --- a/drivers/vfio/vfio_spapr_eeh.c
> +++ b/drivers/vfio/vfio_spapr_eeh.c
> @@ -37,6 +37,7 @@ long vfio_spapr_iommu_eeh_ioctl(struct iommu_group *group,
> struct eeh_pe *pe;
> struct vfio_eeh_pe_op op;
> unsigned long minsz;
> + unsigned long start, end;
> long ret = -EINVAL;
>
> switch (cmd) {
> @@ -86,10 +87,12 @@ long vfio_spapr_iommu_eeh_ioctl(struct iommu_group *group,
> ret = eeh_pe_configure(pe);
> break;
> case VFIO_EEH_PE_INJECT_ERR:
> - minsz = offsetofend(struct vfio_eeh_pe_op, err.mask);
> - if (op.argsz < minsz)
> + start = offsetof(struct vfio_eeh_pe_op, err.type);
> + end = offsetofend(struct vfio_eeh_pe_op, err.mask);
> + if (op.argsz < end)
> return -EINVAL;
> - if (copy_from_user(&op, (void __user *)arg, minsz))
> + if (copy_from_user(&op.err, (char __user *)arg +
> + start, end - start))
> return -EFAULT;
>
> ret = eeh_pe_inject_err(pe, op.err.type, op.err.func,
> --
> 2.7.4
>

2018-10-29 21:33:23

by Alex Williamson

[permalink] [raw]
Subject: Re: [PATCH v4] drivers/vfio: Fix a redundant copy bug

On Mon, 29 Oct 2018 13:56:54 -0500
Wenwen Wang <[email protected]> wrote:

> Hello,
>
> Could you please apply this patch? Thanks!

I'd like to see testing and/or review from David or Alexey since I also
don't have an environment for spapr/eeh. We're already late into the
v4.20 merge window so this is probably v4.21 material. Thanks,

Alex

> On Wed, Oct 17, 2018 at 2:18 PM Wenwen Wang <[email protected]> wrote:
> >
> > In vfio_spapr_iommu_eeh_ioctl(), if the ioctl command is VFIO_EEH_PE_OP,
> > the user-space buffer 'arg' is copied to the kernel object 'op' and the
> > 'argsz' and 'flags' fields of 'op' are checked. If the check fails, an
> > error code EINVAL is returned. Otherwise, 'op.op' is further checked
> > through a switch statement to invoke related handlers. If 'op.op' is
> > VFIO_EEH_PE_INJECT_ERR, the whole user-space buffer 'arg' is copied again
> > to 'op' to obtain the err information. However, in the following execution
> > of this case, the fields of 'op', except the field 'err', are actually not
> > used. That is, the second copy has a redundant part. Therefore, for
> > performance consideration, the redundant part of the second copy should be
> > removed.
> >
> > This patch removes such a part in the second copy. It only copies from
> > 'err.type' to 'err.mask', which is exactly required by the
> > VFIO_EEH_PE_INJECT_ERR op.
> >
> > Signed-off-by: Wenwen Wang <[email protected]>
> > ---
> > drivers/vfio/vfio_spapr_eeh.c | 9 ++++++---
> > 1 file changed, 6 insertions(+), 3 deletions(-)
> >
> > diff --git a/drivers/vfio/vfio_spapr_eeh.c b/drivers/vfio/vfio_spapr_eeh.c
> > index 38edeb4..66634c6 100644
> > --- a/drivers/vfio/vfio_spapr_eeh.c
> > +++ b/drivers/vfio/vfio_spapr_eeh.c
> > @@ -37,6 +37,7 @@ long vfio_spapr_iommu_eeh_ioctl(struct iommu_group *group,
> > struct eeh_pe *pe;
> > struct vfio_eeh_pe_op op;
> > unsigned long minsz;
> > + unsigned long start, end;
> > long ret = -EINVAL;
> >
> > switch (cmd) {
> > @@ -86,10 +87,12 @@ long vfio_spapr_iommu_eeh_ioctl(struct iommu_group *group,
> > ret = eeh_pe_configure(pe);
> > break;
> > case VFIO_EEH_PE_INJECT_ERR:
> > - minsz = offsetofend(struct vfio_eeh_pe_op, err.mask);
> > - if (op.argsz < minsz)
> > + start = offsetof(struct vfio_eeh_pe_op, err.type);
> > + end = offsetofend(struct vfio_eeh_pe_op, err.mask);
> > + if (op.argsz < end)
> > return -EINVAL;
> > - if (copy_from_user(&op, (void __user *)arg, minsz))
> > + if (copy_from_user(&op.err, (char __user *)arg +
> > + start, end - start))
> > return -EFAULT;
> >
> > ret = eeh_pe_inject_err(pe, op.err.type, op.err.func,
> > --
> > 2.7.4
> >


2018-10-29 21:37:42

by Wenwen Wang

[permalink] [raw]
Subject: Re: [PATCH v4] drivers/vfio: Fix a redundant copy bug

On Mon, Oct 29, 2018 at 4:32 PM Alex Williamson
<[email protected]> wrote:
>
> On Mon, 29 Oct 2018 13:56:54 -0500
> Wenwen Wang <[email protected]> wrote:
>
> > Hello,
> >
> > Could you please apply this patch? Thanks!
>
> I'd like to see testing and/or review from David or Alexey since I also
> don't have an environment for spapr/eeh. We're already late into the
> v4.20 merge window so this is probably v4.21 material. Thanks,

I see. Thanks for your response, Alex!

> > On Wed, Oct 17, 2018 at 2:18 PM Wenwen Wang <[email protected]> wrote:
> > >
> > > In vfio_spapr_iommu_eeh_ioctl(), if the ioctl command is VFIO_EEH_PE_OP,
> > > the user-space buffer 'arg' is copied to the kernel object 'op' and the
> > > 'argsz' and 'flags' fields of 'op' are checked. If the check fails, an
> > > error code EINVAL is returned. Otherwise, 'op.op' is further checked
> > > through a switch statement to invoke related handlers. If 'op.op' is
> > > VFIO_EEH_PE_INJECT_ERR, the whole user-space buffer 'arg' is copied again
> > > to 'op' to obtain the err information. However, in the following execution
> > > of this case, the fields of 'op', except the field 'err', are actually not
> > > used. That is, the second copy has a redundant part. Therefore, for
> > > performance consideration, the redundant part of the second copy should be
> > > removed.
> > >
> > > This patch removes such a part in the second copy. It only copies from
> > > 'err.type' to 'err.mask', which is exactly required by the
> > > VFIO_EEH_PE_INJECT_ERR op.
> > >
> > > Signed-off-by: Wenwen Wang <[email protected]>
> > > ---
> > > drivers/vfio/vfio_spapr_eeh.c | 9 ++++++---
> > > 1 file changed, 6 insertions(+), 3 deletions(-)
> > >
> > > diff --git a/drivers/vfio/vfio_spapr_eeh.c b/drivers/vfio/vfio_spapr_eeh.c
> > > index 38edeb4..66634c6 100644
> > > --- a/drivers/vfio/vfio_spapr_eeh.c
> > > +++ b/drivers/vfio/vfio_spapr_eeh.c
> > > @@ -37,6 +37,7 @@ long vfio_spapr_iommu_eeh_ioctl(struct iommu_group *group,
> > > struct eeh_pe *pe;
> > > struct vfio_eeh_pe_op op;
> > > unsigned long minsz;
> > > + unsigned long start, end;
> > > long ret = -EINVAL;
> > >
> > > switch (cmd) {
> > > @@ -86,10 +87,12 @@ long vfio_spapr_iommu_eeh_ioctl(struct iommu_group *group,
> > > ret = eeh_pe_configure(pe);
> > > break;
> > > case VFIO_EEH_PE_INJECT_ERR:
> > > - minsz = offsetofend(struct vfio_eeh_pe_op, err.mask);
> > > - if (op.argsz < minsz)
> > > + start = offsetof(struct vfio_eeh_pe_op, err.type);
> > > + end = offsetofend(struct vfio_eeh_pe_op, err.mask);
> > > + if (op.argsz < end)
> > > return -EINVAL;
> > > - if (copy_from_user(&op, (void __user *)arg, minsz))
> > > + if (copy_from_user(&op.err, (char __user *)arg +
> > > + start, end - start))
> > > return -EFAULT;
> > >
> > > ret = eeh_pe_inject_err(pe, op.err.type, op.err.func,
> > > --
> > > 2.7.4
> > >
>

2018-11-19 06:42:39

by David Gibson

[permalink] [raw]
Subject: Re: [PATCH v4] drivers/vfio: Fix a redundant copy bug

On Mon, Oct 29, 2018 at 03:32:34PM -0600, Alex Williamson wrote:
> On Mon, 29 Oct 2018 13:56:54 -0500
> Wenwen Wang <[email protected]> wrote:
>
> > Hello,
> >
> > Could you please apply this patch? Thanks!
>
> I'd like to see testing and/or review from David or Alexey since I also
> don't have an environment for spapr/eeh. We're already late into the
> v4.20 merge window so this is probably v4.21 material. Thanks,

I didn't spot this for ages, since I wasn't CCed, I'm guessing the
same is true for Alexey.

TBH, I don't think it's worth bothering with this. It's a tiny amount
of extra work on a rare debug path. A couple of code lines simplicity
is worth more than a few bytes worth of redundant copy here.

Testing is.. difficult, since the EEH is already so broken it's hard
to know what to compare against. Sam Bobroff is already looking
medium-long term at a bunch of EEH rework, so it's quite possible this
will be rewritten then anyway.

>
> Alex
>
> > On Wed, Oct 17, 2018 at 2:18 PM Wenwen Wang <[email protected]> wrote:
> > >
> > > In vfio_spapr_iommu_eeh_ioctl(), if the ioctl command is VFIO_EEH_PE_OP,
> > > the user-space buffer 'arg' is copied to the kernel object 'op' and the
> > > 'argsz' and 'flags' fields of 'op' are checked. If the check fails, an
> > > error code EINVAL is returned. Otherwise, 'op.op' is further checked
> > > through a switch statement to invoke related handlers. If 'op.op' is
> > > VFIO_EEH_PE_INJECT_ERR, the whole user-space buffer 'arg' is copied again
> > > to 'op' to obtain the err information. However, in the following execution
> > > of this case, the fields of 'op', except the field 'err', are actually not
> > > used. That is, the second copy has a redundant part. Therefore, for
> > > performance consideration, the redundant part of the second copy should be
> > > removed.
> > >
> > > This patch removes such a part in the second copy. It only copies from
> > > 'err.type' to 'err.mask', which is exactly required by the
> > > VFIO_EEH_PE_INJECT_ERR op.
> > >
> > > Signed-off-by: Wenwen Wang <[email protected]>
> > > ---
> > > drivers/vfio/vfio_spapr_eeh.c | 9 ++++++---
> > > 1 file changed, 6 insertions(+), 3 deletions(-)
> > >
> > > diff --git a/drivers/vfio/vfio_spapr_eeh.c b/drivers/vfio/vfio_spapr_eeh.c
> > > index 38edeb4..66634c6 100644
> > > --- a/drivers/vfio/vfio_spapr_eeh.c
> > > +++ b/drivers/vfio/vfio_spapr_eeh.c
> > > @@ -37,6 +37,7 @@ long vfio_spapr_iommu_eeh_ioctl(struct iommu_group *group,
> > > struct eeh_pe *pe;
> > > struct vfio_eeh_pe_op op;
> > > unsigned long minsz;
> > > + unsigned long start, end;
> > > long ret = -EINVAL;
> > >
> > > switch (cmd) {
> > > @@ -86,10 +87,12 @@ long vfio_spapr_iommu_eeh_ioctl(struct iommu_group *group,
> > > ret = eeh_pe_configure(pe);
> > > break;
> > > case VFIO_EEH_PE_INJECT_ERR:
> > > - minsz = offsetofend(struct vfio_eeh_pe_op, err.mask);
> > > - if (op.argsz < minsz)
> > > + start = offsetof(struct vfio_eeh_pe_op, err.type);
> > > + end = offsetofend(struct vfio_eeh_pe_op, err.mask);
> > > + if (op.argsz < end)
> > > return -EINVAL;
> > > - if (copy_from_user(&op, (void __user *)arg, minsz))
> > > + if (copy_from_user(&op.err, (char __user *)arg +
> > > + start, end - start))
> > > return -EFAULT;
> > >
> > > ret = eeh_pe_inject_err(pe, op.err.type, op.err.func,
>

--
David Gibson | I'll have my music baroque, and my code
david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_
| _way_ _around_!
http://www.ozlabs.org/~dgibson


Attachments:
(No filename) (3.91 kB)
signature.asc (849.00 B)
Download all attachments