Skip to contentSkip to conversation

mce: Fix for another race condition

PATCH Frediano Ziglio 1 participant 2 messages Jan 20, 2014 – Jan 21, 2014

lore
Share & tools
lore
#0 lore raw

Hi,
this are actually two patches which both fixes the same race condition
in mce code. The problem is that these lines (in mctelem_reserve)

	newhead = oldhead->mcte_next;
	if (cmpxchgptr(freelp, oldhead, newhead) == oldhead) {

are racy. After you read the newhead pointer it can happen that another flow (thread of recursive invocation) change all the list but set head with same value. So oldhead is the same as *freelp but you are setting a new head that could point to whatever element (even already used).

The base idea of both patches is to separate mcte_state in a separate state and setting it with cmpxchg to make sure we don't pick up an already allocated element.

The first patch try to use the list detaching entirely (setting head to NULL) to avoid the use of mcte_next falling to a slow_reserve which scan all the array trying to find an element in the state FREE. This is surely safe and easy but if list is mostly allocated you end up scanning the array entirely every time.

The second patch (which needs some cleanup) instead of using pointers use array indexes to allow to bound in an atomic way which head and next pointer. The head is attached to a counter which is incremented to avoid to have the list changed but the head with same value (it's like a list version). The state is attached with the next index (which replace mcte_next if state is FREE) to allow atomic read of state+next. To handle both thread safety and reentrancy mctelem_reserve got a bit more complicated and the updates are not so straight forward.

Now, the question is: Should I just send the first patch and ignore the computation problem in the corner case or should I try to put in shape the second patch?

Frediano

# HG changeset patch
# Parent 63a9bfd7be20a7fddda3013be2713cbab7e33c15

xen/arch/x86/cpu/mcheck/mctelem.c +98 -46
diff -r 63a9bfd7be20 xen/arch/x86/cpu/mcheck/mctelem.c
@@ -32,7 +32,8 @@
 struct mctelem_ent {
 	struct mctelem_ent *mcte_next;	/* next in chronological order */
 	struct mctelem_ent *mcte_prev;	/* previous in chronological order */
-	uint32_t mcte_flags;		/* See MCTE_F_* below */
+	uint16_t mcte_state;		/* See MCTE_STATE_* below */
+	uint16_t mcte_flags;		/* See MCTE_F_* below */
 	uint32_t mcte_refcnt;		/* Reference count */
 	void *mcte_data;		/* corresponding data payload */
 };
@@ -41,17 +42,13 @@ struct mctelem_ent {
 #define	MCTE_F_HOME_NONURGENT		0x0002U /* free to nonurgent freelist */
 #define	MCTE_F_CLASS_URGENT		0x0004U /* in use - urgent errors */
 #define	MCTE_F_CLASS_NONURGENT		0x0008U /* in use - nonurgent errors */
-#define	MCTE_F_STATE_FREE		0x0010U	/* on a freelist */
-#define	MCTE_F_STATE_UNCOMMITTED	0x0020U	/* reserved; on no list */
-#define	MCTE_F_STATE_COMMITTED		0x0040U	/* on a committed list */
-#define	MCTE_F_STATE_PROCESSING		0x0080U	/* on a processing list */
+#define	MCTE_STATE_FREE			0x001U	/* on a freelist */
+#define	MCTE_STATE_UNCOMMITTED		0x002U	/* reserved; on no list */
+#define	MCTE_STATE_COMMITTED		0x003U	/* on a committed list */
+#define	MCTE_STATE_PROCESSING		0x004U	/* on a processing list */
 
 #define	MCTE_F_MASK_HOME	(MCTE_F_HOME_URGENT | MCTE_F_HOME_NONURGENT)
 #define	MCTE_F_MASK_CLASS	(MCTE_F_CLASS_URGENT | MCTE_F_CLASS_NONURGENT)
-#define	MCTE_F_MASK_STATE	(MCTE_F_STATE_FREE | \
-				MCTE_F_STATE_UNCOMMITTED | \
-				MCTE_F_STATE_COMMITTED | \
-				MCTE_F_STATE_PROCESSING)
 
 #define	MCTE_HOME(tep) ((tep)->mcte_flags & MCTE_F_MASK_HOME)
 
@@ -60,11 +57,13 @@ struct mctelem_ent {
     (tep)->mcte_flags &= ~MCTE_F_MASK_CLASS; \
     (tep)->mcte_flags |= MCTE_F_CLASS_##new; } while (0)
 
-#define	MCTE_STATE(tep) ((tep)->mcte_flags & MCTE_F_MASK_STATE)
-#define	MCTE_TRANSITION_STATE(tep, old, new) do { \
-    BUG_ON(MCTE_STATE(tep) != (MCTE_F_STATE_##old)); \
-    (tep)->mcte_flags &= ~MCTE_F_MASK_STATE; \
-    (tep)->mcte_flags |= (MCTE_F_STATE_##new); } while (0)
+#define	MCTE_STATE(tep) ((tep)->mcte_state)
+#define	MCTE_XCHG_TRANSITION_STATE(tep, old, new) \
+	(cmpxchg(&(tep)->mcte_state, MCTE_STATE_##old, MCTE_STATE_##new) == MCTE_STATE_##old)
+#define	MCTE_TRANSITION_STATE0(tep, old, new) \
+	BUG_ON(cmpxchg(&(tep)->mcte_state, old, new) != old)
+#define	MCTE_TRANSITION_STATE(tep, old, new) \
+	MCTE_TRANSITION_STATE0((tep), MCTE_STATE_##old, MCTE_STATE_##new)
 
 #define	MC_URGENT_NENT		10
 #define	MC_NONURGENT_NENT	20
@@ -205,16 +204,27 @@ int mctelem_has_deferred(unsigned int cp
 /* Free an entry to its native free list; the entry must not be linked on
  * any list.
  */
-static void mctelem_free(struct mctelem_ent *tep)
+static void mctelem_free(struct mctelem_ent *tep, uint16_t prev_state)
 {
 	mctelem_class_t target = MCTE_HOME(tep) == MCTE_F_HOME_URGENT ?
 	    MC_URGENT : MC_NONURGENT;
 
 	BUG_ON(tep->mcte_refcnt != 0);
-	BUG_ON(MCTE_STATE(tep) != MCTE_F_STATE_FREE);
+	BUG_ON(MCTE_STATE(tep) != prev_state);
 
 	tep->mcte_prev = NULL;
+	tep->mcte_next = NULL;
 	mctelem_xchg_head(&mctctl.mctc_free[target], &tep->mcte_next, tep);
+	/*
+	 * This must be done after adding to list so we still own the element.
+	 * On reserve we have a slow path that just look at the state so
+	 * setting before inserting to list could lead to cases where
+	 * - you set state to free
+	 * - another thread allocate the element
+	 * - you try to insert into list changing an element which is not
+	 *   owned by you (potentially corrupting the element).
+	 */
+	tep->mcte_state = MCTE_STATE_FREE;
 }
 
 /* Increment the reference count of an entry that is not linked on to
@@ -247,9 +257,8 @@ static void mctelem_processing_release(s
 
 	BUG_ON(tep != mctctl.mctc_processing_head[which]);
 	if (--tep->mcte_refcnt == 0) {
-		MCTE_TRANSITION_STATE(tep, PROCESSING, FREE);
 		mctctl.mctc_processing_head[which] = tep->mcte_next;
-		mctelem_free(tep);
+		mctelem_free(tep, MCTE_STATE_PROCESSING);
 	}
 }
 
@@ -287,16 +296,16 @@ void mctelem_init(int reqdatasz)
 		struct mctelem_ent *tep, **tepp;
 
 		tep = mctctl.mctc_elems + i;
-		tep->mcte_flags = MCTE_F_STATE_FREE;
+		tep->mcte_state = MCTE_STATE_FREE;
 		tep->mcte_refcnt = 0;
 		tep->mcte_data = datarr + i * datasz;
 
 		if (i < MC_URGENT_NENT) {
 			tepp = &mctctl.mctc_free[MC_URGENT];
-			tep->mcte_flags |= MCTE_F_HOME_URGENT;
+			tep->mcte_flags = MCTE_F_HOME_URGENT;
 		} else {
 			tepp = &mctctl.mctc_free[MC_NONURGENT];
-			tep->mcte_flags |= MCTE_F_HOME_NONURGENT;
+			tep->mcte_flags = MCTE_F_HOME_NONURGENT;
 		}
 
 		tep->mcte_next = *tepp;
@@ -308,46 +317,90 @@ void mctelem_init(int reqdatasz)
 /* incremented non-atomically when reserve fails */
 static int mctelem_drop_count;
 
+static void mctelem_init_uncommited(struct mctelem_ent *tep, mctelem_class_t which)
+{
+	mctelem_hold(tep);
+	tep->mcte_next = NULL;
+	tep->mcte_prev = NULL;
+	if (which == MC_URGENT)
+		MCTE_SET_CLASS(tep, URGENT);
+	else
+		MCTE_SET_CLASS(tep, NONURGENT);
+}
+
+static mctelem_cookie_t mctelem_slow_reserve(mctelem_class_t which)
+{
+	int i = (which == MC_URGENT) ? 0 : MC_URGENT_NENT;
+
+	for (; i < MC_URGENT_NENT + MC_NONURGENT_NENT; i++) {
+		struct mctelem_ent *tep = mctctl.mctc_elems + i;
+
+		if (MCTE_XCHG_TRANSITION_STATE(tep, FREE, UNCOMMITTED)) {
+			mctelem_init_uncommited(tep, which);
+			return MCTE2COOKIE(tep);
+		}
+	}
+
+	mctelem_drop_count++;
+	return (NULL);
+}
+
 /* Reserve a telemetry entry, or return NULL if none available.
  * If we return an entry then the caller must subsequently call exactly one of
- * mctelem_unreserve or mctelem_commit for that entry.
+ * mctelem_dismiss or mctelem_commit for that entry.
  */
 mctelem_cookie_t mctelem_reserve(mctelem_class_t which)
 {
 	struct mctelem_ent **freelp;
-	struct mctelem_ent *oldhead, *newhead;
+	struct mctelem_ent *oldhead;
 	mctelem_class_t target = (which == MC_URGENT) ?
 	    MC_URGENT : MC_NONURGENT;
 
 	freelp = &mctctl.mctc_free[target];
 	for (;;) {
-		if ((oldhead = *freelp) == NULL) {
-			if (which == MC_URGENT && target == MC_URGENT) {
-				/* raid the non-urgent freelist */
-				target = MC_NONURGENT;
-				freelp = &mctctl.mctc_free[target];
-				continue;
-			} else {
-				mctelem_drop_count++;
-				return (NULL);
-			}
-		}
+		/*
+		 * In case pointer is NULL fall back to slow reserve
+		 * There are 3 possible cases:
+		 * - there are no more free elements;
+		 * - another thread is reserving an element;
+		 * - list was broken as another thread reserved an element
+		 *   using slow path.
+		 * So it could be possible that there are even some urgent
+		 * elements that are freed but list head is NULL, in that
+		 * case only slow reserve can allocate these elements.
+		 */
+		if ((oldhead = *freelp) == NULL)
+			break;
 
-		newhead = oldhead->mcte_next;
-		if (cmpxchgptr(freelp, oldhead, newhead) == oldhead) {
+		/*
+		 * The correct way to remove from the list would be to set
+		 * head to next of first element but we cannot safely read
+		 * the next so we empty the list entirely for a small moment
+		 * and then try to set the head when we got the ownership
+		 * of the element.
+		 * This way combined with the way we handle slow path make
+		 * head pointer more of an advice that a always valid list.
+		 */
+		if (cmpxchgptr(freelp, oldhead, NULL) == oldhead) {
 			struct mctelem_ent *tep = oldhead;
 
-			mctelem_hold(tep);
-			MCTE_TRANSITION_STATE(tep, FREE, UNCOMMITTED);
-			tep->mcte_next = NULL;
-			tep->mcte_prev = NULL;
-			if (which == MC_URGENT)
-				MCTE_SET_CLASS(tep, URGENT);
-			else
-				MCTE_SET_CLASS(tep, NONURGENT);
+			/*
+			 * element taken but somebody already used it so pointer are not valid
+			 * leave list empty it will be refilled while elements are freed again,
+			 * element unlinked will be take again with mctelem_slow_reserve.
+			 * This can happen in 2 cases:
+			 * - element was allocated using slow path;
+			 * - element was inserted to list but state was not set.
+			 */
+			if (!MCTE_XCHG_TRANSITION_STATE(tep, FREE, UNCOMMITTED))
+				break;
+
+			*freelp = tep->mcte_next;
+			mctelem_init_uncommited(tep, which);
 			return MCTE2COOKIE(tep);
 		}
 	}
+	return mctelem_slow_reserve(which);
 }
 
 void *mctelem_dataptr(mctelem_cookie_t cookie)
@@ -366,8 +419,7 @@ void mctelem_dismiss(mctelem_cookie_t co
 	struct mctelem_ent *tep = COOKIE2MCTE(cookie);
 
 	tep->mcte_refcnt--;
-	MCTE_TRANSITION_STATE(tep, UNCOMMITTED, FREE);
-	mctelem_free(tep);
+	mctelem_free(tep, MCTE_STATE_UNCOMMITTED);
 }
 
 /* Commit an entry with completed telemetry for logging.  The caller must

# HG changeset patch
# Parent c45cce86b944403632c81b0f3b98b0db33658e28

xen/arch/x86/cpu/mcheck/mctelem.c +113 -49

276 lines not on this page · raw message

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#1 lore raw
Quoted 32 lines · Frediano Ziglio

Hi,
this are actually two patches which both fixes the same race condition
in mce code. The problem is that these lines (in mctelem_reserve)

	newhead = oldhead->mcte_next;
	if (cmpxchgptr(freelp, oldhead, newhead) == oldhead) {

are racy. After you read the newhead pointer it can happen that another flow (thread of recursive invocation) change all the list but set head with same value. So oldhead is the same as *freelp but you are setting a new head that could point to whatever element (even already used).

The base idea of both patches is to separate mcte_state in a separate state and setting it with cmpxchg to make sure we don't pick up an already allocated element.

The first patch try to use the list detaching entirely (setting head to NULL) to avoid the use of mcte_next falling to a slow_reserve which scan all the array trying to find an element in the state FREE. This is surely safe and easy but if list is mostly allocated you end up scanning the array entirely every time.

The second patch (which needs some cleanup) instead of using pointers use array indexes to allow to bound in an atomic way which head and next pointer. The head is attached to a counter which is incremented to avoid to have the list changed but the head with same value (it's like a list version). The state is attached with the next index (which replace mcte_next if state is FREE) to allow atomic read of state+next. To handle both thread safety and reentrancy mctelem_reserve got a bit more complicated and the updates are not so straight forward.

Quoted 4 lines

Now, the question is: Should I just send the first patch and ignore the computation problem in the corner case or should I try to put in shape the second patch?

I reply to my own mail with another fix. This is quite easy and mainly uses a bitmap. I used a unsigned long instead of a bitmap as is easier to test if all bits are zero but this limit number of entries.

Beside changing the structure another idea would be to use a single bitmap and start from 0 to search for urgent entries while starting from MC_URGENT_NENT to get non urgent entries.

Frediano

# HG changeset patch
# Parent c45cce86b944403632c81b0f3b98b0db33658e28

xen/arch/x86/cpu/mcheck/mctelem.c +32 -21
diff -r c45cce86b944 xen/arch/x86/cpu/mcheck/mctelem.c
@@ -69,6 +69,11 @@
 #define	MC_URGENT_NENT		10
 #define	MC_NONURGENT_NENT	20
 
+/* Check if we can fit enough bits in the free bit array */
+#if MC_URGENT_NENT + MC_NONURGENT_NENT > BITS_PER_LONG
+#error Too much elements
+#endif
+
 #define	MC_NCLASSES		(MC_NONURGENT + 1)
 
 #define	COOKIE2MCTE(c)		((struct mctelem_ent *)(c))
@@ -77,11 +82,9 @@
 static struct mc_telem_ctl {
 	/* Linked lists that thread the array members together.
 	 *
-	 * The free lists are singly-linked via mcte_next, and we allocate
-	 * from them by atomically unlinking an element from the head.
-	 * Consumed entries are returned to the head of the free list.
-	 * When an entry is reserved off the free list it is not linked
-	 * on any list until it is committed or dismissed.
+	 * The free lists is a bit array where bit 1 means free.
+	 * This as element number is quite small and is easy to
+	 * atomically allocate that way.
 	 *
 	 * The committed list grows at the head and we do not maintain a
 	 * tail pointer; insertions are performed atomically.  The head
@@ -101,7 +104,7 @@
 	 * we can lock it for updates.  The head of the processing list
 	 * always has the oldest telemetry, and we append (as above)
 	 * at the tail of the processing list. */
-	struct mctelem_ent *mctc_free[MC_NCLASSES];
+	unsigned long mctc_free[MC_NCLASSES];
 	struct mctelem_ent *mctc_committed[MC_NCLASSES];
 	struct mctelem_ent *mctc_processing_head[MC_NCLASSES];
 	struct mctelem_ent *mctc_processing_tail[MC_NCLASSES];
@@ -214,7 +217,10 @@
 	BUG_ON(MCTE_STATE(tep) != MCTE_F_STATE_FREE);
 
 	tep->mcte_prev = NULL;
-	mctelem_xchg_head(&mctctl.mctc_free[target], &tep->mcte_next, tep);
+	tep->mcte_next = NULL;
+
+	/* set free in array */
+	set_bit(tep - mctctl.mctc_elems, &mctctl.mctc_free[target]);
 }
 
 /* Increment the reference count of an entry that is not linked on to
@@ -284,7 +290,7 @@
 	}
 
 	for (i = 0; i < MC_URGENT_NENT + MC_NONURGENT_NENT; i++) {
-		struct mctelem_ent *tep, **tepp;
+		struct mctelem_ent *tep;
 
 		tep = mctctl.mctc_elems + i;
 		tep->mcte_flags = MCTE_F_STATE_FREE;
@@ -292,16 +298,15 @@
 		tep->mcte_data = datarr + i * datasz;
 
 		if (i < MC_URGENT_NENT) {
-			tepp = &mctctl.mctc_free[MC_URGENT];
-			tep->mcte_flags |= MCTE_F_HOME_URGENT;
+			__set_bit(i, &mctctl.mctc_free[MC_URGENT]);
+			tep->mcte_flags = MCTE_F_HOME_URGENT;
 		} else {
-			tepp = &mctctl.mctc_free[MC_NONURGENT];
-			tep->mcte_flags |= MCTE_F_HOME_NONURGENT;
+			__set_bit(i, &mctctl.mctc_free[MC_NONURGENT]);
+			tep->mcte_flags = MCTE_F_HOME_NONURGENT;
 		}
 
-		tep->mcte_next = *tepp;
+		tep->mcte_next = NULL;
 		tep->mcte_prev = NULL;
-		*tepp = tep;
 	}
 }
 
@@ -310,18 +315,21 @@
 
 /* Reserve a telemetry entry, or return NULL if none available.
  * If we return an entry then the caller must subsequently call exactly one of
- * mctelem_unreserve or mctelem_commit for that entry.
+ * mctelem_dismiss or mctelem_commit for that entry.
  */
 mctelem_cookie_t mctelem_reserve(mctelem_class_t which)
 {
-	struct mctelem_ent **freelp;
-	struct mctelem_ent *oldhead, *newhead;
+	unsigned long *freelp;
+	unsigned long oldfree;
+	unsigned bit;
 	mctelem_class_t target = (which == MC_URGENT) ?
 	    MC_URGENT : MC_NONURGENT;
 
 	freelp = &mctctl.mctc_free[target];
 	for (;;) {
-		if ((oldhead = *freelp) == NULL) {
+		oldfree = *freelp;
+
+		if (oldfree == 0) {
 			if (which == MC_URGENT && target == MC_URGENT) {
 				/* raid the non-urgent freelist */
 				target = MC_NONURGENT;
@@ -333,9 +341,11 @@
 			}
 		}
 
-		newhead = oldhead->mcte_next;
-		if (cmpxchgptr(freelp, oldhead, newhead) == oldhead) {
-			struct mctelem_ent *tep = oldhead;
+		/* try to allocate, atomically clear free bit */
+		bit = find_first_set_bit(oldfree);
+		if (test_and_clear_bit(bit, freelp)) {
+			/* return element we got */
+			struct mctelem_ent *tep = mctctl.mctc_elems + bit;
 
 			mctelem_hold(tep);
 			MCTE_TRANSITION_STATE(tep, FREE, UNCOMMITTED);
@@ -345,6 +355,7 @@
 				MCTE_SET_CLASS(tep, URGENT);
 			else
 				MCTE_SET_CLASS(tep, NONURGENT);
+
 			return MCTE2COOKIE(tep);
 		}
 	}

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel