From patchwork Tue Jan 10 18:12:08 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: 13095459 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 69B0EC46467 for ; Tue, 10 Jan 2023 18:12:15 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6EE448E0002; Tue, 10 Jan 2023 13:12:14 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 69DE08E0001; Tue, 10 Jan 2023 13:12:14 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 565668E0002; Tue, 10 Jan 2023 13:12:14 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 4762A8E0001 for ; Tue, 10 Jan 2023 13:12:14 -0500 (EST) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id E5E5EA1443 for ; Tue, 10 Jan 2023 18:12:13 +0000 (UTC) X-FDA: 80339683746.10.FAEB0B7 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) by imf26.hostedemail.com (Postfix) with ESMTP id 633D914000C for ; Tue, 10 Jan 2023 18:12:12 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=iDfgH84M; spf=pass (imf26.hostedemail.com: domain of 3e6q9YwQKCOoRhPXSaaSXQ.OaYXUZgj-YYWhMOW.adS@flex--fvdl.bounces.google.com designates 209.85.214.201 as permitted sender) smtp.mailfrom=3e6q9YwQKCOoRhPXSaaSXQ.OaYXUZgj-YYWhMOW.adS@flex--fvdl.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1673374332; a=rsa-sha256; cv=none; b=WMLtWX80o9wZtWi5HY2xhykM/VHlHADZ2EqVkaoJ4I/yFwFofxbFmE1fYz7gXUqD6SiOJF LOpYF8c3St9ulXZos0cE7L71htYqmFIIk30TuvXsNHZ+UQV9GyJ6xS4I90+0SQGy39V1D6 EGY4u3UOZxHw+Hs4kRUF97+E0qiJQ2g= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=iDfgH84M; spf=pass (imf26.hostedemail.com: domain of 3e6q9YwQKCOoRhPXSaaSXQ.OaYXUZgj-YYWhMOW.adS@flex--fvdl.bounces.google.com designates 209.85.214.201 as permitted sender) smtp.mailfrom=3e6q9YwQKCOoRhPXSaaSXQ.OaYXUZgj-YYWhMOW.adS@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=1673374332; 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=HlK/llb+nxfN9PasvJFeioL2b1ZJZyTPv7Bw7vANtL0=; b=PVFxGwcpMt6no0GjX1GaAZWVm5EIQJt8mpGOxeUFHiF2ZlpIBHvPnTUFjQIL6TZEbrBm9E Cf3fue6cTIzh7CW9jFbF5CNuy2meMxHuohOXvonOzZRdaoeltn2f1Z0DGEulG8d0BN1Pl0 7lK0QQZ9Y1Lisj097wrw3TvkiMeiP4M= Received: by mail-pl1-f201.google.com with SMTP id p15-20020a170902a40f00b00192b2bbb7f8so8946470plq.14 for ; Tue, 10 Jan 2023 10:12:12 -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=HlK/llb+nxfN9PasvJFeioL2b1ZJZyTPv7Bw7vANtL0=; b=iDfgH84MU576tgnMq+c2qN9TcAJfEPhDwTDPDGM+A7ox1kMDk9rLsRAfGfOIEGg2cy bJFbLjdVm0S5BXezZs20QIYEz6Vtb9WEhXyHQLeEhExSVYjrXdPtNJAB/qtXe4+scR/x OxiYcDT0UFMHiEtWJlihgUJkcDA4VAY6QYRmFdw+UpwOfdbNOXCsedIp1x254H62n0/4 NnRPkvC6RKFfEmGgi/r8X0CzlHYQBZwwbdt/mpdpU+Zalt9V1nictzwYtFpnA/RTgDLI VPf8NUkxJm7AezqqPFldxEvr6CtSbvBj0NjumXyHGDDuz0icogBb1aTzegy8+FdKSvBX XakA== 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=HlK/llb+nxfN9PasvJFeioL2b1ZJZyTPv7Bw7vANtL0=; b=j4eW1yVmUmPu4ZHodEtcFAFkbB3aQXnqkZVT4RbGnXohdmZ1xLmg6VF5T+7uGKIoa0 DrMf3/hY0y5KUNGR29wg1BPnb0Q6md9WNX/WBKY71aVKA4NzttkW0YfPvMp8xzXY7V15 JdmDih64zURkJia2/zDWjusQlipywYHKLdEEeZQUxlil9UgK9zE3Tiiweo9O6BEj/9ra jvSHx/z5m3B9/7bYjr9Uo4PG6WKE+AZWlJysY5W6O1fV+AGQ5iYzI+7J/hncCReJbvoP GlMMuowXYpiUNWTIjs7hiZGg80VMBdaa6l4wa715HwC05x4Ro6ithsUE33gOLfNTZWo7 FQQQ== X-Gm-Message-State: AFqh2koP+Pxx38dsx3Rm87Iu53zj6bvDT4D5UcwkyglcAw7vaMGmFXKB OaTfcIDKo+yi0vvXX2xrepe46UDG X-Google-Smtp-Source: AMrXdXvbxixMEt1bP2hWcq5pFzfkr7k90ObAm4dqhOBwwU8zV1GTBy6owE03lLyFKpoJWYHm6PVkNrdN X-Received: from fvdl-cloudtop.c.googlers.com ([fda3:e722:ac3:cc00:24:72f4:c0a8:46ab]) (user=fvdl job=sendgmr) by 2002:a17:902:8a8f:b0:190:fc28:8cb6 with SMTP id p15-20020a1709028a8f00b00190fc288cb6mr3958683plo.144.1673374331176; Tue, 10 Jan 2023 10:12:11 -0800 (PST) Date: Tue, 10 Jan 2023 18:12:08 +0000 Mime-Version: 1.0 X-Mailer: git-send-email 2.39.0.314.g84b9a713c41-goog Message-ID: <20230110181208.1633879-1-fvdl@google.com> Subject: [PATCH v3] 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-Rspam-User: X-Rspamd-Queue-Id: 633D914000C X-Rspamd-Server: rspam01 X-Stat-Signature: jeq9jjpwx4imfzp11eoret5p3jr4dj1e X-HE-Tag: 1673374332-777734 X-HE-Meta: U2FsdGVkX19LRZbly1DWnN9DcuNXCPbMWz8g11OIHO4WJ1MGUTCJo4qHxEcmU1zEPtwU00v8g4tC6Qy/uAMMGoxuw6e2ouv8Nci/Gu3sQhZ7Y8XqSwKl3C7IdZxN6/dMdBMVGPv5wsfEu4BnlcqWgbwX3EAgYf9eoY/lywZZgJEfPNj/GvtruenqHXHm6sWr3XfAMKzQFfcerisxAjY2DcF8MFXE0Of9dhpqzvtNr4nW9d13nArnyirP1wwsaZYAHgYutwxG5GADFmwnMSkWtKKUVSYUOTfP04fN+eQpXx011I8dhyCgrwsMcB/dTQCkz3chk2YwcAL0B1rmgpWQ8gtjm6H5El82zYX9zStiooabQH7ZmbKrcFVHOdNYYjiAH6LwVW5GnmvJs9yA6wA58ZNyfHnEp6Ln/T6r8abwb8QFnqKct4YQEPV6GpsY1vfXkK+OBK6JKF6emWjRIAwi1YRfjQwu/7a+C/2MnW12Sm4UZRMn/+aelJL566WmCFvtwK/lEDZfnTCvdCWbgiAcK07yN7IZ3CYo+WoCSo7PV1+0woJt/V5DiiLtZ2CxqFCE/YLUVQKsLlUOLULgcd0lgjdaGN+fI4+3br1XRYZ9O6P7c2agSfNyKdegJTYXm9SQdtezYJWZRp5XlMPSlhWNdxegml7onrf7XdALW2guseLeIhdlj4uBLCh9hF7oG1dlmZRU6cMwZFE83LtgsvLae8qEAPjAvennWF5UUkcY3R3yZqEDOmVgksnsbUXSbvNVyST5jo7qfE+NapmXNb6wiIxqheHSycRK0x0KQagwmtzNSEZXtOfx6gc8/iFq3Cc8tAUWryGUacTVXtbUVY8DZDFEHW5E0xEzoFHaH1J+vPJLOmzfa5zByMny3c8T1dYNCDmcOXCJteC7eMxGDHyVdn4xfNMNMGrcdAhjMmU1cGvESRwkWvDXwNP0nXr87/tLH0EHTRgzjNR6U+4Pgco VCP4Akyh bhSCMoLseorHwFRQzCmVDok0h1OhtKh74YHCT77wYrd/9QAj52LzVn2Wt9Qms+zWOQ1y9D7ukahsOeOqZq+mej6KGv6s/qaYp5YITa+ICJOZYsWKkMwFBF/fEnS0D6Mhb6vux2QzUEy2P43fm7VHK2M998UIgtBlHMVAw/vjVI6xeJj9k94vjqNdkb+32AbqN0naTUvltR6O6a8nVzGU2mxeJ2zKA6bnqkG2HLCuhbW/zDUxfTqQyeEJ3ndvlV6yq9+ruPujK8S6AFi/yASMjgLWAP3y+ORsnCp507aFa0e9RxmqT8Mxaujq3RGTRrkk/92QQ/b+N6Mo+yfHMtG24MeNaU9SlWMilzMcw 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 | 102 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 83 insertions(+), 19 deletions(-) diff --git a/mm/debug_vm_pgtable.c b/mm/debug_vm_pgtable.c index c631ade3f1d2..bb3328f46126 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,85 @@ 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 +1239,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