DEBUG: shctx: name shared memory using vma_set_name()

In 98d22f212 ("MEDIUM: shctx: Naming shared memory context"), David
implemented prctl/PR_SET_VMA support to give a name to shctx maps when
supported. Maps were named after "HAProxy $name". It turns out that it
is not relevant to include "HAProxy" in the map name, given that we're
already looking at maps for a given PID (and here it's HAProxy's pid).

Instead, let's name shctx maps by making use of the new vma_set_name()
helper introduced by previous commit. Resulting maps will be named
"shctx:$name", e.g.: "shctx:globalCache", they will appear like this in
/proc/<pid>/maps:

7ec6aab0f000-7ec6ac000000 rw-s 00000000 00:01 405                        [anon_shmem:shctx:custom_name]
This commit is contained in:
Aurelien DARRAGON 2024-05-20 14:11:31 +02:00
parent 51a8f134ef
commit 6de0da1b54

View File

@ -16,9 +16,7 @@
#include <import/ebmbtree.h>
#include <haproxy/list.h>
#include <haproxy/shctx.h>
#if defined(USE_PRCTL)
#include <sys/prctl.h>
#endif
#include <haproxy/tools.h>
/*
* Reserve a new row if <first> is null, put it in the hotlist, set the refcount to 1
@ -295,30 +293,7 @@ int shctx_init(struct shared_context **orig_shctx, int maxblocks, int blocksize,
goto err;
}
#if defined(USE_PRCTL) && defined(PR_SET_VMA)
{
/**
* From Linux 5.17 (and if the `CONFIG_ANON_VMA_NAME` kernel config is set)`,
* anonymous regions can be named.
* We intentionally ignore errors as it should not jeopardize the memory context
* mapping whatsoever (e.g. older kernels).
*
* The naming can take up to 79 characters, accepting valid ASCII values
* except [, ], \, $ and '.
* As a result, when looking for /proc/<pid>/maps, we can see the anonymous range
* as follow :
* `7364c4fff000-736508000000 rw-s 00000000 00:01 3540 [anon_shmem:HAProxy globalCache]`
*/
int rn;
char fullname[80];
rn = snprintf(fullname, sizeof(fullname), "HAProxy %s", name);
if (rn >= 0) {
(void)prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, (uintptr_t)shctx,
totalsize, (uintptr_t)fullname);
}
}
#endif
vma_set_name(shctx, totalsize, "shctx", name);
shctx->nbav = 0;