Received: by 2002:a05:6358:a55:b0:ec:fcf4:3ecf with SMTP id 21csp3220829rwb; Mon, 16 Jan 2023 05:26:41 -0800 (PST) X-Google-Smtp-Source: AMrXdXtSuWb79je5U3InCNuEb8EGn2u9iBzFMP8TcUjsblCI2DrxRblWaaVjv/fVisveedxB21Io X-Received: by 2002:a17:902:8d86:b0:194:3f40:69b7 with SMTP id v6-20020a1709028d8600b001943f4069b7mr22116928plo.63.1673875601354; Mon, 16 Jan 2023 05:26:41 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673875601; cv=none; d=google.com; s=arc-20160816; b=e1+N75TR6lLyw0g6LlCIbEMkhGrlg7X4Yc+oortahuDQdxw/8cla6g4hDlqIrnyur/ zGAmCHqvECirO+wYQUjuNpIy3X+K7IyhzGe7M92rtVtAVSEUEBd57Hs3NB/8xerMIrRi +aCxfidXuFWfQDTZMMZszvFC8XHkxh3ToMR6csifLH6n0qqlh09TM18iQE2RYZzmEIA8 sld+d9hoqELI06Iho039oiizk1he4dhaDZQXoJ7eSo0G7KYKVwSd69AfIz+1py472V7f KfwCH6BTC0ypQSuhyC2tAXEotd3aYbLXEzfVDL6RQaO51oOsuVD9TRysV739YNriBy0O dqLA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=omN11K0GI/xhx3NUwdC4C9fLf+3VpRmOQ0DYrSkQAI8=; b=bwjO90LnEyMXOfu6K3CFVc9KQwii64y4gEFeMRJwRGO33Y3ypoBikuXDcyc/npicym X0ChpZZV5XjiflqtW7iB2lMQwR3RPeOpg5yQNZh5B+UIZQmMrjT2cDurfJMcObumMrjA 8ZQLt8/oZQdnklB/hA6Ye7PTpOq4SjCZ1BXX/Nat28v75VDgUewPhdzXhyo9PKGoqDN9 5RToq7IT+5Qv//CIheHtNwUlCxkj59ho2OeHYHcqvY34+Aq7Ukp9Qv7aK/L62hPFbylk 4vpjiBzxc5b1Fb238I/tmJrxAXuEl5rUQxmHqpHZZT1urqX6bYsiJFjQAt5j5br0w0Be XQzQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=b3cUub8B; spf=pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id r1-20020a635141000000b0049f478df3b8si29367085pgl.702.2023.01.16.05.26.24; Mon, 16 Jan 2023 05:26:41 -0800 (PST) Received-SPF: pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=b3cUub8B; spf=pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231146AbjAPNTV (ORCPT + 99 others); Mon, 16 Jan 2023 08:19:21 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40012 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231663AbjAPNSj (ORCPT ); Mon, 16 Jan 2023 08:18:39 -0500 Received: from casper.infradead.org (casper.infradead.org [IPv6:2001:8b0:10b:1236::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E5E475249; Mon, 16 Jan 2023 05:18:05 -0800 (PST) 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=omN11K0GI/xhx3NUwdC4C9fLf+3VpRmOQ0DYrSkQAI8=; b=b3cUub8BzpeSKs65+jE7bP9acg X9wv+OJT9mphIB0pbumFvGRvN7B4sryhv07C8XjHqoFGU/HbJp4xWtNxWe7I6CYDx8vu3Ma8Advdf umhAFCq7+w8TlGjO3Xd9zqgKEtcG8M9ukYZaeuGtt1FneWTQnvEQijaBAODQiD/LSCSNOhON2WD0V Hu11mAApRKqpA3DeM4TJYkZZz94qDHxyx12B56CA42TtXPifErJvfA9bdgOE7t/EKowABnAwNMnMg qTpEEjI1FxLozF1Ayx4ll1iPLz7rpBo2/sWYs7+PJFo8qEOudirIlWsXdlZYeMWummt23HdhcNATl 6rxgszPw==; Received: from willy by casper.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1pHPNH-008lPF-Hk; Mon, 16 Jan 2023 13:18:07 +0000 Date: Mon, 16 Jan 2023 13:18:07 +0000 From: Matthew Wilcox To: Christoph Hellwig Cc: "Darrick J. Wong" , Andreas Gruenbacher , Dave Chinner , Alexander Viro , linux-xfs@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-ext4@vger.kernel.org, cluster-devel@redhat.com, Christoph Hellwig Subject: Re: [RFC v6 04/10] iomap: Add iomap_get_folio helper Message-ID: References: <20230108213305.GO1971568@dread.disaster.area> <20230108194034.1444764-1-agruenba@redhat.com> <20230108194034.1444764-5-agruenba@redhat.com> <20230109124642.1663842-1-agruenba@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-ext4@vger.kernel.org On Sun, Jan 15, 2023 at 11:34:26PM -0800, Christoph Hellwig wrote: > We could do that. But while reading what Darrick wrote I came up with > another idea I quite like. Just split the FGP_ENTRY handling into > a separate helper. The logic and use cases are quite different from > the normal page cache lookup, and the returning of the xarray entry > is exactly the kind of layering violation that Dave is complaining > about. So what about just splitting that use case into a separate > self contained helper? Essentially reverting 44835d20b2a0. Although we retain the merging of the lock & get functions via the use of FGP flags. Let me think about it for a day. > --- > >From b4d10f98ea57f8480c03c0b00abad6f2b7186f56 Mon Sep 17 00:00:00 2001 > From: Christoph Hellwig > Date: Mon, 16 Jan 2023 08:26:57 +0100 > Subject: mm: replace FGP_ENTRY with a new __filemap_get_folio_entry helper > > Split the xarray entry returning logic into a separate helper. This will > allow returning ERR_PTRs from __filemap_get_folio, and also isolates the > logic that needs to known about xarray internals into a separate > function. This causes some code duplication, but as most flags to > __filemap_get_folio are not applicable for the users that care about an > entry that amount is very limited. > > Signed-off-by: Christoph Hellwig > --- > include/linux/pagemap.h | 6 +++-- > mm/filemap.c | 50 ++++++++++++++++++++++++++++++++++++----- > mm/huge_memory.c | 4 ++-- > mm/shmem.c | 5 ++--- > mm/swap_state.c | 2 +- > 5 files changed, 53 insertions(+), 14 deletions(-) > > diff --git a/include/linux/pagemap.h b/include/linux/pagemap.h > index 4b3a7124c76712..e06c14b610caf2 100644 > --- a/include/linux/pagemap.h > +++ b/include/linux/pagemap.h > @@ -504,8 +504,7 @@ pgoff_t page_cache_prev_miss(struct address_space *mapping, > #define FGP_NOFS 0x00000010 > #define FGP_NOWAIT 0x00000020 > #define FGP_FOR_MMAP 0x00000040 > -#define FGP_ENTRY 0x00000080 > -#define FGP_STABLE 0x00000100 > +#define FGP_STABLE 0x00000080 > > struct folio *__filemap_get_folio(struct address_space *mapping, pgoff_t index, > int fgp_flags, gfp_t gfp); > @@ -546,6 +545,9 @@ static inline struct folio *filemap_lock_folio(struct address_space *mapping, > return __filemap_get_folio(mapping, index, FGP_LOCK, 0); > } > > +struct folio *__filemap_get_folio_entry(struct address_space *mapping, > + pgoff_t index, int fgp_flags); > + > /** > * find_get_page - find and get a page reference > * @mapping: the address_space to search > diff --git a/mm/filemap.c b/mm/filemap.c > index c4d4ace9cc7003..d04613347b3e71 100644 > --- a/mm/filemap.c > +++ b/mm/filemap.c > @@ -1887,8 +1887,6 @@ static void *mapping_get_entry(struct address_space *mapping, pgoff_t index) > * > * * %FGP_ACCESSED - The folio will be marked accessed. > * * %FGP_LOCK - The folio is returned locked. > - * * %FGP_ENTRY - If there is a shadow / swap / DAX entry, return it > - * instead of allocating a new folio to replace it. > * * %FGP_CREAT - If no page is present then a new page is allocated using > * @gfp and added to the page cache and the VM's LRU list. > * The page is returned locked and with an increased refcount. > @@ -1914,11 +1912,8 @@ struct folio *__filemap_get_folio(struct address_space *mapping, pgoff_t index, > > repeat: > folio = mapping_get_entry(mapping, index); > - if (xa_is_value(folio)) { > - if (fgp_flags & FGP_ENTRY) > - return folio; > + if (xa_is_value(folio)) > folio = NULL; > - } > if (!folio) > goto no_page; > > @@ -1994,6 +1989,49 @@ struct folio *__filemap_get_folio(struct address_space *mapping, pgoff_t index, > } > EXPORT_SYMBOL(__filemap_get_folio); > > + > +/** > + * __filemap_get_folio_entry - Find and get a reference to a folio. > + * @mapping: The address_space to search. > + * @index: The page index. > + * @fgp_flags: %FGP flags modify how the folio is returned. > + * > + * Looks up the page cache entry at @mapping & @index. If there is a shadow / > + * swap / DAX entry, return it instead of allocating a new folio to replace it. > + * > + * @fgp_flags can be zero or more of these flags: > + * > + * * %FGP_LOCK - The folio is returned locked. > + * > + * If there is a page cache page, it is returned with an increased refcount. > + * > + * Return: The found folio or %NULL otherwise. > + */ > +struct folio *__filemap_get_folio_entry(struct address_space *mapping, > + pgoff_t index, int fgp_flags) > +{ > + struct folio *folio; > + > + if (WARN_ON_ONCE(fgp_flags & ~FGP_LOCK)) > + return NULL; > + > +repeat: > + folio = mapping_get_entry(mapping, index); > + if (folio && !xa_is_value(folio) && (fgp_flags & FGP_LOCK)) { > + folio_lock(folio); > + > + /* Has the page been truncated? */ > + if (unlikely(folio->mapping != mapping)) { > + folio_unlock(folio); > + folio_put(folio); > + goto repeat; > + } > + VM_BUG_ON_FOLIO(!folio_contains(folio, index), folio); > + } > + > + return folio; > +} > + > static inline struct folio *find_get_entry(struct xa_state *xas, pgoff_t max, > xa_mark_t mark) > { > diff --git a/mm/huge_memory.c b/mm/huge_memory.c > index abe6cfd92ffa0e..88b517c338a6db 100644 > --- a/mm/huge_memory.c > +++ b/mm/huge_memory.c > @@ -3088,10 +3088,10 @@ static int split_huge_pages_in_file(const char *file_path, pgoff_t off_start, > mapping = candidate->f_mapping; > > for (index = off_start; index < off_end; index += nr_pages) { > - struct folio *folio = __filemap_get_folio(mapping, index, > - FGP_ENTRY, 0); > + struct folio *folio; > > nr_pages = 1; > + folio = __filemap_get_folio_entry(mapping, index, 0); > if (xa_is_value(folio) || !folio) > continue; > > diff --git a/mm/shmem.c b/mm/shmem.c > index c301487be5fb40..0a36563ef7a0c1 100644 > --- a/mm/shmem.c > +++ b/mm/shmem.c > @@ -888,8 +888,7 @@ static struct folio *shmem_get_partial_folio(struct inode *inode, pgoff_t index) > * At first avoid shmem_get_folio(,,,SGP_READ): that fails > * beyond i_size, and reports fallocated pages as holes. > */ > - folio = __filemap_get_folio(inode->i_mapping, index, > - FGP_ENTRY | FGP_LOCK, 0); > + folio = __filemap_get_folio_entry(inode->i_mapping, index, FGP_LOCK); > if (!xa_is_value(folio)) > return folio; > /* > @@ -1860,7 +1859,7 @@ static int shmem_get_folio_gfp(struct inode *inode, pgoff_t index, > sbinfo = SHMEM_SB(inode->i_sb); > charge_mm = vma ? vma->vm_mm : NULL; > > - folio = __filemap_get_folio(mapping, index, FGP_ENTRY | FGP_LOCK, 0); > + folio = __filemap_get_folio_entry(mapping, index, FGP_LOCK); > if (folio && vma && userfaultfd_minor(vma)) { > if (!xa_is_value(folio)) { > folio_unlock(folio); > diff --git a/mm/swap_state.c b/mm/swap_state.c > index 2927507b43d819..1f45241987aea2 100644 > --- a/mm/swap_state.c > +++ b/mm/swap_state.c > @@ -384,7 +384,7 @@ struct folio *filemap_get_incore_folio(struct address_space *mapping, > { > swp_entry_t swp; > struct swap_info_struct *si; > - struct folio *folio = __filemap_get_folio(mapping, index, FGP_ENTRY, 0); > + struct folio *folio = __filemap_get_folio_entry(mapping, index, 0); > > if (!xa_is_value(folio)) > goto out; > -- > 2.39.0 >