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=-9.8 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT autolearn=ham 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 0850CECE58C for ; Mon, 7 Oct 2019 15:39:40 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id BD57320640 for ; Mon, 7 Oct 2019 15:39:39 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org BD57320640 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=arm.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 729028E001A; Mon, 7 Oct 2019 11:39:39 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 6DAA88E0003; Mon, 7 Oct 2019 11:39:39 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5C7DD8E001A; Mon, 7 Oct 2019 11:39:39 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0222.hostedemail.com [216.40.44.222]) by kanga.kvack.org (Postfix) with ESMTP id 35F198E0003 for ; Mon, 7 Oct 2019 11:39:39 -0400 (EDT) Received: from smtpin16.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with SMTP id CB9D052C5 for ; Mon, 7 Oct 2019 15:39:38 +0000 (UTC) X-FDA: 76017398436.16.smoke14_712d09d602049 X-HE-Tag: smoke14_712d09d602049 X-Filterd-Recvd-Size: 8229 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf42.hostedemail.com (Postfix) with ESMTP for ; Mon, 7 Oct 2019 15:39:37 +0000 (UTC) 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 F34261576; Mon, 7 Oct 2019 08:39:36 -0700 (PDT) Received: from e112269-lin.arm.com (unknown [10.1.197.21]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 6881D3F6C4; Mon, 7 Oct 2019 08:39:34 -0700 (PDT) From: Steven Price To: linux-mm@kvack.org Cc: Steven Price , Andy Lutomirski , Ard Biesheuvel , Arnd Bergmann , Borislav Petkov , Catalin Marinas , Dave Hansen , Ingo Molnar , James Morse , =?UTF-8?q?J=C3=A9r=C3=B4me=20Glisse?= , Peter Zijlstra , Thomas Gleixner , Will Deacon , x86@kernel.org, "H. Peter Anvin" , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Mark Rutland , "Liang, Kan" , Andrew Morton Subject: [PATCH v11 19/22] mm: Add generic ptdump Date: Mon, 7 Oct 2019 16:38:19 +0100 Message-Id: <20191007153822.16518-20-steven.price@arm.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20191007153822.16518-1-steven.price@arm.com> References: <20191007153822.16518-1-steven.price@arm.com> MIME-Version: 1.0 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: Add a generic version of page table dumping that architectures can opt-in to Signed-off-by: Steven Price --- include/linux/ptdump.h | 21 ++++++ mm/Kconfig.debug | 21 ++++++ mm/Makefile | 1 + mm/ptdump.c | 150 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 193 insertions(+) create mode 100644 include/linux/ptdump.h create mode 100644 mm/ptdump.c diff --git a/include/linux/ptdump.h b/include/linux/ptdump.h new file mode 100644 index 000000000000..a0fb8dd2be97 --- /dev/null +++ b/include/linux/ptdump.h @@ -0,0 +1,21 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#ifndef _LINUX_PTDUMP_H +#define _LINUX_PTDUMP_H + +#include + +struct ptdump_range { + unsigned long start; + unsigned long end; +}; + +struct ptdump_state { + void (*note_page)(struct ptdump_state *st, unsigned long addr, + int level, unsigned long val); + const struct ptdump_range *range; +}; + +void ptdump_walk_pgd(struct ptdump_state *st, struct mm_struct *mm); + +#endif /* _LINUX_PTDUMP_H */ diff --git a/mm/Kconfig.debug b/mm/Kconfig.debug index 327b3ebf23bf..0271b22e063f 100644 --- a/mm/Kconfig.debug +++ b/mm/Kconfig.debug @@ -117,3 +117,24 @@ config DEBUG_RODATA_TEST depends on STRICT_KERNEL_RWX ---help--- This option enables a testcase for the setting rodata read-only. + +config GENERIC_PTDUMP + bool + +config PTDUMP_CORE + bool + +config PTDUMP_DEBUGFS + bool "Export kernel pagetable layout to userspace via debugfs" + depends on DEBUG_KERNEL + depends on DEBUG_FS + depends on GENERIC_PTDUMP + select PTDUMP_CORE + help + Say Y here if you want to show the kernel pagetable layout in a + debugfs file. This information is only useful for kernel developers + who are working in architecture specific areas of the kernel. + It is probably not a good idea to enable this feature in a production + kernel. + + If in doubt, say N. diff --git a/mm/Makefile b/mm/Makefile index d996846697ef..6e962007f186 100644 --- a/mm/Makefile +++ b/mm/Makefile @@ -107,3 +107,4 @@ obj-$(CONFIG_PERCPU_STATS) +=3D percpu-stats.o obj-$(CONFIG_ZONE_DEVICE) +=3D memremap.o obj-$(CONFIG_HMM_MIRROR) +=3D hmm.o obj-$(CONFIG_MEMFD_CREATE) +=3D memfd.o +obj-$(CONFIG_PTDUMP_CORE) +=3D ptdump.o diff --git a/mm/ptdump.c b/mm/ptdump.c new file mode 100644 index 000000000000..5d349311e77e --- /dev/null +++ b/mm/ptdump.c @@ -0,0 +1,150 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include +#include +#include + +static int ptdump_pgd_entry(pgd_t *pgd, unsigned long addr, + unsigned long next, struct mm_walk *walk) +{ + struct ptdump_state *st =3D walk->private; + pgd_t val =3D READ_ONCE(*pgd); + + if (pgd_leaf(val)) + st->note_page(st, addr, 1, pgd_val(val)); + + return 0; +} + +static int ptdump_p4d_entry(p4d_t *p4d, unsigned long addr, + unsigned long next, struct mm_walk *walk) +{ + struct ptdump_state *st =3D walk->private; + p4d_t val =3D READ_ONCE(*p4d); + + if (p4d_leaf(val)) + st->note_page(st, addr, 2, p4d_val(val)); + + return 0; +} + +static int ptdump_pud_entry(pud_t *pud, unsigned long addr, + unsigned long next, struct mm_walk *walk) +{ + struct ptdump_state *st =3D walk->private; + pud_t val =3D READ_ONCE(*pud); + + if (pud_leaf(val)) + st->note_page(st, addr, 3, pud_val(val)); + + return 0; +} + +static int ptdump_pmd_entry(pmd_t *pmd, unsigned long addr, + unsigned long next, struct mm_walk *walk) +{ + struct ptdump_state *st =3D walk->private; + pmd_t val =3D READ_ONCE(*pmd); + + if (pmd_leaf(val)) + st->note_page(st, addr, 4, pmd_val(val)); + + return 0; +} + +static int ptdump_pte_entry(pte_t *pte, unsigned long addr, + unsigned long next, struct mm_walk *walk) +{ + struct ptdump_state *st =3D walk->private; + + st->note_page(st, addr, 5, pte_val(READ_ONCE(*pte))); + + return 0; +} + +#ifdef CONFIG_KASAN +/* + * This is an optimization for KASAN=3Dy case. Since all kasan page tabl= es + * eventually point to the kasan_early_shadow_page we could call note_pa= ge() + * right away without walking through lower level page tables. This save= s + * us dozens of seconds (minutes for 5-level config) while checking for + * W+X mapping or reading kernel_page_tables debugfs file. + */ +static inline int note_kasan_page_table(struct mm_walk *walk, + unsigned long addr) +{ + struct ptdump_state *st =3D walk->private; + + st->note_page(st, addr, 5, pte_val(kasan_early_shadow_pte[0])); + return 1; +} + +static int ptdump_test_p4d(unsigned long addr, unsigned long next, + p4d_t *p4d, struct mm_walk *walk) +{ +#if CONFIG_PGTABLE_LEVELS > 4 + if (p4d =3D=3D lm_alias(kasan_early_shadow_p4d)) + return note_kasan_page_table(walk, addr); +#endif + return 0; +} + +static int ptdump_test_pud(unsigned long addr, unsigned long next, + pud_t *pud, struct mm_walk *walk) +{ +#if CONFIG_PGTABLE_LEVELS > 3 + if (pud =3D=3D lm_alias(kasan_early_shadow_pud)) + return note_kasan_page_table(walk, addr); +#endif + return 0; +} + +static int ptdump_test_pmd(unsigned long addr, unsigned long next, + pmd_t *pmd, struct mm_walk *walk) +{ +#if CONFIG_PGTABLE_LEVELS > 2 + if (pmd =3D=3D lm_alias(kasan_early_shadow_pmd)) + return note_kasan_page_table(walk, addr); +#endif + return 0; +} +#endif /* CONFIG_KASAN */ + +static int ptdump_hole(unsigned long addr, unsigned long next, + int depth, struct mm_walk *walk) +{ + struct ptdump_state *st =3D walk->private; + + st->note_page(st, addr, depth + 1, 0); + + return 0; +} + +const static struct mm_walk_ops ptdump_ops =3D { + .pgd_entry =3D ptdump_pgd_entry, + .p4d_entry =3D ptdump_p4d_entry, + .pud_entry =3D ptdump_pud_entry, + .pmd_entry =3D ptdump_pmd_entry, + .pte_entry =3D ptdump_pte_entry, +#ifdef CONFIG_KASAN + .test_p4d =3D ptdump_test_p4d, + .test_pud =3D ptdump_test_pud, + .test_pmd =3D ptdump_test_pmd, +#endif + .pte_hole =3D ptdump_hole, +}; + +void ptdump_walk_pgd(struct ptdump_state *st, struct mm_struct *mm) +{ + const struct ptdump_range *range =3D st->range; + + down_read(&mm->mmap_sem); + while (range->start !=3D range->end) { + walk_page_range(mm, range->start, range->end, &ptdump_ops, st); + range++; + } + up_read(&mm->mmap_sem); + + /* Flush out the last page */ + st->note_page(st, 0, 0, 0); +} --=20 2.20.1