From patchwork Fri Jul 7 20:19:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiaqi Yan X-Patchwork-Id: 13305253 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 2898FEB64D9 for ; Fri, 7 Jul 2023 20:19:23 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B1E746B007D; Fri, 7 Jul 2023 16:19:22 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A09CE6B007E; Fri, 7 Jul 2023 16:19:22 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8D1E86B0080; Fri, 7 Jul 2023 16:19:22 -0400 (EDT) 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 7F2646B007D for ; Fri, 7 Jul 2023 16:19:22 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 461D3C0108 for ; Fri, 7 Jul 2023 20:19:22 +0000 (UTC) X-FDA: 80985930564.10.8E77495 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) by imf10.hostedemail.com (Postfix) with ESMTP id 693D1C0016 for ; Fri, 7 Jul 2023 20:19:20 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=TldvMxXO; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf10.hostedemail.com: domain of 3R3OoZAgKCPolkcsk0cpiqqing.eqonkpwz-oomxcem.qti@flex--jiaqiyan.bounces.google.com designates 209.85.216.73 as permitted sender) smtp.mailfrom=3R3OoZAgKCPolkcsk0cpiqqing.eqonkpwz-oomxcem.qti@flex--jiaqiyan.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1688761160; 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=A+DZafON7JsoL7Ay0CKBI9HDnuWj0nfz7I/atmY5O6E=; b=f/ysoRPH0NsLjWulSSQvvY0fphXU9Fj8mv2zaHzXX5J1oDAjEd0JT1kBotn0x1yHcrrLSx a0/7rWrq5IDsWd+StBEGaaPor2McY8s2q7bCnv76rjk335K7pj4WluAZY22XSbKK9AweJ0 Cbc4k9f2NZ+Is77195Ua+t97gmla95g= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=TldvMxXO; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf10.hostedemail.com: domain of 3R3OoZAgKCPolkcsk0cpiqqing.eqonkpwz-oomxcem.qti@flex--jiaqiyan.bounces.google.com designates 209.85.216.73 as permitted sender) smtp.mailfrom=3R3OoZAgKCPolkcsk0cpiqqing.eqonkpwz-oomxcem.qti@flex--jiaqiyan.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1688761160; a=rsa-sha256; cv=none; b=u0b/seMto0mkZh72hns8bXAFC0Qt04LNz5ThDldGxPmkoyoQiq1s5bGBtyuenRWsGOsumw eXybaDvz4DYIpwFP+9hGx7RWP5MqVqTVQhPaGZvxXPxH27x8GJxK2px4pv+H8ove3yh1aY MBm+Bf+5jo5cVWs7DuKadLqHUw/O/x4= Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-262e2cb725eso3744419a91.0 for ; Fri, 07 Jul 2023 13:19:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1688761159; x=1691353159; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=A+DZafON7JsoL7Ay0CKBI9HDnuWj0nfz7I/atmY5O6E=; b=TldvMxXOlAitzanCYBdJPKTFsceaBUtZtfHE+NCMC7uRLSuEMdNFphYxZysQPlOWyt 6IIkCSmbZ0QhAj6BdA2S0ZbkwbhR7x3BX+nwYBGgaOxeyqB+HsmP8eFpNEMX/EjaKoHw Osqpj/mJ1MqPaQbsakEeDVTjMuhGWE9GnXwVc8F/i6rZzYOS2/oL2zNYYKzVVfG8aSiy zLaZC2KHe+RKfCbvOVNYPOcE1YOJUR4cbXfL9Nm1YDmuYA43h0rfivcjthaU3URwFYSK 96WU5M8uzwqIi+5+ywPkqjzgjsM0fGa0w0GzRv5r4kHftPEO8J5DvOvCOoUqVSHiN5aw 6/8g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688761159; x=1691353159; 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=A+DZafON7JsoL7Ay0CKBI9HDnuWj0nfz7I/atmY5O6E=; b=TvH1D5XCEWnzXMUcp4HsQus8cMMihba6/M1cWrnpo6GWauJExxpNCHuGvW07/8TJ/2 ipaTIWVOSUujSjFC4388YJLRP/rUQbX/upMDQFHJA6A6E4d4s+Ti7CrB0HDZji1QCCoG ZC/8rX14SG2WuOotX8JIRbdqVw/q0aey6RyOthv5KGjvO8fAFjI2aDUHHMe2MxdTz24x zW3FJA6J+ACcYLNUEHB4fL2CKj6GzKPz0HTfFzKt1RHU3bIXXZxzaWUMNBGs1rFOlzuJ zGtluNKTmpcCrzHcnGJPqGeo7MAvJ1FxflqRnLwL3unaEsAOqmteRPIQnzuSDkyeoGVa I+/A== X-Gm-Message-State: ABy/qLY2eQYJ5WDPClpIkYTj99npL+4Utjo9ivl4QODz3WHtgGk+s/Pv 6XcX1YWnYYDKIT1nN+pS7bLWTbujc+DeXw== X-Google-Smtp-Source: APBJJlFlQ7RKBImKNNS3u3h3d6OO1HQ+Hd+V+iBkTossPJ4vugefj5v7Rlyl3uxPne+VgqOiM11xG9BXaRtiDQ== X-Received: from yjq3.c.googlers.com ([fda3:e722:ac3:cc00:24:72f4:c0a8:272f]) (user=jiaqiyan job=sendgmr) by 2002:a17:90a:ce18:b0:263:49d3:8024 with SMTP id f24-20020a17090ace1800b0026349d38024mr4778324pju.1.1688761159291; Fri, 07 Jul 2023 13:19:19 -0700 (PDT) Date: Fri, 7 Jul 2023 20:19:03 +0000 In-Reply-To: <20230707201904.953262-1-jiaqiyan@google.com> Mime-Version: 1.0 References: <20230707201904.953262-1-jiaqiyan@google.com> X-Mailer: git-send-email 2.41.0.255.g8b1d071c50-goog Message-ID: <20230707201904.953262-4-jiaqiyan@google.com> Subject: [PATCH v3 3/4] hugetlbfs: improve read HWPOISON hugepage From: Jiaqi Yan To: akpm@linux-foundation.org, mike.kravetz@oracle.com, naoya.horiguchi@nec.com Cc: songmuchun@bytedance.com, shy828301@gmail.com, linmiaohe@huawei.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, duenwen@google.com, axelrasmussen@google.com, jthoughton@google.com, Jiaqi Yan X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 693D1C0016 X-Stat-Signature: cs8gn377atgbqecg3qkmpo7bz7jcnkgp X-HE-Tag: 1688761160-209529 X-HE-Meta: U2FsdGVkX1+8u7NR5YsJ9XKLF6secW/8ZXqvKsv0ldBFoGmdrhWlgr2Tr8KFfVvARC9D5dsvtXZ3VOjsof3rGpxopGJ+Gqjjp4KKGPV3YcNok6ZIQGaWrZQu7Trs/q31oS/oRlR1XQ6JNp/mwMax0Bfdtt5+oVioqjClaPAIWHDQA46fk/aHLjocj6dc9ilpI+0A2v2+/dOxp5bQa1LgNpbeIrvHDaRQcVBpvRPELa+a01nK9un0W2eiwoNzOqp0SR5G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Wrqg53UMZE1104agBGPpAfL14U5NSqR/xPkcM/upb8t+1LMOS34nPPqNNoSlUeh1VNxqPxyhCR/W/uZcDpB2dtWzLWn6ue4oG42AlGoixYhk2EG09jPSvDyB9NQ97VfRiDBJ8DDmyZLhax55Io6+IamC60KeuUj28GqyBZ0rWBUZgIWVIATQw6gD52ctGU5eJ4Yxc5adAc1J9eUDZY0sZGokalhgtbtPY5lXASSVl1pGDzstVjOaLUyTVI8+9OGW2FPVTgxV8PU3PVvgpdg0+t39RviqT4en8Gy7AZgA6ogoZctiF3YHr1nhGB9bRU9XTYLls= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000001, 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. Reviewed-by: Mike Kravetz Reviewed-by: Naoya Horiguchi Signed-off-by: Jiaqi Yan --- 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 7b17ccfa039d..c2b807d37f85 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;