From patchwork Thu Oct 5 16:39:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Breno Leitao X-Patchwork-Id: 13410435 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 4315AE71D4F for ; Thu, 5 Oct 2023 16:39:39 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C942C6B02AD; Thu, 5 Oct 2023 12:39:38 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C1B716B02AE; Thu, 5 Oct 2023 12:39:38 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id ABD766B02AF; Thu, 5 Oct 2023 12:39:38 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 9AD816B02AD for ; Thu, 5 Oct 2023 12:39:38 -0400 (EDT) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 7D63DB40A2 for ; Thu, 5 Oct 2023 16:39:38 +0000 (UTC) X-FDA: 81311968836.02.91EC3E5 Received: from mail-ej1-f53.google.com (mail-ej1-f53.google.com [209.85.218.53]) by imf06.hostedemail.com (Postfix) with ESMTP id 80DD6180018 for ; Thu, 5 Oct 2023 16:39:36 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=none; spf=pass (imf06.hostedemail.com: domain of breno.debian@gmail.com designates 209.85.218.53 as permitted sender) smtp.mailfrom=breno.debian@gmail.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1696523976; 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=aeCkyhaoVtwv2wfDF/uieHTmRe6erji0RQ2H7V1G/pc=; b=OGqAW9ZZCWijtzyDB88IDo5bwFJfNu9eR+g5CYPRU+zQp6DIikdQ6tdODJSScAiiVCM0tS Pk3nTMhXxg+uV3HNYlpBSfla3Dxc/PkcgLCGDVzWAML5gxA/nL1ONdgBEruakezJUY2GCx Q6BkuCHc7skBUanFLHgHAQytLmblzk8= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1696523976; a=rsa-sha256; cv=none; b=egcZ9qUdarQzGkiabR8sxRHjqFXckEp99jiMAHZvFY29Rsel/SZO8C9KKVt0XOO/jg1Qlf 837PZEYl3NZzw/etLKZX/BWTcTJ5uYExlVh0mUZIACPhdXBNzSUSkkG3vmb4Bzbj833Vap /Na0MpRtRtOOaynVfvik9raqiVMTefE= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=none; spf=pass (imf06.hostedemail.com: domain of breno.debian@gmail.com designates 209.85.218.53 as permitted sender) smtp.mailfrom=breno.debian@gmail.com; dmarc=none Received: by mail-ej1-f53.google.com with SMTP id a640c23a62f3a-9b29186e20aso209563466b.2 for ; Thu, 05 Oct 2023 09:39:36 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696523975; x=1697128775; 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=aeCkyhaoVtwv2wfDF/uieHTmRe6erji0RQ2H7V1G/pc=; b=b9jkF/14OaCyUNub8zEpNoaKbyqHY1Wbv+G8SbLV85Xdjj9mML9wrslFZRbucUpyeZ N8GrkTGnWJ73nPYdJ5PWtdo+fFzcgapGSaF36WDbrV/fF+JZlwWoDScg5i6AQoJgHFhq hKsQ1lUixnKOr10gBh2GsfHsJDC1O3fv0TzSfDyW+fCdAx+tjdoBdkW4dbu0L/0GCwNm 340OHFO80wDOYUnW2EXjPuW6ukCKMI7p1Y1GxieW52VSLO+uqwmjS25PiBAQsIyvoA3W aUkbMB7N3aHRWdnXCFvJhu1Xqm4wt4HbFXFPX91bwVca7HZ6E5ia3O5690twdgfG54g6 +kTg== X-Gm-Message-State: AOJu0YxeE6RJBjVaO77W4OH8TBL9v6mQKExmXZiX0Ti/v6lPqSqVjrju 7LuK6oSwK9D9jHxyEocU9Ic= X-Google-Smtp-Source: AGHT+IFXpAGpAAJi5gJLJd/ylndngGVZYM9m0IP6Ma0EXz8nnBppGgbm8KmKKvk2XKyTTj6Y5Uz/ww== X-Received: by 2002:a17:906:cc2:b0:9ae:5db5:149 with SMTP id l2-20020a1709060cc200b009ae5db50149mr5073326ejh.35.1696523974869; Thu, 05 Oct 2023 09:39:34 -0700 (PDT) Received: from localhost (fwdproxy-cln-021.fbsv.net. [2a03:2880:31ff:15::face:b00c]) by smtp.gmail.com with ESMTPSA id w13-20020a170906480d00b0099b76c3041csm1447289ejq.7.2023.10.05.09.39.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Oct 2023 09:39:34 -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 v2 2/2] selftests/mm: Add a new test for madv and hugetlb Date: Thu, 5 Oct 2023 09:39:22 -0700 Message-Id: <20231005163922.87568-3-leitao@debian.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231005163922.87568-1-leitao@debian.org> References: <20231005163922.87568-1-leitao@debian.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: 80DD6180018 X-Rspam-User: X-Rspamd-Server: rspam11 X-St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SAhSGPQQvGlRH8BND2WtW7av4inwAZ1Vf3l3ZRbHjqr6G7f2Kytn3+Om+TSPdxL/6ZAuzXywmGjHaAawbxQGVN+osVC+0WK3AXWZyGrp4pn4RCNqqzSrM2ymS4nTkLzKD1uXud/uxSW/ARmOTZ8F5O5HKnAdul/v0DbflZKjstUOgpmeUPJa2q62wRA74qStZmC/TTQf0YPOHrE1jGfMACVQK89htjit2An6823TwemIUWRw70pIHNE4Pj0Bh6kE4H+KYI+m91wIeTrZXkdr6zUx9aJF2MByexRgqOgckS2b9xzy8rBq/96ncUvkQvOGiwFWoAiMALKlnfZVkjPAnya6I5MyqrYMe+x2ZNCDNqc+Xtsa20peKeMgCXsCWv7ymuuJod72KYTwvrIv4xO8GkA6XEBLc9mzKur20XVRGvF8J0UyLFnbT+c2aoCxfP7Iax/6oCmjynWaZyHXs2DWBIre9nhv4QE+h7g2NBhyT9BRJu75+YvmbA7UmzhbuIRRRyxuzL7csshpeo92yMjvGMkmLQGBUYLI+V2fEYo88lp0lqDZ0IVvgj1+VRH6eMSStA== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000003, 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 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 +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"