From patchwork Thu Mar 2 15:13:32 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brijesh Singh X-Patchwork-Id: 9600441 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 D491C60414 for ; Thu, 2 Mar 2017 15:31:11 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C471E285A1 for ; Thu, 2 Mar 2017 15:31:11 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id B896C285B3; Thu, 2 Mar 2017 15:31:11 +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 54B35285A1 for ; Thu, 2 Mar 2017 15:31:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752497AbdCBPbG (ORCPT ); Thu, 2 Mar 2017 10:31:06 -0500 Received: from mail-dm3nam03on0078.outbound.protection.outlook.com ([104.47.41.78]:24608 "EHLO NAM03-DM3-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1753064AbdCBP37 (ORCPT ); Thu, 2 Mar 2017 10:29:59 -0500 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=CyRSsFXiYIxLHk7bFPEncQBZmVsVAblibXKY4oQwsdo=; b=3U0TcsbCzwvo6q16EX6uqP7/l8QX/FjxPUiC9fCwqKpcanqD8lKvdm5Wv4/KSO3Ccu4Or8HacnnSb1GAiM8t+/JIMhPifunzHW0cOFAk+Tc/cpeDN3RtQyPLX9X2wntIG1mNiEkAuHczFkiGsd11sXcVg4pU0LN6k2sfb2e3Oy8= 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 [127.0.1.1] (165.204.77.1) by CY4PR12MB1606.namprd12.prod.outlook.com (10.172.72.15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P384) id 15.1.933.12; Thu, 2 Mar 2017 15:13:42 +0000 Subject: [RFC PATCH v2 08/32] x86: Use PAGE_KERNEL protection for ioremap of memory page From: Brijesh Singh To: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Date: Thu, 2 Mar 2017 10:13:32 -0500 Message-ID: <148846761276.2349.4899767672892365544.stgit@brijesh-build-machine> In-Reply-To: <148846752022.2349.13667498174822419498.stgit@brijesh-build-machine> References: <148846752022.2349.13667498174822419498.stgit@brijesh-build-machine> User-Agent: StGit/0.17.1-dirty MIME-Version: 1.0 X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: BN6PR03CA0070.namprd03.prod.outlook.com (10.173.137.32) To CY4PR12MB1606.namprd12.prod.outlook.com (10.172.72.15) X-MS-Office365-Filtering-Correlation-Id: 41dc9287-9bdc-45c6-4980-08d4617eba0f X-MS-Office365-Filtering-HT: Tenant X-Microsoft-Antispam: UriScan:; BCL:0; PCL:0; RULEID:(22001)(48565401081); SRVR:CY4PR12MB1606; X-Microsoft-Exchange-Diagnostics: 1; CY4PR12MB1606; 3:SjMe8A4VX2At7jz2ro2YKMsl1xvmHeO40MZE58q5E153YoHJtxMdl04Sg1V/6LgLNdoF+gPBLSQDasr3JWgYh7Xcr84rgdCPr4aJIGdpBY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5+dKFNszrEm39Szd7T77Wx00LYuYQuUH04IINXGj7hsozX/jdlk3tqh+kU1PJ0M+suskvAQJ622Gr3D8R/MpjmUY64y6YPtrjVbS0QeTYxjtMAqdiMdmkTR/miw212xJEvvGxERJN05SIh97EW43eTMqd9P0nFW+y7FEmtS1S2soKmDDUewgXzLy8X5GUxii/MDMKjJVBD2mUu8dZZfZIEUAKEWqS+ENLzCuS5DMF2+GBCCgagsWUeFAPLafb+GbQt2IcxfIIHxwvbSreVY417nBs8oY+4i0K3oo6+LUZYClWVz1kJ+jwTAyOs+jSXzs696SFlujkCncMJGIGSh/wuT2eD3Z4PfWrGSQXSg1mSdFsykupEYZD0FhU2qkFDdbiys X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(767451399110); X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(6040375)(601004)(2401047)(5005006)(8121501046)(10201501046)(3002001)(6055026)(6041248)(20161123560025)(20161123555025)(20161123562025)(20161123564025)(20161123558025)(6072148); SRVR:CY4PR12MB1606; BCL:0; PCL:0; RULEID:; SRVR:CY4PR12MB1606; X-Microsoft-Exchange-Diagnostics: 1; CY4PR12MB1606; 4:1pM6y7WGxuIP5DY1yclP9QWGpJPmbTXF5upgUEWlKWBa1go9eP2WhjJPfaE9OgDnY6dzt8wh7Gpn0wCf2FCBctELFFNQzRfMCvNhEBuq85pjC61neqGGTXL6AhHnz+fe2N4mhQ2D1/OSYEXpX8UA5CbXaWJg3mMn6kzD4Zczt9Dj+satqS+SeedKGCrvUv8dzZBfAWH2HoSFmIetgrUX+QRtHmYUHBBK/vzwEk0XSNci6VK1Pr0g+nrtU8PeZqcIH4tiqOEiSgOQLxgYp5Pruamo5uzWEsWfEYULFFYM0NS4HZnBdiE0ofi5Ipt8h+SQpuLWvCAYP/PJw+lnYJStZUAPkDkWlHKK5CSoOG7QQE9tPfsBdsKBkGPBBnLk8KabdPoQKJV2VrhM6O+QykekJC1UjPEGhlomD7bM1WyZXO08WJT70bpOfHh4qUuJhDA7CnnyOEJOBaQ0l4MxzEFDs79OQNLjd64se3aGJVmEALsJ1aiVsR3WI6MlcMYfSE5vNjQ2cvJqJRssor7SmFOSH7BtmlNNxGTGLxdlDBO60G7+CsAWggrWk68mBx+9YYeyjvH43Cjff2Eum8bUF3Jcjj2rXBQs7m/Dr0tl/5KxMlBTvIdk+V4fyj6kKoJtHd5HKdMICkBAuC3Kgssa6WwdrxDH2h/mbB1te+nP4N3xU6c= X-Forefront-PRVS: 023495660C X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10009020)(4630300001)(6049001)(6009001)(7916002)(39410400002)(39450400003)(39840400002)(39850400002)(77096006)(6486002)(8676002)(81166006)(7736002)(1191002)(90366009)(9686003)(54356999)(76176999)(25786008)(92566002)(103116003)(6116002)(50986999)(50466002)(6666003)(3846002)(2950100002)(7366002)(42186005)(189998001)(5660300001)(305945005)(7416002)(230700001)(33646002)(7406005)(38730400002)(33716001)(83506001)(23676002)(66066001)(2201001)(86362001)(2906002)(47776003)(53936002)(921003)(1121003)(217873001)(84006005)(83996005)(2101003); DIR:OUT; SFP:1101; SCL:1; SRVR:CY4PR12MB1606; H:[127.0.1.1]; FPR:; SPF:None; MLV:sfv; LANG:en; X-Microsoft-Exchange-Diagnostics: =?utf-8?B?MTtDWTRQUjEyTUIxNjA2OzIzOnkyamtsTXZIU09WMTc2UUNGZ0tzK1ErQm1M?= =?utf-8?B?UW5SemY2TUs5SHRWR2JVMWxGNGd0ZFFtdkdQMy9NQlJPUnNtVTNkUS8zQ09r?= =?utf-8?B?MWtkWkpUM1pyaUhXTkRRWnh0NDZtNit2eUFsTk1LZXpLT3hjaGJPQkMrS0Nr?= =?utf-8?B?WFp5SFM3TlZvLzdCMTNocGM0b1I2OU5OdUNoYVpVTjZXMXVEb0lmdC9JTHZZ?= =?utf-8?B?NTZzQTQvU2NWT2tXaU9YcHVRVXZoWGxYdENUd0x4SnBsQmtRaVhWTnNyUUQ3?= =?utf-8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XyelpLiJhHeCYkpb9r7nRAw0CNijQxJk=; 5:0fWdMqpgE/aug160fDngFGgnoUHtD43ReYREg+4cxWtbJy1kh64f5o+m447odbdG0Y+6iukxzJau1vEus3ft7XwWqF9TX4EHSLcgsurAK6S2032hfbRAt40AcH535AENoussYm6wt3BkBrDsXu8nOw==; 24:s4iKEQWk8+oV3PsBGeQiFhTkgHqwp9ooXjTbJ87isTEu3ZMdHe8MkevB1v8oBzXbeLuWg88zlyCiP80yScRWMIp/ixoo96+C5CXlJZ3g+Qo= SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1; CY4PR12MB1606; 7:poW1TIs4SMYZ8+L0apC0jyYZmbyE/yvGigLnMa8jUuTGPR2lg9D5QGpvTed1pc8KJYvo+gILO3fnFdBUefPyAUlqpByb6cqsxrTcv9mGhRITKUpSZRAdrj98fLcEpZM6qr4TnLGHH0Q/eic7H9B2NUNdq9bJ0Hx/dSOYA5S6iqDHIRadq2RwDDrm5blhJ45FYPs1HK0y1jRjNVAggugbWdaIaLsQzN0JzOo3/0SENv28wdZUrj5wuWwFqwMEhsGqiD1hgzhwbpou+6386tX+wQ10ZlZKYlBHaiWHhq7JcG+UjE1JLT+i+7s5GcunlZ1xOI5P2ACvdjcamE5VwSrsvQ==; 20:/YUr2ggQtZ2xzr5/mfGjCpRF7qb6iBVfR0ojim4LhUYxrt+KvGFjQeoi9IdCG9MOZbYCExceI2HeqO144Y0/dQOB2Z5+TQ7ujh3QzovlHxJG2ff+SXQBCdZDtouGRyEPGB/0WNIVZoOl3l72PoLezjNtzYYsntMNTj5Ea7z4Hx0z6gPNV7dCT+AX08x8+O+zbYD0qKZWJ+wrS3Aw+aTus8qACQvzt+EjPwV1HZYvTwPtF+Ph2igbGihAq//a8meL X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Mar 2017 15:13:42.5794 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY4PR12MB1606 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Tom Lendacky In order for memory pages to be properly mapped when SEV is active, we need to use the PAGE_KERNEL protection attribute as the base protection. This will insure that memory mapping of, e.g. ACPI tables, receives the proper mapping attributes. Signed-off-by: Tom Lendacky --- arch/x86/mm/ioremap.c | 8 ++++++++ include/linux/mm.h | 1 + kernel/resource.c | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+) diff --git a/arch/x86/mm/ioremap.c b/arch/x86/mm/ioremap.c index c400ab5..481c999 100644 --- a/arch/x86/mm/ioremap.c +++ b/arch/x86/mm/ioremap.c @@ -151,7 +151,15 @@ static void __iomem *__ioremap_caller(resource_size_t phys_addr, pcm = new_pcm; } + /* + * If the page being mapped is in memory and SEV is active then + * make sure the memory encryption attribute is enabled in the + * resulting mapping. + */ prot = PAGE_KERNEL_IO; + if (sev_active() && page_is_mem(pfn)) + prot = __pgprot(pgprot_val(prot) | _PAGE_ENC); + switch (pcm) { case _PAGE_CACHE_MODE_UC: default: diff --git a/include/linux/mm.h b/include/linux/mm.h index b84615b..825df27 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -445,6 +445,7 @@ static inline int get_page_unless_zero(struct page *page) } extern int page_is_ram(unsigned long pfn); +extern int page_is_mem(unsigned long pfn); enum { REGION_INTERSECTS, diff --git a/kernel/resource.c b/kernel/resource.c index 9b5f044..db56ba3 100644 --- a/kernel/resource.c +++ b/kernel/resource.c @@ -518,6 +518,46 @@ int __weak page_is_ram(unsigned long pfn) } EXPORT_SYMBOL_GPL(page_is_ram); +/* + * This function returns true if the target memory is marked as + * IORESOURCE_MEM and IORESOUCE_BUSY and described as other than + * IORES_DESC_NONE (e.g. IORES_DESC_ACPI_TABLES). + */ +static int walk_mem_range(unsigned long start_pfn, unsigned long nr_pages) +{ + struct resource res; + unsigned long pfn, end_pfn; + u64 orig_end; + int ret = -1; + + res.start = (u64) start_pfn << PAGE_SHIFT; + res.end = ((u64)(start_pfn + nr_pages) << PAGE_SHIFT) - 1; + res.flags = IORESOURCE_MEM | IORESOURCE_BUSY; + orig_end = res.end; + while ((res.start < res.end) && + (find_next_iomem_res(&res, IORES_DESC_NONE, true) >= 0)) { + pfn = (res.start + PAGE_SIZE - 1) >> PAGE_SHIFT; + end_pfn = (res.end + 1) >> PAGE_SHIFT; + if (end_pfn > pfn) + ret = (res.desc != IORES_DESC_NONE) ? 1 : 0; + if (ret) + break; + res.start = res.end + 1; + res.end = orig_end; + } + return ret; +} + +/* + * This generic page_is_mem() returns true if specified address is + * registered as memory in iomem_resource list. + */ +int __weak page_is_mem(unsigned long pfn) +{ + return walk_mem_range(pfn, 1) == 1; +} +EXPORT_SYMBOL_GPL(page_is_mem); + /** * region_intersects() - determine intersection of region with known resources * @start: region start address