Skip to contentSkip to conversation

regcache: Warn if regcache_sync() is called in cache_only mode

PATCH Superseded [email protected] 2 participants 5 messages Jul 15 – Jul 20

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 regcache: Warn if regcache_sync() is called in cache_only mode

From: bui duc phuc <[email protected]>

Calling regcache_sync() while cache_only is enabled is invalid API usage, since writes are intentionally kept in the cache and cannot be synchronized to hardware.
Document that callers must disable cache_only before calling regcache_sync(), and reject incorrect usage with a WARN_ON() and -EINVAL.

---
 drivers/base/regmap/regcache.c | 7 +++++++
 1 file changed, 7 insertions(+)
drivers/base/regmap/regcache.c +7 -0
index 96cdae25b9c4..d786cc8bf4d8 100644
@@ -397,6 +397,10 @@ static int rbtree_all(const void *key, const struct rb_node *node)
  * volatile.  In general drivers can choose not to use the provided
  * syncing functionality if they so require.
  *
+ * This pushes cached changes made while cache_only (e.g. suspend) down
+ * to hardware. The caller must clear cache_only first, or writes will
+ * only update the cache.
+ *
  * Return a negative value on failure, 0 on success.
  */
 int regcache_sync(struct regmap *map)
@@ -411,6 +415,9 @@ int regcache_sync(struct regmap *map)
 	if (WARN_ON(map->cache_type == REGCACHE_NONE))
 		return -EINVAL;
 
+	if (WARN_ON(map->cache_only))
+		return -EINVAL;
+
 	BUG_ON(!map->cache_ops);
 
 	map->lock(map->lock_arg);
Signature · 1 line
2.43.0
maintainer
#1 lore raw

drivers/base/regmap/regcache.c int regcache_sync(struct regmap *map) · line 423

Quoted 10 lines · On Wed, Jul 15, 2026 at 12:21:00PM +0700, [email protected] wrote:
drivers/base/regmap/regcache.c +3 -0
@@ -411,6 +415,9 @@ int regcache_sync(struct regmap *map)
 	if (WARN_ON(map->cache_type == REGCACHE_NONE))
 		return -EINVAL;
 
+	if (WARN_ON(map->cache_only))
+		return -EINVAL;
+
 	BUG_ON(!map->cache_ops);
 
 	map->lock(map->lock_arg);

The check needs to be done under lock otherwise some other thread could come in and put the regmap into cache only mode between the check and when we take the lock.

We also need a similar check in regcache_sync_region().

#2 lore raw

Hi Mark,

Thank you for your review.

Quoted 6 lines

The check needs to be done under lock otherwise some other thread could come in and put the regmap into cache only mode between the check and when we take the lock.

We also need a similar check in regcache_sync_region().

I'll move the check under the lock for v2, and add the same check to regcache_sync_region() as well.

On top of that, I'd like to ask about a design question:
Should regcache_sync() set map->cache_only = true again if it fails partway through,
or should we leave it to the caller to call regcache_cache_only() themselves?

If we leave it to the caller, should we also update the function's documentation
to make clear that after a failed sync, the state of the registers is no longer guaranteed to match the cache, and that further register access should be done carefully , and that calling regcache_cache_only() is recommended to keep the device in a safe/consistent state?

Best regards,
Phuc

maintainer
#3 lore raw
Quoted 4 lines · Bui Duc Phuc

On top of that, I'd like to ask about a design question:
Should regcache_sync() set map->cache_only = true again if it fails partway through,
or should we leave it to the caller to call regcache_cache_only() themselves?

That's for the caller.

Quoted 6 lines

If we leave it to the caller, should we also update the function's documentation
to make clear that after a failed sync, the state of the registers is no longer guaranteed to match the cache, and that further register access should be done carefully , and that calling regcache_cache_only() is recommended to keep the device in a safe/consistent state?

I'd have hoped it'd be fairly obvious that the device is in an indeterminate state after a failed sync, TBH if I/O isn't working recovery options are going to be *super* limited.

#4 lore raw

Hi Mark,

Thanks for the clarification.

Quoted 3 lines

That's for the caller.

Understood. I'll keep the current behavior and leave the error handling to the caller.

Quoted 3 lines

I'd have hoped it'd be fairly obvious that the device is in an indeterminate state after a failed sync, TBH if I/O isn't working recovery options are going to be *super* limited.

I won't make any documentation changes.

Best regards,
Phuc