From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f42.google.com (mail-wm1-f42.google.com [209.85.128.42]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6B6244E0219 for ; Wed, 14 Oct 2026 09:37:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.42 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1791970671; cv=none; b=pad6MbKbJUWLFRZOL/Vqt1mYJzGl20p3oJI+Es4sEpUOHYoouZhsFVwPcCRGmMLTjEol2XTavdqmyehwKKntdghiBsjJeSlMu9onJ8otQU/NGCiFFloKN5lq03u2iT8eauXIUcSUloiujAB4dSCk9eQN+2flyaWd4++EbCgxeT4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1791970671; c=relaxed/simple; bh=2O/iMNJqoH7UyElUt7ICv1K7dgb27nZuWpr+bMknwbw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=SfhGINyTKYc/cPrRsI8gsXq1uERa4T5Keged27Xn+aDrrazfWYI8RzAu7kKZ1o3da90BhYqVHLZDxlnVwsxEzORt+tBuQXapaE5dELrn7jK4FSgjazHuqrDtO31D6YOhCFOqx0BJo8cZk0FI93q36vfRH1LgPsxJr1GgCCvGo4o= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=S3B/rRrK; arc=none smtp.client-ip=209.85.128.42 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="S3B/rRrK" Received: by mail-wm1-f42.google.com with SMTP id 5b1f17b1804b1-4a19cba5c14so32540425e9.3 for ; Wed, 14 Oct 2026 02:37:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1791970662; x=1792575462; darn=lists.linux.dev; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=2bE4GOYNuN0L2A43+aeT8XcVziQfGC7lOtj0K7aqEDA=; b=S3B/rRrKBht9RlfKTVVbl97Tb2iJP1YE8nhXC41B8u0ng24XbHXsORpyLer8tncZ90 yNEhIYJkb6G1FUK6Eex/y5cUA6z8cixqvmnoC2XJvCW39tqM8Lv0dXDqwjGFhDCRe8xM x1wwpPhemi9bVPblamYWeBOohu6akz+fC0n7rU289kApoi/dp0omRE+GV4iZOYKO+fR0 fq70GlnnhxK4tMiYxba9RI81/IAZ6tig32H5Y/DM4bBwpBRMcvCPO7V7HIv+WVVhMggc cCD+KkRmRcrvnjXpGFoPhoesV6oy6Bv+nICHwuCL+SSiMSepGSVTcwhe6IHzDaq9txUj fLuQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1791970662; x=1792575462; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=2bE4GOYNuN0L2A43+aeT8XcVziQfGC7lOtj0K7aqEDA=; b=d7sAobXF1L4bHgsH6kdpzJKqvQa42ed1h5guugPM3TU0sE/GeFrvXqPa8bNCzRdDJk z02rvj3/jVDn6zd9iV1IeHrgaVyVyzhFpSwX9KSJOa8CqNXiNUGWv5uB0+uZ1OsJ51J5 Pxrmc8Uz+StmRg4zhbMpH5DcgqoSiy552J/vdomXQgQd252dQNg6vRvQ9ypc6JXeZo+B ZaKK25pS26bKwss4Z3EWmbTyzG1pV6Ze0qQU6YHw3zh2NIIGyWDHViUHRCrvd4GhriDW GXiYn7jzv+18slcMTvRe9MPOUHzbu+7BxYGukvEncDxpeLbPixXDTC6Q67MRDlmgS3zX o50Q== X-Forwarded-Encrypted: i=1; AKwUvByBpB3f46gLZHOid/lCJNjjlF5adfXHDhnRaFYbaK7o8mw1pUI/EW+zbAIsl8WHFSdFLSviFQ==@lists.linux.dev X-Gm-Message-State: AFq9FYLCFzvyueCfTZh7oumz3r0jWVR4j3ARQ4awO6Gh9vfSnNcQafe2 bCmiNzIfa1FuaIB8RXnFpj41KacTVRmq9CCR/zT67bnzZJ6/0XUyxT/J X-Gm-Gg: AYBFou2v96/198WUeYUcmuQCe66kJNcynbt+HPuIS2TH7xx8bv6bE2fP1iXlY8EueOx 8yT/PI/MICg4FaA82teZqNLxiuvM+i2e/Dmt2KzDsUkeEQt7xSvd1OfS2C+9ZggJUvvMFo4p7bv oNuU0DA6ihBuWwqowL2uM58cIbSu6MaJAb4ugbZ2606zbdTGobkVf2yHUNH0ddv5FbgRtqv/zMy j8/YxJWf5O9t1FVLuvDnyp1uDHRAVyZky9oDY6CoTrICBMWDfSLh4aYU/hdaOIl3itcYhm40oUr Ev7AelO4+zmD12A32P5mPHOa37ZpMw3Ty96v5vZktmmEagemA60TauUHvqg2w6cjMM/O/Knltk7 ROWdxJO18ujBsrkrCi05/HmeBUd1BwEikov/DPXuAov9cuw+BZ/xlJ3AGKz9w5W7cWGen0BSxiG yppg3WXniEIRhKIbGOgE26iSn7VKgdnbDru1im0vMuiEFN7QJ8iir/+DmtrIr7p7DsV/mgTQPCX SExnBRtF4Fs0gtlZLudlXclceZ82ueX9rIfiwi/2uvraAgSqI44FH01MCKhbXizAaDa2+NJEymi MHgKkGgFpWaK8lTp/FJVgakPHPXk4Y1Ei/k= X-Received: by 2002:a05:600c:5493:b0:4a1:78da:d271 with SMTP id 5b1f17b1804b1-4a18e4ee976mr332971435e9.30.1791970662375; Wed, 14 Oct 2026 02:37:42 -0700 (PDT) Received: from dev-dsk-fgriffo-1c-93421965.eu-west-1.amazon.com (54-240-197-234.amazon.com. [54.240.197.234]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-48de3f7f8b8sm9365477f8f.35.2026.10.14.02.37.41 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 14 Oct 2026 02:37:41 -0700 (PDT) From: Fred Griffoul To: Paolo Bonzini , Sean Christopherson , Marc Zyngier , Oliver Upton , Catalin Marinas , Andrew Morton , David Hildenbrand , Alexander Viro , Christian Brauner , Jan Kara , Jason Gunthorpe , Kevin Tian , Joerg Roedel , Will Deacon , Robin Murphy , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H . Peter Anvin" , Jonathan Corbet , Shuah Khan , Shuah Khan Cc: David Woodhouse , Ackerley Tng , "Aneesh Kumar K . V" , Lorenzo Stoakes , "Liam R . Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , Joey Gouly , Suzuki K Poulose , Zenghui Yu , Steffen Eiden , linux-kernel@vger.kernel.org, kvm@vger.kernel.org, kvmarm@lists.linux.dev, linux-arm-kernel@lists.infradead.org, iommu@lists.linux.dev, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org, linux-doc@vger.kernel.org Subject: [RFC PATCH v2 05/11] iommufd: Track the domains of pages that are not pinned Date: Wed, 14 Oct 2026 09:37:27 +0000 Message-ID: <20261014093733.90312-6-griffoul@gmail.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261014093733.90312-1-griffoul@gmail.com> References: <20261014093733.90312-1-griffoul@gmail.com> Precedence: bulk X-Mailing-List: iommu@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Fred Griffoul Memory provider pages, added in the next patch, are not pinned and can change under the domains that map them, as a dma-buf can. They need the same list of (area, domain) pairs that the dma-buf revoke uses. Move that list from struct iopt_pages_dmabuf to struct iopt_pages. No functional change. Assisted-by: Claude:claude-opus-5-5 Signed-off-by: Fred Griffoul --- drivers/iommu/iommufd/io_pagetable.c | 24 +++++----- drivers/iommu/iommufd/io_pagetable.h | 33 +++++++++----- drivers/iommu/iommufd/pages.c | 67 ++++++++++++++-------------- 3 files changed, 67 insertions(+), 57 deletions(-) diff --git a/drivers/iommu/iommufd/io_pagetable.c b/drivers/iommu/iommufd/io_pagetable.c index 24d4917105d9..bcd531acc9dd 100644 --- a/drivers/iommu/iommufd/io_pagetable.c +++ b/drivers/iommu/iommufd/io_pagetable.c @@ -1008,14 +1008,14 @@ static void iopt_unfill_domain(struct io_pagetable *iopt, WARN_ON(!area->storage_domain); if (area->storage_domain == domain) area->storage_domain = storage_domain; - if (iopt_is_dmabuf(pages)) { + if (iopt_pages_tracked(pages)) { if (!iopt_dmabuf_revoked(pages)) iopt_area_unmap_domain(area, domain); - iopt_dmabuf_untrack_domain(pages, area, domain); + iopt_pages_untrack_domain(pages, area, domain); } mutex_unlock(&pages->mutex); - if (!iopt_is_dmabuf(pages)) + if (!iopt_pages_tracked(pages)) iopt_area_unmap_domain(area, domain); } return; @@ -1033,8 +1033,8 @@ static void iopt_unfill_domain(struct io_pagetable *iopt, WARN_ON(area->storage_domain != domain); area->storage_domain = NULL; iopt_area_unfill_domain(area, pages, domain); - if (iopt_is_dmabuf(pages)) - iopt_dmabuf_untrack_domain(pages, area, domain); + if (iopt_pages_tracked(pages)) + iopt_pages_untrack_domain(pages, area, domain); mutex_unlock(&pages->mutex); } } @@ -1065,15 +1065,15 @@ static int iopt_fill_domain(struct io_pagetable *iopt, continue; guard(mutex)(&pages->mutex); - if (iopt_is_dmabuf(pages)) { - rc = iopt_dmabuf_track_domain(pages, area, domain); + if (iopt_pages_tracked(pages)) { + rc = iopt_pages_track_domain(pages, area, domain); if (rc) goto out_unfill; } rc = iopt_area_fill_domain(area, domain); if (rc) { - if (iopt_is_dmabuf(pages)) - iopt_dmabuf_untrack_domain(pages, area, domain); + if (iopt_pages_tracked(pages)) + iopt_pages_untrack_domain(pages, area, domain); goto out_unfill; } if (!area->storage_domain) { @@ -1102,8 +1102,8 @@ static int iopt_fill_domain(struct io_pagetable *iopt, area->storage_domain = NULL; } iopt_area_unfill_domain(area, pages, domain); - if (iopt_is_dmabuf(pages)) - iopt_dmabuf_untrack_domain(pages, area, domain); + if (iopt_pages_tracked(pages)) + iopt_pages_untrack_domain(pages, area, domain); mutex_unlock(&pages->mutex); } return rc; @@ -1315,7 +1315,7 @@ static int iopt_area_split(struct iopt_area *area, unsigned long iova) return -EBUSY; /* Maintaining the domains_itree below is a bit complicated */ - if (iopt_is_dmabuf(pages)) + if (iopt_pages_tracked(pages)) return -EOPNOTSUPP; if (new_start & (alignment - 1) || diff --git a/drivers/iommu/iommufd/io_pagetable.h b/drivers/iommu/iommufd/io_pagetable.h index 27e3e311d395..ba539bfaac31 100644 --- a/drivers/iommu/iommufd/io_pagetable.h +++ b/drivers/iommu/iommufd/io_pagetable.h @@ -70,15 +70,14 @@ void iopt_area_unfill_domain(struct iopt_area *area, struct iopt_pages *pages, void iopt_area_unmap_domain(struct iopt_area *area, struct iommu_domain *domain); -int iopt_dmabuf_track_domain(struct iopt_pages *pages, struct iopt_area *area, - struct iommu_domain *domain); -void iopt_dmabuf_untrack_domain(struct iopt_pages *pages, - struct iopt_area *area, - struct iommu_domain *domain); -int iopt_dmabuf_track_all_domains(struct iopt_area *area, - struct iopt_pages *pages); -void iopt_dmabuf_untrack_all_domains(struct iopt_area *area, - struct iopt_pages *pages); +int iopt_pages_track_domain(struct iopt_pages *pages, struct iopt_area *area, + struct iommu_domain *domain); +void iopt_pages_untrack_domain(struct iopt_pages *pages, struct iopt_area *area, + struct iommu_domain *domain); +int iopt_pages_track_all_domains(struct iopt_area *area, + struct iopt_pages *pages); +void iopt_pages_untrack_all_domains(struct iopt_area *area, + struct iopt_pages *pages); static inline unsigned long iopt_area_index(struct iopt_area *area) { @@ -194,7 +193,8 @@ enum iopt_address_type { IOPT_ADDRESS_DMABUF, }; -struct iopt_pages_dmabuf_track { +/* An area of the pages mapped into a domain, for pages that are not pinned. */ +struct iopt_pages_track { struct iommu_domain *domain; struct iopt_area *area; struct list_head elm; @@ -205,7 +205,6 @@ struct iopt_pages_dmabuf { struct phys_vec phys; /* Always PAGE_SIZE aligned */ unsigned long start; - struct list_head tracker; }; /* @@ -246,6 +245,12 @@ struct iopt_pages { struct rb_root_cached access_itree; /* Of iopt_area::pages_node */ struct rb_root_cached domains_itree; + /* + * Of iopt_pages_track::elm. Pages that are not pinned can change + * under the domains that map them, so every (area, domain) that maps + * them is listed here. See iopt_pages_tracked(). + */ + struct list_head tracker; }; static inline bool iopt_is_dmabuf(struct iopt_pages *pages) @@ -255,6 +260,12 @@ static inline bool iopt_is_dmabuf(struct iopt_pages *pages) return pages->type == IOPT_ADDRESS_DMABUF; } +/* The pages are not pinned, so their domains are tracked in pages->tracker. */ +static inline bool iopt_pages_tracked(struct iopt_pages *pages) +{ + return iopt_is_dmabuf(pages); +} + static inline bool iopt_dmabuf_revoked(struct iopt_pages *pages) { lockdep_assert_held(&pages->mutex); diff --git a/drivers/iommu/iommufd/pages.c b/drivers/iommu/iommufd/pages.c index 03c8379bbc34..660af3d46f5e 100644 --- a/drivers/iommu/iommufd/pages.c +++ b/drivers/iommu/iommufd/pages.c @@ -1384,6 +1384,7 @@ static struct iopt_pages *iopt_alloc_pages(unsigned long start_byte, pages->npages = DIV_ROUND_UP(length + start_byte, PAGE_SIZE); pages->access_itree = RB_ROOT_CACHED; pages->domains_itree = RB_ROOT_CACHED; + INIT_LIST_HEAD(&pages->tracker); pages->writable = writable; if (capable(CAP_IPC_LOCK)) pages->account_mode = IOPT_PAGES_ACCOUNT_NONE; @@ -1434,13 +1435,13 @@ struct iopt_pages *iopt_alloc_file_pages(struct file *file, static void iopt_revoke_notify(struct dma_buf_attachment *attach) { struct iopt_pages *pages = attach->importer_priv; - struct iopt_pages_dmabuf_track *track; + struct iopt_pages_track *track; guard(mutex)(&pages->mutex); if (iopt_dmabuf_revoked(pages)) return; - list_for_each_entry(track, &pages->dmabuf.tracker, elm) { + list_for_each_entry(track, &pages->tracker, elm) { struct iopt_area *area = track->area; iopt_area_unmap_domain_range(area, track->domain, @@ -1557,7 +1558,6 @@ struct iopt_pages *iopt_alloc_dmabuf_pages(struct iommufd_ctx *ictx, pages->account_mode = IOPT_PAGES_ACCOUNT_NONE; pages->type = IOPT_ADDRESS_DMABUF; pages->dmabuf.start = start - start_byte; - INIT_LIST_HEAD(&pages->dmabuf.tracker); rc = iopt_map_dmabuf(ictx, pages, dmabuf); if (rc) { @@ -1568,16 +1568,16 @@ struct iopt_pages *iopt_alloc_dmabuf_pages(struct iommufd_ctx *ictx, return pages; } -int iopt_dmabuf_track_domain(struct iopt_pages *pages, struct iopt_area *area, - struct iommu_domain *domain) +int iopt_pages_track_domain(struct iopt_pages *pages, struct iopt_area *area, + struct iommu_domain *domain) { - struct iopt_pages_dmabuf_track *track; + struct iopt_pages_track *track; lockdep_assert_held(&pages->mutex); - if (WARN_ON(!iopt_is_dmabuf(pages))) + if (WARN_ON(!iopt_pages_tracked(pages))) return -EINVAL; - list_for_each_entry(track, &pages->dmabuf.tracker, elm) + list_for_each_entry(track, &pages->tracker, elm) if (WARN_ON(track->domain == domain && track->area == area)) return -EINVAL; @@ -1586,21 +1586,20 @@ int iopt_dmabuf_track_domain(struct iopt_pages *pages, struct iopt_area *area, return -ENOMEM; track->domain = domain; track->area = area; - list_add_tail(&track->elm, &pages->dmabuf.tracker); + list_add_tail(&track->elm, &pages->tracker); return 0; } -void iopt_dmabuf_untrack_domain(struct iopt_pages *pages, - struct iopt_area *area, - struct iommu_domain *domain) +void iopt_pages_untrack_domain(struct iopt_pages *pages, struct iopt_area *area, + struct iommu_domain *domain) { - struct iopt_pages_dmabuf_track *track; + struct iopt_pages_track *track; lockdep_assert_held(&pages->mutex); - WARN_ON(!iopt_is_dmabuf(pages)); + WARN_ON(!iopt_pages_tracked(pages)); - list_for_each_entry(track, &pages->dmabuf.tracker, elm) { + list_for_each_entry(track, &pages->tracker, elm) { if (track->domain == domain && track->area == area) { list_del(&track->elm); kfree(track); @@ -1610,36 +1609,36 @@ void iopt_dmabuf_untrack_domain(struct iopt_pages *pages, WARN_ON(true); } -int iopt_dmabuf_track_all_domains(struct iopt_area *area, - struct iopt_pages *pages) +int iopt_pages_track_all_domains(struct iopt_area *area, + struct iopt_pages *pages) { - struct iopt_pages_dmabuf_track *track; + struct iopt_pages_track *track; struct iommu_domain *domain; unsigned long index; int rc; - list_for_each_entry(track, &pages->dmabuf.tracker, elm) + list_for_each_entry(track, &pages->tracker, elm) if (WARN_ON(track->area == area)) return -EINVAL; xa_for_each(&area->iopt->domains, index, domain) { - rc = iopt_dmabuf_track_domain(pages, area, domain); + rc = iopt_pages_track_domain(pages, area, domain); if (rc) goto err_untrack; } return 0; err_untrack: - iopt_dmabuf_untrack_all_domains(area, pages); + iopt_pages_untrack_all_domains(area, pages); return rc; } -void iopt_dmabuf_untrack_all_domains(struct iopt_area *area, - struct iopt_pages *pages) +void iopt_pages_untrack_all_domains(struct iopt_area *area, + struct iopt_pages *pages) { - struct iopt_pages_dmabuf_track *track; - struct iopt_pages_dmabuf_track *tmp; + struct iopt_pages_track *track; + struct iopt_pages_track *tmp; - list_for_each_entry_safe(track, tmp, &pages->dmabuf.tracker, + list_for_each_entry_safe(track, tmp, &pages->tracker, elm) { if (track->area == area) { list_del(&track->elm); @@ -1656,6 +1655,7 @@ void iopt_release_pages(struct kref *kref) WARN_ON(!RB_EMPTY_ROOT(&pages->domains_itree.rb_root)); WARN_ON(pages->npinned); WARN_ON(!xa_empty(&pages->pinned_pfns)); + WARN_ON(!list_empty(&pages->tracker)); if (iopt_is_dmabuf(pages) && pages->dmabuf.attach) { struct dma_buf *dmabuf = pages->dmabuf.attach->dmabuf; @@ -1664,7 +1664,6 @@ void iopt_release_pages(struct kref *kref) dma_resv_unlock(dmabuf->resv); dma_buf_detach(dmabuf, pages->dmabuf.attach); dma_buf_put(dmabuf); - WARN_ON(!list_empty(&pages->dmabuf.tracker)); } else if (pages->type == IOPT_ADDRESS_FILE) { fput(pages->file); } @@ -1749,7 +1748,7 @@ static void __iopt_area_unfill_domain(struct iopt_area *area, lockdep_assert_held(&pages->mutex); - if (iopt_is_dmabuf(pages)) { + if (iopt_pages_tracked(pages)) { if (WARN_ON(iopt_dmabuf_revoked(pages))) return; iopt_area_unmap_domain_range(area, domain, start_index, @@ -1917,8 +1916,8 @@ int iopt_area_fill_domains(struct iopt_area *area, struct iopt_pages *pages) return 0; mutex_lock(&pages->mutex); - if (iopt_is_dmabuf(pages)) { - rc = iopt_dmabuf_track_all_domains(area, pages); + if (iopt_pages_tracked(pages)) { + rc = iopt_pages_track_all_domains(area, pages); if (rc) goto out_unlock; } @@ -1983,8 +1982,8 @@ int iopt_area_fill_domains(struct iopt_area *area, struct iopt_pages *pages) } pfn_reader_destroy(&pfns); out_untrack: - if (iopt_is_dmabuf(pages)) - iopt_dmabuf_untrack_all_domains(area, pages); + if (iopt_pages_tracked(pages)) + iopt_pages_untrack_all_domains(area, pages); out_unlock: mutex_unlock(&pages->mutex); return rc; @@ -2024,8 +2023,8 @@ void iopt_area_unfill_domains(struct iopt_area *area, struct iopt_pages *pages) WARN_ON(RB_EMPTY_NODE(&area->pages_node.rb)); interval_tree_remove(&area->pages_node, &pages->domains_itree); iopt_area_unfill_domain(area, pages, area->storage_domain); - if (iopt_is_dmabuf(pages)) - iopt_dmabuf_untrack_all_domains(area, pages); + if (iopt_pages_tracked(pages)) + iopt_pages_untrack_all_domains(area, pages); area->storage_domain = NULL; out_unlock: mutex_unlock(&pages->mutex); -- 2.47.3