Skip to contentSkip to conversation

mm/page_io: use folios in sio_write_complete()

PATCH In review David Carlier 3 participants 5 messages Mar 23 – Mar 30

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

PATCH 1/1 mm/page_io: use folios in sio_write_complete()

Convert sio_write_complete() from legacy page APIs to folio equivalents, matching the conversion already done for sio_read_complete() in commit 6a8c068774ad ("mm/page_io: use a folio in sio_read_complete()").

Replace set_page_dirty(), ClearPageReclaim() and end_page_writeback() with folio_mark_dirty(), folio_clear_reclaim() and folio_end_writeback() respectively, saving implicit calls to compound_head().

Also replace page_swap_entry() with a direct folio->swap access since bvec entries are always set via bvec_set_folio(), meaning bv_page is always the head page.

---
 mm/page_io.c | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)
mm/page_io.c +10 -8
index 63b262f4c5a9..6c7ab3c394ad 100644
@@ -348,7 +348,6 @@ int sio_pool_init(void)
 static void sio_write_complete(struct kiocb *iocb, long ret)
 {
 	struct swap_iocb *sio = container_of(iocb, struct swap_iocb, iocb);
-	struct page *page = sio->bvec[0].bv_page;
 	int p;
 
 	if (ret != sio->len) {
@@ -356,21 +355,24 @@ static void sio_write_complete(struct kiocb *iocb, long ret)
 		 * In the case of swap-over-nfs, this can be a
 		 * temporary failure if the system has limited
 		 * memory for allocating transmit buffers.
-		 * Mark the page dirty and avoid
+		 * Mark the folio dirty and avoid
 		 * folio_rotate_reclaimable but rate-limit the
 		 * messages.
 		 */
+		struct folio *folio = page_folio(sio->bvec[0].bv_page);
 		pr_err_ratelimited("Write error %ld on dio swapfile (%llu)\n",
-				   ret, swap_dev_pos(page_swap_entry(page)));
+				   ret, swap_dev_pos(folio->swap));
 		for (p = 0; p < sio->pages; p++) {
-			page = sio->bvec[p].bv_page;
-			set_page_dirty(page);
-			ClearPageReclaim(page);
+			folio = page_folio(sio->bvec[p].bv_page);
+			folio_mark_dirty(folio);
+			folio_clear_reclaim(folio);
 		}
 	}
 
-	for (p = 0; p < sio->pages; p++)
-		end_page_writeback(sio->bvec[p].bv_page);
+	for (p = 0; p < sio->pages; p++) {
+		struct folio *folio = page_folio(sio->bvec[p].bv_page);
+		folio_end_writeback(folio);
+	}
 
 	mempool_free(sio, sio_pool);
 }
Signature · 1 line
2.53.0
maintainer
#1 lore raw
Quoted 4 lines · On Mon, 23 Mar 2026 17:04:49 +0000 David Carlier <[email protected]> wrote:

Convert sio_write_complete() from legacy page APIs to folio equivalents, matching the conversion already done for sio_read_complete() in commit 6a8c068774ad ("mm/page_io: use a folio in sio_read_complete()").

Quoted 8 lines

Replace set_page_dirty(), ClearPageReclaim() and end_page_writeback() with folio_mark_dirty(), folio_clear_reclaim() and folio_end_writeback() respectively, saving implicit calls to compound_head().

Also replace page_swap_entry() with a direct folio->swap access since bvec entries are always set via bvec_set_folio(), meaning bv_page is always the head page.

This prompted AI review to look at sio_read_complete() and it didn't like what it saw. Can you please check
https://sashiko.dev/#/patchset/[email protected]?

Also, mm.git is (over)full. Can we please park the nice-to-have cleanups until after next -rc1?

#2 lore raw

Hi Andrew,

Thanks for pointing that out. I'll park the folio conversion until after -rc1.

Sashiko's observation is correct — sio_read_complete() undercounts PSWPIN for large folios. sio->pages tracks bvec entries (folios), not base pages. The bdev paths correctly use folio_nr_pages(folio) but the sio path doesn't.

I'll send a fix replacing sio->pages with sio->len >> PAGE_SHIFT, which gives the correct base page count since sio->len is accumulated via folio_size(folio).

Cheers !

Quoted 22 lines · Andrew Morton
Quoted 4 lines · On Mon, 23 Mar 2026 17:04:49 +0000 David Carlier <[email protected]> wrote:

Convert sio_write_complete() from legacy page APIs to folio equivalents, matching the conversion already done for sio_read_complete() in commit 6a8c068774ad ("mm/page_io: use a folio in sio_read_complete()").

Quoted 8 lines

Replace set_page_dirty(), ClearPageReclaim() and end_page_writeback() with folio_mark_dirty(), folio_clear_reclaim() and folio_end_writeback() respectively, saving implicit calls to compound_head().

Also replace page_swap_entry() with a direct folio->swap access since bvec entries are always set via bvec_set_folio(), meaning bv_page is always the head page.

This prompted AI review to look at sio_read_complete() and it didn't like what it saw. Can you please check
https://sashiko.dev/#/patchset/[email protected]?

Also, mm.git is (over)full. Can we please park the nice-to-have cleanups until after next -rc1?

#3 lore raw

PATCH 1/1 mm/page_io: fix PSWPIN undercount for large folios in sio_read_complete()

In reply to David Carlier

sio_read_complete() uses sio->pages to account global PSWPIN vm events, but sio->pages tracks the number of bvec entries (folios), not base pages. For large folios this undercounts compared to the per-memcg path which correctly uses folio_nr_pages(), and compared to the bdev read paths which also use folio_nr_pages().

Use sio->len >> PAGE_SHIFT instead, which gives the correct base page count since sio->len is accumulated via folio_size(folio).

Fixes 6a8c068774ad ("mm/page_io: use a folio in sio_read_complete()")
---
 mm/page_io.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/page_io.c +1 -1
index 63b262f4c5a9..1389cd57ca88 100644
@@ -497,7 +497,7 @@ static void sio_read_complete(struct kiocb *iocb, long ret)
 			folio_mark_uptodate(folio);
 			folio_unlock(folio);
 		}
-		count_vm_events(PSWPIN, sio->pages);
+		count_vm_events(PSWPIN, sio->len >> PAGE_SHIFT);
 	} else {
 		for (p = 0; p < sio->pages; p++) {
 			struct folio *folio = page_folio(sio->bvec[p].bv_page);
Signature · 1 line
2.53.0
#4 lore raw

Re: [PATCH] mm/page_io: fix PSWPIN undercount for large folios in sio_read_complete()

Hi,

please don't send patches a reply to other patches.

Quoted 7 lines · David Carlier

sio_read_complete() uses sio->pages to account global PSWPIN vm events, but sio->pages tracks the number of bvec entries (folios), not base pages.
For large folios this undercounts compared to the per-memcg path which correctly uses folio_nr_pages(), and compared to the bdev read paths which also use folio_nr_pages().

Quoted 6 lines

Use sio->len >> PAGE_SHIFT instead, which gives the correct base page count since sio->len is accumulated via folio_size(folio).

Fixes 6a8c068774ad ("mm/page_io: use a folio in sio_read_complete()")

Is that commit really to blame? I don't see how that one would cause it. It would rather be the code that actually made more than one page be represented in a single entry.

Is maybe a1a0dfd56f97 ("mm: handle THP in swap_*page_fs()") to blame?

Quoted 18 lines
---
 mm/page_io.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/page_io.c +1 -1
index 63b262f4c5a9..1389cd57ca88 100644
@@ -497,7 +497,7 @@ static void sio_read_complete(struct kiocb *iocb, long ret)
 			folio_mark_uptodate(folio);
 			folio_unlock(folio);
 		}
-		count_vm_events(PSWPIN, sio->pages);
+		count_vm_events(PSWPIN, sio->len >> PAGE_SHIFT);
 	} else {
 		for (p = 0; p < sio->pages; p++) {
 			struct folio *folio = page_folio(sio->bvec[p].bv_page);
Signature · 3 lines
Cheers,

David