Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp150079pxk; Thu, 24 Sep 2020 01:53:50 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxEwBzdw7TB0pWNNulSdYmscaYZ87qUPbSrvO8z4lF8AE5P8mZ5g61ompmRQFGz52WhNT99 X-Received: by 2002:a17:906:4b4a:: with SMTP id j10mr3544537ejv.498.1600937630507; Thu, 24 Sep 2020 01:53:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1600937630; cv=none; d=google.com; s=arc-20160816; b=0GzYUf1ESdTt7Me7mDLhW92IMC3zHMkx9BpWt8vNY2LBoyrXVrZmk2Xl7yiazm38vz 5QxQ4YNzJxPDYyB4AubEmC07zWqOCSAVqOgM46RKDsevYZNJB0BlqCZjQEyAGNTCn3uE BsCxfIOcnihsgF0RiMR0evOVKDi7Fkc7dMTgpnn34ZJmWPM701M/Z1TWZ9mzaIhK+bB4 1/6JdZAO13zM4mM1pfhIQtIEE/ze/fUGQmL+iLTJs0PeS4Zk2ZGVbIDXYTbn27FTwXdo 0qW4DSU9VH2BUa0mOcwxT0IfQ66DhakXTty1BWP0f7Ll+YX2fzg0aHiBCIk4xmyl17vO py9g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:from:references :to:subject; bh=MM9i2UqfmZRmJTjYA4tq6YcHxkPuH2FzFD7v471A/yk=; b=bLXVM6DVWAvFlXGbfiSIMsjyNUl4DGiADM3QKPvMiIxqZhKGlUL+BljbdJDwhq/bTn jM9UhSCrEHHP8eB4iOoTigqmlITuglrbdQlzB89j5Hxbjq6ogsHBJzLbNPSNJaO41CVv 5dJH6epKe84NE+m1b4to2aNL/Mmh/Z8RLEkOmWu4eJLQP7ylmv8skzXLqYRgHrvfmT1u CfGg5yFM3O6fxO6Vy2mKY456Kj+ldJK0UsHyJ1J+hC6tyYohq0ILNEWMLZ48dXGJoNfv VQ/gLWvC3f29JQ9gY7SDBuB59RbAnRGRmXYfSnq1EXkNoeVH8EMCBNHBYDfGK3sFe6Ti gcjQ== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id x23si1601716ejb.588.2020.09.24.01.53.27; Thu, 24 Sep 2020 01:53:50 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727328AbgIXIuE (ORCPT + 99 others); Thu, 24 Sep 2020 04:50:04 -0400 Received: from szxga05-in.huawei.com ([45.249.212.191]:14227 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726786AbgIXIuE (ORCPT ); Thu, 24 Sep 2020 04:50:04 -0400 Received: from DGGEMS408-HUB.china.huawei.com (unknown [172.30.72.59]) by Forcepoint Email with ESMTP id E1988906A3711F42ACCA; Thu, 24 Sep 2020 16:49:59 +0800 (CST) Received: from [10.174.185.226] (10.174.185.226) by DGGEMS408-HUB.china.huawei.com (10.3.19.208) with Microsoft SMTP Server id 14.3.487.0; Thu, 24 Sep 2020 16:49:53 +0800 Subject: Re: [PATCH v10 05/11] vfio/pci: Register an iommu fault handler To: Eric Auger , , , , , , , , , , References: <20200320161911.27494-1-eric.auger@redhat.com> <20200320161911.27494-6-eric.auger@redhat.com> From: Zenghui Yu Message-ID: <571978ff-ee8a-6e9f-6755-519d0871646f@huawei.com> Date: Thu, 24 Sep 2020 16:49:53 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:68.0) Gecko/20100101 Thunderbird/68.9.0 MIME-Version: 1.0 In-Reply-To: <20200320161911.27494-6-eric.auger@redhat.com> Content-Type: text/plain; charset="utf-8"; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit X-Originating-IP: [10.174.185.226] X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Eric, On 2020/3/21 0:19, Eric Auger wrote: > Register an IOMMU fault handler which records faults in > the DMA FAULT region ring buffer. In a subsequent patch, we > will add the signaling of a specific eventfd to allow the > userspace to be notified whenever a new fault as shown up. > > Signed-off-by: Eric Auger [...] > diff --git a/drivers/vfio/pci/vfio_pci.c b/drivers/vfio/pci/vfio_pci.c > index 586b89debed5..69595c240baf 100644 > --- a/drivers/vfio/pci/vfio_pci.c > +++ b/drivers/vfio/pci/vfio_pci.c > @@ -27,6 +27,7 @@ > #include > #include > #include > +#include > > #include "vfio_pci_private.h" > > @@ -283,6 +284,38 @@ static const struct vfio_pci_regops vfio_pci_dma_fault_regops = { > .add_capability = vfio_pci_dma_fault_add_capability, > }; > > +int vfio_pci_iommu_dev_fault_handler(struct iommu_fault *fault, void *data) > +{ > + struct vfio_pci_device *vdev = (struct vfio_pci_device *)data; > + struct vfio_region_dma_fault *reg = > + (struct vfio_region_dma_fault *)vdev->fault_pages; > + struct iommu_fault *new = > + (struct iommu_fault *)(vdev->fault_pages + reg->offset + > + reg->head * reg->entry_size); Shouldn't 'reg->head' be protected under the fault_queue_lock? Otherwise things may change behind our backs... We shouldn't take any assumption about how IOMMU driver would report the fault (serially or in parallel), I think. > + int head, tail, size; > + int ret = 0; > + > + if (fault->type != IOMMU_FAULT_DMA_UNRECOV) > + return -ENOENT; > + > + mutex_lock(&vdev->fault_queue_lock); > + > + head = reg->head; > + tail = reg->tail; > + size = reg->nb_entries; > + > + if (CIRC_SPACE(head, tail, size) < 1) { > + ret = -ENOSPC; > + goto unlock; > + } > + > + *new = *fault; > + reg->head = (head + 1) % size; > +unlock: > + mutex_unlock(&vdev->fault_queue_lock); > + return ret; > +} > + > #define DMA_FAULT_RING_LENGTH 512 > > static int vfio_pci_init_dma_fault_region(struct vfio_pci_device *vdev) > @@ -317,6 +350,13 @@ static int vfio_pci_init_dma_fault_region(struct vfio_pci_device *vdev) > header->entry_size = sizeof(struct iommu_fault); > header->nb_entries = DMA_FAULT_RING_LENGTH; > header->offset = sizeof(struct vfio_region_dma_fault); > + > + ret = iommu_register_device_fault_handler(&vdev->pdev->dev, > + vfio_pci_iommu_dev_fault_handler, > + vdev); > + if (ret) > + goto out; > + > return 0; > out: > kfree(vdev->fault_pages); Thanks, Zenghui