From mboxrd@z Thu Jan 1 00:00:00 1970 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=-3.5 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS autolearn=no 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 BC33FC433E6 for ; Fri, 28 Aug 2020 17:31:28 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 3E3802074A for ; Fri, 28 Aug 2020 17:31:28 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="BW0+BBNA" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 3E3802074A Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 4E5356B000A; Fri, 28 Aug 2020 13:31:27 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 46F8F6B000C; Fri, 28 Aug 2020 13:31:27 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 337658E0003; Fri, 28 Aug 2020 13:31:27 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0039.hostedemail.com [216.40.44.39]) by kanga.kvack.org (Postfix) with ESMTP id 19BE46B000A for ; Fri, 28 Aug 2020 13:31:27 -0400 (EDT) Received: from smtpin26.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id C54054DCB for ; Fri, 28 Aug 2020 17:31:26 +0000 (UTC) X-FDA: 77200668972.26.mouth56_4a0ccdc27077 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin26.hostedemail.com (Postfix) with ESMTP id 99E571804B661 for ; Fri, 28 Aug 2020 17:31:26 +0000 (UTC) X-HE-Tag: mouth56_4a0ccdc27077 X-Filterd-Recvd-Size: 4793 Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) by imf25.hostedemail.com (Postfix) with ESMTP for ; Fri, 28 Aug 2020 17:31:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=In-Reply-To:Content-Type:MIME-Version: References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=ezwOT7zE5exDd/5AoI86N6ZKfrsLsV+UrfNckyKy5+s=; b=BW0+BBNAAMr0tC7r7zmBG3QSvh aix0Wy9kZMqnpp+PzXRuXIfuyHHtAHSK/Cmu/wa4P65bEI5LMUDSbqDX13zD9eJkPY7gE632BcZH3 jxYg5nCXVsgiqP9IWjjTJf0kDpAjeMoN/I3yh/qcECokGRgTYcO0RkOgoO9xBQQzVATl9+zBTjBiA C2XmTi2OQ9webtLxSJ32kRskg8Vex+b3G1dhLQNChqItXNtIql72T1mJpJfJHDe0ERlmahkbRmIYF hzbqdlCYKgKUgoztnKW0G60SmRB2OS7/HkP3Bx9mDwZgY1LTmXYiioRgujQjkzA6FUO/G86pIn8t5 qt7VvfHg==; Received: from willy by casper.infradead.org with local (Exim 4.92.3 #3 (Red Hat Linux)) id 1kBiDi-0003sf-Sx; Fri, 28 Aug 2020 17:31:22 +0000 Date: Fri, 28 Aug 2020 18:31:22 +0100 From: Matthew Wilcox To: Hugh Dickins Cc: Linux MM , Yang Shi , Yang Shi Subject: Re: Is shmem page accounting wrong on split? Message-ID: <20200828173122.GP14765@casper.infradead.org> References: <20200828142546.GN14765@casper.infradead.org> <20200828145528.GO14765@casper.infradead.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Rspamd-Queue-Id: 99E571804B661 X-Spamd-Result: default: False [0.00 / 100.00] X-Rspamd-Server: rspam04 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: On Fri, Aug 28, 2020 at 10:08:52AM -0700, Hugh Dickins wrote: > On Fri, 28 Aug 2020, Yang Shi wrote: > > On Fri, Aug 28, 2020 at 7:55 AM Matthew Wilcox wrote: > > > > > > On Fri, Aug 28, 2020 at 03:25:46PM +0100, Matthew Wilcox wrote: > > > > If I understand truncate of a shmem THP correctly ... > > > > > > > > Let's suppose the file has a single 2MB page at index 0, and is being > > > > truncated down to 7 bytes in size. > > > > > > > > shmem_setattr() > > > > i_size_write(7); > > > > shmem_truncate_range(7, -1); > > > > shmem_undo_range(7, -1) > > > > start = 1; > > > > page = &head[1]; > > > > shmem_punch_compound(); > > > > split_huge_page() > > > > end = DIV_ROUND_UP(i_size_read(mapping->host), PAGE_SIZE); # == 1 > > > > __split_huge_page(..., 1, ...); > > > > __delete_from_page_cache(&head[1], ...); > > > > truncate_inode_page(page); > > > > delete_from_page_cache(page) > > > > __delete_from_page_cache(&head[1]) > > > > > > > > I think the solution is to call truncate_inode_page() from within > > > > shmem_punch_compound() if we don't call split_huge_page(). I came across > > > > this while reusing all this infrastructure for the XFS THP patchset, > > > > so I'm not in a great position to test this patch. > > It's a good observation of an oddity that I probably didn't think of, > but you haven't said which kind of shmem page accounting goes wrong here > (vm_enough_memory? df of filesystem? du of filesystem? memcg charge? > all of the above? observed in practice?), and what needs solving. > > If that page has already been deleted from page cache when splitting, > truncate_inode_page() sees NULL page->mapping != mapping and returns > without doing anything. What's the problem? Ah! I missed the check in truncate_inode_page(). This should be fine then. The problem I've observed in practice is following the same pattern in truncate_inode_pages_range(). The call to delete_from_page_cache_batch() trips the assertion that the page hasn't already been deleted from the page cache. I think the solution is obvious -- don't add the page to locked_pvec if page->mapping is NULL. if (thp_punch(page, lstart, lend)) pagevec_add(&locked_pvec, page); else unlock_page(page); } for (i = 0; i < pagevec_count(&locked_pvec); i++) truncate_cleanup_page(mapping, locked_pvec.pages[i]); delete_from_page_cache_batch(mapping, &locked_pvec); for (i = 0; i < pagevec_count(&locked_pvec); i++) unlock_page(locked_pvec.pages[i]); truncate_exceptional_pvec_entries(mapping, &pvec, indices); pagevec_release(&pvec); (shmem_punch_compound() got renamed to thp_punch() and moved to truncate.c)