Skip to content

Commit 68d26c1

Browse files
PhilipYangAalexdeucher
authored andcommitted
drm/amdkfd: Accounting pdd vram_usage for svm
Process device data pdd->vram_usage is read by rocm-smi via sysfs, this is currently missing the svm_bo usage accounting, so "rocm-smi --showpids" per process VRAM usage report is incorrect. Add pdd->vram_usage accounting when svm_bo allocation and release, change to atomic64_t type because it is updated outside process mutex now. Signed-off-by: Philip Yang <[email protected]> Reviewed-by: Felix Kuehling <[email protected]> Signed-off-by: Alex Deucher <[email protected]> (cherry picked from commit 98c0b0e)
1 parent e745753 commit 68d26c1

File tree

4 files changed

+32
-6
lines changed

4 files changed

+32
-6
lines changed

drivers/gpu/drm/amd/amdkfd/kfd_chardev.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1148,7 +1148,7 @@ static int kfd_ioctl_alloc_memory_of_gpu(struct file *filep,
11481148

11491149
if (flags & KFD_IOC_ALLOC_MEM_FLAGS_AQL_QUEUE_MEM)
11501150
size >>= 1;
1151-
WRITE_ONCE(pdd->vram_usage, pdd->vram_usage + PAGE_ALIGN(size));
1151+
atomic64_add(PAGE_ALIGN(size), &pdd->vram_usage);
11521152
}
11531153

11541154
mutex_unlock(&p->mutex);
@@ -1219,7 +1219,7 @@ static int kfd_ioctl_free_memory_of_gpu(struct file *filep,
12191219
kfd_process_device_remove_obj_handle(
12201220
pdd, GET_IDR_HANDLE(args->handle));
12211221

1222-
WRITE_ONCE(pdd->vram_usage, pdd->vram_usage - size);
1222+
atomic64_sub(size, &pdd->vram_usage);
12231223

12241224
err_unlock:
12251225
err_pdd:
@@ -2347,7 +2347,7 @@ static int criu_restore_memory_of_gpu(struct kfd_process_device *pdd,
23472347
} else if (bo_bucket->alloc_flags & KFD_IOC_ALLOC_MEM_FLAGS_VRAM) {
23482348
bo_bucket->restored_offset = offset;
23492349
/* Update the VRAM usage count */
2350-
WRITE_ONCE(pdd->vram_usage, pdd->vram_usage + bo_bucket->size);
2350+
atomic64_add(bo_bucket->size, &pdd->vram_usage);
23512351
}
23522352
return 0;
23532353
}

drivers/gpu/drm/amd/amdkfd/kfd_priv.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -775,7 +775,7 @@ struct kfd_process_device {
775775
enum kfd_pdd_bound bound;
776776

777777
/* VRAM usage */
778-
uint64_t vram_usage;
778+
atomic64_t vram_usage;
779779
struct attribute attr_vram;
780780
char vram_filename[MAX_SYSFS_FILENAME_LEN];
781781

drivers/gpu/drm/amd/amdkfd/kfd_process.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ static ssize_t kfd_procfs_show(struct kobject *kobj, struct attribute *attr,
332332
} else if (strncmp(attr->name, "vram_", 5) == 0) {
333333
struct kfd_process_device *pdd = container_of(attr, struct kfd_process_device,
334334
attr_vram);
335-
return snprintf(buffer, PAGE_SIZE, "%llu\n", READ_ONCE(pdd->vram_usage));
335+
return snprintf(buffer, PAGE_SIZE, "%llu\n", atomic64_read(&pdd->vram_usage));
336336
} else if (strncmp(attr->name, "sdma_", 5) == 0) {
337337
struct kfd_process_device *pdd = container_of(attr, struct kfd_process_device,
338338
attr_sdma);
@@ -1625,7 +1625,7 @@ struct kfd_process_device *kfd_create_process_device_data(struct kfd_node *dev,
16251625
pdd->bound = PDD_UNBOUND;
16261626
pdd->already_dequeued = false;
16271627
pdd->runtime_inuse = false;
1628-
pdd->vram_usage = 0;
1628+
atomic64_set(&pdd->vram_usage, 0);
16291629
pdd->sdma_past_activity_counter = 0;
16301630
pdd->user_gpu_id = dev->id;
16311631
atomic64_set(&pdd->evict_duration_counter, 0);

drivers/gpu/drm/amd/amdkfd/kfd_svm.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,27 @@ static void svm_range_bo_release(struct kref *kref)
405405
spin_lock(&svm_bo->list_lock);
406406
}
407407
spin_unlock(&svm_bo->list_lock);
408+
409+
if (mmget_not_zero(svm_bo->eviction_fence->mm)) {
410+
struct kfd_process_device *pdd;
411+
struct kfd_process *p;
412+
struct mm_struct *mm;
413+
414+
mm = svm_bo->eviction_fence->mm;
415+
/*
416+
* The forked child process takes svm_bo device pages ref, svm_bo could be
417+
* released after parent process is gone.
418+
*/
419+
p = kfd_lookup_process_by_mm(mm);
420+
if (p) {
421+
pdd = kfd_get_process_device_data(svm_bo->node, p);
422+
if (pdd)
423+
atomic64_sub(amdgpu_bo_size(svm_bo->bo), &pdd->vram_usage);
424+
kfd_unref_process(p);
425+
}
426+
mmput(mm);
427+
}
428+
408429
if (!dma_fence_is_signaled(&svm_bo->eviction_fence->base))
409430
/* We're not in the eviction worker. Signal the fence. */
410431
dma_fence_signal(&svm_bo->eviction_fence->base);
@@ -532,6 +553,7 @@ int
532553
svm_range_vram_node_new(struct kfd_node *node, struct svm_range *prange,
533554
bool clear)
534555
{
556+
struct kfd_process_device *pdd;
535557
struct amdgpu_bo_param bp;
536558
struct svm_range_bo *svm_bo;
537559
struct amdgpu_bo_user *ubo;
@@ -623,6 +645,10 @@ svm_range_vram_node_new(struct kfd_node *node, struct svm_range *prange,
623645
list_add(&prange->svm_bo_list, &svm_bo->range_list);
624646
spin_unlock(&svm_bo->list_lock);
625647

648+
pdd = svm_range_get_pdd_by_node(prange, node);
649+
if (pdd)
650+
atomic64_add(amdgpu_bo_size(bo), &pdd->vram_usage);
651+
626652
return 0;
627653

628654
reserve_bo_failed:

0 commit comments

Comments
 (0)