Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753921AbdLHOhx (ORCPT ); Fri, 8 Dec 2017 09:37:53 -0500 Received: from mail-sn1nam02on0053.outbound.protection.outlook.com ([104.47.36.53]:30652 "EHLO NAM02-SN1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1753562AbdLHOhv (ORCPT ); Fri, 8 Dec 2017 09:37:51 -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" Cc: "Kirill A. Shutemov" , Thomas Gleixner , Ingo Molnar , "H. Peter Anvin" , 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> <20171204145755.6xu2w6a6og56rq5v@node.shutemov.name> <20171204163445.qt5dqcrrkilnhowz@black.fi.intel.com> <20171204173931.pjnmfdutys7cnesx@black.fi.intel.com> From: Tom Lendacky Message-ID: <55400fe3-a605-b86f-e14c-c5dd08738fd7@amd.com> Date: Fri, 8 Dec 2017 08:37:43 -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: <20171204173931.pjnmfdutys7cnesx@black.fi.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: MWHPR2201CA0093.namprd22.prod.outlook.com (10.174.103.46) To CY4PR12MB1141.namprd12.prod.outlook.com (10.168.163.149) X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: f2339ed4-a626-431a-9294-08d53e494155 X-MS-Office365-Filtering-HT: Tenant X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:(4534020)(4602075)(4627115)(201703031133081)(201702281549075)(5600026)(4604075)(48565401081)(2017052603307);SRVR:CY4PR12MB1141; X-Microsoft-Exchange-Diagnostics: 1;CY4PR12MB1141;3:Iiqf5vEy5E7WsUwO3TDpwH3AEGDOKNCz25vp4LuexBDcgdmwlGU0G1ULqtfKa52j596RFPysxSseOnCw7W7b4hE7qv5k1gVGEkbMRHoa8dZgxk9iF1kVAxQjcJTEj3qR+zWp9HINqiGVq8cW5ot0kR1lO8mmMfd+nHFQmA840BtMZ21uhp1IqLn5UthLuGEN/4aFt4RXQy+yTeyptoxaCUj3B4soXxE//DbfPeSsMxFyobj0XN3WrObWzT9nxE+U;25:+oTXv1cddUDYUJCBbYSLBzCJNMG6yc0jhL+2heIp8DmV4cKNGfdNFWTDeahN34fRXpDK49xFRiudaxyQEZ+7V8U+KD5uyq1z63pzuQBVcvpOAvwFYnOorWfal+wkGOI0hQ+qXbXW+eQ+gZjE1GGxKRhgcH4TVWRc0Wte3t6gtWbWs9XAdscm0moCXT5eK2JDQJaccMr7OAMv+Aewg8sos+GVqpPun+I+5DNnkKEAfKE4iaGiXnynZpgNOf8Y7rmlo4XlLgsBdGboqSi/SABxdlT02yL100wm5oBG7xf6YE4fIWAtv2VQy6H9/K8j3WXdlzjX7JcYsXuNVW8ksqkksQ==;31:YokLnsFjyiW7YKocSn1ZbKbDpBF3dVQcMac+3eSVw/1Y6M2hIm/emND1tuGqsd11BkfpcvMolOUQWDVBOCNz5dL8nsHMOAw0AVP4dKqO3r/Y7ypllUgUP6gtyp71dge8L6imnsq8gMS7PiegS4Z1fhx8dQGQedK0h/1htEQR6TcWIcll5bfcCy7x0jZUAxiiZO6fqOUdba02Psi+YZNldodhhyzl8sbukwKjhhNuOhY= X-MS-TrafficTypeDiagnostic: CY4PR12MB1141: X-Microsoft-Exchange-Diagnostics: 1;CY4PR12MB1141;20:NMhjJokWjuJSKHi8KB4AoYYTfB1hItIdlPjsY7PJE+PeLJ2FYo17IfJFi/bJkQnRbyzyRfoPxtCmK3WDCk7B/ditewx0kDwc4D/qUF/DwVrFqweUh44g5fhQ8KjblOuxL0XjZrfnlCh4FXlgwW77kT5MtW7FYOi1kXb/d4PKDL4c7zy+tRA0gmr4XMxW30KfmJc24FfY0enkEHZ7w+DlHX+U6f/pkjAo4iIGkplSIGaG3v4pkY8EebHfqyjsvmv4B02M8ir+lnkU60S6YlG26nN6VQ51hULxmAGYhoBA80eBCGOliMLIESPBG/NWsi9w3se8K9PgSTIr+6o1vVXwXlJEjwqS49iKhUTLh9MQKhRARpTDgFFBjnigBCm7UzTLUpsqsUR28XOCuVONGCbFyVZBZfCnsjTWdMusT4jhHHQxsAy+kS6Ic3GnqHpsiycYMKXJk1oGff8WwU/DHScmjRaSzPKzbWfldAA1RdgOvm5D3xJ+Eb1GIjlfRQpJpXAA;4:Gk1KAypD20MbNwkHr/aUbSOww+/WiX/cbGisanP6QKezJjKTzdZ3EI3Smb0R0brymo3dp/gy8skBOhTjSfaNF4Wwr5NBz1F3LzZPVSjfz/9qET/pglseZPM86Qcq5vcQZSz1qJKQy2KVfMNJUUixpfOeh6pqTxw9/RLjcuMqHDHmDu+09rhe/mYc8gkFcSJnJP0HcOXkAWd6sBaFjqsubJTL3THWHsyxYwv2J/y9OPCF86J53aMnlWKB7j2DbyEK/AJmVRVNgjZCH5nMnx2y8A== X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:; X-Exchange-Antispam-Report-CFA-Test: BCL:0;PCL:0;RULEID:(6040450)(2401047)(5005006)(8121501046)(3231022)(10201501046)(3002001)(93006095)(93001095)(6055026)(6041248)(20161123558100)(20161123562025)(20161123560025)(20161123555025)(201703131423075)(201702281528075)(201703061421075)(201703061406153)(20161123564025)(6072148)(201708071742011);SRVR:CY4PR12MB1141;BCL:0;PCL:0;RULEID:(100000803101)(100110400095);SRVR:CY4PR12MB1141; X-Forefront-PRVS: 0515208626 X-Forefront-Antispam-Report: SFV:NSPM;SFS:(10009020)(6049001)(39860400002)(346002)(376002)(366004)(24454002)(199004)(189003)(31686004)(83506002)(33646002)(25786009)(67846002)(6116002)(478600001)(575784001)(3260700006)(8936002)(86362001)(23676004)(68736007)(64126003)(31696002)(65806001)(66066001)(65956001)(6666003)(230700001)(2950100002)(5660300001)(53546010)(16576012)(47776003)(93886005)(50466002)(6916009)(8676002)(90366009)(16526018)(316002)(36756003)(6486002)(106356001)(81156014)(52116002)(3846002)(6246003)(65826007)(105586002)(97736004)(54906003)(81166006)(4326008)(2906002)(2486003)(7736002)(305945005)(76176011)(52146003)(53936002)(72206003)(229853002)(77096006)(58126008);DIR:OUT;SFP:1101;SCL:1;SRVR:CY4PR12MB1141;H:[10.236.65.116];FPR:;SPF:None;PTR:InfoNoRecords;A:1;MX:1;LANG:en; X-Microsoft-Exchange-Diagnostics: =?utf-8?B?MTtDWTRQUjEyTUIxMTQxOzIzOmxLckxUclQrQXFZajNjQUhESmxmei9hamRV?= =?utf-8?B?Q2FpZDV2Vkd2UWxpTncxRDAvV2M0endTZ01ia3BCamx2VUR3Tk9DNFIydE5Y?= =?utf-8?B?UjlNaTdBY0N0M0w2OVlzZmpIbjVNdnhlWUMxOEtWL2ZsOWl0TFpNdEJWTGhT?= =?utf-8?B?VmhJb3lURTB5S0xWTlV3WWFZNmRRTXZ1dVoxYkwwZVJKRW5pbzlHWkloalRD?= =?utf-8?B?eW9NTVBFMDBYOHVKa2V5anNqeUVLZWpOQTlxUWdiTUJmSTlFbGxydFJPblJS?= =?utf-8?B?bEcxNTlPSXg4K2ZPOStCbGJIeUJhL2o2SmJiSWlFdGNtZDc2OXdWSmdwd2I3?= =?utf-8?B?Y0pwY2pNdjlDeWdvR1Jjbk4wK2xESmdQSi8zSS9rQ01ZTU9ZOWU2V1VLSW41?= =?utf-8?B?cEU3WGJZVDBKcG9CVU8yRVpORElxQ3U4Y2cvbmN4angrc0h3b0o1Ri8zNXBI?= =?utf-8?B?ZVZkRXVJSG1zMG9wUHVJdHFNdkU4T2FrcGVGeXVq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9FaPakUgDf7Qpi4Ghf3w2Mqx1Su9egP2I=;5:DTIrXrsb6w1hnGpiO9mcE9FLHJq3d3kfJzA4tFgGaCgRXJPBPxdRsvWn0B0KjadXwYHRKEsFqCF4q97kwmahUSdbOYuw86k31uxNYfNDpVNXSYodmzuu9qw9nHkqHKeZC07WR2r5L82BbqiDx6nTUhaXreIeRbu4OigwCxxrlB4=;24:vld5itD3apjOg3py0HzNsycDqZ//xiLnC3lkUP9E4XrHhT2fwkuHCvd2fYIN+7xMdnolKA5kiVXV8T+aqPCv8nePF0749nGA1TfMgDh2G58=;7:Ey80NQpzVwE/awKnKh0rAuxIhkzKxU8DBtPiOCnsikbSuNzzONZhmplPZLc0DQeyXRWbolZ857yft5lP/Rc2z0VLVmGk221Lp0RC+2EzW+0HsAVKfbs8YYoKExBKhkXbNxI8kv6JECun1eh60iku/sbQ4TmarvvURG8jecOuf3I4AthGkC3cxEwbLLzCv3EoiV+Kxl5RZkL/GCpbPkjposiz/dXC8Ro6O0w9kMcv1Lz2YYZ2N1u3TbIsG5DAZV3+ SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1;CY4PR12MB1141;20:2HFPjqp5M3Y+TMTr87oh8owktVbct2RGBHx4D357MqEB2WMBDEIUSRetnzyZqRMlfFUMLRWS5Tui0SoVLOq8Unl+OkyH/2Ch5hKa0vjqNulGeqG3Y/v43T6VFimMoACngzEgXJgzhJaSYKeJpPUtd6lNEVx+7uMaTOdy6Hrhxm0APJeVBWh1PHP6UZC+c0oS+xhjAfY8NPgb5BG/Qa7oZbU00CueQvTzYgYXA0lCF6+ROrhc+0vEB/Ey5oslFfQ/ X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Dec 2017 14:37:48.0892 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: f2339ed4-a626-431a-9294-08d53e494155 X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY4PR12MB1141 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 4714 Lines: 122 On 12/4/2017 11:39 AM, Kirill A. Shutemov wrote: > On Mon, Dec 04, 2017 at 04:34:45PM +0000, Kirill A. Shutemov wrote: >> On Mon, Dec 04, 2017 at 04:00:26PM +0000, Tom Lendacky wrote: >>> On 12/4/2017 8:57 AM, Kirill A. Shutemov wrote: >>>> On Mon, Dec 04, 2017 at 08:19:11AM -0600, Tom Lendacky wrote: >>>>> 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. >>>> >>>> Wat? Virtual address is virtual address. p?d_offset() doesn't care about >>>> what mapping you're using. >>> >>> Yes it does. For example, pmd_offset() issues a pud_page_addr() call, >>> which does a __va() returning a non-identity mapped address (0xffff88...). >>> Only identity mapped virtual addresses have been setup at this point, so >>> the use of that virtual address panics the kernel. >> >> Stupid me. You are right. >> >> What about something like this: > > sme_pgtable_calc() also looks unnecessary complex. I have no objections to improving this (although I just submitted a patch that modifies this area, so this will have to be updated now). > > Any objections on this: > > diff --git a/arch/x86/mm/mem_encrypt.c b/arch/x86/mm/mem_encrypt.c > index 65e0d68f863f..59b7d7ba9b37 100644 > --- a/arch/x86/mm/mem_encrypt.c > +++ b/arch/x86/mm/mem_encrypt.c > @@ -548,8 +548,7 @@ static void __init *sme_populate_pgd(pgd_t *pgd_base, void *pgtable_area, > > static unsigned long __init sme_pgtable_calc(unsigned long len) > { > - unsigned long p4d_size, pud_size, pmd_size; > - unsigned long total; > + unsigned long entries, tables; > > /* > * Perform a relatively simplistic calculation of the pagetable > @@ -559,41 +558,25 @@ static unsigned long __init sme_pgtable_calc(unsigned long len) > * mappings. Incrementing the count for each covers the case where > * the addresses cross entries. > */ > - if (IS_ENABLED(CONFIG_X86_5LEVEL)) { > - p4d_size = (ALIGN(len, PGDIR_SIZE) / PGDIR_SIZE) + 1; > - p4d_size *= sizeof(p4d_t) * PTRS_PER_P4D; > - pud_size = (ALIGN(len, P4D_SIZE) / P4D_SIZE) + 1; > - pud_size *= sizeof(pud_t) * PTRS_PER_PUD; > - } else { > - p4d_size = 0; > - pud_size = (ALIGN(len, PGDIR_SIZE) / PGDIR_SIZE) + 1; > - pud_size *= sizeof(pud_t) * PTRS_PER_PUD; > - } > - pmd_size = (ALIGN(len, PUD_SIZE) / PUD_SIZE) + 1; > - pmd_size *= sizeof(pmd_t) * PTRS_PER_PMD; > > - total = p4d_size + pud_size + pmd_size; > + entries = (DIV_ROUND_UP(len, PGDIR_SIZE) + 1) * PAGE_SIZE; I stayed away from using PAGE_SIZE directly because other areas/files used the sizeof() * PTRS_PER_ and I was trying to be consistent. Not that the size of a page table is ever likely to change, but maybe defining a macro (similar to the one in mm/pgtable.c) would be best rather than using PAGE_SIZE directly. Not required, just my opinion. > + if (PTRS_PER_P4D > 1) > + entries += (DIV_ROUND_UP(len, P4D_SIZE) + 1) * PAGE_SIZE; > + entries += (DIV_ROUND_UP(len, PUD_SIZE) + 1) * PAGE_SIZE; > + entries += (DIV_ROUND_UP(len, PMD_SIZE) + 1) * PAGE_SIZE; > > /* > * Now calculate the added pagetable structures needed to populate > * the new pagetables. > */ > - if (IS_ENABLED(CONFIG_X86_5LEVEL)) { > - p4d_size = ALIGN(total, PGDIR_SIZE) / PGDIR_SIZE; > - p4d_size *= sizeof(p4d_t) * PTRS_PER_P4D; > - pud_size = ALIGN(total, P4D_SIZE) / P4D_SIZE; > - pud_size *= sizeof(pud_t) * PTRS_PER_PUD; > - } else { > - p4d_size = 0; > - pud_size = ALIGN(total, PGDIR_SIZE) / PGDIR_SIZE; > - pud_size *= sizeof(pud_t) * PTRS_PER_PUD; > - } > - pmd_size = ALIGN(total, PUD_SIZE) / PUD_SIZE; > - pmd_size *= sizeof(pmd_t) * PTRS_PER_PMD; > > - total += p4d_size + pud_size + pmd_size; > + tables = DIV_ROUND_UP(entries, PGDIR_SIZE) * PAGE_SIZE; > + if (PTRS_PER_P4D > 1) > + tables += DIV_ROUND_UP(entries, P4D_SIZE) * PAGE_SIZE; > + tables += DIV_ROUND_UP(entries, PUD_SIZE) * PAGE_SIZE; > + tables += DIV_ROUND_UP(entries, PMD_SIZE) * PAGE_SIZE; > > - return total; > + return entries + tables; > } It all looks reasonable, but I won't be able to test for the next few days, though. Thanks, Tom > > void __init sme_encrypt_kernel(void) >