Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp5985172rdb; Thu, 14 Dec 2023 05:32:54 -0800 (PST) X-Google-Smtp-Source: AGHT+IHwiRLrZxGbjehOJJGUqUKcjD1DBcRWC41lopl38oeGKtuqyLDDsjo+VYAfX1DiXbZaXPjb X-Received: by 2002:a17:903:2cc:b0:1d3:488e:a4b7 with SMTP id s12-20020a17090302cc00b001d3488ea4b7mr4302235plk.128.1702560774033; Thu, 14 Dec 2023 05:32:54 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702560774; cv=none; d=google.com; s=arc-20160816; b=mwQOICAG0wTnK8YFcoEp72UDfheO2Nu3LqkCszD5IICZQx/CTdwK5mBNVpKFGIhUMv tUfEr86gqtm6naKW9x5hFY8dox82dOVXOl+SWN3M0zIAapA+SpUPucoN5ny2iFdDktYu 1SVnjZZjR17n5PwobroUbXWYaiOXRKKa8hZ/vbPir2K2vesputtIEHPjzOnQzsSmpNBC EwpvEYc8pQRvjOLl7D+CcTzxsb1mko0vqAInkM/pz2QwGWfZ987yClH8YqxOKOGcgtGZ Nj1mER0gTlp1P08v7HTN10XejAVpP21h0VClHR5hS5UyQulOyl/OekgHDAGYkD8yzbHX 0YBA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=jim/lOntV9iDTSpGOJDnAOIgsuHO8H2tmMhky2DuBN8=; fh=CIEK4oU1VpeGYez8Kqg5XgL7oVBF13djoAg0dYB/DRs=; b=ypcH7S8nnbkfifXy5b9XYMz662P36y60Lm8gLqie9Wsc1cQGwYFHuPhgn8FEpC72za B0HCoXbUYxHPJ7ORjacF+wdhxS6FSM1px5H2+VriUWOhdbdcz3x2MYMrfUlFKVWQyiUT FKdzcf3xlQF8UQmSffCHUSlELQTouxwuO/FR7Pk1ThRY3iUXGelydI7pXPQ5WXK1gI0S GLJk4AnM+twLwRBxOlIbncd/TdC8eyV0zn4n5OYnSXlsG5es2MUi5FcCZQD4HGliZPdp kdXfzAzMDlS/xDMc62JXXtSJKtoJIw2CdJDqpzap/hUgr/xdWybVckIlBntiPNoALV+D iqAQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=n2O+2yyv; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from morse.vger.email (morse.vger.email. [2620:137:e000::3:1]) by mx.google.com with ESMTPS id j15-20020a170903024f00b001d2e6ed9349si11296353plh.479.2023.12.14.05.32.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Dec 2023 05:32:54 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) client-ip=2620:137:e000::3:1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=n2O+2yyv; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id 9DF6883328C4; Thu, 14 Dec 2023 05:32:50 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1573319AbjLNNci (ORCPT + 99 others); Thu, 14 Dec 2023 08:32:38 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39698 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1573304AbjLNNch (ORCPT ); Thu, 14 Dec 2023 08:32:37 -0500 Received: from mail-lj1-x22a.google.com (mail-lj1-x22a.google.com [IPv6:2a00:1450:4864:20::22a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 19E3C114 for ; Thu, 14 Dec 2023 05:32:43 -0800 (PST) Received: by mail-lj1-x22a.google.com with SMTP id 38308e7fff4ca-2c9f7fe6623so102248511fa.3 for ; Thu, 14 Dec 2023 05:32:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1702560761; x=1703165561; 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=jim/lOntV9iDTSpGOJDnAOIgsuHO8H2tmMhky2DuBN8=; b=n2O+2yyvfMlx3iRGTFyVuJRi0xFba3z3y3enUC03J97GfsWwFblkejJKYbeAQABic2 UVnhBi8643UrO/QyPPbP/EDdy/Vd0YBeJ6ADNLgeeOzGsmg/lpPFIw3t8rrtfLRtSavu JGMgrCqFLfq9b8Euww+onuIyM2c6Fve8F6Ht1O0XbhPxuey9ldNag0dmb2sj/PqIuBgH wlLet/5lQkcrML81DgoPasmqa+3oIlgxmfJBE5CoXFrbPb/RafveqxWKImqAy9xyl2/y a1/TThbSSh1WdchBX2LqFlmLUUWCiyIOIdVEbH0vGaqeBNUbAWQBOF8icKpvHG/zGpvF VuZQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702560761; x=1703165561; 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=jim/lOntV9iDTSpGOJDnAOIgsuHO8H2tmMhky2DuBN8=; b=Dj0aqkYa2NtOESyvixl47NTxvZYoX5rdhKpckyZc5HyDskfsuYWWKYPHcfu3qe0dtG YTlziwf5B3beVpXqJuBb0JXyf1m62XKu0xUrUj7HPLiHMUBMNhc6R2mqgIHqdXHGMajh T4Rt4dFeQnIv7M+9pXB4mUV08cqWus+VUYKPuciiXGxdgp0mfyXTVXmt8OauT2+2tGCA 5UKVBN7gn/1JnG7/wXGU72J8NslKs3TbhgV3x7CV7esRyJ86kFuKep+2qdTbDkuyeI0h kGKDEf+81bI0844Jsw2Jrc4SluXxY40UpOVQ4tx4EghdlVlYEsOSw3Bp6xBoZcAVGU4j bZBg== X-Gm-Message-State: AOJu0YwLulqY7A8PUahrk4LWwsS2T/4P22hmmoeKJUzAl4zu1nI4HFBi bVc5yafTTJaFdXSM5eSphrhwQCX85dHh59FPbOSkUFTfyOG9HmivL9U= X-Received: by 2002:a19:644a:0:b0:50b:f47d:770f with SMTP id b10-20020a19644a000000b0050bf47d770fmr2955552lfj.0.1702560761036; Thu, 14 Dec 2023 05:32:41 -0800 (PST) MIME-Version: 1.0 References: <20231213-zswap-dstmem-v1-0-896763369d04@bytedance.com> <20231213-zswap-dstmem-v1-1-896763369d04@bytedance.com> <8f80fe3e-a8c7-463d-896b-99575c362839@bytedance.com> In-Reply-To: <8f80fe3e-a8c7-463d-896b-99575c362839@bytedance.com> From: Yosry Ahmed Date: Thu, 14 Dec 2023 05:32:03 -0800 Message-ID: Subject: Re: [PATCH 1/5] mm/zswap: reuse dstmem when decompress To: Chengming Zhou Cc: Andrew Morton , Nhat Pham , Chris Li , Johannes Weiner , Seth Jennings , Dan Streetman , Vitaly Wool , linux-kernel@vger.kernel.org, linux-mm@kvack.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Thu, 14 Dec 2023 05:32:50 -0800 (PST) On Thu, Dec 14, 2023 at 5:29=E2=80=AFAM Chengming Zhou wrote: > > On 2023/12/14 07:24, Yosry Ahmed wrote: > > On Tue, Dec 12, 2023 at 8:18=E2=80=AFPM Chengming Zhou > > wrote: > >> > >> In the !zpool_can_sleep_mapped() case such as zsmalloc, we need to fir= st > >> copy the entry->handle memory to a temporary memory, which is allocate= d > >> using kmalloc. > >> > >> Obviously we can reuse the per-compressor dstmem to avoid allocating > >> every time, since it's percpu-compressor and protected in mutex. > >> > >> Signed-off-by: Chengming Zhou > >> Reviewed-by: Nhat Pham > >> --- > >> mm/zswap.c | 29 +++++++++-------------------- > >> 1 file changed, 9 insertions(+), 20 deletions(-) > >> > >> diff --git a/mm/zswap.c b/mm/zswap.c > >> index 7ee54a3d8281..edb8b45ed5a1 100644 > >> --- a/mm/zswap.c > >> +++ b/mm/zswap.c > >> @@ -1772,9 +1772,9 @@ bool zswap_load(struct folio *folio) > >> struct zswap_entry *entry; > >> struct scatterlist input, output; > >> struct crypto_acomp_ctx *acomp_ctx; > >> - u8 *src, *dst, *tmp; > >> + unsigned int dlen =3D PAGE_SIZE; > >> + u8 *src, *dst; > >> struct zpool *zpool; > >> - unsigned int dlen; > >> bool ret; > >> > >> VM_WARN_ON_ONCE(!folio_test_locked(folio)); > >> @@ -1796,27 +1796,18 @@ bool zswap_load(struct folio *folio) > >> goto stats; > >> } > >> > >> - 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); > >> > >> + zpool =3D zswap_find_zpool(entry); > >> + 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; > > > > I don't like that we are now using acomp_ctx->dstmem and > > acomp_ctx->mutex now for purposes other than what the naming suggests. > > The "mutex" name is coherent, "dstmem" depends on how we use IMHO. > Change to just "mem"? Or do you have a better name to replace? > > > > > How about removing these two fields from acomp_ctx, and directly using > > zswap_dstmem and zswap_mutex in both the load and store paths, rename > > them, and add proper comments above their definitions that they are > > for generic percpu buffering on the load and store paths? > > Yes, they are percpu memory and lock, but they are used by per acomp_ctx, > and the cpu maybe changing in the middle, so maybe better to keep them. I don't mean to remove completely. Keep them as (for example) zswap_mem and zswap_mutex global percpu variables, and not have pointers in acomp_ctx to them. Instead of using acomp_ctx->dstmem today, we directly use the global zswap_mem (same for the mutex). This makes it clear that the buffers are not owned or exclusively used by the acomp_ctx. WDYT?