tuxonice_compress.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. /*
  2. * kernel/power/compression.c
  3. *
  4. * Copyright (C) 2003-2014 Nigel Cunningham (nigel at tuxonice net)
  5. *
  6. * This file is released under the GPLv2.
  7. *
  8. * This file contains data compression routines for TuxOnIce,
  9. * using cryptoapi.
  10. */
  11. #include <linux/suspend.h>
  12. #include <linux/highmem.h>
  13. #include <linux/vmalloc.h>
  14. #include <linux/crypto.h>
  15. #include "tuxonice_builtin.h"
  16. #include "tuxonice.h"
  17. #include "tuxonice_modules.h"
  18. #include "tuxonice_sysfs.h"
  19. #include "tuxonice_io.h"
  20. #include "tuxonice_ui.h"
  21. #include "tuxonice_alloc.h"
  22. static int toi_expected_compression;
  23. #ifdef CONFIG_TOI_ENHANCE
  24. static int toi_actual_compression;
  25. #endif
  26. static struct toi_module_ops toi_compression_ops;
  27. static struct toi_module_ops *next_driver;
  28. #if defined(CONFIG_MTK_MTD_NAND) && defined(CONFIG_CRYPTO_LZ4K)
  29. static char toi_compressor_name[32] = "lz4k";
  30. #else
  31. static char toi_compressor_name[32] = "lzo";
  32. #endif
  33. static DEFINE_MUTEX(stats_lock);
  34. struct toi_cpu_context {
  35. u8 *page_buffer;
  36. struct crypto_comp *transform;
  37. unsigned int len;
  38. u8 *buffer_start;
  39. u8 *output_buffer;
  40. };
  41. #define OUT_BUF_SIZE (2 * PAGE_SIZE)
  42. static DEFINE_PER_CPU(struct toi_cpu_context, contexts);
  43. /*
  44. * toi_crypto_prepare
  45. *
  46. * Prepare to do some work by allocating buffers and transforms.
  47. */
  48. static int toi_compress_crypto_prepare(void)
  49. {
  50. int cpu;
  51. if (!*toi_compressor_name) {
  52. pr_warn("TuxOnIce: Compression enabled but no " "compressor name set.\n");
  53. return 1;
  54. }
  55. for_each_online_cpu(cpu) {
  56. struct toi_cpu_context *this = &per_cpu(contexts, cpu);
  57. this->transform = crypto_alloc_comp(toi_compressor_name, 0, 0);
  58. if (IS_ERR(this->transform)) {
  59. pr_warn("TuxOnIce: Failed to initialise the %s compression transform.", toi_compressor_name);
  60. this->transform = NULL;
  61. return 1;
  62. }
  63. this->page_buffer = (char *)toi_get_zeroed_page(16, TOI_ATOMIC_GFP);
  64. if (!this->page_buffer) {
  65. pr_err("Failed to allocate a page buffer for TuxOnIce compression driver.");
  66. return -ENOMEM;
  67. }
  68. this->output_buffer = (char *)vmalloc_32(OUT_BUF_SIZE);
  69. if (!this->output_buffer) {
  70. pr_err("Failed to allocate a output buffer for TuxOnIce compression driver.");
  71. return -ENOMEM;
  72. }
  73. }
  74. return 0;
  75. }
  76. static int toi_compress_rw_cleanup(int writing)
  77. {
  78. int cpu;
  79. for_each_online_cpu(cpu) {
  80. struct toi_cpu_context *this = &per_cpu(contexts, cpu);
  81. if (this->transform) {
  82. crypto_free_comp(this->transform);
  83. this->transform = NULL;
  84. }
  85. if (this->page_buffer)
  86. toi_free_page(16, (unsigned long)this->page_buffer);
  87. this->page_buffer = NULL;
  88. if (this->output_buffer)
  89. vfree(this->output_buffer);
  90. this->output_buffer = NULL;
  91. }
  92. return 0;
  93. }
  94. /*
  95. * toi_compress_init
  96. */
  97. static int toi_compress_init(int toi_or_resume)
  98. {
  99. if (!toi_or_resume)
  100. return 0;
  101. toi_compress_bytes_in = 0;
  102. toi_compress_bytes_out = 0;
  103. next_driver = toi_get_next_filter(&toi_compression_ops);
  104. return next_driver ? 0 : -ECHILD;
  105. }
  106. /*
  107. * toi_compress_rw_init()
  108. */
  109. static int toi_compress_rw_init(int rw, int stream_number)
  110. {
  111. int ret = 0;
  112. if (toi_compress_crypto_prepare()) {
  113. pr_err("Failed to initialise compression " "algorithm.\n");
  114. if (rw == READ) {
  115. pr_warn("Unable to read the image.\n");
  116. ret = -ENODEV;
  117. } else {
  118. pr_warn("Continuing without " "compressing the image.\n");
  119. toi_compression_ops.enabled = 0;
  120. }
  121. }
  122. return ret;
  123. }
  124. /*
  125. * toi_compress_write_page()
  126. *
  127. * Compress a page of data, buffering output and passing on filled
  128. * pages to the next module in the pipeline.
  129. *
  130. * Buffer_page: Pointer to a buffer of size PAGE_SIZE, containing
  131. * data to be compressed.
  132. *
  133. * Returns: 0 on success. Otherwise the error is that returned by later
  134. * modules, -ECHILD if we have a broken pipeline or -EIO if
  135. * zlib errs.
  136. */
  137. static int toi_compress_write_page(unsigned long index, int buf_type,
  138. void *buffer_page, unsigned int buf_size)
  139. {
  140. int ret = 0, cpu = smp_processor_id();
  141. struct toi_cpu_context *ctx = &per_cpu(contexts, cpu);
  142. u8 *output_buffer = buffer_page;
  143. int output_len = buf_size;
  144. int out_buf_type = buf_type;
  145. if (ctx->transform) {
  146. ctx->buffer_start = TOI_MAP(buf_type, buffer_page);
  147. ctx->len = OUT_BUF_SIZE;
  148. ret = crypto_comp_compress(ctx->transform,
  149. ctx->buffer_start, buf_size,
  150. ctx->output_buffer, &ctx->len);
  151. TOI_UNMAP(buf_type, buffer_page);
  152. toi_message(TOI_COMPRESS, TOI_VERBOSE, 0,
  153. "CPU %d, index %lu: %d bytes", cpu, index, ctx->len);
  154. if (!ret && ctx->len < buf_size) { /* some compression */
  155. output_buffer = ctx->output_buffer;
  156. output_len = ctx->len;
  157. out_buf_type = TOI_VIRT;
  158. }
  159. }
  160. mutex_lock(&stats_lock);
  161. toi_compress_bytes_in += buf_size;
  162. toi_compress_bytes_out += output_len;
  163. mutex_unlock(&stats_lock);
  164. if (!ret)
  165. ret = next_driver->write_page(index, out_buf_type, output_buffer, output_len);
  166. return ret;
  167. }
  168. /*
  169. * toi_compress_read_page()
  170. * @buffer_page: struct page *. Pointer to a buffer of size PAGE_SIZE.
  171. *
  172. * Retrieve data from later modules and decompress it until the input buffer
  173. * is filled.
  174. * Zero if successful. Error condition from me or from downstream on failure.
  175. */
  176. static int toi_compress_read_page(unsigned long *index, int buf_type,
  177. void *buffer_page, unsigned int *buf_size)
  178. {
  179. int ret, cpu = smp_processor_id();
  180. unsigned int len;
  181. unsigned int outlen = PAGE_SIZE;
  182. char *buffer_start;
  183. struct toi_cpu_context *ctx = &per_cpu(contexts, cpu);
  184. if (!ctx->transform)
  185. return next_driver->read_page(index, TOI_PAGE, buffer_page, buf_size);
  186. /*
  187. * All our reads must be synchronous - we can't decompress
  188. * data that hasn't been read yet.
  189. */
  190. ret = next_driver->read_page(index, TOI_VIRT, ctx->page_buffer, &len);
  191. buffer_start = kmap(buffer_page);
  192. /* Error or uncompressed data */
  193. if (ret || len == PAGE_SIZE) {
  194. memcpy(buffer_start, ctx->page_buffer, len);
  195. goto out;
  196. }
  197. ret = crypto_comp_decompress(ctx->transform, ctx->page_buffer, len, buffer_start, &outlen);
  198. toi_message(TOI_COMPRESS, TOI_VERBOSE, 0,
  199. "CPU %d, index %lu: %d=>%d (%d).", cpu, *index, len, outlen, ret);
  200. if (ret)
  201. abort_hibernate(TOI_FAILED_IO, "Compress_read returned %d.\n", ret);
  202. else if (outlen != PAGE_SIZE) {
  203. abort_hibernate(TOI_FAILED_IO,
  204. "Decompression yielded %d bytes instead of %ld.\n",
  205. outlen, PAGE_SIZE);
  206. pr_err("Decompression yielded %d bytes instead of %ld.\n", outlen, PAGE_SIZE);
  207. ret = -EIO;
  208. *buf_size = outlen;
  209. }
  210. out:
  211. TOI_UNMAP(buf_type, buffer_page);
  212. return ret;
  213. }
  214. /*
  215. * toi_compress_print_debug_stats
  216. * @buffer: Pointer to a buffer into which the debug info will be printed.
  217. * @size: Size of the buffer.
  218. *
  219. * Print information to be recorded for debugging purposes into a buffer.
  220. * Returns: Number of characters written to the buffer.
  221. */
  222. static int toi_compress_print_debug_stats(char *buffer, int size)
  223. {
  224. unsigned long pages_in = toi_compress_bytes_in >> PAGE_SHIFT,
  225. pages_out = toi_compress_bytes_out >> PAGE_SHIFT;
  226. int len;
  227. /* Output the compression ratio achieved. */
  228. if (*toi_compressor_name)
  229. len = scnprintf(buffer, size, "- Compressor is '%s'.\n", toi_compressor_name);
  230. else
  231. len = scnprintf(buffer, size, "- Compressor is not set.\n");
  232. if (pages_in)
  233. len += scnprintf(buffer + len, size - len,
  234. " Compressed %lu bytes into %lu (%ld percent compression).\n",
  235. toi_compress_bytes_in,
  236. toi_compress_bytes_out, (pages_in - pages_out) * 100 / pages_in);
  237. return len;
  238. }
  239. /*
  240. * toi_compress_compression_memory_needed
  241. *
  242. * Tell the caller how much memory we need to operate during hibernate/resume.
  243. * Returns: Unsigned long. Maximum number of bytes of memory required for
  244. * operation.
  245. */
  246. static int toi_compress_memory_needed(void)
  247. {
  248. return 2 * PAGE_SIZE;
  249. }
  250. static int toi_compress_storage_needed(void)
  251. {
  252. return 2 * sizeof(unsigned long) + 2 * sizeof(int) + strlen(toi_compressor_name) + 1;
  253. }
  254. /*
  255. * toi_compress_save_config_info
  256. * @buffer: Pointer to a buffer of size PAGE_SIZE.
  257. *
  258. * Save informaton needed when reloading the image at resume time.
  259. * Returns: Number of bytes used for saving our data.
  260. */
  261. static int toi_compress_save_config_info(char *buffer)
  262. {
  263. int len = strlen(toi_compressor_name) + 1, offset = 0;
  264. *((unsigned long *)buffer) = toi_compress_bytes_in;
  265. offset += sizeof(unsigned long);
  266. *((unsigned long *)(buffer + offset)) = toi_compress_bytes_out;
  267. offset += sizeof(unsigned long);
  268. *((int *)(buffer + offset)) = toi_expected_compression;
  269. offset += sizeof(int);
  270. *((int *)(buffer + offset)) = len;
  271. offset += sizeof(int);
  272. strncpy(buffer + offset, toi_compressor_name, len);
  273. return offset + len;
  274. }
  275. /* toi_compress_load_config_info
  276. * @buffer: Pointer to the start of the data.
  277. * @size: Number of bytes that were saved.
  278. *
  279. * Description: Reload information needed for decompressing the image at
  280. * resume time.
  281. */
  282. static void toi_compress_load_config_info(char *buffer, int size)
  283. {
  284. int len, offset = 0;
  285. toi_compress_bytes_in = *((unsigned long *)buffer);
  286. offset += sizeof(unsigned long);
  287. toi_compress_bytes_out = *((unsigned long *)(buffer + offset));
  288. offset += sizeof(unsigned long);
  289. toi_expected_compression = *((int *)(buffer + offset));
  290. offset += sizeof(int);
  291. len = *((int *)(buffer + offset));
  292. offset += sizeof(int);
  293. strncpy(toi_compressor_name, buffer + offset, len);
  294. }
  295. static void toi_compress_pre_atomic_restore(struct toi_boot_kernel_data *bkd)
  296. {
  297. bkd->compress_bytes_in = toi_compress_bytes_in;
  298. bkd->compress_bytes_out = toi_compress_bytes_out;
  299. }
  300. static void toi_compress_post_atomic_restore(struct toi_boot_kernel_data *bkd)
  301. {
  302. toi_compress_bytes_in = bkd->compress_bytes_in;
  303. toi_compress_bytes_out = bkd->compress_bytes_out;
  304. }
  305. /*
  306. * toi_expected_compression_ratio
  307. *
  308. * Description: Returns the expected ratio between data passed into this module
  309. * and the amount of data output when writing.
  310. * Returns: 100 if the module is disabled. Otherwise the value set by the
  311. * user via our sysfs entry.
  312. */
  313. static int toi_compress_expected_ratio(void)
  314. {
  315. if (!toi_compression_ops.enabled)
  316. return 100;
  317. else
  318. return 100 - toi_expected_compression;
  319. }
  320. #ifdef CONFIG_TOI_ENHANCE
  321. /*
  322. * toi_actual_compression_ratio
  323. *
  324. * Description: Returns the actual ratio of the lastest compression result.
  325. * Returns: 0 if the module is disabled.
  326. */
  327. static int toi_compress_actual_ratio(void)
  328. {
  329. unsigned long pages_in = toi_compress_bytes_in >> PAGE_SHIFT,
  330. pages_out = toi_compress_bytes_out >> PAGE_SHIFT;
  331. toi_actual_compression = 0;
  332. if (!toi_compression_ops.enabled)
  333. toi_actual_compression = 0;
  334. else if (pages_in > 0 && (pages_in - pages_out >= 0))
  335. toi_actual_compression = (pages_in - pages_out) * 100 / pages_in;
  336. pr_warn("[%s] actual compressed ratio %d (%lu/%lu)\n", __func__,
  337. toi_actual_compression, pages_in, pages_out);
  338. return toi_actual_compression;
  339. }
  340. #endif
  341. /*
  342. * data for our sysfs entries.
  343. */
  344. static struct toi_sysfs_data sysfs_params[] = {
  345. SYSFS_INT("expected_compression", SYSFS_RW, &toi_expected_compression,
  346. 0, 99, 0, NULL),
  347. SYSFS_INT("enabled", SYSFS_RW, &toi_compression_ops.enabled, 0, 1, 0,
  348. NULL),
  349. SYSFS_STRING("algorithm", SYSFS_RW, toi_compressor_name, 31, 0, NULL),
  350. #ifdef CONFIG_TOI_ENHANCE
  351. SYSFS_INT("actual_compression", SYSFS_READONLY, &toi_actual_compression,
  352. 0, 99, 0, NULL),
  353. #endif
  354. };
  355. /*
  356. * Ops structure.
  357. */
  358. static struct toi_module_ops toi_compression_ops = {
  359. .type = FILTER_MODULE,
  360. .name = "compression",
  361. .directory = "compression",
  362. .module = THIS_MODULE,
  363. .initialise = toi_compress_init,
  364. .memory_needed = toi_compress_memory_needed,
  365. .print_debug_info = toi_compress_print_debug_stats,
  366. .save_config_info = toi_compress_save_config_info,
  367. .load_config_info = toi_compress_load_config_info,
  368. .storage_needed = toi_compress_storage_needed,
  369. .expected_compression = toi_compress_expected_ratio,
  370. #ifdef CONFIG_TOI_ENHANCE
  371. .actual_compression = toi_compress_actual_ratio,
  372. #endif
  373. .pre_atomic_restore = toi_compress_pre_atomic_restore,
  374. .post_atomic_restore = toi_compress_post_atomic_restore,
  375. .rw_init = toi_compress_rw_init,
  376. .rw_cleanup = toi_compress_rw_cleanup,
  377. .write_page = toi_compress_write_page,
  378. .read_page = toi_compress_read_page,
  379. .sysfs_data = sysfs_params,
  380. .num_sysfs_entries = sizeof(sysfs_params) / sizeof(struct toi_sysfs_data),
  381. };
  382. /* ---- Registration ---- */
  383. static __init int toi_compress_load(void)
  384. {
  385. return toi_register_module(&toi_compression_ops);
  386. }
  387. #ifdef MODULE
  388. static __exit void toi_compress_unload(void)
  389. {
  390. toi_unregister_module(&toi_compression_ops);
  391. }
  392. module_init(toi_compress_load);
  393. module_exit(toi_compress_unload);
  394. MODULE_LICENSE("GPL");
  395. MODULE_AUTHOR("Nigel Cunningham");
  396. MODULE_DESCRIPTION("Compression Support for TuxOnIce");
  397. #else
  398. late_initcall(toi_compress_load);
  399. #endif