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

Re: [PATCH v2] xen/pci: detect when BARs are not suitably positioned


  • To: Jan Beulich <jbeulich@xxxxxxxx>
  • From: Roger Pau Monné <roger.pau@xxxxxxxxxx>
  • Date: Thu, 27 Jan 2022 10:47:03 +0100
  • Arc-authentication-results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none
  • Arc-message-signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=yTIUS7Z/qeWhGpV1l/PKLzqKWGc3G8Ldfmgdp2vZopA=; b=Ykigqyyqjrc92UHu+8Jv8PnzUEdqonv+P5m6TtffMk6q4tuRFaY2ZewMlfy6Jb350V+fa9HO6RiLotD0n6okRFdExSKzoOnntgTbaP7RBzMiHYXi6zy91qgFmhfovVSPk+/lCh+lokfMKsPM/Bv3dsNgJ+vof0/JlvTvQb3YaS4lpB2XUBKjsd0saw666gAsI7emchyYli+4M0LpVnYlzvNig4tQsQtRr72ymyHx10hJ2MElhQdjw5f1wR8Re2ukytBtwMxN0oOamf4SIqgh/vSblopY/ncyBm5yIlj+ixkR5cqU8yumm6+Crvn3/g9chDKf+DOBzWOpzDXhYeqlSg==
  • Arc-seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=hvS+eZF4XZU6euIVbVl66D27VHNIcx75VMmKl9H5LoOEt7VUydXQw4MQ++odNlxG4zw6XhgGwVVH7MGiYgzpLdjAjmjcyqSvE7jaClowN31eEM3RhpM/QAjGLK2vCRuu4VLXYucNNYwknrq8JteZczhEje0bvGaaJkdSa/93Rvzh2W/p9HaaATBLJ+41a7vHNASJJ6Bx8twutYNm1iVCPLKGWePreMkZ/o2XJzrC6U993CSPck3pEto4+9eoECDqehvM76C2bmj2s21dMm22+V6wp6tznbijLiFj5CTJqBAWxswNZuPxSuduRWwj0uDsYcsEAc/Larbtf6fx7N2RiQ==
  • Authentication-results: esa5.hc3370-68.iphmx.com; dkim=pass (signature verified) header.i=@citrix.onmicrosoft.com
  • Cc: Stefano Stabellini <sstabellini@xxxxxxxxxx>, Julien Grall <julien@xxxxxxx>, Volodymyr Babchuk <Volodymyr_Babchuk@xxxxxxxx>, Bertrand Marquis <bertrand.marquis@xxxxxxx>, Andrew Cooper <andrew.cooper3@xxxxxxxxxx>, George Dunlap <george.dunlap@xxxxxxxxxx>, Wei Liu <wl@xxxxxxx>, Paul Durrant <paul@xxxxxxx>, <xen-devel@xxxxxxxxxxxxxxxxxxxx>
  • Delivery-date: Thu, 27 Jan 2022 09:47:25 +0000
  • Ironport-data: A9a23:De4IFa+FyuCW6/Hi4HixDrUDyniTJUtcMsCJ2f8bNWPcYEJGY0x3x 2FOW2GFPK6KYWXwKo9+Pt6woRlXvZSGzII3HVM6rig8E34SpcT7XtnIdU2Y0wF+jyHgoOCLy +1EN7Es+ehtFie0Si9AttENlFEkvU2ybuOU5NXsZ2YhFWeIdA970Ug5w7dj3dYx6TSEK1jlV e3a8pW31GCNg1aYAkpMg05UgEoy1BhakGpwUm0WPZinjneH/5UmJMt3yZWKB2n5WuFp8tuSH I4v+l0bElTxpH/BAvv9+lryn9ZjrrT6ZWBigVIOM0Sub4QrSoXfHc/XOdJFAXq7hQllkPhjk PlUnqOyYj1qL7eVxv4ZUkNkOXlHaPguFL/veRBTsOSWxkzCNXDt3+9vHAc9OohwFuRfWD8Us 6ZCcXZUM07F17neLLGTE4GAguw5K8bmJsUHs2xIxjDFF/c2B5vERs0m4PcGhmxs2poeRJ4yY eI6RWNFSjbwPycWBXcvFrE3zeKCt3nwJmgwRFW9+vNsvjm7IBZK+KfpGMrYfJqNX8o9tkyVv H7c9mL1RBQTLsWCyCGt+2ipwOTImEvTSI8UUbG16PNuqFmS3XAITg0bU0Ohpvu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2Uh9Rn0dO3O9EM 2R2M1F4ePeV9DByickfB22hFmmt3vFCFpAdH7fRqFDkcg==
  • Ironport-hdrordr: A9a23:uT+cdqODU+yAwsBcTv2jsMiBIKoaSvp037BN7TEUdfU1SL38qy nApoV56faZslcssRIb9+xoWpPwJk80nKQdieN9AV7LZniBhILCFvAB0WKN+V3d8gTFh5dgPf gKScND4afLYmSSJ/yKmDVQaOxN/OW6
  • Ironport-sdr: 4C0kOs2NsJ4Yo/HUFJ9l4hBLys6n8SL7IRW1vRidVBi2q91GAw+Y2qOScr1tBJnKa5C38KoTO3 HGUUVxpWCfYfqHlfRr3eKR2vbsxVtvXErmgzbAQ5B2cDXijBY2ZaZEcQwEKKAbHIOQfkzjg9fe I8v1GartSTFC/16qsNfs6ANVqXHI8H5CTnSWpgsBkZuJY2OT9orQT2GgDF7sqnr5d0Lw0TTPsP kwSnNfRyw8qWVOvgML0wEJWdj2amF8siY9B/boTFGytvgQSLJ8QsRq5OXMHorHfYCt6Eml4Nlv vx/xx9L0TRrv44pX9mpRsRTu
  • List-id: Xen developer discussion <xen-devel.lists.xenproject.org>

On Thu, Jan 27, 2022 at 09:48:16AM +0100, Jan Beulich wrote:
> On 27.01.2022 09:22, Roger Pau Monne wrote:
> > --- a/xen/arch/arm/mm.c
> > +++ b/xen/arch/arm/mm.c
> > @@ -1625,6 +1625,17 @@ bool is_iomem_page(mfn_t mfn)
> >      return !mfn_valid(mfn);
> >  }
> >  
> > +bool is_iomem_range(paddr_t start, uint64_t size)
> > +{
> > +    unsigned int i;
> > +
> > +    for ( i = 0; i < PFN_UP(size); i++ )
> > +        if ( !is_iomem_page(_mfn(PFN_DOWN(start) + i)) )
> > +            return false;
> > +
> > +    return true;
> > +}
> 
> I'm afraid you can't very well call this non-RFC as long as this doesn't
> scale. Or if you're building on this still being dead code on Arm, then
> some kind of "fixme" annotation would be needed here (or the function be
> omitted altogether, for Arm folks to sort out before they actually start
> selecting the HAS_PCI Kconfig setting).

I was expecting the lack of 'RFC' tag to attract the attention of the
maintainers for feedback. If not I'm happy to add a FIXME here or just
drop the chunk.

> > --- a/xen/arch/x86/mm.c
> > +++ b/xen/arch/x86/mm.c
> > @@ -783,6 +783,23 @@ bool is_iomem_page(mfn_t mfn)
> >      return (page_get_owner(page) == dom_io);
> >  }
> >  
> > +bool is_iomem_range(paddr_t start, uint64_t size)
> > +{
> > +    unsigned int i;
> > +
> > +    for ( i = 0; i < e820.nr_map; i++ )
> > +    {
> > +        const struct e820entry *entry = &e820.map[i];
> > +
> > +        /* Do not allow overlaps with any memory range. */
> > +        if ( start < entry->addr + entry->size &&
> > +             entry->addr < start + size )
> > +            return false;
> > +    }
> > +
> > +    return true;
> > +}
> 
> I should have realized (and pointed out) earlier that with the type
> check dropped the function name no longer represents what the
> function does. It now really is unsuitable for about anything other
> that the checking of BARs.

is_devmem_range would be more suitable?

I will wrap this in an #ifdef HAS_PCI section now.

> > @@ -267,11 +270,75 @@ static void check_pdev(const struct pci_dev *pdev)
> >          }
> >          break;
> >  
> > +    case PCI_HEADER_TYPE_NORMAL:
> > +        nbars = PCI_HEADER_NORMAL_NR_BARS;
> > +        rom_pos = PCI_ROM_ADDRESS;
> > +        break;
> > +
> >      case PCI_HEADER_TYPE_CARDBUS:
> >          /* TODO */
> >          break;
> >      }
> >  #undef PCI_STATUS_CHECK
> > +
> > +    /* Check if BARs overlap with other memory regions. */
> > +    val = pci_conf_read16(pdev->sbdf, PCI_COMMAND);
> > +    if ( !(val & PCI_COMMAND_MEMORY) || pdev->ignore_bars )
> > +        return;
> > +
> > +    pci_conf_write16(pdev->sbdf, PCI_COMMAND, val & ~PCI_COMMAND_MEMORY);
> > +    for ( i = 0; i < nbars; )
> > +    {
> > +        uint64_t addr, size;
> > +        unsigned int reg = PCI_BASE_ADDRESS_0 + i * 4;
> > +        int rc = 1;
> > +
> > +        if ( (pci_conf_read32(pdev->sbdf, reg) & PCI_BASE_ADDRESS_SPACE) !=
> > +             PCI_BASE_ADDRESS_SPACE_MEMORY )
> > +            goto next;
> > +
> > +        rc = pci_size_mem_bar(pdev->sbdf, reg, &addr, &size,
> > +                              (i == nbars - 1) ? PCI_BAR_LAST : 0);
> > +        if ( rc < 0 )
> > +            /* Unable to size, better leave memory decoding disabled. */
> > +            return;
> > +        if ( size && !is_iomem_range(addr, size) )
> > +        {
> > +            /*
> > +             * Return without enabling memory decoding if BAR position is 
> > not
> > +             * in IO suitable memory. Let the hardware domain re-position 
> > the
> > +             * BAR.
> > +             */
> > +            printk(XENLOG_WARNING
> > +"%pp disabled: BAR%u [%" PRIx64 ", %" PRIx64 ") overlaps with memory 
> > map\n",
> 
> I guess despite its length this still wants indenting properly. Maybe
> you could fold this and ...
> 
> > +                   &pdev->sbdf, i, addr, addr + size);
> > +            return;
> > +        }
> > +
> > + next:
> > +        ASSERT(rc > 0);
> > +        i += rc;
> > +    }
> > +
> > +    if ( rom_pos &&
> > +         (pci_conf_read32(pdev->sbdf, rom_pos) & PCI_ROM_ADDRESS_ENABLE) )
> > +    {
> > +        uint64_t addr, size;
> > +        int rc = pci_size_mem_bar(pdev->sbdf, rom_pos, &addr, &size,
> > +                                  PCI_BAR_ROM);
> > +
> > +        if ( rc < 0 )
> > +            return;
> > +        if ( size && !is_iomem_range(addr, size) )
> > +        {
> > +            printk(XENLOG_WARNING
> > +"%pp disabled: ROM BAR [%" PRIx64 ", %" PRIx64 ") overlaps with memory 
> > map\n",
> 
> .. this into
> 
>     static const char warn[] =
>         "%pp disabled: %sBAR [%" PRIx64 ", %" PRIx64 ") overlaps with memory 
> map\n";
> 
> to limit indentation and redundancy at the same time? Could then also
> be re-used later for the SR-IOV BAR check.

Sure.

Thanks, Roger.



 


Rackspace

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