Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 1 | /* |
Pavel Machek | 96bc7ae | 2005-10-30 14:59:58 -0800 | [diff] [blame] | 2 | * linux/kernel/power/snapshot.c |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 3 | * |
Pavel Machek | 96bc7ae | 2005-10-30 14:59:58 -0800 | [diff] [blame] | 4 | * This file provide system snapshot/restore functionality. |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 5 | * |
| 6 | * Copyright (C) 1998-2005 Pavel Machek <pavel@suse.cz> |
| 7 | * |
| 8 | * This file is released under the GPLv2, and is based on swsusp.c. |
| 9 | * |
| 10 | */ |
| 11 | |
| 12 | |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 13 | #include <linux/version.h> |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 14 | #include <linux/module.h> |
| 15 | #include <linux/mm.h> |
| 16 | #include <linux/suspend.h> |
| 17 | #include <linux/smp_lock.h> |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 18 | #include <linux/delay.h> |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 19 | #include <linux/bitops.h> |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 20 | #include <linux/spinlock.h> |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 21 | #include <linux/kernel.h> |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 22 | #include <linux/pm.h> |
| 23 | #include <linux/device.h> |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 24 | #include <linux/bootmem.h> |
| 25 | #include <linux/syscalls.h> |
| 26 | #include <linux/console.h> |
| 27 | #include <linux/highmem.h> |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 28 | |
| 29 | #include <asm/uaccess.h> |
| 30 | #include <asm/mmu_context.h> |
| 31 | #include <asm/pgtable.h> |
| 32 | #include <asm/tlbflush.h> |
| 33 | #include <asm/io.h> |
| 34 | |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 35 | #include "power.h" |
| 36 | |
Rafael J. Wysocki | 75534b5 | 2006-09-25 23:32:52 -0700 | [diff] [blame] | 37 | /* List of PBEs used for creating and restoring the suspend image */ |
| 38 | struct pbe *restore_pblist; |
| 39 | |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 40 | static unsigned int nr_copy_pages; |
| 41 | static unsigned int nr_meta_pages; |
Rafael J. Wysocki | 6e1819d | 2006-03-23 03:00:03 -0800 | [diff] [blame] | 42 | static unsigned long *buffer; |
Rafael J. Wysocki | 7088a5c | 2006-01-06 00:13:05 -0800 | [diff] [blame] | 43 | |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 44 | #ifdef CONFIG_HIGHMEM |
Linus Torvalds | 3448097 | 2006-06-25 18:41:00 -0700 | [diff] [blame] | 45 | unsigned int count_highmem_pages(void) |
Rafael J. Wysocki | 72a97e0 | 2006-01-06 00:13:46 -0800 | [diff] [blame] | 46 | { |
| 47 | struct zone *zone; |
| 48 | unsigned long zone_pfn; |
| 49 | unsigned int n = 0; |
| 50 | |
| 51 | for_each_zone (zone) |
| 52 | if (is_highmem(zone)) { |
| 53 | mark_free_pages(zone); |
| 54 | for (zone_pfn = 0; zone_pfn < zone->spanned_pages; zone_pfn++) { |
| 55 | struct page *page; |
| 56 | unsigned long pfn = zone_pfn + zone->zone_start_pfn; |
| 57 | if (!pfn_valid(pfn)) |
| 58 | continue; |
| 59 | page = pfn_to_page(pfn); |
| 60 | if (PageReserved(page)) |
| 61 | continue; |
| 62 | if (PageNosaveFree(page)) |
| 63 | continue; |
| 64 | n++; |
| 65 | } |
| 66 | } |
| 67 | return n; |
| 68 | } |
| 69 | |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 70 | struct highmem_page { |
| 71 | char *data; |
| 72 | struct page *page; |
| 73 | struct highmem_page *next; |
| 74 | }; |
| 75 | |
| 76 | static struct highmem_page *highmem_copy; |
| 77 | |
| 78 | static int save_highmem_zone(struct zone *zone) |
| 79 | { |
| 80 | unsigned long zone_pfn; |
| 81 | mark_free_pages(zone); |
| 82 | for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn) { |
| 83 | struct page *page; |
| 84 | struct highmem_page *save; |
| 85 | void *kaddr; |
| 86 | unsigned long pfn = zone_pfn + zone->zone_start_pfn; |
| 87 | |
Pavel Machek | ce6ed29 | 2006-03-23 03:00:05 -0800 | [diff] [blame] | 88 | if (!(pfn%10000)) |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 89 | printk("."); |
| 90 | if (!pfn_valid(pfn)) |
| 91 | continue; |
| 92 | page = pfn_to_page(pfn); |
| 93 | /* |
| 94 | * This condition results from rvmalloc() sans vmalloc_32() |
| 95 | * and architectural memory reservations. This should be |
| 96 | * corrected eventually when the cases giving rise to this |
| 97 | * are better understood. |
| 98 | */ |
Andrew Morton | c8adb49 | 2006-02-15 15:17:42 -0800 | [diff] [blame] | 99 | if (PageReserved(page)) |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 100 | continue; |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 101 | BUG_ON(PageNosave(page)); |
| 102 | if (PageNosaveFree(page)) |
| 103 | continue; |
| 104 | save = kmalloc(sizeof(struct highmem_page), GFP_ATOMIC); |
| 105 | if (!save) |
| 106 | return -ENOMEM; |
| 107 | save->next = highmem_copy; |
| 108 | save->page = page; |
| 109 | save->data = (void *) get_zeroed_page(GFP_ATOMIC); |
| 110 | if (!save->data) { |
| 111 | kfree(save); |
| 112 | return -ENOMEM; |
| 113 | } |
| 114 | kaddr = kmap_atomic(page, KM_USER0); |
| 115 | memcpy(save->data, kaddr, PAGE_SIZE); |
| 116 | kunmap_atomic(kaddr, KM_USER0); |
| 117 | highmem_copy = save; |
| 118 | } |
| 119 | return 0; |
| 120 | } |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 121 | |
Linus Torvalds | 3448097 | 2006-06-25 18:41:00 -0700 | [diff] [blame] | 122 | int save_highmem(void) |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 123 | { |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 124 | struct zone *zone; |
| 125 | int res = 0; |
| 126 | |
Pavel Machek | ce6ed29 | 2006-03-23 03:00:05 -0800 | [diff] [blame] | 127 | pr_debug("swsusp: Saving Highmem"); |
Shaohua Li | e4e4d66 | 2006-03-23 03:00:06 -0800 | [diff] [blame] | 128 | drain_local_pages(); |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 129 | for_each_zone (zone) { |
| 130 | if (is_highmem(zone)) |
| 131 | res = save_highmem_zone(zone); |
| 132 | if (res) |
| 133 | return res; |
| 134 | } |
Pavel Machek | ce6ed29 | 2006-03-23 03:00:05 -0800 | [diff] [blame] | 135 | printk("\n"); |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 136 | return 0; |
| 137 | } |
| 138 | |
Linus Torvalds | 3448097 | 2006-06-25 18:41:00 -0700 | [diff] [blame] | 139 | int restore_highmem(void) |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 140 | { |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 141 | printk("swsusp: Restoring Highmem\n"); |
| 142 | while (highmem_copy) { |
| 143 | struct highmem_page *save = highmem_copy; |
| 144 | void *kaddr; |
| 145 | highmem_copy = save->next; |
| 146 | |
| 147 | kaddr = kmap_atomic(save->page, KM_USER0); |
| 148 | memcpy(kaddr, save->data, PAGE_SIZE); |
| 149 | kunmap_atomic(kaddr, KM_USER0); |
| 150 | free_page((long) save->data); |
| 151 | kfree(save); |
| 152 | } |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 153 | return 0; |
| 154 | } |
Shaohua Li | ce4ab00 | 2006-06-23 02:04:44 -0700 | [diff] [blame] | 155 | #else |
Adrian Bunk | 7bff24e | 2006-06-23 02:04:47 -0700 | [diff] [blame] | 156 | static inline unsigned int count_highmem_pages(void) {return 0;} |
| 157 | static inline int save_highmem(void) {return 0;} |
| 158 | static inline int restore_highmem(void) {return 0;} |
Rafael J. Wysocki | 0fbeb5a | 2005-11-08 21:34:41 -0800 | [diff] [blame] | 159 | #endif |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 160 | |
Rafael J. Wysocki | f6143aa | 2006-09-25 23:32:50 -0700 | [diff] [blame] | 161 | /** |
| 162 | * @safe_needed - on resume, for storing the PBE list and the image, |
| 163 | * we can only use memory pages that do not conflict with the pages |
| 164 | * used before suspend. |
| 165 | * |
| 166 | * The unsafe pages are marked with the PG_nosave_free flag |
| 167 | * and we count them using unsafe_pages |
| 168 | */ |
| 169 | |
Rafael J. Wysocki | 0bcd888 | 2006-09-25 23:32:52 -0700 | [diff] [blame^] | 170 | #define PG_ANY 0 |
| 171 | #define PG_SAFE 1 |
| 172 | #define PG_UNSAFE_CLEAR 1 |
| 173 | #define PG_UNSAFE_KEEP 0 |
| 174 | |
Rafael J. Wysocki | f6143aa | 2006-09-25 23:32:50 -0700 | [diff] [blame] | 175 | static unsigned int unsafe_pages; |
| 176 | |
| 177 | static void *alloc_image_page(gfp_t gfp_mask, int safe_needed) |
| 178 | { |
| 179 | void *res; |
| 180 | |
| 181 | res = (void *)get_zeroed_page(gfp_mask); |
| 182 | if (safe_needed) |
| 183 | while (res && PageNosaveFree(virt_to_page(res))) { |
| 184 | /* The page is unsafe, mark it for swsusp_free() */ |
| 185 | SetPageNosave(virt_to_page(res)); |
| 186 | unsafe_pages++; |
| 187 | res = (void *)get_zeroed_page(gfp_mask); |
| 188 | } |
| 189 | if (res) { |
| 190 | SetPageNosave(virt_to_page(res)); |
| 191 | SetPageNosaveFree(virt_to_page(res)); |
| 192 | } |
| 193 | return res; |
| 194 | } |
| 195 | |
| 196 | unsigned long get_safe_page(gfp_t gfp_mask) |
| 197 | { |
Rafael J. Wysocki | 0bcd888 | 2006-09-25 23:32:52 -0700 | [diff] [blame^] | 198 | return (unsigned long)alloc_image_page(gfp_mask, PG_SAFE); |
Rafael J. Wysocki | f6143aa | 2006-09-25 23:32:50 -0700 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | /** |
| 202 | * free_image_page - free page represented by @addr, allocated with |
| 203 | * alloc_image_page (page flags set by it must be cleared) |
| 204 | */ |
| 205 | |
| 206 | static inline void free_image_page(void *addr, int clear_nosave_free) |
| 207 | { |
| 208 | ClearPageNosave(virt_to_page(addr)); |
| 209 | if (clear_nosave_free) |
| 210 | ClearPageNosaveFree(virt_to_page(addr)); |
| 211 | free_page((unsigned long)addr); |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * pfn_is_nosave - check if given pfn is in the 'nosave' section |
| 216 | */ |
| 217 | |
Rafael J. Wysocki | ae83c5ee | 2006-09-25 23:32:45 -0700 | [diff] [blame] | 218 | static inline int pfn_is_nosave(unsigned long pfn) |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 219 | { |
| 220 | unsigned long nosave_begin_pfn = __pa(&__nosave_begin) >> PAGE_SHIFT; |
| 221 | unsigned long nosave_end_pfn = PAGE_ALIGN(__pa(&__nosave_end)) >> PAGE_SHIFT; |
| 222 | return (pfn >= nosave_begin_pfn) && (pfn < nosave_end_pfn); |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * saveable - Determine whether a page should be cloned or not. |
| 227 | * @pfn: The page |
| 228 | * |
Rafael J. Wysocki | ae83c5ee | 2006-09-25 23:32:45 -0700 | [diff] [blame] | 229 | * We save a page if it isn't Nosave, and is not in the range of pages |
| 230 | * statically defined as 'unsaveable', and it |
| 231 | * isn't a part of a free chunk of pages. |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 232 | */ |
| 233 | |
Rafael J. Wysocki | ae83c5ee | 2006-09-25 23:32:45 -0700 | [diff] [blame] | 234 | static struct page *saveable_page(unsigned long pfn) |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 235 | { |
Pavel Machek | de49186 | 2005-10-30 14:59:59 -0800 | [diff] [blame] | 236 | struct page *page; |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 237 | |
| 238 | if (!pfn_valid(pfn)) |
Rafael J. Wysocki | ae83c5ee | 2006-09-25 23:32:45 -0700 | [diff] [blame] | 239 | return NULL; |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 240 | |
| 241 | page = pfn_to_page(pfn); |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 242 | |
Rafael J. Wysocki | ae83c5ee | 2006-09-25 23:32:45 -0700 | [diff] [blame] | 243 | if (PageNosave(page)) |
| 244 | return NULL; |
| 245 | if (PageReserved(page) && pfn_is_nosave(pfn)) |
| 246 | return NULL; |
| 247 | if (PageNosaveFree(page)) |
| 248 | return NULL; |
| 249 | |
| 250 | return page; |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 251 | } |
| 252 | |
Rafael J. Wysocki | 72a97e0 | 2006-01-06 00:13:46 -0800 | [diff] [blame] | 253 | unsigned int count_data_pages(void) |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 254 | { |
| 255 | struct zone *zone; |
Rafael J. Wysocki | ae83c5ee | 2006-09-25 23:32:45 -0700 | [diff] [blame] | 256 | unsigned long pfn, max_zone_pfn; |
Pavel Machek | dc19d50 | 2005-11-07 00:58:40 -0800 | [diff] [blame] | 257 | unsigned int n = 0; |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 258 | |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 259 | for_each_zone (zone) { |
| 260 | if (is_highmem(zone)) |
| 261 | continue; |
| 262 | mark_free_pages(zone); |
Rafael J. Wysocki | ae83c5ee | 2006-09-25 23:32:45 -0700 | [diff] [blame] | 263 | max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages; |
| 264 | for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++) |
| 265 | n += !!saveable_page(pfn); |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 266 | } |
Rafael J. Wysocki | a0f4965 | 2005-10-30 14:59:57 -0800 | [diff] [blame] | 267 | return n; |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 268 | } |
| 269 | |
Rafael J. Wysocki | f623f0d | 2006-09-25 23:32:49 -0700 | [diff] [blame] | 270 | static inline void copy_data_page(long *dst, long *src) |
| 271 | { |
| 272 | int n; |
| 273 | |
| 274 | /* copy_page and memcpy are not usable for copying task structs. */ |
| 275 | for (n = PAGE_SIZE / sizeof(long); n; n--) |
| 276 | *dst++ = *src++; |
| 277 | } |
| 278 | |
Rafael J. Wysocki | a0f4965 | 2005-10-30 14:59:57 -0800 | [diff] [blame] | 279 | static void copy_data_pages(struct pbe *pblist) |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 280 | { |
| 281 | struct zone *zone; |
Rafael J. Wysocki | ae83c5ee | 2006-09-25 23:32:45 -0700 | [diff] [blame] | 282 | unsigned long pfn, max_zone_pfn; |
Rafael J. Wysocki | f623f0d | 2006-09-25 23:32:49 -0700 | [diff] [blame] | 283 | struct pbe *pbe; |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 284 | |
Rafael J. Wysocki | a0f4965 | 2005-10-30 14:59:57 -0800 | [diff] [blame] | 285 | pbe = pblist; |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 286 | for_each_zone (zone) { |
| 287 | if (is_highmem(zone)) |
| 288 | continue; |
| 289 | mark_free_pages(zone); |
Rafael J. Wysocki | ae83c5ee | 2006-09-25 23:32:45 -0700 | [diff] [blame] | 290 | max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages; |
| 291 | for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++) { |
| 292 | struct page *page = saveable_page(pfn); |
| 293 | |
| 294 | if (page) { |
Rafael J. Wysocki | f623f0d | 2006-09-25 23:32:49 -0700 | [diff] [blame] | 295 | void *ptr = page_address(page); |
Rafael J. Wysocki | 95018f7 | 2006-07-10 04:45:00 -0700 | [diff] [blame] | 296 | |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 297 | BUG_ON(!pbe); |
Rafael J. Wysocki | f623f0d | 2006-09-25 23:32:49 -0700 | [diff] [blame] | 298 | copy_data_page((void *)pbe->address, ptr); |
| 299 | pbe->orig_address = (unsigned long)ptr; |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 300 | pbe = pbe->next; |
| 301 | } |
| 302 | } |
| 303 | } |
| 304 | BUG_ON(pbe); |
| 305 | } |
| 306 | |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 307 | /** |
| 308 | * free_pagedir - free pages allocated with alloc_pagedir() |
| 309 | */ |
| 310 | |
Rafael J. Wysocki | 4a3b98a | 2006-04-18 22:20:29 -0700 | [diff] [blame] | 311 | static void free_pagedir(struct pbe *pblist, int clear_nosave_free) |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 312 | { |
| 313 | struct pbe *pbe; |
| 314 | |
| 315 | while (pblist) { |
| 316 | pbe = (pblist + PB_PAGE_SKIP)->next; |
Rafael J. Wysocki | f6143aa | 2006-09-25 23:32:50 -0700 | [diff] [blame] | 317 | free_image_page(pblist, clear_nosave_free); |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 318 | pblist = pbe; |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | /** |
| 323 | * fill_pb_page - Create a list of PBEs on a given memory page |
| 324 | */ |
| 325 | |
Rafael J. Wysocki | cd560bb | 2006-09-25 23:32:50 -0700 | [diff] [blame] | 326 | static inline void fill_pb_page(struct pbe *pbpage, unsigned int n) |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 327 | { |
| 328 | struct pbe *p; |
| 329 | |
| 330 | p = pbpage; |
Rafael J. Wysocki | cd560bb | 2006-09-25 23:32:50 -0700 | [diff] [blame] | 331 | pbpage += n - 1; |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 332 | do |
| 333 | p->next = p + 1; |
| 334 | while (++p < pbpage); |
| 335 | } |
| 336 | |
| 337 | /** |
| 338 | * create_pbe_list - Create a list of PBEs on top of a given chain |
| 339 | * of memory pages allocated with alloc_pagedir() |
Rafael J. Wysocki | cd560bb | 2006-09-25 23:32:50 -0700 | [diff] [blame] | 340 | * |
| 341 | * This function assumes that pages allocated by alloc_image_page() will |
| 342 | * always be zeroed. |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 343 | */ |
| 344 | |
Rafael J. Wysocki | 7088a5c | 2006-01-06 00:13:05 -0800 | [diff] [blame] | 345 | static inline void create_pbe_list(struct pbe *pblist, unsigned int nr_pages) |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 346 | { |
Rafael J. Wysocki | cd560bb | 2006-09-25 23:32:50 -0700 | [diff] [blame] | 347 | struct pbe *pbpage; |
Pavel Machek | dc19d50 | 2005-11-07 00:58:40 -0800 | [diff] [blame] | 348 | unsigned int num = PBES_PER_PAGE; |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 349 | |
| 350 | for_each_pb_page (pbpage, pblist) { |
| 351 | if (num >= nr_pages) |
| 352 | break; |
| 353 | |
Rafael J. Wysocki | cd560bb | 2006-09-25 23:32:50 -0700 | [diff] [blame] | 354 | fill_pb_page(pbpage, PBES_PER_PAGE); |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 355 | num += PBES_PER_PAGE; |
| 356 | } |
| 357 | if (pbpage) { |
Rafael J. Wysocki | cd560bb | 2006-09-25 23:32:50 -0700 | [diff] [blame] | 358 | num -= PBES_PER_PAGE; |
| 359 | fill_pb_page(pbpage, nr_pages - num); |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 360 | } |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 361 | } |
| 362 | |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 363 | /** |
| 364 | * alloc_pagedir - Allocate the page directory. |
| 365 | * |
| 366 | * First, determine exactly how many pages we need and |
| 367 | * allocate them. |
| 368 | * |
| 369 | * We arrange the pages in a chain: each page is an array of PBES_PER_PAGE |
| 370 | * struct pbe elements (pbes) and the last element in the page points |
| 371 | * to the next page. |
| 372 | * |
| 373 | * On each page we set up a list of struct_pbe elements. |
| 374 | */ |
| 375 | |
Adrian Bunk | 7bff24e | 2006-06-23 02:04:47 -0700 | [diff] [blame] | 376 | static struct pbe *alloc_pagedir(unsigned int nr_pages, gfp_t gfp_mask, |
| 377 | int safe_needed) |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 378 | { |
Pavel Machek | dc19d50 | 2005-11-07 00:58:40 -0800 | [diff] [blame] | 379 | unsigned int num; |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 380 | struct pbe *pblist, *pbe; |
| 381 | |
| 382 | if (!nr_pages) |
| 383 | return NULL; |
| 384 | |
Rafael J. Wysocki | 054bd4c | 2005-11-08 21:34:39 -0800 | [diff] [blame] | 385 | pblist = alloc_image_page(gfp_mask, safe_needed); |
Rafael J. Wysocki | cd560bb | 2006-09-25 23:32:50 -0700 | [diff] [blame] | 386 | pbe = pblist; |
| 387 | for (num = PBES_PER_PAGE; num < nr_pages; num += PBES_PER_PAGE) { |
| 388 | if (!pbe) { |
Rafael J. Wysocki | 0bcd888 | 2006-09-25 23:32:52 -0700 | [diff] [blame^] | 389 | free_pagedir(pblist, PG_UNSAFE_CLEAR); |
Rafael J. Wysocki | cd560bb | 2006-09-25 23:32:50 -0700 | [diff] [blame] | 390 | return NULL; |
| 391 | } |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 392 | pbe += PB_PAGE_SKIP; |
Rafael J. Wysocki | 054bd4c | 2005-11-08 21:34:39 -0800 | [diff] [blame] | 393 | pbe->next = alloc_image_page(gfp_mask, safe_needed); |
Rafael J. Wysocki | cd560bb | 2006-09-25 23:32:50 -0700 | [diff] [blame] | 394 | pbe = pbe->next; |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 395 | } |
Rafael J. Wysocki | cd560bb | 2006-09-25 23:32:50 -0700 | [diff] [blame] | 396 | create_pbe_list(pblist, nr_pages); |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 397 | return pblist; |
| 398 | } |
| 399 | |
| 400 | /** |
| 401 | * Free pages we allocated for suspend. Suspend pages are alocated |
| 402 | * before atomic copy, so we need to free them after resume. |
| 403 | */ |
| 404 | |
| 405 | void swsusp_free(void) |
| 406 | { |
| 407 | struct zone *zone; |
Rafael J. Wysocki | ae83c5ee | 2006-09-25 23:32:45 -0700 | [diff] [blame] | 408 | unsigned long pfn, max_zone_pfn; |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 409 | |
| 410 | for_each_zone(zone) { |
Rafael J. Wysocki | ae83c5ee | 2006-09-25 23:32:45 -0700 | [diff] [blame] | 411 | max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages; |
| 412 | for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++) |
| 413 | if (pfn_valid(pfn)) { |
| 414 | struct page *page = pfn_to_page(pfn); |
| 415 | |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 416 | if (PageNosave(page) && PageNosaveFree(page)) { |
| 417 | ClearPageNosave(page); |
| 418 | ClearPageNosaveFree(page); |
| 419 | free_page((long) page_address(page)); |
| 420 | } |
| 421 | } |
| 422 | } |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 423 | nr_copy_pages = 0; |
| 424 | nr_meta_pages = 0; |
Rafael J. Wysocki | 75534b5 | 2006-09-25 23:32:52 -0700 | [diff] [blame] | 425 | restore_pblist = NULL; |
Rafael J. Wysocki | 6e1819d | 2006-03-23 03:00:03 -0800 | [diff] [blame] | 426 | buffer = NULL; |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | |
| 430 | /** |
| 431 | * enough_free_mem - Make sure we enough free memory to snapshot. |
| 432 | * |
| 433 | * Returns TRUE or FALSE after checking the number of available |
| 434 | * free pages. |
| 435 | */ |
| 436 | |
Pavel Machek | dc19d50 | 2005-11-07 00:58:40 -0800 | [diff] [blame] | 437 | static int enough_free_mem(unsigned int nr_pages) |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 438 | { |
Rafael J. Wysocki | e5e2fa7 | 2006-01-06 00:14:20 -0800 | [diff] [blame] | 439 | struct zone *zone; |
| 440 | unsigned int n = 0; |
| 441 | |
| 442 | for_each_zone (zone) |
| 443 | if (!is_highmem(zone)) |
| 444 | n += zone->free_pages; |
| 445 | pr_debug("swsusp: available memory: %u pages\n", n); |
| 446 | return n > (nr_pages + PAGES_FOR_IO + |
Rafael J. Wysocki | a0f4965 | 2005-10-30 14:59:57 -0800 | [diff] [blame] | 447 | (nr_pages + PBES_PER_PAGE - 1) / PBES_PER_PAGE); |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 448 | } |
| 449 | |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 450 | static int alloc_data_pages(struct pbe *pblist, gfp_t gfp_mask, int safe_needed) |
Rafael J. Wysocki | 054bd4c | 2005-11-08 21:34:39 -0800 | [diff] [blame] | 451 | { |
| 452 | struct pbe *p; |
| 453 | |
| 454 | for_each_pbe (p, pblist) { |
| 455 | p->address = (unsigned long)alloc_image_page(gfp_mask, safe_needed); |
| 456 | if (!p->address) |
| 457 | return -ENOMEM; |
| 458 | } |
| 459 | return 0; |
| 460 | } |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 461 | |
Pavel Machek | dc19d50 | 2005-11-07 00:58:40 -0800 | [diff] [blame] | 462 | static struct pbe *swsusp_alloc(unsigned int nr_pages) |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 463 | { |
Rafael J. Wysocki | 054bd4c | 2005-11-08 21:34:39 -0800 | [diff] [blame] | 464 | struct pbe *pblist; |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 465 | |
Rafael J. Wysocki | 0bcd888 | 2006-09-25 23:32:52 -0700 | [diff] [blame^] | 466 | pblist = alloc_pagedir(nr_pages, GFP_ATOMIC | __GFP_COLD, PG_ANY); |
| 467 | if (!pblist) { |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 468 | printk(KERN_ERR "suspend: Allocating pagedir failed.\n"); |
Rafael J. Wysocki | a0f4965 | 2005-10-30 14:59:57 -0800 | [diff] [blame] | 469 | return NULL; |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 470 | } |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 471 | |
Rafael J. Wysocki | 0bcd888 | 2006-09-25 23:32:52 -0700 | [diff] [blame^] | 472 | if (alloc_data_pages(pblist, GFP_ATOMIC | __GFP_COLD, PG_ANY)) { |
Rafael J. Wysocki | 054bd4c | 2005-11-08 21:34:39 -0800 | [diff] [blame] | 473 | printk(KERN_ERR "suspend: Allocating image pages failed.\n"); |
| 474 | swsusp_free(); |
| 475 | return NULL; |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 476 | } |
| 477 | |
Rafael J. Wysocki | a0f4965 | 2005-10-30 14:59:57 -0800 | [diff] [blame] | 478 | return pblist; |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 479 | } |
| 480 | |
Rafael J. Wysocki | 2e32a43 | 2005-10-30 15:00:00 -0800 | [diff] [blame] | 481 | asmlinkage int swsusp_save(void) |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 482 | { |
Pavel Machek | dc19d50 | 2005-11-07 00:58:40 -0800 | [diff] [blame] | 483 | unsigned int nr_pages; |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 484 | |
| 485 | pr_debug("swsusp: critical section: \n"); |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 486 | |
| 487 | drain_local_pages(); |
Rafael J. Wysocki | a0f4965 | 2005-10-30 14:59:57 -0800 | [diff] [blame] | 488 | nr_pages = count_data_pages(); |
| 489 | printk("swsusp: Need to copy %u pages\n", nr_pages); |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 490 | |
| 491 | pr_debug("swsusp: pages needed: %u + %lu + %u, free: %u\n", |
Rafael J. Wysocki | a0f4965 | 2005-10-30 14:59:57 -0800 | [diff] [blame] | 492 | nr_pages, |
| 493 | (nr_pages + PBES_PER_PAGE - 1) / PBES_PER_PAGE, |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 494 | PAGES_FOR_IO, nr_free_pages()); |
| 495 | |
Rafael J. Wysocki | a0f4965 | 2005-10-30 14:59:57 -0800 | [diff] [blame] | 496 | if (!enough_free_mem(nr_pages)) { |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 497 | printk(KERN_ERR "swsusp: Not enough free memory\n"); |
| 498 | return -ENOMEM; |
| 499 | } |
| 500 | |
Rafael J. Wysocki | 75534b5 | 2006-09-25 23:32:52 -0700 | [diff] [blame] | 501 | restore_pblist = swsusp_alloc(nr_pages); |
| 502 | if (!restore_pblist) |
Rafael J. Wysocki | a0f4965 | 2005-10-30 14:59:57 -0800 | [diff] [blame] | 503 | return -ENOMEM; |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 504 | |
| 505 | /* During allocating of suspend pagedir, new cold pages may appear. |
| 506 | * Kill them. |
| 507 | */ |
| 508 | drain_local_pages(); |
Rafael J. Wysocki | 75534b5 | 2006-09-25 23:32:52 -0700 | [diff] [blame] | 509 | copy_data_pages(restore_pblist); |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 510 | |
| 511 | /* |
| 512 | * End of critical section. From now on, we can write to memory, |
| 513 | * but we should not touch disk. This specially means we must _not_ |
| 514 | * touch swap space! Except we must write out our image of course. |
| 515 | */ |
| 516 | |
Rafael J. Wysocki | a0f4965 | 2005-10-30 14:59:57 -0800 | [diff] [blame] | 517 | nr_copy_pages = nr_pages; |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 518 | nr_meta_pages = (nr_pages * sizeof(long) + PAGE_SIZE - 1) >> PAGE_SHIFT; |
Rafael J. Wysocki | a0f4965 | 2005-10-30 14:59:57 -0800 | [diff] [blame] | 519 | |
| 520 | printk("swsusp: critical section/: done (%d pages copied)\n", nr_pages); |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 521 | return 0; |
| 522 | } |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 523 | |
| 524 | static void init_header(struct swsusp_info *info) |
| 525 | { |
| 526 | memset(info, 0, sizeof(struct swsusp_info)); |
| 527 | info->version_code = LINUX_VERSION_CODE; |
| 528 | info->num_physpages = num_physpages; |
| 529 | memcpy(&info->uts, &system_utsname, sizeof(system_utsname)); |
| 530 | info->cpus = num_online_cpus(); |
| 531 | info->image_pages = nr_copy_pages; |
| 532 | info->pages = nr_copy_pages + nr_meta_pages + 1; |
Rafael J. Wysocki | 6e1819d | 2006-03-23 03:00:03 -0800 | [diff] [blame] | 533 | info->size = info->pages; |
| 534 | info->size <<= PAGE_SHIFT; |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | /** |
| 538 | * pack_orig_addresses - the .orig_address fields of the PBEs from the |
| 539 | * list starting at @pbe are stored in the array @buf[] (1 page) |
| 540 | */ |
| 541 | |
| 542 | static inline struct pbe *pack_orig_addresses(unsigned long *buf, struct pbe *pbe) |
| 543 | { |
| 544 | int j; |
| 545 | |
| 546 | for (j = 0; j < PAGE_SIZE / sizeof(long) && pbe; j++) { |
| 547 | buf[j] = pbe->orig_address; |
| 548 | pbe = pbe->next; |
| 549 | } |
| 550 | if (!pbe) |
| 551 | for (; j < PAGE_SIZE / sizeof(long); j++) |
| 552 | buf[j] = 0; |
| 553 | return pbe; |
| 554 | } |
| 555 | |
| 556 | /** |
| 557 | * snapshot_read_next - used for reading the system memory snapshot. |
| 558 | * |
| 559 | * On the first call to it @handle should point to a zeroed |
| 560 | * snapshot_handle structure. The structure gets updated and a pointer |
| 561 | * to it should be passed to this function every next time. |
| 562 | * |
| 563 | * The @count parameter should contain the number of bytes the caller |
| 564 | * wants to read from the snapshot. It must not be zero. |
| 565 | * |
| 566 | * On success the function returns a positive number. Then, the caller |
| 567 | * is allowed to read up to the returned number of bytes from the memory |
| 568 | * location computed by the data_of() macro. The number returned |
| 569 | * may be smaller than @count, but this only happens if the read would |
| 570 | * cross a page boundary otherwise. |
| 571 | * |
| 572 | * The function returns 0 to indicate the end of data stream condition, |
| 573 | * and a negative number is returned on error. In such cases the |
| 574 | * structure pointed to by @handle is not updated and should not be used |
| 575 | * any more. |
| 576 | */ |
| 577 | |
| 578 | int snapshot_read_next(struct snapshot_handle *handle, size_t count) |
| 579 | { |
Rafael J. Wysocki | fb13a28 | 2006-09-25 23:32:46 -0700 | [diff] [blame] | 580 | if (handle->cur > nr_meta_pages + nr_copy_pages) |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 581 | return 0; |
| 582 | if (!buffer) { |
| 583 | /* This makes the buffer be freed by swsusp_free() */ |
Rafael J. Wysocki | 0bcd888 | 2006-09-25 23:32:52 -0700 | [diff] [blame^] | 584 | buffer = alloc_image_page(GFP_ATOMIC, PG_ANY); |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 585 | if (!buffer) |
| 586 | return -ENOMEM; |
| 587 | } |
| 588 | if (!handle->offset) { |
| 589 | init_header((struct swsusp_info *)buffer); |
| 590 | handle->buffer = buffer; |
Rafael J. Wysocki | 75534b5 | 2006-09-25 23:32:52 -0700 | [diff] [blame] | 591 | handle->pbe = restore_pblist; |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 592 | } |
Rafael J. Wysocki | fb13a28 | 2006-09-25 23:32:46 -0700 | [diff] [blame] | 593 | if (handle->prev < handle->cur) { |
| 594 | if (handle->cur <= nr_meta_pages) { |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 595 | handle->pbe = pack_orig_addresses(buffer, handle->pbe); |
| 596 | if (!handle->pbe) |
Rafael J. Wysocki | 75534b5 | 2006-09-25 23:32:52 -0700 | [diff] [blame] | 597 | handle->pbe = restore_pblist; |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 598 | } else { |
| 599 | handle->buffer = (void *)handle->pbe->address; |
| 600 | handle->pbe = handle->pbe->next; |
| 601 | } |
Rafael J. Wysocki | fb13a28 | 2006-09-25 23:32:46 -0700 | [diff] [blame] | 602 | handle->prev = handle->cur; |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 603 | } |
Rafael J. Wysocki | fb13a28 | 2006-09-25 23:32:46 -0700 | [diff] [blame] | 604 | handle->buf_offset = handle->cur_offset; |
| 605 | if (handle->cur_offset + count >= PAGE_SIZE) { |
| 606 | count = PAGE_SIZE - handle->cur_offset; |
| 607 | handle->cur_offset = 0; |
| 608 | handle->cur++; |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 609 | } else { |
Rafael J. Wysocki | fb13a28 | 2006-09-25 23:32:46 -0700 | [diff] [blame] | 610 | handle->cur_offset += count; |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 611 | } |
| 612 | handle->offset += count; |
| 613 | return count; |
| 614 | } |
| 615 | |
| 616 | /** |
| 617 | * mark_unsafe_pages - mark the pages that cannot be used for storing |
| 618 | * the image during resume, because they conflict with the pages that |
| 619 | * had been used before suspend |
| 620 | */ |
| 621 | |
| 622 | static int mark_unsafe_pages(struct pbe *pblist) |
| 623 | { |
| 624 | struct zone *zone; |
Rafael J. Wysocki | ae83c5ee | 2006-09-25 23:32:45 -0700 | [diff] [blame] | 625 | unsigned long pfn, max_zone_pfn; |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 626 | struct pbe *p; |
| 627 | |
| 628 | if (!pblist) /* a sanity check */ |
| 629 | return -EINVAL; |
| 630 | |
| 631 | /* Clear page flags */ |
| 632 | for_each_zone (zone) { |
Rafael J. Wysocki | ae83c5ee | 2006-09-25 23:32:45 -0700 | [diff] [blame] | 633 | max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages; |
| 634 | for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++) |
| 635 | if (pfn_valid(pfn)) |
| 636 | ClearPageNosaveFree(pfn_to_page(pfn)); |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | /* Mark orig addresses */ |
| 640 | for_each_pbe (p, pblist) { |
| 641 | if (virt_addr_valid(p->orig_address)) |
| 642 | SetPageNosaveFree(virt_to_page(p->orig_address)); |
| 643 | else |
| 644 | return -EFAULT; |
| 645 | } |
| 646 | |
Rafael J. Wysocki | 968808b8 | 2006-06-23 02:04:48 -0700 | [diff] [blame] | 647 | unsafe_pages = 0; |
| 648 | |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 649 | return 0; |
| 650 | } |
| 651 | |
| 652 | static void copy_page_backup_list(struct pbe *dst, struct pbe *src) |
| 653 | { |
| 654 | /* We assume both lists contain the same number of elements */ |
| 655 | while (src) { |
| 656 | dst->orig_address = src->orig_address; |
| 657 | dst = dst->next; |
| 658 | src = src->next; |
| 659 | } |
| 660 | } |
| 661 | |
| 662 | static int check_header(struct swsusp_info *info) |
| 663 | { |
| 664 | char *reason = NULL; |
| 665 | |
| 666 | if (info->version_code != LINUX_VERSION_CODE) |
| 667 | reason = "kernel version"; |
| 668 | if (info->num_physpages != num_physpages) |
| 669 | reason = "memory size"; |
| 670 | if (strcmp(info->uts.sysname,system_utsname.sysname)) |
| 671 | reason = "system type"; |
| 672 | if (strcmp(info->uts.release,system_utsname.release)) |
| 673 | reason = "kernel release"; |
| 674 | if (strcmp(info->uts.version,system_utsname.version)) |
| 675 | reason = "version"; |
| 676 | if (strcmp(info->uts.machine,system_utsname.machine)) |
| 677 | reason = "machine"; |
| 678 | if (reason) { |
| 679 | printk(KERN_ERR "swsusp: Resume mismatch: %s\n", reason); |
| 680 | return -EPERM; |
| 681 | } |
| 682 | return 0; |
| 683 | } |
| 684 | |
| 685 | /** |
| 686 | * load header - check the image header and copy data from it |
| 687 | */ |
| 688 | |
| 689 | static int load_header(struct snapshot_handle *handle, |
| 690 | struct swsusp_info *info) |
| 691 | { |
| 692 | int error; |
| 693 | struct pbe *pblist; |
| 694 | |
| 695 | error = check_header(info); |
| 696 | if (!error) { |
Rafael J. Wysocki | 0bcd888 | 2006-09-25 23:32:52 -0700 | [diff] [blame^] | 697 | pblist = alloc_pagedir(info->image_pages, GFP_ATOMIC, PG_ANY); |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 698 | if (!pblist) |
| 699 | return -ENOMEM; |
Rafael J. Wysocki | 75534b5 | 2006-09-25 23:32:52 -0700 | [diff] [blame] | 700 | restore_pblist = pblist; |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 701 | handle->pbe = pblist; |
| 702 | nr_copy_pages = info->image_pages; |
| 703 | nr_meta_pages = info->pages - info->image_pages - 1; |
| 704 | } |
| 705 | return error; |
| 706 | } |
| 707 | |
| 708 | /** |
| 709 | * unpack_orig_addresses - copy the elements of @buf[] (1 page) to |
| 710 | * the PBEs in the list starting at @pbe |
| 711 | */ |
| 712 | |
| 713 | static inline struct pbe *unpack_orig_addresses(unsigned long *buf, |
| 714 | struct pbe *pbe) |
| 715 | { |
| 716 | int j; |
| 717 | |
| 718 | for (j = 0; j < PAGE_SIZE / sizeof(long) && pbe; j++) { |
| 719 | pbe->orig_address = buf[j]; |
| 720 | pbe = pbe->next; |
| 721 | } |
| 722 | return pbe; |
| 723 | } |
| 724 | |
| 725 | /** |
Rafael J. Wysocki | 968808b8 | 2006-06-23 02:04:48 -0700 | [diff] [blame] | 726 | * prepare_image - use metadata contained in the PBE list |
Rafael J. Wysocki | 75534b5 | 2006-09-25 23:32:52 -0700 | [diff] [blame] | 727 | * pointed to by restore_pblist to mark the pages that will |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 728 | * be overwritten in the process of restoring the system |
Rafael J. Wysocki | 968808b8 | 2006-06-23 02:04:48 -0700 | [diff] [blame] | 729 | * memory state from the image ("unsafe" pages) and allocate |
| 730 | * memory for the image |
| 731 | * |
| 732 | * The idea is to allocate the PBE list first and then |
| 733 | * allocate as many pages as it's needed for the image data, |
| 734 | * but not to assign these pages to the PBEs initially. |
| 735 | * Instead, we just mark them as allocated and create a list |
| 736 | * of "safe" which will be used later |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 737 | */ |
| 738 | |
Rafael J. Wysocki | 968808b8 | 2006-06-23 02:04:48 -0700 | [diff] [blame] | 739 | struct safe_page { |
| 740 | struct safe_page *next; |
| 741 | char padding[PAGE_SIZE - sizeof(void *)]; |
| 742 | }; |
| 743 | |
| 744 | static struct safe_page *safe_pages; |
| 745 | |
| 746 | static int prepare_image(struct snapshot_handle *handle) |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 747 | { |
| 748 | int error = 0; |
Rafael J. Wysocki | 968808b8 | 2006-06-23 02:04:48 -0700 | [diff] [blame] | 749 | unsigned int nr_pages = nr_copy_pages; |
| 750 | struct pbe *p, *pblist = NULL; |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 751 | |
Rafael J. Wysocki | 75534b5 | 2006-09-25 23:32:52 -0700 | [diff] [blame] | 752 | p = restore_pblist; |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 753 | error = mark_unsafe_pages(p); |
| 754 | if (!error) { |
Rafael J. Wysocki | 0bcd888 | 2006-09-25 23:32:52 -0700 | [diff] [blame^] | 755 | pblist = alloc_pagedir(nr_pages, GFP_ATOMIC, PG_SAFE); |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 756 | if (pblist) |
| 757 | copy_page_backup_list(pblist, p); |
Rafael J. Wysocki | 0bcd888 | 2006-09-25 23:32:52 -0700 | [diff] [blame^] | 758 | free_pagedir(p, PG_UNSAFE_KEEP); |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 759 | if (!pblist) |
| 760 | error = -ENOMEM; |
| 761 | } |
Rafael J. Wysocki | 968808b8 | 2006-06-23 02:04:48 -0700 | [diff] [blame] | 762 | safe_pages = NULL; |
| 763 | if (!error && nr_pages > unsafe_pages) { |
| 764 | nr_pages -= unsafe_pages; |
| 765 | while (nr_pages--) { |
| 766 | struct safe_page *ptr; |
| 767 | |
| 768 | ptr = (struct safe_page *)get_zeroed_page(GFP_ATOMIC); |
| 769 | if (!ptr) { |
| 770 | error = -ENOMEM; |
| 771 | break; |
| 772 | } |
| 773 | if (!PageNosaveFree(virt_to_page(ptr))) { |
| 774 | /* The page is "safe", add it to the list */ |
| 775 | ptr->next = safe_pages; |
| 776 | safe_pages = ptr; |
| 777 | } |
| 778 | /* Mark the page as allocated */ |
| 779 | SetPageNosave(virt_to_page(ptr)); |
| 780 | SetPageNosaveFree(virt_to_page(ptr)); |
| 781 | } |
| 782 | } |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 783 | if (!error) { |
Rafael J. Wysocki | 75534b5 | 2006-09-25 23:32:52 -0700 | [diff] [blame] | 784 | restore_pblist = pblist; |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 785 | } else { |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 786 | handle->pbe = NULL; |
Rafael J. Wysocki | 968808b8 | 2006-06-23 02:04:48 -0700 | [diff] [blame] | 787 | swsusp_free(); |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 788 | } |
| 789 | return error; |
| 790 | } |
| 791 | |
Rafael J. Wysocki | 968808b8 | 2006-06-23 02:04:48 -0700 | [diff] [blame] | 792 | static void *get_buffer(struct snapshot_handle *handle) |
| 793 | { |
| 794 | struct pbe *pbe = handle->pbe, *last = handle->last_pbe; |
| 795 | struct page *page = virt_to_page(pbe->orig_address); |
| 796 | |
| 797 | if (PageNosave(page) && PageNosaveFree(page)) { |
| 798 | /* |
| 799 | * We have allocated the "original" page frame and we can |
| 800 | * use it directly to store the read page |
| 801 | */ |
| 802 | pbe->address = 0; |
| 803 | if (last && last->next) |
| 804 | last->next = NULL; |
| 805 | return (void *)pbe->orig_address; |
| 806 | } |
| 807 | /* |
| 808 | * The "original" page frame has not been allocated and we have to |
| 809 | * use a "safe" page frame to store the read page |
| 810 | */ |
| 811 | pbe->address = (unsigned long)safe_pages; |
| 812 | safe_pages = safe_pages->next; |
| 813 | if (last) |
| 814 | last->next = pbe; |
| 815 | handle->last_pbe = pbe; |
| 816 | return (void *)pbe->address; |
| 817 | } |
| 818 | |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 819 | /** |
| 820 | * snapshot_write_next - used for writing the system memory snapshot. |
| 821 | * |
| 822 | * On the first call to it @handle should point to a zeroed |
| 823 | * snapshot_handle structure. The structure gets updated and a pointer |
| 824 | * to it should be passed to this function every next time. |
| 825 | * |
| 826 | * The @count parameter should contain the number of bytes the caller |
| 827 | * wants to write to the image. It must not be zero. |
| 828 | * |
| 829 | * On success the function returns a positive number. Then, the caller |
| 830 | * is allowed to write up to the returned number of bytes to the memory |
| 831 | * location computed by the data_of() macro. The number returned |
| 832 | * may be smaller than @count, but this only happens if the write would |
| 833 | * cross a page boundary otherwise. |
| 834 | * |
| 835 | * The function returns 0 to indicate the "end of file" condition, |
| 836 | * and a negative number is returned on error. In such cases the |
| 837 | * structure pointed to by @handle is not updated and should not be used |
| 838 | * any more. |
| 839 | */ |
| 840 | |
| 841 | int snapshot_write_next(struct snapshot_handle *handle, size_t count) |
| 842 | { |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 843 | int error = 0; |
| 844 | |
Rafael J. Wysocki | fb13a28 | 2006-09-25 23:32:46 -0700 | [diff] [blame] | 845 | if (handle->prev && handle->cur > nr_meta_pages + nr_copy_pages) |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 846 | return 0; |
| 847 | if (!buffer) { |
| 848 | /* This makes the buffer be freed by swsusp_free() */ |
Rafael J. Wysocki | 0bcd888 | 2006-09-25 23:32:52 -0700 | [diff] [blame^] | 849 | buffer = alloc_image_page(GFP_ATOMIC, PG_ANY); |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 850 | if (!buffer) |
| 851 | return -ENOMEM; |
| 852 | } |
| 853 | if (!handle->offset) |
| 854 | handle->buffer = buffer; |
Andrew Morton | 546e0d2 | 2006-09-25 23:32:44 -0700 | [diff] [blame] | 855 | handle->sync_read = 1; |
Rafael J. Wysocki | fb13a28 | 2006-09-25 23:32:46 -0700 | [diff] [blame] | 856 | if (handle->prev < handle->cur) { |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 857 | if (!handle->prev) { |
Andrew Morton | 546e0d2 | 2006-09-25 23:32:44 -0700 | [diff] [blame] | 858 | error = load_header(handle, |
| 859 | (struct swsusp_info *)buffer); |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 860 | if (error) |
| 861 | return error; |
| 862 | } else if (handle->prev <= nr_meta_pages) { |
Andrew Morton | 546e0d2 | 2006-09-25 23:32:44 -0700 | [diff] [blame] | 863 | handle->pbe = unpack_orig_addresses(buffer, |
| 864 | handle->pbe); |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 865 | if (!handle->pbe) { |
Rafael J. Wysocki | 968808b8 | 2006-06-23 02:04:48 -0700 | [diff] [blame] | 866 | error = prepare_image(handle); |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 867 | if (error) |
| 868 | return error; |
Rafael J. Wysocki | 75534b5 | 2006-09-25 23:32:52 -0700 | [diff] [blame] | 869 | handle->pbe = restore_pblist; |
Rafael J. Wysocki | 968808b8 | 2006-06-23 02:04:48 -0700 | [diff] [blame] | 870 | handle->last_pbe = NULL; |
| 871 | handle->buffer = get_buffer(handle); |
Andrew Morton | 546e0d2 | 2006-09-25 23:32:44 -0700 | [diff] [blame] | 872 | handle->sync_read = 0; |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 873 | } |
| 874 | } else { |
| 875 | handle->pbe = handle->pbe->next; |
Rafael J. Wysocki | 968808b8 | 2006-06-23 02:04:48 -0700 | [diff] [blame] | 876 | handle->buffer = get_buffer(handle); |
Andrew Morton | 546e0d2 | 2006-09-25 23:32:44 -0700 | [diff] [blame] | 877 | handle->sync_read = 0; |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 878 | } |
Rafael J. Wysocki | fb13a28 | 2006-09-25 23:32:46 -0700 | [diff] [blame] | 879 | handle->prev = handle->cur; |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 880 | } |
Rafael J. Wysocki | fb13a28 | 2006-09-25 23:32:46 -0700 | [diff] [blame] | 881 | handle->buf_offset = handle->cur_offset; |
| 882 | if (handle->cur_offset + count >= PAGE_SIZE) { |
| 883 | count = PAGE_SIZE - handle->cur_offset; |
| 884 | handle->cur_offset = 0; |
| 885 | handle->cur++; |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 886 | } else { |
Rafael J. Wysocki | fb13a28 | 2006-09-25 23:32:46 -0700 | [diff] [blame] | 887 | handle->cur_offset += count; |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 888 | } |
| 889 | handle->offset += count; |
| 890 | return count; |
| 891 | } |
| 892 | |
| 893 | int snapshot_image_loaded(struct snapshot_handle *handle) |
| 894 | { |
| 895 | return !(!handle->pbe || handle->pbe->next || !nr_copy_pages || |
Rafael J. Wysocki | fb13a28 | 2006-09-25 23:32:46 -0700 | [diff] [blame] | 896 | handle->cur <= nr_meta_pages + nr_copy_pages); |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 897 | } |