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 92980C47DA9 for ; Tue, 30 Jan 2024 16:21:46 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1E4A66B008A; Tue, 30 Jan 2024 11:21:46 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 193EF6B008C; Tue, 30 Jan 2024 11:21:46 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 05C416B0092; Tue, 30 Jan 2024 11:21:46 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id EC5656B008A for ; Tue, 30 Jan 2024 11:21:45 -0500 (EST) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 978E540B48 for ; Tue, 30 Jan 2024 16:21:45 +0000 (UTC) X-FDA: 81736493370.11.4522AA1 Received: from mail-il1-f176.google.com (mail-il1-f176.google.com [209.85.166.176]) by imf12.hostedemail.com (Postfix) with ESMTP id A79FE40024 for ; Tue, 30 Jan 2024 16:21:43 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=ecEFHPgw; spf=pass (imf12.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.166.176 as permitted sender) smtp.mailfrom=nphamcs@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1706631703; 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=AflKDfWvlGw5qdywRHr9cPLeGhB9EGNqNEemcMp55J4=; b=LaCsHVMoiod9UqR2UOnwFUaWiuG0LYtJnq5aGtVVHnG3ZRp9ObYGCcqGAXx3yG7u8vZgVf 0Q7xrFGqT7G9xwdW2oHJ7PdAinW7vUSjwcE35PVlEymgRjT8Wfk1BHuEJgvS7ttk+ug64I 0riVKdEu5sMhW+/YTB44wKIcqiYJ5s4= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1706631703; a=rsa-sha256; cv=none; b=jnYnC1IrNdeC5Ycep6ue8ovtppblgrd6SHH6dAIHA/riiMqWKTbRhvjZrbovvimNmDAw9H nyI+p3of4cGvDkysaADkF7UbYa1CQUOUeSeZRtrn7FFRfG6v+cepSWGth+h9//xFiIXQMT 3s9f1g48siWJY+ecCuQRjwjXIS8YHmc= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=ecEFHPgw; spf=pass (imf12.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.166.176 as permitted sender) smtp.mailfrom=nphamcs@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-il1-f176.google.com with SMTP id e9e14a558f8ab-36379ffa624so5825995ab.0 for ; Tue, 30 Jan 2024 08:21:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1706631702; x=1707236502; 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=AflKDfWvlGw5qdywRHr9cPLeGhB9EGNqNEemcMp55J4=; b=ecEFHPgw0nfPyJkk7DCR6m6nq9c23sbXXjXluaEVHCirvHyJbfnGiSOB3FsltRogMf ukXhCF8+FnQDOjPluGdBVcWWtcXkRCtHEW/Kve9B9VAhjSGwcTx+DtHDfvLnGS+GZJd+ BhJrvi6XU3m1oiXnrTY9QveBjCa1jpKNsoMdx25N09Og4mIGjjxFhZspZF3CJDJhVTyG bUXO+jKyQERJSgfiIlovvXjHpXawsKcQWJNOeZMP1lW9XP51BITjZuFbFj7ksA0IEfjs bamGW++MSmgNMaGT1zyJy6LAAQQ0HRE2fTJrKA8fWfkhk6h3fxTtyBDso+kFIY1bW8x7 z/qA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706631702; x=1707236502; 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=AflKDfWvlGw5qdywRHr9cPLeGhB9EGNqNEemcMp55J4=; b=HKKPoa0Os6rym6ngyPli9VJ+otlfGOGFanTOA5/JozABf1WB7z18lwx/uYPbyE7TDQ NZDCk/xmaERFyvNw5nxrw3XhsLz2IpqnAIJNVrdfIefCSYNEniL0Y58BJAEVLfEccJxG /4i5jMci63lxOxT9YomUcvtTtO5vwmR9OG7PCnMDThDP5/ZSA3o4KYKPcXcRZ+7JpNvs 9QLh+veZ82AOVvQasq4KE9bnyZwhEsG16gFcACPR70wM+GJixLHUcNKxO14xnrXJZdg7 l3iKWHw12SvwLsrzVRdoI3FYsW3Zh0GSsJETHLdJXLzRDhVyNAAPJBmWOVWRLgKZz5Ln mqKg== X-Gm-Message-State: AOJu0YxWECcFlE4C1Zc13vSR+tYrYm7TOVgOp+/7Plxv0B+P54NlvFjA EdTZcM/qTMN4KjTLnnkwjkzSbpPgZj269Md6sAn65m116OMnuXXaFIME9NSu9/KsTJ3vHSdFE/D GXUefPjkGyEEFnDSsgm4DDq+q/KQ= X-Google-Smtp-Source: AGHT+IH9iD5NQgWDxIpLd1KkGLSeZbaNpPoZ44ePMCg1ia4YEyZLat4eJk7qzKnT4DjhkFsktc2Oa6c87tBulTT43NY= X-Received: by 2002:a05:6e02:1a25:b0:363:6da9:2bc3 with SMTP id g5-20020a056e021a2500b003636da92bc3mr8965204ile.11.1706631702636; Tue, 30 Jan 2024 08:21:42 -0800 (PST) MIME-Version: 1.0 References: <20240130014208.565554-1-hannes@cmpxchg.org> <20240130014208.565554-8-hannes@cmpxchg.org> In-Reply-To: <20240130014208.565554-8-hannes@cmpxchg.org> From: Nhat Pham Date: Tue, 30 Jan 2024 08:21:31 -0800 Message-ID: Subject: Re: [PATCH 07/20] mm: zswap: break out zwap_compress() To: Johannes Weiner Cc: Andrew Morton , Yosry Ahmed , Chengming Zhou , linux-mm@kvack.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Stat-Signature: wosz1ydwcjzx5z1zzaawkwuf6aw8boph X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: A79FE40024 X-Rspam-User: X-HE-Tag: 1706631703-724623 X-HE-Meta: U2FsdGVkX19LUEBWMqVDbY+jmX5V+f0CQqo8YdL+Nf8gWcgIzW3fJMsZK+AXoL613Hpw6YoeJl7pdCOuQzszIFvFvz2TbwsVls15dtQiaT1tUaaruD+i592BahUWVqYrLtgi6UuzFIlpVlX1NJGxfDhBfQyQZ4O1nW85cxMMUY3NC48ZEV9i0yaJTfbt8FuxrkEY3KRpGGQfnbKp1RObEBE4psrSE07J65gQGQgIDu/xOIxwCJ0dmaB1vQrznA5zRPLmk/lynYrbEfy7b+CMjWPuZOYDVIBjViePShzbu1VQ0kc5ZGEX+QYBxJeqvcj5x1DaWWnEfLAWGXtinL7u3gpitAHbAAVEX/Z08O8e8QC3wQJrCEkuDxK3mmMCFCqj+cJn8QqgbEW15bh2iIVzFG2gIO9ZXEoEvc7OnmBsqEWVzfMQwtVLMjIw/MKfdGUe4IJMLdTQTehFw+jAEAOAB64EEpULOcJaBbd7/zuYAeJwcPxwdHIzYK5U7ESIsepCuoP1sP4qrVwt9yH1gLfl7kB35H0V1B9CfbME3Bh+wSMVb5eMPhdlUbJj8Sq+NXxqjDKkIY6i1K0JgmwO8qCF62LBZBO4qITECxr+t33DG/12dvjHx2jaNFBCifYYuU9pirkMdufxWHIRx5OiKkq2P1ovNjMVsB2LB0xcLWF9wa1/PgfVt/Ske+jcumVpuRzK78S9whlBAj0J6WQldwWFvBvMrv9RhaP0FBaFh7Azq5RiETplPRZ01WU+BRC8br8XyPyoQNc9qnB6VGBLKptAnoM/KiGUjeJe5AiEqfBWoooqUEDhfrYu3ya6p50Elsnf0lac6cqY4UYF0yYlXrJTY727R6aZ3OwrUJDPJ9dMf3J3n70hi7QwSg52vd3cn6YQT++y+qkwNaLHMWz24XZw8J0wpp4/P3zg6jJfUJxOjYrWcSGX48Oka8cpAu+K9n6owO/GBEbGnJkOdYnySvd Miur1yOH DAdDFi1gUkMWbRn8eO1ymu/4uwOFK9d/qUWZEHfVh3FotdI6U+x8IktMK0QhxDl4nwyXLz9wTO5hch9loBWfS0oKgWP9BLzRLa+rHR7yknqsMbByQbdl4PQgKRfNqFvgBNT0q75YF5sbx4K8aqcLyVv9WhnWeHJH0bGBvpYT1v+TiTYtZ+sqfmXca1o6YplQY4xtB0n8qq7isG5juANwV1+lkS0C4DpF/vbXV1jb676bk7sAfHRiHG8C3ZwT+shB3ryKdaJ62mh7AFaDF37RgsIQqwb3ahg0xUIK62SjGjdMUSShMtv/mjcqXfxhQc06mTeLvZBfT5SW2W9Du48+A0KiktuaW/haCrXIYL68MfSLbi2KyyAKhY91lcMFaFWjkgS74oCPb2g/wKk2uAq4BK31bLx7Ck2dkFBIvpNthw0lmto7xSRdc68wY9Hlt4o+G0rfY 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 Mon, Jan 29, 2024 at 5:42=E2=80=AFPM Johannes Weiner wrote: > > zswap_store() is long and mixes work at the zswap layer with work at > the backend and compression layer. Move compression & backend work to > zswap_compress(), mirroring zswap_decompress(). > > Signed-off-by: Johannes Weiner > --- > mm/zswap.c | 145 ++++++++++++++++++++++++++++------------------------- > 1 file changed, 77 insertions(+), 68 deletions(-) > > diff --git a/mm/zswap.c b/mm/zswap.c > index bdc9f82fe4b9..f9b9494156ba 100644 > --- a/mm/zswap.c > +++ b/mm/zswap.c > @@ -1316,6 +1316,79 @@ static int zswap_enabled_param_set(const char *val= , > return ret; > } > > +static bool zswap_compress(struct folio *folio, struct zswap_entry *entr= y) > +{ > + struct crypto_acomp_ctx *acomp_ctx; > + struct scatterlist input, output; > + unsigned int dlen =3D PAGE_SIZE; > + unsigned long handle; > + struct zpool *zpool; > + char *buf; > + gfp_t gfp; > + int ret; > + u8 *dst; > + > + acomp_ctx =3D raw_cpu_ptr(entry->pool->acomp_ctx); > + > + mutex_lock(&acomp_ctx->mutex); > + > + dst =3D acomp_ctx->buffer; > + sg_init_table(&input, 1); > + sg_set_page(&input, &folio->page, PAGE_SIZE, 0); > + > + /* > + * We need PAGE_SIZE * 2 here since there maybe over-compression = case, > + * and hardware-accelerators may won't check the dst buffer size,= so > + * giving the dst buffer with enough length to avoid buffer overf= low. > + */ > + sg_init_one(&output, dst, PAGE_SIZE * 2); > + acomp_request_set_params(acomp_ctx->req, &input, &output, PAGE_SI= ZE, dlen); > + > + /* > + * it maybe looks a little bit silly that we send an asynchronous= request, > + * then wait for its completion synchronously. This makes the pro= cess look > + * synchronous in fact. > + * Theoretically, acomp supports users send multiple acomp reques= ts in one > + * acomp instance, then get those requests done simultaneously. b= ut in this > + * case, zswap actually does store and load page by page, there i= s no > + * existing method to send the second page before the first page = is done > + * in one thread doing zwap. > + * but in different threads running on different cpu, we have dif= ferent > + * acomp instance, so multiple threads can do (de)compression in = parallel. > + */ > + ret =3D crypto_wait_req(crypto_acomp_compress(acomp_ctx->req), &a= comp_ctx->wait); > + dlen =3D acomp_ctx->req->dlen; > + if (ret) { > + zswap_reject_compress_fail++; > + goto unlock; > + } > + > + zpool =3D zswap_find_zpool(entry); > + gfp =3D __GFP_NORETRY | __GFP_NOWARN | __GFP_KSWAPD_RECLAIM; > + if (zpool_malloc_support_movable(zpool)) > + gfp |=3D __GFP_HIGHMEM | __GFP_MOVABLE; > + ret =3D zpool_malloc(zpool, dlen, gfp, &handle); > + if (ret =3D=3D -ENOSPC) { > + zswap_reject_compress_poor++; > + goto unlock; > + } > + if (ret) { > + zswap_reject_alloc_fail++; > + goto unlock; > + } > + > + buf =3D zpool_map_handle(zpool, handle, ZPOOL_MM_WO); > + memcpy(buf, dst, dlen); > + zpool_unmap_handle(zpool, handle); > + > + entry->handle =3D handle; > + entry->length =3D dlen; > + > +unlock: > + mutex_unlock(&acomp_ctx->mutex); > + return ret =3D=3D 0; > +} > + > static void zswap_decompress(struct zswap_entry *entry, struct page *pag= e) > { > struct zpool *zpool =3D zswap_find_zpool(entry); > @@ -1472,18 +1545,11 @@ bool zswap_store(struct folio *folio) > struct page *page =3D &folio->page; > struct zswap_tree *tree =3D swap_zswap_tree(swp); > struct zswap_entry *entry, *dupentry; > - struct scatterlist input, output; > - struct crypto_acomp_ctx *acomp_ctx; > struct obj_cgroup *objcg =3D NULL; > struct mem_cgroup *memcg =3D NULL; > struct zswap_pool *pool; > - struct zpool *zpool; > - unsigned int dlen =3D PAGE_SIZE; > - unsigned long handle, value; > - char *buf; > - u8 *src, *dst; > - gfp_t gfp; > - int ret; > + unsigned long value; > + u8 *src; > > VM_WARN_ON_ONCE(!folio_test_locked(folio)); > VM_WARN_ON_ONCE(!folio_test_swapcache(folio)); > @@ -1568,65 +1634,10 @@ bool zswap_store(struct folio *folio) > mem_cgroup_put(memcg); > } > > - /* compress */ > - acomp_ctx =3D raw_cpu_ptr(entry->pool->acomp_ctx); > - > - mutex_lock(&acomp_ctx->mutex); > - > - dst =3D acomp_ctx->buffer; > - sg_init_table(&input, 1); > - sg_set_page(&input, &folio->page, PAGE_SIZE, 0); > + if (!zswap_compress(folio, entry)) > + goto put_pool; > > - /* > - * We need PAGE_SIZE * 2 here since there maybe over-compression = case, > - * and hardware-accelerators may won't check the dst buffer size,= so > - * giving the dst buffer with enough length to avoid buffer overf= low. > - */ > - sg_init_one(&output, dst, PAGE_SIZE * 2); > - acomp_request_set_params(acomp_ctx->req, &input, &output, PAGE_SI= ZE, dlen); > - /* > - * it maybe looks a little bit silly that we send an asynchronous= request, > - * then wait for its completion synchronously. This makes the pro= cess look > - * synchronous in fact. > - * Theoretically, acomp supports users send multiple acomp reques= ts in one > - * acomp instance, then get those requests done simultaneously. b= ut in this > - * case, zswap actually does store and load page by page, there i= s no > - * existing method to send the second page before the first page = is done > - * in one thread doing zwap. > - * but in different threads running on different cpu, we have dif= ferent > - * acomp instance, so multiple threads can do (de)compression in = parallel. > - */ > - ret =3D crypto_wait_req(crypto_acomp_compress(acomp_ctx->req), &a= comp_ctx->wait); > - dlen =3D acomp_ctx->req->dlen; > - > - if (ret) { > - zswap_reject_compress_fail++; > - goto put_dstmem; > - } > - > - /* store */ > - zpool =3D zswap_find_zpool(entry); > - gfp =3D __GFP_NORETRY | __GFP_NOWARN | __GFP_KSWAPD_RECLAIM; > - if (zpool_malloc_support_movable(zpool)) > - gfp |=3D __GFP_HIGHMEM | __GFP_MOVABLE; > - ret =3D zpool_malloc(zpool, dlen, gfp, &handle); > - if (ret =3D=3D -ENOSPC) { > - zswap_reject_compress_poor++; > - goto put_dstmem; > - } > - if (ret) { > - zswap_reject_alloc_fail++; > - goto put_dstmem; > - } > - buf =3D zpool_map_handle(zpool, handle, ZPOOL_MM_WO); > - memcpy(buf, dst, dlen); > - zpool_unmap_handle(zpool, handle); > - mutex_unlock(&acomp_ctx->mutex); > - > - /* populate entry */ > entry->swpentry =3D swp; > - entry->handle =3D handle; > - entry->length =3D dlen; > > insert_entry: > entry->objcg =3D objcg; > @@ -1663,8 +1674,6 @@ bool zswap_store(struct folio *folio) > > return true; > > -put_dstmem: > - mutex_unlock(&acomp_ctx->mutex); > put_pool: > zswap_pool_put(entry->pool); > freepage: > -- > 2.43.0 > LGTM :) Reviewed-by: Nhat Pham