Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933473AbeAHXSf (ORCPT + 1 other); Mon, 8 Jan 2018 18:18:35 -0500 Received: from mail-bn3nam01on0071.outbound.protection.outlook.com ([104.47.33.71]:25610 "EHLO NAM01-BN3-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1755752AbeAHXSc (ORCPT ); Mon, 8 Jan 2018 18:18:32 -0500 Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=Thomas.Lendacky@amd.com; Subject: Re: [PATCH v2 3/5] x86/mm: Centralize PMD flags in sme_encrypt_kernel() To: Borislav Petkov Cc: x86@kernel.org, Brijesh Singh , linux-kernel@vger.kernel.org, Ingo Molnar , "H. Peter Anvin" , Thomas Gleixner References: <20171221220242.30632.5031.stgit@tlendack-t1.amdoffice.net> <20171221220312.30632.22231.stgit@tlendack-t1.amdoffice.net> <20180107162852.ahfcorhvi2i7xjnr@pd.tnic> From: Tom Lendacky Message-ID: Date: Mon, 8 Jan 2018 17:18:26 -0600 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.5.2 MIME-Version: 1.0 In-Reply-To: <20180107162852.ahfcorhvi2i7xjnr@pd.tnic> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit X-Originating-IP: [165.204.78.1] X-ClientProxiedBy: CY4PR1801CA0010.namprd18.prod.outlook.com (10.171.252.23) To BN6PR12MB1140.namprd12.prod.outlook.com (10.168.226.142) X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: 0a5f5780-adbf-4be7-bb2c-08d556ee218f X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:(48565401081)(4534020)(4602075)(4627115)(201703031133081)(201702281549075)(5600026)(4604075)(2017052603307)(7153060)(7193020);SRVR:BN6PR12MB1140; X-Microsoft-Exchange-Diagnostics: 1;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EL7NXL86uE6JX8i4ufHld0rqwxoMoC3kBgl0ft2PZo6lqSPNqAz5OWdO1jUcy3MhAz/0Jm9K6F0bb7DaYrlTHo62dAUF1+4jEseqp3a5nE5fY5PaDwR588mwEE8RhmIPfER89bjK2iwpBNs+fTM4Cek8CiXwjz/WL65uZstqx1wj+rp+Q5spWkaITqqFqcV5k7DI4sXII0UJgciRL64Ep6odX+HPEMuDz72c3OeAEPFAt9tSoj202CLaZhRWuCaCNP+HdLtes9KKKH9g0ZZwyUGwX5wgz/tbQC5JqvcT9mIZwPUIOMnryX//zvMLNQrBhCorkQJAAO6sOae/JzvcH/VN131S2Cga;4:qSoJbeTunswbajonDMnKGFN1CanX7ymvNlNtdj/IloU+c3akGVf+Ody3oES/9reda6qRIBV4apCUS56NdpyudMUalhMash2WQZRTubQX3hoc2xk3JKXd0W2cSozpxum4BAeksNor90aRmnD9KYxeNTNStGGgflcqJpBLIvccDk8h3w1wmQUZ8j1VMwSbeCq3oXESDNTysbLfzonGy0VRu0SJI7NJFnfvKtQwMtb3SddFKy5IhM+WNvCP+rrLPLK2j4ZbtKSvD1VMf6rFuIRApvRHAykHH36cSG+/V7l2MV5cm+iyjoPN0gF0/I45jcaW X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(767451399110); X-Exchange-Antispam-Report-CFA-Test: BCL:0;PCL:0;RULEID:(6040470)(2401047)(5005006)(8121501046)(93006095)(93001095)(10201501046)(3002001)(3231023)(944501075)(6055026)(6041268)(20161123562045)(20161123564045)(20161123558120)(201703131423095)(201702281528075)(20161123555045)(201703061421075)(201703061406153)(20161123560045)(6072148)(201708071742011);SRVR:BN6PR12MB1140;BCL:0;PCL:0;RULEID:(100000803101)(100110400095);SRVR:BN6PR12MB1140; X-Forefront-PRVS: 054642504A X-Forefront-Antispam-Report: SFV:NSPM;SFS:(10009020)(6049001)(376002)(366004)(396003)(39860400002)(346002)(39380400002)(189003)(199004)(24454002)(6246003)(16576012)(3260700006)(8676002)(68736007)(47776003)(86362001)(83506002)(478600001)(65826007)(66066001)(65806001)(65956001)(53936002)(8936002)(7736002)(52146003)(90366009)(5660300001)(54906003)(97736004)(81166006)(81156014)(77096006)(72206003)(2486003)(58126008)(52116002)(3846002)(23676004)(25786009)(31686004)(316002)(16526018)(4326008)(2906002)(305945005)(6666003)(229853002)(50466002)(64126003)(6916009)(386003)(31696002)(53546011)(6486002)(36756003)(105586002)(230700001)(6116002)(76176011)(106356001)(2950100002);DIR:OUT;SFP:1101;SCL:1;SRVR:BN6PR12MB1140;H:[10.236.65.116];FPR:;SPF:None;PTR:InfoNoRecords;MX:1;A:1;LANG:en; X-Microsoft-Exchange-Diagnostics: =?utf-8?B?MTtCTjZQUjEyTUIxMTQwOzIzOlFvcE1vaitQM25QT3dBbExMRHptNmR0TFBw?= =?utf-8?B?a0JmYTZVNjVRZHV5UUd0blBPZmNVdHRCLzI2ekFiVG1kbkNoMlVmMGM0c0hr?= =?utf-8?B?TlZveHpKY0FSazlDVWNQZmQ3Z1RjZ2tWUDUvV1YyQTAyRDZoWjBWelBvRWpl?= =?utf-8?B?bTQ5dENLaU04M3krWm04cUxMN0h4am1WL3RhUE5oTDBBOVVPNllkYkxqWE5S?= =?utf-8?B?YW5kTTlLeFA5cE5jTjNoU1FLR0xNRzliZXRyc3g3K0VFNk1rTmVGd0pROWpN?= =?utf-8?B?NnRmK091eGJ3TnlVd01NMkFqZ1d1Rm90MGFXUEp6eWZvU0tVeDBJcVBZd0RZ?= =?utf-8?B?Zk91OFpyd0xocThEWndBYlo2bHlGSzRhdXgzRnRQamRZZWdxNUVpUUd2TlhX?= =?utf-8?B?TU1KSDBsWnRhVG45R3g5MjVGYVJrVG9xOGhHdDQybVdNOU1XbVZpMEU5QVl5?= =?utf-8?B?aThnYzZ6YUJuM29UeUNHdzJldE9iWVhPSWFJejcyVzBDNzIrZkliZkxvNWFW?= =?utf-8?B?Snl0a1djY3VrVXkvVGM3V3ZyK2RhOHhmL2hjUnl2R21SQTZr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GbeomwfvRqcG6G6n2S7s2yXJNPWgqkWobDyLft5mRdoIm++Xk5DQQ+/kLZpNzcUj2D4JcJAcYVmNrZlCJW0udHa6kYA8PEietboxeE=;24:Jm6/Cx1cMzdyRgvqN+DUB9l2m9nXnTWX5pnfHsM2kO/lLW4lPrK86xEul8NSdlbTmTrHr3ixbp2L3n5+FocH6/B8CMNnlo7bNZ3JInnm8Zs=;7:7lgOxXQ0Y/TWu6ESw5pcYVieXEe0YMMLVWeirYacmjl/ELNRn/VvP13VU2Fp4sJbomOW+pQdWrYxH7mz/TCbuBxSyv1F9ROYkC5+epuwj+sI9kS4F4pRERe7ldGvFDHrJZJz7nA1jM78+qe3RRWeeRK61gQLYiwx43tHgOraQS5fHCwv7fARTpOKx6CNfpvua+gAzxcqhij1JmCW1AVkD2jdeaUOfxBuBiArtwavECCNLxHmFiH8n7Zp2jmpAakL SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1;BN6PR12MB1140;20:WibuoKylPlpAl7pUiCCwdwNfr5VVn4MiKLZqpN16qCek3NV54LtgzchVxRyeOp+hbfDqXv1+CMxz4AVmCUOlrOyp0VSzxTWnpTkB24Ytua9X9j3yDLzxQoriPSVOXIDbdmyZtMysbJvO5tNPn/oCA2U5/Nog1/0IF7cOVBopwQP0MjKrP3i49BgLuisVdPU0oA0acOVmOl4xIZWYvGty3BoRh7xlmNNfZJBPh3IrSmpGGqe54Iuj8Ma8dxFXqhXg X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Jan 2018 23:18:29.8847 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 0a5f5780-adbf-4be7-bb2c-08d556ee218f X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-Transport-CrossTenantHeadersStamped: BN6PR12MB1140 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Return-Path: On 1/7/2018 10:28 AM, Borislav Petkov wrote: > On Thu, Dec 21, 2017 at 04:03:12PM -0600, Tom Lendacky wrote: >> In preparation for encrypting more than just the kernel during early >> boot processing, centralize the use of the PMD flag settings based >> on the type of mapping desired. When 4KB aligned encryption is added, >> this will allow either PTE flags or large page PMD flags to be used >> without requiring the caller to adjust. >> >> Signed-off-by: Tom Lendacky >> --- >> arch/x86/mm/mem_encrypt.c | 131 ++++++++++++++++++++++++++------------------- >> 1 file changed, 77 insertions(+), 54 deletions(-) >> >> diff --git a/arch/x86/mm/mem_encrypt.c b/arch/x86/mm/mem_encrypt.c >> index 5a20696..9b180f8 100644 >> --- a/arch/x86/mm/mem_encrypt.c >> +++ b/arch/x86/mm/mem_encrypt.c >> @@ -468,31 +468,40 @@ struct sme_populate_pgd_data { >> void *pgtable_area; >> pgd_t *pgd; >> >> - pmdval_t pmd_val; >> + pmdval_t pmd_flags; >> + unsigned long paddr; >> + >> unsigned long vaddr; >> + unsigned long vaddr_end; >> }; >> >> -static void __init sme_clear_pgd(pgd_t *pgd_base, unsigned long start, >> - unsigned long end) >> +static void __init sme_clear_pgd(struct sme_populate_pgd_data *ppd) >> { >> unsigned long pgd_start, pgd_end, pgd_size; >> pgd_t *pgd_p; >> >> - pgd_start = start & PGDIR_MASK; >> - pgd_end = end & PGDIR_MASK; >> + pgd_start = ppd->vaddr & PGDIR_MASK; >> + pgd_end = ppd->vaddr_end & PGDIR_MASK; >> >> pgd_size = (((pgd_end - pgd_start) / PGDIR_SIZE) + 1); >> pgd_size *= sizeof(pgd_t); > > This is a strange way of writing this. I'd expect: > > pgd_size = (((pgd_end - pgd_start) / PGDIR_SIZE) + 1) * sizeof(pgd_t); Yup, clearer as one line. I can fix that up. > >> >> - pgd_p = pgd_base + pgd_index(start); >> + pgd_p = ppd->pgd + pgd_index(ppd->vaddr); >> >> memset(pgd_p, 0, pgd_size); >> } >> >> -#define PGD_FLAGS _KERNPG_TABLE_NOENC >> -#define P4D_FLAGS _KERNPG_TABLE_NOENC >> -#define PUD_FLAGS _KERNPG_TABLE_NOENC >> -#define PMD_FLAGS (__PAGE_KERNEL_LARGE_EXEC & ~_PAGE_GLOBAL) >> +#define PGD_FLAGS _KERNPG_TABLE_NOENC >> +#define P4D_FLAGS _KERNPG_TABLE_NOENC >> +#define PUD_FLAGS _KERNPG_TABLE_NOENC >> + >> +#define PMD_FLAGS_LARGE (__PAGE_KERNEL_LARGE_EXEC & ~_PAGE_GLOBAL) >> + >> +#define PMD_FLAGS_DEC PMD_FLAGS_LARGE >> +#define PMD_FLAGS_DEC_WP ((PMD_FLAGS_DEC & ~_PAGE_CACHE_MASK) | \ >> + (_PAGE_PAT | _PAGE_PWT)) >> + >> +#define PMD_FLAGS_ENC (PMD_FLAGS_LARGE | _PAGE_ENC) >> >> static void __init sme_populate_pgd_large(struct sme_populate_pgd_data *ppd) >> { >> @@ -561,7 +570,36 @@ static void __init sme_populate_pgd_large(struct sme_populate_pgd_data *ppd) >> >> pmd_p += pmd_index(ppd->vaddr); >> if (!native_pmd_val(*pmd_p) || !(native_pmd_val(*pmd_p) & _PAGE_PSE)) >> - native_set_pmd(pmd_p, native_make_pmd(ppd->pmd_val)); >> + native_set_pmd(pmd_p, >> + native_make_pmd(ppd->paddr | ppd->pmd_flags)); > > Never do those ugly line breaks. Just let it stick out. Will do. Thanks, Tom > > Otherwise, sme_encrypt_kernel() is starting to look quite readable :) >