From patchwork Fri Feb 28 10:00:10 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: 13996091 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 D22B8C282C5 for ; Fri, 28 Feb 2025 10:00:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E7EDB280005; Fri, 28 Feb 2025 05:00:30 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id DDD2D6B0085; Fri, 28 Feb 2025 05:00:30 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C0869280004; Fri, 28 Feb 2025 05:00:30 -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 986536B0083 for ; Fri, 28 Feb 2025 05:00:30 -0500 (EST) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 5738DAE011 for ; Fri, 28 Feb 2025 10:00:30 +0000 (UTC) X-FDA: 83168908620.20.FC0DA14 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.18]) by imf24.hostedemail.com (Postfix) with ESMTP id B0C8C18000D for ; Fri, 28 Feb 2025 10:00:27 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=JDUhSYCF; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf24.hostedemail.com: domain of kanchana.p.sridhar@intel.com designates 192.198.163.18 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=1740736828; 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=DT7qSsd6NzVQAqBGddK7aj/1FLc2FPkIQ1Vg7McwlLw=; b=tL2MpkcXRDaZgRoa0eeJ5RnzALhm6a142evnmuU2Yy/Z7kK2jA7VwqN8ELX5+fTxk3xIpU h51flH+c24/e2UhVr6fWhmzvkvNafMueOAslSDBk2ouAT02/ZJ3CkvXECCHfTwXgYFO0+f wtW7HAZxR1GcxV6IGaib3s81eqbZsLw= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=JDUhSYCF; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf24.hostedemail.com: domain of kanchana.p.sridhar@intel.com designates 192.198.163.18 as permitted sender) smtp.mailfrom=kanchana.p.sridhar@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1740736828; a=rsa-sha256; cv=none; b=8jrR8SF24K/TTTJsdQWJO6iGhfB1ojMHwLYhOwlIRrdf39wQRDcPATeFcC9ZmIzv1vZ2La a+bPAO4VbfdhAqBRoJPn3l23IaZpD6zz4IRG+WYlz1f+h35DXsgzrVKbimiT+qWp2i994l x7mC5Rh/DDjfyKF+udoVyH9CZvBCcnc= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1740736828; x=1772272828; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=5xvVgcGbsVS3XOQngdpBodjWDsRWeoD2A/h2JxKzioc=; b=JDUhSYCFDohhUPu/GUTIDeX1mevrJ9/2lw3kp61PEctUjKqq0LZje/Mz LcfaQuZkuXqdhB7JkwJADEPI7TzSCH8tkfQ3HuL6qWi2kU4/qJsmJRoA4 v14iNHgfFgbKh/Vozl2c3ibyiSRdGHqG7kYlEPfEKIE7oHO9bZ71gBsB0 N11lXacM279gJ5GHnDXSztQV61o2TiM36jJ8Kv6OUHcsmVIR1VhpilbC1 YB92bjXPc/KZYFQ6Tb/zeyWoQ10mb3amTNNkP9OjIC8+bkQzqlzQbpt0b I/LIKVsk6eCpHn51otRc2SBZ4oSvhE9WrvqGrL84hbl5KzMsBOHd7wCTK w==; X-CSE-ConnectionGUID: 9T+bbddTQK+vOVJkpTXpiQ== X-CSE-MsgGUID: +ACQtzmbRwK5BLi6DH1hPg== X-IronPort-AV: E=McAfee;i="6700,10204,11358"; a="40902563" X-IronPort-AV: E=Sophos;i="6.13,322,1732608000"; d="scan'208";a="40902563" Received: from orviesa006.jf.intel.com ([10.64.159.146]) by fmvoesa112.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Feb 2025 02:00:25 -0800 X-CSE-ConnectionGUID: BN2lmjiZSoC4tJ7eYm7eeg== X-CSE-MsgGUID: vEWfnqsCQWC/9TEbN1FKhA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.13,322,1732608000"; d="scan'208";a="117325690" Received: from jf5300-b11a338t.jf.intel.com ([10.242.51.115]) by orviesa006.jf.intel.com with ESMTP; 28 Feb 2025 02:00:25 -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, ying.huang@linux.alibaba.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 v7 01/15] crypto: acomp - Add synchronous/asynchronous acomp request chaining. Date: Fri, 28 Feb 2025 02:00:10 -0800 Message-Id: <20250228100024.332528-2-kanchana.p.sridhar@intel.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20250228100024.332528-1-kanchana.p.sridhar@intel.com> References: <20250228100024.332528-1-kanchana.p.sridhar@intel.com> MIME-Version: 1.0 X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: B0C8C18000D X-Rspam-User: X-Stat-Signature: sn8q39a7o4gdj9388kajdh8txmx7xtkm X-HE-Tag: 1740736827-498131 X-HE-Meta: U2FsdGVkX1/T6hnLz3ny6cLhNAixn1eMVdWK2MCuQ9lTyqN/D8rRGpwVPAFLQyLJ1zkGREvQay3Gqt2lr+xXUKYV3OsIRIfqhnSBGrxyxrGNMfqnyHZSkc8KwPH7f/SFgKgKQxQ/7mXuIEMv8ub+OPaXgxOlRI3Cn1rI+TAbqTfcrAD/5xeuTT2fAaO5aARvquI89lQNskiEfiXavISvfJlO6tSTt6uV/DV85XLw/eFsuY8iUTYRvUIXGXOeg4KqlGIC/euwYVcM3Yg6o3+nmcwbQl5QLY7pffGWAaFkPEF1I2PTNAmmpTez7NNMT1m4SDLyPDc/2ylGYH9m9BFw3pSR34tZ6Uf9JMGgcM2Vy1RwlRCxhnt4blTmKaq8DeZFsvddpke36Ffu8BH6WOUADqTx4G1JUNfDvodUPQILHbFA4VVhdQKbD339aazqJvxrtsxh2nRMyig4zvX7NAU2Vgw2KHFvnpzoILgRo+gvOc+BflIbbeaU/zV3MXbM887cUELxJ7ZmLr+SO6GKHtQ/xi0u+ebarsNVd78j1+gxxr16sg4WfQ65UBycR75pRc81HCS0c4MmsjLBR3esqCDjB6jiEqH6glMdkOgRnwjCS4lsXwiXF6TDP4CVVr2AZga354c/EYQGJ7danIu1ymqMCcqMi0zkZljs5X1u9MSHGn5wcT+FaN3Qy4BfrIgwUz5wp41G/9EHDt0UDPoOWsXALgWOZvQbFrgjRgNLp9ZS5Uew7Zf0+J9IulAMnMtv/89Ai8k91peaxjLtE2DmVtCUt5pPwKB/yjoSk3gxcacxXHZ8Ae04MG96OWQhn8XQIDP54VB1JulJJ+NpPEkJh/W4Vy6rRcB2RnbV9gS2ZrtAgo20fWg7R8E2lMFr7srvwQ2k3xqSAkjBPkiG0qsZrvDKTX961YhavDAe02Ol1s6T9Hh1kakMcJ8CNPmOguaByyT3CroVBbk4yK14S5u198Y kknKEkRx c2uX7N9c3K4Uu3WjQSDrky/FmolSBX3F0R9mADUjPO4XIobu0DDex9BpvsSKH2BCbcgi4WBA6bZlKOUDB4ekJSYxx4KZPoDe2YL3J5/79Iog08NwoFAiL5OIxEBhFsq8SFMqjVNa7nk2ZlCvaSz7ONWNJrcS6MeY2wWAbpE7cnntell21NF7YiEvECPKkKsJNP183tlyDw3K94X5b21+ZU3QBmU2Blr8yHBeW6vXweyEddxJn2tgck3zy7PJnVglsCa1wZqimhezwHkA= 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 is based on Herbert Xu's request chaining for ahash ("[PATCH 2/6] crypto: hash - Add request chaining API") [1]. The generic framework for request chaining that's provided in the ahash implementation has been used as reference to develop a similar synchronous request chaining framework for crypto_acomp. Furthermore, this commit develops an asynchronous request chaining framework and API that iaa_crypto can use for request chaining with parallelism, in order to fully benefit from Intel IAA's multiple compress/decompress engines in hardware. This allows us to gain significant latency improvements with IAA batching as compared to synchronous request chaining. Usage of acomp request chaining API: ==================================== Any crypto_acomp compressor can avail of request chaining as follows: Step 1: Create request chain: Request 0 (the first req in the chain): void acomp_reqchain_init(struct acomp_req *req, u32 flags, crypto_completion_t compl, void *data); Subsequent requests: void acomp_request_chain(struct acomp_req *req, struct acomp_req *head); Step 2: Process the request chain using the specified compress/decompress "op": 2.a) Synchronous: the chain of requests is processed in series: int acomp_do_req_chain(struct acomp_req *req, int (*op)(struct acomp_req *req)); 2.b) Asynchronous: the chain of requests is processed in parallel using a submit-poll paradigm: int acomp_do_async_req_chain(struct acomp_req *req, int (*op_submit)(struct acomp_req *req), int (*op_poll)(struct acomp_req *req)); Request chaining will be used in subsequent patches to implement compress/decompress batching in the iaa_crypto driver for the two supported IAA driver sync_modes: sync_mode = 'sync' will use (2.a), sync_mode = 'async' will use (2.b). These files are directly re-used from [1] which is not yet merged: include/crypto/algapi.h include/linux/crypto.h Hence, I am adding Herbert as the co-developer of this acomp request chaining patch. [1]: https://lore.kernel.org/linux-crypto/677614fbdc70b31df2e26483c8d2cd1510c8af91.1730021644.git.herbert@gondor.apana.org.au/ Suggested-by: Herbert Xu Signed-off-by: Kanchana P Sridhar Co-developed-by: Herbert Xu Signed-off-by: --- crypto/acompress.c | 284 ++++++++++++++++++++++++++++ include/crypto/acompress.h | 46 +++++ include/crypto/algapi.h | 10 + include/crypto/internal/acompress.h | 10 + include/linux/crypto.h | 39 ++++ 5 files changed, 389 insertions(+) diff --git a/crypto/acompress.c b/crypto/acompress.c index 6fdf0ff9f3c0..cb6444d09dd7 100644 --- a/crypto/acompress.c +++ b/crypto/acompress.c @@ -23,6 +23,19 @@ struct crypto_scomp; static const struct crypto_type crypto_acomp_type; +struct acomp_save_req_state { + struct list_head head; + struct acomp_req *req0; + struct acomp_req *cur; + int (*op)(struct acomp_req *req); + crypto_completion_t compl; + void *data; +}; + +static void acomp_reqchain_done(void *data, int err); +static int acomp_save_req(struct acomp_req *req, crypto_completion_t cplt); +static void acomp_restore_req(struct acomp_req *req); + static inline struct acomp_alg *__crypto_acomp_alg(struct crypto_alg *alg) { return container_of(alg, struct acomp_alg, calg.base); @@ -123,6 +136,277 @@ struct crypto_acomp *crypto_alloc_acomp_node(const char *alg_name, u32 type, } EXPORT_SYMBOL_GPL(crypto_alloc_acomp_node); +static int acomp_save_req(struct acomp_req *req, crypto_completion_t cplt) +{ + struct crypto_acomp *tfm = crypto_acomp_reqtfm(req); + struct acomp_save_req_state *state; + gfp_t gfp; + u32 flags; + + if (!acomp_is_async(tfm)) + return 0; + + flags = acomp_request_flags(req); + gfp = (flags & CRYPTO_TFM_REQ_MAY_SLEEP) ? GFP_KERNEL : GFP_ATOMIC; + state = kmalloc(sizeof(*state), gfp); + if (!state) + return -ENOMEM; + + state->compl = req->base.complete; + state->data = req->base.data; + state->req0 = req; + + req->base.complete = cplt; + req->base.data = state; + + return 0; +} + +static void acomp_restore_req(struct acomp_req *req) +{ + struct crypto_acomp *tfm = crypto_acomp_reqtfm(req); + struct acomp_save_req_state *state; + + if (!acomp_is_async(tfm)) + return; + + state = req->base.data; + + req->base.complete = state->compl; + req->base.data = state->data; + kfree(state); +} + +static int acomp_reqchain_finish(struct acomp_save_req_state *state, + int err, u32 mask) +{ + struct acomp_req *req0 = state->req0; + struct acomp_req *req = state->cur; + struct acomp_req *n; + + req->base.err = err; + + if (req == req0) + INIT_LIST_HEAD(&req->base.list); + else + list_add_tail(&req->base.list, &req0->base.list); + + list_for_each_entry_safe(req, n, &state->head, base.list) { + list_del_init(&req->base.list); + + req->base.flags &= mask; + req->base.complete = acomp_reqchain_done; + req->base.data = state; + state->cur = req; + err = state->op(req); + + if (err == -EINPROGRESS) { + if (!list_empty(&state->head)) + err = -EBUSY; + goto out; + } + + if (err == -EBUSY) + goto out; + + req->base.err = err; + list_add_tail(&req->base.list, &req0->base.list); + } + + acomp_restore_req(req0); + +out: + return err; +} + +static void acomp_reqchain_done(void *data, int err) +{ + struct acomp_save_req_state *state = data; + crypto_completion_t compl = state->compl; + + data = state->data; + + if (err == -EINPROGRESS) { + if (!list_empty(&state->head)) + return; + goto notify; + } + + err = acomp_reqchain_finish(state, err, CRYPTO_TFM_REQ_MAY_BACKLOG); + if (err == -EBUSY) + return; + +notify: + compl(data, err); +} + +int acomp_do_req_chain(struct acomp_req *req, + int (*op)(struct acomp_req *req)) +{ + struct crypto_acomp *tfm = crypto_acomp_reqtfm(req); + struct acomp_save_req_state *state; + struct acomp_save_req_state state0; + int err = 0; + + if (!acomp_request_chained(req) || list_empty(&req->base.list) || + !crypto_acomp_req_chain(tfm)) + return op(req); + + state = &state0; + + if (acomp_is_async(tfm)) { + err = acomp_save_req(req, acomp_reqchain_done); + if (err) { + struct acomp_req *r2; + + req->base.err = err; + list_for_each_entry(r2, &req->base.list, base.list) + r2->base.err = err; + + return err; + } + + state = req->base.data; + } + + state->op = op; + state->cur = req; + INIT_LIST_HEAD(&state->head); + list_splice(&req->base.list, &state->head); + + err = op(req); + if (err == -EBUSY || err == -EINPROGRESS) + return -EBUSY; + + return acomp_reqchain_finish(state, err, ~0); +} +EXPORT_SYMBOL_GPL(acomp_do_req_chain); + +static void acomp_async_reqchain_done(struct acomp_req *req0, + struct list_head *state, + int (*op_poll)(struct acomp_req *req)) +{ + struct acomp_req *req, *n; + bool req0_done = false; + int err; + + while (!list_empty(state)) { + + if (!req0_done) { + err = op_poll(req0); + if (!(err == -EAGAIN || err == -EINPROGRESS || err == -EBUSY)) { + req0->base.err = err; + req0_done = true; + } + } + + list_for_each_entry_safe(req, n, state, base.list) { + err = op_poll(req); + + if (err == -EAGAIN || err == -EINPROGRESS || err == -EBUSY) + continue; + + req->base.err = err; + list_del_init(&req->base.list); + list_add_tail(&req->base.list, &req0->base.list); + } + } + + while (!req0_done) { + err = op_poll(req0); + if (!(err == -EAGAIN || err == -EINPROGRESS || err == -EBUSY)) { + req0->base.err = err; + break; + } + } +} + +static int acomp_async_reqchain_finish(struct acomp_req *req0, + struct list_head *state, + int (*op_submit)(struct acomp_req *req), + int (*op_poll)(struct acomp_req *req)) +{ + struct acomp_req *req, *n; + int err = 0; + + INIT_LIST_HEAD(&req0->base.list); + + list_for_each_entry_safe(req, n, state, base.list) { + BUG_ON(req == req0); + + err = op_submit(req); + + if (!(err == -EINPROGRESS || err == -EBUSY)) { + req->base.err = err; + list_del_init(&req->base.list); + list_add_tail(&req->base.list, &req0->base.list); + } + } + + acomp_async_reqchain_done(req0, state, op_poll); + + return req0->base.err; +} + +int acomp_do_async_req_chain(struct acomp_req *req, + int (*op_submit)(struct acomp_req *req), + int (*op_poll)(struct acomp_req *req)) +{ + struct crypto_acomp *tfm = crypto_acomp_reqtfm(req); + struct list_head state; + struct acomp_req *r2; + int err = 0; + void *req0_data = req->base.data; + + if (!acomp_request_chained(req) || list_empty(&req->base.list) || + !acomp_is_async(tfm) || !crypto_acomp_req_chain(tfm)) { + + err = op_submit(req); + + if (err == -EINPROGRESS || err == -EBUSY) { + bool req0_done = false; + + while (!req0_done) { + err = op_poll(req); + if (!(err == -EAGAIN || err == -EINPROGRESS || err == -EBUSY)) { + req->base.err = err; + break; + } + } + } else { + req->base.err = err; + } + + req->base.data = req0_data; + if (acomp_is_async(tfm)) + req->base.complete(req->base.data, req->base.err); + + return err; + } + + err = op_submit(req); + req->base.err = err; + + if (err && !(err == -EINPROGRESS || err == -EBUSY)) + goto err_prop; + + INIT_LIST_HEAD(&state); + list_splice(&req->base.list, &state); + + err = acomp_async_reqchain_finish(req, &state, op_submit, op_poll); + req->base.data = req0_data; + req->base.complete(req->base.data, req->base.err); + + return err; + +err_prop: + list_for_each_entry(r2, &req->base.list, base.list) + r2->base.err = err; + + return err; +} +EXPORT_SYMBOL_GPL(acomp_do_async_req_chain); + struct acomp_req *acomp_request_alloc(struct crypto_acomp *acomp) { struct crypto_tfm *tfm = crypto_acomp_tfm(acomp); diff --git a/include/crypto/acompress.h b/include/crypto/acompress.h index 54937b615239..e6783deba3ac 100644 --- a/include/crypto/acompress.h +++ b/include/crypto/acompress.h @@ -206,6 +206,7 @@ static inline void acomp_request_set_callback(struct acomp_req *req, req->base.data = data; req->base.flags &= CRYPTO_ACOMP_ALLOC_OUTPUT; req->base.flags |= flgs & ~CRYPTO_ACOMP_ALLOC_OUTPUT; + req->base.flags &= ~CRYPTO_TFM_REQ_CHAIN; } /** @@ -237,6 +238,51 @@ static inline void acomp_request_set_params(struct acomp_req *req, req->flags |= CRYPTO_ACOMP_ALLOC_OUTPUT; } +static inline u32 acomp_request_flags(struct acomp_req *req) +{ + return req->base.flags; +} + +static inline void acomp_reqchain_init(struct acomp_req *req, + u32 flags, crypto_completion_t compl, + void *data) +{ + acomp_request_set_callback(req, flags, compl, data); + crypto_reqchain_init(&req->base); +} + +static inline bool acomp_is_reqchain(struct acomp_req *req) +{ + return crypto_is_reqchain(&req->base); +} + +static inline void acomp_reqchain_clear(struct acomp_req *req, void *data) +{ + struct crypto_wait *wait = (struct crypto_wait *)data; + reinit_completion(&wait->completion); + crypto_reqchain_clear(&req->base); + acomp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG, + crypto_req_done, data); +} + +static inline void acomp_request_chain(struct acomp_req *req, + struct acomp_req *head) +{ + crypto_request_chain(&req->base, &head->base); +} + +int acomp_do_req_chain(struct acomp_req *req, + int (*op)(struct acomp_req *req)); + +int acomp_do_async_req_chain(struct acomp_req *req, + int (*op_submit)(struct acomp_req *req), + int (*op_poll)(struct acomp_req *req)); + +static inline int acomp_request_err(struct acomp_req *req) +{ + return req->base.err; +} + /** * crypto_acomp_compress() -- Invoke asynchronous compress operation * diff --git a/include/crypto/algapi.h b/include/crypto/algapi.h index 156de41ca760..c5df380c7d08 100644 --- a/include/crypto/algapi.h +++ b/include/crypto/algapi.h @@ -271,4 +271,14 @@ static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm) return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK; } +static inline bool crypto_request_chained(struct crypto_async_request *req) +{ + return req->flags & CRYPTO_TFM_REQ_CHAIN; +} + +static inline bool crypto_tfm_req_chain(struct crypto_tfm *tfm) +{ + return tfm->__crt_alg->cra_flags & CRYPTO_ALG_REQ_CHAIN; +} + #endif /* _CRYPTO_ALGAPI_H */ diff --git a/include/crypto/internal/acompress.h b/include/crypto/internal/acompress.h index 8831edaafc05..53b4ef59b48c 100644 --- a/include/crypto/internal/acompress.h +++ b/include/crypto/internal/acompress.h @@ -84,6 +84,16 @@ static inline void __acomp_request_free(struct acomp_req *req) kfree_sensitive(req); } +static inline bool acomp_request_chained(struct acomp_req *req) +{ + return crypto_request_chained(&req->base); +} + +static inline bool crypto_acomp_req_chain(struct crypto_acomp *tfm) +{ + return crypto_tfm_req_chain(&tfm->base); +} + /** * crypto_register_acomp() -- Register asynchronous compression algorithm * diff --git a/include/linux/crypto.h b/include/linux/crypto.h index b164da5e129e..f1bc282e1ed6 100644 --- a/include/linux/crypto.h +++ b/include/linux/crypto.h @@ -13,6 +13,8 @@ #define _LINUX_CRYPTO_H #include +#include +#include #include #include #include @@ -124,6 +126,9 @@ */ #define CRYPTO_ALG_FIPS_INTERNAL 0x00020000 +/* Set if the algorithm supports request chains. */ +#define CRYPTO_ALG_REQ_CHAIN 0x00040000 + /* * Transform masks and values (for crt_flags). */ @@ -133,6 +138,7 @@ #define CRYPTO_TFM_REQ_FORBID_WEAK_KEYS 0x00000100 #define CRYPTO_TFM_REQ_MAY_SLEEP 0x00000200 #define CRYPTO_TFM_REQ_MAY_BACKLOG 0x00000400 +#define CRYPTO_TFM_REQ_CHAIN 0x00000800 /* * Miscellaneous stuff. @@ -174,6 +180,7 @@ struct crypto_async_request { struct crypto_tfm *tfm; u32 flags; + int err; }; /** @@ -391,6 +398,9 @@ void crypto_req_done(void *req, int err); static inline int crypto_wait_req(int err, struct crypto_wait *wait) { + if (!wait) + return err; + switch (err) { case -EINPROGRESS: case -EBUSY: @@ -540,5 +550,34 @@ int crypto_comp_decompress(struct crypto_comp *tfm, const u8 *src, unsigned int slen, u8 *dst, unsigned int *dlen); +static inline void crypto_reqchain_init(struct crypto_async_request *req) +{ + req->err = -EINPROGRESS; + req->flags |= CRYPTO_TFM_REQ_CHAIN; + INIT_LIST_HEAD(&req->list); +} + +static inline bool crypto_is_reqchain(struct crypto_async_request *req) +{ + return req->flags & CRYPTO_TFM_REQ_CHAIN; +} + +static inline void crypto_reqchain_clear(struct crypto_async_request *req) +{ + req->flags &= ~CRYPTO_TFM_REQ_CHAIN; +} + +static inline void crypto_request_chain(struct crypto_async_request *req, + struct crypto_async_request *head) +{ + req->err = -EINPROGRESS; + list_add_tail(&req->list, &head->list); +} + +static inline bool crypto_tfm_is_async(struct crypto_tfm *tfm) +{ + return tfm->__crt_alg->cra_flags & CRYPTO_ALG_ASYNC; +} + #endif /* _LINUX_CRYPTO_H */ From patchwork Fri Feb 28 10:00:11 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: 13996092 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 8C903C19776 for ; Fri, 28 Feb 2025 10:00:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3AAD7280004; Fri, 28 Feb 2025 05:00:32 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 3552A280002; Fri, 28 Feb 2025 05:00:32 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1A78B280004; Fri, 28 Feb 2025 05:00:32 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id EF1F4280002 for ; Fri, 28 Feb 2025 05:00:31 -0500 (EST) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id A2A078069F for ; Fri, 28 Feb 2025 10:00:31 +0000 (UTC) X-FDA: 83168908662.09.99F39A3 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.18]) by imf08.hostedemail.com (Postfix) with ESMTP id 73530160019 for ; Fri, 28 Feb 2025 10:00:29 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=MUmAdtcy; spf=pass (imf08.hostedemail.com: domain of kanchana.p.sridhar@intel.com designates 192.198.163.18 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=1740736829; 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=Ms2/OmM3n4gs5eeF3YqdiC/vRV2Q9z8SVn1htLBnVcc=; b=bJNwXnrZsDOooTlfEGoAXd5cWhbbVkXTNB/Eyyuyw4IB2CTP5v7pIWDs0u0gh4QXfcVt0k +D39ql8KRdm8eb3HTD4mkSsxQpphxzx1wYIteKtpT7ud12IM0TTr7kToaOVq0FsbEOuYWI rKnT+7C4FEyVBr66a8zUEllFWSZJneY= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=MUmAdtcy; spf=pass (imf08.hostedemail.com: domain of kanchana.p.sridhar@intel.com designates 192.198.163.18 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=1740736829; a=rsa-sha256; cv=none; b=KT2xlHMw79ORZg/kDry+ccIxl5BW/yTDx6V0yy0zlQLaob1tp9x8pAcGVOLsgAbOAe2Baq 8fAP2Rd464tOsVbUkqA460Ngqvfp9TLYQvqdXvCz7ypWKqHPwdqaQyBygdpPsBZq2OnQFA oh9q/GVERsj75kiTBZfvSe3lOWmU+8U= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1740736829; x=1772272829; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=2yHMsDkER4HjRnd6CZHCqrmnfsYRe9xtFBscYIAxqhQ=; b=MUmAdtcy3OLZQJXtL4H8L3I9HPg8SCaIOCViiizqLjm6at99eARpm5/5 Ruo4Mm3VDTgqPIVhDrNbjQinaABuTj/acfQgDoUElAulA+9xZ2hKzNjOc /52tQlv4RtcHyNATrIyydlDQg5rSiP5+11n07q+qbhO8/gkWF+NADReXF cK3dU1eYYuzUAO8dX+rnSRbgEtPRto9aCaK7+IxXlkpxUOe41LJwJaF5+ LgU6A9nQ/QuK/DgZi3wJyEk/ybbcUb8v7crUP/n7aKa0stSS9gTYMprg1 c0wM/BYjAlulvobzQWAoJoB/0FEJmd23XwBleKnCS0GfXMzWS8JD4iu8Q Q==; X-CSE-ConnectionGUID: qAwdbi4lS1+SIT+Iy983Kw== X-CSE-MsgGUID: 5RhCkpXxRu2WFUh7d+GT5Q== X-IronPort-AV: E=McAfee;i="6700,10204,11358"; a="40902578" X-IronPort-AV: E=Sophos;i="6.13,322,1732608000"; d="scan'208";a="40902578" Received: from orviesa006.jf.intel.com ([10.64.159.146]) by fmvoesa112.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Feb 2025 02:00:26 -0800 X-CSE-ConnectionGUID: QvtWhEWqRwmK+E0N1RUkWg== X-CSE-MsgGUID: eTAuQPsfQM6G6JzddUkWLA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.13,322,1732608000"; d="scan'208";a="117325694" Received: from jf5300-b11a338t.jf.intel.com ([10.242.51.115]) by orviesa006.jf.intel.com with ESMTP; 28 Feb 2025 02:00:25 -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, ying.huang@linux.alibaba.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 v7 02/15] crypto: acomp - New interfaces to facilitate batching support in acomp & drivers. Date: Fri, 28 Feb 2025 02:00:11 -0800 Message-Id: <20250228100024.332528-3-kanchana.p.sridhar@intel.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20250228100024.332528-1-kanchana.p.sridhar@intel.com> References: <20250228100024.332528-1-kanchana.p.sridhar@intel.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 73530160019 X-Stat-Signature: gh4fd4xjrreiczgpkhec9jaf8neknpdi X-HE-Tag: 1740736829-4951 X-HE-Meta: U2FsdGVkX1/gG+T/Rf3EBVQMNaOkYHRVZrpVgANou1G8kK3fxS/S3RfNeLA4Xc2Jp2+3oRK8SqUfhDdX+ZNeV4SvonKN4OXb9V9UfSmzT2JhR2E1bgQhk15fQvHPt3BXiqbDtSI3nTZfm178eOP2CMJHtlXVhj9PZ2YKo3BWUCOD719GiCfBT5tBpn03dMrnjDqGqwRzfTKVZR2WqYEi6/KVkWPHjsSaJByHN2yp+M5tS5rau+JpbnNqMc2kC1hayxELuqhyUgtKx9660TkuHwap8U3iDvbjTytq/pzs/b/zM59zZ//A5SkrRxnji+o9xAmKcZhOie9vlO30rWT/geDPv/FgCrx3ZWM7CQzp1JkFAo6yvzmvvpWKLYT3jSVQtOMCA/mKMxhVZM/Vq3oSIcNxosMFArUSdgWNfist49WxvRMCNa4qGFnO7BATZzsIuZ4fnIyWq4t8Gclv3fqYfrVD4tMwdLi0t/yHIOoMgobNSdRnUF0SB9eE79bDVxFUw31xn5wlqLxNTOB8hwhNTnmkbGlDZvKs0lmuAYNI11pLgAQNkXIBOTAmQXk85tVWKixyNx9D5P2w/y2hnbMSlIifVWIaHjwm98tBavGjdCsGRZvfAWQpGhp+zR+ePVITA2nNY+Vpp8lH+/nJ6R+5gTTQRZnKhTHO0T6L8bT0pHZy0btoVLnOe/zsfTdz80o6TmmcbNz8UJBzftwKl/aZIQBaeZK6bz/ELUGOJs7lXD+m4Dc8QUXfaf5C1PVL2buedjFm26cEIhQ4tmXLFH6PaLCzvTR9vppAetDFIgELRA5F3DQPjVDvj+BQKmOK+6CqdcH4EwEqPpaek2ytehN27GMJalFavsX7Dhc1NqoXUkczm2Tnc1CPKeV8pmk5pyeqJwhfVZ8VPTTu0qCoLSHcZFzpp2nBTfpjUyJh0uLmSZrc0BJ+sqm3Vg+GP9pEZ/lahv9l+1Cn430+TQ6xMED is0yLqCR 8wHU2n3M5ko5zNHC4txiLN92DxUf11WW6KKImuB/JejvldBiC6lbsfFZw651PjU0bMbeOMiUlweoVBlaQt7gaq2oYBzxyMRDztQwabes3qOf6YuxNGe/0DSjO1n1PZGF6qAduKS1fjW3yTvOTfoCI/KdGqcAvjrqZnS+/gxDTEDYtL0Xwwwa3s7NtJA== 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 commit adds a get_batch_size() interface to: struct acomp_alg struct crypto_acomp A crypto_acomp compression algorithm that supports batching of compressions and decompressions must register and provide an implementation for this API, so that higher level modules such as zswap and zram can allocate resources for submitting multiple compress/decompress jobs that can be batched. A new helper function acomp_has_async_batching() can be invoked to query if a crypto_acomp has registered this API. Further, the newly added crypto_acomp API "crypto_acomp_batch_size()" is provided for use by higher level modules like zswap and zram. crypto_acomp_batch_size() returns 1 if the acomp has not provided an implementation for get_batch_size(). For instance, zswap can call crypto_acomp_batch_size() to get the maximum batch-size supported by the compressor. Based on this, zswap can use the minimum of any zswap-specific upper limits for batch-size and the compressor's max batch-size, to allocate batching resources. Further, the way that zswap can avail of the compressor's batching capability is by using request chaining to create a list requests chained to a head request. zswap can call crypto_acomp_compress() or crypto_acomp_decompress() with the head request in the chain for processing the chain as a batch. The call into crypto for compress/decompress will thus remain the same from zswap's perspective for both, batching and sequential compressions/decompressions. An acomp_is_reqchain() API is introduced, that a driver can call to query if a request received from compress/decompress represents a request chain, and accordingly, process the request chain using either one of: acomp_do_req_chain() acomp_do_async_req_chain() These capabilities allow the iaa_crypto Intel IAA driver to register and implement the get_batch_size() acomp_alg interface, that can subsequently be invoked from the kernel zswap/zram modules to construct a request chain to compress/decompress pages in parallel in the IAA hardware accelerator to improve swapout/swapin performance. Signed-off-by: Kanchana P Sridhar --- crypto/acompress.c | 1 + include/crypto/acompress.h | 28 ++++++++++++++++++++++++++++ include/crypto/internal/acompress.h | 4 ++++ 3 files changed, 33 insertions(+) diff --git a/crypto/acompress.c b/crypto/acompress.c index cb6444d09dd7..b2a6c06d7262 100644 --- a/crypto/acompress.c +++ b/crypto/acompress.c @@ -84,6 +84,7 @@ static int crypto_acomp_init_tfm(struct crypto_tfm *tfm) acomp->compress = alg->compress; acomp->decompress = alg->decompress; + acomp->get_batch_size = alg->get_batch_size; acomp->dst_free = alg->dst_free; acomp->reqsize = alg->reqsize; diff --git a/include/crypto/acompress.h b/include/crypto/acompress.h index e6783deba3ac..147f184b6bea 100644 --- a/include/crypto/acompress.h +++ b/include/crypto/acompress.h @@ -43,6 +43,9 @@ struct acomp_req { * * @compress: Function performs a compress operation * @decompress: Function performs a de-compress operation + * @get_batch_size: Maximum batch-size for batching compress/decompress + * operations. If registered, the acomp must provide + * a batching implementation using request chaining. * @dst_free: Frees destination buffer if allocated inside the * algorithm * @reqsize: Context size for (de)compression requests @@ -51,6 +54,7 @@ struct acomp_req { struct crypto_acomp { int (*compress)(struct acomp_req *req); int (*decompress)(struct acomp_req *req); + unsigned int (*get_batch_size)(void); void (*dst_free)(struct scatterlist *dst); unsigned int reqsize; struct crypto_tfm base; @@ -142,6 +146,13 @@ static inline bool acomp_is_async(struct crypto_acomp *tfm) CRYPTO_ALG_ASYNC; } +static inline bool acomp_has_async_batching(struct crypto_acomp *tfm) +{ + return (acomp_is_async(tfm) && + (crypto_comp_alg_common(tfm)->base.cra_flags & CRYPTO_ALG_TYPE_ACOMPRESS) && + tfm->get_batch_size); +} + static inline struct crypto_acomp *crypto_acomp_reqtfm(struct acomp_req *req) { return __crypto_acomp_tfm(req->base.tfm); @@ -311,4 +322,21 @@ static inline int crypto_acomp_decompress(struct acomp_req *req) return crypto_acomp_reqtfm(req)->decompress(req); } +/** + * crypto_acomp_batch_size() -- Get the algorithm's batch size + * + * Function returns the algorithm's batch size for batching operations + * + * @tfm: ACOMPRESS tfm handle allocated with crypto_alloc_acomp() + * + * Return: crypto_acomp's batch size. + */ +static inline unsigned int crypto_acomp_batch_size(struct crypto_acomp *tfm) +{ + if (acomp_has_async_batching(tfm)) + return tfm->get_batch_size(); + + return 1; +} + #endif diff --git a/include/crypto/internal/acompress.h b/include/crypto/internal/acompress.h index 53b4ef59b48c..24b63db56dfb 100644 --- a/include/crypto/internal/acompress.h +++ b/include/crypto/internal/acompress.h @@ -17,6 +17,9 @@ * * @compress: Function performs a compress operation * @decompress: Function performs a de-compress operation + * @get_batch_size: Maximum batch-size for batching compress/decompress + * operations. If registered, the acomp must provide + * a batching implementation using request chaining. * @dst_free: Frees destination buffer if allocated inside the algorithm * @init: Initialize the cryptographic transformation object. * This function is used to initialize the cryptographic @@ -37,6 +40,7 @@ struct acomp_alg { int (*compress)(struct acomp_req *req); int (*decompress)(struct acomp_req *req); + unsigned int (*get_batch_size)(void); void (*dst_free)(struct scatterlist *dst); int (*init)(struct crypto_acomp *tfm); void (*exit)(struct crypto_acomp *tfm); From patchwork Fri Feb 28 10:00:12 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: 13996093 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 C9AD1C19776 for ; Fri, 28 Feb 2025 10:00:43 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CCE52280006; Fri, 28 Feb 2025 05:00:32 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id C2E37280002; Fri, 28 Feb 2025 05:00:32 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AAA75280006; Fri, 28 Feb 2025 05:00:32 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 87300280002 for ; Fri, 28 Feb 2025 05:00:32 -0500 (EST) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 3BC211A0156 for ; Fri, 28 Feb 2025 10:00:32 +0000 (UTC) X-FDA: 83168908704.23.C9BBCCD Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.18]) by imf27.hostedemail.com (Postfix) with ESMTP id 479D140009 for ; Fri, 28 Feb 2025 10:00:29 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=iha7GWEP; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf27.hostedemail.com: domain of kanchana.p.sridhar@intel.com designates 192.198.163.18 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=1740736829; 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=S7zn2qvfvOLCDp2F8MXE3i6nl8YZve4VX5u9OSi+jXs=; b=YPQS1xOE/jdOtGTvgwZF8vU4kerd152IU3f4OcuHsGskvHxugGGQN1UDGu11VxBeRwtiIa bUvh8PjRiBvNcPecZAmWgoBZ72sDvDaetq9P0SnS7kl9wCdUbO5bYwh3ekSdsJlfFKFrVt kJ9VmNKcO/VPfpPljZczMX2dUvH6adM= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=iha7GWEP; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf27.hostedemail.com: domain of kanchana.p.sridhar@intel.com designates 192.198.163.18 as permitted sender) smtp.mailfrom=kanchana.p.sridhar@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1740736829; a=rsa-sha256; cv=none; b=4Uz4/gSziZrvLxqHkQstsTj4gTixbxfkVs1IVtUBvAsXQ01N3Zz+jEA8o1BSR8IhvvbOXG E+xyRMYtHovSZpLQe+yMZbI/u9CVpI5HVeiJnQ49uRqdZAQPkdyrsPvrPr7NTHs0wLIn6J KIX+/tLDYqBRVDlhE/hIjFW+Pgd+Afg= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1740736829; x=1772272829; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=8AO2hJSnLaCRd/8HtMcdHOYwB/zSpBOSifH5FW9w0Ms=; b=iha7GWEPuGjBRnaJSSWT3MSnuImMMVUjyny+DP9qFU7e6TIDW3xEv/hn BTeGtXpl1QIvLdPRWT3CdxTSaPYdUbJuC5AteaGjMGf02cPRhgb6bg7LC maLmslw826zLHZ9AvWtP/V1YZreu2Zaue1TRTrvHS0fsov7j9jR0+dztS TVsTB1crMmRc6gXlnmKqSm1tQHVsMtQtR0R8NzSR5PfabJ9yEJXLkxikC tAudo/rXzUQEb3T2oekHidtAhUYihkeGaIG/OodHp8y1MHpkyHei6Ain+ h7VllME3NiEAid8Gk+7q5dWJhmVAYynzB5tuburw+6b5NGewT+UxwViRq w==; X-CSE-ConnectionGUID: I4de1aUiQDqHOIhHjgSSuA== X-CSE-MsgGUID: 5FemGjv3T4GCloae4WNRIw== X-IronPort-AV: E=McAfee;i="6700,10204,11358"; a="40902591" X-IronPort-AV: E=Sophos;i="6.13,322,1732608000"; d="scan'208";a="40902591" Received: from orviesa006.jf.intel.com ([10.64.159.146]) by fmvoesa112.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Feb 2025 02:00:26 -0800 X-CSE-ConnectionGUID: +uREVyviRsCit8ekE1F3AQ== X-CSE-MsgGUID: PZ6fGJj+QYaoQWJo3S6Pwg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.13,322,1732608000"; d="scan'208";a="117325698" Received: from jf5300-b11a338t.jf.intel.com ([10.242.51.115]) by orviesa006.jf.intel.com with ESMTP; 28 Feb 2025 02:00:26 -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, ying.huang@linux.alibaba.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 v7 03/15] crypto: iaa - Add an acomp_req flag CRYPTO_ACOMP_REQ_POLL to enable async mode. Date: Fri, 28 Feb 2025 02:00:12 -0800 Message-Id: <20250228100024.332528-4-kanchana.p.sridhar@intel.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20250228100024.332528-1-kanchana.p.sridhar@intel.com> References: <20250228100024.332528-1-kanchana.p.sridhar@intel.com> MIME-Version: 1.0 X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 479D140009 X-Rspam-User: X-Stat-Signature: q93gp6d7kh554ahmie1tn9yf1g58mw8r X-HE-Tag: 1740736829-559184 X-HE-Meta: U2FsdGVkX1/xGTh2qS87ejUJksqwBsOVZ2y209M3Ank40TTsvvokRVRJwXvIRqXB/6u8y8YmG99qJ55P6BqN/VMv+V+kyyBOnjBP15QPRJV2mFXGeQcrIyrUtAIDWSivQjo4dJISQpOL6nKv7Gr+lkRoFE7WnKlS/BwZR0VykhwEZND0oe4APDrARrAaKlECJOyqSwzUyhiaXWx+8BIlm0c9beyNSEFGQxV+mfWBn003cmk5v0XwA3Y5In5IyzN85WE11DBWKyRLrwDEGLijOz6aOI/11bY3k3QECl9ZrgaCMjySiXR+PuYgK/XZakOIw+yz3QfgiP8Wn5t5e3NT2374tRNiJZvS2VLmbfS6CduD+QmxBt6rd3JvoCJ3HyJC35C9yG4I7rxvrN9UEUpGOzo0o7+Jg5kGMNZg3R2TfcLZxaH/64Ki1sYnIZaOaR9yAv2vx0BAGo2mP8Iy8A7KHz6rVQ+gT31GkVaGjJ0+a9Dn+El/reYW3X3pEN4z8SCK8fSEidpTe8q7uv1tVqFZreLI7dL5kEiYg5RAL8iNtgEFvoZr1QaIbmcE+ZZhsa2EfiDWeHOGjCgvEfcq+e2TLBcxKu60ld/X9ZT/jnwbQaffuYkYJvq3PIseFmqu9MhyG/sqpwgzyIBuvsBYvoiV0514tWOZMhbZK0JKr/hBL+No3ajcaVO6UECdXrGad6bWoEX/nkxAl31Ubz8fpje2hj114j1tjRp5jxmIvG6N7Np/6/fSre1m98TOn7YAG3j2TJlMcUFAqA+dHOqDoC1iQXoWEGPZ4/iaqHN/fMjB0WFBM8Bu+s9u7vvT6nrTDQiAs6FaWeFzz3yDIqPIbpMRuJpSlpTmtHDDi4BpHWWmBoqtI4B8FmWuInNbUYbQ8wNFRUiMGTN+2i7RDSqY+1T4+v4G69PAlT41hrbWwFZpT4GfGileoRtKASGGtC8s/N3F7ep7uce+ZDckc4MrGrD WqvAl7de wW8mnTTdjQLT7rkvkccl285dTbB+m3Efa6enZmWJIeHP9IJJj76b0qcERMg/CqkGB04/7TR6g+z1Dse1e7umuMDlWEqXX6h39MUwki1QO+mWfGyKH6+0bZFkpJyr13TugBAgHfv1eoAbxTtSCU3eKIEalaWSCJ4ekE0LKb3pyxskCyZjUN3m9OjTgGZcCMm4tkptkUFMNsTxKeN44aShYuxTO8Q26nKq3JeOTSYveRLJ3cD0boq3WceiqN+nh54HNSVklfFYtkYZyPhQsCjz36q2KrDAQcb6vBtID 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: If the iaa_crypto driver has async_mode set to true, and use_irq set to false, it can still be forced to use synchronous mode by turning off the CRYPTO_ACOMP_REQ_POLL flag in req->flags. In other words, all three of the following need to be true for a request to be processed in fully async poll mode: 1) async_mode should be "true" 2) use_irq should be "false" 3) req->flags & CRYPTO_ACOMP_REQ_POLL should be "true" Suggested-by: Herbert Xu Signed-off-by: Kanchana P Sridhar --- drivers/crypto/intel/iaa/iaa_crypto_main.c | 11 ++++++++++- include/crypto/acompress.h | 5 +++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/drivers/crypto/intel/iaa/iaa_crypto_main.c b/drivers/crypto/intel/iaa/iaa_crypto_main.c index c3776b0de51d..d7983ab3c34a 100644 --- a/drivers/crypto/intel/iaa/iaa_crypto_main.c +++ b/drivers/crypto/intel/iaa/iaa_crypto_main.c @@ -1520,6 +1520,10 @@ static int iaa_comp_acompress(struct acomp_req *req) return -EINVAL; } + /* If the caller has requested no polling, disable async. */ + if (!(req->flags & CRYPTO_ACOMP_REQ_POLL)) + disable_async = true; + cpu = get_cpu(); wq = wq_table_next_wq(cpu); put_cpu(); @@ -1712,6 +1716,7 @@ static int iaa_comp_adecompress(struct acomp_req *req) { struct crypto_tfm *tfm = req->base.tfm; dma_addr_t src_addr, dst_addr; + bool disable_async = false; int nr_sgs, cpu, ret = 0; struct iaa_wq *iaa_wq; struct device *dev; @@ -1727,6 +1732,10 @@ static int iaa_comp_adecompress(struct acomp_req *req) return -EINVAL; } + /* If the caller has requested no polling, disable async. */ + if (!(req->flags & CRYPTO_ACOMP_REQ_POLL)) + disable_async = true; + if (!req->dst) return iaa_comp_adecompress_alloc_dest(req); @@ -1775,7 +1784,7 @@ static int iaa_comp_adecompress(struct acomp_req *req) req->dst, req->dlen, sg_dma_len(req->dst)); ret = iaa_decompress(tfm, req, wq, src_addr, req->slen, - dst_addr, &req->dlen, false); + dst_addr, &req->dlen, disable_async); if (ret == -EINPROGRESS) return ret; diff --git a/include/crypto/acompress.h b/include/crypto/acompress.h index 147f184b6bea..afadf84f236d 100644 --- a/include/crypto/acompress.h +++ b/include/crypto/acompress.h @@ -14,6 +14,11 @@ #include #define CRYPTO_ACOMP_ALLOC_OUTPUT 0x00000001 +/* + * If set, the driver must have a way to submit the req, then + * poll its completion status for success/error. + */ +#define CRYPTO_ACOMP_REQ_POLL 0x00000002 #define CRYPTO_ACOMP_DST_MAX 131072 /** From patchwork Fri Feb 28 10:00:13 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: 13996094 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 D03DCC19776 for ; Fri, 28 Feb 2025 10:00:46 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3F5E7280008; Fri, 28 Feb 2025 05:00:34 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 3ABEA280002; Fri, 28 Feb 2025 05:00:34 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1AA4C280008; Fri, 28 Feb 2025 05:00:34 -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 F0434280002 for ; Fri, 28 Feb 2025 05:00:33 -0500 (EST) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 82A23120722 for ; Fri, 28 Feb 2025 10:00:33 +0000 (UTC) X-FDA: 83168908746.20.4A1DDC9 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.18]) by imf24.hostedemail.com (Postfix) with ESMTP id 982C818000E for ; Fri, 28 Feb 2025 10:00:30 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=KGniG8TD; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf24.hostedemail.com: domain of kanchana.p.sridhar@intel.com designates 192.198.163.18 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=1740736830; 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=CxDBO9hF3bAE8sokaQQ0dSqjUz0eILnCamYYQTChDNg=; b=CIYhSeIJoqGI+AoWFgDmHdqAZD8VY/2CLa1zttnUe4CBqU5O31UoiTl46C89Hgz6a2jsqO dRBTIJy9dEdaMOlNhU2E3J7QhAw4Itfefy/TeeJmqe0tvVfSBeidHKd678Qv6nnyYUGQL/ 2LUNXmkSLXdv87LJm82pAABoZKxhfu8= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=KGniG8TD; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf24.hostedemail.com: domain of kanchana.p.sridhar@intel.com designates 192.198.163.18 as permitted sender) smtp.mailfrom=kanchana.p.sridhar@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1740736830; a=rsa-sha256; cv=none; b=UUD2hf3MaB71/4SQKyN0x1D1PmdaU1KMXrVoGiWMCz6Hg5q3XocHqW01UGJkWhw0w0ppcu FL+4r+Nn5/Zwh73kcNj2zXr3CBXN/r+qvFjeLPYLcuwIS3Rq9XDjDH0oPxsDOIibWGd8I7 leVbMwk2SRzs1/G/dknWdwgDnWnTuJQ= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1740736831; x=1772272831; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=yAxiyAJkmOi+qCLJ6YIryDvvFkW0bqE3LR6f2KKFmuI=; b=KGniG8TDRDGV4sRrBdQqAyM02ifEfGvbzA5mBqDKJ7vXJEb9iM8+2uQM napLGXLVxRTYQh9yTBR/V2WTaCK3Jruh95Rp2fPhgZSh+tQ9Sz6uhmUnW DBogST1dA16EHxQJ0JmCRognZ25z/nOmRG2kfpZ20d46s0wjunZ7FdZ2Q +qrVvUDtebzJ9XWYnO4Ur7p+D/PKui9oiJbH1Y1yr5TR+jAAV6pv5kw7f Igfmqom1lqdbAv7wq9WNiiK4ilqW0alM9BFn/54FXVVoIjl5x0nBPxUAO 6KN7iwPNL6AVzT/MK4vMLGHW1vVF+urty1WD8q7pyRqbGyzDzYcHlvIGj g==; X-CSE-ConnectionGUID: sHPEnrNpQK2dXkDRlQI7Dw== X-CSE-MsgGUID: 6o0SLgMrSnigMm5fZkGB6A== X-IronPort-AV: E=McAfee;i="6700,10204,11358"; a="40902605" X-IronPort-AV: E=Sophos;i="6.13,322,1732608000"; d="scan'208";a="40902605" Received: from orviesa006.jf.intel.com ([10.64.159.146]) by fmvoesa112.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Feb 2025 02:00:26 -0800 X-CSE-ConnectionGUID: JsOxxOoQS1Wcf0vCVk5nKw== X-CSE-MsgGUID: PNSJc6UrQ0OcddnrQDWl/Q== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.13,322,1732608000"; d="scan'208";a="117325703" Received: from jf5300-b11a338t.jf.intel.com ([10.242.51.115]) by orviesa006.jf.intel.com with ESMTP; 28 Feb 2025 02:00:26 -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, ying.huang@linux.alibaba.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 v7 04/15] crypto: iaa - Implement batch compression/decompression with request chaining. Date: Fri, 28 Feb 2025 02:00:13 -0800 Message-Id: <20250228100024.332528-5-kanchana.p.sridhar@intel.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20250228100024.332528-1-kanchana.p.sridhar@intel.com> References: <20250228100024.332528-1-kanchana.p.sridhar@intel.com> MIME-Version: 1.0 X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 982C818000E X-Rspam-User: X-Stat-Signature: 81jtgjp8etpxoxhh7tbxy99rue47e5it X-HE-Tag: 1740736830-479135 X-HE-Meta: U2FsdGVkX1/XMEpNJs0RNW35Vd8jRqI22zOgh14tRSZDt6h/cW5nWdKMZByE/T1sgGeYqhvJvd4eGXXMBMErVy7euIntxPmTsBgEFRA58LaiynLqnRI5Mn1SfEkb7vBXiiA9XJuGIrRI/wYnFpoUp2oKGCG117fUoBV6N/KSFypafWVouiEQqnvcqpMLmfS5jpP1o7V294IoZegTC/siBJ5pPDGEppQ9M+mvno74BMiW57E7PSTXlAZRy9gbv3iqHxhdauaa14a0xOJsksVfSjzOpmB5rJCpWAg/hU8R/gfJJloSug70fmUPAkBofJV9kEvpLi7CMJuYU1e0W9LSapd68o2kbcx88GzeGsTSX00HQKkV2KQnEOeuuz9KfitvdqyHvFJinU3wY6E59P3HRVWhPTSNSEELX7r7PLPFxrqN++KY57RMpjbzgJIm7D//r2cVuWjrs3N5njZT++aVDF70xCrvOJv/N4WyWhSnLeW9qteNRvNOlHmAZWndvr2kftxkUP264qa/suRdsHpUVPP8myEaPxGSxZTPUq1888IYtekezl2hBzf0wG4c2m61UzYKqV++ikw2JiRhrYeFQn7kFcoNp1quHLZhCaCD9GnVm9sz8AvrHbZavL5IC3UwfeZRkczZt3FpRwachKoFTB84pLYRTE5Z2cYWrs81OHfU6+Ra9PY7pPKY1S5HUlPX3Zf6XwflQeK3Z7nea8HMI1Vo6K1JM3ryaS9ES/F3bqxM5vZwrtqRiRQ9v3ayeIy5hRBRUoBgD+RPraHNM737uk0VYjzaywsq31RsENKO09ETYkd4AgQ3vKf+re+NbdAw5WuRLqrYpLiu989eL3iA6i5CsJDVg/hRHBrSWpYLq7WYWGNJnekQT7hILydfCJGGxjF5aKIJLC4UFenVi9/0NUPuiBxBSfvf1UaF14uVhJ7wiNMIX6DjhAYRQseqwchlyn6kqTP2ULF0fUz3QEs bi0bA9pe xTm1ziSqh8Fv2HjRooqUXtxqkwcj2ioyr1F2KRP/vTgu2XnfxrmdMv0brReuMNMsomaPVAeV/mQxYAGFrJhCP4JpjpncMe5JYDAsBJyMlDsWAVe3YPNFz7s+DkdyANjfjQ5xoiMliNDLIqYxtEwwz0wMGoThnWeP2+CFkUq5UDQLydJNPX8/XzG3JC+DZCMfmHjtIRrWgiFcXULJgX6QuO9uQyVyDU2wdyw7MAZP53d2TuxU= 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 provides the iaa_crypto driver implementation for the newly added crypto_acomp "get_batch_size()" interface which will be called when swap modules invoke crypto_acomp_batch_size() to query for the maximum batch size. This will return an iaa_driver specific constant, IAA_CRYPTO_MAX_BATCH_SIZE (set to 8U currently). This allows swap modules such as zswap/zram to allocate required batching resources and then invoke fully asynchronous batch parallel compression/decompression of pages on systems with Intel IAA, by setting up a request chain, and calling crypto_acomp_compress() or crypto_acomp_decompress() with the head request in the chain. This enables zswap compress batching code to be developed in a manner similar to the current single-page synchronous calls to crypto_acomp_compress() and crypto_acomp_decompress(), thereby, facilitating encapsulated and modular hand-off between the kernel zswap/zram code and the crypto_acomp layer. This patch also provides implementations of IAA batching with request chaining for both iaa_crypto sync modes: asynchronous/no-irq and fully synchronous. Since iaa_crypto supports the use of acomp request chaining, this patch also adds CRYPTO_ALG_REQ_CHAIN to the iaa_acomp_fixed_deflate algorithm's cra_flags. Suggested-by: Herbert Xu Signed-off-by: Kanchana P Sridhar --- drivers/crypto/intel/iaa/iaa_crypto.h | 9 + drivers/crypto/intel/iaa/iaa_crypto_main.c | 186 ++++++++++++++++++++- 2 files changed, 192 insertions(+), 3 deletions(-) diff --git a/drivers/crypto/intel/iaa/iaa_crypto.h b/drivers/crypto/intel/iaa/iaa_crypto.h index 56985e395263..45d94a646636 100644 --- a/drivers/crypto/intel/iaa/iaa_crypto.h +++ b/drivers/crypto/intel/iaa/iaa_crypto.h @@ -39,6 +39,15 @@ IAA_DECOMP_CHECK_FOR_EOB | \ IAA_DECOMP_STOP_ON_EOB) +/* + * The maximum compress/decompress batch size for IAA's implementation of + * batched compressions/decompressions. + * The IAA compression algorithms should provide the crypto_acomp + * get_batch_size() interface through a function that returns this + * constant. + */ +#define IAA_CRYPTO_MAX_BATCH_SIZE 8U + /* Representation of IAA workqueue */ struct iaa_wq { struct list_head list; diff --git a/drivers/crypto/intel/iaa/iaa_crypto_main.c b/drivers/crypto/intel/iaa/iaa_crypto_main.c index d7983ab3c34a..a9800b8f3575 100644 --- a/drivers/crypto/intel/iaa/iaa_crypto_main.c +++ b/drivers/crypto/intel/iaa/iaa_crypto_main.c @@ -1807,6 +1807,185 @@ static void compression_ctx_init(struct iaa_compression_ctx *ctx) ctx->use_irq = use_irq; } +static int iaa_comp_poll(struct acomp_req *req) +{ + struct idxd_desc *idxd_desc; + struct idxd_device *idxd; + struct iaa_wq *iaa_wq; + struct pci_dev *pdev; + struct device *dev; + struct idxd_wq *wq; + bool compress_op; + int ret; + + idxd_desc = req->base.data; + if (!idxd_desc) + return -EAGAIN; + + compress_op = (idxd_desc->iax_hw->opcode == IAX_OPCODE_COMPRESS); + wq = idxd_desc->wq; + iaa_wq = idxd_wq_get_private(wq); + idxd = iaa_wq->iaa_device->idxd; + pdev = idxd->pdev; + dev = &pdev->dev; + + ret = check_completion(dev, idxd_desc->iax_completion, true, true); + if (ret == -EAGAIN) + return ret; + if (ret) + goto out; + + req->dlen = idxd_desc->iax_completion->output_size; + + /* Update stats */ + if (compress_op) { + update_total_comp_bytes_out(req->dlen); + update_wq_comp_bytes(wq, req->dlen); + } else { + update_total_decomp_bytes_in(req->slen); + update_wq_decomp_bytes(wq, req->slen); + } + + if (iaa_verify_compress && (idxd_desc->iax_hw->opcode == IAX_OPCODE_COMPRESS)) { + struct crypto_tfm *tfm = req->base.tfm; + dma_addr_t src_addr, dst_addr; + u32 compression_crc; + + compression_crc = idxd_desc->iax_completion->crc; + + dma_sync_sg_for_device(dev, req->dst, 1, DMA_FROM_DEVICE); + dma_sync_sg_for_device(dev, req->src, 1, DMA_TO_DEVICE); + + src_addr = sg_dma_address(req->src); + dst_addr = sg_dma_address(req->dst); + + ret = iaa_compress_verify(tfm, req, wq, src_addr, req->slen, + dst_addr, &req->dlen, compression_crc); + } +out: + /* caller doesn't call crypto_wait_req, so no acomp_request_complete() */ + + dma_unmap_sg(dev, req->dst, sg_nents(req->dst), DMA_FROM_DEVICE); + dma_unmap_sg(dev, req->src, sg_nents(req->src), DMA_TO_DEVICE); + + idxd_free_desc(idxd_desc->wq, idxd_desc); + + dev_dbg(dev, "%s: returning ret=%d\n", __func__, ret); + + return ret; +} + +static unsigned int iaa_comp_get_batch_size(void) +{ + return IAA_CRYPTO_MAX_BATCH_SIZE; +} + +static void iaa_set_reqchain_poll( + struct acomp_req *req0, + bool set_flag) +{ + struct acomp_req *req; + + set_flag ? (req0->flags |= CRYPTO_ACOMP_REQ_POLL) : + (req0->flags &= ~CRYPTO_ACOMP_REQ_POLL); + + list_for_each_entry(req, &req0->base.list, base.list) + set_flag ? (req->flags |= CRYPTO_ACOMP_REQ_POLL) : + (req->flags &= ~CRYPTO_ACOMP_REQ_POLL); +} + +/** + * This API provides IAA compress batching functionality for use by swap + * modules. Batching is implemented using request chaining. + * + * @req: The head asynchronous compress request in the chain. + * + * Returns the compression error status (0 or -errno) of the last + * request that finishes. Caller should call acomp_request_err() + * for each request in the chain, to get its error status. + */ +static int iaa_comp_acompress_batch(struct acomp_req *req) +{ + bool async = (async_mode && !use_irq); + int err = 0; + + if (likely(async)) + iaa_set_reqchain_poll(req, true); + else + iaa_set_reqchain_poll(req, false); + + + if (likely(async)) + /* Process the request chain in parallel. */ + err = acomp_do_async_req_chain(req, iaa_comp_acompress, iaa_comp_poll); + else + /* Process the request chain in series. */ + err = acomp_do_req_chain(req, iaa_comp_acompress); + + /* + * For the same request chain to be usable by + * iaa_comp_acompress()/iaa_comp_adecompress() in synchronous mode, + * clear the CRYPTO_ACOMP_REQ_POLL bit on all acomp_reqs. + */ + iaa_set_reqchain_poll(req, false); + + return err; +} + +/** + * This API provides IAA decompress batching functionality for use by swap + * modules. Batching is implemented using request chaining. + * + * @req: The head asynchronous decompress request in the chain. + * + * Returns the decompression error status (0 or -errno) of the last + * request that finishes. Caller should call acomp_request_err() + * for each request in the chain, to get its error status. + */ +static int iaa_comp_adecompress_batch(struct acomp_req *req) +{ + bool async = (async_mode && !use_irq); + int err = 0; + + if (likely(async)) + iaa_set_reqchain_poll(req, true); + else + iaa_set_reqchain_poll(req, false); + + + if (likely(async)) + /* Process the request chain in parallel. */ + err = acomp_do_async_req_chain(req, iaa_comp_adecompress, iaa_comp_poll); + else + /* Process the request chain in series. */ + err = acomp_do_req_chain(req, iaa_comp_adecompress); + + /* + * For the same request chain to be usable by + * iaa_comp_acompress()/iaa_comp_adecompress() in synchronous mode, + * clear the CRYPTO_ACOMP_REQ_POLL bit on all acomp_reqs. + */ + iaa_set_reqchain_poll(req, false); + + return err; +} + +static int iaa_compress_main(struct acomp_req *req) +{ + if (acomp_is_reqchain(req)) + return iaa_comp_acompress_batch(req); + + return iaa_comp_acompress(req); +} + +static int iaa_decompress_main(struct acomp_req *req) +{ + if (acomp_is_reqchain(req)) + return iaa_comp_adecompress_batch(req); + + return iaa_comp_adecompress(req); +} + static int iaa_comp_init_fixed(struct crypto_acomp *acomp_tfm) { struct crypto_tfm *tfm = crypto_acomp_tfm(acomp_tfm); @@ -1829,13 +2008,14 @@ static void dst_free(struct scatterlist *sgl) static struct acomp_alg iaa_acomp_fixed_deflate = { .init = iaa_comp_init_fixed, - .compress = iaa_comp_acompress, - .decompress = iaa_comp_adecompress, + .compress = iaa_compress_main, + .decompress = iaa_decompress_main, .dst_free = dst_free, + .get_batch_size = iaa_comp_get_batch_size, .base = { .cra_name = "deflate", .cra_driver_name = "deflate-iaa", - .cra_flags = CRYPTO_ALG_ASYNC, + .cra_flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_REQ_CHAIN, .cra_ctxsize = sizeof(struct iaa_compression_ctx), .cra_module = THIS_MODULE, .cra_priority = IAA_ALG_PRIORITY, From patchwork Fri Feb 28 10:00:14 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: 13996095 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 CE573C19776 for ; Fri, 28 Feb 2025 10:00:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0742B280002; Fri, 28 Feb 2025 05:00:35 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id F3BEA280007; Fri, 28 Feb 2025 05:00:34 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CCE4F280009; Fri, 28 Feb 2025 05:00:34 -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 8FEA7280007 for ; Fri, 28 Feb 2025 05:00:34 -0500 (EST) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 3C39CB1587 for ; Fri, 28 Feb 2025 10:00:34 +0000 (UTC) X-FDA: 83168908788.22.D2D6773 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.18]) by imf08.hostedemail.com (Postfix) with ESMTP id C8B77160004 for ; Fri, 28 Feb 2025 10:00:31 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=UARSm8FE; spf=pass (imf08.hostedemail.com: domain of kanchana.p.sridhar@intel.com designates 192.198.163.18 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=1740736832; 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=2PQLRexpGdYT5Ec1XymA/Aac1BcRmmLQuyWuqoDnPs8=; b=b4B006EvuIDdcxjMnSLWblB1ugE9RPsMVI961+4d+rCaW4s+q4jHw2dD/IralD1eyIE0wY G5dGfDFApWvYkN6ROagGbIzy+o9GTbGv/42vcwKvsl6w51x08yXW1myAePaAV7tGBjC2At NdHuzughjnfDSRomf8t++IiuIthgpbo= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=UARSm8FE; spf=pass (imf08.hostedemail.com: domain of kanchana.p.sridhar@intel.com designates 192.198.163.18 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=1740736832; a=rsa-sha256; cv=none; b=3zg4MbiZE4pDfMR4VwOBMrRjWGbLghbroEz7tkesRAgnN0SDV/pKjkfu9VPFIUmRKDjusZ 53O4arErQOO97rBaCUqGFt25uebthDlURq7NDnSC9x/U1lnzDu2hEE3RQEP2Fo6OqDJvHv ESKgFMHy3X81AsG7EPU7+WXvOwjt5aE= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1740736832; x=1772272832; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=VRzCnyQyEfhyAHvAPp33ZwaL6nzqmuQ9xsZtH/mrvjQ=; b=UARSm8FE2/4m3wkpWq8xcISL/SFT0hJg4Nh+r0iEKbRpdKTcyw6nAWKx 7XVip2LU36NuwHRruOBKDa8IaI3va9qZSG1IbFfMVvQX9udJ/rtxt3XYe GoziUxvJcchHMGJYL4SKvorc1yfthflbLUpovclYBnclHIAgxdKVd1UT6 +CmRmTH4+hRCUaYroc1DLAdKtv0mv6VDJZMc1q5ozW4Xv25XzjK5AaEzm 7Mf5HH7YoWK4W09PYIgD0B/glb252k/6JSYIgAXqsPBXsNHyqrsfpUJeW YmTZzmOfuDe5wbVWj0G4/QhEyZpS6EUeggvxM6ejL/uQv7NmUTzgjo4VV A==; X-CSE-ConnectionGUID: nVRrpbWERjekMWC+diptCg== X-CSE-MsgGUID: Fpda5ki+Tp+wEsJ76Bm/hQ== X-IronPort-AV: E=McAfee;i="6700,10204,11358"; a="40902618" X-IronPort-AV: E=Sophos;i="6.13,322,1732608000"; d="scan'208";a="40902618" Received: from orviesa006.jf.intel.com ([10.64.159.146]) by fmvoesa112.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Feb 2025 02:00:26 -0800 X-CSE-ConnectionGUID: X0/nNyTWS0ykGFG2AYIKDA== X-CSE-MsgGUID: uQLjgsO6REGaSnplKdf3iw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.13,322,1732608000"; d="scan'208";a="117325713" Received: from jf5300-b11a338t.jf.intel.com ([10.242.51.115]) by orviesa006.jf.intel.com with ESMTP; 28 Feb 2025 02:00:26 -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, ying.huang@linux.alibaba.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 v7 05/15] crypto: iaa - Enable async mode and make it the default. Date: Fri, 28 Feb 2025 02:00:14 -0800 Message-Id: <20250228100024.332528-6-kanchana.p.sridhar@intel.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20250228100024.332528-1-kanchana.p.sridhar@intel.com> References: <20250228100024.332528-1-kanchana.p.sridhar@intel.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: C8B77160004 X-Stat-Signature: 5g8kfqrqp899maowqta1akzcozjbn5qa X-HE-Tag: 1740736831-415522 X-HE-Meta: U2FsdGVkX18KLsBY+wx9jUCuGKr7j45gvab+V+CGAm7xbUsqUHYswKEhEiHCfm8uN+D/ImrNd2po6qfhpm0OlE8UKRx0/SkDy3dr4o1qO5yB0gCNSmj7ibJQ/JmPDSQybvCm0yY7c+aPJ74wTHA+AJmWJzjWX2Jm+29rmhGSP+GyBp7zdrIndyZ73IRmZjxnZm9d0ZxHYqmE8sYEwRwZ935Mwgp8RgtNCca6X3FYZlPyfzQyBWr567dBgOVLav8SwHDjgnQA2R7CzuzrAmeeEmS06ExDLRX0VmTvCq/iqw6o1GlQAfH54DACbHkMHJHFrlxXG5sUEVhZj5quTe9hNAPdNk3270E/3Q6uHjgKTGivrCMk16Llogb3Fca5agbirttr53uiLT4ii+9OJa32joNM65DTTiK6WkA+kn/tUYdbkpjqiym+PRJxYMFjC2OOJYK45Z+f+ZQBBXnfxa5RMnCXUJyAEKJ0jwdXXIHzAPJSUAB7EHnupnx3Gm6Q8vw6jDpPD6Epb3ytg5poYaF0nk22AXZ5udyo9FkdMK1e/L55gBLsnKaiFCsyXHhjjHuGMx3Uq4UcnOM6KWV+mywpUL1PxkRpWaRytr0KhTDQN6JDeP2A1xF68cR0/SwzDDQNc41Nb5gd0gMhDrYFla0Ec0XLYExGKHnYcSe3yDx8AsvofBIkd/JISn3CwD3u3VBXFhHVxFnNntJ2E0oYmd8vlz5tnMwjXku8eO51OKKbTu+szHjj53DOfgblMyLFigNFlnivN/WAj5kZV3p/n5nPW5MO09ao9XEys5Lka9N6hxX/6oYmGupbUZ4o0hh0L6bBcrx+EFpVc97AbayqdfGOtisvlSz61j4mk4yTVQtUk9xQxm/Y+aeXoUfNxRuv1/zaZ9ecEjrqFx5e9Z4epu4QKdHYH/zIsGCOLi+9Rg8yslfeNftcpFupVUhvq7RsjIoBlyXLHbjqkx/cAr/zMiO kFB1k8tI IToQnujCcoG7KdaE/mMl3kAXr/YjEyB4pLMmJ2ouZxrO+osrXNxYwgtIgvkIRi3tUoui1PqQKxF851WoU38LMa4LgFKftw9ymwE3afjGVAzGf09uDuimQUD/Ogow/TDVTru/rCS97oVXltLabsCKcltrcNOc38ZkvgmYzNUs6jcFu+m5NzPUE/l08cw== 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 enables the 'async' sync_mode in the driver. Further, it sets the default sync_mode to 'async', which makes it easier for IAA hardware acceleration in the iaa_crypto driver to be loaded by default in the most efficient/recommended 'async' mode for parallel compressions/decompressions, namely, asynchronous submission of descriptors, followed by polling for job completions with or without request chaining. Earlier, the "sync" mode used to be the default. This way, anyone who wants to use IAA for zswap/zram can do so after building the kernel, and without having to go through these steps to use async mode: 1) disable all the IAA device/wq bindings that happen at boot time 2) rmmod iaa_crypto 3) modprobe iaa_crypto 4) echo async > /sys/bus/dsa/drivers/crypto/sync_mode 5) re-run initialization of the IAA devices and wqs Signed-off-by: Kanchana P Sridhar --- Documentation/driver-api/crypto/iaa/iaa-crypto.rst | 11 ++--------- drivers/crypto/intel/iaa/iaa_crypto_main.c | 4 ++-- 2 files changed, 4 insertions(+), 11 deletions(-) diff --git a/Documentation/driver-api/crypto/iaa/iaa-crypto.rst b/Documentation/driver-api/crypto/iaa/iaa-crypto.rst index 8e50b900d51c..782da5230fcd 100644 --- a/Documentation/driver-api/crypto/iaa/iaa-crypto.rst +++ b/Documentation/driver-api/crypto/iaa/iaa-crypto.rst @@ -272,7 +272,7 @@ The available attributes are: echo async_irq > /sys/bus/dsa/drivers/crypto/sync_mode Async mode without interrupts (caller must poll) can be enabled by - writing 'async' to it (please see Caveat):: + writing 'async' to it:: echo async > /sys/bus/dsa/drivers/crypto/sync_mode @@ -281,14 +281,7 @@ The available attributes are: echo sync > /sys/bus/dsa/drivers/crypto/sync_mode - The default mode is 'sync'. - - Caveat: since the only mechanism that iaa_crypto currently implements - for async polling without interrupts is via the 'sync' mode as - described earlier, writing 'async' to - '/sys/bus/dsa/drivers/crypto/sync_mode' will internally enable the - 'sync' mode. This is to ensure correct iaa_crypto behavior until true - async polling without interrupts is enabled in iaa_crypto. + The default mode is 'async'. .. _iaa_default_config: diff --git a/drivers/crypto/intel/iaa/iaa_crypto_main.c b/drivers/crypto/intel/iaa/iaa_crypto_main.c index a9800b8f3575..4dac4852c113 100644 --- a/drivers/crypto/intel/iaa/iaa_crypto_main.c +++ b/drivers/crypto/intel/iaa/iaa_crypto_main.c @@ -153,7 +153,7 @@ static DRIVER_ATTR_RW(verify_compress); */ /* Use async mode */ -static bool async_mode; +static bool async_mode = true; /* Use interrupts */ static bool use_irq; @@ -173,7 +173,7 @@ static int set_iaa_sync_mode(const char *name) async_mode = false; use_irq = false; } else if (sysfs_streq(name, "async")) { - async_mode = false; + async_mode = true; use_irq = false; } else if (sysfs_streq(name, "async_irq")) { async_mode = true; From patchwork Fri Feb 28 10:00:15 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: 13996097 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 F1F90C282C5 for ; Fri, 28 Feb 2025 10:01:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EC94928000C; Fri, 28 Feb 2025 05:00:37 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id E78E5280007; Fri, 28 Feb 2025 05:00:37 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D4B9E28000C; Fri, 28 Feb 2025 05:00:37 -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 AF752280007 for ; Fri, 28 Feb 2025 05:00:37 -0500 (EST) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 642064C076 for ; Fri, 28 Feb 2025 10:00:37 +0000 (UTC) X-FDA: 83168908914.02.2A114D4 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.18]) by imf24.hostedemail.com (Postfix) with ESMTP id EEBE4180013 for ; Fri, 28 Feb 2025 10:00:33 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=fxCi4grN; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf24.hostedemail.com: domain of kanchana.p.sridhar@intel.com designates 192.198.163.18 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=1740736834; 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=+X9c7Zr+xYOcQEk2MjjRAl3qcylNM61at0Akh92rUuY=; b=kENUtI97KGYHsvdf0MLMaB1Lcslmk6dAUGLhlcKZ3ysu+SPs64oFP/0DLpUeNHnWUoN0Cf GEhrR/myGMTq/FCdskQIV9sFReTvkS8X7a6+xVW+Jhzk5qeZBNJ8pe7bVkZtFCsmJqum6K 9LQs/3p+atG9WEBqsv78Jw6P+iDN1dY= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=fxCi4grN; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf24.hostedemail.com: domain of kanchana.p.sridhar@intel.com designates 192.198.163.18 as permitted sender) smtp.mailfrom=kanchana.p.sridhar@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1740736834; a=rsa-sha256; cv=none; b=JG2Fl50OMG2r6hvtECln9F2DohfzeSzJX9HsUorp12wRc7+oa825E4nOK4F4l/WzxOsx1t Rfn/mPrYlkE4dDg6VlDDio3giTglY6Glq47duziQywqz9cOR3F4vOabipQWfppUWxDNPIe 79rsUKI8hsdgwP0dYX5S9XqH9UG9KGs= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1740736834; x=1772272834; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=yHP5Zb8+z92UaMlcfYAxfjtnW5rOspDIJ08VjR15a3A=; b=fxCi4grNM2kgXA2s2WP2QJ4d9FA9M9PfXETKXgJoTgnVy2lDAfmCY5Fi C9wQhwa1DVpVVfViceKXFlK8z+e8R1N2vqjmqrPZZl/EmuQ3HqnR5sXyF P+zdxpsjkDQFCpVj2j3ALHeEjZ3WtCMxKK58lGfyQmt3OSicrDoOVuydr NNhinkwkq+FHBc4zJuOCRUB9V7tbbj2oRiaYmM7m/QA6HyV9dBguDM2+y WFtV0TMrsVqlf7+2XbN5XLE5XYSXNyrnbTcfuVeFjA/kth3pW2v7PXRWO 5JGpsQp2rT2CLNSyO/ZVwQ/9Nb+yyssR0H1ndu3TJhhkvEEIogXocbNmH A==; X-CSE-ConnectionGUID: 5/g1781dTVOeFU7I1cW6Ag== X-CSE-MsgGUID: 7l4yrDwdT5eKjRpg6NgfmQ== X-IronPort-AV: E=McAfee;i="6700,10204,11358"; a="40902631" X-IronPort-AV: E=Sophos;i="6.13,322,1732608000"; d="scan'208";a="40902631" Received: from orviesa006.jf.intel.com ([10.64.159.146]) by fmvoesa112.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Feb 2025 02:00:27 -0800 X-CSE-ConnectionGUID: 1Lk0WXSiQfKjC9odq4aZtg== X-CSE-MsgGUID: tjEWDpYHRb+PYAqFYWOxZg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.13,322,1732608000"; d="scan'208";a="117325717" Received: from jf5300-b11a338t.jf.intel.com ([10.242.51.115]) by orviesa006.jf.intel.com with ESMTP; 28 Feb 2025 02:00:27 -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, ying.huang@linux.alibaba.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 v7 06/15] crypto: iaa - Disable iaa_verify_compress by default. Date: Fri, 28 Feb 2025 02:00:15 -0800 Message-Id: <20250228100024.332528-7-kanchana.p.sridhar@intel.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20250228100024.332528-1-kanchana.p.sridhar@intel.com> References: <20250228100024.332528-1-kanchana.p.sridhar@intel.com> MIME-Version: 1.0 X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: EEBE4180013 X-Rspam-User: X-Stat-Signature: tmdr51rekygtdboz7ym3pzz9jk37i4qx X-HE-Tag: 1740736833-775033 X-HE-Meta: U2FsdGVkX1+GLDzFRfPP/RvGmRvDAIjhuiWBlyiUMPU0wtMDYZSlxUIsgBclt2Hn1TGmEM/p2AoceBvOxXMF/B1teLkAka6d8pujT5rPJnvYRMjjKVpskDcYnz3tYylvmWx7KzPpVxybntsufwkHHqGV5boZ0ANycYkU82Fcg1A6RSM1TMIfosY4cI2R2UprhNGyjZvaFy+0e5jhOfO+NUpPujDp796vIwUrvF1Ir1S4cQluijjH4H0TUyaEfvJwJh7a9E4l2HjPqIHZhoEGH3ET2+PzFhZP3vaEyUpAK5vYnNHslyQ4WVU+qKIGcmn0nNHgj7VncpoOIv7si/wv2S1VKNac1vVoxj465wQLv3kYwxQD2TKlLJcL2eJC9nHteaMxNiV2hgEq6VshgWvP/bd3dLpJRxqZH+GTOa89G5gKHeo/Ip8YQbzVRwr32s49demiZhYq5fpiKHPylfmVVWh/zOT+vmSU9pY2ON/XQqsYTML5oJSnKtMTZwWpaW7OBdsrBCuE5K/eXRewWdyxMFtxh41SVQorZWhbuTr7Yp3eWIa3q/0S5r+W5xsKIw5eCoSLWKN4xt0tPzY4OFkN6KCD2MJ9MT2HhU5gzlScyRGlugV6cK65MU8OaioxUOTnff1GJSYR8zZdn2LqXfVv7x7XV9WueCbGpGG7Tn1T2Nxnj2fMaNudRJgJCGU7gEU6BmvF5a81/S0Y9kQ6yUHec8Aquoj7DP0YkFrTGajffyxrQpgf+sgKFYhFLCsUGk6wJzkr/e8hY2doyKiFz4ialrHIg1Da/o7+JYyG99Ugu9A7INpu8gkUS1Bli8OInS1z9rFjO3qKGXvoyTjNUOupMeLjaDEr5I1YXUChk4cpgxDDDv+92DPVWz1wkcCFjbPGYMr7wPt1rby92hSUEDoU+pSyz/iWd0CTGfE3w0gplDV6nWhK84KWDv2h++wulPraNTanKnANEPHu3XiJ3gj BnK2QLhE ufm0Aq6z+43GcKjBsRlTT9H91ut234ZuqVC8FaiHJZmYO6TiDB1M96WcerkF+KidZFVha62GSVStzF4BCMPocNp5jn1Rr5aXJxJn7w1RBthg6+HVgZDMioAqBcF8uBrN6mVpabibiE8d/vOUyWjOjj9p6yYfQ0PjiV2QPKaMXqS1b3+bb7WYZ6cu6HeX6F6MyZ9GroQh12yfIgg41GhOli85626yk9Gj2y+GZ 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 makes it easier for IAA hardware acceleration in the iaa_crypto driver to be loaded by default with "iaa_verify_compress" disabled, to facilitate performance comparisons with software compressors (which also do not run compress verification by default). Earlier, iaa_crypto compress verification used to be enabled by default. With this patch, if users want to enable compress verification, they can do so with these steps: 1) disable all the IAA device/wq bindings that happen at boot time 2) rmmod iaa_crypto 3) modprobe iaa_crypto 4) echo 1 > /sys/bus/dsa/drivers/crypto/verify_compress 5) re-run initialization of the IAA devices and wqs Signed-off-by: Kanchana P Sridhar --- drivers/crypto/intel/iaa/iaa_crypto_main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/crypto/intel/iaa/iaa_crypto_main.c b/drivers/crypto/intel/iaa/iaa_crypto_main.c index 4dac4852c113..5038fd7ced02 100644 --- a/drivers/crypto/intel/iaa/iaa_crypto_main.c +++ b/drivers/crypto/intel/iaa/iaa_crypto_main.c @@ -94,7 +94,7 @@ static bool iaa_crypto_enabled; static bool iaa_crypto_registered; /* Verify results of IAA compress or not */ -static bool iaa_verify_compress = true; +static bool iaa_verify_compress = false; static ssize_t verify_compress_show(struct device_driver *driver, char *buf) { From patchwork Fri Feb 28 10:00:16 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: 13996098 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 5176DC282C5 for ; Fri, 28 Feb 2025 10:01:07 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 970E828000D; Fri, 28 Feb 2025 05:00:38 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 92140280007; Fri, 28 Feb 2025 05:00:38 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 774AD28000D; Fri, 28 Feb 2025 05:00:38 -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 52457280007 for ; Fri, 28 Feb 2025 05:00:38 -0500 (EST) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id EC982806BE for ; Fri, 28 Feb 2025 10:00:37 +0000 (UTC) X-FDA: 83168908914.29.0390659 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.18]) by imf27.hostedemail.com (Postfix) with ESMTP id 0515F4000A for ; Fri, 28 Feb 2025 10:00:33 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=iqzCMMIq; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf27.hostedemail.com: domain of kanchana.p.sridhar@intel.com designates 192.198.163.18 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=1740736834; 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=7THpAo6jTwozO6S7/CoYt0LufUyvsa1fijnlgCmBAFA=; b=gAQJc3sa1ZKWtqU+ynYGrLm2sqrrhZwub3BXNUJWKiyydweZlVD/qHhHzIqftT/MCow9bP ea3Nt9b85wXDPcLOhBT2XoL69VAvrJZGKFjTLf9jYTUImsfpufnCORvij/Vrwu0f4Sj54+ 3887vZjFtkUxsc0od7d67Lij9DAeFgQ= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=iqzCMMIq; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf27.hostedemail.com: domain of kanchana.p.sridhar@intel.com designates 192.198.163.18 as permitted sender) smtp.mailfrom=kanchana.p.sridhar@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1740736834; a=rsa-sha256; cv=none; b=mubEJHzdGFr8RJdA5k1E7Q3SZOwSpbpzpschDn9JCK57QgbE5gLkQ1E6IxkN06BD8SbLaS H/T5fCB5RxBz5ePQbdLGbERuQF28US2UovniqE7De871/g7RFGRsITeRJWObHOahQb9NNp xoZz0AENPvSNbs5MWEB7aMyCu/dNcyQ= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1740736834; x=1772272834; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=U0820r8j/yZHUeGSU9DGtaJk7AzxMBMPmbEhUmO82nI=; b=iqzCMMIq+SNnSaAdy0nar6Mz7RqG3QiOCvWke8JNupZtO4pp9OWgBNhn p0XUvBRlp0l6uMd5x6wJkxD18YvqvcFMI2ELB+3kbyT1k93M1kNgItGtT atslU4T3NCNmjCc4kJw8sRA4iV3vAKHTq+z1pIE13L9H8tAvXVR1aNwnM pEqa0U+Vkh2qBPVXpSDVUjAkrWJQrNkACSHc25UnUQvYbJ9vyymETrj1v SEyP2xSWEoZGMqi5sVottgfoBcHMvEQXJnRr8j4au7I6GIp0TIggUlOHQ RrRQ71uOGN7s6yzET6SBFjO+6GMC47WFbmHJ4u/GZHiISO1DG57R2xY+7 w==; X-CSE-ConnectionGUID: rT8QVzWvT32NlhN9JzlHIw== X-CSE-MsgGUID: ZXDu4l7wSU2Yboh9t2VExg== X-IronPort-AV: E=McAfee;i="6700,10204,11358"; a="40902643" X-IronPort-AV: E=Sophos;i="6.13,322,1732608000"; d="scan'208";a="40902643" Received: from orviesa006.jf.intel.com ([10.64.159.146]) by fmvoesa112.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Feb 2025 02:00:27 -0800 X-CSE-ConnectionGUID: 2SJO+xqmQBSOQbudphkAHg== X-CSE-MsgGUID: JfgU83olTK2GhXgyFyZQag== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.13,322,1732608000"; d="scan'208";a="117325722" Received: from jf5300-b11a338t.jf.intel.com ([10.242.51.115]) by orviesa006.jf.intel.com with ESMTP; 28 Feb 2025 02:00:27 -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, ying.huang@linux.alibaba.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 v7 07/15] crypto: iaa - Re-organize the iaa_crypto driver code. Date: Fri, 28 Feb 2025 02:00:16 -0800 Message-Id: <20250228100024.332528-8-kanchana.p.sridhar@intel.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20250228100024.332528-1-kanchana.p.sridhar@intel.com> References: <20250228100024.332528-1-kanchana.p.sridhar@intel.com> MIME-Version: 1.0 X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 0515F4000A X-Rspam-User: X-Stat-Signature: yp3irgjdc6or5oodgi3zrid4i1pg5c4i X-HE-Tag: 1740736833-392912 X-HE-Meta: U2FsdGVkX19+RZmKoRDVXIfCHwHCP5R+wsCQ5Uvb7oi/LeH9/DwolFKPUnVm5WBk0WP5PvUqkjbBgR6wVTGYr1LZCaJ2Nxo5F1mi3sdNzuLNON/iNmStKRME3g3vkFcaOq4VUiNKiXFuIOsTDXzUwRgzErDdL+KGBG0Vpjmuzu2sMNL1uGokpryZMocnzAcxr4pIA3blwzZhlmoD1b5FYQJNaHxHzt+kEk00xshtGYpuuJk9RMZ/Al2nu0WNCCHFZJOrwNtf29HuERGvJucTONcpJ/vD4d7iEwzJQE9EK5u2t5pkRvA/lhI5BNcUJCJmoRE90TCupm2PrO3O4ZX3XD4Guzn5RZqOFBWETklaptEWrv3RAkwR70Dx574lQESHaDHMxTHSQcrqQ82Tx/YW8OY543T+1ZV9/G+8TtTHS0edhZdrHVa16xuyF6jU7oXsE/A7gNknOBPsB+L4d9Y3GcsYIXKNJHzEsNwWM6k1QUlIIJm4AkigxgbTiOSew6KX3rUgydHYwZ56B8S/y24mhElqPFYGOHMIaOrDOeIImOKPCr0YLiEqpgAOlwFGSqNmLBjyW9KfQB7rCUArY33M34EiNGerD85zf1po6oXTCWzjHY7YhbscgGMu6MNsIRi56DwM6K5rE2XJqc+CYuXL3A4WtHAdSVJWpHNkBlRJyv7vhsQHktY8GQ25uBC1Bcm0WMvyUQ/r29F8Ank484D9ifKeQmHvZdAusp5o1gf6UIgCJfsPgEE2/PAqTh1XJgRimGiHuj5AE/0h4CgOiWULhPvjp6HLpkrrmXhKi2u6cd1OcxcOeNA1+HRgU3MrdOKUMNz/CPuD1tQZlteC4avnuxyrCoLApynJqouhk8bwFHS9sKChzUFSJJoYD4DW4BPe5V/HPJE9BRVBD35iKMH3azr5lyllm9RWG/WWkM3UTnJHlub+ABHtJl3mxDpxjAZXDPlLPn3GlyaT69ZWRaH ORLk2j/F Ik4d69vp81ABvEhAPIt2tMQgTu/ab/jHZL9yZO8m+g4D10QusDOgmZmXFMiZ1PV7iz2qVtpMhXVAsWKXl+CVErrYrNNMrYCziiZRuHdHrewg5uoSSYjpNZ8aBp04BXFHkUWnK4hojlHBDvNBAaCSlpOtbLGTPLiLYIHvPuQErU9KUxFJR+cSTkVVaqJpKLKtHUOrhJCA1+19C+ubNKtbDkq395z2MNqfm/xvBoEjQu9ZsCuU= 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 merely reorganizes the code in iaa_crypto_main.c, so that the functions are consolidated into logically related sub-sections of code. This is expected to make the code more maintainable and for it to be easier to replace functional layers and/or add new features. Signed-off-by: Kanchana P Sridhar --- drivers/crypto/intel/iaa/iaa_crypto_main.c | 540 +++++++++++---------- 1 file changed, 275 insertions(+), 265 deletions(-) diff --git a/drivers/crypto/intel/iaa/iaa_crypto_main.c b/drivers/crypto/intel/iaa/iaa_crypto_main.c index 5038fd7ced02..abaee160e5ec 100644 --- a/drivers/crypto/intel/iaa/iaa_crypto_main.c +++ b/drivers/crypto/intel/iaa/iaa_crypto_main.c @@ -24,6 +24,9 @@ #define IAA_ALG_PRIORITY 300 +/************************************** + * Driver internal global variables. + **************************************/ /* number of iaa instances probed */ static unsigned int nr_iaa; static unsigned int nr_cpus; @@ -36,55 +39,46 @@ static unsigned int cpus_per_iaa; static struct crypto_comp *deflate_generic_tfm; /* Per-cpu lookup table for balanced wqs */ -static struct wq_table_entry __percpu *wq_table; +static struct wq_table_entry __percpu *wq_table = NULL; -static struct idxd_wq *wq_table_next_wq(int cpu) -{ - struct wq_table_entry *entry = per_cpu_ptr(wq_table, cpu); - - if (++entry->cur_wq >= entry->n_wqs) - entry->cur_wq = 0; - - if (!entry->wqs[entry->cur_wq]) - return NULL; - - pr_debug("%s: returning wq at idx %d (iaa wq %d.%d) from cpu %d\n", __func__, - entry->cur_wq, entry->wqs[entry->cur_wq]->idxd->id, - entry->wqs[entry->cur_wq]->id, cpu); - - return entry->wqs[entry->cur_wq]; -} - -static void wq_table_add(int cpu, struct idxd_wq *wq) -{ - struct wq_table_entry *entry = per_cpu_ptr(wq_table, cpu); - - if (WARN_ON(entry->n_wqs == entry->max_wqs)) - return; - - entry->wqs[entry->n_wqs++] = wq; - - pr_debug("%s: added iaa wq %d.%d to idx %d of cpu %d\n", __func__, - entry->wqs[entry->n_wqs - 1]->idxd->id, - entry->wqs[entry->n_wqs - 1]->id, entry->n_wqs - 1, cpu); -} - -static void wq_table_free_entry(int cpu) -{ - struct wq_table_entry *entry = per_cpu_ptr(wq_table, cpu); +/* Verify results of IAA compress or not */ +static bool iaa_verify_compress = false; - kfree(entry->wqs); - memset(entry, 0, sizeof(*entry)); -} +/* + * The iaa crypto driver supports three 'sync' methods determining how + * compressions and decompressions are performed: + * + * - sync: the compression or decompression completes before + * returning. This is the mode used by the async crypto + * interface when the sync mode is set to 'sync' and by + * the sync crypto interface regardless of setting. + * + * - async: the compression or decompression is submitted and returns + * immediately. Completion interrupts are not used so + * the caller is responsible for polling the descriptor + * for completion. This mode is applicable to only the + * async crypto interface and is ignored for anything + * else. + * + * - async_irq: the compression or decompression is submitted and + * returns immediately. Completion interrupts are + * enabled so the caller can wait for the completion and + * yield to other threads. When the compression or + * decompression completes, the completion is signaled + * and the caller awakened. This mode is applicable to + * only the async crypto interface and is ignored for + * anything else. + * + * These modes can be set using the iaa_crypto sync_mode driver + * attribute. + */ -static void wq_table_clear_entry(int cpu) -{ - struct wq_table_entry *entry = per_cpu_ptr(wq_table, cpu); +/* Use async mode */ +static bool async_mode = true; +/* Use interrupts */ +static bool use_irq; - entry->n_wqs = 0; - entry->cur_wq = 0; - memset(entry->wqs, 0, entry->max_wqs * sizeof(struct idxd_wq *)); -} +static struct iaa_compression_mode *iaa_compression_modes[IAA_COMP_MODES_MAX]; LIST_HEAD(iaa_devices); DEFINE_MUTEX(iaa_devices_lock); @@ -93,9 +87,9 @@ DEFINE_MUTEX(iaa_devices_lock); static bool iaa_crypto_enabled; static bool iaa_crypto_registered; -/* Verify results of IAA compress or not */ -static bool iaa_verify_compress = false; - +/************************************************** + * Driver attributes along with get/set functions. + **************************************************/ static ssize_t verify_compress_show(struct device_driver *driver, char *buf) { return sprintf(buf, "%d\n", iaa_verify_compress); @@ -123,40 +117,6 @@ static ssize_t verify_compress_store(struct device_driver *driver, } static DRIVER_ATTR_RW(verify_compress); -/* - * The iaa crypto driver supports three 'sync' methods determining how - * compressions and decompressions are performed: - * - * - sync: the compression or decompression completes before - * returning. This is the mode used by the async crypto - * interface when the sync mode is set to 'sync' and by - * the sync crypto interface regardless of setting. - * - * - async: the compression or decompression is submitted and returns - * immediately. Completion interrupts are not used so - * the caller is responsible for polling the descriptor - * for completion. This mode is applicable to only the - * async crypto interface and is ignored for anything - * else. - * - * - async_irq: the compression or decompression is submitted and - * returns immediately. Completion interrupts are - * enabled so the caller can wait for the completion and - * yield to other threads. When the compression or - * decompression completes, the completion is signaled - * and the caller awakened. This mode is applicable to - * only the async crypto interface and is ignored for - * anything else. - * - * These modes can be set using the iaa_crypto sync_mode driver - * attribute. - */ - -/* Use async mode */ -static bool async_mode = true; -/* Use interrupts */ -static bool use_irq; - /** * set_iaa_sync_mode - Set IAA sync mode * @name: The name of the sync mode @@ -219,8 +179,9 @@ static ssize_t sync_mode_store(struct device_driver *driver, } static DRIVER_ATTR_RW(sync_mode); -static struct iaa_compression_mode *iaa_compression_modes[IAA_COMP_MODES_MAX]; - +/**************************** + * Driver compression modes. + ****************************/ static int find_empty_iaa_compression_mode(void) { int i = -EINVAL; @@ -411,11 +372,6 @@ static void free_device_compression_mode(struct iaa_device *iaa_device, IDXD_OP_FLAG_WR_SRC2_AECS_COMP | \ IDXD_OP_FLAG_AECS_RW_TGLS) -static int check_completion(struct device *dev, - struct iax_completion_record *comp, - bool compress, - bool only_once); - static int init_device_compression_mode(struct iaa_device *iaa_device, struct iaa_compression_mode *mode, int idx, struct idxd_wq *wq) @@ -502,6 +458,10 @@ static void remove_device_compression_modes(struct iaa_device *iaa_device) } } +/*********************************************************** + * Functions for use in crypto probe and remove interfaces: + * allocate/init/query/deallocate devices/wqs. + ***********************************************************/ static struct iaa_device *iaa_device_alloc(void) { struct iaa_device *iaa_device; @@ -614,16 +574,6 @@ static void del_iaa_wq(struct iaa_device *iaa_device, struct idxd_wq *wq) } } -static void clear_wq_table(void) -{ - int cpu; - - for (cpu = 0; cpu < nr_cpus; cpu++) - wq_table_clear_entry(cpu); - - pr_debug("cleared wq table\n"); -} - static void free_iaa_device(struct iaa_device *iaa_device) { if (!iaa_device) @@ -704,43 +654,6 @@ static int iaa_wq_put(struct idxd_wq *wq) return ret; } -static void free_wq_table(void) -{ - int cpu; - - for (cpu = 0; cpu < nr_cpus; cpu++) - wq_table_free_entry(cpu); - - free_percpu(wq_table); - - pr_debug("freed wq table\n"); -} - -static int alloc_wq_table(int max_wqs) -{ - struct wq_table_entry *entry; - int cpu; - - wq_table = alloc_percpu(struct wq_table_entry); - if (!wq_table) - return -ENOMEM; - - for (cpu = 0; cpu < nr_cpus; cpu++) { - entry = per_cpu_ptr(wq_table, cpu); - entry->wqs = kcalloc(max_wqs, sizeof(struct wq *), GFP_KERNEL); - if (!entry->wqs) { - free_wq_table(); - return -ENOMEM; - } - - entry->max_wqs = max_wqs; - } - - pr_debug("initialized wq table\n"); - - return 0; -} - static int save_iaa_wq(struct idxd_wq *wq) { struct iaa_device *iaa_device, *found = NULL; @@ -829,6 +742,87 @@ static void remove_iaa_wq(struct idxd_wq *wq) cpus_per_iaa = 1; } +/*************************************************************** + * Mapping IAA devices and wqs to cores with per-cpu wq_tables. + ***************************************************************/ +static void wq_table_free_entry(int cpu) +{ + struct wq_table_entry *entry = per_cpu_ptr(wq_table, cpu); + + kfree(entry->wqs); + memset(entry, 0, sizeof(*entry)); +} + +static void wq_table_clear_entry(int cpu) +{ + struct wq_table_entry *entry = per_cpu_ptr(wq_table, cpu); + + entry->n_wqs = 0; + entry->cur_wq = 0; + memset(entry->wqs, 0, entry->max_wqs * sizeof(struct idxd_wq *)); +} + +static void clear_wq_table(void) +{ + int cpu; + + for (cpu = 0; cpu < nr_cpus; cpu++) + wq_table_clear_entry(cpu); + + pr_debug("cleared wq table\n"); +} + +static void free_wq_table(void) +{ + int cpu; + + for (cpu = 0; cpu < nr_cpus; cpu++) + wq_table_free_entry(cpu); + + free_percpu(wq_table); + + pr_debug("freed wq table\n"); +} + +static int alloc_wq_table(int max_wqs) +{ + struct wq_table_entry *entry; + int cpu; + + wq_table = alloc_percpu(struct wq_table_entry); + if (!wq_table) + return -ENOMEM; + + for (cpu = 0; cpu < nr_cpus; cpu++) { + entry = per_cpu_ptr(wq_table, cpu); + entry->wqs = kcalloc(max_wqs, sizeof(struct wq *), GFP_KERNEL); + if (!entry->wqs) { + free_wq_table(); + return -ENOMEM; + } + + entry->max_wqs = max_wqs; + } + + pr_debug("initialized wq table\n"); + + return 0; +} + +static void wq_table_add(int cpu, struct idxd_wq *wq) +{ + struct wq_table_entry *entry = per_cpu_ptr(wq_table, cpu); + + if (WARN_ON(entry->n_wqs == entry->max_wqs)) + return; + + entry->wqs[entry->n_wqs++] = wq; + + pr_debug("%s: added iaa wq %d.%d to idx %d of cpu %d\n", __func__, + entry->wqs[entry->n_wqs - 1]->idxd->id, + entry->wqs[entry->n_wqs - 1]->id, entry->n_wqs - 1, cpu); +} + static int wq_table_add_wqs(int iaa, int cpu) { struct iaa_device *iaa_device, *found_device = NULL; @@ -939,6 +933,29 @@ static void rebalance_wq_table(void) } } +/*************************************************************** + * Assign work-queues for driver ops using per-cpu wq_tables. + ***************************************************************/ +static struct idxd_wq *wq_table_next_wq(int cpu) +{ + struct wq_table_entry *entry = per_cpu_ptr(wq_table, cpu); + + if (++entry->cur_wq >= entry->n_wqs) + entry->cur_wq = 0; + + if (!entry->wqs[entry->cur_wq]) + return NULL; + + pr_debug("%s: returning wq at idx %d (iaa wq %d.%d) from cpu %d\n", __func__, + entry->cur_wq, entry->wqs[entry->cur_wq]->idxd->id, + entry->wqs[entry->cur_wq]->id, cpu); + + return entry->wqs[entry->cur_wq]; +} + +/************************************************* + * Core iaa_crypto compress/decompress functions. + *************************************************/ static inline int check_completion(struct device *dev, struct iax_completion_record *comp, bool compress, @@ -1020,13 +1037,130 @@ static int deflate_generic_decompress(struct acomp_req *req) static int iaa_remap_for_verify(struct device *dev, struct iaa_wq *iaa_wq, struct acomp_req *req, - dma_addr_t *src_addr, dma_addr_t *dst_addr); + dma_addr_t *src_addr, dma_addr_t *dst_addr) +{ + int ret = 0; + int nr_sgs; + + dma_unmap_sg(dev, req->dst, sg_nents(req->dst), DMA_FROM_DEVICE); + dma_unmap_sg(dev, req->src, sg_nents(req->src), DMA_TO_DEVICE); + + nr_sgs = dma_map_sg(dev, req->src, sg_nents(req->src), DMA_FROM_DEVICE); + if (nr_sgs <= 0 || nr_sgs > 1) { + dev_dbg(dev, "verify: couldn't map src sg for iaa device %d," + " wq %d: ret=%d\n", iaa_wq->iaa_device->idxd->id, + iaa_wq->wq->id, ret); + ret = -EIO; + goto out; + } + *src_addr = sg_dma_address(req->src); + dev_dbg(dev, "verify: dma_map_sg, src_addr %llx, nr_sgs %d, req->src %p," + " req->slen %d, sg_dma_len(sg) %d\n", *src_addr, nr_sgs, + req->src, req->slen, sg_dma_len(req->src)); + + nr_sgs = dma_map_sg(dev, req->dst, sg_nents(req->dst), DMA_TO_DEVICE); + if (nr_sgs <= 0 || nr_sgs > 1) { + dev_dbg(dev, "verify: couldn't map dst sg for iaa device %d," + " wq %d: ret=%d\n", iaa_wq->iaa_device->idxd->id, + iaa_wq->wq->id, ret); + ret = -EIO; + dma_unmap_sg(dev, req->src, sg_nents(req->src), DMA_FROM_DEVICE); + goto out; + } + *dst_addr = sg_dma_address(req->dst); + dev_dbg(dev, "verify: dma_map_sg, dst_addr %llx, nr_sgs %d, req->dst %p," + " req->dlen %d, sg_dma_len(sg) %d\n", *dst_addr, nr_sgs, + req->dst, req->dlen, sg_dma_len(req->dst)); +out: + return ret; +} static int iaa_compress_verify(struct crypto_tfm *tfm, struct acomp_req *req, struct idxd_wq *wq, dma_addr_t src_addr, unsigned int slen, dma_addr_t dst_addr, unsigned int *dlen, - u32 compression_crc); + u32 compression_crc) +{ + struct iaa_device_compression_mode *active_compression_mode; + struct iaa_compression_ctx *ctx = crypto_tfm_ctx(tfm); + struct iaa_device *iaa_device; + struct idxd_desc *idxd_desc; + struct iax_hw_desc *desc; + struct idxd_device *idxd; + struct iaa_wq *iaa_wq; + struct pci_dev *pdev; + struct device *dev; + int ret = 0; + + iaa_wq = idxd_wq_get_private(wq); + iaa_device = iaa_wq->iaa_device; + idxd = iaa_device->idxd; + pdev = idxd->pdev; + dev = &pdev->dev; + + active_compression_mode = get_iaa_device_compression_mode(iaa_device, ctx->mode); + + idxd_desc = idxd_alloc_desc(wq, IDXD_OP_BLOCK); + if (IS_ERR(idxd_desc)) { + dev_dbg(dev, "idxd descriptor allocation failed\n"); + dev_dbg(dev, "iaa compress failed: ret=%ld\n", + PTR_ERR(idxd_desc)); + return PTR_ERR(idxd_desc); + } + desc = idxd_desc->iax_hw; + + /* Verify (optional) - decompress and check crc, suppress dest write */ + + desc->flags = IDXD_OP_FLAG_CRAV | IDXD_OP_FLAG_RCR | IDXD_OP_FLAG_CC; + desc->opcode = IAX_OPCODE_DECOMPRESS; + desc->decompr_flags = IAA_DECOMP_FLAGS | IAA_DECOMP_SUPPRESS_OUTPUT; + desc->priv = 0; + + desc->src1_addr = (u64)dst_addr; + desc->src1_size = *dlen; + desc->dst_addr = (u64)src_addr; + desc->max_dst_size = slen; + desc->completion_addr = idxd_desc->compl_dma; + + dev_dbg(dev, "(verify) compression mode %s," + " desc->src1_addr %llx, desc->src1_size %d," + " desc->dst_addr %llx, desc->max_dst_size %d," + " desc->src2_addr %llx, desc->src2_size %d\n", + active_compression_mode->name, + desc->src1_addr, desc->src1_size, desc->dst_addr, + desc->max_dst_size, desc->src2_addr, desc->src2_size); + + ret = idxd_submit_desc(wq, idxd_desc); + if (ret) { + dev_dbg(dev, "submit_desc (verify) failed ret=%d\n", ret); + goto err; + } + + ret = check_completion(dev, idxd_desc->iax_completion, false, false); + if (ret) { + dev_dbg(dev, "(verify) check_completion failed ret=%d\n", ret); + goto err; + } + + if (compression_crc != idxd_desc->iax_completion->crc) { + ret = -EINVAL; + dev_dbg(dev, "(verify) iaa comp/decomp crc mismatch:" + " comp=0x%x, decomp=0x%x\n", compression_crc, + idxd_desc->iax_completion->crc); + print_hex_dump(KERN_INFO, "cmp-rec: ", DUMP_PREFIX_OFFSET, + 8, 1, idxd_desc->iax_completion, 64, 0); + goto err; + } + + idxd_free_desc(wq, idxd_desc); +out: + return ret; +err: + idxd_free_desc(wq, idxd_desc); + dev_dbg(dev, "iaa compress failed: ret=%d\n", ret); + + goto out; +} static void iaa_desc_complete(struct idxd_desc *idxd_desc, enum idxd_complete_type comp_type, @@ -1245,133 +1379,6 @@ static int iaa_compress(struct crypto_tfm *tfm, struct acomp_req *req, goto out; } -static int iaa_remap_for_verify(struct device *dev, struct iaa_wq *iaa_wq, - struct acomp_req *req, - dma_addr_t *src_addr, dma_addr_t *dst_addr) -{ - int ret = 0; - int nr_sgs; - - dma_unmap_sg(dev, req->dst, sg_nents(req->dst), DMA_FROM_DEVICE); - dma_unmap_sg(dev, req->src, sg_nents(req->src), DMA_TO_DEVICE); - - nr_sgs = dma_map_sg(dev, req->src, sg_nents(req->src), DMA_FROM_DEVICE); - if (nr_sgs <= 0 || nr_sgs > 1) { - dev_dbg(dev, "verify: couldn't map src sg for iaa device %d," - " wq %d: ret=%d\n", iaa_wq->iaa_device->idxd->id, - iaa_wq->wq->id, ret); - ret = -EIO; - goto out; - } - *src_addr = sg_dma_address(req->src); - dev_dbg(dev, "verify: dma_map_sg, src_addr %llx, nr_sgs %d, req->src %p," - " req->slen %d, sg_dma_len(sg) %d\n", *src_addr, nr_sgs, - req->src, req->slen, sg_dma_len(req->src)); - - nr_sgs = dma_map_sg(dev, req->dst, sg_nents(req->dst), DMA_TO_DEVICE); - if (nr_sgs <= 0 || nr_sgs > 1) { - dev_dbg(dev, "verify: couldn't map dst sg for iaa device %d," - " wq %d: ret=%d\n", iaa_wq->iaa_device->idxd->id, - iaa_wq->wq->id, ret); - ret = -EIO; - dma_unmap_sg(dev, req->src, sg_nents(req->src), DMA_FROM_DEVICE); - goto out; - } - *dst_addr = sg_dma_address(req->dst); - dev_dbg(dev, "verify: dma_map_sg, dst_addr %llx, nr_sgs %d, req->dst %p," - " req->dlen %d, sg_dma_len(sg) %d\n", *dst_addr, nr_sgs, - req->dst, req->dlen, sg_dma_len(req->dst)); -out: - return ret; -} - -static int iaa_compress_verify(struct crypto_tfm *tfm, struct acomp_req *req, - struct idxd_wq *wq, - dma_addr_t src_addr, unsigned int slen, - dma_addr_t dst_addr, unsigned int *dlen, - u32 compression_crc) -{ - struct iaa_device_compression_mode *active_compression_mode; - struct iaa_compression_ctx *ctx = crypto_tfm_ctx(tfm); - struct iaa_device *iaa_device; - struct idxd_desc *idxd_desc; - struct iax_hw_desc *desc; - struct idxd_device *idxd; - struct iaa_wq *iaa_wq; - struct pci_dev *pdev; - struct device *dev; - int ret = 0; - - iaa_wq = idxd_wq_get_private(wq); - iaa_device = iaa_wq->iaa_device; - idxd = iaa_device->idxd; - pdev = idxd->pdev; - dev = &pdev->dev; - - active_compression_mode = get_iaa_device_compression_mode(iaa_device, ctx->mode); - - idxd_desc = idxd_alloc_desc(wq, IDXD_OP_BLOCK); - if (IS_ERR(idxd_desc)) { - dev_dbg(dev, "idxd descriptor allocation failed\n"); - dev_dbg(dev, "iaa compress failed: ret=%ld\n", - PTR_ERR(idxd_desc)); - return PTR_ERR(idxd_desc); - } - desc = idxd_desc->iax_hw; - - /* Verify (optional) - decompress and check crc, suppress dest write */ - - desc->flags = IDXD_OP_FLAG_CRAV | IDXD_OP_FLAG_RCR | IDXD_OP_FLAG_CC; - desc->opcode = IAX_OPCODE_DECOMPRESS; - desc->decompr_flags = IAA_DECOMP_FLAGS | IAA_DECOMP_SUPPRESS_OUTPUT; - desc->priv = 0; - - desc->src1_addr = (u64)dst_addr; - desc->src1_size = *dlen; - desc->dst_addr = (u64)src_addr; - desc->max_dst_size = slen; - desc->completion_addr = idxd_desc->compl_dma; - - dev_dbg(dev, "(verify) compression mode %s," - " desc->src1_addr %llx, desc->src1_size %d," - " desc->dst_addr %llx, desc->max_dst_size %d," - " desc->src2_addr %llx, desc->src2_size %d\n", - active_compression_mode->name, - desc->src1_addr, desc->src1_size, desc->dst_addr, - desc->max_dst_size, desc->src2_addr, desc->src2_size); - - ret = idxd_submit_desc(wq, idxd_desc); - if (ret) { - dev_dbg(dev, "submit_desc (verify) failed ret=%d\n", ret); - goto err; - } - - ret = check_completion(dev, idxd_desc->iax_completion, false, false); - if (ret) { - dev_dbg(dev, "(verify) check_completion failed ret=%d\n", ret); - goto err; - } - - if (compression_crc != idxd_desc->iax_completion->crc) { - ret = -EINVAL; - dev_dbg(dev, "(verify) iaa comp/decomp crc mismatch:" - " comp=0x%x, decomp=0x%x\n", compression_crc, - idxd_desc->iax_completion->crc); - print_hex_dump(KERN_INFO, "cmp-rec: ", DUMP_PREFIX_OFFSET, - 8, 1, idxd_desc->iax_completion, 64, 0); - goto err; - } - - idxd_free_desc(wq, idxd_desc); -out: - return ret; -err: - idxd_free_desc(wq, idxd_desc); - dev_dbg(dev, "iaa compress failed: ret=%d\n", ret); - - goto out; -} - static int iaa_decompress(struct crypto_tfm *tfm, struct acomp_req *req, struct idxd_wq *wq, dma_addr_t src_addr, unsigned int slen, @@ -1986,6 +1993,9 @@ static int iaa_decompress_main(struct acomp_req *req) return iaa_comp_adecompress(req); } +/********************************************* + * Interfaces to crypto_alg and crypto_acomp. + *********************************************/ static int iaa_comp_init_fixed(struct crypto_acomp *acomp_tfm) { struct crypto_tfm *tfm = crypto_acomp_tfm(acomp_tfm); From patchwork Fri Feb 28 10:00:17 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: 13996096 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 36F24C19776 for ; Fri, 28 Feb 2025 10:01:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3A72C280009; Fri, 28 Feb 2025 05:00:37 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 35630280007; Fri, 28 Feb 2025 05:00:37 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1CF7D280009; Fri, 28 Feb 2025 05:00:37 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id EC1A8280007 for ; Fri, 28 Feb 2025 05:00:36 -0500 (EST) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id A628D1C7E56 for ; Fri, 28 Feb 2025 10:00:36 +0000 (UTC) X-FDA: 83168908872.08.C57DAF2 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.18]) by imf08.hostedemail.com (Postfix) with ESMTP id 5A4A3160017 for ; Fri, 28 Feb 2025 10:00:34 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=CLQDsU+M; spf=pass (imf08.hostedemail.com: domain of kanchana.p.sridhar@intel.com designates 192.198.163.18 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=1740736834; 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=21kyIoSHbchHqdtXkC8uEYlZFOHLsgXDKIl1Xuz2U4E=; b=oWOzOJ4iDdvPMxznudnhjT7mjhFraWqo1KUiUY9r1TnM51YXOBl7M5HaFEZgHI8hPgmdPN jC4qeFiuF5ENifkN2fcb5UBBd6p3aTogdrmFjy6fynVuwCWFDHyElCoSVGZfAIaMp+sTvo 6k3lwy7Sivl3Qz68vilmon+guqQRLTw= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=CLQDsU+M; spf=pass (imf08.hostedemail.com: domain of kanchana.p.sridhar@intel.com designates 192.198.163.18 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=1740736834; a=rsa-sha256; cv=none; b=TshKriU9SIowAiwqZ1RDwA6F/EXwEbzgrj3l+bh3Zz8i8VF8hwIcatEudtsQAhaDljKobS /7b1HRcHey9GIQ1/qGWpez/Kd7GA3Z71kOB3vLTBYtYf3ozOaN5jtG7580LsAgNjrkAGQe ZOmKjvk797dtuoi0n+lkrc5auhSYe1E= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1740736834; x=1772272834; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=VUwt0gNvg+/EAq7f9Vl2avLoGXxhNLtj5vjvAXStlZ8=; b=CLQDsU+MbEAFE35jtQMB4H74FnmfIfdH8LhRY2y+oZfCPkX3yqhUquQn 5TszvAyWXb6hXXOAVXXp8iedE2dv8C4Rbxp5ler1qiZV9kF4X2uikM+WS NPsNr3e8p6H9y1TASzO602aLxY3e2bL/jX4jtR4YaiUjYf5E1hlIBTMTc SIDyzP1MGaidZMh0HIGWKLcgD4zRVmoHuS9OHBHDiA29A3TCe09tVIoGn F7Tt/chWiZywWqqkiW2qaNL8253Zvcthm37yo4eosWNf0F4By1jtO2YiG hosi9bPtJbPbcvAJ22KWerjegkj73dIyrN0YtCsiTI5JQGCyWmCkY0nU0 g==; X-CSE-ConnectionGUID: Kl4UQpJNShSHJdKUDLIHNg== X-CSE-MsgGUID: SkhLkJF3RwqwReUCdHvm9Q== X-IronPort-AV: E=McAfee;i="6700,10204,11358"; a="40902656" X-IronPort-AV: E=Sophos;i="6.13,322,1732608000"; d="scan'208";a="40902656" Received: from orviesa006.jf.intel.com ([10.64.159.146]) by fmvoesa112.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Feb 2025 02:00:27 -0800 X-CSE-ConnectionGUID: s4+z0dMzREOAQBXWci/Acw== X-CSE-MsgGUID: 5FdoQSIxR9G/GCcLCUjYSg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.13,322,1732608000"; d="scan'208";a="117325729" Received: from jf5300-b11a338t.jf.intel.com ([10.242.51.115]) by orviesa006.jf.intel.com with ESMTP; 28 Feb 2025 02:00:27 -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, ying.huang@linux.alibaba.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 v7 08/15] crypto: iaa - Map IAA devices/wqs to cores based on packages instead of NUMA. Date: Fri, 28 Feb 2025 02:00:17 -0800 Message-Id: <20250228100024.332528-9-kanchana.p.sridhar@intel.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20250228100024.332528-1-kanchana.p.sridhar@intel.com> References: <20250228100024.332528-1-kanchana.p.sridhar@intel.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 5A4A3160017 X-Stat-Signature: k9z44sg1b8howqxc1ftxn6xi4sf3q74k X-HE-Tag: 1740736834-870443 X-HE-Meta: U2FsdGVkX1/qweLZ5/p2tpj6YYBAPNIqV8s6GCq0Ey6xc7fMrxLHmGGQlYHzxir1hK+A4VWSr9y34GcXa6GWMsTUuFCPEs0EQiixUIfGWZwsaHOAyFpLJTNkoiVagMUgs4xEaGFZE0xF7hl4eSY2itYPpByKRf+hQ6p6y6g71Pz/kRZdsBG8S/qcjL+L/QysJIUzGnEbWutjrjJhqdl2GxXJvYL0byZ9PbapbqLbJooN+bVqZVyTPgctKNSZUyBSL39kG+WaoD/0KSIW3Uplh8nAH4wWh9t23a1Ncmc94aP20f2DZ+6Yfkg5P6X5I2Ymyrknj745+vDttunHwwZvkiZkJtvS9526A4uLFklr6AAZXsKQaE+nkSASLa/A84jLwup1p0wQd0GFcznSUirFCaIq4yktOYjTl3GqdiQJFiOY2GJVDNNEuVtA6hCGKfk0SaGosjXlP2iVAWJoSILfTqUkEURfFeneBcf4Zzta8cZMwPLlQsjDLSnYDvBBwsNJ5LkTIHuN1i4tQzAvTLQ1AAmrETvKKoXPM4L/m20OtZwkztnO2zQXp9YEkVQorJljxVMOnGDp/9UodvgSGPoJtFTlcCKLpKPbKEu8BgBlLGWpLq909aEBPiLftozDTOA+S3OQFGkbJ1LllX3x6PVnUeLkdyLon3INr6kd2psp5HXK0CSk+5no0HHwOTcHeD2T0nQa9koLIeDoGmuINToKqc+vnlv8q5k7xWvXZPSn9xjBuyZyVCoVR4gFSP5uLYK9khIhTbfEqk9eJPtkLMi9N45frsLggyjRK6skTHPYT7fwLEhxQoagss/ob7bNAWveokC0pJuD6VR5IXbW6y7Wce0TGBekax53pZohNsbCs/e9xqiaz9mn4E0dD46tFcuGM9NU0xmPTgYjjf2JU4F/zXVybRa9gW4eG/httt86+p+ruxOM9YPr9Q9qr4Lb8E3HoBfDaQcFhp0y/UNmGAW LPj+OvzV /jvZyxKZfx/3yFFPrwShP46e/dj4+yGg1bRuR7D7wteG2sH7lecE2IxU8x2xeSJCaIKshij0JounEZNlZVKnoAeFLtcoePmzl7r4iphpGvJqD5deM8uu3bIG+hRa2TWWP8OYfMHHjcX+Y/QVMs9XTsKAeBUcjtqZPixpslYOlHRyHpRXZSCpDrwSX5w== 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 modifies the algorithm for mapping available IAA devices and wqs to cores, as they are being discovered, based on packages instead of NUMA nodes. This leads to a more realistic mapping of IAA devices as compression/decompression resources for a package, rather than for a NUMA node. This also resolves problems that were observed during internal validation on Intel platforms with many more NUMA nodes than packages: for such cases, the earlier NUMA based allocation caused some IAAs to be over-subscribed and some to not be utilized at all. As a result of this change from NUMA to packages, some of the core functions used by the iaa_crypto driver's "probe" and "remove" API have been re-written. The new infrastructure maintains a static/global mapping of "local wqs" per IAA device, in the "struct iaa_device" itself. The earlier implementation would allocate memory per-cpu for this data, which never changes once the IAA devices/wqs have been initialized. Two main outcomes from this new iaa_crypto driver infrastructure are: 1) Resolves "task blocked for more than x seconds" errors observed during internal validation on Intel systems with the earlier NUMA node based mappings, which was root-caused to the non-optimal IAA-to-core mappings described earlier. 2) Results in a NUM_THREADS factor reduction in memory footprint cost of initializing IAA devices/wqs, due to eliminating the per-cpu copies of each IAA device's wqs. On a 384 cores Intel Granite Rapids server with 8 IAA devices, this saves 140MiB. Signed-off-by: Kanchana P Sridhar --- drivers/crypto/intel/iaa/iaa_crypto.h | 17 +- drivers/crypto/intel/iaa/iaa_crypto_main.c | 276 ++++++++++++--------- 2 files changed, 171 insertions(+), 122 deletions(-) diff --git a/drivers/crypto/intel/iaa/iaa_crypto.h b/drivers/crypto/intel/iaa/iaa_crypto.h index 45d94a646636..72ffdf55f7b3 100644 --- a/drivers/crypto/intel/iaa/iaa_crypto.h +++ b/drivers/crypto/intel/iaa/iaa_crypto.h @@ -55,6 +55,7 @@ struct iaa_wq { struct idxd_wq *wq; int ref; bool remove; + bool mapped; struct iaa_device *iaa_device; @@ -72,6 +73,13 @@ struct iaa_device_compression_mode { dma_addr_t aecs_comp_table_dma_addr; }; +struct wq_table_entry { + struct idxd_wq **wqs; + int max_wqs; + int n_wqs; + int cur_wq; +}; + /* Representation of IAA device with wqs, populated by probe */ struct iaa_device { struct list_head list; @@ -82,19 +90,14 @@ struct iaa_device { int n_wq; struct list_head wqs; + struct wq_table_entry *iaa_local_wqs; + atomic64_t comp_calls; atomic64_t comp_bytes; atomic64_t decomp_calls; atomic64_t decomp_bytes; }; -struct wq_table_entry { - struct idxd_wq **wqs; - int max_wqs; - int n_wqs; - int cur_wq; -}; - #define IAA_AECS_ALIGN 32 /* diff --git a/drivers/crypto/intel/iaa/iaa_crypto_main.c b/drivers/crypto/intel/iaa/iaa_crypto_main.c index abaee160e5ec..40751d7c83c0 100644 --- a/drivers/crypto/intel/iaa/iaa_crypto_main.c +++ b/drivers/crypto/intel/iaa/iaa_crypto_main.c @@ -30,8 +30,9 @@ /* number of iaa instances probed */ static unsigned int nr_iaa; static unsigned int nr_cpus; -static unsigned int nr_nodes; -static unsigned int nr_cpus_per_node; +static unsigned int nr_packages; +static unsigned int nr_cpus_per_package; +static unsigned int nr_iaa_per_package; /* Number of physical cpus sharing each iaa instance */ static unsigned int cpus_per_iaa; @@ -462,17 +463,46 @@ static void remove_device_compression_modes(struct iaa_device *iaa_device) * Functions for use in crypto probe and remove interfaces: * allocate/init/query/deallocate devices/wqs. ***********************************************************/ -static struct iaa_device *iaa_device_alloc(void) +static struct iaa_device *iaa_device_alloc(struct idxd_device *idxd) { + struct wq_table_entry *local; struct iaa_device *iaa_device; iaa_device = kzalloc(sizeof(*iaa_device), GFP_KERNEL); if (!iaa_device) - return NULL; + goto err; + + iaa_device->idxd = idxd; + + /* IAA device's local wqs. */ + iaa_device->iaa_local_wqs = kzalloc(sizeof(struct wq_table_entry), GFP_KERNEL); + if (!iaa_device->iaa_local_wqs) + goto err; + + local = iaa_device->iaa_local_wqs; + + local->wqs = kzalloc(iaa_device->idxd->max_wqs * sizeof(struct wq *), GFP_KERNEL); + if (!local->wqs) + goto err; + + local->max_wqs = iaa_device->idxd->max_wqs; + local->n_wqs = 0; INIT_LIST_HEAD(&iaa_device->wqs); return iaa_device; + +err: + if (iaa_device) { + if (iaa_device->iaa_local_wqs) { + if (iaa_device->iaa_local_wqs->wqs) + kfree(iaa_device->iaa_local_wqs->wqs); + kfree(iaa_device->iaa_local_wqs); + } + kfree(iaa_device); + } + + return NULL; } static bool iaa_has_wq(struct iaa_device *iaa_device, struct idxd_wq *wq) @@ -491,12 +521,10 @@ static struct iaa_device *add_iaa_device(struct idxd_device *idxd) { struct iaa_device *iaa_device; - iaa_device = iaa_device_alloc(); + iaa_device = iaa_device_alloc(idxd); if (!iaa_device) return NULL; - iaa_device->idxd = idxd; - list_add_tail(&iaa_device->list, &iaa_devices); nr_iaa++; @@ -537,6 +565,7 @@ static int add_iaa_wq(struct iaa_device *iaa_device, struct idxd_wq *wq, iaa_wq->wq = wq; iaa_wq->iaa_device = iaa_device; idxd_wq_set_private(wq, iaa_wq); + iaa_wq->mapped = false; list_add_tail(&iaa_wq->list, &iaa_device->wqs); @@ -580,6 +609,13 @@ static void free_iaa_device(struct iaa_device *iaa_device) return; remove_device_compression_modes(iaa_device); + + if (iaa_device->iaa_local_wqs) { + if (iaa_device->iaa_local_wqs->wqs) + kfree(iaa_device->iaa_local_wqs->wqs); + kfree(iaa_device->iaa_local_wqs); + } + kfree(iaa_device); } @@ -716,9 +752,14 @@ static int save_iaa_wq(struct idxd_wq *wq) if (WARN_ON(nr_iaa == 0)) return -EINVAL; - cpus_per_iaa = (nr_nodes * nr_cpus_per_node) / nr_iaa; + cpus_per_iaa = (nr_packages * nr_cpus_per_package) / nr_iaa; if (!cpus_per_iaa) cpus_per_iaa = 1; + + nr_iaa_per_package = nr_iaa / nr_packages; + if (!nr_iaa_per_package) + nr_iaa_per_package = 1; + out: return 0; } @@ -735,53 +776,45 @@ static void remove_iaa_wq(struct idxd_wq *wq) } if (nr_iaa) { - cpus_per_iaa = (nr_nodes * nr_cpus_per_node) / nr_iaa; + cpus_per_iaa = (nr_packages * nr_cpus_per_package) / nr_iaa; if (!cpus_per_iaa) cpus_per_iaa = 1; - } else + + nr_iaa_per_package = nr_iaa / nr_packages; + if (!nr_iaa_per_package) + nr_iaa_per_package = 1; + } else { cpus_per_iaa = 1; + nr_iaa_per_package = 1; + } } /*************************************************************** * Mapping IAA devices and wqs to cores with per-cpu wq_tables. ***************************************************************/ -static void wq_table_free_entry(int cpu) -{ - struct wq_table_entry *entry = per_cpu_ptr(wq_table, cpu); - - kfree(entry->wqs); - memset(entry, 0, sizeof(*entry)); -} - -static void wq_table_clear_entry(int cpu) -{ - struct wq_table_entry *entry = per_cpu_ptr(wq_table, cpu); - - entry->n_wqs = 0; - entry->cur_wq = 0; - memset(entry->wqs, 0, entry->max_wqs * sizeof(struct idxd_wq *)); -} - -static void clear_wq_table(void) +/* + * Given a cpu, find the closest IAA instance. The idea is to try to + * choose the most appropriate IAA instance for a caller and spread + * available workqueues around to clients. + */ +static inline int cpu_to_iaa(int cpu) { - int cpu; - - for (cpu = 0; cpu < nr_cpus; cpu++) - wq_table_clear_entry(cpu); + int package_id, base_iaa, iaa = 0; - pr_debug("cleared wq table\n"); -} + if (!nr_packages || !nr_iaa_per_package) + return 0; -static void free_wq_table(void) -{ - int cpu; + package_id = topology_logical_package_id(cpu); + base_iaa = package_id * nr_iaa_per_package; + iaa = base_iaa + ((cpu % nr_cpus_per_package) / cpus_per_iaa); - for (cpu = 0; cpu < nr_cpus; cpu++) - wq_table_free_entry(cpu); + pr_debug("cpu = %d, package_id = %d, base_iaa = %d, iaa = %d", + cpu, package_id, base_iaa, iaa); - free_percpu(wq_table); + if (iaa >= 0 && iaa < nr_iaa) + return iaa; - pr_debug("freed wq table\n"); + return (nr_iaa - 1); } static int alloc_wq_table(int max_wqs) @@ -795,13 +828,11 @@ static int alloc_wq_table(int max_wqs) for (cpu = 0; cpu < nr_cpus; cpu++) { entry = per_cpu_ptr(wq_table, cpu); - entry->wqs = kcalloc(max_wqs, sizeof(struct wq *), GFP_KERNEL); - if (!entry->wqs) { - free_wq_table(); - return -ENOMEM; - } + entry->wqs = NULL; entry->max_wqs = max_wqs; + entry->n_wqs = 0; + entry->cur_wq = 0; } pr_debug("initialized wq table\n"); @@ -809,33 +840,27 @@ static int alloc_wq_table(int max_wqs) return 0; } -static void wq_table_add(int cpu, struct idxd_wq *wq) +static void wq_table_add(int cpu, struct wq_table_entry *iaa_local_wqs) { struct wq_table_entry *entry = per_cpu_ptr(wq_table, cpu); - if (WARN_ON(entry->n_wqs == entry->max_wqs)) - return; - - entry->wqs[entry->n_wqs++] = wq; + entry->wqs = iaa_local_wqs->wqs; + entry->max_wqs = iaa_local_wqs->max_wqs; + entry->n_wqs = iaa_local_wqs->n_wqs; + entry->cur_wq = 0; - pr_debug("%s: added iaa wq %d.%d to idx %d of cpu %d\n", __func__, + pr_debug("%s: cpu %d: added %d iaa local wqs up to wq %d.%d\n", __func__, + cpu, entry->n_wqs, entry->wqs[entry->n_wqs - 1]->idxd->id, - entry->wqs[entry->n_wqs - 1]->id, entry->n_wqs - 1, cpu); + entry->wqs[entry->n_wqs - 1]->id); } static int wq_table_add_wqs(int iaa, int cpu) { struct iaa_device *iaa_device, *found_device = NULL; - int ret = 0, cur_iaa = 0, n_wqs_added = 0; - struct idxd_device *idxd; - struct iaa_wq *iaa_wq; - struct pci_dev *pdev; - struct device *dev; + int ret = 0, cur_iaa = 0; list_for_each_entry(iaa_device, &iaa_devices, list) { - idxd = iaa_device->idxd; - pdev = idxd->pdev; - dev = &pdev->dev; if (cur_iaa != iaa) { cur_iaa++; @@ -843,7 +868,8 @@ static int wq_table_add_wqs(int iaa, int cpu) } found_device = iaa_device; - dev_dbg(dev, "getting wq from iaa_device %d, cur_iaa %d\n", + dev_dbg(&found_device->idxd->pdev->dev, + "getting wq from iaa_device %d, cur_iaa %d\n", found_device->idxd->id, cur_iaa); break; } @@ -858,29 +884,58 @@ static int wq_table_add_wqs(int iaa, int cpu) } cur_iaa = 0; - idxd = found_device->idxd; - pdev = idxd->pdev; - dev = &pdev->dev; - dev_dbg(dev, "getting wq from only iaa_device %d, cur_iaa %d\n", + dev_dbg(&found_device->idxd->pdev->dev, + "getting wq from only iaa_device %d, cur_iaa %d\n", found_device->idxd->id, cur_iaa); } - list_for_each_entry(iaa_wq, &found_device->wqs, list) { - wq_table_add(cpu, iaa_wq->wq); - pr_debug("rebalance: added wq for cpu=%d: iaa wq %d.%d\n", - cpu, iaa_wq->wq->idxd->id, iaa_wq->wq->id); - n_wqs_added++; + wq_table_add(cpu, found_device->iaa_local_wqs); + +out: + return ret; +} + +static int map_iaa_device_wqs(struct iaa_device *iaa_device) +{ + struct wq_table_entry *local; + int ret = 0, n_wqs_added = 0; + struct iaa_wq *iaa_wq; + + local = iaa_device->iaa_local_wqs; + + list_for_each_entry(iaa_wq, &iaa_device->wqs, list) { + if (iaa_wq->mapped && ++n_wqs_added) + continue; + + pr_debug("iaa_device %px: processing wq %d.%d\n", iaa_device, iaa_device->idxd->id, iaa_wq->wq->id); + + if (WARN_ON(local->n_wqs == local->max_wqs)) + break; + + local->wqs[local->n_wqs++] = iaa_wq->wq; + pr_debug("iaa_device %px: added local wq %d.%d\n", iaa_device, iaa_device->idxd->id, iaa_wq->wq->id); + + iaa_wq->mapped = true; + ++n_wqs_added; } - if (!n_wqs_added) { - pr_debug("couldn't find any iaa wqs!\n"); + if (!n_wqs_added && !iaa_device->n_wq) { + pr_debug("iaa_device %d: couldn't find any iaa wqs!\n", iaa_device->idxd->id); ret = -EINVAL; - goto out; } -out: + return ret; } +static void map_iaa_devices(void) +{ + struct iaa_device *iaa_device; + + list_for_each_entry(iaa_device, &iaa_devices, list) { + BUG_ON(map_iaa_device_wqs(iaa_device)); + } +} + /* * Rebalance the wq table so that given a cpu, it's easy to find the * closest IAA instance. The idea is to try to choose the most @@ -889,48 +944,42 @@ static int wq_table_add_wqs(int iaa, int cpu) */ static void rebalance_wq_table(void) { - const struct cpumask *node_cpus; - int node, cpu, iaa = -1; + int cpu, iaa; if (nr_iaa == 0) return; - pr_debug("rebalance: nr_nodes=%d, nr_cpus %d, nr_iaa %d, cpus_per_iaa %d\n", - nr_nodes, nr_cpus, nr_iaa, cpus_per_iaa); + map_iaa_devices(); - clear_wq_table(); + pr_debug("rebalance: nr_packages=%d, nr_cpus %d, nr_iaa %d, cpus_per_iaa %d\n", + nr_packages, nr_cpus, nr_iaa, cpus_per_iaa); - if (nr_iaa == 1) { - for (cpu = 0; cpu < nr_cpus; cpu++) { - if (WARN_ON(wq_table_add_wqs(0, cpu))) { - pr_debug("could not add any wqs for iaa 0 to cpu %d!\n", cpu); - return; - } + for (cpu = 0; cpu < nr_cpus; cpu++) { + iaa = cpu_to_iaa(cpu); + pr_debug("rebalance: cpu=%d iaa=%d\n", cpu, iaa); + + if (WARN_ON(iaa == -1)) { + pr_debug("rebalance (cpu_to_iaa(%d)) failed!\n", cpu); + return; } - return; + if (WARN_ON(wq_table_add_wqs(iaa, cpu))) { + pr_debug("could not add any wqs for iaa %d to cpu %d!\n", iaa, cpu); + return; + } } - for_each_node_with_cpus(node) { - node_cpus = cpumask_of_node(node); - - for (cpu = 0; cpu < cpumask_weight(node_cpus); cpu++) { - int node_cpu = cpumask_nth(cpu, node_cpus); - - if (WARN_ON(node_cpu >= nr_cpu_ids)) { - pr_debug("node_cpu %d doesn't exist!\n", node_cpu); - return; - } - - if ((cpu % cpus_per_iaa) == 0) - iaa++; + pr_debug("Finished rebalance local wqs."); +} - if (WARN_ON(wq_table_add_wqs(iaa, node_cpu))) { - pr_debug("could not add any wqs for iaa %d to cpu %d!\n", iaa, cpu); - return; - } - } +static void free_wq_tables(void) +{ + if (wq_table) { + free_percpu(wq_table); + wq_table = NULL; } + + pr_debug("freed local wq table\n"); } /*************************************************************** @@ -2134,7 +2183,7 @@ static int iaa_crypto_probe(struct idxd_dev *idxd_dev) free_iaa_wq(idxd_wq_get_private(wq)); err_save: if (first_wq) - free_wq_table(); + free_wq_tables(); err_alloc: mutex_unlock(&iaa_devices_lock); idxd_drv_disable_wq(wq); @@ -2184,7 +2233,9 @@ static void iaa_crypto_remove(struct idxd_dev *idxd_dev) if (nr_iaa == 0) { iaa_crypto_enabled = false; - free_wq_table(); + free_wq_tables(); + BUG_ON(!list_empty(&iaa_devices)); + INIT_LIST_HEAD(&iaa_devices); module_put(THIS_MODULE); pr_info("iaa_crypto now DISABLED\n"); @@ -2210,16 +2261,11 @@ static struct idxd_device_driver iaa_crypto_driver = { static int __init iaa_crypto_init_module(void) { int ret = 0; - int node; + INIT_LIST_HEAD(&iaa_devices); nr_cpus = num_possible_cpus(); - for_each_node_with_cpus(node) - nr_nodes++; - if (!nr_nodes) { - pr_err("IAA couldn't find any nodes with cpus\n"); - return -ENODEV; - } - nr_cpus_per_node = nr_cpus / nr_nodes; + nr_cpus_per_package = topology_num_cores_per_package(); + nr_packages = topology_max_packages(); if (crypto_has_comp("deflate-generic", 0, 0)) deflate_generic_tfm = crypto_alloc_comp("deflate-generic", 0, 0); From patchwork Fri Feb 28 10:00:18 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: 13996124 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 C98EBC282C5 for ; Fri, 28 Feb 2025 10:03:01 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5F0B9280015; Fri, 28 Feb 2025 05:03:01 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 5A0BB280012; Fri, 28 Feb 2025 05:03:01 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 41931280015; Fri, 28 Feb 2025 05:03:01 -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 1C89F280012 for ; Fri, 28 Feb 2025 05:03:01 -0500 (EST) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 979441A0709 for ; Fri, 28 Feb 2025 10:03:00 +0000 (UTC) X-FDA: 83168914920.14.A489C71 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.18]) by imf08.hostedemail.com (Postfix) with ESMTP id C3E79160017 for ; Fri, 28 Feb 2025 10:00:36 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=DU1JXc7W; spf=pass (imf08.hostedemail.com: domain of kanchana.p.sridhar@intel.com designates 192.198.163.18 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=1740736837; 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=vJAnTHQ3IhxA42sUXNS6RQitcXjblu0cDsHOQ5DIOHM=; b=WFsBq+9nXE+Rozr7CMwLUryH1/E6RJH60dNBYcN+h2AQD/R0NeFLdlOQljCAuApPx5IPle 6PBFYeRY8LPf2ZpBbLhURezsoQyIHSlpp1Kynqc2+YoWcRAM9rrV3GGzW4osUS66h0hSQr 2YIF0oPfNjdEW9obmCFIRbq/m7uP2bU= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=DU1JXc7W; spf=pass (imf08.hostedemail.com: domain of kanchana.p.sridhar@intel.com designates 192.198.163.18 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=1740736837; a=rsa-sha256; cv=none; b=sdV72Kl0VGzE7EZ07JBnLK5+OO+oyHvk2xII8xYZP+x3L7VdCWNwwVcyNQgNmfC50mAnnx 0+eAEgelcSVSUEo6qBPe4vAu4E2CjOeP6BgjaZSVEDaKK+NlsvekZK8xBf5eqjBeVwSATk diu2wlhsjZrbaGr1JTP3Efwyx2TqQO4= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1740736837; x=1772272837; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=ulF50kW0uCIexjMCp05uYzE1nbxjcJTcM/dakNDW84E=; b=DU1JXc7WmfyJSVDGtiuOROLUYNJxIZC1sl6iY++vmUQ2H6kwM3Uif0uy QqD14rTMmo7r9sIpJecEMQOm22kE4U3FsUns1nvcYLiVsLOoxIE4LUPhF ECyO4dmSqGtdYaOeWtbrjyHz8basl0jEIx0j9433dZOWNwxGKpcTtGf1d TwGdyJMTIruk5BY3W+GX9BqCCdzTepjjyKY0UI2PEL770QECH5jUNUwoe KosHaZrvhHSExbaJnqTa5Dnfg8+DaEI146ZiYkU6xmdX+zCsALT+zSJAP O0/Wl1UcNy7ho2NPjnA5FlIIjNkHec6njTa44e/X2l90lsetQ0wGgyD1I Q==; X-CSE-ConnectionGUID: JAtzACAGTkGMG707Q1OGPw== X-CSE-MsgGUID: f4lmaoNFS36K13Fe/J/9cw== X-IronPort-AV: E=McAfee;i="6700,10204,11358"; a="40902668" X-IronPort-AV: E=Sophos;i="6.13,322,1732608000"; d="scan'208";a="40902668" Received: from orviesa006.jf.intel.com ([10.64.159.146]) by fmvoesa112.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Feb 2025 02:00:28 -0800 X-CSE-ConnectionGUID: iY4QjX1tS7mewOmDeRdWAQ== X-CSE-MsgGUID: twmuHe+rRSq76hL+MnD1NA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.13,322,1732608000"; d="scan'208";a="117325732" Received: from jf5300-b11a338t.jf.intel.com ([10.242.51.115]) by orviesa006.jf.intel.com with ESMTP; 28 Feb 2025 02:00:27 -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, ying.huang@linux.alibaba.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 v7 09/15] crypto: iaa - Distribute compress jobs from all cores to all IAAs on a package. Date: Fri, 28 Feb 2025 02:00:18 -0800 Message-Id: <20250228100024.332528-10-kanchana.p.sridhar@intel.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20250228100024.332528-1-kanchana.p.sridhar@intel.com> References: <20250228100024.332528-1-kanchana.p.sridhar@intel.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: C3E79160017 X-Stat-Signature: syf5b1zb4ohrf6gm4bwyjk1tuoe7guga X-HE-Tag: 1740736836-516954 X-HE-Meta: U2FsdGVkX1/HgErWY0ssyoHNwSXYfW/iirMnJntUoby0CRbGNXn+O1IwWEbcgtTlGZd0Ex8rjoNLDSjgyWeRrGmEQZcMijs9rvLeobWsrj5NotP4S0UkXIAUHTEEoUJoUKWMihWC3RXaMAiWogQ87d/IRMrv7678cqLjickLLjITgSfpuJH4s+6PheCeIHkh2GNpjPewSMmGyvVqFrUgHQ8sLin/5i+c2y75+8pNdmO9ea8jmMl0EtiGFTT5D4SaE1DRrtXxdk6Os5i9FKz/0SEEgCWaRT7bGWtiRoOEkI3pxMMKaa0+vSI8WZYF1nO2ihfZ4P2hanv/HjOs6Bzklq1LccxNjw+VQdxXn5F3XION3aSuE3M2XhkKhflPmCa1VUo8Jejpx/mYpUkulXnsTLbcRCbb46L1EA+H9JQY6vahjLFql+0JKa4WPTfJNuOVncWR5NOqwRNJWbwVWmG7FVnFLyswJExdlutK9Ztel7k7WDuj8xF//qZwcUcw9zD4wqspand7DcmWt52frHcmh9G0hM/wJfjDdBgHLoG4k+Faqz56ZfOVS5p5B7xPPOrX4mn62qIwfVlFAoPfTip1wSmFQ+ZS/Pg4lW30+YYxHXeJQzSoDxhDOMCX5dUhGD2BwIVzNEBaakKb2hUbysw1FcfSMjtw70BoKT5ACK6JZkljo5NDPPKu6kSmYv1aptsHK3qKkEYdHh4IfAfZ+fPi69HU5gK3k7x2SVjvosFhV41izrhigNIsQ6mz0Mi1sJkluq0bi3mo2kJBhKr0toQMXeDTo4XHtI+beOmU+Sp5xaBiODjGQHJUC8tDNWCsIESmqTO3xRLO1jNfvuQCMV+zSbw43gtGt0S4Y/yIVk70Llekv3DO6rNEQ2NU2sneVgnb6/K7X1C7CKC2myscpu3VXPiE245SpPW8zGeYla77p/AlbuIPfRmlmsnR4xyfMiiqZibnDpCOs23k8a1dfCM 0Xjri14M DxDHgjBM0kDerEOEvSW33Fix3p8U6lUHHJEJiNA7WF2NlSa1CYwo+Z+vgzT0nmALQEYuX3ep5Qk+DDcATsAGVED9kN71+EvH23L1S9sN+WYAdo1FgCTKdnT180qTWxsLgnd5Uu6pwPO1w3CcxAmWkM2zVEOBv7wPILE2xDPaFYjAfEqrLp/hRMbCY4AKrVsx+CorUFxyuIvfOBJSIiq1hU/jw8Q== 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 change enables processes running on any logical core on a package to use all the IAA devices enabled on that package for compress jobs. In other words, compressions originating from any process in a package will be distributed in round-robin manner to the available IAA devices on the same package. This is not the default behavior, and is recommended only for highly contended scenarios, when there is significant swapout/swapin activity. The commit log describes how to enable this feature through driver parameters, but the key thing to note is that this requires configuring 2 work-queues per IAA device (each with 64 entries), with 1 WQ used solely for decompress jobs, and the other WQ used solely for compress jobs. Hence the above recommendation. The main premise behind this change is to make sure that no compress engines on any IAA device are un-utilized/under-utilized/over-utilized. In other words, the compress engines on all IAA devices are considered a global resource for that package, thus maximizing compression throughput. This allows the use of all IAA devices present in a given package for (batched) compressions originating from zswap/zram, from all cores on this package. A new per-cpu "global_wq_table" implements this in the iaa_crypto driver. We can think of the global WQ per IAA as a WQ to which all cores on that package can submit compress jobs. To avail of this feature, the user must configure 2 WQs per IAA in order to enable distribution of compress jobs to multiple IAA devices. Each IAA will have 2 WQs: wq.0 (local WQ): Used for decompress jobs from cores mapped by the cpu_to_iaa() "even balancing of logical cores to IAA devices" algorithm. wq.1 (global WQ): Used for compress jobs from *all* logical cores on that package. The iaa_crypto driver will place all global WQs from all same-package IAA devices in the global_wq_table per cpu on that package. When the driver receives a compress job, it will lookup the "next" global WQ in the cpu's global_wq_table to submit the descriptor. The starting wq in the global_wq_table for each cpu is the global wq associated with the IAA nearest to it, so that we stagger the starting global wq for each process. This results in very uniform usage of all IAAs for compress jobs. Two new driver module parameters are added for this feature: g_wqs_per_iaa (default 0): /sys/bus/dsa/drivers/crypto/g_wqs_per_iaa This represents the number of global WQs that can be configured per IAA device. The recommended setting is 1 to enable the use of this feature once the user configures 2 WQs per IAA using higher level scripts as described in Documentation/driver-api/crypto/iaa/iaa-crypto.rst. g_consec_descs_per_gwq (default 1): /sys/bus/dsa/drivers/crypto/g_consec_descs_per_gwq This represents the number of consecutive compress jobs that will be submitted to the same global WQ (i.e. to the same IAA device) from a given core, before moving to the next global WQ. The default is 1, which is also the recommended setting to avail of this feature. The decompress jobs from any core will be sent to the "local" IAA, namely the one that the driver assigns with the cpu_to_iaa() mapping algorithm that evenly balances the assignment of logical cores to IAA devices on a package. On a 2-package Sapphire Rapids server where each package has 56 cores and 4 IAA devices, this is how the compress/decompress jobs will be mapped when the user configures 2 WQs per IAA device (which implies wq.1 will be added to the global WQ table for each logical core on that package): package(s): 2 package0 CPU(s): 0-55,112-167 package1 CPU(s): 56-111,168-223 Compress jobs: -------------- package 0: iaa_crypto will send compress jobs from all cpus (0-55,112-167) to all IAA devices on the package (iax1/iax3/iax5/iax7) in round-robin manner: iaa: iax1 iax3 iax5 iax7 package 1: iaa_crypto will send compress jobs from all cpus (56-111,168-223) to all IAA devices on the package (iax9/iax11/iax13/iax15) in round-robin manner: iaa: iax9 iax11 iax13 iax15 Decompress jobs: ---------------- package 0: cpu 0-13,112-125 14-27,126-139 28-41,140-153 42-55,154-167 iaa: iax1 iax3 iax5 iax7 package 1: cpu 56-69,168-181 70-83,182-195 84-97,196-209 98-111,210-223 iaa: iax9 iax11 iax13 iax15 Signed-off-by: Kanchana P Sridhar --- drivers/crypto/intel/iaa/iaa_crypto.h | 1 + drivers/crypto/intel/iaa/iaa_crypto_main.c | 385 ++++++++++++++++++++- 2 files changed, 378 insertions(+), 8 deletions(-) diff --git a/drivers/crypto/intel/iaa/iaa_crypto.h b/drivers/crypto/intel/iaa/iaa_crypto.h index 72ffdf55f7b3..5f38f530c33d 100644 --- a/drivers/crypto/intel/iaa/iaa_crypto.h +++ b/drivers/crypto/intel/iaa/iaa_crypto.h @@ -91,6 +91,7 @@ struct iaa_device { struct list_head wqs; struct wq_table_entry *iaa_local_wqs; + struct wq_table_entry *iaa_global_wqs; atomic64_t comp_calls; atomic64_t comp_bytes; diff --git a/drivers/crypto/intel/iaa/iaa_crypto_main.c b/drivers/crypto/intel/iaa/iaa_crypto_main.c index 40751d7c83c0..cb96897e7fed 100644 --- a/drivers/crypto/intel/iaa/iaa_crypto_main.c +++ b/drivers/crypto/intel/iaa/iaa_crypto_main.c @@ -42,6 +42,18 @@ static struct crypto_comp *deflate_generic_tfm; /* Per-cpu lookup table for balanced wqs */ static struct wq_table_entry __percpu *wq_table = NULL; +static struct wq_table_entry **pkg_global_wq_tables = NULL; + +/* Per-cpu lookup table for global wqs shared by all cpus. */ +static struct wq_table_entry __percpu *global_wq_table = NULL; + +/* + * Per-cpu counter of consecutive descriptors allocated to + * the same wq in the global_wq_table, so that we know + * when to switch to the next wq in the global_wq_table. + */ +static int __percpu *num_consec_descs_per_wq = NULL; + /* Verify results of IAA compress or not */ static bool iaa_verify_compress = false; @@ -79,6 +91,16 @@ static bool async_mode = true; /* Use interrupts */ static bool use_irq; +/* Number of global wqs per iaa*/ +static int g_wqs_per_iaa = 0; + +/* + * Number of consecutive descriptors to allocate from a + * given global wq before switching to the next wq in + * the global_wq_table. + */ +static int g_consec_descs_per_gwq = 1; + static struct iaa_compression_mode *iaa_compression_modes[IAA_COMP_MODES_MAX]; LIST_HEAD(iaa_devices); @@ -180,6 +202,60 @@ static ssize_t sync_mode_store(struct device_driver *driver, } static DRIVER_ATTR_RW(sync_mode); +static ssize_t g_wqs_per_iaa_show(struct device_driver *driver, char *buf) +{ + return sprintf(buf, "%d\n", g_wqs_per_iaa); +} + +static ssize_t g_wqs_per_iaa_store(struct device_driver *driver, + const char *buf, size_t count) +{ + int ret = -EBUSY; + + mutex_lock(&iaa_devices_lock); + + if (iaa_crypto_enabled) + goto out; + + ret = kstrtoint(buf, 10, &g_wqs_per_iaa); + if (ret) + goto out; + + ret = count; +out: + mutex_unlock(&iaa_devices_lock); + + return ret; +} +static DRIVER_ATTR_RW(g_wqs_per_iaa); + +static ssize_t g_consec_descs_per_gwq_show(struct device_driver *driver, char *buf) +{ + return sprintf(buf, "%d\n", g_consec_descs_per_gwq); +} + +static ssize_t g_consec_descs_per_gwq_store(struct device_driver *driver, + const char *buf, size_t count) +{ + int ret = -EBUSY; + + mutex_lock(&iaa_devices_lock); + + if (iaa_crypto_enabled) + goto out; + + ret = kstrtoint(buf, 10, &g_consec_descs_per_gwq); + if (ret) + goto out; + + ret = count; +out: + mutex_unlock(&iaa_devices_lock); + + return ret; +} +static DRIVER_ATTR_RW(g_consec_descs_per_gwq); + /**************************** * Driver compression modes. ****************************/ @@ -465,7 +541,7 @@ static void remove_device_compression_modes(struct iaa_device *iaa_device) ***********************************************************/ static struct iaa_device *iaa_device_alloc(struct idxd_device *idxd) { - struct wq_table_entry *local; + struct wq_table_entry *local, *global; struct iaa_device *iaa_device; iaa_device = kzalloc(sizeof(*iaa_device), GFP_KERNEL); @@ -488,6 +564,20 @@ static struct iaa_device *iaa_device_alloc(struct idxd_device *idxd) local->max_wqs = iaa_device->idxd->max_wqs; local->n_wqs = 0; + /* IAA device's global wqs. */ + iaa_device->iaa_global_wqs = kzalloc(sizeof(struct wq_table_entry), GFP_KERNEL); + if (!iaa_device->iaa_global_wqs) + goto err; + + global = iaa_device->iaa_global_wqs; + + global->wqs = kzalloc(iaa_device->idxd->max_wqs * sizeof(struct wq *), GFP_KERNEL); + if (!global->wqs) + goto err; + + global->max_wqs = iaa_device->idxd->max_wqs; + global->n_wqs = 0; + INIT_LIST_HEAD(&iaa_device->wqs); return iaa_device; @@ -499,6 +589,8 @@ static struct iaa_device *iaa_device_alloc(struct idxd_device *idxd) kfree(iaa_device->iaa_local_wqs->wqs); kfree(iaa_device->iaa_local_wqs); } + if (iaa_device->iaa_global_wqs) + kfree(iaa_device->iaa_global_wqs); kfree(iaa_device); } @@ -616,6 +708,12 @@ static void free_iaa_device(struct iaa_device *iaa_device) kfree(iaa_device->iaa_local_wqs); } + if (iaa_device->iaa_global_wqs) { + if (iaa_device->iaa_global_wqs->wqs) + kfree(iaa_device->iaa_global_wqs->wqs); + kfree(iaa_device->iaa_global_wqs); + } + kfree(iaa_device); } @@ -817,6 +915,58 @@ static inline int cpu_to_iaa(int cpu) return (nr_iaa - 1); } +static void free_global_wq_table(void) +{ + if (global_wq_table) { + free_percpu(global_wq_table); + global_wq_table = NULL; + } + + if (num_consec_descs_per_wq) { + free_percpu(num_consec_descs_per_wq); + num_consec_descs_per_wq = NULL; + } + + pr_debug("freed global wq table\n"); +} + +static int pkg_global_wq_tables_alloc(void) +{ + int i, j; + + pkg_global_wq_tables = kzalloc(nr_packages * sizeof(*pkg_global_wq_tables), GFP_KERNEL); + if (!pkg_global_wq_tables) + return -ENOMEM; + + for (i = 0; i < nr_packages; ++i) { + pkg_global_wq_tables[i] = kzalloc(sizeof(struct wq_table_entry), GFP_KERNEL); + + if (!pkg_global_wq_tables[i]) { + for (j = 0; j < i; ++j) + kfree(pkg_global_wq_tables[j]); + kfree(pkg_global_wq_tables); + pkg_global_wq_tables = NULL; + return -ENOMEM; + } + pkg_global_wq_tables[i]->wqs = NULL; + } + + return 0; +} + +static void pkg_global_wq_tables_dealloc(void) +{ + int i; + + for (i = 0; i < nr_packages; ++i) { + if (pkg_global_wq_tables[i]->wqs) + kfree(pkg_global_wq_tables[i]->wqs); + kfree(pkg_global_wq_tables[i]); + } + kfree(pkg_global_wq_tables); + pkg_global_wq_tables = NULL; +} + static int alloc_wq_table(int max_wqs) { struct wq_table_entry *entry; @@ -835,6 +985,35 @@ static int alloc_wq_table(int max_wqs) entry->cur_wq = 0; } + global_wq_table = alloc_percpu(struct wq_table_entry); + if (!global_wq_table) + return 0; + + for (cpu = 0; cpu < nr_cpus; cpu++) { + entry = per_cpu_ptr(global_wq_table, cpu); + + entry->wqs = NULL; + entry->max_wqs = max_wqs; + entry->n_wqs = 0; + entry->cur_wq = 0; + } + + num_consec_descs_per_wq = alloc_percpu(int); + if (!num_consec_descs_per_wq) { + free_global_wq_table(); + return 0; + } + + for (cpu = 0; cpu < nr_cpus; cpu++) { + int *num_consec_descs = per_cpu_ptr(num_consec_descs_per_wq, cpu); + *num_consec_descs = 0; + } + + if (pkg_global_wq_tables_alloc()) { + free_global_wq_table(); + return 0; + } + pr_debug("initialized wq table\n"); return 0; @@ -895,13 +1074,120 @@ static int wq_table_add_wqs(int iaa, int cpu) return ret; } +static void pkg_global_wq_tables_reinit(void) +{ + int i, cur_iaa = 0, pkg = 0, nr_pkg_wqs = 0; + struct iaa_device *iaa_device; + struct wq_table_entry *global; + + if (!pkg_global_wq_tables) + return; + + /* Reallocate per-package wqs. */ + list_for_each_entry(iaa_device, &iaa_devices, list) { + global = iaa_device->iaa_global_wqs; + nr_pkg_wqs += global->n_wqs; + + if (++cur_iaa == nr_iaa_per_package) { + nr_pkg_wqs = nr_pkg_wqs ? max_t(int, iaa_device->idxd->max_wqs, nr_pkg_wqs) : 0; + + if (pkg_global_wq_tables[pkg]->wqs) { + kfree(pkg_global_wq_tables[pkg]->wqs); + pkg_global_wq_tables[pkg]->wqs = NULL; + } + + if (nr_pkg_wqs) + pkg_global_wq_tables[pkg]->wqs = kzalloc(nr_pkg_wqs * + sizeof(struct wq *), + GFP_KERNEL); + + pkg_global_wq_tables[pkg]->n_wqs = 0; + pkg_global_wq_tables[pkg]->cur_wq = 0; + pkg_global_wq_tables[pkg]->max_wqs = nr_pkg_wqs; + + if (++pkg == nr_packages) + break; + cur_iaa = 0; + nr_pkg_wqs = 0; + } + } + + pkg = 0; + cur_iaa = 0; + + /* Re-initialize per-package wqs. */ + list_for_each_entry(iaa_device, &iaa_devices, list) { + global = iaa_device->iaa_global_wqs; + + if (pkg_global_wq_tables[pkg]->wqs) + for (i = 0; i < global->n_wqs; ++i) + pkg_global_wq_tables[pkg]->wqs[pkg_global_wq_tables[pkg]->n_wqs++] = global->wqs[i]; + + pr_debug("pkg_global_wq_tables[%d] has %d wqs", pkg, pkg_global_wq_tables[pkg]->n_wqs); + + if (++cur_iaa == nr_iaa_per_package) { + if (++pkg == nr_packages) + break; + cur_iaa = 0; + } + } +} + +static void global_wq_table_add(int cpu, struct wq_table_entry *pkg_global_wq_table) +{ + struct wq_table_entry *entry = per_cpu_ptr(global_wq_table, cpu); + + /* This could be NULL. */ + entry->wqs = pkg_global_wq_table->wqs; + entry->max_wqs = pkg_global_wq_table->max_wqs; + entry->n_wqs = pkg_global_wq_table->n_wqs; + entry->cur_wq = 0; + + if (entry->wqs) + pr_debug("%s: cpu %d: added %d iaa global wqs up to wq %d.%d\n", __func__, + cpu, entry->n_wqs, + entry->wqs[entry->n_wqs - 1]->idxd->id, + entry->wqs[entry->n_wqs - 1]->id); +} + +static void global_wq_table_set_start_wq(int cpu) +{ + struct wq_table_entry *entry = per_cpu_ptr(global_wq_table, cpu); + int start_wq = g_wqs_per_iaa * (cpu_to_iaa(cpu) % nr_iaa_per_package); + + if ((start_wq >= 0) && (start_wq < entry->n_wqs)) + entry->cur_wq = start_wq; +} + +static void global_wq_table_add_wqs(void) +{ + int cpu; + + if (!pkg_global_wq_tables) + return; + + for (cpu = 0; cpu < nr_cpus; cpu += nr_cpus_per_package) { + /* cpu's on the same package get the same global_wq_table. */ + int package_id = topology_logical_package_id(cpu); + int pkg_cpu; + + for (pkg_cpu = cpu; pkg_cpu < cpu + nr_cpus_per_package; ++pkg_cpu) { + if (pkg_global_wq_tables[package_id]->n_wqs > 0) { + global_wq_table_add(pkg_cpu, pkg_global_wq_tables[package_id]); + global_wq_table_set_start_wq(pkg_cpu); + } + } + } +} + static int map_iaa_device_wqs(struct iaa_device *iaa_device) { - struct wq_table_entry *local; + struct wq_table_entry *local, *global; int ret = 0, n_wqs_added = 0; struct iaa_wq *iaa_wq; local = iaa_device->iaa_local_wqs; + global = iaa_device->iaa_global_wqs; list_for_each_entry(iaa_wq, &iaa_device->wqs, list) { if (iaa_wq->mapped && ++n_wqs_added) @@ -909,11 +1195,18 @@ static int map_iaa_device_wqs(struct iaa_device *iaa_device) pr_debug("iaa_device %px: processing wq %d.%d\n", iaa_device, iaa_device->idxd->id, iaa_wq->wq->id); - if (WARN_ON(local->n_wqs == local->max_wqs)) - break; + if ((!n_wqs_added || ((n_wqs_added + g_wqs_per_iaa) < iaa_device->n_wq)) && + (local->n_wqs < local->max_wqs)) { + + local->wqs[local->n_wqs++] = iaa_wq->wq; + pr_debug("iaa_device %px: added local wq %d.%d\n", iaa_device, iaa_device->idxd->id, iaa_wq->wq->id); + } else { + if (WARN_ON(global->n_wqs == global->max_wqs)) + break; - local->wqs[local->n_wqs++] = iaa_wq->wq; - pr_debug("iaa_device %px: added local wq %d.%d\n", iaa_device, iaa_device->idxd->id, iaa_wq->wq->id); + global->wqs[global->n_wqs++] = iaa_wq->wq; + pr_debug("iaa_device %px: added global wq %d.%d\n", iaa_device, iaa_device->idxd->id, iaa_wq->wq->id); + } iaa_wq->mapped = true; ++n_wqs_added; @@ -969,6 +1262,10 @@ static void rebalance_wq_table(void) } } + if (iaa_crypto_enabled && pkg_global_wq_tables) { + pkg_global_wq_tables_reinit(); + global_wq_table_add_wqs(); + } pr_debug("Finished rebalance local wqs."); } @@ -979,7 +1276,17 @@ static void free_wq_tables(void) wq_table = NULL; } - pr_debug("freed local wq table\n"); + if (global_wq_table) { + free_percpu(global_wq_table); + global_wq_table = NULL; + } + + if (num_consec_descs_per_wq) { + free_percpu(num_consec_descs_per_wq); + num_consec_descs_per_wq = NULL; + } + + pr_debug("freed wq tables\n"); } /*************************************************************** @@ -1002,6 +1309,35 @@ static struct idxd_wq *wq_table_next_wq(int cpu) return entry->wqs[entry->cur_wq]; } +/* + * Caller should make sure to call only if the + * per_cpu_ptr "global_wq_table" is non-NULL + * and has at least one wq configured. + */ +static struct idxd_wq *global_wq_table_next_wq(int cpu) +{ + struct wq_table_entry *entry = per_cpu_ptr(global_wq_table, cpu); + int *num_consec_descs = per_cpu_ptr(num_consec_descs_per_wq, cpu); + + /* + * Fall-back to local IAA's wq if there were no global wqs configured + * for any IAA device, or if there were problems in setting up global + * wqs for this cpu's package. + */ + if (!entry->wqs) + return wq_table_next_wq(cpu); + + if ((*num_consec_descs) == g_consec_descs_per_gwq) { + if (++entry->cur_wq >= entry->n_wqs) + entry->cur_wq = 0; + *num_consec_descs = 0; + } + + ++(*num_consec_descs); + + return entry->wqs[entry->cur_wq]; +} + /************************************************* * Core iaa_crypto compress/decompress functions. *************************************************/ @@ -1563,6 +1899,7 @@ static int iaa_comp_acompress(struct acomp_req *req) struct idxd_wq *wq; struct device *dev; int order = -1; + struct wq_table_entry *entry; compression_ctx = crypto_tfm_ctx(tfm); @@ -1581,8 +1918,15 @@ static int iaa_comp_acompress(struct acomp_req *req) disable_async = true; cpu = get_cpu(); - wq = wq_table_next_wq(cpu); + entry = per_cpu_ptr(global_wq_table, cpu); + + if (!entry || !entry->wqs || entry->n_wqs == 0) { + wq = wq_table_next_wq(cpu); + } else { + wq = global_wq_table_next_wq(cpu); + } put_cpu(); + if (!wq) { pr_debug("no wq configured for cpu=%d\n", cpu); return -ENODEV; @@ -2233,6 +2577,7 @@ static void iaa_crypto_remove(struct idxd_dev *idxd_dev) if (nr_iaa == 0) { iaa_crypto_enabled = false; + pkg_global_wq_tables_dealloc(); free_wq_tables(); BUG_ON(!list_empty(&iaa_devices)); INIT_LIST_HEAD(&iaa_devices); @@ -2302,6 +2647,20 @@ static int __init iaa_crypto_init_module(void) goto err_sync_attr_create; } + ret = driver_create_file(&iaa_crypto_driver.drv, + &driver_attr_g_wqs_per_iaa); + if (ret) { + pr_debug("IAA g_wqs_per_iaa attr creation failed\n"); + goto err_g_wqs_per_iaa_attr_create; + } + + ret = driver_create_file(&iaa_crypto_driver.drv, + &driver_attr_g_consec_descs_per_gwq); + if (ret) { + pr_debug("IAA g_consec_descs_per_gwq attr creation failed\n"); + goto err_g_consec_descs_per_gwq_attr_create; + } + if (iaa_crypto_debugfs_init()) pr_warn("debugfs init failed, stats not available\n"); @@ -2309,6 +2668,12 @@ static int __init iaa_crypto_init_module(void) out: return ret; +err_g_consec_descs_per_gwq_attr_create: + driver_remove_file(&iaa_crypto_driver.drv, + &driver_attr_g_wqs_per_iaa); +err_g_wqs_per_iaa_attr_create: + driver_remove_file(&iaa_crypto_driver.drv, + &driver_attr_sync_mode); err_sync_attr_create: driver_remove_file(&iaa_crypto_driver.drv, &driver_attr_verify_compress); @@ -2332,6 +2697,10 @@ static void __exit iaa_crypto_cleanup_module(void) &driver_attr_sync_mode); driver_remove_file(&iaa_crypto_driver.drv, &driver_attr_verify_compress); + driver_remove_file(&iaa_crypto_driver.drv, + &driver_attr_g_wqs_per_iaa); + driver_remove_file(&iaa_crypto_driver.drv, + &driver_attr_g_consec_descs_per_gwq); idxd_driver_unregister(&iaa_crypto_driver); iaa_aecs_cleanup_fixed(); crypto_free_comp(deflate_generic_tfm); From patchwork Fri Feb 28 10:00:19 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: 13996118 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 EC730C282C5 for ; Fri, 28 Feb 2025 10:01:10 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7C3EA28000E; Fri, 28 Feb 2025 05:00:41 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 74D4A280007; Fri, 28 Feb 2025 05:00:41 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 616B528000E; Fri, 28 Feb 2025 05:00: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 4436F280007 for ; Fri, 28 Feb 2025 05:00:41 -0500 (EST) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 026081406B8 for ; Fri, 28 Feb 2025 10:00:40 +0000 (UTC) X-FDA: 83168909082.19.2021214 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.18]) by imf24.hostedemail.com (Postfix) with ESMTP id 92F3C18000E for ; Fri, 28 Feb 2025 10:00:37 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=Xd8EixIW; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf24.hostedemail.com: domain of kanchana.p.sridhar@intel.com designates 192.198.163.18 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=1740736837; 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=RSUdPHzQM/668ueiIbP4yqJngLMhWytO1m39iTdoK4A=; b=kdkoPEBMf1HNUi2DxrBTXDgxsJ+64RQGpNETUMN7pEGQpVPxb8TwudNDjVoVt7lpd1y61L 9QYCLIvnVm62q8dNYVQJqpX4Z5cq8FGtr+9paSVVaeI3GWieHK+43RMU+7X592eE/zw1rG 813AqP0y5wZGqbkanNjgrHMWgd9sw74= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=Xd8EixIW; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf24.hostedemail.com: domain of kanchana.p.sridhar@intel.com designates 192.198.163.18 as permitted sender) smtp.mailfrom=kanchana.p.sridhar@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1740736837; a=rsa-sha256; cv=none; b=45zv4KIE8Od4WceH32rXJGiYQGmf7Qm+dv/4nNFIp/jm0obzew6yUKJ+VivlXXi5DaxzjG sHYK+7zriljYyyrpSJtifVATmx8urJvXVWsemRo/9M9UQ24/68V3Xd3mSjoz06wqpRWia/ fTbKGMg8pMSXfGcVCt6uj7YBEkN7iAI= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1740736838; x=1772272838; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=xzJxxiFYwAjT5Xrqds5C/407QeaiWfMMqcMGCN4vRiA=; b=Xd8EixIWMa6Pddb2HEuyLtxtTO4lVo80EJLDqIwXZT9qtPdJifDoUy2y 6Ay0E4+HiLFap/rfC59XZnOTkOhsjVZOD410P0z+db/+o72F15WWnE3g1 +u/FGGcJxcrZ+3b5uBr8vF3lzD0OnwttoK++ZQ4efSZ0lLjBznBADf9Xu RNQodfEZSpCF7vJt9RJJg8j3ESqlVIm09/zKbPQTDP0pFPBtEb498k74h yjkm+YBjGly2Fzciz72UnHeA3PHWKI6cVQnKm+OZHvFqclojKyC9kf/Hb MN67f1iykZqGcwE/WAu4rdZVxS5fvW+hUNb1WunTPVB0I+Ruq9+kJLqU9 A==; X-CSE-ConnectionGUID: SoVyyfPQReyiOwyV3QubHg== X-CSE-MsgGUID: e+QbWR8vQzibCQfqJJt3Cw== X-IronPort-AV: E=McAfee;i="6700,10204,11358"; a="40902682" X-IronPort-AV: E=Sophos;i="6.13,322,1732608000"; d="scan'208";a="40902682" Received: from orviesa006.jf.intel.com ([10.64.159.146]) by fmvoesa112.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Feb 2025 02:00:28 -0800 X-CSE-ConnectionGUID: nOJPl9TkR3mbkH/s2qeF2g== X-CSE-MsgGUID: wuLPm5x3S/61atdiUPCorw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.13,322,1732608000"; d="scan'208";a="117325736" Received: from jf5300-b11a338t.jf.intel.com ([10.242.51.115]) by orviesa006.jf.intel.com with ESMTP; 28 Feb 2025 02:00:28 -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, ying.huang@linux.alibaba.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 v7 10/15] crypto: iaa - Descriptor allocation timeouts with mitigations in iaa_crypto. Date: Fri, 28 Feb 2025 02:00:19 -0800 Message-Id: <20250228100024.332528-11-kanchana.p.sridhar@intel.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20250228100024.332528-1-kanchana.p.sridhar@intel.com> References: <20250228100024.332528-1-kanchana.p.sridhar@intel.com> MIME-Version: 1.0 X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 92F3C18000E X-Rspam-User: X-Stat-Signature: fn6igmiz7bn5p6sf67usfpgb3xyamjyj X-HE-Tag: 1740736837-998238 X-HE-Meta: U2FsdGVkX19hFg1UalyVxs3XNk0GogJhgqzkQ9mUMFvIetzZEBQabsDDV0QK1uS/xUS6FlL6jKUfRfhD/qNQmqdiDJmHWspv3M+qWrrVMZUH/dBAL0t/gVqM6VAwUdFOsiZ4+Imk2guoMDJfFkmVKbV7rTxl7XKdR6R6nFzyjV2qnnX9Prol8CviVesCwpatBkPCceZBXtEhaGyZMW4AJ/CMceZ8ssy9Q+/tdk89BHKzmZcMjop2bn6Y+5r2nKPg19wUCrvNp4Si94U5fjSoo6oA3BH5l4kflYgL4JvYrRBF0iiu7foi1Mbj7HnPVMurdfapsCadW92+D4T8qLXfKV/FYXsvTsvpGPkCN1+v8xxFbDBnQF7K4GwxutWRFzQ7r3T3PA2Vy7RGiTMIbbUFDwmuT9AGDAMehswShkVc3r+IblwBi4MKqB2YQpWpRM3eqFEAA3Ue3rl437QT9xHZIzNxagb14vWGVCxMSYSg+zUKXJNheBisRtChzjBo76mtQug0gmFQ+y0ZXHJaGEvXwgAQSsSgauaf2JrRAGKzWdc+A7R+aNQ5Isw6NexKeucIQRvy8qhsffCS+rwvGybOnLd7nV6kO3/0Doyne/t5FHUzrFosamOVI52F4Y90SXZCeaIIDPptQ8mY+sG+eYc+GmV/vtsxPfy37QOKJrTD3IJ4HVvzm7FYcIAbRfJbI+2iwjEVhyrmez3Z6jmrxMyjhnAHgIxjHtigie8OLiKlBaLl2L25L5N5iLMfuuiy9CtUdjn50NFtZifGByx17d0Pc0kpyaRr5Y8i/asTpq0Hhd2m5FI5UG9PO9GmqKG+32SXqf66XIpA/Wh0OWFGZFlcx7t10jkSBXH2cn8vfbI0nerMQyY1sEw09UKkwoSvg6GVxwgRfZf6DPYvrVwWwIgB2hwsnUdaC0lyqYwhuLSqrhPmhy54LYHqdy0wYugW/jByJpNXwWLkOLgpCFCIemQ wA0u3uWV StPNIpUXkaVYvV4aO+W0pVI0Hoo0RoJzv9DYgXvxv44XJ/meoGrCxyBBn7CfQf2UaZ6NPW68rMCjZ4MUQaogqFNz5KId5+QwnBWFpYz2zD+6eO2YlyyCEqggC7h6Ddnx/DN5WwKmeumEA2BrC5hVKJlolTVXMgB4o2KtvTUBwDG5/t8C+C3ouqL09SxF7cmB//l3P6xp/vL3xYtaCKJBKQal2x0/IGoaIC8HJrwLQnZy9B9w= 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 modifies the descriptor allocation from blocking to non-blocking with bounded retries or "timeouts". This is necessary to prevent task blocked errors in high contention scenarios, for instance, when the platform has only 1 IAA device enabled. With 1 IAA device enabled per package on a dual-package SPR with 56 cores/package, there are 112 logical cores mapped to this single IAA device. In this scenario, the task blocked errors can occur because idxd_alloc_desc() is called with IDXD_OP_BLOCK. Any process that is able to obtain IAA_CRYPTO_MAX_BATCH_SIZE (8U) descriptors, will cause contention for allocating descriptors for all other processes. Under IDXD_OP_BLOCK, this can cause compress/decompress jobs to stall in stress test scenarios (e.g. zswap_store() of 2M folios). In order to make the iaa_crypto driver be more fail-safe, this commit implements the following: 1) Change compress/decompress descriptor allocations to be non-blocking with retries ("timeouts"). 2) Return compress error to zswap if descriptor allocation with timeouts fails during compress ops. zswap_store() will return an error and the folio gets stored in the backing swap device. 3) Fallback to software decompress if descriptor allocation with timeouts fails during decompress ops. 4) Bug fixes for freeing the descriptor consistently in all error cases. With these fixes, there are no task blocked errors seen under stress testing conditions, and no performance degradation observed. Signed-off-by: Kanchana P Sridhar --- drivers/crypto/intel/iaa/iaa_crypto.h | 3 + drivers/crypto/intel/iaa/iaa_crypto_main.c | 74 ++++++++++++---------- 2 files changed, 45 insertions(+), 32 deletions(-) diff --git a/drivers/crypto/intel/iaa/iaa_crypto.h b/drivers/crypto/intel/iaa/iaa_crypto.h index 5f38f530c33d..de14e5e2a017 100644 --- a/drivers/crypto/intel/iaa/iaa_crypto.h +++ b/drivers/crypto/intel/iaa/iaa_crypto.h @@ -21,6 +21,9 @@ #define IAA_COMPLETION_TIMEOUT 1000000 +#define IAA_ALLOC_DESC_COMP_TIMEOUT 1000 +#define IAA_ALLOC_DESC_DECOMP_TIMEOUT 500 + #define IAA_ANALYTICS_ERROR 0x0a #define IAA_ERROR_DECOMP_BUF_OVERFLOW 0x0b #define IAA_ERROR_COMP_BUF_OVERFLOW 0x19 diff --git a/drivers/crypto/intel/iaa/iaa_crypto_main.c b/drivers/crypto/intel/iaa/iaa_crypto_main.c index cb96897e7fed..7503fafca279 100644 --- a/drivers/crypto/intel/iaa/iaa_crypto_main.c +++ b/drivers/crypto/intel/iaa/iaa_crypto_main.c @@ -1406,6 +1406,7 @@ static int deflate_generic_decompress(struct acomp_req *req) void *src, *dst; int ret; + req->dlen = PAGE_SIZE; src = kmap_local_page(sg_page(req->src)) + req->src->offset; dst = kmap_local_page(sg_page(req->dst)) + req->dst->offset; @@ -1469,7 +1470,8 @@ static int iaa_compress_verify(struct crypto_tfm *tfm, struct acomp_req *req, struct iaa_device_compression_mode *active_compression_mode; struct iaa_compression_ctx *ctx = crypto_tfm_ctx(tfm); struct iaa_device *iaa_device; - struct idxd_desc *idxd_desc; + struct idxd_desc *idxd_desc = ERR_PTR(-EAGAIN); + int alloc_desc_retries = 0; struct iax_hw_desc *desc; struct idxd_device *idxd; struct iaa_wq *iaa_wq; @@ -1485,7 +1487,11 @@ static int iaa_compress_verify(struct crypto_tfm *tfm, struct acomp_req *req, active_compression_mode = get_iaa_device_compression_mode(iaa_device, ctx->mode); - idxd_desc = idxd_alloc_desc(wq, IDXD_OP_BLOCK); + while ((idxd_desc == ERR_PTR(-EAGAIN)) && (alloc_desc_retries++ < IAA_ALLOC_DESC_DECOMP_TIMEOUT)) { + idxd_desc = idxd_alloc_desc(wq, IDXD_OP_NONBLOCK); + cpu_relax(); + } + if (IS_ERR(idxd_desc)) { dev_dbg(dev, "idxd descriptor allocation failed\n"); dev_dbg(dev, "iaa compress failed: ret=%ld\n", @@ -1661,7 +1667,8 @@ static int iaa_compress(struct crypto_tfm *tfm, struct acomp_req *req, struct iaa_device_compression_mode *active_compression_mode; struct iaa_compression_ctx *ctx = crypto_tfm_ctx(tfm); struct iaa_device *iaa_device; - struct idxd_desc *idxd_desc; + struct idxd_desc *idxd_desc = ERR_PTR(-EAGAIN); + int alloc_desc_retries = 0; struct iax_hw_desc *desc; struct idxd_device *idxd; struct iaa_wq *iaa_wq; @@ -1677,7 +1684,11 @@ static int iaa_compress(struct crypto_tfm *tfm, struct acomp_req *req, active_compression_mode = get_iaa_device_compression_mode(iaa_device, ctx->mode); - idxd_desc = idxd_alloc_desc(wq, IDXD_OP_BLOCK); + while ((idxd_desc == ERR_PTR(-EAGAIN)) && (alloc_desc_retries++ < IAA_ALLOC_DESC_COMP_TIMEOUT)) { + idxd_desc = idxd_alloc_desc(wq, IDXD_OP_NONBLOCK); + cpu_relax(); + } + if (IS_ERR(idxd_desc)) { dev_dbg(dev, "idxd descriptor allocation failed\n"); dev_dbg(dev, "iaa compress failed: ret=%ld\n", PTR_ERR(idxd_desc)); @@ -1753,15 +1764,10 @@ static int iaa_compress(struct crypto_tfm *tfm, struct acomp_req *req, *compression_crc = idxd_desc->iax_completion->crc; - if (!ctx->async_mode || disable_async) - idxd_free_desc(wq, idxd_desc); -out: - return ret; err: idxd_free_desc(wq, idxd_desc); - dev_dbg(dev, "iaa compress failed: ret=%d\n", ret); - - goto out; +out: + return ret; } static int iaa_decompress(struct crypto_tfm *tfm, struct acomp_req *req, @@ -1773,7 +1779,8 @@ static int iaa_decompress(struct crypto_tfm *tfm, struct acomp_req *req, struct iaa_device_compression_mode *active_compression_mode; struct iaa_compression_ctx *ctx = crypto_tfm_ctx(tfm); struct iaa_device *iaa_device; - struct idxd_desc *idxd_desc; + struct idxd_desc *idxd_desc = ERR_PTR(-EAGAIN); + int alloc_desc_retries = 0; struct iax_hw_desc *desc; struct idxd_device *idxd; struct iaa_wq *iaa_wq; @@ -1789,12 +1796,18 @@ static int iaa_decompress(struct crypto_tfm *tfm, struct acomp_req *req, active_compression_mode = get_iaa_device_compression_mode(iaa_device, ctx->mode); - idxd_desc = idxd_alloc_desc(wq, IDXD_OP_BLOCK); + while ((idxd_desc == ERR_PTR(-EAGAIN)) && (alloc_desc_retries++ < IAA_ALLOC_DESC_DECOMP_TIMEOUT)) { + idxd_desc = idxd_alloc_desc(wq, IDXD_OP_NONBLOCK); + cpu_relax(); + } + if (IS_ERR(idxd_desc)) { dev_dbg(dev, "idxd descriptor allocation failed\n"); dev_dbg(dev, "iaa decompress failed: ret=%ld\n", PTR_ERR(idxd_desc)); - return PTR_ERR(idxd_desc); + ret = PTR_ERR(idxd_desc); + idxd_desc = NULL; + goto fallback_software_decomp; } desc = idxd_desc->iax_hw; @@ -1837,7 +1850,7 @@ static int iaa_decompress(struct crypto_tfm *tfm, struct acomp_req *req, ret = idxd_submit_desc(wq, idxd_desc); if (ret) { dev_dbg(dev, "submit_desc failed ret=%d\n", ret); - goto err; + goto fallback_software_decomp; } /* Update stats */ @@ -1851,19 +1864,20 @@ static int iaa_decompress(struct crypto_tfm *tfm, struct acomp_req *req, } ret = check_completion(dev, idxd_desc->iax_completion, false, false); + +fallback_software_decomp: if (ret) { - dev_dbg(dev, "%s: check_completion failed ret=%d\n", __func__, ret); - if (idxd_desc->iax_completion->status == IAA_ANALYTICS_ERROR) { + dev_dbg(dev, "%s: desc allocation/submission/check_completion failed ret=%d\n", __func__, ret); + if (idxd_desc && idxd_desc->iax_completion->status == IAA_ANALYTICS_ERROR) { pr_warn("%s: falling back to deflate-generic decompress, " "analytics error code %x\n", __func__, idxd_desc->iax_completion->error_code); - ret = deflate_generic_decompress(req); - if (ret) { - dev_dbg(dev, "%s: deflate-generic failed ret=%d\n", - __func__, ret); - goto err; - } - } else { + } + + ret = deflate_generic_decompress(req); + + if (ret) { + pr_err("%s: iaa decompress failed: fallback to deflate-generic software decompress error ret=%d\n", __func__, ret); goto err; } } else { @@ -1872,19 +1886,15 @@ static int iaa_decompress(struct crypto_tfm *tfm, struct acomp_req *req, *dlen = req->dlen; - if (!ctx->async_mode || disable_async) - idxd_free_desc(wq, idxd_desc); - /* Update stats */ update_total_decomp_bytes_in(slen); update_wq_decomp_bytes(wq, slen); + +err: + if (idxd_desc) + idxd_free_desc(wq, idxd_desc); out: return ret; -err: - idxd_free_desc(wq, idxd_desc); - dev_dbg(dev, "iaa decompress failed: ret=%d\n", ret); - - goto out; } static int iaa_comp_acompress(struct acomp_req *req) From patchwork Fri Feb 28 10:00:20 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: 13996119 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 C4862C19776 for ; Fri, 28 Feb 2025 10:01:13 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EA0F6280010; Fri, 28 Feb 2025 05:00:42 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id E51EA28000F; Fri, 28 Feb 2025 05:00:42 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CA426280010; Fri, 28 Feb 2025 05:00:42 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 9C86828000F for ; Fri, 28 Feb 2025 05:00:42 -0500 (EST) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 2421A1A0706 for ; Fri, 28 Feb 2025 10:00:42 +0000 (UTC) X-FDA: 83168909124.30.4070060 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.18]) by imf27.hostedemail.com (Postfix) with ESMTP id 1B6F84000A for ; Fri, 28 Feb 2025 10:00:37 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=CJdeID3i; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf27.hostedemail.com: domain of kanchana.p.sridhar@intel.com designates 192.198.163.18 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=1740736838; 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=+eOTM3Nk/Zac48FupoTlTwnc9jTXgoQyfvQyY2539qA=; b=13WEeDZJyQ1ihWDCblf8L0lyOQ2qrCpD7JOYELdWuIz07HCIcLuw2BDT9sP/y5OBBShdMu 2S6RtTDK6NEHmi7VSeuV8WvteuD5OKP2rDBJvJo3JVR7JhwoQIgajius9OcdgMbq4/0Urb 7Ubtyuy6WGvQ2EaC0sfgR+rXtJxLmR4= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=CJdeID3i; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf27.hostedemail.com: domain of kanchana.p.sridhar@intel.com designates 192.198.163.18 as permitted sender) smtp.mailfrom=kanchana.p.sridhar@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1740736838; a=rsa-sha256; cv=none; b=YFYQa68hK0SF0zmSvnluouSICQVrTsa00FKOwtirhoOQSE13sN0yqdv1RGFkdt81UqfzpG X7u26GzMawDeHQGsxPQfDc4u+yrlikxxYpdq3MGLjm7CYshCKSWYU8ckQHMxruUoo7f0xX U7VPY6lt/LF7e/kyECZCFF9GnrKBEds= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1740736838; x=1772272838; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=nu1yNk8S8EgSrO+ZHhQ4Hl0iMXbUsnYxF38CaLmoAAI=; b=CJdeID3ih8i6tfyoBKtaqC8K+bFs0amxUj7mfkblKLCXgCN1/3rP4Byx Pxqoc+GNcr0VOOE0/dwr6mihXG5rKq1I9S0i8er/oMOKV8rCZGSS+Wqj9 P1zaLyFfXLlFKKbfUYiYqAmGg3ArJt5v6za5ZCzYbRM/mbj++KYatVtPB +oJlTUNKWI/ntzyW0SAGREtPigEzH6XJSHvA9K47N9f4XQcTkhPWqamYj OD+ERUAb57sx+C+zGxJ4fpwAFV1rqs+eq18XXDs7ZY84B4E3Xjqm97FLf qFYM2J3UP2ORZVWT581S4gQRwc2lgU3/FFXykZs+cHijlpomsByqHwz6I w==; X-CSE-ConnectionGUID: aYVUzVKRQLCn/5ea2EPI8g== X-CSE-MsgGUID: 3FrxyS7DTEK76p0INZs8Zw== X-IronPort-AV: E=McAfee;i="6700,10204,11358"; a="40902696" X-IronPort-AV: E=Sophos;i="6.13,322,1732608000"; d="scan'208";a="40902696" Received: from orviesa006.jf.intel.com ([10.64.159.146]) by fmvoesa112.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Feb 2025 02:00:28 -0800 X-CSE-ConnectionGUID: oQnOQUszQLSG6ECXKMtNow== X-CSE-MsgGUID: yhZ+k6hdQkGkDfWwMFVwbA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.13,322,1732608000"; d="scan'208";a="117325741" Received: from jf5300-b11a338t.jf.intel.com ([10.242.51.115]) by orviesa006.jf.intel.com with ESMTP; 28 Feb 2025 02:00:28 -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, ying.huang@linux.alibaba.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 v7 11/15] crypto: iaa - Fix for "deflate_generic_tfm" global being accessed without locks. Date: Fri, 28 Feb 2025 02:00:20 -0800 Message-Id: <20250228100024.332528-12-kanchana.p.sridhar@intel.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20250228100024.332528-1-kanchana.p.sridhar@intel.com> References: <20250228100024.332528-1-kanchana.p.sridhar@intel.com> MIME-Version: 1.0 X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 1B6F84000A X-Rspam-User: X-Stat-Signature: e1g146gzzeo1kk96tyuq4ug6mieh1xoa X-HE-Tag: 1740736837-633042 X-HE-Meta: U2FsdGVkX1/ncbtBLTXBUeFYz4I6jYvvuC9mDwolWaCZlsJO1Qx8NfZKpECkIsGY9a+HcImkAJVnuU5lGs7Ss/+2XsL55TecJf4K4iJF6ASJGOcCQBfZeT9msOK1wLdGaDUPDM6KVV0Wk8A2Jz1qhwR93fFaUgaV5+dSdI2b4A0OOOqoEBtCuVqDReP2xgYg5KNXS2AV6Vom4Bb1Y/aW8TcaYuJ66YfDR+bCguy+0fL8+OJ0OmB/BzcRadA84C6tkEGEmF6x4rYNSXB5BNx8s0h67mlXDBGQuqFB/GE2M5RxIwtDuPI+J4m1/vP8L5KiH2FiE38HSaJcbVOFpifUChGmAkSbDsJ5ongJJGO4Py6KRZxbgzickJtOix4ppa4NklqP+KNL6ehAPxG+vsea3aoh0pTp2vqBxCjwfOVLdmaSxYx20M4P/n6d70ZAXM03TDaTP58TR/QCzPiy74dyNlNeNVoFha5KPMi018Q42sNyDOYtuJguBFIDsvd/6bbnEVgrBHWPKuUhpzC1QBkX/xLWxmOeq5Ymn/7h5VwTbcP1Ue+gxunEz22F7O8jZnHoOaeAM1TmZqN9a3l5azRvQO5DLpm3NWLsCdxxyMrXcRkLQQIC66rOFB5f6gU0VmX4qmnulF3uRDXeVxo3F36RbjlZ1N5OgBUWu+fYXiD1Ljv1xnaLboow4Ds2OB4jdHTOb4h9nfsj6o8GH+orR4QWYB3bzTIdJ/9qgDQY/exRTYE7Ekg3GYjY4Df57RjU9oHIYP8aMoZhhzT7xRIAY8U7xADT5H17khOacFd7cbESV9wVZWkDfJ7+WdrO4Rw/lT6B1FShLOwoTAiQRNULs65ngI8y01LRhqVvppnsatj0tDne5c9F0p/JSsIvjcKdrH+M5TLRb0+j519dJ9gRj2zgD7fzSU6YCasn5gC0g2JrApI4MUJqv5VKvTMeCDNxnjZWadKWvMntT+alAH6gOaO RmdIjVqN WWLfVEvDfIaaKINLYwig7e7wFbKhGJQG7dZWM10vlPGp7/0ROFutLKn5V01GW6tbKZy16U6qWfaKO1PeyO1camNosvo3BgAuddKWNFe86DNMfIWWy8JOfnGwLauhmUHBUp06FIJu9ynMOwi9gyNubBEXiaVIp5gTWKlBeairb4yH3EXbB2y/KsKLisReqQZCK4K8iRiTtuVLCqMBcQL41ERHcvzjNknWUXMtlBbvzEByPsUY= 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 mainline implementation of "deflate_generic_decompress" has a bug in the usage of this global variable: static struct crypto_comp *deflate_generic_tfm; The "deflate_generic_tfm" is allocated at module init time, and freed during module cleanup. Any calls to software decompress, for instance, if descriptor allocation fails or job submission fails, will trigger this bug in the deflate_generic_decompress() procedure. The problem is the unprotected access of "deflate_generic_tfm" in this procedure. While stress testing workloads under high memory pressure, with 1 IAA device and "deflate-iaa" as the compressor, the descriptor allocation times out and the software fallback route is taken. With multiple processes calling: ret = crypto_comp_decompress(deflate_generic_tfm, src, req->slen, dst, &req->dlen); we end up with data corruption, that results in req->dlen being larger than PAGE_SIZE. zswap_decompress() subsequently raises a kernel bug. This bug can manifest under high contention and memory pressure situations with high likelihood. This has been resolved by adding a mutex, which is locked before accessing "deflate_generic_tfm" and unlocked after the crypto_comp call is done. Signed-off-by: Kanchana P Sridhar --- drivers/crypto/intel/iaa/iaa_crypto_main.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/drivers/crypto/intel/iaa/iaa_crypto_main.c b/drivers/crypto/intel/iaa/iaa_crypto_main.c index 7503fafca279..2a994f307679 100644 --- a/drivers/crypto/intel/iaa/iaa_crypto_main.c +++ b/drivers/crypto/intel/iaa/iaa_crypto_main.c @@ -105,6 +105,7 @@ static struct iaa_compression_mode *iaa_compression_modes[IAA_COMP_MODES_MAX]; LIST_HEAD(iaa_devices); DEFINE_MUTEX(iaa_devices_lock); +DEFINE_MUTEX(deflate_generic_tfm_lock); /* If enabled, IAA hw crypto algos are registered, unavailable otherwise */ static bool iaa_crypto_enabled; @@ -1407,6 +1408,9 @@ static int deflate_generic_decompress(struct acomp_req *req) int ret; req->dlen = PAGE_SIZE; + + mutex_lock(&deflate_generic_tfm_lock); + src = kmap_local_page(sg_page(req->src)) + req->src->offset; dst = kmap_local_page(sg_page(req->dst)) + req->dst->offset; @@ -1416,6 +1420,8 @@ static int deflate_generic_decompress(struct acomp_req *req) kunmap_local(src); kunmap_local(dst); + mutex_unlock(&deflate_generic_tfm_lock); + update_total_sw_decomp_calls(); return ret; From patchwork Fri Feb 28 10:00:21 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: 13996123 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 C29D8C282C5 for ; Fri, 28 Feb 2025 10:01:26 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 86095280014; Fri, 28 Feb 2025 05:00:55 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 7E956280012; Fri, 28 Feb 2025 05:00:55 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6148C280014; Fri, 28 Feb 2025 05:00:55 -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 4022A280012 for ; Fri, 28 Feb 2025 05:00:55 -0500 (EST) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 6B965B1647 for ; Fri, 28 Feb 2025 10:00:54 +0000 (UTC) X-FDA: 83168909628.10.9C6BAC0 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.18]) by imf08.hostedemail.com (Postfix) with ESMTP id 3821016000A for ; Fri, 28 Feb 2025 10:00:39 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=VPuXknhS; spf=pass (imf08.hostedemail.com: domain of kanchana.p.sridhar@intel.com designates 192.198.163.18 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=1740736839; 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=IfUQhdhwDyHeOXiteLTMC+zfBfWB0s5xxO1a2tB7HK0=; b=TmAxyNVBkfA2PjzWINS5+Ux68aAn32whXoi5twrMycxHDwMZB2LDDSFMnArQ3V8+tGACJR PukbMU3cgZvVseTifXre+DuVYIjLfQE9oYQrlhCi5SLOqXIp8tnps7Y6Z/gMMKy7CHWuXW sDwN4OK4+rNer2CEGGBUd88003DpYDQ= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=VPuXknhS; spf=pass (imf08.hostedemail.com: domain of kanchana.p.sridhar@intel.com designates 192.198.163.18 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=1740736839; a=rsa-sha256; cv=none; b=PQFge/ustqTXmhZhjq6VDFC18FuvG/byjqbAAohS8IdqeOCmL6iemeqApPtDMZnuo9WAfc 3pjGs7J04HZMY++/ofFoymN0Fr0Ev4BmP8uk/RTfxI5AaRezVpphi5ZYmd/sawWkny+uep CL2WpeNEbSPmRe+VxCg9thWQbOloHs4= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1740736839; x=1772272839; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=0xFXq8Zw4cTjf3cki1MLbp7d9+ucTcZqeFB1NMNpIDM=; b=VPuXknhSVqZlyWAOjlooF8O1VQ2nGcl53aY+G637OD15vZr8broUYLOB Ni/VZouDXy8IANazKmJNA/qmAxBQpnT6ESOVSBCrxwF4CnCapiQE+H0V1 qovDsnpE7yWdcBpkBCWgytZdd5ItBpUyqiWqml7OkdXKICxOslqDm0Yt8 Mxch+IJrfyBaO5fPzYsp7b47YTwj1QgaUhtcLLXFANqH61A98CvGMF/hO 3OXSGIXQckh3+wNuy0gTnq/l0eFqjFdYjdIn5RlM10fMQUt8/aXaRLNqv 5FYvRBQF2Y0Dxkq1ME7Fa253AA5Xj7+qnG3hSGHfytIU4aRi13EHzTWWY g==; X-CSE-ConnectionGUID: umKbpDiYRV6+o4RAIHdmvA== X-CSE-MsgGUID: VOjqLwVuT1KY/oJIkcchQA== X-IronPort-AV: E=McAfee;i="6700,10204,11358"; a="40902709" X-IronPort-AV: E=Sophos;i="6.13,322,1732608000"; d="scan'208";a="40902709" Received: from orviesa006.jf.intel.com ([10.64.159.146]) by fmvoesa112.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Feb 2025 02:00:28 -0800 X-CSE-ConnectionGUID: p6GaJnUYQWeG2qID4nJh6w== X-CSE-MsgGUID: QbWmU+zhR/miHDIBdzhRZA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.13,322,1732608000"; d="scan'208";a="117325745" Received: from jf5300-b11a338t.jf.intel.com ([10.242.51.115]) by orviesa006.jf.intel.com with ESMTP; 28 Feb 2025 02:00:28 -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, ying.huang@linux.alibaba.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 v7 12/15] mm: zswap: Simplify acomp_ctx resource allocation/deletion and mutex lock usage. Date: Fri, 28 Feb 2025 02:00:21 -0800 Message-Id: <20250228100024.332528-13-kanchana.p.sridhar@intel.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20250228100024.332528-1-kanchana.p.sridhar@intel.com> References: <20250228100024.332528-1-kanchana.p.sridhar@intel.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 3821016000A X-Stat-Signature: kwaigmwn7tc1xi1dgdomkn6hsmwfdonz X-HE-Tag: 1740736839-241319 X-HE-Meta: U2FsdGVkX18NRtOrDJ9iCGgtgY0mJiMhPsHMmcbDiJlTEsS4JFH+YSMpHvjiUbp6rpNfBSbK6ePWikIA1EAmHMw3Z4uz4y/q4i+YeM0G47s8JuOKNsLl31xD4Y2SjZCMk6++a4USQRledV405qEpVJzBY8xZiTPMbtVPFVRGy7LCpFuYpjvE4XaJwfwpw9GYzDLwGl2+ilcOylB0dy4sYnzysFbtuMhGAkmYy/ljxk0zVClTD5eCUrVXPff+kBe+UeY6A1qagqVYg/d2FManCT/F5Zxcs6KKAuXM1/e7ZHexbyHDqNiZ6JYkTU9l7drdvenKW+eEeRUj12PsJ3q9WavVxjnUK0Mo3GQxU9/4fJD0L7jyERav5SmotUxHBYWAmpiR4wzj3ZvLpiVx10rrlSoZu23bs7TPLDIXwPnx8kAzbrs3Q0O79wrJZKVVQAkqjOYIs5rq0wJNkB9cNFpN7QSH+d+Rr/aPvvHUFOeszGWsKYE7zteOZUi9fX3wbVRwT4Aq/76VvACfDQaM7g1FESgegYSfZiSWvcTFp79VQAJYT8YhpMzl6aqtviNXanzUIWzrCm2lXWqeX9CCurYLikzYznMvRkHWsWr8nfnP6X4Tid8UgIV7Pw+rceMzy1yi5hf9FsVvuFp7WB96KNGf4y8eAEFcyvj/Qyb+HIHu3driV8tcmtpSgRU2EmNkHq++tO6HlI8ycg4jUBPpaS0biFDOtbKNtNZf85U3Cy3q4oE8uoXp53TVFh5Q0Fb8AZmBJiC8aX6bP26j2bZKpOCgY/j5zW24JPzxWDwI49F61Q1og/Q7BPiCg4ThcALPpZJZ5B7Z8OT/zWpSCe+k9h8m7V8B5nMl4i6Vv2ZfgpMHcoRqTxoyAy15tbuZ/VgeqwqV5vUUYMuNL6WP2+bEF3NNqZl8qYRtUgM1/BoPKPW4AOjcLH5+2IRv4f4ftNhVBqZ7metilS4uCa1naK7NLnQ 0Vi04F99 1xNqfSl+8Cjk88G6UEDmrq5NLQTBZ2d9MtLzz1vxiW6IoeDiXbkSTlmjGeuN7DYUnUCrIICT+04a78IIkwH3DQu5jinLj2v0152qhE+9zS2AlSJNIpJ/SeUvWwchaIuMvMZiaO4DktWvDmg9hTICFvajWmcpa0StlDiRxvOIJWwfTLJDZiVbQncHX8k5vMK6pWIBGvZTG+VgeCujgdPA3dReA/W6GTYOw7SiD8Urcyy6snlM= 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 modifies the acomp_ctx resources' lifetime to be from pool creation to deletion. A "bool __online" and "unsigned int nr_reqs" are added to "struct crypto_acomp_ctx" which simplify a few things: 1) zswap_pool_create() will initialize all members of each percpu acomp_ctx to 0 or NULL and only then initialize the mutex. 2) CPU hotplug will set nr_reqs to 1, allocate resources and set __online to true, without locking the mutex. 3) CPU hotunplug will lock the mutex before setting __online to false. It will not delete any resources. 4) acomp_ctx_get_cpu_lock() will lock the mutex, then check if __online is true, and if so, return the mutex for use in zswap compress and decompress ops. 5) CPU onlining after offlining will simply check if either __online or nr_reqs are non-0, and return 0 if so, with re-allocating the resources. 6) zswap_pool_destroy() will call a newly added zswap_cpu_comp_dealloc() to delete the acomp_ctx resources. 7) Common resource deletion code in case of zswap_cpu_comp_prepare() errors, and for use in zswap_cpu_comp_dealloc(), is factored into a new acomp_ctx_dealloc(). The CPU hot[un]plug callback functions are moved to "pool functions" accordingly. The per-cpu memory cost of not deleting the acomp_ctx resources upon CPU offlining, and only deleting them when the pool is destroyed, is as follows: IAA with batching: 64.8 KB Software compressors: 8.2 KB I would appreciate code review comments on whether this memory cost is acceptable, for the latency improvement that it provides due to a faster reclaim restart after a CPU hotunplug-hotplug sequence - all that the hotplug code needs to do is to check if acomp_ctx->nr_reqs is non-0, and if so, set __online to true and return, and reclaim can proceed. Signed-off-by: Kanchana P Sridhar --- mm/zswap.c | 273 +++++++++++++++++++++++++++++++++++------------------ 1 file changed, 182 insertions(+), 91 deletions(-) diff --git a/mm/zswap.c b/mm/zswap.c index 10f2a16e7586..3a93714a9327 100644 --- a/mm/zswap.c +++ b/mm/zswap.c @@ -144,10 +144,12 @@ bool zswap_never_enabled(void) struct crypto_acomp_ctx { struct crypto_acomp *acomp; struct acomp_req *req; - struct crypto_wait wait; u8 *buffer; + unsigned int nr_reqs; + struct crypto_wait wait; struct mutex mutex; bool is_sleepable; + bool __online; }; /* @@ -246,6 +248,122 @@ static inline struct xarray *swap_zswap_tree(swp_entry_t swp) **********************************/ static void __zswap_pool_empty(struct percpu_ref *ref); +static void acomp_ctx_dealloc(struct crypto_acomp_ctx *acomp_ctx) +{ + if (!IS_ERR_OR_NULL(acomp_ctx) && acomp_ctx->nr_reqs) { + + if (!IS_ERR_OR_NULL(acomp_ctx->req)) + acomp_request_free(acomp_ctx->req); + acomp_ctx->req = NULL; + + kfree(acomp_ctx->buffer); + acomp_ctx->buffer = NULL; + + if (!IS_ERR_OR_NULL(acomp_ctx->acomp)) + crypto_free_acomp(acomp_ctx->acomp); + + acomp_ctx->nr_reqs = 0; + } +} + +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); + int ret = -ENOMEM; + + /* + * Just to be even more fail-safe against changes in assumptions and/or + * implementation of the CPU hotplug code. + */ + if (acomp_ctx->__online) + return 0; + + if (acomp_ctx->nr_reqs) { + acomp_ctx->__online = true; + return 0; + } + + acomp_ctx->acomp = crypto_alloc_acomp_node(pool->tfm_name, 0, 0, cpu_to_node(cpu)); + if (IS_ERR(acomp_ctx->acomp)) { + pr_err("could not alloc crypto acomp %s : %ld\n", + pool->tfm_name, PTR_ERR(acomp_ctx->acomp)); + ret = PTR_ERR(acomp_ctx->acomp); + goto fail; + } + + acomp_ctx->nr_reqs = 1; + + acomp_ctx->req = acomp_request_alloc(acomp_ctx->acomp); + if (!acomp_ctx->req) { + pr_err("could not alloc crypto acomp_request %s\n", + pool->tfm_name); + ret = -ENOMEM; + goto fail; + } + + acomp_ctx->buffer = kmalloc_node(PAGE_SIZE * 2, GFP_KERNEL, cpu_to_node(cpu)); + if (!acomp_ctx->buffer) { + ret = -ENOMEM; + goto fail; + } + + 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(acomp_ctx->req, CRYPTO_TFM_REQ_MAY_BACKLOG, + crypto_req_done, &acomp_ctx->wait); + + acomp_ctx->is_sleepable = acomp_is_async(acomp_ctx->acomp); + + acomp_ctx->__online = true; + + return 0; + +fail: + acomp_ctx_dealloc(acomp_ctx); + + return ret; +} + +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 = per_cpu_ptr(pool->acomp_ctx, cpu); + + mutex_lock(&acomp_ctx->mutex); + acomp_ctx->__online = false; + mutex_unlock(&acomp_ctx->mutex); + + return 0; +} + +static void zswap_cpu_comp_dealloc(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); + + /* + * The lifetime of acomp_ctx resources is from pool creation to + * pool deletion. + * + * Reclaims should not be happening because, we get to this routine only + * in two scenarios: + * + * 1) pool creation failures before/during the pool ref initialization. + * 2) we are in the process of releasing the pool, it is off the + * zswap_pools list and has no references. + * + * Hence, there is no need for locks. + */ + acomp_ctx->__online = false; + acomp_ctx_dealloc(acomp_ctx); +} + static struct zswap_pool *zswap_pool_create(char *type, char *compressor) { struct zswap_pool *pool; @@ -285,13 +403,21 @@ static struct zswap_pool *zswap_pool_create(char *type, char *compressor) goto error; } - for_each_possible_cpu(cpu) - mutex_init(&per_cpu_ptr(pool->acomp_ctx, cpu)->mutex); + for_each_possible_cpu(cpu) { + struct crypto_acomp_ctx *acomp_ctx = per_cpu_ptr(pool->acomp_ctx, cpu); + + acomp_ctx->acomp = NULL; + acomp_ctx->req = NULL; + acomp_ctx->buffer = NULL; + acomp_ctx->__online = false; + acomp_ctx->nr_reqs = 0; + mutex_init(&acomp_ctx->mutex); + } ret = cpuhp_state_add_instance(CPUHP_MM_ZSWP_POOL_PREPARE, &pool->node); if (ret) - goto error; + goto ref_fail; /* being the current pool takes 1 ref; this func expects the * caller to always add the new pool as the current pool @@ -307,6 +433,9 @@ static struct zswap_pool *zswap_pool_create(char *type, char *compressor) return pool; ref_fail: + for_each_possible_cpu(cpu) + zswap_cpu_comp_dealloc(cpu, &pool->node); + cpuhp_state_remove_instance(CPUHP_MM_ZSWP_POOL_PREPARE, &pool->node); error: if (pool->acomp_ctx) @@ -361,8 +490,13 @@ static struct zswap_pool *__zswap_pool_create_fallback(void) static void zswap_pool_destroy(struct zswap_pool *pool) { + int cpu; + zswap_pool_debug("destroying", pool); + for_each_possible_cpu(cpu) + zswap_cpu_comp_dealloc(cpu, &pool->node); + cpuhp_state_remove_instance(CPUHP_MM_ZSWP_POOL_PREPARE, &pool->node); free_percpu(pool->acomp_ctx); @@ -816,85 +950,6 @@ 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) -{ - 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; - - buffer = kmalloc_node(PAGE_SIZE * 2, GFP_KERNEL, cpu_to_node(cpu)); - if (!buffer) { - ret = -ENOMEM; - goto fail; - } - - 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 fail; - } - - req = acomp_request_alloc(acomp); - if (!req) { - pr_err("could not alloc crypto acomp_request %s\n", - pool->tfm_name); - ret = -ENOMEM; - goto fail; - } - - /* - * Only hold the mutex after completing allocations, otherwise we may - * recurse into zswap through reclaim and attempt to hold the mutex - * again resulting in a deadlock. - */ - mutex_lock(&acomp_ctx->mutex); - 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, - crypto_req_done, &acomp_ctx->wait); - - acomp_ctx->buffer = buffer; - 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) - crypto_free_acomp(acomp); - kfree(buffer); - return ret; -} - -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 = 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; - if (!IS_ERR_OR_NULL(acomp_ctx->acomp)) - crypto_free_acomp(acomp_ctx->acomp); - kfree(acomp_ctx->buffer); - } - mutex_unlock(&acomp_ctx->mutex); - - return 0; -} static struct crypto_acomp_ctx *acomp_ctx_get_cpu_lock(struct zswap_pool *pool) { @@ -902,16 +957,52 @@ 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)) - return acomp_ctx; /* - * It is possible that we were migrated to a different CPU after - * getting the per-CPU ctx but before the mutex was acquired. If - * the old CPU got offlined, zswap_cpu_comp_dead() could have - * already freed ctx->req (among other things) and set it to - * NULL. Just try again on the new CPU that we ended up on. + * If the CPU onlining code successfully allocates acomp_ctx resources, + * it sets acomp_ctx->initialized to true. Until this happens, we have + * two options: + * + * 1. Return NULL and fail all stores on this CPU. + * 2. Retry, until onlining has finished allocating resources. + * + * In theory, option 1 could be more appropriate, because it + * allows the calling procedure to decide how it wants to handle + * reclaim racing with CPU hotplug. For instance, it might be Ok + * for compress to return an error for the backing swap device + * to store the folio. Decompress could wait until we get a + * valid and locked mutex after onlining has completed. For now, + * we go with option 2 because adding a do-while in + * zswap_decompress() adds latency for software compressors. + * + * Once initialized, the resources will be de-allocated only + * when the pool is destroyed. The acomp_ctx will hold on to the + * resources through CPU offlining/onlining at any time until + * the pool is destroyed. + * + * This prevents races/deadlocks between reclaim and CPU acomp_ctx + * resource allocation that are a dependency for reclaim. + * It further simplifies the interaction with CPU onlining and + * offlining: + * + * - CPU onlining does not take the mutex. It only allocates + * resources and sets __online to true. + * - CPU offlining acquires the mutex before setting + * __online to false. If reclaim has acquired the mutex, + * offlining will have to wait for reclaim to complete before + * hotunplug can proceed. Further, hotplug merely sets + * __online to false. It does not delete the acomp_ctx + * resources. + * + * Option 1 is better than potentially not exiting the earlier + * for (;;) loop because the system is running low on memory + * and/or CPUs are getting offlined for whatever reason. At + * least failing this store will prevent data loss by failing + * zswap_store(), and saving the data in the backing swap device. */ + mutex_lock(&acomp_ctx->mutex); + if (likely(acomp_ctx->__online)) + return acomp_ctx; + mutex_unlock(&acomp_ctx->mutex); } } From patchwork Fri Feb 28 10:00:22 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: 13996121 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 1BB9DC19776 for ; Fri, 28 Feb 2025 10:01:20 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C802E28000F; Fri, 28 Feb 2025 05:00:44 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id C2F1C280012; Fri, 28 Feb 2025 05:00:44 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A5BEC28000F; Fri, 28 Feb 2025 05:00:44 -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 6BFC7280012 for ; Fri, 28 Feb 2025 05:00:44 -0500 (EST) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 12A79806CA for ; Fri, 28 Feb 2025 10:00:44 +0000 (UTC) X-FDA: 83168909208.02.F3AD3F7 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.18]) by imf24.hostedemail.com (Postfix) with ESMTP id 1EC5D18000F for ; Fri, 28 Feb 2025 10:00:40 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=muGgVPDL; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf24.hostedemail.com: domain of kanchana.p.sridhar@intel.com designates 192.198.163.18 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=1740736841; 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=bikwsSp64FBPkg6BHG0s0nLOAOFo3r/1Vlc0fabeB2E=; b=CLNbJQVyCPJxF5gw9WKfbwm6ajLPqNpzECJ8l8xhVu30mgUOEIly/ZPRwImjyHFwApPztV qfezZK9XVD0kaVVPSVe/GrNZGjiagAu210or62OCn/HMuTesh9oC4QbQODY5DgYJgNJxsq Le78uykKWkPBSxEtHvkCfXLoplDJqlw= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=muGgVPDL; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf24.hostedemail.com: domain of kanchana.p.sridhar@intel.com designates 192.198.163.18 as permitted sender) smtp.mailfrom=kanchana.p.sridhar@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1740736841; a=rsa-sha256; cv=none; b=Q8qdGu9bCI6bSvmEqhlro41qCGST7q7LaBjwwyBw+9tnjEheVQ21eV1PzDFw0TSIVFK/XN zUQ15SF2YjuHQDDayAZDrBc8pLfAg67jNf4FJDA7F8uOXFyDTJe6RlC9av7gYU4EeHHW4x ahQJJ/Gg/9EwRxX41NvbrbmJJiE+Jro= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1740736841; x=1772272841; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=9EoxRBYCdba2ggIV7HlnyrqEHWRv9T6X+J8nbbYqe1A=; b=muGgVPDLbI995uCwCm83YNu4ngtuIC4o9DHAH5MV0eV2zBDlPsGwH75l 25PJKTUtwhE4eVSLb5bHMNppGlxR7Q6ER7hayv+25dvrJ+W8lx7M5YSz2 fxoWlPZ7FsDHHjiG4DUc7FsTAFuKJNnTYi4PZHpvvMAhC0/W8KZ2CZ7A1 tGtpImqaobGRWMti2IeEnQXwcb/NYvUWE3gnyAKruw5kENVUDdl/wZq4r X10E+nIK9Mrrzc03HhpEw866WMT59BwXnrpdi51fk6wc9XbpOP44uZT1b 96Cuhz1STeS7xDW5NCkLmFfU0HwXKoWhCHmwfEoyU79Kb4mIBnuu/lVae A==; X-CSE-ConnectionGUID: QivIvn9VRHi/cK7TwhgMXQ== X-CSE-MsgGUID: Q+KRcpLTRRqKyoMalXf/5A== X-IronPort-AV: E=McAfee;i="6700,10204,11358"; a="40902722" X-IronPort-AV: E=Sophos;i="6.13,322,1732608000"; d="scan'208";a="40902722" Received: from orviesa006.jf.intel.com ([10.64.159.146]) by fmvoesa112.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Feb 2025 02:00:29 -0800 X-CSE-ConnectionGUID: pMzcQvOITtOWPzaQXSTBqw== X-CSE-MsgGUID: IXxfhbbtQEeK9DMYHChuYQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.13,322,1732608000"; d="scan'208";a="117325750" Received: from jf5300-b11a338t.jf.intel.com ([10.242.51.115]) by orviesa006.jf.intel.com with ESMTP; 28 Feb 2025 02:00:29 -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, ying.huang@linux.alibaba.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 v7 13/15] mm: zswap: Allocate pool batching resources if the compressor supports batching. Date: Fri, 28 Feb 2025 02:00:22 -0800 Message-Id: <20250228100024.332528-14-kanchana.p.sridhar@intel.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20250228100024.332528-1-kanchana.p.sridhar@intel.com> References: <20250228100024.332528-1-kanchana.p.sridhar@intel.com> MIME-Version: 1.0 X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 1EC5D18000F X-Rspam-User: X-Stat-Signature: mjf1qk319sg1gz57jac4jwww1h91xda5 X-HE-Tag: 1740736840-439474 X-HE-Meta: U2FsdGVkX1+PYK7w2pHVvSKJRdaAcSJ3mftLdN3oHddxEGaNigsE6yHy3ILbd/gF95yuDMXmHkjn0Tbg5X1khrkvELsbm8dOeup3x0KE0hdFBSDv13kj0m17jvjs/9yYZUCX+nCKKnD9R+gaiPqVTPQ2YFY1g8S3hlXmJ8S07mpM4r8EanhQCrCk0Sr20GGLBcQGcgff1eON7cfkkp+Sf1XE/r8cVT/XD9x8eimZgxxaRsAXh9zPRIlDQtikiQeeO3AAu7vYiQQDhyhZ/1+py+/5OZKOSL2v5y/BuPIAvl+4I58G0kyu+efTKrlgpvfFKIffGafLP1glRZEEwH8oAtm1OWfLf4Lwnx+ZtPnYwjNEkOgr/Og/3CTU9fAfq8x1a7nmM6NzRMyu0vwC/oOtytrkQjujexo9yKpKfbL9JH/A3QCtBgIIaf9ht68jTcY5CALujOChlR4sg1wyJHDzF5yHzGt4rbxWjTQ20bKThz7Im38eNhfDbVqjL0itrxV5HteceJeQJnQyVQUmP6YR4+UXHzIa+w4qcDeAQ74FLZADhRlV/pcXsB8m323yLLdcPZ7HeJVOTqAKk7Rf5YY8BhnMHWdf6rGsswcIMw7MTWfqWzpObASSsdjGCGll1I+r3NC5XxmfPozqlRVLDyRxGKnSDQ85/twtj0EXxGBxNN5vUHBcGi5Av3AjMZCry7xrQ0an7ejgEN78v0Al2VH4NioZGnKNUGY/1FpkqI7vBpO6r0p+u0jxz+yAgR7GnfmIQHF6SyMCcGgrXZ7as2sYjgsThfgCsx6Ww/DI8pK19mo3Mjnswgob6Ts34BYBlzgLP63xyrriOpWXm6+rZi5qMo+e7hC7PazAMVQ2KH8dyW/DbkKEDakR60Fw8SREcWkCDjciWCpn0PiFPc5/v+kJJndfedudYc4Fx1mguNsiy+svFnvsMgQMRRV6Ar8dE6zQeslulwzCUDaD04/Jzkh YTymxW4t bKNjQdUSbdZRW5+geZqImQLM7nudjtZjqApHwx6yRjAkljW2gqocq+DwOGAryz/xCx+ta1FqmlbNpj56MWX7Uy1hx4DPzYWwOYoMMuEC3nr3JhU686r/1Q5wlIKOKzu/Ped/MBDFJlDmeHgOXzE0S2vqdDwqDAGOKiqeXJtT2lTzzuH60i2dWA/zSbw== 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 and multiple compression destination buffers. The zswap_cpu_comp_prepare() CPU onlining code will get the maximum batch-size the compressor supports. 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 a small extra memory overhead of allocating the "reqs" and "buffers" arrays for compressors that do not support batching. Signed-off-by: Kanchana P Sridhar --- mm/zswap.c | 100 ++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 69 insertions(+), 31 deletions(-) diff --git a/mm/zswap.c b/mm/zswap.c index 3a93714a9327..6aa602b8514e 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,8 +153,8 @@ bool zswap_never_enabled(void) struct crypto_acomp_ctx { struct crypto_acomp *acomp; - struct acomp_req *req; - u8 *buffer; + struct acomp_req **reqs; + u8 **buffers; unsigned int nr_reqs; struct crypto_wait wait; struct mutex mutex; @@ -251,13 +261,22 @@ static void __zswap_pool_empty(struct percpu_ref *ref); static void acomp_ctx_dealloc(struct crypto_acomp_ctx *acomp_ctx) { if (!IS_ERR_OR_NULL(acomp_ctx) && acomp_ctx->nr_reqs) { + 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 (!IS_ERR_OR_NULL(acomp_ctx->req)) - acomp_request_free(acomp_ctx->req); - acomp_ctx->req = NULL; - - kfree(acomp_ctx->buffer); - acomp_ctx->buffer = 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); @@ -270,7 +289,7 @@ 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); - int ret = -ENOMEM; + int i, ret = -ENOMEM; /* * Just to be even more fail-safe against changes in assumptions and/or @@ -292,22 +311,41 @@ static int zswap_cpu_comp_prepare(unsigned int cpu, struct hlist_node *node) goto fail; } - acomp_ctx->nr_reqs = 1; + acomp_ctx->nr_reqs = min(ZSWAP_MAX_BATCH_SIZE, + crypto_acomp_batch_size(acomp_ctx->acomp)); - acomp_ctx->req = acomp_request_alloc(acomp_ctx->acomp); - if (!acomp_ctx->req) { - pr_err("could not alloc crypto acomp_request %s\n", - pool->tfm_name); - ret = -ENOMEM; + acomp_ctx->reqs = kcalloc_node(acomp_ctx->nr_reqs, sizeof(struct acomp_req *), + GFP_KERNEL, cpu_to_node(cpu)); + if (!acomp_ctx->reqs) goto fail; + + for (i = 0; i < acomp_ctx->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); + goto fail; + } } - acomp_ctx->buffer = kmalloc_node(PAGE_SIZE * 2, GFP_KERNEL, cpu_to_node(cpu)); - if (!acomp_ctx->buffer) { - ret = -ENOMEM; + acomp_ctx->buffers = kcalloc_node(acomp_ctx->nr_reqs, sizeof(u8 *), + GFP_KERNEL, cpu_to_node(cpu)); + if (!acomp_ctx->buffers) goto fail; + + for (i = 0; i < acomp_ctx->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; } + /* + * 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); /* @@ -315,7 +353,7 @@ 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(acomp_ctx->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->is_sleepable = acomp_is_async(acomp_ctx->acomp); @@ -407,8 +445,8 @@ static struct zswap_pool *zswap_pool_create(char *type, char *compressor) struct crypto_acomp_ctx *acomp_ctx = per_cpu_ptr(pool->acomp_ctx, cpu); acomp_ctx->acomp = NULL; - acomp_ctx->req = NULL; - acomp_ctx->buffer = NULL; + acomp_ctx->reqs = NULL; + acomp_ctx->buffers = NULL; acomp_ctx->__online = false; acomp_ctx->nr_reqs = 0; mutex_init(&acomp_ctx->mutex); @@ -1026,7 +1064,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); @@ -1036,7 +1074,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, @@ -1050,8 +1088,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; @@ -1102,19 +1140,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); } From patchwork Fri Feb 28 10:00:23 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: 13996120 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 E252FC19776 for ; Fri, 28 Feb 2025 10:01:16 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6B2BA280011; Fri, 28 Feb 2025 05:00:44 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 68A0D28000F; Fri, 28 Feb 2025 05:00:44 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4B496280011; Fri, 28 Feb 2025 05:00:44 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 22E9A28000F for ; Fri, 28 Feb 2025 05:00:44 -0500 (EST) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id C77E51406D4 for ; Fri, 28 Feb 2025 10:00:43 +0000 (UTC) X-FDA: 83168909166.13.9825677 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.18]) by imf08.hostedemail.com (Postfix) with ESMTP id 90BE1160014 for ; Fri, 28 Feb 2025 10:00:41 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=BHgeEuNO; spf=pass (imf08.hostedemail.com: domain of kanchana.p.sridhar@intel.com designates 192.198.163.18 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=1740736841; 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=osSdciknXf0N+P0Uk5GYtHkcpbPIHbpGuj7NzjEYSFs=; b=8TwxGx6n1Lc8H3MQvQFej6M3wUstwMkzse/+BPYxyDv9Q4AhfoYkAaK9gGJk7cE6ZCK1zB UWXV/BHr5To/oYUT0Xs9+lM3Jcy81Lt1vLFY7h1oCkvRCGLscooSDFE+EHF23gTqCM05CF pFk3j1dbdiGQsCPOhFR+bByi1/FFVD0= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=BHgeEuNO; spf=pass (imf08.hostedemail.com: domain of kanchana.p.sridhar@intel.com designates 192.198.163.18 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=1740736841; a=rsa-sha256; cv=none; b=x2U3HjJcIm/XlVxEspa+oRlsva6lRMTvI3o6WGNMGwRe96j8+lPx7Usk7NIRRsQmappyP8 2DUvWadIsPYxKpu++Bfin6wK8x87kFD/sYXOzk0+7m/VfH9dH2d8YfvZNkBy25pc+PZ0nt oLqLWSRsO/L5nj5xRm4HjOJgFhDEDog= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1740736842; x=1772272842; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=r6PEYgxq6/N6uxBAD+xcepNUnGuvoW5pJWNnSNPI1s4=; b=BHgeEuNOkhpG8/pNxpu1P6hrGG4BgrGtezDaNBVX5Ni0uKFf4Te913/r 4thQ9Dl+mHFmDUNjXwYGo1xAJepzNM30asz4R2HdJJpqNiVBdMyxVQThl AL1l1dvewyCFC8v3MtfR0N+whh3s7q548h/XCly6974YiDosOqEN95NaD DcNfqxUWJIort6yCGgtfnf/D/x8ZlP1Gxf80z4VlgGuKAdUWF3irZpgkC 1fTbRu9ijPieQjI1D1I/ZEy8Jy5cU59pLvp0QPMuMLQdb6nMk6WKkxJLe FcF/1B+Pk4f5llnxG2QsjwsBtiWm2tvzWPRMsfGCEDussyDtWuFM+28jS g==; X-CSE-ConnectionGUID: e4BMgzs8Sa6tLyTyXeJDAA== X-CSE-MsgGUID: A2gPB7IcQ9OTFv/+Vv9BhQ== X-IronPort-AV: E=McAfee;i="6700,10204,11358"; a="40902739" X-IronPort-AV: E=Sophos;i="6.13,322,1732608000"; d="scan'208";a="40902739" Received: from orviesa006.jf.intel.com ([10.64.159.146]) by fmvoesa112.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Feb 2025 02:00:29 -0800 X-CSE-ConnectionGUID: qKe0qnEyQwu6amG26g9mBA== X-CSE-MsgGUID: mm6IdXK6SA27wiKDPmIP8w== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.13,322,1732608000"; d="scan'208";a="117325757" Received: from jf5300-b11a338t.jf.intel.com ([10.242.51.115]) by orviesa006.jf.intel.com with ESMTP; 28 Feb 2025 02:00:29 -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, ying.huang@linux.alibaba.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 v7 14/15] mm: zswap: Restructure & simplify zswap_store() to make it amenable for batching. Date: Fri, 28 Feb 2025 02:00:23 -0800 Message-Id: <20250228100024.332528-15-kanchana.p.sridhar@intel.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20250228100024.332528-1-kanchana.p.sridhar@intel.com> References: <20250228100024.332528-1-kanchana.p.sridhar@intel.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 90BE1160014 X-Stat-Signature: on831cb3ifrferpa3usoehkqxhahyb6r X-HE-Tag: 1740736841-360990 X-HE-Meta: U2FsdGVkX19gADawrV8Y6juMfSG5lI8cYr0fc7oh40V9UIX1QIUf2pvdAldu11yx+U+3hutOPBxtWbNNn3Lsi7FsnLrueZ5+0aHMTO6tu0UXHLCbdLw+bws9bPQcqpE1dNU6A7IigBL67wJ+VKUITUCKoWT433NCI+aEZiqXjINZXBgXdTULctlshHlSBOfFybAxkzdMqvIkDM38j4aOAO46gFUK6dCtu36w3goplDhcru4kv3rGYySi3nWasg53hevTshiWu4qjeEz+gKvXVS8H10VFepZYEwuXtHa1tpu9kN0NHthO8Dis6AECxgIcDGg/dODskNsUJSN+ZjoAdxKfhd7GO6pmqEDo6Yt0KQrgLRWC9KyqOZO6Y6IZPFZseKkGGZirfMdrgE0iwvXBqNebSxo6TCTDRWrohcMWCJyAz193KUleNU9TZ5k5vKPt4TXZi9tug+Rp2lGTx4LOvR5fHvqp7BBTdWhI7F8IB3wcUp6lJYOZPQsjiWuzPGZpZAumLE5hccmvPHTLOKbSNnOUYlKQgNuY5m4ASu6gXH5tLgOYIdulNvqjpvhJxrSpI/kNHca3JY9gw5jKfHc6P1d9tTVdoBQtSuXxxgdViHfONTK5qaeGYGH0LtjIFwQhm8PHvzjFqKU16JTBohQzXjZz2T3aNac6CraWGdkm5RktqKhbtF2EisK/OXj+DWwpHHA8/tdWYtc6ilTvJHSzcfoVX4TEXyDTlCcPMH/RI2wA7WOmoJWkIuugA8lDgzdtaNaBeYaCvuQbMBL92CUvssSThOS2XSbYMgf7vPsPl4pp/B1GOkyYGoMr8i8YvvmHCA4Lx3wsBl7RpixBtKIBE4Be5uDzEmFU7+UBM5YEyQ3XbEbbpth2mArk5DgtpC4jVmDsBaPmU1d0AO3zYyf7lPnfBfNFy77MT+r8hEYbwGzciIGKeol5ErAA0ul4yvI9DrEzT15ZanuIKermTXM tJNEh1xc HXrLj4TBt5Q6XPO8ss3OZ5NIdWcf9ifvAQJsfrqOm9o5675Gv3Ekh4DOAP9b4wP/0lkb7Mf+gc9SiMmLZk5FuFcEY0WPKKh/J7/m+b3GeAflQ2sSRQVnzEvFz++jhr5xoQ7I0152HTFL9+hzViJsAkOK1Cw1LGl+amPrkC3sXBTAnppTU+oQoljEkjUIH6stWFJ/IOPkLDnN46oe6VSNlWOHJsyXvrjqTufEa+ZomsxUsB9E= 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 introduces zswap_store_folio() that implements all the computes done earlier in zswap_store_page() for a single-page, for all the pages in a folio. This allows us to move the loop over the folio's pages from zswap_store() to zswap_store_folio(). zswap_store_folio() starts by allocating all zswap entries required to store the folio. Next, it iterates over the folio's pages, and for each page, it calls zswap_compress(), adds the zswap entry to the xarray and LRU, charges zswap memory and increments zswap stats. The error handling and cleanup required for all failure scenarios that can occur while storing a folio in zswap are now consolidated to a "store_folio_failed" label in zswap_store_folio(). These changes facilitate developing support for compress batching in zswap_store_folio(). Signed-off-by: Kanchana P Sridhar --- mm/zswap.c | 166 +++++++++++++++++++++++++++++++---------------------- 1 file changed, 98 insertions(+), 68 deletions(-) diff --git a/mm/zswap.c b/mm/zswap.c index 6aa602b8514e..ab9167220cb6 100644 --- a/mm/zswap.c +++ b/mm/zswap.c @@ -1580,81 +1580,115 @@ static void shrink_worker(struct work_struct *w) * main API **********************************/ -static bool zswap_store_page(struct page *page, - struct obj_cgroup *objcg, - struct zswap_pool *pool) +/* + * Store all pages in a folio. + * + * The error handling from all failure points is consolidated to the + * "store_folio_failed" label, based on the initialization of the zswap entries' + * handles to ERR_PTR(-EINVAL) at allocation time, and the fact that the + * entry's handle is subsequently modified only upon a successful zpool_malloc() + * after the page is compressed. + */ +static bool zswap_store_folio(struct folio *folio, + struct obj_cgroup *objcg, + struct zswap_pool *pool) { - swp_entry_t page_swpentry = page_swap_entry(page); - struct zswap_entry *entry, *old; + long index, from_index = 0, nr_pages = folio_nr_pages(folio); + struct zswap_entry **entries = NULL; + int node_id = folio_nid(folio); - /* allocate entry */ - entry = zswap_entry_cache_alloc(GFP_KERNEL, page_to_nid(page)); - if (!entry) { - zswap_reject_kmemcache_fail++; + entries = kmalloc(nr_pages * sizeof(*entries), GFP_KERNEL); + if (!entries) return false; - } - if (!zswap_compress(page, entry, pool)) - goto compress_failed; + for (index = from_index; index < nr_pages; ++index) { + entries[index] = zswap_entry_cache_alloc(GFP_KERNEL, node_id); - old = xa_store(swap_zswap_tree(page_swpentry), - swp_offset(page_swpentry), - entry, GFP_KERNEL); - if (xa_is_err(old)) { - int err = xa_err(old); + if (!entries[index]) { + zswap_reject_kmemcache_fail++; + nr_pages = index; + goto store_folio_failed; + } - WARN_ONCE(err != -ENOMEM, "unexpected xarray error: %d\n", err); - zswap_reject_alloc_fail++; - goto store_failed; + entries[index]->handle = (unsigned long)ERR_PTR(-EINVAL); } - /* - * We may have had an existing entry that became stale when - * the folio was redirtied and now the new version is being - * swapped out. Get rid of the old. - */ - if (old) - zswap_entry_free(old); + for (index = from_index; index < nr_pages; ++index) { + struct page *page = folio_page(folio, index); + swp_entry_t page_swpentry = page_swap_entry(page); + struct zswap_entry *old, *entry = entries[index]; - /* - * The entry is successfully compressed and stored in the tree, there is - * no further possibility of failure. Grab refs to the pool and objcg, - * charge zswap memory, and increment zswap_stored_pages. - * The opposite actions will be performed by zswap_entry_free() - * when the entry is removed from the tree. - */ - zswap_pool_get(pool); - if (objcg) { - obj_cgroup_get(objcg); - obj_cgroup_charge_zswap(objcg, entry->length); - } - atomic_long_inc(&zswap_stored_pages); + if (!zswap_compress(page, entry, pool)) { + from_index = index; + goto store_folio_failed; + } - /* - * We finish initializing the entry while it's already in xarray. - * This is safe because: - * - * 1. Concurrent stores and invalidations are excluded by folio lock. - * - * 2. Writeback is excluded by the entry not being on the LRU yet. - * The publishing order matters to prevent writeback from seeing - * an incoherent entry. - */ - entry->pool = pool; - entry->swpentry = page_swpentry; - entry->objcg = objcg; - entry->referenced = true; - if (entry->length) { - INIT_LIST_HEAD(&entry->lru); - zswap_lru_add(&zswap_list_lru, entry); + old = xa_store(swap_zswap_tree(page_swpentry), + swp_offset(page_swpentry), + entry, GFP_KERNEL); + if (xa_is_err(old)) { + int err = xa_err(old); + + WARN_ONCE(err != -ENOMEM, "unexpected xarray error: %d\n", err); + zswap_reject_alloc_fail++; + from_index = index; + goto store_folio_failed; + } + + /* + * We may have had an existing entry that became stale when + * the folio was redirtied and now the new version is being + * swapped out. Get rid of the old. + */ + if (old) + zswap_entry_free(old); + + /* + * The entry is successfully compressed and stored in the tree, there is + * no further possibility of failure. Grab refs to the pool and objcg, + * charge zswap memory, and increment zswap_stored_pages. + * The opposite actions will be performed by zswap_entry_free() + * when the entry is removed from the tree. + */ + zswap_pool_get(pool); + if (objcg) { + obj_cgroup_get(objcg); + obj_cgroup_charge_zswap(objcg, entry->length); + } + atomic_long_inc(&zswap_stored_pages); + + /* + * We finish initializing the entry while it's already in xarray. + * This is safe because: + * + * 1. Concurrent stores and invalidations are excluded by folio lock. + * + * 2. Writeback is excluded by the entry not being on the LRU yet. + * The publishing order matters to prevent writeback from seeing + * an incoherent entry. + */ + entry->pool = pool; + entry->swpentry = page_swpentry; + entry->objcg = objcg; + entry->referenced = true; + if (entry->length) { + INIT_LIST_HEAD(&entry->lru); + zswap_lru_add(&zswap_list_lru, entry); + } } + kfree(entries); return true; -store_failed: - zpool_free(pool->zpool, entry->handle); -compress_failed: - zswap_entry_cache_free(entry); +store_folio_failed: + for (index = from_index; index < nr_pages; ++index) { + if (!IS_ERR_VALUE(entries[index]->handle)) + zpool_free(pool->zpool, entries[index]->handle); + + zswap_entry_cache_free(entries[index]); + } + + kfree(entries); return false; } @@ -1666,7 +1700,6 @@ bool zswap_store(struct folio *folio) struct mem_cgroup *memcg = NULL; struct zswap_pool *pool; bool ret = false; - long index; VM_WARN_ON_ONCE(!folio_test_locked(folio)); VM_WARN_ON_ONCE(!folio_test_swapcache(folio)); @@ -1700,12 +1733,8 @@ bool zswap_store(struct folio *folio) mem_cgroup_put(memcg); } - for (index = 0; index < nr_pages; ++index) { - struct page *page = folio_page(folio, index); - - if (!zswap_store_page(page, objcg, pool)) - goto put_pool; - } + if (!zswap_store_folio(folio, objcg, pool)) + goto put_pool; if (objcg) count_objcg_events(objcg, ZSWPOUT, nr_pages); @@ -1732,6 +1761,7 @@ bool zswap_store(struct folio *folio) pgoff_t offset = swp_offset(swp); struct zswap_entry *entry; struct xarray *tree; + long index; for (index = 0; index < nr_pages; ++index) { tree = swap_zswap_tree(swp_entry(type, offset + index)); From patchwork Fri Feb 28 10:00:24 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: 13996122 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 5EDDCC19776 for ; Fri, 28 Feb 2025 10:01:23 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D1C6E280013; Fri, 28 Feb 2025 05:00:45 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id CA3FA280012; Fri, 28 Feb 2025 05:00:45 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B1D27280013; Fri, 28 Feb 2025 05:00:45 -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 8EFBD280012 for ; Fri, 28 Feb 2025 05:00:45 -0500 (EST) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 34A1B1406D4 for ; Fri, 28 Feb 2025 10:00:45 +0000 (UTC) X-FDA: 83168909250.19.11314EB Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.18]) by imf27.hostedemail.com (Postfix) with ESMTP id 1570740021 for ; Fri, 28 Feb 2025 10:00:41 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=hYBzWHAk; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf27.hostedemail.com: domain of kanchana.p.sridhar@intel.com designates 192.198.163.18 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=1740736842; 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=j7GIziu1yfO1je/wZqK8YcnG3usjIQRGVwkaxVMWF/o=; b=ivx7o5QlFtwkvTK+V1PnJRfnUFh4JUEgy4epVhEdZe49+sEMCxpshLfcutN4I1tT8ZFQCk e74TKoZXFbBDuSke8uw1wHunXe9L6qxjJwNYw6BPaurcFYohLiGXrr0MUBToNb0yqvaPxM gdgbSqKRwKFyvnIvxxn9F6ZjVdQ9lUg= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=hYBzWHAk; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf27.hostedemail.com: domain of kanchana.p.sridhar@intel.com designates 192.198.163.18 as permitted sender) smtp.mailfrom=kanchana.p.sridhar@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1740736842; a=rsa-sha256; cv=none; b=BZHVbtwImN8ICGptlOKDA9oljiPEdpq3zXnc0jG6tgEfQmnNuxjm9tSfweAR5XquqMCskS vRSX0wDUhhO/L0EoKkrft+XvkSKXMdaEevr0EKL7bnvw55PFErkLy0T5HEsun+jjJ0z39+ SKFlM14imGarWOLd9w1brfkgkaE4Vjw= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1740736842; x=1772272842; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=jvGXi0w+0TEz8Qyri1YoZHBnZuLioAdX+aCO0qi+82U=; b=hYBzWHAksB4ZETazslBFney+nyjhgg+W6i7tWTonlaGPM4F7K2Yq/8mD LlUfvlkeboHShaN/Oz7OidgEuCGPpC0LRxIW78luwMZt4L/ZyiF3WDKd3 Fpl6LPDhIAmqO+eQZXLui9fAxHgrso70jm1/dTKFbAMtlfq+4vCThkKkn WEs0eky5By5xOHFmxprqYngR3PPvG7YdbC7Q70FI2tRuJwR7eh2MGaJwv AXLQnxvQRqlBhJtvzbGcaUTCBJvqTYf6B7XgRQDlumaFXVe2sEsdDUN8U TVYWWmUwGcYpu4fd6Qh9YqnQQxVwAq6DGpIu0imHyY5IvI+BdsIAaL/sn g==; X-CSE-ConnectionGUID: KN/Qij1MSAaiS4/ufIf0Xw== X-CSE-MsgGUID: aVUMBj88SR2R0u0fNH3CWA== X-IronPort-AV: E=McAfee;i="6700,10204,11358"; a="40902749" X-IronPort-AV: E=Sophos;i="6.13,322,1732608000"; d="scan'208";a="40902749" Received: from orviesa006.jf.intel.com ([10.64.159.146]) by fmvoesa112.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Feb 2025 02:00:30 -0800 X-CSE-ConnectionGUID: gf9n65U1T1W3py/NR3L3jA== X-CSE-MsgGUID: FFypTQGRSBufECFT1f0iYQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.13,322,1732608000"; d="scan'208";a="117325761" Received: from jf5300-b11a338t.jf.intel.com ([10.242.51.115]) by orviesa006.jf.intel.com with ESMTP; 28 Feb 2025 02:00:30 -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, ying.huang@linux.alibaba.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 v7 15/15] mm: zswap: Compress batching with request chaining in zswap_store() of large folios. Date: Fri, 28 Feb 2025 02:00:24 -0800 Message-Id: <20250228100024.332528-16-kanchana.p.sridhar@intel.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20250228100024.332528-1-kanchana.p.sridhar@intel.com> References: <20250228100024.332528-1-kanchana.p.sridhar@intel.com> MIME-Version: 1.0 X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 1570740021 X-Rspam-User: X-Stat-Signature: kkqn396rj7d1dpt5998zhcwa6u7f8yes X-HE-Tag: 1740736841-560954 X-HE-Meta: U2FsdGVkX1+C/DyYZsf71efDuSRrBFGUicfsHXyVbnbi3S3hsdlBOQOwHX/XPugTBsGC2MrMLV3jr7yNmudfkx91WapumFdugv/3dSG5PoKgNnkOecwogYtVdlogzIo66ij5mzM6j4kBxGJjWj+yoyMjgG/JDi2ILEYNTpxXIPoRXX4WuEw1TPPH+Dsxvo6fWEArmmRzZACy/jVCdF/dm2ckugFadl0KLQMD3qonLDsDnvPKIJ3q5bt1wJhd4Fk/d4wtWYNDFEr8t9H0AQczMpDi8rghi/mlWrUxS6tb5uaTe34z4GDzQ716LmLPMA/3JANTIgdb92ImfjI3DHauyW+pgzf5dhYOS57ggUxDOvTMKCChVVZyjurJPGAaR1rYvx2G3R1ovHWSJBojyZd38nBtS2Z2Z6CeifMJRGKDGkUNo8SBAPCo5+IWQJ0EfZlX0i2SoBQE6pGLTvkO/C4wZMkSnzmFSAmy23S9cnHJxpuD4YfdrpxblR28/KnWnlbcwAABZkLJSDmVKhkjKyRdhPsHV1pUg1N48D5spr9t6k80J/YS8vX+AdzxiTwr0WiaMyWL267GZgQ5Z53HDY2USxTI0W8QBj3Kjv8fMZMs36AE/1gCTCq4KQxsjDUWS4ZiW+hcya/+HpGgh9zlwcMKNrqbkqOYPn+I/Q1dn+HkEmlhAOyfvuh8qvsH21eZzNN4AJ88JPGh/WjKglOXI5o3WsNqOwx4+55RlffPWGfDlrnhwK8kql/1CS4WpyTIXjQjlPyqrwpKLEcoIVJIjDFY7s6q2AHz581B//KkFRgKaktSYqYYhrwFQnDveO/btFvtmSidhJxqfkl4wSX8Xv/2yvYGM7ezADz3LCeJX1ZlZJyBNqBoc5rh2uTtOCRfj/9KKGnDbUQauPUoEMjdsJ55G5ZfH1dcI65MDQ/TO+H5qGoL+sTBxEB1Wi5tToL6au18iCsRl4PDzJvM02MIgm6 Y3GLxn9+ IHkbAnC/0DQcPQRPlnDuchR/DNxWowII3oVmRV3lmiwltx8ZjQJ84+/Xwb29cf/PJeuhAD4fT2E8+0WlvQb1FBQ9U5/qpwu9QWFXHRDeUa+QWQ45EmjkEq7Hw0VglDVLeVKmWDDBGHJwZUVE4Y3ZpLme/A9BfA7l6dOPoriUqQTT69M/nqdAB5Z5W5d5mH5/wrFzIRxwo55HqckoW+RlmXuItJ5pYGxzBUGs4Ev+JyzHW32M= 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 introduces zswap_batch_compress() that takes an index within a folio, and sets up a request chain for compressing multiple pages of that folio, as a batch. The call to the crypto layer is exactly the same as in zswap_compress(), when batch compressing a request chain in zswap_batch_compress(). zswap_store_folio() is modified to detect if the pool's acomp_ctx has more than one "nr_reqs", which will be the case if the CPU onlining code has allocated multiple batching resources in the acomp_ctx. If so, it means compress batching can be used with a batch-size of "acomp_ctx->nr_reqs". If compress batching can be used, zswap_store_folio() will invoke zswap_batch_compress() to compress and store the folio in batches of "acomp_ctx->nr_reqs" pages. With Intel IAA, the iaa_crypto driver will compress each batch of pages in parallel in hardware. Hence, zswap_batch_compress() does the same computes for a batch, as zswap_compress() does for a page; and returns true if the batch was successfully compressed/stored, and false otherwise. If the pool does not support compress batching, or the folio has only one page, zswap_store_folio() calls zswap_compress() for each individual page in the folio, as before. Signed-off-by: Kanchana P Sridhar --- mm/zswap.c | 296 ++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 224 insertions(+), 72 deletions(-) diff --git a/mm/zswap.c b/mm/zswap.c index ab9167220cb6..626574bd84f6 100644 --- a/mm/zswap.c +++ b/mm/zswap.c @@ -1051,9 +1051,9 @@ static void acomp_ctx_put_unlock(struct crypto_acomp_ctx *acomp_ctx) } static bool zswap_compress(struct page *page, struct zswap_entry *entry, - struct zswap_pool *pool) + struct zswap_pool *pool, + struct crypto_acomp_ctx *acomp_ctx) { - struct crypto_acomp_ctx *acomp_ctx; struct scatterlist input, output; int comp_ret = 0, alloc_ret = 0; unsigned int dlen = PAGE_SIZE; @@ -1063,7 +1063,8 @@ static bool zswap_compress(struct page *page, struct zswap_entry *entry, gfp_t gfp; u8 *dst; - acomp_ctx = acomp_ctx_get_cpu_lock(pool); + lockdep_assert_held(&acomp_ctx->mutex); + dst = acomp_ctx->buffers[0]; sg_init_table(&input, 1); sg_set_page(&input, page, PAGE_SIZE, 0); @@ -1091,7 +1092,7 @@ static bool zswap_compress(struct page *page, struct zswap_entry *entry, 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; + goto check_errors; zpool = pool->zpool; gfp = __GFP_NORETRY | __GFP_NOWARN | __GFP_KSWAPD_RECLAIM; @@ -1099,7 +1100,7 @@ static bool zswap_compress(struct page *page, struct zswap_entry *entry, gfp |= __GFP_HIGHMEM | __GFP_MOVABLE; alloc_ret = zpool_malloc(zpool, dlen, gfp, &handle); if (alloc_ret) - goto unlock; + goto check_errors; buf = zpool_map_handle(zpool, handle, ZPOOL_MM_WO); memcpy(buf, dst, dlen); @@ -1108,7 +1109,7 @@ static bool zswap_compress(struct page *page, struct zswap_entry *entry, entry->handle = handle; entry->length = dlen; -unlock: +check_errors: if (comp_ret == -ENOSPC || alloc_ret == -ENOSPC) zswap_reject_compress_poor++; else if (comp_ret) @@ -1116,7 +1117,6 @@ static bool zswap_compress(struct page *page, struct zswap_entry *entry, else if (alloc_ret) zswap_reject_alloc_fail++; - acomp_ctx_put_unlock(acomp_ctx); return comp_ret == 0 && alloc_ret == 0; } @@ -1580,6 +1580,106 @@ static void shrink_worker(struct work_struct *w) * main API **********************************/ +/* + * Batch compress multiple @nr_pages in @folio, starting from @index. + */ +static bool zswap_batch_compress(struct folio *folio, + long index, + unsigned int nr_pages, + struct zswap_entry *entries[], + struct zswap_pool *pool, + struct crypto_acomp_ctx *acomp_ctx) +{ + struct scatterlist inputs[ZSWAP_MAX_BATCH_SIZE]; + struct scatterlist outputs[ZSWAP_MAX_BATCH_SIZE]; + unsigned int i; + int err = 0; + + lockdep_assert_held(&acomp_ctx->mutex); + + for (i = 0; i < nr_pages; ++i) { + struct page *page = folio_page(folio, index + i); + + sg_init_table(&inputs[i], 1); + sg_set_page(&inputs[i], page, PAGE_SIZE, 0); + + /* + * Each dst buffer should be of size (PAGE_SIZE * 2). + * Reflect same in sg_list. + */ + sg_init_one(&outputs[i], acomp_ctx->buffers[i], PAGE_SIZE * 2); + acomp_request_set_params(acomp_ctx->reqs[i], &inputs[i], + &outputs[i], PAGE_SIZE, PAGE_SIZE); + + /* Use acomp request chaining. */ + if (i) + acomp_request_chain(acomp_ctx->reqs[i], acomp_ctx->reqs[0]); + else + acomp_reqchain_init(acomp_ctx->reqs[0], 0, crypto_req_done, + &acomp_ctx->wait); + } + + err = crypto_wait_req(crypto_acomp_compress(acomp_ctx->reqs[0]), &acomp_ctx->wait); + + /* + * Get the individual compress errors from request chaining. + */ + for (i = 0; i < nr_pages; ++i) { + if (unlikely(acomp_request_err(acomp_ctx->reqs[i]))) { + err = -EINVAL; + if (acomp_request_err(acomp_ctx->reqs[i]) == -ENOSPC) + zswap_reject_compress_poor++; + else + zswap_reject_compress_fail++; + } + } + + if (likely(!err)) { + /* + * All batch pages were successfully compressed. + * Store the pages in zpool. + */ + struct zpool *zpool = pool->zpool; + gfp_t gfp = __GFP_NORETRY | __GFP_NOWARN | __GFP_KSWAPD_RECLAIM; + + if (zpool_malloc_support_movable(zpool)) + gfp |= __GFP_HIGHMEM | __GFP_MOVABLE; + + for (i = 0; i < nr_pages; ++i) { + unsigned long handle; + char *buf; + + err = zpool_malloc(zpool, acomp_ctx->reqs[i]->dlen, gfp, &handle); + + if (err) { + if (err == -ENOSPC) + zswap_reject_compress_poor++; + else + zswap_reject_alloc_fail++; + + break; + } + + buf = zpool_map_handle(zpool, handle, ZPOOL_MM_WO); + memcpy(buf, acomp_ctx->buffers[i], acomp_ctx->reqs[i]->dlen); + zpool_unmap_handle(zpool, handle); + + entries[i]->handle = handle; + entries[i]->length = acomp_ctx->reqs[i]->dlen; + } + } + + /* + * Request chaining cleanup: + * + * - Clear the CRYPTO_TFM_REQ_CHAIN bit on acomp_ctx->reqs[0]. + * - Reset the acomp_ctx->wait to notify acomp_ctx->reqs[0]. + */ + acomp_reqchain_clear(acomp_ctx->reqs[0], &acomp_ctx->wait); + + return !err; +} + /* * Store all pages in a folio. * @@ -1588,95 +1688,146 @@ static void shrink_worker(struct work_struct *w) * handles to ERR_PTR(-EINVAL) at allocation time, and the fact that the * entry's handle is subsequently modified only upon a successful zpool_malloc() * after the page is compressed. + * + * For compressors that don't support batching, the following structure + * showed a performance regression with zstd using 64K as well as 2M folios: + * + * Batched stores: + * --------------- + * - Allocate all entries, + * - Compress all entries, + * - Store all entries in xarray/LRU. + * + * Hence, the above structure is maintained only for batched stores, and the + * following structure is implemented for sequential stores of large folio pages, + * that fixes the regression, while preserving common code paths for batched + * and sequential stores of a folio: + * + * Sequential stores: + * ------------------ + * For each page in folio: + * - allocate an entry, + * - compress the page, + * - store the entry in xarray/LRU. */ static bool zswap_store_folio(struct folio *folio, struct obj_cgroup *objcg, struct zswap_pool *pool) { - long index, from_index = 0, nr_pages = folio_nr_pages(folio); + long index = 0, from_index = 0, nr_pages, nr_folio_pages = folio_nr_pages(folio); struct zswap_entry **entries = NULL; + struct crypto_acomp_ctx *acomp_ctx; int node_id = folio_nid(folio); + unsigned int batch_size; + bool batching; - entries = kmalloc(nr_pages * sizeof(*entries), GFP_KERNEL); + entries = kmalloc(nr_folio_pages * sizeof(*entries), GFP_KERNEL); if (!entries) return false; - for (index = from_index; index < nr_pages; ++index) { - entries[index] = zswap_entry_cache_alloc(GFP_KERNEL, node_id); + acomp_ctx = acomp_ctx_get_cpu_lock(pool); - if (!entries[index]) { - zswap_reject_kmemcache_fail++; - nr_pages = index; - goto store_folio_failed; - } + batch_size = acomp_ctx->nr_reqs; - entries[index]->handle = (unsigned long)ERR_PTR(-EINVAL); - } + nr_pages = (batch_size > 1) ? nr_folio_pages : 1; + batching = (nr_pages > 1) ? true : false; - for (index = from_index; index < nr_pages; ++index) { - struct page *page = folio_page(folio, index); - swp_entry_t page_swpentry = page_swap_entry(page); - struct zswap_entry *old, *entry = entries[index]; + while (1) { + for (index = from_index; index < nr_pages; ++index) { + entries[index] = zswap_entry_cache_alloc(GFP_KERNEL, node_id); - if (!zswap_compress(page, entry, pool)) { - from_index = index; - goto store_folio_failed; - } + if (!entries[index]) { + zswap_reject_kmemcache_fail++; + nr_pages = index; + goto store_folio_failed; + } - old = xa_store(swap_zswap_tree(page_swpentry), - swp_offset(page_swpentry), - entry, GFP_KERNEL); - if (xa_is_err(old)) { - int err = xa_err(old); + entries[index]->handle = (unsigned long)ERR_PTR(-EINVAL); + } - WARN_ONCE(err != -ENOMEM, "unexpected xarray error: %d\n", err); - zswap_reject_alloc_fail++; - from_index = index; - goto store_folio_failed; + if (batching) { + /* Batch compress the pages in the folio. */ + for (index = from_index; index < nr_pages; index += batch_size) { + + if (!zswap_batch_compress(folio, index, + min((unsigned int)(nr_pages - index), + batch_size), + &entries[index], pool, acomp_ctx)) + goto store_folio_failed; + } + } else { + /* Sequential compress the next page in the folio. */ + struct page *page = folio_page(folio, from_index); + + if (!zswap_compress(page, entries[from_index], pool, acomp_ctx)) + goto store_folio_failed; } - /* - * We may have had an existing entry that became stale when - * the folio was redirtied and now the new version is being - * swapped out. Get rid of the old. - */ - if (old) - zswap_entry_free(old); + for (index = from_index; index < nr_pages; ++index) { + swp_entry_t page_swpentry = page_swap_entry(folio_page(folio, index)); + struct zswap_entry *old, *entry = entries[index]; - /* - * The entry is successfully compressed and stored in the tree, there is - * no further possibility of failure. Grab refs to the pool and objcg, - * charge zswap memory, and increment zswap_stored_pages. - * The opposite actions will be performed by zswap_entry_free() - * when the entry is removed from the tree. - */ - zswap_pool_get(pool); - if (objcg) { - obj_cgroup_get(objcg); - obj_cgroup_charge_zswap(objcg, entry->length); - } - atomic_long_inc(&zswap_stored_pages); + old = xa_store(swap_zswap_tree(page_swpentry), + swp_offset(page_swpentry), + entry, GFP_KERNEL); + if (xa_is_err(old)) { + int err = xa_err(old); - /* - * We finish initializing the entry while it's already in xarray. - * This is safe because: - * - * 1. Concurrent stores and invalidations are excluded by folio lock. - * - * 2. Writeback is excluded by the entry not being on the LRU yet. - * The publishing order matters to prevent writeback from seeing - * an incoherent entry. - */ - entry->pool = pool; - entry->swpentry = page_swpentry; - entry->objcg = objcg; - entry->referenced = true; - if (entry->length) { - INIT_LIST_HEAD(&entry->lru); - zswap_lru_add(&zswap_list_lru, entry); + WARN_ONCE(err != -ENOMEM, "unexpected xarray error: %d\n", err); + zswap_reject_alloc_fail++; + from_index = index; + goto store_folio_failed; + } + + /* + * We may have had an existing entry that became stale when + * the folio was redirtied and now the new version is being + * swapped out. Get rid of the old. + */ + if (old) + zswap_entry_free(old); + + /* + * The entry is successfully compressed and stored in the tree, there is + * no further possibility of failure. Grab refs to the pool and objcg, + * charge zswap memory, and increment zswap_stored_pages. + * The opposite actions will be performed by zswap_entry_free() + * when the entry is removed from the tree. + */ + zswap_pool_get(pool); + if (objcg) { + obj_cgroup_get(objcg); + obj_cgroup_charge_zswap(objcg, entry->length); + } + atomic_long_inc(&zswap_stored_pages); + + /* + * We finish initializing the entry while it's already in xarray. + * This is safe because: + * + * 1. Concurrent stores and invalidations are excluded by folio lock. + * + * 2. Writeback is excluded by the entry not being on the LRU yet. + * The publishing order matters to prevent writeback from seeing + * an incoherent entry. + */ + entry->pool = pool; + entry->swpentry = page_swpentry; + entry->objcg = objcg; + entry->referenced = true; + if (entry->length) { + INIT_LIST_HEAD(&entry->lru); + zswap_lru_add(&zswap_list_lru, entry); + } } + + from_index = nr_pages++; + + if (nr_pages > nr_folio_pages) + break; } + acomp_ctx_put_unlock(acomp_ctx); kfree(entries); return true; @@ -1688,6 +1839,7 @@ static bool zswap_store_folio(struct folio *folio, zswap_entry_cache_free(entries[index]); } + acomp_ctx_put_unlock(acomp_ctx); kfree(entries); return false; }