From patchwork Thu Dec 28 09:45:42 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chengming Zhou X-Patchwork-Id: 13505764 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 41662C47073 for ; Thu, 28 Dec 2023 09:46:26 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B31F66B00B2; Thu, 28 Dec 2023 04:46:25 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id ABBC96B00B3; Thu, 28 Dec 2023 04:46:25 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 982766B00B4; Thu, 28 Dec 2023 04:46:25 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 83A3C6B00B2 for ; Thu, 28 Dec 2023 04:46:25 -0500 (EST) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 530521403D7 for ; Thu, 28 Dec 2023 09:46:25 +0000 (UTC) X-FDA: 81615746730.03.0EED690 Received: from out-186.mta0.migadu.com (out-186.mta0.migadu.com [91.218.175.186]) by imf09.hostedemail.com (Postfix) with ESMTP id 6CA96140015 for ; Thu, 28 Dec 2023 09:46:23 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=none; dmarc=fail reason="SPF not aligned (relaxed), No valid DKIM" header.from=bytedance.com (policy=quarantine); spf=pass (imf09.hostedemail.com: domain of chengming.zhou@linux.dev designates 91.218.175.186 as permitted sender) smtp.mailfrom=chengming.zhou@linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1703756783; 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=Jx1R7SbuEotMSzAWjxU0CvOuBYFjtfh3gj1fldL0jmQ=; b=xWXhXVlXFbCIYPyttf4Kjcfl90VhxyyIZXV7gdtPO9pPMKQ931EarHo5DdvyWTa0AiNGYI uC3M67pTbH0WjP/DFdEoQhdmEMxdgNNc/dAfZa8UAXuhcmwrV1pyQq455ll8T/ewquLfEF vXujYVQTTde9R/T2nJ+/lLtPtKrN4PI= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=none; dmarc=fail reason="SPF not aligned (relaxed), No valid DKIM" header.from=bytedance.com (policy=quarantine); spf=pass (imf09.hostedemail.com: domain of chengming.zhou@linux.dev designates 91.218.175.186 as permitted sender) smtp.mailfrom=chengming.zhou@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1703756783; a=rsa-sha256; cv=none; b=Eo220QwGMWPfp8tMnoWnCyYd1R3A0pkjM8Z2u3YQ/sgFRnyc0GNx/Lprf+/k/QyOPRe8Xv 3naMGEd/5OfZlfoR05bwacYl066EbIaAanAWXYQ/nqpScV+4wYCeinkjiJudkjxCUd3zBH zpbLHRmwTrcmsDp2/X6+4L0SRzIOF3E= X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Chengming Zhou Date: Thu, 28 Dec 2023 09:45:42 +0000 Subject: [PATCH v5 1/5] mm/zswap: reuse dstmem when decompress MIME-Version: 1.0 Message-Id: <20231213-zswap-dstmem-v5-1-9382162bbf05@bytedance.com> References: <20231213-zswap-dstmem-v5-0-9382162bbf05@bytedance.com> In-Reply-To: <20231213-zswap-dstmem-v5-0-9382162bbf05@bytedance.com> To: Barry Song <21cnbao@gmail.com>, Yosry Ahmed , Nhat Pham , Andrew Morton , Dan Streetman , Vitaly Wool , Johannes Weiner , Chris Li , Seth Jennings Cc: Yosry Ahmed , Nhat Pham , Chris Li , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Chengming Zhou X-Developer-Signature: v=1; a=ed25519-sha256; t=1703756775; l=4288; i=zhouchengming@bytedance.com; s=20231204; h=from:subject:message-id; bh=oTSr8h2HN5ZVvOErnBDPCcywNhpihCGwWVxYTWIN01A=; b=eZRaOUfTwA7ZmZqcFbY1sJcAk45CveTJ0kOs78M5taspw03yqX+HB6o5DFZ5Pe3HgwS+K9uVc H//cxrexvlCDM7yUYCt/hUgQCvkUWd1J3KZwst83q4EeCVmrfLBqRCg X-Developer-Key: i=zhouchengming@bytedance.com; a=ed25519; pk=xFTmRtMG3vELGJBUiml7OYNdM393WOMv0iWWeQEVVdA= X-Migadu-Flow: FLOW_OUT X-Rspam-User: X-Rspamd-Pre-Result: action=add header; module=dmarc; Action set by DMARC X-Stat-Signature: gc5f4o3w46rzmorxwoe8xnuckhxubdzm X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 6CA96140015 X-Rspam: Yes X-HE-Tag: 1703756783-165320 X-HE-Meta: U2FsdGVkX19ZsNbR9eTDUqgAXz3ufHBSEmiEDbPtCln9K3LlJKB0ZwiK3u0arnBSwoAtdOLggZjyTMhVUyfHFLqKI34tM8aXsjYP7MCb2A6DHRfnqivYoAxmGnDBZSKEPXoKbCTIHU8kc45/oN8T4vvTS/KCqC4oUj3cVlqrtWHancO40fSC+m5+aHlrTFrtxWbLgNVpqx3SjqZHuOonFrWgqov58AODQCsA46MHbZsG6qd1LxwyrhA9ub8Blcig1Sc5J4WB5DHKp/CKy0rOieaLvLSWNhd2dHtn6s1dIsr7+n4pZk3X5YDwua9Nv6ZGm/BKmfUuNKxTzaSaGb6W5y5v78sJyVMQj/tbGLRy3mi7jMExopWatZkckpFiFVc4tQ1QqJ/91WquvrQdqvyDTNYaYKQTZBpRdDJDmB8KdKIq2RpoNwHtYR/IQ4+CasDwPiSTiDM6m5F4Ll87lKiHevLPunYiLS95KtP/LedfttLdV+GYR/sq5tN+UljuPI6q/5V0WRIr5Ra/NQ9KH6RWL9bgEQJY2Ifh2uZWN/pAR8ztW2x/vWKdSHqBksJlOZChTRBNRQSCVWFpW0I7bGMafj1o3mWpXMvGz84SXMw/gFzoKl4yAWWeuKIt580jT5BTQY8YyeURQx0+fCQBTGazmW1zsasbTK5fEOa1ZRx+Z4MATIbx7GrwQtgIcUoerRtYoXJhi+xTScTEInmlhOnvAu/pKuZYRSzxeB79ufFPEvjS6JqWjs5YpZrfN8/ikGUjGg5XEpNrcYWnrV3h9Z9w9VAC743sa6thBrAEctriV/O4H+IfK+P6hsqCC3BMzbeQ79Ntyn9XkqrKABNUwKKizbZ4YOqI9MDU4lqZa/yQ5HD/DH4IEgmtsSTIzxLROZ1bct5TgIUe1vRP8wTU05Ba0PyQGjr7JfhFA65sCbIoYuuvEFpbq7P5b10TAxP6yCQjE+9SyIpZOrf63/mODgm 35X1s9QB VUvy0/G2hnxY1qOPVokQihZRdtgJFs/RcHlPmQEC0I+k46HT2vE2kPDBpVZssIeUjpHoJvAUlU+Ne2RqACgqbp1VzNjnugNGjCj9k62K5KgD0M9gtzyvDXqDYbfmQfJQKxuc+sT0IdoTj/Daxqo8IaL0TuAMRvjDZ6Oqtvwb3nN2Ig0eeqmz3qhG+KblR8cTHJmbCfL76tgdgFGT0X7wpboroLAFZcYcdqo1axbwTz5PT4B7/E0nHKT0zk4q5K+heKuKb1hlOl3TzHz5gieQUoD2B9BRLYfvZXLBbJHOylceUpj4RlSLVri1X+qToMScdWiJ9ByEQlUyK4jTA+zcdQuSJZq/II1p7ggUjEd/07lC4qHcOl0vr6DcxBnZRx0jnADg74cTazFjUnGFQQL1BK8KKEpR0FyzZrj5tWAYQXpYacCs= 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 Reviewed-by: Yosry Ahmed Acked-by: Chris Li (Google) Signed-off-by: Chengming Zhou --- mm/zswap.c | 44 ++++++++++++-------------------------------- 1 file changed, 12 insertions(+), 32 deletions(-) diff --git a/mm/zswap.c b/mm/zswap.c index 7ee54a3d8281..41df2d97951b 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 @@ -1772,7 +1761,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; @@ -1797,26 +1786,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); @@ -1827,15 +1809,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);