From nobody Mon Jun 15 10:48:20 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id EB565C4332F for ; Fri, 29 Apr 2022 20:17:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1380468AbiD2UVG (ORCPT ); Fri, 29 Apr 2022 16:21:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49394 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1380427AbiD2UVE (ORCPT ); Fri, 29 Apr 2022 16:21:04 -0400 Received: from mail-oi1-x22e.google.com (mail-oi1-x22e.google.com [IPv6:2607:f8b0:4864:20::22e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2988D9F3A3 for ; Fri, 29 Apr 2022 13:17:44 -0700 (PDT) Received: by mail-oi1-x22e.google.com with SMTP id r1so9481106oie.4 for ; Fri, 29 Apr 2022 13:17:44 -0700 (PDT) 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=erdqpZYLja6xDuoUNWXMJOEwQp9qkWbhvYqoJ4co3Os=; b=Q8UaonxznDmVc5gYov0m3G8lVeCJ9Wb++XO9fsv/gUqnPbStpjqIwYe0Rw241Jt1og 1wa4l4d7Jn89Q6/jKFGGjyxze+q8g5RO76SUkMpqYIydbKVQsVVVkKPc4RTvRQrGXHMg 6JTEB1ypvif9eRNj71NjI2Evpbuj5TuoHjCM2lZRZVLUM639ACMhnI9YvSOUfv0Q7u2R NOE7GpoeOT13Bxh590c+Vl6CGhR853U0Cgerj84916BoD9SDlzZ/JWwExnQbAUqEej6c z6Hs34KYKYNJDv/dkHUIU5fef1Ko3XrI07IFdNKGHp6BSyhh3MIfVHVkDqFE5bZN8rGH nuJg== 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=erdqpZYLja6xDuoUNWXMJOEwQp9qkWbhvYqoJ4co3Os=; b=1p3kOz88aHaQxOvEHUJyzmZFaXYgVkkeKY8bRGnHdh+unFENJdf8YfIW4QQECHgsA1 WSVG8HAhjmPQCvhVHkNYicQKjfI3GIl3SVI3DpTga+9/SOVcEDUHXSrjWeHRgsdMuzob t94xc71b31TXGuS8a+W92fsPUrGIJSWBQi/2+fAL5AQTaoledQGS4xQ41oSogxGeykUR +l1vM7KKrRBK6t5zk700Erg2LXUr66ZJTLVIXgNCRmj9e7DPltcSXsdeO97wO3FfLTsO Tlal59kgcmyKsYNECN4ZjDGqHtvDA2/yJSY78vER1640cnCqhgcj8c95Xoul8TGbj3WL xqNg== X-Gm-Message-State: AOAM533zd9gVRCPvXQKbjpQa392QVkg4Iws9gyxyw/dAOWFlWqeaPj3I hvwjOmDLSy2pDnQSRW0kv5GJ8t6My6ZhulC0upKBOuwYg2bqh1EXzb8NCClfx+hYlZYvrjAGINp Yb4pse1unyyX43fYtnuC0H9/LtPfUDSMF4ezm72vUsZosRJCdL89he73H8PS56WvTZUT2we5q0O Y2Xn6KEEed8NJ4IgzFJSY= X-Google-Smtp-Source: ABdhPJy1S2Ga4gCTYWLTSbLa/82ZQQmEvsYqOntWhuKnf1oxCmz0Cqnw2boboebtXSxhyQGp+cBYEw== X-Received: by 2002:a05:6808:1115:b0:2ec:e103:99c8 with SMTP id e21-20020a056808111500b002ece10399c8mr533592oih.194.1651263463240; Fri, 29 Apr 2022 13:17:43 -0700 (PDT) Received: from localhost ([181.97.174.128]) by smtp.gmail.com with ESMTPSA id r2-20020a05687002c200b000e99b1909d4sm2650005oaf.25.2022.04.29.13.17.36 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 29 Apr 2022 13:17:41 -0700 (PDT) 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 v8 1/8] mm/memblock: Tag memblocks with crypto capabilities Date: Fri, 29 Apr 2022 17:17:10 -0300 Message-Id: <20220429201717.1946178-2-martin.fernandez@eclypsium.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220429201717.1946178-1-martin.fernandez@eclypsium.com> References: <20220429201717.1946178-1-martin.fernandez@eclypsium.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" 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 | 5 ++++ mm/memblock.c | 62 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) diff --git a/include/linux/memblock.h b/include/linux/memblock.h index 50ad19662a32..00c4f1a20335 100644 --- a/include/linux/memblock.h +++ b/include/linux/memblock.h @@ -40,6 +40,7 @@ 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 =3D 0x0, /* No special request */ @@ -47,6 +48,7 @@ enum memblock_flags { MEMBLOCK_MIRROR =3D 0x2, /* mirrored region */ MEMBLOCK_NOMAP =3D 0x4, /* don't add to kernel direct mapping */ MEMBLOCK_DRIVER_MANAGED =3D 0x8, /* always detected via a driver */ + MEMBLOCK_CRYPTO_CAPABLE =3D 0x10, /* capable of hardware encryption */ }; =20 /** @@ -120,6 +122,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 e4f03a6e8e56..d6399835b155 100644 --- a/mm/memblock.c +++ b/mm/memblock.c @@ -191,6 +191,40 @@ bool __init_memblock memblock_overlaps_region(struct m= emblock_type *type, return i < type->cnt; } =20 +/** + * 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 @nid is capable of encryption, false + * otherwise. + */ +bool __init_memblock memblock_node_is_crypto_capable(int nid) +{ + struct memblock_region *region; + int crypto_capables =3D 0; + int not_crypto_capables =3D 0; + + for_each_mem_region(region) { + if (memblock_get_region_node(region) =3D=3D nid) { + if (region->flags & MEMBLOCK_CRYPTO_CAPABLE) + crypto_capables++; + else + not_crypto_capables++; + } + } + + if (crypto_capables > 0 && not_crypto_capables > 0) + pr_warn("Node %d has %d regions that are encryptable and %d regions that= aren't", + nid, not_crypto_capables, crypto_capables); + + return crypto_capables > 0 && not_crypto_capables =3D=3D 0; +} + /** * __memblock_find_range_bottom_up - find free area utility in bottom-up * @start: start of candidate range @@ -891,6 +925,34 @@ static int __init_memblock memblock_setclr_flag(phys_a= ddr_t base, return 0; } =20 +/** + * 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_HOT= PLUG. * @base: the base phys addr of the region --=20 2.30.2 From nobody Mon Jun 15 10:48:20 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 57BB1C433EF for ; Fri, 29 Apr 2022 20:17:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1380427AbiD2UVO (ORCPT ); Fri, 29 Apr 2022 16:21:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50590 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1380496AbiD2UVL (ORCPT ); Fri, 29 Apr 2022 16:21:11 -0400 Received: from mail-oa1-x31.google.com (mail-oa1-x31.google.com [IPv6:2001:4860:4864:20::31]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 22C27A0BF1 for ; Fri, 29 Apr 2022 13:17:52 -0700 (PDT) Received: by mail-oa1-x31.google.com with SMTP id 586e51a60fabf-e5ca5c580fso9219728fac.3 for ; Fri, 29 Apr 2022 13:17:52 -0700 (PDT) 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=cuoU1kSVf8VEBS5kxyG+ti9eeLYydPT4J1CXt5LwLhU=; b=Ca5CpkEdAGoDq4Rv1TW6+hSfJvX+bFxVgDxtp2Me/AhtkYKBKy7HDq9kO20qbFkCU/ oD5DMChdyFmU3n3DgPNgqCCNm7kr+52q5L9Hs/atSrY+4iP2qvhY/q2YA44Z2lv4tRjd v+qrNTH6cIBZ9Su9FBIIHdnb4f7y2STK1hcq14DBBiSA+1dXsvH5vR9SgVQv5TpKV515 6JTZ+l5RXQFB1Sq+ZHqkQs2djzOBE2pbg+fG2kXx5gpAKt3GP7C7mx5AJDZr8JYHBth7 TYSaK6qQeMoDiSKDnXRZLJRvuIFFmoFjKEfYjUbHtaeI2RjnrZIYZSMrNqd1ZoT95Xiv ckjw== 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=cuoU1kSVf8VEBS5kxyG+ti9eeLYydPT4J1CXt5LwLhU=; b=8M5ne0AQNKejHwVaUcsYYfjY7NdWQmnFPJnaVRFoaN3flkNOXbiwcubnz8HQF9sHCS cwLVSnP4Dfv9VM1MLckO7aSX6Ina+TKQYTcmIIsmHkfQ7LO5U/Ov31gUAJmIeX6WLP5p GfLFunJePFjQMoGY/mUWacfApglVmJfVuf0V8e8Ni2pMXY1n8PwHj2W9Kry101h4cMhj aN/Gr0qAOe0FZJdLGmTn5c7FqTMo+x8ZbbRFKNLcQQ4SpegoX4/CV4k/Ag41FWjNvd6h ogQpdxsPy3RDzFrhqOiUiIZPJrnYywya28tMvwrz3k7VilwIw0kB/kp+qqvnRsJwIDMK Sdow== X-Gm-Message-State: AOAM532JPUzVUKUljyRcAaU8IEN7TwTkma85Yr3gAE2nZkv4Hy9/uhnO fJTqHSXk5kx5hx/APcTZFO5fRmmh6hHFo31bnVexsjkRr5NMTp2wz5OjCE9mK9eSwrMlt/cAMgC uJAdLaL+Rqx9u4eOFPPO47j4iLYeRXTzq0SyZJWzc8PRNTTwIBOiiyFLc9wwoQmb7vYF693Da3s vB/ilh+50Eucnjzjs3HLc= X-Google-Smtp-Source: ABdhPJxIe/y2ZbNmKFx3Owc3D12ozpXuX/TsxcZP+iWHDA2M91qfr82Tlpg84wHwV/O7VsilCNZp4w== X-Received: by 2002:a05:6870:14d5:b0:ec:ab0e:d106 with SMTP id l21-20020a05687014d500b000ecab0ed106mr469524oab.65.1651263471218; Fri, 29 Apr 2022 13:17:51 -0700 (PDT) Received: from localhost ([181.97.174.128]) by smtp.gmail.com with ESMTPSA id w12-20020a4ad02c000000b0035eb4e5a6aesm1130791oor.4.2022.04.29.13.17.45 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 29 Apr 2022 13:17:50 -0700 (PDT) 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 v8 2/8] mm/mmzone: Tag pg_data_t with crypto capabilities Date: Fri, 29 Apr 2022 17:17:11 -0300 Message-Id: <20220429201717.1946178-3-martin.fernandez@eclypsium.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220429201717.1946178-1-martin.fernandez@eclypsium.com> References: <20220429201717.1946178-1-martin.fernandez@eclypsium.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Add a new member in the pg_data_t struct to 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 --- 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 46ffab808f03..89054af9e599 100644 --- a/include/linux/mmzone.h +++ b/include/linux/mmzone.h @@ -886,6 +886,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 0e42038382c1..a244151045b4 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -7699,6 +7699,7 @@ static void __init free_area_init_node(int nid) pgdat->node_id =3D nid; pgdat->node_start_pfn =3D start_pfn; pgdat->per_cpu_nodestats =3D NULL; + pgdat->crypto_capable =3D memblock_node_is_crypto_capable(nid); =20 if (start_pfn !=3D end_pfn) { pr_info("Initmem setup node %d [mem %#018Lx-%#018Lx]\n", nid, --=20 2.30.2 From nobody Mon Jun 15 10:48:20 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 63211C433F5 for ; Fri, 29 Apr 2022 20:18:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1380638AbiD2UVV (ORCPT ); Fri, 29 Apr 2022 16:21:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51336 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1380591AbiD2UVU (ORCPT ); Fri, 29 Apr 2022 16:21:20 -0400 Received: from mail-oa1-x29.google.com (mail-oa1-x29.google.com [IPv6:2001:4860:4864:20::29]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6B772A0BF7 for ; Fri, 29 Apr 2022 13:18:00 -0700 (PDT) Received: by mail-oa1-x29.google.com with SMTP id 586e51a60fabf-e9027efe6aso9186046fac.10 for ; Fri, 29 Apr 2022 13:18:00 -0700 (PDT) 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=bzJenq/qxXZmxxl1Ksk6HJARtm3Ihl+B/CevCTs1JBk=; b=OhU82U+6ILuhHnk9qZFELjk4MynpIJfr5fC/i1ZDSKydOzP1UPlko9p4vuh+p3AvnY dDMg917dkdnCGel1D0ix0GcLZSguMqbKosjPYJPbaC31yeqL9iyQuE/xj+rve5uKfzI+ UskmqyQt2XeV1aBJ973RpX1j9NiBXRRgag1fT6pNE71t1tEGhu25bMpqlsM9bh7rn7JU pEMT/Ia9fg255xXsDl30sEdLwH+sCSS1BsL9xQvM1d+GKSYKIbsdCf6K5XbF6S5cXZqz AZQWu2MesQczlNiuNErl7prDWp+cMY6wyxRIcWalI5vUrKARCo0tik1ffLJtUCWEKv0H 3Rqg== 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=bzJenq/qxXZmxxl1Ksk6HJARtm3Ihl+B/CevCTs1JBk=; b=0RONg9z9R3opjFBdDGrja3DUe01ehl1K1f/zaIqqTnBUdM6xW2Zn4zL4b6QkMWECut A8La7h038ocnTrvn4fIIyuskdf0e/P/H+84upOfanL+OZFPLJ471wg5hZrmZdHvuUUMc Bm9COedxBadgeRBuPBfliayid8/h0ZJQ1HQEyQkRLaGxytz8ojs3gTmH6fURrFPzn+j1 +Z1J9fXyUoO2XkMMgDCBAP/n+mt3r7AJgJ9A9i/Ts+7+e4eTezVkJ4PMIDX9NBDEJC2M 9M5H2lK8LQqbsWbd2yrj4zr4JSA8dvti8Y3Pf7kESIEFUztno6sb4oRbrQTLhdM9Lk5u 9KXg== X-Gm-Message-State: AOAM530kfFF9dsjeSlSAHRAkla+jrcC+tULwGKKOE29VolvDfJ5a4L3h OtxCy40ucQn08P/OlABBh7WYtieg27Jt2tsAbLWKCBZOUbknmr3Oz8mKdiy/QVLbeGe1vHZ1tSx iRqnaHf5uupW9GFLZlwLTPYr/cZ8v6CPmgvJw+DHiojk6mEGSQjI0WcAacIuuCAMD64XgkLK+FQ 4L4vSTObrtAbuPEiuqLv8= X-Google-Smtp-Source: ABdhPJyjjx8Jou1EYk06NDg5lvCjK/dBe+Yjuwv3tb5relSudRv03DIwoXeeO8KaVipm+iQer6YoHA== X-Received: by 2002:a05:6870:f5a3:b0:e1:944b:6450 with SMTP id eh35-20020a056870f5a300b000e1944b6450mr483469oab.254.1651263479419; Fri, 29 Apr 2022 13:17:59 -0700 (PDT) Received: from localhost ([181.97.174.128]) by smtp.gmail.com with ESMTPSA id p9-20020acabf09000000b00325cda1ff97sm70868oif.22.2022.04.29.13.17.54 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 29 Apr 2022 13:17:59 -0700 (PDT) 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 v8 3/8] x86/e820: Add infrastructure to refactor e820__range_{update,remove} Date: Fri, 29 Apr 2022 17:17:12 -0300 Message-Id: <20220429201717.1946178-4-martin.fernandez@eclypsium.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220429201717.1946178-1-martin.fernandez@eclypsium.com> References: <20220429201717.1946178-1-martin.fernandez@eclypsium.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" __e820__range_update and e820__range_remove had a very similar flow in its 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. Since I need to add a third one, similar to this two, in this and the following patches I'll propose a refactor of these functions. In this patch I introduce: - A new type e820_entry_updater that will carry three callbacks, those callbacks will decide WHEN to perform actions over the e820_table and WHAY actions are going to be performed. Note that there is a void pointer "data". This pointer will carry useful information for the callbacks, like the type that we want to update in e820__range_update or if we want to check the type in e820__range_remove. Check it out in the next patches where I do the rework of __e820__range_update and e820__range_remove. - A new function __e820__handle_range_update that has the flow of the original two functions to refactor. Together with e820_entry_updater will perform the desired update on the input table. On version 6 of this patch some people pointed out that this solution was over-complicated. Mike Rapoport suggested a another solution [1]. I took a look at that, and although it is indeed simpler it's more confusing at the same time. I think is manageable to have a single function to update or remove sections of the table (what Mike did), but when I added the functionality to also update the crypto_capable it became really hard to manage. I think that the approach presented in this patch it's complex but is easier to read, to extend and to test. [1] https://git.kernel.org/rppt/h/x86/e820-update-range Signed-off-by: Martin Fernandez -------------------------------------------------- Changes from v7 to v8: (Some) Callbacks of e820_entry_updater can now be NULL to avoid defining empty functions Remove kerneldocs in favor of plain comments just to explain what the functions do. --- arch/x86/kernel/e820.c | 127 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) diff --git a/arch/x86/kernel/e820.c b/arch/x86/kernel/e820.c index f267205f2d5a..e0fa3ab755a5 100644 --- a/arch/x86/kernel/e820.c +++ b/arch/x86/kernel/e820.c @@ -459,6 +459,133 @@ static int __init append_e820_table(struct boot_e820_= entry *entries, u32 nr_entr return __append_e820_table(entries, nr_entries); } =20 +/** + * struct 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. Can be set to NULL to avoid empty f= unctions. + * @new: Create new entry in the table with information gathered from + * @original and @data. Can be set to NULL to avoid empty functions. + * + * 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); +}; + +/* + * 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. + */ +static u64 __init +__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; + u64 entry_end =3D entry->addr + entry->size; + u64 inner_start; + u64 inner_end; + u64 updated_size =3D 0; + + if (size > (ULLONG_MAX - start)) + size =3D ULLONG_MAX - start; + + end =3D start + size; + inner_start =3D max(start, entry->addr); + inner_end =3D 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 =3D inner_start - entry->addr; + /* Entry is on the right */ + } else { + /* Resize and move current section */ + entry->addr =3D inner_end; + entry->size =3D entry_end - inner_end; + } + if (updater->new !=3D NULL) + /* Create new entry with intersected region */ + updater->new(table, inner_start, inner_end - inner_start, entry, data); + + updated_size +=3D inner_end - inner_start; + } /* Else: [start, end) doesn't cover entry */ + + return updated_size; +} + +/* + * Update the table @table in [@start, @start + @size) doing the + * actions given in @updater. + */ +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 =3D 0; + u64 end; + unsigned int i; + + if (size > (ULLONG_MAX - start)) + size =3D ULLONG_MAX - start; + + end =3D start + size; + + for (i =3D 0; i < table->nr_entries; i++) { + struct e820_entry *entry =3D &table->entries[i]; + u64 entry_end =3D entry->addr + entry->size; + + if (updater->should_update(entry, data)) { + /* Range completely covers entry */ + if (entry->addr >=3D start && entry_end <=3D end) { + updated_size +=3D entry->size; + if (updater->update !=3D NULL) + updater->update(entry, data); + /* Entry completely covers range */ + } else if (start > entry->addr && end < entry_end) { + /* Resize current entry */ + entry->size =3D start - entry->addr; + + if (updater->new !=3D NULL) + /* 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 +=3D size; + } else { + updated_size +=3D + __e820__handle_intersected_range_update(table, start, size, + entry, updater, data); + } + } + } + + return updated_size; +} + static u64 __init __e820__range_update(struct e820_table *table, u64 start, u64 size, enum e= 820_type old_type, enum e820_type new_type) { --=20 2.30.2 From nobody Mon Jun 15 10:48:20 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id BBE98C43217 for ; Fri, 29 Apr 2022 20:18:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1380644AbiD2UVe (ORCPT ); Fri, 29 Apr 2022 16:21:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52132 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1380651AbiD2UV3 (ORCPT ); Fri, 29 Apr 2022 16:21:29 -0400 Received: from mail-oa1-x36.google.com (mail-oa1-x36.google.com [IPv6:2001:4860:4864:20::36]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 069CECD32F for ; Fri, 29 Apr 2022 13:18:07 -0700 (PDT) Received: by mail-oa1-x36.google.com with SMTP id 586e51a60fabf-e67799d278so9186991fac.11 for ; Fri, 29 Apr 2022 13:18:07 -0700 (PDT) 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=PLuObBmPkIWHGGIa0YgrZ0Lq4zFfhIdvg//A8xn7ggo=; b=bDqZaABFMgbCLkZSOOaIqWXwNxKq7mseURB3Uho4ueCz1dK5Sg5oiQmKB6FATHblrP jxnZ4zLyyzM4un446F22ja4cDAw0P+SRSXKTENRKrNSkVExlDHbFvvsDAMKzQahVqNEe X+hCpEFt+iz78gB+HqjFukCBu2GzDKsy4ueixp7I0CdFYyCkriviEhAVL8OZIBD7ik2q 0GKnnbWIfLfaHOTPuDSlf0Cixh6bNynHXSQwTNdfmXYveHNXxLJZb6uvzHRaKx5skZRH 1KxzopzMkI8RB0enn0HThdgeAIc2RAcq2FZit5fIVYtbtANMzb4NCuRHfNtkZrYF3bON EUCQ== 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=PLuObBmPkIWHGGIa0YgrZ0Lq4zFfhIdvg//A8xn7ggo=; b=D1vdPEf+KuLIrSCHi02CTLVxkYtsf/ztZm9V71ZAZZ2XFMEKo0PMIAqW0M+r0hzS8n zdbyZK6qs3GvkM2KFIOxNZuZLbHCHghef/Jgvcj/nm+gMmykcqWoBRkl9z/iz2nFwP7W hPT8tLoWBzuuiTjEGYo2ZpyVuh+I+sf9f74xhWY5uMCJ7lCAgsX9MhWKm52GmPcUNBBM 8ZBgn1jmVQdh6eiBgUMDMwWKamfaGgeoSUYzGGFuCrETx4ABHvHoDHZlL5Vn4WS2Y2ft jB5QhU1jm02ZGyMfZ5YVA7w2YI7KDiA6wvywsPQPe+oyOfwecMHt3jMoyakqJ0rSH7tI 3mKQ== X-Gm-Message-State: AOAM530gjMTwcJSCzX0IgIrnZVJe+z53RGaaiRI5VX23130BZ8nebfiL 7od8n37Qr/AssI4HmabsZR0fXHtiykYFyOeeHm/4qteRn9dE2jggk4C+BgEnUpS0s4jlwEkRpjb dtFE0J7BUfvCiwcfUC1ZOL/ZF3nFrZSixkkuilsUcmy6yh+2zCfcKSh/zQQoM6ErysGVthHAnrR iNLwWEW1rewjkd40fCOCc= X-Google-Smtp-Source: ABdhPJzrZ8Kfl4T49yQr7FGvWCo8iE92bI2DDqgrilNpiLxjJDpWs/nerrD+omBLCh+QcoMFJfgdCQ== X-Received: by 2002:a05:6870:708a:b0:e9:9349:9410 with SMTP id v10-20020a056870708a00b000e993499410mr2067692oae.265.1651263487078; Fri, 29 Apr 2022 13:18:07 -0700 (PDT) Received: from localhost ([181.97.174.128]) by smtp.gmail.com with ESMTPSA id q2-20020a056808200200b00325cda1ff9dsm71731oiw.28.2022.04.29.13.18.01 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 29 Apr 2022 13:18:06 -0700 (PDT) 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 v8 4/8] x86/e820: Refactor __e820__range_update Date: Fri, 29 Apr 2022 17:17:13 -0300 Message-Id: <20220429201717.1946178-5-martin.fernandez@eclypsium.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220429201717.1946178-1-martin.fernandez@eclypsium.com> References: <20220429201717.1946178-1-martin.fernandez@eclypsium.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Refactor __e820__range_update with the introduction of e820_type_updater_data, indented to be used as the void pointer in the e820_entry_updater callbacks, and the implementation of the callbacks to perform the update of the type in a range of a e820_table. Signed-off-by: Martin Fernandez --- arch/x86/kernel/e820.c | 119 +++++++++++++++++++++-------------------- 1 file changed, 62 insertions(+), 57 deletions(-) diff --git a/arch/x86/kernel/e820.c b/arch/x86/kernel/e820.c index e0fa3ab755a5..36a22c0a2199 100644 --- a/arch/x86/kernel/e820.c +++ b/arch/x86/kernel/e820.c @@ -586,80 +586,85 @@ __e820__handle_range_update(struct e820_table *table, return updated_size; } =20 -static u64 __init -__e820__range_update(struct e820_table *table, u64 start, u64 size, enum e= 820_type old_type, enum e820_type new_type) -{ - u64 end; - unsigned int i; - u64 real_updated_size =3D 0; - - BUG_ON(old_type =3D=3D new_type); - - if (size > (ULLONG_MAX - start)) - size =3D ULLONG_MAX - start; +/* + * Type helper for the e820_entry_updater callbacks. + */ +struct e820_type_updater_data { + enum e820_type old_type; + enum e820_type new_type; +}; =20 - end =3D start + size; - printk(KERN_DEBUG "e820: update [mem %#010Lx-%#010Lx] ", start, end - 1); - e820_print_type(old_type); - pr_cont(" =3D=3D> "); - e820_print_type(new_type); - pr_cont("\n"); +static bool __init type_updater__should_update(const struct e820_entry *en= try, + const void *data) +{ + const struct e820_type_updater_data *type_updater_data =3D data; =20 - for (i =3D 0; i < table->nr_entries; i++) { - struct e820_entry *entry =3D &table->entries[i]; - u64 final_start, final_end; - u64 entry_end; + return entry->type =3D=3D type_updater_data->old_type; +} =20 - if (entry->type !=3D old_type) - continue; +static void __init type_updater__update(struct e820_entry *entry, + const void *data) +{ + const struct e820_type_updater_data *type_updater_data =3D data; =20 - entry_end =3D entry->addr + entry->size; + entry->type =3D type_updater_data->new_type; +} =20 - /* Completely covered by new range? */ - if (entry->addr >=3D start && entry_end <=3D end) { - entry->type =3D new_type; - real_updated_size +=3D entry->size; - continue; - } +static void __init type_updater__new(struct e820_table *table, u64 new_sta= rt, + u64 new_size, + const struct e820_entry *original, + const void *data) +{ + const struct e820_type_updater_data *type_updater_data =3D data; =20 - /* 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 =3D start - entry->addr; - real_updated_size +=3D size; - continue; - } + __e820__range_add(table, new_start, new_size, + type_updater_data->new_type); +} =20 - /* Partially covered: */ - final_start =3D max(start, entry->addr); - final_end =3D min(end, entry_end); - if (final_start >=3D final_end) - continue; +static u64 __init __e820__range_update(struct e820_table *table, u64 start, + u64 size, enum e820_type old_type, + enum e820_type new_type) +{ + struct e820_entry_updater updater =3D { + .should_update =3D type_updater__should_update, + .update =3D type_updater__update, + .new =3D type_updater__new + }; =20 - __e820__range_add(table, final_start, final_end - final_start, new_type); + struct e820_type_updater_data data =3D { + .old_type =3D old_type, + .new_type =3D new_type + }; =20 - real_updated_size +=3D final_end - final_start; + BUG_ON(old_type =3D=3D new_type); =20 - /* - * Left range could be head or tail, so need to update - * its size first: - */ - entry->size -=3D final_end - final_start; - if (entry->addr < final_start) - continue; + printk(KERN_DEBUG "e820: update [mem %#018Lx-%#018Lx] ", start, + start + size - 1); + e820_print_type(old_type); + pr_cont(" =3D=3D> "); + e820_print_type(new_type); + pr_cont("\n"); =20 - entry->addr =3D final_end; - } - return real_updated_size; + return __e820__handle_range_update(table, start, size, &updater, &data); } =20 -u64 __init e820__range_update(u64 start, u64 size, enum e820_type old_type= , enum e820_type new_type) +/* + * Update type of addresses in [@start, @start + @size) from @old_type + * to @new_type in e820_table. + */ +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); } =20 -static u64 __init e820__range_update_kexec(u64 start, u64 size, enum e820_= type old_type, enum e820_type new_type) +/* + * Update type of addresses in [@start, @start + @size) from @old_type + * to @new_type in e820_table_kexec. + */ +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); } --=20 2.30.2 From nobody Mon Jun 15 10:48:20 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9E3E0C433F5 for ; Fri, 29 Apr 2022 20:18:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1380659AbiD2UVj (ORCPT ); Fri, 29 Apr 2022 16:21:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52214 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1380648AbiD2UVe (ORCPT ); Fri, 29 Apr 2022 16:21:34 -0400 Received: from mail-oa1-x2d.google.com (mail-oa1-x2d.google.com [IPv6:2001:4860:4864:20::2d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A1F37CA0FE for ; Fri, 29 Apr 2022 13:18:15 -0700 (PDT) Received: by mail-oa1-x2d.google.com with SMTP id 586e51a60fabf-e922e68b0fso9244880fac.1 for ; Fri, 29 Apr 2022 13:18:15 -0700 (PDT) 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=rnE47zkhbqWUJCOF0oFEixfkiu1ztx/JWB/tGri5YXI=; b=a9eja63UZsOz8EDkYCqgng2hYRK9WULHVEyppQTAGE6xiRQ32CQn6CFl2j4IpY4D/A T/SILFH1b9jXsHfFF5EPdcHdjPOQ9tzsyECf/C+2FrfimR8jZwXFDAoZPcRS1M6y+YZU wqaNac66Z0pUqdAQgcZpXr/lksm1XFEayCYcjPohjvqxfH61LHyxYLHEC3VoBP/SSzw6 +d1offiCPqehXEje4lXOw68Qk4XWAXNcjzTeC5lZsbpYcAnKW0lB9/YmgZkZTuGojgMQ nFGLT6sdanVPtlGuzu30Ol+fmyRWCBmYGhBBv1OwuLHUY5MzLq8ev+Bm6J9a8H06rP/0 QQbA== 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=rnE47zkhbqWUJCOF0oFEixfkiu1ztx/JWB/tGri5YXI=; b=JY8KyzeeHC08vH59U5ZBbGTzs+dREYnzoxXXZYAkdev96zJyS+Ic0g+xGxUABMy0+l TlAy9GE3RTdFRhg/WS/Bf2rps6vqgVHlV9FUFagIpsAxCmP4OJ39Hypnz/mIlHKoZbQJ BP6dal+4QPL7Ddvf4hyY62QFCSyhpiZxBt/mJc1AFoVBEIWVc7KYfm7xpUmPkU/Lk8xN 1GYEHhmNLacwVhupRPeiA5s9p+i5Y9xP7u9VT6pE1pR+sorWjkyp+Fy70SXaf1t+PQVp dBVD0jC0gY+mLfG+VbJ5k3/TEZ+yYzBb6k3E5gr/VXHJ1F6LPI4FPTJ7qCD0TsjuyyqD CoNA== X-Gm-Message-State: AOAM532BUxsDTxQ/snPE57Ysq0QMVF8Xhh3ryK1BxN/SZ2VK50csOc/N iYhEMzZx9m+Nf23ksnaXc06bXO3hOedD/RY4lb1+0yOUXHOUf5H498gboR9E5u+etGylWx4u/Lj I2HU8V7AfDXvcX4GNR61JTXCaNtj9IfxOWLwDm5pBYvtyuI+2Zm54cA6f3kCpNiAAiLbgltA+C6 nSdkWSN6kRllU2jIQS9yU= X-Google-Smtp-Source: ABdhPJxofcGdrQu7m+cL6vOtGym7ome4qtIu2YMefSz1hnwtbmQ4+6AReZ8mcS6ebQmZ4VwoWkIanA== X-Received: by 2002:a05:6871:28a:b0:e9:16de:c70d with SMTP id i10-20020a056871028a00b000e916dec70dmr477455oae.113.1651263494682; Fri, 29 Apr 2022 13:18:14 -0700 (PDT) Received: from localhost ([181.97.174.128]) by smtp.gmail.com with ESMTPSA id p4-20020a0568301d4400b0060603221248sm85961oth.24.2022.04.29.13.18.09 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 29 Apr 2022 13:18:14 -0700 (PDT) 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 v8 5/8] x86/e820: Refactor e820__range_remove Date: Fri, 29 Apr 2022 17:17:14 -0300 Message-Id: <20220429201717.1946178-6-martin.fernandez@eclypsium.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220429201717.1946178-1-martin.fernandez@eclypsium.com> References: <20220429201717.1946178-1-martin.fernandez@eclypsium.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Refactor e820__range_remove with the introduction of e820_remover_data, indented to be used as the void pointer in the e820_entry_updater callbacks, and the implementation of the callbacks remove a range in the e820_table. Signed-off-by: Martin Fernandez --- arch/x86/kernel/e820.c | 94 ++++++++++++++++++------------------------ 1 file changed, 41 insertions(+), 53 deletions(-) diff --git a/arch/x86/kernel/e820.c b/arch/x86/kernel/e820.c index 36a22c0a2199..0e5aa13ebdb8 100644 --- a/arch/x86/kernel/e820.c +++ b/arch/x86/kernel/e820.c @@ -669,66 +669,54 @@ static u64 __init e820__range_update_kexec(u64 start,= u64 size, return __e820__range_update(e820_table_kexec, start, size, old_type, new_= type); } =20 -/* 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) -{ - int i; - u64 end; - u64 real_removed_size =3D 0; - - if (size > (ULLONG_MAX - start)) - size =3D ULLONG_MAX - start; - - end =3D start + size; - printk(KERN_DEBUG "e820: remove [mem %#010Lx-%#010Lx] ", start, end - 1); - if (check_type) - e820_print_type(old_type); - pr_cont("\n"); - - for (i =3D 0; i < e820_table->nr_entries; i++) { - struct e820_entry *entry =3D &e820_table->entries[i]; - u64 final_start, final_end; - u64 entry_end; - - if (check_type && entry->type !=3D old_type) - continue; +/* + * Type helper for the e820_entry_updater callbacks. + */ +struct e820_remover_data { + enum e820_type old_type; + bool check_type; +}; =20 - entry_end =3D entry->addr + entry->size; +static bool __init remover__should_update(const struct e820_entry *entry, + const void *data) +{ + const struct e820_remover_data *remover_data =3D data; =20 - /* Completely covered? */ - if (entry->addr >=3D start && entry_end <=3D end) { - real_removed_size +=3D entry->size; - memset(entry, 0, sizeof(*entry)); - continue; - } + return !remover_data->check_type || + entry->type =3D=3D remover_data->old_type; +} =20 - /* Is the new range completely covered? */ - if (entry->addr < start && entry_end > end) { - e820__range_add(end, entry_end - end, entry->type); - entry->size =3D start - entry->addr; - real_removed_size +=3D size; - continue; - } +static void __init remover__update(struct e820_entry *entry, const void *d= ata) +{ + memset(entry, 0, sizeof(*entry)); +} =20 - /* Partially covered: */ - final_start =3D max(start, entry->addr); - final_end =3D min(end, entry_end); - if (final_start >=3D final_end) - continue; +/* + * Remove [@start, @start + @size) from e820_table. If @check_type is + * true remove only entries with type @old_type. + */ +u64 __init e820__range_remove(u64 start, u64 size, enum e820_type old_type, + bool check_type) +{ + struct e820_entry_updater updater =3D { + .should_update =3D remover__should_update, + .update =3D remover__update, + .new =3D NULL + }; =20 - real_removed_size +=3D final_end - final_start; + struct e820_remover_data data =3D { + .check_type =3D check_type, + .old_type =3D old_type + }; =20 - /* - * Left range could be head or tail, so need to update - * the size first: - */ - entry->size -=3D final_end - final_start; - if (entry->addr < final_start) - continue; + printk(KERN_DEBUG "e820: remove [mem %#018Lx-%#018Lx] ", start, + start + size - 1); + if (check_type) + e820_print_type(old_type); + pr_cont("\n"); =20 - entry->addr =3D final_end; - } - return real_removed_size; + return __e820__handle_range_update(e820_table, start, size, &updater, + &data); } =20 void __init e820__update_table_print(void) --=20 2.30.2 From nobody Mon Jun 15 10:48:20 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 79E9FC433F5 for ; Fri, 29 Apr 2022 20:18:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1380672AbiD2UVu (ORCPT ); Fri, 29 Apr 2022 16:21:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52372 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1380665AbiD2UVq (ORCPT ); Fri, 29 Apr 2022 16:21:46 -0400 Received: from mail-oa1-x34.google.com (mail-oa1-x34.google.com [IPv6:2001:4860:4864:20::34]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F097FD5E89 for ; Fri, 29 Apr 2022 13:18:22 -0700 (PDT) Received: by mail-oa1-x34.google.com with SMTP id 586e51a60fabf-d6e29fb3d7so9198880fac.7 for ; Fri, 29 Apr 2022 13:18:22 -0700 (PDT) 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=YiEyrkk7zAfWqeyIGUzFkmdI1NMExWAuPYFixtI2XBY=; b=C8/S+PsjTKCTWs7QPbmskNwv0xJZf36RcyWfiPYmgT8PUdBZ0LEt7cVJsQ3qusPUoF Ro4owjn5KseQ6TVU9ePDoGObhGhAcy57k9X8gFCm62pOrVhaxG2OzRgRQTyCk6gZlSJ/ Ysk5lQ2z9WTQ29acnkhjhROEJG8RNCjJfXrBWIV3snlVPHOckkq65ntZadqn59UmgVaN TaH55DhJ/W20T529+4pOBkro+wVtKCEjP9K3Ry7qObQT/YeMJ0iZfaGNqVKSwqKEQ2JZ b111PVjSTWJ4BQ4Tx01B/lbF0nVywroAZowdHYnxd/WL8u4dICC9gOnOd9epHshrm8Ay MUQg== 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=YiEyrkk7zAfWqeyIGUzFkmdI1NMExWAuPYFixtI2XBY=; b=1skltqQMrDjCKxn8E9I+hwx2iXVDwzVXNnqJx78aYcygL+ZOIRzQo25xbDvtjj8tm2 MIJ0PJQJSgYbq2lINXOekNL2BbajaR7CrVWW5Jv4WR4PZgK1rdoXT3a6getq4If/JqLv nvj2bbOoLsA70VOEqHveRy36eFM0BKpohd2Qq3VmKGMf+is2egpb+9vP4MwXTvCEhVfT 7lnYWnXfLuix6fD4wGTuAZH3rU4i4+O3p2aTH4C2WECKMXNvCGYFq1YAYsBThaqrddbo 7NemINoaUpXzFulK9C/iFFcg+GhmnYCDu1X/XV757+0pvwZuGraoilukgUB3ftoPXg95 QNNg== X-Gm-Message-State: AOAM533gOAoUu2gCgw917mhZgytubjsyugQ8Z15R00KHd/tip4YGuHw3 11OnrGywa9E+EQ60/cA8whQcormcG4myOlmmXBbeM2cDR8YpV88A2OgDChjKkPOwaGt90H2UKTt BDbqVWEyLphG6GA318PgFsNctaREr1Nzm+6RsN5k/DBVxkFZXqgyHmcQzRWNpkzaYG+6wmbKzpr jzh7e39sPlppw6D92d5Bc= X-Google-Smtp-Source: ABdhPJySjyVhXI/q/lRO3rvx7oHsGnM1batE9Jn9hgdgQQAem2lB2nDHyoKkEPlP1FFibHKjQkq49A== X-Received: by 2002:a05:6870:c108:b0:e9:49d9:1666 with SMTP id f8-20020a056870c10800b000e949d91666mr464920oad.243.1651263502055; Fri, 29 Apr 2022 13:18:22 -0700 (PDT) Received: from localhost ([181.97.174.128]) by smtp.gmail.com with ESMTPSA id m4-20020a056870030400b000e686d13890sm3310318oaf.42.2022.04.29.13.18.16 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 29 Apr 2022 13:18:21 -0700 (PDT) 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 v8 6/8] x86/e820: Tag e820_entry with crypto capabilities Date: Fri, 29 Apr 2022 17:17:15 -0300 Message-Id: <20220429201717.1946178-7-martin.fernandez@eclypsium.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220429201717.1946178-1-martin.fernandez@eclypsium.com> References: <20220429201717.1946178-1-martin.fernandez@eclypsium.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Add a new enum for crypto capabilities. I choosed an enum instead of a boolean for more visibility in the code and because maybe in the future we would like to track from where the cryptographic capabilities comes (in this case, the EFI memmap). Add a new member in e820_entry to hold this new enum. 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 | 88 +++++++++++++++++++++++++++++-- 3 files changed, 93 insertions(+), 8 deletions(-) diff --git a/arch/x86/include/asm/e820/api.h b/arch/x86/include/asm/e820/ap= i.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 e82= 0_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_typ= e, enum e820_type new_type); extern u64 e820__range_remove(u64 start, u64 size, enum e820_type old_typ= e, bool check_type); +extern u64 e820__range_set_crypto_capable(u64 start, u64 size); =20 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 =3D 128, }; =20 +enum e820_crypto_capabilities { + E820_NOT_CRYPTO_CAPABLE =3D 0, + E820_CRYPTO_CAPABLE =3D 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)); =20 /* diff --git a/arch/x86/kernel/e820.c b/arch/x86/kernel/e820.c index 0e5aa13ebdb8..dade59758b9f 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 =3D table->nr_entries; =20 @@ -176,12 +178,13 @@ static void __init __e820__range_add(struct e820_tabl= e *table, u64 start, u64 si table->entries[x].addr =3D start; table->entries[x].size =3D size; table->entries[x].type =3D type; + table->entries[x].crypto_capable =3D crypto_capable; table->nr_entries++; } =20 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); } =20 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); =20 e820_print_type(e820_table->entries[i].type); + if (e820_table->entries[i].crypto_capable =3D=3D 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; =20 /* 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 =3D 0; /* Index for creating new map entries */ last_type =3D 0; /* Start with undefined memory type */ last_addr =3D 0; /* Start with 0 as last starting address */ + last_crypto =3D E820_NOT_CRYPTO_CAPABLE; =20 /* Loop through change-points, determining effect on the new map: */ for (chg_idx =3D 0; chg_idx < chg_nr; chg_idx++) { @@ -388,13 +395,19 @@ int __init e820__update_table(struct e820_table *tabl= e) * 1=3Dusable, 2,3,4,4+=3Dunusable) */ current_type =3D 0; + current_crypto =3D E820_CRYPTO_CAPABLE; for (i =3D 0; i < overlap_entries; i++) { + if (overlap_list[i]->crypto_capable < current_crypto) + current_crypto =3D overlap_list[i]->crypto_capable; + if (overlap_list[i]->type > current_type) current_type =3D overlap_list[i]->type; } =20 /* Continue building up new map based on this information: */ - if (current_type !=3D last_type || e820_nomerge(current_type)) { + if (current_type !=3D last_type || + current_crypto !=3D last_crypto || + e820_nomerge(current_type)) { if (last_type !=3D 0) { new_entries[new_nr_entries].size =3D change_point[chg_idx]->addr - las= t_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 !=3D 0) { new_entries[new_nr_entries].addr =3D change_point[chg_idx]->addr; new_entries[new_nr_entries].type =3D current_type; + new_entries[new_nr_entries].crypto_capable =3D current_crypto; + last_addr =3D change_point[chg_idx]->addr; } last_type =3D current_type; + last_crypto =3D current_crypto; } } =20 @@ -572,7 +588,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); =20 updated_size +=3D size; } else { @@ -618,7 +635,8 @@ static void __init type_updater__new(struct e820_table = *table, u64 new_start, const struct e820_type_updater_data *type_updater_data =3D data; =20 __e820__range_add(table, new_start, new_size, - type_updater_data->new_type); + type_updater_data->new_type, + original->crypto_capable); } =20 static u64 __init __e820__range_update(struct e820_table *table, u64 start, @@ -719,6 +737,64 @@ u64 __init e820__range_remove(u64 start, u64 size, enu= m e820_type old_type, &data); } =20 +static bool __init crypto_updater__should_update(const struct e820_entry *= entry, + const void *data) +{ + const enum e820_crypto_capabilities *crypto_capable =3D data; + + return *crypto_capable !=3D entry->crypto_capable; +} + +static void __init crypto_updater__update(struct e820_entry *entry, + const void *data) +{ + const enum e820_crypto_capabilities *crypto_capable =3D data; + + entry->crypto_capable =3D *crypto_capable; +} + +static void __init crypto_updater__new(struct e820_table *table, u64 new_s= tart, + u64 new_size, + const struct e820_entry *original, + const void *data) +{ + const enum e820_crypto_capabilities *crypto_capable =3D data; + + __e820__range_add(table, new_start, new_size, original->type, *crypto_cap= able); +} + +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 =3D { + .should_update =3D crypto_updater__should_update, + .update =3D crypto_updater__update, + .new =3D crypto_updater__new + }; + + printk(KERN_DEBUG "e820: crypto update [mem %#018Lx-%#018Lx]", start, + start + size - 1); + pr_cont(" =3D=3D> "); + if (crypto_capable =3D=3D 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, + &crypto_capable); +} + +/* + * Set %E820_CRYPTO_CAPABLE to [@start, @start + @size) in e820_table. + */ +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)) @@ -1461,6 +1537,8 @@ void __init e820__memblock_setup(void) continue; =20 memblock_add(entry->addr, entry->size); + if (entry->crypto_capable =3D=3D E820_CRYPTO_CAPABLE) + memblock_mark_crypto_capable(entry->addr, entry->size); } =20 /* Throw away partial pages: */ --=20 2.30.2 From nobody Mon Jun 15 10:48:20 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id DABE4C433EF for ; Fri, 29 Apr 2022 20:18:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1380677AbiD2UVy (ORCPT ); Fri, 29 Apr 2022 16:21:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52428 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1380648AbiD2UVt (ORCPT ); Fri, 29 Apr 2022 16:21:49 -0400 Received: from mail-oa1-x33.google.com (mail-oa1-x33.google.com [IPv6:2001:4860:4864:20::33]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6B5F9D5E86 for ; Fri, 29 Apr 2022 13:18:30 -0700 (PDT) Received: by mail-oa1-x33.google.com with SMTP id 586e51a60fabf-d39f741ba0so9198399fac.13 for ; Fri, 29 Apr 2022 13:18:30 -0700 (PDT) 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=BvRseK4OxKadqHo12yNsfQ37dcGHfwzf2NABqSmUh4dW14GFsCZ9lNoqxOaprhkpIt RD4NewzP3DKtjv3EXexgwnPx9/D0SPPyIwy17W+pLoOmfBWGYsEfnwEcCoqicMULz7s7 xgniOuDmDzPksbBOjq8y3XGwMK8aG73pFr7TQbKHrtYnU5S3KN9v9vUdtdfvpBBLl6pG /+bPXmuJin8w9zaqauKrTz+CbnNMDCN4Mue5L9B5DMPEnXDbpbTYpDkcgT11Ay11MmVy mMF1NeIq5CeN9ZfsuwJpnFNuT3ReobQHJCtBWNx1hbcv8Gc1Y6Hc7fGasrUup9XPtJfy F/uQ== 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=7uQsczohciX432WacN9D4w7x5JspIAMuPp66g7xxlUUcli8hhoCOPgFDUfm8hoGATN hotUQqGoP9g2NJHcKmdQLhPcPn0mP/aGuarJRsrXENhpIVa4orbIJk1Dd02vxVvlavmi axqDPII2UuURt26j2ZOcNBeF9qLIJVtwApro/nmiAevb0vqVqlQHSJRa/87/1O9Jwzn2 OK8zGp8r3UsPkBOu/tnvVP41ggZCSzG8c9kHdRgGgDghzJ3Z5feAKKRIYQOkXgP8CJkI UIBxlCv2iUVEUwapTEPD4WxPmdD6XhFgv1zhWn42gKYxQjA6gfMDNHl/KFiiMlQAI7V3 PLbA== X-Gm-Message-State: AOAM532eqLAB9kehfW7Ktni/3KgaHEckkj8gibMlkIPjS7uLFZ8SrG4Y 7o6o8yPRyXtpttgbi7PlYlMp6GpZIp0BTj4stsnbLuITqT50792LjQ4aThoV/UmlsNMd85UB4dI 0CTVUNL+0FDrRtiSSLj9DRs6ijEjSzoVrvptVOGvfL3M3DczpucWJMwNHLnKELS3z5Sq83rL+Dm PYfMrBchnkyWj9/mVlFzE= X-Google-Smtp-Source: ABdhPJyOyF9wWfauAE59YnbOyJwYqFYAmT+C3XLQZ8NncYYCmkw0OS1bL4ZQfw7qqScQqcZUkl025g== X-Received: by 2002:a05:6870:b023:b0:db:78e:7197 with SMTP id y35-20020a056870b02300b000db078e7197mr2135132oae.36.1651263509453; Fri, 29 Apr 2022 13:18:29 -0700 (PDT) Received: from localhost ([181.97.174.128]) by smtp.gmail.com with ESMTPSA id i6-20020a9d6506000000b0060603221255sm84923otl.37.2022.04.29.13.18.24 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 29 Apr 2022 13:18:29 -0700 (PDT) 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 v8 7/8] x86/efi: Mark e820_entries as crypto capable from EFI memmap Date: Fri, 29 Apr 2022 17:17:16 -0300 Message-Id: <20220429201717.1946178-8-martin.fernandez@eclypsium.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220429201717.1946178-1-martin.fernandez@eclypsium.com> References: <20220429201717.1946178-1-martin.fernandez@eclypsium.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" 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) } =20 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_tab= le_type_t *arch_tables) return ret; } =20 +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 >=3D E820_MAX_ENTRIES) { + pr_err_once("E820 table is not large enough to fit EFI memmap; not marki= ng 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(); =20 + /* + * 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(); } --=20 2.30.2 From nobody Mon Jun 15 10:48:20 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9F8E9C433EF for ; Fri, 29 Apr 2022 20:18:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1380678AbiD2UWD (ORCPT ); Fri, 29 Apr 2022 16:22:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52546 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1380676AbiD2UV5 (ORCPT ); Fri, 29 Apr 2022 16:21:57 -0400 Received: from mail-ot1-x32f.google.com (mail-ot1-x32f.google.com [IPv6:2607:f8b0:4864:20::32f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1DB46D5E99 for ; Fri, 29 Apr 2022 13:18:38 -0700 (PDT) Received: by mail-ot1-x32f.google.com with SMTP id r14-20020a9d750e000000b00605446d683eso5840413otk.10 for ; Fri, 29 Apr 2022 13:18:38 -0700 (PDT) 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=OkG/F7+kBMNIWCowVtEazmyHhBwO2DxtbJbfhKP712A=; b=CFVomb+9tyTJtBjEhdpiH1zhwooGhEZao10gTFdxqakjE50a70JiLTBnTH7LkExN+V oc+UNf8vfz0q7LVdjvbwtCy8BnniQSsjA83H+fwEwh99U6UXRuClAcfUpV3bpAAc9umN hs0O4wqgUFbbSCgB/tJAtF1o+ndijINfhWVCNdR3bBValgZ8Z1AMvT3jzPCO56a+EUVj eQw67Azpa5Iv4+Ywhr9kjCjFBO3QXN4+XwHcJopDuXewZTbZD9+wbmGww65HIywNbytB K9o9k/DmPxRb4TRXsls4NQStEslTiFL+j1mwuEZbxJ6bjwWQEk+zv+NX6GgbMX2SInsf YuNQ== 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=OkG/F7+kBMNIWCowVtEazmyHhBwO2DxtbJbfhKP712A=; b=2Oy/nlKVjX53D+Ci1JbOlzRDRJm6HZJhBha9Cy3QlMxrSm87zde/vibYlkpzlKDtpP 2iGKgD7ycj5DbksuxvV/egwqryWjKmLRR0M+ebUq/+LxU3OoPtXJOSejCc2w9n7I/DsU NHTjBVToX4tMfznwtFWqhgwArhdu4zRdpUMLFk5xhrI30WN/zxGNwYG0XTZJVXCiezUe CFCk5iw2hVyOkPturXguB4Em4vnhn+guHaWm+qKMKjArNglomdD7JRth26ZJAhkQQVae uU3VHvh6kFWKLGn11ZW49b5V1cGEoTapa3kELSLfjDmt/LN/ORFlh8+9sOuO+82Embe/ IiUg== X-Gm-Message-State: AOAM530EkgduK0D87sW7IiTLRU4Q5dDDVBAjb57wwcwZnAk010eurPW7 AO4CtwwXgbXU/QE5H0SPTv7ESBqk4+cRBlsWzy9zLzpGerv5GShGFGoSjdyWHzndYjdyD9FAFBf 2sGujZN3lfPJta4vRQUrYRKSTMjutn+/kPNI9iafkbuwlieKPRWLx+UFqdH2U1a4H/9njY3naHo v+90VIfqmAuRz/AIEIyg4= X-Google-Smtp-Source: ABdhPJwfcMjRvkAXKIDIz9yJyzv/MGd9Qc+uZBZYcbTg+2X+d01VGqB1uGOCZg191g8uEunpkZerYw== X-Received: by 2002:a9d:37cb:0:b0:5cc:7a51:c984 with SMTP id x69-20020a9d37cb000000b005cc7a51c984mr409643otb.98.1651263517007; Fri, 29 Apr 2022 13:18:37 -0700 (PDT) Received: from localhost ([181.97.174.128]) by smtp.gmail.com with ESMTPSA id t13-20020a05683014cd00b0060603221245sm79060otq.21.2022.04.29.13.18.31 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 29 Apr 2022 13:18:36 -0700 (PDT) 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 v8 8/8] drivers/node: Show in sysfs node's crypto capabilities Date: Fri, 29 Apr 2022 17:17:17 -0300 Message-Id: <20220429201717.1946178-9-martin.fernandez@eclypsium.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220429201717.1946178-1-martin.fernandez@eclypsium.com> References: <20220429201717.1946178-1-martin.fernandez@eclypsium.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Show in each node in sysfs if its memory is able to do be encrypted by the CPU; on EFI systems: 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/A= BI/testing/sysfs-devices-node new file mode 100644 index 000000000000..0e95420bd7c5 --- /dev/null +++ b/Documentation/ABI/testing/sysfs-devices-node @@ -0,0 +1,10 @@ +What: /sys/devices/system/node/nodeX/crypto_capable +Date: April 2022 +Contact: Martin Fernandez +Users: fwupd (https://fwupd.org) +Description: + This value is 1 if all system memory in this node is + capable of being protected with the CPU's memory + cryptographic capabilities. It is 0 otherwise. + On EFI systems the node will be marked with + EFI_MEMORY_CPU_CRYPTO. \ No newline at end of file diff --git a/drivers/base/node.c b/drivers/base/node.c index ec8bb24a5a22..1df15ea03c27 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); =20 +static ssize_t crypto_capable_show(struct device *dev, + struct device_attribute *attr, char *buf) +{ + struct pglist_data *pgdat =3D 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[] =3D { &dev_attr_meminfo.attr, &dev_attr_numastat.attr, &dev_attr_distance.attr, &dev_attr_vmstat.attr, + &dev_attr_crypto_capable.attr, NULL }; =20 --=20 2.30.2