Skip to contentSkip to conversation

regmap: add WARN_ONCE when invalid mask is provided to regmap_field_init()

PATCH In review Matt Ranostay 2 participants 2 messages Sep 9, 2022

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 regmap: add WARN_ONCE when invalid mask is provided to regmap_field_init()

In regmap_field_init() when a invalid mask is provided it still initializes without any warnings of it being incorrect.

An example of this is when the LSB is greater than MSB a mask of zero is produced.

WARN_ONCE() is not ideal for this but requires less changes to core regmap code.

Based on: https://lore.kernel.org/all/[email protected]/

---
 drivers/core/regmap.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/core/regmap.c +3 -0
index 5f98f85cfce..3100a58af2c 100644
@@ -20,6 +20,7 @@
 #include <linux/compat.h>
 #include <linux/err.h>
 #include <linux/bitops.h>
+#include <linux/bug.h>
 
 /*
  * Internal representation of a regmap field. Instead of storing the MSB and
@@ -649,6 +650,8 @@ static void regmap_field_init(struct regmap_field *rm_field,
 	rm_field->reg = reg_field.reg;
 	rm_field->shift = reg_field.lsb;
 	rm_field->mask = GENMASK(reg_field.msb, reg_field.lsb);
+
+	WARN_ONCE(rm_field->mask == 0, "invalid empty mask defined\n");
 }
 
 struct regmap_field *devm_regmap_field_alloc(struct udevice *dev,
Signature · 1 line
2.37.2
#1 lore raw

Hi Matt,

Quoted 39 lines · Matt Ranostay

In regmap_field_init() when a invalid mask is provided it still initializes without any warnings of it being incorrect.

An example of this is when the LSB is greater than MSB a mask of zero is produced.

WARN_ONCE() is not ideal for this but requires less changes to core regmap code.

Based on: https://lore.kernel.org/all/[email protected]/

---
 drivers/core/regmap.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/core/regmap.c +3 -1
index 5f98f85cfce..3100a58af2c 100644
@@ -20,6 +20,7 @@
 #include <linux/compat.h>
 #include <linux/err.h>
 #include <linux/bitops.h>
+#include <linux/bug.h>
 
 /*
  * Internal representation of a regmap field. Instead of storing the MSB and
@@ -649,6 +650,8 @@ static void regmap_field_init(struct regmap_field *rm_field,
        rm_field->reg = reg_field.reg;
        rm_field->shift = reg_field.lsb;
        rm_field->mask = GENMASK(reg_field.msb, reg_field.lsb);
+
+       WARN_ONCE(rm_field->mask == 0, "invalid empty mask defined\n");
 }
 
 struct regmap_field *devm_regmap_field_alloc(struct udevice *dev,
--
Quoted 2 lines

2.37.2

I think it would be better to return an error and check it in the caller, with a log_debug() here. We don't want to increase code size for a programming error, particularly in SPL.

Regards,
Simon