Skip to contentSkip to conversation

page_io: return proper error codes for swap_read_folio_zeromap()

PATCH In review Nhat Pham 5 participants 8 messages Mar 6, 2025 – Apr 1, 2025

Subsystems (2)
lore
Share & tools
lore

Nhat Pham

author reviewer
#0 lore raw

PATCH 1/1 page_io: return proper error codes for swap_read_folio_zeromap()

Similar to zswap_load(), also return proper error codes for swap_read_folio_zeromap():

* 0 on success. The folio is unlocked and marked up-to-date.
* -ENOENT, if the folio is entirely not zeromapped.
* -EINVAL (with the follio unlocked but not marked to date), if the folio is partially zeromapped. This is not supported, and will SIGBUS the faulting process.

This patch is purely a clean-up, and should not have any behavioral change. It is based on (and should be applied on top of) [1].

[1]: https://lore.kernel.org/linux-mm/[email protected]/

---
 mm/page_io.c | 35 ++++++++++++++++++++++++++---------
 1 file changed, 26 insertions(+), 9 deletions(-)
mm/page_io.c +26 -9
index 4bce19df557b..48ed1e810392 100644
@@ -511,7 +511,23 @@ static void sio_read_complete(struct kiocb *iocb, long ret)
 	mempool_free(sio, sio_pool);
 }
 
-static bool swap_read_folio_zeromap(struct folio *folio)
+/**
+ * swap_read_folio_zeromap - check if the folio was zeromapped, and if so,
+ *                           zero-fill it.
+ * @folio: the folio.
+ *
+ * Return: 0 on success, with the folio zero-filled, unlocked, and marked
+ * up-to-date, or one of the following error codes:
+ *
+ *  -ENOENT: the folio is entirely not zeromapped. The folio remains locked.
+ *
+ *  -EINVAL: some of the subpages in the folio are zeromaped, but not all of
+ *  them. This is an error because we don't currently support a large folio
+ *  that is partially in the zeromap. The folio is unlocked, but NOT marked
+ *  up-to-date, so that an IO error is emitted (e.g. do_swap_page() will
+ *  sigbus).
+ */
+static int swap_read_folio_zeromap(struct folio *folio)
 {
 	int nr_pages = folio_nr_pages(folio);
 	struct obj_cgroup *objcg;
@@ -519,15 +535,17 @@ static bool swap_read_folio_zeromap(struct folio *folio)
 
 	/*
 	 * Swapping in a large folio that is partially in the zeromap is not
-	 * currently handled. Return true without marking the folio uptodate so
+	 * currently handled. Return -EINVAL without marking the folio uptodate so
 	 * that an IO error is emitted (e.g. do_swap_page() will sigbus).
 	 */
 	if (WARN_ON_ONCE(swap_zeromap_batch(folio->swap, nr_pages,
-			&is_zeromap) != nr_pages))
-		return true;
+			&is_zeromap) != nr_pages)) {
+		folio_unlock(folio);
+		return -EINVAL;
+	}
 
 	if (!is_zeromap)
-		return false;
+		return -ENOENT;
 
 	objcg = get_obj_cgroup_from_folio(folio);
 	count_vm_events(SWPIN_ZERO, nr_pages);
@@ -538,7 +556,8 @@ static bool swap_read_folio_zeromap(struct folio *folio)
 
 	folio_zero_range(folio, 0, folio_size(folio));
 	folio_mark_uptodate(folio);
-	return true;
+	folio_unlock(folio);
+	return 0;
 }
 
 static void swap_read_folio_fs(struct folio *folio, struct swap_iocb **plug)
@@ -635,10 +654,8 @@ void swap_read_folio(struct folio *folio, struct swap_iocb **plug)
 	}
 	delayacct_swapin_start();
 
-	if (swap_read_folio_zeromap(folio)) {
-		folio_unlock(folio);
+	if (swap_read_folio_zeromap(folio) != -ENOENT)
 		goto finish;
-	}
 
 	if (zswap_load(folio) != -ENOENT)
 		goto finish;
Signature · 1 line
2.43.5
Reviewed-by on v1 1/1
#1 lore raw
Quoted 35 lines · Nhat Pham

Similar to zswap_load(), also return proper error codes for swap_read_folio_zeromap():

* 0 on success. The folio is unlocked and marked up-to-date.
* -ENOENT, if the folio is entirely not zeromapped.
* -EINVAL (with the follio unlocked but not marked to date), if the folio is partially zeromapped. This is not supported, and will SIGBUS the faulting process.

This patch is purely a clean-up, and should not have any behavioral change. It is based on (and should be applied on top of) [1].

[1]: https://lore.kernel.org/linux-mm/[email protected]/

---
 mm/page_io.c | 35 ++++++++++++++++++++++++++---------
 1 file changed, 26 insertions(+), 9 deletions(-)
mm/page_io.c +5 -1
index 4bce19df557b..48ed1e810392 100644
@@ -511,7 +511,23 @@ static void sio_read_complete(struct kiocb *iocb, long ret)
 	mempool_free(sio, sio_pool);
 }
 
-static bool swap_read_folio_zeromap(struct folio *folio)
+/**
+ * swap_read_folio_zeromap - check if the folio was zeromapped, and if so,
+ *                           zero-fill it.
+ * @folio: the folio.
+ *

mm/page_io.c static void sio_read_complete(struct kiocb *iocb, long ret) · line 528

Quoted 10 lines
mm/page_io.c +10 -0
+ * Return: 0 on success, with the folio zero-filled, unlocked, and marked
+ * up-to-date, or one of the following error codes:
+ *
+ *  -ENOENT: the folio is entirely not zeromapped. The folio remains locked.
+ *
+ *  -EINVAL: some of the subpages in the folio are zeromaped, but not all of
+ *  them. This is an error because we don't currently support a large folio
+ *  that is partially in the zeromap. The folio is unlocked, but NOT marked
+ *  up-to-date, so that an IO error is emitted (e.g. do_swap_page() will
+ *  sigbus).

This is a bit repetitive. Maybe:

 *  -EINVAL: The folio is partially in the zeromap, which is not
 *  currently supported. The folio is unlocked, but NOT marked
 *  up-to-date, so that an IO error is emitted (e.g. do_swap_page() will
 *  sigbus).

mm/page_io.c static bool swap_read_folio_zeromap(struct folio *folio) · line 540

Quoted 13 lines
mm/page_io.c +3 -1
+ */
+static int swap_read_folio_zeromap(struct folio *folio)
 {
 	int nr_pages = folio_nr_pages(folio);
 	struct obj_cgroup *objcg;
@@ -519,15 +535,17 @@ static bool swap_read_folio_zeromap(struct folio *folio)
 
 	/*
 	 * Swapping in a large folio that is partially in the zeromap is not
-	 * currently handled. Return true without marking the folio uptodate so
+	 * currently handled. Return -EINVAL without marking the folio uptodate so
 	 * that an IO error is emitted (e.g. do_swap_page() will sigbus).
 	 */

I would drop this whole comment now because it's mostly repeating what's now documneted above.

With the comments fixed up:

Quoted 39 lines
mm/page_io.c +8 -7
 	if (WARN_ON_ONCE(swap_zeromap_batch(folio->swap, nr_pages,
-			&is_zeromap) != nr_pages))
-		return true;
+			&is_zeromap) != nr_pages)) {
+		folio_unlock(folio);
+		return -EINVAL;
+	}
 
 	if (!is_zeromap)
-		return false;
+		return -ENOENT;
 
 	objcg = get_obj_cgroup_from_folio(folio);
 	count_vm_events(SWPIN_ZERO, nr_pages);
@@ -538,7 +556,8 @@ static bool swap_read_folio_zeromap(struct folio *folio)
 
 	folio_zero_range(folio, 0, folio_size(folio));
 	folio_mark_uptodate(folio);
-	return true;
+	folio_unlock(folio);
+	return 0;
 }
 
 static void swap_read_folio_fs(struct folio *folio, struct swap_iocb **plug)
@@ -635,10 +654,8 @@ void swap_read_folio(struct folio *folio, struct swap_iocb **plug)
 	}
 	delayacct_swapin_start();
 
-	if (swap_read_folio_zeromap(folio)) {
-		folio_unlock(folio);
+	if (swap_read_folio_zeromap(folio) != -ENOENT)
 		goto finish;
-	}
 
 	if (zswap_load(folio) != -ENOENT)
 		goto finish;
Signature · 1 line
2.43.5
author reviewer
#2 lore raw
Quoted 17 lines · Yosry Ahmed
Quoted 7 lines

+ *

+ *  -EINVAL: some of the subpages in the folio are zeromaped, but not all of
+ *  them. This is an error because we don't currently support a large folio
+ *  that is partially in the zeromap. The folio is unlocked, but NOT marked
+ *  up-to-date, so that an IO error is emitted (e.g. do_swap_page() will
+ *  sigbus).

This is a bit repetitive. Maybe:

 *  -EINVAL: The folio is partially in the zeromap, which is not
 *  currently supported. The folio is unlocked, but NOT marked
 *  up-to-date, so that an IO error is emitted (e.g. do_swap_page() will
 *  sigbus).
Quoted 7 lines

I would drop this whole comment now because it's mostly repeating what's now documneted above.

With the comments fixed up:

Ah I like both suggestions :) Gimme one sec to send out a fixlet.

Thanks, Yosry!

Acked-by on v1 1/1
#3 lore raw
In reply to Nhat Pham
Quoted 9 lines · Nhat Pham

Similar to zswap_load(), also return proper error codes for swap_read_folio_zeromap():

* 0 on success. The folio is unlocked and marked up-to-date.
* -ENOENT, if the folio is entirely not zeromapped.
* -EINVAL (with the follio unlocked but not marked to date), if the folio is partially zeromapped. This is not supported, and will SIGBUS the faulting process.

Quoted 8 lines

This patch is purely a clean-up, and should not have any behavioral change. It is based on (and should be applied on top of) [1].

[1]: https://lore.kernel.org/linux-mm/[email protected]/

Nice! With Yosry's two suggestions:

Reviewed-by on v1 1/1
#4 lore raw
In reply to Nhat Pham
Quoted 9 lines · Nhat Pham

Similar to zswap_load(), also return proper error codes for swap_read_folio_zeromap():

* 0 on success. The folio is unlocked and marked up-to-date.
* -ENOENT, if the folio is entirely not zeromapped.
* -EINVAL (with the follio unlocked but not marked to date), if the folio is partially zeromapped. This is not supported, and will SIGBUS the faulting process.

Quoted 8 lines

This patch is purely a clean-up, and should not have any behavioral change. It is based on (and should be applied on top of) [1].

[1]: https://lore.kernel.org/linux-mm/[email protected]/

Thanks!

Quoted 77 lines
---
  mm/page_io.c | 35 ++++++++++++++++++++++++++---------
  1 file changed, 26 insertions(+), 9 deletions(-)
mm/page_io.c +26 -9
index 4bce19df557b..48ed1e810392 100644
@@ -511,7 +511,23 @@ static void sio_read_complete(struct kiocb *iocb, long ret)
  	mempool_free(sio, sio_pool);
  }
 
-static bool swap_read_folio_zeromap(struct folio *folio)
+/**
+ * swap_read_folio_zeromap - check if the folio was zeromapped, and if so,
+ *                           zero-fill it.
+ * @folio: the folio.
+ *
+ * Return: 0 on success, with the folio zero-filled, unlocked, and marked
+ * up-to-date, or one of the following error codes:
+ *
+ *  -ENOENT: the folio is entirely not zeromapped. The folio remains locked.
+ *
+ *  -EINVAL: some of the subpages in the folio are zeromaped, but not all of
+ *  them. This is an error because we don't currently support a large folio
+ *  that is partially in the zeromap. The folio is unlocked, but NOT marked
+ *  up-to-date, so that an IO error is emitted (e.g. do_swap_page() will
+ *  sigbus).
+ */
+static int swap_read_folio_zeromap(struct folio *folio)
  {
  	int nr_pages = folio_nr_pages(folio);
  	struct obj_cgroup *objcg;
@@ -519,15 +535,17 @@ static bool swap_read_folio_zeromap(struct folio *folio)
 
  	/*
  	 * Swapping in a large folio that is partially in the zeromap is not
-	 * currently handled. Return true without marking the folio uptodate so
+	 * currently handled. Return -EINVAL without marking the folio uptodate so
  	 * that an IO error is emitted (e.g. do_swap_page() will sigbus).
  	 */
  	if (WARN_ON_ONCE(swap_zeromap_batch(folio->swap, nr_pages,
-			&is_zeromap) != nr_pages))
-		return true;
+			&is_zeromap) != nr_pages)) {
+		folio_unlock(folio);
+		return -EINVAL;
+	}
 
  	if (!is_zeromap)
-		return false;
+		return -ENOENT;
 
  	objcg = get_obj_cgroup_from_folio(folio);
  	count_vm_events(SWPIN_ZERO, nr_pages);
@@ -538,7 +556,8 @@ static bool swap_read_folio_zeromap(struct folio *folio)
 
  	folio_zero_range(folio, 0, folio_size(folio));
  	folio_mark_uptodate(folio);
-	return true;
+	folio_unlock(folio);
+	return 0;
  }
 
  static void swap_read_folio_fs(struct folio *folio, struct swap_iocb **plug)
@@ -635,10 +654,8 @@ void swap_read_folio(struct folio *folio, struct swap_iocb **plug)
  	}
  	delayacct_swapin_start();
 
-	if (swap_read_folio_zeromap(folio)) {
-		folio_unlock(folio);
+	if (swap_read_folio_zeromap(folio) != -ENOENT)
  		goto finish;
-	}
 
  	if (zswap_load(folio) != -ENOENT)
  		goto finish;
author reviewer
#5 lore raw

PATCH 1/1 page_io: return proper error codes for swap_read_folio_zeromap() (fix)

In reply to Nhat Pham

Fix the comments for swap_read_folio_zeromap().

---
 mm/page_io.c | 10 ++--------
 1 file changed, 2 insertions(+), 8 deletions(-)
mm/page_io.c +2 -8
index 48ed1e810392..30ec132c801b 100644
@@ -521,9 +521,8 @@ static void sio_read_complete(struct kiocb *iocb, long ret)
  *
  *  -ENOENT: the folio is entirely not zeromapped. The folio remains locked.
  *
- *  -EINVAL: some of the subpages in the folio are zeromaped, but not all of
- *  them. This is an error because we don't currently support a large folio
- *  that is partially in the zeromap. The folio is unlocked, but NOT marked
+ *  -EINVAL: The folio is partially in the zeromap, which is not
+ *  currently supported. The folio is unlocked, but NOT marked
  *  up-to-date, so that an IO error is emitted (e.g. do_swap_page() will
  *  sigbus).
  */
@@ -533,11 +532,6 @@ static int swap_read_folio_zeromap(struct folio *folio)
 	struct obj_cgroup *objcg;
 	bool is_zeromap;
 
-	/*
-	 * Swapping in a large folio that is partially in the zeromap is not
-	 * currently handled. Return -EINVAL without marking the folio uptodate so
-	 * that an IO error is emitted (e.g. do_swap_page() will sigbus).
-	 */
 	if (WARN_ON_ONCE(swap_zeromap_batch(folio->swap, nr_pages,
 			&is_zeromap) != nr_pages)) {
 		folio_unlock(folio);
Signature · 1 line
2.43.5
author reviewer
#6 lore raw
In reply to Nhat Pham
Quoted 10 lines · Nhat Pham

Similar to zswap_load(), also return proper error codes for swap_read_folio_zeromap():

* 0 on success. The folio is unlocked and marked up-to-date.
* -ENOENT, if the folio is entirely not zeromapped.
* -EINVAL (with the follio unlocked but not marked to date), if the folio is partially zeromapped. This is not supported, and will SIGBUS the faulting process.

Quoted 8 lines

This patch is purely a clean-up, and should not have any behavioral change. It is based on (and should be applied on top of) [1].

[1]: https://lore.kernel.org/linux-mm/[email protected]/

Hi Andrew, I think Yosry and Johannes signed off on this patch (with the fixlet). Looks like it was not merged for 6.15-rc1 - let me know if there is something I need to do.

It's mostly a clean up with no (intended) behavioral change, so no rush of course.

maintainer
#7 lore raw
Quoted 16 lines · On Tue, 1 Apr 2025 11:55:31 -0700 Nhat Pham <[email protected]> wrote:
Quoted 15 lines · Nhat Pham

Similar to zswap_load(), also return proper error codes for swap_read_folio_zeromap():

* 0 on success. The folio is unlocked and marked up-to-date.
* -ENOENT, if the folio is entirely not zeromapped.
* -EINVAL (with the follio unlocked but not marked to date), if the folio is partially zeromapped. This is not supported, and will SIGBUS the faulting process.

This patch is purely a clean-up, and should not have any behavioral change. It is based on (and should be applied on top of) [1].

[1]: https://lore.kernel.org/linux-mm/[email protected]/

Quoted 7 lines
Quoted 3 lines

Hi Andrew, I think Yosry and Johannes signed off on this patch (with the fixlet). Looks like it was not merged for 6.15-rc1 - let me know if there is something I need to do.

Thanks, seems it fell through a crack.

Quoted 2 lines

It's mostly a clean up with no (intended) behavioral change, so no rush of course.

Yup, I'll toss it onto the after-merge-window pile.