Skip to contentSkip to conversation

dmaengine: introduce sg_nents_for_dma() and convert users

PATCH Superseded Andy Shevchenko 3 participants 23 messages Nov 10, 2025 – Nov 24, 2025

Subsystems (11)

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#1 lore raw

PATCH v2 1/13 scatterlist: introduce sg_nents_for_dma() helper

In reply to Andy Shevchenko

Sometimes the user needs to split each entry on the mapped scatter list due to DMA length constrains. This helper returns a number of entities assuming that each of them is not bigger than supplied maximum length.

---
 include/linux/scatterlist.h |  2 ++
 lib/scatterlist.c           | 25 +++++++++++++++++++++++++
 2 files changed, 27 insertions(+)
include/linux/scatterlist.h +2 -0
index 29f6ceb98d74..6de1a2434299 100644
@@ -441,6 +441,8 @@ static inline void sg_init_marker(struct scatterlist *sgl,
 
 int sg_nents(struct scatterlist *sg);
 int sg_nents_for_len(struct scatterlist *sg, u64 len);
+int sg_nents_for_dma(struct scatterlist *sgl, unsigned int sglen, size_t len);
+
 struct scatterlist *sg_last(struct scatterlist *s, unsigned int);
 void sg_init_table(struct scatterlist *, unsigned int);
 void sg_init_one(struct scatterlist *, const void *, unsigned int);
lib/scatterlist.c +25 -0
index 4af1c8b0775a..4d1a010f863c 100644
@@ -64,6 +64,31 @@ int sg_nents_for_len(struct scatterlist *sg, u64 len)
 }
 EXPORT_SYMBOL(sg_nents_for_len);
 
+/**
+ * sg_nents_for_dma - return the count of DMA-capable entries in scatterlist
+ * @sgl:	The scatterlist
+ * @sglen:	The current number of entries
+ * @len:	The maximum length of DMA-capable block
+ *
+ * Description:
+ * Determines the number of entries in @sgl which would be permitted in
+ * DMA-capable transfer if list had been split accordingly, taking into
+ * account chaining as well.
+ *
+ * Returns:
+ *   the number of sgl entries needed
+ *
+ **/
+int sg_nents_for_dma(struct scatterlist *sgl, unsigned int sglen, size_t len)
+{
+	struct scatterlist *sg;
+	int i, nents = 0;
+
+	for_each_sg(sgl, sg, sglen, i)
+		nents += DIV_ROUND_UP(sg_dma_len(sg), len);
+	return nents;
+}
+
 /**
  * sg_last - return the last scatterlist entry in a list
  * @sgl:	First entry in the scatterlist
Signature · 1 line
2.50.1
#6 lore raw

PATCH v2 2/13 dmaengine: altera-msgdma: use sg_nents_for_dma() helper

In reply to Andy Shevchenko

Instead of open coded variant let's use recently introduced helper.

---
 drivers/dma/altera-msgdma.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/dma/altera-msgdma.c +2 -3
index a203fdd84950..48d2a0e638bb 100644
@@ -396,13 +396,12 @@ msgdma_prep_slave_sg(struct dma_chan *dchan, struct scatterlist *sgl,
 	void *desc = NULL;
 	size_t len, avail;
 	dma_addr_t dma_dst, dma_src;
-	u32 desc_cnt = 0, i;
 	struct scatterlist *sg;
+	u32 desc_cnt;
 	u32 stride;
 	unsigned long irqflags;
 
-	for_each_sg(sgl, sg, sg_len, i)
-		desc_cnt += DIV_ROUND_UP(sg_dma_len(sg), MSGDMA_MAX_TRANS_LEN);
+	desc_cnt = sg_nents_for_dma(sgl, sg_len, MSGDMA_MAX_TRANS_LEN);
 
 	spin_lock_irqsave(&mdev->lock, irqflags);
 	if (desc_cnt > mdev->desc_free_cnt) {
Signature · 1 line
2.50.1
#7 lore raw

PATCH v2 3/13 dmaengine: axi-dmac: use sg_nents_for_dma() helper

In reply to Andy Shevchenko

Instead of open coded variant let's use recently introduced helper.

---
 drivers/dma/dma-axi-dmac.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
drivers/dma/dma-axi-dmac.c +1 -4
index 5b06b0dc67ee..5d4436e7d2ee 100644
@@ -674,10 +674,7 @@ static struct dma_async_tx_descriptor *axi_dmac_prep_slave_sg(
 	if (direction != chan->direction)
 		return NULL;
 
-	num_sgs = 0;
-	for_each_sg(sgl, sg, sg_len, i)
-		num_sgs += DIV_ROUND_UP(sg_dma_len(sg), chan->max_length);
-
+	num_sgs = sg_nents_for_dma(sgl, sg_len, chan->max_length);
 	desc = axi_dmac_alloc_desc(chan, num_sgs);
 	if (!desc)
 		return NULL;
Signature · 1 line
2.50.1
#0 lore raw

v2 cover dmaengine: introduce sg_nents_for_dma() and convert users

A handful of the DMAengine drivers use same routine to calculate the number of SG entries needed for the given DMA transfer. Provide a common helper for them and convert.

I left the new helper on SG level of API because brief grepping shows potential candidates outside of DMA engine, e.g.:

drivers/crypto/chelsio/chcr_algo.c:154: nents += DIV_ROUND_UP(less, entlen); drivers/spi/spi-stm32.c:1495: /* Count the number of entries needed */

Changelog v2:
- dropped outdated patches (only 9 years passed :-)
- rebased on top of the current kernel
- left API SG wide It might

v1: https://patchwork.kernel.org/project/linux-dmaengine/patch/[email protected]/

Andy Shevchenko (13):
scatterlist: introduce sg_nents_for_dma() helper
dmaengine: altera-msgdma: use sg_nents_for_dma() helper
dmaengine: axi-dmac: use sg_nents_for_dma() helper
dmaengine: bcm2835-dma: use sg_nents_for_dma() helper
dmaengine: dw-axi-dmac: use sg_nents_for_dma() helper
dmaengine: k3dma: use sg_nents_for_dma() helper
dmaengine: lgm: use sg_nents_for_dma() helper
dmaengine: pxa-dma: use sg_nents_for_dma() helper
dmaengine: qcom: adm: use sg_nents_for_dma() helper
dmaengine: qcom: bam_dma: use sg_nents_for_dma() helper
dmaengine: sa11x0: use sg_nents_for_dma() helper
dmaengine: sh: use sg_nents_for_dma() helper
dmaengine: xilinx: xdma: use sg_nents_for_dma() helper

 drivers/dma/altera-msgdma.c                   |  5 ++--
 drivers/dma/bcm2835-dma.c                     | 19 +-------------
 drivers/dma/dma-axi-dmac.c                    |  5 +---
 .../dma/dw-axi-dmac/dw-axi-dmac-platform.c    |  6 ++---
 drivers/dma/k3dma.c                           |  9 ++-----
 drivers/dma/lgm/lgm-dma.c                     |  9 ++-----
 drivers/dma/pxa_dma.c                         |  5 ++--
 drivers/dma/qcom/bam_dma.c                    |  9 ++-----
 drivers/dma/qcom/qcom_adm.c                   |  9 +++----
 drivers/dma/sa11x0-dma.c                      |  6 ++---
 drivers/dma/sh/shdma-base.c                   |  5 ++--
 drivers/dma/xilinx/xdma.c                     |  6 ++---
 include/linux/scatterlist.h                   |  2 ++
 lib/scatterlist.c                             | 25 +++++++++++++++++++
 14 files changed, 51 insertions(+), 69 deletions(-)
Signature · 1 line
2.50.1
#8 lore raw

PATCH v2 4/13 dmaengine: bcm2835-dma: use sg_nents_for_dma() helper

Instead of open coded variant let's use recently introduced helper.

---
 drivers/dma/bcm2835-dma.c | 19 +------------------
 1 file changed, 1 insertion(+), 18 deletions(-)
drivers/dma/bcm2835-dma.c +1 -18
index 0117bb2e8591..802b23be2fd8 100644
@@ -260,23 +260,6 @@ static void bcm2835_dma_create_cb_set_length(
 	control_block->info |= finalextrainfo;
 }
 
-static inline size_t bcm2835_dma_count_frames_for_sg(
-	struct bcm2835_chan *c,
-	struct scatterlist *sgl,
-	unsigned int sg_len)
-{
-	size_t frames = 0;
-	struct scatterlist *sgent;
-	unsigned int i;
-	size_t plength = bcm2835_dma_max_frame_length(c);
-
-	for_each_sg(sgl, sgent, sg_len, i)
-		frames += bcm2835_dma_frames_for_length(
-			sg_dma_len(sgent), plength);
-
-	return frames;
-}
-
 /**
  * bcm2835_dma_create_cb_chain - create a control block and fills data in
  *
@@ -672,7 +655,7 @@ static struct dma_async_tx_descriptor *bcm2835_dma_prep_slave_sg(
 	}
 
 	/* count frames in sg list */
-	frames = bcm2835_dma_count_frames_for_sg(c, sgl, sg_len);
+	frames = sg_nents_for_dma(sgl, sg_len, bcm2835_dma_max_frame_length(c));
 
 	/* allocate the CB chain */
 	d = bcm2835_dma_create_cb_chain(chan, direction, false,
Signature · 1 line
2.50.1
#10 lore raw

PATCH v2 6/13 dmaengine: k3dma: use sg_nents_for_dma() helper

In reply to Andy Shevchenko

Instead of open coded variant let's use recently introduced helper.

---
 drivers/dma/k3dma.c | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)
drivers/dma/k3dma.c +2 -7
index acc2983e28e0..88f9a2952edc 100644
@@ -536,19 +536,14 @@ static struct dma_async_tx_descriptor *k3_dma_prep_slave_sg(
 	size_t len, avail, total = 0;
 	struct scatterlist *sg;
 	dma_addr_t addr, src = 0, dst = 0;
-	int num = sglen, i;
+	int num, i;
 
 	if (sgl == NULL)
 		return NULL;
 
 	c->cyclic = 0;
 
-	for_each_sg(sgl, sg, sglen, i) {
-		avail = sg_dma_len(sg);
-		if (avail > DMA_MAX_SIZE)
-			num += DIV_ROUND_UP(avail, DMA_MAX_SIZE) - 1;
-	}
-
+	num = sg_nents_for_dma(sgl, sglen, DMA_MAX_SIZE);
 	ds = k3_dma_alloc_desc_resource(num, chan);
 	if (!ds)
 		return NULL;
Signature · 1 line
2.50.1
#11 lore raw

PATCH v2 7/13 dmaengine: lgm: use sg_nents_for_dma() helper

In reply to Andy Shevchenko

Instead of open coded variant let's use recently introduced helper.

---
 drivers/dma/lgm/lgm-dma.c | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)
drivers/dma/lgm/lgm-dma.c +2 -7
index 8173c3f1075a..7d4d3dde6d88 100644
@@ -1164,8 +1164,8 @@ ldma_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
 	struct dw2_desc *hw_ds;
 	struct dw2_desc_sw *ds;
 	struct scatterlist *sg;
-	int num = sglen, i;
 	dma_addr_t addr;
+	int num, i;
 
 	if (!sgl)
 		return NULL;
@@ -1173,12 +1173,7 @@ ldma_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
 	if (d->ver > DMA_VER22)
 		return ldma_chan_desc_cfg(chan, sgl->dma_address, sglen);
 
-	for_each_sg(sgl, sg, sglen, i) {
-		avail = sg_dma_len(sg);
-		if (avail > DMA_MAX_SIZE)
-			num += DIV_ROUND_UP(avail, DMA_MAX_SIZE) - 1;
-	}
-
+	num = sg_nents_for_dma(sgl, sg_len, DMA_MAX_SIZE);
 	ds = dma_alloc_desc_resource(num, c);
 	if (!ds)
 		return NULL;
Signature · 1 line
2.50.1
#14 lore raw

PATCH v2 8/13 dmaengine: pxa-dma: use sg_nents_for_dma() helper

In reply to Andy Shevchenko

Instead of open coded variant let's use recently introduced helper.

---
 drivers/dma/pxa_dma.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/dma/pxa_dma.c +2 -3
index 249296389771..b639c8b51e87 100644
@@ -970,7 +970,7 @@ pxad_prep_slave_sg(struct dma_chan *dchan, struct scatterlist *sgl,
 	struct scatterlist *sg;
 	dma_addr_t dma;
 	u32 dcmd, dsadr = 0, dtadr = 0;
-	unsigned int nb_desc = 0, i, j = 0;
+	unsigned int nb_desc, i, j = 0;
 
 	if ((sgl == NULL) || (sg_len == 0))
 		return NULL;
@@ -979,8 +979,7 @@ pxad_prep_slave_sg(struct dma_chan *dchan, struct scatterlist *sgl,
 	dev_dbg(&chan->vc.chan.dev->device,
 		"%s(): dir=%d flags=%lx\n", __func__, dir, flags);
 
-	for_each_sg(sgl, sg, sg_len, i)
-		nb_desc += DIV_ROUND_UP(sg_dma_len(sg), PDMA_MAX_DESC_BYTES);
+	nb_desc = sg_nents_for_dma(sgl, sg_len, PDMA_MAX_DESC_BYTES);
 	sw_desc = pxad_alloc_desc(chan, nb_desc + 1);
 	if (!sw_desc)
 		return NULL;
Signature · 1 line
2.50.1
#9 lore raw

PATCH v2 5/13 dmaengine: dw-axi-dmac: use sg_nents_for_dma() helper

In reply to Andy Shevchenko

Instead of open coded variant let's use recently introduced helper.

---
 drivers/dma/dw-axi-dmac/dw-axi-dmac-platform.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
drivers/dma/dw-axi-dmac/dw-axi-dmac-platform.c +2 -4
index b23536645ff7..493c2a32b0fe 100644
@@ -850,7 +850,7 @@ dw_axi_dma_chan_prep_slave_sg(struct dma_chan *dchan, struct scatterlist *sgl,
 	unsigned int loop = 0;
 	struct scatterlist *sg;
 	size_t axi_block_len;
-	u32 len, num_sgs = 0;
+	u32 len, num_sgs;
 	unsigned int i;
 	dma_addr_t mem;
 	int status;
@@ -867,9 +867,7 @@ dw_axi_dma_chan_prep_slave_sg(struct dma_chan *dchan, struct scatterlist *sgl,
 	if (axi_block_len == 0)
 		return NULL;
 
-	for_each_sg(sgl, sg, sg_len, i)
-		num_sgs += DIV_ROUND_UP(sg_dma_len(sg), axi_block_len);
-
+	num_sgs = sg_nents_for_dma(sgl, sg_len, axi_block_len);
 	desc = axi_desc_alloc(num_sgs);
 	if (unlikely(!desc))
 		goto err_desc_get;
Signature · 1 line
2.50.1
#15 lore raw

PATCH v2 9/13 dmaengine: qcom: adm: use sg_nents_for_dma() helper

In reply to Andy Shevchenko

Instead of open coded variant let's use recently introduced helper.

---
 drivers/dma/qcom/qcom_adm.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
drivers/dma/qcom/qcom_adm.c +4 -5
index 6be54fddcee1..490edad20ae6 100644
@@ -390,16 +390,15 @@ static struct dma_async_tx_descriptor *adm_prep_slave_sg(struct dma_chan *chan,
 	}
 
 	/* iterate through sgs and compute allocation size of structures */
-	for_each_sg(sgl, sg, sg_len, i) {
-		if (achan->slave.device_fc) {
+	if (achan->slave.device_fc) {
+		for_each_sg(sgl, sg, sg_len, i) {
 			box_count += DIV_ROUND_UP(sg_dma_len(sg) / burst,
 						  ADM_MAX_ROWS);
 			if (sg_dma_len(sg) % burst)
 				single_count++;
-		} else {
-			single_count += DIV_ROUND_UP(sg_dma_len(sg),
-						     ADM_MAX_XFER);
 		}
+	} else {
+		single_count = sg_nents_for_dma(sgl, sg_len, ADM_MAX_XFER);
 	}
 
 	async_desc = kzalloc(sizeof(*async_desc), GFP_NOWAIT);
Signature · 1 line
2.50.1
#19 lore raw

PATCH v2 11/13 dmaengine: sa11x0: use sg_nents_for_dma() helper

In reply to Andy Shevchenko

Instead of open coded variant let's use recently introduced helper.

---
 drivers/dma/sa11x0-dma.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
drivers/dma/sa11x0-dma.c +2 -4
index dc1a9a05252e..3dd33d3f7987 100644
@@ -526,7 +526,7 @@ static struct dma_async_tx_descriptor *sa11x0_dma_prep_slave_sg(
 	struct sa11x0_dma_chan *c = to_sa11x0_dma_chan(chan);
 	struct sa11x0_dma_desc *txd;
 	struct scatterlist *sgent;
-	unsigned i, j = sglen;
+	unsigned int i, j;
 	size_t size = 0;
 
 	/* SA11x0 channels can only operate in their native direction */
@@ -542,10 +542,7 @@ static struct dma_async_tx_descriptor *sa11x0_dma_prep_slave_sg(
 
 	for_each_sg(sg, sgent, sglen, i) {
 		dma_addr_t addr = sg_dma_address(sgent);
-		unsigned int len = sg_dma_len(sgent);
 
-		if (len > DMA_MAX_SIZE)
-			j += DIV_ROUND_UP(len, DMA_MAX_SIZE & ~DMA_ALIGN) - 1;
 		if (addr & DMA_ALIGN) {
 			dev_dbg(chan->device->dev, "vchan %p: bad buffer alignment: %pad\n",
 				&c->vc, &addr);
@@ -553,6 +550,7 @@ static struct dma_async_tx_descriptor *sa11x0_dma_prep_slave_sg(
 		}
 	}
 
+	j = sg_nents_for_dma(sgl, sg_len, DMA_MAX_SIZE & ~DMA_ALIGN);
 	txd = kzalloc(struct_size(txd, sg, j), GFP_ATOMIC);
 	if (!txd) {
 		dev_dbg(chan->device->dev, "vchan %p: kzalloc failed\n", &c->vc);
Signature · 1 line
2.50.1
#20 lore raw

PATCH v2 12/13 dmaengine: sh: use sg_nents_for_dma() helper

In reply to Andy Shevchenko

Instead of open coded variant let's use recently introduced helper.

---
 drivers/dma/sh/shdma-base.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/dma/sh/shdma-base.c +2 -3
index 834741adadaa..1e1b65e46668 100644
@@ -577,12 +577,11 @@ static struct dma_async_tx_descriptor *shdma_prep_sg(struct shdma_chan *schan,
 	struct scatterlist *sg;
 	struct shdma_desc *first = NULL, *new = NULL /* compiler... */;
 	LIST_HEAD(tx_list);
-	int chunks = 0;
+	int chunks;
 	unsigned long irq_flags;
 	int i;
 
-	for_each_sg(sgl, sg, sg_len, i)
-		chunks += DIV_ROUND_UP(sg_dma_len(sg), schan->max_xfer_len);
+	chunks = sg_nents_for_dma(sgl, sg_len, schan->max_xfer_len);
 
 	/* Have to lock the whole loop to protect against concurrent release */
 	spin_lock_irqsave(&schan->chan_lock, irq_flags);
Signature · 1 line
2.50.1
#17 lore raw

PATCH v2 10/13 dmaengine: qcom: bam_dma: use sg_nents_for_dma() helper

In reply to Andy Shevchenko

Instead of open coded variant let's use recently introduced helper.

---
 drivers/dma/qcom/bam_dma.c | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)
drivers/dma/qcom/bam_dma.c +2 -7
index 2cf060174795..62b3921f0d11 100644
@@ -655,22 +655,17 @@ static struct dma_async_tx_descriptor *bam_prep_slave_sg(struct dma_chan *chan,
 	struct scatterlist *sg;
 	u32 i;
 	struct bam_desc_hw *desc;
-	unsigned int num_alloc = 0;
-
+	unsigned int num_alloc;
 
 	if (!is_slave_direction(direction)) {
 		dev_err(bdev->dev, "invalid dma direction\n");
 		return NULL;
 	}
 
-	/* calculate number of required entries */
-	for_each_sg(sgl, sg, sg_len, i)
-		num_alloc += DIV_ROUND_UP(sg_dma_len(sg), BAM_FIFO_SIZE);
-
 	/* allocate enough room to accommodate the number of entries */
+	num_alloc = sg_nents_for_dma(sgl, sg_len, BAM_FIFO_SIZE);
 	async_desc = kzalloc(struct_size(async_desc, desc, num_alloc),
 			     GFP_NOWAIT);
-
 	if (!async_desc)
 		return NULL;
 
Signature · 1 line
2.50.1
#21 lore raw

PATCH v2 13/13 dmaengine: xilinx: xdma: use sg_nents_for_dma() helper

In reply to Andy Shevchenko

Instead of open coded variant let's use recently introduced helper.

---
 drivers/dma/xilinx/xdma.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
drivers/dma/xilinx/xdma.c +2 -4
index 5ecf8223c112..118199a04902 100644
@@ -605,13 +605,11 @@ xdma_prep_device_sg(struct dma_chan *chan, struct scatterlist *sgl,
 	struct xdma_chan *xdma_chan = to_xdma_chan(chan);
 	struct dma_async_tx_descriptor *tx_desc;
 	struct xdma_desc *sw_desc;
-	u32 desc_num = 0, i;
 	u64 addr, dev_addr, *src, *dst;
+	u32 desc_num, i;
 	struct scatterlist *sg;
 
-	for_each_sg(sgl, sg, sg_len, i)
-		desc_num += DIV_ROUND_UP(sg_dma_len(sg), XDMA_DESC_BLEN_MAX);
-
+	desc_num = sg_nents_for_dma(sgl, sg_len, XDMA_DESC_BLEN_MAX);
 	sw_desc = xdma_alloc_desc(xdma_chan, desc_num, false);
 	if (!sw_desc)
 		return NULL;
Signature · 1 line
2.50.1
Reviewed-by on v2 1/13
#2 lore raw

Re: [PATCH v2 01/13] scatterlist: introduce sg_nents_for_dma() helper

In reply to Andy Shevchenko
Quoted 45 lines · Andy Shevchenko

Sometimes the user needs to split each entry on the mapped scatter list due to DMA length constrains. This helper returns a number of entities assuming that each of them is not bigger than supplied maximum length.

---
 include/linux/scatterlist.h |  2 ++
 lib/scatterlist.c           | 25 +++++++++++++++++++++++++
 2 files changed, 27 insertions(+)
include/linux/scatterlist.h +2 -0
index 29f6ceb98d74..6de1a2434299 100644
@@ -441,6 +441,8 @@ static inline void sg_init_marker(struct scatterlist *sgl,
 
 int sg_nents(struct scatterlist *sg);
 int sg_nents_for_len(struct scatterlist *sg, u64 len);
+int sg_nents_for_dma(struct scatterlist *sgl, unsigned int sglen, size_t len);
+
 struct scatterlist *sg_last(struct scatterlist *s, unsigned int);
 void sg_init_table(struct scatterlist *, unsigned int);
 void sg_init_one(struct scatterlist *, const void *, unsigned int);
lib/scatterlist.c +14 -0
index 4af1c8b0775a..4d1a010f863c 100644
@@ -64,6 +64,31 @@ int sg_nents_for_len(struct scatterlist *sg, u64 len)
 }
 EXPORT_SYMBOL(sg_nents_for_len);
 
+/**
+ * sg_nents_for_dma - return the count of DMA-capable entries in scatterlist
+ * @sgl:	The scatterlist
+ * @sglen:	The current number of entries
+ * @len:	The maximum length of DMA-capable block
+ *
+ * Description:
+ * Determines the number of entries in @sgl which would be permitted in
+ * DMA-capable transfer if list had been split accordingly, taking into
+ * account chaining as well.
+ *
+ * Returns:
+ *   the number of sgl entries needed
+ *

lib/scatterlist.c int sg_nents_for_len(struct scatterlist *sg, u64 len) · line 90

Quoted 10 lines
lib/scatterlist.c +10 -0
+ **/
+int sg_nents_for_dma(struct scatterlist *sgl, unsigned int sglen, size_t len)
+{
+	struct scatterlist *sg;
+	int i, nents = 0;
+
+	for_each_sg(sgl, sg, sglen, i)
+		nents += DIV_ROUND_UP(sg_dma_len(sg), len);
+	return nents;
+}

We need an EXPORT_SYMBOL() here.

With that, this looks good to me.

Regards,
Bjorn

Quoted 8 lines
lib/scatterlist.c +1 -0
+
 /**
  * sg_last - return the last scatterlist entry in a list
  * @sgl:	First entry in the scatterlist
Signature · 1 line
2.50.1
Reviewed-by on v2 9/13
#16 lore raw

Re: [PATCH v2 09/13] dmaengine: qcom: adm: use sg_nents_for_dma() helper

In reply to Andy Shevchenko
Quoted 2 lines · Andy Shevchenko

Instead of open coded variant let's use recently introduced helper.

Quoted 34 lines
---
 drivers/dma/qcom/qcom_adm.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
drivers/dma/qcom/qcom_adm.c +4 -5
index 6be54fddcee1..490edad20ae6 100644
@@ -390,16 +390,15 @@ static struct dma_async_tx_descriptor *adm_prep_slave_sg(struct dma_chan *chan,
 	}
 
 	/* iterate through sgs and compute allocation size of structures */
-	for_each_sg(sgl, sg, sg_len, i) {
-		if (achan->slave.device_fc) {
+	if (achan->slave.device_fc) {
+		for_each_sg(sgl, sg, sg_len, i) {
 			box_count += DIV_ROUND_UP(sg_dma_len(sg) / burst,
 						  ADM_MAX_ROWS);
 			if (sg_dma_len(sg) % burst)
 				single_count++;
-		} else {
-			single_count += DIV_ROUND_UP(sg_dma_len(sg),
-						     ADM_MAX_XFER);
 		}
+	} else {
+		single_count = sg_nents_for_dma(sgl, sg_len, ADM_MAX_XFER);
 	}
 
 	async_desc = kzalloc(sizeof(*async_desc), GFP_NOWAIT);
Signature · 1 line
2.50.1
Reviewed-by on v2 10/13
#18 lore raw

Re: [PATCH v2 10/13] dmaengine: qcom: bam_dma: use sg_nents_for_dma() helper

In reply to Andy Shevchenko
Quoted 2 lines · Andy Shevchenko

Instead of open coded variant let's use recently introduced helper.

Regards,
Bjorn

Quoted 38 lines
---
 drivers/dma/qcom/bam_dma.c | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)
drivers/dma/qcom/bam_dma.c +2 -7
index 2cf060174795..62b3921f0d11 100644
@@ -655,22 +655,17 @@ static struct dma_async_tx_descriptor *bam_prep_slave_sg(struct dma_chan *chan,
 	struct scatterlist *sg;
 	u32 i;
 	struct bam_desc_hw *desc;
-	unsigned int num_alloc = 0;
-
+	unsigned int num_alloc;
 
 	if (!is_slave_direction(direction)) {
 		dev_err(bdev->dev, "invalid dma direction\n");
 		return NULL;
 	}
 
-	/* calculate number of required entries */
-	for_each_sg(sgl, sg, sg_len, i)
-		num_alloc += DIV_ROUND_UP(sg_dma_len(sg), BAM_FIFO_SIZE);
-
 	/* allocate enough room to accommodate the number of entries */
+	num_alloc = sg_nents_for_dma(sgl, sg_len, BAM_FIFO_SIZE);
 	async_desc = kzalloc(struct_size(async_desc, desc, num_alloc),
 			     GFP_NOWAIT);
-
 	if (!async_desc)
 		return NULL;
 
Signature · 1 line
2.50.1
#3 lore raw

Re: [PATCH v2 01/13] scatterlist: introduce sg_nents_for_dma() helper

In reply to Bjorn Andersson
Quoted 32 lines · Bjorn Andersson
Quoted 31 lines · Andy Shevchenko

Sometimes the user needs to split each entry on the mapped scatter list due to DMA length constrains. This helper returns a number of entities assuming that each of them is not bigger than supplied maximum length.

---
 include/linux/scatterlist.h |  2 ++
 lib/scatterlist.c           | 25 +++++++++++++++++++++++++
 2 files changed, 27 insertions(+)
include/linux/scatterlist.h +2 -0
index 29f6ceb98d74..6de1a2434299 100644
@@ -441,6 +441,8 @@ static inline void sg_init_marker(struct scatterlist *sgl,
 
 int sg_nents(struct scatterlist *sg);
 int sg_nents_for_len(struct scatterlist *sg, u64 len);
+int sg_nents_for_dma(struct scatterlist *sgl, unsigned int sglen, size_t len);
+
 struct scatterlist *sg_last(struct scatterlist *s, unsigned int);
 void sg_init_table(struct scatterlist *, unsigned int);
 void sg_init_one(struct scatterlist *, const void *, unsigned int);
lib/scatterlist.c +0 -0
index 4af1c8b0775a..4d1a010f863c 100644
@@ -64,6 +64,31 @@ int sg_nents_for_len(struct scatterlist *sg, u64 len)
 }
 EXPORT_SYMBOL(sg_nents_for_len);
 
Quoted 16 lines
Quoted 6 lines
lib/scatterlist.c +6 -0
+/**
+ * sg_nents_for_dma - return the count of DMA-capable entries in scatterlist
+ * @sgl:	The scatterlist
+ * @sglen:	The current number of entries
+ * @len:	The maximum length of DMA-capable block
+ *

lib/scatterlist.c int sg_nents_for_len(struct scatterlist *sg, u64 len) · line 82

Quoted 10 lines
lib/scatterlist.c +10 -0
+ * Description:
+ * Determines the number of entries in @sgl which would be permitted in
+ * DMA-capable transfer if list had been split accordingly, taking into
+ * account chaining as well.
+ *
+ * Returns:
+ *   the number of sgl entries needed
+ *
+ **/
+int sg_nents_for_dma(struct scatterlist *sgl, unsigned int sglen, size_t len)

All but two clients store the value in an unsigned int. Changing the return type to unsigned int also signals that the function is just returning a count (no errors).

Regards,
Bjorn

Quoted 27 lines

lib/scatterlist.c int sg_nents_for_len(struct scatterlist *sg, u64 len) · line 90

Quoted 8 lines
lib/scatterlist.c +8 -0
+{
+	struct scatterlist *sg;
+	int i, nents = 0;
+
+	for_each_sg(sgl, sg, sglen, i)
+		nents += DIV_ROUND_UP(sg_dma_len(sg), len);
+	return nents;
+}

We need an EXPORT_SYMBOL() here.

With that, this looks good to me.

Regards,
Bjorn

Quoted 8 lines
lib/scatterlist.c +1 -0
+
 /**
  * sg_last - return the last scatterlist entry in a list
  * @sgl:	First entry in the scatterlist
Signature · 1 line
2.50.1
Reviewed-by on the v2 series
#22 lore raw
In reply to Andy Shevchenko
Quoted 10 lines · Andy Shevchenko

A handful of the DMAengine drivers use same routine to calculate the number of SG entries needed for the given DMA transfer. Provide a common helper for them and convert.

I left the new helper on SG level of API because brief grepping shows potential candidates outside of DMA engine, e.g.:

drivers/crypto/chelsio/chcr_algo.c:154: nents += DIV_ROUND_UP(less, entlen); drivers/spi/spi-stm32.c:1495: /* Count the number of entries needed */

Comment in patch 1, but for the rest.

Regards,
Bjorn

Quoted 42 lines

Changelog v2:
- dropped outdated patches (only 9 years passed :-)
- rebased on top of the current kernel
- left API SG wide It might

v1: https://patchwork.kernel.org/project/linux-dmaengine/patch/[email protected]/

Andy Shevchenko (13):
scatterlist: introduce sg_nents_for_dma() helper
dmaengine: altera-msgdma: use sg_nents_for_dma() helper
dmaengine: axi-dmac: use sg_nents_for_dma() helper
dmaengine: bcm2835-dma: use sg_nents_for_dma() helper
dmaengine: dw-axi-dmac: use sg_nents_for_dma() helper
dmaengine: k3dma: use sg_nents_for_dma() helper
dmaengine: lgm: use sg_nents_for_dma() helper
dmaengine: pxa-dma: use sg_nents_for_dma() helper
dmaengine: qcom: adm: use sg_nents_for_dma() helper
dmaengine: qcom: bam_dma: use sg_nents_for_dma() helper
dmaengine: sa11x0: use sg_nents_for_dma() helper
dmaengine: sh: use sg_nents_for_dma() helper
dmaengine: xilinx: xdma: use sg_nents_for_dma() helper

 drivers/dma/altera-msgdma.c                   |  5 ++--
 drivers/dma/bcm2835-dma.c                     | 19 +-------------
 drivers/dma/dma-axi-dmac.c                    |  5 +---
 .../dma/dw-axi-dmac/dw-axi-dmac-platform.c    |  6 ++---
 drivers/dma/k3dma.c                           |  9 ++-----
 drivers/dma/lgm/lgm-dma.c                     |  9 ++-----
 drivers/dma/pxa_dma.c                         |  5 ++--
 drivers/dma/qcom/bam_dma.c                    |  9 ++-----
 drivers/dma/qcom/qcom_adm.c                   |  9 +++----
 drivers/dma/sa11x0-dma.c                      |  6 ++---
 drivers/dma/sh/shdma-base.c                   |  5 ++--
 drivers/dma/xilinx/xdma.c                     |  6 ++---
 include/linux/scatterlist.h                   |  2 ++
 lib/scatterlist.c                             | 25 +++++++++++++++++++
 14 files changed, 51 insertions(+), 69 deletions(-)
Signature · 1 line
2.50.1
#4 lore raw

Re: [PATCH v2 01/13] scatterlist: introduce sg_nents_for_dma() helper

In reply to Bjorn Andersson
Quoted 2 lines · Bjorn Andersson
Quoted 1 line · Bjorn Andersson

On Mon, Nov 10, 2025 at 11:23:28AM +0100, Andy Shevchenko wrote:

...

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

int sg_nents(struct scatterlist *sg);
int sg_nents_for_len(struct scatterlist *sg, u64 len);
+int sg_nents_for_dma(struct scatterlist *sgl, unsigned int sglen, size_t len);

^^^

Quoted 5 lines
Quoted 1 line
Quoted 1 line

+int sg_nents_for_dma(struct scatterlist *sgl, unsigned int sglen, size_t len)

All but two clients store the value in an unsigned int. Changing the return type to unsigned int also signals that the function is just returning a count (no errors).

The type is chosen for the consistency with the existing APIs.
So, I prefer consistency in this case, if we need to change type, we need to do that for all above APIs I believe. And this is out of the scope here.

Personally I was also puzzled of the choice as *nents members are all unsigned int in the scatterlist.h.

...

Quoted 1 line
Quoted 1 line

We need an EXPORT_SYMBOL() here.

Good catch! I'll add it in next version.

Quoted 3 lines
Quoted 3 lines

With that, this looks good to me.

Thanks!

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#5 lore raw

Re: [PATCH v2 01/13] scatterlist: introduce sg_nents_for_dma() helper

Quoted 15 lines · Andy Shevchenko
Quoted 2 lines · Bjorn Andersson
Quoted 1 line · Bjorn Andersson

On Mon, Nov 10, 2025 at 11:23:28AM +0100, Andy Shevchenko wrote:

...

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

int sg_nents(struct scatterlist *sg);
int sg_nents_for_len(struct scatterlist *sg, u64 len);
+int sg_nents_for_dma(struct scatterlist *sgl, unsigned int sglen, size_t len);

^^^

Quoted 2 lines
Quoted 1 line
Quoted 1 line

+int sg_nents_for_dma(struct scatterlist *sgl, unsigned int sglen, size_t len)

Quoted 8 lines
Quoted 3 lines

All but two clients store the value in an unsigned int. Changing the return type to unsigned int also signals that the function is just returning a count (no errors).

The type is chosen for the consistency with the existing APIs.
So, I prefer consistency in this case, if we need to change type, we need to do that for all above APIs I believe. And this is out of the scope here.

That makes sense, after a second look I agree with your decision.

Regards,
Bjorn

Quoted 20 lines

Personally I was also puzzled of the choice as *nents members are all unsigned int in the scatterlist.h.

...

Quoted 1 line
Quoted 1 line

We need an EXPORT_SYMBOL() here.

Good catch! I'll add it in next version.

Quoted 3 lines
Quoted 3 lines

With that, this looks good to me.

Thanks!

Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer
#12 lore raw

Re: [PATCH v2 07/13] dmaengine: lgm: use sg_nents_for_dma() helper

In reply to Andy Shevchenko
Quoted 22 lines · Andy Shevchenko

Instead of open coded variant let's use recently introduced helper.

---
 drivers/dma/lgm/lgm-dma.c | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)
drivers/dma/lgm/lgm-dma.c +1 -1
index 8173c3f1075a..7d4d3dde6d88 100644
@@ -1164,8 +1164,8 @@ ldma_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
 	struct dw2_desc *hw_ds;
 	struct dw2_desc_sw *ds;
 	struct scatterlist *sg;
-	int num = sglen, i;
 	dma_addr_t addr;
+	int num, i;
 
 	if (!sgl)
 		return NULL;

drivers/dma/lgm/lgm-dma.c ldma_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl, · line 1176

Quoted 10 lines
drivers/dma/lgm/lgm-dma.c +1 -6
@@ -1173,12 +1173,7 @@ ldma_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
 	if (d->ver > DMA_VER22)
 		return ldma_chan_desc_cfg(chan, sgl->dma_address, sglen);
 
-	for_each_sg(sgl, sg, sglen, i) {
-		avail = sg_dma_len(sg);
-		if (avail > DMA_MAX_SIZE)
-			num += DIV_ROUND_UP(avail, DMA_MAX_SIZE) - 1;
-	}
-
+	num = sg_nents_for_dma(sgl, sg_len, DMA_MAX_SIZE);

drivers/dma/lgm/lgm-dma.c:1176:37: error: ‘sg_len’ undeclared (first use in this function); did you mean ‘sglen’?

         num = sg_nents_for_dma(sgl, sg_len, DMA_MAX_SIZE);

I am getting the build failure, as well as few unused warning with this, pls fix

Signature · 1 line
~Vinod
#13 lore raw

Re: [PATCH v2 07/13] dmaengine: lgm: use sg_nents_for_dma() helper

Quoted 2 lines · Vinod Koul
Quoted 1 line · Andy Shevchenko

Instead of open coded variant let's use recently introduced helper.

...

Quoted 7 lines

drivers/dma/lgm/lgm-dma.c ldma_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl, · line 1176

Quoted 1 line
drivers/dma/lgm/lgm-dma.c +1 -0
+	num = sg_nents_for_dma(sgl, sg_len, DMA_MAX_SIZE);

drivers/dma/lgm/lgm-dma.c:1176:37: error: ‘sg_len’ undeclared (first use in this function); did you mean ‘sglen’?

         num = sg_nents_for_dma(sgl, sg_len, DMA_MAX_SIZE);

I am getting the build failure, as well as few unused warning with this, pls fix

Indeed, sorry for that, in v4 should be fixed.
Thanks!

Signature · 2 lines
With Best Regards,
Andy Shevchenko