The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [PATCH] vdpa/mlx5: Use kvzalloc_flex() for MTT command memory
@ 2026-05-08  5:18 Rosen Penev
  2026-05-08 15:13 ` Dragos Tatulea
  0 siblings, 1 reply; 2+ messages in thread
From: Rosen Penev @ 2026-05-08  5:18 UTC (permalink / raw)
  To: virtualization
  Cc: Dragos Tatulea, Michael S. Tsirkin, Jason Wang, Xuan Zhuo,
	Eugenio Pérez, open list

The create mkey command memory embeds the MTT array as a flexible array
member. Use kvzalloc_flex() to allocate it directly instead of open-coding
the struct_size() calculation with kvcalloc().

The MTT allocation still needs to be aligned to MLX5_VDPA_MTT_ALIGN bytes.
Since each MTT entry is __be64, align the entry count directly and avoid
carrying a separate byte length variable.

Assisted-by: Codex:GPT-5.5
Signed-off-by: Rosen Penev <rosenp@gmail.com>
---
 drivers/vdpa/mlx5/core/mr.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/drivers/vdpa/mlx5/core/mr.c b/drivers/vdpa/mlx5/core/mr.c
index 42c2705077a6..6d02ccf9eb91 100644
--- a/drivers/vdpa/mlx5/core/mr.c
+++ b/drivers/vdpa/mlx5/core/mr.c
@@ -221,11 +221,10 @@ static int create_direct_keys(struct mlx5_vdpa_dev *mvdev, struct mlx5_vdpa_mr *
 
 	list_for_each_entry(dmr, &mr->head, list) {
 		struct mlx5_create_mkey_mem *cmd_mem;
-		int mttlen, mttcount;
+		int mttcount;
 
-		mttlen = roundup(MLX5_ST_SZ_BYTES(mtt) * dmr->nsg, MLX5_VDPA_MTT_ALIGN);
-		mttcount = mttlen / sizeof(cmd_mem->mtt[0]);
-		cmd_mem = kvcalloc(1, struct_size(cmd_mem, mtt, mttcount), GFP_KERNEL);
+		mttcount = ALIGN(dmr->nsg, MLX5_VDPA_MTT_ALIGN / sizeof(cmd_mem->mtt[0]));
+		cmd_mem = kvzalloc_flex(*cmd_mem, mtt, mttcount);
 		if (!cmd_mem) {
 			err = -ENOMEM;
 			goto done;
-- 
2.54.0


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] vdpa/mlx5: Use kvzalloc_flex() for MTT command memory
  2026-05-08  5:18 [PATCH] vdpa/mlx5: Use kvzalloc_flex() for MTT command memory Rosen Penev
@ 2026-05-08 15:13 ` Dragos Tatulea
  0 siblings, 0 replies; 2+ messages in thread
From: Dragos Tatulea @ 2026-05-08 15:13 UTC (permalink / raw)
  To: Rosen Penev, virtualization
  Cc: Michael S. Tsirkin, Jason Wang, Xuan Zhuo, Eugenio Pérez,
	open list

On Thu, May 07, 2026 at 10:18:37PM -0700, Rosen Penev wrote:
> The create mkey command memory embeds the MTT array as a flexible array
> member. Use kvzalloc_flex() to allocate it directly instead of open-coding
> the struct_size() calculation with kvcalloc().
> 
> The MTT allocation still needs to be aligned to MLX5_VDPA_MTT_ALIGN bytes.
> Since each MTT entry is __be64, align the entry count directly and avoid
> carrying a separate byte length variable.
> 
> Assisted-by: Codex:GPT-5.5
> Signed-off-by: Rosen Penev <rosenp@gmail.com>
> ---
>  drivers/vdpa/mlx5/core/mr.c | 7 +++----
>  1 file changed, 3 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/vdpa/mlx5/core/mr.c b/drivers/vdpa/mlx5/core/mr.c
> index 42c2705077a6..6d02ccf9eb91 100644
> --- a/drivers/vdpa/mlx5/core/mr.c
> +++ b/drivers/vdpa/mlx5/core/mr.c
> @@ -221,11 +221,10 @@ static int create_direct_keys(struct mlx5_vdpa_dev *mvdev, struct mlx5_vdpa_mr *
>  
>  	list_for_each_entry(dmr, &mr->head, list) {
>  		struct mlx5_create_mkey_mem *cmd_mem;
> -		int mttlen, mttcount;
> +		int mttcount;
>  
> -		mttlen = roundup(MLX5_ST_SZ_BYTES(mtt) * dmr->nsg, MLX5_VDPA_MTT_ALIGN);
> -		mttcount = mttlen / sizeof(cmd_mem->mtt[0]);
> -		cmd_mem = kvcalloc(1, struct_size(cmd_mem, mtt, mttcount), GFP_KERNEL);
> +		mttcount = ALIGN(dmr->nsg, MLX5_VDPA_MTT_ALIGN / sizeof(cmd_mem->mtt[0]));
> +		cmd_mem = kvzalloc_flex(*cmd_mem, mtt, mttcount);
>  		if (!cmd_mem) {
>  			err = -ENOMEM;
>  			goto done;
> -- 
> 2.54.0
> 
Thanks for the patch. Code and commit message looks good.
Not sure about the Assisted-by tag, but that is not my call.

Reviewed-by: Dragos Tatulea <dtatulea@nvidia.com>

Thanks,
Dragos

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-05-08 15:13 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-05-08  5:18 [PATCH] vdpa/mlx5: Use kvzalloc_flex() for MTT command memory Rosen Penev
2026-05-08 15:13 ` Dragos Tatulea

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox