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 61566C4332F for ; Thu, 2 Nov 2023 12:30:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D75BB8D008A; Thu, 2 Nov 2023 08:29:59 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D25F08D000F; Thu, 2 Nov 2023 08:29:59 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BECF38D008A; Thu, 2 Nov 2023 08:29:59 -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 B01208D000F for ; Thu, 2 Nov 2023 08:29:59 -0400 (EDT) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 8468A40CF3 for ; Thu, 2 Nov 2023 12:29:59 +0000 (UTC) X-FDA: 81412946118.04.5A34295 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf09.hostedemail.com (Postfix) with ESMTP id A52D314002D for ; Thu, 2 Nov 2023 12:29:57 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=none; spf=pass (imf09.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=1698928197; a=rsa-sha256; cv=none; b=qRxH0SIayP3JNq4eVp10brzaKk8PbuPitbW7etAKeitW74mNScGXOQ9k/lEoUMSgD5BjnL d6rrP8J2xf0VfxXwGG6lxBVV8as26p/mqPY915T917roSlWDHeR7dakdA4WOSaS39cX7r5 QjsNmHXHAoMlfYXIRjWAasIlsfnx53M= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=none; spf=pass (imf09.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=1698928197; 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=3qyukudEUCfhf4cpL1mYyZJzbHxLuhIOkWrVIJoS1iQ=; b=k0+h9ABRqngMzSVl5FWeoKhp8aSIVHPtrKN79WLNagOxJp9WunWOLvqK8GlKk2j6ZHdRjc J+jwShxvfMu4SByT2yii2RPVZgHV2pX4mplZ7s/8nCx4BtezhJum8y/s+KLgF7bOxx+P+z 02Hkc4xFE05ePCADlF0S3HUWUf5yLOo= 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 1D909C15; Thu, 2 Nov 2023 05:30:39 -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 B73F63F67D; Thu, 2 Nov 2023 05:29:55 -0700 (PDT) Message-ID: <84df3b17-6f3d-4e9b-94e0-88ba186207e2@arm.com> Date: Thu, 2 Nov 2023 12:29:54 +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 From: Ryan Roberts 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> <662df57e-47f1-4c15-9b84-f2f2d587fc5c@arm.com> In-Reply-To: <662df57e-47f1-4c15-9b84-f2f2d587fc5c@arm.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: A52D314002D X-Stat-Signature: y3m7os1yhn3waqjcib8qd5p1r1ytzikb X-HE-Tag: 1698928197-76966 X-HE-Meta: U2FsdGVkX19yZE7L0h5cBT9xtHLkawrjHFhMm3G2/64NXAUfcQl/+IcvYuJvMBeNJ6YxcQhRrmhIC31VlrXA571Zg6cxUzQ38GW4sfVr/lwITxqDski7+eMOaJ45z2fkWIdnDrSUavxvgaSjpUWlktmHECxXlFCmtd2U4uq3lUIwvqeHFaNfXAropbaUsQop/rKI7nSnBKFcmG1Bx0tG5I5fXoDNfi03GKeu8MSUTqqdW110KPd5HlcNPEJThh/0Ca0LDBD66GOMKeBd+pHFBFt9bakOMAowm69C9JrO+4q4BhuUg29GF9sf4GtKnGQPBC63hBEVtYi6s2ZEdppD6b9mmOAUr/Z1MBuW/PwQuyqhufYkzgmeDqI/p+jPHkKtuEXlI+yNbtUUmwyK6zAJBJTUErmCU3L2DwFbUHUr9FKJw94OJePHEakiY51toCyvTNoW7tN8WpXhF+kCRIw3kb48fH6OhyA5n79vi0R68I5lfQ4Ss+jU7zB+Pf6fu2DcOw8z1HIrwx9XaRVWrNd9PDkGRuyywMq0ekX1ywv1IgiSW+Bv+hLtWVGkD3JfPC8MQZKuAyYat7KFJTXAzDD58XmDJAKO5L4Cd+XUvx1NFa4XQZ4iBj8jjOq3Otn74pDmNVYoR6Z3bk/IHNQYUut908apfto5kkK63DS0NkcK7VBTGQ1uBGqHp8lHVpiK6i4plpceDuGPLgSWUG1NIBjMwYeiKrMC38jGzpB2iWDBYN9h0OKtCWMNQpWxxUehOuECOn7GdJA9EfdaP629FEXCWtf8mxLwl1rMHkZ2JC9qz+P6pay9qeul/FI4sLGx7DLsueBTT09EBQViSZElJLz6JnLT0zIk6eTm1DsI+Ev3iGuD3rUvKQSJSU6tjLvXDGpx8GkbaYmJCzuBdoiGxiclzLFp9jgvlAnMRJ4MKImhxys8MVWd3YWSBcPiZ3Xek9DPzd3QD3MrVNwJVqI3bIy C05HeAXc VSBD0nA06gI4g7FKzlb4q8sSuCGYxJ8RPb+8EuGzi607C+WcoiTndcNQ3WhRycKvvXjkOaNuArrSlWH70nJw9kgq30JigGsgNIcXZy6rQKrUNAa1yOWaJPYqwnvIfhQoejWe14nXVyGRu4zGgNwqniOSv7lxo7el5/lNHKo8nQlyynWOlqnLh9nmIY4eby6DrRzluJ2Ro7JEylwK557w3jTVNng/cz6o755IG1hnwKzf/3GAeO7ROPsVwxlpm9AXHhQgJw4THC5GkkNrfz8D7fXs3o1pn1pf/WVQsYsLlvLqeeugBbAKqZToqM2zGS0ahpKKnOCY56rEqAzMya4x9BN3Bz1ZZdIK+b9/Y 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: On 02/11/2023 12:24, Ryan Roberts wrote: > 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? Actually on closer inspection, I don't think this will be a problem; simply saving and restoring the value around the test will be sufficient. I also notice that the binary for the new test is not added to the .gitignore, which is a minor annoyance. Thanks, Ryan > > 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" >