Skip to contentSkip to conversation

Convert to filemap_get_folios_contig()

PATCH Superseded Vishal Moola (Oracle) 4 participants 16 messages Aug 16, 2022 – Aug 23, 2022

Subsystems (5)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover Convert to filemap_get_folios_contig()

This patch series replaces find_get_pages_contig() with filemap_get_folios_contig(). I've run xfstests on btrfs. I've also tested the ramfs changes. I ran some xfstests on nilfs2, and its seemingly fine although more testing may be beneficial.
---

v2:
- Removed an unused label in nilfs2

Vishal Moola (Oracle) (7):
filemap: Add filemap_get_folios_contig()
btrfs: Convert __process_pages_contig() to use
filemap_get_folios_contig()
btrfs: Convert end_compressed_writeback() to use filemap_get_folios()
btrfs: Convert process_page_range() to use filemap_get_folios_contig()
nilfs2: Convert nilfs_find_uncommited_extent() to use
filemap_get_folios_contig()
ramfs: Convert ramfs_nommu_get_unmapped_area() to use
filemap_get_folios_contig()
filemap: Remove find_get_pages_contig()

 fs/btrfs/compression.c           | 26 ++++++------
 fs/btrfs/extent_io.c             | 33 +++++++--------
 fs/btrfs/subpage.c               |  2 +-
 fs/btrfs/tests/extent-io-tests.c | 31 +++++++-------
 fs/nilfs2/page.c                 | 39 ++++++++----------
 fs/ramfs/file-nommu.c            | 50 ++++++++++++----------
 include/linux/pagemap.h          |  4 +-
 mm/filemap.c                     | 71 +++++++++++++++++++-------------
 8 files changed, 134 insertions(+), 122 deletions(-)
Signature · 1 line
2.36.1
#1 lore raw

PATCH v2 1/7 filemap: Add filemap_get_folios_contig()

This function is meant to replace find_get_pages_contig().

Unlike find_get_pages_contig(), filemap_get_folios_contig() no longer takes in a target number of pages to find - It returns up to 15 contiguous folios.

To be more consistent with filemap_get_folios(), filemap_get_folios_contig() now also updates the start index passed in, and takes an end index.

---
 include/linux/pagemap.h |  2 ++
 mm/filemap.c            | 73 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 75 insertions(+)
include/linux/pagemap.h +2 -0
index cc9adbaddb59..951936a2be1d 100644
@@ -720,6 +720,8 @@ static inline struct page *find_subpage(struct page *head, pgoff_t index)
 
 unsigned filemap_get_folios(struct address_space *mapping, pgoff_t *start,
 		pgoff_t end, struct folio_batch *fbatch);
+unsigned filemap_get_folios_contig(struct address_space *mapping,
+		pgoff_t *start, pgoff_t end, struct folio_batch *fbatch);
 unsigned find_get_pages_contig(struct address_space *mapping, pgoff_t start,
 			       unsigned int nr_pages, struct page **pages);
 unsigned find_get_pages_range_tag(struct address_space *mapping, pgoff_t *index,
mm/filemap.c +73 -0

79 lines not on this page · raw message

Signature · 1 line
2.36.1
#2 lore raw

PATCH v2 2/7 btrfs: Convert __process_pages_contig() to use filemap_get_folios_contig()

In reply to Vishal Moola (Oracle)

Convert to use folios throughout. This is in preparation for the removal of find_get_pages_contig(). Now also supports large folios.

Since we may receive more than nr_pages pages, nr_pages may underflow. Since nr_pages > 0 is equivalent to index <= end_index, we replaced it with this check instead.

---
 fs/btrfs/extent_io.c | 33 +++++++++++++++------------------
 1 file changed, 15 insertions(+), 18 deletions(-)
fs/btrfs/extent_io.c +15 -18
index 8f6b544ae616..f16929bc531b 100644
@@ -1882,9 +1882,8 @@ static int __process_pages_contig(struct address_space *mapping,
 	pgoff_t start_index = start >> PAGE_SHIFT;
 	pgoff_t end_index = end >> PAGE_SHIFT;
 	pgoff_t index = start_index;
-	unsigned long nr_pages = end_index - start_index + 1;
 	unsigned long pages_processed = 0;
-	struct page *pages[16];
+	struct folio_batch fbatch;
 	int err = 0;
 	int i;
 
@@ -1893,16 +1892,17 @@ static int __process_pages_contig(struct address_space *mapping,
 		ASSERT(processed_end && *processed_end == start);
 	}
 
-	if ((page_ops & PAGE_SET_ERROR) && nr_pages > 0)
+	if ((page_ops & PAGE_SET_ERROR) && start_index <= end_index)
 		mapping_set_error(mapping, -EIO);
 
-	while (nr_pages > 0) {
-		int found_pages;
+	folio_batch_init(&fbatch);
+	while (index <= end_index) {
+		int found_folios;
+
+		found_folios = filemap_get_folios_contig(mapping, &index,
+				end_index, &fbatch);
 
-		found_pages = find_get_pages_contig(mapping, index,
-				     min_t(unsigned long,
-				     nr_pages, ARRAY_SIZE(pages)), pages);
-		if (found_pages == 0) {
+		if (found_folios == 0) {
 			/*
 			 * Only if we're going to lock these pages, we can find
 			 * nothing at @index.
@@ -1912,23 +1912,20 @@ static int __process_pages_contig(struct address_space *mapping,
 			goto out;
 		}
 
-		for (i = 0; i < found_pages; i++) {
+		for (i = 0; i < found_folios; i++) {
 			int process_ret;
-
+			struct folio *folio = fbatch.folios[i];
 			process_ret = process_one_page(fs_info, mapping,
-					pages[i], locked_page, page_ops,
+					&folio->page, locked_page, page_ops,
 					start, end);
 			if (process_ret < 0) {
-				for (; i < found_pages; i++)
-					put_page(pages[i]);
 				err = -EAGAIN;
+				folio_batch_release(&fbatch);
 				goto out;
 			}
-			put_page(pages[i]);
-			pages_processed++;
+			pages_processed += folio_nr_pages(folio);
 		}
-		nr_pages -= found_pages;
-		index += found_pages;
+		folio_batch_release(&fbatch);
 		cond_resched();
 	}
 out:
Signature · 1 line
2.36.1
#3 lore raw

Re: [PATCH v2 2/7] btrfs: Convert __process_pages_contig() to use filemap_get_folios_contig()

Quoted 84 lines · Vishal Moola (Oracle)

Convert to use folios throughout. This is in preparation for the removal of find_get_pages_contig(). Now also supports large folios.

Since we may receive more than nr_pages pages, nr_pages may underflow. Since nr_pages > 0 is equivalent to index <= end_index, we replaced it with this check instead.

---
 fs/btrfs/extent_io.c | 33 +++++++++++++++------------------
 1 file changed, 15 insertions(+), 18 deletions(-)
fs/btrfs/extent_io.c +15 -19
index 8f6b544ae616..f16929bc531b 100644
@@ -1882,9 +1882,8 @@ static int __process_pages_contig(struct address_space *mapping,
        pgoff_t start_index = start >> PAGE_SHIFT;
        pgoff_t end_index = end >> PAGE_SHIFT;
        pgoff_t index = start_index;
-       unsigned long nr_pages = end_index - start_index + 1;
        unsigned long pages_processed = 0;
-       struct page *pages[16];
+       struct folio_batch fbatch;
        int err = 0;
        int i;
 
@@ -1893,16 +1892,17 @@ static int __process_pages_contig(struct address_space *mapping,
                ASSERT(processed_end && *processed_end == start);
        }
 
-       if ((page_ops & PAGE_SET_ERROR) && nr_pages > 0)
+       if ((page_ops & PAGE_SET_ERROR) && start_index <= end_index)
                mapping_set_error(mapping, -EIO);
 
-       while (nr_pages > 0) {
-               int found_pages;
+       folio_batch_init(&fbatch);
+       while (index <= end_index) {
+               int found_folios;
+
+               found_folios = filemap_get_folios_contig(mapping, &index,
+                               end_index, &fbatch);
 
-               found_pages = find_get_pages_contig(mapping, index,
-                                    min_t(unsigned long,
-                                    nr_pages, ARRAY_SIZE(pages)), pages);
-               if (found_pages == 0) {
+               if (found_folios == 0) {
                        /*
                         * Only if we're going to lock these pages, we can find
                         * nothing at @index.
@@ -1912,23 +1912,20 @@ static int __process_pages_contig(struct address_space *mapping,
                        goto out;
                }
 
-               for (i = 0; i < found_pages; i++) {
+               for (i = 0; i < found_folios; i++) {
                        int process_ret;
-
+                       struct folio *folio = fbatch.folios[i];
                        process_ret = process_one_page(fs_info, mapping,
-                                       pages[i], locked_page, page_ops,
+                                       &folio->page, locked_page, page_ops,
                                        start, end);
                        if (process_ret < 0) {
-                               for (; i < found_pages; i++)
-                                       put_page(pages[i]);
                                err = -EAGAIN;
+                               folio_batch_release(&fbatch);
                                goto out;
                        }
-                       put_page(pages[i]);
-                       pages_processed++;
+                       pages_processed += folio_nr_pages(folio);
                }
-               nr_pages -= found_pages;
-               index += found_pages;
+               folio_batch_release(&fbatch);
                cond_resched();
        }
 out:
--
Quoted 2 lines

2.36.1

Just following up on these btrfs patches (2/7, 3/7, 4/7). Does anyone have time to review them this week? Feedback would be appreciated.

Acked-by on v2 2/7
#4 lore raw

Re: [PATCH v2 2/7] btrfs: Convert __process_pages_contig() to use filemap_get_folios_contig()

In reply to Vishal Moola (Oracle)
Quoted 8 lines · Vishal Moola (Oracle)

Convert to use folios throughout. This is in preparation for the removal of find_get_pages_contig(). Now also supports large folios.

Since we may receive more than nr_pages pages, nr_pages may underflow. Since nr_pages > 0 is equivalent to index <= end_index, we replaced it with this check instead.

#5 lore raw

PATCH v2 3/7 btrfs: Convert end_compressed_writeback() to use filemap_get_folios()

In reply to Vishal Moola (Oracle)

Converted function to use folios throughout. This is in preparation for the removal of find_get_pages_contig(). Now also supports large folios.

Since we may receive more than nr_pages pages, nr_pages may underflow. Since nr_pages > 0 is equivalent to index <= end_index, we replaced it with this check instead.

Also this function does not care about the pages being contiguous so we can just use filemap_get_folios() to be more efficient.

---
 fs/btrfs/compression.c | 26 ++++++++++++--------------
 1 file changed, 12 insertions(+), 14 deletions(-)
fs/btrfs/compression.c +12 -14
index f4564f32f6d9..3dcd5006cfb4 100644
@@ -8,6 +8,7 @@
 #include <linux/file.h>
 #include <linux/fs.h>
 #include <linux/pagemap.h>
+#include <linux/pagevec.h>
 #include <linux/highmem.h>
 #include <linux/kthread.h>
 #include <linux/time.h>
@@ -339,8 +340,7 @@ static noinline void end_compressed_writeback(struct inode *inode,
 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
 	unsigned long index = cb->start >> PAGE_SHIFT;
 	unsigned long end_index = (cb->start + cb->len - 1) >> PAGE_SHIFT;
-	struct page *pages[16];
-	unsigned long nr_pages = end_index - index + 1;
+	struct folio_batch fbatch;
 	const int errno = blk_status_to_errno(cb->status);
 	int i;
 	int ret;
@@ -348,24 +348,22 @@ static noinline void end_compressed_writeback(struct inode *inode,
 	if (errno)
 		mapping_set_error(inode->i_mapping, errno);
 
-	while (nr_pages > 0) {
-		ret = find_get_pages_contig(inode->i_mapping, index,
-				     min_t(unsigned long,
-				     nr_pages, ARRAY_SIZE(pages)), pages);
+	folio_batch_init(&fbatch);
+	while (index <= end_index) {
+		ret = filemap_get_folios(inode->i_mapping, &index, end_index,
+				&fbatch);
+
 		if (ret == 0) {
-			nr_pages -= 1;
-			index += 1;
-			continue;
+			return;
 		}
 		for (i = 0; i < ret; i++) {
+			struct folio *folio = fbatch.folios[i];
 			if (errno)
-				SetPageError(pages[i]);
-			btrfs_page_clamp_clear_writeback(fs_info, pages[i],
+				folio_set_error(folio);
+			btrfs_page_clamp_clear_writeback(fs_info, &folio->page,
 							 cb->start, cb->len);
-			put_page(pages[i]);
 		}
-		nr_pages -= ret;
-		index += ret;
+		folio_batch_release(&fbatch);
 	}
 	/* the inode may be gone now */
 }
Signature · 1 line
2.36.1
Acked-by on v2 3/7
#6 lore raw

Re: [PATCH v2 3/7] btrfs: Convert end_compressed_writeback() to use filemap_get_folios()

Quoted 11 lines · Vishal Moola (Oracle)

Converted function to use folios throughout. This is in preparation for the removal of find_get_pages_contig(). Now also supports large folios.

Since we may receive more than nr_pages pages, nr_pages may underflow. Since nr_pages > 0 is equivalent to index <= end_index, we replaced it with this check instead.

Also this function does not care about the pages being contiguous so we can just use filemap_get_folios() to be more efficient.

With minor comments

Quoted 29 lines
fs/btrfs/compression.c +3 -6
@@ -8,6 +8,7 @@
 #include <linux/file.h>
 #include <linux/fs.h>
 #include <linux/pagemap.h>
+#include <linux/pagevec.h>
 #include <linux/highmem.h>
 #include <linux/kthread.h>
 #include <linux/time.h>
@@ -339,8 +340,7 @@ static noinline void end_compressed_writeback(struct inode *inode,
 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
 	unsigned long index = cb->start >> PAGE_SHIFT;
 	unsigned long end_index = (cb->start + cb->len - 1) >> PAGE_SHIFT;
-	struct page *pages[16];
-	unsigned long nr_pages = end_index - index + 1;
+	struct folio_batch fbatch;
 	const int errno = blk_status_to_errno(cb->status);
 	int i;
 	int ret;
@@ -348,24 +348,22 @@ static noinline void end_compressed_writeback(struct inode *inode,
 	if (errno)
 		mapping_set_error(inode->i_mapping, errno);
 
-	while (nr_pages > 0) {
-		ret = find_get_pages_contig(inode->i_mapping, index,
-				     min_t(unsigned long,
-				     nr_pages, ARRAY_SIZE(pages)), pages);
+	folio_batch_init(&fbatch);

fs/btrfs/compression.c static noinline void end_compressed_writeback(struct inode *inode, · line 358

Quoted 10 lines
fs/btrfs/compression.c +5 -3
+	while (index <= end_index) {
+		ret = filemap_get_folios(inode->i_mapping, &index, end_index,
+				&fbatch);
+
 		if (ret == 0) {
-			nr_pages -= 1;
-			index += 1;
-			continue;
+			return;
 		}

Please drop { } around single statement

fs/btrfs/compression.c static noinline void end_compressed_writeback(struct inode *inode, · line 360

Quoted 2 lines
fs/btrfs/compression.c +1 -0
 		for (i = 0; i < ret; i++) {
+			struct folio *folio = fbatch.folios[i];

And add a newline after declaration.

Quoted 16 lines
fs/btrfs/compression.c +3 -5
 			if (errno)
-				SetPageError(pages[i]);
-			btrfs_page_clamp_clear_writeback(fs_info, pages[i],
+				folio_set_error(folio);
+			btrfs_page_clamp_clear_writeback(fs_info, &folio->page,
 							 cb->start, cb->len);
-			put_page(pages[i]);
 		}
-		nr_pages -= ret;
-		index += ret;
+		folio_batch_release(&fbatch);
 	}
 	/* the inode may be gone now */
 }
Signature · 1 line
2.36.1
#7 lore raw

PATCH v2 4/7 btrfs: Convert process_page_range() to use filemap_get_folios_contig()

In reply to Vishal Moola (Oracle)

Converted function to use folios throughout. This is in preparation for the removal of find_get_pages_contig(). Now also supports large folios.

Since we may receive more than nr_pages pages, nr_pages may underflow. Since nr_pages > 0 is equivalent to index <= end_index, we replaced it with this check instead.

Also minor comment renaming for consistency in subpage.

---
 fs/btrfs/subpage.c               |  2 +-
 fs/btrfs/tests/extent-io-tests.c | 31 ++++++++++++++++---------------
 2 files changed, 17 insertions(+), 16 deletions(-)
fs/btrfs/subpage.c +1 -1
index a105b291444f..6418c38c4b30 100644
@@ -337,7 +337,7 @@ bool btrfs_subpage_end_and_test_writer(const struct btrfs_fs_info *fs_info,
  *
  * Even with 0 returned, the page still need extra check to make sure
  * it's really the correct page, as the caller is using
- * find_get_pages_contig(), which can race with page invalidating.
+ * filemap_get_folios_contig(), which can race with page invalidating.
  */
 int btrfs_page_start_writer_lock(const struct btrfs_fs_info *fs_info,
 		struct page *page, u64 start, u32 len)
fs/btrfs/tests/extent-io-tests.c +16 -15
index a232b15b8021..530073868916 100644
@@ -4,6 +4,7 @@
  */
 
 #include <linux/pagemap.h>
+#include <linux/pagevec.h>
 #include <linux/sched.h>
 #include <linux/slab.h>
 #include <linux/sizes.h>
@@ -20,39 +21,39 @@ static noinline int process_page_range(struct inode *inode, u64 start, u64 end,
 				       unsigned long flags)
 {
 	int ret;
-	struct page *pages[16];
+	struct folio_batch fbatch;
 	unsigned long index = start >> PAGE_SHIFT;
 	unsigned long end_index = end >> PAGE_SHIFT;
-	unsigned long nr_pages = end_index - index + 1;
 	int i;
 	int count = 0;
 	int loops = 0;
 
-	while (nr_pages > 0) {
-		ret = find_get_pages_contig(inode->i_mapping, index,
-				     min_t(unsigned long, nr_pages,
-				     ARRAY_SIZE(pages)), pages);
+	folio_batch_init(&fbatch);
+
+	while (index <= end_index) {
+		ret = filemap_get_folios_contig(inode->i_mapping, &index,
+				end_index, &fbatch);
 		for (i = 0; i < ret; i++) {
+			struct folio *folio = fbatch.folios[i];
 			if (flags & PROCESS_TEST_LOCKED &&
-			    !PageLocked(pages[i]))
+			    !folio_test_locked(folio))
 				count++;
-			if (flags & PROCESS_UNLOCK && PageLocked(pages[i]))
-				unlock_page(pages[i]);
-			put_page(pages[i]);
+			if (flags & PROCESS_UNLOCK && folio_test_locked(folio))
+				folio_unlock(folio);
 			if (flags & PROCESS_RELEASE)
-				put_page(pages[i]);
+				folio_put(folio);
 		}
-		nr_pages -= ret;
-		index += ret;
+		folio_batch_release(&fbatch);
 		cond_resched();
 		loops++;
 		if (loops > 100000) {
 			printk(KERN_ERR
-		"stuck in a loop, start %llu, end %llu, nr_pages %lu, ret %d\n",
-				start, end, nr_pages, ret);
+		"stuck in a loop, start %llu, end %llu, ret %d\n",
+				start, end, ret);
 			break;
 		}
 	}
+
 	return count;
 }
 
Signature · 1 line
2.36.1
Acked-by on v2 4/7
#8 lore raw

Re: [PATCH v2 4/7] btrfs: Convert process_page_range() to use filemap_get_folios_contig()

Quoted 10 lines · Vishal Moola (Oracle)

Converted function to use folios throughout. This is in preparation for the removal of find_get_pages_contig(). Now also supports large folios.

Since we may receive more than nr_pages pages, nr_pages may underflow. Since nr_pages > 0 is equivalent to index <= end_index, we replaced it with this check instead.

Also minor comment renaming for consistency in subpage.

Quoted 24 lines
fs/btrfs/tests/extent-io-tests.c +2 -3
@@ -4,6 +4,7 @@
  */
 
 #include <linux/pagemap.h>
+#include <linux/pagevec.h>
 #include <linux/sched.h>
 #include <linux/slab.h>
 #include <linux/sizes.h>
@@ -20,39 +21,39 @@ static noinline int process_page_range(struct inode *inode, u64 start, u64 end,
 				       unsigned long flags)
 {
 	int ret;
-	struct page *pages[16];
+	struct folio_batch fbatch;
 	unsigned long index = start >> PAGE_SHIFT;
 	unsigned long end_index = end >> PAGE_SHIFT;
-	unsigned long nr_pages = end_index - index + 1;
 	int i;
 	int count = 0;
 	int loops = 0;
 
-	while (nr_pages > 0) {

fs/btrfs/tests/extent-io-tests.c static noinline int process_page_range(struct inode *inode, u64 start, u64 end, · line 37

Quoted 10 lines
fs/btrfs/tests/extent-io-tests.c +6 -3
-		ret = find_get_pages_contig(inode->i_mapping, index,
-				     min_t(unsigned long, nr_pages,
-				     ARRAY_SIZE(pages)), pages);
+	folio_batch_init(&fbatch);
+
+	while (index <= end_index) {
+		ret = filemap_get_folios_contig(inode->i_mapping, &index,
+				end_index, &fbatch);
 		for (i = 0; i < ret; i++) {
+			struct folio *folio = fbatch.folios[i];

Add a newline please

Quoted 33 lines
fs/btrfs/tests/extent-io-tests.c +8 -9
 			if (flags & PROCESS_TEST_LOCKED &&
-			    !PageLocked(pages[i]))
+			    !folio_test_locked(folio))
 				count++;
-			if (flags & PROCESS_UNLOCK && PageLocked(pages[i]))
-				unlock_page(pages[i]);
-			put_page(pages[i]);
+			if (flags & PROCESS_UNLOCK && folio_test_locked(folio))
+				folio_unlock(folio);
 			if (flags & PROCESS_RELEASE)
-				put_page(pages[i]);
+				folio_put(folio);
 		}
-		nr_pages -= ret;
-		index += ret;
+		folio_batch_release(&fbatch);
 		cond_resched();
 		loops++;
 		if (loops > 100000) {
 			printk(KERN_ERR
-		"stuck in a loop, start %llu, end %llu, nr_pages %lu, ret %d\n",
-				start, end, nr_pages, ret);
+		"stuck in a loop, start %llu, end %llu, ret %d\n",
+				start, end, ret);
 			break;
 		}
 	}
+
 	return count;
 }
 
Signature · 1 line
2.36.1
#9 lore raw

PATCH v2 5/7 nilfs2: Convert nilfs_find_uncommited_extent() to use filemap_get_folios_contig()

In reply to Vishal Moola (Oracle)

Converted function to use folios throughout. This is in preparation for the removal of find_get_pages_contig(). Now also supports large folios.

Also cleaned up an unnecessary if statement - pvec.pages[0]->index > index will always evaluate to false, and filemap_get_folios_contig() returns 0 if there is no folio found at index.

---

v2:
- Fixed a warning regarding a now unused label "out"
- Reported-by: kernel test robot <[email protected]>

---
 fs/nilfs2/page.c | 39 +++++++++++++++++----------------------
 1 file changed, 17 insertions(+), 22 deletions(-)
fs/nilfs2/page.c +17 -22
index 3267e96c256c..14629e03d0da 100644
@@ -480,13 +480,13 @@ unsigned long nilfs_find_uncommitted_extent(struct inode *inode,
 					    sector_t start_blk,
 					    sector_t *blkoff)
 {
-	unsigned int i;
+	unsigned int i, nr;
 	pgoff_t index;
 	unsigned int nblocks_in_page;
 	unsigned long length = 0;
 	sector_t b;
-	struct pagevec pvec;
-	struct page *page;
+	struct folio_batch fbatch;
+	struct folio *folio;
 
 	if (inode->i_mapping->nrpages == 0)
 		return 0;
@@ -494,27 +494,24 @@ unsigned long nilfs_find_uncommitted_extent(struct inode *inode,
 	index = start_blk >> (PAGE_SHIFT - inode->i_blkbits);
 	nblocks_in_page = 1U << (PAGE_SHIFT - inode->i_blkbits);
 
-	pagevec_init(&pvec);
+	folio_batch_init(&fbatch);
 
 repeat:
-	pvec.nr = find_get_pages_contig(inode->i_mapping, index, PAGEVEC_SIZE,
-					pvec.pages);
-	if (pvec.nr == 0)
+	nr = filemap_get_folios_contig(inode->i_mapping, &index, ULONG_MAX,
+			&fbatch);
+	if (nr == 0)
 		return length;
 
-	if (length > 0 && pvec.pages[0]->index > index)
-		goto out;
-
-	b = pvec.pages[0]->index << (PAGE_SHIFT - inode->i_blkbits);
+	b = fbatch.folios[0]->index << (PAGE_SHIFT - inode->i_blkbits);
 	i = 0;
 	do {
-		page = pvec.pages[i];
+		folio = fbatch.folios[i];
 
-		lock_page(page);
-		if (page_has_buffers(page)) {
+		folio_lock(folio);
+		if (folio_buffers(folio)) {
 			struct buffer_head *bh, *head;
 
-			bh = head = page_buffers(page);
+			bh = head = folio_buffers(folio);
 			do {
 				if (b < start_blk)
 					continue;
@@ -532,18 +529,16 @@ unsigned long nilfs_find_uncommitted_extent(struct inode *inode,
 
 			b += nblocks_in_page;
 		}
-		unlock_page(page);
+		folio_unlock(folio);
 
-	} while (++i < pagevec_count(&pvec));
+	} while (++i < nr);
 
-	index = page->index + 1;
-	pagevec_release(&pvec);
+	folio_batch_release(&fbatch);
 	cond_resched();
 	goto repeat;
 
 out_locked:
-	unlock_page(page);
-out:
-	pagevec_release(&pvec);
+	folio_unlock(folio);
+	folio_batch_release(&fbatch);
 	return length;
 }
Signature · 1 line
2.36.1
maintainer
#10 lore raw

Re: [PATCH v2 5/7] nilfs2: Convert nilfs_find_uncommited_extent() to use filemap_get_folios_contig()

Quoted 77 lines · Vishal Moola (Oracle)

Converted function to use folios throughout. This is in preparation for the removal of find_get_pages_contig(). Now also supports large folios.

Also cleaned up an unnecessary if statement - pvec.pages[0]->index > index will always evaluate to false, and filemap_get_folios_contig() returns 0 if there is no folio found at index.

---

v2:
- Fixed a warning regarding a now unused label "out"
- Reported-by: kernel test robot <[email protected]>

---
 fs/nilfs2/page.c | 39 +++++++++++++++++----------------------
 1 file changed, 17 insertions(+), 22 deletions(-)
fs/nilfs2/page.c +12 -15
index 3267e96c256c..14629e03d0da 100644
@@ -480,13 +480,13 @@ unsigned long nilfs_find_uncommitted_extent(struct inode *inode,
                                            sector_t start_blk,
                                            sector_t *blkoff)
 {
-       unsigned int i;
+       unsigned int i, nr;
        pgoff_t index;
        unsigned int nblocks_in_page;
        unsigned long length = 0;
        sector_t b;
-       struct pagevec pvec;
-       struct page *page;
+       struct folio_batch fbatch;
+       struct folio *folio;
 
        if (inode->i_mapping->nrpages == 0)
                return 0;
@@ -494,27 +494,24 @@ unsigned long nilfs_find_uncommitted_extent(struct inode *inode,
        index = start_blk >> (PAGE_SHIFT - inode->i_blkbits);
        nblocks_in_page = 1U << (PAGE_SHIFT - inode->i_blkbits);
 
-       pagevec_init(&pvec);
+       folio_batch_init(&fbatch);
 
 repeat:
-       pvec.nr = find_get_pages_contig(inode->i_mapping, index, PAGEVEC_SIZE,
-                                       pvec.pages);
-       if (pvec.nr == 0)
+       nr = filemap_get_folios_contig(inode->i_mapping, &index, ULONG_MAX,
+                       &fbatch);
+       if (nr == 0)
                return length;
 
-       if (length > 0 && pvec.pages[0]->index > index)
-               goto out;
-
-       b = pvec.pages[0]->index << (PAGE_SHIFT - inode->i_blkbits);
+       b = fbatch.folios[0]->index << (PAGE_SHIFT - inode->i_blkbits);
        i = 0;
        do {
-               page = pvec.pages[i];
+               folio = fbatch.folios[i];
 
-               lock_page(page);
-               if (page_has_buffers(page)) {
+               folio_lock(folio);
+               if (folio_buffers(folio)) {
                        struct buffer_head *bh, *head;
 
-                       bh = head = page_buffers(page);
+                       bh = head = folio_buffers(folio);
                        do {
                                if (b < start_blk)
                                        continue;

fs/nilfs2/page.c unsigned long nilfs_find_uncommitted_extent(struct inode *inode, · line 531

Quoted 3 lines
fs/nilfs2/page.c +0 -0
@@ -532,18 +529,16 @@ unsigned long nilfs_find_uncommitted_extent(struct inode *inode,
 
 
                        b += nblocks_in_page;

Here, It looks like the block index "b" should be updated with the number of blocks in the
folio because the loop is now per folio, not per page.

Instead of replacing it with a calculation that uses folio_size(folio) or folio_shift(folio),
I think it would be better to move the calculation of "b" inside the branch where the folio
has buffers as follows:

                if (folio_buffers(folio)) {
                        struct buffer_head *bh, *head;
                        sector_t b;

                        b = folio->index << (PAGE_SHIFT - inode->i_blkbits);
                        bh = head = folio_buffers(folio);
                        ...
               } else if (length > 0) {
                       goto out_locked;
               }

This way, we can remove calculations for the block index "b" outside the above part
and the variable "nblocks_in_page" as well.

Thanks,
Ryusuke Konishi

Quoted 24 lines
fs/nilfs2/page.c +5 -7
                }
-               unlock_page(page);
+               folio_unlock(folio);
 
-       } while (++i < pagevec_count(&pvec));
+       } while (++i < nr);
 
-       index = page->index + 1;
-       pagevec_release(&pvec);
+       folio_batch_release(&fbatch);
        cond_resched();
        goto repeat;
 
 out_locked:
-       unlock_page(page);
-out:
-       pagevec_release(&pvec);
+       folio_unlock(folio);
+       folio_batch_release(&fbatch);
        return length;

}
--
2.36.1

#11 lore raw

Re: [PATCH v2 5/7] nilfs2: Convert nilfs_find_uncommited_extent() to use filemap_get_folios_contig()

Quoted 81 lines · Ryusuke Konishi
Quoted 78 lines · Vishal Moola (Oracle)

Converted function to use folios throughout. This is in preparation for the removal of find_get_pages_contig(). Now also supports large folios.

Also cleaned up an unnecessary if statement - pvec.pages[0]->index > index will always evaluate to false, and filemap_get_folios_contig() returns 0 if there is no folio found at index.

---

v2:
- Fixed a warning regarding a now unused label "out"
- Reported-by: kernel test robot <[email protected]>

---
 fs/nilfs2/page.c | 39 +++++++++++++++++----------------------
 1 file changed, 17 insertions(+), 22 deletions(-)
fs/nilfs2/page.c +12 -15
index 3267e96c256c..14629e03d0da 100644
@@ -480,13 +480,13 @@ unsigned long nilfs_find_uncommitted_extent(struct inode *inode,
                                            sector_t start_blk,
                                            sector_t *blkoff)
 {
-       unsigned int i;
+       unsigned int i, nr;
        pgoff_t index;
        unsigned int nblocks_in_page;
        unsigned long length = 0;
        sector_t b;
-       struct pagevec pvec;
-       struct page *page;
+       struct folio_batch fbatch;
+       struct folio *folio;
 
        if (inode->i_mapping->nrpages == 0)
                return 0;
@@ -494,27 +494,24 @@ unsigned long nilfs_find_uncommitted_extent(struct inode *inode,
        index = start_blk >> (PAGE_SHIFT - inode->i_blkbits);
        nblocks_in_page = 1U << (PAGE_SHIFT - inode->i_blkbits);
 
-       pagevec_init(&pvec);
+       folio_batch_init(&fbatch);
 
 repeat:
-       pvec.nr = find_get_pages_contig(inode->i_mapping, index, PAGEVEC_SIZE,
-                                       pvec.pages);
-       if (pvec.nr == 0)
+       nr = filemap_get_folios_contig(inode->i_mapping, &index, ULONG_MAX,
+                       &fbatch);
+       if (nr == 0)
                return length;
 
-       if (length > 0 && pvec.pages[0]->index > index)
-               goto out;
-
-       b = pvec.pages[0]->index << (PAGE_SHIFT - inode->i_blkbits);
+       b = fbatch.folios[0]->index << (PAGE_SHIFT - inode->i_blkbits);
        i = 0;
        do {
-               page = pvec.pages[i];
+               folio = fbatch.folios[i];
 
-               lock_page(page);
-               if (page_has_buffers(page)) {
+               folio_lock(folio);
+               if (folio_buffers(folio)) {
                        struct buffer_head *bh, *head;
 
-                       bh = head = page_buffers(page);
+                       bh = head = folio_buffers(folio);
                        do {
                                if (b < start_blk)
                                        continue;
@@ -532,18 +529,16 @@ unsigned long nilfs_find_uncommitted_extent(struct inode *inode,
 
Quoted 5 lines

fs/nilfs2/page.c unsigned long nilfs_find_uncommitted_extent(struct inode *inode, · line 530

Quoted 1 line
fs/nilfs2/page.c +0 -0
                        b += nblocks_in_page;

Here, It looks like the block index "b" should be updated with the number of blocks in the
folio because the loop is now per folio, not per page.

Yup, thanks for catching that.

Quoted 17 lines

Instead of replacing it with a calculation that uses folio_size(folio) or folio_shift(folio),
I think it would be better to move the calculation of "b" inside the branch where the folio
has buffers as follows:

                if (folio_buffers(folio)) {
                        struct buffer_head *bh, *head;
                        sector_t b;

                        b = folio->index << (PAGE_SHIFT - inode->i_blkbits);
                        bh = head = folio_buffers(folio);
                        ...
               } else if (length > 0) {
                       goto out_locked;
               }
Quoted 3 lines

This way, we can remove calculations for the block index "b" outside the above part
and the variable "nblocks_in_page" as well.

Sounds good, I'll do that.

Quoted 27 lines

Thanks,
Ryusuke Konishi

Quoted 24 lines
fs/nilfs2/page.c +5 -8
                }
-               unlock_page(page);
+               folio_unlock(folio);
 
-       } while (++i < pagevec_count(&pvec));
+       } while (++i < nr);
 
-       index = page->index + 1;
-       pagevec_release(&pvec);
+       folio_batch_release(&fbatch);
        cond_resched();
        goto repeat;
 
 out_locked:
-       unlock_page(page);
-out:
-       pagevec_release(&pvec);
+       folio_unlock(folio);
+       folio_batch_release(&fbatch);
        return length;
 }
--

2.36.1

#12 lore raw

PATCH v2 6/7 ramfs: Convert ramfs_nommu_get_unmapped_area() to use filemap_get_folios_contig()

In reply to Vishal Moola (Oracle)

Converted to use folios throughout. This is in preparation for the removal for find_get_pages_contig(). Now also supports large folios.

The initial version of this function set the page_address to be returned after finishing all the checks. Since folio_batches have a maximum of 15 folios, the function had to be modified to support getting and checking up to lpages, 15 pages at a time while still returning the initial page address. Now the function sets ret as soon as the first batch arrives, and updates it only if a check fails.

The physical adjacency check utilizes the page frame numbers. The page frame number of each folio must be nr_pages away from the first folio.

---
 fs/ramfs/file-nommu.c | 50 +++++++++++++++++++++++++------------------
 1 file changed, 29 insertions(+), 21 deletions(-)
fs/ramfs/file-nommu.c +29 -21
index ba3525ccc27e..81817f301e17 100644
@@ -203,9 +203,9 @@ static unsigned long ramfs_nommu_get_unmapped_area(struct file *file,
 					    unsigned long addr, unsigned long len,
 					    unsigned long pgoff, unsigned long flags)
 {
-	unsigned long maxpages, lpages, nr, loop, ret;
+	unsigned long maxpages, lpages, nr, loop, ret, nr_pages, pfn;
 	struct inode *inode = file_inode(file);
-	struct page **pages = NULL, **ptr, *page;
+	struct folio_batch fbatch;
 	loff_t isize;
 
 	/* the mapping mustn't extend beyond the EOF */
@@ -221,31 +221,39 @@ static unsigned long ramfs_nommu_get_unmapped_area(struct file *file,
 		goto out;
 
 	/* gang-find the pages */
-	pages = kcalloc(lpages, sizeof(struct page *), GFP_KERNEL);
-	if (!pages)
-		goto out_free;
-
-	nr = find_get_pages_contig(inode->i_mapping, pgoff, lpages, pages);
-	if (nr != lpages)
-		goto out_free_pages; /* leave if some pages were missing */
+	folio_batch_init(&fbatch);
+	nr_pages = 0;
+repeat:
+	nr = filemap_get_folios_contig(inode->i_mapping, &pgoff,
+			ULONG_MAX, &fbatch);
+	if (!nr) {
+		ret = -ENOSYS;
+		return ret;
+	}
 
+	if (ret == -ENOSYS) {
+		ret = (unsigned long) folio_address(fbatch.folios[0]);
+		pfn = folio_pfn(fbatch.folios[0]);
+	}
 	/* check the pages for physical adjacency */
-	ptr = pages;
-	page = *ptr++;
-	page++;
-	for (loop = lpages; loop > 1; loop--)
-		if (*ptr++ != page++)
-			goto out_free_pages;
+	for (loop = 0; loop < nr; loop++) {
+		if (pfn + nr_pages != folio_pfn(fbatch.folios[loop])) {
+			ret = -ENOSYS;
+			goto out_free; /* leave if not physical adjacent */
+		}
+		nr_pages += folio_nr_pages(fbatch.folios[loop]);
+		if (nr_pages >= lpages)
+			goto out_free; /* successfully found desired pages*/
+	}
 
+	if (nr_pages < lpages) {
+		folio_batch_release(&fbatch);
+		goto repeat; /* loop if pages are missing */
+	}
 	/* okay - all conditions fulfilled */
-	ret = (unsigned long) page_address(pages[0]);
 
-out_free_pages:
-	ptr = pages;
-	for (loop = nr; loop > 0; loop--)
-		put_page(*ptr++);
 out_free:
-	kfree(pages);
+	folio_batch_release(&fbatch);
 out:
 	return ret;
 }
Signature · 1 line
2.36.1
#13 lore raw

PATCH v2 7/7 filemap: Remove find_get_pages_contig()

In reply to Vishal Moola (Oracle)

All callers of find_get_pages_contig() have been removed, so it is no longer needed.

---
 include/linux/pagemap.h |  2 --
 mm/filemap.c            | 60 -----------------------------------------
 2 files changed, 62 deletions(-)
include/linux/pagemap.h +0 -2
index 951936a2be1d..a04a645b64e9 100644
@@ -722,8 +722,6 @@ unsigned filemap_get_folios(struct address_space *mapping, pgoff_t *start,
 		pgoff_t end, struct folio_batch *fbatch);
 unsigned filemap_get_folios_contig(struct address_space *mapping,
 		pgoff_t *start, pgoff_t end, struct folio_batch *fbatch);
-unsigned find_get_pages_contig(struct address_space *mapping, pgoff_t start,
-			       unsigned int nr_pages, struct page **pages);
 unsigned find_get_pages_range_tag(struct address_space *mapping, pgoff_t *index,
 			pgoff_t end, xa_mark_t tag, unsigned int nr_pages,
 			struct page **pages);
mm/filemap.c +0 -60

66 lines not on this page · raw message

Signature · 1 line
2.36.1
#14 lore raw
In reply to Vishal Moola (Oracle)
Quoted 21 lines · Vishal Moola (Oracle)

This patch series replaces find_get_pages_contig() with filemap_get_folios_contig(). I've run xfstests on btrfs. I've also tested the ramfs changes. I ran some xfstests on nilfs2, and its seemingly fine although more testing may be beneficial.
---

v2:
- Removed an unused label in nilfs2

Vishal Moola (Oracle) (7):
filemap: Add filemap_get_folios_contig()
btrfs: Convert __process_pages_contig() to use
filemap_get_folios_contig()
btrfs: Convert end_compressed_writeback() to use filemap_get_folios()
btrfs: Convert process_page_range() to use filemap_get_folios_contig()
nilfs2: Convert nilfs_find_uncommited_extent() to use
filemap_get_folios_contig()
ramfs: Convert ramfs_nommu_get_unmapped_area() to use
filemap_get_folios_contig()
filemap: Remove find_get_pages_contig()

Quoted 4 lines
 fs/btrfs/compression.c           | 26 ++++++------
 fs/btrfs/extent_io.c             | 33 +++++++--------
 fs/btrfs/subpage.c               |  2 +-
 fs/btrfs/tests/extent-io-tests.c | 31 +++++++-------

I've tested the whole branch in my fstest setup, no issues found and did a light review of the code changes, looks ok as well.

How do you want get the patches merged? As it's an API conversion I can ack it and let it go via the mm tree. So far there are no conflicts with our btrfs development patches, I assume it'll be a clean merge in the future too.

Quoted 8 lines
 fs/nilfs2/page.c                 | 39 ++++++++----------
 fs/ramfs/file-nommu.c            | 50 ++++++++++++----------
 include/linux/pagemap.h          |  4 +-
 mm/filemap.c                     | 71 +++++++++++++++++++-------------
 8 files changed, 134 insertions(+), 122 deletions(-)
Signature · 1 line
2.36.1
maintainer
#15 lore raw
Quoted 2 lines · David Sterba

I've tested the whole branch in my fstest setup, no issues found and did a light review of the code changes, looks ok as well.

Thanks!

Quoted 4 lines

How do you want get the patches merged? As it's an API conversion I can ack it and let it go via the mm tree. So far there are no conflicts with our btrfs development patches, I assume it'll be a clean merge in the future too.

I was planning on taking this patch series through the pagecache tree; it's in -next, so any problems will show up as conflicts there.