From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.128.43 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796050757; b=J6Aa6aTIbATP/XGMlxAuUdNimeyK0tN14e0YZ70wxYrLMCI7aaKITORDoEKjKzOvbQhW 9i6Giro9a18o0XvNsycjuLtRcPNR/ig2T6HBqId6bgu1IPlt4+5VWOUg/m7f+GVmIgQ/a 7kz+rsPlC7c/od1Z0bX35pG7q1rzqRVKTdPUzVR0J2pRKpiif7eH4Qpq3GZZY7UYdv3HC DDM4nE6Ekk3r3YBwFU8RxXC18EvPwJ8GVjn/0zaJTy8OVXCcgEJ89tEl6ZwynwTkbstmc PVgEa66b0bt0/BNzVwt2yRHHqZS3YscqTYUKm8xEwk/v4q5geK/FGkO6BuS0DFYlHbQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796050757; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted: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=OM7n37QaLEMMXqk0FmnIiXvJ5iDT8/jvqXeb7lsqPc0=; b=POpnNRpq44atisIJzy98B7oLodQ4FXt/kwmlqoPh+1DsOpSyIenHeczaHZ4BhAmh/Ogb J5JTO1M+rRZLPU3re1+Z4ez+q7vL58my1M6fdDYpq00oQfczaQxJgyHsmb6h3sJZaxZKY 6gdGmEoZeKaLGIYeHO2y61zTEDlGxec25IlRlnYU3PorCCMslpmmvH6p1mAsY0MXlG/+8 Z6zARH63k9Of1uOJqqf4/QXR4V8ni9wD68PbNtwLij36t6rEZGicEIG3ZlCy4QyGRSYHJ hteBZIoRwfYpIWf3vZ69Ibc4xRBlmiOUVGnsL7IJgDa/1jsBwX/zPmQpxveMWwY+hAQ== 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=XEBX5Uyw; arc=none smtp.remote-ip=209.85.128.43 Received: from mail-wm1-f43.google.com (mail-wm1-f43.google.com [209.85.128.43]) (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 95F9C606A31C for ; Mon, 30 Nov 2026 14:59:16 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 95F9C606A31C 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=XEBX5Uyw Received: by mail-wm1-f43.google.com with SMTP id 5b1f17b1804b1-4bb6860f867so489405e9.1 for ; Mon, 30 Nov 2026 06:59:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796050755; x=1796655555; darn=lists.linux.dev; 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=OM7n37QaLEMMXqk0FmnIiXvJ5iDT8/jvqXeb7lsqPc0=; b=XEBX5Uyw19JTt5NCoRfvgP9GuBn728Uyo7PenBiMXt/coSdqZDKJm4CbilvvkshYcH qAhwNjb031ArOOKi653yBe1+242T8Mc1syh5NIBJ2S7uwut2SSaRw+CTclpnb9/0Vu/a 8QrH/yKUhpxajp5HmGIYU7yQAO9uvzaQYwCNLlVq/Yh7RHs6LCOcqWIIFkWE0R6NyIyp R6z26xm+ubqmgwNcKyrBF2odQrNVy/99npzGOW4dK8ep5fJi88iyBEwGuI5Sukfv8dPD wG3jKHlmDAby9pGkJLAoMHeysjUvwJ2j63VGaHyAK5pHv/ETr3RzbevJQFYr0IsvtH6Q Szlg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796050755; x=1796655555; 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=OM7n37QaLEMMXqk0FmnIiXvJ5iDT8/jvqXeb7lsqPc0=; b=XQ9EQFmygHQAgtKu3LGIdW3wOEm8FUF+H6frUZwUqZcRoIe0TaSWzfx3UiZXi7wnon duBT57ndxPepZS4HEOHdpHQM72HJwhdqL6Ox5jxBMfHxkqOA5u76pKVhcIe3Z3n9lCQQ nWq4r2Be3DBXE9jUCeCEXycoJPIijCzrcuRQy/84aor/42QiLFLU6uNof2yYx9J7VVQo fJRWRQvjOxcsRxb18NaUG7/9cV3WDqh5mU8mtJftJ5VSHi6cwhN9K8qXV7tHJ7zgXau0 uKSBlk2d7DbQh84/lclxBARfchhlu18uKglnNEVkpKgGL2MRw66dG68mJoI/Cm/yNGym Do7A== X-Forwarded-Encrypted: i=1; AL+hErEnPawpmI4gw+CquH5NVpHkmfF6wPr5CQezkXV8ja84hwAvYnH/g8Ckoi4jsOG6JOI4PQIoQA==@lists.linux.dev X-Gm-Message-State: APuU4Z+nxdZll9EAMoTOvugl5I+jMwM1l/PQeuxIggR8UcakZXr7uhM/ 2Moa+PLjm5eZlppDZm3Gdc7A4YgyaFSYFfkx3Ds0pc8RkVaLCqaCC12R+udxDj4coYnkKLox5sC uWCxVJw== X-Gm-Gg: AZFdTZdm1N/8wOUFjgDDnMObjYgCOQqWgh2og3LgxqArYR62jveN4KcNZj3fPVfkLbk 3bhnAIayd2cFFbYEBY1yG9okBc3qVw5Xr3pI6RG4rVZhBGkvAuiW4LLjaSB9YKsj2RfGm+Vqb41 owTipQ3ESkzAurtZwqkvNZWjvYGd31cc17oxkIeLraFT8YdSweTlF6Dp3VPHh3PxFMQOw1fiemd d+EdBZFUS2XBOOTDVc7kOsuYZc2PpN3D9c+fSOXffxwzs7LvYal8ZMs7PBgT8XB+AviGWDsWbFg mdFynAQujPbfdydI0N0aBeuFD6NioO7f9UNsftYhwwJRa0xy9++dXz+zL0B3UTAAn2aMUMYtep7 cIVdb84wpNcp6NF0KpnjFb6plJrNlzVB32K3bgRLSszf2v8HXTV1UuRvNxLmUvGnQeAzWEdDhKI tVXwcmx0rBpDKoMPva5Tbk4voSR0pllAzqPUQJXT/nlD8XFRmXhVLLu6iw/RRkgGnWUhDaIXuMY drsE3Ndnz282h7PYK/bniYJF6BiIEfK6JoyNoM8EHDdQFgbnhKil2xkJRfUVUKPKmhM3hkxkpoy 4Cl8gWpuTwyvi+8B4gYsk8K2LK6mQor3sQ5Z4BU/iPot6y2kTg3jdh+OwMlshW3VgXibmSM= X-Received: by 2002:a05:600c:e549:10b0:4bb:7ad8:9a81 with SMTP id 5b1f17b1804b1-4bb91622a3emr3807475e9.7.1796050754535; Mon, 30 Nov 2026 06:59:14 -0800 (PST) Received: from google.com (250.192.189.35.bc.googleusercontent.com. [35.189.192.250]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4ab6c61e53asm71426597f8f.59.2026.11.30.06.59.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Nov 2026 06:59:13 -0800 (PST) Date: Mon, 30 Nov 2026 14:59:10 +0000 From: Mostafa Saleh To: Fred Griffoul Cc: Joerg Roedel , Will Deacon , Jason Gunthorpe , Kevin Tian , Robin Murphy , Shuah Khan , Guanghui Feng , iommu@lists.linux.dev, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, nh-open-source@amazon.com Subject: Re: [PATCH 0/3] iommupt: Unmap and return the PFNs in one walk Message-ID: References: <20261111185931.94882-1-griffoul@gmail.com> X-Mailing-List: iommu@lists.linux.dev 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: <20261111185931.94882-1-griffoul@gmail.com> On Wed, Nov 11, 2026 at 06:59:28PM +0000, Fred Griffoul wrote: > From: Fred Griffoul > > When iommufd unmaps an area, it must unpin the user pages, but it keeps > no list of their PFNs. It reads each one back from the IOMMU domain > with iommu_iova_to_phys(), one CPU page at a time, then walks the page > table again in iommu_unmap(). With 4 KiB pages, a 1 GiB IOPTE costs > 262144 lookups before the unmap starts. > > This series lets the iommupt unmap walk report the physical range of > each IOPTE as it removes it, so iommufd unmaps and collects the PFNs > in one walk. The walk also steps over holes, where iommu_unmap() stops > at the first one. I looked into this a long time ago, in a different context for pKVM, where we need to recover the physical addresses for unmapped pages for the sake of tracking also. There are similar attempts, with callback backs (at io-pgtable level) - https://lore.kernel.org/kvmarm/20241212180423.1578358-41-smostafa@google.com/ - https://android.googlesource.com/kernel/common/+/4d1f47f8079859aa5859c6505cb09739154e0287%5E%21/ I wanted to post that for VFIO also, but I was not sure of the value, so it would be interesting if you have run any benchmarks? The main challenges I faced with this approach: - io-pgtable-arm clears table entries without descending, if covered by the unmap range which requires extra walks, I see that iommupt already walks the whole range anyway which doesn't require extra changes. - Ordering with TLB invalidation, with pKVM, the page tracking has to be strictly done after the PTE was cleared and TLBIs are issued, it would clear the PTE first and then record and batch physical addresses in the callback. This series will keep the PTE live but similarly uses batching in the callback, which should be ok. There are a few Sashiko comments in: https://sashiko.dev/#/patchset/20261111185931.94882-1-griffoul%40gmail.com I believe they are true and need to be fixed, one of those is already existing in the tree in iommu-debug-pagealloc, I will send a patch for that. One more thing, is stepping over holes, does IOMMUFD exercise that? Thanks, Mostafa > > Patch 1 adds iommu_unmap_sparse(), patch 2 uses it in > iopt_area_unpin_domain(), and patch 3 adds a selftest for the areas > that keep the per-page read-back. > > An earlier series [1] proposed iova_to_phys_length() so that iommufd > can read the PFNs back one IOPTE at a time instead of one page at a > time. The unmap is still a second walk there. This series collects > the PFNs in the unmap walk itself. > > The series applies to iommu/next. I wrote parts of the code and the > changelogs with Kiro, an AI coding assistant using Claude models, and > used it to run the tests. I reviewed and tested all of it. > > [1] https://lore.kernel.org/linux-iommu/20260603151804.1963871-1-guanghuifeng@linux.alibaba.com/ > > Suggested-by: Jason Gunthorpe > > Fred Griffoul (3): > iommupt: Add iommu_unmap_sparse() > iommufd: Use iommu_unmap_sparse() to unpin domain pages > iommufd/selftest: Unmap areas that start inside a CPU page > > drivers/iommu/generic_pt/iommu_pt.h | 61 ++++++++- > drivers/iommu/generic_pt/kunit_iommu_pt.h | 144 ++++++++++++++++++++++ > drivers/iommu/iommu-debug-pagealloc.c | 13 ++ > drivers/iommu/iommu-priv.h | 24 ++++ > drivers/iommu/iommu.c | 76 ++++++++++++ > drivers/iommu/iommufd/pages.c | 98 ++++++++++++++- > include/linux/generic_pt/iommu.h | 24 ++++ > include/linux/iommu.h | 22 ++++ > tools/testing/selftests/iommu/iommufd.c | 30 +++++ > 9 files changed, 486 insertions(+), 6 deletions(-) > > > base-commit: 2cf4be57b301ed2d7af82e70cff5dfd96894d8de > -- > 2.47.3 >