| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * linux/kernel/power/snapshot.c |
|---|
| 3 | 4 | * |
|---|
| .. | .. |
|---|
| 5 | 6 | * |
|---|
| 6 | 7 | * Copyright (C) 1998-2005 Pavel Machek <pavel@ucw.cz> |
|---|
| 7 | 8 | * Copyright (C) 2006 Rafael J. Wysocki <rjw@sisk.pl> |
|---|
| 8 | | - * |
|---|
| 9 | | - * This file is released under the GPLv2. |
|---|
| 10 | | - * |
|---|
| 11 | 9 | */ |
|---|
| 12 | 10 | |
|---|
| 13 | | -#define pr_fmt(fmt) "PM: " fmt |
|---|
| 11 | +#define pr_fmt(fmt) "PM: hibernation: " fmt |
|---|
| 14 | 12 | |
|---|
| 15 | 13 | #include <linux/version.h> |
|---|
| 16 | 14 | #include <linux/module.h> |
|---|
| .. | .. |
|---|
| 23 | 21 | #include <linux/pm.h> |
|---|
| 24 | 22 | #include <linux/device.h> |
|---|
| 25 | 23 | #include <linux/init.h> |
|---|
| 26 | | -#include <linux/bootmem.h> |
|---|
| 24 | +#include <linux/memblock.h> |
|---|
| 27 | 25 | #include <linux/nmi.h> |
|---|
| 28 | 26 | #include <linux/syscalls.h> |
|---|
| 29 | 27 | #include <linux/console.h> |
|---|
| .. | .. |
|---|
| 36 | 34 | |
|---|
| 37 | 35 | #include <linux/uaccess.h> |
|---|
| 38 | 36 | #include <asm/mmu_context.h> |
|---|
| 39 | | -#include <asm/pgtable.h> |
|---|
| 40 | 37 | #include <asm/tlbflush.h> |
|---|
| 41 | 38 | #include <asm/io.h> |
|---|
| 42 | 39 | |
|---|
| .. | .. |
|---|
| 105 | 102 | |
|---|
| 106 | 103 | void __init hibernate_image_size_init(void) |
|---|
| 107 | 104 | { |
|---|
| 108 | | - image_size = ((totalram_pages * 2) / 5) * PAGE_SIZE; |
|---|
| 105 | + image_size = ((totalram_pages() * 2) / 5) * PAGE_SIZE; |
|---|
| 109 | 106 | } |
|---|
| 110 | 107 | |
|---|
| 111 | 108 | /* |
|---|
| .. | .. |
|---|
| 738 | 735 | */ |
|---|
| 739 | 736 | |
|---|
| 740 | 737 | /* |
|---|
| 741 | | - * If the zone we wish to scan is the the current zone and the |
|---|
| 738 | + * If the zone we wish to scan is the current zone and the |
|---|
| 742 | 739 | * pfn falls into the current node then we do not need to walk |
|---|
| 743 | 740 | * the tree. |
|---|
| 744 | 741 | */ |
|---|
| .. | .. |
|---|
| 947 | 944 | * Register a range of page frames the contents of which should not be saved |
|---|
| 948 | 945 | * during hibernation (to be used in the early initialization code). |
|---|
| 949 | 946 | */ |
|---|
| 950 | | -void __init __register_nosave_region(unsigned long start_pfn, |
|---|
| 951 | | - unsigned long end_pfn, int use_kmalloc) |
|---|
| 947 | +void __init register_nosave_region(unsigned long start_pfn, unsigned long end_pfn) |
|---|
| 952 | 948 | { |
|---|
| 953 | 949 | struct nosave_region *region; |
|---|
| 954 | 950 | |
|---|
| .. | .. |
|---|
| 964 | 960 | goto Report; |
|---|
| 965 | 961 | } |
|---|
| 966 | 962 | } |
|---|
| 967 | | - if (use_kmalloc) { |
|---|
| 968 | | - /* During init, this shouldn't fail */ |
|---|
| 969 | | - region = kmalloc(sizeof(struct nosave_region), GFP_KERNEL); |
|---|
| 970 | | - BUG_ON(!region); |
|---|
| 971 | | - } else { |
|---|
| 972 | | - /* This allocation cannot fail */ |
|---|
| 973 | | - region = memblock_virt_alloc(sizeof(struct nosave_region), 0); |
|---|
| 974 | | - } |
|---|
| 963 | + /* This allocation cannot fail */ |
|---|
| 964 | + region = memblock_alloc(sizeof(struct nosave_region), |
|---|
| 965 | + SMP_CACHE_BYTES); |
|---|
| 966 | + if (!region) |
|---|
| 967 | + panic("%s: Failed to allocate %zu bytes\n", __func__, |
|---|
| 968 | + sizeof(struct nosave_region)); |
|---|
| 975 | 969 | region->start_pfn = start_pfn; |
|---|
| 976 | 970 | region->end_pfn = end_pfn; |
|---|
| 977 | 971 | list_add_tail(®ion->list, &nosave_regions); |
|---|
| .. | .. |
|---|
| 1143 | 1137 | pr_debug("Basic memory bitmaps freed\n"); |
|---|
| 1144 | 1138 | } |
|---|
| 1145 | 1139 | |
|---|
| 1146 | | -void clear_free_pages(void) |
|---|
| 1140 | +static void clear_or_poison_free_page(struct page *page) |
|---|
| 1141 | +{ |
|---|
| 1142 | + if (page_poisoning_enabled_static()) |
|---|
| 1143 | + __kernel_poison_pages(page, 1); |
|---|
| 1144 | + else if (want_init_on_free()) |
|---|
| 1145 | + clear_highpage(page); |
|---|
| 1146 | +} |
|---|
| 1147 | + |
|---|
| 1148 | +void clear_or_poison_free_pages(void) |
|---|
| 1147 | 1149 | { |
|---|
| 1148 | 1150 | struct memory_bitmap *bm = free_pages_map; |
|---|
| 1149 | 1151 | unsigned long pfn; |
|---|
| .. | .. |
|---|
| 1151 | 1153 | if (WARN_ON(!(free_pages_map))) |
|---|
| 1152 | 1154 | return; |
|---|
| 1153 | 1155 | |
|---|
| 1154 | | - if (IS_ENABLED(CONFIG_PAGE_POISONING_ZERO) || want_init_on_free()) { |
|---|
| 1156 | + if (page_poisoning_enabled() || want_init_on_free()) { |
|---|
| 1155 | 1157 | memory_bm_position_reset(bm); |
|---|
| 1156 | 1158 | pfn = memory_bm_next_pfn(bm); |
|---|
| 1157 | 1159 | while (pfn != BM_END_OF_MAP) { |
|---|
| 1158 | 1160 | if (pfn_valid(pfn)) |
|---|
| 1159 | | - clear_highpage(pfn_to_page(pfn)); |
|---|
| 1161 | + clear_or_poison_free_page(pfn_to_page(pfn)); |
|---|
| 1160 | 1162 | |
|---|
| 1161 | 1163 | pfn = memory_bm_next_pfn(bm); |
|---|
| 1162 | 1164 | } |
|---|
| .. | .. |
|---|
| 1221 | 1223 | if (!pfn_valid(pfn)) |
|---|
| 1222 | 1224 | return NULL; |
|---|
| 1223 | 1225 | |
|---|
| 1224 | | - page = pfn_to_page(pfn); |
|---|
| 1225 | | - if (page_zone(page) != zone) |
|---|
| 1226 | + page = pfn_to_online_page(pfn); |
|---|
| 1227 | + if (!page || page_zone(page) != zone) |
|---|
| 1226 | 1228 | return NULL; |
|---|
| 1227 | 1229 | |
|---|
| 1228 | 1230 | BUG_ON(!PageHighMem(page)); |
|---|
| 1229 | 1231 | |
|---|
| 1230 | | - if (swsusp_page_is_forbidden(page) || swsusp_page_is_free(page) || |
|---|
| 1231 | | - PageReserved(page)) |
|---|
| 1232 | + if (swsusp_page_is_forbidden(page) || swsusp_page_is_free(page)) |
|---|
| 1233 | + return NULL; |
|---|
| 1234 | + |
|---|
| 1235 | + if (PageReserved(page) || PageOffline(page)) |
|---|
| 1232 | 1236 | return NULL; |
|---|
| 1233 | 1237 | |
|---|
| 1234 | 1238 | if (page_is_guard(page)) |
|---|
| .. | .. |
|---|
| 1283 | 1287 | if (!pfn_valid(pfn)) |
|---|
| 1284 | 1288 | return NULL; |
|---|
| 1285 | 1289 | |
|---|
| 1286 | | - page = pfn_to_page(pfn); |
|---|
| 1287 | | - if (page_zone(page) != zone) |
|---|
| 1290 | + page = pfn_to_online_page(pfn); |
|---|
| 1291 | + if (!page || page_zone(page) != zone) |
|---|
| 1288 | 1292 | return NULL; |
|---|
| 1289 | 1293 | |
|---|
| 1290 | 1294 | BUG_ON(PageHighMem(page)); |
|---|
| 1291 | 1295 | |
|---|
| 1292 | 1296 | if (swsusp_page_is_forbidden(page) || swsusp_page_is_free(page)) |
|---|
| 1297 | + return NULL; |
|---|
| 1298 | + |
|---|
| 1299 | + if (PageOffline(page)) |
|---|
| 1293 | 1300 | return NULL; |
|---|
| 1294 | 1301 | |
|---|
| 1295 | 1302 | if (PageReserved(page) |
|---|
| .. | .. |
|---|
| 1340 | 1347 | * safe_copy_page - Copy a page in a safe way. |
|---|
| 1341 | 1348 | * |
|---|
| 1342 | 1349 | * Check if the page we are going to copy is marked as present in the kernel |
|---|
| 1343 | | - * page tables (this always is the case if CONFIG_DEBUG_PAGEALLOC is not set |
|---|
| 1344 | | - * and in that case kernel_page_present() always returns 'true'). |
|---|
| 1350 | + * page tables. This always is the case if CONFIG_DEBUG_PAGEALLOC or |
|---|
| 1351 | + * CONFIG_ARCH_HAS_SET_DIRECT_MAP is not set. In that case kernel_page_present() |
|---|
| 1352 | + * always returns 'true'. |
|---|
| 1345 | 1353 | */ |
|---|
| 1346 | 1354 | static void safe_copy_page(void *dst, struct page *s_page) |
|---|
| 1347 | 1355 | { |
|---|
| .. | .. |
|---|
| 1558 | 1566 | */ |
|---|
| 1559 | 1567 | static unsigned long __fraction(u64 x, u64 multiplier, u64 base) |
|---|
| 1560 | 1568 | { |
|---|
| 1561 | | - x *= multiplier; |
|---|
| 1562 | | - do_div(x, base); |
|---|
| 1563 | | - return (unsigned long)x; |
|---|
| 1569 | + return div64_u64(x * multiplier, base); |
|---|
| 1564 | 1570 | } |
|---|
| 1565 | 1571 | |
|---|
| 1566 | 1572 | static unsigned long preallocate_highmem_fraction(unsigned long nr_pages, |
|---|
| .. | .. |
|---|
| 1658 | 1664 | { |
|---|
| 1659 | 1665 | unsigned long size; |
|---|
| 1660 | 1666 | |
|---|
| 1661 | | - size = global_node_page_state(NR_SLAB_RECLAIMABLE) |
|---|
| 1667 | + size = global_node_page_state_pages(NR_SLAB_RECLAIMABLE_B) |
|---|
| 1662 | 1668 | + global_node_page_state(NR_ACTIVE_ANON) |
|---|
| 1663 | 1669 | + global_node_page_state(NR_INACTIVE_ANON) |
|---|
| 1664 | 1670 | + global_node_page_state(NR_ACTIVE_FILE) |
|---|
| .. | .. |
|---|
| 1675 | 1681 | * hibernation for allocations made while saving the image and for device |
|---|
| 1676 | 1682 | * drivers, in case they need to allocate memory from their hibernation |
|---|
| 1677 | 1683 | * callbacks (these two numbers are given by PAGES_FOR_IO (which is a rough |
|---|
| 1678 | | - * estimate) and reserverd_size divided by PAGE_SIZE (which is tunable through |
|---|
| 1684 | + * estimate) and reserved_size divided by PAGE_SIZE (which is tunable through |
|---|
| 1679 | 1685 | * /sys/power/reserved_size, respectively). To make this happen, we compute the |
|---|
| 1680 | 1686 | * total number of available page frames and allocate at least |
|---|
| 1681 | 1687 | * |
|---|
| 1682 | | - * ([page frames total] + PAGES_FOR_IO + [metadata pages]) / 2 |
|---|
| 1683 | | - * + 2 * DIV_ROUND_UP(reserved_size, PAGE_SIZE) |
|---|
| 1688 | + * ([page frames total] - PAGES_FOR_IO - [metadata pages]) / 2 |
|---|
| 1689 | + * - 2 * DIV_ROUND_UP(reserved_size, PAGE_SIZE) |
|---|
| 1684 | 1690 | * |
|---|
| 1685 | 1691 | * of them, which corresponds to the maximum size of a hibernation image. |
|---|
| 1686 | 1692 | * |
|---|
| .. | .. |
|---|
| 1697 | 1703 | ktime_t start, stop; |
|---|
| 1698 | 1704 | int error; |
|---|
| 1699 | 1705 | |
|---|
| 1700 | | - pr_info("Preallocating image memory... "); |
|---|
| 1706 | + pr_info("Preallocating image memory\n"); |
|---|
| 1701 | 1707 | start = ktime_get(); |
|---|
| 1702 | 1708 | |
|---|
| 1703 | 1709 | error = memory_bm_create(&orig_bm, GFP_IMAGE, PG_ANY); |
|---|
| 1704 | | - if (error) |
|---|
| 1710 | + if (error) { |
|---|
| 1711 | + pr_err("Cannot allocate original bitmap\n"); |
|---|
| 1705 | 1712 | goto err_out; |
|---|
| 1713 | + } |
|---|
| 1706 | 1714 | |
|---|
| 1707 | 1715 | error = memory_bm_create(©_bm, GFP_IMAGE, PG_ANY); |
|---|
| 1708 | | - if (error) |
|---|
| 1716 | + if (error) { |
|---|
| 1717 | + pr_err("Cannot allocate copy bitmap\n"); |
|---|
| 1709 | 1718 | goto err_out; |
|---|
| 1719 | + } |
|---|
| 1710 | 1720 | |
|---|
| 1711 | 1721 | alloc_normal = 0; |
|---|
| 1712 | 1722 | alloc_highmem = 0; |
|---|
| .. | .. |
|---|
| 1733 | 1743 | avail_normal = count; |
|---|
| 1734 | 1744 | count += highmem; |
|---|
| 1735 | 1745 | count -= totalreserve_pages; |
|---|
| 1736 | | - |
|---|
| 1737 | | - /* Add number of pages required for page keys (s390 only). */ |
|---|
| 1738 | | - size += page_key_additional_pages(saveable); |
|---|
| 1739 | 1746 | |
|---|
| 1740 | 1747 | /* Compute the maximum number of saveable pages to leave in memory. */ |
|---|
| 1741 | 1748 | max_size = (count - (size + PAGES_FOR_IO)) / 2 |
|---|
| .. | .. |
|---|
| 1796 | 1803 | alloc -= pages; |
|---|
| 1797 | 1804 | pages += pages_highmem; |
|---|
| 1798 | 1805 | pages_highmem = preallocate_image_highmem(alloc); |
|---|
| 1799 | | - if (pages_highmem < alloc) |
|---|
| 1806 | + if (pages_highmem < alloc) { |
|---|
| 1807 | + pr_err("Image allocation is %lu pages short\n", |
|---|
| 1808 | + alloc - pages_highmem); |
|---|
| 1800 | 1809 | goto err_out; |
|---|
| 1810 | + } |
|---|
| 1801 | 1811 | pages += pages_highmem; |
|---|
| 1802 | 1812 | /* |
|---|
| 1803 | 1813 | * size is the desired number of saveable pages to leave in |
|---|
| .. | .. |
|---|
| 1828 | 1838 | |
|---|
| 1829 | 1839 | out: |
|---|
| 1830 | 1840 | stop = ktime_get(); |
|---|
| 1831 | | - pr_cont("done (allocated %lu pages)\n", pages); |
|---|
| 1841 | + pr_info("Allocated %lu pages for snapshot\n", pages); |
|---|
| 1832 | 1842 | swsusp_show_speed(start, stop, pages, "Allocated"); |
|---|
| 1833 | 1843 | |
|---|
| 1834 | 1844 | return 0; |
|---|
| 1835 | 1845 | |
|---|
| 1836 | 1846 | err_out: |
|---|
| 1837 | | - pr_cont("\n"); |
|---|
| 1838 | 1847 | swsusp_free(); |
|---|
| 1839 | 1848 | return -ENOMEM; |
|---|
| 1840 | 1849 | } |
|---|
| .. | .. |
|---|
| 1968 | 1977 | { |
|---|
| 1969 | 1978 | unsigned int nr_pages, nr_highmem; |
|---|
| 1970 | 1979 | |
|---|
| 1971 | | - pr_info("Creating hibernation image:\n"); |
|---|
| 1980 | + pr_info("Creating image:\n"); |
|---|
| 1972 | 1981 | |
|---|
| 1973 | 1982 | drain_local_pages(NULL); |
|---|
| 1974 | 1983 | nr_pages = count_data_pages(); |
|---|
| .. | .. |
|---|
| 2002 | 2011 | nr_copy_pages = nr_pages; |
|---|
| 2003 | 2012 | nr_meta_pages = DIV_ROUND_UP(nr_pages * sizeof(long), PAGE_SIZE); |
|---|
| 2004 | 2013 | |
|---|
| 2005 | | - pr_info("Hibernation image created (%d pages copied)\n", nr_pages); |
|---|
| 2014 | + pr_info("Image created (%d pages copied)\n", nr_pages); |
|---|
| 2006 | 2015 | |
|---|
| 2007 | 2016 | return 0; |
|---|
| 2008 | 2017 | } |
|---|
| .. | .. |
|---|
| 2015 | 2024 | return 0; |
|---|
| 2016 | 2025 | } |
|---|
| 2017 | 2026 | |
|---|
| 2018 | | -static char *check_image_kernel(struct swsusp_info *info) |
|---|
| 2027 | +static const char *check_image_kernel(struct swsusp_info *info) |
|---|
| 2019 | 2028 | { |
|---|
| 2020 | 2029 | if (info->version_code != LINUX_VERSION_CODE) |
|---|
| 2021 | 2030 | return "kernel version"; |
|---|
| .. | .. |
|---|
| 2063 | 2072 | buf[j] = memory_bm_next_pfn(bm); |
|---|
| 2064 | 2073 | if (unlikely(buf[j] == BM_END_OF_MAP)) |
|---|
| 2065 | 2074 | break; |
|---|
| 2066 | | - /* Save page key for data page (s390 only). */ |
|---|
| 2067 | | - page_key_read(buf + j); |
|---|
| 2068 | 2075 | } |
|---|
| 2069 | 2076 | } |
|---|
| 2070 | 2077 | |
|---|
| .. | .. |
|---|
| 2170 | 2177 | |
|---|
| 2171 | 2178 | static int check_header(struct swsusp_info *info) |
|---|
| 2172 | 2179 | { |
|---|
| 2173 | | - char *reason; |
|---|
| 2180 | + const char *reason; |
|---|
| 2174 | 2181 | |
|---|
| 2175 | 2182 | reason = check_image_kernel(info); |
|---|
| 2176 | 2183 | if (!reason && info->num_physpages != get_num_physpages()) |
|---|
| .. | .. |
|---|
| 2213 | 2220 | for (j = 0; j < PAGE_SIZE / sizeof(long); j++) { |
|---|
| 2214 | 2221 | if (unlikely(buf[j] == BM_END_OF_MAP)) |
|---|
| 2215 | 2222 | break; |
|---|
| 2216 | | - |
|---|
| 2217 | | - /* Extract and buffer page key for data page (s390 only). */ |
|---|
| 2218 | | - page_key_memorize(buf + j); |
|---|
| 2219 | 2223 | |
|---|
| 2220 | 2224 | if (pfn_valid(buf[j]) && memory_bm_pfn_present(bm, buf[j])) |
|---|
| 2221 | 2225 | memory_bm_set_bit(bm, buf[j]); |
|---|
| .. | .. |
|---|
| 2611 | 2615 | if (error) |
|---|
| 2612 | 2616 | return error; |
|---|
| 2613 | 2617 | |
|---|
| 2614 | | - /* Allocate buffer for page keys. */ |
|---|
| 2615 | | - error = page_key_alloc(nr_copy_pages); |
|---|
| 2616 | | - if (error) |
|---|
| 2617 | | - return error; |
|---|
| 2618 | | - |
|---|
| 2619 | 2618 | hibernate_restore_protection_begin(); |
|---|
| 2620 | 2619 | } else if (handle->cur <= nr_meta_pages + 1) { |
|---|
| 2621 | 2620 | error = unpack_orig_pfns(buffer, ©_bm); |
|---|
| .. | .. |
|---|
| 2637 | 2636 | } |
|---|
| 2638 | 2637 | } else { |
|---|
| 2639 | 2638 | copy_last_highmem_page(); |
|---|
| 2640 | | - /* Restore page key for data page (s390 only). */ |
|---|
| 2641 | | - page_key_write(handle->buffer); |
|---|
| 2642 | 2639 | hibernate_restore_protect_page(handle->buffer); |
|---|
| 2643 | 2640 | handle->buffer = get_buffer(&orig_bm, &ca); |
|---|
| 2644 | 2641 | if (IS_ERR(handle->buffer)) |
|---|
| .. | .. |
|---|
| 2661 | 2658 | void snapshot_write_finalize(struct snapshot_handle *handle) |
|---|
| 2662 | 2659 | { |
|---|
| 2663 | 2660 | copy_last_highmem_page(); |
|---|
| 2664 | | - /* Restore page key for data page (s390 only). */ |
|---|
| 2665 | | - page_key_write(handle->buffer); |
|---|
| 2666 | | - page_key_free(); |
|---|
| 2667 | 2661 | hibernate_restore_protect_page(handle->buffer); |
|---|
| 2668 | 2662 | /* Do that only if we have loaded the image entirely */ |
|---|
| 2669 | 2663 | if (handle->cur > 1 && handle->cur > nr_meta_pages + nr_copy_pages) { |
|---|