Skip to contentSkip to conversation

S390 hardware support for kernel zlib

PATCH In review Mikhail Zaslonko 5 participants 21 messages Jan 3, 2020 – Jan 15, 2020

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

v3 cover S390 hardware support for kernel zlib

With IBM z15 mainframe the new DFLTCC instruction is available. It implements deflate algorithm in hardware (Nest Acceleration Unit - NXU) with estimated compression and decompression performance orders of magnitude faster than the current zlib.

This patch-set adds s390 hardware compression support to kernel zlib. The code is based on the userspace zlib implementation:
https://github.com/madler/zlib/pull/410
The coding style is also preserved for future maintainability. There is only limited set of userspace zlib functions represented in kernel. Apart from that, all the memory allocation should be performed in advance. Thus, the workarea structures are extended with the parameter lists required for the DEFLATE CONVENTION CALL instruction. Since kernel zlib itself does not support gzip headers, only Adler-32 checksum is processed (also can be produced by DFLTCC facility). Like it was implemented for userspace, kernel zlib will compress in hardware on level 1, and in software on all other levels. Decompression will always happen in hardware (when enabled).
Two DFLTCC compression calls produce the same results only when they both are made on machines of the same generation, and when the respective buffers have the same offset relative to the start of the page. Therefore care should be taken when using hardware compression when reproducible results are desired. However it does always produce the standard conform output which can be inflated anyway.
The new kernel command line parameter 'dfltcc' is introduced to configure s390 zlib hardware support:
Format: { on | off | def_only | inf_only | always }
on: s390 zlib hardware support for compression on
level 1 and decompression (default)
off: No s390 zlib hardware support
def_only: s390 zlib hardware support for deflate
only (compression on level 1)
inf_only: s390 zlib hardware support for inflate
only (decompression)
always: Same as 'on' but ignores the selected compression
level always using hardware support (used for debugging)

The main purpose of the integration of the NXU support into the kernel zlib is the use of hardware deflate in btrfs filesystem with on-the-fly compression enabled. Apart from that, hardware support can also be used during boot for decompressing the kernel or the ramdisk image

With the patch for btrfs expanding zlib buffer from 1 to 4 pages (patch 6) the following performance results have been achieved using the ramdisk with btrfs. These are relative numbers based on throughput rate and compression ratio for zlib level 1:

  Input data              Deflate rate   Inflate rate   Compression ratio
                          NXU/Software   NXU/Software   NXU/Software
  stream of zeroes        1.46           1.02           1.00
  random ASCII data       10.44          3.00           0.96
  ASCII text (dickens)    6,21           3.33           0.94
  binary data (vmlinux)   8,37           3.90           1.02

This means that s390 hardware deflate can provide up to 10 times faster compression (on level 1) and up to 4 times faster decompression (refers to all compression levels) for btrfs zlib.

Disclaimer: Performance results are based on IBM internal tests using DD command-line utility on btrfs on a Fedora 30 based internal driver in native LPAR on a z15 system. Results may vary based on individual workload, configuration and software levels.

Changelog:
v2 -> v3:
- In btrfs zlib_compress_pages() do not copy input pages to the workspace buffer if no s390 hardware compression is enabled or the remaining input data fits in one page.
- Leave just a single Kconfig option for s390 zlib hardware support (CONFIG_ZLIB_DFLTCC)
- Fix the issues reported by kbuild test robot.
v1 -> v2:
- Added new external zlib function to check if s390 Deflate-Conversion facility is installed and enabled (see patch 5).
- The larger buffer for btrfs zlib workspace is allocated only if s390 hardware compression is enabled. In case of failure to allocate 4-page buffer, we fall back to a PAGE_SIZE buffer, as proposed by Josef Bacik (see patch 6).

Mikhail Zaslonko (6):
lib/zlib: Add s390 hardware support for kernel zlib_deflate s390/boot: Rename HEAP_SIZE due to name collision lib/zlib: Add s390 hardware support for kernel zlib_inflate s390/boot: Add dfltcc= kernel command line parameter lib/zlib: Add zlib_deflate_dfltcc_enabled() function
btrfs: Use larger zlib buffer for s390 hardware compression

 .../admin-guide/kernel-parameters.txt         |  12 +
 arch/s390/boot/compressed/decompressor.c      |   8 +-
 arch/s390/boot/ipl_parm.c                     |  14 +
 arch/s390/include/asm/setup.h                 |   7 +
 arch/s390/kernel/setup.c                      |   2 +
 fs/btrfs/compression.c                        |   2 +-
 fs/btrfs/zlib.c                               | 128 +++++---
 include/linux/zlib.h                          |   6 +
 lib/Kconfig                                   |   7 +
 lib/Makefile                                  |   1 +
 lib/decompress_inflate.c                      |  13 +
 lib/zlib_deflate/deflate.c                    |  85 +++---
 lib/zlib_deflate/deflate_syms.c               |   1 +
 lib/zlib_deflate/deftree.c                    |  54 ----
 lib/zlib_deflate/defutil.h                    | 134 ++++++++-
 lib/zlib_dfltcc/Makefile                      |  11 +
 lib/zlib_dfltcc/dfltcc.c                      |  55 ++++
 lib/zlib_dfltcc/dfltcc.h                      | 155 ++++++++++
 lib/zlib_dfltcc/dfltcc_deflate.c              | 280 ++++++++++++++++++
 lib/zlib_dfltcc/dfltcc_inflate.c              | 149 ++++++++++
 lib/zlib_dfltcc/dfltcc_syms.c                 |  17 ++
 lib/zlib_dfltcc/dfltcc_util.h                 | 103 +++++++
 lib/zlib_inflate/inflate.c                    |  32 +-
 lib/zlib_inflate/inflate.h                    |   8 +
 lib/zlib_inflate/infutil.h                    |  18 +-
 25 files changed, 1150 insertions(+), 152 deletions(-)
 create mode 100644 lib/zlib_dfltcc/Makefile
 create mode 100644 lib/zlib_dfltcc/dfltcc.c
 create mode 100644 lib/zlib_dfltcc/dfltcc.h
 create mode 100644 lib/zlib_dfltcc/dfltcc_deflate.c
 create mode 100644 lib/zlib_dfltcc/dfltcc_inflate.c
 create mode 100644 lib/zlib_dfltcc/dfltcc_syms.c
 create mode 100644 lib/zlib_dfltcc/dfltcc_util.h
Signature · 1 line
2.17.1
#1 lore raw

PATCH v3 1/6 lib/zlib: Add s390 hardware support for kernel zlib_deflate

Create zlib_dfltcc library with the s390 DEFLATE CONVERSION CALL implementation and related compression functions.
Update zlib_deflate functions with the hooks for s390 hardware support and adjust workspace structures with extra parameter lists required for hardware deflate.

---
 lib/Kconfig                      |   7 +
 lib/Makefile                     |   1 +
 lib/zlib_deflate/deflate.c       |  79 ++++-----
 lib/zlib_deflate/deftree.c       |  54 ------
 lib/zlib_deflate/defutil.h       | 134 +++++++++++++--
 lib/zlib_dfltcc/Makefile         |  11 ++
 lib/zlib_dfltcc/dfltcc.c         |  52 ++++++
 lib/zlib_dfltcc/dfltcc.h         | 115 +++++++++++++
 lib/zlib_dfltcc/dfltcc_deflate.c | 274 +++++++++++++++++++++++++++++++
 lib/zlib_dfltcc/dfltcc_syms.c    |  17 ++
 lib/zlib_dfltcc/dfltcc_util.h    | 110 +++++++++++++
 11 files changed, 752 insertions(+), 102 deletions(-)
 create mode 100644 lib/zlib_dfltcc/Makefile
 create mode 100644 lib/zlib_dfltcc/dfltcc.c
 create mode 100644 lib/zlib_dfltcc/dfltcc.h
 create mode 100644 lib/zlib_dfltcc/dfltcc_deflate.c
 create mode 100644 lib/zlib_dfltcc/dfltcc_syms.c
 create mode 100644 lib/zlib_dfltcc/dfltcc_util.h
lib/Kconfig +7 -0
index 6e790dc55c5b..bc7e56370129 100644
@@ -278,6 +278,13 @@ config ZLIB_DEFLATE
 	tristate
 	select BITREVERSE
 
+config ZLIB_DFLTCC
+	def_bool y
+	depends on S390
+	prompt "Enable s390x DEFLATE CONVERSION CALL support for kernel zlib"
+	help
+	 Enable s390x hardware support for zlib in the kernel.
+
 config LZO_COMPRESS
 	tristate
 
lib/Makefile +1 -0
index 93217d44237f..38ea84cbea97 100644
@@ -140,6 +140,7 @@ obj-$(CONFIG_842_COMPRESS) += 842/
 obj-$(CONFIG_842_DECOMPRESS) += 842/
 obj-$(CONFIG_ZLIB_INFLATE) += zlib_inflate/
 obj-$(CONFIG_ZLIB_DEFLATE) += zlib_deflate/
+obj-$(CONFIG_ZLIB_DFLTCC) += zlib_dfltcc/
 obj-$(CONFIG_REED_SOLOMON) += reed_solomon/
 obj-$(CONFIG_BCH) += bch.o
 obj-$(CONFIG_LZO_COMPRESS) += lzo/
lib/zlib_deflate/deflate.c +41 -38

132 lines not on this page · raw message

lib/zlib_deflate/deftree.c +0 -54

72 lines not on this page · raw message

lib/zlib_deflate/defutil.h +124 -10

155 lines not on this page · raw message

lib/zlib_dfltcc/Makefile new file +11 -0
new file mode 100644
index 000000000000..863d3b37e09d
@@ -0,0 +1,11 @@
+# SPDX-License-Identifier: GPL-2.0-only
+#
+# This is a modified version of zlib, which does all memory
+# allocation ahead of time.
+#
+# This is the code for s390 zlib hardware support.
+#
+
+obj-$(CONFIG_ZLIB_DFLTCC) += zlib_dfltcc.o
+
+zlib_dfltcc-objs := dfltcc.o dfltcc_deflate.o dfltcc_syms.o
lib/zlib_dfltcc/dfltcc.c new file +52 -0

52 lines not on this page · raw message

lib/zlib_dfltcc/dfltcc.h new file +115 -0

115 lines not on this page · raw message

lib/zlib_dfltcc/dfltcc_deflate.c new file +274 -0

274 lines not on this page · raw message

lib/zlib_dfltcc/dfltcc_syms.c new file +17 -0
new file mode 100644
index 000000000000..6f23481804c1
@@ -0,0 +1,17 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * linux/lib/zlib_dfltcc/dfltcc_syms.c
+ *
+ * Exported symbols for the s390 zlib dfltcc support.
+ *
+ */
+
+#include <linux/init.h>
+#include <linux/module.h>
+#include <linux/zlib.h>
+#include "dfltcc.h"
+
+EXPORT_SYMBOL(dfltcc_can_deflate);
+EXPORT_SYMBOL(dfltcc_deflate);
+EXPORT_SYMBOL(dfltcc_reset);
+MODULE_LICENSE("GPL");
lib/zlib_dfltcc/dfltcc_util.h new file +110 -0

110 lines not on this page · raw message

Signature · 1 line
2.17.1
#2 lore raw

PATCH v3 2/6 s390/boot: Rename HEAP_SIZE due to name collision

In reply to Mikhail Zaslonko

Change the conflicting macro name in preparation for zlib_inflate hardware support.

---
 arch/s390/boot/compressed/decompressor.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
arch/s390/boot/compressed/decompressor.c +4 -4
index 45046630c56a..368fd372c875 100644
@@ -30,13 +30,13 @@ extern unsigned char _compressed_start[];
 extern unsigned char _compressed_end[];
 
 #ifdef CONFIG_HAVE_KERNEL_BZIP2
-#define HEAP_SIZE	0x400000
+#define BOOT_HEAP_SIZE	0x400000
 #else
-#define HEAP_SIZE	0x10000
+#define BOOT_HEAP_SIZE	0x10000
 #endif
 
 static unsigned long free_mem_ptr = (unsigned long) _end;
-static unsigned long free_mem_end_ptr = (unsigned long) _end + HEAP_SIZE;
+static unsigned long free_mem_end_ptr = (unsigned long) _end + BOOT_HEAP_SIZE;
 
 #ifdef CONFIG_KERNEL_GZIP
 #include "../../../../lib/decompress_inflate.c"
@@ -62,7 +62,7 @@ static unsigned long free_mem_end_ptr = (unsigned long) _end + HEAP_SIZE;
 #include "../../../../lib/decompress_unxz.c"
 #endif
 
-#define decompress_offset ALIGN((unsigned long)_end + HEAP_SIZE, PAGE_SIZE)
+#define decompress_offset ALIGN((unsigned long)_end + BOOT_HEAP_SIZE, PAGE_SIZE)
 
 unsigned long mem_safe_offset(void)
 {
Signature · 1 line
2.17.1
Reviewed-by on v3 2/6
#3 lore raw

Re: [PATCH v3 2/6] s390/boot: Rename HEAP_SIZE due to name collision

Quoted 4 lines · Mikhail Zaslonko

Change the conflicting macro name in preparation for zlib_inflate hardware support.

Quoted 35 lines
---
 arch/s390/boot/compressed/decompressor.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
arch/s390/boot/compressed/decompressor.c +4 -4
index 45046630c56a..368fd372c875 100644
@@ -30,13 +30,13 @@ extern unsigned char _compressed_start[];
 extern unsigned char _compressed_end[];
 
 #ifdef CONFIG_HAVE_KERNEL_BZIP2
-#define HEAP_SIZE	0x400000
+#define BOOT_HEAP_SIZE	0x400000
 #else
-#define HEAP_SIZE	0x10000
+#define BOOT_HEAP_SIZE	0x10000
 #endif
 
 static unsigned long free_mem_ptr = (unsigned long) _end;
-static unsigned long free_mem_end_ptr = (unsigned long) _end + HEAP_SIZE;
+static unsigned long free_mem_end_ptr = (unsigned long) _end + BOOT_HEAP_SIZE;
 
 #ifdef CONFIG_KERNEL_GZIP
 #include "../../../../lib/decompress_inflate.c"
@@ -62,7 +62,7 @@ static unsigned long free_mem_end_ptr = (unsigned long) _end + HEAP_SIZE;
 #include "../../../../lib/decompress_unxz.c"
 #endif
 
-#define decompress_offset ALIGN((unsigned long)_end + HEAP_SIZE, PAGE_SIZE)
+#define decompress_offset ALIGN((unsigned long)_end + BOOT_HEAP_SIZE, PAGE_SIZE)
 
 unsigned long mem_safe_offset(void)
 {
#4 lore raw

PATCH v3 3/6 lib/zlib: Add s390 hardware support for kernel zlib_inflate

In reply to Mikhail Zaslonko

Add decompression functions to zlib_dfltcc library.
Update zlib_inflate functions with the hooks for s390 hardware support and adjust workspace structures with extra parameter lists required for hardware inflate decompression.

---
 lib/decompress_inflate.c         |  13 +++
 lib/zlib_dfltcc/Makefile         |   2 +-
 lib/zlib_dfltcc/dfltcc.h         |  28 ++++++
 lib/zlib_dfltcc/dfltcc_inflate.c | 143 +++++++++++++++++++++++++++++++
 lib/zlib_inflate/inflate.c       |  32 +++++--
 lib/zlib_inflate/inflate.h       |   8 ++
 lib/zlib_inflate/infutil.h       |  18 +++-
 7 files changed, 233 insertions(+), 11 deletions(-)
 create mode 100644 lib/zlib_dfltcc/dfltcc_inflate.c
lib/decompress_inflate.c +13 -0
index 63b4b7eee138..6130c42b8e59 100644
@@ -10,6 +10,10 @@
 #include "zlib_inflate/inftrees.c"
 #include "zlib_inflate/inffast.c"
 #include "zlib_inflate/inflate.c"
+#ifdef CONFIG_ZLIB_DFLTCC
+#include "zlib_dfltcc/dfltcc.c"
+#include "zlib_dfltcc/dfltcc_inflate.c"
+#endif
 
 #else /* STATIC */
 /* initramfs et al: linked */
@@ -76,7 +80,12 @@ STATIC int INIT __gunzip(unsigned char *buf, long len,
 	}
 
 	strm->workspace = malloc(flush ? zlib_inflate_workspacesize() :
+#ifdef CONFIG_ZLIB_DFLTCC
+	/* Always allocate the full workspace for DFLTCC */
+				 zlib_inflate_workspacesize());
+#else
 				 sizeof(struct inflate_state));
+#endif
 	if (strm->workspace == NULL) {
 		error("Out of memory while allocating workspace");
 		goto gunzip_nomem4;
@@ -123,10 +132,14 @@ STATIC int INIT __gunzip(unsigned char *buf, long len,
 
 	rc = zlib_inflateInit2(strm, -MAX_WBITS);
 
+#ifdef CONFIG_ZLIB_DFLTCC
+	/* Always keep the window for DFLTCC */
+#else
 	if (!flush) {
 		WS(strm)->inflate_state.wsize = 0;
 		WS(strm)->inflate_state.window = NULL;
 	}
+#endif
 
 	while (rc == Z_OK) {
 		if (strm->avail_in == 0) {
lib/zlib_dfltcc/Makefile +1 -1
index 863d3b37e09d..8e4d5afbbb10 100644
@@ -8,4 +8,4 @@
 
 obj-$(CONFIG_ZLIB_DFLTCC) += zlib_dfltcc.o
 
-zlib_dfltcc-objs := dfltcc.o dfltcc_deflate.o dfltcc_syms.o
+zlib_dfltcc-objs := dfltcc.o dfltcc_deflate.o dfltcc_inflate.o dfltcc_syms.o
lib/zlib_dfltcc/dfltcc.h +28 -0
index 18fed7a444bc..4782c92bb2ff 100644
@@ -104,6 +104,14 @@ int dfltcc_deflate(z_streamp strm,
                    int flush,
                    block_state *result);
 void dfltcc_reset(z_streamp strm, uInt size);
+int dfltcc_can_inflate(z_streamp strm);
+typedef enum {
+    DFLTCC_INFLATE_CONTINUE,
+    DFLTCC_INFLATE_BREAK,
+    DFLTCC_INFLATE_SOFTWARE,
+} dfltcc_inflate_action;
+dfltcc_inflate_action dfltcc_inflate(z_streamp strm,
+                                     int flush, int *ret);
 
 #define DEFLATE_RESET_HOOK(strm) \
     dfltcc_reset((strm), sizeof(deflate_state))
@@ -112,4 +120,24 @@ void dfltcc_reset(z_streamp strm, uInt size);
 
 #define DEFLATE_NEED_CHECKSUM(strm) (!dfltcc_can_deflate((strm)))
 
+#define INFLATE_RESET_HOOK(strm) \
+    dfltcc_reset((strm), sizeof(struct inflate_state))
+
+#define INFLATE_TYPEDO_HOOK(strm, flush) \
+    if (dfltcc_can_inflate((strm))) { \
+        dfltcc_inflate_action action; \
+\
+        RESTORE(); \
+        action = dfltcc_inflate((strm), (flush), &ret); \
+        LOAD(); \
+        if (action == DFLTCC_INFLATE_CONTINUE) \
+            break; \
+        else if (action == DFLTCC_INFLATE_BREAK) \
+            goto inf_leave; \
+    }
+
+#define INFLATE_NEED_CHECKSUM(strm) (!dfltcc_can_inflate((strm)))
+
+#define INFLATE_NEED_UPDATEWINDOW(strm) (!dfltcc_can_inflate((strm)))
+
 #endif /* DFLTCC_H */
lib/zlib_dfltcc/dfltcc_inflate.c new file +143 -0

143 lines not on this page · raw message

lib/zlib_inflate/inflate.c +24 -8
index 48f14cd58c77..67cc9b08ae9d 100644
@@ -15,6 +15,16 @@
 #include "inffast.h"
 #include "infutil.h"
 
+/* architecture-specific bits */
+#ifdef CONFIG_ZLIB_DFLTCC
+#  include "../zlib_dfltcc/dfltcc.h"
+#else
+#define INFLATE_RESET_HOOK(strm) do {} while (0)
+#define INFLATE_TYPEDO_HOOK(strm, flush) do {} while (0)
+#define INFLATE_NEED_UPDATEWINDOW(strm) 1
+#define INFLATE_NEED_CHECKSUM(strm) 1
+#endif
+
 int zlib_inflate_workspacesize(void)
 {
     return sizeof(struct inflate_workspace);
@@ -42,6 +52,7 @@ int zlib_inflateReset(z_streamp strm)
     state->write = 0;
     state->whave = 0;
 
+    INFLATE_RESET_HOOK(strm);
     return Z_OK;
 }
 
@@ -66,7 +77,15 @@ int zlib_inflateInit2(z_streamp strm, int windowBits)
         return Z_STREAM_ERROR;
     }
     state->wbits = (unsigned)windowBits;
+#ifdef CONFIG_ZLIB_DFLTCC
+    /*
+     * DFLTCC requires the window to be page aligned.
+     * Thus, we overallocate and take the aligned portion of the buffer.
+     */
+    state->window = PTR_ALIGN(&WS(strm)->working_window[0], PAGE_SIZE);
+#else
     state->window = &WS(strm)->working_window[0];
+#endif
 
     return zlib_inflateReset(strm);
 }
@@ -227,11 +246,6 @@ static int zlib_inflateSyncPacket(z_streamp strm)
         bits -= bits & 7; \
     } while (0)
 
-/* Reverse the bytes in a 32-bit value */
-#define REVERSE(q) \
-    ((((q) >> 24) & 0xff) + (((q) >> 8) & 0xff00) + \
-     (((q) & 0xff00) << 8) + (((q) & 0xff) << 24))
-
 /*
    inflate() uses a state machine to process as much input data and generate as
    much output data as possible before returning.  The state machine is
@@ -395,6 +409,7 @@ int zlib_inflate(z_streamp strm, int flush)
             if (flush == Z_BLOCK) goto inf_leave;
 	    /* fall through */
         case TYPEDO:
+            INFLATE_TYPEDO_HOOK(strm, flush);
             if (state->last) {
                 BYTEBITS();
                 state->mode = CHECK;
@@ -692,7 +707,7 @@ int zlib_inflate(z_streamp strm, int flush)
                 out -= left;
                 strm->total_out += out;
                 state->total += out;
-                if (out)
+                if (INFLATE_NEED_CHECKSUM(strm) && out)
                     strm->adler = state->check =
                         UPDATE(state->check, put - out, out);
                 out = left;
@@ -726,7 +741,8 @@ int zlib_inflate(z_streamp strm, int flush)
      */
   inf_leave:
     RESTORE();
-    if (state->wsize || (state->mode < CHECK && out != strm->avail_out))
+    if (INFLATE_NEED_UPDATEWINDOW(strm) &&
+            (state->wsize || (state->mode < CHECK && out != strm->avail_out)))
         zlib_updatewindow(strm, out);
 
     in -= strm->avail_in;
@@ -734,7 +750,7 @@ int zlib_inflate(z_streamp strm, int flush)
     strm->total_in += in;
     strm->total_out += out;
     state->total += out;
-    if (state->wrap && out)
+    if (INFLATE_NEED_CHECKSUM(strm) && state->wrap && out)
         strm->adler = state->check =
             UPDATE(state->check, strm->next_out - out, out);
 
lib/zlib_inflate/inflate.h +8 -0
index 3d17b3d1b21f..f79337ddf98c 100644
@@ -11,6 +11,8 @@
    subject to change. Applications should only use zlib.h.
  */
 
+#include "inftrees.h"
+
 /* Possible inflate modes between inflate() calls */
 typedef enum {
     HEAD,       /* i: waiting for magic header */
@@ -108,4 +110,10 @@ struct inflate_state {
     unsigned short work[288];   /* work area for code table building */
     code codes[ENOUGH];         /* space for code tables */
 };
+
+/* Reverse the bytes in a 32-bit value */
+#define REVERSE(q) \
+    ((((q) >> 24) & 0xff) + (((q) >> 8) & 0xff00) + \
+     (((q) & 0xff00) << 8) + (((q) & 0xff) << 24))
+
 #endif
lib/zlib_inflate/infutil.h +16 -2
index eb1a9007bd86..784ab33b7842 100644
@@ -12,14 +12,28 @@
 #define _INFUTIL_H
 
 #include <linux/zlib.h>
+#ifdef CONFIG_ZLIB_DFLTCC
+#include "../zlib_dfltcc/dfltcc.h"
+#include <asm/page.h>
+#endif
 
 /* memory allocation for inflation */
 
 struct inflate_workspace {
 	struct inflate_state inflate_state;
-	unsigned char working_window[1 << MAX_WBITS];
+#ifdef CONFIG_ZLIB_DFLTCC
+	struct dfltcc_state dfltcc_state;
+	unsigned char working_window[(1 << MAX_WBITS) + PAGE_SIZE];
+#else
+	unsigned char working_window[(1 << MAX_WBITS)];
+#endif
 };
 
-#define WS(z) ((struct inflate_workspace *)(z->workspace))
+#ifdef CONFIG_ZLIB_DFLTCC
+/* dfltcc_state must be doubleword aligned for DFLTCC call */
+static_assert(offsetof(struct inflate_workspace, dfltcc_state) % 8 == 0);
+#endif
+
+#define WS(strm) ((struct inflate_workspace *)(strm->workspace))
 
 #endif
Signature · 1 line
2.17.1
#5 lore raw

PATCH v3 4/6 s390/boot: Add dfltcc= kernel command line parameter

In reply to Mikhail Zaslonko

Add the new kernel command line parameter 'dfltcc=' to configure s390 zlib hardware support.
Format: { on | off | def_only | inf_only | always }
on: s390 zlib hardware support for compression on
level 1 and decompression (default)
off: No s390 zlib hardware support
def_only: s390 zlib hardware support for deflate
only (compression on level 1)
inf_only: s390 zlib hardware support for inflate
only (decompression)
always: Same as 'on' but ignores the selected compression
level always using hardware support (used for debugging)

---
 Documentation/admin-guide/kernel-parameters.txt | 12 ++++++++++++
 arch/s390/boot/ipl_parm.c                       | 14 ++++++++++++++
 arch/s390/include/asm/setup.h                   |  7 +++++++
 arch/s390/kernel/setup.c                        |  2 ++
 lib/zlib_dfltcc/dfltcc.c                        |  5 ++++-
 lib/zlib_dfltcc/dfltcc.h                        |  1 +
 lib/zlib_dfltcc/dfltcc_deflate.c                |  6 ++++++
 lib/zlib_dfltcc/dfltcc_inflate.c                |  6 ++++++
 lib/zlib_dfltcc/dfltcc_util.h                   |  4 +++-
 9 files changed, 55 insertions(+), 2 deletions(-)
Documentation/admin-guide/kernel-parameters.txt +12 -0
index ade4e6ec23e0..08fcbaa69877 100644
@@ -834,6 +834,18 @@
 			dump out devices still on the deferred probe list after
 			retrying.
 
+	dfltcc=		[HW,S390]
+			Format: { on | off | def_only | inf_only | always }
+			on:       s390 zlib hardware support for compression on
+			          level 1 and decompression (default)
+			off:      No s390 zlib hardware support
+			def_only: s390 zlib hardware support for deflate
+			          only (compression on level 1)
+			inf_only: s390 zlib hardware support for inflate
+			          only (decompression)
+			always:   Same as 'on' but ignores the selected compression
+			          level always using hardware support (used for debugging)
+
 	dhash_entries=	[KNL]
 			Set number of hash buckets for dentry cache.
 
arch/s390/boot/ipl_parm.c +14 -0
index 24ef67eb1cef..357adad991d2 100644
@@ -14,6 +14,7 @@
 char __bootdata(early_command_line)[COMMAND_LINE_SIZE];
 struct ipl_parameter_block __bootdata_preserved(ipl_block);
 int __bootdata_preserved(ipl_block_valid);
+unsigned int __bootdata_preserved(zlib_dfltcc_support) = ZLIB_DFLTCC_FULL;
 
 unsigned long __bootdata(vmalloc_size) = VMALLOC_DEFAULT_SIZE;
 unsigned long __bootdata(memory_end);
@@ -229,6 +230,19 @@ void parse_boot_command_line(void)
 		if (!strcmp(param, "vmalloc") && val)
 			vmalloc_size = round_up(memparse(val, NULL), PAGE_SIZE);
 
+		if (!strcmp(param, "dfltcc")) {
+			if (!strcmp(val, "off"))
+				zlib_dfltcc_support = ZLIB_DFLTCC_DISABLED;
+			else if (!strcmp(val, "on"))
+				zlib_dfltcc_support = ZLIB_DFLTCC_FULL;
+			else if (!strcmp(val, "def_only"))
+				zlib_dfltcc_support = ZLIB_DFLTCC_DEFLATE_ONLY;
+			else if (!strcmp(val, "inf_only"))
+				zlib_dfltcc_support = ZLIB_DFLTCC_INFLATE_ONLY;
+			else if (!strcmp(val, "always"))
+				zlib_dfltcc_support = ZLIB_DFLTCC_FULL_DEBUG;
+		}
+
 		if (!strcmp(param, "noexec")) {
 			rc = kstrtobool(val, &enabled);
 			if (!rc && !enabled)
arch/s390/include/asm/setup.h +7 -0
index 69289e99cabd..b241ddb67caf 100644
@@ -79,6 +79,13 @@ struct parmarea {
 	char command_line[ARCH_COMMAND_LINE_SIZE];	/* 0x10480 */
 };
 
+extern unsigned int zlib_dfltcc_support;
+#define ZLIB_DFLTCC_DISABLED		0
+#define ZLIB_DFLTCC_FULL		1
+#define ZLIB_DFLTCC_DEFLATE_ONLY	2
+#define ZLIB_DFLTCC_INFLATE_ONLY	3
+#define ZLIB_DFLTCC_FULL_DEBUG		4
+
 extern int noexec_disabled;
 extern int memory_end_set;
 extern unsigned long memory_end;
arch/s390/kernel/setup.c +2 -0
index 9cbf490fd162..a577d7df379c 100644
@@ -111,6 +111,8 @@ unsigned long __bootdata_preserved(__etext_dma);
 unsigned long __bootdata_preserved(__sdma);
 unsigned long __bootdata_preserved(__edma);
 unsigned long __bootdata_preserved(__kaslr_offset);
+unsigned int __bootdata_preserved(zlib_dfltcc_support);
+EXPORT_SYMBOL(zlib_dfltcc_support);
 
 unsigned long VMALLOC_START;
 EXPORT_SYMBOL(VMALLOC_START);
lib/zlib_dfltcc/dfltcc.c +4 -1
index 7f77e5bb01c6..c30de430b30c 100644
@@ -44,7 +44,10 @@ void dfltcc_reset(
     dfltcc_state->param.nt = 1;
 
     /* Initialize tuning parameters */
-    dfltcc_state->level_mask = DFLTCC_LEVEL_MASK;
+    if (zlib_dfltcc_support == ZLIB_DFLTCC_FULL_DEBUG)
+        dfltcc_state->level_mask = DFLTCC_LEVEL_MASK_DEBUG;
+    else
+        dfltcc_state->level_mask = DFLTCC_LEVEL_MASK;
     dfltcc_state->block_size = DFLTCC_BLOCK_SIZE;
     dfltcc_state->block_threshold = DFLTCC_FIRST_FHT_BLOCK_SIZE;
     dfltcc_state->dht_threshold = DFLTCC_DHT_MIN_SAMPLE_SIZE;
lib/zlib_dfltcc/dfltcc.h +1 -0
index 4782c92bb2ff..be70c807b62f 100644
@@ -8,6 +8,7 @@
  * Tuning parameters.
  */
 #define DFLTCC_LEVEL_MASK 0x2 /* DFLTCC compression for level 1 only */
+#define DFLTCC_LEVEL_MASK_DEBUG 0x3fe /* DFLTCC compression for all levels */
 #define DFLTCC_BLOCK_SIZE 1048576
 #define DFLTCC_FIRST_FHT_BLOCK_SIZE 4096
 #define DFLTCC_DHT_MIN_SAMPLE_SIZE 4096
lib/zlib_dfltcc/dfltcc_deflate.c +6 -0
index b9d16917c30a..aa4539f5c071 100644
@@ -3,6 +3,7 @@
 #include "../zlib_deflate/defutil.h"
 #include "dfltcc_util.h"
 #include "dfltcc.h"
+#include <asm/setup.h>
 #include <linux/zutil.h>
 
 /*
@@ -15,6 +16,11 @@ int dfltcc_can_deflate(
     deflate_state *state = (deflate_state *)strm->state;
     struct dfltcc_state *dfltcc_state = GET_DFLTCC_STATE(state);
 
+    /* Check for kernel dfltcc command line parameter */
+    if (zlib_dfltcc_support == ZLIB_DFLTCC_DISABLED ||
+            zlib_dfltcc_support == ZLIB_DFLTCC_INFLATE_ONLY)
+        return 0;
+
     /* Unsupported compression settings */
     if (!dfltcc_are_params_ok(state->level, state->w_bits, state->strategy,
                               dfltcc_state->level_mask))
lib/zlib_dfltcc/dfltcc_inflate.c +6 -0
index 12a93a06bd61..aa9ef23474df 100644
@@ -3,6 +3,7 @@
 #include "../zlib_inflate/inflate.h"
 #include "dfltcc_util.h"
 #include "dfltcc.h"
+#include <asm/setup.h>
 #include <linux/zutil.h>
 
 /*
@@ -15,6 +16,11 @@ int dfltcc_can_inflate(
     struct inflate_state *state = (struct inflate_state *)strm->state;
     struct dfltcc_state *dfltcc_state = GET_DFLTCC_STATE(state);
 
+    /* Check for kernel dfltcc command line parameter */
+    if (zlib_dfltcc_support == ZLIB_DFLTCC_DISABLED ||
+            zlib_dfltcc_support == ZLIB_DFLTCC_DEFLATE_ONLY)
+        return 0;
+
     /* Unsupported compression settings */
     if (state->wbits != HB_BITS)
         return 0;
lib/zlib_dfltcc/dfltcc_util.h +3 -1
index 82cd1950c416..7c0d3bdc50a9 100644
@@ -4,6 +4,7 @@
 
 #include <linux/zutil.h>
 #include <asm/facility.h>
+#include <asm/setup.h>
 
 /*
  * C wrapper for the DEFLATE CONVERSION CALL instruction.
@@ -102,7 +103,8 @@ static inline int dfltcc_are_params_ok(
 
 static inline int is_dfltcc_enabled(void)
 {
-    return test_facility(DFLTCC_FACILITY);
+    return (zlib_dfltcc_support != ZLIB_DFLTCC_DISABLED &&
+            test_facility(DFLTCC_FACILITY));
 }
 
 char *oesc_msg(char *buf, int oesc);
Signature · 1 line
2.17.1
#6 lore raw

PATCH v3 5/6 lib/zlib: Add zlib_deflate_dfltcc_enabled() function

In reply to Mikhail Zaslonko

Add a new function to zlib.h checking if s390 Deflate-Conversion facility is installed and enabled.

---
 include/linux/zlib.h            |  6 ++++++
 lib/zlib_deflate/deflate.c      |  6 ++++++
 lib/zlib_deflate/deflate_syms.c |  1 +
 lib/zlib_dfltcc/dfltcc.h        | 11 +++++++++++
 lib/zlib_dfltcc/dfltcc_util.h   |  9 ---------
 5 files changed, 24 insertions(+), 9 deletions(-)
include/linux/zlib.h +6 -0
index 92dbbd3f6c75..c757d848a758 100644
@@ -191,6 +191,12 @@ extern int zlib_deflate_workspacesize (int windowBits, int memLevel);
    exceed those passed here.
 */
 
+extern int zlib_deflate_dfltcc_enabled (void);
+/*
+   Returns 1 if Deflate-Conversion facility is installed and enabled,
+   otherwise 0.
+*/
+
 /* 
 extern int deflateInit (z_streamp strm, int level);
 
lib/zlib_deflate/deflate.c +6 -0
index 0d60d81a2637..8a878d0d892c 100644
@@ -59,6 +59,7 @@
 #define DEFLATE_RESET_HOOK(strm) do {} while (0)
 #define DEFLATE_HOOK(strm, flush, bstate) 0
 #define DEFLATE_NEED_CHECKSUM(strm) 1
+#define DEFLATE_DFLTCC_ENABLED() 0
 #endif
 
 /* ===========================================================================
@@ -1138,3 +1139,8 @@ int zlib_deflate_workspacesize(int windowBits, int memLevel)
         + zlib_deflate_head_memsize(memLevel)
         + zlib_deflate_overlay_memsize(memLevel);
 }
+
+int zlib_deflate_dfltcc_enabled(void)
+{
+	return DEFLATE_DFLTCC_ENABLED();
+}
lib/zlib_deflate/deflate_syms.c +1 -0
index 72fe4b73be53..24b740b99678 100644
@@ -12,6 +12,7 @@
 #include <linux/zlib.h>
 
 EXPORT_SYMBOL(zlib_deflate_workspacesize);
+EXPORT_SYMBOL(zlib_deflate_dfltcc_enabled);
 EXPORT_SYMBOL(zlib_deflate);
 EXPORT_SYMBOL(zlib_deflateInit2);
 EXPORT_SYMBOL(zlib_deflateEnd);
lib/zlib_dfltcc/dfltcc.h +11 -0
index be70c807b62f..2a2fac1d050a 100644
@@ -3,6 +3,8 @@
 #define DFLTCC_H
 
 #include "../zlib_deflate/defutil.h"
+#include <asm/facility.h>
+#include <asm/setup.h>
 
 /*
  * Tuning parameters.
@@ -14,6 +16,8 @@
 #define DFLTCC_DHT_MIN_SAMPLE_SIZE 4096
 #define DFLTCC_RIBM 0
 
+#define DFLTCC_FACILITY 151
+
 /*
  * Parameter Block for Query Available Functions.
  */
@@ -113,6 +117,11 @@ typedef enum {
 } dfltcc_inflate_action;
 dfltcc_inflate_action dfltcc_inflate(z_streamp strm,
                                      int flush, int *ret);
+static inline int is_dfltcc_enabled(void)
+{
+return (zlib_dfltcc_support != ZLIB_DFLTCC_DISABLED &&
+        test_facility(DFLTCC_FACILITY));
+}
 
 #define DEFLATE_RESET_HOOK(strm) \
     dfltcc_reset((strm), sizeof(deflate_state))
@@ -121,6 +130,8 @@ dfltcc_inflate_action dfltcc_inflate(z_streamp strm,
 
 #define DEFLATE_NEED_CHECKSUM(strm) (!dfltcc_can_deflate((strm)))
 
+#define DEFLATE_DFLTCC_ENABLED() is_dfltcc_enabled()
+
 #define INFLATE_RESET_HOOK(strm) \
     dfltcc_reset((strm), sizeof(struct inflate_state))
 
lib/zlib_dfltcc/dfltcc_util.h +0 -9
index 7c0d3bdc50a9..4a46b5009f0d 100644
@@ -3,8 +3,6 @@
 #define DFLTCC_UTIL_H
 
 #include <linux/zutil.h>
-#include <asm/facility.h>
-#include <asm/setup.h>
 
 /*
  * C wrapper for the DEFLATE CONVERSION CALL instruction.
@@ -24,7 +22,6 @@ typedef enum {
 #define HBT_CIRCULAR (1 << 7)
 #define HB_BITS 15
 #define HB_SIZE (1 << HB_BITS)
-#define DFLTCC_FACILITY 151
 
 static inline dfltcc_cc dfltcc(
     int fn,
@@ -101,12 +98,6 @@ static inline int dfltcc_are_params_ok(
         (strategy == Z_DEFAULT_STRATEGY);
 }
 
-static inline int is_dfltcc_enabled(void)
-{
-    return (zlib_dfltcc_support != ZLIB_DFLTCC_DISABLED &&
-            test_facility(DFLTCC_FACILITY));
-}
-
 char *oesc_msg(char *buf, int oesc);
 
 #endif /* DFLTCC_UTIL_H */
Signature · 1 line
2.17.1
#7 lore raw

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

In reply to Mikhail Zaslonko

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.

---
 fs/btrfs/compression.c |   2 +-
 fs/btrfs/zlib.c        | 128 ++++++++++++++++++++++++++++++-----------
 2 files changed, 94 insertions(+), 36 deletions(-)
fs/btrfs/compression.c +1 -1
index ee834ef7beb4..6bd0e75a822c 100644
@@ -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);
fs/btrfs/zlib.c +93 -35

216 lines not on this page · raw message

Signature · 1 line
2.17.1
#8 lore raw

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

Quoted 111 lines · Mikhail Zaslonko

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.

---
  fs/btrfs/compression.c |   2 +-
  fs/btrfs/zlib.c        | 128 ++++++++++++++++++++++++++++++-----------
  2 files changed, 94 insertions(+), 36 deletions(-)
fs/btrfs/compression.c +1 -1
index ee834ef7beb4..6bd0e75a822c 100644
@@ -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);
fs/btrfs/zlib.c +28 -6
index a6c90a003c12..159486801631 100644
@@ -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) {

fs/btrfs/zlib.c int zlib_compress_pages(struct list_head *ws, struct address_space *mapping, · line 154

Quoted 10 lines
fs/btrfs/zlib.c +10 -0
+						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?

Quoted 20 lines
fs/btrfs/zlib.c +16 -0
+				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",

fs/btrfs/zlib.c int zlib_compress_pages(struct list_head *ws, struct address_space *mapping, · line 187

Quoted 4 lines
fs/btrfs/zlib.c +1 -0
@@ -136,6 +184,7 @@ int zlib_compress_pages(struct list_head *ws, struct address_space *mapping,
  			ret = -E2BIG;
  			goto out;
  		}
+

Extra newline. Thanks,

Josef

#9 lore raw

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

Hello,

Quoted 110 lines · Josef Bacik
Quoted 109 lines · Mikhail Zaslonko

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.

---
  fs/btrfs/compression.c |   2 +-
  fs/btrfs/zlib.c        | 128 ++++++++++++++++++++++++++++++-----------

  2 files changed, 94 insertions(+), 36 deletions(-)
fs/btrfs/compression.c +1 -1
index ee834ef7beb4..6bd0e75a822c 100644
@@ -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);
fs/btrfs/zlib.c +13 -1
index a6c90a003c12..159486801631 100644
@@ -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;

fs/btrfs/zlib.c +17 -3
@@ -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,
Quoted 8 lines

fs/btrfs/zlib.c int zlib_compress_pages(struct list_head *ws, struct address_space *mapping, · line 154

Quoted 6 lines
fs/btrfs/zlib.c +6 -0
+                                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).

Quoted 29 lines
Quoted 21 lines
fs/btrfs/zlib.c +16 -0
+                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",

fs/btrfs/zlib.c int zlib_compress_pages(struct list_head *ws, struct address_space *mapping, · line 187

Quoted 4 lines
fs/btrfs/zlib.c +1 -0
@@ -136,6 +184,7 @@ int zlib_compress_pages(struct list_head *ws, struct address_space *mapping,
               ret = -E2BIG;
               goto out;
           }
+

Extra newline.  Thanks,

Josef

#10 lore raw

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

In reply to Mikhail Zaslonko
Quoted 7 lines · Mikhail Zaslonko

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.

You could also summarize the previous discussion eg. the question whether zlib will decommpress the stream produced on larger input buffers on system that has only one page available for each decompression round.

Quoted 24 lines
---
 fs/btrfs/compression.c |   2 +-
 fs/btrfs/zlib.c        | 128 ++++++++++++++++++++++++++++++-----------
 2 files changed, 94 insertions(+), 36 deletions(-)
fs/btrfs/compression.c +1 -1
index ee834ef7beb4..6bd0e75a822c 100644
@@ -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);
fs/btrfs/zlib.c +0 -0
index a6c90a003c12..159486801631 100644

fs/btrfs/zlib.c line 23

Quoted 4 lines
fs/btrfs/zlib.c +1 -0
@@ -20,9 +20,12 @@
 #include <linux/refcount.h>
 #include "compression.h"
 
+#define ZLIB_DFLTCC_BUF_SIZE    (4 * PAGE_SIZE)

Please add a comment what's this constant for

fs/btrfs/zlib.c line 28

Quoted 5 lines
fs/btrfs/zlib.c +2 -0
+
 struct workspace {
 	z_stream strm;
 	char *buf;
+	unsigned long buf_size;

int should be enough here

fs/btrfs/zlib.c struct list_head *zlib_alloc_workspace(unsigned int level) · line 71

Quoted 13 lines
fs/btrfs/zlib.c +5 -1
 	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);

Why do you use this wild GFP flag combination? I can understand NOWARN, but why the others?

Quoted 10 lines
fs/btrfs/zlib.c +6 -0
+		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;
 

fs/btrfs/zlib.c int zlib_compress_pages(struct list_head *ws, struct address_space *mapping, · line 94

Quoted 4 lines
fs/btrfs/zlib.c +1 -0
@@ -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;

This should be declared in the inner most scope of use

fs/btrfs/zlib.c int zlib_compress_pages(struct list_head *ws, struct address_space *mapping, · line 102

Quoted 8 lines
fs/btrfs/zlib.c +1 -0
 	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;

long does not seem to be necessary, int

Quoted 18 lines
fs/btrfs/zlib.c +0 -4
 	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;

fs/btrfs/zlib.c int zlib_compress_pages(struct list_head *ws, struct address_space *mapping, · line 137

Quoted 10 lines
fs/btrfs/zlib.c +5 -1
+	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
+		 */

Please format the comment properly

fs/btrfs/zlib.c int zlib_compress_pages(struct list_head *ws, struct address_space *mapping, · line 142

Quoted 5 lines
fs/btrfs/zlib.c +5 -0
+		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) {
				int i;

fs/btrfs/zlib.c int zlib_compress_pages(struct list_head *ws, struct address_space *mapping, · line 151

Quoted 9 lines
fs/btrfs/zlib.c +9 -0
+				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,

spaces around '*': i * PAGE_SIZE

Quoted 55 lines
fs/btrfs/zlib.c +22 -18
+					       data_in, PAGE_SIZE);
+					start += PAGE_SIZE;
+				}
+				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;
 		}
+
 		/* we need another page for writing out.  Test this
 		 * before the total_in so we will pull in a new page for
 		 * the stream end if required
@@ -161,33 +210,42 @@ int zlib_compress_pages(struct list_head *ws, struct address_space *mapping,
 		/* we're all done */
 		if (workspace->strm.total_in >= len)
 			break;
-
-		/* we've read in a full page, get a new one */
-		if (workspace->strm.avail_in == 0) {
-			if (workspace->strm.total_out > max_out)
-				break;
-
-			bytes_left = len - workspace->strm.total_in;
-			kunmap(in_page);
-			put_page(in_page);
-
-			start += PAGE_SIZE;
-			in_page = find_get_page(mapping,
-						start >> PAGE_SHIFT);
-			data_in = kmap(in_page);
-			workspace->strm.avail_in = min(bytes_left,
-							   PAGE_SIZE);
-			workspace->strm.next_in = data_in;
-		}
+		if (workspace->strm.total_out > max_out)
+			break;
 	}

fs/btrfs/zlib.c int zlib_compress_pages(struct list_head *ws, struct address_space *mapping, · line 219

Quoted 10 lines
fs/btrfs/zlib.c +3 -6
 	workspace->strm.avail_in = 0;
-	ret = zlib_deflate(&workspace->strm, Z_FINISH);
-	zlib_deflateEnd(&workspace->strm);
-
-	if (ret != Z_STREAM_END) {
-		ret = -EIO;
-		goto out;
+	/* call deflate with Z_FINISH flush parameter providing more output
+	 * space but no more input data, until it returns with Z_STREAM_END
+	 */

Comment formatting

Quoted 80 lines
fs/btrfs/zlib.c +33 -5
+	while (ret != Z_STREAM_END) {
+		ret = zlib_deflate(&workspace->strm, Z_FINISH);
+		if (ret == Z_STREAM_END)
+			break;
+		if (ret != Z_OK && ret != Z_BUF_ERROR) {
+			zlib_deflateEnd(&workspace->strm);
+			ret = -EIO;
+			goto out;
+		} else if (workspace->strm.avail_out == 0) {
+			/* get another page for the stream end */
+			kunmap(out_page);
+			if (nr_pages == nr_dest_pages) {
+				out_page = NULL;
+				ret = -E2BIG;
+				goto out;
+			}
+			out_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
+			if (out_page == NULL) {
+				ret = -ENOMEM;
+				goto out;
+			}
+			cpage_out = kmap(out_page);
+			pages[nr_pages] = out_page;
+			nr_pages++;
+			workspace->strm.avail_out = PAGE_SIZE;
+			workspace->strm.next_out = cpage_out;
+		}
 	}
+	zlib_deflateEnd(&workspace->strm);
 
 	if (workspace->strm.total_out >= workspace->strm.total_in) {
 		ret = -E2BIG;
@@ -231,7 +289,7 @@ int zlib_decompress_bio(struct list_head *ws, struct compressed_bio *cb)
 
 	workspace->strm.total_out = 0;
 	workspace->strm.next_out = workspace->buf;
-	workspace->strm.avail_out = PAGE_SIZE;
+	workspace->strm.avail_out = workspace->buf_size;
 
 	/* If it's deflate, and it's got no preset dictionary, then
 	   we can tell zlib to skip the adler32 check. */
@@ -270,7 +328,7 @@ int zlib_decompress_bio(struct list_head *ws, struct compressed_bio *cb)
 		}
 
 		workspace->strm.next_out = workspace->buf;
-		workspace->strm.avail_out = PAGE_SIZE;
+		workspace->strm.avail_out = workspace->buf_size;
 
 		if (workspace->strm.avail_in == 0) {
 			unsigned long tmp;
@@ -320,7 +378,7 @@ int zlib_decompress(struct list_head *ws, unsigned char *data_in,
 	workspace->strm.total_in = 0;
 
 	workspace->strm.next_out = workspace->buf;
-	workspace->strm.avail_out = PAGE_SIZE;
+	workspace->strm.avail_out = workspace->buf_size;
 	workspace->strm.total_out = 0;
 	/* If it's deflate, and it's got no preset dictionary, then
 	   we can tell zlib to skip the adler32 check. */
@@ -364,7 +422,7 @@ int zlib_decompress(struct list_head *ws, unsigned char *data_in,
 			buf_offset = 0;
 
 		bytes = min(PAGE_SIZE - pg_offset,
-			    PAGE_SIZE - buf_offset);
+			    PAGE_SIZE - (buf_offset % PAGE_SIZE));
 		bytes = min(bytes, bytes_left);
 
 		kaddr = kmap_atomic(dest_page);
@@ -375,7 +433,7 @@ int zlib_decompress(struct list_head *ws, unsigned char *data_in,
 		bytes_left -= bytes;
 next:
 		workspace->strm.next_out = workspace->buf;
-		workspace->strm.avail_out = PAGE_SIZE;
+		workspace->strm.avail_out = workspace->buf_size;
 	}
 
 	if (ret != Z_STREAM_END && bytes_left != 0)
Signature · 1 line
2.17.1
#11 lore raw

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

Quoted 50 lines · David Sterba
Quoted 7 lines · Mikhail Zaslonko

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.

You could also summarize the previous discussion eg. the question whether zlib will decommpress the stream produced on larger input buffers on system that has only one page available for each decompression round.

Quoted 24 lines
---
  fs/btrfs/compression.c |   2 +-
  fs/btrfs/zlib.c        | 128 ++++++++++++++++++++++++++++++-----------
  2 files changed, 94 insertions(+), 36 deletions(-)
fs/btrfs/compression.c +1 -1
index ee834ef7beb4..6bd0e75a822c 100644
@@ -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);
fs/btrfs/zlib.c +0 -0
index a6c90a003c12..159486801631 100644

fs/btrfs/zlib.c line 23

Quoted 4 lines
fs/btrfs/zlib.c +1 -0
@@ -20,9 +20,12 @@
  #include <linux/refcount.h>
  #include "compression.h"
 
+#define ZLIB_DFLTCC_BUF_SIZE    (4 * PAGE_SIZE)

Please add a comment what's this constant for

fs/btrfs/zlib.c line 28

Quoted 5 lines
fs/btrfs/zlib.c +2 -0
+
  struct workspace {
  	z_stream strm;
  	char *buf;
+	unsigned long buf_size;

int should be enough here

Quoted 15 lines

fs/btrfs/zlib.c struct list_head *zlib_alloc_workspace(unsigned int level) · line 71

Quoted 13 lines
fs/btrfs/zlib.c +5 -1
  	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);

Why do you use this wild GFP flag combination? I can understand NOWARN, but why the others?

This addresses the following complaint about order 2 allocation with GFP_KERNEL:
https://lkml.org/lkml/2019/11/26/417

Below a fallback to a single page is implemented, as it was suggested. So, the initial (more costly) allocation should be made with minimal aggression to allow the allocator fail. Otherwise, that fallback simply doesn't make sense. Hence, such a combination.

Thanks,
Eduard.

Quoted 221 lines
Quoted 10 lines
fs/btrfs/zlib.c +6 -0
+		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;
 

fs/btrfs/zlib.c int zlib_compress_pages(struct list_head *ws, struct address_space *mapping, · line 94

Quoted 4 lines
fs/btrfs/zlib.c +1 -0
@@ -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;

This should be declared in the inner most scope of use

fs/btrfs/zlib.c int zlib_compress_pages(struct list_head *ws, struct address_space *mapping, · line 102

Quoted 8 lines
fs/btrfs/zlib.c +1 -0
  	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;

long does not seem to be necessary, int

Quoted 18 lines
fs/btrfs/zlib.c +0 -4
  	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;

fs/btrfs/zlib.c int zlib_compress_pages(struct list_head *ws, struct address_space *mapping, · line 137

Quoted 10 lines
fs/btrfs/zlib.c +5 -1
+	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
+		 */

Please format the comment properly

fs/btrfs/zlib.c int zlib_compress_pages(struct list_head *ws, struct address_space *mapping, · line 142

Quoted 5 lines
fs/btrfs/zlib.c +5 -0
+		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) {
				int i;

fs/btrfs/zlib.c int zlib_compress_pages(struct list_head *ws, struct address_space *mapping, · line 151

Quoted 9 lines
fs/btrfs/zlib.c +9 -0
+				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,

spaces around '*': i * PAGE_SIZE

Quoted 55 lines
fs/btrfs/zlib.c +22 -18
+					       data_in, PAGE_SIZE);
+					start += PAGE_SIZE;
+				}
+				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;
  		}
+
  		/* we need another page for writing out.  Test this
  		 * before the total_in so we will pull in a new page for
  		 * the stream end if required
@@ -161,33 +210,42 @@ int zlib_compress_pages(struct list_head *ws, struct address_space *mapping,
  		/* we're all done */
  		if (workspace->strm.total_in >= len)
  			break;
-
-		/* we've read in a full page, get a new one */
-		if (workspace->strm.avail_in == 0) {
-			if (workspace->strm.total_out > max_out)
-				break;
-
-			bytes_left = len - workspace->strm.total_in;
-			kunmap(in_page);
-			put_page(in_page);
-
-			start += PAGE_SIZE;
-			in_page = find_get_page(mapping,
-						start >> PAGE_SHIFT);
-			data_in = kmap(in_page);
-			workspace->strm.avail_in = min(bytes_left,
-							   PAGE_SIZE);
-			workspace->strm.next_in = data_in;
-		}
+		if (workspace->strm.total_out > max_out)
+			break;
  	}

fs/btrfs/zlib.c int zlib_compress_pages(struct list_head *ws, struct address_space *mapping, · line 219

Quoted 10 lines
fs/btrfs/zlib.c +3 -6
  	workspace->strm.avail_in = 0;
-	ret = zlib_deflate(&workspace->strm, Z_FINISH);
-	zlib_deflateEnd(&workspace->strm);
-
-	if (ret != Z_STREAM_END) {
-		ret = -EIO;
-		goto out;
+	/* call deflate with Z_FINISH flush parameter providing more output
+	 * space but no more input data, until it returns with Z_STREAM_END
+	 */

Comment formatting

Quoted 80 lines
fs/btrfs/zlib.c +33 -5
+	while (ret != Z_STREAM_END) {
+		ret = zlib_deflate(&workspace->strm, Z_FINISH);
+		if (ret == Z_STREAM_END)
+			break;
+		if (ret != Z_OK && ret != Z_BUF_ERROR) {
+			zlib_deflateEnd(&workspace->strm);
+			ret = -EIO;
+			goto out;
+		} else if (workspace->strm.avail_out == 0) {
+			/* get another page for the stream end */
+			kunmap(out_page);
+			if (nr_pages == nr_dest_pages) {
+				out_page = NULL;
+				ret = -E2BIG;
+				goto out;
+			}
+			out_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
+			if (out_page == NULL) {
+				ret = -ENOMEM;
+				goto out;
+			}
+			cpage_out = kmap(out_page);
+			pages[nr_pages] = out_page;
+			nr_pages++;
+			workspace->strm.avail_out = PAGE_SIZE;
+			workspace->strm.next_out = cpage_out;
+		}
  	}
+	zlib_deflateEnd(&workspace->strm);
 
  	if (workspace->strm.total_out >= workspace->strm.total_in) {
  		ret = -E2BIG;
@@ -231,7 +289,7 @@ int zlib_decompress_bio(struct list_head *ws, struct compressed_bio *cb)
 
  	workspace->strm.total_out = 0;
  	workspace->strm.next_out = workspace->buf;
-	workspace->strm.avail_out = PAGE_SIZE;
+	workspace->strm.avail_out = workspace->buf_size;
 
  	/* If it's deflate, and it's got no preset dictionary, then
  	   we can tell zlib to skip the adler32 check. */
@@ -270,7 +328,7 @@ int zlib_decompress_bio(struct list_head *ws, struct compressed_bio *cb)
  		}
 
  		workspace->strm.next_out = workspace->buf;
-		workspace->strm.avail_out = PAGE_SIZE;
+		workspace->strm.avail_out = workspace->buf_size;
 
  		if (workspace->strm.avail_in == 0) {
  			unsigned long tmp;
@@ -320,7 +378,7 @@ int zlib_decompress(struct list_head *ws, unsigned char *data_in,
  	workspace->strm.total_in = 0;
 
  	workspace->strm.next_out = workspace->buf;
-	workspace->strm.avail_out = PAGE_SIZE;
+	workspace->strm.avail_out = workspace->buf_size;
  	workspace->strm.total_out = 0;
  	/* If it's deflate, and it's got no preset dictionary, then
  	   we can tell zlib to skip the adler32 check. */
@@ -364,7 +422,7 @@ int zlib_decompress(struct list_head *ws, unsigned char *data_in,
  			buf_offset = 0;
 
  		bytes = min(PAGE_SIZE - pg_offset,
-			    PAGE_SIZE - buf_offset);
+			    PAGE_SIZE - (buf_offset % PAGE_SIZE));
  		bytes = min(bytes, bytes_left);
 
  		kaddr = kmap_atomic(dest_page);
@@ -375,7 +433,7 @@ int zlib_decompress(struct list_head *ws, unsigned char *data_in,
  		bytes_left -= bytes;
  next:
  		workspace->strm.next_out = workspace->buf;
-		workspace->strm.avail_out = PAGE_SIZE;
+		workspace->strm.avail_out = workspace->buf_size;
  	}
 
  	if (ret != Z_STREAM_END && bytes_left != 0)
Signature · 1 line
2.17.1
#12 lore raw

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

Quoted 17 lines · Eduard Shishkin
Quoted 4 lines
Quoted 4 lines
fs/btrfs/zlib.c +0 -0
@@ -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;
Quoted 8 lines

fs/btrfs/zlib.c struct list_head *zlib_alloc_workspace(unsigned int level) · line 71

Quoted 6 lines
fs/btrfs/zlib.c +5 -1
-	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);

Why do you use this wild GFP flag combination? I can understand NOWARN, but why the others?

This addresses the following complaint about order 2 allocation with GFP_KERNEL:
https://lkml.org/lkml/2019/11/26/417

Quoted 6 lines

Below a fallback to a single page is implemented, as it was suggested. So, the initial (more costly) allocation should be made with minimal aggression
to allow the allocator fail. Otherwise, that fallback simply doesn't make sense.
Hence, such a combination.

I see, please add a comment explaining that.

#13 lore raw

PATCH v4 1/1 btrfs: Use larger zlib buffer for s390 hardware compression

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.
The data compressed with larger input buffers will still conform to zlib standard and thus can be decompressed also on a systems that uses only PAGE_SIZE buffer for btrfs zlib.

---
 fs/btrfs/compression.c |   2 +-
 fs/btrfs/zlib.c        | 135 ++++++++++++++++++++++++++++++-----------
 2 files changed, 101 insertions(+), 36 deletions(-)
fs/btrfs/compression.c +1 -1
index ee834ef7beb4..6bd0e75a822c 100644
@@ -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);
fs/btrfs/zlib.c +100 -35

211 lines not on this page · raw message

Signature · 1 line
2.17.1
#14 lore raw

Re: [PATCH v4] btrfs: Use larger zlib buffer for s390 hardware compression

Quoted 192 lines · Mikhail Zaslonko

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.
The data compressed with larger input buffers will still conform to zlib standard and thus can be decompressed also on a systems that uses only PAGE_SIZE buffer for btrfs zlib.

---
  fs/btrfs/compression.c |   2 +-
  fs/btrfs/zlib.c        | 135 ++++++++++++++++++++++++++++++-----------
  2 files changed, 101 insertions(+), 36 deletions(-)
fs/btrfs/compression.c +1 -1
index ee834ef7beb4..6bd0e75a822c 100644
@@ -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);
fs/btrfs/zlib.c +78 -30
index a6c90a003c12..05615a1099db 100644
@@ -20,9 +20,13 @@
  #include <linux/refcount.h>
  #include "compression.h"
 
+/* workspace buffer size for s390 zlib hardware support */
+#define ZLIB_DFLTCC_BUF_SIZE    (4 * PAGE_SIZE)
+
  struct workspace {
  	z_stream strm;
  	char *buf;
+	unsigned int buf_size;
  	struct list_head list;
  	int level;
  };
@@ -61,7 +65,21 @@ 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;
+	/*
+	 * In case of s390 zlib hardware support, allocate lager workspace
+	 * buffer. If allocator fails, fall back to a single page buffer.
+	 */
+	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;
 
@@ -85,6 +103,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 int 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 +121,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 +130,51 @@ 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) {
+				int i;
+
+				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;
+				}
+				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,
+						       (unsigned long) workspace->buf_size);
+		}
+
  		ret = zlib_deflate(&workspace->strm, Z_SYNC_FLUSH);
  		if (ret != Z_OK) {
  			pr_debug("BTRFS: deflate in loop returned %d\n",
@@ -161,33 +216,43 @@ int zlib_compress_pages(struct list_head *ws, struct address_space *mapping,
  		/* we're all done */
  		if (workspace->strm.total_in >= len)
  			break;
-
-		/* we've read in a full page, get a new one */
-		if (workspace->strm.avail_in == 0) {
-			if (workspace->strm.total_out > max_out)
-				break;
-
-			bytes_left = len - workspace->strm.total_in;
-			kunmap(in_page);
-			put_page(in_page);
-
-			start += PAGE_SIZE;
-			in_page = find_get_page(mapping,
-						start >> PAGE_SHIFT);
-			data_in = kmap(in_page);
-			workspace->strm.avail_in = min(bytes_left,
-							   PAGE_SIZE);
-			workspace->strm.next_in = data_in;
-		}
+		if (workspace->strm.total_out > max_out)
+			break;
  	}
  	workspace->strm.avail_in = 0;
-	ret = zlib_deflate(&workspace->strm, Z_FINISH);
-	zlib_deflateEnd(&workspace->strm);
-
-	if (ret != Z_STREAM_END) {
-		ret = -EIO;
-		goto out;
+	/*
+	 * Call deflate with Z_FINISH flush parameter providing more output
+	 * space but no more input data, until it returns with Z_STREAM_END.
+	 */
+	while (ret != Z_STREAM_END) {
+		ret = zlib_deflate(&workspace->strm, Z_FINISH);
+		if (ret == Z_STREAM_END)
+			break;
+		if (ret != Z_OK && ret != Z_BUF_ERROR) {
+			zlib_deflateEnd(&workspace->strm);
+			ret = -EIO;
+			goto out;
+		} else if (workspace->strm.avail_out == 0) {
+			/* get another page for the stream end */
+			kunmap(out_page);

fs/btrfs/zlib.c int zlib_compress_pages(struct list_head *ws, struct address_space *mapping, · line 247

Quoted 10 lines
fs/btrfs/zlib.c +10 -0
+			if (nr_pages == nr_dest_pages) {
+				out_page = NULL;
+				ret = -E2BIG;
+				goto out;
+			}
+			out_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
+			if (out_page == NULL) {
+				ret = -ENOMEM;
+				goto out;
+			}

Do we need zlib_deflateEnd() for the above error cases? Thanks,

Josef

#15 lore raw

Re: [PATCH v4] btrfs: Use larger zlib buffer for s390 hardware compression

Hello,

Quoted 191 lines · Josef Bacik
Quoted 190 lines · Mikhail Zaslonko

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.
The data compressed with larger input buffers will still conform to zlib standard and thus can be decompressed also on a systems that uses only PAGE_SIZE buffer for btrfs zlib.

---
  fs/btrfs/compression.c |   2 +-
  fs/btrfs/zlib.c        | 135 ++++++++++++++++++++++++++++++-----------

  2 files changed, 101 insertions(+), 36 deletions(-)
fs/btrfs/compression.c +1 -1
index ee834ef7beb4..6bd0e75a822c 100644
@@ -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);
fs/btrfs/zlib.c +0 -0
index a6c90a003c12..05615a1099db 100644
@@ -20,9 +20,13 @@
   #include <linux/refcount.h>
   #include "compression.h"

  +/* workspace buffer size for s390 zlib hardware support */

fs/btrfs/zlib.c +2 -0
+#define ZLIB_DFLTCC_BUF_SIZE    (4 * PAGE_SIZE)
+

  struct workspace {
      z_stream strm;
      char *buf;
+    unsigned int buf_size;
      struct list_head list;
      int level;
  };

fs/btrfs/zlib.c +15 -1
@@ -61,7 +65,21 @@ 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;
+    /*
+     * In case of s390 zlib hardware support, allocate lager workspace
+     * buffer. If allocator fails, fall back to a single page buffer.
+     */
+    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;
   @@ -85,6 +103,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 int 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;

fs/btrfs/zlib.c +62 -27
@@ -102,9 +121,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 +130,51 @@ 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) {
+                int i;
+
+                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;
+                }
+                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,
+                               (unsigned long) workspace->buf_size);
+        }
+
           ret = zlib_deflate(&workspace->strm, Z_SYNC_FLUSH);
           if (ret != Z_OK) {
               pr_debug("BTRFS: deflate in loop returned %d\n",
@@ -161,33 +216,43 @@ int zlib_compress_pages(struct list_head *ws, struct address_space *mapping,
           /* we're all done */
           if (workspace->strm.total_in >= len)
               break;
-
-        /* we've read in a full page, get a new one */
-        if (workspace->strm.avail_in == 0) {
-            if (workspace->strm.total_out > max_out)
-                break;
-
-            bytes_left = len - workspace->strm.total_in;
-            kunmap(in_page);
-            put_page(in_page);
-
-            start += PAGE_SIZE;
-            in_page = find_get_page(mapping,
-                        start >> PAGE_SHIFT);
-            data_in = kmap(in_page);
-            workspace->strm.avail_in = min(bytes_left,
-                               PAGE_SIZE);
-            workspace->strm.next_in = data_in;
-        }
+        if (workspace->strm.total_out > max_out)
+            break;
       }
       workspace->strm.avail_in = 0;
-    ret = zlib_deflate(&workspace->strm, Z_FINISH);
-    zlib_deflateEnd(&workspace->strm);
-
-    if (ret != Z_STREAM_END) {
-        ret = -EIO;
-        goto out;
+    /*
+     * Call deflate with Z_FINISH flush parameter providing more output
+     * space but no more input data, until it returns with Z_STREAM_END.
+     */
+    while (ret != Z_STREAM_END) {
+        ret = zlib_deflate(&workspace->strm, Z_FINISH);
+        if (ret == Z_STREAM_END)
+            break;
+        if (ret != Z_OK && ret != Z_BUF_ERROR) {
+            zlib_deflateEnd(&workspace->strm);
+            ret = -EIO;
+            goto out;
+        } else if (workspace->strm.avail_out == 0) {
+            /* get another page for the stream end */
+            kunmap(out_page);
+            if (nr_pages == nr_dest_pages) {
+                out_page = NULL;
+                ret = -E2BIG;
+                goto out;
Quoted 8 lines

fs/btrfs/zlib.c int zlib_compress_pages(struct list_head *ws, struct address_space *mapping, · line 247

Quoted 6 lines
fs/btrfs/zlib.c +6 -0
+            }
+            out_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
+            if (out_page == NULL) {
+                ret = -ENOMEM;
+                goto out;
+            }

Do we need zlib_deflateEnd() for the above error cases?  Thanks,

The original btrfs code did not call zlib_deflateEnd() for -E2BIG and -ENOMEM cases, so I stick to the same logic.
Unlike userspace zlib where deflateEnd() frees all dynamically allocated memory, in the kernel it doesn't do much apart from setting the return code (since all the memory allocations for kernel zlib are performed in advance).

Quoted 2 lines

Josef

#16 lore raw

Re: [PATCH v4] btrfs: Use larger zlib buffer for s390 hardware compression

Quoted 14 lines · Zaslonko Mikhail
Quoted 14 lines
Quoted 13 lines

+������� } else if (workspace->strm.avail_out == 0) { +����������� /* get another page for the stream end */ +����������� kunmap(out_page); +����������� if (nr_pages == nr_dest_pages) { +��������������� out_page = NULL; +��������������� ret = -E2BIG; +��������������� goto out; +����������� } +����������� out_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM); +����������� if (out_page == NULL) { +��������������� ret = -ENOMEM; +��������������� goto out; +����������� }

Quoted 7 lines
Quoted 1 line

Do we need zlib_deflateEnd() for the above error cases?� Thanks,

The original btrfs code did not call zlib_deflateEnd() for -E2BIG and -ENOMEM cases, so I stick to the same logic.
Unlike userspace zlib where deflateEnd() frees all dynamically allocated memory, in the kernel it doesn't do much apart from setting the return code (since all the memory allocations for kernel zlib are performed in advance).

Agreed, deflateEnd is not necessary in the error cases.

#17 lore raw

Re: [PATCH v4] btrfs: Use larger zlib buffer for s390 hardware compression

Hello David,

Quoted 17 lines · David Sterba
Quoted 16 lines · Zaslonko Mikhail
Quoted 15 lines
Quoted 5 lines

+        } else if (workspace->strm.avail_out == 0) { +            /* get another page for the stream end */ +            kunmap(out_page);
+            if (nr_pages == nr_dest_pages) { +                out_page = NULL;

Quoted 8 lines

+                ret = -E2BIG;
+                goto out;
+            }
+            out_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM); +            if (out_page == NULL) {
+                ret = -ENOMEM;
+                goto out;
+            }

Do we need zlib_deflateEnd() for the above error cases?  Thanks,

Quoted 7 lines
Quoted 5 lines

The original btrfs code did not call zlib_deflateEnd() for -E2BIG and -ENOMEM cases, so I stick to the same logic.
Unlike userspace zlib where deflateEnd() frees all dynamically allocated memory, in the kernel it doesn't do much apart from setting the return code (since all the memory allocations for kernel zlib are performed in advance).

Agreed, deflateEnd is not necessary in the error cases.

Can I consider this as 'Acked-by' from your side? Are there any unanswered questions left on this patch?

Quoted 1 line

Thanks,
Mikhail

#18 lore raw

Re: [PATCH v4] btrfs: Use larger zlib buffer for s390 hardware compression

Quoted 26 lines · Zaslonko Mikhail

Hello David,

Quoted 23 lines · David Sterba
Quoted 21 lines · Zaslonko Mikhail
Quoted 14 lines
Quoted 5 lines

+������� } else if (workspace->strm.avail_out == 0) { +����������� /* get another page for the stream end */ +����������� kunmap(out_page); +����������� if (nr_pages == nr_dest_pages) { +��������������� out_page = NULL;

Quoted 8 lines

+��������������� ret = -E2BIG; +��������������� goto out; +����������� } +����������� out_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM); +����������� if (out_page == NULL) { +��������������� ret = -ENOMEM; +��������������� goto out; +����������� }

Quoted 1 line

Do we need zlib_deflateEnd() for the above error cases?� Thanks,

The original btrfs code did not call zlib_deflateEnd() for -E2BIG and -ENOMEM cases, so I stick to the same logic.
Unlike userspace zlib where deflateEnd() frees all dynamically allocated memory, in the kernel it doesn't do much apart from setting the return code (since all the memory allocations for kernel zlib are performed in advance).

Quoted 3 lines
Quoted 1 line

Agreed, deflateEnd is not necessary in the error cases.

Can I consider this as 'Acked-by' from your side?

Yes, I also sent rev-by to the patch.

Reviewed-by on v4 1/1
#19 lore raw

Re: [PATCH v4] btrfs: Use larger zlib buffer for s390 hardware compression

In reply to Mikhail Zaslonko
Quoted 11 lines · Mikhail Zaslonko

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.
The data compressed with larger input buffers will still conform to zlib standard and thus can be decompressed also on a systems that uses only PAGE_SIZE buffer for btrfs zlib.

Quoted 1 line
author Reviewed-by David Sterba
#20 lore raw

Fwd: Re: [PATCH v4] btrfs: Use larger zlib buffer for s390 hardware compression

Hello Andrew,

Could you please pick this V4 btrfs patch and the other five patches from V3 patch set (https://lkml.org/lkml/2020/1/3/568).

Older patches can be dropped from linux-next:
* ac2aeaa0f4d3 btrfs: use larger zlib buffer for s390 hardware compression
* ac19f15aea33 lib/zlib: add zlib_deflate_dfltcc_enabled() function
* 3447c9a51196 s390/boot: add dfltcc= kernel command line parameter
* 890060df4401 lib/zlib: add s390 hardware support for kernel zlib_inflate
* 148f7d060405 s390/boot: rename HEAP_SIZE due to name collision
* bfbef17740fd lib/zlib: add s390 hardware support for kernel zlib_deflate

Thanks,
Mikhail.

-------- Forwarded Message --------
Subject: Re: [PATCH v4] btrfs: Use larger zlib buffer for s390 hardware compression
Date: Tue, 14 Jan 2020 17:18:57 +0100
From: David Sterba <[email protected]>
Reply-To: [email protected]
To: Mikhail Zaslonko <[email protected]>
CC: Andrew Morton <[email protected]>, Chris Mason <[email protected]>, Josef Bacik <[email protected]>, David Sterba <[email protected]>, Richard Purdie <[email protected]>, Heiko Carstens <[email protected]>, Vasily Gorbik <[email protected]>, Christian Borntraeger <[email protected]>, Eduard Shishkin <[email protected]>, Ilya Leoshkevich <[email protected]>, [email protected], [email protected]

Quoted 11 lines · Mikhail Zaslonko

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.
The data compressed with larger input buffers will still conform to zlib standard and thus can be decompressed also on a systems that uses only PAGE_SIZE buffer for btrfs zlib.

Quoted 1 line