Skip to contentSkip to conversation

regmap: a small set of cleanups

PATCH In review Andy Shevchenko 3 participants 13 messages Nov 10 – Nov 16

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

cover regmap: a small set of cleanups

This is a mini-series of a small set of ad-hoc cleanups. Read individual patches to get the idea.

Andy Shevchenko (4):
regmap: Switch to use kmemdup_array()
regmap: Drop never used devm_regmap_*_free() APIs
regmap: Defer assignments to the later in some cases
regmap: Drop duplicate ret assignment

 drivers/base/regmap/regmap.c | 62 +++++++++---------------------------
 include/linux/regmap.h       |  3 --
 2 files changed, 15 insertions(+), 50 deletions(-)
Signature · 1 line
2.50.1
#1 lore raw

PATCH 1/4 regmap: Switch to use kmemdup_array()

Let the kmemdup_array() take care about multiplication and possible overflows.

---
 drivers/base/regmap/regmap.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/base/regmap/regmap.c +3 -3
index bc4e53ce8ecc..e327850ecc97 100644
@@ -2595,9 +2595,9 @@ static int _regmap_multi_reg_write(struct regmap *map,
 		 */
 		range = _regmap_range_lookup(map, reg);
 		if (range || regs[i].delay_us) {
-			size_t len = sizeof(struct reg_sequence)*num_regs;
-			struct reg_sequence *base = kmemdup(regs, len,
-							   GFP_KERNEL);
+			struct reg_sequence *base;
+
+			base = kmemdup_array(regs, num_regs, sizeof(*regs), GFP_KERNEL);
 			if (!base)
 				return -ENOMEM;
 			ret = _regmap_range_multi_paged_reg_write(map, base,
Signature · 1 line
2.50.1
#2 lore raw

PATCH 2/4 regmap: Drop never used devm_regmap_*_free() APIs

In reply to Andy Shevchenko

devm_regmap_field_free() was introduced in 2013 and was never ever used. devm_regmap_field_bulk_free() was introduced in 2020 and the same story.

Drop never used APIs.

---
 drivers/base/regmap/regmap.c | 36 ------------------------------------
 include/linux/regmap.h       |  3 ---
 2 files changed, 39 deletions(-)
drivers/base/regmap/regmap.c +0 -36
index e327850ecc97..0617a957559b 100644
@@ -1330,42 +1330,6 @@ void regmap_field_bulk_free(struct regmap_field *field)
 }
 EXPORT_SYMBOL_GPL(regmap_field_bulk_free);
 
-/**
- * devm_regmap_field_bulk_free() - Free a bulk register field allocated using
- *                            devm_regmap_field_bulk_alloc.
- *
- * @dev: Device that will be interacted with
- * @field: regmap field which should be freed.
- *
- * Free register field allocated using devm_regmap_field_bulk_alloc(). Usually
- * drivers need not call this function, as the memory allocated via devm
- * will be freed as per device-driver life-cycle.
- */
-void devm_regmap_field_bulk_free(struct device *dev,
-				 struct regmap_field *field)
-{
-	devm_kfree(dev, field);
-}
-EXPORT_SYMBOL_GPL(devm_regmap_field_bulk_free);
-
-/**
- * devm_regmap_field_free() - Free a register field allocated using
- *                            devm_regmap_field_alloc.
- *
- * @dev: Device that will be interacted with
- * @field: regmap field which should be freed.
- *
- * Free register field allocated using devm_regmap_field_alloc(). Usually
- * drivers need not call this function, as the memory allocated via devm
- * will be freed as per device-driver life-cyle.
- */
-void devm_regmap_field_free(struct device *dev,
-	struct regmap_field *field)
-{
-	devm_kfree(dev, field);
-}
-EXPORT_SYMBOL_GPL(devm_regmap_field_free);
-
 /**
  * regmap_field_alloc() - Allocate and initialise a register field.
  *
include/linux/regmap.h +0 -3
index 8f3034881f8a..e4f8f462fbab 100644
@@ -1488,7 +1488,6 @@ DEFINE_FREE(regmap_field, struct regmap_field *, if (_T) regmap_field_free(_T))
 
 struct regmap_field *devm_regmap_field_alloc(struct device *dev,
 		struct regmap *regmap, struct reg_field reg_field);
-void devm_regmap_field_free(struct device *dev,	struct regmap_field *field);
 
 int regmap_field_bulk_alloc(struct regmap *regmap,
 			     struct regmap_field **rm_field,
@@ -1499,8 +1498,6 @@ int devm_regmap_field_bulk_alloc(struct device *dev, struct regmap *regmap,
 				 struct regmap_field **field,
 				 const struct reg_field *reg_field,
 				 int num_fields);
-void devm_regmap_field_bulk_free(struct device *dev,
-				 struct regmap_field *field);
 
 int regmap_field_read(struct regmap_field *field, unsigned int *val);
 int regmap_field_update_bits_base(struct regmap_field *field,
Signature · 1 line
2.50.1
Reviewed-by on v1 2/4
#3 lore raw

Re: [PATCH v1 2/4] regmap: Drop never used devm_regmap_*_free() APIs

Quoted 6 lines · Andy Shevchenko

devm_regmap_field_free() was introduced in 2013 and was never ever used. devm_regmap_field_bulk_free() was introduced in 2020 and the same story.

Drop never used APIs.

maintainer
#4 lore raw

Re: [PATCH v1 2/4] regmap: Drop never used devm_regmap_*_free() APIs

In reply to Andy Shevchenko
Quoted 4 lines

devm_regmap_field_free() was introduced in 2013 and was never ever used. devm_regmap_field_bulk_free() was introduced in 2020 and the same story.

Drop never used APIs.

I tend to think that if we have allocation functions we should have matching release functions, this avoids cross tree annoyances and bugs due to people using inappropriate release functions which actually exist rather than adding the ones they should use.

Signature · 1 line
Mark Brown <[email protected]>
#5 lore raw

Re: [PATCH v1 2/4] regmap: Drop never used devm_regmap_*_free() APIs

Quoted 9 lines · Mark Brown
Quoted 4 lines

devm_regmap_field_free() was introduced in 2013 and was never ever used. devm_regmap_field_bulk_free() was introduced in 2020 and the same story.

Drop never used APIs.

I tend to think that if we have allocation functions we should have matching release functions, this avoids cross tree annoyances and bugs due to people using inappropriate release functions which actually exist rather than adding the ones they should use.

In general yes, but for devm_*() it makes not much sense as proven by dozens of dropped "release/free" type of devm_*() calls in the past. Also as commit message says for more than 10 years nobody used it even once. What does make you think there eventually be at least a single user?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer
#6 lore raw

Re: [PATCH v1 2/4] regmap: Drop never used devm_regmap_*_free() APIs

Quoted 11 lines · Andy Shevchenko
Quoted 4 lines · Mark Brown

I tend to think that if we have allocation functions we should have matching release functions, this avoids cross tree annoyances and bugs due to people using inappropriate release functions which actually exist rather than adding the ones they should use.

In general yes, but for devm_*() it makes not much sense as proven by dozens of dropped "release/free" type of devm_*() calls in the past. Also as commit message says for more than 10 years nobody used it even once. What does make you think there eventually be at least a single user?

Some of those release/free of devm issues are people intentionally doing a free and using the wrong function to do so, the main use case is enumerating something and deciding that some of the functionality is not usable so freeing the state for that functionality so you're not wasting memory carrying it around unused.

#7 lore raw

Re: [PATCH v1 2/4] regmap: Drop never used devm_regmap_*_free() APIs

Quoted 13 lines · Mark Brown
Quoted 11 lines · Andy Shevchenko
Quoted 4 lines · Mark Brown

I tend to think that if we have allocation functions we should have matching release functions, this avoids cross tree annoyances and bugs due to people using inappropriate release functions which actually exist rather than adding the ones they should use.

In general yes, but for devm_*() it makes not much sense as proven by dozens of dropped "release/free" type of devm_*() calls in the past. Also as commit message says for more than 10 years nobody used it even once. What does make you think there eventually be at least a single user?

Quoted 5 lines

Some of those release/free of devm issues are people intentionally doing a free and using the wrong function to do so, the main use case is enumerating something and deciding that some of the functionality is not usable so freeing the state for that functionality so you're not wasting memory carrying it around unused.

No objection on this in general, but for the mentioned APIs in this patch it's not the case. Nobody *ever* used them. Why do you think we should keep a dead code?

As for the memory side, we can also have a documentation that may list the commit IDs and APIs where they were being deleted, so if one is interested to resurrect them it can be done easily, however the quick universal method is git log -G ... -- include/linux/. Ex:

git log -G devm_gpio_free -- include/linux/ commit 2b038e786f8338a3bc22d791000753e0ec113e00
Author: Andy Shevchenko <[email protected]>
Date: Wed Jun 22 20:28:42 2022 +0300
...

(and using specific folder speeds that up a lot).

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#8 lore raw

PATCH 3/4 regmap: Defer assignments to the later in some cases

In reply to Andy Shevchenko

In some cases the assignments at the top of the certain functions belong to smaller subset of the functionality and may be located too far from their actual users. Defer them to the later.

This makes code robust against potential changes that may lead to the corruption of the respected values.

---
 drivers/base/regmap/regmap.c | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)
drivers/base/regmap/regmap.c +11 -7
index 0617a957559b..1855b5fc76da 100644
@@ -681,12 +681,14 @@ struct regmap *__regmap_init(struct device *dev,
 			     const char *lock_name)
 {
 	struct regmap *map;
-	int ret = -EINVAL;
 	enum regmap_endian reg_endian, val_endian;
 	int i, j;
+	int ret;
 
-	if (!config)
+	if (!config) {
+		ret = -EINVAL;
 		goto err;
+	}
 
 	map = kzalloc_obj(*map);
 	if (map == NULL) {
@@ -1604,8 +1606,7 @@ static int _regmap_raw_write_impl(struct regmap *map, unsigned int reg,
 {
 	struct regmap_range_node *range;
 	unsigned long flags;
-	void *work_val = map->work_buf + map->format.reg_bytes +
-		map->format.pad_bytes;
+	void *work_val;
 	void *buf;
 	int ret = -ENOTSUPP;
 	size_t len;
@@ -1688,6 +1689,7 @@ static int _regmap_raw_write_impl(struct regmap *map, unsigned int reg,
 	 * buffer to write.  Since register syncs often generate raw
 	 * writes of single registers optimise that case.
 	 */
+	work_val = map->work_buf + map->format.reg_bytes + map->format.pad_bytes;
 	if (val != work_val && val_len == map->format.val_bytes) {
 		memcpy(work_val, val, map->format.val_bytes);
 		val = work_val;
@@ -2291,8 +2293,8 @@ EXPORT_SYMBOL_GPL(regmap_fields_update_bits_base);
 int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val,
 		     size_t val_count)
 {
-	int ret = 0, i;
 	size_t val_bytes = map->format.val_bytes;
+	int ret, i;
 
 	if (!IS_ALIGNED(reg, map->reg_stride))
 		return -EINVAL;
@@ -2302,6 +2304,8 @@ int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val,
 	 * single write operations.
 	 */
 	if (!map->write || !map->format.parse_inplace) {
+		ret = 0; /* Later error paths rely on this */
+
 		guard(regmap)(map);
 
 		for (i = 0; i < val_count; i++) {
@@ -2739,12 +2743,12 @@ static int _regmap_bus_read(void *context, unsigned int reg,
 {
 	int ret;
 	struct regmap *map = context;
-	void *work_val = map->work_buf + map->format.reg_bytes +
-		map->format.pad_bytes;
+	void *work_val;
 
 	if (!map->format.parse_val)
 		return -EINVAL;
 
+	work_val = map->work_buf + map->format.reg_bytes + map->format.pad_bytes;
 	ret = _regmap_raw_read(map, reg, work_val, map->format.val_bytes, false);
 	if (ret == 0)
 		*val = map->format.parse_val(work_val);
Signature · 1 line
2.50.1
Reviewed-by on v1 3/4
#9 lore raw

Re: [PATCH v1 3/4] regmap: Defer assignments to the later in some cases

Quoted 8 lines · Andy Shevchenko

In some cases the assignments at the top of the certain functions belong to smaller subset of the functionality and may be located too far from their actual users. Defer them to the later.

This makes code robust against potential changes that may lead to the corruption of the respected values.

#10 lore raw

PATCH 4/4 regmap: Drop duplicate ret assignment

In reply to Andy Shevchenko

In a one case the ret is assigned at the top of a certain function and gets reassigned in all cases after that, making the initial assignment useless. Drop that assignment.

---
 drivers/base/regmap/regmap.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/base/regmap/regmap.c +1 -1
index 1855b5fc76da..b729226e9e3d 100644
@@ -1608,8 +1608,8 @@ static int _regmap_raw_write_impl(struct regmap *map, unsigned int reg,
 	unsigned long flags;
 	void *work_val;
 	void *buf;
-	int ret = -ENOTSUPP;
 	size_t len;
+	int ret;
 	int i;
 
 	/* Check for unwritable or noinc registers in range
Signature · 1 line
2.50.1
Reviewed-by on v1 4/4
#11 lore raw

Re: [PATCH v1 4/4] regmap: Drop duplicate ret assignment

Quoted 5 lines · Andy Shevchenko

In a one case the ret is assigned at the top of a certain function and gets reassigned in all cases after that, making the initial assignment useless. Drop that assignment.

maintainer
#12 lore raw

Re: (subset) [PATCH v1 0/4] regmap: a small set of cleanups

In reply to Andy Shevchenko
Quoted 5 lines · Andy Shevchenko

regmap: a small set of cleanups

This is a mini-series of a small set of ad-hoc cleanups. Read individual patches to get the idea.

Quoted 7 lines

Andy Shevchenko (4):
regmap: Switch to use kmemdup_array()
regmap: Drop never used devm_regmap_*_free() APIs
regmap: Defer assignments to the later in some cases
regmap: Drop duplicate ret assignment

[...]

Applied to

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

Thanks!

[1/4] regmap: Switch to use kmemdup_array()

      https://git.kernel.org/broonie/regmap/c/ed8939e50466
[3/4] regmap: Defer assignments to the later in some cases
      https://git.kernel.org/broonie/regmap/c/ac5fd39a1718
[4/4] regmap: Drop duplicate ret assignment
      https://git.kernel.org/broonie/regmap/c/64db965e43d4

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