mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 0/1] firmware: arm_rmm: Delay RMM activation
@ 2026-12-03 19:04 Suzuki K Poulose
  2026-12-03 19:05 ` [PATCH 1/1] " Suzuki K Poulose
  0 siblings, 1 reply; 3+ messages in thread
From: Suzuki K Poulose @ 2026-12-03 19:04 UTC (permalink / raw)
  To: linux-arm-kernel
  Cc: linux-kernel, catalin.marinas, will, sudeep.holla, maz,
	steven.price, aneesh.kumar, gshan, jonathan.cameron, kvmarm,
	oupton, linux-coco, alpergun, sdonthineni, lpieralisi,
	Suzuki K Poulose

Right now, we configure and activate the RMM at boot. This is not ideal
if there are no users for it (e.g, KVM is not enabled in CCA mode with
kvm-arm.mode=rmm). This disables useful features, like kexec reboot and
hibernation. Delay the RMM activation and leave it to a user (KVM or
PCI TSM which may use RMM as the TSM for IDE setup) as discussed in
[0]

A branch is made available at [0] which show cases how this will be
consumed by the KVM CCA driver support, based on cca-host-mm/v5 [1]

[0] https://lore.kernel.org/all/atj7A1CV4paW3htS@arm.com
[1] git@git.gitlab.arm.com:linux-arm/linux-cca.git cca/cca-host/fw_rmm/delayed-activation/integration
[2] https://lore.kernel.org/all/20261130142534.219108-1-steven.price@arm.com

Suzuki K Poulose (1):
  firmware: arm_rmm: Delay RMM activation

 drivers/firmware/arm_rmm/rmi.c | 68 ++++++++++++++++++++++++++--------
 include/linux/arm-rmi-cmds.h   |  1 +
 2 files changed, 54 insertions(+), 15 deletions(-)

-- 
2.43.0


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH 1/1] firmware: arm_rmm: Delay RMM activation
  2026-12-03 19:04 [PATCH 0/1] firmware: arm_rmm: Delay RMM activation Suzuki K Poulose
@ 2026-12-03 19:05 ` Suzuki K Poulose
       [not found]   ` <sashiko-outbox-248492@kernel.org>
  0 siblings, 1 reply; 3+ messages in thread
From: Suzuki K Poulose @ 2026-12-03 19:05 UTC (permalink / raw)
  To: linux-arm-kernel
  Cc: linux-kernel, catalin.marinas, will, sudeep.holla, maz,
	steven.price, aneesh.kumar, gshan, jonathan.cameron, kvmarm,
	oupton, linux-coco, alpergun, sdonthineni, lpieralisi,
	Suzuki K Poulose

Right now, we configure and activate the RMM at boot. This is not ideal
if there are no users for it (e.g, KVM is not enabled in CCA mode with
kvm-arm.mode=rmm). This disables useful features, like kexec reboot and
hibernation. Delay the RMM activation and leave it to a user (KVM or
PCI TSM which may use RMM as the TSM for IDE setup). Use an rmm_mutex
to serialize the activation requests.

Suggested-by: Catalin Marinas <catalin.marinas@arm.com>
Signed-off-by: Suzuki K Poulose <suzuki.poulose@arm.com>
---
 drivers/firmware/arm_rmm/rmi.c | 68 ++++++++++++++++++++++++++--------
 include/linux/arm-rmi-cmds.h   |  1 +
 2 files changed, 54 insertions(+), 15 deletions(-)

diff --git a/drivers/firmware/arm_rmm/rmi.c b/drivers/firmware/arm_rmm/rmi.c
index 77255a90e0a6..8e53d4ff9ade 100644
--- a/drivers/firmware/arm_rmm/rmi.c
+++ b/drivers/firmware/arm_rmm/rmi.c
@@ -15,9 +15,12 @@
 
 /* RMM v2.0 defines RmiFeatureRegister0 to RmiFeatureRegister4. */
 static unsigned long rmi_feat_reg_cache[5] __ro_after_init;
+static bool rmm_ready_for_activation;
 static bool arm64_rmm_active;
 static bool arm64_rmi_is_available;
 
+static DEFINE_MUTEX(rmm_mutex);
+
 /* is_rmm_active: Returns if the RMM is ACTIVE */
 bool is_rmm_active(void)
 {
@@ -804,7 +807,7 @@ static int rmi_rmm_config_set(unsigned long cfg_ptr)
 	return regs.a0;
 }
 
-static int rmi_configure(void)
+static int __init rmi_configure(void)
 {
 	unsigned long granule_feature;
 	unsigned long granule_size;
@@ -1081,23 +1084,10 @@ static int rmi_init_memory(void)
 	return register_memory_notifier(&rmi_memory_nb);
 }
 
-static int __init arm64_init_rmi(void)
+static int rmm_do_activate(void)
 {
 	int ret;
 
-	/* If we can't agree on the RMI ABI version, don't proceed further */
-	ret = rmi_check_version();
-	if (ret)
-		return ret;
-
-	ret = rmi_read_features();
-	if (ret)
-		return ret;
-
-	ret = rmi_configure();
-	if (ret)
-		return ret;
-
 	/* Activate the RMM */
 	struct rmi_sro_state *sro __free(kfree) = kmalloc_obj(*sro);
 	if (!sro)
@@ -1123,6 +1113,54 @@ static int __init arm64_init_rmi(void)
 	/* Mark the RMM is usable */
 	WRITE_ONCE(arm64_rmi_is_available, true);
 	pr_info("RMI configured\n");
+	return 0;
+}
+
+
+/*
+ * rmm_activate - Activate the RMM if it is usable.
+ *
+ * Returns 0 on success. i.e., when the RMM has been activated (or is already
+ * ACTIVE).
+ */
+int rmm_activate(void)
+{
+	int ret;
+
+	guard(mutex)(&rmm_mutex);
+
+	if (!READ_ONCE(rmm_ready_for_activation))
+		return -ENXIO;
+	if (READ_ONCE(arm64_rmi_is_available))
+		return 0;
+	if (READ_ONCE(arm64_rmm_active))
+		return -ENXIO;
+	ret = rmm_do_activate();
+	if (ret)
+		rmm_ready_for_activation = false;
+	return ret;
+}
+EXPORT_SYMBOL_GPL(rmm_activate);
+
+static int __init arm64_init_rmi(void)
+{
+	int ret;
+
+	guard(mutex)(&rmm_mutex);
+	/* If we can't agree on the RMI ABI version, don't proceed further */
+	ret = rmi_check_version();
+	if (ret)
+		return ret;
+
+	ret = rmi_read_features();
+	if (ret)
+		return ret;
+
+	ret = rmi_configure();
+	if (ret)
+		return ret;
+
+	rmm_ready_for_activation = true;
 
 	return 0;
 }
diff --git a/include/linux/arm-rmi-cmds.h b/include/linux/arm-rmi-cmds.h
index a84e50ac851c..1aa3a293af11 100644
--- a/include/linux/arm-rmi-cmds.h
+++ b/include/linux/arm-rmi-cmds.h
@@ -65,6 +65,7 @@ static __always_inline void rmi_smccc_invoke_once(struct arm_smccc_1_2_regs *reg
 	arm_smccc_1_2_smc(regs, regs);
 }
 
+int rmm_activate(void);
 unsigned long rmi_feat_reg(unsigned int index);
 
 int rmi_delegate_range(phys_addr_t phys, unsigned long size,
-- 
2.43.0


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH 1/1] firmware: arm_rmm: Delay RMM activation
       [not found]   ` <sashiko-outbox-248492@kernel.org>
@ 2026-12-03 22:51     ` Suzuki K Poulose
  0 siblings, 0 replies; 3+ messages in thread
From: Suzuki K Poulose @ 2026-12-03 22:51 UTC (permalink / raw)
  To: sashiko-reviews
  Cc: kvmarm, Oliver Upton, Marc Zyngier, Catalin Marinas,
	linux-kernel, Will Deacon, Gavin Shan

On 03/12/2026 19:23, sashiko-bot@kernel.org wrote:
 > Thank you for your contribution! Sashiko AI review found 4 potential
 > issues to consider:
 > - [High] firmware: arm_rmm: rmm_activate() bypasses kexec and kdump RMM
 >    guard
 > - [High] firmware: arm_rmm: rmm_activate() races with hibernation and
 >    /dev/snapshot
 > - [Medium] firmware: arm_rmm: rmi_init_memory() lacks mem_hotplug_lock
 >    at runtime
 > - [Medium] firmware: arm_rmm: rmi_init_metadata() fails on hotplugged
 >    movable/offline RAM
 > --
 >
 > commit 3e173959956ecb18d9c76645df94a185af047180
 > Author: Suzuki K Poulose <suzuki.poulose@arm.com>
 >
 > firmware: arm_rmm: Delay RMM activation
 >
 > This commit delays RMM activation from subsys_initcall(arm64_init_rmi) to
 > an on-demand rmm_activate() call serialized by rmm_mutex, leaving 
boot-time
 > initialization to version negotiation, feature reading, and 
configuration.
 >
 >> diff --git a/drivers/firmware/arm_rmm/rmi.c 
b/drivers/firmware/arm_rmm/rmi.c
 >> index 77255a90e0a65..8e53d4ff9ade7 100644
 >> --- a/drivers/firmware/arm_rmm/rmi.c
 >> +++ b/drivers/firmware/arm_rmm/rmi.c
 >
 > [ ... ]
 >
 >> @@ -1081,23 +1084,10 @@ static int rmi_init_memory(void)
 >>   	return register_memory_notifier(&rmi_memory_nb);
 >>   }
 >>
 >> -static int __init arm64_init_rmi(void)
 >> +static int rmm_do_activate(void)
 >>   {
 >>   	int ret;
 >>
 >> -	/* If we can't agree on the RMI ABI version, don't proceed further */
 >> -	ret = rmi_check_version();
 >> -	if (ret)
 >> -		return ret;
 >> -
 >> -	ret = rmi_read_features();
 >> -	if (ret)
 >> -		return ret;
 >> -
 >> -	ret = rmi_configure();
 >> -	if (ret)
 >> -		return ret;
 >> -
 >>   	/* Activate the RMM */
 >>   	struct rmi_sro_state *sro __free(kfree) = kmalloc_obj(*sro);
 >>   	if (!sro)
 >
 > [Severity: Medium]
 > Can rmi_init_memory() race with memory hotplug now that rmm_do_activate()
 > runs it at runtime?
 >
 > On arm64 (CONFIG_ARCH_KEEP_MEMBLOCK=y), __add_memory_resource() updates
 > memblock.memory under mem_hotplug_lock, while rmi_init_metadata() 
walks it
 > with for_each_mem_region() without holding get_online_mems():
 >
 > CPU0                                CPU1
 > rmm_do_activate()
 >    rmi_init_memory()
 >      rmi_init_metadata()
 >        for_each_mem_region(r)
 >          rmi_prepare_memory(...)
 >                                      __add_memory_resource()
 >                                        mem_hotplug_begin()
 >                                        memblock_add_node(...)
 >                                          memblock_double_array()
 >                                            kfree(old_array)
 >        r++ / dereference r           <-- use-after-free
 >
 > Because for_each_mem_region() also re-reads memblock.memory.regions on
 > each iteration, comparing the old pointer against the new array bounds
 > results in an out-of-bounds walk.
 > > Separately, memory hot-added and onlined via online_pages() between
 > rmi_init_metadata() and register_memory_notifier(&rmi_memory_nb) misses
 > MEM_GOING_ONLINE and bypasses rmi_prepare_memory().  Holding
 > get_online_mems() / put_online_mems() across both calls in
 > rmi_init_memory() would close both races.
 >

Ack

 > [Severity: Medium]
 > Does running rmi_init_metadata() at runtime regress systems with memory
 > hot-added before rmm_activate() is called?
 >
 > On arm64, __add_memory_resource() inserts hotplugged and driver-managed
 > memory (such as CXL or DAX kmem) into memblock.memory even when it stays
 > offline or is onlined into ZONE_MOVABLE.  Commit 06898e5320b5 ("firmware:
 > arm_rmm: hotplug: Skip memory added to ZONE_MOVABLE") exempted
 > ZONE_MOVABLE ranges in rmi_memory_notifier(), but rmi_init_metadata()
 > only skips memblock_is_nomap(r) and calls rmi_prepare_memory() on every
 > other memblock.memory region.
 >
 > If any pre-activation offline or ZONE_MOVABLE region lacks RMM fine
 > granule tracking or firmware-managed L1 GPTs, rmi_prepare_memory() fails
 > and rmm_activate() clears rmm_ready_for_activation, permanently disabling
 > RMM for the rest of the boot.
 >
 >> @@ -1123,6 +1113,54 @@ static int __init arm64_init_rmi(void)
 >>   	/* Mark the RMM is usable */
 >>   	WRITE_ONCE(arm64_rmi_is_available, true);
 >>   	pr_info("RMI configured\n");
 >> +	return 0;
 >> +}
 >> +
 >> +
 >> +/*
 >> + * rmm_activate - Activate the RMM if it is usable.
 >> + *
 >> + * Returns 0 on success. i.e., when the RMM has been activated (or 
is already
 >> + * ACTIVE).
 >> + */
 >> +int rmm_activate(void)
 >> +{
 >> +	int ret;
 >> +
 >> +	guard(mutex)(&rmm_mutex);
 >> +
 >> +	if (!READ_ONCE(rmm_ready_for_activation))
 >> +		return -ENXIO;
 >> +	if (READ_ONCE(arm64_rmi_is_available))
 >> +		return 0;
 >> +	if (READ_ONCE(arm64_rmm_active))
 >> +		return -ENXIO;
 >> +	ret = rmm_do_activate();
 >> +	if (ret)
 >> +		rmm_ready_for_activation = false;
 >> +	return ret;
 >> +}
 >> +EXPORT_SYMBOL_GPL(rmm_activate);
 >
 > [Severity: High]
 > Can rmm_activate() bypass the kexec and kdump guard when an image is
 > preloaded before RMM activation?

  We expect the users, KVM to activate at boot. On arm64,
KVM is always builtin. The PCI TSM IDE driver, not sure if that can
be built as a module and can be used without KVM. Anyways, I can
make this safe by using something like a refcount on RMM activation
"blockers"


Suzuki


 >
 > Commit fe4a24613bc2 ("arm64: Block hibernate and kexec while RMM is
 > active") only checks is_rmm_active() in machine_kexec_prepare() at image
 > load time.  With activation delayed past boot, kdump.service (or 
kexec -l)
 > can load kexec_crash_image or kexec_image while arm64_rmm_active is still
 > false.  Because rmm_activate() neither synchronizes with kexec_trylock()
 > nor checks for loaded images, and kernel_kexec() / __crash_kexec() /
 > machine_kexec() never re-check is_rmm_active(), entering the preloaded
 > kernel with active RMM and delegated Realm PAS pages will cause fatal
 > Granule Protection Faults.
 >
 > There is also a concurrent race between unlocked is_rmm_active() in
 > machine_kexec_prepare() and rmm_do_activate(), which donates granules in
 > SMC_RMI_RMM_ACTIVATE before setting arm64_rmm_active to true.
 >
 > [Severity: High]
 > Does rmm_activate() race with hibernation and /dev/snapshot now that
 > arm64_rmm_active can transition to true at runtime?
 >
 > arch_hibernation_available() returns !is_rmm_active(), but hibernate()
 > checks hibernation_available() once before lock_system_sleep(),
 > pm_sleep_fs_sync(), and freeze_processes(), and never re-checks it before
 > hibernation_snapshot():
 >
 > CPU0                                CPU1
 > hibernate()
 >    hibernation_available() -> true
 >    lock_system_sleep()
 >    pm_sleep_fs_sync()
 >                                      rmm_activate()
 >                                        SMC_RMI_RMM_ACTIVATE
 >                                        arm64_rmm_active = true
 >    hibernation_snapshot()            <-- fatal GPF on Realm PAS pages
 >
 > Similarly, snapshot_open() checks hibernation_available() only when
 > /dev/snapshot is opened, so a subsequent SNAPSHOT_CREATE_IMAGE or
 > SNAPSHOT_ATOMIC_RESTORE ioctl proceeds after rmm_activate() has already
 > donated or delegated pages.  Synchronizing rmm_activate() with
 > lock_system_sleep() / hibernation_in_progress() (or a PM notifier for
 > PM_HIBERNATION_PREPARE and PM_RESTORE_PREPARE) would prevent RMM
 > activation from overlapping hibernation.
 >

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-12-03 22:51 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-12-03 19:04 [PATCH 0/1] firmware: arm_rmm: Delay RMM activation Suzuki K Poulose
2026-12-03 19:05 ` [PATCH 1/1] " Suzuki K Poulose
     [not found]   ` <sashiko-outbox-248492@kernel.org>
2026-12-03 22:51     ` Suzuki K Poulose

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®