From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.128.44 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796050757; b=MLnZBAyN2N41YgorOe5TFJxAYESSwYdaTGZcjjZ1b+cAPVo5diRGdZI+ttk1MXfagxmU W0hW8OYpNrnE2+xSxVWBae95pPlIGWyI1HFgdXIfffRtXlTNtBKaZJaI3kgIj52ZBBzG1 fhFe3Zb/7cBgzGw7SrQG6UVva7Hx+rV39dPyWMR3i2296jnax3lxkoKlQxvmO4LjwTRj5 WUJbIoEAfRlGBTsS6QkYj3OqeUHZiMmKmj1ap5wVg2aH0lUhcne2J7qbqOV8dbAbSUiLh 07e3j59yC0p7q/UuYFquMGySYBswwNl9S9Lr1K9ZtELW0mJ/R0TeDszRPsZ7FQOxLKg== 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=xVdcurasY6kNoagwuLSk6Hz8kuFyHVudwWx1xU9GfRXM4UkLGGDjBKZe7qn4FtbZJQbf +xVgnaj1q0ph4F16Tlk5xDc1+HYGukOatC8X18hFwLUbWzQ0cnZyug6AzdrSVmppCxMvN yZn3u64bFbsV34tfdwbGgdGqR5KCuRVW2ooHIOd/Ry5dqnmqxzahvJOyUUmYWB0fbU7vr FLxsrQxRw7832GdZ6fR9cs8RwXjvvrrrj2jp7c5S2gnO5HH8EgBDnDFUSbUucOtEVXo9z lkeidrQ8lzCTBpBsXuHzV5t6QrxFlwo3lOZm0Wn9ahD/Kzqs5wA1pRCHF3h1w7sf+9A== 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.44 Received: from mail-wm1-f44.google.com (mail-wm1-f44.google.com [209.85.128.44]) (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 9606B606A320 for ; Mon, 30 Nov 2026 14:59:16 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 9606B606A320 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-f44.google.com with SMTP id 5b1f17b1804b1-4bb6860f867so489415e9.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=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=gz16zureu5gnH+awEHhEW8TI4IVLwK7AkVsKyu+vMjMkFzgqT7ftxqXb4i2Wwpsq/I WxUdpV728dl2+coDit12uM0gHb63AcCQl4j3bHx0wBS+buCAlo2qCbAlIicOdl81D1bT z+bgKhP5ajZBn32L2GEQDIycOPALspyPFdWBkBT0YEzi3sDrCvv98gpxM5XOSTeoVAR2 a8iiFvBoYU9mncFfMHuK5DHJOFKxV4sK6+lj0huMQci4MabeJXksWGWOj/TH12fO4ifA v1JYtMR2emGmoa6gUDzdx3HInWHuPqxyhWAKWaMhQSQrpXvDfQ+mhsfnoQkme7PzNkp9 9gqA== X-Forwarded-Encrypted: i=1; AL+hErGRdZGN6JnS0Kbxd+GEXhfXJq+Jd/HTbOqlumIphAR8ix56u1xIyy2EzBJU+DabpQYuq8IoU+FOpGxQIPtcbdk=@vger.kernel.org X-Gm-Message-State: APuU4Z+ACaaoLp6i6LOoz0i/Wfe9jz13XnEqqI0RXC3L6FlX6QGCqyHQ 1SfIdqFYFiMTGqgAisOaMasXQ+3FsqtnIy3rLgvczlMKujwW6WwIsICBSMG4hgnn4A== X-Gm-Gg: AZFdTZfOEnk2LhnD+CjAFscpW5m8lsB7dlBAbHllZhxQ+15ttMP2zGKzQma723qBroW O8mwtjKHdHa8HbC2mjqzqCGOu0XRjmJ5d84emCNfBy9oV9gVO7CdQHBpWSS2YYIvxIT7F7MpN/9 FqCBWm5wXZdQSOzAjajHjvqm/b6g7Uf6LTift6xioYTp7pA9mvfi9AF1egDQ8YW9Ltog/hIVvht cVnJxOywvMYtjfSjfbPGfa9qJk6/vcyzwrIbZ3WYbysgpiewx/ULOYe5lCbgSYtFVO6wWO3EOvi K2sJvP5mGfQfJl0yh6Fu1gt7IONnRWPQUciy7jbOV4zhPPKO1TkDX2gic8HtwgCuoMjLir59BAI miz3NDsnrjDQokHpd9Mi4+FXw1tGQJXbKv0kBzVPbn5P7xAUhGk/+00orKnbdyVhOhfdNxYboqR jQcG0TEHOuQG+DhX4KZKdUIS3CxwllW0g5eXGy65+iZR+5StDhJ6d2HdNjJJyuImE4zcgfWq1Pa 1iYg2o8Vl5DpmyDR6ps1LVZatZ7lJN3NDWjQI/3ZDKVuU9f3Q014LrKivojM8zh09GI2yXc2V8o BObRemqmh2JSvKKVXUJxUi4HsYZmgJo3YswE97Nwq+yCwFPX7vb/iqmsutZwNHQH7TZNBRk= 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-kselftest@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 >