From patchwork Fri Sep 21 08:22:12 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Roman Kagan X-Patchwork-Id: 10609387 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 8493015A6 for ; Fri, 21 Sep 2018 09:01:36 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 728132D890 for ; Fri, 21 Sep 2018 09:01:36 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 6644D2D895; Fri, 21 Sep 2018 09:01:36 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.8 required=2.0 tests=BAD_ENC_HEADER,BAYES_00, DKIM_SIGNED,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 5F9CA2D890 for ; Fri, 21 Sep 2018 09:01:35 +0000 (UTC) Received: from localhost ([::1]:54627 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1g3HJe-0003AW-72 for patchwork-qemu-devel@patchwork.kernel.org; Fri, 21 Sep 2018 05:01:34 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:49116) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1g3Gi3-0006rF-ER for qemu-devel@nongnu.org; Fri, 21 Sep 2018 04:22:44 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1g3Gi0-00029l-4B for qemu-devel@nongnu.org; Fri, 21 Sep 2018 04:22:43 -0400 Received: from mail-eopbgr70091.outbound.protection.outlook.com ([40.107.7.91]:35048 helo=EUR04-HE1-obe.outbound.protection.outlook.com) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1g3Ghz-00027r-Je for qemu-devel@nongnu.org; Fri, 21 Sep 2018 04:22:40 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=virtuozzo.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=h1TF3puJSDhV4pijmNyVfa1Qzqz5IWEO/QCphaTlqic=; b=iQKE+zrDm/fUwuPZXKxYAA++TxJ+9Y3YyJL0eXK5zFeAI0S6tiJMOF99PHNoULB90AY3ehNXf7qpjIuh8ZM3AjRpzuz9YHuN5lIc4cgOLANwwgEY6LnAh48zkZuy5Djlzx03ARvFPMBn0II5zaEOiB03is5vIg1VCWSBeOiWH9U= Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=rkagan@virtuozzo.com; Received: from rkaganb.sw.ru (185.231.240.5) by AM0PR08MB3588.eurprd08.prod.outlook.com (2603:10a6:208:e1::30) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.1143.18; Fri, 21 Sep 2018 08:22:36 +0000 From: Roman Kagan To: qemu-devel@nongnu.org Date: Fri, 21 Sep 2018 11:22:12 +0300 Message-Id: <20180921082217.29481-6-rkagan@virtuozzo.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180921082217.29481-1-rkagan@virtuozzo.com> References: <20180921082217.29481-1-rkagan@virtuozzo.com> MIME-Version: 1.0 X-Originating-IP: [185.231.240.5] X-ClientProxiedBy: VI1P193CA0003.EURP193.PROD.OUTLOOK.COM (2603:10a6:800:bd::13) To AM0PR08MB3588.eurprd08.prod.outlook.com (2603:10a6:208:e1::30) X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 3a02a6a1-074b-4c23-3f59-08d61f9b63b4 X-Microsoft-Antispam: BCL:0; PCL:0; RULEID:(7020095)(4652040)(8989299)(4534165)(4627221)(201703031133081)(201702281549075)(8990200)(5600074)(711020)(2017052603328)(7153060)(7193020); SRVR:AM0PR08MB3588; X-Microsoft-Exchange-Diagnostics: 1; AM0PR08MB3588; 3:GcISga/XmIBueht9pv3lQhoVfY4sY8CNrFtEe4L0xmTPdWLfPJnu8+xEaK4MIitEZOnfhCKBygftXR+tT+fBTHFk9wCy+VMfZqtLeQaSunmSieqFMMWx/hFmkpEe4bEXc0fm8lrnxDmIvuvNOlTSbVl5mdv9frPp21EmkKYEU6DBJlYbnBzeL+788I3C3MZGKyjrGyFF4R6n8nBVxEzrr4UN5v/7eVjadCFD4lvaxITqLAgfGgdMYnxqsI1pe+gQ; 25:0wFBoz1mX+EIXyEmgw8ZI7vHzsDZcCBU0XhNOxkf7pUdMzXhnppsZe/mJCYqVSLY5Tdp/AqwEH+omLfyy/XmtayQZSMcKFs8bECw1R2SiaxHp//fRNFpYFaFtGQmeFci5mjDglZeBSAZ3HExXl/VdjgVrXnzoyKmI0ym5fCYzF8hibaCwr0xuiSEUJiEqh6MsOumk0qnrLNSo1hmUakeH3mFd9mLpR+Rndscbaf87hyZ6u8bk4V9RdiMijk+N86x4W1MCctISvrwPAGr+zSniiJJnSsHpo+NAbDA/P4+wgi1fa15mH50N+/HaWVPY2WMhsmwIHf1BJBWWjb3ZOcgPg==; 31:96kQDGZy0nrmMz8tuoPdApXR7qv8Ln0CY1kPoVsfQvBFoTCE/cnoLI9xJ0ffBBp8PgQ7q2PcfH7Y37Wcf2oE/BU/CfBzhPFAWt9mSZV3qJt124jPCc32/j9Q6+cwz+1BBZ/tRW17LZC/MuG8eSd9UW1xK1d6bhN7lDXwL5Lomg/ZKc7o8nkbhxYxMEzwe3Z4EncH9QdaLThoHDExeWo7gsuuH0uobY3LIm9eMzWqwbA= X-MS-TrafficTypeDiagnostic: AM0PR08MB3588: X-Microsoft-Exchange-Diagnostics: 1; AM0PR08MB3588; 20:rFI6jqVzfCzDBvLfEEhne/tCGQ+bp2YwVwAMrE/lxkMCUFJnU8ZdCs2lUGL4sHNxBQaTBnZDAWSKdcgjB3gZ+Yoo0pWj3I/2KGDn4k3Mf2fmQALhx615mZPo6UbJRzg6g7Du6xUBI2VPHPGYUz00s+dPVUDPNHrQ9rOEI14KIceENYTnOAHceuZ5JjsVPnJl1TsRtfBgOg4CFmgDhfVHOJsGEMxDiHHhSO6uDAY2Og0ASZlJB4tjmqLJLTT3Vv3aUx9ODKHzgHyutCNyEUfMY/V4zdl7X7hv/6RXI+Q5lkM/k5wkIsiFfVGLZeUOd7rLVq35bw9nLMxjprCCDu+0cd21BPRhb6P+iN13ZJck6YlTpT+Ohs4H2XO6XchNwcgPhVyVqvcEs2uqWKqWB+o/kDqwPRbG3OjQvA4kKIvB2oBYNshhtuGUYcj+vlgSF5QMMHcYwArYqM2eV7/H9gm6wZqw8DcqQTOxLhbc2+HjqWe4gfA/PiaNQ7c6aJuUFJmR; 4:2iGG3Eump7ysLIjxLL7yV/FemuyeklXqEeKKsprtmTkTxclkvzitJ4cjVW9Y6Pd4NnJNdQnuBHyVTzC07CSmYk14p/rAZjivrR7YC0Dgb+AioC1XOX9Z0PL9GK//LWofGTV3yO8s5AgJP7PvmSOzARIFYnyLudC7IadCZ4EJFwJvVmJQKkIu3Og3fwdt9lGVCqzhC+3cPebA9He3UYyFO4YftDhOoOLdXvBR91ou+xqqN6MRxcPpICPSG+FvjJNCN4SxYyun62d9LCfPBJ9blg== X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:; X-MS-Exchange-SenderADCheck: 1 X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(6040522)(2401047)(8121501046)(5005006)(93006095)(93001095)(3231355)(944501410)(52105095)(10201501046)(3002001)(149027)(150027)(6041310)(20161123558120)(201703131423095)(201702281528075)(20161123555045)(201703061421075)(201703061406153)(20161123562045)(20161123560045)(20161123564045)(201708071742011)(7699051); SRVR:AM0PR08MB3588; BCL:0; PCL:0; RULEID:; SRVR:AM0PR08MB3588; X-Forefront-PRVS: 0802ADD973 X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10019020)(346002)(136003)(396003)(39850400004)(366004)(376002)(189003)(199004)(6916009)(16526019)(5660300001)(36756003)(3846002)(54906003)(48376002)(6506007)(476003)(486006)(14444005)(52116002)(446003)(1076002)(76176011)(386003)(51416003)(186003)(6116002)(16586007)(15650500001)(2906002)(66066001)(6512007)(7416002)(50226002)(53936002)(25786009)(106356001)(53416004)(81166006)(47776003)(316002)(956004)(11346002)(2616005)(7736002)(8936002)(305945005)(8676002)(2351001)(105586002)(97736004)(4326008)(86362001)(6666003)(68736007)(478600001)(6486002)(2361001)(50466002)(26005)(69596002)(81156014); DIR:OUT; SFP:1102; SCL:1; SRVR:AM0PR08MB3588; H:rkaganb.sw.ru; FPR:; SPF:None; LANG:en; PTR:InfoNoRecords; MX:1; A:1; Received-SPF: None (protection.outlook.com: virtuozzo.com does not designate permitted sender hosts) X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1; AM0PR08MB3588; 23:g0nYmrljPNIj88b4QMEMFfBZkSO5FMfhQds5CDTWW?= myWp0Uajfe0BTrfJGDyBmlxwZ9GR7fVpdlLuRLhYRWEil9mcY3jnEebS43UgPOWaujLqBuOFdgA3gOeINUI9+AwRcuLALi1EfYqgk9bH0YlSfNdtJQ2mllrtL1SNiaBjHyoDokqMZq4I8ZN0OLidqG8/IFDQEhATAxn1st7iaKjOz6Z4hTBspLKu8n3Rx+fJItxXb2Xyyu8vJPBs2Y2ptSO7AU1EKLp/2obRVjXbRBKAPO8U67gNIjP/UqcApZsNnHAEtrxaAMRYUSL9i7ggxlfjS1n0FcM66YSIN9At6bAgdTioB2jGYjZD7aKS5MQqUL76e5UD5RUsVFNQpZ/ale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Yi+jb7m9W4waCrX/GwDXVKY89QZIgOaueUDD9/0rGZPSOilu44nfg6gXI00h0j3TjB/V6o/naA6D34SR9Iap38c72D7eeu/DjWt/7MNpQNoLRYwHNyMtin3KaPGSaSdyNr8f7adhvCDsCXAwYDnJ12ldJeCP7UBeVo SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1; AM0PR08MB3588; 20:8nSftaQ6AjNqfLEgrU6T+QtVDVNw6xncmLWm57rFVciyCs6DfCCykvqbyeQtwTgA5SdoL8NMABXHthbgDxQ4jZ+MbZe4QUkxc/6RF75QpcghNr1105ioUyL0wyMkHtcOm1eQ3SehPBi3COnWXSBkOGSPlg/CB6vegrcchIMX+Is= X-OriginatorOrg: virtuozzo.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 21 Sep 2018 08:22:36.1563 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 3a02a6a1-074b-4c23-3f59-08d61f9b63b4 X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 0bc7f26d-0264-416e-a6fc-8352af79c58f X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM0PR08MB3588 X-detected-operating-system: by eggs.gnu.org: Windows 7 or 8 [fuzzy] X-Received-From: 40.107.7.91 Subject: [Qemu-devel] [PATCH 05/10] hyperv: add synic message delivery 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: Vijayabhaskar Balakrishna , Eduardo Habkost , Konrad Rzeszutek Wilk , "Michael S. Tsirkin" , Venu Busireddy , Liran Alon , Paolo Bonzini , Si-Wei Liu , Igor Mammedov , Boris Ostrovsky , Karl Heubaum Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP Add infrastructure to deliver SynIC messages to the SynIC message page. Note that KVM may also want to deliver (SynIC timer) messages to the same message slot. The problem is that the access to a SynIC message slot is controlled by the value of its .msg_type field which indicates if the slot is being owned by the hypervisor (zero) or by the guest (non-zero). This leaves no room for synchronizing multiple concurrent producers. The simplest way to deal with this for both KVM and QEMU is to only deliver messages in the vcpu thread. KVM already does this; this patch makes it for QEMU, too. Specifically, - add a function for posting messages, which only copies the message into the staging buffer if its free, and schedules a work on the corresponding vcpu to actually deliver it to the guest slot; - instead of a sint ack callback, set up the sint route with a message status callback. This function is called in a bh whenever there are updates to the message slot status: either the vcpu made definitive progress delivering the message from the staging buffer (succeeded or failed) or the guest issued EOM; the status is passed as an argument to the callback. Signed-off-by: Roman Kagan --- include/hw/hyperv/hyperv.h | 18 ++++- hw/hyperv/hyperv.c | 162 ++++++++++++++++++++++++++++++++++--- 2 files changed, 166 insertions(+), 14 deletions(-) diff --git a/include/hw/hyperv/hyperv.h b/include/hw/hyperv/hyperv.h index 6fba4762c8..82d561fc88 100644 --- a/include/hw/hyperv/hyperv.h +++ b/include/hw/hyperv/hyperv.h @@ -11,18 +11,30 @@ #define HW_HYPERV_HYPERV_H #include "cpu-qom.h" +#include "hw/hyperv/hyperv-proto.h" typedef struct HvSintRoute HvSintRoute; -typedef void (*HvSintAckClb)(void *data); + +/* + * Callback executed in a bottom-half when the status of posting the message + * becomes known, before unblocking the connection for further messages + */ +typedef void (*HvSintMsgCb)(void *data, int status); HvSintRoute *hyperv_sint_route_new(uint32_t vp_index, uint32_t sint, - HvSintAckClb sint_ack_clb, - void *sint_ack_clb_data); + HvSintMsgCb cb, void *cb_data); void hyperv_sint_route_ref(HvSintRoute *sint_route); void hyperv_sint_route_unref(HvSintRoute *sint_route); int hyperv_sint_route_set_sint(HvSintRoute *sint_route); +/* + * Submit a message to be posted in vcpu context. If the submission succeeds, + * the status of posting the message is reported via the callback associated + * with the @sint_route; until then no more messages are accepted. + */ +int hyperv_post_msg(HvSintRoute *sint_route, struct hyperv_message *msg); + static inline uint32_t hyperv_vp_index(CPUState *cs) { return cs->cpu_index; diff --git a/hw/hyperv/hyperv.c b/hw/hyperv/hyperv.c index 70cf129d04..21217631dc 100644 --- a/hw/hyperv/hyperv.c +++ b/hw/hyperv/hyperv.c @@ -148,14 +148,51 @@ static void synic_register_types(void) type_init(synic_register_types) +/* + * KVM has its own message producers (SynIC timers). To guarantee + * serialization with both KVM vcpu and the guest cpu, the messages are first + * staged in an intermediate area and then posted to the SynIC message page in + * the vcpu thread. + */ +typedef struct HvSintStagedMesage { + /* message content staged by hyperv_post_msg */ + struct hyperv_message msg; + /* callback + data (r/o) to complete the processing in a BH */ + HvSintMsgCb cb; + void *cb_data; + /* message posting status filled by cpu_post_msg */ + int status; + /* passing the buck: */ + enum { + /* initial state */ + HV_STAGED_MSG_FREE, + /* + * hyperv_post_msg (e.g. in main loop) grabs the staged area (FREE -> + * BUSY), copies msg, and schedules cpu_post_msg on the assigned cpu + */ + HV_STAGED_MSG_BUSY, + /* + * cpu_post_msg (vcpu thread) tries to copy staged msg to msg slot, + * notify the guest, records the status, marks the posting done (BUSY + * -> POSTED), and schedules sint_msg_bh BH + */ + HV_STAGED_MSG_POSTED, + /* + * sint_msg_bh (BH) verifies that the posting is done, runs the + * callback, and starts over (POSTED -> FREE) + */ + } state; +} HvSintStagedMesage; + struct HvSintRoute { uint32_t sint; SynICState *synic; int gsi; EventNotifier sint_set_notifier; EventNotifier sint_ack_notifier; - HvSintAckClb sint_ack_clb; - void *sint_ack_clb_data; + + HvSintStagedMesage *staged_msg; + unsigned refcount; }; @@ -166,17 +203,115 @@ static CPUState *hyperv_find_vcpu(uint32_t vp_index) return cs; } -static void kvm_hv_sint_ack_handler(EventNotifier *notifier) +/* + * BH to complete the processing of a staged message. + */ +static void sint_msg_bh(void *opaque) +{ + HvSintRoute *sint_route = opaque; + HvSintStagedMesage *staged_msg = sint_route->staged_msg; + + if (atomic_read(&staged_msg->state) != HV_STAGED_MSG_POSTED) { + /* status nor ready yet (spurious ack from guest?), ignore */ + return; + } + + staged_msg->cb(staged_msg->cb_data, staged_msg->status); + staged_msg->status = 0; + + /* staged message processing finished, ready to start over */ + atomic_set(&staged_msg->state, HV_STAGED_MSG_FREE); + /* drop the reference taken in hyperv_post_msg */ + hyperv_sint_route_unref(sint_route); +} + +/* + * Worker to transfer the message from the staging area into the SynIC message + * page in vcpu context. + */ +static void cpu_post_msg(CPUState *cs, run_on_cpu_data data) +{ + HvSintRoute *sint_route = data.host_ptr; + HvSintStagedMesage *staged_msg = sint_route->staged_msg; + SynICState *synic = sint_route->synic; + struct hyperv_message *dst_msg; + bool wait_for_sint_ack = false; + + assert(staged_msg->state == HV_STAGED_MSG_BUSY); + + if (!synic->enabled || !synic->msg_page_addr) { + staged_msg->status = -ENXIO; + goto posted; + } + + dst_msg = &synic->msg_page->slot[sint_route->sint]; + + if (dst_msg->header.message_type != HV_MESSAGE_NONE) { + dst_msg->header.message_flags |= HV_MESSAGE_FLAG_PENDING; + staged_msg->status = -EAGAIN; + wait_for_sint_ack = true; + } else { + memcpy(dst_msg, &staged_msg->msg, sizeof(*dst_msg)); + staged_msg->status = hyperv_sint_route_set_sint(sint_route); + } + + memory_region_set_dirty(&synic->msg_page_mr, 0, sizeof(*synic->msg_page)); + +posted: + atomic_set(&staged_msg->state, HV_STAGED_MSG_POSTED); + /* + * Notify the msg originator of the progress made; if the slot was busy we + * set msg_pending flag in it so it will be the guest who will do EOM and + * trigger the notification from KVM via sint_ack_notifier + */ + if (!wait_for_sint_ack) { + aio_bh_schedule_oneshot(qemu_get_aio_context(), sint_msg_bh, + sint_route); + } +} + +/* + * Post a Hyper-V message to the staging area, for delivery to guest in the + * vcpu thread. + */ +int hyperv_post_msg(HvSintRoute *sint_route, struct hyperv_message *src_msg) +{ + HvSintStagedMesage *staged_msg = sint_route->staged_msg; + + assert(staged_msg); + + /* grab the staging area */ + if (atomic_cmpxchg(&staged_msg->state, HV_STAGED_MSG_FREE, + HV_STAGED_MSG_BUSY) != HV_STAGED_MSG_FREE) { + return -EAGAIN; + } + + memcpy(&staged_msg->msg, src_msg, sizeof(*src_msg)); + + /* hold a reference on sint_route until the callback is finished */ + hyperv_sint_route_ref(sint_route); + + /* schedule message posting attempt in vcpu thread */ + async_run_on_cpu(sint_route->synic->cs, cpu_post_msg, + RUN_ON_CPU_HOST_PTR(sint_route)); + return 0; +} + +static void sint_ack_handler(EventNotifier *notifier) { HvSintRoute *sint_route = container_of(notifier, HvSintRoute, sint_ack_notifier); event_notifier_test_and_clear(notifier); - sint_route->sint_ack_clb(sint_route->sint_ack_clb_data); + + /* + * the guest consumed the previous message so complete the current one with + * -EAGAIN and let the msg originator retry + */ + aio_bh_schedule_oneshot(qemu_get_aio_context(), sint_msg_bh, sint_route); } HvSintRoute *hyperv_sint_route_new(uint32_t vp_index, uint32_t sint, - HvSintAckClb sint_ack_clb, - void *sint_ack_clb_data) + HvSintMsgCb cb, void *cb_data) { HvSintRoute *sint_route; EventNotifier *ack_notifier; @@ -200,14 +335,19 @@ HvSintRoute *hyperv_sint_route_new(uint32_t vp_index, uint32_t sint, goto err; } - ack_notifier = sint_ack_clb ? &sint_route->sint_ack_notifier : NULL; + + ack_notifier = cb ? &sint_route->sint_ack_notifier : NULL; if (ack_notifier) { + sint_route->staged_msg = g_new0(HvSintStagedMesage, 1); + sint_route->staged_msg->cb = cb; + sint_route->staged_msg->cb_data = cb_data; + r = event_notifier_init(ack_notifier, false); if (r) { goto err_sint_set_notifier; } - event_notifier_set_handler(ack_notifier, kvm_hv_sint_ack_handler); + event_notifier_set_handler(ack_notifier, sint_ack_handler); } gsi = kvm_irqchip_add_hv_sint_route(kvm_state, vp_index, sint); @@ -222,8 +362,6 @@ HvSintRoute *hyperv_sint_route_new(uint32_t vp_index, uint32_t sint, goto err_irqfd; } sint_route->gsi = gsi; - sint_route->sint_ack_clb = sint_ack_clb; - sint_route->sint_ack_clb_data = sint_ack_clb_data; sint_route->synic = synic; sint_route->sint = sint; sint_route->refcount = 1; @@ -236,6 +374,7 @@ err_gsi: if (ack_notifier) { event_notifier_set_handler(ack_notifier, NULL); event_notifier_cleanup(ack_notifier); + g_free(sint_route->staged_msg); } err_sint_set_notifier: event_notifier_cleanup(&sint_route->sint_set_notifier); @@ -266,9 +405,10 @@ void hyperv_sint_route_unref(HvSintRoute *sint_route) &sint_route->sint_set_notifier, sint_route->gsi); kvm_irqchip_release_virq(kvm_state, sint_route->gsi); - if (sint_route->sint_ack_clb) { + if (sint_route->staged_msg) { event_notifier_set_handler(&sint_route->sint_ack_notifier, NULL); event_notifier_cleanup(&sint_route->sint_ack_notifier); + g_free(sint_route->staged_msg); } event_notifier_cleanup(&sint_route->sint_set_notifier); g_free(sint_route);