From patchwork Wed Aug 26 15:14:46 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jann Horn X-Patchwork-Id: 11738959 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 2D619109B for ; Wed, 26 Aug 2020 15:15:56 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id EE7002054F for ; Wed, 26 Aug 2020 15:15:55 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=google.com header.i=@google.com header.b="EJaKPT+x" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org EE7002054F Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 3D1DE6B0030; Wed, 26 Aug 2020 11:15:55 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 3832E6B0031; Wed, 26 Aug 2020 11:15:55 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 29CBE8D0001; Wed, 26 Aug 2020 11:15:55 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0241.hostedemail.com [216.40.44.241]) by kanga.kvack.org (Postfix) with ESMTP id 142D16B0030 for ; Wed, 26 Aug 2020 11:15:55 -0400 (EDT) Received: from smtpin28.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id D13FE8249980 for ; Wed, 26 Aug 2020 15:15:54 +0000 (UTC) X-FDA: 77193069828.28.pan59_0c09f0b27065 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin28.hostedemail.com (Postfix) with ESMTP id 1C9EF6C3B for ; Wed, 26 Aug 2020 15:15:47 +0000 (UTC) X-Spam-Summary: 1,0,0,66eaece4c109a92b,d41d8cd98f00b204,3ohxgxwukclmctggazhhzex.vhfebgnq-ffdotvd.hkz@flex--jannh.bounces.google.com,,RULES_HIT:41:69:152:355:379:541:800:960:973:988:989:1260:1277:1313:1314:1345:1359:1437:1516:1518:1535:1544:1593:1594:1711:1730:1747:1777:1792:2393:2559:2562:2898:3138:3139:3140:3141:3142:3152:3354:3865:3866:3867:3868:3872:3874:4118:4321:4605:5007:6261:6653:7974:9592:9969:10004:11026:11473:11658:11914:12043:12291:12294:12296:12297:12438:12555:12683:12895:12986:14096:14097:14110:14181:14394:14659:14721:21080:21444:21627:21990:30003:30054:30070,0,RBL:209.85.218.73:@flex--jannh.bounces.google.com:.lbl8.mailshell.net-62.18.0.100 66.100.201.100;04y8cqszd77zfwm3ougrohmjyyckmoc394ch9k9ts5ufg9zkbfmmf9aq39er6kc.bqzm16sqznfpncbbqayeeusx9eqrkanjz7bbnzxfabe1b9cuydeebhyia3thfcp.4-lbl8.mailshell.net-223.238.255.100,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fp,MSBL:0,DNSBL:neutral,Custom_rules:0:0:0,LFtime:22,LUA_SUMMARY:non e X-HE-Tag: pan59_0c09f0b27065 X-Filterd-Recvd-Size: 7094 Received: from mail-ej1-f73.google.com (mail-ej1-f73.google.com [209.85.218.73]) by imf30.hostedemail.com (Postfix) with ESMTP for ; Wed, 26 Aug 2020 15:15:46 +0000 (UTC) Received: by mail-ej1-f73.google.com with SMTP id gf10so1125616ejb.12 for ; Wed, 26 Aug 2020 08:15:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=sender:date:in-reply-to:message-id:mime-version:references:subject :from:to:cc; bh=37TD1tOgbExZK4EAyJ2auNMattkG66IuBeoEm2InRhU=; b=EJaKPT+xdclNdzHyydB1wtD1f+JDKjLl0qDGYs/FfexMadEImdY6NbC9BpnQwNrpHM LkUSZYbx+9RUly980dt+z2mRNdOG3IbnwGhnUj4glyILCZ0N/jYte6+8NS03h1RlX0rd r3CIvAT8ajv3eqk1/JhxLYJIpzv+WTIYy/s8nkt7JCLLVf+OzXJ4bU+ZJ6YOy5JMa0Bk dXb+vpQGMQ5fMAY6yR2HvUUdC5QYsUzGd5rj4FciLVfkj7Tk8ky3X2ORz+eBuBCOUFkK T4TQvr1uvJ3sLQaCBbckv5jnLmhBWKh9jmcLxktaFyzUL50NN/IE0SwiX2wWUc7MHUtk MSig== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=37TD1tOgbExZK4EAyJ2auNMattkG66IuBeoEm2InRhU=; b=cwDC4G40Fp/UrNCiD+yneOdhK7hyGPkvsK138tXdXeq4q99Q9W1f8x3gUnHKvnP4mn YvXv+MCDO1x9iJiyUZ3guwU3gO9bImg6IApHLo44GAdSo9ipHo3Ba28LNcHKjXTx2P9V tofKq6KzlgeSzamf/IGhbb9UCzoSLHs5+7HEj4PV91ll9+tMZF/z7yEhHSXX9y+lHPs2 rZktr947ZrWwdIkhIIFjmf5Hf+4rrpaYsiO8Ov2IJ9sP7qxJ9bzoVwRNoBCuGnEqHC8M AMmhiG66zwhxZwDaYQKNaFF/0PCs3gDHAgB5pRna3XEkxEUfUexI6Te5dg9fDtfRXWMs 17dw== X-Gm-Message-State: AOAM530BdbWYi4f40rqB9ocen8WH2tx8FKq2n1Coo8T1zY/Mbmbeh23V rvGbOHs+GdLwrYgrkZCvKP6CIuMWPQ== X-Google-Smtp-Source: ABdhPJzt8BeQRY53dXQK6w4X+lfLoMxsOd+cnH57xkDOTHWo+hlkWgtx2J1Byi3ymA4q0Sa382PLimiVAw== X-Received: from jannh2.zrh.corp.google.com ([2a00:79e0:1b:201:1a60:24ff:fea6:bf44]) (user=jannh job=sendgmr) by 2002:a50:fe17:: with SMTP id f23mr13811629edt.364.1598454944949; Wed, 26 Aug 2020 08:15:44 -0700 (PDT) Date: Wed, 26 Aug 2020 17:14:46 +0200 In-Reply-To: <20200826151448.3404695-1-jannh@google.com> Message-Id: <20200826151448.3404695-4-jannh@google.com> Mime-Version: 1.0 References: <20200826151448.3404695-1-jannh@google.com> X-Mailer: git-send-email 2.28.0.297.g1956fa8f8d-goog Subject: [PATCH v4 3/5] coredump: Refactor page range dumping into common helper From: Jann Horn To: Andrew Morton Cc: Linus Torvalds , Christoph Hellwig , linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, Alexander Viro , "Eric W . Biederman" , Oleg Nesterov X-Rspamd-Queue-Id: 1C9EF6C3B X-Spamd-Result: default: False [0.00 / 100.00] X-Rspamd-Server: rspam01 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: Both fs/binfmt_elf.c and fs/binfmt_elf_fdpic.c need to dump ranges of pages into the coredump file. Extract that logic into a common helper. Signed-off-by: Jann Horn --- fs/binfmt_elf.c | 22 ++-------------------- fs/binfmt_elf_fdpic.c | 18 +++--------------- fs/coredump.c | 34 ++++++++++++++++++++++++++++++++++ include/linux/coredump.h | 2 ++ 4 files changed, 41 insertions(+), 35 deletions(-) diff --git a/fs/binfmt_elf.c b/fs/binfmt_elf.c index 13d053982dd7..5fd11a25d320 100644 --- a/fs/binfmt_elf.c +++ b/fs/binfmt_elf.c @@ -2419,26 +2419,8 @@ static int elf_core_dump(struct coredump_params *cprm) for (i = 0, vma = first_vma(current, gate_vma); vma != NULL; vma = next_vma(vma, gate_vma)) { - unsigned long addr; - unsigned long end; - - end = vma->vm_start + vma_filesz[i++]; - - for (addr = vma->vm_start; addr < end; addr += PAGE_SIZE) { - struct page *page; - int stop; - - page = get_dump_page(addr); - if (page) { - void *kaddr = kmap(page); - stop = !dump_emit(cprm, kaddr, PAGE_SIZE); - kunmap(page); - put_page(page); - } else - stop = !dump_skip(cprm, PAGE_SIZE); - if (stop) - goto end_coredump; - } + if (!dump_user_range(cprm, vma->vm_start, vma_filesz[i++])) + goto end_coredump; } dump_truncate(cprm); diff --git a/fs/binfmt_elf_fdpic.c b/fs/binfmt_elf_fdpic.c index a53f83830986..76e8c0defdc8 100644 --- a/fs/binfmt_elf_fdpic.c +++ b/fs/binfmt_elf_fdpic.c @@ -1534,21 +1534,9 @@ static bool elf_fdpic_dump_segments(struct coredump_params *cprm) if (!maydump(vma, cprm->mm_flags)) continue; - for (addr = vma->vm_start; addr < vma->vm_end; - addr += PAGE_SIZE) { - bool res; - struct page *page = get_dump_page(addr); - if (page) { - void *kaddr = kmap(page); - res = dump_emit(cprm, kaddr, PAGE_SIZE); - kunmap(page); - put_page(page); - } else { - res = dump_skip(cprm, PAGE_SIZE); - } - if (!res) - return false; - } + if (!dump_user_range(cprm, vma->vm_start, + vma->vma_end - vma->vm_start)) + return false; } return true; } diff --git a/fs/coredump.c b/fs/coredump.c index 5e24c06092c9..6042d15acd51 100644 --- a/fs/coredump.c +++ b/fs/coredump.c @@ -876,6 +876,40 @@ int dump_skip(struct coredump_params *cprm, size_t nr) } EXPORT_SYMBOL(dump_skip); +#ifdef CONFIG_ELF_CORE +int dump_user_range(struct coredump_params *cprm, unsigned long start, + unsigned long len) +{ + unsigned long addr; + + for (addr = start; addr < start + len; addr += PAGE_SIZE) { + struct page *page; + int stop; + + /* + * To avoid having to allocate page tables for virtual address + * ranges that have never been used yet, and also to make it + * easy to generate sparse core files, use a helper that returns + * NULL when encountering an empty page table entry that would + * otherwise have been filled with the zero page. + */ + page = get_dump_page(addr); + if (page) { + void *kaddr = kmap(page); + + stop = !dump_emit(cprm, kaddr, PAGE_SIZE); + kunmap(page); + put_page(page); + } else { + stop = !dump_skip(cprm, PAGE_SIZE); + } + if (stop) + return 0; + } + return 1; +} +#endif + int dump_align(struct coredump_params *cprm, int align) { unsigned mod = cprm->pos & (align - 1); diff --git a/include/linux/coredump.h b/include/linux/coredump.h index 7a899e83835d..f0b71a74d0bc 100644 --- a/include/linux/coredump.h +++ b/include/linux/coredump.h @@ -16,6 +16,8 @@ extern int dump_skip(struct coredump_params *cprm, size_t nr); extern int dump_emit(struct coredump_params *cprm, const void *addr, int nr); extern int dump_align(struct coredump_params *cprm, int align); extern void dump_truncate(struct coredump_params *cprm); +int dump_user_range(struct coredump_params *cprm, unsigned long start, + unsigned long len); #ifdef CONFIG_COREDUMP extern void do_coredump(const kernel_siginfo_t *siginfo); #else