From patchwork Thu Feb 3 16:43:23 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Fernandez X-Patchwork-Id: 12734323 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 45ECBC433EF for ; Thu, 3 Feb 2022 16:43:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AAD166B03DA; Thu, 3 Feb 2022 11:43:55 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A5C9F6B03DB; Thu, 3 Feb 2022 11:43:55 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8D74F6B03DC; Thu, 3 Feb 2022 11:43:55 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0136.hostedemail.com [216.40.44.136]) by kanga.kvack.org (Postfix) with ESMTP id 7F97E6B03DA for ; Thu, 3 Feb 2022 11:43:55 -0500 (EST) Received: from smtpin09.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 42F18181951DA for ; Thu, 3 Feb 2022 16:43:55 +0000 (UTC) X-FDA: 79102040430.09.C9639D5 Received: from mail-oo1-f48.google.com (mail-oo1-f48.google.com [209.85.161.48]) by imf31.hostedemail.com (Postfix) with ESMTP id E419F20002 for ; Thu, 3 Feb 2022 16:43:54 +0000 (UTC) Received: by mail-oo1-f48.google.com with SMTP id r15-20020a4ae5cf000000b002edba1d3349so1962043oov.3 for ; Thu, 03 Feb 2022 08:43:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=eclypsium.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=zqxGKfIaElLILaxF70bDCH7K86RuUZa6ey16asaAIL8=; b=datlh4dR78ilyTuX5QJMyasJ51wpsY/9yQWHYGQ5q+433ZIPXwWIrpKMsFE6Scjokx VVmwwBvqOxzijJNX6FoIk2lBAXVjwP18b7JWnw/DUTcY2nlfRG1h0DhNZkY/sRr2hBAk cFcAktlfD8CBOQtWCtesgfwtCD30Ag+pnjxeef09PTYlVmecbF7xr47SFmHX4pJr8eaC WbxOL1lARe1FGG//DOrbrVjBmmNl953y/6PcDdyUEevozYmWj3fUpEuOQqaT/kn2Lvke zdywgqZAjdL6sRg3kQs8EKm+q0Gm/wJYCEMcsXnxCnFgDwNUf2wXdDR99WKaXGadra07 jKsg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=zqxGKfIaElLILaxF70bDCH7K86RuUZa6ey16asaAIL8=; b=cowonjHaSPMBXsCHZoxKmeMHZCtIo2luwWn3dWNZdbcp91zlUCBtCsJ3/Ijbne6o7+ u/SHTgvRrypm+2g79Tto6F2DzWaMoCh5asfx16x6IqAKOHKhwy0yzoKVcDuk0YkRTOxv AbJYwl/C+XTikLJFSPzy+8OwsXdv8J+TrllcegpRELd5erqZKK3Z1BOkGa9s+Ox0zo8C 4kg18SaDFsmjFlDHzmD55x1rbzcoMEP5tX9AgIBBX8Jn6m00IpETls6Td+DU5RWrVDOi rI1+g3jt3DoLX12xw/LKYCYkaYD8Kduf5L1LeNjXbgyX7AV7jeCXZilHuNicp261WfKT 2YxA== X-Gm-Message-State: AOAM530iNYSVDlWcGSULqR18vdrnxcfqBmwU9GyA3mNU7B1Hd0dTGxno w3z8CfrK9jiRJEAhTV75QqCdPQ== X-Google-Smtp-Source: ABdhPJz3KHjC1PcTs960s+pnZHPdMPi5tX1gUa+Hy63Y3u0iqFld1EfTMFHHJ54cHT2MusHjn7bfpQ== X-Received: by 2002:a05:6870:2144:: with SMTP id g4mr360672oae.30.1643906634240; Thu, 03 Feb 2022 08:43:54 -0800 (PST) Received: from localhost (host8.190-224-49.telecom.net.ar. [190.224.49.8]) by smtp.gmail.com with ESMTPSA id x16sm3107305ooa.6.2022.02.03.08.43.48 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 03 Feb 2022 08:43:54 -0800 (PST) From: Martin Fernandez To: linux-kernel@vger.kernel.org, linux-efi@vger.kernel.org, platform-driver-x86@vger.kernel.org, linux-mm@kvack.org Cc: tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, ardb@kernel.org, dvhart@infradead.org, andy@infradead.org, gregkh@linuxfoundation.org, rafael@kernel.org, rppt@kernel.org, akpm@linux-foundation.org, daniel.gutson@eclypsium.com, hughsient@gmail.com, alex.bazhaniuk@eclypsium.com, alison.schofield@intel.com, keescook@chromium.org, Martin Fernandez Subject: [PATCH v6 1/6] mm/memblock: Tag memblocks with crypto capabilities Date: Thu, 3 Feb 2022 13:43:23 -0300 Message-Id: <20220203164328.203629-2-martin.fernandez@eclypsium.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220203164328.203629-1-martin.fernandez@eclypsium.com> References: <20220203164328.203629-1-martin.fernandez@eclypsium.com> MIME-Version: 1.0 Authentication-Results: imf31.hostedemail.com; dkim=pass header.d=eclypsium.com header.s=google header.b=datlh4dR; spf=pass (imf31.hostedemail.com: domain of martin.fernandez@eclypsium.com designates 209.85.161.48 as permitted sender) smtp.mailfrom=martin.fernandez@eclypsium.com; dmarc=pass (policy=quarantine) header.from=eclypsium.com X-Rspam-User: nil X-Rspamd-Queue-Id: E419F20002 X-Stat-Signature: ddeuqmxpgf1yxeddjw119tnanirux56j X-Rspamd-Server: rspam12 X-HE-Tag: 1643906634-451335 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Add the capability to mark regions of the memory memory_type able of hardware memory encryption. Also add the capability to query if all regions of a memory node are able to do hardware memory encryption to call it when initializing the nodes. Warn the user if a node has both encryptable and non-encryptable regions. Signed-off-by: Martin Fernandez --- include/linux/memblock.h | 15 ++++++---- mm/memblock.c | 64 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 5 deletions(-) diff --git a/include/linux/memblock.h b/include/linux/memblock.h index 9dc7cb239d21..73edcce165a5 100644 --- a/include/linux/memblock.h +++ b/include/linux/memblock.h @@ -41,13 +41,15 @@ extern unsigned long long max_possible_pfn; * via a driver, and never indicated in the firmware-provided memory map as * system RAM. This corresponds to IORESOURCE_SYSRAM_DRIVER_MANAGED in the * kernel resource tree. + * @MEMBLOCK_CRYPTO_CAPABLE: capable of hardware encryption */ enum memblock_flags { - MEMBLOCK_NONE = 0x0, /* No special request */ - MEMBLOCK_HOTPLUG = 0x1, /* hotpluggable region */ - MEMBLOCK_MIRROR = 0x2, /* mirrored region */ - MEMBLOCK_NOMAP = 0x4, /* don't add to kernel direct mapping */ - MEMBLOCK_DRIVER_MANAGED = 0x8, /* always detected via a driver */ + MEMBLOCK_NONE = 0x0, /* No special request */ + MEMBLOCK_HOTPLUG = 0x1, /* hotpluggable region */ + MEMBLOCK_MIRROR = 0x2, /* mirrored region */ + MEMBLOCK_NOMAP = 0x4, /* don't add to kernel direct mapping */ + MEMBLOCK_DRIVER_MANAGED = 0x8, /* always detected via a driver */ + MEMBLOCK_CRYPTO_CAPABLE = 0x10, /* capable of hardware encryption */ }; /** @@ -121,6 +123,9 @@ int memblock_physmem_add(phys_addr_t base, phys_addr_t size); void memblock_trim_memory(phys_addr_t align); bool memblock_overlaps_region(struct memblock_type *type, phys_addr_t base, phys_addr_t size); +bool memblock_node_is_crypto_capable(int nid); +int memblock_mark_crypto_capable(phys_addr_t base, phys_addr_t size); +int memblock_clear_crypto_capable(phys_addr_t base, phys_addr_t size); int memblock_mark_hotplug(phys_addr_t base, phys_addr_t size); int memblock_clear_hotplug(phys_addr_t base, phys_addr_t size); int memblock_mark_mirror(phys_addr_t base, phys_addr_t size); diff --git a/mm/memblock.c b/mm/memblock.c index 1018e50566f3..fcf79befeab3 100644 --- a/mm/memblock.c +++ b/mm/memblock.c @@ -191,6 +191,42 @@ bool __init_memblock memblock_overlaps_region(struct memblock_type *type, return i < type->cnt; } +/** + * memblock_node_is_crypto_capable - get if whole node is capable + * of encryption + * @nid: number of node + * + * Iterate over all memory memblock_type and find if all regions under + * node @nid are capable of hardware encryption. + * + * Return: + * true if every region in memory memblock_type is capable of + * encryption, false otherwise. + */ +bool __init_memblock memblock_node_is_crypto_capable(int nid) +{ + struct memblock_region *region; + bool crypto_capable = false; + bool not_crypto_capable = false; + + for_each_mem_region(region) { + if (memblock_get_region_node(region) == nid) { + crypto_capable = + crypto_capable || + (region->flags & MEMBLOCK_CRYPTO_CAPABLE); + not_crypto_capable = + not_crypto_capable || + !(region->flags & MEMBLOCK_CRYPTO_CAPABLE); + } + } + + if (crypto_capable && not_crypto_capable) + pr_warn_once("Node %d has regions that are encryptable and regions that aren't", + nid); + + return !not_crypto_capable; +} + /** * __memblock_find_range_bottom_up - find free area utility in bottom-up * @start: start of candidate range @@ -885,6 +921,34 @@ static int __init_memblock memblock_setclr_flag(phys_addr_t base, return 0; } +/** + * memblock_mark_crypto_capable - Mark memory regions capable of hardware + * encryption with flag MEMBLOCK_CRYPTO_CAPABLE. + * @base: the base phys addr of the region + * @size: the size of the region + * + * Return: 0 on success, -errno on failure. + */ +int __init_memblock memblock_mark_crypto_capable(phys_addr_t base, + phys_addr_t size) +{ + return memblock_setclr_flag(base, size, 1, MEMBLOCK_CRYPTO_CAPABLE); +} + +/** + * memblock_clear_crypto_capable - Clear flag MEMBLOCK_CRYPTO for a + * specified region. + * @base: the base phys addr of the region + * @size: the size of the region + * + * Return: 0 on success, -errno on failure. + */ +int __init_memblock memblock_clear_crypto_capable(phys_addr_t base, + phys_addr_t size) +{ + return memblock_setclr_flag(base, size, 0, MEMBLOCK_CRYPTO_CAPABLE); +} + /** * memblock_mark_hotplug - Mark hotpluggable memory with flag MEMBLOCK_HOTPLUG. * @base: the base phys addr of the region From patchwork Thu Feb 3 16:43:24 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Fernandez X-Patchwork-Id: 12734324 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3FEDCC433EF for ; Thu, 3 Feb 2022 16:44:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 814F86B03DB; Thu, 3 Feb 2022 11:44:02 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 79DC76B03DC; Thu, 3 Feb 2022 11:44:02 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 617F56B03DD; Thu, 3 Feb 2022 11:44:02 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0251.hostedemail.com [216.40.44.251]) by kanga.kvack.org (Postfix) with ESMTP id 53EAD6B03DB for ; Thu, 3 Feb 2022 11:44:02 -0500 (EST) Received: from smtpin15.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 0AC8395C9D for ; Thu, 3 Feb 2022 16:44:02 +0000 (UTC) X-FDA: 79102040724.15.FDC1BA8 Received: from mail-vk1-f181.google.com (mail-vk1-f181.google.com [209.85.221.181]) by imf15.hostedemail.com (Postfix) with ESMTP id 94521A0003 for ; Thu, 3 Feb 2022 16:44:01 +0000 (UTC) Received: by mail-vk1-f181.google.com with SMTP id j185so2090705vkc.1 for ; Thu, 03 Feb 2022 08:44:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=eclypsium.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=dL7Kyw5R5uEJ5iFptCvOaKv8TqVxuhvnFOanF/ug8Oo=; b=MR8jXVkyACJhcck+4dcIScEcJjdlILboYDi4bqSv/6PZ8vBTWUY9vgbyWmkv4L/15Q FtimY0idlQDbZUTEV6rjD0xK50eicfInFXj/KAqbBZx+/tiI/11qsBWxJWE2MiAFXpOA /FNHgzWKuvmXWqOkq1muNBj+A2wx5HLtsHpFIY2ndlCa9hIDZgbgMK5TczXwoGmETExB FrOt7tpZowyB0etKJbSK6ojlEgVQ07mYW3A4TZTCmejaIh/8yLC+kSwulqt0/pn+MeM2 ciAwu3tFxEG46MI470mmhgPU7yW2gNwBIIAq3litWGQK53yT6iAypK5CNOL+cpubsYQr 5lpA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=dL7Kyw5R5uEJ5iFptCvOaKv8TqVxuhvnFOanF/ug8Oo=; b=iFzfmQz3muMp9FRazZ1pdUaFcD+jBelHGr/2rrGzarUVk+hbFf2JY2kfDIjzUMHXlZ L8YJygaC6lRirTGhJtRZ/Tdncg1bDhQ1XYzUNIJA9LkBdaFFHkPFAAuiNfagKc6VtLM7 oOkeYFNLafn8CXB5nG5NpaPmXEDTm5Bzv985w9rf/Hh0a/W/ih+fG+/rJYsiEkaH5Qrd i84199WbHy383y0Bsy/Mf74kaRwPebGVvmePm8bUb6KFVZr6hz99/EYyCSAJ2YpDpIT2 Fz6r1/B5QiTh6zRl0ePSeOd5/fVsSHdbfRgMWEFV6lT7R5bXa05tXYm1WZZe+7m8d/HI L6NA== X-Gm-Message-State: AOAM530XkPQYp7ux+Jjb9QXb2SlxSN9W7X8TnMfRT0gLxh1zW8SdzT3t EcuoPZadOC1DcGqplmTCIAuYRw== X-Google-Smtp-Source: ABdhPJxUoacAYoBRc2lU1MnrO2thZNWlOT7k9ldUA5Uo6JdpNTbP0+ss+QIEqv7t+aG2BNl+L+GRew== X-Received: by 2002:a1f:2d10:: with SMTP id t16mr15067999vkt.36.1643906640835; Thu, 03 Feb 2022 08:44:00 -0800 (PST) Received: from localhost (host8.190-224-49.telecom.net.ar. [190.224.49.8]) by smtp.gmail.com with ESMTPSA id k203sm6696515vka.56.2022.02.03.08.43.56 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 03 Feb 2022 08:44:00 -0800 (PST) From: Martin Fernandez To: linux-kernel@vger.kernel.org, linux-efi@vger.kernel.org, platform-driver-x86@vger.kernel.org, linux-mm@kvack.org Cc: tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, ardb@kernel.org, dvhart@infradead.org, andy@infradead.org, gregkh@linuxfoundation.org, rafael@kernel.org, rppt@kernel.org, akpm@linux-foundation.org, daniel.gutson@eclypsium.com, hughsient@gmail.com, alex.bazhaniuk@eclypsium.com, alison.schofield@intel.com, keescook@chromium.org, Martin Fernandez Subject: [PATCH v6 2/6] mm/mmzone: Tag pg_data_t with crypto capabilities Date: Thu, 3 Feb 2022 13:43:24 -0300 Message-Id: <20220203164328.203629-3-martin.fernandez@eclypsium.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220203164328.203629-1-martin.fernandez@eclypsium.com> References: <20220203164328.203629-1-martin.fernandez@eclypsium.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 94521A0003 X-Rspam-User: nil Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=eclypsium.com header.s=google header.b=MR8jXVky; dmarc=pass (policy=quarantine) header.from=eclypsium.com; spf=pass (imf15.hostedemail.com: domain of martin.fernandez@eclypsium.com designates 209.85.221.181 as permitted sender) smtp.mailfrom=martin.fernandez@eclypsium.com X-Stat-Signature: on8m9pww81dmjwi3xce1g7j8hnxmp5sb X-Rspamd-Server: rspam08 X-HE-Tag: 1643906641-771070 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Add a new member in the pg_data_t struct tell whether the node corresponding to that pg_data_t is able to do hardware memory encryption. This will be read from sysfs. Signed-off-by: Martin Fernandez Reviewed-by: Kees Cook --- include/linux/mmzone.h | 3 +++ mm/page_alloc.c | 1 + 2 files changed, 4 insertions(+) diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h index 936dc0b6c226..cec51e7a01d9 100644 --- a/include/linux/mmzone.h +++ b/include/linux/mmzone.h @@ -871,6 +871,9 @@ typedef struct pglist_data { struct task_struct *kcompactd; bool proactive_compact_trigger; #endif + + bool crypto_capable; + /* * This is a per-node reserve of pages that are not available * to userspace allocations. diff --git a/mm/page_alloc.c b/mm/page_alloc.c index c5952749ad40..8bcbd6fa0089 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -7592,6 +7592,7 @@ static void __init free_area_init_node(int nid) pgdat->node_id = nid; pgdat->node_start_pfn = start_pfn; pgdat->per_cpu_nodestats = NULL; + pgdat->crypto_capable = memblock_node_is_crypto_capable(nid); pr_info("Initmem setup node %d [mem %#018Lx-%#018Lx]\n", nid, (u64)start_pfn << PAGE_SHIFT, From patchwork Thu Feb 3 16:43:25 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Fernandez X-Patchwork-Id: 12734325 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id E099AC433EF for ; Thu, 3 Feb 2022 16:44:09 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 79FCB6B03DC; Thu, 3 Feb 2022 11:44:09 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 74F376B03DD; Thu, 3 Feb 2022 11:44:09 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5A2BB6B03DE; Thu, 3 Feb 2022 11:44:09 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0035.hostedemail.com [216.40.44.35]) by kanga.kvack.org (Postfix) with ESMTP id 442EC6B03DC for ; Thu, 3 Feb 2022 11:44:09 -0500 (EST) Received: from smtpin06.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 056A882D5699 for ; Thu, 3 Feb 2022 16:44:09 +0000 (UTC) X-FDA: 79102041018.06.39087EB Received: from mail-ua1-f51.google.com (mail-ua1-f51.google.com [209.85.222.51]) by imf10.hostedemail.com (Postfix) with ESMTP id 7DA6CC0007 for ; Thu, 3 Feb 2022 16:44:08 +0000 (UTC) Received: by mail-ua1-f51.google.com with SMTP id 60so6274859uae.1 for ; Thu, 03 Feb 2022 08:44:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=eclypsium.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Fi3rT5zncMeb0k62MKj5Wn1cLd6ugCHvnXJ1Eq5KCa0=; b=NvdHm8ZyWfi0qPRPM+5eaY3VcYdKTT5T5PTKPAIf5IKMvikxV+woDLhIxH25uoCdQw vHdVPqcx2Uu6lOdtf69jiorByv/Yvs+1n61gBbML04ly+vlppbwM04Q1IviA7SvPdgM0 ZBHQTLbQDi8OoNzNC3bp0w+LKaZICP5hjZFgM7zBPDJXVshnejKKVoSV+MV9bZ0y30uG S/fkfQqlnAbcmdf/neL6WdvadA6sSua07/0pJqxn+CmxEMHxNwUt/VZKiRAGeaF4Bt0U WZdx6403WNT+MsccRohZzeT9QgsL8y9KHpGOTq/infNJ27I5cwtTt0Gl61HrfXKptJum F1fQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Fi3rT5zncMeb0k62MKj5Wn1cLd6ugCHvnXJ1Eq5KCa0=; b=zccm3vRwUsbrkP8bo+xaty2KH+sjQ2zVcqN6xZKgj6xWEqbGKceTOYiDyx286e9Wwj JP4/dTgWM9bMPwPKHSNQm+QoTd0UJRprpVVIzxVeMsMcRn359DDcvL0TXa5Za9psNyHl YOrW/eGG9ABj/b1ymq33Hl73wIbNSmLmpJJfQmOEGnQMcbLUC362M97dXvq0yxgM3bBa Eoiui0hKSJG6/RR77l4p9BzPbjGZEkJQlX2OlubwelgrzcSYrazUPsS2Q9Be2ZJWItzJ Czk68+bNz1EVQrZUtXYtoB+qPEpXBB/657hIbK0wzQVV/lllCxW0pbY7BUaflI5LIZ6C pWDg== X-Gm-Message-State: AOAM531s2OmqkNDj38Y2H/IJNJjbwD+kUrI1oPH9wm6ycqHqBGYBFmm4 06E/6UgYQPsymD9QBXKtyQl8JA== X-Google-Smtp-Source: ABdhPJzTU4dUSsxkR8M13OcbGpk/9wSKp/Q3vNcqDpl7kwLqhu0cZWStdgwP6MKmi2+t0WOgK4+gOA== X-Received: by 2002:a05:6102:32c3:: with SMTP id o3mr13405580vss.4.1643906647473; Thu, 03 Feb 2022 08:44:07 -0800 (PST) Received: from localhost (host8.190-224-49.telecom.net.ar. [190.224.49.8]) by smtp.gmail.com with ESMTPSA id p2sm7048949uao.1.2022.02.03.08.44.02 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 03 Feb 2022 08:44:07 -0800 (PST) From: Martin Fernandez To: linux-kernel@vger.kernel.org, linux-efi@vger.kernel.org, platform-driver-x86@vger.kernel.org, linux-mm@kvack.org Cc: tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, ardb@kernel.org, dvhart@infradead.org, andy@infradead.org, gregkh@linuxfoundation.org, rafael@kernel.org, rppt@kernel.org, akpm@linux-foundation.org, daniel.gutson@eclypsium.com, hughsient@gmail.com, alex.bazhaniuk@eclypsium.com, alison.schofield@intel.com, keescook@chromium.org, Martin Fernandez Subject: [PATCH v6 3/6] x86/e820: Refactor range_update and range_remove Date: Thu, 3 Feb 2022 13:43:25 -0300 Message-Id: <20220203164328.203629-4-martin.fernandez@eclypsium.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220203164328.203629-1-martin.fernandez@eclypsium.com> References: <20220203164328.203629-1-martin.fernandez@eclypsium.com> MIME-Version: 1.0 Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=eclypsium.com header.s=google header.b=NvdHm8Zy; spf=pass (imf10.hostedemail.com: domain of martin.fernandez@eclypsium.com designates 209.85.222.51 as permitted sender) smtp.mailfrom=martin.fernandez@eclypsium.com; dmarc=pass (policy=quarantine) header.from=eclypsium.com X-Rspam-User: nil X-Rspamd-Queue-Id: 7DA6CC0007 X-Stat-Signature: 4brh5qroopdsy1hgmxp3tcghpgbm48r5 X-Rspamd-Server: rspam12 X-HE-Tag: 1643906648-341175 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: __e820__range_update and e820__range_remove had a very similar implementation with a few lines different from each other, the lines that actually perform the modification over the e820_table. The similiraties were found in the checks for the different cases on how each entry intersects with the given range (if it does at all). These checks were very presice and error prone so it was not a good idea to have them in both places. I propose a refactor of those functions, given that I need to create a similar one for this patchset. Add a function to modify a E820 table in a given range. This modification is done backed up by two helper structs: e820_entry_updater and e820_*_data. The first one, e820_entry_updater, carries 3 callbacks which function as the actions to take on the table. The other one, e820_*_data carries information needed by the callbacks, for example in the case of range_update it will carry the type that we are targeting. Signed-off-by: Martin Fernandez --- arch/x86/kernel/e820.c | 383 ++++++++++++++++++++++++++++++----------- 1 file changed, 283 insertions(+), 100 deletions(-) diff --git a/arch/x86/kernel/e820.c b/arch/x86/kernel/e820.c index bc0657f0deed..89b78c6b345b 100644 --- a/arch/x86/kernel/e820.c +++ b/arch/x86/kernel/e820.c @@ -459,144 +459,327 @@ static int __init append_e820_table(struct boot_e820_entry *entries, u32 nr_entr return __append_e820_table(entries, nr_entries); } +/** + * e820_entry_updater - Helper type for __e820__handle_range_update(). + * @should_update: Return true if @entry needs to be updated, false + * otherwise. + * @update: Apply desired actions to an @entry that is inside the + * range and satisfies @should_update. + * @new: Create new entry in the table with information gathered from + * @original and @data. + * + * Each function corresponds to an action that + * __e820__handle_range_update() does. Callbacks need to cast @data back + * to the corresponding type. + */ +struct e820_entry_updater { + bool (*should_update)(const struct e820_entry *entry, const void *data); + void (*update)(struct e820_entry *entry, const void *data); + void (*new)(struct e820_table *table, u64 new_start, u64 new_size, + const struct e820_entry *original, const void *data); +}; + +/** + * e820_remove_data - Helper type for e820__range_remove(). + * @old_type: old_type parameter of e820__range_remove(). + * @check_type: check_type parameter of e820__range_remove(). + * + * This is intended to be used as the @data argument for the + * e820_entry_updater callbacks. + */ +struct e820_remover_data { + enum e820_type old_type; + bool check_type; +}; + +/** + * e820_type_updater_data - Helper type for __e820__range_update(). + * @old_type: old_type parameter of __e820__range_update(). + * @new_type: new_type parameter of __e820__range_update(). + * + * This is intended to be used as the @data argument for the + * e820_entry_updater callbacks. + */ +struct e820_type_updater_data { + enum e820_type old_type; + enum e820_type new_type; +}; + +/** + * __e820__handle_intersected_range_update() - Helper function for + * __e820__handle_range_update(). + * @table: Target e820_table. + * @start: Start of the range. + * @size: Size of the range. + * @entry: Current entry that __e820__handle_range_update() was + * looking into. + * @updater: updater parameter of __e820__handle_range_update(). + * @data: data parameter of __e820__handle_range_update(). + * + * Helper for __e820__handle_range_update to handle the case where + * neither the entry completely covers the range nor the range + * completely covers the entry. + * + * Return: The updated size. + */ static u64 __init -__e820__range_update(struct e820_table *table, u64 start, u64 size, enum e820_type old_type, enum e820_type new_type) +__e820__handle_intersected_range_update(struct e820_table *table, + u64 start, + u64 size, + struct e820_entry *entry, + const struct e820_entry_updater *updater, + const void *data) { u64 end; - unsigned int i; - u64 real_updated_size = 0; - - BUG_ON(old_type == new_type); + u64 entry_end = entry->addr + entry->size; + u64 inner_start; + u64 inner_end; + u64 updated_size = 0; if (size > (ULLONG_MAX - start)) size = ULLONG_MAX - start; end = start + size; - printk(KERN_DEBUG "e820: update [mem %#010Lx-%#010Lx] ", start, end - 1); - e820_print_type(old_type); - pr_cont(" ==> "); - e820_print_type(new_type); - pr_cont("\n"); - - for (i = 0; i < table->nr_entries; i++) { - struct e820_entry *entry = &table->entries[i]; - u64 final_start, final_end; - u64 entry_end; + inner_start = max(start, entry->addr); + inner_end = min(end, entry_end); + + /* Range and entry do intersect and... */ + if (inner_start < inner_end) { + /* Entry is on the left */ + if (entry->addr < inner_start) { + /* Resize current entry */ + entry->size = inner_start - entry->addr; + /* Entry is on the right */ + } else { + /* Resize and move current section */ + entry->addr = inner_end; + entry->size = entry_end - inner_end; + } + /* Create new entry with intersected region */ + updater->new(table, inner_start, inner_end - inner_start, entry, data); - if (entry->type != old_type) - continue; + updated_size += inner_end - inner_start; + } /* Else: [start, end) doesn't cover entry */ - entry_end = entry->addr + entry->size; + return updated_size; +} - /* Completely covered by new range? */ - if (entry->addr >= start && entry_end <= end) { - entry->type = new_type; - real_updated_size += entry->size; - continue; - } +/** __e820__handle_range_update(): Helper function to update a address + * range in a e820_table + * @table: e820_table that we want to modify. + * @start: Start of the range. + * @size: Size of the range. + * @updater: Callbacks to modify the table. + * @data: Information to modify the table. + * + * Update the table @table in [@start, @start + @size) doing the + * actions given in @updater. + * + * Return: The updated size. + */ +static u64 __init +__e820__handle_range_update(struct e820_table *table, + u64 start, + u64 size, + const struct e820_entry_updater *updater, + const void *data) +{ + u64 updated_size = 0; + u64 end; + unsigned int i; - /* New range is completely covered? */ - if (entry->addr < start && entry_end > end) { - __e820__range_add(table, start, size, new_type); - __e820__range_add(table, end, entry_end - end, entry->type); - entry->size = start - entry->addr; - real_updated_size += size; - continue; - } + if (size > (ULLONG_MAX - start)) + size = ULLONG_MAX - start; - /* Partially covered: */ - final_start = max(start, entry->addr); - final_end = min(end, entry_end); - if (final_start >= final_end) - continue; + end = start + size; - __e820__range_add(table, final_start, final_end - final_start, new_type); + for (i = 0; i < table->nr_entries; i++) { + struct e820_entry *entry = &table->entries[i]; + u64 entry_end = entry->addr + entry->size; + + if (updater->should_update(data, entry)) { + /* Range completely covers entry */ + if (entry->addr >= start && entry_end <= end) { + updater->update(entry, data); + updated_size += entry->size; + /* Entry completely covers range */ + } else if (start > entry->addr && end < entry_end) { + /* Resize current entry */ + entry->size = start - entry->addr; + + /* Create new entry with intersection region */ + updater->new(table, start, size, entry, data); + + /* + * Create a new entry for the leftover + * of the current entry + */ + __e820__range_add(table, end, entry_end - end, + entry->type); + + updated_size += size; + } else { + updated_size = + __e820__handle_intersected_range_update(table, start, size, + entry, updater, data); + } + } + } - real_updated_size += final_end - final_start; + return updated_size; +} - /* - * Left range could be head or tail, so need to update - * its size first: - */ - entry->size -= final_end - final_start; - if (entry->addr < final_start) - continue; +static bool __init type_updater__should_update(const struct e820_entry *entry, + const void *data) +{ + struct e820_type_updater_data *type_updater_data = + (struct e820_type_updater_data *)data; - entry->addr = final_end; - } - return real_updated_size; + return entry->type == type_updater_data->old_type; } -u64 __init e820__range_update(u64 start, u64 size, enum e820_type old_type, enum e820_type new_type) +static void __init type_updater__update(struct e820_entry *entry, + const void *data) { - return __e820__range_update(e820_table, start, size, old_type, new_type); + struct e820_type_updater_data *type_updater_data = + (struct e820_type_updater_data *)data; + + entry->type = type_updater_data->new_type; } -static u64 __init e820__range_update_kexec(u64 start, u64 size, enum e820_type old_type, enum e820_type new_type) +static void __init type_updater__new(struct e820_table *table, u64 new_start, + u64 new_size, + const struct e820_entry *original, + const void *data) { - return __e820__range_update(e820_table_kexec, start, size, old_type, new_type); + struct e820_type_updater_data *type_updater_data = + (struct e820_type_updater_data *)data; + + __e820__range_add(table, new_start, new_size, + type_updater_data->new_type); } -/* Remove a range of memory from the E820 table: */ -u64 __init e820__range_remove(u64 start, u64 size, enum e820_type old_type, bool check_type) +static u64 __init __e820__range_update(struct e820_table *table, u64 start, + u64 size, enum e820_type old_type, + enum e820_type new_type) { - int i; - u64 end; - u64 real_removed_size = 0; + struct e820_entry_updater updater = { + .should_update = type_updater__should_update, + .update = type_updater__update, + .new = type_updater__new + }; - if (size > (ULLONG_MAX - start)) - size = ULLONG_MAX - start; + struct e820_type_updater_data data = { + .old_type = old_type, + .new_type = new_type + }; - end = start + size; - printk(KERN_DEBUG "e820: remove [mem %#010Lx-%#010Lx] ", start, end - 1); - if (check_type) - e820_print_type(old_type); + BUG_ON(old_type == new_type); + + printk(KERN_DEBUG "e820: update [mem %#018Lx-%#018Lx] ", start, + start + size - 1); + e820_print_type(old_type); + pr_cont(" ==> "); + e820_print_type(new_type); pr_cont("\n"); - for (i = 0; i < e820_table->nr_entries; i++) { - struct e820_entry *entry = &e820_table->entries[i]; - u64 final_start, final_end; - u64 entry_end; + return __e820__handle_range_update(table, start, size, &updater, &data); +} - if (check_type && entry->type != old_type) - continue; +static bool __init remover__should_update(const struct e820_entry *entry, + const void *data) +{ + struct e820_remover_data *remover_data = + (struct e820_remover_data *)data; - entry_end = entry->addr + entry->size; + return !remover_data->check_type || + entry->type == remover_data->old_type; +} - /* Completely covered? */ - if (entry->addr >= start && entry_end <= end) { - real_removed_size += entry->size; - memset(entry, 0, sizeof(*entry)); - continue; - } +static void __init remover__update(struct e820_entry *entry, const void *data) +{ + memset(entry, 0, sizeof(*entry)); +} - /* Is the new range completely covered? */ - if (entry->addr < start && entry_end > end) { - e820__range_add(end, entry_end - end, entry->type); - entry->size = start - entry->addr; - real_removed_size += size; - continue; - } +static void __init remover__new(struct e820_table *table, u64 new_start, + u64 new_size, const struct e820_entry *original, + const void *data) +{ +} - /* Partially covered: */ - final_start = max(start, entry->addr); - final_end = min(end, entry_end); - if (final_start >= final_end) - continue; +/** + * e820__range_remove() - Remove an address range from e820_table. + * @start: Start of the address range. + * @size: Size of the address range. + * @old_type: Type of the entries that we want to remove. + * @check_type: Bool to decide if ignore @old_type or not. + * + * Remove [@start, @start + @size) from e820_table. If @check_type is + * true remove only entries with type @old_type. + * + * Return: The size removed. + */ +u64 __init e820__range_remove(u64 start, u64 size, enum e820_type old_type, + bool check_type) +{ + struct e820_entry_updater updater = { + .should_update = remover__should_update, + .update = remover__update, + .new = remover__new + }; + + struct e820_remover_data data = { + .check_type = check_type, + .old_type = old_type + }; + + printk(KERN_DEBUG "e820: remove [mem %#018Lx-%#018Lx] ", start, + start + size - 1); + if (check_type) + e820_print_type(old_type); + pr_cont("\n"); - real_removed_size += final_end - final_start; + return __e820__handle_range_update(e820_table, start, size, &updater, + &data); +} - /* - * Left range could be head or tail, so need to update - * the size first: - */ - entry->size -= final_end - final_start; - if (entry->addr < final_start) - continue; +/** + * e820__range_update() - Update the type of a given address range in + * e820_table. + * @start: Start of the range. + * @size: Size of the range. + * @old_type: Type that we want to change. + * @new_type: New type to replace @old_type. + * + * Update type of addresses in [@start, @start + @size) from @old_type + * to @new_type in e820_table. + * + * Return: The size updated. + */ +u64 __init e820__range_update(u64 start, u64 size, enum e820_type old_type, + enum e820_type new_type) +{ + return __e820__range_update(e820_table, start, size, old_type, new_type); +} - entry->addr = final_end; - } - return real_removed_size; +/** + * e820__range_update_kexec() - Update the type of a given address + * range in e820_table_kexec. + * @start: Start of the range. + * @size: Size of the range. + * @old_type: Type that we want to change. + * @new_type: New type to replace @old_type. + * + * Update type of addresses in [@start, @start + @size) from @old_type + * to @new_type in e820_table_kexec. + * + * Return: The size updated. + */ +static u64 __init e820__range_update_kexec(u64 start, u64 size, + enum e820_type old_type, + enum e820_type new_type) +{ + return __e820__range_update(e820_table_kexec, start, size, old_type, new_type); } void __init e820__update_table_print(void) From patchwork Thu Feb 3 16:43:26 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Fernandez X-Patchwork-Id: 12734326 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 66BAEC433FE for ; Thu, 3 Feb 2022 16:44:15 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D6BDF6B03DD; Thu, 3 Feb 2022 11:44:14 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D1BD56B03DE; Thu, 3 Feb 2022 11:44:14 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B957D8D0129; Thu, 3 Feb 2022 11:44:14 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (relay.hostedemail.com [64.99.140.28]) by kanga.kvack.org (Postfix) with ESMTP id ABBD56B03DD for ; Thu, 3 Feb 2022 11:44:14 -0500 (EST) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay13.hostedemail.com (Postfix) with ESMTP id 7F4D5609AA for ; Thu, 3 Feb 2022 16:44:14 +0000 (UTC) X-FDA: 79102041228.02.96DCD88 Received: from mail-ua1-f48.google.com (mail-ua1-f48.google.com [209.85.222.48]) by imf20.hostedemail.com (Postfix) with ESMTP id 07CE81C0006 for ; Thu, 3 Feb 2022 16:44:13 +0000 (UTC) Received: by mail-ua1-f48.google.com with SMTP id e17so6191680uad.9 for ; Thu, 03 Feb 2022 08:44:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=eclypsium.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=yjdK5U5Da9cqL6OL8NEqAW4tX/0WDFOVYE3N0Y7mjjg=; b=RnHSE2ec2sy3mA4qRJGiuwYWc3KmdYerQlEAtmAWqgljRdYp/FHmkV11qBJPmqY4hI 9PJJnVDjSOhF+R0a/Be7S6KXoosBDsqd0/nw2NkuWgQ6aA0W7R38t390QoRmY4vua4kr snytvlWjEUY9Fkyf5CjPalbYvWzn/RbuGow8QXySbzQ0cJD0P5xAS/Fepm7M+sX8rulM 3vDcgzZd/fiJW/CQT8T9xOiDYDlyDqAL0XKfr+adDMFfSMHCNg5taonwfHtBBKIPm8rx t78P10CyuzuchirzZch6Vf17N4vuzDmRNAf6D5vEB2RYot5cPpubKjh4jBIHTO239Zct rzRA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=yjdK5U5Da9cqL6OL8NEqAW4tX/0WDFOVYE3N0Y7mjjg=; b=lzXxr+zkJoHkvT8VanVsqzV1Qdd/0Lccd5b/QmUYv5DXXI1okRdYS91FzEMA6dxgbF BSfq7YhVGoJt+JZDfTeAIbc1xekgbcBszyDRpRA/sC8FBLN6DicrHLrCO9C1b89LGuue ThAyi0zMwzkh7zFGLGJIAgmlqaVeB4xd0G1x07YZCtpuVXPtaGWUwT9BLNwMQt4mHwka AD4itYePAFobsivDij1vGVmoHXqj/XKp/8mYPS15a3CHoXYPLSsA62eP2AQhbQ4gu0mh KlZAhM3kOMbfNrc1Ed1a+xEUdavSCAYER2Evscb4kW6a8Qj9yY6tgv3Fka+Gs0tEINg2 zEFw== X-Gm-Message-State: AOAM5304vcdHRYcgaljaavMG78CFVkjz8Q8B4A2h/ZZEYHZgEZxsp4ZU i7PD7rZWAnWVNP3FSDK5nvd5jQ== X-Google-Smtp-Source: ABdhPJxfw66WC7p5te/X+pajwCMpJVrLZDihaKaoVnPruFsPWW9RLMsUn/DfHzIlf64y+VH5gAesjA== X-Received: by 2002:a05:6102:f08:: with SMTP id v8mr14207630vss.3.1643906653209; Thu, 03 Feb 2022 08:44:13 -0800 (PST) Received: from localhost (host8.190-224-49.telecom.net.ar. [190.224.49.8]) by smtp.gmail.com with ESMTPSA id u70sm3653044vsu.10.2022.02.03.08.44.09 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 03 Feb 2022 08:44:12 -0800 (PST) From: Martin Fernandez To: linux-kernel@vger.kernel.org, linux-efi@vger.kernel.org, platform-driver-x86@vger.kernel.org, linux-mm@kvack.org Cc: tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, ardb@kernel.org, dvhart@infradead.org, andy@infradead.org, gregkh@linuxfoundation.org, rafael@kernel.org, rppt@kernel.org, akpm@linux-foundation.org, daniel.gutson@eclypsium.com, hughsient@gmail.com, alex.bazhaniuk@eclypsium.com, alison.schofield@intel.com, keescook@chromium.org, Martin Fernandez Subject: [PATCH v6 4/6] x86/e820: Tag e820_entry with crypto capabilities Date: Thu, 3 Feb 2022 13:43:26 -0300 Message-Id: <20220203164328.203629-5-martin.fernandez@eclypsium.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220203164328.203629-1-martin.fernandez@eclypsium.com> References: <20220203164328.203629-1-martin.fernandez@eclypsium.com> MIME-Version: 1.0 X-Rspamd-Server: rspam11 X-Rspamd-Queue-Id: 07CE81C0006 X-Stat-Signature: 1tu8sx9o1xoifzx1gxd1xscie49ocu4b Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=eclypsium.com header.s=google header.b=RnHSE2ec; dmarc=pass (policy=quarantine) header.from=eclypsium.com; spf=pass (imf20.hostedemail.com: domain of martin.fernandez@eclypsium.com designates 209.85.222.48 as permitted sender) smtp.mailfrom=martin.fernandez@eclypsium.com X-Rspam-User: nil X-HE-Tag: 1643906653-278234 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Add a new enum for crypto capabilities. Add a new member in e820_entry to hold whether an entry is able to do hardware memory encryption or not. Add a new function e820__range_set_crypto_capable to mark all the entries in a range of addresses as encryptable. This will be called when initializing EFI. Change e820__update_table to handle merging and overlap problems taking into account crypto_capable. Signed-off-by: Martin Fernandez --- arch/x86/include/asm/e820/api.h | 1 + arch/x86/include/asm/e820/types.h | 12 +++- arch/x86/kernel/e820.c | 114 ++++++++++++++++++++++++++++-- 3 files changed, 119 insertions(+), 8 deletions(-) diff --git a/arch/x86/include/asm/e820/api.h b/arch/x86/include/asm/e820/api.h index e8f58ddd06d9..4b3b01fafdd1 100644 --- a/arch/x86/include/asm/e820/api.h +++ b/arch/x86/include/asm/e820/api.h @@ -17,6 +17,7 @@ extern bool e820__mapped_all(u64 start, u64 end, enum e820_type type); extern void e820__range_add (u64 start, u64 size, enum e820_type type); extern u64 e820__range_update(u64 start, u64 size, enum e820_type old_type, enum e820_type new_type); extern u64 e820__range_remove(u64 start, u64 size, enum e820_type old_type, bool check_type); +extern u64 e820__range_set_crypto_capable(u64 start, u64 size); extern void e820__print_table(char *who); extern int e820__update_table(struct e820_table *table); diff --git a/arch/x86/include/asm/e820/types.h b/arch/x86/include/asm/e820/types.h index 314f75d886d0..aef03c665f5e 100644 --- a/arch/x86/include/asm/e820/types.h +++ b/arch/x86/include/asm/e820/types.h @@ -46,6 +46,11 @@ enum e820_type { E820_TYPE_RESERVED_KERN = 128, }; +enum e820_crypto_capabilities { + E820_NOT_CRYPTO_CAPABLE = 0, + E820_CRYPTO_CAPABLE = 1, +}; + /* * A single E820 map entry, describing a memory range of [addr...addr+size-1], * of 'type' memory type: @@ -53,9 +58,10 @@ enum e820_type { * (We pack it because there can be thousands of them on large systems.) */ struct e820_entry { - u64 addr; - u64 size; - enum e820_type type; + u64 addr; + u64 size; + enum e820_type type; + enum e820_crypto_capabilities crypto_capable; } __attribute__((packed)); /* diff --git a/arch/x86/kernel/e820.c b/arch/x86/kernel/e820.c index 89b78c6b345b..098882d02120 100644 --- a/arch/x86/kernel/e820.c +++ b/arch/x86/kernel/e820.c @@ -163,7 +163,9 @@ int e820__get_entry_type(u64 start, u64 end) /* * Add a memory region to the kernel E820 map. */ -static void __init __e820__range_add(struct e820_table *table, u64 start, u64 size, enum e820_type type) +static void __init __e820__range_add(struct e820_table *table, u64 start, + u64 size, enum e820_type type, + enum e820_crypto_capabilities crypto_capable) { int x = table->nr_entries; @@ -176,12 +178,13 @@ static void __init __e820__range_add(struct e820_table *table, u64 start, u64 si table->entries[x].addr = start; table->entries[x].size = size; table->entries[x].type = type; + table->entries[x].crypto_capable = crypto_capable; table->nr_entries++; } void __init e820__range_add(u64 start, u64 size, enum e820_type type) { - __e820__range_add(e820_table, start, size, type); + __e820__range_add(e820_table, start, size, type, E820_NOT_CRYPTO_CAPABLE); } static void __init e820_print_type(enum e820_type type) @@ -211,6 +214,8 @@ void __init e820__print_table(char *who) e820_table->entries[i].addr + e820_table->entries[i].size - 1); e820_print_type(e820_table->entries[i].type); + if (e820_table->entries[i].crypto_capable == E820_CRYPTO_CAPABLE) + pr_cont("; crypto-capable"); pr_cont("\n"); } } @@ -327,6 +332,7 @@ int __init e820__update_table(struct e820_table *table) unsigned long long last_addr; u32 new_nr_entries, overlap_entries; u32 i, chg_idx, chg_nr; + enum e820_crypto_capabilities current_crypto, last_crypto; /* If there's only one memory region, don't bother: */ if (table->nr_entries < 2) @@ -367,6 +373,7 @@ int __init e820__update_table(struct e820_table *table) new_nr_entries = 0; /* Index for creating new map entries */ last_type = 0; /* Start with undefined memory type */ last_addr = 0; /* Start with 0 as last starting address */ + last_crypto = E820_NOT_CRYPTO_CAPABLE; /* Loop through change-points, determining effect on the new map: */ for (chg_idx = 0; chg_idx < chg_nr; chg_idx++) { @@ -388,13 +395,19 @@ int __init e820__update_table(struct e820_table *table) * 1=usable, 2,3,4,4+=unusable) */ current_type = 0; + current_crypto = E820_CRYPTO_CAPABLE; for (i = 0; i < overlap_entries; i++) { + if (overlap_list[i]->crypto_capable < current_crypto) + current_crypto = overlap_list[i]->crypto_capable; + if (overlap_list[i]->type > current_type) current_type = overlap_list[i]->type; } /* Continue building up new map based on this information: */ - if (current_type != last_type || e820_nomerge(current_type)) { + if (current_type != last_type || + current_crypto != last_crypto || + e820_nomerge(current_type)) { if (last_type != 0) { new_entries[new_nr_entries].size = change_point[chg_idx]->addr - last_addr; /* Move forward only if the new size was non-zero: */ @@ -406,9 +419,12 @@ int __init e820__update_table(struct e820_table *table) if (current_type != 0) { new_entries[new_nr_entries].addr = change_point[chg_idx]->addr; new_entries[new_nr_entries].type = current_type; + new_entries[new_nr_entries].crypto_capable = current_crypto; + last_addr = change_point[chg_idx]->addr; } last_type = current_type; + last_crypto = current_crypto; } } @@ -505,6 +521,19 @@ struct e820_type_updater_data { enum e820_type new_type; }; +/** + * e820_crypto_updater_data - Helper type for + * __e820__range_update_crypto(). + * @crypto_capable: crypto_capable parameter of + * __e820__range_update_crypto(). + * + * This is intended to be used as the @data argument for the + * e820_entry_updater callbacks. + */ +struct e820_crypto_updater_data { + enum e820_crypto_capabilities crypto_capable; +}; + /** * __e820__handle_intersected_range_update() - Helper function for * __e820__handle_range_update(). @@ -615,7 +644,8 @@ __e820__handle_range_update(struct e820_table *table, * of the current entry */ __e820__range_add(table, end, entry_end - end, - entry->type); + entry->type, + entry->crypto_capable); updated_size += size; } else { @@ -656,7 +686,7 @@ static void __init type_updater__new(struct e820_table *table, u64 new_start, (struct e820_type_updater_data *)data; __e820__range_add(table, new_start, new_size, - type_updater_data->new_type); + type_updater_data->new_type, original->crypto_capable); } static u64 __init __e820__range_update(struct e820_table *table, u64 start, @@ -686,6 +716,62 @@ static u64 __init __e820__range_update(struct e820_table *table, u64 start, return __e820__handle_range_update(table, start, size, &updater, &data); } +static bool __init crypto_updater__should_update(const struct e820_entry *entry, + const void *data) +{ + struct e820_crypto_updater_data *crypto_updater_data = + (struct e820_crypto_updater_data *)data; + + return crypto_updater_data->crypto_capable != entry->crypto_capable; +} + +static void __init crypto_updater__update(struct e820_entry *entry, + const void *data) +{ + struct e820_crypto_updater_data *crypto_updater_data = + (struct e820_crypto_updater_data *)data; + + entry->crypto_capable = crypto_updater_data->crypto_capable; +} + +static void __init crypto_updater__new(struct e820_table *table, u64 new_start, + u64 new_size, + const struct e820_entry *original, + const void *data) +{ + struct e820_crypto_updater_data *crypto_updater_data = + (struct e820_crypto_updater_data *)data; + + __e820__range_add(table, new_start, new_size, original->type, + crypto_updater_data->crypto_capable); +} + +static u64 __init +__e820__range_update_crypto(struct e820_table *table, u64 start, u64 size, + enum e820_crypto_capabilities crypto_capable) +{ + struct e820_entry_updater updater = { + .should_update = crypto_updater__should_update, + .update = crypto_updater__update, + .new = crypto_updater__new + }; + + struct e820_crypto_updater_data data = { + .crypto_capable = crypto_capable, + }; + + printk(KERN_DEBUG "e820: crypto update [mem %#018Lx-%#018Lx]", start, + start + size - 1); + pr_cont(" ==> "); + if (crypto_capable == E820_CRYPTO_CAPABLE) + pr_cont("crypto capable"); + else + pr_cont("not crypto capable"); + pr_cont("\n"); + + return __e820__handle_range_update(table, start, size, &updater, &data); +} + static bool __init remover__should_update(const struct e820_entry *entry, const void *data) { @@ -782,6 +868,22 @@ static u64 __init e820__range_update_kexec(u64 start, u64 size, return __e820__range_update(e820_table_kexec, start, size, old_type, new_type); } +/** + * e820__range_set_crypto_capable() - Set %E820_CRYPTO_CAPABLE to a + * given range of addresses in e820_table. + * @start: Start of the range. + * @size: Size of the range. + * + * Set %E820_CRYPTO_CAPABLE to [@start, @start + @size) in e820_table. + * + * Return: The size updated. + */ +u64 __init e820__range_set_crypto_capable(u64 start, u64 size) +{ + return __e820__range_update_crypto(e820_table, start, size, + E820_CRYPTO_CAPABLE); +} + void __init e820__update_table_print(void) { if (e820__update_table(e820_table)) @@ -1505,6 +1607,8 @@ void __init e820__memblock_setup(void) continue; memblock_add(entry->addr, entry->size); + if (entry->crypto_capable == E820_CRYPTO_CAPABLE) + memblock_mark_crypto_capable(entry->addr, entry->size); } /* Throw away partial pages: */ From patchwork Thu Feb 3 16:43:27 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Fernandez X-Patchwork-Id: 12734327 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6A1CEC433F5 for ; Thu, 3 Feb 2022 16:44:22 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F19DB6B03DF; Thu, 3 Feb 2022 11:44:21 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id EC9A38D0129; Thu, 3 Feb 2022 11:44:21 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D6A346B03E1; Thu, 3 Feb 2022 11:44:21 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0245.hostedemail.com [216.40.44.245]) by kanga.kvack.org (Postfix) with ESMTP id C73606B03DF for ; Thu, 3 Feb 2022 11:44:21 -0500 (EST) Received: from smtpin10.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 8670095C9D for ; Thu, 3 Feb 2022 16:44:21 +0000 (UTC) X-FDA: 79102041522.10.C042475 Received: from mail-vk1-f175.google.com (mail-vk1-f175.google.com [209.85.221.175]) by imf22.hostedemail.com (Postfix) with ESMTP id 27B35C0004 for ; Thu, 3 Feb 2022 16:44:21 +0000 (UTC) Received: by mail-vk1-f175.google.com with SMTP id 48so2129962vki.0 for ; Thu, 03 Feb 2022 08:44:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=eclypsium.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=2VGMhr11GA+eGLRm5CsL28eo528BHkwYTiIF+ncvNsE=; b=IAiLk5cjQwcE3yLS3aO2GBCqcyaetSmqrMvYHWmRcTBrX/70H06j9PR2aE3QGP81gN G6QDkvipCsGgjSUtRGArhWJu9iDWKOYWnm+NZ0aqk4cri2wqtuUQX+b9q3EK3qLf7feX OImmnQkBiMNjk2/7Ue9/B8Qdinvi+e/rlfFOS1vMUliTuXgm65EiqOAZE9CaQY4tGH/U /17cWo3icgYBFxnClGbVhoptcy+1QRAjpqIMCkUQhjxF94o8InjEYUfRnEDRxY6mI8V3 60dkSPCH/hnp20RoME00w3RlqarwXBsRUPrQ7hYsR3fup3yBWAysWmRuspyPg2EBrkB9 r+AQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=2VGMhr11GA+eGLRm5CsL28eo528BHkwYTiIF+ncvNsE=; b=jciTX/1l/2Yu8+dto5Ar98MeTLBzxRuEzNAs9RV1BWWnD0tiyE1Rl5USzXykqvw+l1 K8yO6uvKc2Gpg21stLLV2ToJiGPRamK+ZdTeoC57nPNLt2O2SnFC95H550EbrVqfeHFh u7YhDJOsZXSzlAZwewPi3sm6tURUFNZXHlMnmE2hv6HUQbciT6BweSCaOcXeZkBvZWBl w5GKVDOcHT2TKoT4sWoXW6Ngqiai48ZIX3PmlGLG35xCCOygh2MLqD/YG7O8TMKfJWPi +C6+a7BMtby0Blh0hOTcAWR2ydoUGqgxxtB39XdIH4J9po05iKtpRhlB1ZOZPcVcQIgj IWeQ== X-Gm-Message-State: AOAM532TgZNQWH62duROLqviHUHH5AF7DTfQhlt0QmLysFguZAfqtzRf sR0Cf/lp1nR2eKa8k0xw0XqXug== X-Google-Smtp-Source: ABdhPJzsOFFOqxgDzTCzuaTzUcSCoJoy32VcScNJTJMZP7zNyU9OfyuKOow2hpJD7GfcAiCruPNRVA== X-Received: by 2002:a05:6122:c90:: with SMTP id ba16mr15282992vkb.39.1643906660499; Thu, 03 Feb 2022 08:44:20 -0800 (PST) Received: from localhost (host8.190-224-49.telecom.net.ar. [190.224.49.8]) by smtp.gmail.com with ESMTPSA id s11sm6623102vke.47.2022.02.03.08.44.15 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 03 Feb 2022 08:44:20 -0800 (PST) From: Martin Fernandez To: linux-kernel@vger.kernel.org, linux-efi@vger.kernel.org, platform-driver-x86@vger.kernel.org, linux-mm@kvack.org Cc: tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, ardb@kernel.org, dvhart@infradead.org, andy@infradead.org, gregkh@linuxfoundation.org, rafael@kernel.org, rppt@kernel.org, akpm@linux-foundation.org, daniel.gutson@eclypsium.com, hughsient@gmail.com, alex.bazhaniuk@eclypsium.com, alison.schofield@intel.com, keescook@chromium.org, Martin Fernandez Subject: [PATCH v6 5/6] x86/efi: Tag e820_entries as crypto capable from EFI memmap Date: Thu, 3 Feb 2022 13:43:27 -0300 Message-Id: <20220203164328.203629-6-martin.fernandez@eclypsium.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220203164328.203629-1-martin.fernandez@eclypsium.com> References: <20220203164328.203629-1-martin.fernandez@eclypsium.com> MIME-Version: 1.0 X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 27B35C0004 X-Rspam-User: nil Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=eclypsium.com header.s=google header.b=IAiLk5cj; dmarc=pass (policy=quarantine) header.from=eclypsium.com; spf=pass (imf22.hostedemail.com: domain of martin.fernandez@eclypsium.com designates 209.85.221.175 as permitted sender) smtp.mailfrom=martin.fernandez@eclypsium.com X-Stat-Signature: 7iij4crrjyc38dpmfyq4by1mcii8yfxd X-HE-Tag: 1643906661-450013 X-Bogosity: Ham, tests=bogofilter, spamicity=0.008378, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Add a function to iterate over the EFI Memory Map and mark the regions tagged with EFI_MEMORY_CPU_CRYPTO in the e820_table; and call it from efi_init if add_efi_memmap is disabled. Also modify do_add_efi_memmap to mark the regions there. If add_efi_memmap is false, also check that the e820_table has enough size to (possibly) store also the EFI memmap. Signed-off-by: Martin Fernandez --- arch/x86/platform/efi/efi.c | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/arch/x86/platform/efi/efi.c b/arch/x86/platform/efi/efi.c index 147c30a81f15..3efa1c620c75 100644 --- a/arch/x86/platform/efi/efi.c +++ b/arch/x86/platform/efi/efi.c @@ -184,6 +184,8 @@ static void __init do_add_efi_memmap(void) } e820__range_add(start, size, e820_type); + if (md->attribute & EFI_MEMORY_CPU_CRYPTO) + e820__range_set_crypto_capable(start, size); } e820__update_table(e820_table); } @@ -441,6 +443,34 @@ static int __init efi_config_init(const efi_config_table_type_t *arch_tables) return ret; } +static void __init efi_mark_e820_regions_as_crypto_capable(void) +{ + efi_memory_desc_t *md; + + /* + * Calling e820__range_set_crypto_capable several times + * creates a bunch of entries in the E820 table. They probably + * will get merged when calling update_table but we need the + * space there anyway + */ + if (efi.memmap.nr_map + e820_table->nr_entries >= E820_MAX_ENTRIES) { + pr_err_once("E820 table is not large enough to fit EFI memmap; not marking entries as crypto capable\n"); + return; + } + + for_each_efi_memory_desc(md) { + if (md->attribute & EFI_MEMORY_CPU_CRYPTO) + e820__range_set_crypto_capable(md->phys_addr, + md->num_pages << EFI_PAGE_SHIFT); + } + + /* + * We added and modified regions so it's good to update the + * table to merge/sort + */ + e820__update_table(e820_table); +} + void __init efi_init(void) { if (IS_ENABLED(CONFIG_X86_32) && @@ -494,6 +524,13 @@ void __init efi_init(void) set_bit(EFI_RUNTIME_SERVICES, &efi.flags); efi_clean_memmap(); + /* + * If add_efi_memmap then there is no need to mark the regions + * again + */ + if (!add_efi_memmap) + efi_mark_e820_regions_as_crypto_capable(); + if (efi_enabled(EFI_DBG)) efi_print_memmap(); } From patchwork Thu Feb 3 16:43:28 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Martin Fernandez X-Patchwork-Id: 12734328 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 01D89C4332F for ; Thu, 3 Feb 2022 16:44:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9263A6B03E1; Thu, 3 Feb 2022 11:44:27 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 8D5936B03E2; Thu, 3 Feb 2022 11:44:27 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 776646B03E3; Thu, 3 Feb 2022 11:44:27 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (relay.hostedemail.com [64.99.140.26]) by kanga.kvack.org (Postfix) with ESMTP id 6826A6B03E1 for ; Thu, 3 Feb 2022 11:44:27 -0500 (EST) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 39B8C210B5 for ; Thu, 3 Feb 2022 16:44:27 +0000 (UTC) X-FDA: 79102041774.03.D7C9D4E Received: from mail-ua1-f42.google.com (mail-ua1-f42.google.com [209.85.222.42]) by imf23.hostedemail.com (Postfix) with ESMTP id E5122140009 for ; Thu, 3 Feb 2022 16:44:26 +0000 (UTC) Received: by mail-ua1-f42.google.com with SMTP id r8so6360531uaj.0 for ; Thu, 03 Feb 2022 08:44:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=eclypsium.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=/7hWGiKDX/+3TtHvDCLSMJlu4NmyHYy7nVm6Glu5jcs=; b=Eu44Mg8zNIVsl+UBg0lHCou2bGR731muhWBsGIFlH7Qlfig/tH9EjGG7LhYDeVUgL2 yBKfPclwTyg18UZrOiqfuJ89i8XybLNqTaYSX/a4KtBkhWzz5k8noCv5qKrQnf7pz9gC cU9EIuCV0fRK8UzYYfyJ83sxVUdyV2whm2zfukAd37PEkif+0OQrFVjVtJRfk/NpB5kF aTGPNskbxUyxI7LLEMPrnqlI95zdXOWkLpELHRbx4BnCz2oqk41Y/TO4YHXrNBFYCbhD etxqFqFShnXPHvrKvugW+xoT0rdqJ0frp0xn6PltLCnhV9Fkya4hBOscS61TtYtWobdj h4pQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=/7hWGiKDX/+3TtHvDCLSMJlu4NmyHYy7nVm6Glu5jcs=; b=Usj4n5L0uXB5KERcZvMOyPsGnFcIPjGtxo5cOhVVPod1PJ0MCtNQklc2aDCDPUNo+E gWG+k8XJx98KteVB0lJQasZPbSZ6dmDr9RRc6XkQbtxEiQCbqx1CCpTL6TkbVEwy929o 0sxipw1cfVYwEtXjTFI+KnkCVHwImndcFErEzm/UFVmyVJ1UwKWUwbPXQ3qFCMs6vKDR 0TGAsn9LtCi+Ey5lOkd9RrV7Ci9SOmUB5ngw5n+VdAb9jV5/Pav1YJVK5yjosnAy4pws yYbzXP2oD3M15V89qp4wKmcxL8Jy5/tzAabMD054C46rrP6y9wddz50w57YAaznBnWYc LDWg== X-Gm-Message-State: AOAM532TkySPfZYt+GaiIGsx9BkVfsbfpDBypvw/NXYYrmSHyE1ynC6i jdQ5pF1d8KZFq8OtHAlUimNXxQ== X-Google-Smtp-Source: ABdhPJx2HB4OlovRK80h8tYU9Mdgc8buyu05GDejH1rOLWTHA+JswrW0YVKJpoDXnPTHBnmidcJ4GQ== X-Received: by 2002:a67:fd55:: with SMTP id g21mr14698421vsr.86.1643906666202; Thu, 03 Feb 2022 08:44:26 -0800 (PST) Received: from localhost (host8.190-224-49.telecom.net.ar. [190.224.49.8]) by smtp.gmail.com with ESMTPSA id m185sm6362545vke.30.2022.02.03.08.44.22 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 03 Feb 2022 08:44:26 -0800 (PST) From: Martin Fernandez To: linux-kernel@vger.kernel.org, linux-efi@vger.kernel.org, platform-driver-x86@vger.kernel.org, linux-mm@kvack.org Cc: tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, ardb@kernel.org, dvhart@infradead.org, andy@infradead.org, gregkh@linuxfoundation.org, rafael@kernel.org, rppt@kernel.org, akpm@linux-foundation.org, daniel.gutson@eclypsium.com, hughsient@gmail.com, alex.bazhaniuk@eclypsium.com, alison.schofield@intel.com, keescook@chromium.org, Martin Fernandez Subject: [PATCH v6 6/6] drivers/node: Show in sysfs node's crypto capabilities Date: Thu, 3 Feb 2022 13:43:28 -0300 Message-Id: <20220203164328.203629-7-martin.fernandez@eclypsium.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220203164328.203629-1-martin.fernandez@eclypsium.com> References: <20220203164328.203629-1-martin.fernandez@eclypsium.com> MIME-Version: 1.0 X-Stat-Signature: y4i4khd4dw6jmd1rimf49miozm49i3z6 X-Rspam-User: nil Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=eclypsium.com header.s=google header.b=Eu44Mg8z; spf=pass (imf23.hostedemail.com: domain of martin.fernandez@eclypsium.com designates 209.85.222.42 as permitted sender) smtp.mailfrom=martin.fernandez@eclypsium.com; dmarc=pass (policy=quarantine) header.from=eclypsium.com X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: E5122140009 X-HE-Tag: 1643906666-975712 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000016, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Show in each node in sysfs if its memory is able to do be encrypted by the CPU, ie. if all its memory is marked with EFI_MEMORY_CPU_CRYPTO in the EFI memory map. Signed-off-by: Martin Fernandez --- Documentation/ABI/testing/sysfs-devices-node | 10 ++++++++++ drivers/base/node.c | 10 ++++++++++ 2 files changed, 20 insertions(+) create mode 100644 Documentation/ABI/testing/sysfs-devices-node diff --git a/Documentation/ABI/testing/sysfs-devices-node b/Documentation/ABI/testing/sysfs-devices-node new file mode 100644 index 000000000000..0d1fd86c9faf --- /dev/null +++ b/Documentation/ABI/testing/sysfs-devices-node @@ -0,0 +1,10 @@ +What: /sys/devices/system/node/nodeX/crypto_capable +Date: February 2022 +Contact: Martin Fernandez +Users: fwupd (https://fwupd.org) +Description: + This value is 1 if all system memory in this node is + marked with EFI_MEMORY_CPU_CRYPTO, indicating that the + system memory is capable of being protected with the + CPU’s memory cryptographic capabilities. It is 0 + otherwise. \ No newline at end of file diff --git a/drivers/base/node.c b/drivers/base/node.c index 87acc47e8951..dabaed997ecd 100644 --- a/drivers/base/node.c +++ b/drivers/base/node.c @@ -560,11 +560,21 @@ static ssize_t node_read_distance(struct device *dev, } static DEVICE_ATTR(distance, 0444, node_read_distance, NULL); +static ssize_t crypto_capable_show(struct device *dev, + struct device_attribute *attr, char *buf) +{ + struct pglist_data *pgdat = NODE_DATA(dev->id); + + return sysfs_emit(buf, "%d\n", pgdat->crypto_capable); +} +static DEVICE_ATTR_RO(crypto_capable); + static struct attribute *node_dev_attrs[] = { &dev_attr_meminfo.attr, &dev_attr_numastat.attr, &dev_attr_distance.attr, &dev_attr_vmstat.attr, + &dev_attr_crypto_capable.attr, NULL };