Skip to contentSkip to conversation

dmaengine: idxd: Change wmb() to smp_wmb() when copying completion record to user space

PATCH In review Fenghua Yu 7 participants 9 messages Jan 30, 2024 – Jan 31, 2024

Subsystems (2)
lore
Share & tools
lore

Fenghua Yu

author
#0 lore raw

PATCH 1/1 dmaengine: idxd: Change wmb() to smp_wmb() when copying completion record to user space

wmb() is used to ensure status in the completion record is written after the rest of the completion record, making it visible to the user. However, on SMP systems, this may not guarantee visibility across different CPUs.

Considering this scenario that event log handler is running on CPU1 while user app is polling completion record (cr) status on CPU2:

	CPU1				CPU2

event log handler user app

1. cr = 0 (status = 0)
2. copy X to user cr except "status"
3. wmb()
4. copy Y to user cr "status"
5. poll status value Y
6. read rest cr which is still 0. cr handling fails
7. cr value X visible now

Although wmb() ensure value Y is written and visible after X is written on CPU1, the order is not guaranteed on CPU2. So user app may see status value Y while cr value X is still not visible yet on CPU2. This will cause reading 0 from the rest of cr and cr handling fails.

Changing wmb() to smp_wmb() ensures Y is written after X on both CPU1 and CPU2. This guarantees that user app can consume cr in right order.

Fixes b022f59725f0 ("dmaengine: idxd: add idxd_copy_cr() to copy user completion record during page fault handling")
---
 drivers/dma/idxd/cdev.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/dma/idxd/cdev.c +3 -2
index 77f8885cf407..9b7388a23cbe 100644
@@ -681,9 +681,10 @@ int idxd_copy_cr(struct idxd_wq *wq, ioasid_t pasid, unsigned long addr,
 		 * Ensure that the completion record's status field is written
 		 * after the rest of the completion record has been written.
 		 * This ensures that the user receives the correct completion
-		 * record information once polling for a non-zero status.
+		 * record information on any CPU once polling for a non-zero
+		 * status.
 		 */
-		wmb();
+		smp_wmb();
 		status = *(u8 *)cr;
 		if (put_user(status, (u8 __user *)addr))
 			left += status_size;
Signature · 1 line
2.37.1
reviewer Reviewed-by on v1 1/1
#1 lore raw
Quoted 25 lines · Fenghua Yu

wmb() is used to ensure status in the completion record is written after the rest of the completion record, making it visible to the user. However, on SMP systems, this may not guarantee visibility across different CPUs.

Considering this scenario that event log handler is running on CPU1 while user app is polling completion record (cr) status on CPU2:

	CPU1				CPU2

event log handler user app

1. cr = 0 (status = 0)
2. copy X to user cr except "status"
3. wmb()
4. copy Y to user cr "status"
5. poll status value Y
6. read rest cr which is still 0. cr handling fails
7. cr value X visible now

Although wmb() ensure value Y is written and visible after X is written on CPU1, the order is not guaranteed on CPU2. So user app may see status value Y while cr value X is still not visible yet on CPU2. This will cause reading 0 from the rest of cr and cr handling fails.

Quoted 7 lines

Changing wmb() to smp_wmb() ensures Y is written after X on both CPU1 and CPU2. This guarantees that user app can consume cr in right order.

Fixes b022f59725f0 ("dmaengine: idxd: add idxd_copy_cr() to copy user completion record during page fault handling")
Quoted 21 lines
---
 drivers/dma/idxd/cdev.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/dma/idxd/cdev.c +3 -2
index 77f8885cf407..9b7388a23cbe 100644
@@ -681,9 +681,10 @@ int idxd_copy_cr(struct idxd_wq *wq, ioasid_t pasid, unsigned long addr,
 		 * Ensure that the completion record's status field is written
 		 * after the rest of the completion record has been written.
 		 * This ensures that the user receives the correct completion
-		 * record information once polling for a non-zero status.
+		 * record information on any CPU once polling for a non-zero
+		 * status.
 		 */
-		wmb();
+		smp_wmb();
 		status = *(u8 *)cr;
 		if (put_user(status, (u8 __user *)addr))
 			left += status_size;
Acked-by on v1 1/1
#2 lore raw
In reply to Fenghua Yu
Quoted 25 lines · Fenghua Yu

wmb() is used to ensure status in the completion record is written after the rest of the completion record, making it visible to the user. However, on SMP systems, this may not guarantee visibility across different CPUs.

Considering this scenario that event log handler is running on CPU1 while user app is polling completion record (cr) status on CPU2:

	CPU1				CPU2

event log handler user app

1. cr = 0 (status = 0)
2. copy X to user cr except "status"
3. wmb()
4. copy Y to user cr "status"
5. poll status value Y
6. read rest cr which is still 0. cr handling fails
7. cr value X visible now

Although wmb() ensure value Y is written and visible after X is written on CPU1, the order is not guaranteed on CPU2. So user app may see status value Y while cr value X is still not visible yet on CPU2. This will cause reading 0 from the rest of cr and cr handling fails.

Quoted 7 lines

Changing wmb() to smp_wmb() ensures Y is written after X on both CPU1 and CPU2. This guarantees that user app can consume cr in right order.

Fixes b022f59725f0 ("dmaengine: idxd: add idxd_copy_cr() to copy user completion record during page fault handling")

some minor comments below.

Quoted 9 lines
---
  drivers/dma/idxd/cdev.c | 5 +++--
  1 file changed, 3 insertions(+), 2 deletions(-)
drivers/dma/idxd/cdev.c +0 -0
index 77f8885cf407..9b7388a23cbe 100644

drivers/dma/idxd/cdev.c int idxd_copy_cr(struct idxd_wq *wq, ioasid_t pasid, unsigned long addr, · line 684

Quoted 5 lines
drivers/dma/idxd/cdev.c +1 -1
@@ -681,9 +681,10 @@ int idxd_copy_cr(struct idxd_wq *wq, ioasid_t pasid, unsigned long addr,
  		 * Ensure that the completion record's status field is written
  		 * after the rest of the completion record has been written.
  		 * This ensures that the user receives the correct completion
-		 * record information once polling for a non-zero status.
+		 * record information on any CPU once polling for a non-zero

Maybe add "smp system" to the sentence to be more explicit since people usually only read above comments instead of the commit message later on. say,
"record information on any CPUs of a SMP system once polling for a non-zero"

Quoted 7 lines
drivers/dma/idxd/cdev.c +2 -1
+		 * status.
  		 */
-		wmb();
+		smp_wmb();
  		status = *(u8 *)cr;
  		if (put_user(status, (u8 __user *)addr))
  			left += status_size;
#3 lore raw
In reply to Fenghua Yu

This patch might be ok (it looks reasonable as an optimization), but I think the description of wmb() and smp_wmb() is incorrect. I also think that you're missing an rmb()/smp_rmb()eor equivalent on the reader side.

Quoted 20 lines · Fenghua Yu

wmb() is used to ensure status in the completion record is written after the rest of the completion record, making it visible to the user. However, on SMP systems, this may not guarantee visibility across different CPUs.

Considering this scenario that event log handler is running on CPU1 while user app is polling completion record (cr) status on CPU2:

	CPU1				CPU2

event log handler user app

1. cr = 0 (status = 0)
2. copy X to user cr except "status"
3. wmb()
4. copy Y to user cr "status"
5. poll status value Y
6. read rest cr which is still 0. cr handling fails
7. cr value X visible now

Quoted 4 lines

Although wmb() ensure value Y is written and visible after X is written on CPU1, the order is not guaranteed on CPU2. So user app may see status value Y while cr value X is still not visible yet on CPU2. This will cause reading 0 from the rest of cr and cr handling fails.

The wmb() on CPU1 ensures the order of the reads, but you need an rmb() on CPU2 between reading the 'status' and 'rest' parts; otherwise CPU2 (or the compiler!) is permitted to hoist the read of 'rest' early, before reading from 'status', and hence you can end up with a sequence that is effectively:

	CPU1				CPU2

event log handler user app

1. cr = 0 (status = 0) 6a. read rest cr which is still 0.
2. copy X to user cr except "status"
3. wmb()
4. copy Y to user cr "status"
5. poll status value Y 6b. cr handling fails
7. cr value X visible now

Since this is all to regular cacheable memory, it's *sufficient* to use smp_wmb() and smp_rmb(), but that's an optimization rather than an ordering fix.

Note that on x86_64, TSO means that the stores are in-order (and so smp_wmb() is just a compiler barrier), and IIUC loads are not reordered w.r.t. other loads (and so smp_rmb() is also just a compiler barrier).

Quoted 2 lines

Changing wmb() to smp_wmb() ensures Y is written after X on both CPU1 and CPU2. This guarantees that user app can consume cr in right order.

This implies that smp_wmb() is *stronger* than wmb(), whereas smp_wmb() is actually *weaker* (e.g. on x86_64 wmb() is an sfence, whereas smp_wmb() is a barrier()).

Thanks,
Mark.

Quoted 30 lines
Fixes b022f59725f0 ("dmaengine: idxd: add idxd_copy_cr() to copy user completion record during page fault handling")
---
 drivers/dma/idxd/cdev.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/dma/idxd/cdev.c +3 -2
index 77f8885cf407..9b7388a23cbe 100644
@@ -681,9 +681,10 @@ int idxd_copy_cr(struct idxd_wq *wq, ioasid_t pasid, unsigned long addr,
 		 * Ensure that the completion record's status field is written
 		 * after the rest of the completion record has been written.
 		 * This ensures that the user receives the correct completion
-		 * record information once polling for a non-zero status.
+		 * record information on any CPU once polling for a non-zero
+		 * status.
 		 */
-		wmb();
+		smp_wmb();
 		status = *(u8 *)cr;
 		if (put_user(status, (u8 __user *)addr))
 			left += status_size;
Signature · 1 line
2.37.1
#4 lore raw
Quoted 3 lines · Mark Rutland

This patch might be ok (it looks reasonable as an optimization), but I think the description of wmb() and smp_wmb() is incorrect. I also think that you're missing an rmb()/smp_rmb()eor equivalent on the reader side.

Sorry, the above should have said:

an rmb()/smp_rmb() *or* equivalent

Quoted 22 lines · Fenghua Yu
Quoted 20 lines

wmb() is used to ensure status in the completion record is written after the rest of the completion record, making it visible to the user. However, on SMP systems, this may not guarantee visibility across different CPUs.

Considering this scenario that event log handler is running on CPU1 while user app is polling completion record (cr) status on CPU2:

	CPU1				CPU2

event log handler user app

1. cr = 0 (status = 0)
2. copy X to user cr except "status"
3. wmb()
4. copy Y to user cr "status"
5. poll status value Y
6. read rest cr which is still 0. cr handling fails
7. cr value X visible now

Quoted 6 lines
Quoted 4 lines

Although wmb() ensure value Y is written and visible after X is written on CPU1, the order is not guaranteed on CPU2. So user app may see status value Y while cr value X is still not visible yet on CPU2. This will cause reading 0 from the rest of cr and cr handling fails.

The wmb() on CPU1 ensures the order of the reads, but you need an rmb() on CPU2

Sorry again, the above should have said:

The wmb() on CPU1 ensures the order of the *writes*

Apologies for any confusion resulting from those mistakes.

Mark.

Quoted 65 lines

between reading the 'status' and 'rest' parts; otherwise CPU2 (or the compiler!) is permitted to hoist the read of 'rest' early, before reading from 'status', and hence you can end up with a sequence that is effectively:

	CPU1				CPU2

event log handler user app

1. cr = 0 (status = 0) 6a. read rest cr which is still 0.
2. copy X to user cr except "status"
3. wmb()
4. copy Y to user cr "status"
5. poll status value Y 6b. cr handling fails
7. cr value X visible now

Since this is all to regular cacheable memory, it's *sufficient* to use smp_wmb() and smp_rmb(), but that's an optimization rather than an ordering fix.

Note that on x86_64, TSO means that the stores are in-order (and so smp_wmb() is just a compiler barrier), and IIUC loads are not reordered w.r.t. other loads (and so smp_rmb() is also just a compiler barrier).

Quoted 2 lines

Changing wmb() to smp_wmb() ensures Y is written after X on both CPU1 and CPU2. This guarantees that user app can consume cr in right order.

This implies that smp_wmb() is *stronger* than wmb(), whereas smp_wmb() is actually *weaker* (e.g. on x86_64 wmb() is an sfence, whereas smp_wmb() is a barrier()).

Thanks,
Mark.

Quoted 30 lines
Fixes b022f59725f0 ("dmaengine: idxd: add idxd_copy_cr() to copy user completion record during page fault handling")
---
 drivers/dma/idxd/cdev.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/dma/idxd/cdev.c +3 -2
index 77f8885cf407..9b7388a23cbe 100644
@@ -681,9 +681,10 @@ int idxd_copy_cr(struct idxd_wq *wq, ioasid_t pasid, unsigned long addr,
 		 * Ensure that the completion record's status field is written
 		 * after the rest of the completion record has been written.
 		 * This ensures that the user receives the correct completion
-		 * record information once polling for a non-zero status.
+		 * record information on any CPU once polling for a non-zero
+		 * status.
 		 */
-		wmb();
+		smp_wmb();
 		status = *(u8 *)cr;
 		if (put_user(status, (u8 __user *)addr))
 			left += status_size;
Signature · 1 line
2.37.1
#5 lore raw
In reply to Mark Rutland
Quoted 2 lines · Mark Rutland

This patch might be ok (it looks reasonable as an optimization), but I think the description of wmb() and smp_wmb() is incorrect. I also think that you're

Agreed. A wmb() -> smp_wmb() change can only be an optimization rather than a fix.

Quoted 45 lines

missing an rmb()/smp_rmb()eor equivalent on the reader side.

Quoted 20 lines · Fenghua Yu

wmb() is used to ensure status in the completion record is written after the rest of the completion record, making it visible to the user. However, on SMP systems, this may not guarantee visibility across different CPUs.

Considering this scenario that event log handler is running on CPU1 while user app is polling completion record (cr) status on CPU2:

	CPU1				CPU2

event log handler user app

1. cr = 0 (status = 0)
2. copy X to user cr except "status"
3. wmb()
4. copy Y to user cr "status"
5. poll status value Y
6. read rest cr which is still 0. cr handling fails
7. cr value X visible now

Quoted 4 lines

Although wmb() ensure value Y is written and visible after X is written on CPU1, the order is not guaranteed on CPU2. So user app may see status value Y while cr value X is still not visible yet on CPU2. This will cause reading 0 from the rest of cr and cr handling fails.

The wmb() on CPU1 ensures the order of the reads, but you need an rmb() on CPU2 between reading the 'status' and 'rest' parts; otherwise CPU2 (or the compiler!) is permitted to hoist the read of 'rest' early, before reading from 'status', and hence you can end up with a sequence that is effectively:

	CPU1				CPU2

event log handler user app

1. cr = 0 (status = 0) 6a. read rest cr which is still 0.
2. copy X to user cr except "status"
3. wmb()
4. copy Y to user cr "status"
5. poll status value Y 6b. cr handling fails
7. cr value X visible now

Quoted 10 lines

Since this is all to regular cacheable memory, it's *sufficient* to use smp_wmb() and smp_rmb(), but that's an optimization rather than an ordering fix.

Note that on x86_64, TSO means that the stores are in-order (and so smp_wmb() is just a compiler barrier), and IIUC loads are not reordered w.r.t. other loads (and so smp_rmb() is also just a compiler barrier).

Quoted 2 lines

Changing wmb() to smp_wmb() ensures Y is written after X on both CPU1 and CPU2. This guarantees that user app can consume cr in right order.

A barrier can only provide ordering for memory accesses on the same CPU, so this doesn't make any sense.

Quoted 12 lines

This implies that smp_wmb() is *stronger* than wmb(), whereas smp_wmb() is actually *weaker* (e.g. on x86_64 wmb() is an sfence, whereas smp_wmb() is a barrier()).

Thanks,
Mark.

Quoted 4 lines
Fixes b022f59725f0 ("dmaengine: idxd: add idxd_copy_cr() to copy user completion record during page fault handling")

Since it has a "Fixes" tag and a "Tested-by" tag, I'd assume there has been a test w/ and w/o this patch showing it can resolve a real issue *constantly*? If so, I think x86 might be broken somewhere.

[Cc x86 maintainers]

Regards,
Boqun

Quoted 26 lines
Quoted 26 lines
---
 drivers/dma/idxd/cdev.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/dma/idxd/cdev.c +3 -2
index 77f8885cf407..9b7388a23cbe 100644
@@ -681,9 +681,10 @@ int idxd_copy_cr(struct idxd_wq *wq, ioasid_t pasid, unsigned long addr,
 		 * Ensure that the completion record's status field is written
 		 * after the rest of the completion record has been written.
 		 * This ensures that the user receives the correct completion
-		 * record information once polling for a non-zero status.
+		 * record information on any CPU once polling for a non-zero
+		 * status.
 		 */
-		wmb();
+		smp_wmb();
 		status = *(u8 *)cr;
 		if (put_user(status, (u8 __user *)addr))
 			left += status_size;
Signature · 1 line
2.37.1
#6 lore raw
Quoted 8 lines · Boqun Feng
Quoted 3 lines
Quoted 3 lines
Fixes b022f59725f0 ("dmaengine: idxd: add idxd_copy_cr() to copy user completion record during page fault handling")

Since it has a "Fixes" tag and a "Tested-by" tag, I'd assume there has been a test w/ and w/o this patch showing it can resolve a real issue *constantly*? If so, I think x86 might be broken somewhere.

[Cc x86 maintainers]

Fenghua, could you perhaps explain how this problem affects end users? What symptom was observed that made it obvious something was broken and what changes with this patch?

author
#7 lore raw

Hi, Dave, Boqun, and Mark,

Quoted 8 lines · Dave Hansen
Quoted 7 lines · Boqun Feng
Quoted 3 lines
Quoted 3 lines
Fixes b022f59725f0 ("dmaengine: idxd: add idxd_copy_cr() to copy user completion record during page fault handling")

Since it has a "Fixes" tag and a "Tested-by" tag, I'd assume there has been a test w/ and w/o this patch showing it can resolve a real issue *constantly*? If so, I think x86 might be broken somewhere.

Quoted 5 lines
Quoted 1 line

[Cc x86 maintainers]

Fenghua, could you perhaps explain how this problem affects end users? What symptom was observed that made it obvious something was broken and what changes with this patch?

There is no issue found by any test. This wmb() code was reviewed and was "thought" that it may have a potential issue. The patch was tested without breaking any existing tests.

From the discussions with Boqun and Mark, this patch might just be an optimization rather than a fix. Let me think about it further and may update commit message in v2 or withdraw this patch since it won't really fix an issue.

Thank you very much for review!

-Fenghua

#8 lore raw
Quoted 29 lines

-----Original Message-----
From: Yu, Fenghua <[email protected]>
Sent: Wednesday, January 31, 2024 2:12 AM
To: Hansen, Dave <[email protected]>; Boqun Feng <[email protected]>; Mark Rutland <[email protected]>
Cc: Vinod Koul <[email protected]>; Jiang, Dave <[email protected]>; [email protected]; linux-kernel <[email protected]>; Rao, Nikhil <[email protected]>; Zhu, Tony <[email protected]>; Mathieu Desnoyers <[email protected]>; Thomas Gleixner <[email protected]>; Ingo Molnar <[email protected]>; Borislav Petkov <[email protected]>; Dave Hansen <[email protected]>; [email protected]
Subject: Re: [PATCH] dmaengine: idxd: Change wmb() to smp_wmb() when copying completion record to user space

Hi, Dave, Boqun, and Mark,

Quoted 9 lines · Dave Hansen
Quoted 8 lines · Boqun Feng
Quoted 4 lines
Quoted 4 lines
Fixes b022f59725f0 ("dmaengine: idxd: add idxd_copy_cr() to copy

user completion record during page fault handling")

Since it has a "Fixes" tag and a "Tested-by" tag, I'd assume there has been a test w/ and w/o this patch showing it can resolve a real issue *constantly*? If so, I think x86 might be broken somewhere.

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

[Cc x86 maintainers]

Fenghua, could you perhaps explain how this problem affects end users? What symptom was observed that made it obvious something was broken and what changes with this patch?

There is no issue found by any test. This wmb() code was reviewed and was "thought" that it may have a potential issue.

I had made this suggestion since the code only needed a smp_wmb(), If the review refers to my suggestion, sorry if my message indicated a potential issue, that certainly wasn't my intention. memory-barriers.txt does say that mandatory barriers (of which wmb() is one) should not be used to control SMP effects.

Nikhil