From patchwork Tue Apr 18 21:21:21 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Lendacky X-Patchwork-Id: 9686399 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 3F871602C2 for ; Tue, 18 Apr 2017 21:22:14 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2E9732094F for ; Tue, 18 Apr 2017 21:22:14 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 23370269DA; Tue, 18 Apr 2017 21:22:14 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D2F3F25223 for ; Tue, 18 Apr 2017 21:22:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932855AbdDRVWB (ORCPT ); Tue, 18 Apr 2017 17:22:01 -0400 Received: from mail-bl2nam02on0067.outbound.protection.outlook.com ([104.47.38.67]:26167 "EHLO NAM02-BL2-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1758013AbdDRVVw (ORCPT ); Tue, 18 Apr 2017 17:21:52 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amdcloud.onmicrosoft.com; s=selector1-amd-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=8+IiNQAfGzobmRiYLBVRyxy35UZbrDlDlYMBM4s8M6s=; b=BW3jATR5CaF7ahmzDnbC9vvRDQZdqyZULpjMXRDrnHzQPsd9bx4tVYmppbgqilVXQCMROABhGBtzS50+ai+bdUEw0JTMAdY7kQ6Eop8jjLZqNprOUv8gnqMJW/kG88KgU5plxKDpQcHmHB0Z2m9Po3BkBv1c9wLJPMKg2V828HU= Authentication-Results: vger.kernel.org; dkim=none (message not signed) header.d=none; vger.kernel.org; dmarc=none action=none header.from=amd.com; Received: from tlendack-t1.amdoffice.net (165.204.77.1) by CY4PR12MB1144.namprd12.prod.outlook.com (10.168.164.136) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1034.10; Tue, 18 Apr 2017 21:21:43 +0000 From: Tom Lendacky Subject: [PATCH v5 28/32] x86/mm, kexec: Allow kexec to be used with SME To: , , , , , , , , , CC: Rik van Riel , Radim =?utf-8?b?S3LEjW3DocWZ?= , Toshimitsu Kani , Arnd Bergmann , Jonathan Corbet , Matt Fleming , "Michael S. Tsirkin" , Joerg Roedel , Konrad Rzeszutek Wilk , Paolo Bonzini , Larry Woodman , Brijesh Singh , Ingo Molnar , Borislav Petkov , Andy Lutomirski , "H. Peter Anvin" , Andrey Ryabinin , Alexander Potapenko , Dave Young , Thomas Gleixner , Dmitry Vyukov Date: Tue, 18 Apr 2017 16:21:21 -0500 Message-ID: <20170418212121.10190.94885.stgit@tlendack-t1.amdoffice.net> In-Reply-To: <20170418211612.10190.82788.stgit@tlendack-t1.amdoffice.net> References: <20170418211612.10190.82788.stgit@tlendack-t1.amdoffice.net> User-Agent: StGit/0.17.1-dirty MIME-Version: 1.0 X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: DM5PR13CA0036.namprd13.prod.outlook.com (10.168.240.150) To CY4PR12MB1144.namprd12.prod.outlook.com (10.168.164.136) X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 6a5b8bd0-9038-419f-772a-08d486a0ea31 X-MS-Office365-Filtering-HT: Tenant X-Microsoft-Antispam: UriScan:; BCL:0; PCL:0; RULEID:(22001)(48565401081)(201703131423075)(201703031133081); SRVR:CY4PR12MB1144; X-Microsoft-Exchange-Diagnostics: 1; CY4PR12MB1144; 3:Zp56Yhu3VPdNMXXA7EAE7QWkEr4Xx1r8g+RtIMI4HCjJfUoV+iKR/1QMWq2K0BkPYfqy8GGoWaKQ/r1GI7ASVbDhQpkb/jwmdDGP9wKUgl8W2Cw2kcfqT1tT1HEt4LYdMgBapNUSdJCP2kpLCUhfsYmQtPt/9YukB2+pah7TRWNgG/fUKJ/1AKNlfVG3DfRKFYHyK8A8a6ZWC2iZyiRFmwwc89osckpP8vi9cxucPcB4DMj8Vpjs+0oW0nK+ro1964hbE1kbvYo1Pici+S8tkf7/nil84aam7SkptWX/EW6EyWV6b3Y8hFLt29hDWWPJjtOp/0kOaO+2PcqIuBx5/cFLtsPwspHWCO4861hcVRU=; 25:uUPEhBXJOQtP5xrENIyE9FO6jO+KwLGEFDWbviywApqMtVbDRUTVEGpTv4URrDWNNRyBPH5eLSe37hGvUpmav1lBJC/nNceacDLzdeDgz14NFWUfoCBU3Z9htkutebbsqrj6cGxzRPZk3jMZAfGds4QzJMj4vJVFl9mZTjnuayj0XvMnEwIiVehXt3aTxceR6UH3rswJa6NKMmE0FSDxS0KyNKTMZnhNA0CxbFXsDiC0pFW66+Lm02pRIq7n9/vRH4VHXCCg7f6yDC5rk/ZWcZW5zND800vainlgj3Lg5DcRV0uOMfxhgKuYbijL0+aujZqACoeIUosWkGV87ZDpmoEHWJCSxsfMEjEuupClFbHlYT2ZFjv+m7UM5267tU+h0YbOO+m3GYRsvCT11Nki2mKIyvDtR1OdGVXMYaNPLMHxJ7NzkjJII2e90iWq3uRB41+rR9c1fslPlpSwbOJtIA== X-Microsoft-Exchange-Diagnostics: 1; CY4PR12MB1144; 31:E+TK3ltIPD3oMnoBZ1O9WHKjXBUdv2LpFRl7w57Cz+/q7V9cQY9ME6M5KVVrwnwaWFIxVt7bTjE/80Oz4TXHM9+2nFlQfkMVLGhgmXKF2pGqHFtrzBMfDvmwMyP5wCzAzM7LK3FTYCCdvfQFSuLowiNtbQyO3CEvjDc/zpOzgQZcOevUyCk+pYqeUBtgB6twF4jFYWjJPTet1c5pV07hxJKm8HOJc2B2v8BrJTJIKcc=; 20:07Fk+a4NRGCxdOScOnCICBXpDWVd4LqdnU3zL0oq91nmfKuMeyR5OYfoa/iwPsR99/WRCTGs9tghKPCnFHL+qqmr0Olsy4R1JLOKgPR8l0Ls53OQjOAt0ZV2Q80ht0f3R9352kYlzVkDU2c3wuinKa8Rw7ae2UjD8/9PhdiS7wHikqU7VeqtyMwB5UMsbFElB7QKUVzUaoJ+b3ciVw4pA5t+P+3VgFk1HlbQr5LdBExkVXwN0vy5odvJYO/nVc8PyRzRujIqE00xTqhNMLEaI1FsyzLbWfRje2kl42Vg31fTCiDpjwLZMyk6bZdKlZB5KBwxNKTasdKZe79C1a7m9/GnOeZ20DN5u3uUYQUkobWjfPU94oc8NkG0+v3p5BxWdaERZND68v3PldpFQGrxVcdJrlKLWDPAm6n2EGjzmrJ3JQbL8gjjr8CE8wS8aLOKKlzDEKHXPR6asVlm43jdmE0/RkVqxXALRx9/hf3A8p3eEOb109IO0JVD3jYSq+bl X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(767451399110); X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(6040450)(601004)(2401047)(5005006)(8121501046)(93006095)(93001095)(3002001)(10201501046)(6055026)(6041248)(201703131423075)(201702281528075)(201703061421075)(20161123555025)(20161123560025)(20161123562025)(20161123564025)(6072148); SRVR:CY4PR12MB1144; BCL:0; PCL:0; RULEID:; SRVR:CY4PR12MB1144; X-Microsoft-Exchange-Diagnostics: 1; CY4PR12MB1144; 4:JIiGCGQ+zfzcC/t7HSCYOJFs+Mvd+SU7G7aloO5mTuEz4Sfj3WfzlmNGzjH1ZBl526O8oE0dIvI/9gzDVvU1OgJ+68xsFZktl/K6AaphQW00Exy9r+sZ1xPfZ/vuLIlU6xV4Lu0iUEampg6eZ/rIsp+7Ant2gF43+zQYwHIslQUMep50GY1/r5AiSrZ5MKGtnnls0y+n3MyVC88FgD+88+LH/6OREYJblQOc637ICcRT/KdkH+uNW2sdrORiscbJTAhkeDMt269PbNf2gSf+mWEy6ijJstX1hJRPVkPNQ2QcBv8Qf5oJ++tQZXIGJamA+ZA/QmS+UJPibtarLoNkkztyhQZAfvFBSuFOb/Dx0hhBTTZo0CVKA4yiJtbHKkiI/eMhi1qyBpAs4b28uF8VGFTl5BtX8iQ9WtPwFIy5cLrc3HE0SJnxKfpHjXLUOBBAL+FZkCgPXqp6eCU4dge54DMr1IzzyWp+1xYLZzKaYqnGDIsfnAaahTv2rcKYuw8i5LPws/aOgp/ZgBbEFWXnxiB2aIb6a5AA0sXUphXdCvhNDTqZPdmVZL7vvLXtBujF2JhWCbON3iCNQwUrgjtNRMZ7H8BaIQT0g3uVWTTzwt/kVQOSE6Voi65DIv4cEvo/ooOlMV8ED0So6tiGEsYTsgsUdnzjukMUrS7xualwWirJ30wba3vORW6F2LjHQrqX6a7+naGB6FRo9ka4t7nfWhT3CRydrBN/McrThsqm+Xn42/fvXqgtu15NDJ0566tLDrQD2EPADe2ys+0KTVQIBg== X-Forefront-PRVS: 028166BF91 X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10009020)(4630300001)(6009001)(39850400002)(39840400002)(39410400002)(39400400002)(39860400002)(39450400003)(5660300001)(575784001)(7736002)(1076002)(2201001)(97746001)(305945005)(7416002)(83506001)(6666003)(2950100002)(25786009)(86362001)(3846002)(6506006)(2906002)(230700001)(7406005)(9686003)(55016002)(4001350100001)(6116002)(189998001)(54906002)(53936002)(54356999)(76176999)(38730400002)(33646002)(50986999)(4326008)(50466002)(66066001)(81166006)(23676002)(103116003)(8676002)(42186005)(47776003)(53416004)(921003)(71626007)(1121003); DIR:OUT; SFP:1101; SCL:1; SRVR:CY4PR12MB1144; H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ft-Exchange-Diagnostics: 1; CY4PR12MB1144; 6:7GKO2aotBEFEEBFpAQAptMQOIRN9KJqwU8DXhP76ogjGgHTuoYL4ImeP/Ipfaf2S1MP5JlUNKsRXUq8md2M1wae1sCJK1/KJlxiQ0FsJEhct7bVBpRmKXyGQ5cNrbvCN6o3jKCeJMNEni43KcaMKlAxaFp3P9P9LozLJzfD7s+GYFl3CVc3/F0/izNLaf8HQPQOdhnoL230Rsil1OboJYC0itLD8qbPQnp/3fiA6nsiKuBPp6enyW82RrkW/282h3HgO0R6AToIa+vW5MIklB5RKcBsaPOItWI2Wtr/DFjXZBbKMnW4R8Zc0UEgCezXsHZFrZVIFOty5Xjj+YfRblYryHXWTo6/V1pBqwVSdQcUIC3/ODfOdR1+4ZSn7xnQe/d+hMqtNr7BSePidaOBGxTuVex2s+RA4PwJll4j/8akASdGtr9roK79RnesGlYr4bsW+xw2YRODKZc10OYt0AgAejt12KUzDGe38R71Uz/Y=; 5:J9VmhlOams0ybJSk6EjKFBWinnItB9RcRjHGowRa1Ot4GI35236R0eRlk542LGvyKj+Bh+REA+xIRNpJv7TCyI+QzMulPelkviymQzJcUrqWnWGwSh6BeHgNRlV02VFvewf+K7xMLy1vSr/N9ZT6Lw==; 24:5i8C547VhYGVKOpzfr/OscwmcOIvNtlef08yz9C0PUeh7BLCxIgntC3noz9YJCzVcGY20L+aFsfTEzudlk//Xf5MaD9tIjmhu9QxE6zmGfM= SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1; CY4PR12MB1144; 7:4MEyzOHh3x0ke+xRsGAa94xI9JjKa62xZuusmy6YswNlnVC0vcbtqCFSSeJngJeXagnxhbeSmdrhnrrhpPVzz+xJCH5KKuPyXDd9fMYKa04pyJulKBpnO7No2vqqvFvjgbh/z6GOsUHVaoqn1oXzIRVVFAId5tgb4DjtpzQigkRzuUgrUmN4taD8Zp36cT/tWD4jggBHySSfu/MPLQanBr52t2sBQwYXzAYPyG1NbKpAuJrEDSJ0ePrdHFLbdJ7p8+ukAoaVa0XH+TXcJtOph3dNUHhPAoCTUnlSufG7UjYQxEnZ++t0MwuJ/WbgSBvjkazrohL4qdVjsrRfMMyuNA==; 20:zUJ9gAEwGXgPU5859mfdZt1aBLE+svCXwgHA3vWOFSi9NTHAJm54rqz5cMB64Ub7YAafeCSMo1oYvHSGICmQmkXQ3S6osGj16zBWaVz+HBcbzs9NE/QX9FPjHEn/Ut+CXTes/JcCWvCGcM2jajTeZsrhSh373WJosqn4qIt/q5KLbcMDPxb4eYY5deK11LvlEjZ8ghIgQOZg8K3uzWCM1Xt0LUqJm4GcyeGuZKFRqFZU43d1wTiEEI1umwefdJSx X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 18 Apr 2017 21:21:43.5361 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY4PR12MB1144 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Provide support so that kexec can be used to boot a kernel when SME is enabled. Support is needed to allocate pages for kexec without encryption. This is needed in order to be able to reboot in the kernel in the same manner as originally booted. Additionally, when shutting down all of the CPUs we need to be sure to flush the caches and then halt. This is needed when booting from a state where SME was not active into a state where SME is active (or vice-versa). Without these steps, it is possible for cache lines to exist for the same physical location but tagged both with and without the encryption bit. This can cause random memory corruption when caches are flushed depending on which cacheline is written last. Signed-off-by: Tom Lendacky --- arch/x86/include/asm/init.h | 1 + arch/x86/include/asm/irqflags.h | 5 +++++ arch/x86/include/asm/kexec.h | 8 ++++++++ arch/x86/include/asm/pgtable_types.h | 1 + arch/x86/kernel/machine_kexec_64.c | 35 +++++++++++++++++++++++++++++++++- arch/x86/kernel/process.c | 26 +++++++++++++++++++++++-- arch/x86/mm/ident_map.c | 11 +++++++---- include/linux/kexec.h | 14 ++++++++++++++ kernel/kexec_core.c | 7 +++++++ 9 files changed, 101 insertions(+), 7 deletions(-) diff --git a/arch/x86/include/asm/init.h b/arch/x86/include/asm/init.h index 737da62..b2ec511 100644 --- a/arch/x86/include/asm/init.h +++ b/arch/x86/include/asm/init.h @@ -6,6 +6,7 @@ struct x86_mapping_info { void *context; /* context for alloc_pgt_page */ unsigned long pmd_flag; /* page flag for PMD entry */ unsigned long offset; /* ident mapping offset */ + unsigned long kernpg_flag; /* kernel pagetable flag override */ }; int kernel_ident_mapping_init(struct x86_mapping_info *info, pgd_t *pgd_page, diff --git a/arch/x86/include/asm/irqflags.h b/arch/x86/include/asm/irqflags.h index ac7692d..38b5920 100644 --- a/arch/x86/include/asm/irqflags.h +++ b/arch/x86/include/asm/irqflags.h @@ -58,6 +58,11 @@ static inline __cpuidle void native_halt(void) asm volatile("hlt": : :"memory"); } +static inline __cpuidle void native_wbinvd_halt(void) +{ + asm volatile("wbinvd; hlt" : : : "memory"); +} + #endif #ifdef CONFIG_PARAVIRT diff --git a/arch/x86/include/asm/kexec.h b/arch/x86/include/asm/kexec.h index 70ef205..e8183ac 100644 --- a/arch/x86/include/asm/kexec.h +++ b/arch/x86/include/asm/kexec.h @@ -207,6 +207,14 @@ struct kexec_entry64_regs { uint64_t r15; uint64_t rip; }; + +extern int arch_kexec_post_alloc_pages(void *vaddr, unsigned int pages, + gfp_t gfp); +#define arch_kexec_post_alloc_pages arch_kexec_post_alloc_pages + +extern void arch_kexec_pre_free_pages(void *vaddr, unsigned int pages); +#define arch_kexec_pre_free_pages arch_kexec_pre_free_pages + #endif typedef void crash_vmclear_fn(void); diff --git a/arch/x86/include/asm/pgtable_types.h b/arch/x86/include/asm/pgtable_types.h index ce8cb1c..0f326f4 100644 --- a/arch/x86/include/asm/pgtable_types.h +++ b/arch/x86/include/asm/pgtable_types.h @@ -213,6 +213,7 @@ enum page_cache_mode { #define PAGE_KERNEL __pgprot(__PAGE_KERNEL | _PAGE_ENC) #define PAGE_KERNEL_RO __pgprot(__PAGE_KERNEL_RO | _PAGE_ENC) #define PAGE_KERNEL_EXEC __pgprot(__PAGE_KERNEL_EXEC | _PAGE_ENC) +#define PAGE_KERNEL_EXEC_NOENC __pgprot(__PAGE_KERNEL_EXEC) #define PAGE_KERNEL_RX __pgprot(__PAGE_KERNEL_RX | _PAGE_ENC) #define PAGE_KERNEL_NOCACHE __pgprot(__PAGE_KERNEL_NOCACHE | _PAGE_ENC) #define PAGE_KERNEL_LARGE __pgprot(__PAGE_KERNEL_LARGE | _PAGE_ENC) diff --git a/arch/x86/kernel/machine_kexec_64.c b/arch/x86/kernel/machine_kexec_64.c index 085c3b3..11c0ca9 100644 --- a/arch/x86/kernel/machine_kexec_64.c +++ b/arch/x86/kernel/machine_kexec_64.c @@ -86,7 +86,7 @@ static int init_transition_pgtable(struct kimage *image, pgd_t *pgd) set_pmd(pmd, __pmd(__pa(pte) | _KERNPG_TABLE)); } pte = pte_offset_kernel(pmd, vaddr); - set_pte(pte, pfn_pte(paddr >> PAGE_SHIFT, PAGE_KERNEL_EXEC)); + set_pte(pte, pfn_pte(paddr >> PAGE_SHIFT, PAGE_KERNEL_EXEC_NOENC)); return 0; err: free_transition_pgtable(image); @@ -114,6 +114,7 @@ static int init_pgtable(struct kimage *image, unsigned long start_pgtable) .alloc_pgt_page = alloc_pgt_page, .context = image, .pmd_flag = __PAGE_KERNEL_LARGE_EXEC, + .kernpg_flag = _KERNPG_TABLE_NOENC, }; unsigned long mstart, mend; pgd_t *level4p; @@ -597,3 +598,35 @@ void arch_kexec_unprotect_crashkres(void) { kexec_mark_crashkres(false); } + +int arch_kexec_post_alloc_pages(void *vaddr, unsigned int pages, gfp_t gfp) +{ + int ret; + + if (sme_active()) { + /* + * If SME is active we need to be sure that kexec pages are + * not encrypted because when we boot to the new kernel the + * pages won't be accessed encrypted (initially). + */ + ret = set_memory_decrypted((unsigned long)vaddr, pages); + if (ret) + return ret; + + if (gfp & __GFP_ZERO) + memset(vaddr, 0, pages * PAGE_SIZE); + } + + return 0; +} + +void arch_kexec_pre_free_pages(void *vaddr, unsigned int pages) +{ + if (sme_active()) { + /* + * If SME is active we need to reset the pages back to being + * an encrypted mapping before freeing them. + */ + set_memory_encrypted((unsigned long)vaddr, pages); + } +} diff --git a/arch/x86/kernel/process.c b/arch/x86/kernel/process.c index 0bb8842..f4e5de6 100644 --- a/arch/x86/kernel/process.c +++ b/arch/x86/kernel/process.c @@ -24,6 +24,7 @@ #include #include #include +#include #include #include #include @@ -355,8 +356,25 @@ bool xen_set_default_idle(void) return ret; } #endif + void stop_this_cpu(void *dummy) { + bool do_wbinvd_halt = false; + + if (kexec_in_progress && boot_cpu_has(X86_FEATURE_SME)) { + /* + * If we are performing a kexec and the processor supports + * SME then we need to clear out cache information before + * halting. With kexec, going from SME inactive to SME active + * requires clearing cache entries so that addresses without + * the encryption bit set don't corrupt the same physical + * address that has the encryption bit set when caches are + * flushed. Perform a wbinvd followed by a halt to achieve + * this. + */ + do_wbinvd_halt = true; + } + local_irq_disable(); /* * Remove this CPU: @@ -365,8 +383,12 @@ void stop_this_cpu(void *dummy) disable_local_APIC(); mcheck_cpu_clear(this_cpu_ptr(&cpu_info)); - for (;;) - halt(); + for (;;) { + if (do_wbinvd_halt) + native_wbinvd_halt(); + else + halt(); + } } /* diff --git a/arch/x86/mm/ident_map.c b/arch/x86/mm/ident_map.c index 04210a2..2c9fd3e 100644 --- a/arch/x86/mm/ident_map.c +++ b/arch/x86/mm/ident_map.c @@ -20,6 +20,7 @@ static void ident_pmd_init(struct x86_mapping_info *info, pmd_t *pmd_page, static int ident_pud_init(struct x86_mapping_info *info, pud_t *pud_page, unsigned long addr, unsigned long end) { + unsigned long kernpg_flag = info->kernpg_flag ? : _KERNPG_TABLE; unsigned long next; for (; addr < end; addr = next) { @@ -39,7 +40,7 @@ static int ident_pud_init(struct x86_mapping_info *info, pud_t *pud_page, if (!pmd) return -ENOMEM; ident_pmd_init(info, pmd, addr, next); - set_pud(pud, __pud(__pa(pmd) | _KERNPG_TABLE)); + set_pud(pud, __pud(__pa(pmd) | kernpg_flag)); } return 0; @@ -48,6 +49,7 @@ static int ident_pud_init(struct x86_mapping_info *info, pud_t *pud_page, static int ident_p4d_init(struct x86_mapping_info *info, p4d_t *p4d_page, unsigned long addr, unsigned long end) { + unsigned long kernpg_flag = info->kernpg_flag ? : _KERNPG_TABLE; unsigned long next; for (; addr < end; addr = next) { @@ -67,7 +69,7 @@ static int ident_p4d_init(struct x86_mapping_info *info, p4d_t *p4d_page, if (!pud) return -ENOMEM; ident_pud_init(info, pud, addr, next); - set_p4d(p4d, __p4d(__pa(pud) | _KERNPG_TABLE)); + set_p4d(p4d, __p4d(__pa(pud) | kernpg_flag)); } return 0; @@ -76,6 +78,7 @@ static int ident_p4d_init(struct x86_mapping_info *info, p4d_t *p4d_page, int kernel_ident_mapping_init(struct x86_mapping_info *info, pgd_t *pgd_page, unsigned long pstart, unsigned long pend) { + unsigned long kernpg_flag = info->kernpg_flag ? : _KERNPG_TABLE; unsigned long addr = pstart + info->offset; unsigned long end = pend + info->offset; unsigned long next; @@ -104,14 +107,14 @@ int kernel_ident_mapping_init(struct x86_mapping_info *info, pgd_t *pgd_page, if (result) return result; if (IS_ENABLED(CONFIG_X86_5LEVEL)) { - set_pgd(pgd, __pgd(__pa(p4d) | _KERNPG_TABLE)); + set_pgd(pgd, __pgd(__pa(p4d) | kernpg_flag)); } else { /* * With p4d folded, pgd is equal to p4d. * The pgd entry has to point to the pud page table in this case. */ pud_t *pud = pud_offset(p4d, 0); - set_pgd(pgd, __pgd(__pa(pud) | _KERNPG_TABLE)); + set_pgd(pgd, __pgd(__pa(pud) | kernpg_flag)); } } diff --git a/include/linux/kexec.h b/include/linux/kexec.h index d419d0e..1c76e3b 100644 --- a/include/linux/kexec.h +++ b/include/linux/kexec.h @@ -383,6 +383,20 @@ static inline void *boot_phys_to_virt(unsigned long entry) return phys_to_virt(boot_phys_to_phys(entry)); } +#ifndef arch_kexec_post_alloc_pages +static inline int arch_kexec_post_alloc_pages(void *vaddr, unsigned int pages, + gfp_t gfp) +{ + return 0; +} +#endif + +#ifndef arch_kexec_pre_free_pages +static inline void arch_kexec_pre_free_pages(void *vaddr, unsigned int pages) +{ +} +#endif + #else /* !CONFIG_KEXEC_CORE */ struct pt_regs; struct task_struct; diff --git a/kernel/kexec_core.c b/kernel/kexec_core.c index bfe62d5..bb5e7e3 100644 --- a/kernel/kexec_core.c +++ b/kernel/kexec_core.c @@ -38,6 +38,7 @@ #include #include #include +#include #include #include @@ -315,6 +316,9 @@ static struct page *kimage_alloc_pages(gfp_t gfp_mask, unsigned int order) count = 1 << order; for (i = 0; i < count; i++) SetPageReserved(pages + i); + + arch_kexec_post_alloc_pages(page_address(pages), count, + gfp_mask); } return pages; @@ -326,6 +330,9 @@ static void kimage_free_pages(struct page *page) order = page_private(page); count = 1 << order; + + arch_kexec_pre_free_pages(page_address(page), count); + for (i = 0; i < count; i++) ClearPageReserved(page + i); __free_pages(page, order);