From patchwork Tue May 7 10:14:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Usama Arif X-Patchwork-Id: 13656576 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 1E7B1C10F1A for ; Tue, 7 May 2024 10:14:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 96B516B0083; Tue, 7 May 2024 06:14:50 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8F1B56B0087; Tue, 7 May 2024 06:14:50 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7431B6B0088; Tue, 7 May 2024 06:14:50 -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 542166B0083 for ; Tue, 7 May 2024 06:14:50 -0400 (EDT) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id D34E01C1799 for ; Tue, 7 May 2024 10:14:49 +0000 (UTC) X-FDA: 82091191098.11.EFC846B Received: from mail-qk1-f174.google.com (mail-qk1-f174.google.com [209.85.222.174]) by imf15.hostedemail.com (Postfix) with ESMTP id 1DF29A001D for ; Tue, 7 May 2024 10:14:47 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=Dn7v6O+d; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf15.hostedemail.com: domain of usamaarif642@gmail.com designates 209.85.222.174 as permitted sender) smtp.mailfrom=usamaarif642@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1715076888; 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=MmNF1Voon22sEbCDSc1Fl59X5+YrmSQN7fGFAIA4IKw=; b=qH7iXs8pT1OQDAJoWUhfKonCMAwNY6eDFA695fyveg+gmiAU3nZcJnr0AUgpQsPM1nqyR5 7XEBWvdkwWdtw+KURKhxiOG+3QOKZ+GdfziwE5JZsmOj8PaA8922dsrqUCfGH2S6grdFGK iEICti3j8+EKiIASCVFyRhAbO56f018= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1715076888; a=rsa-sha256; cv=none; b=iT/9ArmAyPovuXKg10H6WwgOqrIr67U7+HW1eO8EERG1141ukJKP85udPLoGiZwCZNl1d0 VOeW0SaLVSS8xomxBEhZOOLvMaiEwTLmVOQbhmfDUFosJq96Jsla1SImsW92HZx2c/X9VN g+x2X+43X9IqVt+U6gorLlRopU1EFCI= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=Dn7v6O+d; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf15.hostedemail.com: domain of usamaarif642@gmail.com designates 209.85.222.174 as permitted sender) smtp.mailfrom=usamaarif642@gmail.com Received: by mail-qk1-f174.google.com with SMTP id af79cd13be357-7928c5cb63fso207419085a.1 for ; Tue, 07 May 2024 03:14:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1715076887; x=1715681687; 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=MmNF1Voon22sEbCDSc1Fl59X5+YrmSQN7fGFAIA4IKw=; b=Dn7v6O+dDR4MwdaiKCE0HaXf13ocoeskJv7jEiNwufMKiUlm3cx0xlaOU/zGUFNvEy EvGbxfXbB4BLMCf2PVqEaD7YQnQpxZlH6/RP9zNLSJnGxogLoTUfpWxBqwl6y4ULkA9A sToddhRoyCsT+Sprl7dk5ddxRv5KonW3I34Ll4Q93XSufZ/DaAMCeXNom+nhny8uy+BW oIjOOJuXlbyib6zEvh33uK/gaWC9gIPzIeRPWYRGl6NIgiKRAGB+1vOo/4JtKJZd9tO2 A5XQGLh++Gg3fzLSHdwAfXa7dIBuYJAYEd+gEVpzZTX+1X1siaZMB98AhaiDkT2GCmBN vr4g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715076887; x=1715681687; 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=MmNF1Voon22sEbCDSc1Fl59X5+YrmSQN7fGFAIA4IKw=; b=GjjFfxxpZ5b9H0ofdGlr2PuXNhozmvK+4bWoval0tX0LMoogjZXCWZ8jVmIxlnBB8/ x9QgpABTIpBhFQWOEH8Nm8rJ327ZknsjtSIiJB+Bd0QnpPKg7k4UofekqDLC7113s9xm LP2t/b0OQeiywo16Lzr96kPklNAKLlmdCTXYsTMfVs5nFLAFSTajcDqmHrygVfjUFSqn UB8GiNf9TkvfAH02Cqtowr26JHsTYYIaF8V++fttExi4jydxoFK3yG65/U999CPdj+km r/LmL50+XntF4dWBBbsFKXL7R15R63SjeN+0+abB7YfSNTjVPhmTX1pbBB1/bKmySzcz xF4Q== X-Forwarded-Encrypted: i=1; AJvYcCXsXl0vEgveOKCpuyc/9H7DYU/vRaCb/fod75fodXiYH7t9ThhkTF+5HdRrdCh0I7R8PW6xKITlTtNBpGr0VdeYBOs= X-Gm-Message-State: AOJu0YwKXzLePKHeHuQYcIm/Zi4iaoXhI1FpSctqcGcFVHOvpAdEB7fC ocWqMpB8GrsbCUWwM9lvYWVnTNXtMJjx2qJB52Ba4a3SMNb1BXMV X-Google-Smtp-Source: AGHT+IE2YsE3yqQYRExxya240CqbhsZmmvu5SeOBp0MN191rAx6SG008x5nutOE0gbokYKbEGm6/zg== X-Received: by 2002:a05:620a:370f:b0:790:96e8:5bfa with SMTP id de15-20020a05620a370f00b0079096e85bfamr4342499qkb.4.1715076887088; Tue, 07 May 2024 03:14:47 -0700 (PDT) Received: from localhost (fwdproxy-nao-010.fbsv.net. [2a03:2880:23ff:a::face:b00c]) by smtp.gmail.com with ESMTPSA id h18-20020ae9ec12000000b00790fc75f318sm4759270qkg.32.2024.05.07.03.14.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 May 2024 03:14:46 -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 v3] selftests: cgroup: add tests to verify the zswap writeback path Date: Tue, 7 May 2024 11:14:42 +0100 Message-ID: <20240507101443.720190-1-usamaarif642@gmail.com> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 1DF29A001D X-Stat-Signature: cs7tnx7kk3e1r71jbsbgyfoycbkjb8ps X-Rspam-User: X-HE-Tag: 1715076887-72030 X-HE-Meta: U2FsdGVkX1+Az/p4FJ9COthfW2mnds0nmb+mEc/I2sUNv90iLc9T+yoH7rLMbHEcv30csqI+i6+wZa1RbyCJVDN9sdJviW+t5mu3MdsoAoZg6xzYOwM9lHTMbELiqe1CgKitx+4pv5RUGSC+XfCy0aczZNkwl6Tj6ZnP/VXJFDLvgUjWrKTESXbfvnhVkWnV7W/Wq+ibkrVpdTbMz7JtZCBjgff+sKUJTain3XSgLgThSEwuhwNDq649wif5f17bmcrLVp12CuHOGvRH8nomPRM7t4UsRRWXnQ2XsoE2+gJJ0B/OuoXcCt2vVWNKKLnFcUplOARhoNo12zCKztYqadi8nEErmcjkmlwg6c6HkHli7hnnbk3SDkn4+Tst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linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: 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 Signed-off-by: Usama Arif --- 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++; + } + } + + /* 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), };