From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 62D4CC46CD2 for ; Wed, 27 Dec 2023 06:32:37 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EA4A46B0074; Wed, 27 Dec 2023 01:32:36 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id E54B86B0075; Wed, 27 Dec 2023 01:32:36 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D1CF66B007B; Wed, 27 Dec 2023 01:32:36 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id C04B26B0074 for ; Wed, 27 Dec 2023 01:32:36 -0500 (EST) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 9115AA0F66 for ; Wed, 27 Dec 2023 06:32:36 +0000 (UTC) X-FDA: 81611629512.12.E7CBF26 Received: from mail-ua1-f50.google.com (mail-ua1-f50.google.com [209.85.222.50]) by imf30.hostedemail.com (Postfix) with ESMTP id CE52980002 for ; Wed, 27 Dec 2023 06:32:34 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=CiMlPfxo; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf30.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.222.50 as permitted sender) smtp.mailfrom=21cnbao@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1703658754; a=rsa-sha256; cv=none; b=o1fOz9vGivQQe3rrSdNGP5gGbntwZiIKDToIu0EnHoBGt+ujtJGpdbSFMMWy0N0FxWug4Z vY2L1NzJY07Il0PM+epW7nn+kfChioR9aq0S955u2eOZAAIXoeEfF/k0GleVDvcA3NY6qU U5O2cSPzZgwX3dkCSxnNA0HB0sE5ED4= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=CiMlPfxo; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf30.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.222.50 as permitted sender) smtp.mailfrom=21cnbao@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1703658754; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=DQpBmQb9ZKGVvTt+Da2dXb1ErxO/idvu8KhLAqdvHs4=; b=8ljDEcCboN2Rz9zlC/PdvTY3lgJYUn69JuvMegt8LInm3sCB7WwCK2xYXWw9YjNzA7nCC4 zLfWE3DpyuU6hrvB86hZv03/lKVmjUjSgZ3GP87VVZ705q3SO6e2tM2sdYr7Wk7gIP8Xdq V2xhNThV19hQ44qtj4YMUMoEgsYaKgM= Received: by mail-ua1-f50.google.com with SMTP id a1e0cc1a2514c-7ccec01ef89so391964241.1 for ; Tue, 26 Dec 2023 22:32:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1703658754; x=1704263554; darn=kvack.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=DQpBmQb9ZKGVvTt+Da2dXb1ErxO/idvu8KhLAqdvHs4=; b=CiMlPfxoOUWgIYM/8/YezBO4S7t6Ea4Mg0NpaC0R5H4DEHlOzwEpqqLRU6dkab4/zv e7JPil3FLVPFo2B494ANYkXZsA5bBujr04NkSE/6rWfTfX0GFcOJRnJTY8THRgX+903O HH0/6bMVVh0simD70fmJdsth5Xvi9jEmQlLvgMr2HyXpr1oi6Pu7SYHy910FY86w9CeX aQiFkVYKKYWovTal73nIQks89+o70sTS1jLHH1mPfyIUQEKMiT8lCTos3hp/iPFE2rlX DisuXvbtwgUSIoE8p/LS5oCKKm6RysNoPMXU+I1LqqrzadWf2EFLlXp+xErM7WLNYSPG lhKQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1703658754; x=1704263554; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=DQpBmQb9ZKGVvTt+Da2dXb1ErxO/idvu8KhLAqdvHs4=; b=dUpGNDyGUASUjCrjPZ+a1uE9ae8PtUWf4ncIbvNBvDksBRD29EEeCgbtPa/oZXBzsR F7r6fGHdCZglwofvEa8b6TLF6xYSYi3HAOylcEqopoH4kOFFonlMhfiPbTpw1/EMQTK3 VahkXGfXu0zitBXUaVshy6kJcNNbQWYbbaQT/Y/AAScpDxya8Zy79o/vGR9UlYBfkhMx JLHIt/Hwx8tvmfsXwFAGOeY+onwjjAG0t9TXaGDHhH/Bi6I9vyZIvQq/CTWFEqY2R+Ls jFWOs2vNU92qSrqtGUA3QjXtfxZkcdedIXEKoVnurDB4txNXsMtU4uiOZ20LYfOAryD0 9YhA== X-Gm-Message-State: AOJu0Ywc6dUvMmf0Nf8PoSsBrHJxby12OF+OmXfcDk8gsh5lTpacrKtf uTri6BT9IprKKl3HdsrDcUe5kdiDzQvzlToejhE= X-Google-Smtp-Source: AGHT+IGAqeHImSOZ0T3Y92vVzDH1Ch+3oencaCeBlIf2Q1ydOIIMCwEm1BUPQ00EUCXEkbUqzmrDGfftwJ/xknh8o5s= X-Received: by 2002:a05:6102:3f05:b0:467:33b2:231c with SMTP id k5-20020a0561023f0500b0046733b2231cmr104286vsv.21.1703658753720; Tue, 26 Dec 2023 22:32:33 -0800 (PST) MIME-Version: 1.0 References: <20231213-zswap-dstmem-v4-0-f228b059dd89@bytedance.com> <20231213-zswap-dstmem-v4-1-f228b059dd89@bytedance.com> In-Reply-To: From: Barry Song <21cnbao@gmail.com> Date: Wed, 27 Dec 2023 19:32:22 +1300 Message-ID: Subject: Re: [PATCH v4 1/6] mm/zswap: change dstmem size to one page To: Chengming Zhou 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 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: CE52980002 X-Stat-Signature: howmzieywakaqag1zdye83m1ynsckbst X-HE-Tag: 1703658754-981319 X-HE-Meta: U2FsdGVkX1997qLTTN5njSX/i8Comr1gejtDUVyD8R0cSCOc7SyTRw9ftbrn/g/ViYu3/Z4o6lfKz/GPhceQggPm156bB+cmB6uxUu8x2csIyGmYOLdgY6Ez9QBPQysB569Zl5RCmKq0KN1llX0v+fhm8CEL04LbeqF9gf7gbmcgZZEEXdO6bANh184XcjoqoDARUn7MeIfqYFuxurS2SInH/w9TtHWNaCuXoICK3WRF5Gc3HtFlvE4or86ABnNQH1W41XqhvYzDrw4OwU8zKIdAobuaqDFr35+5ze7x+XBk6scEdfL95vahYtxVRTlx/bx5qa28R9DqWK9amofotFZHFRBUY/kHLGYJgQHlyi0r6uYzMvd2Ss6oJUc8o0uzOpJziiVswJmSik3+K3+vMNiuywzKf3mFi/mSX5gdv1xDY5E9HkHT2/ShECP11uhYcWq0HVi4i3M1exjTGrRnXm3gg69gDEupXmeE/SwoPYj5OYcef4hfnQGeEEI86zMaDf8kG6my4SZH7vT41DJD/tFZxaYrhqQaMs9VWNBhXcmGPAi1qcx6EHYUaQCr6JxTYAjgE3afMwLsTJTN/srI9nb9mwvGk508J2KiK8p20o5DXRfXHPVDKkxGeNBaBgWAOc0Sz+doLN1CUPwbp4Wimf06dCrju5pChNjvR8I1GnVe0R/ZyIhgz7Ik3JQt7sucIvgl0j8zFgt2C0ZHTQV4RZsobS6MI39i7bHLNQadjfnNzSYWAgTTrszFVOuRB6J+vcbQHMxO8GzBXBAw4qLW/PYg3lNRg1ZFWpKr7llgdk1X0w95LDsz5nGfXdIdM8aSDBp+OBTKzUnZ/PFKQHDLEY7d4uSFMs7qPrWlXrOdLkm4hs/EObOTruc8/frNB5k7iDkrX9mClgl7A190fsljh+QaoMigyyAbMhUgLF77CQbsSvHZQ1xIMP/ZTzypt3lFCJoMhIAyqA5MiJNe+re 2OnLi7xN d8Xlcqsy7Jn09QO3DHF2JoiP3LcFgLZs//u5CFjyK/7P4hmGgBpcx0yZ37Q03BWGDCVOoPJ4hNEf/uQ1uZ+oOidmlBIAgoEi1LcTBB4qLASzjb1FUruwrBjQOWbLFoPw5ljjSUxQSRT2EAy8elFR//NZh88RUg3ppTyNyzu2pccXg0cjuRbR3oBhROT8UWgcuNw3946M8tCTvzziP4sNPwRs6r2/bL8aruO3ahft1Ls5yRfZ93TNSjSP4KtoqCjTNPbfCkENqHAGpJLdHuRRCAW0FZceqIVVHm86m07Bzxpt2uMh+Q1z5x8ieupg26LeE5kXAzTG8wlZ7b9p6UvmOV84kB57aO5eM3NG1Btc03fUW2jZ390V8TRBj8FAHPtxK9CTSAPQLCcnEjivsqgiECpIUigH1CPmUeFlv0iw2MGe8rS6DqxNjNfC+nsXgQRAyDlKJZOozjQAjhBK1h/5w9qu5K2W++8nHXqNOUlRKDWRN0NIoEXl3ZXLSNwRMJqwlq/3GZE8MOmYOOWY= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: On Wed, Dec 27, 2023 at 7:11=E2=80=AFPM Chengming Zhou wrote: > > On 2023/12/27 09:07, Barry Song wrote: > > On Wed, Dec 27, 2023 at 4:55=E2=80=AFAM 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_SIZ= E > >> 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 compress= ed > > 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 accepta= ble, > so we should fix any buffer overflow problem IMHO. Anyway, 2 pages maybe > overflowed too, just with smaller probability, right? practically, the typical page size is 4KB or above, so we have never seen 2 pages can be overflowed. We may have a chance to let CPU-based compression code to return earlier before overflowing though it is still very tough. but for accelerators-based compression in drivers/crypto, the only choice i= s giving its dma engine a buffer whose length is enough - 2*PAGE_SIZE. so i don't think this patch is correct. > > 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' siz= ed > > * because sometimes we can endup having a bigger compressed da= ta > > * due to various reasons: for example compression algorithms t= end > > * to add some padding to the compressed buffer. Speaking of pa= dding, > > * comp algorithm `842' pads the compressed length to multiple = of 8 > > * and returns -ENOSP when the dst memory is not big enough, wh= ich > > * 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 =3D 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 =3D=3D PAGE_SIZE), which we can see > >> from the bpftrace tool: > >> > >> bpftrace -e 'k:zpool_malloc {@[(uint32)arg1=3D=3D4096]=3Dcount()}' > >> @[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 =3D kmalloc_node(PAGE_SIZE * 2, GFP_KERNEL, cpu_to_node(cp= u)); > >> + dst =3D 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 asynchron= ous request, > >> > >> -- > >> b4 0.10.1 > >> Thanks Barry