mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v2 0/2] regmap: Fix cache lifecycle locking and teardown
@ 2026-10-04 12:38 Michael Reeves via B4 Relay
  2026-10-04 12:38 ` [PATCH v2 1/2] regmap: Remove debugfs before cache teardown Michael Reeves via B4 Relay
  2026-10-04 12:38 ` [PATCH v2 2/2] regmap: Drop map lock during cache init and exit Michael Reeves via B4 Relay
  0 siblings, 2 replies; 5+ messages in thread
From: Michael Reeves via B4 Relay @ 2026-10-04 12:38 UTC (permalink / raw)
  To: Mark Brown
  Cc: Greg Kroah-Hartman, Rafael J. Wysocki, Danilo Krummrich,
	Liam R. Howlett, linux-kernel, driver-core, maple-tree, asahi,
	Michael Reeves

Fix an "Invalid wait context" lockdep warning during Apple GPIO probe and
correct debugfs ordering during cache teardown.

Tested on Apple M3 MacBook Air (T8122):

- All 4 GPIO controllers probe successfully with lockdep enabled.
- The "Invalid wait context" warning no longer occurs.
- The regmap KUnit suite passes and lockdep remains enabled.

---
Changes in v2:
- Link to v1: https://lore.kernel.org/r/20260925-regcache-lifecycle-submit-v1-1-83b75ca79d96@gmail.com
- Make Maple cache teardown IRQ-safe.
- Fix debugfs ordering during cache teardown and reinitialization.
- Rebase onto the latest upstream tree.

---
Michael Reeves (2):
      regmap: Remove debugfs before cache teardown
      regmap: Drop map lock during cache init and exit

 drivers/base/regmap/regcache-maple.c |  5 +++--
 drivers/base/regmap/regcache.c       |  6 ------
 drivers/base/regmap/regmap-debugfs.c |  1 +
 drivers/base/regmap/regmap.c         | 15 +++++++++------
 4 files changed, 13 insertions(+), 14 deletions(-)
---
base-commit: 6addb4f385570ebc11c4eb499a4f1c149f313e84
change-id: 20260925-regcache-lifecycle-submit-74f366be0fbc

Best regards,
-- 
Michael Reeves <michael.reeves077@gmail.com>



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

* [PATCH v2 1/2] regmap: Remove debugfs before cache teardown
  2026-10-04 12:38 [PATCH v2 0/2] regmap: Fix cache lifecycle locking and teardown Michael Reeves via B4 Relay
@ 2026-10-04 12:38 ` Michael Reeves via B4 Relay
  2026-10-04 12:38 ` [PATCH v2 2/2] regmap: Drop map lock during cache init and exit Michael Reeves via B4 Relay
  1 sibling, 0 replies; 5+ messages in thread
From: Michael Reeves via B4 Relay @ 2026-10-04 12:38 UTC (permalink / raw)
  To: Mark Brown
  Cc: Greg Kroah-Hartman, Rafael J. Wysocki, Danilo Krummrich,
	Liam R. Howlett, linux-kernel, driver-core, maple-tree, asahi,
	Michael Reeves

From: Michael Reeves <michael.reeves077@gmail.com>

regmap_exit() and regmap_reinit_cache() destroy the register cache before
removing debugfs. A concurrent debugfs cache operation can therefore
access cache state after the backend exit callback starts freeing it.

Remove debugfs before cache teardown, and clear the debugfs pointer after
removal. During cache reinitialization, recreate debugfs only after the
new cache initializes successfully.

Signed-off-by: Michael Reeves <michael.reeves077@gmail.com>
---
 drivers/base/regmap/regmap-debugfs.c |  1 +
 drivers/base/regmap/regmap.c         | 15 +++++++++------
 2 files changed, 10 insertions(+), 6 deletions(-)

diff --git a/drivers/base/regmap/regmap-debugfs.c b/drivers/base/regmap/regmap-debugfs.c
index 18f1c60749..9a09df2ba8 100644
--- a/drivers/base/regmap/regmap-debugfs.c
+++ b/drivers/base/regmap/regmap-debugfs.c
@@ -664,6 +664,7 @@ void regmap_debugfs_exit(struct regmap *map)
 {
 	if (map->debugfs) {
 		debugfs_remove_recursive(map->debugfs);
+		map->debugfs = NULL;
 		mutex_lock(&map->cache_lock);
 		regmap_debugfs_free_dump_cache(map);
 		mutex_unlock(&map->cache_lock);
diff --git a/drivers/base/regmap/regmap.c b/drivers/base/regmap/regmap.c
index e6e022b026..8e50a05ff0 100644
--- a/drivers/base/regmap/regmap.c
+++ b/drivers/base/regmap/regmap.c
@@ -1422,8 +1422,8 @@ int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config)
 {
 	int ret;
 
-	regcache_exit(map);
 	regmap_debugfs_exit(map);
+	regcache_exit(map);
 
 	map->max_register = config->max_register;
 	map->max_register_is_set = map->max_register ?: config->max_register_is_0;
@@ -1440,12 +1440,16 @@ int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config)
 	if (ret)
 		return ret;
 
-	regmap_debugfs_init(map);
-
 	map->cache_bypass = false;
 	map->cache_only = false;
 
-	return regcache_init(map, config);
+	ret = regcache_init(map, config);
+	if (ret)
+		return ret;
+
+	regmap_debugfs_init(map);
+
+	return 0;
 }
 EXPORT_SYMBOL_GPL(regmap_reinit_cache);
 
@@ -1459,9 +1463,8 @@ void regmap_exit(struct regmap *map)
 	struct regmap_async *async;
 
 	regmap_detach_dev(map->dev, map);
-	regcache_exit(map);
-
 	regmap_debugfs_exit(map);
+	regcache_exit(map);
 	regmap_range_exit(map);
 	if (map->bus && map->bus->free_context)
 		map->bus->free_context(map->bus_context);

-- 
2.55.0



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

* [PATCH v2 2/2] regmap: Drop map lock during cache init and exit
  2026-10-04 12:38 [PATCH v2 0/2] regmap: Fix cache lifecycle locking and teardown Michael Reeves via B4 Relay
  2026-10-04 12:38 ` [PATCH v2 1/2] regmap: Remove debugfs before cache teardown Michael Reeves via B4 Relay
@ 2026-10-04 12:38 ` Michael Reeves via B4 Relay
  2026-10-05 12:03   ` Mark Brown
  1 sibling, 1 reply; 5+ messages in thread
From: Michael Reeves via B4 Relay @ 2026-10-04 12:38 UTC (permalink / raw)
  To: Mark Brown
  Cc: Greg Kroah-Hartman, Rafael J. Wysocki, Danilo Krummrich,
	Liam R. Howlett, linux-kernel, driver-core, maple-tree, asahi,
	Michael Reeves

From: Michael Reeves <michael.reeves077@gmail.com>

On systems with CONFIG_PROVE_RAW_LOCK_NESTING enabled, Apple GPIO probe
triggers an "Invalid wait context" lockdep warning.

The driver uses a regmap with a raw spinlock and REGCACHE_FLAT. During
regcache_init(), regcache_flat_init() allocates memory while holding the
raw spinlock. The memory allocator can acquire a non-raw spinlock, which
causes an invalid raw-to-non-raw lock nesting error. The abbreviated call
chain is:

  apple_gpio_pinctrl_probe
    regcache_init
      regcache_flat_init
        __kmalloc_noprof
          __pcs_replace_empty_main
            refill_objects
              _raw_spin_lock_irqsave

Cache allocation occurs before the regmap is published, cache destruction
occurs after users have stopped accessing it, and regmap_reinit_cache()
requires callers to prevent concurrent access. Remove the outer map lock
from cache initialization and destruction. Keep it for cache population
and normal register access.

The Maple cache still uses its internal spinlock during teardown. That
lock can also be acquired from hard IRQ context during register access, so
use spin_lock_irqsave() during teardown to preserve its IRQ-safe lockdep
state.

Fixes: fd4ebc07b4df ("regmap: Hold the regmap lock when allocating and freeing the cache")
Signed-off-by: Michael Reeves <michael.reeves077@gmail.com>
---
 drivers/base/regmap/regcache-maple.c | 5 +++--
 drivers/base/regmap/regcache.c       | 6 ------
 2 files changed, 3 insertions(+), 8 deletions(-)

diff --git a/drivers/base/regmap/regcache-maple.c b/drivers/base/regmap/regcache-maple.c
index e46fe5c32b..9419dc1be9 100644
--- a/drivers/base/regmap/regcache-maple.c
+++ b/drivers/base/regmap/regcache-maple.c
@@ -312,16 +312,17 @@ static void regcache_maple_exit(struct regmap *map)
 	struct maple_tree *mt = map->cache;
 	MA_STATE(mas, mt, 0, UINT_MAX);
 	unsigned int *entry;
+	unsigned long flags;
 
 	/* if we've already been called then just return */
 	if (!mt)
 		return;
 
-	mas_lock(&mas);
+	spin_lock_irqsave(&mt->ma_lock, flags);
 	mas_for_each(&mas, entry, UINT_MAX)
 		kfree(entry);
 	__mt_destroy(mt);
-	mas_unlock(&mas);
+	spin_unlock_irqrestore(&mt->ma_lock, flags);
 
 	kfree(mt);
 	map->cache = NULL;
diff --git a/drivers/base/regmap/regcache.c b/drivers/base/regmap/regcache.c
index 0d58d900a2..b137ded7fd 100644
--- a/drivers/base/regmap/regcache.c
+++ b/drivers/base/regmap/regcache.c
@@ -225,9 +225,7 @@ int regcache_init(struct regmap *map, const struct regmap_config *config)
 	if (map->cache_ops->init) {
 		dev_dbg(map->dev, "Initializing %s cache\n",
 			map->cache_ops->name);
-		map->lock(map->lock_arg);
 		ret = map->cache_ops->init(map);
-		map->unlock(map->lock_arg);
 		if (ret)
 			goto err_free_reg_defaults;
 	}
@@ -259,9 +257,7 @@ int regcache_init(struct regmap *map, const struct regmap_config *config)
 err_exit:
 	if (map->cache_ops->exit) {
 		dev_dbg(map->dev, "Destroying %s cache\n", map->cache_ops->name);
-		map->lock(map->lock_arg);
 		map->cache_ops->exit(map);
-		map->unlock(map->lock_arg);
 	}
 err_free_reg_defaults:
 	kfree(map->reg_defaults);
@@ -281,9 +277,7 @@ void regcache_exit(struct regmap *map)
 	if (map->cache_ops->exit) {
 		dev_dbg(map->dev, "Destroying %s cache\n",
 			map->cache_ops->name);
-		map->lock(map->lock_arg);
 		map->cache_ops->exit(map);
-		map->unlock(map->lock_arg);
 	}
 
 	kfree(map->reg_defaults);

-- 
2.55.0



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

* Re: [PATCH v2 2/2] regmap: Drop map lock during cache init and exit
  2026-10-04 12:38 ` [PATCH v2 2/2] regmap: Drop map lock during cache init and exit Michael Reeves via B4 Relay
@ 2026-10-05 12:03   ` Mark Brown
  2026-10-05 15:49     ` Liam R. Howlett
  0 siblings, 1 reply; 5+ messages in thread
From: Mark Brown @ 2026-10-05 12:03 UTC (permalink / raw)
  To: michael.reeves077
  Cc: Greg Kroah-Hartman, Rafael J. Wysocki, Danilo Krummrich,
	Liam R. Howlett, linux-kernel, driver-core, maple-tree, asahi

[-- Attachment #1: Type: text/plain, Size: 590 bytes --]

On Sun, Oct 04, 2026 at 11:38:43PM +1100, Michael Reeves via B4 Relay wrote:
> From: Michael Reeves <michael.reeves077@gmail.com>
> 
> On systems with CONFIG_PROVE_RAW_LOCK_NESTING enabled, Apple GPIO probe
> triggers an "Invalid wait context" lockdep warning.

> -	mas_lock(&mas);
> +	spin_lock_irqsave(&mt->ma_lock, flags);

I think this needs at least a comment explaining why we're not using
mas_lock(), otherwise people will try to change to using mas_lock()
since there's no obvious reason we need to exclude interrupts and we're
peering inside the maple tree internals.

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 228 bytes --]

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

* Re: [PATCH v2 2/2] regmap: Drop map lock during cache init and exit
  2026-10-05 12:03   ` Mark Brown
@ 2026-10-05 15:49     ` Liam R. Howlett
  0 siblings, 0 replies; 5+ messages in thread
From: Liam R. Howlett @ 2026-10-05 15:49 UTC (permalink / raw)
  To: Mark Brown
  Cc: michael.reeves077, Greg Kroah-Hartman, Rafael J. Wysocki,
	Danilo Krummrich, linux-kernel, driver-core, maple-tree, asahi

On 26/10/05 02:03PM, Mark Brown wrote:
> On Sun, Oct 04, 2026 at 11:38:43PM +1100, Michael Reeves via B4 Relay wrote:
> > From: Michael Reeves <michael.reeves077@gmail.com>
> > 
> > On systems with CONFIG_PROVE_RAW_LOCK_NESTING enabled, Apple GPIO probe
> > triggers an "Invalid wait context" lockdep warning.
> 
> > -	mas_lock(&mas);
> > +	spin_lock_irqsave(&mt->ma_lock, flags);
> 
> I think this needs at least a comment explaining why we're not using
> mas_lock(), otherwise people will try to change to using mas_lock()
> since there's no obvious reason we need to exclude interrupts and we're
> peering inside the maple tree internals.

An irqsave mas_lock() should be added to the maple_tree.h for others to
use.  We probably still need the comment Mark suggested as well.

Michael, can you please add the irqsave variant to the header and Cc me
on the change?

Thanks,
Liam



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

end of thread, other threads:[~2026-10-05 15:49 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-10-04 12:38 [PATCH v2 0/2] regmap: Fix cache lifecycle locking and teardown Michael Reeves via B4 Relay
2026-10-04 12:38 ` [PATCH v2 1/2] regmap: Remove debugfs before cache teardown Michael Reeves via B4 Relay
2026-10-04 12:38 ` [PATCH v2 2/2] regmap: Drop map lock during cache init and exit Michael Reeves via B4 Relay
2026-10-05 12:03   ` Mark Brown
2026-10-05 15:49     ` Liam R. Howlett

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®