From patchwork Sat Dec 21 06:31:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kanchana P Sridhar X-Patchwork-Id: 13917688 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 3C54EE7718B for ; Sat, 21 Dec 2024 06:32:13 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B0C136B009D; Sat, 21 Dec 2024 01:31:33 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id ABC1D6B009E; Sat, 21 Dec 2024 01:31:33 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 90DD56B009F; Sat, 21 Dec 2024 01:31:33 -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 72C376B009D for ; Sat, 21 Dec 2024 01:31:33 -0500 (EST) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id E9A8BC0C11 for ; Sat, 21 Dec 2024 06:31:32 +0000 (UTC) X-FDA: 82917993816.24.BCEAB20 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.20]) by imf11.hostedemail.com (Postfix) with ESMTP id 8A5BC40002 for ; Sat, 21 Dec 2024 06:30:58 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=cycBWoIf; spf=pass (imf11.hostedemail.com: domain of kanchana.p.sridhar@intel.com designates 198.175.65.20 as permitted sender) smtp.mailfrom=kanchana.p.sridhar@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1734762654; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=aKcsXHV7RLAIliO2e3Akr8LalDlWIbFgTE+WHmIxoiE=; b=oqB9jh65Wr32eh2JwvMgcO01gof9ObRV0W8S89fiq5MbGHWY1wUBUko0mhXHP5r+0Z0Bxp D2s8ZWj/H3UnBUEr73wD9b6gwvDK5I24Tle+PVWuaPHhCEEj42cdaRgrC5ky1W8fbCaiu8 9eFV9zsDT3QJkBl6/dWLKeszXIl09KQ= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=cycBWoIf; spf=pass (imf11.hostedemail.com: domain of kanchana.p.sridhar@intel.com designates 198.175.65.20 as permitted sender) smtp.mailfrom=kanchana.p.sridhar@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1734762654; a=rsa-sha256; cv=none; b=MXMMwrspp5CVCwpkKAY3Kudr7IwavatowXlBqBeLmFZJQlxU/Iyqi8C+Eu8NbaW11H9pvU CtgXLqyjiVzwH1rVZfUpZYMgSdXYgBuaE6wOrw7MDZmlMFvpB9BkLBxL23qX+wVtUJOC9A qPUN8Ibh3YQC1d9/3pX0ESlmsmWxylQ= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1734762691; x=1766298691; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=KPv/lIOr9UJuu1TDwAGjapkWxba0uS6X9GnVkLeTgfc=; b=cycBWoIf0+hEGXQULvjKeDK4mD+yISi3+5DA3bvvsCN63sI72calG9q9 DdYE/nkndDI0TPKoc0tEi+Ernwx3OSREr/QIo6Q0CMxxeqj2wCTQllNnf 3mcuN5iHm4zC3W31j9q84UDKCq8LOrXYKuewF7sV9MJGU96O22izOOsQQ XLjp7OUL7x8HWQaRVihgaqRM9+CNkk7MXU9EtWrkgs3DJ3gow4TIkFBP1 gb6XzAKw03uuKuu+tLEDR5UgIyWCS2ADHJx51qOkGPqxQ6BkPy8gs4fT4 8ZC+Sf1YPdAIy3XUqMEh+vk8JM6S/oFiPva5/MOv3JGQaor6km7gv5uie A==; X-CSE-ConnectionGUID: r6j2wmRWShGndOiVXsQgYg== X-CSE-MsgGUID: de2QPsF9RaaaR3naz81PAQ== X-IronPort-AV: E=McAfee;i="6700,10204,11292"; a="35021722" X-IronPort-AV: E=Sophos;i="6.12,253,1728975600"; d="scan'208";a="35021722" Received: from orviesa007.jf.intel.com ([10.64.159.147]) by orvoesa112.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 20 Dec 2024 22:31:21 -0800 X-CSE-ConnectionGUID: 7G/EcktGThmli+QGpElkYQ== X-CSE-MsgGUID: M7yZhvoTT6i5+xSQGaVIsA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.12,224,1728975600"; d="scan'208";a="99184607" Received: from jf5300-b11a338t.jf.intel.com ([10.242.51.115]) by orviesa007.jf.intel.com with ESMTP; 20 Dec 2024 22:31:21 -0800 From: Kanchana P Sridhar To: linux-kernel@vger.kernel.org, linux-mm@kvack.org, hannes@cmpxchg.org, yosryahmed@google.com, nphamcs@gmail.com, chengming.zhou@linux.dev, usamaarif642@gmail.com, ryan.roberts@arm.com, 21cnbao@gmail.com, akpm@linux-foundation.org, linux-crypto@vger.kernel.org, herbert@gondor.apana.org.au, davem@davemloft.net, clabbe@baylibre.com, ardb@kernel.org, ebiggers@google.com, surenb@google.com, kristen.c.accardi@intel.com Cc: wajdi.k.feghali@intel.com, vinodh.gopal@intel.com, kanchana.p.sridhar@intel.com Subject: [PATCH v5 10/12] mm: zswap: Allocate pool batching resources if the crypto_alg supports batching. Date: Fri, 20 Dec 2024 22:31:17 -0800 Message-Id: <20241221063119.29140-11-kanchana.p.sridhar@intel.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20241221063119.29140-1-kanchana.p.sridhar@intel.com> References: <20241221063119.29140-1-kanchana.p.sridhar@intel.com> MIME-Version: 1.0 X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 8A5BC40002 X-Rspam-User: X-Stat-Signature: e56kdi1sdhzudk5r31ebdwj4sqa8ax6z X-HE-Tag: 1734762658-324740 X-HE-Meta: U2FsdGVkX1+1n1WkkwR5OHAbE5bDoVKHFKtLy278mZy818FkoYMWp7Atq+ldacilf22JdIQsX47928WR5ZJZ3y6zgsirzc0meek/9ZEQl7kcyh8q4wltcxfqFJgdluefKcaafdopV/Xxhc1TYgSOxVxAxyyAiu2N+u2OuyNwF8SGua8XxBGaIxzUkpLQJNcfVt3Y1OaVBn99Vu4r8ogM6lLpScEIS5nFUDxKAfvMaU57sVrPErHnYFVf14oKRKUhLDHVcli4mGyVTcz9YK+k1+oHfh1ojG+n3JXazTwfhjScyw8dUJ8F0kdzUZIzrfn32hw4pG2E48iCKuwqS0GEUvPD9bMUVYdIiapUAU3SI1Lft/FwZBESk9GsAMwfi5WN3s7qJn992hswm32YJBXpQKHprBeVXsAh36uEDuaSjLTgidpt9/BIUeG8EAhHtAGx8b4SdOsmLmF9VttRZTY+/VxZlGCJLyfkk59NQngTern49nyEgwJAT8A02oOrU9n0NG6gR3WGxOC8Fzqy7uVPCULwAuiL8tS/xa41E7PaIJkdcbfouNguRvWt3z1OQ1+J4Wx7EWo6EeB2AeO6XJyrmkRaAsTozARyRQ7Iwceb2PUmaS0obIpSkF/B8/9UrmZyoQQh237TCmiBaskT4LXDntcrF6p0KViZzsuK+QPYj3i9fI+7fVyc8hvnTxY4T+HA0iyJww2o5/rw+B9sJq94LWvPin2iqlOj8BWgPg2PK0xDjYUhJeZ8892gf8ShuogybrVsJfz96WhYAppvgeoRYknBQJeCazajvMFIBWuJwuy+kkBBVVFOG3GrTVLtjyrU6Tf/ch2OPvWlAceTKu1tHutp1DMt8ckLStW+xT7uvpbB3x6ZW+vh0B41sNwEbTfVvyEqkU3E2eru2Bo7Z1GOUOt5EcwDBsg3RLyJ8kPwc+l6byd0g+y/RweMCldJmiQjPBR2aOCaD55TiP4MNnO Pw0cKsRx W3uG/M88b4i3zVVz4CuCd0dVLKtrWsFI7VwcbWgysilWHXO1e2yJjaJ9Reh+ThMdkQR3E1SpMRPWstdBI95OtwT2M9jaMEEXFR0HEuYbhjf9BSplpKF7iIDlFPUZO7bBGDPeu4EmGj2fjZDgPgElfb8bzezhocP3EiMHQZDG0SggFqUuqRbj7QI35rPHyW4QnUN89nHXRsowIZehYU29KXrc1M7T4Ir6RkRNvXq31WFrI2HwUAmm7OZFLJiGcmFWnlWCDzdnWOqhigXA= 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: This patch does the following: 1) Defines ZSWAP_MAX_BATCH_SIZE to denote the maximum number of acomp_ctx batching resources (acomp_reqs and buffers) to allocate if the zswap compressor supports batching. Currently, ZSWAP_MAX_BATCH_SIZE is set to 8U. 2) Modifies the definition of "struct crypto_acomp_ctx" to represent a configurable number of acomp_reqs and buffers. Adds a "nr_reqs" to "struct crypto_acomp_ctx" to contain the number of resources that will be allocated in the cpu hotplug onlining code. 3) The zswap_cpu_comp_prepare() cpu onlining code will detect if the crypto_acomp created for the zswap pool (in other words, the zswap compression algorithm) has registered implementations for batch_compress() and batch_decompress(). If so, it will query the crypto_acomp for the maximum batch size supported by the compressor, and set "nr_reqs" to the minimum of this compressor-specific max batch size and ZSWAP_MAX_BATCH_SIZE. Finally, it will allocate "nr_reqs" reqs/buffers, and set the acomp_ctx->nr_reqs accordingly. 4) If the crypto_acomp does not support batching, "nr_reqs" defaults to 1. Signed-off-by: Kanchana P Sridhar --- mm/zswap.c | 122 +++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 90 insertions(+), 32 deletions(-) diff --git a/mm/zswap.c b/mm/zswap.c index 9718c33f8192..99cd78891fd0 100644 --- a/mm/zswap.c +++ b/mm/zswap.c @@ -78,6 +78,13 @@ static bool zswap_pool_reached_full; #define ZSWAP_PARAM_UNSET "" +/* + * For compression batching of large folios: + * Maximum number of acomp compress requests that will be processed + * in a batch, iff the zswap compressor supports batching. + */ +#define ZSWAP_MAX_BATCH_SIZE 8U + static int zswap_setup(void); /* Enable/disable zswap */ @@ -143,9 +150,10 @@ bool zswap_never_enabled(void) struct crypto_acomp_ctx { struct crypto_acomp *acomp; - struct acomp_req *req; + struct acomp_req **reqs; + u8 **buffers; + unsigned int nr_reqs; struct crypto_wait wait; - u8 *buffer; struct mutex mutex; bool is_sleepable; }; @@ -818,49 +826,88 @@ static int zswap_cpu_comp_prepare(unsigned int cpu, struct hlist_node *node) struct zswap_pool *pool = hlist_entry(node, struct zswap_pool, node); struct crypto_acomp_ctx *acomp_ctx = per_cpu_ptr(pool->acomp_ctx, cpu); struct crypto_acomp *acomp; - struct acomp_req *req; - int ret; + unsigned int nr_reqs = 1; + int ret = -ENOMEM; + int i, j; mutex_init(&acomp_ctx->mutex); - - acomp_ctx->buffer = kmalloc_node(PAGE_SIZE * 2, GFP_KERNEL, cpu_to_node(cpu)); - if (!acomp_ctx->buffer) - return -ENOMEM; + acomp_ctx->nr_reqs = 0; acomp = crypto_alloc_acomp_node(pool->tfm_name, 0, 0, cpu_to_node(cpu)); if (IS_ERR(acomp)) { pr_err("could not alloc crypto acomp %s : %ld\n", pool->tfm_name, PTR_ERR(acomp)); - ret = PTR_ERR(acomp); - goto acomp_fail; + return PTR_ERR(acomp); } acomp_ctx->acomp = acomp; acomp_ctx->is_sleepable = acomp_is_async(acomp); - req = acomp_request_alloc(acomp_ctx->acomp); - if (!req) { - pr_err("could not alloc crypto acomp_request %s\n", - pool->tfm_name); - ret = -ENOMEM; + /* + * Create the necessary batching resources if the crypto acomp alg + * implements the batch_compress and batch_decompress API. + */ + if (acomp_has_async_batching(acomp)) { + nr_reqs = min(ZSWAP_MAX_BATCH_SIZE, crypto_acomp_batch_size(acomp)); + pr_info_once("Creating acomp_ctx with %d reqs/buffers for batching since crypto acomp\n%s has registered batch_compress() and batch_decompress().\n", + nr_reqs, pool->tfm_name); + } + + acomp_ctx->buffers = kmalloc_node(nr_reqs * sizeof(u8 *), GFP_KERNEL, cpu_to_node(cpu)); + if (!acomp_ctx->buffers) + goto buf_fail; + + for (i = 0; i < nr_reqs; ++i) { + acomp_ctx->buffers[i] = kmalloc_node(PAGE_SIZE * 2, GFP_KERNEL, cpu_to_node(cpu)); + if (!acomp_ctx->buffers[i]) { + for (j = 0; j < i; ++j) + kfree(acomp_ctx->buffers[j]); + kfree(acomp_ctx->buffers); + ret = -ENOMEM; + goto buf_fail; + } + } + + acomp_ctx->reqs = kmalloc_node(nr_reqs * sizeof(struct acomp_req *), GFP_KERNEL, cpu_to_node(cpu)); + if (!acomp_ctx->reqs) goto req_fail; + + for (i = 0; i < nr_reqs; ++i) { + acomp_ctx->reqs[i] = acomp_request_alloc(acomp_ctx->acomp); + if (!acomp_ctx->reqs[i]) { + pr_err("could not alloc crypto acomp_request reqs[%d] %s\n", + i, pool->tfm_name); + for (j = 0; j < i; ++j) + acomp_request_free(acomp_ctx->reqs[j]); + kfree(acomp_ctx->reqs); + ret = -ENOMEM; + goto req_fail; + } } - acomp_ctx->req = req; + /* + * The crypto_wait is used only in fully synchronous, i.e., with scomp + * or non-poll mode of acomp, hence there is only one "wait" per + * acomp_ctx, with callback set to reqs[0], under the assumption that + * there is at least 1 request per acomp_ctx. + */ crypto_init_wait(&acomp_ctx->wait); /* * if the backend of acomp is async zip, crypto_req_done() will wakeup * crypto_wait_req(); if the backend of acomp is scomp, the callback * won't be called, crypto_wait_req() will return without blocking. */ - acomp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG, + acomp_request_set_callback(acomp_ctx->reqs[0], CRYPTO_TFM_REQ_MAY_BACKLOG, crypto_req_done, &acomp_ctx->wait); + acomp_ctx->nr_reqs = nr_reqs; return 0; req_fail: + for (i = 0; i < nr_reqs; ++i) + kfree(acomp_ctx->buffers[i]); + kfree(acomp_ctx->buffers); +buf_fail: crypto_free_acomp(acomp_ctx->acomp); -acomp_fail: - kfree(acomp_ctx->buffer); return ret; } @@ -870,11 +917,22 @@ static int zswap_cpu_comp_dead(unsigned int cpu, struct hlist_node *node) struct crypto_acomp_ctx *acomp_ctx = per_cpu_ptr(pool->acomp_ctx, cpu); if (!IS_ERR_OR_NULL(acomp_ctx)) { - if (!IS_ERR_OR_NULL(acomp_ctx->req)) - acomp_request_free(acomp_ctx->req); + int i; + + for (i = 0; i < acomp_ctx->nr_reqs; ++i) + if (!IS_ERR_OR_NULL(acomp_ctx->reqs[i])) + acomp_request_free(acomp_ctx->reqs[i]); + kfree(acomp_ctx->reqs); + + for (i = 0; i < acomp_ctx->nr_reqs; ++i) + kfree(acomp_ctx->buffers[i]); + kfree(acomp_ctx->buffers); + if (!IS_ERR_OR_NULL(acomp_ctx->acomp)) crypto_free_acomp(acomp_ctx->acomp); - kfree(acomp_ctx->buffer); + + acomp_ctx->nr_reqs = 0; + acomp_ctx = NULL; } return 0; @@ -897,7 +955,7 @@ static bool zswap_compress(struct page *page, struct zswap_entry *entry, mutex_lock(&acomp_ctx->mutex); - dst = acomp_ctx->buffer; + dst = acomp_ctx->buffers[0]; sg_init_table(&input, 1); sg_set_page(&input, page, PAGE_SIZE, 0); @@ -907,7 +965,7 @@ static bool zswap_compress(struct page *page, struct zswap_entry *entry, * giving the dst buffer with enough length to avoid buffer overflow. */ sg_init_one(&output, dst, PAGE_SIZE * 2); - acomp_request_set_params(acomp_ctx->req, &input, &output, PAGE_SIZE, dlen); + acomp_request_set_params(acomp_ctx->reqs[0], &input, &output, PAGE_SIZE, dlen); /* * it maybe looks a little bit silly that we send an asynchronous request, @@ -921,8 +979,8 @@ static bool zswap_compress(struct page *page, struct zswap_entry *entry, * but in different threads running on different cpu, we have different * acomp instance, so multiple threads can do (de)compression in parallel. */ - comp_ret = crypto_wait_req(crypto_acomp_compress(acomp_ctx->req), &acomp_ctx->wait); - dlen = acomp_ctx->req->dlen; + comp_ret = crypto_wait_req(crypto_acomp_compress(acomp_ctx->reqs[0]), &acomp_ctx->wait); + dlen = acomp_ctx->reqs[0]->dlen; if (comp_ret) goto unlock; @@ -975,20 +1033,20 @@ static void zswap_decompress(struct zswap_entry *entry, struct folio *folio) */ if ((acomp_ctx->is_sleepable && !zpool_can_sleep_mapped(zpool)) || !virt_addr_valid(src)) { - memcpy(acomp_ctx->buffer, src, entry->length); - src = acomp_ctx->buffer; + memcpy(acomp_ctx->buffers[0], src, entry->length); + src = acomp_ctx->buffers[0]; zpool_unmap_handle(zpool, entry->handle); } sg_init_one(&input, src, entry->length); sg_init_table(&output, 1); sg_set_folio(&output, folio, PAGE_SIZE, 0); - acomp_request_set_params(acomp_ctx->req, &input, &output, entry->length, PAGE_SIZE); - BUG_ON(crypto_wait_req(crypto_acomp_decompress(acomp_ctx->req), &acomp_ctx->wait)); - BUG_ON(acomp_ctx->req->dlen != PAGE_SIZE); + acomp_request_set_params(acomp_ctx->reqs[0], &input, &output, entry->length, PAGE_SIZE); + BUG_ON(crypto_wait_req(crypto_acomp_decompress(acomp_ctx->reqs[0]), &acomp_ctx->wait)); + BUG_ON(acomp_ctx->reqs[0]->dlen != PAGE_SIZE); mutex_unlock(&acomp_ctx->mutex); - if (src != acomp_ctx->buffer) + if (src != acomp_ctx->buffers[0]) zpool_unmap_handle(zpool, entry->handle); }