Skip to contentSkip to conversation

mce: Fix race condition in mctelem_xchg_head

PATCH Superseded Frediano Ziglio 3 participants 8 messages Jan 16, 2014 – Jan 17, 2014

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mce: Fix race condition in mctelem_xchg_head

The function that exchange the head is racy.
The cmpxchg and the compare are not atomic.
Assume two thread one (T1) inserting on committed list and another trying to comsume it (T2).
T1 start inserting the element (A), set prev pointer (commit list use mcte_prev) then is stop after the cmpxchg succeeded.
T2 get the list and change elements (A) and update the commit list head.
T1 resume, read pointer to prev again and compare with result from cmpxchg which succeeded but in the meantime prev changed in memory. Not T1 assume the element was not inserted on the list and try to insert again. Now A however is in another state and should not be modified by T1.
To solve the race use temporary variable for prev pointer. Note that compiler should not optimize the memory fetch as cmpxhg do a full barrier.

---
 xen/arch/x86/cpu/mcheck/mctelem.c |    8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
xen/arch/x86/cpu/mcheck/mctelem.c +5 -3
index 37d830f..5821278 100644
@@ -127,13 +127,15 @@ static DEFINE_PER_CPU(struct mc_telem_cpu_ctl, mctctl);
 static DEFINE_SPINLOCK(processing_lock);
 
 static void mctelem_xchg_head(struct mctelem_ent **headp,
-				struct mctelem_ent **old,
+				struct mctelem_ent **oldp,
 				struct mctelem_ent *new)
 {
+	struct mctelem_ent *old;
+
 	for (;;) {
-		*old = *headp;
+		*oldp = old = *headp;
 		wmb();
-		if (cmpxchgptr(headp, *old, new) == *old)
+		if (cmpxchgptr(headp, old, new) == old)
 			break;
 	}
 }
Signature · 1 line
1.7.10.4
#1 lore raw
Quoted 17 lines
Quoted 1 line
Quoted 1 line · Frediano Ziglio

The function that exchange the head is racy.
The cmpxchg and the compare are not atomic.
Assume two thread one (T1) inserting on committed list and another trying to comsume it (T2).
T1 start inserting the element (A), set prev pointer (commit list use mcte_prev) then is stop after the cmpxchg succeeded.
T2 get the list and change elements (A) and update the commit list head.
T1 resume, read pointer to prev again and compare with result from cmpxchg which succeeded but in the meantime prev changed in memory. Not T1 assume the element was not inserted on the list and try to insert again. Now A however is in another state and should not be modified by T1.
To solve the race use temporary variable for prev pointer. Note that compiler should not optimize the memory fetch as cmpxhg do a full barrier.

This last sentence is pretty pointless, since there's an explicit wmb() between setting old and doing the cmpxchg. (Question is whether that wmb() actually is necessary; without a comment I can't easily see what it is supposed to fence - surely it's not the write to *oldp. And that's regardless of it being redundant with the barrier embedded with cmpxchgptr().)

But overall, while I think your analysis is correct, the description could do with some cleanup, also spelling-wise.

Quoted 6 lines

static void mctelem_xchg_head(struct mctelem_ent **headp,

xen/arch/x86/cpu/mcheck/mctelem.c +2 -1
-				struct mctelem_ent **old,
+				struct mctelem_ent **oldp,
 				struct mctelem_ent *new)
 {
+	struct mctelem_ent *old;

xen/arch/x86/cpu/mcheck/mctelem.c static DEFINE_PER_CPU(struct mc_telem_cpu_ctl, mctctl); · line 141

Quoted 10 lines
xen/arch/x86/cpu/mcheck/mctelem.c +3 -2
+
 	for (;;) {
-		*old = *headp;
+		*oldp = old = *headp;
 		wmb();
-		if (cmpxchgptr(headp, *old, new) == *old)
+		if (cmpxchgptr(headp, old, new) == old)
 			break;
 	}
 }

Now that you use a temporary, it would make sense (and make the code easier to read) if you set *oldp to old just once, after the loop.

Jan

#2 lore raw
Quoted 25 lines · Jan Beulich
Quoted 17 lines
Quoted 1 line
Quoted 1 line · Frediano Ziglio

The function that exchange the head is racy.
The cmpxchg and the compare are not atomic.
Assume two thread one (T1) inserting on committed list and another trying to comsume it (T2).
T1 start inserting the element (A), set prev pointer (commit list use mcte_prev) then is stop after the cmpxchg succeeded.
T2 get the list and change elements (A) and update the commit list head.
T1 resume, read pointer to prev again and compare with result from cmpxchg which succeeded but in the meantime prev changed in memory. Not T1 assume the element was not inserted on the list and try to insert again. Now A however is in another state and should not be modified by T1.
To solve the race use temporary variable for prev pointer. Note that compiler should not optimize the memory fetch as cmpxhg do a full barrier.

This last sentence is pretty pointless, since there's an explicit wmb() between setting old and doing the cmpxchg. (Question is whether that wmb() actually is necessary; without a comment I can't easily see what it is supposed to fence - surely it's not the write to *oldp. And that's regardless of it being redundant with the barrier embedded with cmpxchgptr().)

Quoted 3 lines

But overall, while I think your analysis is correct, the description could do with some cleanup, also spelling-wise.

Yes, sorry, I'm not native English. Suggestion accepted.

The race is here

if (cmpxchgptr(headp, *old, new) == *old)

You do the cmpxchg (which is atomic), then you read old pointer (*old) then you compare cmpxchg result with the content of the pointer.

Yes, wmb is quite useless, cmpxchg should be enough.

Quoted 21 lines
Quoted 6 lines

static void mctelem_xchg_head(struct mctelem_ent **headp,

xen/arch/x86/cpu/mcheck/mctelem.c +2 -1
-				struct mctelem_ent **old,
+				struct mctelem_ent **oldp,
 				struct mctelem_ent *new)
 {
+	struct mctelem_ent *old;

xen/arch/x86/cpu/mcheck/mctelem.c static DEFINE_PER_CPU(struct mc_telem_cpu_ctl, mctctl); · line 141

Quoted 10 lines
xen/arch/x86/cpu/mcheck/mctelem.c +3 -2
+
 	for (;;) {
-		*old = *headp;
+		*oldp = old = *headp;
 		wmb();
-		if (cmpxchgptr(headp, *old, new) == *old)
+		if (cmpxchgptr(headp, old, new) == old)
 			break;
 	}
 }

Now that you use a temporary, it would make sense (and make the code easier to read) if you set *oldp to old just once, after the loop.

Quoted 2 lines

Jan

No, this will create another race. After cmpxchg you must assume that the list element can be own by somebody else.

Frediano

#3 lore raw
Quoted 27 lines
Quoted 1 line
Quoted 1 line · Frediano Ziglio
Quoted 25 lines · Jan Beulich
Quoted 17 lines
Quoted 1 line
Quoted 1 line · Frediano Ziglio

The function that exchange the head is racy.
The cmpxchg and the compare are not atomic.
Assume two thread one (T1) inserting on committed list and another trying to comsume it (T2).
T1 start inserting the element (A), set prev pointer (commit list use mcte_prev) then is stop after the cmpxchg succeeded.
T2 get the list and change elements (A) and update the commit list head.
T1 resume, read pointer to prev again and compare with result from cmpxchg which succeeded but in the meantime prev changed in memory. Not T1 assume the element was not inserted on the list and try to insert again. Now A however is in another state and should not be modified by T1.
To solve the race use temporary variable for prev pointer. Note that compiler should not optimize the memory fetch as cmpxhg do a full barrier.

This last sentence is pretty pointless, since there's an explicit wmb() between setting old and doing the cmpxchg. (Question is whether that wmb() actually is necessary; without a comment I can't easily see what it is supposed to fence - surely it's not the write to *oldp. And that's regardless of it being redundant with the barrier embedded with cmpxchgptr().)

Quoted 5 lines
Quoted 3 lines

But overall, while I think your analysis is correct, the description could do with some cleanup, also spelling-wise.

Yes, sorry, I'm not native English.

Neither am I.

Quoted 8 lines

Suggestion accepted.

The race is here

if (cmpxchgptr(headp, *old, new) == *old)

You do the cmpxchg (which is atomic), then you read old pointer (*old) then you compare cmpxchg result with the content of the pointer.

All understood.

Quoted 23 lines

Yes, wmb is quite useless, cmpxchg should be enough.

Quoted 12 lines
Quoted 12 lines

static void mctelem_xchg_head(struct mctelem_ent **headp,

xen/arch/x86/cpu/mcheck/mctelem.c +4 -3
-				struct mctelem_ent **old,
+				struct mctelem_ent **oldp,
 				struct mctelem_ent *new)
 {
+	struct mctelem_ent *old;
+
 	for (;;) {
-		*old = *headp;
+		*oldp = old = *headp;
 		wmb();
-		if (cmpxchgptr(headp, *old, new) == *old)
Quoted 8 lines

xen/arch/x86/cpu/mcheck/mctelem.c static DEFINE_PER_CPU(struct mc_telem_cpu_ctl, mctctl); · line 141

Quoted 4 lines
xen/arch/x86/cpu/mcheck/mctelem.c +1 -0
+		if (cmpxchgptr(headp, old, new) == old)
 			break;
 	}
 }

Now that you use a temporary, it would make sense (and make the code easier to read) if you set *oldp to old just once, after the loop.

Quoted 2 lines

No, this will create another race. After cmpxchg you must assume that the list element can be own by somebody else.

"The list element" being which? I simply don't see how storing to *oldp non-atomically is race free when done before the cmpxchg, but is a problem when done afterwards. Are there perhaps additional requirement put on this by some (but not all) of the callers? I ask because the function by itself doesn't seem to have a race other than the one you try to fix, no matter where *oldp is being stored to. If indeed there are further requirements, then adding a comment here would seem necessary (since I'm sure there'd be me or someone else stumbling across that inefficiency again in the future, and hence wanting to clean it up in the same way I suggested).

Jan

#4 lore raw
Quoted 84 lines · Jan Beulich
Quoted 27 lines
Quoted 1 line
Quoted 1 line · Frediano Ziglio
Quoted 25 lines · Jan Beulich
Quoted 17 lines
Quoted 1 line
Quoted 1 line · Frediano Ziglio

The function that exchange the head is racy.
The cmpxchg and the compare are not atomic.
Assume two thread one (T1) inserting on committed list and another trying to comsume it (T2).
T1 start inserting the element (A), set prev pointer (commit list use mcte_prev) then is stop after the cmpxchg succeeded.
T2 get the list and change elements (A) and update the commit list head.
T1 resume, read pointer to prev again and compare with result from cmpxchg which succeeded but in the meantime prev changed in memory. Not T1 assume the element was not inserted on the list and try to insert again. Now A however is in another state and should not be modified by T1.
To solve the race use temporary variable for prev pointer. Note that compiler should not optimize the memory fetch as cmpxhg do a full barrier.

This last sentence is pretty pointless, since there's an explicit wmb() between setting old and doing the cmpxchg. (Question is whether that wmb() actually is necessary; without a comment I can't easily see what it is supposed to fence - surely it's not the write to *oldp. And that's regardless of it being redundant with the barrier embedded with cmpxchgptr().)

Quoted 5 lines
Quoted 3 lines

But overall, while I think your analysis is correct, the description could do with some cleanup, also spelling-wise.

Yes, sorry, I'm not native English.

Neither am I.

Quoted 8 lines

Suggestion accepted.

The race is here

if (cmpxchgptr(headp, *old, new) == *old)

You do the cmpxchg (which is atomic), then you read old pointer (*old) then you compare cmpxchg result with the content of the pointer.

All understood.

Quoted 23 lines

Yes, wmb is quite useless, cmpxchg should be enough.

Quoted 12 lines
Quoted 12 lines

static void mctelem_xchg_head(struct mctelem_ent **headp,

xen/arch/x86/cpu/mcheck/mctelem.c +4 -3
-				struct mctelem_ent **old,
+				struct mctelem_ent **oldp,
 				struct mctelem_ent *new)
 {
+	struct mctelem_ent *old;
+
 	for (;;) {
-		*old = *headp;
+		*oldp = old = *headp;
 		wmb();
-		if (cmpxchgptr(headp, *old, new) == *old)
Quoted 8 lines

xen/arch/x86/cpu/mcheck/mctelem.c static DEFINE_PER_CPU(struct mc_telem_cpu_ctl, mctctl); · line 141

Quoted 4 lines
xen/arch/x86/cpu/mcheck/mctelem.c +1 -0
+		if (cmpxchgptr(headp, old, new) == old)
 			break;
 	}
 }

Now that you use a temporary, it would make sense (and make the code easier to read) if you set *oldp to old just once, after the loop.

Quoted 2 lines

No, this will create another race. After cmpxchg you must assume that the list element can be own by somebody else.

"The list element" being which? I simply don't see how storing to *oldp non-atomically is race free when done before the cmpxchg, but is a problem when done afterwards. Are there perhaps additional requirement put on this by some (but not all) of the callers? I ask because the function by itself doesn't seem to have a race other than the one you try to fix, no matter where *oldp is being stored to. If indeed there are further requirements, then adding a comment here would seem necessary (since I'm sure there'd be me or someone else stumbling across that inefficiency again in the future, and hence wanting to clean it up in the same way I suggested).

Quoted 2 lines

Jan

This function is used to safely add a element to a list. You pass:
- pointer to pointer to head (headp);
- pointer to pointer to next/prev pointer (old/oldp);
- pointer to list element (new).
Now you own (nobody should allow to touch) the element till it get inserted to the list.
Now assume we set the pointer after the loop. The head points to an element which has no next/prev set so you break the list. Another thread will see a corrupted or shortened list.

Frediano

#5 lore raw
Quoted 10 lines
Quoted 1 line
Quoted 1 line · Frediano Ziglio

This function is used to safely add a element to a list. You pass:
- pointer to pointer to head (headp);
- pointer to pointer to next/prev pointer (old/oldp);
- pointer to list element (new).
Now you own (nobody should allow to touch) the element till it get inserted to the list.
Now assume we set the pointer after the loop. The head points to an element which has no next/prev set so you break the list. Another thread will see a corrupted or shortened list.

Ah, okay. So "old" (or "oldp" after you patch) is really misguiding. This should rather become "linkp" or some such then.

And in that case I'd then also ask for the declaration of the new "old" to be moved inside the for() body.

With that and with at least the spelling mistakes fixed in the description, I'll be fine with the patch.

Jan

#6 lore raw

PATCH v2 1/1 mce: Fix race condition in mctelem_xchg_head

The function (mctelem_xchg_head()) used to exchange mce telemetry list heads is racy. It may write to the head twice, with the second write linking to an element in the wrong state.

If there are two threads, T1 inserting on committed list; and T2 trying to consume it.

1. T1 starts inserting an element (A), sets prev pointer (mcte_prev).
2. T1 is interrupted after the cmpxchg succeeded.
3. T2 gets the list and changes element A and updates the commit list head.
4. T1 resumes, reads pointer to prev again and compare with result from the cmpxchg which succeeded but in the meantime prev changed in memory.
5. T1 thinks the cmpxchg failed and goes around the loop again, linking head to A again.

To solve the race use temporary variable for prev pointer.

*linkp (which point to a field in the element) must be updated before the cmpxchg() as after a successful cmpxchg the element might be immediately removed and reinitialized.

The wmb() prior to the cmpchgptr() call is not necessary since it is already a full memory barrier. This wmb() is thus removed.

---
 xen/arch/x86/cpu/mcheck/mctelem.c |    9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

Version 2:
- changed commit message;
- remove useless wmb().

xen/arch/x86/cpu/mcheck/mctelem.c +5 -4
index 37d830f..895ce1a 100644
@@ -127,13 +127,14 @@ static DEFINE_PER_CPU(struct mc_telem_cpu_ctl, mctctl);
 static DEFINE_SPINLOCK(processing_lock);
 
 static void mctelem_xchg_head(struct mctelem_ent **headp,
-				struct mctelem_ent **old,
+				struct mctelem_ent **linkp,
 				struct mctelem_ent *new)
 {
 	for (;;) {
-		*old = *headp;
-		wmb();
-		if (cmpxchgptr(headp, *old, new) == *old)
+		struct mctelem_ent *old;
+
+		*linkp = old = *headp;
+		if (cmpxchgptr(headp, old, new) == old)
 			break;
 	}
 }
Signature · 1 line
1.7.10.4
Reviewed-by on v2 1/1
#7 lore raw
Quoted 19 lines · Frediano Ziglio

The function (mctelem_xchg_head()) used to exchange mce telemetry list heads is racy. It may write to the head twice, with the second write linking to an element in the wrong state.

If there are two threads, T1 inserting on committed list; and T2 trying to consume it.

1. T1 starts inserting an element (A), sets prev pointer (mcte_prev).
2. T1 is interrupted after the cmpxchg succeeded.
3. T2 gets the list and changes element A and updates the commit list head.
4. T1 resumes, reads pointer to prev again and compare with result from the cmpxchg which succeeded but in the meantime prev changed in memory.
5. T1 thinks the cmpxchg failed and goes around the loop again, linking head to A again.

To solve the race use temporary variable for prev pointer.

Quoted 8 lines

*linkp (which point to a field in the element) must be updated before the cmpxchg() as after a successful cmpxchg the element might be immediately removed and reinitialized.

The wmb() prior to the cmpchgptr() call is not necessary since it is already a full memory barrier. This wmb() is thus removed.