diff mbox series

[v2] mmap_24-1: Change vm.max_map_count if needed

Message ID 20250509113206.59574-1-mdoucha@suse.cz
State Accepted
Headers show
Series [v2] mmap_24-1: Change vm.max_map_count if needed | expand

Checks

Context Check Description
ltpci/debian_stable_s390x-linux-gnu-gcc_s390x success success
ltpci/debian_stable_aarch64-linux-gnu-gcc_arm64 success success
ltpci/debian_stable_powerpc64le-linux-gnu-gcc_ppc64el success success
ltpci/debian_stable_gcc success success
ltpci/debian_stable_gcc success success
ltpci/ubuntu_bionic_gcc success success
ltpci/debian_testing_gcc success success
ltpci/debian_oldstable_clang success success
ltpci/alpine_latest_gcc success success
ltpci/ubuntu_jammy_gcc success success
ltpci/debian_testing_clang success success
ltpci/quay-io-centos-centos_stream9_gcc success success
ltpci/opensuse-archive_42-2_gcc success success
ltpci/opensuse-leap_latest_gcc success success
ltpci/fedora_latest_clang success success
ltpci/debian_oldstable_gcc success success

Commit Message

Martin Doucha May 9, 2025, 11:32 a.m. UTC
If vm.max_map_count system parameter is too high, mmap24-1 may get
killed by OOM. Set the parameter to a reasonable low value so that
mmap() quickly fails as expected.

Signed-off-by: Martin Doucha <mdoucha@suse.cz>
Reviewed-by: Petr Vorel <pvorel@suse.cz>
Reviewed-by: Cyril Hrubis <chrubis@suse.cz>
---

Changes since v1:
- Print warning if procfile cannot be opened
- Use fopen(..., "r") instead of "r+" in main()

 .../conformance/interfaces/mmap/24-1.c        | 37 ++++++++++++++++++-
 1 file changed, 36 insertions(+), 1 deletion(-)

Comments

Li Wang May 9, 2025, 12:08 p.m. UTC | #1
On Fri, May 9, 2025 at 7:32 PM Martin Doucha <mdoucha@suse.cz> wrote:
>
> If vm.max_map_count system parameter is too high, mmap24-1 may get
> killed by OOM. Set the parameter to a reasonable low value so that
> mmap() quickly fails as expected.
>
> Signed-off-by: Martin Doucha <mdoucha@suse.cz>
> Reviewed-by: Petr Vorel <pvorel@suse.cz>
> Reviewed-by: Cyril Hrubis <chrubis@suse.cz>
> ---
>
> Changes since v1:
> - Print warning if procfile cannot be opened
> - Use fopen(..., "r") instead of "r+" in main()
>
>  .../conformance/interfaces/mmap/24-1.c        | 37 ++++++++++++++++++-
>  1 file changed, 36 insertions(+), 1 deletion(-)
>
> diff --git a/testcases/open_posix_testsuite/conformance/interfaces/mmap/24-1.c b/testcases/open_posix_testsuite/conformance/interfaces/mmap/24-1.c
> index 6cc8349e1..49673d603 100644
> --- a/testcases/open_posix_testsuite/conformance/interfaces/mmap/24-1.c
> +++ b/testcases/open_posix_testsuite/conformance/interfaces/mmap/24-1.c

As I pointed in the previous thread, we should update the code
comments to explicitly state that the failure is caused by exceeding
the maximum number of memory mappings (vm.max_map_count),
rather than overall memory exhaustion.

Without this clarification, readers may be misled by the current comments
and mistakenly assume that the code is intended to test total memory
exhaustion, which is not the case.

Anyway, patch itself looks good.
And I vote for merging this before a new release.

Reviewed-by: Li Wang <liwang@redhat.com>

> @@ -31,12 +31,42 @@
>  #include <stdint.h>
>  #include "posixtest.h"
>
> +#define MAX_MAP_COUNT_PATH "/proc/sys/vm/max_map_count"
> +#define MAP_COUNT_LIMIT 65530
> +
> +void proc_write_val(const char *path, size_t val)
> +{
> +       FILE *procfile;
> +
> +       procfile = fopen(path, "r+");
> +
> +       if (!procfile) {
> +               printf("Warning: Could not open %s\n", path);
> +               return;
> +       }
> +
> +       fprintf(procfile, "%zu", val);
> +       fclose(procfile);
> +}
> +
>  int main(void)
>  {
>         char tmpfname[256];
>         void *pa;
> -       size_t len;
> +       size_t len, max_map_count = 0;
>         int fd;
> +       FILE *procfile;
> +
> +       /* Change vm.max_map_count to avoid OOM */
> +       procfile = fopen(MAX_MAP_COUNT_PATH, "r");
> +
> +       if (procfile) {
> +               fscanf(procfile, "%zu", &max_map_count);
> +               fclose(procfile);
> +       }
> +
> +       if (max_map_count > MAP_COUNT_LIMIT)
> +               proc_write_val(MAX_MAP_COUNT_PATH, MAP_COUNT_LIMIT);
>
>         /* Size of the shared memory object */
>         size_t shm_size = 1024;
> @@ -78,5 +108,10 @@ int main(void)
>
>         close(fd);
>         printf("Test FAILED: Did not get ENOMEM as expected\n");
> +
> +       /* Restore original vm.max_map_count */
> +       if (max_map_count > MAP_COUNT_LIMIT)
> +               proc_write_val(MAX_MAP_COUNT_PATH, max_map_count);
> +
>         return PTS_FAIL;
>  }
> --
> 2.49.0
>
>
> --
> Mailing list info: https://lists.linux.it/listinfo/ltp
>
Petr Vorel May 12, 2025, 1:28 p.m. UTC | #2
Hi all,

merged, thank you!
@Li, please merge your follow up fixing comment.

Kind regards,
Petr
diff mbox series

Patch

diff --git a/testcases/open_posix_testsuite/conformance/interfaces/mmap/24-1.c b/testcases/open_posix_testsuite/conformance/interfaces/mmap/24-1.c
index 6cc8349e1..49673d603 100644
--- a/testcases/open_posix_testsuite/conformance/interfaces/mmap/24-1.c
+++ b/testcases/open_posix_testsuite/conformance/interfaces/mmap/24-1.c
@@ -31,12 +31,42 @@ 
 #include <stdint.h>
 #include "posixtest.h"
 
+#define MAX_MAP_COUNT_PATH "/proc/sys/vm/max_map_count"
+#define MAP_COUNT_LIMIT 65530
+
+void proc_write_val(const char *path, size_t val)
+{
+	FILE *procfile;
+
+	procfile = fopen(path, "r+");
+
+	if (!procfile) {
+		printf("Warning: Could not open %s\n", path);
+		return;
+	}
+
+	fprintf(procfile, "%zu", val);
+	fclose(procfile);
+}
+
 int main(void)
 {
 	char tmpfname[256];
 	void *pa;
-	size_t len;
+	size_t len, max_map_count = 0;
 	int fd;
+	FILE *procfile;
+
+	/* Change vm.max_map_count to avoid OOM */
+	procfile = fopen(MAX_MAP_COUNT_PATH, "r");
+
+	if (procfile) {
+		fscanf(procfile, "%zu", &max_map_count);
+		fclose(procfile);
+	}
+
+	if (max_map_count > MAP_COUNT_LIMIT)
+		proc_write_val(MAX_MAP_COUNT_PATH, MAP_COUNT_LIMIT);
 
 	/* Size of the shared memory object */
 	size_t shm_size = 1024;
@@ -78,5 +108,10 @@  int main(void)
 
 	close(fd);
 	printf("Test FAILED: Did not get ENOMEM as expected\n");
+
+	/* Restore original vm.max_map_count */
+	if (max_map_count > MAP_COUNT_LIMIT)
+		proc_write_val(MAX_MAP_COUNT_PATH, max_map_count);
+
 	return PTS_FAIL;
 }