Skip to contentSkip to conversation

nvmem: core: eeprom: at24: Handle EEPROM with both read-only and wp-gpios

PATCH In review Marek Vasut 2 participants 12 messages Apr 21 – Apr 26

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

PATCH 1/1 nvmem: core: eeprom: at24: Handle EEPROM with both read-only and wp-gpios

Handle special-case of AT24 EEPROM described in DT, which contains both "read-only" and "wp-gpios" properties. Interpret this configuration as default read-only, but with the possibility of unlock via force_ro sysfs attribute.

For this to work, the nvmem core must keep the "nvmem" sysfs bin attr as read-write, any writes into the bin attr which occur while the device is read-only are blocked in bin_attr_nvmem_write(). The AT24 EEPROM driver must not mark the EEPROM as read-only if both "read-only" and "wp-gpios" DT properties are present.

The updated behavior can be tested as follows:

Current content:
"

$ hexdump -C /sys/bus/nvmem/devices/logging7/nvmem
00000000  66 6f 6f 0a ff ff ff ff
"

Write into default-read-only device:
"

$ echo bar > /sys/bus/nvmem/devices/logging7/nvmem
bash: echo: write error: Operation not permitted
$ cat /sys/bus/nvmem/devices/logging7/force_ro
1
"

Unlock and write into device:
"

$ echo 0 > /sys/bus/nvmem/devices/logging7/force_ro
$ cat /sys/bus/nvmem/devices/logging7/force_ro
0
$ echo bar > /sys/bus/nvmem/devices/logging7/nvmem
$ hexdump -C /sys/bus/nvmem/devices/logging7/nvmem
00000000  62 61 72 0a ff ff ff ff
"

Relock and write into device, fails because device is read-only again:
"

$ echo 1 > /sys/bus/nvmem/devices/logging7/force_ro
$ echo baz > /sys/bus/nvmem/devices/logging7/nvmem
bash: echo: write error: Operation not permitted
$ hexdump -C /sys/bus/nvmem/devices/logging7/nvmem
00000000  62 61 72 0a ff ff ff ff
"

---
Cc: Arnd Bergmann <[email protected]>
Cc: Bartosz Golaszewski <[email protected]>
Cc: Greg Kroah-Hartman <[email protected]>
Cc: Srinivas Kandagatla <[email protected]>
Cc: [email protected]
Cc: [email protected]

---
 drivers/misc/eeprom/at24.c | 3 ++-
 drivers/nvmem/core.c       | 3 +++
 2 files changed, 5 insertions(+), 1 deletion(-)
drivers/misc/eeprom/at24.c +2 -1
index 0200288d3a7ab..9ef00b0ce83d2 100644
@@ -627,7 +627,8 @@ static int at24_probe(struct i2c_client *client)
 		page_size = 1;
 
 	flags = cdata->flags;
-	if (device_property_present(dev, "read-only"))
+	if (device_property_present(dev, "read-only") &&
+	    !device_property_present(dev, "wp-gpios"))
 		flags |= AT24_FLAG_READONLY;
 	if (device_property_present(dev, "no-read-rollover"))
 		flags |= AT24_FLAG_NO_RDROL;
drivers/nvmem/core.c +3 -0
index 311cb2e5a5c02..c0eedfe8d706c 100644
@@ -282,6 +282,9 @@ static umode_t nvmem_bin_attr_get_umode(struct nvmem_device *nvmem)
 	if (!nvmem->root_only)
 		mode |= 0044;
 
+	if (nvmem->read_only && nvmem->reg_write)
+		mode |= 0200;
+
 	if (!nvmem->read_only)
 		mode |= 0200;
 
Signature · 1 line
2.53.0
#1 lore raw
Quoted 6 lines · Marek Vasut

Handle special-case of AT24 EEPROM described in DT, which contains both "read-only" and "wp-gpios" properties. Interpret this configuration as default read-only, but with the possibility of unlock via force_ro sysfs attribute.

Patch looks ok code-wise but does this really make sense? If an EEPROM is read-only, we should forbid writes in the kernel. Which board uses it? Can we simply remove the read-only flag from DT?

Admittedly: the DT bindings do allow it as read-only and wp-gpios are not mutually exclusive but I think it's more of an accidental omission than a planned feature.

Bart

#2 lore raw
Quoted 11 lines · Bartosz Golaszewski
Quoted 6 lines · Marek Vasut

Handle special-case of AT24 EEPROM described in DT, which contains both "read-only" and "wp-gpios" properties. Interpret this configuration as default read-only, but with the possibility of unlock via force_ro sysfs attribute.

Patch looks ok code-wise but does this really make sense? If an EEPROM is read-only, we should forbid writes in the kernel. Which board uses it? Can we simply remove the read-only flag from DT?

Currently I am not aware of any upstream users, I plan to introduce one once this patch or some for of it lands.

I have is an ID EEPROM which I would like to be able to program under special circumstances (hence the wp-gpios control) , but it should be by default read-only .

If I remove the read-only, then by default the EEPROM is read-write, which is undesired. If I remote wp-gpios then I loose access to the force_ro attribute which controls the nWP GPIO from userspace, which is undesired.

So I think defining this special-case where wp-gpios and read-only are used together as default-read-only is sensible.

Quoted 3 lines

Admittedly: the DT bindings do allow it as read-only and wp-gpios are not mutually exclusive but I think it's more of an accidental omission than a planned feature.

I think it is currently an undefined behavior, and this patch defines it.

#3 lore raw
Quoted 9 lines · Marek Vasut
Quoted 8 lines · Bartosz Golaszewski
Quoted 6 lines · Marek Vasut

Handle special-case of AT24 EEPROM described in DT, which contains both "read-only" and "wp-gpios" properties. Interpret this configuration as default read-only, but with the possibility of unlock via force_ro sysfs attribute.

Quoted 5 lines
Quoted 3 lines

Patch looks ok code-wise but does this really make sense? If an EEPROM is read-only, we should forbid writes in the kernel. Which board uses it? Can we simply remove the read-only flag from DT?

Currently I am not aware of any upstream users, I plan to introduce one once this patch or some for of it lands.

I have to amend my statement, I would also like to adjust an already upstream DT to make use of this default-read-only functionality now.

I would however like to get go/no-go on this patch before I roll out the DT patches.

Quoted 10 lines

I have is an ID EEPROM which I would like to be able to program under special circumstances (hence the wp-gpios control) , but it should be by default read-only .

If I remove the read-only, then by default the EEPROM is read-write, which is undesired. If I remote wp-gpios then I loose access to the force_ro attribute which controls the nWP GPIO from userspace, which is undesired.

So I think defining this special-case where wp-gpios and read-only are used together as default-read-only is sensible.

Quoted 3 lines

Admittedly: the DT bindings do allow it as read-only and wp-gpios are not mutually exclusive but I think it's more of an accidental omission than a planned feature.

I think it is currently an undefined behavior, and this patch defines it.

Also, this default-read-only behavior is effectively the same behavior like the eMMC HW BOOT partitions have, they are also default read-only, but can be switched and written to by setting their force_ro sysfs attribute.

#4 lore raw
Quoted 15 lines · Marek Vasut
Quoted 13 lines · Marek Vasut
Quoted 11 lines · Bartosz Golaszewski
Quoted 6 lines · Marek Vasut

Handle special-case of AT24 EEPROM described in DT, which contains both "read-only" and "wp-gpios" properties. Interpret this configuration as default read-only, but with the possibility of unlock via force_ro sysfs attribute.

Patch looks ok code-wise but does this really make sense? If an EEPROM is read-only, we should forbid writes in the kernel. Which board uses it? Can we simply remove the read-only flag from DT?

Quoted 9 lines
Quoted 2 lines

Currently I am not aware of any upstream users, I plan to introduce one once this patch or some for of it lands.

I have to amend my statement, I would also like to adjust an already upstream DT to make use of this default-read-only functionality now.

I would however like to get go/no-go on this patch before I roll out the DT patches.

Yes, go ahead.

Quoted 15 lines
Quoted 15 lines

I have is an ID EEPROM which I would like to be able to program under special circumstances (hence the wp-gpios control) , but it should be by default read-only .

If I remove the read-only, then by default the EEPROM is read-write, which is undesired. If I remote wp-gpios then I loose access to the force_ro attribute which controls the nWP GPIO from userspace, which is undesired.

So I think defining this special-case where wp-gpios and read-only are used together as default-read-only is sensible.

Quoted 3 lines

Admittedly: the DT bindings do allow it as read-only and wp-gpios are not mutually exclusive but I think it's more of an accidental omission than a planned feature.

Quoted 6 lines
Quoted 1 line

I think it is currently an undefined behavior, and this patch defines it.

Also, this default-read-only behavior is effectively the same behavior like the eMMC HW BOOT partitions have, they are also default read-only, but can be switched and written to by setting their force_ro sysfs attribute.

I see. Ok, please send a v2.

Bart

#5 lore raw
Quoted 43 lines · Bartosz Golaszewski
Quoted 15 lines · Marek Vasut
Quoted 13 lines · Marek Vasut
Quoted 11 lines · Bartosz Golaszewski
Quoted 6 lines · Marek Vasut

Handle special-case of AT24 EEPROM described in DT, which contains both "read-only" and "wp-gpios" properties. Interpret this configuration as default read-only, but with the possibility of unlock via force_ro sysfs attribute.

Patch looks ok code-wise but does this really make sense? If an EEPROM is read-only, we should forbid writes in the kernel. Which board uses it? Can we simply remove the read-only flag from DT?

Quoted 9 lines
Quoted 2 lines

Currently I am not aware of any upstream users, I plan to introduce one once this patch or some for of it lands.

I have to amend my statement, I would also like to adjust an already upstream DT to make use of this default-read-only functionality now.

I would however like to get go/no-go on this patch before I roll out the DT patches.

Yes, go ahead.

Quoted 15 lines
Quoted 15 lines

I have is an ID EEPROM which I would like to be able to program under special circumstances (hence the wp-gpios control) , but it should be by default read-only .

If I remove the read-only, then by default the EEPROM is read-write, which is undesired. If I remote wp-gpios then I loose access to the force_ro attribute which controls the nWP GPIO from userspace, which is undesired.

So I think defining this special-case where wp-gpios and read-only are used together as default-read-only is sensible.

Quoted 3 lines

Admittedly: the DT bindings do allow it as read-only and wp-gpios are not mutually exclusive but I think it's more of an accidental omission than a planned feature.

Quoted 8 lines
Quoted 6 lines
Quoted 1 line

I think it is currently an undefined behavior, and this patch defines it.

Also, this default-read-only behavior is effectively the same behavior like the eMMC HW BOOT partitions have, they are also default read-only, but can be switched and written to by setting their force_ro sysfs attribute.

I see. Ok, please send a v2.

Does this patch require any changes ?

I will be sending the DT changes separately.

#7 lore raw
Quoted 10 lines · Bartosz Golaszewski
Quoted 6 lines · Marek Vasut
Quoted 3 lines

I see. Ok, please send a v2.

Does this patch require any changes ?

I will be sending the DT changes separately.

Sashiko is saying this:
https://sashiko.dev/#/patchset/20260421140755.54222-1-marex%40nabladev.com

What does this mean ?

Quoted 2 lines

Shouldn't we report the device as read-only in sysfs unless it was "unlocked" with force_ro?

This would be ideal, but I did not find a way to toggle the "nvmem" bin attr permissions at runtime. Is that even possible ?

#8 lore raw
Quoted 13 lines · Marek Vasut
Quoted 10 lines · Bartosz Golaszewski
Quoted 6 lines · Marek Vasut
Quoted 3 lines

I see. Ok, please send a v2.

Does this patch require any changes ?

I will be sending the DT changes separately.

Sashiko is saying this:
https://sashiko.dev/#/patchset/20260421140755.54222-1-marex%40nabladev.com

Quoted 6 lines

What does this mean ?

Quoted 2 lines

Shouldn't we report the device as read-only in sysfs unless it was "unlocked" with force_ro?

This would be ideal, but I did not find a way to toggle the "nvmem" bin attr permissions at runtime. Is that even possible ?

Right, it seems like it's set once and can't be changed (Greg: correct me if I'm wrong).

Ok, nevermind the comment then. Maybe just split the changes into nvmem and at24 changes and I can take both with an Ack from Srini.

Bart

#9 lore raw
Quoted 18 lines · Bartosz Golaszewski
Quoted 17 lines · Marek Vasut
Quoted 10 lines · Bartosz Golaszewski
Quoted 6 lines · Marek Vasut
Quoted 3 lines

I see. Ok, please send a v2.

Does this patch require any changes ?

I will be sending the DT changes separately.

Sashiko is saying this:
https://sashiko.dev/#/patchset/20260421140755.54222-1-marex%40nabladev.com

What does this mean ?

Quoted 2 lines

Shouldn't we report the device as read-only in sysfs unless it was "unlocked" with force_ro?

Quoted 8 lines
Quoted 2 lines

This would be ideal, but I did not find a way to toggle the "nvmem" bin attr permissions at runtime. Is that even possible ?

Right, it seems like it's set once and can't be changed (Greg: correct me if I'm wrong).

Ok, nevermind the comment then. Maybe just split the changes into nvmem and at24 changes and I can take both with an Ack from Srini.

I have two more ideas I would like to run past you ... how about either:

- If wp-gpios is present, set the device as default RO after boot, and let force_ro sysfs attribute toggle the protection of the device back and forth afterward. This would however change the userspace facing behavior slightly, because right now, with wp-gpios present in DT, the device is default RW.

- Introduce new DT property, wp-gpios-default-read-only or default-read-only or some such, to indicate the device should be in read-only mode by default. That would mitigate the downside of the aforementioned point, but would require a new DT property.

Thoughts ?

#10 lore raw
Quoted 23 lines · Marek Vasut
Quoted 21 lines · Bartosz Golaszewski
Quoted 19 lines · Marek Vasut
Quoted 10 lines · Bartosz Golaszewski
Quoted 6 lines · Marek Vasut
Quoted 3 lines

I see. Ok, please send a v2.

Does this patch require any changes ?

I will be sending the DT changes separately.

Sashiko is saying this:
https://sashiko.dev/#/patchset/20260421140755.54222-1-marex%40nabladev.com

What does this mean ?

Quoted 2 lines

Shouldn't we report the device as read-only in sysfs unless it was "unlocked" with force_ro?

This would be ideal, but I did not find a way to toggle the "nvmem" bin attr permissions at runtime. Is that even possible ?

Quoted 8 lines
Quoted 5 lines

Right, it seems like it's set once and can't be changed (Greg: correct me if I'm wrong).

Ok, nevermind the comment then. Maybe just split the changes into nvmem and at24 changes and I can take both with an Ack from Srini.

I have two more ideas I would like to run past you ... how about either:

- If wp-gpios is present, set the device as default RO after boot, and

Isn't this already what happens though? nvmem core requests the GPIO as output-high and drives it low only when it's doing the writing.

Quoted 5 lines

let force_ro sysfs attribute toggle the protection of the device back and forth afterward. This would however change the userspace facing behavior slightly, because right now, with wp-gpios present in DT, the device is default RW.

Or do you mean just the file permissions?

If the latter, then that does sound logically sound but yeah, it's asking for a regression report. :)

Quoted 6 lines

- Introduce new DT property, wp-gpios-default-read-only or default-read-only or some such, to indicate the device should be in read-only mode by default. That would mitigate the downside of the aforementioned point, but would require a new DT property.

Thoughts ?

I like "default-read-only" and it both allows to introduce this behavior and not break existing users. Let's loop in DT maintainers and see. I'd just like to clarify if we're talking about sysfs permissions or GPIO behavior here.

Bart

#11 lore raw
Quoted 27 lines · Bartosz Golaszewski
Quoted 26 lines · Marek Vasut
Quoted 24 lines · Bartosz Golaszewski
Quoted 13 lines · Marek Vasut
Quoted 10 lines · Bartosz Golaszewski
Quoted 6 lines · Marek Vasut
Quoted 3 lines

I see. Ok, please send a v2.

Does this patch require any changes ?

I will be sending the DT changes separately.

Sashiko is saying this:
https://sashiko.dev/#/patchset/20260421140755.54222-1-marex%40nabladev.com

Quoted 6 lines

What does this mean ?

Quoted 2 lines

Shouldn't we report the device as read-only in sysfs unless it was "unlocked" with force_ro?

This would be ideal, but I did not find a way to toggle the "nvmem" bin attr permissions at runtime. Is that even possible ?

Right, it seems like it's set once and can't be changed (Greg: correct me if I'm wrong).

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Ok, nevermind the comment then. Maybe just split the changes into nvmem and at24 changes and I can take both with an Ack from Srini.

I have two more ideas I would like to run past you ... how about either:

- If wp-gpios is present, set the device as default RO after boot, and

Isn't this already what happens though? nvmem core requests the GPIO as output-high and drives it low only when it's doing the writing.

It isn't, the default is read-write . But I now submitted a V2:

nvmem: core: Default to read-only if wp-gpios present

Quoted 10 lines
Quoted 5 lines

let force_ro sysfs attribute toggle the protection of the device back and forth afterward. This would however change the userspace facing behavior slightly, because right now, with wp-gpios present in DT, the device is default RW.

Or do you mean just the file permissions?

If the latter, then that does sound logically sound but yeah, it's asking for a regression report. :)

Please see "nvmem: core: Default to read-only if wp-gpios present" that I just submitted.

Quoted 11 lines
Quoted 6 lines

- Introduce new DT property, wp-gpios-default-read-only or default-read-only or some such, to indicate the device should be in read-only mode by default. That would mitigate the downside of the aforementioned point, but would require a new DT property.

Thoughts ?

I like "default-read-only" and it both allows to introduce this behavior and not break existing users. Let's loop in DT maintainers and see. I'd just like to clarify if we're talking about sysfs permissions or GPIO behavior here.

I think we might not need this, please see above.