Skip to contentSkip to conversation

PM: hibernate: Fix memory leak in snapshot_write_next() error path

PATCH Applied Malaya Kumar Rout 3 participants 3 messages Jul 11 – Jul 23

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

PATCH 1/1 PM: hibernate: Fix memory leak in snapshot_write_next() error path

When memory_bm_create() succeeds for copy_bm but fails for zero_bm, the function returns without freeing the resources allocated for copy_bm. This results in a memory leak that includes radix tree nodes, zone structures, and page lists.

Fix this by calling memory_bm_free() to release copy_bm's resources before returning the error code when zero_bm allocation fails.

Fixes 005e8dddd497 ("PM: hibernate: don't store zero pages in the image file")
---
 kernel/power/snapshot.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
kernel/power/snapshot.c +3 -2
index d933b5b2c05d..4a73927cc55b 100644
@@ -2797,9 +2797,10 @@ int snapshot_write_next(struct snapshot_handle *handle)
 			return error;
 
 		error = memory_bm_create(&zero_bm, GFP_ATOMIC, PG_ANY);
-		if (error)
+		if (error) {
+			memory_bm_free(&copy_bm, PG_UNSAFE_CLEAR);
 			return error;
-
+		}
 		nr_zero_pages = 0;
 
 		hibernate_restore_protection_begin();
Signature · 1 line
2.54.0
Acked-by on v1 1/1
#1 lore raw
Quoted 11 lines · Malaya Kumar Rout

When memory_bm_create() succeeds for copy_bm but fails for zero_bm, the function returns without freeing the resources allocated for copy_bm. This results in a memory leak that includes radix tree nodes, zone structures, and page lists.

Fix this by calling memory_bm_free() to release copy_bm's resources before returning the error code when zero_bm allocation fails.

Fixes 005e8dddd497 ("PM: hibernate: don't store zero pages in the image file")
Quoted 25 lines
---
 kernel/power/snapshot.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
kernel/power/snapshot.c +3 -3
index d933b5b2c05d..4a73927cc55b 100644
@@ -2797,9 +2797,10 @@ int snapshot_write_next(struct snapshot_handle *handle)
                        return error;
 
                error = memory_bm_create(&zero_bm, GFP_ATOMIC, PG_ANY);
-               if (error)
+               if (error) {
+                       memory_bm_free(&copy_bm, PG_UNSAFE_CLEAR);
                        return error;
-
+               }
                nr_zero_pages = 0;
 
                hibernate_restore_protection_begin();
--

2.54.0

maintainer Applied
#2 lore raw
Quoted 9 lines · Brian Geffon
Quoted 6 lines · Malaya Kumar Rout

When memory_bm_create() succeeds for copy_bm but fails for zero_bm, the function returns without freeing the resources allocated for copy_bm. This results in a memory leak that includes radix tree nodes, zone structures, and page lists.

Quoted 7 lines
Quoted 5 lines

Fix this by calling memory_bm_free() to release copy_bm's resources before returning the error code when zero_bm allocation fails.

Fixes 005e8dddd497 ("PM: hibernate: don't store zero pages in the image file")

Applied as 7.3 material, thanks!

Quoted 24 lines
Quoted 24 lines
---
 kernel/power/snapshot.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
kernel/power/snapshot.c +3 -3
index d933b5b2c05d..4a73927cc55b 100644
@@ -2797,9 +2797,10 @@ int snapshot_write_next(struct snapshot_handle *handle)
                        return error;
 
                error = memory_bm_create(&zero_bm, GFP_ATOMIC, PG_ANY);
-               if (error)
+               if (error) {
+                       memory_bm_free(&copy_bm, PG_UNSAFE_CLEAR);
                        return error;
-
+               }
                nr_zero_pages = 0;
 
                hibernate_restore_protection_begin();
--

2.54.0