Skip to content

Commit 415273f

Browse files
Bharat Bhushangregkh
authored andcommitted
crypto: octeontx2 - Fix address alignment on CN10KB and CN10KA-B0
commit a091a58 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.8+ Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 11a6e7d commit 415273f

File tree

1 file changed

+44
-15
lines changed

1 file changed

+44
-15
lines changed

drivers/crypto/marvell/octeontx2/otx2_cpt_reqmgr.h

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -350,22 +350,48 @@ static inline struct otx2_cpt_inst_info *
350350
cn10k_sgv2_info_create(struct pci_dev *pdev, struct otx2_cpt_req_info *req,
351351
gfp_t gfp)
352352
{
353-
u32 dlen = 0, g_len, sg_len, info_len;
354-
int align = OTX2_CPT_DMA_MINALIGN;
353+
u32 dlen = 0, g_len, s_len, sg_len, info_len;
355354
struct otx2_cpt_inst_info *info;
356-
u16 g_sz_bytes, s_sz_bytes;
357355
u32 total_mem_len;
358356
int i;
359357

360-
g_sz_bytes = ((req->in_cnt + 2) / 3) *
361-
sizeof(struct cn10kb_cpt_sglist_component);
362-
s_sz_bytes = ((req->out_cnt + 2) / 3) *
363-
sizeof(struct cn10kb_cpt_sglist_component);
358+
/* Allocate memory to meet below alignment requirement:
359+
* ------------------------------------
360+
* | struct otx2_cpt_inst_info |
361+
* | (No alignment required) |
362+
* | --------------------------------|
363+
* | | padding for ARCH_DMA_MINALIGN |
364+
* | | alignment |
365+
* |------------------------------------|
366+
* | SG List Gather/Input memory |
367+
* | Length = multiple of 32Bytes |
368+
* | Alignment = 8Byte |
369+
* |---------------------------------- |
370+
* | SG List Scatter/Output memory |
371+
* | Length = multiple of 32Bytes |
372+
* | Alignment = 8Byte |
373+
* | -------------------------------|
374+
* | | padding for 32B alignment |
375+
* |------------------------------------|
376+
* | Result response memory |
377+
* | Alignment = 32Byte |
378+
* ------------------------------------
379+
*/
380+
381+
info_len = sizeof(*info);
382+
383+
g_len = ((req->in_cnt + 2) / 3) *
384+
sizeof(struct cn10kb_cpt_sglist_component);
385+
s_len = ((req->out_cnt + 2) / 3) *
386+
sizeof(struct cn10kb_cpt_sglist_component);
387+
sg_len = g_len + s_len;
364388

365-
g_len = ALIGN(g_sz_bytes, align);
366-
sg_len = ALIGN(g_len + s_sz_bytes, align);
367-
info_len = ALIGN(sizeof(*info), align);
368-
total_mem_len = sg_len + info_len + sizeof(union otx2_cpt_res_s);
389+
/* Allocate extra memory for SG and response address alignment */
390+
total_mem_len = ALIGN(info_len, OTX2_CPT_DPTR_RPTR_ALIGN);
391+
total_mem_len += (ARCH_DMA_MINALIGN - 1) &
392+
~(OTX2_CPT_DPTR_RPTR_ALIGN - 1);
393+
total_mem_len += ALIGN(sg_len, OTX2_CPT_RES_ADDR_ALIGN);
394+
total_mem_len += sizeof(union otx2_cpt_res_s);
369395

370396
info = kzalloc(total_mem_len, gfp);
371397
if (unlikely(!info))
@@ -375,7 +401,8 @@ cn10k_sgv2_info_create(struct pci_dev *pdev, struct otx2_cpt_req_info *req,
375401
dlen += req->in[i].size;
376402

377403
info->dlen = dlen;
378-
info->in_buffer = (u8 *)info + info_len;
404+
info->in_buffer = PTR_ALIGN((u8 *)info + info_len, ARCH_DMA_MINALIGN);
405+
info->out_buffer = info->in_buffer + g_len;
379406
info->gthr_sz = req->in_cnt;
380407
info->sctr_sz = req->out_cnt;
381408

@@ -387,7 +414,7 @@ cn10k_sgv2_info_create(struct pci_dev *pdev, struct otx2_cpt_req_info *req,
387414
}
388415

389416
if (sgv2io_components_setup(pdev, req->out, req->out_cnt,
390-
&info->in_buffer[g_len])) {
417+
info->out_buffer)) {
391418
dev_err(&pdev->dev, "Failed to setup scatter list\n");
392419
goto destroy_info;
393420
}
@@ -404,8 +431,10 @@ cn10k_sgv2_info_create(struct pci_dev *pdev, struct otx2_cpt_req_info *req,
404431
* Get buffer for union otx2_cpt_res_s response
405432
* structure and its physical address
406433
*/
407-
info->completion_addr = info->in_buffer + sg_len;
408-
info->comp_baddr = info->dptr_baddr + sg_len;
434+
info->completion_addr = PTR_ALIGN((info->in_buffer + sg_len),
435+
OTX2_CPT_RES_ADDR_ALIGN);
436+
info->comp_baddr = ALIGN((info->dptr_baddr + sg_len),
437+
OTX2_CPT_RES_ADDR_ALIGN);
409438

410439
return info;
411440

0 commit comments

Comments
 (0)