From patchwork Sun Feb 19 14:47:07 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "lan,Tianyu" X-Patchwork-Id: 9581615 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id D37736057F for ; Sun, 19 Feb 2017 14:54:10 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B9809286CB for ; Sun, 19 Feb 2017 14:54:10 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id ADF9D28707; Sun, 19 Feb 2017 14:54:10 +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=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3282F286F0 for ; Sun, 19 Feb 2017 14:54:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751522AbdBSOyE (ORCPT ); Sun, 19 Feb 2017 09:54:04 -0500 Received: from mga04.intel.com ([192.55.52.120]:41594 "EHLO mga04.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751208AbdBSOyD (ORCPT ); Sun, 19 Feb 2017 09:54:03 -0500 Received: from orsmga002.jf.intel.com ([10.7.209.21]) by fmsmga104.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 19 Feb 2017 06:54:02 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.35,181,1484035200"; d="scan'208";a="49897412" Received: from lantianyu-ws.sh.intel.com (HELO localhost) ([10.239.159.159]) by orsmga002.jf.intel.com with ESMTP; 19 Feb 2017 06:54:00 -0800 From: Lan Tianyu To: kvm@vger.kernel.org Cc: Lan Tianyu , kevin.tian@intel.com, mst@redhat.com, jan.kiszka@siemens.com, jasowang@redhat.com, peterx@redhat.com, david@gibson.dropbear.id.au, alex.williamson@redhat.com, yi.l.liu@intel.com Subject: [RFC PATCH 1/3] VFIO: Add new cmd to receive eventfd from userspace to notify IOMMU fault event Date: Sun, 19 Feb 2017 22:47:07 +0800 Message-Id: <1487515629-13815-2-git-send-email-tianyu.lan@intel.com> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1487515629-13815-1-git-send-email-tianyu.lan@intel.com> References: <1487515629-13815-1-git-send-email-tianyu.lan@intel.com> Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP This patch is to receive IOMMU fault eventfd and IOMMU fault event flag from userspace. VFIO should register IOMMU fault event handler according fault event flag which designates what kind of fault events need to be reported. When VFIO get IOMMU fault event from IOMMU driver, it should notify userspace via received fd. Signed-off-by: Lan Tianyu --- drivers/vfio/vfio_iommu_type1.c | 45 +++++++++++++++++++++++++++++++++++++++++ include/uapi/linux/vfio.h | 15 ++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c index b3cc33f..46674ea 100644 --- a/drivers/vfio/vfio_iommu_type1.c +++ b/drivers/vfio/vfio_iommu_type1.c @@ -38,6 +38,7 @@ #include #include #include +#include #define DRIVER_VERSION "0.2" #define DRIVER_AUTHOR "Alex Williamson " @@ -61,6 +62,8 @@ struct vfio_iommu { struct mutex lock; struct rb_root dma_list; struct blocking_notifier_head notifier; + struct eventfd_ctx *iommu_fault_fd; + struct mutex fault_lock; bool v2; bool nesting; }; @@ -1452,6 +1455,7 @@ static void *vfio_iommu_type1_open(unsigned long arg) INIT_LIST_HEAD(&iommu->domain_list); iommu->dma_list = RB_ROOT; mutex_init(&iommu->lock); + mutex_init(&iommu->fault_lock); BLOCKING_INIT_NOTIFIER_HEAD(&iommu->notifier); return iommu; @@ -1582,6 +1586,47 @@ static long vfio_iommu_type1_ioctl(void *iommu_data, return copy_to_user((void __user *)arg, &unmap, minsz) ? -EFAULT : 0; + } else if (cmd == VFIO_IOMMU_SET_FAULT_EVENTFD) { + struct vfio_iommu_type1_set_fault_eventfd eventfd; + int fd; + int ret; + + minsz = offsetofend(struct vfio_iommu_type1_set_fault_eventfd, + fd); + + if (copy_from_user(&eventfd, (void __user *)arg, minsz)) + return -EFAULT; + + if (eventfd.argsz < minsz) + return -EINVAL; + + + mutex_lock(&iommu->fault_lock); + + fd = eventfd.fd; + if (fd == -1) { + if (iommu->iommu_fault_fd) + eventfd_ctx_put(iommu->iommu_fault_fd); + iommu->iommu_fault_fd = NULL; + ret = 0; + } else if (fd >= 0) { + struct eventfd_ctx *ctx; + + ctx = eventfd_ctx_fdget(fd); + if (IS_ERR(ctx)) + return PTR_ERR(ctx); + + if (ctx) + eventfd_ctx_put(ctx); + + iommu->iommu_fault_fd = ctx; + ret = 0; + } else + ret = -EINVAL; + + mutex_unlock(&iommu->fault_lock); + + return ret; } return -ENOTTY; diff --git a/include/uapi/linux/vfio.h b/include/uapi/linux/vfio.h index 519eff3..8616334 100644 --- a/include/uapi/linux/vfio.h +++ b/include/uapi/linux/vfio.h @@ -547,6 +547,21 @@ struct vfio_iommu_type1_dma_unmap { #define VFIO_IOMMU_ENABLE _IO(VFIO_TYPE, VFIO_BASE + 15) #define VFIO_IOMMU_DISABLE _IO(VFIO_TYPE, VFIO_BASE + 16) +/* + * VFIO_IOMMU_SET_FAULT_EVENT_FD _IO(VFIO_TYPE, VFIO_BASE + 17) + * + * Receive eventfd from userspace to notify fault event from IOMMU. + */ +struct vfio_iommu_type1_set_fault_eventfd { + __u32 argsz; + __u32 flags; +/* What IOMMU Fault events should be reported. */ +#define VFIO_IOMMU_UR_FAULT_WITHOUT_PASID (1 << 0) + __s32 fd; /* Eventfd from user space */ +}; + +#define VFIO_IOMMU_SET_FAULT_EVENTFD _IO(VFIO_TYPE, VFIO_BASE + 17) + /* -------- Additional API for SPAPR TCE (Server POWERPC) IOMMU -------- */ /*