From patchwork Mon Feb 5 22:56:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nhat Pham X-Patchwork-Id: 770207 Received: from mail-vs1-f52.google.com (mail-vs1-f52.google.com [209.85.217.52]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B61DBC148; Mon, 5 Feb 2024 22:56:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.217.52 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707173773; cv=none; b=Mi6zBa46B07jCD6SUJyN0gfoHo+Oixk7aT5X5CLJntL/PJaPL03g9yW9p7757pz2hO7tFdEApI3bikWRBMk8Hk5GAVF61vdBPF91vAzf24FbueLVyaXy8lNIxkmfuBYCpj+a2+IJSjDWedNa+dw8tjnRtOCo1I9DgmZfxyk11zU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707173773; c=relaxed/simple; bh=UAPFAa2YoR9r9pULmKsHh3JBLlD3Aq6ViNtLGWwsu4E=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=IXuof2HZbJR3kH7CvD/wb+E4/f3YoTLiBbaZ3Kn6mDV9pwa4MamLsBIqv1hwXizVpIW4T+6pEfQbTszc/tZ4pJ+cEEMi4vyrzFW1BFv8ulIF7hrvxPVJPFq/BQsp/bSegP5BcYNyY08UMZqRLKw86FkkYhSeTqswz/5WENWe0IE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=nj+nMklA; arc=none smtp.client-ip=209.85.217.52 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="nj+nMklA" Received: by mail-vs1-f52.google.com with SMTP id ada2fe7eead31-46d2dff740dso355131137.2; Mon, 05 Feb 2024 14:56:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1707173770; x=1707778570; darn=vger.kernel.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=0dsOQxIuZglfzGL/UkHixra2YxsOVoucMj4slNXKYQ0=; b=nj+nMklAtRAissrfk62n4MZpM2Ko9rB5NEP2SSxQu1VlR8ujwjE97EggjNKU/Wg8RD cSC99BV0m5qNHFvekvH3iWROae3PXbwQKMcuGpVoXTZ2/2ewfzWQt8pHL9bNSWWqp+7N /9giQZn/AfxmQE32Z3xbkqb07fTXg9HvkNBtY7zdnoGoDZwy82PMehnxgILT/LCKTqzB ZXQebmqP/+wHhcS5XApFGjvuFIGjrvvRMMkgfuWGUF48ThvXZABt9/4BqCM0LV6Ns96W ondR6LBm4wQ34Qvl8LI76d1EuowYWoSDrJCDQa2ktC88qsbP7nBWLGr+UT6uRzZfHmWR Mk6w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707173770; x=1707778570; 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=0dsOQxIuZglfzGL/UkHixra2YxsOVoucMj4slNXKYQ0=; b=G49EihBdrlNvwwK1oc/OgwH7T2gFqcPIdLD91JsmnIi/xAohVvKfyfWT7MoYG94TL6 pX2zmv+qv6UvM9TRy9xVuxwcKfLf+R+4qIXNx0PUOcm468SN9EOsUCeUyU+ZiCVmOzfH wz/fAlizL+UF15nfSKK6a4IwFG/TKTHD/2drH98UfcPIJzqJgj+p9XKK/RZJEyGZy5fT UA0kYBa5yfa8cGmPMGdrc/DHFWuQuoghnTzhc9TmFWyVflh7/mnNWdi40J9XxoVOAFi/ rdDqzeL4wcq/XmabTqofSsKsDDxTR0yNIL3P7ZgaIDvUK7TE8e+tbLr3aHDd/8SCjD+A OWqg== X-Gm-Message-State: AOJu0YwK5TCho9sfSaoiNBMUFwoyS5KUOH85Xbx+bxQzvIPG/Pize8ux IW41tww8aWq9a7kSL8oXD4PAcb/wMRxYV//Yb0U/FU7PjSnfJzHf X-Google-Smtp-Source: AGHT+IFV1B+G9o8CvxcMN6huuwce6++oo07ZBBECnU4NSWU45WI0gVmfqhod2IRB+zTWiuirM2ArzQ== X-Received: by 2002:a67:f943:0:b0:46d:1a45:fa17 with SMTP id u3-20020a67f943000000b0046d1a45fa17mr297997vsq.7.1707173770466; Mon, 05 Feb 2024 14:56:10 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCXv2T5epHhjVpbPo8Hg+vYMCZlBiki07P+Chd3M52zwZTeQd1rAFiYC5lKFN4O8RLtJxJOEJ3QIHY4vvZjUMU4jv3OAOraB6/NpWN+FMzZY3vRcmuacj4pqGleuweIfJvM/dNZcp61u5lSRJd8ruOUgVyVhFmmAaCCovDU8kXn785oiLgAvM/pnvj3wixwysmjfWA2CSL4GCnJTfczW3SID87jJVgA376FhH81Ofsm2lb2GwLownGFbb2iniL9vd9BjVYsiCNj7lL9BX6Lwl5vDbm74cijKrF73E5lL898jM96kPHExYDMMZ3ht5/K3X7wqfYmbAMXkqS+Ak+Vl5kc3QK2EOJ36MzIDu6SC8Zr12yEKKKd/Yo3quldFrKHOnAC7Q/5ACWD7sEubRGXLIIo= Received: from localhost (fwdproxy-nao-006.fbsv.net. [2a03:2880:23ff:6::face:b00c]) by smtp.gmail.com with ESMTPSA id mu9-20020a056214328900b0068cac993878sm424486qvb.115.2024.02.05.14.56.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Feb 2024 14:56:10 -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 1/3] selftests: zswap: add zswap selftest file to zswap maintainer entry Date: Mon, 5 Feb 2024 14:56:06 -0800 Message-Id: <20240205225608.3083251-2-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> Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Make it easier for contributors to find the zswap maintainers when they update the zswap tests. Signed-off-by: Nhat Pham Acked-by: Yosry Ahmed --- MAINTAINERS | 1 + 1 file changed, 1 insertion(+) diff --git a/MAINTAINERS b/MAINTAINERS index 6527850f24d1..126090f0e418 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -24398,6 +24398,7 @@ F: include/linux/zpool.h F: include/linux/zswap.h F: mm/zpool.c F: mm/zswap.c +F: tools/testing/selftests/cgroup/test_zswap.c THE REST M: Linus Torvalds 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: 770206 Received: from mail-qt1-f176.google.com (mail-qt1-f176.google.com [209.85.160.176]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 83DA6482CD; Mon, 5 Feb 2024 22:56:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.176 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707173775; cv=none; b=Qi3HCP2GlYjZ+/Qr26XqSFKiznf6a2EaZanzJqLUIljGl3KkGwWMIlNca1NyJrLqatPwN8s75bByFa5D8GscmsuBN6qmNYQVi+U62XfFKD2y4M8aPigISyArt+TPqbMcGnmk8ritaizLUZUvfbMsQNDYvcErw3FurIug1cCYXgU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707173775; c=relaxed/simple; bh=Tie7E5mz6Z/lTyy1A0uRA2fDXZNOAArdxEQCEDzT0do=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=BfOt7DvuMB6G5UoRzb1kSLsOKXA6W92F0Q+QD1OmAe6bcDv+XBwa9L31XjkB1uHb+GfClBb7Azt4csoobRQKZGebll0TvXtzHsK++YctGxQBS6DwFhhJTji5GZX4rNqxhKHJOnVeOOIHTSSPQ0Chkb3FmQ62mByi9ja0tsKi6u0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=mTaCl+gl; arc=none smtp.client-ip=209.85.160.176 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="mTaCl+gl" Received: by mail-qt1-f176.google.com with SMTP id d75a77b69052e-42ab7522c75so28861251cf.0; 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=vger.kernel.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=mTaCl+glq5+P0E/uvDegW3hvaSAfJWv8eJA61HO8+76+lv9C/vbGob+qhaARnzpKPo Oj8YalEnBSZsEAFMtZlV3oETUmglose3w0jy+6TZ+XA1QsLuUZuZjXyoAK20mmi87Ek4 o+fnaWLeTNycZE7rsw3qJfw1WGIq3zFMHDK9WUAvlmqW+g+ryLMDA+d41FqRlpN7ysAM JDQutzmFpijK5RHfX9Hr7K7xYWnemj0YxAYcsgusRNO/nJ3nbu14Id6hsgLia9oVVYoH YqRPhunMB/bwNgsvTemLqWSWruDIxgQqsYuN2spp1itduEL6Aj8RGdR28RAGs0GlNGeV zM4Q== 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=p7DQ1XqgkxXOkxAdmbeVFBgR2F2x+BZFbLBQl+jOCqQWWtWQHIJZQZvFQBs72w9jBz 2oRT9NGEbXh6MVYcGLP2D9PpIyTQpNL0Ul8L8gnSEh3P/8TMNhNYsrzCq3Wkm3QchRbG PqdAMYsbctyDNwmOwBwFh3bzHnpl0A+E0zHqSGn0cvMPTj72ciQ9YqVTVtojMEoP3pl4 dH3ndwKKnMx2S30fk604xhRecsYMchTXa8EUdqjNgqkoT7tXM4cidIizJBZ3/gVt8Isu TLAEWMJwD6QCha+uFNxe5Mc1myw+k86sdIifGaiKlfQKcrORospimpjlL7pe5RIXFXIG WJLg== X-Gm-Message-State: AOJu0Yw3R3WPK+Nou0a4t6u1AnYpapSCl0ZeCm/Fq3jrmC72D34ZktAy XD9EhdRrUSK/taoRtsTlRkmQHkyrXFgDY8XTF90mXGaBRMjTp3mS 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> Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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 --- 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), };