Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp1782144pxb; Mon, 11 Oct 2021 13:02:31 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyzXD5UU037V76zsPiNOimjvNSWGGUMz16AEDPzClmby2KJ+uqkp56nhuHGy3DpIheSttLm X-Received: by 2002:a50:9b06:: with SMTP id o6mr44753639edi.284.1633982551522; Mon, 11 Oct 2021 13:02:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1633982551; cv=none; d=google.com; s=arc-20160816; b=dGptkO7aqoeKoyjra/+q9HLCspsZn+n54PJd7K0lPZrOddE1HFbw2KAfFcorSmrWxi vTNuNyyd8LWnyGC/LvHG9gX+khbGb7jwbGIGDdfjsHqviMOpVw8mddGrJ1GbQZQQ3NaO +OH0XQLKSeyq2Qw0JcX8c447TVDjEQMf/uLOMZZWOpuKynoZ3mxaqACCkb6PwE0rfNm2 KTjt0Dr+67MIE34NatbRCuDL4b2b3Gql0gLAkbCLrhueXtiAfJKw9tdKlMgC4vGxGKe3 RI45W0+HTvUx0Uqq6JB/Ui1JFRK93tc5YhnSMmcLe/+uEWfmKixPrh8cuj5pQjxc1WcP kAuA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=SKh3z8/eFx39DZqeLn4BLSOUQsWLVYgA6sDrH+x4Ulk=; b=trC7LQ9tpA5m6w9rtx0s0RM1srB7tLM2ls868MVYqIjun/5sL+6NBr5ctoX9ZJmSqq cE5S5yMDI+iSU+Qh9nCXXY4lem5aIlZ22st7TZayXqNoo5yxKhj3UZIbdm1ZEHjrA4ji tnMO0qtuIA+C7YEW1EDp2U0fiajB2btxUAVye4uACjuNWUuJICs6iUMLXBOB0xNiCTq/ W0ihmN2/RBkfByy33qFbGOjcBcEwCWrkc2BJn3H7w4UpCTBghoxoYmzAKrasztkjW2Ax qitvZ3rWIhhhytqRlnHi2MlgUD2Uwg/JVylxfr3ZfM9AqjgN9Z8BGOTBC4PUekCdamqV /0FQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=mbTZFkNU; 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=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id cz1si10767524edb.419.2021.10.11.13.02.02; Mon, 11 Oct 2021 13:02:31 -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=@gmail.com header.s=20210112 header.b=mbTZFkNU; 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=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233817AbhJKUAF (ORCPT + 99 others); Mon, 11 Oct 2021 16:00:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35544 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231156AbhJKUAE (ORCPT ); Mon, 11 Oct 2021 16:00:04 -0400 Received: from mail-ed1-x531.google.com (mail-ed1-x531.google.com [IPv6:2a00:1450:4864:20::531]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5C1E5C061570; Mon, 11 Oct 2021 12:58:04 -0700 (PDT) Received: by mail-ed1-x531.google.com with SMTP id g8so72056419edt.7; Mon, 11 Oct 2021 12:58:04 -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=SKh3z8/eFx39DZqeLn4BLSOUQsWLVYgA6sDrH+x4Ulk=; b=mbTZFkNUWLVTd4DmzdjidHDoed1HvzabyqoqjfbSfDSfnZRHFe4aWbFxP2vttXkLYG fgY+vmdfVg8Mb4knc/uzjZjfn95JQUhUJc5HJhCh9fN/PBs8dpnFew/8uq99xByifowb zBoI1HjWz9lcOv+6aEy9mW2W1p2/VB7839rIxbIRaOXmHbm1i2/FZ2WkzUvWFgD76T78 vWYhEa2D/8Mzg57eBzfslfMifTlrCf/5JZr9d7Fgj+UFyhA3ujRICitMDtAQmdSFaVMD DAjQ7PSre+Q5+1Lizyafz9GbRf7aeSI9wN0kfkztsb23UEChT6x/uZTDYIyay0WtQ7DR fBzQ== 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=SKh3z8/eFx39DZqeLn4BLSOUQsWLVYgA6sDrH+x4Ulk=; b=oKATgOAcA0HYmOdQYC+3hsWhPneEOTDRZJMB6Isb42kZpvRoz3wjKlYuvzx7s3tOJE l3A3sbmkUFwMU7ypz54j+BqoQyVYrhYMVQ6aMyRJtPRQophYqaY6gSYCpx4iK4uFrOmF L+2DtT5+rEGO/ukRo24eNHHK58unu8cpsYAZ1+38aQWN/4fONq1KYoWwmchXZcxHYiCc jqZKDXpCxi6GHCZ2roW8GTTIOJiZEnS3QBqyHcQzYLOGx3KpjOhYdvN6dsRa8SYwuHaf AWfrCmVbmJdWe29YI0PK3u/L77wjdmgRg3akHztHfAaARMhwftr0mAbg//gdBTIoejrj c5Uw== X-Gm-Message-State: AOAM533jPKP7l3KTiFoQxiHVus7XvKhriXOqAQWGD4fSgbgyjKqJmQUU VzeHiMG/yv+b1gd7XM/5X3DIRUWxzMifwC4W1xpXkiGBBh0= X-Received: by 2002:a17:907:6297:: with SMTP id nd23mr28983343ejc.62.1633982282937; Mon, 11 Oct 2021 12:58:02 -0700 (PDT) MIME-Version: 1.0 References: <20210917205731.262693-1-shy828301@gmail.com> In-Reply-To: From: Yang Shi Date: Mon, 11 Oct 2021 12:57:51 -0700 Message-ID: Subject: Re: [PATCH] fs: buffer: check huge page size instead of single page for invalidatepage To: Matthew Wilcox Cc: Hugh Dickins , cfijalkovich@google.com, 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" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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. > 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.