xen/virtio: Handle cases when page offset > PAGE_SIZE properly
PATCH 1/1 xen/virtio: Handle cases when page offset > PAGE_SIZE properly
From: Oleksandr Tyshchenko <[email protected]>
Passed to xen_grant_dma_map_page() offset in the page can be > PAGE_SIZE even if the guest uses the same page granularity as Xen (4KB).
Before current patch, if such case happened we ended up providing grants for the whole region in xen_grant_dma_map_page() which was really unnecessary. The more, we ended up not releasing all grants which represented that region in xen_grant_dma_unmap_page().
Current patch updates the code to be able to deal with such cases.
---
Cc: Juergen Gross <[email protected]>
Cc: Xenia Ragiadakou <[email protected]>
Depens on:
https://lore.kernel.org/xen-devel/[email protected]/
Should go in only after that series.
--- drivers/xen/grant-dma-ops.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/xen/grant-dma-ops.c +5 -3
@@ -168,7 +168,9 @@ static dma_addr_t xen_grant_dma_map_page(struct device *dev, struct page *page, unsigned long attrs) {struct xen_grant_dma_data *data; -unsigned int i, n_pages =PFN_UP (offset + size); +unsigned long dma_offset =offset_in_page (offset), + gfn_offset =PFN_DOWN (offset); +unsigned int i, n_pages =PFN_UP (dma_offset + size);grant_ref_t grant;dma_addr_t dma_handle;
@@ -187,10 +189,10 @@ static dma_addr_t xen_grant_dma_map_page(struct device *dev, struct page *page,for (i =0 ; i < n_pages; i++) {gnttab_grant_foreign_access_ref (grant + i, data->backend_domid, -xen_page_to_gfn (page) + i, dir ==DMA_TO_DEVICE ); +xen_page_to_gfn (page) + i + gfn_offset, dir ==DMA_TO_DEVICE ); } - dma_handle =grant_to_dma (grant) + offset; + dma_handle =grant_to_dma (grant) + dma_offset;return dma_handle; }
Signature · 1 line
2.25.1
Quoted 6 lines · Oleksandr Tyshchenko
From: Oleksandr Tyshchenko <[email protected]>
Passed to
xen_grant_dma_map_page()offset in the page can be >PAGE_SIZEeven if the guest uses the same page granularity as Xen (4KB).
Quoted 8 lines
Before current patch, if such case happened we ended up providing grants for the whole region in
xen_grant_dma_map_page()which was really unnecessary. The more, we ended up not releasing all grants which represented that region inxen_grant_dma_unmap_page().Current patch updates the code to be able to deal with such cases.
Signed-off-by Oleksandr Tyshchenko <[email protected]>
Juergen
On 10/7/22 16:27, Oleksandr Tyshchenko wrote:
Hi Oleksandr
Quoted 31 lines
From: Oleksandr Tyshchenko <[email protected]>
Passed to
xen_grant_dma_map_page()offset in the page can be >PAGE_SIZEeven if the guest uses the same page granularity as Xen (4KB).Before current patch, if such case happened we ended up providing grants for the whole region in
xen_grant_dma_map_page()which was really unnecessary. The more, we ended up not releasing all grants which represented that region inxen_grant_dma_unmap_page().Current patch updates the code to be able to deal with such cases.
Signed-off-by Oleksandr Tyshchenko <[email protected]>---
Cc: Juergen Gross <[email protected]>
Cc: Xenia Ragiadakou <[email protected]>Depens on:
https://lore.kernel.org/xen-devel/[email protected]/Should go in only after that series.
--- drivers/xen/grant-dma-ops.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)drivers/xen/grant-dma-ops.c +0
-0
drivers/xen/grant-dma-ops.c static dma_addr_t xen_grant_dma_map_page(struct device *dev, struct page *page, · line 173
Quoted 7 lines
drivers/xen/grant-dma-ops.c +3
-1@@ -168,7 +168,9 @@ static dma_addr_t xen_grant_dma_map_page(struct device *dev, struct page *page, unsigned long attrs) {struct xen_grant_dma_data *data; -unsigned int i, n_pages =PFN_UP (offset + size); +unsigned long dma_offset =offset_in_page (offset), + gfn_offset =PFN_DOWN (offset); +unsigned int i, n_pages =PFN_UP (dma_offset + size);
IIUC, the above with a later patch will become:
dma_offset = xen_offset_in_page(offset)gfn_offset = XEN_PFN_DOWN(offset)n_pages = XEN_PFN_UP(dma_offset + size)
drivers/xen/grant-dma-ops.c static dma_addr_t xen_grant_dma_map_page(struct device *dev, struct page *page, · line 192
Quoted 9 lines
drivers/xen/grant-dma-ops.c +1
-1grant_ref_t grant;dma_addr_t dma_handle;@@ -187,10 +189,10 @@ static dma_addr_t xen_grant_dma_map_page(struct device *dev, struct page *page,for (i =0 ; i < n_pages; i++) {gnttab_grant_foreign_access_ref (grant + i, data->backend_domid, -xen_page_to_gfn (page) + i, dir ==DMA_TO_DEVICE ); +xen_page_to_gfn (page) + i + gfn_offset, dir ==DMA_TO_DEVICE );
Here, why the pfn is not calculated before passing it to pfn_to_gfn()? I mean sth like pfn_to_gfn(page_to_xen_pfn(page) + gfn_offset + i)
Quoted 7 lines
drivers/xen/grant-dma-ops.c +1
-1} - dma_handle =grant_to_dma (grant) + offset; + dma_handle =grant_to_dma (grant) + dma_offset;return dma_handle; }
Signature · 1 line
Xenia
Oleksandr Tyshchenko
reviewerOn 08.10.22 14:08, Xenia Ragiadakou wrote:
Hello Xenia
Quoted 48 lines · Oleksandr Tyshchenko
Hi Oleksandr
Quoted 32 lines
From: Oleksandr Tyshchenko <[email protected]>
Passed to
xen_grant_dma_map_page()offset in the page can be >PAGE_SIZEeven if the guest uses the same page granularity as Xen (4KB).Before current patch, if such case happened we ended up providing grants for the whole region in
xen_grant_dma_map_page()which was really unnecessary. The more, we ended up not releasing all grants which represented that region inxen_grant_dma_unmap_page().Current patch updates the code to be able to deal with such cases.
Signed-off-by Oleksandr Tyshchenko <[email protected]>---
Cc: Juergen Gross <[email protected]>
Cc: Xenia Ragiadakou <[email protected]>Depens on:
https://urldefense.com/v3/__https://lore.kernel.org/xen-devel/[email protected]/__;!!GF_29dbcQIUBPA!xnkNaKpfZ4LssQJcJs_J91KERZKMP2Rd-xEdBqXNXJ8GyCXJ0gkRer1elVYfxOWtwN_FOl9tVieDWlfN-UZaHQsyLMhA$
[lore[.]kernel[.]org]Should go in only after that series.
---
drivers/xen/grant-dma-ops.c| 8 +++++---1 file changed, 5 insertions(+), 3 deletions(-)drivers/xen/grant-dma-ops.c +0
-0drivers/xen/grant-dma-ops.c static dma_addr_t xen_grant_dma_map_page(struct · line 174
Quoted 8 lines
drivers/xen/grant-dma-ops.c +3
-1@@ -168,7 +168,9 @@ static dma_addr_t xen_grant_dma_map_page(struct device *dev, struct page *page, unsigned long attrs) { struct xen_grant_dma_data *data; - unsigned int i, n_pages = PFN_UP(offset + size); + unsigned long dma_offset = offset_in_page(offset), + gfn_offset = PFN_DOWN(offset); + unsigned int i, n_pages = PFN_UP(dma_offset + size);IIUC, the above with a later patch will become:
Quoted 3 lines
dma_offset=xen_offset_in_page(offset)gfn_offset=XEN_PFN_DOWN(offset)n_pages=XEN_PFN_UP(dma_offset + size)
If saying "later" patch you meant "xen/virtio: Convert PAGE_SIZE/PAGE_SHIFT/PFN_UP to Xen counterparts" then yes, exactly.
Quoted 13 lines
Quoted 4 lines
drivers/xen/grant-dma-ops.c +0
-0grant_ref_t grant; dma_addr_t dma_handle;Quoted 8 lines
@@ -187,10 +189,10 @@ static
dma_addr_txen_grant_dma_map_page(struct device *dev, struct page *page, for (i = 0; i <n_pages; i++) { gnttab_grant_foreign_access_ref(grant + i,data->backend_domid,
-xen_page_to_gfn(page)+ i, dir ==DMA_TO_DEVICE); +xen_page_to_gfn(page)+ i +gfn_offset, dir ==DMA_TO_DEVICE);
Quoted 2 lines
Here, why the pfn is not calculated before passing it to
pfn_to_gfn()? I mean sth likepfn_to_gfn(page_to_xen_pfn(page) + gfn_offset + i)
The gfn_offset is just a const value here, which just means how many gfns we should skip. But ...
... I think, I get your point. So, if the region which is contiguous in pfn might be non-contiguous in gfn (which seems to be the case for x86's PV, but I may mistake) we should indeed use open-coded
construction "pfn_to_gfn(page_to_xen_pfn(page) + gfn_offset + i)". And the gfn_offset should be renamed to pfn_offset then.
Correct?
Thanks.
Quoted 7 lines
Quoted 6 lines
}
-dma_handle=grant_to_dma(grant)+ offset; +dma_handle=grant_to_dma(grant)+dma_offset; returndma_handle;
}
Signature · 3 lines
Regards, Oleksandr Tyshchenko
Quoted 53 lines · Oleksandr Tyshchenko
On 08.10.22 14:08, Xenia Ragiadakou wrote:
Hello Xenia
Quoted 48 lines · Oleksandr Tyshchenko
Hi Oleksandr
Quoted 32 lines
From: Oleksandr Tyshchenko <[email protected]>
Passed to
xen_grant_dma_map_page()offset in the page can be >PAGE_SIZEeven if the guest uses the same page granularity as Xen (4KB).Before current patch, if such case happened we ended up providing grants for the whole region in
xen_grant_dma_map_page()which was really unnecessary. The more, we ended up not releasing all grants which represented that region inxen_grant_dma_unmap_page().Current patch updates the code to be able to deal with such cases.
Signed-off-by Oleksandr Tyshchenko <[email protected]>---
Cc: Juergen Gross <[email protected]>
Cc: Xenia Ragiadakou <[email protected]>Depens on:
https://urldefense.com/v3/__https://lore.kernel.org/xen-devel/[email protected]/__;!!GF_29dbcQIUBPA!xnkNaKpfZ4LssQJcJs_J91KERZKMP2Rd-xEdBqXNXJ8GyCXJ0gkRer1elVYfxOWtwN_FOl9tVieDWlfN-UZaHQsyLMhA$
[lore[.]kernel[.]org]Should go in only after that series.
--- drivers/xen/grant-dma-ops.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)drivers/xen/grant-dma-ops.c +0
-0drivers/xen/grant-dma-ops.c static dma_addr_t xen_grant_dma_map_page(struct · line 174
Quoted 8 lines
drivers/xen/grant-dma-ops.c +3
-1@@ -168,7 +168,9 @@ static dma_addr_t xen_grant_dma_map_page(struct device *dev, struct page *page, unsigned long attrs) { struct xen_grant_dma_data *data; - unsigned int i, n_pages = PFN_UP(offset + size); + unsigned long dma_offset = offset_in_page(offset), + gfn_offset = PFN_DOWN(offset); + unsigned int i, n_pages = PFN_UP(dma_offset + size);IIUC, the above with a later patch will become:
Quoted 7 lines
Quoted 3 lines
dma_offset=xen_offset_in_page(offset)gfn_offset=XEN_PFN_DOWN(offset)n_pages=XEN_PFN_UP(dma_offset + size)If saying "later" patch you meant "xen/virtio: Convert PAGE_SIZE/PAGE_SHIFT/PFN_UP to Xen counterparts" then yes, exactly.
Ah ok, I see.
Quoted 23 lines
Quoted 13 lines
Quoted 4 lines
drivers/xen/grant-dma-ops.c +0
-0grant_ref_t grant; dma_addr_t dma_handle;Quoted 8 lines
@@ -187,10 +189,10 @@ static
dma_addr_t
xen_grant_dma_map_page(struct device *dev, struct page *page,
for (i = 0; i <n_pages; i++) { gnttab_grant_foreign_access_ref(grant + i,data->backend_domid,
-xen_page_to_gfn(page)+ i, dir ==DMA_TO_DEVICE); +xen_page_to_gfn(page)+ i +gfn_offset, dir ==DMA_TO_DEVICE);Quoted 2 lines
Here, why the pfn is not calculated before passing it to
pfn_to_gfn()? I mean sth likepfn_to_gfn(page_to_xen_pfn(page) + gfn_offset + i)The
gfn_offsetis just a const value here, which just means how many gfns we should skip. But ...... I think, I get your point. So, if the region which is contiguous in pfn might be non-contiguous in gfn (which seems to be the case for x86's PV, but I may mistake) we should indeed use open-coded
Quoted 5 lines
construction "
pfn_to_gfn(page_to_xen_pfn(page) + gfn_offset + i)". And thegfn_offsetshould be renamed topfn_offsetthen.Correct?
Yes, that 's what I had in mind unless I 'm missing sth.
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
}
-dma_handle=grant_to_dma(grant)+ offset;
+dma_handle=grant_to_dma(grant)+dma_offset;
returndma_handle;
}
Signature · 1 line
Xenia
Oleksandr Tyshchenko
reviewerOn 08.10.22 15:59, Xenia Ragiadakou wrote:
Hello Xenia
Quoted 89 lines · Oleksandr Tyshchenko
Quoted 54 lines · Xenia Ragiadakou
Hello Xenia
Quoted 49 lines · Oleksandr Tyshchenko
Hi Oleksandr
Quoted 33 lines
From: Oleksandr Tyshchenko <[email protected]>
Passed to
xen_grant_dma_map_page()offset in the page can be >PAGE_SIZEeven if the guest uses the same page granularity as Xen (4KB).Before current patch, if such case happened we ended up providing grants for the whole region in
xen_grant_dma_map_page()which was really unnecessary. The more, we ended up not releasing all grants which represented that region inxen_grant_dma_unmap_page().Current patch updates the code to be able to deal with such cases.
Signed-off-by Oleksandr Tyshchenko <[email protected]>---
Cc: Juergen Gross <[email protected]>
Cc: Xenia Ragiadakou <[email protected]>[lore[.]kernel[.]org]
Should go in only after that series.
---
drivers/xen/grant-dma-ops.c| 8 +++++---1 file changed, 5 insertions(+), 3 deletions(-)drivers/xen/grant-dma-ops.c +0
-0drivers/xen/grant-dma-ops.c static dma_addr_t xen_grant_dma_map_page(struct · line 174
Quoted 8 lines
drivers/xen/grant-dma-ops.c +3
-1@@ -168,7 +168,9 @@ static dma_addr_t xen_grant_dma_map_page(struct device *dev, struct page *page, unsigned long attrs) { struct xen_grant_dma_data *data; - unsigned int i, n_pages = PFN_UP(offset + size); + unsigned long dma_offset = offset_in_page(offset), + gfn_offset = PFN_DOWN(offset); + unsigned int i, n_pages = PFN_UP(dma_offset + size);IIUC, the above with a later patch will become:
Quoted 7 lines
Quoted 3 lines
dma_offset=xen_offset_in_page(offset)gfn_offset=XEN_PFN_DOWN(offset)n_pages=XEN_PFN_UP(dma_offset + size)If saying "later" patch you meant "xen/virtio: Convert PAGE_SIZE/PAGE_SHIFT/PFN_UP to Xen counterparts" then yes, exactly.
Ah ok, I see.
Quoted 23 lines
Quoted 13 lines
Quoted 4 lines
drivers/xen/grant-dma-ops.c +0
-0grant_ref_t grant; dma_addr_t dma_handle;Quoted 8 lines
@@ -187,10 +189,10 @@ static
dma_addr_txen_grant_dma_map_page(struct device *dev, struct page *page, for (i = 0; i <n_pages; i++) { gnttab_grant_foreign_access_ref(grant + i,data->backend_domid,
-xen_page_to_gfn(page)+ i, dir ==DMA_TO_DEVICE); +xen_page_to_gfn(page)+ i +gfn_offset, dir ==DMA_TO_DEVICE);Quoted 2 lines
Here, why the pfn is not calculated before passing it to
pfn_to_gfn()? I mean sth likepfn_to_gfn(page_to_xen_pfn(page) + gfn_offset + i)The
gfn_offsetis just a const value here, which just means how many gfns we should skip. But ...... I think, I get your point. So, if the region which is contiguous in pfn might be non-contiguous in gfn (which seems to be the case for x86's PV, but I may mistake) we should indeed use open-coded
Quoted 7 lines
Quoted 5 lines
construction "
pfn_to_gfn(page_to_xen_pfn(page) + gfn_offset + i)". And thegfn_offsetshould be renamed topfn_offsetthen.Correct?
Yes, that 's what I had in mind unless I 'm missing sth.
ok, thanks for confirming. So I will create V2 then.
Quoted 9 lines
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
}
-dma_handle=grant_to_dma(grant)+ offset; +dma_handle=grant_to_dma(grant)+dma_offset; returndma_handle;
}
Signature · 3 lines
Regards, Oleksandr Tyshchenko