From patchwork Mon Jan 9 17:43:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Frank van der Linden X-Patchwork-Id: 13093960 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id A122FC6379F for ; Mon, 9 Jan 2023 17:43:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CE9E78E0005; Mon, 9 Jan 2023 12:43:48 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id C984A8E0001; Mon, 9 Jan 2023 12:43:48 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B5FF98E0005; Mon, 9 Jan 2023 12:43:48 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id A6EBD8E0001 for ; Mon, 9 Jan 2023 12:43:48 -0500 (EST) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 59BA81C5DE5 for ; Mon, 9 Jan 2023 17:43:48 +0000 (UTC) X-FDA: 80335983336.23.3D3370B Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) by imf03.hostedemail.com (Postfix) with ESMTP id CDAF820008 for ; Mon, 9 Jan 2023 17:43:45 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=Hv91rouB; spf=pass (imf03.hostedemail.com: domain of 3UFK8YwQKCAsq6owrzzrwp.nzxwty58-xxv6lnv.z2r@flex--fvdl.bounces.google.com designates 209.85.214.201 as permitted sender) smtp.mailfrom=3UFK8YwQKCAsq6owrzzrwp.nzxwty58-xxv6lnv.z2r@flex--fvdl.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1673286225; h=from:from:sender: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:in-reply-to: references:dkim-signature; bh=64tq07ZZEfTel55WnkWqJmjGMUhXoumUucc/79tdHMo=; b=v8+PywgGV8NNsMNQ/ZMkGlvlaVHV4aiJ1mBIXHeTYKDD8+DaI2nqN/K5Mu7hKfpiv3w4uK uHN8eb2K9pOFSCTy0xqXaywUcRGJOjjng/loQ5pq39bSsBEF/9yG8Pq7ahd6O73pjCvF4S 3DQjszusEKrz6juWa8T3O0DA0f0I9UM= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=Hv91rouB; spf=pass (imf03.hostedemail.com: domain of 3UFK8YwQKCAsq6owrzzrwp.nzxwty58-xxv6lnv.z2r@flex--fvdl.bounces.google.com designates 209.85.214.201 as permitted sender) smtp.mailfrom=3UFK8YwQKCAsq6owrzzrwp.nzxwty58-xxv6lnv.z2r@flex--fvdl.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1673286225; a=rsa-sha256; cv=none; b=QGsWrppgcXUSTL9cTN1gkiA/Q6B2y3iFLBpJPCKs43GPGWMlIPyfwTHUhlldQi7J1O+npX 5MhD8IuWt7ZiTPUmSSuv/+1xQRq/5dX5X4o2Pz0QRqzhPEEOnKfJ/lyr6YHf1ASqF6IJsd LakaoPhxgVyGG9WFHQlj0C9ZobtfgXE= Received: by mail-pl1-f201.google.com with SMTP id y8-20020a170902b48800b00192a600df83so6721833plr.15 for ; Mon, 09 Jan 2023 09:43:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=64tq07ZZEfTel55WnkWqJmjGMUhXoumUucc/79tdHMo=; b=Hv91rouByRSJMVGgsbzcnTJ3J+HtIUtZ8W+AYyZ4emPYfz4nS13cva9Wa/QBULdC9j 7/zHukdJc2FrTP2XqAPqaR1pMgwHAmc0hpDHAd7i81rsD0Z0vRvxh/lxtatElHrBhLl9 DNX6kTYq5CZzuJPSVUIh2gbrzn+0GDGIQR9Ox0ppxujLX+qnPKREMSNjzK/zGIJlQM/5 o/8xl7ebhB53rsZuKR38sajloTYkd9x6E5ejLt7H6tWzYDS3GzTMRylmUmLKcjp10El7 etgMHHPnl4TZur+kMprirsrKHwkc1nh1/7vsvW1FuGU1Wbdvjv6jnPEmO+0W649CZVkh 5bfA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=64tq07ZZEfTel55WnkWqJmjGMUhXoumUucc/79tdHMo=; b=tU6tu0x0M6N/FaHIn3VJ/uSHxDtV8XljIiFsApgWCCYH+WiSb2/+bXYVct6isgcKIG rdEbnYKTsyW/xgEfFBAPO+iHYzgQGQYrsxbwAN3fQNuSRon0mwuFDA3/zP/RB/J6vMMB jl3TLKnLFfJAGCbHTFgQgxKHvzP9fmNuIYPNeaQufpeG3V30ggGluzG+w7vG0gOq/2IB R02EH2bahyCVEI9xhr5C4PhfOPSXfZNkoQX8smxEM5XTbfiqfzCjHz6j2GeSSpbv3CSt gq8je6WLBlo8xy457ZWz26ESPX3uORUdi7y3Jyi0M2O0E6oI+pk+Car9UxVMm2AxZDki l+MQ== X-Gm-Message-State: AFqh2kq5DL61cLKV94VOZaNStc/Xo7xJ6nBTLbJXaDevs5jwjuFt0X15 /bmA+CUT2g2FIp2U43jkvtubJqrn X-Google-Smtp-Source: AMrXdXuBaxmztt8J6Uspw5CHIoV5wfkO+wn+zBniHRc+H/3vfg/Wq8j23dZa0eQwg+4RlRiG819Q5jh0 X-Received: from fvdl-cloudtop.c.googlers.com ([fda3:e722:ac3:cc00:24:72f4:c0a8:46ab]) (user=fvdl job=sendgmr) by 2002:a17:902:b611:b0:189:a5b9:4838 with SMTP id b17-20020a170902b61100b00189a5b94838mr3704420pls.104.1673286224721; Mon, 09 Jan 2023 09:43:44 -0800 (PST) Date: Mon, 9 Jan 2023 17:43:32 +0000 Mime-Version: 1.0 X-Mailer: git-send-email 2.39.0.314.g84b9a713c41-goog Message-ID: <20230109174332.329366-1-fvdl@google.com> Subject: [PATCH v2] mm/debug: use valid physical memory for pmd/pud tests From: Frank van der Linden To: Andrew Morton , linux-mm@kvack.org Cc: Frank van der Linden , Anshuman Khandual X-Stat-Signature: udbu47wqmno98x8ikr4gpq9g64zgtm3n X-Rspam-User: X-Rspamd-Queue-Id: CDAF820008 X-Rspamd-Server: rspam06 X-HE-Tag: 1673286225-143534 X-HE-Meta: U2FsdGVkX1/Z50caLYQumrRHldMfIJEXYm3PjqTem26jK4/F8+xxe0prc3SKtQPHI4DjWjgdxwKQboqxGGvpztCKxpWfhZXRnAzUOKAHd7o1bE6+a+vdpMtIJtwfIWfQNImkAhhOqtYqxV5qCLwZ9H32ln8WWk92jdDj/dvoDeS1fhC07NZsUhNlUtMbKK0Kk7tnvXuCb1Hplkay0twXRrUZh1DrJwKT9k/hDWOyTNP7aAYyneqKzLjr4tiGlYJWm28UbnlZm6tR8d+rYFzIwSXmhznQAiV9S1reGJU+/m+Nb88HgtyDwFFH3PQyPC3drzQTI5JX9/vEQEhcYeBFFmmpQrJJDPqlGNc/BWvo5fc7Paph/SkpcjG2OpgA6/JW0LBfN562e9+mPOG3UME+24nP63CuFdu8s1xvPmnJEpwLRna7kA7cSWzHmfb3R7Pvq1uxY4GUnCIQANVk9yJOqmVBliJvR2+dQpowKFJHZYVp0QY001UwYZ5ZWcBTif/vcYPpoeC5RDI2Iw8A8IPzuLDA0hIuopwTx+pqxY+WwVBuBC3NIzNMrEYklpjEMnM0dM3gZDDpdTNLHPmI685bDQCxHmrVcE4f6Owkbf8asSC3h6DfMLbiTeVn5UoiMjOPvD8EOfjgIwLIKi2yNvC78R993dnuWezH4ZOd1oMNvHVEWho1WLckSHsa9VrRmEfPrigxgbdXDBKbJEO9Q1tfJeCJ0uAC9n+QvWtuvRdic1xOvbhfgkgFrOJCtVr8dbZofcnOiMTk1vmy3ASc4wrCjKJxK27LZqG5nhCC/aZOjtpF4r0AcOflZuK1O6kCupIGgx/ZFzGgH2pdgpGpi8Etc+nuEwYOQa9Gjt2VVo0gFfz4kp+/MScDo7PEYYCI3voXaADx0/adEl0dN8BnDgvD3x5bcCfG4+49rLVyRyMgPWq39HdnoWQ2140p/EGJMwtdttD6STsvyxDREcSWdlh 02zExlXp pCKYi5FkO/kttq5rNN1Au9o/xiaaiLv+wZfAxF002UuV+WsUJ4e6Jw5FLVSd6hugMtrx7dnVGekAkM9Rw9DhniLxxHoa2BGbtKIaiffehha9TTLc4yU1ZWwm63Vkh2Cv4GBTVotHXZEEoljockYW7QpnemtoS1eNtyARifOV+BTW/jUB/yuoVngBFGBdB/6Vq+pf/4SQNyv2rpM+SJEM7ESKtaLrMj/LbD8Nyh5PNqIWv5EkkpAsEXPQXKIz7W6tiah4ojBGzUxjp5q765mUS1BzYBSXxlse9RVhMgDRp6+C+ML/YE/ckMrDNjX38P6dkoF0bO7pW99/lSjmFSH/ANos9/gkDbw9cpgku 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: The page table debug tests need a physical address to validate low-level page table manipulation with. The memory at this address is not actually touched, it just encoded in the page table entries at various levels during the tests only. Since the memory is not used, the code just picks the physical address of the start_kernel symbol. This value is then truncated to get a properly aligned address that is to be used for various tests. Because of the truncation, the address might not actually exist, or might not describe a complete huge page. That's not a problem for most tests, but the arch-specific code may check for attribute validity and consistency. The x86 version of {pud,pmd}_set_huge actually validates the MTRRs for the PMD/PUD range. This may fail with an address derived from start_kernel, depending on where the kernel was loaded and what the physical memory layout of the system is. This then leads to false negatives for the {pud,pmd}_set_huge tests. Avoid this by finding a properly aligned memory range that exists and is usable. If such a range is not found, skip the tests that needed it. Fixes: 399145f9eb6c ("mm/debug: add tests validating architecture page table helpers") Cc: Anshuman Khandual Signed-off-by: Frank van der Linden Reviewed-by: Anshuman Khandual --- mm/debug_vm_pgtable.c | 103 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 84 insertions(+), 19 deletions(-) diff --git a/mm/debug_vm_pgtable.c b/mm/debug_vm_pgtable.c index c631ade3f1d2..503358332c92 100644 --- a/mm/debug_vm_pgtable.c +++ b/mm/debug_vm_pgtable.c @@ -15,6 +15,7 @@ #include #include #include +#include #include #include #include @@ -80,6 +81,7 @@ struct pgtable_debug_args { unsigned long pmd_pfn; unsigned long pte_pfn; + unsigned long fixed_alignment; unsigned long fixed_pgd_pfn; unsigned long fixed_p4d_pfn; unsigned long fixed_pud_pfn; @@ -430,7 +432,8 @@ static void __init pmd_huge_tests(struct pgtable_debug_args *args) { pmd_t pmd; - if (!arch_vmap_pmd_supported(args->page_prot)) + if (!arch_vmap_pmd_supported(args->page_prot) || + args->fixed_alignment < PMD_SIZE) return; pr_debug("Validating PMD huge\n"); @@ -449,7 +452,8 @@ static void __init pud_huge_tests(struct pgtable_debug_args *args) { pud_t pud; - if (!arch_vmap_pud_supported(args->page_prot)) + if (!arch_vmap_pud_supported(args->page_prot) || + args->fixed_alignment < PUD_SIZE) return; pr_debug("Validating PUD huge\n"); @@ -1077,10 +1081,86 @@ debug_vm_pgtable_alloc_huge_page(struct pgtable_debug_args *args, int order) return page; } +/* + * Check if a physical memory range described by contains + * an area that is of size psize, and aligned to psize. + * + * Don't use address 0, an all-zeroes physical address might mask bugs, and + * it's not used on x86. + */ +static void __init phys_align_check(phys_addr_t pstart, + phys_addr_t pend, unsigned long psize, phys_addr_t *physp, + unsigned long *alignp) +{ + phys_addr_t aligned_start, aligned_end; + + if (pstart == 0) + pstart = PAGE_SIZE; + + aligned_start = ALIGN(pstart, psize); + aligned_end = aligned_start + psize; + + if (aligned_end > aligned_start && aligned_end <= pend) { + *alignp = psize; + *physp = aligned_start; + } +} + +static void __init init_fixed_pfns(struct pgtable_debug_args *args) +{ + u64 idx; + phys_addr_t phys, pstart, pend; + + /* + * Initialize the fixed pfns. To do this, try to find a + * valid physical range, preferably aligned to PUD_SIZE, + * but settling for aligned to PMD_SIZE as a fallback. If + * neither of those is found, use the physical address of + * the start_kernel symbol. + * + * The memory doesn't need to be allocated, it just needs to exist + * as usable memory. It won't be touched. + * + * The alignment is recorded, and can be checked to see if we + * can run the tests that require an actual valid physical + * address range on some architectures ({pmd,pud}_huge_test + * on x86). + */ + + phys = __pa_symbol(&start_kernel); + args->fixed_alignment = PAGE_SIZE; + + for_each_mem_range(idx, &pstart, &pend) { + /* First check for a PUD-aligned area */ + phys_align_check(pstart, pend, PUD_SIZE, &phys, + &args->fixed_alignment); + + /* If a PUD-aligned area is found, we're done */ + if (args->fixed_alignment >= PUD_SIZE) + break; + + /* + * If no PMD-aligned area found yet, check for one, + * but continue the loop to look for a PUD-aligned area. + */ + if (args->fixed_alignment < PMD_SIZE) { + phys_align_check(pstart, pend, PMD_SIZE, &phys, + &args->fixed_alignment); + } + } + + args->fixed_pgd_pfn = __phys_to_pfn(phys & PGDIR_MASK); + args->fixed_p4d_pfn = __phys_to_pfn(phys & P4D_MASK); + args->fixed_pud_pfn = __phys_to_pfn(phys & PUD_MASK); + args->fixed_pmd_pfn = __phys_to_pfn(phys & PMD_MASK); + args->fixed_pte_pfn = __phys_to_pfn(phys & PAGE_MASK); + WARN_ON(!pfn_valid(args->fixed_pte_pfn)); +} + + static int __init init_args(struct pgtable_debug_args *args) { struct page *page = NULL; - phys_addr_t phys; int ret = 0; /* @@ -1160,22 +1240,7 @@ static int __init init_args(struct pgtable_debug_args *args) args->start_ptep = 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 = __pa_symbol(&start_kernel); - args->fixed_pgd_pfn = __phys_to_pfn(phys & PGDIR_MASK); - args->fixed_p4d_pfn = __phys_to_pfn(phys & P4D_MASK); - args->fixed_pud_pfn = __phys_to_pfn(phys & PUD_MASK); - args->fixed_pmd_pfn = __phys_to_pfn(phys & PMD_MASK); - args->fixed_pte_pfn = __phys_to_pfn(phys & PAGE_MASK); - WARN_ON(!pfn_valid(args->fixed_pte_pfn)); + init_fixed_pfns(args); /* * Allocate (huge) pages because some of the tests need to access