[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH v2 1/2] x86/iommu: account for IOMEM caps when populating dom0 IOMMU page-tables


  • To: Roger Pau Monne <roger.pau@xxxxxxxxxx>
  • From: Jan Beulich <jbeulich@xxxxxxxx>
  • Date: Tue, 18 Feb 2025 16:32:33 +0100
  • Autocrypt: addr=jbeulich@xxxxxxxx; keydata= xsDiBFk3nEQRBADAEaSw6zC/EJkiwGPXbWtPxl2xCdSoeepS07jW8UgcHNurfHvUzogEq5xk hu507c3BarVjyWCJOylMNR98Yd8VqD9UfmX0Hb8/BrA+Hl6/DB/eqGptrf4BSRwcZQM32aZK 7Pj2XbGWIUrZrd70x1eAP9QE3P79Y2oLrsCgbZJfEwCgvz9JjGmQqQkRiTVzlZVCJYcyGGsD /0tbFCzD2h20ahe8rC1gbb3K3qk+LpBtvjBu1RY9drYk0NymiGbJWZgab6t1jM7sk2vuf0Py O9Hf9XBmK0uE9IgMaiCpc32XV9oASz6UJebwkX+zF2jG5I1BfnO9g7KlotcA/v5ClMjgo6Gl MDY4HxoSRu3i1cqqSDtVlt+AOVBJBACrZcnHAUSuCXBPy0jOlBhxPqRWv6ND4c9PH1xjQ3NP nxJuMBS8rnNg22uyfAgmBKNLpLgAGVRMZGaGoJObGf72s6TeIqKJo/LtggAS9qAUiuKVnygo 3wjfkS9A3DRO+SpU7JqWdsveeIQyeyEJ/8PTowmSQLakF+3fote9ybzd880fSmFuIEJldWxp Y2ggPGpiZXVsaWNoQHN1c2UuY29tPsJgBBMRAgAgBQJZN5xEAhsDBgsJCAcDAgQVAggDBBYC AwECHgECF4AACgkQoDSui/t3IH4J+wCfQ5jHdEjCRHj23O/5ttg9r9OIruwAn3103WUITZee e7Sbg12UgcQ5lv7SzsFNBFk3nEQQCACCuTjCjFOUdi5Nm244F+78kLghRcin/awv+IrTcIWF hUpSs1Y91iQQ7KItirz5uwCPlwejSJDQJLIS+QtJHaXDXeV6NI0Uef1hP20+y8qydDiVkv6l IreXjTb7DvksRgJNvCkWtYnlS3mYvQ9NzS9PhyALWbXnH6sIJd2O9lKS1Mrfq+y0IXCP10eS FFGg+Av3IQeFatkJAyju0PPthyTqxSI4lZYuJVPknzgaeuJv/2NccrPvmeDg6Coe7ZIeQ8Yj t0ARxu2xytAkkLCel1Lz1WLmwLstV30g80nkgZf/wr+/BXJW/oIvRlonUkxv+IbBM3dX2OV8 AmRv1ySWPTP7AAMFB/9PQK/VtlNUJvg8GXj9ootzrteGfVZVVT4XBJkfwBcpC/XcPzldjv+3 HYudvpdNK3lLujXeA5fLOH+Z/G9WBc5pFVSMocI71I8bT8lIAzreg0WvkWg5V2WZsUMlnDL9 mpwIGFhlbM3gfDMs7MPMu8YQRFVdUvtSpaAs8OFfGQ0ia3LGZcjA6Ik2+xcqscEJzNH+qh8V m5jjp28yZgaqTaRbg3M/+MTbMpicpZuqF4rnB0AQD12/3BNWDR6bmh+EkYSMcEIpQmBM51qM EKYTQGybRCjpnKHGOxG0rfFY1085mBDZCH5Kx0cl0HVJuQKC+dV2ZY5AqjcKwAxpE75MLFkr wkkEGBECAAkFAlk3nEQCGwwACgkQoDSui/t3IH7nnwCfcJWUDUFKdCsBH/E5d+0ZnMQi+G0A nAuWpQkjM1ASeQwSHEeAWPgskBQL
  • Cc: Andrew Cooper <andrew.cooper3@xxxxxxxxxx>, xen-devel@xxxxxxxxxxxxxxxxxxxx
  • Delivery-date: Tue, 18 Feb 2025 15:32:43 +0000
  • List-id: Xen developer discussion <xen-devel.lists.xenproject.org>

On 17.02.2025 15:16, Roger Pau Monne wrote:
> The current code in arch_iommu_hwdom_init() kind of open-codes the same
> MMIO permission ranges that are added to the hardware domain ->iomem_caps.
> Avoid this duplication and use ->iomem_caps in arch_iommu_hwdom_init() to
> filter which memory regions should be added to the dom0 IOMMU page-tables.
> 
> This should result in no change in the memory regions that end up identity
> mapped in the domain IOMMU page tables.
> 
> Note the IO-APIC and MCFG page(s) must be set as not accessible for a PVH
> dom0, otherwise the internal Xen emulation for those ranges won't work.
> This requires an adjustment in dom0_setup_permissions().
> 
> Also the special casing of E820_UNUSABLE regions no longer needs to be done
> in arch_iommu_hwdom_init(), as those regions are already blocked in
> ->iomem_caps and thus would be removed from the rangeset as part of
> ->iomem_caps processing in arch_iommu_hwdom_init().

Only almost: ->iomem_caps has them removed only for addresses 1Mb and up.

> --- a/xen/arch/x86/dom0_build.c
> +++ b/xen/arch/x86/dom0_build.c
> @@ -552,7 +552,8 @@ int __init dom0_setup_permissions(struct domain *d)
>      for ( i = 0; i < nr_ioapics; i++ )
>      {
>          mfn = paddr_to_pfn(mp_ioapics[i].mpc_apicaddr);
> -        if ( !rangeset_contains_singleton(mmio_ro_ranges, mfn) )
> +        if ( is_hvm_domain(d) ||
> +             !rangeset_contains_singleton(mmio_ro_ranges, mfn) )
>              rc |= iomem_deny_access(d, mfn, mfn);
>      }

The original code used has_vioapic() and had a comment. At least one of#
the two would better be transplanted here, I think.

> @@ -593,6 +594,22 @@ int __init dom0_setup_permissions(struct domain *d)
>              rc |= rangeset_add_singleton(mmio_ro_ranges, mfn);
>      }
>  
> +    /* For PVH dom0 prevent access to MCFG, it's emulated by Xen. */
> +    if ( is_hvm_domain(d) )
> +    {
> +        for ( i = 0; i < pci_mmcfg_config_num; i++ )
> +        {
> +            const unsigned long s =
> +                PFN_DOWN(pci_mmcfg_config[i].address) +
> +                PCI_BDF(pci_mmcfg_config[i].start_bus_number, 0, 0);
> +            const unsigned long e =
> +                PFN_DOWN(pci_mmcfg_config[i].address) +
> +                PCI_BDF(pci_mmcfg_config[i].end_bus_number, ~0, ~0);
> +
> +            rc |= iomem_deny_access(d, s, e);
> +        }
> +    }

Similarly here, the original code used has_vpci() and also had a comment.
Is there actually a strong reason to replace the original MCFG code?

> --- a/xen/drivers/passthrough/x86/iommu.c
> +++ b/xen/drivers/passthrough/x86/iommu.c
> @@ -320,6 +320,26 @@ static int __hwdom_init cf_check map_subtract(unsigned 
> long s, unsigned long e,
>      return rangeset_remove_range(map, s, e);
>  }
>  
> +struct handle_iomemcap {
> +    struct rangeset *r;
> +    unsigned long last;
> +};
> +static int __hwdom_init cf_check map_subtract_iomemcap(unsigned long s,
> +                                                       unsigned long e,
> +                                                       void *data)
> +{
> +    struct handle_iomemcap *h = data;
> +    int rc = 0;
> +
> +    if ( h->last != s )
> +        rc = rangeset_remove_range(h->r, h->last, s - 1);
> +
> +    /* Be careful with overflows. */
> +    h->last = max(e + 1, e);

What overflow could occur here? Addresses are limited to 52 bits; frame
numbers are limited to 40 bits. And ->iomem_caps starts out with a range
ending at the last address permitted by paddr_bits.

> @@ -475,22 +488,13 @@ void __hwdom_init arch_iommu_hwdom_init(struct domain 
> *d)
>      if ( rc )
>          panic("IOMMU failed to remove Xen ranges: %d\n", rc);
>  
> -    /* Remove any overlap with the Interrupt Address Range. */
> -    rc = rangeset_remove_range(map, 0xfee00, 0xfeeff);
> +    iomem.r = map;
> +    rc = rangeset_report_ranges(d->iomem_caps, 0, ~0UL, 
> map_subtract_iomemcap,
> +                                &iomem);
> +    if ( !rc && iomem.last < ~0UL )
> +        rc = rangeset_remove_range(map, iomem.last, ~0UL);

Similarly here I'm wondering about the upper bound of ~0UL. Is this just
to be on the safe side? And/or just because it's simpler than calculating
the actual upper bound? No ranges above the system physical address width
should ever be entered into the rangeset. Kind of as an implication
iomem.last also is guaranteed to be below ~0UL when making it here.

Jan



 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.