From patchwork Wed Dec 6 09:46:26 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chengming Zhou X-Patchwork-Id: 13481320 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 0A6BCC10F04 for ; Wed, 6 Dec 2023 09:46:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4E6C06B00A2; Wed, 6 Dec 2023 04:46:41 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 4972F6B00A3; Wed, 6 Dec 2023 04:46:41 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 386616B00A4; Wed, 6 Dec 2023 04:46:41 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 2ADBD6B00A2 for ; Wed, 6 Dec 2023 04:46:41 -0500 (EST) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 0562E1A012F for ; Wed, 6 Dec 2023 09:46:41 +0000 (UTC) X-FDA: 81535913802.02.B730B88 Received: from out-172.mta1.migadu.com (out-172.mta1.migadu.com [95.215.58.172]) by imf24.hostedemail.com (Postfix) with ESMTP id 232D7180023 for ; Wed, 6 Dec 2023 09:46:38 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=none; dmarc=fail reason="SPF not aligned (relaxed), No valid DKIM" header.from=bytedance.com (policy=quarantine); spf=pass (imf24.hostedemail.com: domain of chengming.zhou@linux.dev designates 95.215.58.172 as permitted sender) smtp.mailfrom=chengming.zhou@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1701855999; a=rsa-sha256; cv=none; b=fvTR9Lj8Qea/ls4L+QtDlsPFk3xK0L+uwAUOA4coQPDS1NLlmWjg/DQI1iM2Rb6KPN/TL6 9iImcrXHvUNkWKHMlUGwKJU5AB2UqWVkM8qYUGNrbzCDbtY+HFfsRWPGHlS6ZzasQpSyeF AldHLE0xmi7lIgc4CxqAGvkkvQgGfVk= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=none; dmarc=fail reason="SPF not aligned (relaxed), No valid DKIM" header.from=bytedance.com (policy=quarantine); spf=pass (imf24.hostedemail.com: domain of chengming.zhou@linux.dev designates 95.215.58.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=1701855999; 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=vGUImAAKdFNwNoK42Ha+6s53PcbZS8gtKpqCNEPV03Q=; b=ITUgXk9dfnDZ2DYZMwBygZUMsYB2VJMDX/NVM/90UcT3r+JKpiykcDuEIlkn07GW+8ruFY kj97F68+/LBAbyYVhxjyT2h6lfWXyRCP+N1aP70Mu37mj9FEO36fQU6dT+VYq0pqcMJLFn +pNTBLuVVH76J7F+m4IU1GAZEirUPYU= X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Chengming Zhou Date: Wed, 06 Dec 2023 09:46:26 +0000 Subject: [PATCH 3/7] mm/zswap: reuse dstmem when decompress MIME-Version: 1.0 Message-Id: <20231206-zswap-lock-optimize-v1-3-e25b059f9c3a@bytedance.com> References: <20231206-zswap-lock-optimize-v1-0-e25b059f9c3a@bytedance.com> In-Reply-To: <20231206-zswap-lock-optimize-v1-0-e25b059f9c3a@bytedance.com> To: Vitaly Wool , Nhat Pham , Johannes Weiner , Michal Hocko , Seth Jennings , Dan Streetman , Andrew Morton , Yosry Ahmed Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Chengming Zhou X-Developer-Signature: v=1; a=ed25519-sha256; t=1701855988; l=2376; i=zhouchengming@bytedance.com; s=20231204; h=from:subject:message-id; bh=fpCgC2uqFVZCuogrKgOI9EeZNXHQYwaI+M0J57wRCA4=; b=wudSyMvTqyzG7OZuJjTkGo+D9zCpS8Tofe/rODy3CP0YFRdLMSz4sWGiyGOubX4khTnGFFSyX Zm343RFwsfDDmmErMRr+FCJT56Wg4VhAosu9lLVrHVfRY+DTTGtIZ9b X-Developer-Key: i=zhouchengming@bytedance.com; a=ed25519; pk=xFTmRtMG3vELGJBUiml7OYNdM393WOMv0iWWeQEVVdA= X-Migadu-Flow: FLOW_OUT X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 232D7180023 X-Stat-Signature: yfzzrkwnma1ijfrwr3tyb1gacpdfx9bn X-Rspamd-Pre-Result: action=add header; module=dmarc; Action set by DMARC X-Rspam-User: X-Rspam: Yes X-HE-Tag: 1701855998-982497 X-HE-Meta: U2FsdGVkX19d0HTExebnmayb0VUmArbbW4j9sgqc1u9MJ03Rx6k2tPjRnXWfj19DBcdGdl3uOSBSIkTwEclN6ICoA6K8AiOyn1kNSglZaOkvWC78oHMozsQG3MKD86924Agwrrh/HfyonSdqzRCzlX4efZ6TmeQifInOy7p9y73fwiqp0Vsx9rFxvCehqoZWdSTmLa0ElqAn2fQLbj8o25S6+OA9lvFAsqUG6MUhEf4Txg4mFpIUHjSJF9Chxorj7D5jGVztNbU6dTEf5bG6DKxory0EYK/oqcUfint6aO+X6G+a9Wl+6qbWr14Xcjj3SfqoB38/J5RnNVU/gSIKnu652SHpvvYwsvq3jzzReurEzQKmQNpJX9QP9QOW6tQsbff2fgAUPRodT1BRNOuQ7xhZ3ClRH+Cs1QsSdqLeCusHHAJl093KiIl+9Y8H+o0DqEOgmla8k8K6Nm7zd3CxUCiZP2MCMdXJsgHWAkxq3YBWZLhMorwTN1H31m9Dq3eqbStqFktC7ShmM86/1uXIB4c8HztQCAsiJyalaFSfJqRGsJau7NWwW9IGQgvqkLz4OyWjheEkrmnckDVw4U3b4kWCbY9yFaczy1jjBEOF2BzBMaNMhElJPnpq9w6Xha7jU/D9rXDNHkj78bUNXT6FCBVqreobaCh7fscmhMWH/3LjTFkVT4cEgiI+Xux+grLivehxP5nxp/2g8Tj3yrPnqDOA6Iqexo8SjIuuiBAxyubZrzphH7dYKWlVvSjUnArLnw0b2yJNbLCLD/Rm92omVCg48BIpEgYIqvF7aykaB2sxHuJJN4IEbwYHyyn4X34+wNQViJuMjmig7ClnsptHJFccDSFGWClY6e1G5XDjfRQDaF617g05mRhlNpmNgap2NigLjkOEVmiAUjw8qwdvHI1WBLPz3erJHy2QnMvVDPh9t6Bkc8fQaW/q9MVJDFvhDAsA4ZtmKBHWaGPPYur 0MwMfD9z rNDQdg1FPpBiJvAJkzDhO0hay6zPjQ0YgYimLPax03fVdM1Bek8Y4sZotL/Aynr8LXGhwp6mcd9GEfzcmOjeyzBYbEBr24+GHK0BLR2nHTd02YN3YBAjWeN9ry3S9cwcLHV0v0u/h9E5qS8K1qur+RgURaYoE8Wg6onrjiJIiieE0HgteJ/HhbhO5S3N4tGsLsbNJ8XsfnPeJpxbntKCwnXN3kM79f7XzMUzqPlYIaZZ2wZHjI4sMgbkediUGA76AgJQi76Wp3bysXUqJNCkGdepfSSjJX/A6jKjGkqLKWjvNEiioyezRcl53bt9KCtbJ+Y1SE5NgPGaVYuM= 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 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 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 = 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 = 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); + zpool = zswap_find_zpool(entry); + 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); @@ -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 = 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);