Received: by 2002:a05:7412:37c9:b0:e2:908c:2ebd with SMTP id jz9csp620590rdb; Tue, 19 Sep 2023 05:50:19 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHTEX0yqPfADlvFKdZXrQmxuCwSsYpaAXwdxuNJFpa71pSvHiIbR1tfkiZiSn1Gx0eVNBxI X-Received: by 2002:a17:902:e88e:b0:1c3:19d6:3559 with SMTP id w14-20020a170902e88e00b001c319d63559mr3159486plg.1.1695127818808; Tue, 19 Sep 2023 05:50:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695127818; cv=none; d=google.com; s=arc-20160816; b=s9rCvZZsgCatJkE+EjcEfOJAu/EPEtry5cbHg0/6TQWBPGoOl08jAryKG+dLcJiosV WiJ2hMtBW23JGapqG907IU0sXTygm3hlbg/CjSnZRyz1uoA7140jv9Hn6qUSj+mN+a5/ QgfFPqSlVoPcis3ezlsSReoQX96YlVSCYzA8nwEU3g/O4R2ga2EqDo1Qe9v9HsEh4QOO V3lrCbazgFdc+YSarpNBRlshVfHciAzCiuPEVixb1rUM/5R1tiNUJ28kw/E+VyCnuTZs Q7R80uBLZ0OWn/+l0puBaYzL89ETs8eVwoQ3TLBzDPrY9s5Zu1sg/CgZ2JMBi/5UrWmX 0pHw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from; bh=ktF0S8EIFvtDT7hW8yr7ZUF6Dgiol4x2QqibMRDKqlY=; fh=MX+aITdMWn2IuzvwIOZJEsYFr8RPjLjVWt28DSeolHI=; b=AKLsxk8GoDgqsPYHA34fAYxaTP+HqQFvWSZQYoHVe4NHdeJn8djiFVQ7rM064hcHTt 9i+BkenyzAEnLWMU+OGireUCOeDNlJEoDj34YzjvYRoQUTlMhpwlKeCjhDAujpfmVvXS be4bJqC+p09jlnAXz/SqDFqn1WZKLfK8IEVx2ju4uSsu0issViAIOCGDNl8waTy7y0FU HWuRMfcUBlRoeGBa67FgnnjOZ7XVfpT969HhsU8NE+ewCy7GlUMcZNcxLbyPIp+I5cto PGi4dZ/NbHoEK+e+XOqpimg+lQ4ctVWP5zUyE22AlkpPVVTq1t95ip5pDW8Z+oTMvT6W WSJA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Return-Path: Received: from lipwig.vger.email (lipwig.vger.email. [23.128.96.33]) by mx.google.com with ESMTPS id d1-20020a170902cec100b001b874c5f7a8si9625127plg.653.2023.09.19.05.50.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Sep 2023 05:50:18 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) client-ip=23.128.96.33; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id EA774807384E; Tue, 19 Sep 2023 05:36:24 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231368AbjISMgV (ORCPT + 99 others); Tue, 19 Sep 2023 08:36:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53624 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229891AbjISMgT (ORCPT ); Tue, 19 Sep 2023 08:36:19 -0400 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CB07FB8 for ; Tue, 19 Sep 2023 05:36:12 -0700 (PDT) Received: from canpemm500009.china.huawei.com (unknown [172.30.72.57]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4Rqgz34dJWzJsYW; Tue, 19 Sep 2023 20:32:15 +0800 (CST) Received: from localhost.localdomain (10.50.163.32) by canpemm500009.china.huawei.com (7.192.105.203) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.31; Tue, 19 Sep 2023 20:36:01 +0800 From: Yicong Yang To: , , , CC: , , , , , , Subject: [PATCH] arch_topology: Support SMT control on arm64 Date: Tue, 19 Sep 2023 20:33:19 +0800 Message-ID: <20230919123319.23785-1-yangyicong@huawei.com> X-Mailer: git-send-email 2.31.0 MIME-Version: 1.0 Content-Transfer-Encoding: 7BIT Content-Type: text/plain; charset=US-ASCII X-Originating-IP: [10.50.163.32] X-ClientProxiedBy: dggems703-chm.china.huawei.com (10.3.19.180) To canpemm500009.china.huawei.com (7.192.105.203) X-CFilter-Loop: Reflected X-Spam-Status: No, score=-0.8 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Tue, 19 Sep 2023 05:36:25 -0700 (PDT) From: Yicong Yang The core CPU control framework supports runtime SMT control which is not yet supported on arm64. Besides the general vulnerabilities concerns we want this runtime control on our arm64 server for: - better single CPU performance in some cases - saving overall power consumption This patch implements it in the following aspects: - implement the callbacks of the core - update the SMT status after the topology enumerated on arm64 - select HOTPLUG_SMT for arm64 For disabling SMT we'll offline all the secondary threads and only leave the primary thread. Since we don't have restriction for primary thread selection, the first thread is chosen as the primary thread in this implementation. Tests has been done on our ACPI based arm64 server and on ACPI/OF based QEMU VMs. Signed-off-by: Yicong Yang --- arch/arm64/Kconfig | 1 + drivers/base/arch_topology.c | 63 +++++++++++++++++++++++++++++++++++ include/linux/arch_topology.h | 11 ++++++ 3 files changed, 75 insertions(+) diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig index b10515c0200b..531a71c7f499 100644 --- a/arch/arm64/Kconfig +++ b/arch/arm64/Kconfig @@ -233,6 +233,7 @@ config ARM64 select HAVE_KRETPROBES select HAVE_GENERIC_VDSO select HOTPLUG_CORE_SYNC_DEAD if HOTPLUG_CPU + select HOTPLUG_SMT if SMP select IRQ_DOMAIN select IRQ_FORCED_THREADING select KASAN_VMALLOC if KASAN diff --git a/drivers/base/arch_topology.c b/drivers/base/arch_topology.c index b741b5ba82bd..75a693834fff 100644 --- a/drivers/base/arch_topology.c +++ b/drivers/base/arch_topology.c @@ -729,6 +729,63 @@ const struct cpumask *cpu_clustergroup_mask(int cpu) return &cpu_topology[cpu].cluster_sibling; } +#ifdef CONFIG_HOTPLUG_SMT +static int topology_smt_num_threads = 1; + +void __init topology_smt_set_num_threads(void) +{ + int cpu, sibling, threads; + + /* + * Walk all the CPUs to find the largest thread number, in case we're + * on a heterogeneous platform with only part of the CPU cores support + * SMT. + * + * Get the thread number by checking the CPUs with same core id + * rather than checking the topology_sibling_cpumask(), since the + * sibling mask will not cover all the CPUs if there's CPU offline. + */ + for_each_possible_cpu(cpu) { + threads = 1; + + /* Invalid thread id, this CPU is not in a SMT core */ + if (cpu_topology[cpu].thread_id == -1) + continue; + + for_each_possible_cpu(sibling) { + if (sibling == cpu || cpu_topology[sibling].thread_id == -1) + continue; + + if (cpu_topology[cpu].core_id == cpu_topology[sibling].core_id) + threads++; + } + + if (threads > topology_smt_num_threads) + topology_smt_num_threads = threads; + } + + /* + * We don't support CONFIG_SMT_NUM_THREADS_DYNAMIC so make the + * max_threads == num_threads. + */ + cpu_smt_set_num_threads(topology_smt_num_threads, topology_smt_num_threads); +} + +/* + * On SMT Hotplug the primary thread of the SMT won't be disabled. For x86 they + * seem to have a primary thread for special purpose. For other arthitectures + * like arm64 there's no such restriction for a primary thread, so make the + * first thread in the SMT as the primary thread. + */ +bool topology_is_primary_thread(unsigned int cpu) +{ + if (cpu == cpumask_first(topology_sibling_cpumask(cpu))) + return true; + + return false; +} +#endif + void update_siblings_masks(unsigned int cpuid) { struct cpu_topology *cpu_topo, *cpuid_topo = &cpu_topology[cpuid]; @@ -841,6 +898,12 @@ void __init init_cpu_topology(void) reset_cpu_topology(); } + /* + * By this stage we get to know whether we support SMT or not, update + * the information for the core. + */ + topology_smt_set_num_threads(); + for_each_possible_cpu(cpu) { ret = fetch_cache_info(cpu); if (!ret) diff --git a/include/linux/arch_topology.h b/include/linux/arch_topology.h index a07b510e7dc5..cf605a576e7b 100644 --- a/include/linux/arch_topology.h +++ b/include/linux/arch_topology.h @@ -92,6 +92,17 @@ void update_siblings_masks(unsigned int cpu); void remove_cpu_topology(unsigned int cpuid); void reset_cpu_topology(void); int parse_acpi_topology(void); + +#ifdef CONFIG_HOTPLUG_SMT +bool topology_smt_supported(void); +bool topology_is_primary_thread(unsigned int cpu); +void topology_smt_set_num_threads(void); +#else +static inline bool topology_smt_supported(void) { return false; } +static inline bool topology_is_primary_thread(unsigned int cpu) { return false; } +static inline void topology_smt_set_num_threads(void) { } +#endif + #endif #endif /* _LINUX_ARCH_TOPOLOGY_H_ */ -- 2.24.0