From patchwork Wed May 17 16:09:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiaqi Yan X-Patchwork-Id: 13245277 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 EF6D8C77B7D for ; Wed, 17 May 2023 16:10:10 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2EEF7900004; Wed, 17 May 2023 12:10:10 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 29F1C900003; Wed, 17 May 2023 12:10:10 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 140AE900004; Wed, 17 May 2023 12:10:10 -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 02087900003 for ; Wed, 17 May 2023 12:10:10 -0400 (EDT) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id BF498A0672 for ; Wed, 17 May 2023 16:10:08 +0000 (UTC) X-FDA: 80800233696.04.104F25C Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) by imf01.hostedemail.com (Postfix) with ESMTP id 22E814001C for ; Wed, 17 May 2023 16:10:05 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=KnKAovPQ; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf01.hostedemail.com: domain of 3XPxkZAgKCBM21t91Ht6z77z4x.v75416DG-553Etv3.7Az@flex--jiaqiyan.bounces.google.com designates 209.85.216.73 as permitted sender) smtp.mailfrom=3XPxkZAgKCBM21t91Ht6z77z4x.v75416DG-553Etv3.7Az@flex--jiaqiyan.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1684339806; 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=kGxg+zhhm/tdTqkE4MZxwuMTE52MydtCt+SDG4bfPzg=; b=h56i9hl3MbSdhT8BwZrb/vverfCRpk0dX0gFk86wt0aYylbZtue1n8iLirfi/4A6kUggXx RModtsmvyHiF+z8CRrF7VM2Vc+inSNtdFJhWIJ2r6bD7wSksLDWQKRo6b214ZwNgT1o61k xziyKDESzXqTwA5aUGD/5Xe7QEtfnZ0= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=KnKAovPQ; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf01.hostedemail.com: domain of 3XPxkZAgKCBM21t91Ht6z77z4x.v75416DG-553Etv3.7Az@flex--jiaqiyan.bounces.google.com designates 209.85.216.73 as permitted sender) smtp.mailfrom=3XPxkZAgKCBM21t91Ht6z77z4x.v75416DG-553Etv3.7Az@flex--jiaqiyan.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1684339806; a=rsa-sha256; cv=none; b=ZuumBtG/XzSlXMnYhsg+gwgv0PRjtXDGhSLEYj1xOojjbHawNyFWaeg67dyjWds8g/0zsC pmrcQOdkp2pxWokJvgcC0fwGiX3cYz5FZcl6gdDh6Zas/UdTbVXLKlTB6OKQCEa6aaf3fQ WJOA05nAnZvR6SIRzWTaMxQ4QfBnGa8= Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-2505b80d5b3so98424a91.0 for ; Wed, 17 May 2023 09:10:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1684339805; x=1686931805; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=kGxg+zhhm/tdTqkE4MZxwuMTE52MydtCt+SDG4bfPzg=; b=KnKAovPQXFj1RExR67Af1qVGSh2sLTfcDMR3AcMJtvl3707WaSzfh6aMkZSZmDD1X+ uzn/EpGaYm2cxuC0FxRYA+GlUxMapzpXiQSZLwZhacfAiuPjYYlLDj85PEJfe1PEIV2b 1D9DlD2BU/7jhp1KwyV52RHdPFRtnFHBB5wK3f6YVen8fubFlK4YWeCIctalUfZwKjKw YTKrF3QNZkBgvpjwSv9cFl6QHcB+0zWqTgPqRHQcoyEbHy4QQTQGIDt1d3FcwkENmt1n Fco52N7YFesAPXg4TnvA5SWtsXJwC7EHDj/K9pELCkr7bQaLZFqUya9X/vOAu+zxDYAE Ou6A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684339805; x=1686931805; 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=kGxg+zhhm/tdTqkE4MZxwuMTE52MydtCt+SDG4bfPzg=; b=Iw8z+2F88jWwcVFq6N3/6ljtpJfuh32gwQwHS+7IapijCuxGWKkNIm06/8cT9U2BW7 PCQiTCSWvlRspx4yVncCH+CsgI1zhgnYy0sZBB7Jm7ZywiDLAzaaVucgdPuEh1epEnj3 OAUQ1eZt2MU/RxHvRbNTDF4amcSNbrCYJKMPovXNNrlXKUMbuIdnyjt1HVcmlZcuJazn IMxr1GggSx3BNRuPs0LLmaDbwquniJqyZ1VRYuStYcs9aaZevu8P7y06vZofpi3vPbWH 83f08xSeJS9UQareka0IoVr+ZaTZIZuNngd6mLjSpnLPB3Wogw23UK3pWcw4FOeQSM1y 6TOA== X-Gm-Message-State: AC+VfDx2bhdliXWgJMcn63QmM9I7c+dOmxDidPVAfge8Rr3cA9d42gLO cVdkn3p4I1VHXmSnqeKKTtqRIFqGgUaPCA== X-Google-Smtp-Source: ACHHUZ5+d60PcaMXbxsAMlias9eLlwRUCq3s5d4aGQz1cuagr41Ek8tZIoO9lZquq6XMY0bgQXJgoSJwjv+7Jg== X-Received: from yjq3.c.googlers.com ([fda3:e722:ac3:cc00:24:72f4:c0a8:272f]) (user=jiaqiyan job=sendgmr) by 2002:a17:90b:89:b0:253:4800:438b with SMTP id bb9-20020a17090b008900b002534800438bmr7216pjb.2.1684339804718; Wed, 17 May 2023 09:10:04 -0700 (PDT) Date: Wed, 17 May 2023 16:09:48 +0000 In-Reply-To: <20230517160948.811355-1-jiaqiyan@google.com> Mime-Version: 1.0 References: <20230517160948.811355-1-jiaqiyan@google.com> X-Mailer: git-send-email 2.40.1.606.ga4b1b128d6-goog Message-ID: <20230517160948.811355-4-jiaqiyan@google.com> Subject: [PATCH v1 3/3] selftests/mm: add tests for HWPOISON hugetlbfs read From: Jiaqi Yan To: mike.kravetz@oracle.com, songmuchun@bytedance.com, naoya.horiguchi@nec.com, shy828301@gmail.com, linmiaohe@huawei.com Cc: akpm@linux-foundation.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, duenwen@google.com, axelrasmussen@google.com, jthoughton@google.com, Jiaqi Yan X-Stat-Signature: 3gigqch91cgfxazjjtyuc4bnh4qutgz6 X-Rspam-User: X-Rspamd-Queue-Id: 22E814001C X-Rspamd-Server: rspam07 X-HE-Tag: 1684339805-834597 X-HE-Meta: U2FsdGVkX1915DGnWj+Ns1O1RbHZgXfTpJ3CkPbOZBK9PsdS56DfZ9mZM9HctYQ9Jp34GmKE4s6855V0b6FulvFTWd5+Vhf7Xe/PGgRuc67HFoBLYP5buEM82SZmS6NQuMl01+DrHhLapxeAiK0uJFChsCOnDHPEpZfOMNicaqWTHcRvFsmCfeF2+ZY8sB0RQuzLH5jvSDuyaDKf4jEOMadAWYS1yW8s7NTUio8nR4iPCLgYXeFHqKet1GrBpl9QD6KlkHuZHO4p9mqGDO6CInjr1ZXwn2yWLre2zAqAGjHVkvK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D21LuRTjf3Yg/TET0pYFTZlzEalbf7BneGtPfkLDc12lAX6gI4CWJ+qvDYg289R94D6Mp3IX0Avg/eIYfmPMao9vAFBj62BZI4P2CjladiXhOdDQ2xk5LErGntuG7wK04eOjJHNCh+csE4HTy8U4qpudtJSZGEeojGxnkLlqMs60CGo8UTQiedMPMMrtjQxK2sBGNFFkeknY1CmiMfl4FcJXZMQisIzesEZ+Ja0qAk/+IgfSf+m44A== 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: Add tests for the improvement made to read operations on HWPOISON hugetlb page with different read granularities. 0) Simple regression test on read. 1) Sequential read page by page should succeed until encounters the 1st raw HWPOISON subpage. 2) After skip raw HWPOISON subpage by lseek, read always succeeds. Signed-off-by: Jiaqi Yan --- tools/testing/selftests/mm/.gitignore | 1 + tools/testing/selftests/mm/Makefile | 1 + .../selftests/mm/hugetlb-read-hwpoison.c | 322 ++++++++++++++++++ 3 files changed, 324 insertions(+) create mode 100644 tools/testing/selftests/mm/hugetlb-read-hwpoison.c diff --git a/tools/testing/selftests/mm/.gitignore b/tools/testing/selftests/mm/.gitignore index 8917455f4f51..fe8224d2ee06 100644 --- a/tools/testing/selftests/mm/.gitignore +++ b/tools/testing/selftests/mm/.gitignore @@ -5,6 +5,7 @@ hugepage-mremap hugepage-shm hugepage-vmemmap hugetlb-madvise +hugetlb-read-hwpoison khugepaged map_hugetlb map_populate diff --git a/tools/testing/selftests/mm/Makefile b/tools/testing/selftests/mm/Makefile index 23af4633f0f4..6cc63668c50e 100644 --- a/tools/testing/selftests/mm/Makefile +++ b/tools/testing/selftests/mm/Makefile @@ -37,6 +37,7 @@ TEST_GEN_PROGS += compaction_test TEST_GEN_PROGS += gup_test TEST_GEN_PROGS += hmm-tests TEST_GEN_PROGS += hugetlb-madvise +TEST_GEN_PROGS += hugetlb-read-hwpoison TEST_GEN_PROGS += hugepage-mmap TEST_GEN_PROGS += hugepage-mremap TEST_GEN_PROGS += hugepage-shm diff --git a/tools/testing/selftests/mm/hugetlb-read-hwpoison.c b/tools/testing/selftests/mm/hugetlb-read-hwpoison.c new file mode 100644 index 000000000000..2f8e84eceb3d --- /dev/null +++ b/tools/testing/selftests/mm/hugetlb-read-hwpoison.c @@ -0,0 +1,322 @@ +// SPDX-License-Identifier: GPL-2.0 + +#define _GNU_SOURCE +#include +#include +#include + +#include +#include +#include +#include +#include + +#include "../kselftest.h" + +#define PREFIX " ... " +#define ERROR_PREFIX " !!! " + +#define MAX_WRITE_READ_CHUNK_SIZE (getpagesize() * 16) +#define MAX(a, b) (((a) > (b)) ? (a) : (b)) + +enum test_status { + TEST_PASSED = 0, + TEST_FAILED = 1, + TEST_SKIPPED = 2, +}; + +static char *status_to_str(enum test_status status) +{ + switch (status) { + case TEST_PASSED: + return "TEST_PASSED"; + case TEST_FAILED: + return "TEST_FAILED"; + case TEST_SKIPPED: + return "TEST_SKIPPED"; + default: + return "TEST_???"; + } +} + +static int setup_filemap(char *filemap, size_t len, size_t wr_chunk_size) +{ + char iter = 0; + + for (size_t offset = 0; offset < len; + offset += wr_chunk_size) { + iter++; + memset(filemap + offset, iter, wr_chunk_size); + } + + return 0; +} + +static bool verify_chunk(char *buf, size_t len, char val) +{ + size_t i; + + for (i = 0; i < len; ++i) { + if (buf[i] != val) { + printf(ERROR_PREFIX "check fail: buf[%lu] = %u != %u\n", + i, buf[i], val); + return false; + } + } + + return true; +} + +static bool seek_read_hugepage_filemap(int fd, size_t len, size_t wr_chunk_size, + off_t offset, size_t expected) +{ + char buf[MAX_WRITE_READ_CHUNK_SIZE]; + ssize_t ret_count = 0; + ssize_t total_ret_count = 0; + char val = offset / wr_chunk_size + offset % wr_chunk_size; + + printf(PREFIX "init val=%u with offset=0x%lx\n", val, offset); + printf(PREFIX "expect to read 0x%lx bytes of data in total\n", + expected); + if (lseek(fd, offset, SEEK_SET) < 0) { + perror(ERROR_PREFIX "seek failed"); + return false; + } + + while (offset + total_ret_count < len) { + ret_count = read(fd, buf, wr_chunk_size); + if (ret_count == 0) { + printf(PREFIX "read reach end of the file\n"); + break; + } else if (ret_count < 0) { + perror(ERROR_PREFIX "read failed"); + break; + } + ++val; + if (!verify_chunk(buf, ret_count, val)) + return false; + + total_ret_count += ret_count; + } + printf(PREFIX "actually read 0x%lx bytes of data in total\n", + total_ret_count); + + return total_ret_count == expected; +} + +static bool read_hugepage_filemap(int fd, size_t len, + size_t wr_chunk_size, size_t expected) +{ + char buf[MAX_WRITE_READ_CHUNK_SIZE]; + ssize_t ret_count = 0; + ssize_t total_ret_count = 0; + char val = 0; + + printf(PREFIX "expect to read 0x%lx bytes of data in total\n", + expected); + while (total_ret_count < len) { + ret_count = read(fd, buf, wr_chunk_size); + if (ret_count == 0) { + printf(PREFIX "read reach end of the file\n"); + break; + } else if (ret_count < 0) { + perror(ERROR_PREFIX "read failed"); + break; + } + ++val; + if (!verify_chunk(buf, ret_count, val)) + return false; + + total_ret_count += ret_count; + } + printf(PREFIX "actually read 0x%lx bytes of data in total\n", + total_ret_count); + + return total_ret_count == expected; +} + +static enum test_status +test_hugetlb_read(int fd, size_t len, size_t wr_chunk_size) +{ + enum test_status status = TEST_SKIPPED; + char *filemap = NULL; + + if (ftruncate(fd, len) < 0) { + perror(ERROR_PREFIX "ftruncate failed"); + return status; + } + + filemap = mmap(NULL, len, PROT_READ | PROT_WRITE, + MAP_SHARED | MAP_POPULATE, fd, 0); + if (filemap == MAP_FAILED) { + perror(ERROR_PREFIX "mmap for primary mapping failed"); + goto done; + } + + setup_filemap(filemap, len, wr_chunk_size); + status = TEST_FAILED; + + if (read_hugepage_filemap(fd, len, wr_chunk_size, len)) + status = TEST_PASSED; + + munmap(filemap, len); +done: + if (ftruncate(fd, 0) < 0) { + perror(ERROR_PREFIX "ftruncate back to 0 failed"); + status = TEST_FAILED; + } + + return status; +} + +static enum test_status +test_hugetlb_read_hwpoison(int fd, size_t len, size_t wr_chunk_size, + bool skip_hwpoison_page) +{ + enum test_status status = TEST_SKIPPED; + char *filemap = NULL; + char *hwp_addr = NULL; + const unsigned long pagesize = getpagesize(); + + if (ftruncate(fd, len) < 0) { + perror(ERROR_PREFIX "ftruncate failed"); + return status; + } + + filemap = mmap(NULL, len, PROT_READ | PROT_WRITE, + MAP_SHARED | MAP_POPULATE, fd, 0); + if (filemap == MAP_FAILED) { + perror(ERROR_PREFIX "mmap for primary mapping failed"); + goto done; + } + + setup_filemap(filemap, len, wr_chunk_size); + status = TEST_FAILED; + + /* + * Poisoned hugetlb page layout (assume hugepagesize=2MB): + * |<---------------------- 1MB ---------------------->| + * |<---- healthy page ---->|<---- HWPOISON page ----->| + * |<------------------- (1MB - 8KB) ----------------->| + */ + hwp_addr = filemap + len / 2 + pagesize; + if (madvise(hwp_addr, pagesize, MADV_HWPOISON) < 0) { + perror(ERROR_PREFIX "MADV_HWPOISON failed"); + goto unmap; + } + + if (!skip_hwpoison_page) { + /* + * Userspace should be able to read (1MB + 1 page) from + * the beginning of the HWPOISONed hugepage. + */ + if (read_hugepage_filemap(fd, len, wr_chunk_size, + len / 2 + pagesize)) + status = TEST_PASSED; + } else { + /* + * Userspace should be able to read (1MB - 2 pages) from + * HWPOISONed hugepage. + */ + if (seek_read_hugepage_filemap(fd, len, wr_chunk_size, + len / 2 + MAX(2 * pagesize, wr_chunk_size), + len / 2 - MAX(2 * pagesize, wr_chunk_size))) + status = TEST_PASSED; + } + +unmap: + munmap(filemap, len); +done: + if (ftruncate(fd, 0) < 0) { + perror(ERROR_PREFIX "ftruncate back to 0 failed"); + status = TEST_FAILED; + } + + return status; +} + +static int create_hugetlbfs_file(struct statfs *file_stat) +{ + int fd; + + fd = memfd_create("hugetlb_tmp", MFD_HUGETLB); + if (fd < 0) { + perror(ERROR_PREFIX "could not open hugetlbfs file"); + return -1; + } + + memset(file_stat, 0, sizeof(*file_stat)); + if (fstatfs(fd, file_stat)) { + perror(ERROR_PREFIX "fstatfs failed"); + goto close; + } + if (file_stat->f_type != HUGETLBFS_MAGIC) { + printf(ERROR_PREFIX "not hugetlbfs file\n"); + goto close; + } + + return fd; +close: + close(fd); + return -1; +} + +int main(void) +{ + int fd; + struct statfs file_stat; + enum test_status status; + /* Test read() in different granularity. */ + size_t wr_chunk_sizes[] = { + getpagesize() / 2, getpagesize(), + getpagesize() * 2, getpagesize() * 4 + }; + size_t i; + + for (i = 0; i < ARRAY_SIZE(wr_chunk_sizes); ++i) { + printf(PREFIX "Write/read chunk size=0x%lx\n", + wr_chunk_sizes[i]); + + fd = create_hugetlbfs_file(&file_stat); + if (fd < 0) + goto create_failure; + printf("HugeTLB read regression test...\n"); + status = test_hugetlb_read(fd, file_stat.f_bsize, + wr_chunk_sizes[i]); + printf("HugeTLB read regression test...%s\n", + status_to_str(status)); + close(fd); + if (status == TEST_FAILED) + return -1; + + fd = create_hugetlbfs_file(&file_stat); + if (fd < 0) + goto create_failure; + printf("HugeTLB read HWPOISON test...\n"); + status = test_hugetlb_read_hwpoison(fd, file_stat.f_bsize, + wr_chunk_sizes[i], false); + printf("HugeTLB read HWPOISON test...%s\n", + status_to_str(status)); + close(fd); + if (status == TEST_FAILED) + return -1; + + fd = create_hugetlbfs_file(&file_stat); + if (fd < 0) + goto create_failure; + printf("HugeTLB seek then read HWPOISON test...\n"); + status = test_hugetlb_read_hwpoison(fd, file_stat.f_bsize, + wr_chunk_sizes[i], true); + printf("HugeTLB seek then read HWPOISON test...%s\n", + status_to_str(status)); + close(fd); + if (status == TEST_FAILED) + return -1; + } + + return 0; + +create_failure: + printf(ERROR_PREFIX "Abort test: failed to create hugetlbfs file\n"); + return -1; +}