Skip to content

Commit 2566de3

Browse files
Zenghui Yuherbertx
authored andcommitted
crypto: hisilicon - Use fine grained DMA mapping direction
The following splat was triggered when booting the kernel built with arm64's defconfig + CRYPTO_SELFTESTS + DMA_API_DEBUG. ------------[ cut here ]------------ DMA-API: hisi_sec2 0000:75:00.0: cacheline tracking EEXIST, overlapping mappings aren't supported WARNING: CPU: 24 PID: 1273 at kernel/dma/debug.c:596 add_dma_entry+0x248/0x308 Call trace: add_dma_entry+0x248/0x308 (P) debug_dma_map_sg+0x208/0x3e4 __dma_map_sg_attrs+0xbc/0x118 dma_map_sg_attrs+0x10/0x24 hisi_acc_sg_buf_map_to_hw_sgl+0x80/0x218 [hisi_qm] sec_cipher_map+0xc4/0x338 [hisi_sec2] sec_aead_sgl_map+0x18/0x24 [hisi_sec2] sec_process+0xb8/0x36c [hisi_sec2] sec_aead_crypto+0xe4/0x264 [hisi_sec2] sec_aead_encrypt+0x14/0x20 [hisi_sec2] crypto_aead_encrypt+0x24/0x38 test_aead_vec_cfg+0x480/0x7e4 test_aead_vec+0x84/0x1b8 alg_test_aead+0xc0/0x498 alg_test.part.0+0x518/0x524 alg_test+0x20/0x64 cryptomgr_test+0x24/0x44 kthread+0x130/0x1fc ret_from_fork+0x10/0x20 ---[ end trace 0000000000000000 ]--- DMA-API: Mapped at: debug_dma_map_sg+0x234/0x3e4 __dma_map_sg_attrs+0xbc/0x118 dma_map_sg_attrs+0x10/0x24 hisi_acc_sg_buf_map_to_hw_sgl+0x80/0x218 [hisi_qm] sec_cipher_map+0xc4/0x338 [hisi_sec2] This occurs in selftests where the input and the output scatterlist point to the same underlying memory (e.g., when tested with INPLACE_TWO_SGLISTS mode). The problem is that the hisi_sec2 driver maps these two different scatterlists using the DMA_BIDIRECTIONAL flag which leads to overlapped write mappings which are not supported by the DMA layer. Fix it by using the fine grained and correct DMA mapping directions. While at it, switch the DMA directions used by the hisi_zip driver too. Signed-off-by: Zenghui Yu <yuzenghui@huawei.com> Reviewed-by: Longfang Liu <liulongfang@huawei.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent c71187c commit 2566de3

File tree

4 files changed

+30
-23
lines changed

4 files changed

+30
-23
lines changed

drivers/crypto/hisilicon/sec2/sec_crypto.c

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -965,6 +965,7 @@ static int sec_cipher_map(struct sec_ctx *ctx, struct sec_req *req,
965965
struct sec_qp_ctx *qp_ctx = req->qp_ctx;
966966
struct sec_alg_res *res = &qp_ctx->res[req->req_id];
967967
struct device *dev = ctx->dev;
968+
enum dma_data_direction src_direction;
968969
int ret;
969970

970971
if (req->use_pbuf) {
@@ -990,10 +991,11 @@ static int sec_cipher_map(struct sec_ctx *ctx, struct sec_req *req,
990991
a_req->out_mac_dma = res->out_mac_dma;
991992
}
992993

994+
src_direction = dst == src ? DMA_BIDIRECTIONAL : DMA_TO_DEVICE;
993995
req->in = hisi_acc_sg_buf_map_to_hw_sgl(dev, src,
994996
qp_ctx->c_in_pool,
995997
req->req_id,
996-
&req->in_dma);
998+
&req->in_dma, src_direction);
997999
if (IS_ERR(req->in)) {
9981000
dev_err(dev, "fail to dma map input sgl buffers!\n");
9991001
return PTR_ERR(req->in);
@@ -1003,7 +1005,7 @@ static int sec_cipher_map(struct sec_ctx *ctx, struct sec_req *req,
10031005
ret = sec_aead_mac_init(a_req);
10041006
if (unlikely(ret)) {
10051007
dev_err(dev, "fail to init mac data for ICV!\n");
1006-
hisi_acc_sg_buf_unmap(dev, src, req->in);
1008+
hisi_acc_sg_buf_unmap(dev, src, req->in, src_direction);
10071009
return ret;
10081010
}
10091011
}
@@ -1015,11 +1017,12 @@ static int sec_cipher_map(struct sec_ctx *ctx, struct sec_req *req,
10151017
c_req->c_out = hisi_acc_sg_buf_map_to_hw_sgl(dev, dst,
10161018
qp_ctx->c_out_pool,
10171019
req->req_id,
1018-
&c_req->c_out_dma);
1020+
&c_req->c_out_dma,
1021+
DMA_FROM_DEVICE);
10191022

10201023
if (IS_ERR(c_req->c_out)) {
10211024
dev_err(dev, "fail to dma map output sgl buffers!\n");
1022-
hisi_acc_sg_buf_unmap(dev, src, req->in);
1025+
hisi_acc_sg_buf_unmap(dev, src, req->in, src_direction);
10231026
return PTR_ERR(c_req->c_out);
10241027
}
10251028
}
@@ -1036,10 +1039,12 @@ static void sec_cipher_unmap(struct sec_ctx *ctx, struct sec_req *req,
10361039
if (req->use_pbuf) {
10371040
sec_cipher_pbuf_unmap(ctx, req, dst);
10381041
} else {
1039-
if (dst != src)
1040-
hisi_acc_sg_buf_unmap(dev, src, req->in);
1041-
1042-
hisi_acc_sg_buf_unmap(dev, dst, c_req->c_out);
1042+
if (dst != src) {
1043+
hisi_acc_sg_buf_unmap(dev, dst, c_req->c_out, DMA_FROM_DEVICE);
1044+
hisi_acc_sg_buf_unmap(dev, src, req->in, DMA_TO_DEVICE);
1045+
} else {
1046+
hisi_acc_sg_buf_unmap(dev, src, req->in, DMA_BIDIRECTIONAL);
1047+
}
10431048
}
10441049
}
10451050

drivers/crypto/hisilicon/sgl.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -210,15 +210,15 @@ static void clear_hw_sgl_sge(struct hisi_acc_hw_sgl *hw_sgl)
210210
* @pool: Pool which hw sgl memory will be allocated in.
211211
* @index: Index of hisi_acc_hw_sgl in pool.
212212
* @hw_sgl_dma: The dma address of allocated hw sgl.
213+
* @dir: DMA direction.
213214
*
214215
* This function builds hw sgl according input sgl, user can use hw_sgl_dma
215216
* as src/dst in its BD. Only support single hw sgl currently.
216217
*/
217218
struct hisi_acc_hw_sgl *
218-
hisi_acc_sg_buf_map_to_hw_sgl(struct device *dev,
219-
struct scatterlist *sgl,
220-
struct hisi_acc_sgl_pool *pool,
221-
u32 index, dma_addr_t *hw_sgl_dma)
219+
hisi_acc_sg_buf_map_to_hw_sgl(struct device *dev, struct scatterlist *sgl,
220+
struct hisi_acc_sgl_pool *pool, u32 index,
221+
dma_addr_t *hw_sgl_dma, enum dma_data_direction dir)
222222
{
223223
struct hisi_acc_hw_sgl *curr_hw_sgl;
224224
unsigned int i, sg_n_mapped;
@@ -232,7 +232,7 @@ hisi_acc_sg_buf_map_to_hw_sgl(struct device *dev,
232232

233233
sg_n = sg_nents(sgl);
234234

235-
sg_n_mapped = dma_map_sg(dev, sgl, sg_n, DMA_BIDIRECTIONAL);
235+
sg_n_mapped = dma_map_sg(dev, sgl, sg_n, dir);
236236
if (!sg_n_mapped) {
237237
dev_err(dev, "DMA mapping for SG error!\n");
238238
return ERR_PTR(-EINVAL);
@@ -276,16 +276,17 @@ EXPORT_SYMBOL_GPL(hisi_acc_sg_buf_map_to_hw_sgl);
276276
* @dev: The device which hw sgl belongs to.
277277
* @sgl: Related scatterlist.
278278
* @hw_sgl: Virtual address of hw sgl.
279+
* @dir: DMA direction.
279280
*
280281
* This function unmaps allocated hw sgl.
281282
*/
282283
void hisi_acc_sg_buf_unmap(struct device *dev, struct scatterlist *sgl,
283-
struct hisi_acc_hw_sgl *hw_sgl)
284+
struct hisi_acc_hw_sgl *hw_sgl, enum dma_data_direction dir)
284285
{
285286
if (!dev || !sgl || !hw_sgl)
286287
return;
287288

288-
dma_unmap_sg(dev, sgl, sg_nents(sgl), DMA_BIDIRECTIONAL);
289+
dma_unmap_sg(dev, sgl, sg_nents(sgl), dir);
289290
clear_hw_sgl_sge(hw_sgl);
290291
hw_sgl->entry_sum_in_chain = 0;
291292
hw_sgl->entry_sum_in_sgl = 0;

drivers/crypto/hisilicon/zip/zip_crypto.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,8 @@ static int hisi_zip_do_work(struct hisi_zip_qp_ctx *qp_ctx,
224224
return -EINVAL;
225225

226226
req->hw_src = hisi_acc_sg_buf_map_to_hw_sgl(dev, a_req->src, pool,
227-
req->req_id << 1, &req->dma_src);
227+
req->req_id << 1, &req->dma_src,
228+
DMA_TO_DEVICE);
228229
if (IS_ERR(req->hw_src)) {
229230
dev_err(dev, "failed to map the src buffer to hw sgl (%ld)!\n",
230231
PTR_ERR(req->hw_src));
@@ -233,7 +234,7 @@ static int hisi_zip_do_work(struct hisi_zip_qp_ctx *qp_ctx,
233234

234235
req->hw_dst = hisi_acc_sg_buf_map_to_hw_sgl(dev, a_req->dst, pool,
235236
(req->req_id << 1) + 1,
236-
&req->dma_dst);
237+
&req->dma_dst, DMA_FROM_DEVICE);
237238
if (IS_ERR(req->hw_dst)) {
238239
ret = PTR_ERR(req->hw_dst);
239240
dev_err(dev, "failed to map the dst buffer to hw slg (%d)!\n",
@@ -258,9 +259,9 @@ static int hisi_zip_do_work(struct hisi_zip_qp_ctx *qp_ctx,
258259
return -EINPROGRESS;
259260

260261
err_unmap_output:
261-
hisi_acc_sg_buf_unmap(dev, a_req->dst, req->hw_dst);
262+
hisi_acc_sg_buf_unmap(dev, a_req->dst, req->hw_dst, DMA_FROM_DEVICE);
262263
err_unmap_input:
263-
hisi_acc_sg_buf_unmap(dev, a_req->src, req->hw_src);
264+
hisi_acc_sg_buf_unmap(dev, a_req->src, req->hw_src, DMA_TO_DEVICE);
264265
return ret;
265266
}
266267

@@ -303,8 +304,8 @@ static void hisi_zip_acomp_cb(struct hisi_qp *qp, void *data)
303304
err = -EIO;
304305
}
305306

306-
hisi_acc_sg_buf_unmap(dev, acomp_req->src, req->hw_src);
307-
hisi_acc_sg_buf_unmap(dev, acomp_req->dst, req->hw_dst);
307+
hisi_acc_sg_buf_unmap(dev, acomp_req->dst, req->hw_dst, DMA_FROM_DEVICE);
308+
hisi_acc_sg_buf_unmap(dev, acomp_req->src, req->hw_src, DMA_TO_DEVICE);
308309

309310
acomp_req->dlen = ops->get_dstlen(sqe);
310311

include/linux/hisi_acc_qm.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -556,9 +556,9 @@ int hisi_qm_mb(struct hisi_qm *qm, u8 cmd, dma_addr_t dma_addr, u16 queue,
556556
struct hisi_acc_sgl_pool;
557557
struct hisi_acc_hw_sgl *hisi_acc_sg_buf_map_to_hw_sgl(struct device *dev,
558558
struct scatterlist *sgl, struct hisi_acc_sgl_pool *pool,
559-
u32 index, dma_addr_t *hw_sgl_dma);
559+
u32 index, dma_addr_t *hw_sgl_dma, enum dma_data_direction dir);
560560
void hisi_acc_sg_buf_unmap(struct device *dev, struct scatterlist *sgl,
561-
struct hisi_acc_hw_sgl *hw_sgl);
561+
struct hisi_acc_hw_sgl *hw_sgl, enum dma_data_direction dir);
562562
struct hisi_acc_sgl_pool *hisi_acc_create_sgl_pool(struct device *dev,
563563
u32 count, u32 sge_nr);
564564
void hisi_acc_free_sgl_pool(struct device *dev,

0 commit comments

Comments
 (0)