Re: [PATCH v3 3/3] selftests: add zswapin and no zswap tests

From: Nhat Pham
Date: Mon Feb 05 2024 - 18:25:55 EST


On Mon, Feb 5, 2024 at 3:05 PM Yosry Ahmed <yosryahmed@xxxxxxxxxx> wrote:
>
> On Mon, Feb 05, 2024 at 02:56:08PM -0800, Nhat Pham wrote:
> > Add a selftest to cover the zswapin code path, allocating more memory
> > than the cgroup limit to trigger swapout/zswapout, then reading the
> > pages back in memory several times. This is inspired by a recently
> > encountered 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 verify that when memory.zswap.max = 0, no pages can go
> > to the zswap pool for the cgroup.
> >
> > Suggested-by: Rik van Riel <riel@xxxxxxxxxxx>
> > Suggested-by: Yosry Ahmed <yosryahmed@xxxxxxxxxx>
> > Signed-off-by: Nhat Pham <nphamcs@xxxxxxxxx>
>
> LGTM with a few nits below:
> Acked-by: Yosry Ahmed <yosryahmed@xxxxxxxxxx>
>
> Thanks!
>
> > ---
> > tools/testing/selftests/cgroup/test_zswap.c | 120 +++++++++++++++++++-
> > 1 file changed, 119 insertions(+), 1 deletion(-)
> >
> > diff --git a/tools/testing/selftests/cgroup/test_zswap.c b/tools/testing/selftests/cgroup/test_zswap.c
> > index 32ce975b21d1..c263610a4a60 100644
> > --- a/tools/testing/selftests/cgroup/test_zswap.c
> > +++ b/tools/testing/selftests/cgroup/test_zswap.c
> > @@ -60,6 +60,27 @@ static long get_zswpout(const char *cgroup)
> > return cg_read_key_long(cgroup, "memory.stat", "zswpout ");
> > }
> >
> > +static int allocate_and_read_bytes(const char *cgroup, void *arg)
> > +{
> > + 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';
> > +
> > + /* go through the allocated memory to (z)swap in and out pages */
>
> nit: s/go/Go
>
> > + for (int i = 0; i < size; i += 4095) {
> > + if (mem[i] != 'a')
> > + ret = -1;
> > + }
> > +
> > + free(mem);
> > + return ret;
> > +}
> > +
> > static int allocate_bytes(const char *cgroup, void *arg)
> > {
> > size_t size = (size_t)arg;
> > @@ -100,7 +121,6 @@ static int test_zswap_usage(const char *root)
> > int ret = KSFT_FAIL;
> > char *test_group;
> >
> > - /* Set up */
>
> We removed this comment here.
>
> > test_group = cg_name(root, "no_shrink_test");
> > if (!test_group)
> > goto out;
> > @@ -133,6 +153,102 @@ static int test_zswap_usage(const char *root)
> > return ret;
> > }
> >
> > +/*
> > + * Check that when memory.zswap.max = 0, no pages can go to the zswap pool for
> > + * the cgroup.
> > + */
> > +static int test_swapin_nozswap(const char *root)
> > +{
> > + int ret = KSFT_FAIL;
> > + char *test_group;
> > + long swap_peak, zswpout;
> > +
> > + test_group = cg_name(root, "no_zswap_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", "0"))
> > + goto out;
> > +
> > + /* Allocate and read more than memory.max to trigger swapin */
> > + if (cg_run(test_group, allocate_and_read_bytes, (void *)MB(32)))
> > + goto out;
> > +
> > + /* Verify that pages are swapped out, but no zswap happened */
> > + swap_peak = cg_read_long(test_group, "memory.swap.peak");
> > + if (swap_peak < 0) {
> > + ksft_print_msg("failed to get cgroup's swap_peak\n");
> > + goto out;
> > + }
> > +
> > + if (swap_peak == 0) {
> > + ksft_print_msg("pages should be swapped out\n");
> > + goto out;
> > + }
>
> We can actually check that this number is >= 24M instead. Not a big
> deal, but might as well.
>
> > +
> > + zswpout = get_zswpout(test_group);
> > + if (zswpout < 0) {
> > + ksft_print_msg("failed to get zswpout\n");
> > + goto out;
> > + }
> > +
> > + if (zswpout > 0) {
> > + ksft_print_msg("zswapout > 0 when memory.zswap.max = 0\n");
> > + goto out;
> > + }
> > +
> > + ret = KSFT_PASS;
> > +
> > +out:
> > + cg_destroy(test_group);
> > + free(test_group);
> > + return ret;
> > +}
> > +
> > +/* Simple test to verify the (z)swapin code paths */
> > +static int test_zswapin(const char *root)
> > +{
> > + int ret = KSFT_FAIL;
> > + char *test_group;
> > + long zswpin;
> > +
> > + /* Set up */
>
> Yet we added a similar one here :)

Urgh I am stupid :) I meant to remove this, but accidentally removed
it from the old test instead :)


>
> > + 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", "max"))
> > + goto out;
> > +
> > + /* Allocate and read more than memory.max to trigger (z)swap in */
> > + if (cg_run(test_group, allocate_and_read_bytes, (void *)MB(32)))
> > + goto out;
> > +
> > + zswpin = cg_read_key_long(test_group, "memory.stat", "zswpin ");
> > + if (zswpin < 0) {
> > + ksft_print_msg("failed to get zswpin\n");
> > + goto out;
> > + }
> > +
> > + if (zswpin == 0) {
> > + ksft_print_msg("zswpin should not be 0\n");
> > + goto out;
> > + }
>
> Same here, we can check that zswpin is at least 24M worth of events.
> Again, not a big deal, but might as well.