From patchwork Tue Dec 26 15:54:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chengming Zhou X-Patchwork-Id: 13504906 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 84E64C46CD3 for ; Tue, 26 Dec 2023 15:54:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1AF896B007D; Tue, 26 Dec 2023 10:54:56 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 139976B007E; Tue, 26 Dec 2023 10:54:56 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E7E876B0080; Tue, 26 Dec 2023 10:54:55 -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 CBD146B007D for ; Tue, 26 Dec 2023 10:54:55 -0500 (EST) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 9DDA3160505 for ; Tue, 26 Dec 2023 15:54:55 +0000 (UTC) X-FDA: 81609417750.11.BBD4D00 Received: from out-172.mta0.migadu.com (out-172.mta0.migadu.com [91.218.175.172]) by imf04.hostedemail.com (Postfix) with ESMTP id CA8E740008 for ; Tue, 26 Dec 2023 15:54:53 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=none; dmarc=fail reason="SPF not aligned (relaxed), No valid DKIM" header.from=bytedance.com (policy=quarantine); spf=pass (imf04.hostedemail.com: domain of chengming.zhou@linux.dev designates 91.218.175.172 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=1703606094; 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=8wWojJj68vjmM4kc0JFut5a3qFzZiRt4b0vz/ZVD3J4=; b=TQfycmwiT+iD/hpMA21jGJGBX/vmXuR9orvsOVy/qa38HuAYj/tLl7jHrbxw9ECMImbuo4 PbiN6DHYgDK3xoOerv3L8Gk9lKUSmph//K8z2IHICHjykRyod+Rr8MCvZlo5OzE/T/YvMR FogDFwGfZkQcnXC3zaqMVHap4mmS4UE= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=none; dmarc=fail reason="SPF not aligned (relaxed), No valid DKIM" header.from=bytedance.com (policy=quarantine); spf=pass (imf04.hostedemail.com: domain of chengming.zhou@linux.dev designates 91.218.175.172 as permitted sender) smtp.mailfrom=chengming.zhou@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1703606094; a=rsa-sha256; cv=none; b=sEd/XtG7GQp0qEjNek7qyfOY2ENGPoVweoalSVU1c5C/l+SVhMipkkC+qGLu9kwwdXueXR ED1wIMQuJGjWYEAUxVsTC1B0CNic7cJ4iWWqEkOKbhUB+5PdFE9Ha+1dWQSzDgmpim+JlM bd/eQgDe7s6laywYqLjFN+ReWwhErCw= X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Chengming Zhou Date: Tue, 26 Dec 2023 15:54:09 +0000 Subject: [PATCH v4 2/6] mm/zswap: reuse dstmem when decompress MIME-Version: 1.0 Message-Id: <20231213-zswap-dstmem-v4-2-f228b059dd89@bytedance.com> References: <20231213-zswap-dstmem-v4-0-f228b059dd89@bytedance.com> In-Reply-To: <20231213-zswap-dstmem-v4-0-f228b059dd89@bytedance.com> To: Andrew Morton , Seth Jennings , Johannes Weiner , Vitaly Wool , Nhat Pham , Chris Li , Yosry Ahmed , Dan Streetman Cc: linux-kernel@vger.kernel.org, Chengming Zhou , linux-mm@kvack.org, Nhat Pham , Yosry Ahmed , Chris Li X-Developer-Signature: v=1; a=ed25519-sha256; t=1703606082; l=4288; i=zhouchengming@bytedance.com; s=20231204; h=from:subject:message-id; bh=ee6CjzVfu3cdsKJFW0DjU4Yqsb6snUyFV2P+WjqVZ2k=; b=/M2fa/388zR47dnBIiQiB32KQcYRHMBqBcSykhWTPfpxspJa5FXo9+SaEpDo1xt/3FIcNllNz cLnkXIGD4JGCQBc8dxPU/SL9D1/IOJXC80mVvuuJ/DxyQRPRAi4uCfR X-Developer-Key: i=zhouchengming@bytedance.com; a=ed25519; pk=xFTmRtMG3vELGJBUiml7OYNdM393WOMv0iWWeQEVVdA= X-Migadu-Flow: FLOW_OUT X-Rspam-User: X-Rspamd-Queue-Id: CA8E740008 X-Rspamd-Server: rspam12 X-Stat-Signature: fndijk1u9sidfwe3yf59fgibcmjf83re X-Rspamd-Pre-Result: action=add header; module=dmarc; Action set by DMARC X-Rspam: Yes X-HE-Tag: 1703606093-569179 X-HE-Meta: U2FsdGVkX1/+8KGkKKztwHN0lT37d90/TQQNN4VDRNElS6ZteK6Q4xkAoj0ubVyUJ/UHZZnM7A5NKXA8YjgXOT0ePd5wJ3yL6XxNKbcvIqPrd+i+gmyBecByVEdkn1E+uGNd5Ca75whHEVIpi+4bLP2ITRwOqT04nhJmkVS+56iX026qUecXTbz8cmNOcZBB+1AQkjveaRY2Zh8pKXv3vvNalaBh7WJhGJDw/0hGKhiCAOeDAxiKR2RrWCyWv5kLciyOQGmcxDVSFuJ0usF+czZq8WMQtndJdJI3JCNCPJNibu1nan9+kO70rHOO+1e6P40qoOLeoWCMnJ8DCn33Y20/F0LPsom3aIPsX/abT8pLiq3gOPivGorAhQAT7ttsfMtpY6VAfpE+qqItqeMQQ7+peikKnkdq0Lny80E1a5Mfz3n1BPA4EElnLfl/w96MYILCiN/OeaOSGZGzpthKJv5zUun9iudyVGakIFNy/GpUIu++DPnVunytR/1GpkWF/g+b420leKuPvR//WWQneYlXDKY7P+QUMrXOF3gz7nVRUAbFpiYi5tcnbbuINW2fqsTPh7MHWLAokukVUAGQLe/kxzhkDwh29bq4athcZpOzz7WHDuLThW598G+avZJ0GtK9h2L5+dcSeMhokPfv4EjfRiZ7Lr3a5lQnlOsGzvUg9RMRwkiaD6kyCS7oIv9M9SsiUK6SVvXU6Ljo/hmxxJQPUJjDJnWUhYexS41q06JUizFxJkLpyBjW/ioPhv6oECDBAnxVdPaqpVi56V7UxpdDNy9ACOVJkDUOfIKDOEIn5301cTmPvPSoMq/0DwdXFYbc1d5P80kLdQiH6qIkvMMs9Gix2w/dyYFgBRBpR74ib3eGhP4OwFb0G7WpGMNlgN9ti6wCx00kZyyIyZaAPbqMSg/rG0dADrGl7BN0JXGnTGlxcZTDFlMYB+kxywUh82Z0z+bHeLqnT84BRE1 2uvwmKMn O/1HIyFpoJSX+AssqTBdSb2H6TKffa97fN7s1u6F7dVGkLMs= 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 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);