decompressor_zstd.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. #include <linux/zstd.h>
  3. #include "compress.h"
  4. struct z_erofs_zstd {
  5. struct z_erofs_zstd *next;
  6. u8 bounce[PAGE_SIZE];
  7. void *wksp;
  8. unsigned int wkspsz;
  9. };
  10. static DEFINE_SPINLOCK(z_erofs_zstd_lock);
  11. static unsigned int z_erofs_zstd_max_dictsize;
  12. static unsigned int z_erofs_zstd_nstrms, z_erofs_zstd_avail_strms;
  13. static struct z_erofs_zstd *z_erofs_zstd_head;
  14. static DECLARE_WAIT_QUEUE_HEAD(z_erofs_zstd_wq);
  15. module_param_named(zstd_streams, z_erofs_zstd_nstrms, uint, 0444);
  16. static struct z_erofs_zstd *z_erofs_isolate_strms(bool all)
  17. {
  18. struct z_erofs_zstd *strm;
  19. again:
  20. spin_lock(&z_erofs_zstd_lock);
  21. strm = z_erofs_zstd_head;
  22. if (!strm) {
  23. spin_unlock(&z_erofs_zstd_lock);
  24. wait_event(z_erofs_zstd_wq, READ_ONCE(z_erofs_zstd_head));
  25. goto again;
  26. }
  27. z_erofs_zstd_head = all ? NULL : strm->next;
  28. spin_unlock(&z_erofs_zstd_lock);
  29. return strm;
  30. }
  31. static void z_erofs_zstd_exit(void)
  32. {
  33. while (z_erofs_zstd_avail_strms) {
  34. struct z_erofs_zstd *strm, *n;
  35. for (strm = z_erofs_isolate_strms(true); strm; strm = n) {
  36. n = strm->next;
  37. kvfree(strm->wksp);
  38. kfree(strm);
  39. --z_erofs_zstd_avail_strms;
  40. }
  41. }
  42. }
  43. static int __init z_erofs_zstd_init(void)
  44. {
  45. /* by default, use # of possible CPUs instead */
  46. if (!z_erofs_zstd_nstrms)
  47. z_erofs_zstd_nstrms = num_possible_cpus();
  48. for (; z_erofs_zstd_avail_strms < z_erofs_zstd_nstrms;
  49. ++z_erofs_zstd_avail_strms) {
  50. struct z_erofs_zstd *strm;
  51. strm = kzalloc_obj(*strm);
  52. if (!strm) {
  53. z_erofs_zstd_exit();
  54. return -ENOMEM;
  55. }
  56. spin_lock(&z_erofs_zstd_lock);
  57. strm->next = z_erofs_zstd_head;
  58. z_erofs_zstd_head = strm;
  59. spin_unlock(&z_erofs_zstd_lock);
  60. }
  61. return 0;
  62. }
  63. static int z_erofs_load_zstd_config(struct super_block *sb,
  64. struct erofs_super_block *dsb, void *data, int size)
  65. {
  66. static DEFINE_MUTEX(zstd_resize_mutex);
  67. struct z_erofs_zstd_cfgs *zstd = data;
  68. unsigned int dict_size, wkspsz;
  69. struct z_erofs_zstd *strm, *head = NULL;
  70. void *wksp;
  71. if (!zstd || size < sizeof(struct z_erofs_zstd_cfgs) || zstd->format) {
  72. erofs_err(sb, "unsupported zstd format, size=%u", size);
  73. return -EINVAL;
  74. }
  75. if (zstd->windowlog > ilog2(Z_EROFS_ZSTD_MAX_DICT_SIZE) - 10) {
  76. erofs_err(sb, "unsupported zstd window log %u", zstd->windowlog);
  77. return -EINVAL;
  78. }
  79. dict_size = 1U << (zstd->windowlog + 10);
  80. /* in case 2 z_erofs_load_zstd_config() race to avoid deadlock */
  81. mutex_lock(&zstd_resize_mutex);
  82. if (z_erofs_zstd_max_dictsize >= dict_size) {
  83. mutex_unlock(&zstd_resize_mutex);
  84. return 0;
  85. }
  86. /* 1. collect/isolate all streams for the following check */
  87. while (z_erofs_zstd_avail_strms) {
  88. struct z_erofs_zstd *n;
  89. for (strm = z_erofs_isolate_strms(true); strm; strm = n) {
  90. n = strm->next;
  91. strm->next = head;
  92. head = strm;
  93. --z_erofs_zstd_avail_strms;
  94. }
  95. }
  96. /* 2. walk each isolated stream and grow max dict_size if needed */
  97. wkspsz = zstd_dstream_workspace_bound(dict_size);
  98. for (strm = head; strm; strm = strm->next) {
  99. wksp = kvmalloc(wkspsz, GFP_KERNEL);
  100. if (!wksp)
  101. break;
  102. kvfree(strm->wksp);
  103. strm->wksp = wksp;
  104. strm->wkspsz = wkspsz;
  105. }
  106. /* 3. push back all to the global list and update max dict_size */
  107. spin_lock(&z_erofs_zstd_lock);
  108. DBG_BUGON(z_erofs_zstd_head);
  109. z_erofs_zstd_head = head;
  110. spin_unlock(&z_erofs_zstd_lock);
  111. z_erofs_zstd_avail_strms = z_erofs_zstd_nstrms;
  112. wake_up_all(&z_erofs_zstd_wq);
  113. if (!strm)
  114. z_erofs_zstd_max_dictsize = dict_size;
  115. mutex_unlock(&zstd_resize_mutex);
  116. return strm ? -ENOMEM : 0;
  117. }
  118. static const char *z_erofs_zstd_decompress(struct z_erofs_decompress_req *rq,
  119. struct page **pgpl)
  120. {
  121. struct super_block *sb = rq->sb;
  122. struct z_erofs_stream_dctx dctx = { .rq = rq, .no = -1, .ni = 0 };
  123. zstd_in_buffer in_buf = { NULL, 0, 0 };
  124. zstd_out_buffer out_buf = { NULL, 0, 0 };
  125. struct z_erofs_zstd *strm;
  126. zstd_dstream *stream;
  127. const char *reason;
  128. int zerr;
  129. /* 1. get the exact compressed size */
  130. dctx.kin = kmap_local_page(*rq->in);
  131. reason = z_erofs_fixup_insize(rq, dctx.kin + rq->pageofs_in,
  132. min(rq->inputsize, sb->s_blocksize - rq->pageofs_in));
  133. if (reason) {
  134. kunmap_local(dctx.kin);
  135. return reason;
  136. }
  137. /* 2. get an available ZSTD context */
  138. strm = z_erofs_isolate_strms(false);
  139. /* 3. multi-call decompress */
  140. stream = zstd_init_dstream(z_erofs_zstd_max_dictsize, strm->wksp, strm->wkspsz);
  141. if (!stream) {
  142. reason = ERR_PTR(-ENOMEM);
  143. goto failed_zinit;
  144. }
  145. rq->fillgaps = true; /* ZSTD doesn't support NULL output buffer */
  146. in_buf.size = min_t(u32, rq->inputsize, PAGE_SIZE - rq->pageofs_in);
  147. rq->inputsize -= in_buf.size;
  148. in_buf.src = dctx.kin + rq->pageofs_in;
  149. dctx.bounce = strm->bounce;
  150. do {
  151. dctx.inbuf_sz = in_buf.size;
  152. dctx.inbuf_pos = in_buf.pos;
  153. reason = z_erofs_stream_switch_bufs(&dctx, &out_buf.dst,
  154. (void **)&in_buf.src, pgpl);
  155. if (reason)
  156. break;
  157. if (out_buf.size == out_buf.pos) {
  158. out_buf.size = dctx.avail_out;
  159. out_buf.pos = 0;
  160. }
  161. in_buf.size = dctx.inbuf_sz;
  162. in_buf.pos = dctx.inbuf_pos;
  163. zerr = zstd_decompress_stream(stream, &out_buf, &in_buf);
  164. dctx.avail_out = out_buf.size - out_buf.pos;
  165. if (zstd_is_error(zerr) ||
  166. ((rq->outputsize + dctx.avail_out) && (!zerr || (zerr > 0 &&
  167. !(rq->inputsize + in_buf.size - in_buf.pos))))) {
  168. reason = zstd_is_error(zerr) ? zstd_get_error_name(zerr) :
  169. "unexpected end of stream";
  170. break;
  171. }
  172. } while (rq->outputsize + dctx.avail_out);
  173. if (dctx.kout)
  174. kunmap_local(dctx.kout);
  175. failed_zinit:
  176. kunmap_local(dctx.kin);
  177. /* 4. push back ZSTD stream context to the global list */
  178. spin_lock(&z_erofs_zstd_lock);
  179. strm->next = z_erofs_zstd_head;
  180. z_erofs_zstd_head = strm;
  181. spin_unlock(&z_erofs_zstd_lock);
  182. wake_up(&z_erofs_zstd_wq);
  183. return reason;
  184. }
  185. const struct z_erofs_decompressor z_erofs_zstd_decomp = {
  186. .config = z_erofs_load_zstd_config,
  187. .decompress = z_erofs_zstd_decompress,
  188. .init = z_erofs_zstd_init,
  189. .exit = z_erofs_zstd_exit,
  190. .name = "zstd",
  191. };