Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752703AbdFGTSu (ORCPT ); Wed, 7 Jun 2017 15:18:50 -0400 Received: from mail-co1nam03on0072.outbound.protection.outlook.com ([104.47.40.72]:62176 "EHLO NAM03-CO1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1752530AbdFGTSk (ORCPT ); Wed, 7 Jun 2017 15:18:40 -0400 Authentication-Results: vger.kernel.org; dkim=none (message not signed) header.d=none;vger.kernel.org; dmarc=none action=none header.from=amd.com; From: Tom Lendacky Subject: [PATCH v6 30/34] x86/mm, kexec: Allow kexec to be used with SME To: linux-arch@vger.kernel.org, linux-efi@vger.kernel.org, kvm@vger.kernel.org, linux-doc@vger.kernel.org, x86@kernel.org, kexec@lists.infradead.org, linux-kernel@vger.kernel.org, kasan-dev@googlegroups.com, linux-mm@kvack.org, iommu@lists.linux-foundation.org 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: Wed, 07 Jun 2017 14:18:27 -0500 Message-ID: <20170607191827.28645.93989.stgit@tlendack-t1.amdoffice.net> In-Reply-To: <20170607191309.28645.15241.stgit@tlendack-t1.amdoffice.net> References: <20170607191309.28645.15241.stgit@tlendack-t1.amdoffice.net> User-Agent: StGit/0.17.1-dirty MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: BN6PR04CA0029.namprd04.prod.outlook.com (10.174.93.146) To MWHPR12MB1152.namprd12.prod.outlook.com (10.169.204.16) X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: MWHPR12MB1152: X-MS-Office365-Filtering-Correlation-Id: 4f86407c-be28-49e9-42a6-08d4add9ff5c X-MS-Office365-Filtering-HT: Tenant X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:(22001)(48565401081)(201703131423075)(201703031133081);SRVR:MWHPR12MB1152; X-Microsoft-Exchange-Diagnostics: 1;MWHPR12MB1152;3:XZUDKIq8aRmuZ59r75wxr8M4DYDIZrss0MGGN+9TqY9V38aTILIz12NjjwKjf/PSTlm5jPa8pgRRgjYlBJ62ojBEQ3W4G1PbQqbY8TXlXxwlJ2PbIJCG+yL+F/fDW1e/iCKAHCqGGMZjM5vuJvKt0U4FQsxwiHoS8XvIMPTYHE5zoRuknlOKH159PJkvA22wAuJbW2d0qppwfmB9NthJOCOflT0iXdzdZmXCEtGMHYwaL5nMpN5ep/M9LaYKFVt9nBQRAIId8h51EilE8bFCwSkA/n77CYK0UKhl0fpaO0uKy/GadfKWutwDnwSdttjDzP9H9nRYHTtBgeHLCcycOntE6V2iZ/zc90WV7HsNMkA=;25:Uz3fqEkTomVpuySM+62MBYpKxgXuwHyuPkV4+/b81WoVIa5nzPkIhNdD/dXEJ1FrEQrWBq1eb0SN6O3yAN33vbtKfNA0V/mDW+XcZBuoZzjTkC7lKn4Zq03JrpAQoR2kwfhv6LK9T6+MCzvMvCawIDS3DVAy8kykQldi3H5VGjLsnYpxl2O5mwCBfq31/FQVCBHYuXuPlqbZib0aSC3+Z4cm5Y5XlIGGfIZO0mOKV0sNCDZLsmsR9ORHKA3uVGRxY1fOR5x61C3DPoKGKRo20xuR5EWiTGsNhESZib5krIge7kHB/GUpK34gH9A9pHYNhaIIpWMxSgdS+bkI9SKkvauOexAKDLmvwyDfAPmiqPPkt3Y/gFny8B47YhpVov5U0bGqQkCXK/nDhh0cJ9nREyxy5ruRe4IkoCC8roYFxbmIYFKK6ZGnI9SwoZ+2QNo5yYtxJsIKyZP59icoAmwESjSjKrQMYKmsBaiSqJQ3Oxg= X-Microsoft-Exchange-Diagnostics: 1;MWHPR12MB1152;31:BDoTcfEdJ6QHOPcjfK7qznwv16agXGM+3eJHi4tdwqDGNG1SWA8HHmZslNm8YPNEibVTqBRwLg/cw6pr+4FNPLu6WSRNpcbFp34EiB+lGjcEAAJoRHbew+0kea848njIvSmW8vYkYgdFlERVXNzmXEjRHrCfMwDk5EYbGK3Ro8ifm9ffnkDBXlhuxQzo4YySHPgE6+VOi9h8V4zqsiC3eqk91+Hw0jSxitzlgMwAa4Y=;20:NoVfCPTeXcWe12KojwxEgyWJfghDYCIwVTMpuSBbh+6PfjJC9bGBcIb6NdMVAUYTV6QQi9HzyoFkTLvHlPXNAK2sqhbAMRBemBC0WNPV1zKLL0Zdak6rhelCEPysgIoymigrIhEMoH9NAc1blSNS7ii3TtmRhAIXRoUc3B5gvzPN0f9NkMYvbiA38dcGiz5ZrmqRYGGZ5w13rFbHwU8UI/GL1bEF9ZSBB5SdV2r/zz4VYu8Napv+3ODNMJcV78BbSVebOCMVCPWkofIRuPbt/JlUR0dP9wRdNi4luuZh9pGSdcjJsXhlGelEEdFXWltcZFoIlI1FOjOnKGvOOLWBNeDm1K0KuesLnI0r7ovxT9R+uGDSJZlFWZ6dQEWkbaXA7XktfXwbFCSowKywDr4bDnbK3ex84e+WsI2byldOp9Niee7QfMIJMQBvLwYEgpj/FLLGSWv/BVKTEki+6ctQTQdcbcQ4PU+F13DyGFmiGxTfdoVaz6tlenZ+xIpBL7JQ X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(767451399110); X-Exchange-Antispam-Report-CFA-Test: BCL:0;PCL:0;RULEID:(100000700101)(100105000095)(100000701101)(100105300095)(100000702101)(100105100095)(6040450)(601004)(2401047)(8121501046)(5005006)(93006095)(93001095)(10201501046)(3002001)(100000703101)(100105400095)(6055026)(6041248)(20161123558100)(201703131423075)(201702281528075)(201703061421075)(201703061406153)(20161123564025)(20161123560025)(20161123555025)(20161123562025)(6072148)(100000704101)(100105200095)(100000705101)(100105500095);SRVR:MWHPR12MB1152;BCL:0;PCL:0;RULEID:(100000800101)(100110000095)(100000801101)(100110300095)(100000802101)(100110100095)(100000803101)(100110400095)(100000804101)(100110200095)(100000805101)(100110500095);SRVR:MWHPR12MB1152; X-Microsoft-Exchange-Diagnostics: =?utf-8?B?MTtNV0hQUjEyTUIxMTUyOzQ6M29uOWNCaFpYZFJ2VDZRWE1wVWt3dC9BOCtY?= =?utf-8?B?eWNudGZkZHY1cnp4QURiSHFGRnIwUXpKekhvL2dEb0FMNGc1UzUwM1dmdWps?= =?utf-8?B?QnRiSGpIRE15VTVkVHJYb2pKaWpEbzNBUDZYdlZSVzl1Z3B6NFJXbjMwQWZt?= =?utf-8?B?TmxQTmJFUkRqV3ZrdmZwa2VjbTA3T1BwOWtrSzFjQXdzREhmRDdLN3NkN3N2?= =?utf-8?B?ellPRnZ3OVZZR0hCbVhtd0tiekg3Smc2QzV5MGluZ2dtMzBtNkcvV3FNK1VP?= =?utf-8?B?RUpPZ2g5eTdvMXh4Q0V2Y3k3RDRMZi94SXdKRWlHTG5zeEZweDdhZGxaZnZk?= =?utf-8?B?TVlKTTZkUWpBVUVJVjRpekRDblJmSlNEMWYvUTd2QkhmK1JxRzVlL3dic2Rm?= =?utf-8?B?Y25xL21DRm1KTUd6UTZGaGE2SDdudWJaRW9TR1djb253ei9zOTZ3ZE5nVWNp?= =?utf-8?B?TmluUlJMenpxaGhnV2tLUk5OYnZDN25UQmV4aVVTejVvRWRvS09POFl1ZUUx?= =?utf-8?B?ZVhCbEJ3RTlTK1dHUWxJSEdHV0Z4MDQwZUpUVkZuMFBhS3FodnhMZVFaOGFu?= =?utf-8?B?ZEZ6THc5VmI4NGtELzJxQzRPRnE1VVlKN085Zit2aUJrb05qM3ZRWWhvVWZa?= =?utf-8?B?UVNud3BGNG9jWkF6MkdyUDJqSGthK3lXV3hHR1lRVUtrd2sxWmxKWXVwOFFW?= =?utf-8?B?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mNrNnVl?= =?utf-8?Q?l01pOsyyThpxke6uVwa?= X-Forefront-PRVS: 03319F6FEF X-Forefront-Antispam-Report: SFV:NSPM;SFS:(10009020)(4630300001)(6009001)(39450400003)(39840400002)(39400400002)(39860400002)(39850400002)(39410400002)(81166006)(4001350100001)(7406005)(50986999)(54356999)(8676002)(76176999)(305945005)(47776003)(66066001)(7416002)(5660300001)(23676002)(42186005)(50466002)(33646002)(7736002)(53416004)(2906002)(6666003)(53936002)(103116003)(54906002)(86362001)(575784001)(9686003)(55016002)(2950100002)(478600001)(6116002)(3846002)(4326008)(1076002)(230700001)(25786009)(6506006)(189998001)(97746001)(83506001)(110136004)(38730400002)(72206003)(921003)(1121003);DIR:OUT;SFP:1101;SCL:1;SRVR:MWHPR12MB1152;H:tlendack-t1.amdoffice.net;FPR:;SPF:None;MLV:sfv;LANG:en; X-Microsoft-Exchange-Diagnostics: =?utf-8?B?MTtNV0hQUjEyTUIxMTUyOzIzOjYvVDExcVVCRkFtTzdRdGx5ejdoL0x0N25B?= =?utf-8?B?VXhXMTVKdFFBMVFwcGhpSU5ieFkxMGJZeEo1eVRodXY0TW1iOExSZTJmeGtF?= =?utf-8?B?Um5wQ1NuQXF4R0dZbmJrSXRBU0VocjhUZ0J3YUljQ2E3RUh4cndpbjl1dGhj?= =?utf-8?B?UlB4M28zTXRVQ01lSEVFdCs5d1Y5UGQrT00rby9OdWFyRDNNQTRMTGh4VWVQ?= =?utf-8?B?SHZQM2JKWVZBSFd2aDVhL0RJMjNIMTd5dDNqbXBMRExVd3krM0F6NXZmLzh1?= =?utf-8?B?WWhEOW9iWWVuVmNkaVNaUDY2R1dYS1ZUZ0lkdFNGK1Z6dHc2aDd0S0JFWFo2?= =?utf-8?B?cVEzdSt6YkdYY1JJZHNjT0kyd3MyYzNIaGY3YmhDNEFMNWRmU3NwWEhyZXRp?= =?utf-8?B?Q1lmMy9rWHN6a0VBenRvUElKWWdYblUzc2hKd1pXUEhqY1lJL1owZ1F6MTRn?= =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2m0oM= X-Microsoft-Exchange-Diagnostics: 1;MWHPR12MB1152;5:SJtc7hSKhxay2/xIUSudk4MrxIjGg44Vxk5T9CuPJzKu1VHFT0j/ufkgQS/ElLyG0u4AAVwWca8BQviAAlO+tMFNr3XStkQAIneBWRbl4vm2NCXwASNGywvO7xjb/KksmSvLHb3rYom6H6Vifxm2skpsH8lqSJHXP8SubPbTR0GYszQQlapucO2wBtNCP7PgHhukuymZI8rO9xiayxyZYaGNSbFcJWebS/quw3oYi5mdl6Dfe/xUigvf2JMjr0tAhH8bfJ0nyl3R7NLn0i7TTxvBQo5WibK7FW2vo/NeIDvbexcdtLyd+dpb00FQS6RWU0xAB1sBnnwV/vo6zyjFME7KdaOdSYwg4J+gKwCXEW8zAXzVlf/g7zAwfWon3FYfYEteZHC51fJfAEFyZhisp3Yzk9KLraO99TRgjTBFmboi0bSEIJtQ+bsURuTkP2fQNp/kRkVmv90KedX6vo+HqZoaTK/1QQtw3x8Gt1Oorh0R38woNs/t/vAqhJrWNLNJ;24:lxK1fanwVVTzFxH9rf8T51QyNno+pFVs4Q+3H63E7tVqpFVigIXyRRacT+tbL28S2Z+SzsbIWT1kSwGyiVAhRD76LFF/WskOMc8uzgla0us= SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1;MWHPR12MB1152;7:DjgzR8c1I8k86eYSj6At3LHkEr/gXNDyRDLOpVOtNOfWEAgk9CE3nQ+JCAvY70+7PZckEV7chu6ZXSI129PsGukYKB7JgsRoeO5Cx99oAhyJukYkMYkHhpCUvlCEPviSoJPrpZHEw3OgdJAm0jsCgbANMbfOtbdr/TAz3SsvZv64cXXfKJ0k2Ai3ripYt0nPxdJ8VWQehw7THYSlUt3IUA3g7TaaLa4alma/o9cWThHUiMGtXFXs4LYciTB4P37hq7eVZez0gDZi8CqWQmDDlG2xpJZYKVq8TCgfYIaJQ+/WW5JEbOVOFUSAh2g6qSLLIEijwJjNzr90m6vfuRHZsQ==;20:7AzmTfd7CR16LWnesXP1J0oSSRaaInnOr0saD6dMBEQHbz2OFh53PtY+U7JFFsxhDSMGw5J2d7mvkWXg8YCbte86x2txJJEnVed44GvZ5KKhM/3DxYiwtxj5/LPIkWMT1r+WkmUXUyEnbn1XQMpdoWXbCnj5b87kP+yk8sbzStWRECtnpoXU2Rn/PfCX6F6Jtc1G1lwRYFdKVw1QYjIpH69AkxrKNAubXwhp/5wLg/tveRhmTcvIeebNFNS2izgF X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 07 Jun 2017 19:18:32.2728 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: MWHPR12MB1152 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 8960 Lines: 261 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/kexec.h | 8 ++++++++ arch/x86/include/asm/pgtable_types.h | 1 + arch/x86/kernel/machine_kexec_64.c | 35 +++++++++++++++++++++++++++++++++- arch/x86/kernel/process.c | 17 +++++++++++++++-- arch/x86/mm/ident_map.c | 12 ++++++++---- include/linux/kexec.h | 14 ++++++++++++++ kernel/kexec_core.c | 6 ++++++ 8 files changed, 87 insertions(+), 7 deletions(-) diff --git a/arch/x86/include/asm/init.h b/arch/x86/include/asm/init.h index 474eb8c..05c4aa0 100644 --- a/arch/x86/include/asm/init.h +++ b/arch/x86/include/asm/init.h @@ -7,6 +7,7 @@ struct x86_mapping_info { unsigned long page_flag; /* page flag for PMD or PUD entry */ unsigned long offset; /* ident mapping offset */ bool direct_gbpages; /* PUD level 1GB page support */ + 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/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 6f5ca4e..35e069a 100644 --- a/arch/x86/kernel/machine_kexec_64.c +++ b/arch/x86/kernel/machine_kexec_64.c @@ -87,7 +87,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); @@ -115,6 +115,7 @@ static int init_pgtable(struct kimage *image, unsigned long start_pgtable) .alloc_pgt_page = alloc_pgt_page, .context = image, .page_flag = __PAGE_KERNEL_LARGE_EXEC, + .kernpg_flag = _KERNPG_TABLE_NOENC, }; unsigned long mstart, mend; pgd_t *level4p; @@ -602,3 +603,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..fdad0fb 100644 --- a/arch/x86/kernel/process.c +++ b/arch/x86/kernel/process.c @@ -355,6 +355,7 @@ bool xen_set_default_idle(void) return ret; } #endif + void stop_this_cpu(void *dummy) { local_irq_disable(); @@ -365,8 +366,20 @@ void stop_this_cpu(void *dummy) disable_local_APIC(); mcheck_cpu_clear(this_cpu_ptr(&cpu_info)); - for (;;) - halt(); + for (;;) { + /* + * Use wbinvd followed by hlt to stop the processor. This + * provides support for kexec on a processor that supports + * SME. 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. To achieve this a wbinvd is performed followed by + * a hlt. Even if the processor is not in the kexec/SME + * scenario this only adds a wbinvd to a halting processor. + */ + asm volatile("wbinvd; hlt" : : : "memory"); + } } /* diff --git a/arch/x86/mm/ident_map.c b/arch/x86/mm/ident_map.c index adab159..31cea98 100644 --- a/arch/x86/mm/ident_map.c +++ b/arch/x86/mm/ident_map.c @@ -51,7 +51,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) | info->kernpg_flag)); } return 0; @@ -79,7 +79,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) | info->kernpg_flag)); } return 0; @@ -93,6 +93,10 @@ int kernel_ident_mapping_init(struct x86_mapping_info *info, pgd_t *pgd_page, unsigned long next; int result; + /* Set the default pagetable flags if not supplied */ + if (!info->kernpg_flag) + info->kernpg_flag = _KERNPG_TABLE; + for (; addr < end; addr = next) { pgd_t *pgd = pgd_page + pgd_index(addr); p4d_t *p4d; @@ -116,14 +120,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) | info->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) | info->kernpg_flag)); } } diff --git a/include/linux/kexec.h b/include/linux/kexec.h index c9481eb..5d17fd6 100644 --- a/include/linux/kexec.h +++ b/include/linux/kexec.h @@ -334,6 +334,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 ae1a3ba..ecab7b3 100644 --- a/kernel/kexec_core.c +++ b/kernel/kexec_core.c @@ -309,6 +309,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; @@ -320,6 +323,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);