mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "tip-bot2 for Ricardo Neri" <tip-bot2@linutronix.de>
To: linux-tip-commits@vger.kernel.org
Cc: Ricardo Neri <ricardo.neri-calderon@linux.intel.com>,
	"Borislav Petkov (AMD)" <bp@alien8.de>,
	Radu Rendec <rrendec@redhat.com>,
	Nikolay Borisov <nik.borisov@suse.com>,
	Andreas Herrmann <aherrmann@suse.de>,
	Sudeep Holla <sudeep.holla@arm.com>,
	stable@vger.kernel.org, #@tip-bot2.tec.linutronix.de,
	6.3+@tip-bot2.tec.linutronix.de, x86@kernel.org,
	linux-kernel@vger.kernel.org
Subject: [tip: x86/urgent] cacheinfo: Allocate memory during CPU hotplug if not done from the primary CPU
Date: Fri, 06 Dec 2024 12:24:07 -0000	[thread overview]
Message-ID: <173348784735.412.17119811902152182023.tip-bot2@tip-bot2> (raw)
In-Reply-To: <20241128002247.26726-2-ricardo.neri-calderon@linux.intel.com>

The following commit has been merged into the x86/urgent branch of tip:

Commit-ID:     b3fce429a1e030b50c1c91351d69b8667eef627b
Gitweb:        https://git.kernel.org/tip/b3fce429a1e030b50c1c91351d69b8667eef627b
Author:        Ricardo Neri <ricardo.neri-calderon@linux.intel.com>
AuthorDate:    Wed, 27 Nov 2024 16:22:46 -08:00
Committer:     Borislav Petkov (AMD) <bp@alien8.de>
CommitterDate: Fri, 06 Dec 2024 13:07:47 +01:00

cacheinfo: Allocate memory during CPU hotplug if not done from the primary CPU

Commit

  5944ce092b97 ("arch_topology: Build cacheinfo from primary CPU")

adds functionality that architectures can use to optionally allocate and
build cacheinfo early during boot. Commit

  6539cffa9495 ("cacheinfo: Add arch specific early level initializer")

lets secondary CPUs correct (and reallocate memory) cacheinfo data if
needed.

If the early build functionality is not used and cacheinfo does not need
correction, memory for cacheinfo is never allocated. x86 does not use
the early build functionality. Consequently, during the cacheinfo CPU
hotplug callback, last_level_cache_is_valid() attempts to dereference
a NULL pointer:

  BUG: kernel NULL pointer dereference, address: 0000000000000100
  #PF: supervisor read access in kernel mode
  #PF: error_code(0x0000) - not present page
  PGD 0 P4D 0
  Oops: 0000 [#1] PREEPMT SMP NOPTI
  CPU: 0 PID 19 Comm: cpuhp/0 Not tainted 6.4.0-rc2 #1
  RIP: 0010: last_level_cache_is_valid+0x95/0xe0a

Allocate memory for cacheinfo during the cacheinfo CPU hotplug callback
if not done earlier.

Moreover, before determining the validity of the last-level cache info,
ensure that it has been allocated. Simply checking for non-zero
cache_leaves() is not sufficient, as some architectures (e.g., Intel
processors) have non-zero cache_leaves() before allocation.

Dereferencing NULL cacheinfo can occur in update_per_cpu_data_slice_size().
This function iterates over all online CPUs. However, a CPU may have come
online recently, but its cacheinfo may not have been allocated yet.

While here, remove an unnecessary indentation in allocate_cache_info().

  [ bp: Massage. ]

Fixes: 6539cffa9495 ("cacheinfo: Add arch specific early level initializer")
Signed-off-by: Ricardo Neri <ricardo.neri-calderon@linux.intel.com>
Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de>
Reviewed-by: Radu Rendec <rrendec@redhat.com>
Reviewed-by: Nikolay Borisov <nik.borisov@suse.com>
Reviewed-by: Andreas Herrmann <aherrmann@suse.de>
Reviewed-by: Sudeep Holla <sudeep.holla@arm.com>
Cc: stable@vger.kernel.org # 6.3+
Link: https://lore.kernel.org/r/20241128002247.26726-2-ricardo.neri-calderon@linux.intel.com
---
 drivers/base/cacheinfo.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)

diff --git a/drivers/base/cacheinfo.c b/drivers/base/cacheinfo.c
index 609935a..cf0d455 100644
--- a/drivers/base/cacheinfo.c
+++ b/drivers/base/cacheinfo.c
@@ -58,7 +58,7 @@ bool last_level_cache_is_valid(unsigned int cpu)
 {
 	struct cacheinfo *llc;
 
-	if (!cache_leaves(cpu))
+	if (!cache_leaves(cpu) || !per_cpu_cacheinfo(cpu))
 		return false;
 
 	llc = per_cpu_cacheinfo_idx(cpu, cache_leaves(cpu) - 1);
@@ -458,11 +458,9 @@ int __weak populate_cache_leaves(unsigned int cpu)
 	return -ENOENT;
 }
 
-static inline
-int allocate_cache_info(int cpu)
+static inline int allocate_cache_info(int cpu)
 {
-	per_cpu_cacheinfo(cpu) = kcalloc(cache_leaves(cpu),
-					 sizeof(struct cacheinfo), GFP_ATOMIC);
+	per_cpu_cacheinfo(cpu) = kcalloc(cache_leaves(cpu), sizeof(struct cacheinfo), GFP_ATOMIC);
 	if (!per_cpu_cacheinfo(cpu)) {
 		cache_leaves(cpu) = 0;
 		return -ENOMEM;
@@ -534,7 +532,11 @@ static inline int init_level_allocate_ci(unsigned int cpu)
 	 */
 	ci_cacheinfo(cpu)->early_ci_levels = false;
 
-	if (cache_leaves(cpu) <= early_leaves)
+	/*
+	 * Some architectures (e.g., x86) do not use early initialization.
+	 * Allocate memory now in such case.
+	 */
+	if (cache_leaves(cpu) <= early_leaves && per_cpu_cacheinfo(cpu))
 		return 0;
 
 	kfree(per_cpu_cacheinfo(cpu));

  parent reply	other threads:[~2024-12-06 12:24 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-11-28  0:22 [PATCH v8 0/2] x86/cacheinfo: Set the number of leaves per CPU Ricardo Neri
2024-11-28  0:22 ` [PATCH v8 1/2] cacheinfo: Allocate memory during CPU hotplug if not done from the primary CPU Ricardo Neri
2024-11-29 16:12   ` Radu Rendec
2024-12-01 22:39     ` Ricardo Neri
2024-12-02  6:53   ` Nikolay Borisov
2024-12-03  4:09     ` Ricardo Neri
2024-12-02  8:02   ` Andreas Herrmann
2024-12-03  4:09     ` Ricardo Neri
2024-12-02 11:03   ` Sudeep Holla
2024-12-03  4:08     ` Ricardo Neri
2024-12-06 12:24   ` tip-bot2 for Ricardo Neri [this message]
2024-11-28  0:22 ` [PATCH v8 2/2] x86/cacheinfo: Delete global num_cache_leaves Ricardo Neri
2024-12-04 14:32   ` Borislav Petkov
2024-12-04 16:39     ` Ricardo Neri
2024-12-04 19:32       ` Borislav Petkov
2024-12-04 22:22         ` Ricardo Neri
2024-12-05 15:14           ` Borislav Petkov
2024-12-06  1:47             ` Ricardo Neri
2024-12-06 12:08               ` Borislav Petkov
2024-12-06 12:24   ` [tip: x86/urgent] " tip-bot2 for Ricardo Neri
2024-11-28  9:30 ` [PATCH v8 0/2] x86/cacheinfo: Set the number of leaves per CPU Borislav Petkov
2024-12-01 22:41   ` Ricardo Neri
2024-12-02 10:31     ` Borislav Petkov
2025-07-19  5:50 [tip: x86/urgent] cacheinfo: Allocate memory during CPU hotplug if not done from the primary CPU Logan Matthews

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=173348784735.412.17119811902152182023.tip-bot2@tip-bot2 \
    --to=tip-bot2@linutronix.de \
    --cc=#@tip-bot2.tec.linutronix.de \
    --cc=6.3+@tip-bot2.tec.linutronix.de \
    --cc=aherrmann@suse.de \
    --cc=bp@alien8.de \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-tip-commits@vger.kernel.org \
    --cc=nik.borisov@suse.com \
    --cc=ricardo.neri-calderon@linux.intel.com \
    --cc=rrendec@redhat.com \
    --cc=stable@vger.kernel.org \
    --cc=sudeep.holla@arm.com \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®