From patchwork Thu Mar 23 22:42:42 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Radu Rendec X-Patchwork-Id: 13186122 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 0E409C6FD1C for ; Thu, 23 Mar 2023 22:45:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:Message-Id:Date:Subject:Cc :To:From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=UzluR519ZNWi3IrNz1D/9L1iAFOsy2V+99eJoH3HQrg=; b=ilzY8d6dzhUGQq 6fnVhGd5aLbuV51kqC1WVP/tOltmvaLvucdSklt5ljqpo1JSPbdJpDI2nZ4xMKF/2pMIiADJggxt6 PPTqMtj4c1siue5u+M23WTmNjBPKSLsIgnh+grSuJ17c4+QnZeEaa4vPfydLJ6FbwtR9MtJ/NQHCz igeShhBaCaDmC5nV0thgnXZSRamWrpnJC3hSMtKzLIkIRZE3OktpDP2PFQDkXILi7bIVVRKUF31LK 6KPdJG8eg4KbAPgmGu8lON9Q+oBqm9uauTsj6L/uIWnsPtJpaE68N+GUyY/WvBbjkUIbKw1UrSxSP H+LUpDQd7yMNHBGoiyBw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1pfTg9-0036PU-1Z; Thu, 23 Mar 2023 22:45:05 +0000 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1pfTg7-0036On-10 for linux-arm-kernel@lists.infradead.org; Thu, 23 Mar 2023 22:45:04 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1679611501; 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: content-transfer-encoding:content-transfer-encoding; bh=D5mgiFvz9CCdE4sW6q9w0qPDEDfG88P7d7C4c/d55dk=; b=fBiHGoig17bCtObrWSNwAqvI6fXEQ0UHD8/4nMu0f8dl6HC5O1D/c0XcemSyNV39GuBmGR 2jBmDkZZOsaroCGKqIXwWrO0ZAyUS56R05WcHyoRCoMC++1Jeg1iOdUIibTOG/Z6ds3lxC TJY5r6af29ZEJbqEPpMKRvof9szY4pE= Received: from mail-qt1-f199.google.com (mail-qt1-f199.google.com [209.85.160.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-323-bCgP8SuoNTqgMXmQfw0PKw-1; Thu, 23 Mar 2023 18:42:56 -0400 X-MC-Unique: bCgP8SuoNTqgMXmQfw0PKw-1 Received: by mail-qt1-f199.google.com with SMTP id ga17-20020a05622a591100b003bfdf586476so13874389qtb.7 for ; Thu, 23 Mar 2023 15:42:55 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679611374; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=D5mgiFvz9CCdE4sW6q9w0qPDEDfG88P7d7C4c/d55dk=; b=CS6eYq0Q379Cxqpv8Ai7/YQRIp2BZDrWUjO9omgBUuNR+MRSSWgX7mC9WiLki3Xr1f N8jTwKlkqHrelhjPKczIv0eUiIn8+StEQ2QmngbK+H5zZA1gd8j1C9ZVXjF/r+Ua7x7v r7hWaNsi2ijwWXBx2tnBSTPfskxu2AYRHUiyZL5NqWmS9AcGANMANUHZPgSApfXfnn9V mU3x0L8F/JnVNl4rDCQG22KnHSbaI+CJJIG7t1tjystBDpLYP0IbDX0KPKg1ll9BM4L5 uJeEJGDSplDTg1VCj29aWK3x+/01SZS4KiduD59SqMxR+5EBhk7ZI1GsG70zwuE6w0Dr ADTQ== X-Gm-Message-State: AO0yUKW3iLQHsB7Hs6FF6IorHA3MpKqbRZO3L4eUI8pRMHtvDVC5rXIN Q1O3Rc4El9XHhWbBQilThjcyewiVeEygr6ZugLEHES/mQvXrzdXlY/HliFrZfZVENRBbdSYyvkU Cdfj15z5AKzGyWnTv1mdOfoB++K1lfxk0cUQ6+2qS24tAnYV3v0zI4tx6Zl5CV9AiS2wB9s2uMb xGdkjDvofRWqjNF2qoMg== X-Received: by 2002:a05:622a:5cf:b0:3e3:8119:7c68 with SMTP id d15-20020a05622a05cf00b003e381197c68mr1834853qtb.52.1679611374098; Thu, 23 Mar 2023 15:42:54 -0700 (PDT) X-Google-Smtp-Source: AK7set/c8+tPB+kPz4xxEoEg8QytbV6nyOS3nzKt6zN0LL6QAxWB0Swn5jLky00FQ3zPSfLkxhNyFw== X-Received: by 2002:a05:622a:5cf:b0:3e3:8119:7c68 with SMTP id d15-20020a05622a05cf00b003e381197c68mr1834813qtb.52.1679611373731; Thu, 23 Mar 2023 15:42:53 -0700 (PDT) Received: from thinkpad-p1.localdomain (cpe00fc8d79db03-cm00fc8d79db00.cpe.net.fido.ca. [72.137.118.218]) by smtp.gmail.com with ESMTPSA id d124-20020ae9ef82000000b007469587115dsm6844996qkg.19.2023.03.23.15.42.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Mar 2023 15:42:53 -0700 (PDT) From: Radu Rendec To: linux-arm-kernel@lists.infradead.org Cc: Pierre Gondois , Sudeep Holla , Adrien Thierry , Eric Chanudet Subject: [RFC PATCH] arch_topology: Pre-allocate cacheinfo from primary CPU Date: Thu, 23 Mar 2023 18:42:42 -0400 Message-Id: <20230323224242.31142-1-rrendec@redhat.com> X-Mailer: git-send-email 2.39.2 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230323_154503_545392_5C3E69F0 X-CRM114-Status: GOOD ( 26.37 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Commit 5944ce092b97 ("arch_topology: Build cacheinfo from primary CPU") tries to build the cacheinfo from the primary CPU prior to secondary CPUs boot, if the DT/ACPI description contains cache information. However, if such information is not present, it still reverts to the old behavior, which allocates the cacheinfo memory on each secondary CPU. On RT kernels, this triggers a "BUG: sleeping function called from invalid context" because the allocation is done before preemption is first enabled on the secondary CPU. The solution is to add cache information to DT/ACPI, but at least on aarch64 systems this can be avoided by leveraging automatic detection (through the CLIDR_EL1 register), which is already implemented but currently doesn't work on RT kernels for the reason described above. This patch attempts to enable automatic detection for RT kernels when no DT/ACPI cache information is available, by pre-allocating cacheinfo memory on the primary CPU. The allocated memory size depends on the number of cache leaves, which at that point is unknown without the DT/ACPI information. What this patch does is guess the number of cache leaves and pre-allocate memory on the primary CPU, then go back and reallocate the memory if the guess turns out to be wrong when automatic detection eventually runs on the secondary CPU. In that case, it will basically revert to the original behavior and still trigger a splat on RT kernels. The assumption is that most systems have identical CPUs, so the number of cache leaves will be the same on the secondary CPUs as the primary CPU. The "guess" uses the number of leaves of the primary CPU. If the DT/ACPI cache information is present, the previous behavior of pre-allocating memory through init_cpu_topology() is preserved. With this patch applied, automatic detection should work on RT kernels for all systems with identical CPUs, without requiring to modify the DT/ACPI to include the cache information. Signed-off-by: Radu Rendec Signed-off-by: Radu Rendec --- arch/arm64/kernel/smp.c | 3 +++ drivers/base/cacheinfo.c | 49 +++++++++++++++++++++++++++++++++++++-- include/linux/cacheinfo.h | 1 + 3 files changed, 51 insertions(+), 2 deletions(-) diff --git a/arch/arm64/kernel/smp.c b/arch/arm64/kernel/smp.c index 4e8327264255..7ee2c38185d4 100644 --- a/arch/arm64/kernel/smp.c +++ b/arch/arm64/kernel/smp.c @@ -33,6 +33,7 @@ #include #include #include +#include #include #include @@ -730,6 +731,8 @@ void __init smp_prepare_cpus(unsigned int max_cpus) numa_store_cpu_info(this_cpu); numa_add_cpu(this_cpu); + pre_alloc_cache_info(); + /* * If UP is mandated by "nosmp" (which implies "maxcpus=0"), don't set * secondary CPUs present. diff --git a/drivers/base/cacheinfo.c b/drivers/base/cacheinfo.c index f6573c335f4c..c7d691ef7839 100644 --- a/drivers/base/cacheinfo.c +++ b/drivers/base/cacheinfo.c @@ -28,6 +28,9 @@ static DEFINE_PER_CPU(struct cpu_cacheinfo, ci_cpu_cacheinfo); #define per_cpu_cacheinfo_idx(cpu, idx) \ (per_cpu_cacheinfo(cpu) + (idx)) +static DEFINE_PER_CPU(struct cacheinfo *, pre_alloc_ci_list); +static unsigned int pre_alloc_ci_leaves; + struct cpu_cacheinfo *get_cpu_cacheinfo(unsigned int cpu) { return ci_cacheinfo(cpu); @@ -408,9 +411,51 @@ int __weak populate_cache_leaves(unsigned int cpu) return -ENOENT; } -static inline -int allocate_cache_info(int cpu) +void pre_alloc_cache_info(void) { + unsigned int leaves = cache_leaves(smp_processor_id()); + unsigned int cpu; + struct cacheinfo *ci; + + if (!leaves) + return; + + for_each_possible_cpu(cpu) { + if (per_cpu_cacheinfo(cpu)) + /* + * Early allocation through init_cpu_topology() was + * successful, so there is no point in pre-allocating. + */ + continue; + + ci = kcalloc(leaves, sizeof(struct cacheinfo), GFP_ATOMIC); + if (!ci) { + for_each_possible_cpu(cpu) + kfree(per_cpu(pre_alloc_ci_list, cpu)); + return; + } + + per_cpu(pre_alloc_ci_list, cpu) = ci; + } + + pre_alloc_ci_leaves = leaves; +} + +static int allocate_cache_info(int cpu) +{ + struct cacheinfo *ci = per_cpu(pre_alloc_ci_list, cpu); + + if (ci) { + per_cpu(pre_alloc_ci_list, cpu) = NULL; + + if (cache_leaves(cpu) <= pre_alloc_ci_leaves) { + per_cpu_cacheinfo(cpu) = ci; + return 0; + } + + kfree(ci); + } + per_cpu_cacheinfo(cpu) = kcalloc(cache_leaves(cpu), sizeof(struct cacheinfo), GFP_ATOMIC); if (!per_cpu_cacheinfo(cpu)) { diff --git a/include/linux/cacheinfo.h b/include/linux/cacheinfo.h index 908e19d17f49..23f9dac61d67 100644 --- a/include/linux/cacheinfo.h +++ b/include/linux/cacheinfo.h @@ -85,6 +85,7 @@ int populate_cache_leaves(unsigned int cpu); int cache_setup_acpi(unsigned int cpu); bool last_level_cache_is_valid(unsigned int cpu); bool last_level_cache_is_shared(unsigned int cpu_x, unsigned int cpu_y); +void pre_alloc_cache_info(void); int fetch_cache_info(unsigned int cpu); int detect_cache_attributes(unsigned int cpu); #ifndef CONFIG_ACPI_PPTT