Require that xenstored writes to a domain complete in a single chunk
PATCH 1/1 Require that xenstored writes to a domain complete in a single chunk
If xenstored is part-way through writing a header+payload into the buffer shared with a guest domain when the guest domain decides to suspend, the buffer is corrupted, as xenstored doesn't know that it has a partial write to complete when the domain revives. The domain is expecting proper completion of the partial header+payload and is disappointed.
The attached patch modifies xenstored such that it checks for sufficient space for header+payload before making any changes to the shared buffer.
It is against 3.0.4-1, but the code in unstable looks the same.
Require that writes to a domain complete in a single chunk (i.e both header and payload are written at the same time) to avoid leaving the ring in an inconsistent state across suspend/resume.
tools/xenstore/xenstored_core.c +45 -15
@@ -264,10 +264,17 @@ static bool write_messages(struct connec {int ret;struct buffered_data *out; +unsigned int space_required =0 ; +bool r = false; out = list_top(&conn->out_list, struct buffered_data, list);if (out ==NULL )return true; + +if (out->inhdr) + space_required +=sizeof (out->hdr); + space_required += out->hdr.msg.len; + space_required -= out->used;if (out->inhdr) {if (verbose)
@@ -277,36 +284,59 @@ static bool write_messages(struct connec out->buffer, conn); ret = conn->write (conn, out->hdr.raw + out->used,sizeof (out->hdr) - out->used); -if (ret <0 ) -return false; - +if (ret <0 ) { + r = false; + goto done; + } + + space_required -= ret; out->used += ret; -if (out->used <sizeof (out->hdr)) -return true; +if (out->used <sizeof (out->hdr)) { + r = true; + goto done; + } out->inhdr = false; out->used =0 ;/* Second write might block if non-zero. */ -if (out->hdr.msg.len && !conn->domain) -return true; +if ((out->hdr.msg.len !=0 ) && + (conn->domain ==NULL )) { + r = true; + goto done; + } } ret = conn->write (conn, out->buffer + out->used, out->hdr.msg.len - out->used); -if (ret <0 ) -return false; - +if (ret <0 ) { + r = false; + goto done; + } + + space_required -= ret; out->used += ret; -if (out->used != out->hdr.msg.len) -return true; - +if (out->used != out->hdr.msg.len) { + r = true; + goto done; + } + + r = true;trace_io (conn,"OUT" , out);list_del (&out->list);talloc_free (out); -return true; +done: +if ((conn->domain !=NULL ) && (space_required !=0 )) +/* +* Not all of the data was consumed. This can leave +* the shared ring pointers in an inconsistent state +* across suspend/resume. +*/ +eprintf ("partial write to shared page of domain" ); + +return r; }static int destroy_conn (void *_conn)
@@ -1997,7 +2027,7 @@ int main(int argc, char *argv[]) goto more; } -if (domain_can_write (i) && !list_empty (&i->out_list)) { +if (domain_can_write (i)) {handle_output (i); goto more; }
tools/xenstore/xenstored_domain.c +47 -18
77 lines not on this page · raw message
dme.
Signature · 1 line
David Edmondson, Sun Microsystems, http://www.dme.org
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel
Quoted 7 lines · David Edmondson
If xenstored is part-way through writing a header+payload into the buffer shared with a guest domain when the guest domain decides to suspend, the buffer is corrupted, as xenstored doesn't know that it has a partial write to complete when the domain revives. The domain is expecting proper completion of the partial header+payload and is disappointed.
Quoted 5 lines
The attached patch modifies xenstored such that it checks for sufficient space for header+payload before making any changes to the shared buffer.
It is against 3.0.4-1, but the code in unstable looks the same.
This seems dubious. There's no reason we might not have payloads bigger than the ring size (which is only 1kB).
The right fix would be in the guest, which should already be stopping any transactions or commands across save/restore. Does this problem occur when xenstored sends an asynchronous watch-fired message? Probably the packet-reading thread should be interrupted and put to sleep before suspending.
For older guest compatibility perhaps we can take a variant of your patch that only waits for enough space is the entire message fits in the ring in one go. This would be 'best-effort' at compatibility while not precluding use of larger messages in general.
-- Keir
* [email protected] [2007-02-26 17:20:34]
Quoted 2 lines
This seems dubious. There's no reason we might not have payloads bigger than the ring size (which is only 1kB).
Agreed.
Quoted 4 lines
The right fix would be in the guest, which should already be stopping any transactions or commands across save/restore. Does this problem occur when xenstored sends an asynchronous watch-fired message?
All of the cases I examined (a few dozen) were for watch events.
Quoted 2 lines
Probably the packet-reading thread should be interrupted and put to sleep before suspending.
I'll look at this.
Quoted 4 lines
For older guest compatibility perhaps we can take a variant of your patch that only waits for enough space is the entire message fits in the ring in one go. This would be 'best-effort' at compatibility while not precluding use of larger messages in general.
Is the implication that you think that this problem could occur with a Linux guest (I've never seen it, though have tested much less)?
dme.
Signature · 1 line
David Edmondson, Sun Microsystems, http://www.dme.org
Quoted 7 lines · David Edmondson
Quoted 4 lines
For older guest compatibility perhaps we can take a variant of your patch that only waits for enough space is the entire message fits in the ring in one go. This would be 'best-effort' at compatibility while not precluding use of larger messages in general.
Is the implication that you think that this problem could occur with a Linux guest (I've never seen it, though have tested much less)?
The Linux suspend thread does not sync with the xenbus reader thread at all. I'm not sure why we've never seen any problems on Linux, but I guess it's rare that a message cannot be sent all in one go. Especially a watch event, as those are usually fairly short.
Oh..... Wait a minute! On Linux we explicitly tear down watches before suspend. Or at least we used to, before a patch of a few weeks ago (c/s 13519, Jan 19th 2007). This would save us because no watches registered -> no watches fire. Do you not have anything similar to this in your xenbus code (presumably you took the dual-licensed files as a basis for the Solaris implementation)?
So Linux now needs fixing too, but the bug window here has been only a few weeks and no supported kernel releases include the bug. This being the case we should probably just fix this issue in current guest kernels.
-- Keir
Quoted 4 lines · Keir Fraser
The Linux suspend thread does not sync with the xenbus reader thread at all. I'm not sure why we've never seen any problems on Linux, but I guess it's rare that a message cannot be sent all in one go. Especially a watch event, as those are usually fairly short.
Presumably we have to make sure we've waited for all threads waiting for a response to have woken up from the sleep-until-reply too, though; I'm not sure we do that.
regards
john
Quoted 8 lines · John Levon
Quoted 4 lines
The Linux suspend thread does not sync with the xenbus reader thread at all. I'm not sure why we've never seen any problems on Linux, but I guess it's rare that a message cannot be sent all in one go. Especially a watch event, as those are usually fairly short.
Presumably we have to make sure we've waited for all threads waiting for a response to have woken up from the sleep-until-reply too, though; I'm not sure we do that.
Request-response pairs are serialised on a mutex. This mutex is locked out across save/restore, which means that save/restore cannot proceed until any outstanding response has been fully received. There's nothing even halfway tricky going on here. :-)
-- Keir