Early sharing needs direct-mapped per-CPU storage. In TDX, a private
direct-map alias of a shared page can terminate the guest, including
through load_unaligned_zeropad(). Require embed rather than adding early
vmalloc conversion and alias synchronization.

Warn when overriding percpu_alloc=page and let embed failures reach the
existing panic. Restrict this policy to encrypted guests, leaving host
SME allocation unchanged.

Suggested-by: Kiryl Shutsemau <[email protected]>
Link: https://lore.kernel.org/r/aqvxQoIoYhJTZpAC@thinkstation
Signed-off-by: Zack Rusin <[email protected]>
---
 Documentation/admin-guide/kernel-parameters.txt |  3 +++
 arch/x86/kernel/setup_percpu.c                  | 12 ++++++++++--
 2 files changed, 13 insertions(+), 2 deletions(-)

diff --git a/Documentation/admin-guide/kernel-parameters.txt 
b/Documentation/admin-guide/kernel-parameters.txt
index 33cd30996e47..8ba881af3520 100644
--- a/Documentation/admin-guide/kernel-parameters.txt
+++ b/Documentation/admin-guide/kernel-parameters.txt
@@ -5347,6 +5347,9 @@ Kernel parameters
                        See comments in mm/percpu.c for details on each
                        allocator.  This parameter is primarily for debugging
                        and performance comparison.
+                       On x86 encrypted guests, only "embed" is supported;
+                       "page" is ignored with a warning. An embed allocation
+                       failure is fatal instead of falling back to "page".
 
        pirq=           [SMP,APIC] Manual mp-table setup
                        See Documentation/arch/x86/i386/IO-APIC.rst.
diff --git a/arch/x86/kernel/setup_percpu.c b/arch/x86/kernel/setup_percpu.c
index bfa48e7a32a2..c83c61e0b20a 100644
--- a/arch/x86/kernel/setup_percpu.c
+++ b/arch/x86/kernel/setup_percpu.c
@@ -8,6 +8,7 @@
 #include <linux/percpu.h>
 #include <linux/kexec.h>
 #include <linux/crash_dump.h>
+#include <linux/cc_platform.h>
 #include <linux/smp.h>
 #include <linux/topology.h>
 #include <linux/pfn.h>
@@ -112,6 +113,7 @@ void __init setup_per_cpu_areas(void)
 {
        unsigned int cpu;
        unsigned long delta;
+       bool encrypted = cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT);
        int rc;
 
        pr_info("NR_CPUS:%d nr_cpumask_bits:%d nr_cpu_ids:%u nr_node_ids:%u\n",
@@ -127,6 +129,12 @@ void __init setup_per_cpu_areas(void)
        if (pcpu_chosen_fc == PCPU_FC_AUTO && pcpu_need_numa())
                pcpu_chosen_fc = PCPU_FC_PAGE;
 #endif
+       if (encrypted) {
+               if (pcpu_chosen_fc == PCPU_FC_PAGE)
+                       pr_warn("Ignoring percpu_alloc=page in an encrypted 
guest\n");
+               pcpu_chosen_fc = PCPU_FC_EMBED;
+       }
+
        rc = -EINVAL;
        if (pcpu_chosen_fc != PCPU_FC_PAGE) {
                const size_t dyn_size = PERCPU_MODULE_RESERVE +
@@ -149,11 +157,11 @@ void __init setup_per_cpu_areas(void)
                                            dyn_size, atom_size,
                                            pcpu_cpu_distance,
                                            pcpu_cpu_to_node);
-               if (rc < 0)
+               if (rc < 0 && !encrypted)
                        pr_warn("%s allocator failed (%d), falling back to page 
size\n",
                                pcpu_fc_names[pcpu_chosen_fc], rc);
        }
-       if (rc < 0)
+       if (rc < 0 && !encrypted)
                rc = pcpu_page_first_chunk(PERCPU_FIRST_CHUNK_RESERVE,
                                           pcpu_cpu_to_node);
        if (rc < 0)
-- 
2.53.0


Reply via email to