Skip to contentSkip to conversation

mm/gup: Cleanup memfd_pin_folios()

PATCH Applied Vishal Moola (Oracle) 3 participants 7 messages Apr 30, 2025 – May 1, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover mm/gup: Cleanup memfd_pin_folios()

A couple straightforward cleanups to memfd_pin_folios() found through code inspection. Saves 124 bytes of kernel text overall and makes the code more readable.

Vishal Moola (Oracle) (2):
mm/gup: Remove unnecessary check in memfd_pin_folios() mm/gup: Remove page_folio() in memfd_pin_folios()

 mm/gup.c | 17 ++---------------
 1 file changed, 2 insertions(+), 15 deletions(-)
Signature · 1 line
2.49.0
#1 lore raw

PATCH 1/2 mm/gup: Remove unnecessary check in memfd_pin_folios()

Commit 89c1905d9c14 ("mm/gup: introduce memfd_pin_folios() for pinning memfd folios") checks if filemap_get_folios_contig() returned duplicate folios to prevent multiple attempts at pinning the same folio.

Commit 8ab1b1602396 ("mm: fix filemap_get_folios_contig returning batches of identical folios") ensures that filemap_get_folios_contig() returns a batch of distinct folios.

We can remove the duplicate folio check to simplify the code and save 58 bytes of text.

---
 mm/gup.c | 15 +--------------
 1 file changed, 1 insertion(+), 14 deletions(-)
mm/gup.c +1 -14
index f32168339390..1fb8f3b9a493 100644
@@ -3589,7 +3589,7 @@ long memfd_pin_folios(struct file *memfd, loff_t start, loff_t end,
 {
 	unsigned int flags, nr_folios, nr_found;
 	unsigned int i, pgshift = PAGE_SHIFT;
-	pgoff_t start_idx, end_idx, next_idx;
+	pgoff_t start_idx, end_idx;
 	struct folio *folio = NULL;
 	struct folio_batch fbatch;
 	struct hstate *h;
@@ -3639,19 +3639,7 @@ long memfd_pin_folios(struct file *memfd, loff_t start, loff_t end,
 				folio = NULL;
 			}
 
-			next_idx = 0;
 			for (i = 0; i < nr_found; i++) {
-				/*
-				 * As there can be multiple entries for a
-				 * given folio in the batch returned by
-				 * filemap_get_folios_contig(), the below
-				 * check is to ensure that we pin and return a
-				 * unique set of folios between start and end.
-				 */
-				if (next_idx &&
-				    next_idx != folio_index(fbatch.folios[i]))
-					continue;
-
 				folio = page_folio(&fbatch.folios[i]->page);
 
 				if (try_grab_folio(folio, 1, FOLL_PIN)) {
@@ -3664,7 +3652,6 @@ long memfd_pin_folios(struct file *memfd, loff_t start, loff_t end,
 					*offset = offset_in_folio(folio, start);
 
 				folios[nr_folios] = folio;
-				next_idx = folio_next_index(folio);
 				if (++nr_folios == max_folios)
 					break;
 			}
Signature · 1 line
2.49.0
Acked-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] mm/gup: Remove unnecessary check in memfd_pin_folios()

Quoted 4 lines · Vishal Moola (Oracle)

Commit 89c1905d9c14 ("mm/gup: introduce memfd_pin_folios() for pinning memfd folios") checks if filemap_get_folios_contig() returned duplicate folios to prevent multiple attempts at pinning the same folio.

Quoted 8 lines

Commit 8ab1b1602396 ("mm: fix filemap_get_folios_contig returning batches of identical folios") ensures that filemap_get_folios_contig() returns a batch of distinct folios.

We can remove the duplicate folio check to simplify the code and save 58 bytes of text.

---

Signature · 3 lines
Cheers,

David / dhildenb
Acked-by on v1 1/2
#3 lore raw

RE: [PATCH 1/2] mm/gup: Remove unnecessary check in memfd_pin_folios()

In reply to Vishal Moola (Oracle)

Hi Vishal,

Quoted 41 lines

Subject: [PATCH 1/2] mm/gup: Remove unnecessary check in memfd_pin_folios()

Commit 89c1905d9c14 ("mm/gup: introduce memfd_pin_folios() for pinning memfd folios")
checks if filemap_get_folios_contig() returned duplicate folios to prevent multiple attempts at pinning the same folio.

Commit 8ab1b1602396 ("mm: fix filemap_get_folios_contig returning batches of identical folios")
ensures that filemap_get_folios_contig() returns a batch of distinct folios.

We can remove the duplicate folio check to simplify the code and save 58 bytes of text.

---
 mm/gup.c | 15 +--------------
 1 file changed, 1 insertion(+), 14 deletions(-)
mm/gup.c +1 -1
index f32168339390..1fb8f3b9a493 100644
@@ -3589,7 +3589,7 @@ long memfd_pin_folios(struct file *memfd, loff_t
 start, loff_t end,
 {
 	unsigned int flags, nr_folios, nr_found;
 	unsigned int i, pgshift = PAGE_SHIFT;
-	pgoff_t start_idx, end_idx, next_idx;
+	pgoff_t start_idx, end_idx;
 	struct folio *folio = NULL;
 	struct folio_batch fbatch;
mm/gup.c +0 -1
 	struct hstate *h;
@@ -3639,19 +3639,7 @@ long memfd_pin_folios(struct file *memfd, loff_t
 start, loff_t end,
 				folio = NULL;
 			}
 
-			next_idx = 0;
 			for (i = 0; i < nr_found; i++) {

mm/gup.c long memfd_pin_folios(struct file *memfd, loff_t · line 3654

Quoted 10 lines
mm/gup.c +0 -10
-				/*
-				 * As there can be multiple entries for a
-				 * given folio in the batch returned by
-				 * filemap_get_folios_contig(), the below
-				 * check is to ensure that we pin and return a
-				 * unique set of folios between start and end.
-				 */
-				if (next_idx &&
-				    next_idx != folio_index(fbatch.folios[i]))
-					continue;

At that time, I believed that filemap_get_folios_contig() returning duplicate folios was by design and not a result of a bug and hence added this workaround, which is no longer needed in light of your fix.
Anyway, LGTM.

Thanks,
Vivek

Quoted 16 lines
mm/gup.c +0 -1
-
 				folio = page_folio(&fbatch.folios[i]->page);
 
mm/gup.c +0 -1
 				if (try_grab_folio(folio, 1, FOLL_PIN)) {
@@ -3664,7 +3652,6 @@ long memfd_pin_folios(struct file *memfd, loff_t
 start, loff_t end,
 					*offset = offset_in_folio(folio, start);
 
 				folios[nr_folios] = folio;
-				next_idx = folio_next_index(folio);
 				if (++nr_folios == max_folios)
 					break;

}
--
2.49.0

#4 lore raw

PATCH 2/2 mm/gup: Remove page_folio() in memfd_pin_folios()

In reply to Vishal Moola (Oracle)

We can get the folio directly from the folio batch, so remove the unnecessary page_folio() call.

---
 mm/gup.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/gup.c +1 -1
index 1fb8f3b9a493..795fd94f379d 100644
@@ -3640,7 +3640,7 @@ long memfd_pin_folios(struct file *memfd, loff_t start, loff_t end,
 			}
 
 			for (i = 0; i < nr_found; i++) {
-				folio = page_folio(&fbatch.folios[i]->page);
+				folio = fbatch.folios[i];
 
 				if (try_grab_folio(folio, 1, FOLL_PIN)) {
 					folio_batch_release(&fbatch);
Signature · 1 line
2.49.0
Acked-by on v1 2/2
#5 lore raw

Re: [PATCH 2/2] mm/gup: Remove page_folio() in memfd_pin_folios()

Quoted 13 lines · Vishal Moola (Oracle)

We can get the folio directly from the folio batch, so remove the unnecessary page_folio() call.

---
  mm/gup.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
mm/gup.c +0 -0
index 1fb8f3b9a493..795fd94f379d 100644

mm/gup.c long memfd_pin_folios(struct file *memfd, loff_t start, loff_t end, · line 3646

Quoted 8 lines
mm/gup.c +1 -1
@@ -3640,7 +3640,7 @@ long memfd_pin_folios(struct file *memfd, loff_t start, loff_t end,
  			}
 
  			for (i = 0; i < nr_found; i++) {
-				folio = page_folio(&fbatch.folios[i]->page);
+				folio = fbatch.folios[i];
 
  				if (try_grab_folio(folio, 1, FOLL_PIN)) {
  					folio_batch_release(&fbatch);
Signature · 3 lines
Cheers,

David / dhildenb
Acked-by on v1 2/2
#6 lore raw

RE: [PATCH 2/2] mm/gup: Remove page_folio() in memfd_pin_folios()

In reply to Vishal Moola (Oracle)

Hi Vishal,

Quoted 15 lines

Subject: [PATCH 2/2] mm/gup: Remove page_folio() in memfd_pin_folios()

We can get the folio directly from the folio batch, so remove the unnecessary page_folio() call.

---
 mm/gup.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/gup.c +0 -0
index 1fb8f3b9a493..795fd94f379d 100644

mm/gup.c long memfd_pin_folios(struct file *memfd, loff_t · line 3644

Quoted 6 lines
mm/gup.c +1 -1
@@ -3640,7 +3640,7 @@ long memfd_pin_folios(struct file *memfd, loff_t
 start, loff_t end,
 			}
 
 			for (i = 0; i < nr_found; i++) {
-				folio = page_folio(&fbatch.folios[i]->page);
+				folio = fbatch.folios[i];

I don't recall why I chose to extract the folio in a roundabout way. LGTM.

Thanks,
Vivek

Quoted 5 lines
mm/gup.c +0 -1
 				if (try_grab_folio(folio, 1, FOLL_PIN)) {
 					folio_batch_release(&fbatch);
--

2.49.0