1 // SPDX-License-Identifier: GPL-2.0-only
4 * Phillip Lougher <phillip@squashfs.org.uk>
7 #include <linux/types.h>
8 #include <linux/slab.h>
9 #include <linux/percpu.h>
10 #include <linux/local_lock.h>
12 #include "squashfs_fs.h"
13 #include "squashfs_fs_sb.h"
14 #include "decompressor.h"
18 * This file implements multi-threaded decompression using percpu
19 * variables, one thread per cpu core.
22 struct squashfs_stream
{
27 static void *squashfs_decompressor_create(struct squashfs_sb_info
*msblk
,
30 struct squashfs_stream
*stream
;
31 struct squashfs_stream __percpu
*percpu
;
34 percpu
= alloc_percpu(struct squashfs_stream
);
36 return ERR_PTR(-ENOMEM
);
38 for_each_possible_cpu(cpu
) {
39 stream
= per_cpu_ptr(percpu
, cpu
);
40 stream
->stream
= msblk
->decompressor
->init(msblk
, comp_opts
);
41 if (IS_ERR(stream
->stream
)) {
42 err
= PTR_ERR(stream
->stream
);
45 local_lock_init(&stream
->lock
);
49 return (void *)(__force
unsigned long) percpu
;
52 for_each_possible_cpu(cpu
) {
53 stream
= per_cpu_ptr(percpu
, cpu
);
54 if (!IS_ERR_OR_NULL(stream
->stream
))
55 msblk
->decompressor
->free(stream
->stream
);
61 static void squashfs_decompressor_destroy(struct squashfs_sb_info
*msblk
)
63 struct squashfs_stream __percpu
*percpu
=
64 (void __percpu
*)(unsigned long) msblk
->stream
;
65 struct squashfs_stream
*stream
;
69 for_each_possible_cpu(cpu
) {
70 stream
= per_cpu_ptr(percpu
, cpu
);
71 msblk
->decompressor
->free(stream
->stream
);
77 static int squashfs_decompress(struct squashfs_sb_info
*msblk
, struct bio
*bio
,
78 int offset
, int length
, struct squashfs_page_actor
*output
)
80 struct squashfs_stream
*stream
;
81 struct squashfs_stream __percpu
*percpu
=
82 (void __percpu
*)(unsigned long) msblk
->stream
;
85 local_lock(&percpu
->lock
);
86 stream
= this_cpu_ptr(percpu
);
88 res
= msblk
->decompressor
->decompress(msblk
, stream
->stream
, bio
,
89 offset
, length
, output
);
91 local_unlock(&percpu
->lock
);
94 ERROR("%s decompression failed, data probably corrupt\n",
95 msblk
->decompressor
->name
);
100 static int squashfs_max_decompressors(void)
102 return num_possible_cpus();
105 const struct squashfs_decompressor_thread_ops squashfs_decompressor_percpu
= {
106 .create
= squashfs_decompressor_create
,
107 .destroy
= squashfs_decompressor_destroy
,
108 .decompress
= squashfs_decompress
,
109 .max_decompressors
= squashfs_max_decompressors
,