Skip to contentSkip to conversation

PM: hibernate: clean up sync_read handling in snapshot_write_next

PATCH New Brian Geffon 2 participants 4 messages Sep 22, 2023 – Sep 26, 2023

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

PATCH 1/1 PM: hibernate: clean up sync_read handling in snapshot_write_next

In snapshot_write_next sync_read is set and unset in three different spots unnecessiarly. As a result there is a subtle bug where the first page after the meta data has been loaded unconditionally sets sync_read to 0. If this first pfn was actually a highmem page then the returned buffer will be the global "buffer," and the page needs to be loaded synchronously.

That is, I'm not sure we can always assume the following to be safe:

		handle->buffer = get_buffer(&orig_bm, &ca);
		handle->sync_read = 0;

Because get_buffer can call get_highmem_page_buffer which can return 'buffer'

The easiest way to address this is just set sync_read before snapshot_write_next returns if handle->buffer == buffer.

---
 kernel/power/snapshot.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
kernel/power/snapshot.c +1 -5
index 190ed707ddcc..362e6bae5891 100644
@@ -2780,8 +2780,6 @@ int snapshot_write_next(struct snapshot_handle *handle)
 	if (handle->cur > 1 && handle->cur > nr_meta_pages + nr_copy_pages + nr_zero_pages)
 		return 0;
 
-	handle->sync_read = 1;
-
 	if (!handle->cur) {
 		if (!buffer)
 			/* This makes the buffer be freed by swsusp_free() */
@@ -2824,7 +2822,6 @@ int snapshot_write_next(struct snapshot_handle *handle)
 			memory_bm_position_reset(&zero_bm);
 			restore_pblist = NULL;
 			handle->buffer = get_buffer(&orig_bm, &ca);
-			handle->sync_read = 0;
 			if (IS_ERR(handle->buffer))
 				return PTR_ERR(handle->buffer);
 		}
@@ -2834,9 +2831,8 @@ int snapshot_write_next(struct snapshot_handle *handle)
 		handle->buffer = get_buffer(&orig_bm, &ca);
 		if (IS_ERR(handle->buffer))
 			return PTR_ERR(handle->buffer);
-		if (handle->buffer != buffer)
-			handle->sync_read = 0;
 	}
+	handle->sync_read = (handle->buffer == buffer);
 	handle->cur++;
 
 	/* Zero pages were not included in the image, memset it and move on. */
Signature · 1 line
2.42.0.515.g380fc7ccd1-goog
#1 lore raw

PATCH 1/1 PM: hibernate: clean up sync_read handling in snapshot_write_next

In snapshot_write_next sync_read is set and unset in three different spots unnecessiarly. As a result there is a subtle bug where the first page after the meta data has been loaded unconditionally sets sync_read to 0. If this first pfn was actually a highmem page then the returned buffer will be the global "buffer," and the page needs to be loaded synchronously.

That is, I'm not sure we can always assume the following to be safe:

		handle->buffer = get_buffer(&orig_bm, &ca);
		handle->sync_read = 0;

Because get_buffer can call get_highmem_page_buffer which can return 'buffer'

The easiest way to address this is just set sync_read before snapshot_write_next returns if handle->buffer == buffer.

Fixes 8357376d3df2 ("[PATCH] swsusp: Improve handling of highmem")
---
 kernel/power/snapshot.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
kernel/power/snapshot.c +1 -5
index 190ed707ddcc..362e6bae5891 100644
@@ -2780,8 +2780,6 @@ int snapshot_write_next(struct snapshot_handle *handle)
 	if (handle->cur > 1 && handle->cur > nr_meta_pages + nr_copy_pages + nr_zero_pages)
 		return 0;
 
-	handle->sync_read = 1;
-
 	if (!handle->cur) {
 		if (!buffer)
 			/* This makes the buffer be freed by swsusp_free() */
@@ -2824,7 +2822,6 @@ int snapshot_write_next(struct snapshot_handle *handle)
 			memory_bm_position_reset(&zero_bm);
 			restore_pblist = NULL;
 			handle->buffer = get_buffer(&orig_bm, &ca);
-			handle->sync_read = 0;
 			if (IS_ERR(handle->buffer))
 				return PTR_ERR(handle->buffer);
 		}
@@ -2834,9 +2831,8 @@ int snapshot_write_next(struct snapshot_handle *handle)
 		handle->buffer = get_buffer(&orig_bm, &ca);
 		if (IS_ERR(handle->buffer))
 			return PTR_ERR(handle->buffer);
-		if (handle->buffer != buffer)
-			handle->sync_read = 0;
 	}
+	handle->sync_read = (handle->buffer == buffer);
 	handle->cur++;
 
 	/* Zero pages were not included in the image, memset it and move on. */
Signature · 1 line
2.42.0.515.g380fc7ccd1-goog
#2 lore raw
Quoted 15 lines · Brian Geffon

In snapshot_write_next sync_read is set and unset in three different spots unnecessiarly. As a result there is a subtle bug where the first page after the meta data has been loaded unconditionally sets sync_read to 0. If this first pfn was actually a highmem page then the returned buffer will be the global "buffer," and the page needs to be loaded synchronously.

That is, I'm not sure we can always assume the following to be safe:

                handle->buffer = get_buffer(&orig_bm, &ca);
                handle->sync_read = 0;

Because get_buffer can call get_highmem_page_buffer which can return 'buffer'

Quoted 6 lines

The easiest way to address this is just set sync_read before snapshot_write_next returns if handle->buffer == buffer.

Fixes 8357376d3df2 ("[PATCH] swsusp: Improve handling of highmem")

If you send an update of a patch, it is always better to give it a higher version number to avoid any possible confusion.

Quoted 27 lines
---
 kernel/power/snapshot.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
kernel/power/snapshot.c +0 -3
index 190ed707ddcc..362e6bae5891 100644
@@ -2780,8 +2780,6 @@ int snapshot_write_next(struct snapshot_handle *handle)
        if (handle->cur > 1 && handle->cur > nr_meta_pages + nr_copy_pages + nr_zero_pages)
                return 0;
 
-       handle->sync_read = 1;
-
        if (!handle->cur) {
                if (!buffer)
                        /* This makes the buffer be freed by swsusp_free() */
@@ -2824,7 +2822,6 @@ int snapshot_write_next(struct snapshot_handle *handle)
                        memory_bm_position_reset(&zero_bm);
                        restore_pblist = NULL;
                        handle->buffer = get_buffer(&orig_bm, &ca);
-                       handle->sync_read = 0;
                        if (IS_ERR(handle->buffer))
                                return PTR_ERR(handle->buffer);
                }
@@ -2834,9 +2831,8 @@ int snapshot_write_next(struct snapshot_handle *handle)
                handle->buffer = get_buffer(&orig_bm, &ca);

kernel/power/snapshot.c int snapshot_write_next(struct snapshot_handle *handle)

Quoted 10 lines
kernel/power/snapshot.c +1 -3
                if (IS_ERR(handle->buffer))
                        return PTR_ERR(handle->buffer);
-               if (handle->buffer != buffer)
-                       handle->sync_read = 0;
        }
+       handle->sync_read = (handle->buffer == buffer);
        handle->cur++;
 
        /* Zero pages were not included in the image, memset it and move on. */
--

Anyway, applied as 6.7 material with some minor edits in the subject and changelog.

Thanks!

#3 lore raw
Quoted 20 lines · Rafael J. Wysocki
Quoted 18 lines · Brian Geffon

In snapshot_write_next sync_read is set and unset in three different spots unnecessiarly. As a result there is a subtle bug where the first page after the meta data has been loaded unconditionally sets sync_read to 0. If this first pfn was actually a highmem page then the returned buffer will be the global "buffer," and the page needs to be loaded synchronously.

That is, I'm not sure we can always assume the following to be safe:

                handle->buffer = get_buffer(&orig_bm, &ca);
                handle->sync_read = 0;

Because get_buffer can call get_highmem_page_buffer which can return 'buffer'

The easiest way to address this is just set sync_read before snapshot_write_next returns if handle->buffer == buffer.

Quoted 6 lines
Quoted 3 lines
Fixes 8357376d3df2 ("[PATCH] swsusp: Improve handling of highmem")

If you send an update of a patch, it is always better to give it a higher version number to avoid any possible confusion.

Yes, I apologize. I wanted to add a Fixes line and I wasn't sure what the best practice was. I'll do my best to not create any additional work for you going forward!

Quoted 36 lines
Quoted 36 lines
---
 kernel/power/snapshot.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
kernel/power/snapshot.c +1 -5
index 190ed707ddcc..362e6bae5891 100644
@@ -2780,8 +2780,6 @@ int snapshot_write_next(struct snapshot_handle *handle)
        if (handle->cur > 1 && handle->cur > nr_meta_pages + nr_copy_pages + nr_zero_pages)
                return 0;
 
-       handle->sync_read = 1;
-
        if (!handle->cur) {
                if (!buffer)
                        /* This makes the buffer be freed by swsusp_free() */
@@ -2824,7 +2822,6 @@ int snapshot_write_next(struct snapshot_handle *handle)
                        memory_bm_position_reset(&zero_bm);
                        restore_pblist = NULL;
                        handle->buffer = get_buffer(&orig_bm, &ca);
-                       handle->sync_read = 0;
                        if (IS_ERR(handle->buffer))
                                return PTR_ERR(handle->buffer);
                }
@@ -2834,9 +2831,8 @@ int snapshot_write_next(struct snapshot_handle *handle)
                handle->buffer = get_buffer(&orig_bm, &ca);
                if (IS_ERR(handle->buffer))
                        return PTR_ERR(handle->buffer);
-               if (handle->buffer != buffer)
-                       handle->sync_read = 0;
        }
+       handle->sync_read = (handle->buffer == buffer);
        handle->cur++;
 
Quoted 5 lines

kernel/power/snapshot.c int snapshot_write_next(struct snapshot_handle *handle)

Quoted 2 lines
kernel/power/snapshot.c +0 -1
        /* Zero pages were not included in the image, memset it and move on. */
--

Anyway, applied as 6.7 material with some minor edits in the subject and changelog.

Thank you for taking the time to review!
Cheers

Quoted 2 lines

Thanks!