Skip to contentSkip to conversation

dma: map_benchmark: turn dma_sg_map_param buf into a flexible array

PATCH Superseded Rosen Penev 3 participants 4 messages May 25 – Jun 3

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 dma: map_benchmark: turn dma_sg_map_param buf into a flexible array

The buf pointer was kmalloc_array()'d immediately after the parent struct allocation, with the count (granule, validated to 1..1024 by the ioctl) trivially available beforehand. Move buf to the struct tail as a flexible array member and fold the two allocations into a single kzalloc_flex(), dropping the kfree(params->buf) in both the prepare error path and unprepare.

Add __counted_by for extra runtime analysis.

Assisted-by Claude:Opus-4.7
---
 kernel/dma/map_benchmark.c | 29 +++++++++++++----------------
 1 file changed, 13 insertions(+), 16 deletions(-)
kernel/dma/map_benchmark.c +13 -16
index 29eeb5fdf199..a65da5c7710c 100644
@@ -121,35 +121,35 @@ static struct map_benchmark_ops dma_single_map_benchmark_ops = {
 struct dma_sg_map_param {
 	struct sg_table sgt;
 	struct device *dev;
-	void **buf;
 	u32 npages;
 	u32 dma_dir;
+	void *buf[] __counted_by(npages);
 };
 
 static void *dma_sg_map_benchmark_prepare(struct map_benchmark_data *map)
 {
+	struct dma_sg_map_param *params;
 	struct scatterlist *sg;
+	u32 npages;
 	int i;
 
-	struct dma_sg_map_param *params = kzalloc(sizeof(*params), GFP_KERNEL);
-
-	if (!params)
-		return NULL;
 	/*
 	 * Set the number of scatterlist entries based on the granule.
 	 * In SG mode, 'granule' represents the number of scatterlist entries.
 	 * Each scatterlist entry corresponds to a single page.
 	 */
-	params->npages = map->bparam.granule;
+	npages = map->bparam.granule;
+
+	params = kzalloc_flex(*params, buf, npages);
+	if (!params)
+		return NULL;
+
+	params->npages = npages;
 	params->dma_dir = map->bparam.dma_dir;
 	params->dev = map->dev;
-	params->buf = kmalloc_array(params->npages, sizeof(*params->buf),
-				    GFP_KERNEL);
-	if (!params->buf)
-		goto out;
 
-	if (sg_alloc_table(&params->sgt, params->npages, GFP_KERNEL))
-		goto free_buf;
+	if (sg_alloc_table(&params->sgt, npages, GFP_KERNEL))
+		goto free_params;
 
 	for_each_sgtable_sg(&params->sgt, sg, i) {
 		params->buf[i] = (void *)__get_free_page(GFP_KERNEL);
@@ -166,9 +166,7 @@ static void *dma_sg_map_benchmark_prepare(struct map_benchmark_data *map)
 		free_page((unsigned long)params->buf[i]);
 
 	sg_free_table(&params->sgt);
-free_buf:
-	kfree(params->buf);
-out:
+free_params:
 	kfree(params);
 	return NULL;
 }
@@ -183,7 +181,6 @@ static void dma_sg_map_benchmark_unprepare(void *mparam)
 
 	sg_free_table(&params->sgt);
 
-	kfree(params->buf);
 	kfree(params);
 }
 
Signature · 1 line
2.54.0
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 79 lines · Rosen Penev

The buf pointer was kmalloc_array()'d immediately after the parent struct allocation, with the count (granule, validated to 1..1024 by the ioctl) trivially available beforehand. Move buf to the struct tail as a flexible array member and fold the two allocations into a single kzalloc_flex(), dropping the kfree(params->buf) in both the prepare error path and unprepare.

Add __counted_by for extra runtime analysis.

Assisted-by Claude:Opus-4.7
---
  kernel/dma/map_benchmark.c | 29 +++++++++++++----------------
  1 file changed, 13 insertions(+), 16 deletions(-)
kernel/dma/map_benchmark.c +13 -15
index 29eeb5fdf199..a65da5c7710c 100644
@@ -121,35 +121,35 @@ static struct map_benchmark_ops dma_single_map_benchmark_ops = {
  struct dma_sg_map_param {
  	struct sg_table sgt;
  	struct device *dev;
-	void **buf;
  	u32 npages;
  	u32 dma_dir;
+	void *buf[] __counted_by(npages);
  };
 
  static void *dma_sg_map_benchmark_prepare(struct map_benchmark_data *map)
  {
+	struct dma_sg_map_param *params;
  	struct scatterlist *sg;
+	u32 npages;
  	int i;
 
-	struct dma_sg_map_param *params = kzalloc(sizeof(*params), GFP_KERNEL);
-
-	if (!params)
-		return NULL;
  	/*
  	 * Set the number of scatterlist entries based on the granule.
  	 * In SG mode, 'granule' represents the number of scatterlist entries.
  	 * Each scatterlist entry corresponds to a single page.
  	 */
-	params->npages = map->bparam.granule;
+	npages = map->bparam.granule;
+
+	params = kzalloc_flex(*params, buf, npages);
+	if (!params)
+		return NULL;
+
+	params->npages = npages;
  	params->dma_dir = map->bparam.dma_dir;
  	params->dev = map->dev;
-	params->buf = kmalloc_array(params->npages, sizeof(*params->buf),
-				    GFP_KERNEL);
-	if (!params->buf)
-		goto out;
 
-	if (sg_alloc_table(&params->sgt, params->npages, GFP_KERNEL))
-		goto free_buf;
+	if (sg_alloc_table(&params->sgt, npages, GFP_KERNEL))
+		goto free_params;
 
  	for_each_sgtable_sg(&params->sgt, sg, i) {
  		params->buf[i] = (void *)__get_free_page(GFP_KERNEL);
@@ -166,9 +166,7 @@ static void *dma_sg_map_benchmark_prepare(struct map_benchmark_data *map)
  		free_page((unsigned long)params->buf[i]);
 
  	sg_free_table(&params->sgt);
-free_buf:
-	kfree(params->buf);
-out:
+free_params:
  	kfree(params);
  	return NULL;
  }

kernel/dma/map_benchmark.c static void dma_sg_map_benchmark_unprepare(void *mparam) · line 186

Quoted 7 lines
kernel/dma/map_benchmark.c +0 -1
@@ -183,7 +181,6 @@ static void dma_sg_map_benchmark_unprepare(void *mparam)
 
  	sg_free_table(&params->sgt);
 
-	kfree(params->buf);
  	kfree(params);
  }
 

Thanks for the patch.

Sorry for the delay — your email was unfortunately flagged as phishing and almost missed. I suspect the issue was the Cc list containing descriptive text.

It's a nice clean code.
I've tested it on Kunpeng 920, and it looks good.
Feel free to add my Reviewed-by:

Signature · 2 lines
Thanks,
Qinxin
#2 lore raw
In reply to Rosen Penev
Quoted 53 lines · Rosen Penev

The buf pointer was kmalloc_array()'d immediately after the parent struct allocation, with the count (granule, validated to 1..1024 by the ioctl) trivially available beforehand. Move buf to the struct tail as a flexible array member and fold the two allocations into a single kzalloc_flex(), dropping the kfree(params->buf) in both the prepare error path and unprepare.

Add __counted_by for extra runtime analysis.

Assisted-by Claude:Opus-4.7
---
 kernel/dma/map_benchmark.c | 29 +++++++++++++----------------
 1 file changed, 13 insertions(+), 16 deletions(-)
kernel/dma/map_benchmark.c +10 -6
index 29eeb5fdf199..a65da5c7710c 100644
@@ -121,35 +121,35 @@ static struct map_benchmark_ops dma_single_map_benchmark_ops = {
 struct dma_sg_map_param {
 	struct sg_table sgt;
 	struct device *dev;
-	void **buf;
 	u32 npages;
 	u32 dma_dir;
+	void *buf[] __counted_by(npages);
 };
 
 static void *dma_sg_map_benchmark_prepare(struct map_benchmark_data *map)
 {
+	struct dma_sg_map_param *params;
 	struct scatterlist *sg;
+	u32 npages;
 	int i;
 
-	struct dma_sg_map_param *params = kzalloc(sizeof(*params), GFP_KERNEL);
-
-	if (!params)
-		return NULL;
 	/*
 	 * Set the number of scatterlist entries based on the granule.
 	 * In SG mode, 'granule' represents the number of scatterlist entries.
 	 * Each scatterlist entry corresponds to a single page.
 	 */
-	params->npages = map->bparam.granule;
+	npages = map->bparam.granule;
+
+	params = kzalloc_flex(*params, buf, npages);
+	if (!params)
+		return NULL;
+
+	params->npages = npages;

kernel/dma/map_benchmark.c static struct map_benchmark_ops dma_single_map_benchmark_ops = { · line 151

Quoted 10 lines
kernel/dma/map_benchmark.c +1 -6
 	params->dma_dir = map->bparam.dma_dir;
 	params->dev = map->dev;
-	params->buf = kmalloc_array(params->npages, sizeof(*params->buf),
-				    GFP_KERNEL);
-	if (!params->buf)
-		goto out;
 
-	if (sg_alloc_table(&params->sgt, params->npages, GFP_KERNEL))
-		goto free_buf;
+	if (sg_alloc_table(&params->sgt, npages, GFP_KERNEL))

nit: I think it's better to use the params->npages here just because it is obviously tied to params->sgt, and reduces code churn for this patch (i.e. that line would be left alone).

Otherwise looks good.

-Kees

Quoted 26 lines
kernel/dma/map_benchmark.c +2 -4
+		goto free_params;
 
 	for_each_sgtable_sg(&params->sgt, sg, i) {
 		params->buf[i] = (void *)__get_free_page(GFP_KERNEL);
@@ -166,9 +166,7 @@ static void *dma_sg_map_benchmark_prepare(struct map_benchmark_data *map)
 		free_page((unsigned long)params->buf[i]);
 
 	sg_free_table(&params->sgt);
-free_buf:
-	kfree(params->buf);
-out:
+free_params:
 	kfree(params);
 	return NULL;
 }
@@ -183,7 +181,6 @@ static void dma_sg_map_benchmark_unprepare(void *mparam)
 
 	sg_free_table(&params->sgt);
 
-	kfree(params->buf);
 	kfree(params);
 }
 
Signature · 1 line
2.54.0
Signature · 1 line
Kees Cook
#3 lore raw
Quoted 62 lines · Kees Cook
Quoted 60 lines · Rosen Penev

The buf pointer was kmalloc_array()'d immediately after the parent struct allocation, with the count (granule, validated to 1..1024 by the ioctl) trivially available beforehand. Move buf to the struct tail as a flexible array member and fold the two allocations into a single kzalloc_flex(), dropping the kfree(params->buf) in both the prepare error path and unprepare.

Add __counted_by for extra runtime analysis.

Assisted-by Claude:Opus-4.7
---
 kernel/dma/map_benchmark.c | 29 +++++++++++++----------------
 1 file changed, 13 insertions(+), 16 deletions(-)
kernel/dma/map_benchmark.c +10 -10
index 29eeb5fdf199..a65da5c7710c 100644
@@ -121,35 +121,35 @@ static struct map_benchmark_ops dma_single_map_benchmark_ops = {
 struct dma_sg_map_param {
      struct sg_table sgt;
      struct device *dev;
-     void **buf;
      u32 npages;
      u32 dma_dir;
+     void *buf[] __counted_by(npages);
 };
 
 static void *dma_sg_map_benchmark_prepare(struct map_benchmark_data *map)
 {
+     struct dma_sg_map_param *params;
      struct scatterlist *sg;
+     u32 npages;
      int i;
 
-     struct dma_sg_map_param *params = kzalloc(sizeof(*params), GFP_KERNEL);
-
-     if (!params)
-             return NULL;
      /*
       * Set the number of scatterlist entries based on the granule.
       * In SG mode, 'granule' represents the number of scatterlist entries.
       * Each scatterlist entry corresponds to a single page.
       */
-     params->npages = map->bparam.granule;
+     npages = map->bparam.granule;
+
+     params = kzalloc_flex(*params, buf, npages);
+     if (!params)
+             return NULL;
+
+     params->npages = npages;
      params->dma_dir = map->bparam.dma_dir;
      params->dev = map->dev;
-     params->buf = kmalloc_array(params->npages, sizeof(*params->buf),
-                                 GFP_KERNEL);
-     if (!params->buf)
-             goto out;
 
Quoted 7 lines

kernel/dma/map_benchmark.c static struct map_benchmark_ops dma_single_map_benchmark_ops = { · line 151

Quoted 3 lines
kernel/dma/map_benchmark.c +1 -2
-     if (sg_alloc_table(&params->sgt, params->npages, GFP_KERNEL))
-             goto free_buf;
+     if (sg_alloc_table(&params->sgt, npages, GFP_KERNEL))

nit: I think it's better to use the params->npages here just because it is obviously tied to params->sgt, and reduces code churn for this patch (i.e. that line would be left alone).

Fixed. I only did it to reduce line length.

Quoted 34 lines

Otherwise looks good.

-Kees

Quoted 24 lines
kernel/dma/map_benchmark.c +2 -5
+             goto free_params;
 
      for_each_sgtable_sg(&params->sgt, sg, i) {
              params->buf[i] = (void *)__get_free_page(GFP_KERNEL);
@@ -166,9 +166,7 @@ static void *dma_sg_map_benchmark_prepare(struct map_benchmark_data *map)
              free_page((unsigned long)params->buf[i]);
 
      sg_free_table(&params->sgt);
-free_buf:
-     kfree(params->buf);
-out:
+free_params:
      kfree(params);
      return NULL;
 }
@@ -183,7 +181,6 @@ static void dma_sg_map_benchmark_unprepare(void *mparam)
 
      sg_free_table(&params->sgt);
 
-     kfree(params->buf);
      kfree(params);
 }
 
--
Quoted 2 lines

2.54.0

--
Kees Cook