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