From nobody Sun Apr 26 10:53:24 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 654A3C433EF for ; Mon, 4 Jul 2022 13:59:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234530AbiGDN7E (ORCPT ); Mon, 4 Jul 2022 09:59:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37166 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233322AbiGDN7A (ORCPT ); Mon, 4 Jul 2022 09:59:00 -0400 Received: from mail-vs1-xe2e.google.com (mail-vs1-xe2e.google.com [IPv6:2607:f8b0:4864:20::e2e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8F6D4959E for ; Mon, 4 Jul 2022 06:58:53 -0700 (PDT) Received: by mail-vs1-xe2e.google.com with SMTP id 126so9051681vsq.13 for ; Mon, 04 Jul 2022 06:58:53 -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=V55waJR1QuKM8kC/rFEuxJwFmNLdmPKax7+zW9/siL7Mj36HUtopitwFYdMlAFbeEs rJWfeTLZaAEsZjfihwA9mJ7xH9DS6H9oWLlqTnMtLoS/+23JMDhQ3IeR7ap5oaFtTKxK Y+I83AZEsaJ4Ay6DnahMYHrQ3Gb04a7ON/+7oqwxrHjP8z+A6KzcYQNYXByQHkOWPqg2 FMPJkOHXrsJy7Eom5pARQ4bPJtkhaMhDPV5VhmeydbYuyBO61FHK10EdQ0vYkxE/OsLv t0rVB5AnOjfnQIbOlZIGo+FUsNZbVLhmEYJK9HYBt89yoXq1Mt6q8egiAwbnhj3Hep95 8kTw== 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=eaJLsDqtU7DSsljvM3XSL+jBe0W1PMF/NMPykLsAN4H1+l5NrGpv7ZXj8C1matt0SP Uby3xfHtgSm87YwlZQjLoKVwoWw8kt1N3iWME19qYcGHAESza3ez9pyn6qS6g6GicIAS vAZwgShgMUyh0ZeljzVzSTKtdJMSaIDfQNWGOY0xa8b2GJXuinWrwu92YDQx6ADW4RFw KX4NnkISJ7nK4+uEPktCzNpyk/BBc1ChhpIHfE0pN9FlAmIUHn6qXtXkcNd32/+ePo69 YRYmfwo5WTksNsAwyNF7O09CtsQkTu98h2a0sEq/SvKUnmQBvtJny+lxjjwnbtiO6/g4 ewXQ== X-Gm-Message-State: AJIora/dT/24Gfu/97CNkZVSHp2YE0DwRVy5Ql6hHeSRb7kqm0/3xe1n yqkoTey/Lma8u9PWR/UImotlYLaBsEpZreZ2Jvc3Bqzud56JLUhh5c9cU7rbcEKyMCAlbE4g1y8 LUCwOSaCmi9SqYuhOXGHpbsK2/fisCuwJThkfJFJdEtn2lXbuNKi7WOQJeEkf4wLoT2IzD/yrVZ CjNOGVsRPyBdkpuAGM/1k= X-Google-Smtp-Source: AGRyM1uu2u3rbg5Giv2gmMbrQfmLY/m5Z5qJo0bY5VChxGRJJIApXm7e/0jrs7sHJbYJu65GFpqcqw== X-Received: by 2002:a67:43c4:0:b0:354:49cf:f663 with SMTP id q187-20020a6743c4000000b0035449cff663mr16345113vsa.38.1656943132140; Mon, 04 Jul 2022 06:58:52 -0700 (PDT) Received: from localhost ([181.97.174.128]) by smtp.gmail.com with ESMTPSA id v16-20020a056102303000b0035609fbb1a9sm3567826vsa.1.2022.07.04.06.58.47 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 04 Jul 2022 06:58:51 -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, kunit-dev@googlegroups.com, linux-kselftest@vger.kernel.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 v9 1/9] mm/memblock: Tag memblocks with crypto capabilities Date: Mon, 4 Jul 2022 10:58:25 -0300 Message-Id: <20220704135833.1496303-2-martin.fernandez@eclypsium.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220704135833.1496303-1-martin.fernandez@eclypsium.com> References: <20220704135833.1496303-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 Sun Apr 26 10:53:24 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 28FF3CCA479 for ; Mon, 4 Jul 2022 13:59:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234529AbiGDN7M (ORCPT ); Mon, 4 Jul 2022 09:59:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37226 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234526AbiGDN7E (ORCPT ); Mon, 4 Jul 2022 09:59:04 -0400 Received: from mail-vs1-xe2a.google.com (mail-vs1-xe2a.google.com [IPv6:2607:f8b0:4864:20::e2a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 399A0DEE8 for ; Mon, 4 Jul 2022 06:59:01 -0700 (PDT) Received: by mail-vs1-xe2a.google.com with SMTP id j6so9137801vsi.0 for ; Mon, 04 Jul 2022 06:59:01 -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=k/5GR/KX7teTknQriuvUZb8IVKxvs7e03/RMVm9+BQI=; b=GLaHhFySQ9gOLZrKKeJvwwMHsWCWM3q5PzByQA9CXY12npsM3Osv/S1W71q725TU13 H5NgkCeE2bqltfg5cRYAP4bll17byyhwEuYP2oQvTfgEY8qwK7/IU098iZXMhug19osr ZVHqSxQ9/UM+9k0N0DjOiIDLRQUJrp4S0oIyi3G8Ex4p6qAXsybjVeMBv4Dbh9566/GV A83eDRvuXPThAtIIG2X0ViUECgwh1c0F7AZNRLFNCik5k/6jdzoHzuKZiGlJ6RaoZy2B YaEZhCfWYPzaUbYiNoV+pDcaYkqv3AlFGu8AyC1alTFvHvP1W/TLvNOxjNOHCeYvPNML BAOw== 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=k/5GR/KX7teTknQriuvUZb8IVKxvs7e03/RMVm9+BQI=; b=CSGtjfN5PhrhrGDy5Ao6s4/EXFx0uV9J5Vs+rbFmlGezAdgX1UKT+CJ0D9If3uHje6 gjoZUySYk2l1dt1bCIIgddpnaQmIo/LK/lwRkcRt5pZHiZL3WMXMWZH5XVOtMgc7Ql5D T04vf4DVpZCpQcPECYIYvFNnA7rzCigTwgP9XsHR61FnRM6+/byCI1LnWoxa/diKflWZ aAVGWRlQbVBIA6klZqllqqiIf2kxITeVXkgE/DD6qGQkLFn8EPIGvk4p/betQDtnyW/n UmJn0n93A36o/ZVApkf6xdpCIfF2/0njdt8AyrsDOfESakJcrKH/zShxbd2siuNVqij0 vSUw== X-Gm-Message-State: AJIora/KY8Cv81rAPhrSoNBC/KEKL4gnyfWwMVEh8bmQGEYMAjpJ/mK1 mQ4Y63hk/+Hroh30iPZY1vPMSzhFIaHTUDtvEnPYwam8ajeip/jkLRu779XKaXiFWmIIriaJNY5 uKmNMsvGbxfZlN/aPQ266v1CWiLUxZuhKNf/W106XIwf14qBa9XAUgG4C6wGLUz2TAgGJXkjhZO yp8W1UGIv+jhl8w22OWRU= X-Google-Smtp-Source: AGRyM1t9iMJScGmHX08KEczn8L5RecJYqWxEG84yc3pB37kCvlZO6ibtfKtkmWCmJt0mxLsO9yKhOQ== X-Received: by 2002:a67:ac0d:0:b0:356:a09d:b063 with SMTP id v13-20020a67ac0d000000b00356a09db063mr6746100vse.69.1656943140208; Mon, 04 Jul 2022 06:59:00 -0700 (PDT) Received: from localhost ([181.97.174.128]) by smtp.gmail.com with ESMTPSA id p2-20020ab01ec2000000b003828ebf6b49sm722929uak.4.2022.07.04.06.58.54 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 04 Jul 2022 06:58: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, kunit-dev@googlegroups.com, linux-kselftest@vger.kernel.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 v9 2/9] mm/mmzone: Tag pg_data_t with crypto capabilities Date: Mon, 4 Jul 2022 10:58:26 -0300 Message-Id: <20220704135833.1496303-3-martin.fernandez@eclypsium.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220704135833.1496303-1-martin.fernandez@eclypsium.com> References: <20220704135833.1496303-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 aab70355d64f..6fd4785f1d05 100644 --- a/include/linux/mmzone.h +++ b/include/linux/mmzone.h @@ -883,6 +883,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 e008a3df0485..147437329ac7 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -7729,6 +7729,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 Sun Apr 26 10:53:24 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 AF4BDC43334 for ; Mon, 4 Jul 2022 13:59:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234839AbiGDN73 (ORCPT ); Mon, 4 Jul 2022 09:59:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37224 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234684AbiGDN7L (ORCPT ); Mon, 4 Jul 2022 09:59:11 -0400 Received: from mail-vs1-xe29.google.com (mail-vs1-xe29.google.com [IPv6:2607:f8b0:4864:20::e29]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CA5F6DF49 for ; Mon, 4 Jul 2022 06:59:07 -0700 (PDT) Received: by mail-vs1-xe29.google.com with SMTP id a184so6646vsa.1 for ; Mon, 04 Jul 2022 06:59: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=bzJenq/qxXZmxxl1Ksk6HJARtm3Ihl+B/CevCTs1JBk=; b=X2BYzcnRkUF9v6ZNqHr/J99bVmc6++VMWdfFNw36eEddAOQr4vRkHVyotRUK42q4bW 5fK+j8JbnkgPutr6FLwuTu4IhOuvnYXJK9KhSRIE074BY3hJIA/dSCQ0ZPokoYKRq9bG K2w65R5Pr8SdwAtf0y4ushiB/Qi4Gt/KoqyOQ6V7U/lK23q17WXGXZaYWD0URiM/D1lA KKLDW8cek6v4qnaFNCRC1ANZEcL0kVN0mz+4wBzKyMnZ1sxdBX07rM3AMrM19cVrRaxe 8WAHIANoBJg3qczWKPs9+w29IEuY+hvYAIDCH3/4EoTNTkMSYKR8Y4WjU1ffGUNI3QKy Aebg== 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=slSsioSw3UbmE859aH1i9vj5yiIArTOxR4RjN3/QoAvzMc0BL0AqKmwBoYYPa1UE5z L2gvoBD7ZGk70KyNaPDIN1z9WscP8w9DXadOZBTrOR3GOf0dmbvQR9m2x/r3U0Efe/OO kUsWl7XmecUzqg50S1xHbpAKLAN8RiUo4TpZ9ZYim0LxPe1kEyJDiPZHsB32aThTobY+ Jwua3QX2e3BR4RBMLdHSFPvj0liiGC5UbJccLc7bSUpSOpSW3zNLarqX4Gr1bVbkDYpx knerBKiOfRoAzcnHgVKq+jUg+2MrtCtDJIvWhRtAsJzmYT3e1xQ31ka0yafYaGUWaC4V UKCg== X-Gm-Message-State: AJIora8IhwdhvEn2Z/r+t59d8wF+K2sUtkqKtMz4jW/EB7SjVzNYfn3S 1fLOhh89hPjFHOf/mvcSmBs21Cmo3D36W0oRtPAU1Xp5KjNhVkidPNpu4OAcGzkNUNjPUtA/UJH 7wT+sbvHSApaCZwRZ9LRxwtY7hruq6IWBSGDxdyx/14ucERVEN00ucVaFvEb0QN8t9lI9CAGrj2 WtO6QNRg2CFhB8u6hT+JU= X-Google-Smtp-Source: AGRyM1sww168oavtBsM/BaefGrhT8xpWzlBWhslMAxgTNmfgjWhNTVANPgnpuFJruMWEoqpoYBxi6Q== X-Received: by 2002:a67:f74c:0:b0:354:5c29:aad7 with SMTP id w12-20020a67f74c000000b003545c29aad7mr14534161vso.40.1656943146559; Mon, 04 Jul 2022 06:59:06 -0700 (PDT) Received: from localhost ([181.97.174.128]) by smtp.gmail.com with ESMTPSA id j68-20020a1fa047000000b0036cefdde1cbsm4173891vke.10.2022.07.04.06.59.02 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 04 Jul 2022 06:59: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, kunit-dev@googlegroups.com, linux-kselftest@vger.kernel.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 v9 3/9] x86/e820: Add infrastructure to refactor e820__range_{update,remove} Date: Mon, 4 Jul 2022 10:58:27 -0300 Message-Id: <20220704135833.1496303-4-martin.fernandez@eclypsium.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220704135833.1496303-1-martin.fernandez@eclypsium.com> References: <20220704135833.1496303-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 Sun Apr 26 10:53:24 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 63818C43334 for ; Mon, 4 Jul 2022 13:59:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234784AbiGDN7n (ORCPT ); Mon, 4 Jul 2022 09:59:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37890 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234774AbiGDN7Z (ORCPT ); Mon, 4 Jul 2022 09:59:25 -0400 Received: from mail-ua1-x933.google.com (mail-ua1-x933.google.com [IPv6:2607:f8b0:4864:20::933]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A1FC3DFC4 for ; Mon, 4 Jul 2022 06:59:14 -0700 (PDT) Received: by mail-ua1-x933.google.com with SMTP id u41so1366626uau.8 for ; Mon, 04 Jul 2022 06:59:14 -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=gN9JTuRL5WoxiIFiSJH2BsN3ItwrNWqzLFOY4N3NUhOsXJIwDtjxxMEHz41YepsIDP W5E6V7n5JvDJ/i5baNtKTxaK2W6aWVWX9mVibtQoRH44epoqm/m8uN5jdit9wmjVdYS8 +hdvMwPPAKccBpDl0ixE6hnJML8ORQ3KT/4DcMLEnag6hMpoBtf2VLDNqUlCMCeEImNz uwHVQaW6fsQ3nXFqvFUctCXE6YBk4kl9EDCXPRv58XWMJg+HfRVk6NzIQX7sp6cQPuJY KZMLXxpZoH/68Tlsu1k83O0hChoQL7Pyx5RKXY6wF4QbeZ3eKGDBfBcgbvElggYQeek8 lyrw== 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=OWlwl0C6ndq1wq9DfUxR6yQOpE4+hO96vXtwSc1CCzMmDMAoPjWRq0lqDC1aAmrpZx YPWdc038gL+GZ2MI1VZw4023/fg53BAKGMQ9zKJJFwict3mhOq1hsrpcXZNbPBc3F9wn 1I9QZOku0ps17n+I0k8DY+7lyQQ/d8EF9pN3efdwHGVI41PQkctJQbgPmYTSEZsa8Cgl 0m58OhNjRZFstxxTR4PMLxOza7Ws1jFEg0MJZd2eqPNDTFfqBwYL3udWKMFYDyz1c3AA LiaRUlDOk8DYqTETdi36Hd/1jlmM8J/T6fxyWo8cWCr5H+6Ty38Q6RVtjVHHlh8nzYld YG8Q== X-Gm-Message-State: AJIora89xI34cNLCuF8+T7OWUY+x41U44QGx4IUeeU3iH7YNDL7/laGB 72xqivK87x9rZqpi0yBEdFmC4p1OQKWOaAOvcSG9v6aVygCynun1QEw7nYveI3xDwKrTpGsqzFH yCY5ZuyrhE7RxUjQor6LNC2Md3cUBivKO53siMhkzazMMdLlge3jXm6QeZhLpoYkX/KsE1goCbg cy1uVgvwyPEuBI6fDrj4s= X-Google-Smtp-Source: AGRyM1tNxtduN57UQfGfrlt1doS6keFtGNxx4LQceUUCxOy7O2ATtIFJcC6rdjX3eolce6y12vCeSQ== X-Received: by 2002:a9f:22ac:0:b0:382:3c6c:f238 with SMTP id 41-20020a9f22ac000000b003823c6cf238mr5926773uan.123.1656943153149; Mon, 04 Jul 2022 06:59:13 -0700 (PDT) Received: from localhost ([181.97.174.128]) by smtp.gmail.com with ESMTPSA id x24-20020ab036f8000000b0038296f80d22sm723705uau.8.2022.07.04.06.59.08 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 04 Jul 2022 06:59:12 -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, kunit-dev@googlegroups.com, linux-kselftest@vger.kernel.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 v9 4/9] x86/e820: Refactor __e820__range_update Date: Mon, 4 Jul 2022 10:58:28 -0300 Message-Id: <20220704135833.1496303-5-martin.fernandez@eclypsium.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220704135833.1496303-1-martin.fernandez@eclypsium.com> References: <20220704135833.1496303-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 Sun Apr 26 10:53:24 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 85FBFCCA479 for ; Mon, 4 Jul 2022 13:59:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234392AbiGDN7t (ORCPT ); Mon, 4 Jul 2022 09:59:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38002 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234836AbiGDN73 (ORCPT ); Mon, 4 Jul 2022 09:59:29 -0400 Received: from mail-vk1-xa33.google.com (mail-vk1-xa33.google.com [IPv6:2607:f8b0:4864:20::a33]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EE83CE0A7 for ; Mon, 4 Jul 2022 06:59:21 -0700 (PDT) Received: by mail-vk1-xa33.google.com with SMTP id h26so4481043vkc.2 for ; Mon, 04 Jul 2022 06:59:21 -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=LPaDJYKM/DGiUp92Hh+jPbmInAsxrzuU0gaRNet6zG9cOie6oZ549PRb7KfPsYEAE+ HT+Z8VnJYmxAZDDADvU5oJsQTb96tcokrDSY9ESIQ/Avq4F0dqKDdRdiVdhzH5+fc+su fTZHP+JiqUbKTr5LPoZ3FJyLmgK/NTHxa0HmS1z60R9vUPkQ6YFr8wjBwHBpDiB4ndAi +oWLWmAKK3VzUR0A5KgO+mouqtrfvhrJf2JcoZdlBIohxQoLcA6EXMSTgVkgDl+MuMRv 00ZdBkTssQWwSgp7WN00hdh5KoWXbc4eMznXdDuJk4B/+n7DH9HCAl9gWwxm76WIA8a7 XkLg== 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=UIJUmQAiWqAU9oqGOQGJEH8aAW7LpnArjE5yE4gAlFfAQeOOhiz55q2UNmXYdwQLxr hyQ8N2xOT1MUCe/uHCNWVBTWZB2zXHnva2kafMDeqeUR3JSePCAGVEUSAKQY0b8QuUU0 QtQwxSAJ36IQQZwtT4r330WbOOOeUhAzLXVBZVck3yQZgvy3hSxZ8JUjVVcy6jWT3ByK NCVk/UDP0UNkKkH4WYtHge5yjiNRc6Zbd+oYUACbMOpWqJiX3Kn/G89MMp9hjlkZmBQG VQo3KfWth8MMPmoHeSNrvZXkkdkZyVlE34lDHsrDYQpJ9+uEG/xWnjbkxDOOymXJYU4Y mKvg== X-Gm-Message-State: AJIora8lGZZqMLxHWMZMVd/FWKY6q46t24QtZKxw574eB2t/6K2UFu3s UyjxjnffmESj9oEhwHcdYFH1HTuoI14pbku5AESLZIQJ2t7eYLRKvhCyT4ABSdFK7qBY7sVAJMW FYnhxIx7XxJEMk2El+V63kac9KgafwsnckjFG4kCbqpExBMn6DUguKpMaUCQFjzwuW2c8kUIZP0 7t29zR0yqVGh0K0tiO X-Google-Smtp-Source: AGRyM1uhvKX9DY0lHDCjtBY+5BrycH2Gr0rJb29k0daor6bM+GZnlj9mVlr6FF1AhkwgIQkJl3l0dQ== X-Received: by 2002:a05:6122:1485:b0:36c:c60b:877f with SMTP id z5-20020a056122148500b0036cc60b877fmr17367399vkp.4.1656943160742; Mon, 04 Jul 2022 06:59:20 -0700 (PDT) Received: from localhost ([181.97.174.128]) by smtp.gmail.com with ESMTPSA id w1-20020ab055c1000000b0037c965ac47fsm4564520uaa.31.2022.07.04.06.59.15 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 04 Jul 2022 06:59:20 -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, kunit-dev@googlegroups.com, linux-kselftest@vger.kernel.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 v9 5/9] x86/e820: Refactor e820__range_remove Date: Mon, 4 Jul 2022 10:58:29 -0300 Message-Id: <20220704135833.1496303-6-martin.fernandez@eclypsium.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220704135833.1496303-1-martin.fernandez@eclypsium.com> References: <20220704135833.1496303-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 Sun Apr 26 10:53:24 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 D27ECC43334 for ; Mon, 4 Jul 2022 14:00:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234546AbiGDOAG (ORCPT ); Mon, 4 Jul 2022 10:00:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37798 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234860AbiGDN7j (ORCPT ); Mon, 4 Jul 2022 09:59:39 -0400 Received: from mail-ua1-x936.google.com (mail-ua1-x936.google.com [IPv6:2607:f8b0:4864:20::936]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8B68DDF88 for ; Mon, 4 Jul 2022 06:59:28 -0700 (PDT) Received: by mail-ua1-x936.google.com with SMTP id l27so3470904uac.10 for ; Mon, 04 Jul 2022 06:59:28 -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=RShTyro/t+3yHpqWRsCttc9MszdQZtO3zGbp/Xdpr35/Vq47G6fLUGZvcvHS5hwi/v WI4SjH57gA+ThZQkNBKiJ9M+QVum6LiE5aon0AseaOO816PXQSXgtyxtr2gsJc3rDVrZ EL/CSk67bbN1oPa+SqqjhG3nRkJLpfg03JS2CZm0wuLUEV05jVU4daNaRVD+SgGAOvfi axEOBfzsCw0Y5DmnACNzWhubwKbO6sb56iuZsrvH/seJCleBZslWdr74yxXTJmw35nBx Cd4L1df5X3jmGhSMFgFRiBqu7MVwN31ka2hQjX4xXKhwrK+hTCAMSnxABA0y8UNMbPa4 QeWA== 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=gTAciA1OP+YQ5zlEE9VGPkXYSL8UEE7Ah3MVKQcIosaPRd7I/LVs3XzVZWnToo72js GlU7QXRwmt08TE2eWfzP9D5PDqNtW8P2wVWDwviNDvFgh9jp6jYtQYCAnFWtF6G5x82/ zDV+Pyy8lFlEiQ+IRdqDTYiFt/PriBnkk3/K0PAyvfjq/8l68BrJuiukQSofEd94KrIF t5uOCSOIHYHjkFPqasI9VkskAJEJ6iUq9swCL+CobmCDq4WYF84BTsFmEl8iLEcBcMqX KkyVQXofrdHZdZvTAm7Uc1Eps/fzSWX1h/I8Q04aR29ddrDjBw2VOcuP5+DgM17mlw/t MQQw== X-Gm-Message-State: AJIora/j4bNAgixOCiZ/aMFsDTrT6QN5SMux2sgsKSRmu4oS2NrYUE1A o0gkmxXcItzDtBll+nxxIJIuqPoBVE4He02NDulU3a76DoR4Xz7ZXVWxtW3gjwSXBx3F9s8Hola //Yh4dySiUPukPrn8ChqpF1rq+icWdc30UNio8zH8vQjIPhn7JD5y1CAS/l6VMHVeOZahPO56Yc OS8fcOVPXaTvMXYK0L X-Google-Smtp-Source: AGRyM1uh+dtYwcEVNnwTgVE50IeoqbYCHITAu0RuThJV5+xla1vgT2mPtaGX0ThCQRCIvsXn4jKOcw== X-Received: by 2002:ab0:409:0:b0:382:746b:4c5a with SMTP id 9-20020ab00409000000b00382746b4c5amr3162878uav.83.1656943167360; Mon, 04 Jul 2022 06:59:27 -0700 (PDT) Received: from localhost ([181.97.174.128]) by smtp.gmail.com with ESMTPSA id r1-20020a1fa801000000b0036ca2afd7c9sm5759379vke.0.2022.07.04.06.59.22 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 04 Jul 2022 06:59:26 -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, kunit-dev@googlegroups.com, linux-kselftest@vger.kernel.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 v9 6/9] x86/e820: Tag e820_entry with crypto capabilities Date: Mon, 4 Jul 2022 10:58:30 -0300 Message-Id: <20220704135833.1496303-7-martin.fernandez@eclypsium.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220704135833.1496303-1-martin.fernandez@eclypsium.com> References: <20220704135833.1496303-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 Sun Apr 26 10:53:24 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 A9FC6C433EF for ; Mon, 4 Jul 2022 14:00:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234692AbiGDOAR (ORCPT ); Mon, 4 Jul 2022 10:00:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37300 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234783AbiGDN7n (ORCPT ); Mon, 4 Jul 2022 09:59:43 -0400 Received: from mail-vs1-xe29.google.com (mail-vs1-xe29.google.com [IPv6:2607:f8b0:4864:20::e29]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6470CDF40 for ; Mon, 4 Jul 2022 06:59:36 -0700 (PDT) Received: by mail-vs1-xe29.google.com with SMTP id i186so9078046vsc.9 for ; Mon, 04 Jul 2022 06:59:36 -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=z0DiM2QAFWfRCxyX4K2DXOUSOAXhJvDrH/Deu05TYDY=; b=cMagWlDmtdm3o1f6Pp61sL3GddrPHHQmPJWDvSTOuRfLNvZ4cPGhTJfzZcNjMxrfjr sEbPLuO9RK2jvBXKZnS9xayzZJumnLdPqvIynQ9cWked5s3vnTxQdeo28d4/wfb8C+zL +VqvZGqgvnIkqw2lFDv9JbvIbcCYWy3hB/iNAJspkY9a7o4Q1VoIgfVn/623KrxQ6I8u eBXrG3RP91GB1gMw5zXSyVmi+899r4kspuuZqoR6lSx57tZplo1sbF5vEuTQ+y2qVoe1 Uleb7KRX5XfIrNZYJg5CDMbrmtp+/rvl0vg6DLjHMniyMb0Rvs4lD12kZGILIPOpk4sW U/4Q== 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=z0DiM2QAFWfRCxyX4K2DXOUSOAXhJvDrH/Deu05TYDY=; b=oKUv8kxBs9QUTKJE+eZvUfYfDR0TaLIGN4Obc95C8PYRMNYxsqYpQu+3boDNgZ42zQ q+2lSMQ7TMp3XlASVMABxryhEDTHLMQOc7rZIairDutozbCWAF7iOG7xQL3Rp9KqzPBx qaC88h0RkCYxaOAaVOEpy3EQ+8u8JznLSfZSS+NR2icHhgYp6o5WbeFbNmHJEzCH+x55 IKAm0K/tEdrH/lp4DhY2xIUutIFbhyhLlfCsSOCQ7AKxTyVLDRrljefy6h33hsMqS1Wi jI4mFGMx7u2r/ztpKlQ+mWBjEH0XUuUswEPWbnSYqwwsRueZ751iXNimTsmUE1xfCoeb Ft5g== X-Gm-Message-State: AJIora9UJvvfOod7XxmYbPbiOvID+IPbmnDN98twZxfuVqgAXhdwzNgS SPCQlbYi8O/WU6dPlvhwg5o5YcCzEfzwW0LwE7F5X0a27jaYIgkNxPEf2FbLdeRHfOuBOG1z1M1 49hrE9yRZvwlnmuRISmrvF8TmEUyj9FLAfq9Qm4nX+ezFeRj/wzkowS3ssFWb8TKIZnD1rb3EVG QpOnG1uoneIUuD46QR X-Google-Smtp-Source: AGRyM1sE6muSOvx2k+xe3UnPJZ2WVCBPj7qW4PpsjOJJKZzXCsbFp4uxqOIHZ9lHyDxUhrjpJWshnQ== X-Received: by 2002:a67:eb0b:0:b0:354:5c77:775c with SMTP id a11-20020a67eb0b000000b003545c77775cmr17327338vso.46.1656943175020; Mon, 04 Jul 2022 06:59:35 -0700 (PDT) Received: from localhost ([181.97.174.128]) by smtp.gmail.com with ESMTPSA id x24-20020ab036f8000000b0038296f80d22sm724438uau.8.2022.07.04.06.59.29 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 04 Jul 2022 06:59:34 -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, kunit-dev@googlegroups.com, linux-kselftest@vger.kernel.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 v9 7/9] x86/e820: Add unit tests for e820_range_* functions Date: Mon, 4 Jul 2022 10:58:31 -0300 Message-Id: <20220704135833.1496303-8-martin.fernandez@eclypsium.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220704135833.1496303-1-martin.fernandez@eclypsium.com> References: <20220704135833.1496303-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 KUnit tests for the e820_range_* functions. Signed-off-by: Martin Fernandez Reviewed-by: David Gow --- arch/x86/Kconfig.debug | 10 ++ arch/x86/kernel/e820.c | 5 + arch/x86/kernel/e820_test.c | 249 ++++++++++++++++++++++++++++++++++++ 3 files changed, 264 insertions(+) create mode 100644 arch/x86/kernel/e820_test.c diff --git a/arch/x86/Kconfig.debug b/arch/x86/Kconfig.debug index d872a7522e55..b5040d345fb4 100644 --- a/arch/x86/Kconfig.debug +++ b/arch/x86/Kconfig.debug @@ -225,6 +225,16 @@ config PUNIT_ATOM_DEBUG The current power state can be read from /sys/kernel/debug/punit_atom/dev_power_state =20 +config E820_KUNIT_TEST + tristate "Tests for E820" if !KUNIT_ALL_TESTS + depends on KUNIT=3Dy + default KUNIT_ALL_TESTS + help + This option enables unit tests for the e820.c code. It + should be enabled only in development environments. + + If unsure, say N. + choice prompt "Choose kernel unwinder" default UNWINDER_ORC if X86_64 diff --git a/arch/x86/kernel/e820.c b/arch/x86/kernel/e820.c index dade59758b9f..a6ced3e306dd 100644 --- a/arch/x86/kernel/e820.c +++ b/arch/x86/kernel/e820.c @@ -1546,3 +1546,8 @@ void __init e820__memblock_setup(void) =20 memblock_dump_all(); } + +#ifdef CONFIG_E820_KUNIT_TEST +/* Let e820_test have access the static functions in this file */ +#include "e820_test.c" +#endif diff --git a/arch/x86/kernel/e820_test.c b/arch/x86/kernel/e820_test.c new file mode 100644 index 000000000000..6b28ea131380 --- /dev/null +++ b/arch/x86/kernel/e820_test.c @@ -0,0 +1,249 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include + +#include +#include + +#define KUNIT_EXPECT_E820_ENTRY_EQ(_test, _entry, _addr, _size, _type, = \ + _crypto_capable) \ + do { \ + KUNIT_EXPECT_EQ((_test), (_entry).addr, (_addr)); \ + KUNIT_EXPECT_EQ((_test), (_entry).size, (_size)); \ + KUNIT_EXPECT_EQ((_test), (_entry).type, (_type)); \ + KUNIT_EXPECT_EQ((_test), (_entry).crypto_capable, \ + (_crypto_capable)); \ + } while (0) + +struct e820_table test_table __initdata; + +static void __init test_e820_range_add(struct kunit *test) +{ + u32 full =3D ARRAY_SIZE(test_table.entries); + /* Add last entry. */ + test_table.nr_entries =3D full - 1; + __e820__range_add(&test_table, 0, 15, 0, 0); + KUNIT_EXPECT_EQ(test, test_table.nr_entries, full); + /* Skip new entry when full. */ + __e820__range_add(&test_table, 0, 15, 0, 0); + KUNIT_EXPECT_EQ(test, test_table.nr_entries, full); +} + +static void __init test_e820_range_update(struct kunit *test) +{ + u64 entry_size =3D 15; + u64 updated_size =3D 0; + /* Initialize table */ + test_table.nr_entries =3D 0; + __e820__range_add(&test_table, 0, entry_size, E820_TYPE_RAM, + E820_NOT_CRYPTO_CAPABLE); + __e820__range_add(&test_table, entry_size, entry_size, E820_TYPE_RAM, + E820_NOT_CRYPTO_CAPABLE); + __e820__range_add(&test_table, entry_size * 2, entry_size, + E820_TYPE_ACPI, E820_NOT_CRYPTO_CAPABLE); + + updated_size =3D __e820__range_update(&test_table, 0, entry_size * 2, + E820_TYPE_RAM, E820_TYPE_RESERVED); + + /* The first 2 regions were updated */ + KUNIT_EXPECT_EQ(test, updated_size, entry_size * 2); + KUNIT_EXPECT_E820_ENTRY_EQ(test, test_table.entries[0], 0, entry_size, + E820_TYPE_RESERVED, E820_NOT_CRYPTO_CAPABLE); + KUNIT_EXPECT_E820_ENTRY_EQ(test, test_table.entries[1], entry_size, + entry_size, E820_TYPE_RESERVED, + E820_NOT_CRYPTO_CAPABLE); + KUNIT_EXPECT_E820_ENTRY_EQ(test, test_table.entries[2], entry_size * 2, + entry_size, E820_TYPE_ACPI, + E820_NOT_CRYPTO_CAPABLE); + + updated_size =3D __e820__range_update(&test_table, 0, entry_size * 3, + E820_TYPE_RESERVED, E820_TYPE_RAM); + + /* + * Only the first 2 regions were updated, + * since E820_TYPE_ACPI > E820_TYPE_RESERVED + */ + KUNIT_EXPECT_EQ(test, updated_size, entry_size * 2); + KUNIT_EXPECT_E820_ENTRY_EQ(test, test_table.entries[0], 0, entry_size, + E820_TYPE_RAM, E820_NOT_CRYPTO_CAPABLE); + KUNIT_EXPECT_E820_ENTRY_EQ(test, test_table.entries[1], entry_size, + entry_size, E820_TYPE_RAM, + E820_NOT_CRYPTO_CAPABLE); + KUNIT_EXPECT_E820_ENTRY_EQ(test, test_table.entries[2], entry_size * 2, + entry_size, E820_TYPE_ACPI, + E820_NOT_CRYPTO_CAPABLE); +} + +static void __init test_e820_range_remove(struct kunit *test) +{ + u64 entry_size =3D 15; + u64 removed_size =3D 0; + + struct e820_entry_updater updater =3D { .should_update =3D + remover__should_update, + .update =3D remover__update, + .new =3D NULL }; + + struct e820_remover_data data =3D { .check_type =3D true, + .old_type =3D E820_TYPE_RAM }; + + /* Initialize table */ + test_table.nr_entries =3D 0; + __e820__range_add(&test_table, 0, entry_size, E820_TYPE_RAM, + E820_NOT_CRYPTO_CAPABLE); + __e820__range_add(&test_table, entry_size, entry_size, E820_TYPE_RAM, + E820_NOT_CRYPTO_CAPABLE); + __e820__range_add(&test_table, entry_size * 2, entry_size, + E820_TYPE_ACPI, E820_NOT_CRYPTO_CAPABLE); + + /* + * Need to use __e820__handle_range_update because + * e820__range_remove doesn't ask for the table + */ + removed_size =3D __e820__handle_range_update(&test_table, + 0, entry_size * 2, + &updater, &data); + + /* The first two regions were removed */ + KUNIT_EXPECT_EQ(test, removed_size, entry_size * 2); + KUNIT_EXPECT_E820_ENTRY_EQ(test, test_table.entries[0], 0, 0, 0, 0); + KUNIT_EXPECT_E820_ENTRY_EQ(test, test_table.entries[1], 0, 0, 0, 0); + + removed_size =3D __e820__handle_range_update(&test_table, + 0, entry_size * 3, + &updater, &data); + + /* Nothing was removed, since nothing matched the target type */ + KUNIT_EXPECT_EQ(test, removed_size, 0); + KUNIT_EXPECT_E820_ENTRY_EQ(test, test_table.entries[0], 0, 0, 0, 0); + KUNIT_EXPECT_E820_ENTRY_EQ(test, test_table.entries[1], 0, 0, 0, 0); + KUNIT_EXPECT_E820_ENTRY_EQ(test, test_table.entries[2], entry_size * 2, + entry_size, E820_TYPE_ACPI, + E820_NOT_CRYPTO_CAPABLE); +} + +static void __init test_e820_range_crypto_update(struct kunit *test) +{ + u64 entry_size =3D 15; + u64 updated_size =3D 0; + /* Initialize table */ + test_table.nr_entries =3D 0; + __e820__range_add(&test_table, 0, entry_size, E820_TYPE_RAM, + E820_CRYPTO_CAPABLE); + __e820__range_add(&test_table, entry_size, entry_size, E820_TYPE_RAM, + E820_NOT_CRYPTO_CAPABLE); + __e820__range_add(&test_table, entry_size * 2, entry_size, + E820_TYPE_RAM, E820_CRYPTO_CAPABLE); + + updated_size =3D __e820__range_update_crypto(&test_table, + 0, entry_size * 3, + E820_CRYPTO_CAPABLE); + + /* Only the region in the middle was updated */ + KUNIT_EXPECT_EQ(test, updated_size, entry_size); + KUNIT_EXPECT_E820_ENTRY_EQ(test, test_table.entries[0], 0, entry_size, + E820_TYPE_RAM, E820_CRYPTO_CAPABLE); + KUNIT_EXPECT_E820_ENTRY_EQ(test, test_table.entries[1], entry_size, + entry_size, E820_TYPE_RAM, + E820_CRYPTO_CAPABLE); + KUNIT_EXPECT_E820_ENTRY_EQ(test, test_table.entries[2], entry_size * 2, + entry_size, E820_TYPE_RAM, + E820_CRYPTO_CAPABLE); +} + +static void __init test_e820_handle_range_update_intersection(struct kunit= *test) +{ + struct e820_entry_updater updater =3D { + .should_update =3D type_updater__should_update, + .update =3D type_updater__update, + .new =3D type_updater__new + }; + + struct e820_type_updater_data data =3D { .old_type =3D E820_TYPE_RAM, + .new_type =3D E820_TYPE_RESERVED }; + + u64 entry_size =3D 15; + u64 intersection_size =3D 2; + u64 updated_size =3D 0; + /* Initialize table */ + test_table.nr_entries =3D 0; + __e820__range_add(&test_table, 0, entry_size, E820_TYPE_RAM, + E820_NOT_CRYPTO_CAPABLE); + + updated_size =3D + __e820__handle_range_update(&test_table, 0, + entry_size - intersection_size, + &updater, &data); + + KUNIT_EXPECT_EQ(test, updated_size, entry_size - intersection_size); + + /* There is a new entry */ + KUNIT_EXPECT_EQ(test, test_table.nr_entries, intersection_size); + + /* The original entry now is moved */ + KUNIT_EXPECT_E820_ENTRY_EQ(test, test_table.entries[0], + entry_size - intersection_size, + intersection_size, E820_TYPE_RAM, + E820_NOT_CRYPTO_CAPABLE); + + /* The new entry has the correct values */ + KUNIT_EXPECT_E820_ENTRY_EQ(test, test_table.entries[1], 0, + entry_size - intersection_size, + E820_TYPE_RESERVED, E820_NOT_CRYPTO_CAPABLE); +} + +static void __init test_e820_handle_range_update_inside(struct kunit *test) +{ + struct e820_entry_updater updater =3D { + .should_update =3D type_updater__should_update, + .update =3D type_updater__update, + .new =3D type_updater__new + }; + + struct e820_type_updater_data data =3D { .old_type =3D E820_TYPE_RAM, + .new_type =3D E820_TYPE_RESERVED }; + + u64 entry_size =3D 15; + u64 updated_size =3D 0; + /* Initialize table */ + test_table.nr_entries =3D 0; + __e820__range_add(&test_table, 0, entry_size, E820_TYPE_RAM, + E820_NOT_CRYPTO_CAPABLE); + + updated_size =3D __e820__handle_range_update(&test_table, 5, + entry_size - 10, + &updater, &data); + + KUNIT_EXPECT_EQ(test, updated_size, entry_size - 10); + + /* There are two new entrie */ + KUNIT_EXPECT_EQ(test, test_table.nr_entries, 3); + + /* The original entry now shrunk */ + KUNIT_EXPECT_E820_ENTRY_EQ(test, test_table.entries[0], 0, 5, + E820_TYPE_RAM, E820_NOT_CRYPTO_CAPABLE); + + /* The new entries have the correct values */ + KUNIT_EXPECT_E820_ENTRY_EQ(test, test_table.entries[1], 5, + entry_size - 10, E820_TYPE_RESERVED, + E820_NOT_CRYPTO_CAPABLE); + /* Left over of the original region */ + KUNIT_EXPECT_E820_ENTRY_EQ(test, test_table.entries[2], entry_size - 5, + 5, E820_TYPE_RAM, E820_NOT_CRYPTO_CAPABLE); +} + +static struct kunit_case e820_test_cases[] __initdata =3D { + KUNIT_CASE(test_e820_range_add), + KUNIT_CASE(test_e820_range_update), + KUNIT_CASE(test_e820_range_remove), + KUNIT_CASE(test_e820_range_crypto_update), + KUNIT_CASE(test_e820_handle_range_update_intersection), + KUNIT_CASE(test_e820_handle_range_update_inside), + {} +}; + +static struct kunit_suite e820_test_suite __initdata =3D { + .name =3D "e820", + .test_cases =3D e820_test_cases, +}; + +kunit_test_init_section_suite(e820_test_suite); --=20 2.30.2 From nobody Sun Apr 26 10:53:24 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 8383BC43334 for ; Mon, 4 Jul 2022 14:00:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234350AbiGDOAV (ORCPT ); Mon, 4 Jul 2022 10:00:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37392 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232209AbiGDN76 (ORCPT ); Mon, 4 Jul 2022 09:59:58 -0400 Received: from mail-vs1-xe34.google.com (mail-vs1-xe34.google.com [IPv6:2607:f8b0:4864:20::e34]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B7AB1DFD0 for ; Mon, 4 Jul 2022 06:59:42 -0700 (PDT) Received: by mail-vs1-xe34.google.com with SMTP id a184so8283vsa.1 for ; Mon, 04 Jul 2022 06:59:42 -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=PFcwfz9YjnTwBY53CZ26/cKSWQCH95fLXQHITYfVqQs=; b=CQTNFByZSPbcjp+kBGJxruUIiefylatXkTKZ4UXWXph7ZtNVp8T6HpEfD1XKzuncEy y3FR1f6yAyetwOtLdPHa0NQDBeJgn7zQpIarid63VNYuSxz4OGxt1VLadB3LAfdvW+aw JwrhslRX3sESXzTPsm5dzaMQdQKd1ykl1P4YJXXYlQn3Wb8Q0Ro2q8KdWxdfb1ke5HU5 cTmyxBSvuFbDFi2x49F91NFcxUqn/XPns/cqItahB3rYnpS0VgEsz46iamTD1IvZni3+ s4yfyUsSiGmDTuIfozuuX80v86m2Q/l0Op6AZ+KtMUA/qUrt2196D1PPf4BMGWhUuzJG gU0A== 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=PFcwfz9YjnTwBY53CZ26/cKSWQCH95fLXQHITYfVqQs=; b=0YvwPggETAu8nJVplQ6NOY7LOGCvYjifRxCYV6s5Pwy/1JbEYOk3vXUGeS5L530aiZ POMpyD6EaBJjU+8/8b7kbwXSBN0esiAQRAeaC6nhdIngJcm93kK/WfQcLdPbcBUQi4UM hhXIYrZLuODd37v2hoWlWPum6fFOOlMaGy6vBKOdJGs3zMCu07kvRPqik0WxdRYCBXDL M4Kz+/HeKLk4Q0n2JbM1/xq8Ys4BhxibLTc+nFUpU0Oa7EHQc9L7I650uDVO7W0BZlOC HzrYIy8P14oKlzTTA2t4JNHRMvDMzn6BxWKA+PPDaZEzdAlTfE/ralmA1fm0VUXTbnq+ E/MA== X-Gm-Message-State: AJIora/vAF8Kq7X7yLb0o1xXCE+EJfNFv/djT7SOPr4UslyRjHSP/ymx NFiS0BEnwps3f3aBTcRp88Y4KW+Z5K0sJLkXoO61sgRlX7Tuc7z02HuqDolRHKkYcmn+mxRLhtA HL2QXFA8iczlsML8Dt0pOCYSYfApELYlt9O7GG4uGlUlhhP3qA62c6wjCgyGAtqm+MIF2CkqVKp D96K2LLFkwVYb8XwvD X-Google-Smtp-Source: AGRyM1u4xzaefoptHuBiQ/eWFY+5bcW4tRDbJaAC2o6qv7KsWce1Xme3nK+Lkq/KSfHakteIXi79og== X-Received: by 2002:a05:6102:4410:b0:356:f108:157b with SMTP id df16-20020a056102441000b00356f108157bmr272236vsb.66.1656943181405; Mon, 04 Jul 2022 06:59:41 -0700 (PDT) Received: from localhost ([181.97.174.128]) by smtp.gmail.com with ESMTPSA id q192-20020a1f5cc9000000b00356d8a0fbb2sm4413105vkb.51.2022.07.04.06.59.36 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 04 Jul 2022 06:59: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, kunit-dev@googlegroups.com, linux-kselftest@vger.kernel.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 v9 8/9] x86/efi: Mark e820_entries as crypto capable from EFI memmap Date: Mon, 4 Jul 2022 10:58:32 -0300 Message-Id: <20220704135833.1496303-9-martin.fernandez@eclypsium.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220704135833.1496303-1-martin.fernandez@eclypsium.com> References: <20220704135833.1496303-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 1591d67e0bcd..397d5e54d65e 100644 --- a/arch/x86/platform/efi/efi.c +++ b/arch/x86/platform/efi/efi.c @@ -187,6 +187,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); } @@ -444,6 +446,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) && @@ -497,6 +527,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 Sun Apr 26 10:53:24 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 5630DC433EF for ; Mon, 4 Jul 2022 14:00:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234560AbiGDOAe (ORCPT ); Mon, 4 Jul 2022 10:00:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38208 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234844AbiGDOAB (ORCPT ); Mon, 4 Jul 2022 10:00:01 -0400 Received: from mail-vs1-xe2c.google.com (mail-vs1-xe2c.google.com [IPv6:2607:f8b0:4864:20::e2c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1E53CE092 for ; Mon, 4 Jul 2022 06:59:48 -0700 (PDT) Received: by mail-vs1-xe2c.google.com with SMTP id j1so9071859vsj.12 for ; Mon, 04 Jul 2022 06:59:48 -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=hiQZs7RQJ57Y3iMEvjKPq7apwG47z8xfODnJPhnyEtM=; b=h0cx2J376VdMDKbdnNSudAZQFU5bNAb+af1nsukIB0ohm1yOAPCujoxEhD9RXdrsTN 59gWNY7oIZmpnuoyFxH0Lfsl2dJloyzrktCOwSAhx/pcUBpqAhwobasrepx8jaNSG5IB +3V+Cz/MOtaMXMEN5TEaTQPabi0feo2LWbZUM09F5pJcBvlR9ydj4wefZ1gUCoZ/Kj3d 6Zk5liSBxCrOnBdnuOagWCvacvfTLNkQFDUyGttjpU3agSg/uS+wBYUlyl0jI+GWDtdB 7y3rJJBNyXJ0bQlJkj2s+b4F6h4T+dammjL6tWPz3wX8lhSw7YKSuZK8HHmrFpWUNLAO TyUQ== 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=hiQZs7RQJ57Y3iMEvjKPq7apwG47z8xfODnJPhnyEtM=; b=hlQn0/PKO1n1sLtxe8+DpLOiSOeHfn4pbnagffF6rEBsVlmz+RDTFMm27figZiyhg+ UNZZGW+c2joUe5ksaZaW0G2CNORCED5LLSLJkNCYbrA61JTIUyjzRdfPPZvpH6ZCktt2 z8CXMZJFQhjrSNTtBujiCGk0zBemIPBDGhdvzMbYuAN6i2K1K8bOkrzS9tVU5Cqxtw32 83LeFlynZ+nJDx8IQo0ihT4udSpGnJzL7WjnVRNufKoJrOQbgPryL0axCJ5h9lIKtCMa 7qgrMRiMPdo8U4VlmYzhHDi2LD1i3jx8W82OOyCcHBP337TWXPILPTLgkEgI8dwY93mF yxNg== X-Gm-Message-State: AJIora8xdfNjNmG1bT1nMin4vYYk3/lSXAFRcKjdMUjzaLM+01H3GLN+ uo481/IQxJ5T9K6kH0V+7hVbE6n6M7dTDvVWBhYAmXWueI6E2f2yurkOG4FDkR7Ru3hUok3tWfz etQM+8bBbtn41q/XcL4JR37s06CvYddWmxmErCBkHzawtZ8U6EmVQctrE3pgJYndMauhY4c1ExJ 7xbunxrHvErQrsu02l X-Google-Smtp-Source: AGRyM1tZA+RFAUNVohbAzEyn5WDlKTAd76W0duJR+JCRJSy8RhT4D5c7wIBcLsYpIxHYYEOGInbcGg== X-Received: by 2002:a67:f142:0:b0:356:3251:e53d with SMTP id t2-20020a67f142000000b003563251e53dmr16561722vsm.40.1656943187638; Mon, 04 Jul 2022 06:59:47 -0700 (PDT) Received: from localhost ([181.97.174.128]) by smtp.gmail.com with ESMTPSA id p25-20020ab06259000000b00382b14a73afsm311704uao.30.2022.07.04.06.59.43 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 04 Jul 2022 06:59:47 -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, kunit-dev@googlegroups.com, linux-kselftest@vger.kernel.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 v9 9/9] drivers/node: Show in sysfs node's crypto capabilities Date: Mon, 4 Jul 2022 10:58:33 -0300 Message-Id: <20220704135833.1496303-10-martin.fernandez@eclypsium.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220704135833.1496303-1-martin.fernandez@eclypsium.com> References: <20220704135833.1496303-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 0ac6376ef7a1..f081fa48c8e6 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