From nobody Sun Feb 8 00:12:06 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 CEC41C7EE29 for ; Fri, 9 Jun 2023 17:12:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229944AbjFIRMs (ORCPT ); Fri, 9 Jun 2023 13:12:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60460 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229503AbjFIRMp (ORCPT ); Fri, 9 Jun 2023 13:12:45 -0400 Received: from mga14.intel.com (mga14.intel.com [192.55.52.115]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E2CB9C1 for ; Fri, 9 Jun 2023 10:12:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1686330763; x=1717866763; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=zpfHxU4Ucz0hkY3lY7l9y3mAQLbd7FMy7NJHOD5zHn8=; b=GIJg0Jzbs3OtoONEQkdQg6bS0hr7OAFsxYmFRNAjCMnb7BYPKUAks/BL sVMVGfpc5M/d2tb9To/38Cx2V1/Kn4Rsp5/exehc9w5BrNhJDYoJ8wMCC puqrqZlQy0Ebv3j3zKAOa4cN0plI+nEiyVTmJnNT2kUBC3oeT7fzUSEgL rrS8Y0dMKclnXsJDIDr6TgfLoLJZdVdHlKMO3zLjjwz8INh75wRgWpIUi kUemub/C9h7LwLFis5AXMK1+bcYIe3ojo9MSk/1+eBg/8hZKdKYQYO+rz bcyFyKXGI55EbYJSlPmdzuWg7w3khe3FLGcLn3IWbqh7a2T/GIX98IuAO w==; X-IronPort-AV: E=McAfee;i="6600,9927,10736"; a="357647321" X-IronPort-AV: E=Sophos;i="6.00,229,1681196400"; d="scan'208";a="357647321" Received: from orsmga003.jf.intel.com ([10.7.209.27]) by fmsmga103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 09 Jun 2023 10:12:43 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10736"; a="660824635" X-IronPort-AV: E=Sophos;i="6.00,229,1681196400"; d="scan'208";a="660824635" Received: from black.fi.intel.com (HELO black.fi.intel.com.) ([10.237.72.28]) by orsmga003.jf.intel.com with ESMTP; 09 Jun 2023 10:12:40 -0700 From: Alexander Shishkin To: linux-kernel@vger.kernel.org, x86@kernel.org Cc: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "H. Peter Anvin" , Andy Lutomirski , Peter Zijlstra , Alison Schofield , Alexander Shishkin Subject: [PATCH v2 1/2] x86/sev: Move sev_setup_arch() to mem_encrypt.c Date: Fri, 9 Jun 2023 20:12:13 +0300 Message-Id: <20230609171214.31846-2-alexander.shishkin@linux.intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230609171214.31846-1-alexander.shishkin@linux.intel.com> References: <20230609171214.31846-1-alexander.shishkin@linux.intel.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" Since commit 4d96f9109109b ("x86/sev: Replace occurrences of sev_active() with cc_platform_has()"), the SWIOTLB bounce buffer size adjustment and restricted virtio memory setting also inadvertently apply to TDX: the code is using cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT) as a gatekeeping condition, which is also true for TDX, and this is also what we want. To reflect this, move the corresponding code to generic mem_encrypt.c. No functional changes intended. Signed-off-by: Alexander Shishkin --- arch/x86/include/asm/mem_encrypt.h | 11 ++++++++-- arch/x86/kernel/setup.c | 2 +- arch/x86/mm/mem_encrypt.c | 34 +++++++++++++++++++++++++++++ arch/x86/mm/mem_encrypt_amd.c | 35 ------------------------------ 4 files changed, 44 insertions(+), 38 deletions(-) diff --git a/arch/x86/include/asm/mem_encrypt.h b/arch/x86/include/asm/mem_= encrypt.h index b7126701574c..4283063c1e1c 100644 --- a/arch/x86/include/asm/mem_encrypt.h +++ b/arch/x86/include/asm/mem_encrypt.h @@ -37,7 +37,6 @@ void __init sme_map_bootdata(char *real_mode_data); void __init sme_unmap_bootdata(char *real_mode_data); =20 void __init sme_early_init(void); -void __init sev_setup_arch(void); =20 void __init sme_encrypt_kernel(struct boot_params *bp); void __init sme_enable(struct boot_params *bp); @@ -67,7 +66,6 @@ static inline void __init sme_map_bootdata(char *real_mod= e_data) { } static inline void __init sme_unmap_bootdata(char *real_mode_data) { } =20 static inline void __init sme_early_init(void) { } -static inline void __init sev_setup_arch(void) { } =20 static inline void __init sme_encrypt_kernel(struct boot_params *bp) { } static inline void __init sme_enable(struct boot_params *bp) { } @@ -92,6 +90,15 @@ void __init mem_encrypt_init(void); =20 void add_encrypt_protection_map(void); =20 +#ifdef CONFIG_X86_MEM_ENCRYPT + +void __init mem_encrypt_setup_arch(void); + +#else /* !CONFIG_X86_MEM_ENCRYPT */ + +static inline void __init mem_encrypt_setup_arch(void) { } + +#endif /* CONFIG_X86_MEM_ENCRYPT */ /* * The __sme_pa() and __sme_pa_nodebug() macros are meant for use when * writing to or comparing values from the cr3 register. Having the diff --git a/arch/x86/kernel/setup.c b/arch/x86/kernel/setup.c index 16babff771bd..e2aa1d5b37a9 100644 --- a/arch/x86/kernel/setup.c +++ b/arch/x86/kernel/setup.c @@ -1121,7 +1121,7 @@ void __init setup_arch(char **cmdline_p) * Needs to run after memblock setup because it needs the physical * memory size. */ - sev_setup_arch(); + mem_encrypt_setup_arch(); =20 efi_fake_memmap(); efi_find_mirror(); diff --git a/arch/x86/mm/mem_encrypt.c b/arch/x86/mm/mem_encrypt.c index 9f27e14e185f..c290c55b632b 100644 --- a/arch/x86/mm/mem_encrypt.c +++ b/arch/x86/mm/mem_encrypt.c @@ -12,6 +12,7 @@ #include #include #include +#include =20 /* Override for DMA direct allocation check - ARCH_HAS_FORCE_DMA_UNENCRYPT= ED */ bool force_dma_unencrypted(struct device *dev) @@ -86,3 +87,36 @@ void __init mem_encrypt_init(void) =20 print_mem_encrypt_feature_info(); } + +void __init mem_encrypt_setup_arch(void) +{ + phys_addr_t total_mem =3D memblock_phys_mem_size(); + unsigned long size; + + if (!cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT)) + return; + + /* + * For SEV and TDX, all DMA has to occur via shared/unencrypted pages. + * Kernel uses SWIOTLB to make this happen without changing device + * drivers. However, depending on the workload being run, the + * default 64MB of SWIOTLB may not be enough and SWIOTLB may + * run out of buffers for DMA, resulting in I/O errors and/or + * performance degradation especially with high I/O workloads. + * + * Adjust the default size of SWIOTLB using a percentage of guest + * memory for SWIOTLB buffers. Also, as the SWIOTLB bounce buffer + * memory is allocated from low memory, ensure that the adjusted size + * is within the limits of low available memory. + * + * The percentage of guest memory used here for SWIOTLB buffers + * is more of an approximation of the static adjustment which + * 64MB for <1G, and ~128M to 256M for 1G-to-4G, i.e., the 6% + */ + size =3D total_mem * 6 / 100; + size =3D clamp_val(size, IO_TLB_DEFAULT_SIZE, SZ_1G); + swiotlb_adjust_size(size); + + /* Set restricted memory access for virtio. */ + virtio_set_mem_acc_cb(virtio_require_restricted_mem_acc); +} diff --git a/arch/x86/mm/mem_encrypt_amd.c b/arch/x86/mm/mem_encrypt_amd.c index e0b51c09109f..b1edeb8642b8 100644 --- a/arch/x86/mm/mem_encrypt_amd.c +++ b/arch/x86/mm/mem_encrypt_amd.c @@ -20,7 +20,6 @@ #include #include #include -#include #include =20 #include @@ -215,40 +214,6 @@ void __init sme_map_bootdata(char *real_mode_data) __sme_early_map_unmap_mem(__va(cmdline_paddr), COMMAND_LINE_SIZE, true); } =20 -void __init sev_setup_arch(void) -{ - phys_addr_t total_mem =3D memblock_phys_mem_size(); - unsigned long size; - - if (!cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT)) - return; - - /* - * For SEV, all DMA has to occur via shared/unencrypted pages. - * SEV uses SWIOTLB to make this happen without changing device - * drivers. However, depending on the workload being run, the - * default 64MB of SWIOTLB may not be enough and SWIOTLB may - * run out of buffers for DMA, resulting in I/O errors and/or - * performance degradation especially with high I/O workloads. - * - * Adjust the default size of SWIOTLB for SEV guests using - * a percentage of guest memory for SWIOTLB buffers. - * Also, as the SWIOTLB bounce buffer memory is allocated - * from low memory, ensure that the adjusted size is within - * the limits of low available memory. - * - * The percentage of guest memory used here for SWIOTLB buffers - * is more of an approximation of the static adjustment which - * 64MB for <1G, and ~128M to 256M for 1G-to-4G, i.e., the 6% - */ - size =3D total_mem * 6 / 100; - size =3D clamp_val(size, IO_TLB_DEFAULT_SIZE, SZ_1G); - swiotlb_adjust_size(size); - - /* Set restricted memory access for virtio. */ - virtio_set_mem_acc_cb(virtio_require_restricted_mem_acc); -} - static unsigned long pg_level_to_pfn(int level, pte_t *kpte, pgprot_t *ret= _prot) { unsigned long pfn =3D 0; --=20 2.39.2 From nobody Sun Feb 8 00:12:06 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 210EAC7EE29 for ; Fri, 9 Jun 2023 17:13:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230061AbjFIRNA (ORCPT ); Fri, 9 Jun 2023 13:13:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60536 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230041AbjFIRMw (ORCPT ); Fri, 9 Jun 2023 13:12:52 -0400 Received: from mga14.intel.com (mga14.intel.com [192.55.52.115]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 32D0B1734 for ; Fri, 9 Jun 2023 10:12:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1686330767; x=1717866767; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=WQeK8cRefKQp/n56iwZnAne7cbfUjkNfhV5SR+TckvM=; b=NP5G2np32NyJUPb0/uDiSyJpFNnIAXoRj1YY11qfuLM6qIzhtLBSrrnP MNqGrP7KmQQZtFhawbYmIGK3WrV+iJ21RyyD37UWrG2Yi2I5o+XMLa4la U3WZKiOxAf3n5+aunOha81HYKo+V1EZn9UUR/yxCuZG9LIZfrkj7s8enG KZxFaLB1iJut/CufCCHFJk3ZXQmlYELYOEpRZ6nocdy0EaynZVvq5uKy9 GkrE9qNi2pdlXYb+x5W7eudwvwL6WZrKOwHcASPmsszVIy1DIUztuuYWA DD94gCQomQ8o/0p7mXxFEFIIYB+PnzEQxLtMIHnWRKgWVeM6/L2TBKfP/ A==; X-IronPort-AV: E=McAfee;i="6600,9927,10736"; a="357647346" X-IronPort-AV: E=Sophos;i="6.00,229,1681196400"; d="scan'208";a="357647346" Received: from orsmga003.jf.intel.com ([10.7.209.27]) by fmsmga103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 09 Jun 2023 10:12:46 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10736"; a="660824653" X-IronPort-AV: E=Sophos;i="6.00,229,1681196400"; d="scan'208";a="660824653" Received: from black.fi.intel.com (HELO black.fi.intel.com.) ([10.237.72.28]) by orsmga003.jf.intel.com with ESMTP; 09 Jun 2023 10:12:43 -0700 From: Alexander Shishkin To: linux-kernel@vger.kernel.org, x86@kernel.org Cc: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "H. Peter Anvin" , Andy Lutomirski , Peter Zijlstra , Alison Schofield , Alexander Shishkin Subject: [PATCH v2 2/2] x86/sev: Drop unneeded include Date: Fri, 9 Jun 2023 20:12:14 +0300 Message-Id: <20230609171214.31846-3-alexander.shishkin@linux.intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230609171214.31846-1-alexander.shishkin@linux.intel.com> References: <20230609171214.31846-1-alexander.shishkin@linux.intel.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" Commit 20f07a044a76 ("x86/sev: Move common memory encryption code to mem_encrypt.c") forgot to remove the include of virtio_config.h from mem_encrypt_amd.c when it moved the related code to mem_encrypt.c (from where this include subsequently got removed by a later commit). Remove it now. No functional changes. Signed-off-by: Alexander Shishkin --- arch/x86/mm/mem_encrypt_amd.c | 1 - 1 file changed, 1 deletion(-) diff --git a/arch/x86/mm/mem_encrypt_amd.c b/arch/x86/mm/mem_encrypt_amd.c index b1edeb8642b8..30dc5f1978ab 100644 --- a/arch/x86/mm/mem_encrypt_amd.c +++ b/arch/x86/mm/mem_encrypt_amd.c @@ -19,7 +19,6 @@ #include #include #include -#include #include =20 #include --=20 2.39.2