From nobody Fri Apr 19 15:31:25 2024 Delivered-To: importer@patchew.org Received-SPF: none (zoho.com: 192.237.175.120 is neither permitted nor denied by domain of lists.xenproject.org) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; spf=none (zoho.com: 192.237.175.120 is neither permitted nor denied by domain of lists.xenproject.org) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1557755882; cv=none; d=zoho.com; s=zohoarc; b=iBNZLXvF8XE1x9V1eE8PX01qjKc7kE7gbpDxJqdEV1c0sDj1+ub0n83qCNY6mV8JeP7B3K3BQnZPwKykh6MML0LHabth+rZLL87eXwZB6p7nawfSWer9HmSEyDEilOq84THKTJpxiNSuCxAr9CN5YJ10aYb2UiuAaTEGwU8ZBjM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1557755882; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:To:ARC-Authentication-Results; bh=J3fzWxnJbxxizRaPjvS7A+4YP5sj3aNLcICyHNauO3c=; b=i+i2hPVz0xvw6ijkGpVlrk8MJvgDOY5nmFKkDpUrwGonWpv8M5em+RDxAezInRd5qN6eof04VKrUeUz6h3y35NtAvAopFR8uc5mrg+GF6YsiwSQhu4KroU2YzOAtzrPYTadTnTKnGbbGLuawGONJlz6sJ2TD+QEt2pvQxhwbm7M= ARC-Authentication-Results: i=1; mx.zoho.com; spf=none (zoho.com: 192.237.175.120 is neither permitted nor denied by domain of lists.xenproject.org) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 15577558828541019.1000742890229; Mon, 13 May 2019 06:58:02 -0700 (PDT) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1hQBRe-0005j0-Le; Mon, 13 May 2019 13:56:46 +0000 Received: from all-amaz-eas1.inumbo.com ([34.197.232.57] helo=us1-amaz-eas2.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1hQBRd-0005it-3w for xen-devel@lists.xenproject.org; Mon, 13 May 2019 13:56:45 +0000 Received: from SMTP03.CITRIX.COM (unknown [162.221.156.55]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTPS id ef870b78-7586-11e9-80b1-835e1d15a431; Mon, 13 May 2019 13:56:42 +0000 (UTC) X-Inumbo-ID: ef870b78-7586-11e9-80b1-835e1d15a431 X-IronPort-AV: E=Sophos;i="5.60,465,1549929600"; d="scan'208";a="85388141" From: Ross Lagerwall To: Date: Mon, 13 May 2019 14:56:35 +0100 Message-ID: <20190513135635.22406-1-ross.lagerwall@citrix.com> X-Mailer: git-send-email 2.17.2 MIME-Version: 1.0 Subject: [Xen-devel] [PATCH v2] xenbus: Avoid deadlock during suspend due to open transactions X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: Juergen Gross , Ross Lagerwall , Boris Ostrovsky Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" During a suspend/resume, the xenwatch thread waits for all outstanding xenstore requests and transactions to complete. This does not work correctly for transactions started by userspace because it waits for them to complete after freezing userspace threads which means the transactions have no way of completing, resulting in a deadlock. This is trivial to reproduce by running this script and then suspending the VM: import pyxs, time c =3D pyxs.client.Client(xen_bus_path=3D"/dev/xen/xenbus") c.connect() c.transaction() time.sleep(3600) Even if this deadlock were resolved, misbehaving userspace should not prevent a VM from being migrated. So, instead of waiting for these transactions to complete before suspending, store the current generation id for each transaction when it is started. The global generation id is incremented during resume. If the caller commits the transaction and the generation id does not match the current generation id, return EAGAIN so that they try again. If the transaction was instead discarded, return OK since no changes were made anyway. This only affects users of the xenbus file interface. In-kernel users of xenbus are assumed to be well-behaved and complete all transactions before freezing. Signed-off-by: Ross Lagerwall Reviewed-by: Juergen Gross --- Changed in v2: rewrote according to Juergen's suggestion. drivers/xen/xenbus/xenbus.h | 3 +++ drivers/xen/xenbus/xenbus_dev_frontend.c | 18 ++++++++++++++++++ drivers/xen/xenbus/xenbus_xs.c | 7 +++++-- 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/drivers/xen/xenbus/xenbus.h b/drivers/xen/xenbus/xenbus.h index 092981171df1..d75a2385b37c 100644 --- a/drivers/xen/xenbus/xenbus.h +++ b/drivers/xen/xenbus/xenbus.h @@ -83,6 +83,7 @@ struct xb_req_data { int num_vecs; int err; enum xb_req_state state; + bool user_req; void (*cb)(struct xb_req_data *); void *par; }; @@ -133,4 +134,6 @@ void xenbus_ring_ops_init(void); int xenbus_dev_request_and_reply(struct xsd_sockmsg *msg, void *par); void xenbus_dev_queue_reply(struct xb_req_data *req); =20 +extern unsigned int xb_dev_generation_id; + #endif diff --git a/drivers/xen/xenbus/xenbus_dev_frontend.c b/drivers/xen/xenbus/= xenbus_dev_frontend.c index 0782ff3c2273..39c63152a358 100644 --- a/drivers/xen/xenbus/xenbus_dev_frontend.c +++ b/drivers/xen/xenbus/xenbus_dev_frontend.c @@ -62,6 +62,8 @@ =20 #include "xenbus.h" =20 +unsigned int xb_dev_generation_id; + /* * An element of a list of outstanding transactions, for which we're * still waiting a reply. @@ -69,6 +71,7 @@ struct xenbus_transaction_holder { struct list_head list; struct xenbus_transaction handle; + unsigned int generation_id; }; =20 /* @@ -441,6 +444,7 @@ static int xenbus_write_transaction(unsigned msg_type, rc =3D -ENOMEM; goto out; } + trans->generation_id =3D xb_dev_generation_id; list_add(&trans->list, &u->transactions); } else if (msg->hdr.tx_id !=3D 0 && !xenbus_get_transaction(u, msg->hdr.tx_id)) @@ -449,6 +453,20 @@ static int xenbus_write_transaction(unsigned msg_type, !(msg->hdr.len =3D=3D 2 && (!strcmp(msg->body, "T") || !strcmp(msg->body, "F")))) return xenbus_command_reply(u, XS_ERROR, "EINVAL"); + else if (msg_type =3D=3D XS_TRANSACTION_END) { + trans =3D xenbus_get_transaction(u, msg->hdr.tx_id); + if (trans && trans->generation_id !=3D xb_dev_generation_id) { + list_del(&trans->list); + kfree(trans); + if (!strcmp(msg->body, "T")) + return xenbus_command_reply(u, XS_ERROR, + "EAGAIN"); + else + return xenbus_command_reply(u, + XS_TRANSACTION_END, + "OK"); + } + } =20 rc =3D xenbus_dev_request_and_reply(&msg->hdr, u); if (rc && trans) { diff --git a/drivers/xen/xenbus/xenbus_xs.c b/drivers/xen/xenbus/xenbus_xs.c index 49a3874ae6bb..ddc18da61834 100644 --- a/drivers/xen/xenbus/xenbus_xs.c +++ b/drivers/xen/xenbus/xenbus_xs.c @@ -105,6 +105,7 @@ static void xs_suspend_enter(void) =20 static void xs_suspend_exit(void) { + xb_dev_generation_id++; spin_lock(&xs_state_lock); xs_suspend_active--; spin_unlock(&xs_state_lock); @@ -125,7 +126,7 @@ static uint32_t xs_request_enter(struct xb_req_data *re= q) spin_lock(&xs_state_lock); } =20 - if (req->type =3D=3D XS_TRANSACTION_START) + if (req->type =3D=3D XS_TRANSACTION_START && !req->user_req) xs_state_users++; xs_state_users++; rq_id =3D xs_request_id++; @@ -140,7 +141,7 @@ void xs_request_exit(struct xb_req_data *req) spin_lock(&xs_state_lock); xs_state_users--; if ((req->type =3D=3D XS_TRANSACTION_START && req->msg.type =3D=3D XS_ERR= OR) || - (req->type =3D=3D XS_TRANSACTION_END && + (req->type =3D=3D XS_TRANSACTION_END && !req->user_req && !WARN_ON_ONCE(req->msg.type =3D=3D XS_ERROR && !strcmp(req->body, "ENOENT")))) xs_state_users--; @@ -286,6 +287,7 @@ int xenbus_dev_request_and_reply(struct xsd_sockmsg *ms= g, void *par) req->num_vecs =3D 1; req->cb =3D xenbus_dev_queue_reply; req->par =3D par; + req->user_req =3D true; =20 xs_send(req, msg); =20 @@ -313,6 +315,7 @@ static void *xs_talkv(struct xenbus_transaction t, req->vec =3D iovec; req->num_vecs =3D num_vecs; req->cb =3D xs_wake_up; + req->user_req =3D false; =20 msg.req_id =3D 0; msg.tx_id =3D t.id; --=20 2.17.2 _______________________________________________ Xen-devel mailing list Xen-devel@lists.xenproject.org https://lists.xenproject.org/mailman/listinfo/xen-devel