From patchwork Thu Feb 6 07:20:58 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Sridhar, Kanchana P" X-Patchwork-Id: 13962463 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 DC5A1C02196 for ; Thu, 6 Feb 2025 07:21:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 98EAC28000C; Thu, 6 Feb 2025 02:21:18 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 917DE28000B; Thu, 6 Feb 2025 02:21:18 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 71A4328000C; Thu, 6 Feb 2025 02:21:18 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 4B98F280004 for ; Thu, 6 Feb 2025 02:21:18 -0500 (EST) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 0132BC0F4A for ; Thu, 6 Feb 2025 07:21:17 +0000 (UTC) X-FDA: 83088673836.15.3C85AB6 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.8]) by imf26.hostedemail.com (Postfix) with ESMTP id BE5DB140002 for ; Thu, 6 Feb 2025 07:21:14 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=jjuVXBHK; spf=pass (imf26.hostedemail.com: domain of kanchana.p.sridhar@intel.com designates 192.198.163.8 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=1738826475; 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=wJeRHi8PdIL+/6sqlqyoxEfcWSS7xsFU9sSDV6xUOP0=; b=4RWHR+0w2G5+IonDsQFHWR2XskJHqBrglcpsNQ0vt+m0vBlx34ZAioH1w1KmmIbLl/o/Tu U+EcC+ZEhS6NBe66NOWPTw8cG1iO6J3mou5FqQthHcwjA37y5bOjCkzonwyjqMJ5TiczRm 9+7S/H0wYi4NNcS/AC76KVp7QHmO2HM= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=jjuVXBHK; spf=pass (imf26.hostedemail.com: domain of kanchana.p.sridhar@intel.com designates 192.198.163.8 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=1738826475; a=rsa-sha256; cv=none; b=IjBsrZd04brn2vD4wxnp8Glueh90TCbbm81uwWyL5Fx+tUvCJ+YGpIWzYr9F+EFG2LUAX2 aytlei7eaxtQaoySJS1O7Y9Rb3wSZpridbZJcPqRcIiVJuNMm45LHtYXew23+A5xMqDIAK odZ7Iqn4B4WKSa6arQ4d9EJR/7lC0Fo= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1738826475; x=1770362475; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=jrbewW+SO0IZ8ImSpKIkSqygAyFsg0B5SijCQLy64nI=; b=jjuVXBHKYX37py3ChwTYcodTfVKdKsQxH/fRmeayhYy3O43SmVfsckRs ZJlxuNJjGbUZg1mcNP1in20motupZG/cWKrsyh2Za60lBRRSBLELfXMcH rmJirWypjvUg3JB3NsfQhxrXknvrobize0nA6rpUaMOjPBeOz1CQVPqFA 0XcIPw6MueVzAq/s6C5uKKHew74GghIXnc6Nn7aWLbdCxa8hdpCCqnUdj iHcQbOtGltVtKKQyX6kOHDCLeWXspuuCh2HQ/iBuY0C6T2UunCva8gb6A UxEm1xtuInj0T0zNKXKkRiULmJOyzkhKXKd6T/Im0aeobjY2d2p2JylYI w==; X-CSE-ConnectionGUID: Ss2H1e9VRLOUeXaINDFZnQ== X-CSE-MsgGUID: EiEtlBgqSX6Q4iJrRWxn1g== X-IronPort-AV: E=McAfee;i="6700,10204,11336"; a="56962705" X-IronPort-AV: E=Sophos;i="6.13,263,1732608000"; d="scan'208";a="56962705" Received: from orviesa008.jf.intel.com ([10.64.159.148]) by fmvoesa102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Feb 2025 23:21:05 -0800 X-CSE-ConnectionGUID: fxqLBazpQmmqNiuGMIhKtg== X-CSE-MsgGUID: veEW1kXWQMSAIAuvPJ7kCA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.12,224,1728975600"; d="scan'208";a="112022635" Received: from jf5300-b11a338t.jf.intel.com ([10.242.51.115]) by orviesa008.jf.intel.com with ESMTP; 05 Feb 2025 23:21:05 -0800 From: Kanchana P Sridhar To: linux-kernel@vger.kernel.org, linux-mm@kvack.org, hannes@cmpxchg.org, yosry.ahmed@linux.dev, 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 v6 12/16] mm: zswap: Allocate pool batching resources if the compressor supports batching. Date: Wed, 5 Feb 2025 23:20:58 -0800 Message-Id: <20250206072102.29045-13-kanchana.p.sridhar@intel.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20250206072102.29045-1-kanchana.p.sridhar@intel.com> References: <20250206072102.29045-1-kanchana.p.sridhar@intel.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: BE5DB140002 X-Stat-Signature: rocmjicd1zk7rukdkxj7z6mauat5c6da X-Rspamd-Server: rspam08 X-Rspam-User: X-HE-Tag: 1738826474-78659 X-HE-Meta: U2FsdGVkX1+GZm2jz2VeVA23nsnVnDfRXKh91C5s9yDXPngBVBIg3kF3R1GRs2STo56E0bFfs50Seoezw1SGGUEGQ+x04yLR1hijXtHYlG/pFvYPcrzf5y4ztR+J+u7f9HbXVxgHHr4pl3KupNBFpwDHfcpGj5Z3yOSWU1VyBSLy9cg0Xb8CbPfRgqcAPiHhHVCosIfpsTLocN1mgb4RM9ySfj7X1zRPa4HFCrb+12jigtXCReNAhZBba5IlOJW1hbe+0MCpYx/f+VH8HYvGe5KCpCTx/tjK8SuCo7UU5RY74GnJ/u1Ofvs1wUroBAJ2q54cLN7zgZ24puKCdutGtwxL4VX0JgL2tnnHzsRUaBiymIDkJepDRhN9kVLZsr3PsXYhIfANG97LpnHACpsRnIEEDNE03Q4nQfjQ4kuH4YXcIoJEUTxZWRmevRTl4hcrkO8G1RxvgJbydct9lkhFjTvn9Ak7p/PNwGNOL2YTi5Nsc2CfIoyWFrALQCdJQ0S4/4zy5rBKrkqINOdu23OUEnmUArXgm7Ys+CTSLQDYwD2a3FdSVIS0zLFR7BeuiRcD6eFbiIXVctrtXevAny/BWb3R52P6Xmaf8mTObBvSY0cfn/o1NQPXN5dZSTH4Gzmr3o/PYEm4+ACDLnjmY9bUGUzay0D4GDma0nC8G1ATa+Peeayqc67Q7E/JEtpwq4A6tJqFsFtXvEABwJe8Cnywzc/unBTwn2wF0wC4GdEic6Wl1CEaEflf9I1IwfUXYE3G2x+WeHO08fVO1e1q8o+6wEXQPXsdJHacL2X+hIRG7p8HQtKbs3TMw9SHutHGkeHfeZVcds5y1Qryr8rAjg4higqvOsD5/3EPYjs6KxoSOQVD4NwOgkYsPsf9xvOgAA1KZh05k2ZYSzSWmpcYp9TG8uYTH1d/OiY1Je1PubzWk1xNORHFLLOaCW5j0P/pAgz1kPGsn4bhKnargcudsFZ eC9klOZz GjGFv1PC0k7O8lpxQ+IZa1jtRdQbFfT5H6B3ifT+5yuaZJrM6yw9V938gTIKxRChofpWLPLCWlAGn+SRf6RLvuIZ7Yxa3GityjZzHGnWPFKZRvB8rLlwfdquvFnoIXv/QSoG4S3w5W19RTG9+WWvNv1CfWG00WGJMAqOidZq3RW5Vvgw9RSFDhQUPnMBvf13KJ/Vh13kLowCtGR/1ltLjsvC/yw== 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 adds support for the per-CPU acomp_ctx to track multiple compression/decompression requests. The zswap_cpu_comp_prepare() cpu onlining code will check if the compressor supports batching. If so, it will allocate the necessary batching resources. However, zswap does not use more than one request yet. Follow-up patches will actually utilize the multiple acomp_ctx requests/buffers for batch compression/decompression of multiple pages. The newly added ZSWAP_MAX_BATCH_SIZE limits the amount of extra memory used for batching. There is no extra memory usage for compressors that do not support batching. Signed-off-by: Kanchana P Sridhar --- mm/zswap.c | 132 +++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 98 insertions(+), 34 deletions(-) diff --git a/mm/zswap.c b/mm/zswap.c index a2baceed3bf9..dc7d1ff04b22 100644 --- a/mm/zswap.c +++ b/mm/zswap.c @@ -78,6 +78,16 @@ 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. + * This limit exists because we preallocate enough requests and buffers + * in the per-cpu acomp_ctx accordingly. Hence, a higher limit means higher + * memory usage. + */ +#define ZSWAP_MAX_BATCH_SIZE 8U + static int zswap_setup(void); /* Enable/disable zswap */ @@ -143,9 +153,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; }; @@ -821,15 +832,13 @@ 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 = NULL; - struct acomp_req *req = NULL; - u8 *buffer = NULL; - int ret; + unsigned int nr_reqs = 1; + int ret = -ENOMEM; + int i; - buffer = kmalloc_node(PAGE_SIZE * 2, GFP_KERNEL, cpu_to_node(cpu)); - if (!buffer) { - ret = -ENOMEM; - goto fail; - } + acomp_ctx->buffers = NULL; + acomp_ctx->reqs = NULL; + acomp_ctx->nr_reqs = 0; acomp = crypto_alloc_acomp_node(pool->tfm_name, 0, 0, cpu_to_node(cpu)); if (IS_ERR(acomp)) { @@ -839,12 +848,30 @@ static int zswap_cpu_comp_prepare(unsigned int cpu, struct hlist_node *node) goto fail; } - req = acomp_request_alloc(acomp); - if (!req) { - pr_err("could not alloc crypto acomp_request %s\n", - pool->tfm_name); - ret = -ENOMEM; + if (acomp_has_async_batching(acomp)) + nr_reqs = min(ZSWAP_MAX_BATCH_SIZE, crypto_acomp_batch_size(acomp)); + + acomp_ctx->buffers = kcalloc_node(nr_reqs, sizeof(u8 *), GFP_KERNEL, cpu_to_node(cpu)); + if (!acomp_ctx->buffers) + goto 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]) + goto fail; + } + + acomp_ctx->reqs = kcalloc_node(nr_reqs, sizeof(struct acomp_req *), GFP_KERNEL, cpu_to_node(cpu)); + if (!acomp_ctx->reqs) goto fail; + + for (i = 0; i < nr_reqs; ++i) { + acomp_ctx->reqs[i] = acomp_request_alloc(acomp); + if (!acomp_ctx->reqs[i]) { + pr_err("could not alloc crypto acomp_request reqs[%d] %s\n", + i, pool->tfm_name); + goto fail; + } } /* @@ -853,6 +880,13 @@ static int zswap_cpu_comp_prepare(unsigned int cpu, struct hlist_node *node) * again resulting in a deadlock. */ mutex_lock(&acomp_ctx->mutex); + + /* + * 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); /* @@ -860,20 +894,33 @@ static int zswap_cpu_comp_prepare(unsigned int cpu, struct hlist_node *node) * 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->buffer = buffer; + acomp_ctx->nr_reqs = nr_reqs; acomp_ctx->acomp = acomp; acomp_ctx->is_sleepable = acomp_is_async(acomp); - acomp_ctx->req = req; mutex_unlock(&acomp_ctx->mutex); return 0; fail: + if (acomp_ctx->buffers) { + for (i = 0; i < nr_reqs; ++i) + kfree(acomp_ctx->buffers[i]); + kfree(acomp_ctx->buffers); + acomp_ctx->buffers = NULL; + } + + if (acomp_ctx->reqs) { + for (i = 0; i < nr_reqs; ++i) + if (!IS_ERR_OR_NULL(acomp_ctx->reqs[i])) + acomp_request_free(acomp_ctx->reqs[i]); + kfree(acomp_ctx->reqs); + acomp_ctx->reqs = NULL; + } + if (acomp) crypto_free_acomp(acomp); - kfree(buffer); return ret; } @@ -883,14 +930,31 @@ 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); mutex_lock(&acomp_ctx->mutex); + if (!IS_ERR_OR_NULL(acomp_ctx)) { - if (!IS_ERR_OR_NULL(acomp_ctx->req)) - acomp_request_free(acomp_ctx->req); - acomp_ctx->req = NULL; + int i; + + if (acomp_ctx->reqs) { + 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); + acomp_ctx->reqs = NULL; + } + + if (acomp_ctx->buffers) { + for (i = 0; i < acomp_ctx->nr_reqs; ++i) + kfree(acomp_ctx->buffers[i]); + kfree(acomp_ctx->buffers); + acomp_ctx->buffers = NULL; + } + if (!IS_ERR_OR_NULL(acomp_ctx->acomp)) crypto_free_acomp(acomp_ctx->acomp); - kfree(acomp_ctx->buffer); + + acomp_ctx->nr_reqs = 0; } + mutex_unlock(&acomp_ctx->mutex); return 0; @@ -903,7 +967,7 @@ static struct crypto_acomp_ctx *acomp_ctx_get_cpu_lock(struct zswap_pool *pool) for (;;) { acomp_ctx = raw_cpu_ptr(pool->acomp_ctx); mutex_lock(&acomp_ctx->mutex); - if (likely(acomp_ctx->req)) + if (likely(acomp_ctx->reqs)) return acomp_ctx; /* * It is possible that we were migrated to a different CPU after @@ -935,7 +999,7 @@ static bool zswap_compress(struct page *page, struct zswap_entry *entry, u8 *dst; acomp_ctx = acomp_ctx_get_cpu_lock(pool); - dst = acomp_ctx->buffer; + dst = acomp_ctx->buffers[0]; sg_init_table(&input, 1); sg_set_page(&input, page, PAGE_SIZE, 0); @@ -945,7 +1009,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, @@ -959,8 +1023,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; @@ -1011,19 +1075,19 @@ 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); - if (src != acomp_ctx->buffer) + if (src != acomp_ctx->buffers[0]) zpool_unmap_handle(zpool, entry->handle); acomp_ctx_put_unlock(acomp_ctx); }