Skip to contentSkip to conversation

drm/amdgpu: individualize amdgpu entity allocation per HW IP

PATCH In review Nirmoy Das 3 participants 4 messages Jan 22, 2020

Subsystems (2)
lore
Share & tools
lore

Nirmoy Das

author
#0 lore raw

PATCH 1/2 drm/amdgpu: individualize amdgpu entity allocation per HW IP

Do not allocate all the entity at once. This is required for dynamic amdgpu_ctx_entity creation.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c | 143 +++++++++++++-----------
 1 file changed, 76 insertions(+), 67 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +76 -67
index 05c2af61e7de..eecbd68db986 100644
@@ -42,16 +42,6 @@ const unsigned int amdgpu_ctx_num_entities[AMDGPU_HW_IP_NUM] = {
 	[AMDGPU_HW_IP_VCN_JPEG]	=	1,
 };
 
-static int amdgpu_ctx_total_num_entities(void)
-{
-	unsigned i, num_entities = 0;
-
-	for (i = 0; i < AMDGPU_HW_IP_NUM; ++i)
-		num_entities += amdgpu_ctx_num_entities[i];
-
-	return num_entities;
-}
-
 static int amdgpu_ctx_priority_permit(struct drm_file *filp,
 				      enum drm_sched_priority priority)
 {
@@ -73,7 +63,6 @@ static int amdgpu_ctx_init(struct amdgpu_device *adev,
 			   struct drm_file *filp,
 			   struct amdgpu_ctx *ctx)
 {
-	unsigned num_entities = amdgpu_ctx_total_num_entities();
 	unsigned i, j;
 	int r;
 
@@ -87,28 +76,30 @@ static int amdgpu_ctx_init(struct amdgpu_device *adev,
 	memset(ctx, 0, sizeof(*ctx));
 	ctx->adev = adev;
 
+	for (i = 0; i < AMDGPU_HW_IP_NUM; ++i) {
+		ctx->entities[i] = kcalloc(amdgpu_ctx_num_entities[i],
+					   sizeof(struct amdgpu_ctx_entity),
+					   GFP_KERNEL);
 
-	ctx->entities[0] = kcalloc(num_entities,
-				   sizeof(struct amdgpu_ctx_entity),
-				   GFP_KERNEL);
-	if (!ctx->entities[0])
-		return -ENOMEM;
-
-
-	for (i = 0; i < num_entities; ++i) {
-		struct amdgpu_ctx_entity *entity = &ctx->entities[0][i];
+		if (!ctx->entities[0]) {
+			r =  -ENOMEM;
+			goto error_cleanup_entity_memory;
+		}
+	}
 
-		entity->sequence = 1;
-		entity->fences = kcalloc(amdgpu_sched_jobs,
-					 sizeof(struct dma_fence*), GFP_KERNEL);
-		if (!entity->fences) {
-			r = -ENOMEM;
-			goto error_cleanup_memory;
+	for (i = 0; i < AMDGPU_HW_IP_NUM; ++i) {
+		for (j = 0; j < amdgpu_ctx_num_entities[i]; ++j) {
+			struct amdgpu_ctx_entity *entity = &ctx->entities[i][j];
+
+			entity->sequence = 1;
+			entity->fences = kcalloc(amdgpu_sched_jobs,
+						 sizeof(struct dma_fence*), GFP_KERNEL);
+			if (!entity->fences) {
+				r = -ENOMEM;
+				goto error_cleanup_memory;
+			}
 		}
 	}
-	for (i = 1; i < AMDGPU_HW_IP_NUM; ++i)
-		ctx->entities[i] = ctx->entities[i - 1] +
-			amdgpu_ctx_num_entities[i - 1];
 
 	kref_init(&ctx->refcount);
 	spin_lock_init(&ctx->ring_lock);
@@ -179,44 +170,55 @@ static int amdgpu_ctx_init(struct amdgpu_device *adev,
 	return 0;
 
 error_cleanup_entities:
-	for (i = 0; i < num_entities; ++i)
-		drm_sched_entity_destroy(&ctx->entities[0][i].entity);
+	for (i = 0; i < AMDGPU_HW_IP_NUM; ++i) {
+		for (j = 0; j < amdgpu_ctx_num_entities[i]; ++j)
+			drm_sched_entity_destroy(&ctx->entities[i][j].entity);
+	}
 
 error_cleanup_memory:
-	for (i = 0; i < num_entities; ++i) {
-		struct amdgpu_ctx_entity *entity = &ctx->entities[0][i];
+	for (i = 0; i < AMDGPU_HW_IP_NUM; ++i) {
+		for (j = 0; j < amdgpu_ctx_num_entities[i]; ++j) {
+			struct amdgpu_ctx_entity *entity = &ctx->entities[i][j];
 
-		kfree(entity->fences);
-		entity->fences = NULL;
+			kfree(entity->fences);
+			entity->fences = NULL;
+		}
+	}
+
+error_cleanup_entity_memory:
+	for (i = 0; i < AMDGPU_HW_IP_NUM; ++i) {
+		kfree(ctx->entities[i]);
+		ctx->entities[i] = NULL;
 	}
 
-	kfree(ctx->entities[0]);
-	ctx->entities[0] = NULL;
 	return r;
 }
 
 static void amdgpu_ctx_fini(struct kref *ref)
 {
 	struct amdgpu_ctx *ctx = container_of(ref, struct amdgpu_ctx, refcount);
-	unsigned num_entities = amdgpu_ctx_total_num_entities();
 	struct amdgpu_device *adev = ctx->adev;
-	unsigned i, j;
+	unsigned i, j, k;
 
 	if (!adev)
 		return;
+	for (i = 0; i < AMDGPU_HW_IP_NUM; ++i) {
+		for (j = 0; j < amdgpu_ctx_num_entities[i]; ++j) {
+			struct amdgpu_ctx_entity *entity = &ctx->entities[i][j];
 
-	for (i = 0; i < num_entities; ++i) {
-		struct amdgpu_ctx_entity *entity = &ctx->entities[0][i];
+			for (k = 0; k < amdgpu_sched_jobs; ++k)
+				dma_fence_put(entity->fences[k]);
 
-		for (j = 0; j < amdgpu_sched_jobs; ++j)
-			dma_fence_put(entity->fences[j]);
+			kfree(entity->fences);
+		}
+	}
 
-		kfree(entity->fences);
+	for (i = 0; i < AMDGPU_HW_IP_NUM; ++i) {
+		kfree(ctx->entities[i]);
+		ctx->entities[i] = NULL;
 	}
 
-	kfree(ctx->entities[0]);
 	mutex_destroy(&ctx->lock);
-
 	kfree(ctx);
 }
 
@@ -279,14 +281,13 @@ static int amdgpu_ctx_alloc(struct amdgpu_device *adev,
 static void amdgpu_ctx_do_release(struct kref *ref)
 {
 	struct amdgpu_ctx *ctx;
-	unsigned num_entities;
-	u32 i;
+	u32 i, j;
 
 	ctx = container_of(ref, struct amdgpu_ctx, refcount);
-
-	num_entities = amdgpu_ctx_total_num_entities();
-	for (i = 0; i < num_entities; i++)
-		drm_sched_entity_destroy(&ctx->entities[0][i].entity);
+	for (i = 0; i < AMDGPU_HW_IP_NUM; ++i) {
+		for (j = 0; j < amdgpu_ctx_num_entities[i]; ++j)
+			drm_sched_entity_destroy(&ctx->entities[i][j].entity);
+	}
 
 	amdgpu_ctx_fini(ref);
 }
@@ -516,19 +517,21 @@ struct dma_fence *amdgpu_ctx_get_fence(struct amdgpu_ctx *ctx,
 void amdgpu_ctx_priority_override(struct amdgpu_ctx *ctx,
 				  enum drm_sched_priority priority)
 {
-	unsigned num_entities = amdgpu_ctx_total_num_entities();
 	enum drm_sched_priority ctx_prio;
-	unsigned i;
+	unsigned i, j;
 
 	ctx->override_priority = priority;
 
 	ctx_prio = (ctx->override_priority == DRM_SCHED_PRIORITY_UNSET) ?
 			ctx->init_priority : ctx->override_priority;
+	for (i = 0; i < AMDGPU_HW_IP_NUM; ++i) {
+		for (j = 0; j < amdgpu_ctx_num_entities[i]; ++j) {
 
-	for (i = 0; i < num_entities; i++) {
-		struct drm_sched_entity *entity = &ctx->entities[0][i].entity;
+			struct drm_sched_entity *entity =
+				&ctx->entities[i][j].entity;
 
-		drm_sched_entity_set_priority(entity, ctx_prio);
+			drm_sched_entity_set_priority(entity, ctx_prio);
+		}
 	}
 }
 
@@ -564,20 +567,21 @@ void amdgpu_ctx_mgr_init(struct amdgpu_ctx_mgr *mgr)
 
 long amdgpu_ctx_mgr_entity_flush(struct amdgpu_ctx_mgr *mgr, long timeout)
 {
-	unsigned num_entities = amdgpu_ctx_total_num_entities();
 	struct amdgpu_ctx *ctx;
 	struct idr *idp;
-	uint32_t id, i;
+	uint32_t id, i, j;
 
 	idp = &mgr->ctx_handles;
 
 	mutex_lock(&mgr->lock);
 	idr_for_each_entry(idp, ctx, id) {
-		for (i = 0; i < num_entities; i++) {
-			struct drm_sched_entity *entity;
+		for (i = 0; i < AMDGPU_HW_IP_NUM; ++i) {
+			for (j = 0; j < amdgpu_ctx_num_entities[i]; ++j) {
+				struct drm_sched_entity *entity;
 
-			entity = &ctx->entities[0][i].entity;
-			timeout = drm_sched_entity_flush(entity, timeout);
+				entity = &ctx->entities[i][j].entity;
+				timeout = drm_sched_entity_flush(entity, timeout);
+			}
 		}
 	}
 	mutex_unlock(&mgr->lock);
@@ -586,10 +590,9 @@ long amdgpu_ctx_mgr_entity_flush(struct amdgpu_ctx_mgr *mgr, long timeout)
 
 void amdgpu_ctx_mgr_entity_fini(struct amdgpu_ctx_mgr *mgr)
 {
-	unsigned num_entities = amdgpu_ctx_total_num_entities();
 	struct amdgpu_ctx *ctx;
 	struct idr *idp;
-	uint32_t id, i;
+	uint32_t id, i, j;
 
 	idp = &mgr->ctx_handles;
 
@@ -599,8 +602,14 @@ void amdgpu_ctx_mgr_entity_fini(struct amdgpu_ctx_mgr *mgr)
 			continue;
 		}
 
-		for (i = 0; i < num_entities; i++)
-			drm_sched_entity_fini(&ctx->entities[0][i].entity);
+		for (i = 0; i < AMDGPU_HW_IP_NUM; ++i) {
+			for (j = 0; j < amdgpu_ctx_num_entities[i]; ++j) {
+				struct drm_sched_entity *entity;
+
+				entity = &ctx->entities[i][j].entity;
+				drm_sched_entity_fini(entity);
+			}
+		}
 	}
 }
 
Signature · 6 lines
2.24.1

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
author
#1 lore raw

PATCH 2/2 drm/amdgpu: allocate entities on demand

Currently we pre-allocate entities and fences for all the HW IPs on context creation and some of which are might never be used.

This patch tries to resolve entity/fences wastage by creating entities for a HW IP only when it is required.

v2: consolidated priority checking at amdgpu_ctx_priority_permit()

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c | 160 +++++++++++++-----------
 1 file changed, 87 insertions(+), 73 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +87 -73
index eecbd68db986..d704e1bebb1b 100644
@@ -45,6 +45,9 @@ const unsigned int amdgpu_ctx_num_entities[AMDGPU_HW_IP_NUM] = {
 static int amdgpu_ctx_priority_permit(struct drm_file *filp,
 				      enum drm_sched_priority priority)
 {
+	if (priority < 0 || priority >= DRM_SCHED_PRIORITY_MAX)
+		return -EINVAL;
+
 	/* NORMAL and below are accessible by everyone */
 	if (priority <= DRM_SCHED_PRIORITY_NORMAL)
 		return 0;
@@ -58,65 +61,37 @@ static int amdgpu_ctx_priority_permit(struct drm_file *filp,
 	return -EACCES;
 }
 
-static int amdgpu_ctx_init(struct amdgpu_device *adev,
-			   enum drm_sched_priority priority,
-			   struct drm_file *filp,
-			   struct amdgpu_ctx *ctx)
+static int amdgpu_ctx_init_entity(struct amdgpu_ctx *ctx, const u32 hw_ip)
 {
-	unsigned i, j;
-	int r;
+	struct amdgpu_device *adev = ctx->adev;
+	struct drm_gpu_scheduler **scheds;
+	struct drm_gpu_scheduler *sched;
+	unsigned num_scheds = 0;
+	enum drm_sched_priority priority;
+	int j, r;
 
-	if (priority < 0 || priority >= DRM_SCHED_PRIORITY_MAX)
-		return -EINVAL;
+	ctx->entities[hw_ip] = kcalloc(amdgpu_ctx_num_entities[hw_ip],
+				       sizeof(struct amdgpu_ctx_entity), GFP_KERNEL);
 
-	r = amdgpu_ctx_priority_permit(filp, priority);
-	if (r)
-		return r;
+	if (!ctx->entities[hw_ip])
+		return  -ENOMEM;
 
-	memset(ctx, 0, sizeof(*ctx));
-	ctx->adev = adev;
+	for (j = 0; j < amdgpu_ctx_num_entities[hw_ip]; ++j) {
 
-	for (i = 0; i < AMDGPU_HW_IP_NUM; ++i) {
-		ctx->entities[i] = kcalloc(amdgpu_ctx_num_entities[i],
-					   sizeof(struct amdgpu_ctx_entity),
-					   GFP_KERNEL);
+		struct amdgpu_ctx_entity *entity = &ctx->entities[hw_ip][j];
 
-		if (!ctx->entities[0]) {
-			r =  -ENOMEM;
-			goto error_cleanup_entity_memory;
+		entity->sequence = 1;
+		entity->fences = kcalloc(amdgpu_sched_jobs,
+					 sizeof(struct dma_fence*), GFP_KERNEL);
+		if (!entity->fences) {
+			r = -ENOMEM;
+			goto error_cleanup_memory;
 		}
 	}
 
-	for (i = 0; i < AMDGPU_HW_IP_NUM; ++i) {
-		for (j = 0; j < amdgpu_ctx_num_entities[i]; ++j) {
-			struct amdgpu_ctx_entity *entity = &ctx->entities[i][j];
-
-			entity->sequence = 1;
-			entity->fences = kcalloc(amdgpu_sched_jobs,
-						 sizeof(struct dma_fence*), GFP_KERNEL);
-			if (!entity->fences) {
-				r = -ENOMEM;
-				goto error_cleanup_memory;
-			}
-		}
-	}
-
-	kref_init(&ctx->refcount);
-	spin_lock_init(&ctx->ring_lock);
-	mutex_init(&ctx->lock);
-
-	ctx->reset_counter = atomic_read(&adev->gpu_reset_counter);
-	ctx->reset_counter_query = ctx->reset_counter;
-	ctx->vram_lost_counter = atomic_read(&adev->vram_lost_counter);
-	ctx->init_priority = priority;
-	ctx->override_priority = DRM_SCHED_PRIORITY_UNSET;
-
-	for (i = 0; i < AMDGPU_HW_IP_NUM; ++i) {
-		struct drm_gpu_scheduler **scheds;
-		struct drm_gpu_scheduler *sched;
-		unsigned num_scheds = 0;
-
-		switch (i) {
+	priority = (ctx->override_priority == DRM_SCHED_PRIORITY_UNSET) ?
+				ctx->init_priority : ctx->override_priority;
+	switch (hw_ip) {
 		case AMDGPU_HW_IP_GFX:
 			sched = &adev->gfx.gfx_ring[0].sched;
 			scheds = &sched;
@@ -157,12 +132,12 @@ static int amdgpu_ctx_init(struct amdgpu_device *adev,
 			scheds = adev->jpeg.jpeg_sched;
 			num_scheds =  adev->jpeg.num_jpeg_sched;
 			break;
-		}
+	}
 
-		for (j = 0; j < amdgpu_ctx_num_entities[i]; ++j)
-			r = drm_sched_entity_init(&ctx->entities[i][j].entity,
-						  priority, scheds,
-						  num_scheds, &ctx->guilty);
+	for (j = 0; j < amdgpu_ctx_num_entities[hw_ip]; ++j) {
+		r = drm_sched_entity_init(&ctx->entities[hw_ip][j].entity,
+					  priority, scheds,
+					  num_scheds, &ctx->guilty);
 		if (r)
 			goto error_cleanup_entities;
 	}
@@ -170,30 +145,51 @@ static int amdgpu_ctx_init(struct amdgpu_device *adev,
 	return 0;
 
 error_cleanup_entities:
-	for (i = 0; i < AMDGPU_HW_IP_NUM; ++i) {
-		for (j = 0; j < amdgpu_ctx_num_entities[i]; ++j)
-			drm_sched_entity_destroy(&ctx->entities[i][j].entity);
-	}
+	for (j = 0; j < amdgpu_ctx_num_entities[hw_ip]; ++j)
+		drm_sched_entity_destroy(&ctx->entities[hw_ip][j].entity);
 
 error_cleanup_memory:
-	for (i = 0; i < AMDGPU_HW_IP_NUM; ++i) {
-		for (j = 0; j < amdgpu_ctx_num_entities[i]; ++j) {
-			struct amdgpu_ctx_entity *entity = &ctx->entities[i][j];
+	for (j = 0; j < amdgpu_ctx_num_entities[hw_ip]; ++j) {
+		struct amdgpu_ctx_entity *entity = &ctx->entities[hw_ip][j];
 
-			kfree(entity->fences);
-			entity->fences = NULL;
-		}
+		kfree(entity->fences);
+		entity->fences = NULL;
 	}
 
-error_cleanup_entity_memory:
-	for (i = 0; i < AMDGPU_HW_IP_NUM; ++i) {
-		kfree(ctx->entities[i]);
-		ctx->entities[i] = NULL;
-	}
+	kfree(ctx->entities[hw_ip]);
+	ctx->entities[hw_ip] = NULL;
 
 	return r;
 }
 
+static int amdgpu_ctx_init(struct amdgpu_device *adev,
+			   enum drm_sched_priority priority,
+			   struct drm_file *filp,
+			   struct amdgpu_ctx *ctx)
+{
+	int r;
+
+	r = amdgpu_ctx_priority_permit(filp, priority);
+	if (r)
+		return r;
+
+	memset(ctx, 0, sizeof(*ctx));
+	ctx->adev = adev;
+
+	kref_init(&ctx->refcount);
+	spin_lock_init(&ctx->ring_lock);
+	mutex_init(&ctx->lock);
+
+	ctx->reset_counter = atomic_read(&adev->gpu_reset_counter);
+	ctx->reset_counter_query = ctx->reset_counter;
+	ctx->vram_lost_counter = atomic_read(&adev->vram_lost_counter);
+	ctx->init_priority = priority;
+	ctx->override_priority = DRM_SCHED_PRIORITY_UNSET;
+
+	return 0;
+
+}
+
 static void amdgpu_ctx_fini(struct kref *ref)
 {
 	struct amdgpu_ctx *ctx = container_of(ref, struct amdgpu_ctx, refcount);
@@ -204,7 +200,14 @@ static void amdgpu_ctx_fini(struct kref *ref)
 		return;
 	for (i = 0; i < AMDGPU_HW_IP_NUM; ++i) {
 		for (j = 0; j < amdgpu_ctx_num_entities[i]; ++j) {
-			struct amdgpu_ctx_entity *entity = &ctx->entities[i][j];
+			struct amdgpu_ctx_entity *entity;
+
+			if (!ctx->entities[i])
+				continue;
+
+			entity = &ctx->entities[i][j];
+			if (!entity->fences)
+				continue;
 
 			for (k = 0; k < amdgpu_sched_jobs; ++k)
 				dma_fence_put(entity->fences[k]);
@@ -241,6 +244,9 @@ int amdgpu_ctx_get_entity(struct amdgpu_ctx *ctx, u32 hw_ip, u32 instance,
 		return -EINVAL;
 	}
 
+	if (ctx->entities[hw_ip] == NULL)
+		amdgpu_ctx_init_entity(ctx, hw_ip);
+
 	*entity = &ctx->entities[hw_ip][ring].entity;
 	return 0;
 }
@@ -285,8 +291,11 @@ static void amdgpu_ctx_do_release(struct kref *ref)
 
 	ctx = container_of(ref, struct amdgpu_ctx, refcount);
 	for (i = 0; i < AMDGPU_HW_IP_NUM; ++i) {
-		for (j = 0; j < amdgpu_ctx_num_entities[i]; ++j)
+		for (j = 0; j < amdgpu_ctx_num_entities[i]; ++j) {
+			if (!ctx->entities[i])
+				continue;
 			drm_sched_entity_destroy(&ctx->entities[i][j].entity);
+		}
 	}
 
 	amdgpu_ctx_fini(ref);
@@ -579,6 +588,9 @@ long amdgpu_ctx_mgr_entity_flush(struct amdgpu_ctx_mgr *mgr, long timeout)
 			for (j = 0; j < amdgpu_ctx_num_entities[i]; ++j) {
 				struct drm_sched_entity *entity;
 
+				if (!ctx->entities[i])
+					continue;
+
 				entity = &ctx->entities[i][j].entity;
 				timeout = drm_sched_entity_flush(entity, timeout);
 			}
@@ -601,11 +613,13 @@ void amdgpu_ctx_mgr_entity_fini(struct amdgpu_ctx_mgr *mgr)
 			DRM_ERROR("ctx %p is still alive\n", ctx);
 			continue;
 		}
-
 		for (i = 0; i < AMDGPU_HW_IP_NUM; ++i) {
 			for (j = 0; j < amdgpu_ctx_num_entities[i]; ++j) {
 				struct drm_sched_entity *entity;
 
+				if (!ctx->entities[i])
+					continue;
+
 				entity = &ctx->entities[i][j].entity;
 				drm_sched_entity_fini(entity);
 			}
Signature · 6 lines
2.24.1

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
maintainer
#2 lore raw

Re: [PATCH 2/2] drm/amdgpu: allocate entities on demand

Am 22.01.20 um 10:33 schrieb Nirmoy Das:

Quoted 7 lines

Currently we pre-allocate entities and fences for all the HW IPs on context creation and some of which are might never be used.

This patch tries to resolve entity/fences wastage by creating entities for a HW IP only when it is required.

v2: consolidated priority checking at amdgpu_ctx_priority_permit()

Well that is not really what I had in mind. See almost all applications use only the first instance of a hw_ip.

So what we should probably do allocate entities on demand instead of whole hw_ips.

Do you know what I mean?

Regards,
Christian.

Quoted 247 lines
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c | 160 +++++++++++++-----------
  1 file changed, 87 insertions(+), 73 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +84 -73
index eecbd68db986..d704e1bebb1b 100644
@@ -45,6 +45,9 @@ const unsigned int amdgpu_ctx_num_entities[AMDGPU_HW_IP_NUM] = {
  static int amdgpu_ctx_priority_permit(struct drm_file *filp,
  				      enum drm_sched_priority priority)
  {
+	if (priority < 0 || priority >= DRM_SCHED_PRIORITY_MAX)
+		return -EINVAL;
+
  	/* NORMAL and below are accessible by everyone */
  	if (priority <= DRM_SCHED_PRIORITY_NORMAL)
  		return 0;
@@ -58,65 +61,37 @@ static int amdgpu_ctx_priority_permit(struct drm_file *filp,
  	return -EACCES;
  }
 
-static int amdgpu_ctx_init(struct amdgpu_device *adev,
-			   enum drm_sched_priority priority,
-			   struct drm_file *filp,
-			   struct amdgpu_ctx *ctx)
+static int amdgpu_ctx_init_entity(struct amdgpu_ctx *ctx, const u32 hw_ip)
  {
-	unsigned i, j;
-	int r;
+	struct amdgpu_device *adev = ctx->adev;
+	struct drm_gpu_scheduler **scheds;
+	struct drm_gpu_scheduler *sched;
+	unsigned num_scheds = 0;
+	enum drm_sched_priority priority;
+	int j, r;
 
-	if (priority < 0 || priority >= DRM_SCHED_PRIORITY_MAX)
-		return -EINVAL;
+	ctx->entities[hw_ip] = kcalloc(amdgpu_ctx_num_entities[hw_ip],
+				       sizeof(struct amdgpu_ctx_entity), GFP_KERNEL);
 
-	r = amdgpu_ctx_priority_permit(filp, priority);
-	if (r)
-		return r;
+	if (!ctx->entities[hw_ip])
+		return  -ENOMEM;
 
-	memset(ctx, 0, sizeof(*ctx));
-	ctx->adev = adev;
+	for (j = 0; j < amdgpu_ctx_num_entities[hw_ip]; ++j) {
 
-	for (i = 0; i < AMDGPU_HW_IP_NUM; ++i) {
-		ctx->entities[i] = kcalloc(amdgpu_ctx_num_entities[i],
-					   sizeof(struct amdgpu_ctx_entity),
-					   GFP_KERNEL);
+		struct amdgpu_ctx_entity *entity = &ctx->entities[hw_ip][j];
 
-		if (!ctx->entities[0]) {
-			r =  -ENOMEM;
-			goto error_cleanup_entity_memory;
+		entity->sequence = 1;
+		entity->fences = kcalloc(amdgpu_sched_jobs,
+					 sizeof(struct dma_fence*), GFP_KERNEL);
+		if (!entity->fences) {
+			r = -ENOMEM;
+			goto error_cleanup_memory;
  		}
  	}
 
-	for (i = 0; i < AMDGPU_HW_IP_NUM; ++i) {
-		for (j = 0; j < amdgpu_ctx_num_entities[i]; ++j) {
-			struct amdgpu_ctx_entity *entity = &ctx->entities[i][j];
-
-			entity->sequence = 1;
-			entity->fences = kcalloc(amdgpu_sched_jobs,
-						 sizeof(struct dma_fence*), GFP_KERNEL);
-			if (!entity->fences) {
-				r = -ENOMEM;
-				goto error_cleanup_memory;
-			}
-		}
-	}
-
-	kref_init(&ctx->refcount);
-	spin_lock_init(&ctx->ring_lock);
-	mutex_init(&ctx->lock);
-
-	ctx->reset_counter = atomic_read(&adev->gpu_reset_counter);
-	ctx->reset_counter_query = ctx->reset_counter;
-	ctx->vram_lost_counter = atomic_read(&adev->vram_lost_counter);
-	ctx->init_priority = priority;
-	ctx->override_priority = DRM_SCHED_PRIORITY_UNSET;
-
-	for (i = 0; i < AMDGPU_HW_IP_NUM; ++i) {
-		struct drm_gpu_scheduler **scheds;
-		struct drm_gpu_scheduler *sched;
-		unsigned num_scheds = 0;
-
-		switch (i) {
+	priority = (ctx->override_priority == DRM_SCHED_PRIORITY_UNSET) ?
+				ctx->init_priority : ctx->override_priority;
+	switch (hw_ip) {
  		case AMDGPU_HW_IP_GFX:
  			sched = &adev->gfx.gfx_ring[0].sched;
  			scheds = &sched;
@@ -157,12 +132,12 @@ static int amdgpu_ctx_init(struct amdgpu_device *adev,
  			scheds = adev->jpeg.jpeg_sched;
  			num_scheds =  adev->jpeg.num_jpeg_sched;
  			break;
-		}
+	}
 
-		for (j = 0; j < amdgpu_ctx_num_entities[i]; ++j)
-			r = drm_sched_entity_init(&ctx->entities[i][j].entity,
-						  priority, scheds,
-						  num_scheds, &ctx->guilty);
+	for (j = 0; j < amdgpu_ctx_num_entities[hw_ip]; ++j) {
+		r = drm_sched_entity_init(&ctx->entities[hw_ip][j].entity,
+					  priority, scheds,
+					  num_scheds, &ctx->guilty);
  		if (r)
  			goto error_cleanup_entities;
  	}
@@ -170,30 +145,51 @@ static int amdgpu_ctx_init(struct amdgpu_device *adev,
  	return 0;
 
  error_cleanup_entities:
-	for (i = 0; i < AMDGPU_HW_IP_NUM; ++i) {
-		for (j = 0; j < amdgpu_ctx_num_entities[i]; ++j)
-			drm_sched_entity_destroy(&ctx->entities[i][j].entity);
-	}
+	for (j = 0; j < amdgpu_ctx_num_entities[hw_ip]; ++j)
+		drm_sched_entity_destroy(&ctx->entities[hw_ip][j].entity);
 
  error_cleanup_memory:
-	for (i = 0; i < AMDGPU_HW_IP_NUM; ++i) {
-		for (j = 0; j < amdgpu_ctx_num_entities[i]; ++j) {
-			struct amdgpu_ctx_entity *entity = &ctx->entities[i][j];
+	for (j = 0; j < amdgpu_ctx_num_entities[hw_ip]; ++j) {
+		struct amdgpu_ctx_entity *entity = &ctx->entities[hw_ip][j];
 
-			kfree(entity->fences);
-			entity->fences = NULL;
-		}
+		kfree(entity->fences);
+		entity->fences = NULL;
  	}
 
-error_cleanup_entity_memory:
-	for (i = 0; i < AMDGPU_HW_IP_NUM; ++i) {
-		kfree(ctx->entities[i]);
-		ctx->entities[i] = NULL;
-	}
+	kfree(ctx->entities[hw_ip]);
+	ctx->entities[hw_ip] = NULL;
 
  	return r;
  }
 
+static int amdgpu_ctx_init(struct amdgpu_device *adev,
+			   enum drm_sched_priority priority,
+			   struct drm_file *filp,
+			   struct amdgpu_ctx *ctx)
+{
+	int r;
+
+	r = amdgpu_ctx_priority_permit(filp, priority);
+	if (r)
+		return r;
+
+	memset(ctx, 0, sizeof(*ctx));
+	ctx->adev = adev;
+
+	kref_init(&ctx->refcount);
+	spin_lock_init(&ctx->ring_lock);
+	mutex_init(&ctx->lock);
+
+	ctx->reset_counter = atomic_read(&adev->gpu_reset_counter);
+	ctx->reset_counter_query = ctx->reset_counter;
+	ctx->vram_lost_counter = atomic_read(&adev->vram_lost_counter);
+	ctx->init_priority = priority;
+	ctx->override_priority = DRM_SCHED_PRIORITY_UNSET;
+
+	return 0;
+
+}
+
  static void amdgpu_ctx_fini(struct kref *ref)
  {
  	struct amdgpu_ctx *ctx = container_of(ref, struct amdgpu_ctx, refcount);
@@ -204,7 +200,14 @@ static void amdgpu_ctx_fini(struct kref *ref)
  		return;
  	for (i = 0; i < AMDGPU_HW_IP_NUM; ++i) {
  		for (j = 0; j < amdgpu_ctx_num_entities[i]; ++j) {
-			struct amdgpu_ctx_entity *entity = &ctx->entities[i][j];
+			struct amdgpu_ctx_entity *entity;
+
+			if (!ctx->entities[i])
+				continue;
+
+			entity = &ctx->entities[i][j];
+			if (!entity->fences)
+				continue;
 
  			for (k = 0; k < amdgpu_sched_jobs; ++k)
  				dma_fence_put(entity->fences[k]);
@@ -241,6 +244,9 @@ int amdgpu_ctx_get_entity(struct amdgpu_ctx *ctx, u32 hw_ip, u32 instance,
  		return -EINVAL;
  	}
 
+	if (ctx->entities[hw_ip] == NULL)
+		amdgpu_ctx_init_entity(ctx, hw_ip);
+
  	*entity = &ctx->entities[hw_ip][ring].entity;
  	return 0;
  }
@@ -285,8 +291,11 @@ static void amdgpu_ctx_do_release(struct kref *ref)
 
  	ctx = container_of(ref, struct amdgpu_ctx, refcount);
  	for (i = 0; i < AMDGPU_HW_IP_NUM; ++i) {
-		for (j = 0; j < amdgpu_ctx_num_entities[i]; ++j)
+		for (j = 0; j < amdgpu_ctx_num_entities[i]; ++j) {
+			if (!ctx->entities[i])
+				continue;
  			drm_sched_entity_destroy(&ctx->entities[i][j].entity);
+		}
  	}
 
  	amdgpu_ctx_fini(ref);
@@ -579,6 +588,9 @@ long amdgpu_ctx_mgr_entity_flush(struct amdgpu_ctx_mgr *mgr, long timeout)
  			for (j = 0; j < amdgpu_ctx_num_entities[i]; ++j) {
  				struct drm_sched_entity *entity;
 
+				if (!ctx->entities[i])
+					continue;
+
  				entity = &ctx->entities[i][j].entity;
  				timeout = drm_sched_entity_flush(entity, timeout);
  			}
@@ -601,11 +613,13 @@ void amdgpu_ctx_mgr_entity_fini(struct amdgpu_ctx_mgr *mgr)
  			DRM_ERROR("ctx %p is still alive\n", ctx);
  			continue;
  		}
-

drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c void amdgpu_ctx_mgr_entity_fini(struct amdgpu_ctx_mgr *mgr) · line 625

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +3 -0
  		for (i = 0; i < AMDGPU_HW_IP_NUM; ++i) {
  			for (j = 0; j < amdgpu_ctx_num_entities[i]; ++j) {
  				struct drm_sched_entity *entity;
 
+				if (!ctx->entities[i])
+					continue;
+
  				entity = &ctx->entities[i][j].entity;
  				drm_sched_entity_fini(entity);
  			}

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

#3 lore raw

Re: [PATCH 2/2] drm/amdgpu: allocate entities on demand

Quoted 7 lines · Christian König

Am 22.01.20 um 10:33 schrieb Nirmoy Das:

Quoted 6 lines

Currently we pre-allocate entities and fences for all the HW IPs on context creation and some of which are might never be used.

This patch tries to resolve entity/fences wastage by creating entities for a HW IP only when it is required.

Quoted 7 lines
Quoted 1 line

v2: consolidated priority checking at amdgpu_ctx_priority_permit()

Well that is not really what I had in mind. See almost all applications use only the first instance of a hw_ip.

So what we should probably do allocate entities on demand instead of whole hw_ips.

Do you know what I mean?

Okay I understand what you mean. I will rework this and get back to you.

Regards,

Nirmoy

Quoted 241 lines

Regards,
Christian.

Quoted 237 lines

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c | 160 +++++++++++++-----------

  1 file changed, 87 insertions(+), 73 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +3 -0
index eecbd68db986..d704e1bebb1b 100644
@@ -45,6 +45,9 @@ const unsigned int 
 amdgpu_ctx_num_entities[AMDGPU_HW_IP_NUM] = {
   static int amdgpu_ctx_priority_permit(struct drm_file *filp,
                         enum drm_sched_priority priority)
   {
+    if (priority < 0 || priority >= DRM_SCHED_PRIORITY_MAX)
+        return -EINVAL;
+
       /* NORMAL and below are accessible by everyone */
       if (priority <= DRM_SCHED_PRIORITY_NORMAL)

          return 0;

drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +22 -43
@@ -58,65 +61,37 @@ static int amdgpu_ctx_priority_permit(struct 
 drm_file *filp,
       return -EACCES;
   }
   -static int amdgpu_ctx_init(struct amdgpu_device *adev,
-               enum drm_sched_priority priority,
-               struct drm_file *filp,
-               struct amdgpu_ctx *ctx)
+static int amdgpu_ctx_init_entity(struct amdgpu_ctx *ctx, const u32 
 hw_ip)
   {
-    unsigned i, j;
-    int r;
+    struct amdgpu_device *adev = ctx->adev;
+    struct drm_gpu_scheduler **scheds;
+    struct drm_gpu_scheduler *sched;
+    unsigned num_scheds = 0;
+    enum drm_sched_priority priority;
+    int j, r;
   -    if (priority < 0 || priority >= DRM_SCHED_PRIORITY_MAX)
-        return -EINVAL;
+    ctx->entities[hw_ip] = kcalloc(amdgpu_ctx_num_entities[hw_ip],
+                       sizeof(struct amdgpu_ctx_entity), GFP_KERNEL);
   -    r = amdgpu_ctx_priority_permit(filp, priority);
-    if (r)
-        return r;
+    if (!ctx->entities[hw_ip])
+        return  -ENOMEM;
   -    memset(ctx, 0, sizeof(*ctx));
-    ctx->adev = adev;
+    for (j = 0; j < amdgpu_ctx_num_entities[hw_ip]; ++j) {
   -    for (i = 0; i < AMDGPU_HW_IP_NUM; ++i) {
-        ctx->entities[i] = kcalloc(amdgpu_ctx_num_entities[i],
-                       sizeof(struct amdgpu_ctx_entity),
-                       GFP_KERNEL);
+        struct amdgpu_ctx_entity *entity = &ctx->entities[hw_ip][j];
   -        if (!ctx->entities[0]) {
-            r =  -ENOMEM;
-            goto error_cleanup_entity_memory;
+        entity->sequence = 1;
+        entity->fences = kcalloc(amdgpu_sched_jobs,
+                     sizeof(struct dma_fence*), GFP_KERNEL);
+        if (!entity->fences) {
+            r = -ENOMEM;
+            goto error_cleanup_memory;
           }
       }
   -    for (i = 0; i < AMDGPU_HW_IP_NUM; ++i) {
-        for (j = 0; j < amdgpu_ctx_num_entities[i]; ++j) {
-            struct amdgpu_ctx_entity *entity = &ctx->entities[i][j];
-
-            entity->sequence = 1;
-            entity->fences = kcalloc(amdgpu_sched_jobs,
-                         sizeof(struct dma_fence*), GFP_KERNEL);
-            if (!entity->fences) {
-                r = -ENOMEM;
-                goto error_cleanup_memory;
-            }
-        }
-    }
-
-    kref_init(&ctx->refcount);
-    spin_lock_init(&ctx->ring_lock);
-    mutex_init(&ctx->lock);
-
-    ctx->reset_counter = atomic_read(&adev->gpu_reset_counter);
-    ctx->reset_counter_query = ctx->reset_counter;
-    ctx->vram_lost_counter = atomic_read(&adev->vram_lost_counter);
-    ctx->init_priority = priority;
-    ctx->override_priority = DRM_SCHED_PRIORITY_UNSET;
-
-    for (i = 0; i < AMDGPU_HW_IP_NUM; ++i) {
-        struct drm_gpu_scheduler **scheds;
-        struct drm_gpu_scheduler *sched;
-        unsigned num_scheds = 0;
-
-        switch (i) {
+    priority = (ctx->override_priority == DRM_SCHED_PRIORITY_UNSET) ?
+                ctx->init_priority : ctx->override_priority;
+    switch (hw_ip) {

          case AMDGPU_HW_IP_GFX:
              sched = &adev->gfx.gfx_ring[0].sched; scheds = &sched;

drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +5 -4
@@ -157,12 +132,12 @@ static int amdgpu_ctx_init(struct amdgpu_device 
 *adev,
               scheds = adev->jpeg.jpeg_sched;
               num_scheds =  adev->jpeg.num_jpeg_sched;
               break;
-        }
+    }
   -        for (j = 0; j < amdgpu_ctx_num_entities[i]; ++j)
-            r = drm_sched_entity_init(&ctx->entities[i][j].entity,
-                          priority, scheds,
-                          num_scheds, &ctx->guilty);
+    for (j = 0; j < amdgpu_ctx_num_entities[hw_ip]; ++j) {
+        r = drm_sched_entity_init(&ctx->entities[hw_ip][j].entity,
+                      priority, scheds,
+                      num_scheds, &ctx->guilty);
           if (r)
               goto error_cleanup_entities;

      }

drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +0 -0
@@ -170,30 +145,51 @@ static int amdgpu_ctx_init(struct amdgpu_device 
 *adev,
       return 0;

    error_cleanup_entities:
-    for (i = 0; i < AMDGPU_HW_IP_NUM; ++i) {
-        for (j = 0; j < amdgpu_ctx_num_entities[i]; ++j)
- drm_sched_entity_destroy(&ctx->entities[i][j].entity);
-    }
+    for (j = 0; j < amdgpu_ctx_num_entities[hw_ip]; ++j)
+ drm_sched_entity_destroy(&ctx->entities[hw_ip][j].entity); error_cleanup_memory:
-    for (i = 0; i < AMDGPU_HW_IP_NUM; ++i) {
-        for (j = 0; j < amdgpu_ctx_num_entities[i]; ++j) { -            struct amdgpu_ctx_entity *entity = &ctx->entities[i][j]; +    for (j = 0; j < amdgpu_ctx_num_entities[hw_ip]; ++j) { +        struct amdgpu_ctx_entity *entity = &ctx->entities[hw_ip][j]; -            kfree(entity->fences);
-            entity->fences = NULL;
-        }
+        kfree(entity->fences);
+        entity->fences = NULL;
      }
  -error_cleanup_entity_memory:
-    for (i = 0; i < AMDGPU_HW_IP_NUM; ++i) {
-        kfree(ctx->entities[i]);
-        ctx->entities[i] = NULL;
-    }
+    kfree(ctx->entities[hw_ip]);
+    ctx->entities[hw_ip] = NULL;
        return r;
  }
  +static int amdgpu_ctx_init(struct amdgpu_device *adev, +               enum drm_sched_priority priority,
+               struct drm_file *filp,
+               struct amdgpu_ctx *ctx)

drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +24 -0
+{
+    int r;
+
+    r = amdgpu_ctx_priority_permit(filp, priority);
+    if (r)
+        return r;
+
+    memset(ctx, 0, sizeof(*ctx));
+    ctx->adev = adev;
+
+    kref_init(&ctx->refcount);
+    spin_lock_init(&ctx->ring_lock);
+    mutex_init(&ctx->lock);
+
+    ctx->reset_counter = atomic_read(&adev->gpu_reset_counter);
+    ctx->reset_counter_query = ctx->reset_counter;
+    ctx->vram_lost_counter = atomic_read(&adev->vram_lost_counter);
+    ctx->init_priority = priority;
+    ctx->override_priority = DRM_SCHED_PRIORITY_UNSET;
+
+    return 0;
+
+}
+

  static void amdgpu_ctx_fini(struct kref *ref)
  {
      struct amdgpu_ctx *ctx = container_of(ref, struct amdgpu_ctx, refcount);

drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +10 -1
@@ -204,7 +200,14 @@ static void amdgpu_ctx_fini(struct kref *ref)
           return;
       for (i = 0; i < AMDGPU_HW_IP_NUM; ++i) {
           for (j = 0; j < amdgpu_ctx_num_entities[i]; ++j) {
-            struct amdgpu_ctx_entity *entity = &ctx->entities[i][j];
+            struct amdgpu_ctx_entity *entity;
+
+            if (!ctx->entities[i])
+                continue;
+
+            entity = &ctx->entities[i][j];
+            if (!entity->fences)
+                continue;
                 for (k = 0; k < amdgpu_sched_jobs; ++k)
                   dma_fence_put(entity->fences[k]);
@@ -241,6 +244,9 @@ int amdgpu_ctx_get_entity(struct amdgpu_ctx *ctx, 
 u32 hw_ip, u32 instance,
           return -EINVAL;
       }
   +    if (ctx->entities[hw_ip] == NULL)
+        amdgpu_ctx_init_entity(ctx, hw_ip);
+
       *entity = &ctx->entities[hw_ip][ring].entity;
       return 0;

  }

drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +6 -1
@@ -285,8 +291,11 @@ static void amdgpu_ctx_do_release(struct kref *ref)
         ctx = container_of(ref, struct amdgpu_ctx, refcount);
       for (i = 0; i < AMDGPU_HW_IP_NUM; ++i) {
-        for (j = 0; j < amdgpu_ctx_num_entities[i]; ++j)
+        for (j = 0; j < amdgpu_ctx_num_entities[i]; ++j) {
+            if (!ctx->entities[i])
+                continue;
 drm_sched_entity_destroy(&ctx->entities[i][j].entity);
+        }
       }
         amdgpu_ctx_fini(ref);
@@ -579,6 +588,9 @@ long amdgpu_ctx_mgr_entity_flush(struct 
 amdgpu_ctx_mgr *mgr, long timeout)
               for (j = 0; j < amdgpu_ctx_num_entities[i]; ++j) {
                   struct drm_sched_entity *entity;
   +                if (!ctx->entities[i])
+                    continue;
+
                   entity = &ctx->entities[i][j].entity;
                   timeout = drm_sched_entity_flush(entity, timeout);

              }

drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +0 -1
@@ -601,11 +613,13 @@ void amdgpu_ctx_mgr_entity_fini(struct 
 amdgpu_ctx_mgr *mgr)
               DRM_ERROR("ctx %p is still alive\n", ctx);
               continue;
           }
-
           for (i = 0; i < AMDGPU_HW_IP_NUM; ++i) {
Quoted 9 lines
Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +2 -0
               for (j = 0; j < amdgpu_ctx_num_entities[i]; ++j) {
                   struct drm_sched_entity *entity;
   +                if (!ctx->entities[i])
+                    continue;
+
                   entity = &ctx->entities[i][j].entity;
                   drm_sched_entity_fini(entity);

              }

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx