^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (c) 2013
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Minchan Kim <minchan@kernel.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/bio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/wait.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/cpumask.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include "squashfs_fs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include "squashfs_fs_sb.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include "decompressor.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include "squashfs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * This file implements multi-threaded decompression in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * decompressor framework
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * The reason that multiply two is that a CPU can request new I/O
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * while it is waiting previous request.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #define MAX_DECOMPRESSOR (num_online_cpus() * 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) int squashfs_max_decompressors(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) return MAX_DECOMPRESSOR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) struct squashfs_stream {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) void *comp_opts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) struct list_head strm_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) struct mutex mutex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) int avail_decomp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) wait_queue_head_t wait;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) struct decomp_stream {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) void *stream;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) struct list_head list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) static void put_decomp_stream(struct decomp_stream *decomp_strm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) struct squashfs_stream *stream)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) mutex_lock(&stream->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) list_add(&decomp_strm->list, &stream->strm_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) mutex_unlock(&stream->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) wake_up(&stream->wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) void *squashfs_decompressor_create(struct squashfs_sb_info *msblk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) void *comp_opts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) struct squashfs_stream *stream;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) struct decomp_stream *decomp_strm = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) int err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) stream = kzalloc(sizeof(*stream), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) if (!stream)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) stream->comp_opts = comp_opts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) mutex_init(&stream->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) INIT_LIST_HEAD(&stream->strm_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) init_waitqueue_head(&stream->wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) * We should have a decompressor at least as default
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) * so if we fail to allocate new decompressor dynamically,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) * we could always fall back to default decompressor and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) * file system works.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) decomp_strm = kmalloc(sizeof(*decomp_strm), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) if (!decomp_strm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) decomp_strm->stream = msblk->decompressor->init(msblk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) stream->comp_opts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) if (IS_ERR(decomp_strm->stream)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) err = PTR_ERR(decomp_strm->stream);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) list_add(&decomp_strm->list, &stream->strm_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) stream->avail_decomp = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) return stream;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) kfree(decomp_strm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) kfree(stream);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) void squashfs_decompressor_destroy(struct squashfs_sb_info *msblk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) struct squashfs_stream *stream = msblk->stream;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) if (stream) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) struct decomp_stream *decomp_strm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) while (!list_empty(&stream->strm_list)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) decomp_strm = list_entry(stream->strm_list.prev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) struct decomp_stream, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) list_del(&decomp_strm->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) msblk->decompressor->free(decomp_strm->stream);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) kfree(decomp_strm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) stream->avail_decomp--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) WARN_ON(stream->avail_decomp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) kfree(stream->comp_opts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) kfree(stream);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) static struct decomp_stream *get_decomp_stream(struct squashfs_sb_info *msblk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) struct squashfs_stream *stream)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) struct decomp_stream *decomp_strm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) mutex_lock(&stream->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) /* There is available decomp_stream */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) if (!list_empty(&stream->strm_list)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) decomp_strm = list_entry(stream->strm_list.prev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) struct decomp_stream, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) list_del(&decomp_strm->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) mutex_unlock(&stream->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) * If there is no available decomp and already full,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) * let's wait for releasing decomp from other users.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) if (stream->avail_decomp >= MAX_DECOMPRESSOR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) goto wait;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) /* Let's allocate new decomp */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) decomp_strm = kmalloc(sizeof(*decomp_strm), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) if (!decomp_strm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) goto wait;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) decomp_strm->stream = msblk->decompressor->init(msblk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) stream->comp_opts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) if (IS_ERR(decomp_strm->stream)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) kfree(decomp_strm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) goto wait;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) stream->avail_decomp++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) WARN_ON(stream->avail_decomp > MAX_DECOMPRESSOR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) mutex_unlock(&stream->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) wait:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) * If system memory is tough, let's for other's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) * releasing instead of hurting VM because it could
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) * make page cache thrashing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) mutex_unlock(&stream->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) wait_event(stream->wait,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) !list_empty(&stream->strm_list));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) return decomp_strm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) int squashfs_decompress(struct squashfs_sb_info *msblk, struct bio *bio,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) int offset, int length,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) struct squashfs_page_actor *output)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) int res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) struct squashfs_stream *stream = msblk->stream;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) struct decomp_stream *decomp_stream = get_decomp_stream(msblk, stream);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) res = msblk->decompressor->decompress(msblk, decomp_stream->stream,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) bio, offset, length, output);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) put_decomp_stream(decomp_stream, stream);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) if (res < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) ERROR("%s decompression failed, data probably corrupt\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) msblk->decompressor->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) }