Linux Hardening
 help / color / mirror / Atom feed
From: Qinxin Xia <xiaqinxin@huawei.com>
To: Rosen Penev <rosenp@gmail.com>, <iommu@lists.linux.dev>
Cc: Barry Song <baohua@kernel.org>, <linusw@kernel.org>,
	Marek Szyprowski <m.szyprowski@samsung.com>,
	Robin Murphy <robin.murphy@arm.com>, Kees Cook <kees@kernel.org>,
	"Gustavo A. R. Silva" <gustavoars@kernel.org>,
	open list <linux-kernel@vger.kernel.org>,
	"open list:KERNEL HARDENING (not covered by other
	areas):Keyword:b__counted_by(_le|_be|_ptr)?b"
	<linux-hardening@vger.kernel.org>
Subject: Re: [PATCH] dma: map_benchmark: turn dma_sg_map_param buf into a flexible array
Date: Tue, 2 Jun 2026 23:50:20 +0800	[thread overview]
Message-ID: <d78c453a-1edf-42b2-9ac8-cab6cc0305c5@huawei.com> (raw)
In-Reply-To: <20260525220628.94833-1-rosenp@gmail.com>



在 2026/5/26 6:06, 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
> Signed-off-by: Rosen Penev <rosenp@gmail.com>
> ---
>   kernel/dma/map_benchmark.c | 29 +++++++++++++----------------
>   1 file changed, 13 insertions(+), 16 deletions(-)
> 
> diff --git a/kernel/dma/map_benchmark.c b/kernel/dma/map_benchmark.c
> index 29eeb5fdf199..a65da5c7710c 100644
> --- a/kernel/dma/map_benchmark.c
> +++ b/kernel/dma/map_benchmark.c
> @@ -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);
>   }
>   

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:
Reviewed-by: Qinxin Xia <xiaqinxin@huawei.com>

-- 
Thanks,
Qinxin


  reply	other threads:[~2026-06-02 15:50 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-25 22:06 [PATCH] dma: map_benchmark: turn dma_sg_map_param buf into a flexible array Rosen Penev
2026-06-02 15:50 ` Qinxin Xia [this message]
2026-06-02 16:52 ` Kees Cook
2026-06-03  3:18   ` Rosen Penev

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=d78c453a-1edf-42b2-9ac8-cab6cc0305c5@huawei.com \
    --to=xiaqinxin@huawei.com \
    --cc=baohua@kernel.org \
    --cc=gustavoars@kernel.org \
    --cc=iommu@lists.linux.dev \
    --cc=kees@kernel.org \
    --cc=linusw@kernel.org \
    --cc=linux-hardening@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=m.szyprowski@samsung.com \
    --cc=robin.murphy@arm.com \
    --cc=rosenp@gmail.com \
    /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