From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.128.45 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796050757; b=WPAPQ5z5IKF5XdTmLWQcEB8ucBcEsXqzIN/wHkxwg1rYpbCdNMYgwLF9owY2fIw3Xi0G tsG9cEzYVPWrnHQ42YTrXzrDXQSzqnEaJ7Kuq6RJFqLvYZ1ECzE1xJvySTn/Bc9syci5V 4yNsVAT4IWkT9CCPJ/t3VCJJ469xZWrqOzy6/8zNoTu/kT+hGijgqgUoZIdSIxJJDeZBH SEJhkdZ2WHGsSTUkzaCuo4hdGoi4k6vI7lyb+rSliJET1P6sUpBltEsmUS1Jr3Lpda9D9 D1Yv467mtpVJirxHw6yhEucqUx5pdPeSy15Y+G7931gVDxNg+9Cthm9TzefKpLrz4hw== 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=A3OpsR8T+j2uoWK9hvKuMhVS8HvbO0jfP/9UgXhnD9+hqSbVik12K65KOUncbbSkdRYZ IQQrIZmTNqnimuiAE4mED1mbSd2ASJGjWScVTegDkOkGtwuXvzqmjJf6cEKqFSYwWn6Uz eRvN+E87/4GtMRGQQQ99fnSqfh9P+b5QSe/iglVsPTw9j1THbp7W6QAoexjrxvjg6pEbQ ad6jhOJgUQBm8In1Qe3IAU3m7itNfgtDnZIcRHL9/5JOb3dR03AW2d8IcKDWuqa4G/2LR +BNOwgfw/MtABaWyU3+MN28nRY6i9Xt954sxaN/kSZUqO9xQiuI7IOgc9Mkzo91Adng== 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=e92wBPJE; arc=none smtp.remote-ip=209.85.128.45 Received: from mail-wm1-f45.google.com (mail-wm1-f45.google.com [209.85.128.45]) (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 99D8B606A325 for ; Mon, 30 Nov 2026 14:59:16 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 99D8B606A325 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=e92wBPJE Received: by mail-wm1-f45.google.com with SMTP id 5b1f17b1804b1-4bb845433b8so368195e9.0 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=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=OM7n37QaLEMMXqk0FmnIiXvJ5iDT8/jvqXeb7lsqPc0=; b=e92wBPJEiszXyLT2/zqkvdJRKWt0iQsTTuB7Ovzzv+4iAl+pb6icA/qZslF6aFQKSY p9SQeaT/yOTXZz/UGKACO4Bv4hO/R1N47nnQ1KmJMnXzAzYTJecDX3OlFyox3kG8IHV9 /yQUCAcUMnsNwr5BED/RRdrI7sGc7ww+hTv8UxUEF5hjeO5dIteqfcp5Ukx23Eo6BcdK dJdIY9KZWeT4Nv91wnyFIgAf/zoyRIMc53LUxLjq2QRUk1lS0yltw6SQqMYYpVD9FS+i qcwLwIDQcGAFcZU5F75cnE6L5amA+59nKFxxQYb19AvCrxyHcP3l+nL8DQmTL5h7CNlr l6pg== 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=mf5amOMs/CPcpuHSCOgqyBWWuqylwlReV+oINR4z/p6/6SsPdv6x/Q+1cofNvhhmcw d9zp0hxnGCq14UqLkxyu+KcFV+134fWHzYTrjp2haY479tTTChJ5TNGhBtxoXAmP4+JJ cTz5N+Ec0Oyp/e16TgyiITL4JIouOWY8i9XbsfKgI0JQFId5u7RGGFy44S42sxFBif5U XDrLQYF6eAgwAC3cecnIbmiZVMc2ty8t1bvB7WHHNxbM5JrxMLQ3otC6qtGrE/mVKkA1 HyIilYS1ZFZcJUfBuB/G4EZ3YhblzchcRytptZe48R0bCEs1rVCelY6S0VKRQflfqRro iL2g== X-Forwarded-Encrypted: i=1; AL+hErGTl7vblDsoUCUKns9+E9inBO31f/c/EdlhkGpm7A7dubJqOgtKu/B9MTYDMGUNJULYFd4OyjhX9SQ+n/c=@vger.kernel.org X-Gm-Message-State: APuU4Z+zRrAZOlmQgjAUy2vka4bpwLrkQFjwjpAQ5ENFc45z8Y0oMU9a eEhLmvp2aSdJTQtxocmUh/GdQyHBqUMXb2QRbu3bQ//YccUlkAPNG+mBnBMXQjraMQ== X-Gm-Gg: AZFdTZfyRXugHlpRDk6qmKAKSj+35aya2RIbSxUSN6nMK7QwZOZZOBADbUC37bWV4lG h6ZERQ1Y6ho9EH7xBY626Vk8vGEpGq2XpxLsvztBmBXbw0gUuaFMDjy949B8v71ViNwU4CdsjCS eEBQL3Ga+L6enkNr1jNLXCQj9RoveOE5twoWZFiPLS3riGbu7NjywmoowkOTXMMdMrujOpiF/Km cQf+UWgDBLb7xjtiavRG2q8xCBsnxM10g3lhZIv24DErnHL077hIMlHCAuFSe0P/lZdFsm5NE+V KgkYreBXjHOrPsiykO2O36mL7R8OXSBrr4D0QVOZUeHKMz1eFiNsDISZHZawYBD6ys583UHdzU0 N2u4pAnDAd3VZO8xJwKREUVqpB2aB1N3M+m6v+IQC9k9u4fmpm01FUsJDkvlFtvsyujRbI/8O1n 4bj8+c2MvpFd1s/z/OCExczbsbIDgBv7fjmX7KovU4Q+/TM3qhzH0Yse8ZM5Dw1FsmuCGv+2PVY ThBHyWhHyoCoe1rSz0AgqL6ptrermHusgKGikL2dSoqbna3tArLheS6bR9PQvDOnUaYQMSIVeie D3T8wH9S78Ln9oM4SN+O3q1fAmU/T62hWh/AYQN11Ka/uAt3tWY2RIS4uEJQ6WwcyiBb8RY= 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: linux-kernel@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: <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 >