* [LTP] [PATCH v3] Migrating the libhugetlbfs/testcases/truncate_sigbus_versus_oom.c test
@ 2024-04-07 9:52 Samir Mulani
2024-09-04 10:15 ` Cyril Hrubis
0 siblings, 1 reply; 2+ messages in thread
From: Samir Mulani @ 2024-04-07 9:52 UTC (permalink / raw)
To: ltp; +Cc: Samir Mulani, rpalethorpe
In this test case, we are verifying the bug fix commit that is attached as
a part of the test case structure,
Some kernel have a bug in the positioning of the test against
i_size. This bug means that attempting to instantiate a page
beyond the end of a hugepage file can result in an OOM and SIGKILL
instead of the correct SIGBUS.
Signed-off-by: Samir Mulani <samir@linux.vnet.ibm.com>
---
v3:
--Addressed the below requested changes
1. Added volatile variable in signal handler to varify the SIGBUS signal in runtest method.
2. Fixed run test more times (-iN)
3. Removed number of required hugepages.
4. Corrected commit numberin struct tst_test.
5. Ran make check and fixed the issues.
---
runtest/hugetlb | 1 +
testcases/kernel/mem/.gitignore | 1 +
.../kernel/mem/hugetlb/hugemmap/hugemmap37.c | 103 ++++++++++++++++++
3 files changed, 105 insertions(+)
create mode 100644 testcases/kernel/mem/hugetlb/hugemmap/hugemmap37.c
diff --git a/runtest/hugetlb b/runtest/hugetlb
index 299c07ac9..7b7c44b77 100644
--- a/runtest/hugetlb
+++ b/runtest/hugetlb
@@ -35,6 +35,7 @@ hugemmap29 hugemmap29
hugemmap30 hugemmap30
hugemmap31 hugemmap31
hugemmap32 hugemmap32
+hugemmap37 hugemmap37
hugemmap05_1 hugemmap05 -m
hugemmap05_2 hugemmap05 -s
hugemmap05_3 hugemmap05 -s -m
diff --git a/testcases/kernel/mem/.gitignore b/testcases/kernel/mem/.gitignore
index c96fe8bfc..a74260689 100644
--- a/testcases/kernel/mem/.gitignore
+++ b/testcases/kernel/mem/.gitignore
@@ -34,6 +34,7 @@
/hugetlb/hugemmap/hugemmap30
/hugetlb/hugemmap/hugemmap31
/hugetlb/hugemmap/hugemmap32
+/hugetlb/hugemmap/hugemmap37
/hugetlb/hugeshmat/hugeshmat01
/hugetlb/hugeshmat/hugeshmat02
/hugetlb/hugeshmat/hugeshmat03
diff --git a/testcases/kernel/mem/hugetlb/hugemmap/hugemmap37.c b/testcases/kernel/mem/hugetlb/hugemmap/hugemmap37.c
new file mode 100644
index 000000000..5c55afe54
--- /dev/null
+++ b/testcases/kernel/mem/hugetlb/hugemmap/hugemmap37.c
@@ -0,0 +1,103 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2005-2006 David Gibson & Adam Litke, IBM Corporation.
+ */
+
+/*\
+ * [Description]
+ *
+ * Test Name: Truncate_sigbus_versus_oom
+ *
+ * Some kernel have a bug in the positioning of the test against
+ * i_size. This bug means that attempting to instantiate a page
+ * beyond the end of a hugepage file can result in an OOM and SIGKILL
+ * instead of the correct SIGBUS.
+ */
+
+#include <setjmp.h>
+#include <signal.h>
+#include "hugetlb.h"
+
+#define MNTPOINT "hugetlbfs/"
+#define PTS_PASS 0
+static int fd = -1, fdx = -1;
+
+static unsigned long long hpage_size;
+static unsigned long totpages;
+struct sigaction sa;
+static sigjmp_buf sig_escape;
+static volatile int test_pass;
+static int sigbus_count;
+
+static void sigbus_handler(int signum LTP_ATTRIBUTE_UNUSED)
+{
+ test_pass = 1;
+ siglongjmp(sig_escape, 17);
+}
+
+static void run_test(void)
+{
+ void *p, *q;
+ unsigned long i;
+
+ sigbus_count = 0;
+ test_pass = 0;
+
+ fd = tst_creat_unlinked(MNTPOINT, 0);
+ p = SAFE_MMAP(NULL, hpage_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
+ SAFE_FTRUNCATE(fd, 0);
+
+ fdx = tst_creat_unlinked(MNTPOINT, 0);
+ totpages = SAFE_READ_MEMINFO(MEMINFO_HPAGE_FREE);
+ q = SAFE_MMAP(NULL, totpages * hpage_size, PROT_READ|PROT_WRITE, MAP_SHARED, fdx, 0);
+ /* Touch the pages to ensure they're removed from the pool */
+ for (i = 0; i < totpages; i++) {
+ volatile char *x = (volatile char *)q + i*hpage_size;
+ *x = 0;
+ }
+ /* SIGBUS is what *should* happen */
+ SAFE_FTRUNCATE(fdx, 0);
+ if (sigsetjmp(sig_escape, 1) == 0)
+ *((volatile unsigned int *)p);
+ else
+ sigbus_count++;
+
+ if (sigbus_count != 1)
+ tst_res(TFAIL, "Didn't SIGBUS");
+
+ if (test_pass == 1)
+ tst_res(TPASS, "Expected SIGBUS triggered");
+}
+
+void setup(void)
+{
+ sa.sa_flags = SA_SIGINFO;
+ sa.sa_handler = sigbus_handler;
+ SAFE_SIGACTION(SIGBUS, &sa, NULL);
+ totpages = SAFE_READ_MEMINFO(MEMINFO_HPAGE_FREE);
+ hpage_size = tst_get_hugepage_size();
+}
+
+void cleanup(void)
+{
+ if (fd > 0)
+ SAFE_CLOSE(fd);
+ if (fdx > 0)
+ SAFE_CLOSE(fdx);
+}
+
+
+static struct tst_test test = {
+ .tags = (struct tst_tag[]) {
+ {"linux-git", "ebed4bfc8da8"},
+ {}
+ },
+ .needs_root = 1,
+ .mntpoint = MNTPOINT,
+ .needs_hugetlbfs = 1,
+ .needs_tmpdir = 1,
+ .setup = setup,
+ .cleanup = cleanup,
+ .test_all = run_test,
+ .hugepages = {1, TST_NEEDS},
+};
--
2.43.0
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [LTP] [PATCH v3] Migrating the libhugetlbfs/testcases/truncate_sigbus_versus_oom.c test
2024-04-07 9:52 [LTP] [PATCH v3] Migrating the libhugetlbfs/testcases/truncate_sigbus_versus_oom.c test Samir Mulani
@ 2024-09-04 10:15 ` Cyril Hrubis
0 siblings, 0 replies; 2+ messages in thread
From: Cyril Hrubis @ 2024-09-04 10:15 UTC (permalink / raw)
To: Samir Mulani; +Cc: rpalethorpe, ltp
Hi!
> Some kernel have a bug in the positioning of the test against
> i_size. This bug means that attempting to instantiate a page
> beyond the end of a hugepage file can result in an OOM and SIGKILL
> instead of the correct SIGBUS.
>
> Signed-off-by: Samir Mulani <samir@linux.vnet.ibm.com>
> ---
> v3:
> --Addressed the below requested changes
> 1. Added volatile variable in signal handler to varify the SIGBUS signal in runtest method.
> 2. Fixed run test more times (-iN)
> 3. Removed number of required hugepages.
> 4. Corrected commit numberin struct tst_test.
> 5. Ran make check and fixed the issues.
> ---
> runtest/hugetlb | 1 +
> testcases/kernel/mem/.gitignore | 1 +
> .../kernel/mem/hugetlb/hugemmap/hugemmap37.c | 103 ++++++++++++++++++
> 3 files changed, 105 insertions(+)
> create mode 100644 testcases/kernel/mem/hugetlb/hugemmap/hugemmap37.c
>
> diff --git a/runtest/hugetlb b/runtest/hugetlb
> index 299c07ac9..7b7c44b77 100644
> --- a/runtest/hugetlb
> +++ b/runtest/hugetlb
> @@ -35,6 +35,7 @@ hugemmap29 hugemmap29
> hugemmap30 hugemmap30
> hugemmap31 hugemmap31
> hugemmap32 hugemmap32
> +hugemmap37 hugemmap37
> hugemmap05_1 hugemmap05 -m
> hugemmap05_2 hugemmap05 -s
> hugemmap05_3 hugemmap05 -s -m
> diff --git a/testcases/kernel/mem/.gitignore b/testcases/kernel/mem/.gitignore
> index c96fe8bfc..a74260689 100644
> --- a/testcases/kernel/mem/.gitignore
> +++ b/testcases/kernel/mem/.gitignore
> @@ -34,6 +34,7 @@
> /hugetlb/hugemmap/hugemmap30
> /hugetlb/hugemmap/hugemmap31
> /hugetlb/hugemmap/hugemmap32
> +/hugetlb/hugemmap/hugemmap37
> /hugetlb/hugeshmat/hugeshmat01
> /hugetlb/hugeshmat/hugeshmat02
> /hugetlb/hugeshmat/hugeshmat03
> diff --git a/testcases/kernel/mem/hugetlb/hugemmap/hugemmap37.c b/testcases/kernel/mem/hugetlb/hugemmap/hugemmap37.c
> new file mode 100644
> index 000000000..5c55afe54
> --- /dev/null
> +++ b/testcases/kernel/mem/hugetlb/hugemmap/hugemmap37.c
> @@ -0,0 +1,103 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +/*
> + * Copyright (C) 2005-2006 David Gibson & Adam Litke, IBM Corporation.
> + */
> +
> +/*\
> + * [Description]
> + *
> + * Test Name: Truncate_sigbus_versus_oom
Please drop the test name here as well.
> + * Some kernel have a bug in the positioning of the test against
> + * i_size. This bug means that attempting to instantiate a page
> + * beyond the end of a hugepage file can result in an OOM and SIGKILL
> + * instead of the correct SIGBUS.
> + */
> +
> +#include <setjmp.h>
> +#include <signal.h>
> +#include "hugetlb.h"
> +
> +#define MNTPOINT "hugetlbfs/"
> +#define PTS_PASS 0
This macro is unused now.
> +static int fd = -1, fdx = -1;
> +
> +static unsigned long long hpage_size;
> +static unsigned long totpages;
> +struct sigaction sa;
> +static sigjmp_buf sig_escape;
> +static volatile int test_pass;
> +static int sigbus_count;
> +
> +static void sigbus_handler(int signum LTP_ATTRIBUTE_UNUSED)
> +{
> + test_pass = 1;
> + siglongjmp(sig_escape, 17);
> +}
> +
> +static void run_test(void)
> +{
> + void *p, *q;
> + unsigned long i;
> +
> + sigbus_count = 0;
> + test_pass = 0;
> +
> + fd = tst_creat_unlinked(MNTPOINT, 0);
> + p = SAFE_MMAP(NULL, hpage_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
> + SAFE_FTRUNCATE(fd, 0);
> +
> + fdx = tst_creat_unlinked(MNTPOINT, 0);
> + totpages = SAFE_READ_MEMINFO(MEMINFO_HPAGE_FREE);
> + q = SAFE_MMAP(NULL, totpages * hpage_size, PROT_READ|PROT_WRITE, MAP_SHARED, fdx, 0);
> + /* Touch the pages to ensure they're removed from the pool */
> + for (i = 0; i < totpages; i++) {
> + volatile char *x = (volatile char *)q + i*hpage_size;
> + *x = 0;
> + }
> + /* SIGBUS is what *should* happen */
> + SAFE_FTRUNCATE(fdx, 0);
> + if (sigsetjmp(sig_escape, 1) == 0)
> + *((volatile unsigned int *)p);
> + else
> + sigbus_count++;
I still think that this is actually useless, we already set a flag in
the signal handler so we can drop the whole sigbus_count logic from the
test and it could be simplified as:
if (segsetjmp(sig_escape, 1) == 0)
*((voilatile unsigned int *)p);
if (test_pass)
tst_res(TPASS, ...);
else
tst_res(TFAIL, ...);
I mean we are not going to get to the else branch that increments the
sigbus_count from anywhere else than the signal handler which already
set the test_pass, so the values of test_pass and sigbus_count will be
always the same.
> + if (sigbus_count != 1)
> + tst_res(TFAIL, "Didn't SIGBUS");
> +
> + if (test_pass == 1)
> + tst_res(TPASS, "Expected SIGBUS triggered");
> +}
> +
> +void setup(void)
> +{
> + sa.sa_flags = SA_SIGINFO;
> + sa.sa_handler = sigbus_handler;
> + SAFE_SIGACTION(SIGBUS, &sa, NULL);
> + totpages = SAFE_READ_MEMINFO(MEMINFO_HPAGE_FREE);
> + hpage_size = tst_get_hugepage_size();
> +}
> +
> +void cleanup(void)
> +{
> + if (fd > 0)
> + SAFE_CLOSE(fd);
> + if (fdx > 0)
> + SAFE_CLOSE(fdx);
> +}
> +
> +
> +static struct tst_test test = {
> + .tags = (struct tst_tag[]) {
> + {"linux-git", "ebed4bfc8da8"},
> + {}
> + },
> + .needs_root = 1,
> + .mntpoint = MNTPOINT,
> + .needs_hugetlbfs = 1,
> + .needs_tmpdir = 1,
> + .setup = setup,
> + .cleanup = cleanup,
> + .test_all = run_test,
> + .hugepages = {1, TST_NEEDS},
> +};
> --
> 2.43.0
>
>
> --
> Mailing list info: https://lists.linux.it/listinfo/ltp
--
Cyril Hrubis
chrubis@suse.cz
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2024-09-04 10:16 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-04-07 9:52 [LTP] [PATCH v3] Migrating the libhugetlbfs/testcases/truncate_sigbus_versus_oom.c test Samir Mulani
2024-09-04 10:15 ` Cyril Hrubis
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox