From patchwork Thu Apr 22 00:37:33 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hugh Dickins X-Patchwork-Id: 12217205 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-24.8 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_SANE_1,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id B68ACC43460 for ; Thu, 22 Apr 2021 00:37:39 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 784D861445 for ; Thu, 22 Apr 2021 00:37:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343936AbhDVAiM (ORCPT ); Wed, 21 Apr 2021 20:38:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50740 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234856AbhDVAiM (ORCPT ); Wed, 21 Apr 2021 20:38:12 -0400 Received: from mail-qk1-x72f.google.com (mail-qk1-x72f.google.com [IPv6:2607:f8b0:4864:20::72f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D466FC06174A for ; Wed, 21 Apr 2021 17:37:36 -0700 (PDT) Received: by mail-qk1-x72f.google.com with SMTP id 8so11037896qkv.8 for ; Wed, 21 Apr 2021 17:37:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:from:to:cc:subject:in-reply-to:message-id:references :user-agent:mime-version; bh=zQ3oRqFEaYwUupWBz0Ob+9p/m78IpqRSVRzOwl1NExU=; b=YA4YGWCqTJFmMsrOHxlI/UdbFCODBETZ0PKicjaq8W+MO7k74dvfKTRVJ5blFY7Bat i4hOjfxV+MY28sfBh7CZQ2S4mSLZcTJzzwU5phkRvU0O95Qp8gQvGugTxKyjvJOTEZ/4 vYnaIZGmMbA1ipt0DA1S73cMSVJRkDXPuK3n2oFOkJI7QjXuQVL9NNaBxfT3GSKYDU1h THOWkOTIgOZnvrWCMm3JEH/LYbRfEoVLD6GZib4mLzDzIvE8WRoIKI7csn9sKv8qET9b wuHpi3aE4SL8MkROICgmJxe7MRbe0q72Zyp7h8cUbriKNb5YqtAekLueCKKnBVBzNGPt AV/A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:in-reply-to:message-id :references:user-agent:mime-version; bh=zQ3oRqFEaYwUupWBz0Ob+9p/m78IpqRSVRzOwl1NExU=; b=enDqZwgI6RcR8N/RCVJU1vFImd+UzuN83L3+MJRj7In4xt6jW/CAk4OSRutC4P6A95 5XcJnvN/pzS6acyRg0BMKfzqlMJtlmaJyquqaEnjTnHl0tEIm5O6rwkMNYY6cjemL7jz T9DJNDa58pz4J8Srros1l4lN4Y8WFJEVnhrOpxOInPSCX8nQR/Zlo2/7kYYXW1ZCtRFV 20agFYxufrYTHJ2MuJsNiiKlUbSt60eAKfPRPHgYeqEHpaD7Cj8MTwOFhXAl3s+o8HsY D+7IznMhYi9cIKt09fAMe0e5pOfvHunV/0ehrCs2EBflZy8eRDgymH6wFZMdaRwWfSu/ g7CQ== X-Gm-Message-State: AOAM531Gjovl3rY4Bw9q3bejgJg2V5SM/AKkUvD13N6qyGWyN2yRX4sO YvM+CudQY05wYjYxvobt/tI52w== X-Google-Smtp-Source: ABdhPJxhPr50cgrqI2sPQlTusR7MLgOC52sKYJ/5T1GklWlbSpBpN1INHypgW+5A+R7wJBx4RtPRvg== X-Received: by 2002:a37:8bc1:: with SMTP id n184mr1021888qkd.268.1619051855892; Wed, 21 Apr 2021 17:37:35 -0700 (PDT) Received: from eggly.attlocal.net (172-10-233-147.lightspeed.sntcca.sbcglobal.net. [172.10.233.147]) by smtp.gmail.com with ESMTPSA id h65sm933294qkc.128.2021.04.21.17.37.34 (version=TLS1 cipher=ECDHE-ECDSA-AES128-SHA bits=128/128); Wed, 21 Apr 2021 17:37:35 -0700 (PDT) Date: Wed, 21 Apr 2021 17:37:33 -0700 (PDT) From: Hugh Dickins X-X-Sender: hugh@eggly.anvils To: Andrew Morton cc: Hugh Dickins , Matthew Wilcox , William Kucharski , Christoph Hellwig , Jan Kara , Dave Chinner , Johannes Weiner , "Kirill A. Shutemov" , Yang Shi , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH 1/2] mm/filemap: fix find_lock_entries hang on 32-bit THP In-Reply-To: Message-ID: References: User-Agent: Alpine 2.11 (LSU 23 2013-08-11) MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org No problem on 64-bit, or without huge pages, but xfstests generic/308 hung uninterruptibly on 32-bit huge tmpfs. Since 4.13's 0cc3b0ec23ce ("Clarify (and fix) MAX_LFS_FILESIZE macros"), MAX_LFS_FILESIZE is only a PAGE_SIZE away from wrapping 32-bit xa_index to 0, so the new find_lock_entries() has to be extra careful when handling a THP. Fixes: 5c211ba29deb ("mm: add and use find_lock_entries") Signed-off-by: Hugh Dickins --- mm/filemap.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) --- 5.12-rc8/mm/filemap.c 2021-02-26 19:42:39.812156085 -0800 +++ linux/mm/filemap.c 2021-04-20 23:20:20.509464440 -0700 @@ -1969,8 +1969,14 @@ unlock: put: put_page(page); next: - if (!xa_is_value(page) && PageTransHuge(page)) - xas_set(&xas, page->index + thp_nr_pages(page)); + if (!xa_is_value(page) && PageTransHuge(page)) { + unsigned int nr_pages = thp_nr_pages(page); + + /* Final THP may cross MAX_LFS_FILESIZE on 32-bit */ + xas_set(&xas, page->index + nr_pages); + if (xas.xa_index < nr_pages) + break; + } } rcu_read_unlock(); From patchwork Thu Apr 22 00:39:14 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hugh Dickins X-Patchwork-Id: 12217229 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-24.8 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_SANE_1,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8657FC433ED for ; Thu, 22 Apr 2021 00:40:09 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 3FEF661445 for ; Thu, 22 Apr 2021 00:40:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244014AbhDVAkl (ORCPT ); Wed, 21 Apr 2021 20:40:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51132 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344046AbhDVAjw (ORCPT ); Wed, 21 Apr 2021 20:39:52 -0400 Received: from mail-qt1-x82f.google.com (mail-qt1-x82f.google.com [IPv6:2607:f8b0:4864:20::82f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 02AF4C06138B for ; Wed, 21 Apr 2021 17:39:18 -0700 (PDT) Received: by mail-qt1-x82f.google.com with SMTP id c6so32665735qtc.1 for ; Wed, 21 Apr 2021 17:39:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:from:to:cc:subject:in-reply-to:message-id:references :user-agent:mime-version; bh=7i3jgTFMgSvDDCvaM/z3dZrqsQ7aoX20W1+zrU4bC9A=; b=cYmtc9W0OisFwodZq+YMaQ5sdbzZ/dQA3o03OF3rzbtnjrJU7alFKYG+oCHrfcyqzu kqD//2oRwaiOSkrEikSGLP4Z/xoOFm2vGexS/FdGw0ODgbaP/v9HEXOeXxeT4STG/DZu xj1uXLAAlfjhE1NkzxuDcYRLZ8aWdH7hwtvsBJsF/0IP47cTo8mwajMlw5g51ymOIeT/ J+di8TSFsA/30iBnurEbpjMPKJ1fvziHdujGmsGazeWJd2YZVgU8Q8ISgc7Wczn8rMXE qcuEYVkffuhPSfe5sghURh8aRX6EolLzfur43tAgomfo8XqpXmudWHThAwG42MDpIgaz bWlA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:in-reply-to:message-id :references:user-agent:mime-version; bh=7i3jgTFMgSvDDCvaM/z3dZrqsQ7aoX20W1+zrU4bC9A=; b=Q2TuGgvo8Y4WpnIzeqwliUBidUN68qBmSOvgJj/P8+N1kmf2Nx76tNRJlzXkBkI8mt Y0caTB8ji236vrcNe3jzflyyfTytO9pqvXIzhylfzF5YNvMfBUTwDXLjFCLJoFBDrZQE SNMsS/1S7b5bxRVRQp9Tu1361WRVm8DyZ0DixpfklrBEfeyhjDTzAp/iT6P31VbrqxM3 hHAY0Iriz9i7tAzv3VbDM69y3orA2buRj2sP9kBvFKRP3ybWvW6q+n2LPDmHOmb6Rl65 mT/g7Knshhn8KEoaDrYaGu96AgckIb4hDFfZYlcQtIV+B1iMI95PQWFG/c9TJk7OSqXN 58Cw== X-Gm-Message-State: AOAM532SQCsN69d3CtClsHt22ARZ3tLjAwtxfHOAb7brM0IPVEDVzZVj AHE2ij6fRW7VmTaW2BPDShm6yA== X-Google-Smtp-Source: ABdhPJxyK80llc+AABNVsntc/Os39N9486kEwBMS9LuEx4w7eCu0ZjsQZO2CgxQ+NLgYQ2hVhC1Deg== X-Received: by 2002:a05:622a:1449:: with SMTP id v9mr531639qtx.324.1619051957003; Wed, 21 Apr 2021 17:39:17 -0700 (PDT) Received: from eggly.attlocal.net (172-10-233-147.lightspeed.sntcca.sbcglobal.net. [172.10.233.147]) by smtp.gmail.com with ESMTPSA id a30sm1269538qtn.4.2021.04.21.17.39.15 (version=TLS1 cipher=ECDHE-ECDSA-AES128-SHA bits=128/128); Wed, 21 Apr 2021 17:39:16 -0700 (PDT) Date: Wed, 21 Apr 2021 17:39:14 -0700 (PDT) From: Hugh Dickins X-X-Sender: hugh@eggly.anvils To: Andrew Morton cc: Hugh Dickins , Matthew Wilcox , William Kucharski , Christoph Hellwig , Jan Kara , Dave Chinner , Johannes Weiner , "Kirill A. Shutemov" , Yang Shi , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH 2/2] mm/filemap: fix mapping_seek_hole_data on THP & 32-bit In-Reply-To: Message-ID: References: User-Agent: Alpine 2.11 (LSU 23 2013-08-11) MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org No problem on 64-bit without huge pages, but xfstests generic/285 and other SEEK_HOLE/SEEK_DATA tests have regressed on huge tmpfs, and on 32-bit architectures, with the new mapping_seek_hole_data(). Several different bugs turned out to need fixing. u64 casts added to stop unfortunate sign-extension when shifting (and let's use shifts throughout, rather than mixed with * and /). Use round_up() when advancing pos, to stop assuming that pos was already THP-aligned when advancing it by THP-size. (But I believe this use of round_up() assumes that any THP must be THP-aligned: true while tmpfs enforces that alignment, and is the only fs with FS_THP_SUPPORT; but might need to be generalized in the future? If I try to generalize it right now, I'm sure to get it wrong!) Use xas_set() when iterating away from a THP, so that xa_index stays in synch with start, instead of drifting away to return bogus offset. Check start against end to avoid wrapping 32-bit xa_index to 0 (and to handle these additional cases, seek_data or not, it's easier to break the loop than goto: so rearrange exit from the function). Fixes: 41139aa4c3a3 ("mm/filemap: add mapping_seek_hole_data") Signed-off-by: Hugh Dickins --- mm/filemap.c | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) --- 5.12-rc8/mm/filemap.c 2021-02-26 19:42:39.812156085 -0800 +++ linux/mm/filemap.c 2021-04-20 23:20:20.509464440 -0700 @@ -2671,8 +2671,8 @@ unsigned int seek_page_size(struct xa_st loff_t mapping_seek_hole_data(struct address_space *mapping, loff_t start, loff_t end, int whence) { - XA_STATE(xas, &mapping->i_pages, start >> PAGE_SHIFT); - pgoff_t max = (end - 1) / PAGE_SIZE; + XA_STATE(xas, &mapping->i_pages, (u64)start >> PAGE_SHIFT); + pgoff_t max = (u64)(end - 1) >> PAGE_SHIFT; bool seek_data = (whence == SEEK_DATA); struct page *page; @@ -2681,7 +2681,8 @@ loff_t mapping_seek_hole_data(struct add rcu_read_lock(); while ((page = find_get_entry(&xas, max, XA_PRESENT))) { - loff_t pos = xas.xa_index * PAGE_SIZE; + loff_t pos = (u64)xas.xa_index << PAGE_SHIFT; + unsigned int seek_size; if (start < pos) { if (!seek_data) @@ -2689,25 +2690,25 @@ loff_t mapping_seek_hole_data(struct add start = pos; } - pos += seek_page_size(&xas, page); + seek_size = seek_page_size(&xas, page); + pos = round_up((u64)pos + 1, seek_size); start = page_seek_hole_data(&xas, mapping, page, start, pos, seek_data); if (start < pos) goto unlock; + if (start >= end) + break; + if (seek_size > PAGE_SIZE) + xas_set(&xas, (u64)pos >> PAGE_SHIFT); if (!xa_is_value(page)) put_page(page); } - rcu_read_unlock(); - if (seek_data) - return -ENXIO; - goto out; - + start = -ENXIO; unlock: rcu_read_unlock(); - if (!xa_is_value(page)) + if (page && !xa_is_value(page)) put_page(page); -out: if (start > end) return end; return start;