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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id E53AEC021B2 for ; Sun, 23 Feb 2025 01:47:59 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 763C6280002; Sat, 22 Feb 2025 20:47:59 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 7136A280001; Sat, 22 Feb 2025 20:47:59 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5DB02280002; Sat, 22 Feb 2025 20:47:59 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 41080280001 for ; Sat, 22 Feb 2025 20:47:59 -0500 (EST) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id B89D1C1DEE for ; Sun, 23 Feb 2025 01:47:58 +0000 (UTC) X-FDA: 83149523436.19.E2D615E Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) by imf03.hostedemail.com (Postfix) with ESMTP id 8BAD620004 for ; Sun, 23 Feb 2025 01:47:56 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b=c+7Y3LOQ; dmarc=none; spf=none (imf03.hostedemail.com: domain of willy@infradead.org has no SPF policy when checking 90.155.50.34) smtp.mailfrom=willy@infradead.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1740275277; a=rsa-sha256; cv=none; b=5e30U+7Guv+rylBeWBOcWWj07t0MTOI1SRnzKJUhTG7kWE/AWVGc3d12jP6qQhel3btAB4 HeZXaa8Q3HF5BIzseR34I5pBV/BtmhMLo6j4dh8f97YFZP9RzuhhJrPP1d+Lzv95bvzz48 JRYlAfGuolJZSaEYaCZLMmtD0527Mns= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b=c+7Y3LOQ; dmarc=none; spf=none (imf03.hostedemail.com: domain of willy@infradead.org has no SPF policy when checking 90.155.50.34) smtp.mailfrom=willy@infradead.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1740275277; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=tO6tOZLCUciZBJnG/Rw817r3O8VM969b6bQPUS7fc/c=; b=KYPQOx5HTm0OWPdLNsZadHCP1ekNCG4xkCQPyhjrCH6oIfZ4prLAdZ6BJW7zZeGY5DRMq1 +XJCjye6pYfvh9NghOlXgCCqmN/z3hPPkvF8duuHWszltihLnlIfkm9WogZa6wuJJm8tuY ktcDiuXF3A/nSy+ZfHP3i2mtsnPW3YQ= 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=tO6tOZLCUciZBJnG/Rw817r3O8VM969b6bQPUS7fc/c=; b=c+7Y3LOQdt3ZEU9PdFiZ+vjJqt eTS4RgngV2dHy+DpdpzHSqYWw7dWcqSEZmk3Q1Xv3PsaGleg1LKCab0iQ1Vu3oZDppCc4CMHoL7MY bwWrm3dLFxF+KiapF5WEWN63f1luIiR6ytmNgonmS1IFv/VrcrcA7qKoHqGRj+/60DSG10KR+bRbP yqPC+lgRPvBjyXYVxKCipqGE4AeRXps2kufsq03QEU4IGiOecCtCsfLRO896XLCi5kTX041ukCznR iguT0g+go7fC3LBzEyAFpCLRLx1uGvRzJYZ/604s2iBb5WFJD0nuDA6SkqcA1Aa0PFGdYKC7r0Xvc 2XbM5w0Q==; Received: from willy by casper.infradead.org with local (Exim 4.98 #2 (Red Hat Linux)) id 1tm161-00000001vcW-0Mqa; Sun, 23 Feb 2025 01:47:53 +0000 Date: Sun, 23 Feb 2025 01:47:52 +0000 From: Matthew Wilcox To: David Frank Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: Re: Efficient mapping of sparse file holes to zero-pages Message-ID: References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Rspam-User: X-Rspamd-Queue-Id: 8BAD620004 X-Rspamd-Server: rspam12 X-Stat-Signature: 97yrypd67btetczuo5w6i89zny4b78d6 X-HE-Tag: 1740275276-433066 X-HE-Meta: U2FsdGVkX18hL1+TaNElin6GU15GaaN3eCcnUFS0UKsZyGycGzXWngqcp4acQmeNPkI59Dxsv3DwM37O5ENUecF8b0sLixSWHM+0KRkAR8+xw90qUh1T0vA8EVRzhXsmcmCl1TfB03Lc+K5yO4a7+kRb1RfktfW4mo0qPyULkHe5AZ6kYLsvcC5F/Iz+Y6oFPPV1Jo4xtW+2vYaS1AF2zb0v6qtahNf4mdYXAG7OUsPLAwb+JBi81YJarZoczUI0ugGESZq89zTDQmiuscyk3tCEX+mIU6Ad7JT1OBjsOlAomck57zhxIMKU87PdTj0kaSoYp+58+jZxhEWFa++6vlQHh4Fd1DZMju9dBpDjIWaBGq5vKIpww4TH3C4FBigcdK8k0SD3QvCo9UdwTFFEmzRVMrIPyjtZTutZGhPCh9ClrFjdCOeHqwkReVN2XClrFQd4ixZOgZpeIlfIz6hsLE3xGIUp2e0n/t8zHATivbhd0qe386NL8Wd+zzLNLCGP0hYCbKSwkOPMv4WGcAkSQ5bK6adUPQvBET8077gNC96UifbRnDLbigf7RRvMJm6mYFTXRXtFTLbKr7H5eDLOKKjG7Vp6VzkpSE55w9jFEjy+gBjx1aRKe/6RLLcJW+2gL0j3d3C9JM3we8MeYpX+LV1RcfZ1w2V4pncO1fVNXDUh+nGeqmKUqUHPYKmrQBhJ+FoSxfk43bc5XF4d+5hAGlzv/PNeGenb1caGQihjOYAE/Ja0sYKEfV81NDXq7LQ2XCo7+DEfcNDvRniJJhug7EJ+o4gFWL78DKpkjuSvHHd7hyeJP6QSD+KnAc44PxyWuT2Dg/KGJsO08VXjknr5TetKAEi4YHA5Y8PXjQbYsyyKFFRj4+l8fT0pyG3yH085xXVrcvZMyB4C1RaYg0rfapoHLg+LRvuIg+HWeqxAnPvroX0Pfs6oVgyImpSpysmTcL2DQDNxD5kXsrQEP/1 dhtflwSG GAZIYu3nZRmznBDkblhHf9ZGv4pchSVdxvDQUxhn0IGMGRJ+4DUUtHMBCnleLULl4rbmW3F/p+WglMDZ/ZRKkGmZe+ZwJr59+aQyn1st9Fyim1ZSNuP/fVKeeR82aXrXycHiRuP0mFRk/vRs4bejpbpoCPOMofdWQ0LNfYcZn40lCXUqpId7RFGHTVWMnlWBhY3PCE4Hk+bXZhKYEoZtp7apYksBpt09FisDAPEFI6DscDwTxufSc6I6mrqaKdTFuZAGRscOXRhQt8deoU9UBsvu7fOuH77xp1T7mNHhJL9So8HyfIxHN5TGioQ== 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: List-Subscribe: List-Unsubscribe: On Thu, Feb 20, 2025 at 09:46:08PM +0100, David Frank wrote: > Thank you, Matthew, for your reply. > > What do you think about the complexity of this task? I'd be interested > in taking a look but I don't have kernel development experience so I > would need guidance. Unfortunately, I would say this is a high complexity task. At a high level, I think we'd need: - Choose a data structure in the VFS to store this range information (a tree of some kind) - Design a protocol such that the VFS can query this information about a range of a particular file, and the filesystem can invalidate the VFS's knowledge - Use that range information when performing readahead [1] - Put zero entries into the page cache - Handle retrieving zero entries appropriately at all the points which currently retrieve folios from the page cache - Handle tearing down mmaps of zero entries when written to Probably a few other things, but that's about the size of it. I started hinting at a way to do the second point, and it was not well-received. https://lore.kernel.org/linux-fsdevel/Ytcd2a0RVCccWOmC@casper.infradead.org/ got no responses https://lore.kernel.org/linux-fsdevel/Zs97qHI-wA1a53Mm@casper.infradead.org/ got a lot of push-back. I consider most of the responses on that thread to be from people who understand the problems far better than I do, so I'd need to learn a lot more before making another proposal. [1] Little secret, almost all reads / page faults are handled by readahead > On Thu, 20 Feb 2025 at 14:47, Matthew Wilcox wrote: > > > > On Thu, Feb 20, 2025 at 01:48:18PM +0100, David Frank wrote: > > > I'd like to efficiently mmap a large sparse file (ext4), 95% of which > > > is holes. I was unsatisfied with the performance and after profiling, > > > I found that most of the time is spent in filemap_add_folio and > > > filemap_alloc_folio - much more than in my algorithm: > > > > > > - 97.87% filemap_fault > > > - 97.57% do_sync_mmap_readahead > > > - page_cache_ra_order > > > - 97.28% page_cache_ra_unbounded > > > - 40.80% filemap_add_folio > > > + 21.93% __filemap_add_folio > > > + 8.88% folio_add_lru > > > + 7.56% workingset_refault > > > + 28.73% filemap_alloc_folio > > > + 22.34% read_pages > > > + 3.29% xa_load > > > > Yes, this is expected. > > > > The fundamental problem is that we don't have the sparseness information > > at the right point. So the read request (or pagefault) comes in, the > > VFS allocates a page, puts it in the pagecache, then asks the filesystem > > to fill it. The filesystem knows, so could theoretically tell the VFS > > "Oh, this is a hole", but by this point the "damage" is done -- the page > > has been allocated and added to the page cache. > > > > Of course, this is a soluble problem. The VFS could ask the filesystem > > for its sparseness information (as you do in userspace), but unlike your > > particular usecase, the kernel must handle attackers who are trying to > > make it do the wrong thing as well as ill-timed writes. So the VFS has > > to ensure it does not use stale data from the filesystem. > > > > This is a problem I'm somewhat interested in solving, but I'm a bit > > busy with folios right now. And once that project is done, improving > > the page cache for reflinked files is next on my list, so I'm not likely > > to get to this problem for a few years. > >