From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-ej1-f47.google.com (mail-ej1-f47.google.com [209.85.218.47]) (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 40DB64A5EC7 for ; Tue, 6 Oct 2026 18:32:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.47 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1791311570; cv=none; b=TDIRwnb/yyz7jPUtjP/eXuU1HHqnmqqLKjjCJA+csmpiXDDXqmoy8S2wLn28ou+4587Xe/d8OAkcLnqkNkmqyuMiUWJ6Q+BKVOlxVDKLwPQTapz9JDkVPkKmOeuktb61G2Md3I38BqmbqoEqycCL/R4QC1oAbDG0nfwUyrhDsEQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1791311570; c=relaxed/simple; bh=kkqbUgVwBKOTYlQ78x90lP7izeCrXUFDv8Sx7QBUcdI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=W7SjodyDAw7L6+RUhujlqiTleZ1TmAwZN+v5VRyKBQA1CS8QixkoML+iMGrkFzjCai2mGbkQ9Knmc2sCXv3crlDbGiakjKpowgjOut1nTfMQbzLMtwmmxt678t5fkt0tmw3DvWaHXDbZHpt0qAkSPYDFq/n2jn9vorF74cW8E8A= 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=Qv541VRt; arc=none smtp.client-ip=209.85.218.47 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="Qv541VRt" Received: by mail-ej1-f47.google.com with SMTP id a640c23a62f3a-c2e4df2e26fso122294566b.2 for ; Tue, 06 Oct 2026 11:32:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1791311564; x=1791916364; darn=vger.kernel.org; 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=Qv541VRt3PNp8Chvg5sA/p+odPF3RMDBJSwiu4k6ujyJXW4xpx2ThWtuHunKO3UeDg iV10HjLRlKvSZWdcxzANhUwXGKXWlLaq4aMVrC0dNV2j60wURJrsfN3ZGpqOI8BfJIkB Dl4uPurD6jTPihVojso+x6T7qqKes+lZLQ2ubdhT8QVxBubDDkSo/PKYnaYeiiOM3obm x65HdsvZ55VkDPyBK9BcDIxtRbdfV3sLrTC4rScIdqH14wjwt0g4pf0nkI2sGTRR35Dw in/yFYCOvhLNosP7AxHNmF16BFf6DePdWzTXsZFpxWDX7+/sRdnd4HWQj33aQrGAEqo/ WnCg== 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=JwRnAzCeuGT9ZwHwdJc/tfaVncTS/y2sDhjYuIRT84CruT2l8rPTaKoxdcaeWxogxr 7djg7vAdoeKuwL92+VPD8yo9slz5ySKGH5nBglnWprl5adLNZdDwYKP7iF/K2l7hWm8g wwbxIddLosAlFHptOPb9eZZ3mV2a57FZ6gwkLxCanL8tmgK5QUJnQLaSJ+I5G5MVM6JJ uud61AdzUN5qwap+OZavuXWbOKktoo2Z0G1TOBqUfDQsckMMNtT7oCnnYyGif4YYBiH0 az1ROBZql/UiHmsZ8OdWFeeFropJp7zLjYNNFQaZ4YL3nA8l2JparX4qDiK+Yf7u8A0O wSyA== X-Forwarded-Encrypted: i=1; AKwUvByCcXakqbxtCv6lO3ZwoEA8/Q9xkC37SOL72s+9Ovi7Q5VlzOs+zyzDhtivEfnRWVsibuujHpx9Dy9omhk=@vger.kernel.org X-Gm-Message-State: AFuF++nlcDv3IKqFD50rpc1srX5UYtX69+L36tA2vpm2izwNLsokr4Ke Cq31d2dkBZohB0qo4aikYqkca22bqzC8/xx0EGLaWjwJ+eXQRRejbhzL X-Gm-Gg: AYBFou29SpJCkRfik3yBMumE1Pq6pjAlEyoZU534iVUlvlUHvAnPQRV7V/QKDErOKd0 bhlEo3efGQRwuBveTopAM8le0E8AiLx7WoqZsQBKrhHAC+UQtN+U3lQBzD+llgGUSMFPDl7RwhC n9c5yXcKkM1ARcOrKPyilEjVNzKGaLdDckTl3IZ2pJLjAihSwxBfePf2dZUqZcJcOyA7fk5rke+ xMvnKA8eJSVCY1A8x1u/K2/ISABxGir4cO+0vzVEjPFJ+M9SUry7Hh4R8OkKhoEP5H4uS+kKkLL z2lD5VNELsBi9EHutgQrqo1KkHs87m/n/6EMvACgxZ9dbdp7X5anODfo9IIhKrc88hER8wtXIGi l/Lw/Ev5RTd2Cfa8IZrVJnvoa1v8o41UuDv7rPKrG8uHFxA+qvTyaAauRfvUpPtrjizHQfQy4XH 19R4lwDoIHOVeS2H2cuGJAMxTGstnF9ubPJoKB+fbMJgILwcsbulWT02wLull6AaxzHxbxnLnCg cEg1KX121IvZXj+hdmmtiIc/CALG3l7XP35fXkCrg2xfBCx1OGrfjepiiPpFiWwmK5EsJBm+fFI pwWyOFu8MVlq4WQLfQk8Xm7LyP1mJOsUZCs= 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: linux-kernel@vger.kernel.org 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);