From patchwork Sat Dec 11 03:33:08 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tiezhu Yang X-Patchwork-Id: 12671627 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id A7A42C433F5 for ; Sat, 11 Dec 2021 03:34:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:List-Subscribe:List-Help: List-Post:List-Archive:List-Unsubscribe:List-Id:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=+14j5Ze2OvXMX9MCoHkIxQvoWEREtwVVxB10MF1IDbQ=; b=F9aMjAN/PYicJ+ FN56y4K4kib0ai/MRIiW/SqjEhvzqpZ9DEV3sc7aiigmdPkwbnSXpzc7um+2I2niUDpJz4xbwFd0a XjjNQzKy9PkuZEVZTeexDmuQWMIcbfYqRmdgK2RdpJNTnRWqP1ODJDlPhlZdCDT2OmPhx+x9nV87p 8wttU7LzZThpVhDWGkwVSfli3IUoSyS1kP+02C1K+WeovCrB8hTtbSl3abhZk3BwxUiBr1FSr1DCg 3uel59cZiiyFeasIvaa6CepwAgroSoSPbYISHVoW432DvClb9DkTmeTkke0kU65qxtJD0/n0ro1vj 0kP5g2KspivcYLWTtyEQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1mvt96-004Uc4-5T; Sat, 11 Dec 2021 03:34:00 +0000 Received: from mail.loongson.cn ([114.242.206.163] helo=loongson.cn) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1mvt8N-004UMe-4W; Sat, 11 Dec 2021 03:33:18 +0000 Received: from linux.localdomain (unknown [113.200.148.30]) by mail.loongson.cn (Coremail) with SMTP id AQAAf9Ax+sj1G7RhlA0GAA--.13327S4; Sat, 11 Dec 2021 11:33:10 +0800 (CST) From: Tiezhu Yang To: Dave Young , Baoquan He , Vivek Goyal , Andrew Morton Cc: linux-arm-kernel@lists.infradead.org, linux-ia64@vger.kernel.org, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, linux-riscv@lists.infradead.org, linux-sh@vger.kernel.org, x86@kernel.org, linux-fsdevel@vger.kernel.org, kexec@lists.infradead.org, linux-kernel@vger.kernel.org, Xuefeng Li Subject: [PATCH v2 2/2] kdump: crashdump: use copy_to_user_or_kernel() to simplify code Date: Sat, 11 Dec 2021 11:33:08 +0800 Message-Id: <1639193588-7027-3-git-send-email-yangtiezhu@loongson.cn> X-Mailer: git-send-email 2.1.0 In-Reply-To: <1639193588-7027-1-git-send-email-yangtiezhu@loongson.cn> References: <1639193588-7027-1-git-send-email-yangtiezhu@loongson.cn> X-CM-TRANSID: AQAAf9Ax+sj1G7RhlA0GAA--.13327S4 X-Coremail-Antispam: 1UD129KBjvJXoW3uryUCF4DKFWkJr43tF1kKrg_yoWDKw1kpr 1kK39IyF4Sgas8GwnrtwnrWa48Ww1kG3W7t39Ika4rZ3Z2qFnFv3yDAasFg3yUtr90kFyS yas5Krn0y3yUZw7anT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUPC14x267AKxVWrJVCq3wAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2048vs2IY020E87I2jVAFwI0_Jryl82xGYIkIc2 x26xkF7I0E14v26ryj6s0DM28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48ve4kI8wA2z4x0 Y4vE2Ix0cI8IcVAFwI0_Jr0_JF4l84ACjcxK6xIIjxv20xvEc7CjxVAFwI0_Gr0_Cr1l84 ACjcxK6I8E87Iv67AKxVWxJVW8Jr1l84ACjcxK6I8E87Iv6xkF7I0E14v26r4UJVWxJr1l e2I262IYc4CY6c8Ij28IcVAaY2xG8wAqx4xG64xvF2IEw4CE5I8CrVC2j2WlYx0E2Ix0cI 8IcVAFwI0_Jr0_Jr4lYx0Ex4A2jsIE14v26r1j6r4UMcvjeVCFs4IE7xkEbVWUJVW8JwAC jcxG0xvY0x0EwIxGrwACjI8F5VA0II8E6IAqYI8I648v4I1lFIxGxcIEc7CjxVA2Y2ka0x kIwI1lc2xSY4AK67AK6r48MxAIw28IcxkI7VAKI48JMxC20s026xCaFVCjc4AY6r1j6r4U MI8I3I0E5I8CrVAFwI0_Jr0_Jr4lx2IqxVCjr7xvwVAFwI0_JrI_JrWlx4CE17CEb7AF67 AKxVWUtVW8ZwCIc40Y0x0EwIxGrwCI42IY6xIIjxv20xvE14v26r1j6r1xMIIF0xvE2Ix0 cI8IcVCY1x0267AKxVW8JVWxJwCI42IY6xAIw20EY4v20xvaj40_Jr0_JF4lIxAIcVC2z2 80aVAFwI0_Jr0_Gr1lIxAIcVC2z280aVCY1x0267AKxVW8JVW8JrUvcSsGvfC2KfnxnUUI 43ZEXa7VUjknY5UUUUU== X-CM-SenderInfo: p1dqw3xlh2x3gn0dqz5rrqw2lrqou0/ X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20211210_193315_624169_4E481091 X-CRM114-Status: GOOD ( 13.27 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org Use copy_to_user_or_kernel() to simplify the related code about copy_oldmem_page() in arch/*/kernel/crash_dump*.c files. Signed-off-by: Tiezhu Yang --- arch/arm/kernel/crash_dump.c | 12 +++--------- arch/arm64/kernel/crash_dump.c | 12 +++--------- arch/ia64/kernel/crash_dump.c | 12 +++++------- arch/mips/kernel/crash_dump.c | 11 +++-------- arch/powerpc/kernel/crash_dump.c | 11 ++++------- arch/riscv/kernel/crash_dump.c | 11 +++-------- arch/sh/kernel/crash_dump.c | 11 +++-------- arch/x86/kernel/crash_dump_32.c | 11 +++-------- arch/x86/kernel/crash_dump_64.c | 15 +++++---------- fs/proc/vmcore.c | 4 ++-- include/linux/crash_dump.h | 8 ++++---- 11 files changed, 38 insertions(+), 80 deletions(-) diff --git a/arch/arm/kernel/crash_dump.c b/arch/arm/kernel/crash_dump.c index 53cb924..a27c5df 100644 --- a/arch/arm/kernel/crash_dump.c +++ b/arch/arm/kernel/crash_dump.c @@ -29,7 +29,7 @@ */ ssize_t copy_oldmem_page(unsigned long pfn, char *buf, size_t csize, unsigned long offset, - int userbuf) + bool userbuf) { void *vaddr; @@ -40,14 +40,8 @@ ssize_t copy_oldmem_page(unsigned long pfn, char *buf, if (!vaddr) return -ENOMEM; - if (userbuf) { - if (copy_to_user(buf, vaddr + offset, csize)) { - iounmap(vaddr); - return -EFAULT; - } - } else { - memcpy(buf, vaddr + offset, csize); - } + if (copy_to_user_or_kernel(buf, vaddr + offset, csize, userbuf)) + csize = -EFAULT; iounmap(vaddr); return csize; diff --git a/arch/arm64/kernel/crash_dump.c b/arch/arm64/kernel/crash_dump.c index 58303a9..d22988f 100644 --- a/arch/arm64/kernel/crash_dump.c +++ b/arch/arm64/kernel/crash_dump.c @@ -27,7 +27,7 @@ */ ssize_t copy_oldmem_page(unsigned long pfn, char *buf, size_t csize, unsigned long offset, - int userbuf) + bool userbuf) { void *vaddr; @@ -38,14 +38,8 @@ ssize_t copy_oldmem_page(unsigned long pfn, char *buf, if (!vaddr) return -ENOMEM; - if (userbuf) { - if (copy_to_user((char __user *)buf, vaddr + offset, csize)) { - memunmap(vaddr); - return -EFAULT; - } - } else { - memcpy(buf, vaddr + offset, csize); - } + if (copy_to_user_or_kernel(buf, vaddr + offset, csize, userbuf)) + csize = -EFAULT; memunmap(vaddr); diff --git a/arch/ia64/kernel/crash_dump.c b/arch/ia64/kernel/crash_dump.c index 0ed3c3d..12128f8 100644 --- a/arch/ia64/kernel/crash_dump.c +++ b/arch/ia64/kernel/crash_dump.c @@ -33,19 +33,17 @@ */ ssize_t copy_oldmem_page(unsigned long pfn, char *buf, - size_t csize, unsigned long offset, int userbuf) + size_t csize, unsigned long offset, bool userbuf) { void *vaddr; if (!csize) return 0; + vaddr = __va(pfn< static ssize_t __copy_oldmem_page(unsigned long pfn, char *buf, size_t csize, - unsigned long offset, int userbuf, + unsigned long offset, bool userbuf, bool encrypted) { void *vaddr; @@ -29,13 +29,8 @@ static ssize_t __copy_oldmem_page(unsigned long pfn, char *buf, size_t csize, if (!vaddr) return -ENOMEM; - if (userbuf) { - if (copy_to_user((void __user *)buf, vaddr + offset, csize)) { - iounmap((void __iomem *)vaddr); - return -EFAULT; - } - } else - memcpy(buf, vaddr + offset, csize); + if (copy_to_user_or_kernel(buf, vaddr + offset, csize, userbuf)) + csize = -EFAULT; set_iounmap_nonlazy(); iounmap((void __iomem *)vaddr); @@ -56,7 +51,7 @@ static ssize_t __copy_oldmem_page(unsigned long pfn, char *buf, size_t csize, * mapped in the current kernel. We stitch up a pte, similar to kmap_atomic. */ ssize_t copy_oldmem_page(unsigned long pfn, char *buf, size_t csize, - unsigned long offset, int userbuf) + unsigned long offset, bool userbuf) { return __copy_oldmem_page(pfn, buf, csize, offset, userbuf, false); } @@ -67,7 +62,7 @@ ssize_t copy_oldmem_page(unsigned long pfn, char *buf, size_t csize, * machines. */ ssize_t copy_oldmem_page_encrypted(unsigned long pfn, char *buf, size_t csize, - unsigned long offset, int userbuf) + unsigned long offset, bool userbuf) { return __copy_oldmem_page(pfn, buf, csize, offset, userbuf, true); } diff --git a/fs/proc/vmcore.c b/fs/proc/vmcore.c index f67fd77..bba52aa 100644 --- a/fs/proc/vmcore.c +++ b/fs/proc/vmcore.c @@ -133,7 +133,7 @@ static int open_vmcore(struct inode *inode, struct file *file) /* Reads a page from the oldmem device from given offset. */ ssize_t read_from_oldmem(char *buf, size_t count, - u64 *ppos, int userbuf, + u64 *ppos, bool userbuf, bool encrypted) { unsigned long pfn, offset; @@ -233,7 +233,7 @@ int __weak remap_oldmem_pfn_range(struct vm_area_struct *vma, */ ssize_t __weak copy_oldmem_page_encrypted(unsigned long pfn, char *buf, size_t csize, - unsigned long offset, int userbuf) + unsigned long offset, bool userbuf) { return copy_oldmem_page(pfn, buf, csize, offset, userbuf); } diff --git a/include/linux/crash_dump.h b/include/linux/crash_dump.h index 6208215..033448b 100644 --- a/include/linux/crash_dump.h +++ b/include/linux/crash_dump.h @@ -25,10 +25,10 @@ extern int remap_oldmem_pfn_range(struct vm_area_struct *vma, unsigned long size, pgprot_t prot); extern ssize_t copy_oldmem_page(unsigned long, char *, size_t, - unsigned long, int); + unsigned long, bool); extern ssize_t copy_oldmem_page_encrypted(unsigned long pfn, char *buf, size_t csize, unsigned long offset, - int userbuf); + bool userbuf); void vmcore_cleanup(void); @@ -136,11 +136,11 @@ static inline int vmcore_add_device_dump(struct vmcoredd_data *data) #ifdef CONFIG_PROC_VMCORE ssize_t read_from_oldmem(char *buf, size_t count, - u64 *ppos, int userbuf, + u64 *ppos, bool userbuf, bool encrypted); #else static inline ssize_t read_from_oldmem(char *buf, size_t count, - u64 *ppos, int userbuf, + u64 *ppos, bool userbuf, bool encrypted) { return -EOPNOTSUPP;