diff mbox series

[v3] Hugetlb: Test to detect bug with freeing gigantic hugetlb pages

Message ID 20230419114144.1219505-1-tsahu@linux.ibm.com
State Accepted
Headers show
Series [v3] Hugetlb: Test to detect bug with freeing gigantic hugetlb pages | expand

Commit Message

Tarun Sahu April 19, 2023, 11:41 a.m. UTC
Before kernel version 5.10-rc7, there was a bug that resulted in a
"Bad Page State" error when freeing gigantic hugepages. This happened
because the struct page entry compound_nr, which overlapped with
page->mapping in the first tail page, was not cleared, causing the
error. To ensure that this issue does not reoccur as struct page keeps
changes and some fields are managed by folio, this test checks that
freeing gigantic hugepages does not produce the above-mentioned error.

Signed-off-by: Tarun Sahu <tsahu@linux.ibm.com>
---
 .../kernel/mem/hugetlb/hugemmap/hugemmap32.c  | 83 +++++++++++++++++++
 testcases/kernel/mem/hugetlb/lib/hugetlb.h    |  6 ++
 2 files changed, 89 insertions(+)
 create mode 100644 testcases/kernel/mem/hugetlb/hugemmap/hugemmap32.c

Comments

Avinesh Kumar April 19, 2023, 3:38 p.m. UTC | #1
Hi Tarun,

You are missing the runtest/hugetlb and .gitignore entries for this new test.


--
Regards,
Avinesh
Tarun Sahu April 19, 2023, 6 p.m. UTC | #2
> Hi Tarun,
>
> You are missing the runtest/hugetlb and .gitignore entries for this new test.
>
Hi Avinesh,

Thanks for pointing this out.

Will soon send the update.

~Tarun

>
> --
> Regards,
> Avinesh
>
>
>
> -- 
> Mailing list info: https://lists.linux.it/listinfo/ltp
Cyril Hrubis April 20, 2023, 9:15 a.m. UTC | #3
Hi!
I've added a .gitignore and runtest entry for the test and pushed,
(please do not forget about that next time)
thanks.
Tarun Sahu April 20, 2023, 6:50 p.m. UTC | #4
Hi Cyril,

> Hi!
> I've added a .gitignore and runtest entry for the test and pushed,
> (please do not forget about that next time)
Sure, Will keep in mind. Thanks.

~Tarun

> thanks.
>
> -- 
> Cyril Hrubis
> chrubis@suse.cz
>
> -- 
> Mailing list info: https://lists.linux.it/listinfo/ltp
diff mbox series

Patch

diff --git a/testcases/kernel/mem/hugetlb/hugemmap/hugemmap32.c b/testcases/kernel/mem/hugetlb/hugemmap/hugemmap32.c
new file mode 100644
index 000000000..34b322bfa
--- /dev/null
+++ b/testcases/kernel/mem/hugetlb/hugemmap/hugemmap32.c
@@ -0,0 +1,83 @@ 
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2023, IBM Corporation.
+ * Author: Tarun Sahu
+ */
+
+/*\
+ * [Description]
+ *
+ * Before kernel version 5.10-rc7, there was a bug that resulted in a "Bad Page
+ * State" error when freeing gigantic hugepages. This happened because the
+ * struct page entry compound_nr, which overlapped with page->mapping in the
+ * first tail page, was not cleared, causing the error. To ensure that this
+ * issue does not reoccur as struct page keeps changing and some fields are
+ * managed by folio, this test checks that freeing gigantic hugepages does not
+ * produce the above-mentioned error.
+ */
+
+#define _GNU_SOURCE
+#include <dirent.h>
+
+#include <stdio.h>
+
+#include "hugetlb.h"
+
+#define PATH_HUGEPAGE "/sys/kernel/mm/hugepages"
+#define GIGANTIC_MIN_ORDER 10
+
+static int org_g_hpages;
+static char g_hpage_path[4096];
+
+static void run_test(void)
+{
+	if (FILE_PRINTF(g_hpage_path, "%d", 1))
+		tst_brk(TCONF, "Can't update the gigantic hugepages.");
+	SAFE_FILE_PRINTF(g_hpage_path, "%d", 0);
+
+	if (tst_taint_check())
+		tst_res(TFAIL, "Freeing Gigantic pages resulted in Bad Page State bug.");
+	else
+		tst_res(TPASS, "Successfully freed the gigantic hugepages");
+}
+
+static void setup(void)
+{
+	DIR *dir;
+	struct dirent *ent;
+	unsigned long hpage_size;
+
+	dir = SAFE_OPENDIR(PATH_HUGEPAGE);
+	while ((ent = SAFE_READDIR(dir))) {
+		if ((sscanf(ent->d_name, "hugepages-%lukB", &hpage_size) == 1) &&
+			is_hugetlb_gigantic(hpage_size * 1024)) {
+			sprintf(g_hpage_path, "%s/%s/%s", PATH_HUGEPAGE,
+					ent->d_name, "nr_hugepages");
+			break;
+		}
+	}
+	if (!g_hpage_path[0])
+		tst_brk(TCONF, "Gigantic hugepages not supported");
+
+	SAFE_CLOSEDIR(dir);
+	SAFE_FILE_LINES_SCANF(g_hpage_path, "%d", &org_g_hpages);
+}
+
+static void cleanup(void)
+{
+	if (g_hpage_path[0])
+		SAFE_FILE_PRINTF(g_hpage_path, "%d", org_g_hpages);
+}
+
+static struct tst_test test = {
+	.tags = (struct tst_tag[]) {
+	    {"linux-git", "ba9c1201beaa"},
+	    {"linux-git", "a01f43901cfb"},
+	    {}
+	},
+	.needs_root = 1,
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = run_test,
+	.taint_check = TST_TAINT_B,
+};
diff --git a/testcases/kernel/mem/hugetlb/lib/hugetlb.h b/testcases/kernel/mem/hugetlb/lib/hugetlb.h
index 241dab708..34fe08c24 100644
--- a/testcases/kernel/mem/hugetlb/lib/hugetlb.h
+++ b/testcases/kernel/mem/hugetlb/lib/hugetlb.h
@@ -39,6 +39,12 @@ 
 # endif
 #endif
 
+/* Check if hugetlb page is gigantic */
+static inline int is_hugetlb_gigantic(unsigned long hpage_size)
+{
+	return (hpage_size / getpagesize()) >> 11;
+}
+
 /*
  * to get the lower nine permission bits
  * from shmid_ds.ipc_perm.mode