Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 79F32606A32D for ; Thu, 3 Dec 2026 20:24:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=SJ88jfuA Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id B1F15112BD9; Thu, 3 Dec 2026 20:24:27 +0000 (UTC) Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.20]) by gabe.freedesktop.org (Postfix) with ESMTPS id 4B786112BAA; Thu, 3 Dec 2026 20:24:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1796329452; x=1827865452; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=eGuEOESPvFQdYtX04c5SGop/vRW5PsioJ9Hor3ebg+I=; b=SJ88jfuALZoingrjZ/DK+I6QMkAqAW0GFcxClY2vbYJ4UCyMwYSqhwFA bV0oxzuWO3YnkplN2f8jUtL3H5c9/jwSsgKH83JftwlGJzB6Jk72WiXOu iXttsgYwBYOLLuRVoXclUSRdUMz52Pu3sfQAMrR6274G2OHW+zpwndGsU maDiu20+YtAgHVaCJeVx8FuqogjbzGyUscAkEtSqsjiuTPyyWFRbuEoqW OQ7gKoGot7I2qIAex3fhADuCbDoJY4pq1r6qD4YRZ18T01d1bqKAv+5qV xWKrwEWLDhiqcrhom1q4ckZmzF7rRn30wRnOnirwMcHv1vzJ7L7wKVCIU g==; X-CSE-ConnectionGUID: MuNehvcVTZS2mQhjspgq2A== X-CSE-MsgGUID: ucvfNzPJS2yPpoJpiGUh9A== X-IronPort-AV: E=McAfee;i="6800,10657,11985"; a="5351086" X-IronPort-AV: E=Sophos;i="6.29,77,1794902400"; d="scan'208";a="5351086" Received: from fmviesa013.fm.intel.com ([10.60.135.153]) by orvoesa112.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 03 Dec 2026 12:24:02 -0800 X-CSE-ConnectionGUID: Qk68tZaGTMiIelcDBtUyrQ== X-CSE-MsgGUID: 7s/euVcOQ/65U61LjTOCWg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.29,77,1794902400"; d="scan'208";a="15862455" Received: from gsse-cloud1.jf.intel.com ([10.54.39.91]) by smtpauth.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 03 Dec 2026 12:24:01 -0800 From: Matthew Brost To: intel-xe@lists.freedesktop.org, dri-devel@lists.freedesktop.org Cc: =?UTF-8?q?Thomas=20Hellstr=C3=B6m?= , Natalie Vock , Arun Pravin , Niranjana Vishwanathapura Subject: [PATCH v6 12/35] drm/gpusvm: Add a DMA-mapping accounting callback Date: Thu, 3 Dec 2026 12:23:29 -0800 Message-Id: <20261203202352.4153159-13-matthew.brost@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20261203202352.4153159-1-matthew.brost@intel.com> References: <20261203202352.4153159-1-matthew.brost@intel.com> X-Archived-By: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "dri-devel" X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 From: Thomas Hellström Drivers that use the GPU SVM core to DMA-map system memory for GPU access may want to keep track of how many pages they have mapped, for example to expose statistics or to detect leaks. Doing this accounting in the driver around drm_gpusvm_get_pages() / drm_gpusvm_unmap_pages() is error prone: the number, order and kind of the dma_addr[] entries can change between map and unmap (migration, partial unmaps, the iova vs non-iova paths), which makes symmetric accounting hard to get right. Add an optional @dma_map_account callback to struct drm_gpusvm_ops and invoke it once per &drm_pagemap_addr entry at the exact points where the entry is DMA-mapped (sign == +1) in drm_gpusvm_get_pages() and unmapped (sign == -1) in __drm_gpusvm_unmap_pages(). Since the map error path and every unmap/free path funnel through __drm_gpusvm_unmap_pages() for the entries that were actually mapped, the accounting is symmetric by construction. The callback receives the full &drm_pagemap_addr so the driver can use the order and the proto field to distinguish system memory mappings from device interconnect mappings. The callback must also be usable by the core drm_gpusvm_pages-only API (mm == NULL), as used e.g. for userptr-style mappings. Relax drm_gpusvm_init() to allow such users to pass a restricted @ops that only implements the page-level hooks; the full-SVM hooks (@invalidate) and the chunk configuration must still be unset in that mode. Cc: Natalie Vock Cc: Arun Pravin Signed-off-by: Thomas Hellström Signed-off-by: Matthew Brost Reviewed-by: Niranjana Vishwanathapura --- drivers/gpu/drm/drm_gpusvm.c | 41 ++++++++++++++++++++++++++++-------- include/drm/drm_gpusvm.h | 19 +++++++++++++++++ 2 files changed, 51 insertions(+), 9 deletions(-) diff --git a/drivers/gpu/drm/drm_gpusvm.c b/drivers/gpu/drm/drm_gpusvm.c index de2fa2684e84..92f46aad71f3 100644 --- a/drivers/gpu/drm/drm_gpusvm.c +++ b/drivers/gpu/drm/drm_gpusvm.c @@ -503,8 +503,16 @@ int drm_gpusvm_init(struct drm_gpusvm *gpusvm, return -EINVAL; mmgrab(mm); } else { - /* No full SVM mode, only core drm_gpusvm_pages API. */ - if (ops || num_chunks || mm_range || notifier_size) + /* + * No full SVM mode, only core drm_gpusvm_pages API. A + * restricted @ops that only implements the page-level hooks + * (e.g. @dma_map_account) is allowed; the full-SVM hooks must + * not be set. + */ + if (num_chunks || mm_range || notifier_size) + return -EINVAL; + if (ops && (ops->invalidate || ops->range_alloc || + ops->range_free)) return -EINVAL; } @@ -1240,9 +1248,12 @@ static void __drm_gpusvm_unmap_pages(struct drm_gpusvm *gpusvm, struct drm_gpusvm_pages_flags flags = { .__flags = svm_pages->flags.__flags, }; + bool contiguous; const struct drm_pagemap_addr *addrs = - drm_gpusvm_pages_first_dma(svm_pages, NULL); + drm_gpusvm_pages_first_dma(svm_pages, &contiguous); bool use_iova = dma_use_iova(&svm_pages->state); + bool needs_account = gpusvm->ops && + gpusvm->ops->dma_map_account; /* * IOVA is reserved for the whole range but only the linked @@ -1260,14 +1271,18 @@ static void __drm_gpusvm_unmap_pages(struct drm_gpusvm *gpusvm, /* * With IOVA and no device page the unlink above tore every - * entry down, and that is also when the range may be folded - * to one entry, which must not be walked per entry. dpagemap - * is set before the first device_map(), so it is also right - * on the error path, where the flags are not published yet. + * entry down, but the per-entry walk is still needed when the + * driver accounts the mappings. A folded (inline) mapping has + * a single entry describing the whole range, so it must be + * re-read for every iteration instead of indexed by @j. + * dpagemap is set before the first device_map(), so it is also + * right on the error path, where the flags are not published + * yet. */ for (i = 0, j = 0; - (!use_iova || dpagemap) && i < npages; j++) { - const struct drm_pagemap_addr *addr = &addrs[j]; + (!use_iova || dpagemap || needs_account) && i < npages; j++) { + const struct drm_pagemap_addr *addr = + contiguous ? &addrs[0] : &addrs[j]; if (addr->proto == DRM_INTERCONNECT_SYSTEM) { /* @@ -1283,6 +1298,10 @@ static void __drm_gpusvm_unmap_pages(struct drm_gpusvm *gpusvm, } else if (dpagemap && dpagemap->ops->device_unmap) dpagemap->ops->device_unmap(dpagemap, dev, addr); + + if (needs_account) + gpusvm->ops->dma_map_account(gpusvm, addr, -1); + i += 1 << addr->order; } @@ -1705,6 +1724,10 @@ static int drm_gpusvm_dma_map_pages(struct drm_gpusvm *gpusvm, (addr, DRM_INTERCONNECT_SYSTEM, order, dma_dir); } + if (gpusvm->ops && gpusvm->ops->dma_map_account) + gpusvm->ops->dma_map_account(gpusvm, + &svm_pages->dma_addr[j], + 1); i += 1 << order; num_dma_mapped = i; flags.has_dma_mapping = true; diff --git a/include/drm/drm_gpusvm.h b/include/drm/drm_gpusvm.h index b43432af5637..fb6c1fa78e2d 100644 --- a/include/drm/drm_gpusvm.h +++ b/include/drm/drm_gpusvm.h @@ -75,6 +75,25 @@ struct drm_gpusvm_ops { void (*invalidate)(struct drm_gpusvm *gpusvm, struct drm_gpusvm_notifier *notifier, const struct mmu_notifier_range *mmu_range); + + /** + * @dma_map_account: Account a DMA-mapping change (optional) + * @gpusvm: Pointer to the GPU SVM + * @addr: The address descriptor of the chunk being (un)mapped + * @sign: +1 when @addr has just been DMA-mapped, -1 when it is being + * unmapped + * + * Called once per &drm_pagemap_addr entry, when the entry is + * DMA-mapped by drm_gpusvm_get_pages() (@sign == +1) and when it is + * unmapped (@sign == -1), so the driver can keep symmetric accounting + * of the pages it has mapped for GPU access. The @addr->proto field + * can be used to distinguish system-memory mappings from device + * interconnect mappings. May be provided in both full SVM mode and the + * core drm_gpusvm_pages-only mode. + */ + void (*dma_map_account)(struct drm_gpusvm *gpusvm, + const struct drm_pagemap_addr *addr, + int sign); }; /** -- 2.34.1