diff mbox

[1/2] IOMMU/MMU: Adjust top level functions for VT-d Device-TLB flush error.

Message ID 945CA011AD5F084CBEA3E851C0AB28894B86D176@SHSMSX101.ccr.corp.intel.com (mailing list archive)
State New, archived
Headers show

Commit Message

Quan Xu March 28, 2016, 3:33 a.m. UTC
On March 18, 2016 1:15am, <JBeulich@suse.com> wrote:
> >>> On 17.03.16 at 07:54, <quan.xu@intel.com> wrote:
> > @@ -53,11 +55,21 @@ static int device_power_down(void)
> >
> >      ioapic_suspend();
> >
> > -    iommu_suspend();
> > +    err = iommu_suspend();
> > +    if ( err )
> > +        goto iommu_suspend_error;
> >
> >      lapic_suspend();
> >
> >      return 0;
> > +
> > +iommu_suspend_error:
> 
> Labels indented by at least one space please.
> 

Good, I wasn't aware of it.


> > --- a/xen/arch/x86/mm/p2m-ept.c
> > +++ b/xen/arch/x86/mm/p2m-ept.c
> > @@ -830,7 +830,15 @@ out:
> >          {
> >              if ( iommu_flags )
> >                  for ( i = 0; i < (1 << order); i++ )
> > -                    iommu_map_page(d, gfn + i, mfn_x(mfn) + i,
> iommu_flags);
> > +                {
> > +                    rc = iommu_map_page(d, gfn + i, mfn_x(mfn) + i,
> iommu_flags);
> > +                    if ( rc )
> > +                    {
> > +                        while ( i-- > 0 )
> > +                            iommu_unmap_page(d, gfn + i);
> > +                        break;
> > +                    }
> > +                }
> >              else
> >                  for ( i = 0; i < (1 << order); i++ )
> >                      iommu_unmap_page(d, gfn + i);
> 
> Earlier on in the PV mm code you also checked iommu_unmap_page()'s return
> code - why not here (and also in p2m-pt.c)?
> 
> Also I'm quite unhappy about the inconsistent state you leave things
> in: You unmap from the IOMMU, return an error, but leave the EPT entry in
> place.
> 

As I mentioned for the abstract model,
     For iommu_{,un}map_page(), we'd better fix it as a normal error, as the error is not only from iommu flush, .e.g, '-ENOMEM'.
     So, we need to {,un}map from the IOMMU, return an error, and roll back the failed operation. 

For iommu_unmap_page, it is still under discussion in another thread. I think we could hold it on, waiting for another discussion.


> > --- a/xen/common/grant_table.c
> > +++ b/xen/common/grant_table.c
> > @@ -932,8 +932,9 @@ __gnttab_map_grant_ref(
> >              {
> >                  nr_gets++;
> >                  (void)get_page(pg, rd);
> > -                if ( !(op->flags & GNTMAP_readonly) )
> > -                    get_page_type(pg, PGT_writable_page);
> > +                if ( !(op->flags & GNTMAP_readonly) &&
> > +                     !get_page_type(pg, PGT_writable_page) )
> > +                        goto could_not_pin;
> 
> This needs explanation, as it doesn't look related to what your actual goal is: If
> an error was possible here, I think this would be a security issue. However, as
> also kind of documented by the explicitly ignored return value from get_page(),
> it is my understanding there here we only obtain an _extra_ reference.
> 

For this point, I inferred from:
map_vcpu_info()
{
...
    if ( !get_page_type(page, PGT_writable_page) )
    {
        put_page(page);
        return -EINVAL;
    }
...
}
, then for get_page_type(), I think the return value:
     0 -- error, 
     1-- right.

So if get_page_type() is failed, we should goto could_not_pin.

btw, there is another issue in the call path:
    iommu_{,un}map_page() -- __get_page_type() -- get_page_type()---


I tried to return iommu_{,un}map_page() error code in __get_page_type(), is it right?

> > --- a/xen/common/memory.c
> > +++ b/xen/common/memory.c
> > @@ -678,8 +678,9 @@ static int xenmem_add_to_physmap(struct domain
> *d,
> >      if ( need_iommu(d) )
> >      {
> >          this_cpu(iommu_dont_flush_iotlb) = 0;
> > -        iommu_iotlb_flush(d, xatp->idx - done, done);
> > -        iommu_iotlb_flush(d, xatp->gpfn - done, done);
> > +        rc = iommu_iotlb_flush(d, xatp->idx - done, done);
> > +        if ( !rc )
> > +            rc = iommu_iotlb_flush(d, xatp->gpfn - done, done);
> >      }
> 
> And the pattern repeats - you now return an error, but you don't roll back the
> now failed operation. But wait - maybe that intended:
> Are you meaning to crash the guest in such cases (somewhere deep in the flush
> code)? If so, I think that's fine, but you absolutely would need to say so in the
> commit message.
> 

Yes, I should enhance the commit message.

> > --- a/xen/drivers/passthrough/x86/iommu.c
> > +++ b/xen/drivers/passthrough/x86/iommu.c
> > @@ -104,7 +104,11 @@ int arch_iommu_populate_page_table(struct
> domain *d)
> >      this_cpu(iommu_dont_flush_iotlb) = 0;
> >
> >      if ( !rc )
> > -        iommu_iotlb_flush_all(d);
> > +    {
> > +        rc = iommu_iotlb_flush_all(d);
> > +        if ( rc )
> > +            iommu_teardown(d);
> > +    }
> >      else if ( rc != -ERESTART )
> >          iommu_teardown(d);
> 
> Why can't you just use the existing call to iommu_teardown(), by simply deleting
> the "else"?
> 

Just check it, could I modify it as below:
Quan

Comments

Jan Beulich March 29, 2016, 7:20 a.m. UTC | #1
>>> On 28.03.16 at 05:33, <quan.xu@intel.com> wrote:
> On March 18, 2016 1:15am, <JBeulich@suse.com> wrote:
>> >>> On 17.03.16 at 07:54, <quan.xu@intel.com> wrote:
>> > --- a/xen/common/grant_table.c
>> > +++ b/xen/common/grant_table.c
>> > @@ -932,8 +932,9 @@ __gnttab_map_grant_ref(
>> >              {
>> >                  nr_gets++;
>> >                  (void)get_page(pg, rd);
>> > -                if ( !(op->flags & GNTMAP_readonly) )
>> > -                    get_page_type(pg, PGT_writable_page);
>> > +                if ( !(op->flags & GNTMAP_readonly) &&
>> > +                     !get_page_type(pg, PGT_writable_page) )
>> > +                        goto could_not_pin;
>> 
>> This needs explanation, as it doesn't look related to what your actual goal is: If
>> an error was possible here, I think this would be a security issue. However, as
>> also kind of documented by the explicitly ignored return value from get_page(),
>> it is my understanding there here we only obtain an _extra_ reference.
>> 
> 
> For this point, I inferred from:
> map_vcpu_info()
> {
> ...
>     if ( !get_page_type(page, PGT_writable_page) )
>     {
>         put_page(page);
>         return -EINVAL;
>     }
> ...
> }
> , then for get_page_type(), I think the return value:
>      0 -- error, 
>      1-- right.
> 
> So if get_page_type() is failed, we should goto could_not_pin.

Did you read my reply at all? The explanation I'm expecting here is
why error checking is all of the sudden needed _at all_.

> btw, there is another issue in the call path:
>     iommu_{,un}map_page() -- __get_page_type() -- get_page_type()---
> 
> 
> I tried to return iommu_{,un}map_page() error code in __get_page_type(), is 
> it right?

If the operation got fully rolled back - yes. Whether fully rolling back
is feasible there though is - see the respective discussion - an open
question.

>> > --- a/xen/drivers/passthrough/x86/iommu.c
>> > +++ b/xen/drivers/passthrough/x86/iommu.c
>> > @@ -104,7 +104,11 @@ int arch_iommu_populate_page_table(struct
>> domain *d)
>> >      this_cpu(iommu_dont_flush_iotlb) = 0;
>> >
>> >      if ( !rc )
>> > -        iommu_iotlb_flush_all(d);
>> > +    {
>> > +        rc = iommu_iotlb_flush_all(d);
>> > +        if ( rc )
>> > +            iommu_teardown(d);
>> > +    }
>> >      else if ( rc != -ERESTART )
>> >          iommu_teardown(d);
>> 
>> Why can't you just use the existing call to iommu_teardown(), by simply 
> deleting
>> the "else"?
>> 
> 
> Just check it, could I modify it as below:
> --- a/xen/drivers/passthrough/x86/iommu.c
> +++ b/xen/drivers/passthrough/x86/iommu.c
> @@ -105,7 +105,8 @@ int arch_iommu_populate_page_table(struct domain *d)
> 
>      if ( !rc )
>          iommu_iotlb_flush_all(d);
> -    else if ( rc != -ERESTART )
> +
> +    if ( rc != -ERESTART )
>          iommu_teardown(d);

Clearly not - not only are you losing the return value of
iommu_iotlb_flush_all() now, you would then also call
iommu_teardown() in the "success" case. My comment was
related to code structure, yet you seem to have taken it
literally.

Jan
diff mbox

Patch

--- a/xen/drivers/passthrough/x86/iommu.c
+++ b/xen/drivers/passthrough/x86/iommu.c
@@ -105,7 +105,8 @@  int arch_iommu_populate_page_table(struct domain *d)

     if ( !rc )
         iommu_iotlb_flush_all(d);
-    else if ( rc != -ERESTART )
+
+    if ( rc != -ERESTART )
         iommu_teardown(d);