diff mbox series

[-next,v2,2/4] mm: page_table_check: add hooks to public helpers

Message ID 20220322144447.3563146-3-tongtiangen@huawei.com (mailing list archive)
State New, archived
Headers show
Series mm: page_table_check: add support on arm64 and riscv | expand

Commit Message

Tong Tiangen March 22, 2022, 2:44 p.m. UTC
Move ptep_clear() to the include/linux/pgtable.h and add page table check
relate hooks to some helpers, it's prepare for support page table check
feature on new architecture.

Signed-off-by: Tong Tiangen <tongtiangen@huawei.com>
---
 arch/x86/include/asm/pgtable.h | 10 ----------
 include/linux/pgtable.h        | 27 +++++++++++++++++++--------
 2 files changed, 19 insertions(+), 18 deletions(-)

Comments

Pasha Tatashin March 23, 2022, 5:42 p.m. UTC | #1
On Tue, Mar 22, 2022 at 10:25 AM Tong Tiangen <tongtiangen@huawei.com> wrote:
>
> Move ptep_clear() to the include/linux/pgtable.h and add page table check
> relate hooks to some helpers, it's prepare for support page table check
> feature on new architecture.
>
> Signed-off-by: Tong Tiangen <tongtiangen@huawei.com>
> ---
>  arch/x86/include/asm/pgtable.h | 10 ----------
>  include/linux/pgtable.h        | 27 +++++++++++++++++++--------
>  2 files changed, 19 insertions(+), 18 deletions(-)
>
> diff --git a/arch/x86/include/asm/pgtable.h b/arch/x86/include/asm/pgtable.h
> index 8cd6514e3052..8c85f2eabbaa 100644
> --- a/arch/x86/include/asm/pgtable.h
> +++ b/arch/x86/include/asm/pgtable.h
> @@ -1077,16 +1077,6 @@ static inline pte_t ptep_get_and_clear_full(struct mm_struct *mm,
>         return pte;
>  }
>
> -#define __HAVE_ARCH_PTEP_CLEAR
> -static inline void ptep_clear(struct mm_struct *mm, unsigned long addr,
> -                             pte_t *ptep)
> -{
> -       if (IS_ENABLED(CONFIG_PAGE_TABLE_CHECK))
> -               ptep_get_and_clear(mm, addr, ptep);
> -       else
> -               pte_clear(mm, addr, ptep);
> -}
> -
>  #define __HAVE_ARCH_PTEP_SET_WRPROTECT
>  static inline void ptep_set_wrprotect(struct mm_struct *mm,
>                                       unsigned long addr, pte_t *ptep)
> diff --git a/include/linux/pgtable.h b/include/linux/pgtable.h
> index f4f4077b97aa..d27fd0ed84a9 100644
> --- a/include/linux/pgtable.h
> +++ b/include/linux/pgtable.h
> @@ -12,6 +12,7 @@
>  #include <linux/bug.h>
>  #include <linux/errno.h>
>  #include <asm-generic/pgtable_uffd.h>
> +#include <linux/page_table_check.h>
>
>  #if 5 - defined(__PAGETABLE_P4D_FOLDED) - defined(__PAGETABLE_PUD_FOLDED) - \
>         defined(__PAGETABLE_PMD_FOLDED) != CONFIG_PGTABLE_LEVELS
> @@ -259,14 +260,6 @@ static inline int pmdp_clear_flush_young(struct vm_area_struct *vma,
>  #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
>  #endif
>
> -#ifndef __HAVE_ARCH_PTEP_CLEAR
> -static inline void ptep_clear(struct mm_struct *mm, unsigned long addr,
> -                             pte_t *ptep)
> -{
> -       pte_clear(mm, addr, ptep);
> -}
> -#endif
> -
>  #ifndef __HAVE_ARCH_PTEP_GET_AND_CLEAR
>  static inline pte_t ptep_get_and_clear(struct mm_struct *mm,
>                                        unsigned long address,
> @@ -274,10 +267,23 @@ static inline pte_t ptep_get_and_clear(struct mm_struct *mm,
>  {
>         pte_t pte = *ptep;
>         pte_clear(mm, address, ptep);
> +       page_table_check_pte_clear(mm, address, pte);
>         return pte;
>  }
>  #endif
>
> +#ifndef __HAVE_ARCH_PTEP_CLEAR
> +static inline void ptep_clear(struct mm_struct *mm, unsigned long addr,
> +                             pte_t *ptep)
> +{
> +#ifdef CONFIG_PAGE_TABLE_CHECK
> +       ptep_get_and_clear(mm, addr, ptep);
> +#else
> +       pte_clear(mm, addr, ptep);
> +#endif

I have a preference to use if (IS_ENABLED(CONFIG_PAGE_TABLE_CHECK))
instead of #ifdef. The end result is the same. Otherwise it looks
good.

Thanks,
Pasha
Tong Tiangen March 24, 2022, 2:07 a.m. UTC | #2
在 2022/3/24 1:42, Pasha Tatashin 写道:
> On Tue, Mar 22, 2022 at 10:25 AM Tong Tiangen <tongtiangen@huawei.com> wrote:
>>
>> Move ptep_clear() to the include/linux/pgtable.h and add page table check
>> relate hooks to some helpers, it's prepare for support page table check
>> feature on new architecture.
>>
>> Signed-off-by: Tong Tiangen <tongtiangen@huawei.com>
>> ---
>>   arch/x86/include/asm/pgtable.h | 10 ----------
>>   include/linux/pgtable.h        | 27 +++++++++++++++++++--------
>>   2 files changed, 19 insertions(+), 18 deletions(-)
>>
>> diff --git a/arch/x86/include/asm/pgtable.h b/arch/x86/include/asm/pgtable.h
>> index 8cd6514e3052..8c85f2eabbaa 100644
>> --- a/arch/x86/include/asm/pgtable.h
>> +++ b/arch/x86/include/asm/pgtable.h
>> @@ -1077,16 +1077,6 @@ static inline pte_t ptep_get_and_clear_full(struct mm_struct *mm,
>>          return pte;
>>   }
>>
>> -#define __HAVE_ARCH_PTEP_CLEAR
>> -static inline void ptep_clear(struct mm_struct *mm, unsigned long addr,
>> -                             pte_t *ptep)
>> -{
>> -       if (IS_ENABLED(CONFIG_PAGE_TABLE_CHECK))
>> -               ptep_get_and_clear(mm, addr, ptep);
>> -       else
>> -               pte_clear(mm, addr, ptep);
>> -}
>> -
>>   #define __HAVE_ARCH_PTEP_SET_WRPROTECT
>>   static inline void ptep_set_wrprotect(struct mm_struct *mm,
>>                                        unsigned long addr, pte_t *ptep)
>> diff --git a/include/linux/pgtable.h b/include/linux/pgtable.h
>> index f4f4077b97aa..d27fd0ed84a9 100644
>> --- a/include/linux/pgtable.h
>> +++ b/include/linux/pgtable.h
>> @@ -12,6 +12,7 @@
>>   #include <linux/bug.h>
>>   #include <linux/errno.h>
>>   #include <asm-generic/pgtable_uffd.h>
>> +#include <linux/page_table_check.h>
>>
>>   #if 5 - defined(__PAGETABLE_P4D_FOLDED) - defined(__PAGETABLE_PUD_FOLDED) - \
>>          defined(__PAGETABLE_PMD_FOLDED) != CONFIG_PGTABLE_LEVELS
>> @@ -259,14 +260,6 @@ static inline int pmdp_clear_flush_young(struct vm_area_struct *vma,
>>   #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
>>   #endif
>>
>> -#ifndef __HAVE_ARCH_PTEP_CLEAR
>> -static inline void ptep_clear(struct mm_struct *mm, unsigned long addr,
>> -                             pte_t *ptep)
>> -{
>> -       pte_clear(mm, addr, ptep);
>> -}
>> -#endif
>> -
>>   #ifndef __HAVE_ARCH_PTEP_GET_AND_CLEAR
>>   static inline pte_t ptep_get_and_clear(struct mm_struct *mm,
>>                                         unsigned long address,
>> @@ -274,10 +267,23 @@ static inline pte_t ptep_get_and_clear(struct mm_struct *mm,
>>   {
>>          pte_t pte = *ptep;
>>          pte_clear(mm, address, ptep);
>> +       page_table_check_pte_clear(mm, address, pte);
>>          return pte;
>>   }
>>   #endif
>>
>> +#ifndef __HAVE_ARCH_PTEP_CLEAR
>> +static inline void ptep_clear(struct mm_struct *mm, unsigned long addr,
>> +                             pte_t *ptep)
>> +{
>> +#ifdef CONFIG_PAGE_TABLE_CHECK
>> +       ptep_get_and_clear(mm, addr, ptep);
>> +#else
>> +       pte_clear(mm, addr, ptep);
>> +#endif
> 
> I have a preference to use if (IS_ENABLED(CONFIG_PAGE_TABLE_CHECK))
> instead of #ifdef. The end result is the same. Otherwise it looks
> good.
> 
> Thanks,
> Pasha
> .

I have a little hesitation when making this change , in theory, add if 
here may affect the performance a little in some scenarios. However, the 
impact on the whole call path should be small.

I will send v3 using if (IS_ENABLED(CONFIG_PAGE_TABLE_CHECK)).

Thanks.
Tong
.
Pasha Tatashin March 24, 2022, 2:12 a.m. UTC | #3
On Wed, Mar 23, 2022 at 10:07 PM Tong Tiangen <tongtiangen@huawei.com> wrote:
>
>
>
> 在 2022/3/24 1:42, Pasha Tatashin 写道:
> > On Tue, Mar 22, 2022 at 10:25 AM Tong Tiangen <tongtiangen@huawei.com> wrote:
> >>
> >> Move ptep_clear() to the include/linux/pgtable.h and add page table check
> >> relate hooks to some helpers, it's prepare for support page table check
> >> feature on new architecture.
> >>
> >> Signed-off-by: Tong Tiangen <tongtiangen@huawei.com>
> >> ---
> >>   arch/x86/include/asm/pgtable.h | 10 ----------
> >>   include/linux/pgtable.h        | 27 +++++++++++++++++++--------
> >>   2 files changed, 19 insertions(+), 18 deletions(-)
> >>
> >> diff --git a/arch/x86/include/asm/pgtable.h b/arch/x86/include/asm/pgtable.h
> >> index 8cd6514e3052..8c85f2eabbaa 100644
> >> --- a/arch/x86/include/asm/pgtable.h
> >> +++ b/arch/x86/include/asm/pgtable.h
> >> @@ -1077,16 +1077,6 @@ static inline pte_t ptep_get_and_clear_full(struct mm_struct *mm,
> >>          return pte;
> >>   }
> >>
> >> -#define __HAVE_ARCH_PTEP_CLEAR
> >> -static inline void ptep_clear(struct mm_struct *mm, unsigned long addr,
> >> -                             pte_t *ptep)
> >> -{
> >> -       if (IS_ENABLED(CONFIG_PAGE_TABLE_CHECK))
> >> -               ptep_get_and_clear(mm, addr, ptep);
> >> -       else
> >> -               pte_clear(mm, addr, ptep);
> >> -}
> >> -
> >>   #define __HAVE_ARCH_PTEP_SET_WRPROTECT
> >>   static inline void ptep_set_wrprotect(struct mm_struct *mm,
> >>                                        unsigned long addr, pte_t *ptep)
> >> diff --git a/include/linux/pgtable.h b/include/linux/pgtable.h
> >> index f4f4077b97aa..d27fd0ed84a9 100644
> >> --- a/include/linux/pgtable.h
> >> +++ b/include/linux/pgtable.h
> >> @@ -12,6 +12,7 @@
> >>   #include <linux/bug.h>
> >>   #include <linux/errno.h>
> >>   #include <asm-generic/pgtable_uffd.h>
> >> +#include <linux/page_table_check.h>
> >>
> >>   #if 5 - defined(__PAGETABLE_P4D_FOLDED) - defined(__PAGETABLE_PUD_FOLDED) - \
> >>          defined(__PAGETABLE_PMD_FOLDED) != CONFIG_PGTABLE_LEVELS
> >> @@ -259,14 +260,6 @@ static inline int pmdp_clear_flush_young(struct vm_area_struct *vma,
> >>   #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
> >>   #endif
> >>
> >> -#ifndef __HAVE_ARCH_PTEP_CLEAR
> >> -static inline void ptep_clear(struct mm_struct *mm, unsigned long addr,
> >> -                             pte_t *ptep)
> >> -{
> >> -       pte_clear(mm, addr, ptep);
> >> -}
> >> -#endif
> >> -
> >>   #ifndef __HAVE_ARCH_PTEP_GET_AND_CLEAR
> >>   static inline pte_t ptep_get_and_clear(struct mm_struct *mm,
> >>                                         unsigned long address,
> >> @@ -274,10 +267,23 @@ static inline pte_t ptep_get_and_clear(struct mm_struct *mm,
> >>   {
> >>          pte_t pte = *ptep;
> >>          pte_clear(mm, address, ptep);
> >> +       page_table_check_pte_clear(mm, address, pte);
> >>          return pte;
> >>   }
> >>   #endif
> >>
> >> +#ifndef __HAVE_ARCH_PTEP_CLEAR
> >> +static inline void ptep_clear(struct mm_struct *mm, unsigned long addr,
> >> +                             pte_t *ptep)
> >> +{
> >> +#ifdef CONFIG_PAGE_TABLE_CHECK
> >> +       ptep_get_and_clear(mm, addr, ptep);
> >> +#else
> >> +       pte_clear(mm, addr, ptep);
> >> +#endif
> >
> > I have a preference to use if (IS_ENABLED(CONFIG_PAGE_TABLE_CHECK))
> > instead of #ifdef. The end result is the same. Otherwise it looks
> > good.
> >
> > Thanks,
> > Pasha
> > .
>
> I have a little hesitation when making this change , in theory, add if
> here may affect the performance a little in some scenarios. However, the
> impact on the whole call path should be small.

I do not think so, the compiler should optimize out IS_ENABLED() when
not enabled, no?

>
> I will send v3 using if (IS_ENABLED(CONFIG_PAGE_TABLE_CHECK)).
>
> Thanks.
> Tong
> .
Tong Tiangen March 24, 2022, 2:44 a.m. UTC | #4
在 2022/3/24 10:12, Pasha Tatashin 写道:
> On Wed, Mar 23, 2022 at 10:07 PM Tong Tiangen <tongtiangen@huawei.com> wrote:
>>
>>
>>
>> 在 2022/3/24 1:42, Pasha Tatashin 写道:
>>> On Tue, Mar 22, 2022 at 10:25 AM Tong Tiangen <tongtiangen@huawei.com> wrote:
>>>>
>>>> Move ptep_clear() to the include/linux/pgtable.h and add page table check
>>>> relate hooks to some helpers, it's prepare for support page table check
>>>> feature on new architecture.
>>>>
>>>> Signed-off-by: Tong Tiangen <tongtiangen@huawei.com>
>>>> ---
>>>>    arch/x86/include/asm/pgtable.h | 10 ----------
>>>>    include/linux/pgtable.h        | 27 +++++++++++++++++++--------
>>>>    2 files changed, 19 insertions(+), 18 deletions(-)
>>>>
>>>> diff --git a/arch/x86/include/asm/pgtable.h b/arch/x86/include/asm/pgtable.h
>>>> index 8cd6514e3052..8c85f2eabbaa 100644
>>>> --- a/arch/x86/include/asm/pgtable.h
>>>> +++ b/arch/x86/include/asm/pgtable.h
>>>> @@ -1077,16 +1077,6 @@ static inline pte_t ptep_get_and_clear_full(struct mm_struct *mm,
>>>>           return pte;
>>>>    }
>>>>
>>>> -#define __HAVE_ARCH_PTEP_CLEAR
>>>> -static inline void ptep_clear(struct mm_struct *mm, unsigned long addr,
>>>> -                             pte_t *ptep)
>>>> -{
>>>> -       if (IS_ENABLED(CONFIG_PAGE_TABLE_CHECK))
>>>> -               ptep_get_and_clear(mm, addr, ptep);
>>>> -       else
>>>> -               pte_clear(mm, addr, ptep);
>>>> -}
>>>> -
>>>>    #define __HAVE_ARCH_PTEP_SET_WRPROTECT
>>>>    static inline void ptep_set_wrprotect(struct mm_struct *mm,
>>>>                                         unsigned long addr, pte_t *ptep)
>>>> diff --git a/include/linux/pgtable.h b/include/linux/pgtable.h
>>>> index f4f4077b97aa..d27fd0ed84a9 100644
>>>> --- a/include/linux/pgtable.h
>>>> +++ b/include/linux/pgtable.h
>>>> @@ -12,6 +12,7 @@
>>>>    #include <linux/bug.h>
>>>>    #include <linux/errno.h>
>>>>    #include <asm-generic/pgtable_uffd.h>
>>>> +#include <linux/page_table_check.h>
>>>>
>>>>    #if 5 - defined(__PAGETABLE_P4D_FOLDED) - defined(__PAGETABLE_PUD_FOLDED) - \
>>>>           defined(__PAGETABLE_PMD_FOLDED) != CONFIG_PGTABLE_LEVELS
>>>> @@ -259,14 +260,6 @@ static inline int pmdp_clear_flush_young(struct vm_area_struct *vma,
>>>>    #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
>>>>    #endif
>>>>
>>>> -#ifndef __HAVE_ARCH_PTEP_CLEAR
>>>> -static inline void ptep_clear(struct mm_struct *mm, unsigned long addr,
>>>> -                             pte_t *ptep)
>>>> -{
>>>> -       pte_clear(mm, addr, ptep);
>>>> -}
>>>> -#endif
>>>> -
>>>>    #ifndef __HAVE_ARCH_PTEP_GET_AND_CLEAR
>>>>    static inline pte_t ptep_get_and_clear(struct mm_struct *mm,
>>>>                                          unsigned long address,
>>>> @@ -274,10 +267,23 @@ static inline pte_t ptep_get_and_clear(struct mm_struct *mm,
>>>>    {
>>>>           pte_t pte = *ptep;
>>>>           pte_clear(mm, address, ptep);
>>>> +       page_table_check_pte_clear(mm, address, pte);
>>>>           return pte;
>>>>    }
>>>>    #endif
>>>>
>>>> +#ifndef __HAVE_ARCH_PTEP_CLEAR
>>>> +static inline void ptep_clear(struct mm_struct *mm, unsigned long addr,
>>>> +                             pte_t *ptep)
>>>> +{
>>>> +#ifdef CONFIG_PAGE_TABLE_CHECK
>>>> +       ptep_get_and_clear(mm, addr, ptep);
>>>> +#else
>>>> +       pte_clear(mm, addr, ptep);
>>>> +#endif
>>>
>>> I have a preference to use if (IS_ENABLED(CONFIG_PAGE_TABLE_CHECK))
>>> instead of #ifdef. The end result is the same. Otherwise it looks
>>> good.
>>>
>>> Thanks,
>>> Pasha
>>> .
>>
>> I have a little hesitation when making this change , in theory, add if
>> here may affect the performance a little in some scenarios. However, the
>> impact on the whole call path should be small.
> 
> I do not think so, the compiler should optimize out IS_ENABLED() when
> not enabled, no?
> 

You are right.

https://www.kernel.org/doc/Documentation/process/coding-style.rst

The compiler will constant-fold the conditional away, and include or 
exclude the block of code just as with an #ifdef, so this will **not add 
any runtime overhead**.

Thanks :)

>>
>> I will send v3 using if (IS_ENABLED(CONFIG_PAGE_TABLE_CHECK)).
>>
>> Thanks.
>> Tong
>> .
> .
diff mbox series

Patch

diff --git a/arch/x86/include/asm/pgtable.h b/arch/x86/include/asm/pgtable.h
index 8cd6514e3052..8c85f2eabbaa 100644
--- a/arch/x86/include/asm/pgtable.h
+++ b/arch/x86/include/asm/pgtable.h
@@ -1077,16 +1077,6 @@  static inline pte_t ptep_get_and_clear_full(struct mm_struct *mm,
 	return pte;
 }
 
-#define __HAVE_ARCH_PTEP_CLEAR
-static inline void ptep_clear(struct mm_struct *mm, unsigned long addr,
-			      pte_t *ptep)
-{
-	if (IS_ENABLED(CONFIG_PAGE_TABLE_CHECK))
-		ptep_get_and_clear(mm, addr, ptep);
-	else
-		pte_clear(mm, addr, ptep);
-}
-
 #define __HAVE_ARCH_PTEP_SET_WRPROTECT
 static inline void ptep_set_wrprotect(struct mm_struct *mm,
 				      unsigned long addr, pte_t *ptep)
diff --git a/include/linux/pgtable.h b/include/linux/pgtable.h
index f4f4077b97aa..d27fd0ed84a9 100644
--- a/include/linux/pgtable.h
+++ b/include/linux/pgtable.h
@@ -12,6 +12,7 @@ 
 #include <linux/bug.h>
 #include <linux/errno.h>
 #include <asm-generic/pgtable_uffd.h>
+#include <linux/page_table_check.h>
 
 #if 5 - defined(__PAGETABLE_P4D_FOLDED) - defined(__PAGETABLE_PUD_FOLDED) - \
 	defined(__PAGETABLE_PMD_FOLDED) != CONFIG_PGTABLE_LEVELS
@@ -259,14 +260,6 @@  static inline int pmdp_clear_flush_young(struct vm_area_struct *vma,
 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
 #endif
 
-#ifndef __HAVE_ARCH_PTEP_CLEAR
-static inline void ptep_clear(struct mm_struct *mm, unsigned long addr,
-			      pte_t *ptep)
-{
-	pte_clear(mm, addr, ptep);
-}
-#endif
-
 #ifndef __HAVE_ARCH_PTEP_GET_AND_CLEAR
 static inline pte_t ptep_get_and_clear(struct mm_struct *mm,
 				       unsigned long address,
@@ -274,10 +267,23 @@  static inline pte_t ptep_get_and_clear(struct mm_struct *mm,
 {
 	pte_t pte = *ptep;
 	pte_clear(mm, address, ptep);
+	page_table_check_pte_clear(mm, address, pte);
 	return pte;
 }
 #endif
 
+#ifndef __HAVE_ARCH_PTEP_CLEAR
+static inline void ptep_clear(struct mm_struct *mm, unsigned long addr,
+			      pte_t *ptep)
+{
+#ifdef CONFIG_PAGE_TABLE_CHECK
+	ptep_get_and_clear(mm, addr, ptep);
+#else
+	pte_clear(mm, addr, ptep);
+#endif
+}
+#endif
+
 #ifndef __HAVE_ARCH_PTEP_GET
 static inline pte_t ptep_get(pte_t *ptep)
 {
@@ -347,7 +353,10 @@  static inline pmd_t pmdp_huge_get_and_clear(struct mm_struct *mm,
 					    pmd_t *pmdp)
 {
 	pmd_t pmd = *pmdp;
+
 	pmd_clear(pmdp);
+	page_table_check_pmd_clear(mm, address, pmd);
+
 	return pmd;
 }
 #endif /* __HAVE_ARCH_PMDP_HUGE_GET_AND_CLEAR */
@@ -359,6 +368,8 @@  static inline pud_t pudp_huge_get_and_clear(struct mm_struct *mm,
 	pud_t pud = *pudp;
 
 	pud_clear(pudp);
+	page_table_check_pud_clear(mm, address, pud);
+
 	return pud;
 }
 #endif /* __HAVE_ARCH_PUDP_HUGE_GET_AND_CLEAR */