regmap: regcache: Fixup locking for custom lock callbacks
Subsystems (5)
PATCH 1/2 regmap: regcache: Fixup locking for custom lock callbacks
The parameter passed to the regmap lock/unlock callbacks needs to be map->lock_arg, regcache passes just map. This works fine in the case that no custom locking callbacks are used, since in this case map->lock_arg equals map, but will break when custom locking callbacks are used. The issue was introduced in commit 0d4529c5 ("regmap: make lock/unlock functions customizable") and is fixed by this patch.
--- drivers/base/regmap/regcache-rbtree.c | 4 ++-- drivers/base/regmap/regcache.c | 24 ++++++++++++------------ 2 files changed, 14 insertions(+), 14 deletions(-)
drivers/base/regmap/regcache-rbtree.c +2 -2
@@ -143,7 +143,7 @@ static int rbtree_show(struct seq_file *s, void *ignored)int registers =0 ;int this_registers, average; - map->lock (map); + map->lock (map->lock_arg); mem_size =sizeof (*rbtree_ctx); mem_size +=BITS_TO_LONGS (map->cache_present_nbits) *sizeof (long );
@@ -170,7 +170,7 @@ static int rbtree_show(struct seq_file *s, void *ignored)seq_printf (s,"%d nodes, %d registers, average %d registers, used %zu bytes\n" , nodes, registers, average, mem_size); - map->unlock (map); + map->unlock (map->lock_arg);return 0 ; }
drivers/base/regmap/regcache.c +12 -12
@@ -270,7 +270,7 @@ int regcache_sync(struct regmap *map)BUG_ON (!map->cache_ops || !map->cache_ops->sync); - map->lock (map); + map->lock (map->lock_arg);/* Remember the initial bypass state */ bypass = map->cache_bypass; dev_dbg(map->dev, "Syncing %s cache\n",
@@ -306,7 +306,7 @@ out:trace_regcache_sync (map->dev, name,"stop" );/* Restore the bypass state */ map->cache_bypass = bypass; - map->unlock (map); + map->unlock (map->lock_arg);return ret; }
@@ -333,7 +333,7 @@ int regcache_sync_region(struct regmap *map, unsigned int min,BUG_ON (!map->cache_ops || !map->cache_ops->sync); - map->lock (map); + map->lock (map->lock_arg);/* Remember the initial bypass state */ bypass = map->cache_bypass;
@@ -352,7 +352,7 @@ out:trace_regcache_sync (map->dev, name,"stop region" );/* Restore the bypass state */ map->cache_bypass = bypass; - map->unlock (map); + map->unlock (map->lock_arg);return ret; }
@@ -378,7 +378,7 @@ int regcache_drop_region(struct regmap *map, unsigned int min,if (!map->cache_present && !(map->cache_ops && map->cache_ops->drop))return -EINVAL ; - map->lock (map); + map->lock (map->lock_arg);trace_regcache_drop_region (map->dev, min, max);
@@ -389,7 +389,7 @@ int regcache_drop_region(struct regmap *map, unsigned int min,if (map->cache_ops && map->cache_ops->drop) ret = map->cache_ops->drop (map, min, max); - map->unlock (map); + map->unlock (map->lock_arg);return ret; }
@@ -409,11 +409,11 @@ EXPORT_SYMBOL_GPL(regcache_drop_region);*/ void regcache_cache_only (struct regmap *map,bool enable) { - map->lock (map); + map->lock (map->lock_arg);WARN_ON (map->cache_bypass && enable); map->cache_only = enable;trace_regmap_cache_only (map->dev, enable); - map->unlock (map); + map->unlock (map->lock_arg); }EXPORT_SYMBOL_GPL (regcache_cache_only);
@@ -428,9 +428,9 @@ EXPORT_SYMBOL_GPL(regcache_cache_only);*/ void regcache_mark_dirty (struct regmap *map) { - map->lock (map); + map->lock (map->lock_arg); map->cache_dirty = true; - map->unlock (map); + map->unlock (map->lock_arg); }EXPORT_SYMBOL_GPL (regcache_mark_dirty);
@@ -447,11 +447,11 @@ EXPORT_SYMBOL_GPL(regcache_mark_dirty);*/ void regcache_cache_bypass (struct regmap *map,bool enable) { - map->lock (map); + map->lock (map->lock_arg);WARN_ON (map->cache_only && enable); map->cache_bypass = enable;trace_regmap_cache_bypass (map->dev, enable); - map->unlock (map); + map->unlock (map->lock_arg); }EXPORT_SYMBOL_GPL (regcache_cache_bypass);
Signature · 1 line
1.8.0
PATCH 2/2 regmap: Make regmap-mmio usable from different contexts
regmap-mmio uses a spinlock with spin_lock() and spin_unlock() for locking. Which means in order to avoid race conditions the lock always needs to be taken from the same context. In practice at least one of the calls to the regmap API happens from sleepable context, which means all calls to the regmap API have to be made from sleepable context. This not only prevents us from using regmap-mmio from interrupt context but also from other sections where IRQs are disabled. E.g. if interrupts are disabled because a interrupt safe spinlock is locked. Not being able to use regmap from different contexts limits the number of usecases for regmap-mmio quite a bit.
This patch updates the adds a flags parameter to the regmap lock and unlock callbacks and uses spin_lock_irqsave() and spin_unlock_restore() for the mmio case. This allows us to use regmap-mmio from different contexts.
For the non-mmio case where regmap uses a mutex for locking the flags parameter will be ignored and it is still only possible to use regmap API from sleepable context.
---
E.g. the Tegra ASoC drivers should suffer from this since the ALSA trigger callback is called with interrupts off.
--- drivers/base/regmap/regcache-rbtree.c | 5 +-- drivers/base/regmap/regcache.c | 33 ++++++++++++-------- drivers/base/regmap/regmap.c | 57 ++++++++++++++++++++--------------- drivers/mfd/sta2x11-mfd.c | 6 ++-- drivers/staging/imx-drm/imx-tve.c | 4 +-- include/linux/regmap.h | 4 +-- 6 files changed, 64 insertions(+), 45 deletions(-)
drivers/base/regmap/regcache-rbtree.c +3 -2
@@ -142,8 +142,9 @@ static int rbtree_show(struct seq_file *s, void *ignored)int nodes =0 ;int registers =0 ;int this_registers, average; +unsigned long flags; - map->lock (map->lock_arg); + map->lock (map->lock_arg, &flags); mem_size =sizeof (*rbtree_ctx); mem_size +=BITS_TO_LONGS (map->cache_present_nbits) *sizeof (long );
@@ -170,7 +171,7 @@ static int rbtree_show(struct seq_file *s, void *ignored)seq_printf (s,"%d nodes, %d registers, average %d registers, used %zu bytes\n" , nodes, registers, average, mem_size); - map->unlock (map->lock_arg); + map->unlock (map->lock_arg, &flags);return 0 ; }
drivers/base/regmap/regcache.c +21 -12
@@ -267,10 +267,11 @@ int regcache_sync(struct regmap *map)unsigned int i;const char *name;unsigned int bypass; +unsigned long flags;BUG_ON (!map->cache_ops || !map->cache_ops->sync); - map->lock (map->lock_arg); + map->lock (map->lock_arg, &flags);/* Remember the initial bypass state */ bypass = map->cache_bypass; dev_dbg(map->dev, "Syncing %s cache\n",
@@ -306,7 +307,7 @@ out:trace_regcache_sync (map->dev, name,"stop" );/* Restore the bypass state */ map->cache_bypass = bypass; - map->unlock (map->lock_arg); + map->unlock (map->lock_arg, &flags);return ret; }
@@ -330,10 +331,11 @@ int regcache_sync_region(struct regmap *map, unsigned int min,int ret =0 ;const char *name;unsigned int bypass; +unsigned long flags;BUG_ON (!map->cache_ops || !map->cache_ops->sync); - map->lock (map->lock_arg); + map->lock (map->lock_arg, &flags);/* Remember the initial bypass state */ bypass = map->cache_bypass;
@@ -352,7 +354,7 @@ out:trace_regcache_sync (map->dev, name,"stop region" );/* Restore the bypass state */ map->cache_bypass = bypass; - map->unlock (map->lock_arg); + map->unlock (map->lock_arg, &flags);return ret; }
@@ -372,13 +374,14 @@ EXPORT_SYMBOL_GPL(regcache_sync_region);int regcache_drop_region (struct regmap *map,unsigned int min,unsigned int max) { +unsigned long flags;unsigned int reg;int ret =0 ;if (!map->cache_present && !(map->cache_ops && map->cache_ops->drop))return -EINVAL ; - map->lock (map->lock_arg); + map->lock (map->lock_arg, &flags);trace_regcache_drop_region (map->dev, min, max);
@@ -389,7 +392,7 @@ int regcache_drop_region(struct regmap *map, unsigned int min,if (map->cache_ops && map->cache_ops->drop) ret = map->cache_ops->drop (map, min, max); - map->unlock (map->lock_arg); + map->unlock (map->lock_arg, &flags);return ret; }
@@ -409,11 +412,13 @@ EXPORT_SYMBOL_GPL(regcache_drop_region);*/ void regcache_cache_only (struct regmap *map,bool enable) { - map->lock (map->lock_arg); +unsigned long flags; + + map->lock (map->lock_arg, &flags);WARN_ON (map->cache_bypass && enable); map->cache_only = enable;trace_regmap_cache_only (map->dev, enable); - map->unlock (map->lock_arg); + map->unlock (map->lock_arg, &flags); }EXPORT_SYMBOL_GPL (regcache_cache_only);
@@ -428,9 +433,11 @@ EXPORT_SYMBOL_GPL(regcache_cache_only);*/ void regcache_mark_dirty (struct regmap *map) { - map->lock (map->lock_arg); +unsigned long flags; + + map->lock (map->lock_arg, &flags); map->cache_dirty = true; - map->unlock (map->lock_arg); + map->unlock (map->lock_arg, &flags); }EXPORT_SYMBOL_GPL (regcache_mark_dirty);
@@ -447,11 +454,13 @@ EXPORT_SYMBOL_GPL(regcache_mark_dirty);*/ void regcache_cache_bypass (struct regmap *map,bool enable) { - map->lock (map->lock_arg); +unsigned long flags; + + map->lock (map->lock_arg, &flags);WARN_ON (map->cache_only && enable); map->cache_bypass = enable;trace_regmap_cache_bypass (map->dev, enable); - map->unlock (map->lock_arg); + map->unlock (map->lock_arg, &flags); }EXPORT_SYMBOL_GPL (regcache_cache_bypass);
drivers/base/regmap/regmap.c +33 -24
210 lines not on this page · raw message
drivers/mfd/sta2x11-mfd.c +3 -3
@@ -154,20 +154,20 @@ EXPORT_SYMBOL(sta2x11_mfd_get_regs_data);* Special sta2x11-mfd regmap lock/unlock functions */ -static void sta2x11_regmap_lock (void *__lock) +static void sta2x11_regmap_lock (void *__lock,unsigned long *flags) {spinlock_t *lock = __lock;spin_lock (lock); } -static void sta2x11_regmap_unlock (void *__lock) +static void sta2x11_regmap_unlock (void *__lock,unsigned long *flags) {spinlock_t *lock = __lock;spin_unlock (lock); }/* OTP (one time programmable registers do not require locking */ -static void sta2x11_regmap_nolock (void *__lock) +static void sta2x11_regmap_nolock (void *__lock,unsigned long *flags) { }
drivers/staging/imx-drm/imx-tve.c +2 -2
@@ -131,13 +131,13 @@ struct imx_tve {int hsync_pin; }; -static void tve_lock (void *__tve) +static void tve_lock (void *__tve,unsigned long *flags) {struct imx_tve *tve = __tve;spin_lock (&tve->lock); } -static void tve_unlock (void *__tve) +static void tve_unlock (void *__tve,unsigned long *flags) {struct imx_tve *tve = __tve;spin_unlock (&tve->lock);
include/linux/regmap.h +2 -2
@@ -85,8 +85,8 @@ struct regmap_access_table {unsigned int n_no_ranges; }; -typedef void (*regmap_lock )(void *); -typedef void (*regmap_unlock )(void *); +typedef void (*regmap_lock )(void *,unsigned long *flags); +typedef void (*regmap_unlock )(void *,unsigned long *flags);/** * Configuration for the register map of a device.
Signature · 1 line
1.8.0
Mark Brown
maintainerRe: [PATCH 2/2] regmap: Make regmap-mmio usable from different contexts
Quoted 3 lines · Lars-Peter Clausen
This patch updates the adds a flags parameter to the regmap lock and unlock callbacks and uses
spin_lock_irqsave()andspin_unlock_restore()for the mmio case. This allows us to use regmap-mmio from different contexts.
This seems really invasive, why not just have the lock that gets passed in point to a struct which has both the lock and the flags? As far as the core is concerned the lock is just whatever data is required to do the locking, the fact that it's actually two values is an implementation detail of this locking implementation.
Re: [PATCH 2/2] regmap: Make regmap-mmio usable from different contexts
Quoted 11 lines · Mark Brown
Quoted 3 lines · Lars-Peter Clausen
This patch updates the adds a flags parameter to the regmap lock and unlock callbacks and uses
spin_lock_irqsave()andspin_unlock_restore()for the mmio case. This allows us to use regmap-mmio from different contexts.This seems really invasive, why not just have the lock that gets passed in point to a struct which has both the lock and the flags? As far as the core is concerned the lock is just whatever data is required to do the locking, the fact that it's actually two values is an implementation detail of this locking implementation.
I think that won't work. spin_lock_irqsave() will write to the flags parameter before it has successfully taken the look. So if a process running on another CPU tries to acquire the the lock while it is already held we'll end up overwriting the flags. E.g:
CPU0 CPU1spin_lock_irqsave()
- write flags
spin_lock_irqsave()
- overwrite flags
spin_unlock_irqrestore()
- restore wrong flags
Hence flags needs to go onto the stack.
- Lars
Mark Brown
maintainerRe: [PATCH 2/2] regmap: Make regmap-mmio usable from different contexts
Quoted 8 lines · Lars-Peter Clausen
Quoted 5 lines · Mark Brown
This seems really invasive, why not just have the lock that gets passed in point to a struct which has both the lock and the flags? As far as the core is concerned the lock is just whatever data is required to do the locking, the fact that it's actually two values is an implementation detail of this locking implementation.
Quoted 4 lines
I think that won't work.
spin_lock_irqsave()will write to the flags parameter before it has successfully taken the look. So if a process running on another CPU tries to acquire the the lock while it is already held we'll end up overwriting the flags. E.g:
So you'd have to allocate a struct on the stack with a pointer and the flags in it and initialise the pointer. Not awesome but not the end of the world.
Re: [PATCH 2/2] regmap: Make regmap-mmio usable from different contexts
Quoted 9 lines · Mark Brown
Quoted 7 lines · Lars-Peter Clausen
Quoted 5 lines · Mark Brown
This seems really invasive, why not just have the lock that gets passed in point to a struct which has both the lock and the flags? As far as the core is concerned the lock is just whatever data is required to do the locking, the fact that it's actually two values is an implementation detail of this locking implementation.
Quoted 8 lines
Quoted 4 lines
I think that won't work.
spin_lock_irqsave()will write to the flags parameter before it has successfully taken the look. So if a process running on another CPU tries to acquire the the lock while it is already held we'll end up overwriting the flags. E.g:So you'd have to allocate a struct on the stack with a pointer and the flags in it and initialise the pointer. Not awesome but not the end of the world.
I'm not sure I understand what you mean. This needs to be done for each caller of a lock()/unlock() pair. We can't allocate the flags on the stack inside the lock() function since the stack will be gone once the function exits.
- Lars
Mark Brown
maintainerRe: [PATCH 2/2] regmap: Make regmap-mmio usable from different contexts
Quoted 9 lines · Lars-Peter Clausen
Quoted 3 lines · Mark Brown
So you'd have to allocate a struct on the stack with a pointer and the flags in it and initialise the pointer. Not awesome but not the end of the world.
I'm not sure I understand what you mean. This needs to be done for each caller of a lock()/unlock() pair. We can't allocate the flags on the stack inside the
lock()function since the stack will be gone once the function exits.
Allocate the struct on the stack - if you can allocate flags on the stack you can allocate the struct on the stack too. Though I suppose you still need to change all the callers for this which is just nasty and makes it seem pointless to bother with abstraction here.
This is all stuff you should've been going through in the changelog when you're jumping off into something that involves changing every single user...
Re: [PATCH 2/2] regmap: Make regmap-mmio usable from different contexts
In reply to Lars-Peter ClausenQuoted 3 lines · Lars-Peter Clausen
regmap-mmio uses a spinlock with
spin_lock()andspin_unlock()for locking. Which means in order to avoid race conditions the lock always needs to be taken from the same context.
I'm not really sure what this means. I assume contexts are atomic-vs-nonatomic? If so, spinlocks should work fine for this, right?
I guess the core of the issue is that you want to replace spin_lock() with spin_lock_irqsave(). I'd like to see that explicitly described in the commit description, if that is the core aspect of this change.
Re: the other comments about the API change: I think this can be done non-invasively:
static void regmap_lock_spinlock(void *__map)
{
struct regmap *map = __map;
unsigned long local_flags;
spin_lock_irqsave(&map->spinlock, local_flags);
/*
* Here, we have the lock locked, so we own the flags,
* and can write to them.
*/
map->spinlock_flags = local_flags;
}static void regmap_unlock_spinlock(void *__map, unsigned long *flags)
{
struct regmap *map = __map;
spin_unlock_irqrestore(&map->spinlock, map->spinlock_flags);
}... and obviously add a spinlock_flags field to struct regmap (perhaps start unioning the mutex and spinlock data fields there if you want to save space).
Re: [PATCH 2/2] regmap: Make regmap-mmio usable from different contexts
Quoted 7 lines · Stephen Warren
Quoted 3 lines · Lars-Peter Clausen
regmap-mmio uses a spinlock with
spin_lock()andspin_unlock()for locking. Which means in order to avoid race conditions the lock always needs to be taken from the same context.I'm not really sure what this means. I assume contexts are atomic-vs-nonatomic?
Yes.
Quoted 1 line
If so, spinlocks should work fine for this, right?
No, you have to take special care if you want to take the same spinlock from different contexts. And you have to take even more care if the code that takes the lock can run in different contexts.
Quoted 4 lines
I guess the core of the issue is that you want to replace
spin_lock()withspin_lock_irqsave(). I'd like to see that explicitly described in the commit description, if that is the core aspect of this change.
Hm, it does.
regmap-mmio uses a spinlock with spin_lock() and spin_unlock() for locking.
...
This patch updates the adds a flags parameter to the regmap lock and unlock callbacks and uses spin_lock_irqsave() and spin_unlock_restore() ...
Quoted 18 lines
Re: the other comments about the API change: I think this can be done non-invasively:
static void
regmap_lock_spinlock(void *__map){ struct regmap *map = __map; unsigned long local_flags; spin_lock_irqsave(&map->spinlock, local_flags); /* * Here, we have the lock locked, so we own the flags, * and can write to them. */ map->spinlock_flags = local_flags; }static void
regmap_unlock_spinlock(void *__map, unsigned long *flags)
Quoted 8 lines
{ struct regmap *map = __map; spin_unlock_irqrestore(&map->spinlock, map->spinlock_flags); }... and obviously add a
spinlock_flagsfield to struct regmap (perhaps start unioning the mutex and spinlock data fields there if you want to save space).
Hm, that might work.
Re: [PATCH 2/2] regmap: Make regmap-mmio usable from different contexts
Quoted 9 lines · Lars-Peter Clausen
Quoted 7 lines · Stephen Warren
Quoted 3 lines · Lars-Peter Clausen
regmap-mmio uses a spinlock with
spin_lock()andspin_unlock()for locking. Which means in order to avoid race conditions the lock always needs to be taken from the same context.I'm not really sure what this means. I assume contexts are atomic-vs-nonatomic?
Quoted 7 lines
Yes.
Quoted 1 line
If so, spinlocks should work fine for this, right?
No, you have to take special care if you want to take the same spinlock from different contexts. And you have to take even more care if the code that takes the lock can run in different contexts.
OK, but what is that "special care" that must be taken? I assume from this patch, you mean using spin_lock_irqsave() rather than spin_lock(). So the issue isn't so much that spin locks are used, but rather how they're used. The code still has a spin_lock_t before and after...
Quoted 4 lines
Quoted 3 lines
I guess the core of the issue is that you want to replace
spin_lock()withspin_lock_irqsave(). I'd like to see that explicitly described in the commit description, if that is the core aspect of this change.
Quoted 8 lines
Hm, it does.
regmap-mmio uses a spinlock with
spin_lock()andspin_unlock()for locking.
...
This patch updates the adds a flags parameter to the regmap lock and unlock callbacks and usesspin_lock_irqsave()andspin_unlock_restore()...
To me, that doesn't make me realize that the core part of this change is to switch between the two different APIs that operate on the spin lock. I'd expect the commit description to say something more like:
regmap-mmio uses a spinlock for locking. In order for this to work correctly from different contexts (atomic vs non-atomic), this spinlock must be locked using spin_lock_irqsave() rather than spin_lock(). To support this, a spinlock_flags field is added to struct regmap to store the flags between regmap_{,un}lock_spinlock().
Mark Brown
maintainerRe: [PATCH 2/2] regmap: Make regmap-mmio usable from different contexts
In reply to Lars-Peter ClausenQuoted 6 lines · Lars-Peter Clausen
Quoted 3 lines · Stephen Warren
I guess the core of the issue is that you want to replace
spin_lock()withspin_lock_irqsave(). I'd like to see that explicitly described in the commit description, if that is the core aspect of this change.
Quoted 8 lines
Hm, it does.
regmap-mmio uses a spinlock with
spin_lock()andspin_unlock()for locking.
...
This patch updates the adds a flags parameter to the regmap lock and unlock callbacks and usesspin_lock_irqsave()andspin_unlock_restore()...
That's pretty buried in the changelog though - really all the first paragraph needs to say is that we want to use the irqsave versions to allow interaction with interrupt context.
Mark Brown
maintainerRe: [PATCH 2/2] regmap: Make regmap-mmio usable from different contexts
In reply to Stephen WarrenQuoted 3 lines · Stephen Warren
I guess the core of the issue is that you want to replace
spin_lock()withspin_lock_irqsave(). I'd like to see that explicitly described in the commit description, if that is the core aspect of this change.
Yes, I was just going to rewrite the commit log to make this a lot clearer - the entire first paragraph could pretty much just be replaced by just saying that we need spin_lock_irqsave() to allow interaction with interrupt context.
Quoted 3 lines
... and obviously add a
spinlock_flagsfield to struct regmap (perhaps start unioning the mutex and spinlock data fields there if you want to save space).
This should work, yes - care to respin Lars-Peter?
Mark Brown
maintainerQuoted 6 lines · Lars-Peter Clausen
The parameter passed to the regmap lock/unlock callbacks needs to be
map->lock_arg, regcache passes just map. This works fine in the case that no custom locking callbacks are used, since in this casemap->lock_argequals map, but will break when custom locking callbacks are used. The issue was introduced in commit0d4529c5("regmap: make lock/unlock functions customizable") and is fixed by this patch.
I've applied this but can you please also generate a version that applies against v3.10-rc2 since this should really go in as a bug fix but the patch depends on recent changes in the regcache code.