From: Rosen Penev <rosenp@gmail.com>
To: kvm@vger.kernel.org
Cc: Alex Williamson <alex@shazbot.org>, Kees Cook <kees@kernel.org>,
"Gustavo A. R. Silva" <gustavoars@kernel.org>,
linux-kernel@vger.kernel.org (open list),
linux-hardening@vger.kernel.org (open list:KERNEL HARDENING (not
covered by other areas):Keyword:\b__counted_by(_le|_be)?\b)
Subject: [PATCH] vfio: pci: use kzalloc_flex
Date: Wed, 25 Mar 2026 19:37:47 -0700 [thread overview]
Message-ID: <20260326023747.54485-1-rosenp@gmail.com> (raw)
Simplify allocation by using a flexible array member and kzalloc_flex.
Less memory management needed.
Use __counted_by for extra runtime analysis. Move assignment to after
allocation as required by __counted_by.
Signed-off-by: Rosen Penev <rosenp@gmail.com>
---
drivers/vfio/pci/vfio_pci_dmabuf.c | 18 +++++-------------
1 file changed, 5 insertions(+), 13 deletions(-)
diff --git a/drivers/vfio/pci/vfio_pci_dmabuf.c b/drivers/vfio/pci/vfio_pci_dmabuf.c
index 3a803923141b..40e7e035a720 100644
--- a/drivers/vfio/pci/vfio_pci_dmabuf.c
+++ b/drivers/vfio/pci/vfio_pci_dmabuf.c
@@ -14,12 +14,12 @@ struct vfio_pci_dma_buf {
struct vfio_pci_core_device *vdev;
struct list_head dmabufs_elm;
size_t size;
- struct phys_vec *phys_vec;
struct p2pdma_provider *provider;
u32 nr_ranges;
struct kref kref;
struct completion comp;
u8 revoked : 1;
+ struct phys_vec phys_vec[] __counted_by(nr_ranges);
};
static int vfio_pci_dma_buf_attach(struct dma_buf *dmabuf,
@@ -95,7 +95,6 @@ static void vfio_pci_dma_buf_release(struct dma_buf *dmabuf)
up_write(&priv->vdev->memory_lock);
vfio_device_put_registration(&priv->vdev->vdev);
}
- kfree(priv->phys_vec);
kfree(priv);
}
@@ -258,33 +257,28 @@ int vfio_pci_core_feature_dma_buf(struct vfio_pci_core_device *vdev, u32 flags,
if (ret)
goto err_free_ranges;
- priv = kzalloc_obj(*priv);
+ priv = kzalloc_flex(*priv, phys_vec, get_dma_buf.nr_ranges);
if (!priv) {
ret = -ENOMEM;
goto err_free_ranges;
}
- priv->phys_vec = kzalloc_objs(*priv->phys_vec, get_dma_buf.nr_ranges);
- if (!priv->phys_vec) {
- ret = -ENOMEM;
- goto err_free_priv;
- }
- priv->vdev = vdev;
priv->nr_ranges = get_dma_buf.nr_ranges;
+ priv->vdev = vdev;
priv->size = length;
ret = vdev->pci_ops->get_dmabuf_phys(vdev, &priv->provider,
get_dma_buf.region_index,
priv->phys_vec, dma_ranges,
priv->nr_ranges);
if (ret)
- goto err_free_phys;
+ goto err_free_priv;
kfree(dma_ranges);
dma_ranges = NULL;
if (!vfio_device_try_get_registration(&vdev->vdev)) {
ret = -ENODEV;
- goto err_free_phys;
+ goto err_free_priv;
}
exp_info.ops = &vfio_pci_dmabuf_ops;
@@ -323,8 +317,6 @@ int vfio_pci_core_feature_dma_buf(struct vfio_pci_core_device *vdev, u32 flags,
dma_buf_put(priv->dmabuf);
err_dev_put:
vfio_device_put_registration(&vdev->vdev);
-err_free_phys:
- kfree(priv->phys_vec);
err_free_priv:
kfree(priv);
err_free_ranges:
--
2.53.0
next reply other threads:[~2026-03-26 2:38 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-03-26 2:37 Rosen Penev [this message]
2026-03-30 22:19 ` [PATCH] vfio: pci: use kzalloc_flex Alex Williamson
2026-03-30 23:24 ` Rosen Penev
2026-03-30 23:46 ` Gustavo A. R. Silva
2026-03-31 0:18 ` Rosen Penev
2026-03-31 0:51 ` Gustavo A. R. Silva
2026-03-31 0:23 ` Alex Williamson
2026-03-31 5:11 ` Leon Romanovsky
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=20260326023747.54485-1-rosenp@gmail.com \
--to=rosenp@gmail.com \
--cc=alex@shazbot.org \
--cc=gustavoars@kernel.org \
--cc=kees@kernel.org \
--cc=kvm@vger.kernel.org \
--cc=linux-hardening@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
/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