Squashfs: Refactor decompressor interface and code
Phillip Lougher
maintainerPATCH 1/7 Squashfs: Refactor decompressor interface and code
The decompressor interface and code was written from the point of view of single-threaded operation. In doing so it mixed a lot of single-threaded implementation specific aspects into the decompressor code and elsewhere which makes it difficult to seamlessly support multiple different decompressor implementations.
This patch does the following:
1. It removes compressor_options parsing from the decompressor init() function. This allows the decompressor init() function to be dynamically called to instantiate multiple decompressors, without the compressor options needing to be read and parsed each time.
2. It moves threading and all sleeping operations out of the decompressors. In doing so, it makes the decompressors non-blocking wrappers which only deal with interfacing with the decompressor implementation.
3. It splits decompressor.[ch] into decompressor generic functions in decompressor.[ch], and moves the single threaded decompressor implementation into decompressor_single.c.
The result of this patch is Squashfs should now be able to support multiple decompressors by adding new decompressor_xxx.c files with specialised implementations of the functions in decompressor_single.c
--- fs/squashfs/Makefile | 2 +- fs/squashfs/block.c | 11 +++-- fs/squashfs/decompressor.c | 47 ++++++++++++++------- fs/squashfs/decompressor.h | 21 +++------ fs/squashfs/decompressor_single.c | 86 +++++++++++++++++++++++++++++++++++++ fs/squashfs/lzo_wrapper.c | 24 +++-------- fs/squashfs/squashfs.h | 9 +++- fs/squashfs/squashfs_fs_sb.h | 3 +- fs/squashfs/super.c | 10 ++--- fs/squashfs/xz_wrapper.c | 89 +++++++++++++++++++++------------------ fs/squashfs/zlib_wrapper.c | 50 +++++++--------------- 11 files changed, 216 insertions(+), 136 deletions(-) create mode 100644 fs/squashfs/decompressor_single.c
fs/squashfs/Makefile +1 -1
@@ -4,7 +4,7 @@ obj-$(CONFIG_SQUASHFS) += squashfs.o squashfs-y += block.o cache.o dir.o export.o file.o fragment.o id.o inode.o -squashfs-y += namei.o super.o symlink.o decompressor.o +squashfs-y += namei.o super.o symlink.o decompressor.o decompressor_single.o squashfs-$(CONFIG_SQUASHFS_XATTR) += xattr.o xattr_id.o squashfs-$(CONFIG_SQUASHFS_LZO) += lzo_wrapper.o squashfs-$(CONFIG_SQUASHFS_XZ) += xz_wrapper.o
fs/squashfs/block.c +7 -4
@@ -93,7 +93,7 @@ int squashfs_read_data(struct super_block *sb, void **buffer, u64 index,struct buffer_head **bh;int offset = index & ((1 << msblk->devblksize_log2) -1 );u64 cur_index = index >> msblk->devblksize_log2; -int bytes, compressed, b =0 , k =0 , page =0 , avail; +int bytes, compressed, b =0 , k =0 , page =0 , avail, i; bh =kcalloc (((srclength + msblk->devblksize -1 ) >> msblk->devblksize_log2) +1 ,sizeof (*bh),GFP_KERNEL );
@@ -158,6 +158,12 @@ int squashfs_read_data(struct super_block *sb, void **buffer, u64 index,ll_rw_block (READ , b -1 , bh +1 ); } +for (i =0 ; i < b; i++) { +wait_on_buffer (bh[i]); +if (!buffer_uptodate (bh[i])) + goto block_release; + } +if (compressed) { length =squashfs_decompress (msblk, buffer, bh, b, offset, length, srclength, pages);
@@ -172,9 +178,6 @@ int squashfs_read_data(struct super_block *sb, void **buffer, u64 index,for (bytes = length; k < b; k++) { in =min (bytes, msblk->devblksize - offset); bytes -= in; -wait_on_buffer (bh[k]); -if (!buffer_uptodate (bh[k])) - goto block_release;while (in) {if (pg_offset ==PAGE_CACHE_SIZE ) { page++;
fs/squashfs/decompressor.c +32 -15
94 lines not on this page · raw message
fs/squashfs/decompressor.h +7 -14
@@ -24,28 +24,21 @@*/ struct squashfs_decompressor { -void *(*init)(struct squashfs_sb_info *,void *,int ); +void *(*init)(struct squashfs_sb_info *,void *); +void *(*comp_opts)(struct squashfs_sb_info *,void *,int );void (*free)(void *); -int (*decompress)(struct squashfs_sb_info *,void **, +int (*decompress)(struct squashfs_sb_info *,void *,void **,struct buffer_head **,int ,int ,int ,int ,int );int id;char *name;int supported; }; -static inline void squashfs_decompressor_free (struct squashfs_sb_info *msblk, -void *s) +static inline void *squashfs_comp_opts (struct squashfs_sb_info *msblk, +void *buff,int length) { -if (msblk->decompressor) - msblk->decompressor->free (s); -} - -static inline int squashfs_decompress (struct squashfs_sb_info *msblk, -void **buffer,struct buffer_head **bh,int b,int offset,int length, -int srclength,int pages) -{ -return msblk->decompressor->decompress (msblk, buffer, bh, b, offset, - length, srclength, pages); +return msblk->decompressor->comp_opts ? + msblk->decompressor->comp_opts (msblk, buff, length) :NULL ; }#ifdef CONFIG_SQUASHFS_XZ
fs/squashfs/decompressor_single.c new file +86 -0
86 lines not on this page · raw message
fs/squashfs/lzo_wrapper.c +5 -19
@@ -37,7 +37,7 @@ struct squashfs_lzo {void *output; }; -static void *lzo_init (struct squashfs_sb_info *msblk,void *buff,int len) +static void *lzo_init (struct squashfs_sb_info *msblk,void *buff) {int block_size =max_t (int, msblk->block_size,SQUASHFS_METADATA_SIZE );
@@ -74,22 +74,16 @@ static void lzo_free(void *strm) } -static int lzo_uncompress (struct squashfs_sb_info *msblk,void **buffer, -struct buffer_head **bh,int b,int offset,int length,int srclength, -int pages) +static int lzo_uncompress (struct squashfs_sb_info *msblk,void *strm, +void **buffer,struct buffer_head **bh,int b,int offset,int length, +int srclength,int pages) { -struct squashfs_lzo *stream = msblk->stream; +struct squashfs_lzo *stream = strm;void *buff = stream->input;int avail, i, bytes = length, res;size_t out_len = srclength; -mutex_lock (&msblk->read_data_mutex); -for (i =0 ; i < b; i++) { -wait_on_buffer (bh[i]); -if (!buffer_uptodate (bh[i])) - goto block_release; - avail =min (bytes, msblk->devblksize - offset);memcpy (buff, bh[i]->b_data + offset, avail); buff += avail;
@@ -111,17 +105,9 @@ static int lzo_uncompress(struct squashfs_sb_info *msblk, void **buffer, bytes -= avail; } -mutex_unlock (&msblk->read_data_mutex);return res; -block_release: -for (; i < b; i++) -put_bh (bh[i]); - failed: -mutex_unlock (&msblk->read_data_mutex); - -ERROR ("lzo decompression failed, data probably corrupt\n" );return -EIO ; }
fs/squashfs/squashfs.h +8 -1
@@ -48,7 +48,14 @@ extern void *squashfs_read_table(struct super_block *, u64, int);/* decompressor.c */ extern const struct squashfs_decompressor *squashfs_lookup_decompressor (int ); -extern void *squashfs_decompressor_init (struct super_block *,unsigned short ); +extern void *squashfs_decompressor_setup (struct super_block *,unsigned short ); + +/* decompressor_xxx.c */ +extern void *squashfs_decompressor_create (struct squashfs_sb_info *,void *); +extern void squashfs_decompressor_destroy (struct squashfs_sb_info *); +extern int squashfs_decompress (struct squashfs_sb_info *,void **, +struct buffer_head **,int ,int ,int ,int ,int ); +extern int squashfs_max_decompressors (void );/* export.c */ extern __le64 *squashfs_read_inode_lookup_table(struct super_block *, u64, u64,
fs/squashfs/squashfs_fs_sb.h +1 -2
@@ -63,10 +63,9 @@ struct squashfs_sb_info {__le64 *id_table;__le64 *fragment_index;__le64 *xattr_id_table; -struct mutex read_data_mutex;struct mutex meta_index_mutex;struct meta_index *meta_index; -void *stream; +struct squashfs_stream *stream;__le64 *inode_lookup_table;u64 inode_table;u64 directory_table;
fs/squashfs/super.c +5 -5
@@ -98,7 +98,6 @@ static int squashfs_fill_super(struct super_block *sb, void *data, int silent) msblk->devblksize =sb_min_blocksize (sb,SQUASHFS_DEVBLK_SIZE ); msblk->devblksize_log2 =ffz (~msblk->devblksize); -mutex_init (&msblk->read_data_mutex);mutex_init (&msblk->meta_index_mutex);/*
@@ -206,13 +205,14 @@ static int squashfs_fill_super(struct super_block *sb, void *data, int silent) goto failed_mount;/* Allocate read_page block */ - msblk->read_page =squashfs_cache_init ("data" ,1 , msblk->block_size); + msblk->read_page =squashfs_cache_init ("data" , +squashfs_max_decompressors (), msblk->block_size);if (msblk->read_page ==NULL ) {ERROR ("Failed to allocate read_page block\n" ); goto failed_mount; } - msblk->stream =squashfs_decompressor_init (sb, flags); + msblk->stream =squashfs_decompressor_setup (sb, flags);if (IS_ERR (msblk->stream)) { err =PTR_ERR (msblk->stream); msblk->stream =NULL ;
@@ -336,7 +336,7 @@ failed_mount:squashfs_cache_delete (msblk->block_cache);squashfs_cache_delete (msblk->fragment_cache);squashfs_cache_delete (msblk->read_page); -squashfs_decompressor_free (msblk, msblk->stream); +squashfs_decompressor_destroy (msblk);kfree (msblk->inode_lookup_table);kfree (msblk->fragment_index);kfree (msblk->id_table);
@@ -383,7 +383,7 @@ static void squashfs_put_super(struct super_block *sb)squashfs_cache_delete (sbi->block_cache);squashfs_cache_delete (sbi->fragment_cache);squashfs_cache_delete (sbi->read_page); -squashfs_decompressor_free (sbi, sbi->stream); +squashfs_decompressor_destroy (sbi);kfree (sbi->id_table);kfree (sbi->fragment_index);kfree (sbi->meta_index);
fs/squashfs/xz_wrapper.c +49 -40
148 lines not on this page · raw message
fs/squashfs/zlib_wrapper.c +15 -35
88 lines not on this page · raw message
Signature · 1 line
1.8.3.2
Phillip Lougher
maintainerPATCH 2/7 squashfs: Enhance parallel I/O
From: Minchan Kim <[email protected]>
Now squashfs have used for only one stream buffer for decompression so it hurts parallel read performance so this patch supports multiple decompressor to enhance performance parallel I/O.
Four 1G file dd read on KVM machine which has 2 CPU and 4G memory.
dd if=test/test1.dat of=/dev/null &
dd if=test/test2.dat of=/dev/null &
dd if=test/test3.dat of=/dev/null &
dd if=test/test4.dat of=/dev/null &
old : 1m39s -> new : 9s
* From v1
* Change comp_strm with decomp_strm - Phillip
* Change/add comments - Phillip
--- fs/squashfs/Kconfig | 13 +++ fs/squashfs/Makefile | 9 +- fs/squashfs/decompressor_multi.c | 200 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 221 insertions(+), 1 deletion(-) create mode 100644 fs/squashfs/decompressor_multi.c
fs/squashfs/Kconfig +13 -0
@@ -63,6 +63,19 @@ config SQUASHFS_LZO If unsure, say N. +config SQUASHFS_MULTI_DECOMPRESSOR + bool "Use multiple decompressors for handling parallel I/O" + depends on SQUASHFS + help + By default Squashfs uses a single decompressor but it gives + poor performance on parallel I/O workloads when using multiple CPU + machines due to waiting on decompressor availability. + + If you have a parallel I/O workload and your system has enough memory, + using this option may improve overall I/O performance. + + If unsure, say N. + config SQUASHFS_XZ bool "Include support for XZ compressed file systems" depends on SQUASHFS
fs/squashfs/Makefile +8 -1
@@ -4,8 +4,15 @@ obj-$(CONFIG_SQUASHFS) += squashfs.o squashfs-y += block.o cache.o dir.o export.o file.o fragment.o id.o inode.o -squashfs-y += namei.o super.o symlink.o decompressor.o decompressor_single.o +squashfs-y += namei.o super.o symlink.o decompressor.o + squashfs-$(CONFIG_SQUASHFS_XATTR) += xattr.o xattr_id.o squashfs-$(CONFIG_SQUASHFS_LZO) += lzo_wrapper.o squashfs-$(CONFIG_SQUASHFS_XZ) += xz_wrapper.o squashfs-$(CONFIG_SQUASHFS_ZLIB) += zlib_wrapper.o + +ifdef CONFIG_SQUASHFS_MULTI_DECOMPRESSOR + squashfs-y += decompressor_multi.o +else + squashfs-y += decompressor_single.o +endif
fs/squashfs/decompressor_multi.c new file +200 -0
200 lines not on this page · raw message
Signature · 1 line
1.8.3.2
Phillip Lougher
maintainerPATCH 3/7 Squashfs: add multi-threaded decompression using percpu variables (V2)
In reply to Phillip LougherAdd a multi-threaded decompression implementation which uses percpu variables.
Using percpu variables has advantages and disadvantages over implementations which do not use percpu variables.
Advantages:
* the nature of percpu variables ensures decompression is load-balanced across the multiple cores.
* simplicity.
Disadvantages: it limits decompression to one thread per core.
V2:
* squashfs_decompressor_create: improve error handling path, re freeing of decompressors and comp_opts
* decompressor_multi_percpu.c: include percpu.h header
* Kconfig: indentation
--- fs/squashfs/Kconfig | 57 ++++++++++++++----- fs/squashfs/Makefile | 10 +--- fs/squashfs/decompressor_multi_percpu.c | 98 +++++++++++++++++++++++++++++++++ 3 files changed, 145 insertions(+), 20 deletions(-) create mode 100644 fs/squashfs/decompressor_multi_percpu.c
fs/squashfs/Kconfig +44 -13
@@ -25,6 +25,50 @@ config SQUASHFS If unsure, say N. +choice + prompt "Decompressor parallelisation options" + depends on SQUASHFS + help + Squashfs now supports three parallelisation options for + decompression. Each one exhibits various trade-offs between + decompression performance and CPU and memory usage. + + If in doubt, select "Single threaded compression" + +config SQUASHFS_DECOMP_SINGLE + bool "Single threaded compression" + help + Traditionally Squashfs has used single-threaded decompression. + Only one block (data or metadata) can be decompressed at any + one time. This limits CPU and memory usage to a minimum. + +config SQUASHFS_DECOMP_MULTI + bool "Use multiple decompressors for parallel I/O" + help + By default Squashfs uses a single decompressor but it gives + poor performance on parallel I/O workloads when using multiple CPU + machines due to waiting on decompressor availability. + + If you have a parallel I/O workload and your system has enough memory, + using this option may improve overall I/O performance. + + This decompressor implementation uses up to two parallel + decompressors per core. It dynamically allocates decompressors + on a demand basis. + +config SQUASHFS_DECOMP_MULTI_PERCPU + bool "Use percpu multiple decompressors for parallel I/O" + help + By default Squashfs uses a single decompressor but it gives + poor performance on parallel I/O workloads when using multiple CPU + machines due to waiting on decompressor availability. + + This decompressor implementation uses a maximum of one + decompressor per core. It uses percpu variables to ensure + decompression is load-balanced across the cores. + +endchoice + config SQUASHFS_XATTR bool "Squashfs XATTR support" depends on SQUASHFS
@@ -63,19 +107,6 @@ config SQUASHFS_LZO If unsure, say N. -config SQUASHFS_MULTI_DECOMPRESSOR - bool "Use multiple decompressors for handling parallel I/O" - depends on SQUASHFS - help - By default Squashfs uses a single decompressor but it gives - poor performance on parallel I/O workloads when using multiple CPU - machines due to waiting on decompressor availability. - - If you have a parallel I/O workload and your system has enough memory, - using this option may improve overall I/O performance. - - If unsure, say N. - config SQUASHFS_XZ bool "Include support for XZ compressed file systems" depends on SQUASHFS
fs/squashfs/Makefile +3 -7
@@ -5,14 +5,10 @@ obj-$(CONFIG_SQUASHFS) += squashfs.o squashfs-y += block.o cache.o dir.o export.o file.o fragment.o id.o inode.o squashfs-y += namei.o super.o symlink.o decompressor.o - +squashfs-$(CONFIG_SQUASHFS_DECOMP_SINGLE) += decompressor_single.o +squashfs-$(CONFIG_SQUASHFS_DECOMP_MULTI) += decompressor_multi.o +squashfs-$(CONFIG_SQUASHFS_DECOMP_MULTI_PERCPU) += decompressor_multi_percpu.o squashfs-$(CONFIG_SQUASHFS_XATTR) += xattr.o xattr_id.o squashfs-$(CONFIG_SQUASHFS_LZO) += lzo_wrapper.o squashfs-$(CONFIG_SQUASHFS_XZ) += xz_wrapper.o squashfs-$(CONFIG_SQUASHFS_ZLIB) += zlib_wrapper.o - -ifdef CONFIG_SQUASHFS_MULTI_DECOMPRESSOR - squashfs-y += decompressor_multi.o -else - squashfs-y += decompressor_single.o -endif
fs/squashfs/decompressor_multi_percpu.c new file +98 -0
98 lines not on this page · raw message
Signature · 1 line
1.8.3.2
Re: [PATCH 3/7] Squashfs: add multi-threaded decompression using percpu variables (V2)
Quoted 32 lines · Phillip Lougher
Add a multi-threaded decompression implementation which uses percpu variables.
Using percpu variables has advantages and disadvantages over implementations which do not use percpu variables.
Advantages:
* the nature of percpu variables ensures decompression is load-balanced across the multiple cores.
* simplicity.Disadvantages: it limits decompression to one thread per core.
V2:
*squashfs_decompressor_create: improve error handling path, re freeing of decompressors andcomp_opts
*decompressor_multi_percpu.c: includepercpu.hheader
*Kconfig: indentationSigned-off-by Phillip Lougher <[email protected]>--- fs/squashfs/Kconfig | 57 ++++++++++++++----- fs/squashfs/Makefile | 10 +--- fs/squashfs/decompressor_multi_percpu.c | 98 +++++++++++++++++++++++++++++++++ 3 files changed, 145 insertions(+), 20 deletions(-) create mode 100644 fs/squashfs/decompressor_multi_percpu.cfs/squashfs/Kconfig +0
-0
fs/squashfs/Kconfig config SQUASHFS · line 29
Quoted 5 lines
fs/squashfs/Kconfig +2
-0@@ -25,6 +25,50 @@ config SQUASHFS If unsure, say N. +choice + prompt "Decompressor parallelisation options"
Nitpick:
How about adding default explicitly?
default SQUASHFS_DECOMP_SINGLE
Quoted 28 lines
fs/squashfs/Kconfig +28
-0+ depends on SQUASHFS + help + Squashfs now supports three parallelisation options for + decompression. Each one exhibits various trade-offs between + decompression performance and CPU and memory usage. + + If in doubt, select "Single threaded compression" + +config SQUASHFS_DECOMP_SINGLE + bool "Single threaded compression" + help + Traditionally Squashfs has used single-threaded decompression. + Only one block (data or metadata) can be decompressed at any + one time. This limits CPU and memory usage to a minimum. + +config SQUASHFS_DECOMP_MULTI + bool "Use multiple decompressors for parallel I/O" + help + By default Squashfs uses a single decompressor but it gives + poor performance on parallel I/O workloads when using multiple CPU + machines due to waiting on decompressor availability. + + If you have a parallel I/O workload and your system has enough memory, + using this option may improve overall I/O performance. + + This decompressor implementation uses up to two parallel + decompressors per core. It dynamically allocates decompressors + on a demand basis.
fs/squashfs/Kconfig config SQUASHFS · line 67
Quoted 10 lines
fs/squashfs/Kconfig +10
-0+ +config SQUASHFS_DECOMP_MULTI_PERCPU + bool "Use percpu multiple decompressors for parallel I/O" + help + By default Squashfs uses a single decompressor but it gives + poor performance on parallel I/O workloads when using multiple CPU + machines due to waiting on decompressor availability. + + This decompressor implementation uses a maximum of one + decompressor per core. It uses percpu variables to ensure
Minor:
^
unnecessary white space.
fs/squashfs/Kconfig config SQUASHFS · line 68
Quoted 1 line
fs/squashfs/Kconfig +1
-0+ decompression is load-balanced across the cores.
Actually, I am not sure it's good idea to mention percpu in description. Normal people wouldn't know that and I think what they can want to know is what's benefit compared to SQUASHFS_DECOMP_MULTI.
How about this?
This decompressor implementation uses a maximum of one decompressor per core and the decompressor is allocated statically so memory footprint is small and limited and I/O cannot be fluctuated by not failing decompressor dynamic allocation compared to SQUAHSDS_DECOMP_MULTI.
And I'd like to see what's your point about "decompression is load-balanced across the cores".
If scheduler assigns process A, B, C into a core, it couldn't be load-balanced. If scheduler assign process A, B, C into each core, it could be load-balanced. And it's same with SQUSHFS_DECOMP_MULTI.
Could you elaborate it a bit?
Otherwise, looks good to me.
Thanks!
Quoted 160 lines
fs/squashfs/Kconfig +3
-13+ +endchoice + config SQUASHFS_XATTR bool "Squashfs XATTR support" depends on SQUASHFS@@ -63,19 +107,6 @@ config SQUASHFS_LZO If unsure, say N. -config SQUASHFS_MULTI_DECOMPRESSOR - bool "Use multiple decompressors for handling parallel I/O" - depends on SQUASHFS - help - By default Squashfs uses a single decompressor but it gives - poor performance on parallel I/O workloads when using multiple CPU - machines due to waiting on decompressor availability. - - If you have a parallel I/O workload and your system has enough memory, - using this option may improve overall I/O performance. - - If unsure, say N. - config SQUASHFS_XZ bool "Include support for XZ compressed file systems" depends on SQUASHFSfs/squashfs/Makefile +3
-7@@ -5,14 +5,10 @@ obj-$(CONFIG_SQUASHFS) += squashfs.o squashfs-y += block.o cache.o dir.o export.o file.o fragment.o id.o inode.o squashfs-y += namei.o super.o symlink.o decompressor.o - +squashfs-$(CONFIG_SQUASHFS_DECOMP_SINGLE) += decompressor_single.o +squashfs-$(CONFIG_SQUASHFS_DECOMP_MULTI) += decompressor_multi.o +squashfs-$(CONFIG_SQUASHFS_DECOMP_MULTI_PERCPU) += decompressor_multi_percpu.o squashfs-$(CONFIG_SQUASHFS_XATTR) += xattr.o xattr_id.o squashfs-$(CONFIG_SQUASHFS_LZO) += lzo_wrapper.o squashfs-$(CONFIG_SQUASHFS_XZ) += xz_wrapper.o squashfs-$(CONFIG_SQUASHFS_ZLIB) += zlib_wrapper.o - -ifdef CONFIG_SQUASHFS_MULTI_DECOMPRESSOR - squashfs-y += decompressor_multi.o -else - squashfs-y += decompressor_single.o -endiffs/squashfs/decompressor_multi_percpu.c new file +98
-0@@ -0,0 +1,98 @@ +/* +* Copyright (c) 2013 +* Phillip Lougher <[email protected]> +* +* This work is licensed under the terms of the GNU GPL, version 2. See +* the COPYING file in the top-level directory. +*/ + +#include <linux/types.h> +#include <linux/slab.h> +#include <linux/percpu.h> +#include <linux/buffer_head.h> + +#include "squashfs_fs.h" +#include "squashfs_fs_sb.h" +#include "decompressor.h" +#include "squashfs.h" + +/* +* This file implements multi-threaded decompression using percpu +* variables, one thread per cpu core. +*/ + +struct squashfs_stream { +void *stream; +}; + +void *squashfs_decompressor_create (struct squashfs_sb_info *msblk, +void *comp_opts) +{ +struct squashfs_stream *stream; +struct squashfs_stream __percpu *percpu; +int err, cpu; + + percpu = alloc_percpu(struct squashfs_stream); +if (percpu ==NULL ) +return ERR_PTR (-ENOMEM ); + +for_each_possible_cpu (cpu) { + stream =per_cpu_ptr (percpu, cpu); + stream->stream = msblk->decompressor->init (msblk, comp_opts); +if (IS_ERR (stream->stream)) { + err =PTR_ERR (stream->stream); + goto out; + } + } + +kfree (comp_opts); +return (__force void *) percpu; + +out: +for_each_possible_cpu (cpu) { + stream =per_cpu_ptr (percpu, cpu); +if (!IS_ERR_OR_NULL (stream->stream)) + msblk->decompressor->free (stream->stream); + } +free_percpu (percpu); +return ERR_PTR (err); +} + +void squashfs_decompressor_destroy (struct squashfs_sb_info *msblk) +{ +struct squashfs_stream __percpu *percpu = + (struct squashfs_stream __percpu *) msblk->stream; +struct squashfs_stream *stream; +int cpu; + +if (msblk->stream) { +for_each_possible_cpu (cpu) { + stream =per_cpu_ptr (percpu, cpu); + msblk->decompressor->free (stream->stream); + } +free_percpu (percpu); + } +} + +int squashfs_decompress (struct squashfs_sb_info *msblk, +void **buffer,struct buffer_head **bh,int b,int offset,int length, +int srclength,int pages) +{ +struct squashfs_stream __percpu *percpu = + (struct squashfs_stream __percpu *) msblk->stream; +struct squashfs_stream *stream =get_cpu_ptr (percpu); +int res = msblk->decompressor->decompress (msblk, stream->stream, buffer, + bh, b, offset, length, srclength, pages); +put_cpu_ptr (stream); + +if (res <0 ) +ERROR ("%s decompression failed, data probably corrupt\n" , + msblk->decompressor->name); + +return res; +} + +int squashfs_max_decompressors (void ) +{ +return num_possible_cpus (); +}Signature · 7 lines
1.8.3.2 -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/
Signature · 2 lines
Kind regards, Minchan Kim
Re: [PATCH 3/7] Squashfs: add multi-threaded decompression using percpu variables (V2)
Quoted 36 lines · Minchan Kim
Quoted 35 lines · Phillip Lougher
Add a multi-threaded decompression implementation which uses percpu variables.
Using percpu variables has advantages and disadvantages over implementations which do not use percpu variables.
Advantages:
* the nature of percpu variables ensures decompression is load-balanced across the multiple cores.
* simplicity.Disadvantages: it limits decompression to one thread per core.
V2:
*squashfs_decompressor_create: improve error handling path, re freeing of decompressors andcomp_opts
*decompressor_multi_percpu.c: includepercpu.hheader
*Kconfig: indentationSigned-off-by Phillip Lougher <[email protected]>--- fs/squashfs/Kconfig | 57 ++++++++++++++----- fs/squashfs/Makefile | 10 +--- fs/squashfs/decompressor_multi_percpu.c | 98 +++++++++++++++++++++++++++++++++ 3 files changed, 145 insertions(+), 20 deletions(-)create mode 100644
fs/squashfs/decompressor_multi_percpu.cfs/squashfs/Kconfig +0
-0@@ -25,6 +25,50 @@ config SQUASHFS If unsure, say N.
Quoted 7 lines
fs/squashfs/Kconfig config SQUASHFS · line 29
Quoted 2 lines
fs/squashfs/Kconfig +2
-0+choice + prompt "Decompressor parallelisation options"Nitpick:
How about adding default explicitly?default
SQUASHFS_DECOMP_SINGLE
I initially did that :-) .... only to get an error returned from kbuild that defaults on choices were
not supported. PITA.
Quoted 35 lines
Quoted 35 lines
fs/squashfs/Kconfig +34
-0+ depends on SQUASHFS + help + Squashfs now supports three parallelisation options for + decompression. Each one exhibits various trade-offs between + decompression performance and CPU and memory usage. + + If in doubt, select "Single threaded compression" + +config SQUASHFS_DECOMP_SINGLE + bool "Single threaded compression" + help + Traditionally Squashfs has used single-threaded decompression. + Only one block (data or metadata) can be decompressed at any + one time. This limits CPU and memory usage to a minimum. + +config SQUASHFS_DECOMP_MULTI + bool "Use multiple decompressors for parallel I/O" + help + By default Squashfs uses a single decompressor but it gives + poor performance on parallel I/O workloads when using multiple CPU + machines due to waiting on decompressor availability. + + If you have a parallel I/O workload and your system has enough memory, + using this option may improve overall I/O performance. + + This decompressor implementation uses up to two parallel + decompressors per core. It dynamically allocates decompressors + on a demand basis. + +config SQUASHFS_DECOMP_MULTI_PERCPU + bool "Use percpu multiple decompressors for parallel I/O" + help + By default Squashfs uses a single decompressor but it gives + poor performance on parallel I/O workloads when using multiple CPU
Quoted 8 lines
fs/squashfs/Kconfig config SQUASHFS · line 67
Quoted 4 lines
fs/squashfs/Kconfig +4
-0+ machines due to waiting on decompressor availability. + + This decompressor implementation uses a maximum of one + decompressor per core. It uses percpu variables to ensureMinor:
^
unnecessary white space.
Two spaces after the full stop, starting a new sentence? As a kid back in the 1970s and early 80s that's what I was taught. Maybe it's become old fashioned and I've not noticed.
http://www.rednovalabs.com/generation-gap-one-space-or-two-after-a-period/
Quoted 6 lines
fs/squashfs/Kconfig config SQUASHFS · line 68
Quoted 2 lines
fs/squashfs/Kconfig +1
-0+ decompression is load-balanced across the cores.Actually, I am not sure it's good idea to mention percpu in description. Normal people wouldn't know that and I think what they can want to know is what's benefit compared to
SQUASHFS_DECOMP_MULTI.
The people who have asked me for the percpu implementation will know :-)
Some people want to have the percpu implementation. Some don't.
Some people who want the percpu implementation *only* want the percpu implementation.
Some people who don't want the percpu implementation *only* want the non-percpu implementation.
People are different, and try as I might, that's not going to go away anytime soon.
I have spent more time arguing over this 90 odd lines of code than anything else. Should I call the percpu implementation the new "bike shed"? Because that's how it seems to me at the moment.
Life's too short to get hung up about this. I'm adding both implementations because that keeps more people happy than the alternative.
Maybe I should just drop both implementations for this merge window, and invite everyone else to fight it out over which single implementation they want, and I'll take a ring side seat while it happens.
Quoted 9 lines
How about this?
This decompressor implementation uses a maximum of one decompressor per core and the decompressor is allocated
statically so memory footprint is small and limited and I/O cannot be fluctuated by not failing decompressor dynamic allocation compared toSQUAHSDS_DECOMP_MULTI.
Quoted 8 lines
And I'd like to see what's your point about "decompression is load-balanced across the cores".
If scheduler assigns process A, B, C into a core, it couldn't be load-balanced. If scheduler assign process A, B, C into each core, it could be load-balanced. And it's same with
SQUSHFS_DECOMP_MULTI.Could you elaborate it a bit?
Taking an percpu variable is an exclusive lock on that core. Whilst that percpu variable is held it ensures nothing else can be scheduled on that core. Any other parallel decompressions will by definition be guaranteed to be scheduled on another core.
Without percpu variables, and parallel threads it is entirely up to the scheduler whether it schedules them on different cores, or schedules both threads on the same core.
Phillip
Quoted 166 lines
Otherwise, looks good to me.
Thanks!
Quoted 160 lines
fs/squashfs/Kconfig +3
-13+ +endchoice + config SQUASHFS_XATTR bool "Squashfs XATTR support" depends on SQUASHFS@@ -63,19 +107,6 @@ config SQUASHFS_LZO If unsure, say N. -config SQUASHFS_MULTI_DECOMPRESSOR - bool "Use multiple decompressors for handling parallel I/O" - depends on SQUASHFS - help - By default Squashfs uses a single decompressor but it gives - poor performance on parallel I/O workloads when using multiple CPU - machines due to waiting on decompressor availability. - - If you have a parallel I/O workload and your system has enough memory, - using this option may improve overall I/O performance. - - If unsure, say N. - config SQUASHFS_XZ bool "Include support for XZ compressed file systems" depends on SQUASHFSfs/squashfs/Makefile +3
-7@@ -5,14 +5,10 @@ obj-$(CONFIG_SQUASHFS) += squashfs.o squashfs-y += block.o cache.o dir.o export.o file.o fragment.o id.o inode.o squashfs-y += namei.o super.o symlink.o decompressor.o - +squashfs-$(CONFIG_SQUASHFS_DECOMP_SINGLE) += decompressor_single.o +squashfs-$(CONFIG_SQUASHFS_DECOMP_MULTI) += decompressor_multi.o +squashfs-$(CONFIG_SQUASHFS_DECOMP_MULTI_PERCPU) += decompressor_multi_percpu.o squashfs-$(CONFIG_SQUASHFS_XATTR) += xattr.o xattr_id.o squashfs-$(CONFIG_SQUASHFS_LZO) += lzo_wrapper.o squashfs-$(CONFIG_SQUASHFS_XZ) += xz_wrapper.o squashfs-$(CONFIG_SQUASHFS_ZLIB) += zlib_wrapper.o - -ifdef CONFIG_SQUASHFS_MULTI_DECOMPRESSOR - squashfs-y += decompressor_multi.o -else - squashfs-y += decompressor_single.o -endiffs/squashfs/decompressor_multi_percpu.c new file +98
-1@@ -0,0 +1,98 @@ +/* +* Copyright (c) 2013 +* Phillip Lougher <[email protected]> +* +* This work is licensed under the terms of the GNU GPL, version 2. See +* the COPYING file in the top-level directory. +*/ + +#include <linux/types.h> +#include <linux/slab.h> +#include <linux/percpu.h> +#include <linux/buffer_head.h> + +#include "squashfs_fs.h" +#include "squashfs_fs_sb.h" +#include "decompressor.h" +#include "squashfs.h" + +/* +* This file implements multi-threaded decompression using percpu +* variables, one thread per cpu core. +*/ + +struct squashfs_stream { +void *stream; +}; + +void *squashfs_decompressor_create (struct squashfs_sb_info *msblk, +void *comp_opts) +{ +struct squashfs_stream *stream; +struct squashfs_stream __percpu *percpu; +int err, cpu; + + percpu = alloc_percpu(struct squashfs_stream); +if (percpu ==NULL ) +return ERR_PTR (-ENOMEM ); + +for_each_possible_cpu (cpu) { + stream =per_cpu_ptr (percpu, cpu); + stream->stream = msblk->decompressor->init (msblk, comp_opts); +if (IS_ERR (stream->stream)) { + err =PTR_ERR (stream->stream); + goto out; + } + } + +kfree (comp_opts); +return (__force void *) percpu; + +out: +for_each_possible_cpu (cpu) { + stream =per_cpu_ptr (percpu, cpu); +if (!IS_ERR_OR_NULL (stream->stream)) + msblk->decompressor->free (stream->stream); + } +free_percpu (percpu); +return ERR_PTR (err); +} + +void squashfs_decompressor_destroy (struct squashfs_sb_info *msblk) +{ +struct squashfs_stream __percpu *percpu = + (struct squashfs_stream __percpu *) msblk->stream; +struct squashfs_stream *stream; +int cpu; + +if (msblk->stream) { +for_each_possible_cpu (cpu) { + stream =per_cpu_ptr (percpu, cpu); + msblk->decompressor->free (stream->stream); + } +free_percpu (percpu); + } +} + +int squashfs_decompress (struct squashfs_sb_info *msblk, +void **buffer,struct buffer_head **bh,int b,int offset,int length, +int srclength,int pages) +{ +struct squashfs_stream __percpu *percpu = + (struct squashfs_stream __percpu *) msblk->stream; +struct squashfs_stream *stream =get_cpu_ptr (percpu); +int res = msblk->decompressor->decompress (msblk, stream->stream, buffer, + bh, b, offset, length, srclength, pages); +put_cpu_ptr (stream); + +if (res <0 ) +ERROR ("%s decompression failed, data probably corrupt\n" , + msblk->decompressor->name); + +return res; +} + +int squashfs_max_decompressors (void ) +{ +return num_possible_cpus (); +} --1.8.3.2
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
Phillip Lougher
maintainerPATCH 4/7 Squashfs: Generalise paging handling in the decompressors
In reply to Phillip LougherFurther generalise the decompressors by adding a page handler abstraction. This adds helpers to allow the decompressors to access and process the output buffers in an implementation independant manner.
This allows different types of output buffer to be passed to the decompressors, with the implementation specific aspects handled at decompression time, but without the knowledge being held in the decompressor wrapper code.
This will allow the decompressors to handle Squashfs cache buffers, and page cache pages.
This patch adds the abstraction and an implementation for the caches.
--- fs/squashfs/block.c | 27 ++++++++++-------- fs/squashfs/cache.c | 28 +++++++++++++++---- fs/squashfs/decompressor.c | 14 ++++++++-- fs/squashfs/decompressor.h | 5 ++-- fs/squashfs/decompressor_multi.c | 7 ++--- fs/squashfs/decompressor_multi_percpu.c | 9 +++--- fs/squashfs/decompressor_single.c | 9 +++--- fs/squashfs/lzo_wrapper.c | 27 ++++++++++++------ fs/squashfs/page_actor.h | 49 +++++++++++++++++++++++++++++++++ fs/squashfs/squashfs.h | 8 +++--- fs/squashfs/squashfs_fs_sb.h | 1 + fs/squashfs/xz_wrapper.c | 22 +++++++++------ fs/squashfs/zlib_wrapper.c | 24 ++++++++++------ 13 files changed, 163 insertions(+), 67 deletions(-) create mode 100644 fs/squashfs/page_actor.h
fs/squashfs/block.c +15 -12
@@ -36,6 +36,7 @@#include "squashfs_fs_sb.h" #include "squashfs.h" #include "decompressor.h" +#include "page_actor.h" /* * Read the metadata block length, this is stored in the first two
@@ -86,16 +87,16 @@ static struct buffer_head *get_block_length(struct super_block *sb,* generated a larger block - this does occasionally happen with compression * algorithms). */ -int squashfs_read_data (struct super_block *sb,void **buffer,u64 index, -int length,u64 *next_index,int srclength,int pages) +int squashfs_read_data (struct super_block *sb,u64 index,int length, +u64 *next_index,struct squashfs_page_actor *output) {struct squashfs_sb_info *msblk = sb->s_fs_info;struct buffer_head **bh;int offset = index & ((1 << msblk->devblksize_log2) -1 );u64 cur_index = index >> msblk->devblksize_log2; -int bytes, compressed, b =0 , k =0 , page =0 , avail, i; +int bytes, compressed, b =0 , k =0 , avail, i; - bh =kcalloc (((srclength + msblk->devblksize -1 ) + bh =kcalloc (((output->length + msblk->devblksize -1 ) >> msblk->devblksize_log2) +1 ,sizeof (*bh),GFP_KERNEL );if (bh ==NULL )return -ENOMEM ;
@@ -111,9 +112,9 @@ int squashfs_read_data(struct super_block *sb, void **buffer, u64 index, *next_index = index + length;TRACE ("Block @ 0x%llx, %scompressed size %d, src size %d\n" , - index, compressed ?"" :"un" , length, srclength); + index, compressed ?"" :"un" , length, output->length); -if (length <0 || length > srclength || +if (length <0 || length > output->length || (index + length) > msblk->bytes_used) goto read_failure;
@@ -145,7 +146,7 @@ int squashfs_read_data(struct super_block *sb, void **buffer, u64 index,TRACE ("Block @ 0x%llx, %scompressed size %d\n" , index, compressed ?"" :"un" , length); -if (length <0 || length > srclength || +if (length <0 || length > output->length || (index + length) > msblk->bytes_used) goto block_release;
@@ -165,8 +166,8 @@ int squashfs_read_data(struct super_block *sb, void **buffer, u64 index, }if (compressed) { - length =squashfs_decompress (msblk, buffer, bh, b, offset, - length, srclength, pages); + length =squashfs_decompress (msblk, bh, b, offset, length, + output);if (length <0 ) goto read_failure; }else {
@@ -174,19 +175,20 @@ int squashfs_read_data(struct super_block *sb, void **buffer, u64 index,* Block is uncompressed. */ int in, pg_offset =0 ; +void *data =squashfs_first_page (output);for (bytes = length; k < b; k++) { in =min (bytes, msblk->devblksize - offset); bytes -= in;while (in) {if (pg_offset ==PAGE_CACHE_SIZE ) { - page++; + data =squashfs_next_page (output); pg_offset =0 ; } avail =min_t (int, in,PAGE_CACHE_SIZE - pg_offset); -memcpy (buffer[page] + pg_offset, - bh[k]->b_data + offset, avail); +memcpy (data + pg_offset, bh[k]->b_data + offset, + avail); in -= avail; pg_offset += avail; offset += avail;
@@ -194,6 +196,7 @@ int squashfs_read_data(struct super_block *sb, void **buffer, u64 index, offset =0 ;put_bh (bh[k]); } +squashfs_finish_page (output); }kfree (bh);
fs/squashfs/cache.c +23 -5
@@ -56,6 +56,7 @@#include "squashfs_fs.h" #include "squashfs_fs_sb.h" #include "squashfs.h" +#include "page_actor.h" /* * Look-up block in cache, and increment usage count. If not in cache, read
@@ -119,9 +120,8 @@ struct squashfs_cache_entry *squashfs_cache_get(struct super_block *sb, entry->error =0 ;spin_unlock (&cache->lock); - entry->length =squashfs_read_data (sb, entry->data, - block, length, &entry->next_index, - cache->block_size, cache->pages); + entry->length =squashfs_read_data (sb, block, length, + &entry->next_index, entry->actor);spin_lock (&cache->lock);
@@ -220,6 +220,7 @@ void squashfs_cache_delete(struct squashfs_cache *cache)kfree (cache->entry[i].data[j]);kfree (cache->entry[i].data); } +kfree (cache->entry[i].actor); }kfree (cache->entry);
@@ -280,6 +281,13 @@ struct squashfs_cache *squashfs_cache_init(char *name, int entries, goto cleanup; } } + + entry->actor =squashfs_page_actor_init (entry->data, + cache->pages,0 ); +if (entry->actor ==NULL ) { +ERROR ("Failed to allocate %s cache entry\n" , name); + goto cleanup; + } }return cache;
@@ -410,6 +418,7 @@ void *squashfs_read_table(struct super_block *sb, u64 block, int length)int pages = (length +PAGE_CACHE_SIZE -1 ) >>PAGE_CACHE_SHIFT ;int i, res;void *table, *buffer, **data; +struct squashfs_page_actor *actor; table = buffer =kmalloc (length,GFP_KERNEL );if (table ==NULL )
@@ -421,19 +430,28 @@ void *squashfs_read_table(struct super_block *sb, u64 block, int length) goto failed; } + actor =squashfs_page_actor_init (data, pages, length); +if (actor ==NULL ) { + res = -ENOMEM ; + goto failed2; + } +for (i =0 ; i < pages; i++, buffer +=PAGE_CACHE_SIZE ) data[i] = buffer; - res =squashfs_read_data (sb, data, block, length | -SQUASHFS_COMPRESSED_BIT_BLOCK ,NULL , length, pages); + res =squashfs_read_data (sb, block, length | +SQUASHFS_COMPRESSED_BIT_BLOCK ,NULL , actor);kfree (data); +kfree (actor);if (res <0 ) goto failed;return table; +failed2: +kfree (data); failed:kfree (table);return ERR_PTR (res);
fs/squashfs/decompressor.c +11 -3
@@ -30,6 +30,7 @@#include "squashfs_fs_sb.h" #include "decompressor.h" #include "squashfs.h" +#include "page_actor.h" /* * This file (and decompressor.h) implements a decompressor framework for
@@ -87,6 +88,7 @@ static void *get_comp_opts(struct super_block *sb, unsigned short flags) {struct squashfs_sb_info *msblk = sb->s_fs_info;void *buffer =NULL , *comp_opts; +struct squashfs_page_actor *actor =NULL ;int length =0 ;/*
@@ -99,9 +101,14 @@ static void *get_comp_opts(struct super_block *sb, unsigned short flags) goto out; } - length =squashfs_read_data (sb, &buffer, -sizeof (struct squashfs_super_block ),0 ,NULL , -PAGE_CACHE_SIZE ,1 ); + actor =squashfs_page_actor_init (&buffer,1 ,0 ); +if (actor ==NULL ) { + comp_opts =ERR_PTR (-ENOMEM ); + goto out; + } + + length =squashfs_read_data (sb, +sizeof (struct squashfs_super_block ),0 ,NULL , actor);if (length <0 ) { comp_opts =ERR_PTR (length);
@@ -112,6 +119,7 @@ static void *get_comp_opts(struct super_block *sb, unsigned short flags) comp_opts =squashfs_comp_opts (msblk, buffer, length); out: +kfree (actor);kfree (buffer);return comp_opts; }
fs/squashfs/decompressor.h +3 -2
@@ -27,8 +27,9 @@ struct squashfs_decompressor {void *(*init)(struct squashfs_sb_info *,void *);void *(*comp_opts)(struct squashfs_sb_info *,void *,int );void (*free)(void *); -int (*decompress)(struct squashfs_sb_info *,void *,void **, -struct buffer_head **,int ,int ,int ,int ,int ); +int (*decompress)(struct squashfs_sb_info *,void *, +struct buffer_head **,int ,int ,int , +struct squashfs_page_actor *);int id;char *name;int supported;
fs/squashfs/decompressor_multi.c +3 -4
@@ -183,15 +183,14 @@ wait: } -int squashfs_decompress (struct squashfs_sb_info *msblk, -void **buffer,struct buffer_head **bh,int b,int offset,int length, -int srclength,int pages) +int squashfs_decompress (struct squashfs_sb_info *msblk,struct buffer_head **bh, +int b,int offset,int length,struct squashfs_page_actor *output) {int res;struct squashfs_stream *stream = msblk->stream;struct decomp_stream *decomp_stream =get_decomp_stream (msblk, stream); res = msblk->decompressor->decompress (msblk, decomp_stream->stream, - buffer, bh, b, offset, length, srclength, pages); + bh, b, offset, length, output);put_decomp_stream (decomp_stream, stream);if (res <0 ) ERROR("%s decompression failed, data probably corrupt\n",
fs/squashfs/decompressor_multi_percpu.c +4 -5
@@ -74,15 +74,14 @@ void squashfs_decompressor_destroy(struct squashfs_sb_info *msblk) } } -int squashfs_decompress (struct squashfs_sb_info *msblk, -void **buffer,struct buffer_head **bh,int b,int offset,int length, -int srclength,int pages) +int squashfs_decompress (struct squashfs_sb_info *msblk,struct buffer_head **bh, +int b,int offset,int length,struct squashfs_page_actor *output) {struct squashfs_stream __percpu *percpu = (struct squashfs_stream __percpu *) msblk->stream;struct squashfs_stream *stream =get_cpu_ptr (percpu); -int res = msblk->decompressor->decompress (msblk, stream->stream, buffer, - bh, b, offset, length, srclength, pages); +int res = msblk->decompressor->decompress (msblk, stream->stream, bh, b, + offset, length, output);put_cpu_ptr (stream);if (res <0 )
fs/squashfs/decompressor_single.c +4 -5
@@ -61,16 +61,15 @@ void squashfs_decompressor_destroy(struct squashfs_sb_info *msblk) } } -int squashfs_decompress (struct squashfs_sb_info *msblk, -void **buffer,struct buffer_head **bh,int b,int offset,int length, -int srclength,int pages) +int squashfs_decompress (struct squashfs_sb_info *msblk,struct buffer_head **bh, +int b,int offset,int length,struct squashfs_page_actor *output) {int res;struct squashfs_stream *stream = msblk->stream;mutex_lock (&stream->mutex); - res = msblk->decompressor->decompress (msblk, stream->stream, buffer, - bh, b, offset, length, srclength, pages); + res = msblk->decompressor->decompress (msblk, stream->stream, bh, b, + offset, length, output);mutex_unlock (&stream->mutex);if (res <0 )
fs/squashfs/lzo_wrapper.c +18 -9
@@ -31,6 +31,7 @@#include "squashfs_fs_sb.h" #include "squashfs.h" #include "decompressor.h" +#include "page_actor.h" struct squashfs_lzo {void *input;
@@ -75,13 +76,13 @@ static void lzo_free(void *strm)static int lzo_uncompress (struct squashfs_sb_info *msblk,void *strm, -void **buffer,struct buffer_head **bh,int b,int offset,int length, -int srclength,int pages) +struct buffer_head **bh,int b,int offset,int length, +struct squashfs_page_actor *output) {struct squashfs_lzo *stream = strm; -void *buff = stream->input; +void *buff = stream->input, *data;int avail, i, bytes = length, res; -size_t out_len = srclength; +size_t out_len = output->length;for (i =0 ; i < b; i++) { avail =min (bytes, msblk->devblksize - offset);
@@ -98,12 +99,20 @@ static int lzo_uncompress(struct squashfs_sb_info *msblk, void *strm, goto failed; res = bytes = (int )out_len; -for (i =0 , buff = stream->output; bytes && i < pages; i++) { - avail =min_t (int, bytes,PAGE_CACHE_SIZE ); -memcpy (buffer[i], buff, avail); - buff += avail; - bytes -= avail; + data =squashfs_first_page (output); + buff = stream->output; +while (data) { +if (bytes <=PAGE_CACHE_SIZE ) { +memcpy (data, buff, bytes); +break ; + }else { +memcpy (data, buff,PAGE_CACHE_SIZE ); + buff +=PAGE_CACHE_SIZE ; + bytes -=PAGE_CACHE_SIZE ; + data =squashfs_next_page (output); + } } +squashfs_finish_page (output);return res;
fs/squashfs/page_actor.h new file +49 -0
49 lines not on this page · raw message
fs/squashfs/squashfs.h +4 -4
@@ -28,8 +28,8 @@ #define WARNING(s, args...) pr_warning("SQUASHFS: "s, ## args)/* block.c */ -extern int squashfs_read_data (struct super_block *,void **,u64 ,int ,u64 *, -int ,int ); +extern int squashfs_read_data (struct super_block *,u64 ,int ,u64 *, +struct squashfs_page_actor *);/* cache.c */ extern struct squashfs_cache *squashfs_cache_init (char *,int ,int );
@@ -53,8 +53,8 @@ extern void *squashfs_decompressor_setup(struct super_block *, unsigned short);/* decompressor_xxx.c */ extern void *squashfs_decompressor_create (struct squashfs_sb_info *,void *);extern void squashfs_decompressor_destroy (struct squashfs_sb_info *); -extern int squashfs_decompress (struct squashfs_sb_info *,void **, -struct buffer_head **,int ,int ,int ,int ,int ); +extern int squashfs_decompress (struct squashfs_sb_info *,struct buffer_head **, +int ,int ,int ,struct squashfs_page_actor *);extern int squashfs_max_decompressors (void );/* export.c */
fs/squashfs/squashfs_fs_sb.h +1 -0
@@ -50,6 +50,7 @@ struct squashfs_cache_entry {wait_queue_head_t wait_queue;struct squashfs_cache *cache;void **data; +struct squashfs_page_actor *actor; };struct squashfs_sb_info {
fs/squashfs/xz_wrapper.c +13 -9
@@ -32,6 +32,7 @@#include "squashfs_fs_sb.h" #include "squashfs.h" #include "decompressor.h" +#include "page_actor.h" struct squashfs_xz {struct xz_dec *state;
@@ -129,11 +130,11 @@ static void squashfs_xz_free(void *strm)static int squashfs_xz_uncompress (struct squashfs_sb_info *msblk,void *strm, -void **buffer,struct buffer_head **bh,int b,int offset,int length, -int srclength,int pages) +struct buffer_head **bh,int b,int offset,int length, +struct squashfs_page_actor *output) {enum xz_ret xz_err; -int avail, total =0 , k =0 , page =0 ; +int avail, total =0 , k =0 ;struct squashfs_xz *stream = strm;xz_dec_reset (stream->state);
@@ -141,7 +142,7 @@ static int squashfs_xz_uncompress(struct squashfs_sb_info *msblk, void *strm, stream->buf.in_size =0 ; stream->buf.out_pos =0 ; stream->buf.out_size =PAGE_CACHE_SIZE ; - stream->buf.out = buffer[page++]; + stream->buf.out =squashfs_first_page (output); do { if (stream->buf.in_pos == stream->buf.in_size && k < b) {
@@ -153,11 +154,12 @@ static int squashfs_xz_uncompress(struct squashfs_sb_info *msblk, void *strm, offset =0 ; } -if (stream->buf.out_pos == stream->buf.out_size - && page < pages) { - stream->buf.out = buffer[page++]; - stream->buf.out_pos =0 ; - total +=PAGE_CACHE_SIZE ; +if (stream->buf.out_pos == stream->buf.out_size) { + stream->buf.out =squashfs_next_page (output); +if (stream->buf.out !=NULL ) { + stream->buf.out_pos =0 ; + total +=PAGE_CACHE_SIZE ; + } } xz_err =xz_dec_run (stream->state, &stream->buf);
@@ -166,6 +168,8 @@ static int squashfs_xz_uncompress(struct squashfs_sb_info *msblk, void *strm,put_bh (bh[k++]); }while (xz_err ==XZ_OK ); +squashfs_finish_page (output); +if (xz_err !=XZ_STREAM_END || k < b) goto out;
fs/squashfs/zlib_wrapper.c +15 -9
@@ -32,6 +32,7 @@#include "squashfs_fs_sb.h" #include "squashfs.h" #include "decompressor.h" +#include "page_actor.h" static void *zlib_init (struct squashfs_sb_info *dummy,void *buff) {
@@ -62,14 +63,14 @@ static void zlib_free(void *strm)static int zlib_uncompress (struct squashfs_sb_info *msblk,void *strm, -void **buffer,struct buffer_head **bh,int b,int offset,int length, -int srclength,int pages) +struct buffer_head **bh,int b,int offset,int length, +struct squashfs_page_actor *output) { -int zlib_err, zlib_init =0 ; -int k =0 , page =0 ; +int zlib_err, zlib_init =0 , k =0 ;z_stream *stream = strm; - stream->avail_out =0 ; + stream->avail_out =PAGE_CACHE_SIZE ; + stream->next_out =squashfs_first_page (output); stream->avail_in =0 ; do {
@@ -81,15 +82,18 @@ static int zlib_uncompress(struct squashfs_sb_info *msblk, void *strm, offset =0 ; } -if (stream->avail_out ==0 && page < pages) { - stream->next_out = buffer[page++]; - stream->avail_out =PAGE_CACHE_SIZE ; +if (stream->avail_out ==0 ) { + stream->next_out =squashfs_next_page (output); +if (stream->next_out !=NULL ) + stream->avail_out =PAGE_CACHE_SIZE ; }if (!zlib_init) { zlib_err =zlib_inflateInit (stream); -if (zlib_err !=Z_OK ) +if (zlib_err !=Z_OK ) { +squashfs_finish_page (output); goto out; + } zlib_init =1 ; }
@@ -99,6 +103,8 @@ static int zlib_uncompress(struct squashfs_sb_info *msblk, void *strm,put_bh (bh[k++]); }while (zlib_err ==Z_OK ); +squashfs_finish_page (output); +if (zlib_err !=Z_STREAM_END ) goto out;
Signature · 1 line
1.8.3.2
Phillip Lougher
maintainerPATCH 5/7 Squashfs: Restructure squashfs_readpage()
In reply to Phillip LougherRestructure squashfs_readpage() splitting it into separate functions for datablocks, fragments and sparse blocks.
Move the memcpying (from squashfs cache entry) implementation of squashfs_readpage_block into file_cache.c
This allows different implementations to be supported.
--- fs/squashfs/Makefile | 2 +- fs/squashfs/file.c | 142 ++++++++++++++++++++++++----------------------- fs/squashfs/file_cache.c | 38 +++++++++++++ fs/squashfs/squashfs.h | 7 +++ 4 files changed, 118 insertions(+), 71 deletions(-) create mode 100644 fs/squashfs/file_cache.c
fs/squashfs/Makefile +1 -1
@@ -4,7 +4,7 @@ obj-$(CONFIG_SQUASHFS) += squashfs.o squashfs-y += block.o cache.o dir.o export.o file.o fragment.o id.o inode.o -squashfs-y += namei.o super.o symlink.o decompressor.o +squashfs-y += namei.o super.o symlink.o decompressor.o file_cache.o squashfs-$(CONFIG_SQUASHFS_DECOMP_SINGLE) += decompressor_single.o squashfs-$(CONFIG_SQUASHFS_DECOMP_MULTI) += decompressor_multi.o squashfs-$(CONFIG_SQUASHFS_DECOMP_MULTI_PERCPU) += decompressor_multi_percpu.o
fs/squashfs/file.c +72 -70
167 lines not on this page · raw message
fs/squashfs/file_cache.c new file +38 -0
@@ -0,0 +1,38 @@ +/* +* Copyright (c) 2013 +* Phillip Lougher <[email protected]> +* +* This work is licensed under the terms of the GNU GPL, version 2. See +* the COPYING file in the top-level directory. +*/ + +#include <linux/fs.h> +#include <linux/vfs.h> +#include <linux/kernel.h> +#include <linux/slab.h> +#include <linux/string.h> +#include <linux/pagemap.h> +#include <linux/mutex.h> + +#include "squashfs_fs.h" +#include "squashfs_fs_sb.h" +#include "squashfs_fs_i.h" +#include "squashfs.h" + +/* Read separately compressed datablock and memcopy into page cache */ +int squashfs_readpage_block (struct page *page,u64 block,int bsize) +{ +struct inode *i = page->mapping->host; +struct squashfs_cache_entry *buffer =squashfs_get_datablock (i->i_sb, + block, bsize); +int res = buffer->error; + +if (res) +ERROR ("Unable to read page, block %llx, size %x\n" , block, + bsize); +else +squashfs_copy_cache (page, buffer, buffer->length,0 ); + +squashfs_cache_put (buffer); +return res; +}
fs/squashfs/squashfs.h +7 -0
@@ -66,6 +66,13 @@ extern int squashfs_frag_lookup(struct super_block *, unsigned int, u64 *);extern __le64 *squashfs_read_fragment_index_table (struct super_block *,u64 ,u64 ,unsigned int ); +/* file.c */ +void squashfs_copy_cache (struct page *,struct squashfs_cache_entry *,int , +int ); + +/* file_xxx.c */ +extern int squashfs_readpage_block (struct page *,u64 ,int ); +/* id.c */ extern int squashfs_get_id (struct super_block *,unsigned int ,unsigned int *); extern __le64 *squashfs_read_id_index_table(struct super_block *, u64, u64,
Signature · 1 line
1.8.3.2
Phillip Lougher
maintainerPATCH 6/7 Squashfs: Directly decompress into the page cache for file data
In reply to Phillip LougherThis introduces an implementation of squashfs_readpage_block() that directly decompresses into the page cache.
This uses the previously added page handler abstraction to push down the necessary kmap_atomic/kunmap_atomic operations on the page cache buffers into the decompressors. This enables direct copying into the page cache without using the slow kmap/kunmap calls.
The code detects when multiple threads are racing in squashfs_readpage() to decompress the same block, and avoids this regression by falling back to using an intermediate buffer.
This patch enhances the performance of Squashfs significantly when multiple processes are accessing the filesystem simultaneously because it not only reduces memcopying, but it more importantly eliminates the lock contention on the intermediate buffer.
Using single-thread decompression.
dd if=file1 of=/dev/null bs=4096 &
dd if=file2 of=/dev/null bs=4096 &
dd if=file3 of=/dev/null bs=4096 &
dd if=file4 of=/dev/null bs=4096Before:
629145600 bytes (629 MB) copied, 45.8046 s, 13.7 MB/s
After:
629145600 bytes (629 MB) copied, 9.29414 s, 67.7 MB/s
--- fs/squashfs/Kconfig | 28 ++++++++ fs/squashfs/Makefile | 4 +- fs/squashfs/file_direct.c | 173 ++++++++++++++++++++++++++++++++++++++++++++++ fs/squashfs/page_actor.c | 100 +++++++++++++++++++++++++++ fs/squashfs/page_actor.h | 32 +++++++++ 5 files changed, 336 insertions(+), 1 deletion(-) create mode 100644 fs/squashfs/file_direct.c create mode 100644 fs/squashfs/page_actor.c
fs/squashfs/Kconfig +28 -0
@@ -26,6 +26,34 @@ config SQUASHFS If unsure, say N. choice + prompt "File decompression options" + depends on SQUASHFS + help + Squashfs now supports two options for decompressing file + data. Traditionally Squashfs has decompressed into an + intermediate buffer and then memcopied it into the page cache. + Squashfs now supports the ability to decompress directly into + the page cache. + + If unsure, select "Decompress file data into an intermediate buffer" + +config SQUASHFS_FILE_CACHE + bool "Decompress file data into an intermediate buffer" + help + Decompress file data into an intermediate buffer and then + memcopy it into the page cache. + +config SQUASHFS_FILE_DIRECT + bool "Decompress files directly into the page cache" + help + Directly decompress file data into the page cache. + Doing so can significantly improve performance because + it eliminates a memcpy and it also removes the lock contention + on the single buffer. + +endchoice + +choice prompt "Decompressor parallelisation options" depends on SQUASHFS help
fs/squashfs/Makefile +3 -1
@@ -4,7 +4,9 @@ obj-$(CONFIG_SQUASHFS) += squashfs.o squashfs-y += block.o cache.o dir.o export.o file.o fragment.o id.o inode.o -squashfs-y += namei.o super.o symlink.o decompressor.o file_cache.o +squashfs-y += namei.o super.o symlink.o decompressor.o +squashfs-$(CONFIG_SQUASHFS_FILE_CACHE) += file_cache.o +squashfs-$(CONFIG_SQUASHFS_FILE_DIRECT) += file_direct.o page_actor.o squashfs-$(CONFIG_SQUASHFS_DECOMP_SINGLE) += decompressor_single.o squashfs-$(CONFIG_SQUASHFS_DECOMP_MULTI) += decompressor_multi.o squashfs-$(CONFIG_SQUASHFS_DECOMP_MULTI_PERCPU) += decompressor_multi_percpu.o
fs/squashfs/file_direct.c new file +173 -0
173 lines not on this page · raw message
fs/squashfs/page_actor.c new file +100 -0
100 lines not on this page · raw message
fs/squashfs/page_actor.h +32 -0
@@ -8,6 +8,7 @@* the COPYING file in the top-level directory. */ +#ifndef CONFIG_SQUASHFS_FILE_DIRECT struct squashfs_page_actor { void **page; int pages;
@@ -46,4 +47,35 @@ static inline void squashfs_finish_page(struct squashfs_page_actor *actor) {/* empty */ } +#else +struct squashfs_page_actor { +union { +void **buffer; +struct page **page; + }; +void *pageaddr; +void *(*squashfs_first_page)(struct squashfs_page_actor *); +void *(*squashfs_next_page)(struct squashfs_page_actor *); +void (*squashfs_finish_page)(struct squashfs_page_actor *); +int pages; +int length; +int next_page; +}; + +extern struct squashfs_page_actor *squashfs_page_actor_init (void **,int ,int ); +extern struct squashfs_page_actor *squashfs_page_actor_init_special (struct page + **,int ,int ); +static inline void *squashfs_first_page (struct squashfs_page_actor *actor) +{ +return actor->squashfs_first_page (actor); +} +static inline void *squashfs_next_page (struct squashfs_page_actor *actor) +{ +return actor->squashfs_next_page (actor); +} +static inline void squashfs_finish_page (struct squashfs_page_actor *actor) +{ + actor->squashfs_finish_page (actor); +} +#endif #endif
Signature · 1 line
1.8.3.2
Re: [PATCH 6/7] Squashfs: Directly decompress into the page cache for file data
Hello, Phillip.
one remark below:
Quoted 2 lines
+static int squashfs_read_cache(struct page
*target_page, u64 block, int
bsize,
fs/squashfs/file_direct.c line 149
Quoted 13 lines
fs/squashfs/file_direct.c new file +4
-0+ int pages, struct page **page) +{ + struct inode *i = target_page->mapping->host; + struct squashfs_cache_entry *buffer = squashfs_get_datablock(i-
i_sb,fs/squashfs/file_direct.c new file +8
-0+ block, bsize); +int bytes = buffer->length, res = buffer->error, n, offset =0 ; +void *pageaddr; + +if (res) { +ERROR ("Unable to read page, block %llx, size %x\n" , block, + bsize); + goto out;
have you forgotten to unlock the pages on error path?
in case of error squashfs_readpage will unlock only target page.
fs/squashfs/file_direct.c
Quoted 4 lines
fs/squashfs/file_direct.c new file +4
-0+ } + + for (n = 0; n < pages && bytes > 0; n++, + bytes -= PAGE_CACHE_SIZE, offset +=
PAGE_CACHE_SIZE) {
Quoted 1 line
fs/squashfs/file_direct.c new file +1
-0+int avail =min_t (int, bytes,PAGE_CACHE_SIZE );
Re: [PATCH 6/7] Squashfs: Directly decompress into the page cache for file data
Quoted 24 lines · Roman Peniaev
Hello, Phillip.
one remark below:
Quoted 2 lines
+static int squashfs_read_cache(struct page
*target_page, u64 block, intbsize,
fs/squashfs/file_direct.c line 149
Quoted 13 lines
fs/squashfs/file_direct.c new file +4
-0+ int pages, struct page **page) +{ + struct inode *i = target_page->mapping->host; + struct squashfs_cache_entry *buffer = squashfs_get_datablock(i-
i_sb,fs/squashfs/file_direct.c new file +8
-0+ block, bsize); +int bytes = buffer->length, res = buffer->error, n, offset =0 ; +void *pageaddr; + +if (res) { +ERROR ("Unable to read page, block %llx, size %x\n" , block, + bsize); + goto out;have you forgotten to unlock the pages on error path?
Quoted 1 line
in case of error
squashfs_readpagewill unlock only target page.
Yup, a fix for -rc1 is on its way.
This error path failed to trigger in my stress tests because it is pretty hard to hit, because it relies on decompress failure at the same time as we've raced against another process also grabbing the pages.
Phillip
Quoted 16 lines
fs/squashfs/file_direct.c
Quoted 6 lines
fs/squashfs/file_direct.c new file +4
-0+ } + + for (n = 0; n < pages && bytes > 0; n++, + bytes -= PAGE_CACHE_SIZE, offset +=
PAGE_CACHE_SIZE) {fs/squashfs/file_direct.c line 154
Quoted 1 line
fs/squashfs/file_direct.c new file +1
-0+int avail =min_t (int, bytes,PAGE_CACHE_SIZE );--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
Phillip Lougher
maintainerPATCH 7/7 Squashfs: Check stream is not NULL in decompressor_multi.c
In reply to Phillip LougherFix static checker complaint that stream is not checked in squashfs_decompressor_destroy().
--- fs/squashfs/decompressor_multi.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-)
fs/squashfs/decompressor_multi.c +3 -4
@@ -119,11 +119,10 @@ void squashfs_decompressor_destroy(struct squashfs_sb_info *msblk)kfree (decomp_strm); stream->avail_decomp--; } +WARN_ON (stream->avail_decomp); +kfree (stream->comp_opts); +kfree (stream); } - -WARN_ON (stream->avail_decomp); -kfree (stream->comp_opts); -kfree (stream); }
Signature · 1 line
1.8.3.2