Skip to contentSkip to conversation

Clarify huge_pte_offset() semantics

PATCH Applied Punit Agrawal 4 participants 18 messages Jul 25, 2017 – Aug 30, 2017

Subsystems (3)
lore
Share & tools
lore
#1 lore raw

PATCH 1/1 mm/hugetlb: Make huge_pte_offset() consistent and document behaviour

In reply to Punit Agrawal

When walking the page tables to resolve an address that points to !p*d_present() entry, huge_pte_offset() returns inconsistent values depending on the level of page table (PUD or PMD).

It returns NULL in the case of a PUD entry while in the case of a PMD entry, it returns a pointer to the page table entry.

A similar inconsitency exists when handling swap entries - returns NULL for a PUD entry while a pointer to the pte_t is retured for the PMD entry.

Update huge_pte_offset() to make the behaviour consistent - return NULL in the case of p*d_none() and a pointer to the pte_t for hugepage or swap entries.

Document the behaviour to clarify the expected behaviour of this function. This is to set clear semantics for architecture specific implementations of huge_pte_offset().

---
 mm/hugetlb.c | 22 +++++++++++++++++++---
 1 file changed, 19 insertions(+), 3 deletions(-)
mm/hugetlb.c +19 -3
index bc48ee783dd9..72dd1139a8e4 100644
@@ -4603,6 +4603,13 @@ pte_t *huge_pte_alloc(struct mm_struct *mm,
 	return pte;
 }
 
+/*
+ * huge_pte_offset() - Walk the page table to resolve the hugepage
+ * entry at address @addr
+ *
+ * Return: Pointer to page table or swap entry (PUD or PMD) for address @addr
+ * or NULL if the entry is p*d_none().
+ */
 pte_t *huge_pte_offset(struct mm_struct *mm,
 		       unsigned long addr, unsigned long sz)
 {
@@ -4617,13 +4624,22 @@ pte_t *huge_pte_offset(struct mm_struct *mm,
 	p4d = p4d_offset(pgd, addr);
 	if (!p4d_present(*p4d))
 		return NULL;
+
 	pud = pud_offset(p4d, addr);
-	if (!pud_present(*pud))
+	if (pud_none(*pud))
 		return NULL;
-	if (pud_huge(*pud))
+	/* hugepage or swap? */
+	if (pud_huge(*pud) || !pud_present(*pud))
 		return (pte_t *)pud;
+
 	pmd = pmd_offset(pud, addr);
-	return (pte_t *) pmd;
+	if (pmd_none(*pmd))
+		return NULL;
+	/* hugepage or swap? */
+	if (pmd_huge(*pmd) || !pmd_present(*pmd))
+		return (pte_t *) pmd;
+
+	return NULL;
 }
 
 #endif /* CONFIG_ARCH_WANT_GENERAL_HUGETLB */
Signature · 1 line
2.11.0
#0 lore raw

cover Clarify huge_pte_offset() semantics

Hi,

The following patch is an attempt to make huge_pte_offset() consistent when dealing with different levels of the page table and document the expected semantics. Previously posting can be found at [0].

Changelog

RFC - v1
* Merge Patch 1 and 2 - preserve bisectability
* Drop RFC tag

Original cover letter follows...

The generic implementation of huge_pte_offset() has inconsistent behaviour when looking up hugepage PUDs vs PMDs entries that are not present (returning NULL vs pte_t*).

Similarly, it returns NULL when encountering swap entries although all the callers have special checks to properly deal with swap entries.

Without clear semantics, it is difficult to determine if a change breaks huge_pte_offset() without going through all the scenarios where it is used.

I faced this recently when updating the arm64 implementation of huge_pte_offset() to handle swap entries (related to enabling poisoned memeory)[1]. And will come across again when I update it for contiguous hugepage support now that core changes have been merged.

To address these issues, this following patch -

* makes huge_pte_offset() consistent between PUD and PMDs
* and, documents the expected behaviour of huge_pte_offset()

All feedback welcome.

Thanks,
Punit

[0] https://lkml.org/lkml/2017/7/24/514
[1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=f02ab08afbe76ee7b0b2a34a9970e7dd200d8b01

Punit Agrawal (1):
mm/hugetlb: Make huge_pte_offset() consistent and document behaviour

 mm/hugetlb.c | 22 +++++++++++++++++++---
 1 file changed, 19 insertions(+), 3 deletions(-)
Signature · 1 line
2.11.0
Reviewed-by on v1 1/1
#2 lore raw

Re: [PATCH 1/1] mm/hugetlb: Make huge_pte_offset() consistent and document behaviour

In reply to Punit Agrawal
Quoted 15 lines · Punit Agrawal

When walking the page tables to resolve an address that points to !p*d_present() entry, huge_pte_offset() returns inconsistent values depending on the level of page table (PUD or PMD).

It returns NULL in the case of a PUD entry while in the case of a PMD entry, it returns a pointer to the page table entry.

A similar inconsitency exists when handling swap entries - returns NULL for a PUD entry while a pointer to the pte_t is retured for the PMD entry.

Update huge_pte_offset() to make the behaviour consistent - return NULL in the case of p*d_none() and a pointer to the pte_t for hugepage or swap entries.

Quoted 5 lines

Document the behaviour to clarify the expected behaviour of this function. This is to set clear semantics for architecture specific implementations of huge_pte_offset().

#3 lore raw

Re: [PATCH 1/1] mm/hugetlb: Make huge_pte_offset() consistent and document behaviour

In reply to Punit Agrawal
Quoted 11 lines · Punit Agrawal

When walking the page tables to resolve an address that points to !p*d_present() entry, huge_pte_offset() returns inconsistent values depending on the level of page table (PUD or PMD).

It returns NULL in the case of a PUD entry while in the case of a PMD entry, it returns a pointer to the page table entry.

A similar inconsitency exists when handling swap entries - returns NULL for a PUD entry while a pointer to the pte_t is retured for the PMD entry.

Quoted 7 lines

Update huge_pte_offset() to make the behaviour consistent - return NULL in the case of p*d_none() and a pointer to the pte_t for hugepage or swap entries.

Document the behaviour to clarify the expected behaviour of this function. This is to set clear semantics for architecture specific implementations of huge_pte_offset().

hugetlb pte semantic is a disaster and I agree it could see some cleanup/clarifications but I am quite nervous to see a patchi like this. How do we check that nothing will get silently broken by this change?

Quoted 52 lines
---
 mm/hugetlb.c | 22 +++++++++++++++++++---
 1 file changed, 19 insertions(+), 3 deletions(-)
mm/hugetlb.c +19 -3
index bc48ee783dd9..72dd1139a8e4 100644
@@ -4603,6 +4603,13 @@ pte_t *huge_pte_alloc(struct mm_struct *mm,
 	return pte;
 }
 
+/*
+ * huge_pte_offset() - Walk the page table to resolve the hugepage
+ * entry at address @addr
+ *
+ * Return: Pointer to page table or swap entry (PUD or PMD) for address @addr
+ * or NULL if the entry is p*d_none().
+ */
 pte_t *huge_pte_offset(struct mm_struct *mm,
 		       unsigned long addr, unsigned long sz)
 {
@@ -4617,13 +4624,22 @@ pte_t *huge_pte_offset(struct mm_struct *mm,
 	p4d = p4d_offset(pgd, addr);
 	if (!p4d_present(*p4d))
 		return NULL;
+
 	pud = pud_offset(p4d, addr);
-	if (!pud_present(*pud))
+	if (pud_none(*pud))
 		return NULL;
-	if (pud_huge(*pud))
+	/* hugepage or swap? */
+	if (pud_huge(*pud) || !pud_present(*pud))
 		return (pte_t *)pud;
+
 	pmd = pmd_offset(pud, addr);
-	return (pte_t *) pmd;
+	if (pmd_none(*pmd))
+		return NULL;
+	/* hugepage or swap? */
+	if (pmd_huge(*pmd) || !pmd_present(*pmd))
+		return (pte_t *) pmd;
+
+	return NULL;
 }
 
 #endif /* CONFIG_ARCH_WANT_GENERAL_HUGETLB */
Signature · 1 line
2.11.0
Signature · 2 lines
Michal Hocko
SUSE Labs
#4 lore raw

Re: [PATCH 1/1] mm/hugetlb: Make huge_pte_offset() consistent and document behaviour

Quoted 16 lines · Michal Hocko
Quoted 15 lines · Punit Agrawal

When walking the page tables to resolve an address that points to !p*d_present() entry, huge_pte_offset() returns inconsistent values depending on the level of page table (PUD or PMD).

It returns NULL in the case of a PUD entry while in the case of a PMD entry, it returns a pointer to the page table entry.

A similar inconsitency exists when handling swap entries - returns NULL for a PUD entry while a pointer to the pte_t is retured for the PMD entry.

Update huge_pte_offset() to make the behaviour consistent - return NULL in the case of p*d_none() and a pointer to the pte_t for hugepage or swap entries.

Quoted 7 lines
Quoted 3 lines

Document the behaviour to clarify the expected behaviour of this function. This is to set clear semantics for architecture specific implementations of huge_pte_offset().

hugetlb pte semantic is a disaster and I agree it could see some cleanup/clarifications but I am quite nervous to see a patchi like this. How do we check that nothing will get silently broken by this change?

Forgot to add. Hugetlb have been special because of the pte sharing. I haven't looked into that code for quite some time but there might be a good reason why pud behave differently.

Signature · 8 lines
Michal Hocko
SUSE Labs

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#5 lore raw

Re: [PATCH 1/1] mm/hugetlb: Make huge_pte_offset() consistent and document behaviour

Hi Michal,

Quoted 17 lines · Michal Hocko
Quoted 16 lines · Michal Hocko
Quoted 15 lines · Punit Agrawal

When walking the page tables to resolve an address that points to !p*d_present() entry, huge_pte_offset() returns inconsistent values depending on the level of page table (PUD or PMD).

It returns NULL in the case of a PUD entry while in the case of a PMD entry, it returns a pointer to the page table entry.

A similar inconsitency exists when handling swap entries - returns NULL for a PUD entry while a pointer to the pte_t is retured for the PMD entry.

Update huge_pte_offset() to make the behaviour consistent - return NULL in the case of p*d_none() and a pointer to the pte_t for hugepage or swap entries.

Quoted 7 lines
Quoted 7 lines
Quoted 3 lines

Document the behaviour to clarify the expected behaviour of this function. This is to set clear semantics for architecture specific implementations of huge_pte_offset().

hugetlb pte semantic is a disaster and I agree it could see some cleanup/clarifications but I am quite nervous to see a patchi like this. How do we check that nothing will get silently broken by this change?

Glad I'm not the only one who finds the hugetlb semantics somewhat confusing. :)

I've been running tests from mce-test suite and libhugetlbfs for similar changes we did on arm64. There could be assumptions that were not exercised but I'm not sure how to check for all the possible usages.

Do you have any other suggestions that can help improve confidence in the patch?

Quoted 4 lines

Forgot to add. Hugetlb have been special because of the pte sharing. I haven't looked into that code for quite some time but there might be a good reason why pud behave differently.

I checked the code and don't see anything that would explain (or require) the difference in behaviour.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#6 lore raw

Re: [PATCH 1/1] mm/hugetlb: Make huge_pte_offset() consistent and document behaviour

Quoted 25 lines · Punit Agrawal

Hi Michal,

Quoted 21 lines · Michal Hocko
Quoted 20 lines · Michal Hocko
Quoted 18 lines · Punit Agrawal

When walking the page tables to resolve an address that points to !p*d_present() entry, huge_pte_offset() returns inconsistent values depending on the level of page table (PUD or PMD).

It returns NULL in the case of a PUD entry while in the case of a PMD entry, it returns a pointer to the page table entry.

A similar inconsitency exists when handling swap entries - returns NULL for a PUD entry while a pointer to the pte_t is retured for the PMD entry.

Update huge_pte_offset() to make the behaviour consistent - return NULL in the case of p*d_none() and a pointer to the pte_t for hugepage or swap entries.

Document the behaviour to clarify the expected behaviour of this function. This is to set clear semantics for architecture specific implementations of huge_pte_offset().

Quoted 6 lines
Quoted 3 lines
Quoted 3 lines

hugetlb pte semantic is a disaster and I agree it could see some cleanup/clarifications but I am quite nervous to see a patchi like this. How do we check that nothing will get silently broken by this change?

Glad I'm not the only one who finds the hugetlb semantics somewhat confusing. :)

This is a huge understatement. It is a source of nightmares.

Quoted 6 lines

I've been running tests from mce-test suite and libhugetlbfs for similar changes we did on arm64. There could be assumptions that were not exercised but I'm not sure how to check for all the possible usages.

Do you have any other suggestions that can help improve confidence in the patch?

Unfortunatelly I don't. I just know there were many subtle assumptions all over the place so I am rather careful to not touch the code unless really necessary.

That being said, I am not opposing your patch.

Signature · 8 lines
Michal Hocko
SUSE Labs

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#7 lore raw

Re: [PATCH 1/1] mm/hugetlb: Make huge_pte_offset() consistent and document behaviour

Quoted 1 line · Michal Hocko

On Wed 26-07-17 13:11:46, Punit Agrawal wrote:

[...]

Quoted 4 lines
Quoted 4 lines

I've been running tests from mce-test suite and libhugetlbfs for similar changes we did on arm64. There could be assumptions that were not exercised but I'm not sure how to check for all the possible usages.

Quoted 8 lines
Quoted 2 lines

Do you have any other suggestions that can help improve confidence in the patch?

Unfortunatelly I don't. I just know there were many subtle assumptions all over the place so I am rather careful to not touch the code unless really necessary.

That being said, I am not opposing your patch.

Let me be more specific. I am not opposing your patch but we should definitely need more reviewers to have a look. I am not seeing any immediate problems with it but I do not see a large improvements either (slightly less nightmare doesn't make me sleep all that well ;)). So I will leave the decisions to others.

Signature · 8 lines
Michal Hocko
SUSE Labs

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#8 lore raw

Re: [PATCH 1/1] mm/hugetlb: Make huge_pte_offset() consistent and document behaviour

Quoted 14 lines · Michal Hocko
Quoted 1 line · Michal Hocko

On Wed 26-07-17 13:11:46, Punit Agrawal wrote:

[...]

Quoted 11 lines
Quoted 6 lines

I've been running tests from mce-test suite and libhugetlbfs for similar changes we did on arm64. There could be assumptions that were not exercised but I'm not sure how to check for all the possible usages.

Do you have any other suggestions that can help improve confidence in the patch?

Unfortunatelly I don't. I just know there were many subtle assumptions all over the place so I am rather careful to not touch the code unless really necessary.

Quoted 7 lines
Quoted 1 line

That being said, I am not opposing your patch.

Let me be more specific. I am not opposing your patch but we should definitely need more reviewers to have a look. I am not seeing any immediate problems with it but I do not see a large improvements either (slightly less nightmare doesn't make me sleep all that well ;)). So I will leave the decisions to others.

I hear you - I'd definitely appreciate more eyes on the code change and description.

Thanks for taking a look.

#9 lore raw

Re: [PATCH 1/1] mm/hugetlb: Make huge_pte_offset() consistent and document behaviour

Quoted 18 lines · Punit Agrawal
Quoted 16 lines · Michal Hocko
Quoted 1 line · Michal Hocko

On Wed 26-07-17 13:11:46, Punit Agrawal wrote:

[...]

Quoted 12 lines
Quoted 6 lines

I've been running tests from mce-test suite and libhugetlbfs for similar changes we did on arm64. There could be assumptions that were not exercised but I'm not sure how to check for all the possible usages.

Do you have any other suggestions that can help improve confidence in the patch?

Unfortunatelly I don't. I just know there were many subtle assumptions all over the place so I am rather careful to not touch the code unless really necessary.

That being said, I am not opposing your patch.

Quoted 8 lines
Quoted 5 lines

Let me be more specific. I am not opposing your patch but we should definitely need more reviewers to have a look. I am not seeing any immediate problems with it but I do not see a large improvements either (slightly less nightmare doesn't make me sleep all that well ;)). So I will leave the decisions to others.

I hear you - I'd definitely appreciate more eyes on the code change and description.

I like the change in semantics for the routine. Like you, I examined all callers of huge_pte_offset() and it appears that they will not be impacted by your change.

My only concern is that arch specific versions of huge_pte_offset, may not (yet) follow the new semantic. Someone could potentially introduce a new huge_pte_offset call and depend on the new 'documented' semantics. Yet, an unmodified arch specific version of huge_pte_offset might have different semantics. I have not reviewed all the arch specific instances of the routine to know if this is even possible. Just curious if you examined these, or perhaps you think this is not an issue?

Signature · 7 lines
Mike Kravetz

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#10 lore raw

Re: [PATCH 1/1] mm/hugetlb: Make huge_pte_offset() consistent and document behaviour

Quoted 32 lines · Mike Kravetz
Quoted 18 lines · Punit Agrawal
Quoted 16 lines · Michal Hocko
Quoted 1 line · Michal Hocko

On Wed 26-07-17 13:11:46, Punit Agrawal wrote:

[...]

Quoted 12 lines
Quoted 6 lines

I've been running tests from mce-test suite and libhugetlbfs for similar changes we did on arm64. There could be assumptions that were not exercised but I'm not sure how to check for all the possible usages.

Do you have any other suggestions that can help improve confidence in the patch?

Unfortunatelly I don't. I just know there were many subtle assumptions all over the place so I am rather careful to not touch the code unless really necessary.

That being said, I am not opposing your patch.

Quoted 8 lines
Quoted 5 lines

Let me be more specific. I am not opposing your patch but we should definitely need more reviewers to have a look. I am not seeing any immediate problems with it but I do not see a large improvements either (slightly less nightmare doesn't make me sleep all that well ;)). So I will leave the decisions to others.

I hear you - I'd definitely appreciate more eyes on the code change and description.

I like the change in semantics for the routine. Like you, I examined all callers of huge_pte_offset() and it appears that they will not be impacted by your change.

Quoted 7 lines

My only concern is that arch specific versions of huge_pte_offset, may not (yet) follow the new semantic. Someone could potentially introduce a new huge_pte_offset call and depend on the new 'documented' semantics. Yet, an unmodified arch specific version of huge_pte_offset might have different semantics. I have not reviewed all the arch specific instances of the routine to know if this is even possible. Just curious if you examined these, or perhaps you think this is not an issue?

From checking through the implementations of huge_pte_offset() architectures, the change shouldn't break anything. (I also cc'd the posting to linux-arch for architecture maintainers to take more notice).

This is because existing users actively deal with the different returned values (NULL, huge pte_t*, swap pte_t*) and are not checking explicitly for pmd or pud.

Guarding against future users is more tricky - it would definitely help to align all the implementations.

#11 lore raw

PATCH v2 1/1 mm/hugetlb.c: make huge_pte_offset() consistent and document behaviour

In reply to Punit Agrawal

When walking the page tables to resolve an address that points to !p*d_present() entry, huge_pte_offset() returns inconsistent values depending on the level of page table (PUD or PMD).

It returns NULL in the case of a PUD entry while in the case of a PMD entry, it returns a pointer to the page table entry.

A similar inconsitency exists when handling swap entries - returns NULL for a PUD entry while a pointer to the pte_t is retured for the PMD entry.

Update huge_pte_offset() to make the behaviour consistent - return a pointer to the pte_t for hugepage or swap entries. Only return NULL in instances where we have a p*d_none() entry and the size parameter doesn't match the hugepage size at this level of the page table.

Document the behaviour to clarify the expected behaviour of this function. This is to set clear semantics for architecture specific implementations of huge_pte_offset().

Cc Catalin Marinas <[email protected]>
Cc Naoya Horiguchi <[email protected]>
Cc Steve Capper <[email protected]>
Cc Will Deacon <[email protected]>
Cc Kirill A. Shutemov <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Mike Kravetz <[email protected]>

---

Hi Andrew,

From discussions on the arm64 implementation of huge_pte_offset()[0] we realised that there is benefit from returning a pte_t* in the case of p*d_none().

The fault handling code in hugetlb_fault() can handle p*d_none() entries and saves an extra round trip to huge_pte_alloc(). Other callers of huge_pte_offset() should be ok as well.

Apologies for sending a late update but I thought if we are defining the semantics, it's worth getting them right.

Could you please pick this version please?

Thanks,
Punit

[0] http://www.spinics.net/lists/linux-mm/msg133699.html

v2:

 mm/hugetlb.c | 24 +++++++++++++++++++++---
 1 file changed, 21 insertions(+), 3 deletions(-)
mm/hugetlb.c +21 -3
index 31e207cb399b..1d54a131bdd5 100644
@@ -4600,6 +4600,15 @@ pte_t *huge_pte_alloc(struct mm_struct *mm,
 	return pte;
 }
 
+/*
+ * huge_pte_offset() - Walk the page table to resolve the hugepage
+ * entry at address @addr
+ *
+ * Return: Pointer to page table or swap entry (PUD or PMD) for
+ * address @addr, or NULL if a p*d_none() entry is encountered and the
+ * size @sz doesn't match the hugepage size at this level of the page
+ * table.
+ */
 pte_t *huge_pte_offset(struct mm_struct *mm,
 		       unsigned long addr, unsigned long sz)
 {
@@ -4614,13 +4623,22 @@ pte_t *huge_pte_offset(struct mm_struct *mm,
 	p4d = p4d_offset(pgd, addr);
 	if (!p4d_present(*p4d))
 		return NULL;
+
 	pud = pud_offset(p4d, addr);
-	if (!pud_present(*pud))
+	if (sz != PUD_SIZE && pud_none(*pud))
 		return NULL;
-	if (pud_huge(*pud))
+	/* hugepage or swap? */
+	if (pud_huge(*pud) || !pud_present(*pud))
 		return (pte_t *)pud;
+
 	pmd = pmd_offset(pud, addr);
-	return (pte_t *) pmd;
+	if (sz != PMD_SIZE && pmd_none(*pmd))
+		return NULL;
+	/* hugepage or swap? */
+	if (pmd_huge(*pmd) || !pmd_present(*pmd))
+		return (pte_t *)pmd;
+
+	return NULL;
 }
 
 #endif /* CONFIG_ARCH_WANT_GENERAL_HUGETLB */
Signature · 1 line
2.13.2
#12 lore raw

Re: [PATCH v2] mm/hugetlb.c: make huge_pte_offset() consistent and document behaviour

Quoted 31 lines · Punit Agrawal

When walking the page tables to resolve an address that points to !p*d_present() entry, huge_pte_offset() returns inconsistent values depending on the level of page table (PUD or PMD).

It returns NULL in the case of a PUD entry while in the case of a PMD entry, it returns a pointer to the page table entry.

A similar inconsitency exists when handling swap entries - returns NULL for a PUD entry while a pointer to the pte_t is retured for the PMD entry.

Update huge_pte_offset() to make the behaviour consistent - return a pointer to the pte_t for hugepage or swap entries. Only return NULL in instances where we have a p*d_none() entry and the size parameter doesn't match the hugepage size at this level of the page table.

Document the behaviour to clarify the expected behaviour of this function. This is to set clear semantics for architecture specific implementations of huge_pte_offset().

Cc Catalin Marinas <[email protected]>
Cc Naoya Horiguchi <[email protected]>
Cc Steve Capper <[email protected]>
Cc Will Deacon <[email protected]>
Cc Kirill A. Shutemov <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Mike Kravetz <[email protected]>

---

Hi Andrew,

Quoted 7 lines

From discussions on the arm64 implementation of huge_pte_offset()[0] we realised that there is benefit from returning a pte_t* in the case of p*d_none().

The fault handling code in hugetlb_fault() can handle p*d_none() entries and saves an extra round trip to huge_pte_alloc(). Other callers of huge_pte_offset() should be ok as well.

Yes, this change would eliminate that call to huge_pte_alloc() in hugetlb_fault(). However, huge_pte_offset() is now returning a pointer to a p*d_none() pte in some instances where it would have previously returned NULL. Correct?

I went through the callers, and like you am fairly confident that they can handle this situation. But, returning p*d_none() instead of NULL does change the execution path in several routines such as copy_hugetlb_page_range, __unmap_hugepage_range hugetlb_change_protection, and follow_hugetlb_page. If huge_pte_alloc() returns NULL to these routines, they do a quick continue, exit, etc. If they are returned a pointer, they typically lock the page table(s) and then check for p*d_none() before continuing, exiting, etc. So, it appears that these routines could potentially slow down a bit with this change (in the specific case of p*d_none).

I 'think' one could argue that the the fault case is more important. So, the savings there would outweigh any potential slowdown in the other routines.

IMO, this new version of the patch has more potential for issues than the previous version. It would be helpful if others could take a look.

One thing I am still 'thinking' about is how this patch could potentially change behavior in huge_pmd_share. With the patch, pmd sharing could potentially be set up in situations (pmd_none) where it previously would not have been set up. I don't think this is an issue, but any changes to this concerns me.

Signature · 1 line
Mike Kravetz
Quoted 53 lines

Apologies for sending a late update but I thought if we are defining the semantics, it's worth getting them right.

Could you please pick this version please?

Thanks,
Punit

[0] http://www.spinics.net/lists/linux-mm/msg133699.html

v2:

 mm/hugetlb.c | 24 +++++++++++++++++++++---
 1 file changed, 21 insertions(+), 3 deletions(-)
mm/hugetlb.c +14 -3
index 31e207cb399b..1d54a131bdd5 100644
@@ -4600,6 +4600,15 @@ pte_t *huge_pte_alloc(struct mm_struct *mm,
 	return pte;
 }
 
+/*
+ * huge_pte_offset() - Walk the page table to resolve the hugepage
+ * entry at address @addr
+ *
+ * Return: Pointer to page table or swap entry (PUD or PMD) for
+ * address @addr, or NULL if a p*d_none() entry is encountered and the
+ * size @sz doesn't match the hugepage size at this level of the page
+ * table.
+ */
 pte_t *huge_pte_offset(struct mm_struct *mm,
 		       unsigned long addr, unsigned long sz)
 {
@@ -4614,13 +4623,22 @@ pte_t *huge_pte_offset(struct mm_struct *mm,
 	p4d = p4d_offset(pgd, addr);
 	if (!p4d_present(*p4d))
 		return NULL;
+
 	pud = pud_offset(p4d, addr);
-	if (!pud_present(*pud))
+	if (sz != PUD_SIZE && pud_none(*pud))
 		return NULL;
-	if (pud_huge(*pud))
+	/* hugepage or swap? */
+	if (pud_huge(*pud) || !pud_present(*pud))
 		return (pte_t *)pud;
+
 	pmd = pmd_offset(pud, addr);
-	return (pte_t *) pmd;

mm/hugetlb.c pte_t *huge_pte_offset(struct mm_struct *mm, · line 4644

Quoted 10 lines
mm/hugetlb.c +7 -0
+	if (sz != PMD_SIZE && pmd_none(*pmd))
+		return NULL;
+	/* hugepage or swap? */
+	if (pmd_huge(*pmd) || !pmd_present(*pmd))
+		return (pte_t *)pmd;
+
+	return NULL;
 }
 
 #endif /* CONFIG_ARCH_WANT_GENERAL_HUGETLB */

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#13 lore raw

Re: [PATCH v2] mm/hugetlb.c: make huge_pte_offset() consistent and document behaviour

Quoted 36 lines · Mike Kravetz
Quoted 35 lines · Punit Agrawal

When walking the page tables to resolve an address that points to !p*d_present() entry, huge_pte_offset() returns inconsistent values depending on the level of page table (PUD or PMD).

It returns NULL in the case of a PUD entry while in the case of a PMD entry, it returns a pointer to the page table entry.

A similar inconsitency exists when handling swap entries - returns NULL for a PUD entry while a pointer to the pte_t is retured for the PMD entry.

Update huge_pte_offset() to make the behaviour consistent - return a pointer to the pte_t for hugepage or swap entries. Only return NULL in instances where we have a p*d_none() entry and the size parameter doesn't match the hugepage size at this level of the page table.

Document the behaviour to clarify the expected behaviour of this function. This is to set clear semantics for architecture specific implementations of huge_pte_offset().

Cc Catalin Marinas <[email protected]>
Cc Naoya Horiguchi <[email protected]>
Cc Steve Capper <[email protected]>
Cc Will Deacon <[email protected]>
Cc Kirill A. Shutemov <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Mike Kravetz <[email protected]>

---

Hi Andrew,

From discussions on the arm64 implementation of huge_pte_offset()[0] we realised that there is benefit from returning a pte_t* in the case of p*d_none().

Quoted 8 lines
Quoted 3 lines

The fault handling code in hugetlb_fault() can handle p*d_none() entries and saves an extra round trip to huge_pte_alloc(). Other callers of huge_pte_offset() should be ok as well.

Yes, this change would eliminate that call to huge_pte_alloc() in hugetlb_fault(). However, huge_pte_offset() is now returning a pointer to a p*d_none() pte in some instances where it would have previously returned NULL. Correct?

Yes (whether it was previously the right thing to return is a different matter; that's what we are trying to clarify in the generic code so that we can have similar semantics on arm64).

Quoted 10 lines

I went through the callers, and like you am fairly confident that they can handle this situation. But, returning p*d_none() instead of NULL does change the execution path in several routines such as copy_hugetlb_page_range, __unmap_hugepage_range hugetlb_change_protection, and follow_hugetlb_page. If huge_pte_alloc() returns NULL to these routines, they do a quick continue, exit, etc. If they are returned a pointer, they typically lock the page table(s) and then check for p*d_none() before continuing, exiting, etc. So, it appears that these routines could potentially slow down a bit with this change (in the specific case of p*d_none).

Arguably (well, my interpretation), it should return a NULL only if the entry is a table entry, potentially pointing to a next level (pmd). In the pud case, this means that sz < PUD_SIZE.

If the pud is a last level huge page entry (either present or !present), huge_pte_offset() should return the pointer to it and never NULL. If the entry is a swap or migration one (pte_present() == false) with the current code we don't even enter the corresponding checks in copy_hugetlb_page_range().

I also assume that the ptl __unmap_hugepage_range() is taken to avoid some race when the entry is a huge page (present or not). If such race doesn't exist, we could as well check the huge_pte_none() outside the locked region (which is what the current huge_pte_offset() does with !pud_present()).

IMHO, while the current generic huge_pte_offset() avoids some code paths in the functions you mentioned, the results are not always correct (missing swap/migration entries or potentially racy).

Signature · 1 line
Catalin
Reviewed-by
#14 lore raw

Re: [PATCH v2] mm/hugetlb.c: make huge_pte_offset() consistent and document behaviour

Quoted 77 lines · Catalin Marinas
Quoted 36 lines · Mike Kravetz
Quoted 35 lines · Punit Agrawal

When walking the page tables to resolve an address that points to !p*d_present() entry, huge_pte_offset() returns inconsistent values depending on the level of page table (PUD or PMD).

It returns NULL in the case of a PUD entry while in the case of a PMD entry, it returns a pointer to the page table entry.

A similar inconsitency exists when handling swap entries - returns NULL for a PUD entry while a pointer to the pte_t is retured for the PMD entry.

Update huge_pte_offset() to make the behaviour consistent - return a pointer to the pte_t for hugepage or swap entries. Only return NULL in instances where we have a p*d_none() entry and the size parameter doesn't match the hugepage size at this level of the page table.

Document the behaviour to clarify the expected behaviour of this function. This is to set clear semantics for architecture specific implementations of huge_pte_offset().

Cc Catalin Marinas <[email protected]>
Cc Naoya Horiguchi <[email protected]>
Cc Steve Capper <[email protected]>
Cc Will Deacon <[email protected]>
Cc Kirill A. Shutemov <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Mike Kravetz <[email protected]>

---

Hi Andrew,

From discussions on the arm64 implementation of huge_pte_offset()[0] we realised that there is benefit from returning a pte_t* in the case of p*d_none().

Quoted 8 lines
Quoted 3 lines

The fault handling code in hugetlb_fault() can handle p*d_none() entries and saves an extra round trip to huge_pte_alloc(). Other callers of huge_pte_offset() should be ok as well.

Yes, this change would eliminate that call to huge_pte_alloc() in hugetlb_fault(). However, huge_pte_offset() is now returning a pointer to a p*d_none() pte in some instances where it would have previously returned NULL. Correct?

Yes (whether it was previously the right thing to return is a different matter; that's what we are trying to clarify in the generic code so that we can have similar semantics on arm64).

Quoted 10 lines

I went through the callers, and like you am fairly confident that they can handle this situation. But, returning p*d_none() instead of NULL does change the execution path in several routines such as copy_hugetlb_page_range, __unmap_hugepage_range hugetlb_change_protection, and follow_hugetlb_page. If huge_pte_alloc() returns NULL to these routines, they do a quick continue, exit, etc. If they are returned a pointer, they typically lock the page table(s) and then check for p*d_none() before continuing, exiting, etc. So, it appears that these routines could potentially slow down a bit with this change (in the specific case of p*d_none).

Arguably (well, my interpretation), it should return a NULL only if the entry is a table entry, potentially pointing to a next level (pmd). In the pud case, this means that sz < PUD_SIZE.

If the pud is a last level huge page entry (either present or !present), huge_pte_offset() should return the pointer to it and never NULL. If the entry is a swap or migration one (pte_present() == false) with the current code we don't even enter the corresponding checks in copy_hugetlb_page_range().

I also assume that the ptl __unmap_hugepage_range() is taken to avoid some race when the entry is a huge page (present or not). If such race doesn't exist, we could as well check the huge_pte_none() outside the locked region (which is what the current huge_pte_offset() does with !pud_present()).

Quoted 3 lines

IMHO, while the current generic huge_pte_offset() avoids some code paths in the functions you mentioned, the results are not always correct (missing swap/migration entries or potentially racy).

Thanks Catalin,

The more I look at this code and think about it, the more I like it. As Michal previously mentioned, changes in this area can break things in subtle ways. That is why I was cautious and asked for more people to look at it. My primary concerns with these changes in this area were:
- Any potential changes in behavior. I think this has been sufficiently explored. While there may be small differences in behavior (for the better), this change should not introduce any bugs/breakage.
- Other arch specific implementations are not aligned with the new behavior. Again, this should not cause any issues. Punit (and I) have looked at the arch specific implementations for issues and found none. In addition, since we are not changing any of the 'calling code', no issues should be introduced for arch specific implementations.

I like the new semantics and did not find any issues.

Signature · 7 lines
Mike Kravetz

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Reviewed-by on v2 1/1
#16 lore raw

Re: [PATCH v2] mm/hugetlb.c: make huge_pte_offset() consistent and document behaviour

In reply to Punit Agrawal
Quoted 19 lines · Punit Agrawal

When walking the page tables to resolve an address that points to !p*d_present() entry, huge_pte_offset() returns inconsistent values depending on the level of page table (PUD or PMD).

It returns NULL in the case of a PUD entry while in the case of a PMD entry, it returns a pointer to the page table entry.

A similar inconsitency exists when handling swap entries - returns NULL for a PUD entry while a pointer to the pte_t is retured for the PMD entry.

Update huge_pte_offset() to make the behaviour consistent - return a pointer to the pte_t for hugepage or swap entries. Only return NULL in instances where we have a p*d_none() entry and the size parameter doesn't match the hugepage size at this level of the page table.

Document the behaviour to clarify the expected behaviour of this function. This is to set clear semantics for architecture specific implementations of huge_pte_offset().

Quoted 8 lines
Cc Catalin Marinas <[email protected]>
Cc Naoya Horiguchi <[email protected]>
Cc Steve Capper <[email protected]>
Cc Will Deacon <[email protected]>
Cc Kirill A. Shutemov <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Mike Kravetz <[email protected]>

FWIW:

Thanks.

#15 lore raw

Re: [PATCH v2] mm/hugetlb.c: make huge_pte_offset() consistent and document behaviour

In reply to Mike Kravetz

Hi Mike,

Quoted 97 lines · Mike Kravetz
Quoted 77 lines · Catalin Marinas
Quoted 36 lines · Mike Kravetz
Quoted 35 lines · Punit Agrawal

When walking the page tables to resolve an address that points to !p*d_present() entry, huge_pte_offset() returns inconsistent values depending on the level of page table (PUD or PMD).

It returns NULL in the case of a PUD entry while in the case of a PMD entry, it returns a pointer to the page table entry.

A similar inconsitency exists when handling swap entries - returns NULL for a PUD entry while a pointer to the pte_t is retured for the PMD entry.

Update huge_pte_offset() to make the behaviour consistent - return a pointer to the pte_t for hugepage or swap entries. Only return NULL in instances where we have a p*d_none() entry and the size parameter doesn't match the hugepage size at this level of the page table.

Document the behaviour to clarify the expected behaviour of this function. This is to set clear semantics for architecture specific implementations of huge_pte_offset().

Cc Catalin Marinas <[email protected]>
Cc Naoya Horiguchi <[email protected]>
Cc Steve Capper <[email protected]>
Cc Will Deacon <[email protected]>
Cc Kirill A. Shutemov <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Mike Kravetz <[email protected]>

---

Hi Andrew,

From discussions on the arm64 implementation of huge_pte_offset()[0] we realised that there is benefit from returning a pte_t* in the case of p*d_none().

Quoted 8 lines
Quoted 3 lines

The fault handling code in hugetlb_fault() can handle p*d_none() entries and saves an extra round trip to huge_pte_alloc(). Other callers of huge_pte_offset() should be ok as well.

Yes, this change would eliminate that call to huge_pte_alloc() in hugetlb_fault(). However, huge_pte_offset() is now returning a pointer to a p*d_none() pte in some instances where it would have previously returned NULL. Correct?

Yes (whether it was previously the right thing to return is a different matter; that's what we are trying to clarify in the generic code so that we can have similar semantics on arm64).

Quoted 10 lines

I went through the callers, and like you am fairly confident that they can handle this situation. But, returning p*d_none() instead of NULL does change the execution path in several routines such as copy_hugetlb_page_range, __unmap_hugepage_range hugetlb_change_protection, and follow_hugetlb_page. If huge_pte_alloc() returns NULL to these routines, they do a quick continue, exit, etc. If they are returned a pointer, they typically lock the page table(s) and then check for p*d_none() before continuing, exiting, etc. So, it appears that these routines could potentially slow down a bit with this change (in the specific case of p*d_none).

Arguably (well, my interpretation), it should return a NULL only if the entry is a table entry, potentially pointing to a next level (pmd). In the pud case, this means that sz < PUD_SIZE.

If the pud is a last level huge page entry (either present or !present), huge_pte_offset() should return the pointer to it and never NULL. If the entry is a swap or migration one (pte_present() == false) with the current code we don't even enter the corresponding checks in copy_hugetlb_page_range().

I also assume that the ptl __unmap_hugepage_range() is taken to avoid some race when the entry is a huge page (present or not). If such race doesn't exist, we could as well check the huge_pte_none() outside the locked region (which is what the current huge_pte_offset() does with !pud_present()).

Quoted 3 lines

IMHO, while the current generic huge_pte_offset() avoids some code paths in the functions you mentioned, the results are not always correct (missing swap/migration entries or potentially racy).

Thanks Catalin,

The more I look at this code and think about it, the more I like it. As Michal previously mentioned, changes in this area can break things in subtle ways. That is why I was cautious and asked for more people to look at it. My primary concerns with these changes in this area were:
- Any potential changes in behavior. I think this has been sufficiently explored. While there may be small differences in behavior (for the better), this change should not introduce any bugs/breakage.
- Other arch specific implementations are not aligned with the new behavior. Again, this should not cause any issues. Punit (and I) have looked at the arch specific implementations for issues and found none. In addition, since we are not changing any of the 'calling code', no issues should be introduced for arch specific implementations.

Quoted 3 lines

I like the new semantics and did not find any issues.

Thanks for reviewing the updated semantics against existing usage. I'll monitor the lists for any reported breakage but please do shout out if you notice any issues.

Thanks,
Punit

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

Acked-by on v2 1/1
#17 lore raw

Re: [PATCH v2] mm/hugetlb.c: make huge_pte_offset() consistent and document behaviour

In reply to Punit Agrawal
Quoted 19 lines · Punit Agrawal

When walking the page tables to resolve an address that points to !p*d_present() entry, huge_pte_offset() returns inconsistent values depending on the level of page table (PUD or PMD).

It returns NULL in the case of a PUD entry while in the case of a PMD entry, it returns a pointer to the page table entry.

A similar inconsitency exists when handling swap entries - returns NULL for a PUD entry while a pointer to the pte_t is retured for the PMD entry.

Update huge_pte_offset() to make the behaviour consistent - return a pointer to the pte_t for hugepage or swap entries. Only return NULL in instances where we have a p*d_none() entry and the size parameter doesn't match the hugepage size at this level of the page table.

Document the behaviour to clarify the expected behaviour of this function. This is to set clear semantics for architecture specific implementations of huge_pte_offset().

Quoted 8 lines
Cc Catalin Marinas <[email protected]>
Cc Naoya Horiguchi <[email protected]>
Cc Steve Capper <[email protected]>
Cc Will Deacon <[email protected]>
Cc Kirill A. Shutemov <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Mike Kravetz <[email protected]>

I always thought that the weird semantic is a result of the hugetlb pte sharing. But now that I dug into history it has been added by 02b0ccef903e ("[PATCH] hugetlb: check p?d_present in huge_pte_offset()") for a completely different reason. I suspec the weird semantic just wasn't noticed back then.

Anyway, I didn't find any problem with the patch

Quoted 76 lines

---

Hi Andrew,

Quoted 1 line

From discussions on the arm64 implementation of huge_pte_offset()[0]

we realised that there is benefit from returning a pte_t* in the case of p*d_none().

The fault handling code in hugetlb_fault() can handle p*d_none() entries and saves an extra round trip to huge_pte_alloc(). Other callers of huge_pte_offset() should be ok as well.

Apologies for sending a late update but I thought if we are defining the semantics, it's worth getting them right.

Could you please pick this version please?

Thanks,
Punit

[0] http://www.spinics.net/lists/linux-mm/msg133699.html

v2:

 mm/hugetlb.c | 24 +++++++++++++++++++++---
 1 file changed, 21 insertions(+), 3 deletions(-)
mm/hugetlb.c +21 -3
index 31e207cb399b..1d54a131bdd5 100644
@@ -4600,6 +4600,15 @@ pte_t *huge_pte_alloc(struct mm_struct *mm,
 	return pte;
 }
 
+/*
+ * huge_pte_offset() - Walk the page table to resolve the hugepage
+ * entry at address @addr
+ *
+ * Return: Pointer to page table or swap entry (PUD or PMD) for
+ * address @addr, or NULL if a p*d_none() entry is encountered and the
+ * size @sz doesn't match the hugepage size at this level of the page
+ * table.
+ */
 pte_t *huge_pte_offset(struct mm_struct *mm,
 		       unsigned long addr, unsigned long sz)
 {
@@ -4614,13 +4623,22 @@ pte_t *huge_pte_offset(struct mm_struct *mm,
 	p4d = p4d_offset(pgd, addr);
 	if (!p4d_present(*p4d))
 		return NULL;
+
 	pud = pud_offset(p4d, addr);
-	if (!pud_present(*pud))
+	if (sz != PUD_SIZE && pud_none(*pud))
 		return NULL;
-	if (pud_huge(*pud))
+	/* hugepage or swap? */
+	if (pud_huge(*pud) || !pud_present(*pud))
 		return (pte_t *)pud;
+
 	pmd = pmd_offset(pud, addr);
-	return (pte_t *) pmd;
+	if (sz != PMD_SIZE && pmd_none(*pmd))
+		return NULL;
+	/* hugepage or swap? */
+	if (pmd_huge(*pmd) || !pmd_present(*pmd))
+		return (pte_t *)pmd;
+
+	return NULL;
 }
 
 #endif /* CONFIG_ARCH_WANT_GENERAL_HUGETLB */
Signature · 1 line
2.13.2
Signature · 8 lines
Michal Hocko
SUSE Labs

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>