From patchwork Wed Oct 4 17:11:26 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Breno Leitao X-Patchwork-Id: 13409091 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 794CFE7C4E7 for ; Wed, 4 Oct 2023 17:12:16 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E4F986B02B3; Wed, 4 Oct 2023 13:12:15 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id DD8F96B02B4; Wed, 4 Oct 2023 13:12:15 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C2C226B02B5; Wed, 4 Oct 2023 13:12:15 -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 AC6196B02B3 for ; Wed, 4 Oct 2023 13:12:15 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 6F7FE40365 for ; Wed, 4 Oct 2023 17:12:15 +0000 (UTC) X-FDA: 81308422230.12.BDA722A Received: from mail-ed1-f51.google.com (mail-ed1-f51.google.com [209.85.208.51]) by imf30.hostedemail.com (Postfix) with ESMTP id 8AD1E80013 for ; Wed, 4 Oct 2023 17:12:13 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf30.hostedemail.com: domain of breno.debian@gmail.com designates 209.85.208.51 as permitted sender) smtp.mailfrom=breno.debian@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1696439533; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=v8ih6B2Rlcxp81srnkWek1jGwJ1l7RM9hd1ykDQtxDA=; b=uSly4E7ROXidqKOyIp+I4d/m5wROdzScEnWL+iiuaCFyyWdlAh8h87wCC4M/P7fRPeHZHY byEgppzNBVtm8WX2dEQriZpYOxeM0M+78n+JdwxnzyixW7lFxNyjsylcvypPjyEvXCDPpM XnpKfngkxNzZfOp5eF1ocgRQVSRT09o= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf30.hostedemail.com: domain of breno.debian@gmail.com designates 209.85.208.51 as permitted sender) smtp.mailfrom=breno.debian@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1696439533; a=rsa-sha256; cv=none; b=3GqIHq1XJnqnkjWZCWkkgdppnUsULoL3x1S/k7+x2nR4aLElZuLk4Ll+m+XOL8HLeA97q+ fL0cS1cXiUFNEdKVCuI5lsJL0SP+BMPz7zczWT/NBJ0misbjIiQjM9Uqq9nRa0IhXz/2pN WaC82KP0um5qZ7IcOgsKYnSmtZ3OAl0= Received: by mail-ed1-f51.google.com with SMTP id 4fb4d7f45d1cf-533d31a8523so4539449a12.1 for ; Wed, 04 Oct 2023 10:12:13 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696439532; x=1697044332; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=v8ih6B2Rlcxp81srnkWek1jGwJ1l7RM9hd1ykDQtxDA=; b=b0P3e/G4ABP4rYoTZGzocYU9ZJ7BMtqFmrKJMXbLIj6mtlrKvuDZehsoryguWQMu6N 7suRL6JKmJ79mEuSqoQcK3YY4/8GarYZyuHFV+09jNzceAgpRA34ryYfVnNt9D1WWzmV Fu6RZpaIwbctJynvx5jz9/skmUtdP+2ubdtgYR9YlcMerLSRLDMnW+1rA1tIo733DOhs SRSjkog22MX8CnFRGaitJL+Ov7DXNDIbHvE4ulDoX3go14VPijhZKX4W1S2GWtZrTpXe RiMMZEoPka/juq3orF1/Pptj75ItKpb/p3cEB5OKhP+tf5EDqXJmV48NvGvkby1cdqQM WwRw== X-Gm-Message-State: AOJu0YzKxv6byR4/HMQ1cnXqmDw2SFLNg1U8pxTg8w99cxjDQI6By72K uZ0GBkiqsVam85vZQ6mByLg= X-Google-Smtp-Source: AGHT+IGBToqvNL/hEcJArvik0DADNV1zdswIJEVmop94ejSDsVbBAfm79zjk87sbIoIzygWd/MmBMQ== X-Received: by 2002:a17:906:309a:b0:9b7:1e26:e2de with SMTP id 26-20020a170906309a00b009b71e26e2demr2388918ejv.21.1696439532120; Wed, 04 Oct 2023 10:12:12 -0700 (PDT) Received: from localhost (fwdproxy-cln-025.fbsv.net. [2a03:2880:31ff:19::face:b00c]) by smtp.gmail.com with ESMTPSA id g10-20020a170906348a00b0099bd0b5a2bcsm3108543ejb.101.2023.10.04.10.12.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Oct 2023 10:12:11 -0700 (PDT) From: Breno Leitao To: mike.kravetz@oracle.com, muchun.song@linux.dev, akpm@linux-foundation.org, Shuah Khan Cc: linux-mm@kvack.org, riel@surriel.com, linux-kernel@vger.kernel.org (open list), linux-kselftest@vger.kernel.org (open list:KERNEL SELFTEST FRAMEWORK) Subject: [PATCH 2/2] selftests/mm: Add a new test for madv and hugetlb Date: Wed, 4 Oct 2023 10:11:26 -0700 Message-Id: <20231004171127.106056-2-leitao@debian.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231004171127.106056-1-leitao@debian.org> References: <20231004171127.106056-1-leitao@debian.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: 8AD1E80013 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: 599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9Oek/Z2M/BtQUIhnRdWSX3IdyVQmNATi2L5iqAZiOZkoIpdS93xBXqzGVA3zXLSvUvnkrfTZe3CUzX/HemyNELFomhA7SkYxhbj5LQowsIcn/m5KZw+ufsFZQ7Ezc5Ew1Hn16VjV9okEnVl3xnePn6J3rJuF90q207nEYWsjuoGisGT2r0jJfZOTZaDpFkDEk/RW5W2QzRyWHKam74m2tNF5NZ7EJ1LgSkAVm5zmq6OPkLHqcwaM8cJvzqZeYRIyOKBHI5BOt2Oa1QnlQPxu6nWfxYcO+W0T/nCZKkBng8B0vC/ivi1ZVxDi+8iAxbpEHpAD+vSfqbrEwfiLCMhKWUVY25I/5GP8DQ58hd39sagmBXsOAnMP/IzxZ6cS5quhi+sJ3V6NZnckur+Zpg0IIfoVvMCa5N99V09lIpTWIzdYKDaFtdfK0/Mle/WATlBTDd9wvi3pUTPbzBwGyLMtThY8/eqj9Kl6Vx+IDdOdeKVOvbiC7qaig9wa6KXDScqSbnc99OUWC1ezV40nAx80SQnAWMRQVa4wT7Oiu9s6U9yFxjgF6D2KQ== 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: Create a selftest that exercises the conflict 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 | 82 +++++++++++++++++++ tools/testing/selftests/mm/run_vmtests.sh | 4 + 3 files changed, 87 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..d6d38d443840 --- /dev/null +++ b/tools/testing/selftests/mm/hugetlb_fault_after_madv.c @@ -0,0 +1,82 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include +#include +#include +#include +#include + +#include "vm_util.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; + + if (!ptr) { + fprintf(stderr, "Failed to allocate memory\n"); + perror(""); + } + + for (int i = 0; i < INLOOP_ITER; i++) + ptr[0] = '.'; + + return NULL; +} + +void *madv(void *unused) +{ + usleep(rand() % 10); + if (!huge_ptr) + return NULL; + + 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) { + fprintf(stderr, + "This test needs one and only one page to execute. Got %lu\n", + free_hugepages); + exit(1); + } + + 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) { + perror("Failed to allocate\n"); + continue; + } + + 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 0; +} 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 +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"