From patchwork Wed May 8 17:13:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Usama Arif X-Patchwork-Id: 13658934 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id B729EC04FFE for ; Wed, 8 May 2024 17:14:08 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4C61C6B009D; Wed, 8 May 2024 13:14:08 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 475CE6B009E; Wed, 8 May 2024 13:14:08 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 33D586B009F; Wed, 8 May 2024 13:14:08 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 17B226B009D for ; Wed, 8 May 2024 13:14:08 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 9FF7D1C1738 for ; Wed, 8 May 2024 17:14:07 +0000 (UTC) X-FDA: 82095876534.22.4955FC3 Received: from mail-oi1-f177.google.com (mail-oi1-f177.google.com [209.85.167.177]) by imf27.hostedemail.com (Postfix) with ESMTP id DC8174001B for ; Wed, 8 May 2024 17:14:05 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=A45ghwBC; spf=pass (imf27.hostedemail.com: domain of usamaarif642@gmail.com designates 209.85.167.177 as permitted sender) smtp.mailfrom=usamaarif642@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1715188445; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:references:dkim-signature; bh=w6EQnYXqwhIh5NpITcONCWmNDrHXysWnVbFLQfKBz5k=; b=u0PDAqgvM2MdJnEgE+/1CLJQvexmdjFnE96ntQ5kwFDl1feYdOI+BJp/cYDobt+LMQs7/U BzgcZ9q9s/+knNUTN7hrSVFN6ANskDtiuD20A9PUHXNlAO0HOpNS9fCZTfYzqJ4FxA3eFa 54FtTMBsnjRwf5HP51OkqtsPGeloWoc= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=A45ghwBC; spf=pass (imf27.hostedemail.com: domain of usamaarif642@gmail.com designates 209.85.167.177 as permitted sender) smtp.mailfrom=usamaarif642@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1715188445; a=rsa-sha256; cv=none; b=6eD2IRxvyvpqWS28XBrCvcRSMzAJPy2lLrarv7QhbtJZKQn5swVXNfU13TkzSrHHYVffb6 0kDwhan7QNRLSSt2K4uNQwbwgBw+FgYd38Bzsfg8ML3eHsj3CZN2ob1cm51vwMyCm07aEv fSMayfA42olY7iIK8RVi5mAq4hUTbQk= Received: by mail-oi1-f177.google.com with SMTP id 5614622812f47-3c989d4e84dso24639b6e.0 for ; Wed, 08 May 2024 10:14:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1715188445; x=1715793245; darn=kvack.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=w6EQnYXqwhIh5NpITcONCWmNDrHXysWnVbFLQfKBz5k=; b=A45ghwBC9Uu+xACguXZELm2lbwJ5xh/HchkUnEjDUBgdyZRcWNsIcl/ii8udZWyC1y aKUJ+xBYWs6R9pZ07GFQC988ld5964eQ2PsR/gSXM9h/g2bLs1MPMx3YK2MJq36Q0z2J s024KPFMmOVhmj7eZ6f89gIVeTcFd6XRcMYUmlqjCLsBjxZoUGN8FkqvVMPcmg8afvd2 c2k2d4e7OhkdgCny+eWnZJI9jXvUJ1jYJ6I6s3Xy5jOQDF8EfHmnV0SLYJFj1JnESMQQ PSAYb2asQwcKLa70v42kJOUI5I4Conh/Kr6h3tBvlDWF17r48qkzapQ70wacC3LbjUkG bufA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715188445; x=1715793245; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=w6EQnYXqwhIh5NpITcONCWmNDrHXysWnVbFLQfKBz5k=; b=Xs5kuHfSIxvEMiRLh5oZJnjazp8iFNwbK+U5yxGggm9fkfVMRMQNKV/VZZWrKLA29D htzCA2BhO4npbakOfR+YC5OAWxlb1YdW/j72NDea3IFPDUqlVD+jJdiJTibeiIc/Jboi sLf2oR5AHhp0QEyA+vDts45/mN5ZJ114dKq5IdeB134nJOUfp9QWeUaKdWZqkim3ShN9 7NXiqs2ANlYK2/Dnq4fI34AEis38SfCt7IHfkawgivJ3Aa7euf8tsTHD1Gd+7JhuBou3 bo9wN2vvX4uGT8sW9RSJx6J7BFvHmeAckvu9SRMC49piAOeWwCgOOfRd4sLu3dxbiTas ihZA== X-Forwarded-Encrypted: i=1; AJvYcCVyzBGsTHcX4BGmL1Fflxzy4E5ayxSbtvZItSjFue4Y9sQM3sA42wAfP6RvWwQ9Fx+z7IHbBlCneyeeD5mWVqGEFHU= X-Gm-Message-State: AOJu0YwsilkklbnqqOHuvyhaQtDbVuLT7ZQzNxiDrUkRGRsydNnbZKWF uTMgsx+97ewSBvRLDq37HDFMWF46M7h47lnrudreUOnoB5ivBMdwC6AxAxq87Qk= X-Google-Smtp-Source: AGHT+IGJ6iC8d+ot25WgM9yxm37AYM8B7GTiDRdw+zaeYv1Gq6+4s1t3/CYLeeHdIK6PJV9mZRc82Q== X-Received: by 2002:aca:1317:0:b0:3c9:714d:cc7b with SMTP id 5614622812f47-3c9852acfbbmr3272118b6e.9.1715188443237; Wed, 08 May 2024 10:14:03 -0700 (PDT) Received: from localhost (fwdproxy-nao-000.fbsv.net. [2a03:2880:23ff::face:b00c]) by smtp.gmail.com with ESMTPSA id k9-20020a056214102900b006a0e6ffd710sm5761102qvr.97.2024.05.08.10.14.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 May 2024 10:14:02 -0700 (PDT) From: Usama Arif To: akpm@linux-foundation.org Cc: hannes@cmpxchg.org, yosryahmed@google.com, nphamcs@gmail.com, chengming.zhou@linux.dev, linux-mm@kvack.org, linux-kernel@vger.kernel.org, kernel-team@meta.com, Usama Arif Subject: [PATCH v5] selftests: cgroup: add tests to verify the zswap writeback path Date: Wed, 8 May 2024 18:13:59 +0100 Message-ID: <20240508171359.1545744-1-usamaarif642@gmail.com> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 X-Stat-Signature: 1g3izxke8qj8rr86ys49sbbw6m5kbua3 X-Rspam-User: X-Rspamd-Queue-Id: DC8174001B X-Rspamd-Server: rspam05 X-HE-Tag: 1715188445-746444 X-HE-Meta: U2FsdGVkX1/s0e4g07r++1iwZiI7vhYJaWKNjLnv7NfwSmQ1ZEA644JfWjxXdcAKiwi+LRJv/3EenBRZtdF9Iqa3sVQyF7T+chf6BKx03sJUctu+Cm42k0pINn0jnAY2M9cH8w5wjB3xS87GxEkMwZV5DDHR2fqFJ7Tz/3FhilHDpqJOOSOPHr0GXj+W/VxmyjOKonIrsoDbQ50mmPwyxpt2tpn6BfInDuURMdTZDfTJ8Oeb+bBbIrs0Ea8owVpqeiXYv7b0pigZQmwNuWdl2Km/or7voadczEZEtPwO5ZlQBt8r6BWzOtZa3KKO3e8qlZfC0j0Smz1LhawfgBQykZih4BdtCnnWk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cg/8Vj41iuHGEDU93qOce X-Bogosity: Ham, tests=bogofilter, spamicity=0.058671, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Attempt 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 it's enabled, or reclamation will fail if writeback is disabled as there isn't enough zswap space. Suggested-by: Nhat Pham Signed-off-by: Usama Arif Acked-by: Yosry Ahmed Acked-by: Nhat Pham --- v4 -> v5 (Yosry Ahmed): - Use cg_write_numeric instead of cg_write for memory.reclaim missed in v4 v3 -> v4 (Yosry Ahmed): - Use a fixed page-sized buffer for filling and checking memory when attempting writeback - Use cg_write_numeric instead of cg_write for memory.reclaim - Improved error checking for zswpwb_before and zswpwb_after 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 | 130 +++++++++++++++++++- 1 file changed, 129 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/cgroup/test_zswap.c b/tools/testing/selftests/cgroup/test_zswap.c index f0e488ed90d8..67186b7fa31d 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,132 @@ 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 it's 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); + char buf[pagesize]; + long zswap_usage; + bool wb_enabled; + int ret = -1; + 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 < pagesize; i++) + buf[i] = i < pagesize/2 ? (char) i : 0; + + for (int i = 0; i < memsize; i += pagesize) + memcpy(&mem[i], buf, pagesize); + + /* Try and reclaim allocated memory */ + if (cg_write_numeric(test_group, "memory.reclaim", memsize)) { + 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) { + if (memcmp(&mem[i], buf, pagesize)) { + 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_numeric(test_group, "memory.reclaim", memsize); + 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("zswpwb_before = %ld instead of 0\n", zswpwb_before); + 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 (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 +551,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), };