Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp6112765ybi; Wed, 31 Jul 2019 08:32:24 -0700 (PDT) X-Google-Smtp-Source: APXvYqxObXq0YbzU2WoTPbC+02wN3yGQzJhnIxFqlIJ3AsizflhN48H0Id9N6wPaAH5J7pzlm6oN X-Received: by 2002:a17:90a:8591:: with SMTP id m17mr3639390pjn.100.1564587144702; Wed, 31 Jul 2019 08:32:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1564587144; cv=none; d=google.com; s=arc-20160816; b=kssTJcKIJgAJrKEf3Xrz1xT0fw/J2gil2ipytbJFxtWnyTU8jXNT4DIZGz/6QWdq7l /ANNfd4TczQquM9XVypT9Rx/l1fCD6oW6H6qJF/ip6GepBVTtvfK8Cx4W4GsgquhIglR BbNxda7jTzywRQayuhHkenFmwBqniUlZpdOisR1kETYscs6pdaR+p4XhORB7LlIyImrs 4Jk7Jn0Q/g8QjeTV/ixpm5lON4J7iSpv7XgoXd7UuDsaAX4F3WL53j78Vq2ByD8MH7pH tyqvjLJqKIc5ffK+gAfAUSyQPOo6K/lPjeqkpo/GNY3+h6loXV0M4i9+yoZpfrztinzO 8ZsA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=UJ4hP1jpsdk64Bv8Sf387CJe/yf6g4uoo14C+HjaYmc=; b=T6Wcw3+2uHwIiC6DdGIv8Xl7MmVMnjBxJTYmLpjAkunMuzbHa2C1VqDPjQ7IhV/Q+2 docOgDvILNrPrc78b7V6JrIrkgWVlsobJ4ml3KQOAX5AjPUD73U6JzLFgqwPdmee3hGD R5vgfg2s6WvkRviOc5V9UE1KTcZXnwy77RGjKQ6z4KigqYmXSsQK6hMFgqrLd1kJRRd+ sKlw7EXiXY6aJDdXHKVAfgEZBeuE1ra6BSxkceyCgP4E1nFNiLrnPxE8nEWWO9C+Eevz uq0GGf2+PIq2fXuAkOyubK7eWcdlzEFDt1aa3clEyvzM1K2a3Pa5IRa5+VfUQLmpQz5I 8aXg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@shutemov-name.20150623.gappssmtp.com header.s=20150623 header.b=1WGj7wJ1; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id a24si32927881pfi.205.2019.07.31.08.32.08; Wed, 31 Jul 2019 08:32:24 -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; dkim=pass header.i=@shutemov-name.20150623.gappssmtp.com header.s=20150623 header.b=1WGj7wJ1; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729334AbfGaPXx (ORCPT + 99 others); Wed, 31 Jul 2019 11:23:53 -0400 Received: from mail-ed1-f67.google.com ([209.85.208.67]:46548 "EHLO mail-ed1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727508AbfGaPXu (ORCPT ); Wed, 31 Jul 2019 11:23:50 -0400 Received: by mail-ed1-f67.google.com with SMTP id d4so66107220edr.13 for ; Wed, 31 Jul 2019 08:23:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=shutemov-name.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=UJ4hP1jpsdk64Bv8Sf387CJe/yf6g4uoo14C+HjaYmc=; b=1WGj7wJ1Xlp3fgcH7vRPMztYr6U5tkfbscC1DOrRMnOQVJ19MV2EFmQ3dnX/uhMZZn +9L8x0NHV+iAuJ5WNvg45vfgpudSqJX8d/cH7WzdQ31ED8yVQqTnWs55ok0ywoT/cwgf LRqfk29P9c2a6t2n4GX6Zecm1hTDG8T8yRuJTxwS4MxO8xCaa30nlcP9NO9vMwzhD2/o jqb8W3AN4/5jM2qm735SpdYqlgeo8FHpUWhX0TEFnQDL0Nc2/fTIBdSXwohDdZnHEMKC JwVVXHl6iKII+YD7dMq1HoHGHE4mMuXMCZtypv68y+qEBnZs7tcFjmgIVrRdiTc9lYx6 gpdw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=UJ4hP1jpsdk64Bv8Sf387CJe/yf6g4uoo14C+HjaYmc=; b=deZdC1x1csNoRnfUNoYmKlh2k+Hmft1vytoC25UxgjYLMYPGEOCSVUqgAFpwjAW8UC Aqyd00LfgtAQD1zQIj5rOR9+D0usH+1TpY+wkVyjMe3AGCZZy60FpKBipuwA/iiy9eKq qTIfUIyTci3XyWwLRGhneAR7ha8c1sfH2bs6BYTGjT+oUyXSOZktLTI7Q9vILP3tvmpk nzkzTQa25rTw4kM5lgW8TmBookfGSoEjNkY3CR0FmtF/W6ZCpZVuX0ptdwksbFCTnKSM efDuGn0evL4eveNMdvZ1FtlnrwiS3h0vCKeFlkfkf1X5VvbSjnAc1j18r28a4XJwYg7W +/5Q== X-Gm-Message-State: APjAAAXx6HznOmHdpnx+xhWAJizDMYZjynvwvhQXBeThEJMmH4GbtqJv +lIkOZ4xEjZUo7mBcjkBdY8= X-Received: by 2002:a50:a5ec:: with SMTP id b41mr104531465edc.52.1564586628484; Wed, 31 Jul 2019 08:23:48 -0700 (PDT) Received: from box.localdomain ([86.57.175.117]) by smtp.gmail.com with ESMTPSA id k20sm17485239ede.66.2019.07.31.08.23.46 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 31 Jul 2019 08:23:47 -0700 (PDT) From: "Kirill A. Shutemov" X-Google-Original-From: "Kirill A. Shutemov" Received: by box.localdomain (Postfix, from userid 1000) id 6575C1048A4; Wed, 31 Jul 2019 18:08:17 +0300 (+03) To: Andrew Morton , x86@kernel.org, Thomas Gleixner , Ingo Molnar , "H. Peter Anvin" , Borislav Petkov , Peter Zijlstra , Andy Lutomirski , David Howells Cc: Kees Cook , Dave Hansen , Kai Huang , Jacob Pan , Alison Schofield , linux-mm@kvack.org, kvm@vger.kernel.org, keyrings@vger.kernel.org, linux-kernel@vger.kernel.org, "Kirill A. Shutemov" Subject: [PATCHv2 52/59] x86/mm: Disable MKTME if not all system memory supports encryption Date: Wed, 31 Jul 2019 18:08:06 +0300 Message-Id: <20190731150813.26289-53-kirill.shutemov@linux.intel.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190731150813.26289-1-kirill.shutemov@linux.intel.com> References: <20190731150813.26289-1-kirill.shutemov@linux.intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org UEFI memory attribute EFI_MEMORY_CPU_CRYPTO indicates whether the memory region supports encryption. Kernel doesn't handle situation when only part of the system memory supports encryption. Disable MKTME if not all system memory supports encryption. Signed-off-by: Kirill A. Shutemov --- arch/x86/mm/mktme.c | 35 +++++++++++++++++++++++++++++++++++ drivers/firmware/efi/efi.c | 25 +++++++++++++------------ include/linux/efi.h | 1 + 3 files changed, 49 insertions(+), 12 deletions(-) diff --git a/arch/x86/mm/mktme.c b/arch/x86/mm/mktme.c index 17366d81c21b..4e00c244478b 100644 --- a/arch/x86/mm/mktme.c +++ b/arch/x86/mm/mktme.c @@ -1,9 +1,11 @@ #include #include #include +#include #include #include #include +#include /* Mask to extract KeyID from physical address. */ phys_addr_t __mktme_keyid_mask; @@ -48,9 +50,42 @@ void mktme_disable(void) static bool need_page_mktme(void) { + int nid; + /* Make sure keyid doesn't collide with extended page flags */ BUILD_BUG_ON(__NR_PAGE_EXT_FLAGS > 16); + if (!mktme_nr_keyids()) + return 0; + + for_each_node_state(nid, N_MEMORY) { + const efi_memory_desc_t *md; + unsigned long node_start, node_end; + + node_start = node_start_pfn(nid) << PAGE_SHIFT; + node_end = node_end_pfn(nid) << PAGE_SHIFT; + + for_each_efi_memory_desc(md) { + u64 efi_start = md->phys_addr; + u64 efi_end = md->phys_addr + PAGE_SIZE * md->num_pages; + + if (md->attribute & EFI_MEMORY_CPU_CRYPTO) + continue; + if (efi_start > node_end) + continue; + if (efi_end < node_start) + continue; + if (!e820__mapped_any(efi_start, efi_end, E820_TYPE_RAM)) + continue; + + printk("Memory range %#llx-%#llx: doesn't support encryption\n", + efi_start, efi_end); + printk("Disable MKTME\n"); + mktme_disable(); + break; + } + } + return !!mktme_nr_keyids(); } diff --git a/drivers/firmware/efi/efi.c b/drivers/firmware/efi/efi.c index ad3b1f4866b3..fc19da5da3e8 100644 --- a/drivers/firmware/efi/efi.c +++ b/drivers/firmware/efi/efi.c @@ -852,25 +852,26 @@ char * __init efi_md_typeattr_format(char *buf, size_t size, if (attr & ~(EFI_MEMORY_UC | EFI_MEMORY_WC | EFI_MEMORY_WT | EFI_MEMORY_WB | EFI_MEMORY_UCE | EFI_MEMORY_RO | EFI_MEMORY_WP | EFI_MEMORY_RP | EFI_MEMORY_XP | - EFI_MEMORY_NV | + EFI_MEMORY_NV | EFI_MEMORY_CPU_CRYPTO | EFI_MEMORY_RUNTIME | EFI_MEMORY_MORE_RELIABLE)) snprintf(pos, size, "|attr=0x%016llx]", (unsigned long long)attr); else snprintf(pos, size, - "|%3s|%2s|%2s|%2s|%2s|%2s|%2s|%3s|%2s|%2s|%2s|%2s]", + "|%3s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%3s|%2s|%2s|%2s|%2s]", attr & EFI_MEMORY_RUNTIME ? "RUN" : "", attr & EFI_MEMORY_MORE_RELIABLE ? "MR" : "", - attr & EFI_MEMORY_NV ? "NV" : "", - attr & EFI_MEMORY_XP ? "XP" : "", - attr & EFI_MEMORY_RP ? "RP" : "", - attr & EFI_MEMORY_WP ? "WP" : "", - attr & EFI_MEMORY_RO ? "RO" : "", - attr & EFI_MEMORY_UCE ? "UCE" : "", - attr & EFI_MEMORY_WB ? "WB" : "", - attr & EFI_MEMORY_WT ? "WT" : "", - attr & EFI_MEMORY_WC ? "WC" : "", - attr & EFI_MEMORY_UC ? "UC" : ""); + attr & EFI_MEMORY_NV ? "NV" : "", + attr & EFI_MEMORY_CPU_CRYPTO ? "CR" : "", + attr & EFI_MEMORY_XP ? "XP" : "", + attr & EFI_MEMORY_RP ? "RP" : "", + attr & EFI_MEMORY_WP ? "WP" : "", + attr & EFI_MEMORY_RO ? "RO" : "", + attr & EFI_MEMORY_UCE ? "UCE" : "", + attr & EFI_MEMORY_WB ? "WB" : "", + attr & EFI_MEMORY_WT ? "WT" : "", + attr & EFI_MEMORY_WC ? "WC" : "", + attr & EFI_MEMORY_UC ? "UC" : ""); return buf; } diff --git a/include/linux/efi.h b/include/linux/efi.h index f87fabea4a85..4ac54a168ffe 100644 --- a/include/linux/efi.h +++ b/include/linux/efi.h @@ -112,6 +112,7 @@ typedef struct { #define EFI_MEMORY_MORE_RELIABLE \ ((u64)0x0000000000010000ULL) /* higher reliability */ #define EFI_MEMORY_RO ((u64)0x0000000000020000ULL) /* read-only */ +#define EFI_MEMORY_CPU_CRYPTO ((u64)0x0000000000080000ULL) /* memory encryption supported */ #define EFI_MEMORY_RUNTIME ((u64)0x8000000000000000ULL) /* range requires runtime mapping */ #define EFI_MEMORY_DESCRIPTOR_VERSION 1 -- 2.21.0