Merge tag 'for-6.5/io_uring-2023-06-23' of git://git.kernel.dk/linux
[linux-block.git] / fs / squashfs / decompressor_multi_percpu.c
CommitLineData
20c8ccb1 1// SPDX-License-Identifier: GPL-2.0-only
d208383d
PL
2/*
3 * Copyright (c) 2013
4 * Phillip Lougher <phillip@squashfs.org.uk>
d208383d
PL
5 */
6
7#include <linux/types.h>
8#include <linux/slab.h>
9#include <linux/percpu.h>
10#include <linux/buffer_head.h>
fd56200a 11#include <linux/local_lock.h>
d208383d
PL
12
13#include "squashfs_fs.h"
14#include "squashfs_fs_sb.h"
15#include "decompressor.h"
16#include "squashfs.h"
17
18/*
19 * This file implements multi-threaded decompression using percpu
20 * variables, one thread per cpu core.
21 */
22
23struct squashfs_stream {
fd56200a
JC
24 void *stream;
25 local_lock_t lock;
d208383d
PL
26};
27
80f78409 28static void *squashfs_decompressor_create(struct squashfs_sb_info *msblk,
d208383d
PL
29 void *comp_opts)
30{
31 struct squashfs_stream *stream;
32 struct squashfs_stream __percpu *percpu;
33 int err, cpu;
34
35 percpu = alloc_percpu(struct squashfs_stream);
36 if (percpu == NULL)
37 return ERR_PTR(-ENOMEM);
38
39 for_each_possible_cpu(cpu) {
40 stream = per_cpu_ptr(percpu, cpu);
41 stream->stream = msblk->decompressor->init(msblk, comp_opts);
42 if (IS_ERR(stream->stream)) {
43 err = PTR_ERR(stream->stream);
44 goto out;
45 }
fd56200a 46 local_lock_init(&stream->lock);
d208383d
PL
47 }
48
49 kfree(comp_opts);
50 return (__force void *) percpu;
51
52out:
53 for_each_possible_cpu(cpu) {
54 stream = per_cpu_ptr(percpu, cpu);
55 if (!IS_ERR_OR_NULL(stream->stream))
56 msblk->decompressor->free(stream->stream);
57 }
58 free_percpu(percpu);
59 return ERR_PTR(err);
60}
61
80f78409 62static void squashfs_decompressor_destroy(struct squashfs_sb_info *msblk)
d208383d
PL
63{
64 struct squashfs_stream __percpu *percpu =
65 (struct squashfs_stream __percpu *) msblk->stream;
66 struct squashfs_stream *stream;
67 int cpu;
68
69 if (msblk->stream) {
70 for_each_possible_cpu(cpu) {
71 stream = per_cpu_ptr(percpu, cpu);
72 msblk->decompressor->free(stream->stream);
73 }
74 free_percpu(percpu);
75 }
76}
77
80f78409 78static int squashfs_decompress(struct squashfs_sb_info *msblk, struct bio *bio,
93e72b3c 79 int offset, int length, struct squashfs_page_actor *output)
d208383d 80{
fd56200a 81 struct squashfs_stream *stream;
80f78409
XN
82 struct squashfs_stream __percpu *percpu =
83 (struct squashfs_stream __percpu *) msblk->stream;
fd56200a
JC
84 int res;
85
80f78409
XN
86 local_lock(&percpu->lock);
87 stream = this_cpu_ptr(percpu);
fd56200a 88
93e72b3c
PL
89 res = msblk->decompressor->decompress(msblk, stream->stream, bio,
90 offset, length, output);
fd56200a 91
80f78409 92 local_unlock(&percpu->lock);
d208383d
PL
93
94 if (res < 0)
95 ERROR("%s decompression failed, data probably corrupt\n",
96 msblk->decompressor->name);
97
98 return res;
99}
100
80f78409 101static int squashfs_max_decompressors(void)
d208383d
PL
102{
103 return num_possible_cpus();
104}
80f78409
XN
105
106const struct squashfs_decompressor_thread_ops squashfs_decompressor_percpu = {
107 .create = squashfs_decompressor_create,
108 .destroy = squashfs_decompressor_destroy,
109 .decompress = squashfs_decompress,
110 .max_decompressors = squashfs_max_decompressors,
111};