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 | |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/mm.h> |
| 15 | #include <linux/suspend.h> |
| 16 | #include <linux/smp_lock.h> |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 17 | #include <linux/delay.h> |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 18 | #include <linux/bitops.h> |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 19 | #include <linux/spinlock.h> |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 20 | #include <linux/kernel.h> |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 21 | #include <linux/pm.h> |
| 22 | #include <linux/device.h> |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 23 | #include <linux/bootmem.h> |
| 24 | #include <linux/syscalls.h> |
| 25 | #include <linux/console.h> |
| 26 | #include <linux/highmem.h> |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 27 | |
| 28 | #include <asm/uaccess.h> |
| 29 | #include <asm/mmu_context.h> |
| 30 | #include <asm/pgtable.h> |
| 31 | #include <asm/tlbflush.h> |
| 32 | #include <asm/io.h> |
| 33 | |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 34 | #include "power.h" |
| 35 | |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 36 | #ifdef CONFIG_HIGHMEM |
| 37 | struct highmem_page { |
| 38 | char *data; |
| 39 | struct page *page; |
| 40 | struct highmem_page *next; |
| 41 | }; |
| 42 | |
| 43 | static struct highmem_page *highmem_copy; |
| 44 | |
| 45 | static int save_highmem_zone(struct zone *zone) |
| 46 | { |
| 47 | unsigned long zone_pfn; |
| 48 | mark_free_pages(zone); |
| 49 | for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn) { |
| 50 | struct page *page; |
| 51 | struct highmem_page *save; |
| 52 | void *kaddr; |
| 53 | unsigned long pfn = zone_pfn + zone->zone_start_pfn; |
| 54 | |
| 55 | if (!(pfn%1000)) |
| 56 | printk("."); |
| 57 | if (!pfn_valid(pfn)) |
| 58 | continue; |
| 59 | page = pfn_to_page(pfn); |
| 60 | /* |
| 61 | * This condition results from rvmalloc() sans vmalloc_32() |
| 62 | * and architectural memory reservations. This should be |
| 63 | * corrected eventually when the cases giving rise to this |
| 64 | * are better understood. |
| 65 | */ |
| 66 | if (PageReserved(page)) { |
| 67 | printk("highmem reserved page?!\n"); |
| 68 | continue; |
| 69 | } |
| 70 | BUG_ON(PageNosave(page)); |
| 71 | if (PageNosaveFree(page)) |
| 72 | continue; |
| 73 | save = kmalloc(sizeof(struct highmem_page), GFP_ATOMIC); |
| 74 | if (!save) |
| 75 | return -ENOMEM; |
| 76 | save->next = highmem_copy; |
| 77 | save->page = page; |
| 78 | save->data = (void *) get_zeroed_page(GFP_ATOMIC); |
| 79 | if (!save->data) { |
| 80 | kfree(save); |
| 81 | return -ENOMEM; |
| 82 | } |
| 83 | kaddr = kmap_atomic(page, KM_USER0); |
| 84 | memcpy(save->data, kaddr, PAGE_SIZE); |
| 85 | kunmap_atomic(kaddr, KM_USER0); |
| 86 | highmem_copy = save; |
| 87 | } |
| 88 | return 0; |
| 89 | } |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 90 | |
| 91 | |
| 92 | static int save_highmem(void) |
| 93 | { |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 94 | struct zone *zone; |
| 95 | int res = 0; |
| 96 | |
| 97 | pr_debug("swsusp: Saving Highmem\n"); |
| 98 | for_each_zone (zone) { |
| 99 | if (is_highmem(zone)) |
| 100 | res = save_highmem_zone(zone); |
| 101 | if (res) |
| 102 | return res; |
| 103 | } |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | int restore_highmem(void) |
| 108 | { |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 109 | printk("swsusp: Restoring Highmem\n"); |
| 110 | while (highmem_copy) { |
| 111 | struct highmem_page *save = highmem_copy; |
| 112 | void *kaddr; |
| 113 | highmem_copy = save->next; |
| 114 | |
| 115 | kaddr = kmap_atomic(save->page, KM_USER0); |
| 116 | memcpy(kaddr, save->data, PAGE_SIZE); |
| 117 | kunmap_atomic(kaddr, KM_USER0); |
| 118 | free_page((long) save->data); |
| 119 | kfree(save); |
| 120 | } |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 121 | return 0; |
| 122 | } |
Pavel Machek | de49186 | 2005-10-30 14:59:59 -0800 | [diff] [blame] | 123 | #else |
| 124 | static int save_highmem(void) { return 0; } |
| 125 | int restore_highmem(void) { return 0; } |
| 126 | #endif /* CONFIG_HIGHMEM */ |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 127 | |
| 128 | |
| 129 | static int pfn_is_nosave(unsigned long pfn) |
| 130 | { |
| 131 | unsigned long nosave_begin_pfn = __pa(&__nosave_begin) >> PAGE_SHIFT; |
| 132 | unsigned long nosave_end_pfn = PAGE_ALIGN(__pa(&__nosave_end)) >> PAGE_SHIFT; |
| 133 | return (pfn >= nosave_begin_pfn) && (pfn < nosave_end_pfn); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * saveable - Determine whether a page should be cloned or not. |
| 138 | * @pfn: The page |
| 139 | * |
| 140 | * We save a page if it's Reserved, and not in the range of pages |
| 141 | * statically defined as 'unsaveable', or if it isn't reserved, and |
| 142 | * isn't part of a free chunk of pages. |
| 143 | */ |
| 144 | |
Pavel Machek | de49186 | 2005-10-30 14:59:59 -0800 | [diff] [blame] | 145 | static int saveable(struct zone *zone, unsigned long *zone_pfn) |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 146 | { |
| 147 | unsigned long pfn = *zone_pfn + zone->zone_start_pfn; |
Pavel Machek | de49186 | 2005-10-30 14:59:59 -0800 | [diff] [blame] | 148 | struct page *page; |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 149 | |
| 150 | if (!pfn_valid(pfn)) |
| 151 | return 0; |
| 152 | |
| 153 | page = pfn_to_page(pfn); |
| 154 | BUG_ON(PageReserved(page) && PageNosave(page)); |
| 155 | if (PageNosave(page)) |
| 156 | return 0; |
| 157 | if (PageReserved(page) && pfn_is_nosave(pfn)) { |
| 158 | pr_debug("[nosave pfn 0x%lx]", pfn); |
| 159 | return 0; |
| 160 | } |
| 161 | if (PageNosaveFree(page)) |
| 162 | return 0; |
| 163 | |
| 164 | return 1; |
| 165 | } |
| 166 | |
Rafael J. Wysocki | a0f4965 | 2005-10-30 14:59:57 -0800 | [diff] [blame] | 167 | static unsigned count_data_pages(void) |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 168 | { |
| 169 | struct zone *zone; |
| 170 | unsigned long zone_pfn; |
Pavel Machek | dc19d50 | 2005-11-07 00:58:40 -0800 | [diff] [blame^] | 171 | unsigned int n = 0; |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 172 | |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 173 | for_each_zone (zone) { |
| 174 | if (is_highmem(zone)) |
| 175 | continue; |
| 176 | mark_free_pages(zone); |
| 177 | for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn) |
Rafael J. Wysocki | a0f4965 | 2005-10-30 14:59:57 -0800 | [diff] [blame] | 178 | n += saveable(zone, &zone_pfn); |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 179 | } |
Rafael J. Wysocki | a0f4965 | 2005-10-30 14:59:57 -0800 | [diff] [blame] | 180 | return n; |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 181 | } |
| 182 | |
Rafael J. Wysocki | a0f4965 | 2005-10-30 14:59:57 -0800 | [diff] [blame] | 183 | static void copy_data_pages(struct pbe *pblist) |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 184 | { |
| 185 | struct zone *zone; |
| 186 | unsigned long zone_pfn; |
Rafael J. Wysocki | a0f4965 | 2005-10-30 14:59:57 -0800 | [diff] [blame] | 187 | struct pbe *pbe, *p; |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 188 | |
Rafael J. Wysocki | a0f4965 | 2005-10-30 14:59:57 -0800 | [diff] [blame] | 189 | pbe = pblist; |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 190 | for_each_zone (zone) { |
| 191 | if (is_highmem(zone)) |
| 192 | continue; |
| 193 | mark_free_pages(zone); |
| 194 | /* This is necessary for swsusp_free() */ |
Rafael J. Wysocki | a0f4965 | 2005-10-30 14:59:57 -0800 | [diff] [blame] | 195 | for_each_pb_page (p, pblist) |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 196 | SetPageNosaveFree(virt_to_page(p)); |
Rafael J. Wysocki | a0f4965 | 2005-10-30 14:59:57 -0800 | [diff] [blame] | 197 | for_each_pbe (p, pblist) |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 198 | SetPageNosaveFree(virt_to_page(p->address)); |
| 199 | for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn) { |
| 200 | if (saveable(zone, &zone_pfn)) { |
Pavel Machek | de49186 | 2005-10-30 14:59:59 -0800 | [diff] [blame] | 201 | struct page *page; |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 202 | page = pfn_to_page(zone_pfn + zone->zone_start_pfn); |
| 203 | BUG_ON(!pbe); |
| 204 | pbe->orig_address = (unsigned long)page_address(page); |
| 205 | /* copy_page is not usable for copying task structs. */ |
| 206 | memcpy((void *)pbe->address, (void *)pbe->orig_address, PAGE_SIZE); |
| 207 | pbe = pbe->next; |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | BUG_ON(pbe); |
| 212 | } |
| 213 | |
| 214 | |
| 215 | /** |
| 216 | * free_pagedir - free pages allocated with alloc_pagedir() |
| 217 | */ |
| 218 | |
Rafael J. Wysocki | 2c1b4a5 | 2005-10-30 14:59:58 -0800 | [diff] [blame] | 219 | static void free_pagedir(struct pbe *pblist) |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 220 | { |
| 221 | struct pbe *pbe; |
| 222 | |
| 223 | while (pblist) { |
| 224 | pbe = (pblist + PB_PAGE_SKIP)->next; |
| 225 | ClearPageNosave(virt_to_page(pblist)); |
| 226 | ClearPageNosaveFree(virt_to_page(pblist)); |
| 227 | free_page((unsigned long)pblist); |
| 228 | pblist = pbe; |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * fill_pb_page - Create a list of PBEs on a given memory page |
| 234 | */ |
| 235 | |
| 236 | static inline void fill_pb_page(struct pbe *pbpage) |
| 237 | { |
| 238 | struct pbe *p; |
| 239 | |
| 240 | p = pbpage; |
| 241 | pbpage += PB_PAGE_SKIP; |
| 242 | do |
| 243 | p->next = p + 1; |
| 244 | while (++p < pbpage); |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * create_pbe_list - Create a list of PBEs on top of a given chain |
| 249 | * of memory pages allocated with alloc_pagedir() |
| 250 | */ |
| 251 | |
Pavel Machek | dc19d50 | 2005-11-07 00:58:40 -0800 | [diff] [blame^] | 252 | void create_pbe_list(struct pbe *pblist, unsigned int nr_pages) |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 253 | { |
| 254 | struct pbe *pbpage, *p; |
Pavel Machek | dc19d50 | 2005-11-07 00:58:40 -0800 | [diff] [blame^] | 255 | unsigned int num = PBES_PER_PAGE; |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 256 | |
| 257 | for_each_pb_page (pbpage, pblist) { |
| 258 | if (num >= nr_pages) |
| 259 | break; |
| 260 | |
| 261 | fill_pb_page(pbpage); |
| 262 | num += PBES_PER_PAGE; |
| 263 | } |
| 264 | if (pbpage) { |
| 265 | for (num -= PBES_PER_PAGE - 1, p = pbpage; num < nr_pages; p++, num++) |
| 266 | p->next = p + 1; |
| 267 | p->next = NULL; |
| 268 | } |
| 269 | pr_debug("create_pbe_list(): initialized %d PBEs\n", num); |
| 270 | } |
| 271 | |
| 272 | static void *alloc_image_page(void) |
| 273 | { |
| 274 | void *res = (void *)get_zeroed_page(GFP_ATOMIC | __GFP_COLD); |
| 275 | if (res) { |
| 276 | SetPageNosave(virt_to_page(res)); |
| 277 | SetPageNosaveFree(virt_to_page(res)); |
| 278 | } |
| 279 | return res; |
| 280 | } |
| 281 | |
| 282 | /** |
| 283 | * alloc_pagedir - Allocate the page directory. |
| 284 | * |
| 285 | * First, determine exactly how many pages we need and |
| 286 | * allocate them. |
| 287 | * |
| 288 | * We arrange the pages in a chain: each page is an array of PBES_PER_PAGE |
| 289 | * struct pbe elements (pbes) and the last element in the page points |
| 290 | * to the next page. |
| 291 | * |
| 292 | * On each page we set up a list of struct_pbe elements. |
| 293 | */ |
| 294 | |
Pavel Machek | dc19d50 | 2005-11-07 00:58:40 -0800 | [diff] [blame^] | 295 | struct pbe *alloc_pagedir(unsigned int nr_pages) |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 296 | { |
Pavel Machek | dc19d50 | 2005-11-07 00:58:40 -0800 | [diff] [blame^] | 297 | unsigned int num; |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 298 | struct pbe *pblist, *pbe; |
| 299 | |
| 300 | if (!nr_pages) |
| 301 | return NULL; |
| 302 | |
| 303 | pr_debug("alloc_pagedir(): nr_pages = %d\n", nr_pages); |
Pavel Machek | de49186 | 2005-10-30 14:59:59 -0800 | [diff] [blame] | 304 | pblist = alloc_image_page(); |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 305 | /* FIXME: rewrite this ugly loop */ |
| 306 | for (pbe = pblist, num = PBES_PER_PAGE; pbe && num < nr_pages; |
| 307 | pbe = pbe->next, num += PBES_PER_PAGE) { |
| 308 | pbe += PB_PAGE_SKIP; |
Pavel Machek | de49186 | 2005-10-30 14:59:59 -0800 | [diff] [blame] | 309 | pbe->next = alloc_image_page(); |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 310 | } |
| 311 | if (!pbe) { /* get_zeroed_page() failed */ |
| 312 | free_pagedir(pblist); |
| 313 | pblist = NULL; |
| 314 | } |
| 315 | return pblist; |
| 316 | } |
| 317 | |
| 318 | /** |
| 319 | * Free pages we allocated for suspend. Suspend pages are alocated |
| 320 | * before atomic copy, so we need to free them after resume. |
| 321 | */ |
| 322 | |
| 323 | void swsusp_free(void) |
| 324 | { |
| 325 | struct zone *zone; |
| 326 | unsigned long zone_pfn; |
| 327 | |
| 328 | for_each_zone(zone) { |
| 329 | for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn) |
| 330 | if (pfn_valid(zone_pfn + zone->zone_start_pfn)) { |
Pavel Machek | dc19d50 | 2005-11-07 00:58:40 -0800 | [diff] [blame^] | 331 | struct page *page; |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 332 | page = pfn_to_page(zone_pfn + zone->zone_start_pfn); |
| 333 | if (PageNosave(page) && PageNosaveFree(page)) { |
| 334 | ClearPageNosave(page); |
| 335 | ClearPageNosaveFree(page); |
| 336 | free_page((long) page_address(page)); |
| 337 | } |
| 338 | } |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | |
| 343 | /** |
| 344 | * enough_free_mem - Make sure we enough free memory to snapshot. |
| 345 | * |
| 346 | * Returns TRUE or FALSE after checking the number of available |
| 347 | * free pages. |
| 348 | */ |
| 349 | |
Pavel Machek | dc19d50 | 2005-11-07 00:58:40 -0800 | [diff] [blame^] | 350 | static int enough_free_mem(unsigned int nr_pages) |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 351 | { |
| 352 | pr_debug("swsusp: available memory: %u pages\n", nr_free_pages()); |
Rafael J. Wysocki | a0f4965 | 2005-10-30 14:59:57 -0800 | [diff] [blame] | 353 | return nr_free_pages() > (nr_pages + PAGES_FOR_IO + |
| 354 | (nr_pages + PBES_PER_PAGE - 1) / PBES_PER_PAGE); |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | |
Pavel Machek | dc19d50 | 2005-11-07 00:58:40 -0800 | [diff] [blame^] | 358 | static struct pbe *swsusp_alloc(unsigned int nr_pages) |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 359 | { |
Rafael J. Wysocki | a0f4965 | 2005-10-30 14:59:57 -0800 | [diff] [blame] | 360 | struct pbe *pblist, *p; |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 361 | |
Rafael J. Wysocki | a0f4965 | 2005-10-30 14:59:57 -0800 | [diff] [blame] | 362 | if (!(pblist = alloc_pagedir(nr_pages))) { |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 363 | printk(KERN_ERR "suspend: Allocating pagedir failed.\n"); |
Rafael J. Wysocki | a0f4965 | 2005-10-30 14:59:57 -0800 | [diff] [blame] | 364 | return NULL; |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 365 | } |
Rafael J. Wysocki | a0f4965 | 2005-10-30 14:59:57 -0800 | [diff] [blame] | 366 | create_pbe_list(pblist, nr_pages); |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 367 | |
Rafael J. Wysocki | a0f4965 | 2005-10-30 14:59:57 -0800 | [diff] [blame] | 368 | for_each_pbe (p, pblist) { |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 369 | p->address = (unsigned long)alloc_image_page(); |
| 370 | if (!p->address) { |
| 371 | printk(KERN_ERR "suspend: Allocating image pages failed.\n"); |
| 372 | swsusp_free(); |
Rafael J. Wysocki | a0f4965 | 2005-10-30 14:59:57 -0800 | [diff] [blame] | 373 | return NULL; |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 374 | } |
| 375 | } |
| 376 | |
Rafael J. Wysocki | a0f4965 | 2005-10-30 14:59:57 -0800 | [diff] [blame] | 377 | return pblist; |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 378 | } |
| 379 | |
Rafael J. Wysocki | 2e32a43 | 2005-10-30 15:00:00 -0800 | [diff] [blame] | 380 | asmlinkage int swsusp_save(void) |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 381 | { |
Pavel Machek | dc19d50 | 2005-11-07 00:58:40 -0800 | [diff] [blame^] | 382 | unsigned int nr_pages; |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 383 | |
| 384 | pr_debug("swsusp: critical section: \n"); |
| 385 | if (save_highmem()) { |
| 386 | printk(KERN_CRIT "swsusp: Not enough free pages for highmem\n"); |
| 387 | restore_highmem(); |
| 388 | return -ENOMEM; |
| 389 | } |
| 390 | |
| 391 | drain_local_pages(); |
Rafael J. Wysocki | a0f4965 | 2005-10-30 14:59:57 -0800 | [diff] [blame] | 392 | nr_pages = count_data_pages(); |
| 393 | printk("swsusp: Need to copy %u pages\n", nr_pages); |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 394 | |
| 395 | pr_debug("swsusp: pages needed: %u + %lu + %u, free: %u\n", |
Rafael J. Wysocki | a0f4965 | 2005-10-30 14:59:57 -0800 | [diff] [blame] | 396 | nr_pages, |
| 397 | (nr_pages + PBES_PER_PAGE - 1) / PBES_PER_PAGE, |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 398 | PAGES_FOR_IO, nr_free_pages()); |
| 399 | |
Rafael J. Wysocki | a0f4965 | 2005-10-30 14:59:57 -0800 | [diff] [blame] | 400 | /* This is needed because of the fixed size of swsusp_info */ |
| 401 | if (MAX_PBES < (nr_pages + PBES_PER_PAGE - 1) / PBES_PER_PAGE) |
| 402 | return -ENOSPC; |
| 403 | |
| 404 | if (!enough_free_mem(nr_pages)) { |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 405 | printk(KERN_ERR "swsusp: Not enough free memory\n"); |
| 406 | return -ENOMEM; |
| 407 | } |
| 408 | |
Rafael J. Wysocki | a0f4965 | 2005-10-30 14:59:57 -0800 | [diff] [blame] | 409 | if (!enough_swap(nr_pages)) { |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 410 | printk(KERN_ERR "swsusp: Not enough free swap\n"); |
| 411 | return -ENOSPC; |
| 412 | } |
| 413 | |
Rafael J. Wysocki | a0f4965 | 2005-10-30 14:59:57 -0800 | [diff] [blame] | 414 | pagedir_nosave = swsusp_alloc(nr_pages); |
| 415 | if (!pagedir_nosave) |
| 416 | return -ENOMEM; |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 417 | |
| 418 | /* During allocating of suspend pagedir, new cold pages may appear. |
| 419 | * Kill them. |
| 420 | */ |
| 421 | drain_local_pages(); |
Rafael J. Wysocki | a0f4965 | 2005-10-30 14:59:57 -0800 | [diff] [blame] | 422 | copy_data_pages(pagedir_nosave); |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 423 | |
| 424 | /* |
| 425 | * End of critical section. From now on, we can write to memory, |
| 426 | * but we should not touch disk. This specially means we must _not_ |
| 427 | * touch swap space! Except we must write out our image of course. |
| 428 | */ |
| 429 | |
Rafael J. Wysocki | a0f4965 | 2005-10-30 14:59:57 -0800 | [diff] [blame] | 430 | nr_copy_pages = nr_pages; |
| 431 | |
| 432 | printk("swsusp: critical section/: done (%d pages copied)\n", nr_pages); |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 433 | return 0; |
| 434 | } |