Re: [PATCH v3 29/54] selftests/mm: vm_util: add helpers to set and restore shm limits

Sarthak Sharma
2026-05-06T12:03:12Z

On 4/29/26 2:12 AM, Mike Rapoport wrote:
> From: "Mike Rapoport (Microsoft)" <rppt@kernel.org>
>
> hugetlb-shm and thuge-gen tests require that limits defined by
> /proc/sys/kernel/{shmmax,shmall} should be higher than certain values.
>
> Add helpers that allow setting these limits and restoring their settings on
> a test exit.
>
> They will be used later in hugetlb-shm and thuge-gen.
>
> Signed-off-by: Mike Rapoport (Microsoft) <rppt@kernel.org>

Looks good to me.

Reviewed-by: Sarthak Sharma <sarthak.sharma@arm.com>

> ---
> tools/testing/selftests/mm/vm_util.c | 28 ++++++++++++++++++++++++++++
> tools/testing/selftests/mm/vm_util.h | 9 +++++++++
> 2 files changed, 37 insertions(+)
>
> diff --git a/tools/testing/selftests/mm/vm_util.c b/tools/testing/selftests/mm/vm_util.c
> index 752566f75c0b..5a89c2d903be 100644
> --- a/tools/testing/selftests/mm/vm_util.c
> +++ b/tools/testing/selftests/mm/vm_util.c
> @@ -761,3 +761,31 @@ void write_num(const char *path, unsigned long num)
> sprintf(buf, "%lu", num);
> write_file(path, buf, strlen(buf) + 1);
> }
> +
> +static unsigned long shmall, shmmax;
> +
> +void __shm_limits_restore(void)
> +{
> + if (shmmax)
> + write_num("/proc/sys/kernel/shmmax", shmmax);
> + if (shmall)
> + write_num("/proc/sys/kernel/shmall", shmall);
> +}
> +
> +void shm_limits_prepare(unsigned long length)
> +{
> + unsigned long nr = length / psize();
> + unsigned long val;
> +
> + val = read_num("/proc/sys/kernel/shmmax");
> + if (val < length) {
> + write_num("/proc/sys/kernel/shmmax", length);
> + shmmax = val;
> + }
> +
> + val = read_num("/proc/sys/kernel/shmall");
> + if (val < nr) {
> + write_num("/proc/sys/kernel/shmall", nr);
> + shmall = val;
> + }
> +}
> diff --git a/tools/testing/selftests/mm/vm_util.h b/tools/testing/selftests/mm/vm_util.h
> index 5fc9707f6b9a..ea8fc8fdf0eb 100644
> --- a/tools/testing/selftests/mm/vm_util.h
> +++ b/tools/testing/selftests/mm/vm_util.h
> @@ -168,3 +168,12 @@ void write_file(const char *path, const char *buf, size_t buflen);
> int read_file(const char *path, char *buf, size_t buflen);
> unsigned long read_num(const char *path);
> void write_num(const char *path, unsigned long num);
> +
> +void shm_limits_prepare(unsigned long length);
> +void __shm_limits_restore(void);
> +
> +#define SHM_LIMITS_RESTORE() \
> +static void __attribute__((destructor)) shm_limits_restore(void) \
> +{ \
> + __shm_limits_restore(); \
> +}