mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH master 1/3] pstore: ramoops: fix use of wrong types on 64-bit
@ 2024-05-06 16:02 Ahmad Fatoum
  2024-05-06 16:02 ` [PATCH master 2/3] pstore: fix size of OF fixup memory region Ahmad Fatoum
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Ahmad Fatoum @ 2024-05-06 16:02 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

The kernel struct persistent_ram_buffer uses atomic_t for start and size
members in the persistent_ram_buffer chunk, which are 32-bit always,
unlike resource_size_t that we used in barebox, which is 64-bit when
CONFIG_PHYS_ADDR_T_64BIT=y.

To fix this, we could use either int32_t or atomic_t. To make
synchronization with Linux easier, let's use atomic_t.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 fs/pstore/ram_core.c | 25 ++++++++++++++-----------
 1 file changed, 14 insertions(+), 11 deletions(-)

diff --git a/fs/pstore/ram_core.c b/fs/pstore/ram_core.c
index 76b8a109be9d..24948d701744 100644
--- a/fs/pstore/ram_core.c
+++ b/fs/pstore/ram_core.c
@@ -14,12 +14,14 @@
 
 #define pr_fmt(fmt) "persistent_ram: " fmt
 
+#include <linux/build_bug.h>
 #include <linux/err.h>
 #include <linux/kernel.h>
 #include <linux/list.h>
 #include <linux/rslib.h>
 #include <linux/pstore_ram.h>
 #include <linux/string.h>
+#include <linux/atomic.h>
 #include <stdio.h>
 #include <malloc.h>
 #include <memory.h>
@@ -27,21 +29,22 @@
 
 struct persistent_ram_buffer {
 	uint32_t sig;
-	resource_size_t start;
-	resource_size_t size;
-	uint8_t data[0];
+	atomic_t start;
+	atomic_t size;
+	uint8_t data[];
 };
+static_assert(sizeof(struct persistent_ram_buffer) == 12);
 
 #define PERSISTENT_RAM_SIG (0x43474244) /* DBGC */
 
 static inline size_t buffer_size(struct persistent_ram_zone *prz)
 {
-	return prz->buffer->size;
+	return atomic_read(&prz->buffer->size);
 }
 
 static inline size_t buffer_start(struct persistent_ram_zone *prz)
 {
-	return prz->buffer->start;
+	return atomic_read(&prz->buffer->start);
 }
 
 /* increase and wrap the start pointer, returning the old value */
@@ -50,11 +53,11 @@ static size_t buffer_start_add(struct persistent_ram_zone *prz, size_t a)
 	int old;
 	int new;
 
-	old = prz->buffer->start;
+	old = atomic_read(&prz->buffer->start);
 	new = old + a;
 	while (unlikely(new >= prz->buffer_size))
 		new -= prz->buffer_size;
-	prz->buffer->start = new;
+	atomic_set(&prz->buffer->start, new);
 
 	return old;
 }
@@ -65,14 +68,14 @@ static void buffer_size_add(struct persistent_ram_zone *prz, size_t a)
 	size_t old;
 	size_t new;
 
-	old = prz->buffer->size;
+	old = atomic_read(&prz->buffer->size);
 	if (old == prz->buffer_size)
 		return;
 
 	new = old + a;
 	if (new > prz->buffer_size)
 		new = prz->buffer_size;
-	prz->buffer->size = new;
+	atomic_set(&prz->buffer->size, new);
 }
 
 static void notrace persistent_ram_encode_rs8(struct persistent_ram_zone *prz,
@@ -331,8 +334,8 @@ void persistent_ram_free_old(struct persistent_ram_zone *prz)
 
 void persistent_ram_zap(struct persistent_ram_zone *prz)
 {
-	prz->buffer->start = 0;
-	prz->buffer->size = 0;
+	atomic_set(&prz->buffer->start, 0);
+	atomic_set(&prz->buffer->size, 0);
 	persistent_ram_update_header_ecc(prz);
 }
 
-- 
2.39.2




^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH master 2/3] pstore: fix size of OF fixup memory region
  2024-05-06 16:02 [PATCH master 1/3] pstore: ramoops: fix use of wrong types on 64-bit Ahmad Fatoum
@ 2024-05-06 16:02 ` Ahmad Fatoum
  2024-05-06 16:02 ` [PATCH master 3/3] pstore: return -ENOENT when file is missing Ahmad Fatoum
  2024-05-07  7:33 ` [PATCH master 1/3] pstore: ramoops: fix use of wrong types on 64-bit Sascha Hauer
  2 siblings, 0 replies; 4+ messages in thread
From: Ahmad Fatoum @ 2024-05-06 16:02 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

struct resource::end points at the last byte of a region, but we pointed
1 byte after it instead. Fix this, so we have a proper (aligned) size
for the memory region.

It's possible this had no adverse effect.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 fs/pstore/ram.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/fs/pstore/ram.c b/fs/pstore/ram.c
index 4732bd4e3154..9ecf7ef5e901 100644
--- a/fs/pstore/ram.c
+++ b/fs/pstore/ram.c
@@ -478,7 +478,7 @@ static int ramoops_of_fixup(struct device_node *root, void *data)
 	int ret;
 
 	res.start = pdata->mem_address;
-	res.end = res.start + pdata->mem_size;
+	res.end = res.start + pdata->mem_size - 1;
 	res.name = "ramoops";
 
 	ret = of_fixup_reserved_memory(root, &res);
-- 
2.39.2




^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH master 3/3] pstore: return -ENOENT when file is missing
  2024-05-06 16:02 [PATCH master 1/3] pstore: ramoops: fix use of wrong types on 64-bit Ahmad Fatoum
  2024-05-06 16:02 ` [PATCH master 2/3] pstore: fix size of OF fixup memory region Ahmad Fatoum
@ 2024-05-06 16:02 ` Ahmad Fatoum
  2024-05-07  7:33 ` [PATCH master 1/3] pstore: ramoops: fix use of wrong types on 64-bit Sascha Hauer
  2 siblings, 0 replies; 4+ messages in thread
From: Ahmad Fatoum @ 2024-05-06 16:02 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

The expectation with stat(2) and open(2) is that they report -ENOENT if
a file name doesn't exist. Return this instead of the unexpected -EINVAL
error code.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 fs/pstore/fs.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/fs/pstore/fs.c b/fs/pstore/fs.c
index 777e2448cc62..238a316e9157 100644
--- a/fs/pstore/fs.c
+++ b/fs/pstore/fs.c
@@ -148,7 +148,7 @@ static int pstore_open(struct device *dev, FILE *file, const char *filename)
 
 	d = pstore_get_by_name(head, filename);
 	if (!d)
-		return -EINVAL;
+		return -ENOENT;
 
 	file->size = d->size;
 	file->priv = d;
@@ -226,7 +226,7 @@ static int pstore_stat(struct device *dev, const char *filename,
 
 	d = pstore_get_by_name(&allpstore, filename);
 	if (!d)
-		return -EINVAL;
+		return -ENOENT;
 
 	s->st_size = d->size;
 	s->st_mode = S_IFREG | S_IRWXU | S_IRWXG | S_IRWXO;
-- 
2.39.2




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH master 1/3] pstore: ramoops: fix use of wrong types on 64-bit
  2024-05-06 16:02 [PATCH master 1/3] pstore: ramoops: fix use of wrong types on 64-bit Ahmad Fatoum
  2024-05-06 16:02 ` [PATCH master 2/3] pstore: fix size of OF fixup memory region Ahmad Fatoum
  2024-05-06 16:02 ` [PATCH master 3/3] pstore: return -ENOENT when file is missing Ahmad Fatoum
@ 2024-05-07  7:33 ` Sascha Hauer
  2 siblings, 0 replies; 4+ messages in thread
From: Sascha Hauer @ 2024-05-07  7:33 UTC (permalink / raw)
  To: barebox, Ahmad Fatoum


On Mon, 06 May 2024 18:02:44 +0200, Ahmad Fatoum wrote:
> The kernel struct persistent_ram_buffer uses atomic_t for start and size
> members in the persistent_ram_buffer chunk, which are 32-bit always,
> unlike resource_size_t that we used in barebox, which is 64-bit when
> CONFIG_PHYS_ADDR_T_64BIT=y.
> 
> To fix this, we could use either int32_t or atomic_t. To make
> synchronization with Linux easier, let's use atomic_t.
> 
> [...]

Applied, thanks!

[1/3] pstore: ramoops: fix use of wrong types on 64-bit
      https://git.pengutronix.de/cgit/barebox/commit/?id=914622436219 (link may not be stable)
[2/3] pstore: fix size of OF fixup memory region
      https://git.pengutronix.de/cgit/barebox/commit/?id=0efa116b9f48 (link may not be stable)
[3/3] pstore: return -ENOENT when file is missing
      https://git.pengutronix.de/cgit/barebox/commit/?id=e70a514bce4b (link may not be stable)

Best regards,
-- 
Sascha Hauer <s.hauer@pengutronix.de>




^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2024-05-07  7:34 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-05-06 16:02 [PATCH master 1/3] pstore: ramoops: fix use of wrong types on 64-bit Ahmad Fatoum
2024-05-06 16:02 ` [PATCH master 2/3] pstore: fix size of OF fixup memory region Ahmad Fatoum
2024-05-06 16:02 ` [PATCH master 3/3] pstore: return -ENOENT when file is missing Ahmad Fatoum
2024-05-07  7:33 ` [PATCH master 1/3] pstore: ramoops: fix use of wrong types on 64-bit Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox