Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp3031006ybt; Mon, 22 Jun 2020 13:08:14 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyS+Ghz/uNh9bhp+WtrfbsQgVy3ehsGZwqnmc3sjxbbQfcY/MnfdwlXgPd0IyumB82KMcAO X-Received: by 2002:a50:f694:: with SMTP id d20mr18024150edn.171.1592856493833; Mon, 22 Jun 2020 13:08:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1592856493; cv=none; d=google.com; s=arc-20160816; b=T09dM0YvWVtrLzJ6TyL+qR/ybKGs3eI34mk6VGMk/8Z4NeCR9CFcE9Ayc/tHKjnwYe rex1USDB/Ddy95IEozv9VyyyuzrkhJNggcRSN31MsB0K6N1Y2FSKUUJlZ9MjO91PAvVY E+OtE56F2gNsDk784NgipP6g91udzeKcg9hI6G+TtToLU2OCB5H5PNtQBcTtkRNvN/Al Sh1B335tRvjwVqC4DpAlkzSBnxR2BrOhGIl0+ouFd6qlMmOE2+5Vjd2pvYkLqootUUh9 iIqf/X8ljhpShumyuHTEpLaB6NQLTpB0zfApanTqWE2KkDnQPQDGAUS/ZtZqq/KiFN0E ZCjQ== 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=keb35z3NMBBjQTvNVpo33JF+C0/lqm6fE5WWPgz5GC4=; b=plnwPXXBaIDLK/rui4jfCKOuNj4cJomazEg8QnpvfKI0+MCpjobhErrB63zj9vIbHh XOn3/keYxaxRftHLS1oZVPDG1rVNB+nngmot51CHCm+THOsMg9m977PBUtmTqxLdBS1z PffrHM+Z4sH3yfDijbeBV9E+q7oCaGIb8uUPK/YNiZev4SoZcIz51ZPiMGZqn4FQHJLg CBdsMpt6rDjM+9WyazycrLv9XAwtAEsSJdlWCmTGcolJMAcDfxG4dhfsMMYuziTs3yWj YRiru2tttl+X+tuSSOS7xpUJuywmmAQXPXk+i5TJ6IXsUL68Ul0zwmOIdTBNhfEnq9FV vwUw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=j5T4jsCD; 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 dr12si10416109ejc.352.2020.06.22.13.07.51; Mon, 22 Jun 2020 13:08:13 -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=j5T4jsCD; 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 S1730294AbgFVUFI (ORCPT + 99 others); Mon, 22 Jun 2020 16:05:08 -0400 Received: from smtp-fw-33001.amazon.com ([207.171.190.10]:32734 "EHLO smtp-fw-33001.amazon.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728228AbgFVUFG (ORCPT ); Mon, 22 Jun 2020 16:05:06 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1592856306; x=1624392306; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=keb35z3NMBBjQTvNVpo33JF+C0/lqm6fE5WWPgz5GC4=; b=j5T4jsCDeNLdNyjjqANCMA5kfkeEsozGWMSiV6DljZQDf9ajUdhhQG5E GzgOkb/VpFa8O1lAGzUO3JjXqfld2w/Muh7hqtAFhlh9aFZ40UI+KrjQh UtQNddvkeeHt6VcmxbWDUGpe9ProWaGbqwbHv46p0asLwpQKN0v6Vc2MZ k=; IronPort-SDR: MUKZKu3mWnCy0x4zRmzzaCOTrtPTay7hG/nOX5GXWKpvVC7mLP+gBOqBY5L99ouj9Y+vu52JPP fPhurKACQayQ== X-IronPort-AV: E=Sophos;i="5.75,268,1589241600"; d="scan'208";a="52986620" Received: from sea32-co-svc-lb4-vlan3.sea.corp.amazon.com (HELO email-inbound-relay-2b-c7131dcf.us-west-2.amazon.com) ([10.47.23.38]) by smtp-border-fw-out-33001.sea14.amazon.com with ESMTP; 22 Jun 2020 20:05:06 +0000 Received: from EX13MTAUEA002.ant.amazon.com (pdx4-ws-svc-p6-lb7-vlan2.pdx.amazon.com [10.170.41.162]) by email-inbound-relay-2b-c7131dcf.us-west-2.amazon.com (Postfix) with ESMTPS id 1E711A074B; Mon, 22 Jun 2020 20:05:05 +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, 22 Jun 2020 20:05:04 +0000 Received: from 38f9d34ed3b1.ant.amazon.com (10.43.161.145) by EX13D16EUB003.ant.amazon.com (10.43.166.99) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Mon, 22 Jun 2020 20:04:54 +0000 From: Andra Paraschiv To: CC: Anthony Liguori , Benjamin Herrenschmidt , Colm MacCarthaigh , "Bjoern Doebel" , David Woodhouse , "Frank van der Linden" , Alexander Graf , Greg KH , Martin Pohlack , "Matt Wilson" , Paolo Bonzini , Balbir Singh , Stefano Garzarella , "Stefan Hajnoczi" , Stewart Smith , "Uwe Dannowski" , , , Andra Paraschiv Subject: [PATCH v4 08/18] nitro_enclaves: Add logic for enclave vm creation Date: Mon, 22 Jun 2020 23:03:19 +0300 Message-ID: <20200622200329.52996-9-andraprs@amazon.com> X-Mailer: git-send-email 2.20.1 (Apple Git-117) In-Reply-To: <20200622200329.52996-1-andraprs@amazon.com> References: <20200622200329.52996-1-andraprs@amazon.com> MIME-Version: 1.0 X-Originating-IP: [10.43.161.145] X-ClientProxiedBy: EX13D12UWC003.ant.amazon.com (10.43.162.12) 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 Add ioctl command logic for enclave VM creation. It triggers a slot allocation. The enclave resources will be associated with this slot and it will be used as an identifier for triggering enclave run. Return a file descriptor, namely enclave fd. This is further used by the associated user space enclave process to set enclave resources and trigger enclave termination. The poll function is implemented in order to notify the enclave process when an enclave exits without a specific enclave termination command trigger e.g. when an enclave crashes. Signed-off-by: Alexandru Vasile Signed-off-by: Andra Paraschiv --- Changelog v3 -> v4 * Use dev_err instead of custom NE log pattern. * Update the NE ioctl call to match the decoupling from the KVM API. * Add metadata for the NUMA node for the enclave memory and CPUs. v2 -> v3 * Remove the WARN_ON calls. * Update static calls sanity checks. * Update kzfree() calls to kfree(). * Remove file ops that do nothing for now - open. v1 -> v2 * Add log pattern for NE. * Update goto labels to match their purpose. * Remove the BUG_ON calls. --- drivers/virt/nitro_enclaves/ne_misc_dev.c | 168 ++++++++++++++++++++++ 1 file changed, 168 insertions(+) diff --git a/drivers/virt/nitro_enclaves/ne_misc_dev.c b/drivers/virt/nitro_enclaves/ne_misc_dev.c index 628fb10c2b36..f70496813033 100644 --- a/drivers/virt/nitro_enclaves/ne_misc_dev.c +++ b/drivers/virt/nitro_enclaves/ne_misc_dev.c @@ -60,12 +60,180 @@ struct ne_cpu_pool { static struct ne_cpu_pool ne_cpu_pool; +static __poll_t ne_enclave_poll(struct file *file, poll_table *wait) +{ + __poll_t mask = 0; + struct ne_enclave *ne_enclave = file->private_data; + + poll_wait(file, &ne_enclave->eventq, wait); + + if (!ne_enclave->has_event) + return mask; + + mask = POLLHUP; + + return mask; +} + +static const struct file_operations ne_enclave_fops = { + .owner = THIS_MODULE, + .llseek = noop_llseek, + .poll = ne_enclave_poll, +}; + +/** + * ne_create_vm_ioctl - Alloc slot to be associated with an enclave. Create + * enclave file descriptor to be further used for enclave resources handling + * e.g. memory regions and CPUs. + * + * This function gets called with the ne_pci_dev enclave mutex held. + * + * @pdev: PCI device used for enclave lifetime management. + * @ne_pci_dev: private data associated with the PCI device. + * @slot_uid: generated unique slot id associated with an enclave. + * + * @returns: enclave fd on success, negative return value on failure. + */ +static int ne_create_vm_ioctl(struct pci_dev *pdev, + struct ne_pci_dev *ne_pci_dev, u64 *slot_uid) +{ + struct ne_pci_dev_cmd_reply cmd_reply = {}; + unsigned int cpu = 0; + int fd = 0; + struct file *file = NULL; + struct ne_enclave *ne_enclave = NULL; + int numa_node = -1; + int rc = -EINVAL; + struct slot_alloc_req slot_alloc_req = {}; + + ne_enclave = kzalloc(sizeof(*ne_enclave), GFP_KERNEL); + if (!ne_enclave) + return -ENOMEM; + + if (!zalloc_cpumask_var(&ne_enclave->cpu_siblings, GFP_KERNEL)) { + kfree(ne_enclave); + + return -ENOMEM; + } + + cpu = cpumask_any(ne_cpu_pool.avail); + if (cpu >= nr_cpu_ids) { + dev_err_ratelimited(ne_misc_dev.this_device, + "No CPUs available in CPU pool\n"); + + goto free_cpumask; + } + + numa_node = cpu_to_node(cpu); + if (numa_node < 0) { + dev_err_ratelimited(ne_misc_dev.this_device, + "Invalid NUMA node %d\n", numa_node); + + goto free_cpumask; + } + + fd = get_unused_fd_flags(O_CLOEXEC); + if (fd < 0) { + rc = fd; + + dev_err_ratelimited(ne_misc_dev.this_device, + "Error in getting unused fd [rc=%d]\n", + rc); + + goto free_cpumask; + } + + file = anon_inode_getfile("ne-vm", &ne_enclave_fops, ne_enclave, + O_RDWR); + if (IS_ERR(file)) { + rc = PTR_ERR(file); + + dev_err_ratelimited(ne_misc_dev.this_device, + "Error in anon inode get file [rc=%d]\n", + rc); + + goto put_fd; + } + + ne_enclave->pdev = pdev; + + rc = ne_do_request(ne_enclave->pdev, SLOT_ALLOC, &slot_alloc_req, + sizeof(slot_alloc_req), &cmd_reply, + sizeof(cmd_reply)); + if (rc < 0) { + dev_err_ratelimited(ne_misc_dev.this_device, + "Error in slot alloc [rc=%d]\n", rc); + + goto put_file; + } + + init_waitqueue_head(&ne_enclave->eventq); + ne_enclave->has_event = false; + mutex_init(&ne_enclave->enclave_info_mutex); + ne_enclave->max_mem_regions = cmd_reply.mem_regions; + INIT_LIST_HEAD(&ne_enclave->mem_regions_list); + ne_enclave->mm = current->mm; + ne_enclave->numa_node = numa_node; + ne_enclave->slot_uid = cmd_reply.slot_uid; + ne_enclave->state = NE_STATE_INIT; + INIT_LIST_HEAD(&ne_enclave->vcpu_ids_list); + + list_add(&ne_enclave->enclave_list_entry, &ne_pci_dev->enclaves_list); + + *slot_uid = ne_enclave->slot_uid; + + fd_install(fd, file); + + return fd; + +put_file: + fput(file); +put_fd: + put_unused_fd(fd); +free_cpumask: + free_cpumask_var(ne_enclave->cpu_siblings); + kfree(ne_enclave); + + return rc; +} + static long ne_ioctl(struct file *file, unsigned int cmd, unsigned long arg) { + struct ne_pci_dev *ne_pci_dev = NULL; + struct pci_dev *pdev = pci_get_device(PCI_VENDOR_ID_AMAZON, + PCI_DEVICE_ID_NE, NULL); + + if (!pdev) + return -ENODEV; + + ne_pci_dev = pci_get_drvdata(pdev); + if (!ne_pci_dev) + return -EINVAL; + switch (cmd) { case NE_GET_API_VERSION: return NE_API_VERSION; + case NE_CREATE_VM: { + u64 slot_uid = 0; + int rc = -EINVAL; + + mutex_lock(&ne_pci_dev->enclaves_list_mutex); + + rc = ne_create_vm_ioctl(pdev, ne_pci_dev, &slot_uid); + + mutex_unlock(&ne_pci_dev->enclaves_list_mutex); + + if (copy_to_user((void *)arg, &slot_uid, sizeof(slot_uid))) { + dev_err_ratelimited(ne_misc_dev.this_device, + "Error in copy to user\n"); + + return -EFAULT; + } + + return rc; + } + default: return -ENOTTY; } -- 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.