diff mbox series

[v3] selftests: cgroup: add tests to verify the zswap writeback path

Message ID 20240507101443.720190-1-usamaarif642@gmail.com (mailing list archive)
State New
Headers show
Series [v3] selftests: cgroup: add tests to verify the zswap writeback path | expand

Commit Message

Usama Arif May 7, 2024, 10:14 a.m. UTC
Initate writeback with the below steps and check using
memory.stat.zswpwb if zswap writeback occurred:
1. Allocate memory.
2. Reclaim memory equal to the amount that was allocated in step 1.
   This will move it into zswap.
3. Save current zswap usage.
4. Move the memory allocated in step 1 back in from zswap.
5. Set zswap.max to half the amount that was recorded in step 3.
6. Attempt to reclaim memory equal to the amount that was allocated,
   this will either trigger writeback if its enabled, or reclamation
   will fail if writeback is disabled as there isn't enough zswap
   space.

Suggested-by: Nhat Pham <nphamcs@gmail.com>
Signed-off-by: Usama Arif <usamaarif642@gmail.com>
---

v2 -> v3:
- Remove memory.max (Yosry Ahmed)
- change from random allocation of memory to increasing and 0 allocation
  (Yosry Ahmed)
- stricter error checking when writeback is disabled (Yosry Ahmed)
- Ensure zswpwb_before == 0 (Yosry Ahmed)
- Variable definition reorder, function name change (Yosry Ahmed)

v1 -> v2:
- Change method of causing writeback from limit zswap to memory reclaim.
  (Further described in commit message) (Yosry Ahmed)
- Document why using random memory (Nhat Pham)
---
 tools/testing/selftests/cgroup/test_zswap.c | 138 +++++++++++++++++++-
 1 file changed, 137 insertions(+), 1 deletion(-)

Comments

Yosry Ahmed May 7, 2024, 6:28 p.m. UTC | #1
On Tue, May 7, 2024 at 3:14 AM Usama Arif <usamaarif642@gmail.com> wrote:
>
> Initate writeback with the below steps and check using

s/Initate/Initiate

> memory.stat.zswpwb if zswap writeback occurred:
> 1. Allocate memory.
> 2. Reclaim memory equal to the amount that was allocated in step 1.
>    This will move it into zswap.
> 3. Save current zswap usage.
> 4. Move the memory allocated in step 1 back in from zswap.
> 5. Set zswap.max to half the amount that was recorded in step 3.
> 6. Attempt to reclaim memory equal to the amount that was allocated,
>    this will either trigger writeback if its enabled, or reclamation

s/its/it's

>    will fail if writeback is disabled as there isn't enough zswap
>    space.
>
> Suggested-by: Nhat Pham <nphamcs@gmail.com>
> Signed-off-by: Usama Arif <usamaarif642@gmail.com>
> ---
>
> v2 -> v3:
> - Remove memory.max (Yosry Ahmed)
> - change from random allocation of memory to increasing and 0 allocation
>   (Yosry Ahmed)
> - stricter error checking when writeback is disabled (Yosry Ahmed)
> - Ensure zswpwb_before == 0 (Yosry Ahmed)
> - Variable definition reorder, function name change (Yosry Ahmed)
>
> v1 -> v2:
> - Change method of causing writeback from limit zswap to memory reclaim.
>   (Further described in commit message) (Yosry Ahmed)
> - Document why using random memory (Nhat Pham)
> ---
>  tools/testing/selftests/cgroup/test_zswap.c | 138 +++++++++++++++++++-
>  1 file changed, 137 insertions(+), 1 deletion(-)
>
> diff --git a/tools/testing/selftests/cgroup/test_zswap.c b/tools/testing/selftests/cgroup/test_zswap.c
> index f0e488ed90d8..805301554fa8 100644
> --- a/tools/testing/selftests/cgroup/test_zswap.c
> +++ b/tools/testing/selftests/cgroup/test_zswap.c
> @@ -50,7 +50,7 @@ static int get_zswap_stored_pages(size_t *value)
>         return read_int("/sys/kernel/debug/zswap/stored_pages", value);
>  }
>
> -static int get_cg_wb_count(const char *cg)
> +static long get_cg_wb_count(const char *cg)
>  {
>         return cg_read_key_long(cg, "memory.stat", "zswpwb");
>  }
> @@ -248,6 +248,140 @@ static int test_zswapin(const char *root)
>         return ret;
>  }
>
> +/*
> + * Attempt writeback with the following steps:
> + * 1. Allocate memory.
> + * 2. Reclaim memory equal to the amount that was allocated in step 1.
> +      This will move it into zswap.
> + * 3. Save current zswap usage.
> + * 4. Move the memory allocated in step 1 back in from zswap.
> + * 5. Set zswap.max to half the amount that was recorded in step 3.
> + * 6. Attempt to reclaim memory equal to the amount that was allocated,
> +      this will either trigger writeback if its enabled, or reclamation
> +      will fail if writeback is disabled as there isn't enough zswap space.
> + */
> +static int attempt_writeback(const char *cgroup, void *arg)
> +{
> +       long pagesize = sysconf(_SC_PAGESIZE);
> +       char *test_group = arg;
> +       size_t memsize = MB(4);
> +       long zswap_usage;
> +       bool wb_enabled;
> +       int ret = -1;
> +       char memval;
> +       char *mem;
> +
> +       wb_enabled = cg_read_long(test_group, "memory.zswap.writeback");
> +       mem = (char *)malloc(memsize);
> +       if (!mem)
> +               return ret;
> +
> +       /*
> +        * Fill half of each page with increasing data, and keep other
> +        * half empty, this will result in data that is still compressible
> +        * and ends up in zswap, with material zswap usage.
> +        */
> +       for (int i = 0; i < memsize; i += pagesize) {
> +               memval = 1;
> +               for (int j = i; j < i + pagesize/2; j++) {
> +                       /* Overflow of memval is ok, will check with same values later */
> +                       mem[j] = memval++;
> +               }
> +       }

It may be more readable if you fill a fixed page-sized buffer with the
pattern, then use memcpy() to fill the whole region with it. You can
also use memcmp() when validating the data below to compare each page
against it.

> +
> +       /* Try and reclaim allocated memory */
> +       if (cg_write(test_group, "memory.reclaim", "4M")) {

Should we use cg_write_numeric() and memsize here instead of the
hardcoded value?

> +               ksft_print_msg("Failed to reclaim all of the requested memory\n");
> +               goto out;
> +       }
> +
> +       zswap_usage = cg_read_long(test_group, "memory.zswap.current");
> +
> +       /* zswpin */
> +       for (int i = 0; i < memsize; i += pagesize) {
> +               memval = 1;
> +               for (int j = i; j < i + pagesize/2; j++) {
> +                       if (mem[j] != memval++) {
> +                               ksft_print_msg("invalid memory\n");
> +                               goto out;
> +                       }
> +               }
> +       }
> +
> +       if (cg_write_numeric(test_group, "memory.zswap.max", zswap_usage/2))
> +               goto out;
> +
> +       /*
> +        * If writeback is enabled, trying to reclaim memory now will trigger a
> +        * writeback as zswap.max is half of what was needed when reclaim ran the first time.
> +        * If writeback is disabled, memory reclaim will fail as zswap is limited and
> +        * it can't writeback to swap.
> +        */
> +       ret = cg_write(test_group, "memory.reclaim", "4M");
> +       if (!wb_enabled) {
> +               if (ret == -EAGAIN)
> +                       ret = 0;
> +               else
> +                       ret = -1;
> +       }

nit: This can be more compact

if (!wb_enabled)
       ret = (ret == -EAGAIN) ? 0 : -1;

> +
> +out:
> +       free(mem);
> +       return ret;
> +}
> +
> +/* Test to verify the zswap writeback path */
> +static int test_zswap_writeback(const char *root, bool wb)
> +{
> +       long zswpwb_before, zswpwb_after;
> +       int ret = KSFT_FAIL;
> +       char *test_group;
> +
> +       test_group = cg_name(root, "zswap_writeback_test");
> +       if (!test_group)
> +               goto out;
> +       if (cg_create(test_group))
> +               goto out;
> +       if (cg_write(test_group, "memory.zswap.writeback", wb ? "1" : "0"))
> +               goto out;
> +
> +       zswpwb_before = get_cg_wb_count(test_group);
> +       if (zswpwb_before != 0) {
> +               ksft_print_msg("failed to get zswpwb_before\n");

A negative value means that the read failed. A positive value means
that writeback occurred outside of our control. Let's use a generic
message here instead that zswpwb_before was $value instead of 0.

> +               goto out;
> +       }
> +
> +       if (cg_run(test_group, attempt_writeback, (void *) test_group))
> +               goto out;
> +
> +       /* Verify that zswap writeback occurred only if writeback was enabled */
> +       zswpwb_after = get_cg_wb_count(test_group);
> +       if (wb && zswpwb_after <= zswpwb_before) {
> +               ksft_print_msg("zswpwb_after <= zswpwb_before\n");
> +               goto out;
> +       } else if (!wb && zswpwb_after) {
> +               ksft_print_msg("zswpwb_after != zswpwb_before\n");
> +               goto out;
> +       }

I think we should split the check for a negative zswpwb_after, which
means that the read failed, from the check that the value makes sense
based on whether writeback is enabled or not. Also, we do not need two
separate checks here for the correctness of zswpwb_after. Perhaps
something like:

if (zswpwb_after < 0)
        goto out;

if (wb != !!zswpwb_after) {
        ksft_print_msg("zswpwb_after is %ld while wb is %s",
zswpwb_after, wb ? "enabled" : "disabled");
       goto out;
}

> +
> +       ret = KSFT_PASS;
> +
> +out:
> +       cg_destroy(test_group);
> +       free(test_group);
> +       return ret;
> +}
> +
> +static int test_zswap_writeback_enabled(const char *root)
> +{
> +       return test_zswap_writeback(root, true);
> +}
> +
> +static int test_zswap_writeback_disabled(const char *root)
> +{
> +       return test_zswap_writeback(root, false);
> +}
> +
>  /*
>   * 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
> @@ -425,6 +559,8 @@ struct zswap_test {
>         T(test_zswap_usage),
>         T(test_swapin_nozswap),
>         T(test_zswapin),
> +       T(test_zswap_writeback_enabled),
> +       T(test_zswap_writeback_disabled),
>         T(test_no_kmem_bypass),
>         T(test_no_invasive_cgroup_shrink),
>  };
> --
> 2.43.0
>
>
diff mbox series

Patch

diff --git a/tools/testing/selftests/cgroup/test_zswap.c b/tools/testing/selftests/cgroup/test_zswap.c
index f0e488ed90d8..805301554fa8 100644
--- a/tools/testing/selftests/cgroup/test_zswap.c
+++ b/tools/testing/selftests/cgroup/test_zswap.c
@@ -50,7 +50,7 @@  static int get_zswap_stored_pages(size_t *value)
 	return read_int("/sys/kernel/debug/zswap/stored_pages", value);
 }
 
-static int get_cg_wb_count(const char *cg)
+static long get_cg_wb_count(const char *cg)
 {
 	return cg_read_key_long(cg, "memory.stat", "zswpwb");
 }
@@ -248,6 +248,140 @@  static int test_zswapin(const char *root)
 	return ret;
 }
 
+/*
+ * Attempt writeback with the following steps:
+ * 1. Allocate memory.
+ * 2. Reclaim memory equal to the amount that was allocated in step 1.
+      This will move it into zswap.
+ * 3. Save current zswap usage.
+ * 4. Move the memory allocated in step 1 back in from zswap.
+ * 5. Set zswap.max to half the amount that was recorded in step 3.
+ * 6. Attempt to reclaim memory equal to the amount that was allocated,
+      this will either trigger writeback if its enabled, or reclamation
+      will fail if writeback is disabled as there isn't enough zswap space.
+ */
+static int attempt_writeback(const char *cgroup, void *arg)
+{
+	long pagesize = sysconf(_SC_PAGESIZE);
+	char *test_group = arg;
+	size_t memsize = MB(4);
+	long zswap_usage;
+	bool wb_enabled;
+	int ret = -1;
+	char memval;
+	char *mem;
+
+	wb_enabled = cg_read_long(test_group, "memory.zswap.writeback");
+	mem = (char *)malloc(memsize);
+	if (!mem)
+		return ret;
+
+	/*
+	 * Fill half of each page with increasing data, and keep other
+	 * half empty, this will result in data that is still compressible
+	 * and ends up in zswap, with material zswap usage.
+	 */
+	for (int i = 0; i < memsize; i += pagesize) {
+		memval = 1;
+		for (int j = i; j < i + pagesize/2; j++) {
+			/* Overflow of memval is ok, will check with same values later */
+			mem[j] = memval++;
+		}
+	}
+
+	/* Try and reclaim allocated memory */
+	if (cg_write(test_group, "memory.reclaim", "4M")) {
+		ksft_print_msg("Failed to reclaim all of the requested memory\n");
+		goto out;
+	}
+
+	zswap_usage = cg_read_long(test_group, "memory.zswap.current");
+
+	/* zswpin */
+	for (int i = 0; i < memsize; i += pagesize) {
+		memval = 1;
+		for (int j = i; j < i + pagesize/2; j++) {
+			if (mem[j] != memval++) {
+				ksft_print_msg("invalid memory\n");
+				goto out;
+			}
+		}
+	}
+
+	if (cg_write_numeric(test_group, "memory.zswap.max", zswap_usage/2))
+		goto out;
+
+	/*
+	 * If writeback is enabled, trying to reclaim memory now will trigger a
+	 * writeback as zswap.max is half of what was needed when reclaim ran the first time.
+	 * If writeback is disabled, memory reclaim will fail as zswap is limited and
+	 * it can't writeback to swap.
+	 */
+	ret = cg_write(test_group, "memory.reclaim", "4M");
+	if (!wb_enabled) {
+		if (ret == -EAGAIN)
+			ret = 0;
+		else
+			ret = -1;
+	}
+
+out:
+	free(mem);
+	return ret;
+}
+
+/* Test to verify the zswap writeback path */
+static int test_zswap_writeback(const char *root, bool wb)
+{
+	long zswpwb_before, zswpwb_after;
+	int ret = KSFT_FAIL;
+	char *test_group;
+
+	test_group = cg_name(root, "zswap_writeback_test");
+	if (!test_group)
+		goto out;
+	if (cg_create(test_group))
+		goto out;
+	if (cg_write(test_group, "memory.zswap.writeback", wb ? "1" : "0"))
+		goto out;
+
+	zswpwb_before = get_cg_wb_count(test_group);
+	if (zswpwb_before != 0) {
+		ksft_print_msg("failed to get zswpwb_before\n");
+		goto out;
+	}
+
+	if (cg_run(test_group, attempt_writeback, (void *) test_group))
+		goto out;
+
+	/* Verify that zswap writeback occurred only if writeback was enabled */
+	zswpwb_after = get_cg_wb_count(test_group);
+	if (wb && zswpwb_after <= zswpwb_before) {
+		ksft_print_msg("zswpwb_after <= zswpwb_before\n");
+		goto out;
+	} else if (!wb && zswpwb_after) {
+		ksft_print_msg("zswpwb_after != zswpwb_before\n");
+		goto out;
+	}
+
+	ret = KSFT_PASS;
+
+out:
+	cg_destroy(test_group);
+	free(test_group);
+	return ret;
+}
+
+static int test_zswap_writeback_enabled(const char *root)
+{
+	return test_zswap_writeback(root, true);
+}
+
+static int test_zswap_writeback_disabled(const char *root)
+{
+	return test_zswap_writeback(root, false);
+}
+
 /*
  * 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
@@ -425,6 +559,8 @@  struct zswap_test {
 	T(test_zswap_usage),
 	T(test_swapin_nozswap),
 	T(test_zswapin),
+	T(test_zswap_writeback_enabled),
+	T(test_zswap_writeback_disabled),
 	T(test_no_kmem_bypass),
 	T(test_no_invasive_cgroup_shrink),
 };