From patchwork Mon Aug 7 13:53:37 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Gleixner X-Patchwork-Id: 132102 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c44e:0:b0:3f2:4152:657d with SMTP id w14csp1496349vqr; Mon, 7 Aug 2023 07:36:59 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGbUQvJJrAlWABU7n9v6LXbhp0RD3mifAPXrfKRyzJlSuw1IgBfb56JCsheHwUNMxTKBf4y X-Received: by 2002:a05:6a21:3293:b0:132:ce08:1e28 with SMTP id yt19-20020a056a21329300b00132ce081e28mr9874443pzb.22.1691419019218; Mon, 07 Aug 2023 07:36:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691419019; cv=none; d=google.com; s=arc-20160816; b=atA9MLOhTaeI6QmCjYeGNtkLUXv8RzR3AzNrjtQHm/Tgk0fubDC3kplASb733oxzwR QdQbVH5rsBAhvMOQYsbCUDn/TArNj2PjcvelR7d2rIK+12oVaXCu/znqxZ8L4rEw+PXx vQqQ+rRWI6tlNft6KKUjaljjvtc1Eqpcm5lQRNHN165ngtevn5uVeNI6GZdmSGxBpn1C BWFnS5MwXegUIJC6rMUzWObfkXziuMCOhYrOPLXE44pLXLf7Dy1TEiLA4th+nKwDK9bA /57DA0aB0bmsuTrf43pq+tx8D1e3PCbO413z0I2TsakEI2G7OxZHKO7AGneGb1GOWIR/ G9BA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:date:mime-version:references:subject:cc:to:from :dkim-signature:dkim-signature:message-id; bh=o9SLDTJj0ahrwCVinyEBp+cl3PdBP7t2Ahe9Zy+O4Hg=; fh=/92WVKYrcN3rA268QXyKzVtm/UtxHeI2lnbU295XDiQ=; b=ZedwQ3pQP11q/b7mVxl06kJNoA3q6z4+NI39YpMYKkRWK1e+1HHHKD2VAyeGBSDnx8 9L3W07FjgOEoNGYsd6pjpGp7TzZCvDbMp7xMIMstgIqsW3xv2NLKAmYbXMs65bBn8Fyr qXQL4Kud5I2aydM3GGlpMX9Mcm6kALps8ZkbaxaA3FMXd7B4SHLo0WrO2edPegJ7BnYv +YhzDdZ0HXHZ/cv/5Hb/OyZxxaBSjjf+SJ1vBP4Y1D4UyEHhVkIVtewLiF+oSnm89VCW EyY5c+4oKCM6aXLRaEpKNlZeJ6Y3Dj0+kLzvc8lh6Ue6UflepvX66c2g9BkD9DStzDeK mOuA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=qxJvIkao; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id i6-20020a655b86000000b00564aee56716si5598038pgr.403.2023.08.07.07.36.45; Mon, 07 Aug 2023 07:36:59 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=qxJvIkao; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231945AbjHGNzm (ORCPT + 99 others); Mon, 7 Aug 2023 09:55:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41908 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234700AbjHGNyj (ORCPT ); Mon, 7 Aug 2023 09:54:39 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4EEF32136 for ; Mon, 7 Aug 2023 06:53:40 -0700 (PDT) Message-ID: <20230807135028.590964892@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1691416418; 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=o9SLDTJj0ahrwCVinyEBp+cl3PdBP7t2Ahe9Zy+O4Hg=; b=qxJvIkaob+sckp0E/Da7Q7nfjiLdwKRN313sR467mp0de3ZCApwTHm16s7a3z5aRCxal1g 3kP3eHzRW1I/0VO2eRYYlsGpMTctiBssw6PNIPNzJaRPef2xoixxzRRF4pWff84MQbiV37 QKEYHo0CWE1HKAonM5dEbii6DS5gYUmLg+EHT0wAkgOa5hh47EL1Mn47LOVL6s/xKQK/ow d0a2HBYxeFa/WuzGMQwU3imkV8gM6LSTj/yzRIEAlreID2OpLzUeJ2l+RQGGIIz0qf6gcW wjmeShMeLOwedLmc2TPFdRDNzVTdWqPHqhRcOH7L3t3Sz4nSZO+AKatinqnF5A== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1691416418; 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=o9SLDTJj0ahrwCVinyEBp+cl3PdBP7t2Ahe9Zy+O4Hg=; b=1oHVvXW3zugTioz7NcC7YLqdyBQOGBYt7nb34N5AFkST+ZYjs85CN3l8F3biTdkhWKuf+r /a432DbYL5kUfxBQ== From: Thomas Gleixner To: LKML Cc: x86@kernel.org, Tom Lendacky , Andrew Cooper , Arjan van de Ven , Huang Rui , Juergen Gross , Dimitri Sivanich , Michael Kelley , Sohil Mehta , K Prateek Nayak , Kan Liang , Zhang Rui , "Paul E. McKenney" , Feng Tang , Andy Shevchenko Subject: [patch 41/53] x86/xen/smp_pv: Count number of vCPUs early References: <20230807130108.853357011@linutronix.de> MIME-Version: 1.0 Date: Mon, 7 Aug 2023 15:53:37 +0200 (CEST) X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1773581389190864049 X-GMAIL-MSGID: 1773581389190864049 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 @@ -189,6 +189,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,