From: Sascha Hauer <s.hauer@pengutronix.de>
To: Barebox List <barebox@lists.infradead.org>
Subject: [PATCH] bareboximd: Use mmap when possibly
Date: Wed, 14 Apr 2021 15:46:19 +0200 [thread overview]
Message-ID: <20210414134619.8022-1-s.hauer@pengutronix.de> (raw)
It's hard to believe but there are systems out there that are so sparse
with memory that they can't afford 1MiB of RAM to read a file into.
Use mmap when possible and fall back to reading into an allocated buffer
otherwise.
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
common/imd.c | 6 ++++-
scripts/bareboximd.c | 53 ++++++++++++++++++++++++++++----------------
2 files changed, 39 insertions(+), 20 deletions(-)
diff --git a/common/imd.c b/common/imd.c
index aff3b00b6b..e1d5733c6b 100644
--- a/common/imd.c
+++ b/common/imd.c
@@ -17,6 +17,10 @@ int imd_command_setenv(const char *variable_name, const char *value)
return -ENOSYS;
}
#endif
+static inline void read_file_2_free(void *buf)
+{
+ free(buf);
+}
#endif
/*
@@ -542,6 +546,6 @@ int imd_command(int argc, char *argv[])
ret = 0;
out:
- free(buf);
+ read_file_2_free(buf);
return ret;
}
diff --git a/scripts/bareboximd.c b/scripts/bareboximd.c
index 9558c77d1a..c3dcb4dcf0 100644
--- a/scripts/bareboximd.c
+++ b/scripts/bareboximd.c
@@ -15,6 +15,7 @@
#include <stdarg.h>
#include <linux/err.h>
#include <linux/kernel.h>
+#include <sys/mman.h>
#include "../include/image-metadata.h"
@@ -96,29 +97,35 @@ static int read_file_2(const char *filename, size_t *size, void **outbuf, size_t
goto close;
}
- buf = malloc(max_size);
- if (!buf) {
- fprintf(stderr, "Cannot allocate memory\n");
- ret = -ENOMEM;
- goto close;
- }
+ buf = mmap(NULL, max_size, PROT_READ, MAP_SHARED, fd, 0);
+ if (buf == MAP_FAILED ) {
+ buf = malloc(max_size);
+ if (!buf) {
+ fprintf(stderr, "Cannot allocate memory\n");
+ ret = -ENOMEM;
+ goto close;
+ }
- *outbuf = buf;
- while (*size < max_size) {
- rsize = read(fd, buf, max_size-*size);
- if (rsize == 0) {
- ret = -EIO;
- goto free;
- } else if (rsize < 0) {
- if (errno == EAGAIN)
- continue;
- else {
+ *outbuf = buf;
+
+ while (*size < max_size) {
+ rsize = read(fd, buf, max_size - *size);
+ if (rsize == 0) {
+ ret = -EIO;
+ goto free;
+ }
+
+ if (rsize < 0) {
ret = -errno;
goto free;
}
- } /* ret > 0 */
- buf += rsize;
- *size += rsize;
+
+ buf += rsize;
+ *size += rsize;
+ }
+ } else {
+ *outbuf = buf;
+ *size = max_size;
}
ret = 0;
@@ -131,6 +138,14 @@ close:
return ret;
}
+static inline void read_file_2_free(void *buf)
+{
+ /*
+ * Can't free() here because buffer might be mmapped. No need
+ * to do anything as we are exitting in a moment anyway.
+ */
+}
+
static unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
{
return strtoul(cp, endp, base);
--
2.29.2
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
reply other threads:[~2021-04-14 13:48 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20210414134619.8022-1-s.hauer@pengutronix.de \
--to=s.hauer@pengutronix.de \
--cc=barebox@lists.infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox