linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Nhat Pham <nphamcs@gmail.com>
To: akpm@linux-foundation.org
Cc: shuah@kernel.org, hannes@cmpxchg.org, yosryahmed@google.com,
	tj@kernel.org, lizefan.x@bytedance.com, linux-mm@kvack.org,
	kernel-team@meta.com, linux-kernel@vger.kernel.org,
	cgroups@vger.kernel.org, linux-kselftest@vger.kernel.org
Subject: [PATCH 3/3] selftests: add test for zswapin
Date: Mon, 29 Jan 2024 14:45:42 -0800	[thread overview]
Message-ID: <20240129224542.162599-4-nphamcs@gmail.com> (raw)
In-Reply-To: <20240129224542.162599-1-nphamcs@gmail.com>

We recently encountered a kernel crash on the zswapin path in our
internal kernel, which went undetected because of a lack of test
coverage for this path. Add a selftest to cover this code path,
allocating more memories than the cgroup limit to trigger
swapout/zswapout, then reading the pages back in memories several times.

Also add a variant of this test that runs with zswap disabled, to verify
swapin correctness as well.

Suggested-by: Rik van Riel <riel@surriel.com>
Signed-off-by: Nhat Pham <nphamcs@gmail.com>
---
 tools/testing/selftests/cgroup/test_zswap.c | 67 ++++++++++++++++++++-
 1 file changed, 65 insertions(+), 2 deletions(-)

diff --git a/tools/testing/selftests/cgroup/test_zswap.c b/tools/testing/selftests/cgroup/test_zswap.c
index 32ce975b21d1..86231c86dc89 100644
--- a/tools/testing/selftests/cgroup/test_zswap.c
+++ b/tools/testing/selftests/cgroup/test_zswap.c
@@ -60,17 +60,39 @@ static long get_zswpout(const char *cgroup)
 	return cg_read_key_long(cgroup, "memory.stat", "zswpout ");
 }
 
-static int allocate_bytes(const char *cgroup, void *arg)
+static int allocate_bytes_and_read(const char *cgroup, void *arg, bool read)
 {
 	size_t size = (size_t)arg;
 	char *mem = (char *)malloc(size);
+	int ret = 0;
 
 	if (!mem)
 		return -1;
 	for (int i = 0; i < size; i += 4095)
 		mem[i] = 'a';
+
+	if (read) {
+		/* cycle through the allocated memory to (z)swap in and out pages */
+		for (int t = 0; t < 5; t++) {
+			for (int i = 0; i < size; i += 4095) {
+				if (mem[i] != 'a')
+					ret = -1;
+			}
+		}
+	}
+
 	free(mem);
-	return 0;
+	return ret;
+}
+
+static int allocate_bytes(const char *cgroup, void *arg)
+{
+	return allocate_bytes_and_read(cgroup, arg, false);
+}
+
+static int read_bytes(const char *cgroup, void *arg)
+{
+	return allocate_bytes_and_read(cgroup, arg, true);
 }
 
 static char *setup_test_group_1M(const char *root, const char *name)
@@ -133,6 +155,45 @@ static int test_zswap_usage(const char *root)
 	return ret;
 }
 
+/* Simple test to verify the (z)swapin code paths */
+static int test_zswapin_size(const char *root, char *zswap_size)
+{
+	int ret = KSFT_FAIL;
+	char *test_group;
+
+	/* Set up */
+	test_group = cg_name(root, "zswapin_test");
+	if (!test_group)
+		goto out;
+	if (cg_create(test_group))
+		goto out;
+	if (cg_write(test_group, "memory.max", "8M"))
+		goto out;
+	if (cg_write(test_group, "memory.zswap.max", zswap_size))
+		goto out;
+
+	/* Allocate and read more than memory.max to trigger (z)swap in */
+	if (cg_run(test_group, read_bytes, (void *)MB(32)))
+		goto out;
+
+	ret = KSFT_PASS;
+
+out:
+	cg_destroy(test_group);
+	free(test_group);
+	return ret;
+}
+
+static int test_swapin(const char *root)
+{
+	return test_zswapin_size(root, "0");
+}
+
+static int test_zswapin_no_limit(const char *root)
+{
+	return test_zswapin_size(root, "max");
+}
+
 /*
  * When trying to store a memcg page in zswap, if the memcg hits its memory
  * limit in zswap, writeback should affect only the zswapped pages of that
@@ -309,6 +370,8 @@ struct zswap_test {
 	const char *name;
 } tests[] = {
 	T(test_zswap_usage),
+	T(test_swapin),
+	T(test_zswapin_no_limit),
 	T(test_no_kmem_bypass),
 	T(test_no_invasive_cgroup_shrink),
 };
-- 
2.39.3



  parent reply	other threads:[~2024-01-29 22:45 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-29 22:45 [PATCH 0/3] fix and extend zswap kselftests Nhat Pham
2024-01-29 22:45 ` [PATCH 1/3] selftests: zswap: add zswap selftest file to zswap maintainer entry Nhat Pham
2024-01-30  1:02   ` Yosry Ahmed
2024-01-30 18:37     ` Nhat Pham
2024-01-30 18:55       ` Yosry Ahmed
2024-01-29 22:45 ` [PATCH 2/3] selftests: fix the zswap invasive shrink test Nhat Pham
2024-01-30  1:05   ` Yosry Ahmed
2024-01-29 22:45 ` Nhat Pham [this message]
2024-01-30  1:24   ` [PATCH 3/3] selftests: add test for zswapin Yosry Ahmed
2024-01-30 18:31     ` Nhat Pham
2024-01-30 18:54       ` Yosry Ahmed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240129224542.162599-4-nphamcs@gmail.com \
    --to=nphamcs@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=cgroups@vger.kernel.org \
    --cc=hannes@cmpxchg.org \
    --cc=kernel-team@meta.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=lizefan.x@bytedance.com \
    --cc=shuah@kernel.org \
    --cc=tj@kernel.org \
    --cc=yosryahmed@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).