Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp4048223pxj; Tue, 8 Jun 2021 05:22:54 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxiviEXHN/039k88wxHMwywC9LBK3vEDT72gLcTEkJ9SZ0wv3DSo1DqftMv8DcJTXs5o1DZ X-Received: by 2002:a50:ff15:: with SMTP id a21mr25055498edu.103.1623154974038; Tue, 08 Jun 2021 05:22:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1623154974; cv=none; d=google.com; s=arc-20160816; b=N/YdlmuUG9f+e/iMXPB0Bh+aHiqyG4f8e82ZEE7xfbM0ShD4rM7laIEchVP58qQdkO lDmkeKnYbV+frmySNjSszqhmhCE5gzkcBLUXIBR5GWrgucBxbmBkowvMmii5CNMOVF4g r8rt9mnrENX8TW4kUwK1qZ/QN50ns3OZR+UYbUkv6gPtXOjlVsdJQZ7/HL0rsYVPMBp8 7pUEASzmKfr/rIb7gpWDwrEEuSCM/JhLjvWaK36gTjpOHmY7n6n12j23t1ex/iEJucl8 4sFrtNJSAsAPlo6Rgr9Hat8UtihxEl0WPnsKunts7lcJkRsMAbZge5t9gRUixf0SZezv ChiQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature:dkim-signature; bh=gX+FqIGbLzH7Df0aDpilgfPqa5jp+2x0O0Dq2GScA/w=; b=ViYDqyCpkKJfSPc+6x43n6p4hX9CmaZbEdKe8Mi3ktR2mBb/aun8BO+ZR3Dku143+N jrI0+BKWVa1gEvrhFRjCHtHgGwW8jYkaea/PnMzADOpmUbfjvYYjpl8b5jbDse6b51LX O1y9/JXjS/OwiECNH2uX1S/nlHaKH0+RKfLe8A6ntJsUAcZoQMSIcwN1diu5a0c/M1qs DC6DKPoyxw/YuBnai3j9JJocr96+J/LsSBOUYCNi9+Rw/LOiJD8lYIykPBElrIZoSL6J 22vgCdOrEN3VQ0WQ6EQkF7qXmDWqNZ8//S6vQTeoBnnlC29E3hQQWcBnGIUgaqC5YnWO ae0Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.cz header.s=susede2_rsa header.b=KmRVHQ5+; dkim=neutral (no key) header.i=@suse.cz header.s=susede2_ed25519 header.b=j7Q267F2; spf=pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id lr17si14048166ejb.104.2021.06.08.05.22.27; Tue, 08 Jun 2021 05:22:54 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-ext4-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=@suse.cz header.s=susede2_rsa header.b=KmRVHQ5+; dkim=neutral (no key) header.i=@suse.cz header.s=susede2_ed25519 header.b=j7Q267F2; spf=pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232431AbhFHMVK (ORCPT + 99 others); Tue, 8 Jun 2021 08:21:10 -0400 Received: from smtp-out2.suse.de ([195.135.220.29]:40962 "EHLO smtp-out2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232131AbhFHMVJ (ORCPT ); Tue, 8 Jun 2021 08:21:09 -0400 Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out2.suse.de (Postfix) with ESMTP id 8DB101FD46; Tue, 8 Jun 2021 12:19:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1623154755; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=gX+FqIGbLzH7Df0aDpilgfPqa5jp+2x0O0Dq2GScA/w=; b=KmRVHQ5+eR/D68RNgyB5xqEUQDaqcQqU3/DEnGMciUAKR23nxPjvOV8jc9mUj+j4mpbKkR Lv+ZgNiPi3HkId2Fw9ZdzoixgegRoPUsKgKc6PqFoywzWqoPqQ05G/Vtr2p/bP/v7A9DUJ iAW7WgczZtH9yytwp+giynzFLBLau4U= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1623154755; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=gX+FqIGbLzH7Df0aDpilgfPqa5jp+2x0O0Dq2GScA/w=; b=j7Q267F20vgcAERTkfcT3mAjx5Fvz3Q43MRpV441Cp/5gRV1if/+naEY6l28Qaymueg/Ex tPMoFbA1xdeL5vDA== Received: from quack2.suse.cz (unknown [10.100.200.198]) by relay2.suse.de (Postfix) with ESMTP id 4ABA8A3B84; Tue, 8 Jun 2021 12:19:15 +0000 (UTC) Received: by quack2.suse.cz (Postfix, from userid 1000) id 2C36B1F2C94; Tue, 8 Jun 2021 14:19:15 +0200 (CEST) Date: Tue, 8 Jun 2021 14:19:15 +0200 From: Jan Kara To: "Darrick J. Wong" Cc: Jan Kara , linux-fsdevel@vger.kernel.org, Christoph Hellwig , Dave Chinner , ceph-devel@vger.kernel.org, Chao Yu , Damien Le Moal , "Darrick J. Wong" , Jaegeuk Kim , Jeff Layton , Johannes Thumshirn , linux-cifs@vger.kernel.org, linux-ext4@vger.kernel.org, linux-f2fs-devel@lists.sourceforge.net, linux-mm@kvack.org, linux-xfs@vger.kernel.org, Miklos Szeredi , Steve French , Ted Tso , Matthew Wilcox Subject: Re: [PATCH 03/14] mm: Protect operations adding pages to page cache with invalidate_lock Message-ID: <20210608121915.GG5562@quack2.suse.cz> References: <20210607144631.8717-1-jack@suse.cz> <20210607145236.31852-3-jack@suse.cz> <20210607160922.GA2945763@locust> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210607160922.GA2945763@locust> User-Agent: Mutt/1.10.1 (2018-07-13) Precedence: bulk List-ID: X-Mailing-List: linux-ext4@vger.kernel.org On Mon 07-06-21 09:09:22, Darrick J. Wong wrote: > On Mon, Jun 07, 2021 at 04:52:13PM +0200, Jan Kara wrote: > > Currently, serializing operations such as page fault, read, or readahead > > against hole punching is rather difficult. The basic race scheme is > > like: > > > > fallocate(FALLOC_FL_PUNCH_HOLE) read / fault / .. > > truncate_inode_pages_range() > > > cache here> > > > > > > Now the problem is in this way read / page fault / readahead can > > instantiate pages in page cache with potentially stale data (if blocks > > get quickly reused). Avoiding this race is not simple - page locks do > > not work because we want to make sure there are *no* pages in given > > range. inode->i_rwsem does not work because page fault happens under > > mmap_sem which ranks below inode->i_rwsem. Also using it for reads makes > > the performance for mixed read-write workloads suffer. > > > > So create a new rw_semaphore in the address_space - invalidate_lock - > > that protects adding of pages to page cache for page faults / reads / > > readahead. > > > > Signed-off-by: Jan Kara ... > > +->fallocate implementation must be really careful to maintain page cache > > +consistency when punching holes or performing other operations that invalidate > > +page cache contents. Usually the filesystem needs to call > > +truncate_inode_pages_range() to invalidate relevant range of the page cache. > > +However the filesystem usually also needs to update its internal (and on disk) > > +view of file offset -> disk block mapping. Until this update is finished, the > > +filesystem needs to block page faults and reads from reloading now-stale page > > +cache contents from the disk. VFS provides mapping->invalidate_lock for this > > +and acquires it in shared mode in paths loading pages from disk > > +(filemap_fault(), filemap_read(), readahead paths). The filesystem is > > +responsible for taking this lock in its fallocate implementation and generally > > +whenever the page cache contents needs to be invalidated because a block is > > +moving from under a page. > > Having a page cache invalidation lock isn't optional anymore, so I think > these last two sentences could be condensed: > > "...from reloading now-stale page cache contents from disk. Since VFS > acquires mapping->invalidate_lock in shared mode when loading pages from > disk (filemap_fault(), filemap_read(), readahead), the fallocate > implementation must take the invalidate_lock to prevent reloading." > > > + > > +->copy_file_range and ->remap_file_range implementations need to serialize > > +against modifications of file data while the operation is running. For > > +blocking changes through write(2) and similar operations inode->i_rwsem can be > > +used. For blocking changes through memory mapping, the filesystem can use > > +mapping->invalidate_lock provided it also acquires it in its ->page_mkwrite > > +implementation. > > Following the same line of reasoning, if taking the invalidate_lock is > no longer optional, then the conditional language in this last sentence > is incorrect. How about: > > "To block changes to file contents via a memory mapping during the > operation, the filesystem must take mapping->invalidate_lock to > coordinate with ->page_mkwrite." > > The code changes look fine to me, though I'm no mm expert. ;) OK, I've updated the documentation as you suggested. Thanks for review. Honza -- Jan Kara SUSE Labs, CR