Skip to content

Commit 11a6e7d

Browse files
Bharat Bhushangregkh
authored andcommitted
crypto: octeontx2 - Fix address alignment on CN10K A0/A1 and OcteonTX2
commit 2e13163 upstream. octeontx2 crypto driver allocates memory using kmalloc/kzalloc, and uses this memory for dma (does dma_map_single()). It assumes that kmalloc/kzalloc will return 128-byte aligned address. But kmalloc/kzalloc returns 8-byte aligned address after below changes: "9382bc44b5f5 arm64: allow kmalloc() caches aligned to the smaller cache_line_size() Memory allocated are used for following purpose: - Input data or scatter list address - 8-Byte alignment - Output data or gather list address - 8-Byte alignment - Completion address - 32-Byte alignment. This patch ensures all addresses are aligned as mentioned above. Signed-off-by: Bharat Bhushan <bbhushan2@marvell.com> Cc: <stable@vger.kernel.org> # v6.5+ Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 6208992 commit 11a6e7d

File tree

1 file changed

+51
-15
lines changed

1 file changed

+51
-15
lines changed

drivers/crypto/marvell/octeontx2/otx2_cpt_reqmgr.h

Lines changed: 51 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434
#define SG_COMP_2 2
3535
#define SG_COMP_1 1
3636

37+
#define OTX2_CPT_DPTR_RPTR_ALIGN 8
38+
#define OTX2_CPT_RES_ADDR_ALIGN 32
39+
3740
union otx2_cpt_opcode {
3841
u16 flags;
3942
struct {
@@ -417,10 +420,9 @@ static inline struct otx2_cpt_inst_info *
417420
otx2_sg_info_create(struct pci_dev *pdev, struct otx2_cpt_req_info *req,
418421
gfp_t gfp)
419422
{
420-
int align = OTX2_CPT_DMA_MINALIGN;
421423
struct otx2_cpt_inst_info *info;
422-
u32 dlen, align_dlen, info_len;
423-
u16 g_sz_bytes, s_sz_bytes;
424+
u32 dlen, info_len;
425+
u16 g_len, s_len;
424426
u32 total_mem_len;
425427

426428
if (unlikely(req->in_cnt > OTX2_CPT_MAX_SG_IN_CNT ||
@@ -429,22 +431,54 @@ otx2_sg_info_create(struct pci_dev *pdev, struct otx2_cpt_req_info *req,
429431
return NULL;
430432
}
431433

432-
g_sz_bytes = ((req->in_cnt + 3) / 4) *
433-
sizeof(struct otx2_cpt_sglist_component);
434-
s_sz_bytes = ((req->out_cnt + 3) / 4) *
435-
sizeof(struct otx2_cpt_sglist_component);
434+
/* Allocate memory to meet below alignment requirement:
435+
* ------------------------------------
436+
* | struct otx2_cpt_inst_info |
437+
* | (No alignment required) |
438+
* | --------------------------------|
439+
* | | padding for ARCH_DMA_MINALIGN |
440+
* | | alignment |
441+
* |------------------------------------|
442+
* | SG List Header of 8 Byte |
443+
* |------------------------------------|
444+
* | SG List Gather/Input memory |
445+
* | Length = multiple of 32Bytes |
446+
* | Alignment = 8Byte |
447+
* |---------------------------------- |
448+
* | SG List Scatter/Output memory |
449+
* | Length = multiple of 32Bytes |
450+
* | Alignment = 8Byte |
451+
* | -------------------------------|
452+
* | | padding for 32B alignment |
453+
* |------------------------------------|
454+
* | Result response memory |
455+
* | Alignment = 32Byte |
456+
* ------------------------------------
457+
*/
436458

437-
dlen = g_sz_bytes + s_sz_bytes + SG_LIST_HDR_SIZE;
438-
align_dlen = ALIGN(dlen, align);
439-
info_len = ALIGN(sizeof(*info), align);
440-
total_mem_len = align_dlen + info_len + sizeof(union otx2_cpt_res_s);
459+
info_len = sizeof(*info);
460+
461+
g_len = ((req->in_cnt + 3) / 4) *
462+
sizeof(struct otx2_cpt_sglist_component);
463+
s_len = ((req->out_cnt + 3) / 4) *
464+
sizeof(struct otx2_cpt_sglist_component);
465+
466+
dlen = g_len + s_len + SG_LIST_HDR_SIZE;
467+
468+
/* Allocate extra memory for SG and response address alignment */
469+
total_mem_len = ALIGN(info_len, OTX2_CPT_DPTR_RPTR_ALIGN);
470+
total_mem_len += (ARCH_DMA_MINALIGN - 1) &
471+
~(OTX2_CPT_DPTR_RPTR_ALIGN - 1);
472+
total_mem_len += ALIGN(dlen, OTX2_CPT_RES_ADDR_ALIGN);
473+
total_mem_len += sizeof(union otx2_cpt_res_s);
441474

442475
info = kzalloc(total_mem_len, gfp);
443476
if (unlikely(!info))
444477
return NULL;
445478

446479
info->dlen = dlen;
447-
info->in_buffer = (u8 *)info + info_len;
480+
info->in_buffer = PTR_ALIGN((u8 *)info + info_len, ARCH_DMA_MINALIGN);
481+
info->out_buffer = info->in_buffer + SG_LIST_HDR_SIZE + g_len;
448482

449483
((u16 *)info->in_buffer)[0] = req->out_cnt;
450484
((u16 *)info->in_buffer)[1] = req->in_cnt;
@@ -460,7 +494,7 @@ otx2_sg_info_create(struct pci_dev *pdev, struct otx2_cpt_req_info *req,
460494
}
461495

462496
if (setup_sgio_components(pdev, req->out, req->out_cnt,
463-
&info->in_buffer[8 + g_sz_bytes])) {
497+
info->out_buffer)) {
464498
dev_err(&pdev->dev, "Failed to setup scatter list\n");
465499
goto destroy_info;
466500
}
@@ -476,8 +510,10 @@ otx2_sg_info_create(struct pci_dev *pdev, struct otx2_cpt_req_info *req,
476510
* Get buffer for union otx2_cpt_res_s response
477511
* structure and its physical address
478512
*/
479-
info->completion_addr = info->in_buffer + align_dlen;
480-
info->comp_baddr = info->dptr_baddr + align_dlen;
513+
info->completion_addr = PTR_ALIGN((info->in_buffer + dlen),
514+
OTX2_CPT_RES_ADDR_ALIGN);
515+
info->comp_baddr = ALIGN((info->dptr_baddr + dlen),
516+
OTX2_CPT_RES_ADDR_ALIGN);
481517

482518
return info;
483519

0 commit comments

Comments
 (0)