Skip to content

Commit

Permalink
vfio/iova_bitmap: Fix PAGE_SIZE unaligned bitmaps
Browse files Browse the repository at this point in the history
iova_bitmap_set() doesn't consider the end of the page boundary when the
first bitmap page offset isn't zero, and wrongly changes the consecutive
page right after. Consequently this leads to missing dirty pages from
reported by the device as seen from the VMM.

The current logic iterates over a given number of base pages and clamps it
to the remaining indexes to iterate in the last page.  Instead of having to
consider extra pages to pin (e.g. first and extra pages), just handle the
first page as its own range and let the rest of the bitmap be handled as if
it was base page aligned.

This is done by changing iova_bitmap_mapped_remaining() to return PAGE_SIZE
- pgoff (on the first bitmap page), and leads to pgoff being set to 0 on
following iterations.

Fixes: 58ccf01 ("vfio: Add an IOVA bitmap support")
Reported-by: Avihai Horon <avihaih@nvidia.com>
Tested-by: Avihai Horon <avihaih@nvidia.com>
Signed-off-by: Joao Martins <joao.m.martins@oracle.com>
Link: https://lore.kernel.org/r/20221025193114.58695-3-joao.m.martins@oracle.com
Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
  • Loading branch information
Joao Martins authored and Alex Williamson committed Nov 9, 2022
1 parent ea00d4e commit f38044e
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions drivers/vfio/iova_bitmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -296,11 +296,15 @@ void iova_bitmap_free(struct iova_bitmap *bitmap)
*/
static unsigned long iova_bitmap_mapped_remaining(struct iova_bitmap *bitmap)
{
unsigned long remaining;
unsigned long remaining, bytes;

/* Cap to one page in the first iteration, if PAGE_SIZE unaligned. */
bytes = !bitmap->mapped.pgoff ? bitmap->mapped.npages << PAGE_SHIFT :
PAGE_SIZE - bitmap->mapped.pgoff;

remaining = bitmap->mapped_total_index - bitmap->mapped_base_index;
remaining = min_t(unsigned long, remaining,
(bitmap->mapped.npages << PAGE_SHIFT) / sizeof(*bitmap->bitmap));
bytes / sizeof(*bitmap->bitmap));

return remaining;
}
Expand Down

0 comments on commit f38044e

Please sign in to comment.