Received: by 2002:a05:6a10:a841:0:0:0:0 with SMTP id d1csp61028pxy; Wed, 21 Apr 2021 18:31:59 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzg1vnc9c28V9Nez+LKdl9KuhTYbMW56mpQbuuZu3IED9lJqAHRfgnooCtvTpxfxhaUcW15 X-Received: by 2002:a17:907:1c01:: with SMTP id nc1mr671989ejc.283.1619055119553; Wed, 21 Apr 2021 18:31:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1619055119; cv=none; d=google.com; s=arc-20160816; b=SApUmKQmqkds/T01FS3pJWdZM7vEu/RjmJK7Bg9WQ6Dl5+fsRVvROqGX0tPS6eULsN aELy64+NPNPulLimI7fmeezR3ZBoMG1Fu+BYOz2F+fSo8P8jbIUfx3LC/xYFwp7AyCjj Oi/a5cBTMRMov4ie9GKD46E8r/MJMNO5IZ80ICV5QzmbIUrwWnVOEBHClZuZZQYVdWtI HWOrZN4GDlrbf/mTjUFm+t6kyxzj87MbhqmHoJR272XgXLX/EMqglr3riDwhPXrBjzdf qLCsVfjNrUV/bfdfxaCgWJjlMgyd7+dn+M1CRxsKA3Ec7WuDPtQTOdaBtvZpGJ20N3Hh U3wg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:user-agent:references:message-id :in-reply-to:subject:cc:to:from:date:dkim-signature; bh=7i3jgTFMgSvDDCvaM/z3dZrqsQ7aoX20W1+zrU4bC9A=; b=k7OBmkQvYZAorC0gvRmjXCUFIA+n81QF+kU+IeMbYowUUMsLxe+iGpMcxDV323VTK7 0GphcNMw2FfhsV0wt3rWSbDi0pxqjcNgqYnGammYlKPoriKUIHtAgm0eDX1CB45wTrft ohW6dnZhfGKIogqY4XFp7pstVDoqdIfMZe8cj1/zu0JE1tB/bRqQFCI0w8CuKcfdm9sk NhT8v3/8+M6r0PQdcHa4S+VGtHaNYlfpoPBxpMebmWYqHw4kfGxtjWvMS6s0FXb9LAzS +qr0ftXVIqLMJwHwW4BMkDD1eDJFyN49xPZ1Vl2WC2dBL8ql9f3xSru5KqRacZBLlwIT uQ1A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=cYmtc9W0; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id h9si834470ejb.384.2021.04.21.18.31.36; Wed, 21 Apr 2021 18:31:59 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=cYmtc9W0; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344046AbhDVAkm (ORCPT + 99 others); Wed, 21 Apr 2021 20:40:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51130 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344047AbhDVAjw (ORCPT ); Wed, 21 Apr 2021 20:39:52 -0400 Received: from mail-qt1-x836.google.com (mail-qt1-x836.google.com [IPv6:2607:f8b0:4864:20::836]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EF43FC06138A for ; Wed, 21 Apr 2021 17:39:17 -0700 (PDT) Received: by mail-qt1-x836.google.com with SMTP id m16so31999895qtx.9 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=Q5MHmjSABQd8h1Eoi/AK+vsJXzyvK87A0l1M8iVwbka9knw/KnawsRFy4SP0Ax2v3/ yxpwARl5DcjfmZlc31H5kfkHsoMEpuNmpfn8w47c8kylEv8T6ykt0q0Xb0kNmkD3Sib5 Sl7d3S8smzItMcOLtNRDQ+jJK950c7usv19+61ac0Ewk/JEcxqIVw2RB/3RIqpzCkQOS RmdKaHxN8C3OLcf1R27oNxCeoGmDS23p46E3+RZGOR6txRhdJ0egeS3f2P7Za9DwOMrX cK7t6iXs4YC4ZNqn6t/kJ/IAkGIw2hSMy4kHhkF7HKRIQ/KMqvrhvj9Xzmsxz1VQGbJD C3Vg== X-Gm-Message-State: AOAM5300JPZYyCztMX2nwVm8AVGLpyezA7ZPdPRUVXvFwEPxXQPRLQRs qKuSu88mwp9ck6SgPcyH/dCgsw== 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 Content-Type: TEXT/PLAIN; charset=US-ASCII Precedence: bulk List-ID: X-Mailing-List: linux-kernel@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;