From patchwork Mon Dec 18 08:22:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chengming Zhou X-Patchwork-Id: 13496351 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 9609DC35274 for ; Mon, 18 Dec 2023 08:22:38 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id ED4708D0007; Mon, 18 Dec 2023 03:22:37 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id E84F08D0001; Mon, 18 Dec 2023 03:22:37 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C631D8D0007; Mon, 18 Dec 2023 03:22:37 -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 AFB248D0001 for ; Mon, 18 Dec 2023 03:22:37 -0500 (EST) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 7D18B16021D for ; Mon, 18 Dec 2023 08:22:37 +0000 (UTC) X-FDA: 81579247554.03.347A504 Received: from out-173.mta1.migadu.com (out-173.mta1.migadu.com [95.215.58.173]) by imf18.hostedemail.com (Postfix) with ESMTP id 8E59B1C0005 for ; Mon, 18 Dec 2023 08:22:35 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=none; spf=pass (imf18.hostedemail.com: domain of chengming.zhou@linux.dev designates 95.215.58.173 as permitted sender) smtp.mailfrom=chengming.zhou@linux.dev; dmarc=fail reason="SPF not aligned (relaxed), No valid DKIM" header.from=bytedance.com (policy=quarantine) ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1702887755; 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; bh=T3GWg0eQ/f74Kkc/EMROSUotrx6hZIGFelAG2ou5unE=; b=odkBYsSNmpP6q8XenOupyDPdVHlJHLkc14DJm8lH7huWpUGynpNVgykphITlnNJu3G8/NN iGZRz6CPFoXT+wqV0wcUuXtU2qcg8LgBs/dhGG5jYIAnpwapP3hk1rKxTYalPDQ4B/CnBA YaALptIiYjmNFe4LukAkT9pbS0o3jYU= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1702887755; a=rsa-sha256; cv=none; b=dnjAaBi6OaDn3qwBVfTBRNwVGTTFtlOtHIhZLQsC9IFqXNkXT3EF0fgeBBfs3lbPTcuN93 VHvqbw5yjcxyxspfasVK9o3P9sn12WjRTXwvpCg3DCX1SzMj4rQKee32RzclKVvAVrrTvq xZ/xSgmfU571cEr2dSxHio8nkhzhcFI= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=none; spf=pass (imf18.hostedemail.com: domain of chengming.zhou@linux.dev designates 95.215.58.173 as permitted sender) smtp.mailfrom=chengming.zhou@linux.dev; dmarc=fail reason="SPF not aligned (relaxed), No valid DKIM" header.from=bytedance.com (policy=quarantine) X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Chengming Zhou Date: Mon, 18 Dec 2023 08:22:01 +0000 Subject: [PATCH v2 2/6] mm/zswap: reuse dstmem when decompress MIME-Version: 1.0 Message-Id: <20231213-zswap-dstmem-v2-2-daa5d9ae41a7@bytedance.com> References: <20231213-zswap-dstmem-v2-0-daa5d9ae41a7@bytedance.com> In-Reply-To: <20231213-zswap-dstmem-v2-0-daa5d9ae41a7@bytedance.com> To: Seth Jennings , Dan Streetman , Chris Li , Nhat Pham , Vitaly Wool , Yosry Ahmed , Andrew Morton , Johannes Weiner Cc: linux-kernel@vger.kernel.org, Nhat Pham , linux-mm@kvack.org, Yosry Ahmed , Chengming Zhou , Chris Li X-Developer-Signature: v=1; a=ed25519-sha256; t=1702887745; l=4229; i=zhouchengming@bytedance.com; s=20231204; h=from:subject:message-id; bh=aB+tFXtYFtFovOCKjFmKhfN20WzOhqogX+a8O/o262s=; b=Kd7hGtuBRqJ6usbhtM5mZ4Sj4K7wMC6fwtx+Dp2KUGxlw6etTViNm+SeBpEiBp80WtiJS41o8 B1a8dk6TqidDIJodwMtroZsMduENCYRUKjYDOAzxK6wHZnm5C2hLLQ7 X-Developer-Key: i=zhouchengming@bytedance.com; a=ed25519; pk=xFTmRtMG3vELGJBUiml7OYNdM393WOMv0iWWeQEVVdA= X-Migadu-Flow: FLOW_OUT X-Rspamd-Pre-Result: action=add header; module=dmarc; Action set by DMARC X-Rspam-User: X-Rspamd-Queue-Id: 8E59B1C0005 X-Rspamd-Server: rspam11 X-Stat-Signature: 86yhz8apxzz4wxbirbsi61rehos8s5o9 X-Rspam: Yes X-HE-Tag: 1702887755-5409 X-HE-Meta: U2FsdGVkX1+JKm235B5P5bqpxBMZTNGROTJ6LsEdk5Qyeq+WxwT1Neu+gcT2cUw6N0lz6Q1HR8jSzDRblxXkyT2FQR7o9f9T1hZDGWV2Jc8HyZKyOZ1aozy034RcMhY2JwMysBdYRVHDAu2pffVB1Mb6yoJ7/i21C4x6wVsbNsXv/Avxt2qtBbScqlEZkheEaHhj3D53JPV/ZarxLoqObMLdqmrn0w24o8iRDdbDn6Xzud/rpBeCR74EOmOk6ZF/TEUTe45MJKmmFqWibvEGS5/f+W3Bn0EkNW54WpHtD26wEXcWKUDK9cwSIe2JjBi+coEAS7L05bhcgeUT847BijgtASlJXxGEyJ6Pspp9mXtiwAYiM11PQ8DnE3nfqVHSCRDNTma70gf91B6tq8mq4xmeRSB1+Warq4V3GaNEbM2bSSq83u1redU7zM8wsM6bGH3pOziTogzyICJGfndTwf2D5qITOJlQHy37brcaCH3VwTT8rStYVK+5pTrb0Y2jgBcgW52SA6mBlZt6CFPPXqtE7wHMWgQXJE1/aYXefFzHXpCbtTbwn+cpHQPQJhw+Kh+WeVCvHBhGOfLCAcaxNmhAHFqhaCi0gApNIMMzYW/6H6QdEYSc24bjTnPRU+zyMBGjsj99Kt9qjnjUgUVGi+88HJr9alLoTv35vzA3Bt7OP5ok+CILvwcNB/Ptx2exok2NizKNKC5YIhPIFzg2N7x0DODBfgF8YhWbYlmW0y556jNrv7z23ID6qK4abO08nm8mbymmOkuYlw7kwrOwMihDwwj1TSQtYYdQVRHaYWFn2EIDRdazf9mIEs8g/oD1Rem+ZAe4Ej976+nGgmKn6MZ1XNY1M0KEiKuXfZOgul+y23UgzqfyBWTc3fEpbK2lhUACcCHtAQaPKp4kAv2jHOsNBwwqUD7qnesB5IfbRLvZgGIuoq+QG9ArRcTxiqQbMt22fVhj5ZFrzj3/9G6 bOsJ9uAU NKLfJrGMyNemvuFmYT78oXE5h8y5fsChBL3sh1cuiEjavnVpbJqNR+D2jFc1epLlR8K/gf4VCOaXAB6nqc+IBViWFatyCHA45/R8Nq+Rw6KzgU4KD9nH8jsS0tnxOXk2ZpUCUhh+BbWUSl8Vw6nT/K5VKdCwdJszQ3jKTeE7hpcLgyOJ3kAoMnmrXYz4L3ZgsuKe9wKO3ZTXDJKEoHyCzC65zU4vf2wuhuu+78oEp+OhRYAEgTDQiZYnUytp1W1GTHoUObyHumL5tAMdtOYo8McDlWeJ5kUR8RqzpqDUaz1IwGd7uSy/FY3KVwDJC3m9ZGe/JtLSHsPtZWUTHzz9Cbt4zwqZAUJ5MuaDkT6xf1iTbek5Pd8BGCoSvmJ1zgMz9nkRqqwlXZxvgKZTiHKsxMQ4VmkB+efHfxJcnTdkrJCS3ZsZFtzQQnSgRPO2+ELVLky9o 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: 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_entry *entry, struct crypto_acomp_ctx *acomp_ctx; struct zpool *pool = zswap_find_zpool(entry); bool page_was_allocated; - u8 *src, *tmp = NULL; + u8 *src; unsigned int dlen; int ret; struct writeback_control wbc = { .sync_mode = WB_SYNC_NONE, }; - if (!zpool_can_sleep_mapped(pool)) { - tmp = kmalloc(PAGE_SIZE, GFP_KERNEL); - if (!tmp) - return -ENOMEM; - } - /* try to allocate swap cache page */ mpol = get_task_policy(current); page = __read_swap_cache_async(swpentry, GFP_KERNEL, mpol, @@ -1465,15 +1459,15 @@ static int zswap_writeback_entry(struct zswap_entry *entry, /* decompress */ acomp_ctx = raw_cpu_ptr(entry->pool->acomp_ctx); dlen = PAGE_SIZE; + mutex_lock(acomp_ctx->mutex); src = zpool_map_handle(pool, entry->handle, ZPOOL_MM_RO); if (!zpool_can_sleep_mapped(pool)) { - memcpy(tmp, src, entry->length); - src = tmp; + memcpy(acomp_ctx->dstmem, src, entry->length); + src = 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 = 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 = zswap_find_zpool(entry); - if (!zpool_can_sleep_mapped(zpool)) { - tmp = kmalloc(entry->length, GFP_KERNEL); - if (!tmp) { - ret = false; - goto freeentry; - } - } /* decompress */ dlen = PAGE_SIZE; - src = zpool_map_handle(zpool, entry->handle, ZPOOL_MM_RO); + acomp_ctx = raw_cpu_ptr(entry->pool->acomp_ctx); + mutex_lock(acomp_ctx->mutex); + src = zpool_map_handle(zpool, entry->handle, ZPOOL_MM_RO); if (!zpool_can_sleep_mapped(zpool)) { - memcpy(tmp, src, entry->length); - src = tmp; + memcpy(acomp_ctx->dstmem, src, entry->length); + src = acomp_ctx->dstmem; zpool_unmap_handle(zpool, entry->handle); } - acomp_ctx = 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 = 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);