crypto: qat - re-enable algorithms
Subsystems (2)
v3 cover crypto: qat - re-enable algorithms
This set is an extension of a previous set called crypto: qat - fix dm-crypt related issues which aims to re-enable the algorithms in the QAT driver after [1].
This fixes a number of issues with the implementation of the QAT algs, both symmetric and asymmetric.
In particular this set enables the QAT driver to handle correctly the flags CRYPTO_TFM_REQ_MAY_BACKLOG and CRYPTO_TFM_REQ_MAY_SLEEP, fixes an hidden issue in RSA and DH which appeared after commit f5ff79fddf0e, related to the usage of dma_free_coherent() from a tasklet, and includes important fixes in the akcipher algorithms.
One item to mention is that, differently from the previous set, this one does not removes the flag CRYPTO_ALG_ALLOCATES_MEMORY which will be removed after the conversation in [2] is closed.
[1] https://lore.kernel.org/linux-crypto/YiEyGoHacN80FcOL@silpixa00400314/
[2] https://lore.kernel.org/linux-crypto/Yl6PlqyucVLCzwF5@silpixa00400314/
Changes from v2:
- Removed crypto: qat - set to zero DH parameters before free from set.
- Added fixes tags to patches crypto: qat - add param check for RSA and crypto: qat - add param check for DH
Changes from v1:
- Clarified commit message in crypto: qat - refactor submission logic to indicate why the patch should be included in stable kernels
- Removed crypto: qat - use memzero_explicit() for algs from set after feedback from Greg KH
- Replaced memzero_explicit() with memset() in crypto: qat - set to zero DH parameters before free after feedback from Greg KH
Giovanni Cabiddu (10):
crypto: qat - use pre-allocated buffers in datapath
crypto: qat - refactor submission logic
crypto: qat - add backlog mechanism
crypto: qat - fix memory leak in RSA
crypto: qat - remove dma_free_coherent() for RSA
crypto: qat - remove dma_free_coherent() for DH
crypto: qat - add param check for RSA
crypto: qat - add param check for DH
crypto: qat - honor CRYPTO_TFM_REQ_MAY_SLEEP flag
crypto: qat - re-enable registration of algorithms
drivers/crypto/qat/qat_4xxx/adf_drv.c | 7 - drivers/crypto/qat/qat_common/Makefile | 1 + drivers/crypto/qat/qat_common/adf_transport.c | 11 + drivers/crypto/qat/qat_common/adf_transport.h | 1 + .../qat/qat_common/adf_transport_internal.h | 1 + drivers/crypto/qat/qat_common/qat_algs.c | 153 +++++---- drivers/crypto/qat/qat_common/qat_algs_send.c | 86 +++++ drivers/crypto/qat/qat_common/qat_algs_send.h | 11 + drivers/crypto/qat/qat_common/qat_asym_algs.c | 304 +++++++++--------- drivers/crypto/qat/qat_common/qat_crypto.c | 10 +- drivers/crypto/qat/qat_common/qat_crypto.h | 44 +++ 11 files changed, 392 insertions(+), 237 deletions(-) create mode 100644 drivers/crypto/qat/qat_common/qat_algs_send.c create mode 100644 drivers/crypto/qat/qat_common/qat_algs_send.h
Signature · 1 line
2.35.1
PATCH v3 1/10 crypto: qat - use pre-allocated buffers in datapath
In order to do DMAs, the QAT device requires that the scatterlist structures are mapped and translated into a format that the firmware can understand. This is defined as the composition of a scatter gather list (SGL) descriptor header, the struct qat_alg_buf_list, plus a variable number of flat buffer descriptors, the struct qat_alg_buf.
The allocation and mapping of these data structures is done each time a request is received from the skcipher and aead APIs.
In an OOM situation, this behaviour might lead to a dead-lock if an allocation fails.
Based on the conversation in [1], increase the size of the aead and skcipher request contexts to include an SGL descriptor that can handle a maximum of 4 flat buffers.
If requests exceed 4 entries buffers, memory is allocated dynamically.
[1] https://lore.kernel.org/linux-crypto/[email protected]/
--- drivers/crypto/qat/qat_common/qat_algs.c | 64 +++++++++++++--------- drivers/crypto/qat/qat_common/qat_crypto.h | 24 ++++++++ 2 files changed, 61 insertions(+), 27 deletions(-)
drivers/crypto/qat/qat_common/qat_algs.c +37 -27
@@ -46,19 +46,6 @@static DEFINE_MUTEX (algs_lock);static unsigned int active_devs; -struct qat_alg_buf { -u32 len; -u32 resrvd; -u64 addr; -} __packed; - -struct qat_alg_buf_list { -u64 resrvd; -u32 num_bufs; -u32 num_mapped_bufs; -struct qat_alg_buf bufers[]; -} __packed__aligned(64) ; -/* Common content descriptor */ struct qat_alg_cd { union {
@@ -693,7 +680,10 @@ static void qat_alg_free_bufl(struct qat_crypto_instance *inst, bl->bufers[i].len, DMA_BIDIRECTIONAL);dma_unmap_single (dev, blp, sz,DMA_TO_DEVICE ); -kfree (bl); + +if (!qat_req->buf.sgl_src_valid) +kfree (bl); +if (blp != blpout) {/* If out of place operation dma unmap only data */ int bufless = blout->num_bufs - blout->num_mapped_bufs;
@@ -704,7 +694,9 @@ static void qat_alg_free_bufl(struct qat_crypto_instance *inst, DMA_BIDIRECTIONAL); }dma_unmap_single (dev, blpout, sz_out,DMA_TO_DEVICE ); -kfree (blout); + +if (!qat_req->buf.sgl_dst_valid) +kfree (blout); } }
@@ -721,15 +713,24 @@ static int qat_alg_sgl_to_bufl(struct qat_crypto_instance *inst,dma_addr_t blp =DMA_MAPPING_ERROR ;dma_addr_t bloutp =DMA_MAPPING_ERROR ;struct scatterlist *sg; -size_t sz_out, sz =struct_size (bufl, bufers, n +1 ); +size_t sz_out, sz =struct_size (bufl, bufers, n); +int node =dev_to_node (&GET_DEV (inst->accel_dev));if (unlikely (!n))return -EINVAL ; - bufl =kzalloc_node (sz,GFP_ATOMIC , -dev_to_node (&GET_DEV (inst->accel_dev))); -if (unlikely (!bufl)) -return -ENOMEM ; + qat_req->buf.sgl_src_valid = false; + qat_req->buf.sgl_dst_valid = false; + +if (n >QAT_MAX_BUFF_DESC ) { + bufl =kzalloc_node (sz,GFP_ATOMIC , node); +if (unlikely (!bufl)) +return -ENOMEM ; + }else { + bufl = &qat_req->buf.sgl_src.sgl_hdr; +memset (bufl,0 ,sizeof (struct qat_alg_buf_list )); + qat_req->buf.sgl_src_valid = true; + }for_each_sg (sgl, sg, n, i) bufl->bufers[i].addr =DMA_MAPPING_ERROR ;
@@ -760,12 +761,18 @@ static int qat_alg_sgl_to_bufl(struct qat_crypto_instance *inst,struct qat_alg_buf *bufers; n =sg_nents (sglout); - sz_out =struct_size (buflout, bufers, n +1 ); + sz_out =struct_size (buflout, bufers, n); sg_nctr =0 ; - buflout =kzalloc_node (sz_out,GFP_ATOMIC , -dev_to_node (&GET_DEV (inst->accel_dev))); -if (unlikely (!buflout)) - goto err_in; + +if (n >QAT_MAX_BUFF_DESC ) { + buflout =kzalloc_node (sz_out,GFP_ATOMIC , node); +if (unlikely (!buflout)) + goto err_in; + }else { + buflout = &qat_req->buf.sgl_dst.sgl_hdr; +memset (buflout,0 ,sizeof (struct qat_alg_buf_list )); + qat_req->buf.sgl_dst_valid = true; + } bufers = buflout->bufers;for_each_sg (sglout, sg, n, i)
@@ -810,7 +817,9 @@ static int qat_alg_sgl_to_bufl(struct qat_crypto_instance *inst,dma_unmap_single (dev, buflout->bufers[i].addr, buflout->bufers[i].len,DMA_BIDIRECTIONAL ); -kfree (buflout); + +if (!qat_req->buf.sgl_dst_valid) +kfree (buflout); err_in:if (!dma_mapping_error (dev, blp))
@@ -823,7 +832,8 @@ static int qat_alg_sgl_to_bufl(struct qat_crypto_instance *inst, bufl->bufers[i].len, DMA_BIDIRECTIONAL); -kfree (bufl); +if (!qat_req->buf.sgl_src_valid) +kfree (bufl);dev_err (dev,"Failed to map buf for dma\n" );return -ENOMEM ;
drivers/crypto/qat/qat_common/qat_crypto.h +24 -0
@@ -21,6 +21,26 @@ struct qat_crypto_instance {atomic_t refctr; }; +#define QAT_MAX_BUFF_DESC 4 + +struct qat_alg_buf { +u32 len; +u32 resrvd; +u64 addr; +} __packed; + +struct qat_alg_buf_list { +u64 resrvd; +u32 num_bufs; +u32 num_mapped_bufs; +struct qat_alg_buf bufers[]; +} __packed; + +struct qat_alg_fixed_buf_list { +struct qat_alg_buf_list sgl_hdr; +struct qat_alg_buf descriptors[QAT_MAX_BUFF_DESC ]; +} __packed__aligned(64) ; +struct qat_crypto_request_buffs {struct qat_alg_buf_list *bl;dma_addr_t blp;
@@ -28,6 +48,10 @@ struct qat_crypto_request_buffs {dma_addr_t bloutp;size_t sz;size_t sz_out; +bool sgl_src_valid; +bool sgl_dst_valid; +struct qat_alg_fixed_buf_list sgl_src; +struct qat_alg_fixed_buf_list sgl_dst; };struct qat_crypto_request ;
Signature · 1 line
2.35.1
PATCH v3 2/10 crypto: qat - refactor submission logic
In reply to Giovanni CabidduAll the algorithms in qat_algs.c and qat_asym_algs.c use the same pattern to submit messages to the HW queues. Move the submission loop to a new function, qat_alg_send_message(), and share it between the symmetric and the asymmetric algorithms.
As part of this rework, since the number of retries before returning an error is inconsistent between the symmetric and asymmetric implementations, set it to a value that works for both (i.e. 20, was 10 in qat_algs.c and 100 in qat_asym_algs.c)
In addition fix the return code reported when the HW queues are full. In that case return -ENOSPC instead of -EBUSY.
Including stable in CC since (1) the error code returned if the HW queues are full is incorrect and (2) to facilitate the backport of the next fix "crypto: qat - add backlog mechanism".
--- drivers/crypto/qat/qat_common/Makefile | 1 + drivers/crypto/qat/qat_common/qat_algs.c | 68 +++++++++---------- drivers/crypto/qat/qat_common/qat_algs_send.c | 21 ++++++ drivers/crypto/qat/qat_common/qat_algs_send.h | 10 +++ drivers/crypto/qat/qat_common/qat_asym_algs.c | 50 +++++++++----- drivers/crypto/qat/qat_common/qat_crypto.h | 5 ++ 6 files changed, 101 insertions(+), 54 deletions(-) create mode 100644 drivers/crypto/qat/qat_common/qat_algs_send.c create mode 100644 drivers/crypto/qat/qat_common/qat_algs_send.h
drivers/crypto/qat/qat_common/Makefile +1 -0
@@ -16,6 +16,7 @@ intel_qat-objs := adf_cfg.o \ qat_crypto.o \ qat_algs.o \ qat_asym_algs.o \ + qat_algs_send.o \ qat_uclo.o \ qat_hal.o
drivers/crypto/qat/qat_common/qat_algs.c +32 -36
133 lines not on this page · raw message
drivers/crypto/qat/qat_common/qat_algs_send.c new file +21 -0
@@ -0,0 +1,21 @@ +// SPDX-License-Identifier: (BSD-3-Clause OR GPL-2.0-only) +/* Copyright(c) 2022 Intel Corporation */ +#include "adf_transport.h" +#include "qat_algs_send.h" +#include "qat_crypto.h" + +#define ADF_MAX_RETRIES 20 + +int qat_alg_send_message (struct qat_alg_req *req) +{ +int ret =0 , ctr =0 ; + +do { + ret =adf_send_message (req->tx_ring, req->fw_req); + }while (ret == -EAGAIN && ctr++ <ADF_MAX_RETRIES ); + +if (ret == -EAGAIN ) +return -ENOSPC ; + +return -EINPROGRESS ; +}
drivers/crypto/qat/qat_common/qat_algs_send.h new file +10 -0
@@ -0,0 +1,10 @@ +/* SPDX-License-Identifier: (BSD-3-Clause OR GPL-2.0-only) */ +/* Copyright(c) 2022 Intel Corporation */ +#ifndef QAT_ALGS_SEND_H +#define QAT_ALGS_SEND_H + +#include "qat_crypto.h" + +int qat_alg_send_message (struct qat_alg_req *req); + +#endif
drivers/crypto/qat/qat_common/qat_asym_algs.c +32 -18
104 lines not on this page · raw message
drivers/crypto/qat/qat_common/qat_crypto.h +5 -0
@@ -9,6 +9,11 @@#include "adf_accel_devices.h" #include "icp_qat_fw_la.h" +struct qat_alg_req { +u32 *fw_req; +struct adf_etr_ring_data *tx_ring; +}; +struct qat_crypto_instance {struct adf_etr_ring_data *sym_tx;struct adf_etr_ring_data *sym_rx;
Signature · 1 line
2.35.1
PATCH v3 3/10 crypto: qat - add backlog mechanism
In reply to Giovanni CabidduThe implementations of the crypto algorithms (aead, skcipher, etc) in the QAT driver do not properly support requests with the CRYPTO_TFM_REQ_MAY_BACKLOG flag set. If the HW queue is full, the driver returns -EBUSY but does not enqueue the request. This can result in applications like dm-crypt waiting indefinitely for the completion of a request that was never submitted to the hardware.
Fix this by adding a software backlog queue: if the ring buffer is more than eighty percent full, then the request is enqueued to a backlog list and the error code -EBUSY is returned back to the caller. Requests in the backlog queue are resubmitted at a later time, in the context of the callback of a previously submitted request. The request for which -EBUSY is returned is then marked as -EINPROGRESS once submitted to the HW queues.
The submission loop inside the function qat_alg_send_message() has been modified to decide which submission policy to use based on the request flags. If the request does not have the CRYPTO_TFM_REQ_MAY_BACKLOG set, the previous behaviour has been preserved.
Based on a patch by
Vishnu Das Ramachandran <[email protected]>
--- drivers/crypto/qat/qat_common/adf_transport.c | 11 +++ drivers/crypto/qat/qat_common/adf_transport.h | 1 + .../qat/qat_common/adf_transport_internal.h | 1 + drivers/crypto/qat/qat_common/qat_algs.c | 24 ++++--- drivers/crypto/qat/qat_common/qat_algs_send.c | 67 ++++++++++++++++++- drivers/crypto/qat/qat_common/qat_algs_send.h | 1 + drivers/crypto/qat/qat_common/qat_asym_algs.c | 23 ++++--- drivers/crypto/qat/qat_common/qat_crypto.c | 3 + drivers/crypto/qat/qat_common/qat_crypto.h | 10 +++ 9 files changed, 123 insertions(+), 18 deletions(-)
drivers/crypto/qat/qat_common/adf_transport.c +11 -0
@@ -8,6 +8,9 @@#include "adf_cfg.h" #include "adf_common_drv.h" +#define ADF_MAX_RING_THRESHOLD 80 +#define ADF_PERCENT (tot, percent) (((tot) * (percent)) / 100) +static inline u32 adf_modulo (u32 data,u32 shift) {u32 div = data >> shift;
@@ -77,6 +80,11 @@ static void adf_disable_ring_irq(struct adf_etr_bank_data *bank, u32 ring) bank->irq_mask); } +bool adf_ring_nearly_full (struct adf_etr_ring_data *ring) +{ +return atomic_read (ring->inflights) > ring->threshold; +} +int adf_send_message (struct adf_etr_ring_data *ring,u32 *msg) {struct adf_hw_csr_ops *csr_ops =GET_CSR_OPS (ring->bank->accel_dev);
@@ -217,6 +225,7 @@ int adf_create_ring(struct adf_accel_dev *accel_dev, const char *section,struct adf_etr_bank_data *bank;struct adf_etr_ring_data *ring;char val[ADF_CFG_MAX_VAL_LEN_IN_BYTES ]; +int max_inflights;u32 ring_num;int ret;
@@ -263,6 +272,8 @@ int adf_create_ring(struct adf_accel_dev *accel_dev, const char *section, ring->ring_size =adf_verify_ring_size (msg_size, num_msgs); ring->head =0 ; ring->tail =0 ; + max_inflights =ADF_MAX_INFLIGHTS (ring->ring_size, ring->msg_size); + ring->threshold =ADF_PERCENT (max_inflights,ADF_MAX_RING_THRESHOLD );atomic_set (ring->inflights,0 ); ret =adf_init_ring (ring);if (ret)
drivers/crypto/qat/qat_common/adf_transport.h +1 -0
@@ -14,6 +14,7 @@ int adf_create_ring(struct adf_accel_dev *accel_dev, const char *section, const char *ring_name, adf_callback_fn callback, int poll_mode, struct adf_etr_ring_data **ring_ptr); +bool adf_ring_nearly_full (struct adf_etr_ring_data *ring);int adf_send_message (struct adf_etr_ring_data *ring,u32 *msg);void adf_remove_ring (struct adf_etr_ring_data *ring);#endif
drivers/crypto/qat/qat_common/adf_transport_internal.h +1 -0
@@ -22,6 +22,7 @@ struct adf_etr_ring_data {spinlock_t lock;/* protects ring data struct */ u16 head;u16 tail; +u32 threshold;u8 ring_number;u8 ring_size;u8 msg_size;
drivers/crypto/qat/qat_common/qat_algs.c +15 -9
@@ -935,19 +935,25 @@ void qat_alg_callback(void *resp)struct icp_qat_fw_la_resp *qat_resp = resp;struct qat_crypto_request *qat_req = (void *)(__force long )qat_resp->opaque_data; +struct qat_instance_backlog *backlog = qat_req->alg_req.backlog; qat_req->cb (qat_resp, qat_req); + +qat_alg_send_backlog (backlog); }static int qat_alg_send_sym_message (struct qat_crypto_request *qat_req, -struct qat_crypto_instance *inst) +struct qat_crypto_instance *inst, +struct crypto_async_request *base) { -struct qat_alg_req req; +struct qat_alg_req *alg_req = &qat_req->alg_req; - req.fw_req = (u32 *)&qat_req->req; - req.tx_ring = inst->sym_tx; + alg_req->fw_req = (u32 *)&qat_req->req; + alg_req->tx_ring = inst->sym_tx; + alg_req->base = base; + alg_req->backlog = &inst->backlog; -return qat_alg_send_message (&req); +return qat_alg_send_message (alg_req); }static int qat_alg_aead_dec (struct aead_request *areq)
@@ -987,7 +993,7 @@ static int qat_alg_aead_dec(struct aead_request *areq) auth_param->auth_off =0 ; auth_param->auth_len = areq->assoclen + cipher_param->cipher_length; - ret =qat_alg_send_sym_message (qat_req, ctx->inst); + ret =qat_alg_send_sym_message (qat_req, ctx->inst, &areq->base);if (ret == -ENOSPC )qat_alg_free_bufl (ctx->inst, qat_req);
@@ -1031,7 +1037,7 @@ static int qat_alg_aead_enc(struct aead_request *areq) auth_param->auth_off =0 ; auth_param->auth_len = areq->assoclen + areq->cryptlen; - ret =qat_alg_send_sym_message (qat_req, ctx->inst); + ret =qat_alg_send_sym_message (qat_req, ctx->inst, &areq->base);if (ret == -ENOSPC )qat_alg_free_bufl (ctx->inst, qat_req);
@@ -1212,7 +1218,7 @@ static int qat_alg_skcipher_encrypt(struct skcipher_request *req)qat_alg_set_req_iv (qat_req); - ret =qat_alg_send_sym_message (qat_req, ctx->inst); + ret =qat_alg_send_sym_message (qat_req, ctx->inst, &req->base);if (ret == -ENOSPC )qat_alg_free_bufl (ctx->inst, qat_req);
@@ -1278,7 +1284,7 @@ static int qat_alg_skcipher_decrypt(struct skcipher_request *req)qat_alg_set_req_iv (qat_req);qat_alg_update_iv (qat_req); - ret =qat_alg_send_sym_message (qat_req, ctx->inst); + ret =qat_alg_send_sym_message (qat_req, ctx->inst, &req->base);if (ret == -ENOSPC )qat_alg_free_bufl (ctx->inst, qat_req);
drivers/crypto/qat/qat_common/qat_algs_send.c +66 -1
76 lines not on this page · raw message
drivers/crypto/qat/qat_common/qat_algs_send.h +1 -0
@@ -6,5 +6,6 @@#include "qat_crypto.h" int qat_alg_send_message (struct qat_alg_req *req); +void qat_alg_send_backlog (struct qat_instance_backlog *backlog);#endif
drivers/crypto/qat/qat_common/qat_asym_algs.c +15 -8
@@ -136,17 +136,21 @@ struct qat_asym_request { } areq;int err;void (*cb)(struct icp_qat_fw_pke_resp *resp); +struct qat_alg_req alg_req; }__aligned(64) ;static int qat_alg_send_asym_message (struct qat_asym_request *qat_req, -struct qat_crypto_instance *inst) +struct qat_crypto_instance *inst, +struct crypto_async_request *base) { -struct qat_alg_req req; +struct qat_alg_req *alg_req = &qat_req->alg_req; - req.fw_req = (u32 *)&qat_req->req; - req.tx_ring = inst->pke_tx; + alg_req->fw_req = (u32 *)&qat_req->req; + alg_req->tx_ring = inst->pke_tx; + alg_req->base = base; + alg_req->backlog = &inst->backlog; -return qat_alg_send_message (&req); +return qat_alg_send_message (alg_req); }static void qat_dh_cb (struct icp_qat_fw_pke_resp *resp)
@@ -350,7 +354,7 @@ static int qat_dh_compute_value(struct kpp_request *req) msg->input_param_count = n_input_params; msg->output_param_count =1 ; - ret =qat_alg_send_asym_message (qat_req, ctx->inst); + ret =qat_alg_send_asym_message (qat_req, inst, &req->base);if (ret == -ENOSPC ) goto unmap_all;
@@ -554,8 +558,11 @@ void qat_alg_asym_callback(void *_resp) {struct icp_qat_fw_pke_resp *resp = _resp;struct qat_asym_request *areq = (void *)(__force long )resp->opaque; +struct qat_instance_backlog *backlog = areq->alg_req.backlog; areq->cb (resp); + +qat_alg_send_backlog (backlog); }#define PKE_RSA_EP_512 0x1c161b21
@@ -745,7 +752,7 @@ static int qat_rsa_enc(struct akcipher_request *req) msg->input_param_count =3 ; msg->output_param_count =1 ; - ret =qat_alg_send_asym_message (qat_req, ctx->inst); + ret =qat_alg_send_asym_message (qat_req, inst, &req->base);if (ret == -ENOSPC ) goto unmap_all;
@@ -898,7 +905,7 @@ static int qat_rsa_dec(struct akcipher_request *req) msg->output_param_count =1 ; - ret =qat_alg_send_asym_message (qat_req, ctx->inst); + ret =qat_alg_send_asym_message (qat_req, inst, &req->base);if (ret == -ENOSPC ) goto unmap_all;
drivers/crypto/qat/qat_common/qat_crypto.c +3 -0
@@ -353,6 +353,9 @@ static int qat_crypto_create_instances(struct adf_accel_dev *accel_dev) &inst->pke_rx);if (ret) goto err; + +INIT_LIST_HEAD (&inst->backlog.list); +spin_lock_init (&inst->backlog.lock); }return 0 ; err:
drivers/crypto/qat/qat_common/qat_crypto.h +10 -0
@@ -9,9 +9,17 @@#include "adf_accel_devices.h" #include "icp_qat_fw_la.h" +struct qat_instance_backlog { +struct list_head list; +spinlock_t lock;/* protects backlog list */ +}; +struct qat_alg_req {u32 *fw_req;struct adf_etr_ring_data *tx_ring; +struct crypto_async_request *base; +struct list_head list; +struct qat_instance_backlog *backlog; };struct qat_crypto_instance {
@@ -24,6 +32,7 @@ struct qat_crypto_instance {unsigned long state;int id;atomic_t refctr; +struct qat_instance_backlog backlog; };#define QAT_MAX_BUFF_DESC 4
@@ -82,6 +91,7 @@ struct qat_crypto_request {u8 iv[AES_BLOCK_SIZE ]; };bool encryption; +struct qat_alg_req alg_req; };static inline bool adf_hw_dev_has_crypto (struct adf_accel_dev *accel_dev)
Signature · 1 line
2.35.1
PATCH v3 4/10 crypto: qat - fix memory leak in RSA
In reply to Giovanni CabidduWhen an RSA key represented in form 2 (as defined in PKCS #1 V2.1) is used, some components of the private key persist even after the TFM is released.
Replace the explicit calls to free the buffers in qat_rsa_exit_tfm() with a call to qat_rsa_clear_ctx() which frees all buffers referenced in the TFM context.
--- drivers/crypto/qat/qat_common/qat_asym_algs.c | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-)
drivers/crypto/qat/qat_common/qat_asym_algs.c +1 -11
@@ -1254,18 +1254,8 @@ static void qat_rsa_exit_tfm(struct crypto_akcipher *tfm)struct qat_rsa_ctx *ctx =akcipher_tfm_ctx (tfm);struct device *dev = &GET_DEV (ctx->inst->accel_dev); -if (ctx->n) -dma_free_coherent (dev, ctx->key_sz, ctx->n, ctx->dma_n); -if (ctx->e) -dma_free_coherent (dev, ctx->key_sz, ctx->e, ctx->dma_e); -if (ctx->d) { -memset (ctx->d,'\0' , ctx->key_sz); -dma_free_coherent (dev, ctx->key_sz, ctx->d, ctx->dma_d); - } +qat_rsa_clear_ctx (dev, ctx);qat_crypto_put_instance (ctx->inst); - ctx->n =NULL ; - ctx->e =NULL ; - ctx->d =NULL ; }static struct akcipher_alg rsa = {
Signature · 1 line
2.35.1
PATCH v3 5/10 crypto: qat - remove dma_free_coherent() for RSA
In reply to Giovanni CabidduAfter commit f5ff79fddf0e ("dma-mapping: remove CONFIG_DMA_REMAP"), if the algorithms are enabled, the driver crashes with a BUG_ON while executing vunmap() in the context of a tasklet. This is due to the fact that the function dma_free_coherent() cannot be called in an interrupt context (see Documentation/core-api/dma-api-howto.rst).
The functions qat_rsa_enc() and qat_rsa_dec() allocate memory with dma_alloc_coherent() if the source or the destination buffers are made of multiple flat buffers or of a size that is not compatible with the hardware.
This memory is then freed with dma_free_coherent() in the context of a tasklet invoked to handle the response for the corresponding request.
Replace allocations with dma_alloc_coherent() in the functions qat_rsa_enc() and qat_rsa_dec() with kmalloc() + dma_map_single().
--- drivers/crypto/qat/qat_common/qat_asym_algs.c | 137 ++++++++---------- 1 file changed, 60 insertions(+), 77 deletions(-)
drivers/crypto/qat/qat_common/qat_asym_algs.c +60 -77
@@ -526,25 +526,22 @@ static void qat_rsa_cb(struct icp_qat_fw_pke_resp *resp) err = (err ==ICP_QAT_FW_COMN_STATUS_FLAG_OK ) ?0 : -EINVAL ; -if (req->src_align) -dma_free_coherent (dev, req->ctx.rsa->key_sz, req->src_align, - req->in.rsa.enc.m); -else -dma_unmap_single (dev, req->in.rsa.enc.m, req->ctx.rsa->key_sz, -DMA_TO_DEVICE ); +kfree_sensitive (req->src_align); + +dma_unmap_single (dev, req->in.rsa.enc.m, req->ctx.rsa->key_sz, +DMA_TO_DEVICE ); areq->dst_len = req->ctx.rsa->key_sz;if (req->dst_align) {scatterwalk_map_and_copy (req->dst_align, areq->dst,0 , areq->dst_len,1 ); -dma_free_coherent (dev, req->ctx.rsa->key_sz, req->dst_align, - req->out.rsa.enc.c); - }else { -dma_unmap_single (dev, req->out.rsa.enc.c, req->ctx.rsa->key_sz, -DMA_FROM_DEVICE ); +kfree_sensitive (req->dst_align); } +dma_unmap_single (dev, req->out.rsa.enc.c, req->ctx.rsa->key_sz, +DMA_FROM_DEVICE ); +dma_unmap_single (dev, req->phy_in,sizeof (struct qat_rsa_input_params ),DMA_TO_DEVICE ); dma_unmap_single(dev, req->phy_out,
@@ -661,6 +658,7 @@ static int qat_rsa_enc(struct akcipher_request *req)struct qat_asym_request *qat_req =PTR_ALIGN (akcipher_request_ctx (req),64 );struct icp_qat_fw_pke_request *msg = &qat_req->req; +u8 *vaddr;int ret;if (unlikely (!ctx->n || !ctx->e))
@@ -698,40 +696,39 @@ static int qat_rsa_enc(struct akcipher_request *req)*/ if (sg_is_last (req->src) && req->src_len == ctx->key_sz) { qat_req->src_align =NULL ; - qat_req->in.rsa.enc.m =dma_map_single (dev,sg_virt (req->src), - req->src_len,DMA_TO_DEVICE ); -if (unlikely (dma_mapping_error (dev, qat_req->in.rsa.enc.m))) -return ret; - + vaddr =sg_virt (req->src); }else {int shift = ctx->key_sz - req->src_len; - qat_req->src_align =dma_alloc_coherent (dev, ctx->key_sz, - &qat_req->in.rsa.enc.m, -GFP_KERNEL ); + qat_req->src_align =kzalloc (ctx->key_sz,GFP_KERNEL );if (unlikely (!qat_req->src_align))return ret;scatterwalk_map_and_copy (qat_req->src_align + shift, req->src,0 , req->src_len,0 ); + vaddr = qat_req->src_align; } -if (sg_is_last (req->dst) && req->dst_len == ctx->key_sz) { - qat_req->dst_align =NULL ; - qat_req->out.rsa.enc.c =dma_map_single (dev,sg_virt (req->dst), - req->dst_len, -DMA_FROM_DEVICE ); -if (unlikely (dma_mapping_error (dev, qat_req->out.rsa.enc.c))) - goto unmap_src; + qat_req->in.rsa.enc.m =dma_map_single (dev, vaddr, ctx->key_sz, +DMA_TO_DEVICE ); +if (unlikely (dma_mapping_error (dev, qat_req->in.rsa.enc.m))) + goto unmap_src; +if (sg_is_last (req->dst) && req->dst_len == ctx->key_sz) { + qat_req->dst_align =NULL ; + vaddr =sg_virt (req->dst); }else { - qat_req->dst_align =dma_alloc_coherent (dev, ctx->key_sz, - &qat_req->out.rsa.enc.c, -GFP_KERNEL ); + qat_req->dst_align =kzalloc (ctx->key_sz,GFP_KERNEL );if (unlikely (!qat_req->dst_align)) goto unmap_src; - + vaddr = qat_req->dst_align; } + + qat_req->out.rsa.enc.c =dma_map_single (dev, vaddr, ctx->key_sz, +DMA_FROM_DEVICE ); +if (unlikely (dma_mapping_error (dev, qat_req->out.rsa.enc.c))) + goto unmap_dst; + qat_req->in.rsa.in_tab[3 ] =0 ; qat_req->out.rsa.out_tab[1 ] =0 ; qat_req->phy_in = dma_map_single(dev, &qat_req->in.rsa.enc.m,
@@ -769,21 +766,15 @@ static int qat_rsa_enc(struct akcipher_request *req)sizeof (struct qat_rsa_input_params ), DMA_TO_DEVICE); unmap_dst: -if (qat_req->dst_align) -dma_free_coherent (dev, ctx->key_sz, qat_req->dst_align, - qat_req->out.rsa.enc.c); -else -if (!dma_mapping_error (dev, qat_req->out.rsa.enc.c)) -dma_unmap_single (dev, qat_req->out.rsa.enc.c, - ctx->key_sz,DMA_FROM_DEVICE ); +if (!dma_mapping_error (dev, qat_req->out.rsa.enc.c)) +dma_unmap_single (dev, qat_req->out.rsa.enc.c, + ctx->key_sz,DMA_FROM_DEVICE ); +kfree_sensitive (qat_req->dst_align); unmap_src: -if (qat_req->src_align) -dma_free_coherent (dev, ctx->key_sz, qat_req->src_align, - qat_req->in.rsa.enc.m); -else -if (!dma_mapping_error (dev, qat_req->in.rsa.enc.m)) -dma_unmap_single (dev, qat_req->in.rsa.enc.m, - ctx->key_sz,DMA_TO_DEVICE ); +if (!dma_mapping_error (dev, qat_req->in.rsa.enc.m)) +dma_unmap_single (dev, qat_req->in.rsa.enc.m, ctx->key_sz, +DMA_TO_DEVICE ); +kfree_sensitive (qat_req->src_align);return ret; }
@@ -796,6 +787,7 @@ static int qat_rsa_dec(struct akcipher_request *req)struct qat_asym_request *qat_req =PTR_ALIGN (akcipher_request_ctx (req),64 );struct icp_qat_fw_pke_request *msg = &qat_req->req; +u8 *vaddr;int ret;if (unlikely (!ctx->n || !ctx->d))
@@ -843,40 +835,37 @@ static int qat_rsa_dec(struct akcipher_request *req)*/ if (sg_is_last (req->src) && req->src_len == ctx->key_sz) { qat_req->src_align =NULL ; - qat_req->in.rsa.dec.c =dma_map_single (dev,sg_virt (req->src), - req->dst_len,DMA_TO_DEVICE ); -if (unlikely (dma_mapping_error (dev, qat_req->in.rsa.dec.c))) -return ret; - + vaddr =sg_virt (req->src); }else {int shift = ctx->key_sz - req->src_len; - qat_req->src_align =dma_alloc_coherent (dev, ctx->key_sz, - &qat_req->in.rsa.dec.c, -GFP_KERNEL ); + qat_req->src_align =kzalloc (ctx->key_sz,GFP_KERNEL );if (unlikely (!qat_req->src_align))return ret;scatterwalk_map_and_copy (qat_req->src_align + shift, req->src,0 , req->src_len,0 ); + vaddr = qat_req->src_align; } -if (sg_is_last (req->dst) && req->dst_len == ctx->key_sz) { - qat_req->dst_align =NULL ; - qat_req->out.rsa.dec.m =dma_map_single (dev,sg_virt (req->dst), - req->dst_len, -DMA_FROM_DEVICE ); -if (unlikely (dma_mapping_error (dev, qat_req->out.rsa.dec.m))) - goto unmap_src; + qat_req->in.rsa.dec.c =dma_map_single (dev, vaddr, ctx->key_sz, +DMA_TO_DEVICE ); +if (unlikely (dma_mapping_error (dev, qat_req->in.rsa.dec.c))) + goto unmap_src; +if (sg_is_last (req->dst) && req->dst_len == ctx->key_sz) { + qat_req->dst_align =NULL ; + vaddr =sg_virt (req->dst); }else { - qat_req->dst_align =dma_alloc_coherent (dev, ctx->key_sz, - &qat_req->out.rsa.dec.m, -GFP_KERNEL ); + qat_req->dst_align =kzalloc (ctx->key_sz,GFP_KERNEL );if (unlikely (!qat_req->dst_align)) goto unmap_src; - + vaddr = qat_req->dst_align; } + qat_req->out.rsa.dec.m =dma_map_single (dev, vaddr, ctx->key_sz, +DMA_FROM_DEVICE ); +if (unlikely (dma_mapping_error (dev, qat_req->out.rsa.dec.m))) + goto unmap_dst;if (ctx->crt_mode) qat_req->in.rsa.in_tab[6 ] =0 ;
@@ -922,21 +911,15 @@ static int qat_rsa_dec(struct akcipher_request *req)sizeof (struct qat_rsa_input_params ), DMA_TO_DEVICE); unmap_dst: -if (qat_req->dst_align) -dma_free_coherent (dev, ctx->key_sz, qat_req->dst_align, - qat_req->out.rsa.dec.m); -else -if (!dma_mapping_error (dev, qat_req->out.rsa.dec.m)) -dma_unmap_single (dev, qat_req->out.rsa.dec.m, - ctx->key_sz,DMA_FROM_DEVICE ); +if (!dma_mapping_error (dev, qat_req->out.rsa.dec.m)) +dma_unmap_single (dev, qat_req->out.rsa.dec.m, + ctx->key_sz,DMA_FROM_DEVICE ); +kfree_sensitive (qat_req->dst_align); unmap_src: -if (qat_req->src_align) -dma_free_coherent (dev, ctx->key_sz, qat_req->src_align, - qat_req->in.rsa.dec.c); -else -if (!dma_mapping_error (dev, qat_req->in.rsa.dec.c)) -dma_unmap_single (dev, qat_req->in.rsa.dec.c, - ctx->key_sz,DMA_TO_DEVICE ); +if (!dma_mapping_error (dev, qat_req->in.rsa.dec.c)) +dma_unmap_single (dev, qat_req->in.rsa.dec.c, ctx->key_sz, +DMA_TO_DEVICE ); +kfree_sensitive (qat_req->src_align);return ret; }
Signature · 1 line
2.35.1
PATCH v3 6/10 crypto: qat - remove dma_free_coherent() for DH
In reply to Giovanni CabidduThe functions qat_dh_compute_value() allocates memory with dma_alloc_coherent() if the source or the destination buffers are made of multiple flat buffers or of a size that is not compatible with the hardware.
This memory is then freed with dma_free_coherent() in the context of a tasklet invoked to handle the response for the corresponding request.
According to Documentation/core-api/dma-api-howto.rst, the function dma_free_coherent() cannot be called in an interrupt context.
Replace allocations with dma_alloc_coherent() in the function qat_dh_compute_value() with kmalloc() + dma_map_single().
--- drivers/crypto/qat/qat_common/qat_asym_algs.c | 83 ++++++++----------- 1 file changed, 34 insertions(+), 49 deletions(-)
drivers/crypto/qat/qat_common/qat_asym_algs.c +34 -49
@@ -164,26 +164,21 @@ static void qat_dh_cb(struct icp_qat_fw_pke_resp *resp) err = (err ==ICP_QAT_FW_COMN_STATUS_FLAG_OK ) ?0 : -EINVAL ;if (areq->src) { -if (req->src_align) -dma_free_coherent (dev, req->ctx.dh->p_size, - req->src_align, req->in.dh.in.b); -else -dma_unmap_single (dev, req->in.dh.in.b, - req->ctx.dh->p_size,DMA_TO_DEVICE ); +dma_unmap_single (dev, req->in.dh.in.b, req->ctx.dh->p_size, +DMA_TO_DEVICE ); +kfree_sensitive (req->src_align); } areq->dst_len = req->ctx.dh->p_size;if (req->dst_align) {scatterwalk_map_and_copy (req->dst_align, areq->dst,0 , areq->dst_len,1 ); - -dma_free_coherent (dev, req->ctx.dh->p_size, req->dst_align, - req->out.dh.r); - }else { -dma_unmap_single (dev, req->out.dh.r, req->ctx.dh->p_size, -DMA_FROM_DEVICE ); +kfree_sensitive (req->dst_align); } +dma_unmap_single (dev, req->out.dh.r, req->ctx.dh->p_size, +DMA_FROM_DEVICE ); +dma_unmap_single (dev, req->phy_in,sizeof (struct qat_dh_input_params ),DMA_TO_DEVICE ); dma_unmap_single(dev, req->phy_out,
@@ -231,6 +226,7 @@ static int qat_dh_compute_value(struct kpp_request *req)struct icp_qat_fw_pke_request *msg = &qat_req->req;int ret;int n_input_params =0 ; +u8 *vaddr;if (unlikely (!ctx->xa))return -EINVAL ;
@@ -287,27 +283,24 @@ static int qat_dh_compute_value(struct kpp_request *req)*/ if (sg_is_last (req->src) && req->src_len == ctx->p_size) { qat_req->src_align =NULL ; - qat_req->in.dh.in.b =dma_map_single (dev, -sg_virt (req->src), - req->src_len, -DMA_TO_DEVICE ); -if (unlikely (dma_mapping_error (dev, - qat_req->in.dh.in.b))) -return ret; - + vaddr =sg_virt (req->src); }else {int shift = ctx->p_size - req->src_len; - qat_req->src_align =dma_alloc_coherent (dev, - ctx->p_size, - &qat_req->in.dh.in.b, -GFP_KERNEL ); + qat_req->src_align =kzalloc (ctx->p_size,GFP_KERNEL );if (unlikely (!qat_req->src_align))return ret;scatterwalk_map_and_copy (qat_req->src_align + shift, req->src,0 , req->src_len,0 ); + + vaddr = qat_req->src_align; } + + qat_req->in.dh.in.b =dma_map_single (dev, vaddr, ctx->p_size, +DMA_TO_DEVICE ); +if (unlikely (dma_mapping_error (dev, qat_req->in.dh.in.b))) + goto unmap_src; }/* * dst can be of any size in valid range, but HW expects it to be the
@@ -318,20 +311,18 @@ static int qat_dh_compute_value(struct kpp_request *req)*/ if (sg_is_last (req->dst) && req->dst_len == ctx->p_size) { qat_req->dst_align =NULL ; - qat_req->out.dh.r =dma_map_single (dev,sg_virt (req->dst), - req->dst_len, -DMA_FROM_DEVICE ); - -if (unlikely (dma_mapping_error (dev, qat_req->out.dh.r))) - goto unmap_src; - + vaddr =sg_virt (req->dst); }else { - qat_req->dst_align =dma_alloc_coherent (dev, ctx->p_size, - &qat_req->out.dh.r, -GFP_KERNEL ); + qat_req->dst_align =kzalloc (ctx->p_size,GFP_KERNEL );if (unlikely (!qat_req->dst_align)) goto unmap_src; + + vaddr = qat_req->dst_align; } + qat_req->out.dh.r =dma_map_single (dev, vaddr, ctx->p_size, +DMA_FROM_DEVICE ); +if (unlikely (dma_mapping_error (dev, qat_req->out.dh.r))) + goto unmap_dst; qat_req->in.dh.in_tab[n_input_params] =0 ; qat_req->out.dh.out_tab[1 ] =0 ;
@@ -371,23 +362,17 @@ static int qat_dh_compute_value(struct kpp_request *req)sizeof (struct qat_dh_input_params ), DMA_TO_DEVICE); unmap_dst: -if (qat_req->dst_align) -dma_free_coherent (dev, ctx->p_size, qat_req->dst_align, - qat_req->out.dh.r); -else -if (!dma_mapping_error (dev, qat_req->out.dh.r)) -dma_unmap_single (dev, qat_req->out.dh.r, ctx->p_size, -DMA_FROM_DEVICE ); +if (!dma_mapping_error (dev, qat_req->out.dh.r)) +dma_unmap_single (dev, qat_req->out.dh.r, ctx->p_size, +DMA_FROM_DEVICE ); +kfree_sensitive (qat_req->dst_align); unmap_src:if (req->src) { -if (qat_req->src_align) -dma_free_coherent (dev, ctx->p_size, qat_req->src_align, - qat_req->in.dh.in.b); -else -if (!dma_mapping_error (dev, qat_req->in.dh.in.b)) -dma_unmap_single (dev, qat_req->in.dh.in.b, - ctx->p_size, -DMA_TO_DEVICE ); +if (!dma_mapping_error (dev, qat_req->in.dh.in.b)) +dma_unmap_single (dev, qat_req->in.dh.in.b, + ctx->p_size, +DMA_TO_DEVICE ); +kfree_sensitive (qat_req->src_align); }return ret; }
Signature · 1 line
2.35.1
PATCH v3 7/10 crypto: qat - add param check for RSA
In reply to Giovanni CabidduReject requests with a source buffer that is bigger than the size of the key. This is to prevent a possible integer underflow that might happen when copying the source scatterlist into a linear buffer.
--- drivers/crypto/qat/qat_common/qat_asym_algs.c | 8 ++++++++ 1 file changed, 8 insertions(+)
drivers/crypto/qat/qat_common/qat_asym_algs.c +8 -0
@@ -653,6 +653,10 @@ static int qat_rsa_enc(struct akcipher_request *req) req->dst_len = ctx->key_sz;return -EOVERFLOW ; } + +if (req->src_len > ctx->key_sz) +return -EINVAL ; +memset (msg,'\0' ,sizeof (*msg));ICP_QAT_FW_PKE_HDR_VALID_FLAG_SET (msg->pke_hdr,ICP_QAT_FW_COMN_REQ_FLAG_SET );
@@ -782,6 +786,10 @@ static int qat_rsa_dec(struct akcipher_request *req) req->dst_len = ctx->key_sz;return -EOVERFLOW ; } + +if (req->src_len > ctx->key_sz) +return -EINVAL ; +memset (msg,'\0' ,sizeof (*msg));ICP_QAT_FW_PKE_HDR_VALID_FLAG_SET (msg->pke_hdr,ICP_QAT_FW_COMN_REQ_FLAG_SET );
Signature · 1 line
2.35.1
PATCH v3 8/10 crypto: qat - add param check for DH
In reply to Giovanni CabidduReject requests with a source buffer that is bigger than the size of the key. This is to prevent a possible integer underflow that might happen when copying the source scatterlist into a linear buffer.
--- drivers/crypto/qat/qat_common/qat_asym_algs.c | 4 ++++ 1 file changed, 4 insertions(+)
drivers/crypto/qat/qat_common/qat_asym_algs.c +4 -0
@@ -235,6 +235,10 @@ static int qat_dh_compute_value(struct kpp_request *req) req->dst_len = ctx->p_size;return -EOVERFLOW ; } + +if (req->src_len > ctx->p_size) +return -EINVAL ; +memset (msg,'\0' ,sizeof (*msg));ICP_QAT_FW_PKE_HDR_VALID_FLAG_SET (msg->pke_hdr,ICP_QAT_FW_COMN_REQ_FLAG_SET );
Signature · 1 line
2.35.1
PATCH v3 9/10 crypto: qat - honor CRYPTO_TFM_REQ_MAY_SLEEP flag
In reply to Giovanni CabidduIf a request has the flag CRYPTO_TFM_REQ_MAY_SLEEP set, allocate memory using the flag GFP_KERNEL otherwise use GFP_ATOMIC.
--- drivers/crypto/qat/qat_common/qat_algs.c | 19 ++++++++++++------- drivers/crypto/qat/qat_common/qat_asym_algs.c | 17 ++++++++++------- drivers/crypto/qat/qat_common/qat_crypto.h | 5 +++++ 3 files changed, 27 insertions(+), 14 deletions(-)
drivers/crypto/qat/qat_common/qat_algs.c +12 -7
@@ -703,7 +703,8 @@ static void qat_alg_free_bufl(struct qat_crypto_instance *inst,static int qat_alg_sgl_to_bufl (struct qat_crypto_instance *inst,struct scatterlist *sgl,struct scatterlist *sglout, -struct qat_crypto_request *qat_req) +struct qat_crypto_request *qat_req, +gfp_t flags) {struct device *dev = &GET_DEV (inst->accel_dev);int i, sg_nctr =0 ;
@@ -723,7 +724,7 @@ static int qat_alg_sgl_to_bufl(struct qat_crypto_instance *inst, qat_req->buf.sgl_dst_valid = false;if (n >QAT_MAX_BUFF_DESC ) { - bufl =kzalloc_node (sz,GFP_ATOMIC , node); + bufl =kzalloc_node (sz, flags, node);if (unlikely (!bufl))return -ENOMEM ; }else {
@@ -765,7 +766,7 @@ static int qat_alg_sgl_to_bufl(struct qat_crypto_instance *inst, sg_nctr =0 ;if (n >QAT_MAX_BUFF_DESC ) { - buflout =kzalloc_node (sz_out,GFP_ATOMIC , node); + buflout =kzalloc_node (sz_out, flags, node);if (unlikely (!buflout)) goto err_in; }else {
@@ -966,6 +967,7 @@ static int qat_alg_aead_dec(struct aead_request *areq)struct icp_qat_fw_la_auth_req_params *auth_param;struct icp_qat_fw_la_bulk_req *msg;int digst_size =crypto_aead_authsize (aead_tfm); +gfp_t f =qat_algs_alloc_flags (&areq->base);int ret;u32 cipher_len;
@@ -973,7 +975,7 @@ static int qat_alg_aead_dec(struct aead_request *areq)if (cipher_len %AES_BLOCK_SIZE !=0 )return -EINVAL ; - ret =qat_alg_sgl_to_bufl (ctx->inst, areq->src, areq->dst, qat_req); + ret =qat_alg_sgl_to_bufl (ctx->inst, areq->src, areq->dst, qat_req, f);if (unlikely (ret))return ret;
@@ -1008,6 +1010,7 @@ static int qat_alg_aead_enc(struct aead_request *areq)struct qat_crypto_request *qat_req =aead_request_ctx (areq);struct icp_qat_fw_la_cipher_req_params *cipher_param;struct icp_qat_fw_la_auth_req_params *auth_param; +gfp_t f =qat_algs_alloc_flags (&areq->base);struct icp_qat_fw_la_bulk_req *msg;u8 *iv = areq->iv;int ret;
@@ -1015,7 +1018,7 @@ static int qat_alg_aead_enc(struct aead_request *areq)if (areq->cryptlen %AES_BLOCK_SIZE !=0 )return -EINVAL ; - ret =qat_alg_sgl_to_bufl (ctx->inst, areq->src, areq->dst, qat_req); + ret =qat_alg_sgl_to_bufl (ctx->inst, areq->src, areq->dst, qat_req, f);if (unlikely (ret))return ret;
@@ -1193,13 +1196,14 @@ static int qat_alg_skcipher_encrypt(struct skcipher_request *req)struct qat_alg_skcipher_ctx *ctx =crypto_tfm_ctx (tfm);struct qat_crypto_request *qat_req =skcipher_request_ctx (req);struct icp_qat_fw_la_cipher_req_params *cipher_param; +gfp_t f =qat_algs_alloc_flags (&req->base);struct icp_qat_fw_la_bulk_req *msg;int ret;if (req->cryptlen ==0 )return 0 ; - ret =qat_alg_sgl_to_bufl (ctx->inst, req->src, req->dst, qat_req); + ret =qat_alg_sgl_to_bufl (ctx->inst, req->src, req->dst, qat_req, f);if (unlikely (ret))return ret;
@@ -1258,13 +1262,14 @@ static int qat_alg_skcipher_decrypt(struct skcipher_request *req)struct qat_alg_skcipher_ctx *ctx =crypto_tfm_ctx (tfm);struct qat_crypto_request *qat_req =skcipher_request_ctx (req);struct icp_qat_fw_la_cipher_req_params *cipher_param; +gfp_t f =qat_algs_alloc_flags (&req->base);struct icp_qat_fw_la_bulk_req *msg;int ret;if (req->cryptlen ==0 )return 0 ; - ret =qat_alg_sgl_to_bufl (ctx->inst, req->src, req->dst, qat_req); + ret =qat_alg_sgl_to_bufl (ctx->inst, req->src, req->dst, qat_req, f);if (unlikely (ret))return ret;
drivers/crypto/qat/qat_common/qat_asym_algs.c +10 -7
@@ -224,9 +224,10 @@ static int qat_dh_compute_value(struct kpp_request *req)struct qat_asym_request *qat_req =PTR_ALIGN (kpp_request_ctx (req),64 );struct icp_qat_fw_pke_request *msg = &qat_req->req; -int ret; +gfp_t flags =qat_algs_alloc_flags (&req->base);int n_input_params =0 ;u8 *vaddr; +int ret;if (unlikely (!ctx->xa))return -EINVAL ;
@@ -291,7 +292,7 @@ static int qat_dh_compute_value(struct kpp_request *req) }else {int shift = ctx->p_size - req->src_len; - qat_req->src_align =kzalloc (ctx->p_size,GFP_KERNEL ); + qat_req->src_align =kzalloc (ctx->p_size, flags);if (unlikely (!qat_req->src_align))return ret;
@@ -317,7 +318,7 @@ static int qat_dh_compute_value(struct kpp_request *req) qat_req->dst_align =NULL ; vaddr =sg_virt (req->dst); }else { - qat_req->dst_align =kzalloc (ctx->p_size,GFP_KERNEL ); + qat_req->dst_align =kzalloc (ctx->p_size, flags);if (unlikely (!qat_req->dst_align)) goto unmap_src;
@@ -647,6 +648,7 @@ static int qat_rsa_enc(struct akcipher_request *req)struct qat_asym_request *qat_req =PTR_ALIGN (akcipher_request_ctx (req),64 );struct icp_qat_fw_pke_request *msg = &qat_req->req; +gfp_t flags =qat_algs_alloc_flags (&req->base);u8 *vaddr;int ret;
@@ -693,7 +695,7 @@ static int qat_rsa_enc(struct akcipher_request *req) }else {int shift = ctx->key_sz - req->src_len; - qat_req->src_align =kzalloc (ctx->key_sz,GFP_KERNEL ); + qat_req->src_align =kzalloc (ctx->key_sz, flags);if (unlikely (!qat_req->src_align))return ret;
@@ -711,7 +713,7 @@ static int qat_rsa_enc(struct akcipher_request *req) qat_req->dst_align =NULL ; vaddr =sg_virt (req->dst); }else { - qat_req->dst_align =kzalloc (ctx->key_sz,GFP_KERNEL ); + qat_req->dst_align =kzalloc (ctx->key_sz, flags);if (unlikely (!qat_req->dst_align)) goto unmap_src; vaddr = qat_req->dst_align;
@@ -780,6 +782,7 @@ static int qat_rsa_dec(struct akcipher_request *req)struct qat_asym_request *qat_req =PTR_ALIGN (akcipher_request_ctx (req),64 );struct icp_qat_fw_pke_request *msg = &qat_req->req; +gfp_t flags =qat_algs_alloc_flags (&req->base);u8 *vaddr;int ret;
@@ -836,7 +839,7 @@ static int qat_rsa_dec(struct akcipher_request *req) }else {int shift = ctx->key_sz - req->src_len; - qat_req->src_align =kzalloc (ctx->key_sz,GFP_KERNEL ); + qat_req->src_align =kzalloc (ctx->key_sz, flags);if (unlikely (!qat_req->src_align))return ret;
@@ -854,7 +857,7 @@ static int qat_rsa_dec(struct akcipher_request *req) qat_req->dst_align =NULL ; vaddr =sg_virt (req->dst); }else { - qat_req->dst_align =kzalloc (ctx->key_sz,GFP_KERNEL ); + qat_req->dst_align =kzalloc (ctx->key_sz, flags);if (unlikely (!qat_req->dst_align)) goto unmap_src; vaddr = qat_req->dst_align;
drivers/crypto/qat/qat_common/qat_crypto.h +5 -0
@@ -109,4 +109,9 @@ static inline bool adf_hw_dev_has_crypto(struct adf_accel_dev *accel_dev) return true; } +static inline gfp_t qat_algs_alloc_flags (struct crypto_async_request *req) +{ +return req->flags &CRYPTO_TFM_REQ_MAY_SLEEP ?GFP_KERNEL :GFP_ATOMIC ; +} +#endif
Signature · 1 line
2.35.1
PATCH v3 10/10 crypto: qat - re-enable registration of algorithms
In reply to Giovanni CabidduRe-enable the registration of algorithms after fixes to (1) use pre-allocated buffers in the datapath and (2) support the CRYPTO_TFM_REQ_MAY_BACKLOG flag.
This reverts commit 8893d27ffcaf6ec6267038a177cb87bcde4dd3de.
--- drivers/crypto/qat/qat_4xxx/adf_drv.c | 7 ------- drivers/crypto/qat/qat_common/qat_crypto.c | 7 ------- 2 files changed, 14 deletions(-)
drivers/crypto/qat/qat_4xxx/adf_drv.c +0 -7
@@ -75,13 +75,6 @@ static int adf_crypto_dev_config(struct adf_accel_dev *accel_dev)if (ret) goto err; -/* Temporarily set the number of crypto instances to zero to avoid -* registering the crypto algorithms. -* This will be removed when the algorithms will support the -* CRYPTO_TFM_REQ_MAY_BACKLOG flag -*/ - instances =0 ; -for (i =0 ; i < instances; i++) { val = i; bank = i *2 ;
drivers/crypto/qat/qat_common/qat_crypto.c +0 -7
@@ -161,13 +161,6 @@ int qat_crypto_dev_config(struct adf_accel_dev *accel_dev)if (ret) goto err; -/* Temporarily set the number of crypto instances to zero to avoid -* registering the crypto algorithms. -* This will be removed when the algorithms will support the -* CRYPTO_TFM_REQ_MAY_BACKLOG flag -*/ - instances =0 ; -for (i =0 ; i < instances; i++) { val = i;snprintf (key,sizeof (key),ADF_CY "%d" ADF_RING_ASYM_BANK_NUM , i);
Signature · 1 line
2.35.1
Greg KH
maintainerRe: [PATCH v3 10/10] crypto: qat - re-enable registration of algorithms
Quoted 5 lines · Giovanni Cabiddu
Re-enable the registration of algorithms after fixes to (1) use pre-allocated buffers in the datapath and (2) support the
CRYPTO_TFM_REQ_MAY_BACKLOGflag.This reverts commit
8893d27ffcaf6ec6267038a177cb87bcde4dd3de.
Then why not just have this be a "normal" revert commit?
And why is this ok for stable kernels? This feels like a new feature (i.e. the code finally works.) Why not just have users who want to use this use a newer kernel? What bugfix does this resolve in older kernels (and "the driver did not work" is not really a good reason.)
thanks,
greg k-h
Re: [PATCH v3 10/10] crypto: qat - re-enable registration of algorithms
Quoted 8 lines · Greg KH
Quoted 5 lines · Giovanni Cabiddu
Re-enable the registration of algorithms after fixes to (1) use pre-allocated buffers in the datapath and (2) support the
CRYPTO_TFM_REQ_MAY_BACKLOGflag.This reverts commit
8893d27ffcaf6ec6267038a177cb87bcde4dd3de.Then why not just have this be a "normal" revert commit?
It can be a revert commit. I didn't send a revert commit since I never saw one in the crypto mailing list.
Quoted 4 lines
And why is this ok for stable kernels? This feels like a new feature (i.e. the code finally works.) Why not just have users who want to use this use a newer kernel? What bugfix does this resolve in older kernels (and "the driver did not work" is not really a good reason.)
After the bug report in [1], the final decision was to disable the algorithms until the issue was fixed.
This set is fixing the issues that cause [1] and a few other minor ones before re-enabling the algos.
I think there is value in having these fixes in stable as they allow to re-enable services that were available before the 5.17 time frame.
Regards,
Signature · 3 lines
Giovanni [1] https://lore.kernel.org/linux-crypto/CACsaVZ+mt3CfdXV0_yJh7d50tRcGcRZ12j3n6-hoX2cz3+njsg@mail.gmail.com/
Herbert Xu
maintainerQuoted 45 lines · Giovanni Cabiddu
This set is an extension of a previous set called
crypto: qat - fix dm-crypt related issueswhich aims to re-enable the algorithms in the QAT driver after [1].This fixes a number of issues with the implementation of the QAT algs, both symmetric and asymmetric.
In particular this set enables the QAT driver to handle correctly the flagsCRYPTO_TFM_REQ_MAY_BACKLOGandCRYPTO_TFM_REQ_MAY_SLEEP, fixes an hidden issue in RSA and DH which appeared after commitf5ff79fddf0e, related to the usage ofdma_free_coherent()from a tasklet, and includes important fixes in the akcipher algorithms.One item to mention is that, differently from the previous set, this one does not removes the flag
CRYPTO_ALG_ALLOCATES_MEMORYwhich will be removed after the conversation in [2] is closed.[1] https://lore.kernel.org/linux-crypto/YiEyGoHacN80FcOL@silpixa00400314/
[2] https://lore.kernel.org/linux-crypto/Yl6PlqyucVLCzwF5@silpixa00400314/Changes from v2:
- Removedcrypto: qat - set to zero DH parameters before freefrom set.
- Added fixes tags to patchescrypto: qat - add param check for RSAandcrypto: qat - add param check for DHChanges from v1:
- Clarified commit message incrypto: qat - refactor submission logicto indicate why the patch should be included in stable kernels
- Removedcrypto: qat - use memzero_explicit() for algsfrom set after feedback from Greg KH
- Replacedmemzero_explicit()withmemset()incrypto: qat - set to zero DH parameters before freeafter feedback from Greg KHGiovanni Cabiddu (10):
crypto: qat - use pre-allocated buffers in datapath
crypto: qat - refactor submission logic
crypto: qat - add backlog mechanism
crypto: qat - fix memory leak in RSA
crypto: qat - removedma_free_coherent()for RSA
crypto: qat - removedma_free_coherent()for DH
crypto: qat - add param check for RSA
crypto: qat - add param check for DH
crypto: qat - honorCRYPTO_TFM_REQ_MAY_SLEEPflag
crypto: qat - re-enable registration of algorithms
Quoted 14 lines
drivers/crypto/qat/qat_4xxx/adf_drv.c | 7 - drivers/crypto/qat/qat_common/Makefile | 1 + drivers/crypto/qat/qat_common/adf_transport.c | 11 + drivers/crypto/qat/qat_common/adf_transport.h | 1 + .../qat/qat_common/adf_transport_internal.h | 1 + drivers/crypto/qat/qat_common/qat_algs.c | 153 +++++---- drivers/crypto/qat/qat_common/qat_algs_send.c | 86 +++++ drivers/crypto/qat/qat_common/qat_algs_send.h | 11 + drivers/crypto/qat/qat_common/qat_asym_algs.c | 304 +++++++++--------- drivers/crypto/qat/qat_common/qat_crypto.c | 10 +- drivers/crypto/qat/qat_common/qat_crypto.h | 44 +++ 11 files changed, 392 insertions(+), 237 deletions(-) create mode 100644 drivers/crypto/qat/qat_common/qat_algs_send.c create mode 100644 drivers/crypto/qat/qat_common/qat_algs_send.h
All applied. Thanks.
Signature · 3 lines
Email: Herbert Xu <[email protected]> Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt