Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752987AbdLDOT1 (ORCPT ); Mon, 4 Dec 2017 09:19:27 -0500 Received: from mail-bn3nam01on0045.outbound.protection.outlook.com ([104.47.33.45]:49903 "EHLO NAM01-BN3-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1751990AbdLDOTZ (ORCPT ); Mon, 4 Dec 2017 09:19:25 -0500 Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=Thomas.Lendacky@amd.com; Subject: Re: [PATCH] x86/mm: Rewrite sme_populate_pgd() in a more sensible way To: "Kirill A. Shutemov" , Thomas Gleixner , Ingo Molnar , "H. Peter Anvin" Cc: x86@kernel.org, Borislav Petkov , Brijesh Singh , linux-mm@kvack.org, linux-kernel@vger.kernel.org References: <20171204112323.47019-1-kirill.shutemov@linux.intel.com> From: Tom Lendacky Message-ID: Date: Mon, 4 Dec 2017 08:19:11 -0600 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.5.0 MIME-Version: 1.0 In-Reply-To: <20171204112323.47019-1-kirill.shutemov@linux.intel.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit X-Originating-IP: [165.204.78.1] X-ClientProxiedBy: DM5PR1101CA0019.namprd11.prod.outlook.com (10.174.246.29) To BN6PR12MB1140.namprd12.prod.outlook.com (10.168.226.142) X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: a0b99c85-baee-418c-ad1c-08d53b2203f3 X-MS-Office365-Filtering-HT: Tenant X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:(5600026)(4604075)(4534020)(4602075)(4627115)(201703031133081)(201702281549075)(48565401081)(2017052603286);SRVR:BN6PR12MB1140; X-Microsoft-Exchange-Diagnostics: 1;BN6PR12MB1140;3:f0CjigAZ7hJoErjydvbA7Gg/mQgmP2ZJ4JM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6hEC7fH8IBACymO5m72UO3PufeFIXZMtfduPCgNjsP1CtZj/OmIkSmsFKFPFYjNCz9XZFD11nPOPKPhgaO4FpxZkIAITdU4nNMa5/Jf2VL238Rgy1/0yDeaSub7Ke2doVoaIZc0qrPirnm2IICftDRyfCEBcrsdxvK4928b5LfpRTFLtoP1ac41va09KaWS6zSYSQfHkzlweDS5Z25deXHD9r55Q0KoB4JekjeuFCBbdDNizP5bhsRD4vSTOh6F8m5qaCbTeOSsMaNvS583mmVRkx9g9GOQ8QY8NRo2dSUxvBlqP6XJD1totp;4:aXEblJkl+AvyTYOKhxLGBhwsPbelG2+/lai+n0OKcvoxyZmr1FZjoWqTMHYH7gKQ5qowGcbR5k/w6+i7wwmlIqgs1g9Py99MjVv7EuSb5HgzppAF9BLtXH6kg3EqGFd9NuqiV76z8hnBMCrA7dfZjlh0Vznjniy94qwoeJKRnjBbhp+xEWB/NHEA0cL0uG3/JkveOZ0T2KkvwgKDqetmi3y+ejA6ZROvVgMxnWMmqdyc/Yoqh6TQEtbOHhVizix+gJ8m/IQQ/MiR+6mqKiwMIDCCZrouvpy9FYba9ScuKU/+kN6G5n8CjG7NyWK8z8Gx X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(228905959029699); X-Exchange-Antispam-Report-CFA-Test: BCL:0;PCL:0;RULEID:(6040450)(2401047)(8121501046)(5005006)(3231022)(3002001)(93006095)(93001095)(10201501046)(6055026)(6041248)(20161123562025)(20161123560025)(20161123564025)(201703131423075)(201702281528075)(201703061421075)(201703061406153)(20161123558100)(20161123555025)(6072148)(201708071742011);SRVR:BN6PR12MB1140;BCL:0;PCL:0;RULEID:(100000803101)(100110400095);SRVR:BN6PR12MB1140; X-Forefront-PRVS: 051158ECBB X-Forefront-Antispam-Report: SFV:NSPM;SFS:(10009020)(6049001)(6009001)(366004)(346002)(376002)(39860400002)(189002)(24454002)(199003)(2486003)(86362001)(54356011)(16576012)(105586002)(52116002)(23676004)(52146003)(76176011)(106356001)(36756003)(31686004)(33646002)(77096006)(58126008)(54906003)(110136005)(6486002)(230700001)(90366009)(97736004)(16526018)(6246003)(229853002)(4326008)(65956001)(50466002)(65806001)(66066001)(67846002)(189998001)(316002)(53936002)(47776003)(83506002)(7736002)(53546010)(5660300001)(25786009)(65826007)(8676002)(81156014)(81166006)(101416001)(8936002)(2950100002)(3846002)(6116002)(2906002)(72206003)(305945005)(6666003)(3260700006)(478600001)(68736007)(64126003)(31696002);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?MTtCTjZQUjEyTUIxMTQwOzIzOmZ1bGd2WHB0cFdYb0RySnRGWERHTzlDRGZs?= =?utf-8?B?SHoxcVVHRndFZ0hML0VFRUlCbkI2MW4wZXlkalBpeTQ4d2w3VU8vRlJ0bDlB?= =?utf-8?B?SVR0U2RUZHZXRndzU090dlVBVkMyaG5KTEltYWZIN3RMOGpIRWxPMFVac2xy?= =?utf-8?B?MVVxR2MxVUJFZk9BMFJKU3hyNTN4OXBKZ0dvdHFrWVIzTU9ZWk9haDBobktG?= =?utf-8?B?OGQ4c014bE4vKzJqM1dnY2NhUm9vQ2s5UTNMa0NMK2gvS3pnemZpWGM2eUR3?= =?utf-8?B?WHFQd2trK1FhNXR4U1NLckM5bGR0ajR1SXNWeXB2UzVENlUxVitwMDhHZ1JV?= =?utf-8?B?bjJ0R2lWdjdjdDloNDM2Z1RSc2YxZ2p6YWVRZG12MWt4TzhxSEN4Q1NWME1n?= =?utf-8?B?dENtZ1NxZStKWGlJalpmOEVwNEZOSHl3Ynh5T2o2K2RZUnpUTkZLNWswdllE?= =?utf-8?B?ZmtYU2xYVHprdzNXdkNuNHdaSzg4emF4dlVQUTJXbTBjTnZSMmg1Zmp2Y1Bt?= =?utf-8?B?N0lTdFhSWUg2WE9CYkhucVo2aHI2OXIrcFdzeHpicl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T49jMANFYHseW1Tq1HNqrDsjq40brxzRIhUNgHt2ql/ghQ=;5:0Z4HIvYeOmXSLfeEQY5OJDB5Js3jcoSNBPkV69WCGh+DK3ca8XFr7+DzTDKHxjBn6YVduE1i/tx5NgXH66egFvOE2ucESYOVJ6UZcL2Nx8sK1ttjYhQMuNWINMefeezivceiWwJVJ+nH222GPfAsGMO+4oGawMfuE+taXcr/Jes=;24:LgR2hBlqsXnzAufG/6cjCcoEfoEGfSm5QoCrEwPFXHfze9WIQclEwEGuxBu4BZYniKGuywezbBdcjLnZoQFXLQ+iJUA7hmTvCgNgmqAABVI=;7:y1qIu33k+qMXbAZmeBEZd3rqdJu6LLDFZPbgbdeAp6asJk26dFLswhlylRnsRDuJlVOlgC4AotISyZUvCaHTa29Vh34EJ9GPsfT9pEAVFOFFcq8OD9N1NC/KBWw2dpcRXLtkiVKIV/UIR7CP5gM32wVSxz7j/GnHctOMbjdVZnXMGmXnMGFPNiCJuaHKkScv48gMiGPg+5+7XEabbBf7RlPGjtY3S1A//EZ8dCZTvZmRdIOViyVAqw/Vlvd8Nh6g SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1;BN6PR12MB1140;20:+K1nqmqmd+L/9vyN9PGBtzSIUahVEk8UepL530LI4Im3RULoenkrrJaUPtipvgu0GxmtOiY6mWd05F8NLh2P/BmaRZwY4NnP+AtcFr11sbGd7V2WYSLIxlGrgZztjUAMTgRoz6gF/gle9ZV7V1DpsNBrUgKzoUA7AHS2/kVBin1NIDT4+agOXQCcUDkAuIrz+9cko5vsLZ1py5muCbtrGU0rUcO8tmZPKyqQxDxHlVRnnwhOYD6oS+o9QiQyFpO3 X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 04 Dec 2017 14:19:21.6052 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: a0b99c85-baee-418c-ad1c-08d53b2203f3 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 Content-Length: 4568 Lines: 141 On 12/4/2017 5:23 AM, Kirill A. Shutemov wrote: > sme_populate_pgd() open-codes a lot of things that are not needed to be > open-coded. > > Let's rewrite it in a more stream-lined way. > > This would also buy us boot-time switching between support between > paging modes, when rest of the pieces will be upstream. Hi Kirill, Unfortunately, some of these can't be changed. The use of p4d_offset(), pud_offset(), etc., use non-identity mapped virtual addresses which cause failures at this point of the boot process. Also, calls such as __p4d(), __pud(), etc., are part of the paravirt support and can't be used yet, either. I can take a closer look at some of the others (p*d_none() and p*d_large()) which make use of the native_ macros, but my worry would be that these get changed in the future to the non-native calls and then boot failures occur. Thanks, Tom > > Signed-off-by: Kirill A. Shutemov > --- > > The patch is only build tested. I don't have hardware. Tom, could you give it a try? > > --- > arch/x86/mm/mem_encrypt.c | 89 +++++++++++++++-------------------------------- > 1 file changed, 29 insertions(+), 60 deletions(-) > > diff --git a/arch/x86/mm/mem_encrypt.c b/arch/x86/mm/mem_encrypt.c > index d9a9e9fc75dd..16038f7472ca 100644 > --- a/arch/x86/mm/mem_encrypt.c > +++ b/arch/x86/mm/mem_encrypt.c > @@ -489,73 +489,42 @@ static void __init sme_clear_pgd(pgd_t *pgd_base, unsigned long start, > static void __init *sme_populate_pgd(pgd_t *pgd_base, void *pgtable_area, > unsigned long vaddr, pmdval_t pmd_val) > { > - pgd_t *pgd_p; > - p4d_t *p4d_p; > - pud_t *pud_p; > - pmd_t *pmd_p; > - > - pgd_p = pgd_base + pgd_index(vaddr); > - if (native_pgd_val(*pgd_p)) { > - if (IS_ENABLED(CONFIG_X86_5LEVEL)) > - p4d_p = (p4d_t *)(native_pgd_val(*pgd_p) & ~PTE_FLAGS_MASK); > - else > - pud_p = (pud_t *)(native_pgd_val(*pgd_p) & ~PTE_FLAGS_MASK); > - } else { > - pgd_t pgd; > - > - if (IS_ENABLED(CONFIG_X86_5LEVEL)) { > - p4d_p = pgtable_area; > - memset(p4d_p, 0, sizeof(*p4d_p) * PTRS_PER_P4D); > - pgtable_area += sizeof(*p4d_p) * PTRS_PER_P4D; > - > - pgd = native_make_pgd((pgdval_t)p4d_p + PGD_FLAGS); > - } else { > - pud_p = pgtable_area; > - memset(pud_p, 0, sizeof(*pud_p) * PTRS_PER_PUD); > - pgtable_area += sizeof(*pud_p) * PTRS_PER_PUD; > - > - pgd = native_make_pgd((pgdval_t)pud_p + PGD_FLAGS); > - } > - native_set_pgd(pgd_p, pgd); > + pgd_t *pgd; > + p4d_t *p4d; > + pud_t *pud; > + pmd_t *pmd; > + > + pgd = pgd_base + pgd_index(vaddr); > + if (pgd_none(*pgd)) { > + p4d = pgtable_area; > + memset(p4d, 0, sizeof(*p4d) * PTRS_PER_P4D); > + pgtable_area += sizeof(*p4d) * PTRS_PER_P4D; > + native_set_pgd(pgd, __pgd(PGD_FLAGS | __pa(p4d))); > } > > - if (IS_ENABLED(CONFIG_X86_5LEVEL)) { > - p4d_p += p4d_index(vaddr); > - if (native_p4d_val(*p4d_p)) { > - pud_p = (pud_t *)(native_p4d_val(*p4d_p) & ~PTE_FLAGS_MASK); > - } else { > - p4d_t p4d; > - > - pud_p = pgtable_area; > - memset(pud_p, 0, sizeof(*pud_p) * PTRS_PER_PUD); > - pgtable_area += sizeof(*pud_p) * PTRS_PER_PUD; > - > - p4d = native_make_p4d((pudval_t)pud_p + P4D_FLAGS); > - native_set_p4d(p4d_p, p4d); > - } > + p4d = p4d_offset(pgd, vaddr); > + if (p4d_none(*p4d)) { > + pud = pgtable_area; > + memset(pud, 0, sizeof(*pud) * PTRS_PER_PUD); > + pgtable_area += sizeof(*pud) * PTRS_PER_PUD; > + native_set_p4d(p4d, __p4d(P4D_FLAGS | __pa(pud))); > } > > - pud_p += pud_index(vaddr); > - if (native_pud_val(*pud_p)) { > - if (native_pud_val(*pud_p) & _PAGE_PSE) > - goto out; > - > - pmd_p = (pmd_t *)(native_pud_val(*pud_p) & ~PTE_FLAGS_MASK); > - } else { > - pud_t pud; > - > - pmd_p = pgtable_area; > - memset(pmd_p, 0, sizeof(*pmd_p) * PTRS_PER_PMD); > - pgtable_area += sizeof(*pmd_p) * PTRS_PER_PMD; > - > - pud = native_make_pud((pmdval_t)pmd_p + PUD_FLAGS); > - native_set_pud(pud_p, pud); > + pud = pud_offset(p4d, vaddr); > + if (pud_none(*pud)) { > + pmd = pgtable_area; > + memset(pmd, 0, sizeof(*pmd) * PTRS_PER_PMD); > + pgtable_area += sizeof(*pmd) * PTRS_PER_PMD; > + native_set_pud(pud, __pud(PUD_FLAGS | __pa(pmd))); > } > + if (pud_large(*pud)) > + goto out; > > - pmd_p += pmd_index(vaddr); > - if (!native_pmd_val(*pmd_p) || !(native_pmd_val(*pmd_p) & _PAGE_PSE)) > - native_set_pmd(pmd_p, native_make_pmd(pmd_val)); > + pmd = pmd_offset(pud, vaddr); > + if (pmd_large(*pmd)) > + goto out; > > + native_set_pmd(pmd, native_make_pmd(pmd_val)); > out: > return pgtable_area; > } >