decompressor_multi_percpu.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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/local_lock.h>
  10. #include "squashfs_fs.h"
  11. #include "squashfs_fs_sb.h"
  12. #include "decompressor.h"
  13. #include "squashfs.h"
  14. /*
  15. * This file implements multi-threaded decompression using percpu
  16. * variables, one thread per cpu core.
  17. */
  18. struct squashfs_stream {
  19. void *stream;
  20. local_lock_t lock;
  21. };
  22. static void *squashfs_decompressor_create(struct squashfs_sb_info *msblk,
  23. void *comp_opts)
  24. {
  25. struct squashfs_stream *stream;
  26. struct squashfs_stream __percpu *percpu;
  27. int err, cpu;
  28. percpu = alloc_percpu(struct squashfs_stream);
  29. if (percpu == NULL)
  30. return ERR_PTR(-ENOMEM);
  31. for_each_possible_cpu(cpu) {
  32. stream = per_cpu_ptr(percpu, cpu);
  33. stream->stream = msblk->decompressor->init(msblk, comp_opts);
  34. if (IS_ERR(stream->stream)) {
  35. err = PTR_ERR(stream->stream);
  36. goto out;
  37. }
  38. local_lock_init(&stream->lock);
  39. }
  40. kfree(comp_opts);
  41. return (void *)(__force unsigned long) percpu;
  42. out:
  43. for_each_possible_cpu(cpu) {
  44. stream = per_cpu_ptr(percpu, cpu);
  45. if (!IS_ERR_OR_NULL(stream->stream))
  46. msblk->decompressor->free(stream->stream);
  47. }
  48. free_percpu(percpu);
  49. return ERR_PTR(err);
  50. }
  51. static void squashfs_decompressor_destroy(struct squashfs_sb_info *msblk)
  52. {
  53. struct squashfs_stream __percpu *percpu =
  54. (void __percpu *)(unsigned long) msblk->stream;
  55. struct squashfs_stream *stream;
  56. int cpu;
  57. if (msblk->stream) {
  58. for_each_possible_cpu(cpu) {
  59. stream = per_cpu_ptr(percpu, cpu);
  60. msblk->decompressor->free(stream->stream);
  61. }
  62. free_percpu(percpu);
  63. }
  64. }
  65. static int squashfs_decompress(struct squashfs_sb_info *msblk, struct bio *bio,
  66. int offset, int length, struct squashfs_page_actor *output)
  67. {
  68. struct squashfs_stream *stream;
  69. struct squashfs_stream __percpu *percpu =
  70. (void __percpu *)(unsigned long) msblk->stream;
  71. int res;
  72. local_lock(&percpu->lock);
  73. stream = this_cpu_ptr(percpu);
  74. res = msblk->decompressor->decompress(msblk, stream->stream, bio,
  75. offset, length, output);
  76. local_unlock(&percpu->lock);
  77. if (res < 0)
  78. ERROR("%s decompression failed, data probably corrupt\n",
  79. msblk->decompressor->name);
  80. return res;
  81. }
  82. static int squashfs_max_decompressors(void)
  83. {
  84. return num_possible_cpus();
  85. }
  86. const struct squashfs_decompressor_thread_ops squashfs_decompressor_percpu = {
  87. .create = squashfs_decompressor_create,
  88. .destroy = squashfs_decompressor_destroy,
  89. .decompress = squashfs_decompress,
  90. .max_decompressors = squashfs_max_decompressors,
  91. };