Received: by 2002:a05:7412:bbc7:b0:fc:a2b0:25d7 with SMTP id kh7csp2699291rdb; Mon, 5 Feb 2024 15:07:22 -0800 (PST) X-Google-Smtp-Source: AGHT+IExapO6m3Xw54usj4tIbddqw2AEJmjMY9To29/nfFDV/gsg8YAlw3CwK51o6Cg+2C8A9t+O X-Received: by 2002:a05:6a21:6d9e:b0:19e:987a:59a6 with SMTP id wl30-20020a056a216d9e00b0019e987a59a6mr483559pzb.59.1707174442191; Mon, 05 Feb 2024 15:07:22 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707174442; cv=pass; d=google.com; s=arc-20160816; b=B+g7mhv6olgCPU99MG0oUy91D2QtKHMlKNJ5o6rr3c4udRqTG3jEWhv6EXXst6d+bF f4/b+VnvmfD4kDkXVWEFpwWHs/Runq45zul727I2Yvii57ByzjShsLoDdEhrvnByL2H0 IEGMC4dF4BH8F5l1A7x7A+bcJIfppV83Lm/NsvPzpIfepdk+Yc4grJQyhQllYgm36jKX aUVePf7wW1DxE+7Im9NAh5TG3h3azsBcSwFlJEXoaREE4HLtZEGWpnnnTRb913hcF3vR R6GsEB8jWfBI9dToKUBGhmC4RkbfaEsclNN4M2ZTkOJTghl9Cl2usb+sONktR7tPSZj7 sRbw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :dkim-signature; bh=BZxrWnq5zjsPkEKkrjYrS8M9CKMdrdKLinApXAx16Nw=; fh=uSLYP6XrkJZ5NO0ZSnf4SLOfmbAbQJ2JQN5KqB+/Q94=; b=me4aBWqKExopk1Pf8UFl1GkzwMjXBAtcR7VyFkOVywy4BHDVxiOjeh7i5PSnbgDR/D 5wSwqivdGZNc9/GeAvE87t35orRD3Nt6nR2Cf2kQmQDdzz+tQcGgim62oQBnM+VYDBf1 BQv6VYjUphGCW5DUmwrxUllDhxDdeLxntICdKd11Hhhkwz3f6VBz2j4mnDaDuOHLrf/n fenasOX0GZ5bE9HBDYv6pdMKnSTjTjQuO24PRx1luvR5AfhKB0NxsPOK0j8BX3yzCrvb RP2lIKJPm+1Vt9tYpuhj9IkqtrRKjg7tk7tyS2oBjef5m2NzM/Q3jIJCAxhlizAymJIf DPlQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="TKzJc/BO"; arc=pass (i=1 spf=pass spfdomain=flex--yosryahmed.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-54066-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-54066-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=1; AJvYcCVPtM1BWw/pK8SGtXv0z2K5GVP358UzU9HObLMJvFnWqb+ecz2EpRjsaho4asjEErCh0OxXvf+yV5dAYrmNmnXro4Ty770Hurpy2pwsHQ== Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id d5-20020aa78685000000b006de448c1e87si511516pfo.90.2024.02.05.15.07.21 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Feb 2024 15:07:22 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-54066-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="TKzJc/BO"; arc=pass (i=1 spf=pass spfdomain=flex--yosryahmed.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-54066-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-54066-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id E0CF3292338 for ; Mon, 5 Feb 2024 23:05:22 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 3F9A6487BE; Mon, 5 Feb 2024 23:05:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="TKzJc/BO" Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (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 458C6482F0 for ; Mon, 5 Feb 2024 23:05:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707174308; cv=none; b=uKpv2To5urzci0PqHXcZ0XFW9yIOMFsJMEy6DsWusxtCHTgWSpMgqOWFHzLKTVibNIBfdZUsfwZmIKS1GjT8QddOwCYNyIT6GU/O1xFEgvqtYhl3NJSSe99FovBkWcu6LfndChzhVsXHEWVCFxfF4dzTjGTMm0sfcLerA9l9TEo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707174308; c=relaxed/simple; bh=r7hwl9FZPSbwtzf91HLG2uYS3wWs5xJYPgd288Oe7tU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=WwtOgaS3v8qyKxeou6GOBps5BpdgOoA5dHuw/0YkeYjNaBdBtGvU3i/vQiwXlKi2lNwsXv1Yb/9FPxYVjWQJcVqg1n3FKvGNqFP4YlHcCo+B6GgpTTLXa4+FkwDZtPQ4oqA0o42XxYCnc7vck3t4pcrz7kdGUGu32+N99x5+5Q4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--yosryahmed.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=TKzJc/BO; arc=none smtp.client-ip=209.85.128.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--yosryahmed.bounces.google.com Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-60416f4cba4so91547867b3.2 for ; Mon, 05 Feb 2024 15:05:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1707174304; x=1707779104; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=BZxrWnq5zjsPkEKkrjYrS8M9CKMdrdKLinApXAx16Nw=; b=TKzJc/BOKkbOwVMIjJZKR3Q42nBn6EtR2HouUMZRJtZRYg1hL3dyfIKLoB3+WLFf9m TwFjk4kud47L02YJEss4rYDWIOB1nZ0mt2MyxaCKobGqRaucivPsKa3xpyGILWIGIkth ANlZzzdkse0Tvz0ed6qMKEdb2TTeAQLPLUgRNlTzV7MuLxDN4Mh9DdCyohry80dAa2Y7 YLqtnMbNELFIlBqc/eeqYVCsLE11u8bXtPn58In7nIEtJdUzENUOMcKhinH8LOi2j7gv z0kZbwkCfNUJQj5S8rWQAZwP/5qXOBClEo/eCMsVK37Ebg6VnSaN7BwI3SLwjGzIVtLt zBFg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707174304; x=1707779104; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=BZxrWnq5zjsPkEKkrjYrS8M9CKMdrdKLinApXAx16Nw=; b=OTLg5b76sJH4GM/osfm4b8F7/avX6HFjGuTzUtsL41zmcc6mE4/Ph6f32eaVBXIhqS qtQds+pyxjvo3lJcWRgbApC7K4EEsQs+X161fNw+dob9PxEVIhUOXR+55KRG0/R2rO1M wXBa+Ll2Za3LDgy8o2QvpQM8U264AiriKgK9WS/07uoudQmOsYleaW8bTUYTPbanrN/b fFCcQ3XzSe4mON7v8clO2oWWat4hQMXv6LV71Igyp5YSG92dT8aZxbYphaUV7VV0PhXJ WiZDC1p+tCR72B2MYMHXrxgGrekgpRqwx6TCGBxOudRcCmyNfgQlQGQ/LIdd3fyaVzPY IFZg== X-Gm-Message-State: AOJu0YxrIOdFKh1qxtgncxJD7PW2z00vkajoiw7ArjVIuaT4z/PmpPgE dwhvssFKlMbplVTVSZ0duQMfPymdeZ+Q9fiP5TPUrROTWlh6dF5xhA12MDtRyhL0AcAnaKTBT6m DvoISqgUW1Vrcf8t6Xw== X-Received: from yosry.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:29b4]) (user=yosryahmed job=sendgmr) by 2002:a05:6902:e04:b0:dc6:af9a:8cfa with SMTP id df4-20020a0569020e0400b00dc6af9a8cfamr225097ybb.6.1707174304249; Mon, 05 Feb 2024 15:05:04 -0800 (PST) Date: Mon, 5 Feb 2024 23:05:02 +0000 In-Reply-To: <20240205225608.3083251-4-nphamcs@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240205225608.3083251-1-nphamcs@gmail.com> <20240205225608.3083251-4-nphamcs@gmail.com> Message-ID: Subject: Re: [PATCH v3 3/3] selftests: add zswapin and no zswap tests From: Yosry Ahmed To: Nhat Pham Cc: akpm@linux-foundation.org, riel@surriel.com, shuah@kernel.org, hannes@cmpxchg.org, 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 Content-Type: text/plain; charset="us-ascii" On Mon, Feb 05, 2024 at 02:56:08PM -0800, Nhat Pham wrote: > 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 LGTM with a few nits below: Acked-by: Yosry Ahmed Thanks! > --- > 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 */ nit: s/go/Go > + 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 */ We removed this comment here. > 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; > + } We can actually check that this number is >= 24M instead. Not a big deal, but might as well. > + > + 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 */ Yet we added a similar one here :) > + 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; > + } Same here, we can check that zswpin is at least 24M worth of events. Again, not a big deal, but might as well.