From patchwork Sat Dec 11 03:33:07 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tiezhu Yang X-Patchwork-Id: 12671619 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D992EC4321E for ; Sat, 11 Dec 2021 03:33:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345759AbhLKDhD (ORCPT ); Fri, 10 Dec 2021 22:37:03 -0500 Received: from mail.loongson.cn ([114.242.206.163]:37174 "EHLO loongson.cn" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S239512AbhLKDg4 (ORCPT ); Fri, 10 Dec 2021 22:36:56 -0500 Received: from linux.localdomain (unknown [113.200.148.30]) by mail.loongson.cn (Coremail) with SMTP id AQAAf9Ax+sj1G7RhlA0GAA--.13327S3; 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 1/2] kdump: vmcore: remove copy_to() and add copy_to_user_or_kernel() Date: Sat, 11 Dec 2021 11:33:07 +0800 Message-Id: <1639193588-7027-2-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+sj1G7RhlA0G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FwI0_Jr0_Gr1lIxAIcVC2z280aVCY1x0267AKxVW8JVW8JrUvcSsGvfC2KfnxnUUI 43ZEXa7VUU0PfPUUUUU== X-CM-SenderInfo: p1dqw3xlh2x3gn0dqz5rrqw2lrqou0/ Precedence: bulk List-ID: X-Mailing-List: linux-mips@vger.kernel.org In arch/*/kernel/crash_dump*.c, there exist many similar code about copy_oldmem_page(), remove copy_to() in fs/proc/vmcore.c and add copy_to_user_or_kernel() in lib/usercopy.c, then we can use copy_to_user_or_kernel() to simplify the related code. Signed-off-by: Tiezhu Yang --- fs/proc/vmcore.c | 28 +++++++--------------------- include/linux/uaccess.h | 1 + lib/usercopy.c | 15 +++++++++++++++ 3 files changed, 23 insertions(+), 21 deletions(-) diff --git a/fs/proc/vmcore.c b/fs/proc/vmcore.c index 509f851..f67fd77 100644 --- a/fs/proc/vmcore.c +++ b/fs/proc/vmcore.c @@ -238,22 +238,8 @@ copy_oldmem_page_encrypted(unsigned long pfn, char *buf, size_t csize, return copy_oldmem_page(pfn, buf, csize, offset, userbuf); } -/* - * Copy to either kernel or user space - */ -static int copy_to(void *target, void *src, size_t size, int userbuf) -{ - if (userbuf) { - if (copy_to_user((char __user *) target, src, size)) - return -EFAULT; - } else { - memcpy(target, src, size); - } - return 0; -} - #ifdef CONFIG_PROC_VMCORE_DEVICE_DUMP -static int vmcoredd_copy_dumps(void *dst, u64 start, size_t size, int userbuf) +static int vmcoredd_copy_dumps(void *dst, u64 start, size_t size, bool userbuf) { struct vmcoredd_node *dump; u64 offset = 0; @@ -266,7 +252,7 @@ static int vmcoredd_copy_dumps(void *dst, u64 start, size_t size, int userbuf) if (start < offset + dump->size) { tsz = min(offset + (u64)dump->size - start, (u64)size); buf = dump->buf + start - offset; - if (copy_to(dst, buf, tsz, userbuf)) { + if (copy_to_user_or_kernel(dst, buf, tsz, userbuf)) { ret = -EFAULT; goto out_unlock; } @@ -330,7 +316,7 @@ static int vmcoredd_mmap_dumps(struct vm_area_struct *vma, unsigned long dst, * returned otherwise number of bytes read are returned. */ static ssize_t __read_vmcore(char *buffer, size_t buflen, loff_t *fpos, - int userbuf) + bool userbuf) { ssize_t acc = 0, tmp; size_t tsz; @@ -347,7 +333,7 @@ static ssize_t __read_vmcore(char *buffer, size_t buflen, loff_t *fpos, /* Read ELF core header */ if (*fpos < elfcorebuf_sz) { tsz = min(elfcorebuf_sz - (size_t)*fpos, buflen); - if (copy_to(buffer, elfcorebuf + *fpos, tsz, userbuf)) + if (copy_to_user_or_kernel(buffer, elfcorebuf + *fpos, tsz, userbuf)) return -EFAULT; buflen -= tsz; *fpos += tsz; @@ -395,7 +381,7 @@ static ssize_t __read_vmcore(char *buffer, size_t buflen, loff_t *fpos, /* Read remaining elf notes */ tsz = min(elfcorebuf_sz + elfnotes_sz - (size_t)*fpos, buflen); kaddr = elfnotes_buf + *fpos - elfcorebuf_sz - vmcoredd_orig_sz; - if (copy_to(buffer, kaddr, tsz, userbuf)) + if (copy_to_user_or_kernel(buffer, kaddr, tsz, userbuf)) return -EFAULT; buflen -= tsz; @@ -435,7 +421,7 @@ static ssize_t __read_vmcore(char *buffer, size_t buflen, loff_t *fpos, static ssize_t read_vmcore(struct file *file, char __user *buffer, size_t buflen, loff_t *fpos) { - return __read_vmcore((__force char *) buffer, buflen, fpos, 1); + return __read_vmcore((__force char *) buffer, buflen, fpos, true); } /* @@ -461,7 +447,7 @@ static vm_fault_t mmap_vmcore_fault(struct vm_fault *vmf) if (!PageUptodate(page)) { offset = (loff_t) index << PAGE_SHIFT; buf = __va((page_to_pfn(page) << PAGE_SHIFT)); - rc = __read_vmcore(buf, PAGE_SIZE, &offset, 0); + rc = __read_vmcore(buf, PAGE_SIZE, &offset, false); if (rc < 0) { unlock_page(page); put_page(page); diff --git a/include/linux/uaccess.h b/include/linux/uaccess.h index ac03940..a25e682e 100644 --- a/include/linux/uaccess.h +++ b/include/linux/uaccess.h @@ -283,6 +283,7 @@ __copy_from_user_inatomic_nocache(void *to, const void __user *from, #endif /* ARCH_HAS_NOCACHE_UACCESS */ extern __must_check int check_zeroed_user(const void __user *from, size_t size); +extern __must_check int copy_to_user_or_kernel(void *target, void *src, size_t size, bool userbuf); /** * copy_struct_from_user: copy a struct from userspace diff --git a/lib/usercopy.c b/lib/usercopy.c index 7413dd3..7431b1b 100644 --- a/lib/usercopy.c +++ b/lib/usercopy.c @@ -90,3 +90,18 @@ int check_zeroed_user(const void __user *from, size_t size) return -EFAULT; } EXPORT_SYMBOL(check_zeroed_user); + +/* + * Copy to either user or kernel space + */ +int copy_to_user_or_kernel(void *target, void *src, size_t size, bool userbuf) +{ + if (userbuf) { + if (copy_to_user((char __user *) target, src, size)) + return -EFAULT; + } else { + memcpy(target, src, size); + } + return 0; +} +EXPORT_SYMBOL(copy_to_user_or_kernel); 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: 12671617 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9EC77C4332F for ; Sat, 11 Dec 2021 03:33:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345784AbhLKDhE (ORCPT ); Fri, 10 Dec 2021 22:37:04 -0500 Received: from mail.loongson.cn ([114.242.206.163]:37182 "EHLO loongson.cn" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S244514AbhLKDg6 (ORCPT ); Fri, 10 Dec 2021 22:36:58 -0500 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/ Precedence: bulk List-ID: X-Mailing-List: linux-mips@vger.kernel.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;