decompressor_multi_percpu.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2013
  4. * Phillip Lougher <phillip@squashfs.org.uk>
  5. */
  6. #include <linux/types.h>
  7. #include <linux/slab.h>
  8. #include <linux/percpu.h>
  9. #include <linux/buffer_head.h>
  10. #include <linux/local_lock.h>
  11. #include "squashfs_fs.h"
  12. #include "squashfs_fs_sb.h"
  13. #include "decompressor.h"
  14. #include "squashfs.h"
  15. /*
  16. * This file implements multi-threaded decompression using percpu
  17. * variables, one thread per cpu core.
  18. */
  19. struct squashfs_stream {
  20. void *stream;
  21. local_lock_t lock;
  22. };
  23. void *squashfs_decompressor_create(struct squashfs_sb_info *msblk,
  24. void *comp_opts)
  25. {
  26. struct squashfs_stream *stream;
  27. struct squashfs_stream __percpu *percpu;
  28. int err, cpu;
  29. percpu = alloc_percpu(struct squashfs_stream);
  30. if (percpu == NULL)
  31. return ERR_PTR(-ENOMEM);
  32. for_each_possible_cpu(cpu) {
  33. stream = per_cpu_ptr(percpu, cpu);
  34. stream->stream = msblk->decompressor->init(msblk, comp_opts);
  35. if (IS_ERR(stream->stream)) {
  36. err = PTR_ERR(stream->stream);
  37. goto out;
  38. }
  39. local_lock_init(&stream->lock);
  40. }
  41. kfree(comp_opts);
  42. return (__force void *) percpu;
  43. out:
  44. for_each_possible_cpu(cpu) {
  45. stream = per_cpu_ptr(percpu, cpu);
  46. if (!IS_ERR_OR_NULL(stream->stream))
  47. msblk->decompressor->free(stream->stream);
  48. }
  49. free_percpu(percpu);
  50. return ERR_PTR(err);
  51. }
  52. void squashfs_decompressor_destroy(struct squashfs_sb_info *msblk)
  53. {
  54. struct squashfs_stream __percpu *percpu =
  55. (struct squashfs_stream __percpu *) msblk->stream;
  56. struct squashfs_stream *stream;
  57. int cpu;
  58. if (msblk->stream) {
  59. for_each_possible_cpu(cpu) {
  60. stream = per_cpu_ptr(percpu, cpu);
  61. msblk->decompressor->free(stream->stream);
  62. }
  63. free_percpu(percpu);
  64. }
  65. }
  66. int squashfs_decompress(struct squashfs_sb_info *msblk, struct bio *bio,
  67. int offset, int length, struct squashfs_page_actor *output)
  68. {
  69. struct squashfs_stream *stream;
  70. int res;
  71. local_lock(&msblk->stream->lock);
  72. stream = this_cpu_ptr(msblk->stream);
  73. res = msblk->decompressor->decompress(msblk, stream->stream, bio,
  74. offset, length, output);
  75. local_unlock(&msblk->stream->lock);
  76. if (res < 0)
  77. ERROR("%s decompression failed, data probably corrupt\n",
  78. msblk->decompressor->name);
  79. return res;
  80. }
  81. int squashfs_max_decompressors(void)
  82. {
  83. return num_possible_cpus();
  84. }