Received: by 2002:a05:6a10:17d3:0:0:0:0 with SMTP id hz19csp1012702pxb; Sun, 11 Apr 2021 04:52:24 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzwnMxpc0TGfzFhZoXMfvEKaLV5V6XrxHl1FDjjyPEX8TKNd+CDhIEsmOgjAOAdHJfJRx6J X-Received: by 2002:a05:6a00:cc8:b029:217:4606:5952 with SMTP id b8-20020a056a000cc8b029021746065952mr20944372pfv.50.1618141944215; Sun, 11 Apr 2021 04:52:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1618141944; cv=none; d=google.com; s=arc-20160816; b=AaLhYSG+ZWY0AhSm5/7HzMvHxOO/9GCzgcHON/jqRJWzgdqJXwb8NdkOPzGALvmsBZ J/DEmR81BiVZUiiKfLOfVquJ2mkk+3MpD1h+n6skEZfDin4bsmjPMXjuogNSbripgm9d 6rv1g4/xDT6Pc5c98/AuxlyPNIegGBymYvK/NH6MZzyQn3gczCv9lW1mccHupnbX8vkX yjU7bQkOO0Jkt/0OklDUHg6KwE/RfDx+xgQFjLqyyA4fYvDlFfYdbrxXxzCIrDaod83J fjacJ5+mRjQvQ6H9Le1+0ppvMB8A3Heyr73TqBEY91CidV4jEA9z45oamXSVwy9aKeen 13Hg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=0aSD6V877Q7HGWA4DO5hGFo60VEegdaef4o5r+M0zIE=; b=zhY59k9xi/Aqy30dKxDKsu3NB9lGKTHdj5MALi06FQDr4iKkk3WX+1TP4i5+Y13OyA jUD+75ZtZsPDf81Rp76VIf8GWRpzYKTE8v9c+8AICnefdEVAGHLagiRkrYJAC0chHWIX U0a8iwiHK/yMG3Y4vCIh2qWbpB6dqmAvZ9Ipfo4/PgziGI+1XtBQ+NtfK9Qj0fFhkqRz YyhxPWgwkz+jsg22T2QQusRqTTbMDTxy288iDbQQDZfwGzG9iz6seWf7xDteVVvHp1Lz rabFINZOSIlb5XuIu4KGjxTQvjdMAlkYRevIfQiuZJ2FmOsyH3WRZKPeWYWMidE7dzpt cLXg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=YLz2Q+RY; 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=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id b9si10196268pgr.217.2021.04.11.04.52.11; Sun, 11 Apr 2021 04:52:24 -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=@redhat.com header.s=mimecast20190719 header.b=YLz2Q+RY; 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=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235550AbhDKLu2 (ORCPT + 99 others); Sun, 11 Apr 2021 07:50:28 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:21240 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235655AbhDKLuF (ORCPT ); Sun, 11 Apr 2021 07:50:05 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1618141788; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=0aSD6V877Q7HGWA4DO5hGFo60VEegdaef4o5r+M0zIE=; b=YLz2Q+RYTgxGTgoLt8H+lJAZRuHBqoMzL/7yxmI23rPtKQI1reakfKMZf1TxV/vMl88dJC ln1tu7EQKphucwFcBOUmyIrhp0cqAY8UW05w47NQYrVH+LlwxZuSftGntlQpAvcvPG2+Kp Dn3glavJibAc2ImAf3NZxE4KWeOE04Q= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-152-c866b95IPdOWfDRSXpyyTw-1; Sun, 11 Apr 2021 07:49:47 -0400 X-MC-Unique: c866b95IPdOWfDRSXpyyTw-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 9E03E10054F6; Sun, 11 Apr 2021 11:49:44 +0000 (UTC) Received: from laptop.redhat.com (ovpn-112-22.ams2.redhat.com [10.36.112.22]) by smtp.corp.redhat.com (Postfix) with ESMTP id 6BBC25C266; Sun, 11 Apr 2021 11:49:36 +0000 (UTC) From: Eric Auger To: eric.auger.pro@gmail.com, eric.auger@redhat.com, iommu@lists.linux-foundation.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, kvmarm@lists.cs.columbia.edu, will@kernel.org, maz@kernel.org, robin.murphy@arm.com, joro@8bytes.org, alex.williamson@redhat.com, tn@semihalf.com, zhukeqian1@huawei.com Cc: jacob.jun.pan@linux.intel.com, yi.l.liu@intel.com, wangxingang5@huawei.com, jean-philippe@linaro.org, zhangfei.gao@linaro.org, zhangfei.gao@gmail.com, vivek.gautam@arm.com, shameerali.kolothum.thodi@huawei.com, yuzenghui@huawei.com, nicoleotsuka@gmail.com, lushenming@huawei.com, vsethi@nvidia.com, chenxiang66@hisilicon.com, vdumpa@nvidia.com, jiangkunkun@huawei.com Subject: [PATCH v13 13/13] vfio/pci: Inject page response upon response region fill Date: Sun, 11 Apr 2021 13:46:59 +0200 Message-Id: <20210411114659.15051-14-eric.auger@redhat.com> In-Reply-To: <20210411114659.15051-1-eric.auger@redhat.com> References: <20210411114659.15051-1-eric.auger@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org When the userspace increments the head of the page response buffer ring, let's push the response into the iommu layer. This is done through a workqueue that pops the responses from the ring buffer and increment the tail. Signed-off-by: Eric Auger --- drivers/vfio/pci/vfio_pci.c | 40 +++++++++++++++++++++++++++++ drivers/vfio/pci/vfio_pci_private.h | 7 +++++ drivers/vfio/pci/vfio_pci_rdwr.c | 1 + 3 files changed, 48 insertions(+) diff --git a/drivers/vfio/pci/vfio_pci.c b/drivers/vfio/pci/vfio_pci.c index 560b1a830726..bb4a0e1e39bf 100644 --- a/drivers/vfio/pci/vfio_pci.c +++ b/drivers/vfio/pci/vfio_pci.c @@ -552,6 +552,32 @@ static int vfio_pci_dma_fault_init(struct vfio_pci_device *vdev) return ret; } +static void dma_response_inject(struct work_struct *work) +{ + struct vfio_pci_dma_fault_response_work *rwork = + container_of(work, struct vfio_pci_dma_fault_response_work, inject); + struct vfio_region_dma_fault_response *header = rwork->header; + struct vfio_pci_device *vdev = rwork->vdev; + struct iommu_page_response *resp; + u32 tail, head, size; + + mutex_lock(&vdev->fault_response_queue_lock); + + tail = header->tail; + head = header->head; + size = header->nb_entries; + + while (CIRC_CNT(head, tail, size) >= 1) { + resp = (struct iommu_page_response *)(vdev->fault_response_pages + header->offset + + tail * header->entry_size); + + /* TODO: properly handle the return value */ + iommu_page_response(&vdev->pdev->dev, resp); + header->tail = tail = (tail + 1) % size; + } + mutex_unlock(&vdev->fault_response_queue_lock); +} + #define DMA_FAULT_RESPONSE_RING_LENGTH 512 static int vfio_pci_dma_fault_response_init(struct vfio_pci_device *vdev) @@ -597,8 +623,22 @@ static int vfio_pci_dma_fault_response_init(struct vfio_pci_device *vdev) header->nb_entries = DMA_FAULT_RESPONSE_RING_LENGTH; header->offset = PAGE_SIZE; + vdev->response_work = kzalloc(sizeof(*vdev->response_work), GFP_KERNEL); + if (!vdev->response_work) + goto out; + vdev->response_work->header = header; + vdev->response_work->vdev = vdev; + + /* launch the thread that will extract the response */ + INIT_WORK(&vdev->response_work->inject, dma_response_inject); + vdev->dma_fault_response_wq = + create_singlethread_workqueue("vfio-dma-fault-response"); + if (!vdev->dma_fault_response_wq) + return -ENOMEM; + return 0; out: + kfree(vdev->fault_response_pages); vdev->fault_response_pages = NULL; return ret; } diff --git a/drivers/vfio/pci/vfio_pci_private.h b/drivers/vfio/pci/vfio_pci_private.h index f7b1e7fb86e5..835fbb221dea 100644 --- a/drivers/vfio/pci/vfio_pci_private.h +++ b/drivers/vfio/pci/vfio_pci_private.h @@ -52,6 +52,12 @@ struct vfio_pci_irq_ctx { struct irq_bypass_producer producer; }; +struct vfio_pci_dma_fault_response_work { + struct work_struct inject; + struct vfio_region_dma_fault_response *header; + struct vfio_pci_device *vdev; +}; + struct vfio_pci_device; struct vfio_pci_region; @@ -146,6 +152,7 @@ struct vfio_pci_device { u8 *fault_pages; u8 *fault_response_pages; struct workqueue_struct *dma_fault_response_wq; + struct vfio_pci_dma_fault_response_work *response_work; struct mutex fault_queue_lock; struct mutex fault_response_queue_lock; struct list_head dummy_resources_list; diff --git a/drivers/vfio/pci/vfio_pci_rdwr.c b/drivers/vfio/pci/vfio_pci_rdwr.c index efde0793360b..78c494fe35cc 100644 --- a/drivers/vfio/pci/vfio_pci_rdwr.c +++ b/drivers/vfio/pci/vfio_pci_rdwr.c @@ -430,6 +430,7 @@ size_t vfio_pci_dma_fault_response_rw(struct vfio_pci_device *vdev, char __user mutex_lock(&vdev->fault_response_queue_lock); header->head = new_head; mutex_unlock(&vdev->fault_response_queue_lock); + queue_work(vdev->dma_fault_response_wq, &vdev->response_work->inject); } else { if (copy_to_user(buf, base + pos, count)) return -EFAULT; -- 2.26.3