Skip to content

Commit fab76ce

Browse files
committed
drm/xe: Add xe_vm_has_valid_gpu_mapping helper
Rather than having multiple READ_ONCE of the tile_* fields and comments in code, use helper with kernel doc for single access point and clear rules. v3: - s/xe_vm_has_valid_gpu_pages/xe_vm_has_valid_gpu_mapping Suggested-by: Thomas Hellström <[email protected]> Signed-off-by: Matthew Brost <[email protected]> Reviewed-by: Himal Prasad Ghimiray <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 1a5ce0c commit fab76ce

File tree

5 files changed

+32
-20
lines changed

5 files changed

+32
-20
lines changed

drivers/gpu/drm/xe/xe_gt_pagefault.c

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,10 @@ static bool access_is_atomic(enum access_type access_type)
6969

7070
static bool vma_is_valid(struct xe_tile *tile, struct xe_vma *vma)
7171
{
72-
/*
73-
* Advisory only check whether the VMA currently has a valid mapping,
74-
* READ_ONCE pairs with WRITE_ONCE in xe_pt.c
75-
*/
76-
return BIT(tile->id) & READ_ONCE(vma->tile_present) &&
77-
!(BIT(tile->id) & READ_ONCE(vma->tile_invalidated));
72+
return xe_vm_has_valid_gpu_mapping(tile, vma->tile_present,
73+
vma->tile_invalidated);
7874
}
7975

80-
8176
static int xe_pf_begin(struct drm_exec *exec, struct xe_vma *vma,
8277
bool atomic, unsigned int id)
8378
{

drivers/gpu/drm/xe/xe_pt.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2196,7 +2196,7 @@ static void bind_op_commit(struct xe_vm *vm, struct xe_tile *tile,
21962196
DMA_RESV_USAGE_KERNEL :
21972197
DMA_RESV_USAGE_BOOKKEEP);
21982198
}
2199-
/* All WRITE_ONCE pair with READ_ONCE in xe_gt_pagefault.c */
2199+
/* All WRITE_ONCE pair with READ_ONCE in xe_vm_has_valid_gpu_mapping() */
22002200
WRITE_ONCE(vma->tile_present, vma->tile_present | BIT(tile->id));
22012201
if (invalidate_on_bind)
22022202
WRITE_ONCE(vma->tile_invalidated,
@@ -2255,7 +2255,7 @@ static void range_present_and_invalidated_tile(struct xe_vm *vm,
22552255
struct xe_svm_range *range,
22562256
u8 tile_id)
22572257
{
2258-
/* WRITE_ONCE pairs with READ_ONCE in xe_svm.c */
2258+
/* All WRITE_ONCE pair with READ_ONCE in xe_vm_has_valid_gpu_mapping() */
22592259

22602260
lockdep_assert_held(&vm->svm.gpusvm.notifier_lock);
22612261

@@ -2324,7 +2324,7 @@ static void op_commit(struct xe_vm *vm,
23242324
}
23252325
case DRM_GPUVA_OP_DRIVER:
23262326
{
2327-
/* WRITE_ONCE pairs with READ_ONCE in xe_svm.c */
2327+
/* WRITE_ONCE pairs with READ_ONCE in xe_vm_has_valid_gpu_mapping() */
23282328
if (op->subop == XE_VMA_SUBOP_MAP_RANGE)
23292329
range_present_and_invalidated_tile(vm, op->map_range.range, tile->id);
23302330
else if (op->subop == XE_VMA_SUBOP_UNMAP_RANGE)

drivers/gpu/drm/xe/xe_svm.c

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,10 @@ xe_svm_range_notifier_event_begin(struct xe_vm *vm, struct drm_gpusvm_range *r,
141141
for_each_tile(tile, xe, id)
142142
if (xe_pt_zap_ptes_range(tile, vm, range)) {
143143
tile_mask |= BIT(id);
144-
/* Pairs with READ_ONCE in xe_svm_range_is_valid */
144+
/*
145+
* WRITE_ONCE pairs with READ_ONCE in
146+
* xe_vm_has_valid_gpu_mapping()
147+
*/
145148
WRITE_ONCE(range->tile_invalidated,
146149
range->tile_invalidated | BIT(id));
147150
}
@@ -605,14 +608,9 @@ static bool xe_svm_range_is_valid(struct xe_svm_range *range,
605608
struct xe_tile *tile,
606609
bool devmem_only)
607610
{
608-
/*
609-
* Advisory only check whether the range currently has a valid mapping,
610-
* READ_ONCE pairs with WRITE_ONCE in xe_pt.c,
611-
* xe_svm_range_notifier_event_begin
612-
*/
613-
return ((READ_ONCE(range->tile_present) &
614-
~READ_ONCE(range->tile_invalidated)) & BIT(tile->id)) &&
615-
(!devmem_only || xe_svm_range_in_vram(range));
611+
return (xe_vm_has_valid_gpu_mapping(tile, range->tile_present,
612+
range->tile_invalidated) &&
613+
(!devmem_only || xe_svm_range_in_vram(range)));
616614
}
617615

618616
/** xe_svm_range_migrate_to_smem() - Move range pages from VRAM to SMEM

drivers/gpu/drm/xe/xe_vm.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3961,7 +3961,7 @@ int xe_vm_invalidate_vma(struct xe_vma *vma)
39613961
ret = xe_vm_range_tilemask_tlb_invalidation(xe_vma_vm(vma), xe_vma_start(vma),
39623962
xe_vma_end(vma), tile_mask);
39633963

3964-
/* WRITE_ONCE pair with READ_ONCE in xe_gt_pagefault.c */
3964+
/* WRITE_ONCE pairs with READ_ONCE in xe_vm_has_valid_gpu_mapping() */
39653965
WRITE_ONCE(vma->tile_invalidated, vma->tile_mask);
39663966

39673967
return ret;

drivers/gpu/drm/xe/xe_vm.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,25 @@ static inline bool xe_vm_is_validating(struct xe_vm *vm)
375375
return false;
376376
}
377377

378+
/**
379+
* xe_vm_has_valid_gpu_mapping() - Advisory helper to check if VMA or SVM range has
380+
* a valid GPU mapping
381+
* @tile: The tile which the GPU mapping belongs to
382+
* @tile_present: Tile present mask
383+
* @tile_invalidated: Tile invalidated mask
384+
*
385+
* The READ_ONCEs pair with WRITE_ONCEs in either the TLB invalidation paths
386+
* (xe_vm.c, xe_svm.c) or the binding paths (xe_pt.c). These are not reliable
387+
* without the notifier lock in userptr or SVM cases, and not reliable without
388+
* the BO dma-resv lock in the BO case. As such, they should only be used in
389+
* opportunistic cases (e.g., skipping a page fault fix or not skipping a TLB
390+
* invalidation) where it is harmless.
391+
*
392+
* Return: True is there are valid GPU pages, False otherwise
393+
*/
394+
#define xe_vm_has_valid_gpu_mapping(tile, tile_present, tile_invalidated) \
395+
((READ_ONCE(tile_present) & ~READ_ONCE(tile_invalidated)) & BIT((tile)->id))
396+
378397
#if IS_ENABLED(CONFIG_DRM_XE_USERPTR_INVAL_INJECT)
379398
void xe_vma_userptr_force_invalidate(struct xe_userptr_vma *uvma);
380399
#else

0 commit comments

Comments
 (0)