From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id AAE5FC433EF for ; Mon, 7 Feb 2022 21:18:41 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B54BA6B0074; Mon, 7 Feb 2022 16:18:40 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B048C6B0075; Mon, 7 Feb 2022 16:18:40 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9F4416B0078; Mon, 7 Feb 2022 16:18:40 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0045.hostedemail.com [216.40.44.45]) by kanga.kvack.org (Postfix) with ESMTP id 92A036B0074 for ; Mon, 7 Feb 2022 16:18:40 -0500 (EST) Received: from smtpin20.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 4E0F4180DA17A for ; Mon, 7 Feb 2022 21:18:40 +0000 (UTC) X-FDA: 79117248000.20.78390DE Received: from mail-pl1-f173.google.com (mail-pl1-f173.google.com [209.85.214.173]) by imf14.hostedemail.com (Postfix) with ESMTP id B740610000A for ; Mon, 7 Feb 2022 21:18:39 +0000 (UTC) Received: by mail-pl1-f173.google.com with SMTP id c3so12131832pls.5 for ; Mon, 07 Feb 2022 13:18:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=d9ldJz2Q7a+QVeuu97JN0SwCkiiYh+lvruWks9vjas4=; b=HN5KAg7FfgSlG7ghrTxnwTa8Ug/eFmqTBqT3YZyhVOi2WegMyHWZ9kBoUmgzz7iGCB 1Lu01bwgofvuB4skGH5FcZr7JQ0Tnxb1Zvf8OKjODPhmT7I6p8vX0ip5dSbHXWYIatCb rpbeA86N2Kb6Zi1ZTFPZ3r2GHF+ERV7cJxxuo= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=d9ldJz2Q7a+QVeuu97JN0SwCkiiYh+lvruWks9vjas4=; b=3AwZn5ZEPZrRTZmWCUqm6WCVbpZv+A37Pg2J/pxYYzj7oN3H7Mqgud82fjUhnRp4Sw 7e3hUzXbEpbdQ0m5nFhNFSWbON6IRgvkPmdWX5a0tQS/pN0wQ+d4Q49ZSigbzQbITJ3G GY54OsKcxEpqiSldC3g0lZW5nqBv4Fzg8sIPRciQ09SvLSiPHqgADNTLbl56Q92t/Ukq GV+ZtnHpHmsD7KPnc39+Fcl2fp4q2LWvVApxrsTvRwfLfqnFFrUx0tWK0eAnak+bv7YW XUpDRj1UPO2ETHnwbytzjoNUbtma5FI7n4KDyi1qsArKMqRNxyjQrw0qfaGHnSgODitk 9Q0A== X-Gm-Message-State: AOAM5336txMo1Esyh9UH4J/c7ksWfjVBbei5qpmokKu5mRSPqWr8sG81 K6ptSw2i2hlQFz319+xIzwfJzA== X-Google-Smtp-Source: ABdhPJyibyWrBtKFyxmmMoKFcsegALaR2GiBWlPPZmpl80cyGmRpdn7SL+WBVzHCcvr1CvrcX8MA+Q== X-Received: by 2002:a17:90a:348f:: with SMTP id p15mr858535pjb.173.1644268718557; Mon, 07 Feb 2022 13:18:38 -0800 (PST) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id lk8sm263098pjb.40.2022.02.07.13.18.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Feb 2022 13:18:38 -0800 (PST) Date: Mon, 7 Feb 2022 13:18:37 -0800 From: Kees Cook To: Martin Fernandez Cc: linux-kernel@vger.kernel.org, linux-efi@vger.kernel.org, platform-driver-x86@vger.kernel.org, linux-mm@kvack.org, 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 Subject: Re: [PATCH v6 1/6] mm/memblock: Tag memblocks with crypto capabilities Message-ID: <202202061945.DACC7BD04@keescook> References: <20220203164328.203629-1-martin.fernandez@eclypsium.com> <20220203164328.203629-2-martin.fernandez@eclypsium.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220203164328.203629-2-martin.fernandez@eclypsium.com> X-Stat-Signature: aci7fyg61uqwhc8epqijio4th6xerfp8 X-Rspam-User: nil Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=HN5KAg7F; spf=pass (imf14.hostedemail.com: domain of keescook@chromium.org designates 209.85.214.173 as permitted sender) smtp.mailfrom=keescook@chromium.org; dmarc=pass (policy=none) header.from=chromium.org X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: B740610000A X-HE-Tag: 1644268719-897418 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Thu, Feb 03, 2022 at 01:43:23PM -0300, Martin Fernandez wrote: > Add the capability to mark regions of the memory memory_type able of > hardware memory encryption. > > Also add the capability to query if all regions of a memory node are > able to do hardware memory encryption to call it when initializing the > nodes. Warn the user if a node has both encryptable and > non-encryptable regions. > > Signed-off-by: Martin Fernandez > --- > include/linux/memblock.h | 15 ++++++---- > mm/memblock.c | 64 ++++++++++++++++++++++++++++++++++++++++ > 2 files changed, 74 insertions(+), 5 deletions(-) > > diff --git a/include/linux/memblock.h b/include/linux/memblock.h > index 9dc7cb239d21..73edcce165a5 100644 > --- a/include/linux/memblock.h > +++ b/include/linux/memblock.h > @@ -41,13 +41,15 @@ extern unsigned long long max_possible_pfn; > * via a driver, and never indicated in the firmware-provided memory map as > * system RAM. This corresponds to IORESOURCE_SYSRAM_DRIVER_MANAGED in the > * kernel resource tree. > + * @MEMBLOCK_CRYPTO_CAPABLE: capable of hardware encryption > */ > enum memblock_flags { > - MEMBLOCK_NONE = 0x0, /* No special request */ > - MEMBLOCK_HOTPLUG = 0x1, /* hotpluggable region */ > - MEMBLOCK_MIRROR = 0x2, /* mirrored region */ > - MEMBLOCK_NOMAP = 0x4, /* don't add to kernel direct mapping */ > - MEMBLOCK_DRIVER_MANAGED = 0x8, /* always detected via a driver */ > + MEMBLOCK_NONE = 0x0, /* No special request */ > + MEMBLOCK_HOTPLUG = 0x1, /* hotpluggable region */ > + MEMBLOCK_MIRROR = 0x2, /* mirrored region */ > + MEMBLOCK_NOMAP = 0x4, /* don't add to kernel direct mapping */ > + MEMBLOCK_DRIVER_MANAGED = 0x8, /* always detected via a driver */ > + MEMBLOCK_CRYPTO_CAPABLE = 0x10, /* capable of hardware encryption */ As already suggested, please keep the tabs like they were. If you're going to change every line, maybe expand the single-digit literals to 2 digits. (i.e. 0x0 -> 0x00, to keep the most significant bits lined up.) > }; > > /** > @@ -121,6 +123,9 @@ int memblock_physmem_add(phys_addr_t base, phys_addr_t size); > void memblock_trim_memory(phys_addr_t align); > bool memblock_overlaps_region(struct memblock_type *type, > phys_addr_t base, phys_addr_t size); > +bool memblock_node_is_crypto_capable(int nid); > +int memblock_mark_crypto_capable(phys_addr_t base, phys_addr_t size); > +int memblock_clear_crypto_capable(phys_addr_t base, phys_addr_t size); > int memblock_mark_hotplug(phys_addr_t base, phys_addr_t size); > int memblock_clear_hotplug(phys_addr_t base, phys_addr_t size); > int memblock_mark_mirror(phys_addr_t base, phys_addr_t size); > diff --git a/mm/memblock.c b/mm/memblock.c > index 1018e50566f3..fcf79befeab3 100644 > --- a/mm/memblock.c > +++ b/mm/memblock.c > @@ -191,6 +191,42 @@ bool __init_memblock memblock_overlaps_region(struct memblock_type *type, > return i < type->cnt; > } > > +/** > + * memblock_node_is_crypto_capable - get if whole node is capable > + * of encryption > + * @nid: number of node > + * > + * Iterate over all memory memblock_type and find if all regions under > + * node @nid are capable of hardware encryption. > + * > + * Return: > + * true if every region in memory memblock_type is capable of > + * encryption, false otherwise. > + */ > +bool __init_memblock memblock_node_is_crypto_capable(int nid) > +{ > + struct memblock_region *region; > + bool crypto_capable = false; > + bool not_crypto_capable = false; > + > + for_each_mem_region(region) { > + if (memblock_get_region_node(region) == nid) { > + crypto_capable = > + crypto_capable || > + (region->flags & MEMBLOCK_CRYPTO_CAPABLE); This was already mentioned, but I just thought I'd add: this made me double-take, given the "||" (instead of "|") in an assignment. It looked like a typo, but yes it's correct. I was expecting something like: crypto_capable |= !!(region->flags & MEMBLOCK_CRYPTO_CAPABLE); > + not_crypto_capable = > + not_crypto_capable || > + !(region->flags & MEMBLOCK_CRYPTO_CAPABLE); not_crypto_capable |= !(region->flags & MEMBLOCK_CRYPTO_CAPABLE); > + } > + } > + > + if (crypto_capable && not_crypto_capable) > + pr_warn_once("Node %d has regions that are encryptable and regions that aren't", > + nid); > + > + return !not_crypto_capable; > +} > + > /** > * __memblock_find_range_bottom_up - find free area utility in bottom-up > * @start: start of candidate range > @@ -885,6 +921,34 @@ static int __init_memblock memblock_setclr_flag(phys_addr_t base, > return 0; > } > > +/** > + * memblock_mark_crypto_capable - Mark memory regions capable of hardware > + * encryption with flag MEMBLOCK_CRYPTO_CAPABLE. > + * @base: the base phys addr of the region > + * @size: the size of the region > + * > + * Return: 0 on success, -errno on failure. > + */ > +int __init_memblock memblock_mark_crypto_capable(phys_addr_t base, > + phys_addr_t size) > +{ > + return memblock_setclr_flag(base, size, 1, MEMBLOCK_CRYPTO_CAPABLE); > +} > + > +/** > + * memblock_clear_crypto_capable - Clear flag MEMBLOCK_CRYPTO for a > + * specified region. > + * @base: the base phys addr of the region > + * @size: the size of the region > + * > + * Return: 0 on success, -errno on failure. > + */ > +int __init_memblock memblock_clear_crypto_capable(phys_addr_t base, > + phys_addr_t size) > +{ > + return memblock_setclr_flag(base, size, 0, MEMBLOCK_CRYPTO_CAPABLE); > +} > + > /** > * memblock_mark_hotplug - Mark hotpluggable memory with flag MEMBLOCK_HOTPLUG. > * @base: the base phys addr of the region > -- > 2.30.2 > -- Kees Cook