From patchwork Thu Jan 16 18:17:57 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alex Williamson X-Patchwork-Id: 11337539 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id EAFBF14B7 for ; Thu, 16 Jan 2020 18:18:10 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id C7D9B20684 for ; Thu, 16 Jan 2020 18:18:10 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="b2hJqLWe" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2392755AbgAPSSJ (ORCPT ); Thu, 16 Jan 2020 13:18:09 -0500 Received: from us-smtp-delivery-1.mimecast.com ([207.211.31.120]:24803 "EHLO us-smtp-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S2391646AbgAPSSG (ORCPT ); Thu, 16 Jan 2020 13:18:06 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1579198685; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=niuGWb81Q8zr23FXcglCGpZUWD+TFqFFh4zn2anjs0s=; b=b2hJqLWeO4Od0oZHkg9fqdKheEF5YGfL2XwSqf0pqawmWqHpy961NLtKVY8V7g5aYLNmz7 rylkERsO83yAut8OeFjLuY9jYKjU3ULjvQvz1DtivYoe8vDu9lT3eKnQZ+4pQzadyE/0rm /SDTfl7UyCSfs9VyCGN0jQd3YSf3U/0= 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-327-2pz5OV7JOFemg2VndDEo1Q-1; Thu, 16 Jan 2020 13:18:01 -0500 X-MC-Unique: 2pz5OV7JOFemg2VndDEo1Q-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id A06B7800D5C; Thu, 16 Jan 2020 18:18:00 +0000 (UTC) Received: from gimli.home (ovpn-116-28.phx2.redhat.com [10.3.116.28]) by smtp.corp.redhat.com (Postfix) with ESMTP id 496AF80617; Thu, 16 Jan 2020 18:17:57 +0000 (UTC) Subject: [RFC PATCH 1/3] vfio/type1: Convert vfio_iommu.lock from mutex to rwsem From: Alex Williamson To: yan.y.zhao@intel.com Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Date: Thu, 16 Jan 2020 11:17:57 -0700 Message-ID: <157919867745.21002.3239982207630090516.stgit@gimli.home> In-Reply-To: <157919849533.21002.4782774695733669879.stgit@gimli.home> References: <157919849533.21002.4782774695733669879.stgit@gimli.home> User-Agent: StGit/0.19-dirty MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org As a first step reducing lock contention, maintain the same locking granularity using a rwsem rather than a mutex. Signed-off-by: Alex Williamson --- drivers/vfio/vfio_iommu_type1.c | 51 ++++++++++++++++++++------------------- 1 file changed, 26 insertions(+), 25 deletions(-) diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c index 2ada8e6cdb88..7ae58350af5b 100644 --- a/drivers/vfio/vfio_iommu_type1.c +++ b/drivers/vfio/vfio_iommu_type1.c @@ -28,6 +28,7 @@ #include #include #include +#include #include #include #include @@ -64,7 +65,7 @@ struct vfio_iommu { struct list_head domain_list; struct list_head iova_list; struct vfio_domain *external_domain; /* domain for external user */ - struct mutex lock; + struct rw_semaphore lock; struct rb_root dma_list; struct blocking_notifier_head notifier; unsigned int dma_avail; @@ -538,7 +539,7 @@ static int vfio_iommu_type1_pin_pages(void *iommu_data, if (!iommu->v2) return -EACCES; - mutex_lock(&iommu->lock); + down_write(&iommu->lock); /* Fail if notifier list is empty */ if (!iommu->notifier.head) { @@ -602,7 +603,7 @@ static int vfio_iommu_type1_pin_pages(void *iommu_data, phys_pfn[j] = 0; } pin_done: - mutex_unlock(&iommu->lock); + up_write(&iommu->lock); return ret; } @@ -621,7 +622,7 @@ static int vfio_iommu_type1_unpin_pages(void *iommu_data, if (!iommu->v2) return -EACCES; - mutex_lock(&iommu->lock); + down_write(&iommu->lock); do_accounting = !IS_IOMMU_CAP_DOMAIN_IN_CONTAINER(iommu); for (i = 0; i < npage; i++) { @@ -636,7 +637,7 @@ static int vfio_iommu_type1_unpin_pages(void *iommu_data, } unpin_exit: - mutex_unlock(&iommu->lock); + up_write(&iommu->lock); return i > npage ? npage : (i > 0 ? i : -EINVAL); } @@ -829,10 +830,10 @@ static unsigned long vfio_pgsize_bitmap(struct vfio_iommu *iommu) struct vfio_domain *domain; unsigned long bitmap = ULONG_MAX; - mutex_lock(&iommu->lock); + down_write(&iommu->lock); list_for_each_entry(domain, &iommu->domain_list, next) bitmap &= domain->domain->pgsize_bitmap; - mutex_unlock(&iommu->lock); + up_write(&iommu->lock); /* * In case the IOMMU supports page sizes smaller than PAGE_SIZE @@ -870,7 +871,7 @@ static int vfio_dma_do_unmap(struct vfio_iommu *iommu, WARN_ON(mask & PAGE_MASK); again: - mutex_lock(&iommu->lock); + down_write(&iommu->lock); /* * vfio-iommu-type1 (v1) - User mappings were coalesced together to @@ -945,7 +946,7 @@ static int vfio_dma_do_unmap(struct vfio_iommu *iommu, * Vendor drivers MUST unpin pages in response to an * invalidation. */ - mutex_unlock(&iommu->lock); + up_write(&iommu->lock); blocking_notifier_call_chain(&iommu->notifier, VFIO_IOMMU_NOTIFY_DMA_UNMAP, &nb_unmap); @@ -956,7 +957,7 @@ static int vfio_dma_do_unmap(struct vfio_iommu *iommu, } unlock: - mutex_unlock(&iommu->lock); + up_write(&iommu->lock); /* Report how much was unmapped */ unmap->size = unmapped; @@ -1081,7 +1082,7 @@ static int vfio_dma_do_map(struct vfio_iommu *iommu, if (iova + size - 1 < iova || vaddr + size - 1 < vaddr) return -EINVAL; - mutex_lock(&iommu->lock); + down_write(&iommu->lock); if (vfio_find_dma(iommu, iova, size)) { ret = -EEXIST; @@ -1150,7 +1151,7 @@ static int vfio_dma_do_map(struct vfio_iommu *iommu, ret = vfio_pin_map_dma(iommu, dma, size); out_unlock: - mutex_unlock(&iommu->lock); + up_write(&iommu->lock); return ret; } @@ -1645,18 +1646,18 @@ static int vfio_iommu_type1_attach_group(void *iommu_data, LIST_HEAD(iova_copy); LIST_HEAD(group_resv_regions); - mutex_lock(&iommu->lock); + down_write(&iommu->lock); list_for_each_entry(d, &iommu->domain_list, next) { if (find_iommu_group(d, iommu_group)) { - mutex_unlock(&iommu->lock); + up_write(&iommu->lock); return -EINVAL; } } if (iommu->external_domain) { if (find_iommu_group(iommu->external_domain, iommu_group)) { - mutex_unlock(&iommu->lock); + up_write(&iommu->lock); return -EINVAL; } } @@ -1693,7 +1694,7 @@ static int vfio_iommu_type1_attach_group(void *iommu_data, list_add(&group->next, &iommu->external_domain->group_list); - mutex_unlock(&iommu->lock); + up_write(&iommu->lock); return 0; } @@ -1815,7 +1816,7 @@ static int vfio_iommu_type1_attach_group(void *iommu_data, done: /* Delete the old one and insert new iova list */ vfio_iommu_iova_insert_copy(iommu, &iova_copy); - mutex_unlock(&iommu->lock); + up_write(&iommu->lock); vfio_iommu_resv_free(&group_resv_regions); return 0; @@ -1829,7 +1830,7 @@ static int vfio_iommu_type1_attach_group(void *iommu_data, out_free: kfree(domain); kfree(group); - mutex_unlock(&iommu->lock); + up_write(&iommu->lock); return ret; } @@ -1969,7 +1970,7 @@ static void vfio_iommu_type1_detach_group(void *iommu_data, struct vfio_group *group; LIST_HEAD(iova_copy); - mutex_lock(&iommu->lock); + down_write(&iommu->lock); if (iommu->external_domain) { group = find_iommu_group(iommu->external_domain, iommu_group); @@ -2033,7 +2034,7 @@ static void vfio_iommu_type1_detach_group(void *iommu_data, vfio_iommu_iova_free(&iova_copy); detach_group_done: - mutex_unlock(&iommu->lock); + up_write(&iommu->lock); } static void *vfio_iommu_type1_open(unsigned long arg) @@ -2062,7 +2063,7 @@ static void *vfio_iommu_type1_open(unsigned long arg) INIT_LIST_HEAD(&iommu->iova_list); iommu->dma_list = RB_ROOT; iommu->dma_avail = dma_entry_limit; - mutex_init(&iommu->lock); + init_rwsem(&iommu->lock); BLOCKING_INIT_NOTIFIER_HEAD(&iommu->notifier); return iommu; @@ -2114,14 +2115,14 @@ static int vfio_domains_have_iommu_cache(struct vfio_iommu *iommu) struct vfio_domain *domain; int ret = 1; - mutex_lock(&iommu->lock); + down_write(&iommu->lock); list_for_each_entry(domain, &iommu->domain_list, next) { if (!(domain->prot & IOMMU_CACHE)) { ret = 0; break; } } - mutex_unlock(&iommu->lock); + up_write(&iommu->lock); return ret; } @@ -2155,7 +2156,7 @@ static int vfio_iommu_iova_build_caps(struct vfio_iommu *iommu, size_t size; int iovas = 0, i = 0, ret; - mutex_lock(&iommu->lock); + down_write(&iommu->lock); list_for_each_entry(iova, &iommu->iova_list, list) iovas++; @@ -2189,7 +2190,7 @@ static int vfio_iommu_iova_build_caps(struct vfio_iommu *iommu, kfree(cap_iovas); out_unlock: - mutex_unlock(&iommu->lock); + up_write(&iommu->lock); return ret; } From patchwork Thu Jan 16 18:18:05 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alex Williamson X-Patchwork-Id: 11337541 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id BF23F109A for ; Thu, 16 Jan 2020 18:18:17 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 9C68B20684 for ; Thu, 16 Jan 2020 18:18:17 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="AIJytp3K" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2387663AbgAPSSQ (ORCPT ); Thu, 16 Jan 2020 13:18:16 -0500 Received: from us-smtp-1.mimecast.com ([205.139.110.61]:44670 "EHLO us-smtp-delivery-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S2436490AbgAPSSN (ORCPT ); Thu, 16 Jan 2020 13:18:13 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1579198693; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=LP60/34E9bpmTo0YvyONW5BNSwA1vtrUNhAbAktUFEI=; b=AIJytp3KL8VRGBI6PYe5zYsIGRARzm1kEqe1beOU2tDfEmPztd8CEaHuAzpfDUWXE1I34j FiSskgjbwVyOhDAHuTGfbEt7qIxb3SnWa3FXv+2K01esqyyKZsZmOuSn0wgk7usKXhtvuX LCDXvbn3Rx6UZQqD6jMJ/OicZzQQCRY= 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-354-CqNHnw9EMeGDxSYvVUjxCA-1; Thu, 16 Jan 2020 13:18:09 -0500 X-MC-Unique: CqNHnw9EMeGDxSYvVUjxCA-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id C29D01088380; Thu, 16 Jan 2020 18:18:08 +0000 (UTC) Received: from gimli.home (ovpn-116-28.phx2.redhat.com [10.3.116.28]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7B5085D9C9; Thu, 16 Jan 2020 18:18:06 +0000 (UTC) Subject: [RFC PATCH 2/3] vfio/type1: Replace obvious read lock instances From: Alex Williamson To: yan.y.zhao@intel.com Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Date: Thu, 16 Jan 2020 11:18:05 -0700 Message-ID: <157919868571.21002.6651113746893305820.stgit@gimli.home> In-Reply-To: <157919849533.21002.4782774695733669879.stgit@gimli.home> References: <157919849533.21002.4782774695733669879.stgit@gimli.home> User-Agent: StGit/0.19-dirty MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Replace some instances where no internal state is changed to read locks. Signed-off-by: Alex Williamson --- drivers/vfio/vfio_iommu_type1.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c index 7ae58350af5b..e78067cc74b3 100644 --- a/drivers/vfio/vfio_iommu_type1.c +++ b/drivers/vfio/vfio_iommu_type1.c @@ -830,10 +830,10 @@ static unsigned long vfio_pgsize_bitmap(struct vfio_iommu *iommu) struct vfio_domain *domain; unsigned long bitmap = ULONG_MAX; - down_write(&iommu->lock); + down_read(&iommu->lock); list_for_each_entry(domain, &iommu->domain_list, next) bitmap &= domain->domain->pgsize_bitmap; - up_write(&iommu->lock); + up_read(&iommu->lock); /* * In case the IOMMU supports page sizes smaller than PAGE_SIZE @@ -2115,14 +2115,14 @@ static int vfio_domains_have_iommu_cache(struct vfio_iommu *iommu) struct vfio_domain *domain; int ret = 1; - down_write(&iommu->lock); + down_read(&iommu->lock); list_for_each_entry(domain, &iommu->domain_list, next) { if (!(domain->prot & IOMMU_CACHE)) { ret = 0; break; } } - up_write(&iommu->lock); + up_read(&iommu->lock); return ret; } @@ -2156,7 +2156,7 @@ static int vfio_iommu_iova_build_caps(struct vfio_iommu *iommu, size_t size; int iovas = 0, i = 0, ret; - down_write(&iommu->lock); + down_read(&iommu->lock); list_for_each_entry(iova, &iommu->iova_list, list) iovas++; @@ -2190,7 +2190,7 @@ static int vfio_iommu_iova_build_caps(struct vfio_iommu *iommu, kfree(cap_iovas); out_unlock: - up_write(&iommu->lock); + up_read(&iommu->lock); return ret; } From patchwork Thu Jan 16 18:18:13 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alex Williamson X-Patchwork-Id: 11337543 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 5AD2E13B4 for ; Thu, 16 Jan 2020 18:18:26 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 3597D24684 for ; Thu, 16 Jan 2020 18:18:26 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="JoknkQfT" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2395376AbgAPSSX (ORCPT ); Thu, 16 Jan 2020 13:18:23 -0500 Received: from us-smtp-1.mimecast.com ([205.139.110.61]:35641 "EHLO us-smtp-1.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2391584AbgAPSSU (ORCPT ); Thu, 16 Jan 2020 13:18:20 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1579198699; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=k1/NMdNWn1AdZcspQctzd5y5I9ng1wPTgP3J0ukiT7Q=; b=JoknkQfT5ucMH2NQa0ZhWlA2mndVIttjue4/80/K3AzROjMwMHYP5GWsIiAIHG2QReeYAc uXlwftRIX48VeeLeAOoNUtF4blbvTdtU6iBQsdzXDeDfdmUqGi7ll0r5Cp+02QPcfs0bf7 yyFB/HsDPCuZI0LfxbUTR92BIS0tB8M= 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-14-LCMC3GR5MASKg19CERzZTw-1; Thu, 16 Jan 2020 13:18:15 -0500 X-MC-Unique: LCMC3GR5MASKg19CERzZTw-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 8A6DD800D48; Thu, 16 Jan 2020 18:18:14 +0000 (UTC) Received: from gimli.home (ovpn-116-28.phx2.redhat.com [10.3.116.28]) by smtp.corp.redhat.com (Postfix) with ESMTP id 444755D9C9; Thu, 16 Jan 2020 18:18:14 +0000 (UTC) Subject: [RFC PATCH 3/3] vfio/type1: Introduce pfn_list mutex From: Alex Williamson To: yan.y.zhao@intel.com Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Date: Thu, 16 Jan 2020 11:18:13 -0700 Message-ID: <157919869385.21002.5744246004583751102.stgit@gimli.home> In-Reply-To: <157919849533.21002.4782774695733669879.stgit@gimli.home> References: <157919849533.21002.4782774695733669879.stgit@gimli.home> User-Agent: StGit/0.19-dirty MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org We can promote external page {un}pinning to a reader lock, allowing concurrency since these don't change the vfio_iommu state. We do need to protect the vpfn list per vfio_dma in place of that serialization though. Signed-off-by: Alex Williamson --- drivers/vfio/vfio_iommu_type1.c | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c index e78067cc74b3..ea63306c16f7 100644 --- a/drivers/vfio/vfio_iommu_type1.c +++ b/drivers/vfio/vfio_iommu_type1.c @@ -90,6 +90,7 @@ struct vfio_dma { bool iommu_mapped; bool lock_cap; /* capable(CAP_IPC_LOCK) */ struct task_struct *task; + struct mutex pfn_list_lock; struct rb_root pfn_list; /* Ex-user pinned pfn list */ }; @@ -539,7 +540,7 @@ static int vfio_iommu_type1_pin_pages(void *iommu_data, if (!iommu->v2) return -EACCES; - down_write(&iommu->lock); + down_read(&iommu->lock); /* Fail if notifier list is empty */ if (!iommu->notifier.head) { @@ -570,8 +571,11 @@ static int vfio_iommu_type1_pin_pages(void *iommu_data, goto pin_unwind; } + mutex_lock(&dma->pfn_list_lock); + vpfn = vfio_iova_get_vfio_pfn(dma, iova); if (vpfn) { + mutex_unlock(&dma->pfn_list_lock); phys_pfn[i] = vpfn->pfn; continue; } @@ -579,14 +583,19 @@ static int vfio_iommu_type1_pin_pages(void *iommu_data, remote_vaddr = dma->vaddr + iova - dma->iova; ret = vfio_pin_page_external(dma, remote_vaddr, &phys_pfn[i], do_accounting); - if (ret) + if (ret) { + mutex_unlock(&dma->pfn_list_lock); goto pin_unwind; + } ret = vfio_add_to_pfn_list(dma, iova, phys_pfn[i]); if (ret) { vfio_unpin_page_external(dma, iova, do_accounting); + mutex_unlock(&dma->pfn_list_lock); goto pin_unwind; } + + mutex_unlock(&dma->pfn_list_lock); } ret = i; @@ -599,11 +608,13 @@ static int vfio_iommu_type1_pin_pages(void *iommu_data, iova = user_pfn[j] << PAGE_SHIFT; dma = vfio_find_dma(iommu, iova, PAGE_SIZE); + mutex_lock(&dma->pfn_list_lock); vfio_unpin_page_external(dma, iova, do_accounting); + mutex_unlock(&dma->pfn_list_lock); phys_pfn[j] = 0; } pin_done: - up_write(&iommu->lock); + up_read(&iommu->lock); return ret; } @@ -622,7 +633,7 @@ static int vfio_iommu_type1_unpin_pages(void *iommu_data, if (!iommu->v2) return -EACCES; - down_write(&iommu->lock); + down_read(&iommu->lock); do_accounting = !IS_IOMMU_CAP_DOMAIN_IN_CONTAINER(iommu); for (i = 0; i < npage; i++) { @@ -633,11 +644,13 @@ static int vfio_iommu_type1_unpin_pages(void *iommu_data, dma = vfio_find_dma(iommu, iova, PAGE_SIZE); if (!dma) goto unpin_exit; + mutex_lock(&dma->pfn_list_lock); vfio_unpin_page_external(dma, iova, do_accounting); + mutex_unlock(&dma->pfn_list_lock); } unpin_exit: - up_write(&iommu->lock); + up_read(&iommu->lock); return i > npage ? npage : (i > 0 ? i : -EINVAL); } @@ -1109,6 +1122,7 @@ static int vfio_dma_do_map(struct vfio_iommu *iommu, dma->iova = iova; dma->vaddr = vaddr; dma->prot = prot; + mutex_init(&dma->pfn_list_lock); /* * We need to be able to both add to a task's locked memory and test