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 D2311C4167B for ; Thu, 2 Nov 2023 12:24:53 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 66D0680021; Thu, 2 Nov 2023 08:24:53 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 61D318D000F; Thu, 2 Nov 2023 08:24:53 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4E48280021; Thu, 2 Nov 2023 08:24:53 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 3C0C28D000F for ; Thu, 2 Nov 2023 08:24:53 -0400 (EDT) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 0E8E8B52D1 for ; Thu, 2 Nov 2023 12:24:53 +0000 (UTC) X-FDA: 81412933266.11.A8729AF Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf15.hostedemail.com (Postfix) with ESMTP id 3047DA0008 for ; Thu, 2 Nov 2023 12:24:50 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=none; spf=pass (imf15.hostedemail.com: domain of ryan.roberts@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=ryan.roberts@arm.com; dmarc=pass (policy=none) header.from=arm.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1698927891; 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; bh=XgykUmLRvRQZX/v1Sas2bZz6maBXTNulaMvM2obg+Wg=; b=J6CzaWpvutacs5B2nPNViW/4WgsuuzQeZ9A13G2VqHOM5FUgA13eAK5Vq96UztYtjF+0pT 2aYQkVVOw2BErhAmvmyNomeBkLkaEyhhLhIu9l7DQ9Fi0bR02/+C8emMLXbOU+wqjIfqpd YslhFwCwDYAVyAcIIHemNHiPFzwZM/s= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=none; spf=pass (imf15.hostedemail.com: domain of ryan.roberts@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=ryan.roberts@arm.com; dmarc=pass (policy=none) header.from=arm.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698927891; a=rsa-sha256; cv=none; b=YdW/UOBhaFclBW8gz/+p7w9cgLafVEbBfZxeznn4twR3r1h8i+iKI9ImDgK7cNMDj6w0yB GZmbL6FqYb4DM+lQVcGEtCSlXgSuHIIxumifdNqWg0CCJ1fRE37AiRzGp5i/SDV6M0OMeE lDdnUv98FKb+u19eZkni/HlczYwM48w= Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 64CA42F4; Thu, 2 Nov 2023 05:25:32 -0700 (PDT) Received: from [10.1.33.173] (XHFQ2J9959.cambridge.arm.com [10.1.33.173]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 0D7C33F67D; Thu, 2 Nov 2023 05:24:48 -0700 (PDT) Message-ID: <662df57e-47f1-4c15-9b84-f2f2d587fc5c@arm.com> Date: Thu, 2 Nov 2023 12:24:47 +0000 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v2 2/2] selftests/mm: Add a new test for madv and hugetlb Content-Language: en-GB To: Breno Leitao , mike.kravetz@oracle.com, muchun.song@linux.dev, akpm@linux-foundation.org, Shuah Khan Cc: linux-mm@kvack.org, riel@surriel.com, open list , "open list:KERNEL SELFTEST FRAMEWORK" References: <20231005163922.87568-1-leitao@debian.org> <20231005163922.87568-3-leitao@debian.org> From: Ryan Roberts In-Reply-To: <20231005163922.87568-3-leitao@debian.org> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Rspamd-Queue-Id: 3047DA0008 X-Rspam-User: X-Stat-Signature: ti4qgrsbifhqsoyu4wmx8yowdq79c5q9 X-Rspamd-Server: rspam01 X-HE-Tag: 1698927890-727101 X-HE-Meta: U2FsdGVkX19eudOHDNMpS3bSYRlj6Zv2Gi4SBq2KKPjBdiDyJX/7adMQmMM5EWNtA51Y9i/v36FJ93bz6dnoDb70y/fPW0OxEWCErF8dyv5jyOQ0KYFfjPJTDY6eOQ2k/n5aZqLFIYrSPqp2BDSXWrrNewE1XIQ8bwnCKYMs/Y1xlA+WZE4FhK/r0l+TpK2L+Gqm+mNeMbNus0FetWsD4wUS8xmN0aAVJkwVYiE2yo8fqeHx0nQ5cu/SfLBAIYnhtBWvmoFF7XWDWmu3aNjdRG6ioOE6eQonVmKuQzA9DlM/Kmeo//0OEpk1shaldHLaTHm5BNjuSmxeFW7e+CeDE9FmNeZGZeoef3R1ftfLeRZ5zKebgvYtf6+8teWl4LZ5nxrVca+f4dMBBmydJC8jUQhwYzU7Q8StZ8UIYP/EfB1yEStZW7Fxk3QehfRAqY/51KdwixzcKCF7xMDhSLZTDRiGqWZzgx0xmKoMuAaSnUzVnCS8GRclBKUVTCDpWSh21vOQDM2pZRZQr9rQLtiM4p06gzTyR62fwreYbligEfbO5rWl2PrtD2iUKRtrAOp7nVoAqbje4BhTwZkZF01LS02/kBi9tfDYMD610kAb5qyEteaiXzeFsEScetb+IcNV1f8M92LDStY/k9v/VDuk0mRv0ZY8QNN3j+cIuB/zB+4r4tgJWno3mGuBbAV3+CUmz6PA2Hh6or3g6340f+gWK9892EDgzhqjmz5c1BiH4RaUZIPL5c5XmQ3cVdg4o9RG3eChjPWNWDsooAhtWPvxvxl6h5l7w4+if6fSENcGRG5kbpzfBAEcgWMlIUnnAOK1UaAjT92Qhp36H1Bn8tBE66Bkt23a6cokziMLcI9TcSzSGgxltmOabsVIBQ6Y3N44FwH5YhKKVIdvnBbr1+6JWSeeGsbGweR4ecj8gyZZF/JZiwjTIfqu4f8IbGwNQY9SJocd4PbTaBJBvrdItNO IhUPa72/ QU+fAYCnGz8CDkcBaV8//d13jXPscUyYHevJOeiTcgD+ciXy50aSRYM0kY2Y8LHz0SlLHn7y0vFZuyN6q6zqGKjJuKiozaq88VG0mzfFA9h149NckTIica93XukXgCSyf1zd2GCmfTO5i/1wkZXTavAQ3DTl/5nU/qWrPW766n6mxSySNM5Ebh6003gufkwvdBPgXQ9Lde2W+N77rjrv/UDfLaQJ7S3BcbG4baPVT2Nya+4P9ZDfRuHHoM9DheSKztOtD1HlhYyPMB0cmXeJ+gjaseipujNNN0kPNysnY8WJb0rBU3dWSdmmV+DyIpiwViKGyQeQNaKolhDvf+WPkR6njJywDMceeGlZG X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Hi Breno, On 05/10/2023 17:39, Breno Leitao wrote: > Create a selftest that exercises the race between page faults and > madvise(MADV_DONTNEED) in the same huge page. Do it by running two > threads that touches the huge page and madvise(MADV_DONTNEED) at the same > time. > > In case of a SIGBUS coming at pagefault, the test should fail, since we > hit the bug. > > The test doesn't have a signal handler, and if it fails, it fails like > the following > > ---------------------------------- > running ./hugetlb_fault_after_madv > ---------------------------------- > ./run_vmtests.sh: line 186: 595563 Bus error (core dumped) "$@" > [FAIL] > > This selftest goes together with the fix of the bug[1] itself. > > [1] https://lore.kernel.org/all/20231001005659.2185316-1-riel@surriel.com/#r > > Signed-off-by: Breno Leitao > --- > tools/testing/selftests/mm/Makefile | 1 + > .../selftests/mm/hugetlb_fault_after_madv.c | 73 +++++++++++++++++++ > tools/testing/selftests/mm/run_vmtests.sh | 4 + > 3 files changed, 78 insertions(+) > create mode 100644 tools/testing/selftests/mm/hugetlb_fault_after_madv.c > > diff --git a/tools/testing/selftests/mm/Makefile b/tools/testing/selftests/mm/Makefile > index 6a9fc5693145..e71ec9910c62 100644 > --- a/tools/testing/selftests/mm/Makefile > +++ b/tools/testing/selftests/mm/Makefile > @@ -68,6 +68,7 @@ TEST_GEN_FILES += split_huge_page_test > TEST_GEN_FILES += ksm_tests > TEST_GEN_FILES += ksm_functional_tests > TEST_GEN_FILES += mdwe_test > +TEST_GEN_FILES += hugetlb_fault_after_madv > > ifneq ($(ARCH),arm64) > TEST_GEN_PROGS += soft-dirty > diff --git a/tools/testing/selftests/mm/hugetlb_fault_after_madv.c b/tools/testing/selftests/mm/hugetlb_fault_after_madv.c > new file mode 100644 > index 000000000000..73b81c632366 > --- /dev/null > +++ b/tools/testing/selftests/mm/hugetlb_fault_after_madv.c > @@ -0,0 +1,73 @@ > +// SPDX-License-Identifier: GPL-2.0 > +#include > +#include > +#include > +#include > +#include > +#include > + > +#include "vm_util.h" > +#include "../kselftest.h" > + > +#define MMAP_SIZE (1 << 21) > +#define INLOOP_ITER 100 > + > +char *huge_ptr; > + > +/* Touch the memory while it is being madvised() */ > +void *touch(void *unused) > +{ > + char *ptr = (char *)huge_ptr; > + > + for (int i = 0; i < INLOOP_ITER; i++) > + ptr[0] = '.'; > + > + return NULL; > +} > + > +void *madv(void *unused) > +{ > + usleep(rand() % 10); > + > + for (int i = 0; i < INLOOP_ITER; i++) > + madvise(huge_ptr, MMAP_SIZE, MADV_DONTNEED); > + > + return NULL; > +} > + > +int main(void) > +{ > + unsigned long free_hugepages; > + pthread_t thread1, thread2; > + /* > + * On kernel 6.4, we are able to reproduce the problem with ~1000 > + * interactions > + */ > + int max = 10000; > + > + srand(getpid()); > + > + free_hugepages = get_free_hugepages(); > + if (free_hugepages != 1) { > + ksft_exit_skip("This test needs one and only one page to execute. Got %lu\n", > + free_hugepages); > + } > + > + while (max--) { > + huge_ptr = mmap(NULL, MMAP_SIZE, PROT_READ | PROT_WRITE, > + MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB, > + -1, 0); > + > + if ((unsigned long)huge_ptr == -1) > + ksft_exit_skip("Failed to allocated huge page\n"); > + > + pthread_create(&thread1, NULL, madv, NULL); > + pthread_create(&thread2, NULL, touch, NULL); > + > + pthread_join(thread1, NULL); > + pthread_join(thread2, NULL); > + munmap(huge_ptr, MMAP_SIZE); > + } > + > + return KSFT_PASS; > +} > diff --git a/tools/testing/selftests/mm/run_vmtests.sh b/tools/testing/selftests/mm/run_vmtests.sh > index 3e2bc818d566..9f53f7318a38 100755 > --- a/tools/testing/selftests/mm/run_vmtests.sh > +++ b/tools/testing/selftests/mm/run_vmtests.sh > @@ -221,6 +221,10 @@ CATEGORY="hugetlb" run_test ./hugepage-mremap > CATEGORY="hugetlb" run_test ./hugepage-vmemmap > CATEGORY="hugetlb" run_test ./hugetlb-madvise > > +# For this test, we need one and just one huge page > +echo 1 > /proc/sys/vm/nr_hugepages I've noticed that this change breaks some of the uffd-stress tests further down the file, because you have freed previously reserved hugepages that the test requires to run. I notice that the patch is already in mm-stable, so perhaps its possible to submit a patch that does a save and restore? Although I'm not sure if that might be tricky because the previous reservation is per-size and per-node (our CI does this on the kernel command line), and I suspect if you want just 1 huge page in the entire system you won't be able to get back to the previous state by just restoring this value? These are the failing tests for reference: # ------------------------------------ # running ./uffd-stress hugetlb 128 32 # ------------------------------------ # nr_pages: 64, nr_pages_per_cpu: 8 # ERROR: context init failed (errno=12, @uffd-stress.c:254) # [FAIL] # -------------------------------------------- # running ./uffd-stress hugetlb-private 128 32 # -------------------------------------------- # nr_pages: 64, nr_pages_per_cpu: 8 # ERROR: context init failed (errno=12, @uffd-stress.c:254) # [FAIL] Thanks, Ryan > +CATEGORY="hugetlb" run_test ./hugetlb_fault_after_madv > + > if test_selected "hugetlb"; then > echo "NOTE: These hugetlb tests provide minimal coverage. Use" > echo " https://github.com/libhugetlbfs/libhugetlbfs.git for"