tuxonice_modules.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. /*
  2. * kernel/power/tuxonice_modules.c
  3. *
  4. * Copyright (C) 2004-2014 Nigel Cunningham (nigel at tuxonice net)
  5. *
  6. */
  7. #include <linux/suspend.h>
  8. #include "tuxonice.h"
  9. #include "tuxonice_modules.h"
  10. #include "tuxonice_sysfs.h"
  11. #include "tuxonice_ui.h"
  12. LIST_HEAD(toi_filters);
  13. LIST_HEAD(toiAllocators);
  14. LIST_HEAD(toi_modules);
  15. EXPORT_SYMBOL_GPL(toi_modules);
  16. struct toi_module_ops *toiActiveAllocator;
  17. EXPORT_SYMBOL_GPL(toiActiveAllocator);
  18. static int toi_num_filters;
  19. int toiNumAllocators, toi_num_modules;
  20. /*
  21. * toi_header_storage_for_modules
  22. *
  23. * Returns the amount of space needed to store configuration
  24. * data needed by the modules prior to copying back the original
  25. * kernel. We can exclude data for pageset2 because it will be
  26. * available anyway once the kernel is copied back.
  27. */
  28. long toi_header_storage_for_modules(void)
  29. {
  30. struct toi_module_ops *this_module;
  31. int bytes = 0;
  32. list_for_each_entry(this_module, &toi_modules, module_list) {
  33. if (!this_module->enabled ||
  34. (this_module->type == WRITER_MODULE && toiActiveAllocator != this_module))
  35. continue;
  36. if (this_module->storage_needed) {
  37. int this = this_module->storage_needed() +
  38. sizeof(struct toi_module_header) + sizeof(int);
  39. this_module->header_requested = this;
  40. bytes += this;
  41. }
  42. }
  43. /* One more for the empty terminator */
  44. return bytes + sizeof(struct toi_module_header);
  45. }
  46. void print_toi_header_storage_for_modules(void)
  47. {
  48. struct toi_module_ops *this_module;
  49. int bytes = 0;
  50. pr_debug("Header storage:\n");
  51. list_for_each_entry(this_module, &toi_modules, module_list) {
  52. if (!this_module->enabled ||
  53. (this_module->type == WRITER_MODULE && toiActiveAllocator != this_module))
  54. continue;
  55. if (this_module->storage_needed) {
  56. int this = this_module->storage_needed() +
  57. sizeof(struct toi_module_header) + sizeof(int);
  58. this_module->header_requested = this;
  59. bytes += this;
  60. pr_debug("+ %16s : %-4d/%d.\n",
  61. this_module->name, this_module->header_used, this);
  62. }
  63. }
  64. pr_debug("+ empty terminator : %zu.\n", sizeof(struct toi_module_header));
  65. pr_debug(" ====\n");
  66. pr_debug(" %zu\n", bytes + sizeof(struct toi_module_header));
  67. }
  68. EXPORT_SYMBOL_GPL(print_toi_header_storage_for_modules);
  69. /*
  70. * toi_memory_for_modules
  71. *
  72. * Returns the amount of memory requested by modules for
  73. * doing their work during the cycle.
  74. */
  75. long toi_memory_for_modules(int print_parts)
  76. {
  77. long bytes = 0, result;
  78. struct toi_module_ops *this_module;
  79. if (print_parts)
  80. pr_warn("Memory for modules:\n===================\n");
  81. list_for_each_entry(this_module, &toi_modules, module_list) {
  82. int this;
  83. if (!this_module->enabled)
  84. continue;
  85. if (this_module->memory_needed) {
  86. this = this_module->memory_needed();
  87. if (print_parts)
  88. pr_warn("%10d bytes (%5ld pages) for module '%s'.\n", this,
  89. DIV_ROUND_UP(this, PAGE_SIZE), this_module->name);
  90. bytes += this;
  91. }
  92. }
  93. result = DIV_ROUND_UP(bytes, PAGE_SIZE);
  94. if (print_parts)
  95. pr_warn(" => %ld bytes, %ld pages.\n", bytes, result);
  96. return result;
  97. }
  98. /*
  99. * toi_expected_compression_ratio
  100. *
  101. * Returns the compression ratio expected when saving the image.
  102. */
  103. int toi_expected_compression_ratio(void)
  104. {
  105. int ratio = 100;
  106. struct toi_module_ops *this_module;
  107. list_for_each_entry(this_module, &toi_modules, module_list) {
  108. if (!this_module->enabled)
  109. continue;
  110. if (this_module->expected_compression)
  111. ratio = ratio * this_module->expected_compression()
  112. / 100;
  113. }
  114. return ratio;
  115. }
  116. #ifdef CONFIG_TOI_ENHANCE
  117. /*
  118. * toi_actual_compression_ratio
  119. *
  120. * Returns the actual compression ratio when saving the image.
  121. */
  122. int toi_actual_compression_ratio(void)
  123. {
  124. int ratio = 0;
  125. struct toi_module_ops *this_module;
  126. list_for_each_entry(this_module, &toi_modules, module_list) {
  127. if (!this_module->enabled)
  128. continue;
  129. if (this_module->actual_compression)
  130. ratio = this_module->actual_compression();
  131. }
  132. return ratio;
  133. }
  134. #endif /* CONFIG_TOI_ENHANCE */
  135. /* toi_find_module_given_dir
  136. * Functionality : Return a module (if found), given a pointer
  137. * to its directory name
  138. */
  139. static struct toi_module_ops *toi_find_module_given_dir(char *name)
  140. {
  141. struct toi_module_ops *this_module, *found_module = NULL;
  142. list_for_each_entry(this_module, &toi_modules, module_list) {
  143. if (!strcmp(name, this_module->directory)) {
  144. found_module = this_module;
  145. break;
  146. }
  147. }
  148. return found_module;
  149. }
  150. /* toi_find_module_given_name
  151. * Functionality : Return a module (if found), given a pointer
  152. * to its name
  153. */
  154. struct toi_module_ops *toi_find_module_given_name(char *name)
  155. {
  156. struct toi_module_ops *this_module, *found_module = NULL;
  157. list_for_each_entry(this_module, &toi_modules, module_list) {
  158. if (!strcmp(name, this_module->name)) {
  159. found_module = this_module;
  160. break;
  161. }
  162. }
  163. return found_module;
  164. }
  165. /*
  166. * toi_print_module_debug_info
  167. * Functionality : Get debugging info from modules into a buffer.
  168. */
  169. int toi_print_module_debug_info(char *buffer, int buffer_size)
  170. {
  171. struct toi_module_ops *this_module;
  172. int len = 0;
  173. list_for_each_entry(this_module, &toi_modules, module_list) {
  174. if (!this_module->enabled)
  175. continue;
  176. if (this_module->print_debug_info) {
  177. int result;
  178. result = this_module->print_debug_info(buffer + len, buffer_size - len);
  179. len += result;
  180. }
  181. }
  182. /* Ensure null terminated */
  183. buffer[buffer_size] = 0;
  184. return len;
  185. }
  186. /*
  187. * toi_register_module
  188. *
  189. * Register a module.
  190. */
  191. int toi_register_module(struct toi_module_ops *module)
  192. {
  193. int i;
  194. struct kobject *kobj;
  195. if (!hibernation_available())
  196. return -ENODEV;
  197. module->enabled = 1;
  198. if (toi_find_module_given_name(module->name)) {
  199. pr_warn("TuxOnIce: Trying to load module %s, which is already registered.\n", module->name);
  200. return -EBUSY;
  201. }
  202. switch (module->type) {
  203. case FILTER_MODULE:
  204. list_add_tail(&module->type_list, &toi_filters);
  205. toi_num_filters++;
  206. break;
  207. case WRITER_MODULE:
  208. list_add_tail(&module->type_list, &toiAllocators);
  209. toiNumAllocators++;
  210. break;
  211. case MISC_MODULE:
  212. case MISC_HIDDEN_MODULE:
  213. case BIO_ALLOCATOR_MODULE:
  214. break;
  215. default:
  216. pr_err("Hmmm. Module '%s' has an invalid type. It has been ignored.\n", module->name);
  217. return -EINVAL;
  218. }
  219. list_add_tail(&module->module_list, &toi_modules);
  220. toi_num_modules++;
  221. if ((!module->directory && !module->shared_directory) ||
  222. !module->sysfs_data || !module->num_sysfs_entries)
  223. return 0;
  224. /*
  225. * Modules may share a directory, but those with shared_dir
  226. * set must be loaded (via symbol dependencies) after parents
  227. * and unloaded beforehand.
  228. */
  229. if (module->shared_directory) {
  230. struct toi_module_ops *shared = toi_find_module_given_dir(module->shared_directory);
  231. if (!shared) {
  232. pr_err("TuxOnIce: Module %s wants to share %s's directory but %s isn't loaded",
  233. module->name, module->shared_directory, module->shared_directory);
  234. toi_unregister_module(module);
  235. return -ENODEV;
  236. }
  237. kobj = shared->dir_kobj;
  238. } else {
  239. if (!strncmp(module->directory, "[ROOT]", 6))
  240. kobj = tuxonice_kobj;
  241. else
  242. kobj = make_toi_sysdir(module->directory);
  243. }
  244. module->dir_kobj = kobj;
  245. for (i = 0; i < module->num_sysfs_entries; i++) {
  246. int result = toi_register_sysfs_file(kobj,
  247. &module->sysfs_data[i]);
  248. if (result)
  249. return result;
  250. }
  251. return 0;
  252. }
  253. EXPORT_SYMBOL_GPL(toi_register_module);
  254. /*
  255. * toi_unregister_module
  256. *
  257. * Remove a module.
  258. */
  259. void toi_unregister_module(struct toi_module_ops *module)
  260. {
  261. int i;
  262. if (module->dir_kobj)
  263. for (i = 0; i < module->num_sysfs_entries; i++)
  264. toi_unregister_sysfs_file(module->dir_kobj, &module->sysfs_data[i]);
  265. if (!module->shared_directory && module->directory &&
  266. strncmp(module->directory, "[ROOT]", 6))
  267. remove_toi_sysdir(module->dir_kobj);
  268. switch (module->type) {
  269. case FILTER_MODULE:
  270. list_del(&module->type_list);
  271. toi_num_filters--;
  272. break;
  273. case WRITER_MODULE:
  274. list_del(&module->type_list);
  275. toiNumAllocators--;
  276. if (toiActiveAllocator == module) {
  277. toiActiveAllocator = NULL;
  278. clear_toi_state(TOI_CAN_RESUME);
  279. clear_toi_state(TOI_CAN_HIBERNATE);
  280. }
  281. break;
  282. case MISC_MODULE:
  283. case MISC_HIDDEN_MODULE:
  284. case BIO_ALLOCATOR_MODULE:
  285. break;
  286. default:
  287. pr_err("Module '%s' has an invalid type. It has been ignored", module->name);
  288. return;
  289. }
  290. list_del(&module->module_list);
  291. toi_num_modules--;
  292. }
  293. EXPORT_SYMBOL_GPL(toi_unregister_module);
  294. /*
  295. * toi_move_module_tail
  296. *
  297. * Rearrange modules when reloading the config.
  298. */
  299. void toi_move_module_tail(struct toi_module_ops *module)
  300. {
  301. switch (module->type) {
  302. case FILTER_MODULE:
  303. if (toi_num_filters > 1)
  304. list_move_tail(&module->type_list, &toi_filters);
  305. break;
  306. case WRITER_MODULE:
  307. if (toiNumAllocators > 1)
  308. list_move_tail(&module->type_list, &toiAllocators);
  309. break;
  310. case MISC_MODULE:
  311. case MISC_HIDDEN_MODULE:
  312. case BIO_ALLOCATOR_MODULE:
  313. break;
  314. default:
  315. pr_err("Module '%s' has an invalid type. It has been ignored.\n", module->name);
  316. return;
  317. }
  318. if ((toi_num_filters + toiNumAllocators) > 1)
  319. list_move_tail(&module->module_list, &toi_modules);
  320. }
  321. /*
  322. * toi_initialise_modules
  323. *
  324. * Get ready to do some work!
  325. */
  326. int toi_initialise_modules(int starting_cycle, int early)
  327. {
  328. struct toi_module_ops *this_module;
  329. int result;
  330. list_for_each_entry(this_module, &toi_modules, module_list) {
  331. this_module->header_requested = 0;
  332. this_module->header_used = 0;
  333. if (!this_module->enabled)
  334. continue;
  335. if (this_module->early != early)
  336. continue;
  337. if (this_module->initialise) {
  338. result = this_module->initialise(starting_cycle);
  339. if (result) {
  340. toi_cleanup_modules(starting_cycle);
  341. return result;
  342. }
  343. this_module->initialised = 1;
  344. }
  345. }
  346. return 0;
  347. }
  348. /*
  349. * toi_cleanup_modules
  350. *
  351. * Tell modules the work is done.
  352. */
  353. void toi_cleanup_modules(int finishing_cycle)
  354. {
  355. struct toi_module_ops *this_module;
  356. list_for_each_entry(this_module, &toi_modules, module_list) {
  357. if (!this_module->enabled || !this_module->initialised)
  358. continue;
  359. if (this_module->cleanup)
  360. this_module->cleanup(finishing_cycle);
  361. this_module->initialised = 0;
  362. }
  363. }
  364. /*
  365. * toi_pre_atomic_restore_modules
  366. *
  367. * Get ready to do some work!
  368. */
  369. void toi_pre_atomic_restore_modules(struct toi_boot_kernel_data *bkd)
  370. {
  371. struct toi_module_ops *this_module;
  372. list_for_each_entry(this_module, &toi_modules, module_list) {
  373. if (this_module->enabled && this_module->pre_atomic_restore)
  374. this_module->pre_atomic_restore(bkd);
  375. }
  376. }
  377. /*
  378. * toi_post_atomic_restore_modules
  379. *
  380. * Get ready to do some work!
  381. */
  382. void toi_post_atomic_restore_modules(struct toi_boot_kernel_data *bkd)
  383. {
  384. struct toi_module_ops *this_module;
  385. list_for_each_entry(this_module, &toi_modules, module_list) {
  386. if (this_module->enabled && this_module->post_atomic_restore)
  387. this_module->post_atomic_restore(bkd);
  388. }
  389. }
  390. /*
  391. * toi_get_next_filter
  392. *
  393. * Get the next filter in the pipeline.
  394. */
  395. struct toi_module_ops *toi_get_next_filter(struct toi_module_ops *filter_sought)
  396. {
  397. struct toi_module_ops *last_filter = NULL, *this_filter = NULL;
  398. list_for_each_entry(this_filter, &toi_filters, type_list) {
  399. if (!this_filter->enabled)
  400. continue;
  401. if ((last_filter == filter_sought) || (!filter_sought))
  402. return this_filter;
  403. last_filter = this_filter;
  404. }
  405. return toiActiveAllocator;
  406. }
  407. EXPORT_SYMBOL_GPL(toi_get_next_filter);
  408. /**
  409. * toi_show_modules: Printk what support is loaded.
  410. */
  411. void toi_print_modules(void)
  412. {
  413. struct toi_module_ops *this_module;
  414. int prev = 0;
  415. pr_warn("TuxOnIce " TOI_CORE_VERSION ", with support for");
  416. list_for_each_entry(this_module, &toi_modules, module_list) {
  417. if (this_module->type == MISC_HIDDEN_MODULE)
  418. continue;
  419. pr_warn("%s %s%s%s", prev ? "," : "",
  420. this_module->enabled ? "" : "[",
  421. this_module->name, this_module->enabled ? "" : "]");
  422. prev = 1;
  423. }
  424. pr_warn(".\n");
  425. }
  426. /* toi_get_modules
  427. *
  428. * Take a reference to modules so they can't go away under us.
  429. */
  430. int toi_get_modules(void)
  431. {
  432. struct toi_module_ops *this_module;
  433. list_for_each_entry(this_module, &toi_modules, module_list) {
  434. struct toi_module_ops *this_module2;
  435. if (try_module_get(this_module->module))
  436. continue;
  437. /* Failed! Reverse gets and return error */
  438. list_for_each_entry(this_module2, &toi_modules, module_list) {
  439. if (this_module == this_module2)
  440. return -EINVAL;
  441. module_put(this_module2->module);
  442. }
  443. }
  444. return 0;
  445. }
  446. /* toi_put_modules
  447. *
  448. * Release our references to modules we used.
  449. */
  450. void toi_put_modules(void)
  451. {
  452. struct toi_module_ops *this_module;
  453. list_for_each_entry(this_module, &toi_modules, module_list)
  454. module_put(this_module->module);
  455. }