Received: by 2002:a05:7412:b995:b0:f9:9502:5bb8 with SMTP id it21csp3198566rdb; Tue, 26 Dec 2023 22:11:33 -0800 (PST) X-Google-Smtp-Source: AGHT+IFxOmCnpsnfD5H+amSOmCB7+/7lCU1nVZe7AXAPIvVcqcu1gPDblwYtGO+HZF3h7gs28EX2 X-Received: by 2002:ac8:7d10:0:b0:427:7814:f60b with SMTP id g16-20020ac87d10000000b004277814f60bmr10183977qtb.81.1703657493276; Tue, 26 Dec 2023 22:11:33 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1703657493; cv=none; d=google.com; s=arc-20160816; b=mbNYR3vaX7aBd7lBhXOqPjv159MMNyXrM1HkOXh0eaMPiU1a1qmd4xU8TxycGjwM+V 5ZjeeAz6ADvvEhxR5um9LdG9LDBWrrl+x8KyEV61G+ctU2RgBjMO9fZbhr33yUklCUak kMGza0kDrs3gRe3bVwcDyPg+ffHAXlltxQQb/phPwHMeSFa2Rcou2lRdDXEL1go99MN8 2d4YUA3BtTeG9jlbk/qbEM8XExj6yhYV+HeLeue2SD3zG/0FPt6IaF4oObnW1i92Pop/ pgcWNPBEfk2s+a0tZ5jisiorgk0uEAJrMChS2cMVBklCPDiWNY9X0j2ascbQ9xo5xDXf JM/w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:message-id:dkim-signature; bh=hk2kRuZJzsTncYz8diW0amRVuQLlRcJy88VPv7kReGA=; fh=V0MmOoxTVEYqnNMJ4WP2313b7Ecswq3o5pulBGF++qs=; b=eGfxpJIVGdDpYZ1KxVVXMm8YGkLi2GbLU/3YTdlenUhJ+wlaUwQShae0TrkNuK7Cv1 PPZYCR4qIISCTsOGjl/4T6iWRYFBwvRQNvYQjux4oe2t8rBAYi6u7PYYBDOABc8hoUFi UR5SuHCHxeiLmVEqAUpPtQ1Am+VuCOQEii0oQyt3r3cQC3ul9ZLZCc2MiU2wWKV6LKHQ B5BrI2Pjy+kD/XWP7U/DVlJ2SdaR7PjvU2VTPntUmzcnfU+K5XgFXvlCNP3F/cHhGNL/ Fpqg/XOJqmGcJ+191MMUvam+9WqO494lFHhvaUD3M8PMTeUf5fRsXC9GNIZE6fUDhTFO 5evA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bytedance.com header.s=google header.b=BG62qPNp; spf=pass (google.com: domain of linux-kernel+bounces-11848-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-11848-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=bytedance.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id i16-20020ac85e50000000b00417b523872dsi13291424qtx.621.2023.12.26.22.11.33 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 26 Dec 2023 22:11:33 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-11848-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@bytedance.com header.s=google header.b=BG62qPNp; spf=pass (google.com: domain of linux-kernel+bounces-11848-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-11848-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=bytedance.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 ny.mirrors.kernel.org (Postfix) with ESMTPS id EFCC01C2165C for ; Wed, 27 Dec 2023 06:11:32 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 28D0B6134; Wed, 27 Dec 2023 06:11:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b="BG62qPNp" X-Original-To: linux-kernel@vger.kernel.org Received: from mail-pg1-f174.google.com (mail-pg1-f174.google.com [209.85.215.174]) (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 67C8D5CB5 for ; Wed, 27 Dec 2023 06:11:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bytedance.com Received: by mail-pg1-f174.google.com with SMTP id 41be03b00d2f7-5ce0656a7bdso213169a12.3 for ; Tue, 26 Dec 2023 22:11:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1703657473; x=1704262273; darn=vger.kernel.org; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=hk2kRuZJzsTncYz8diW0amRVuQLlRcJy88VPv7kReGA=; b=BG62qPNpG+znh4rwLdgWLOpoOfGvQGuTjf7OzOx0RZUA2WKGrDFMZuSzVbXGPwJ/VO X3SIc/U4ADdPLy5s6Ggle4d9UPwVXG+niCfFphD4OX6U4m6xA1qLGB4agyUsp2YyNayb 7pgCuyqpR+71IeNyvhWgO4MJ+fF5oSbDokJ7cC17APFHnMLP1qvzl/HO86+k7V9OQJZT 95yGQ25h8nQ2x0zlB9DmH5nBiyKWYESPNwjoqN7S8hyWHaYX9dD39u4IA8/cfVLcBltx y2Y3i6CqxVq5wJ9OdIpEy8jhnUtJPUjZDYsohuj1DkmUC14+fgw/jnVhYiVSSph/5V1M 5DSg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1703657473; x=1704262273; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=hk2kRuZJzsTncYz8diW0amRVuQLlRcJy88VPv7kReGA=; b=kSdu1/p+haoYnAavddfJg2fc94KO9RHjwGzGPUAaPKsX/ErpZY3jYUXjkd7c64AtVj Nq3ca2FT4cQpxUlGML7gyhKXwuYTH00EtnpWS4l4Jei5uftcFA9csfrWm0pWh2x0auRb zxjOJHidCO1P1LSnayED6AMaavtrtnvvx2WPHfzlsNWHFiaWcd5cMeeyI3/u3H4tBpgH nwOd66RmfR+cO+R7p6JNVXTiFd57xtxbVXUtcFFWYCsV8F7r7zzmKsLt+JafXv760XqX K/TLHTlGJKGpfgJzG3ZckyJUkVK8rRQBRupOuWubkeMruhrvd9WFolLTgd8MMaUWXQKU pNTA== X-Gm-Message-State: AOJu0YwX3cVbIhRLLfar/i3DSNocJ4Adh5EGh6GtUT2bRR0BNeyANb97 p0IvCP1BMB75vzd8mMLuRFwdmP/K+tttHQ== X-Received: by 2002:a05:6a21:a5a8:b0:195:e20b:f02c with SMTP id gd40-20020a056a21a5a800b00195e20bf02cmr835139pzc.98.1703657473489; Tue, 26 Dec 2023 22:11:13 -0800 (PST) Received: from [10.254.10.159] ([139.177.225.237]) by smtp.gmail.com with ESMTPSA id jb2-20020a170903258200b001d0c5037ed3sm11155478plb.46.2023.12.26.22.11.09 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 26 Dec 2023 22:11:13 -0800 (PST) Message-ID: Date: Wed, 27 Dec 2023 14:11:06 +0800 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v4 1/6] mm/zswap: change dstmem size to one page Content-Language: en-US To: Barry Song <21cnbao@gmail.com> Cc: Andrew Morton , Seth Jennings , Johannes Weiner , Vitaly Wool , Nhat Pham , Chris Li , Yosry Ahmed , Dan Streetman , linux-kernel@vger.kernel.org, linux-mm@kvack.org, Chris Li References: <20231213-zswap-dstmem-v4-0-f228b059dd89@bytedance.com> <20231213-zswap-dstmem-v4-1-f228b059dd89@bytedance.com> From: Chengming Zhou In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit On 2023/12/27 09:07, Barry Song wrote: > On Wed, Dec 27, 2023 at 4:55 AM Chengming Zhou > wrote: >> >> Change the dstmem size from 2 * PAGE_SIZE to only one page since >> we only need at most one page when compress, and the "dlen" is also >> PAGE_SIZE in acomp_request_set_params(). If the output size > PAGE_SIZE >> we don't wanna store the output in zswap anyway. >> >> So change it to one page, and delete the stale comment. >> >> There is no any history about the reason why we needed 2 pages, it has >> been 2 * PAGE_SIZE since the time zswap was first merged. > > i remember there was an over-compression case, that means the compressed > data can be bigger than the source data. the similar thing is also done in zram > drivers/block/zram/zcomp.c Right, there is a buffer overflow report[1] that I just +to you. I think over-compression is all right, but buffer overflow is not acceptable, so we should fix any buffer overflow problem IMHO. Anyway, 2 pages maybe overflowed too, just with smaller probability, right? Thanks. > > int zcomp_compress(struct zcomp_strm *zstrm, > const void *src, unsigned int *dst_len) > { > /* > * Our dst memory (zstrm->buffer) is always `2 * PAGE_SIZE' sized > * because sometimes we can endup having a bigger compressed data > * due to various reasons: for example compression algorithms tend > * to add some padding to the compressed buffer. Speaking of padding, > * comp algorithm `842' pads the compressed length to multiple of 8 > * and returns -ENOSP when the dst memory is not big enough, which > * is not something that ZRAM wants to see. We can handle the > * `compressed_size > PAGE_SIZE' case easily in ZRAM, but when we > * receive -ERRNO from the compressing backend we can't help it > * anymore. To make `842' happy we need to tell the exact size of > * the dst buffer, zram_drv will take care of the fact that > * compressed buffer is too big. > */ > *dst_len = PAGE_SIZE * 2; > > return crypto_comp_compress(zstrm->tfm, > src, PAGE_SIZE, > zstrm->buffer, dst_len); > } > > >> >> According to Yosry and Nhat, one potential reason is that we used to >> store a zswap header containing the swap entry in the compressed page >> for writeback purposes, but we don't do that anymore. >> >> This patch works good in kernel build testing even when the input data >> doesn't compress at all (i.e. dlen == PAGE_SIZE), which we can see >> from the bpftrace tool: >> >> bpftrace -e 'k:zpool_malloc {@[(uint32)arg1==4096]=count()}' >> @[1]: 2 >> @[0]: 12011430 >> >> Reviewed-by: Yosry Ahmed >> Reviewed-by: Nhat Pham >> Acked-by: Chris Li (Google) >> Signed-off-by: Chengming Zhou >> --- >> mm/zswap.c | 5 ++--- >> 1 file changed, 2 insertions(+), 3 deletions(-) >> >> diff --git a/mm/zswap.c b/mm/zswap.c >> index 7ee54a3d8281..976f278aa507 100644 >> --- a/mm/zswap.c >> +++ b/mm/zswap.c >> @@ -707,7 +707,7 @@ static int zswap_dstmem_prepare(unsigned int cpu) >> struct mutex *mutex; >> u8 *dst; >> >> - dst = kmalloc_node(PAGE_SIZE * 2, GFP_KERNEL, cpu_to_node(cpu)); >> + dst = kmalloc_node(PAGE_SIZE, GFP_KERNEL, cpu_to_node(cpu)); >> if (!dst) >> return -ENOMEM; >> >> @@ -1662,8 +1662,7 @@ bool zswap_store(struct folio *folio) >> sg_init_table(&input, 1); >> sg_set_page(&input, page, PAGE_SIZE, 0); >> >> - /* zswap_dstmem is of size (PAGE_SIZE * 2). Reflect same in sg_list */ >> - sg_init_one(&output, dst, PAGE_SIZE * 2); >> + sg_init_one(&output, dst, PAGE_SIZE); >> acomp_request_set_params(acomp_ctx->req, &input, &output, PAGE_SIZE, dlen); >> /* >> * it maybe looks a little bit silly that we send an asynchronous request, >> >> -- >> b4 0.10.1 >> > > Thanks > Barry