diff mbox series

Hugetlb: Migrating libhugetlbfs test truncate_reserve_wraparound.c

Message ID 20230928074624.67065-1-geetika@linux.ibm.com
State Changes Requested
Headers show
Series Hugetlb: Migrating libhugetlbfs test truncate_reserve_wraparound.c | expand

Commit Message

Geetika Sept. 28, 2023, 7:46 a.m. UTC
Test Description:
Test to verify the Hugepages_Rsvd before and after mmap, truncate and sigbus operations.
At one stage, improper handling of tests against i_size could mess
up accounting of reserved hugepages on certain truncate operations.

Signed-off-by: Geetika <geetika@linux.ibm.com>
---
 runtest/hugetlb                               |   1 +
 testcases/kernel/mem/.gitignore               |   1 +
 .../kernel/mem/hugetlb/hugemmap/hugemmap38.c  | 124 ++++++++++++++++++
 3 files changed, 126 insertions(+)
 create mode 100644 testcases/kernel/mem/hugetlb/hugemmap/hugemmap38.c

Comments

Petr Vorel Nov. 28, 2023, 11:25 a.m. UTC | #1
Hi,

Please have look at my comments at your colleague patch [1] [2], these changes
apply a lot for your patches as well.

[1] https://lore.kernel.org/ltp/20231128111024.GA364870@pevik/
[2] https://lore.kernel.org/ltp/20231128112254.GA367506@pevik/

...
> +++ b/runtest/hugetlb
> @@ -35,6 +35,7 @@ hugemmap29 hugemmap29
>  hugemmap30 hugemmap30
>  hugemmap31 hugemmap31
>  hugemmap32 hugemmap32
> +hugemmap38 hugemmap38
nit: Why not hugemmap34? (your colleague used hugemmap33 for his patch [1].

>  hugemmap05_1 hugemmap05 -m
>  hugemmap05_2 hugemmap05 -s
>  hugemmap05_3 hugemmap05 -s -m
...

Kind regards,
Petr
Geetika March 27, 2024, 5:22 a.m. UTC | #2
On 28/11/23 4:55 pm, Petr Vorel wrote:
> Hi,
>
> Please have look at my comments at your colleague patch [1] [2], these changes
> apply a lot for your patches as well.
>
> [1] https://lore.kernel.org/ltp/20231128111024.GA364870@pevik/
> [2] https://lore.kernel.org/ltp/20231128112254.GA367506@pevik/

I will have a look at both of these and apply the changes that apply to 
this
testcase and send the next version.

> ...
>> +++ b/runtest/hugetlb
>> @@ -35,6 +35,7 @@ hugemmap29 hugemmap29
>>   hugemmap30 hugemmap30
>>   hugemmap31 hugemmap31
>>   hugemmap32 hugemmap32
>> +hugemmap38 hugemmap38
> nit: Why not hugemmap34? (your colleague used hugemmap33 for his patch [1].
We already have:

hugemmap34 : 
https://lore.kernel.org/ltp/20230913082214.8453-1-samir@linux.vnet.ibm.com/
hugemmap36 : 
https://lore.kernel.org/ltp/20230912064126.3350-1-samir@linux.vnet.ibm.com/
hugemmap35 : 
https://lore.kernel.org/all/20230908121135.245296-1-sachinpb@linux.vnet.ibm.com/
hugemmap37 : 
https://lore.kernel.org/ltp/20230929075435.72172-1-geetika@linux.ibm.com/

Hence hugemmap38!
>>   hugemmap05_1 hugemmap05 -m
>>   hugemmap05_2 hugemmap05 -s
>>   hugemmap05_3 hugemmap05 -s -m
> ...
>
> Kind regards,
> Petr

Thanks & Regards,
Geetika
diff mbox series

Patch

diff --git a/runtest/hugetlb b/runtest/hugetlb
index 299c07ac9..2ffd35aec 100644
--- a/runtest/hugetlb
+++ b/runtest/hugetlb
@@ -35,6 +35,7 @@  hugemmap29 hugemmap29
 hugemmap30 hugemmap30
 hugemmap31 hugemmap31
 hugemmap32 hugemmap32
+hugemmap38 hugemmap38
 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 7258489ed..346bf9533 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/hugemmap38
 /hugetlb/hugeshmat/hugeshmat01
 /hugetlb/hugeshmat/hugeshmat02
 /hugetlb/hugeshmat/hugeshmat03
diff --git a/testcases/kernel/mem/hugetlb/hugemmap/hugemmap38.c b/testcases/kernel/mem/hugetlb/hugemmap/hugemmap38.c
new file mode 100644
index 000000000..2de6aa4da
--- /dev/null
+++ b/testcases/kernel/mem/hugetlb/hugemmap/hugemmap38.c
@@ -0,0 +1,124 @@ 
+// SPDX-License-Identifier: LGPL-2.1-or-later
+/*
+ * Copyright (C) 2005-2006 David Gibson & Adam Litke, IBM Corporation.
+ * Copyright (C) 2006 Hugh Dickins <hugh@veritas.com>
+ */
+
+/*\
+ *[Descripiton]
+ *
+ * At one stage, improper handling of tests against i_size could mess
+ * up accounting of reserved hugepages on certain truncate
+ * operations.
+ *
+ */
+
+#include <signal.h>
+#include <setjmp.h>
+#include "hugetlb.h"
+
+#define MNTPOINT "hugetlbfs/"
+#define RANDOM_CONSTANT 0x1234ABCD
+
+static long hpage_size;
+static int fd = -1;
+static int sigbus_count;
+static unsigned long initial_rsvd, after_map_rsvd, after_touch_rsvd;
+static unsigned long after_trunc_rsvd, after_unmap_rsvd, after_sigbus_rsvd;
+static volatile unsigned int *q;
+
+
+static sigjmp_buf sig_escape;
+
+static void sigbus_handler(int signum, siginfo_t *si, void *uc)
+{
+	siglongjmp(sig_escape, 17);
+}
+
+static void run_test(void)
+{
+	void *p;
+	struct sigaction sa = {
+	.sa_sigaction = sigbus_handler,
+	.sa_flags = SA_SIGINFO,
+	};
+
+	sigbus_count = 0;
+
+	initial_rsvd = SAFE_READ_MEMINFO(MEMINFO_HPAGE_RSVD);
+	tst_res(TINFO, "Reserve count before map: %lu\n", initial_rsvd);
+
+	p = SAFE_MMAP(NULL, hpage_size, PROT_READ|PROT_WRITE, MAP_SHARED,
+			fd, 0);
+	q = p;
+
+	after_map_rsvd = SAFE_READ_MEMINFO(MEMINFO_HPAGE_RSVD);
+	tst_res(TINFO, "Reserve count after map: %lu\n", after_map_rsvd);
+
+	*q = 0;
+	after_touch_rsvd = SAFE_READ_MEMINFO(MEMINFO_HPAGE_RSVD);
+	tst_res(TINFO, "Reserve count after touch: %lu\n", after_touch_rsvd);
+
+	SAFE_FTRUNCATE(fd, 0);
+	after_trunc_rsvd = SAFE_READ_MEMINFO(MEMINFO_HPAGE_RSVD);
+	tst_res(TINFO, "Reserve count after truncate: %lu\n", after_trunc_rsvd);
+
+	if (after_trunc_rsvd != initial_rsvd) {
+		tst_res(TFAIL, "Reserved count is not restored after truncate: %lu instead of %lu", after_trunc_rsvd, initial_rsvd);
+		goto windup;
+	}
+
+	SAFE_SIGACTION(SIGBUS, &sa, NULL);
+	if (sigsetjmp(sig_escape, 1) == 0)
+		*q; /* Fault, triggering a SIGBUS */
+	else
+		sigbus_count++;
+
+	if (sigbus_count != 1) {
+		tst_res(TFAIL, "Didn't SIGBUS after truncate");
+		goto windup;
+	}
+
+	after_sigbus_rsvd = SAFE_READ_MEMINFO(MEMINFO_HPAGE_RSVD);
+	tst_res(TINFO, "Reserve count after truncate: %lu\n", after_sigbus_rsvd);
+
+	if (after_trunc_rsvd != initial_rsvd) {
+		tst_res(TFAIL, "Reserved count is altered by SIGBUS fault: %lu instead of %lu", after_sigbus_rsvd, initial_rsvd);
+		goto windup;
+	}
+
+	tst_res(TPASS, "Test passed!");
+
+windup:
+	SAFE_MUNMAP(p, hpage_size);
+	after_unmap_rsvd = SAFE_READ_MEMINFO(MEMINFO_HPAGE_RSVD);
+	tst_res(TINFO, "Reserve count after munmap: %lu\n", after_unmap_rsvd);
+
+}
+
+static void setup(void)
+{
+	hpage_size = tst_get_hugepage_size();
+	fd = tst_creat_unlinked(MNTPOINT, 0);
+}
+
+static void cleanup(void)
+{
+	if (fd >= 0)
+		SAFE_CLOSE(fd);
+}
+
+static struct tst_test test = {
+	.tags = (struct tst_tag[]) {
+		{"linux-git", "ebed4bfc8da8"},
+		{}
+	},
+	.needs_root = 1,
+	.mntpoint = MNTPOINT,
+	.needs_hugetlbfs = 1,
+	.hugepages = {4, TST_NEEDS},
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = run_test,
+};
+