From patchwork Mon Jan 29 22:45:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nhat Pham X-Patchwork-Id: 13536475 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 EBDE8C47DA9 for ; Mon, 29 Jan 2024 22:45:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 780886B00A7; Mon, 29 Jan 2024 17:45:49 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 72D186B00A8; Mon, 29 Jan 2024 17:45:49 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5A95E6B00A9; Mon, 29 Jan 2024 17:45:49 -0500 (EST) 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 429B56B00A7 for ; Mon, 29 Jan 2024 17:45:49 -0500 (EST) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 14CD940AAA for ; Mon, 29 Jan 2024 22:45:49 +0000 (UTC) X-FDA: 81733832418.07.2CA8752 Received: from mail-qk1-f180.google.com (mail-qk1-f180.google.com [209.85.222.180]) by imf15.hostedemail.com (Postfix) with ESMTP id 52729A000F for ; Mon, 29 Jan 2024 22:45:46 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=gO723lLs; spf=pass (imf15.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.222.180 as permitted sender) smtp.mailfrom=nphamcs@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=1706568346; 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=xef0hVDdHdF2jNfb1sbGTppVQNq89HdiJsXx3j0Z+RA=; b=1neKXjsAk28Ga9K0UN+sRv4Huo34VLBxR6tdlv7/QRHj4YyCmHkoqNSYjqVDLR5jkY/f5r +N2/nMVjzB0eBe61Bitn7aTl4RSmatC85p7qQU6NDogOQe6nm6BQPJvXZxOcEWKWCKAFL1 oFOoAWzhUs0W2cCASq3qt1D9Bir4Kf8= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1706568346; a=rsa-sha256; cv=none; b=rOp0v4JscUpnWs1on43ZbWAeu5wPP6gPLuQKn6kLR1w8/eu4ExECbQWzffKANVC7IOo/J0 0bCsvWqoAMQZ7Q50IyIPi6Cof3uZPO3cqm8/PdDsFA8dyI9+5JLJ/d/Rnh0X4R3PbcTKlT 691K2HLdZmFiu6vGzqfewjrNcTpLgKo= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=gO723lLs; spf=pass (imf15.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.222.180 as permitted sender) smtp.mailfrom=nphamcs@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-qk1-f180.google.com with SMTP id af79cd13be357-783d4b3a00bso304391385a.3 for ; Mon, 29 Jan 2024 14:45:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1706568345; x=1707173145; 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=xef0hVDdHdF2jNfb1sbGTppVQNq89HdiJsXx3j0Z+RA=; b=gO723lLsWoPnFW6iRt3FVD+EPwMPvjw1Va9ltfyRVuXXB9z9bdv6ispPuL/c85yri6 GuljeY9e2eWT9O0YtxjvaarXb1BfPCNJJWkT+7qUey+ldAleGgQMJvD/N9N0SGQ9ZfNp 9GfoaFFM70JOcnm1nYJKy5ymizBS3TffzYB0llzdcT522ak/yMZeeqIzvLEeUk0MGqEW aznKBGOStqZQc65TOjFbVYn0FWeK7oK2e+So/empIVPToATCPn9JUSYRtZuB4dPwMpTH RPCfZYuneRijzwup1A0Xf1viVH8TgIGqsvV2KkIJ4ufHTSh6/kqLdEyuUal+jzheIxgX 6HNA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706568345; x=1707173145; 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=xef0hVDdHdF2jNfb1sbGTppVQNq89HdiJsXx3j0Z+RA=; b=H7yDdkhdlL4zHpIC3mt4DiyXkoAw+Cy0xFC6tCZbAr4AMVJEtE32A8j1qb2RrOVG2E wM/ZNAQ8qlS4hHeRPYFUhAfma/0UKijsjsNGZKk+DAdpm4BNEc0kaOQwbuQxc9R+FKs1 K4HS2U4uMifk0PH3dv04FBf/SAtb90RKPfOgUC+7wQ5lXBqXC4PfPLpZtYnP2uvNbDhB Z2+eCv0OyCsIt+lNfh82Vur3Q6EBcDxRv20xr+vDovzZpwuhEuqcKLwbKJqNKZphwq4f GTnBWTq+4S7eEwKBI4v4f6y9hnfc6FPRSVXM8ZEJbN8cjTL5jN8+Dvucnsnf408+P7T9 0k1g== X-Gm-Message-State: AOJu0YxlKsuOQNSwbpMoSmhitJMK4lS0Y6p0Hwjo+dBxNHQdfiw0GPqJ pAjFjG9ptowCbsOWaDEGgl/XJ92Ptg3FZwibMxCweaZZUV9o1b8b X-Google-Smtp-Source: AGHT+IFA4GtOI5DVLY0CR9h66W+OqJ3ySRkcMjVMpPQ4jAh32cn+UJ5P037C4f1Ww4qSMDoHjtOsIA== X-Received: by 2002:a05:620a:4485:b0:783:f862:88a7 with SMTP id x5-20020a05620a448500b00783f86288a7mr4466928qkp.10.1706568345431; Mon, 29 Jan 2024 14:45:45 -0800 (PST) Received: from localhost (fwdproxy-nao-013.fbsv.net. [2a03:2880:23ff:d::face:b00c]) by smtp.gmail.com with ESMTPSA id p15-20020ae9f30f000000b00783f606dcf8sm1926576qkg.73.2024.01.29.14.45.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 29 Jan 2024 14:45:45 -0800 (PST) From: Nhat Pham To: akpm@linux-foundation.org Cc: shuah@kernel.org, hannes@cmpxchg.org, yosryahmed@google.com, tj@kernel.org, lizefan.x@bytedance.com, linux-mm@kvack.org, kernel-team@meta.com, linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH 3/3] selftests: add test for zswapin Date: Mon, 29 Jan 2024 14:45:42 -0800 Message-Id: <20240129224542.162599-4-nphamcs@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20240129224542.162599-1-nphamcs@gmail.com> References: <20240129224542.162599-1-nphamcs@gmail.com> MIME-Version: 1.0 X-Stat-Signature: h3kb8zpfttnhjkj4p8i1hx57z66wompe X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 52729A000F X-Rspam-User: X-HE-Tag: 1706568346-44712 X-HE-Meta: U2FsdGVkX1+qybOJeEPj+7TSpbSGixQ9ka+U9gc+Jg0rVpPG5QFDIjxJG0cmh3vfyufvihqpfthtlf5F23RGHXFTcxS26x46Y3JqC8iVD8yKDk9FREZC9TG7YXmdslp2kVMpLVgUVjfQU5XEBSXC8oMoKkgVYEpxGc5cs0lT8/getIhZsrg8SEyLtT96ZH8SDPRD02IIod5twoLhHwTLfbgMeD9x9lyahsNGsE7+SYl0eEMDtKRUa3kZhNgXXmQ0jEk3dbqSV413+VtX+29aPtajBhjo4xdfgzqAUzb6gvR5sbDye0sOrzbMzrO5ph0Hs+4+CKV8r2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t9FwKldd0q98go0ZVUASKPxVTQ6m3QSq044NJySFub7Amo8R33fboB5lh0bGNyg= X-Bogosity: Ham, tests=bogofilter, spamicity=0.339968, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: We recently encountered a 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 cover this code path, allocating more memories than the cgroup limit to trigger swapout/zswapout, then reading the pages back in memories several times. Also add a variant of this test that runs with zswap disabled, to verify swapin correctness as well. Suggested-by: Rik van Riel Signed-off-by: Nhat Pham --- tools/testing/selftests/cgroup/test_zswap.c | 67 ++++++++++++++++++++- 1 file changed, 65 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/cgroup/test_zswap.c b/tools/testing/selftests/cgroup/test_zswap.c index 32ce975b21d1..86231c86dc89 100644 --- a/tools/testing/selftests/cgroup/test_zswap.c +++ b/tools/testing/selftests/cgroup/test_zswap.c @@ -60,17 +60,39 @@ static long get_zswpout(const char *cgroup) return cg_read_key_long(cgroup, "memory.stat", "zswpout "); } -static int allocate_bytes(const char *cgroup, void *arg) +static int allocate_bytes_and_read(const char *cgroup, void *arg, bool read) { 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'; + + if (read) { + /* cycle through the allocated memory to (z)swap in and out pages */ + for (int t = 0; t < 5; t++) { + for (int i = 0; i < size; i += 4095) { + if (mem[i] != 'a') + ret = -1; + } + } + } + free(mem); - return 0; + return ret; +} + +static int allocate_bytes(const char *cgroup, void *arg) +{ + return allocate_bytes_and_read(cgroup, arg, false); +} + +static int read_bytes(const char *cgroup, void *arg) +{ + return allocate_bytes_and_read(cgroup, arg, true); } static char *setup_test_group_1M(const char *root, const char *name) @@ -133,6 +155,45 @@ static int test_zswap_usage(const char *root) return ret; } +/* Simple test to verify the (z)swapin code paths */ +static int test_zswapin_size(const char *root, char *zswap_size) +{ + int ret = KSFT_FAIL; + char *test_group; + + /* 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", zswap_size)) + goto out; + + /* Allocate and read more than memory.max to trigger (z)swap in */ + if (cg_run(test_group, read_bytes, (void *)MB(32))) + goto out; + + ret = KSFT_PASS; + +out: + cg_destroy(test_group); + free(test_group); + return ret; +} + +static int test_swapin(const char *root) +{ + return test_zswapin_size(root, "0"); +} + +static int test_zswapin_no_limit(const char *root) +{ + return test_zswapin_size(root, "max"); +} + /* * 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 +370,8 @@ struct zswap_test { const char *name; } tests[] = { T(test_zswap_usage), + T(test_swapin), + T(test_zswapin_no_limit), T(test_no_kmem_bypass), T(test_no_invasive_cgroup_shrink), };