Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp656433imu; Fri, 21 Dec 2018 05:24:45 -0800 (PST) X-Google-Smtp-Source: ALg8bN6uxQCJt0duyZTyy6JmRbXb/zE9QizU8vAID5LKtsZP/o+2bF0yXH7kGukXPrbSCUxRkxIc X-Received: by 2002:a17:902:aa4c:: with SMTP id c12mr2542502plr.48.1545398685145; Fri, 21 Dec 2018 05:24:45 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1545398685; cv=none; d=google.com; s=arc-20160816; b=UhsBK5wUzZF9XLWNfP+MFQIJULmLUKCjypS8jKxc1jKbmAKqhqprUoMvZIU2Ghsk3U h2KakAN2hkBzQ+FHIzwHx+BfD1smrzzOnWgaEGf/+uZZ7/PdF/fbPRF+22N/C50X3E2W q3mtKtdV2FD/58mwvzymklQIeX0551FxrCQGuaUCUPOZiQrVfXDolfm+8oZx7tJY7Nke GpC87XBc/AUyhwWDPnDyi7G12RVvhLgdjGyBggeT+zRtaAphD/rJ3F6zxJhsyOCVWHE5 KfzDD7XZsw6khJ09hts/JYS3jltK4bc9PkQi2G1ShD7j0mFHP/0yIOehIROGWPUpkK9U GvRw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from; bh=OFKMClpIBXA05bGKXI2Pwdx7qxjfaQf3j7j/s69Lw4o=; b=QFkxs51rsGaPR5IogmRUgjRyo74P5kzpajnWRuJVwN8EzD6Zh2NUaoaqz9h0L82/CA pk+vqbYP3NzCdwb8Nrc1+VF9E2Ei7UVygdrwhSG20zvTT4tF6Rww3EBgGX2uEJRheq82 0L+z6MH/CrK5YrYXv2udGJ0Q0KO4qA0PdpD1mWjggTQ07dj9pIciAsqfsJegxTtTK2zy TaK+dhzkeuxu38/8wAKmjAEbrEZxLoGCpv7v5lLxpC5LyvTXDXP2oXGIhX4vMWY2tqNu GA1tLOVOlO8wJAxfe3yIoAmYg5gxqbGPrwaWzFrt+x4ISPaTT0FbsF6BpmwcipD7SAWM MGtA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id c17si21345036pgl.385.2018.12.21.05.24.28; Fri, 21 Dec 2018 05:24:45 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388683AbeLUBiv (ORCPT + 99 others); Thu, 20 Dec 2018 20:38:51 -0500 Received: from mx1.redhat.com ([209.132.183.28]:33322 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730104AbeLUBiu (ORCPT ); Thu, 20 Dec 2018 20:38:50 -0500 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 000F034CB; Fri, 21 Dec 2018 01:38:49 +0000 (UTC) Received: from kasong-desktop-nay-redhat-com.nay.redhat.com (unknown [10.66.128.41]) by smtp.corp.redhat.com (Postfix) with ESMTP id CD4D1600C1; Fri, 21 Dec 2018 01:38:42 +0000 (UTC) From: Kairui Song To: linux-kernel@vger.kernel.org Cc: tglx@linutronix.de, bp@alien8.de, mingo@redhat.com, hpa@zytor.com, jbohac@suse.cz, adobriyan@gmail.com, akpm@linux-foundation.org, osandov@fb.com, bhsharma@redhat.com, kasong@redhat.com, bhe@redhat.com, dyoung@redhat.com Subject: [PATCH 1/1] x86/gart/kcore: Exclude GART aperture from kcore Date: Fri, 21 Dec 2018 09:38:12 +0800 Message-Id: <20181221013812.22790-1-kasong@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.29]); Fri, 21 Dec 2018 01:38:50 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On machines where the GART aperture is mapped over physical RAM, /proc/kcore contains the GART aperture range and reading it may lead to kernel panic. In 'commit 2a3e83c6f96c ("x86/gart: Exclude GART aperture from vmcore")', a special workaround is applied for vmcore to let /proc/vmcore return zeroes when attempting to read the aperture region, as vmcore and kcore have the same issue, and after 'commit 707d4eefbdb3 ("Revert "[PATCH] Insert GART region into resource map"")', userspace tools can't detect and exclude GART region. This patch applies the same workaround for kcore. Let /proc/kcore return zeroes too when trying to read the aperture region to fix the issue that reading GART region may raise unexpected exceptions. This applies to both first and second kernels as GART may get initialized in the first and second kernels. To get the same workaround work for kcore, this patch implement a hook infrastructure for kcore which is same as the hook infrastructure for vmcore introduced in 'commit 997c136f518c ("fs/proc/vmcore.c: add hook to read_from_oldmem() to check for non-ram pages")'', and reuses the checking function gart_oldmem_pfn_is_ram introduced in 'commit 2a3e83c6f96c ("x86/gart: Exclude GART aperture from vmcore"),' as the hook function, but rename to gart_mem_pfn_is_ram as now it's for a more generic use. Suggested-by: Baoquan He Signed-off-by: Kairui Song --- arch/x86/kernel/aperture_64.c | 6 ++++-- fs/proc/kcore.c | 34 ++++++++++++++++++++++++++++++++++ include/linux/kcore.h | 3 +++ 3 files changed, 41 insertions(+), 2 deletions(-) diff --git a/arch/x86/kernel/aperture_64.c b/arch/x86/kernel/aperture_64.c index 2c4d5ece7456..e2e45ae63309 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 +#include #include #include #include @@ -66,7 +67,7 @@ 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)); @@ -76,7 +77,8 @@ static void exclude_from_vmcore(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)); + WARN_ON(register_oldmem_pfn_is_ram(&gart_mem_pfn_is_ram)); + WARN_ON(register_mem_pfn_is_ram(&gart_mem_pfn_is_ram)); } #else static void exclude_from_vmcore(u64 aper_base, u32 aper_order) diff --git a/fs/proc/kcore.c b/fs/proc/kcore.c index bbcc185062bb..24e13fcc31ea 100644 --- a/fs/proc/kcore.c +++ b/fs/proc/kcore.c @@ -54,6 +54,35 @@ static LIST_HEAD(kclist_head); static DECLARE_RWSEM(kclist_lock); static int kcore_need_update = 1; +static int (*mem_pfn_is_ram)(unsigned long pfn); + +int register_mem_pfn_is_ram(int (*fn)(unsigned long pfn)) +{ + if (mem_pfn_is_ram) + return -EBUSY; + mem_pfn_is_ram = fn; + return 0; +} + +void unregister_mem_pfn_is_ram(void) +{ + mem_pfn_is_ram = NULL; + wmb(); +} + +static int pfn_is_ram(unsigned long pfn) +{ + int (*fn)(unsigned long pfn); + /* pfn is ram unless fn() checks pagetype */ + int ret = 1; + + fn = mem_pfn_is_ram; + if (fn) + ret = fn(pfn); + + return ret; +} + /* 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 +494,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..6818156fe520 100644 --- a/include/linux/kcore.h +++ b/include/linux/kcore.h @@ -56,4 +56,7 @@ void kclist_add_remap(struct kcore_list *m, void *addr, void *vaddr, size_t sz) } #endif +extern int register_mem_pfn_is_ram(int (*fn)(unsigned long pfn)); +extern void unregister_mem_pfn_is_ram(void); + #endif /* _LINUX_KCORE_H */ -- 2.19.1