Received: by 2002:a05:6500:1b8f:b0:1fa:5c73:8e2d with SMTP id df15csp1174349lqb; Thu, 30 May 2024 02:31:28 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWPLkMS9F6IyR4TVg0ye/6KhusSm0ZwG7VLAdpCt9NwSB9ObSUb12soCHudXD6YTyvoLlcQCSWDC+BmK1h93QA6L2JEdNC6zoLETht24A== X-Google-Smtp-Source: AGHT+IF0HbryfFdhWMegC1pcxOu2tZzrhJLpBrAcm5+4j6XqEEa6UNXHKxxOpF7dGLwgFGl+5oRS X-Received: by 2002:a17:903:32cb:b0:1f4:89e4:b60c with SMTP id d9443c01a7336-1f619630925mr23229405ad.31.1717061487873; Thu, 30 May 2024 02:31:27 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1717061487; cv=pass; d=google.com; s=arc-20160816; b=nscppr8cGlAbzDt3TF7j4v4a0AYsA+j40hShCOTYJ84De2Xa3pwvakXaGgAJsyu2Ar 2KblElCMuhne/MdbaqEDcmhKl8J5ibLiXueIF6trbDYVe4QE89GNc17yfnkaJkal+Jet dQ+ikiMkNOxXUROyAwdJhmmcZscSxEOS+ZCjrwndkb+0yF+mypOu+yLo2BIHz+/jA51o nlAi5TljgaQPiG9oobJ0UEHL+k/5E4XxtcL4wUgLHB0HTIAiIrHZ+fkwUJ9uEGDZ8x9V oQs512ESbx4Q+lJeyM3Df5+UePjjafNqK2X8i6keoEr0IoFDDkbwwrABFK6a4Xh2m8BB cWbQ== 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=KSowKF1yk2HrBD5wWxaTcKui5ikFhcGrTJRBSctfjpk=; fh=d5RNBTGH+8NLy8IkaJakkFJ05GBmRAoAt0jVG0pBMUA=; b=Dbp3rm2xgvCFaZ+nhjfxQbRAH7w6ngGPPEwB0jQ8qdJfM3W+J0vfDwlkIcdVSlWvfi gckRGomV0XDcSuhdDX00YxxLdS6ttVPrs/dFyOjmQAYGvMoj/KvdVv8VUg81FaS3n5CX RygXMcB5SeW/zQY0kpVrXJCaDPXlykUuF5BXFERWmj1WYOVdGQyQ+CWs9KIGRKId2nrX E5x25tTDaqcnNltt4Hh4qouPdiqduc0V+TkPdlFq9wgge5sdFuUOSdkKEkH5UTds+fze 7JEOLCgrfbsjOnzYVesLqcR8LB6Ez+sG4aUWIZtbfqjU+bDiWIaGhKYBaX9AzrdZIhn7 iWPw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linux.alibaba.com header.s=default header.b="JH07/8pB"; arc=pass (i=1 spf=pass spfdomain=linux.alibaba.com dkim=pass dkdomain=linux.alibaba.com dmarc=pass fromdomain=linux.alibaba.com); spf=pass (google.com: domain of linux-kernel+bounces-195159-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-195159-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.alibaba.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id d9443c01a7336-1f483584ab5si85200125ad.368.2024.05.30.02.31.27 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 May 2024 02:31:27 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-195159-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.alibaba.com header.s=default header.b="JH07/8pB"; arc=pass (i=1 spf=pass spfdomain=linux.alibaba.com dkim=pass dkdomain=linux.alibaba.com dmarc=pass fromdomain=linux.alibaba.com); spf=pass (google.com: domain of linux-kernel+bounces-195159-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-195159-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.alibaba.com 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 59AF4B25B6D for ; Thu, 30 May 2024 09:23:20 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id EFC8E176AC5; Thu, 30 May 2024 09:22:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b="JH07/8pB" Received: from out30-131.freemail.mail.aliyun.com (out30-131.freemail.mail.aliyun.com [115.124.30.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6F8A46F313; Thu, 30 May 2024 09:22:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=115.124.30.131 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717060937; cv=none; b=TcYlCJobyrseNkRvng4o6sZuUHPj+LkjN4135fQIb+nnbji4K6AQ+eu7T+439GrC88sw/lYbvpafI0skOq6v20RZVQpJXJ/0LV4uukagxKbt3JwMC9f87HzuJPU/zbVRPVd+R1XCXuxOpqza2lK9d4/CVdcPTpW7jqwuBHOE2qg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717060937; c=relaxed/simple; bh=PH7lguH/SoU2pDjlCsNDNkRAdVHrJ1mLAeNQcTzlXa4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=DY34nGUhmU1+r0NtXCWv69OGJxXjbzBJIi62Ia94bmXgLXCor2uRT0+wjn2LtNwjTTL6ZDp18M5XQJJ5YEEe+scO0BaFJtvH+H9SjZmOXHoo4vAulprWZ060O0b6lq9KrpOQ4B8NyONpkr3fduZkAiFrF4pfcy2DGyCormdR+0w= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com; spf=pass smtp.mailfrom=linux.alibaba.com; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b=JH07/8pB; arc=none smtp.client-ip=115.124.30.131 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.alibaba.com DKIM-Signature:v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1717060931; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=KSowKF1yk2HrBD5wWxaTcKui5ikFhcGrTJRBSctfjpk=; b=JH07/8pBYGsVwzAz0GaF4LXVRxaYGJ/EZ5qspveSjLDScwrFhSTJeQAcW/Xw7G65Zx1nMYQ7Y3EwjipebP9DKG6Sk4vud7H2VV9yEMTtAq+rPgqIMWkSy1wl7l1RVSLvfIPFF19NgxUhTMtaJx9dkeATpUI3My68cXGwRFJAdbA= X-Alimail-AntiSpam:AC=PASS;BC=-1|-1;BR=01201311R161e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=maildocker-contentspam033037067110;MF=hsiangkao@linux.alibaba.com;NM=1;PH=DS;RN=7;SR=0;TI=SMTPD_---0W7WbjNt_1717060930; Received: from x31i01179.sqa.na131.tbsite.net(mailfrom:hsiangkao@linux.alibaba.com fp:SMTPD_---0W7WbjNt_1717060930) by smtp.aliyun-inc.com; Thu, 30 May 2024 17:22:11 +0800 From: Gao Xiang To: stable@vger.kernel.org, Greg Kroah-Hartman Cc: linux-erofs@lists.ozlabs.org, Baokun Li , LKML , Gao Xiang , Sandeep Dhavale Subject: [PATCH 6.6.y] erofs: avoid allocating DEFLATE streams before mounting Date: Thu, 30 May 2024 17:22:01 +0800 Message-Id: <20240530092201.16873-3-hsiangkao@linux.alibaba.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20240530092201.16873-1-hsiangkao@linux.alibaba.com> References: <20240530092201.16873-1-hsiangkao@linux.alibaba.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit commit 80eb4f62056d6ae709bdd0636ab96ce660f494b2 upstream. Currently, each DEFLATE stream takes one 32 KiB permanent internal window buffer even if there is no running instance which uses DEFLATE algorithm. It's unexpected and wasteful on embedded devices with limited resources and servers with hundreds of CPU cores if DEFLATE is enabled but unused. Fixes: ffa09b3bd024 ("erofs: DEFLATE compression support") Cc: # 6.6+ Reviewed-by: Sandeep Dhavale Link: https://lore.kernel.org/r/20240520090106.2898681-1-hsiangkao@linux.alibaba.com [ Gao Xiang: resolve trivial conflicts. ] Signed-off-by: Gao Xiang --- fs/erofs/decompressor_deflate.c | 55 +++++++++++++++++---------------- 1 file changed, 29 insertions(+), 26 deletions(-) diff --git a/fs/erofs/decompressor_deflate.c b/fs/erofs/decompressor_deflate.c index 0e1946a6bda5..aac2c837ef35 100644 --- a/fs/erofs/decompressor_deflate.c +++ b/fs/erofs/decompressor_deflate.c @@ -47,39 +47,15 @@ int __init z_erofs_deflate_init(void) /* by default, use # of possible CPUs instead */ if (!z_erofs_deflate_nstrms) z_erofs_deflate_nstrms = num_possible_cpus(); - - for (; z_erofs_deflate_avail_strms < z_erofs_deflate_nstrms; - ++z_erofs_deflate_avail_strms) { - struct z_erofs_deflate *strm; - - strm = kzalloc(sizeof(*strm), GFP_KERNEL); - if (!strm) - goto out_failed; - - /* XXX: in-kernel zlib cannot shrink windowbits currently */ - strm->z.workspace = vmalloc(zlib_inflate_workspacesize()); - if (!strm->z.workspace) { - kfree(strm); - goto out_failed; - } - - spin_lock(&z_erofs_deflate_lock); - strm->next = z_erofs_deflate_head; - z_erofs_deflate_head = strm; - spin_unlock(&z_erofs_deflate_lock); - } return 0; - -out_failed: - pr_err("failed to allocate zlib workspace\n"); - z_erofs_deflate_exit(); - return -ENOMEM; } int z_erofs_load_deflate_config(struct super_block *sb, struct erofs_super_block *dsb, void *data, int size) { struct z_erofs_deflate_cfgs *dfl = data; + static DEFINE_MUTEX(deflate_resize_mutex); + static bool inited; if (!dfl || size < sizeof(struct z_erofs_deflate_cfgs)) { erofs_err(sb, "invalid deflate cfgs, size=%u", size); @@ -90,9 +66,36 @@ int z_erofs_load_deflate_config(struct super_block *sb, erofs_err(sb, "unsupported windowbits %u", dfl->windowbits); return -EOPNOTSUPP; } + mutex_lock(&deflate_resize_mutex); + if (!inited) { + for (; z_erofs_deflate_avail_strms < z_erofs_deflate_nstrms; + ++z_erofs_deflate_avail_strms) { + struct z_erofs_deflate *strm; + + strm = kzalloc(sizeof(*strm), GFP_KERNEL); + if (!strm) + goto failed; + /* XXX: in-kernel zlib cannot customize windowbits */ + strm->z.workspace = vmalloc(zlib_inflate_workspacesize()); + if (!strm->z.workspace) { + kfree(strm); + goto failed; + } + spin_lock(&z_erofs_deflate_lock); + strm->next = z_erofs_deflate_head; + z_erofs_deflate_head = strm; + spin_unlock(&z_erofs_deflate_lock); + } + inited = true; + } + mutex_unlock(&deflate_resize_mutex); erofs_info(sb, "EXPERIMENTAL DEFLATE feature in use. Use at your own risk!"); return 0; +failed: + mutex_unlock(&deflate_resize_mutex); + z_erofs_deflate_exit(); + return -ENOMEM; } int z_erofs_deflate_decompress(struct z_erofs_decompress_req *rq, -- 2.39.3