Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp1355318iob; Fri, 29 Apr 2022 03:46:25 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxQRq4oXnCy+l6TDzYH6ASYRA553CHsnzt5Xz6EtBc00p0Lu08Eql1n8aibD+e+TXEc0eKX X-Received: by 2002:a17:902:b613:b0:156:7d82:c09b with SMTP id b19-20020a170902b61300b001567d82c09bmr37836781pls.80.1651229184991; Fri, 29 Apr 2022 03:46:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1651229184; cv=none; d=google.com; s=arc-20160816; b=ZiyAGDmWOf207YLfe/RYHTYVPpoiD3XRePQib+cZ/wkzZ5Y+OesJDSrdBkN/rrKwUK tyoKSQUFoPI5Q1L9T+3dU0HyfWMjW4BmRPs/p4Fiw7AI3smiS7tBlijZUB1lKn9Ai0Iv gvbnh//mhBQxad+JxPwAw0M6Vq+7upIr4u1wnr4ta9T1I9V4Q7t3O+cS7eu87Xndva9i Map3RcVHweOK4Au/ct28FrEbwg67smCGHsRsE6yutFlPw5tHgLMWt04yqZ3Yu2ALp5nL VEl8RpQoM/a126ytu1qzLv7/NcyuFFsJxhsg4zX4wbmFKcva3wcuP68tW2+0v7tPjV0O WdcA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id; bh=sPjBvq/LNClE4YP1gYXmvR8/34s78NnUFFFCfbRIVhQ=; b=Uxg6X0byvj59X8wTx2/bATJJA1WKEADmJ8pi4wgj4BDYTXPXZPF0RS7alrXpbHL2oR rBGo1nlMBBQLfXCh31KCnbb/jF26JMRkl8HRc2eqGw+CTGR7Tzmr9DNkAYvP/iA9fOTr db5CcCVF9qh26J6x5OQEcgI7+9Rw3+F1eTfslw1vAy8FMgS02b9zvOglXLMkB3STN/7e PIgn9gPOiHWOyfHhA3A6atYg12LfpP+E9Oart5TAZr8Uo2NPHoQ7LxehX92QHCXq3ua1 9NHNVosF4zhnxyd5ffgIT2g60hYamwzBolIzOkozPrnDLZXRjghjKd8lbtIr8tyTAiIr 2umw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id d17-20020a170902ced100b00156647ce264si7470583plg.315.2022.04.29.03.46.10; Fri, 29 Apr 2022 03:46:24 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1354485AbiD2GUy (ORCPT + 99 others); Fri, 29 Apr 2022 02:20:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56614 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1354478AbiD2GUx (ORCPT ); Fri, 29 Apr 2022 02:20:53 -0400 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 2CD54B9F10 for ; Thu, 28 Apr 2022 23:17:33 -0700 (PDT) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 3A10811FB; Thu, 28 Apr 2022 23:17:33 -0700 (PDT) Received: from [10.163.42.113] (unknown [10.163.42.113]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 861ED3F5A1; Thu, 28 Apr 2022 23:17:22 -0700 (PDT) Message-ID: Date: Fri, 29 Apr 2022 11:48:18 +0530 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.5.0 Subject: Re: [PATCH -next v6 5/6] arm64/mm: Enable ARCH_SUPPORTS_PAGE_TABLE_CHECK Content-Language: en-US To: Tong Tiangen , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Pasha Tatashin , Andrew Morton , Catalin Marinas , Will Deacon , Paul Walmsley , Palmer Dabbelt , Albert Ou Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-arm-kernel@lists.infradead.org, linux-riscv@lists.infradead.org, Kefeng Wang , Guohanjun References: <20220426081036.1374452-1-tongtiangen@huawei.com> <20220426081036.1374452-6-tongtiangen@huawei.com> From: Anshuman Khandual In-Reply-To: <20220426081036.1374452-6-tongtiangen@huawei.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00,NICE_REPLY_A, RCVD_IN_DNSWL_HI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 4/26/22 13:40, Tong Tiangen wrote: > From: Kefeng Wang > > As commit d283d422c6c4 ("x86: mm: add x86_64 support for page table check") > , enable ARCH_SUPPORTS_PAGE_TABLE_CHECK on arm64. > > Add additional page table check stubs for page table helpers, these stubs > can be used to check the existing page table entries. > > Signed-off-by: Kefeng Wang > Signed-off-by: Tong Tiangen > Reviewed-by: Pasha Tatashin > --- > arch/arm64/Kconfig | 1 + > arch/arm64/include/asm/pgtable.h | 59 +++++++++++++++++++++++++++++--- > 2 files changed, 55 insertions(+), 5 deletions(-) > > diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig > index 18a18a0e855d..c1509525ab8e 100644 > --- a/arch/arm64/Kconfig > +++ b/arch/arm64/Kconfig > @@ -92,6 +92,7 @@ config ARM64 > select ARCH_SUPPORTS_ATOMIC_RMW > select ARCH_SUPPORTS_INT128 if CC_HAS_INT128 > select ARCH_SUPPORTS_NUMA_BALANCING > + select ARCH_SUPPORTS_PAGE_TABLE_CHECK > select ARCH_WANT_COMPAT_IPC_PARSE_VERSION if COMPAT > select ARCH_WANT_DEFAULT_BPF_JIT > select ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT > diff --git a/arch/arm64/include/asm/pgtable.h b/arch/arm64/include/asm/pgtable.h > index ad9b221963d4..fe17788a6885 100644 > --- a/arch/arm64/include/asm/pgtable.h > +++ b/arch/arm64/include/asm/pgtable.h > @@ -33,6 +33,7 @@ > #include > #include > #include > +#include > > #ifdef CONFIG_TRANSPARENT_HUGEPAGE > #define __HAVE_ARCH_FLUSH_PMD_TLB_RANGE > @@ -96,6 +97,7 @@ static inline pteval_t __phys_to_pte_val(phys_addr_t phys) > #define pte_young(pte) (!!(pte_val(pte) & PTE_AF)) > #define pte_special(pte) (!!(pte_val(pte) & PTE_SPECIAL)) > #define pte_write(pte) (!!(pte_val(pte) & PTE_WRITE)) > +#define pte_user(pte) (!!(pte_val(pte) & PTE_USER)) > #define pte_user_exec(pte) (!(pte_val(pte) & PTE_UXN)) > #define pte_cont(pte) (!!(pte_val(pte) & PTE_CONT)) > #define pte_devmap(pte) (!!(pte_val(pte) & PTE_DEVMAP)) > @@ -312,7 +314,7 @@ static inline void __check_racy_pte_update(struct mm_struct *mm, pte_t *ptep, > __func__, pte_val(old_pte), pte_val(pte)); > } > > -static inline void set_pte_at(struct mm_struct *mm, unsigned long addr, > +static inline void __set_pte_at(struct mm_struct *mm, unsigned long addr, > pte_t *ptep, pte_t pte) A small nit. Subsequent line containing the arguments "pte_t *ptep ..." needs to be aligned properly, after '__' got added into set_pte_at(). > { > if (pte_present(pte) && pte_user_exec(pte) && !pte_special(pte)) > @@ -343,6 +345,13 @@ static inline void set_pte_at(struct mm_struct *mm, unsigned long addr, > set_pte(ptep, pte); > } > > +static inline void set_pte_at(struct mm_struct *mm, unsigned long addr, > + pte_t *ptep, pte_t pte) > +{ > + page_table_check_pte_set(mm, addr, ptep, pte); > + return __set_pte_at(mm, addr, ptep, pte); > +} > + > /* > * Huge pte definitions. > */ > @@ -454,6 +463,8 @@ static inline int pmd_trans_huge(pmd_t pmd) > #define pmd_dirty(pmd) pte_dirty(pmd_pte(pmd)) > #define pmd_young(pmd) pte_young(pmd_pte(pmd)) > #define pmd_valid(pmd) pte_valid(pmd_pte(pmd)) > +#define pmd_user(pmd) pte_user(pmd_pte(pmd)) > +#define pmd_user_exec(pmd) pte_user_exec(pmd_pte(pmd)) > #define pmd_cont(pmd) pte_cont(pmd_pte(pmd)) > #define pmd_wrprotect(pmd) pte_pmd(pte_wrprotect(pmd_pte(pmd))) > #define pmd_mkold(pmd) pte_pmd(pte_mkold(pmd_pte(pmd))) > @@ -501,8 +512,19 @@ static inline pmd_t pmd_mkdevmap(pmd_t pmd) > #define pud_pfn(pud) ((__pud_to_phys(pud) & PUD_MASK) >> PAGE_SHIFT) > #define pfn_pud(pfn,prot) __pud(__phys_to_pud_val((phys_addr_t)(pfn) << PAGE_SHIFT) | pgprot_val(prot)) > > -#define set_pmd_at(mm, addr, pmdp, pmd) set_pte_at(mm, addr, (pte_t *)pmdp, pmd_pte(pmd)) > -#define set_pud_at(mm, addr, pudp, pud) set_pte_at(mm, addr, (pte_t *)pudp, pud_pte(pud)) > +static inline void set_pmd_at(struct mm_struct *mm, unsigned long addr, > + pmd_t *pmdp, pmd_t pmd) > +{ > + page_table_check_pmd_set(mm, addr, pmdp, pmd); > + return __set_pte_at(mm, addr, (pte_t *)pmdp, pmd_pte(pmd)); > +} > + > +static inline void set_pud_at(struct mm_struct *mm, unsigned long addr, > + pud_t *pudp, pud_t pud) > +{ > + page_table_check_pud_set(mm, addr, pudp, pud); > + return __set_pte_at(mm, addr, (pte_t *)pudp, pud_pte(pud)); > +} > > #define __p4d_to_phys(p4d) __pte_to_phys(p4d_pte(p4d)) > #define __phys_to_p4d_val(phys) __phys_to_pte_val(phys) > @@ -643,6 +665,24 @@ static inline unsigned long pmd_page_vaddr(pmd_t pmd) > #define pud_present(pud) pte_present(pud_pte(pud)) > #define pud_leaf(pud) pud_sect(pud) > #define pud_valid(pud) pte_valid(pud_pte(pud)) > +#define pud_user(pud) pte_user(pud_pte(pud)) > + > +#ifdef CONFIG_PAGE_TABLE_CHECK > +static inline bool pte_user_accessible_page(pte_t pte) > +{ > + return pte_present(pte) && (pte_user(pte) || pte_user_exec(pte)); > +} > + > +static inline bool pmd_user_accessible_page(pmd_t pmd) > +{ > + return pmd_present(pmd) && (pmd_user(pmd) || pmd_user_exec(pmd)); > +} > + > +static inline bool pud_user_accessible_page(pud_t pud) > +{ > + return pud_present(pud) && pud_user(pud); > +} > +#endif > > static inline void set_pud(pud_t *pudp, pud_t pud) > { > @@ -876,7 +916,11 @@ static inline int pmdp_test_and_clear_young(struct vm_area_struct *vma, > static inline pte_t ptep_get_and_clear(struct mm_struct *mm, > unsigned long address, pte_t *ptep) > { > - return __pte(xchg_relaxed(&pte_val(*ptep), 0)); > + pte_t pte = __pte(xchg_relaxed(&pte_val(*ptep), 0)); > + > + page_table_check_pte_clear(mm, address, pte); > + > + return pte; > } > > #ifdef CONFIG_TRANSPARENT_HUGEPAGE > @@ -884,7 +928,11 @@ static inline pte_t ptep_get_and_clear(struct mm_struct *mm, > static inline pmd_t pmdp_huge_get_and_clear(struct mm_struct *mm, > unsigned long address, pmd_t *pmdp) > { > - return pte_pmd(ptep_get_and_clear(mm, address, (pte_t *)pmdp)); > + pmd_t pmd = pte_pmd(__pte(xchg_relaxed(&pte_val(*(pte_t *)pmdp), 0))); Why there is any change here ? Why not just the following instead, like what you did in ptep_get_and_clear() above. The page table entry return value here just needs to be preserved for subsequent page table check helpers. pmd_t pmd = pte_pmd(ptep_get_and_clear(mm, address, (pte_t *)pmdp)); > + > + page_table_check_pmd_clear(mm, address, pmd); > + > + return pmd; > } > #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ > > @@ -918,6 +966,7 @@ static inline void pmdp_set_wrprotect(struct mm_struct *mm, > static inline pmd_t pmdp_establish(struct vm_area_struct *vma, > unsigned long address, pmd_t *pmdp, pmd_t pmd) > { > + page_table_check_pmd_set(vma->vm_mm, address, pmdp, pmd); > return __pmd(xchg_relaxed(&pmd_val(*pmdp), pmd_val(pmd))); > } > #endif