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 8121BC04FFE for ; Wed, 8 May 2024 16:49:47 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E2D256B0089; Wed, 8 May 2024 12:49:46 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id DDD336B008A; Wed, 8 May 2024 12:49:46 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CA4D86B008C; Wed, 8 May 2024 12:49:46 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id AD6596B0089 for ; Wed, 8 May 2024 12:49:46 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 5F8181C022B for ; Wed, 8 May 2024 16:49:46 +0000 (UTC) X-FDA: 82095815172.14.52F2507 Received: from mail-ej1-f49.google.com (mail-ej1-f49.google.com [209.85.218.49]) by imf22.hostedemail.com (Postfix) with ESMTP id 86B10C0018 for ; Wed, 8 May 2024 16:49:44 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=arIybg6q; spf=pass (imf22.hostedemail.com: domain of yosryahmed@google.com designates 209.85.218.49 as permitted sender) smtp.mailfrom=yosryahmed@google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1715186984; 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-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=eqiY82pP+oUOkFDwK0rSdutI8cJvBxh2Jo3lnsppe7k=; b=Wtk6N6DEC7dcmkWRgbdtFMl0468YwsivE3atJwFfroTNpNEtlPu/WZ+1IuRjPQycHFbePR XyQw6bvfaBK+cwqx6GYQ+qETBKXj8cuhL0FCqWkyoQBRXHOSi1sTn+O4vFqhqVRPFAmCTF X1nhTIg4VswZs1PnTultUGDGpghkh8E= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=arIybg6q; spf=pass (imf22.hostedemail.com: domain of yosryahmed@google.com designates 209.85.218.49 as permitted sender) smtp.mailfrom=yosryahmed@google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1715186984; a=rsa-sha256; cv=none; b=zbajnEQAp22dqc1XttuFGt8e8JXPMi3MerATuvSZon58TLRxO1Iu2gSLc5Z0rEZNUkCG6n +9SrNfnmvga+0Ezms7lHyqESop4Gp4pIXtmpUbPZ2dTpj773aZFD3Kx/YdhCcOVs+VT+0f nAeqVedwLxhLWnYq/K3OeT6BtNhoSO4= Received: by mail-ej1-f49.google.com with SMTP id a640c23a62f3a-a59a64db066so1183887666b.3 for ; Wed, 08 May 2024 09:49:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1715186983; x=1715791783; darn=kvack.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=eqiY82pP+oUOkFDwK0rSdutI8cJvBxh2Jo3lnsppe7k=; b=arIybg6qyUa+fw3FY+xDhLa+258G1FrAmVQ42Fe4TLREeDaSd7xFz7uH7QRqLtFNQN KweRYkpkqtIc2J7uIz4lDGOZmIs/otCPFNYmWGPHkpk10imdiOlTbDwLWrQAoSwW7VWk R+swi/XR6Rn5/Zm4CjVmBDyPGGmnRy3NUshy4JX1TVe7up/1ZRrVCwB0jKhqytqvMJU/ wtbtL6FunhBaxI7t6hbQTbx5LB3peizUpu5SXstalP18Kzmhrq7O2orkihI9ChFg5/2P I3163GKMilYJrpEVDTrhbvco98SMtTye9DxmY00PYkpVbNLH5VB9PSHNqhJh+u5oVlJw KnZw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715186983; x=1715791783; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=eqiY82pP+oUOkFDwK0rSdutI8cJvBxh2Jo3lnsppe7k=; b=ftdQaLVyG2qogGd7r280q05wvrZRWPyyxJ2tEtp1QDbHL+Qk+FqfowRra7SfgiuvD2 Phsp97X9XzCWmKSd34f89/T6j9ILRgj6dfM0Cm6hwYEyBjJbU/p1a94yM09YB0XiSEj5 CapwnIv2R28AEFDcvxHWC12wVP4V7YWgsTcAbqJSq39xtgUAyW5HB9q0/6SggjgEhxs7 dmsq87Koj9EPsmnbNbv0I6UlcQxOP35+UnnF8maioZN9bcu6In0YDRN8vp1JMl34rA3e yV3Z+A4oO7crD4ZRI4gRZuYXH29Vw2QKFHlz2mPNNGCEqOAPHPWM2gQqQpim94WNd2lM rqdQ== X-Forwarded-Encrypted: i=1; AJvYcCURwwTMQzS0K8WTBSclN+kT+a5t6EsTyzmXteG74MtzVFG5JMrb1y0SP5ZJbbggmPE6ynPGQQGHWj9gBGeCW2/AWjg= X-Gm-Message-State: AOJu0Yz8c30w9ks4sZPDADF5piS4JtlPp/gKt7oVYXqtUvsS3tnENP6U iTK3Fvr7QoAcGSKMEABYrGECgUbv7zvXOri7gnRdzQq8G9zyDNJpS5gZaNmt43Zyq3tWzO3mljA l400eB5mHI/4dxQdkzptz1T5nFfGHDA687PvI X-Google-Smtp-Source: AGHT+IGylNLJehM/FOKtigjjdYwggjBh2t+TVlXasYlgbtfZnEPnX/eY/4zlcTXlMuGNKTOtkKB3pmH3Ddf2g4jqwlw= X-Received: by 2002:a17:906:c78c:b0:a59:ced4:25af with SMTP id a640c23a62f3a-a59fb95d55cmr183230266b.41.1715186982572; Wed, 08 May 2024 09:49:42 -0700 (PDT) MIME-Version: 1.0 References: <20240508102222.4001882-1-usamaarif642@gmail.com> In-Reply-To: <20240508102222.4001882-1-usamaarif642@gmail.com> From: Yosry Ahmed Date: Wed, 8 May 2024 09:49:05 -0700 Message-ID: Subject: Re: [PATCH v4] selftests: cgroup: add tests to verify the zswap writeback path To: Usama Arif Cc: akpm@linux-foundation.org, hannes@cmpxchg.org, nphamcs@gmail.com, chengming.zhou@linux.dev, linux-mm@kvack.org, linux-kernel@vger.kernel.org, kernel-team@meta.com Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Rspam-User: X-Rspamd-Queue-Id: 86B10C0018 X-Rspamd-Server: rspam06 X-Stat-Signature: 1c7k5sd6s7j5p666hkkqx55oe994ek4w X-HE-Tag: 1715186984-684345 X-HE-Meta: U2FsdGVkX18Rf8B/cAL3k/Tcap+XH2o5cKBZj01Ya2g4JyoNSH+q03pnDnXrzltcfvWuJ9xjBAc1rtKqdIZdrXZQWvMmGzuViSUs5qzcmAdldxdtSd3HFpna7liUTHGocJgOO1nea13H2oWr2M1eXMp9+Mmd1Sb1VP0W7hQHV3lLvlGsBdjw9iDLqM1hCn1nUEvhHUlpPlqcJlL6olBLD2RiOH0EWymmzOEKC+UTM+oM/PMT5iU7XOw5tpzIMJ/V600jCdCDqlfF8OQMuw6/4FDtlc9WwoVl8TVp8LSZ3awohRsAeEzkuO6gMlYrKblQZlxPM/wv7eKOlLr7QFnZUpf4q79Bn+QX3V2jLzSqTCryrDOvkAKeFQ+8+WkWU0ikG8oWK7YIMjC4w/yyV3di9INwAjloptWlOnBej9wFwCzMzLDT5YRr3INxlR/FYF+eTlR6YO1AycVrNxSgXyrafHwAQHqf/i8qULx5o+vDek2xq0kHXV8H9pSCmIZ5t1snERVtvhr2A3zf6DwT+CHRuKXb431pHW7UadC6nyAaBxjVx2nrTXOqgo38CAVFnpD9yfU3LEAwnjSMwmK8b3GAK3YAelOZk8JWoROaK/oH9ay1njeAeRymHuZUivhpc+39Cxc/R4YxI76V6SzTCbAui9JpoCqB2LyWpmLkj7IOW9LoflT9ZTNlgghu+uMmtAG7CYr4zMAFA5OmhHIO+DE6UggiHF/OWq2j7wgQnSJOKYHVVzuWy5Ks6xqLT9NlDaWwVUnIiEuk3C0Xw9+CIJe1HN2rvZM88afpMx7cPJNgoIxiR3RbVfumNNXIvR192l1+YK4MBRg5y/tloFLfK7ryViBPa2D9HN3lRJJNX3WoOPYFW34uOl21zeG/OY5bhC/MyIKUwlE3Snh6axnDQo08yMtlniugiLeSAeKQdkAHk1vOwz33ExDbriFYzh/8uOftxHBIO1o1HQ27xNGWR6b bMX7rvUW fK2cMFZhRZiv6Y5x8eLxhUQt9tLgPgj78zY8jw1X3y04ttb9krnsBE6Kf4wmhJdJMdSy/PYrMpzNE+FUl5LrSqZhQubcE5pt8bPdzIzohOT/0x1Mf2McMYinUQ5ldLATuMUgWlTViLops0ilJR69WQQAGJ+AzdU95We11UscWvWoMdjZ9JXPszs1m8hYalP4wchzaR+koBw0Pj3HqaVx3D2QJyYwlVyOghMkd5XksqFh2caN/8pL+nVX6Vma66uAXSNkj3ohyAK8ozkASv20L8l+tww== X-Bogosity: Ham, tests=bogofilter, spamicity=0.038887, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: On Wed, May 8, 2024 at 3:22=E2=80=AFAM Usama Arif = wrote: > > Attempt 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 it's 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 > --- > > v3 -> v4 (Yosry Ahmed): > - Use a fixed page-sized buffer for filling and checking memory when > attempting writeback > - Use cg_write_numeric instead of cg_write for memory.reclaim > - Improved error checking for zswpwb_before and zswpwb_after > > v2 -> v3: > - Remove memory.max (Yosry Ahmed) > - change from random allocation of memory to increasing and 0 allocation > (Yosry Ahmed) > - stricter error checking when writeback is disabled (Yosry Ahmed) > - Ensure zswpwb_before =3D=3D 0 (Yosry Ahmed) > - Variable definition reorder, function name change (Yosry Ahmed) > > v1 -> v2: > - Change method of causing writeback from limit zswap to memory reclaim. > (Further described in commit message) (Yosry Ahmed) > - Document why using random memory (Nhat Pham) > --- > tools/testing/selftests/cgroup/test_zswap.c | 130 +++++++++++++++++++- > 1 file changed, 129 insertions(+), 1 deletion(-) > > diff --git a/tools/testing/selftests/cgroup/test_zswap.c b/tools/testing/= selftests/cgroup/test_zswap.c > index f0e488ed90d8..beab9b979957 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,132 @@ static int test_zswapin(const char *root) > return ret; > } > > +/* > + * Attempt 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 it's enabled, or reclamation > + will fail if writeback is disabled as there isn't enough zswap spa= ce. > + */ > +static int attempt_writeback(const char *cgroup, void *arg) > +{ > + long pagesize =3D sysconf(_SC_PAGESIZE); > + char *test_group =3D arg; > + size_t memsize =3D MB(4); > + char buf[pagesize]; > + long zswap_usage; > + bool wb_enabled; > + int ret =3D -1; > + char *mem; > + > + wb_enabled =3D cg_read_long(test_group, "memory.zswap.writeback")= ; > + mem =3D (char *)malloc(memsize); > + if (!mem) > + return ret; > + > + /* > + * Fill half of each page with increasing data, and keep other > + * half empty, this will result in data that is still compressibl= e > + * and ends up in zswap, with material zswap usage. > + */ > + for (int i =3D 0; i < pagesize; i++) > + buf[i] =3D i < pagesize/2 ? (char) i : 0; > + > + for (int i =3D 0; i < memsize; i +=3D pagesize) > + memcpy(&mem[i], buf, pagesize); > + > + /* Try and reclaim allocated memory */ > + if (cg_write_numeric(test_group, "memory.reclaim", memsize)) { > + ksft_print_msg("Failed to reclaim all of the requested me= mory\n"); > + goto out; > + } > + > + zswap_usage =3D cg_read_long(test_group, "memory.zswap.current"); > + > + /* zswpin */ > + for (int i =3D 0; i < memsize; i +=3D pagesize) { > + if (memcmp(&mem[i], buf, pagesize)) { > + ksft_print_msg("invalid memory\n"); > + goto out; > + } > + } > + > + if (cg_write_numeric(test_group, "memory.zswap.max", zswap_usage/= 2)) > + goto out; > + > + /* > + * If writeback is enabled, trying to reclaim memory now will tri= gger 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 =3D cg_write(test_group, "memory.reclaim", "4M"); We need cg_write_numeric(.., memsize) here as well. Otherwise, this LGTM. After fixing this, feel free to add: Acked-by: Yosry Ahmed > + if (!wb_enabled) > + ret =3D (ret =3D=3D -EAGAIN) ? 0 : -1; > + > +out: > + free(mem); > + return ret; > +} > + > +/* Test to verify the zswap writeback path */ > +static int test_zswap_writeback(const char *root, bool wb) > +{ > + long zswpwb_before, zswpwb_after; > + int ret =3D KSFT_FAIL; > + char *test_group; > + > + test_group =3D cg_name(root, "zswap_writeback_test"); > + if (!test_group) > + goto out; > + if (cg_create(test_group)) > + goto out; > + if (cg_write(test_group, "memory.zswap.writeback", wb ? "1" : "0"= )) > + goto out; > + > + zswpwb_before =3D get_cg_wb_count(test_group); > + if (zswpwb_before !=3D 0) { > + ksft_print_msg("zswpwb_before =3D %ld instead of 0\n", zs= wpwb_before); > + goto out; > + } > + > + if (cg_run(test_group, attempt_writeback, (void *) test_group)) > + goto out; > + > + /* Verify that zswap writeback occurred only if writeback was ena= bled */ > + zswpwb_after =3D get_cg_wb_count(test_group); > + if (zswpwb_after < 0) > + goto out; > + > + if (wb !=3D !!zswpwb_after) { > + ksft_print_msg("zswpwb_after is %ld while wb is %s", > + zswpwb_after, wb ? "enabled" : "disabled"= ); > + goto out; > + } > + > + ret =3D 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 mem= ory > * limit in zswap, writeback should affect only the zswapped pages of th= at > @@ -425,6 +551,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 >