From patchwork Thu Oct 10 07:47:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Huang, Ying" X-Patchwork-Id: 13829711 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 00330CF07BD for ; Thu, 10 Oct 2024 07:47:58 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4405E6B0082; Thu, 10 Oct 2024 03:47:58 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3F0136B0083; Thu, 10 Oct 2024 03:47:58 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 26A096B0085; Thu, 10 Oct 2024 03:47:58 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 060516B0082 for ; Thu, 10 Oct 2024 03:47:57 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id E081881411 for ; Thu, 10 Oct 2024 07:47:54 +0000 (UTC) X-FDA: 82656913794.28.74FC347 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.11]) by imf05.hostedemail.com (Postfix) with ESMTP id CCFF5100003 for ; Thu, 10 Oct 2024 07:47:52 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b="kDw6/uZH"; spf=pass (imf05.hostedemail.com: domain of ying.huang@intel.com designates 192.198.163.11 as permitted sender) smtp.mailfrom=ying.huang@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1728546293; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:references:dkim-signature; bh=dMEQ5A18s+M6WaxXnMJq23X6egw7lYR46KzEruQzp90=; b=lIIMlzIClRK8DiCPg2AdvhqGJIM0J/89NjAXDUrt5xyN9U9loPQshqm5miO4FFuGM5g2Q4 TB6EgZsDNNX5ph6SFvsfVvnsUG7zuZBfXtNS2YukoR+I6DltSep6YNVeTBungeKlTQ9hdv Zud8a5McIbfMo3EKCpw9rLwIZj15TyA= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b="kDw6/uZH"; spf=pass (imf05.hostedemail.com: domain of ying.huang@intel.com designates 192.198.163.11 as permitted sender) smtp.mailfrom=ying.huang@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1728546293; a=rsa-sha256; cv=none; b=UkitwEZl9UJZlzDqsdnokyQZm3VTH9Z/waKZ11A3CwgZuytgCA77r1kv/CuRcRxobqbTPW /DiElok0mFRs+oYwH9YkyWmGQz43LojTbHZJgg7sJvb+r22WzkNgrxz4+jj0axVlT5Owga 6ZYyaw0gI8VnOgFNXLZCd/cihAGaKkU= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1728546475; x=1760082475; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=br+zYnMwtP2W99coXs+94igddUMorqNE7meqm6enj60=; b=kDw6/uZHMpIM915deEEgcPBs6Qg1XvrYmywOwxCCUEtu4yYiihdSxj/a ZHB6w7Uhd3biy+cTlceHTAqNkUY2BQ16LELCbzHgmnXNsCz7V/O6NPpqm mBlYyE1ec+xzGDEPo4dhf9OU9Ijxvu2KmtAhFpR/rf1E2loKEWPs0BvIT ywetlun58kxbIGpRoaBa3UzccA58/7reKZkjFAg2d0pGC475ZX8cVOrlf BPeXVRYd3tqYJsMbDjM5vll3Oyue2zywQO/OWVaQMUqBYIebvSxv3uK47 6L9E478PWz7cJK5JmQ6xz7cAfUZ4+tPmkBSL7mMHpgMFGprHEJVIwWMx8 w==; X-CSE-ConnectionGUID: NJexHO3kQwGe8FdfexDSbg== X-CSE-MsgGUID: iXBAgRwQTNqh0ch46gA/IQ== X-IronPort-AV: E=McAfee;i="6700,10204,11220"; a="38472900" X-IronPort-AV: E=Sophos;i="6.11,192,1725346800"; d="scan'208";a="38472900" Received: from orviesa005.jf.intel.com ([10.64.159.145]) by fmvoesa105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Oct 2024 00:47:48 -0700 X-CSE-ConnectionGUID: 89YLszNPRTyY143WsKhdvw== X-CSE-MsgGUID: HB4PB0d6TX+q+6mLkQ42Nw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,192,1725346800"; d="scan'208";a="81307090" Received: from unknown (HELO yhuang6-mobl2.ccr.corp.intel.com) ([10.245.243.193]) by orviesa005-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Oct 2024 00:47:44 -0700 From: Huang Ying To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "Kirill A . Shutemov" Cc: x86@kernel.org, linux-coco@lists.linux.dev, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Huang Ying , Dan Williams , Kai Huang , David Hildenbrand , "H. Peter Anvin" , Andy Lutomirski , Oscar Salvador Subject: [PATCH -V2] tdx, memory hotplug: Check whole hot-adding memory range for TDX Date: Thu, 10 Oct 2024 15:47:26 +0800 Message-Id: <20241010074726.1397820-1-ying.huang@intel.com> X-Mailer: git-send-email 2.39.2 MIME-Version: 1.0 X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: CCFF5100003 X-Stat-Signature: 3uxj6d8w7bt3bwsxg443zq6mrdyxnnys X-Rspam-User: X-H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BWxFDCK5fb1/dI+CcF7MDGvNMlTKj76PeQrCKqE7pAPwCr+n8DMNKd/8gMpAywCEyhQGzrgHpRW4W9jVe5+0CfX4vtAcJga9N05uHuqEel9UII8gcUVFy5SVb8SPniqSBvFlP5ROKjiu4VU9O8xKxGVZ//fJQNxYFh926FoaigI3C7lu5kB71NLvru8x4D8hP+JRTagk0qAGGDAQqvQVLyKDDNapg6NM+8ONaTPxXe 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: List-Subscribe: List-Unsubscribe: On systems with TDX (Trust Domain eXtensions) enabled, memory ranges hot-added must be checked for compatibility by TDX. This is currently implemented through memory hotplug notifiers for each memory_block. If a memory range which isn't TDX compatible is hot-added, for example, some CXL memory, the command line as follows, $ echo 1 > /sys/devices/system/node/nodeX/memoryY/online will report something like, bash: echo: write error: Operation not permitted If pr_debug() is enabled, the error message like below will be shown in the kernel log, online_pages [mem 0xXXXXXXXXXX-0xXXXXXXXXXX] failed Both are too general to root cause the problem. This will confuse users. One solution is to print some error messages in the TDX memory hotplug notifier. However, memory hotplug notifiers are called for each memory block, so this may lead to a large volume of messages in the kernel log if a large number of memory blocks are onlined with a script or automatically. For example, the typical size of memory block is 128MB on x86_64, when online 64GB CXL memory, 512 messages will be logged. Therefore, in this patch, the whole hot-adding memory range is checked for TDX compatibility through a newly added architecture specific function (arch_check_hotplug_memory_range()). If rejected, the memory hot-adding will be aborted with a proper kernel log message. Which looks like something as below, virt/tdx: Reject hot-adding memory range: 0xXXXXXXXX-0xXXXXXXXX for TDX compatibility. The target use case is to support CXL memory on TDX enabled systems. If the CXL memory isn't compatible with TDX, the whole CXL memory range hot-adding will be rejected. While the CXL memory can still be used via devdax interface. This also makes the original TDX memory hotplug notifier useless, so delete it. Signed-off-by: "Huang, Ying" Suggested-by: Dan Williams Reviewed-by: Dan Williams Reviewed-by: Kirill A. Shutemov Acked-by: Kai Huang Acked-by: David Hildenbrand Cc: Thomas Gleixner Cc: Dave Hansen Cc: "H. Peter Anvin" Cc: Andy Lutomirski Cc: Oscar Salvador --- Changes: v2: - Collected reviewed-by and acked-by. - Added comments for tdx_check_hotplug_memory_range(), Thanks David! - Link to v1: https://lore.kernel.org/lkml/20240930055112.344206-1-ying.huang@intel.com/ --- arch/x86/include/asm/tdx.h | 2 ++ arch/x86/mm/init_64.c | 6 +++++ arch/x86/virt/vmx/tdx/tdx.c | 40 +++++++++++++++------------------- include/linux/memory_hotplug.h | 3 +++ mm/memory_hotplug.c | 7 +++++- 5 files changed, 34 insertions(+), 24 deletions(-) diff --git a/arch/x86/include/asm/tdx.h b/arch/x86/include/asm/tdx.h index eba178996d84..6db5da34e4ba 100644 --- a/arch/x86/include/asm/tdx.h +++ b/arch/x86/include/asm/tdx.h @@ -116,11 +116,13 @@ static inline u64 sc_retry(sc_func_t func, u64 fn, int tdx_cpu_enable(void); int tdx_enable(void); const char *tdx_dump_mce_info(struct mce *m); +int tdx_check_hotplug_memory_range(u64 start, u64 size); #else static inline void tdx_init(void) { } static inline int tdx_cpu_enable(void) { return -ENODEV; } static inline int tdx_enable(void) { return -ENODEV; } static inline const char *tdx_dump_mce_info(struct mce *m) { return NULL; } +static inline int tdx_check_hotplug_memory_range(u64 start, u64 size) { return 0; } #endif /* CONFIG_INTEL_TDX_HOST */ #endif /* !__ASSEMBLY__ */ diff --git a/arch/x86/mm/init_64.c b/arch/x86/mm/init_64.c index ff253648706f..30a4ad4272ce 100644 --- a/arch/x86/mm/init_64.c +++ b/arch/x86/mm/init_64.c @@ -55,6 +55,7 @@ #include #include #include +#include #include "mm_internal.h" @@ -974,6 +975,11 @@ int add_pages(int nid, unsigned long start_pfn, unsigned long nr_pages, return ret; } +int arch_check_hotplug_memory_range(u64 start, u64 size) +{ + return tdx_check_hotplug_memory_range(start, size); +} + int arch_add_memory(int nid, u64 start, u64 size, struct mhp_params *params) { diff --git a/arch/x86/virt/vmx/tdx/tdx.c b/arch/x86/virt/vmx/tdx/tdx.c index 4e2b2e2ac9f9..f70b4ebe7cc5 100644 --- a/arch/x86/virt/vmx/tdx/tdx.c +++ b/arch/x86/virt/vmx/tdx/tdx.c @@ -1388,36 +1388,37 @@ static bool is_tdx_memory(unsigned long start_pfn, unsigned long end_pfn) return false; } -static int tdx_memory_notifier(struct notifier_block *nb, unsigned long action, - void *v) +/* + * We don't allow mixture of TDX and !TDX memory in the buddy so we + * won't run into trouble when launching encrypted VMs that really + * need TDX-capable memory. + */ +int tdx_check_hotplug_memory_range(u64 start, u64 size) { - struct memory_notify *mn = v; - - if (action != MEM_GOING_ONLINE) - return NOTIFY_OK; + u64 start_pfn = PHYS_PFN(start); + u64 end_pfn = PHYS_PFN(start + size); /* * Empty list means TDX isn't enabled. Allow any memory - * to go online. + * to be hot-added. */ if (list_empty(&tdx_memlist)) - return NOTIFY_OK; + return 0; /* * The TDX memory configuration is static and can not be - * changed. Reject onlining any memory which is outside of + * changed. Reject hot-adding any memory which is outside of * the static configuration whether it supports TDX or not. */ - if (is_tdx_memory(mn->start_pfn, mn->start_pfn + mn->nr_pages)) - return NOTIFY_OK; + if (is_tdx_memory(start_pfn, end_pfn)) + return 0; - return NOTIFY_BAD; + pr_info("Reject hot-adding memory range: %#llx-%#llx for TDX compatibility.\n", + start, start + size); + + return -EINVAL; } -static struct notifier_block tdx_memory_nb = { - .notifier_call = tdx_memory_notifier, -}; - static void __init check_tdx_erratum(void) { /* @@ -1465,13 +1466,6 @@ void __init tdx_init(void) return; } - err = register_memory_notifier(&tdx_memory_nb); - if (err) { - pr_err("initialization failed: register_memory_notifier() failed (%d)\n", - err); - return; - } - #if defined(CONFIG_ACPI) && defined(CONFIG_SUSPEND) pr_info("Disable ACPI S3. Turn off TDX in the BIOS to use ACPI S3.\n"); acpi_suspend_lowlevel = NULL; diff --git a/include/linux/memory_hotplug.h b/include/linux/memory_hotplug.h index b27ddce5d324..c5ba7b909bb4 100644 --- a/include/linux/memory_hotplug.h +++ b/include/linux/memory_hotplug.h @@ -140,6 +140,9 @@ extern int try_online_node(int nid); extern int arch_add_memory(int nid, u64 start, u64 size, struct mhp_params *params); + +extern int arch_check_hotplug_memory_range(u64 start, u64 size); + extern u64 max_mem_size; extern int mhp_online_type_from_str(const char *str); diff --git a/mm/memory_hotplug.c b/mm/memory_hotplug.c index 621ae1015106..c4769f24b1e2 100644 --- a/mm/memory_hotplug.c +++ b/mm/memory_hotplug.c @@ -1305,6 +1305,11 @@ int try_online_node(int nid) return ret; } +int __weak arch_check_hotplug_memory_range(u64 start, u64 size) +{ + return 0; +} + static int check_hotplug_memory_range(u64 start, u64 size) { /* memory range must be block size aligned */ @@ -1315,7 +1320,7 @@ static int check_hotplug_memory_range(u64 start, u64 size) return -EINVAL; } - return 0; + return arch_check_hotplug_memory_range(start, size); } static int online_memory_block(struct memory_block *mem, void *arg)