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 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id B27B2C433EF for ; Wed, 20 Oct 2021 23:39:04 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 38E3F610A1 for ; Wed, 20 Oct 2021 23:39:04 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 38E3F610A1 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id 52F496B006C; Wed, 20 Oct 2021 19:39:03 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4B7C8900002; Wed, 20 Oct 2021 19:39:03 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 358776B0072; Wed, 20 Oct 2021 19:39:03 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0104.hostedemail.com [216.40.44.104]) by kanga.kvack.org (Postfix) with ESMTP id 220166B006C for ; Wed, 20 Oct 2021 19:39:03 -0400 (EDT) Received: from smtpin19.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id BE11E8249980 for ; Wed, 20 Oct 2021 23:39:02 +0000 (UTC) X-FDA: 78718433724.19.F1F47FF Received: from mail-ed1-f41.google.com (mail-ed1-f41.google.com [209.85.208.41]) by imf30.hostedemail.com (Postfix) with ESMTP id D2F81E0016A1 for ; Wed, 20 Oct 2021 23:38:56 +0000 (UTC) Received: by mail-ed1-f41.google.com with SMTP id y30so764035edi.0 for ; Wed, 20 Oct 2021 16:39:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=m++dlrHIml0Wqg63117GS9AXAYHvTxZYinRAd885b3w=; b=ZWK4LF2ala6mstOiD6RlG3TD4qZEDkA2Msb/OjkpXmlOInfQPxJ7XBZ75Bt0zUII5b rjaKl5Iym86LwOdDw0fsbFCYaaDKVoRR1lnePJIBEbJmI9Lo8j1k5uHKIgiXAiKxswtJ ywnN8XEABOt9AlzFbONgvMyyYM3YMWs0gUctv0hSIVWzgfL9p1x1jsvdTTr5w6A6koTT qMCH0f3rOyqvVOXVl4YT681I0kn1cfAoinup44MF34mAvzOX75Z2yPzuw9/465I3BFGT KQI5hgSd+cixKfzylsckip37zcGsL9XLoM46lGuSymBSY5Gkbq9xwizPNVi9xsUcNWvr UxWg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=m++dlrHIml0Wqg63117GS9AXAYHvTxZYinRAd885b3w=; b=y3RTkmnXyeLXj30Zm8TmsLLRDZCnLydFU4sTr4P8FYR4A3+uaG2Zsk9/aLYk66PsY4 nkpgqPupDx+6xG9MmyUuwpdC+NFd4GQculRIiPcXvIDE4OGFVZ+y3L7vssyvd69hGLJt EZCyAxsLrDyXd9ymkRYqMc+69MJXMFNopHVEjAwOGkomlG+icYZDSOL8ZrLzpAvoW9W0 1mL20qcvII8w4hPt6yFZ/93QxknRMVo7Y0dSj6HpB2U5leqfNktrBRAlK87c+ttszM77 iU23h9V1012d4Npp/SL/EpAyA8zkxAQzlfZF3/a/en5gB2OiaNB/oegbN/Jk6EunjHWT KwFQ== X-Gm-Message-State: AOAM532vJo7RD99RXeF2RWKAhhE7E5baHhb7ohZB68rDKYEiLsiylZ92 E8R/u8BtyZVYylxv9Dkoha+5yFUyylPDEXtwJlc= X-Google-Smtp-Source: ABdhPJzjnzn+utT/TRyXjvVFzmfumtSjzkS0NZwcdCpDbCIuNTc9fq8vmpA5Vw4/ZlT3F4I/aUv/gYjj1utvNPJ0Vpc= X-Received: by 2002:a05:6402:16d2:: with SMTP id r18mr2703521edx.363.1634773140864; Wed, 20 Oct 2021 16:39:00 -0700 (PDT) MIME-Version: 1.0 References: <20210917205731.262693-1-shy828301@gmail.com> In-Reply-To: From: Yang Shi Date: Wed, 20 Oct 2021 16:38:49 -0700 Message-ID: Subject: Re: [PATCH] fs: buffer: check huge page size instead of single page for invalidatepage To: Matthew Wilcox Cc: Hugh Dickins , Song Liu , Andrew Morton , Hao Sun , Linux MM , Linux FS-devel Mailing List , Linux Kernel Mailing List , Song Liu Content-Type: text/plain; charset="UTF-8" X-Rspamd-Queue-Id: D2F81E0016A1 X-Stat-Signature: oqfiie6rdayz5q3rd5tw3mhf46t8j68z Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=ZWK4LF2a; spf=pass (imf30.hostedemail.com: domain of shy828301@gmail.com designates 209.85.208.41 as permitted sender) smtp.mailfrom=shy828301@gmail.com; dmarc=pass (policy=none) header.from=gmail.com X-Rspamd-Server: rspam04 X-HE-Tag: 1634773136-992506 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 Mon, Oct 11, 2021 at 12:57 PM Yang Shi wrote: > > On Mon, Sep 20, 2021 at 3:35 PM Yang Shi wrote: > > > > On Mon, Sep 20, 2021 at 2:50 PM Matthew Wilcox wrote: > > > > > > On Mon, Sep 20, 2021 at 02:23:41PM -0700, Yang Shi wrote: > > > > On Sun, Sep 19, 2021 at 7:41 AM Matthew Wilcox wrote: > > > > > > > > > > On Fri, Sep 17, 2021 at 05:07:03PM -0700, Yang Shi wrote: > > > > > > > The debugging showed the page passed to invalidatepage is a huge page > > > > > > > and the length is the size of huge page instead of single page due to > > > > > > > read only FS THP support. But block_invalidatepage() would throw BUG if > > > > > > > the size is greater than single page. > > > > > > > > > > Things have already gone wrong before we get to this point. See > > > > > do_dentry_open(). You aren't supposed to be able to get a writable file > > > > > descriptor on a file which has had huge pages added to the page cache > > > > > without the filesystem's knowledge. That's the problem that needs to > > > > > be fixed. > > > > > > > > I don't quite understand your point here. Do you mean do_dentry_open() > > > > should fail for such cases instead of truncating the page cache? > > > > > > No, do_dentry_open() should have truncated the page cache when it was > > > called and found that there were THPs in the cache. Then khugepaged > > > should see that someone has the file open for write and decline to create > > > new THPs. So it shouldn't be possible to get here with THPs in the cache. > > > > I think Hugh's skipping special file patch > (https://lore.kernel.org/linux-mm/a07564a3-b2fc-9ffe-3ace-3f276075ea5c@google.com/) > could fix this specific BUG report and seems like a more proper fix to > this. > > However, it still doesn't make too much sense to have thp_size passed > to do_invalidatepage(), then have PAGE_SIZE hardcoded in a BUG > assertion IMHO. So it seems this patch is still useful because > block_invalidatepage() is called by a few filesystems as well, for > example, ext4. Or I'm wondering whether we should call > do_invalidatepage() for each subpage of THP in truncate_cleanup_page() > since private is for each subpage IIUC. Seems no interest? Anyway the more I was staring at the code the more I thought calling do_invalidatepage() for each subpage made more sense. So, something like the below makes sense? diff --git a/mm/truncate.c b/mm/truncate.c index 714eaf19821d..9048f498cd02 100644 --- a/mm/truncate.c +++ b/mm/truncate.c @@ -169,11 +169,16 @@ void do_invalidatepage(struct page *page, unsigned int offset, */ static void truncate_cleanup_page(struct page *page) { + int nr = thp_nr_pages(page); + int i; + if (page_mapped(page)) unmap_mapping_page(page); - if (page_has_private(page)) - do_invalidatepage(page, 0, thp_size(page)); + for (i = 0; i < nr; i++) { + if (page_has_private(page + i)) + do_invalidatepage(page + i, 0, PAGE_SIZE); + } /* * Some filesystems seem to re-dirty the page even after > > > AFAICT, it does so. > > > > In do_dentry_open(): > > /* > > * XXX: Huge page cache doesn't support writing yet. Drop all page > > * cache for this file before processing writes. > > */ > > if (f->f_mode & FMODE_WRITE) { > > /* > > * Paired with smp_mb() in collapse_file() to ensure nr_thps > > * is up to date and the update to i_writecount by > > * get_write_access() is visible. Ensures subsequent insertion > > * of THPs into the page cache will fail. > > */ > > smp_mb(); > > if (filemap_nr_thps(inode->i_mapping)) > > truncate_pagecache(inode, 0); > > } > > > > > > In khugepaged: > > filemap_nr_thps_inc(mapping); > > /* > > * Paired with smp_mb() in do_dentry_open() to ensure > > * i_writecount is up to date and the update to nr_thps is > > * visible. Ensures the page cache will be truncated if the > > * file is opened writable. > > */ > > smp_mb(); > > if (inode_is_open_for_write(mapping->host)) { > > result = SCAN_FAIL; > > __mod_lruvec_page_state(new_page, NR_FILE_THPS, -nr); > > filemap_nr_thps_dec(mapping); > > goto xa_locked; > > } > > > > But I'm not quite sure if there is any race condition.