Received: by 2002:ab2:6991:0:b0:1f7:f6c3:9cb1 with SMTP id v17csp336579lqo; Wed, 8 May 2024 00:51:14 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXpBkwdVZWQEgKyKZ0YKJrhhS1jcPSIqCuFg0k5jTWlDinCQMzGw/RY1yjfyJQnwMWaXoe6aPC5LIa5nyM8EJi1setF4Mf4vObfPGIJwg== X-Google-Smtp-Source: AGHT+IEeVPduP84Efq1UgBCB2uchf8kRPwWmF5Hkd9NUZbRPluEOgRZ585RRDTmu5ufNFkuleeow X-Received: by 2002:a05:6e02:1fcd:b0:36c:7f3d:59a with SMTP id e9e14a558f8ab-36caeb9b886mr18565765ab.4.1715154674443; Wed, 08 May 2024 00:51:14 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1715154674; cv=pass; d=google.com; s=arc-20160816; b=mcAJHAuE0Qnzj6oI/kU1g1fmofWK13zF+Wo5kIfGlb5wuMW2siNDA2ymamK+93wdsL vPLrnSLprP3rjVyoEBUcK13vBAHR17PN0rwS2V2YiOrR9+ITGeZTEcSdLt+ie0gwXFoe LkWdXakADZ3NDVAE6aq7w+fRUE3BoBj72m5NcwIxJux5MWr8aJ5nHvV3Re/v8g0YCfGx eYITUMbSY3T7h3yj2i6Dfo0KR2CCzbqTGnPCaPO6OVVWBhzpqEFXC/sdlVI52dcqo+La UWXWgOIxkA0WqLwOwM7J/8jel4b6hCtWILJ9BevOG+IpqwglMQoHHUuWGCrkgIe4csNO nlKA== 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=egL7/ZOm/LwTDOSUeZ3gfevqUUa8V/EYy8mdcUtRQbg=; fh=UKstJvjqAQ3P/Vkvglih02Z8FmmRnlaJbql9clnU9dw=; b=PnhzOOtt4mxke139J+mXaCAU+j55PLpE29jV5jyLCJLiBnMcWY0+NHeqgwG1vZ3IaO Pds/kXj9IrhrsVfGhbIB1pkpkAKA1aEmBeYf2gq8/NItwEKkyu/UFLhCQusFx07pDKSt 8Khyt1VFc0Ukx/CtSD05SWkNTM3LV4QhZQYGUHUdO9+JHBM2olBsffD31i1AarbyD4u3 9v0eRLcpwDMniF75pzWEjLdkttOnVRGEiiFFpCn4TPCl4MoYj0pjQFoPvbF457ivvil2 z7mxOxZEBIv7tcQQNwP6Zr4hWgeVCirzeBM1EOFsZEvLD1se/UvuQGm+WjMbPnVudHy4 0sqA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b="C/fFqpBH"; 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-172846-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-172846-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id z10-20020a63330a000000b0061ceda3d7f0si10426458pgz.527.2024.05.08.00.51.13 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 May 2024 00:51:14 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-172846-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b="C/fFqpBH"; 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-172846-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-172846-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 sy.mirrors.kernel.org (Postfix) with ESMTPS id 3904DB21EF6 for ; Wed, 8 May 2024 07:46:47 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 84F0A76F1D; Wed, 8 May 2024 07:43:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="C/fFqpBH" Received: from mail-pg1-f175.google.com (mail-pg1-f175.google.com [209.85.215.175]) (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 141833E47F for ; Wed, 8 May 2024 07:43:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.175 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715154192; cv=none; b=bh7IHlQSNjqTQFIpi2pcG1FKh8+fOWRMoI37HzBwp5+M7D0Ri17Ia39ddVWC6S/+eqqRbL/RYe/bbuU/VSCDYQzUx5fzx3mTkXnvjTtB+FxbizF3J+JUE917ucXTfoerrZ3TBu3GxuuD1GAzx1KVmYK2ZdVr5rdLrCavjfLuKOQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715154192; c=relaxed/simple; bh=S+q9egEJGrJejpCCNByxOEX2jrCMT0cr23KcD3p6dh4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=NzoYs398ftrFYlljJE3OB+18+rMSv3l2kLLoRmd3PVVoIHoCZ5p0fzQI/Mnj/qSMrfXSa6WhQIDgax87lMzx8iNEOMwS7tMFLIiN0kZPb/xwhz9rvYXdb+DWvRE7zh4uQiCOl0u4OJgoXiA1C5m5x70fsvnhc8opxwxEnl0IXLg= 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=C/fFqpBH; arc=none smtp.client-ip=209.85.215.175 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-pg1-f175.google.com with SMTP id 41be03b00d2f7-5ff57410ebbso3263125a12.1 for ; Wed, 08 May 2024 00:43:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1715154190; x=1715758990; 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=egL7/ZOm/LwTDOSUeZ3gfevqUUa8V/EYy8mdcUtRQbg=; b=C/fFqpBHUFTgOJ+SKvQAlmoH8yvxpTX1JXg+wLvGA9pVuCiL26eEVREbP3x8b+uCtW ifcd6VFQTpK1PWBS0o8/pOff5uXs6aSfz5PvsYiVwtUp0Vlab6VqX9rLcErDcxirChF5 wbBZrTsGI0rjRcFBHte456n9VSKuacQM63HEQ= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715154190; x=1715758990; 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=egL7/ZOm/LwTDOSUeZ3gfevqUUa8V/EYy8mdcUtRQbg=; b=ezuSPB6qZfcwysrXelt0BkSTjJR0FyeM/Iy16reUnJL3VLYS/7jfgLHY0GmZIUlk++ P4IS4LDnooc9Fj1ip24Ta6ikieHCya9pgDWVJ1cluB/8UhNl4Byprd5xEA1OZXXmfiPc 9ZlVXdODrzJufLDr5CFrIpJdcFySPRTm25ZuZg+omlkslMi3Xn7Y1KYFOZCvAHUZzvDo OTpkJMXy8t0PSAQnXz4eG6YbrA1nAEkT2frJDfdKuBAJtr3qvOcvPZge2I0Zp5ndUTdh rWPKHPsMY2i43leTMKJqUlRYcI2VjDu6rzLn8R098gQvSrjJ44dKng+CnYOm25zEwKtE NvRw== X-Gm-Message-State: AOJu0Yzq6XeE9ypQdkMFAv2nRoKx1LgDiZt3A1WUO4H1NFY7n41SUkeE LnjTIX6E2BoRkcHI7ZOK6v/XabY8YK+k/pInJnmp7a54/S8j6Hpj/fmdtKZ0jw== X-Received: by 2002:a17:90a:a592:b0:2a3:be59:e969 with SMTP id 98e67ed59e1d1-2b616c05945mr1560239a91.47.1715154190329; Wed, 08 May 2024 00:43:10 -0700 (PDT) Received: from tigerii.tok.corp.google.com ([2401:fa00:8f:203:ad4d:5f6c:6699:2da4]) by smtp.gmail.com with ESMTPSA id l5-20020a17090aec0500b002b328adaa40sm780011pjy.17.2024.05.08.00.43.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 May 2024 00:43:10 -0700 (PDT) From: Sergey Senozhatsky To: Andrew Morton , Minchan Kim Cc: linux-kernel@vger.kernel.org, linux-block@vger.kernel.org, Sergey Senozhatsky Subject: [PATCHv3 14/19] zram: add dictionary support to zstd backend Date: Wed, 8 May 2024 16:42:07 +0900 Message-ID: <20240508074223.652784-15-senozhatsky@chromium.org> X-Mailer: git-send-email 2.45.0.rc1.225.g2a3ae87e7f-goog In-Reply-To: <20240508074223.652784-1-senozhatsky@chromium.org> References: <20240508074223.652784-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 This adds support for pre-trained zstd dictionaries [1] Dictionary is loaded once (per-config) and then loaded to Cctx and Dctx by reference, so we don't allocate extra memory. Regarding GFP_ATOMIC in Cctx customAlloc(), we probably would want to do something about it. Either make sure that we always (somehow) fully setup all Cctx contexts from non-atomic context before we attempt to use them, come up with some sort of custom allocator or stop calling zcomp_compress() from atomic context. TEST ==== - default zstd /sys/block/zram0/mm_stat 1750315008 504602831 514256896 0 514256896 1 0 34204 34204 - zstd level=7 dict=/etc/dictionary /sys/block/zram0/mm_stat 1750310912 432540606 441712640 0 441712640 1 0 34187 34187 [1] https://github.com/facebook/zstd/blob/dev/programs/zstd.1.md#dictionary-builder Signed-off-by: Sergey Senozhatsky --- drivers/block/zram/backend_zstd.c | 126 ++++++++++++++++++++++++------ 1 file changed, 101 insertions(+), 25 deletions(-) diff --git a/drivers/block/zram/backend_zstd.c b/drivers/block/zram/backend_zstd.c index b2fb94902bef..006d0f40617d 100644 --- a/drivers/block/zram/backend_zstd.c +++ b/drivers/block/zram/backend_zstd.c @@ -12,23 +12,52 @@ struct zstd_ctx { zstd_dctx *dctx; void *cctx_mem; void *dctx_mem; + ZSTD_customMem ctx_mem; + ZSTD_CDict *cdict; + ZSTD_DDict *ddict; s32 level; }; +/* + * Cctx allocator.customAlloc() can be called from zcomp_compress() under + * local-lock (per-CPU compression stream), in which case we must use + * GFP_ATOMIC. + */ +static void *zstd_ctx_alloc(void *opaque, size_t size) +{ + if (!preemptible()) + return kvzalloc(size, GFP_ATOMIC); + + return kvzalloc(size, GFP_KERNEL); +} + +static void zstd_ctx_free(void *opaque, void *address) +{ + kvfree(address); +} + static void zstd_destroy(void *ctx) { struct zstd_ctx *zctx = ctx; - vfree(zctx->cctx_mem); - vfree(zctx->dctx_mem); + if (zctx->cctx_mem) + vfree(zctx->cctx_mem); + else + ZSTD_freeCCtx(zctx->cctx); + + if (zctx->dctx_mem) + vfree(zctx->dctx_mem); + else + ZSTD_freeDCtx(zctx->dctx); + + ZSTD_freeCDict(zctx->cdict); + ZSTD_freeDDict(zctx->ddict); kfree(zctx); } static void *zstd_create(struct zcomp_config *config) { - zstd_parameters params; struct zstd_ctx *ctx; - size_t sz; ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); if (!ctx) @@ -39,24 +68,62 @@ static void *zstd_create(struct zcomp_config *config) else ctx->level = ZSTD_defaultCLevel(); - params = zstd_get_params(ctx->level, PAGE_SIZE); - sz = zstd_cctx_workspace_bound(¶ms.cParams); - ctx->cctx_mem = vzalloc(sz); - if (!ctx->cctx_mem) - goto error; - - ctx->cctx = zstd_init_cctx(ctx->cctx_mem, sz); - if (!ctx->cctx) - goto error; - - sz = zstd_dctx_workspace_bound(); - ctx->dctx_mem = vzalloc(sz); - if (!ctx->dctx_mem) - goto error; - - ctx->dctx = zstd_init_dctx(ctx->dctx_mem, sz); - if (!ctx->dctx) - goto error; + ctx->ctx_mem.customAlloc = zstd_ctx_alloc; + ctx->ctx_mem.customFree = zstd_ctx_free; + + if (config->dict_sz == 0) { + zstd_parameters params; + size_t sz; + + params = zstd_get_params(ctx->level, PAGE_SIZE); + sz = zstd_cctx_workspace_bound(¶ms.cParams); + ctx->cctx_mem = vzalloc(sz); + if (!ctx->cctx_mem) + goto error; + + ctx->cctx = zstd_init_cctx(ctx->cctx_mem, sz); + if (!ctx->cctx) + goto error; + + sz = zstd_dctx_workspace_bound(); + ctx->dctx_mem = vzalloc(sz); + if (!ctx->dctx_mem) + goto error; + + ctx->dctx = zstd_init_dctx(ctx->dctx_mem, sz); + if (!ctx->dctx) + goto error; + } else { + ZSTD_compressionParameters params; + + ctx->cctx = ZSTD_createCCtx_advanced(ctx->ctx_mem); + if (!ctx->cctx) + goto error; + + ctx->dctx = ZSTD_createDCtx_advanced(ctx->ctx_mem); + if (!ctx->dctx) + goto error; + + params = ZSTD_getCParams(ctx->level, PAGE_SIZE, + config->dict_sz); + + ctx->cdict = ZSTD_createCDict_advanced(config->dict, + config->dict_sz, + ZSTD_dlm_byRef, + ZSTD_dct_auto, + params, + ctx->ctx_mem); + if (!ctx->cdict) + goto error; + + ctx->ddict = ZSTD_createDDict_advanced(config->dict, + config->dict_sz, + ZSTD_dlm_byRef, + ZSTD_dct_auto, + ctx->ctx_mem); + if (!ctx->ddict) + goto error; + } return ctx; @@ -72,8 +139,12 @@ static int zstd_compress(void *ctx, const unsigned char *src, const zstd_parameters params = zstd_get_params(zctx->level, PAGE_SIZE); size_t ret; - ret = zstd_compress_cctx(zctx->cctx, dst, *dst_len, - src, PAGE_SIZE, ¶ms); + if (!zctx->cdict) + ret = zstd_compress_cctx(zctx->cctx, dst, *dst_len, + src, PAGE_SIZE, ¶ms); + else + ret = ZSTD_compress_usingCDict(zctx->cctx, dst, *dst_len, + src, PAGE_SIZE, zctx->cdict); if (zstd_is_error(ret)) return -EINVAL; *dst_len = ret; @@ -86,7 +157,12 @@ static int zstd_decompress(void *ctx, const unsigned char *src, size_t src_len, struct zstd_ctx *zctx = ctx; size_t ret; - ret = zstd_decompress_dctx(zctx->dctx, dst, PAGE_SIZE, src, src_len); + if (!zctx->ddict) + ret = zstd_decompress_dctx(zctx->dctx, dst, PAGE_SIZE, + src, src_len); + else + ret = ZSTD_decompress_usingDDict(zctx->dctx, dst, PAGE_SIZE, + src, src_len, zctx->ddict); if (zstd_is_error(ret)) return -EINVAL; return 0; -- 2.45.0.rc1.225.g2a3ae87e7f-goog