Re: [PATCH 3/3] selftests: add test for zswapin

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Mon, Jan 29, 2024 at 02:45:42PM -0800, Nhat Pham wrote:
> 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

s/memories/memory

> 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@xxxxxxxxxxx>
> Signed-off-by: Nhat Pham <nphamcs@xxxxxxxxx>
> ---
>  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++) {

What benefit does the iteration serve here? I would guess one iteration
is enough to swap everything in at least once, no?

> +			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);
>  }

I don't like how we reuse allocate_bytes_and_read(), we are not saving
much. Let's keep allocate_bytes() as-is and add a separate helper. Also,
I think allocate_and_read_bytes() is easier to read.

>  
>  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");
> +}

Why are we testing the no zswap case? I am all for testing but it seems
out of scope here. It would have been understandable if we are testing
memory.zswap.max itself, but we are not doing that.

FWIW, I think the tests here should really be separated from cgroup
tests, but I understand why they were added here. There is a lot of
testing for memcg interface and control for zswap, and a lot of nice
helpers present.





[Index of Archives]     [Linux ARM Kernel]     [Linux ARM]     [Linux Omap]     [Fedora ARM]     [IETF Annouce]     [Security]     [Bugtraq]     [Linux OMAP]     [Linux MIPS]     [eCos]     [Asterisk Internet PBX]     [Linux API]     [Monitors]

  Powered by Linux