Skip to contentSkip to conversation

regmap: Add can_sleep configuration option

PATCH In review Dmitry Osipenko 3 participants 4 messages Sep 2, 2020

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 regmap: Add can_sleep configuration option

Regmap can't sleep if spinlock is used for the locking protection. This patch fixes regression caused by a previous commit that switched regmap to use fsleep() and this broke Amlogic S922X platform.

This patch adds new configuration option for regmap users, allowing to specify whether regmap operations can sleep and assuming that sleep is allowed if mutex is used for the regmap locking protection.

Fixes 2b32d2f7ce0a ("regmap: Use flexible sleep")
---
 drivers/base/regmap/internal.h |  3 +++
 drivers/base/regmap/regmap.c   | 19 +++++++++++++++----
 include/linux/regmap.h         |  3 +++
 3 files changed, 21 insertions(+), 4 deletions(-)
drivers/base/regmap/internal.h +3 -0
index 3d80c4b43f72..8a59359e145f 100644
@@ -161,6 +161,9 @@ struct regmap {
 	void *selector_work_buf;	/* Scratch buffer used for selector */
 
 	struct hwspinlock *hwlock;
+
+	/* if set, the regmap core can sleep */
+	bool can_sleep;
 };
 
 struct regcache_ops {
drivers/base/regmap/regmap.c +15 -4
index a417cb1a11dc..2807e544658e 100644
@@ -697,11 +697,13 @@ struct regmap *__regmap_init(struct device *dev,
 
 	if (config->disable_locking) {
 		map->lock = map->unlock = regmap_lock_unlock_none;
+		map->can_sleep = config->can_sleep;
 		regmap_debugfs_disable(map);
 	} else if (config->lock && config->unlock) {
 		map->lock = config->lock;
 		map->unlock = config->unlock;
 		map->lock_arg = config->lock_arg;
+		map->can_sleep = config->can_sleep;
 	} else if (config->use_hwlock) {
 		map->hwlock = hwspin_lock_request_specific(config->hwlock_id);
 		if (!map->hwlock) {
@@ -737,6 +739,7 @@ struct regmap *__regmap_init(struct device *dev,
 			mutex_init(&map->mutex);
 			map->lock = regmap_lock_mutex;
 			map->unlock = regmap_unlock_mutex;
+			map->can_sleep = true;
 			lockdep_set_class_and_name(&map->mutex,
 						   lock_key, lock_name);
 		}
@@ -2230,8 +2233,12 @@ static int _regmap_range_multi_paged_reg_write(struct regmap *map,
 				if (ret != 0)
 					return ret;
 
-				if (regs[i].delay_us)
-					fsleep(regs[i].delay_us);
+				if (regs[i].delay_us) {
+					if (map->can_sleep)
+						fsleep(regs[i].delay_us);
+					else
+						udelay(regs[i].delay_us);
+				}
 
 				base += n;
 				n = 0;
@@ -2267,8 +2274,12 @@ static int _regmap_multi_reg_write(struct regmap *map,
 			if (ret != 0)
 				return ret;
 
-			if (regs[i].delay_us)
-				fsleep(regs[i].delay_us);
+			if (regs[i].delay_us) {
+				if (map->can_sleep)
+					fsleep(regs[i].delay_us);
+				else
+					udelay(regs[i].delay_us);
+			}
 		}
 		return 0;
 	}
include/linux/regmap.h +3 -0
index d865d8fea535..0c49d59168b5 100644
@@ -342,6 +342,7 @@ typedef void (*regmap_unlock)(void *);
  * @hwlock_id: Specify the hardware spinlock id.
  * @hwlock_mode: The hardware spinlock mode, should be HWLOCK_IRQSTATE,
  *		 HWLOCK_IRQ or 0.
+ * @can_sleep: Optional, specifies whether regmap operations can sleep.
  */
 struct regmap_config {
 	const char *name;
@@ -398,6 +399,8 @@ struct regmap_config {
 	bool use_hwlock;
 	unsigned int hwlock_id;
 	unsigned int hwlock_mode;
+
+	bool can_sleep;
 };
 
 /**
Signature · 1 line
2.27.0
Tested-by on v1 1/1
#1 lore raw

Hi Dmitry,

Quoted 11 lines · Dmitry Osipenko

Regmap can't sleep if spinlock is used for the locking protection. This patch fixes regression caused by a previous commit that switched regmap to use fsleep() and this broke Amlogic S922X platform.

This patch adds new configuration option for regmap users, allowing to specify whether regmap operations can sleep and assuming that sleep is allowed if mutex is used for the regmap locking protection.

Fixes 2b32d2f7ce0a ("regmap: Use flexible sleep")

This fixes the issue I've reported. Thanks!

Quoted 89 lines
---
  drivers/base/regmap/internal.h |  3 +++
  drivers/base/regmap/regmap.c   | 19 +++++++++++++++----
  include/linux/regmap.h         |  3 +++
  3 files changed, 21 insertions(+), 4 deletions(-)
drivers/base/regmap/internal.h +3 -0
index 3d80c4b43f72..8a59359e145f 100644
@@ -161,6 +161,9 @@ struct regmap {
  	void *selector_work_buf;	/* Scratch buffer used for selector */
 
  	struct hwspinlock *hwlock;
+
+	/* if set, the regmap core can sleep */
+	bool can_sleep;
  };
 
  struct regcache_ops {
drivers/base/regmap/regmap.c +15 -4
index a417cb1a11dc..2807e544658e 100644
@@ -697,11 +697,13 @@ struct regmap *__regmap_init(struct device *dev,
 
  	if (config->disable_locking) {
  		map->lock = map->unlock = regmap_lock_unlock_none;
+		map->can_sleep = config->can_sleep;
  		regmap_debugfs_disable(map);
  	} else if (config->lock && config->unlock) {
  		map->lock = config->lock;
  		map->unlock = config->unlock;
  		map->lock_arg = config->lock_arg;
+		map->can_sleep = config->can_sleep;
  	} else if (config->use_hwlock) {
  		map->hwlock = hwspin_lock_request_specific(config->hwlock_id);
  		if (!map->hwlock) {
@@ -737,6 +739,7 @@ struct regmap *__regmap_init(struct device *dev,
  			mutex_init(&map->mutex);
  			map->lock = regmap_lock_mutex;
  			map->unlock = regmap_unlock_mutex;
+			map->can_sleep = true;
  			lockdep_set_class_and_name(&map->mutex,
  						   lock_key, lock_name);
  		}
@@ -2230,8 +2233,12 @@ static int _regmap_range_multi_paged_reg_write(struct regmap *map,
  				if (ret != 0)
  					return ret;
 
-				if (regs[i].delay_us)
-					fsleep(regs[i].delay_us);
+				if (regs[i].delay_us) {
+					if (map->can_sleep)
+						fsleep(regs[i].delay_us);
+					else
+						udelay(regs[i].delay_us);
+				}
 
  				base += n;
  				n = 0;
@@ -2267,8 +2274,12 @@ static int _regmap_multi_reg_write(struct regmap *map,
  			if (ret != 0)
  				return ret;
 
-			if (regs[i].delay_us)
-				fsleep(regs[i].delay_us);
+			if (regs[i].delay_us) {
+				if (map->can_sleep)
+					fsleep(regs[i].delay_us);
+				else
+					udelay(regs[i].delay_us);
+			}
  		}
  		return 0;
  	}
include/linux/regmap.h +1 -0
index d865d8fea535..0c49d59168b5 100644
@@ -342,6 +342,7 @@ typedef void (*regmap_unlock)(void *);
   * @hwlock_id: Specify the hardware spinlock id.
   * @hwlock_mode: The hardware spinlock mode, should be HWLOCK_IRQSTATE,
   *		 HWLOCK_IRQ or 0.
+ * @can_sleep: Optional, specifies whether regmap operations can sleep.
   */
  struct regmap_config {
  	const char *name;

include/linux/regmap.h struct regmap_config { · line 406

Quoted 8 lines
include/linux/regmap.h +2 -0
@@ -398,6 +399,8 @@ struct regmap_config {
  	bool use_hwlock;
  	unsigned int hwlock_id;
  	unsigned int hwlock_mode;
+
+	bool can_sleep;
  };
 
  /**

Best regards

Signature · 2 lines
Marek Szyprowski, PhD
Samsung R&D Institute Poland
#2 lore raw

02.09.2020 17:53, Marek Szyprowski пишет:

Quoted 11 lines

Hi Dmitry,

Quoted 8 lines · Dmitry Osipenko

Regmap can't sleep if spinlock is used for the locking protection. This patch fixes regression caused by a previous commit that switched regmap to use fsleep() and this broke Amlogic S922X platform.

This patch adds new configuration option for regmap users, allowing to specify whether regmap operations can sleep and assuming that sleep is allowed if mutex is used for the regmap locking protection.

Quoted 7 lines
Quoted 3 lines
Fixes 2b32d2f7ce0a ("regmap: Use flexible sleep")

This fixes the issue I've reported. Thanks!

Awesome! Thank you!

maintainer
#3 lore raw
In reply to Dmitry Osipenko
Quoted 7 lines · Dmitry Osipenko

Regmap can't sleep if spinlock is used for the locking protection. This patch fixes regression caused by a previous commit that switched regmap to use fsleep() and this broke Amlogic S922X platform.

This patch adds new configuration option for regmap users, allowing to specify whether regmap operations can sleep and assuming that sleep is allowed if mutex is used for the regmap locking protection.

Applied to

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

Thanks!

[1/1] regmap: Add can_sleep configuration option

      commit: 21f8e4828c44da39b0670c5d99d5728b739542a1

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