From patchwork Mon Feb 5 22:56:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nhat Pham X-Patchwork-Id: 13546374 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 446C9C4829A for ; Mon, 5 Feb 2024 22:56:18 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A52AA6B0082; Mon, 5 Feb 2024 17:56:16 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A02196B0083; Mon, 5 Feb 2024 17:56:16 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 80D376B0085; Mon, 5 Feb 2024 17:56:16 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 6C7CD6B0082 for ; Mon, 5 Feb 2024 17:56:16 -0500 (EST) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 19F1A1A04BC for ; Mon, 5 Feb 2024 22:56:16 +0000 (UTC) X-FDA: 81759260352.17.DBA579B Received: from mail-qt1-f174.google.com (mail-qt1-f174.google.com [209.85.160.174]) by imf18.hostedemail.com (Postfix) with ESMTP id 4D1AB1C001D for ; Mon, 5 Feb 2024 22:56:13 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=G0nMVm4L; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf18.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.160.174 as permitted sender) smtp.mailfrom=nphamcs@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1707173773; 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=8HqWjgU6D10aye5QHEqX+OBFLhtZ/fQssfKAsKXBKvY=; b=Qr/vET55OYRoBrjS1kcavvIBao4GtarfZ/7DvlacP2lJV3UqCq21EZM7lYVS/4mWTZB3f5 ssm0lN/S4NSLOvCmXO04BxuwaPBbeiTwfgLmLryY9YiI48Oxm/WOYxp+tNb8zCz3XE+YXu zpLA0lNr+S2w4q00i3ddSFnuL5rGQr8= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=G0nMVm4L; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf18.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.160.174 as permitted sender) smtp.mailfrom=nphamcs@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1707173773; a=rsa-sha256; cv=none; b=3vYVN3L+m0tKsOuaClKrrrmHLUSm+PVcai5bv0wUcq4kvyFlHUm4EwrymusxU21nGMqadU a9EIPIKQhNv3VD61hgqlm4CIs2Y07hvMRiC/I5E1yPbDKFTK5UyyW/YTLVBR8AYpKaps5G NDROQxZYu9yscC4f/BXFY71bR7lCvwI= Received: by mail-qt1-f174.google.com with SMTP id d75a77b69052e-42aabb1cdaaso38196911cf.2 for ; Mon, 05 Feb 2024 14:56:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1707173772; x=1707778572; 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=8HqWjgU6D10aye5QHEqX+OBFLhtZ/fQssfKAsKXBKvY=; b=G0nMVm4LQBhMVGDIltdzgoGmuCAtPQttdS56awA4bQ5GAMBB6AbLCj8rudfXyALe2i yOa3DH1JsQzsZhIX06xd887tIqmktwhiIXk21HLltL5feX+HwNARv8GG4s2c7RsJNBJy Sw9IUgHOlFAF1mRfff+8wdjUB1POFEbBwx9MIulp1VH+4UkyePD67WyX38fJ47das8DO XbK5ffm9r1OAaZJcN11bbM/JQ6/C9muoZx685mbcki5w/Jm5XTdh4vUwtmO7oMlDmhEM u5q0F849ak59O9JbyOoNK8J03pu4zI3U+B1LMrvg/bewh366sqoLaPybrKORqKTv6kvr kF+g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707173772; x=1707778572; 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=8HqWjgU6D10aye5QHEqX+OBFLhtZ/fQssfKAsKXBKvY=; b=cW2+GTZaQEuTR1nJliHIEoXzQtXK7v9gEmgi9Qk50wsKuTXveUNbft3ra27iCNpuK2 OBCcNf+XWOtSYO1eNpoMkJoVdC+15ul4C1SZ7Hn1Hjgd8nO1VbbQrUGNUagoC3yfBv2s A15lGUaHNOpU4oDD3hX3yJWJj0niKAVjIDS75pfuizxZ+h43mjf7NFKzoNC24TxcFp1+ GXh2etJ17W6sDL3Vul4oTYAqxzwI+9YjYu0H5VIzy9PtIXs9ZuQmykJtU7R2S322fSJE 1Hf471Mogg/Pp4VY95pGdmxWoeRGWXKcZl8j8p9ucVEWm9T6SQbRXguxi9lseCmkVzJF eyiA== X-Gm-Message-State: AOJu0YzKmSgiMHBTBzsh031aNf4TWKsEPck/yuFXy++fiFjVpsAJHX7K Fr3IU/pChJ1hl/wlOaSHPX2m7WJI42cqAMXgUCVWjWi9FpQGn7OT X-Google-Smtp-Source: AGHT+IGCJNv8Ji8AU8IubQXBQCs37V3e6UKJOoL7efbPjiM7OFm3oBHUGScLF4gh82NdJqEYVnvINw== X-Received: by 2002:a05:622a:48c:b0:42a:4350:64c7 with SMTP id p12-20020a05622a048c00b0042a435064c7mr856118qtx.68.1707173772351; Mon, 05 Feb 2024 14:56:12 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCUqJ/8EC/uAZwZYtWAjBIdqbUeGbSxx64XcNJIk/q+Br0fd2ZnF42wZpS0SYMUietCy0wo5VDpFFrErX3imrvpZO7WUNzR0srWRl30TV27h+6vc/IS6iQWrv4/WZwlIoPcLjhVHGiLlYCHcOhLHtmU/XHgKHVHhuxMDRGoHmyQMpIEtQEnWW34EFfNe4blyDBX6rzDg4TWP0sFRajPTZJgN3taO51FviZbbDsTb2g6pQs2CZ1VtBqISF4HUt3BAYXagM3lV6duO/3YsUmZC9mGd1ykVuOVgbrKn5/IrLdfdhnCAgZS9LRo7X/PgcaV4z62Tujb6K1xdzYVGR7dc+ZgfZgASlu5mhpHQiT4/C7oRjIHVukimfyzDAXIBatf/OZoWz0Lci4ZJjl5Ne7STXbQ= Received: from localhost (fwdproxy-nao-001.fbsv.net. [2a03:2880:23ff:1::face:b00c]) by smtp.gmail.com with ESMTPSA id e2-20020ac845c2000000b0042c2034c54fsm367932qto.17.2024.02.05.14.56.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Feb 2024 14:56:12 -0800 (PST) From: Nhat Pham To: akpm@linux-foundation.org Cc: riel@surriel.com, shuah@kernel.org, hannes@cmpxchg.org, yosryahmed@google.com, tj@kernel.org, lizefan.x@bytedance.com, roman.gushchin@linux.dev, linux-mm@kvack.org, kernel-team@meta.com, linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH v3 3/3] selftests: add zswapin and no zswap tests Date: Mon, 5 Feb 2024 14:56:08 -0800 Message-Id: <20240205225608.3083251-4-nphamcs@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20240205225608.3083251-1-nphamcs@gmail.com> References: <20240205225608.3083251-1-nphamcs@gmail.com> MIME-Version: 1.0 X-Rspam-User: X-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V1 0foC59iA d0+rC6Hl53thqVFQ4Zu5GW/hvRQ/00f/dQ36VjZcZ2RP2VQmaRW1CMwmzw87v7Qv9rKrDV6MayFRwlVDb1NWceJKeHNAi3nr3YPIBxQtiI8yg6orjc1xZLGRfY6HVxWKBz4VAJB35HIOEROQ+XJUkI9W1Y9LBa7TTk7ckJDj8G8oukcU87AF1J4ev5B0mzSCY3FJBxd8zFtTfuX0zW7y1+2zF7bQwbKQPbP6i3Kwr/r4Arre2M9fnwdPWw58b5EpbNkES1nXfZKNi44MI2BkknVqlRtYzLWtBhDCI38pqjI5T8xSUz7IQnHGZpJFNO2w52VLcqnvDFT9Xwt0EONLMRoQkvX+ozt2yA55kDjvjbUDO7qFMJ8gGpB2WYv3XlgICyBqoPitFHtmPv2X196IuW/2rYRHWbE3nHoZ57h6+PBx/xwuxKoEyGEVZriEGl8kiLl162mAXyLj2xyrmt+v6OExT7vPRDWkH4vfZfNPKrq6oeZrQ8V+fMg+S4IQ5r1eCxwyf X-Bogosity: Ham, tests=bogofilter, spamicity=0.374203, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: 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 Suggested-by: Yosry Ahmed Signed-off-by: Nhat Pham Acked-by: Yosry Ahmed --- 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 */ + 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 */ 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; + } + + 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 */ + 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; + } + + ret = KSFT_PASS; + +out: + cg_destroy(test_group); + free(test_group); + return ret; +} + /* * 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 @@ -309,6 +425,8 @@ struct zswap_test { const char *name; } tests[] = { T(test_zswap_usage), + T(test_swapin_nozswap), + T(test_zswapin), T(test_no_kmem_bypass), T(test_no_invasive_cgroup_shrink), };