From patchwork Wed Feb 28 19:37:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Luck, Tony" X-Patchwork-Id: 208004 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:a81b:b0:108:e6aa:91d0 with SMTP id bq27csp3568988dyb; Wed, 28 Feb 2024 11:39:54 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWQaD36uIxSKMT8CKhEsLlw+VLoZHq7lvoqkMaWO237myjYhtvOJ/ocECLC3SfzwmzArSS6wseUnoolaSTTdVWiRMZHGQ== X-Google-Smtp-Source: AGHT+IGvSLm8VuUExGH7Xb3l8VzHKJ46TlcnOLvXN/ouV9Yu5MlqeOiCk5SEW1+sStYtGv3SVfQm X-Received: by 2002:a05:620a:845:b0:787:f8da:c65c with SMTP id u5-20020a05620a084500b00787f8dac65cmr33982qku.39.1709149193805; Wed, 28 Feb 2024 11:39:53 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709149193; cv=pass; d=google.com; s=arc-20160816; b=zsNEe7N9BJfUU6NWPzdzn8kMxrSZ1PRW2+T+mTMfj4JovRqp7Hg/lEdPxS1cmgHtkN 84+4H124MEtFTfcZQtqpOLsH87DtP0ahKzY//wu58OFKuCrvHT7WWHedFt6NkEqEZEhy srjFrsZPDbBq2XmC51f0hzrmYwE7FXtTW7wt8mgmRYEWL8c0ameFuMZqrWQF8XBfuF3Z wR1ufIdlzjYHck0Rr0rpXzgqLqZHJ1uskBeaKGCJw6jwBh9nrWw3X3R54CsbaEEKlWb4 aepn7w+NTzDyiL/rEeJJk9tuM9374L30CpJ0ACtvWzRon2LswtEd7IdXR+KgGKTLLHlg L34w== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=SxWJFZx6Rj5Xr3aojWWj+omft8QItIfOXssLW0VhROE=; fh=kDu4WSgSKQjfqS0+uR7gDD47kd+EMQAW47hbuuOMwvI=; b=qKI+4pr7FP/D8qgkKR0VNU6qfjzuE9duX7jPZodAlkXfaVtwFqtqPWHs8ZeWXeJ7mc EyeGg+grvf4YdDOxk7YXWsQxKA3iN4P0/WVz0W54arIpBVAi6l0BDQVqMEtuuF3UXIhx T6ERGIFY6vn1br2d/TRMi8S81lfzAjLyA7FnoDxonPVyFIK7fGMjjlt+5V1ALS/O+6ks IurwVKnGoVCxOvejQXpy1RVscz9iRAPzPKlT77tGwD01UVhvQnCJKfxXWOS9nTKOM2UZ XgFkOrhG16gcnxs3sRowBZ9nK0eUhNaH2myUoFDq0B9j5XYrrJ11uesRTG0OU9RpLOyr wLfw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=jsW574lA; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-85623-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-85623-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id t1-20020a05620a0b0100b00787f1f412dcsi192837qkg.529.2024.02.28.11.39.53 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Feb 2024 11:39:53 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-85623-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=jsW574lA; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-85623-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-85623-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com 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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 84A2E1C244F4 for ; Wed, 28 Feb 2024 19:39:53 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 59591135F89; Wed, 28 Feb 2024 19:37:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="jsW574lA" Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.21]) (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 259606FCF4; Wed, 28 Feb 2024 19:37:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.21 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709149049; cv=none; b=DPwqkgumjSCa8Zcfa7t2/eFzCwyJm0Re1XR8w/1qMSVHaSWTVl6f5VJ0ImAM32ZxnRV8R05GwqGOf883dN3rnrvuQt+uESpV8TR7jomRdGVcezAdcOjRV1jePVSvytjRtMH8x6ea+drn8shqnMgpVwsABEK2H6bCCdQirH6RdgE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709149049; c=relaxed/simple; bh=uw03m9ZQZxlYnVUC+rJ4IH+RF9QU90tWoq8OpJpcIp0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ixtJtkedKMHXtoU+Wlfvx5RC417e3jCJhMRW/FgF8f5ig9vvhTvVngH9IDlud6Ks1haDaExNjQEGxJsNkTgm1dGzGOedvhzDBHVSdVtjyY2Rc4adW63D9UF8Cr0qU2bNTi8CHE8PjuX3B6J+vr2ycTVe1/uCvnt/RRO1FztCHUI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=jsW574lA; arc=none smtp.client-ip=198.175.65.21 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1709149048; x=1740685048; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=uw03m9ZQZxlYnVUC+rJ4IH+RF9QU90tWoq8OpJpcIp0=; b=jsW574lAZ6Rmbk6wf45Rk5H++OD4gGsWmGoREp0l7YayvDzirw1rHjmJ gwYNm3aF/iuIZUQ8NH09EsyxU1U1Mg5SbssVZSGZutR/4P5gME0LjU2gg a1Jl6CVNEmgdN2eJ193sZ8W3QSbevHo+VNF0MLZUpsneUQf0ot15v2EE5 3UldcaZX/nh/fkWYnE90ePZemAWRHbRzZSvB8/pv26hdFauGXTGl/2wi+ 6Psek/SspJcYB6K0TZwwuuzPPKGN1cFISImjQnFWCkdUl1L5tANGGNEaz JxBuCmlz3GCp+LEivtLFFk/P0KPD+2EcRit4iWy2jRHtZ5EdqeWg8RdI9 Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10998"; a="3495573" X-IronPort-AV: E=Sophos;i="6.06,191,1705392000"; d="scan'208";a="3495573" Received: from orviesa010.jf.intel.com ([10.64.159.150]) by orvoesa113.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Feb 2024 11:37:21 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.06,191,1705392000"; d="scan'208";a="7485404" Received: from agluck-desk3.sc.intel.com ([172.25.222.105]) by orviesa010-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Feb 2024 11:37:20 -0800 From: Tony Luck To: Fenghua Yu , Reinette Chatre , Peter Newman , Jonathan Corbet , Shuah Khan , x86@kernel.org Cc: Shaopeng Tan , James Morse , Jamie Iles , Babu Moger , Randy Dunlap , Drew Fustini , linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, patches@lists.linux.dev, Tony Luck Subject: [PATCH v15 7/8] x86/resctrl: Sub NUMA Cluster detection and enable Date: Wed, 28 Feb 2024 11:37:02 -0800 Message-ID: <20240228193717.8170-9-tony.luck@intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240228112935.8087-tony.luck@intel.com> References: <20240228112215.8044-tony.luck@intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1792172825455527395 X-GMAIL-MSGID: 1792172825455527395 There isn't a simple hardware bit that indicates whether a CPU is running in Sub NUMA Cluster (SNC) mode. Infer the state by comparing the ratio of NUMA nodes to L3 cache instances. When SNC mode is detected, reconfigure the RMID counters by updating the MSR_RMID_SNC_CONFIG MSR on each socket as CPUs are seen. Clearing bit zero of the MSR divides the RMIDs and renumbers the ones on the second SNC node to start from zero. Signed-off-by: Tony Luck --- arch/x86/include/asm/msr-index.h | 1 + arch/x86/kernel/cpu/resctrl/core.c | 119 +++++++++++++++++++++++++++++ 2 files changed, 120 insertions(+) diff --git a/arch/x86/include/asm/msr-index.h b/arch/x86/include/asm/msr-index.h index f1bd7b91b3c6..f6ba7d0397b8 100644 --- a/arch/x86/include/asm/msr-index.h +++ b/arch/x86/include/asm/msr-index.h @@ -1119,6 +1119,7 @@ #define MSR_IA32_QM_CTR 0xc8e #define MSR_IA32_PQR_ASSOC 0xc8f #define MSR_IA32_L3_CBM_BASE 0xc90 +#define MSR_RMID_SNC_CONFIG 0xca0 #define MSR_IA32_L2_CBM_BASE 0xd10 #define MSR_IA32_MBA_THRTL_BASE 0xd50 diff --git a/arch/x86/kernel/cpu/resctrl/core.c b/arch/x86/kernel/cpu/resctrl/core.c index cb181796f73b..65cec8c45047 100644 --- a/arch/x86/kernel/cpu/resctrl/core.c +++ b/arch/x86/kernel/cpu/resctrl/core.c @@ -21,7 +21,9 @@ #include #include #include +#include +#include #include #include #include "internal.h" @@ -744,11 +746,42 @@ static void clear_closid_rmid(int cpu) RESCTRL_RESERVED_CLOSID); } +/* + * The power-on reset value of MSR_RMID_SNC_CONFIG is 0x1 + * which indicates that RMIDs are configured in legacy mode. + * This mode is incompatible with Linux resctrl semantics + * as RMIDs are partitioned between SNC nodes, which requires + * a user to know which RMID is allocated to a task. + * Clearing bit 0 reconfigures the RMID counters for use + * in Sub NUMA Cluster mode. This mode is better for Linux. + * The RMID space is divided between all SNC nodes with the + * RMIDs renumbered to start from zero in each node when + * couning operations from tasks. Code to read the counters + * must adjust RMID counter numbers based on SNC node. See + * __rmid_read() for code that does this. + */ +static void snc_remap_rmids(int cpu) +{ + u64 val; + + /* Only need to enable once per package. */ + if (cpumask_first(topology_core_cpumask(cpu)) != cpu) + return; + + rdmsrl(MSR_RMID_SNC_CONFIG, val); + val &= ~BIT_ULL(0); + wrmsrl(MSR_RMID_SNC_CONFIG, val); +} + static int resctrl_arch_online_cpu(unsigned int cpu) { struct rdt_resource *r; mutex_lock(&domain_list_lock); + + if (snc_nodes_per_l3_cache > 1) + snc_remap_rmids(cpu); + for_each_capable_rdt_resource(r) domain_add_cpu(cpu, r); mutex_unlock(&domain_list_lock); @@ -988,11 +1021,97 @@ static __init bool get_rdt_resources(void) return (rdt_mon_capable || rdt_alloc_capable); } +/* CPU models that support MSR_RMID_SNC_CONFIG */ +static const struct x86_cpu_id snc_cpu_ids[] __initconst = { + X86_MATCH_INTEL_FAM6_MODEL(ICELAKE_X, 0), + X86_MATCH_INTEL_FAM6_MODEL(SAPPHIRERAPIDS_X, 0), + X86_MATCH_INTEL_FAM6_MODEL(EMERALDRAPIDS_X, 0), + X86_MATCH_INTEL_FAM6_MODEL(GRANITERAPIDS_X, 0), + X86_MATCH_INTEL_FAM6_MODEL(ATOM_CRESTMONT_X, 0), + {} +}; + +/* + * There isn't a simple hardware bit that indicates whether a CPU is running + * in Sub NUMA Cluster (SNC) mode. Infer the state by comparing the + * ratio of NUMA nodes to L3 cache instances. + * It is not possible to accurately determine SNC state if the system is + * booted with a maxcpus=N parameter. That distorts the ratio of SNC nodes + * to L3 caches. It will be OK if system is booted with hyperthreading + * disabled (since this doesn't affect the ratio). + */ +static __init int snc_get_config(void) +{ + unsigned long *node_caches; + int mem_only_nodes = 0; + int cpu, node, ret; + int num_l3_caches; + int cache_id; + + if (!x86_match_cpu(snc_cpu_ids)) + return 1; + + node_caches = bitmap_zalloc(num_possible_cpus(), GFP_KERNEL); + if (!node_caches) + return 1; + + cpus_read_lock(); + + if (num_online_cpus() != num_present_cpus()) + pr_warn("Some CPUs offline, SNC detection may be incorrect\n"); + + for_each_node(node) { + cpu = cpumask_first(cpumask_of_node(node)); + if (cpu < nr_cpu_ids) { + cache_id = get_cpu_cacheinfo_id(cpu, 3); + if (cache_id != -1) + set_bit(cache_id, node_caches); + } else { + mem_only_nodes++; + } + } + cpus_read_unlock(); + + num_l3_caches = bitmap_weight(node_caches, num_possible_cpus()); + kfree(node_caches); + + if (!num_l3_caches) + goto insane; + + /* sanity check #1: Number of CPU nodes must be multiple of num_l3_caches */ + if ((nr_node_ids - mem_only_nodes) % num_l3_caches) + goto insane; + + ret = (nr_node_ids - mem_only_nodes) / num_l3_caches; + + /* sanity check #2: Only valid results are 1, 2, 3, 4 */ + switch (ret) { + case 1: + break; + case 2: + case 3: + case 4: + pr_info("Sub-NUMA cluster detected with %d nodes per L3 cache\n", ret); + rdt_resources_all[RDT_RESOURCE_L3].r_resctrl.mon_scope = RESCTRL_NODE; + break; + default: + goto insane; + } + + return ret; +insane: + pr_warn("SNC insanity: CPU nodes = %d num_l3_caches = %d\n", + (nr_node_ids - mem_only_nodes), num_l3_caches); + return 1; +} + static __init void rdt_init_res_defs_intel(void) { struct rdt_hw_resource *hw_res; struct rdt_resource *r; + snc_nodes_per_l3_cache = snc_get_config(); + for_each_rdt_resource(r) { hw_res = resctrl_to_arch_res(r);