From: "Jan Beulich" Subject: [PATCH, RFC] x86/iommu: don't map RAM holes above 4G Date: Mon, 13 Dec 2010 17:05:29 +0000 Message-ID: <4D06606902000078000279CE@vpn.id2.novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__Part38147E49.0__=" Return-path: List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xensource.com Errors-To: xen-devel-bounces@lists.xensource.com To: "xen-devel@lists.xensource.com" List-Id: xen-devel@lists.xenproject.org This is a MIME message. If you are reading this text, you may want to consider changing to a mail reader or gateway that understands how to properly handle MIME multipart messages. --=__Part38147E49.0__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Matching the comment in iommu_set_dom0_mapping(), map only actual RAM from the address range starting at 4G. It's not clear though whether that comment is actually correct (which is why I'm sending this as RFC), but it is certain that on systems with sparse physical memory map we're currently wasting a potentially significant amount of memory for setting up IOMMU page tables that will never be used. The main question is what happens for MMIO ranges living above 4G. Of course, the same issue would currently exist for any such ranges sitting beyon the end of RAM. Signed-off-by: Jan Beulich --- a/xen/drivers/passthrough/amd/pci_amd_iommu.c +++ b/xen/drivers/passthrough/amd/pci_amd_iommu.c @@ -230,8 +230,16 @@ static void __init amd_iommu_dom0_init(s if ( !iommu_passthrough && !need_iommu(d) ) { /* Set up 1:1 page table for dom0 */ - for ( i =3D 0; i < max_page; i++ ) - amd_iommu_map_page(d, i, i, IOMMUF_readable|IOMMUF_writable); + for ( i =3D 0; i < max_pdx; i++ ) + { + unsigned long pfn =3D pdx_to_pfn(i); + + /* + * XXX Should we really map all non-RAM (above 4G)? Minimally + * a pfn_valid() check would seem desirable here. + */ + amd_iommu_map_page(d, pfn, pfn, IOMMUF_readable|IOMMUF_writabl= e); + } } =20 amd_iommu_setup_dom0_devices(d); --- a/xen/drivers/passthrough/vtd/x86/vtd.c +++ b/xen/drivers/passthrough/vtd/x86/vtd.c @@ -129,14 +129,14 @@ void hvm_dpci_isairq_eoi(struct domain * =20 void __init iommu_set_dom0_mapping(struct domain *d) { - u64 i, j, tmp, max_pfn; + unsigned long i, j, tmp, top; extern int xen_in_range(unsigned long mfn); =20 BUG_ON(d->domain_id !=3D 0); =20 - max_pfn =3D max_t(u64, max_page, 0x100000000ull >> PAGE_SHIFT); + top =3D max(max_pdx, pfn_to_pdx(0xffffffffUL >> PAGE_SHIFT) + 1); =20 - for ( i =3D 0; i < max_pfn; i++ ) + for ( i =3D 0; i < top; i++ ) { /* * Set up 1:1 mapping for dom0. Default to use only conventional = RAM @@ -144,18 +144,23 @@ void __init iommu_set_dom0_mapping(struc * inclusive mapping maps in everything below 4GB except unusable * ranges. */ - if ( !page_is_ram_type(i, RAM_TYPE_CONVENTIONAL) && - (!iommu_inclusive_mapping || - page_is_ram_type(i, RAM_TYPE_UNUSABLE)) ) + unsigned long pfn =3D pdx_to_pfn(i); + + if ( pfn > (0xffffffffUL >> PAGE_SHIFT) ? + (!mfn_valid(pfn) || + !page_is_ram_type(pfn, RAM_TYPE_CONVENTIONAL)) : + iommu_inclusive_mapping ? + page_is_ram_type(pfn, RAM_TYPE_UNUSABLE) : + !page_is_ram_type(pfn, RAM_TYPE_CONVENTIONAL) ) continue; =20 /* Exclude Xen bits */ - if ( xen_in_range(i) ) + if ( xen_in_range(pfn) ) continue; =20 tmp =3D 1 << (PAGE_SHIFT - PAGE_SHIFT_4K); for ( j =3D 0; j < tmp; j++ ) - iommu_map_page(d, (i*tmp+j), (i*tmp+j), + iommu_map_page(d, pfn * tmp + j, pfn * tmp + j, IOMMUF_readable|IOMMUF_writable); =20 if (!(i & (0xfffff >> (PAGE_SHIFT - PAGE_SHIFT_4K)))) --=__Part38147E49.0__= Content-Type: text/plain; name="iommu-dom0-holes.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="iommu-dom0-holes.patch" Matching the comment in iommu_set_dom0_mapping(), map only actual = RAM=0Afrom the address range starting at 4G. It's not clear though = whether=0Athat comment is actually correct (which is why I'm sending this = as=0ARFC), but it is certain that on systems with sparse physical = memory=0Amap we're currently wasting a potentially significant amount of = memory=0Afor setting up IOMMU page tables that will never be used.=0A=0AThe= main question is what happens for MMIO ranges living above 4G. Of=0Acourse= , the same issue would currently exist for any such ranges=0Asitting beyon = the end of RAM.=0A=0ASigned-off-by: Jan Beulich =0A=0A= --- a/xen/drivers/passthrough/amd/pci_amd_iommu.c=0A+++ b/xen/drivers/passt= hrough/amd/pci_amd_iommu.c=0A@@ -230,8 +230,16 @@ static void __init = amd_iommu_dom0_init(s=0A if ( !iommu_passthrough && !need_iommu(d) = )=0A {=0A /* Set up 1:1 page table for dom0 */=0A- for = ( i =3D 0; i < max_page; i++ )=0A- amd_iommu_map_page(d, i, i, = IOMMUF_readable|IOMMUF_writable);=0A+ for ( i =3D 0; i < max_pdx; = i++ )=0A+ {=0A+ unsigned long pfn =3D pdx_to_pfn(i);=0A+= =0A+ /*=0A+ * XXX Should we really map all non-RAM = (above 4G)? Minimally=0A+ * a pfn_valid() check would seem = desirable here.=0A+ */=0A+ amd_iommu_map_page(d, = pfn, pfn, IOMMUF_readable|IOMMUF_writable);=0A+ }=0A }=0A =0A = amd_iommu_setup_dom0_devices(d);=0A--- a/xen/drivers/passthrough/vtd/x86/= vtd.c=0A+++ b/xen/drivers/passthrough/vtd/x86/vtd.c=0A@@ -129,14 +129,14 = @@ void hvm_dpci_isairq_eoi(struct domain *=0A =0A void __init iommu_set_do= m0_mapping(struct domain *d)=0A {=0A- u64 i, j, tmp, max_pfn;=0A+ = unsigned long i, j, tmp, top;=0A extern int xen_in_range(unsigned long = mfn);=0A =0A BUG_ON(d->domain_id !=3D 0);=0A =0A- max_pfn =3D = max_t(u64, max_page, 0x100000000ull >> PAGE_SHIFT);=0A+ top =3D = max(max_pdx, pfn_to_pdx(0xffffffffUL >> PAGE_SHIFT) + 1);=0A =0A- for ( = i =3D 0; i < max_pfn; i++ )=0A+ for ( i =3D 0; i < top; i++ )=0A = {=0A /*=0A * Set up 1:1 mapping for dom0. Default to use = only conventional RAM=0A@@ -144,18 +144,23 @@ void __init iommu_set_dom0_ma= pping(struc=0A * inclusive mapping maps in everything below 4GB = except unusable=0A * ranges.=0A */=0A- if ( = !page_is_ram_type(i, RAM_TYPE_CONVENTIONAL) &&=0A- (!iommu_incl= usive_mapping ||=0A- page_is_ram_type(i, RAM_TYPE_UNUSABLE)) = )=0A+ unsigned long pfn =3D pdx_to_pfn(i);=0A+=0A+ if ( pfn = > (0xffffffffUL >> PAGE_SHIFT) ?=0A+ (!mfn_valid(pfn) ||=0A+ = !page_is_ram_type(pfn, RAM_TYPE_CONVENTIONAL)) :=0A+ = iommu_inclusive_mapping ?=0A+ page_is_ram_type(pfn, RAM_TYPE_U= NUSABLE) :=0A+ !page_is_ram_type(pfn, RAM_TYPE_CONVENTIONAL) = )=0A continue;=0A =0A /* Exclude Xen bits */=0A- = if ( xen_in_range(i) )=0A+ if ( xen_in_range(pfn) )=0A = continue;=0A =0A tmp =3D 1 << (PAGE_SHIFT - PAGE_SHIFT_4K);=0A = for ( j =3D 0; j < tmp; j++ )=0A- iommu_map_page(d, = (i*tmp+j), (i*tmp+j),=0A+ iommu_map_page(d, pfn * tmp + j, pfn = * tmp + j,=0A IOMMUF_readable|IOMMUF_writable);= =0A =0A if (!(i & (0xfffff >> (PAGE_SHIFT - PAGE_SHIFT_4K))))=0A --=__Part38147E49.0__= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Xen-devel mailing list Xen-devel@lists.xensource.com http://lists.xensource.com/xen-devel --=__Part38147E49.0__=--