From mboxrd@z Thu Jan 1 00:00:00 1970 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 E88DDC4345F for ; Thu, 2 May 2024 19:02:43 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 48D126B0099; Thu, 2 May 2024 15:02:43 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 43E856B009A; Thu, 2 May 2024 15:02:43 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 304746B009B; Thu, 2 May 2024 15:02:43 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 1306E6B0099 for ; Thu, 2 May 2024 15:02:43 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 962C140EC2 for ; Thu, 2 May 2024 19:02:42 +0000 (UTC) X-FDA: 82074377364.28.CECC7AD Received: from mail-qt1-f175.google.com (mail-qt1-f175.google.com [209.85.160.175]) by imf15.hostedemail.com (Postfix) with ESMTP id A3EECA003F for ; Thu, 2 May 2024 19:02:36 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=K9p1n2vr; spf=pass (imf15.hostedemail.com: domain of usamaarif642@gmail.com designates 209.85.160.175 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=1714676556; 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:in-reply-to:references:references:dkim-signature; bh=+7eQcQPlLfPoF1F7OxifffHm/SsC0VJeyg2xs0tjwD8=; b=mxtlf+UztvZngzvsFYizFX4tMS1CBDUiIYnQtybwizK6PvuTMFsUZ9QbdiE3tYxQ/9XaEW l7wlvwmqw39WO1jDyAR6YSvDdSpGxUnI6bfv0gD1uKBB/ZGhB3uxYwQ6W+sYw8C9974U29 fyammfAlbmKkJt173qucTssTN0f7gNQ= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=K9p1n2vr; spf=pass (imf15.hostedemail.com: domain of usamaarif642@gmail.com designates 209.85.160.175 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=1714676556; a=rsa-sha256; cv=none; b=0GO3xlmeHNtXhoK7Y/Ym7OvucFLbgdQtZ0HD0/F4/vpj5YiHjPuvc3D3e31ar3cZj5aLtK RpWGSF0NIOPF7YugSTnYciGDzR6blqnOwzgC0ffSKysVw78OPISDmXU+rD0Tbp30VdxDNU 2ZT2rPiAn+1U78BRoX94jUHhqzJxzRc= Received: by mail-qt1-f175.google.com with SMTP id d75a77b69052e-434d0a63151so48162031cf.3 for ; Thu, 02 May 2024 12:02:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1714676554; x=1715281354; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=+7eQcQPlLfPoF1F7OxifffHm/SsC0VJeyg2xs0tjwD8=; b=K9p1n2vrm7z+rHt9y9R+l3tIzTh4dZhjc+2DkPH6mTx9qL7pfLp/oe0ot8N/DY+NxI ceF4V4UDppe/22Qao6AMykpxYA5gHsiYtsFYkkCr09kZtP2WzZSfsgGPKInGExKylald OevqG+l7ZgDkald0hncmg/96usllWYqcVC4cmsvM2NP5D6KFHaiN15qRQ6zZVSPrVRBG 4JcCxN8Bu7LYzt+aPkfiLSlwzlyPpi88D6obPnH99wI9V1msvQYrxAQ97N6REx6q8dxZ gjmI23n7EkOm0puo6kRYh5C3YGqSaCCVSDwFYCjvLTVYji0k+cu/TuyuWT5AgIVA7oGn tXpQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714676554; x=1715281354; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=+7eQcQPlLfPoF1F7OxifffHm/SsC0VJeyg2xs0tjwD8=; b=jqXCjI8rGiJM0RmJaosyIkVwsIBlUHS6AmLrB/EkluLCSpxXVSZPK3vODuuXwfU8Pu f7rp/uxazwJqwo7AlUIRscLAaM3ocNDAJHm5VJpyjIdSwTaxQrnueDtFHo9YEXdS60IE 0xzV8sHL8VdzYVoQyY8YuwFKmkbWBOpM/L34ayorpCLxYaA3b/Ew339Zs8pqjTr6uNp/ tt7/RO20NMDFgG6b86Ahk4wVoXnMvZU3eIgguJMlVhlZ8uQPy+JQDbdtbQq+/diNvc57 bhIznNQfLkopqH2fIepqTacHBHwdOvdDl/JXCIePScof0dAcqmjxLZilQ0uJ/23bTMHw O+mw== X-Forwarded-Encrypted: i=1; AJvYcCVgvgSVxlV5HvcGBtkvRAV7inFyhLkwJCVGDGcBJg0g8NBvTFE2mgfBj3LGKbc27MpaCSgNsr4l1arDp2BhRZQPElk= X-Gm-Message-State: AOJu0Yyvf/JKc4POhhZ0uxD6IyMT6+/ix5rGwFz6KLKkHOtjh2MRM2Z6 hms6pAqLZltpDOiyNymotcWtyv3fIHUgtFIUE3Kkbq0Rs0QRzISt X-Google-Smtp-Source: AGHT+IG+vh405FIs2xF2Ei9F+WVe3rZDH8UmyV2+XlPjV5yMR1Q0m/xNIXBzcVWijqXvA2+J6ynj8w== X-Received: by 2002:ac8:7d83:0:b0:439:a719:702e with SMTP id c3-20020ac87d83000000b00439a719702emr485958qtd.54.1714676554441; Thu, 02 May 2024 12:02:34 -0700 (PDT) Received: from localhost (fwdproxy-nao-000.fbsv.net. [2a03:2880:23ff::face:b00c]) by smtp.gmail.com with ESMTPSA id bb20-20020a05622a1b1400b0043cc0583dd3sm602970qtb.50.2024.05.02.12.02.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 May 2024 12:02:34 -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 v2 1/1] selftests: cgroup: add tests to verify the zswap writeback path Date: Thu, 2 May 2024 19:58:37 +0100 Message-ID: <20240502190223.3975970-2-usamaarif642@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240502190223.3975970-1-usamaarif642@gmail.com> References: <20240502190223.3975970-1-usamaarif642@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Rspamd-Queue-Id: A3EECA003F X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: ubgw3xfqssp9etgkxz9exioaj9s91iia X-HE-Tag: 1714676556-800569 X-HE-Meta: U2FsdGVkX185v4gW0889U6m45x+6+fnRIzR9hzP9qSUK5GMizy+S04f5BFEon2u4Mf+oh3Qgcwlxy7jT0UwTWEEAyFcQeCLwlMqguX1fEqhIASkemlg9BJSS+OXnQXPSwOk7oXAV8Ajnwv91zboI3OsWmyUxa6RqiuPa0QB2Y+pRu1zeq+eObSOWDk8kxq3+DJmKdi1vrpL2J3KL/+r2lu9/+G3MNRXzVpFTH/zlJ3cPQWtnHsyccv9DpzrX02F33iqFMIyaCRBSPmQLXGqAcblKCkudDuhZxEpcxWJY1kikxFCl4hLgRc+focP3LrnHfx/L0r6x4biQp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I= X-Bogosity: Ham, tests=bogofilter, spamicity=0.404282, version=1.2.4 Sender: owner-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 --- tools/testing/selftests/cgroup/test_zswap.c | 125 +++++++++++++++++++- 1 file changed, 124 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/cgroup/test_zswap.c b/tools/testing/selftests/cgroup/test_zswap.c index f0e488ed90d8..cd864ab825d0 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,127 @@ static int test_zswapin(const char *root) return ret; } +/* + * Initate 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 initiate_writeback(const char *cgroup, void *arg) +{ + char *test_group = arg; + size_t memsize = MB(4); + int ret = -1; + bool wb_enabled = cg_read_long(test_group, "memory.zswap.writeback"); + long zswap_usage; + + if (cg_write(test_group, "memory.max", "8M")) + return ret; + + /* Allocate random memory to enusre high zswap memory usage */ + char *mem = (char *)malloc(memsize); + + if (!mem) + return ret; + for (int i = 0; i < memsize; i++) + mem[i] = rand() % 128; + + /* 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++) { + if (mem[i] < 0 || mem[i] > 127) { + ksft_print_msg("invalid memory\n"); + ret = -1; + } + } + + 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 && ret) + ret = 0; +out: + free(mem); + return ret; +} + +/* Test to verify the zswap writeback path */ +static int test_zswap_writeback(const char *root, bool wb) +{ + int ret = KSFT_FAIL; + char *test_group; + long zswpwb_before, zswpwb_after; + + test_group = cg_name(root, + wb ? "zswap_writeback_enabled_test" : "zswap_writeback_disabled_test"); + if (!test_group) + goto out; + if (cg_create(test_group)) + goto out; + if (cg_write(test_group, "memory.zswap.writeback", wb ? "1" : "0")) + return ret; + + 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, initiate_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) { + if (zswpwb_after <= zswpwb_before) { + ksft_print_msg("writeback enabled and zswpwb_after <= zswpwb_before\n"); + goto out; + } + } else { + if (zswpwb_after != zswpwb_before) { + ksft_print_msg("writeback disabled and 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 +546,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