[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [PATCH 05/18] vfio/common: Add VFIOBitmap and (de)alloc functions
From: |
Alex Williamson |
Subject: |
Re: [PATCH 05/18] vfio/common: Add VFIOBitmap and (de)alloc functions |
Date: |
Fri, 27 Jan 2023 14:11:01 -0700 |
On Thu, 26 Jan 2023 20:49:35 +0200
Avihai Horon <avihaih@nvidia.com> wrote:
> There are already two places where dirty page bitmap allocation and
> calculations are done in open code. With device dirty page tracking
> being added in next patches, there are going to be even more places.
>
> To avoid code duplication, introduce VFIOBitmap struct and corresponding
> alloc and dealloc functions and use them where applicable.
>
> Signed-off-by: Avihai Horon <avihaih@nvidia.com>
> ---
> hw/vfio/common.c | 89 ++++++++++++++++++++++++++++++++----------------
> 1 file changed, 60 insertions(+), 29 deletions(-)
>
> diff --git a/hw/vfio/common.c b/hw/vfio/common.c
> index 8e8ffbc046..e554573eb5 100644
> --- a/hw/vfio/common.c
> +++ b/hw/vfio/common.c
> @@ -319,6 +319,41 @@ const MemoryRegionOps vfio_region_ops = {
> * Device state interfaces
> */
>
> +typedef struct {
> + unsigned long *bitmap;
> + hwaddr size;
> + hwaddr pages;
> +} VFIOBitmap;
> +
> +static VFIOBitmap *vfio_bitmap_alloc(hwaddr size)
> +{
> + VFIOBitmap *vbmap = g_try_new0(VFIOBitmap, 1);
> + if (!vbmap) {
> + errno = ENOMEM;
> +
> + return NULL;
> + }
> +
> + vbmap->pages = REAL_HOST_PAGE_ALIGN(size) / qemu_real_host_page_size();
> + vbmap->size = ROUND_UP(vbmap->pages, sizeof(__u64) * BITS_PER_BYTE) /
> + BITS_PER_BYTE;
> + vbmap->bitmap = g_try_malloc0(vbmap->size);
> + if (!vbmap->bitmap) {
> + g_free(vbmap);
> + errno = ENOMEM;
> +
> + return NULL;
> + }
> +
> + return vbmap;
> +}
> +
> +static void vfio_bitmap_dealloc(VFIOBitmap *vbmap)
> +{
> + g_free(vbmap->bitmap);
> + g_free(vbmap);
> +}
> +
> bool vfio_mig_active(void)
> {
> VFIOGroup *group;
> @@ -421,9 +456,14 @@ static int vfio_dma_unmap_bitmap(VFIOContainer
> *container,
> {
> struct vfio_iommu_type1_dma_unmap *unmap;
> struct vfio_bitmap *bitmap;
> - uint64_t pages = REAL_HOST_PAGE_ALIGN(size) / qemu_real_host_page_size();
> + VFIOBitmap *vbmap;
> int ret;
>
> + vbmap = vfio_bitmap_alloc(size);
> + if (!vbmap) {
> + return -errno;
> + }
> +
> unmap = g_malloc0(sizeof(*unmap) + sizeof(*bitmap));
>
> unmap->argsz = sizeof(*unmap) + sizeof(*bitmap);
> @@ -437,35 +477,28 @@ static int vfio_dma_unmap_bitmap(VFIOContainer
> *container,
> * qemu_real_host_page_size to mark those dirty. Hence set bitmap_pgsize
> * to qemu_real_host_page_size.
> */
> -
> bitmap->pgsize = qemu_real_host_page_size();
> - bitmap->size = ROUND_UP(pages, sizeof(__u64) * BITS_PER_BYTE) /
> - BITS_PER_BYTE;
> + bitmap->size = vbmap->size;
> + bitmap->data = (__u64 *)vbmap->bitmap;
>
> - if (bitmap->size > container->max_dirty_bitmap_size) {
> - error_report("UNMAP: Size of bitmap too big 0x%"PRIx64,
> - (uint64_t)bitmap->size);
> + if (vbmap->size > container->max_dirty_bitmap_size) {
> + error_report("UNMAP: Size of bitmap too big 0x%"PRIx64, vbmap->size);
Why not pass the container to the alloc function so we can test this
consistently for each bitmap we allocate? Thanks,
Alex
- [PATCH 00/18] vfio: Add migration pre-copy support and device dirty tracking, Avihai Horon, 2023/01/26
- [PATCH 02/18] vfio/common: Fix error reporting in vfio_get_dirty_bitmap(), Avihai Horon, 2023/01/26
- [PATCH 01/18] vfio/migration: Add VFIO migration pre-copy support, Avihai Horon, 2023/01/26
- [PATCH 03/18] vfio/common: Fix wrong %m usages, Avihai Horon, 2023/01/26
- [PATCH 04/18] vfio/common: Abort migration if dirty log start/stop/sync fails, Avihai Horon, 2023/01/26
- [PATCH 07/18] util: Extend iova_tree_foreach() to take data argument, Avihai Horon, 2023/01/26
- [PATCH 05/18] vfio/common: Add VFIOBitmap and (de)alloc functions, Avihai Horon, 2023/01/26
- Re: [PATCH 05/18] vfio/common: Add VFIOBitmap and (de)alloc functions,
Alex Williamson <=
- [PATCH 08/18] vfio/common: Record DMA mapped IOVA ranges, Avihai Horon, 2023/01/26
- [PATCH 09/18] vfio/common: Add device dirty page tracking start/stop, Avihai Horon, 2023/01/26
- [PATCH 06/18] util: Add iova_tree_nnodes(), Avihai Horon, 2023/01/26
- [PATCH 17/18] vfio/migration: Query device dirty page tracking support, Avihai Horon, 2023/01/26
- [PATCH 11/18] vfio/common: Add device dirty page bitmap sync, Avihai Horon, 2023/01/26
- [PATCH 13/18] memory/iommu: Add IOMMU_ATTR_MAX_IOVA attribute, Avihai Horon, 2023/01/26
- [PATCH 16/18] vfio/common: Optimize device dirty page tracking with vIOMMU, Avihai Horon, 2023/01/26
- [PATCH 12/18] vfio/common: Extract vIOMMU code from vfio_sync_dirty_bitmap(), Avihai Horon, 2023/01/26