From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.214.169 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796170047; b=Ulhw4fj+QslC/pev08F6MJ1z4/LMfs5I+QCIXinOvzZgS9S0iQmD7VfV4PM0O10KLd9a f3agdw0Qbq0hMCFWHfiWOH/D3/zKk0beX2itYz2f+rqKWI+zSdEYGWx3H/iWvIA7ZfdM1 e4XhqvIJA3hc5ktov+ZYJFDr8Tu5SVWHrZ/BAwrQxYjNHCZKk8nLRKAWW4zWnioL3V8kM hOxEczabMuBRpcXu0mtf6XWy+wvYeEnn1ciYgIehfHY0//vmJlAk81Qce8OvZeUHr+9B0 2HZnqPbeOWQoSqraHWR9HmUFFXPBVRjM0qW27WPna+hsMtmRxvQ6Cm5GjoMcxLE2+aA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796170047; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Gm-Gg:X-Received:Received:Date:From:To:Cc: Subject:Message-ID:References:MIME-Version:Content-Type: Content-Disposition:In-Reply-To; bh=ss+yWtrgCWXbkyUcbbwuZOlhujF+DPDXVJ4JQts2c4c=; b=IQPlXjtUi3kbvnt8Ny8nJlMU5vyUbWxtcN+mgORKWpLti0hQ9pcaRiPZzp1xAMbjvZmz MZW9K4uPDu9w87ZFuWdCDyxqJwIefoah41SVIcoXZE1oTRgYXRb3lCUSCxHfaezEMJvF6 FxAxt3mCZ9NVJion25wbTgo6bbX3UuhEEH6X+fjgJxGVnTAqMWCcAbMWutVL0pAlkvQqF JY1W78oZQx6JllzRgmyOLhPcFZ4ID6htl2VREYd6m1EhfS0uY8l26I3vhPnggJz9bL8x+ jK8i5D/wO8Uzhvj1dMGahvZU178strWU/7VLWQRCslexWkU3rlS0SswgCnGDrQyQbgQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b="cg/P/Ae3"; arc=none smtp.remote-ip=209.85.214.169 Received: from mail-pl1-f169.google.com (mail-pl1-f169.google.com [209.85.214.169]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 536F9606A31A for ; Wed, 2 Dec 2026 00:07:27 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 536F9606A31A Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=cg/P/Ae3 Received: by mail-pl1-f169.google.com with SMTP id d9443c01a7336-2f0e8bbc7a4so3605ad.1 for ; Tue, 01 Dec 2026 16:07:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796170046; x=1796774846; darn=vger.kernel.org; h=in-reply-to:content-disposition:content-type:mime-version :references:message-id:subject:cc:to:from:date:from:to:cc:subject :date:message-id:reply-to:content-type; bh=ss+yWtrgCWXbkyUcbbwuZOlhujF+DPDXVJ4JQts2c4c=; b=cg/P/Ae352j3WISbYNaRD5rhkfB7v0+8gCp4jSaPavO+WwUCgixeOLwch706vhfihU ednY9e7ZQ1OVy3OYrv8uJ7H7wAWHHA8UzFma5MVjJseDipLYloEM144bs3U3fpUmnevG y75t9cIHR1Fp9IejZq0yIHqlkxkcyv51WBTpy1ghfJpkQoA0YFxj/hz8MwASYIWUEVxP 1JL5lLiJYuC0L9WfY78S++9DZY6yuIWn4qYVMhBNDmF/vJY6/W+5HaWmTxlYVHDHDqLJ rhLmw/wuYfX0wfHYcyr+9tQ9VqEvsSOcUVHXOXB0+gbyabBLpwoo4XKpiqjbzNJABX1n LQ3Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796170046; x=1796774846; h=in-reply-to:content-disposition:content-type:mime-version :references:message-id:subject:cc:to:from:date:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=ss+yWtrgCWXbkyUcbbwuZOlhujF+DPDXVJ4JQts2c4c=; b=YnYK9CVtQVaL/j9/tLbMeE80MFnb2b+hSopHs4pBJMUlqx3C7XFvyGTjQY/jFFRizO scvoTvhoKqydFWSiLBeugzGlf3CJVP2TpjbGzkNOqd1MC6cdJPyT763k/+XLAHoFLHtO 1oqOIth91R9Puyp/803heS26GLbXaXxc1O33gWbMftX+JBkdzhgtICUV2UmXyo9OhQve cBJkArvqFXkhXqyCIwSycrUITnKHcrHd04YmcfppLIhhaLZ+BJc7IlZCaCzzx4QYc94E eKX2OcwRtnF3lY+7jfMK7dS5p/q4jheYd/EUQWuFFBiU4t37Hv/WkH2PCX3ilAH/UP48 394w== X-Gm-Message-State: APuU4Z9+1qi4r4kXHAvXAxPN7QB/Pt4Ua7O+VsJb1vamv1ufalcbT/YP 9NKiAmQtD78e+84uqUwVmH0pQB4IeR20KVqfS+c/PA6xzScQBxtZ6sqSFvGnejMxxQ== X-Gm-Gg: AZFdTZeywlIAQWjCKBwi2oEx81wykSJ1gnV7jZy0VkK/FzjztNXyl561WVNQrFsdUDu XVUPKuysZnn0AD8ju2c4V8GyQulzSlmqozDzE1mo6vjfZiA1DsUQ6q1xw+bnqlK2o1lRYP4wya6 /B7omUqOgPlmMd2kjQD9STPZKgtCqInkugKHoLp4Yu7ibrZ1iKCgpWJeNC0fuwfFG+usgEIhNMX IjLUhzcGHznfJkdG5MJ1TEqk2LkZQuDTolZMzq1vkUvjdA8afL8nNwmNAmrePZ0Zryr13i1KkaL FpIJ4X067vwBS8CY8vy7NczTCBNWPIZhU/QZnS6s5kmTBde+xJkWf1xdJoslTCb1XxzCamZVQir w0xB0u5w8whQBFehb6CjAI4epQPg6X9my5F73/Xnn1slDyzkEZnU5m3Vyhzgq4c7zds75hRd6Bw DIVDDgDap/WfP7+q+XbXMk5TcyuvzOu2IPdh09P19rNXgvJY6b+SNMl/fnhP9O1ZyEhQSzxdS1W mOWfX320yWxPhwdu54kj+y+2IAJ46YDEZvog12WgF0PlB24+X42y/66sNt5j9G3CimY5ISYH7gQ yRulQ3N2oy9ag7pBvxMfg1DUHStpoXUUyO1e74ECDWDn80INQNyQDBL2ETLYwtHebYM= X-Received: by 2002:a17:902:ccce:b0:2ea:4a1:e8a4 with SMTP id d9443c01a7336-2f179ef551cmr355955ad.13.1796170045890; Tue, 01 Dec 2026 16:07:25 -0800 (PST) Received: from google.com (176.13.105.34.bc.googleusercontent.com. [34.105.13.176]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2f16e685d36sm11946845ad.48.2026.12.01.16.07.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Dec 2026 16:07:25 -0800 (PST) Date: Tue, 1 Dec 2026 16:07:21 -0800 From: Vipin Sharma To: Aaron Lewis Cc: kvm@vger.kernel.org, alex@shazbot.org, dmatlack@google.com, jgg@nvidia.com Subject: Re: [PATCH 2/3] vfio/type1: Track GUP vs PFNMAP in DMA map/unmap Message-ID: <20261201234244.GC2554154.vipinsh@google.com> References: <20261119185659.889654-1-aaronlewis@google.com> <20261119185659.889654-3-aaronlewis@google.com> X-Mailing-List: kvm@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20261119185659.889654-3-aaronlewis@google.com> On Thu, Nov 19, 2026 at 06:56:58PM +0000, Aaron Lewis wrote: > Both vfio_pin_pages_remote() and vfio_unpin_pages_remote() use > PageReserved() as a heuristic to infer whether a page was PFNMAP'd or > GUP-pinned in vaddr_get_pfns(). That heuristic breaks down when the same > reserved struct pages can appear on both paths: > > - DevDax pages mapped from /dev/dax* are reserved and GUP-pinned. > - Those same pages mapped from /dev/mem (PFNMAP) are also reserved, > but they are *not* GUP-pinned. > > Because of this, when unpinning the DMA region, vfio_unpin_pages_remote() > doesn't know whether those reserved pages were GUP-pinned and skips > unpinning them altogether, *leaking page pins and refcounts*. > > Fix this by replacing has_rsvd and has_non_rsvd in struct vfio_dma with > has_pfnmap and has_gup_pinned, tracking which path vaddr_get_pfns() > actually used, and clearing them when a DMA range is unpinned. Since > PFNMAP pages bypass struct vfio_batch, this also fixes locked_vm > accounting and batch error-unwind unpinning for reserved GUP-pinned > pages. This commit has a user visible changes as devdax will now be charged to RLIMIT_MEMLOCK, we should call it out explicitly in this commit message. will get > > This isn't an exhaustive fix because mixed mappings are still allowed. > For example, if a single DMA region contains both GUP-pinned and > PFNMAP pages, vfio_unpin_pages_remote() can't tell them apart. Handle > this by preserving the existing put_pfn() fallback, and add > a comment explaining the situation. We still have the bug with the mixed mapping. I was thinking about two options: 1. We reject DMA mapping (vfio_dma) which have mixed mapping. Allow only one type PFNMAP or GUP but not both. I am not sure how big of user visible impact it will cause and also not sure if it is very common to have mixed mappings. 2. Alternatively, we can also store pfnmap range in vfio_dma. Basically, with what half of your patch 2 does (separating PFNMAP and GUP processing in vfio_pin_pages_remote) and then storing a pfn mapped ranges into an interval tree. During unmap, we can unpin iova ranges outside of those pfns. This approach will not leak any pins and gup pages will be unpinnned even in mixed case. https://github.com/shvipin/linux/tree/vfio/type1_pfnmap_ranges Specifically, in my tree the commit 78fe78498c30 ("vfio/type1: Track PFNMAP ranges to fix pin leaks in DMA unmap") I am more inclined with approach 2, as that handles almost all of the cases. It is not tested but overall it gives the direction. WDYT? > > Lastly, clear the new flags once a vfio_dma is fully unpinned. A vfio_dma > can outlive its pins and be pinned again later, after > vfio_iommu_unmap_unpin_reaccount() or a failed vfio_iommu_replay(), by > which time the user may have remapped the range. A stale has_rsvd only > selected the slower unpin path, but a stale has_pfnmap would send a > mapping that is now entirely GUP-pinned down the wrong unpin path. > > Fixes: 3565fce3a659 ("mm, x86: get_user_pages() for dax mappings") Is this correct fixes tag? > Signed-off-by: Aaron Lewis > --- > drivers/vfio/vfio_iommu_type1.c | 103 ++++++++++++++++++-------------- > 1 file changed, 58 insertions(+), 45 deletions(-) > > diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c > index 7bad0508dd21..ee40ba942fd5 100644 > --- a/drivers/vfio/vfio_iommu_type1.c > +++ b/drivers/vfio/vfio_iommu_type1.c > @@ -93,8 +93,8 @@ struct vfio_dma { > bool iommu_mapped; > bool lock_cap; /* capable(CAP_IPC_LOCK) */ > bool vaddr_invalid; > - bool has_rsvd; /* has 1 or more rsvd pfns */ > - bool has_non_rsvd; /* has 1 or more !rsvd pfns */ > + bool has_pfnmap; /* has 1 or more pfnmap pfns */ > + bool has_gup_pinned; /* has 1 or more GUP-pinned pfns */ The patch contains a rename (fields and put_valid_unreserved_pfns()), the fix, and the flag-clearing. I think splitting rename to a no-function-change patch will make it easier to review. Same with clearing flag?