systemd/journald-advice-about-use-of-memory.patch

147 lines
5.7 KiB
Diff
Raw Normal View History

---
src/journal/catalog.c | 18 +++++++++++++++++-
src/journal/journal-authenticate.c | 4 ++--
src/journal/journal-file.c | 2 +-
src/journal/journald-kmsg.c | 2 +-
src/journal/mmap-cache.c | 22 +++++++++++++++++++++-
5 files changed, 42 insertions(+), 6 deletions(-)
Index: systemd-218/src/journal/catalog.c
===================================================================
--- systemd-218.orig/src/journal/catalog.c
+++ systemd-218/src/journal/catalog.c
@@ -471,9 +471,19 @@ finish:
static int open_mmap(const char *database, int *_fd, struct stat *_st, void **_p) {
const CatalogHeader *h;
+ static const struct {
+ const int index;
+ int advise;
+ } advises[] = {
+ {0,MADV_WILLNEED},
+ {1,MADV_SEQUENTIAL},
+ {2,MADV_DONTDUMP},
+ {3,MADV_DONTFORK}
+ };
int fd;
void *p;
struct stat st;
+ size_t psize;
assert(_fd);
assert(_st);
@@ -493,11 +503,17 @@ static int open_mmap(const char *databas
return -EINVAL;
}
- p = mmap(NULL, PAGE_ALIGN(st.st_size), PROT_READ, MAP_SHARED, fd, 0);
+ psize = PAGE_ALIGN(st.st_size);
+ p = mmap(NULL, psize, PROT_READ, MAP_SHARED|MAP_POPULATE|MAP_NONBLOCK, fd, 0);
if (p == MAP_FAILED) {
safe_close(fd);
return -errno;
}
+ for (size_t n = 0; n < sizeof(advises) / sizeof(*advises); ++n) {
+ int r = madvise(p, psize, advises[n].advise);
+ if (r < 0)
+ log_warning("Failed to give advice about use of memory: %m");
+ }
h = p;
if (memcmp(h->signature, CATALOG_SIGNATURE, sizeof(h->signature)) != 0 ||
Index: systemd-218/src/journal/journal-authenticate.c
===================================================================
--- systemd-218.orig/src/journal/journal-authenticate.c
+++ systemd-218/src/journal/journal-authenticate.c
@@ -355,7 +355,7 @@ int journal_file_fss_load(JournalFile *f
goto finish;
}
- m = mmap(NULL, PAGE_ALIGN(sizeof(FSSHeader)), PROT_READ, MAP_SHARED, fd, 0);
+ m = mmap(NULL, PAGE_ALIGN(sizeof(FSSHeader)), PROT_READ, MAP_SHARED|MAP_STACK, fd, 0);
if (m == MAP_FAILED) {
m = NULL;
r = -errno;
@@ -399,7 +399,7 @@ int journal_file_fss_load(JournalFile *f
goto finish;
}
- f->fss_file = mmap(NULL, PAGE_ALIGN(f->fss_file_size), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
+ f->fss_file = mmap(NULL, PAGE_ALIGN(f->fss_file_size), PROT_READ|PROT_WRITE, MAP_SHARED|MAP_STACK, fd, 0);
if (f->fss_file == MAP_FAILED) {
f->fss_file = NULL;
r = -errno;
Index: systemd-218/src/journal/journal-file.c
===================================================================
--- systemd-218.orig/src/journal/journal-file.c
+++ systemd-218/src/journal/journal-file.c
@@ -2559,7 +2559,7 @@ int journal_file_open(
goto fail;
}
- f->header = mmap(NULL, PAGE_ALIGN(sizeof(Header)), prot_from_flags(flags), MAP_SHARED, f->fd, 0);
+ f->header = mmap(NULL, PAGE_ALIGN(sizeof(Header)), prot_from_flags(flags), MAP_SHARED|MAP_STACK, f->fd, 0);
if (f->header == MAP_FAILED) {
f->header = NULL;
r = -errno;
Index: systemd-218/src/journal/journald-kmsg.c
===================================================================
--- systemd-218.orig/src/journal/journald-kmsg.c
+++ systemd-218/src/journal/journald-kmsg.c
@@ -455,7 +455,7 @@ int server_open_kernel_seqnum(Server *s)
return 0;
}
- p = mmap(NULL, sizeof(uint64_t), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
+ p = mmap(NULL, sizeof(uint64_t), PROT_READ|PROT_WRITE, MAP_SHARED|MAP_STACK, fd, 0);
if (p == MAP_FAILED) {
log_error_errno(errno, "Failed to map sequential number file, ignoring: %m");
return 0;
Index: systemd-218/src/journal/mmap-cache.c
===================================================================
--- systemd-218.orig/src/journal/mmap-cache.c
+++ systemd-218/src/journal/mmap-cache.c
@@ -446,6 +446,15 @@ static int add_mmap(
void **ret,
void **release_cookie) {
+ static const struct {
+ const int index;
+ int vise;
+ } ad[] = {
+ {0, MADV_WILLNEED},
+ {1, MADV_SEQUENTIAL},
+ {2, MADV_DONTDUMP},
+ {3, MADV_DONTFORK}
+ };
uint64_t woffset, wsize;
Context *c;
FileDescriptor *f;
@@ -488,7 +497,7 @@ static int add_mmap(
}
for (;;) {
- d = mmap(NULL, wsize, prot, MAP_SHARED, fd, woffset);
+ d = mmap(NULL, wsize, prot, MAP_SHARED|MAP_POPULATE|MAP_NONBLOCK, fd, woffset);
if (d != MAP_FAILED)
break;
if (errno != ENOMEM)
@@ -501,6 +510,17 @@ static int add_mmap(
return -ENOMEM;
}
+ for (size_t n = 0; n < sizeof(ad) / sizeof(*ad); ++n) {
+ if (ad[n].vise == MADV_DONTFORK) {
+ int flags = fcntl(fd, F_GETFD);
+ if (flags < 0 || !(flags & FD_CLOEXEC))
+ continue;
+ }
+ r = madvise(d, wsize, ad[n].vise);
+ if (r < 0)
+ log_warning("Failed to give advice about use of memory: %m");
+ }
+
c = context_add(m, context);
if (!c)
goto outofmem;