From nobody Sat May 18 10:48:39 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1696501549; cv=none; d=zohomail.com; s=zohoarc; b=PtnGuKpVcmYN4T8bXtHCsyOtADqezvYhGF4ZUqzuUuwUFoseEYiFSyW5oy019zU6wb7zWK1hfU9R3orYZ7rTgHZASUroBmtZvfhb7TX0CIZw722K0pOvUaTXASQbba7ysymRWc2phNtFlsA5pXpc998EWIc7gWrFcA1Gzj7RUXA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1696501549; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=VA2GT8eqbiQpihVq6IatV7jxO70qnqwP/ImeaPQmFQY=; b=ARRubaafqlyCwl/Uvv38xiy+kmqz/sbcvUaYAjyYz15E4AEdZzO+RWru+Iu8rBbZSBTuS/g/Zx92IY1/A+gXBOXW6Y5+POwvVvdBrQXuS8K9RySzxbYwsyh85CclDmxcK98D101Bhyl0UfA1MDclgnFOh4L9uSJ+QNV4vmV8ry4= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1696501549833789.7691642811589; Thu, 5 Oct 2023 03:25:49 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.612944.953144 (Exim 4.92) (envelope-from ) id 1qoLXo-0004fQ-IK; Thu, 05 Oct 2023 10:25:24 +0000 Received: by outflank-mailman (output) from mailman id 612944.953144; Thu, 05 Oct 2023 10:25:24 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qoLXo-0004fJ-Eh; Thu, 05 Oct 2023 10:25:24 +0000 Received: by outflank-mailman (input) for mailman id 612944; Thu, 05 Oct 2023 10:25:23 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qoLXn-0004Gs-Lc for xen-devel@lists.xenproject.org; Thu, 05 Oct 2023 10:25:23 +0000 Received: from mail-pf1-x42e.google.com (mail-pf1-x42e.google.com [2607:f8b0:4864:20::42e]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 7d5fd71f-6369-11ee-98d3-6d05b1d4d9a1; Thu, 05 Oct 2023 12:25:23 +0200 (CEST) Received: by mail-pf1-x42e.google.com with SMTP id d2e1a72fcca58-6907e44665bso684231b3a.1 for ; Thu, 05 Oct 2023 03:25:23 -0700 (PDT) Received: from localhost ([122.172.81.92]) by smtp.gmail.com with ESMTPSA id g6-20020a62e306000000b0068fe23b8792sm1055988pfh.176.2023.10.05.03.25.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Oct 2023 03:25:20 -0700 (PDT) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 7d5fd71f-6369-11ee-98d3-6d05b1d4d9a1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1696501521; x=1697106321; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=VA2GT8eqbiQpihVq6IatV7jxO70qnqwP/ImeaPQmFQY=; b=CI+Sf3iBgZgrPDHPCbnhK3JsTUXvLl33YZkCEohu1wMZkawyqfHJGBOeMdb1V1rSt4 CNzj2CHMUeUkFyj6ZWovn+Cx/szBwjCuvxr7hVc56u3LKnfg5xSdN268k428JelRYWAY SRGnz2WCb2nVyVg/VmPn+jq8lTHTjBiaCmp3mgtLGu/PgwE0ALpYnMd2rYSk2MuzpzUc KQKhUmd4qeWAKL87Ttz6Gl+FqOU8jQxs4DhJ6git18tYTQWNGxLwwXOKzmmDx9br4h0p jLwN9qW8wsNrig+/5YiWjvwotApPs2d9ys7W476G9gY3hHi0wxqy38i/5T1smXv9/LXs rbcA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696501521; x=1697106321; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=VA2GT8eqbiQpihVq6IatV7jxO70qnqwP/ImeaPQmFQY=; b=iWxm85jRANWHsyyQrMicAORKoqUhGY9B0fsI0KA79aGyLWxmM5fmQq/QY4BYY4BJ6U xLKcHsDRxAol+pMuw8wPxNjKYPMWA/vgyXz1txZ0OJV+9c0R7GGPfc/AwO+vS3LCdNFO hENla8Lw7SJ8D+n0uescJ2LJ05xV9ZnqCsWfbqfRRBzwMiCQUJGBUmEcV+rF1wgkOWLY mqZRlAsD+Iv8mOlcphCkyVxdl3fy9Di+Vn799ZdzPYZF1xYODnGrEN4y+YZM95MLMAdu 8cEH0K9ikx0eCE3Y9AyND/vRRu8l7l0oXnNRhCC79jCvEjVvJFZomoS4oGBujT2aF0FA 1gvQ== X-Gm-Message-State: AOJu0YyLJAd8djGgtuxzFFuSAGZIMHcrICvMIpXcJ3RBcSTKzOIiOIfN N9bOuk2ynZHQBi2+HSstAycz5Q== X-Google-Smtp-Source: AGHT+IFVyZfwPYortU/vCd/KtcZf9ROm2DtiD8fm0D3u1191c/tmNd0xEGXS4Lh56+iNRP/lGDs57g== X-Received: by 2002:a05:6a00:2d8d:b0:68e:236a:93d9 with SMTP id fb13-20020a056a002d8d00b0068e236a93d9mr5460543pfb.17.1696501521543; Thu, 05 Oct 2023 03:25:21 -0700 (PDT) From: Viresh Kumar To: Juergen Gross , Stefano Stabellini , Oleksandr Tyshchenko Cc: Viresh Kumar , Vincent Guittot , =?UTF-8?q?Alex=20Benn=C3=A9e?= , stratos-dev@op-lists.linaro.org, Erik Schilling , Manos Pitsidianakis , Mathieu Poirier , xen-devel@lists.xenproject.org, linux-kernel@vger.kernel.org Subject: [PATCH V2 1/2] xen: evtchn: Allow shared registration of IRQ handers Date: Thu, 5 Oct 2023 15:55:11 +0530 Message-Id: <8380c7b1c8cd9edb2e45e9a204650b34df3458a7.1696500578.git.viresh.kumar@linaro.org> X-Mailer: git-send-email 2.31.1.272.g89b43f80a514 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @linaro.org) X-ZM-MESSAGEID: 1696501551607100001 Content-Type: text/plain; charset="utf-8" Currently the handling of events is supported either in the kernel or userspace, but not both. In order to support fast delivery of interrupts from the guest to the backend, we need to handle the Queue notify part of Virtio protocol in kernel and the rest in userspace. Update the interrupt handler registration flag to IRQF_SHARED for event channels, which would allow multiple entities to bind their interrupt handler for the same event channel port. Also increment the reference count of irq_info when multiple entities try to bind event channel to irqchip, so the unbinding happens only after all the users are gone. Signed-off-by: Viresh Kumar --- drivers/xen/events/events_base.c | 3 ++- drivers/xen/evtchn.c | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/drivers/xen/events/events_base.c b/drivers/xen/events/events_b= ase.c index c7715f8bd452..d72fb26cc051 100644 --- a/drivers/xen/events/events_base.c +++ b/drivers/xen/events/events_base.c @@ -1238,7 +1238,8 @@ static int bind_evtchn_to_irq_chip(evtchn_port_t evtc= hn, struct irq_chip *chip, bind_evtchn_to_cpu(evtchn, 0, false); } else { struct irq_info *info =3D info_for_irq(irq); - WARN_ON(info =3D=3D NULL || info->type !=3D IRQT_EVTCHN); + if (!WARN_ON(!info || info->type !=3D IRQT_EVTCHN)) + info->refcnt++; } =20 out: diff --git a/drivers/xen/evtchn.c b/drivers/xen/evtchn.c index c99415a70051..43f77915feb5 100644 --- a/drivers/xen/evtchn.c +++ b/drivers/xen/evtchn.c @@ -397,7 +397,7 @@ static int evtchn_bind_to_user(struct per_user_data *u,= evtchn_port_t port) if (rc < 0) goto err; =20 - rc =3D bind_evtchn_to_irqhandler_lateeoi(port, evtchn_interrupt, 0, + rc =3D bind_evtchn_to_irqhandler_lateeoi(port, evtchn_interrupt, IRQF_SHA= RED, u->name, evtchn); if (rc < 0) goto err; --=20 2.31.1.272.g89b43f80a514 From nobody Sat May 18 10:48:39 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1696501552; cv=none; d=zohomail.com; s=zohoarc; b=AlOWr93pxzC+Lu1jm7rqMwvjilAOT9wt9MXlKNMTCANru8Ur7N5Dnl10lDNZLT91bP3sioDW+wS7TqXDUMApGzT4UBXEQC6xPfDbNgcBMEGTQyJa+XswE0SxyrakhkmCATRR5KjDP2gSPJ4pxzZgkAk9g7/riiwmE03dkK7VH7c= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1696501552; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=p3TcD8voV/4cdB31EHJGJeiN80RxHqTFgqX9z+XYw+8=; b=c3MB+mKKJXDv+NwHODV6t+Ond2F3Hy9bB7zCNv+FLnpjkHBLs7zHRdld4dGBz9vyHKvusLfytku44bRgSjsrQSpXXc7HyKyAm7QT0ALA9x3McP0v1qAAQa8ybgOr8Be+SIh9GloPPLbqU+Cz1YhwkKmV4GW/wPHXl5MBDkyK0/w= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1696501552390438.1829854151175; Thu, 5 Oct 2023 03:25:52 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.612945.953154 (Exim 4.92) (envelope-from ) id 1qoLXt-0004y6-Q2; Thu, 05 Oct 2023 10:25:29 +0000 Received: by outflank-mailman (output) from mailman id 612945.953154; Thu, 05 Oct 2023 10:25:29 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qoLXt-0004xx-Ma; Thu, 05 Oct 2023 10:25:29 +0000 Received: by outflank-mailman (input) for mailman id 612945; Thu, 05 Oct 2023 10:25:28 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qoLXs-0004Gs-9i for xen-devel@lists.xenproject.org; Thu, 05 Oct 2023 10:25:28 +0000 Received: from mail-pl1-x62f.google.com (mail-pl1-x62f.google.com [2607:f8b0:4864:20::62f]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 7fc9fd3e-6369-11ee-98d3-6d05b1d4d9a1; Thu, 05 Oct 2023 12:25:27 +0200 (CEST) Received: by mail-pl1-x62f.google.com with SMTP id d9443c01a7336-1c87e55a6baso5279715ad.3 for ; Thu, 05 Oct 2023 03:25:27 -0700 (PDT) Received: from localhost ([122.172.81.92]) by smtp.gmail.com with ESMTPSA id x11-20020a1709028ecb00b001c746b986e5sm1285346plo.45.2023.10.05.03.25.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Oct 2023 03:25:24 -0700 (PDT) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 7fc9fd3e-6369-11ee-98d3-6d05b1d4d9a1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1696501525; x=1697106325; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=p3TcD8voV/4cdB31EHJGJeiN80RxHqTFgqX9z+XYw+8=; b=twjfBgotURDq2yfzkBrpk1pAQQJNjMRsICBRAnO9nnDXN/OxRBX3qw/xtkORTfIFJM PTK5INmkShJzsACPFXM/m/Hebre7dZecYCswRrDrowSR9fSOOtVJxAIqFi62TJO6fbzo +UN/QIQadRkwbpoLPc26GWpXUAPsFeD8YxIzAN5qd4yLb8JUB62v1XNxv5r7UR24LUH4 XBh3If9YpfHHoM1id0iCJZV/zhoJ+hTub1h+9zHLJEg0YSDMm68qLmgPnd4ETnt/wLFY OLpWIrYMv1fnBLFXHzT9sMaMkW8wtRp2+ubu/czTBh11fYMeGHBA2kOUreaGEG6dkSp4 Ukeg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696501525; x=1697106325; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=p3TcD8voV/4cdB31EHJGJeiN80RxHqTFgqX9z+XYw+8=; b=aQROOHRYxpefzFrMknsQ4JTpNCKG1a+VrDaGItxFjXauJGd0edALyK/KCIaA57/HLl g/4eyUAR4pT/MVuo2zAZRTpPYmfxeMiGmHt+IYvualyAmDJhf/pgkOHiYEkdQxh1k+A9 7TtDLaVodLdMOQWZGO+JOup8/2TZzKnU+jhX/iCgHLd3jHdxV3AerOU3TR2b9GbUsDUi D6xvZ96Gr0Nu3+7EUpxGrG7meNzJiwEzzMSW1A/FGr0RwMI9Knbt+j2cTXrXk1Y3iawh 9VGyvjE9fG9hmV9e3U9N8LOytxj2O6Es/WqYrIXF1MkFXYhqNsngL2Lk6V6g8oKHQ5iT hDPQ== X-Gm-Message-State: AOJu0Yy0p+obwV+AnSrS1fo3Bc5rf3+gys07bPco5F4MMLboRt4a5MSd gJse3KZ/SQOqUz/cOJvrO8tFNg== X-Google-Smtp-Source: AGHT+IHp7gJlgM1dKOAKoaiwc4LoCqVfBDpQ2P763UoC03veSIwevnIMdl4PzVsujIGzIfySVftSAA== X-Received: by 2002:a17:902:da90:b0:1c6:de7:addd with SMTP id j16-20020a170902da9000b001c60de7adddmr6229734plx.48.1696501525466; Thu, 05 Oct 2023 03:25:25 -0700 (PDT) From: Viresh Kumar To: Juergen Gross , Stefano Stabellini , Oleksandr Tyshchenko Cc: Viresh Kumar , Vincent Guittot , =?UTF-8?q?Alex=20Benn=C3=A9e?= , stratos-dev@op-lists.linaro.org, Erik Schilling , Manos Pitsidianakis , Mathieu Poirier , linux-kernel@vger.kernel.org, xen-devel@lists.xenproject.org Subject: [PATCH V2 2/2] xen: privcmd: Add support for ioeventfd Date: Thu, 5 Oct 2023 15:55:12 +0530 Message-Id: <6e43e092e35a56619b0a132e78fac01b68ded94a.1696500578.git.viresh.kumar@linaro.org> X-Mailer: git-send-email 2.31.1.272.g89b43f80a514 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @linaro.org) X-ZM-MESSAGEID: 1696501553689100001 Content-Type: text/plain; charset="utf-8" Virtio guests send VIRTIO_MMIO_QUEUE_NOTIFY notification when they need to notify the backend of an update to the status of the virtqueue. The backend or another entity, polls the MMIO address for updates to know when the notification is sent. It works well if the backend does this polling by itself. But as we move towards generic backend implementations, we end up implementing this in a separate user-space program. Generally, the Virtio backends are implemented to work with the Eventfd based mechanism. In order to make such backends work with Xen, another software layer needs to do the polling and send an event via eventfd to the backend once the notification from guest is received. This results in an extra context switch. This is not a new problem in Linux though. It is present with other hypervisors like KVM, etc. as well. The generic solution implemented in the kernel for them is to provide an IOCTL call to pass the address to poll and eventfd, which lets the kernel take care of polling and raise an event on the eventfd, instead of handling this in user space (which involves an extra context switch). This patch adds similar support for xen. Inspired by existing implementations for KVM, etc.. This also copies ioreq.h header file (only struct ioreq and related macros) from Xen's source tree (Top commit 5d84f07fe6bf ("xen/pci: drop remaining uses of bool_t")). Signed-off-by: Viresh Kumar --- drivers/xen/Kconfig | 8 +- drivers/xen/privcmd.c | 398 +++++++++++++++++++++++++++++- include/uapi/xen/privcmd.h | 18 ++ include/xen/interface/hvm/ioreq.h | 51 ++++ 4 files changed, 469 insertions(+), 6 deletions(-) create mode 100644 include/xen/interface/hvm/ioreq.h diff --git a/drivers/xen/Kconfig b/drivers/xen/Kconfig index d43153fec18e..d5989871dd5d 100644 --- a/drivers/xen/Kconfig +++ b/drivers/xen/Kconfig @@ -269,12 +269,12 @@ config XEN_PRIVCMD disaggregated Xen setups this driver might be needed for other domains, too. =20 -config XEN_PRIVCMD_IRQFD - bool "Xen irqfd support" +config XEN_PRIVCMD_EVENTFD + bool "Xen Ioeventfd and irqfd support" depends on XEN_PRIVCMD && XEN_VIRTIO && EVENTFD help - Using the irqfd mechanism a virtio backend running in a daemon can - speed up interrupt injection into a guest. + Using the ioeventfd / irqfd mechanism a virtio backend running in a + daemon can speed up interrupt delivery from / to a guest. =20 config XEN_ACPI_PROCESSOR tristate "Xen ACPI processor" diff --git a/drivers/xen/privcmd.c b/drivers/xen/privcmd.c index 120af57999fc..6dee3450339a 100644 --- a/drivers/xen/privcmd.c +++ b/drivers/xen/privcmd.c @@ -29,15 +29,18 @@ #include #include #include +#include =20 #include #include =20 #include +#include #include #include #include #include +#include #include #include #include @@ -782,6 +785,7 @@ static long privcmd_ioctl_mmap_resource(struct file *fi= le, goto out; =20 pages =3D vma->vm_private_data; + for (i =3D 0; i < kdata.num; i++) { xen_pfn_t pfn =3D page_to_xen_pfn(pages[i / XEN_PFN_PER_PAGE]); @@ -838,7 +842,7 @@ static long privcmd_ioctl_mmap_resource(struct file *fi= le, return rc; } =20 -#ifdef CONFIG_XEN_PRIVCMD_IRQFD +#ifdef CONFIG_XEN_PRIVCMD_EVENTFD /* Irqfd support */ static struct workqueue_struct *irqfd_cleanup_wq; static DEFINE_MUTEX(irqfds_lock); @@ -1079,6 +1083,382 @@ static void privcmd_irqfd_exit(void) =20 destroy_workqueue(irqfd_cleanup_wq); } + +/* Ioeventfd Support */ +#define QUEUE_NOTIFY_VQ_MASK 0xFFFF + +static DEFINE_MUTEX(ioreq_lock); +static LIST_HEAD(ioreq_list); + +/* per-eventfd structure */ +struct privcmd_kernel_ioeventfd { + struct eventfd_ctx *eventfd; + struct list_head list; + u64 addr; + unsigned int addr_len; + unsigned int vq; +}; + +/* per-guest CPU / port structure */ +struct ioreq_port { + int vcpu; + unsigned int port; + struct privcmd_kernel_ioreq *kioreq; +}; + +/* per-guest structure */ +struct privcmd_kernel_ioreq { + domid_t dom; + unsigned int vcpus; + void __user *uioreq; + struct ioreq *ioreq; + spinlock_t lock; /* Protects ioeventfds list */ + struct list_head ioeventfds; + struct list_head list; + struct ioreq_port ports[0]; +}; + +static irqreturn_t ioeventfd_interrupt(int irq, void *dev_id) +{ + struct ioreq_port *port =3D dev_id; + struct privcmd_kernel_ioreq *kioreq =3D port->kioreq; + struct ioreq *ioreq =3D &kioreq->ioreq[port->vcpu]; + struct privcmd_kernel_ioeventfd *kioeventfd; + unsigned int state =3D STATE_IOREQ_READY; + + if (ioreq->state !=3D STATE_IOREQ_READY || + ioreq->type !=3D IOREQ_TYPE_COPY || ioreq->dir !=3D IOREQ_WRITE) + return IRQ_NONE; + + /* Barrier to ensure reads are finished before `state` is updated */ + smp_mb(); + + ioreq->state =3D STATE_IOREQ_INPROCESS; + + spin_lock(&kioreq->lock); + list_for_each_entry(kioeventfd, &kioreq->ioeventfds, list) { + if (ioreq->addr =3D=3D kioeventfd->addr + VIRTIO_MMIO_QUEUE_NOTIFY && + ioreq->size =3D=3D kioeventfd->addr_len && + (ioreq->data & QUEUE_NOTIFY_VQ_MASK) =3D=3D kioeventfd->vq) { + eventfd_signal(kioeventfd->eventfd, 1); + state =3D STATE_IORESP_READY; + break; + } + } + spin_unlock(&kioreq->lock); + + /* Barrier to ensure writes are finished before `state` is updated */ + smp_mb(); + + ioreq->state =3D state; + + if (state =3D=3D STATE_IORESP_READY) { + notify_remote_via_evtchn(port->port); + return IRQ_HANDLED; + } + + return IRQ_NONE; +} + +static void ioreq_free(struct privcmd_kernel_ioreq *kioreq) +{ + struct ioreq_port *ports =3D kioreq->ports; + int i; + + lockdep_assert_held(&ioreq_lock); + + if (!list_empty(&kioreq->ioeventfds)) { + pr_warn_ratelimited("Ioeventfds list should be empty\n"); + return; + } + + list_del(&kioreq->list); + + for (i =3D kioreq->vcpus - 1; i >=3D 0; i--) + unbind_from_irqhandler(irq_from_evtchn(ports[i].port), &ports[i]); + + kfree(kioreq); +} + +static +struct privcmd_kernel_ioreq *alloc_ioreq(struct privcmd_ioeventfd *ioevent= fd) +{ + struct privcmd_kernel_ioreq *kioreq; + struct mm_struct *mm =3D current->mm; + struct vm_area_struct *vma; + struct page **pages; + unsigned int *ports; + int ret, size, i; + + lockdep_assert_held(&ioreq_lock); + + size =3D struct_size(kioreq, ports, ioeventfd->vcpus); + kioreq =3D kzalloc(size, GFP_KERNEL); + if (!kioreq) + return ERR_PTR(-ENOMEM); + + kioreq->dom =3D ioeventfd->dom; + kioreq->vcpus =3D ioeventfd->vcpus; + kioreq->uioreq =3D ioeventfd->ioreq; + spin_lock_init(&kioreq->lock); + INIT_LIST_HEAD(&kioreq->ioeventfds); + + /* The memory for ioreq server must have been mapped earlier */ + mmap_write_lock(mm); + vma =3D find_vma(mm, (unsigned long)ioeventfd->ioreq); + if (!vma) { + pr_err("Failed to find vma for ioreq page!\n"); + mmap_write_unlock(mm); + ret =3D -EFAULT; + goto error_kfree; + } + + pages =3D vma->vm_private_data; + kioreq->ioreq =3D (struct ioreq *)(page_to_virt(pages[0])); + mmap_write_unlock(mm); + + size =3D sizeof(*ports) * kioreq->vcpus; + ports =3D kzalloc(size, GFP_KERNEL); + if (!ports) { + ret =3D -ENOMEM; + goto error_kfree; + } + + if (copy_from_user(ports, ioeventfd->ports, size)) { + ret =3D -EFAULT; + goto error_kfree_ports; + } + + for (i =3D 0; i < kioreq->vcpus; i++) { + kioreq->ports[i].vcpu =3D i; + kioreq->ports[i].port =3D ports[i]; + kioreq->ports[i].kioreq =3D kioreq; + + ret =3D bind_evtchn_to_irqhandler_lateeoi(ports[i], + ioeventfd_interrupt, IRQF_SHARED, "ioeventfd", + &kioreq->ports[i]); + if (ret < 0) + goto error_unbind; + } + + kfree(ports); + + list_add_tail(&kioreq->list, &ioreq_list); + + return kioreq; + +error_unbind: + while (--i >=3D 0) + unbind_from_irqhandler(irq_from_evtchn(ports[i]), &kioreq->ports[i]); +error_kfree_ports: + kfree(ports); +error_kfree: + kfree(kioreq); + return ERR_PTR(ret); +} + +static struct privcmd_kernel_ioreq * +get_ioreq(struct privcmd_ioeventfd *ioeventfd, struct eventfd_ctx *eventfd) +{ + struct privcmd_kernel_ioreq *kioreq; + unsigned long flags; + + list_for_each_entry(kioreq, &ioreq_list, list) { + struct privcmd_kernel_ioeventfd *kioeventfd; + + /* + * kioreq fields can be accessed here without a lock as they are + * never updated after being added to the ioreq_list. + */ + if (kioreq->uioreq !=3D ioeventfd->ioreq) { + continue; + } else if (kioreq->dom !=3D ioeventfd->dom || + kioreq->vcpus !=3D ioeventfd->vcpus) { + pr_err("Invalid ioeventfd configuration mismatch, dom (%u vs %u), vcpus= (%u vs %u)\n", + kioreq->dom, ioeventfd->dom, kioreq->vcpus, + ioeventfd->vcpus); + return ERR_PTR(-EINVAL); + } + + /* Look for a duplicate eventfd for the same guest */ + spin_lock_irqsave(&kioreq->lock, flags); + list_for_each_entry(kioeventfd, &kioreq->ioeventfds, list) { + if (eventfd =3D=3D kioeventfd->eventfd) { + spin_unlock_irqrestore(&kioreq->lock, flags); + return ERR_PTR(-EBUSY); + } + } + spin_unlock_irqrestore(&kioreq->lock, flags); + + return kioreq; + } + + /* Matching kioreq isn't found, allocate a new one */ + return alloc_ioreq(ioeventfd); +} + +static void ioeventfd_free(struct privcmd_kernel_ioeventfd *kioeventfd) +{ + list_del(&kioeventfd->list); + eventfd_ctx_put(kioeventfd->eventfd); + kfree(kioeventfd); +} + +static int privcmd_ioeventfd_assign(struct privcmd_ioeventfd *ioeventfd) +{ + struct privcmd_kernel_ioeventfd *kioeventfd; + struct privcmd_kernel_ioreq *kioreq; + unsigned long flags; + struct fd f; + int ret; + + /* Check for range overflow */ + if (ioeventfd->addr + ioeventfd->addr_len < ioeventfd->addr) + return -EINVAL; + + /* Vhost requires us to support length 1, 2, 4, and 8 */ + if (!(ioeventfd->addr_len =3D=3D 1 || ioeventfd->addr_len =3D=3D 2 || + ioeventfd->addr_len =3D=3D 4 || ioeventfd->addr_len =3D=3D 8)) + return -EINVAL; + + /* 4096 vcpus limit enough ? */ + if (!ioeventfd->vcpus || ioeventfd->vcpus > 4096) + return -EINVAL; + + kioeventfd =3D kzalloc(sizeof(*kioeventfd), GFP_KERNEL); + if (!kioeventfd) + return -ENOMEM; + + f =3D fdget(ioeventfd->event_fd); + if (!f.file) { + ret =3D -EBADF; + goto error_kfree; + } + + kioeventfd->eventfd =3D eventfd_ctx_fileget(f.file); + fdput(f); + + if (IS_ERR(kioeventfd->eventfd)) { + ret =3D PTR_ERR(kioeventfd->eventfd); + goto error_kfree; + } + + kioeventfd->addr =3D ioeventfd->addr; + kioeventfd->addr_len =3D ioeventfd->addr_len; + kioeventfd->vq =3D ioeventfd->vq; + + mutex_lock(&ioreq_lock); + kioreq =3D get_ioreq(ioeventfd, kioeventfd->eventfd); + if (IS_ERR(kioreq)) { + mutex_unlock(&ioreq_lock); + ret =3D PTR_ERR(kioreq); + goto error_eventfd; + } + + spin_lock_irqsave(&kioreq->lock, flags); + list_add_tail(&kioeventfd->list, &kioreq->ioeventfds); + spin_unlock_irqrestore(&kioreq->lock, flags); + + mutex_unlock(&ioreq_lock); + + return 0; + +error_eventfd: + eventfd_ctx_put(kioeventfd->eventfd); + +error_kfree: + kfree(kioeventfd); + return ret; +} + +static int privcmd_ioeventfd_deassign(struct privcmd_ioeventfd *ioeventfd) +{ + struct privcmd_kernel_ioreq *kioreq, *tkioreq; + struct eventfd_ctx *eventfd; + unsigned long flags; + int ret =3D 0; + + eventfd =3D eventfd_ctx_fdget(ioeventfd->event_fd); + if (IS_ERR(eventfd)) + return PTR_ERR(eventfd); + + mutex_lock(&ioreq_lock); + list_for_each_entry_safe(kioreq, tkioreq, &ioreq_list, list) { + struct privcmd_kernel_ioeventfd *kioeventfd, *tmp; + /* + * kioreq fields can be accessed here without a lock as they are + * never updated after being added to the ioreq_list. + */ + if (kioreq->dom !=3D ioeventfd->dom || + kioreq->uioreq !=3D ioeventfd->ioreq || + kioreq->vcpus !=3D ioeventfd->vcpus) + continue; + + spin_lock_irqsave(&kioreq->lock, flags); + list_for_each_entry_safe(kioeventfd, tmp, &kioreq->ioeventfds, list) { + if (eventfd =3D=3D kioeventfd->eventfd) { + ioeventfd_free(kioeventfd); + spin_unlock_irqrestore(&kioreq->lock, flags); + + ioreq_free(kioreq); + goto unlock; + } + } + spin_unlock_irqrestore(&kioreq->lock, flags); + break; + } + + pr_err("Ioeventfd isn't already assigned, dom: %u, addr: %llu\n", + ioeventfd->dom, ioeventfd->addr); + ret =3D -ENODEV; + +unlock: + mutex_unlock(&ioreq_lock); + eventfd_ctx_put(eventfd); + + return ret; +} + +static long privcmd_ioctl_ioeventfd(struct file *file, void __user *udata) +{ + struct privcmd_data *data =3D file->private_data; + struct privcmd_ioeventfd ioeventfd; + + if (copy_from_user(&ioeventfd, udata, sizeof(ioeventfd))) + return -EFAULT; + + /* No other flags should be set */ + if (ioeventfd.flags & ~PRIVCMD_IOEVENTFD_FLAG_DEASSIGN) + return -EINVAL; + + /* If restriction is in place, check the domid matches */ + if (data->domid !=3D DOMID_INVALID && data->domid !=3D ioeventfd.dom) + return -EPERM; + + if (ioeventfd.flags & PRIVCMD_IOEVENTFD_FLAG_DEASSIGN) + return privcmd_ioeventfd_deassign(&ioeventfd); + + return privcmd_ioeventfd_assign(&ioeventfd); +} + +static void privcmd_ioeventfd_exit(void) +{ + struct privcmd_kernel_ioreq *kioreq, *tmp; + unsigned long flags; + + mutex_lock(&ioreq_lock); + list_for_each_entry_safe(kioreq, tmp, &ioreq_list, list) { + struct privcmd_kernel_ioeventfd *kioeventfd, *tmp; + + spin_lock_irqsave(&kioreq->lock, flags); + list_for_each_entry_safe(kioeventfd, tmp, &kioreq->ioeventfds, list) + ioeventfd_free(kioeventfd); + spin_unlock_irqrestore(&kioreq->lock, flags); + + ioreq_free(kioreq); + } + mutex_unlock(&ioreq_lock); +} #else static inline long privcmd_ioctl_irqfd(struct file *file, void __user *uda= ta) { @@ -1093,7 +1473,16 @@ static inline int privcmd_irqfd_init(void) static inline void privcmd_irqfd_exit(void) { } -#endif /* CONFIG_XEN_PRIVCMD_IRQFD */ + +static inline long privcmd_ioctl_ioeventfd(struct file *file, void __user = *udata) +{ + return -EOPNOTSUPP; +} + +static inline void privcmd_ioeventfd_exit(void) +{ +} +#endif /* CONFIG_XEN_PRIVCMD_EVENTFD */ =20 static long privcmd_ioctl(struct file *file, unsigned int cmd, unsigned long data) @@ -1134,6 +1523,10 @@ static long privcmd_ioctl(struct file *file, ret =3D privcmd_ioctl_irqfd(file, udata); break; =20 + case IOCTL_PRIVCMD_IOEVENTFD: + ret =3D privcmd_ioctl_ioeventfd(file, udata); + break; + default: break; } @@ -1278,6 +1671,7 @@ static int __init privcmd_init(void) =20 static void __exit privcmd_exit(void) { + privcmd_ioeventfd_exit(); privcmd_irqfd_exit(); misc_deregister(&privcmd_dev); misc_deregister(&xen_privcmdbuf_dev); diff --git a/include/uapi/xen/privcmd.h b/include/uapi/xen/privcmd.h index 375718ba4ab6..ce71390f0024 100644 --- a/include/uapi/xen/privcmd.h +++ b/include/uapi/xen/privcmd.h @@ -110,6 +110,22 @@ struct privcmd_irqfd { __u8 pad[2]; }; =20 +/* For privcmd_ioeventfd::flags */ +#define PRIVCMD_IOEVENTFD_FLAG_DEASSIGN (1 << 0) + +struct privcmd_ioeventfd { + void __user *ioreq; + unsigned int __user *ports; + __u64 addr; + __u32 addr_len; + __u32 event_fd; + __u32 vcpus; + __u32 vq; + __u32 flags; + domid_t dom; + __u8 pad[2]; +}; + /* * @cmd: IOCTL_PRIVCMD_HYPERCALL * @arg: &privcmd_hypercall_t @@ -139,5 +155,7 @@ struct privcmd_irqfd { _IOC(_IOC_NONE, 'P', 7, sizeof(struct privcmd_mmap_resource)) #define IOCTL_PRIVCMD_IRQFD \ _IOC(_IOC_NONE, 'P', 8, sizeof(struct privcmd_irqfd)) +#define IOCTL_PRIVCMD_IOEVENTFD \ + _IOC(_IOC_NONE, 'P', 9, sizeof(struct privcmd_ioeventfd)) =20 #endif /* __LINUX_PUBLIC_PRIVCMD_H__ */ diff --git a/include/xen/interface/hvm/ioreq.h b/include/xen/interface/hvm/= ioreq.h new file mode 100644 index 000000000000..b02cfeae7eb5 --- /dev/null +++ b/include/xen/interface/hvm/ioreq.h @@ -0,0 +1,51 @@ +/* SPDX-License-Identifier: MIT */ +/* + * ioreq.h: I/O request definitions for device models + * Copyright (c) 2004, Intel Corporation. + */ + +#ifndef __XEN_PUBLIC_HVM_IOREQ_H__ +#define __XEN_PUBLIC_HVM_IOREQ_H__ + +#define IOREQ_READ 1 +#define IOREQ_WRITE 0 + +#define STATE_IOREQ_NONE 0 +#define STATE_IOREQ_READY 1 +#define STATE_IOREQ_INPROCESS 2 +#define STATE_IORESP_READY 3 + +#define IOREQ_TYPE_PIO 0 /* pio */ +#define IOREQ_TYPE_COPY 1 /* mmio ops */ +#define IOREQ_TYPE_PCI_CONFIG 2 +#define IOREQ_TYPE_TIMEOFFSET 7 +#define IOREQ_TYPE_INVALIDATE 8 /* mapcache */ + +/* + * VMExit dispatcher should cooperate with instruction decoder to + * prepare this structure and notify service OS and DM by sending + * virq. + * + * For I/O type IOREQ_TYPE_PCI_CONFIG, the physical address is formatted + * as follows: + * + * 63....48|47..40|39..35|34..32|31........0 + * SEGMENT |BUS |DEV |FN |OFFSET + */ +struct ioreq { + uint64_t addr; /* physical address */ + uint64_t data; /* data (or paddr of data) */ + uint32_t count; /* for rep prefixes */ + uint32_t size; /* size in bytes */ + uint32_t vp_eport; /* evtchn for notifications to/from device model = */ + uint16_t _pad0; + uint8_t state:4; + uint8_t data_is_ptr:1; /* if 1, data above is the guest paddr + * of the real data to use. */ + uint8_t dir:1; /* 1=3Dread, 0=3Dwrite */ + uint8_t df:1; + uint8_t _pad1:1; + uint8_t type; /* I/O type */ +}; + +#endif /* __XEN_PUBLIC_HVM_IOREQ_H__ */ --=20 2.31.1.272.g89b43f80a514