Linux driver-core infrastructure
 help / color / mirror / Atom feed
From: Shrikanth Hegde <sshegde@linux.ibm.com>
To: maddy@linux.ibm.com, linuxppc-dev@lists.ozlabs.org,
	linux-kernel@vger.kernel.org, driver-core@lists.linux.dev
Cc: sshegde@linux.ibm.com, christophe.leroy@csgroup.eu,
	ritesh.list@gmail.com, tim.c.chen@linux.intel.com,
	yu.c.chen@intel.com, gregkh@linuxfoundation.org,
	rafael@kernel.org, dakr@kernel.org, johan@kernel.org,
	sudeep.holla@kernel.org, peterz@infradead.org, tglx@kernel.org,
	kprateek.nayak@amd.com, srikar@linux.ibm.com
Subject: [PATCH v2 3/6] cacheinfo: Add firmware topology replacement helpers
Date: Fri, 27 Nov 2026 20:52:39 +0530	[thread overview]
Message-ID: <20261127152242.306333-4-sshegde@linux.ibm.com> (raw)
In-Reply-To: <20261127152242.306333-1-sshegde@linux.ibm.com>

Generic cacheinfo normally retains cache leaves when a CPU is taken offline.
This permits the information to be reused when the CPU is brought online again
and the firmware topology has not changed.

Some platforms can instead detach or replace the firmware nodes referenced by
the retained leaves. Retaining cacheinfo in that case also retains stale
firmware tokens, cache IDs and cache properties.

Add cacheinfo_invalidate_cpu() to discard retained cacheinfo for DLPAR CPU
remove which is to be called after normal cacheinfo CPU hotplug teardown
completed and before its firmware nodes are detached.

Also add cacheinfo_teardown_all() and cacheinfo_rebuild_all() for platforms
that replace the complete firmware cache topology while CPUs remain online.

The destructive helpers require the CPU hotplug lock to be held. Platform
code that replaces firmware topology must hold CPU hotplug write lock.
This excludes readers protected by cpus_read_lock() while the per-CPU leaf
arrays are freed and rebuilt.

If rebuilding any online CPU fails, remove the hierarchy already rebuilt
for rebuilt CPUs, discard any partially initialized state and clear the
cacheinfo-derived scheduler and page-allocator state. This is better than
leaving inconsistent state. However if an any CPU is onlined later
belonging that cpu_map, then values may remain inflated until next full
rebuild.

[Both draft changelog and code are written by LLM under guidance]
Assisted-by: LLM
Signed-off-by: Shrikanth Hegde <sshegde@linux.ibm.com>
---
 drivers/base/cacheinfo.c  | 125 ++++++++++++++++++++++++++++++++++++++
 include/linux/cacheinfo.h |   3 +
 2 files changed, 128 insertions(+)

diff --git a/drivers/base/cacheinfo.c b/drivers/base/cacheinfo.c
index 4a23377e9312..cbeb4805ef19 100644
--- a/drivers/base/cacheinfo.c
+++ b/drivers/base/cacheinfo.c
@@ -1120,6 +1120,131 @@ static int cacheinfo_cpu_pre_down(unsigned int cpu)
 	return 0;
 }
 
+/*
+ * Clear the cached values before destroying the info_list.
+ * The CPU must have completed cacheinfo_cpu_pre_down()
+ *
+ * Freeing info_list also discards retained firmware tokens, cache IDs
+ * and cache properties. A subsequent online operation must discover
+ * cacheinfo from the current firmware topology.
+ */
+static void cacheinfo_reset_cpu(unsigned int cpu)
+{
+	struct cpu_cacheinfo *ci = ci_cacheinfo(cpu);
+
+	ci->num_levels = 0;
+	ci->num_leaves = 0;
+	ci->per_cpu_data_slice_size = 0;
+	ci->cpu_map_populated = false;
+	ci->early_ci_levels = false;
+
+	kfree(ci->info_list);
+	ci->info_list = NULL;
+}
+
+/*
+ * Discard retained cacheinfo for an offline CPU before detaching firmware
+ * nodes referenced by its cache leaves.
+ *
+ * The CPU must have completed cacheinfo_cpu_pre_down(), so its cache devices
+ * and sharing-map membership are no longer present. The caller must hold the
+ * CPU hotplug write lock.
+ */
+void cacheinfo_invalidate_cpu(unsigned int cpu)
+{
+	lockdep_assert_cpus_held();
+
+	if (WARN_ON_ONCE(cpu_online(cpu)))
+		return;
+
+	cacheinfo_reset_cpu(cpu);
+	setup_pcp_cacheinfo(cpu);
+}
+
+/*
+ * Remove cacheinfo before replacing the complete firmware cache topology.
+ * The caller must hold the CPU hotplug write lock to stop any concurrent
+ * readers for cacheinfo.
+ */
+void cacheinfo_teardown_all(void)
+{
+	unsigned int cpu;
+
+	lockdep_assert_cpus_held();
+
+	for_each_online_cpu(cpu) {
+		if (!ci_cacheinfo(cpu)->info_list)
+			continue;
+
+		cacheinfo_cpu_pre_down(cpu);
+	}
+
+	/* Offline CPUs may retain leaves allocated while previously online. */
+	for_each_possible_cpu(cpu)
+		cacheinfo_reset_cpu(cpu);
+
+	/*
+	 * Clear all derived state after removing the complete hierarchy.
+	 * Corresponding cacheinfo_rebuild_all will build those states.
+	 */
+	for_each_online_cpu(cpu)
+		setup_pcp_cacheinfo(cpu);
+
+	sched_update_llc_bytes(cpu_online_mask);
+}
+
+/*
+ * Rebuild cacheinfo after replacing the complete firmware cache topology.
+ * The caller must hold the CPU hotplug write lock.
+ */
+int cacheinfo_rebuild_all(void)
+{
+	unsigned int cpu, failed_cpu;
+	cpumask_var_t rebuilt;
+	int ret;
+
+	lockdep_assert_cpus_held();
+
+	if (!zalloc_cpumask_var(&rebuilt, GFP_KERNEL))
+		return -ENOMEM;
+
+	for_each_online_cpu(cpu) {
+		ret = cacheinfo_cpu_online(cpu);
+		if (ret) {
+			failed_cpu = cpu;
+			pr_err("cache creation failed for CPU: %d ret: %d\n",
+			       failed_cpu, ret);
+			goto rollback;
+		}
+
+		cpumask_set_cpu(cpu, rebuilt);
+	}
+
+	free_cpumask_var(rebuilt);
+	return 0;
+
+rollback:
+	/*
+	 * Remove active state only for CPUs that completed the online path.
+	 * Then discard all allocations, including partial failed-CPU state.
+	 */
+	pr_err("clearing cache entries for CPUs: %*pbl\n", cpumask_pr_args(rebuilt));
+	for_each_cpu(cpu, rebuilt) {
+		cacheinfo_cpu_pre_down(cpu);
+		cacheinfo_reset_cpu(cpu);
+	}
+
+	cacheinfo_reset_cpu(failed_cpu);
+
+	/* Clear all derived states */
+	for_each_online_cpu(cpu)
+		setup_pcp_cacheinfo(cpu);
+
+	sched_update_llc_bytes(cpu_online_mask);
+	free_cpumask_var(rebuilt);
+	return ret;
+}
+
 static int __init cacheinfo_sysfs_init(void)
 {
 	return cpuhp_setup_state(CPUHP_AP_BASE_CACHEINFO_ONLINE,
diff --git a/include/linux/cacheinfo.h b/include/linux/cacheinfo.h
index d3557a1b464b..2892df92fc24 100644
--- a/include/linux/cacheinfo.h
+++ b/include/linux/cacheinfo.h
@@ -94,6 +94,9 @@ struct cacheinfo *get_cpu_cacheinfo_llc(unsigned int cpu);
 int fetch_cache_info(unsigned int cpu);
 int detect_cache_attributes(unsigned int cpu);
 int cacheinfo_get_arch_id(unsigned int cpu, unsigned int level, unsigned int *id);
+void cacheinfo_invalidate_cpu(unsigned int cpu);
+void cacheinfo_teardown_all(void);
+int cacheinfo_rebuild_all(void);
 #ifndef CONFIG_ACPI_PPTT
 /*
  * acpi_get_cache_info() is only called on ACPI enabled
-- 
2.52.0


  parent reply	other threads:[~2026-11-27 15:23 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-27 15:22 [PATCH v2 0/6] cacheinfo, powerpc, sched: Move powerpc to generic cacheinfo Shrikanth Hegde
2026-11-27 15:22 ` [PATCH v2 1/6] cacheinfo: Allow architectures to provide custom cache IDs Shrikanth Hegde
2026-11-27 15:22 ` [PATCH v2 2/6] cacheinfo: Support unified caches described by d-cache properties Shrikanth Hegde
2026-11-27 15:22 ` Shrikanth Hegde [this message]
2026-11-27 15:22 ` [PATCH v2 4/6] powerpc: Set CPU device OF node before generic cacheinfo setup Shrikanth Hegde
2026-11-27 15:22 ` [PATCH v2 5/6] powerpc/cacheinfo: Move to generic cacheinfo Shrikanth Hegde
2026-11-27 15:22 ` [PATCH v2 6/6] sched/topology: Remove stale comment around llc_bytes on powerpc Shrikanth Hegde

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=20261127152242.306333-4-sshegde@linux.ibm.com \
    --to=sshegde@linux.ibm.com \
    --cc=christophe.leroy@csgroup.eu \
    --cc=dakr@kernel.org \
    --cc=driver-core@lists.linux.dev \
    --cc=gregkh@linuxfoundation.org \
    --cc=johan@kernel.org \
    --cc=kprateek.nayak@amd.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linuxppc-dev@lists.ozlabs.org \
    --cc=maddy@linux.ibm.com \
    --cc=peterz@infradead.org \
    --cc=rafael@kernel.org \
    --cc=ritesh.list@gmail.com \
    --cc=srikar@linux.ibm.com \
    --cc=sudeep.holla@kernel.org \
    --cc=tglx@kernel.org \
    --cc=tim.c.chen@linux.intel.com \
    --cc=yu.c.chen@intel.com \
    /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