From patchwork Fri Jan 5 15:54:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Breno Leitao X-Patchwork-Id: 13512325 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 E1E12C47079 for ; Fri, 5 Jan 2024 15:54:36 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7DC056B02F3; Fri, 5 Jan 2024 10:54:36 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 78D256B02F4; Fri, 5 Jan 2024 10:54:36 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 67C696B02F6; Fri, 5 Jan 2024 10:54:36 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 56A336B02F3 for ; Fri, 5 Jan 2024 10:54:36 -0500 (EST) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 21F5F1604DE for ; Fri, 5 Jan 2024 15:54:36 +0000 (UTC) X-FDA: 81645704952.21.D3BCA4C Received: from mail-ed1-f43.google.com (mail-ed1-f43.google.com [209.85.208.43]) by imf27.hostedemail.com (Postfix) with ESMTP id 46FEF40004 for ; Fri, 5 Jan 2024 15:54:34 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf27.hostedemail.com: domain of breno.debian@gmail.com designates 209.85.208.43 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=1704470074; 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=JbaSpzmTDb6aKo/45GWljzrU5ASqXEKi0BCO7IaChNk=; b=c1TfW7Ct6l/4LvglosPyHR4t8NgDqfHIyCcXuXK1xOYdSPYYl13iNQ+9fBgfwP0zv7yIkQ 3C5Sw/GTRPweD8BfEFc27l3OyeUUjsH099MafjE8L+KWxh7j+xzH5/i2CoMyPilcij0Yu8 NSh8OrHcu4YHT7xkFJLyjlNnT0Q2+Uc= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf27.hostedemail.com: domain of breno.debian@gmail.com designates 209.85.208.43 as permitted sender) smtp.mailfrom=breno.debian@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1704470074; a=rsa-sha256; cv=none; b=iW3mT3b+iSbADpZ5+zJNiOg6ORRsd6SuqCt8e/J/rVwE/Q/jwcgN8IoiCk3X4CkfQ9B3zL Oo6SeJNOghegK1pJToiAfy6Jy4hnord90tl/kP68AIrhw23XGMpgezQzlE60sqG4uNtXpP SvodFziOA5pPXqD2hrmnxVXLWoOHvzw= Received: by mail-ed1-f43.google.com with SMTP id 4fb4d7f45d1cf-5574797e741so144733a12.3 for ; Fri, 05 Jan 2024 07:54:33 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704470073; x=1705074873; 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=JbaSpzmTDb6aKo/45GWljzrU5ASqXEKi0BCO7IaChNk=; b=JJroi+wtxPGkLqxRlS2FxDpgQePzMQRtaaBd5RytNX33aEXh6+5VcA/eX2NHmxKKZm WXkfsQW6DxWZ3GUHC82aAuCDd+2ju9R6eHXPlzpYaOyFzZ2Gk76sIw+IPUlKk3ro8VV6 7FolbOaVYndfmKpZ+Ev3kEMc04g9zPb81AVoc8Glp5dyqKyI4N5e1P0UsXK4FLtphrLU 0UMjUEaJ6VTzT753+/zSAUlTtoyZkb8IDkqlnWP2e8hnU+vogxOaMWdcg62T/yEpk10A 14+yh78Qu+8j7PjLMUKRqLgQLMN1PV+FExdx3DLSysBuToDopx4wP91uqgJv6eiEnhA8 n76w== X-Gm-Message-State: AOJu0YyMfqDdmSEPAjjJjTD0Dpa8TGqlFbLU+OKm3DBNb4EPPuDBgN4U eEizGsJeX1ESisnraXf7W64= X-Google-Smtp-Source: AGHT+IE+OteFp3sRNlrDPPup4dAsUX7gWH5CtQwy32Ik6ZXyC6MXvdMZUBr6Hmu7WFNnO4U3tU8cNg== X-Received: by 2002:a50:8a96:0:b0:557:aff:2de8 with SMTP id j22-20020a508a96000000b005570aff2de8mr1213773edj.19.1704470072702; Fri, 05 Jan 2024 07:54:32 -0800 (PST) Received: from localhost (fwdproxy-cln-007.fbsv.net. [2a03:2880:31ff:7::face:b00c]) by smtp.gmail.com with ESMTPSA id fd5-20020a056402388500b0055743d6e9ebsm227571edb.41.2024.01.05.07.54.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 05 Jan 2024 07:54:32 -0800 (PST) From: Breno Leitao To: riel@surriel.com, linux-mm@kvack.org, akpm@linux-foundation.org, Shuah Khan Cc: vegard.nossum@oracle.com, rppt@kernel.org, songmuchun@bytedance.com, shy828301@gmail.com, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 1/1] selftests/mm: new test that steals pages Date: Fri, 5 Jan 2024 07:54:19 -0800 Message-Id: <20240105155419.1939484-2-leitao@debian.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240105155419.1939484-1-leitao@debian.org> References: <20240105155419.1939484-1-leitao@debian.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: 46FEF40004 X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: 3fmtf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SgEvNESwcxYBJzbhD33abDI7kqb9GoN2TLY1bLx/o7zcO1XHqZj8Q5+yMIxkLEoIvCehlh3Mpplsc0JyjksTnJ/6hWokpMKK6aCdCas4vX/DeYnmpj1rbfTf4Q/nmk7GJzP9M1DP07FWo/PM3jC41piwt990GWOiKIFqdUA7T1i+NMviX+GkqLHRU7l37/92FA9kHMCgvdmd23QvyWZwx3hs7rQtf0sdcDzUREAObPeZnWkOmNtQuVtmxc3PzscjWGuKaY4wy9Tg7GHQl5ZZAOgo4NZWsxolN4R3mUcWzvw+4AF/UAxwRkr05yPz612xnUQf+W7kHUmI5Wa4RIVddBUvBnSXNtQ3slAqw4TmlZMFeKYBUg7wIuO58JIIduqsTLfpKrHvhNeilQcb0zCXZNy+t9I2mGBeR0FpGtN8mVKQ= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000011, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: This test stresses the race between of madvise(DONTNEED), a page fault and a parallel huge page mmap, which should fail due to lack of available page available for mapping. This test case must run on a system with one and only one huge page available. # echo 1 > /sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages During setup, the test allocates the only available page, and starts three threads: - thread 1: * madvise(MADV_DONTNEED) on the allocated huge page - thread 2: * Write to the allocated huge page - thread 3: * Tries to allocated (steal) an extra huge page (which is not available) thread 3 should never succeed in the allocation, since the only huge page was never unmapped, and should be reserved. Touching the old page after thread3 allocation will raise a SIGBUS. Signed-off-by: Breno Leitao --- tools/testing/selftests/mm/.gitignore | 1 + tools/testing/selftests/mm/Makefile | 1 + .../selftests/mm/hugetlb_madv_vs_map.c | 124 ++++++++++++++++++ 3 files changed, 126 insertions(+) create mode 100644 tools/testing/selftests/mm/hugetlb_madv_vs_map.c diff --git a/tools/testing/selftests/mm/.gitignore b/tools/testing/selftests/mm/.gitignore index 4ff10ea61461..d26e962f2ac4 100644 --- a/tools/testing/selftests/mm/.gitignore +++ b/tools/testing/selftests/mm/.gitignore @@ -46,3 +46,4 @@ gup_longterm mkdirty va_high_addr_switch hugetlb_fault_after_madv +hugetlb_madv_vs_map diff --git a/tools/testing/selftests/mm/Makefile b/tools/testing/selftests/mm/Makefile index dede0bcf97a3..f6e42a773e1e 100644 --- a/tools/testing/selftests/mm/Makefile +++ b/tools/testing/selftests/mm/Makefile @@ -70,6 +70,7 @@ TEST_GEN_FILES += ksm_tests TEST_GEN_FILES += ksm_functional_tests TEST_GEN_FILES += mdwe_test TEST_GEN_FILES += hugetlb_fault_after_madv +TEST_GEN_FILES += hugetlb_madv_vs_map ifneq ($(ARCH),arm64) TEST_GEN_FILES += soft-dirty diff --git a/tools/testing/selftests/mm/hugetlb_madv_vs_map.c b/tools/testing/selftests/mm/hugetlb_madv_vs_map.c new file mode 100644 index 000000000000..d01e8d4901d0 --- /dev/null +++ b/tools/testing/selftests/mm/hugetlb_madv_vs_map.c @@ -0,0 +1,124 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * A test case that must run on a system with one and only one huge page available. + * # echo 1 > /sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages + * + * During setup, the test allocates the only available page, and starts three threads: + * - thread1: + * * madvise(MADV_DONTNEED) on the allocated huge page + * - thread 2: + * * Write to the allocated huge page + * - thread 3: + * * Try to allocated an extra huge page (which must not available) + * + * The test fails if thread3 is able to allocate a page. + * + * Touching the first page after thread3's allocation will raise a SIGBUS + * + * Author: Breno Leitao + */ +#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) +{ + for (int i = 0; i < INLOOP_ITER; i++) + huge_ptr[0] = '.'; + + return NULL; +} + +void *madv(void *unused) +{ + for (int i = 0; i < INLOOP_ITER; i++) + madvise(huge_ptr, MMAP_SIZE, MADV_DONTNEED); + + return NULL; +} + +/* + * We got here, and there must be no huge page available for mapping + * The other hugepage should be flipping from used <-> reserved, because + * of madvise(DONTNEED). + */ +void *map_extra(void *unused) +{ + void *ptr; + + for (int i = 0; i < INLOOP_ITER; i++) { + ptr = mmap(NULL, MMAP_SIZE, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB, + -1, 0); + + if ((long)ptr != -1) { + /* Touching the other page now will cause a SIGBUG + * huge_ptr[0] = '1'; + */ + return ptr; + } + } + + return NULL; +} + +int main(void) +{ + pthread_t thread1, thread2, thread3; + unsigned long free_hugepages; + void *ret; + + /* + * On kernel 6.7, we are able to reproduce the problem with ~10 + * interactions + */ + int max = 10; + + 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"); + return KSFT_SKIP; + } + + pthread_create(&thread1, NULL, madv, NULL); + pthread_create(&thread2, NULL, touch, NULL); + pthread_create(&thread3, NULL, map_extra, NULL); + + pthread_join(thread1, NULL); + pthread_join(thread2, NULL); + pthread_join(thread3, &ret); + + if (ret) { + ksft_test_result_fail("Unexpected huge page allocation\n"); + return KSFT_FAIL; + } + + /* Unmap and restart */ + munmap(huge_ptr, MMAP_SIZE); + } + + return KSFT_PASS; +}