Skip to contentSkip to conversation

PM: hibernate: Drain trailing zero pages on userspace restore

PATCH Superseded Alberto Garcia 2 participants 5 messages Mar 9

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 PM: hibernate: Drain trailing zero pages on userspace restore

Commit 005e8dddd497 ("PM: hibernate: don't store zero pages in the image file") added an optimization to skip zero-filled pages in the hibernation image. On restore, zero pages are handled internally by snapshot_write_next() in a loop that processes them without returning to the caller.

With the userspace restore interface, writing the last non-zero page to /dev/snapshot is followed by the SNAPSHOT_ATOMIC_RESTORE ioctl. At this point there are no more calls to snapshot_write_next() so any trailing zero pages are not processed, snapshot_image_loaded() fails because handle->cur is smaller than expected, the ioctl returns -EPERM and the image is not restored.

The in-kernel restore path is not affected by this because the loop in load_image() in swap.c calls snapshot_write_next() until it returns 0. It is this final call that drains any trailing zero pages.

Fixed by calling snapshot_write_next() in the SNAPSHOT_ATOMIC_RESTORE handler before snapshot_write_finalize(), giving the kernel the chance to process any trailing zero pages.

Fixes 005e8dddd497 ("PM: hibernate: don't store zero pages in the image file")
---
 kernel/power/user.c | 8 ++++++++
 1 file changed, 8 insertions(+)
kernel/power/user.c +8 -0
index 4401cfe26e5c..d5f189c4c93e 100644
@@ -319,6 +319,14 @@ static long snapshot_ioctl(struct file *filp, unsigned int cmd,
 		break;
 
 	case SNAPSHOT_ATOMIC_RESTORE:
+		/*
+		 * We need to call snapshot_write_next() one last time
+		 * before finalizing in order to process any trailing
+		 * zero pages.
+		 */
+		error = snapshot_write_next(&data->handle);
+		if (error < 0)
+			break;
 		error = snapshot_write_finalize(&data->handle);
 		if (error)
 			break;
Signature · 1 line
2.47.3
#1 lore raw

kernel/power/user.c static long snapshot_ioctl(struct file *filp, unsigned int cmd, · line 329

Quoted 9 lines · Alberto Garcia
kernel/power/user.c +8 -0
 	case SNAPSHOT_ATOMIC_RESTORE:
+		/*
+		 * We need to call snapshot_write_next() one last time
+		 * before finalizing in order to process any trailing
+		 * zero pages.
+		 */
+		error = snapshot_write_next(&data->handle);
+		if (error < 0)
+			break;

I realized that this patch assumes that userspace calls SNAPSHOT_ATOMIC_RESTORE only after having written the full image, but if that happens earlier I'm not sure that it's safe to call snapshot_write_next().

An alternative solution would be to do it at the beginning of snapshot_write_finalize() if handle->cur > nr_meta_pages + 1.

If you think that's a better idea I can send v2 of the patch.

Regards,

Berto

#2 lore raw
Quoted 12 lines · Alberto Garcia

kernel/power/user.c static long snapshot_ioctl(struct file *filp, unsigned int cmd, · line 329

Quoted 9 lines · Alberto Garcia

case SNAPSHOT_ATOMIC_RESTORE:

kernel/power/user.c +8 -0
+             /*
+              * We need to call snapshot_write_next() one last time
+              * before finalizing in order to process any trailing
+              * zero pages.
+              */
+             error = snapshot_write_next(&data->handle);
+             if (error < 0)
+                     break;
Quoted 7 lines

I realized that this patch assumes that userspace calls SNAPSHOT_ATOMIC_RESTORE only after having written the full image, but if that happens earlier I'm not sure that it's safe to call snapshot_write_next().

An alternative solution would be to do it at the beginning of snapshot_write_finalize() if handle->cur > nr_meta_pages + 1.

I think this makes sense, but if snapshot_write_next() returns PAGE_SIZE then we need to indicate some error (eg. -ENODATA) in that the kernel was still expecting another copy page.

Also it appears that the kernel restore path and the userspace restore path return two different error codes for !snapshot_image_loaded(), the former does -ENODATA and the latter -EPERM.

Quoted 6 lines

If you think that's a better idea I can send v2 of the patch.

Regards,

Berto

#3 lore raw
Quoted 6 lines · Brian Geffon
Quoted 2 lines

An alternative solution would be to do it at the beginning of snapshot_write_finalize() if handle->cur > nr_meta_pages + 1.

I think this makes sense, but if snapshot_write_next() returns PAGE_SIZE then we need to indicate some error (eg. -ENODATA) in that the kernel was still expecting another copy page.

This would fail anyway in snapshot_image_loaded() because of this condition:

handle->cur <= nr_meta_pages + nr_copy_pages + nr_zero_pages

But I think it make sense to return an error directly from snapshot_write_finalize() as you suggest, I can use -ENODATA.

Quoted 3 lines

Also it appears that the kernel restore path and the userspace restore path return two different error codes for !snapshot_image_loaded(), the former does -ENODATA and the latter -EPERM.

Yes, -EPERM is the one that I hit in my tests.

Does this need to be corrected?

Berto

#4 lore raw
Quoted 5 lines · Alberto Garcia
Quoted 3 lines · Brian Geffon
Quoted 2 lines

An alternative solution would be to do it at the beginning of snapshot_write_finalize() if handle->cur > nr_meta_pages + 1.

Quoted 8 lines
Quoted 3 lines

I think this makes sense, but if snapshot_write_next() returns PAGE_SIZE then we need to indicate some error (eg. -ENODATA) in that the kernel was still expecting another copy page.

This would fail anyway in snapshot_image_loaded() because of this condition:

   handle->cur <= nr_meta_pages + nr_copy_pages + nr_zero_pages

A subtle bug exists where if you had exactly 1 page remaining (which was to be copied) handle->cur would have been incremented in snapshot_write_next(), however, no data would have been copied into that page.

Quoted 3 lines

But I think it make sense to return an error directly from snapshot_write_finalize() as you suggest, I can use -ENODATA.

Yes, I think the safest option is if you call snapshot_write_finalize() and snapshot_write_next() returns a positive value you return -ENODATA.

Quoted 8 lines
Quoted 4 lines

Also it appears that the kernel restore path and the userspace restore path return two different error codes for !snapshot_image_loaded(), the former does -ENODATA and the latter -EPERM.

Yes, -EPERM is the one that I hit in my tests.

Does this need to be corrected?

Yes, I think we should change that code to check if !snapshot_image_loaded() and explicitly return -ENODATA rather than -EPERM which is meant for cases where the operation is neither write-only nor ready. This new explicit check should happen after that.

Quoted 2 lines

Berto