Skip to contentSkip to conversation

arm64: mm: Drop redundant check in pmd_trans_huge()

PATCH Applied Gavin Shan 3 participants 4 messages May 8, 2025 – May 16, 2025 arm64

lore
Share & tools
lore

Gavin Shan

author
#0 lore raw

PATCH v2 1/1 arm64: mm: Drop redundant check in pmd_trans_huge()

pmd_val(pmd) is redundant because a positive pmd_present(pmd) ensures a positive pmd_val(pmd) according to their definitions like below.

  #define pmd_val(x)       ((x).pmd)
  #define pmd_present(pmd) pte_present(pmd_pte(pmd))
  #define pte_present(pte) (pte_valid(pte) || pte_present_invalid(pte))
  #define pte_valid(pte)   (!!(pte_val(pte) & PTE_VALID))
  #define pte_present_invalid(pte) \
          ((pte_val(pte) & (PTE_VALID | PTE_PRESENT_INVALID)) == PTE_PRESENT_INVALID)

pte_present() can't be positive unless either of the flag PTE_VALID or PTE_PRESENT_INVALID is set. In this case, pmd_val(pmd) should be positive either.

So lets drop the redundant check pmd_val(pmd) and no functional changes intended.

---
v1: https://lore.kernel.org/linux-arm-kernel/[email protected]/T/#u
v2: Improved commit log per Anshuman

---
 arch/arm64/include/asm/pgtable.h | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
arch/arm64/include/asm/pgtable.h +1 -2
index d3b538be1500..2599b9b8666f 100644
@@ -739,8 +739,7 @@ static inline int pmd_trans_huge(pmd_t pmd)
 	 * If pmd is present-invalid, pmd_table() won't detect it
 	 * as a table, so force the valid bit for the comparison.
 	 */
-	return pmd_val(pmd) && pmd_present(pmd) &&
-	       !pmd_table(__pmd(pmd_val(pmd) | PTE_VALID));
+	return pmd_present(pmd) && !pmd_table(__pmd(pmd_val(pmd) | PTE_VALID));
 }
 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
 
Signature · 1 line
2.49.0
Reviewed-by on v2 1/1
#1 lore raw
Quoted 32 lines · Gavin Shan

pmd_val(pmd) is redundant because a positive pmd_present(pmd) ensures a positive pmd_val(pmd) according to their definitions like below.

  #define pmd_val(x)       ((x).pmd)
  #define pmd_present(pmd) pte_present(pmd_pte(pmd))
  #define pte_present(pte) (pte_valid(pte) || pte_present_invalid(pte))
  #define pte_valid(pte)   (!!(pte_val(pte) & PTE_VALID))
  #define pte_present_invalid(pte) \
          ((pte_val(pte) & (PTE_VALID | PTE_PRESENT_INVALID)) == PTE_PRESENT_INVALID)

pte_present() can't be positive unless either of the flag PTE_VALID or PTE_PRESENT_INVALID is set. In this case, pmd_val(pmd) should be positive either.

So lets drop the redundant check pmd_val(pmd) and no functional changes intended.

---
v1: https://lore.kernel.org/linux-arm-kernel/[email protected]/T/#u
v2: Improved commit log per Anshuman

---
 arch/arm64/include/asm/pgtable.h | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
arch/arm64/include/asm/pgtable.h +0 -0
index d3b538be1500..2599b9b8666f 100644

arch/arm64/include/asm/pgtable.h static inline int pmd_trans_huge(pmd_t pmd) · line 745

Quoted 9 lines
arch/arm64/include/asm/pgtable.h +1 -2
@@ -739,8 +739,7 @@ static inline int pmd_trans_huge(pmd_t pmd)
 	 * If pmd is present-invalid, pmd_table() won't detect it
 	 * as a table, so force the valid bit for the comparison.
 	 */
-	return pmd_val(pmd) && pmd_present(pmd) &&
-	       !pmd_table(__pmd(pmd_val(pmd) | PTE_VALID));
+	return pmd_present(pmd) && !pmd_table(__pmd(pmd_val(pmd) | PTE_VALID));
 }
 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
 

I agree the cleanup is useful and correct, so:

But personally I find it maddening that we have:

#define pmd_table(pmd) ((pmd_val(pmd) & PMD_TYPE_MASK) == \
PMD_TYPE_TABLE)
#define pmd_sect(pmd) ((pmd_val(pmd) & PMD_TYPE_MASK) == \
PMD_TYPE_SECT)
#define pmd_leaf(pmd) (pmd_present(pmd) && !pmd_table(pmd))

static inline int pmd_trans_huge(pmd_t pmd)

{
	/*
	 * If pmd is present-invalid, pmd_table() won't detect it
	 * as a table, so force the valid bit for the comparison.
	 */
	return pmd_val(pmd) && pmd_present(pmd) &&
	       !pmd_table(__pmd(pmd_val(pmd) | PTE_VALID));
}

Which all do basically the same thing, but with some very subtle differences.

Surely we should really only need 2 basic functions; is it a table? is it a leaf? Then pmd_sect() and pmd_trans_huge() should just be aliases of pmd_leaf().

I *think* pmd_sect() and pmd_leaf() are really just 2 different ways of expressing the same thing? Which is "Is this a *VALID* leaf?"

And pmd_trans_huge() is asking "Is this either a *VALID* or *PRESENT_INVALID* leaf?"

I'm not sure if we can relax pmd_sect()/pmd_leaf() to give the same semantics as pmd_trans_huge()? I would guess we can. In which case it would be nice to clean this all up to a single implementation and make the others wrappers. Or better yet, fix the callers to consistently use pmd_leaf(). And do the same for pud.

Thanks,
Ryan

author
#2 lore raw
Quoted 45 lines · Ryan Roberts
Quoted 32 lines · Gavin Shan

pmd_val(pmd) is redundant because a positive pmd_present(pmd) ensures a positive pmd_val(pmd) according to their definitions like below.

   #define pmd_val(x)       ((x).pmd)
   #define pmd_present(pmd) pte_present(pmd_pte(pmd))
   #define pte_present(pte) (pte_valid(pte) || pte_present_invalid(pte))
   #define pte_valid(pte)   (!!(pte_val(pte) & PTE_VALID))
   #define pte_present_invalid(pte) \
           ((pte_val(pte) & (PTE_VALID | PTE_PRESENT_INVALID)) == PTE_PRESENT_INVALID)

pte_present() can't be positive unless either of the flag PTE_VALID or PTE_PRESENT_INVALID is set. In this case, pmd_val(pmd) should be positive either.

So lets drop the redundant check pmd_val(pmd) and no functional changes intended.

---
v1: https://lore.kernel.org/linux-arm-kernel/[email protected]/T/#u
v2: Improved commit log per Anshuman

---
  arch/arm64/include/asm/pgtable.h | 3 +--
  1 file changed, 1 insertion(+), 2 deletions(-)
arch/arm64/include/asm/pgtable.h +0 -0
index d3b538be1500..2599b9b8666f 100644

arch/arm64/include/asm/pgtable.h static inline int pmd_trans_huge(pmd_t pmd) · line 745

Quoted 9 lines
arch/arm64/include/asm/pgtable.h +1 -2
@@ -739,8 +739,7 @@ static inline int pmd_trans_huge(pmd_t pmd)
  	 * If pmd is present-invalid, pmd_table() won't detect it
  	 * as a table, so force the valid bit for the comparison.
  	 */
-	return pmd_val(pmd) && pmd_present(pmd) &&
-	       !pmd_table(__pmd(pmd_val(pmd) | PTE_VALID));
+	return pmd_present(pmd) && !pmd_table(__pmd(pmd_val(pmd) | PTE_VALID));
  }
  #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
 

I agree the cleanup is useful and correct, so:

Quoted 2 lines

Thanks.

Quoted 27 lines

But personally I find it maddening that we have:

#define pmd_table(pmd) ((pmd_val(pmd) & PMD_TYPE_MASK) == \
PMD_TYPE_TABLE)
#define pmd_sect(pmd) ((pmd_val(pmd) & PMD_TYPE_MASK) == \
PMD_TYPE_SECT)
#define pmd_leaf(pmd) (pmd_present(pmd) && !pmd_table(pmd))

static inline int pmd_trans_huge(pmd_t pmd)

{
	/*
	 * If pmd is present-invalid, pmd_table() won't detect it
	 * as a table, so force the valid bit for the comparison.
	 */
	return pmd_val(pmd) && pmd_present(pmd) &&
	       !pmd_table(__pmd(pmd_val(pmd) | PTE_VALID));
}

Which all do basically the same thing, but with some very subtle differences.

Surely we should really only need 2 basic functions; is it a table? is it a leaf? Then pmd_sect() and pmd_trans_huge() should just be aliases of pmd_leaf().

I *think* pmd_sect() and pmd_leaf() are really just 2 different ways of expressing the same thing? Which is "Is this a *VALID* leaf?"

Quoted 7 lines

And pmd_trans_huge() is asking "Is this either a *VALID* or *PRESENT_INVALID* leaf?"

I'm not sure if we can relax pmd_sect()/pmd_leaf() to give the same semantics as pmd_trans_huge()? I would guess we can. In which case it would be nice to clean this all up to a single implementation and make the others wrappers. Or better yet, fix the callers to consistently use pmd_leaf(). And do the same for pud.

Right, pmd_trans_huge() is the superset of pmd_sect() and pmd_leaf(). The later two macros are equivalent in logic. I had a quick check on the sites where pmd_sect() and pmd_leaf() are called, there seems no PRESENT_INVALID case. So I think it would be safe to sync up pmd_leaf() to pmd_trans_huges(), and then make the later one as an alias to pmd_leaf(). Besides, pmd_sect() needs to be replaced by pmd_leaf() as you're suggesting.

Hopefully, I don't miss anything before I'm going to prepare the patches, which will be stacked up on current one. I'm understanding you want something (for PMD) like below.

----

static inline int pmd_leaf(pmd_t pmd)

{
    /*
     * If pmd is present-invalid, pmd_table() won't detect it
     * as a table, so force the valid bit for the comparison.
     */
    return pmd_present(pmd) && !pmd_table(__pmd(pmd_val(pmd) | PTE_VALID));
}

#ifdef CONFIG_TRANSPARENT_HUGEPAGE
#define pmd_trans_huge(pmd) pmd_leaf(pmd)
#endif

With above changes, replace pmd_sect() with pmd_leaf() so that pmd_sect() can be completely dropped.

Thanks,
Gavin

maintainer Applied
#3 lore raw
In reply to Gavin Shan
Quoted 11 lines · Gavin Shan

pmd_val(pmd) is redundant because a positive pmd_present(pmd) ensures a positive pmd_val(pmd) according to their definitions like below.

  #define pmd_val(x)       ((x).pmd)
  #define pmd_present(pmd) pte_present(pmd_pte(pmd))
  #define pte_present(pte) (pte_valid(pte) || pte_present_invalid(pte))
  #define pte_valid(pte)   (!!(pte_val(pte) & PTE_VALID))
  #define pte_present_invalid(pte) \
          ((pte_val(pte) & (PTE_VALID | PTE_PRESENT_INVALID)) == PTE_PRESENT_INVALID)

[...]

Applied to arm64 (for-next/mm), thanks!

[1/1] arm64: mm: Drop redundant check in pmd_trans_huge()

      https://git.kernel.org/arm64/c/13c63ce35883

Cheers,

Signature · 5 lines
Will

https://fixes.arm64.dev
https://next.arm64.dev
https://will.arm64.dev