Commit 2711032c authored by David Hildenbrand's avatar David Hildenbrand Committed by Linus Torvalds
Browse files

fs/proc/kcore: pfn_is_ram check only applies to KCORE_RAM

Let's resturcture the code, using switch-case, and checking pfn_is_ram()
only when we are dealing with KCORE_RAM.

Link: https://lkml.kernel.org/r/20210526093041.8800-3-david@redhat.com


Signed-off-by: default avatarDavid Hildenbrand <david@redhat.com>
Reviewed-by: default avatarMike Rapoport <rppt@linux.ibm.com>
Cc: Aili Yao <yaoaili@kingsoft.com>
Cc: Alexey Dobriyan <adobriyan@gmail.com>
Cc: Alex Shi <alex.shi@linux.alibaba.com>
Cc: Haiyang Zhang <haiyangz@microsoft.com>
Cc: Jason Wang <jasowang@redhat.com>
Cc: Jiri Bohac <jbohac@suse.cz>
Cc: "K. Y. Srinivasan" <kys@microsoft.com>
Cc: "Matthew Wilcox (Oracle)" <willy@infradead.org>
Cc: "Michael S. Tsirkin" <mst@redhat.com>
Cc: Michal Hocko <mhocko@suse.com>
Cc: Mike Kravetz <mike.kravetz@oracle.com>
Cc: Naoya Horiguchi <naoya.horiguchi@nec.com>
Cc: Oscar Salvador <osalvador@suse.de>
Cc: Roman Gushchin <guro@fb.com>
Cc: Stephen Hemminger <sthemmin@microsoft.com>
Cc: Steven Price <steven.price@arm.com>
Cc: Wei Liu <wei.liu@kernel.org>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent 3c36b419
Loading
Loading
Loading
Loading
+27 −8
Original line number Diff line number Diff line
@@ -483,25 +483,36 @@ read_kcore(struct file *file, char __user *buffer, size_t buflen, loff_t *fpos)
				goto out;
			}
			m = NULL;	/* skip the list anchor */
		} else if (!pfn_is_ram(__pa(start) >> PAGE_SHIFT)) {
			if (clear_user(buffer, tsz)) {
				ret = -EFAULT;
				goto out;
			goto skip;
		}
		} else if (m->type == KCORE_VMALLOC) {

		switch (m->type) {
		case KCORE_VMALLOC:
			vread(buf, (char *)start, tsz);
			/* we have to zero-fill user buffer even if no read */
			if (copy_to_user(buffer, buf, tsz)) {
				ret = -EFAULT;
				goto out;
			}
		} else if (m->type == KCORE_USER) {
			break;
		case KCORE_USER:
			/* User page is handled prior to normal kernel page: */
			if (copy_to_user(buffer, (char *)start, tsz)) {
				ret = -EFAULT;
				goto out;
			}
		} else {
			break;
		case KCORE_RAM:
			if (!pfn_is_ram(__pa(start) >> PAGE_SHIFT)) {
				if (clear_user(buffer, tsz)) {
					ret = -EFAULT;
					goto out;
				}
				break;
			}
			fallthrough;
		case KCORE_VMEMMAP:
		case KCORE_TEXT:
			if (kern_addr_valid(start)) {
				/*
				 * Using bounce buffer to bypass the
@@ -525,7 +536,15 @@ read_kcore(struct file *file, char __user *buffer, size_t buflen, loff_t *fpos)
					goto out;
				}
			}
			break;
		default:
			pr_warn_once("Unhandled KCORE type: %d\n", m->type);
			if (clear_user(buffer, tsz)) {
				ret = -EFAULT;
				goto out;
			}
		}
skip:
		buflen -= tsz;
		*fpos += tsz;
		buffer += tsz;