Skip to contentSkip to conversation

oxenstored: fix short-write issue

PATCH In review Wei Liu 6 participants 15 messages Oct 27, 2015 – Nov 2, 2015

lore
Share & tools
lore

Wei Liu

author
#0 lore raw

PATCH 1/1 oxenstored: fix short-write issue

When oxenstored wrote to the ring, it wrote a chunk of contiguous data. Originally when it tried to write across ring boundary, it returned a short-write when there is still room. That led to stalling mini-os's xenstore thread at times.

Fix this by calling write function for a second time when the first write completes partially.

---
Cc: David Scott <[email protected]>
Cc: Ian Campbell <[email protected]>
Cc: Ian Jackson <[email protected]>

Ian, backport candidate for as far as you can manage.

---
 tools/ocaml/libs/xb/xb.ml | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
tools/ocaml/libs/xb/xb.ml +5 -3
index 50944b5..0730d13 100644
@@ -91,10 +91,12 @@ let write_fd back con s len =
 	Unix.write back.fd s 0 len
 
 let write_mmap back con s len =
-	let ws = Xs_ring.write back.mmap s len in
-	if ws > 0 then
+	let ws = ref (Xs_ring.write back.mmap s len) in
+	if !ws < len then
+		ws := !ws + Xs_ring.write back.mmap (String.sub s !ws (len - !ws)) (len - !ws);
+	if !ws > 0 then
 		back.eventchn_notify ();
-	ws
+	!ws
 
 let write con s len =
 	match con.backend with
Signature · 1 line
2.1.4
#1 lore raw

That being said,

tools/ocaml/libs/xb/xb.ml let write_fd back con s len = · line 95

Quoted 8 lines · Wei Liu, le Tue 27 Oct 2015 17:10:09 +0000, a écrit :
tools/ocaml/libs/xb/xb.ml +2 -2
@@ -91,10 +91,12 @@ let write_fd back con s len =
 	Unix.write back.fd s 0 len
 
 let write_mmap back con s len =
-	let ws = Xs_ring.write back.mmap s len in
-	if ws > 0 then
+	let ws = ref (Xs_ring.write back.mmap s len) in
+	if !ws < len then

This could also be

   	if !ws > 0 && !ws < len then

And also perhaps add a comment:

(* Xs_ring.write returned a short-write. Perhaps it just hit
the ring boundary and there is actually still room. Call it a second time to try to finish writing at the beginning of the ring. *)

Quoted 11 lines
tools/ocaml/libs/xb/xb.ml +3 -1
+		ws := !ws + Xs_ring.write back.mmap (String.sub s !ws (len - !ws)) (len - !ws);
+	if !ws > 0 then
 		back.eventchn_notify ();
-	ws
+	!ws
 
 let write con s len =
 	match con.backend with
Signature · 1 line
2.1.4
Signature · 5 lines
Samuel
<N> un driver qui fait quoi, alors ?
<y> ben pour les bips
<s> pour passer les oops en morse
 -+- #ens-mim - vive les rapports de bug -+-
#2 lore raw
In reply to Wei Liu
Quoted 25 lines · Wei Liu

When oxenstored wrote to the ring, it wrote a chunk of contiguous data. Originally when it tried to write across ring boundary, it returned a short-write when there is still room. That led to stalling mini-os's xenstore thread at times.

Fix this by calling write function for a second time when the first write completes partially.

---
Cc: David Scott <[email protected]>
Cc: Ian Campbell <[email protected]>
Cc: Ian Jackson <[email protected]>

Ian, backport candidate for as far as you can manage.

---
 tools/ocaml/libs/xb/xb.ml | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
tools/ocaml/libs/xb/xb.ml +0 -0
index 50944b5..0730d13 100644

tools/ocaml/libs/xb/xb.ml let write_fd back con s len = · line 96

Quoted 8 lines
tools/ocaml/libs/xb/xb.ml +3 -2
@@ -91,10 +91,12 @@ let write_fd back con s len =
 	Unix.write back.fd s 0 len
 
 let write_mmap back con s len =
-	let ws = Xs_ring.write back.mmap s len in
-	if ws > 0 then
+	let ws = ref (Xs_ring.write back.mmap s len) in
+	if !ws < len then
+		ws := !ws + Xs_ring.write back.mmap (String.sub s !ws (len - !ws)) (len - !ws);

This is surely a TOCTOU, as the second attempted write could return short as well.

The correct behaviour would be for Xs_ring.write to return the actual number of bytes it put into the ring, even if this is shorter than len.

~Andrew

#3 lore raw
Quoted 2 lines · Andrew Cooper, le Tue 27 Oct 2015 17:21:39 +0000, a écrit :

The correct behaviour would be for Xs_ring.write to return the actual number of bytes it put into the ring, even if this is shorter than len.

That is already what it does.

Samuel

#4 lore raw
In reply to Andrew Cooper
Quoted 1 line · Andrew Cooper, le Tue 27 Oct 2015 17:21:39 +0000, a écrit :

as the second attempted write could return short as well.

That is fine. The second attempt will only return a short write if there was really not enough room for it, which is what we want.

Samuel

#5 lore raw

Another way would be to make ml_interface_write write both pieces instead of just a contiguous one. The caml code would then look less suspicious :)

Samuel

#6 lore raw
In reply to Samuel Thibault
Quoted 4 lines · Samuel Thibault
Quoted 1 line · Andrew Cooper, le Tue 27 Oct 2015 17:21:39 +0000, a écrit :

as the second attempted write could return short as well.

That is fine. The second attempt will only return a short write if there was really not enough room for it, which is what we want.

Then surely the bug is that Xs_ring.write returns short when it shouldn't?

~Andrew

#7 lore raw
Quoted 10 lines · Andrew Cooper
Quoted 4 lines · Samuel Thibault
Quoted 1 line · Andrew Cooper, le Tue 27 Oct 2015 17:21:39 +0000, a écrit :

as the second attempted write could return short as well.

That is fine. The second attempt will only return a short write if there was really not enough room for it, which is what we want.

Quoted 9 lines

Then surely the bug is that Xs_ring.write returns short when it shouldn’t?

On 27 Oct 2015, at 17:31, Samuel Thibault <[email protected]> wrote:

Another way would be to make ml_interface_write write both pieces instead of just a contiguous one. The caml code would then look less suspicious :)

I agree with both Andrew and Samuel that it would be a better fix if Xs_ring.write (== “ml_interface_write”) wrote both pieces at once.

However I believe the ‘suspicious’ OCaml patch fixes the specific issue (— or have I missed something?).

Last time I meddled with the C-level ring reading/writing code I didn’t get it quite right. Does anyone have time to prototype what a C-level fix would look like? If we’re short of time I could live with the OCaml-level fix, especially since Wei has done some stress-testing (assuming everyone believes it fixes the issue)

Cheers,
Dave

#8 lore raw
Quoted 1 line · David Scott, le Wed 28 Oct 2015 13:34:10 +0000, a écrit :

However I believe the ‘suspicious’ OCaml patch fixes the specific issue (— or have I missed something?).

It does.

Quoted 1 line

Does anyone have time to prototype what a C-level fix would look like?

Here is a try, untested though.

tools/ocaml/libs/xb/xs_ring_stubs.c +20 -5
index fd561a2..5c771d6 100644
@@ -99,12 +99,12 @@ CAMLprim value ml_interface_write(value ml_interface,
 	int result;
 
 	struct xenstore_domain_interface *intf = interface->addr;
-	XENSTORE_RING_IDX cons, prod;
+	XENSTORE_RING_IDX cons, old_prod, prod;
 	int can_write;
 	uint32_t connection;
 
 	cons = *(volatile uint32_t*)&intf->rsp_cons;
-	prod = *(volatile uint32_t*)&intf->rsp_prod;
+	old_prod = prod = *(volatile uint32_t*)&intf->rsp_prod;
 	connection = *(volatile uint32_t*)&intf->connection;
 
 	if (connection != XENSTORE_CONNECTED)
@@ -116,15 +116,30 @@ CAMLprim value ml_interface_write(value ml_interface,
 		goto exit;
 	}
 	if (MASK_XENSTORE_IDX(prod) >= MASK_XENSTORE_IDX(cons))
+	{
 		can_write = XENSTORE_RING_SIZE - MASK_XENSTORE_IDX(prod);
-	else
+		if (can_write < len)
+		{
+			/* Not enough room at end of ring.  Copy what we can first.  */
+			memcpy(intf->rsp + MASK_XENSTORE_IDX(prod), buffer, can_write);
+			prod += can_write;
+			len -= can_write;
+			buffer += can_write;
+
+			/* We are now back at beginning of ring.  */
+			can_write = MASK_XENSTORE_IDX(cons) - MASK_XENSTORE_IDX(prod);
+		}
+	}
+	else
 		can_write = MASK_XENSTORE_IDX(cons) - MASK_XENSTORE_IDX(prod);
 	if (can_write < len)
+		/* Real short write: not enough room.  */
 		len = can_write;
 	memcpy(intf->rsp + MASK_XENSTORE_IDX(prod), buffer, len);
+	prod += len;
 	xen_mb();
-	intf->rsp_prod += len;
-	result = len;
+	intf->rsp_prod = prod;
+	result = prod - old_prod;
 exit:
 	ml_result = Val_int(result);
 	CAMLreturn(ml_result);
tools/ocaml/libs/xb/xs_ring_stubs.c +20 -5
index fd561a2..5c771d6 100644
@@ -99,12 +99,12 @@ CAMLprim value ml_interface_write(value ml_interface,
 	int result;
 
 	struct xenstore_domain_interface *intf = interface->addr;
-	XENSTORE_RING_IDX cons, prod;
+	XENSTORE_RING_IDX cons, old_prod, prod;
 	int can_write;
 	uint32_t connection;
 
 	cons = *(volatile uint32_t*)&intf->rsp_cons;
-	prod = *(volatile uint32_t*)&intf->rsp_prod;
+	old_prod = prod = *(volatile uint32_t*)&intf->rsp_prod;
 	connection = *(volatile uint32_t*)&intf->connection;
 
 	if (connection != XENSTORE_CONNECTED)
@@ -116,15 +116,30 @@ CAMLprim value ml_interface_write(value ml_interface,
 		goto exit;
 	}
 	if (MASK_XENSTORE_IDX(prod) >= MASK_XENSTORE_IDX(cons))
+	{
 		can_write = XENSTORE_RING_SIZE - MASK_XENSTORE_IDX(prod);
-	else 
+		if (can_write < len)
+		{
+			/* Not enough room at end of ring.  Copy what we can first.  */
+			memcpy(intf->rsp + MASK_XENSTORE_IDX(prod), buffer, can_write);
+			prod += can_write;
+			len -= can_write;
+			buffer += can_write;
+
+			/* We are now back at beginning of ring.  */
+			can_write = MASK_XENSTORE_IDX(cons) - MASK_XENSTORE_IDX(prod);
+		}
+	}
+	else
 		can_write = MASK_XENSTORE_IDX(cons) - MASK_XENSTORE_IDX(prod);
 	if (can_write < len)
+		/* Real short write: not enough room.  */
 		len = can_write;
 	memcpy(intf->rsp + MASK_XENSTORE_IDX(prod), buffer, len);
+	prod += len;
 	xen_mb();
-	intf->rsp_prod += len;
-	result = len;
+	intf->rsp_prod = prod;
+	result = prod - old_prod;
 exit:
 	ml_result = Val_int(result);
 	CAMLreturn(ml_result);

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

#9 lore raw
In reply to David Scott
Quoted 12 lines · David Scott
Quoted 12 lines · Andrew Cooper
Quoted 4 lines · Samuel Thibault
Quoted 1 line · Andrew Cooper, le Tue 27 Oct 2015 17:21:39 +0000, a écrit :

as the second attempted write could return short as well.

That is fine. The second attempt will only return a short write if there was really not enough room for it, which is what we want.

Then surely the bug is that Xs_ring.write returns short when it shouldn’t?

On 27 Oct 2015, at 17:31, Samuel Thibault <[email protected]> wrote:

Quoted 7 lines
Quoted 4 lines

Another way would be to make ml_interface_write write both pieces instead of just a contiguous one. The caml code would then look less suspicious :)

I agree with both Andrew and Samuel that it would be a better fix if Xs_ring.write (== “ml_interface_write”) wrote both pieces at once.

However I believe the ‘suspicious’ OCaml patch fixes the specific issue (— or have I missed something?).

Actually the suspicious OCaml is still buggy in the case that there was a genuine short write followed by a false short write from wrapping the ring.

Quoted 2 lines

Last time I meddled with the C-level ring reading/writing code I didn’t get it quite right. Does anyone have time to prototype what a C-level fix would look like? If we’re short of time I could live with the OCaml-level fix, especially since Wei has done some stress-testing (assuming everyone believes it fixes the issue)

There are several bugs in that function (the remaining size calculations are plain wrong), and Samuels patch sadly doesn't address all of them.

I will throw a different patch together addressing all the issues I can spot.

~Andrew

#10 lore raw
In reply to Wei Liu
Quoted 7 lines · Wei Liu

When oxenstored wrote to the ring, it wrote a chunk of contiguous data. Originally when it tried to write across ring boundary, it returned a short-write when there is still room. That led to stalling mini-os's xenstore thread at times.

Fix this by calling write function for a second time when the first write completes partially.

What happens if the 1st write is short because there is not enough space, and the 2nd write is short because of the ring wraparound? You will still stall.

David

#11 lore raw
In reply to Wei Liu
Quoted 4 lines · Wei Liu

When oxenstored wrote to the ring, it wrote a chunk of contiguous data. Originally when it tried to write across ring boundary, it returned a short-write when there is still room. That led to stalling mini-os's xenstore thread at times.

What is a "short-write" in this context?

Given data bytes 0..M I assumed it is only writing bytes 0..N and not N+1..M because the ring boundary is at N. But what is it writing to the ->prod ring pointer N or M?

AIUI writing N should be allowed by the ring protocol, the client should keep looking for more data until it has a complete request.

Writing M would be a server error.

Ian.

#12 lore raw
Quoted 10 lines · Ian Campbell
Quoted 4 lines · Wei Liu

When oxenstored wrote to the ring, it wrote a chunk of contiguous data. Originally when it tried to write across ring boundary, it returned a short-write when there is still room. That led to stalling mini-os's xenstore thread at times.

What is a "short-write" in this context?

Given data bytes 0..M I assumed it is only writing bytes 0..N and not N+1..M because the ring boundary is at N. But what is it writing to the ->prod ring pointer N or M?

Prod gets incremented by N in this case.

Quoted 5 lines

AIUI writing N should be allowed by the ring protocol, the client should keep looking for more data until it has a complete request.

Writing M would be a server error.

Correct, and this is what is happening.

The server (believes) that the ring is full, when it is not. It waits for the client to make more space in the ring, while the client is waiting for the server to complete its message in the ring, thus stalling.

~Andrew

#13 lore raw
Quoted 16 lines · Andrew Cooper
Quoted 11 lines · Ian Campbell
Quoted 5 lines · Wei Liu

When oxenstored wrote to the ring, it wrote a chunk of contiguous data.
Originally when it tried to write across ring boundary, it returned a short-write when there is still room. That led to stalling mini-os's xenstore thread at times.

What is a "short-write" in this context?

Given data bytes 0..M I assumed it is only writing bytes 0..N and not N+1..M because the ring boundary is at N. But what is it writing to the ->prod ring pointer N or M?

Prod gets incremented by N in this case.

Quoted 1 line
Quoted 7 lines
Quoted 5 lines

AIUI writing N should be allowed by the ring protocol, the client should
keep looking for more data until it has a complete request.

Writing M would be a server error.

Correct, and this is what is happening.

The first or second? Your first comment suggests the first, but your second binds most closely to the second.

Quoted 4 lines

The server (believes) that the ring is full, when it is not. It waits for the client to make more space in the ring, while the client is waiting for the server to complete its message in the ring, thus stalling.

That makes sense, thanks.

I think this needs to be spelled out more fully in the commit message, in particular "server thinks ring is full when it is not" is the most relevant thing, the short write is just how we arrived there.

Ian.

#14 lore raw
Quoted 15 lines · Ian Campbell
Quoted 14 lines · Andrew Cooper
Quoted 11 lines · Ian Campbell
Quoted 5 lines · Wei Liu

When oxenstored wrote to the ring, it wrote a chunk of contiguous data.
Originally when it tried to write across ring boundary, it returned a short-write when there is still room. That led to stalling mini-os's xenstore thread at times.

What is a "short-write" in this context?

Given data bytes 0..M I assumed it is only writing bytes 0..N and not N+1..M because the ring boundary is at N. But what is it writing to the ->prod ring pointer N or M?

Prod gets incremented by N in this case.

Quoted 8 lines
Quoted 6 lines
Quoted 5 lines

AIUI writing N should be allowed by the ring protocol, the client should
keep looking for more data until it has a complete request.

Writing M would be a server error.

Correct, and this is what is happening.

The first or second? Your first comment suggests the first, but your second binds most closely to the second.

Oops yes. Writing M would be an error. Prod currently gets incremented by N.

Quoted 10 lines
Quoted 5 lines

The server (believes) that the ring is full, when it is not. It waits for the client to make more space in the ring, while the client is waiting for the server to complete its message in the ring, thus stalling.

That makes sense, thanks.

I think this needs to be spelled out more fully in the commit message, in particular "server thinks ring is full when it is not" is the most relevant thing, the short write is just how we arrived there.

This patch here is buggy, and superseeded by one of mine which attempts to fix the C stubs.

I need to post a v2.

~Andrew