Compare
ffbfd3f98f4393e91dffce3a46ccb7cf5329cf2f..abff0a48b110fe52d43999b4cb7c793a510a6bf1
regmap: Drop map lock during cache init and exit
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.
Add Maple-state irqsave helpers and use them during teardown to preserve
the lock's IRQ-safe lockdep state.
Fixes: fd4ebc07b4df ("regmap: Hold the regmap lock when allocating and freeing the cache")
Suggested-by: Mark Brown <broonie@kernel.org>
Suggested-by: Liam R. Howlett <liam@infradead.org>
Signed-off-by: Michael Reeves <michael.reeves077@gmail.com>
Reviewed-by: Peng Fan <peng.fan@nxp.com>
Link: https://patch.msgid.link/20261006-regcache-lifecycle-submit-v3-2-37bdd82fd47c@gmail.com
Signed-off-by: Mark Brown <broonie@kernel.org>
Michael Reeves <michael.reeves077@gmail.com> · 2026-10-06 07:03 UTC · committed by Mark Brown
6 files changed,
+22
−18
- drivers/base/regmap/regcache-maple.c
- drivers/base/regmap/regcache.c
- drivers/base/regmap/regmap-debugfs.c
- drivers/base/regmap/regmap.c
- drivers/gpu/drm/xe/xe_bo.c
- include/linux/maple_tree.h
modified drivers/base/regmap/regcache-maple.c
| @@ -312,16 +312,21 @@ static void regcache_maple_exit(struct regmap *map) | |||
| 312 | 312 | struct maple_tree *mt = map->cache; | |
| 313 | 313 | MA_STATE(mas, mt, 0, UINT_MAX); | |
| 314 | 314 | unsigned int *entry; | |
| 315 | + | unsigned long flags; | |
| 315 | 316 | ||
| 316 | 317 | /* if we've already been called then just return */ | |
| 317 | 318 | if (!mt) | |
| 318 | 319 | return; | |
| 319 | 320 | ||
| 320 | - | mas_lock(&mas); | |
| 321 | + | /* | |
| 322 | + | * Register access may take the Maple lock from hard IRQ context, so | |
| 323 | + | * preserve its IRQ-safe lockdep state during teardown. | |
| 324 | + | */ | |
| 325 | + | mas_lock_irqsave(&mas, flags); | |
| 321 | 326 | mas_for_each(&mas, entry, UINT_MAX) | |
| 322 | 327 | kfree(entry); | |
| 323 | 328 | __mt_destroy(mt); | |
| 324 | - | mas_unlock(&mas); | |
| 329 | + | mas_unlock_irqrestore(&mas, flags); | |
| 325 | 330 | ||
| 326 | 331 | kfree(mt); | |
| 327 | 332 | map->cache = NULL; | |
modified drivers/base/regmap/regcache.c
| @@ -225,9 +225,7 @@ int regcache_init(struct regmap *map, const struct regmap_config *config) | |||
| 225 | 225 | if (map->cache_ops->init) { | |
| 226 | 226 | dev_dbg(map->dev, "Initializing %s cache\n", | |
| 227 | 227 | map->cache_ops->name); | |
| 228 | - | map->lock(map->lock_arg); | |
| 229 | 228 | ret = map->cache_ops->init(map); | |
| 230 | - | map->unlock(map->lock_arg); | |
| 231 | 229 | if (ret) | |
| 232 | 230 | goto err_free_reg_defaults; | |
| 233 | 231 | } | |
| @@ -259,9 +257,7 @@ err_free: | |||
| 259 | 257 | err_exit: | |
| 260 | 258 | if (map->cache_ops->exit) { | |
| 261 | 259 | dev_dbg(map->dev, "Destroying %s cache\n", map->cache_ops->name); | |
| 262 | - | map->lock(map->lock_arg); | |
| 263 | 260 | map->cache_ops->exit(map); | |
| 264 | - | map->unlock(map->lock_arg); | |
| 265 | 261 | } | |
| 266 | 262 | err_free_reg_defaults: | |
| 267 | 263 | kfree(map->reg_defaults); | |
| @@ -281,9 +277,7 @@ void regcache_exit(struct regmap *map) | |||
| 281 | 277 | if (map->cache_ops->exit) { | |
| 282 | 278 | dev_dbg(map->dev, "Destroying %s cache\n", | |
| 283 | 279 | map->cache_ops->name); | |
| 284 | - | map->lock(map->lock_arg); | |
| 285 | 280 | map->cache_ops->exit(map); | |
| 286 | - | map->unlock(map->lock_arg); | |
| 287 | 281 | } | |
| 288 | 282 | ||
| 289 | 283 | kfree(map->reg_defaults); | |
modified drivers/base/regmap/regmap-debugfs.c
| @@ -664,6 +664,7 @@ void regmap_debugfs_exit(struct regmap *map) | |||
| 664 | 664 | { | |
| 665 | 665 | if (map->debugfs) { | |
| 666 | 666 | debugfs_remove_recursive(map->debugfs); | |
| 667 | + | map->debugfs = NULL; | |
| 667 | 668 | mutex_lock(&map->cache_lock); | |
| 668 | 669 | regmap_debugfs_free_dump_cache(map); | |
| 669 | 670 | mutex_unlock(&map->cache_lock); | |
modified drivers/base/regmap/regmap.c
| @@ -1422,8 +1422,8 @@ int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config) | |||
| 1422 | 1422 | { | |
| 1423 | 1423 | int ret; | |
| 1424 | 1424 | ||
| 1425 | - | regcache_exit(map); | |
| 1426 | 1425 | regmap_debugfs_exit(map); | |
| 1426 | + | regcache_exit(map); | |
| 1427 | 1427 | ||
| 1428 | 1428 | map->max_register = config->max_register; | |
| 1429 | 1429 | 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) | |||
| 1440 | 1440 | if (ret) | |
| 1441 | 1441 | return ret; | |
| 1442 | 1442 | ||
| 1443 | - | regmap_debugfs_init(map); | |
| 1444 | - | ||
| 1445 | 1443 | map->cache_bypass = false; | |
| 1446 | 1444 | map->cache_only = false; | |
| 1447 | 1445 | ||
| 1448 | - | return regcache_init(map, config); | |
| 1446 | + | ret = regcache_init(map, config); | |
| 1447 | + | if (ret) | |
| 1448 | + | return ret; | |
| 1449 | + | ||
| 1450 | + | regmap_debugfs_init(map); | |
| 1451 | + | ||
| 1452 | + | return 0; | |
| 1449 | 1453 | } | |
| 1450 | 1454 | EXPORT_SYMBOL_GPL(regmap_reinit_cache); | |
| 1451 | 1455 | ||
| @@ -1459,9 +1463,8 @@ void regmap_exit(struct regmap *map) | |||
| 1459 | 1463 | struct regmap_async *async; | |
| 1460 | 1464 | ||
| 1461 | 1465 | regmap_detach_dev(map->dev, map); | |
| 1462 | - | regcache_exit(map); | |
| 1463 | - | ||
| 1464 | 1466 | regmap_debugfs_exit(map); | |
| 1467 | + | regcache_exit(map); | |
| 1465 | 1468 | regmap_range_exit(map); | |
| 1466 | 1469 | if (map->bus && map->bus->free_context) | |
| 1467 | 1470 | map->bus->free_context(map->bus_context); | |
modified drivers/gpu/drm/xe/xe_bo.c
| @@ -944,8 +944,6 @@ static int xe_bo_move_notify(struct xe_bo *bo, | |||
| 944 | 944 | * | |
| 945 | 945 | * Transfers pages between shrinkable and purgeable buckets when the BO | |
| 946 | 946 | * purgeable state changes. Called automatically from xe_bo_set_purgeable_state(). | |
| 947 | - | * For pinned BOs, whose pages are not accounted, only the purgeable flag is | |
| 948 | - | * updated, so that the correct bucket is used when the BO is unpinned. | |
| 949 | 947 | */ | |
| 950 | 948 | static void xe_bo_set_purgeable_shrinker(struct xe_bo *bo, | |
| 951 | 949 | enum xe_madv_purgeable_state new_state) | |
| @@ -962,8 +960,7 @@ static void xe_bo_set_purgeable_shrinker(struct xe_bo *bo, | |||
| 962 | 960 | return; | |
| 963 | 961 | ||
| 964 | 962 | xe_tt = container_of(tt, struct xe_ttm_tt, ttm); | |
| 965 | - | /* Pinned bos are not accounted. Only update the purgeable flag. */ | |
| 966 | - | tt_pages = xe_bo_is_pinned(bo) ? 0 : tt->num_pages; | |
| 963 | + | tt_pages = tt->num_pages; | |
| 967 | 964 | ||
| 968 | 965 | if (!xe_tt->purgeable && new_state == XE_MADV_PURGEABLE_DONTNEED) { | |
| 969 | 966 | xe_tt->purgeable = true; | |
modified include/linux/maple_tree.h
| @@ -507,9 +507,13 @@ struct ma_wr_state { | |||
| 507 | 507 | }; | |
| 508 | 508 | ||
| 509 | 509 | #define mas_lock(mas) spin_lock(&((mas)->tree->ma_lock)) | |
| 510 | + | #define mas_lock_irqsave(mas, flags) \ | |
| 511 | + | spin_lock_irqsave(&((mas)->tree->ma_lock), flags) | |
| 510 | 512 | #define mas_lock_nested(mas, subclass) \ | |
| 511 | 513 | spin_lock_nested(&((mas)->tree->ma_lock), subclass) | |
| 512 | 514 | #define mas_unlock(mas) spin_unlock(&((mas)->tree->ma_lock)) | |
| 515 | + | #define mas_unlock_irqrestore(mas, flags) \ | |
| 516 | + | spin_unlock_irqrestore(&((mas)->tree->ma_lock), flags) | |
| 513 | 517 | ||
| 514 | 518 | /* | |
| 515 | 519 | * Special values for ma_state.node. | |