Received: by 2002:a25:2c96:0:0:0:0:0 with SMTP id s144csp1410797ybs; Mon, 25 May 2020 15:38:26 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw+ccHBEYE5h2Dbb3U0AJSApqEegVhvBLRdChhR1gfabQEPK4Rv/9+wEtTqi0DMnAszOjID X-Received: by 2002:a17:906:d216:: with SMTP id w22mr7610638ejz.420.1590446306767; Mon, 25 May 2020 15:38:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1590446306; cv=none; d=google.com; s=arc-20160816; b=qGzG+hBwpCKwph8/5Pv0So9XGn3gGXHbDgsLeL2cwdfBM4AFgLlk/0r1h8WlXxoIJc cSOXDtahufhdvFsMQfzIm+XsDXd1Nv+wuC1PrMuSxtKUJ2AGL0GLKsRLGy05qZHb1KET zggNKOS0ecu1wGg/LIAUGNw7XAvVYSUSIyObz0gWyeWvR4/dmEPfEZi26mszrA2BhsZ4 q8oQYg3rr7BvXDtkRMyszAiGnGR5zGoyrAj24Z7+y1DXmCBR8pl9VfDaRqcuvS+2Kyeg 3EF19ut3y3kg17PufoZCknwM9Fm57M2VVXk6jJ86nAeTYyimtruNiHS2pd91V53NFra7 YWow== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :ironport-sdr:dkim-signature; bh=P/qEZJCt6qdv3pyqWURKzGxQMD7Hj0vfXnPBfD7p5Z4=; b=RlB+SFppP9T6m68PMFEgHZFa4rgiyiW1p6SRwgYmRuiJcu82LDFghTHS89YyVjTur4 J4u1YS0NUznUipmKkx4kOgCVcENxEybOOJO0wCy9vzJXPD2ge8ozHzMkm7KMDB5lPd96 /4WJbeO7RmpZP+DAX9mnyW6+UrnH1dIzfwvFlB6Dn2zcaSXVwEbS4dnLHR2NW7ZmH2AR LVUMBMqt4w7EviQ6bmhr/99gIjnKX6uCQLrd4uTv97b+3dVWXEyopRNvYKkrmXH4+oVJ mZgHImZTvFfgt7cxGUgwSZWek97uNtFMZWEYxcd+ChgduWHjeD3cbte2F6JNK6T4GPzX IGiw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b="SOruN/6m"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id x25si10662027edl.429.2020.05.25.15.38.03; Mon, 25 May 2020 15:38:26 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b="SOruN/6m"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388969AbgEYWO5 (ORCPT + 99 others); Mon, 25 May 2020 18:14:57 -0400 Received: from smtp-fw-6002.amazon.com ([52.95.49.90]:59685 "EHLO smtp-fw-6002.amazon.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388902AbgEYWO4 (ORCPT ); Mon, 25 May 2020 18:14:56 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1590444894; x=1621980894; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=P/qEZJCt6qdv3pyqWURKzGxQMD7Hj0vfXnPBfD7p5Z4=; b=SOruN/6mtmAQezkfGB2YsoDdeCGy53jjuM9Fp/2eoLQ00JYooLn0a8O9 IZlXL518sJZyTr/1XnV9lQFOhaqQuJnoslSn41HeDRUOldRQBzI1no6vD ohcZeJVsAMB1PcKbmzXgDaIhEExfM4NPPdkYdiX4KOl0Y6crSKOT6gT2G A=; IronPort-SDR: /OEF0AyFy4MhOw+bn+yZHBqSBpye5EFaNs3QtEh7tB/i3x0id0HEZI2c1OT4Jnlv04g3Kq2G+C 0lQXMtZhGw7w== X-IronPort-AV: E=Sophos;i="5.73,435,1583193600"; d="scan'208";a="32053534" Received: from iad12-co-svc-p1-lb1-vlan3.amazon.com (HELO email-inbound-relay-1d-38ae4ad2.us-east-1.amazon.com) ([10.43.8.6]) by smtp-border-fw-out-6002.iad6.amazon.com with ESMTP; 25 May 2020 22:14:53 +0000 Received: from EX13MTAUEA002.ant.amazon.com (iad55-ws-svc-p15-lb9-vlan3.iad.amazon.com [10.40.159.166]) by email-inbound-relay-1d-38ae4ad2.us-east-1.amazon.com (Postfix) with ESMTPS id A7B28A2530; Mon, 25 May 2020 22:14:51 +0000 (UTC) Received: from EX13D16EUB003.ant.amazon.com (10.43.166.99) by EX13MTAUEA002.ant.amazon.com (10.43.61.77) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Mon, 25 May 2020 22:14:51 +0000 Received: from 38f9d34ed3b1.ant.amazon.com (10.43.160.90) by EX13D16EUB003.ant.amazon.com (10.43.166.99) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Mon, 25 May 2020 22:14:41 +0000 From: Andra Paraschiv To: CC: Anthony Liguori , Benjamin Herrenschmidt , Colm MacCarthaigh , "Bjoern Doebel" , David Woodhouse , "Frank van der Linden" , Alexander Graf , "Martin Pohlack" , Matt Wilson , Paolo Bonzini , Balbir Singh , Stefano Garzarella , Stefan Hajnoczi , Stewart Smith , Uwe Dannowski , , , Andra Paraschiv Subject: [PATCH v3 06/18] nitro_enclaves: Handle out-of-band PCI device events Date: Tue, 26 May 2020 01:13:22 +0300 Message-ID: <20200525221334.62966-7-andraprs@amazon.com> X-Mailer: git-send-email 2.20.1 (Apple Git-117) In-Reply-To: <20200525221334.62966-1-andraprs@amazon.com> References: <20200525221334.62966-1-andraprs@amazon.com> MIME-Version: 1.0 X-Originating-IP: [10.43.160.90] X-ClientProxiedBy: EX13D14UWC002.ant.amazon.com (10.43.162.214) To EX13D16EUB003.ant.amazon.com (10.43.166.99) Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org In addition to the replies sent by the Nitro Enclaves PCI device in response to command requests, out-of-band enclave events can happen e.g. an enclave crashes. In this case, the Nitro Enclaves driver needs to be aware of the event and notify the corresponding user space process that abstracts the enclave. Register an MSI-X interrupt vector to be used for this kind of out-of-band events. The interrupt notifies that the state of an enclave changed and the driver logic scans the state of each running enclave to identify for which this notification is intended. Create an workqueue to handle the out-of-band events. Notify user space enclave process that is using a polling mechanism on the enclave fd. The enclave fd is returned as a result of KVM_CREATE_VM ioctl call. Signed-off-by: Alexandru-Catalin Vasile Signed-off-by: Andra Paraschiv --- Changelog v2 -> v3 * Remove the WARN_ON calls. * Update static calls sanity checks. * Remove "ratelimited" from the logs that are not in the ioctl call paths. v1 -> v2 * Add log pattern for NE. * Update goto labels to match their purpose. --- drivers/virt/nitro_enclaves/ne_pci_dev.c | 118 +++++++++++++++++++++++ 1 file changed, 118 insertions(+) diff --git a/drivers/virt/nitro_enclaves/ne_pci_dev.c b/drivers/virt/nitro_enclaves/ne_pci_dev.c index 5e8bfda4bd0f..fd796450c9eb 100644 --- a/drivers/virt/nitro_enclaves/ne_pci_dev.c +++ b/drivers/virt/nitro_enclaves/ne_pci_dev.c @@ -232,6 +232,87 @@ static irqreturn_t ne_reply_handler(int irq, void *args) return IRQ_HANDLED; } +/** + * ne_event_work_handler - Work queue handler for notifying enclaves on + * a state change received by the event interrupt handler. + * + * An out-of-band event is being issued by the Nitro Hypervisor when at least + * one enclave is changing state without client interaction. + * + * @work: item containing the Nitro Enclaves PCI device for which a + * out-of-band event was issued. + */ +static void ne_event_work_handler(struct work_struct *work) +{ + struct ne_pci_dev_cmd_reply cmd_reply = {}; + struct ne_enclave *ne_enclave = NULL; + struct ne_pci_dev *ne_pci_dev = + container_of(work, struct ne_pci_dev, notify_work); + int rc = -EINVAL; + struct slot_info_req slot_info_req = {}; + + mutex_lock(&ne_pci_dev->enclaves_list_mutex); + + /* + * Iterate over all enclaves registered for the Nitro Enclaves + * PCI device and determine for which enclave(s) the out-of-band event + * is corresponding to. + */ + list_for_each_entry(ne_enclave, &ne_pci_dev->enclaves_list, + enclave_list_entry) { + mutex_lock(&ne_enclave->enclave_info_mutex); + + /* + * Enclaves that were never started cannot receive out-of-band + * events. + */ + if (ne_enclave->state != NE_STATE_RUNNING) + goto unlock; + + slot_info_req.slot_uid = ne_enclave->slot_uid; + + rc = ne_do_request(ne_enclave->pdev, SLOT_INFO, &slot_info_req, + sizeof(slot_info_req), &cmd_reply, + sizeof(cmd_reply)); + if (rc < 0) + dev_err(&ne_enclave->pdev->dev, + NE "Error in slot info [rc=%d]\n", rc); + + /* Notify enclave process that the enclave state changed. */ + if (ne_enclave->state != cmd_reply.state) { + ne_enclave->state = cmd_reply.state; + + ne_enclave->has_event = true; + + wake_up_interruptible(&ne_enclave->eventq); + } + +unlock: + mutex_unlock(&ne_enclave->enclave_info_mutex); + } + + mutex_unlock(&ne_pci_dev->enclaves_list_mutex); +} + +/** + * ne_event_handler - Interrupt handler for PCI device out-of-band + * events. This interrupt does not supply any data in the MMIO region. + * It notifies a change in the state of any of the launched enclaves. + * + * @irq: received interrupt for an out-of-band event. + * @args: PCI device private data structure. + * + * @returns: IRQ_HANDLED on handled interrupt, IRQ_NONE otherwise. + */ +static irqreturn_t ne_event_handler(int irq, void *args) +{ + struct ne_pci_dev *ne_pci_dev = (struct ne_pci_dev *)args; + + queue_work(ne_pci_dev->event_wq, &ne_pci_dev->notify_work); + + return IRQ_HANDLED; +} + /** * ne_setup_msix - Setup MSI-X vectors for the PCI device. * @@ -277,8 +358,38 @@ static int ne_setup_msix(struct pci_dev *pdev) goto free_irq_vectors; } + ne_pci_dev->event_wq = create_singlethread_workqueue("ne_pci_dev_wq"); + if (!ne_pci_dev->event_wq) { + rc = -ENOMEM; + + dev_err(&pdev->dev, NE "Cannot get wq for dev events [rc=%d]\n", + rc); + + goto free_reply_irq_vec; + } + + INIT_WORK(&ne_pci_dev->notify_work, ne_event_work_handler); + + /* + * This IRQ gets triggered every time any enclave's state changes. Its + * handler then scans for the changes and propagates them to the user + * space. + */ + rc = request_irq(pci_irq_vector(pdev, NE_VEC_EVENT), + ne_event_handler, 0, "enclave_evt", ne_pci_dev); + if (rc < 0) { + dev_err(&pdev->dev, NE "Error in request irq event [rc=%d]\n", + rc); + + goto destroy_wq; + } + return 0; +destroy_wq: + destroy_workqueue(ne_pci_dev->event_wq); +free_reply_irq_vec: + free_irq(pci_irq_vector(pdev, NE_VEC_REPLY), ne_pci_dev); free_irq_vectors: pci_free_irq_vectors(pdev); @@ -294,6 +405,13 @@ static void ne_teardown_msix(struct pci_dev *pdev) { struct ne_pci_dev *ne_pci_dev = pci_get_drvdata(pdev); + + free_irq(pci_irq_vector(pdev, NE_VEC_EVENT), ne_pci_dev); + + flush_work(&ne_pci_dev->notify_work); + flush_workqueue(ne_pci_dev->event_wq); + destroy_workqueue(ne_pci_dev->event_wq); + free_irq(pci_irq_vector(pdev, NE_VEC_REPLY), ne_pci_dev); pci_free_irq_vectors(pdev); -- 2.20.1 (Apple Git-117) Amazon Development Center (Romania) S.R.L. registered office: 27A Sf. Lazar Street, UBC5, floor 2, Iasi, Iasi County, 700045, Romania. Registered in Romania. Registration number J22/2621/2005.