Skip to content

Commit dfbca61

Browse files
mrutland-armctmarinas
authored andcommitted
arm64: hibernate: handle allocation failures
In create_safe_exec_page(), we create a copy of the hibernate exit text, along with some page tables to map this via TTBR0. We then install the new tables in TTBR0. In swsusp_arch_resume() we call create_safe_exec_page() before trying a number of operations which may fail (e.g. copying the linear map page tables). If these fail, we bail out of swsusp_arch_resume() and return an error code, but leave TTBR0 as-is. Subsequently, the core hibernate code will call free_basic_memory_bitmaps(), which will free all of the memory allocations we made, including the page tables installed in TTBR0. Thus, we may have TTBR0 pointing at dangling freed memory for some period of time. If the hibernate attempt was triggered by a user requesting a hibernate test via the reboot syscall, we may return to userspace with the clobbered TTBR0 value. Avoid these issues by reorganising swsusp_arch_resume() such that we have no failure paths after create_safe_exec_page(). We also add a check that the zero page allocation succeeded, matching what we have for other allocations. Fixes: 82869ac ("arm64: kernel: Add support for hibernate/suspend-to-disk") Signed-off-by: Mark Rutland <mark.rutland@arm.com> Acked-by: James Morse <james.morse@arm.com> Cc: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com> Cc: Will Deacon <will.deacon@arm.com> Cc: <stable@vger.kernel.org> # 4.7+ Signed-off-by: Catalin Marinas <catalin.marinas@arm.com>
1 parent 0194e76 commit dfbca61

File tree

1 file changed

+32
-27
lines changed

1 file changed

+32
-27
lines changed

arch/arm64/kernel/hibernate.c

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,38 @@ int swsusp_arch_resume(void)
404404
void __noreturn (*hibernate_exit)(phys_addr_t, phys_addr_t, void *,
405405
void *, phys_addr_t, phys_addr_t);
406406

407+
/*
408+
* Restoring the memory image will overwrite the ttbr1 page tables.
409+
* Create a second copy of just the linear map, and use this when
410+
* restoring.
411+
*/
412+
tmp_pg_dir = (pgd_t *)get_safe_page(GFP_ATOMIC);
413+
if (!tmp_pg_dir) {
414+
pr_err("Failed to allocate memory for temporary page tables.");
415+
rc = -ENOMEM;
416+
goto out;
417+
}
418+
rc = copy_page_tables(tmp_pg_dir, PAGE_OFFSET, 0);
419+
if (rc)
420+
goto out;
421+
422+
/*
423+
* Since we only copied the linear map, we need to find restore_pblist's
424+
* linear map address.
425+
*/
426+
lm_restore_pblist = LMADDR(restore_pblist);
427+
428+
/*
429+
* We need a zero page that is zero before & after resume in order to
430+
* to break before make on the ttbr1 page tables.
431+
*/
432+
zero_page = (void *)get_safe_page(GFP_ATOMIC);
433+
if (!zero_page) {
434+
pr_err("Failed to allocate zero page.");
435+
rc = -ENOMEM;
436+
goto out;
437+
}
438+
407439
/*
408440
* Locate the exit code in the bottom-but-one page, so that *NULL
409441
* still has disastrous affects.
@@ -429,27 +461,6 @@ int swsusp_arch_resume(void)
429461
*/
430462
__flush_dcache_area(hibernate_exit, exit_size);
431463

432-
/*
433-
* Restoring the memory image will overwrite the ttbr1 page tables.
434-
* Create a second copy of just the linear map, and use this when
435-
* restoring.
436-
*/
437-
tmp_pg_dir = (pgd_t *)get_safe_page(GFP_ATOMIC);
438-
if (!tmp_pg_dir) {
439-
pr_err("Failed to allocate memory for temporary page tables.");
440-
rc = -ENOMEM;
441-
goto out;
442-
}
443-
rc = copy_page_tables(tmp_pg_dir, PAGE_OFFSET, 0);
444-
if (rc)
445-
goto out;
446-
447-
/*
448-
* Since we only copied the linear map, we need to find restore_pblist's
449-
* linear map address.
450-
*/
451-
lm_restore_pblist = LMADDR(restore_pblist);
452-
453464
/*
454465
* KASLR will cause the el2 vectors to be in a different location in
455466
* the resumed kernel. Load hibernate's temporary copy into el2.
@@ -464,12 +475,6 @@ int swsusp_arch_resume(void)
464475
__hyp_set_vectors(el2_vectors);
465476
}
466477

467-
/*
468-
* We need a zero page that is zero before & after resume in order to
469-
* to break before make on the ttbr1 page tables.
470-
*/
471-
zero_page = (void *)get_safe_page(GFP_ATOMIC);
472-
473478
hibernate_exit(virt_to_phys(tmp_pg_dir), resume_hdr.ttbr1_el1,
474479
resume_hdr.reenter_kernel, lm_restore_pblist,
475480
resume_hdr.__hyp_stub_vectors, virt_to_phys(zero_page));

0 commit comments

Comments
 (0)