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 CB67E606A32F for ; Thu, 3 Dec 2026 00:30:18 +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=N6rEAlrb Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 3578C10E210; Thu, 3 Dec 2026 00:30:13 +0000 (UTC) Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.13]) by gabe.freedesktop.org (Postfix) with ESMTPS id 69EB51126C9; Thu, 3 Dec 2026 00:30:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1796257807; x=1827793807; h=date:from:to:cc:subject:message-id:references: content-transfer-encoding:in-reply-to:mime-version; bh=HG8/EXQVsNEPjZOhmtjlabCCPpTTFzLbyhZRzX7WxHA=; b=N6rEAlrb/feMCDYZJaQLCApbsXx50Z38lrstqggKd3tM2fZzxsNUYhk5 CJNxTZAOH3EFpW1tqXHWHwceIq5t0aE2MBuFYNZq3YAOcZMPCfchYYwqK zeEbL4AHoep1V3Lq0tyud+QeEGvp71JXnpWak13+YFQ9tPPUiAoRlQJaq YqLLoUbKPf7sBU1xxNVC9hwAOmK1PrBu6Odr4mqcoOxDKe3nTiEWVhd4D aIKMoTMWo7XgUOnIT2s/xb4y8HIuBlCK0EwvzfW8stQ1Ee5Eyh/6XjOCT Zjx2qGN3flCHu/5yqOR+8Gig3CzMUNvTgPmZ5eR4kDzFpvcdibXOuzBP8 A==; X-CSE-ConnectionGUID: y5UnVN5rQ0SjivSHA7YXTw== X-CSE-MsgGUID: 3JKTcfObRnGxlExpdN++6A== X-IronPort-AV: E=McAfee;i="6800,10657,11984"; a="5111417" X-IronPort-AV: E=Sophos;i="6.27,247,1787036400"; d="scan'208";a="5111417" Received: from orviesa005.jf.intel.com ([10.64.159.145]) by orvoesa105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Dec 2026 16:30:06 -0800 X-CSE-ConnectionGUID: i3FC/7MJQHaepcigezbV2A== X-CSE-MsgGUID: iyUXHi7+QgmCPjiWwUp5hQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.27,247,1787036400"; d="scan'208";a="13483345" Received: from fmsmsx901.amr.corp.intel.com ([10.18.126.90]) by orviesa005.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Dec 2026 16:30:06 -0800 Received: from FMSMSX902.amr.corp.intel.com (10.18.126.91) by fmsmsx901.amr.corp.intel.com (10.18.126.90) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.57; Wed, 2 Dec 2026 16:30:05 -0800 Received: from fmsedg902.ED.cps.intel.com (10.1.192.144) by FMSMSX902.amr.corp.intel.com (10.18.126.91) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.57 via Frontend Transport; Wed, 2 Dec 2026 16:30:05 -0800 Received: from BN1PR04CU002.outbound.protection.outlook.com (52.101.56.20) by edgegateway.intel.com (192.55.55.82) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.57; Wed, 2 Dec 2026 16:30:04 -0800 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=HuP8UQCyj0Vx4umxvxEuxSWCTThZt0qrrKzg+g6Mj5YrFsG97MbZSMQTsmFLmh9UmeX5/dq69JzVoSZm182B/Y8MjJTdbqi/Zzyo3M7C3BVdgo/whPKnJYp2W8U/lzXXfSIH4VnAua60BHtus4s0IZBgjTH13eW+OHRWJel8gYvUDFhrdmA1grVZbBBvFIY72NciKQmUm3rHjxpYFfR5op64oRgv1K6/I5SnQSVPLVudyUvJR6n2tq8RA0pbIX4LgkKVOtMH0b2/JYYi1AuGjK8dSVS2MI1PAuq3qTcdoulVjEYB1kO/yIRSDDsEgyFqyHEQ0+s3EiZLtQPu6Z1xDQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=A4F2SzivouMFxdfj1Tl3sypOaIzFjfBX41L0lr+XbYY=; b=EIbHfS6jbG/EwswCjfi70zt2GgLuzIKyyw/VT2FS+P5kfj039L5BjYHDYz5ddQedBDbOp19jCxBzBj+flDnxWPfTOpB47B8dn2DIBfFbpxKtYo+tlsfYQD4PkQ8xyVo530/8CVx0IbvT6M5qZqvhd2MPWscXm7uIgp/nHdjcgFaycxawCw/udyCUxAbFee4uourRx+2Tqaby0cOQfW+YsAvX7GrhwE+UOEC/yKAY2NZ2YyzYa2WWDsawpTJwMYosimZGjpjhznxhP4GfiQUdGX19vTboAx0MfIP3zPTD22WcqeKYaa4NEv04LM8DQMx9wTfnnC5VG75CZ92WrXelaA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=intel.com; dmarc=pass action=none header.from=intel.com; dkim=pass header.d=intel.com; arc=none Authentication-Results: mx.microsoft.com 1; dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=intel.com; Received: from CO1PR11MB4787.namprd11.prod.outlook.com (2603:10b6:303:95::23) by IA1PR11MB7319.namprd11.prod.outlook.com (2603:10b6:208:425::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.688.10; Thu, 3 Dec 2026 00:29:56 +0000 Received: from CO1PR11MB4787.namprd11.prod.outlook.com ([fe80::e7eb:a872:53d1:21fd]) by CO1PR11MB4787.namprd11.prod.outlook.com ([fe80::e7eb:a872:53d1:21fd%4]) with mapi id 15.21.0688.009; Thu, 3 Dec 2026 00:29:56 +0000 Date: Wed, 2 Dec 2026 16:29:54 -0800 From: Matthew Brost To: Niranjana Vishwanathapura CC: , , Thomas =?iso-8859-1?Q?Hellstr=F6m?= , Natalie Vock , Arun Pravin Subject: Re: [PATCH v5 13/36] drm/xe: Add debugfs stats for DMA-mapped pages per order Message-ID: References: <20261202233323.3743190-1-matthew.brost@intel.com> <20261202233323.3743190-14-matthew.brost@intel.com> Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: X-ClientProxiedBy: MW4PR03CA0292.namprd03.prod.outlook.com (2603:10b6:303:b5::27) To CO1PR11MB4787.namprd11.prod.outlook.com (2603:10b6:303:95::23) X-Archived-By: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CO1PR11MB4787:EE_|IA1PR11MB7319:EE_ X-MS-Office365-Filtering-Correlation-Id: be1c3e9c-9a4a-475c-8031-08df50d48e48 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230040|23010399003|366016|1800799024|376014|11063799006|4143699003|18002099003|6133799003|261009221227099003|261009223027099003|22082099003|10067099003|56012099006|261022075727099003|261102123227099003|261026140227099003|261026121527099003|261028144727099003|261022105027099003|261021121427099003|261021123027099003; X-Microsoft-Antispam-Message-Info: 3Q8J1rSDBe7uSQlKtQ65x3r4FeZ5o4BmZZl6uR+npTZHLWCRDb0zV7VCMb1V4SCRC1sZ1TbYDu80xkYnN64l5duye+qlvdlUXIWrkG6w3a7HmCWYZLXBFPCzhlE8Rq91+d+2T2xRoC9R+1tjesb5VZyCg8GzSqi6CbPtHpoFpd5OK24X6vj8stNcaODgC3nRwP/shsvmI8UiFfiKtIPDv91sq70FoATIQhIh+iB7WkEMNhUkF+pZdUH9Pj5Em2T/u1H8vqsm+/urBTFqUDOPVMcJ/DL0pBi4frYS0bpvNjrSH9bjfgyDlRa41KghGy0O7/FzvxTf1Vclwpq1NQfSzWLkCIdymkzboZ40U9hT8l19/Syv1MG3rn84CmR3N4BRpFLWjkBAyQfX3OsKrjM1Sg/RLzNqwERKVYnzTckAWa8XjAffkVbmUDrWD6Qm/fBPsS3yvg3VDu5cJALahZHl3A7ESYRaeAANv5AgC4jeaHZUjIc1UuZ91Da1TQRZvA4jPpz5XqSqQ1YbBMtbygCCZiA96aDct5cQ/ZMdKnnc8BXdue9bv5ndHU6Z8DWCM+PFWRL6MXRADAeB9m1WI8IeIkGX5XPSY7L9dNMS81eEFsp2khrD3XdGufuXYzqCaWgZAdTMG90+ixoDnePB8fCjYPP3bYiFzEzQfzX2tIDEx/M= X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:CO1PR11MB4787.namprd11.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230040)(23010399003)(366016)(1800799024)(376014)(11063799006)(4143699003)(18002099003)(6133799003)(261009221227099003)(261009223027099003)(22082099003)(10067099003)(56012099006)(261022075727099003)(261102123227099003)(261026140227099003)(261026121527099003)(261028144727099003)(261022105027099003)(261021121427099003)(261021123027099003); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?iso-8859-1?Q?hU34ycpCOH1Fmurtac5gZ0E1DGwxVwLgUOYUx/FxGcpH8quhWI8uYurC7r?= =?iso-8859-1?Q?Mjb3xCDxPEwZFJG+xoeKiOSA2PppAVGvb6w9kSN3Ixa5Zxjm4eXvWW2Kzx?= =?iso-8859-1?Q?Ul0YuSZjn+Z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19makBatbn+JwUUFWXhyQABuHXj5i8x84x59mVt2YWSKfTDk4E+p3c29HHyp1S0Vu/ZBhF9MxC3Om6jrdS1RBmzVkpp8BJxoQuoONpj0jLkrMLr8UsdA2ko9+EGnCI+HQlRB+4yYe7MeN9IMjdPhE7UvSJMgffEdFNuh4utI4ji5s5WLMR4eEPWU/Ry2taqIRPHJDax+FG9yh584RNjThdV8jCcldIp8pjP1Mkmt168pE3i/Yk3iQ/fZsqTYXlVTvSRVLsAw/Aoks+6Vhvm5PRGj9JYJ79Nd7dfyGg0OER/RKGCdzDHuhG3ow== X-MS-Exchange-CrossTenant-Network-Message-Id: be1c3e9c-9a4a-475c-8031-08df50d48e48 X-MS-Exchange-CrossTenant-AuthSource: CO1PR11MB4787.namprd11.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 03 Dec 2026 00:29:56.1656 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 46c98d88-e344-4ed4-8496-4ed7712e255d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: JcUqYgm7O4vfajAMqOypFFeT7l2JKrJQvDxtMR9BauLaUn27xKaTGobRo7Uq8z2AxSmR73z6w3kF8RA7z/S5GQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA1PR11MB7319 X-OriginatorOrg: intel.com 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=-15.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 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 On Wed, Dec 02, 2026 at 04:25:31PM -0800, Niranjana Vishwanathapura wrote: > On Wed, Dec 02, 2026 at 03:33:00PM -0800, Matthew Brost wrote: > > From: Thomas Hellström > > > > Expose per-page-order DMA mapping counts for the system memory that the > > xe driver maps for GPU access, split into two categories: TTM buffer > > objects, and the GPU SVM / userptr ranges (which share a single > > drm_gpusvm instance per VM). > > > > The stats are visible at: > > > > /dri//dma_mapped_pages > > > > and are broken out into two rows (bo, svm/userptr), one column per page > > order, mirroring the layout of the TTM pool page_pool stat. > > > > For TTM BOs the per-order counts are derived from the DMA scatter-gather > > table: each mapped segment is physically contiguous, so its page count is > > split into the largest power-of-two runs (a segment of N pages contributes > > a 2^k chunk for the largest k with 2^k <= N, repeated for the remainder). > > Driving the accounting off the sg table rather than the pages[] array keeps > > it correct even when pages[] entries are cleared while still DMA-mapped, > > as happens on the defrag page-borrowing path. Dma-buf BOs are not > > accounted for in dma_mapped_pages stats. > > > > For the SVM and userptr ranges the accounting is driven by the > > drm_gpusvm @dma_map_account callback, which fires once per dma_addr[] > > entry at the exact point the entry is DMA-mapped and unmapped. This > > makes the accounting symmetric by construction and avoids driver-side > > walks that could drift across migration, partial unmaps and the iova vs > > non-iova paths. Only DRM_INTERCONNECT_SYSTEM entries are real DMA maps > > and counted; device interconnect (VRAM, P2P) entries are skipped, so the > > counter reflects only system pages actually mapped through the DMA layer. > > > > The callback is shared between the full SVM instance (fault mode) and the > > core drm_gpusvm_pages-only instance used for userptr, so userptr > > mappings are accounted in both the CONFIG_DRM_XE_GPUSVM=y and =n (but > > CONFIG_DRM_GPUSVM=y) configurations, into the same svm/userptr counter. > > > > Since the counts are only ever consumed through debugfs, gate the counter > > storage and all accounting on CONFIG_DEBUG_FS so that builds without > > debugfs carry no extra atomic traffic or device state; the drm_gpusvm > > callback is simply left unregistered in that case. > > > > On device teardown assert that every counter has returned to zero, to > > catch any unbalanced accounting (a leaked DMA mapping) during testing. > > > > Cc: Natalie Vock > > Cc: Arun Pravin > > Signed-off-by: Thomas Hellström > > Signed-off-by: Matthew Brost > > --- > > drivers/gpu/drm/xe/xe_bo.c | 53 ++++++++++++++++++++--- > > drivers/gpu/drm/xe/xe_debugfs.c | 26 ++++++++++++ > > drivers/gpu/drm/xe/xe_device.c | 24 +++++++++++ > > drivers/gpu/drm/xe/xe_device_types.h | 17 ++++++++ > > drivers/gpu/drm/xe/xe_svm.c | 8 +++- > > drivers/gpu/drm/xe/xe_svm.h | 4 +- > > drivers/gpu/drm/xe/xe_userptr.c | 63 ++++++++++++++++++++++++++++ > > drivers/gpu/drm/xe/xe_userptr.h | 7 ++++ > > 8 files changed, 194 insertions(+), 8 deletions(-) > > > > diff --git a/drivers/gpu/drm/xe/xe_bo.c b/drivers/gpu/drm/xe/xe_bo.c > > index c89b39b503fe..ff5610ba4b74 100644 > > --- a/drivers/gpu/drm/xe/xe_bo.c > > +++ b/drivers/gpu/drm/xe/xe_bo.c > > @@ -398,6 +398,50 @@ struct xe_ttm_tt { > > bool purgeable; > > }; > > > > +/* > > + * xe_tt_account_dma_pages - account DMA-mapped tt pages per order > > + * @xe: the xe device > > + * @xe_tt: the xe_ttm_tt whose sg table to walk > > + * @sign: +1 to add, -1 to subtract > > + * > > + * Walk the tt's DMA scatter-gather table and split each (physically > > + * contiguous) segment into the largest power-of-two page runs: a segment of N > > + * pages contributes a 2^k chunk for the largest k with 2^k <= N, repeated for > > + * the remainder. Accumulate the count of 2^k pages into > > + * xe->mem.dma_mapped_pages[k]. Driving this off the sg table rather than > > + * tt->pages keeps the accounting correct even when pages[] entries are cleared > > + * while still DMA-mapped (e.g. the defrag page-borrowing path). > > + */ > > +#if IS_ENABLED(CONFIG_DEBUG_FS) > > +static void xe_tt_account_dma_pages(struct xe_device *xe, > > + struct xe_ttm_tt *xe_tt, int sign) > > +{ > > + struct scatterlist *sg; > > + unsigned int i; > > + > > + for_each_sgtable_sg(xe_tt->sg, sg, i) { > > + unsigned long nr_pages = sg->length >> PAGE_SHIFT; > > + > > + while (nr_pages) { > > + unsigned int order = min_t(unsigned int, > > + __fls(nr_pages), > > + MAX_PAGE_ORDER); > > + unsigned long chunk = 1UL << order; > > + > > + atomic_long_add((long)chunk * sign, > > + &xe->mem.dma_mapped_pages[order]); > > NIT...'chunk' here uses 'order' which is clamped value. > In xe_userptr_dma_map_account(), '1UL << addr->order' is used and > addr->order is not clamped. I think it is fine, just that this > asymmetry may raise eyebrows. > > > + > > + nr_pages -= chunk; > > + } > > + } > > +} > > +#else > > +static void xe_tt_account_dma_pages(struct xe_device *xe, > > + struct xe_ttm_tt *xe_tt, int sign) > > +{ > > +} > > +#endif > > + > > static int xe_tt_map_sg(struct xe_device *xe, struct ttm_tt *tt) > > { > > struct xe_ttm_tt *xe_tt = container_of(tt, struct xe_ttm_tt, ttm); > > @@ -427,6 +471,7 @@ static int xe_tt_map_sg(struct xe_device *xe, struct ttm_tt *tt) > > return ret; > > } > > > > + xe_tt_account_dma_pages(xe, xe_tt, 1); > > return 0; > > } > > > > @@ -435,6 +480,7 @@ static void xe_tt_unmap_sg(struct xe_device *xe, struct ttm_tt *tt) > > struct xe_ttm_tt *xe_tt = container_of(tt, struct xe_ttm_tt, ttm); > > > > if (xe_tt->sg) { > > + xe_tt_account_dma_pages(xe, xe_tt, -1); > > dma_unmap_sgtable(xe->drm.dev, xe_tt->sg, > > DMA_BIDIRECTIONAL, 0); > > sg_free_table(xe_tt->sg); > > @@ -1890,11 +1936,8 @@ int xe_bo_dma_unmap_pinned(struct xe_bo *bo) > > ttm_bo->sg = NULL; > > xe_tt->sg = NULL; > > } else if (xe_tt->sg) { > > - dma_unmap_sgtable(ttm_to_xe_device(ttm_bo->bdev)->drm.dev, > > - xe_tt->sg, > > - DMA_BIDIRECTIONAL, 0); > > - sg_free_table(xe_tt->sg); > > - xe_tt->sg = NULL; > > + xe_tt_unmap_sg(ttm_to_xe_device(ttm_bo->bdev), > > + &xe_tt->ttm); > > } > > } > > > > diff --git a/drivers/gpu/drm/xe/xe_debugfs.c b/drivers/gpu/drm/xe/xe_debugfs.c > > index bc62d1054d29..be533435097f 100644 > > --- a/drivers/gpu/drm/xe/xe_debugfs.c > > +++ b/drivers/gpu/drm/xe/xe_debugfs.c > > @@ -8,6 +8,7 @@ > > #include > > #include > > #include > > +#include > > #include > > #include > > > > @@ -349,6 +350,28 @@ static int dgfx_pcie_link_residencies_show(struct seq_file *m, void *data) > > return 0; > > } > > > > +static int dma_mapped_pages_show(struct seq_file *m, void *data) > > +{ > > + struct xe_device *xe = m->private; > > + unsigned int i; > > + > > + seq_printf(m, "%-11s ", ""); > > + for (i = 0; i < NR_PAGE_ORDERS; i++) > > + seq_printf(m, " ---%2u---", i); > > + seq_printf(m, "\n%-11s:", "bo"); > > + for (i = 0; i < NR_PAGE_ORDERS; i++) > > + seq_printf(m, " %8ld", > > + atomic_long_read(&xe->mem.dma_mapped_pages[i])); > > + seq_printf(m, "\n%-11s:", "svm/userptr"); > > + for (i = 0; i < NR_PAGE_ORDERS; i++) > > + seq_printf(m, " %8ld", > > + atomic_long_read(&xe->mem.dma_mapped_pages_svm[i])); > > + seq_puts(m, "\n"); > > + > > + return 0; > > +} > > +DEFINE_SHOW_ATTRIBUTE(dma_mapped_pages); > > + > > static const struct drm_info_list debugfs_list[] = { > > {"info", info, 0}, > > { .name = "sriov_info", .show = sriov_info, }, > > @@ -768,6 +791,9 @@ void xe_debugfs_register(struct xe_device *xe) > > > > xe_ttm_vram_debugfs_init(xe, root); > > > > + debugfs_create_file("dma_mapped_pages", 0444, root, xe, > > + &dma_mapped_pages_fops); > > + > > for_each_tile(tile, xe, tile_id) > > xe_tile_debugfs_register(tile); > > > > diff --git a/drivers/gpu/drm/xe/xe_device.c b/drivers/gpu/drm/xe/xe_device.c > > index 86de5f183c0a..781324ef950d 100644 > > --- a/drivers/gpu/drm/xe/xe_device.c > > +++ b/drivers/gpu/drm/xe/xe_device.c > > @@ -382,6 +382,28 @@ bool xe_device_is_admin_only(const struct xe_device *xe) > > /* Number of allocated struct xe_device */ > > static atomic_t xe_device_count; > > > > +#if IS_ENABLED(CONFIG_DEBUG_FS) > > +static void xe_device_assert_dma_pages_zero(struct xe_device *xe) > > +{ > > + unsigned int i; > > + > > + /* > > + * All BOs, userptr VMAs and SVM ranges must have been torn down by the > > + * time the device is destroyed, so every DMA-mapped-pages counter must > > + * have returned to zero. A non-zero value indicates unbalanced > > + * accounting, i.e. a missing unmap-side decrement. > > + */ > > + for (i = 0; i < NR_PAGE_ORDERS; i++) { > > + xe_assert(xe, !atomic_long_read(&xe->mem.dma_mapped_pages[i])); > > + xe_assert(xe, !atomic_long_read(&xe->mem.dma_mapped_pages_svm[i])); > > + } > > +} > > +#else > > +static void xe_device_assert_dma_pages_zero(struct xe_device *xe) > > +{ > > +} > > +#endif > > + > > static void xe_device_destroy(struct drm_device *dev, void *dummy) > > { > > struct xe_device *xe = to_xe_device(dev); > > @@ -405,6 +427,8 @@ static void xe_device_destroy(struct drm_device *dev, void *dummy) > > > > ttm_device_fini(&xe->ttm); > > > > + xe_device_assert_dma_pages_zero(xe); > > + > > if (atomic_dec_and_test(&xe_device_count)) > > wake_up_var(&xe_device_count); > > } > > diff --git a/drivers/gpu/drm/xe/xe_device_types.h b/drivers/gpu/drm/xe/xe_device_types.h > > index e4e2fba44c8e..f00fc544d21c 100644 > > --- a/drivers/gpu/drm/xe/xe_device_types.h > > +++ b/drivers/gpu/drm/xe/xe_device_types.h > > @@ -6,6 +6,7 @@ > > #ifndef _XE_DEVICE_TYPES_H_ > > #define _XE_DEVICE_TYPES_H_ > > > > +#include > > #include > > #include > > > > @@ -307,6 +308,22 @@ struct xe_device { > > struct xe_shrinker *shrinker; > > /** @mem.stolen_mgr: stolen memory manager. */ > > struct xe_ttm_stolen_mgr *stolen_mgr; > > +#if IS_ENABLED(CONFIG_DEBUG_FS) > > + /** > > + * @mem.dma_mapped_pages: number of DMA-mapped pages per page > > + * order currently live for this device, for TTM BOs. Only > > + * accounted when CONFIG_DEBUG_FS is enabled, since it is solely > > + * exposed through debugfs. > > + */ > > + atomic_long_t dma_mapped_pages[NR_PAGE_ORDERS]; > > + /** > > + * @mem.dma_mapped_pages_svm: number of DMA-mapped pages per > > + * page order currently live for this device, for SVM ranges > > + * and userptr VMAs (both share a single drm_gpusvm instance). > > + * Only accounted when CONFIG_DEBUG_FS is enabled. > > + */ > > + atomic_long_t dma_mapped_pages_svm[NR_PAGE_ORDERS]; > > +#endif > > } mem; > > > > /** @sriov: device level virtualization data */ > > diff --git a/drivers/gpu/drm/xe/xe_svm.c b/drivers/gpu/drm/xe/xe_svm.c > > index bc6902b5bf57..3f90b8314132 100644 > > --- a/drivers/gpu/drm/xe/xe_svm.c > > +++ b/drivers/gpu/drm/xe/xe_svm.c > > @@ -24,6 +24,7 @@ > > #include "xe_tile.h" > > #include "xe_tlb_inval.h" > > #include "xe_ttm_vram_mgr.h" > > +#include "xe_userptr.h" > > #include "xe_vm.h" > > #include "xe_vm_types.h" > > #include "xe_vram_types.h" > > @@ -850,6 +851,9 @@ static const struct drm_gpusvm_ops gpusvm_ops = { > > .range_alloc = xe_svm_range_alloc, > > .range_free = xe_svm_range_free, > > .invalidate = xe_svm_invalidate, > > +#if IS_ENABLED(CONFIG_DEBUG_FS) > > + .dma_map_account = xe_userptr_dma_map_account, > > +#endif > > }; > > > > static const unsigned long fault_chunk_sizes[] = { > > @@ -947,8 +951,8 @@ int xe_svm_init(struct xe_vm *vm) > > } > > } else { > > err = drm_gpusvm_init(&vm->svm.gpusvm, "Xe SVM (simple)", > > - NULL, 0, 0, 0, NULL, > > - NULL, 0); > > + NULL, 0, 0, 0, > > + xe_userptr_gpusvm_ops_get(), NULL, 0); > > if (err) > > goto out_err; > > } > > diff --git a/drivers/gpu/drm/xe/xe_svm.h b/drivers/gpu/drm/xe/xe_svm.h > > index ffc1cc808102..f8b908a74ba9 100644 > > --- a/drivers/gpu/drm/xe/xe_svm.h > > +++ b/drivers/gpu/drm/xe/xe_svm.h > > @@ -249,6 +249,7 @@ struct drm_pagemap *xe_drm_pagemap_from_fd(int fd, u32 region_instance); > > > > #else > > #include > > +#include "xe_userptr.h" > > #include "xe_vm.h" > > > > struct drm_pagemap_addr; > > @@ -296,7 +297,8 @@ int xe_svm_init(struct xe_vm *vm) > > { > > #if IS_ENABLED(CONFIG_DRM_GPUSVM) > > return drm_gpusvm_init(&vm->svm.gpusvm, "Xe SVM (simple)", > > - NULL, 0, 0, 0, NULL, NULL, 0); > > + NULL, 0, 0, 0, > > + xe_userptr_gpusvm_ops_get(), NULL, 0); > > #else > > return 0; > > #endif > > diff --git a/drivers/gpu/drm/xe/xe_userptr.c b/drivers/gpu/drm/xe/xe_userptr.c > > index 39806ca9f54a..28770bffeced 100644 > > --- a/drivers/gpu/drm/xe/xe_userptr.c > > +++ b/drivers/gpu/drm/xe/xe_userptr.c > > @@ -8,9 +8,72 @@ > > > > #include > > > > +#include > > + > > #include "xe_tlb_inval.h" > > #include "xe_trace_bo.h" > > > > +#if IS_ENABLED(CONFIG_DEBUG_FS) > > +/* > > + * xe_userptr_dma_map_account() - drm_gpusvm DMA-mapping accounting callback > > + * @gpusvm: The GPU SVM the mapping belongs to > > + * @addr: The address descriptor of the chunk being (un)mapped > > + * @sign: +1 when @addr was DMA-mapped, -1 when it is being unmapped > > + * > > + * Account, per page order, the system-memory pages DMA-mapped for GPU access > > + * through the drm_gpusvm_pages instance shared by userptr (and, in fault mode, > > + * SVM). Only DRM_INTERCONNECT_SYSTEM entries are real DMA maps and counted; > > + * device interconnect (VRAM, P2P) entries are skipped. The counts are solely > > + * exposed through debugfs, so the callback is only registered when > > + * CONFIG_DEBUG_FS is enabled. > > + */ > > +void xe_userptr_dma_map_account(struct drm_gpusvm *gpusvm, > > + const struct drm_pagemap_addr *addr, > > + int sign) > > +{ > > + struct xe_vm *vm = container_of(gpusvm, struct xe_vm, svm.gpusvm); > > NIT...should gpusvm_to_vm() be moved from xe_svm.c to xe_svm.h and used here > instead? > Not a huge ROI on that and xe_svm doesn't build in some Kconfigs, I suppose if it is inline in xe_svm.h it probably doesn't matter but I'd rather not have include xe_svm.h just container_of. Matt > In anycase, > Reviewed-by: Niranjana Vishwanathapura > > Niranjana > > > + unsigned int order = addr->order; > > + > > + if (addr->proto != DRM_INTERCONNECT_SYSTEM) > > + return; > > + > > + /* > > + * The CPU-PTE map order reported by hmm can exceed MAX_PAGE_ORDER > > + * (e.g. an order-18 1GB mapping), so clamp the array index to avoid an > > + * out-of-bounds access. Clamping is symmetric across the +1/-1 calls, > > + * so the per-order counters stay balanced. > > + */ > > + order = min(order, (unsigned int)NR_PAGE_ORDERS - 1); > > + > > + atomic_long_add((long)(1UL << addr->order) * sign, > > + &vm->xe->mem.dma_mapped_pages_svm[order]); > > +} > > + > > +static const struct drm_gpusvm_ops xe_userptr_gpusvm_ops = { > > + .dma_map_account = xe_userptr_dma_map_account, > > +}; > > +#endif > > + > > +/** > > + * xe_userptr_gpusvm_ops_get() - Accounting ops for the simple gpusvm instance > > + * > > + * The core drm_gpusvm_pages-only instance used for userptr (and, in fault > > + * mode, shared with SVM) is initialised with these ops so that DMA-mapped > > + * system pages are accounted and exposed through debugfs. Returns NULL when > > + * CONFIG_DEBUG_FS is disabled, in which case no accounting is kept and the > > + * instance is initialised without ops. > > + * > > + * Return: Pointer to the restricted &drm_gpusvm_ops, or NULL. > > + */ > > +const struct drm_gpusvm_ops *xe_userptr_gpusvm_ops_get(void) > > +{ > > +#if IS_ENABLED(CONFIG_DEBUG_FS) > > + return &xe_userptr_gpusvm_ops; > > +#else > > + return NULL; > > +#endif > > +} > > + > > static void xe_userptr_assert_in_notifier(struct xe_vm *vm) > > { > > lockdep_assert(lockdep_is_held_type(&vm->svm.gpusvm.notifier_lock, 0) || > > diff --git a/drivers/gpu/drm/xe/xe_userptr.h b/drivers/gpu/drm/xe/xe_userptr.h > > index 179f2aca26a3..52d9734ccecc 100644 > > --- a/drivers/gpu/drm/xe/xe_userptr.h > > +++ b/drivers/gpu/drm/xe/xe_userptr.h > > @@ -151,6 +151,12 @@ struct xe_userptr { > > }; > > > > #if IS_ENABLED(CONFIG_DRM_GPUSVM) > > + > > +#if IS_ENABLED(CONFIG_DEBUG_FS) > > +void xe_userptr_dma_map_account(struct drm_gpusvm *gpusvm, > > + const struct drm_pagemap_addr *addr, int sign); > > +#endif > > + > > void xe_userptr_remove(struct xe_userptr_vma *uvma); > > int xe_userptr_setup(struct xe_userptr_vma *uvma, unsigned long start, > > unsigned long range); > > @@ -161,6 +167,7 @@ int __xe_vm_userptr_needs_repin(struct xe_vm *vm); > > int xe_vm_userptr_check_repin(struct xe_vm *vm); > > int xe_vma_userptr_pin_pages(struct xe_userptr_vma *uvma); > > int xe_vma_userptr_check_repin(struct xe_userptr_vma *uvma); > > +const struct drm_gpusvm_ops *xe_userptr_gpusvm_ops_get(void); > > #else > > static inline void xe_userptr_remove(struct xe_userptr_vma *uvma) {} > > > > -- > > 2.34.1 > >