From mboxrd@z Thu Jan 1 00:00:00 1970 From: Tom Lendacky Subject: Re: [RFC PATCH v2 08/32] x86: Use PAGE_KERNEL protection for ioremap of memory page Date: Thu, 16 Mar 2017 15:04:42 -0500 Message-ID: <413f12e9-818a-745d-374b-3dbc439e972c@amd.com> References: <148846752022.2349.13667498174822419498.stgit@brijesh-build-machine> <148846761276.2349.4899767672892365544.stgit@brijesh-build-machine> <20170307145954.l2fqy5s5h65wbtyz@pd.tnic> Mime-Version: 1.0 Content-Type: text/plain; charset="utf-8"; format=flowed Content-Transfer-Encoding: 7bit Cc: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Return-path: In-Reply-To: <20170307145954.l2fqy5s5h65wbtyz@pd.tnic> Sender: owner-linux-mm@kvack.org List-Id: linux-crypto.vger.kernel.org On 3/7/2017 8:59 AM, Borislav Petkov wrote: > On Thu, Mar 02, 2017 at 10:13:32AM -0500, Brijesh Singh wrote: >> 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 >> --- > >> 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)) > > Hmm, a resource tree walk per ioremap call. This could get expensive for > ioremap-heavy workloads. > > __ioremap_caller() gets called here during boot 55 times so not a whole > lot but I wouldn't be surprised if there were some nasty use cases which > ioremap a lot. > > ... > >> 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; >> +} > > So the relevant difference between this one and walk_system_ram_range() > is this: > > - ret = (*func)(pfn, end_pfn - pfn, arg); > + ret = (res.desc != IORES_DESC_NONE) ? 1 : 0; > > so it seems to me you can have your own *func() pointer which does that > IORES_DESC_NONE comparison. And then you can define your own workhorse > __walk_memory_range() which gets called by both walk_mem_range() and > walk_system_ram_range() instead of almost duplicating them. > > And looking at walk_system_ram_res(), that one looks similar too except > the pfn computation. But AFAICT the pfn/end_pfn things are computed from > res.start and res.end so it looks to me like all those three functions > are crying for unification... I'll take a look at what it takes to consolidate these with a pre-patch. Then I'll add the new support. Thanks, Tom > -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to majordomo@kvack.org. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: email@kvack.org From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753497AbdCPUj7 (ORCPT ); Thu, 16 Mar 2017 16:39:59 -0400 Received: from mail-sn1nam02on0066.outbound.protection.outlook.com ([104.47.36.66]:44461 "EHLO NAM02-SN1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1752122AbdCPUjy (ORCPT ); Thu, 16 Mar 2017 16:39:54 -0400 Authentication-Results: davemloft.net; dkim=none (message not signed) header.d=none;davemloft.net; dmarc=none action=none header.from=amd.com; Subject: Re: [RFC PATCH v2 08/32] x86: Use PAGE_KERNEL protection for ioremap of memory page To: Borislav Petkov , Brijesh Singh References: <148846752022.2349.13667498174822419498.stgit@brijesh-build-machine> <148846761276.2349.4899767672892365544.stgit@brijesh-build-machine> <20170307145954.l2fqy5s5h65wbtyz@pd.tnic> CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , From: Tom Lendacky Message-ID: <413f12e9-818a-745d-374b-3dbc439e972c@amd.com> Date: Thu, 16 Mar 2017 15:04:42 -0500 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.8.0 MIME-Version: 1.0 In-Reply-To: <20170307145954.l2fqy5s5h65wbtyz@pd.tnic> Content-Type: text/plain; charset="utf-8"; format=flowed Content-Transfer-Encoding: 7bit X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: BN6PR15CA0016.namprd15.prod.outlook.com (10.172.204.154) To CY4PR12MB1141.namprd12.prod.outlook.com (10.168.163.149) X-MS-Office365-Filtering-Correlation-Id: 4a8ebdc9-f416-471b-d9f6-08d46ca7b51a X-MS-Office365-Filtering-HT: Tenant X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:(22001)(48565401081);SRVR:CY4PR12MB1141; X-Microsoft-Exchange-Diagnostics: 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hW9PCembf+;20:E9zFjwrQdzG5hKqrHKOXzrKK+DWIBdZ4cZgbFwMLySxpKNHXWjv3QObGuhRhmRO6PDOdvZ+GhbDTEBbnF01Pk4ZHefqDXmAO+PxX1gR7D4t2sQmoTDIPqAw8T+UW8HTHm57S+LQuyWwdsbfLjiuZqHcrNHFX21v5n67QaE/2/dVI7ACCzEb+tI7EsBr9HbUCQYq+LnZeD1OJWoZdaz39sR6upCPvO0zl1REoKiBrw7jHgHxyMMLS4lBK4vCC/PQ4o7ozDFsy8Am2uBQPenQhlq0aQoyVK1B3Uv23baGSgLJ1UawXTstPsWi5JHmvuKwPZ/UJHn415oFf6zfowY0ATG4H9cimpZeHR5PMgqzfZJ4kmF+xlXUvkOtBQUTMXZ4av1gcypTrevup049kjxekrCmTpqyW2qpOuWKmJuFuUuzk2GCnhGXmnrgyfUd0RQTp2sw4EFaB7ni3D8/bGkbyxeRhrfzzxe70lLXCsz4PwBwZtFxVcJMCT4YlF49ohO9D 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)(3002001)(10201501046)(6055026)(6041248)(20161123558025)(20161123555025)(20161123560025)(20161123562025)(20161123564025)(6072148);SRVR:CY4PR12MB1141;BCL:0;PCL:0;RULEID:;SRVR:CY4PR12MB1141; X-Microsoft-Exchange-Diagnostics: 1;CY4PR12MB1141;4:m+v8fou5j56bTW/vHYwDxEj13dDa/CwsgNWRm4oTjWqwDrQ66wgeIyDNhXz3iKzj5bqvei3FtpytBG10vTH34Aatpc5pk9YNGSbv+5jOgPo64PZ0McQ7NJCvRyIxH6TS4qPj+ITBx6q/vexlRptmGIEIOJaukn6uXo7QKxnq1MNKXPi3bKfbVc3pPremlLfpDGKbNOR/P9KsROn1mkum2PD9qG8wB3FvCwYVziNgv1wH1OPC/gT8o+jlcrpxXzfL/19rfVN8wW697b1/wZCJ3uiX9Xlx7uD9oVugjCoqYmdLZjhtTvfUIckQa2FmR7SiQP/40bYN3BFLKibF8d91CU28GrX9rVKz0FQjDkiD6jvImL+N1Dg2sO5oIMX00N0KgCKCmqF3WrwPiXN3Uomjk1wvj5MOO5rnBQn2j+pStas/qfhG+VozcBlVeFG+e0s5btxV1A4QwK2ksrgRDD4ziDF5upm4qFrdvIlIpENYwOAsFmBsNa12SNQQCdttNHILVICPT1LwzT2HUjBcDoRjRuYaOhsxNiXJEjV+/nYrrNHeMx5pzRZbJbEdGaj4KNB6DA5ZPdPLAKKshFLznNqPfd5tJwqla3kjmIILwVP06AUjt7CQqoJ3+ywA/HzQHq4VDklVaNtARCFP8KxSwCJywz4R09wUK1wzjPfwaIZ1tf8= X-Forefront-PRVS: 024847EE92 X-Forefront-Antispam-Report: SFV:NSPM;SFS:(10009020)(4630300001)(6049001)(6009001)(39840400002)(39450400003)(39860400002)(377454003)(24454002)(90366009)(6486002)(53546007)(33646002)(54906002)(25786008)(53936002)(6636002)(38730400002)(66066001)(65826007)(65956001)(2906002)(6666003)(83506001)(6246003)(4326008)(77096006)(229853002)(47776003)(42186005)(31686004)(6116002)(36756003)(230700001)(76176999)(31696002)(5660300001)(305945005)(23676002)(8676002)(81166006)(2950100002)(4001350100001)(7736002)(50466002)(50986999)(86362001)(7406005)(189998001)(54356999)(7416002)(217873001);DIR:OUT;SFP:1101;SCL:1;SRVR:CY4PR12MB1141;H:[10.236.64.195];FPR:;SPF:None;MLV:sfv;LANG:en; X-Microsoft-Exchange-Diagnostics: =?utf-8?B?MTtDWTRQUjEyTUIxMTQxOzIzOnQvZXkwQWw5aFhWazhNR0VESkg4SUxlWEFZ?= =?utf-8?B?SE4xb1k4cVd1SWFYZzdURWYyS3o5Rm42aS9nQ2lQUEhLaGRRR0JhMEt6bmVD?= =?utf-8?B?QkFWQjI3MzNpR1Z4Q0xxaFVUUWRhRGJGZkx5S1VFc3VTWHhzL0pBTGRMMGpw?= =?utf-8?B?VUNqcGlZ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eiseZLWtIQo2 X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 16 Mar 2017 20:04:46.8603 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY4PR12MB1141 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 3/7/2017 8:59 AM, Borislav Petkov wrote: > On Thu, Mar 02, 2017 at 10:13:32AM -0500, Brijesh Singh wrote: >> 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 >> --- > >> 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)) > > Hmm, a resource tree walk per ioremap call. This could get expensive for > ioremap-heavy workloads. > > __ioremap_caller() gets called here during boot 55 times so not a whole > lot but I wouldn't be surprised if there were some nasty use cases which > ioremap a lot. > > ... > >> 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; >> +} > > So the relevant difference between this one and walk_system_ram_range() > is this: > > - ret = (*func)(pfn, end_pfn - pfn, arg); > + ret = (res.desc != IORES_DESC_NONE) ? 1 : 0; > > so it seems to me you can have your own *func() pointer which does that > IORES_DESC_NONE comparison. And then you can define your own workhorse > __walk_memory_range() which gets called by both walk_mem_range() and > walk_system_ram_range() instead of almost duplicating them. > > And looking at walk_system_ram_res(), that one looks similar too except > the pfn computation. But AFAICT the pfn/end_pfn things are computed from > res.start and res.end so it looks to me like all those three functions > are crying for unification... I'll take a look at what it takes to consolidate these with a pre-patch. Then I'll add the new support. Thanks, Tom > From mboxrd@z Thu Jan 1 00:00:00 1970 From: Tom Lendacky Subject: Re: [RFC PATCH v2 08/32] x86: Use PAGE_KERNEL protection for ioremap of memory page Date: Thu, 16 Mar 2017 15:04:42 -0500 Message-ID: <413f12e9-818a-745d-374b-3dbc439e972c@amd.com> References: <148846752022.2349.13667498174822419498.stgit@brijesh-build-machine> <148846761276.2349.4899767672892365544.stgit@brijesh-build-machine> <20170307145954.l2fqy5s5h65wbtyz@pd.tnic> Mime-Version: 1.0 Content-Type: text/plain; charset="utf-8"; format=flowed Content-Transfer-Encoding: 7bit Return-path: In-Reply-To: <20170307145954.l2fqy5s5h65wbtyz@pd.tnic> Sender: owner-linux-mm@kvack.org To: Borislav Petkov , Brijesh Singh Cc: simon.guinot@sequanux.org, linux-efi@vger.kernel.org, kvm@vger.kernel.org, rkrcmar@redhat.com, matt@codeblueprint.co.uk, linux-pci@vger.kernel.org, linus.walleij@linaro.org, gary.hook@amd.com, linux-mm@kvack.org, paul.gortmaker@windriver.com, hpa@zytor.com, cl@linux.com, dan.j.williams@intel.com, aarcange@redhat.com, sfr@canb.auug.org.au, andriy.shevchenko@linux.intel.com, herbert@gondor.apana.org.au, bhe@redhat.com, xemul@parallels.com, joro@8bytes.org, x86@kernel.org, peterz@infradead.org, piotr.luc@intel.com, mingo@redhat.com, msalter@redhat.com, ross.zwisler@linux.intel.com, dyoung@redhat.com, jroedel@suse.de, keescook@chromium.org, arnd@arndb.de, toshi.kani@hpe.com, mathieu.desnoyers@efficios.com, luto@kernel.org, devel@linuxdriverproject.org, bhelgaas@google.com, tglx@linutronix.de, mchehab@ke List-Id: linux-efi@vger.kernel.org On 3/7/2017 8:59 AM, Borislav Petkov wrote: > On Thu, Mar 02, 2017 at 10:13:32AM -0500, Brijesh Singh wrote: >> 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 >> --- > >> 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)) > > Hmm, a resource tree walk per ioremap call. This could get expensive for > ioremap-heavy workloads. > > __ioremap_caller() gets called here during boot 55 times so not a whole > lot but I wouldn't be surprised if there were some nasty use cases which > ioremap a lot. > > ... > >> 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; >> +} > > So the relevant difference between this one and walk_system_ram_range() > is this: > > - ret = (*func)(pfn, end_pfn - pfn, arg); > + ret = (res.desc != IORES_DESC_NONE) ? 1 : 0; > > so it seems to me you can have your own *func() pointer which does that > IORES_DESC_NONE comparison. And then you can define your own workhorse > __walk_memory_range() which gets called by both walk_mem_range() and > walk_system_ram_range() instead of almost duplicating them. > > And looking at walk_system_ram_res(), that one looks similar too except > the pfn computation. But AFAICT the pfn/end_pfn things are computed from > res.start and res.end so it looks to me like all those three functions > are crying for unification... I'll take a look at what it takes to consolidate these with a pre-patch. Then I'll add the new support. Thanks, Tom > -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to majordomo@kvack.org. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: email@kvack.org From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pf0-f199.google.com (mail-pf0-f199.google.com [209.85.192.199]) by kanga.kvack.org (Postfix) with ESMTP id 098966B0038 for ; Thu, 16 Mar 2017 16:04:55 -0400 (EDT) Received: by mail-pf0-f199.google.com with SMTP id e129so103213617pfh.1 for ; Thu, 16 Mar 2017 13:04:55 -0700 (PDT) Received: from NAM02-BL2-obe.outbound.protection.outlook.com (mail-bl2nam02on0062.outbound.protection.outlook.com. [104.47.38.62]) by mx.google.com with ESMTPS id a21si52826itc.80.2017.03.16.13.04.53 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 16 Mar 2017 13:04:53 -0700 (PDT) Subject: Re: [RFC PATCH v2 08/32] x86: Use PAGE_KERNEL protection for ioremap of memory page References: <148846752022.2349.13667498174822419498.stgit@brijesh-build-machine> <148846761276.2349.4899767672892365544.stgit@brijesh-build-machine> <20170307145954.l2fqy5s5h65wbtyz@pd.tnic> From: Tom Lendacky Message-ID: <413f12e9-818a-745d-374b-3dbc439e972c@amd.com> Date: Thu, 16 Mar 2017 15:04:42 -0500 MIME-Version: 1.0 In-Reply-To: <20170307145954.l2fqy5s5h65wbtyz@pd.tnic> Content-Type: text/plain; charset="utf-8"; format=flowed Content-Transfer-Encoding: 7bit Sender: owner-linux-mm@kvack.org List-ID: To: Borislav Petkov , Brijesh Singh Cc: simon.guinot@sequanux.org, linux-efi@vger.kernel.org, kvm@vger.kernel.org, rkrcmar@redhat.com, matt@codeblueprint.co.uk, linux-pci@vger.kernel.org, linus.walleij@linaro.org, gary.hook@amd.com, linux-mm@kvack.org, paul.gortmaker@windriver.com, hpa@zytor.com, cl@linux.com, dan.j.williams@intel.com, aarcange@redhat.com, sfr@canb.auug.org.au, andriy.shevchenko@linux.intel.com, herbert@gondor.apana.org.au, bhe@redhat.com, xemul@parallels.com, joro@8bytes.org, x86@kernel.org, peterz@infradead.org, piotr.luc@intel.com, mingo@redhat.com, msalter@redhat.com, ross.zwisler@linux.intel.com, dyoung@redhat.com, jroedel@suse.de, keescook@chromium.org, arnd@arndb.de, toshi.kani@hpe.com, mathieu.desnoyers@efficios.com, luto@kernel.org, devel@linuxdriverproject.org, bhelgaas@google.com, tglx@linutronix.de, mchehab@kernel.org, iamjoonsoo.kim@lge.com, labbott@fedoraproject.org, tony.luck@intel.com, alexandre.bounine@idt.com, kuleshovmail@gmail.com, linux-kernel@vger.kernel.org, mcgrof@kernel.org, mst@redhat.com, linux-crypto@vger.kernel.org, tj@kernel.org, pbonzini@redhat.com, akpm@linux-foundation.org, davem@davemloft.net On 3/7/2017 8:59 AM, Borislav Petkov wrote: > On Thu, Mar 02, 2017 at 10:13:32AM -0500, Brijesh Singh wrote: >> 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 >> --- > >> 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)) > > Hmm, a resource tree walk per ioremap call. This could get expensive for > ioremap-heavy workloads. > > __ioremap_caller() gets called here during boot 55 times so not a whole > lot but I wouldn't be surprised if there were some nasty use cases which > ioremap a lot. > > ... > >> 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; >> +} > > So the relevant difference between this one and walk_system_ram_range() > is this: > > - ret = (*func)(pfn, end_pfn - pfn, arg); > + ret = (res.desc != IORES_DESC_NONE) ? 1 : 0; > > so it seems to me you can have your own *func() pointer which does that > IORES_DESC_NONE comparison. And then you can define your own workhorse > __walk_memory_range() which gets called by both walk_mem_range() and > walk_system_ram_range() instead of almost duplicating them. > > And looking at walk_system_ram_res(), that one looks similar too except > the pfn computation. But AFAICT the pfn/end_pfn things are computed from > res.start and res.end so it looks to me like all those three functions > are crying for unification... I'll take a look at what it takes to consolidate these with a pre-patch. Then I'll add the new support. Thanks, Tom > -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to majordomo@kvack.org. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: email@kvack.org