From patchwork Wed Nov 6 19:21:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Sridhar, Kanchana P" X-Patchwork-Id: 13865324 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 BF591D59F6B for ; Wed, 6 Nov 2024 19:21:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4B9066B009B; Wed, 6 Nov 2024 14:21:19 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 46A416B009C; Wed, 6 Nov 2024 14:21:19 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2BAF46B009D; Wed, 6 Nov 2024 14:21:19 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 058F06B009A for ; Wed, 6 Nov 2024 14:21:18 -0500 (EST) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id AC172121D60 for ; Wed, 6 Nov 2024 19:21:18 +0000 (UTC) X-FDA: 82756637334.25.DD3A023 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.11]) by imf18.hostedemail.com (Postfix) with ESMTP id 243091C0010 for ; Wed, 6 Nov 2024 19:21:00 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=A2CMKXob; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf18.hostedemail.com: domain of kanchana.p.sridhar@intel.com designates 198.175.65.11 as permitted sender) smtp.mailfrom=kanchana.p.sridhar@intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1730920792; 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=aYdW0zq6nuiuC3om5oGcxQH/8ZdFh38GqlmuO3OCSvQ=; b=dk3ssuQLTaHjx6G1lrWSGWMndTa2cBgEXtHUi+9fRWw3bjeqYCKNq+iizSMFAZA6JkHY92 5xn7mRHAG2f5M4WH18rzb0LFYNVS5VcNF8jwbVmzDHOLEJ6I6yP7VTC25u2qfYv79DGjYM yK3mD9CWUvxZp9IexzK/CwO1eXToSSQ= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=A2CMKXob; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf18.hostedemail.com: domain of kanchana.p.sridhar@intel.com designates 198.175.65.11 as permitted sender) smtp.mailfrom=kanchana.p.sridhar@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1730920792; a=rsa-sha256; cv=none; b=X+hM024D8oSM8zM7Q0w3GgTnx6wqFFIBs6MwIFLKIsVWmKASqT3OLIDZYd0NofvZMO9diw s3uykt7P5juxbWYo4MaD2Nkr6Zyle4r6I7QkWQ0PowuD4LnoJ/uE4en0bVEimmBfZlbfG1 c5L4ebM225CiMncQPKNX1TPFJmjrrLE= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1730920877; x=1762456877; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=r/PiZ6L9gIKB3NWj5x6kNiz1Cz0bRXxaZZCRHikpYT4=; b=A2CMKXobzn98lTpPpLJ/pnmIsH8rGzC/N8Py3XXDdlxP45tETgSFDPX2 bB6bxmO/htZ1lGZqfrc+1+n+N1Vij1ziYUSnwwqjh0TUAh4ZDECPkAQpF GMFRU6BXZN6DhV9kQmQj06foQj+j+CbPly+ldArKaFUfJIsspE/jMM8ve zK2Jh5VYB30CoRmaOJE/ejcGaIM4E1oOvxXXdxKTP2+fZ2JILOlw1I5fD zPBPqJVF2/hBIKj5lNGRTc3zFzSnPj753WjYGwpRMpbkJ5K1TOMlPmxbr DzViTbqj/Yb2Qo1eEDQAwIGzlyC8kG8fA67G1N6X1D+cHkIU0E0LI8SYW g==; X-CSE-ConnectionGUID: 1DKdQZo1QZmM0cpXw0q2SA== X-CSE-MsgGUID: D/L2vyjXSLqqUNEKt43gJg== X-IronPort-AV: E=McAfee;i="6700,10204,11222"; a="41292037" X-IronPort-AV: E=Sophos;i="6.11,199,1725346800"; d="scan'208";a="41292037" Received: from fmviesa003.fm.intel.com ([10.60.135.143]) by orvoesa103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 06 Nov 2024 11:21:12 -0800 X-CSE-ConnectionGUID: dprjYx6MRXCZ/IHxNSx7pA== X-CSE-MsgGUID: QW0E949yR+eziFEtkGDYIA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,263,1725346800"; d="scan'208";a="88695405" Received: from unknown (HELO JF5300-B11A338T.jf.intel.com) ([10.242.51.115]) by fmviesa003.fm.intel.com with ESMTP; 06 Nov 2024 11:21:11 -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, ying.huang@intel.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, zanussi@kernel.org Cc: wajdi.k.feghali@intel.com, vinodh.gopal@intel.com, kanchana.p.sridhar@intel.com Subject: [PATCH v3 09/13] mm: zswap: Modify struct crypto_acomp_ctx to be configurable in nr of acomp_reqs. Date: Wed, 6 Nov 2024 11:21:01 -0800 Message-Id: <20241106192105.6731-10-kanchana.p.sridhar@intel.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20241106192105.6731-1-kanchana.p.sridhar@intel.com> References: <20241106192105.6731-1-kanchana.p.sridhar@intel.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 243091C0010 X-Stat-Signature: pf6gudikirgtgfoe6wcg6dpj4zkzhdoe X-HE-Tag: 1730920860-279956 X-HE-Meta: U2FsdGVkX18PCv3S6WabM/8ZRWLXp+DM6XlH3QAsv0NVtH4IUvOAbbX0v4gHTo/VbqOXoOVY1U1/CILpBYfdxxK4tWhPEzeMVkuKwO0avbW/+Rc3XS81JqeiVNG2dtQmLh4L+a8ZHZbS1o86kqSNuBGLxez1zSEcb20RO4Ot/j14QIxf0JCFDJjlb/d0X825HD1UEYeQ0Zdm1K1/Wz3+zfZPD/BcPQkGtUIxFce8efl8ipMJXHhJQXubcueSmwgdMxtf5M5b3bx3HyDyQDYQIBgDmoWnZNhKZd4HwHQ8axkHCr5E8kDZIz/oC3pbpLAGkJvv0WblZXEcFdF7g356KNLxoUM/KjDAnKNDKGsTmFT90gBulpffA1jTApMcQavVdSABxQsBxjvArUcn/OgMzcdX0Do7EGlFYT50FA5KVAT2ijeXnwmv9UmsmmHdxnCAR5BrINKyLwA0oB9kXA1+BkEF6mabnnlR1TVSVQCOLJbdbzaNDa2fDUDxmfBLnin93oY7Ja4YtQXQEwSm7DlZ7oE6vd7o2Bz8VMRqrz0dbf9F12QRAkLnDpxtmHdlUM3rqxgPM58o+TY74LyLqoJTbNsgzsb2IKLIEK6+Te3O1XhH3zTVwkPd0TnDSUjPE/bWcoSSApVrArpPBW3DyNKu8LwNwwRIZZOECSqFe8ojRknQLsTiDqkjo1pe5hiziI/Smc5HGn9DKlyW8kTUbVy68ImnhHaDM/R0WkFvBco4osSSgPtSDVupe+oQrI480inFwM5x6JJLNDjvV7J/7T8V23gfA0rqfd3qFcJnyuS4ZCA2hvtyIvj20Aa7aLS+xhv+bGhqNgKPgPv/Jzta8YFGAUfmW195ktpcxiI/qSTE01BEHprJUGJJL6IacQVnTjmL4gwEb/jw766e7rydvKNLeelrQuloCuvS7BsPBk+0Rb+wt4RmCyfn1oAOCPxaA0HSiU8xd34CAjtVVnkol0T g316XfUp ORSbEjh/YY4d4PebRQ2VAz1cieJQPECWY+wfudVFpCjSeRoBr+wUrNXQ9Byrk4pFouZqODDoG6SQDDKgXuaO2CXqPoVfIjqVhMI6CAlPwYDIKRJP9Epq2NrFo5BsTOgyrTHnJnWiUrME/vfhJfaKJn5UvKOgkGTKrEPOF+Vp6pE36/NFASxY1M6Sc4vSMseLcC/tR6rgazifcc8gbVagmCSdRjWS/TA80TGg75eRVQ4rrJp8= 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: Modified the definition of "struct crypto_acomp_ctx" to represent a configurable number of acomp_reqs and the required number of buffers. Accordingly, refactored the code that allocates/deallocates the acomp_ctx resources, so that it can be called to create a regular acomp_ctx with exactly one acomp_req/buffer, for use in the the existing non-batching zswap_store(), as well as to create a separate "batching acomp_ctx" with multiple acomp_reqs/buffers for IAA compress batching. Signed-off-by: Kanchana P Sridhar --- mm/zswap.c | 149 ++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 107 insertions(+), 42 deletions(-) diff --git a/mm/zswap.c b/mm/zswap.c index 3e899fa61445..02e031122fdf 100644 --- a/mm/zswap.c +++ b/mm/zswap.c @@ -143,9 +143,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; }; @@ -241,6 +242,11 @@ static inline struct xarray *swap_zswap_tree(swp_entry_t swp) pr_debug("%s pool %s/%s\n", msg, (p)->tfm_name, \ zpool_get_type((p)->zpool)) +static int zswap_create_acomp_ctx(unsigned int cpu, + struct crypto_acomp_ctx *acomp_ctx, + char *tfm_name, + unsigned int nr_reqs); + /********************************* * pool functions **********************************/ @@ -813,69 +819,128 @@ static void zswap_entry_free(struct zswap_entry *entry) /********************************* * compressed storage functions **********************************/ -static int zswap_cpu_comp_prepare(unsigned int cpu, struct hlist_node *node) +static int zswap_create_acomp_ctx(unsigned int cpu, + struct crypto_acomp_ctx *acomp_ctx, + char *tfm_name, + unsigned int nr_reqs) { - 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; + int ret = -ENOMEM; + int i, j; + acomp_ctx->nr_reqs = 0; 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 = crypto_alloc_acomp_node(pool->tfm_name, 0, 0, cpu_to_node(cpu)); + acomp = crypto_alloc_acomp_node(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; + tfm_name, PTR_ERR(acomp)); + 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; + 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, 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; } -static int zswap_cpu_comp_dead(unsigned int cpu, struct hlist_node *node) +static void zswap_delete_acomp_ctx(struct crypto_acomp_ctx *acomp_ctx) { - struct zswap_pool *pool = hlist_entry(node, struct zswap_pool, 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; } +} + +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; + + acomp_ctx = per_cpu_ptr(pool->acomp_ctx, cpu); + return zswap_create_acomp_ctx(cpu, acomp_ctx, pool->tfm_name, 1); +} + +static int zswap_cpu_comp_dead(unsigned int cpu, struct hlist_node *node) +{ + struct zswap_pool *pool = hlist_entry(node, struct zswap_pool, node); + struct crypto_acomp_ctx *acomp_ctx; + + acomp_ctx = per_cpu_ptr(pool->acomp_ctx, cpu); + zswap_delete_acomp_ctx(acomp_ctx); return 0; } @@ -898,7 +963,7 @@ static bool zswap_compress(struct page *page, struct zswap_entry *entry, acomp_ctx = raw_cpu_ptr(pool->acomp_ctx); - dst = acomp_ctx->buffer; + dst = acomp_ctx->buffers[0]; sg_init_table(&input, 1); sg_set_page(&input, page, PAGE_SIZE, 0); @@ -908,7 +973,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, @@ -922,8 +987,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,24 +1040,24 @@ 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); /* * It is safer to unlock the mutex after the check for - * "src != acomp_ctx->buffer" so that the value of "src" + * "src != acomp_ctx->buffers[0]" so that the value of "src" * does not change. */ mutex_unlock(&acomp_ctx->mutex);