mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH 1/3] scripts/common: Do not mmap in read_file_2()
@ 2022-02-11  9:42 Sascha Hauer
  2022-02-11  9:42 ` [PATCH 2/3] scripts: bareboximd: Use mmap when possible Sascha Hauer
  2022-02-11  9:42 ` [PATCH 3/3] Revert "scripts/common: fix write_file when opened with mmap" Sascha Hauer
  0 siblings, 2 replies; 6+ messages in thread
From: Sascha Hauer @ 2022-02-11  9:42 UTC (permalink / raw)
  To: Barebox List

Using mmap() in read_file_2() leads to problems because there are
cases where the buffer returned from read_file_2() is modified and
then written back to the same file. This doesn't work when the
original file has been mmapped instead of being read into an allocated
buffer.

Using mmap() was introduced for a usecase where the system is very tight
in memory and bareboximd ran out of memory. Support for this usecase is
removed here, we'll bring it back in the next patch.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 scripts/common.c | 53 +++++++++++++++++++++++-------------------------
 1 file changed, 25 insertions(+), 28 deletions(-)

diff --git a/scripts/common.c b/scripts/common.c
index 154d6dffcb..3d07be3630 100644
--- a/scripts/common.c
+++ b/scripts/common.c
@@ -17,7 +17,7 @@
 int read_file_2(const char *filename, size_t *size, void **outbuf, size_t max_size)
 {
 	off_t fsize;
-	ssize_t rsize;
+	ssize_t read_size, now;
 	int ret, fd;
 	void *buf;
 
@@ -37,8 +37,10 @@ int read_file_2(const char *filename, size_t *size, void **outbuf, size_t max_si
 		goto close;
 	}
 
-	if (fsize < max_size)
-		max_size = fsize;
+	if (max_size < fsize)
+		read_size = max_size;
+	else
+		read_size = fsize;
 
 	if (lseek(fd, 0, SEEK_SET) == -1) {
 		fprintf(stderr, "Cannot seek to start %s: %s\n", filename, strerror(errno));
@@ -46,35 +48,30 @@ int read_file_2(const char *filename, size_t *size, void **outbuf, size_t max_si
 		goto close;
 	}
 
-	buf = mmap(NULL, max_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
-	if (buf == MAP_FAILED ) {
-		buf = malloc(max_size);
-		if (!buf) {
-			fprintf(stderr, "Cannot allocate memory\n");
-			ret = -ENOMEM;
-			goto close;
-		}
-
-		*outbuf = buf;
+	buf = malloc(read_size);
+	if (!buf) {
+		fprintf(stderr, "Cannot allocate memory\n");
+		ret = -ENOMEM;
+		goto close;
+	}
 
-		while (*size < max_size) {
-			rsize = read(fd, buf, max_size - *size);
-			if (rsize == 0) {
-				ret = -EIO;
-				goto free;
-			}
+	*outbuf = buf;
 
-			if (rsize < 0) {
-				ret = -errno;
-				goto free;
-			}
+	while (read_size) {
+		now = read(fd, buf, read_size);
+		if (now == 0) {
+			ret = -EIO;
+			goto free;
+		}
 
-			buf += rsize;
-			*size += rsize;
+		if (now < 0) {
+			ret = -errno;
+			goto free;
 		}
-	} else {
-		*outbuf = buf;
-		*size = max_size;
+
+		buf += now;
+		*size += now;
+		read_size -= now;
 	}
 
 	ret = 0;
-- 
2.30.2


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


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

* [PATCH 2/3] scripts: bareboximd: Use mmap when possible
  2022-02-11  9:42 [PATCH 1/3] scripts/common: Do not mmap in read_file_2() Sascha Hauer
@ 2022-02-11  9:42 ` Sascha Hauer
  2022-02-11  9:42 ` [PATCH 3/3] Revert "scripts/common: fix write_file when opened with mmap" Sascha Hauer
  1 sibling, 0 replies; 6+ messages in thread
From: Sascha Hauer @ 2022-02-11  9:42 UTC (permalink / raw)
  To: Barebox List

Using mmap() in read_file_2 was dropped in the last patch, bring it back
in a bareboximd specific function here.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 common/imd.c         |  7 ++++++-
 scripts/bareboximd.c | 34 ++++++++++++++++++++++++++++++++++
 2 files changed, 40 insertions(+), 1 deletion(-)

diff --git a/common/imd.c b/common/imd.c
index 0295d84d34..9ca0248523 100644
--- a/common/imd.c
+++ b/common/imd.c
@@ -21,6 +21,11 @@ static inline void read_file_2_free(void *buf)
 {
 	free(buf);
 }
+
+static int imd_read_file(const char *filename, size_t *size, void **outbuf)
+{
+	return read_file_2(filename, size, outbuf, 0x100000);
+}
 #endif
 
 /*
@@ -473,7 +478,7 @@ int imd_command(int argc, char *argv[])
 
 	filename = argv[optind];
 
-	ret = read_file_2(filename, &size, &buf, 0x100000);
+	ret = imd_read_file(filename, &size, &buf);
 	if (ret && ret != -EFBIG)
 		return -errno;
 
diff --git a/scripts/bareboximd.c b/scripts/bareboximd.c
index a734399aa5..2d4750d7fb 100644
--- a/scripts/bareboximd.c
+++ b/scripts/bareboximd.c
@@ -55,6 +55,40 @@ static unsigned long simple_strtoul(const char *cp, char **endp, unsigned int ba
 	return strtoul(cp, endp, base);
 }
 
+static int imd_read_file(const char *filename, size_t *size, void **outbuf)
+{
+	void *buf;
+	int fd, ret;
+	size_t fsize;
+
+	fd = open(filename, O_RDONLY);
+	if (fd < 0) {
+		fprintf(stderr, "Cannot open %s: %s\n", filename, strerror(errno));
+		return -errno;
+	}
+
+	fsize = lseek(fd, 0, SEEK_END);
+	if (fsize == -1) {
+		fprintf(stderr, "Cannot get size %s: %s\n", filename, strerror(errno));
+		ret = -errno;
+		goto close;
+	}
+
+	buf = mmap(NULL, fsize, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
+	if (buf == MAP_FAILED) {
+		close(fd);
+		return read_file_2(filename, size, outbuf, 0x100000);
+	}
+
+	*outbuf = buf;
+	*size = fsize;
+
+	return 0;
+close:
+	close(fd);
+	return ret;
+}
+
 #include "../include/xfuncs.h"
 #include "../crypto/crc32.c"
 #include "../common/imd.c"
-- 
2.30.2


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


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

* [PATCH 3/3] Revert "scripts/common: fix write_file when opened with mmap"
  2022-02-11  9:42 [PATCH 1/3] scripts/common: Do not mmap in read_file_2() Sascha Hauer
  2022-02-11  9:42 ` [PATCH 2/3] scripts: bareboximd: Use mmap when possible Sascha Hauer
@ 2022-02-11  9:42 ` Sascha Hauer
  2022-02-24 13:22   ` Andrej Picej
  1 sibling, 1 reply; 6+ messages in thread
From: Sascha Hauer @ 2022-02-11  9:42 UTC (permalink / raw)
  To: Barebox List

mmap() is no longer used in read_file_2(), so this patch is no longer
necessary.

This reverts commit 738601e1258c55953284ee10801b26b9977918c2.
---
 scripts/common.c | 15 ++++-----------
 1 file changed, 4 insertions(+), 11 deletions(-)

diff --git a/scripts/common.c b/scripts/common.c
index 3d07be3630..2be41615ea 100644
--- a/scripts/common.c
+++ b/scripts/common.c
@@ -102,33 +102,26 @@ int write_file(const char *filename, const void *buf, size_t size)
 {
 	int fd, ret = 0;
 	int now;
-	size_t left = size;
 
-	/* The same file may be mmapped currently, so can't use O_TRUNC here */
-	fd = open(filename, O_WRONLY | O_CREAT,
+	fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT,
 		  S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
 	if (fd < 0) {
 		fprintf(stderr, "Cannot open %s: %s\n", filename, strerror(errno));
 		return -errno;
 	}
 
-	while (left) {
-		now = write(fd, buf, left);
+	while (size) {
+		now = write(fd, buf, size);
 		if (now < 0) {
 			fprintf(stderr, "Cannot write to %s: %s\n", filename,
 				strerror(errno));
 			ret = -errno;
 			goto out;
 		}
-		left -= now;
+		size -= now;
 		buf += now;
 	}
 
-	if (ftruncate(fd, size) < 0) {
-		fprintf(stderr, "Cannot truncate file: %s", strerror(errno));
-		ret = -errno;
-	}
-
 out:
 	close(fd);
 
-- 
2.30.2


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


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

* Re: [PATCH 3/3] Revert "scripts/common: fix write_file when opened with mmap"
  2022-02-11  9:42 ` [PATCH 3/3] Revert "scripts/common: fix write_file when opened with mmap" Sascha Hauer
@ 2022-02-24 13:22   ` Andrej Picej
  2022-02-28 10:16     ` Sascha Hauer
  0 siblings, 1 reply; 6+ messages in thread
From: Andrej Picej @ 2022-02-24 13:22 UTC (permalink / raw)
  To: Sascha Hauer, Barebox List

Hi Sascha,

sorry for responding this late, but we are still getting the same errors 
as before when using bareboximd with these three patches.

Are you sure we can revert this patch? The file can still be opened in 
bareboximd with mmap(), and then be written with this write_file(), 
which fails with the same error:

> Cannot write to barebox.bin: Bad address
> CRC: write crc token to barebox.bin failed: -14
> Bad address

Best regards,
Andrej



On 11. 02. 22 10:42, Sascha Hauer wrote:
> mmap() is no longer used in read_file_2(), so this patch is no longer
> necessary.
> 
> This reverts commit 738601e1258c55953284ee10801b26b9977918c2.
> ---
>   scripts/common.c | 15 ++++-----------
>   1 file changed, 4 insertions(+), 11 deletions(-)
> 
> diff --git a/scripts/common.c b/scripts/common.c
> index 3d07be3630..2be41615ea 100644
> --- a/scripts/common.c
> +++ b/scripts/common.c
> @@ -102,33 +102,26 @@ int write_file(const char *filename, const void *buf, size_t size)
>   {
>   	int fd, ret = 0;
>   	int now;
> -	size_t left = size;
>   
> -	/* The same file may be mmapped currently, so can't use O_TRUNC here */
> -	fd = open(filename, O_WRONLY | O_CREAT,
> +	fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT,
>   		  S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
>   	if (fd < 0) {
>   		fprintf(stderr, "Cannot open %s: %s\n", filename, strerror(errno));
>   		return -errno;
>   	}
>   
> -	while (left) {
> -		now = write(fd, buf, left);
> +	while (size) {
> +		now = write(fd, buf, size);
>   		if (now < 0) {
>   			fprintf(stderr, "Cannot write to %s: %s\n", filename,
>   				strerror(errno));
>   			ret = -errno;
>   			goto out;
>   		}
> -		left -= now;
> +		size -= now;
>   		buf += now;
>   	}
>   
> -	if (ftruncate(fd, size) < 0) {
> -		fprintf(stderr, "Cannot truncate file: %s", strerror(errno));
> -		ret = -errno;
> -	}
> -
>   out:
>   	close(fd);
>   

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


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

* Re: [PATCH 3/3] Revert "scripts/common: fix write_file when opened with mmap"
  2022-02-24 13:22   ` Andrej Picej
@ 2022-02-28 10:16     ` Sascha Hauer
  2022-02-28 11:40       ` Andrej Picej
  0 siblings, 1 reply; 6+ messages in thread
From: Sascha Hauer @ 2022-02-28 10:16 UTC (permalink / raw)
  To: Andrej Picej; +Cc: Barebox List

On Thu, Feb 24, 2022 at 02:22:12PM +0100, Andrej Picej wrote:
> Hi Sascha,
> 
> sorry for responding this late, but we are still getting the same errors as
> before when using bareboximd with these three patches.
> 
> Are you sure we can revert this patch? The file can still be opened in
> bareboximd with mmap(), and then be written with this write_file(), which
> fails with the same error:
> 
> > Cannot write to barebox.bin: Bad address
> > CRC: write crc token to barebox.bin failed: -14
> > Bad address

Damn, my bad. It seems my brain was on vacation. I just created a fix
for this, you should be on Cc.

Sascha

-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


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

* Re: [PATCH 3/3] Revert "scripts/common: fix write_file when opened with mmap"
  2022-02-28 10:16     ` Sascha Hauer
@ 2022-02-28 11:40       ` Andrej Picej
  0 siblings, 0 replies; 6+ messages in thread
From: Andrej Picej @ 2022-02-28 11:40 UTC (permalink / raw)
  To: Sascha Hauer; +Cc: Barebox List



On 28. 02. 22 11:16, Sascha Hauer wrote:
> On Thu, Feb 24, 2022 at 02:22:12PM +0100, Andrej Picej wrote:
>> Hi Sascha,
>>
>> sorry for responding this late, but we are still getting the same errors as
>> before when using bareboximd with these three patches.
>>
>> Are you sure we can revert this patch? The file can still be opened in
>> bareboximd with mmap(), and then be written with this write_file(), which
>> fails with the same error:
>>
>>> Cannot write to barebox.bin: Bad address
>>> CRC: write crc token to barebox.bin failed: -14
>>> Bad address
> 
> Damn, my bad. It seems my brain was on vacation. I just created a fix
> for this, you should be on Cc.
> 

No worries, it happens :). Thanks for a quick fix. it works as intended :).

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


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

end of thread, other threads:[~2022-02-28 12:24 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-02-11  9:42 [PATCH 1/3] scripts/common: Do not mmap in read_file_2() Sascha Hauer
2022-02-11  9:42 ` [PATCH 2/3] scripts: bareboximd: Use mmap when possible Sascha Hauer
2022-02-11  9:42 ` [PATCH 3/3] Revert "scripts/common: fix write_file when opened with mmap" Sascha Hauer
2022-02-24 13:22   ` Andrej Picej
2022-02-28 10:16     ` Sascha Hauer
2022-02-28 11:40       ` Andrej Picej

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