Received: by 2002:a05:6a10:a841:0:0:0:0 with SMTP id d1csp799848pxy; Thu, 22 Apr 2021 13:49:59 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw1YAHXAdD70jBt/I0oGEeeflg7W5x9r0xVSfWsOTYHNjgrxO4DHPrDn/m16rYZGmbCq4ZA X-Received: by 2002:a17:902:e8d5:b029:e6:cabb:d07 with SMTP id v21-20020a170902e8d5b02900e6cabb0d07mr365313plg.3.1619124599004; Thu, 22 Apr 2021 13:49:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1619124598; cv=none; d=google.com; s=arc-20160816; b=qEyEJPguZ2EAboVcAn+0uT91oWc9XdceePk7BiHlHUgHYtmvmwbfEDxJ3Hv5rSRsTc RiAjI0106CmG0Lux79FzAU+DAfG2BbWs5Onqzb9wFVSn8/yU+KEkjXkPfyz0B2lUBqmX cnLq8Xd/D26rXRbyid/ECrtZMck51jNzc/HJ0BtVR0GPmyP15n7oxxYsM4GIzDOKN95G 8W47Eqni1lZWtBzSBSMS3bSnSlMQN3iskQJayqA+11vHzbFBc7ysxEBodX2GalNYjNsL ikOMxVoU5eqJ+JiQnyQlUypQHMAwvN43siacOTqjvDG+gPHm/G+xr0CY7MO12XxVCB9A A2cA== 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=bR2kbdfapTwBu00MYDhOgJUb5mkcImnnOhrX139DRgA=; b=BafqKVIVaJDR6P1yqTCl6wypRyV5ba0s8PsKethhdwNCuKYqVXWSG3eLPkX26Xw3EF vODhiKe8hmt+Ps+EXqX9/4VeJrPTNVDO693CkBlVjVpHp23YYVMMfi0Fu3RO06B1Rj/j Nf6WL+YZztuIIaM5P4U36dF8Ghef4Jyk7MagQaPf5MiRxHbelXwF6HN2e3kiCN34vx8j N0lwBiCGa0n26jIOMv1ostnzq/uCRhe0jf3k4KPmHF+UbURwb2MA2Dhhan1vc1N6adcZ 45q0iMoVxz19/JSdunID/4YNBSQ00CW+hAx0S2RhfbDSnKR9ZAFE+BXAEsQXxRlb4k9y gdoA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=Kjz3sRR9; 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 k14si5247128pgg.55.2021.04.22.13.49.46; Thu, 22 Apr 2021 13:49:58 -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=Kjz3sRR9; 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 S239521AbhDVUtg (ORCPT + 99 others); Thu, 22 Apr 2021 16:49:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36304 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239499AbhDVUtf (ORCPT ); Thu, 22 Apr 2021 16:49:35 -0400 Received: from mail-pj1-x1029.google.com (mail-pj1-x1029.google.com [IPv6:2607:f8b0:4864:20::1029]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 28457C061756 for ; Thu, 22 Apr 2021 13:49:00 -0700 (PDT) Received: by mail-pj1-x1029.google.com with SMTP id s14so17133440pjl.5 for ; Thu, 22 Apr 2021 13:49:00 -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=bR2kbdfapTwBu00MYDhOgJUb5mkcImnnOhrX139DRgA=; b=Kjz3sRR9igPIoYPoWrLfO3ghVY5woWH5jeiWDkZTtktCSeGesdMHB4wwilHwZAiL+g NEOGC1nVRCyNGOEUCMTkkp/KgY6WCTcd8eZa5BhXi28lFE21x2LMBXnbo4TJPTj+QGwD BA2ZaaEeRYglXew+Xo3PKxxSguRsJdnz5vZToN0MOjjW6+sFIop2oNER9IJKDVLoEJQd UrRIEY2D/2/Dpg4tS0nWYFlwJ9466BErCGCOxH6zlUS5V1HT9lNRvbpjIHBdIuNd0gMp uCkgny/HndIt02p9Oz6hXevhVroWCID6XJAbcObLYzd38ber7ogc6L+DkhhJ27I1+OI1 4cTw== 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=bR2kbdfapTwBu00MYDhOgJUb5mkcImnnOhrX139DRgA=; b=mqaZDFtb4dFewt2Oz9bGT6cp0D5yCrBgB1WTmIs132s3/J/qDPJ+nxoUvoAkCeQI37 GjpV/Xu93Xv715S4mZ5U944lSu0QPwfHOYduhxEaxEnUIUzzIQvZW+IEI2iuO5vIEbE4 LlfknWESUQAG1YCZfGlZAkuQnziUoUuZ8PLW9Rv9vGMgjaZM+oBeNixaQrDr4RKi8jWZ eV5EOvgbCez5Inyr5VYO/EVjiCyv14HrYAjPbZ+EP8M9TkGtLxanZBo7TZBTwLxC7njI XkPd3xr1j9nxPbk7wm+9LlLSzqtEahVIgELrnDKwk6pBCqi5WSLTal5KwZSKtAJO8S1C cblQ== X-Gm-Message-State: AOAM531kCqV5qvBY/W4ZiNFnJ4Nt2ZEk5smyYO9dvmljR5kOYIgksGKo KD8N7W8w4D/sHeAHEN/l340Shg== X-Received: by 2002:a17:90b:1646:: with SMTP id il6mr1971668pjb.27.1619124539456; Thu, 22 Apr 2021 13:48:59 -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 h18sm2482627pfv.158.2021.04.22.13.48.58 (version=TLS1 cipher=ECDHE-ECDSA-AES128-SHA bits=128/128); Thu, 22 Apr 2021 13:48:59 -0700 (PDT) Date: Thu, 22 Apr 2021 13:48:57 -0700 (PDT) From: Hugh Dickins X-X-Sender: hugh@eggly.anvils To: Andrew Morton cc: Matthew Wilcox , Hugh Dickins , 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 v2 2/2] mm/filemap: fix mapping_seek_hole_data on THP & 32-bit In-Reply-To: Message-ID: References: <20210422011631.GL3596236@casper.infradead.org> 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 cast to stop losing bits when converting unsigned long to loff_t (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. (This use of round_up() assumes that any THP has THP-aligned index: true at present and true going forward, but could be recoded to avoid the assumption.) 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 --- v2: Removed all but one of v1's u64 casts, as suggested my Matthew. Updated commit message on u64 cast and THP alignment, per Matthew. Andrew, I'd have just sent a -fix.patch to remove the unnecessary u64s, but need to reword the commit message: so please replace yesterday's mm-filemap-fix-mapping_seek_hole_data-on-thp-32-bit.patch by this one - thanks. mm/filemap.c | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) --- 5.12-rc8/mm/filemap.c 2021-02-26 19:42:39.812156085 -0800 +++ linux/mm/filemap.c 2021-04-21 22:58:03.699655576 -0700 @@ -2672,7 +2672,7 @@ loff_t mapping_seek_hole_data(struct add loff_t end, int whence) { XA_STATE(xas, &mapping->i_pages, start >> PAGE_SHIFT); - pgoff_t max = (end - 1) / PAGE_SIZE; + pgoff_t max = (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(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, 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;