drm/xe/pat: Type cleanup and invalid index hardening
Subsystems (3)
v2 cover drm/xe/pat: Type cleanup and invalid index hardening
Small follow-up series to harden the PAT subsystem:
Patch 1 fixes two remaining spots where pat_index was typed as u32/int instead of u16, aligning with the rest of the driver.
Patch 2 defaults XE_CACHE_NONE_COMPRESSION to XE_PAT_INVALID_IDX (like WB_COMPRESSION already does).
Patch 3 introduces xe_cache_pat_idx(), an inline helper that validates cache_mode bounds and checks for XE_PAT_INVALID_IDX before returning the PAT index. All 20 pat.idx[] read sites across the driver are converted.
v2:
- Dropped xe_assert() guards from xe_migrate that were added in v1 patch 2; the new helper in patch 3 now provides the same coverage uniformly across all call sites (Matthew Auld)
- New patch 3: xe_cache_pat_idx() helper with xe_assert() for every pat.idx[] read access, including bounds check on cache_mode
Xin Wang (3):
drm/xe: Standardize pat_index to u16 type drm/xe/pat: Default XE_CACHE_NONE_COMPRESSION to invalid drm/xe/pat: Introduce xe_cache_pat_idx() helper
drivers/gpu/drm/xe/display/xe_fb_pin.c | 11 ++++++----- drivers/gpu/drm/xe/tests/xe_migrate.c | 3 ++- drivers/gpu/drm/xe/xe_device_types.h | 2 +- drivers/gpu/drm/xe/xe_ggtt.c | 7 ++++--- drivers/gpu/drm/xe/xe_migrate.c | 15 ++++++++------- drivers/gpu/drm/xe/xe_pat.c | 1 + drivers/gpu/drm/xe/xe_pat.h | 24 +++++++++++++++++++++++- drivers/gpu/drm/xe/xe_pt.c | 3 ++- drivers/gpu/drm/xe/xe_vm.c | 8 ++++---- 9 files changed, 51 insertions(+), 23 deletions(-)
Signature · 1 line
2.43.0
PATCH v2 1/3 drm/xe: Standardize pat_index to u16 type
Ensure all pat_index definitions consistently use u16 type across the XE driver. This addresses two remaining instances where pat_index was incorrectly typed:
- xe_vm_snapshot structure used int for pat_index field
- xe_device pat.idx array used u32 instead of u16
This cleanup improves type consistency and ensures proper alignment with the PAT subsystem design.
--- drivers/gpu/drm/xe/xe_device_types.h | 2 +- drivers/gpu/drm/xe/xe_vm.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
drivers/gpu/drm/xe/xe_device_types.h +1 -1
@@ -403,7 +403,7 @@ struct xe_device {const struct xe_pat_table_entry *pat_primary_pta;/** @pat.pat_media_pta: media GT PAT entry for page table accesses */ const struct xe_pat_table_entry *pat_media_pta; -u32 idx[__XE_CACHE_LEVEL_COUNT]; +u16 idx[__XE_CACHE_LEVEL_COUNT]; } pat;/** @d3cold: Encapsulate d3cold related stuff */
drivers/gpu/drm/xe/xe_vm.c +1 -1
@@ -4407,7 +4407,7 @@ struct xe_vm_snapshot {#define XE_VM_SNAP_FLAG_IS_NULL BIT(2)unsigned long flags;int uapi_mem_region; -int pat_index; +u16 pat_index;int cpu_caching;struct xe_bo *bo;void *data;
Signature · 1 line
2.43.0
PATCH v2 2/3 drm/xe/pat: Default XE_CACHE_NONE_COMPRESSION to invalid
In reply to Xin WangInitialize XE_CACHE_NONE_COMPRESSION PAT index to XE_PAT_INVALID_IDX by default, same as XE_CACHE_WB_COMPRESSION. Platforms that support this cache mode will override it in xe_pat_init_early(). This ensures that accidental use on unsupported platforms can be detected.
A subsequent patch introduces a helper to assert on invalid PAT index access at all call sites.
--- drivers/gpu/drm/xe/xe_pat.c | 1 + 1 file changed, 1 insertion(+)
drivers/gpu/drm/xe/xe_pat.c +1 -0
@@ -559,6 +559,7 @@ static const struct xe_pat_ops xe3p_xpc_pat_ops = {void xe_pat_init_early (struct xe_device *xe) { xe->pat.idx[XE_CACHE_WB_COMPRESSION ] =XE_PAT_INVALID_IDX ; + xe->pat.idx[XE_CACHE_NONE_COMPRESSION ] =XE_PAT_INVALID_IDX ;if (GRAPHICS_VERx100 (xe) ==3511 ) { xe->pat.ops = &xe3p_xpc_pat_ops; xe->pat.table = xe3p_xpc_pat_table;
Signature · 1 line
2.43.0
PATCH v2 3/3 drm/xe/pat: Introduce xe_cache_pat_idx() helper
In reply to Xin WangAdd an inline helper xe_cache_pat_idx() that wraps pat.idx[] read accesses with xe_assert() validation, catching any attempt to use an invalid PAT index at runtime.
--- drivers/gpu/drm/xe/display/xe_fb_pin.c | 11 ++++++----- drivers/gpu/drm/xe/tests/xe_migrate.c | 3 ++- drivers/gpu/drm/xe/xe_ggtt.c | 7 ++++--- drivers/gpu/drm/xe/xe_migrate.c | 15 ++++++++------- drivers/gpu/drm/xe/xe_pat.h | 24 +++++++++++++++++++++++- drivers/gpu/drm/xe/xe_pt.c | 3 ++- drivers/gpu/drm/xe/xe_vm.c | 6 +++--- 7 files changed, 48 insertions(+), 21 deletions(-)
drivers/gpu/drm/xe/display/xe_fb_pin.c +6 -5
@@ -14,6 +14,7 @@#include "xe_device.h" #include "xe_display_vma.h" #include "xe_ggtt.h" +#include "xe_pat.h" #include "xe_pm.h" #include "xe_vram_types.h"
@@ -24,7 +25,7 @@ write_dpt_rotated(struct xe_bo *bo, struct iosys_map *map, u32 *dpt_ofs, u32 bo_struct xe_device *xe =xe_bo_device (bo);struct xe_ggtt *ggtt =xe_device_get_root_tile (xe)->mem.ggtt;u32 column, row; -u64 pte =xe_ggtt_encode_pte_flags (ggtt, bo, xe->pat.idx[XE_CACHE_NONE ]); +u64 pte =xe_ggtt_encode_pte_flags (ggtt, bo,xe_cache_pat_idx (xe,XE_CACHE_NONE ));/* TODO: Maybe rewrite so we can traverse the bo addresses sequentially, * by writing dpt/ggtt in a different order?
@@ -64,7 +65,7 @@ write_dpt_remapped_linear(struct xe_bo *bo, struct iosys_map *map,struct xe_device *xe =xe_bo_device (bo);struct xe_ggtt *ggtt =xe_device_get_root_tile (xe)->mem.ggtt;const u64 pte =xe_ggtt_encode_pte_flags (ggtt, bo, - xe->pat.idx[XE_CACHE_NONE ]); +xe_cache_pat_idx (xe,XE_CACHE_NONE ));unsigned int offset = plane->offset *XE_PAGE_SIZE ;unsigned int size = plane->size;
@@ -87,7 +88,7 @@ write_dpt_remapped_tiled(struct xe_bo *bo, struct iosys_map *map,struct xe_device *xe =xe_bo_device (bo);struct xe_ggtt *ggtt =xe_device_get_root_tile (xe)->mem.ggtt;const u64 pte =xe_ggtt_encode_pte_flags (ggtt, bo, - xe->pat.idx[XE_CACHE_NONE ]); +xe_cache_pat_idx (xe,XE_CACHE_NONE ));unsigned int offset, column, row;for (row =0 ; row < plane->height; row++) {
@@ -190,7 +191,7 @@ static int __xe_pin_fb_vma_dpt(const struct intel_framebuffer *fb,return PTR_ERR (dpt);if (view->type ==I915_GTT_VIEW_NORMAL ) { -u64 pte =xe_ggtt_encode_pte_flags (ggtt, bo, xe->pat.idx[XE_CACHE_NONE ]); +u64 pte =xe_ggtt_encode_pte_flags (ggtt, bo,xe_cache_pat_idx (xe,XE_CACHE_NONE ));u32 x;for (x =0 ; x < size /XE_PAGE_SIZE ; x++) {
@@ -306,7 +307,7 @@ static int __xe_pin_fb_vma_ggtt(const struct intel_framebuffer *fb,/* display uses tiles instead of bytes here, so convert it back.. */ size =intel_rotation_info_size (&view->rotated) *XE_PAGE_SIZE ; - pte =xe_ggtt_encode_pte_flags (ggtt, bo, xe->pat.idx[XE_CACHE_NONE ]); + pte =xe_ggtt_encode_pte_flags (ggtt, bo,xe_cache_pat_idx (xe,XE_CACHE_NONE )); vma->node = xe_ggtt_insert_node_transform(ggtt, bo, pte, ALIGN(size, align), align, view->type == I915_GTT_VIEW_NORMAL ?
drivers/gpu/drm/xe/tests/xe_migrate.c +2 -1
@@ -9,6 +9,7 @@#include "tests/xe_kunit_helpers.h" #include "tests/xe_pci_test.h" +#include "xe_pat.h" #include "xe_pci.h" #include "xe_pm.h"
@@ -246,7 +247,7 @@ static void xe_migrate_sanity_test(struct xe_migrate *m, struct kunit *test,/* First part of the test, are we updating our pagetable bo with a new entry? */ xe_map_wr (xe, &bo->vmap,XE_PAGE_SIZE * (NUM_KERNEL_PDE -1 ), u64,0xdeaddeadbeefbeef ); - expected = m->q->vm->pt_ops->pte_encode_bo (pt,0 , xe->pat.idx[XE_CACHE_WB ],0 ); + expected = m->q->vm->pt_ops->pte_encode_bo (pt,0 ,xe_cache_pat_idx (xe,XE_CACHE_WB ),0 );if (m->q->vm->flags &XE_VM_FLAG_64K ) expected |=XE_PTE_PS64 ;if (xe_bo_is_vram (pt))
drivers/gpu/drm/xe/xe_ggtt.c +4 -3
@@ -24,6 +24,7 @@#include "xe_gt_types.h" #include "xe_map.h" #include "xe_mmio.h" +#include "xe_pat.h" #include "xe_pm.h" #include "xe_res_cursor.h" #include "xe_sriov.h"
@@ -258,7 +259,7 @@ static u64 xe_ggtt_get_pte(struct xe_ggtt *ggtt, u64 addr)static void xe_ggtt_clear (struct xe_ggtt *ggtt,u64 start,u64 size) { -u16 pat_index =tile_to_xe (ggtt->tile)->pat.idx[XE_CACHE_WB ]; +u16 pat_index =xe_cache_pat_idx (tile_to_xe (ggtt->tile),XE_CACHE_WB );u64 end = start + size -1 ;u64 scratch_pte;
@@ -723,7 +724,7 @@ static void xe_ggtt_map_bo(struct xe_ggtt *ggtt, struct xe_ggtt_node *node,void xe_ggtt_map_bo_unlocked (struct xe_ggtt *ggtt,struct xe_bo *bo) {u16 cache_mode = bo->flags &XE_BO_FLAG_NEEDS_UC ?XE_CACHE_NONE :XE_CACHE_WB ; -u16 pat_index =tile_to_xe (ggtt->tile)->pat.idx[cache_mode]; +u16 pat_index =xe_cache_pat_idx (tile_to_xe (ggtt->tile), cache_mode);u64 pte;mutex_lock (&ggtt->lock);
@@ -840,7 +841,7 @@ static int __xe_ggtt_insert_bo_at(struct xe_ggtt *ggtt, struct xe_bo *bo, bo->ggtt_node[tile_id] =NULL ; }else {u16 cache_mode = bo->flags &XE_BO_FLAG_NEEDS_UC ?XE_CACHE_NONE :XE_CACHE_WB ; -u16 pat_index =tile_to_xe (ggtt->tile)->pat.idx[cache_mode]; +u16 pat_index =xe_cache_pat_idx (tile_to_xe (ggtt->tile), cache_mode);u64 pte = ggtt->pt_ops->pte_encode_flags (bo, pat_index);xe_ggtt_map_bo (ggtt, bo->ggtt_node[tile_id], bo, pte);
drivers/gpu/drm/xe/xe_migrate.c +8 -7
@@ -30,6 +30,7 @@#include "xe_lrc.h" #include "xe_map.h" #include "xe_mocs.h" +#include "xe_pat.h" #include "xe_printk.h" #include "xe_pt.h" #include "xe_res_cursor.h"
@@ -216,7 +217,7 @@ static void xe_migrate_prepare_vm(struct xe_tile *tile, struct xe_migrate *m, struct xe_vm *vm, u32 *ofs) { struct xe_device *xe = tile_to_xe(tile); -u16 pat_index = xe->pat.idx[XE_CACHE_WB ]; +u16 pat_index =xe_cache_pat_idx (xe,XE_CACHE_WB );u8 id = tile->id;u32 num_entries =NUM_PT_SLOTS , num_level = vm->pt_root[id]->level;#define VRAM_IDENTITY_MAP_COUNT 2
@@ -336,7 +337,7 @@ static void xe_migrate_prepare_vm(struct xe_tile *tile, struct xe_migrate *m,* if flat ccs is enabled. */ if (GRAPHICS_VER (xe) >=20 &&xe_device_has_flat_ccs (xe)) { -u16 comp_pat_index = xe->pat.idx[XE_CACHE_NONE_COMPRESSION ]; +u16 comp_pat_index =xe_cache_pat_idx (xe,XE_CACHE_NONE_COMPRESSION );u64 vram_offset =IDENTITY_OFFSET +DIV_ROUND_UP_ULL (actual_phy_size,SZ_1G );u64 pt31_ofs =xe_bo_size (bo) -XE_PAGE_SIZE ;
@@ -636,10 +637,10 @@ static void emit_pte(struct xe_migrate *m,/* Indirect access needs compression enabled uncached PAT index */ if (GRAPHICS_VERx100 (xe) >=2000 ) - pat_index = is_comp_pte ? xe->pat.idx[XE_CACHE_NONE_COMPRESSION ] : - xe->pat.idx[XE_CACHE_WB ]; + pat_index = is_comp_pte ?xe_cache_pat_idx (xe,XE_CACHE_NONE_COMPRESSION ) : +xe_cache_pat_idx (xe,XE_CACHE_WB );else - pat_index = xe->pat.idx[XE_CACHE_WB ]; + pat_index =xe_cache_pat_idx (xe,XE_CACHE_WB ); ptes =DIV_ROUND_UP (size,XE_PAGE_SIZE );
@@ -1870,7 +1871,7 @@ __xe_migrate_update_pgtables(struct xe_migrate *m,/* For sysmem PTE's, need to map them in our hole.. */ if (!IS_DGFX (xe)) { -u16 pat_index = xe->pat.idx[XE_CACHE_WB ]; +u16 pat_index =xe_cache_pat_idx (xe,XE_CACHE_WB );u32 ptes, ofs; ppgtt_ofs =NUM_KERNEL_PDE -1 ;
@@ -2092,7 +2093,7 @@ static void build_pt_update_batch_sram(struct xe_migrate *m,struct drm_pagemap_addr *sram_addr, u32 size, int level) { - u16 pat_index = tile_to_xe(m->tile)->pat.idx[XE_CACHE_WB]; + u16 pat_index = xe_cache_pat_idx(tile_to_xe(m->tile), XE_CACHE_WB);u64 gpu_page_size =0x1ull <<xe_pt_shift (level);u32 ptes;int i =0 ;
drivers/gpu/drm/xe/xe_pat.h +23 -1
@@ -8,8 +8,11 @@#include <linux/types.h> +#include "xe_assert.h" +#include "xe_device_types.h" +#include "xe_pt_types.h" +struct drm_printer ; -struct xe_device ;struct xe_gt ;#define XE_PAT_INVALID_IDX U16_MAX
@@ -82,4 +85,23 @@ bool xe_pat_index_get_comp_en(struct xe_device *xe, u16 pat_index);*/ u16 xe_pat_index_get_l3_policy (struct xe_device *xe,u16 pat_index); +/** +* xe_cache_pat_idx - Get the PAT index for a given cache level +* @xe: xe device +* @cache_mode: cache level to look up +* +* Assert that @cache_mode is within bounds and that the PAT index has +* been configured for the requested cache level, then return it. +* +* Return: PAT index corresponding to @cache_mode +*/ +static inline u16 xe_cache_pat_idx (struct xe_device *xe, +enum xe_cache_level cache_mode) +{ +xe_assert (xe, cache_mode < __XE_CACHE_LEVEL_COUNT); +xe_assert (xe, xe->pat.idx[cache_mode] !=XE_PAT_INVALID_IDX ); + +return xe->pat.idx[cache_mode]; +} +#endif
drivers/gpu/drm/xe/xe_pt.c +2 -1
@@ -4,6 +4,7 @@*/ #include "xe_pt.h" +#include "xe_pat.h" #include "regs/xe_gtt_defs.h" #include "xe_bo.h"
@@ -62,7 +63,7 @@ static u64 __xe_pt_empty_pte(struct xe_tile *tile, struct xe_vm *vm, unsigned int level) {struct xe_device *xe =tile_to_xe (tile); -u16 pat_index = xe->pat.idx[XE_CACHE_WB ]; +u16 pat_index =xe_cache_pat_idx (xe,XE_CACHE_WB );u8 id = tile->id;if (!xe_vm_has_scratch (vm))
drivers/gpu/drm/xe/xe_vm.c +3 -3
@@ -1399,9 +1399,9 @@ static u16 pde_pat_index(struct xe_bo *bo)* something which is always safe). */ if (!xe_bo_is_vram (bo) && bo->ttm.ttm->caching == ttm_cached) - pat_index = xe->pat.idx[XE_CACHE_WB ]; + pat_index =xe_cache_pat_idx (xe,XE_CACHE_WB );else - pat_index = xe->pat.idx[XE_CACHE_NONE ]; + pat_index =xe_cache_pat_idx (xe,XE_CACHE_NONE );xe_assert (xe, pat_index <=3 );
@@ -4204,7 +4204,7 @@ struct dma_fence *xe_vm_bind_kernel_bo(struct xe_vm *vm, struct xe_bo *bo, ops =vm_bind_ioctl_ops_create (vm, &vops, bo,0 , addr,xe_bo_size (bo),DRM_XE_VM_BIND_OP_MAP ,0 ,0 , - vm->xe->pat.idx[cache_lvl]); +xe_cache_pat_idx (vm->xe, cache_lvl));if (IS_ERR (ops)) { err =PTR_ERR (ops); goto release_vm_lock;
Signature · 1 line
2.43.0