From patchwork Thu Apr 22 20:48:57 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hugh Dickins X-Patchwork-Id: 12219181 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 C622EC433ED for ; Thu, 22 Apr 2021 20:49:02 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 4558E61437 for ; Thu, 22 Apr 2021 20:49:02 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 4558E61437 Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id BC9828E0005; Thu, 22 Apr 2021 16:49:01 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B7A578E0001; Thu, 22 Apr 2021 16:49:01 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A1A7D8E0005; Thu, 22 Apr 2021 16:49:01 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0035.hostedemail.com [216.40.44.35]) by kanga.kvack.org (Postfix) with ESMTP id 803C98E0001 for ; Thu, 22 Apr 2021 16:49:01 -0400 (EDT) Received: from smtpin32.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 3BAC562DD for ; Thu, 22 Apr 2021 20:49:01 +0000 (UTC) X-FDA: 78061192482.32.F6C6A35 Received: from mail-pj1-f53.google.com (mail-pj1-f53.google.com [209.85.216.53]) by imf12.hostedemail.com (Postfix) with ESMTP id 1C4DCE4 for ; Thu, 22 Apr 2021 20:48:51 +0000 (UTC) Received: by mail-pj1-f53.google.com with SMTP id e8-20020a17090a7288b029014e51f5a6baso22916pjg.2 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=jSLJ+gSDuhLlNuJp0cKkN4SllYvqcMWpG4jZPPUanIMt/G5k34/8Fg+Kqt1RFYP0bb WAC5toNM+8Tz308A2f5DIqHw/lDYFcaALkErMqk1KYL/150ux7zYtDbXUMDCM4qN5MsJ BDbMw9z6phAD+KCxItZ3FUK3Ml/z/00Hbp0KFAARq1iCRnjTir7IeoS3i1ZqFdHkhqZU 7+Hh8fLlwPodU0m25YLEZaygenUMuPPacimytvFHXbyYgho+/GFojlPn4qi/Et2qZv+V ES/H4AzRwCn0VBU8CDPx424E5Y65gVhmhHp9jMipKfSOZEK3p1Yl3yzMwnFDsCkY0hxX IpRw== X-Gm-Message-State: AOAM531w0Z3sw75Pf/AxBRw/ni7w2F6bGocuC8G/K1fgy2boSabetuZZ UcOUdaGCM/mOl2YPGFu067OjRA== X-Google-Smtp-Source: ABdhPJwi7i9LEeLrIgM50j5BAu+Matuql9UhJCD0KpBS6GPffrqTP8NgOGWa1YRFMCOqTGUlOH1nwQ== 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 X-Rspamd-Queue-Id: 1C4DCE4 X-Stat-Signature: 5ozpt4daph3rf8i8i4e1noabjnw17jzi X-Rspamd-Server: rspam02 Received-SPF: none (google.com>: No applicable sender policy available) receiver=imf12; identity=mailfrom; envelope-from=""; helo=mail-pj1-f53.google.com; client-ip=209.85.216.53 X-HE-DKIM-Result: pass/pass X-HE-Tag: 1619124531-62868 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: 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;