Received: by 2002:ab2:6a05:0:b0:1f8:1780:a4ed with SMTP id w5csp3139056lqo; Wed, 15 May 2024 00:17:56 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUp3t1EnQU9p1xKHSfNO2Il21uau5oRxz6ozNhdPcMo2ziycETbaod7eX4GaZPT2KsTmgX3laYDfQh7JYF8EG7PACm1NEAR3E5cpj6OIQ== X-Google-Smtp-Source: AGHT+IGfoajEsUZbYfVCdPGhwY3Z4/3mM9TZy5s5tGfmrxHJ+Qo57bEocqY3EJEPEUadP3srIB5B X-Received: by 2002:a05:620a:171e:b0:792:bf10:3ebf with SMTP id af79cd13be357-792c75ffdd0mr2177871085a.56.1715757476436; Wed, 15 May 2024 00:17:56 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1715757476; cv=pass; d=google.com; s=arc-20160816; b=LCr0/JdNwsYpgbGU4PvhYPrBYewdaO1m3/YcikKrXj5JtsRl1Op6nWChc0z+42EBhK XdJdxsjF5gBVPJiCXpoLk4s2RKBX2dJAsVel6dXoXCwzhwehTjkSUYnw1mqNlDsw9y2V oYQUz9kWfEbf/bi7C4LIQobYbERWUXo/l+4ejxgE/pPxvLqsKRC0Gvs2MWy3vkecgSb7 wCv5letpMuukJcovfs8YhiG4tvJSLUmDWayEC5xwI8XqfEDxmGuHgVv5QJirzQixtQz4 mNI5GUDbRRbOBPJo4NcTnGR00tEo3YPagDP7cTMZJvtFUZdyy99TfdQA5vmAi4P5y7BV fdsg== 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=5y1JEx/byw+/lYPDinjRUJ1Lo0uZERSy7KI3xtlcbP0=; fh=UKstJvjqAQ3P/Vkvglih02Z8FmmRnlaJbql9clnU9dw=; b=TMu7HXzT2k0qIkxru5T6p6SsILcbPMNCFQ7ecafeJzi2E0xH3/85YhPL5JhxooanJH jlQ6uNndVLlzuJgX5O/lKttSSjeI5iKGoB+IYnUZrDwjJHWS32AtrP3ajaA2EHX2R7IY S4Ks66Jf3kpuXkP78OUpmM3UXbdjbQ+HmBUoRYMV0qaJ2WNteq8TZZeEwsyYm9MQAnBk ReRkaBynVS95LKo0KCHLOv7wEawEnT3kdM/t4RseZFLYQE0po4ILC/6kgFU9bOfe4nhX am6eb1b9XQS1pO9h+y3PX2FBFaqJ3LeBNQ5jHel0OD5pr0NGWsZEkFFf83yZ3k2/Fika 1IMA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=Vw+WBCOZ; 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-179554-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-179554-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 af79cd13be357-792bf33f9b3si1284861085a.524.2024.05.15.00.17.56 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 May 2024 00:17:56 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-179554-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=Vw+WBCOZ; 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-179554-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-179554-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 205B31C21CAC for ; Wed, 15 May 2024 07:17:56 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id F30C64E1C9; Wed, 15 May 2024 07:17:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="Vw+WBCOZ" Received: from mail-pl1-f171.google.com (mail-pl1-f171.google.com [209.85.214.171]) (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 9391948CC6 for ; Wed, 15 May 2024 07:17:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.171 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715757426; cv=none; b=S9g3uxoL4iKKPo8PdiW4RDwCzGRcZVZ8yI2pV9tFHf/4myJtVcPp62sRBUMqwoL82PCNEXaKWhC/JzB4PSytJQNtapjOvP9sjrocqXJevJfMMAHHzP3/ldDTP611R8pS0iaZsDh1KZyNENQJp2sZoaH5qPtB4gftCLrwTTqtnYY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715757426; c=relaxed/simple; bh=YnePQvm5yTrRFyDy+nrRta3ObTgqKOo4iqn1FqtURHk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=bM6cak/y9wBS4JhB6vbXh+Jyrpnpx7n6zTwtApJlo49GTuCJ1oB/itllgLGBcbjCCzEjRgujY0haWURvpvPOcoc5zKu2QRhUeW23oZF51hmCj5AO0UqkrdlEP0/4OfOeA65a3iVTUptayiNbjjBqL8+Q7a6H89fYZUg9C7M9lf4= 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=Vw+WBCOZ; arc=none smtp.client-ip=209.85.214.171 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-f171.google.com with SMTP id d9443c01a7336-1ec41d82b8bso61699925ad.2 for ; Wed, 15 May 2024 00:17:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1715757424; x=1716362224; 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=5y1JEx/byw+/lYPDinjRUJ1Lo0uZERSy7KI3xtlcbP0=; b=Vw+WBCOZaKo9SWdEdeLIPfENpcr8UAHjh3BRSzFyrj9WX2igxbRzDaDPXR/1kFESa4 ZKQ7sdIlW3SMwiPk8Tlz94WtGScKDAP7jWLTxCY59b1TySsQhU0ML7ZmZcTy+fOlXHK7 T34iemPWtcpVZxQWJemp/wtuUJR6DPepUVw/E= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715757424; x=1716362224; 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=5y1JEx/byw+/lYPDinjRUJ1Lo0uZERSy7KI3xtlcbP0=; b=Dfrn+LOo4HLk3dDFV37WmbWVeH7GYlkWauswOS1qYqY8J0zBF/Vb0TQlZogcSy7/Ef 5JUo6N3XeJEQ5fiTBvBrj++rA4nRQIz1amDuqPge1YMG697c6FUbWA1a+CN+p4iXghuG /LXbJ+RJuZ188SG4x0R5ehlq+Syf4/ueLtooClfh/cYYF94eqrUnIZni5t6V8DibBXa9 MgX9bThs7NNx96BRzzLLnOeHuRXuyfmoeAcLOuH0nTLiGu4gQYMdEeAYGRFQwx8+yuOG wkla+28j35Fm2DwvBEdTUXBoC72UiySjDzfaF3M8fpOy4tc5sd828EHm4IyNv3BpICJ9 c5zQ== X-Gm-Message-State: AOJu0YwJJru0dHXO80/lGBTv+71Cr3dWaTItURRlRPwr1GxqoBEtD8BI QCVpE4KdlUI9ZxN1sRfaiN273qWwhqbf+c4tblbnw4SiY0r2aHDHwUuNHsmomOUZf/yBpl/cHJM = X-Received: by 2002:a17:902:b111:b0:1eb:d914:64e4 with SMTP id d9443c01a7336-1ef43e25f72mr142390235ad.32.1715757423975; Wed, 15 May 2024 00:17:03 -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.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 May 2024 00:17:03 -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 03/21] zram: add lz4 compression backend support Date: Wed, 15 May 2024 16:12:40 +0900 Message-ID: <20240515071645.1788128-4-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 lz4 compression support. Signed-off-by: Sergey Senozhatsky --- drivers/block/zram/Kconfig | 11 +++++ drivers/block/zram/Makefile | 1 + drivers/block/zram/backend_lz4.c | 73 ++++++++++++++++++++++++++++++++ drivers/block/zram/backend_lz4.h | 10 +++++ drivers/block/zram/zcomp.c | 4 ++ 5 files changed, 99 insertions(+) create mode 100644 drivers/block/zram/backend_lz4.c create mode 100644 drivers/block/zram/backend_lz4.h diff --git a/drivers/block/zram/Kconfig b/drivers/block/zram/Kconfig index 5d329a887b12..f1e76fc8431a 100644 --- a/drivers/block/zram/Kconfig +++ b/drivers/block/zram/Kconfig @@ -20,6 +20,12 @@ config ZRAM_BACKEND_LZO select LZO_COMPRESS select LZO_DECOMPRESS +config ZRAM_BACKEND_LZ4 + bool "lz4 compression support" + depends on ZRAM + select LZ4_COMPRESS + select LZ4_DECOMPRESS + choice prompt "Default zram compressor" default ZRAM_DEF_COMP_LZORLE @@ -33,12 +39,17 @@ config ZRAM_DEF_COMP_LZO bool "lzo" depends on ZRAM_BACKEND_LZO +config ZRAM_DEF_COMP_LZ4 + bool "lz4" + depends on ZRAM_BACKEND_LZ4 + endchoice config ZRAM_DEF_COMP string default "lzo-rle" if ZRAM_DEF_COMP_LZORLE default "lzo" if ZRAM_DEF_COMP_LZO + default "lz4" if ZRAM_DEF_COMP_LZ4 default "unset-value" config ZRAM_WRITEBACK diff --git a/drivers/block/zram/Makefile b/drivers/block/zram/Makefile index 2a3db3368af9..567f4434aee8 100644 --- a/drivers/block/zram/Makefile +++ b/drivers/block/zram/Makefile @@ -3,5 +3,6 @@ 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 obj-$(CONFIG_ZRAM) += zram.o diff --git a/drivers/block/zram/backend_lz4.c b/drivers/block/zram/backend_lz4.c new file mode 100644 index 000000000000..6ea67511d782 --- /dev/null +++ b/drivers/block/zram/backend_lz4.c @@ -0,0 +1,73 @@ +#include +#include +#include +#include + +#include "backend_lz4.h" + +struct lz4_ctx { + void *mem; + s32 level; +}; + +static void lz4_destroy(void *ctx) +{ + struct lz4_ctx *zctx = ctx; + + vfree(zctx->mem); + kfree(zctx); +} + +static void *lz4_create(void) +{ + struct lz4_ctx *ctx; + + ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); + if (!ctx) + return NULL; + + /* @FIXME: using a hardcoded LZ4_ACCELERATION_DEFAULT for now */ + ctx->level = LZ4_ACCELERATION_DEFAULT; + ctx->mem = vmalloc(LZ4_MEM_COMPRESS); + if (!ctx->mem) + goto error; + + return ctx; +error: + lz4_destroy(ctx); + return NULL; +} + +static int lz4_compress(void *ctx, const unsigned char *src, + unsigned char *dst, size_t *dst_len) +{ + struct lz4_ctx *zctx = ctx; + int ret; + + ret = LZ4_compress_fast(src, dst, PAGE_SIZE, *dst_len, + zctx->level, zctx->mem); + if (!ret) + return -EINVAL; + *dst_len = ret; + return 0; +} + +static int lz4_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_lz4 = { + .compress = lz4_compress, + .decompress = lz4_decompress, + .create_ctx = lz4_create, + .destroy_ctx = lz4_destroy, + .name = "lz4", +}; diff --git a/drivers/block/zram/backend_lz4.h b/drivers/block/zram/backend_lz4.h new file mode 100644 index 000000000000..a5fb5564835c --- /dev/null +++ b/drivers/block/zram/backend_lz4.h @@ -0,0 +1,10 @@ +// SPDX-License-Identifier: GPL-2.0-or-later + +#ifndef __BACKEND_LZ4_H__ +#define __BACKEND_LZ4_H__ + +#include "zcomp.h" + +extern struct zcomp_backend backend_lz4; + +#endif /* __BACKEND_LZ4_H__ */ diff --git a/drivers/block/zram/zcomp.c b/drivers/block/zram/zcomp.c index 58fb3ac91f4b..902bdaf7e299 100644 --- a/drivers/block/zram/zcomp.c +++ b/drivers/block/zram/zcomp.c @@ -17,11 +17,15 @@ #include "backend_lzo.h" #include "backend_lzorle.h" +#include "backend_lz4.h" static struct zcomp_backend *backends[] = { #if IS_ENABLED(CONFIG_ZRAM_BACKEND_LZO) &backend_lzorle, &backend_lzo, +#endif +#if IS_ENABLED(CONFIG_ZRAM_BACKEND_LZ4) + &backend_lz4, #endif NULL }; -- 2.45.0.rc1.225.g2a3ae87e7f-goog