diff mbox

[V5,1/2] arm: mm: Introduce {pte, pmd}_isset and {pte, pmd}_isclear

Message ID 1403612604-2645-2-git-send-email-steve.capper@linaro.org (mailing list archive)
State New, archived
Headers show

Commit Message

Steve Capper June 24, 2014, 12:23 p.m. UTC
Long descriptors on ARM are 64 bits, and some pte functions such as
pte_dirty return a bitwise-and of a flag with the pte value. If the
flag to be tested resides in the upper 32 bits of the pte, then we run
into the danger of the result being dropped if downcast.

For example:
	gather_stats(page, md, pte_dirty(*pte), 1);
where pte_dirty(*pte) is downcast to an int.

This patch introduces a new macro pte_isset which performs the bitwise
and, then performs a double logical invert (where needed) to ensure
predictable downcasting. The logical inverse pte_isclear is also
introduced.

Equivalent pmd functions for Transparent HugePages have also been
added.

Signed-off-by: Steve Capper <steve.capper@linaro.org>
---
Changed in V5: routed pmd_trans_splitting through the pmd_isset logic.
---
 arch/arm/include/asm/pgtable-3level.h | 12 ++++++++----
 arch/arm/include/asm/pgtable.h        | 18 +++++++++++-------
 2 files changed, 19 insertions(+), 11 deletions(-)

Comments

Will Deacon June 27, 2014, 11:24 a.m. UTC | #1
Hi Steve,

On Tue, Jun 24, 2014 at 01:23:23PM +0100, Steve Capper wrote:
> Long descriptors on ARM are 64 bits, and some pte functions such as
> pte_dirty return a bitwise-and of a flag with the pte value. If the
> flag to be tested resides in the upper 32 bits of the pte, then we run
> into the danger of the result being dropped if downcast.
> 
> For example:
> 	gather_stats(page, md, pte_dirty(*pte), 1);
> where pte_dirty(*pte) is downcast to an int.
> 
> This patch introduces a new macro pte_isset which performs the bitwise
> and, then performs a double logical invert (where needed) to ensure
> predictable downcasting. The logical inverse pte_isclear is also
> introduced.
> 
> Equivalent pmd functions for Transparent HugePages have also been
> added.


[...]

> diff --git a/arch/arm/include/asm/pgtable-3level.h b/arch/arm/include/asm/pgtable-3level.h
> index 85c60ad..3b10ec6 100644
> --- a/arch/arm/include/asm/pgtable-3level.h
> +++ b/arch/arm/include/asm/pgtable-3level.h
> @@ -207,17 +207,21 @@ static inline pmd_t *pmd_offset(pud_t *pud, unsigned long addr)
>  #define pte_huge(pte)		(pte_val(pte) && !(pte_val(pte) & PTE_TABLE_BIT))
>  #define pte_mkhuge(pte)		(__pte(pte_val(pte) & ~PTE_TABLE_BIT))
>  
> -#define pmd_young(pmd)		(pmd_val(pmd) & PMD_SECT_AF)
> +#define pmd_isset(pmd, val)	((u32)(val) == (val) ? pmd_val(pmd) & (val) \
> +						: !!(pmd_val(pmd) & (val)))
> +#define pmd_isclear(pmd, val)	(!(pmd_val(pmd) & (val)))
> +
> +#define pmd_young(pmd)		(pmd_isset((pmd), PMD_SECT_AF))
>  
>  #define __HAVE_ARCH_PMD_WRITE
> -#define pmd_write(pmd)		(!(pmd_val(pmd) & PMD_SECT_RDONLY))
> +#define pmd_write(pmd)		(pmd_isclear((pmd), PMD_SECT_RDONLY))
>  
>  #define pmd_hugewillfault(pmd)	(!pmd_young(pmd) || !pmd_write(pmd))
>  #define pmd_thp_or_huge(pmd)	(pmd_huge(pmd) || pmd_trans_huge(pmd))
>  
>  #ifdef CONFIG_TRANSPARENT_HUGEPAGE
> -#define pmd_trans_huge(pmd)	(pmd_val(pmd) && !(pmd_val(pmd) & PMD_TABLE_BIT))
> -#define pmd_trans_splitting(pmd) (pmd_val(pmd) & PMD_SECT_SPLITTING)
> +#define pmd_trans_huge(pmd)	(pmd_val(pmd) && pmd_isclear((pmd), PMD_TABLE_BIT))

Why isn't this just pmd_present(pmd) && !pmd_table(pmd)? Put another way, I
see to have forgotten why we need PMD_TABLE_BIT instead of just using
pmd_table and pmd_sect to work out whether we have a table or a block.

Will
Steve Capper June 27, 2014, 12:24 p.m. UTC | #2
On Fri, Jun 27, 2014 at 12:24:20PM +0100, Will Deacon wrote:
> Hi Steve,

Hey Will,

> 
> On Tue, Jun 24, 2014 at 01:23:23PM +0100, Steve Capper wrote:
> > Long descriptors on ARM are 64 bits, and some pte functions such as
> > pte_dirty return a bitwise-and of a flag with the pte value. If the
> > flag to be tested resides in the upper 32 bits of the pte, then we run
> > into the danger of the result being dropped if downcast.
> > 
> > For example:
> > 	gather_stats(page, md, pte_dirty(*pte), 1);
> > where pte_dirty(*pte) is downcast to an int.
> > 
> > This patch introduces a new macro pte_isset which performs the bitwise
> > and, then performs a double logical invert (where needed) to ensure
> > predictable downcasting. The logical inverse pte_isclear is also
> > introduced.
> > 
> > Equivalent pmd functions for Transparent HugePages have also been
> > added.
> 
> 
> [...]
> 
> > diff --git a/arch/arm/include/asm/pgtable-3level.h b/arch/arm/include/asm/pgtable-3level.h
> > index 85c60ad..3b10ec6 100644
> > --- a/arch/arm/include/asm/pgtable-3level.h
> > +++ b/arch/arm/include/asm/pgtable-3level.h
> > @@ -207,17 +207,21 @@ static inline pmd_t *pmd_offset(pud_t *pud, unsigned long addr)
> >  #define pte_huge(pte)		(pte_val(pte) && !(pte_val(pte) & PTE_TABLE_BIT))
> >  #define pte_mkhuge(pte)		(__pte(pte_val(pte) & ~PTE_TABLE_BIT))
> >  
> > -#define pmd_young(pmd)		(pmd_val(pmd) & PMD_SECT_AF)
> > +#define pmd_isset(pmd, val)	((u32)(val) == (val) ? pmd_val(pmd) & (val) \
> > +						: !!(pmd_val(pmd) & (val)))
> > +#define pmd_isclear(pmd, val)	(!(pmd_val(pmd) & (val)))
> > +
> > +#define pmd_young(pmd)		(pmd_isset((pmd), PMD_SECT_AF))
> >  
> >  #define __HAVE_ARCH_PMD_WRITE
> > -#define pmd_write(pmd)		(!(pmd_val(pmd) & PMD_SECT_RDONLY))
> > +#define pmd_write(pmd)		(pmd_isclear((pmd), PMD_SECT_RDONLY))
> >  
> >  #define pmd_hugewillfault(pmd)	(!pmd_young(pmd) || !pmd_write(pmd))
> >  #define pmd_thp_or_huge(pmd)	(pmd_huge(pmd) || pmd_trans_huge(pmd))
> >  
> >  #ifdef CONFIG_TRANSPARENT_HUGEPAGE
> > -#define pmd_trans_huge(pmd)	(pmd_val(pmd) && !(pmd_val(pmd) & PMD_TABLE_BIT))
> > -#define pmd_trans_splitting(pmd) (pmd_val(pmd) & PMD_SECT_SPLITTING)
> > +#define pmd_trans_huge(pmd)	(pmd_val(pmd) && pmd_isclear((pmd), PMD_TABLE_BIT))
> 
> Why isn't this just pmd_present(pmd) && !pmd_table(pmd)? Put another way, I
> see to have forgotten why we need PMD_TABLE_BIT instead of just using
> pmd_table and pmd_sect to work out whether we have a table or a block.
> 

If we use pmd_sect, we are testing for block entries, but THPs are allowed to
be faulting entries (i.e. PROT_NONE). A non-zero pmd that does not have the
table bit set can safely be assumed to be a PROT_NONE THP.

Cheers,
Will Deacon June 27, 2014, 12:34 p.m. UTC | #3
On Fri, Jun 27, 2014 at 01:24:00PM +0100, Steve Capper wrote:
> On Fri, Jun 27, 2014 at 12:24:20PM +0100, Will Deacon wrote:
> > On Tue, Jun 24, 2014 at 01:23:23PM +0100, Steve Capper wrote:
> > >  #ifdef CONFIG_TRANSPARENT_HUGEPAGE
> > > -#define pmd_trans_huge(pmd)	(pmd_val(pmd) && !(pmd_val(pmd) & PMD_TABLE_BIT))
> > > -#define pmd_trans_splitting(pmd) (pmd_val(pmd) & PMD_SECT_SPLITTING)
> > > +#define pmd_trans_huge(pmd)	(pmd_val(pmd) && pmd_isclear((pmd), PMD_TABLE_BIT))
> > 
> > Why isn't this just pmd_present(pmd) && !pmd_table(pmd)? Put another way, I
> > see to have forgotten why we need PMD_TABLE_BIT instead of just using
> > pmd_table and pmd_sect to work out whether we have a table or a block.
> > 
> 
> If we use pmd_sect, we are testing for block entries, but THPs are allowed to
> be faulting entries (i.e. PROT_NONE). A non-zero pmd that does not have the
> table bit set can safely be assumed to be a PROT_NONE THP.

Ah yes, I forgot about faulting entries. We should still be able to use
pmd_table(pmd) as opposed to PMD_TABLE_BIT though, right?

Will
Steve Capper June 27, 2014, 12:48 p.m. UTC | #4
On Fri, Jun 27, 2014 at 01:34:34PM +0100, Will Deacon wrote:
> On Fri, Jun 27, 2014 at 01:24:00PM +0100, Steve Capper wrote:
> > On Fri, Jun 27, 2014 at 12:24:20PM +0100, Will Deacon wrote:
> > > On Tue, Jun 24, 2014 at 01:23:23PM +0100, Steve Capper wrote:
> > > >  #ifdef CONFIG_TRANSPARENT_HUGEPAGE
> > > > -#define pmd_trans_huge(pmd)	(pmd_val(pmd) && !(pmd_val(pmd) & PMD_TABLE_BIT))
> > > > -#define pmd_trans_splitting(pmd) (pmd_val(pmd) & PMD_SECT_SPLITTING)
> > > > +#define pmd_trans_huge(pmd)	(pmd_val(pmd) && pmd_isclear((pmd), PMD_TABLE_BIT))
> > > 
> > > Why isn't this just pmd_present(pmd) && !pmd_table(pmd)? Put another way, I
> > > see to have forgotten why we need PMD_TABLE_BIT instead of just using
> > > pmd_table and pmd_sect to work out whether we have a table or a block.
> > > 
> > 
> > If we use pmd_sect, we are testing for block entries, but THPs are allowed to
> > be faulting entries (i.e. PROT_NONE). A non-zero pmd that does not have the
> > table bit set can safely be assumed to be a PROT_NONE THP.
> 
> Ah yes, I forgot about faulting entries. We should still be able to use
> pmd_table(pmd) as opposed to PMD_TABLE_BIT though, right?

Yes, that should be okay, thanks I'll simplify this.

> 
> Will
diff mbox

Patch

diff --git a/arch/arm/include/asm/pgtable-3level.h b/arch/arm/include/asm/pgtable-3level.h
index 85c60ad..3b10ec6 100644
--- a/arch/arm/include/asm/pgtable-3level.h
+++ b/arch/arm/include/asm/pgtable-3level.h
@@ -207,17 +207,21 @@  static inline pmd_t *pmd_offset(pud_t *pud, unsigned long addr)
 #define pte_huge(pte)		(pte_val(pte) && !(pte_val(pte) & PTE_TABLE_BIT))
 #define pte_mkhuge(pte)		(__pte(pte_val(pte) & ~PTE_TABLE_BIT))
 
-#define pmd_young(pmd)		(pmd_val(pmd) & PMD_SECT_AF)
+#define pmd_isset(pmd, val)	((u32)(val) == (val) ? pmd_val(pmd) & (val) \
+						: !!(pmd_val(pmd) & (val)))
+#define pmd_isclear(pmd, val)	(!(pmd_val(pmd) & (val)))
+
+#define pmd_young(pmd)		(pmd_isset((pmd), PMD_SECT_AF))
 
 #define __HAVE_ARCH_PMD_WRITE
-#define pmd_write(pmd)		(!(pmd_val(pmd) & PMD_SECT_RDONLY))
+#define pmd_write(pmd)		(pmd_isclear((pmd), PMD_SECT_RDONLY))
 
 #define pmd_hugewillfault(pmd)	(!pmd_young(pmd) || !pmd_write(pmd))
 #define pmd_thp_or_huge(pmd)	(pmd_huge(pmd) || pmd_trans_huge(pmd))
 
 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
-#define pmd_trans_huge(pmd)	(pmd_val(pmd) && !(pmd_val(pmd) & PMD_TABLE_BIT))
-#define pmd_trans_splitting(pmd) (pmd_val(pmd) & PMD_SECT_SPLITTING)
+#define pmd_trans_huge(pmd)	(pmd_val(pmd) && pmd_isclear((pmd), PMD_TABLE_BIT))
+#define pmd_trans_splitting(pmd) (pmd_isset((pmd), PMD_SECT_SPLITTING))
 #endif
 
 #define PMD_BIT_FUNC(fn,op) \
diff --git a/arch/arm/include/asm/pgtable.h b/arch/arm/include/asm/pgtable.h
index 5478e5d..01baef0 100644
--- a/arch/arm/include/asm/pgtable.h
+++ b/arch/arm/include/asm/pgtable.h
@@ -214,18 +214,22 @@  static inline pte_t *pmd_page_vaddr(pmd_t pmd)
 
 #define pte_clear(mm,addr,ptep)	set_pte_ext(ptep, __pte(0), 0)
 
+#define pte_isset(pte, val)	((u32)(val) == (val) ? pte_val(pte) & (val) \
+						: !!(pte_val(pte) & (val)))
+#define pte_isclear(pte, val)	(!(pte_val(pte) & (val)))
+
 #define pte_none(pte)		(!pte_val(pte))
-#define pte_present(pte)	(pte_val(pte) & L_PTE_PRESENT)
-#define pte_valid(pte)		(pte_val(pte) & L_PTE_VALID)
+#define pte_present(pte)	(pte_isset((pte), L_PTE_PRESENT))
+#define pte_valid(pte)		(pte_isset((pte), L_PTE_VALID))
 #define pte_accessible(mm, pte)	(mm_tlb_flush_pending(mm) ? pte_present(pte) : pte_valid(pte))
-#define pte_write(pte)		(!(pte_val(pte) & L_PTE_RDONLY))
-#define pte_dirty(pte)		(pte_val(pte) & L_PTE_DIRTY)
-#define pte_young(pte)		(pte_val(pte) & L_PTE_YOUNG)
-#define pte_exec(pte)		(!(pte_val(pte) & L_PTE_XN))
+#define pte_write(pte)		(pte_isclear((pte), L_PTE_RDONLY))
+#define pte_dirty(pte)		(pte_isset((pte), L_PTE_DIRTY))
+#define pte_young(pte)		(pte_isset((pte), L_PTE_YOUNG))
+#define pte_exec(pte)		(pte_isclear((pte), L_PTE_XN))
 #define pte_special(pte)	(0)
 
 #define pte_valid_user(pte)	\
-	(pte_valid(pte) && (pte_val(pte) & L_PTE_USER) && pte_young(pte))
+	(pte_valid(pte) && pte_isset((pte), L_PTE_USER) && pte_young(pte))
 
 #if __LINUX_ARM_ARCH__ < 6
 static inline void __sync_icache_dcache(pte_t pteval)