From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-ej1-f51.google.com (mail-ej1-f51.google.com [209.85.218.51]) (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 138724A5C32 for ; Tue, 6 Oct 2026 18:32:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.51 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1791311572; cv=none; b=fqoDYsVDZBlkX5eX7PQvIUAFh0LKi2oNfD/V9Wn+HBYgGr6GpOQ6tnwitHy2KhcSOqrNj+A26RbRkwKGLpePgWyPocKMlunt3h86S4hocp2vD9wOnR+B1XxM5H8Ulh/lHTaL0Ei+UfOsqvT1VKUVQqP5OyzIQMJDReXl+ArOIa8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1791311572; c=relaxed/simple; bh=kkqbUgVwBKOTYlQ78x90lP7izeCrXUFDv8Sx7QBUcdI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=aydcJF6zxs/4n2FbMbAzgbAKffFRZjNn6OPsdXqgjvEMzdpxmAPyAWk1qS7jv8aMaipf3Eo53nXMF9HQdPJUuUzQ+rL6+2sMAeDSCEi/RHygVhdawD2RutWeIblxA60rkLn8rPD4F6MkAwFr38OYP66rH2RsyATRRzm7JD2seUo= 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=hodc3WlG; arc=none smtp.client-ip=209.85.218.51 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="hodc3WlG" Received: by mail-ej1-f51.google.com with SMTP id a640c23a62f3a-c2055573c8cso128690966b.3 for ; Tue, 06 Oct 2026 11:32:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1791311564; x=1791916364; 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=2oSZUxao1RfQULD3rGp8RyuZAgKuZ2Qm5VdphDYSiGw=; b=hodc3WlGP62k4H/ofvFFTc/WvELjMbSazAtISEzZ076XfXzdxz/9H/AWUJYAlkEIZT MY1ZfEz7e8rn1p8MWe2fy/g1NFhllYl1G4wlDFawIKdQ5WGm0oCv4u+pJJX1SS4fflq5 iL+Kes53vMUIp9pU/2G5Jn/rFi8T/dcue7olcNIO7BifZA0AikRTLFmQ5zVeRJ5IacH6 mwctOeApCsa2afYrdG2VxCFYdxemeZz3adxV5KN3ll5buyFZG3coBpxVZvC/IuNmM50t Z+phzeGYBXTNz/4K7ZNQW5u+yEjwyLc87Ey7r2tSjV87gdluwuTzqG8ccdFmTPP5p9PG f5cw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1791311564; x=1791916364; 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=2oSZUxao1RfQULD3rGp8RyuZAgKuZ2Qm5VdphDYSiGw=; b=V6RUzGZ5H5qXkGDv7swrXi6dUevuHm+12jUGugd8K+hXJN0v4+bIu3UNWhBY9fzFE2 7TMDliP2Yach5iZs6N4WDWcOEVii71kD1/YlP4dDgwLXcpDxmNQOVO8bQo4Vv6vXfOip /9jsdPxaQbho56x9bARy7x9ZMMKLap+kv/BrCUMtDnzF8FUj5VT+jG1v3PMfWa/01DWa aH1Ku72RvlLbq4OTbssWJMPXSTHM2fM9OE6d/Dq/Joe9XjlUANe7K670RrTLiZTh8+0D uI2uJEj4x04IBfm/szdO3ycWcsJ6w28LrLKCLN3/WxTIVd3/tFzTIB5xqaiDyT0Sa/z4 Wgwg== X-Forwarded-Encrypted: i=1; AKwUvByQTrJI6tTal8r4oWHVbHDtRP6L7leOuW3E+yr1P9GZYXTVz798JEkBRC+qzmtCt2RcZsxKy1A=@lists.linux.dev X-Gm-Message-State: AFuF++nnso7p+4bYY0N/xBJnjuLgTrRwj4f2CyJj3boyzcbOlhvf2TAw BZbUHGf22GOSM4Hk+MPkPJ7/N5cvUKaqXk5aitnSE7wFxjpirBwDv8uh X-Gm-Gg: AYBFou35nSdlJs6S6Gyfw7Q0DYfWbNCh2xRnVpXvEMExBOIujoyKGmCGgSg+nE/vtTh KrWppT8qkgH5rULnbg0/9JsaC89BFwt6yS8Syd5K+5GvQlu0JQSjqWq9rwIAmQTgTkdKmGmsVx5 lgrITOFdSQ/3a71jvNdsAZ9fnJXtS/WjoJvsEIOwwGmkPJqma/inaQ0FTQX0NyUTllu3WXSwJMc zCNtOl28dLHYg0xe57xryWny7uao1gU5h+ZuKGxv4G7chOkHDmyrCYESZPis04rjvkg7P8k5vCJ vse//8rgjNkglxltIj9V4mdJ2PQoqGm0AFtGVDOXGU0klxuHABa0U/QnbnyRhK571FfPazDqpwN bRKcb29KaZMVOpb91jR0ttyfmUS/O6HmQeOk7Ir4fyaKaQpNeciw4QWej7e0sXKCIqguKGOmbO7 48AOajqizME/NbDW1dsgN5+pL2qtyTp8hDIefj4JnGRYr2pFeFiWoKJgRTkul9rvIPNMjdxDOGp MYgW7uAlP1obO0E8937I6VaqW/waeXnyqmnLSjn26yTII1NSsAzcpPcssRI8wSK2Ja3UZ+435tK x8wEmYaLiqOnR45ByfIVd6bNBjLHO6yi13E= X-Received: by 2002:a17:906:ef0a:b0:c2a:f571:312f with SMTP id a640c23a62f3a-c3169fb0c2fmr247672166b.20.1791311563344; Tue, 06 Oct 2026 11:32:43 -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 a640c23a62f3a-c3158260f7bsm221934866b.6.2026.10.06.11.32.41 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 06 Oct 2026 11:32:42 -0700 (PDT) From: Fred Griffoul To: Paolo Bonzini , Sean Christopherson , Marc Zyngier , Oliver Upton , 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 Cc: David Woodhouse , Ackerley Tng , 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, iommu@lists.linux.dev, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org Subject: [PATCH 4/9] iommufd: Track the domains of pages that are not pinned Date: Tue, 6 Oct 2026 18:32:30 +0000 Message-ID: <20261006183235.16576-5-griffoul@gmail.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261006183235.16576-1-griffoul@gmail.com> References: <20260720111259.122911-1-dwmw2@infradead.org> <20261006183235.16576-1-griffoul@gmail.com> Precedence: bulk X-Mailing-List: kvmarm@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. 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 887ed94474c7..5389227eb6ff 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; /* * true if the exporter's phys is CPU RAM (map with IOMMU_CACHE, no * IOMMU_MMIO); false for MMIO/BAR memory (map with IOMMU_MMIO). Set @@ -253,6 +252,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) @@ -262,6 +267,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 f9b2ae6d7e96..d68f6eea836d 100644 --- a/drivers/iommu/iommufd/pages.c +++ b/drivers/iommu/iommufd/pages.c @@ -1392,6 +1392,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; @@ -1442,13 +1443,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, @@ -1598,7 +1599,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) { @@ -1609,16 +1609,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; @@ -1627,21 +1627,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); @@ -1651,36 +1650,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); @@ -1697,6 +1696,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; @@ -1705,7 +1705,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); } @@ -1790,7 +1789,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, @@ -1958,8 +1957,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; } @@ -2024,8 +2023,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; @@ -2065,8 +2064,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);