From patchwork Mon Feb 5 12:02:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Gowans, James" X-Patchwork-Id: 13545361 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7C416C48291 for ; Mon, 5 Feb 2024 12:06:01 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 11C5A6B007D; Mon, 5 Feb 2024 07:06:01 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 0A5536B00A5; Mon, 5 Feb 2024 07:06:01 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EAEC36B00A6; Mon, 5 Feb 2024 07:06:00 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id DC1396B00A4 for ; Mon, 5 Feb 2024 07:06:00 -0500 (EST) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id BF42340A33 for ; Mon, 5 Feb 2024 12:06:00 +0000 (UTC) X-FDA: 81757621680.04.156E627 Received: from smtp-fw-80008.amazon.com (smtp-fw-80008.amazon.com [99.78.197.219]) by imf27.hostedemail.com (Postfix) with ESMTP id 9DC7440009 for ; Mon, 5 Feb 2024 12:05:58 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=amazon.com header.s=amazon201209 header.b="jXlC/27v"; dmarc=pass (policy=quarantine) header.from=amazon.com; spf=pass (imf27.hostedemail.com: domain of "prvs=75897cb1d=jgowans@amazon.com" designates 99.78.197.219 as permitted sender) smtp.mailfrom="prvs=75897cb1d=jgowans@amazon.com" ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1707134758; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=2AUEdbvoftHMg2AkT8RS6u9G56Zx/z0Bmygk8dweA74=; b=scMqYu6yx8UwqCxl5Mxj7RPxGPlTot7GtW1GnsUUjIOxXcbIRtZvCSA3aD3gavYC42DJKS F8tTGdWkJBzUs6wlv+j7iafDX5vsMS4Ty5Elu+jMyx74xcctnyLRjdwENfjM0vJwsdygTW TuilfBKXdi90le0gJE8qJaC+fZX0Byo= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=amazon.com header.s=amazon201209 header.b="jXlC/27v"; dmarc=pass (policy=quarantine) header.from=amazon.com; spf=pass (imf27.hostedemail.com: domain of "prvs=75897cb1d=jgowans@amazon.com" designates 99.78.197.219 as permitted sender) smtp.mailfrom="prvs=75897cb1d=jgowans@amazon.com" ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1707134758; a=rsa-sha256; cv=none; b=hOauNB5poW6Kmr3nDibQiHqmuW0ZMbJjxkaE2A1ZqvTtKBd22POChjc+c2jvUlwXOKk8CV XE6a3He8kocZPaHQXGz9fDu0WvYnjp/fWkQpZJulGp/RA1oKKIm7nfRzV6mc25bnk+djfu n/eITryQeMF9Fuj3Z6l6DiMk7uxg52M= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1707134758; x=1738670758; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=2AUEdbvoftHMg2AkT8RS6u9G56Zx/z0Bmygk8dweA74=; b=jXlC/27vV4NXEiaotGKEkf/n78ZOBFYDligP/bum6PA2zDvcpN1B6Gk7 KupFHQz0+vHvkLuJ+muCE4+RDJ4x1MWyUOWN6uIEOHn5apMhCA7tWruU5 5ZB1698j5/qYUokPgobd9+X645SEe8iO2P0kvK1o0UDh1sOYhf53WCarE U=; X-IronPort-AV: E=Sophos;i="6.05,245,1701129600"; d="scan'208";a="63755948" Received: from pdx4-co-svc-p1-lb2-vlan3.amazon.com (HELO smtpout.prod.us-west-2.prod.farcaster.email.amazon.dev) ([10.25.36.214]) by smtp-border-fw-80008.pdx80.corp.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Feb 2024 12:05:57 +0000 Received: from EX19MTAEUB001.ant.amazon.com [10.0.17.79:13316] by smtpin.naws.eu-west-1.prod.farcaster.email.amazon.dev [10.0.45.85:2525] with esmtp (Farcaster) id 596cc4dd-1066-4e32-b40f-00d6ab21896d; Mon, 5 Feb 2024 12:05:56 +0000 (UTC) X-Farcaster-Flow-ID: 596cc4dd-1066-4e32-b40f-00d6ab21896d Received: from EX19D014EUC004.ant.amazon.com (10.252.51.182) by EX19MTAEUB001.ant.amazon.com (10.252.51.26) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.40; Mon, 5 Feb 2024 12:05:56 +0000 Received: from dev-dsk-jgowans-1a-a3faec1f.eu-west-1.amazon.com (172.19.112.191) by EX19D014EUC004.ant.amazon.com (10.252.51.182) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.40; Mon, 5 Feb 2024 12:05:49 +0000 From: James Gowans To: CC: Eric Biederman , , "Joerg Roedel" , Will Deacon , , Alexander Viro , "Christian Brauner" , , Paolo Bonzini , Sean Christopherson , , Andrew Morton , , Alexander Graf , David Woodhouse , "Jan H . Schoenherr" , Usama Arif , Anthony Yznaga , Stanislav Kinsburskii , , , Subject: [RFC 16/18] vfio: support not mapping IOMMU pgtables on live-update Date: Mon, 5 Feb 2024 12:02:01 +0000 Message-ID: <20240205120203.60312-17-jgowans@amazon.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240205120203.60312-1-jgowans@amazon.com> References: <20240205120203.60312-1-jgowans@amazon.com> MIME-Version: 1.0 X-Originating-IP: [172.19.112.191] X-ClientProxiedBy: EX19D042UWB002.ant.amazon.com (10.13.139.175) To EX19D014EUC004.ant.amazon.com (10.252.51.182) X-Rspamd-Queue-Id: 9DC7440009 X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: 4pu8cp1f8c8nw814wonpedixscqipwi6 X-HE-Tag: 1707134758-776707 X-HE-Meta: U2FsdGVkX1/HzIGwvVuWu8shUn4eMNQpgXdY9Ddcs4VujI5yGOwBbgDhje/hKe9j08dhJk6EDbKJ/NaD4UhancI4Kf7+oC0CXNaJhD6oHjh+eNCszejsf6cl4PoBFz1QFQPHkreWPleC5TJPdEjwJnyNuJ+in/7Kn5T90MR3FraGMlaSHV4xyNAOIdSi3mvH9iZ/V80M0YENlE96u7xO9fHMk2Hos+bUiMNIa+zYt5piq+X1qlu0hz3L2AuuktjGcH1KdT6hnwm3ztsV2Bg6uIvHSw/YuAaQbRqj4u5DOP5MQNc/hhvSQFNwOp9lDTXImCBNEx+DUAIgYufz45o6PE/d7sPPcxf9yWjZzSUNiwtj5TRFCZjwzkoWud1z7Rpi7ePZEQu/srOsxO/lbCY+GKy/cH/wInBTPq25bBOfBYBoZNfm8jbgkcLGZ3tFC3q+EYcbrKAYO030AaMXARqd9CTvGO8yEXvQ8Op4F+1D7/xbkAuRxvSpky7z0tlpitcPe/Q+ftd8EIgATUtkIck0/i/BBByDzp0/TrW17QOFmNFfcCgMk2/CbCUJbu79ig/RTOgnrsCzsJtcKG8z5KJI3W/qTZpTaH8s9repEbBLRQED3F5wK6wOpYvk7zD27LJNOeNNKX0nv8xVcOXh2nfaQJh8hY4N9Gc/P5WRAdTTBTBAwp9k8ocgwPFnXb2APDlHDfXlqh0vtl4RS3JVqeAxwKgc1PtyOoiGS92diL10Ue4TKPGz1Dn00/uXwpdQ5WNUNnwhYTR8W7+6gzAsLwdUZ0inwf1N5FaRaq6OReKK7bJZ/ayKfxu2NZsFZri5zcIqODTx49dSeKvHyRnvIm4nhPnkNCBsi02w80XTVBHKoEOz0nqoT3odOB8uZg2zvLYOjINd/L3NaaN/h86JOMwh5QWsw8uJCsXeLVoRgsUBilK9QTbvqyG7rCSlHj9D/JXGhR7SjAg5rtbAna5xjwQ ueM1PD0L EY6HzFZUcvu4jgo7gK7wqW0onF682QXG7kS7jYthuUTiCE7H01uPQooE3HmZTZ5PaESkZAzI2RNIeOcauuR/ZE8kxRDS51k8STZAzm0fHn7J7QKyH6cr+cE9zV0fMFMq4smeOsm7UgZofkZHlZf7t8xKsIHW+hhFshfKpZ88nzpMZetfszmHGdK7yqYzAccMauehp4+Koi3zYIHmaI5Qacq58SvEWIko36dK8HVOZf6WiLNoucg+BUS4W3AYLmfKlw5W0/IjO2tPnqpijYiSsx691PhtyUafaRe3lgoQRot3xpyxZJZ2CB4Vc7HqoQFpRLca0KXOOX2YqDml9E9uuXkDNp+HJ4SdRNr7If0Lu8geTnV+Q8rpTeX69Lyeroz9jgCgf0ldzg4/jpAv4L+5tyKMZo1GWmxDt6cInNtsm4iaCq765eP8mK5YuZIsAvwMnmFP2KOJcTLhikMF0JLSBzgMZGHnJYdgqAWm6QR64ZjgTZIvL1kdYeB37raE7HBnwz5kg16tFBNQgE2RvEXb8TmCegbiMGEsueBemYKahEu9mjlg= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: When restoring VMs after live update kexec, the IOVAs for the guest VM are already present in the persisted page tables. It is unnecessary to clobber the existing pgtable entries and it may introduce races if pgtable modifications happen concurrently with DMA. Provide a new VFIO MAP_DMA flag which userspace can supply to inform VFIO that the IOVAs are already mapped. In this case VFIO will skip over the call to the IOMMU driver to do the mapping. VFIO still needs the MAP_DMA ioctl to set up its internal data structures about the mapping. It would probably be better to move the persistence one layer up and persist the VFIO container in pkernfs. That way the whole container could be picked up and re-used without needing to do any MAP_DMA ioctls after kexec. --- drivers/vfio/vfio_iommu_type1.c | 24 +++++++++++++----------- include/uapi/linux/vfio.h | 1 + 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c index b36edfc5c9ef..dc2682fbda2e 100644 --- a/drivers/vfio/vfio_iommu_type1.c +++ b/drivers/vfio/vfio_iommu_type1.c @@ -1456,7 +1456,7 @@ static int vfio_iommu_map(struct vfio_iommu *iommu, dma_addr_t iova, } static int vfio_pin_map_dma(struct vfio_iommu *iommu, struct vfio_dma *dma, - size_t map_size) + size_t map_size, unsigned int flags) { dma_addr_t iova = dma->iova; unsigned long vaddr = dma->vaddr; @@ -1479,14 +1479,16 @@ static int vfio_pin_map_dma(struct vfio_iommu *iommu, struct vfio_dma *dma, break; } - /* Map it! */ - ret = vfio_iommu_map(iommu, iova + dma->size, pfn, npage, - dma->prot); - if (ret) { - vfio_unpin_pages_remote(dma, iova + dma->size, pfn, - npage, true); - vfio_batch_unpin(&batch, dma); - break; + if (!(flags & VFIO_DMA_MAP_FLAG_LIVE_UPDATE)) { + /* Map it! */ + ret = vfio_iommu_map(iommu, iova + dma->size, pfn, npage, + dma->prot); + if (ret) { + vfio_unpin_pages_remote(dma, iova + dma->size, pfn, + npage, true); + vfio_batch_unpin(&batch, dma); + break; + } } size -= npage << PAGE_SHIFT; @@ -1662,7 +1664,7 @@ static int vfio_dma_do_map(struct vfio_iommu *iommu, if (list_empty(&iommu->domain_list)) dma->size = size; else - ret = vfio_pin_map_dma(iommu, dma, size); + ret = vfio_pin_map_dma(iommu, dma, size, map->flags); if (!ret && iommu->dirty_page_tracking) { ret = vfio_dma_bitmap_alloc(dma, pgsize); @@ -2836,7 +2838,7 @@ static int vfio_iommu_type1_map_dma(struct vfio_iommu *iommu, struct vfio_iommu_type1_dma_map map; unsigned long minsz; uint32_t mask = VFIO_DMA_MAP_FLAG_READ | VFIO_DMA_MAP_FLAG_WRITE | - VFIO_DMA_MAP_FLAG_VADDR; + VFIO_DMA_MAP_FLAG_VADDR | VFIO_DMA_MAP_FLAG_LIVE_UPDATE; minsz = offsetofend(struct vfio_iommu_type1_dma_map, size); diff --git a/include/uapi/linux/vfio.h b/include/uapi/linux/vfio.h index fa9676bb4b26..d04d28e52110 100644 --- a/include/uapi/linux/vfio.h +++ b/include/uapi/linux/vfio.h @@ -1536,6 +1536,7 @@ struct vfio_iommu_type1_dma_map { #define VFIO_DMA_MAP_FLAG_READ (1 << 0) /* readable from device */ #define VFIO_DMA_MAP_FLAG_WRITE (1 << 1) /* writable from device */ #define VFIO_DMA_MAP_FLAG_VADDR (1 << 2) +#define VFIO_DMA_MAP_FLAG_LIVE_UPDATE (1 << 3) /* IOVAs already mapped in IOMMU before LU */ __u64 vaddr; /* Process virtual address */ __u64 iova; /* IO virtual address */ __u64 size; /* Size of mapping (bytes) */