decompressor_zstd.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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(sizeof(*strm), GFP_KERNEL);
  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 int 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 = {
  123. .rq = rq,
  124. .inpages = PAGE_ALIGN(rq->inputsize) >> PAGE_SHIFT,
  125. .outpages = PAGE_ALIGN(rq->pageofs_out + rq->outputsize)
  126. >> PAGE_SHIFT,
  127. .no = -1, .ni = 0,
  128. };
  129. zstd_in_buffer in_buf = { NULL, 0, 0 };
  130. zstd_out_buffer out_buf = { NULL, 0, 0 };
  131. struct z_erofs_zstd *strm;
  132. zstd_dstream *stream;
  133. int zerr, err;
  134. /* 1. get the exact compressed size */
  135. dctx.kin = kmap_local_page(*rq->in);
  136. err = z_erofs_fixup_insize(rq, dctx.kin + rq->pageofs_in,
  137. min(rq->inputsize, sb->s_blocksize - rq->pageofs_in));
  138. if (err) {
  139. kunmap_local(dctx.kin);
  140. return err;
  141. }
  142. /* 2. get an available ZSTD context */
  143. strm = z_erofs_isolate_strms(false);
  144. /* 3. multi-call decompress */
  145. stream = zstd_init_dstream(z_erofs_zstd_max_dictsize, strm->wksp, strm->wkspsz);
  146. if (!stream) {
  147. err = -EIO;
  148. goto failed_zinit;
  149. }
  150. rq->fillgaps = true; /* ZSTD doesn't support NULL output buffer */
  151. in_buf.size = min_t(u32, rq->inputsize, PAGE_SIZE - rq->pageofs_in);
  152. rq->inputsize -= in_buf.size;
  153. in_buf.src = dctx.kin + rq->pageofs_in;
  154. dctx.bounce = strm->bounce;
  155. do {
  156. dctx.avail_out = out_buf.size - out_buf.pos;
  157. dctx.inbuf_sz = in_buf.size;
  158. dctx.inbuf_pos = in_buf.pos;
  159. err = z_erofs_stream_switch_bufs(&dctx, &out_buf.dst,
  160. (void **)&in_buf.src, pgpl);
  161. if (err)
  162. break;
  163. if (out_buf.size == out_buf.pos) {
  164. out_buf.size = dctx.avail_out;
  165. out_buf.pos = 0;
  166. }
  167. in_buf.size = dctx.inbuf_sz;
  168. in_buf.pos = dctx.inbuf_pos;
  169. zerr = zstd_decompress_stream(stream, &out_buf, &in_buf);
  170. if (zstd_is_error(zerr) || (!zerr && rq->outputsize)) {
  171. erofs_err(sb, "failed to decompress in[%u] out[%u]: %s",
  172. rq->inputsize, rq->outputsize,
  173. zerr ? zstd_get_error_name(zerr) : "unexpected end of stream");
  174. err = -EFSCORRUPTED;
  175. break;
  176. }
  177. } while (rq->outputsize || out_buf.pos < out_buf.size);
  178. if (dctx.kout)
  179. kunmap_local(dctx.kout);
  180. failed_zinit:
  181. kunmap_local(dctx.kin);
  182. /* 4. push back ZSTD stream context to the global list */
  183. spin_lock(&z_erofs_zstd_lock);
  184. strm->next = z_erofs_zstd_head;
  185. z_erofs_zstd_head = strm;
  186. spin_unlock(&z_erofs_zstd_lock);
  187. wake_up(&z_erofs_zstd_wq);
  188. return err;
  189. }
  190. const struct z_erofs_decompressor z_erofs_zstd_decomp = {
  191. .config = z_erofs_load_zstd_config,
  192. .decompress = z_erofs_zstd_decompress,
  193. .init = z_erofs_zstd_init,
  194. .exit = z_erofs_zstd_exit,
  195. .name = "zstd",
  196. };