tuxonice_prepare_image.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239
  1. /*
  2. * kernel/power/tuxonice_prepare_image.c
  3. *
  4. * Copyright (C) 2003-2014 Nigel Cunningham (nigel at tuxonice net)
  5. *
  6. * This file is released under the GPLv2.
  7. *
  8. * We need to eat memory until we can:
  9. * 1. Perform the save without changing anything (RAM_NEEDED < #pages)
  10. * 2. Fit it all in available space (toiActiveAllocator->available_space() >=
  11. * main_storage_needed())
  12. * 3. Reload the pagedir and pageset1 to places that don't collide with their
  13. * final destinations, not knowing to what extent the resumed kernel will
  14. * overlap with the one loaded at boot time. I think the resumed kernel
  15. * should overlap completely, but I don't want to rely on this as it is
  16. * an unproven assumption. We therefore assume there will be no overlap at
  17. * all (worse case).
  18. * 4. Meet the user's requested limit (if any) on the size of the image.
  19. * The limit is in MB, so pages/256 (assuming 4K pages).
  20. *
  21. */
  22. #include <linux/highmem.h>
  23. #include <linux/freezer.h>
  24. #include <linux/hardirq.h>
  25. #include <linux/mmzone.h>
  26. #include <linux/console.h>
  27. #ifdef CONFIG_TOI_FIXUP
  28. #include <linux/syscalls.h> /* for sys_sync() */
  29. #include <linux/oom.h>
  30. #endif
  31. #include "tuxonice_pageflags.h"
  32. #include "tuxonice_modules.h"
  33. #include "tuxonice_io.h"
  34. #include "tuxonice_ui.h"
  35. #include "tuxonice_prepare_image.h"
  36. #include "tuxonice.h"
  37. #include "tuxonice_extent.h"
  38. #include "tuxonice_checksum.h"
  39. #include "tuxonice_sysfs.h"
  40. #include "tuxonice_alloc.h"
  41. #include "tuxonice_atomic_copy.h"
  42. #include "tuxonice_builtin.h"
  43. static unsigned long num_nosave, main_storage_allocated, storage_limit, header_storage_needed;
  44. unsigned long extra_pd1_pages_allowance = CONFIG_TOI_DEFAULT_EXTRA_PAGES_ALLOWANCE;
  45. long image_size_limit = CONFIG_TOI_DEFAULT_IMAGE_SIZE_LIMIT;
  46. static int no_ps2_needed;
  47. struct attention_list {
  48. struct task_struct *task;
  49. struct attention_list *next;
  50. };
  51. static struct attention_list *attention_list;
  52. #define PAGESET1 0
  53. #define PAGESET2 1
  54. void free_attention_list(void)
  55. {
  56. struct attention_list *last = NULL;
  57. while (attention_list) {
  58. last = attention_list;
  59. attention_list = attention_list->next;
  60. toi_kfree(6, last, sizeof(*last));
  61. }
  62. }
  63. static int build_attention_list(void)
  64. {
  65. int i, task_count = 0;
  66. struct task_struct *p;
  67. struct attention_list *next;
  68. #ifdef CONFIG_TOI_FIXUP
  69. int task_count2 = 0, task_count3 = 0;
  70. #endif
  71. /*
  72. * Count all userspace process (with task->mm) marked PF_NOFREEZE.
  73. */
  74. toi_read_lock_tasklist();
  75. for_each_process(p)
  76. if ((p->flags & PF_NOFREEZE) || p == current)
  77. task_count++;
  78. toi_read_unlock_tasklist();
  79. /*
  80. * Allocate attention list structs.
  81. */
  82. for (i = 0; i < task_count; i++) {
  83. struct attention_list *this = toi_kzalloc(6, sizeof(struct attention_list),
  84. TOI_WAIT_GFP);
  85. if (!this) {
  86. pr_warn("Failed to allocate slab for " "attention list.\n");
  87. free_attention_list();
  88. return 1;
  89. }
  90. this->next = NULL;
  91. if (attention_list)
  92. this->next = attention_list;
  93. attention_list = this;
  94. }
  95. next = attention_list;
  96. toi_read_lock_tasklist();
  97. for_each_process(p)
  98. if ((p->flags & PF_NOFREEZE) || p == current) {
  99. #ifdef CONFIG_TOI_FIXUP
  100. task_count2++;
  101. if (next == NULL)
  102. goto ERR;
  103. #endif
  104. next->task = p;
  105. next = next->next;
  106. }
  107. toi_read_unlock_tasklist();
  108. return 0;
  109. #ifdef CONFIG_TOI_FIXUP
  110. ERR:
  111. hib_err("WARN (%d/%d)\n", task_count, task_count2);
  112. hib_err("DUMP tasks......\n");
  113. for_each_process(p)
  114. if ((p->flags & PF_NOFREEZE) || p == current) {
  115. task_count3++;
  116. hib_err("%s(0x%08x) ", p->comm, p->flags);
  117. }
  118. hib_err("DUMP tasks (#%d) done.\n", task_count3);
  119. toi_read_unlock_tasklist();
  120. return 1;
  121. #endif
  122. }
  123. static void pageset2_full(void)
  124. {
  125. struct zone *zone;
  126. struct page *page;
  127. unsigned long flags;
  128. int i;
  129. toi_trace_index++;
  130. for_each_populated_zone(zone) {
  131. #if defined(CONFIG_TOI_FIXUP) && defined(CONFIG_CGROUP_MEM_RES_CTLR)
  132. struct mem_cgroup *memcg;
  133. struct lruvec *lruvec;
  134. /* Root memcg */
  135. memcg = mem_cgroup_iter(NULL, NULL, NULL);
  136. do {
  137. /* Find corresponding lruvec */
  138. lruvec = mem_cgroup_zone_lruvec(zone, memcg);
  139. /* Go through memcg lrus */
  140. spin_lock_irqsave(&zone->lru_lock, flags);
  141. for_each_lru(i) {
  142. /* Is this memcg lru[i] empty? */
  143. if (!mem_cgroup_zone_nr_lru_pages
  144. (memcg, zone_to_nid(zone), zone_idx(zone), BIT(i)))
  145. continue;
  146. /* Scan this lru */
  147. list_for_each_entry(page, &lruvec->lists[i], lru) {
  148. struct address_space *mapping;
  149. mapping = page_mapping(page);
  150. if (!mapping || !mapping->host ||
  151. !(mapping->host->i_flags & S_ATOMIC_COPY))
  152. SetPagePageset2(page);
  153. }
  154. }
  155. spin_unlock_irqrestore(&zone->lru_lock, flags);
  156. /* Next memcg */
  157. memcg = mem_cgroup_iter(NULL, memcg, NULL);
  158. } while (memcg);
  159. #else
  160. spin_lock_irqsave(&zone->lru_lock, flags);
  161. for_each_lru(i) {
  162. if (!zone_page_state(zone, NR_LRU_BASE + i))
  163. continue;
  164. list_for_each_entry(page, &zone->lruvec.lists[i], lru) {
  165. struct address_space *mapping;
  166. mapping = page_mapping(page);
  167. if (!mapping || !mapping->host ||
  168. !(mapping->host->i_flags & S_ATOMIC_COPY)) {
  169. TOI_TRACE_DEBUG(page_to_pfn(page),
  170. "_Pageset2 pageset2_full.");
  171. SetPagePageset2(page);
  172. }
  173. }
  174. }
  175. spin_unlock_irqrestore(&zone->lru_lock, flags);
  176. #endif
  177. }
  178. }
  179. /*
  180. * toi_mark_task_as_pageset
  181. * Functionality : Marks all the saveable pages belonging to a given process
  182. * as belonging to a particular pageset.
  183. */
  184. static void toi_mark_task_as_pageset(struct task_struct *t, int pageset2)
  185. {
  186. struct vm_area_struct *vma;
  187. struct mm_struct *mm;
  188. mm = t->active_mm;
  189. #ifdef CONFIG_TOI_FIXUP
  190. if (mm == (void *)0x6b6b6b6b || mm == (void *)0x6b6b6b6b6b6b6b6b) {
  191. pr_err("[%s] use after free: task %s rq(%d)\n", __func__, t->comm, t->on_rq);
  192. WARN_ON(1);
  193. return;
  194. }
  195. #endif
  196. if (!mm || !mm->mmap)
  197. return;
  198. toi_trace_index++;
  199. if (!irqs_disabled())
  200. down_read(&mm->mmap_sem);
  201. for (vma = mm->mmap; vma; vma = vma->vm_next) {
  202. unsigned long posn;
  203. if (!vma->vm_start || vma->vm_flags & (VM_IO | VM_DONTDUMP | VM_PFNMAP))
  204. continue;
  205. for (posn = vma->vm_start; posn < vma->vm_end; posn += PAGE_SIZE) {
  206. struct page *page = follow_page(vma, posn, 0);
  207. struct address_space *mapping;
  208. if (!page || !pfn_valid(page_to_pfn(page)))
  209. continue;
  210. mapping = page_mapping(page);
  211. if (mapping && mapping->host && mapping->host->i_flags & S_ATOMIC_COPY)
  212. continue;
  213. if (pageset2) {
  214. TOI_TRACE_DEBUG(page_to_pfn(page), "_MarkTaskAsPageset 1");
  215. SetPagePageset2(page);
  216. } else {
  217. TOI_TRACE_DEBUG(page_to_pfn(page), "_MarkTaskAsPageset 2");
  218. ClearPagePageset2(page);
  219. SetPagePageset1(page);
  220. }
  221. }
  222. }
  223. if (!irqs_disabled())
  224. up_read(&mm->mmap_sem);
  225. }
  226. static void mark_tasks(int pageset)
  227. {
  228. struct task_struct *p;
  229. toi_read_lock_tasklist();
  230. for_each_process(p) {
  231. if (!p->mm)
  232. continue;
  233. if (p->flags & PF_KTHREAD)
  234. continue;
  235. toi_mark_task_as_pageset(p, pageset);
  236. }
  237. toi_read_unlock_tasklist();
  238. }
  239. /* mark_pages_for_pageset2
  240. *
  241. * Description: Mark unshared pages in processes not needed for hibernate as
  242. * being able to be written out in a separate pagedir.
  243. * HighMem pages are simply marked as pageset2. They won't be
  244. * needed during hibernate.
  245. */
  246. static void toi_mark_pages_for_pageset2(void)
  247. {
  248. struct attention_list *this = attention_list;
  249. memory_bm_clear(pageset2_map);
  250. if (test_action_state(TOI_NO_PAGESET2) || no_ps2_needed)
  251. return;
  252. if (test_action_state(TOI_PAGESET2_FULL))
  253. pageset2_full();
  254. else
  255. mark_tasks(PAGESET2);
  256. /*
  257. * Because the tasks in attention_list are ones related to hibernating,
  258. * we know that they won't go away under us.
  259. */
  260. while (this) {
  261. if (!test_result_state(TOI_ABORTED))
  262. toi_mark_task_as_pageset(this->task, PAGESET1);
  263. this = this->next;
  264. }
  265. }
  266. /*
  267. * The atomic copy of pageset1 is stored in pageset2 pages.
  268. * But if pageset1 is larger (normally only just after boot),
  269. * we need to allocate extra pages to store the atomic copy.
  270. * The following data struct and functions are used to handle
  271. * the allocation and freeing of that memory.
  272. */
  273. static unsigned long extra_pages_allocated;
  274. struct extras {
  275. struct page *page;
  276. int order;
  277. struct extras *next;
  278. };
  279. static struct extras *extras_list;
  280. /* toi_free_extra_pagedir_memory
  281. *
  282. * Description: Free previously allocated extra pagedir memory.
  283. */
  284. void toi_free_extra_pagedir_memory(void)
  285. {
  286. /* Free allocated pages */
  287. while (extras_list) {
  288. struct extras *this = extras_list;
  289. int i;
  290. extras_list = this->next;
  291. for (i = 0; i < (1 << this->order); i++)
  292. ClearPageNosave(this->page + i);
  293. #ifdef CONFIG_TOI_ENHANCE
  294. /* keep clean contents for better compression next time */
  295. clear_page(page_address(this->page));
  296. #endif
  297. toi_free_pages(9, this->page, this->order);
  298. toi_kfree(7, this, sizeof(*this));
  299. }
  300. extra_pages_allocated = 0;
  301. }
  302. #ifdef CONFIG_TOI_FIXUP
  303. #define TOI_BUFFER_RESERVED ((70*1024*1024) / PAGE_SIZE)
  304. static int is_memory_low(unsigned long wanted)
  305. {
  306. unsigned long free_pages;
  307. free_pages = real_nr_free_low_pages();
  308. if (free_pages < (wanted + TOI_BUFFER_RESERVED)) {
  309. hib_warn("memory status: free(%lu) < wanted(%lu)+reserved(%lu)\n",
  310. free_pages, wanted, TOI_BUFFER_RESERVED);
  311. HIB_SHOW_MEMINFO();
  312. return 1;
  313. }
  314. return 0;
  315. }
  316. #endif
  317. /* toi_allocate_extra_pagedir_memory
  318. *
  319. * Description: Allocate memory for making the atomic copy of pagedir1 in the
  320. * case where it is bigger than pagedir2.
  321. * Arguments: int num_to_alloc: Number of extra pages needed.
  322. * Result: int. Number of extra pages we now have allocated.
  323. */
  324. static int toi_allocate_extra_pagedir_memory(int extra_pages_needed)
  325. {
  326. int j, order, num_to_alloc = extra_pages_needed - extra_pages_allocated;
  327. #ifdef CONFIG_TOI_FIXUP
  328. gfp_t flags = TOI_ATOMIC_GFP | __GFP_NO_KSWAPD;
  329. #else
  330. gfp_t flags = TOI_ATOMIC_GFP;
  331. #endif
  332. if (num_to_alloc < 1)
  333. return 0;
  334. order = fls(num_to_alloc);
  335. if (order >= MAX_ORDER)
  336. order = MAX_ORDER - 1;
  337. while (num_to_alloc) {
  338. struct page *newpage;
  339. unsigned long virt;
  340. struct extras *extras_entry;
  341. while ((1 << order) > num_to_alloc)
  342. order--;
  343. #ifdef CONFIG_TOI_FIXUP
  344. if (is_memory_low(4))
  345. return extra_pages_allocated;
  346. #endif
  347. extras_entry = (struct extras *)toi_kzalloc(7,
  348. sizeof(struct extras), TOI_ATOMIC_GFP);
  349. if (!extras_entry)
  350. return extra_pages_allocated;
  351. virt = toi_get_free_pages(9, flags, order);
  352. while (!virt && order) {
  353. order--;
  354. virt = toi_get_free_pages(9, flags, order);
  355. }
  356. #ifdef CONFIG_TOI_FIXUP
  357. if (virt && is_memory_low(0)) {
  358. toi_free_pages(9, virt_to_page((void *)virt), order);
  359. virt = 0;
  360. }
  361. #endif
  362. if (!virt) {
  363. toi_kfree(7, extras_entry, sizeof(*extras_entry));
  364. return extra_pages_allocated;
  365. }
  366. newpage = virt_to_page(virt);
  367. extras_entry->page = newpage;
  368. extras_entry->order = order;
  369. extras_entry->next = extras_list;
  370. extras_list = extras_entry;
  371. for (j = 0; j < (1 << order); j++) {
  372. SetPageNosave(newpage + j);
  373. SetPagePageset1Copy(newpage + j);
  374. }
  375. extra_pages_allocated += (1 << order);
  376. num_to_alloc -= (1 << order);
  377. }
  378. return extra_pages_allocated;
  379. }
  380. /*
  381. * real_nr_free_pages: Count pcp pages for a zone type or all zones
  382. * (-1 for all, otherwise zone_idx() result desired).
  383. */
  384. unsigned long real_nr_free_pages(unsigned long zone_idx_mask)
  385. {
  386. struct zone *zone;
  387. int result = 0, cpu;
  388. /* PCP lists */
  389. for_each_populated_zone(zone) {
  390. if (!(zone_idx_mask & (1 << zone_idx(zone))))
  391. continue;
  392. for_each_online_cpu(cpu) {
  393. struct per_cpu_pageset *pset = per_cpu_ptr(zone->pageset, cpu);
  394. struct per_cpu_pages *pcp = &pset->pcp;
  395. result += pcp->count;
  396. }
  397. result += zone_page_state(zone, NR_FREE_PAGES);
  398. }
  399. return result;
  400. }
  401. EXPORT_SYMBOL_GPL(real_nr_free_pages);
  402. /*
  403. * Discover how much extra memory will be required by the drivers
  404. * when they're asked to hibernate. We can then ensure that amount
  405. * of memory is available when we really want it.
  406. */
  407. static void get_extra_pd1_allowance(void)
  408. {
  409. unsigned long orig_num_free = real_nr_free_pages(all_zones_mask), final;
  410. toi_prepare_status(CLEAR_BAR, "Finding allowance for drivers.");
  411. if (toi_go_atomic(PMSG_FREEZE, 1))
  412. return;
  413. final = real_nr_free_pages(all_zones_mask);
  414. toi_end_atomic(ATOMIC_ALL_STEPS, 1, 0);
  415. extra_pd1_pages_allowance = (orig_num_free > final) ?
  416. orig_num_free - final + MIN_EXTRA_PAGES_ALLOWANCE : MIN_EXTRA_PAGES_ALLOWANCE;
  417. }
  418. /*
  419. * Amount of storage needed, possibly taking into account the
  420. * expected compression ratio and possibly also ignoring our
  421. * allowance for extra pages.
  422. */
  423. static unsigned long main_storage_needed(int use_ecr, int ignore_extra_pd1_allow)
  424. {
  425. return (pagedir1.size + pagedir2.size +
  426. (ignore_extra_pd1_allow ? 0 : extra_pd1_pages_allowance)) *
  427. (use_ecr ? toi_expected_compression_ratio() : 100) / 100;
  428. }
  429. /*
  430. * Storage needed for the image header, in bytes until the return.
  431. */
  432. unsigned long get_header_storage_needed(void)
  433. {
  434. unsigned long bytes = sizeof(struct toi_header) +
  435. toi_header_storage_for_modules() +
  436. toi_pageflags_space_needed() + fs_info_space_needed();
  437. return DIV_ROUND_UP(bytes, PAGE_SIZE);
  438. }
  439. EXPORT_SYMBOL_GPL(get_header_storage_needed);
  440. /*
  441. * When freeing memory, pages from either pageset might be freed.
  442. *
  443. * When seeking to free memory to be able to hibernate, for every ps1 page
  444. * freed, we need 2 less pages for the atomic copy because there is one less
  445. * page to copy and one more page into which data can be copied.
  446. *
  447. * Freeing ps2 pages saves us nothing directly. No more memory is available
  448. * for the atomic copy. Indirectly, a ps1 page might be freed (slab?), but
  449. * that's too much work to figure out.
  450. *
  451. * => ps1_to_free functions
  452. *
  453. * Of course if we just want to reduce the image size, because of storage
  454. * limitations or an image size limit either ps will do.
  455. *
  456. * => any_to_free function
  457. */
  458. static unsigned long lowpages_usable_for_highmem_copy(void)
  459. {
  460. unsigned long needed = get_lowmem_size(pagedir1) +
  461. extra_pd1_pages_allowance + MIN_FREE_RAM +
  462. toi_memory_for_modules(0),
  463. available = get_lowmem_size(pagedir2) +
  464. real_nr_free_low_pages() + extra_pages_allocated;
  465. return available > needed ? available - needed : 0;
  466. }
  467. static unsigned long highpages_ps1_to_free(void)
  468. {
  469. unsigned long need = get_highmem_size(pagedir1),
  470. available = get_highmem_size(pagedir2) +
  471. real_nr_free_high_pages() + lowpages_usable_for_highmem_copy();
  472. return need > available ? DIV_ROUND_UP(need - available, 2) : 0;
  473. }
  474. static unsigned long lowpages_ps1_to_free(void)
  475. {
  476. unsigned long needed = get_lowmem_size(pagedir1) +
  477. extra_pd1_pages_allowance + MIN_FREE_RAM +
  478. toi_memory_for_modules(0),
  479. available = get_lowmem_size(pagedir2) +
  480. real_nr_free_low_pages() + extra_pages_allocated;
  481. return needed > available ? DIV_ROUND_UP(needed - available, 2) : 0;
  482. }
  483. static unsigned long current_image_size(void)
  484. {
  485. return pagedir1.size + pagedir2.size + header_storage_needed;
  486. }
  487. static unsigned long storage_still_required(void)
  488. {
  489. unsigned long needed = main_storage_needed(1, 1);
  490. return needed > storage_limit ? needed - storage_limit : 0;
  491. }
  492. static unsigned long ram_still_required(void)
  493. {
  494. unsigned long needed = MIN_FREE_RAM + toi_memory_for_modules(0) +
  495. 2 * extra_pd1_pages_allowance,
  496. available = real_nr_free_low_pages() + extra_pages_allocated;
  497. return needed > available ? needed - available : 0;
  498. }
  499. unsigned long any_to_free(int use_image_size_limit)
  500. {
  501. int use_soft_limit = use_image_size_limit && image_size_limit > 0;
  502. unsigned long current_size = current_image_size(),
  503. soft_limit = use_soft_limit ? (image_size_limit << 8) : 0,
  504. to_free = use_soft_limit ? (current_size > soft_limit ?
  505. current_size - soft_limit : 0) : 0,
  506. storage_limit = storage_still_required(),
  507. ram_limit = ram_still_required(), first_max = max(to_free, storage_limit);
  508. return max(first_max, ram_limit);
  509. }
  510. static int need_pageset2(void)
  511. {
  512. return (real_nr_free_low_pages() + extra_pages_allocated -
  513. 2 * extra_pd1_pages_allowance - MIN_FREE_RAM -
  514. toi_memory_for_modules(0) - pagedir1.size) < pagedir2.size;
  515. }
  516. /* amount_needed
  517. *
  518. * Calculates the amount by which the image size needs to be reduced to meet
  519. * our constraints.
  520. */
  521. static unsigned long amount_needed(int use_image_size_limit)
  522. {
  523. return max(highpages_ps1_to_free() + lowpages_ps1_to_free(),
  524. any_to_free(use_image_size_limit));
  525. }
  526. static int image_not_ready(int use_image_size_limit)
  527. {
  528. toi_message(TOI_EAT_MEMORY, TOI_LOW, 1,
  529. "Amount still needed (%lu) > 0:%u, Storage allocd: %lu < %lu: %u.\n",
  530. amount_needed(use_image_size_limit),
  531. (amount_needed(use_image_size_limit) > 0),
  532. main_storage_allocated,
  533. main_storage_needed(1, 1), main_storage_allocated < main_storage_needed(1, 1));
  534. toi_cond_pause(0, NULL);
  535. return (amount_needed(use_image_size_limit) > 0) ||
  536. main_storage_allocated < main_storage_needed(1, 1);
  537. }
  538. static void display_failure_reason(int tries_exceeded)
  539. {
  540. unsigned long storage_required = storage_still_required(),
  541. ram_required = ram_still_required(),
  542. high_ps1 = highpages_ps1_to_free(), low_ps1 = lowpages_ps1_to_free();
  543. pr_warn("Failed to prepare the image because...\n");
  544. if (!storage_limit) {
  545. pr_warn("- You need some storage available to be " "able to hibernate.\n");
  546. return;
  547. }
  548. if (tries_exceeded)
  549. pr_warn("- The maximum number of iterations was reached without successfully preparing the image.\n");
  550. if (storage_required) {
  551. pr_warn(" - We need at least %lu pages of storage (ignoring the header), but only have %lu.\n",
  552. main_storage_needed(1, 1), main_storage_allocated);
  553. set_abort_result(TOI_INSUFFICIENT_STORAGE);
  554. }
  555. if (ram_required) {
  556. pr_warn(" - We need %lu more free pages of low memory.\n", ram_required);
  557. pr_warn(" Minimum free : %8d\n", MIN_FREE_RAM);
  558. pr_warn(" + Reqd. by modules : %8lu\n", toi_memory_for_modules(0));
  559. pr_warn(" + 2 * extra allow : %8lu\n", 2 * extra_pd1_pages_allowance);
  560. pr_warn(" - Currently free : %8lu\n", real_nr_free_low_pages());
  561. pr_warn(" - Pages allocd : %8lu\n", extra_pages_allocated);
  562. pr_warn(" : ========\n");
  563. pr_warn(" Still needed : %8lu\n", ram_required);
  564. /* Print breakdown of memory needed for modules */
  565. toi_memory_for_modules(1);
  566. set_abort_result(TOI_UNABLE_TO_FREE_ENOUGH_MEMORY);
  567. }
  568. if (high_ps1) {
  569. pr_warn("- We need to free %lu highmem pageset 1 " "pages.\n", high_ps1);
  570. set_abort_result(TOI_UNABLE_TO_FREE_ENOUGH_MEMORY);
  571. }
  572. if (low_ps1) {
  573. pr_warn(" - We need to free %ld lowmem pageset 1 " "pages.\n", low_ps1);
  574. set_abort_result(TOI_UNABLE_TO_FREE_ENOUGH_MEMORY);
  575. }
  576. }
  577. static void display_stats(int always, int sub_extra_pd1_allow)
  578. {
  579. int len = 0;
  580. char buffer[255];
  581. len = snprintf(buffer, 254,
  582. "Free:%lu(%lu). Sets:%lu(%lu),%lu(%lu). Nosave:%lu-%lu=%lu. Storage:%lu/%lu(%lu=>%lu). ",
  583. /* Free */
  584. real_nr_free_pages(all_zones_mask), real_nr_free_low_pages(),
  585. /* Sets */
  586. pagedir1.size, pagedir1.size - get_highmem_size(pagedir1),
  587. pagedir2.size, pagedir2.size - get_highmem_size(pagedir2),
  588. /* Nosave */
  589. num_nosave, extra_pages_allocated, num_nosave - extra_pages_allocated,
  590. /* Storage */
  591. main_storage_allocated,
  592. storage_limit,
  593. main_storage_needed(1, sub_extra_pd1_allow), main_storage_needed(1, 1));
  594. if (len >= 0 && len < 255)
  595. len += snprintf(buffer+len, 254-len,
  596. "Needed:%lu,%lu,%lu(%u,%lu,%lu,%ld) (PS2:%s)\n",
  597. /* Needed */
  598. lowpages_ps1_to_free(), highpages_ps1_to_free(),
  599. any_to_free(1),
  600. MIN_FREE_RAM, toi_memory_for_modules(0),
  601. extra_pd1_pages_allowance, image_size_limit, need_pageset2() ? "yes" : "no");
  602. if (always)
  603. pr_warn("%s", buffer);
  604. else
  605. toi_message(TOI_EAT_MEMORY, TOI_MEDIUM, 1, "%s", buffer);
  606. }
  607. /* generate_free_page_map
  608. *
  609. * Description: This routine generates a bitmap of free pages from the
  610. * lists used by the memory manager. We then use the bitmap
  611. * to quickly calculate which pages to save and in which
  612. * pagesets.
  613. */
  614. static void generate_free_page_map(void)
  615. {
  616. int order, cpu, t;
  617. unsigned long flags, i;
  618. struct zone *zone;
  619. struct list_head *curr;
  620. unsigned long pfn;
  621. struct page *page;
  622. for_each_populated_zone(zone) {
  623. if (!zone->spanned_pages)
  624. continue;
  625. spin_lock_irqsave(&zone->lock, flags);
  626. for (i = 0; i < zone->spanned_pages; i++) {
  627. pfn = zone->zone_start_pfn + i;
  628. if (!pfn_valid(pfn))
  629. continue;
  630. page = pfn_to_page(pfn);
  631. ClearPageNosaveFree(page);
  632. }
  633. for_each_migratetype_order(order, t) {
  634. list_for_each(curr, &zone->free_area[order].free_list[t]) {
  635. unsigned long j;
  636. pfn = page_to_pfn(list_entry(curr, struct page, lru));
  637. for (j = 0; j < (1UL << order); j++)
  638. SetPageNosaveFree(pfn_to_page(pfn + j));
  639. }
  640. }
  641. for_each_online_cpu(cpu) {
  642. struct per_cpu_pageset *pset = per_cpu_ptr(zone->pageset, cpu);
  643. struct per_cpu_pages *pcp = &pset->pcp;
  644. struct page *page;
  645. int t;
  646. for (t = 0; t < MIGRATE_PCPTYPES; t++)
  647. list_for_each_entry(page, &pcp->lists[t], lru)
  648. SetPageNosaveFree(page);
  649. }
  650. spin_unlock_irqrestore(&zone->lock, flags);
  651. }
  652. }
  653. /* size_of_free_region
  654. *
  655. * Description: Return the number of pages that are free, beginning with and
  656. * including this one.
  657. */
  658. static int size_of_free_region(struct zone *zone, unsigned long start_pfn)
  659. {
  660. unsigned long this_pfn = start_pfn, end_pfn = zone_end_pfn(zone);
  661. while (pfn_valid(this_pfn) && this_pfn < end_pfn && PageNosaveFree(pfn_to_page(this_pfn)))
  662. this_pfn++;
  663. return this_pfn - start_pfn;
  664. }
  665. /* flag_image_pages
  666. *
  667. * This routine generates our lists of pages to be stored in each
  668. * pageset. Since we store the data using extents, and adding new
  669. * extents might allocate a new extent page, this routine may well
  670. * be called more than once.
  671. */
  672. static void flag_image_pages(int atomic_copy)
  673. {
  674. int num_free = 0;
  675. unsigned long loop;
  676. struct zone *zone;
  677. pagedir1.size = 0;
  678. pagedir2.size = 0;
  679. set_highmem_size(pagedir1, 0);
  680. set_highmem_size(pagedir2, 0);
  681. num_nosave = 0;
  682. toi_trace_index++;
  683. memory_bm_clear(pageset1_map);
  684. generate_free_page_map();
  685. /*
  686. * Pages not to be saved are marked Nosave irrespective of being
  687. * reserved.
  688. */
  689. for_each_populated_zone(zone) {
  690. int highmem = is_highmem(zone);
  691. for (loop = 0; loop < zone->spanned_pages; loop++) {
  692. unsigned long pfn = zone->zone_start_pfn + loop;
  693. struct page *page;
  694. int chunk_size;
  695. if (!pfn_valid(pfn)) {
  696. TOI_TRACE_DEBUG(pfn, "_Flag Invalid");
  697. continue;
  698. }
  699. chunk_size = size_of_free_region(zone, pfn);
  700. if (chunk_size) {
  701. unsigned long y;
  702. for (y = pfn; y < pfn + chunk_size; y++) {
  703. page = pfn_to_page(y);
  704. TOI_TRACE_DEBUG(y, "_Flag Free");
  705. ClearPagePageset1(page);
  706. ClearPagePageset2(page);
  707. }
  708. num_free += chunk_size;
  709. loop += chunk_size - 1;
  710. continue;
  711. }
  712. page = pfn_to_page(pfn);
  713. if (PageNosave(page)) {
  714. char *desc = PagePageset1Copy(page) ? "Pageset1Copy" : "NoSave";
  715. TOI_TRACE_DEBUG(pfn, "_Flag %s", desc);
  716. num_nosave++;
  717. continue;
  718. }
  719. page = highmem ? saveable_highmem_page(zone, pfn) :
  720. saveable_page(zone, pfn);
  721. if (!page) {
  722. TOI_TRACE_DEBUG(pfn, "_Flag Nosave2");
  723. num_nosave++;
  724. continue;
  725. }
  726. if (PagePageset2(page)) {
  727. pagedir2.size++;
  728. TOI_TRACE_DEBUG(pfn, "_Flag PS2");
  729. if (PageHighMem(page))
  730. inc_highmem_size(pagedir2);
  731. else
  732. SetPagePageset1Copy(page);
  733. if (PageResave(page)) {
  734. SetPagePageset1(page);
  735. ClearPagePageset1Copy(page);
  736. pagedir1.size++;
  737. if (PageHighMem(page))
  738. inc_highmem_size(pagedir1);
  739. }
  740. } else {
  741. pagedir1.size++;
  742. TOI_TRACE_DEBUG(pfn, "_Flag PS1");
  743. SetPagePageset1(page);
  744. if (PageHighMem(page))
  745. inc_highmem_size(pagedir1);
  746. }
  747. }
  748. }
  749. if (!atomic_copy)
  750. toi_message(TOI_EAT_MEMORY, TOI_MEDIUM, 0,
  751. "Count data pages: Set1 (%lu) + Set2 (%lu) + Nosave (%lu) + NumFree (%d) = %lu.\n",
  752. pagedir1.size, pagedir2.size, num_nosave, num_free,
  753. (unsigned long) (pagedir1.size + pagedir2.size + num_nosave + num_free));
  754. }
  755. void toi_recalculate_image_contents(int atomic_copy)
  756. {
  757. memory_bm_clear(pageset1_map);
  758. if (!atomic_copy) {
  759. unsigned long pfn;
  760. memory_bm_position_reset(pageset2_map);
  761. for (pfn = memory_bm_next_pfn(pageset2_map, 0);
  762. pfn != BM_END_OF_MAP; pfn = memory_bm_next_pfn(pageset2_map, 0))
  763. ClearPagePageset1Copy(pfn_to_page(pfn));
  764. /* Need to call this before getting pageset1_size! */
  765. toi_mark_pages_for_pageset2();
  766. }
  767. memory_bm_position_reset(pageset2_map);
  768. flag_image_pages(atomic_copy);
  769. if (!atomic_copy) {
  770. storage_limit = toiActiveAllocator->storage_available();
  771. #ifdef CONFIG_TOI_FIXUP
  772. display_stats(1, 0);
  773. #else
  774. display_stats(0, 0);
  775. #endif
  776. }
  777. }
  778. int try_allocate_extra_memory(void)
  779. {
  780. #ifdef CONFIG_TOI_FIXUP
  781. int wanted = pagedir1.size + extra_pd1_pages_allowance - get_lowmem_size(pagedir2);
  782. if ((wanted > 0) && (wanted > extra_pages_allocated)) {
  783. int got = toi_allocate_extra_pagedir_memory(wanted);
  784. hib_warn("%s: Want %d extra pages for pageset1, got %d, free:%lu\n",
  785. wanted > got ? "FAIL" : "PASS", wanted, got, real_nr_free_low_pages());
  786. if (unlikely(wanted > got))
  787. return 1;
  788. } else
  789. hib_warn("PASS: Want %lu extra pages for pageset1, got %lu, free:%lu\n",
  790. pagedir1.size + extra_pd1_pages_allowance,
  791. extra_pages_allocated + get_lowmem_size(pagedir2),
  792. real_nr_free_low_pages());
  793. #else /* buggy codes, (1) why wanted < got and return 1 ? ; (2) wanted might be negative value. */
  794. unsigned long wanted = pagedir1.size + extra_pd1_pages_allowance -
  795. get_lowmem_size(pagedir2);
  796. if (wanted > extra_pages_allocated) {
  797. unsigned long got = toi_allocate_extra_pagedir_memory(wanted);
  798. if (wanted < got) {
  799. toi_message(TOI_EAT_MEMORY, TOI_LOW, 1,
  800. "Want %d extra pages for pageset1, got %d.\n", wanted, got);
  801. return 1;
  802. }
  803. }
  804. #endif
  805. return 0;
  806. }
  807. /* update_image
  808. *
  809. * Allocate [more] memory and storage for the image.
  810. */
  811. static void update_image(int ps2_recalc)
  812. {
  813. int old_header_req;
  814. unsigned long seek;
  815. if (try_allocate_extra_memory())
  816. return;
  817. if (ps2_recalc)
  818. goto recalc;
  819. thaw_kernel_threads();
  820. /*
  821. * Allocate remaining storage space, if possible, up to the
  822. * maximum we know we'll need. It's okay to allocate the
  823. * maximum if the writer is the swapwriter, but
  824. * we don't want to grab all available space on an NFS share.
  825. * We therefore ignore the expected compression ratio here,
  826. * thereby trying to allocate the maximum image size we could
  827. * need (assuming compression doesn't expand the image), but
  828. * don't complain if we can't get the full amount we're after.
  829. */
  830. do {
  831. int result;
  832. old_header_req = header_storage_needed;
  833. toiActiveAllocator->reserve_header_space(header_storage_needed);
  834. /* How much storage is free with the reservation applied? */
  835. storage_limit = toiActiveAllocator->storage_available();
  836. seek = min(storage_limit, main_storage_needed(0, 0));
  837. result = toiActiveAllocator->allocate_storage(seek);
  838. if (result)
  839. pr_warn("Failed to allocate storage (%d).\n", result);
  840. main_storage_allocated = toiActiveAllocator->storage_allocated();
  841. /* Need more header because more storage allocated? */
  842. header_storage_needed = get_header_storage_needed();
  843. } while (header_storage_needed > old_header_req);
  844. if (freeze_kernel_threads()) {
  845. hib_log("after calling freeze_kernel_threads() with failed result\n");
  846. set_abort_result(TOI_FREEZING_FAILED);
  847. }
  848. recalc:
  849. toi_recalculate_image_contents(0);
  850. }
  851. /* attempt_to_freeze
  852. *
  853. * Try to freeze processes.
  854. */
  855. static int attempt_to_freeze(void)
  856. {
  857. int result;
  858. /* Stop processes before checking again */
  859. toi_prepare_status(CLEAR_BAR, "Freezing processes & syncing " "filesystems.");
  860. #ifdef CONFIG_TOI_FIXUP
  861. hib_warn("Syncing filesystems ... ");
  862. sys_sync();
  863. hib_warn("done.\n");
  864. #endif
  865. result = freeze_processes();
  866. if (result)
  867. set_abort_result(TOI_FREEZING_FAILED);
  868. result = freeze_kernel_threads();
  869. #ifdef CONFIG_MTK_HIBERNATION
  870. if (!result)
  871. shrink_all_memory(0); /* purpose for early trigger PASR to release userspace memory pages. */
  872. #endif
  873. if (result)
  874. set_abort_result(TOI_FREEZING_FAILED);
  875. return result;
  876. }
  877. /* eat_memory
  878. *
  879. * Try to free some memory, either to meet hard or soft constraints on the image
  880. * characteristics.
  881. *
  882. * Hard constraints:
  883. * - Pageset1 must be < half of memory;
  884. * - We must have enough memory free at resume time to have pageset1
  885. * be able to be loaded in pages that don't conflict with where it has to
  886. * be restored.
  887. * Soft constraints
  888. * - User specificied image size limit.
  889. */
  890. static void eat_memory(void)
  891. {
  892. unsigned long amount_wanted = 0;
  893. int did_eat_memory = 0;
  894. /*
  895. * Note that if we have enough storage space and enough free memory, we
  896. * may exit without eating anything. We give up when the last 10
  897. * iterations ate no extra pages because we're not going to get much
  898. * more anyway, but the few pages we get will take a lot of time.
  899. *
  900. * We freeze processes before beginning, and then unfreeze them if we
  901. * need to eat memory until we think we have enough. If our attempts
  902. * to freeze fail, we give up and abort.
  903. */
  904. amount_wanted = amount_needed(1);
  905. switch (image_size_limit) {
  906. case -1: /* Don't eat any memory */
  907. if (amount_wanted > 0) {
  908. set_abort_result(TOI_WOULD_EAT_MEMORY);
  909. return;
  910. }
  911. break;
  912. case -2: /* Free caches only */
  913. drop_pagecache();
  914. toi_recalculate_image_contents(0);
  915. amount_wanted = amount_needed(1);
  916. break;
  917. default:
  918. break;
  919. }
  920. if (amount_wanted > 0 && !test_result_state(TOI_ABORTED) && image_size_limit != -1) {
  921. unsigned long request = amount_wanted;
  922. unsigned long high_req = max(highpages_ps1_to_free(),
  923. any_to_free(1));
  924. unsigned long low_req = lowpages_ps1_to_free();
  925. unsigned long got = 0;
  926. toi_prepare_status(CLEAR_BAR,
  927. "Seeking to free %ldMB of memory.", MB(amount_wanted));
  928. thaw_kernel_threads();
  929. /*
  930. * Ask for too many because shrink_memory_mask doesn't
  931. * currently return enough most of the time.
  932. */
  933. if (low_req)
  934. got = shrink_memory_mask(low_req, GFP_KERNEL);
  935. if (high_req)
  936. shrink_memory_mask(high_req - got, GFP_HIGHUSER);
  937. did_eat_memory = 1;
  938. toi_recalculate_image_contents(0);
  939. amount_wanted = amount_needed(1);
  940. pr_debug("Asked shrink_memory_mask for %ld low pages & %ld pages from anywhere, got %ld.\n",
  941. high_req, low_req, request - amount_wanted);
  942. toi_cond_pause(0, NULL);
  943. if (freeze_kernel_threads())
  944. set_abort_result(TOI_FREEZING_FAILED);
  945. }
  946. if (did_eat_memory)
  947. toi_recalculate_image_contents(0);
  948. }
  949. /* toi_prepare_image
  950. *
  951. * Entry point to the whole image preparation section.
  952. *
  953. * We do four things:
  954. * - Freeze processes;
  955. * - Ensure image size constraints are met;
  956. * - Complete all the preparation for saving the image,
  957. * including allocation of storage. The only memory
  958. * that should be needed when we're finished is that
  959. * for actually storing the image (and we know how
  960. * much is needed for that because the modules tell
  961. * us).
  962. * - Make sure that all dirty buffers are written out.
  963. */
  964. #define MAX_TRIES 2
  965. int toi_prepare_image(void)
  966. {
  967. int result = 1, tries = 1;
  968. main_storage_allocated = 0;
  969. no_ps2_needed = 0;
  970. if (attempt_to_freeze())
  971. return 1;
  972. hib_log("@line:%d return value (%d)\n", __LINE__, result);
  973. lock_device_hotplug();
  974. set_toi_state(TOI_DEVICE_HOTPLUG_LOCKED);
  975. if (!extra_pd1_pages_allowance)
  976. get_extra_pd1_allowance();
  977. storage_limit = toiActiveAllocator->storage_available();
  978. if (!storage_limit) {
  979. pr_warn("No storage available. Didn't try to prepare " "an image.\n");
  980. display_failure_reason(0);
  981. set_abort_result(TOI_NOSTORAGE_AVAILABLE);
  982. return 1;
  983. }
  984. if (build_attention_list()) {
  985. abort_hibernate(TOI_UNABLE_TO_PREPARE_IMAGE,
  986. "Unable to successfully prepare the image.\n");
  987. return 1;
  988. }
  989. toi_recalculate_image_contents(0);
  990. do {
  991. toi_prepare_status(CLEAR_BAR, "Preparing Image. Try %d.", tries);
  992. eat_memory();
  993. if (test_result_state(TOI_ABORTED))
  994. break;
  995. update_image(0);
  996. tries++;
  997. } while (image_not_ready(1) && tries <= MAX_TRIES && !test_result_state(TOI_ABORTED));
  998. hib_log("@line%d return value (%d)\n", __LINE__, result);
  999. result = image_not_ready(0);
  1000. hib_log("@line:%d return value (%d)\n", __LINE__, result);
  1001. if (!test_result_state(TOI_ABORTED)) {
  1002. if (result) {
  1003. display_stats(1, 0);
  1004. display_failure_reason(tries > MAX_TRIES);
  1005. abort_hibernate(TOI_UNABLE_TO_PREPARE_IMAGE,
  1006. "Unable to successfully prepare the image.\n");
  1007. } else {
  1008. /* Pageset 2 needed? */
  1009. if (!need_pageset2() && test_action_state(TOI_NO_PS2_IF_UNNEEDED)) {
  1010. no_ps2_needed = 1;
  1011. toi_recalculate_image_contents(0);
  1012. update_image(1);
  1013. }
  1014. toi_cond_pause(1, "Image preparation complete.");
  1015. hib_log("Image preparation complete.\n");
  1016. }
  1017. }
  1018. hib_log("@line:%d return value (%d)\n", __LINE__, result);
  1019. return result ? result : allocate_checksum_pages();
  1020. }