Received: by 2002:ac8:6d01:0:b0:423:7e07:f8e4 with SMTP id o1csp6615257qtt; Mon, 18 Dec 2023 01:25:12 -0800 (PST) X-Google-Smtp-Source: AGHT+IGWifN9WiOYd9mEHqeYovE9wFpX9q9kr5ASc+jemylhMcCG+lG+U1M3/WBijRtfElcOYJY3 X-Received: by 2002:a17:902:6847:b0:1d3:b31e:9002 with SMTP id f7-20020a170902684700b001d3b31e9002mr1533041pln.78.1702891512279; Mon, 18 Dec 2023 01:25:12 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702891512; cv=none; d=google.com; s=arc-20160816; b=j1B4Fru19mQXYktiF6ZelLmGuZN/VgZLkMuXuPxU0A9sntcId6u7h7TZ7M5Q0h1cNv 5WnAUjw4WO7p+1RfySDwEctoAqfBiDcTpNRQmxFGyOaYA2j52OCmIPPZDNsqEQWjjQFz sbNKIKOCdS9zaCMNwl/w6jkwxnWwY2K2fEYVSguFTWyYRzlFcnlYQ/gy2PapdC6peQ50 BCCJpfRw7vBk2ndgQRsCrvq7dYCQZqcAHIuMss+IpDae/4wSWJVHh/C0p9DkFjxSPNBB vulI3qb6EpZr/u8u9Nwhmpi08mbqWNa43vmSA9bxGI0btV9f8Q8ExtgEe6JtRjhhccNh 433A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:dkim-signature; bh=NLu+cBh6WIZkpMvvSxJ1FksEywwC35seFJURn3v2r5A=; fh=4d87QboLWmBzpm9kwNq9ZUdZO5eyJG0dmYX86KRWtCg=; b=sMDTYISm6q7vwSG3J1x/SGBN4bxyhgToCYNESS43Z20Jr49yiObuCBGB6D78+9ZVyP ulijZdilhkHW21Y8lomOhcJkl3NJaYt9gJtZ3zwTiJDaeM1lbXXvAwM8QDwYnVSQaRqU FlDh4t0vvXacuYFWlGcZhncDWL+A8ooKVCybnTQq4uUxphV1w/WhfLEh3CYWtav1f/4R yenIh3AAgiTNNSaoNYORq2NvtK9fcvhCTHOf/m/fALDdJ7DaVCcCLMpaMeQ/dcAMTrbT yPSM+y4R6VZ5QspgV9y93WY3Ro00y0cnt0wyCAoh/1xszNcrtUOyGOzpmnEPXVEw0IMl +nZA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=w4wkPxTm; spf=pass (google.com: domain of linux-kernel+bounces-3245-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-3245-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id iy22-20020a170903131600b001bdcd2e1706si2020926plb.196.2023.12.18.01.25.12 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Dec 2023 01:25:12 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-3245-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=w4wkPxTm; spf=pass (google.com: domain of linux-kernel+bounces-3245-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-3245-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 045DC2853E0 for ; Mon, 18 Dec 2023 09:25:12 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 942D311CAA; Mon, 18 Dec 2023 09:25:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="w4wkPxTm" X-Original-To: linux-kernel@vger.kernel.org Received: from mail-ej1-f41.google.com (mail-ej1-f41.google.com [209.85.218.41]) (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 36017101F0 for ; Mon, 18 Dec 2023 09:24:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=google.com Received: by mail-ej1-f41.google.com with SMTP id a640c23a62f3a-a233a60f8feso136581766b.0 for ; Mon, 18 Dec 2023 01:24:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1702891498; x=1703496298; darn=vger.kernel.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=NLu+cBh6WIZkpMvvSxJ1FksEywwC35seFJURn3v2r5A=; b=w4wkPxTmpEQADNuD1bAG6Az7L3SMrgeMTe2TGV3iJCR0k/jyKwKcDswAt6HoCQknF8 Cui0u+t0BCrEGtu/+FGmTNgo4+iKAv4pjXOgCGmLCWb0DHNGJb3fQ7rz81+jI04Z/B0X kaw0qXvIYK1OHkIOrDhm92HK0opsHFUZiMZw990Un1SgNNqSePUVHV6kIRipMS4Kkz8i 2E1m6aRrp1TvxGKt3XsCvdysdsJ8kNX9VYiv4t+atx/rE3HV+sZugw6NEj0KXboyDzOw sIUu3r8vAk5a72pEymjbl7hkFfB532Zd5Qefv1myrh+dWYu+MooR0Y8vb1Tofw9hyWUT l8HQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702891498; x=1703496298; 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=NLu+cBh6WIZkpMvvSxJ1FksEywwC35seFJURn3v2r5A=; b=qDueuFUj5Udjh8ysp6BTpa73rtkt4Na/wluGxXIUS4yFUmZrOjHhbLTfZmpaEdolxQ aNVos/kKWhNCbbP+GuRRUHJ0InmwXWuJOnYhpntKmNnSYB0Tw1+0wc/DYrg2ceQoZUU5 LumeTZO1KmId4H3AYQJUYizqLHoeNyHMfeTfV9n6LTSj8unC/JUK2oH9bySy7hUELI23 c3I8T+Sg8Ox1zSnMzBMOuXTdPU0l9gsfshKpab8eo+eFCploTptR25K7iiCp5S6tWfDY rrHa6sffR7P2Y6MR7h74ZlTjODAdFPPuCFy3pu56PLrjljUHqqMwEmAo9OiDbdR6Jkhf XpYw== X-Gm-Message-State: AOJu0YyKJVL1172IqyEmB02W9Vvh5RxbSOs0V/4AlGw/E74tG/my9hth Afvn0FuRIRj3teQwyEJ9kUx+yvXIaUNuDIjsb6sq+Q== X-Received: by 2002:a17:906:42:b0:a23:195c:8aa7 with SMTP id 2-20020a170906004200b00a23195c8aa7mr2422048ejg.15.1702891498140; Mon, 18 Dec 2023 01:24:58 -0800 (PST) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20231213-zswap-dstmem-v2-0-daa5d9ae41a7@bytedance.com> <20231213-zswap-dstmem-v2-2-daa5d9ae41a7@bytedance.com> In-Reply-To: <20231213-zswap-dstmem-v2-2-daa5d9ae41a7@bytedance.com> From: Yosry Ahmed Date: Mon, 18 Dec 2023 01:24:20 -0800 Message-ID: Subject: Re: [PATCH v2 2/6] mm/zswap: reuse dstmem when decompress To: Chengming Zhou Cc: Seth Jennings , Dan Streetman , Chris Li , Nhat Pham , Vitaly Wool , Andrew Morton , Johannes Weiner , linux-kernel@vger.kernel.org, linux-mm@kvack.org, Chris Li Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Mon, Dec 18, 2023 at 12:22=E2=80=AFAM Chengming Zhou wrote: > > In the !zpool_can_sleep_mapped() case such as zsmalloc, we need to first > copy the entry->handle memory to a temporary memory, which is allocated > using kmalloc. > > Obviously we can reuse the per-compressor dstmem to avoid allocating > every time, since it's percpu-compressor and protected in percpu mutex. > > Reviewed-by: Nhat Pham > Acked-by: Chris Li > Signed-off-by: Chengming Zhou Reviewed-by: Yosry Ahmed > --- > mm/zswap.c | 44 ++++++++++++-------------------------------- > 1 file changed, 12 insertions(+), 32 deletions(-) > > diff --git a/mm/zswap.c b/mm/zswap.c > index 976f278aa507..6b872744e962 100644 > --- a/mm/zswap.c > +++ b/mm/zswap.c > @@ -1417,19 +1417,13 @@ static int zswap_writeback_entry(struct zswap_ent= ry *entry, > struct crypto_acomp_ctx *acomp_ctx; > struct zpool *pool =3D zswap_find_zpool(entry); > bool page_was_allocated; > - u8 *src, *tmp =3D NULL; > + u8 *src; > unsigned int dlen; > int ret; > struct writeback_control wbc =3D { > .sync_mode =3D WB_SYNC_NONE, > }; > > - if (!zpool_can_sleep_mapped(pool)) { > - tmp =3D kmalloc(PAGE_SIZE, GFP_KERNEL); > - if (!tmp) > - return -ENOMEM; > - } > - > /* try to allocate swap cache page */ > mpol =3D get_task_policy(current); > page =3D __read_swap_cache_async(swpentry, GFP_KERNEL, mpol, > @@ -1465,15 +1459,15 @@ static int zswap_writeback_entry(struct zswap_ent= ry *entry, > /* decompress */ > acomp_ctx =3D raw_cpu_ptr(entry->pool->acomp_ctx); > dlen =3D PAGE_SIZE; > + mutex_lock(acomp_ctx->mutex); > > src =3D zpool_map_handle(pool, entry->handle, ZPOOL_MM_RO); > if (!zpool_can_sleep_mapped(pool)) { > - memcpy(tmp, src, entry->length); > - src =3D tmp; > + memcpy(acomp_ctx->dstmem, src, entry->length); > + src =3D acomp_ctx->dstmem; > zpool_unmap_handle(pool, entry->handle); > } > > - mutex_lock(acomp_ctx->mutex); > sg_init_one(&input, src, entry->length); > sg_init_table(&output, 1); > sg_set_page(&output, page, PAGE_SIZE, 0); > @@ -1482,9 +1476,7 @@ static int zswap_writeback_entry(struct zswap_entry= *entry, > dlen =3D acomp_ctx->req->dlen; > mutex_unlock(acomp_ctx->mutex); > > - if (!zpool_can_sleep_mapped(pool)) > - kfree(tmp); > - else > + if (zpool_can_sleep_mapped(pool)) > zpool_unmap_handle(pool, entry->handle); > > BUG_ON(ret); > @@ -1508,9 +1500,6 @@ static int zswap_writeback_entry(struct zswap_entry= *entry, > return ret; > > fail: > - if (!zpool_can_sleep_mapped(pool)) > - kfree(tmp); > - > /* > * If we get here because the page is already in swapcache, a > * load may be happening concurrently. It is safe and okay to > @@ -1771,7 +1760,7 @@ bool zswap_load(struct folio *folio) > struct zswap_entry *entry; > struct scatterlist input, output; > struct crypto_acomp_ctx *acomp_ctx; > - u8 *src, *dst, *tmp; > + u8 *src, *dst; > struct zpool *zpool; > unsigned int dlen; > bool ret; > @@ -1796,26 +1785,19 @@ bool zswap_load(struct folio *folio) > } > > zpool =3D zswap_find_zpool(entry); > - if (!zpool_can_sleep_mapped(zpool)) { > - tmp =3D kmalloc(entry->length, GFP_KERNEL); > - if (!tmp) { > - ret =3D false; > - goto freeentry; > - } > - } > > /* decompress */ > dlen =3D PAGE_SIZE; > - src =3D zpool_map_handle(zpool, entry->handle, ZPOOL_MM_RO); > + acomp_ctx =3D raw_cpu_ptr(entry->pool->acomp_ctx); > + mutex_lock(acomp_ctx->mutex); > > + src =3D zpool_map_handle(zpool, entry->handle, ZPOOL_MM_RO); > if (!zpool_can_sleep_mapped(zpool)) { > - memcpy(tmp, src, entry->length); > - src =3D tmp; > + memcpy(acomp_ctx->dstmem, src, entry->length); > + src =3D acomp_ctx->dstmem; > zpool_unmap_handle(zpool, entry->handle); > } > > - acomp_ctx =3D raw_cpu_ptr(entry->pool->acomp_ctx); > - mutex_lock(acomp_ctx->mutex); > sg_init_one(&input, src, entry->length); > sg_init_table(&output, 1); > sg_set_page(&output, page, PAGE_SIZE, 0); > @@ -1826,15 +1808,13 @@ bool zswap_load(struct folio *folio) > > if (zpool_can_sleep_mapped(zpool)) > zpool_unmap_handle(zpool, entry->handle); > - else > - kfree(tmp); > > ret =3D true; > stats: > count_vm_event(ZSWPIN); > if (entry->objcg) > count_objcg_event(entry->objcg, ZSWPIN); > -freeentry: > + > spin_lock(&tree->lock); > if (ret && zswap_exclusive_loads_enabled) { > zswap_invalidate_entry(tree, entry); > > -- > b4 0.10.1