DONOT-Review: Pin vram limit
Subsystems (2)
PATCH v4 1/1 DONOT-Review: Pin vram limit
drm/xe/dma-buf: propagate pin error
Return the error from xe_bo_pin_external() instead of asserting success, so callers observe failures such as -ENOSPC from the pin limit.
drm/xe: add pin_vram_percent modparam
Add a module parameter that caps the percentage of each VRAM region which may be pinned via dma-buf. A value of 0 disables the limit.
drm/xe/vram: add pinned vram counters
Add per-region counters tracking dma-buf and total pinned VRAM bytes along with the enforced dma-buf pin limit.
drm/xe/vram: init dma-buf pin limit
Derive the per-region dma-buf pin limit from pin_vram_percent during VRAM manager initialization.
drm/xe/bo: account dma-buf pinned vram
Track dma-buf pinned VRAM in the per-region counters on external pin and unpin.
drm/xe/bo: account kernel pinned vram
Track kernel pinned VRAM in the per-region total counter on pin and unpin.
drm/xe/bo: enforce dma-buf pin limit
Reject external VRAM pins that would exceed the per-region dma-buf limit with -ENOSPC.
drm/xe/vram: show pinned vram in debug
Expose the pinned VRAM counters and the dma-buf limit in the VRAM manager debug output.
drm/xe/tests: add VRAM pin limit KUnit tests
Test under-limit success, over-limit rejection, counter recovery after unpin, and the limit=0 unlimited case in xe_bo tests. Add an end-to-end dma-buf pin limit test in xe_dma_buf tests.
--- drivers/gpu/drm/xe/tests/xe_bo.c | 230 +++++++++++++++++++++ drivers/gpu/drm/xe/tests/xe_dma_buf.c | 96 +++++++++ drivers/gpu/drm/xe/xe_bo.c | 72 ++++++- drivers/gpu/drm/xe/xe_bo_types.h | 8 + drivers/gpu/drm/xe/xe_defaults.h | 1 + drivers/gpu/drm/xe/xe_dma_buf.c | 5 +- drivers/gpu/drm/xe/xe_module.c | 5 + drivers/gpu/drm/xe/xe_module.h | 1 + drivers/gpu/drm/xe/xe_ttm_vram_mgr.c | 21 ++ drivers/gpu/drm/xe/xe_ttm_vram_mgr_types.h | 9 + 10 files changed, 439 insertions(+), 9 deletions(-)
drivers/gpu/drm/xe/tests/xe_bo.c +230 -0
@@ -17,7 +17,9 @@#include "tests/xe_pci_test.h" #include "tests/xe_test.h" +#include "xe_bo.h" #include "xe_bo_evict.h" +#include "xe_ttm_vram_mgr.h" #include "xe_gt.h" #include "xe_pci.h" #include "xe_pm.h"
@@ -828,9 +830,237 @@ static void xe_bo_shrink_kunit(struct kunit *test)shrink_test_run_device (xe); } +/* Returns NULL if the device has no VRAM (skips the test). */ +static struct xe_ttm_vram_mgr *pin_limit_get_mgr (struct xe_device *xe, +struct kunit *test) +{ +struct ttm_resource_manager *man =ttm_manager_type (&xe->ttm,XE_PL_VRAM0 ); + +if (!man) { +kunit_skip (test,"no VRAM\n" ); +return NULL ; + } +return to_xe_ttm_vram_mgr (man); +} + +static struct xe_bo *pin_limit_bo_create (struct xe_device *xe, +struct kunit *test,u64 size) +{ +struct xe_bo *bo =xe_bo_create_user (xe,NULL , size, +DRM_XE_GEM_CPU_CACHING_WC , +XE_BO_FLAG_VRAM0 ,NULL ); +if (IS_ERR (bo)) +KUNIT_FAIL (test,"bo create err=%pe\n" , bo); +return bo; +} + +static void xe_bo_pin_limit_under_kunit (struct kunit *test) +{ +struct xe_device *xe = test->priv; +struct xe_ttm_vram_mgr *mgr; +struct xe_bo *bo; +u64 orig_limit; +int err; + +guard (xe_pm_runtime)(xe); + + mgr =pin_limit_get_mgr (xe, test); +if (!mgr) +return ; + + bo =pin_limit_bo_create (xe, test,SZ_64K ); +if (IS_ERR (bo)) +return ; + +spin_lock (&xe->pinned.lock); + orig_limit = mgr->pin.dmabuf_limit; +/* limit = current usage + exactly one BO */ + mgr->pin.dmabuf_limit = mgr->pin.dmabuf_used +SZ_64K ; +spin_unlock (&xe->pinned.lock); + +xe_bo_lock (bo, false); + err =xe_bo_pin_external (bo, false,XE_VALIDATION_OPT_OUT ); +xe_bo_unlock (bo); +KUNIT_EXPECT_EQ (test, err,0 ); + +xe_bo_lock (bo, false); +xe_bo_unpin_external (bo); +xe_bo_unlock (bo); + +spin_lock (&xe->pinned.lock); +/* dmabuf_used must be back to what it was before the pin */ +KUNIT_EXPECT_EQ (test, mgr->pin.dmabuf_used, + mgr->pin.dmabuf_limit -SZ_64K ); + mgr->pin.dmabuf_limit = orig_limit; +spin_unlock (&xe->pinned.lock); + +xe_bo_put (bo); +} + +static void xe_bo_pin_limit_reject_kunit (struct kunit *test) +{ +struct xe_device *xe = test->priv; +struct xe_ttm_vram_mgr *mgr; +struct xe_bo *bo; +u64 orig_limit; +int err; + +guard (xe_pm_runtime)(xe); + + mgr =pin_limit_get_mgr (xe, test); +if (!mgr) +return ; + + bo =pin_limit_bo_create (xe, test,SZ_64K ); +if (IS_ERR (bo)) +return ; + +spin_lock (&xe->pinned.lock); + orig_limit = mgr->pin.dmabuf_limit; +/* limit = current usage + less than one BO, forcing rejection */ + mgr->pin.dmabuf_limit = mgr->pin.dmabuf_used +SZ_4K ; +spin_unlock (&xe->pinned.lock); + +xe_bo_lock (bo, false); + err =xe_bo_pin_external (bo, false,XE_VALIDATION_OPT_OUT ); +xe_bo_unlock (bo); +KUNIT_EXPECT_EQ (test, err, -ENOSPC ); + +spin_lock (&xe->pinned.lock); +/* dmabuf_used must be unchanged — no partial charge on rejection */ +KUNIT_EXPECT_EQ (test, mgr->pin.dmabuf_used, + mgr->pin.dmabuf_limit -SZ_4K ); + mgr->pin.dmabuf_limit = orig_limit; +spin_unlock (&xe->pinned.lock); + +xe_bo_put (bo); +} + +static void xe_bo_pin_limit_unpin_restores_kunit (struct kunit *test) +{ +struct xe_device *xe = test->priv; +struct xe_ttm_vram_mgr *mgr; +struct xe_bo *bo_a, *bo_b; +u64 orig_limit; +int err; + +guard (xe_pm_runtime)(xe); + + mgr =pin_limit_get_mgr (xe, test); +if (!mgr) +return ; + + bo_a =pin_limit_bo_create (xe, test,SZ_64K ); +if (IS_ERR (bo_a)) +return ; + + bo_b =pin_limit_bo_create (xe, test,SZ_64K ); +if (IS_ERR (bo_b)) { +xe_bo_put (bo_a); +return ; + } + +spin_lock (&xe->pinned.lock); + orig_limit = mgr->pin.dmabuf_limit; +/* limit = current usage + exactly one BO */ + mgr->pin.dmabuf_limit = mgr->pin.dmabuf_used +SZ_64K ; +spin_unlock (&xe->pinned.lock); + +xe_bo_lock (bo_a, false); + err =xe_bo_pin_external (bo_a, false,XE_VALIDATION_OPT_OUT ); +xe_bo_unlock (bo_a); +KUNIT_EXPECT_EQ (test, err,0 ); +if (err) + goto out; + +/* Second pin must be rejected while first is held. */ +xe_bo_lock (bo_b, false); + err =xe_bo_pin_external (bo_b, false,XE_VALIDATION_OPT_OUT ); +xe_bo_unlock (bo_b); +KUNIT_EXPECT_EQ (test, err, -ENOSPC ); + +xe_bo_lock (bo_a, false); +xe_bo_unpin_external (bo_a); +xe_bo_unlock (bo_a); + +/* After unpin, the second BO must fit. */ +xe_bo_lock (bo_b, false); + err =xe_bo_pin_external (bo_b, false,XE_VALIDATION_OPT_OUT ); +xe_bo_unlock (bo_b); +KUNIT_EXPECT_EQ (test, err,0 ); + +xe_bo_lock (bo_b, false); +xe_bo_unpin_external (bo_b); +xe_bo_unlock (bo_b); + +out: +spin_lock (&xe->pinned.lock); + mgr->pin.dmabuf_limit = orig_limit; +spin_unlock (&xe->pinned.lock); + +xe_bo_put (bo_b); +xe_bo_put (bo_a); +} + +static void xe_bo_pin_limit_zero_unlimited_kunit (struct kunit *test) +{ +struct xe_device *xe = test->priv; +struct xe_ttm_vram_mgr *mgr; +struct xe_bo *bo; +u64 orig_limit; +int err; + +guard (xe_pm_runtime)(xe); + + mgr =pin_limit_get_mgr (xe, test); +if (!mgr) +return ; + +/* BO is 2x SZ_64K so it would fail if limit were SZ_64K */ + bo =pin_limit_bo_create (xe, test,2 *SZ_64K ); +if (IS_ERR (bo)) +return ; + +spin_lock (&xe->pinned.lock); + orig_limit = mgr->pin.dmabuf_limit; +/* limit = current usage + one BO; 2*SZ_64K BO will be rejected */ + mgr->pin.dmabuf_limit = mgr->pin.dmabuf_used +SZ_64K ; +spin_unlock (&xe->pinned.lock); + +/* Confirm this size is rejected when a limit is set. */ +xe_bo_lock (bo, false); + err =xe_bo_pin_external (bo, false,XE_VALIDATION_OPT_OUT ); +xe_bo_unlock (bo); +KUNIT_EXPECT_EQ (test, err, -ENOSPC ); + +spin_lock (&xe->pinned.lock); + mgr->pin.dmabuf_limit =0 ;/* now disable limit */ +spin_unlock (&xe->pinned.lock); + +/* Same BO must succeed once limit is cleared. */ +xe_bo_lock (bo, false); + err =xe_bo_pin_external (bo, false,XE_VALIDATION_OPT_OUT ); +xe_bo_unlock (bo); +KUNIT_EXPECT_EQ (test, err,0 ); + +xe_bo_lock (bo, false); +xe_bo_unpin_external (bo); +xe_bo_unlock (bo); + +spin_lock (&xe->pinned.lock); + mgr->pin.dmabuf_limit = orig_limit; +spin_unlock (&xe->pinned.lock); + +xe_bo_put (bo); +} +static struct kunit_case xe_bo_tests[] = {KUNIT_CASE_PARAM (xe_ccs_migrate_kunit, xe_pci_live_device_gen_param),KUNIT_CASE_PARAM (xe_bo_evict_kunit, xe_pci_live_device_gen_param), +KUNIT_CASE_PARAM (xe_bo_pin_limit_under_kunit, xe_pci_live_device_gen_param), +KUNIT_CASE_PARAM (xe_bo_pin_limit_reject_kunit, xe_pci_live_device_gen_param), +KUNIT_CASE_PARAM (xe_bo_pin_limit_unpin_restores_kunit, xe_pci_live_device_gen_param), +KUNIT_CASE_PARAM (xe_bo_pin_limit_zero_unlimited_kunit, xe_pci_live_device_gen_param), {} };
drivers/gpu/drm/xe/tests/xe_dma_buf.c +96 -0
110 lines not on this page · raw message
drivers/gpu/drm/xe/xe_bo.c +67 -5
130 lines not on this page · raw message
drivers/gpu/drm/xe/xe_bo_types.h +8 -0
@@ -90,6 +90,14 @@ struct xe_bo {/** @ccs_cleared: true means that CCS region of BO is already cleared */ bool ccs_cleared; +/** +* @dmabuf_pin_count: number of active dma-buf pins. 0->1 charges the +* VRAM limit and adds to the restore list; N->0 uncharges and removes. +* Independent of ttm pin_count, which also counts display pins that +* bypass xe_bo_pin_external. +*/ +u32 dmabuf_pin_count; +/** @bb_ccs: BB instructions of CCS read/write. Valid only for VF */ struct xe_mem_pool_node *bb_ccs[XE_SRIOV_VF_CCS_CTX_COUNT ];
drivers/gpu/drm/xe/xe_defaults.h +1 -0
@@ -22,5 +22,6 @@#define XE_DEFAULT_WEDGED_MODE XE_WEDGED_MODE_UPON_CRITICAL_ERROR#define XE_DEFAULT_WEDGED_MODE_STR "upon-critical-error"#define XE_DEFAULT_SVM_NOTIFIER_SIZE 512 +#define XE_DEFAULT_PIN_VRAM_PERCENT 50#endif
drivers/gpu/drm/xe/xe_dma_buf.c +1 -4
@@ -80,10 +80,7 @@ static int xe_dma_buf_pin(struct dma_buf_attachment *attach) } } - ret =xe_bo_pin_external (bo, !allow_vram, exec); -xe_assert (xe, !ret); - -return 0 ; +return xe_bo_pin_external (bo, !allow_vram, exec); }static void xe_dma_buf_unpin (struct dma_buf_attachment *attach)
drivers/gpu/drm/xe/xe_module.c +5 -0
@@ -30,6 +30,7 @@ struct xe_modparam xe_modparam = {#endif .wedged_mode = XE_DEFAULT_WEDGED_MODE, .svm_notifier_size = XE_DEFAULT_SVM_NOTIFIER_SIZE, + .pin_vram_percent = XE_DEFAULT_PIN_VRAM_PERCENT,/* the rest are 0 by default */ };
@@ -37,6 +38,10 @@ module_param_named(svm_notifier_size, xe_modparam.svm_notifier_size, uint, 0600) MODULE_PARM_DESC(svm_notifier_size, "Set the svm notifier size in MiB, must be power of 2 " "[default=" __stringify(XE_DEFAULT_SVM_NOTIFIER_SIZE) "]"); +module_param_named (pin_vram_percent, xe_modparam.pin_vram_percent, uint,0600 ); +MODULE_PARM_DESC(pin_vram_percent, "Max percent of VRAM pinnable via dma-buf per region (0=unlimited) " + "[default=" __stringify(XE_DEFAULT_PIN_VRAM_PERCENT) "]"); +#if IS_ENABLED (CONFIG_DRM_XE_DISPLAY )module_param_named (probe_display, xe_modparam.probe_display, bool,0444 ); MODULE_PARM_DESC(probe_display, "Probe display HW, otherwise it's left untouched "
drivers/gpu/drm/xe/xe_module.h +1 -0
@@ -24,6 +24,7 @@ struct xe_modparam {#endif unsigned int wedged_mode;u32 svm_notifier_size; +unsigned int pin_vram_percent; };extern struct xe_modparam xe_modparam;
drivers/gpu/drm/xe/xe_ttm_vram_mgr.c +21 -0
@@ -8,11 +8,14 @@#include <drm/drm_drv.h> #include <drm/drm_buddy.h> +#include <linux/math64.h> +#include <drm/ttm/ttm_placement.h> #include <drm/ttm/ttm_range_manager.h> #include "xe_bo.h" #include "xe_device.h" +#include "xe_module.h" #include "xe_res_cursor.h" #include "xe_ttm_vram_mgr.h" #include "xe_vram_types.h"
@@ -190,8 +193,17 @@ static void xe_ttm_vram_mgr_del(struct ttm_resource_manager *man,static void xe_ttm_vram_mgr_debug (struct ttm_resource_manager *man,struct drm_printer *printer) { +struct xe_device *xe =ttm_to_xe_device (man->bdev);struct xe_ttm_vram_mgr *mgr =to_xe_ttm_vram_mgr (man);struct gpu_buddy *mm = &mgr->mm; +u64 dmabuf_used, dmabuf_limit, total_used; + +/* snapshot pin counters under their own lock before sleeping on mgr->lock */ +spin_lock (&xe->pinned.lock); + dmabuf_used = mgr->pin.dmabuf_used; + dmabuf_limit = mgr->pin.dmabuf_limit; + total_used = mgr->pin.total_used; +spin_unlock (&xe->pinned.lock);mutex_lock (&mgr->lock); drm_printf(printer, "default_page_size: %lluKiB\n",
@@ -200,6 +212,12 @@ static void xe_ttm_vram_mgr_debug(struct ttm_resource_manager *man, (u64)mgr->visible_avail >> 20);drm_printf (printer,"visible_size: %lluMiB\n" , (u64 )mgr->visible_size >>20 ); +drm_printf (printer,"pin_dmabuf_used: %lluMiB\n" , + dmabuf_used >>20 ); +drm_printf (printer,"pin_dmabuf_limit: %lluMiB\n" , + dmabuf_limit >>20 ); +drm_printf (printer,"pin_total_used: %lluMiB\n" , + total_used >>20 );drm_buddy_print (mm, printer);mutex_unlock (&mgr->lock);
@@ -309,6 +327,9 @@ int __xe_ttm_vram_mgr_init(struct xe_device *xe, struct xe_ttm_vram_mgr *mgr, man->func = &xe_ttm_vram_mgr_func; mgr->mem_type = mem_type; +if (xe_modparam.pin_vram_percent) + mgr->pin.dmabuf_limit = +mul_u64_u32_div (size, xe_modparam.pin_vram_percent,100 ); err =drmm_mutex_init (&xe->drm, &mgr->lock);if (err)return err;
drivers/gpu/drm/xe/xe_ttm_vram_mgr_types.h +9 -0
@@ -29,6 +29,15 @@ struct xe_ttm_vram_mgr {struct mutex lock;/** @mem_type: The TTM memory type */ u32 mem_type; +/** @pin: pinned VRAM accounting in bytes, protected by xe->pinned.lock */ +struct { +/** @pin.dmabuf_limit: max bytes pinnable via dma-buf, 0 = unlimited */ +u64 dmabuf_limit; +/** @pin.dmabuf_used: bytes currently pinned via dma-buf */ +u64 dmabuf_used; +/** @pin.total_used: bytes currently pinned via any path */ +u64 total_used; + } pin; };/**
Signature · 1 line
2.43.0