Received: by 2002:a05:6359:6284:b0:131:369:b2a3 with SMTP id se4csp3098361rwb; Mon, 7 Aug 2023 08:13:42 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFwLjwSECGYzYn9IuHr0OvyT14Ey+CJnmUz+71AaoG//VWWH1kxnZVqE1SNsxJ9e/SOYT3/ X-Received: by 2002:a17:902:e846:b0:1bc:2d43:c74d with SMTP id t6-20020a170902e84600b001bc2d43c74dmr9839543plg.34.1691421222483; Mon, 07 Aug 2023 08:13:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691421222; cv=none; d=google.com; s=arc-20160816; b=fZYJFZEZ2km1fz43jFMczLBAUWiJ16s/TwkffXSeUKAPbWjBWo4ZSj1OMear8uOnAq HUQlAR3s0wjZ0Kopwm7RUkJQqv+PiS0YfFHO0Tnh8u2b8DE8vl6RFOXvL+KDkknockcS eLD0JQa8IA2S4G3R7DxFHc/DOhnOslEUu3Ecje3suli20D2S85JxZqfJxWbjqAnArfyf 4VjPQeoq7H7jFhudws/I8Nv3dkRsjxSJaGaSLi/Uk9hsL9tx3U6IlXLXHavUJsOzXQrQ IPgSdVnEkCHLIYlqH4JyQnfkY4mHao29R/fQn4Jp1vHz3Hau2dKBcYd+Bb4uZBkTVLDr Q7Jg== 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 header.s=2020e header.b=1oHVvXW3; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id kr5-20020a170903080500b001b9c7300823si5776776plb.221.2023.08.07.08.13.30; Mon, 07 Aug 2023 08:13:42 -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 header.s=2020e header.b=1oHVvXW3; 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 S234603AbjHGNzm (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 Content-Type: text/plain; charset=UTF-8 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 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,