stable.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Sasha Levin <sashal@kernel.org>
To: linux-kernel@vger.kernel.org, stable@vger.kernel.org
Cc: Kairui Song <kasong@redhat.com>,
	Thomas Gleixner <tglx@linutronix.de>,
	Borislav Petkov <bp@alien8.de>, "H. Peter Anvin" <hpa@zytor.com>,
	Alexey Dobriyan <adobriyan@gmail.com>,
	Andrew Morton <akpm@linux-foundation.org>,
	Omar Sandoval <osandov@fb.com>, Dave Young <dyoung@redhat.com>,
	Sasha Levin <sashal@kernel.org>,
	linux-fsdevel@vger.kernel.org
Subject: [PATCH AUTOSEL 5.0 66/67] x86/gart: Exclude GART aperture from kcore
Date: Fri, 29 Mar 2019 20:50:46 -0400	[thread overview]
Message-ID: <20190330005047.25998-66-sashal@kernel.org> (raw)
In-Reply-To: <20190330005047.25998-1-sashal@kernel.org>

From: Kairui Song <kasong@redhat.com>

[ Upstream commit ffc8599aa9763f39f6736a79da4d1575e7006f9a ]

On machines where the GART aperture is mapped over physical RAM,
/proc/kcore contains the GART aperture range. Accessing the GART range via
/proc/kcore results in a kernel crash.

vmcore used to have the same issue, until it was fixed with commit
2a3e83c6f96c ("x86/gart: Exclude GART aperture from vmcore")', leveraging
existing hook infrastructure in vmcore to let /proc/vmcore return zeroes
when attempting to read the aperture region, and so it won't read from the
actual memory.

Apply the same workaround for kcore. First implement the same hook
infrastructure for kcore, then reuse the hook functions introduced in the
previous vmcore fix. Just with some minor adjustment, rename some functions
for more general usage, and simplify the hook infrastructure a bit as there
is no module usage yet.

Suggested-by: Baoquan He <bhe@redhat.com>
Signed-off-by: Kairui Song <kasong@redhat.com>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Reviewed-by: Jiri Bohac <jbohac@suse.cz>
Acked-by: Baoquan He <bhe@redhat.com>
Cc: Borislav Petkov <bp@alien8.de>
Cc: "H. Peter Anvin" <hpa@zytor.com>
Cc: Alexey Dobriyan <adobriyan@gmail.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Omar Sandoval <osandov@fb.com>
Cc: Dave Young <dyoung@redhat.com>
Link: https://lkml.kernel.org/r/20190308030508.13548-1-kasong@redhat.com
Signed-off-by: Sasha Levin <sashal@kernel.org>
---
 arch/x86/kernel/aperture_64.c | 20 +++++++++++++-------
 fs/proc/kcore.c               | 27 +++++++++++++++++++++++++++
 include/linux/kcore.h         |  2 ++
 3 files changed, 42 insertions(+), 7 deletions(-)

diff --git a/arch/x86/kernel/aperture_64.c b/arch/x86/kernel/aperture_64.c
index 58176b56354e..294ed4392a0e 100644
--- a/arch/x86/kernel/aperture_64.c
+++ b/arch/x86/kernel/aperture_64.c
@@ -14,6 +14,7 @@
 #define pr_fmt(fmt) "AGP: " fmt
 
 #include <linux/kernel.h>
+#include <linux/kcore.h>
 #include <linux/types.h>
 #include <linux/init.h>
 #include <linux/memblock.h>
@@ -57,7 +58,7 @@ int fallback_aper_force __initdata;
 
 int fix_aperture __initdata = 1;
 
-#ifdef CONFIG_PROC_VMCORE
+#if defined(CONFIG_PROC_VMCORE) || defined(CONFIG_PROC_KCORE)
 /*
  * If the first kernel maps the aperture over e820 RAM, the kdump kernel will
  * use the same range because it will remain configured in the northbridge.
@@ -66,20 +67,25 @@ int fix_aperture __initdata = 1;
  */
 static unsigned long aperture_pfn_start, aperture_page_count;
 
-static int gart_oldmem_pfn_is_ram(unsigned long pfn)
+static int gart_mem_pfn_is_ram(unsigned long pfn)
 {
 	return likely((pfn < aperture_pfn_start) ||
 		      (pfn >= aperture_pfn_start + aperture_page_count));
 }
 
-static void exclude_from_vmcore(u64 aper_base, u32 aper_order)
+static void __init exclude_from_core(u64 aper_base, u32 aper_order)
 {
 	aperture_pfn_start = aper_base >> PAGE_SHIFT;
 	aperture_page_count = (32 * 1024 * 1024) << aper_order >> PAGE_SHIFT;
-	WARN_ON(register_oldmem_pfn_is_ram(&gart_oldmem_pfn_is_ram));
+#ifdef CONFIG_PROC_VMCORE
+	WARN_ON(register_oldmem_pfn_is_ram(&gart_mem_pfn_is_ram));
+#endif
+#ifdef CONFIG_PROC_KCORE
+	WARN_ON(register_mem_pfn_is_ram(&gart_mem_pfn_is_ram));
+#endif
 }
 #else
-static void exclude_from_vmcore(u64 aper_base, u32 aper_order)
+static void exclude_from_core(u64 aper_base, u32 aper_order)
 {
 }
 #endif
@@ -474,7 +480,7 @@ out:
 			 * may have allocated the range over its e820 RAM
 			 * and fixed up the northbridge
 			 */
-			exclude_from_vmcore(last_aper_base, last_aper_order);
+			exclude_from_core(last_aper_base, last_aper_order);
 
 			return 1;
 		}
@@ -520,7 +526,7 @@ out:
 	 * overlap with the first kernel's memory. We can't access the
 	 * range through vmcore even though it should be part of the dump.
 	 */
-	exclude_from_vmcore(aper_alloc, aper_order);
+	exclude_from_core(aper_alloc, aper_order);
 
 	/* Fix up the north bridges */
 	for (i = 0; i < amd_nb_bus_dev_ranges[i].dev_limit; i++) {
diff --git a/fs/proc/kcore.c b/fs/proc/kcore.c
index bbcc185062bb..d29d869abec1 100644
--- a/fs/proc/kcore.c
+++ b/fs/proc/kcore.c
@@ -54,6 +54,28 @@ static LIST_HEAD(kclist_head);
 static DECLARE_RWSEM(kclist_lock);
 static int kcore_need_update = 1;
 
+/*
+ * Returns > 0 for RAM pages, 0 for non-RAM pages, < 0 on error
+ * Same as oldmem_pfn_is_ram in vmcore
+ */
+static int (*mem_pfn_is_ram)(unsigned long pfn);
+
+int __init register_mem_pfn_is_ram(int (*fn)(unsigned long pfn))
+{
+	if (mem_pfn_is_ram)
+		return -EBUSY;
+	mem_pfn_is_ram = fn;
+	return 0;
+}
+
+static int pfn_is_ram(unsigned long pfn)
+{
+	if (mem_pfn_is_ram)
+		return mem_pfn_is_ram(pfn);
+	else
+		return 1;
+}
+
 /* This doesn't grab kclist_lock, so it should only be used at init time. */
 void __init kclist_add(struct kcore_list *new, void *addr, size_t size,
 		       int type)
@@ -465,6 +487,11 @@ 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;
+			}
 		} else if (m->type == KCORE_VMALLOC) {
 			vread(buf, (char *)start, tsz);
 			/* we have to zero-fill user buffer even if no read */
diff --git a/include/linux/kcore.h b/include/linux/kcore.h
index 8c3f8c14eeaa..c843f4a9c512 100644
--- a/include/linux/kcore.h
+++ b/include/linux/kcore.h
@@ -44,6 +44,8 @@ void kclist_add_remap(struct kcore_list *m, void *addr, void *vaddr, size_t sz)
 	m->vaddr = (unsigned long)vaddr;
 	kclist_add(m, addr, sz, KCORE_REMAP);
 }
+
+extern int __init register_mem_pfn_is_ram(int (*fn)(unsigned long pfn));
 #else
 static inline
 void kclist_add(struct kcore_list *new, void *addr, size_t size, int type)
-- 
2.19.1


  parent reply	other threads:[~2019-03-30  1:08 UTC|newest]

Thread overview: 67+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-03-30  0:49 [PATCH AUTOSEL 5.0 01/67] ARC: u-boot args: check that magic number is correct Sasha Levin
2019-03-30  0:49 ` [PATCH AUTOSEL 5.0 02/67] arc: hsdk_defconfig: Enable CONFIG_BLK_DEV_RAM Sasha Levin
2019-03-30  0:49 ` [PATCH AUTOSEL 5.0 03/67] inotify: Fix fsnotify_mark refcount leak in inotify_update_existing_watch() Sasha Levin
2019-03-30  0:49 ` [PATCH AUTOSEL 5.0 04/67] perf/core: Restore mmap record type correctly Sasha Levin
2019-03-30  0:49 ` [PATCH AUTOSEL 5.0 05/67] perf data: Don't store auxtrace index for directory data file Sasha Levin
2019-03-30  0:49 ` [PATCH AUTOSEL 5.0 06/67] mips: bcm47xx: Enable USB power on Netgear WNDR3400v2 Sasha Levin
2019-03-30  0:49 ` [PATCH AUTOSEL 5.0 07/67] ext4: avoid panic during forced reboot Sasha Levin
2019-03-30  0:49 ` [PATCH AUTOSEL 5.0 08/67] ext4: add missing brelse() in add_new_gdb_meta_bg() Sasha Levin
2019-03-30  0:49 ` [PATCH AUTOSEL 5.0 09/67] ext4: report real fs size after failed resize Sasha Levin
2019-03-30  0:49 ` [PATCH AUTOSEL 5.0 10/67] ALSA: echoaudio: add a check for ioremap_nocache Sasha Levin
2019-03-30  0:49 ` [PATCH AUTOSEL 5.0 11/67] ALSA: sb8: add a check for request_region Sasha Levin
2019-03-30  0:49 ` [PATCH AUTOSEL 5.0 12/67] auxdisplay: hd44780: Fix memory leak on ->remove() Sasha Levin
2019-03-30  0:49 ` [PATCH AUTOSEL 5.0 13/67] drm/udl: use drm_gem_object_put_unlocked Sasha Levin
2019-03-30  0:49 ` [PATCH AUTOSEL 5.0 14/67] IB/mlx4: Fix race condition between catas error reset and aliasguid flows Sasha Levin
2019-03-30  0:49 ` [PATCH AUTOSEL 5.0 15/67] i40iw: Avoid panic when handling the inetdev event Sasha Levin
2019-03-30  0:49 ` [PATCH AUTOSEL 5.0 16/67] mmc: davinci: remove extraneous __init annotation Sasha Levin
2019-03-30  0:49 ` [PATCH AUTOSEL 5.0 17/67] ALSA: opl3: fix mismatch between snd_opl3_drum_switch definition and declaration Sasha Levin
2019-03-30  0:49 ` [PATCH AUTOSEL 5.0 18/67] paride/pf: cleanup queues when detection fails Sasha Levin
2019-03-30  0:49 ` [PATCH AUTOSEL 5.0 19/67] paride/pcd: " Sasha Levin
2019-03-30  0:50 ` [PATCH AUTOSEL 5.0 20/67] thermal/intel_powerclamp: fix __percpu declaration of worker_data Sasha Levin
2019-03-30  0:50 ` [PATCH AUTOSEL 5.0 21/67] thermal: samsung: Fix incorrect check after code merge Sasha Levin
2019-03-30  0:50 ` [PATCH AUTOSEL 5.0 22/67] thermal: bcm2835: Fix crash in bcm2835_thermal_debugfs Sasha Levin
2019-03-30  0:50 ` [PATCH AUTOSEL 5.0 23/67] thermal/int340x_thermal: Add additional UUIDs Sasha Levin
2019-03-30  0:50 ` [PATCH AUTOSEL 5.0 24/67] thermal/int340x_thermal: fix mode setting Sasha Levin
2019-03-30  0:50 ` [PATCH AUTOSEL 5.0 25/67] thermal/intel_powerclamp: fix truncated kthread name Sasha Levin
2019-03-30  0:50 ` [PATCH AUTOSEL 5.0 26/67] scsi: iscsi: flush running unbind operations when removing a session Sasha Levin
2019-03-30  0:50 ` [PATCH AUTOSEL 5.0 27/67] sched/cpufreq: Fix 32-bit math overflow Sasha Levin
2019-03-30  0:50 ` [PATCH AUTOSEL 5.0 28/67] sched/core: Fix buffer overflow in cgroup2 property cpu.max Sasha Levin
2019-03-30  0:50 ` [PATCH AUTOSEL 5.0 29/67] x86/mm: Don't leak kernel addresses Sasha Levin
2019-03-30  0:50 ` [PATCH AUTOSEL 5.0 30/67] tools/power turbostat: return the exit status of a command Sasha Levin
2019-03-30  0:50 ` [PATCH AUTOSEL 5.0 31/67] scsi: core: Also call destroy_rcu_head() for passthrough requests Sasha Levin
2019-03-30  0:50 ` [PATCH AUTOSEL 5.0 32/67] scsi: qla2xxx: Fix NULL pointer crash due to stale CPUID Sasha Levin
2019-03-30  0:50 ` [PATCH AUTOSEL 5.0 33/67] perf stat: Fix --no-scale Sasha Levin
2019-03-30  0:50 ` [PATCH AUTOSEL 5.0 34/67] perf list: Don't forget to drop the reference to the allocated thread_map Sasha Levin
2019-03-30  0:50 ` [PATCH AUTOSEL 5.0 35/67] perf tools: Fix errors under optimization level '-Og' Sasha Levin
2019-03-30  0:50 ` [PATCH AUTOSEL 5.0 36/67] perf config: Fix an error in the config template documentation Sasha Levin
2019-03-30  0:50 ` [PATCH AUTOSEL 5.0 37/67] perf config: Fix a memory leak in collect_config() Sasha Levin
2019-03-30  0:50 ` [PATCH AUTOSEL 5.0 38/67] perf build-id: Fix memory leak in print_sdt_events() Sasha Levin
2019-03-30  0:50 ` [PATCH AUTOSEL 5.0 39/67] perf top: Delete the evlist before perf_session, fixing heap-use-after-free issue Sasha Levin
2019-03-30  0:50 ` [PATCH AUTOSEL 5.0 40/67] perf top: Fix error handling in cmd_top() Sasha Levin
2019-03-30  0:50 ` [PATCH AUTOSEL 5.0 41/67] perf hist: Add missing map__put() in error case Sasha Levin
2019-03-30  0:50 ` [PATCH AUTOSEL 5.0 42/67] perf map: Remove map from 'names' tree in __maps__remove() Sasha Levin
2019-03-30  0:50 ` [PATCH AUTOSEL 5.0 43/67] perf maps: Purge all maps from the 'names' tree Sasha Levin
2019-03-30  0:50 ` [PATCH AUTOSEL 5.0 44/67] perf top: Fix global-buffer-overflow issue Sasha Levin
2019-03-30  0:50 ` [PATCH AUTOSEL 5.0 45/67] perf evsel: Free evsel->counts in perf_evsel__exit() Sasha Levin
2019-03-30  0:50 ` [PATCH AUTOSEL 5.0 46/67] perf tests: Fix a memory leak of cpu_map object in the openat_syscall_event_on_all_cpus test Sasha Levin
2019-03-30  0:50 ` [PATCH AUTOSEL 5.0 47/67] perf tests: Fix memory leak by expr__find_other() in test__expr() Sasha Levin
2019-03-30  0:50 ` [PATCH AUTOSEL 5.0 48/67] perf tests: Fix a memory leak in test__perf_evsel__tp_sched_test() Sasha Levin
2019-03-30  0:50 ` [PATCH AUTOSEL 5.0 49/67] ACPI / utils: Drop reference in test for device presence Sasha Levin
2019-03-30  0:50 ` [PATCH AUTOSEL 5.0 50/67] PM / Domains: Avoid a potential deadlock Sasha Levin
2019-03-30  0:50 ` [PATCH AUTOSEL 5.0 51/67] blk-iolatency: #include "blk.h" Sasha Levin
2019-03-30  0:50 ` [PATCH AUTOSEL 5.0 52/67] drm/exynos/mixer: fix MIXER shadow registry synchronisation code Sasha Levin
2019-03-30  0:50 ` [PATCH AUTOSEL 5.0 53/67] irqchip/stm32: Don't clear rising/falling config registers at init Sasha Levin
2019-03-30  0:50 ` [PATCH AUTOSEL 5.0 54/67] irqchip/stm32: Don't set rising configuration " Sasha Levin
2019-03-30  0:50 ` [PATCH AUTOSEL 5.0 55/67] irqchip/mbigen: Don't clear eventid when freeing an MSI Sasha Levin
2019-03-30  0:50 ` [PATCH AUTOSEL 5.0 56/67] x86/hpet: Prevent potential NULL pointer dereference Sasha Levin
2019-03-30  0:50 ` [PATCH AUTOSEL 5.0 57/67] x86/hyperv: " Sasha Levin
2019-03-30  0:50 ` [PATCH AUTOSEL 5.0 58/67] x86/cpu/cyrix: Use correct macros for Cyrix calls on Geode processors Sasha Levin
2019-03-30  0:50 ` [PATCH AUTOSEL 5.0 59/67] drm/nouveau/debugfs: Fix check of pm_runtime_get_sync failure Sasha Levin
2019-03-30  0:50 ` [PATCH AUTOSEL 5.0 60/67] iommu/vt-d: Check capability before disabling protected memory Sasha Levin
2019-03-30  0:50 ` [PATCH AUTOSEL 5.0 61/67] iommu/vt-d: Save the right domain ID used by hardware Sasha Levin
2019-03-30  0:50 ` [PATCH AUTOSEL 5.0 62/67] x86/hw_breakpoints: Make default case in hw_breakpoint_arch_parse() return an error Sasha Levin
2019-03-30  0:50 ` [PATCH AUTOSEL 5.0 63/67] cifs: fix that return -EINVAL when do dedupe operation Sasha Levin
2019-03-30  0:50 ` [PATCH AUTOSEL 5.0 64/67] fix incorrect error code mapping for OBJECTID_NOT_FOUND Sasha Levin
2019-03-30  0:50 ` [PATCH AUTOSEL 5.0 65/67] cifs: Fix slab-out-of-bounds when tracing SMB tcon Sasha Levin
2019-03-30  0:50 ` Sasha Levin [this message]
2019-03-30  0:50 ` [PATCH AUTOSEL 5.0 67/67] ext4: prohibit fstrim in norecovery mode Sasha Levin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190330005047.25998-66-sashal@kernel.org \
    --to=sashal@kernel.org \
    --cc=adobriyan@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=bp@alien8.de \
    --cc=dyoung@redhat.com \
    --cc=hpa@zytor.com \
    --cc=kasong@redhat.com \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=osandov@fb.com \
    --cc=stable@vger.kernel.org \
    --cc=tglx@linutronix.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).