diff mbox

[SRU,Trusty,1/1] x86/mm: Fix slow_virt_to_phys() for X86_PAE again

Message ID 4da45ecff9026d743e70cdc4e835b05fb71abb1f.1457030886.git.joseph.salisbury@canonical.com
State New
Headers show

Commit Message

Joseph Salisbury March 9, 2016, 5:50 p.m. UTC
From: Dexuan Cui <decui@microsoft.com>

BugLink: http://bugs.launchpad.net/bugs/1549601

"d1cd12108346: x86, pageattr: Prevent overflow in slow_virt_to_phys() for
X86_PAE" was unintentionally removed by the recent "34437e67a672: x86/mm: Fix
slow_virt_to_phys() to handle large PAT bit".

And, the variable 'phys_addr' was defined as "unsigned long" by mistake -- it should
be "phys_addr_t".

As a result, Hyper-V network driver in 32-PAE Linux guest can't work again.

Fixes: commit 34437e67a672: "x86/mm: Fix slow_virt_to_phys() to handle large PAT bit"
Signed-off-by: Dexuan Cui <decui@microsoft.com>
Reviewed-by: Toshi Kani <toshi.kani@hpe.com>
Cc: olaf@aepfle.de
Cc: gregkh@linuxfoundation.org
Cc: jasowang@redhat.com
Cc: driverdev-devel@linuxdriverproject.org
Cc: linux-mm@kvack.org
Cc: apw@canonical.com
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: K. Y. Srinivasan <kys@microsoft.com>
Cc: Haiyang Zhang <haiyangz@microsoft.com>
Link: http://lkml.kernel.org/r/1456394292-9030-1-git-send-email-decui@microsoft.com
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
(backported from commit bf70e5513dfea29c3682e7eb3dbb45f0723bac09)
Signed-off-by: Joseph Salisbury <joseph.salisbury@canonical.com>
---
 arch/x86/include/asm/page_types.h |  9 ++++++---
 arch/x86/mm/pageattr.c            | 26 +++++++++++++++++++++-----
 2 files changed, 27 insertions(+), 8 deletions(-)

Comments

Tim Gardner March 10, 2016, 12:36 p.m. UTC | #1
On 03/09/2016 10:50 AM, Joseph Salisbury wrote:
> From: Dexuan Cui <decui@microsoft.com>
> 
> BugLink: http://bugs.launchpad.net/bugs/1549601
> 
> "d1cd12108346: x86, pageattr: Prevent overflow in slow_virt_to_phys() for
> X86_PAE" was unintentionally removed by the recent "34437e67a672: x86/mm: Fix
> slow_virt_to_phys() to handle large PAT bit".
> 
> And, the variable 'phys_addr' was defined as "unsigned long" by mistake -- it should
> be "phys_addr_t".
> 
> As a result, Hyper-V network driver in 32-PAE Linux guest can't work again.
> 
> Fixes: commit 34437e67a672: "x86/mm: Fix slow_virt_to_phys() to handle large PAT bit"
> Signed-off-by: Dexuan Cui <decui@microsoft.com>
> Reviewed-by: Toshi Kani <toshi.kani@hpe.com>
> Cc: olaf@aepfle.de
> Cc: gregkh@linuxfoundation.org
> Cc: jasowang@redhat.com
> Cc: driverdev-devel@linuxdriverproject.org
> Cc: linux-mm@kvack.org
> Cc: apw@canonical.com
> Cc: Andrew Morton <akpm@linux-foundation.org>
> Cc: K. Y. Srinivasan <kys@microsoft.com>
> Cc: Haiyang Zhang <haiyangz@microsoft.com>
> Link: http://lkml.kernel.org/r/1456394292-9030-1-git-send-email-decui@microsoft.com
> Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
> (backported from commit bf70e5513dfea29c3682e7eb3dbb45f0723bac09)
> Signed-off-by: Joseph Salisbury <joseph.salisbury@canonical.com>
> ---
>  arch/x86/include/asm/page_types.h |  9 ++++++---
>  arch/x86/mm/pageattr.c            | 26 +++++++++++++++++++++-----
>  2 files changed, 27 insertions(+), 8 deletions(-)
> 
> diff --git a/arch/x86/include/asm/page_types.h b/arch/x86/include/asm/page_types.h
> index f97fbe3..53193b7 100644
> --- a/arch/x86/include/asm/page_types.h
> +++ b/arch/x86/include/asm/page_types.h
> @@ -9,6 +9,12 @@
>  #define PAGE_SIZE	(_AC(1,UL) << PAGE_SHIFT)
>  #define PAGE_MASK	(~(PAGE_SIZE-1))
>  
> +#define PMD_PAGE_SIZE		(_AC(1, UL) << PMD_SHIFT)
> +#define PMD_PAGE_MASK		(~(PMD_PAGE_SIZE-1))
> +
> +#define PUD_PAGE_SIZE           (_AC(1, UL) << PUD_SHIFT)
> +#define PUD_PAGE_MASK           (~(PUD_PAGE_SIZE-1))
> +
>  #define __PHYSICAL_MASK		((phys_addr_t)((1ULL << __PHYSICAL_MASK_SHIFT) - 1))
>  #define __VIRTUAL_MASK		((1UL << __VIRTUAL_MASK_SHIFT) - 1)
>  
> @@ -17,9 +23,6 @@
>     (ie, 32-bit PAE). */
>  #define PHYSICAL_PAGE_MASK	(((signed long)PAGE_MASK) & __PHYSICAL_MASK)
>  
> -#define PMD_PAGE_SIZE		(_AC(1, UL) << PMD_SHIFT)
> -#define PMD_PAGE_MASK		(~(PMD_PAGE_SIZE-1))
> -
>  #define HPAGE_SHIFT		PMD_SHIFT
>  #define HPAGE_SIZE		(_AC(1,UL) << HPAGE_SHIFT)
>  #define HPAGE_MASK		(~(HPAGE_SIZE - 1))

Why the changes to these macros ? They are not part of the upstream
commit, which is an otherwise clean cherry-pick except for some minor
whitespace differences.

rtg
Tim Gardner March 10, 2016, 12:47 p.m. UTC | #2
On 03/10/2016 05:36 AM, Tim Gardner wrote:
> On 03/09/2016 10:50 AM, Joseph Salisbury wrote:
>> From: Dexuan Cui <decui@microsoft.com>
>>
>> BugLink: http://bugs.launchpad.net/bugs/1549601
>>
>> "d1cd12108346: x86, pageattr: Prevent overflow in slow_virt_to_phys() for
>> X86_PAE" was unintentionally removed by the recent "34437e67a672: x86/mm: Fix
>> slow_virt_to_phys() to handle large PAT bit".
>>
>> And, the variable 'phys_addr' was defined as "unsigned long" by mistake -- it should
>> be "phys_addr_t".
>>
>> As a result, Hyper-V network driver in 32-PAE Linux guest can't work again.
>>
>> Fixes: commit 34437e67a672: "x86/mm: Fix slow_virt_to_phys() to handle large PAT bit"
>> Signed-off-by: Dexuan Cui <decui@microsoft.com>
>> Reviewed-by: Toshi Kani <toshi.kani@hpe.com>
>> Cc: olaf@aepfle.de
>> Cc: gregkh@linuxfoundation.org
>> Cc: jasowang@redhat.com
>> Cc: driverdev-devel@linuxdriverproject.org
>> Cc: linux-mm@kvack.org
>> Cc: apw@canonical.com
>> Cc: Andrew Morton <akpm@linux-foundation.org>
>> Cc: K. Y. Srinivasan <kys@microsoft.com>
>> Cc: Haiyang Zhang <haiyangz@microsoft.com>
>> Link: http://lkml.kernel.org/r/1456394292-9030-1-git-send-email-decui@microsoft.com
>> Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
>> (backported from commit bf70e5513dfea29c3682e7eb3dbb45f0723bac09)
>> Signed-off-by: Joseph Salisbury <joseph.salisbury@canonical.com>
>> ---
>>  arch/x86/include/asm/page_types.h |  9 ++++++---
>>  arch/x86/mm/pageattr.c            | 26 +++++++++++++++++++++-----
>>  2 files changed, 27 insertions(+), 8 deletions(-)
>>
>> diff --git a/arch/x86/include/asm/page_types.h b/arch/x86/include/asm/page_types.h
>> index f97fbe3..53193b7 100644
>> --- a/arch/x86/include/asm/page_types.h
>> +++ b/arch/x86/include/asm/page_types.h
>> @@ -9,6 +9,12 @@
>>  #define PAGE_SIZE	(_AC(1,UL) << PAGE_SHIFT)
>>  #define PAGE_MASK	(~(PAGE_SIZE-1))
>>  
>> +#define PMD_PAGE_SIZE		(_AC(1, UL) << PMD_SHIFT)
>> +#define PMD_PAGE_MASK		(~(PMD_PAGE_SIZE-1))
>> +
>> +#define PUD_PAGE_SIZE           (_AC(1, UL) << PUD_SHIFT)
>> +#define PUD_PAGE_MASK           (~(PUD_PAGE_SIZE-1))
>> +
>>  #define __PHYSICAL_MASK		((phys_addr_t)((1ULL << __PHYSICAL_MASK_SHIFT) - 1))
>>  #define __VIRTUAL_MASK		((1UL << __VIRTUAL_MASK_SHIFT) - 1)
>>  
>> @@ -17,9 +23,6 @@
>>     (ie, 32-bit PAE). */
>>  #define PHYSICAL_PAGE_MASK	(((signed long)PAGE_MASK) & __PHYSICAL_MASK)
>>  
>> -#define PMD_PAGE_SIZE		(_AC(1, UL) << PMD_SHIFT)
>> -#define PMD_PAGE_MASK		(~(PMD_PAGE_SIZE-1))
>> -
>>  #define HPAGE_SHIFT		PMD_SHIFT
>>  #define HPAGE_SIZE		(_AC(1,UL) << HPAGE_SHIFT)
>>  #define HPAGE_MASK		(~(HPAGE_SIZE - 1))
> 
> Why the changes to these macros ? They are not part of the upstream
> commit, which is an otherwise clean cherry-pick except for some minor
> whitespace differences.
> 
> rtg
> 

Never mind, I see why you've done this. You probably should have
mentioned the partial backports from prerequisite commits and why this
is much simpler.

rtg
Brad Figg March 10, 2016, 6:09 p.m. UTC | #3
On Wed, Mar 09, 2016 at 12:50:53PM -0500, Joseph Salisbury wrote:
> From: Dexuan Cui <decui@microsoft.com>
> 
> BugLink: http://bugs.launchpad.net/bugs/1549601
> 
> "d1cd12108346: x86, pageattr: Prevent overflow in slow_virt_to_phys() for
> X86_PAE" was unintentionally removed by the recent "34437e67a672: x86/mm: Fix
> slow_virt_to_phys() to handle large PAT bit".
> 
> And, the variable 'phys_addr' was defined as "unsigned long" by mistake -- it should
> be "phys_addr_t".
> 
> As a result, Hyper-V network driver in 32-PAE Linux guest can't work again.
> 
> Fixes: commit 34437e67a672: "x86/mm: Fix slow_virt_to_phys() to handle large PAT bit"
> Signed-off-by: Dexuan Cui <decui@microsoft.com>
> Reviewed-by: Toshi Kani <toshi.kani@hpe.com>
> Cc: olaf@aepfle.de
> Cc: gregkh@linuxfoundation.org
> Cc: jasowang@redhat.com
> Cc: driverdev-devel@linuxdriverproject.org
> Cc: linux-mm@kvack.org
> Cc: apw@canonical.com
> Cc: Andrew Morton <akpm@linux-foundation.org>
> Cc: K. Y. Srinivasan <kys@microsoft.com>
> Cc: Haiyang Zhang <haiyangz@microsoft.com>
> Link: http://lkml.kernel.org/r/1456394292-9030-1-git-send-email-decui@microsoft.com
> Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
> (backported from commit bf70e5513dfea29c3682e7eb3dbb45f0723bac09)
> Signed-off-by: Joseph Salisbury <joseph.salisbury@canonical.com>
> ---
>  arch/x86/include/asm/page_types.h |  9 ++++++---
>  arch/x86/mm/pageattr.c            | 26 +++++++++++++++++++++-----
>  2 files changed, 27 insertions(+), 8 deletions(-)
> 
> diff --git a/arch/x86/include/asm/page_types.h b/arch/x86/include/asm/page_types.h
> index f97fbe3..53193b7 100644
> --- a/arch/x86/include/asm/page_types.h
> +++ b/arch/x86/include/asm/page_types.h
> @@ -9,6 +9,12 @@
>  #define PAGE_SIZE	(_AC(1,UL) << PAGE_SHIFT)
>  #define PAGE_MASK	(~(PAGE_SIZE-1))
>  
> +#define PMD_PAGE_SIZE		(_AC(1, UL) << PMD_SHIFT)
> +#define PMD_PAGE_MASK		(~(PMD_PAGE_SIZE-1))
> +
> +#define PUD_PAGE_SIZE           (_AC(1, UL) << PUD_SHIFT)
> +#define PUD_PAGE_MASK           (~(PUD_PAGE_SIZE-1))
> +
>  #define __PHYSICAL_MASK		((phys_addr_t)((1ULL << __PHYSICAL_MASK_SHIFT) - 1))
>  #define __VIRTUAL_MASK		((1UL << __VIRTUAL_MASK_SHIFT) - 1)
>  
> @@ -17,9 +23,6 @@
>     (ie, 32-bit PAE). */
>  #define PHYSICAL_PAGE_MASK	(((signed long)PAGE_MASK) & __PHYSICAL_MASK)
>  
> -#define PMD_PAGE_SIZE		(_AC(1, UL) << PMD_SHIFT)
> -#define PMD_PAGE_MASK		(~(PMD_PAGE_SIZE-1))
> -
>  #define HPAGE_SHIFT		PMD_SHIFT
>  #define HPAGE_SIZE		(_AC(1,UL) << HPAGE_SHIFT)
>  #define HPAGE_MASK		(~(HPAGE_SIZE - 1))
> diff --git a/arch/x86/mm/pageattr.c b/arch/x86/mm/pageattr.c
> index d528ae3..bbaea09 100644
> --- a/arch/x86/mm/pageattr.c
> +++ b/arch/x86/mm/pageattr.c
> @@ -402,11 +402,27 @@ phys_addr_t slow_virt_to_phys(void *__virt_addr)
>  
>  	pte = lookup_address(virt_addr, &level);
>  	BUG_ON(!pte);
> -	psize = page_level_size(level);
> -	pmask = page_level_mask(level);
> -	offset = virt_addr & ~pmask;
> -	phys_addr = (phys_addr_t)pte_pfn(*pte) << PAGE_SHIFT;
> -	return (phys_addr | offset);
> +
> +	/*
> +	 * pXX_pfn() returns unsigned long, which must be cast to phys_addr_t
> +	 * before being left-shifted PAGE_SHIFT bits -- this trick is to
> +	 * make 32-PAE kernel work correctly.
> +	 */
> +	switch (level) {
> +	case PG_LEVEL_1G:
> +		phys_addr = (phys_addr_t)pud_pfn(*(pud_t *)pte) << PAGE_SHIFT;
> +		offset = virt_addr & ~PUD_PAGE_MASK;
> +		break;
> +	case PG_LEVEL_2M:
> +		phys_addr = (phys_addr_t)pmd_pfn(*(pmd_t *)pte) << PAGE_SHIFT;
> +		offset = virt_addr & ~PMD_PAGE_MASK;
> +		break;
> +	default:
> +		phys_addr = (phys_addr_t)pte_pfn(*pte) << PAGE_SHIFT;
> +		offset = virt_addr & ~PAGE_MASK;
> +	}
> +
> +	return (phys_addr_t)(phys_addr | offset);
>  }
>  EXPORT_SYMBOL_GPL(slow_virt_to_phys);
>  
> -- 
> 1.9.1
> 
> 
> -- 
> kernel-team mailing list
> kernel-team@lists.ubuntu.com
> https://lists.ubuntu.com/mailman/listinfo/kernel-team

Looks good.
Brad Figg March 14, 2016, 3:45 p.m. UTC | #4
Applied to the master-next branch of Trusty.
diff mbox

Patch

diff --git a/arch/x86/include/asm/page_types.h b/arch/x86/include/asm/page_types.h
index f97fbe3..53193b7 100644
--- a/arch/x86/include/asm/page_types.h
+++ b/arch/x86/include/asm/page_types.h
@@ -9,6 +9,12 @@ 
 #define PAGE_SIZE	(_AC(1,UL) << PAGE_SHIFT)
 #define PAGE_MASK	(~(PAGE_SIZE-1))
 
+#define PMD_PAGE_SIZE		(_AC(1, UL) << PMD_SHIFT)
+#define PMD_PAGE_MASK		(~(PMD_PAGE_SIZE-1))
+
+#define PUD_PAGE_SIZE           (_AC(1, UL) << PUD_SHIFT)
+#define PUD_PAGE_MASK           (~(PUD_PAGE_SIZE-1))
+
 #define __PHYSICAL_MASK		((phys_addr_t)((1ULL << __PHYSICAL_MASK_SHIFT) - 1))
 #define __VIRTUAL_MASK		((1UL << __VIRTUAL_MASK_SHIFT) - 1)
 
@@ -17,9 +23,6 @@ 
    (ie, 32-bit PAE). */
 #define PHYSICAL_PAGE_MASK	(((signed long)PAGE_MASK) & __PHYSICAL_MASK)
 
-#define PMD_PAGE_SIZE		(_AC(1, UL) << PMD_SHIFT)
-#define PMD_PAGE_MASK		(~(PMD_PAGE_SIZE-1))
-
 #define HPAGE_SHIFT		PMD_SHIFT
 #define HPAGE_SIZE		(_AC(1,UL) << HPAGE_SHIFT)
 #define HPAGE_MASK		(~(HPAGE_SIZE - 1))
diff --git a/arch/x86/mm/pageattr.c b/arch/x86/mm/pageattr.c
index d528ae3..bbaea09 100644
--- a/arch/x86/mm/pageattr.c
+++ b/arch/x86/mm/pageattr.c
@@ -402,11 +402,27 @@  phys_addr_t slow_virt_to_phys(void *__virt_addr)
 
 	pte = lookup_address(virt_addr, &level);
 	BUG_ON(!pte);
-	psize = page_level_size(level);
-	pmask = page_level_mask(level);
-	offset = virt_addr & ~pmask;
-	phys_addr = (phys_addr_t)pte_pfn(*pte) << PAGE_SHIFT;
-	return (phys_addr | offset);
+
+	/*
+	 * pXX_pfn() returns unsigned long, which must be cast to phys_addr_t
+	 * before being left-shifted PAGE_SHIFT bits -- this trick is to
+	 * make 32-PAE kernel work correctly.
+	 */
+	switch (level) {
+	case PG_LEVEL_1G:
+		phys_addr = (phys_addr_t)pud_pfn(*(pud_t *)pte) << PAGE_SHIFT;
+		offset = virt_addr & ~PUD_PAGE_MASK;
+		break;
+	case PG_LEVEL_2M:
+		phys_addr = (phys_addr_t)pmd_pfn(*(pmd_t *)pte) << PAGE_SHIFT;
+		offset = virt_addr & ~PMD_PAGE_MASK;
+		break;
+	default:
+		phys_addr = (phys_addr_t)pte_pfn(*pte) << PAGE_SHIFT;
+		offset = virt_addr & ~PAGE_MASK;
+	}
+
+	return (phys_addr_t)(phys_addr | offset);
 }
 EXPORT_SYMBOL_GPL(slow_virt_to_phys);