From patchwork Fri Jun 23 16:40:14 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiaqi Yan X-Patchwork-Id: 13290811 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 7487AC0015E for ; Fri, 23 Jun 2023 16:40:29 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DA2508D0008; Fri, 23 Jun 2023 12:40:26 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D054F8D0001; Fri, 23 Jun 2023 12:40:26 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BA5128D0008; Fri, 23 Jun 2023 12:40:26 -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 AB2048D0001 for ; Fri, 23 Jun 2023 12:40:26 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 73A5A1A02C0 for ; Fri, 23 Jun 2023 16:40:26 +0000 (UTC) X-FDA: 80934575652.03.03E7BA2 Received: from mail-pg1-f201.google.com (mail-pg1-f201.google.com [209.85.215.201]) by imf16.hostedemail.com (Postfix) with ESMTP id 7D09D180013 for ; Fri, 23 Jun 2023 16:40:24 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=5jLczQMp; spf=pass (imf16.hostedemail.com: domain of 398qVZAgKCBAzyq6yEq3w44w1u.s421y3AD-220Bqs0.47w@flex--jiaqiyan.bounces.google.com designates 209.85.215.201 as permitted sender) smtp.mailfrom=398qVZAgKCBAzyq6yEq3w44w1u.s421y3AD-220Bqs0.47w@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=1687538424; 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=bgkvS77jKBasQJ+d6+AqQuQkcASYxscDuOzFX9WAgds=; b=TD3xR/ZBeqraiHHaTEkeqry4VYqwAwMTTB//9HgjZBQlbtejTJvG5QN5EEmScLYn8Y3k6k rjNrUyAIS1IH6JOutiPBNMeTq0hTf2RsKvUFBMJdZiMvXNBFtsFHqBFExrxeO5ldPAvq9B nc8IYcSsY39b827JW1jXN4skUr0qoaQ= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1687538424; a=rsa-sha256; cv=none; b=nLPOd3wd/Kmc0GhDBJ2CR/OTXwfj6ktyIxLaNBQZIsrDTeRCZhv9lmUXo1Beyh0m+clwzb zdibMPDmHeKSW8LwHUMSmtDuNiVB9F5eI1ueRCnaCNqYGRaVxcaZUdTMYsMj0XICRwMe0d UcLkTv70LImQwCCV0oiRHYmjmNs3VAs= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=5jLczQMp; spf=pass (imf16.hostedemail.com: domain of 398qVZAgKCBAzyq6yEq3w44w1u.s421y3AD-220Bqs0.47w@flex--jiaqiyan.bounces.google.com designates 209.85.215.201 as permitted sender) smtp.mailfrom=398qVZAgKCBAzyq6yEq3w44w1u.s421y3AD-220Bqs0.47w@flex--jiaqiyan.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-5428d1915acso556818a12.0 for ; Fri, 23 Jun 2023 09:40:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1687538423; x=1690130423; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=bgkvS77jKBasQJ+d6+AqQuQkcASYxscDuOzFX9WAgds=; b=5jLczQMp0KSTUIagObRwmH0mr7CUCxFkzN7ENpojhmLHZ5GyofnMb+Nwwg0KbCrGdj 3WAW+iB30hh1CIP2jXtjzzqMP6aQHq0SumA8ktu4p1yPKHIcW5cNVVYSJhPCOejXnvPu nKl3TIA+Sl6yCFYzA+PCG++nBBaxmuxCyqVDC21b2OKlQOdzRKNglDYsR9F3RTTJRMYl AAMVjrlGjICuPd2odT2iHSoPtCqMZtxg6+MlTsPgioLvqTryZxWthG+Vh2bh22MjQDaQ 9B3I8J2H+/UZPeZ9NSrWwZ6tFvGN95BBwimNdsppJUqYqfC4usU/76Fm0JacOMdSXTse zXfw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687538423; x=1690130423; 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=bgkvS77jKBasQJ+d6+AqQuQkcASYxscDuOzFX9WAgds=; b=dBhdpdxL7PZOYHQK3wxdc/uiamM9s8dEq8sQu9k3ru6F2hTEltRq9nRUlGQqF6fhrQ CWtuqyq4mCjJxD9A17P3/S9MFmIB9Q6mxG+Yu4yji2rwKHKk9gMwCvmvX5jhTinVODgF YpZx8NgQ3AoNtjhTbyndBNyY/t3aLGdl6dAZfklEpgoEt1HdXOQK/kwbX1ysZTqeOOpb FPwolaAq33MmSJTEw04pCrh9PH+7mla/W81UaPdcl6I35m05+fgOv28cSxpO4ut/HrvB ia1C6ieSmdTfy/ImOwDQ1ETf6xqzw4Gx0fUq/jE2bvGTsWNjjQ2SBQktAvItTvFKyyFv NF3g== X-Gm-Message-State: AC+VfDzOykcEdkMKSInYn5yaf25kourovZBa/QGLm6l7eQdxaI07JzdD W3RWU5Y1eddvcXoyj9qkXuwRW5HR9DZMkA== X-Google-Smtp-Source: ACHHUZ7SVzvvZkW36k+iXmGUMtIymkIH4w+s1GAMmdhSj7Lx2MUYP2jnFMjcbaXPnGb5kaXvodFcOEtl1lhEHg== X-Received: from yjq3.c.googlers.com ([fda3:e722:ac3:cc00:24:72f4:c0a8:272f]) (user=jiaqiyan job=sendgmr) by 2002:a63:fc01:0:b0:557:2a54:cf08 with SMTP id j1-20020a63fc01000000b005572a54cf08mr1060696pgi.6.1687538423324; Fri, 23 Jun 2023 09:40:23 -0700 (PDT) Date: Fri, 23 Jun 2023 16:40:14 +0000 In-Reply-To: <20230623164015.3431990-1-jiaqiyan@google.com> Mime-Version: 1.0 References: <20230623164015.3431990-1-jiaqiyan@google.com> X-Mailer: git-send-email 2.41.0.162.gfafddb0af9-goog Message-ID: <20230623164015.3431990-4-jiaqiyan@google.com> Subject: [PATCH v2 3/4] hugetlbfs: improve read HWPOISON hugepage From: Jiaqi Yan To: mike.kravetz@oracle.com, naoya.horiguchi@nec.com Cc: songmuchun@bytedance.com, shy828301@gmail.com, linmiaohe@huawei.com, 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: ny377minbuy7hkunfixzmydgjocm3smq X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 7D09D180013 X-Rspam-User: X-HE-Tag: 1687538424-37870 X-HE-Meta: U2FsdGVkX1+DU3Hx/0y4aaxfwzxssCF0q50LS6PRRbGfjKcF1R0D73zChetjqv9yWn4ev8ovqwPf3BIglje00T+A1pfNYmbfC8XsQ2FxmcANeI02YsdkC1Oc+w4vRTopWZTru4PRAFAHeQbpL3byHdYbiiddXrXSEVIdHJd9Zy7tnZZlgMLIOUBRYJO8MykRZzgLvhHNlnThyZNFH0jDCR3BSdh1LWhaWI+jEBcyRCpDfC5EiobRAdlmKtUKDfIDgbHPL1sTc8nqEl0W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V4dn0rP/bruWjIkopiVHrP6llLtoGlq6WEu5kVKbYND66rIOF2ZflNWa0bS40MMAMKlZsL1Uau0fiUGrOjLZ1zSGLh0N1XTaPZEaitbpLavX9I5ZYK+5C9zl0EaykaMlo9kTnmev1C9sTgYBzISxgj34JZ017jBbJsbYh+prGPKXQzT5a4QOH6Nkiv9oTlQ7RPmfRmJAZXhJkT5FzQN6UUEVCKCW7sifILQWI0AAy/NVe519m1vzxStoAqEpb/pg1EKDTr1MEM4KB9Nt2hFevVRrrSvQ== 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: When a hugepage contains HWPOISON pages, read() fails to read any byte of the hugepage and returns -EIO, although many bytes in the HWPOISON hugepage are readable. Improve this by allowing hugetlbfs_read_iter returns as many bytes as possible. For a requested range [offset, offset + len) that contains HWPOISON page, return [offset, first HWPOISON page addr); the next read attempt will fail and return -EIO. Signed-off-by: Jiaqi Yan Reviewed-by: Mike Kravetz Reviewed-by: Naoya Horiguchi --- fs/hugetlbfs/inode.c | 58 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 52 insertions(+), 6 deletions(-) diff --git a/fs/hugetlbfs/inode.c b/fs/hugetlbfs/inode.c index 90361a922cec..86879ca3ff1e 100644 --- a/fs/hugetlbfs/inode.c +++ b/fs/hugetlbfs/inode.c @@ -282,6 +282,42 @@ hugetlb_get_unmapped_area(struct file *file, unsigned long addr, } #endif +/* + * Someone wants to read @bytes from a HWPOISON hugetlb @page from @offset. + * Returns the maximum number of bytes one can read without touching the 1st raw + * HWPOISON subpage. + * + * The implementation borrows the iteration logic from copy_page_to_iter*. + */ +static size_t adjust_range_hwpoison(struct page *page, size_t offset, size_t bytes) +{ + size_t n = 0; + size_t res = 0; + struct folio *folio = page_folio(page); + + /* First subpage to start the loop. */ + page += offset / PAGE_SIZE; + offset %= PAGE_SIZE; + while (1) { + if (is_raw_hwp_subpage(folio, page)) + break; + + /* Safe to read n bytes without touching HWPOISON subpage. */ + n = min(bytes, (size_t)PAGE_SIZE - offset); + res += n; + bytes -= n; + if (!bytes || !n) + break; + offset += n; + if (offset == PAGE_SIZE) { + page++; + offset = 0; + } + } + + return res; +} + /* * Support for read() - Find the page attached to f_mapping and copy out the * data. This provides functionality similar to filemap_read(). @@ -300,7 +336,7 @@ static ssize_t hugetlbfs_read_iter(struct kiocb *iocb, struct iov_iter *to) while (iov_iter_count(to)) { struct page *page; - size_t nr, copied; + size_t nr, copied, want; /* nr is the maximum number of bytes to copy from this page */ nr = huge_page_size(h); @@ -328,16 +364,26 @@ static ssize_t hugetlbfs_read_iter(struct kiocb *iocb, struct iov_iter *to) } else { unlock_page(page); - if (PageHWPoison(page)) { - put_page(page); - retval = -EIO; - break; + if (!PageHWPoison(page)) + want = nr; + else { + /* + * Adjust how many bytes safe to read without + * touching the 1st raw HWPOISON subpage after + * offset. + */ + want = adjust_range_hwpoison(page, offset, nr); + if (want == 0) { + put_page(page); + retval = -EIO; + break; + } } /* * We have the page, copy it to user space buffer. */ - copied = copy_page_to_iter(page, offset, nr, to); + copied = copy_page_to_iter(page, offset, want, to); put_page(page); } offset += copied;