From patchwork Sun Feb 19 14:47:08 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "lan,Tianyu" X-Patchwork-Id: 9581617 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 0226F6045A for ; Sun, 19 Feb 2017 14:54:13 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id DCA7128709 for ; Sun, 19 Feb 2017 14:54:12 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id D15E6286CB; Sun, 19 Feb 2017 14:54:12 +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 707E8286CB for ; Sun, 19 Feb 2017 14:54:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751583AbdBSOyI (ORCPT ); Sun, 19 Feb 2017 09:54:08 -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 S1751452AbdBSOyF (ORCPT ); Sun, 19 Feb 2017 09:54:05 -0500 Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by fmsmga104.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 19 Feb 2017 06:54:05 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.35,181,1484035200"; d="scan'208";a="67105436" Received: from lantianyu-ws.sh.intel.com (HELO localhost) ([10.239.159.159]) by fmsmga005.fm.intel.com with ESMTP; 19 Feb 2017 06:54:03 -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 2/3] VFIO: Add IOMMU fault notifier callback Date: Sun, 19 Feb 2017 22:47:08 +0800 Message-Id: <1487515629-13815-3-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 add callback to handle fault event reported by IOMMU driver. Callback stores fault into an array and notify userspace via eventfd to read fault info. Signed-off-by: Lan Tianyu --- drivers/vfio/vfio_iommu_type1.c | 30 ++++++++++++++++++++++++++++++ include/linux/iommu.h | 7 +++++++ include/uapi/linux/vfio.h | 7 +++++++ 3 files changed, 44 insertions(+) diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c index 46674ea..dc434a3 100644 --- a/drivers/vfio/vfio_iommu_type1.c +++ b/drivers/vfio/vfio_iommu_type1.c @@ -56,6 +56,8 @@ MODULE_PARM_DESC(disable_hugepages, "Disable VFIO IOMMU support for IOMMU hugepages."); +#define NR_IOMMU_FAULT_INFO 10 + struct vfio_iommu { struct list_head domain_list; struct vfio_domain *external_domain; /* domain for external user */ @@ -64,6 +66,9 @@ struct vfio_iommu { struct blocking_notifier_head notifier; struct eventfd_ctx *iommu_fault_fd; struct mutex fault_lock; + struct vfio_iommu_fault_info fault_info[NR_IOMMU_FAULT_INFO]; + struct blocking_notifier_head iommu_fault_notifier; + u8 fault_count; bool v2; bool nesting; }; @@ -1456,6 +1461,7 @@ static void *vfio_iommu_type1_open(unsigned long arg) iommu->dma_list = RB_ROOT; mutex_init(&iommu->lock); mutex_init(&iommu->fault_lock); + iommu->fault_count = 0; BLOCKING_INIT_NOTIFIER_HEAD(&iommu->notifier); return iommu; @@ -1516,6 +1522,30 @@ static int vfio_domains_have_iommu_cache(struct vfio_iommu *iommu) return ret; } +static int vfio_iommu_fault_event_notifier(struct notifier_block *nb, + struct iommu_fault_info *fault_info, + void *data) +{ + struct vfio_iommu *iommu = data; + struct vfio_iommu_fault_info *info; + + mutex_lock(&iommu->fault_lock); + + info = &iommu->fault_info[iommu->fault_count]; + info->addr = fault_info->addr; + info->sid = fault_info->sid; + info->fault_reason = fault_info->fault_reason; + info->is_write = fault_info->is_write; + + iommu->fault_count++; + + if (iommu->iommu_fault_fd) + eventfd_signal(iommu->iommu_fault_fd, 1); + + mutex_unlock(&iommu->fault_lock); + return 0; +} + static long vfio_iommu_type1_ioctl(void *iommu_data, unsigned int cmd, unsigned long arg) { diff --git a/include/linux/iommu.h b/include/linux/iommu.h index 0ff5111..b6a7bdb 100644 --- a/include/linux/iommu.h +++ b/include/linux/iommu.h @@ -86,6 +86,13 @@ struct iommu_domain { void *iova_cookie; }; +struct iommu_fault_info { + __u64 addr; + __u16 sid; + __u8 fault_reason; + __u8 is_write:1; +}; + enum iommu_cap { IOMMU_CAP_CACHE_COHERENCY, /* IOMMU can enforce cache coherent DMA transactions */ diff --git a/include/uapi/linux/vfio.h b/include/uapi/linux/vfio.h index 8616334..da359dd 100644 --- a/include/uapi/linux/vfio.h +++ b/include/uapi/linux/vfio.h @@ -562,6 +562,13 @@ struct vfio_iommu_type1_set_fault_eventfd { #define VFIO_IOMMU_SET_FAULT_EVENTFD _IO(VFIO_TYPE, VFIO_BASE + 17) +struct vfio_iommu_fault_info { + __u64 addr; + __u16 sid; + __u8 fault_reason; + __u8 is_write:1; +}; + /* -------- Additional API for SPAPR TCE (Server POWERPC) IOMMU -------- */ /*