Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp4965955rdb; Tue, 12 Dec 2023 14:58:57 -0800 (PST) X-Google-Smtp-Source: AGHT+IFlvJg1Pvmav9sgsAK3LI4mTA1wtgxhloOyv45e1oemWZERxhAR/B7TvCxrMn3LbBR7Cwa5 X-Received: by 2002:a05:6e02:1609:b0:35c:8f50:acd3 with SMTP id t9-20020a056e02160900b0035c8f50acd3mr13186210ilu.18.1702421937609; Tue, 12 Dec 2023 14:58:57 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702421937; cv=none; d=google.com; s=arc-20160816; b=vRrA9DKbIjHgLJqU8Rhb0M1kdE7PC7s9EVsWzHkzxZTHLxjnqGzHLoFM/MVbrdaGfb ISjPkRQdk/WTv/rvC3vyaC6/0ViqnPxQ2ajzgaDqG0xrQKIMIygu3GFPLGv+Oj3E39hj hunlUS5/3nwTPFYZdfEup5ELF7ToP9lW5DV0qOqqFIzxDZEguuKfWI1Z1AHnNDn2h1oe IQFVcpCuQdovmkQAmt/47bv5mO/IlC5Jsz1JIB26S1japm4gcive+nJc5mN7CGETNUMl Iv7Ui3UGUVhBtdKnjAb2+O2v8slV4BOJvftMWrQzQGHCm6t5puPuV+Unb9KPAos+MLMm jCmQ== 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=ZDYI96oaJmpGgcxheV1xw9QQettrzMHe/j2ukeYjaYk=; fh=OYyyqLl+7kHm8cIdrlovIOx10UybNbaabSjUs6DJgRA=; b=0voo+VM3BQ22ueCkmrtDrLWgPzgYhxgPXb79J6jDODhjpClEVIRb7va+XWAEsL71Mn gHPP2c2rSDqFPiMHyCY+xZDDSof/B+4CBsyIonud7ElnEkva1sL42oPvqJnfaNiND1ML 3EZ3AbkFdyuok9U3711prCLNlJWlWtAevrr3sLWmFHFL2WIzEkj7E4Fs1rbDXLMAuoOW fbDfD8oPZ4xJSdEp/i1Jc1vwSaGJ4xLsw9GuzfGZw9oXLlSOYJj7nd/TR312L0RPl8FU QAryoTCzMKjBUuQqwiCmNwFQQe2/4lhT5lMiyT12GfAyFjsZy1v+qv6xhzR95Ag24jTn 4D5g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=M287BWsT; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from agentk.vger.email (agentk.vger.email. [2620:137:e000::3:2]) by mx.google.com with ESMTPS id j5-20020a170903024500b001d344ae7ac3si910742plh.533.2023.12.12.14.58.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Dec 2023 14:58:57 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) client-ip=2620:137:e000::3:2; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=M287BWsT; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id 340FB804B878; Tue, 12 Dec 2023 14:58:55 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232371AbjLLW6j (ORCPT + 99 others); Tue, 12 Dec 2023 17:58:39 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36256 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232173AbjLLW6i (ORCPT ); Tue, 12 Dec 2023 17:58:38 -0500 Received: from mail-io1-xd34.google.com (mail-io1-xd34.google.com [IPv6:2607:f8b0:4864:20::d34]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5688AAD for ; Tue, 12 Dec 2023 14:58:44 -0800 (PST) Received: by mail-io1-xd34.google.com with SMTP id ca18e2360f4ac-7b3a8366e13so261504039f.1 for ; Tue, 12 Dec 2023 14:58:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1702421923; x=1703026723; 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=ZDYI96oaJmpGgcxheV1xw9QQettrzMHe/j2ukeYjaYk=; b=M287BWsTBhEpqFXKf8JJuZDW3YuBqwtRUPfWXn/bnfz68g5FCLlZ/jKRUCpK9HeMn0 00Pvb9OBrSQ5styTk9N2mIZGPNBYrOI6+ZtEwr2LJOLw4BMqUIkFOW4esm2ZZcLsrUoe B/hvGBT1LVJXb2V7PbcA93HSovrit/QP0IQs+GQWki98LRzFZwCuMkhNqfZ0jO/jqPUB y0b5HX1bjgwmmYMwDgrT9JXHe7l3EWGrI12QoVy7PHiogHfG8qDoNSe4aFrWur+Sg/lO HC7n0pRFhx919tWqZ4tHIr6ebSroocU8tXCxqwGAalietlxFveIG+rremI7rWc9mFRu4 NxLA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702421923; x=1703026723; 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=ZDYI96oaJmpGgcxheV1xw9QQettrzMHe/j2ukeYjaYk=; b=TvKyQ0nEdHrjFI5AyXwItEN/LaTd1RHDc0PUKgVnK6pNZK6qQldTXP2RVzmOiL9gVJ 96XNGvnGgAeuRAB7nKNA5WT3qjL3jMinmlrp46AQBVoKFKcWQGjY1wMQeduqqUNVcH5g H4KlUINnkIrrPTREIRnDzMPFARvEaJ8S66B57S2vXxmHN4azRzqlgwY/DfD7Jf16/QhZ Q1LRqOSt49f04FbK1AYLNYcvgwVeh8GficoUOwnY3ZJ5X04DnpKfhvYC/x30If27JtOI +XBURAQa0ziP/UNEUpSaUgw/CzBLanUbhATsuOLCRQpEkfdz8lZZmEkIBZBSuQK1o4na 6KHw== X-Gm-Message-State: AOJu0Yz/yX1nnb7LgT4aonofp1gTN1hZ/XcVJxvUGlQ/aAptnMG1V7LU o+2833/lLMfNkJzr2btByM6pOvcrl96SYXrp6qI= X-Received: by 2002:a05:6602:342a:b0:7b7:5850:7d62 with SMTP id n42-20020a056602342a00b007b758507d62mr4505169ioz.19.1702421923589; Tue, 12 Dec 2023 14:58:43 -0800 (PST) MIME-Version: 1.0 References: <20231206-zswap-lock-optimize-v1-0-e25b059f9c3a@bytedance.com> <20231206-zswap-lock-optimize-v1-3-e25b059f9c3a@bytedance.com> In-Reply-To: <20231206-zswap-lock-optimize-v1-3-e25b059f9c3a@bytedance.com> From: Nhat Pham Date: Tue, 12 Dec 2023 14:58:32 -0800 Message-ID: Subject: Re: [PATCH 3/7] mm/zswap: reuse dstmem when decompress To: Chengming Zhou Cc: Vitaly Wool , Johannes Weiner , Michal Hocko , Seth Jennings , Dan Streetman , Andrew Morton , Yosry Ahmed , linux-mm@kvack.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-0.6 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.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 (agentk.vger.email [0.0.0.0]); Tue, 12 Dec 2023 14:58:55 -0800 (PST) On Wed, Dec 6, 2023 at 1:46=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 mutex. Ah this sounds like a good idea. We have to grab that mutex anyway - might as well use the memory slot that is protected by that mutex. > > Signed-off-by: Chengming Zhou > --- > mm/zswap.c | 29 +++++++++-------------------- > 1 file changed, 9 insertions(+), 20 deletions(-) > > diff --git a/mm/zswap.c b/mm/zswap.c > index a6b4859a0164..d93a7b58b5af 100644 > --- a/mm/zswap.c > +++ b/mm/zswap.c > @@ -1758,9 +1758,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)); > @@ -1782,27 +1782,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; We're moving handle (un)mapping and the memory copying inside the critical section protected by the mutex. Seems fine to me - zswap_store() already did this IIUC. > 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); > @@ -1813,15 +1804,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: So it reduces the chance of zswap_load() failure due to unable to memory allocation failure? Nice! > + > spin_lock(&tree->lock); > if (ret && zswap_exclusive_loads_enabled) { > zswap_invalidate_entry(tree, entry); > > -- > b4 0.10.1 Reviewed-by: Nhat Pham