[systemd-devel] systemd-coredump large memory allocation

Zbigniew Jędrzejewski-Szmek zbyszek at in.waw.pl
Wed Apr 24 13:57:48 PDT 2013


On Wed, Apr 24, 2013 at 02:41:05PM -0400, Colin Walters wrote:
> On Wed, 2013-04-24 at 16:22 +0200, Lennart Poettering wrote:
> 
> > Happy to take a patch that turns this into a realloc() loop.
> 
> Attached.  Booted and tested in gnome-ostree.
> 

> From 6c11bcf7c151dfa8511aab0dd6e545572a7597ec Mon Sep 17 00:00:00 2001
> From: Colin Walters <walters at verbum.org>
> Date: Wed, 24 Apr 2013 14:17:03 -0400
> Subject: [PATCH] coredump: use realloc() loop instead of malloc(768M)
> 
> I typically run VMs with 1024MiB allocated; systemd is unable to write
> coredumps in this scenario at all because the default kernel
> configuration will only overcommit 50% of available RAM.
> 
> Avoid this failure by using a realloc() loop.
Hi,
we actually have greedy_realloc to wrap realloc. Using it will simplify things
a bit.

Zbyszek


> See: http://lists.freedesktop.org/archives/systemd-devel/2013-April/010709.html
> ---
>  src/journal/coredump.c |   42 +++++++++++++++++++++++++++++++-----------
>  1 files changed, 31 insertions(+), 11 deletions(-)
> 
> diff --git a/src/journal/coredump.c b/src/journal/coredump.c
> index 2be6d94..74782df 100644
> --- a/src/journal/coredump.c
> +++ b/src/journal/coredump.c
> @@ -37,6 +37,8 @@
>  #include "special.h"
>  #include "cgroup-util.h"
>  
> +/* Few programs have less than 3MiB resident */
> +#define COREDUMP_MIN_START (3*1024*1024)
>  /* Make sure to not make this larger than the maximum journal entry
>   * size. See ENTRY_SIZE_MAX in journald-native.c. */
>  #define COREDUMP_MAX (768*1024*1024)
> @@ -103,9 +105,11 @@ int main(int argc, char* argv[]) {
>          uid_t uid;
>          gid_t gid;
>          struct iovec iovec[14];
> +        size_t coredump_bufsize;
> +        size_t coredump_size;
>          _cleanup_free_ char *core_pid = NULL, *core_uid = NULL, *core_gid = NULL, *core_signal = NULL,
>                  *core_timestamp = NULL, *core_comm = NULL, *core_exe = NULL, *core_unit = NULL,
> -                *core_session = NULL, *core_message = NULL, *core_cmdline = NULL, *p = NULL;
> +                *core_session = NULL, *core_message = NULL, *core_cmdline = NULL, *coredump_data = NULL;
>  
>          prctl(PR_SET_DUMPABLE, 0);
>  
> @@ -234,23 +238,39 @@ int main(int argc, char* argv[]) {
>                  goto finish;
>          }
>  
> -        p = malloc(9 + COREDUMP_MAX);
> -        if (!p) {
> +        coredump_size = 0;
> +        coredump_bufsize = COREDUMP_MIN_START + 9;
> +        coredump_data = malloc(coredump_bufsize);
> +        if (!coredump_data) {
>                  r = log_oom();
>                  goto finish;
>          }
>  
> -        memcpy(p, "COREDUMP=", 9);
> +        memcpy(coredump_data + coredump_size, "COREDUMP=", 9);
> +        coredump_size += 9;
>  
> -        n = loop_read(STDIN_FILENO, p + 9, COREDUMP_MAX, false);
> -        if (n < 0) {
> -                log_error("Failed to read core dump data: %s", strerror(-n));
> -                r = (int) n;
> -                goto finish;
> +        for (;;) {
> +                n = loop_read(STDIN_FILENO, coredump_data + coredump_size,
> +                              coredump_bufsize - coredump_size, false);
> +                if (n < 0) {
> +                        log_error("Failed to read core dump data: %s", strerror(-n));
> +                        r = (int) n;
> +                        goto finish;
> +                } else if (n == 0)
> +                        break;
> +
> +                coredump_size += n;
> +                if (coredump_size == coredump_bufsize) {
> +                        coredump_data = realloc(coredump_data, coredump_bufsize *= 2);
> +                        if (!coredump_data) {
> +                                r = log_oom();
> +                                goto finish;
> +                        }
> +                }
>          }
>  
> -        iovec[j].iov_base = p;
> -        iovec[j].iov_len = 9 + n;
> +        iovec[j].iov_base = coredump_data;
> +        iovec[j].iov_len = coredump_size;
>          j++;
>  
>          r = sd_journal_sendv(iovec, j);
> -- 
> 1.7.1
> 

> _______________________________________________
> systemd-devel mailing list
> systemd-devel at lists.freedesktop.org
> http://lists.freedesktop.org/mailman/listinfo/systemd-devel



More information about the systemd-devel mailing list