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=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 06AEBC33CA1 for ; Wed, 8 Jan 2020 14:57:21 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id BB9F6206F0 for ; Wed, 8 Jan 2020 14:57:20 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org BB9F6206F0 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 36C098E0005; Wed, 8 Jan 2020 09:57:20 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 31C5E8E0001; Wed, 8 Jan 2020 09:57:20 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 234288E0005; Wed, 8 Jan 2020 09:57:20 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0004.hostedemail.com [216.40.44.4]) by kanga.kvack.org (Postfix) with ESMTP id 0AE218E0001 for ; Wed, 8 Jan 2020 09:57:20 -0500 (EST) Received: from smtpin19.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with SMTP id A46D0181AC9CC for ; Wed, 8 Jan 2020 14:57:19 +0000 (UTC) X-FDA: 76354770198.19.bird24_3fe1730cb1444 X-HE-Tag: bird24_3fe1730cb1444 X-Filterd-Recvd-Size: 9219 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf27.hostedemail.com (Postfix) with ESMTP for ; Wed, 8 Jan 2020 14:57:18 +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 08D7531B; Wed, 8 Jan 2020 06:57:18 -0800 (PST) Received: from e112269-lin.cambridge.arm.com (e112269-lin.cambridge.arm.com [10.1.194.52]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id E1BE13F703; Wed, 8 Jan 2020 06:57:16 -0800 (PST) From: Steven Price To: Andrew Morton , x86@kernel.org Cc: Stephen Rothwell , Linux Next Mailing List , Linux Kernel Mailing List , linux-mm@kvack.org, linux-arm-kernel@lists.infradead.org, Steven Price Subject: [PATCH] x86: mm: Avoid allocating struct mm_struct on the stack Date: Wed, 8 Jan 2020 14:57:10 +0000 Message-Id: <20200108145710.34314-1-steven.price@arm.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200108145248.GA2584@arm.com> References: <20200108145248.GA2584@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: struct mm_struct is quite large (~1664 bytes) and so allocating on the stack may cause problems as the kernel stack size is small. Since ptdump_walk_pgd_level_core() was only allocating the structure so that it could modify the pgd argument we can instead introduce a pgd override in struct mm_walk and pass this down the call stack to where it is needed. Since the correct mm_struct is now being passed down, it is now also unnecessary to take the mmap_sem semaphore because ptdump_walk_pgd() will now take the semaphore on the real mm. Signed-off-by: Steven Price --- arch/arm64/mm/dump.c | 4 ++-- arch/x86/mm/debug_pagetables.c | 10 ++-------- arch/x86/mm/dump_pagetables.c | 18 +++++++----------- include/linux/pagewalk.h | 3 +++ include/linux/ptdump.h | 2 +- mm/pagewalk.c | 7 ++++++- mm/ptdump.c | 4 ++-- 7 files changed, 23 insertions(+), 25 deletions(-) diff --git a/arch/arm64/mm/dump.c b/arch/arm64/mm/dump.c index ef4b3ca1e058..860c00ec8bd3 100644 --- a/arch/arm64/mm/dump.c +++ b/arch/arm64/mm/dump.c @@ -323,7 +323,7 @@ void ptdump_walk(struct seq_file *s, struct ptdump_in= fo *info) } }; =20 - ptdump_walk_pgd(&st.ptdump, info->mm); + ptdump_walk_pgd(&st.ptdump, info->mm, NULL); } =20 static void ptdump_initialize(void) @@ -361,7 +361,7 @@ void ptdump_check_wx(void) } }; =20 - ptdump_walk_pgd(&st.ptdump, &init_mm); + ptdump_walk_pgd(&st.ptdump, &init_mm, NULL); =20 if (st.wx_pages || st.uxn_pages) pr_warn("Checked W+X mappings: FAILED, %lu W+X pages found, %lu non-UX= N pages found\n", diff --git a/arch/x86/mm/debug_pagetables.c b/arch/x86/mm/debug_pagetable= s.c index d0efec713c6c..4a3b62f780b4 100644 --- a/arch/x86/mm/debug_pagetables.c +++ b/arch/x86/mm/debug_pagetables.c @@ -15,11 +15,8 @@ DEFINE_SHOW_ATTRIBUTE(ptdump); =20 static int ptdump_curknl_show(struct seq_file *m, void *v) { - if (current->mm->pgd) { - down_read(¤t->mm->mmap_sem); + if (current->mm->pgd) ptdump_walk_pgd_level_debugfs(m, current->mm, false); - up_read(¤t->mm->mmap_sem); - } return 0; } =20 @@ -28,11 +25,8 @@ DEFINE_SHOW_ATTRIBUTE(ptdump_curknl); #ifdef CONFIG_PAGE_TABLE_ISOLATION static int ptdump_curusr_show(struct seq_file *m, void *v) { - if (current->mm->pgd) { - down_read(¤t->mm->mmap_sem); + if (current->mm->pgd) ptdump_walk_pgd_level_debugfs(m, current->mm, true); - up_read(¤t->mm->mmap_sem); - } return 0; } =20 diff --git a/arch/x86/mm/dump_pagetables.c b/arch/x86/mm/dump_pagetables.= c index 411f6a758998..64229dad7eab 100644 --- a/arch/x86/mm/dump_pagetables.c +++ b/arch/x86/mm/dump_pagetables.c @@ -357,7 +357,8 @@ static void note_page(struct ptdump_state *pt_st, uns= igned long addr, int level, } } =20 -static void ptdump_walk_pgd_level_core(struct seq_file *m, pgd_t *pgd, +static void ptdump_walk_pgd_level_core(struct seq_file *m, + struct mm_struct *mm, pgd_t *pgd, bool checkwx, bool dmesg) { const struct ptdump_range ptdump_ranges[] =3D { @@ -386,12 +387,7 @@ static void ptdump_walk_pgd_level_core(struct seq_fi= le *m, pgd_t *pgd, .seq =3D m }; =20 - struct mm_struct fake_mm =3D { - .pgd =3D pgd - }; - init_rwsem(&fake_mm.mmap_sem); - - ptdump_walk_pgd(&st.ptdump, &fake_mm); + ptdump_walk_pgd(&st.ptdump, mm, pgd); =20 if (!checkwx) return; @@ -404,7 +400,7 @@ static void ptdump_walk_pgd_level_core(struct seq_fil= e *m, pgd_t *pgd, =20 void ptdump_walk_pgd_level(struct seq_file *m, struct mm_struct *mm) { - ptdump_walk_pgd_level_core(m, mm->pgd, false, true); + ptdump_walk_pgd_level_core(m, mm, mm->pgd, false, true); } =20 void ptdump_walk_pgd_level_debugfs(struct seq_file *m, struct mm_struct = *mm, @@ -415,7 +411,7 @@ void ptdump_walk_pgd_level_debugfs(struct seq_file *m= , struct mm_struct *mm, if (user && boot_cpu_has(X86_FEATURE_PTI)) pgd =3D kernel_to_user_pgdp(pgd); #endif - ptdump_walk_pgd_level_core(m, pgd, false, false); + ptdump_walk_pgd_level_core(m, mm, pgd, false, false); } EXPORT_SYMBOL_GPL(ptdump_walk_pgd_level_debugfs); =20 @@ -430,13 +426,13 @@ void ptdump_walk_user_pgd_level_checkwx(void) =20 pr_info("x86/mm: Checking user space page tables\n"); pgd =3D kernel_to_user_pgdp(pgd); - ptdump_walk_pgd_level_core(NULL, pgd, true, false); + ptdump_walk_pgd_level_core(NULL, &init_mm, pgd, true, false); #endif } =20 void ptdump_walk_pgd_level_checkwx(void) { - ptdump_walk_pgd_level_core(NULL, INIT_PGD, true, false); + ptdump_walk_pgd_level_core(NULL, &init_mm, INIT_PGD, true, false); } =20 static int __init pt_dump_init(void) diff --git a/include/linux/pagewalk.h b/include/linux/pagewalk.h index 745a654c6ea7..b1cb6b753abb 100644 --- a/include/linux/pagewalk.h +++ b/include/linux/pagewalk.h @@ -74,6 +74,7 @@ enum page_walk_action { * mm_walk - walk_page_range data * @ops: operation to call during the walk * @mm: mm_struct representing the target process of page table walk + * @pgd: pointer to PGD; only valid with no_vma (otherwise set to NULL) * @vma: vma currently walked (NULL if walking outside vmas) * @action: next action to perform (see enum page_walk_action) * @no_vma: walk ignoring vmas (vma will always be NULL) @@ -84,6 +85,7 @@ enum page_walk_action { struct mm_walk { const struct mm_walk_ops *ops; struct mm_struct *mm; + pgd_t *pgd; struct vm_area_struct *vma; enum page_walk_action action; bool no_vma; @@ -95,6 +97,7 @@ int walk_page_range(struct mm_struct *mm, unsigned long= start, void *private); int walk_page_range_novma(struct mm_struct *mm, unsigned long start, unsigned long end, const struct mm_walk_ops *ops, + pgd_t *pgd, void *private); int walk_page_vma(struct vm_area_struct *vma, const struct mm_walk_ops *= ops, void *private); diff --git a/include/linux/ptdump.h b/include/linux/ptdump.h index b28f3f2acf90..a67065c403c3 100644 --- a/include/linux/ptdump.h +++ b/include/linux/ptdump.h @@ -17,6 +17,6 @@ struct ptdump_state { const struct ptdump_range *range; }; =20 -void ptdump_walk_pgd(struct ptdump_state *st, struct mm_struct *mm); +void ptdump_walk_pgd(struct ptdump_state *st, struct mm_struct *mm, pgd_= t *pgd); =20 #endif /* _LINUX_PTDUMP_H */ diff --git a/mm/pagewalk.c b/mm/pagewalk.c index 5895ce4f1a85..928df1638c30 100644 --- a/mm/pagewalk.c +++ b/mm/pagewalk.c @@ -206,7 +206,10 @@ static int walk_pgd_range(unsigned long addr, unsign= ed long end, const struct mm_walk_ops *ops =3D walk->ops; int err =3D 0; =20 - pgd =3D pgd_offset(walk->mm, addr); + if (walk->pgd) + pgd =3D walk->pgd + pgd_index(addr); + else + pgd =3D pgd_offset(walk->mm, addr); do { next =3D pgd_addr_end(addr, end); if (pgd_none_or_clear_bad(pgd)) { @@ -436,11 +439,13 @@ int walk_page_range(struct mm_struct *mm, unsigned = long start, */ int walk_page_range_novma(struct mm_struct *mm, unsigned long start, unsigned long end, const struct mm_walk_ops *ops, + pgd_t *pgd, void *private) { struct mm_walk walk =3D { .ops =3D ops, .mm =3D mm, + .pgd =3D pgd, .private =3D private, .no_vma =3D true }; diff --git a/mm/ptdump.c b/mm/ptdump.c index ad18a9839d6f..26208d0d03b7 100644 --- a/mm/ptdump.c +++ b/mm/ptdump.c @@ -122,14 +122,14 @@ static const struct mm_walk_ops ptdump_ops =3D { .pte_hole =3D ptdump_hole, }; =20 -void ptdump_walk_pgd(struct ptdump_state *st, struct mm_struct *mm) +void ptdump_walk_pgd(struct ptdump_state *st, struct mm_struct *mm, pgd_= t *pgd) { const struct ptdump_range *range =3D st->range; =20 down_read(&mm->mmap_sem); while (range->start !=3D range->end) { walk_page_range_novma(mm, range->start, range->end, - &ptdump_ops, st); + &ptdump_ops, pgd, st); range++; } up_read(&mm->mmap_sem); --=20 2.20.1