From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2CE55C432BE for ; Mon, 9 Aug 2021 09:27:29 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id BC1B360D07 for ; Mon, 9 Aug 2021 09:27:28 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org BC1B360D07 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id 55D578D0005; Mon, 9 Aug 2021 05:27:28 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5018D8D0003; Mon, 9 Aug 2021 05:27:28 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3C9706B0080; Mon, 9 Aug 2021 05:27:28 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0183.hostedemail.com [216.40.44.183]) by kanga.kvack.org (Postfix) with ESMTP id 220B56B007D for ; Mon, 9 Aug 2021 05:27:28 -0400 (EDT) Received: from smtpin02.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id CB3EB1F36F for ; Mon, 9 Aug 2021 09:27:27 +0000 (UTC) X-FDA: 78455014134.02.5E2C895 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by imf24.hostedemail.com (Postfix) with ESMTP id 73B89B000200 for ; Mon, 9 Aug 2021 09:27:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1628501246; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=podcA5H2BuUd09am8VR8N0VW57UDDy9BRKI3yPiysu0=; b=Xm8DtOf4U7uPjYeh2XpttFGzXo9F9AWkrdAn/9dv1pwEsb/rmEDddtwkZXOcfDEkx5FLZ/ B7HLeWWMBU/yPL3ZAJsRjYsGjIaQ4AnLYUfgLuBOyCbLpHD2P0ftPF3CrCYGspqoNl38Wq sTDFmN8I40q5y1P7RToxB9S4isLh6W0= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-565-2fTAfO6-P4qdGiQpHBoaTQ-1; Mon, 09 Aug 2021 05:27:23 -0400 X-MC-Unique: 2fTAfO6-P4qdGiQpHBoaTQ-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 74EC5DF8A0; Mon, 9 Aug 2021 09:27:21 +0000 (UTC) Received: from gshan.redhat.com (vpn2-54-155.bne.redhat.com [10.64.54.155]) by smtp.corp.redhat.com (Postfix) with ESMTPS id F3AAD5D6A8; Mon, 9 Aug 2021 09:27:14 +0000 (UTC) From: Gavin Shan To: linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org, anshuman.khandual@arm.com, gerald.schaefer@linux.ibm.com, aneesh.kumar@linux.ibm.com, christophe.leroy@csgroup.eu, cai@lca.pw, catalin.marinas@arm.com, will@kernel.org, vgupta@synopsys.com, akpm@linux-foundation.org, chuhu@redhat.com, shan.gavin@gmail.com Subject: [PATCH v6 01/12] mm/debug_vm_pgtable: Introduce struct pgtable_debug_args Date: Mon, 9 Aug 2021 17:26:20 +0800 Message-Id: <20210809092631.1888748-2-gshan@redhat.com> In-Reply-To: <20210809092631.1888748-1-gshan@redhat.com> References: <20210809092631.1888748-1-gshan@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=Xm8DtOf4; spf=none (imf24.hostedemail.com: domain of gshan@redhat.com has no SPF policy when checking 216.205.24.124) smtp.mailfrom=gshan@redhat.com; dmarc=pass (policy=none) header.from=redhat.com X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 73B89B000200 X-Stat-Signature: cdg7ewkxyyjjkg15s68ypub8q365kwih X-HE-Tag: 1628501247-133007 Content-Transfer-Encoding: quoted-printable X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: In debug_vm_pgtable(), there are many local variables introduced to track the needed information and they are passed to the functions for various test cases. It'd better to introduce a struct as place holder for these information. With it, what the tests functions need is the struct. In this way, the code is simplified and easier to be maintained. Besides, set_xxx_at() could access the data on the corresponding pages in the page table modifying tests. So the accessed pages in the tests should have been allocated from buddy. Otherwise, we're accessing pages that aren't owned by us. This causes issues like page flag corruption or kernel crash on accessing unmapped page when CONFIG_DEBUG_PAGEALLOC is enabled. This introduces "struct pgtable_debug_args". The struct is initialized and destroyed, but the information in the struct isn't used yet. It will be used in subsequent patches. Signed-off-by: Gavin Shan Reviewed-by: Anshuman Khandual --- mm/debug_vm_pgtable.c | 270 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 269 insertions(+), 1 deletion(-) diff --git a/mm/debug_vm_pgtable.c b/mm/debug_vm_pgtable.c index 1c922691aa61..7b6bcf59e376 100644 --- a/mm/debug_vm_pgtable.c +++ b/mm/debug_vm_pgtable.c @@ -58,6 +58,37 @@ #define RANDOM_ORVALUE (GENMASK(BITS_PER_LONG - 1, 0) & ~ARCH_SKIP_MASK) #define RANDOM_NZVALUE GENMASK(7, 0) =20 +struct pgtable_debug_args { + struct mm_struct *mm; + struct vm_area_struct *vma; + + pgd_t *pgdp; + p4d_t *p4dp; + pud_t *pudp; + pmd_t *pmdp; + pte_t *ptep; + + p4d_t *start_p4dp; + pud_t *start_pudp; + pmd_t *start_pmdp; + pgtable_t start_ptep; + + unsigned long vaddr; + pgprot_t page_prot; + pgprot_t page_prot_none; + + bool is_contiguous_page; + unsigned long pud_pfn; + unsigned long pmd_pfn; + unsigned long pte_pfn; + + unsigned long fixed_pgd_pfn; + unsigned long fixed_p4d_pfn; + unsigned long fixed_pud_pfn; + unsigned long fixed_pmd_pfn; + unsigned long fixed_pte_pfn; +}; + static void __init pte_basic_tests(unsigned long pfn, int idx) { pgprot_t prot =3D protection_map[idx]; @@ -955,8 +986,239 @@ static unsigned long __init get_random_vaddr(void) return random_vaddr; } =20 +static void __init destroy_args(struct pgtable_debug_args *args) +{ + struct page *page =3D NULL; + + /* Free (huge) page */ + if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) && + IS_ENABLED(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD) && + has_transparent_hugepage() && + args->pud_pfn !=3D ULONG_MAX) { + if (args->is_contiguous_page) { + free_contig_range(args->pud_pfn, + (1 << (HPAGE_PUD_SHIFT - PAGE_SHIFT))); + } else { + page =3D pfn_to_page(args->pud_pfn); + __free_pages(page, HPAGE_PUD_SHIFT - PAGE_SHIFT); + } + + args->pud_pfn =3D ULONG_MAX; + args->pmd_pfn =3D ULONG_MAX; + args->pte_pfn =3D ULONG_MAX; + } + + if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) && + has_transparent_hugepage() && + args->pmd_pfn !=3D ULONG_MAX) { + if (args->is_contiguous_page) { + free_contig_range(args->pmd_pfn, (1 << HPAGE_PMD_ORDER)); + } else { + page =3D pfn_to_page(args->pmd_pfn); + __free_pages(page, HPAGE_PMD_ORDER); + } + + args->pmd_pfn =3D ULONG_MAX; + args->pte_pfn =3D ULONG_MAX; + } + + if (args->pte_pfn !=3D ULONG_MAX) { + page =3D pfn_to_page(args->pte_pfn); + __free_pages(page, 0); + + args->pte_pfn =3D ULONG_MAX; + } + + /* Free page table entries */ + if (args->start_ptep) { + pte_free(args->mm, args->start_ptep); + mm_dec_nr_ptes(args->mm); + } + + if (args->start_pmdp) { + pmd_free(args->mm, args->start_pmdp); + mm_dec_nr_pmds(args->mm); + } + + if (args->start_pudp) { + pud_free(args->mm, args->start_pudp); + mm_dec_nr_puds(args->mm); + } + + if (args->start_p4dp) + p4d_free(args->mm, args->start_p4dp); + + /* Free vma and mm struct */ + if (args->vma) + vm_area_free(args->vma); + + if (args->mm) + mmdrop(args->mm); +} + +static struct page * __init +debug_vm_pgtable_alloc_huge_page(struct pgtable_debug_args *args, int or= der) +{ + struct page *page =3D NULL; + +#ifdef CONFIG_CONTIG_ALLOC + if (order >=3D MAX_ORDER) { + page =3D alloc_contig_pages((1 << order), GFP_KERNEL, + first_online_node, NULL); + if (page) { + args->is_contiguous_page =3D true; + return page; + } + } +#endif + + if (order < MAX_ORDER) + page =3D alloc_pages(GFP_KERNEL, order); + + return page; +} + +static int __init init_args(struct pgtable_debug_args *args) +{ + struct page *page =3D NULL; + phys_addr_t phys; + int ret =3D 0; + + /* + * Initialize the debugging data. + * + * __P000 (or even __S000) will help create page table entries with + * PROT_NONE permission as required for pxx_protnone_tests(). + */ + memset(args, 0, sizeof(*args)); + args->vaddr =3D get_random_vaddr(); + args->page_prot =3D vm_get_page_prot(VMFLAGS); + args->page_prot_none =3D __P000; + args->is_contiguous_page =3D false; + args->pud_pfn =3D ULONG_MAX; + args->pmd_pfn =3D ULONG_MAX; + args->pte_pfn =3D ULONG_MAX; + args->fixed_pgd_pfn =3D ULONG_MAX; + args->fixed_p4d_pfn =3D ULONG_MAX; + args->fixed_pud_pfn =3D ULONG_MAX; + args->fixed_pmd_pfn =3D ULONG_MAX; + args->fixed_pte_pfn =3D ULONG_MAX; + + /* Allocate mm and vma */ + args->mm =3D mm_alloc(); + if (!args->mm) { + pr_err("Failed to allocate mm struct\n"); + ret =3D -ENOMEM; + goto error; + } + + args->vma =3D vm_area_alloc(args->mm); + if (!args->vma) { + pr_err("Failed to allocate vma\n"); + ret =3D -ENOMEM; + goto error; + } + + /* + * Allocate page table entries. They will be modified in the tests. + * Lets save the page table entries so that they can be released + * when the tests are completed. + */ + args->pgdp =3D pgd_offset(args->mm, args->vaddr); + args->p4dp =3D p4d_alloc(args->mm, args->pgdp, args->vaddr); + if (!args->p4dp) { + pr_err("Failed to allocate p4d entries\n"); + ret =3D -ENOMEM; + goto error; + } + args->start_p4dp =3D p4d_offset(args->pgdp, 0UL); + WARN_ON(!args->start_p4dp); + + args->pudp =3D pud_alloc(args->mm, args->p4dp, args->vaddr); + if (!args->pudp) { + pr_err("Failed to allocate pud entries\n"); + ret =3D -ENOMEM; + goto error; + } + args->start_pudp =3D pud_offset(args->p4dp, 0UL); + WARN_ON(!args->start_pudp); + + args->pmdp =3D pmd_alloc(args->mm, args->pudp, args->vaddr); + if (!args->pmdp) { + pr_err("Failed to allocate pmd entries\n"); + ret =3D -ENOMEM; + goto error; + } + args->start_pmdp =3D pmd_offset(args->pudp, 0UL); + WARN_ON(!args->start_pmdp); + + if (pte_alloc(args->mm, args->pmdp)) { + pr_err("Failed to allocate pte entries\n"); + ret =3D -ENOMEM; + goto error; + } + args->start_ptep =3D pmd_pgtable(READ_ONCE(*args->pmdp)); + WARN_ON(!args->start_ptep); + + /* + * PFN for mapping at PTE level is determined from a standard kernel + * text symbol. But pfns for higher page table levels are derived by + * masking lower bits of this real pfn. These derived pfns might not + * exist on the platform but that does not really matter as pfn_pxx() + * helpers will still create appropriate entries for the test. This + * helps avoid large memory block allocations to be used for mapping + * at higher page table levels in some of the tests. + */ + phys =3D __pa_symbol(&start_kernel); + args->fixed_pgd_pfn =3D __phys_to_pfn(phys & PGDIR_MASK); + args->fixed_p4d_pfn =3D __phys_to_pfn(phys & P4D_MASK); + args->fixed_pud_pfn =3D __phys_to_pfn(phys & PUD_MASK); + args->fixed_pmd_pfn =3D __phys_to_pfn(phys & PMD_MASK); + args->fixed_pte_pfn =3D __phys_to_pfn(phys & PAGE_MASK); + WARN_ON(!pfn_valid(args->fixed_pte_pfn)); + + /* + * Allocate (huge) pages because some of the tests need to access + * the data in the pages. The corresponding tests will be skipped + * if we fail to allocate (huge) pages. + */ + if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) && + IS_ENABLED(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD) && + has_transparent_hugepage()) { + page =3D debug_vm_pgtable_alloc_huge_page(args, + HPAGE_PUD_SHIFT - PAGE_SHIFT); + if (page) { + args->pud_pfn =3D page_to_pfn(page); + args->pmd_pfn =3D args->pud_pfn; + args->pte_pfn =3D args->pud_pfn; + return 0; + } + } + + if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) && + has_transparent_hugepage()) { + page =3D debug_vm_pgtable_alloc_huge_page(args, HPAGE_PMD_ORDER); + if (page) { + args->pmd_pfn =3D page_to_pfn(page); + args->pte_pfn =3D args->pmd_pfn; + return 0; + } + } + + page =3D alloc_pages(GFP_KERNEL, 0); + if (page) + args->pte_pfn =3D page_to_pfn(page); + + return 0; + +error: + destroy_args(args); + return ret; +} + static int __init debug_vm_pgtable(void) { + struct pgtable_debug_args args; struct vm_area_struct *vma; struct mm_struct *mm; pgd_t *pgdp; @@ -970,9 +1232,13 @@ static int __init debug_vm_pgtable(void) unsigned long vaddr, pte_aligned, pmd_aligned; unsigned long pud_aligned, p4d_aligned, pgd_aligned; spinlock_t *ptl =3D NULL; - int idx; + int idx, ret; =20 pr_info("Validating architecture page table helpers\n"); + ret =3D init_args(&args); + if (ret) + return ret; + prot =3D vm_get_page_prot(VMFLAGS); vaddr =3D get_random_vaddr(); mm =3D mm_alloc(); @@ -1127,6 +1393,8 @@ static int __init debug_vm_pgtable(void) mm_dec_nr_pmds(mm); mm_dec_nr_ptes(mm); mmdrop(mm); + + destroy_args(&args); return 0; } late_initcall(debug_vm_pgtable); --=20 2.23.0