From patchwork Wed Dec 6 09:46:28 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chengming Zhou X-Patchwork-Id: 13481322 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 7C6A8C10F07 for ; Wed, 6 Dec 2023 09:46:46 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A3F9C6B00A4; Wed, 6 Dec 2023 04:46:45 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 904956B00A5; Wed, 6 Dec 2023 04:46:45 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7571B6B00A6; Wed, 6 Dec 2023 04:46:45 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 5F1536B00A4 for ; Wed, 6 Dec 2023 04:46:45 -0500 (EST) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 37AEBC0131 for ; Wed, 6 Dec 2023 09:46:45 +0000 (UTC) X-FDA: 81535913970.21.69509F5 Received: from out-178.mta1.migadu.com (out-178.mta1.migadu.com [95.215.58.178]) by imf25.hostedemail.com (Postfix) with ESMTP id 4C876A0020 for ; Wed, 6 Dec 2023 09:46:43 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=none; spf=pass (imf25.hostedemail.com: domain of chengming.zhou@linux.dev designates 95.215.58.178 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=1701856003; 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=rtbcaV9uZyvJ49g+RnmIBpB2G5m2DALCMWgXlaPRAwo=; b=KJfHRThBqUDIzxJp7JsRCfCSKEo6/bEUPCDn7cjqN8EQZKxCz9m8t58tIBhkIe/cXT+urU yPlkMh5LdCjkWrmQgJxxctakQcyNUw/vR2GIQt6TJwuC+aVPdbXUeRru1ra3eix01iS96u rWlZgVUx9QVJYdheV3v9qrK/8/L5M40= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1701856003; a=rsa-sha256; cv=none; b=h6oCHueXdalPNLOVX74Yz+wwy72easkex7aJzXxlTVjLxzzkGo2mANG0d8bL+RiDHQ/NrK fn6MZuYkNQ+4gALYlDVVvkaNNs2gtVnMpRC9J1khvSWcymMaQqzKGHfeZ1J/7wyKor+qbl JDoFi6FjjJ/fxSSKA3HaJUHy88LcB2o= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=none; spf=pass (imf25.hostedemail.com: domain of chengming.zhou@linux.dev designates 95.215.58.178 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: Wed, 06 Dec 2023 09:46:28 +0000 Subject: [PATCH 5/7] mm/zswap: refactor out __zswap_load() MIME-Version: 1.0 Message-Id: <20231206-zswap-lock-optimize-v1-5-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=5290; i=zhouchengming@bytedance.com; s=20231204; h=from:subject:message-id; bh=ju3QBW8cgbkzkTilyJdKpdv6IgD35PF5PphWVt2TBXg=; b=sN2QzBgyvVSvfWoqMPvo+BA+Z/wX8hCyJ3NhGmP4yMNcUemm3UiYwXWjJiunDYw+3nKWn0X+s um4PqJ1hXePCeO2kEV8Gev6JvZihkII6L9Ti8GacRbiWlvx29DlAv96 X-Developer-Key: i=zhouchengming@bytedance.com; a=ed25519; pk=xFTmRtMG3vELGJBUiml7OYNdM393WOMv0iWWeQEVVdA= X-Migadu-Flow: FLOW_OUT X-Rspamd-Queue-Id: 4C876A0020 X-Rspam-User: X-Rspamd-Pre-Result: action=add header; module=dmarc; Action set by DMARC X-Stat-Signature: j15nsiszx5pyj7i93d4k8u6x9xujhjyi X-Rspamd-Server: rspam03 X-Rspam: Yes X-HE-Tag: 1701856003-765298 X-HE-Meta: U2FsdGVkX1+ZdLTWGpCa2kw3ie9ONqm+pWDQt1nt6crf+fN3XkMR8U4/P0a25+1gmydvjOSDz4Do+b4QLVFVwuO8Oz9/UrgR5UsIDKw/Ie3rz6OA3YG+IuKWXZATraXNjPJ+xKupzzYq0CmsRFdNXGmAA47U2VNJsGrOkxTluHJKVda6aKLXh7hYYhUWKnBd/W2eFs3i5rGSCknHKpXlgF/g29q+wew2wTJ5+kHbhFqqTQpeaVKdo+ykT9jsnQ0r/pQIfV9fFLHBp9vpPyvHiK2ditoRaUXg5oCRr7kpBQRojNvt0kBIV7o18CIURQWWnQfBqWxlvmN0J0fr3XA4r/4orzp4jVV1sAOI5urjIURBHDkQvPsrSqAOJZHgoAqVi1rqI9BlftEBKACOwJLKdGAa60Fag+CPgjBYvnrWhXKqr1tEtbG5LxpKVpUlH5BAqaCxXgkrffpDMHpac+nifHC+L6+xKh/PBoEoKqRk/T2sStzNNqDjp6OjgPbwklC2G9ZKGc64avJUKWQ4KPhJ2lzup9HN5PH2HN/dShuiL0lwlxypMe0G6XEtp6Y7r1bkYfxxwgyaPAHCf0N54OwSJMy7rInZ2jHjTgExHpCH5+H/5U9QNATbdXeuAG1rlcCmhMycmhiJsXrFjHymcsEj6A35BKTg5X0Hj+jRtfsG7KxUWPh26IhIh4fFvfQlfmpqWAfl6v/HX77l8SUqBOkRVwICpcLXafDIAiMoQQu1hfZwk3oMGiwG4Jtr9ahmM7UQqM2gBIM8IqI1LCtTW8HGk65AZMRVlmvYobwH029wRkAhKiZIc4fd/kpbUjfa0ylXRB9L8EMq5e0rEgIByGuJwHilHyZMeMcxh5zLDyepsO1Asg6ZfCVQiw4GCp0j5rdmMiW3rxrum1HhM5bqWmQQ4Tno6gZq9/r6sfVOq6fW41WXqJwUlKEjp1Ej2p/fT0WX8AWo0HOof4Lxjq4NuPI yth6C6MH pqxXk1mkykGKvb0fyrOSeSmHp94c2LScFuGLp7uYr5DTTif7Arr0bbH6czfwLx4N9859u9yWTH21SsMci+ek456aLVrUnpjFrcG23CchMf229ceWf+VbOMeCH8sqxZZGLAwsi/GcRNqVfIib7TTxixZRrC7EfCEdP9++s7nZG9diCOgUKWTV403pi9U+bdcuEXzF+H0SxpSYIZ+P0hPMDi6jqbJgTb9mwP+j9RDP1c1uooKS5FpuH/vQisBFQnUu6ai0WOVpLecySHztL+bDBM22OpDakXj9nA8u/F2MF6fY4JqTehVEGw6qNMhHa5ztpFKGAQ1OneQLhdyo+UxGxP+k0HiQbVMPnd7/oh4yZB1bF1467QhVEfDTYoQ== 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: The zswap_load() and zswap_writeback_entry() have the same part that decompress the data from zswap_entry to page, so refactor out the common part as __zswap_load(entry, page). Signed-off-by: Chengming Zhou Reviewed-by: Nhat Pham --- mm/zswap.c | 108 ++++++++++++++++++++++--------------------------------------- 1 file changed, 39 insertions(+), 69 deletions(-) diff --git a/mm/zswap.c b/mm/zswap.c index 999671dcb469..667b66a3911b 100644 --- a/mm/zswap.c +++ b/mm/zswap.c @@ -1380,6 +1380,42 @@ static int zswap_enabled_param_set(const char *val, return ret; } +static void __zswap_load(struct zswap_entry *entry, struct page *page) +{ + struct scatterlist input, output; + unsigned int dlen = PAGE_SIZE; + struct crypto_acomp_ctx *acomp_ctx; + struct zpool *zpool; + u8 *src; + int ret; + + /* decompress */ + 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(acomp_ctx->dstmem, src, entry->length); + src = acomp_ctx->dstmem; + zpool_unmap_handle(zpool, entry->handle); + } + + sg_init_one(&input, src, entry->length); + sg_init_table(&output, 1); + sg_set_page(&output, page, PAGE_SIZE, 0); + acomp_request_set_params(acomp_ctx->req, &input, &output, entry->length, dlen); + ret = crypto_wait_req(crypto_acomp_decompress(acomp_ctx->req), &acomp_ctx->wait); + dlen = acomp_ctx->req->dlen; + mutex_unlock(acomp_ctx->mutex); + + if (zpool_can_sleep_mapped(zpool)) + zpool_unmap_handle(zpool, entry->handle); + + BUG_ON(ret); + BUG_ON(dlen != PAGE_SIZE); +} + /********************************* * writeback code **********************************/ @@ -1401,23 +1437,12 @@ static int zswap_writeback_entry(struct zswap_entry *entry, swp_entry_t swpentry = entry->swpentry; struct page *page; struct mempolicy *mpol; - struct scatterlist input, output; - struct crypto_acomp_ctx *acomp_ctx; - struct zpool *pool = zswap_find_zpool(entry); bool page_was_allocated; - u8 *src, *tmp = NULL; - 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, @@ -1450,33 +1475,7 @@ static int zswap_writeback_entry(struct zswap_entry *entry, } spin_unlock(&tree->lock); - /* decompress */ - acomp_ctx = raw_cpu_ptr(entry->pool->acomp_ctx); - dlen = PAGE_SIZE; - - src = zpool_map_handle(pool, entry->handle, ZPOOL_MM_RO); - if (!zpool_can_sleep_mapped(pool)) { - memcpy(tmp, src, entry->length); - src = tmp; - 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); - acomp_request_set_params(acomp_ctx->req, &input, &output, entry->length, dlen); - ret = crypto_wait_req(crypto_acomp_decompress(acomp_ctx->req), &acomp_ctx->wait); - dlen = acomp_ctx->req->dlen; - mutex_unlock(acomp_ctx->mutex); - - if (!zpool_can_sleep_mapped(pool)) - kfree(tmp); - else - zpool_unmap_handle(pool, entry->handle); - - BUG_ON(ret); - BUG_ON(dlen != PAGE_SIZE); + __zswap_load(entry, page); /* page is up to date */ SetPageUptodate(page); @@ -1496,9 +1495,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 @@ -1755,11 +1751,7 @@ bool zswap_load(struct folio *folio) struct page *page = &folio->page; struct zswap_tree *tree = swap_zswap_tree(swp); struct zswap_entry *entry; - struct scatterlist input, output; - struct crypto_acomp_ctx *acomp_ctx; - unsigned int dlen = PAGE_SIZE; - u8 *src, *dst; - struct zpool *zpool; + u8 *dst; bool ret; VM_WARN_ON_ONCE(!folio_test_locked(folio)); @@ -1781,29 +1773,7 @@ bool zswap_load(struct folio *folio) goto stats; } - /* decompress */ - 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(acomp_ctx->dstmem, src, entry->length); - src = acomp_ctx->dstmem; - zpool_unmap_handle(zpool, entry->handle); - } - - sg_init_one(&input, src, entry->length); - sg_init_table(&output, 1); - sg_set_page(&output, page, PAGE_SIZE, 0); - acomp_request_set_params(acomp_ctx->req, &input, &output, entry->length, dlen); - if (crypto_wait_req(crypto_acomp_decompress(acomp_ctx->req), &acomp_ctx->wait)) - WARN_ON(1); - mutex_unlock(acomp_ctx->mutex); - - if (zpool_can_sleep_mapped(zpool)) - zpool_unmap_handle(zpool, entry->handle); - + __zswap_load(entry, page); ret = true; stats: count_vm_event(ZSWPIN);