Received: by 2002:ab2:6a05:0:b0:1f8:1780:a4ed with SMTP id w5csp3139170lqo; Wed, 15 May 2024 00:18:13 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUBE6+AzPMLSR5XUMwZsSAC6pJRTV/iYKoAoBD2OMkJAz0N7oToakYcAtOcarRqExd6m/aS24In46QYMknku3rZ14ACmsVuFKsKlu946A== X-Google-Smtp-Source: AGHT+IGRmgswuM/zaA5BNhkd/CvYcH3TbzCH5mMpz9RLE3ZlI3fwfX0Id9BfbdJL0qrIHTtrve86 X-Received: by 2002:ac8:5f49:0:b0:43d:dc3c:3c5e with SMTP id d75a77b69052e-43dfdb07843mr188218821cf.1.1715757493594; Wed, 15 May 2024 00:18:13 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1715757493; cv=pass; d=google.com; s=arc-20160816; b=BlvzBzbhjWOU+7BiD/NVq82Jkj5Pvlc15q4T8fZILeJYVYwNvsFGWiYisgK5jss5X2 zYLVMvEe0/4gDgMGy+XNQ/hFODcNzDAKE3+9Q9aJHy4bcGVt7Y5XoFqPRKTnMi6fO2F8 2y7zEOGvdraBVh8txSE4I4q3kIl04z2QAduwwk+/OiyPX0+qlsmXhWZgI2APgMKiTnZV wJv0VVsJuTMyDoBzSCgfmAwORpDT1yB1CeRoigLwLSz24kl5jfosU0bjeFPL4J0wzBhT GxMVUT+Xuj9NTgfWjIgc7YZh4NCjnDBGFC4kodBPdwgOssH/U+6sNYVuo65ceP2dkdd9 WxEg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=i/3U4lC6hcVdfnHTL8Mc0HJ1E1QNWG67FTnBC8OLuFc=; fh=UKstJvjqAQ3P/Vkvglih02Z8FmmRnlaJbql9clnU9dw=; b=rF0B74hKJEVkg62nIuLA6/1X++46aYrrcQVyIFvjmEeGODDU/lkilSN/rgcRUxbS3k /AI4BvZ+goW7fCUF9b8U2Gfl57oQqJROhyb4gvMGYZwgoTTjntdjL0BXmdy+0m7wF0Op Ej2jDRRQ6BYMAM2SWev1/HzgLoyhNNyUEcDNQGWBfkdPLCuc0V2HxDeFyQYmtrCjyEcR xKqLWpVEpR8N1oM7LlECEYDWUK1ZX8h45b4pBLa8rS9XYvFwcz2zUcfN7jHyl2NEIgBz alNPGRkvASO/eX5JtnoEjKAIJeC9CjL3Lli0kLE8LIYfrZIC0ZVqKcpeXYWoNBj3ZQf4 e3Mg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=TqKtLYPJ; arc=pass (i=1 spf=pass spfdomain=chromium.org dkim=pass dkdomain=chromium.org dmarc=pass fromdomain=chromium.org); spf=pass (google.com: domain of linux-kernel+bounces-179555-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-179555-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id d75a77b69052e-43e10966e68si79974311cf.535.2024.05.15.00.18.13 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 May 2024 00:18:13 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-179555-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=TqKtLYPJ; arc=pass (i=1 spf=pass spfdomain=chromium.org dkim=pass dkdomain=chromium.org dmarc=pass fromdomain=chromium.org); spf=pass (google.com: domain of linux-kernel+bounces-179555-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-179555-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 4869C1C21CBC for ; Wed, 15 May 2024 07:18:13 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6000450278; Wed, 15 May 2024 07:17:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="TqKtLYPJ" Received: from mail-pl1-f172.google.com (mail-pl1-f172.google.com [209.85.214.172]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F2E1C4E1C3 for ; Wed, 15 May 2024 07:17:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.172 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715757428; cv=none; b=FHnxVLkax5CTgQKpgSiwG7zkUpJ7V2GZcsS/goqEqGZIKC0YjTTLI1AzOYRtruVKFA7HoA6/DinFOeZ9oJam76NAHPQ5yXVzsd+DNThtd+FlF3K1Y5BNh8aWhcDC3GEH3odSd2HiFjw0j4bywzECTETkV6/EMiGrO9buPcSOWyI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715757428; c=relaxed/simple; bh=QIqKpR6W+iYpBKRdE8LyZOci6XU5F9QsGlMydzcKw90=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=GOjIpW6KDxiNu+49WFKyBxuZkDJieOmI5LFZJNpDJSXbLwNB47TGJ5BhE0U8fbF9Eq/ri0/+eQcfZri5m3ElzfEt5CdnFNbrEslBtX5UhdGWB7OLHlaZyLAE0eRN0J01rO/mb6HkWRIQUp+/2a3Z9yu33cS+AAr46K97AMmadmQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=TqKtLYPJ; arc=none smtp.client-ip=209.85.214.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Received: by mail-pl1-f172.google.com with SMTP id d9443c01a7336-1ecd3867556so50280145ad.0 for ; Wed, 15 May 2024 00:17:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1715757426; x=1716362226; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=i/3U4lC6hcVdfnHTL8Mc0HJ1E1QNWG67FTnBC8OLuFc=; b=TqKtLYPJJzfGx3mpymohFTwUklUkkKIHSdVKWhJAsSz8aW4dOO+OaKam8x6KfC889x NLSXtm+OXQDkNaOmG0+PcXXlX6ca+RvBY7ej8nMVcNddFoGd/CNY5Tl1TQGv8BlWn/RN 1btDHINGL6t0k0+NDlPVD0D80Zvv9iIzl6og0= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715757426; x=1716362226; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=i/3U4lC6hcVdfnHTL8Mc0HJ1E1QNWG67FTnBC8OLuFc=; b=GiWqxuTw/AcZPyiUVr+S9pGEiH3sWGNeOv8DkIAY/rr27dpfpEgxWcHuS63h83UFDb pa6Ng5NK6Yp2OOwgthERfJG1bj+iqKhMZoipTpaS1wWHaC2AkaB9+ImAwtzIcOgdWB7x d9lSUT4w4TA1KsINn1EdhTJgO5PUH4wgd/NbaHVhIirIjWqSounElRIL32J4HJXgTsJl 5bib6tfN1QHrtjYe5/+EuRMfeBIHdUSIjLVtOV09p3AAOKRvzdkHFr9GEi2+Gl9KB28j WvDXjWYUhHXidI0IyuHL3HXiYBUo8mOWvAIrmRAZpszRRyZ3Ha3YbxsWVuH/MHRR0Lc9 H3ig== X-Gm-Message-State: AOJu0YyLev6FO3se7S4xmp7g3QGTXR62RN7seXOCBU/PHmymo0mL5RLw CJezd1lB/PSRE1VMfk/giq9s3VSPI5GeWe+GXG0ab1jygQ9GbZUXfUbIRBzuwg== X-Received: by 2002:a17:902:ec89:b0:1eb:58d2:8739 with SMTP id d9443c01a7336-1ef43d0a009mr183267855ad.3.1715757426418; Wed, 15 May 2024 00:17:06 -0700 (PDT) Received: from tigerii.tok.corp.google.com ([2401:fa00:8f:203:111d:a618:3172:cd5b]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1ef0c136d53sm110941605ad.254.2024.05.15.00.17.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 May 2024 00:17:06 -0700 (PDT) From: Sergey Senozhatsky To: Andrew Morton , Minchan Kim Cc: linux-kernel@vger.kernel.org, linux-block@vger.kernel.org, Sergey Senozhatsky Subject: [PATCHv4 04/21] zram: add lz4hc compression backend support Date: Wed, 15 May 2024 16:12:41 +0900 Message-ID: <20240515071645.1788128-5-senozhatsky@chromium.org> X-Mailer: git-send-email 2.45.0.rc1.225.g2a3ae87e7f-goog In-Reply-To: <20240515071645.1788128-1-senozhatsky@chromium.org> References: <20240515071645.1788128-1-senozhatsky@chromium.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Add s/w lz4hc compression support. Signed-off-by: Sergey Senozhatsky --- drivers/block/zram/Kconfig | 11 +++++ drivers/block/zram/Makefile | 5 +- drivers/block/zram/backend_lz4hc.c | 73 ++++++++++++++++++++++++++++++ drivers/block/zram/backend_lz4hc.h | 10 ++++ drivers/block/zram/zcomp.c | 4 ++ 5 files changed, 101 insertions(+), 2 deletions(-) create mode 100644 drivers/block/zram/backend_lz4hc.c create mode 100644 drivers/block/zram/backend_lz4hc.h diff --git a/drivers/block/zram/Kconfig b/drivers/block/zram/Kconfig index f1e76fc8431a..0d890a8d2dc4 100644 --- a/drivers/block/zram/Kconfig +++ b/drivers/block/zram/Kconfig @@ -26,6 +26,12 @@ config ZRAM_BACKEND_LZ4 select LZ4_COMPRESS select LZ4_DECOMPRESS +config ZRAM_BACKEND_LZ4HC + bool "lz4hc compression support" + depends on ZRAM + select LZ4HC_COMPRESS + select LZ4_DECOMPRESS + choice prompt "Default zram compressor" default ZRAM_DEF_COMP_LZORLE @@ -43,6 +49,10 @@ config ZRAM_DEF_COMP_LZ4 bool "lz4" depends on ZRAM_BACKEND_LZ4 +config ZRAM_DEF_COMP_LZ4HC + bool "lz4hc" + depends on ZRAM_BACKEND_LZ4HC + endchoice config ZRAM_DEF_COMP @@ -50,6 +60,7 @@ config ZRAM_DEF_COMP default "lzo-rle" if ZRAM_DEF_COMP_LZORLE default "lzo" if ZRAM_DEF_COMP_LZO default "lz4" if ZRAM_DEF_COMP_LZ4 + default "lz4hc" if ZRAM_DEF_COMP_LZ4HC default "unset-value" config ZRAM_WRITEBACK diff --git a/drivers/block/zram/Makefile b/drivers/block/zram/Makefile index 567f4434aee8..727c9d68e3e3 100644 --- a/drivers/block/zram/Makefile +++ b/drivers/block/zram/Makefile @@ -2,7 +2,8 @@ zram-y := zcomp.o zram_drv.o -zram-$(CONFIG_ZRAM_BACKEND_LZO) += backend_lzorle.o backend_lzo.o -zram-$(CONFIG_ZRAM_BACKEND_LZ4) += backend_lz4.o +zram-$(CONFIG_ZRAM_BACKEND_LZO) += backend_lzorle.o backend_lzo.o +zram-$(CONFIG_ZRAM_BACKEND_LZ4) += backend_lz4.o +zram-$(CONFIG_ZRAM_BACKEND_LZ4HC) += backend_lz4hc.o obj-$(CONFIG_ZRAM) += zram.o diff --git a/drivers/block/zram/backend_lz4hc.c b/drivers/block/zram/backend_lz4hc.c new file mode 100644 index 000000000000..9bcc5aa19a2b --- /dev/null +++ b/drivers/block/zram/backend_lz4hc.c @@ -0,0 +1,73 @@ +#include +#include +#include +#include + +#include "backend_lz4hc.h" + +struct lz4hc_ctx { + void *mem; + s32 level; +}; + +static void lz4hc_destroy(void *ctx) +{ + struct lz4hc_ctx *zctx = ctx; + + vfree(zctx->mem); + kfree(zctx); +} + +static void *lz4hc_create(void) +{ + struct lz4hc_ctx *ctx; + + ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); + if (!ctx) + return NULL; + + /* @FIXME: using a hardcoded LZ4HC_DEFAULT_CLEVEL for now */ + ctx->level = LZ4HC_DEFAULT_CLEVEL; + ctx->mem = vmalloc(LZ4HC_MEM_COMPRESS); + if (!ctx->mem) + goto error; + + return ctx; +error: + lz4hc_destroy(ctx); + return NULL; +} + +static int lz4hc_compress(void *ctx, const unsigned char *src, + unsigned char *dst, size_t *dst_len) +{ + struct lz4hc_ctx *zctx = ctx; + int ret; + + ret = LZ4_compress_HC(src, dst, PAGE_SIZE, *dst_len, + zctx->level, zctx->mem); + if (!ret) + return -EINVAL; + *dst_len = ret; + return 0; +} + +static int lz4hc_decompress(void *ctx, const unsigned char *src, + size_t src_len, unsigned char *dst) +{ + int dst_len = PAGE_SIZE; + int ret; + + ret = LZ4_decompress_safe(src, dst, src_len, dst_len); + if (ret < 0) + return -EINVAL; + return 0; +} + +struct zcomp_backend backend_lz4hc = { + .compress = lz4hc_compress, + .decompress = lz4hc_decompress, + .create_ctx = lz4hc_create, + .destroy_ctx = lz4hc_destroy, + .name = "lz4hc", +}; diff --git a/drivers/block/zram/backend_lz4hc.h b/drivers/block/zram/backend_lz4hc.h new file mode 100644 index 000000000000..29c428a850e2 --- /dev/null +++ b/drivers/block/zram/backend_lz4hc.h @@ -0,0 +1,10 @@ +// SPDX-License-Identifier: GPL-2.0-or-later + +#ifndef __BACKEND_LZ4HC_H__ +#define __BACKEND_LZ4HC_H__ + +#include "zcomp.h" + +extern struct zcomp_backend backend_lz4hc; + +#endif /* __BACKEND_LZ4HC_H__ */ diff --git a/drivers/block/zram/zcomp.c b/drivers/block/zram/zcomp.c index 902bdaf7e299..f04f5844a23c 100644 --- a/drivers/block/zram/zcomp.c +++ b/drivers/block/zram/zcomp.c @@ -18,6 +18,7 @@ #include "backend_lzo.h" #include "backend_lzorle.h" #include "backend_lz4.h" +#include "backend_lz4hc.h" static struct zcomp_backend *backends[] = { #if IS_ENABLED(CONFIG_ZRAM_BACKEND_LZO) @@ -26,6 +27,9 @@ static struct zcomp_backend *backends[] = { #endif #if IS_ENABLED(CONFIG_ZRAM_BACKEND_LZ4) &backend_lz4, +#endif +#if IS_ENABLED(CONFIG_ZRAM_BACKEND_LZ4HC) + &backend_lz4hc, #endif NULL }; -- 2.45.0.rc1.225.g2a3ae87e7f-goog