From patchwork Sat Jul 23 02:02:47 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 12927052 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E836CCCA489 for ; Sat, 23 Jul 2022 02:04:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236374AbiGWCEW (ORCPT ); Fri, 22 Jul 2022 22:04:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50530 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232244AbiGWCES (ORCPT ); Fri, 22 Jul 2022 22:04:18 -0400 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2056.outbound.protection.outlook.com [40.107.237.56]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 05AD593686; Fri, 22 Jul 2022 19:04:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=KcOieI1QzXGXz4O/1a95MXVLp4jKPZ68zElm8DFghV5OJBcQ81m0/2R0T2g3bcelCHVfKW4lVrBZByZgMr1bpoBkTmFNqbF7K7x1X5DwQmOqvH2AYRlpzyoVu9pOn6dYexwwDPFBx3bB8fr/xZYCxPnCB5FAdGCXs5ieb+WZwCkUkHOR3zcX4XEM6tuurTUt6gl8Y09sEceOuGCUkAooi9qCII4JOJ0RKSqV5HTiamS9pkRAbIgWTm7pPH1wBRau7EGlk7izGMBF9HA/CKu8jyeLCrAIKmHPpV+4xQV97tp4tfPkbGpLkctMla6Nbc4VAY7uY6zMvglmeCbgMMRCKw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=W7EZc0LC2Jw4c0WihginmT32wjY/Gohmcnl1Pz6AneA=; b=mh7vbu9QRbAtlwEgX833IKTbWeIkBeP7lWuFqEdk8sD3pw1m1O77tPmN4QukDvUPD7Rtxc7P8NUUBoE1IWFnzWAL+vq83CCcS7KCC31VkWtNZALiUZV5lOnLPagbb9FiQGZqL7JpEj/QqO9ffkogPRdmsdfwkdfq1xmxWTNm00XcPk9ubCmwA7mJk6Qqi6xzRJeYOot2dSOGM9QoKnt9fo0jv1WypwyrHAFxh1R2GiITm/IujEBfY1VH8wHkPN3Xc+zguMQwBiIC2mKpyLQQKxvvgtql33bAXbIIl0a1zJtxIvJf9EngVOng9HcR7UmUCilB3UGtw2IRghlz0zLWug== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 12.22.5.234) smtp.rcpttodomain=linux.ibm.com smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=W7EZc0LC2Jw4c0WihginmT32wjY/Gohmcnl1Pz6AneA=; b=J4fTnkSO/3smwe3R5J19jmKstRM9Zq48FMHGw33R6jeMFYsDdDHiZBXcL4hyhnHcZXuwTAzTwHnQtnLFVKhJzXkvHlphVZ8hMBwpekADd1YGfQ55DRTTcIWddO3BjNgJgr6cnUrJ2GxMyYe7A4PmW0qtVhUW3Iz7qxl2XXl4e5v6hhXvD6gPHkSA/k/3k19IkTBHmMdy5ShVfq+S6wDHJ/HP3DyPwfHdWCalUvaVX/lMkwxidq3332E60I2GWBFsVQgm8n3YNsQCjdCuHAZe2E3jdo/5HL2/Kcd1TG2ZmJvFfjNRIyG4FIOUT5sUSc04pEXC4brb73qVjXFDeGVvgw== Received: from BN9PR03CA0936.namprd03.prod.outlook.com (2603:10b6:408:108::11) by SN1PR12MB2448.namprd12.prod.outlook.com (2603:10b6:802:28::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5458.19; Sat, 23 Jul 2022 02:04:12 +0000 Received: from BN8NAM11FT067.eop-nam11.prod.protection.outlook.com (2603:10b6:408:108:cafe::9a) by BN9PR03CA0936.outlook.office365.com (2603:10b6:408:108::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5458.18 via Frontend Transport; Sat, 23 Jul 2022 02:04:12 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 12.22.5.234) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 12.22.5.234 as permitted sender) receiver=protection.outlook.com; client-ip=12.22.5.234; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (12.22.5.234) by BN8NAM11FT067.mail.protection.outlook.com (10.13.177.159) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.20.5458.17 via Frontend Transport; Sat, 23 Jul 2022 02:04:11 +0000 Received: from drhqmail202.nvidia.com (10.126.190.181) by DRHQMAIL101.nvidia.com (10.27.9.10) with Microsoft SMTP Server (TLS) id 15.0.1497.32; Sat, 23 Jul 2022 02:04:11 +0000 Received: from drhqmail203.nvidia.com (10.126.190.182) by drhqmail202.nvidia.com (10.126.190.181) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.26; Fri, 22 Jul 2022 19:04:10 -0700 Received: from Asurada-Nvidia.nvidia.com (10.127.8.10) by mail.nvidia.com (10.126.190.182) with Microsoft SMTP Server id 15.2.986.26 via Frontend Transport; Fri, 22 Jul 2022 19:04:09 -0700 From: Nicolin Chen To: , , , , , , , , , , , , , , , , , , , , , , , , , , , CC: , , , , , , , , Subject: [PATCH v4 01/10] vfio: Make vfio_unpin_pages() return void Date: Fri, 22 Jul 2022 19:02:47 -0700 Message-ID: <20220723020256.30081-2-nicolinc@nvidia.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220723020256.30081-1-nicolinc@nvidia.com> References: <20220723020256.30081-1-nicolinc@nvidia.com> MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 5f94ad56-060f-43f9-040f-08da6c4fa321 X-MS-TrafficTypeDiagnostic: SN1PR12MB2448:EE_ X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: oZwHL4DWlMUJv0RvZFTqUNaVwFX2iLGF6+p4TpQ775ZGzEEQuprJEADUU30tyYgkoNqZ3HoroWG4a3InL6xaMVLNqaFEpaZ91QWHVg9IXgKYdkxH0gkTJo4FND5kcf+RAzPjC0WkYh+bnTUF0SnzXDTucX/sPT1o2I8xPwDIv/cDBVStUl4NmF0VUrutmKdAAvrLub1jYhUDQdGNt8blk6o3szHjXRv0HubCuGLHBxxcuslsMMenimuky1wMxytO2CgW9kYy7MKHXTVMTWwKJ/e88VIUoe2FZ7lmKPkI0wMViIJ00hEarZLj0/4CHVEoeUuvk55UwaCwY++yZZiJPM37GsDw0wIo8vthJmGStRAJvDN6ECqFWPwfrnm/HHtIcngTrwbZd+YcVEKxdmtfjNIMUtKbCpc/ZP6T3Ex8ECxJeHhJJdUu4SOfxEb87YCM2DkyXUGLCL0wWeQDLHcawu+sj1yGYZ/+DpaCjbtKk0P2qH5WVtF5KIbgffzRpUKo96UnkgqUwvml2NYyWkVofmZ8P7qyfuYGookLTsfk1HomIXzwo9Xm0GpnjJ0+mLe3/fGA14kEfQ/jVjuRp+cIayRvOpMKDXDX7trkNaCIhSzAuWicFiJ4zJ0IQf9Xs7s2eF3Q7Ct0fE1S4WWKAaC1n6xOgznzCNbS9L/IIJz9mOBCDPiKQq49jiDrlYRp3XPb1ctEDfAWQZYRAqqLzba6SVtnaJ9BXWOGPa2DUY7vPQbUTcL3sA/4f4NZfb1+n/x9HmdZXj/RljqZ7+RZzJf2xPE/NzpHmPcmCzcRtOkeEcv0tmlJhackhRqNmhG7xq8M59yB9szbQx7M15KCGkqrRLqh5WQhOS56/sGwOhLAUuaTZDbsI3YorXcCX5it4oMC+bMRZ7tigX5WqmoDgbjdQA== X-Forefront-Antispam-Report: CIP:12.22.5.234;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:InfoNoRecords;CAT:NONE;SFS:(13230016)(4636009)(376002)(346002)(136003)(396003)(39860400002)(46966006)(40470700004)(36840700001)(26005)(8936002)(8676002)(336012)(47076005)(5660300002)(2906002)(7416002)(7406005)(70586007)(54906003)(110136005)(1076003)(478600001)(6666004)(7696005)(82310400005)(36756003)(186003)(426003)(41300700001)(2616005)(81166007)(83380400001)(86362001)(356005)(316002)(40480700001)(36860700001)(70206006)(4326008)(40460700003)(82740400003)(921005)(2101003)(36900700001)(83996005);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 23 Jul 2022 02:04:11.9069 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 5f94ad56-060f-43f9-040f-08da6c4fa321 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[12.22.5.234];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: BN8NAM11FT067.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN1PR12MB2448 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org There's only one caller that checks its return value with a WARN_ON_ONCE, while all other callers don't check the return value at all. Above that, an undo function should not fail. So, simplify the API to return void by embedding similar WARN_ONs. Also for users to pinpoint which condition fails, separate WARN_ON lines, yet remove the "driver->ops->unpin_pages" check, since it's unreasonable for callers to unpin on something totally random that wasn't even pinned. And remove NULL pointer checks for they would trigger oops vs. warnings. Note that npage is already validated in the vfio core, thus drop the same check in the type1 code. Suggested-by: Christoph Hellwig Reviewed-by: Christoph Hellwig Reviewed-by: Jason Gunthorpe Reviewed-by: Kirti Wankhede Tested-by: Terrence Xu Signed-off-by: Nicolin Chen --- .../driver-api/vfio-mediated-device.rst | 2 +- drivers/gpu/drm/i915/gvt/kvmgt.c | 5 +---- drivers/vfio/vfio.c | 21 +++++++------------ drivers/vfio/vfio.h | 2 +- drivers/vfio/vfio_iommu_type1.c | 15 ++++++------- include/linux/vfio.h | 4 ++-- 6 files changed, 18 insertions(+), 31 deletions(-) diff --git a/Documentation/driver-api/vfio-mediated-device.rst b/Documentation/driver-api/vfio-mediated-device.rst index 1c57815619fd..b0fdf76b339a 100644 --- a/Documentation/driver-api/vfio-mediated-device.rst +++ b/Documentation/driver-api/vfio-mediated-device.rst @@ -265,7 +265,7 @@ driver:: int vfio_pin_pages(struct vfio_device *device, unsigned long *user_pfn, int npage, int prot, unsigned long *phys_pfn); - int vfio_unpin_pages(struct vfio_device *device, unsigned long *user_pfn, + void vfio_unpin_pages(struct vfio_device *device, unsigned long *user_pfn, int npage); These functions call back into the back-end IOMMU module by using the pin_pages diff --git a/drivers/gpu/drm/i915/gvt/kvmgt.c b/drivers/gpu/drm/i915/gvt/kvmgt.c index ecd5bb37b63a..4d32a2748958 100644 --- a/drivers/gpu/drm/i915/gvt/kvmgt.c +++ b/drivers/gpu/drm/i915/gvt/kvmgt.c @@ -231,18 +231,15 @@ static void intel_gvt_cleanup_vgpu_type_groups(struct intel_gvt *gvt) static void gvt_unpin_guest_page(struct intel_vgpu *vgpu, unsigned long gfn, unsigned long size) { - struct drm_i915_private *i915 = vgpu->gvt->gt->i915; int total_pages; int npage; - int ret; total_pages = roundup(size, PAGE_SIZE) / PAGE_SIZE; for (npage = 0; npage < total_pages; npage++) { unsigned long cur_gfn = gfn + npage; - ret = vfio_unpin_pages(&vgpu->vfio_device, &cur_gfn, 1); - drm_WARN_ON(&i915->drm, ret != 1); + vfio_unpin_pages(&vgpu->vfio_device, &cur_gfn, 1); } } diff --git a/drivers/vfio/vfio.c b/drivers/vfio/vfio.c index b3ce8073cfb1..92b10aafae28 100644 --- a/drivers/vfio/vfio.c +++ b/drivers/vfio/vfio.c @@ -1983,31 +1983,24 @@ EXPORT_SYMBOL(vfio_pin_pages); * PFNs should not be greater than VFIO_PIN_PAGES_MAX_ENTRIES. * @npage [in] : count of elements in user_pfn array. This count should not * be greater than VFIO_PIN_PAGES_MAX_ENTRIES. - * Return error or number of pages unpinned. */ -int vfio_unpin_pages(struct vfio_device *device, unsigned long *user_pfn, - int npage) +void vfio_unpin_pages(struct vfio_device *device, unsigned long *user_pfn, + int npage) { struct vfio_container *container; struct vfio_iommu_driver *driver; - int ret; - if (!user_pfn || !npage || !vfio_assert_device_open(device)) - return -EINVAL; + if (WARN_ON(npage <= 0 || npage > VFIO_PIN_PAGES_MAX_ENTRIES)) + return; - if (npage > VFIO_PIN_PAGES_MAX_ENTRIES) - return -E2BIG; + if (WARN_ON(!vfio_assert_device_open(device))) + return; /* group->container cannot change while a vfio device is open */ container = device->group->container; driver = container->iommu_driver; - if (likely(driver && driver->ops->unpin_pages)) - ret = driver->ops->unpin_pages(container->iommu_data, user_pfn, - npage); - else - ret = -ENOTTY; - return ret; + driver->ops->unpin_pages(container->iommu_data, user_pfn, npage); } EXPORT_SYMBOL(vfio_unpin_pages); diff --git a/drivers/vfio/vfio.h b/drivers/vfio/vfio.h index 4a7db1f3c33e..6a8424b407c7 100644 --- a/drivers/vfio/vfio.h +++ b/drivers/vfio/vfio.h @@ -53,7 +53,7 @@ struct vfio_iommu_driver_ops { unsigned long *user_pfn, int npage, int prot, unsigned long *phys_pfn); - int (*unpin_pages)(void *iommu_data, + void (*unpin_pages)(void *iommu_data, unsigned long *user_pfn, int npage); void (*register_device)(void *iommu_data, struct vfio_device *vdev); diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c index 026a1d2553a2..e49fbe9968ef 100644 --- a/drivers/vfio/vfio_iommu_type1.c +++ b/drivers/vfio/vfio_iommu_type1.c @@ -949,20 +949,16 @@ static int vfio_iommu_type1_pin_pages(void *iommu_data, return ret; } -static int vfio_iommu_type1_unpin_pages(void *iommu_data, - unsigned long *user_pfn, - int npage) +static void vfio_iommu_type1_unpin_pages(void *iommu_data, + unsigned long *user_pfn, int npage) { struct vfio_iommu *iommu = iommu_data; bool do_accounting; int i; - if (!iommu || !user_pfn || npage <= 0) - return -EINVAL; - /* Supported for v2 version only */ - if (!iommu->v2) - return -EACCES; + if (WARN_ON(!iommu->v2)) + return; mutex_lock(&iommu->lock); @@ -980,7 +976,8 @@ static int vfio_iommu_type1_unpin_pages(void *iommu_data, } mutex_unlock(&iommu->lock); - return i > 0 ? i : -EINVAL; + + WARN_ON(i != npage); } static long vfio_sync_unpin(struct vfio_dma *dma, struct vfio_domain *domain, diff --git a/include/linux/vfio.h b/include/linux/vfio.h index 19cefbaa3d06..9f7d74c24925 100644 --- a/include/linux/vfio.h +++ b/include/linux/vfio.h @@ -163,8 +163,8 @@ bool vfio_file_has_dev(struct file *file, struct vfio_device *device); int vfio_pin_pages(struct vfio_device *device, unsigned long *user_pfn, int npage, int prot, unsigned long *phys_pfn); -int vfio_unpin_pages(struct vfio_device *device, unsigned long *user_pfn, - int npage); +void vfio_unpin_pages(struct vfio_device *device, unsigned long *user_pfn, + int npage); int vfio_dma_rw(struct vfio_device *device, dma_addr_t user_iova, void *data, size_t len, bool write); From patchwork Sat Jul 23 02:02:48 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 12927051 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 12393C433EF for ; Sat, 23 Jul 2022 02:04:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236232AbiGWCET (ORCPT ); Fri, 22 Jul 2022 22:04:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50512 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229572AbiGWCER (ORCPT ); Fri, 22 Jul 2022 22:04:17 -0400 Received: from NAM02-DM3-obe.outbound.protection.outlook.com (mail-dm3nam02on2061.outbound.protection.outlook.com [40.107.95.61]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 383C187369; Fri, 22 Jul 2022 19:04:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=BzL4gafJK8lIXLnkW1kJLy5ETaOkbmz4DmvUM4+3wrjwS5S8x0iuhrY3Pmq8SYvxO9XtDYIXKz+jWGTTGwd3UwjBgCSZgs0rD4+csiItqTgyL4z8WIgoJa4agwV8wzcdLzvtkIRO36xiBcQ8b7vzU6TsmeFp7crzz4dGKe4laOS9pbEtU4/hXLGPwJfMGPK1k0wfyzEyHiAFU2e0mj+GqfQ1SUMkI+SpID8cyqzfYvv5R4zav6bDou5pRjQmUyxPLOQ1Bt4wSQaAE88AMgtEP54NHBlj5UjQsjxxCNsJ7W7typlMQCC/QUoGPe+ecZGPelb+XuVqb4kNgKGp+5aPUg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=XYGLl2xarAtEzZfa2ZaHDrupakUeBSyblRP8+JbV6hM=; b=JDMBaiGE7TWjtzcRR9zj1JDslsU9qsw6HFAeK3Rp7g+ZQxhoYKe58OZxqtWjWCANdmMA3DIsCdkK6q0KNjqUqizcVkClq6VVNoWOAw1xIWfri5c+xHFKKTwYHc1NMqRn6sINWvFnN5iVLEHrc6isfsdxf8jriCOsOWJqObUglwMuu1COYL9MPCpSZqfg9OVxAL1NSA3tNC1Ba0ZizKz8EfNP/oYlJlPshW129NCPTRjkXj0Mzx89hNvqLQzBd1uN0OHQjJUzR2U7d/m8rRnaIRzDGiVeEXMWX/KujhfmQ2SPHIvBJWFfQCZzUbPtOwpe8kyRudxrq3E2NAJhpcz90A== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 12.22.5.236) smtp.rcpttodomain=linux.ibm.com smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=XYGLl2xarAtEzZfa2ZaHDrupakUeBSyblRP8+JbV6hM=; b=MqL8dKeM6SB94VmE2NmdS3g/teiRDxqJIym6YOuzYySKVQWIKDKJVWhudfeufVMrzdVd0TV3f+5HLlcL1ehBQGkiYyskLmefMfnFYIjy0bmAp0/xQ96ES5pH85ULt4ET0KzkUOfPpMGydQXSNtHQ7tqoCmtmLjELgawKKAGcjYGnKsq/scfDbHGRnrLBFfJpAct4XozBPgX3U+y72FVUYAGlVEnzddoRlLnV27cwtjZcIIKd0v1kIrb8cJFyhcGkvThg5WuoLVFuNSP3z59SwAji+TmqGYnL76S40RxlLBnJpOtiwfs6qmkv5djW/6mTThcvyIvQkrejU49LhEt2ww== Received: from DS7PR03CA0282.namprd03.prod.outlook.com (2603:10b6:5:3ad::17) by DM6PR12MB3388.namprd12.prod.outlook.com (2603:10b6:5:11b::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5438.19; Sat, 23 Jul 2022 02:04:13 +0000 Received: from DM6NAM11FT033.eop-nam11.prod.protection.outlook.com (2603:10b6:5:3ad:cafe::66) by DS7PR03CA0282.outlook.office365.com (2603:10b6:5:3ad::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5438.19 via Frontend Transport; Sat, 23 Jul 2022 02:04:13 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 12.22.5.236) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 12.22.5.236 as permitted sender) receiver=protection.outlook.com; client-ip=12.22.5.236; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (12.22.5.236) by DM6NAM11FT033.mail.protection.outlook.com (10.13.172.221) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.20.5458.17 via Frontend Transport; Sat, 23 Jul 2022 02:04:13 +0000 Received: from drhqmail202.nvidia.com (10.126.190.181) by DRHQMAIL109.nvidia.com (10.27.9.19) with Microsoft SMTP Server (TLS) id 15.0.1497.32; Sat, 23 Jul 2022 02:04:12 +0000 Received: from drhqmail203.nvidia.com (10.126.190.182) by drhqmail202.nvidia.com (10.126.190.181) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.26; Fri, 22 Jul 2022 19:04:12 -0700 Received: from Asurada-Nvidia.nvidia.com (10.127.8.10) by mail.nvidia.com (10.126.190.182) with Microsoft SMTP Server id 15.2.986.26 via Frontend Transport; Fri, 22 Jul 2022 19:04:11 -0700 From: Nicolin Chen To: , , , , , , , , , , , , , , , , , , , , , , , , , , , CC: , , , , , , , , Subject: [PATCH v4 02/10] drm/i915/gvt: Replace roundup with DIV_ROUND_UP Date: Fri, 22 Jul 2022 19:02:48 -0700 Message-ID: <20220723020256.30081-3-nicolinc@nvidia.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220723020256.30081-1-nicolinc@nvidia.com> References: <20220723020256.30081-1-nicolinc@nvidia.com> MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: cdda2033-e12f-44fa-962d-08da6c4fa3ee X-MS-TrafficTypeDiagnostic: DM6PR12MB3388:EE_ X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: qdNedtXhXkMhsZxYq11bTHzD0t/gRlMVrcsjGaBhMUUZg4ItqGC54M7GBcA2A4ieqO9cKQ9YjpFgCcwUCLShGgqZHyYdDE3F9axgkctt26cAK6HddDjLs5hF7JXkJEH5naJ4ONcvNv8BHiXbsMRlgMCchgqrKt1z9pUR8IY7QyPvTZHjTmJ0K71eXAH4SA+hvLuMjjpbMmZxo81axy5GKEA3JcE+C3XLTXkKGo+4N7gUU7ViiiAf2t3aAQC+hiaN0ZFRxzQXqF9BAJswzxW01mBtETzdT9gcLHYNlf6k0d88bKuxd9Px5HYHUrgSdObFOA/UqNWBfihGbGX6MMExd4OUG6kCYaGr8eI65P3PYO3YHObnSyeH/5qnQpPP+N+Ya+E7pHYMfAe+ZSTArwFA2BEBM8BFWL2Yg0yY1hFTeb6yK9/BHea95iIWzrEtfyVMvbzULHsFWK2emsKoUjwoqP1m6j90f4lYxTdtNz42Pm1At/4f6j6+XcvtXB75NolzxJdOonN5vlZf/nVKXnGrCr0v/HofUpYaJssH1VPMHUnN9iWafeGmk3SBp4+veTVHMiZgXGi7JJLh0VyPBTQLgYerbQGUiS+bQ1pbGCWBqCIYOoHNKGjDtoSoG6+iipZxKJaFy+yKnpsuNXJtei7PwhiePFTUR0ulZDhuqCstma1/TXoKcCH93vB10REyoWB6sIj1GuQOAD68fa5Q/FUoR8yRG8cjZ/EOiCZ8CDoaY5tQK60GrjMMYSferJpOOqzBLvgzmIwZfirus1qzrm55DQNFWWnzlKaFVXGcMze0APzEURvSDFAJ7Yl1bSEi73evWpS14YHzonoSvRU8/5IUFLmP0ppe24J+SmpXZfi6UE8uwbBwt/glAbWFxd1VZJCcSLdupT0wWc4bYtAm/HTREw== X-Forefront-Antispam-Report: CIP:12.22.5.236;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:InfoNoRecords;CAT:NONE;SFS:(13230016)(4636009)(39860400002)(136003)(346002)(396003)(376002)(46966006)(40470700004)(36840700001)(6666004)(41300700001)(47076005)(336012)(1076003)(426003)(478600001)(40480700001)(82310400005)(83380400001)(2616005)(36860700001)(7696005)(36756003)(5660300002)(8936002)(110136005)(2906002)(26005)(54906003)(316002)(186003)(356005)(81166007)(86362001)(70586007)(82740400003)(70206006)(40460700003)(921005)(7406005)(7416002)(4326008)(8676002)(36900700001)(83996005)(2101003);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 23 Jul 2022 02:04:13.3791 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: cdda2033-e12f-44fa-962d-08da6c4fa3ee X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[12.22.5.236];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: DM6NAM11FT033.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR12MB3388 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org It's a bit redundant for the maths here using roundup. Suggested-by: Jason Gunthorpe Tested-by: Terrence Xu Signed-off-by: Nicolin Chen --- drivers/gpu/drm/i915/gvt/kvmgt.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/drivers/gpu/drm/i915/gvt/kvmgt.c b/drivers/gpu/drm/i915/gvt/kvmgt.c index 4d32a2748958..2fee5695515a 100644 --- a/drivers/gpu/drm/i915/gvt/kvmgt.c +++ b/drivers/gpu/drm/i915/gvt/kvmgt.c @@ -231,11 +231,9 @@ static void intel_gvt_cleanup_vgpu_type_groups(struct intel_gvt *gvt) static void gvt_unpin_guest_page(struct intel_vgpu *vgpu, unsigned long gfn, unsigned long size) { - int total_pages; + int total_pages = DIV_ROUND_UP(size, PAGE_SIZE); int npage; - total_pages = roundup(size, PAGE_SIZE) / PAGE_SIZE; - for (npage = 0; npage < total_pages; npage++) { unsigned long cur_gfn = gfn + npage; @@ -247,12 +245,11 @@ static void gvt_unpin_guest_page(struct intel_vgpu *vgpu, unsigned long gfn, static int gvt_pin_guest_page(struct intel_vgpu *vgpu, unsigned long gfn, unsigned long size, struct page **page) { + int total_pages = DIV_ROUND_UP(size, PAGE_SIZE); unsigned long base_pfn = 0; - int total_pages; int npage; int ret; - total_pages = roundup(size, PAGE_SIZE) / PAGE_SIZE; /* * We pin the pages one-by-one to avoid allocating a big arrary * on stack to hold pfns. From patchwork Sat Jul 23 02:02:49 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 12927053 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id EB82ACCA489 for ; Sat, 23 Jul 2022 02:04:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236679AbiGWCE1 (ORCPT ); Fri, 22 Jul 2022 22:04:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50614 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235852AbiGWCEU (ORCPT ); Fri, 22 Jul 2022 22:04:20 -0400 Received: from NAM11-CO1-obe.outbound.protection.outlook.com (mail-co1nam11on2061.outbound.protection.outlook.com [40.107.220.61]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DCB19A6F90; Fri, 22 Jul 2022 19:04:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=nC0g7TsCj+jppLKRRWzHD2raGr3tRQ/F0VJZHoD2OlkFBS94WzkNvrtl3JGCVF2Gv/ykGFZzDM4Vs0hJLnv5K6H5n2yyTu0hsorq1ohnJNbQwAtL0f+EuwV2F73OImiHL7JPzgej5mTE4xTdKOKGjgUs1jvEeJrfpK1JmF/XXbE0OyoWN1TZkOgRJYDF5m3O3Vxe6OOkmd9cVhSIFHoMNZn23iyTYIIvlrjbMtmKgcmJpSkqy1zZcpHDjPn6NPyC8X/ytXgngAj7YgEYVq4ia+7wN+/ZfiLrTF6GmuoMSrSqqNUxq5KNC74Fxzp/JP5n+5QHqhjY38DN1PTpUGwtWA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=penYKVGxjl1jd5IFx4LfrtdCcwzEeylJscCPqynjoNU=; b=abFG6UjB/+OUGHipUep/0n5hMWQdJrAigqCE+9xMXuT0h8mskhyEBYQ/vnZ83Rjus/923lGjxjNnNGf4v1zjHzJ08+M2Bch5SydrHPe5+IOLiAZrGZmGwA2zZs6Gt1ilsQklmeFxA7tcjzj+UOugvmv4OLkyJve3i9NPGc+0CWKWXDwffFScr5znR3veXURLgM3dmwQxOV+uI5Z2EY3R5VXYYqFYyKc3Ew8nCG5IUVNrPeH/OYGKWlHu2+RB2CKQVT4FhPfIij5FlFilkZut9aupdQe+F94ndX5ubFBc3qVK3aGN7O3ur0sTIWcWTd+U/pLFRaZKnSOBGNKJykeROg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 12.22.5.234) smtp.rcpttodomain=linux.ibm.com smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=penYKVGxjl1jd5IFx4LfrtdCcwzEeylJscCPqynjoNU=; b=VhFQl85CTUj3ZVSsE0tq5tsU2x49B41EQUHu/LJiipI2pUyA55O2/P9XEzzBms/Tl6zYrXvrYfuqnj4K6BT2qoCqSofQaa3/UvE6oZQbIIdMKxGHEeIJ40P+vQ+b6szUtdXqi6fW245EAw+mNs7cJyNFZcH3h4HcwrD96X1JHugk56a4eSpESZVP1H/IfWxGPUfY9t89OuvmJ7y858qFWDfM9DHoPLF87FBgz8zhTd2qPJyd6LGWu3MnrDF85o7gnzSsaKtusSO+sskimRk6Rt7MrobQVC//s+pDOh07eM8MNpV38diBRVraCf3dzl/THhii30O+aZfxlgT5d9E12w== Received: from BN8PR07CA0030.namprd07.prod.outlook.com (2603:10b6:408:ac::43) by PH7PR12MB6786.namprd12.prod.outlook.com (2603:10b6:510:1ac::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5458.19; Sat, 23 Jul 2022 02:04:16 +0000 Received: from BN8NAM11FT018.eop-nam11.prod.protection.outlook.com (2603:10b6:408:ac:cafe::35) by BN8PR07CA0030.outlook.office365.com (2603:10b6:408:ac::43) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5458.19 via Frontend Transport; Sat, 23 Jul 2022 02:04:15 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 12.22.5.234) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 12.22.5.234 as permitted sender) receiver=protection.outlook.com; client-ip=12.22.5.234; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (12.22.5.234) by BN8NAM11FT018.mail.protection.outlook.com (10.13.176.89) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.20.5458.17 via Frontend Transport; Sat, 23 Jul 2022 02:04:15 +0000 Received: from drhqmail201.nvidia.com (10.126.190.180) by DRHQMAIL101.nvidia.com (10.27.9.10) with Microsoft SMTP Server (TLS) id 15.0.1497.32; Sat, 23 Jul 2022 02:04:14 +0000 Received: from drhqmail203.nvidia.com (10.126.190.182) by drhqmail201.nvidia.com (10.126.190.180) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.26; Fri, 22 Jul 2022 19:04:14 -0700 Received: from Asurada-Nvidia.nvidia.com (10.127.8.10) by mail.nvidia.com (10.126.190.182) with Microsoft SMTP Server id 15.2.986.26 via Frontend Transport; Fri, 22 Jul 2022 19:04:12 -0700 From: Nicolin Chen To: , , , , , , , , , , , , , , , , , , , , , , , , , , , CC: , , , , , , , , Subject: [PATCH v4 03/10] vfio/ap: Pass in physical address of ind to ap_aqic() Date: Fri, 22 Jul 2022 19:02:49 -0700 Message-ID: <20220723020256.30081-4-nicolinc@nvidia.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220723020256.30081-1-nicolinc@nvidia.com> References: <20220723020256.30081-1-nicolinc@nvidia.com> MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 53928bd3-d69e-4944-809f-08da6c4fa529 X-MS-TrafficTypeDiagnostic: PH7PR12MB6786:EE_ X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: RkaBhx0YduWx2+qWH3bL1xwDSLzlRAeLKIGavvpjJWoW90V0zieCrJh3EgqpaM+18BmPhuAfJpIAvV8oFdLXqLbSTp/dd52oDaMTLfaplFTV6zsLy3rQKAsgLPmZUqwciDAhf7J6abSTTvKmUHM4DOUy6nlSrAMxCBtYMyqLEapqj6AO/lMJQDO8zQTKDS+UamXy/hyH1R6vdfI/m/55a2L4auqzSNrHgZCmWTTaMMu22ZpUDdKz9fW9L/q7dEcpXA0VugFw2uAAIPNVNeMM+ngXtYmpsTZMd5eJo2f+qAeQ5hCK6DHxKVrfz43QleVmi+mYMtbToIepkZCqVfNVXvvdNajDT6Pdu54sHDIWTLVyEzl+7axCct59dK3vTEA1pj8ej0lakcd8zGFv2gxj+R9c+irkqxwPXWhsy53wZdBGk4/jjYSS2NJOj1s2Tj6PAidgP3/uWGcvfFyFkjo2BB1/xfVjUSI3DleYz3uOBp6xtpUUzxxkmaHoVysyNKEu1Xr765UcClGOxvpKU+Hnf5Xb5NklyqU7niQpd+lM2OxLCqULvgbZjpzfXNmE/hewPyr2v/TaaRN4prd8MhNcBRken+fZkWZp3/RnqTZwfHxQSGYqJ1vWIJdS+xD3wZMfjvH/0w9v74yWeRSuNC2eM+2IZb9UKxXfeq0SndDbKlMN4+C1stYZZL2ZZqg6X3dNmqZZruTdRg4Xs64ll5MuFl3H8eufpkWzWr3d2sBAXqxGkXie4jEKxF1SNVg5F52sLsTxr6UsWaXu4hgEJFKiKcHoTmng5zDjc0cbqJwGygKhKoX1SbcV2CvjQbi9eBP5pZgpBaWEdM8E5h6K+I+kAmZe3iWtHx2nTdn1AtCScxShf0PZ8/k8FUO0hQYfKgeVrr2R30fDyGuNKZrWc5C3cg== X-Forefront-Antispam-Report: CIP:12.22.5.234;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:InfoNoRecords;CAT:NONE;SFS:(13230016)(4636009)(346002)(376002)(39860400002)(136003)(396003)(46966006)(40470700004)(36840700001)(40480700001)(316002)(36860700001)(356005)(81166007)(86362001)(921005)(70206006)(82740400003)(70586007)(7406005)(4326008)(8676002)(82310400005)(7416002)(478600001)(40460700003)(5660300002)(54906003)(110136005)(8936002)(2616005)(1076003)(186003)(83380400001)(6666004)(47076005)(426003)(336012)(41300700001)(26005)(7696005)(36756003)(2906002)(83996005)(2101003)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 23 Jul 2022 02:04:15.3804 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 53928bd3-d69e-4944-809f-08da6c4fa529 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[12.22.5.234];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: BN8NAM11FT018.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR12MB6786 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org The ap_aqic() is called by vfio_ap_irq_enable() where it passes in a virt value that's casted from a physical address "h_nib". Inside the ap_aqic(), it does virt_to_phys() again. Since ap_aqic() needs a physical address, let's just pass in a pa of ind directly. So change the "ind" to "pa_ind". Reviewed-by: Harald Freudenberger Reviewed-by: Jason Gunthorpe Tested-by: Eric Farman Signed-off-by: Nicolin Chen --- arch/s390/include/asm/ap.h | 6 +++--- drivers/s390/crypto/ap_queue.c | 2 +- drivers/s390/crypto/vfio_ap_ops.c | 7 ++++--- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/arch/s390/include/asm/ap.h b/arch/s390/include/asm/ap.h index b515cfa62bd9..f508f5025e38 100644 --- a/arch/s390/include/asm/ap.h +++ b/arch/s390/include/asm/ap.h @@ -227,13 +227,13 @@ struct ap_qirq_ctrl { * ap_aqic(): Control interruption for a specific AP. * @qid: The AP queue number * @qirqctrl: struct ap_qirq_ctrl (64 bit value) - * @ind: The notification indicator byte + * @pa_ind: Physical address of the notification indicator byte * * Returns AP queue status. */ static inline struct ap_queue_status ap_aqic(ap_qid_t qid, struct ap_qirq_ctrl qirqctrl, - void *ind) + phys_addr_t pa_ind) { unsigned long reg0 = qid | (3UL << 24); /* fc 3UL is AQIC */ union { @@ -241,7 +241,7 @@ static inline struct ap_queue_status ap_aqic(ap_qid_t qid, struct ap_qirq_ctrl qirqctrl; struct ap_queue_status status; } reg1; - unsigned long reg2 = virt_to_phys(ind); + unsigned long reg2 = pa_ind; reg1.qirqctrl = qirqctrl; diff --git a/drivers/s390/crypto/ap_queue.c b/drivers/s390/crypto/ap_queue.c index c48b0db824e3..a32457b4cbb8 100644 --- a/drivers/s390/crypto/ap_queue.c +++ b/drivers/s390/crypto/ap_queue.c @@ -34,7 +34,7 @@ static int ap_queue_enable_irq(struct ap_queue *aq, void *ind) qirqctrl.ir = 1; qirqctrl.isc = AP_ISC; - status = ap_aqic(aq->qid, qirqctrl, ind); + status = ap_aqic(aq->qid, qirqctrl, virt_to_phys(ind)); switch (status.response_code) { case AP_RESPONSE_NORMAL: case AP_RESPONSE_OTHERWISE_CHANGED: diff --git a/drivers/s390/crypto/vfio_ap_ops.c b/drivers/s390/crypto/vfio_ap_ops.c index bb1a1677c5c2..5781059d3ed2 100644 --- a/drivers/s390/crypto/vfio_ap_ops.c +++ b/drivers/s390/crypto/vfio_ap_ops.c @@ -154,7 +154,7 @@ static struct ap_queue_status vfio_ap_irq_disable(struct vfio_ap_queue *q) int retries = 5; do { - status = ap_aqic(q->apqn, aqic_gisa, NULL); + status = ap_aqic(q->apqn, aqic_gisa, 0); switch (status.response_code) { case AP_RESPONSE_OTHERWISE_CHANGED: case AP_RESPONSE_NORMAL: @@ -245,7 +245,8 @@ static struct ap_queue_status vfio_ap_irq_enable(struct vfio_ap_queue *q, struct kvm_s390_gisa *gisa; int nisc; struct kvm *kvm; - unsigned long h_nib, g_pfn, h_pfn; + unsigned long g_pfn, h_pfn; + phys_addr_t h_nib; int ret; /* Verify that the notification indicator byte address is valid */ @@ -290,7 +291,7 @@ static struct ap_queue_status vfio_ap_irq_enable(struct vfio_ap_queue *q, aqic_gisa.ir = 1; aqic_gisa.gisa = (uint64_t)gisa >> 4; - status = ap_aqic(q->apqn, aqic_gisa, (void *)h_nib); + status = ap_aqic(q->apqn, aqic_gisa, h_nib); switch (status.response_code) { case AP_RESPONSE_NORMAL: /* See if we did clear older IRQ configuration */ From patchwork Sat Jul 23 02:02:50 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 12927054 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1F898CCA489 for ; Sat, 23 Jul 2022 02:04:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236864AbiGWCEg (ORCPT ); Fri, 22 Jul 2022 22:04:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50790 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236580AbiGWCEZ (ORCPT ); Fri, 22 Jul 2022 22:04:25 -0400 Received: from NAM11-CO1-obe.outbound.protection.outlook.com (mail-co1nam11on2078.outbound.protection.outlook.com [40.107.220.78]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 42BC8A9BB5; Fri, 22 Jul 2022 19:04:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=WxTA3lDEEmv51Rv7jogs8CbSgqV8eYQc97Zmm4GExfbxHBQ0yEtFVz2sSX4+/7AEtWU2BEsTvByYakmgvmEYvVxOIXVkObnIOWnSSENK91WzJLTxbnE0HGCvrteSRRDosf7OVDFS84UzPs2xRUUTS/khvsc/jyDmqk2F+XxRROkioQ0A1jgDOMU5tpPw5zYeYNtEe1CdMYN47/8jDjrmX8w694UrZFGkLyEbW0EZ1BxP73TlZGz2A3EIxETln5MnxJYF6+WW+fLZIMQYMvdJhquK5Sw3GeQxWGZHePLDd2unTYSUx5LFnPkuULiCR/jvobFOrVaIFGAH2WXRdmsTQA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=jSbr0ptHu8jicIoCQSf/c9ikdG/DcMNO02c6EI8L3Yw=; b=TRJSvNh6/3fJIrHq/jDU5Dq8KBzYfcN9Uu6U89swsjRlUaHdC7Knwtc9uhZvprSNlj298GoR3o1jxak4GRpXQwVFk4NsIFORZFCFh7mb8Xh1fR04HzJB03nNkoDb72QZpEESefHvfECJZTEDQr7pZA/pZPfOvn1DfbyVxhrVuVV+23R6V79qzoHrFS2uryOqvPsdZxj0AUMbSf/QK5KVUS9T/TnqgEr5Nou+eGutzYL+8Qg4BZvL3NecPMu3FCtbJ5C1R/+6e6JIRbGxH9dElC2E2+cV0LZN+2W0Vtt7ytj5ste/Xkd7CvFV6pL3nZ4Pz6ATFhy6uu1m3jb1uERPZQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 12.22.5.236) smtp.rcpttodomain=linux.ibm.com smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=jSbr0ptHu8jicIoCQSf/c9ikdG/DcMNO02c6EI8L3Yw=; b=G98MiUGfKNMQ0WSHxPJe5pLuNhK3uT02OIL26zqyPPs0MVxpzusOe187pT4fc2J1LwEp0zUJtF2ScQqGcjm084Epa18Z8QAF0uokk4sSfDYG7TzSmoxC+RaA9finftuu0nPx28Pj0CTm+T3bBW/JdzO+fZe3wMVQNWsy+fTPaUH4u4Vf68GyTobw1vxHTTOI5TsGl8lzAb8iZ1KEmXlNYpuKUfQKttuYvoIPSgRFpaAM1H98It7FO3yrxxTpwmfMJ/piaMCB6+evc+mBb1uZ1zVYmZUoyNOr4AYik62IKCxuCdAo9fQghXxgc3nwhLih+VOKVkwLqlvSJqFQHJ870A== Received: from DS7PR03CA0157.namprd03.prod.outlook.com (2603:10b6:5:3b2::12) by MW2PR12MB2409.namprd12.prod.outlook.com (2603:10b6:907:9::26) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5458.18; Sat, 23 Jul 2022 02:04:17 +0000 Received: from DM6NAM11FT016.eop-nam11.prod.protection.outlook.com (2603:10b6:5:3b2:cafe::e5) by DS7PR03CA0157.outlook.office365.com (2603:10b6:5:3b2::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5438.17 via Frontend Transport; Sat, 23 Jul 2022 02:04:17 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 12.22.5.236) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 12.22.5.236 as permitted sender) receiver=protection.outlook.com; client-ip=12.22.5.236; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (12.22.5.236) by DM6NAM11FT016.mail.protection.outlook.com (10.13.173.139) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.20.5458.17 via Frontend Transport; Sat, 23 Jul 2022 02:04:17 +0000 Received: from drhqmail201.nvidia.com (10.126.190.180) by DRHQMAIL109.nvidia.com (10.27.9.19) with Microsoft SMTP Server (TLS) id 15.0.1497.32; Sat, 23 Jul 2022 02:04:16 +0000 Received: from drhqmail203.nvidia.com (10.126.190.182) by drhqmail201.nvidia.com (10.126.190.180) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.26; Fri, 22 Jul 2022 19:04:15 -0700 Received: from Asurada-Nvidia.nvidia.com (10.127.8.10) by mail.nvidia.com (10.126.190.182) with Microsoft SMTP Server id 15.2.986.26 via Frontend Transport; Fri, 22 Jul 2022 19:04:14 -0700 From: Nicolin Chen To: , , , , , , , , , , , , , , , , , , , , , , , , , , , CC: , , , , , , , , Subject: [PATCH v4 04/10] vfio/ccw: Only pass in contiguous pages Date: Fri, 22 Jul 2022 19:02:50 -0700 Message-ID: <20220723020256.30081-5-nicolinc@nvidia.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220723020256.30081-1-nicolinc@nvidia.com> References: <20220723020256.30081-1-nicolinc@nvidia.com> MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: f6b1109a-5add-4a94-dccb-08da6c4fa61d X-MS-TrafficTypeDiagnostic: MW2PR12MB2409:EE_ X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 8tunMkP7s1o8GRv32MIO4hq5Rdijc2CsTBwKpeftbgHr0lPwbvURij2Eqp7uL1qZ/ff0T+B+6MEskHfNZgzKVZrrD03jgdF10ldzE39gL2DBnfEc5MN3pqSrLBgPUoujQ3VaOoOafAS+3k4NClqr7MsLejhGoixC25T3qy8rOn4+27sA75fKOPSZwaBBSW7HeSDDDLrcz5sjZLp8yvMsZcqF4OnBQvpf5N7iatc37I86Ef7954BYC8s7RmFmhOvpuBLb6qQ5mVoH2ZjRWrHyR5/qlGK+wtwu/OEAA4SKnVYqQ6SAdpl1uWsiO1nuIQHcq8ymKULpXNAg95GW0SCWZbNuvfNncM3DOEjfEsdi6zV7vtNweKGsYx6Dq5OA2KxQb7Q8hm0KicemOP1h1QW+6SFonvfRIfFv9hq5WklCyC321AxgcyeHuRP5//NZqFNvGDtFAlEycfGD6bsNNCiUUwc2dTy7S53yYCpfJt0PjuzMQkqmfn+QJ4Wao6asRx7kcYmc2ye2Fvs8R+GdWooVN+EIhMRgH7ceh6s9tmr7Lkz+idZeLXhunvFLVQARTjAHbNkCgckX0Rwuft2nc4iW+vOSE8nFT4BS3hIFyMwf1wF+uwBMjNRD5j9vgM2QBTfzhu1gRZ4+7SOoZNOIHDztVTlbzI3yVYIDbiHbkAHcVvbYwiZvUtkq9Z+QkYoFdUg60BhBwRiL6pNaZjSc8ugubbXFVWVmPsjSqf16t3d0a+J7lIsQqP/sHaQzQowgjddsOtjtxkuMTXjVaMcmHjudwuFAbe6i/PmoWASotK+/tP7BFJ8SNbgRwXak7SPqtpzBSFtvK6X9C9MsMYR9lSW6AS2eX3PPuajororHJ6/LjXG/eaHahzCn478ItHxdfcpWlSYvjCNWCOEQPeAvQRH6cw== X-Forefront-Antispam-Report: CIP:12.22.5.236;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:InfoNoRecords;CAT:NONE;SFS:(13230016)(4636009)(39860400002)(136003)(346002)(376002)(396003)(40470700004)(36840700001)(46966006)(86362001)(41300700001)(110136005)(921005)(356005)(82310400005)(36756003)(2616005)(81166007)(83380400001)(1076003)(186003)(5660300002)(426003)(47076005)(336012)(6666004)(7406005)(8676002)(2906002)(26005)(316002)(8936002)(70586007)(40480700001)(478600001)(54906003)(7416002)(4326008)(82740400003)(36860700001)(40460700003)(70206006)(7696005)(83996005)(2101003)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 23 Jul 2022 02:04:17.0287 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: f6b1109a-5add-4a94-dccb-08da6c4fa61d X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[12.22.5.236];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: DM6NAM11FT016.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW2PR12MB2409 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org This driver is the only caller of vfio_pin/unpin_pages that might pass in a non-contiguous PFN list, but in many cases it has a contiguous PFN list to process. So letting VFIO API handle a non-contiguous PFN list is actually counterproductive. Add a pair of simple loops to pass in contiguous PFNs only, to have an efficient implementation in VFIO. Reviewed-by: Jason Gunthorpe Reviewed-by: Eric Farman Tested-by: Eric Farman Signed-off-by: Nicolin Chen --- drivers/s390/cio/vfio_ccw_cp.c | 70 +++++++++++++++++++++++++++------- 1 file changed, 56 insertions(+), 14 deletions(-) diff --git a/drivers/s390/cio/vfio_ccw_cp.c b/drivers/s390/cio/vfio_ccw_cp.c index 0c2be9421ab7..3b94863ad24e 100644 --- a/drivers/s390/cio/vfio_ccw_cp.c +++ b/drivers/s390/cio/vfio_ccw_cp.c @@ -90,6 +90,38 @@ static int pfn_array_alloc(struct pfn_array *pa, u64 iova, unsigned int len) return 0; } +/* + * pfn_array_unpin() - Unpin user pages in memory + * @pa: pfn_array on which to perform the operation + * @vdev: the vfio device to perform the operation + * @pa_nr: number of user pages to unpin + * + * Only unpin if any pages were pinned to begin with, i.e. pa_nr > 0, + * otherwise only clear pa->pa_nr + */ +static void pfn_array_unpin(struct pfn_array *pa, + struct vfio_device *vdev, int pa_nr) +{ + int unpinned = 0, npage = 1; + + while (unpinned < pa_nr) { + unsigned long *first = &pa->pa_iova_pfn[unpinned]; + unsigned long *last = &first[npage]; + + if (unpinned + npage < pa_nr && + *first + npage == *last) { + npage++; + continue; + } + + vfio_unpin_pages(vdev, first, npage); + unpinned += npage; + npage = 1; + } + + pa->pa_nr = 0; +} + /* * pfn_array_pin() - Pin user pages in memory * @pa: pfn_array on which to perform the operation @@ -101,34 +133,44 @@ static int pfn_array_alloc(struct pfn_array *pa, u64 iova, unsigned int len) */ static int pfn_array_pin(struct pfn_array *pa, struct vfio_device *vdev) { + int pinned = 0, npage = 1; int ret = 0; - ret = vfio_pin_pages(vdev, pa->pa_iova_pfn, pa->pa_nr, - IOMMU_READ | IOMMU_WRITE, pa->pa_pfn); + while (pinned < pa->pa_nr) { + unsigned long *first = &pa->pa_iova_pfn[pinned]; + unsigned long *last = &first[npage]; - if (ret < 0) { - goto err_out; - } else if (ret > 0 && ret != pa->pa_nr) { - vfio_unpin_pages(vdev, pa->pa_iova_pfn, ret); - ret = -EINVAL; - goto err_out; + if (pinned + npage < pa->pa_nr && + *first + npage == *last) { + npage++; + continue; + } + + ret = vfio_pin_pages(vdev, first, npage, + IOMMU_READ | IOMMU_WRITE, + &pa->pa_pfn[pinned]); + if (ret < 0) { + goto err_out; + } else if (ret > 0 && ret != npage) { + pinned += ret; + ret = -EINVAL; + goto err_out; + } + pinned += npage; + npage = 1; } return ret; err_out: - pa->pa_nr = 0; - + pfn_array_unpin(pa, vdev, pinned); return ret; } /* Unpin the pages before releasing the memory. */ static void pfn_array_unpin_free(struct pfn_array *pa, struct vfio_device *vdev) { - /* Only unpin if any pages were pinned to begin with */ - if (pa->pa_nr) - vfio_unpin_pages(vdev, pa->pa_iova_pfn, pa->pa_nr); - pa->pa_nr = 0; + pfn_array_unpin(pa, vdev, pa->pa_nr); kfree(pa->pa_iova_pfn); } From patchwork Sat Jul 23 02:02:51 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 12927055 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6372CC433EF for ; Sat, 23 Jul 2022 02:04:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236961AbiGWCEi (ORCPT ); Fri, 22 Jul 2022 22:04:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50794 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236596AbiGWCEZ (ORCPT ); Fri, 22 Jul 2022 22:04:25 -0400 Received: from NAM04-DM6-obe.outbound.protection.outlook.com (mail-dm6nam04on2065.outbound.protection.outlook.com [40.107.102.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 61F73AF96F; Fri, 22 Jul 2022 19:04:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=QdAdPL4hi3jcHgTcYjA37kh8AbNrq8kZE0CopPb7AUiKcNDHecokqQTv7Y54o3uyCRz38IsKj7vBILvW0tcoN02cy6IwiPyDB8+AhI81Zq9mdfbaoDfCML9fnfntodspcmSkpSq9ZEd36vqg8O95uDbUgy5/dGdXORe0Ho8B+6BCKFUfUTlk9SYCqjd/BvNwBdAMRzpkgWmukwLLo/n3ub8cEv7b1ia+xsqUCDW9HeFPJRL/j1VLlCWUlxjRQTDviQcOgX0nVEIuZLWXLhRNhNt6Py1JftkF6XdebsROkR6JI2qVC6Y1QOxWJAMWjnELFC5vzxycV3MhCoRHO2tPOw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=IkZUlYCR++ge2kXJMMvJl0UA/sy1ms1bTpAalCz/4yE=; b=J88T2wS/u0gffc0SPRfr55HvWdMyjiN6i78u0MEceSS3atOHbbJf1hdzhMbxCEaa0r5Dnx9RfhSX+YnFCmNj72KNjJtG+k9cehoi8c6PvSDhx5OsIM4q3xgzcenRjiaeDfFOnyOcqJpK7wQe8D76MTis+VLUdyuUYAjxqW5etE8Kv5d3omj/JpeWYnnTIcZwMG8PIG0C7aBHfOxlMzLfiRx0jB1EM/eRSp7qBjKeZwrppONvsp/O7eD655NNUAW7WQ0qxlceGkGwXsg/Y7bhl9PHsCP+99zhuxlcaxDJjksfe1yt6mXhNZ7su0YnMakkH+eRHBVwFkMKZwtXbLXDVQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 12.22.5.235) smtp.rcpttodomain=linux.ibm.com smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=IkZUlYCR++ge2kXJMMvJl0UA/sy1ms1bTpAalCz/4yE=; b=g06EbZAByiQMHNo2BPEq/+nvTl+fVXeUj7fYoEmLGsS9AEi6MZARUhZpZonUvdXTw6Qz3WA22hbtdGR4NKnJkfjQlwXQ+6Nc584TnGXoQk2gPIqP8Mqu+lc6Y+4ZNIr4KcOmZoutC3NhjluqImAP2Ebl7JhYCqheoXgRmFrZj6WDbWgnctsqciYKk/VmgEhrCYsyWRh5quJBfhxkNsr5I0JE0eOnFxijngpgEFMnCf47qVe4WmBi3rFDMQkvYrFVqkCWfFzVza8ZnzmYt3qcL96RbpWfvI2PBkLkp8orQVDGPRSq6ZAFNShh2ki0biokt13TdVlDgzPHSJPCWFNN5w== Received: from BN0PR04CA0143.namprd04.prod.outlook.com (2603:10b6:408:ed::28) by DM5PR1201MB0009.namprd12.prod.outlook.com (2603:10b6:3:de::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5438.20; Sat, 23 Jul 2022 02:04:19 +0000 Received: from BN8NAM11FT042.eop-nam11.prod.protection.outlook.com (2603:10b6:408:ed:cafe::90) by BN0PR04CA0143.outlook.office365.com (2603:10b6:408:ed::28) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5438.20 via Frontend Transport; Sat, 23 Jul 2022 02:04:19 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 12.22.5.235) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 12.22.5.235 as permitted sender) receiver=protection.outlook.com; client-ip=12.22.5.235; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (12.22.5.235) by BN8NAM11FT042.mail.protection.outlook.com (10.13.177.85) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.20.5458.17 via Frontend Transport; Sat, 23 Jul 2022 02:04:19 +0000 Received: from drhqmail201.nvidia.com (10.126.190.180) by DRHQMAIL107.nvidia.com (10.27.9.16) with Microsoft SMTP Server (TLS) id 15.0.1497.32; Sat, 23 Jul 2022 02:04:18 +0000 Received: from drhqmail203.nvidia.com (10.126.190.182) by drhqmail201.nvidia.com (10.126.190.180) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.26; Fri, 22 Jul 2022 19:04:17 -0700 Received: from Asurada-Nvidia.nvidia.com (10.127.8.10) by mail.nvidia.com (10.126.190.182) with Microsoft SMTP Server id 15.2.986.26 via Frontend Transport; Fri, 22 Jul 2022 19:04:16 -0700 From: Nicolin Chen To: , , , , , , , , , , , , , , , , , , , , , , , , , , , CC: , , , , , , , , Subject: [PATCH v4 05/10] vfio: Pass in starting IOVA to vfio_pin/unpin_pages API Date: Fri, 22 Jul 2022 19:02:51 -0700 Message-ID: <20220723020256.30081-6-nicolinc@nvidia.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220723020256.30081-1-nicolinc@nvidia.com> References: <20220723020256.30081-1-nicolinc@nvidia.com> MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 3f07d4c0-039d-4c95-0be2-08da6c4fa766 X-MS-TrafficTypeDiagnostic: DM5PR1201MB0009:EE_ X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: UV3kHfyDETbmbuybkLsksliQ0dsIYSFmNiy3tHW6pDT+SN1haoYES/kEyvLqzmN1bRmDmMp61e5Yts/R+Mzo4ihcDck2QI3cfTI2Wrom9dS/hoPJM0wYUtdyfZni3oTucUKuIFRAqmAF0l0XNVd5aSYe9ZuXsDWz6C6DFcvhjyGjHmH12Le6o9wxQ4qIwNk4pQ17JD0EcaqLwOCdp7+LSVQewhA0ciXNHeEXcPDx85KBBdUaYx74UUW8CHWhpqykIoxoOQhSdS8sYZ42MmEtgb2g7ReG+758msJMDLnIGYQvChG3yyCQCYSeArAwsZS7S9iaFmygNslFR1f73AMT1r+FWRZ6BTtrcW6bFZlFVAWreQ16Ls1gZpAq1hcIzONd1TJPzMur8dhMyDLazcyrmvhMezA2W7ew6LQ3pwE3xlwaf5uYA7wUoSJGHuhxTEA1dK8mxG8qhg20ptL6wnMdd0ntI1yhtXm8vJHglLD9gTonbw2/q6NDUXafcfVHldm2xRHTq8w6cyQagfw9pWdj2DTEyq54c6ZDLlGoRpGDRavo0q4icnrv5zf94uKmNl4rcnec/+IZkoegSgBMZOnKqi5ut8wdaV2jXIev59Rfw29E0Qe7rosOtWA4h4Y2vqCgiwTYFJ3PLOQUiGKzexWv6qEX3Nqr9hoWTE2tjdKnidrNaKAWAFFhfyff4d0zQevakkpwnOt664I5qRuf21Lzr09VxMzTjxvVG2bydGF/wGYb20ZWqHhdHfp0WGP3EXvbzVbnEMu+ra+xsIIUgUgv4QP9VQx149B460MPB8pLLt3tDVLL9tXuG5w5Rly+ugvIFisjn6iuUtUzActbNv9mjUbEdGPv1DJGHqhQq/K1DiYQtwP9LGT78s02h/AD9Tcfrgtbex0V+8dn9B/4DBZT6Q== X-Forefront-Antispam-Report: CIP:12.22.5.235;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:InfoNoRecords;CAT:NONE;SFS:(13230016)(4636009)(39860400002)(136003)(346002)(396003)(376002)(46966006)(40470700004)(36840700001)(6666004)(41300700001)(47076005)(336012)(1076003)(426003)(478600001)(40480700001)(82310400005)(83380400001)(2616005)(36860700001)(7696005)(36756003)(5660300002)(8936002)(110136005)(2906002)(26005)(30864003)(54906003)(316002)(186003)(356005)(81166007)(86362001)(70586007)(82740400003)(70206006)(40460700003)(921005)(7406005)(7416002)(4326008)(8676002)(36900700001)(83996005)(2101003);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 23 Jul 2022 02:04:19.0701 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 3f07d4c0-039d-4c95-0be2-08da6c4fa766 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[12.22.5.235];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: BN8NAM11FT042.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM5PR1201MB0009 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org The vfio_pin/unpin_pages() so far accepted arrays of PFNs of user IOVA. Among all three callers, there was only one caller possibly passing in a non-contiguous PFN list, which is now ensured to have contiguous PFN inputs too. Pass in the starting address with "iova" alone to simplify things, so callers no longer need to maintain a PFN list or to pin/unpin one page at a time. This also allows VFIO to use more efficient implementations of pin/unpin_pages. For now, also update vfio_iommu_type1 to fit this new parameter too, while keeping its input intact (being user_iova) since we don't want to spend too much effort swapping its parameters and local variables at that level. Reviewed-by: Christoph Hellwig Reviewed by: Kirti Wankhede Reviewed-by: Jason Gunthorpe Reviewed-by: Kevin Tian Reviewed-by: Tony Krowiak Acked-by: Eric Farman Tested-by: Terrence Xu Tested-by: Eric Farman Signed-off-by: Nicolin Chen --- .../driver-api/vfio-mediated-device.rst | 4 +-- drivers/gpu/drm/i915/gvt/kvmgt.c | 18 +++++-------- drivers/s390/cio/vfio_ccw_cp.c | 4 +-- drivers/s390/crypto/vfio_ap_ops.c | 9 +++---- drivers/vfio/vfio.c | 27 +++++++++---------- drivers/vfio/vfio.h | 4 +-- drivers/vfio/vfio_iommu_type1.c | 15 +++++------ include/linux/vfio.h | 5 ++-- 8 files changed, 37 insertions(+), 49 deletions(-) diff --git a/Documentation/driver-api/vfio-mediated-device.rst b/Documentation/driver-api/vfio-mediated-device.rst index b0fdf76b339a..ea32a0f13ddb 100644 --- a/Documentation/driver-api/vfio-mediated-device.rst +++ b/Documentation/driver-api/vfio-mediated-device.rst @@ -262,10 +262,10 @@ Translation APIs for Mediated Devices The following APIs are provided for translating user pfn to host pfn in a VFIO driver:: - int vfio_pin_pages(struct vfio_device *device, unsigned long *user_pfn, + int vfio_pin_pages(struct vfio_device *device, dma_addr_t iova, int npage, int prot, unsigned long *phys_pfn); - void vfio_unpin_pages(struct vfio_device *device, unsigned long *user_pfn, + void vfio_unpin_pages(struct vfio_device *device, dma_addr_t iova, int npage); These functions call back into the back-end IOMMU module by using the pin_pages diff --git a/drivers/gpu/drm/i915/gvt/kvmgt.c b/drivers/gpu/drm/i915/gvt/kvmgt.c index 2fee5695515a..8be75c282611 100644 --- a/drivers/gpu/drm/i915/gvt/kvmgt.c +++ b/drivers/gpu/drm/i915/gvt/kvmgt.c @@ -231,14 +231,8 @@ static void intel_gvt_cleanup_vgpu_type_groups(struct intel_gvt *gvt) static void gvt_unpin_guest_page(struct intel_vgpu *vgpu, unsigned long gfn, unsigned long size) { - int total_pages = DIV_ROUND_UP(size, PAGE_SIZE); - int npage; - - for (npage = 0; npage < total_pages; npage++) { - unsigned long cur_gfn = gfn + npage; - - vfio_unpin_pages(&vgpu->vfio_device, &cur_gfn, 1); - } + vfio_unpin_pages(&vgpu->vfio_device, gfn << PAGE_SHIFT, + DIV_ROUND_UP(size, PAGE_SIZE)); } /* Pin a normal or compound guest page for dma. */ @@ -255,14 +249,14 @@ static int gvt_pin_guest_page(struct intel_vgpu *vgpu, unsigned long gfn, * on stack to hold pfns. */ for (npage = 0; npage < total_pages; npage++) { - unsigned long cur_gfn = gfn + npage; + dma_addr_t cur_iova = (gfn + npage) << PAGE_SHIFT; unsigned long pfn; - ret = vfio_pin_pages(&vgpu->vfio_device, &cur_gfn, 1, + ret = vfio_pin_pages(&vgpu->vfio_device, cur_iova, 1, IOMMU_READ | IOMMU_WRITE, &pfn); if (ret != 1) { - gvt_vgpu_err("vfio_pin_pages failed for gfn 0x%lx, ret %d\n", - cur_gfn, ret); + gvt_vgpu_err("vfio_pin_pages failed for iova %pad, ret %d\n", + &cur_iova, ret); goto err; } diff --git a/drivers/s390/cio/vfio_ccw_cp.c b/drivers/s390/cio/vfio_ccw_cp.c index 3b94863ad24e..a739262f988d 100644 --- a/drivers/s390/cio/vfio_ccw_cp.c +++ b/drivers/s390/cio/vfio_ccw_cp.c @@ -114,7 +114,7 @@ static void pfn_array_unpin(struct pfn_array *pa, continue; } - vfio_unpin_pages(vdev, first, npage); + vfio_unpin_pages(vdev, *first << PAGE_SHIFT, npage); unpinned += npage; npage = 1; } @@ -146,7 +146,7 @@ static int pfn_array_pin(struct pfn_array *pa, struct vfio_device *vdev) continue; } - ret = vfio_pin_pages(vdev, first, npage, + ret = vfio_pin_pages(vdev, *first << PAGE_SHIFT, npage, IOMMU_READ | IOMMU_WRITE, &pa->pa_pfn[pinned]); if (ret < 0) { diff --git a/drivers/s390/crypto/vfio_ap_ops.c b/drivers/s390/crypto/vfio_ap_ops.c index 5781059d3ed2..70f484668ca0 100644 --- a/drivers/s390/crypto/vfio_ap_ops.c +++ b/drivers/s390/crypto/vfio_ap_ops.c @@ -124,7 +124,7 @@ static void vfio_ap_free_aqic_resources(struct vfio_ap_queue *q) q->saved_isc = VFIO_AP_ISC_INVALID; } if (q->saved_pfn && !WARN_ON(!q->matrix_mdev)) { - vfio_unpin_pages(&q->matrix_mdev->vdev, &q->saved_pfn, 1); + vfio_unpin_pages(&q->matrix_mdev->vdev, q->saved_pfn << PAGE_SHIFT, 1); q->saved_pfn = 0; } } @@ -258,7 +258,7 @@ static struct ap_queue_status vfio_ap_irq_enable(struct vfio_ap_queue *q, return status; } - ret = vfio_pin_pages(&q->matrix_mdev->vdev, &g_pfn, 1, + ret = vfio_pin_pages(&q->matrix_mdev->vdev, g_pfn << PAGE_SHIFT, 1, IOMMU_READ | IOMMU_WRITE, &h_pfn); switch (ret) { case 1: @@ -301,7 +301,7 @@ static struct ap_queue_status vfio_ap_irq_enable(struct vfio_ap_queue *q, break; case AP_RESPONSE_OTHERWISE_CHANGED: /* We could not modify IRQ setings: clear new configuration */ - vfio_unpin_pages(&q->matrix_mdev->vdev, &g_pfn, 1); + vfio_unpin_pages(&q->matrix_mdev->vdev, g_pfn << PAGE_SHIFT, 1); kvm_s390_gisc_unregister(kvm, isc); break; default: @@ -1232,9 +1232,8 @@ static void vfio_ap_mdev_dma_unmap(struct vfio_device *vdev, u64 iova, { struct ap_matrix_mdev *matrix_mdev = container_of(vdev, struct ap_matrix_mdev, vdev); - unsigned long g_pfn = iova >> PAGE_SHIFT; - vfio_unpin_pages(&matrix_mdev->vdev, &g_pfn, 1); + vfio_unpin_pages(&matrix_mdev->vdev, iova, 1); } /** diff --git a/drivers/vfio/vfio.c b/drivers/vfio/vfio.c index 92b10aafae28..ffd1a492eea9 100644 --- a/drivers/vfio/vfio.c +++ b/drivers/vfio/vfio.c @@ -1934,17 +1934,17 @@ int vfio_set_irqs_validate_and_prepare(struct vfio_irq_set *hdr, int num_irqs, EXPORT_SYMBOL(vfio_set_irqs_validate_and_prepare); /* - * Pin a set of guest PFNs and return their associated host PFNs for local + * Pin contiguous user pages and return their associated host pages for local * domain only. * @device [in] : device - * @user_pfn [in]: array of user/guest PFNs to be pinned. - * @npage [in] : count of elements in user_pfn array. This count should not - * be greater VFIO_PIN_PAGES_MAX_ENTRIES. + * @iova [in] : starting IOVA of user pages to be pinned. + * @npage [in] : count of pages to be pinned. This count should not + * be greater than VFIO_PIN_PAGES_MAX_ENTRIES. * @prot [in] : protection flags * @phys_pfn[out]: array of host PFNs * Return error or number of pages pinned. */ -int vfio_pin_pages(struct vfio_device *device, unsigned long *user_pfn, +int vfio_pin_pages(struct vfio_device *device, dma_addr_t iova, int npage, int prot, unsigned long *phys_pfn) { struct vfio_container *container; @@ -1952,8 +1952,7 @@ int vfio_pin_pages(struct vfio_device *device, unsigned long *user_pfn, struct vfio_iommu_driver *driver; int ret; - if (!user_pfn || !phys_pfn || !npage || - !vfio_assert_device_open(device)) + if (!phys_pfn || !npage || !vfio_assert_device_open(device)) return -EINVAL; if (npage > VFIO_PIN_PAGES_MAX_ENTRIES) @@ -1967,7 +1966,7 @@ int vfio_pin_pages(struct vfio_device *device, unsigned long *user_pfn, driver = container->iommu_driver; if (likely(driver && driver->ops->pin_pages)) ret = driver->ops->pin_pages(container->iommu_data, - group->iommu_group, user_pfn, + group->iommu_group, iova, npage, prot, phys_pfn); else ret = -ENOTTY; @@ -1977,15 +1976,13 @@ int vfio_pin_pages(struct vfio_device *device, unsigned long *user_pfn, EXPORT_SYMBOL(vfio_pin_pages); /* - * Unpin set of host PFNs for local domain only. + * Unpin contiguous host pages for local domain only. * @device [in] : device - * @user_pfn [in]: array of user/guest PFNs to be unpinned. Number of user/guest - * PFNs should not be greater than VFIO_PIN_PAGES_MAX_ENTRIES. - * @npage [in] : count of elements in user_pfn array. This count should not + * @iova [in] : starting address of user pages to be unpinned. + * @npage [in] : count of pages to be unpinned. This count should not * be greater than VFIO_PIN_PAGES_MAX_ENTRIES. */ -void vfio_unpin_pages(struct vfio_device *device, unsigned long *user_pfn, - int npage) +void vfio_unpin_pages(struct vfio_device *device, dma_addr_t iova, int npage) { struct vfio_container *container; struct vfio_iommu_driver *driver; @@ -2000,7 +1997,7 @@ void vfio_unpin_pages(struct vfio_device *device, unsigned long *user_pfn, container = device->group->container; driver = container->iommu_driver; - driver->ops->unpin_pages(container->iommu_data, user_pfn, npage); + driver->ops->unpin_pages(container->iommu_data, iova, npage); } EXPORT_SYMBOL(vfio_unpin_pages); diff --git a/drivers/vfio/vfio.h b/drivers/vfio/vfio.h index 6a8424b407c7..e9767e13f00f 100644 --- a/drivers/vfio/vfio.h +++ b/drivers/vfio/vfio.h @@ -50,11 +50,11 @@ struct vfio_iommu_driver_ops { struct iommu_group *group); int (*pin_pages)(void *iommu_data, struct iommu_group *group, - unsigned long *user_pfn, + dma_addr_t user_iova, int npage, int prot, unsigned long *phys_pfn); void (*unpin_pages)(void *iommu_data, - unsigned long *user_pfn, int npage); + dma_addr_t user_iova, int npage); void (*register_device)(void *iommu_data, struct vfio_device *vdev); void (*unregister_device)(void *iommu_data, diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c index e49fbe9968ef..e629e059118c 100644 --- a/drivers/vfio/vfio_iommu_type1.c +++ b/drivers/vfio/vfio_iommu_type1.c @@ -829,7 +829,7 @@ static int vfio_unpin_page_external(struct vfio_dma *dma, dma_addr_t iova, static int vfio_iommu_type1_pin_pages(void *iommu_data, struct iommu_group *iommu_group, - unsigned long *user_pfn, + dma_addr_t user_iova, int npage, int prot, unsigned long *phys_pfn) { @@ -841,7 +841,7 @@ static int vfio_iommu_type1_pin_pages(void *iommu_data, bool do_accounting; dma_addr_t iova; - if (!iommu || !user_pfn || !phys_pfn) + if (!iommu || !phys_pfn) return -EINVAL; /* Supported for v2 version only */ @@ -857,7 +857,7 @@ static int vfio_iommu_type1_pin_pages(void *iommu_data, again: if (iommu->vaddr_invalid_count) { for (i = 0; i < npage; i++) { - iova = user_pfn[i] << PAGE_SHIFT; + iova = user_iova + PAGE_SIZE * i; ret = vfio_find_dma_valid(iommu, iova, PAGE_SIZE, &dma); if (ret < 0) goto pin_done; @@ -882,7 +882,7 @@ static int vfio_iommu_type1_pin_pages(void *iommu_data, for (i = 0; i < npage; i++) { struct vfio_pfn *vpfn; - iova = user_pfn[i] << PAGE_SHIFT; + iova = user_iova + PAGE_SIZE * i; dma = vfio_find_dma(iommu, iova, PAGE_SIZE); if (!dma) { ret = -EINVAL; @@ -939,7 +939,7 @@ static int vfio_iommu_type1_pin_pages(void *iommu_data, for (j = 0; j < i; j++) { dma_addr_t iova; - iova = user_pfn[j] << PAGE_SHIFT; + iova = user_iova + PAGE_SIZE * j; dma = vfio_find_dma(iommu, iova, PAGE_SIZE); vfio_unpin_page_external(dma, iova, do_accounting); phys_pfn[j] = 0; @@ -950,7 +950,7 @@ static int vfio_iommu_type1_pin_pages(void *iommu_data, } static void vfio_iommu_type1_unpin_pages(void *iommu_data, - unsigned long *user_pfn, int npage) + dma_addr_t user_iova, int npage) { struct vfio_iommu *iommu = iommu_data; bool do_accounting; @@ -964,10 +964,9 @@ static void vfio_iommu_type1_unpin_pages(void *iommu_data, do_accounting = list_empty(&iommu->domain_list); for (i = 0; i < npage; i++) { + dma_addr_t iova = user_iova + PAGE_SIZE * i; struct vfio_dma *dma; - dma_addr_t iova; - iova = user_pfn[i] << PAGE_SHIFT; dma = vfio_find_dma(iommu, iova, PAGE_SIZE); if (!dma) break; diff --git a/include/linux/vfio.h b/include/linux/vfio.h index 9f7d74c24925..9e3b6abcf890 100644 --- a/include/linux/vfio.h +++ b/include/linux/vfio.h @@ -161,10 +161,9 @@ bool vfio_file_has_dev(struct file *file, struct vfio_device *device); #define VFIO_PIN_PAGES_MAX_ENTRIES (PAGE_SIZE/sizeof(unsigned long)) -int vfio_pin_pages(struct vfio_device *device, unsigned long *user_pfn, +int vfio_pin_pages(struct vfio_device *device, dma_addr_t iova, int npage, int prot, unsigned long *phys_pfn); -void vfio_unpin_pages(struct vfio_device *device, unsigned long *user_pfn, - int npage); +void vfio_unpin_pages(struct vfio_device *device, dma_addr_t iova, int npage); int vfio_dma_rw(struct vfio_device *device, dma_addr_t user_iova, void *data, size_t len, bool write); From patchwork Sat Jul 23 02:02:52 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 12927057 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3EC16CCA489 for ; Sat, 23 Jul 2022 02:04:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236785AbiGWCEo (ORCPT ); Fri, 22 Jul 2022 22:04:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50794 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236786AbiGWCEf (ORCPT ); Fri, 22 Jul 2022 22:04:35 -0400 Received: from NAM12-DM6-obe.outbound.protection.outlook.com (mail-dm6nam12on2072.outbound.protection.outlook.com [40.107.243.72]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 56E2793686; Fri, 22 Jul 2022 19:04:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=E6j9Mg7mOKV1ygjiqMvoO5xiEuCAemtPnstmBlQ7rxyARhic3HELH7/Hb4E0Fp4BjnQxCLdEtH8JXPmXMGnXpqnwYs3JEvKiygShunF3/MfYjpACjDSFYs+sWpgI/Mkh6t9oLZnXMQe6GoXCpDUmebLa/m+ade4ZLR/ntrtGkNag8uOpzole62wyMVq+wtQaSGDJesajiBfvMcpGPKUKoNRGJFUn/o+4F/zffiaglDxd6RXtRABCWBbHoxCsizS3trIiao410JRY6kNLUkdvhKmkDwZPtBlsHvBjlebT1oFRdN5Y1Mk/ByfclzsGC3tsjjzYd9U3PS0HlokOvpkPKA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=+6Ueh5ZIRe1c7jfu0thPGJb699aApUXrGyC8jbWoOYY=; b=DIeyQyVQxPPGmOrI8i4FfSCWfpjmCz9IIKJURfTlkDg//ofjYQjbHwBR5oEUw4jP1/7i8b0O/q/OqiAzs3UT4W2aYc+X7JjW7+Efr8C2S8ydN4rtSZjcCtt0hZRTaaw32jJgld9Hcey7F89B3+m078sHjTA4fNnIcFepN4YdyjjDvA7y7XyCvSxoNIVL09f0jF5oH5F8urr/YTulX+91i4/qXgVLD7g5W9EgY1PYwxJda1tunBNg62IUkNRDT9X0iohVJJHdo/6NBLfnzZFysWvKTDt6SDIJ3ZLIAXDfChtbK1NqOLPVR8h7441Ba6/fjY8XlBgEBi5uiiw6YjPjfQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 12.22.5.238) smtp.rcpttodomain=linux.ibm.com smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=+6Ueh5ZIRe1c7jfu0thPGJb699aApUXrGyC8jbWoOYY=; b=ek64Fae0kd0L0UxyrrFdy4DB1ZLrpITmSp1EhC5MS94R8zPod/t197Dba1GBeOEXXXFa5399ob34IJ68kmPshHyV7Oom7b1V42sQKgq3JdDnUokZAFNlBoSAFAZY3U8wK1S9z9tkpyBjJExCUNBoEkpHYzAJrOpuJVPYtRfpqtckgXVz0ePB4QFSVY4I48TfSGuxcVRpTOJKAE5qmRIDc2WJ2/MnFyrWzlg3bBwVHPia5eR6J4BLZ65iiWy3xjTFS+OneDGvK3pL5IK0ydkkIzQXUDtRMSmv85Ba0bgNDjTOtCwE3cUlJnvH11JjzsCnOrjf6IQ7MLpVH92ZWQAACg== Received: from BN9PR03CA0125.namprd03.prod.outlook.com (2603:10b6:408:fe::10) by DM5PR1201MB0201.namprd12.prod.outlook.com (2603:10b6:4:5b::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5438.21; Sat, 23 Jul 2022 02:04:21 +0000 Received: from BN8NAM11FT030.eop-nam11.prod.protection.outlook.com (2603:10b6:408:fe:cafe::ef) by BN9PR03CA0125.outlook.office365.com (2603:10b6:408:fe::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5458.18 via Frontend Transport; Sat, 23 Jul 2022 02:04:21 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 12.22.5.238) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 12.22.5.238 as permitted sender) receiver=protection.outlook.com; client-ip=12.22.5.238; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (12.22.5.238) by BN8NAM11FT030.mail.protection.outlook.com (10.13.177.146) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.20.5458.17 via Frontend Transport; Sat, 23 Jul 2022 02:04:20 +0000 Received: from drhqmail201.nvidia.com (10.126.190.180) by DRHQMAIL105.nvidia.com (10.27.9.14) with Microsoft SMTP Server (TLS) id 15.0.1497.32; Sat, 23 Jul 2022 02:04:20 +0000 Received: from drhqmail203.nvidia.com (10.126.190.182) by drhqmail201.nvidia.com (10.126.190.180) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.26; Fri, 22 Jul 2022 19:04:19 -0700 Received: from Asurada-Nvidia.nvidia.com (10.127.8.10) by mail.nvidia.com (10.126.190.182) with Microsoft SMTP Server id 15.2.986.26 via Frontend Transport; Fri, 22 Jul 2022 19:04:18 -0700 From: Nicolin Chen To: , , , , , , , , , , , , , , , , , , , , , , , , , , , CC: , , , , , , , , Subject: [PATCH v4 06/10] vfio/ap: Change saved_pfn to saved_iova Date: Fri, 22 Jul 2022 19:02:52 -0700 Message-ID: <20220723020256.30081-7-nicolinc@nvidia.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220723020256.30081-1-nicolinc@nvidia.com> References: <20220723020256.30081-1-nicolinc@nvidia.com> MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 48d6bc7c-f711-46d4-fa52-08da6c4fa86f X-MS-TrafficTypeDiagnostic: DM5PR1201MB0201:EE_ X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: fCb0kdsVky5sIvtT0Cm/J+lz/4sjmUgErpLohUClDcF27afRHjHjdzc4JEKG+VFsys09C2aPAJUKZCeb9746R/A/Zj/l/Ok6W56imf8KZQS5mCwWolzwKvOj3zNxVC16kyB4aMWpaYzJn4PR8pjvz//ol+ZuOjNEaG6DFQXk8HlDSogN6xAIKalWdCbMrtcgfIqUCG1qaFzmEeH212xgtkP5qEoSkrBmts0MPGSG4RD9tba7H6N+kykphWNrWAVIZM3usrnjTrBI1ISbvvQOeDBvFDukZq/8//4kkZVXCCYMEfdymca8LrqLNM183Jbx5JsOVGx2nim90Sz5KYg9c+OzNNkPg8NEPArW3s83odKGGptE5PVsVUnqIP5tCG/xX7AjTWLzBZqIyOvZuPNaeHmNwCyt1Qb+D2k4xcyuq88Xor/oM9vFDY8HB2cKBzveEc5oAtr6AATL5b2BRxCpCIYueYjwiSM9oQfIN7lsJQn1iPKQePM7asxe4QAiLuBuPWRo3kan5ldLWCaJ1WmYyn8BpHnV3bUqLz0SrteB1eUzeKSTyNxL4aw6excodgUXzf1MCrvczsYhpqgnQiFc3st6OcytnorEURRC7sVWGSjIfZwRSxjD6xRiwNED/Gd4dgF0Cyp4DyZtrW9pL7CaqvzcC4wl+vl0Cbc4yvbjbwnob3v9sWy0vD3aZ8G7BK2/+WJeoq+mVBjzridRubFSLhlyfdLWewutEUtoqv9NIvwuKLYE6m3QfYdOqlYMNvG3dbBdvXQFyy1inmqf1dou+eXl8Lwrzy5BTZEj2WTmnuVMB6VVByyrQi1sC4d6M0J+iAoqTWUDtfwGJdPefmGwcFjlYJuFamcaAYBYoOecjJwIdmMgeo2Ae+nhBJ4IlthwHi785gQJsKF9P1jRGbLewA== X-Forefront-Antispam-Report: CIP:12.22.5.238;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:InfoNoRecords;CAT:NONE;SFS:(13230016)(4636009)(376002)(346002)(39860400002)(136003)(396003)(46966006)(36840700001)(40470700004)(426003)(70586007)(5660300002)(8936002)(7416002)(7406005)(70206006)(4326008)(82310400005)(40480700001)(2906002)(8676002)(83380400001)(36860700001)(86362001)(356005)(1076003)(81166007)(82740400003)(26005)(478600001)(316002)(54906003)(36756003)(40460700003)(2616005)(110136005)(41300700001)(6666004)(7696005)(186003)(336012)(47076005)(921005)(83996005)(2101003)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 23 Jul 2022 02:04:20.8598 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 48d6bc7c-f711-46d4-fa52-08da6c4fa86f X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[12.22.5.238];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: BN8NAM11FT030.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM5PR1201MB0201 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org The vfio_ap_ops code maintains both nib address and its PFN, which is redundant, merely because vfio_pin/unpin_pages API wanted pfn. Since vfio_pin/unpin_pages() now accept "iova", change "saved_pfn" to "saved_iova" and remove pfn in the vfio_ap_validate_nib(). Reviewed-by: Jason Gunthorpe Reviewed-by: Tony Krowiak Tested-by: Eric Farman Signed-off-by: Nicolin Chen --- drivers/s390/crypto/vfio_ap_ops.c | 42 +++++++++++---------------- drivers/s390/crypto/vfio_ap_private.h | 4 +-- 2 files changed, 19 insertions(+), 27 deletions(-) diff --git a/drivers/s390/crypto/vfio_ap_ops.c b/drivers/s390/crypto/vfio_ap_ops.c index 70f484668ca0..d7c38c82f694 100644 --- a/drivers/s390/crypto/vfio_ap_ops.c +++ b/drivers/s390/crypto/vfio_ap_ops.c @@ -112,7 +112,7 @@ static void vfio_ap_wait_for_irqclear(int apqn) * * Unregisters the ISC in the GIB when the saved ISC not invalid. * Unpins the guest's page holding the NIB when it exists. - * Resets the saved_pfn and saved_isc to invalid values. + * Resets the saved_iova and saved_isc to invalid values. */ static void vfio_ap_free_aqic_resources(struct vfio_ap_queue *q) { @@ -123,9 +123,9 @@ static void vfio_ap_free_aqic_resources(struct vfio_ap_queue *q) kvm_s390_gisc_unregister(q->matrix_mdev->kvm, q->saved_isc); q->saved_isc = VFIO_AP_ISC_INVALID; } - if (q->saved_pfn && !WARN_ON(!q->matrix_mdev)) { - vfio_unpin_pages(&q->matrix_mdev->vdev, q->saved_pfn << PAGE_SHIFT, 1); - q->saved_pfn = 0; + if (q->saved_iova && !WARN_ON(!q->matrix_mdev)) { + vfio_unpin_pages(&q->matrix_mdev->vdev, q->saved_iova, 1); + q->saved_iova = 0; } } @@ -189,27 +189,19 @@ static struct ap_queue_status vfio_ap_irq_disable(struct vfio_ap_queue *q) * * @vcpu: the object representing the vcpu executing the PQAP(AQIC) instruction. * @nib: the location for storing the nib address. - * @g_pfn: the location for storing the page frame number of the page containing - * the nib. * * When the PQAP(AQIC) instruction is executed, general register 2 contains the * address of the notification indicator byte (nib) used for IRQ notification. - * This function parses the nib from gr2 and calculates the page frame - * number for the guest of the page containing the nib. The values are - * stored in @nib and @g_pfn respectively. - * - * The g_pfn of the nib is then validated to ensure the nib address is valid. + * This function parses and validates the nib from gr2. * * Return: returns zero if the nib address is a valid; otherwise, returns * -EINVAL. */ -static int vfio_ap_validate_nib(struct kvm_vcpu *vcpu, unsigned long *nib, - unsigned long *g_pfn) +static int vfio_ap_validate_nib(struct kvm_vcpu *vcpu, dma_addr_t *nib) { *nib = vcpu->run->s.regs.gprs[2]; - *g_pfn = *nib >> PAGE_SHIFT; - if (kvm_is_error_hva(gfn_to_hva(vcpu->kvm, *g_pfn))) + if (kvm_is_error_hva(gfn_to_hva(vcpu->kvm, *nib >> PAGE_SHIFT))) return -EINVAL; return 0; @@ -239,34 +231,34 @@ static struct ap_queue_status vfio_ap_irq_enable(struct vfio_ap_queue *q, int isc, struct kvm_vcpu *vcpu) { - unsigned long nib; struct ap_qirq_ctrl aqic_gisa = {}; struct ap_queue_status status = {}; struct kvm_s390_gisa *gisa; int nisc; struct kvm *kvm; - unsigned long g_pfn, h_pfn; + unsigned long h_pfn; phys_addr_t h_nib; + dma_addr_t nib; int ret; /* Verify that the notification indicator byte address is valid */ - if (vfio_ap_validate_nib(vcpu, &nib, &g_pfn)) { - VFIO_AP_DBF_WARN("%s: invalid NIB address: nib=%#lx, g_pfn=%#lx, apqn=%#04x\n", - __func__, nib, g_pfn, q->apqn); + if (vfio_ap_validate_nib(vcpu, &nib)) { + VFIO_AP_DBF_WARN("%s: invalid NIB address: nib=%pad, apqn=%#04x\n", + __func__, &nib, q->apqn); status.response_code = AP_RESPONSE_INVALID_ADDRESS; return status; } - ret = vfio_pin_pages(&q->matrix_mdev->vdev, g_pfn << PAGE_SHIFT, 1, + ret = vfio_pin_pages(&q->matrix_mdev->vdev, nib, 1, IOMMU_READ | IOMMU_WRITE, &h_pfn); switch (ret) { case 1: break; default: VFIO_AP_DBF_WARN("%s: vfio_pin_pages failed: rc=%d," - "nib=%#lx, g_pfn=%#lx, apqn=%#04x\n", - __func__, ret, nib, g_pfn, q->apqn); + "nib=%pad, apqn=%#04x\n", + __func__, ret, &nib, q->apqn); status.response_code = AP_RESPONSE_INVALID_ADDRESS; return status; @@ -296,12 +288,12 @@ static struct ap_queue_status vfio_ap_irq_enable(struct vfio_ap_queue *q, case AP_RESPONSE_NORMAL: /* See if we did clear older IRQ configuration */ vfio_ap_free_aqic_resources(q); - q->saved_pfn = g_pfn; + q->saved_iova = nib; q->saved_isc = isc; break; case AP_RESPONSE_OTHERWISE_CHANGED: /* We could not modify IRQ setings: clear new configuration */ - vfio_unpin_pages(&q->matrix_mdev->vdev, g_pfn << PAGE_SHIFT, 1); + vfio_unpin_pages(&q->matrix_mdev->vdev, nib, 1); kvm_s390_gisc_unregister(kvm, isc); break; default: diff --git a/drivers/s390/crypto/vfio_ap_private.h b/drivers/s390/crypto/vfio_ap_private.h index abb59d59f81b..d912487175d3 100644 --- a/drivers/s390/crypto/vfio_ap_private.h +++ b/drivers/s390/crypto/vfio_ap_private.h @@ -99,13 +99,13 @@ struct ap_matrix_mdev { * struct vfio_ap_queue - contains the data associated with a queue bound to the * vfio_ap device driver * @matrix_mdev: the matrix mediated device - * @saved_pfn: the guest PFN pinned for the guest + * @saved_iova: the notification indicator byte (nib) address * @apqn: the APQN of the AP queue device * @saved_isc: the guest ISC registered with the GIB interface */ struct vfio_ap_queue { struct ap_matrix_mdev *matrix_mdev; - unsigned long saved_pfn; + dma_addr_t saved_iova; int apqn; #define VFIO_AP_ISC_INVALID 0xff unsigned char saved_isc; From patchwork Sat Jul 23 02:02:53 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 12927056 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5847DC433EF for ; Sat, 23 Jul 2022 02:04:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237019AbiGWCEk (ORCPT ); Fri, 22 Jul 2022 22:04:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51164 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236777AbiGWCEf (ORCPT ); Fri, 22 Jul 2022 22:04:35 -0400 Received: from NAM11-CO1-obe.outbound.protection.outlook.com (mail-co1nam11on2069.outbound.protection.outlook.com [40.107.220.69]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 28F4ABA27F; Fri, 22 Jul 2022 19:04:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=jbKTedd5kN4m7b+pZeNhvTHKXlXj4IZu9F8qAfIVBxsSO1hTfTNEAH20+ttMArFgTQ/VIwWym8T1Z8x8zZhApcBC4kac0Qfi0qEKtwCMy1ZyCnoKNu8kkImmyHqqhiFEaHEk19T9XcaXmqE5xU4P+1OrXmXrF/QYWdMBoH2Y0eZiyRlb6qqWvROROIUFRlcphlXAi51qpfsFRrdvZb4Ookszjb57hi5hlWrlXalF1w/Rvs61qfc5oFYRj4y4YCOCLgCyuRL4RWMFfkfaCS0n07S1/FO6JdbIS813+t0nFT/pMvEt8t/8i/xr9gJKOgZ2iMMMnXluButktHu6GgsNTA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=iNhJaDERNaeZNaiarqKaIGTiwrrjSzADzEYwD4oQDmg=; b=SilDli/NwlgxWro2Vk7VcunXveFE2ni51vFBSvj5x2eHpIe42C5NBhg47bYoMMzibx1xt4rHLDHxX1pI8l/q7vtBgcExUzurHbIPtaJholOl/eSUnezhlaYq+HWzIjIQSUFpUTo8QX0mCkEQjLtkQGQA+w346Q7eWsOl8K7LQsg8Lbq4y0oHad7ZLfVuBhFzzGCnb0RB+2kyq9lesQphU0BY2bfsYYTHLTR0iMsnejChyIuOchkfW2AwbIFJkoH40MViouArxvVpRLzKcvTHtZFC6sl/X7GqETwNF0ZA1UBdMlu7K0eDuYQLnv5asa22++reZUYjA7ARf6y/ft4lrw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 12.22.5.234) smtp.rcpttodomain=linux.ibm.com smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=iNhJaDERNaeZNaiarqKaIGTiwrrjSzADzEYwD4oQDmg=; b=QDGUs8rxWYjPqSOe67W15uHpmtcMEMdMFHxVR8OKYLjXOu8wy7iNZeCwIapsxlEEJKEXv1xAECFCShHX/xfYs+aoDq8Wqv8ae9ugstB/fp44aSuy9C0jUI9Mtgj//V0jk98deOhnE4KKYjMxldi1R2gDkk4Ubm9VeHOSqTtputTO2y3v3iZ+3fZfFBND8TVTjYUxnzl5w8iwRVBEuBOfWz1TY1whoMoWCkGth724Kmk15f8aC1dnB6CMk30+Z8aHdyO10v0bu8JCbKSMO9orta2494y5i6UpcfRBP3totN/NKx6a+ZTq63jRjFS15I9MKfooova/Ek+04uwQAJDekw== Received: from BN8PR07CA0010.namprd07.prod.outlook.com (2603:10b6:408:ac::23) by MN0PR12MB6080.namprd12.prod.outlook.com (2603:10b6:208:3c8::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5458.19; Sat, 23 Jul 2022 02:04:22 +0000 Received: from BN8NAM11FT018.eop-nam11.prod.protection.outlook.com (2603:10b6:408:ac:cafe::93) by BN8PR07CA0010.outlook.office365.com (2603:10b6:408:ac::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5438.20 via Frontend Transport; Sat, 23 Jul 2022 02:04:22 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 12.22.5.234) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 12.22.5.234 as permitted sender) receiver=protection.outlook.com; client-ip=12.22.5.234; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (12.22.5.234) by BN8NAM11FT018.mail.protection.outlook.com (10.13.176.89) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.20.5458.17 via Frontend Transport; Sat, 23 Jul 2022 02:04:22 +0000 Received: from drhqmail201.nvidia.com (10.126.190.180) by DRHQMAIL101.nvidia.com (10.27.9.10) with Microsoft SMTP Server (TLS) id 15.0.1497.32; Sat, 23 Jul 2022 02:04:21 +0000 Received: from drhqmail203.nvidia.com (10.126.190.182) by drhqmail201.nvidia.com (10.126.190.180) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.26; Fri, 22 Jul 2022 19:04:21 -0700 Received: from Asurada-Nvidia.nvidia.com (10.127.8.10) by mail.nvidia.com (10.126.190.182) with Microsoft SMTP Server id 15.2.986.26 via Frontend Transport; Fri, 22 Jul 2022 19:04:20 -0700 From: Nicolin Chen To: , , , , , , , , , , , , , , , , , , , , , , , , , , , CC: , , , , , , , , Subject: [PATCH v4 07/10] vfio/ccw: Change pa_pfn list to pa_iova list Date: Fri, 22 Jul 2022 19:02:53 -0700 Message-ID: <20220723020256.30081-8-nicolinc@nvidia.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220723020256.30081-1-nicolinc@nvidia.com> References: <20220723020256.30081-1-nicolinc@nvidia.com> MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 844347d8-d46f-490a-29f4-08da6c4fa972 X-MS-TrafficTypeDiagnostic: MN0PR12MB6080:EE_ X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: awsZ7OO3VuXIc0X735dPek7FvyiwhVlN1kIuBl3eZb10fuAaLXXfgCiqPy7iGhUYGWKSgFwkjnR5aZxkZ/bCmlKXOJDB71iQsNSCcbw+jLNSbZP+DF/gRCnauDeo6grup7MPlqkMg96Ci424UR7N4rWlQuqOpn2+JihGKqNCg+aqZlLpDdxYP1G9styGUD/Wk3B3+ToUig9lGqySTsJwIAu2MlpP8RAq+5kAZnrtJdYTb6QIvpbaCmt8MkipOePaBt8k2IgAPPVXKIDVrKxS1vngfYnD7Ql3zQ2yQOwWkUCsG0CVo6b+wCZxp2BoGKLGE1mC11ZVC0Eskf2KFztfQihYCAzZ66D5yueS/ZzDDaNGcKQq2flvj+njXUrtO99+RsfzFB+YAWqkjd6H8KHEMMq+5RrNzLOmcYD1CZMbQ9no7aFjjZYtXIXQeKpFK3QRFk9nJgoUeCLqM8yuctSjAQ3mMsFuMsGQVYV8T7DAp7SWlb+5IaoQWc2EXGFfNmA6nTkTllTK0wdzs9LAg2wVMbBaeExDDK1ngoDPQ7Ijccl8R8p8F70WMZRq5/A+PClhyal4d6iPDUbUYkT7SJNBe6vGPuK8LCTpM7V/N/L1bKXeSRRJhy0cGrT/1dmy085oB08EKAS2kqQZsEQ/eTsaID7Mrmw0qNpGw4uZs+AgiTD1XXCofrUL+qXS4Lv1X1GbV1AjKJLQKgf7YQJLgE8Q8GOMBgRGOiNGsB1SXkbIFWJw40iLoLAo1F4vAODuPTXLPmbkVURpjSBjC8z/nL+peCG0b3u38F0pWLV9fZwN2h1cnj6u5NGZgxyeirdbUWvJq31GnTAKJjF4kucgHGwFJxWejwD4P0m7WOpDazcevANvc503ZPXPvuaGWJZDFUK+Iv2NZpo+66poB/gzGZgelQ== X-Forefront-Antispam-Report: CIP:12.22.5.234;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:InfoNoRecords;CAT:NONE;SFS:(13230016)(4636009)(346002)(396003)(376002)(39860400002)(136003)(46966006)(36840700001)(40470700004)(83380400001)(921005)(7696005)(82310400005)(186003)(36756003)(40460700003)(82740400003)(81166007)(54906003)(316002)(36860700001)(8676002)(110136005)(4326008)(70586007)(70206006)(356005)(8936002)(86362001)(2616005)(478600001)(5660300002)(426003)(7406005)(30864003)(26005)(41300700001)(7416002)(1076003)(47076005)(336012)(2906002)(6666004)(40480700001)(36900700001)(83996005)(2101003);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 23 Jul 2022 02:04:22.4894 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 844347d8-d46f-490a-29f4-08da6c4fa972 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[12.22.5.234];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: BN8NAM11FT018.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN0PR12MB6080 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org The vfio_ccw_cp code maintains both iova and its PFN list because the vfio_pin/unpin_pages API wanted pfn list. Since vfio_pin/unpin_pages() now accept "iova", change to maintain only pa_iova list and rename all "pfn_array" strings to "page_array", so as to simplify the code. Reviewed-by: Jason Gunthorpe Reviewed-by: Eric Farman Tested-by: Eric Farman Signed-off-by: Nicolin Chen --- drivers/s390/cio/vfio_ccw_cp.c | 135 ++++++++++++++++----------------- 1 file changed, 64 insertions(+), 71 deletions(-) diff --git a/drivers/s390/cio/vfio_ccw_cp.c b/drivers/s390/cio/vfio_ccw_cp.c index a739262f988d..3854c3d573f5 100644 --- a/drivers/s390/cio/vfio_ccw_cp.c +++ b/drivers/s390/cio/vfio_ccw_cp.c @@ -18,11 +18,9 @@ #include "vfio_ccw_cp.h" #include "vfio_ccw_private.h" -struct pfn_array { - /* Starting guest physical I/O address. */ - unsigned long pa_iova; - /* Array that stores PFNs of the pages need to pin. */ - unsigned long *pa_iova_pfn; +struct page_array { + /* Array that stores pages need to pin. */ + dma_addr_t *pa_iova; /* Array that receives PFNs of the pages pinned. */ unsigned long *pa_pfn; /* Number of pages pinned from @pa_iova. */ @@ -37,53 +35,50 @@ struct ccwchain { /* Count of the valid ccws in chain. */ int ch_len; /* Pinned PAGEs for the original data. */ - struct pfn_array *ch_pa; + struct page_array *ch_pa; }; /* - * pfn_array_alloc() - alloc memory for PFNs - * @pa: pfn_array on which to perform the operation + * page_array_alloc() - alloc memory for page array + * @pa: page_array on which to perform the operation * @iova: target guest physical address * @len: number of bytes that should be pinned from @iova * - * Attempt to allocate memory for PFNs. + * Attempt to allocate memory for page array. * - * Usage of pfn_array: - * We expect (pa_nr == 0) and (pa_iova_pfn == NULL), any field in + * Usage of page_array: + * We expect (pa_nr == 0) and (pa_iova == NULL), any field in * this structure will be filled in by this function. * * Returns: - * 0 if PFNs are allocated - * -EINVAL if pa->pa_nr is not initially zero, or pa->pa_iova_pfn is not NULL + * 0 if page array is allocated + * -EINVAL if pa->pa_nr is not initially zero, or pa->pa_iova is not NULL * -ENOMEM if alloc failed */ -static int pfn_array_alloc(struct pfn_array *pa, u64 iova, unsigned int len) +static int page_array_alloc(struct page_array *pa, u64 iova, unsigned int len) { int i; - if (pa->pa_nr || pa->pa_iova_pfn) + if (pa->pa_nr || pa->pa_iova) return -EINVAL; - pa->pa_iova = iova; - pa->pa_nr = ((iova & ~PAGE_MASK) + len + (PAGE_SIZE - 1)) >> PAGE_SHIFT; if (!pa->pa_nr) return -EINVAL; - pa->pa_iova_pfn = kcalloc(pa->pa_nr, - sizeof(*pa->pa_iova_pfn) + - sizeof(*pa->pa_pfn), - GFP_KERNEL); - if (unlikely(!pa->pa_iova_pfn)) { + pa->pa_iova = kcalloc(pa->pa_nr, + sizeof(*pa->pa_iova) + sizeof(*pa->pa_pfn), + GFP_KERNEL); + if (unlikely(!pa->pa_iova)) { pa->pa_nr = 0; return -ENOMEM; } - pa->pa_pfn = pa->pa_iova_pfn + pa->pa_nr; + pa->pa_pfn = (unsigned long *)&pa->pa_iova[pa->pa_nr]; - pa->pa_iova_pfn[0] = pa->pa_iova >> PAGE_SHIFT; + pa->pa_iova[0] = iova; pa->pa_pfn[0] = -1ULL; for (i = 1; i < pa->pa_nr; i++) { - pa->pa_iova_pfn[i] = pa->pa_iova_pfn[i - 1] + 1; + pa->pa_iova[i] = pa->pa_iova[i - 1] + PAGE_SIZE; pa->pa_pfn[i] = -1ULL; } @@ -91,30 +86,30 @@ static int pfn_array_alloc(struct pfn_array *pa, u64 iova, unsigned int len) } /* - * pfn_array_unpin() - Unpin user pages in memory - * @pa: pfn_array on which to perform the operation + * page_array_unpin() - Unpin user pages in memory + * @pa: page_array on which to perform the operation * @vdev: the vfio device to perform the operation * @pa_nr: number of user pages to unpin * * Only unpin if any pages were pinned to begin with, i.e. pa_nr > 0, * otherwise only clear pa->pa_nr */ -static void pfn_array_unpin(struct pfn_array *pa, - struct vfio_device *vdev, int pa_nr) +static void page_array_unpin(struct page_array *pa, + struct vfio_device *vdev, int pa_nr) { int unpinned = 0, npage = 1; while (unpinned < pa_nr) { - unsigned long *first = &pa->pa_iova_pfn[unpinned]; - unsigned long *last = &first[npage]; + dma_addr_t *first = &pa->pa_iova[unpinned]; + dma_addr_t *last = &first[npage]; if (unpinned + npage < pa_nr && - *first + npage == *last) { + *first + npage * PAGE_SIZE == *last) { npage++; continue; } - vfio_unpin_pages(vdev, *first << PAGE_SHIFT, npage); + vfio_unpin_pages(vdev, *first, npage); unpinned += npage; npage = 1; } @@ -123,30 +118,30 @@ static void pfn_array_unpin(struct pfn_array *pa, } /* - * pfn_array_pin() - Pin user pages in memory - * @pa: pfn_array on which to perform the operation + * page_array_pin() - Pin user pages in memory + * @pa: page_array on which to perform the operation * @mdev: the mediated device to perform pin operations * * Returns number of pages pinned upon success. * If the pin request partially succeeds, or fails completely, * all pages are left unpinned and a negative error value is returned. */ -static int pfn_array_pin(struct pfn_array *pa, struct vfio_device *vdev) +static int page_array_pin(struct page_array *pa, struct vfio_device *vdev) { int pinned = 0, npage = 1; int ret = 0; while (pinned < pa->pa_nr) { - unsigned long *first = &pa->pa_iova_pfn[pinned]; - unsigned long *last = &first[npage]; + dma_addr_t *first = &pa->pa_iova[pinned]; + dma_addr_t *last = &first[npage]; if (pinned + npage < pa->pa_nr && - *first + npage == *last) { + *first + npage * PAGE_SIZE == *last) { npage++; continue; } - ret = vfio_pin_pages(vdev, *first << PAGE_SHIFT, npage, + ret = vfio_pin_pages(vdev, *first, npage, IOMMU_READ | IOMMU_WRITE, &pa->pa_pfn[pinned]); if (ret < 0) { @@ -163,32 +158,30 @@ static int pfn_array_pin(struct pfn_array *pa, struct vfio_device *vdev) return ret; err_out: - pfn_array_unpin(pa, vdev, pinned); + page_array_unpin(pa, vdev, pinned); return ret; } /* Unpin the pages before releasing the memory. */ -static void pfn_array_unpin_free(struct pfn_array *pa, struct vfio_device *vdev) +static void page_array_unpin_free(struct page_array *pa, struct vfio_device *vdev) { - pfn_array_unpin(pa, vdev, pa->pa_nr); - kfree(pa->pa_iova_pfn); + page_array_unpin(pa, vdev, pa->pa_nr); + kfree(pa->pa_iova); } -static bool pfn_array_iova_pinned(struct pfn_array *pa, unsigned long iova) +static bool page_array_iova_pinned(struct page_array *pa, unsigned long iova) { - unsigned long iova_pfn = iova >> PAGE_SHIFT; int i; for (i = 0; i < pa->pa_nr; i++) - if (pa->pa_iova_pfn[i] == iova_pfn) + if (pa->pa_iova[i] == iova) return true; return false; } -/* Create the list of IDAL words for a pfn_array. */ -static inline void pfn_array_idal_create_words( - struct pfn_array *pa, - unsigned long *idaws) +/* Create the list of IDAL words for a page_array. */ +static inline void page_array_idal_create_words(struct page_array *pa, + unsigned long *idaws) { int i; @@ -204,7 +197,7 @@ static inline void pfn_array_idal_create_words( idaws[i] = pa->pa_pfn[i] << PAGE_SHIFT; /* Adjust the first IDAW, since it may not start on a page boundary */ - idaws[0] += pa->pa_iova & (PAGE_SIZE - 1); + idaws[0] += pa->pa_iova[0] & (PAGE_SIZE - 1); } static void convert_ccw0_to_ccw1(struct ccw1 *source, unsigned long len) @@ -236,18 +229,18 @@ static void convert_ccw0_to_ccw1(struct ccw1 *source, unsigned long len) static long copy_from_iova(struct vfio_device *vdev, void *to, u64 iova, unsigned long n) { - struct pfn_array pa = {0}; + struct page_array pa = {0}; u64 from; int i, ret; unsigned long l, m; - ret = pfn_array_alloc(&pa, iova, n); + ret = page_array_alloc(&pa, iova, n); if (ret < 0) return ret; - ret = pfn_array_pin(&pa, vdev); + ret = page_array_pin(&pa, vdev); if (ret < 0) { - pfn_array_unpin_free(&pa, vdev); + page_array_unpin_free(&pa, vdev); return ret; } @@ -268,7 +261,7 @@ static long copy_from_iova(struct vfio_device *vdev, void *to, u64 iova, break; } - pfn_array_unpin_free(&pa, vdev); + page_array_unpin_free(&pa, vdev); return l; } @@ -371,7 +364,7 @@ static struct ccwchain *ccwchain_alloc(struct channel_program *cp, int len) chain->ch_ccw = (struct ccw1 *)data; data = (u8 *)(chain->ch_ccw) + sizeof(*chain->ch_ccw) * len; - chain->ch_pa = (struct pfn_array *)data; + chain->ch_pa = (struct page_array *)data; chain->ch_len = len; @@ -555,7 +548,7 @@ static int ccwchain_fetch_direct(struct ccwchain *chain, struct vfio_device *vdev = &container_of(cp, struct vfio_ccw_private, cp)->vdev; struct ccw1 *ccw; - struct pfn_array *pa; + struct page_array *pa; u64 iova; unsigned long *idaws; int ret; @@ -589,13 +582,13 @@ static int ccwchain_fetch_direct(struct ccwchain *chain, } /* - * Allocate an array of pfn's for pages to pin/translate. + * Allocate an array of pages to pin/translate. * The number of pages is actually the count of the idaws * required for the data transfer, since we only only support * 4K IDAWs today. */ pa = chain->ch_pa + idx; - ret = pfn_array_alloc(pa, iova, bytes); + ret = page_array_alloc(pa, iova, bytes); if (ret < 0) goto out_free_idaws; @@ -606,21 +599,21 @@ static int ccwchain_fetch_direct(struct ccwchain *chain, goto out_unpin; /* - * Copy guest IDAWs into pfn_array, in case the memory they + * Copy guest IDAWs into page_array, in case the memory they * occupy is not contiguous. */ for (i = 0; i < idaw_nr; i++) - pa->pa_iova_pfn[i] = idaws[i] >> PAGE_SHIFT; + pa->pa_iova[i] = idaws[i]; } else { /* - * No action is required here; the iova addresses in pfn_array - * were initialized sequentially in pfn_array_alloc() beginning + * No action is required here; the iova addresses in page_array + * were initialized sequentially in page_array_alloc() beginning * with the contents of ccw->cda. */ } if (ccw_does_data_transfer(ccw)) { - ret = pfn_array_pin(pa, vdev); + ret = page_array_pin(pa, vdev); if (ret < 0) goto out_unpin; } else { @@ -630,13 +623,13 @@ static int ccwchain_fetch_direct(struct ccwchain *chain, ccw->cda = (__u32) virt_to_phys(idaws); ccw->flags |= CCW_FLAG_IDA; - /* Populate the IDAL with pinned/translated addresses from pfn */ - pfn_array_idal_create_words(pa, idaws); + /* Populate the IDAL with pinned/translated addresses from page */ + page_array_idal_create_words(pa, idaws); return 0; out_unpin: - pfn_array_unpin_free(pa, vdev); + page_array_unpin_free(pa, vdev); out_free_idaws: kfree(idaws); out_init: @@ -742,7 +735,7 @@ void cp_free(struct channel_program *cp) cp->initialized = false; list_for_each_entry_safe(chain, temp, &cp->ccwchain_list, next) { for (i = 0; i < chain->ch_len; i++) { - pfn_array_unpin_free(chain->ch_pa + i, vdev); + page_array_unpin_free(chain->ch_pa + i, vdev); ccwchain_cda_free(chain, i); } ccwchain_free(chain); @@ -918,7 +911,7 @@ bool cp_iova_pinned(struct channel_program *cp, u64 iova) list_for_each_entry(chain, &cp->ccwchain_list, next) { for (i = 0; i < chain->ch_len; i++) - if (pfn_array_iova_pinned(chain->ch_pa + i, iova)) + if (page_array_iova_pinned(chain->ch_pa + i, iova)) return true; } From patchwork Sat Jul 23 02:02:54 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 12927058 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1E0B2CCA473 for ; Sat, 23 Jul 2022 02:05:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237109AbiGWCFF (ORCPT ); Fri, 22 Jul 2022 22:05:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50848 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236887AbiGWCEh (ORCPT ); Fri, 22 Jul 2022 22:04:37 -0400 Received: from NAM10-BN7-obe.outbound.protection.outlook.com (mail-bn7nam10on2063.outbound.protection.outlook.com [40.107.92.63]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7934CBB20E; Fri, 22 Jul 2022 19:04:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=fuWlw5uEEspmsEeZe46/pB21QNsUFRLm7bKKUqhE0yEKyKAp/34tfUYonOsd7GFxCiVWH5OQsTjd/9EcdltwpRn1UIcpNGBo3CjN8VEu8Id3/mX8zI/cN/W7ihO+CJkISRnBARqBjMmAlC2kMLbo6U6lfWnopcp7GK+a7vz6KtxsoQiJ+/63IQEghdvW6cUv67UQtK5a9qX5LPaOF6uZk33iQ3FvXPA3qN4zKNRtcGl6ftg1AFGG6O1Jk/A4cVlx7z5J3lhqIUrFbU7OD/llYI26imf8hgJZKYvCtxlGNuTQJM4/vvVhH+q5MFgWbb0KjkGbhBVKOABZZ0hX5QIRvQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=vikqsyFr0cUNkzLiNnVEYY2PhcMznhk0zaOJOlwEqJw=; b=lY7L7vItQB9UR/UOVuG4wwhFWOTYJaWU8n+EGxVUg4ZsOrQoSRJ9A0j71twJdwWjy06rLpFQcWS9rVYiigFxf17RDOFsRYePHCwqu1e/MkI5kkzW3ckRuxuOPPPXxYJHlUflgis5h0rym1bEAgaEtZet0Zy8K8N4hv7Hu/0bPEtDpcwDfHgQ30WySAdKmJg4FhR649Ih6YmDLgMmFLooBelGqW4DcckVLhI0augSC6wbRn5VSFcbAHPV+FI/4g3B31xh3BavjtHDvua/YNlzfEi6lhynZ0PQQRDeiwMWKzdtOzJhxn1ZrvjpwfpDPo+7TwGkFpxspSnuT1QQSXoA2g== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 12.22.5.235) smtp.rcpttodomain=linux.ibm.com smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=vikqsyFr0cUNkzLiNnVEYY2PhcMznhk0zaOJOlwEqJw=; b=a+WzWoFB8F4vk6RH8zuvBedDSLX7VSdGfZMb86INxYXoOyRgXNmodJsj3q5A2vPUBYtvcGOGjpZZ+SQdmv6tTfG6t/LS3GEoddrikMBbtWfrKbvucm8oBhesdPXZPA6De2jOXdYHzERsil0kWslG+wjnrGsZTMgS6L6EKWKLdff+34ZQS1eQubl/wnChF42wd2SpIjoG8BbC/ZidTN5TKK5p0Y8ufHA1eU3yUIFuLmnV4BPDx1TCVNR+gXfAGq0gBcxtZkcCEqDIM0fNCh3PBuN34Msy9blCu3D+xMXvVxq70WTAkxTqiBtOAVGY7HX4cEt7G5CcFFpEd2KYlAhh/Q== Received: from BN0PR04CA0187.namprd04.prod.outlook.com (2603:10b6:408:e9::12) by SA1PR12MB6679.namprd12.prod.outlook.com (2603:10b6:806:252::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5458.19; Sat, 23 Jul 2022 02:04:24 +0000 Received: from BN8NAM11FT038.eop-nam11.prod.protection.outlook.com (2603:10b6:408:e9:cafe::59) by BN0PR04CA0187.outlook.office365.com (2603:10b6:408:e9::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5438.15 via Frontend Transport; Sat, 23 Jul 2022 02:04:24 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 12.22.5.235) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 12.22.5.235 as permitted sender) receiver=protection.outlook.com; client-ip=12.22.5.235; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (12.22.5.235) by BN8NAM11FT038.mail.protection.outlook.com (10.13.176.246) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.20.5458.17 via Frontend Transport; Sat, 23 Jul 2022 02:04:24 +0000 Received: from drhqmail203.nvidia.com (10.126.190.182) by DRHQMAIL107.nvidia.com (10.27.9.16) with Microsoft SMTP Server (TLS) id 15.0.1497.32; Sat, 23 Jul 2022 02:04:23 +0000 Received: from drhqmail203.nvidia.com (10.126.190.182) by drhqmail203.nvidia.com (10.126.190.182) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.26; Fri, 22 Jul 2022 19:04:23 -0700 Received: from Asurada-Nvidia.nvidia.com (10.127.8.10) by mail.nvidia.com (10.126.190.182) with Microsoft SMTP Server id 15.2.986.26 via Frontend Transport; Fri, 22 Jul 2022 19:04:21 -0700 From: Nicolin Chen To: , , , , , , , , , , , , , , , , , , , , , , , , , , , CC: , , , , , , , , Subject: [PATCH v4 08/10] vfio: Rename user_iova of vfio_dma_rw() Date: Fri, 22 Jul 2022 19:02:54 -0700 Message-ID: <20220723020256.30081-9-nicolinc@nvidia.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220723020256.30081-1-nicolinc@nvidia.com> References: <20220723020256.30081-1-nicolinc@nvidia.com> MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: d623102a-1fc3-45ab-ffa2-08da6c4faa61 X-MS-TrafficTypeDiagnostic: SA1PR12MB6679:EE_ X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: FzCFZ2U4Nn9FoB9ZXaV6n8D5mil5MuZkm0yi1LvijFA4jI7orIX/7wbu3vmHO5OeLP/JboD0gK+gtZEJ0Tn5J9yEM4zjbSvVlu60WqW9a6od+11ah0x4NQ53ky8zS0p5qKovYgkMMKdvGgEzbZKdwJVoCc5Rz5f0d6az1GYaCqaTF+paOLF/IFSIMBHrfKGsUJkxnphRbXTGjyIdZq3uAPO7ZHbF84kNlEZXKnPUgtBz/V+AUkuqdFU3M1k/EYF3Le/leqpUUpBxQgyjtC3GBjQYSyFxi1oJ4sJu0mP4SudH7LNwx+FHUH/eniWI2+m4k2QxZP0Mfr6gzIyVH3sozIlDX15MnbdhP2OhMFAXFwiP4yi/0leOlTx013X73m+8MkXLF7BNfgHR1bD5c9751NQkAsTTD0CApCrUfZaROfu1SrINzjqwjXIZxxRpVf2onaN2bYTcmxpEnFFktWWbE78QWODD4ARTr87df7jaPzSQ8OekJj6xwqadpZ7AczQ52+8sVvqOyJ1oFrxNQHUbbn6DZeCXwVsU3OIsuF0vDVUqHCmqlyUT77nFunqVNNniKNQpG9bJHu8ozHABzsC33GVbeTkn/igq7Ie2iwcWGKHjexDZwImch3FT2bSK6subRuVrB9Lf9OzFHtcLj38zwCCoQjLJc3NztjgSrYOJqLpnuA4sPSJSuXsdIlPY65sQj0Wn5jIVyV1yYiPvqIp6U/QOVhakBgxlIL/v6PDudr1AUdfTZeJnu53Oz2lITZuXCPRS619QYLaWwvOuYIeuKUnZ96M5TCVvYol8mUs3COQqCn/c1TlhRuIM7ILvYTZwnFngwBgmA1onOAliPqHWmZrfjwZROTfMWb2JhYaib2Rg9VqnrP7pv8JWsEkxcZ/3BZf/FlDnVsd0Yjqai+bOGQ== X-Forefront-Antispam-Report: CIP:12.22.5.235;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:InfoNoRecords;CAT:NONE;SFS:(13230016)(4636009)(396003)(39860400002)(346002)(376002)(136003)(46966006)(36840700001)(40470700004)(1076003)(186003)(336012)(8676002)(4326008)(70206006)(426003)(47076005)(70586007)(6666004)(7696005)(41300700001)(26005)(7406005)(2906002)(8936002)(40460700003)(2616005)(7416002)(86362001)(478600001)(5660300002)(82740400003)(921005)(36756003)(316002)(356005)(81166007)(110136005)(83380400001)(82310400005)(54906003)(36860700001)(40480700001)(2101003)(83996005)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 23 Jul 2022 02:04:24.1393 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: d623102a-1fc3-45ab-ffa2-08da6c4faa61 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[12.22.5.235];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: BN8NAM11FT038.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA1PR12MB6679 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Following the updated vfio_pin/unpin_pages(), use the simpler "iova". Reviewed-by: Christoph Hellwig Reviewed-by: Jason Gunthorpe Reviewed-by: Kevin Tian Tested-by: Terrence Xu Tested-by: Eric Farman Signed-off-by: Nicolin Chen --- drivers/vfio/vfio.c | 6 +++--- include/linux/vfio.h | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/drivers/vfio/vfio.c b/drivers/vfio/vfio.c index ffd1a492eea9..606a20b605ba 100644 --- a/drivers/vfio/vfio.c +++ b/drivers/vfio/vfio.c @@ -2012,13 +2012,13 @@ EXPORT_SYMBOL(vfio_unpin_pages); * not a real device DMA, it is not necessary to pin the user space memory. * * @device [in] : VFIO device - * @user_iova [in] : base IOVA of a user space buffer + * @iova [in] : base IOVA of a user space buffer * @data [in] : pointer to kernel buffer * @len [in] : kernel buffer length * @write : indicate read or write * Return error code on failure or 0 on success. */ -int vfio_dma_rw(struct vfio_device *device, dma_addr_t user_iova, void *data, +int vfio_dma_rw(struct vfio_device *device, dma_addr_t iova, void *data, size_t len, bool write) { struct vfio_container *container; @@ -2034,7 +2034,7 @@ int vfio_dma_rw(struct vfio_device *device, dma_addr_t user_iova, void *data, if (likely(driver && driver->ops->dma_rw)) ret = driver->ops->dma_rw(container->iommu_data, - user_iova, data, len, write); + iova, data, len, write); else ret = -ENOTTY; return ret; diff --git a/include/linux/vfio.h b/include/linux/vfio.h index 9e3b6abcf890..acefd663e63b 100644 --- a/include/linux/vfio.h +++ b/include/linux/vfio.h @@ -164,7 +164,7 @@ bool vfio_file_has_dev(struct file *file, struct vfio_device *device); int vfio_pin_pages(struct vfio_device *device, dma_addr_t iova, int npage, int prot, unsigned long *phys_pfn); void vfio_unpin_pages(struct vfio_device *device, dma_addr_t iova, int npage); -int vfio_dma_rw(struct vfio_device *device, dma_addr_t user_iova, +int vfio_dma_rw(struct vfio_device *device, dma_addr_t iova, void *data, size_t len, bool write); /* From patchwork Sat Jul 23 02:02:55 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 12927059 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 634C8C433EF for ; Sat, 23 Jul 2022 02:05:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237141AbiGWCF3 (ORCPT ); Fri, 22 Jul 2022 22:05:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52144 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237126AbiGWCFF (ORCPT ); Fri, 22 Jul 2022 22:05:05 -0400 Received: from NAM12-MW2-obe.outbound.protection.outlook.com (mail-mw2nam12on2082.outbound.protection.outlook.com [40.107.244.82]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7E4FBB5575; Fri, 22 Jul 2022 19:04:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=RWjAbtgYTIHdiJfk8RNCr/hKeiCiwE/4T3zeZm1+qrknNRmJl2o/WRqxvH+wHJjIwGS/V94RdJcVeA6zuQ0c6HzKFlwuHrftC76pTBlLFh8RKNAfETkQ5Inm4wXqGZBTF04g+BM4RVEtYqtTUj5FGf3SmhL74ygTPdE1slDNitq2T2+T4l6R3zOwgorwSdMtPkuPT2CU6iKJDCuwhkZ1NNM7guFI6jNa+grhchvkYg6hhwSwbK5+RchQWcdmoTaUcJOobtI66hySHenUtFWQVquTUBIxLTy/E2GYHYOARKDktOKh2c/nzu7N1bkgqw3QgsMBW/HqjkfFx/1uvcGamg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=fGLzRxlahipGqkxQeGiJRlLebLZDQeu5tc4Mr/URf1o=; b=QOMrhx/2OpsjOsgkyIoxbUo3GcC+2HkA+Hbbsmzk3AnC/OrVny22WemAT3ZRUNmrMgm14FEUgouYHWkZ4fpWP8OsUJsdaL1/yGGrMiw7/8eGmryLjHBqk6ZF3EV8Hvpe5AVSrba4pwccb+5/08JLHEBVZabHTBb+UAz7gOmLwBoCtQ8qnseyeCy0V+vRTVf4bXcDsCPya/OGh/JxMIEIidNfscfOlO4TsIwBSEqTEX27D6fn7OBd/R5fajyTdc0fykDDEjBeKuLjuRcxVHUFI/MlRw0PkepaPq1XlXpsqwHUOXga4Ii+ZWloSXC3tLs53/EposYAo2B8aWPKbH6G2w== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 12.22.5.236) smtp.rcpttodomain=linux.ibm.com smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=fGLzRxlahipGqkxQeGiJRlLebLZDQeu5tc4Mr/URf1o=; b=hUKprwyS2ShX4UW6A5yWb8MLOpVl2ulqb5b+XMps8LpZNeMzho8uXHIkvL9E1Vo1aeOFd1Wote9xiqrDkmJzSNqz0DRuksDh79EbuqAM3/WTydK6r7A2RucCDEkOwbqzIZNEE5DSfw1a123MiFP+NSswD3txpnytvN/qngwwaQr04Jfx+9PE34W+nCJABe2FHQWKF/F2q3+1PndTAnjjTLpnH1orD5i8841NBhjDURR7Z24JpLX6ikGpNGPYXHPM8+fX6sDO5G294LvQf1dhWOS5gQERd8MhujUBwEYrQQ5KGKkA7yGYng/o3Y0HIV3Tssf2Q1tmqo016qmo+T6VeQ== Received: from DS7PR06CA0043.namprd06.prod.outlook.com (2603:10b6:8:54::25) by CY4PR1201MB0264.namprd12.prod.outlook.com (2603:10b6:910:1e::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5438.15; Sat, 23 Jul 2022 02:04:33 +0000 Received: from DM6NAM11FT037.eop-nam11.prod.protection.outlook.com (2603:10b6:8:54:cafe::aa) by DS7PR06CA0043.outlook.office365.com (2603:10b6:8:54::25) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5458.18 via Frontend Transport; Sat, 23 Jul 2022 02:04:33 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 12.22.5.236) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 12.22.5.236 as permitted sender) receiver=protection.outlook.com; client-ip=12.22.5.236; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (12.22.5.236) by DM6NAM11FT037.mail.protection.outlook.com (10.13.172.122) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.20.5458.17 via Frontend Transport; Sat, 23 Jul 2022 02:04:32 +0000 Received: from drhqmail201.nvidia.com (10.126.190.180) by DRHQMAIL109.nvidia.com (10.27.9.19) with Microsoft SMTP Server (TLS) id 15.0.1497.32; Sat, 23 Jul 2022 02:04:25 +0000 Received: from drhqmail203.nvidia.com (10.126.190.182) by drhqmail201.nvidia.com (10.126.190.180) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.26; Fri, 22 Jul 2022 19:04:24 -0700 Received: from Asurada-Nvidia.nvidia.com (10.127.8.10) by mail.nvidia.com (10.126.190.182) with Microsoft SMTP Server id 15.2.986.26 via Frontend Transport; Fri, 22 Jul 2022 19:04:23 -0700 From: Nicolin Chen To: , , , , , , , , , , , , , , , , , , , , , , , , , , , CC: , , , , , , , , Subject: [PATCH v4 09/10] vfio/ccw: Add kmap_local_page() for memcpy Date: Fri, 22 Jul 2022 19:02:55 -0700 Message-ID: <20220723020256.30081-10-nicolinc@nvidia.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220723020256.30081-1-nicolinc@nvidia.com> References: <20220723020256.30081-1-nicolinc@nvidia.com> MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: d1f3ab0d-40a5-47c6-b5cf-08da6c4fafa1 X-MS-TrafficTypeDiagnostic: CY4PR1201MB0264:EE_ X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: +ezRfR+lYn+Jz1aJFJzDLwZ6D0vcmn/Jrhvn9hPFBAJai7lDOUEG/G8caofJfIWqIdA6XxkcMSDlZuh+zT5JJzUaTICvZfCOdKGwQjJ8NV2VIlVCSAUx9Uku08JOA0jcAgjErFL+byn4JG4mDLr2EwcszCOkJaPCeRCh0mZQq2g/x2ZFpYrywR9huFNlTekIeQamZ5zjiqfCnXPRTqFpBvOMRK5EEL1lnqn10UU/73aGO7BzFWgkJDTRhrADnMDVhqvYxN+mgsBcS1qTYX/RpRbwHB166ZzGLP+qolRjUKXXWSYkUGMsakPFHUt2mfCMnHdkjUK6oyUVwK5uKLWHytNK4WBZuCUH1ZmKh3dqJ4yCSDwvgLKDi2sBctCXtRIMbhikgQkxE7Vqoy/HARKUeL6DH8lwlX9OIyWgLpMTtlFH9Pj8EF+/vSx1OBwejYKPxSAIMCvU3l/OGFzk+cw69i8SdfbnHJIDBja/+8i8uQiteQ7kCb1LYN10XKLYNsftsEmCCjqOYStNaK74y207OU+ZOqaGvu4RPZv566+FJr5KkgyZJoVm/AMOWGxjtFy2lCr6c1bs2ZMDUpo3M4nJdaUQdmGsIFt5GiqJB469pDh6/8JRiZCPR7sJrVKqIsYWwTf+lYf/3exoHIvvVZTTsFIgHsIoNYU1r5vWhtFDR54EX44kdVCi3fBwWAODllQuTAVa37MChfQGKBuwClx3jMbklGSnhyAPWZhhvOHvrkwfIDYSaYhkkWHDo1Uc5ZE+6QogzK7r8nu7/LSd0IdgLY/+GQZo3vJ3LvWIneudDda/NU43qIavGemdF+IG8lTpepcfeCnhYoo74KC0Oz1aOGSMxwDtHluksBDB0tGV6HV4jrdtzZ2Y2Igtyg0oOL2n/6dOHhk72DzILPvANnU6/w== X-Forefront-Antispam-Report: CIP:12.22.5.236;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:InfoNoRecords;CAT:NONE;SFS:(13230016)(4636009)(136003)(346002)(376002)(39860400002)(396003)(36840700001)(46966006)(40470700004)(2906002)(8676002)(6666004)(7416002)(478600001)(41300700001)(110136005)(26005)(356005)(70586007)(82740400003)(7406005)(7696005)(921005)(82310400005)(81166007)(86362001)(4326008)(47076005)(426003)(186003)(36756003)(83380400001)(336012)(54906003)(40460700003)(2616005)(316002)(5660300002)(36860700001)(70206006)(1076003)(8936002)(40480700001)(2101003)(36900700001)(83996005);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 23 Jul 2022 02:04:32.3361 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: d1f3ab0d-40a5-47c6-b5cf-08da6c4fafa1 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[12.22.5.236];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: DM6NAM11FT037.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY4PR1201MB0264 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org A PFN is not secure enough to promise that the memory is not IO. And direct access via memcpy() that only handles CPU memory will crash on S390 if the PFN is an IO PFN, as we have to use the memcpy_to/fromio() that uses the special S390 IO access instructions. On the other hand, a "struct page *" is always a CPU coherent thing that fits memcpy(). Also, casting a PFN to "void *" for memcpy() is not a proper practice, kmap_local_page() is the correct API to call here, though S390 doesn't use highmem, which means kmap_local_page() is a NOP. There's a following patch changing the vfio_pin_pages() API to return a list of "struct page *" instead of PFNs. It will block any IO memory from ever getting into this call path, for such a security purpose. In this patch, add kmap_local_page() to prepare for that. Suggested-by: Jason Gunthorpe Reviewed-by: Jason Gunthorpe Acked-by: Eric Farman Tested-by: Eric Farman Signed-off-by: Nicolin Chen --- drivers/s390/cio/vfio_ccw_cp.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/drivers/s390/cio/vfio_ccw_cp.c b/drivers/s390/cio/vfio_ccw_cp.c index 3854c3d573f5..cd4ec4f6d6ff 100644 --- a/drivers/s390/cio/vfio_ccw_cp.c +++ b/drivers/s390/cio/vfio_ccw_cp.c @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -230,7 +231,6 @@ static long copy_from_iova(struct vfio_device *vdev, void *to, u64 iova, unsigned long n) { struct page_array pa = {0}; - u64 from; int i, ret; unsigned long l, m; @@ -246,7 +246,9 @@ static long copy_from_iova(struct vfio_device *vdev, void *to, u64 iova, l = n; for (i = 0; i < pa.pa_nr; i++) { - from = pa.pa_pfn[i] << PAGE_SHIFT; + struct page *page = pfn_to_page(pa.pa_pfn[i]); + void *from = kmap_local_page(page); + m = PAGE_SIZE; if (i == 0) { from += iova & (PAGE_SIZE - 1); @@ -254,7 +256,8 @@ static long copy_from_iova(struct vfio_device *vdev, void *to, u64 iova, } m = min(l, m); - memcpy(to + (n - l), (void *)from, m); + memcpy(to + (n - l), from, m); + kunmap_local(from); l -= m; if (l == 0) From patchwork Sat Jul 23 02:02:56 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 12927060 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C5854CCA47C for ; Sat, 23 Jul 2022 02:05:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237175AbiGWCFj (ORCPT ); Fri, 22 Jul 2022 22:05:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51326 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229871AbiGWCFG (ORCPT ); Fri, 22 Jul 2022 22:05:06 -0400 Received: from NAM02-SN1-obe.outbound.protection.outlook.com (mail-sn1anam02on2075.outbound.protection.outlook.com [40.107.96.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4A48EC06FB; Fri, 22 Jul 2022 19:04:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=a1qzvJGnGM/kDwO3/E0GhseQsyjEbUiTVNKfgjUV48frxwR44Ds8COT4n9Rm6sktHk8tDhm4c1CpFPcQyFVKpuE0O4Az+nzk+YGswKWA00f5bvSuCV5hXWvJdFOZzD9V2lSIZQL8H3FLu5C6ra0aZntHBzi+Wgq67qyvmpYxrTmiPjWyN0Dpwtc+OauAFbGQHiENP7bLcZ3sri+H0qPv+HCDmVDY+z1Sc1l6XcWPILZcK9CCuXcwZ+8aUaJ4pMF4ou4aQPjpvJh1wCJ3cM2Ai1T7EA702gzDS3lgJHVOQ+czHPEYrCrSFCeBZJ87v88Ug5Uhd8GAUKDGGyQ7gWrbKA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=bxC0mDq13W+BnjqQ7KFHTfHIlM4tk7VY/COCaDQQKUg=; b=Ibl4NZMwNbpIN4z47b+RyhndhUvF0UEyrgq67eIETn01ug1HOSOC0h/J2o4R81UM6UOIf/G2Q/JAlNztD6uPPL3zPanIV1pU8aX2OmdxVMUwcTtcvYuWZ3NQ6PgKA0FCxLg/9tS16G5LF8QlO4NVbEWmp3f0F43rZ0jwqURdX7yECgeinjlZfQr0jXg5CiI1VT+xj02Sn2KsrC6zYW9y5VB3N5oMPzGM+RADWdCdOg7Xyv0udOatNC6HReeLskzjB4fuGSNfugtwDfVG0ofdOD6Ush5pV9VIRyeyTY8r+SOJ5tV6SJ3hXXcK3J8qeovbdCphlrbZCVpbnRBkKa+2pw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 12.22.5.235) smtp.rcpttodomain=linux.ibm.com smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=bxC0mDq13W+BnjqQ7KFHTfHIlM4tk7VY/COCaDQQKUg=; b=AofF4oaXSDO/LZDrU2ZZq1IkrLSUrZgLcta3chGijpSs2jBmy2uqfk9uRJ5++0z1reC2HCiqVZiD5y4Sjsv7IPMVSqk5JFN+D5FIKEC7ngTZFgJ6Zbcf3w1O8ZLEZ9HqjqGOyMrEjr3HZNKMlreYsEI2bFpCesR6w4IrK8NLsVx94CWy3skThx48sg3wN+I9zDHjsTa67Izb0H87OR/gok9wUyWKcdOuCF/TbEoZjtBZD0muVEPMUDQkK6Ov50Ia3AcmNbVL4AtCao9nEb5U8MTYJoA8s7EV6sdP4aWxYgfpoYvs7jq2s4J4ndgKC7FYxlQdBVfPSYyS6VVyeECUMw== Received: from BN9PR03CA0294.namprd03.prod.outlook.com (2603:10b6:408:f5::29) by BY5PR12MB3857.namprd12.prod.outlook.com (2603:10b6:a03:196::28) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5438.20; Sat, 23 Jul 2022 02:04:28 +0000 Received: from BN8NAM11FT016.eop-nam11.prod.protection.outlook.com (2603:10b6:408:f5:cafe::57) by BN9PR03CA0294.outlook.office365.com (2603:10b6:408:f5::29) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5438.23 via Frontend Transport; Sat, 23 Jul 2022 02:04:28 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 12.22.5.235) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 12.22.5.235 as permitted sender) receiver=protection.outlook.com; client-ip=12.22.5.235; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (12.22.5.235) by BN8NAM11FT016.mail.protection.outlook.com (10.13.176.97) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.20.5458.17 via Frontend Transport; Sat, 23 Jul 2022 02:04:27 +0000 Received: from drhqmail201.nvidia.com (10.126.190.180) by DRHQMAIL107.nvidia.com (10.27.9.16) with Microsoft SMTP Server (TLS) id 15.0.1497.32; Sat, 23 Jul 2022 02:04:27 +0000 Received: from drhqmail203.nvidia.com (10.126.190.182) by drhqmail201.nvidia.com (10.126.190.180) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.26; Fri, 22 Jul 2022 19:04:26 -0700 Received: from Asurada-Nvidia.nvidia.com (10.127.8.10) by mail.nvidia.com (10.126.190.182) with Microsoft SMTP Server id 15.2.986.26 via Frontend Transport; Fri, 22 Jul 2022 19:04:25 -0700 From: Nicolin Chen To: , , , , , , , , , , , , , , , , , , , , , , , , , , , CC: , , , , , , , , Subject: [PATCH v4 10/10] vfio: Replace phys_pfn with pages for vfio_pin_pages() Date: Fri, 22 Jul 2022 19:02:56 -0700 Message-ID: <20220723020256.30081-11-nicolinc@nvidia.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220723020256.30081-1-nicolinc@nvidia.com> References: <20220723020256.30081-1-nicolinc@nvidia.com> MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 1e587d79-c7d4-4a67-16bd-08da6c4fac9e X-MS-TrafficTypeDiagnostic: BY5PR12MB3857:EE_ X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: BV4kXMvlWw6Cf8CaIE7KMSBEgZpwJsjD2O5Q4ZkXBZclmj47gF6GAmdMY0Pg/GE2YXG7j+J/qMwwGjVv3mukqQj7U7iRqU8PkEPuuN1FiElmoThIQ3Ietxc4ugoNCs3W5yY2v4NXpXX4aqoXSr5/GKlQYPC1iPyWWK6tb6gs4DLQx1hQh/OByxz0+b5UEoDYN8bmA4owJ3lduMYa0hjrGtiLNArWYGTbFVIzW4VmNCq0J/POuXCFgTWVy2r5YC/CjkIwrDvtWGiNaXVoImFJUbxUHrUWSDE7qDixEHufrhtDbIGIHYBV20n5W60AbJOR9MzCHzfacC+s3WSRDrAo4Os0Bxo5idjyzSzSxgRGJo+Kl4GSrY7u9I5VfEfaQUdx/QtPJkbnetMrJ5R1gBlH9WrHT0QJgxhftmcjFybrR1qYJvIu/8dzNU0NLOa/Sc1Rpld7ysDkrhNtOUEBFsk7YoAtfV4T53ijZWsaAxfdfyGhxn8+rXo6Q2UHoC6Xa/jxJclQ5tP3lgcRFI9DNpbF/Aq46pbPN5OuKytldRa0vbK4ohkuWGsxoVQXgoeGtjYJKrCsg4xO4RxyLq+f2vZ3Ks58G9ngPXop4AVREPg5sfgLMX3oRxO+vsmx/GqChmzywyGhbKxbhTvoXu8pUhxNw9tRXMBlREsrxtDtC2vyKOdJTjGIIYJfCSBHk6vnBzlgSs6fkEI+PPgJlfzZv5R4rJR0m7m/f/iVRR0a3CH4L5C2KGp+5A/3ZIHouIHUG2sKltXrAzAiKa6ZzVSwWe+tQz7qwmXP0dhBCUgOLzcgKxlKkd2MW2YRW4YRgRULHTW5MMsnXY9Z2I4LCZRQwk+BUzGxaFE9Xa9J930OCdULxzDw9LmRIj9p7sAnp6gEEjeOCEmJZpJNabFq3f2hQvzaQw== X-Forefront-Antispam-Report: CIP:12.22.5.235;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:InfoNoRecords;CAT:NONE;SFS:(13230016)(4636009)(136003)(39860400002)(376002)(346002)(396003)(46966006)(40470700004)(36840700001)(110136005)(356005)(7406005)(478600001)(81166007)(54906003)(26005)(336012)(36860700001)(82740400003)(6666004)(86362001)(47076005)(41300700001)(36756003)(7696005)(2616005)(316002)(7416002)(40480700001)(82310400005)(70586007)(921005)(30864003)(2906002)(4326008)(8676002)(186003)(70206006)(5660300002)(8936002)(83380400001)(40460700003)(1076003)(426003)(83996005)(36900700001)(2101003);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 23 Jul 2022 02:04:27.8260 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 1e587d79-c7d4-4a67-16bd-08da6c4fac9e X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[12.22.5.235];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: BN8NAM11FT016.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY5PR12MB3857 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Most of the callers of vfio_pin_pages() want "struct page *" and the low-level mm code to pin pages returns a list of "struct page *" too. So there's no gain in converting "struct page *" to PFN in between. Replace the output parameter "phys_pfn" list with a "pages" list, to simplify callers. This also allows us to replace the vfio_iommu_type1 implementation with a more efficient one. And drop the pfn_valid check in the gvt code, as there is no need to do such a check at a page-backed struct page pointer. For now, also update vfio_iommu_type1 to fit this new parameter too. Reviewed-by: Christoph Hellwig Reviewed-by: Kirti Wankhede Reviewed-by: Jason Gunthorpe Reviewed-by: Kevin Tian Acked-by: Eric Farman Tested-by: Terrence Xu Tested-by: Eric Farman Signed-off-by: Nicolin Chen --- .../driver-api/vfio-mediated-device.rst | 2 +- drivers/gpu/drm/i915/gvt/kvmgt.c | 19 ++++++------------- drivers/s390/cio/vfio_ccw_cp.c | 19 +++++++++---------- drivers/s390/crypto/vfio_ap_ops.c | 6 +++--- drivers/vfio/vfio.c | 8 ++++---- drivers/vfio/vfio.h | 2 +- drivers/vfio/vfio_iommu_type1.c | 19 +++++++++++-------- include/linux/vfio.h | 2 +- 8 files changed, 36 insertions(+), 41 deletions(-) diff --git a/Documentation/driver-api/vfio-mediated-device.rst b/Documentation/driver-api/vfio-mediated-device.rst index ea32a0f13ddb..ba5fefcdae1a 100644 --- a/Documentation/driver-api/vfio-mediated-device.rst +++ b/Documentation/driver-api/vfio-mediated-device.rst @@ -263,7 +263,7 @@ The following APIs are provided for translating user pfn to host pfn in a VFIO driver:: int vfio_pin_pages(struct vfio_device *device, dma_addr_t iova, - int npage, int prot, unsigned long *phys_pfn); + int npage, int prot, struct page **pages); void vfio_unpin_pages(struct vfio_device *device, dma_addr_t iova, int npage); diff --git a/drivers/gpu/drm/i915/gvt/kvmgt.c b/drivers/gpu/drm/i915/gvt/kvmgt.c index 8be75c282611..e3cd58946477 100644 --- a/drivers/gpu/drm/i915/gvt/kvmgt.c +++ b/drivers/gpu/drm/i915/gvt/kvmgt.c @@ -240,7 +240,7 @@ static int gvt_pin_guest_page(struct intel_vgpu *vgpu, unsigned long gfn, unsigned long size, struct page **page) { int total_pages = DIV_ROUND_UP(size, PAGE_SIZE); - unsigned long base_pfn = 0; + struct page *base_page = NULL; int npage; int ret; @@ -250,26 +250,19 @@ static int gvt_pin_guest_page(struct intel_vgpu *vgpu, unsigned long gfn, */ for (npage = 0; npage < total_pages; npage++) { dma_addr_t cur_iova = (gfn + npage) << PAGE_SHIFT; - unsigned long pfn; + struct page *cur_page; ret = vfio_pin_pages(&vgpu->vfio_device, cur_iova, 1, - IOMMU_READ | IOMMU_WRITE, &pfn); + IOMMU_READ | IOMMU_WRITE, &cur_page); if (ret != 1) { gvt_vgpu_err("vfio_pin_pages failed for iova %pad, ret %d\n", &cur_iova, ret); goto err; } - if (!pfn_valid(pfn)) { - gvt_vgpu_err("pfn 0x%lx is not mem backed\n", pfn); - npage++; - ret = -EFAULT; - goto err; - } - if (npage == 0) - base_pfn = pfn; - else if (base_pfn + npage != pfn) { + base_page = cur_page; + else if (base_page + npage != cur_page) { gvt_vgpu_err("The pages are not continuous\n"); ret = -EINVAL; npage++; @@ -277,7 +270,7 @@ static int gvt_pin_guest_page(struct intel_vgpu *vgpu, unsigned long gfn, } } - *page = pfn_to_page(base_pfn); + *page = base_page; return 0; err: gvt_unpin_guest_page(vgpu, gfn, npage * PAGE_SIZE); diff --git a/drivers/s390/cio/vfio_ccw_cp.c b/drivers/s390/cio/vfio_ccw_cp.c index cd4ec4f6d6ff..8963f452f963 100644 --- a/drivers/s390/cio/vfio_ccw_cp.c +++ b/drivers/s390/cio/vfio_ccw_cp.c @@ -22,8 +22,8 @@ struct page_array { /* Array that stores pages need to pin. */ dma_addr_t *pa_iova; - /* Array that receives PFNs of the pages pinned. */ - unsigned long *pa_pfn; + /* Array that receives the pinned pages. */ + struct page **pa_page; /* Number of pages pinned from @pa_iova. */ int pa_nr; }; @@ -68,19 +68,19 @@ static int page_array_alloc(struct page_array *pa, u64 iova, unsigned int len) return -EINVAL; pa->pa_iova = kcalloc(pa->pa_nr, - sizeof(*pa->pa_iova) + sizeof(*pa->pa_pfn), + sizeof(*pa->pa_iova) + sizeof(*pa->pa_page), GFP_KERNEL); if (unlikely(!pa->pa_iova)) { pa->pa_nr = 0; return -ENOMEM; } - pa->pa_pfn = (unsigned long *)&pa->pa_iova[pa->pa_nr]; + pa->pa_page = (struct page **)&pa->pa_iova[pa->pa_nr]; pa->pa_iova[0] = iova; - pa->pa_pfn[0] = -1ULL; + pa->pa_page[0] = NULL; for (i = 1; i < pa->pa_nr; i++) { pa->pa_iova[i] = pa->pa_iova[i - 1] + PAGE_SIZE; - pa->pa_pfn[i] = -1ULL; + pa->pa_page[i] = NULL; } return 0; @@ -144,7 +144,7 @@ static int page_array_pin(struct page_array *pa, struct vfio_device *vdev) ret = vfio_pin_pages(vdev, *first, npage, IOMMU_READ | IOMMU_WRITE, - &pa->pa_pfn[pinned]); + &pa->pa_page[pinned]); if (ret < 0) { goto err_out; } else if (ret > 0 && ret != npage) { @@ -195,7 +195,7 @@ static inline void page_array_idal_create_words(struct page_array *pa, */ for (i = 0; i < pa->pa_nr; i++) - idaws[i] = pa->pa_pfn[i] << PAGE_SHIFT; + idaws[i] = page_to_phys(pa->pa_page[i]); /* Adjust the first IDAW, since it may not start on a page boundary */ idaws[0] += pa->pa_iova[0] & (PAGE_SIZE - 1); @@ -246,8 +246,7 @@ static long copy_from_iova(struct vfio_device *vdev, void *to, u64 iova, l = n; for (i = 0; i < pa.pa_nr; i++) { - struct page *page = pfn_to_page(pa.pa_pfn[i]); - void *from = kmap_local_page(page); + void *from = kmap_local_page(pa.pa_page[i]); m = PAGE_SIZE; if (i == 0) { diff --git a/drivers/s390/crypto/vfio_ap_ops.c b/drivers/s390/crypto/vfio_ap_ops.c index d7c38c82f694..75cd92c291e3 100644 --- a/drivers/s390/crypto/vfio_ap_ops.c +++ b/drivers/s390/crypto/vfio_ap_ops.c @@ -234,9 +234,9 @@ static struct ap_queue_status vfio_ap_irq_enable(struct vfio_ap_queue *q, struct ap_qirq_ctrl aqic_gisa = {}; struct ap_queue_status status = {}; struct kvm_s390_gisa *gisa; + struct page *h_page; int nisc; struct kvm *kvm; - unsigned long h_pfn; phys_addr_t h_nib; dma_addr_t nib; int ret; @@ -251,7 +251,7 @@ static struct ap_queue_status vfio_ap_irq_enable(struct vfio_ap_queue *q, } ret = vfio_pin_pages(&q->matrix_mdev->vdev, nib, 1, - IOMMU_READ | IOMMU_WRITE, &h_pfn); + IOMMU_READ | IOMMU_WRITE, &h_page); switch (ret) { case 1: break; @@ -267,7 +267,7 @@ static struct ap_queue_status vfio_ap_irq_enable(struct vfio_ap_queue *q, kvm = q->matrix_mdev->kvm; gisa = kvm->arch.gisa_int.origin; - h_nib = (h_pfn << PAGE_SHIFT) | (nib & ~PAGE_MASK); + h_nib = page_to_phys(h_page) | (nib & ~PAGE_MASK); aqic_gisa.gisc = isc; nisc = kvm_s390_gisc_register(kvm, isc); diff --git a/drivers/vfio/vfio.c b/drivers/vfio/vfio.c index 606a20b605ba..8e23ca59ceed 100644 --- a/drivers/vfio/vfio.c +++ b/drivers/vfio/vfio.c @@ -1941,18 +1941,18 @@ EXPORT_SYMBOL(vfio_set_irqs_validate_and_prepare); * @npage [in] : count of pages to be pinned. This count should not * be greater than VFIO_PIN_PAGES_MAX_ENTRIES. * @prot [in] : protection flags - * @phys_pfn[out]: array of host PFNs + * @pages[out] : array of host pages * Return error or number of pages pinned. */ int vfio_pin_pages(struct vfio_device *device, dma_addr_t iova, - int npage, int prot, unsigned long *phys_pfn) + int npage, int prot, struct page **pages) { struct vfio_container *container; struct vfio_group *group = device->group; struct vfio_iommu_driver *driver; int ret; - if (!phys_pfn || !npage || !vfio_assert_device_open(device)) + if (!pages || !npage || !vfio_assert_device_open(device)) return -EINVAL; if (npage > VFIO_PIN_PAGES_MAX_ENTRIES) @@ -1967,7 +1967,7 @@ int vfio_pin_pages(struct vfio_device *device, dma_addr_t iova, if (likely(driver && driver->ops->pin_pages)) ret = driver->ops->pin_pages(container->iommu_data, group->iommu_group, iova, - npage, prot, phys_pfn); + npage, prot, pages); else ret = -ENOTTY; diff --git a/drivers/vfio/vfio.h b/drivers/vfio/vfio.h index e9767e13f00f..503bea6c843d 100644 --- a/drivers/vfio/vfio.h +++ b/drivers/vfio/vfio.h @@ -52,7 +52,7 @@ struct vfio_iommu_driver_ops { struct iommu_group *group, dma_addr_t user_iova, int npage, int prot, - unsigned long *phys_pfn); + struct page **pages); void (*unpin_pages)(void *iommu_data, dma_addr_t user_iova, int npage); void (*register_device)(void *iommu_data, diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c index e629e059118c..db516c90a977 100644 --- a/drivers/vfio/vfio_iommu_type1.c +++ b/drivers/vfio/vfio_iommu_type1.c @@ -831,7 +831,7 @@ static int vfio_iommu_type1_pin_pages(void *iommu_data, struct iommu_group *iommu_group, dma_addr_t user_iova, int npage, int prot, - unsigned long *phys_pfn) + struct page **pages) { struct vfio_iommu *iommu = iommu_data; struct vfio_iommu_group *group; @@ -841,7 +841,7 @@ static int vfio_iommu_type1_pin_pages(void *iommu_data, bool do_accounting; dma_addr_t iova; - if (!iommu || !phys_pfn) + if (!iommu || !pages) return -EINVAL; /* Supported for v2 version only */ @@ -880,6 +880,7 @@ static int vfio_iommu_type1_pin_pages(void *iommu_data, do_accounting = list_empty(&iommu->domain_list); for (i = 0; i < npage; i++) { + unsigned long phys_pfn; struct vfio_pfn *vpfn; iova = user_iova + PAGE_SIZE * i; @@ -896,23 +897,25 @@ static int vfio_iommu_type1_pin_pages(void *iommu_data, vpfn = vfio_iova_get_vfio_pfn(dma, iova); if (vpfn) { - phys_pfn[i] = vpfn->pfn; + pages[i] = pfn_to_page(vpfn->pfn); continue; } remote_vaddr = dma->vaddr + (iova - dma->iova); - ret = vfio_pin_page_external(dma, remote_vaddr, &phys_pfn[i], + ret = vfio_pin_page_external(dma, remote_vaddr, &phys_pfn, do_accounting); if (ret) goto pin_unwind; - ret = vfio_add_to_pfn_list(dma, iova, phys_pfn[i]); + ret = vfio_add_to_pfn_list(dma, iova, phys_pfn); if (ret) { - if (put_pfn(phys_pfn[i], dma->prot) && do_accounting) + if (put_pfn(phys_pfn, dma->prot) && do_accounting) vfio_lock_acct(dma, -1, true); goto pin_unwind; } + pages[i] = pfn_to_page(phys_pfn); + if (iommu->dirty_page_tracking) { unsigned long pgshift = __ffs(iommu->pgsize_bitmap); @@ -935,14 +938,14 @@ static int vfio_iommu_type1_pin_pages(void *iommu_data, goto pin_done; pin_unwind: - phys_pfn[i] = 0; + pages[i] = NULL; for (j = 0; j < i; j++) { dma_addr_t iova; iova = user_iova + PAGE_SIZE * j; dma = vfio_find_dma(iommu, iova, PAGE_SIZE); vfio_unpin_page_external(dma, iova, do_accounting); - phys_pfn[j] = 0; + pages[j] = NULL; } pin_done: mutex_unlock(&iommu->lock); diff --git a/include/linux/vfio.h b/include/linux/vfio.h index acefd663e63b..e05ddc6fe6a5 100644 --- a/include/linux/vfio.h +++ b/include/linux/vfio.h @@ -162,7 +162,7 @@ bool vfio_file_has_dev(struct file *file, struct vfio_device *device); #define VFIO_PIN_PAGES_MAX_ENTRIES (PAGE_SIZE/sizeof(unsigned long)) int vfio_pin_pages(struct vfio_device *device, dma_addr_t iova, - int npage, int prot, unsigned long *phys_pfn); + int npage, int prot, struct page **pages); void vfio_unpin_pages(struct vfio_device *device, dma_addr_t iova, int npage); int vfio_dma_rw(struct vfio_device *device, dma_addr_t iova, void *data, size_t len, bool write);