From: Aurelien DARRAGON Date: Fri, 17 May 2024 08:54:54 +0000 (+0200) Subject: BUG/MEDIUM: fd: prevent memory waste in fdtab array X-Git-Tag: v3.0-dev12~22 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=b9915a745e1c19852bc2abbee8076ed877d358e8;p=thirdparty%2Fhaproxy.git BUG/MEDIUM: fd: prevent memory waste in fdtab array In 97ea9c49f1 ("BUG/MEDIUM: fd: always align fdtab[] to 64 bytes"), the patch doesn't do what the message says. The intent was only to align the base fdtab addr on 64 bytes so that all fdtab entries are aligned and thus don't share the same cache line. For that, fdtab pointer is adjusted from fdtab_addr (unaligned) address after it is allocated. Thus, all we need is an extra 64 bytes in the fdtab_addr array for the aligment. Because we use calloc() to perform the allocation, a dumb mistake was made: the '+64' was added on calloc argument, which means EACH fdtab entry is allocated with 64 extra bytes. Given that a single fdtab entry is 64 bytes, since 97ea9c49f1 each fdtab entry now takes 128 bytes! We doubled fdtab memory consumption. To give you an idea, on my laptop, when looking at memory consumption using 'ps -p `pidof haproxy` -o size' right after starting haproxy process with default settings (no maxsock enforced): before 97ea9c49f1: -> 118440 (KB, ~= 118MB) after 97ea9c49f1: -> 183976 (KB, ~= 184MB) To fix this, use calloc with 1 and manually provide the size with as we would do if we used malloc(). With this patch, we're back to pre-97ea9c49f1 for fdtab memory consumption (with 64 extra bytes the whole array, which is insignificant). It should be backported to all stable versions. --- diff --git a/src/fd.c b/src/fd.c index 0feebfce67..feaed87336 100644 --- a/src/fd.c +++ b/src/fd.c @@ -1158,7 +1158,7 @@ int init_pollers() int p; struct poller *bp; - if ((fdtab_addr = calloc(global.maxsock, sizeof(*fdtab) + 64)) == NULL) { + if ((fdtab_addr = calloc(1, global.maxsock * sizeof(*fdtab) + 64)) == NULL) { ha_alert("Not enough memory to allocate %d entries for fdtab!\n", global.maxsock); goto fail_tab; }