Devicetree
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Jose Nieto" <jnieto.alaber@gmail.com>,
	"Corentin Labbe" <clabbe@baylibre.com>,
	"Herbert Xu" <herbert@gondor.apana.org.au>
Cc: linux-crypto@vger.kernel.org, linux-amlogic@lists.infradead.org,
	devicetree@vger.kernel.org,
	"Neil Armstrong" <neil.armstrong@linaro.org>,
	"Martin Blumenstingl" <martin.blumenstingl@googlemail.com>,
	"Krzysztof Kozlowski" <krzk+dt@kernel.org>,
	"Rob Herring" <robh@kernel.org>,
	"Conor Dooley" <conor+dt@kernel.org>,
	"Alexey Romanov" <romanov.alexey2000@gmail.com>
Subject: Re: [PATCH v14 11/30] crypto: amlogic - Process more than MAXDESCS descriptors
Date: Fri, 27 Nov 2026 12:13:16 +0000	[thread overview]
Message-ID: <sashiko-outbox-237334@kernel.org> (raw)
In-Reply-To: <20261127120103.1532882-12-jnieto.alaber@gmail.com>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] crypto: amlogic: unmapped IV and bad keyiv DMA length in
  meson_cipher()
- [High] crypto: amlogic: bad sg_virt() offset and DMA sync in
  meson_kick_hardware()
--

commit 4d7b0e2239c95f15cc0e337acf98505c54b39ed1
Author: Alexey Romanov <romanov.alexey2000@gmail.com>

crypto: amlogic - Process more than MAXDESCS descriptors

This commit rewrites the Amlogic GXL cipher descriptor handling to split
large skcipher requests across multiple hardware batches and makes the
number of key/IV setup descriptors configurable via platform data.

> diff --git a/drivers/crypto/amlogic/amlogic-gxl-cipher.c b/drivers/crypto/amlogic/amlogic-gxl-cipher.c
> index c17ce5cc70a87..cb6b959bb1f96 100644
> --- a/drivers/crypto/amlogic/amlogic-gxl-cipher.c
> +++ b/drivers/crypto/amlogic/amlogic-gxl-cipher.c

[ ... ]

> +static void meson_setup_keyiv_descs(struct cipher_ctx *ctx)
> +{
> +	struct meson_cipher_req_ctx *rctx = skcipher_request_ctx(ctx->areq);
> +	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(ctx->areq);
> +	struct skcipher_alg *alg = crypto_skcipher_alg(tfm);
> +	struct meson_alg_template *algt = container_of(alg,
> +		struct meson_alg_template, alg.skcipher.base);
> +	struct meson_cipher_tfm_ctx *op = crypto_skcipher_ctx(tfm);
> +	struct meson_dev *mc = op->mc;
> +	unsigned int ivsize = crypto_skcipher_ivsize(tfm);
> +	unsigned int blockmode = algt->blockmode;
> +	int i;
> +
> +	if (ctx->tloffset)
> +		return;
> +
> +	if (blockmode == DESC_OPMODE_CBC) {
> +		memcpy(ctx->bkeyiv + AES_MAX_KEY_SIZE, ctx->areq->iv, ivsize);
> +		dma_sync_single_for_device(mc->dev, ctx->keyiv.addr,
> +					   ctx->keyiv.len, DMA_TO_DEVICE);
> +	}
> +
> +	for (i = 0; i < mc->pdata->setup_desc_cnt; i++) {
> +		struct meson_desc *desc =
> +			&mc->chanlist[rctx->flow].tl[ctx->tloffset];
> +		int offset = i * 16;
> +
> +		desc->t_src = cpu_to_le32(ctx->keyiv.addr + offset);
> +		desc->t_dst = cpu_to_le32(offset);
> +		desc->t_status = cpu_to_le32(DESC_OWN | DESC_MODE_KEY | ctx->keyiv.len);
> +
> +		ctx->tloffset++;
> +	}
> +}

[Severity: High]
Does meson_setup_keyiv_descs() leave the CBC IV outside the DMA mapping and
program each setup descriptor with ctx->keyiv.len instead of 16 bytes?

In meson_cipher(), ctx.keyiv.len is set to op->keylen (or AES_MAX_KEY_SIZE
for AES-192), so dma_map_single() and dma_sync_single_for_device() only
cover the first 16 or 32 bytes of ctx.bkeyiv and never map or flush the CBC
IV copied to offset 32 (AES_MAX_KEY_SIZE).  In addition, setting each of the
setup_desc_cnt descriptors at offset = i * 16 to transfer ctx->keyiv.len
bytes causes descriptors i > 0 to DMA-read outside the mapped range and up
to offset 64, which is 16 bytes past the end of the 48-byte ctx.bkeyiv
buffer.

Later commits in this series ("crypto: amlogic - Avoid kzalloc in engine
thread" and "crypto: amlogic: fix setup descriptor lengths and CBC IV copy")
partially adjust this, but still leave ECB mode mapping 16 or 32 bytes while
issuing 3 descriptors of length ctx->keyiv.len / 3 (5 or 10 bytes instead of
16) up to offset 32, and remove the IV update in meson_setup_keyiv_descs()
needed between multi-batch CBC kicks.

> +static bool meson_setup_data_descs(struct cipher_ctx *ctx)
> +{
> +	struct meson_cipher_req_ctx *rctx = skcipher_request_ctx(ctx->areq);
> +	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(ctx->areq);
> +	struct meson_cipher_tfm_ctx *op = crypto_skcipher_ctx(tfm);
> +	struct skcipher_alg *alg = crypto_skcipher_alg(tfm);
> +	struct meson_alg_template *algt = container_of(alg,
> +						       struct meson_alg_template,
> +						       alg.skcipher.base);
> +	struct meson_dev *mc = op->mc;
> +	struct meson_desc *desc = &mc->chanlist[rctx->flow].tl[ctx->tloffset];
> +	unsigned int blocksize = crypto_skcipher_blocksize(tfm);
> +	unsigned int blockmode = algt->blockmode;
> +	unsigned int maxlen = rounddown(DESC_MAXLEN, blocksize);
> +	unsigned int todo;
> +	u32 v;
> +
> +	ctx->tloffset++;
> +
> +	todo = min(ctx->cryptlen, maxlen);
> +	todo = min(todo, ctx->cryptlen);
> +	todo = min(todo, sg_dma_len(ctx->src_sg) - ctx->src_offset);
> +	todo = min(todo, sg_dma_len(ctx->dst_sg) - ctx->dst_offset);
> +
> +	desc->t_src = cpu_to_le32(sg_dma_address(ctx->src_sg) + ctx->src_offset);
> +	desc->t_dst = cpu_to_le32(sg_dma_address(ctx->dst_sg) + ctx->dst_offset);
> +
> +	ctx->cryptlen -= todo;
> +	ctx->src_offset += todo;
> +	ctx->dst_offset += todo;
> +
> +	v = DESC_OWN | blockmode | op->keymode | todo;
> +	if (rctx->op_dir == MESON_ENCRYPT)
> +		v |= DESC_ENCRYPTION;
> +
> +	if (!ctx->cryptlen || ctx->tloffset == MAXDESC)
> +		v |= DESC_LAST;
> +
> +	desc->t_status = cpu_to_le32(v);
> +
> +	return v & DESC_LAST;
> +}
> +
> +static int meson_kick_hardware(struct cipher_ctx *ctx)
> +{
> +	struct meson_cipher_req_ctx *rctx = skcipher_request_ctx(ctx->areq);
> +	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(ctx->areq);
> +	struct meson_cipher_tfm_ctx *op = crypto_skcipher_ctx(tfm);
> +	struct skcipher_alg *alg = crypto_skcipher_alg(tfm);
> +	struct meson_alg_template *algt = container_of(alg,
> +						       struct meson_alg_template,
> +						       alg.skcipher.base);
> +	struct meson_dev *mc = op->mc;
> +	unsigned int ivsize = crypto_skcipher_ivsize(tfm);
> +	unsigned int blockmode = algt->blockmode;
> +	enum dma_data_direction new_iv_dir;
> +	struct scatterlist *sg_head;
> +	void *new_iv;
> +	int err;
> +
> +	if (blockmode == DESC_OPMODE_CBC) {
> +		struct scatterlist *sg_current;
> +		unsigned int offset;
> +
> +		if (rctx->op_dir == MESON_ENCRYPT) {
> +			sg_current = ctx->dst_sg;
> +			sg_head = ctx->areq->dst;
> +			offset = ctx->dst_offset;
> +			new_iv_dir = DMA_FROM_DEVICE;
> +		} else {
> +			sg_current = ctx->src_sg;
> +			sg_head = ctx->areq->src;
> +			offset = ctx->src_offset;
> +			new_iv_dir = DMA_TO_DEVICE;
> +		}
> +
> +		if (ctx->areq->src == ctx->areq->dst)
> +			new_iv_dir = DMA_BIDIRECTIONAL;
> +
> +		offset -= ivsize;
> +		new_iv = sg_virt(sg_current) + offset;
> +	}
> +
> +	if (blockmode == DESC_OPMODE_CBC &&
> +	    rctx->op_dir == MESON_DECRYPT) {
> +		dma_sync_sg_for_cpu(mc->dev, sg_head,
> +				    sg_nents(sg_head), new_iv_dir);
> +		memcpy(ctx->areq->iv, new_iv, ivsize);
> +	}
> +
> +	reinit_completion(&mc->chanlist[rctx->flow].complete);
> +	meson_dma_start(mc, rctx->flow);
> +	err = wait_for_completion_interruptible_timeout(&mc->chanlist[rctx->flow].complete,
> +							msecs_to_jiffies(500));
> +	if (err == 0) {
> +		dev_err(mc->dev, "DMA timeout for flow %d\n", rctx->flow);
> +		return -EINVAL;
> +	} else if (err < 0) {
> +		dev_err(mc->dev, "Waiting for DMA completion is failed (%d)\n", err);
> +		return err;
> +	}
> +
> +	if (blockmode == DESC_OPMODE_CBC &&
> +	    rctx->op_dir == MESON_ENCRYPT) {
> +		dma_sync_sg_for_cpu(mc->dev, sg_head,
> +				    sg_nents(sg_head), new_iv_dir);
> +		memcpy(ctx->areq->iv, new_iv, ivsize);
> +	}

[Severity: High]
Can computing new_iv as sg_virt(sg_current) + offset read past the end of
sg_current's page when dma_map_sg() coalesces scatterlist entries?

meson_setup_data_descs() and meson_cipher() advance ctx->src_offset and
ctx->dst_offset using sg_dma_len(), which can exceed sg_current->length when
adjacent entries are merged by dma_map_sg().  Indexing sg_virt(sg_current)
by that DMA segment offset reads outside sg_current's virtual buffer (or
from the wrong scatterlist entry after coalesced segments).

Also, meson_kick_hardware() calls dma_sync_sg_for_cpu() over the entire
scatterlist (walking sg_nents(sg_head) on every batch) to read new_iv
without a matching dma_sync_sg_for_device() before meson_dma_start() on
decrypt or before the next batch's meson_dma_start() on multi-batch encrypt,
leaving dirty or stale CPU cache lines across the DMA transfer.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261127120103.1532882-1-jnieto.alaber@gmail.com?part=11

  reply	other threads:[~2026-11-27 12:13 UTC|newest]

Thread overview: 81+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-26 19:32 [PATCH v13 00/25] crypto: amlogic: add support for G12/AXG/A1/S4 and misc fixes Jose Nieto
2026-11-26 19:32 ` [PATCH v13 01/25] dt-bindings: crypto: amlogic,gxl-crypto: correct clk and interrupt lines Jose Nieto
2026-11-26 19:43   ` sashiko-bot
2026-11-26 19:32 ` [PATCH v13 02/25] dt-bindings: crypto: amlogic,gxl-crypto: support new SoC's Jose Nieto
2026-11-26 19:32 ` [PATCH v13 03/25] crypto: amlogic - Don't hardcode IRQ count Jose Nieto
2026-11-26 19:42   ` sashiko-bot
2026-11-26 19:32 ` [PATCH v13 04/25] crypto: amlogic - Add platform data Jose Nieto
2026-11-26 19:32 ` [PATCH v13 05/25] crypto: amlogic - Remove clock input Jose Nieto
2026-11-26 19:44   ` sashiko-bot
2026-11-26 19:32 ` [PATCH v13 06/25] crypto: amlogic - Add MMIO helpers Jose Nieto
2026-11-26 19:32 ` [PATCH v13 07/25] crypto: amlogic - Move get_engine_number() Jose Nieto
2026-11-26 19:32 ` [PATCH v13 08/25] crypto: amlogic - Drop status field from meson_flow Jose Nieto
2026-11-26 19:32 ` [PATCH v13 09/25] crypto: amlogic - Move algs definition and cipher API to cipher.c Jose Nieto
2026-11-26 19:41   ` sashiko-bot
2026-11-26 19:32 ` [PATCH v13 10/25] crypto: amlogic - Cleanup defines Jose Nieto
2026-11-26 19:32 ` [PATCH v13 11/25] crypto: amlogic - Process more than MAXDESCS descriptors Jose Nieto
2026-11-26 19:45   ` sashiko-bot
2026-11-26 19:32 ` [PATCH v13 12/25] crypto: amlogic - Avoid kzalloc in engine thread Jose Nieto
2026-11-26 19:48   ` sashiko-bot
2026-11-26 19:32 ` [PATCH v13 13/25] crypto: amlogic - Introduce hasher Jose Nieto
2026-11-26 19:56   ` sashiko-bot
2026-11-26 19:32 ` [PATCH v13 14/25] crypto: amlogic - Use fallback for 192-bit keys Jose Nieto
2026-11-26 19:32 ` [PATCH v13 15/25] crypto: amlogic - Add support for G12-series Jose Nieto
2026-11-26 19:55   ` sashiko-bot
2026-11-26 19:32 ` [PATCH v13 16/25] crypto: amlogic - Add support for AXG-series Jose Nieto
2026-11-26 19:32 ` [PATCH v13 17/25] crypto: amlogic - Add support for A1-series Jose Nieto
2026-11-26 19:48   ` sashiko-bot
2026-11-26 19:32 ` [PATCH v13 18/25] crypto: amlogic: g12: drop CRYPTO_ALG_ALLOCATES_MEMORY Jose Nieto
2026-11-26 19:32 ` [PATCH v13 19/25] crypto: amlogic: hasher: add one-shot digest path Jose Nieto
2026-11-26 19:50   ` sashiko-bot
2026-11-26 19:32 ` [PATCH v13 20/25] crypto: amlogic: drop unused field and stale includes Jose Nieto
2026-11-26 19:32 ` [PATCH v13 21/25] arm64: dts: amlogic: gxl: correct crypto node definition Jose Nieto
2026-11-26 19:32 ` [PATCH v13 22/25] arm64: dts: amlogic: a1: add crypto node Jose Nieto
2026-11-26 20:01   ` sashiko-bot
2026-11-26 19:32 ` [PATCH v13 23/25] arm64: dts: amlogic: s4: " Jose Nieto
2026-11-26 20:02   ` sashiko-bot
2026-11-26 19:32 ` [PATCH v13 24/25] arm64: dts: amlogic: g12: " Jose Nieto
2026-11-26 19:32 ` [PATCH v13 25/25] arm64: dts: amlogic: axg: " Jose Nieto
2026-11-26 20:00   ` sashiko-bot
2026-11-27 12:00 ` [PATCH v14 00/30] crypto: amlogic: add support for G12/AXG/A1/S4 and misc fixes Jose Nieto
2026-11-27 12:00   ` [PATCH v14 01/30] dt-bindings: crypto: amlogic,gxl-crypto: correct clk and interrupt lines Jose Nieto
2026-11-27 12:00   ` [PATCH v14 02/30] dt-bindings: crypto: amlogic,gxl-crypto: support new SoC's Jose Nieto
2026-11-27 12:00   ` [PATCH v14 03/30] crypto: amlogic - Don't hardcode IRQ count Jose Nieto
2026-11-27 12:11     ` sashiko-bot
2026-11-27 12:00   ` [PATCH v14 04/30] crypto: amlogic - Add platform data Jose Nieto
2026-11-27 12:00   ` [PATCH v14 05/30] crypto: amlogic - Remove clock input Jose Nieto
2026-11-27 12:14     ` sashiko-bot
2026-11-27 12:00   ` [PATCH v14 06/30] crypto: amlogic - Add MMIO helpers Jose Nieto
2026-11-27 12:00   ` [PATCH v14 07/30] crypto: amlogic - Move get_engine_number() Jose Nieto
2026-11-27 12:00   ` [PATCH v14 08/30] crypto: amlogic - Drop status field from meson_flow Jose Nieto
2026-11-27 12:00   ` [PATCH v14 09/30] crypto: amlogic - Move algs definition and cipher API to cipher.c Jose Nieto
2026-11-27 12:14     ` sashiko-bot
2026-11-27 12:00   ` [PATCH v14 10/30] crypto: amlogic - Cleanup defines Jose Nieto
2026-11-27 12:00   ` [PATCH v14 11/30] crypto: amlogic - Process more than MAXDESCS descriptors Jose Nieto
2026-11-27 12:13     ` sashiko-bot [this message]
2026-11-27 12:00   ` [PATCH v14 12/30] crypto: amlogic - Avoid kzalloc in engine thread Jose Nieto
2026-11-27 12:18     ` sashiko-bot
2026-11-27 12:00   ` [PATCH v14 13/30] crypto: amlogic - Introduce hasher Jose Nieto
2026-11-27 12:24     ` sashiko-bot
2026-11-27 12:00   ` [PATCH v14 14/30] crypto: amlogic - Use fallback for 192-bit keys Jose Nieto
2026-11-27 12:00   ` [PATCH v14 15/30] crypto: amlogic - Add support for G12-series Jose Nieto
2026-11-27 12:27     ` sashiko-bot
2026-11-27 12:00   ` [PATCH v14 16/30] crypto: amlogic - Add support for AXG-series Jose Nieto
2026-11-27 12:00   ` [PATCH v14 17/30] crypto: amlogic - Add support for A1-series Jose Nieto
2026-11-27 12:00   ` [PATCH v14 18/30] crypto: amlogic: g12: drop CRYPTO_ALG_ALLOCATES_MEMORY Jose Nieto
2026-11-27 12:00   ` [PATCH v14 19/30] crypto: amlogic: hasher: add one-shot digest path Jose Nieto
2026-11-27 12:22     ` sashiko-bot
2026-11-27 12:00   ` [PATCH v14 20/30] crypto: amlogic: drop unused field and stale includes Jose Nieto
2026-11-27 12:00   ` [PATCH v14 21/30] arm64: dts: amlogic: gxl: correct crypto node definition Jose Nieto
2026-11-27 12:00   ` [PATCH v14 22/30] arm64: dts: amlogic: a1: add crypto node Jose Nieto
2026-11-27 12:00   ` [PATCH v14 23/30] arm64: dts: amlogic: s4: " Jose Nieto
2026-11-27 12:00   ` [PATCH v14 24/30] arm64: dts: amlogic: g12: " Jose Nieto
2026-11-27 12:36     ` sashiko-bot
2026-11-27 12:00   ` [PATCH v14 25/30] arm64: dts: amlogic: axg: " Jose Nieto
2026-11-27 12:00   ` [PATCH v14 26/30] dt-bindings: crypto: amlogic,gxl-crypto: keep standard property order Jose Nieto
2026-11-27 12:01   ` [PATCH v14 27/30] crypto: amlogic: fix probe and algorithm registration error paths Jose Nieto
2026-11-27 12:21     ` sashiko-bot
2026-11-27 12:01   ` [PATCH v14 28/30] crypto: amlogic: fix setup descriptor lengths and CBC IV copy Jose Nieto
2026-11-27 12:20     ` sashiko-bot
2026-11-27 12:01   ` [PATCH v14 29/30] crypto: amlogic: drop redundant A1 platform data Jose Nieto
2026-11-27 12:01   ` [PATCH v14 30/30] arm64: dts: amlogic: use the system clock for A1/S4 crypto Jose Nieto

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-237334@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=clabbe@baylibre.com \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=herbert@gondor.apana.org.au \
    --cc=jnieto.alaber@gmail.com \
    --cc=krzk+dt@kernel.org \
    --cc=linux-amlogic@lists.infradead.org \
    --cc=linux-crypto@vger.kernel.org \
    --cc=martin.blumenstingl@googlemail.com \
    --cc=neil.armstrong@linaro.org \
    --cc=robh@kernel.org \
    --cc=romanov.alexey2000@gmail.com \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox