Skip to contentSkip to conversation

regmap: Fix race condition in hwspinlock irqsave routine

PATCH Superseded Yu-Chun Lin 2 participants 4 messages Jan 7 – Jan 12

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 regmap: Fix race condition in hwspinlock irqsave routine

From: Cheng-Yu Lee <[email protected]>

Previously, the address of the shared member '&map->spinlock_flags' was passed directly to 'hwspin_lock_timeout_irqsave'. This creates a race condition where multiple contexts contending for the lock could overwrite the shared flags variable, potentially corrupting the state for the current lock owner.

Fix this by using a local stack variable 'flags' to store the IRQ state temporarily.

Fixes 8698b9364710 ("regmap: Add hardware spinlock support")

---
v2:
- Initialize 'flags' to 0. This fixes a -Werror build failure when CONFIG_HWSPINLOCK is disabled, as the stub function in that case does not initialize the pointer.

v1: https://lore.kernel.org/lkml/[email protected]/

 drivers/base/regmap/regmap.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/base/regmap/regmap.c +3 -1
index ce9be3989a21..8d889372517f 100644
@@ -408,9 +408,11 @@ static void regmap_lock_hwlock_irq(void *__map)
 static void regmap_lock_hwlock_irqsave(void *__map)
 {
 	struct regmap *map = __map;
+	unsigned long flags;
 
 	hwspin_lock_timeout_irqsave(map->hwlock, UINT_MAX,
-				    &map->spinlock_flags);
+				    &flags);
+	map->spinlock_flags = flags;
 }
 
 static void regmap_unlock_hwlock(void *__map)
Signature · 1 line
2.34.1
maintainer
#1 lore raw

drivers/base/regmap/regmap.c static void regmap_lock_hwlock_irq(void *__map) · line 415

Quoted 13 lines · Yu-Chun Lin

v2:
- Initialize 'flags' to 0. This fixes a -Werror build failure when CONFIG_HWSPINLOCK is disabled, as the stub function in that case does not initialize the pointer.

{

drivers/base/regmap/regmap.c +3 -1
 	struct regmap *map = __map;
+	unsigned long flags;
 
 	hwspin_lock_timeout_irqsave(map->hwlock, UINT_MAX,
-				    &map->spinlock_flags);
+				    &flags);
+	map->spinlock_flags = flags;

I'm not seeing that initialisation in the actual patch?

#2 lore raw
Quoted 11 lines · Yu-Chun Lin
Quoted 10 lines

v2:
- Initialize 'flags' to 0. This fixes a -Werror build failure when CONFIG_HWSPINLOCK is disabled, as the stub function in that case does not initialize the pointer.

{

drivers/base/regmap/regmap.c +1 -0
 	struct regmap *map = __map;
+	unsigned long flags;
Quoted 6 lines

drivers/base/regmap/regmap.c static void regmap_lock_hwlock_irq(void *__map) · line 415

Quoted 4 lines
drivers/base/regmap/regmap.c +2 -1
 	hwspin_lock_timeout_irqsave(map->hwlock, UINT_MAX,
-				    &map->spinlock_flags);
+				    &flags);
+	map->spinlock_flags = flags;

I'm not seeing that initialisation in the actual patch?

Oops, sorry about that. I made a mistake and accidentally kept the v1 code. I will fix it in v3.

Yu-Chun

maintainer
#3 lore raw
In reply to Yu-Chun Lin
Quoted 10 lines · Yu-Chun Lin

Previously, the address of the shared member '&map->spinlock_flags' was passed directly to 'hwspin_lock_timeout_irqsave'. This creates a race condition where multiple contexts contending for the lock could overwrite the shared flags variable, potentially corrupting the state for the current lock owner.

Fix this by using a local stack variable 'flags' to store the IRQ state temporarily.

[...]

Applied to

https://git.kernel.org/pub/scm/linux/kernel/git/broonie/regmap.git for-next

Thanks!

[1/1] regmap: Fix race condition in hwspinlock irqsave routine

      commit: 4b58aac989c1e3fafb1c68a733811859df388250

All being well this means that it will be integrated into the linux-next tree (usually sometime in the next 24 hours) and sent to Linus during the next merge window (or sooner if it is a bug fix), however if problems are discovered then the patch may be dropped or reverted.

You may get further e-mails resulting from automated or manual testing and review of the tree, please engage with people reporting problems and send followup patches addressing any issues that are reported if needed.

If any updates are required or you are submitting further changes they should be sent as incremental updates against current git, existing patches will not be replaced.

Please add any relevant lists and maintainers to the CCs when replying to this mail.

Thanks,
Mark