mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git
synced 2024-09-07 03:18:31 +00:00
crypto: qat - remove dma_free_coherent() for RSA
[ Upstream commit3dfaf0071e
] After commitf5ff79fddf
("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(). Cc: stable@vger.kernel.org Fixes:a990532023
("crypto: qat - Add support for RSA algorithm") Signed-off-by: Giovanni Cabiddu <giovanni.cabiddu@intel.com> Reviewed-by: Adam Guerin <adam.guerin@intel.com> Reviewed-by: Wojciech Ziemba <wojciech.ziemba@intel.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au> Signed-off-by: Sasha Levin <sashal@kernel.org>
This commit is contained in:
parent
0f967fdc09
commit
055e83bf21
1 changed files with 60 additions and 77 deletions
|
@ -529,10 +529,8 @@ static void qat_rsa_cb(struct icp_qat_fw_pke_resp *resp)
|
||||||
|
|
||||||
err = (err == ICP_QAT_FW_COMN_STATUS_FLAG_OK) ? 0 : -EINVAL;
|
err = (err == ICP_QAT_FW_COMN_STATUS_FLAG_OK) ? 0 : -EINVAL;
|
||||||
|
|
||||||
if (req->src_align)
|
kfree_sensitive(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_unmap_single(dev, req->in.rsa.enc.m, req->ctx.rsa->key_sz,
|
||||||
DMA_TO_DEVICE);
|
DMA_TO_DEVICE);
|
||||||
|
|
||||||
|
@ -541,12 +539,11 @@ static void qat_rsa_cb(struct icp_qat_fw_pke_resp *resp)
|
||||||
scatterwalk_map_and_copy(req->dst_align, areq->dst, 0,
|
scatterwalk_map_and_copy(req->dst_align, areq->dst, 0,
|
||||||
areq->dst_len, 1);
|
areq->dst_len, 1);
|
||||||
|
|
||||||
dma_free_coherent(dev, req->ctx.rsa->key_sz, req->dst_align,
|
kfree_sensitive(req->dst_align);
|
||||||
req->out.rsa.enc.c);
|
}
|
||||||
} else {
|
|
||||||
dma_unmap_single(dev, req->out.rsa.enc.c, req->ctx.rsa->key_sz,
|
dma_unmap_single(dev, req->out.rsa.enc.c, req->ctx.rsa->key_sz,
|
||||||
DMA_FROM_DEVICE);
|
DMA_FROM_DEVICE);
|
||||||
}
|
|
||||||
|
|
||||||
dma_unmap_single(dev, req->phy_in, sizeof(struct qat_rsa_input_params),
|
dma_unmap_single(dev, req->phy_in, sizeof(struct qat_rsa_input_params),
|
||||||
DMA_TO_DEVICE);
|
DMA_TO_DEVICE);
|
||||||
|
@ -664,6 +661,7 @@ static int qat_rsa_enc(struct akcipher_request *req)
|
||||||
struct qat_asym_request *qat_req =
|
struct qat_asym_request *qat_req =
|
||||||
PTR_ALIGN(akcipher_request_ctx(req), 64);
|
PTR_ALIGN(akcipher_request_ctx(req), 64);
|
||||||
struct icp_qat_fw_pke_request *msg = &qat_req->req;
|
struct icp_qat_fw_pke_request *msg = &qat_req->req;
|
||||||
|
u8 *vaddr;
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
if (unlikely(!ctx->n || !ctx->e))
|
if (unlikely(!ctx->n || !ctx->e))
|
||||||
|
@ -701,40 +699,39 @@ static int qat_rsa_enc(struct akcipher_request *req)
|
||||||
*/
|
*/
|
||||||
if (sg_is_last(req->src) && req->src_len == ctx->key_sz) {
|
if (sg_is_last(req->src) && req->src_len == ctx->key_sz) {
|
||||||
qat_req->src_align = NULL;
|
qat_req->src_align = NULL;
|
||||||
qat_req->in.rsa.enc.m = dma_map_single(dev, sg_virt(req->src),
|
vaddr = sg_virt(req->src);
|
||||||
req->src_len, DMA_TO_DEVICE);
|
|
||||||
if (unlikely(dma_mapping_error(dev, qat_req->in.rsa.enc.m)))
|
|
||||||
return ret;
|
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
int shift = ctx->key_sz - req->src_len;
|
int shift = ctx->key_sz - req->src_len;
|
||||||
|
|
||||||
qat_req->src_align = dma_alloc_coherent(dev, ctx->key_sz,
|
qat_req->src_align = kzalloc(ctx->key_sz, GFP_KERNEL);
|
||||||
&qat_req->in.rsa.enc.m,
|
|
||||||
GFP_KERNEL);
|
|
||||||
if (unlikely(!qat_req->src_align))
|
if (unlikely(!qat_req->src_align))
|
||||||
return ret;
|
return ret;
|
||||||
|
|
||||||
scatterwalk_map_and_copy(qat_req->src_align + shift, req->src,
|
scatterwalk_map_and_copy(qat_req->src_align + shift, req->src,
|
||||||
0, req->src_len, 0);
|
0, req->src_len, 0);
|
||||||
|
vaddr = qat_req->src_align;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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) {
|
if (sg_is_last(req->dst) && req->dst_len == ctx->key_sz) {
|
||||||
qat_req->dst_align = NULL;
|
qat_req->dst_align = NULL;
|
||||||
qat_req->out.rsa.enc.c = dma_map_single(dev, sg_virt(req->dst),
|
vaddr = 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;
|
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
qat_req->dst_align = dma_alloc_coherent(dev, ctx->key_sz,
|
qat_req->dst_align = kzalloc(ctx->key_sz, GFP_KERNEL);
|
||||||
&qat_req->out.rsa.enc.c,
|
|
||||||
GFP_KERNEL);
|
|
||||||
if (unlikely(!qat_req->dst_align))
|
if (unlikely(!qat_req->dst_align))
|
||||||
goto unmap_src;
|
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->in.rsa.in_tab[3] = 0;
|
||||||
qat_req->out.rsa.out_tab[1] = 0;
|
qat_req->out.rsa.out_tab[1] = 0;
|
||||||
qat_req->phy_in = dma_map_single(dev, &qat_req->in.rsa.enc.m,
|
qat_req->phy_in = dma_map_single(dev, &qat_req->in.rsa.enc.m,
|
||||||
|
@ -772,21 +769,15 @@ static int qat_rsa_enc(struct akcipher_request *req)
|
||||||
sizeof(struct qat_rsa_input_params),
|
sizeof(struct qat_rsa_input_params),
|
||||||
DMA_TO_DEVICE);
|
DMA_TO_DEVICE);
|
||||||
unmap_dst:
|
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))
|
if (!dma_mapping_error(dev, qat_req->out.rsa.enc.c))
|
||||||
dma_unmap_single(dev, qat_req->out.rsa.enc.c,
|
dma_unmap_single(dev, qat_req->out.rsa.enc.c,
|
||||||
ctx->key_sz, DMA_FROM_DEVICE);
|
ctx->key_sz, DMA_FROM_DEVICE);
|
||||||
|
kfree_sensitive(qat_req->dst_align);
|
||||||
unmap_src:
|
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))
|
if (!dma_mapping_error(dev, qat_req->in.rsa.enc.m))
|
||||||
dma_unmap_single(dev, qat_req->in.rsa.enc.m,
|
dma_unmap_single(dev, qat_req->in.rsa.enc.m, ctx->key_sz,
|
||||||
ctx->key_sz, DMA_TO_DEVICE);
|
DMA_TO_DEVICE);
|
||||||
|
kfree_sensitive(qat_req->src_align);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -799,6 +790,7 @@ static int qat_rsa_dec(struct akcipher_request *req)
|
||||||
struct qat_asym_request *qat_req =
|
struct qat_asym_request *qat_req =
|
||||||
PTR_ALIGN(akcipher_request_ctx(req), 64);
|
PTR_ALIGN(akcipher_request_ctx(req), 64);
|
||||||
struct icp_qat_fw_pke_request *msg = &qat_req->req;
|
struct icp_qat_fw_pke_request *msg = &qat_req->req;
|
||||||
|
u8 *vaddr;
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
if (unlikely(!ctx->n || !ctx->d))
|
if (unlikely(!ctx->n || !ctx->d))
|
||||||
|
@ -846,40 +838,37 @@ static int qat_rsa_dec(struct akcipher_request *req)
|
||||||
*/
|
*/
|
||||||
if (sg_is_last(req->src) && req->src_len == ctx->key_sz) {
|
if (sg_is_last(req->src) && req->src_len == ctx->key_sz) {
|
||||||
qat_req->src_align = NULL;
|
qat_req->src_align = NULL;
|
||||||
qat_req->in.rsa.dec.c = dma_map_single(dev, sg_virt(req->src),
|
vaddr = sg_virt(req->src);
|
||||||
req->dst_len, DMA_TO_DEVICE);
|
|
||||||
if (unlikely(dma_mapping_error(dev, qat_req->in.rsa.dec.c)))
|
|
||||||
return ret;
|
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
int shift = ctx->key_sz - req->src_len;
|
int shift = ctx->key_sz - req->src_len;
|
||||||
|
|
||||||
qat_req->src_align = dma_alloc_coherent(dev, ctx->key_sz,
|
qat_req->src_align = kzalloc(ctx->key_sz, GFP_KERNEL);
|
||||||
&qat_req->in.rsa.dec.c,
|
|
||||||
GFP_KERNEL);
|
|
||||||
if (unlikely(!qat_req->src_align))
|
if (unlikely(!qat_req->src_align))
|
||||||
return ret;
|
return ret;
|
||||||
|
|
||||||
scatterwalk_map_and_copy(qat_req->src_align + shift, req->src,
|
scatterwalk_map_and_copy(qat_req->src_align + shift, req->src,
|
||||||
0, req->src_len, 0);
|
0, req->src_len, 0);
|
||||||
|
vaddr = qat_req->src_align;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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) {
|
if (sg_is_last(req->dst) && req->dst_len == ctx->key_sz) {
|
||||||
qat_req->dst_align = NULL;
|
qat_req->dst_align = NULL;
|
||||||
qat_req->out.rsa.dec.m = dma_map_single(dev, sg_virt(req->dst),
|
vaddr = 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;
|
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
qat_req->dst_align = dma_alloc_coherent(dev, ctx->key_sz,
|
qat_req->dst_align = kzalloc(ctx->key_sz, GFP_KERNEL);
|
||||||
&qat_req->out.rsa.dec.m,
|
|
||||||
GFP_KERNEL);
|
|
||||||
if (unlikely(!qat_req->dst_align))
|
if (unlikely(!qat_req->dst_align))
|
||||||
goto unmap_src;
|
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)
|
if (ctx->crt_mode)
|
||||||
qat_req->in.rsa.in_tab[6] = 0;
|
qat_req->in.rsa.in_tab[6] = 0;
|
||||||
|
@ -925,21 +914,15 @@ static int qat_rsa_dec(struct akcipher_request *req)
|
||||||
sizeof(struct qat_rsa_input_params),
|
sizeof(struct qat_rsa_input_params),
|
||||||
DMA_TO_DEVICE);
|
DMA_TO_DEVICE);
|
||||||
unmap_dst:
|
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))
|
if (!dma_mapping_error(dev, qat_req->out.rsa.dec.m))
|
||||||
dma_unmap_single(dev, qat_req->out.rsa.dec.m,
|
dma_unmap_single(dev, qat_req->out.rsa.dec.m,
|
||||||
ctx->key_sz, DMA_FROM_DEVICE);
|
ctx->key_sz, DMA_FROM_DEVICE);
|
||||||
|
kfree_sensitive(qat_req->dst_align);
|
||||||
unmap_src:
|
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))
|
if (!dma_mapping_error(dev, qat_req->in.rsa.dec.c))
|
||||||
dma_unmap_single(dev, qat_req->in.rsa.dec.c,
|
dma_unmap_single(dev, qat_req->in.rsa.dec.c, ctx->key_sz,
|
||||||
ctx->key_sz, DMA_TO_DEVICE);
|
DMA_TO_DEVICE);
|
||||||
|
kfree_sensitive(qat_req->src_align);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue