From nobody Thu May 16 00:29:48 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 1513593853768725.617431243734; Mon, 18 Dec 2017 02:44:13 -0800 (PST) Received: from localhost ([::1]:57818 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eQstp-0004e1-Jp for importer@patchew.org; Mon, 18 Dec 2017 05:43:57 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:48729) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eQsrV-00036n-Gm for qemu-devel@nongnu.org; Mon, 18 Dec 2017 05:41:34 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eQsrR-0002OH-15 for qemu-devel@nongnu.org; Mon, 18 Dec 2017 05:41:33 -0500 Received: from 20.mo1.mail-out.ovh.net ([188.165.45.168]:33037) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1eQsrQ-0002Jy-NG for qemu-devel@nongnu.org; Mon, 18 Dec 2017 05:41:28 -0500 Received: from player691.ha.ovh.net (gw6.ovh.net [213.251.189.206]) by mo1.mail-out.ovh.net (Postfix) with ESMTP id 6430AB2641 for ; Mon, 18 Dec 2017 11:41:26 +0100 (CET) Received: from bahia.lan (lns-bzn-46-82-253-208-248.adsl.proxad.net [82.253.208.248]) (Authenticated sender: groug@kaod.org) by player691.ha.ovh.net (Postfix) with ESMTPA id 31E1126007D; Mon, 18 Dec 2017 11:41:23 +0100 (CET) From: Greg Kurz To: qemu-devel@nongnu.org Date: Mon, 18 Dec 2017 11:41:23 +0100 Message-ID: <151359368310.6820.9701358868942550883.stgit@bahia.lan> In-Reply-To: <151359366858.6820.6567549632253797169.stgit@bahia.lan> References: <151359366858.6820.6567549632253797169.stgit@bahia.lan> User-Agent: StGit/0.17.1-46-g6855-dirty MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Ovh-Tracer-Id: 11193133924827175205 X-VR-SPAMSTATE: OK X-VR-SPAMSCORE: -100 X-VR-SPAMCAUSE: gggruggvucftvghtrhhoucdtuddrgedtuddrgedtgddulecutefuodetggdotefrodftvfcurfhrohhfihhlvgemucfqggfjpdevjffgvefmvefgnecuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 188.165.45.168 Subject: [Qemu-devel] [PATCH v3 1/2] 9pfs: allow PDU to complete without sending a reply X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: keno@juliacomputing.com, Stefano Stabellini , Greg Kurz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 The 9p protocol mostly relies on a request/reply dialog between the client and the server. A notable exception to this rule is request cancellation (ie, flush in 9p wording): the server shouldn't send a reply when the request was flushed. This patch changes the transport API so that the core 9p code may choose to discard a reply instead of pushing it back to the client. As a consequence, the push_and_notify naming isn't quite appropriate anymore. And by the way, this operation also frees ring descriptors allocated by the transport, ie, finalizes the completion of the PDU in the transport layer. It is hence renamed to pdu_complete. This will be used by a subsequent patch to fix request cancellation. Signed-off-by: Greg Kurz Reviewed-by: Stefano Stabellini --- hw/9pfs/9p.c | 10 +++++----- hw/9pfs/9p.h | 2 +- hw/9pfs/virtio-9p-device.c | 11 +++++++---- hw/9pfs/xen-9p-backend.c | 14 ++++++++------ 4 files changed, 21 insertions(+), 16 deletions(-) diff --git a/hw/9pfs/9p.c b/hw/9pfs/9p.c index 1e4ebbe57687..dd8b7c6d69c6 100644 --- a/hw/9pfs/9p.c +++ b/hw/9pfs/9p.c @@ -642,7 +642,7 @@ static void coroutine_fn pdu_complete(V9fsPDU *pdu, ssi= ze_t len) =20 ret =3D pdu_marshal(pdu, len, "s", &str); if (ret < 0) { - goto out_notify; + goto out_complete; } len +=3D ret; id =3D P9_RERROR; @@ -650,7 +650,7 @@ static void coroutine_fn pdu_complete(V9fsPDU *pdu, ssi= ze_t len) =20 ret =3D pdu_marshal(pdu, len, "d", err); if (ret < 0) { - goto out_notify; + goto out_complete; } len +=3D ret; =20 @@ -662,15 +662,15 @@ static void coroutine_fn pdu_complete(V9fsPDU *pdu, s= size_t len) =20 /* fill out the header */ if (pdu_marshal(pdu, 0, "dbw", (int32_t)len, id, pdu->tag) < 0) { - goto out_notify; + goto out_complete; } =20 /* keep these in sync */ pdu->size =3D len; pdu->id =3D id; =20 -out_notify: - pdu->s->transport->push_and_notify(pdu); +out_complete: + pdu->s->transport->pdu_complete(pdu, false); =20 /* Now wakeup anybody waiting in flush for this request */ if (!qemu_co_queue_next(&pdu->complete)) { diff --git a/hw/9pfs/9p.h b/hw/9pfs/9p.h index ffe658ab8975..ea7657837784 100644 --- a/hw/9pfs/9p.h +++ b/hw/9pfs/9p.h @@ -363,7 +363,7 @@ struct V9fsTransport { unsigned int *pniov, size_t size); void (*init_out_iov_from_pdu)(V9fsPDU *pdu, struct iovec **piov, unsigned int *pniov, size_t size); - void (*push_and_notify)(V9fsPDU *pdu); + void (*pdu_complete)(V9fsPDU *pdu, bool discard); }; =20 static inline int v9fs_register_transport(V9fsState *s, const V9fsTranspor= t *t) diff --git a/hw/9pfs/virtio-9p-device.c b/hw/9pfs/virtio-9p-device.c index 43f4e53f336f..083df987fd93 100644 --- a/hw/9pfs/virtio-9p-device.c +++ b/hw/9pfs/virtio-9p-device.c @@ -20,14 +20,17 @@ #include "hw/virtio/virtio-access.h" #include "qemu/iov.h" =20 -static void virtio_9p_push_and_notify(V9fsPDU *pdu) +static void virtio_pdu_complete(V9fsPDU *pdu, bool discard) { V9fsState *s =3D pdu->s; V9fsVirtioState *v =3D container_of(s, V9fsVirtioState, state); VirtQueueElement *elem =3D v->elems[pdu->idx]; =20 - /* push onto queue and notify */ - virtqueue_push(v->vq, elem, pdu->size); + if (discard) { + virtqueue_detach_element(v->vq, elem, 0); + } else { + virtqueue_push(v->vq, elem, pdu->size); + } g_free(elem); v->elems[pdu->idx] =3D NULL; =20 @@ -189,7 +192,7 @@ static const V9fsTransport virtio_9p_transport =3D { .pdu_vunmarshal =3D virtio_pdu_vunmarshal, .init_in_iov_from_pdu =3D virtio_init_in_iov_from_pdu, .init_out_iov_from_pdu =3D virtio_init_out_iov_from_pdu, - .push_and_notify =3D virtio_9p_push_and_notify, + .pdu_complete =3D virtio_pdu_complete, }; =20 static void virtio_9p_device_realize(DeviceState *dev, Error **errp) diff --git a/hw/9pfs/xen-9p-backend.c b/hw/9pfs/xen-9p-backend.c index df2a4100bf55..dc1e6c88885d 100644 --- a/hw/9pfs/xen-9p-backend.c +++ b/hw/9pfs/xen-9p-backend.c @@ -210,7 +210,7 @@ static void xen_9pfs_init_in_iov_from_pdu(V9fsPDU *pdu, *pniov =3D num; } =20 -static void xen_9pfs_push_and_notify(V9fsPDU *pdu) +static void xen_9pfs_pdu_complete(V9fsPDU *pdu, bool discard) { RING_IDX prod; Xen9pfsDev *priv =3D container_of(pdu->s, Xen9pfsDev, state); @@ -222,10 +222,12 @@ static void xen_9pfs_push_and_notify(V9fsPDU *pdu) ring->intf->out_cons =3D ring->out_cons; xen_wmb(); =20 - prod =3D ring->intf->in_prod; - xen_rmb(); - ring->intf->in_prod =3D prod + pdu->size; - xen_wmb(); + if (!discard) { + prod =3D ring->intf->in_prod; + xen_rmb(); + ring->intf->in_prod =3D prod + pdu->size; + xen_wmb(); + } =20 ring->inprogress =3D false; xenevtchn_notify(ring->evtchndev, ring->local_port); @@ -238,7 +240,7 @@ static const V9fsTransport xen_9p_transport =3D { .pdu_vunmarshal =3D xen_9pfs_pdu_vunmarshal, .init_in_iov_from_pdu =3D xen_9pfs_init_in_iov_from_pdu, .init_out_iov_from_pdu =3D xen_9pfs_init_out_iov_from_pdu, - .push_and_notify =3D xen_9pfs_push_and_notify, + .pdu_complete =3D xen_9pfs_pdu_complete, }; =20 static int xen_9pfs_init(struct XenDevice *xendev) From nobody Thu May 16 00:29:48 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 1513593782979808.7849280925072; Mon, 18 Dec 2017 02:43:02 -0800 (PST) Received: from localhost ([::1]:57814 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eQssq-0003q8-SX for importer@patchew.org; Mon, 18 Dec 2017 05:42:56 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:48796) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eQsrd-0003Bs-Np for qemu-devel@nongnu.org; Mon, 18 Dec 2017 05:41:46 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eQsrY-0002i3-QJ for qemu-devel@nongnu.org; Mon, 18 Dec 2017 05:41:41 -0500 Received: from 17.mo1.mail-out.ovh.net ([87.98.179.142]:59409) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eQsrY-0002fG-FX for qemu-devel@nongnu.org; Mon, 18 Dec 2017 05:41:36 -0500 Received: from player691.ha.ovh.net (gw6.ovh.net [213.251.189.206]) by mo1.mail-out.ovh.net (Postfix) with ESMTP id B8998B253A for ; Mon, 18 Dec 2017 11:41:34 +0100 (CET) Received: from bahia.lan (lns-bzn-46-82-253-208-248.adsl.proxad.net [82.253.208.248]) (Authenticated sender: groug@kaod.org) by player691.ha.ovh.net (Postfix) with ESMTPA id 6441E2600C3; Mon, 18 Dec 2017 11:41:31 +0100 (CET) From: Greg Kurz To: qemu-devel@nongnu.org Date: Mon, 18 Dec 2017 11:41:31 +0100 Message-ID: <151359369131.6820.6589067560403425412.stgit@bahia.lan> In-Reply-To: <151359366858.6820.6567549632253797169.stgit@bahia.lan> References: <151359366858.6820.6567549632253797169.stgit@bahia.lan> User-Agent: StGit/0.17.1-46-g6855-dirty MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Ovh-Tracer-Id: 11195385727268460837 X-VR-SPAMSTATE: OK X-VR-SPAMSCORE: -100 X-VR-SPAMCAUSE: gggruggvucftvghtrhhoucdtuddrgedtuddrgedtgddulecutefuodetggdotefrodftvfcurfhrohhfihhlvgemucfqggfjpdevjffgvefmvefgnecuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 87.98.179.142 Subject: [Qemu-devel] [PATCH v3 2/2] 9pfs: Correctly handle cancelled requests X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: keno@juliacomputing.com, Stefano Stabellini , Greg Kurz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 From: Keno Fischer # Background I was investigating spurious non-deterministic EINTR returns from various 9p file system operations in a Linux guest served from the qemu 9p server. ## EINTR, ERESTARTSYS and the linux kernel When a signal arrives that the Linux kernel needs to deliver to user-space while a given thread is blocked (in the 9p case waiting for a reply to its request in 9p_client_rpc -> wait_event_interruptible), it asks whatever driver is currently running to abort its current operation (in the 9p case causing the submission of a TFLUSH message) and return to user space. In these situations, the error message reported is generally ERESTARTSYS. If the userspace processes specified SA_RESTART, this means that the system call will get restarted upon completion of the signal handler delivery (assuming the signal handler doesn't modify the process state in complicated ways not relevant here). If SA_RESTART is not specified, ERESTARTSYS gets translated to EINTR and user space is expected to handle the restart itself. ## The 9p TFLUSH command The 9p TFLUSH commands requests that the server abort an ongoing operation. The man page [1] specifies: ``` If it recognizes oldtag as the tag of a pending transaction, it should abor= t any pending response and discard that tag. [...] When the client sends a Tflush, it must wait to receive the corresponding R= flush before reusing oldtag for subsequent messages. If a response to the flushed= request is received before the Rflush, the client must honor the response as if it = had not been flushed, since the completed request may signify a state change in the= server ``` In particular, this means that the server must not send a reply with the or= ignal tag in response to the cancellation request, because the client is obligated to interpret such a reply as a coincidental reply to the original request. # The bug When qemu receives a TFlush request, it sets the `cancelled` flag on the re= levant pdu. This flag is periodically checked, e.g. in `v9fs_co_name_to_path`, and= if set, the operation is aborted and the error is set to EINTR. However, the s= erver then violates the spec, by returning to the client an Rerror response, rath= er than discarding the message entirely. As a result, the client is required to assume that said Rerror response is a result of the original request, not a result of the cancellation and thus passes the EINTR error back to user s= pace. This is not the worst thing it could do, however as discussed above, the co= rrect error code would have been ERESTARTSYS, such that user space programs with SA_RESTART set get correctly restarted upon completion of the signal handle= r. Instead, such programs get spurious EINTR results that they were not expect= ing to handle. It should be noted that there are plenty of user space programs that do not set SA_RESTART and do not correctly handle EINTR either. However, that is t= hen a userspace bug. It should also be noted that this bug has been mitigated by a recent commit to the Linux kernel [2], which essentially prevents the ker= nel from sending Tflush requests unless the process is about to die (in which c= ase the process likely doesn't care about the response). Nevertheless, for older kernels and to comply with the spec, I believe this change is beneficial. # Implementation The fix is fairly simple, just skipping notification of a reply if the pdu was previously cancelled. We do however, also notify the transport layer that we're doing this, so it can clean up any resources it may be holding. I also added a new trace event to distinguish operations that caused an error reply from those that were cancelled. One complication is that we only omit sending the message on EINTR errors in order to avoid confusing the rest of the code (which may assume that a client knows about a fid if it sucessfully passed it off to pud_complete without checking for cancellation status). This does mean that if the server acts upon the cancellation flag, it always needs to set err to EINTR. I bel= ieve this is true of the current code. [1] https://9fans.github.io/plan9port/man/man9/flush.html [2] https://github.com/torvalds/linux/commit/9523feac272ccad2ad8186ba4fcc89= 103754de52 Signed-off-by: Keno Fischer Reviewed-by: Greg Kurz [groug, use new pdu_complete transport API] Signed-off-by: Greg Kurz --- hw/9pfs/9p.c | 19 ++++++++++++++++++- hw/9pfs/trace-events | 1 + 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/hw/9pfs/9p.c b/hw/9pfs/9p.c index dd8b7c6d69c6..ccbc75265a52 100644 --- a/hw/9pfs/9p.c +++ b/hw/9pfs/9p.c @@ -630,6 +630,23 @@ static void coroutine_fn pdu_complete(V9fsPDU *pdu, ss= ize_t len) V9fsState *s =3D pdu->s; int ret; =20 + /* + * The 9p spec requires that successfully cancelled pdus receive no re= ply. + * Sending a reply would confuse clients because they would + * assume that any EINTR is the actual result of the operation, + * rather than a consequence of the cancellation. However, if + * the operation completed (succesfully or with an error other + * than caused be cancellation), we do send out that reply, both + * for efficiency and to avoid confusing the rest of the state machine + * that assumes passing a non-error here will mean a successful + * transmission of the reply. + */ + bool discard =3D pdu->cancelled && len =3D=3D -EINTR; + if (discard) { + trace_v9fs_rcancel(pdu->tag, pdu->id); + goto out_complete; + } + if (len < 0) { int err =3D -len; len =3D 7; @@ -670,7 +687,7 @@ static void coroutine_fn pdu_complete(V9fsPDU *pdu, ssi= ze_t len) pdu->id =3D id; =20 out_complete: - pdu->s->transport->pdu_complete(pdu, false); + pdu->s->transport->pdu_complete(pdu, discard); =20 /* Now wakeup anybody waiting in flush for this request */ if (!qemu_co_queue_next(&pdu->complete)) { diff --git a/hw/9pfs/trace-events b/hw/9pfs/trace-events index 08a4abf22ea4..1aee350c42f1 100644 --- a/hw/9pfs/trace-events +++ b/hw/9pfs/trace-events @@ -1,6 +1,7 @@ # See docs/devel/tracing.txt for syntax documentation. =20 # hw/9pfs/virtio-9p.c +v9fs_rcancel(uint16_t tag, uint8_t id) "tag %d id %d" v9fs_rerror(uint16_t tag, uint8_t id, int err) "tag %d id %d err %d" v9fs_version(uint16_t tag, uint8_t id, int32_t msize, char* version) "tag = %d id %d msize %d version %s" v9fs_version_return(uint16_t tag, uint8_t id, int32_t msize, char* version= ) "tag %d id %d msize %d version %s"