Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp3141290yba; Mon, 22 Apr 2019 21:08:14 -0700 (PDT) X-Google-Smtp-Source: APXvYqwopRAz3hfTjnjtG+SxwONX9Ggw/CwXL7/Ux1JUVA7OZxIL+4yDioH3BD1NrpNWTrYaipD8 X-Received: by 2002:aa7:8694:: with SMTP id d20mr24100238pfo.81.1555992494752; Mon, 22 Apr 2019 21:08:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1555992494; cv=none; d=google.com; s=arc-20160816; b=YL0h+pf2nqL9jOUq23hR7HzA5u8FmWl1pFg5RNYQwNzKz/afPnErLm4u7024p9oYQw D2xtyvAlnvAWzMTS0l7QlaZ8MHXIC/RnXCYT/gS5yFb9o2BxiZoIC1JDhm1xag3Cf41a +jnkajyx7aCYtFNrtKWnZJUXcQB3P3xfcW2L4tAnSDjw4KTZl1oKAvpyhomUCwsQRUDh JO/NSJcc20op0zUe/RO/fc9DanM8W2H/29fYpX/FLcJWGZa3ABjuvfiVRFhFDvFTDr8U E4G1WfpRGenBIA86O1Zf5p5uGBtn6GFLWuIrWKWUfq8oUsdouEqeBD1SLp9TKr5pHf+l yOEw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from; bh=MrxVeTUSCbBWLaMDDFG56XJhD4UFK6nhzyiKwiEo3+A=; b=FTSvKzT9ncBSMOySLfEWoQvNl3LbM7SZ5NUJEwxP398S1IlRQAGmGH/ryttA1xYgbU QFkyvhWVFaCypxGnrAYKsYy0O5x62HPwNQuZe/jZo+W8ogcL3MPQxjDLMbH6UhFgno/R dZ5GND6uvTAOO5GBHojGTnniDxpFkBxotqi2ptLyWwaeLvLRtSyQSaLCYIqCeWl6NaRF dXfrATkQhRn4Rw46VL+lNyZNlvi9mifXJeWLII6t9g12BRhlnj/E/OqV+h5WUFWChtWN PWe4nwbIWTrrkVO6YX7H3DTUL98pXh3Upyr3RNWvh0M90SHclKsWgvsCGF8wWLBHGvhX hmPQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 33si14385701pgm.385.2019.04.22.21.07.59; Mon, 22 Apr 2019 21:08:14 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729326AbfDWBar (ORCPT + 99 others); Mon, 22 Apr 2019 21:30:47 -0400 Received: from mx1.redhat.com ([209.132.183.28]:55752 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729062AbfDWBaq (ORCPT ); Mon, 22 Apr 2019 21:30:46 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id C300230842AC; Tue, 23 Apr 2019 01:30:45 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-12-81.pek2.redhat.com [10.72.12.81]) by smtp.corp.redhat.com (Postfix) with ESMTP id BEEB96013D; Tue, 23 Apr 2019 01:30:33 +0000 (UTC) From: Lianbo Jiang To: linux-kernel@vger.kernel.org Cc: kexec@lists.infradead.org, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, akpm@linux-foundation.org, dave.hansen@linux.intel.com, luto@kernel.org, peterz@infradead.org, x86@kernel.org, hpa@zytor.com, dyoung@redhat.com, bhe@redhat.com, Thomas.Lendacky@amd.com Subject: [PATCH 2/3 v11] x86/mm: change the check condition in SEV because a new descriptor is introduced Date: Tue, 23 Apr 2019 09:30:06 +0800 Message-Id: <20190423013007.17838-3-lijiang@redhat.com> In-Reply-To: <20190423013007.17838-1-lijiang@redhat.com> References: <20190423013007.17838-1-lijiang@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.40]); Tue, 23 Apr 2019 01:30:45 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Originally, those areas described as IORES_DESC_NONE are not mapped encrypted in SEV when using ioremap(). It checks for a resource that is not described as IORES_DESC_NONE, which can ensure the reserved areas are not mapped encrypted when using ioremap(). But now, a new descriptor IORES_DESC_RESERVED has been created for the reserved areas, similarly, the IORES_DESC_{NONE,RESERVED} should not be mapped encrypted in SEV when using ioremap(). Therefore, need to modify the check condition in SEV and improve them. Suggested-by: Borislav Petkov Signed-off-by: Lianbo Jiang --- arch/x86/mm/ioremap.c | 59 ++++++++++++++++++++++++++---------------- include/linux/ioport.h | 9 +++++++ 2 files changed, 46 insertions(+), 22 deletions(-) diff --git a/arch/x86/mm/ioremap.c b/arch/x86/mm/ioremap.c index dd73d5d74393..82be5707124b 100644 --- a/arch/x86/mm/ioremap.c +++ b/arch/x86/mm/ioremap.c @@ -27,9 +27,8 @@ #include "physaddr.h" -struct ioremap_mem_flags { - bool system_ram; - bool desc_other; +struct ioremap_desc { + unsigned int flags; }; /* @@ -61,13 +60,13 @@ int ioremap_change_attr(unsigned long vaddr, unsigned long size, return err; } -static bool __ioremap_check_ram(struct resource *res) +static unsigned int __ioremap_check_ram(struct resource *res) { unsigned long start_pfn, stop_pfn; unsigned long i; if ((res->flags & IORESOURCE_SYSTEM_RAM) != IORESOURCE_SYSTEM_RAM) - return false; + return 0; start_pfn = (res->start + PAGE_SIZE - 1) >> PAGE_SHIFT; stop_pfn = (res->end + 1) >> PAGE_SHIFT; @@ -75,28 +74,44 @@ static bool __ioremap_check_ram(struct resource *res) for (i = 0; i < (stop_pfn - start_pfn); ++i) if (pfn_valid(start_pfn + i) && !PageReserved(pfn_to_page(start_pfn + i))) - return true; + return IORES_MAP_SYSTEM_RAM; } - return false; + return 0; } -static int __ioremap_check_desc_other(struct resource *res) +/* + * NONE and RESERVED should not be mapped encrypted in SEV because there + * the whole memory is already encrypted. + */ +static unsigned int __ioremap_check_desc(struct resource *res) { - return (res->desc != IORES_DESC_NONE); + if (!sev_active()) + return 0; + + switch (res->desc) { + case IORES_DESC_NONE: + case IORES_DESC_RESERVED: + break; + default: + return IORES_MAP_ENCRYPTED; + } + + return 0; } static int __ioremap_res_check(struct resource *res, void *arg) { - struct ioremap_mem_flags *flags = arg; + struct ioremap_desc *desc = arg; - if (!flags->system_ram) - flags->system_ram = __ioremap_check_ram(res); + if (!(desc->flags & IORES_MAP_SYSTEM_RAM)) + desc->flags |= __ioremap_check_ram(res); - if (!flags->desc_other) - flags->desc_other = __ioremap_check_desc_other(res); + if (!(desc->flags & IORES_MAP_ENCRYPTED)) + desc->flags |= __ioremap_check_desc(res); - return flags->system_ram && flags->desc_other; + return ((desc->flags & (IORES_MAP_SYSTEM_RAM | IORES_MAP_ENCRYPTED)) == + (IORES_MAP_SYSTEM_RAM | IORES_MAP_ENCRYPTED)); } /* @@ -105,15 +120,15 @@ static int __ioremap_res_check(struct resource *res, void *arg) * resource described not as IORES_DESC_NONE (e.g. IORES_DESC_ACPI_TABLES). */ static void __ioremap_check_mem(resource_size_t addr, unsigned long size, - struct ioremap_mem_flags *flags) + struct ioremap_desc *desc) { u64 start, end; start = (u64)addr; end = start + size - 1; - memset(flags, 0, sizeof(*flags)); + memset(desc, 0, sizeof(struct ioremap_desc)); - walk_mem_res(start, end, flags, __ioremap_res_check); + walk_mem_res(start, end, desc, __ioremap_res_check); } /* @@ -138,7 +153,7 @@ static void __iomem *__ioremap_caller(resource_size_t phys_addr, resource_size_t last_addr; const resource_size_t unaligned_phys_addr = phys_addr; const unsigned long unaligned_size = size; - struct ioremap_mem_flags mem_flags; + struct ioremap_desc io_desc; struct vm_struct *area; enum page_cache_mode new_pcm; pgprot_t prot; @@ -157,12 +172,12 @@ static void __iomem *__ioremap_caller(resource_size_t phys_addr, return NULL; } - __ioremap_check_mem(phys_addr, size, &mem_flags); + __ioremap_check_mem(phys_addr, size, &io_desc); /* * Don't allow anybody to remap normal RAM that we're using.. */ - if (mem_flags.system_ram) { + if (io_desc.flags & IORES_MAP_SYSTEM_RAM) { WARN_ONCE(1, "ioremap on RAM at %pa - %pa\n", &phys_addr, &last_addr); return NULL; @@ -200,7 +215,7 @@ static void __iomem *__ioremap_caller(resource_size_t phys_addr, * resulting mapping. */ prot = PAGE_KERNEL_IO; - if ((sev_active() && mem_flags.desc_other) || encrypted) + if ((io_desc.flags & IORES_MAP_ENCRYPTED) || encrypted) prot = pgprot_encrypted(prot); switch (pcm) { diff --git a/include/linux/ioport.h b/include/linux/ioport.h index 6ed59de48bd5..5db386cfc2d4 100644 --- a/include/linux/ioport.h +++ b/include/linux/ioport.h @@ -12,6 +12,7 @@ #ifndef __ASSEMBLY__ #include #include +#include /* * Resources are tree-like, allowing * nesting etc.. @@ -136,6 +137,14 @@ enum { IORES_DESC_RESERVED = 8, }; +/* + * Flags controlling ioremap() behavior. + */ +enum { + IORES_MAP_SYSTEM_RAM = BIT(0), + IORES_MAP_ENCRYPTED = BIT(1), +}; + /* helpers to define resources */ #define DEFINE_RES_NAMED(_start, _size, _name, _flags) \ { \ -- 2.17.1