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 85025EE49A3 for ; Tue, 22 Aug 2023 15:55:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 03447280006; Tue, 22 Aug 2023 11:55:27 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id EFF8194001B; Tue, 22 Aug 2023 11:55:26 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D7964280006; Tue, 22 Aug 2023 11:55:26 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id C159694001B for ; Tue, 22 Aug 2023 11:55:26 -0400 (EDT) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 97402B1C5E for ; Tue, 22 Aug 2023 15:55:26 +0000 (UTC) X-FDA: 81152190252.19.E54E14C Received: from mail-ua1-f46.google.com (mail-ua1-f46.google.com [209.85.222.46]) by imf23.hostedemail.com (Postfix) with ESMTP id D245314001B for ; Tue, 22 Aug 2023 15:55:24 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=gmail.com header.s=20221208 header.b=TU3fLEui; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf23.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.222.46 as permitted sender) smtp.mailfrom=nphamcs@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1692719724; a=rsa-sha256; cv=none; b=XZqsisZZmy15XzaPZcwPvdpGm/4ukCG3WhFsLgtFSisvDo6AgQpDBxldL6dqlOy5AQOQks urr43cjh8XrfJz8e5SXNSv5b51wRAFExPr6g770Kb2vIBZ01JeNI/9oX4GWDJQtlsueMG1 W1kAcvfLP4YbVsap1J14blKK1g3YolI= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=gmail.com header.s=20221208 header.b=TU3fLEui; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf23.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.222.46 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=1692719724; 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=GGDC7f3iS1dC4ufAdA6D7FCUMW4+E/u8Z+mXRP5rOco=; b=goGNQokeEvmscmIAU5wIbfQsKo/w9t07Ri3lobCySLxq32d9VCm/pVaAqhq+SS5IgC403b Yv0nxZ4DLkHJF8jfU7+K9XnOf12dEK+0DplGNJK5FEe7Cx5NzVCpAuXTxFt0d/US8UR7Nt 86KYn4WKr5dFfT0trWeqBVpaRuXDU+Q= Received: by mail-ua1-f46.google.com with SMTP id a1e0cc1a2514c-7a02252eb5dso1112261241.1 for ; Tue, 22 Aug 2023 08:55:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1692719724; x=1693324524; 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=GGDC7f3iS1dC4ufAdA6D7FCUMW4+E/u8Z+mXRP5rOco=; b=TU3fLEuiW3VIJpmZP5AdlUmiZOFwZqLUUH5ZTVBYlWBFfTK3azd0E3gJEYJFJA1qFq 36rz7GFv6kiuzLjw3+davKSFG0pDBmW6E41EBqGM2pi6RY8SV2bDTgkj+icAAQ//+KbR 5lLrO7gViUG6RgPoU5YWKFH+ib/8pgze1pfF9+KWvIAgXBeNxvb2eveGtcEQvoFsBTYo IgxHb8nx7BWXRLtqjI+Dz4PwPA9G1gq/2/XvdIBfZ0Fe/MmFAp1H3zB0KfAZLWwV9d6m oKl4alKX6HWEqPB70fI4H8QdIFUZCpqN8BcHoFOcSFdC+zqbY7kNYG8pREuMwhMM8HFb rQlw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692719724; x=1693324524; 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=GGDC7f3iS1dC4ufAdA6D7FCUMW4+E/u8Z+mXRP5rOco=; b=cqjfj6fP+z9KPdbcODXtzKclPLcydcAzFc7827Wqmg48SxnyK52vL5cMYLrbHO1Ndd p2wK8M4T4tuObdwB19By5owXKLQoIa0lhbVU9zU1Psrc/Nmsu6rY+6a4c4hRKDS4gN3w pU91eX9rYtgWcn7E+vKYwXuFiV+Qf/bbzD+rzJ+/dWJ1G5o/XS6n4oSX1rrRGZZcEhnQ GFmB83mCHr48/omr2BJ7nJK4iObhZuUtuNPJt2j9GHg+Wd2iJMSxXocFTDOlI8H1uqmm dD7V5e1r7z3NdvArHnDlYDnAoQMxN6cNyuiA3dXd+mvpyFl/HqX08qt1B0d8EN/w2Iom 9Wzw== X-Gm-Message-State: AOJu0YyUmLZFtgVxsKesXBexhbJG+VEfyB16obAbdqzITTbz8BYX2yrg XRMZTMXrkdqnOINSUYenYMU0+X4z77Ks/FNKZAQ= X-Google-Smtp-Source: AGHT+IHXPjWyxj5ouCGPqQlGMW2Gf47yy5ZoK3qu1KzWgxaGAd35ldVGFywIxgCQoEJ1ym9JjCSP2ImoZ5qHMPmD0WU= X-Received: by 2002:a67:de08:0:b0:42c:9d63:47ea with SMTP id q8-20020a67de08000000b0042c9d6347eamr6292504vsk.0.1692719723773; Tue, 22 Aug 2023 08:55:23 -0700 (PDT) MIME-Version: 1.0 References: <20230821160534.3414911-1-andre.przywara@arm.com> <20230821160534.3414911-3-andre.przywara@arm.com> In-Reply-To: <20230821160534.3414911-3-andre.przywara@arm.com> From: Nhat Pham Date: Tue, 22 Aug 2023 08:55:12 -0700 Message-ID: Subject: Re: [PATCH v2 2/2] selftests: cachestat: catch failing fsync test on tmpfs To: Andre Przywara Cc: Shuah Khan , Johannes Weiner , linux-kselftest@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: D245314001B X-Stat-Signature: rxjkii16c9r48jsba9x58r3t6tkajkmo X-HE-Tag: 1692719724-116149 X-HE-Meta: U2FsdGVkX19KLSg3vCiDR836I/+r/8Lx7y1bygoVVY7M6Z4L2pU1EiCYcuoa8UH0XZ+f5IRAGoVDfp6a5X3lvghpxnSCF8BXs7Yx2Hq9+kcXc/rOTDtKy1wqaJ11GRkkx60uBflEUOJdEzUpWu96ETQnF9K+EHAbsgW5gDfAzh/IcsDYUKuThR16WiFl+5iwRhBLLsAAMxwF+G2mt9oJklBZY30mlXJaGEvTzi9xGJ13Mb2KWyVj7ZZWou1y5bbM+sjIkUNmHwK5KX7WgiqQYGm+nLTwbasb7hA8N2qL6jO/xyHeJL2PBmtqULNlyDxNBthdP3OMn5Bzt/9ecDrT8DaxVqmxPFXPr8FF8hqxPHD12z1B1/qOGbi5LTOzzkom0Q6OTfwdbcmu3PMl0lQwRHQisSFOVzy1pWc4YT51jJpJPxMj7QjZ3JlJITz4rfw7e0lwc5Ex4Vixat1mvTO5tKQXeWuccXUo/XWyu5/5TNau6mSCdKI/5Q4Jd4aLN0XHxvrXXEjcgrJZD/bG9xGxPDRgxlKJGQIA5cc8lHWrZvADC4mKv/oqYYHaGYM0JYc0Kjck72qf0hB0vb7VRkQcx//PYHBs6Zm3njIUgN2IHdEtME0bfp0rwQtZ7s4nzWFprxFJmcVKLQrowgmKsGsCd1UlqBqjKD05w8R0NwnIcUwCVO4FfswM0kN6E373jNWVgHvbCu2f5GBzBRXl2T1C5/++5PVLU3rQNXe3/94WGR8e/ApEJgvpuzx+JxqL3F48rtH1A2GtWv4TXjrMBlBAjl3Pmzjj8oNDSX2FIA4M0/3I8BfI61+8A8YidBeLY/SnYMKiM68EegBVjbb+xTFvMisJXrmbQ8Za86tuSCtltxx1BC+Nlz71ovfpvq7WII/465CIwN1McscBJhQIT++dCDsdrrT4yaCc9vfCQ6I/diCppmv6xJES08zhSCWjU+78EWeicV2mMlX8o4NVyl/ LNbgvHXJ DCa/I9WQs1UUj0BiwCtUQ0ge9ZA2N2QxJLNXVClMa4plCZPKKRD3zY0DVGIoprqeWz3cdLfcBAmIPX9QkKvdYuueLoCeG12jo53TiSlLpGHP2LSqJj8Z+T33/ygcZB1clEJCdf0SQqxmcddySwSyl1pZj/8TbpS9Hfjfi5Iojx0nsTnHHNlNFnfu9Qv6EntG8DPV1XIwNRExPgWjLcC3huHb2MFtWOZj6BUfwkcJMyixpavsD+/EvdNrtmF/s46tRgMZR/cqoHwVsXl8QkIRQFJB+lEVfgk4+XK0IL6dmB8/iznVaOD9v88rb4oZgR63NCSvI3P8lifCMSH5BgSIBQgIaEneW6c9Ut6KJ4TIlejuGgcIeUUrQRbPgP04tbaTogiA9hW29rXDUmnlim/4g69OqRxiVr1QXcWALf0aITfPoScNz3dsoNgG/ZQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000012, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Mon, Aug 21, 2023 at 9:05=E2=80=AFAM Andre Przywara wrote: > > The cachestat kselftest runs a test on a normal file, which is created > temporarily in the current directory. Among the tests it runs there is a > call to fsync(), which is expected to clean all dirty pages used by the > file. > However the tmpfs filesystem implements fsync() as noop_fsync(), so the > call will not even attempt to clean anything when this test file happens > to live on a tmpfs instance. This happens in an initramfs, or when the > current directory is in /dev/shm or sometimes /tmp. > > To avoid this test failing wrongly, use statfs() to check which filesyste= m > the test file lives on. If that is "tmpfs", we skip the fsync() test. > > Since the fsync test is only one part of the "normal file" test, we now > execute this twice, skipping the fsync part on the first call. > This way only the second test, including the fsync part, would be skipped= . > > Signed-off-by: Andre Przywara > --- > .../selftests/cachestat/test_cachestat.c | 62 ++++++++++++++----- > 1 file changed, 47 insertions(+), 15 deletions(-) > > diff --git a/tools/testing/selftests/cachestat/test_cachestat.c b/tools/t= esting/selftests/cachestat/test_cachestat.c > index 8f8f46c24846d..4804c7dc7b312 100644 > --- a/tools/testing/selftests/cachestat/test_cachestat.c > +++ b/tools/testing/selftests/cachestat/test_cachestat.c > @@ -4,10 +4,12 @@ > #include > #include > #include > +#include > #include > #include > #include > #include > +#include > #include > #include > #include > @@ -15,7 +17,7 @@ > > #include "../kselftest.h" > > -#define NR_TESTS 8 > +#define NR_TESTS 9 > > static const char * const dev_files[] =3D { > "/dev/zero", "/dev/null", "/dev/urandom", > @@ -91,6 +93,20 @@ bool write_exactly(int fd, size_t filesize) > return ret; > } > > +/* > + * fsync() is implemented via noop_fsync() on tmpfs. This makes the fsyn= c() > + * test fail below, so we need to check for test file living on a tmpfs. > + */ > +static bool is_on_tmpfs(int fd) > +{ > + struct statfs statfs_buf; > + > + if (fstatfs(fd, &statfs_buf)) > + return false; > + > + return statfs_buf.f_type =3D=3D TMPFS_MAGIC; > +} > + > /* > * Open/create the file at filename, (optionally) write random data to i= t > * (exactly num_pages), then test the cachestat syscall on this file. > @@ -98,13 +114,13 @@ bool write_exactly(int fd, size_t filesize) > * If test_fsync =3D=3D true, fsync the file, then check the number of d= irty > * pages. > */ > -bool test_cachestat(const char *filename, bool write_random, bool create= , > - bool test_fsync, unsigned long num_pages, int open_flags, > - mode_t open_mode) > +static int test_cachestat(const char *filename, bool write_random, bool = create, > + bool test_fsync, unsigned long num_pages, > + int open_flags, mode_t open_mode) > { Hmm would the semantic change a bit here? Previously, this function returned true if passed. But it seems like KSFT_PASS is defined as 0: https://github.com/torvalds/linux/blob/9e38be7/tools/testing/selftests/ksel= ftest.h#L74 So maybe we have to change from: if (test_()) to: if (!test_)()) or, explicitly as: if (test_() =3D=3D KSFT_PASS) > size_t PS =3D sysconf(_SC_PAGESIZE); > int filesize =3D num_pages * PS; > - bool ret =3D true; > + int ret =3D KSFT_PASS; > long syscall_ret; > struct cachestat cs; > struct cachestat_range cs_range =3D { 0, filesize }; > @@ -113,7 +129,7 @@ bool test_cachestat(const char *filename, bool write_= random, bool create, > > if (fd =3D=3D -1) { > ksft_print_msg("Unable to create/open file.\n"); > - ret =3D false; > + ret =3D KSFT_FAIL; > goto out; > } else { > ksft_print_msg("Create/open %s\n", filename); > @@ -122,7 +138,7 @@ bool test_cachestat(const char *filename, bool write_= random, bool create, > if (write_random) { > if (!write_exactly(fd, filesize)) { > ksft_print_msg("Unable to access urandom.\n"); > - ret =3D false; > + ret =3D KSFT_FAIL; > goto out1; > } > } > @@ -133,7 +149,7 @@ bool test_cachestat(const char *filename, bool write_= random, bool create, > > if (syscall_ret) { > ksft_print_msg("Cachestat returned non-zero.\n"); > - ret =3D false; > + ret =3D KSFT_FAIL; > goto out1; > > } else { > @@ -143,15 +159,17 @@ bool test_cachestat(const char *filename, bool writ= e_random, bool create, > if (cs.nr_cache + cs.nr_evicted !=3D num_pages) { > ksft_print_msg( > "Total number of cached and evict= ed pages is off.\n"); > - ret =3D false; > + ret =3D KSFT_FAIL; > } > } > } > > if (test_fsync) { > - if (fsync(fd)) { > + if (is_on_tmpfs(fd)) { > + ret =3D KSFT_SKIP; > + } else if (fsync(fd)) { > ksft_print_msg("fsync fails.\n"); > - ret =3D false; > + ret =3D KSFT_FAIL; > } else { > syscall_ret =3D syscall(__NR_cachestat, fd, &cs_r= ange, &cs, 0); > > @@ -162,13 +180,13 @@ bool test_cachestat(const char *filename, bool writ= e_random, bool create, > print_cachestat(&cs); > > if (cs.nr_dirty) { > - ret =3D false; > + ret =3D KSFT_FAIL; > ksft_print_msg( > "Number of dirty should b= e zero after fsync.\n"); > } > } else { > ksft_print_msg("Cachestat (after fsync) r= eturned non-zero.\n"); > - ret =3D false; > + ret =3D KSFT_FAIL; > goto out1; > } > } > @@ -259,7 +277,7 @@ int main(void) > const char *dev_filename =3D dev_files[i]; > > if (test_cachestat(dev_filename, false, false, false, > - 4, O_RDONLY, 0400)) > + 4, O_RDONLY, 0400) =3D=3D KSFT_PASS) > ksft_test_result_pass("cachestat works with %s\n"= , dev_filename); > else { > ksft_test_result_fail("cachestat fails with %s\n"= , dev_filename); > @@ -268,13 +286,27 @@ int main(void) > } > > if (test_cachestat("tmpfilecachestat", true, true, > - true, 4, O_CREAT | O_RDWR, 0400 | 0600)) > + false, 4, O_CREAT | O_RDWR, 0600) =3D=3D KSFT_PASS) > ksft_test_result_pass("cachestat works with a normal file= \n"); > else { > ksft_test_result_fail("cachestat fails with normal file\n= "); > ret =3D 1; > } > > + switch (test_cachestat("tmpfilecachestat", true, true, > + true, 4, O_CREAT | O_RDWR, 0600)) { > + case KSFT_FAIL: > + ksft_test_result_fail("cachestat fsync fails with normal = file\n"); > + ret =3D KSFT_FAIL; > + break; > + case KSFT_PASS: > + ksft_test_result_pass("cachestat fsync works with a norma= l file\n"); > + break; > + case KSFT_SKIP: > + ksft_test_result_skip("tmpfilecachestat is on tmpfs\n"); > + break; > + } > + > if (test_cachestat_shmem()) > ksft_test_result_pass("cachestat works with a shmem file\= n"); > else { > -- > 2.25.1 >