passthrough/vtd: Don't DMA to the stack in queue_invalidate_wait()
PATCH 1/1 passthrough/vtd: Don't DMA to the stack in queue_invalidate_wait()
DMA-ing to the stack is generally considered bad practice. In this case, if a timeout occurs because of a sluggish device which is processing the request, the completion notification will corrupt the stack of a subsequent deeper call tree.
Place the poll_slot in a percpu area and DMA to that instead.
---
CC: Jan Beulich <[email protected]>
CC: Kevin Tian <[email protected]>
CC: Julien Grall <[email protected]>
Julien: This wants backporting to all releases, and therefore should be considered for 4.10 at this point.
--- xen/drivers/passthrough/vtd/qinval.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
xen/drivers/passthrough/vtd/qinval.c +4 -3
@@ -147,7 +147,8 @@ static int __must_check queue_invalidate_wait(struct iommu *iommu, u8 iflag, u8 sw, u8 fn, bool_t flush_dev_iotlb) { - volatile u32 poll_slot = QINVAL_STAT_INIT; + static DEFINE_PER_CPU(u32, poll_slot); +volatile u32 *this_poll_slot = &this_cpu (poll_slot);unsigned int index;unsigned long flags;u64 entry_base;
@@ -167,7 +168,7 @@ static int __must_check queue_invalidate_wait(struct iommu *iommu, qinval_entry->q.inv_wait_dsc.lo.res_1 =0 ; qinval_entry->q.inv_wait_dsc.lo.sdata =QINVAL_STAT_DONE ; qinval_entry->q.inv_wait_dsc.hi.res_1 =0 ; - qinval_entry->q.inv_wait_dsc.hi.saddr =virt_to_maddr (&poll_slot) >>2 ; + qinval_entry->q.inv_wait_dsc.hi.saddr =virt_to_maddr (this_poll_slot) >>2 ;unmap_vtd_domain_page (qinval_entries);qinval_update_qtail (iommu, index);
@@ -182,7 +183,7 @@ static int __must_check queue_invalidate_wait(struct iommu *iommu, timeout =NOW () +MILLISECS (flush_dev_iotlb ? iommu_dev_iotlb_timeout :VTD_QI_TIMEOUT ); -while ( poll_slot !=QINVAL_STAT_DONE ) +while ( *this_poll_slot !=QINVAL_STAT_DONE ) {if (NOW () > timeout ) {
Signature · 7 lines
2.1.4 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
xen/drivers/passthrough/vtd/qinval.c static int __must_check queue_invalidate_wait(struct iommu *iommu, · line 150
Quoted 8 lines
Quoted 1 line
Quoted 1 line · On 19.10.17 at 13:26, <[email protected]> wrote:
xen/drivers/passthrough/vtd/qinval.c +0
-1@@ -147,7 +147,8 @@ static int __must_check queue_invalidate_wait(struct iommu *iommu, u8 iflag, u8 sw, u8 fn, bool_t flush_dev_iotlb) { - volatile u32 poll_slot = QINVAL_STAT_INIT;
You've lost the initializer.
xen/drivers/passthrough/vtd/qinval.c static int __must_check queue_invalidate_wait(struct iommu *iommu, · line 150
Quoted 1 line
xen/drivers/passthrough/vtd/qinval.c +1
-0+ static DEFINE_PER_CPU(u32, poll_slot);
volatile u32
xen/drivers/passthrough/vtd/qinval.c static int __must_check queue_invalidate_wait(struct iommu *iommu, · line 189
Quoted 9 lines
xen/drivers/passthrough/vtd/qinval.c +1
-1@@ -182,7 +183,7 @@ static int __must_check queue_invalidate_wait(struct iommu *iommu, timeout =NOW () +MILLISECS (flush_dev_iotlb ? iommu_dev_iotlb_timeout :VTD_QI_TIMEOUT ); -while ( poll_slot !=QINVAL_STAT_DONE ) +while ( *this_poll_slot !=QINVAL_STAT_DONE ) {if (NOW () > timeout ) {
Okay, you indeed improve the situation. But is that improvement enough? I.e. what if the write of a first (timed out) request happens while waiting for a subsequent one? Don't you need distinct addresses for every possible slot? Or alternatively isn't it high time for the interrupt approach to be made work (perhaps not by you, but rather by Intel folks)?
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Quoted 9 lines · Jan Beulich
xen/drivers/passthrough/vtd/qinval.c static int __must_check queue_invalidate_wait(struct iommu *iommu, · line 150
Quoted 8 lines
Quoted 1 line
Quoted 1 line · On 19.10.17 at 13:26, <[email protected]> wrote:
xen/drivers/passthrough/vtd/qinval.c +0
-1@@ -147,7 +147,8 @@ static int __must_check queue_invalidate_wait(struct iommu *iommu, u8 iflag, u8 sw, u8 fn, bool_t flush_dev_iotlb) { - volatile u32 poll_slot = QINVAL_STAT_INIT;You've lost the initializer.
Deliberately so.
Quoted 3 lines
xen/drivers/passthrough/vtd/qinval.c static int __must_check queue_invalidate_wait(struct iommu *iommu, · line 150
Quoted 2 lines
xen/drivers/passthrough/vtd/qinval.c +1
-0+ static DEFINE_PER_CPU(u32, poll_slot);volatile u32
You've clipped out the bit declaring the pointer as volatile, which suffices to retain the previous properties.
Quoted 5 lines
Quoted 5 lines
xen/drivers/passthrough/vtd/qinval.c +0
-0@@ -182,7 +183,7 @@ static int __must_check queue_invalidate_wait(struct iommu *iommu, timeout =NOW () +MILLISECS (flush_dev_iotlb ? iommu_dev_iotlb_timeout :VTD_QI_TIMEOUT );
Quoted 7 lines
xen/drivers/passthrough/vtd/qinval.c static int __must_check queue_invalidate_wait(struct iommu *iommu, · line 189
Quoted 5 lines
xen/drivers/passthrough/vtd/qinval.c +1
-1-while ( poll_slot !=QINVAL_STAT_DONE ) +while ( *this_poll_slot !=QINVAL_STAT_DONE ) {if (NOW () > timeout ) {Okay, you indeed improve the situation. But is that improvement enough?
For not corrupting the stack, yes.
Quoted 3 lines
I.e. what if the write of a first (timed out) request happens while waiting for a subsequent one? Don't you need distinct addresses for every possible slot?
Certainly everything which is currently pending.
Quoted 3 lines
Or alternatively isn't it high time for the
interrupt approach to be made work (perhaps not by you, but rather by Intel folks)?
I'm not going to pretend that the current implementation is great, but I really don't have time to address the other remaining swamps here.
~Andrew
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Quoted 12 lines
Quoted 1 line
Quoted 1 line · On 19.10.17 at 14:54, <[email protected]> wrote:
Quoted 9 lines · Jan Beulich
xen/drivers/passthrough/vtd/qinval.c static int __must_check queue_invalidate_wait(struct iommu *iommu, · line 150
Quoted 8 lines
Quoted 1 line
Quoted 1 line · On 19.10.17 at 13:26, <[email protected]> wrote:
xen/drivers/passthrough/vtd/qinval.c +0
-1@@ -147,7 +147,8 @@ static int __must_check queue_invalidate_wait(struct iommu *iommu, u8 iflag, u8 sw, u8 fn, bool_t flush_dev_iotlb) { - volatile u32 poll_slot = QINVAL_STAT_INIT;You've lost the initializer.
Quoted 1 line
Deliberately so.
I don't understand: By never writing QINVAL_STAT_INIT, how can multiple waits work? Afaict you'll find the variable set to QINVAL_STAT_DONE the 2nd time you come here, and hence you won't wait at all.
Quoted 5 lines
Quoted 2 lines
xen/drivers/passthrough/vtd/qinval.c static int __must_check queue_invalidate_wait(struct iommu *iommu, · line 150
Quoted 1 line
xen/drivers/passthrough/vtd/qinval.c +1
-0+ static DEFINE_PER_CPU(u32, poll_slot);volatile u32
You've clipped out the bit declaring the pointer as volatile, which suffices to retain the previous properties.
Still the variable itself would better also be declared volatile.
Quoted 6 lines
Quoted 3 lines
Or alternatively isn't it high time for the
interrupt approach to be made work (perhaps not by you, but rather by Intel folks)?I'm not going to pretend that the current implementation is great, but I really don't have time to address the other remaining swamps here.
Right, hence my hint at this really being something the maintainer(s) should look after.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Quoted 11 lines · Jan Beulich
Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 19.10.17 at 14:54, <[email protected]> wrote:
Quoted 9 lines · Jan Beulich
xen/drivers/passthrough/vtd/qinval.c static int __must_check queue_invalidate_wait(struct iommu *iommu, · line 150
Quoted 8 lines
Quoted 1 line
Quoted 1 line · On 19.10.17 at 13:26, <[email protected]> wrote:
xen/drivers/passthrough/vtd/qinval.c +0
-1@@ -147,7 +147,8 @@ static int __must_check queue_invalidate_wait(struct iommu *iommu, u8 iflag, u8 sw, u8 fn, bool_t flush_dev_iotlb) { - volatile u32 poll_slot = QINVAL_STAT_INIT;You've lost the initializer.
Quoted 5 lines
Quoted 1 line
Deliberately so.
I don't understand: By never writing
QINVAL_STAT_INIT, how can multiple waits work? Afaict you'll find the variable set toQINVAL_STAT_DONEthe 2nd time you come here, and hence you won't wait at all.
Oh yes - you are quite correct.
My test box, which does end up doing frequent queued invalidations, doesn't appear to suffer any bad side effects from this quite-clearly-wrong code.
~Andrew
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
PATCH v2 1/1 passthrough/vtd: Don't DMA to the stack in queue_invalidate_wait()
In reply to Andrew CooperDMA-ing to the stack is generally considered bad practice. In this case, if a timeout occurs because of a sluggish device which is processing the request, the completion notification will corrupt the stack of a subsequent deeper call tree.
Place the poll_slot in a percpu area and DMA to that instead.
---
CC: Jan Beulich <[email protected]>
CC: Kevin Tian <[email protected]>
CC: Julien Grall <[email protected]>
Julien: This wants backporting to all releases, and therefore should be considered for 4.10 at this point.
v2:
* Retain volatile declaration for poll_slot.
* Initialise poll_slot to QINVAL_STAT_INIT on each call.
--- xen/drivers/passthrough/vtd/qinval.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)
xen/drivers/passthrough/vtd/qinval.c +5 -3
@@ -147,13 +147,15 @@ static int __must_check queue_invalidate_wait(struct iommu *iommu, u8 iflag, u8 sw, u8 fn, bool_t flush_dev_iotlb) { - volatile u32 poll_slot = QINVAL_STAT_INIT; + static DEFINE_PER_CPU(volatile u32, poll_slot);unsigned int index;unsigned long flags;u64 entry_base;struct qinval_entry *qinval_entry, *qinval_entries; +volatile u32 *this_poll_slot = &this_cpu (poll_slot);spin_lock_irqsave (&iommu->register_lock, flags); + *this_poll_slot =QINVAL_STAT_INIT ; index =qinval_next_index (iommu); entry_base =iommu_qi_ctrl (iommu)->qinval_maddr + ((index >>QINVAL_ENTRY_ORDER ) <<PAGE_SHIFT );
@@ -167,7 +169,7 @@ static int __must_check queue_invalidate_wait(struct iommu *iommu, qinval_entry->q.inv_wait_dsc.lo.res_1 =0 ; qinval_entry->q.inv_wait_dsc.lo.sdata =QINVAL_STAT_DONE ; qinval_entry->q.inv_wait_dsc.hi.res_1 =0 ; - qinval_entry->q.inv_wait_dsc.hi.saddr =virt_to_maddr (&poll_slot) >>2 ; + qinval_entry->q.inv_wait_dsc.hi.saddr =virt_to_maddr (this_poll_slot) >>2 ;unmap_vtd_domain_page (qinval_entries);qinval_update_qtail (iommu, index);
@@ -182,7 +184,7 @@ static int __must_check queue_invalidate_wait(struct iommu *iommu, timeout =NOW () +MILLISECS (flush_dev_iotlb ? iommu_dev_iotlb_timeout :VTD_QI_TIMEOUT ); -while ( poll_slot !=QINVAL_STAT_DONE ) +while ( *this_poll_slot !=QINVAL_STAT_DONE ) {if (NOW () > timeout ) {
Signature · 7 lines
2.1.4 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 19.10.17 at 18:22, <[email protected]> wrote:
DMA-ing to the stack is generally considered bad practice. In this case, if a timeout occurs because of a sluggish device which is processing the request, the completion notification will corrupt the stack of a subsequent deeper call tree.
Place the
poll_slotin a percpu area and DMA to that instead.Signed-off-by Andrew Cooper <[email protected]>
Please could you extend the commit message to state the issue remaining with using a single per-CPU slot? With that
albeit ...
xen/drivers/passthrough/vtd/qinval.c static int __must_check queue_invalidate_wait(struct iommu *iommu, · line 172
Quoted 6 lines
xen/drivers/passthrough/vtd/qinval.c +1
-1@@ -167,7 +169,7 @@ static int __must_check queue_invalidate_wait(struct iommu *iommu, qinval_entry->q.inv_wait_dsc.lo.res_1 =0 ; qinval_entry->q.inv_wait_dsc.lo.sdata =QINVAL_STAT_DONE ; qinval_entry->q.inv_wait_dsc.hi.res_1 =0 ; - qinval_entry->q.inv_wait_dsc.hi.saddr =virt_to_maddr (&poll_slot) >>2 ; + qinval_entry->q.inv_wait_dsc.hi.saddr =virt_to_maddr (this_poll_slot) >>2 ;
... this one is still a literal number rather than something allowing to associate back where that value is coming from (but since you're not introducing it here, I also won't insist on you changing it in this patch).
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Quoted 6 lines · Jan Beulich
Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 19.10.17 at 18:22, <[email protected]> wrote:
DMA-ing to the stack is generally considered bad practice. In this case, if a timeout occurs because of a sluggish device which is processing the request, the completion notification will corrupt the stack of a subsequent deeper call tree.
Quoted 6 lines
Quoted 3 lines
Place the
poll_slotin a percpu area and DMA to that instead.Signed-off-by Andrew Cooper <[email protected]>Please could you extend the commit message to state the issue remaining with using a single per-CPU slot? With that
Reviewed-by Jan Beulich <[email protected]>
How about this?
Note: This change does not address other issues with the current implementation, such as once a timeout has been suffered, subsequent completions can't be correlated with their requests.
Quoted 12 lines
albeit ...
xen/drivers/passthrough/vtd/qinval.c static int __must_check queue_invalidate_wait(struct iommu *iommu, · line 172
Quoted 6 lines
xen/drivers/passthrough/vtd/qinval.c +1
-1@@ -167,7 +169,7 @@ static int __must_check queue_invalidate_wait(struct iommu *iommu, qinval_entry->q.inv_wait_dsc.lo.res_1 =0 ; qinval_entry->q.inv_wait_dsc.lo.sdata =QINVAL_STAT_DONE ; qinval_entry->q.inv_wait_dsc.hi.res_1 =0 ; - qinval_entry->q.inv_wait_dsc.hi.saddr =virt_to_maddr (&poll_slot) >>2 ; + qinval_entry->q.inv_wait_dsc.hi.saddr =virt_to_maddr (this_poll_slot) >>2 ;... this one is still a literal number rather than something allowing to associate back where that value is coming from (but since you're not introducing it here, I also won't insist on you changing it in this patch).
I don't understand. What is still a literal number?
The sdata field is software-chosen, so one option would be to count the number of wait descriptors we've requested of the hardware, and have it echo that back. That would allow us to detect if it is out of date.
~Andrew
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Quoted 20 lines
From: Andrew Cooper [mailto:[email protected]]
Sent: Saturday, October 21, 2017 1:55 AMQuoted 2 lines · Jan Beulich
Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 19.10.17 at 18:22, <[email protected]> wrote:
DMA-ing to the stack is generally considered bad practice. In this case, if
a
Quoted 1 line
Quoted 1 line
timeout occurs because of a sluggish device which is processing the
request,
Quoted 1 line
Quoted 1 line
the completion notification will corrupt the stack of a subsequent
deeper call
Quoted 8 lines
Quoted 5 lines
tree.
Place the
poll_slotin a percpu area and DMA to that instead.Signed-off-by Andrew Cooper <[email protected]>Please could you extend the commit message to state the issue remaining with using a single per-CPU slot? With that
Reviewed-by Jan Beulich <[email protected]>
Quoted 6 lines
How about this?
Note: This change does not address other issues with the current implementation, such as once a timeout has been suffered, subsequent completions can't be correlated with their requests.
Can you increase the poll data +1 every time when a new wait comes?
Thanks
Kevin
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
A message that is not in the archive (sent off-list or not yet received): AADFC41AFE54684AB9EE6CBC0274A5D190E3101C@SHSMSX101.ccr.corp.intel.com
Quoted 26 lines
From: Tian, Kevin
Sent: Monday, October 23, 2017 3:05 PMQuoted 6 lines
From: Andrew Cooper [mailto:[email protected]]
Sent: Saturday, October 21, 2017 1:55 AMQuoted 2 lines · Jan Beulich
Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 19.10.17 at 18:22, <[email protected]> wrote:
DMA-ing to the stack is generally considered bad practice. In this case,
if
Quoted 16 lines
a
Quoted 1 line
Quoted 1 line
timeout occurs because of a sluggish device which is processing the
request,
Quoted 1 line
Quoted 1 line
the completion notification will corrupt the stack of a subsequent
deeper call
Quoted 8 lines
Quoted 5 lines
tree.
Place the
poll_slotin a percpu area and DMA to that instead.Signed-off-by Andrew Cooper <[email protected]>Please could you extend the commit message to state the issue remaining with using a single per-CPU slot? With that
Reviewed-by Jan Beulich <[email protected]>How about this?
Quoted 8 lines
Quoted 4 lines
Note: This change does not address other issues with the current implementation, such as once a timeout has been suffered, subsequent completions can't be correlated with their requests.
Can you increase the poll data +1 every time when a new wait comes?
btw curious whether you observed a real issue or just eye-caught the problem?
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Quoted 17 lines
Quoted 1 line
Quoted 1 line · On 20.10.17 at 19:55, <[email protected]> wrote:
Quoted 2 lines · Jan Beulich
Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 19.10.17 at 18:22, <[email protected]> wrote:
DMA-ing to the stack is generally considered bad practice. In this case, if
a
Quoted 2 lines
Quoted 2 lines
timeout occurs because of a sluggish device which is processing the request, the completion notification will corrupt the stack of a subsequent deeper
call
Quoted 8 lines
Quoted 5 lines
tree.
Place the
poll_slotin a percpu area and DMA to that instead.Signed-off-by Andrew Cooper <[email protected]>Please could you extend the commit message to state the issue remaining with using a single per-CPU slot? With that
Reviewed-by Jan Beulich <[email protected]>
Quoted 5 lines
How about this?
Note: This change does not address other issues with the current implementation, such as once a timeout has been suffered, subsequent completions can't be correlated with their requests.
Sounds good.
Quoted 13 lines
Quoted 12 lines
albeit ...
xen/drivers/passthrough/vtd/qinval.c static int __must_check queue_invalidate_wait(struct iommu *iommu, · line 172
Quoted 6 lines
xen/drivers/passthrough/vtd/qinval.c +1
-1@@ -167,7 +169,7 @@ static int __must_check queue_invalidate_wait(struct iommu *iommu, qinval_entry->q.inv_wait_dsc.lo.res_1 =0 ; qinval_entry->q.inv_wait_dsc.lo.sdata =QINVAL_STAT_DONE ; qinval_entry->q.inv_wait_dsc.hi.res_1 =0 ; - qinval_entry->q.inv_wait_dsc.hi.saddr =virt_to_maddr (&poll_slot) >>2 ; + qinval_entry->q.inv_wait_dsc.hi.saddr =virt_to_maddr (this_poll_slot) >>2 ;... this one is still a literal number rather than something allowing to associate back where that value is coming from (but since you're not introducing it here, I also won't insist on you changing it in this patch).
Quoted 1 line
I don't understand. What is still a literal number?
There's still that literal 2 there as the shift count.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Quoted 35 lines · Jan Beulich
Quoted 13 lines
Quoted 1 line
Quoted 1 line · On 20.10.17 at 19:55, <[email protected]> wrote:
Quoted 2 lines · Jan Beulich
Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 19.10.17 at 18:22, <[email protected]> wrote:
DMA-ing to the stack is generally considered bad practice. In this case, if
a
Quoted 2 lines
Quoted 2 lines
timeout occurs because of a sluggish device which is processing the request, the completion notification will corrupt the stack of a subsequent deeper
call
Quoted 5 lines
Quoted 5 lines
tree.
Place the
poll_slotin a percpu area and DMA to that instead.Signed-off-by Andrew Cooper <[email protected]>Quoted 8 lines
Quoted 3 lines
Please could you extend the commit message to state the issue remaining with using a single per-CPU slot? With that
Reviewed-by Jan Beulich <[email protected]>How about this?
Note: This change does not address other issues with the current implementation, such as once a timeout has been suffered, subsequent completions can't be correlated with their requests.
Sounds good.
Quoted 12 lines
Quoted 12 lines
albeit ...
xen/drivers/passthrough/vtd/qinval.c static int __must_check queue_invalidate_wait(struct iommu *iommu, · line 172
Quoted 6 lines
xen/drivers/passthrough/vtd/qinval.c +1
-1@@ -167,7 +169,7 @@ static int __must_check queue_invalidate_wait(struct iommu *iommu, qinval_entry->q.inv_wait_dsc.lo.res_1 =0 ; qinval_entry->q.inv_wait_dsc.lo.sdata =QINVAL_STAT_DONE ; qinval_entry->q.inv_wait_dsc.hi.res_1 =0 ; - qinval_entry->q.inv_wait_dsc.hi.saddr =virt_to_maddr (&poll_slot) >>2 ; + qinval_entry->q.inv_wait_dsc.hi.saddr =virt_to_maddr (this_poll_slot) >>2 ;... this one is still a literal number rather than something allowing to associate back where that value is coming from (but since you're not introducing it here, I also won't insist on you changing it in this patch).
Quoted 2 lines
Quoted 1 line
I don't understand. What is still a literal number?
There's still that literal 2 there as the shift count.
Ah - that is because of the bitfield definition of saddr. It is a 32bit field, but the bottom two bits are reserved, to cause an aligned dword write.
It would probably be cleaner to not declare saddr as a bitfield, and rely on the alignment of u32 to keep the bottom two bits clear.
~Andrew
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel