From nobody Sun May 10 19:52:58 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 3532BC433FE for ; Mon, 25 Apr 2022 17:15:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243837AbiDYRTA (ORCPT ); Mon, 25 Apr 2022 13:19:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36262 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243819AbiDYRSx (ORCPT ); Mon, 25 Apr 2022 13:18:53 -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 E1D502495C for ; Mon, 25 Apr 2022 10:15:48 -0700 (PDT) Received: by mail-oa1-x29.google.com with SMTP id 586e51a60fabf-e68392d626so11678292fac.4 for ; Mon, 25 Apr 2022 10:15: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=31fpVwLUvVD/6XAm2dtZJP4qrzc0azCUndly9IICdXE=; b=KnyaefTks6jo+4TILCrF756WGX8lxZt9aHpg0PI9qew7ve2EigS6/YECweUifICg8H LiAgoqO+vwKrnqUqJe60Yhzfl4IRxDCH7Vch0OnSf0MasLEDbaR1qcsK3+9Jpog58ake F4bAxiod+Uu/zhOVRpQ8YJNXcyJMRaQi9/uMpgLuX8oqdN+PudGRnCL9IsfrDWJryraa KQsJD09z4zFNgyowj6+Ncy1TY21huL63RCM/uc8NHQDqRM9DjfJP3RHfBP6TW+I4kd56 5xHxqyMJDOL7qxbQ1IfYpbKFhZUmuZlARjfbywcn3ycXGvjGJWyJkr3Gb2fSI2NqRAGF 8coQ== 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=31fpVwLUvVD/6XAm2dtZJP4qrzc0azCUndly9IICdXE=; b=k1Vsp4PLkpDHHRbUyestjNY61F9L182fbhfVqSE/1JzdgbsutmE3jTLKHfX6gW/VA+ FKqbmI3DFd4smsU0QGdFNJDzXG2nVKwuotru89f/aZ+Rc6o/4vE52pFsChG45ovjiW+r c1EaRBIZxpXd33zzjaMRlMk1AEiavocfmk2UoW6YyEo/nasVzE2GkkzgJeFmnjeCJrFv lsrKim2JMk9Fxxu02czXTC639Kl7BPBPKGWyuVZwX7WW9H3NVYyqyy4OmjU7txljV3bQ nFy2dGQk5qO6RqHHILYN+hBbt9vEsij8R4j/CNDOE9YK8lbOTW+sYRX0zt1tNkrG2pEt k3Ew== X-Gm-Message-State: AOAM5306Zmw6AGRaJg+KAxx9j36hbwSbedKjOetDLs961/apjfTr83jv FbPxrsaRQV0R/FprbQDg0FJFqno7NbxlIaj1utw77KcrIDIII140h4AYaTXY8sCD0Kg/xzW0Po7 3+r/d1sJ5k7IoB80/B6gtzShCbDvP5rRZMXDJupBEzm+1RWY4zLTi3PRZ6L9l7e/gFoJzEmdm1C JzLS27MWcK9iIPRYMe8wE3VQ== X-Google-Smtp-Source: ABdhPJxvV/YHrMeAQmhkW4afxhbyXcn6p7T6NUeeJ6tU6iHC/HNuYfsN2vuiql0DYLtiS3hO/Qmmlw== X-Received: by 2002:a05:6870:3052:b0:da:a150:befe with SMTP id u18-20020a056870305200b000daa150befemr7457503oau.129.1650906947228; Mon, 25 Apr 2022 10:15:47 -0700 (PDT) Received: from localhost ([181.97.174.128]) by smtp.gmail.com with ESMTPSA id q12-20020a4ad54c000000b003245ac0a745sm4538568oos.22.2022.04.25.10.15.41 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 25 Apr 2022 10:15:46 -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 v7 1/8] mm/memblock: Tag memblocks with crypto capabilities Date: Mon, 25 Apr 2022 14:15:19 -0300 Message-Id: <20220425171526.44925-2-martin.fernandez@eclypsium.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220425171526.44925-1-martin.fernandez@eclypsium.com> References: <20220425171526.44925-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..fe62f81572e6 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 memory memblock_type 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 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 May 10 19:52:58 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 C5244C433F5 for ; Mon, 25 Apr 2022 17:16:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243846AbiDYRTT (ORCPT ); Mon, 25 Apr 2022 13:19:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38128 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243839AbiDYRTA (ORCPT ); Mon, 25 Apr 2022 13:19:00 -0400 Received: from mail-oi1-x22f.google.com (mail-oi1-x22f.google.com [IPv6:2607:f8b0:4864:20::22f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 979F163F4 for ; Mon, 25 Apr 2022 10:15:55 -0700 (PDT) Received: by mail-oi1-x22f.google.com with SMTP id r8so17846701oib.5 for ; Mon, 25 Apr 2022 10:15:55 -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=WVNRKEKob2y9lFrXgNElxa26Uxh1XvllzgMqrT+R70+SAvu/khzVJ0sFW9Kny0Qa7N jLVQLq9bTQ71s6VakMPFRKCHzlO8ZQ8mMVLzhNDI47Tsc6K6t3zkkwCNQe76Hs/qU+h5 pff6FNHYBVqwtXfDT2vS4howEmhCTaodjYQw7fzcHX4fgvsmvS2MLEoXGEWf8QrHMsop kGxt72nAdiuqmo2bcyzM+I4DRQjwsBtp+Is15HwL9rka8zB2koeqRTkw+HAsvAlcMyfK u6R0LRV4pUi4IWR8NROkxZ2EmmJAi1skJ5sO/+YxWmwC+pZ4xs3as8lXszeFy8RS3TMf tmvA== 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=fO/ikhjXXbSb0Y9VtUk1pVSHD406osjQ50kDjMGMzbI/j1tk45w4zDHvrskMqdUqqv /+QzUVGmVV+aX552xuJgkOAAkbTgJQWTMvf71FgUQGQfrEUDQV/MN/ERwP3ByqzhmLUp jTjFTBFmLnSXXtX7ytCGdovnhz/9kb6lkWSe4gaBQh3o3dGceqg42/SQ8ELlkYoZhOf0 RGs0KxxAsuZfFkh8cMrXxjLQGHAe/Mvgl6dtPttaVYAaibAatvkh0dBUklmDe6l6rUmK DLE0dJEzCH2x8Mg2tkRQ1qLTsxRje9FyQWXxa9cGSe2ELnpRVNougk47VqA2GMAXYIbf s0ww== X-Gm-Message-State: AOAM533Za96mYdH0U+EO9bmeh45EXkdFT5v60hAne+WPjGXbf544uywu 8NaWl0MB2f08l3KHzPSeytCM/Db9ACY7Mdl5f8CvDCFSLyIOi6VOuvqZym09//BFZcm42tFtLSG ShuTxEa6i68M/DK4jtw8Npiaob3FrHZupoZ9z/49Xe3nUKQBFV0gVgDPMNceqfVjvLW0SnVGpbf qA2rgl9cP5JA2kJZqbFNpF2A== X-Google-Smtp-Source: ABdhPJwJqrLz4LvPdbpxTti+gZ2zpgNv7oGdv4vd7aYab8Kx1ulj4m3IkrTedE0A5L9UCyT5RbSlZA== X-Received: by 2002:a05:6808:1805:b0:323:a81:bdff with SMTP id bh5-20020a056808180500b003230a81bdffmr8671177oib.7.1650906954532; Mon, 25 Apr 2022 10:15:54 -0700 (PDT) Received: from localhost ([181.97.174.128]) by smtp.gmail.com with ESMTPSA id j9-20020a056808056900b0032252797ea4sm4007238oig.6.2022.04.25.10.15.49 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 25 Apr 2022 10:15:54 -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 v7 2/8] mm/mmzone: Tag pg_data_t with crypto capabilities Date: Mon, 25 Apr 2022 14:15:20 -0300 Message-Id: <20220425171526.44925-3-martin.fernandez@eclypsium.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220425171526.44925-1-martin.fernandez@eclypsium.com> References: <20220425171526.44925-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 Reviewed-by: Mike Rapoport --- 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 Sun May 10 19:52:58 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 CBE10C4332F for ; Mon, 25 Apr 2022 17:16:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243875AbiDYRT7 (ORCPT ); Mon, 25 Apr 2022 13:19:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39016 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243890AbiDYRTc (ORCPT ); Mon, 25 Apr 2022 13:19:32 -0400 Received: from mail-oi1-x235.google.com (mail-oi1-x235.google.com [IPv6:2607:f8b0:4864:20::235]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AD4FD27B1D for ; Mon, 25 Apr 2022 10:16:03 -0700 (PDT) Received: by mail-oi1-x235.google.com with SMTP id b188so17797764oia.13 for ; Mon, 25 Apr 2022 10:16:03 -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=scNNecAxK89bCsIv0vpIcsFL1bTqr463ACa9Pmhvay4=; b=Xwe/HZQ7QWugCsF4WnCrUGpuXwV1gaK3ZKZdlbT70p+37ejUk3CLqzzzPcu5CNkCLf CGrax6l11PoWQUWAiqepna03GEHKQuPCaABKVFzgk1+QB6dKZPw49cMsm2dI7OipMjGf 4p3gpErYOE85yNl37xI80+X0kBuIv8a27lPKf1GrFBbjhxbzGv6Ff97rQZQLsW4P5MBp 4dhjSTORIXAlRYu7rKmo5mqF78SpA5YnhC42lifS4/WCQAGP8Z0cVIe2jdgK6Ds0IgOv 9U7YRa3Dd0uYubs5wbYMhhiw+1OBoJ0kh1nuJi6TtmpkxTeJ4ecWP8jBdria2LZdCbkW fkMA== 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=scNNecAxK89bCsIv0vpIcsFL1bTqr463ACa9Pmhvay4=; b=4iyBOcIRBp5mvQKtMR+UWy8OPXuhFKzylsRM/31nAy9RuFaMjZek8JxPtAH1OhRkDn ZyPYHJnn4BY7v3sA9nPwGu6kOhRxqKj/brU9KrqK3ORegsjLn/miuhm8tKFidt1pOTzQ ZjPIBC4gPmkI3KikS/Wtrep6vwTO1dpFA/mkBs0z5pyRSE9estBu1vahRgIaRfBBnIqw FRV3gCM0cFSVAuUZmZPqle1ZSqx8gt0pLX8wUoYhEMMQDpUv78bFsFqN/YPRTUpofZNu wLTsKVw5xz1O5W+uAge13GKL203UrS5cbanryooO/Dj7g/uoCR45zq4aPvAgYBUvIZnP rJnQ== X-Gm-Message-State: AOAM530CVOl7cB+hHL8L7XS3GYsE0LP05GfrWB3wqUYpqy6rUhOwn6Cx ED/VJDj8IP3ukgHnEhaUkM3beIHN6KSAi3/wputqWPH+KRflXnuus2gU5NOSqYr/mra/oG6CouJ v7VazOqNWqXuM31alTeHSIvG1rKzghNdQEmrNWDFd/cJI4LgNe74ewaMr7elo7yzw0Jl0OsFAPK dQLMYYivOUGgQ4ghrF8g4QDA== X-Google-Smtp-Source: ABdhPJxdW0TUVi/qCKY+MEYfc75LBEinyGxemFyKERNQFWQ35/MgZzJ97hAPoKTCadYesjTyi6gU6Q== X-Received: by 2002:aca:bd41:0:b0:2ec:ff42:814f with SMTP id n62-20020acabd41000000b002ecff42814fmr8838220oif.63.1650906961998; Mon, 25 Apr 2022 10:16:01 -0700 (PDT) Received: from localhost ([181.97.174.128]) by smtp.gmail.com with ESMTPSA id 1-20020a05687011c100b000de98359b43sm3502824oav.1.2022.04.25.10.15.56 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 25 Apr 2022 10:16:01 -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 v7 3/8] x86/e820: Add infrastructure to refactor e820__range_{update,remove} Date: Mon, 25 Apr 2022 14:15:21 -0300 Message-Id: <20220425171526.44925-4-martin.fernandez@eclypsium.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220425171526.44925-1-martin.fernandez@eclypsium.com> References: <20220425171526.44925-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 --- arch/x86/kernel/e820.c | 148 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 148 insertions(+) diff --git a/arch/x86/kernel/e820.c b/arch/x86/kernel/e820.c index f267205f2d5a..923585ab8377 100644 --- a/arch/x86/kernel/e820.c +++ b/arch/x86/kernel/e820.c @@ -459,6 +459,154 @@ 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. + * @new: Create new entry in the table with information gathered from + * @original and @data. + * + * Each function corresponds to an action that + * __e820__handle_range_update() does. Callbacks need to cast @data back + * to the corresponding type. + */ +struct e820_entry_updater { + bool (*should_update)(const struct e820_entry *entry, const void *data); + void (*update)(struct e820_entry *entry, const void *data); + void (*new)(struct e820_table *table, u64 new_start, u64 new_size, + const struct e820_entry *original, const void *data); +}; + +/** + * __e820__handle_intersected_range_update() - Helper function for + * __e820__handle_range_update(). + * @table: Target e820_table. + * @start: Start of the range. + * @size: Size of the range. + * @entry: Current entry that __e820__handle_range_update() was + * looking into. + * @updater: updater parameter of __e820__handle_range_update(). + * @data: data parameter of __e820__handle_range_update(). + * + * Helper for __e820__handle_range_update to handle the case where + * neither the entry completely covers the range nor the range + * completely covers the entry. + * + * Return: The updated size. + */ +static u64 __init +__e820__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; + } + /* 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; +} + +/** + * __e820__handle_range_update(): Helper function to update an address + * range in a e820_table + * @table: e820_table that we want to modify. + * @start: Start of the range. + * @size: Size of the range. + * @updater: Callbacks to modify the table. + * @data: Information to modify the table. This must be an struct + * e820_type_*_data. + * + * Update the table @table in [@start, @start + @size) doing the + * actions given in @updater. + * + * Return: The updated size. + */ +static u64 __init +__e820__handle_range_update(struct e820_table *table, + u64 start, + u64 size, + const struct e820_entry_updater *updater, + const void *data) +{ + u64 updated_size =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; + 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; + + /* 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, + entry->crypto_capable); + + 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 May 10 19:52:58 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 2022BC433EF for ; Mon, 25 Apr 2022 17:16:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243825AbiDYRTt (ORCPT ); Mon, 25 Apr 2022 13:19:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41426 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243885AbiDYRTg (ORCPT ); Mon, 25 Apr 2022 13:19:36 -0400 Received: from mail-oa1-x30.google.com (mail-oa1-x30.google.com [IPv6:2001:4860:4864:20::30]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D9498286E3 for ; Mon, 25 Apr 2022 10:16:10 -0700 (PDT) Received: by mail-oa1-x30.google.com with SMTP id 586e51a60fabf-e656032735so15500382fac.0 for ; Mon, 25 Apr 2022 10:16:10 -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=qzwUVSRCTs7yF7NC9RaputFYAMnZCA1mZDnEg/SpEoc=; b=DRhjerDdBI/0oOQFXmN1bF3llrdra9rWGkn4nBmO2KvVeB0kt07/vY4ECI/VgIUTxQ sEEELkTAlRFpqnnphHIURXWHe5ayIwoUn290gBIKXtC72XqXx4eVZ2exM/MeQo3aMwLq 5TQBGmO5ZWIGihyIk5bNmzHyYQsuur11t33L3jm41ClHcfiTxNdWeR6CcS04NWp2+oNC Vy6DkPYgyGo/rI4yQKqeUbZ7xe6KtAVzY/lsaFLXMegNf49MDK4RcnH29l49Z44BeMjE sVxFRdiXIYpE6tHva/FHqq/TWZzN6osF+tqnTL42iOJvPOmzWE80X8BAjt8K8iEd4w4N HXAw== 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=qzwUVSRCTs7yF7NC9RaputFYAMnZCA1mZDnEg/SpEoc=; b=hJ9Q3nz3fAXJCJt155cY14d7V2ZONdXuQhFhXg2RLtoJ+1KFV+aWGmYJhlKSBU7Bx6 pYkthiqFrRsAoXEdCTq+mwDQsj8k3gwwe3x9WcyVm8NdQ52flpIOBXs/PCmmlGZwYc56 3tn+WyMLbcMKql0+MdYq8cruyTnRlgGTL1sjPhzYyCMIt1z1zZ0oVKxbrTVbWNqoKUcC NIwH9FB7YafSiTVKGzCard5TgujrScEJJtIGUwR34ELH2AmOT8f6FffP2w1b+Jt2PIm2 GKa6sJoKYH6Stznk4X3DcL4Wm9Y0RqpL3eFPItVxk4OYEQXXOh8rBeqEW23d/smPy36H G7Kw== X-Gm-Message-State: AOAM531K5/tz3UT6M23HnzHL7ItKbErWWRg99IscjYi6yiU2fgtrerhR LgTazN9o3h2kJXhtxR3vWNENyzWi7ptUwqHCUE52dI5Sl72VyqQjexWF33N1wzo7fzh4bhoNn+g t1lbR9C5w4ERNBvbPEnBzXEoD7vonzdFsdvJvVBiWWWRqL+VTnVrsJdeVQXrwW2E0quRb8OSvgF vypUSmihtcuzGceHOo6XwYHg== X-Google-Smtp-Source: ABdhPJwbA+Mxla+CdrXZkNdzNcBLQ0nX8DluKtoxXNDB7ZKRkcFPHsqw5ahJLlGkdY1qBkKGVXMlzA== X-Received: by 2002:a05:6870:462a:b0:dd:f4f7:24be with SMTP id z42-20020a056870462a00b000ddf4f724bemr7488816oao.39.1650906969504; Mon, 25 Apr 2022 10:16:09 -0700 (PDT) Received: from localhost ([181.97.174.128]) by smtp.gmail.com with ESMTPSA id z13-20020a4a9c8d000000b0035e6c882517sm1471792ooj.16.2022.04.25.10.16.04 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 25 Apr 2022 10:16:09 -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 v7 4/8] x86/e820: Refactor __e820__range_update Date: Mon, 25 Apr 2022 14:15:22 -0300 Message-Id: <20220425171526.44925-5-martin.fernandez@eclypsium.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220425171526.44925-1-martin.fernandez@eclypsium.com> References: <20220425171526.44925-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 | 146 +++++++++++++++++++++++++---------------- 1 file changed, 89 insertions(+), 57 deletions(-) diff --git a/arch/x86/kernel/e820.c b/arch/x86/kernel/e820.c index 923585ab8377..763b8b20a1fd 100644 --- a/arch/x86/kernel/e820.c +++ b/arch/x86/kernel/e820.c @@ -607,80 +607,112 @@ __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; +/** + * struct e820_type_updater_data - Helper type for + * __e820__range_update(). + * @old_type: old_type parameter of __e820__range_update(). + * @new_type: new_type parameter of __e820__range_update(). + * + * This is intended to be used as the @data argument for the + * e820_entry_updater callbacks. + */ +struct e820_type_updater_data { + enum e820_type old_type; + enum e820_type new_type; +}; =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 + (const struct e820_type_updater_data *)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 + (const struct e820_type_updater_data *)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 + (const struct e820_type_updater_data *)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, original->crypto_capable); +} =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) +/** + * e820__range_update() - Update the type of a given address range in + * e820_table. + * @start: Start of the range. + * @size: Size of the range. + * @old_type: Type that we want to change. + * @new_type: New type to replace @old_type. + * + * Update type of addresses in [@start, @start + @size) from @old_type + * to @new_type in e820_table. + * + * Return: The size updated. + */ +u64 __init e820__range_update(u64 start, u64 size, enum e820_type old_type, + enum e820_type new_type) { return __e820__range_update(e820_table, start, size, old_type, new_type); } =20 -static u64 __init e820__range_update_kexec(u64 start, u64 size, enum e820_= type old_type, enum e820_type new_type) +/** + * e820__range_update_kexec() - Update the type of a given address + * range in e820_table_kexec. + * @start: Start of the range. + * @size: Size of the range. + * @old_type: Type that we want to change. + * @new_type: New type to replace @old_type. + * + * Update type of addresses in [@start, @start + @size) from @old_type + * to @new_type in e820_table_kexec. + * + * Return: The size updated. + */ +static u64 __init e820__range_update_kexec(u64 start, u64 size, + enum e820_type old_type, + enum e820_type new_type) { return __e820__range_update(e820_table_kexec, start, size, old_type, new_= type); } --=20 2.30.2 From nobody Sun May 10 19:52:58 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 6F879C433EF for ; Mon, 25 Apr 2022 17:16:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243876AbiDYRT4 (ORCPT ); Mon, 25 Apr 2022 13:19:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40998 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243958AbiDYRTm (ORCPT ); Mon, 25 Apr 2022 13:19:42 -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 B0981289AB for ; Mon, 25 Apr 2022 10:16:17 -0700 (PDT) Received: by mail-oa1-x31.google.com with SMTP id 586e51a60fabf-e5ca5c580fso16771646fac.3 for ; Mon, 25 Apr 2022 10:16:17 -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=ft+wZQIdPNos4T3lDuMvy0g55vrLthZNQPyCA6Ri2Oc=; b=aRdrpQIzsB5Rys7TKFmkToLr1c2+7gMgCFOy3LQCThR0i95Wdz60D85WV4oFBu6CP+ NO1HiG+5q4S26R7wLVT3F3xuPjL0NFcMFbGs8ilMjJ+Rnv0GQrkwI3D/uqHL2+HU/NaN pOL8mA1pjelM8jseVenZ/cNtAoiSrf569bi8DQdPzZ7hCdC5MQfL2ZNL+bCrxWN7akup NE0muzOpw0mXDW12dhOGZHESYV77gW+gwQEDEVf0FOExUdFl7nneauWzSDfcATb6bzv8 pbOUFGMAOnCG+imkMSUHMpmO7HHPZBqogf3HMvptmKVewa42f4DvUiHZ3Mz7SvPakHF7 BmtA== 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=ft+wZQIdPNos4T3lDuMvy0g55vrLthZNQPyCA6Ri2Oc=; b=vQPy5Dh2yK+XN7vGPgm4eCOXkFaBv1lzrU8PbWmHkhyMBQNcvpwihkddpd6qV3jA2n ueG1A9JPET8mrhszqURT4vGLdctZzdEuzkX4Luhn6fBRi3pj6x2iJzU09HRXHlSXjmOn C3XZcw4w7n/yh4CiRmqMcY5AxPrJH9hZqai6UW4m8O+S6fwfFwQ9Qv/z2f3i9vA5jaBN GPmRyS4680YleZ5Fe1LDThF+0/3r5dmI2tiv2n7juLDb43J7VQCQJJlksLmua56dbMow 8Q3ipzWQkW83kQYpMjLJ+B0FmepMPbsoWi+jjrULj47JIhwCexBcmCVrZu0tB/AYpfTL OVqQ== X-Gm-Message-State: AOAM530K+2eVyEd62GaDOz0cfDXaHYTlzbA+utGgnTH/pRu3fZmzQwrA icigjVakn10l8jt+JzsX3+WwBVPMGsqGCVXH6V4SzKo9z27hnFSxe6DOF+lJaULzwyzndFP2xc3 RiJA8Q6pNxKX+WYvQ+W6SnU3uLEbBtt70r3IkSmi+Oo9rY+aM3amx3XJKbq/ASivgF0DiYKWvS8 Ksgvxu1vuz1eypxJf0lxzU5w== X-Google-Smtp-Source: ABdhPJxhe3SolTYD/SoMykhNbjVWS3EDWFMCXc59Vp6VJr5Cun6bm5rlf51Rm3y1ZigNDcBZHtq7jw== X-Received: by 2002:a05:6870:63a7:b0:e9:363:51e1 with SMTP id t39-20020a05687063a700b000e9036351e1mr6295289oap.246.1650906976814; Mon, 25 Apr 2022 10:16:16 -0700 (PDT) Received: from localhost ([181.97.174.128]) by smtp.gmail.com with ESMTPSA id ep36-20020a056870a9a400b000de98fe4869sm3651433oab.35.2022.04.25.10.16.11 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 25 Apr 2022 10:16:16 -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 v7 5/8] x86/e820: Refactor e820__range_remove Date: Mon, 25 Apr 2022 14:15:23 -0300 Message-Id: <20220425171526.44925-6-martin.fernandez@eclypsium.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220425171526.44925-1-martin.fernandez@eclypsium.com> References: <20220425171526.44925-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 | 112 ++++++++++++++++++++++------------------- 1 file changed, 60 insertions(+), 52 deletions(-) diff --git a/arch/x86/kernel/e820.c b/arch/x86/kernel/e820.c index 763b8b20a1fd..9e32c9819e99 100644 --- a/arch/x86/kernel/e820.c +++ b/arch/x86/kernel/e820.c @@ -717,66 +717,74 @@ 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; +/** + * struct e820_remover_data - Helper type for e820__range_remove(). + * @old_type: old_type parameter of e820__range_remove(). + * @check_type: check_type parameter of e820__range_remove(). + * + * This is intended to be used as the @data argument for the + * e820_entry_updater callbacks. + */ +struct e820_remover_data { + enum e820_type old_type; + bool check_type; +}; =20 - if (check_type && entry->type !=3D old_type) - continue; +static bool __init remover__should_update(const struct e820_entry *entry, + const void *data) +{ + const struct e820_remover_data *remover_data =3D + (const struct e820_remover_data *)data; =20 - entry_end =3D entry->addr + entry->size; + return !remover_data->check_type || + entry->type =3D=3D remover_data->old_type; +} =20 - /* Completely covered? */ - if (entry->addr >=3D start && entry_end <=3D end) { - real_removed_size +=3D entry->size; - memset(entry, 0, sizeof(*entry)); - continue; - } +static void __init remover__update(struct e820_entry *entry, const void *d= ata) +{ + memset(entry, 0, sizeof(*entry)); +} =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__new(struct e820_table *table, u64 new_start, + u64 new_size, const struct e820_entry *original, + const void *data) +{ +} =20 - /* Partially covered: */ - final_start =3D max(start, entry->addr); - final_end =3D min(end, entry_end); - if (final_start >=3D final_end) - continue; +/** + * e820__range_remove() - Remove an address range from e820_table. + * @start: Start of the address range. + * @size: Size of the address range. + * @old_type: Type of the entries that we want to remove. + * @check_type: Bool to decide if ignore @old_type or not. + * + * Remove [@start, @start + @size) from e820_table. If @check_type is + * true remove only entries with type @old_type. + * + * Return: The size removed. + */ +u64 __init e820__range_remove(u64 start, u64 size, enum e820_type old_type, + bool check_type) +{ + struct e820_entry_updater updater =3D { + .should_update =3D remover__should_update, + .update =3D remover__update, + .new =3D remover__new + }; =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 May 10 19:52:58 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 C770BC433F5 for ; Mon, 25 Apr 2022 17:17:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243893AbiDYRUC (ORCPT ); Mon, 25 Apr 2022 13:20:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41272 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243970AbiDYRTp (ORCPT ); Mon, 25 Apr 2022 13:19:45 -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 847A82A25D for ; Mon, 25 Apr 2022 10:16:24 -0700 (PDT) Received: by mail-ot1-x32f.google.com with SMTP id w23-20020a056830111700b00603c6d1ce73so11226079otq.9 for ; Mon, 25 Apr 2022 10:16:24 -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=x5j2s/6auQucRxLAf9Ddhgi2axiFoZCC4vb8eW6IJg4=; b=UzhU/r/fsPHYwIlw4msXifyEI7nxZ+jG1YJiVhaXK/tyfW8cpYaJCZ9n22kKw1OTaG TAqWQmNdaAAvWUWJy7tuAEkFKC1k1xx+QiP9XFDwUVwNG+gqTR9JYyhie98DzQVDUFRC pN+fqO4/xUBw0XwidyE4pQ4iCaQ6mnO5WIzMxQT44KdRDaHXx8SUXSadxxN82C8OVMib 5s8lfCgk7cmVW/lOETzpLo+DdTXffubrQtnHKgYDM8QJpvhrg6T2d+rmPRY7tTq67C0a +nvNNcEmcxTrUWx7W38sayrPrRtfpyzPgOtD0A0hiKm057+YPcE21JGQ55likqqZyBOy DtMQ== 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=x5j2s/6auQucRxLAf9Ddhgi2axiFoZCC4vb8eW6IJg4=; b=M2MK6VKeDHcRM2vcu8WPFWBoPiYs5GqIoJ/3tM87SsukK8KTszAlTQ/bpJjmfWxJaI DN06NwP/+seU7A3lgvqA7QUfgkhDcIb9niuRgSIN7Vj34q7yoRbatJTsqw2XNWIOZNiS FHYeELVQ37QDDrk+b2AXNxLh01iQOCysJaTrPacsj5vJ16306O0ZC/voC/38E+EJ5eC8 NFNU4QvYQgz5pD10CRf1nbTpAv+XGOdAwaL/JeDWOODJ8/j21S08b4OE+pQrqSr5j92B LhGsylcQlnShsceY594uXCk5m4BqX559HDHtKZZG/ba4gxjkLwQCb0Zf3nNKIOT1QxNl Q2Dg== X-Gm-Message-State: AOAM533f70O5wjjNePxkiEzo/ibVGB0P/E2rAQJNka5z/RBAwUuaFIJ/ AD4Es6lb9DT8B6tJ3/T2/+FqOm4ILGewEibUQJHk5LVzdz5GqdQvcxybHWyagtF+dBDn9oEKadl eACAvuRbC8j/sgbcp4M1PtplenCi1RiK/hETfpXjn2vSKPMynm8cmEj32GwxjlYD+mujDmqXEyk jegnVInff9DohvtYvAsoA= X-Google-Smtp-Source: ABdhPJyQ41RMyEthWEjm+6ciDRpHPtmT8aytvsxpm9atcBLjPis+prWS1Cox48s8CUOayxhJspsWkg== X-Received: by 2002:a9d:7dcf:0:b0:605:4e52:fc20 with SMTP id k15-20020a9d7dcf000000b006054e52fc20mr6661239otn.332.1650906983399; Mon, 25 Apr 2022 10:16:23 -0700 (PDT) Received: from localhost ([181.97.174.128]) by smtp.gmail.com with ESMTPSA id e18-20020a544f12000000b002fa0db80b13sm3952553oiy.48.2022.04.25.10.16.18 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 25 Apr 2022 10:16:23 -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 v7 6/8] x86/e820: Tag e820_entry with crypto capabilities Date: Mon, 25 Apr 2022 14:15:24 -0300 Message-Id: <20220425171526.44925-7-martin.fernandez@eclypsium.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220425171526.44925-1-martin.fernandez@eclypsium.com> References: <20220425171526.44925-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 | 109 +++++++++++++++++++++++++++++- 3 files changed, 116 insertions(+), 6 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 9e32c9819e99..5837b7c1d197 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 @@ -787,6 +803,91 @@ u64 __init e820__range_remove(u64 start, u64 size, enu= m e820_type old_type, &data); } =20 +/** + * struct e820_crypto_updater_data - Helper type for + * __e820__range_update_crypto(). + * @crypto_capable: crypto_capable parameter of + * __e820__range_update_crypto(). + * + * This is intended to be used as the @data argument for the + * e820_entry_updater callbacks. + */ +struct e820_crypto_updater_data { + enum e820_crypto_capabilities crypto_capable; +}; + +static bool __init crypto_updater__should_update(const struct e820_entry *= entry, + const void *data) +{ + const struct e820_crypto_updater_data *crypto_updater_data =3D + (const struct e820_crypto_updater_data *)data; + + return crypto_updater_data->crypto_capable !=3D entry->crypto_capable; +} + +static void __init crypto_updater__update(struct e820_entry *entry, + const void *data) +{ + const struct e820_crypto_updater_data *crypto_updater_data =3D + (const struct e820_crypto_updater_data *)data; + + entry->crypto_capable =3D crypto_updater_data->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 struct e820_crypto_updater_data *crypto_updater_data =3D + (const struct e820_crypto_updater_data *)data; + + __e820__range_add(table, new_start, new_size, original->type, + crypto_updater_data->crypto_capable); +} + +static u64 __init +__e820__range_update_crypto(struct e820_table *table, u64 start, u64 size, + enum e820_crypto_capabilities crypto_capable) +{ + struct e820_entry_updater updater =3D { + .should_update =3D crypto_updater__should_update, + .update =3D crypto_updater__update, + .new =3D crypto_updater__new + }; + + struct e820_crypto_updater_data data =3D { + .crypto_capable =3D crypto_capable, + }; + + 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, &data); +} + +/** + * e820__range_set_crypto_capable() - Set %E820_CRYPTO_CAPABLE to a + * given range of addresses in e820_table. + * @start: Start of the range. + * @size: Size of the range. + * + * Set %E820_CRYPTO_CAPABLE to [@start, @start + @size) in e820_table. + * + * Return: The size updated. + */ +u64 __init e820__range_set_crypto_capable(u64 start, u64 size) +{ + return __e820__range_update_crypto(e820_table, start, size, + E820_CRYPTO_CAPABLE); +} + void __init e820__update_table_print(void) { if (e820__update_table(e820_table)) @@ -1529,6 +1630,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 May 10 19:52:58 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 41326C4332F for ; Mon, 25 Apr 2022 17:17:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243922AbiDYRUJ (ORCPT ); Mon, 25 Apr 2022 13:20:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41114 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243937AbiDYRTp (ORCPT ); Mon, 25 Apr 2022 13:19:45 -0400 Received: from mail-oo1-xc2e.google.com (mail-oo1-xc2e.google.com [IPv6:2607:f8b0:4864:20::c2e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 904032AE1A for ; Mon, 25 Apr 2022 10:16:31 -0700 (PDT) Received: by mail-oo1-xc2e.google.com with SMTP id f13-20020a4aa68d000000b0033a2c53d0baso2911880oom.0 for ; Mon, 25 Apr 2022 10:16:31 -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=hzdDmfKq8Bk9MII6db84BuRQTA/8rrlJsMn81H+8Z6fmZowtvXXor0jrWgoYGoELCw IsIzz074TMbE9AtUH2hfNN9cl4+cL2kALjO15QlLwx+J7IiSpWMDUQXN1ky+b6TyHLgV g8ZcTwU6ddQZlGfTw3ZExEZZIipHXbZS7hfXg/PZIIxAaplsyeLYNZu3Ph7olpJmw70s T5p5+74Sg002Z8SZlFK2U93bEY/7rC/66iOkS3hLBRCcIohkPQmE2rwS/StCBweQD5LG fGpr/YgqwEmPUoDwJp0CpaEWHLvQxyCttWiqadb6WN/2/2KYX9p/eIOXN7IoM+YLhNc3 l0dw== 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=oNTiJJSuZ+Fev7zatjvlw5uULZ/VO8hEyu9o2R6iqrkJgL3wL0jy3i2u55/1WWX96V Kiv6yGdFVtNJyagOjE+Qr5Y+jYiVeULbn66jdcWwLJu8LZa26rOXxSuT25nO2Efl0xng 8EG5NCUwDik9SP/C1dRLCSMc8kjrVc8ZZsHIkSJ++RsHN5wnbumdbZKZb20Hxp2Hvykb DdCqTh/7SCIAB9GS9CoThDxy4il0YvMniCow8udx/CXzgTxRuaK5vKYKsQLpuxYNJy9j PAd7eYlUM/1eOYbZXPFLe0fiMWC7sWRV775pdZwwnR5m1J8nZ37GOYRDR0/BaNt7HQ3Z 0U6g== X-Gm-Message-State: AOAM531Y7dPMwMoOPKqm5b6e0yihEd4+zaKh7qJ9L8n5EeAz7QjRIHyy JB0QsZUCWIdjTpRsOoyNYejEM9uJ8aCO3dXaNdhLli6sGCtwHr/LAoCWTEHEbG/7IyQgcTvOvnJ vJJL4sl2Y84m1l/DztZ+U8pzZxTC0g0IqtjdwPJ0tJTxbUCuWTUYleUgE4Y+Dq0BVnwQlY13YUe bKZbk+qQUXAdr1HAUrjbc= X-Google-Smtp-Source: ABdhPJw+qg0OrSQnHpLZqEEKRtsnl5QwBjo+IrjHQ4B0lUJPMGV26uBWO99+0KzzBzURpIrWg8ExmQ== X-Received: by 2002:a4a:92d4:0:b0:33a:3d7d:fe5 with SMTP id j20-20020a4a92d4000000b0033a3d7d0fe5mr6747103ooh.83.1650906990628; Mon, 25 Apr 2022 10:16:30 -0700 (PDT) Received: from localhost ([181.97.174.128]) by smtp.gmail.com with ESMTPSA id w8-20020a056830410800b00605b48122eesm1191834ott.14.2022.04.25.10.16.25 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 25 Apr 2022 10:16:30 -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 v7 7/8] x86/efi: Mark e820_entries as crypto capable from EFI memmap Date: Mon, 25 Apr 2022 14:15:25 -0300 Message-Id: <20220425171526.44925-8-martin.fernandez@eclypsium.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220425171526.44925-1-martin.fernandez@eclypsium.com> References: <20220425171526.44925-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 Sun May 10 19:52:58 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 93882C4332F for ; Mon, 25 Apr 2022 17:17:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243872AbiDYRUT (ORCPT ); Mon, 25 Apr 2022 13:20:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39182 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243983AbiDYRTr (ORCPT ); Mon, 25 Apr 2022 13:19:47 -0400 Received: from mail-oi1-x22a.google.com (mail-oi1-x22a.google.com [IPv6:2607:f8b0:4864:20::22a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E5E4726AC4 for ; Mon, 25 Apr 2022 10:16:38 -0700 (PDT) Received: by mail-oi1-x22a.google.com with SMTP id t15so17859725oie.1 for ; Mon, 25 Apr 2022 10:16: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=dBhJQBFFy4Ceg8cAsnJhQlAVWnX2jFGl2toK7LYg2vI=; b=ZUI1SW0LDagU7wptgLkLAyo11l74Ya3MsuwD1LvUoGpHB7JFCD9eGwk+TPuFbsg94J 1/2jlCIpJxuoSUuRE77FpuY8ivlJycTuspgiLAQKEm++YBdSPzIgc1bCHwvyU3bM1OIx QVQlUy6mpNLw/RH+old6c5hmH0AGxCuRDi6JZa0UvmfRHrSkPnUMz3ibircJSgTzvhqv vzyJoGr57dosEvqOjyqJIo0/ae+ilr8gmRG4Nybz3RfmwCFa0D2hnsmc/RJNg/8pRgkF kbG9O8mJJfwfTK2t76xzeoPxWz1lE+4Zb2NHMLo5de+3pUBSoQI789poPU5L2Sskqw9r QDHA== 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=dBhJQBFFy4Ceg8cAsnJhQlAVWnX2jFGl2toK7LYg2vI=; b=WlosovNO2PqrGdq9NSkiGFFSY9Kt+ROu3yY6NagpfnQgBLaBR0vqTiOWSgOJEqZP7e LSWcQyLktFZK6wepXMCWB8XndgJKbPGemntdLX88FNLpdfZuQ0U/CBVS2ebavZUXNzTN 1n6DKA3hihe0Aki9beKswKtujt3kTyu6MWs54rZyyHtOo1cCyNz3O7isSxeLXbnIXznE znT9NE13o8cArFFplBKB9piN9Tv/0cs4rygbgAgovw6A4KSydD2Xu+DhU8B+LhSC7oWJ mXl1jhOWrPw6LXQ1iEGVEJF/GklJliLz2meKLnVO8eQfcpTQBI9hsXZiuZHTKtBfc87L jWmQ== X-Gm-Message-State: AOAM532f0LHekJJviEIdUrJOmGn8TA04jw7EZai0Ta/EUAWM8L6taVZL 8np/sf5KQ1qWRDtpCO+vTfS9oQOWHMSMycbYoo2nfuYFwTe/zYwprSkmELW4pHcdoMC2IZ1gzqh YUFefm7/HSOOM2Eh/eASmI+UTWbS97ixq8vIErD2PhfigAIMrNpz504wk+AUU/Yar2r5+TZn6Zc NSb/QFO/QJtyFBNFOiisw= X-Google-Smtp-Source: ABdhPJw2CA5OnWQr+1gbJ1Fj/HVda/dNTu+4D8Lx2yhRTTRMdTF6nMZAB44F7+DCfcQSISEqTGmGkw== X-Received: by 2002:a54:4d86:0:b0:324:ecc3:fd02 with SMTP id y6-20020a544d86000000b00324ecc3fd02mr7359683oix.243.1650906997950; Mon, 25 Apr 2022 10:16:37 -0700 (PDT) Received: from localhost ([181.97.174.128]) by smtp.gmail.com with ESMTPSA id fz13-20020a056870ed8d00b000e593f1f26fsm3441269oab.18.2022.04.25.10.16.32 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 25 Apr 2022 10:16:37 -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 v7 8/8] drivers/node: Show in sysfs node's crypto capabilities Date: Mon, 25 Apr 2022 14:15:26 -0300 Message-Id: <20220425171526.44925-9-martin.fernandez@eclypsium.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220425171526.44925-1-martin.fernandez@eclypsium.com> References: <20220425171526.44925-1-martin.fernandez@eclypsium.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Show in each node in sysfs if its memory is able to do be encrypted by the CPU, ie. if all its memory is marked with EFI_MEMORY_CPU_CRYPTO in the EFI memory map. Signed-off-by: Martin Fernandez --- Documentation/ABI/testing/sysfs-devices-node | 10 ++++++++++ drivers/base/node.c | 10 ++++++++++ 2 files changed, 20 insertions(+) create mode 100644 Documentation/ABI/testing/sysfs-devices-node diff --git a/Documentation/ABI/testing/sysfs-devices-node b/Documentation/A= BI/testing/sysfs-devices-node new file mode 100644 index 000000000000..5fd5dc7fc2eb --- /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 + marked with EFI_MEMORY_CPU_CRYPTO, indicating that the + system memory is capable of being protected with the + CPU=E2=80=99s memory cryptographic capabilities. It is 0 + otherwise. \ No newline at end of file diff --git a/drivers/base/node.c b/drivers/base/node.c index 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