iommufd: Fix offset handling in folio subroutines
PATCH 1/1 iommufd: Fix offset handling in folio subroutines
The @offset from memfd_pin_folios() is pgoff_t and is bytes, not a number of pages as batch_from_folios() treats it.
Convert byte offset to pfn offset and use that.
---
Found this when tried mapping a bunch of folios (>64) with an offset and the first folio was used for all the mappings in the batch.
--- drivers/iommu/iommufd/pages.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/iommu/iommufd/pages.c +3 -2
@@ -646,8 +646,9 @@ static int batch_from_folios(struct pfn_batch *batch, struct folio ***folios_p,while (npages) {struct folio *folio = *folios; -unsigned long nr =folio_nr_pages (folio) - offset; -unsigned long pfn =page_to_pfn (folio_page (folio, offset)); +unsigned long offset_nr = offset >>PAGE_SHIFT ; +unsigned long nr =folio_nr_pages (folio) - offset_nr; +unsigned long pfn =page_to_pfn (folio_page (folio, offset_nr)); nr =min (nr, npages); npages -= nr;
Signature · 1 line
2.47.1
Tian, Kevin
maintainerQuoted 25 lines
From: Alexey Kardashevskiy <[email protected]>
Sent: Wednesday, April 2, 2025 9:44 AMThe @offset from
memfd_pin_folios()ispgoff_tand is bytes, not a number of pages asbatch_from_folios()treats it.Convert byte offset to pfn offset and use that.
Fixes ed9178fbfd4e ("iommufd: Folio subroutines")Signed-off-by Alexey Kardashevskiy <[email protected]>---
Found this when tried mapping a bunch of folios (>64) with an offset and the first folio was used for all the mappings in the batch.
--- drivers/iommu/iommufd/pages.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)drivers/iommu/iommufd/pages.c +0
-0
drivers/iommu/iommufd/pages.c static int batch_from_folios(struct pfn_batch *batch, · line 652
Quoted 9 lines
drivers/iommu/iommufd/pages.c +3
-2@@ -646,8 +646,9 @@ static int batch_from_folios(struct pfn_batch *batch,struct folio ***folios_p, while (npages) { struct folio *folio = *folios; -unsigned long nr =folio_nr_pages (folio) - offset; -unsigned long pfn =page_to_pfn (folio_page (folio, offset)); +unsigned long offset_nr = offset >>PAGE_SHIFT ; +unsigned long nr =folio_nr_pages (folio) - offset_nr; +unsigned long pfn =page_to_pfn (folio_page (folio, offset_nr));
pin_memfd_pages():
nfolios = memfd_pin_folios(user->file, start, end, user->ufolios, nfolios, &offset); offset >>= PAGE_SHIFT; user->ufolios_next = user->ufolios; user->ufolios_offset = offset;
pfn_reader_fill_span():
if (!user->file) batch_from_pages(&pfns->batch, user->upages + start_index, npages); else rc = batch_from_folios(&pfns->batch, &user->ufolios_next, &user->ufolios_offset, npages);
So the offset is already about a page number as batch_from_folios() expects.
Quoted 5 lines
drivers/iommu/iommufd/pages.c +0
-1nr =min (nr, npages); npages -= nr; --2.47.1
Quoted 36 lines · Kevin
Quoted 25 lines
From: Alexey Kardashevskiy <[email protected]>
Sent: Wednesday, April 2, 2025 9:44 AMThe @offset from
memfd_pin_folios()ispgoff_tand is bytes, not a number of pages asbatch_from_folios()treats it.Convert byte offset to pfn offset and use that.
Fixes ed9178fbfd4e ("iommufd: Folio subroutines")Signed-off-by Alexey Kardashevskiy <[email protected]>---
Found this when tried mapping a bunch of folios (>64) with an offset and the first folio was used for all the mappings in the batch.
--- drivers/iommu/iommufd/pages.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)drivers/iommu/iommufd/pages.c +0
-0drivers/iommu/iommufd/pages.c static int batch_from_folios(struct pfn_batch *batch, · line 652
Quoted 9 lines
drivers/iommu/iommufd/pages.c +3
-2@@ -646,8 +646,9 @@ static int batch_from_folios(struct pfn_batch *batch,struct folio ***folios_p, while (npages) { struct folio *folio = *folios; -unsigned long nr =folio_nr_pages (folio) - offset; -unsigned long pfn =page_to_pfn (folio_page (folio, offset)); +unsigned long offset_nr = offset >>PAGE_SHIFT ; +unsigned long nr =folio_nr_pages (folio) - offset_nr; +unsigned long pfn =page_to_pfn (folio_page (folio, offset_nr));
pin_memfd_pages():
Quoted 3 lines
nfolios = memfd_pin_folios(user->file, start, end, user->ufolios, nfolios, &offset); offset >>= PAGE_SHIFT;
Aaaaahhh missed that so there is a bug in my variant of memfd_pin_folios() for gmemfd and sorry for the noise. Thanks,
Quoted 19 lines
user->ufolios_next = user->ufolios; user->ufolios_offset = offset;
pfn_reader_fill_span():if (!user->file) batch_from_pages(&pfns->batch, user->upages + start_index, npages); else rc = batch_from_folios(&pfns->batch, &user->ufolios_next, &user->ufolios_offset, npages);So the offset is already about a page number as
batch_from_folios()expects.Quoted 5 lines
drivers/iommu/iommufd/pages.c +0
-1nr =min (nr, npages); npages -= nr; --2.47.1
Signature · 1 line
Alexey