From patchwork Tue Feb 13 21:06:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Gleixner X-Patchwork-Id: 200648 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:bc8a:b0:106:860b:bbdd with SMTP id dn10csp814646dyb; Tue, 13 Feb 2024 13:20:13 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWWsTFzsnChxU0TWj1oTml1lmgLcocuKO0/X1Yebu/5qAN2kgBqWaRQP1mGTSal8hBDRFrQTXJtEK0rg7vnZ7QSPY1Y8w== X-Google-Smtp-Source: AGHT+IFyL+5iunzOXO13q62wK+gyKsZpUJaGdAo7ql3FFHxD2MYhwo4pRLZHXpTkeqmE0u1uiASh X-Received: by 2002:a17:902:e752:b0:1d9:b08:92b6 with SMTP id p18-20020a170902e75200b001d90b0892b6mr931640plf.31.1707859213774; Tue, 13 Feb 2024 13:20:13 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707859213; cv=pass; d=google.com; s=arc-20160816; b=wHdlGd9S76DzKWmXY9Q0RinWbIGs2orOdbs1DkzKOCqKzOkH6VTDvuSrkfqnDmLJtM gviAF7tpazaWOpkOtKmy95AAsknvQEROMrCyPcDvLiPcrE5L8opy/udD9V0VVENwE+ON ziSuu2lwRDm/0pNI5oDSoRNdvbN88svLxN5Bsth34Q4/PK7+cbgQA6m4ihNEvXdQbhA8 HNcGFo08FltopqcrJ/BljQRSoXsQf2JUhMe64sxBP+LGbwvokKSHRITjTkjtCfuZrae7 V+lOQfnXyrS6CqOOqwU4MowE6btDEzp+WZcALFWqzG1KuZ+VjB/ewf0WahzoeA60UOPd MQFA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=date:mime-version:list-unsubscribe:list-subscribe:list-id :precedence:references:subject:cc:to:from:dkim-signature :dkim-signature:message-id; bh=l5IIheic7HTdLbtL8FePLNCYvWctux8lTDROKkU5h0g=; fh=VSsu3R8VVD1wceXgqMIifUXHgsLIMMoaUU+4OXhGsGo=; b=MyleB3ghmfFDQSVSEOabHPEhKQflPyW55+L0iM67eCY+FOjITyKYxCfuIL4gPnRxoM BmScf7LRmSLTqrDWq0QEQl92zXPHo5m1rhVmMOyG44DLkH0DNaVgkjWlfDit5R1MqowH P3/JhYaUsGldN6lr2uxyVjxOMqbbzje+GMredKZbrzJBHNVdiw5hTHSm3iUEbC2SiDr5 1N3wG3gQM5OGgZzD5q7xdt6Orkqr9ZQDPgs4mvnaCTwN1SmwZfGn7VsLj4Jqf0zJTqC5 qjqK+W2dXbUWB3sSW5s5c2Yg3QpaUZSgJlBnr+CwqCFdzWAEue9mWkkjxmtBLI7GOJXh QZsw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=Wt+Q6owG; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; arc=pass (i=1 spf=pass spfdomain=linutronix.de dkim=pass dkdomain=linutronix.de dmarc=pass fromdomain=linutronix.de); spf=pass (google.com: domain of linux-kernel+bounces-64304-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-64304-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de X-Forwarded-Encrypted: i=2; AJvYcCXKsxEddgArxEjUS7OWnw2UE2Xw12ZKQAy8mJnZKpK9xaVit44k3emMoIM1bWxTRn96pK6S3G4yzF00SZ1iTTajjSQY8A== Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id m15-20020a637d4f000000b005dc1b73dd16si2530783pgn.372.2024.02.13.13.20.13 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 13 Feb 2024 13:20:13 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-64304-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=Wt+Q6owG; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; arc=pass (i=1 spf=pass spfdomain=linutronix.de dkim=pass dkdomain=linutronix.de dmarc=pass fromdomain=linutronix.de); spf=pass (google.com: domain of linux-kernel+bounces-64304-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-64304-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id EA0AD28AAE6 for ; Tue, 13 Feb 2024 21:19:52 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 9CA8A84FD5; Tue, 13 Feb 2024 21:06:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="Wt+Q6owG"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="a1Vsgm7b" Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 86DAE839E7 for ; Tue, 13 Feb 2024 21:06:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707858364; cv=none; b=CwtjeUYKI4qNC48koSkRadENcqefIhsZdiVyCRWrc1MksqvYHZHNJ/OwnlhOSkHWkqtLEDLNQUui8tlWt69vqeaj856e2bkMSy21bd160PVk35m/Fdx1+pS89aNJyommLtMU16iqTsQuDbos5/cXZzK6gbRMSjSTjs9GRwc/2fs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707858364; c=relaxed/simple; bh=rLsp+V/ZBSzycpeyYr3sAEzojATjfDc9ZcQACwfYXk0=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=VQDYbK4WdVn4nIESp8wM5SbBPRW7laapD366mrYh5gPs3C7omByM3j/7wR/pj/J4Fdxt55Ba+byGfy4VdfcmPAFxcdrTSMZqijtxtE/D0wcq0hmmBQqQhd3wakjIdKUbZ5HPpnVM1Cp1O++xyfGV/r5as1ajqdz0CWpTFN2dNHU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=Wt+Q6owG; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=a1Vsgm7b; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Message-ID: <20240213210252.571795063@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1707858361; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=l5IIheic7HTdLbtL8FePLNCYvWctux8lTDROKkU5h0g=; b=Wt+Q6owGWnrNJeI13LNavmxTt5Lzs5ThH/dQXT6qesO4DKSLqgiS3ondhGjNgKOQbCvDgW 9R2GOO5EAsdXU6sSJ+htWRBEmUrGCB/661l3aSPAsEEpCKKyJESlpafzVFP+cGSmVB8bkc 42kYfpgOCQyZ65aXNn24eWvGdmVh708pAxPG7xivimdq00BIo+iv0VJwEIN8EPlJs3KfTT pII6gtnyJR95M5aNNod+zIBJybHtvUj0Z4gdtEQPnXXLSi9zCKzmUa7KMd2CmdMBycl8c/ T1vcJwagFyvVJrB9cilnL4xYqhZO2Ghot1WZ8m60nRit2K8h4+U9Y0Z0rs1LtQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1707858361; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=l5IIheic7HTdLbtL8FePLNCYvWctux8lTDROKkU5h0g=; b=a1Vsgm7b2KoILqZlz/V01LMAGTu+EopHn2bXvBSK5ls0/E3HO1hQG+Dc294cj3B67tmfnk WaGUvLvojc1mH2DQ== From: Thomas Gleixner To: LKML Cc: x86@kernel.org, Tom Lendacky , Andrew Cooper , Arjan van de Ven , Huang Rui , Juergen Gross , Dimitri Sivanich , Sohil Mehta , K Prateek Nayak , Kan Liang , Zhang Rui , "Paul E. McKenney" , Feng Tang , Andy Shevchenko , Michael Kelley , "Peter Zijlstra (Intel)" Subject: [patch 19/30] x86/xen/smp_pv: Count number of vCPUs early References: <20240213205415.307029033@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Tue, 13 Feb 2024 22:06:00 +0100 (CET) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790820182819879420 X-GMAIL-MSGID: 1790820182819879420 From: Thomas Gleixner XEN/PV has a completely broken vCPU enumeration scheme, which just works by chance and provides zero topology information. Each vCPU ends up being a single core package. Dom0 provides MADT which can be used for topology information, but that table is the unmodified host table, which means that there can be more CPUs registered than the number of vCPUs XEN provides for the dom0 guest. DomU does not have ACPI and both rely on counting the possible vCPUs via an hypercall. To prepare for using CPUID topology information either via MADT or via fake APIC IDs count the number of possible CPUs during early boot and adjust nr_cpu_ids() accordingly. Signed-off-by: Thomas Gleixner --- arch/x86/xen/enlighten_pv.c | 3 +++ arch/x86/xen/smp.h | 2 ++ arch/x86/xen/smp_pv.c | 14 ++++++++++++++ 3 files changed, 19 insertions(+) --- --- a/arch/x86/xen/enlighten_pv.c +++ b/arch/x86/xen/enlighten_pv.c @@ -200,6 +200,9 @@ static void __init xen_pv_init_platform( xen_set_mtrr_data(); else mtrr_overwrite_state(NULL, 0, MTRR_TYPE_WRBACK); + + /* Adjust nr_cpu_ids before "enumeration" happens */ + xen_smp_count_cpus(); } static void __init xen_pv_guest_late_init(void) --- a/arch/x86/xen/smp.h +++ b/arch/x86/xen/smp.h @@ -19,6 +19,7 @@ extern void xen_smp_intr_free(unsigned i int xen_smp_intr_init_pv(unsigned int cpu); void xen_smp_intr_free_pv(unsigned int cpu); +void xen_smp_count_cpus(void); void xen_smp_cpus_done(unsigned int max_cpus); void xen_smp_send_reschedule(int cpu); @@ -44,6 +45,7 @@ static inline int xen_smp_intr_init_pv(u return 0; } static inline void xen_smp_intr_free_pv(unsigned int cpu) {} +static inline void xen_smp_count_cpus(void) { } #endif /* CONFIG_SMP */ #endif --- a/arch/x86/xen/smp_pv.c +++ b/arch/x86/xen/smp_pv.c @@ -411,6 +411,20 @@ static irqreturn_t xen_irq_work_interrup return IRQ_HANDLED; } +void __init xen_smp_count_cpus(void) +{ + unsigned int cpus; + + for (cpus = 0; cpus < nr_cpu_ids; cpus++) { + if (HYPERVISOR_vcpu_op(VCPUOP_is_up, cpus, NULL) < 0) + break; + } + + pr_info("Xen PV: Detected %u vCPUS\n", cpus); + if (cpus < nr_cpu_ids) + set_nr_cpu_ids(cpus); +} + static const struct smp_ops xen_smp_ops __initconst = { .smp_prepare_boot_cpu = xen_pv_smp_prepare_boot_cpu, .smp_prepare_cpus = xen_pv_smp_prepare_cpus,