diff mbox series

[v2,44/46] mm/filemap: Convert mapping_get_entry to return a folio

Message ID 20210622121551.3398730-45-willy@infradead.org (mailing list archive)
State New, archived
Headers show
Series Folio-enabling the page cache | expand

Commit Message

Matthew Wilcox June 22, 2021, 12:15 p.m. UTC
The pagecache only contains folios, so indicate that this is definitely
not a tail page.  Shrinks mapping_get_entry() by 56 bytes, but grows
pagecache_get_page() by 21 bytes as gcc makes slightly different hot/cold
code decisions.  A net reduction of 35 bytes of text.

Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>
---
 mm/filemap.c | 38 +++++++++++++++++---------------------
 1 file changed, 17 insertions(+), 21 deletions(-)

Comments

Christoph Hellwig June 23, 2021, 11:32 a.m. UTC | #1
On Tue, Jun 22, 2021 at 01:15:49PM +0100, Matthew Wilcox (Oracle) wrote:
> - * Return: The head page or shadow entry, %NULL if nothing is found.
> + * Return: The folio, swap or shadow entry, %NULL if nothing is found.

This (old and new) reads a little weird, given that it returns a
struct folio, even if that happens to be a magic entry.

Otherwise looks good:

Reviewed-by: Christoph Hellwig <hch@lst.de>
Matthew Wilcox June 25, 2021, 3:29 a.m. UTC | #2
On Wed, Jun 23, 2021 at 01:32:39PM +0200, Christoph Hellwig wrote:
> On Tue, Jun 22, 2021 at 01:15:49PM +0100, Matthew Wilcox (Oracle) wrote:
> > - * Return: The head page or shadow entry, %NULL if nothing is found.
> > + * Return: The folio, swap or shadow entry, %NULL if nothing is found.
> 
> This (old and new) reads a little weird, given that it returns a
> struct folio, even if that happens to be a magic entry.

Yeah.  How about this?

- * Return: The head page or shadow entry, %NULL if nothing is found.
+ * Return: The folio, swap or shadow entry, %NULL if nothing is found.
  */
-static struct page *mapping_get_entry(struct address_space *mapping,
-               pgoff_t index)
+static void *mapping_get_entry(struct address_space *mapping, pgoff_t index)
 {

I still use a struct folio in mapping_get_entry(), but this means that
pagecache_get_page() doesn't change in this patch.
Christoph Hellwig June 28, 2021, 6:07 a.m. UTC | #3
On Fri, Jun 25, 2021 at 04:29:47AM +0100, Matthew Wilcox wrote:
> > > - * Return: The head page or shadow entry, %NULL if nothing is found.
> > > + * Return: The folio, swap or shadow entry, %NULL if nothing is found.
> > 
> > This (old and new) reads a little weird, given that it returns a
> > struct folio, even if that happens to be a magic entry.
> 
> Yeah.  How about this?
> 
> - * Return: The head page or shadow entry, %NULL if nothing is found.
> + * Return: The folio, swap or shadow entry, %NULL if nothing is found.
>   */
> -static struct page *mapping_get_entry(struct address_space *mapping,
> -               pgoff_t index)
> +static void *mapping_get_entry(struct address_space *mapping, pgoff_t index)
>  {
> 
> I still use a struct folio in mapping_get_entry(), but this means that
> pagecache_get_page() doesn't change in this patch.

Much better, thanks.
diff mbox series

Patch

diff --git a/mm/filemap.c b/mm/filemap.c
index a174f8ce87ea..7d0b51f30223 100644
--- a/mm/filemap.c
+++ b/mm/filemap.c
@@ -1754,49 +1754,43 @@  EXPORT_SYMBOL(page_cache_prev_miss);
  * @mapping: the address_space to search
  * @index: The page cache index.
  *
- * Looks up the page cache slot at @mapping & @index.  If there is a
- * page cache page, the head page is returned with an increased refcount.
+ * Looks up the page cache entry at @mapping & @index.  If it is a folio,
+ * it is returned with an increased refcount.  If it is a shadow entry
+ * of a previously evicted folio, or a swap entry from shmem/tmpfs,
+ * it is returned without further action.
  *
- * If the slot holds a shadow entry of a previously evicted page, or a
- * swap entry from shmem/tmpfs, it is returned.
- *
- * Return: The head page or shadow entry, %NULL if nothing is found.
+ * Return: The folio, swap or shadow entry, %NULL if nothing is found.
  */
-static struct page *mapping_get_entry(struct address_space *mapping,
+static struct folio *mapping_get_entry(struct address_space *mapping,
 		pgoff_t index)
 {
 	XA_STATE(xas, &mapping->i_pages, index);
-	struct page *page;
+	struct folio *folio;
 
 	rcu_read_lock();
 repeat:
 	xas_reset(&xas);
-	page = xas_load(&xas);
-	if (xas_retry(&xas, page))
+	folio = xas_load(&xas);
+	if (xas_retry(&xas, folio))
 		goto repeat;
 	/*
 	 * A shadow entry of a recently evicted page, or a swap entry from
 	 * shmem/tmpfs.  Return it without attempting to raise page count.
 	 */
-	if (!page || xa_is_value(page))
+	if (!folio || xa_is_value(folio))
 		goto out;
 
-	if (!page_cache_get_speculative(page))
+	if (!folio_try_get_rcu(folio))
 		goto repeat;
 
-	/*
-	 * Has the page moved or been split?
-	 * This is part of the lockless pagecache protocol. See
-	 * include/linux/pagemap.h for details.
-	 */
-	if (unlikely(page != xas_reload(&xas))) {
-		put_page(page);
+	if (unlikely(folio != xas_reload(&xas))) {
+		folio_put(folio);
 		goto repeat;
 	}
 out:
 	rcu_read_unlock();
 
-	return page;
+	return folio;
 }
 
 /**
@@ -1836,10 +1830,12 @@  static struct page *mapping_get_entry(struct address_space *mapping,
 struct page *pagecache_get_page(struct address_space *mapping, pgoff_t index,
 		int fgp_flags, gfp_t gfp_mask)
 {
+	struct folio *folio;
 	struct page *page;
 
 repeat:
-	page = mapping_get_entry(mapping, index);
+	folio = mapping_get_entry(mapping, index);
+	page = &folio->page;
 	if (xa_is_value(page)) {
 		if (fgp_flags & FGP_ENTRY)
 			return page;