Skip to contentSkip to conversation

x86/mm: fix lookup_address() to handle physical memory holes in direct mapping

PATCH In review Ashish Kalra 4 participants 15 messages Jun 28, 2024 – Jul 2, 2024

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/mm: fix lookup_address() to handle physical memory holes in direct mapping

From: Ashish Kalra <[email protected]>

lookup_address_in_pgd_attr() at pte level it is simply returning pte_offset_kernel() and there does not seem to be a check for returning NULL if pte_none().

Fix lookup_address_in_pgd_attr() to add check for pte_none() after pte_offset_kernel() and return NULL if it is true.

---
 arch/x86/mm/pat/set_memory.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
arch/x86/mm/pat/set_memory.c +6 -1
index 443a97e515c0..be8b5bf3bc3f 100644
@@ -672,6 +672,7 @@ pte_t *lookup_address_in_pgd_attr(pgd_t *pgd, unsigned long address,
 	p4d_t *p4d;
 	pud_t *pud;
 	pmd_t *pmd;
+	pte_t *pte;
 
 	*level = PG_LEVEL_256T;
 	*nx = false;
@@ -717,7 +718,11 @@ pte_t *lookup_address_in_pgd_attr(pgd_t *pgd, unsigned long address,
 	*nx |= pmd_flags(*pmd) & _PAGE_NX;
 	*rw &= pmd_flags(*pmd) & _PAGE_RW;
 
-	return pte_offset_kernel(pmd, address);
+	pte = pte_offset_kernel(pmd, address);
+	if (pte_none(*pte))
+		return NULL;
+
+	return pte;
 }
 
 /*
Signature · 1 line
2.34.1
#1 lore raw
Quoted 17 lines · Ashish Kalra
arch/x86/mm/pat/set_memory.c +1 -0
index 443a97e515c0..be8b5bf3bc3f 100644
@@ -672,6 +672,7 @@ pte_t *lookup_address_in_pgd_attr(pgd_t *pgd, unsigned
 long address,
         p4d_t *p4d;
         pud_t *pud;
         pmd_t *pmd;
+       pte_t *pte;


        *level = PG_LEVEL_256T;
        *nx = false;

arch/x86/mm/pat/set_memory.c +0 -0
@@ -717,7 +718,11 @@ pte_t *lookup_address_in_pgd_attr(pgd_t *pgd, unsigned
 long address,
         *nx |= pmd_flags(*pmd) & _PAGE_NX;
         *rw &= pmd_flags(*pmd) & _PAGE_RW;
Quoted 7 lines


-       return pte_offset_kernel(pmd, address);
+       pte = pte_offset_kernel(pmd, address);
+       if (pte_none(*pte))
+               return NULL;
+
+       return pte;

The other levels check for pXX_none() before adjusting *level. Not sure what the effect would be, but I think it should be the same behavior for all.

#2 lore raw
Quoted 19 lines · Rick P
Quoted 18 lines · Ashish Kalra
arch/x86/mm/pat/set_memory.c +1 -0
index 443a97e515c0..be8b5bf3bc3f 100644
@@ -672,6 +672,7 @@ pte_t *lookup_address_in_pgd_attr(pgd_t *pgd, unsigned
 long address,
         p4d_t *p4d;
         pud_t *pud;
         pmd_t *pmd;
+       pte_t *pte;


        *level = PG_LEVEL_256T;
        *nx = false;

arch/x86/mm/pat/set_memory.c +0 -0
@@ -717,7 +718,11 @@ pte_t *lookup_address_in_pgd_attr(pgd_t *pgd, unsigned
 long address,
         *nx |= pmd_flags(*pmd) & _PAGE_NX;
         *rw &= pmd_flags(*pmd) & _PAGE_RW;

Quoted 8 lines
Quoted 6 lines

-       return pte_offset_kernel(pmd, address);
+       pte = pte_offset_kernel(pmd, address);
+       if (pte_none(*pte))
+               return NULL;
+
+       return pte;

The other levels check for pXX_none() before adjusting *level. Not sure what the effect would be, but I think it should be the same behavior for all.

If we are returning NULL, why should adjusting *level matter.

Thanks, Ashish

#3 lore raw
Quoted 16 lines · Ashish
Quoted 7 lines
Quoted 7 lines
(quoted diff) +0 -0
@@ -717,7 +718,11 @@ pte_t *lookup_address_in_pgd_attr(pgd_t *pgd,

unsigned
long address,
        *nx |= pmd_flags(*pmd) & _PAGE_NX;
        *rw &= pmd_flags(*pmd) & _PAGE_RW;

-       return pte_offset_kernel(pmd, address);

Quoted 8 lines
Quoted 5 lines

+       pte = pte_offset_kernel(pmd, address);
+       if (pte_none(*pte))
+               return NULL;
+
+       return pte;

The other levels check for pXX_none() before adjusting *level. Not sure what the
effect would be, but I think it should be the same behavior for all.

Quoted 1 line

If we are returning NULL, why should adjusting *level matter.

Well, I think symmetry is enough of a reason, but actually it should be ok.

I was looking at this diff compared to my working tree, but this tip commit (which is about that scenario) makes it set *level before checking none for all of them:
https://lore.kernel.org/lkml/171871930159.10875.16081839197437299007.tip-bot2@tip-bot2/

So sorry, nevermind.

#4 lore raw
Quoted 17 lines · Rick P
Quoted 16 lines · Ashish
Quoted 15 lines
Quoted 4 lines
(quoted diff) +0 -0
@@ -717,7 +718,11 @@ pte_t *lookup_address_in_pgd_attr(pgd_t *pgd,

unsigned
long address,
        *nx |= pmd_flags(*pmd) & _PAGE_NX;

Quoted 8 lines

        *rw &= pmd_flags(*pmd) & _PAGE_RW;

-       return pte_offset_kernel(pmd, address);
+       pte = pte_offset_kernel(pmd, address);
+       if (pte_none(*pte))
+               return NULL;
+
+       return pte;

The other levels check for pXX_none() before adjusting *level. Not sure what the
effect would be, but I think it should be the same behavior for all.

Quoted 5 lines
Quoted 1 line

If we are returning NULL, why should adjusting *level matter.

Well, I think symmetry is enough of a reason, but actually it should be ok.

I was looking at this diff compared to my working tree, but this tip commit

Ditto for me.

Thanks,
Tom

Quoted 5 lines

(which is about that scenario) makes it set *level before checking none for all of them:
https://lore.kernel.org/lkml/171871930159.10875.16081839197437299007.tip-bot2@tip-bot2/

So sorry, nevermind.

#5 lore raw
In reply to Edgecombe, Rick P
Quoted 20 lines · Rick P
Quoted 19 lines · Ashish Kalra
arch/x86/mm/pat/set_memory.c +1 -0
index 443a97e515c0..be8b5bf3bc3f 100644
@@ -672,6 +672,7 @@ pte_t *lookup_address_in_pgd_attr(pgd_t *pgd, unsigned
 long address,
         p4d_t *p4d;
         pud_t *pud;
         pmd_t *pmd;
+       pte_t *pte;


        *level = PG_LEVEL_256T;
        *nx = false;

arch/x86/mm/pat/set_memory.c +0 -0
@@ -717,7 +718,11 @@ pte_t *lookup_address_in_pgd_attr(pgd_t *pgd, unsigned
 long address,
         *nx |= pmd_flags(*pmd) & _PAGE_NX;
         *rw &= pmd_flags(*pmd) & _PAGE_RW;


-       return pte_offset_kernel(pmd, address);

Quoted 8 lines
Quoted 5 lines

+       pte = pte_offset_kernel(pmd, address);
+       if (pte_none(*pte))
+               return NULL;
+
+       return pte;

The other levels check for pXX_none() before adjusting *level. Not sure what the effect would be, but I think it should be the same behavior for all.

Agreed. It should follow the same logic as the previous checks.

It looks like the *nx and *rw should be updated, too, right? That seems to be missing from the change that added them.

Thanks,
Tom

#6 lore raw
In reply to Ashish Kalra
Quoted 8 lines · Ashish Kalra

From: Ashish Kalra <[email protected]>

lookup_address_in_pgd_attr() at pte level it is simply returning pte_offset_kernel() and there does not seem to be a check for returning NULL if pte_none().

Fix lookup_address_in_pgd_attr() to add check for pte_none() after pte_offset_kernel() and return NULL if it is true.

Please have a look at the comment above lookup_address(). You should not break the documented behavior without verifying that no caller is relying on the current behavior. If this is fine, please update the comment.

Juergen

Quoted 31 lines
---
  arch/x86/mm/pat/set_memory.c | 7 ++++++-
  1 file changed, 6 insertions(+), 1 deletion(-)
arch/x86/mm/pat/set_memory.c +6 -1
index 443a97e515c0..be8b5bf3bc3f 100644
@@ -672,6 +672,7 @@ pte_t *lookup_address_in_pgd_attr(pgd_t *pgd, unsigned long address,
  	p4d_t *p4d;
  	pud_t *pud;
  	pmd_t *pmd;
+	pte_t *pte;
 
  	*level = PG_LEVEL_256T;
  	*nx = false;
@@ -717,7 +718,11 @@ pte_t *lookup_address_in_pgd_attr(pgd_t *pgd, unsigned long address,
  	*nx |= pmd_flags(*pmd) & _PAGE_NX;
  	*rw &= pmd_flags(*pmd) & _PAGE_RW;
 
-	return pte_offset_kernel(pmd, address);
+	pte = pte_offset_kernel(pmd, address);
+	if (pte_none(*pte))
+		return NULL;
+
+	return pte;
  }
 
  /*
#7 lore raw
Quoted 7 lines · Jürgen Groß
Quoted 6 lines · Ashish Kalra

From: Ashish Kalra <[email protected]>

lookup_address_in_pgd_attr() at pte level it is simply returning pte_offset_kernel() and there does not seem to be a check for returning NULL if pte_none().

Quoted 6 lines
Quoted 2 lines

Fix lookup_address_in_pgd_attr() to add check for pte_none() after pte_offset_kernel() and return NULL if it is true.

Please have a look at the comment above lookup_address(). You should not break the documented behavior without verifying that no caller is relying on the current behavior. If this is fine, please update the comment.

This brings up a point from my other reply. The comment says that it returns "the effective NX and RW bits of all page table levels", but in fact NX and RW are not updated for the PTE. Since the comment says all page table levels, shouldn't they be updated with the PTE values, too?

Thanks,
Tom

Quoted 35 lines

Juergen

Quoted 30 lines

---
  arch/x86/mm/pat/set_memory.c | 7 ++++++-

  1 file changed, 6 insertions(+), 1 deletion(-)
arch/x86/mm/pat/set_memory.c +6 -0
index 443a97e515c0..be8b5bf3bc3f 100644
@@ -672,6 +672,7 @@ pte_t *lookup_address_in_pgd_attr(pgd_t *pgd,
 unsigned long address,
       p4d_t *p4d;
       pud_t *pud;
       pmd_t *pmd;
+    pte_t *pte;
         *level = PG_LEVEL_256T;
       *nx = false;
@@ -717,7 +718,11 @@ pte_t *lookup_address_in_pgd_attr(pgd_t *pgd,
 unsigned long address,
       *nx |= pmd_flags(*pmd) & _PAGE_NX;
       *rw &= pmd_flags(*pmd) & _PAGE_RW;
   -    return pte_offset_kernel(pmd, address);
+    pte = pte_offset_kernel(pmd, address);
+    if (pte_none(*pte))
+        return NULL;
+
+    return pte;
   }
     /*
#8 lore raw
Quoted 11 lines · Tom Lendacky
Quoted 10 lines · Jürgen Groß
Quoted 8 lines · Ashish Kalra

From: Ashish Kalra <[email protected]>

lookup_address_in_pgd_attr() at pte level it is simply returning pte_offset_kernel() and there does not seem to be a check for returning NULL if pte_none().

Fix lookup_address_in_pgd_attr() to add check for pte_none() after pte_offset_kernel() and return NULL if it is true.

Quoted 8 lines
Quoted 3 lines

Please have a look at the comment above lookup_address(). You should not break the documented behavior without verifying that no caller is relying on the current behavior. If this is fine, please update the comment.

This brings up a point from my other reply. The comment says that it returns "the effective NX and RW bits of all page table levels", but in fact NX and RW are not updated for the PTE. Since the comment says all page table levels, shouldn't they be updated with the PTE values, too?

Hmm, the comment could need some clarifications.

It returns the effective NX and RW bits of the levels above the PTE. Reason is that the function is used in case the NX/RW bits of a PTE are updated, so the PTE settings are not always really important.

Juergen

#9 lore raw
In reply to Jürgen Groß
Quoted 7 lines · Jürgen Groß
Quoted 6 lines · Ashish Kalra

From: Ashish Kalra <[email protected]>

lookup_address_in_pgd_attr() at pte level it is simply returning pte_offset_kernel() and there does not seem to be a check for returning NULL if pte_none().

Quoted 8 lines
Quoted 2 lines

Fix lookup_address_in_pgd_attr() to add check for pte_none() after pte_offset_kernel() and return NULL if it is true.

Please have a look at the comment above lookup_address(). You should not break the documented behavior without verifying that no caller is relying on the current behavior. If this is fine, please update the comment.

I don't get that, in this case the PTE does not exist, so as per the comments here lookup_address() should have returned NULL.

Thanks, Ashish

Quoted 30 lines
Quoted 29 lines

---
  arch/x86/mm/pat/set_memory.c | 7 ++++++-

  1 file changed, 6 insertions(+), 1 deletion(-)
arch/x86/mm/pat/set_memory.c +6 -0
index 443a97e515c0..be8b5bf3bc3f 100644
@@ -672,6 +672,7 @@ pte_t *lookup_address_in_pgd_attr(pgd_t *pgd, unsigned long address,
       p4d_t *p4d;
       pud_t *pud;
       pmd_t *pmd;
+    pte_t *pte;
         *level = PG_LEVEL_256T;
       *nx = false;
@@ -717,7 +718,11 @@ pte_t *lookup_address_in_pgd_attr(pgd_t *pgd, unsigned long address,
       *nx |= pmd_flags(*pmd) & _PAGE_NX;
       *rw &= pmd_flags(*pmd) & _PAGE_RW;
   -    return pte_offset_kernel(pmd, address);
+    pte = pte_offset_kernel(pmd, address);
+    if (pte_none(*pte))
+        return NULL;
+
+    return pte;
   }
     /*
#10 lore raw
Quoted 12 lines · Ashish
Quoted 10 lines · Jürgen Groß
Quoted 8 lines · Ashish Kalra

From: Ashish Kalra <[email protected]>

lookup_address_in_pgd_attr() at pte level it is simply returning pte_offset_kernel() and there does not seem to be a check for returning NULL if pte_none().

Fix lookup_address_in_pgd_attr() to add check for pte_none() after pte_offset_kernel() and return NULL if it is true.

Quoted 6 lines
Quoted 5 lines

Please have a look at the comment above lookup_address(). You should not break the documented behavior without verifying that no caller is relying on the current behavior. If this is fine, please update the comment.

I don't get that, in this case the PTE does not exist, so as per the comments here lookup_address() should have returned NULL.

There is a PTE, but it is all 0.

There is no _valid_ PTE. No PTE would mean that the related PMD entry (or any other higher level entry) is invalid.

Remember that the W^X checking needs to be performed _before_ a new PTE is written.

Juergen

#11 lore raw
Quoted 18 lines · Jürgen Groß
Quoted 17 lines · Ashish
Quoted 15 lines · Jürgen Groß
Quoted 8 lines · Ashish Kalra

From: Ashish Kalra <[email protected]>

lookup_address_in_pgd_attr() at pte level it is simply returning pte_offset_kernel() and there does not seem to be a check for returning NULL if pte_none().

Fix lookup_address_in_pgd_attr() to add check for pte_none() after pte_offset_kernel() and return NULL if it is true.

Please have a look at the comment above lookup_address(). You should not break the documented behavior without verifying that no caller is relying on the current behavior. If this is fine, please update the comment.

Quoted 6 lines
Quoted 1 line

I don't get that, in this case the PTE does not exist, so as per the comments here lookup_address() should have returned NULL.

There is a PTE, but it is all 0.

There is no _valid_ PTE. No PTE would mean that the related PMD entry (or any other higher level entry) is invalid.

Then what is the caller supposed to do in this case ?

As the return from lookup_address() is non-NULL in this case, accessing it causes a fatal #PF.

Is the caller supposed to add the check for a valid PTE using pte_none(*pte) ?

Thanks, Ashish

Quoted 6 lines

Remember that the W^X checking needs to be performed _before_ a new PTE is written.

Juergen

#12 lore raw
Quoted 7 lines · Ashish

Then what is the caller supposed to do in this case ?

As the return from lookup_address() is non-NULL in this case, accessing it causes a fatal #PF.

Is the caller supposed to add the check for a valid PTE using pte_none(*pte) ?

I did a quick look at the callers, and some do their own check for pte_none(). But some don't. Some also assume the return can't be NULL.

Can you elaborate on your goal for this change? Just a cleanup?

#13 lore raw
Quoted 11 lines · Rick P
Quoted 6 lines · Ashish

Then what is the caller supposed to do in this case ?

As the return from lookup_address() is non-NULL in this case, accessing it causes a fatal #PF.

Is the caller supposed to add the check for a valid PTE using pte_none(*pte) ?

I did a quick look at the callers, and some do their own check for pte_none(). But some don't. Some also assume the return can't be NULL.

Can you elaborate on your goal for this change? Just a cleanup?

Hit this issue while implementing and testing SNP guest kexec.

So trying to understand if need a generic fix for this issue or do i need to add my own check for pte_none() ?

Thanks, Ashish

#14 lore raw
Quoted 9 lines · Ashish
Quoted 7 lines · Rick P
Quoted 6 lines · Ashish

Then what is the caller supposed to do in this case ?

As the return from lookup_address() is non-NULL in this case, accessing it causes a fatal #PF.

Is the caller supposed to add the check for a valid PTE using pte_none(*pte) ?

Quoted 8 lines
Quoted 4 lines

I did a quick look at the callers, and some do their own check for pte_none(). But some don't. Some also assume the return can't be NULL.

Can you elaborate on your goal for this change? Just a cleanup?

Hit this issue while implementing and testing SNP guest kexec.

So trying to understand if need a generic fix for this issue or do i need to add my own check for pte_none() ?

Please add a check for pte_none() after calling lookup_address().

Juergen