Re: [PATCH v3 6/6] btrfs: Use larger zlib buffer for s390 hardware compression

From: Zaslonko Mikhail
Date: Tue Jan 07 2020 - 05:31:08 EST


Hello,

On 06.01.2020 17:40, Josef Bacik wrote:
> On 1/3/20 5:33 PM, Mikhail Zaslonko wrote:
>> In order to benefit from s390 zlib hardware compression support,
>> increase the btrfs zlib workspace buffer size from 1 to 4 pages (if
>> s390 zlib hardware support is enabled on the machine). This brings up
>> to 60% better performance in hardware on s390 compared to the PAGE_SIZE
>> buffer and much more compared to the software zlib processing in btrfs.
>> In case of memory pressure fall back to a single page buffer during
>> workspace allocation.
>>
>> Signed-off-by: Mikhail Zaslonko <zaslonko@xxxxxxxxxxxxx>
>> ---
>> Â fs/btrfs/compression.c |ÂÂ 2 +-
>> Â fs/btrfs/zlib.cÂÂÂÂÂÂÂ | 128 ++++++++++++++++++++++++++++++-----------
>> Â 2 files changed, 94 insertions(+), 36 deletions(-)
>>
>> diff --git a/fs/btrfs/compression.c b/fs/btrfs/compression.c
>> index ee834ef7beb4..6bd0e75a822c 100644
>> --- a/fs/btrfs/compression.c
>> +++ b/fs/btrfs/compression.c
>> @@ -1285,7 +1285,7 @@ int btrfs_decompress_buf2page(const char *buf, unsigned long buf_start,
>> ÂÂÂÂÂ /* copy bytes from the working buffer into the pages */
>> ÂÂÂÂÂ while (working_bytes > 0) {
>> ÂÂÂÂÂÂÂÂÂ bytes = min_t(unsigned long, bvec.bv_len,
>> -ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ PAGE_SIZE - buf_offset);
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ PAGE_SIZE - (buf_offset % PAGE_SIZE));
>> ÂÂÂÂÂÂÂÂÂ bytes = min(bytes, working_bytes);
>> Â ÂÂÂÂÂÂÂÂÂ kaddr = kmap_atomic(bvec.bv_page);
>> diff --git a/fs/btrfs/zlib.c b/fs/btrfs/zlib.c
>> index a6c90a003c12..159486801631 100644
>> --- a/fs/btrfs/zlib.c
>> +++ b/fs/btrfs/zlib.c
>> @@ -20,9 +20,12 @@
>> Â #include <linux/refcount.h>
>> Â #include "compression.h"
>> Â +#define ZLIB_DFLTCC_BUF_SIZEÂÂÂ (4 * PAGE_SIZE)
>> +
>> Â struct workspace {
>> ÂÂÂÂÂ z_stream strm;
>> ÂÂÂÂÂ char *buf;
>> +ÂÂÂ unsigned long buf_size;
>> ÂÂÂÂÂ struct list_head list;
>> ÂÂÂÂÂ int level;
>> Â };
>> @@ -61,7 +64,17 @@ struct list_head *zlib_alloc_workspace(unsigned int level)
>> ÂÂÂÂÂÂÂÂÂÂÂÂÂ zlib_inflate_workspacesize());
>> ÂÂÂÂÂ workspace->strm.workspace = kvmalloc(workspacesize, GFP_KERNEL);
>> ÂÂÂÂÂ workspace->level = level;
>> -ÂÂÂ workspace->buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
>> +ÂÂÂ workspace->buf = NULL;
>> +ÂÂÂ if (zlib_deflate_dfltcc_enabled()) {
>> +ÂÂÂÂÂÂÂ workspace->buf = kmalloc(ZLIB_DFLTCC_BUF_SIZE,
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ __GFP_NOMEMALLOC | __GFP_NORETRY |
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ __GFP_NOWARN | GFP_NOIO);
>> +ÂÂÂÂÂÂÂ workspace->buf_size = ZLIB_DFLTCC_BUF_SIZE;
>> +ÂÂÂ }
>> +ÂÂÂ if (!workspace->buf) {
>> +ÂÂÂÂÂÂÂ workspace->buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
>> +ÂÂÂÂÂÂÂ workspace->buf_size = PAGE_SIZE;
>> +ÂÂÂ }
>> ÂÂÂÂÂ if (!workspace->strm.workspace || !workspace->buf)
>> ÂÂÂÂÂÂÂÂÂ goto fail;
>> Â @@ -78,6 +91,7 @@ int zlib_compress_pages(struct list_head *ws, struct address_space *mapping,
>> ÂÂÂÂÂÂÂÂÂ unsigned long *total_in, unsigned long *total_out)
>> Â {
>> ÂÂÂÂÂ struct workspace *workspace = list_entry(ws, struct workspace, list);
>> +ÂÂÂ int i;
>> ÂÂÂÂÂ int ret;
>> ÂÂÂÂÂ char *data_in;
>> ÂÂÂÂÂ char *cpage_out;
>> @@ -85,6 +99,7 @@ int zlib_compress_pages(struct list_head *ws, struct address_space *mapping,
>> ÂÂÂÂÂ struct page *in_page = NULL;
>> ÂÂÂÂÂ struct page *out_page = NULL;
>> ÂÂÂÂÂ unsigned long bytes_left;
>> +ÂÂÂ unsigned long in_buf_pages;
>> ÂÂÂÂÂ unsigned long len = *total_out;
>> ÂÂÂÂÂ unsigned long nr_dest_pages = *out_pages;
>> ÂÂÂÂÂ const unsigned long max_out = nr_dest_pages * PAGE_SIZE;
>> @@ -102,9 +117,6 @@ int zlib_compress_pages(struct list_head *ws, struct address_space *mapping,
>> ÂÂÂÂÂ workspace->strm.total_in = 0;
>> ÂÂÂÂÂ workspace->strm.total_out = 0;
>> Â -ÂÂÂ in_page = find_get_page(mapping, start >> PAGE_SHIFT);
>> -ÂÂÂ data_in = kmap(in_page);
>> -
>> ÂÂÂÂÂ out_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
>> ÂÂÂÂÂ if (out_page == NULL) {
>> ÂÂÂÂÂÂÂÂÂ ret = -ENOMEM;
>> @@ -114,12 +126,48 @@ int zlib_compress_pages(struct list_head *ws, struct address_space *mapping,
>> ÂÂÂÂÂ pages[0] = out_page;
>> ÂÂÂÂÂ nr_pages = 1;
>> Â -ÂÂÂ workspace->strm.next_in = data_in;
>> +ÂÂÂ workspace->strm.next_in = workspace->buf;
>> +ÂÂÂ workspace->strm.avail_in = 0;
>> ÂÂÂÂÂ workspace->strm.next_out = cpage_out;
>> ÂÂÂÂÂ workspace->strm.avail_out = PAGE_SIZE;
>> -ÂÂÂ workspace->strm.avail_in = min(len, PAGE_SIZE);
>> Â ÂÂÂÂÂ while (workspace->strm.total_in < len) {
>> +ÂÂÂÂÂÂÂ /* get next input pages and copy the contents to
>> +ÂÂÂÂÂÂÂÂ * the workspace buffer if required
>> +ÂÂÂÂÂÂÂÂ */
>> +ÂÂÂÂÂÂÂ if (workspace->strm.avail_in == 0) {
>> +ÂÂÂÂÂÂÂÂÂÂÂ bytes_left = len - workspace->strm.total_in;
>> +ÂÂÂÂÂÂÂÂÂÂÂ in_buf_pages = min(DIV_ROUND_UP(bytes_left, PAGE_SIZE),
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ workspace->buf_size / PAGE_SIZE);
>> +ÂÂÂÂÂÂÂÂÂÂÂ if (in_buf_pages > 1) {
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ for (i = 0; i < in_buf_pages; i++) {
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ if (in_page) {
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ kunmap(in_page);
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ put_page(in_page);
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ }
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ in_page = find_get_page(mapping,
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ start >> PAGE_SHIFT);
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ data_in = kmap(in_page);
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ memcpy(workspace->buf + i*PAGE_SIZE,
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ data_in, PAGE_SIZE);
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ start += PAGE_SIZE;
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ }
>
> Is there a reason to leave the last in_page mapped here? I realize we'll clean it up further down, but since we're copying everything into the buf anyway why not just map->copy->unmap for everything?

The idea was not to copy the last data chunk if it fits in a single page, thus using the same code flow as for a PAGE_SIZE buffer (the ELSE branch below).

>
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ workspace->strm.next_in = workspace->buf;
>> +ÂÂÂÂÂÂÂÂÂÂÂ } else {
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ if (in_page) {
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ kunmap(in_page);
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ put_page(in_page);
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ }
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ in_page = find_get_page(mapping,
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ start >> PAGE_SHIFT);
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ data_in = kmap(in_page);
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ start += PAGE_SIZE;
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ workspace->strm.next_in = data_in;
>> +ÂÂÂÂÂÂÂÂÂÂÂ }
>> +ÂÂÂÂÂÂÂÂÂÂÂ workspace->strm.avail_in = min(bytes_left,
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ workspace->buf_size);
>> +ÂÂÂÂÂÂÂ }
>> +
>> ÂÂÂÂÂÂÂÂÂ ret = zlib_deflate(&workspace->strm, Z_SYNC_FLUSH);
>> ÂÂÂÂÂÂÂÂÂ if (ret != Z_OK) {
>> ÂÂÂÂÂÂÂÂÂÂÂÂÂ pr_debug("BTRFS: deflate in loop returned %d\n",
>> @@ -136,6 +184,7 @@ int zlib_compress_pages(struct list_head *ws, struct address_space *mapping,
>> ÂÂÂÂÂÂÂÂÂÂÂÂÂ ret = -E2BIG;
>> ÂÂÂÂÂÂÂÂÂÂÂÂÂ goto out;
>> ÂÂÂÂÂÂÂÂÂ }
>> +
>
> Extra newline. Thanks,
>
> Josef