From patchwork Sun Jan 19 18:06:07 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiaqi Yan X-Patchwork-Id: 13944558 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 4985FC0218A for ; Sun, 19 Jan 2025 18:06:21 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B856E6B0085; Sun, 19 Jan 2025 13:06:20 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B2879280001; Sun, 19 Jan 2025 13:06:20 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 92EC36B0088; Sun, 19 Jan 2025 13:06:20 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 76F386B0083 for ; Sun, 19 Jan 2025 13:06:20 -0500 (EST) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id F2686B23B2 for ; Sun, 19 Jan 2025 18:06:19 +0000 (UTC) X-FDA: 83024980878.06.ECD74D3 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) by imf20.hostedemail.com (Postfix) with ESMTP id 2B1231C000D for ; Sun, 19 Jan 2025 18:06:17 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=q3ND9dDf; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf20.hostedemail.com: domain of 3GD-NZwgKCA8yxp5xDp2v33v0t.r310x29C-11zAprz.36v@flex--jiaqiyan.bounces.google.com designates 209.85.216.74 as permitted sender) smtp.mailfrom=3GD-NZwgKCA8yxp5xDp2v33v0t.r310x29C-11zAprz.36v@flex--jiaqiyan.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1737309978; a=rsa-sha256; cv=none; b=fXoza33QGJcIWQB9QUlj2qmvs1F8RwC+SR4pW97Z4KBUylyOEiIQw3g0CuJrXopTmuVR4X Vli5LBbb8gwPbki9pKbrUXX0oq0mk0j5Z8j/CPaaeYnTvhUmCnoo0lBpMfLFq2uFT4C6Sa VGLqNjkyO8cGq/YBjDbVxT9OK0L0//M= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=q3ND9dDf; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf20.hostedemail.com: domain of 3GD-NZwgKCA8yxp5xDp2v33v0t.r310x29C-11zAprz.36v@flex--jiaqiyan.bounces.google.com designates 209.85.216.74 as permitted sender) smtp.mailfrom=3GD-NZwgKCA8yxp5xDp2v33v0t.r310x29C-11zAprz.36v@flex--jiaqiyan.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1737309978; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=NKJABtBkaO86V03jjBLlHT4N0854yHcZTt6nQ8d1Z1A=; b=PPU2KImy2hamWonfN4nqyxmDRqL9Yb7vTvSaORS8m+FjgAliX75h1yRHUV4oocg8VZXoHF PJn7DPdE+vTBdYkM/4CnNEvUKvtlPjxfneVrEvV8vlNXbWHS5wQi2L85ao8gQ1IpLogw5y JWOS062ATLvlDAbLujSP3DiDfBcJHSc= Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-2efa0eb9cfeso7140064a91.0 for ; Sun, 19 Jan 2025 10:06:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1737309977; x=1737914777; darn=kvack.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=NKJABtBkaO86V03jjBLlHT4N0854yHcZTt6nQ8d1Z1A=; b=q3ND9dDfcZSoJ6CvB/JSD4FB2hlawGzWxsWAcKbcqkqGXIhDTlrKJWzK8BmoMSMSnV ITJYcZlbM4DBO0v5hzCoqZOorRtoNBxvYOcIjSrDv7UuNJvnhJUfdz9O5CN4o7fav7Vy PsNDFDyQJ0kgAfXu/g149g6hEVBcFjYk8BcVfD+PUOo7hQkxmrLPAo5P8KvTZadTRRlp mmkGsxHk8W2lEfSVDIx6qgNVZJ+ljs6z1b6w6cIC+s92qVPTI4QhhHjGOhIXlayZi9Cv fmcV5R1OSj6je4LEFprE7jHaAQ82cT8EZ7yGflwhHrMLzFsq/HcBgLm3h51rwjRDpBRt jh0w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737309977; x=1737914777; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=NKJABtBkaO86V03jjBLlHT4N0854yHcZTt6nQ8d1Z1A=; b=arHnCayK73WL0lJIKWxWNfETFM5YjvaLGKWG7MMcxSMPeSMjDuGtqG2bs44NADZcFR boY2O9b0Zl7xCCwpK7YBi5u/WCfe6fylaZHcKwII4kCr/IBzGidbAwBvQytVCdPxqK4/ WP/Oe8aJATqqjpEGmfdnQBUihqh43C0W+ThimmP1hnj+aY6IwkgYMc0vh76txuGTenB0 NjRUS1MYm2cNWMCElaBsCVPkZgwVBjhzvoEMvRHFl0g3DC4cvhbkm+6wbfdrV6CVpmx+ Zow1TS1QNbGaOS35pyaRkxnOdGeVCgdJ9Bl7f2fpbK0WO6qWCUchrwJ6dSc5H+xZHjvp vu7Q== X-Forwarded-Encrypted: i=1; AJvYcCXvKuEPQUprrzoRaYvy/c9s05fvx4h6ZMb3y4c5azhMUyOu5wmD1pFwZgSWJfWcFAKefYVK285BFw==@kvack.org X-Gm-Message-State: AOJu0Yy/eTjdJthPryO0m3p8Ehs5OhCTOZorjlppoFBynsULoMnGN8B2 O0zqndxtfINPm/wnd7ybeK2Bgdymt1QNwvw8k143oA9XrjaDddSWR+jSlQ8ritPBxCHc9ZywRFG lATPkL6J2ig== X-Google-Smtp-Source: AGHT+IE0EsAHOx0uRDMT/3GiFXAeoNZBpraudT+237mAXqRVcZkX5PO08oN7uYZXDI5uoXbE/QjkSMRHWMVvoA== X-Received: from pjot3.prod.google.com ([2002:a17:90a:9503:b0:2ea:9d23:79a0]) (user=jiaqiyan job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:4d01:b0:2ee:5a82:433a with SMTP id 98e67ed59e1d1-2f782d1f5ddmr14382391a91.17.1737309976904; Sun, 19 Jan 2025 10:06:16 -0800 (PST) Date: Sun, 19 Jan 2025 18:06:07 +0000 In-Reply-To: <20250119180608.2132296-1-jiaqiyan@google.com> Mime-Version: 1.0 References: <20250119180608.2132296-1-jiaqiyan@google.com> X-Mailer: git-send-email 2.48.0.rc2.279.g1de40edade-goog Message-ID: <20250119180608.2132296-2-jiaqiyan@google.com> Subject: [RFC PATCH v1 1/2] selftest/mm: test HWPoison hugetlb truncation behavior From: Jiaqi Yan To: nao.horiguchi@gmail.com, linmiaohe@huawei.com, sidhartha.kumar@oracle.com, muchun.song@linux.dev Cc: jane.chu@oracle.com, akpm@linux-foundation.org, osalvador@suse.de, rientjes@google.com, jthoughton@google.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Jiaqi Yan X-Rspam-User: X-Rspamd-Queue-Id: 2B1231C000D X-Rspamd-Server: rspam10 X-Stat-Signature: gsogu7faks5diff7epardew1awcqkcr6 X-HE-Tag: 1737309977-684810 X-HE-Meta: U2FsdGVkX18i0V5lm/ftb4+DFOj/NmfBLvmUhe+KCb7/RnSiLcadFgYPff3oCmbmkAa8Qv/aCsQV3Oipkvk5M8St4Ec29Ybync5/lzoPJyBWbXm4kHHvfx47iRusf+WEzP9fmd1u7IbGjUeQ+iLjZJ/hsVOjdvH8LsRL+eJuZ0H5a8OlhxN97lDHzzhhbMDHaz8xfwB8GgfQLydPlH4I2h/SuJddHV9hPIgRzteDCu3i5t20qMww5cT0d4hLSHwwsuDK/vjnp1AjcvFm3TGPhhXL0rUTPnSbirmBbtfwRNQprL0Y4efmnYkeYzGdTuHZqyZn3eajFXmBAnHDhf4yogK8JYg9Xxwa53HEwFNOW3Yj4hBB+xWkPHE44t8HMO2SO7Vx0969l33k8vHf1ElRPQW5o4wUY4jECl/fePzTqjfG/oG4np3nlmOMYS0OuocU9tiqXWkZqNId0AKMxKqjZfAdgzOY+caANrrXeeRMRya6WJijZ1BiaTFTN3X6ccsQPhBOXa3vJANpJvpnEqco2hMLhJMt8rUWmns615Q7TG3g8egO2cJeFoUoo1jkom5rxUv/NeZSHZjQy8zW8paoGIIllMlc9CjrUpPCgRYFowg2+yiLThSDhwvyJCz3P9d0APcKY6WIBVKNLHVcgHmn/1TvbRMMQqqsw3bCPMWdFf7kZspz+qUcGGLdRrD1wmcHSConO9qcTQJ6E0k/3zigpskw1Avo0U8jI3s5xmVNfhrSMDeAIM8gyrxXzfGg00RNjR1uqekR63X98NcnNicssp04WfVq56ox9vN29Nno5q+cxxWlCEaIBdnhYkd8AILPiTWeq+qUQaya0aTJVbQA+QJkw1Ye2VNLedoNjNHjJGSScV4a6EPMDPaB5JWPM6M/FfPBFHPSmkdlLBB1Za9FOrNDRQwZxE24DAbB5dObFTEmzH9ZyMdqupDNfeWO0VItXMUrbuWW15mNiJN+IL1 RRHEyMDl xOppQa+pJjqLbMfYNmktEaduJNB1VmQwV91O8+tNgGtD7/8MZmLuGmNmTFjS3mcqnXwUb28qY9AiFZUrLJAaYnJupj4JUuP4UJ1ZlncLdlkht9J7sbLAHxb10pCjv4wlnCAyVZW/FGuXOmlWZKY8z9WoigTZJDyVaCqIlIEYDNRVvnJfeNX/czIq2LD3/3RuWpHKRAUjXETUpKKdgiJ0z92fvlcNoFq7YWh0HbnkxCWHtk5RbTdxXavVt/Af3xWwWX1vkfCgqYBlPAbQ/oE2nIgmaP0ukK/AIUqf67r1M4eP4WL9WHg98GTeL35GPxoQAjqcPXw9H6pdCCbsihPhpefdKq98xqEGD9WSD8HNvtgxP6zb7AtanAfjF0FzK+FB5RQsB/A+PlpoyEikUUP4HNsSlQuCmVhuAa+Ui1K/mqHQnfitWBL+pLJxM8sryJL9FusLO8PgzjOIhn1jgUZv8e1beSIQiZplukVkJoXJIEx/jCO6f/2p110qR1UpWVupo6XF7EOaaHCWahjJuBBcm3MG8gzcSfpYHtZu5vxxBTL3H7LAUwSSPafRZNU3Cn2ra6GNBA2o3RhXr9hwyndnCgjyyjiIB83eQHU/yQFxwv1EUTLR+aw5XTNShHmvhiL/5Pzng 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: Test based on my understanding of the memory failure recovery behavior for HugeTLB file system, especially after file is truncated/closed. Signed-off-by: Jiaqi Yan --- tools/testing/selftests/mm/Makefile | 1 + tools/testing/selftests/mm/hugetlb-mfr-base.c | 240 ++++++++++++++++++ 2 files changed, 241 insertions(+) create mode 100644 tools/testing/selftests/mm/hugetlb-mfr-base.c diff --git a/tools/testing/selftests/mm/Makefile b/tools/testing/selftests/mm/Makefile index 63ce39d024bb5..576626c93ccab 100644 --- a/tools/testing/selftests/mm/Makefile +++ b/tools/testing/selftests/mm/Makefile @@ -62,6 +62,7 @@ TEST_GEN_FILES += hmm-tests TEST_GEN_FILES += hugetlb-madvise TEST_GEN_FILES += hugetlb-read-hwpoison TEST_GEN_FILES += hugetlb-soft-offline +TEST_GEN_FILES += hugetlb-mfr-base TEST_GEN_FILES += hugepage-mmap TEST_GEN_FILES += hugepage-mremap TEST_GEN_FILES += hugepage-shm diff --git a/tools/testing/selftests/mm/hugetlb-mfr-base.c b/tools/testing/selftests/mm/hugetlb-mfr-base.c new file mode 100644 index 0000000000000..b8eee071babe6 --- /dev/null +++ b/tools/testing/selftests/mm/hugetlb-mfr-base.c @@ -0,0 +1,240 @@ +// SPDX-License-Identifier: GPL-2.0 + +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#include "../kselftest.h" +#include "vm_util.h" + +#define EPREFIX " !!! " +#define BYTE_LENTH_IN_1G 0x40000000 +#define HUGETLB_FILL 0xab + +static void *sigbus_addr; +static int sigbus_addr_lsb; +static bool expecting_sigbus; +static bool got_sigbus; +static bool was_mceerr; + +static int create_hugetlbfs_file(struct statfs *file_stat) +{ + int fd; + int flags = MFD_HUGETLB | MFD_HUGE_1GB; + + fd = memfd_create("hugetlb_tmp", flags); + if (fd < 0) + ksft_exit_fail_perror("Failed to memfd_create"); + + memset(file_stat, 0, sizeof(*file_stat)); + if (fstatfs(fd, file_stat)) { + close(fd); + ksft_exit_fail_perror("Failed to fstatfs"); + } + if (file_stat->f_type != HUGETLBFS_MAGIC) { + close(fd); + ksft_exit_fail_msg("Not hugetlbfs file"); + } + + ksft_print_msg("Created hugetlb_tmp file\n"); + ksft_print_msg("hugepagesize=%#lx\n", file_stat->f_bsize); + if (file_stat->f_bsize != BYTE_LENTH_IN_1G) + ksft_exit_fail_msg("Hugepage size is not 1G"); + + return fd; +} + +/* + * SIGBUS handler for "do_hwpoison" thread that mapped and MADV_HWPOISON + */ +static void sigbus_handler(int signo, siginfo_t *info, void *context) +{ + if (!expecting_sigbus) + ksft_exit_fail_msg("unexpected sigbus with addr=%p", + info->si_addr); + + got_sigbus = true; + was_mceerr = (info->si_code == BUS_MCEERR_AO || + info->si_code == BUS_MCEERR_AR); + sigbus_addr = info->si_addr; + sigbus_addr_lsb = info->si_addr_lsb; +} + +static void *do_hwpoison(void *hwpoison_addr) +{ + int hwpoison_size = getpagesize(); + + ksft_print_msg("MADV_HWPOISON hwpoison_addr=%p, len=%d\n", + hwpoison_addr, hwpoison_size); + if (madvise(hwpoison_addr, hwpoison_size, MADV_HWPOISON) < 0) + ksft_exit_fail_perror("Failed to MADV_HWPOISON"); + + pthread_exit(NULL); +} + +static void test_hwpoison_multiple_pages(unsigned char *start_addr) +{ + pthread_t pthread; + int ret; + unsigned char *hwpoison_addr; + unsigned long offsets[] = {0x200000}; + + for (size_t i = 0; i < ARRAY_SIZE(offsets); ++i) { + sigbus_addr = (void *)0xBADBADBAD; + sigbus_addr_lsb = 0; + was_mceerr = false; + got_sigbus = false; + expecting_sigbus = true; + hwpoison_addr = start_addr + offsets[i]; + + ret = pthread_create(&pthread, NULL, &do_hwpoison, hwpoison_addr); + if (ret) + ksft_exit_fail_perror("Failed to create hwpoison thread"); + + ksft_print_msg("Created thread to hwpoison and access hwpoison_addr=%p\n", + hwpoison_addr); + + pthread_join(pthread, NULL); + + if (!got_sigbus) + ksft_test_result_fail("Didn't get a SIGBUS\n"); + if (!was_mceerr) + ksft_test_result_fail("Didn't get a BUS_MCEERR_A(R|O)\n"); + if (sigbus_addr != hwpoison_addr) + ksft_test_result_fail("Incorrect address: got=%p, expected=%p\n", + sigbus_addr, hwpoison_addr); + if (sigbus_addr_lsb != 30) + ksft_test_result_fail("Incorrect address LSB: got=%d, expected=%d\n", + sigbus_addr_lsb, pshift()); + + ksft_print_msg("Received expected and correct SIGBUS\n"); + } +} + +static int read_nr_hugepages(unsigned long hugepage_size, + unsigned long *nr_hugepages) +{ + char buffer[256] = {0}; + char cmd[256] = {0}; + + sprintf(cmd, "cat /sys/kernel/mm/hugepages/hugepages-%ldkB/nr_hugepages", + hugepage_size); + FILE *cmdfile = popen(cmd, "r"); + + if (cmdfile == NULL) { + ksft_perror(EPREFIX "failed to popen nr_hugepages"); + return -1; + } + + if (!fgets(buffer, sizeof(buffer), cmdfile)) { + ksft_perror(EPREFIX "failed to read nr_hugepages"); + pclose(cmdfile); + return -1; + } + + *nr_hugepages = atoll(buffer); + pclose(cmdfile); + return 0; +} + +/* + * Main thread that drives the test. + */ +static void test_main(int fd, size_t len) +{ + unsigned char *map; + struct sigaction new, old; + const unsigned long hugepagesize_kb = BYTE_LENTH_IN_1G / 1024; + unsigned long nr_hugepages_before = 0; + unsigned long nr_hugepages_after = 0; + + if (read_nr_hugepages(hugepagesize_kb, &nr_hugepages_before) != 0) { + close(fd); + ksft_exit_fail_msg("Failed to read nr_hugepages\n"); + } + ksft_print_msg("NR hugepages before MADV_HWPOISON is %ld\n", nr_hugepages_before); + + if (ftruncate(fd, len) < 0) + ksft_exit_fail_perror("Failed to ftruncate"); + + ksft_print_msg("Allocated %#lx bytes to HugeTLB file\n", len); + + map = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + if (map == MAP_FAILED) + ksft_exit_fail_msg("Failed to mmap"); + + ksft_print_msg("Created HugeTLB mapping: %p\n", map); + + memset(map, HUGETLB_FILL, len); + ksft_print_msg("Memset every byte to 0xab\n"); + + new.sa_sigaction = &sigbus_handler; + new.sa_flags = SA_SIGINFO; + if (sigaction(SIGBUS, &new, &old) < 0) + ksft_exit_fail_msg("Failed to setup SIGBUS handler"); + + ksft_print_msg("Setup SIGBUS handler successfully\n"); + + test_hwpoison_multiple_pages(map); + + if (read_nr_hugepages(hugepagesize_kb, &nr_hugepages_after) != 0) { + close(fd); + ksft_exit_fail_msg("Failed to read nr_hugepages\n"); + } + + /* + * After MADV_HWPOISON, hugepage should still be in HugeTLB pool. + */ + ksft_print_msg("NR hugepages after MADV_HWPOISON is %ld\n", nr_hugepages_after); + if (nr_hugepages_before != nr_hugepages_after) + ksft_test_result_fail("NR hugepages reduced by %ld after MADV_HWPOISON\n", + nr_hugepages_before - nr_hugepages_after); + + /* End of the lifetime of the created HugeTLB memfd. */ + if (ftruncate(fd, 0) < 0) + ksft_exit_fail_perror("Failed to ftruncate to 0"); + munmap(map, len); + close(fd); + + /* + * After freed by userspace, MADV_HWPOISON-ed hugepage should be + * dissolved into raw pages and removed from HugeTLB pool. + */ + if (read_nr_hugepages(hugepagesize_kb, &nr_hugepages_after) != 0) { + close(fd); + ksft_exit_fail_msg("Failed to read nr_hugepages\n"); + } + ksft_print_msg("NR hugepages after truncate is %ld\n", nr_hugepages_after); + if (nr_hugepages_before != nr_hugepages_after + 1) + ksft_test_result_fail("NR hugepages is not reduced after truncate memfd\n"); + + ksft_test_result_pass("All done\n"); +} + +int main(int argc, char **argv) +{ + int fd; + struct statfs file_stat; + size_t len = BYTE_LENTH_IN_1G; + + ksft_print_header(); + ksft_set_plan(1); + + fd = create_hugetlbfs_file(&file_stat); + test_main(fd, len); + + ksft_finished(); +} From patchwork Sun Jan 19 18:06:08 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiaqi Yan X-Patchwork-Id: 13944559 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 14F6BC02187 for ; Sun, 19 Jan 2025 18:06:23 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8BB236B0088; Sun, 19 Jan 2025 13:06:22 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 86A8B280002; Sun, 19 Jan 2025 13:06:22 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6E48D280001; Sun, 19 Jan 2025 13:06:22 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 4AA2F6B0088 for ; Sun, 19 Jan 2025 13:06:22 -0500 (EST) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id DFB35C2378 for ; Sun, 19 Jan 2025 18:06:21 +0000 (UTC) X-FDA: 83024980962.26.E59F907 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) by imf25.hostedemail.com (Postfix) with ESMTP id 1FB54A0016 for ; Sun, 19 Jan 2025 18:06:19 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=QGN+FPE8; spf=pass (imf25.hostedemail.com: domain of 3Gj-NZwgKCBE0zr7zFr4x55x2v.t532z4BE-331Crt1.58x@flex--jiaqiyan.bounces.google.com designates 209.85.214.201 as permitted sender) smtp.mailfrom=3Gj-NZwgKCBE0zr7zFr4x55x2v.t532z4BE-331Crt1.58x@flex--jiaqiyan.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1737309980; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=vgfFFIARtCStVkFiN3Y7mh6lovuYIVjrCOS7aUtFuQ0=; b=xmYi/HDwkEFLNzM9r/NiYn/hGVCbGpnFiAeqPyqG6yGLMqthsjdkGzh5ekKSAhKZZYv7wA Fz3coAVyqUE6R7ZGVIfAKtM3QGkLdmVPhGNzTuEYHPdueCP4vTdDZ7LI3AuGScpAu5DLe7 5g8xbXuy0z556OeBKwMUbbpo9xIeS6Y= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=QGN+FPE8; spf=pass (imf25.hostedemail.com: domain of 3Gj-NZwgKCBE0zr7zFr4x55x2v.t532z4BE-331Crt1.58x@flex--jiaqiyan.bounces.google.com designates 209.85.214.201 as permitted sender) smtp.mailfrom=3Gj-NZwgKCBE0zr7zFr4x55x2v.t532z4BE-331Crt1.58x@flex--jiaqiyan.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1737309980; a=rsa-sha256; cv=none; b=o5n9LGTNfiHWfWFX2u2Wb12GQ4J8hrh/Aw0pnMvGIRRBBfyHj2v3b6rTCLzFdRAVspeuxD nVhAGVQ/7LAHxetayaQbMdonnECkT+v1kfro7Zw/rrToD0c7WxQ9+HKcgyw+4Lluj9ijBy Xys/NW8Lro7sb9zS82MDJn8EndssFog= Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-2163a2a1ec2so117556595ad.1 for ; Sun, 19 Jan 2025 10:06:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1737309979; x=1737914779; darn=kvack.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=vgfFFIARtCStVkFiN3Y7mh6lovuYIVjrCOS7aUtFuQ0=; b=QGN+FPE8CJzbqrRZbdFPJa6snd6Qt5QwKwaQt0MZ67zakNaxWgnDRZCu95+IKtnHVa OYczOAdzNxpW/d1Gk2osmmh3i4alc+goPACuP4InAjCrefLGEHSTRP8z10qrdT6QUsh/ 66Z22wTQfV8yapFWoYCHJpXUtl+xOXGY/s5Q9ykMXkbgQ+af0ged75YhivwzfOi1HpN2 iMUlyN0ewQFqew6vFo/lYRGqZrnUfxrxw5YgLOedXoDxq//TLSjAxj0kXOnruuwfxHeZ V4WvzwoGRaJ4IVhQuEVwUirwRWQtjqksbj4+1//kMg381EyhpmzclHwQRoUlSPofripy n+xg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737309979; x=1737914779; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=vgfFFIARtCStVkFiN3Y7mh6lovuYIVjrCOS7aUtFuQ0=; b=tDnqbp2OxjJx9tgd/ZEmCa8mHyWUVXnr3hjAJsMOYBYj+Xdy8mL8bFYGPS5b2/9yGA Lzpqn7NZ/cS8LBM1yAj19NSQ8CgJc+fmxEgdmiV/YHM1xatdDEv/jxdDxe0Q7YN2rSBd r6qnjkRe2V7Q6YOiZ/VKlZ9JoTblUqqX2EgXuOEnXyOsF0XqoZB0HnDasTjKd3UP3NH9 W4/tnofPLTat2uiLe0HtoZ7+g14dScrWXzJhw5Alyt9sb3uTl5cQp731NWMBkQ2rifAE xedf41HiQT+5Rw/LgWzpUxyU8SCwejX+unRuSd0JW/dSgJ3RSsl4d/6GzZwgYDtbEFJN 2EFw== X-Forwarded-Encrypted: i=1; AJvYcCV2205Sgm18rSjyGUPy3jyCJ/KEebeIuSQwmqVeRp7+U8OW71lRqS+HkWIjlBRpCqeVOovQYXAoqA==@kvack.org X-Gm-Message-State: AOJu0YyphbzRsllIg+85co7upx1BMS8K8UadtnbcaY2g0iisxNuGxa03 mkCnmMOSxwiYvbjOrh/bFq3MWlYAttOR9nmESz55CbkEuLvR1iyYz8m5Srlpur6VAH1Pybw8EjP rhvIVMijVtQ== X-Google-Smtp-Source: AGHT+IHAS67bXCUJs2OIQ7UfIjBMR7UVyDySNgDtOcvl1WDYTlpoiA2k8t5+Ag+yzr17DwX0g6JuMamqP59O8Q== X-Received: from plge12.prod.google.com ([2002:a17:902:cf4c:b0:219:21b2:3071]) (user=jiaqiyan job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:f606:b0:216:4016:5aea with SMTP id d9443c01a7336-21c355835ecmr179457375ad.29.1737309978916; Sun, 19 Jan 2025 10:06:18 -0800 (PST) Date: Sun, 19 Jan 2025 18:06:08 +0000 In-Reply-To: <20250119180608.2132296-1-jiaqiyan@google.com> Mime-Version: 1.0 References: <20250119180608.2132296-1-jiaqiyan@google.com> X-Mailer: git-send-email 2.48.0.rc2.279.g1de40edade-goog Message-ID: <20250119180608.2132296-3-jiaqiyan@google.com> Subject: [RFC PATCH v1 2/2] mm/hugetlb: immature fix to handle HWPoisoned folio From: Jiaqi Yan To: nao.horiguchi@gmail.com, linmiaohe@huawei.com, sidhartha.kumar@oracle.com, muchun.song@linux.dev Cc: jane.chu@oracle.com, akpm@linux-foundation.org, osalvador@suse.de, rientjes@google.com, jthoughton@google.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Jiaqi Yan X-Rspamd-Queue-Id: 1FB54A0016 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: ouaht1djt4jt6kgoki6x3acbozxjaean X-HE-Tag: 1737309979-439007 X-HE-Meta: U2FsdGVkX18PGTB9opopDAZj1AFeiCwdjg+SJ11Q8NTuJmkcaEkOmZLFsjVELv5S4j/mJtBGZCRo41kcf/2dNeS6G1g9qbzvaEG+/4G8+Q2w6CWttdSD/lcz9Br3KZK19TPY8UFh452eomDM780XrW9nuCWXdzTOKk27mLbWHJLnrJybqxnyVNRGVer4Vc4TEtDUF7VVHES2aMcgVJhGJB23cjDgzkC34Q0VzfT4I/SlXBbGfBW7BNRtD7gOA2mve5i3Mu202b6qLn6OQU0YnbFaBZRpqT37uRxwincREuz63L/JVDEbhAsFp2Ko6yakC7N4uHROSyWvpaUa07ou1s2AXzn/x70VU8erP/xffb1HTa8Wa/9kK4EbFVwr8fbxKIWCWftzWOpP0IGqHQNdD2Offi8WkjCVordQohPyrjh1x4S2sFATtA1jDQZo1CsSfvOCy7rg/AsXIZdjWIFBHPKIyGgmEjW20NnNLP/QGlbe0b8BIWOLxRDkwKLxLBWK3qXk6gyLl2/tIPMyrhg9qQAaU5PxZpx+BTdv3Z/1qOWBhwEXrRrb4gssVZ9bkQbw9FM4smpTTnOrKgjKFIvG7NCiKA9/kB3Kgx5QGlzHLnkZUPUCZ1ZVxoa9GtO4pO/DjI5raiNFjcA2eUG740RPvuf7p98txs5i7I8++zkDQTuROfjcIzfEHfWCqJV1RIl0vRz+q8NwFcEVVqDDZqI9O/kn4NsvTQ980NNK+6Q45O8eb55sl72csFjUXrSTppJ+CCUX4oL677RckKL1qsRcRh5hNq4VouLN6WUuKxirBRP59NKWYa+SbfqyWYKtNXXS+7wnrkIJneRTf4MEea2cnJawKD0MarhV+DrPi5eGO1Ywh60TbQyf27c0Igxz6fYcEpK8r7H9MCDxA9pZidIXOWBtyJz+7V2D6Da6Idc7A9Cj8UWtihFnNC8K1viBsw76DdOT+LqqWGbB1P/wFkx aouDKdsX 4q30wRnDWGJCrvgSaGqFt3fGZ+/sLkSbuyc8y/seaiFRclTeUcltzB0FAxP3p4teJlz9VWhKeOY3Lkl6xEmQ4lG+yUZ/IX+E4Hug3rriKVx/LdlCYh9g+ehuvPVNIWAHlernCnIKIwkj3cBMN+P83keGyNvo2wS4bZmUprTUHpYQGX+rXpFk/8sryKV05P/x7N/TAr6QXwPuiv5J4S8puubuT2ADHbwQIreSwmfMhJZzbBcvUbAKbVl2LDnPsgrHj8/28Cmqc6hHQJanok0epZq86TAWqUa9xmcQecanXlEXfTghiEEESbZMuU5HaLT4KvNHkNMxipho5dFLIen/zlI5i9EPwZmEOSORMCrlYCuOGbaZYwfVyo4qFY98QqN7/Zb4fDUbZudvFq9UcNy6wF/IbtOnEoaBvsRV8tld4NWWsXL2qmF8ziWesbAk6yldaljoLWPPDUu3gOp+lH4fYSEBfkM1SGkRPW3cooop+z2CxMgMKzWIv2lee80gbrJBN6/zLsDezo6cjyXGIXhf1d+AlNvC4ktGgNt88f8QPwDVJ7u/VNChVuIGnBgz4PVuZ+ZEZ+vNXDbLLGpW74AbAa/QWhPCg12wgXdq44VSP9izYkA/IiPL3FYllVUln/YURjPKn 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: The fix has two steps 1. Decrement HWPoison HugeTLB huge folio's refcount when it is 1. It is done in folios_put_refs during truncating or eviting HugeTLB file. 2. Dissolve HugeTLB HWpoison folio in free_huge_folio. Again, just for demo purpose, not a proper fix, especially step 1. Signed-off-by: Jiaqi Yan --- mm/hugetlb.c | 6 ++++++ mm/swap.c | 9 ++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 87761b042ed04..b28e0bc7f199f 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -1818,6 +1818,7 @@ void free_huge_folio(struct folio *folio) int nid = folio_nid(folio); struct hugepage_subpool *spool = hugetlb_folio_subpool(folio); bool restore_reserve; + bool hwpoison = folio_test_hwpoison(folio); unsigned long flags; VM_BUG_ON_FOLIO(folio_ref_count(folio), folio); @@ -1869,6 +1870,11 @@ void free_huge_folio(struct folio *folio) remove_hugetlb_folio(h, folio, true); spin_unlock_irqrestore(&hugetlb_lock, flags); update_and_free_hugetlb_folio(h, folio, true); + } else if (hwpoison) { + remove_hugetlb_folio(h, folio, false); + h->max_huge_pages--; + spin_unlock_irqrestore(&hugetlb_lock, flags); + update_and_free_hugetlb_folio(h, folio, true); } else { arch_clear_hugetlb_flags(folio); enqueue_hugetlb_folio(h, folio); diff --git a/mm/swap.c b/mm/swap.c index 746a5ceba42c9..d6b4d4cb4004f 100644 --- a/mm/swap.c +++ b/mm/swap.c @@ -961,7 +961,14 @@ void folios_put_refs(struct folio_batch *folios, unsigned int *refs) continue; } - if (!folio_ref_sub_and_test(folio, nr_refs)) + folio_ref_sub(folio, nr_refs); + + if (folio_test_hugetlb(folio) && + folio_ref_count(folio) == 1 && + folio_test_hwpoison(folio)) + folio_ref_dec(folio); + + if (folio_ref_count(folio) > 0) continue; /* hugetlb has its own memcg */