xen/swiotlb: Exchange to contiguous memory for map_sg hook
Subsystems (2)
PATCH 1/1 xen/swiotlb: Exchange to contiguous memory for map_sg hook
While mapping sg buffers, checking to cross page DMA buffer is also needed. If the guest DMA buffer crosses page boundary, Xen should exchange contiguous memory for it.
Besides, it is needed to backup the original page contents and copy it back after memory exchange is done.
This fixes issues if device DMA into software static buffers, and in case the static buffer cross page boundary which pages are not contiguous in real hardware.
--- drivers/xen/swiotlb-xen.c | 47 ++++++++++++++++++++++++++++++++++++++++++++- 1 files changed, 46 insertions(+), 1 deletions(-)
drivers/xen/swiotlb-xen.c +46 -1
@@ -461,6 +461,22 @@ xen_swiotlb_sync_single_for_device(struct device *hwdev, dma_addr_t dev_addr, }EXPORT_SYMBOL_GPL (xen_swiotlb_sync_single_for_device); +static bool +check_continguous_region (unsigned long vstart,unsigned long order) +{ +unsigned long prev_ma =xen_virt_to_bus ((void *)vstart); +unsigned long next_ma; +int i; + +for (i =1 ; i < (1 << order); i++) { + next_ma =xen_virt_to_bus ((void *)(vstart + i *PAGE_SIZE )); +if (next_ma != prev_ma +PAGE_SIZE ) +return false; + prev_ma = next_ma; + } +return true; +} +/* * Map a set of buffers described by scatterlist in streaming mode for DMA. * This is the scatter-gather version of the above xen_swiotlb_map_page
@@ -489,7 +505,36 @@ xen_swiotlb_map_sg_attrs(struct device *hwdev, struct scatterlist *sgl,for_each_sg (sgl, sg, nelems, i) {phys_addr_t paddr =sg_phys (sg); -dma_addr_t dev_addr =xen_phys_to_bus (paddr); +unsigned long vstart, order; +dma_addr_t dev_addr; + +/* +* While mapping sg buffers, checking to cross page DMA buffer +* is also needed. If the guest DMA buffer crosses page +* boundary, Xen should exchange contiguous memory for it. +* Besides, it is needed to backup the original page contents +* and copy it back after memory exchange is done. +*/ +if (range_straddles_page_boundary (paddr, sg->length)) { + vstart = (unsigned long )__va (paddr &PAGE_MASK ); + order =get_order (sg->length + (paddr & ~PAGE_MASK )); +if (!check_continguous_region (vstart, order)) { +unsigned long buf; + buf =__get_free_pages (GFP_KERNEL , order); +memcpy ((void *)buf, (void *)vstart, +PAGE_SIZE * (1 << order)); +if (xen_create_contiguous_region (vstart, order, +fls64 (paddr))) { +free_pages (buf, order); +return 0 ; + } +memcpy ((void *)vstart, (void *)buf, +PAGE_SIZE * (1 << order)); +free_pages (buf, order); + } + } + + dev_addr =xen_phys_to_bus (paddr); if (swiotlb_force || !dma_capable(hwdev, dev_addr, sg->length) ||
Signature · 1 line
1.7.1
Quoted 24 lines
Quoted 1 line
Quoted 1 line · Dongxiao Xu
While mapping sg buffers, checking to cross page DMA buffer is also needed. If the guest DMA buffer crosses page boundary, Xen should exchange contiguous memory for it.
Besides, it is needed to backup the original page contents and copy it back after memory exchange is done.
This fixes issues if device DMA into software static buffers, and in case the static buffer cross page boundary which pages are not contiguous in real hardware.
Signed-off-by Dongxiao Xu <[email protected]>Signed-off-by Xiantao Zhang <[email protected]>--- drivers/xen/swiotlb-xen.c | 47 ++++++++++++++++++++++++++++++++++++++++++++- 1 files changed, 46 insertions(+), 1 deletions(-)drivers/xen/swiotlb-xen.c +0
-0
drivers/xen/swiotlb-xen.c xen_swiotlb_sync_single_for_device(struct device *hwdev, · line 466
Quoted 6 lines
drivers/xen/swiotlb-xen.c +2
-0@@ -461,6 +461,22 @@ xen_swiotlb_sync_single_for_device(struct device *hwdev,dma_addr_t dev_addr, }EXPORT_SYMBOL_GPL (xen_swiotlb_sync_single_for_device ); +static bool +check_continguous_region (unsigned long vstart,unsigned long order)
check_continguous_region(unsigned long vstart, unsigned int order)
But - why do you need to do this check order based in the first place? Checking the actual length of the buffer should suffice.
drivers/xen/swiotlb-xen.c xen_swiotlb_sync_single_for_device(struct device *hwdev, dma_addr_t dev_addr, · line 468
Quoted 3 lines
drivers/xen/swiotlb-xen.c +3
-0+{ +unsigned long prev_ma =xen_virt_to_bus ((void *)vstart); +unsigned long next_ma;
phys_addr_t or some such for both of them.
drivers/xen/swiotlb-xen.c xen_swiotlb_sync_single_for_device(struct device *hwdev, dma_addr_t dev_addr, · line 469
Quoted 1 line
drivers/xen/swiotlb-xen.c +1
-0+int i;
unsigned long
drivers/xen/swiotlb-xen.c xen_swiotlb_sync_single_for_device(struct device *hwdev, dma_addr_t dev_addr, · line 471
Quoted 2 lines
drivers/xen/swiotlb-xen.c +2
-0+ +for (i =1 ; i < (1 << order); i++) {
1UL
Quoted 49 lines
drivers/xen/swiotlb-xen.c +8
-0+ next_ma =xen_virt_to_bus ((void *)(vstart + i *PAGE_SIZE )); +if (next_ma != prev_ma +PAGE_SIZE ) +return false; + prev_ma = next_ma; + } +return true; +} +/* * Map a set of buffers described by scatterlist in streaming mode for DMA. * This is the scatter-gather version of the above
xen_swiotlb_map_pagedrivers/xen/swiotlb-xen.c +30
-1@@ -489,7 +505,36 @@ xen_swiotlb_map_sg_attrs(struct device *hwdev, struct scatterlist *sgl, for_each_sg(sgl, sg, nelems, i) {phys_addr_t paddr =sg_phys (sg); -dma_addr_t dev_addr =xen_phys_to_bus (paddr); +unsigned long vstart, order; +dma_addr_t dev_addr; + +/* +* While mapping sg buffers, checking to cross page DMA buffer +* is also needed. If the guest DMA buffer crosses page +* boundary, Xen should exchange contiguous memory for it. +* Besides, it is needed to backup the original page contents +* and copy it back after memory exchange is done. +*/ +if (range_straddles_page_boundary (paddr, sg->length)) { + vstart = (unsigned long )__va (paddr &PAGE_MASK ); + order =get_order (sg->length + (paddr & ~PAGE_MASK )); +if (!check_continguous_region (vstart, order)) { +unsigned long buf; + buf =__get_free_pages (GFP_KERNEL , order); +memcpy ((void *)buf, (void *)vstart, +PAGE_SIZE * (1 << order)); +if (xen_create_contiguous_region (vstart, order, +fls64 (paddr))) { +free_pages (buf, order); +return 0 ; + } +memcpy ((void *)vstart, (void *)buf, +PAGE_SIZE * (1 << order)); +free_pages (buf, order); + } + } + + dev_addr =xen_phys_to_bus (paddr); if (swiotlb_force ||
Quoted 1 line
!dma_capable(hwdev,
dev_addr,sg->length) ||
How about swiotlb_map_page() (for the compound page case)?
Jan
Quoted 24 lines
Quoted 1 line
Quoted 1 line · Dongxiao Xu
While mapping sg buffers, checking to cross page DMA buffer is also needed. If the guest DMA buffer crosses page boundary, Xen should exchange contiguous memory for it.
Besides, it is needed to backup the original page contents and copy it back after memory exchange is done.
This fixes issues if device DMA into software static buffers, and in case the static buffer cross page boundary which pages are not contiguous in real hardware.
Signed-off-by Dongxiao Xu <[email protected]>Signed-off-by Xiantao Zhang <[email protected]>--- drivers/xen/swiotlb-xen.c | 47 ++++++++++++++++++++++++++++++++++++++++++++- 1 files changed, 46 insertions(+), 1 deletions(-)drivers/xen/swiotlb-xen.c +0
-0
drivers/xen/swiotlb-xen.c xen_swiotlb_sync_single_for_device(struct device *hwdev, · line 466
Quoted 6 lines
drivers/xen/swiotlb-xen.c +2
-0@@ -461,6 +461,22 @@ xen_swiotlb_sync_single_for_device(struct device *hwdev,dma_addr_t dev_addr, }EXPORT_SYMBOL_GPL (xen_swiotlb_sync_single_for_device ); +static bool +check_continguous_region (unsigned long vstart,unsigned long order)
check_continguous_region(unsigned long vstart, unsigned int order)
But - why do you need to do this check order based in the first place? Checking the actual length of the buffer should suffice.
drivers/xen/swiotlb-xen.c xen_swiotlb_sync_single_for_device(struct device *hwdev, dma_addr_t dev_addr, · line 468
Quoted 3 lines
drivers/xen/swiotlb-xen.c +3
-0+{ +unsigned long prev_ma =xen_virt_to_bus ((void *)vstart); +unsigned long next_ma;
phys_addr_t or some such for both of them.
drivers/xen/swiotlb-xen.c xen_swiotlb_sync_single_for_device(struct device *hwdev, dma_addr_t dev_addr, · line 469
Quoted 1 line
drivers/xen/swiotlb-xen.c +1
-0+int i;
unsigned long
drivers/xen/swiotlb-xen.c xen_swiotlb_sync_single_for_device(struct device *hwdev, dma_addr_t dev_addr, · line 471
Quoted 2 lines
drivers/xen/swiotlb-xen.c +2
-0+ +for (i =1 ; i < (1 << order); i++) {
1UL
Quoted 49 lines
drivers/xen/swiotlb-xen.c +8
-0+ next_ma =xen_virt_to_bus ((void *)(vstart + i *PAGE_SIZE )); +if (next_ma != prev_ma +PAGE_SIZE ) +return false; + prev_ma = next_ma; + } +return true; +} +/* * Map a set of buffers described by scatterlist in streaming mode for DMA. * This is the scatter-gather version of the above
xen_swiotlb_map_pagedrivers/xen/swiotlb-xen.c +30
-1@@ -489,7 +505,36 @@ xen_swiotlb_map_sg_attrs(struct device *hwdev, struct scatterlist *sgl, for_each_sg(sgl, sg, nelems, i) {phys_addr_t paddr =sg_phys (sg); -dma_addr_t dev_addr =xen_phys_to_bus (paddr); +unsigned long vstart, order; +dma_addr_t dev_addr; + +/* +* While mapping sg buffers, checking to cross page DMA buffer +* is also needed. If the guest DMA buffer crosses page +* boundary, Xen should exchange contiguous memory for it. +* Besides, it is needed to backup the original page contents +* and copy it back after memory exchange is done. +*/ +if (range_straddles_page_boundary (paddr, sg->length)) { + vstart = (unsigned long )__va (paddr &PAGE_MASK ); + order =get_order (sg->length + (paddr & ~PAGE_MASK )); +if (!check_continguous_region (vstart, order)) { +unsigned long buf; + buf =__get_free_pages (GFP_KERNEL , order); +memcpy ((void *)buf, (void *)vstart, +PAGE_SIZE * (1 << order)); +if (xen_create_contiguous_region (vstart, order, +fls64 (paddr))) { +free_pages (buf, order); +return 0 ; + } +memcpy ((void *)vstart, (void *)buf, +PAGE_SIZE * (1 << order)); +free_pages (buf, order); + } + } + + dev_addr =xen_phys_to_bus (paddr); if (swiotlb_force ||
Quoted 1 line
!dma_capable(hwdev,
dev_addr,sg->length) ||
How about swiotlb_map_page() (for the compound page case)?
Jan
Quoted 99 lines · Jan Beulich
Quoted 24 lines
Quoted 1 line
Quoted 1 line · Dongxiao Xu
While mapping sg buffers, checking to cross page DMA buffer is also needed. If the guest DMA buffer crosses page boundary, Xen should exchange contiguous memory for it.
Besides, it is needed to backup the original page contents and copy it back after memory exchange is done.
This fixes issues if device DMA into software static buffers, and in case the static buffer cross page boundary which pages are not contiguous in real hardware.
Signed-off-by Dongxiao Xu <[email protected]>Signed-off-by Xiantao Zhang <[email protected]>--- drivers/xen/swiotlb-xen.c | 47 ++++++++++++++++++++++++++++++++++++++++++++- 1 files changed, 46 insertions(+), 1 deletions(-)drivers/xen/swiotlb-xen.c +0
-0drivers/xen/swiotlb-xen.c xen_swiotlb_sync_single_for_device(struct device *hwdev, · line 466
Quoted 6 lines
drivers/xen/swiotlb-xen.c +2
-0@@ -461,6 +461,22 @@ xen_swiotlb_sync_single_for_device(struct device *hwdev,dma_addr_t dev_addr, }EXPORT_SYMBOL_GPL (xen_swiotlb_sync_single_for_device ); +static bool +check_continguous_region (unsigned long vstart,unsigned long order)
check_continguous_region(unsigned long vstart, unsigned int order)But - why do you need to do this check order based in the first place? Checking the actual length of the buffer should suffice.
drivers/xen/swiotlb-xen.c xen_swiotlb_sync_single_for_device(struct device *hwdev, dma_addr_t dev_addr, · line 468
Quoted 3 lines
drivers/xen/swiotlb-xen.c +3
-0+{ +unsigned long prev_ma =xen_virt_to_bus ((void *)vstart); +unsigned long next_ma;
phys_addr_tor some such for both of them.drivers/xen/swiotlb-xen.c xen_swiotlb_sync_single_for_device(struct device *hwdev, dma_addr_t dev_addr, · line 469
Quoted 1 line
drivers/xen/swiotlb-xen.c +1
-0+int i;unsigned long
drivers/xen/swiotlb-xen.c xen_swiotlb_sync_single_for_device(struct device *hwdev, dma_addr_t dev_addr, · line 471
Quoted 2 lines
drivers/xen/swiotlb-xen.c +2
-0+ +for (i =1 ; i < (1 << order); i++) {1UL
Quoted 48 lines
drivers/xen/swiotlb-xen.c +8
-0+ next_ma =xen_virt_to_bus ((void *)(vstart + i *PAGE_SIZE )); +if (next_ma != prev_ma +PAGE_SIZE ) +return false; + prev_ma = next_ma; + } +return true; +} +/* * Map a set of buffers described by scatterlist in streaming mode for DMA. * This is the scatter-gather version of the above
xen_swiotlb_map_pagedrivers/xen/swiotlb-xen.c +30
-1@@ -489,7 +505,36 @@ xen_swiotlb_map_sg_attrs(struct device *hwdev, struct scatterlist *sgl, for_each_sg(sgl, sg, nelems, i) {phys_addr_t paddr =sg_phys (sg); -dma_addr_t dev_addr =xen_phys_to_bus (paddr); +unsigned long vstart, order; +dma_addr_t dev_addr; + +/* +* While mapping sg buffers, checking to cross page DMA buffer +* is also needed. If the guest DMA buffer crosses page +* boundary, Xen should exchange contiguous memory for it. +* Besides, it is needed to backup the original page contents +* and copy it back after memory exchange is done. +*/ +if (range_straddles_page_boundary (paddr, sg->length)) { + vstart = (unsigned long )__va (paddr &PAGE_MASK ); + order =get_order (sg->length + (paddr & ~PAGE_MASK )); +if (!check_continguous_region (vstart, order)) { +unsigned long buf; + buf =__get_free_pages (GFP_KERNEL , order); +memcpy ((void *)buf, (void *)vstart, +PAGE_SIZE * (1 << order)); +if (xen_create_contiguous_region (vstart, order, +fls64 (paddr))) { +free_pages (buf, order); +return 0 ; + } +memcpy ((void *)vstart, (void *)buf, +PAGE_SIZE * (1 << order)); +free_pages (buf, order); + } + } + + dev_addr =xen_phys_to_bus (paddr);
Quoted 4 lines
Quoted 2 lines
drivers/xen/swiotlb-xen.c +0
-0if (swiotlb_force ||!dma_capable(hwdev,
dev_addr,sg->length) ||How about
swiotlb_map_page()(for the compound page case)?
Heh. Thanks - I just got to your reply now and had the same question.
Interestingly enough - this looks like a problem that has been forever and nobody ever hit this.
Worst, the problem is even present if a driver uses the pci_alloc_coherent and asks for a 3MB region or such - as we can at most give out only 2MB swaths.
Quoted 3 lines
Jan
Quoted 99 lines · Jan Beulich
Quoted 24 lines
Quoted 1 line
Quoted 1 line · Dongxiao Xu
While mapping sg buffers, checking to cross page DMA buffer is also needed. If the guest DMA buffer crosses page boundary, Xen should exchange contiguous memory for it.
Besides, it is needed to backup the original page contents and copy it back after memory exchange is done.
This fixes issues if device DMA into software static buffers, and in case the static buffer cross page boundary which pages are not contiguous in real hardware.
Signed-off-by Dongxiao Xu <[email protected]>Signed-off-by Xiantao Zhang <[email protected]>--- drivers/xen/swiotlb-xen.c | 47 ++++++++++++++++++++++++++++++++++++++++++++- 1 files changed, 46 insertions(+), 1 deletions(-)drivers/xen/swiotlb-xen.c +0
-0drivers/xen/swiotlb-xen.c xen_swiotlb_sync_single_for_device(struct device *hwdev, · line 466
Quoted 6 lines
drivers/xen/swiotlb-xen.c +2
-0@@ -461,6 +461,22 @@ xen_swiotlb_sync_single_for_device(struct device *hwdev,dma_addr_t dev_addr, }EXPORT_SYMBOL_GPL (xen_swiotlb_sync_single_for_device ); +static bool +check_continguous_region (unsigned long vstart,unsigned long order)
check_continguous_region(unsigned long vstart, unsigned int order)But - why do you need to do this check order based in the first place? Checking the actual length of the buffer should suffice.
drivers/xen/swiotlb-xen.c xen_swiotlb_sync_single_for_device(struct device *hwdev, dma_addr_t dev_addr, · line 468
Quoted 3 lines
drivers/xen/swiotlb-xen.c +3
-0+{ +unsigned long prev_ma =xen_virt_to_bus ((void *)vstart); +unsigned long next_ma;
phys_addr_tor some such for both of them.drivers/xen/swiotlb-xen.c xen_swiotlb_sync_single_for_device(struct device *hwdev, dma_addr_t dev_addr, · line 469
Quoted 1 line
drivers/xen/swiotlb-xen.c +1
-0+int i;unsigned long
drivers/xen/swiotlb-xen.c xen_swiotlb_sync_single_for_device(struct device *hwdev, dma_addr_t dev_addr, · line 471
Quoted 2 lines
drivers/xen/swiotlb-xen.c +2
-0+ +for (i =1 ; i < (1 << order); i++) {1UL
Quoted 48 lines
drivers/xen/swiotlb-xen.c +8
-0+ next_ma =xen_virt_to_bus ((void *)(vstart + i *PAGE_SIZE )); +if (next_ma != prev_ma +PAGE_SIZE ) +return false; + prev_ma = next_ma; + } +return true; +} +/* * Map a set of buffers described by scatterlist in streaming mode for DMA. * This is the scatter-gather version of the above
xen_swiotlb_map_pagedrivers/xen/swiotlb-xen.c +30
-1@@ -489,7 +505,36 @@ xen_swiotlb_map_sg_attrs(struct device *hwdev, struct scatterlist *sgl, for_each_sg(sgl, sg, nelems, i) {phys_addr_t paddr =sg_phys (sg); -dma_addr_t dev_addr =xen_phys_to_bus (paddr); +unsigned long vstart, order; +dma_addr_t dev_addr; + +/* +* While mapping sg buffers, checking to cross page DMA buffer +* is also needed. If the guest DMA buffer crosses page +* boundary, Xen should exchange contiguous memory for it. +* Besides, it is needed to backup the original page contents +* and copy it back after memory exchange is done. +*/ +if (range_straddles_page_boundary (paddr, sg->length)) { + vstart = (unsigned long )__va (paddr &PAGE_MASK ); + order =get_order (sg->length + (paddr & ~PAGE_MASK )); +if (!check_continguous_region (vstart, order)) { +unsigned long buf; + buf =__get_free_pages (GFP_KERNEL , order); +memcpy ((void *)buf, (void *)vstart, +PAGE_SIZE * (1 << order)); +if (xen_create_contiguous_region (vstart, order, +fls64 (paddr))) { +free_pages (buf, order); +return 0 ; + } +memcpy ((void *)vstart, (void *)buf, +PAGE_SIZE * (1 << order)); +free_pages (buf, order); + } + } + + dev_addr =xen_phys_to_bus (paddr);
Quoted 4 lines
Quoted 2 lines
drivers/xen/swiotlb-xen.c +0
-0if (swiotlb_force ||!dma_capable(hwdev,
dev_addr,sg->length) ||How about
swiotlb_map_page()(for the compound page case)?
Heh. Thanks - I just got to your reply now and had the same question.
Interestingly enough - this looks like a problem that has been forever and nobody ever hit this.
Worst, the problem is even present if a driver uses the pci_alloc_coherent and asks for a 3MB region or such - as we can at most give out only 2MB swaths.
Quoted 3 lines
Jan
Quoted 3 lines · Dongxiao Xu
While mapping sg buffers, checking to cross page DMA buffer is also needed. If the guest DMA buffer crosses page boundary, Xen should exchange contiguous memory for it.
So this is when we cross those 2MB contingous swatch of buffers. Wouldn't we get the same problem with the 'map_page' call? If the driver tried to map say a 4MB DMA region?
What if this check was done in the routines that provide the software static buffers and there try to provide a nice DMA contingous swatch of pages?
Quoted 82 lines
Besides, it is needed to backup the original page contents and copy it back after memory exchange is done.
This fixes issues if device DMA into software static buffers, and in case the static buffer cross page boundary which pages are not contiguous in real hardware.
Signed-off-by Dongxiao Xu <[email protected]>Signed-off-by Xiantao Zhang <[email protected]>--- drivers/xen/swiotlb-xen.c | 47 ++++++++++++++++++++++++++++++++++++++++++++- 1 files changed, 46 insertions(+), 1 deletions(-)drivers/xen/swiotlb-xen.c +46
-1@@ -461,6 +461,22 @@ xen_swiotlb_sync_single_for_device(struct device *hwdev, dma_addr_t dev_addr, }EXPORT_SYMBOL_GPL (xen_swiotlb_sync_single_for_device); +static bool +check_continguous_region (unsigned long vstart,unsigned long order) +{ +unsigned long prev_ma =xen_virt_to_bus ((void *)vstart); +unsigned long next_ma; +int i; + +for (i =1 ; i < (1 << order); i++) { + next_ma =xen_virt_to_bus ((void *)(vstart + i *PAGE_SIZE )); +if (next_ma != prev_ma +PAGE_SIZE ) +return false; + prev_ma = next_ma; + } +return true; +} +/* * Map a set of buffers described by scatterlist in streaming mode for DMA. * This is the scatter-gather version of the above xen_swiotlb_map_page @@ -489,7 +505,36 @@ xen_swiotlb_map_sg_attrs(struct device *hwdev, struct scatterlist *sgl,for_each_sg (sgl, sg, nelems, i) {phys_addr_t paddr =sg_phys (sg); -dma_addr_t dev_addr =xen_phys_to_bus (paddr); +unsigned long vstart, order; +dma_addr_t dev_addr; + +/* +* While mapping sg buffers, checking to cross page DMA buffer +* is also needed. If the guest DMA buffer crosses page +* boundary, Xen should exchange contiguous memory for it. +* Besides, it is needed to backup the original page contents +* and copy it back after memory exchange is done. +*/ +if (range_straddles_page_boundary (paddr, sg->length)) { + vstart = (unsigned long )__va (paddr &PAGE_MASK ); + order =get_order (sg->length + (paddr & ~PAGE_MASK )); +if (!check_continguous_region (vstart, order)) { +unsigned long buf; + buf =__get_free_pages (GFP_KERNEL , order); +memcpy ((void *)buf, (void *)vstart, +PAGE_SIZE * (1 << order)); +if (xen_create_contiguous_region (vstart, order, +fls64 (paddr))) { +free_pages (buf, order); +return 0 ; + } +memcpy ((void *)vstart, (void *)buf, +PAGE_SIZE * (1 << order)); +free_pages (buf, order); + } + } + + dev_addr =xen_phys_to_bus (paddr); if (swiotlb_force || !dma_capable(hwdev, dev_addr, sg->length) ||Signature · 1 line
1.7.1
Quoted 3 lines · Dongxiao Xu
While mapping sg buffers, checking to cross page DMA buffer is also needed. If the guest DMA buffer crosses page boundary, Xen should exchange contiguous memory for it.
So this is when we cross those 2MB contingous swatch of buffers. Wouldn't we get the same problem with the 'map_page' call? If the driver tried to map say a 4MB DMA region?
What if this check was done in the routines that provide the software static buffers and there try to provide a nice DMA contingous swatch of pages?
Quoted 82 lines
Besides, it is needed to backup the original page contents and copy it back after memory exchange is done.
This fixes issues if device DMA into software static buffers, and in case the static buffer cross page boundary which pages are not contiguous in real hardware.
Signed-off-by Dongxiao Xu <[email protected]>Signed-off-by Xiantao Zhang <[email protected]>--- drivers/xen/swiotlb-xen.c | 47 ++++++++++++++++++++++++++++++++++++++++++++- 1 files changed, 46 insertions(+), 1 deletions(-)drivers/xen/swiotlb-xen.c +46
-1@@ -461,6 +461,22 @@ xen_swiotlb_sync_single_for_device(struct device *hwdev, dma_addr_t dev_addr, }EXPORT_SYMBOL_GPL (xen_swiotlb_sync_single_for_device); +static bool +check_continguous_region (unsigned long vstart,unsigned long order) +{ +unsigned long prev_ma =xen_virt_to_bus ((void *)vstart); +unsigned long next_ma; +int i; + +for (i =1 ; i < (1 << order); i++) { + next_ma =xen_virt_to_bus ((void *)(vstart + i *PAGE_SIZE )); +if (next_ma != prev_ma +PAGE_SIZE ) +return false; + prev_ma = next_ma; + } +return true; +} +/* * Map a set of buffers described by scatterlist in streaming mode for DMA. * This is the scatter-gather version of the above xen_swiotlb_map_page @@ -489,7 +505,36 @@ xen_swiotlb_map_sg_attrs(struct device *hwdev, struct scatterlist *sgl,for_each_sg (sgl, sg, nelems, i) {phys_addr_t paddr =sg_phys (sg); -dma_addr_t dev_addr =xen_phys_to_bus (paddr); +unsigned long vstart, order; +dma_addr_t dev_addr; + +/* +* While mapping sg buffers, checking to cross page DMA buffer +* is also needed. If the guest DMA buffer crosses page +* boundary, Xen should exchange contiguous memory for it. +* Besides, it is needed to backup the original page contents +* and copy it back after memory exchange is done. +*/ +if (range_straddles_page_boundary (paddr, sg->length)) { + vstart = (unsigned long )__va (paddr &PAGE_MASK ); + order =get_order (sg->length + (paddr & ~PAGE_MASK )); +if (!check_continguous_region (vstart, order)) { +unsigned long buf; + buf =__get_free_pages (GFP_KERNEL , order); +memcpy ((void *)buf, (void *)vstart, +PAGE_SIZE * (1 << order)); +if (xen_create_contiguous_region (vstart, order, +fls64 (paddr))) { +free_pages (buf, order); +return 0 ; + } +memcpy ((void *)vstart, (void *)buf, +PAGE_SIZE * (1 << order)); +free_pages (buf, order); + } + } + + dev_addr =xen_phys_to_bus (paddr); if (swiotlb_force || !dma_capable(hwdev, dev_addr, sg->length) ||Signature · 1 line
1.7.1
Quoted 8 lines
-----Original Message-----
From: Konrad Rzeszutek Wilk [mailto:[email protected]]
Sent: Friday, December 07, 2012 10:09 PM
To: Xu, Dongxiao
Cc: [email protected]; [email protected]
Subject: Re: [PATCH] xen/swiotlb: Exchange to contiguous memory formap_sghook
Quoted 8 lines
Quoted 3 lines · Dongxiao Xu
While mapping sg buffers, checking to cross page DMA buffer is also needed. If the guest DMA buffer crosses page boundary, Xen should exchange contiguous memory for it.
So this is when we cross those 2MB contingous swatch of buffers. Wouldn't we get the same problem with the '
map_page' call? If the driver tried to map say a 4MB DMA region?
Yes, it also needs such check, as I just replied to Jan's mail.
Quoted 3 lines
What if this check was done in the routines that provide the software static buffers and there try to provide a nice DMA contingous swatch of pages?
Yes, this approach also came to our mind, which needs to modify the driver itself.
If so, it requires driver not using such static buffers (e.g., from kmalloc) to do DMA even if the buffer is continuous in native. Is this acceptable by kernel/driver upstream?
Thanks,
Dongxiao
Quoted 86 lines
Quoted 5 lines
Besides, it is needed to backup the original page contents and copy it back after memory exchange is done.
Quoted 8 lines
This fixes issues if device DMA into software static buffers, and in case the static buffer cross page boundary which pages are not contiguous in real hardware.
Signed-off-by Dongxiao Xu <[email protected]>Signed-off-by Xiantao Zhang <[email protected]>--- drivers/xen/swiotlb-xen.c | 47++++++++++++++++++++++++++++++++++++++++++++-
Quoted 17 lines
1 files changed, 46 insertions(+), 1 deletions(-)drivers/xen/swiotlb-xen.c +7
-0@@ -461,6 +461,22 @@ xen_swiotlb_sync_single_for_device(struct device *hwdev, dma_addr_t dev_addr, }EXPORT_SYMBOL_GPL (xen_swiotlb_sync_single_for_device); +static bool +check_continguous_region (unsigned long vstart,unsigned long order) { +unsigned long prev_ma =xen_virt_to_bus ((void *)vstart); +unsigned long next_ma; +int i; + +for (i =1 ; i < (1 << order); i++) {drivers/xen/swiotlb-xen.c xen_swiotlb_sync_single_for_device(struct device · line 480
Quoted 10 lines
drivers/xen/swiotlb-xen.c +8
-0+ next_ma =xen_virt_to_bus ((void *)(vstart + i *PAGE_SIZE )); +if (next_ma != prev_ma +PAGE_SIZE ) +return false; + prev_ma = next_ma; + } +return true; +} +/* * Map a set of buffers described by scatterlist in streaming mode for DMA.
Quoted 44 lines
drivers/xen/swiotlb-xen.c +0
-0* This is the scatter-gather version of the above
xen_swiotlb_map_page@@ -489,7 +505,36 @@ xen_swiotlb_map_sg_attrs(struct device *hwdev, struct scatterlist *sgl,drivers/xen/swiotlb-xen.c +30
-2for_each_sg (sgl, sg, nelems, i) {phys_addr_t paddr =sg_phys (sg); -dma_addr_t dev_addr =xen_phys_to_bus (paddr); +unsigned long vstart, order; +dma_addr_t dev_addr; + +/* +* While mapping sg buffers, checking to cross page DMA buffer +* is also needed. If the guest DMA buffer crosses page +* boundary, Xen should exchange contiguous memory for it. +* Besides, it is needed to backup the original page contents +* and copy it back after memory exchange is done. +*/ +if (range_straddles_page_boundary (paddr, sg->length)) { + vstart = (unsigned long )__va (paddr &PAGE_MASK ); + order =get_order (sg->length + (paddr & ~PAGE_MASK )); +if (!check_continguous_region (vstart, order)) { +unsigned long buf; + buf =__get_free_pages (GFP_KERNEL , order); +memcpy ((void *)buf, (void *)vstart, +PAGE_SIZE * (1 << order)); +if (xen_create_contiguous_region (vstart, order, +fls64 (paddr))) { +free_pages (buf, order); +return 0 ; + } +memcpy ((void *)vstart, (void *)buf, +PAGE_SIZE * (1 << order)); +free_pages (buf, order); + } + } + + dev_addr =xen_phys_to_bus (paddr); if (swiotlb_force || !dma_capable(hwdev, dev_addr, sg->length) || --1.7.1
Quoted 8 lines
-----Original Message-----
From: Konrad Rzeszutek Wilk [mailto:[email protected]]
Sent: Friday, December 07, 2012 10:09 PM
To: Xu, Dongxiao
Cc: [email protected]; [email protected]
Subject: Re: [PATCH] xen/swiotlb: Exchange to contiguous memory formap_sghook
Quoted 8 lines
Quoted 3 lines · Dongxiao Xu
While mapping sg buffers, checking to cross page DMA buffer is also needed. If the guest DMA buffer crosses page boundary, Xen should exchange contiguous memory for it.
So this is when we cross those 2MB contingous swatch of buffers. Wouldn't we get the same problem with the '
map_page' call? If the driver tried to map say a 4MB DMA region?
Yes, it also needs such check, as I just replied to Jan's mail.
Quoted 3 lines
What if this check was done in the routines that provide the software static buffers and there try to provide a nice DMA contingous swatch of pages?
Yes, this approach also came to our mind, which needs to modify the driver itself.
If so, it requires driver not using such static buffers (e.g., from kmalloc) to do DMA even if the buffer is continuous in native. Is this acceptable by kernel/driver upstream?
Thanks,
Dongxiao
Quoted 86 lines
Quoted 5 lines
Besides, it is needed to backup the original page contents and copy it back after memory exchange is done.
Quoted 8 lines
This fixes issues if device DMA into software static buffers, and in case the static buffer cross page boundary which pages are not contiguous in real hardware.
Signed-off-by Dongxiao Xu <[email protected]>Signed-off-by Xiantao Zhang <[email protected]>--- drivers/xen/swiotlb-xen.c | 47++++++++++++++++++++++++++++++++++++++++++++-
Quoted 17 lines
1 files changed, 46 insertions(+), 1 deletions(-)drivers/xen/swiotlb-xen.c +7
-0@@ -461,6 +461,22 @@ xen_swiotlb_sync_single_for_device(struct device *hwdev, dma_addr_t dev_addr, }EXPORT_SYMBOL_GPL (xen_swiotlb_sync_single_for_device); +static bool +check_continguous_region (unsigned long vstart,unsigned long order) { +unsigned long prev_ma =xen_virt_to_bus ((void *)vstart); +unsigned long next_ma; +int i; + +for (i =1 ; i < (1 << order); i++) {drivers/xen/swiotlb-xen.c xen_swiotlb_sync_single_for_device(struct device · line 480
Quoted 10 lines
drivers/xen/swiotlb-xen.c +8
-0+ next_ma =xen_virt_to_bus ((void *)(vstart + i *PAGE_SIZE )); +if (next_ma != prev_ma +PAGE_SIZE ) +return false; + prev_ma = next_ma; + } +return true; +} +/* * Map a set of buffers described by scatterlist in streaming mode for DMA.
Quoted 44 lines
drivers/xen/swiotlb-xen.c +0
-0* This is the scatter-gather version of the above
xen_swiotlb_map_page@@ -489,7 +505,36 @@ xen_swiotlb_map_sg_attrs(struct device *hwdev, struct scatterlist *sgl,drivers/xen/swiotlb-xen.c +30
-2for_each_sg (sgl, sg, nelems, i) {phys_addr_t paddr =sg_phys (sg); -dma_addr_t dev_addr =xen_phys_to_bus (paddr); +unsigned long vstart, order; +dma_addr_t dev_addr; + +/* +* While mapping sg buffers, checking to cross page DMA buffer +* is also needed. If the guest DMA buffer crosses page +* boundary, Xen should exchange contiguous memory for it. +* Besides, it is needed to backup the original page contents +* and copy it back after memory exchange is done. +*/ +if (range_straddles_page_boundary (paddr, sg->length)) { + vstart = (unsigned long )__va (paddr &PAGE_MASK ); + order =get_order (sg->length + (paddr & ~PAGE_MASK )); +if (!check_continguous_region (vstart, order)) { +unsigned long buf; + buf =__get_free_pages (GFP_KERNEL , order); +memcpy ((void *)buf, (void *)vstart, +PAGE_SIZE * (1 << order)); +if (xen_create_contiguous_region (vstart, order, +fls64 (paddr))) { +free_pages (buf, order); +return 0 ; + } +memcpy ((void *)vstart, (void *)buf, +PAGE_SIZE * (1 << order)); +free_pages (buf, order); + } + } + + dev_addr =xen_phys_to_bus (paddr); if (swiotlb_force || !dma_capable(hwdev, dev_addr, sg->length) || --1.7.1
Quoted 17 lines · Dongxiao
Quoted 8 lines
-----Original Message-----
From: Konrad Rzeszutek Wilk [mailto:[email protected]]
Sent: Friday, December 07, 2012 10:09 PM
To: Xu, Dongxiao
Cc: [email protected]; [email protected]
Subject: Re: [PATCH] xen/swiotlb: Exchange to contiguous memory formap_sghookQuoted 8 lines
Quoted 3 lines · Dongxiao Xu
While mapping sg buffers, checking to cross page DMA buffer is also needed. If the guest DMA buffer crosses page boundary, Xen should exchange contiguous memory for it.
So this is when we cross those 2MB contingous swatch of buffers. Wouldn't we get the same problem with the '
map_page' call? If the driver tried to map say a 4MB DMA region?
Quoted 8 lines
Yes, it also needs such check, as I just replied to Jan's mail.
Quoted 3 lines
What if this check was done in the routines that provide the software static buffers and there try to provide a nice DMA contingous swatch of pages?
Yes, this approach also came to our mind, which needs to modify the driver itself.
If so, it requires driver not using such static buffers (e.g., from kmalloc) to do DMA even if the buffer is continuous in native.
I am bit loss here.
Is the issue you found only with drivers that do not use DMA API? Can you perhaps point me to the code that triggered this fix in the first place?
Quoted 1 line
Is this acceptable by kernel/driver upstream?
I am still not completely clear on what you had in mind. The one method I thought about that might help in this is to have Xen-SWIOTLB track which memory ranges were exchanged (so xen_swiotlb_fixup would save the *buf and the size for each call to xen_create_contiguous_region in a list or array).
When xen_swiotlb_map/xen_swiotlb_map_sg_attrs care called they would consult said array/list to see if the region they retrieved crosses said 2MB chunks. If so.. and here I am unsure of what would be the best way to proceed.
Quoted 17 lines · Dongxiao
Quoted 8 lines
-----Original Message-----
From: Konrad Rzeszutek Wilk [mailto:[email protected]]
Sent: Friday, December 07, 2012 10:09 PM
To: Xu, Dongxiao
Cc: [email protected]; [email protected]
Subject: Re: [PATCH] xen/swiotlb: Exchange to contiguous memory formap_sghookQuoted 8 lines
Quoted 3 lines · Dongxiao Xu
While mapping sg buffers, checking to cross page DMA buffer is also needed. If the guest DMA buffer crosses page boundary, Xen should exchange contiguous memory for it.
So this is when we cross those 2MB contingous swatch of buffers. Wouldn't we get the same problem with the '
map_page' call? If the driver tried to map say a 4MB DMA region?
Quoted 8 lines
Yes, it also needs such check, as I just replied to Jan's mail.
Quoted 3 lines
What if this check was done in the routines that provide the software static buffers and there try to provide a nice DMA contingous swatch of pages?
Yes, this approach also came to our mind, which needs to modify the driver itself.
If so, it requires driver not using such static buffers (e.g., from kmalloc) to do DMA even if the buffer is continuous in native.
I am bit loss here.
Is the issue you found only with drivers that do not use DMA API? Can you perhaps point me to the code that triggered this fix in the first place?
Quoted 1 line
Is this acceptable by kernel/driver upstream?
I am still not completely clear on what you had in mind. The one method I thought about that might help in this is to have Xen-SWIOTLB track which memory ranges were exchanged (so xen_swiotlb_fixup would save the *buf and the size for each call to xen_create_contiguous_region in a list or array).
When xen_swiotlb_map/xen_swiotlb_map_sg_attrs care called they would consult said array/list to see if the region they retrieved crosses said 2MB chunks. If so.. and here I am unsure of what would be the best way to proceed.
Quoted 34 lines
-----Original Message-----
From: Konrad Rzeszutek Wilk [mailto:[email protected]]
Sent: Wednesday, December 12, 2012 1:07 AM
To: Xu, Dongxiao
Cc: [email protected]; [email protected]
Subject: Re: [PATCH] xen/swiotlb: Exchange to contiguous memory formap_sghookQuoted 13 lines · Dongxiao
Quoted 13 lines
-----Original Message-----
From: Konrad Rzeszutek Wilk [mailto:[email protected]]
Sent: Friday, December 07, 2012 10:09 PM
To: Xu, Dongxiao
Cc: [email protected]; [email protected]
Subject: Re: [PATCH] xen/swiotlb: Exchange to contiguous memory formap_sghookQuoted 3 lines · Dongxiao Xu
While mapping sg buffers, checking to cross page DMA buffer is also needed. If the guest DMA buffer crosses page boundary, Xen should exchange contiguous memory for it.
Quoted 9 lines
Quoted 3 lines
So this is when we cross those 2MB contingous swatch of buffers. Wouldn't we get the same problem with the '
map_page' call? If the driver tried to map say a 4MB DMA region?Yes, it also needs such check, as I just replied to Jan's mail.
Quoted 3 lines
What if this check was done in the routines that provide the software static buffers and there try to provide a nice DMA contingous
swatch of pages?
Quoted 2 lines
Yes, this approach also came to our mind, which needs to modify the driver
Quoted 8 lines
itself.
Quoted 1 line
If so, it requires driver not using such static buffers (e.g., from kmalloc) to do
DMA even if the buffer is continuous in native.
I am bit loss here.
Is the issue you found only with drivers that do not use DMA API? Can you perhaps point me to the code that triggered this fix in the first place?
Yes, we met this issue on a specific SAS device/driver, and it calls into libata-core code, you can refer to function ata_dev_read_id() called from ata_dev_reread_id() in drivers/ata/libata-core.c.
In the above function, the target buffer is (void *)dev->link->ap->sector_buf, which is 512 bytes static buffer and unfortunately it across the page boundary.
Quoted 10 lines
Quoted 1 line
Is this acceptable by kernel/driver upstream?
I am still not completely clear on what you had in mind. The one method I thought about that might help in this is to have Xen-SWIOTLB track which memory ranges were exchanged (so
xen_swiotlb_fixupwould save the *buf and the size for each call toxen_create_contiguous_regionin a list or array).When xen_swiotlb_map/xen_swiotlb_map_sg_attrs care called they would consult said array/list to see if the region they retrieved crosses said 2MB chunks. If so.. and here I am unsure of what would be the best way to proceed.
We thought we can solve the issue in several ways:
1) Like the previous patch I sent out, we check the DMA region in xen_swiotlb_map_page() and xen_swiotlb_map_sg_attr(), and if DMA region crosses page boundary, we exchange the memory and copy the content. However it has race condition that when copying the memory content (we introduced two memory copies in the patch), some other code may also visit the page, which may encounter incorrect values.
2) Mostly the same as item 1), the only difference is that we put the memory content copy inside Xen hypervisor but not in Dom0. This requires we add certain flags to indicating memory moving in the XENMEM_exchange hypercall.
3) As you also mentioned, this is not a common case, it is only triggered by some specific devices/drivers. we can fix it in certain driver to avoid DMA into static buffers. Like (void *)dev->link->ap->sector_buf in the above case. But I am not sure whether it is acceptable by kernel/driver upstream.
Thanks,
Dongxiao
Quoted 34 lines
-----Original Message-----
From: Konrad Rzeszutek Wilk [mailto:[email protected]]
Sent: Wednesday, December 12, 2012 1:07 AM
To: Xu, Dongxiao
Cc: [email protected]; [email protected]
Subject: Re: [PATCH] xen/swiotlb: Exchange to contiguous memory formap_sghookQuoted 13 lines · Dongxiao
Quoted 13 lines
-----Original Message-----
From: Konrad Rzeszutek Wilk [mailto:[email protected]]
Sent: Friday, December 07, 2012 10:09 PM
To: Xu, Dongxiao
Cc: [email protected]; [email protected]
Subject: Re: [PATCH] xen/swiotlb: Exchange to contiguous memory formap_sghookQuoted 3 lines · Dongxiao Xu
While mapping sg buffers, checking to cross page DMA buffer is also needed. If the guest DMA buffer crosses page boundary, Xen should exchange contiguous memory for it.
Quoted 9 lines
Quoted 3 lines
So this is when we cross those 2MB contingous swatch of buffers. Wouldn't we get the same problem with the '
map_page' call? If the driver tried to map say a 4MB DMA region?Yes, it also needs such check, as I just replied to Jan's mail.
Quoted 3 lines
What if this check was done in the routines that provide the software static buffers and there try to provide a nice DMA contingous
swatch of pages?
Quoted 2 lines
Yes, this approach also came to our mind, which needs to modify the driver
Quoted 8 lines
itself.
Quoted 1 line
If so, it requires driver not using such static buffers (e.g., from kmalloc) to do
DMA even if the buffer is continuous in native.
I am bit loss here.
Is the issue you found only with drivers that do not use DMA API? Can you perhaps point me to the code that triggered this fix in the first place?
Yes, we met this issue on a specific SAS device/driver, and it calls into libata-core code, you can refer to function ata_dev_read_id() called from ata_dev_reread_id() in drivers/ata/libata-core.c.
In the above function, the target buffer is (void *)dev->link->ap->sector_buf, which is 512 bytes static buffer and unfortunately it across the page boundary.
Quoted 10 lines
Quoted 1 line
Is this acceptable by kernel/driver upstream?
I am still not completely clear on what you had in mind. The one method I thought about that might help in this is to have Xen-SWIOTLB track which memory ranges were exchanged (so
xen_swiotlb_fixupwould save the *buf and the size for each call toxen_create_contiguous_regionin a list or array).When xen_swiotlb_map/xen_swiotlb_map_sg_attrs care called they would consult said array/list to see if the region they retrieved crosses said 2MB chunks. If so.. and here I am unsure of what would be the best way to proceed.
We thought we can solve the issue in several ways:
1) Like the previous patch I sent out, we check the DMA region in xen_swiotlb_map_page() and xen_swiotlb_map_sg_attr(), and if DMA region crosses page boundary, we exchange the memory and copy the content. However it has race condition that when copying the memory content (we introduced two memory copies in the patch), some other code may also visit the page, which may encounter incorrect values.
2) Mostly the same as item 1), the only difference is that we put the memory content copy inside Xen hypervisor but not in Dom0. This requires we add certain flags to indicating memory moving in the XENMEM_exchange hypercall.
3) As you also mentioned, this is not a common case, it is only triggered by some specific devices/drivers. we can fix it in certain driver to avoid DMA into static buffers. Like (void *)dev->link->ap->sector_buf in the above case. But I am not sure whether it is acceptable by kernel/driver upstream.
Thanks,
Dongxiao
Quoted 20 lines
Quoted 11 lines
Quoted 1 line · Dongxiao
From: Konrad Rzeszutek Wilk [mailto:[email protected]]
Quoted 3 lines · Dongxiao
Quoted 3 lines
From: Konrad Rzeszutek Wilk [mailto:[email protected]] What if this check was done in the routines that provide the software static buffers and there try to provide a nice DMA contingous
swatch of pages?
Quoted 2 lines
Yes, this approach also came to our mind, which needs to modify the driver
itself.
Quoted 1 line
If so, it requires driver not using such static buffers (e.g., from
kmalloc) to do
Quoted 6 lines
DMA even if the buffer is continuous in native.
I am bit loss here.
Is the issue you found only with drivers that do not use DMA API? Can you perhaps point me to the code that triggered this fix in the first
place?
Quoted 7 lines
Yes, we met this issue on a specific SAS device/driver, and it calls into libata-core code, you can refer to function
ata_dev_read_id()called fromata_dev_reread_id()indrivers/ata/libata-core.c.In the above function, the target buffer is (void *)dev->link->ap->sector_buf, which is 512 bytes static buffer and unfortunately it across the page boundary.
I wonder whether such use of sg_init_one()/sg_set_buf() is correct in the first place. While there aren't any restrictions documented for its use, one clearly can't pass in whatever one wants (a pointer into vmalloc()-ed memory, for instance, won't work afaict).
I didn't go through all other users of it, but quite a few of the uses elsewhere look similarly questionable.
Quoted 11 lines
Quoted 4 lines
I am still not completely clear on what you had in mind. The one method I thought about that might help in this is to have Xen-SWIOTLB track which memory ranges were exchanged (so
xen_swiotlb_fixupwould save the *buf and the size for each call toxen_create_contiguous_regionin a list orarray).
Quoted 4 lines
When xen_swiotlb_map/xen_swiotlb_map_sg_attrs care called they would consult said array/list to see if the region they retrieved crosses said 2MB chunks. If so.. and here I am unsure of what would be the best way to
proceed.
Quoted 8 lines
We thought we can solve the issue in several ways:
1) Like the previous patch I sent out, we check the DMA region in
xen_swiotlb_map_page()andxen_swiotlb_map_sg_attr(), and if DMA region crosses page boundary, we exchange the memory and copy the content. However it has race condition that when copying the memory content (we introduced two memory copies in the patch), some other code may also visit the page, which may encounter incorrect values.
That's why, after mapping a buffer (or SG list) one has to call the sync functions before looking at data. Any race as described by you is therefore a programming error.
Jan
Quoted 20 lines
Quoted 11 lines
Quoted 1 line · Dongxiao
From: Konrad Rzeszutek Wilk [mailto:[email protected]]
Quoted 3 lines · Dongxiao
Quoted 3 lines
From: Konrad Rzeszutek Wilk [mailto:[email protected]] What if this check was done in the routines that provide the software static buffers and there try to provide a nice DMA contingous
swatch of pages?
Quoted 2 lines
Yes, this approach also came to our mind, which needs to modify the driver
itself.
Quoted 1 line
If so, it requires driver not using such static buffers (e.g., from
kmalloc) to do
Quoted 6 lines
DMA even if the buffer is continuous in native.
I am bit loss here.
Is the issue you found only with drivers that do not use DMA API? Can you perhaps point me to the code that triggered this fix in the first
place?
Quoted 7 lines
Yes, we met this issue on a specific SAS device/driver, and it calls into libata-core code, you can refer to function
ata_dev_read_id()called fromata_dev_reread_id()indrivers/ata/libata-core.c.In the above function, the target buffer is (void *)dev->link->ap->sector_buf, which is 512 bytes static buffer and unfortunately it across the page boundary.
I wonder whether such use of sg_init_one()/sg_set_buf() is correct in the first place. While there aren't any restrictions documented for its use, one clearly can't pass in whatever one wants (a pointer into vmalloc()-ed memory, for instance, won't work afaict).
I didn't go through all other users of it, but quite a few of the uses elsewhere look similarly questionable.
Quoted 11 lines
Quoted 4 lines
I am still not completely clear on what you had in mind. The one method I thought about that might help in this is to have Xen-SWIOTLB track which memory ranges were exchanged (so
xen_swiotlb_fixupwould save the *buf and the size for each call toxen_create_contiguous_regionin a list orarray).
Quoted 4 lines
When xen_swiotlb_map/xen_swiotlb_map_sg_attrs care called they would consult said array/list to see if the region they retrieved crosses said 2MB chunks. If so.. and here I am unsure of what would be the best way to
proceed.
Quoted 8 lines
We thought we can solve the issue in several ways:
1) Like the previous patch I sent out, we check the DMA region in
xen_swiotlb_map_page()andxen_swiotlb_map_sg_attr(), and if DMA region crosses page boundary, we exchange the memory and copy the content. However it has race condition that when copying the memory content (we introduced two memory copies in the patch), some other code may also visit the page, which may encounter incorrect values.
That's why, after mapping a buffer (or SG list) one has to call the sync functions before looking at data. Any race as described by you is therefore a programming error.
Jan
Quoted 28 lines · Jan Beulich
Quoted 20 lines
Quoted 11 lines
Quoted 1 line · Dongxiao
From: Konrad Rzeszutek Wilk [mailto:[email protected]]
Quoted 3 lines · Dongxiao
Quoted 3 lines
From: Konrad Rzeszutek Wilk [mailto:[email protected]] What if this check was done in the routines that provide the software static buffers and there try to provide a nice DMA contingous
swatch of pages?
Quoted 2 lines
Yes, this approach also came to our mind, which needs to modify the driver
itself.
Quoted 1 line
If so, it requires driver not using such static buffers (e.g., from
kmalloc) to do
Quoted 6 lines
DMA even if the buffer is continuous in native.
I am bit loss here.
Is the issue you found only with drivers that do not use DMA API? Can you perhaps point me to the code that triggered this fix in the first
place?
Quoted 7 lines
Yes, we met this issue on a specific SAS device/driver, and it calls into libata-core code, you can refer to function
ata_dev_read_id()called fromata_dev_reread_id()indrivers/ata/libata-core.c.In the above function, the target buffer is (void *)dev->link->ap->sector_buf, which is 512 bytes static buffer and unfortunately it across the page boundary.
Quoted 18 lines
I wonder whether such use of sg_init_one()/sg_set_buf() is correct in the first place. While there aren't any restrictions documented for its use, one clearly can't pass in whatever one wants (a pointer into vmalloc()-ed memory, for instance, won't work afaict).
I didn't go through all other users of it, but quite a few of the uses elsewhere look similarly questionable.
Quoted 10 lines
Quoted 4 lines
I am still not completely clear on what you had in mind. The one method I thought about that might help in this is to have Xen-SWIOTLB track which memory ranges were exchanged (so
xen_swiotlb_fixupwould save the *buf and the size for each call toxen_create_contiguous_regionin a list orarray).
Quoted 4 lines
When xen_swiotlb_map/xen_swiotlb_map_sg_attrs care called they would consult said array/list to see if the region they retrieved crosses said 2MB chunks. If so.. and here I am unsure of what would be the best way to
proceed.
And from finally looking at the code I misunderstood your initial description.
Quoted 10 lines
Quoted 9 lines
We thought we can solve the issue in several ways:
1) Like the previous patch I sent out, we check the DMA region in
xen_swiotlb_map_page()andxen_swiotlb_map_sg_attr(), and if DMA region crosses page boundary, we exchange the memory and copy the content. However it has race condition that when copying the memory content (we introduced two memory copies in the patch), some other code may also visit the page, which may encounter incorrect values.
Quoted 3 lines
That's why, after mapping a buffer (or SG list) one has to call the sync functions before looking at data. Any race as described by you is therefore a programming error.
I am with Jan here. It looks like the libata is depending on the dma_unmap to do this. But it is unclear to me when the ata_qc_complete is actually called to unmap the buffer (and hence sync it).
Quoted 4 lines
Jan
Quoted 28 lines · Jan Beulich
Quoted 20 lines
Quoted 11 lines
Quoted 1 line · Dongxiao
From: Konrad Rzeszutek Wilk [mailto:[email protected]]
Quoted 3 lines · Dongxiao
Quoted 3 lines
From: Konrad Rzeszutek Wilk [mailto:[email protected]] What if this check was done in the routines that provide the software static buffers and there try to provide a nice DMA contingous
swatch of pages?
Quoted 2 lines
Yes, this approach also came to our mind, which needs to modify the driver
itself.
Quoted 1 line
If so, it requires driver not using such static buffers (e.g., from
kmalloc) to do
Quoted 6 lines
DMA even if the buffer is continuous in native.
I am bit loss here.
Is the issue you found only with drivers that do not use DMA API? Can you perhaps point me to the code that triggered this fix in the first
place?
Quoted 7 lines
Yes, we met this issue on a specific SAS device/driver, and it calls into libata-core code, you can refer to function
ata_dev_read_id()called fromata_dev_reread_id()indrivers/ata/libata-core.c.In the above function, the target buffer is (void *)dev->link->ap->sector_buf, which is 512 bytes static buffer and unfortunately it across the page boundary.
Quoted 18 lines
I wonder whether such use of sg_init_one()/sg_set_buf() is correct in the first place. While there aren't any restrictions documented for its use, one clearly can't pass in whatever one wants (a pointer into vmalloc()-ed memory, for instance, won't work afaict).
I didn't go through all other users of it, but quite a few of the uses elsewhere look similarly questionable.
Quoted 10 lines
Quoted 4 lines
I am still not completely clear on what you had in mind. The one method I thought about that might help in this is to have Xen-SWIOTLB track which memory ranges were exchanged (so
xen_swiotlb_fixupwould save the *buf and the size for each call toxen_create_contiguous_regionin a list orarray).
Quoted 4 lines
When xen_swiotlb_map/xen_swiotlb_map_sg_attrs care called they would consult said array/list to see if the region they retrieved crosses said 2MB chunks. If so.. and here I am unsure of what would be the best way to
proceed.
And from finally looking at the code I misunderstood your initial description.
Quoted 10 lines
Quoted 9 lines
We thought we can solve the issue in several ways:
1) Like the previous patch I sent out, we check the DMA region in
xen_swiotlb_map_page()andxen_swiotlb_map_sg_attr(), and if DMA region crosses page boundary, we exchange the memory and copy the content. However it has race condition that when copying the memory content (we introduced two memory copies in the patch), some other code may also visit the page, which may encounter incorrect values.
Quoted 3 lines
That's why, after mapping a buffer (or SG list) one has to call the sync functions before looking at data. Any race as described by you is therefore a programming error.
I am with Jan here. It looks like the libata is depending on the dma_unmap to do this. But it is unclear to me when the ata_qc_complete is actually called to unmap the buffer (and hence sync it).
Quoted 4 lines
Jan
Quoted 75 lines
-----Original Message-----
From: Konrad Rzeszutek Wilk [mailto:[email protected]]
Sent: Thursday, December 20, 2012 4:09 AM
To: Jan Beulich
Cc: Xu, Dongxiao; [email protected]; [email protected]
Subject: Re: [Xen-devel] [PATCH] xen/swiotlb: Exchange to contiguous memory formap_sghookQuoted 33 lines · Jan Beulich
Quoted 24 lines
Quoted 7 lines
Quoted 1 line · Dongxiao
From: Konrad Rzeszutek Wilk [mailto:[email protected]] On Tue,
Quoted 4 lines · Dongxiao
Quoted 4 lines
From: Konrad Rzeszutek Wilk [mailto:[email protected]] What if this check was done in the routines that provide the software static buffers and there try to provide a nice DMA contingous
Quoted 7 lines
swatch of pages?
Quoted 3 lines
Yes, this approach also came to our mind, which needs to modify the driver
itself.
Quoted 2 lines
If so, it requires driver not using such static buffers (e.g., from
kmalloc) to do
Quoted 7 lines
DMA even if the buffer is continuous in native.
I am bit loss here.
Is the issue you found only with drivers that do not use DMA API? Can you perhaps point me to the code that triggered this fix in the first
place?
Quoted 8 lines
Yes, we met this issue on a specific SAS device/driver, and it calls into libata-core code, you can refer to function
ata_dev_read_id()called fromata_dev_reread_id()indrivers/ata/libata-core.c.In the above function, the target buffer is (void *)dev->link->ap->sector_buf, which is 512 bytes static buffer and unfortunately it across the page boundary.
Quoted 20 lines
I wonder whether such use of sg_init_one()/sg_set_buf() is correct in the first place. While there aren't any restrictions documented for its use, one clearly can't pass in whatever one wants (a pointer into vmalloc()-ed memory, for instance, won't work afaict).
I didn't go through all other users of it, but quite a few of the uses elsewhere look similarly questionable.
Quoted 12 lines
Quoted 5 lines
I am still not completely clear on what you had in mind. The one method I thought about that might help in this is to have Xen-SWIOTLB track which memory ranges were exchanged (so
xen_swiotlb_fixupwould save the *buf and the size for each call toxen_create_contiguous_regionin a list orarray).
Quoted 5 lines
When xen_swiotlb_map/xen_swiotlb_map_sg_attrs care called they would consult said array/list to see if the region they retrieved crosses said 2MB chunks. If so.. and here I am unsure of what would be the best way to
proceed.
And from finally looking at the code I misunderstood your initial description.
Quoted 10 lines
Quoted 9 lines
We thought we can solve the issue in several ways:
1) Like the previous patch I sent out, we check the DMA region in
xen_swiotlb_map_page()andxen_swiotlb_map_sg_attr(), and if DMA region crosses page boundary, we exchange the memory and copy the content. However it has race condition that when copying the memory content (we introduced two memory copies in the patch), some other code may also visit the page, which may encounter incorrect values.
Quoted 7 lines
Quoted 3 lines
That's why, after mapping a buffer (or SG list) one has to call the sync functions before looking at data. Any race as described by you is therefore a programming error.
I am with Jan here. It looks like the libata is depending on the
dma_unmapto do this. But it is unclear to me when theata_qc_completeis actually called to unmap the buffer (and hence sync it).
Sorry, maybe I am still not describing this issue clearly.
Take the libata case as an example, the static DMA buffer locates (dev->link->ap->sector_buf , here we use Data Structure B in the graph) in the following structure:
-------------------------------------Page boundary <Data Structure A>
<Data Structure B> -------------------------------------Page boundary <Data Structure B (cross page)>
<Data Structure C> -------------------------------------Page boundary
Where Structure B is our DMA target.
For Data Structure B, we didn't care about the simultaneous access, either lock or sync function will take care of it.
What we are not sure is "read/write of A and C from other processor". As we will have memory copy for the pages, and at the same time, other CPU may access A/C.
Thanks,
Dongxiao
Quoted 5 lines
Quoted 5 lines
Jan
Quoted 75 lines
-----Original Message-----
From: Konrad Rzeszutek Wilk [mailto:[email protected]]
Sent: Thursday, December 20, 2012 4:09 AM
To: Jan Beulich
Cc: Xu, Dongxiao; [email protected]; [email protected]
Subject: Re: [Xen-devel] [PATCH] xen/swiotlb: Exchange to contiguous memory formap_sghookQuoted 33 lines · Jan Beulich
Quoted 24 lines
Quoted 7 lines
Quoted 1 line · Dongxiao
From: Konrad Rzeszutek Wilk [mailto:[email protected]] On Tue,
Quoted 4 lines · Dongxiao
Quoted 4 lines
From: Konrad Rzeszutek Wilk [mailto:[email protected]] What if this check was done in the routines that provide the software static buffers and there try to provide a nice DMA contingous
Quoted 7 lines
swatch of pages?
Quoted 3 lines
Yes, this approach also came to our mind, which needs to modify the driver
itself.
Quoted 2 lines
If so, it requires driver not using such static buffers (e.g., from
kmalloc) to do
Quoted 7 lines
DMA even if the buffer is continuous in native.
I am bit loss here.
Is the issue you found only with drivers that do not use DMA API? Can you perhaps point me to the code that triggered this fix in the first
place?
Quoted 8 lines
Yes, we met this issue on a specific SAS device/driver, and it calls into libata-core code, you can refer to function
ata_dev_read_id()called fromata_dev_reread_id()indrivers/ata/libata-core.c.In the above function, the target buffer is (void *)dev->link->ap->sector_buf, which is 512 bytes static buffer and unfortunately it across the page boundary.
Quoted 20 lines
I wonder whether such use of sg_init_one()/sg_set_buf() is correct in the first place. While there aren't any restrictions documented for its use, one clearly can't pass in whatever one wants (a pointer into vmalloc()-ed memory, for instance, won't work afaict).
I didn't go through all other users of it, but quite a few of the uses elsewhere look similarly questionable.
Quoted 12 lines
Quoted 5 lines
I am still not completely clear on what you had in mind. The one method I thought about that might help in this is to have Xen-SWIOTLB track which memory ranges were exchanged (so
xen_swiotlb_fixupwould save the *buf and the size for each call toxen_create_contiguous_regionin a list orarray).
Quoted 5 lines
When xen_swiotlb_map/xen_swiotlb_map_sg_attrs care called they would consult said array/list to see if the region they retrieved crosses said 2MB chunks. If so.. and here I am unsure of what would be the best way to
proceed.
And from finally looking at the code I misunderstood your initial description.
Quoted 10 lines
Quoted 9 lines
We thought we can solve the issue in several ways:
1) Like the previous patch I sent out, we check the DMA region in
xen_swiotlb_map_page()andxen_swiotlb_map_sg_attr(), and if DMA region crosses page boundary, we exchange the memory and copy the content. However it has race condition that when copying the memory content (we introduced two memory copies in the patch), some other code may also visit the page, which may encounter incorrect values.
Quoted 7 lines
Quoted 3 lines
That's why, after mapping a buffer (or SG list) one has to call the sync functions before looking at data. Any race as described by you is therefore a programming error.
I am with Jan here. It looks like the libata is depending on the
dma_unmapto do this. But it is unclear to me when theata_qc_completeis actually called to unmap the buffer (and hence sync it).
Sorry, maybe I am still not describing this issue clearly.
Take the libata case as an example, the static DMA buffer locates (dev->link->ap->sector_buf , here we use Data Structure B in the graph) in the following structure:
-------------------------------------Page boundary <Data Structure A>
<Data Structure B> -------------------------------------Page boundary <Data Structure B (cross page)>
<Data Structure C> -------------------------------------Page boundary
Where Structure B is our DMA target.
For Data Structure B, we didn't care about the simultaneous access, either lock or sync function will take care of it.
What we are not sure is "read/write of A and C from other processor". As we will have memory copy for the pages, and at the same time, other CPU may access A/C.
Thanks,
Dongxiao
Quoted 5 lines
Quoted 5 lines
Jan
Quoted 2 lines
Quoted 1 line
Quoted 1 line · Dongxiao
Sorry, maybe I am still not describing this issue clearly.
No, at least I understood you the way you re-describe below.
Quoted 12 lines
Take the libata case as an example, the static DMA buffer locates (
dev->link->ap->sector_buf, here we use Data Structure B in the graph) in the following structure:-------------------------------------Page boundary <Data Structure A>
<Data Structure B> -------------------------------------Page boundary <Data Structure B (cross page)>
<Data Structure C> -------------------------------------Page boundary
Quoted 7 lines
Where Structure B is our DMA target.
For Data Structure B, we didn't care about the simultaneous access, either lock or sync function will take care of it.
What we are not sure is "read/write of A and C from other processor". As we will have memory copy for the pages, and at the same time, other CPU may access A/C.
The question is whether what libata does here is valid in the first place - fill an SG list entry with something that crosses a page boundary and is not a compound page.
Jan
Quoted 2 lines
Quoted 1 line
Quoted 1 line · Dongxiao
Sorry, maybe I am still not describing this issue clearly.
No, at least I understood you the way you re-describe below.
Quoted 12 lines
Take the libata case as an example, the static DMA buffer locates (
dev->link->ap->sector_buf, here we use Data Structure B in the graph) in the following structure:-------------------------------------Page boundary <Data Structure A>
<Data Structure B> -------------------------------------Page boundary <Data Structure B (cross page)>
<Data Structure C> -------------------------------------Page boundary
Quoted 7 lines
Where Structure B is our DMA target.
For Data Structure B, we didn't care about the simultaneous access, either lock or sync function will take care of it.
What we are not sure is "read/write of A and C from other processor". As we will have memory copy for the pages, and at the same time, other CPU may access A/C.
The question is whether what libata does here is valid in the first place - fill an SG list entry with something that crosses a page boundary and is not a compound page.
Jan
Quoted 34 lines
-----Original Message-----
From: Jan Beulich [mailto:[email protected]]
Sent: Thursday, December 20, 2012 4:56 PM
To: Xu, Dongxiao
Cc: [email protected]; Konrad Rzeszutek Wilk; [email protected]
Subject: RE: [Xen-devel] [PATCH] xen/swiotlb: Exchange to contiguous memory formap_sghookQuoted 2 lines
Quoted 1 line
Quoted 1 line · Dongxiao
Sorry, maybe I am still not describing this issue clearly.
No, at least I understood you the way you re-describe below.
Quoted 12 lines
Take the libata case as an example, the static DMA buffer locates (
dev->link->ap->sector_buf, here we use Data Structure B in the graph) in the following structure:-------------------------------------Page boundary <Data Structure A>
<Data Structure B> -------------------------------------Page boundary <Data Structure B (cross page)>
<Data Structure C> -------------------------------------Page boundaryQuoted 7 lines
Where Structure B is our DMA target.
For Data Structure B, we didn't care about the simultaneous access, either lock or sync function will take care of it.
What we are not sure is "read/write of A and C from other processor". As we will have memory copy for the pages, and at the same time, other CPU may access A/C.
Quoted 3 lines
The question is whether what libata does here is valid in the first place - fill an SG list entry with something that crosses a page boundary and is not a compound page.
Sorry for the late response about this thread.
To make sure I understand you correctly, so do you mean the correct fix should be applied to libata driver, and make sure it DMA from/to correct place (for example, some memory allocated by DMA API), but not such certain field in a static structure?
If we fix it in device driver side, then we may not need to modify the xen-swiotlb part.
Thanks,
Dongxiao
Quoted 2 lines
Jan
Quoted 34 lines
-----Original Message-----
From: Jan Beulich [mailto:[email protected]]
Sent: Thursday, December 20, 2012 4:56 PM
To: Xu, Dongxiao
Cc: [email protected]; Konrad Rzeszutek Wilk; [email protected]
Subject: RE: [Xen-devel] [PATCH] xen/swiotlb: Exchange to contiguous memory formap_sghookQuoted 2 lines
Quoted 1 line
Quoted 1 line · Dongxiao
Sorry, maybe I am still not describing this issue clearly.
No, at least I understood you the way you re-describe below.
Quoted 12 lines
Take the libata case as an example, the static DMA buffer locates (
dev->link->ap->sector_buf, here we use Data Structure B in the graph) in the following structure:-------------------------------------Page boundary <Data Structure A>
<Data Structure B> -------------------------------------Page boundary <Data Structure B (cross page)>
<Data Structure C> -------------------------------------Page boundaryQuoted 7 lines
Where Structure B is our DMA target.
For Data Structure B, we didn't care about the simultaneous access, either lock or sync function will take care of it.
What we are not sure is "read/write of A and C from other processor". As we will have memory copy for the pages, and at the same time, other CPU may access A/C.
Quoted 3 lines
The question is whether what libata does here is valid in the first place - fill an SG list entry with something that crosses a page boundary and is not a compound page.
Sorry for the late response about this thread.
To make sure I understand you correctly, so do you mean the correct fix should be applied to libata driver, and make sure it DMA from/to correct place (for example, some memory allocated by DMA API), but not such certain field in a static structure?
If we fix it in device driver side, then we may not need to modify the xen-swiotlb part.
Thanks,
Dongxiao
Quoted 2 lines
Jan
Quoted 23 lines
Quoted 1 line
Quoted 1 line
"Xu, Dongxiao" <[email protected]> 01/07/13 8:17 AM >>>
From: Jan Beulich [mailto:[email protected]]
Quoted 21 lines
Quoted 20 lines
Quoted 1 line · Dongxiao
Take the libata case as an example, the static DMA buffer locates (
dev->link->ap->sector_buf, here we use Data Structure B in the graph) in the following structure:-------------------------------------Page boundary <Data Structure A>
<Data Structure B> -------------------------------------Page boundary <Data Structure B (cross page)>
<Data Structure C> -------------------------------------Page boundaryWhere Structure B is our DMA target.
For Data Structure B, we didn't care about the simultaneous access, either lock or sync function will take care of it.
What we are not sure is "read/write of A and C from other processor". As we will have memory copy for the pages, and at the same time, other CPU may access A/C.
Quoted 7 lines
Quoted 3 lines
The question is whether what libata does here is valid in the first place - fill an SG list entry with something that crosses a page boundary and is not a compound page.
To make sure I understand you correctly, so do you mean the correct fix should be applied to libata driver, and make sure it DMA from/to correct place (for example, some memory allocated by DMA API), but not such certain field in a static structure?
Yes.
Jan
Quoted 23 lines
Quoted 1 line
Quoted 1 line
"Xu, Dongxiao" <[email protected]> 01/07/13 8:17 AM >>>
From: Jan Beulich [mailto:[email protected]]
Quoted 21 lines
Quoted 20 lines
Quoted 1 line · Dongxiao
Take the libata case as an example, the static DMA buffer locates (
dev->link->ap->sector_buf, here we use Data Structure B in the graph) in the following structure:-------------------------------------Page boundary <Data Structure A>
<Data Structure B> -------------------------------------Page boundary <Data Structure B (cross page)>
<Data Structure C> -------------------------------------Page boundaryWhere Structure B is our DMA target.
For Data Structure B, we didn't care about the simultaneous access, either lock or sync function will take care of it.
What we are not sure is "read/write of A and C from other processor". As we will have memory copy for the pages, and at the same time, other CPU may access A/C.
Quoted 7 lines
Quoted 3 lines
The question is whether what libata does here is valid in the first place - fill an SG list entry with something that crosses a page boundary and is not a compound page.
To make sure I understand you correctly, so do you mean the correct fix should be applied to libata driver, and make sure it DMA from/to correct place (for example, some memory allocated by DMA API), but not such certain field in a static structure?
Yes.
Jan
Quoted 34 lines · Dongxiao
Quoted 34 lines
-----Original Message-----
From: Jan Beulich [mailto:[email protected]]
Sent: Thursday, December 20, 2012 4:56 PM
To: Xu, Dongxiao
Cc: [email protected]; Konrad Rzeszutek Wilk; [email protected]
Subject: RE: [Xen-devel] [PATCH] xen/swiotlb: Exchange to contiguous memory formap_sghookQuoted 2 lines
Quoted 1 line
Quoted 1 line · Dongxiao
Sorry, maybe I am still not describing this issue clearly.
No, at least I understood you the way you re-describe below.
Quoted 19 lines
Take the libata case as an example, the static DMA buffer locates (
dev->link->ap->sector_buf, here we use Data Structure B in the graph) in the following structure:-------------------------------------Page boundary <Data Structure A>
<Data Structure B> -------------------------------------Page boundary <Data Structure B (cross page)>
<Data Structure C> -------------------------------------Page boundaryWhere Structure B is our DMA target.
For Data Structure B, we didn't care about the simultaneous access, either lock or sync function will take care of it.
What we are not sure is "read/write of A and C from other processor". As we will have memory copy for the pages, and at the same time, other CPU may access A/C.
Quoted 7 lines
Quoted 3 lines
The question is whether what libata does here is valid in the first place - fill an SG list entry with something that crosses a page boundary and is not a compound page.
Sorry for the late response about this thread.
To make sure I understand you correctly, so do you mean the correct fix should be applied to libata driver, and make sure it DMA from/to correct place (for example, some memory allocated by DMA API), but not such certain field in a static structure?
Or with baremetal swiotlb if the user booted with 'swiotlb=force'.
Quoted 3 lines
If we fix it in device driver side, then we may not need to modify the xen-swiotlb part.
Correct. This is a bug in the device driver where it checks the contents of its buffer _before_ doing an DMA unmap or DMA sync.
Quoted 34 lines · Dongxiao
Quoted 34 lines
-----Original Message-----
From: Jan Beulich [mailto:[email protected]]
Sent: Thursday, December 20, 2012 4:56 PM
To: Xu, Dongxiao
Cc: [email protected]; Konrad Rzeszutek Wilk; [email protected]
Subject: RE: [Xen-devel] [PATCH] xen/swiotlb: Exchange to contiguous memory formap_sghookQuoted 2 lines
Quoted 1 line
Quoted 1 line · Dongxiao
Sorry, maybe I am still not describing this issue clearly.
No, at least I understood you the way you re-describe below.
Quoted 19 lines
Take the libata case as an example, the static DMA buffer locates (
dev->link->ap->sector_buf, here we use Data Structure B in the graph) in the following structure:-------------------------------------Page boundary <Data Structure A>
<Data Structure B> -------------------------------------Page boundary <Data Structure B (cross page)>
<Data Structure C> -------------------------------------Page boundaryWhere Structure B is our DMA target.
For Data Structure B, we didn't care about the simultaneous access, either lock or sync function will take care of it.
What we are not sure is "read/write of A and C from other processor". As we will have memory copy for the pages, and at the same time, other CPU may access A/C.
Quoted 7 lines
Quoted 3 lines
The question is whether what libata does here is valid in the first place - fill an SG list entry with something that crosses a page boundary and is not a compound page.
Sorry for the late response about this thread.
To make sure I understand you correctly, so do you mean the correct fix should be applied to libata driver, and make sure it DMA from/to correct place (for example, some memory allocated by DMA API), but not such certain field in a static structure?
Or with baremetal swiotlb if the user booted with 'swiotlb=force'.
Quoted 3 lines
If we fix it in device driver side, then we may not need to modify the xen-swiotlb part.
Correct. This is a bug in the device driver where it checks the contents of its buffer _before_ doing an DMA unmap or DMA sync.
Quoted 38 lines · Dongxiao
Quoted 38 lines
-----Original Message-----
From: Konrad Rzeszutek Wilk [mailto:[email protected]]
Sent: Wednesday, December 12, 2012 1:07 AM
To: Xu, Dongxiao
Cc: [email protected]; [email protected]
Subject: Re: [PATCH] xen/swiotlb: Exchange to contiguous memory formap_sghookQuoted 13 lines · Dongxiao
Quoted 13 lines
-----Original Message-----
From: Konrad Rzeszutek Wilk [mailto:[email protected]]
Sent: Friday, December 07, 2012 10:09 PM
To: Xu, Dongxiao
Cc: [email protected]; [email protected]
Subject: Re: [PATCH] xen/swiotlb: Exchange to contiguous memory formap_sghookQuoted 3 lines · Dongxiao Xu
While mapping sg buffers, checking to cross page DMA buffer is also needed. If the guest DMA buffer crosses page boundary, Xen should exchange contiguous memory for it.
Quoted 9 lines
Quoted 3 lines
So this is when we cross those 2MB contingous swatch of buffers. Wouldn't we get the same problem with the '
map_page' call? If the driver tried to map say a 4MB DMA region?Yes, it also needs such check, as I just replied to Jan's mail.
Quoted 3 lines
What if this check was done in the routines that provide the software static buffers and there try to provide a nice DMA contingous
swatch of pages?
Quoted 2 lines
Yes, this approach also came to our mind, which needs to modify the driver
itself.
Quoted 1 line
If so, it requires driver not using such static buffers (e.g., from kmalloc) to do
DMA even if the buffer is continuous in native.
Quoted 8 lines
Quoted 4 lines
I am bit loss here.
Is the issue you found only with drivers that do not use DMA API? Can you perhaps point me to the code that triggered this fix in the first place?
Yes, we met this issue on a specific SAS device/driver, and it calls into libata-core code, you can refer to function
ata_dev_read_id()called fromata_dev_reread_id()indrivers/ata/libata-core.c.In the above function, the target buffer is (void *)dev->link->ap->sector_buf, which is 512 bytes static buffer and unfortunately it across the page boundary.
Hm, that looks like a DMA API violation. If you ran the code with CONFIG_DEBUG_DMA_API did it complain about this? I recall the floppy driver doing something similar and the Debug DMA API was quite verbose in pointing this out.
Quoted 22 lines
Quoted 11 lines
Quoted 2 lines
Is this acceptable by kernel/driver upstream?
I am still not completely clear on what you had in mind. The one method I thought about that might help in this is to have Xen-SWIOTLB track which memory ranges were exchanged (so
xen_swiotlb_fixupwould save the *buf and the size for each call toxen_create_contiguous_regionin a list or array).When xen_swiotlb_map/xen_swiotlb_map_sg_attrs care called they would consult said array/list to see if the region they retrieved crosses said 2MB chunks. If so.. and here I am unsure of what would be the best way to proceed.
We thought we can solve the issue in several ways:
1) Like the previous patch I sent out, we check the DMA region in
xen_swiotlb_map_page()andxen_swiotlb_map_sg_attr(), and if DMA region crosses page boundary, we exchange the memory and copy the content. However it has race condition that when copying the memory content (we introduced two memory copies in the patch), some other code may also visit the page, which may encounter incorrect values.
2) Mostly the same as item 1), the only difference is that we put the memory content copy inside Xen hypervisor but not in Dom0. This requires we add certain flags to indicating memory moving in theXENMEM_exchangehypercall.
3) As you also mentioned, this is not a common case, it is only triggered by some specific devices/drivers. we can fix it in certain driver to avoid DMA into static buffers. Like (void *)dev->link->ap->sector_buf in the above case. But I am not sure whether it is acceptable by kernel/driver upstream.Thanks,
Dongxiao
Quoted 38 lines · Dongxiao
Quoted 38 lines
-----Original Message-----
From: Konrad Rzeszutek Wilk [mailto:[email protected]]
Sent: Wednesday, December 12, 2012 1:07 AM
To: Xu, Dongxiao
Cc: [email protected]; [email protected]
Subject: Re: [PATCH] xen/swiotlb: Exchange to contiguous memory formap_sghookQuoted 13 lines · Dongxiao
Quoted 13 lines
-----Original Message-----
From: Konrad Rzeszutek Wilk [mailto:[email protected]]
Sent: Friday, December 07, 2012 10:09 PM
To: Xu, Dongxiao
Cc: [email protected]; [email protected]
Subject: Re: [PATCH] xen/swiotlb: Exchange to contiguous memory formap_sghookQuoted 3 lines · Dongxiao Xu
While mapping sg buffers, checking to cross page DMA buffer is also needed. If the guest DMA buffer crosses page boundary, Xen should exchange contiguous memory for it.
Quoted 9 lines
Quoted 3 lines
So this is when we cross those 2MB contingous swatch of buffers. Wouldn't we get the same problem with the '
map_page' call? If the driver tried to map say a 4MB DMA region?Yes, it also needs such check, as I just replied to Jan's mail.
Quoted 3 lines
What if this check was done in the routines that provide the software static buffers and there try to provide a nice DMA contingous
swatch of pages?
Quoted 2 lines
Yes, this approach also came to our mind, which needs to modify the driver
itself.
Quoted 1 line
If so, it requires driver not using such static buffers (e.g., from kmalloc) to do
DMA even if the buffer is continuous in native.
Quoted 8 lines
Quoted 4 lines
I am bit loss here.
Is the issue you found only with drivers that do not use DMA API? Can you perhaps point me to the code that triggered this fix in the first place?
Yes, we met this issue on a specific SAS device/driver, and it calls into libata-core code, you can refer to function
ata_dev_read_id()called fromata_dev_reread_id()indrivers/ata/libata-core.c.In the above function, the target buffer is (void *)dev->link->ap->sector_buf, which is 512 bytes static buffer and unfortunately it across the page boundary.
Hm, that looks like a DMA API violation. If you ran the code with CONFIG_DEBUG_DMA_API did it complain about this? I recall the floppy driver doing something similar and the Debug DMA API was quite verbose in pointing this out.
Quoted 22 lines
Quoted 11 lines
Quoted 2 lines
Is this acceptable by kernel/driver upstream?
I am still not completely clear on what you had in mind. The one method I thought about that might help in this is to have Xen-SWIOTLB track which memory ranges were exchanged (so
xen_swiotlb_fixupwould save the *buf and the size for each call toxen_create_contiguous_regionin a list or array).When xen_swiotlb_map/xen_swiotlb_map_sg_attrs care called they would consult said array/list to see if the region they retrieved crosses said 2MB chunks. If so.. and here I am unsure of what would be the best way to proceed.
We thought we can solve the issue in several ways:
1) Like the previous patch I sent out, we check the DMA region in
xen_swiotlb_map_page()andxen_swiotlb_map_sg_attr(), and if DMA region crosses page boundary, we exchange the memory and copy the content. However it has race condition that when copying the memory content (we introduced two memory copies in the patch), some other code may also visit the page, which may encounter incorrect values.
2) Mostly the same as item 1), the only difference is that we put the memory content copy inside Xen hypervisor but not in Dom0. This requires we add certain flags to indicating memory moving in theXENMEM_exchangehypercall.
3) As you also mentioned, this is not a common case, it is only triggered by some specific devices/drivers. we can fix it in certain driver to avoid DMA into static buffers. Like (void *)dev->link->ap->sector_buf in the above case. But I am not sure whether it is acceptable by kernel/driver upstream.Thanks,
Dongxiao