Skip to contentSkip to conversation

drm/panthor: Collection of tiler heap related fixes

PATCH Superseded Boris Brezillon 2 participants 11 messages Apr 25, 2024

Subsystems (4)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Boris Brezillon

author maintainer
#0 lore raw

cover drm/panthor: Collection of tiler heap related fixes

Hello,

This is a collection of tiler heap fixes for bugs/oddities found while looking at incremental rendering.

We need to make sure those land before 6.10 is released (we still have plenty of time), so we don't need to increment the driver version to reflect the ABI changes.

Regards,

Boris

Antonino Maniscalco (1):
drm/panthor: Fix tiler OOM handling to allow incremental rendering

Boris Brezillon (2):
drm/panthor: Make sure the tiler initial/max chunks are consistent drm/panthor: Relax the check on the tiler chunk size

 drivers/gpu/drm/panthor/panthor_heap.c  | 5 ++++-
 drivers/gpu/drm/panthor/panthor_sched.c | 8 +++++++-
 2 files changed, 11 insertions(+), 2 deletions(-)
Signature · 1 line
2.44.0
author maintainer
#1 lore raw

PATCH 1/3 drm/panthor: Fix tiler OOM handling to allow incremental rendering

From: Antonino Maniscalco <[email protected]>

If the kernel couldn't allocate memory because we reached the maximum number of chunks but no render passes are in flight (panthor_heap_grow() returning -ENOMEM), we should defer the OOM handling to the FW by returning a NULL chunk. The FW will then call the tiler OOM exception handler, which is supposed to implement incremental rendering (execute an intermediate fragment job to flush the pending primitives, release the tiler memory that was used to store those primitives, and start over from where it stopped).

Fixes de8548813824 ("drm/panthor: Add the scheduler logical block")
---
 drivers/gpu/drm/panthor/panthor_sched.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/gpu/drm/panthor/panthor_sched.c +7 -1
index b3a51a6de523..6de8c0c702cb 100644
@@ -1354,7 +1354,13 @@ static int group_process_tiler_oom(struct panthor_group *group, u32 cs_id)
 					pending_frag_count, &new_chunk_va);
 	}
 
-	if (ret && ret != -EBUSY) {
+	/* If the kernel couldn't allocate memory because we reached the maximum
+	 * number of chunks (EBUSY if we have render passes in flight, ENOMEM
+	 * otherwise), we want to let the FW try to reclaim memory by waiting
+	 * for fragment jobs to land or by executing the tiler OOM exception
+	 * handler, which is supposed to implement incremental rendering.
+	 */
+	if (ret && ret != -EBUSY && ret != -ENOMEM) {
 		drm_warn(&ptdev->base, "Failed to extend the tiler heap\n");
 		group->fatal_queues |= BIT(cs_id);
 		sched_queue_delayed_work(sched, tick, 0);
Signature · 1 line
2.44.0
maintainer Reviewed-by on v1 1/3
#2 lore raw

Re: [PATCH 1/3] drm/panthor: Fix tiler OOM handling to allow incremental rendering

Quoted 11 lines · Boris Brezillon

From: Antonino Maniscalco <[email protected]>

If the kernel couldn't allocate memory because we reached the maximum number of chunks but no render passes are in flight (panthor_heap_grow() returning -ENOMEM), we should defer the OOM handling to the FW by returning a NULL chunk. The FW will then call the tiler OOM exception handler, which is supposed to implement incremental rendering (execute an intermediate fragment job to flush the pending primitives, release the tiler memory that was used to store those primitives, and start over from where it stopped).

Quoted 3 lines
Fixes de8548813824 ("drm/panthor: Add the scheduler logical block")

Although I think the real issue here is that we haven't clearly defined the return values from panthor_heap_grow - it's a bit weird to have two different error codes for the same "try again later after incremental rendering" result. But as a fix this seems most clear.

Steve

Quoted 23 lines
---
 drivers/gpu/drm/panthor/panthor_sched.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/gpu/drm/panthor/panthor_sched.c +7 -1
index b3a51a6de523..6de8c0c702cb 100644
@@ -1354,7 +1354,13 @@ static int group_process_tiler_oom(struct panthor_group *group, u32 cs_id)
 					pending_frag_count, &new_chunk_va);
 	}
 
-	if (ret && ret != -EBUSY) {
+	/* If the kernel couldn't allocate memory because we reached the maximum
+	 * number of chunks (EBUSY if we have render passes in flight, ENOMEM
+	 * otherwise), we want to let the FW try to reclaim memory by waiting
+	 * for fragment jobs to land or by executing the tiler OOM exception
+	 * handler, which is supposed to implement incremental rendering.
+	 */
+	if (ret && ret != -EBUSY && ret != -ENOMEM) {
 		drm_warn(&ptdev->base, "Failed to extend the tiler heap\n");
 		group->fatal_queues |= BIT(cs_id);
 		sched_queue_delayed_work(sched, tick, 0);
author maintainer
#3 lore raw

Re: [PATCH 1/3] drm/panthor: Fix tiler OOM handling to allow incremental rendering

Quoted 16 lines · On Thu, 25 Apr 2024 10:28:49 +0100 Steven Price <[email protected]> wrote:
Quoted 11 lines · Boris Brezillon

From: Antonino Maniscalco <[email protected]>

If the kernel couldn't allocate memory because we reached the maximum number of chunks but no render passes are in flight (panthor_heap_grow() returning -ENOMEM), we should defer the OOM handling to the FW by returning a NULL chunk. The FW will then call the tiler OOM exception handler, which is supposed to implement incremental rendering (execute an intermediate fragment job to flush the pending primitives, release the tiler memory that was used to store those primitives, and start over from where it stopped).

Quoted 3 lines
Fixes de8548813824 ("drm/panthor: Add the scheduler logical block")
Quoted 6 lines

Although I think the real issue here is that we haven't clearly defined the return values from panthor_heap_grow - it's a bit weird to have two different error codes for the same "try again later after incremental rendering" result. But as a fix this seems most clear.

Yeah, I actually considered returning -EBUSY for the 'max_chunks reached' situation, but then realized we would also want to trigger incremental rendering for actual mem allocation failures (when chunk_count < max_chunks) once the fail-able/non-blocking allocation logic is implemented, and for this kind of failure it makes more sense to return -ENOMEM, even though this implies checking against two values instead of one.

I guess returning -ENOMEM instead of -EBUSY for the case where we have render passes in-flight wouldn't be too awkward, as this can be seen as the kernel refusing to allocate more memory.

Quoted 27 lines

Steve

Quoted 23 lines
---
 drivers/gpu/drm/panthor/panthor_sched.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/gpu/drm/panthor/panthor_sched.c +7 -1
index b3a51a6de523..6de8c0c702cb 100644
@@ -1354,7 +1354,13 @@ static int group_process_tiler_oom(struct panthor_group *group, u32 cs_id)
 					pending_frag_count, &new_chunk_va);
 	}
 
-	if (ret && ret != -EBUSY) {
+	/* If the kernel couldn't allocate memory because we reached the maximum
+	 * number of chunks (EBUSY if we have render passes in flight, ENOMEM
+	 * otherwise), we want to let the FW try to reclaim memory by waiting
+	 * for fragment jobs to land or by executing the tiler OOM exception
+	 * handler, which is supposed to implement incremental rendering.
+	 */
+	if (ret && ret != -EBUSY && ret != -ENOMEM) {
 		drm_warn(&ptdev->base, "Failed to extend the tiler heap\n");
 		group->fatal_queues |= BIT(cs_id);
 		sched_queue_delayed_work(sched, tick, 0);  
author maintainer
#4 lore raw

PATCH 2/3 drm/panthor: Make sure the tiler initial/max chunks are consistent

In reply to Boris Brezillon

It doesn't make sense to have a maximum number of chunks smaller than the initial number of chunks attached to the context.

Fix the uAPI header to reflect the new constraint, and mention the undocumented "initial_chunk_count > 0" constraint while at it.

Fixes 9cca48fa4f89 ("drm/panthor: Add the heap logical block")
---
 drivers/gpu/drm/panthor/panthor_heap.c | 3 +++
 include/uapi/drm/panthor_drm.h         | 8 ++++++--
 2 files changed, 9 insertions(+), 2 deletions(-)
drivers/gpu/drm/panthor/panthor_heap.c +3 -0
index 143fa35f2e74..8728c9bb76e4 100644
@@ -281,6 +281,9 @@ int panthor_heap_create(struct panthor_heap_pool *pool,
 	if (initial_chunk_count == 0)
 		return -EINVAL;
 
+	if (initial_chunk_count < max_chunks)
+		return -EINVAL;
+
 	if (hweight32(chunk_size) != 1 ||
 	    chunk_size < SZ_256K || chunk_size > SZ_2M)
 		return -EINVAL;
include/uapi/drm/panthor_drm.h +6 -2
index dadb05ab1235..5db80a0682d5 100644
@@ -895,13 +895,17 @@ struct drm_panthor_tiler_heap_create {
 	/** @vm_id: VM ID the tiler heap should be mapped to */
 	__u32 vm_id;
 
-	/** @initial_chunk_count: Initial number of chunks to allocate. */
+	/** @initial_chunk_count: Initial number of chunks to allocate. Must be at least one. */
 	__u32 initial_chunk_count;
 
 	/** @chunk_size: Chunk size. Must be a power of two at least 256KB large. */
 	__u32 chunk_size;
 
-	/** @max_chunks: Maximum number of chunks that can be allocated. */
+	/**
+	 * @max_chunks: Maximum number of chunks that can be allocated.
+	 *
+	 * Must be at least @initial_chunk_count.
+	 */
 	__u32 max_chunks;
 
 	/**
Signature · 1 line
2.44.0
maintainer Reviewed-by on v1 2/3
#5 lore raw

Re: [PATCH 2/3] drm/panthor: Make sure the tiler initial/max chunks are consistent

Quoted 8 lines · Boris Brezillon

It doesn't make sense to have a maximum number of chunks smaller than the initial number of chunks attached to the context.

Fix the uAPI header to reflect the new constraint, and mention the undocumented "initial_chunk_count > 0" constraint while at it.

Fixes 9cca48fa4f89 ("drm/panthor: Add the heap logical block")
Quoted 43 lines
---
 drivers/gpu/drm/panthor/panthor_heap.c | 3 +++
 include/uapi/drm/panthor_drm.h         | 8 ++++++--
 2 files changed, 9 insertions(+), 2 deletions(-)
drivers/gpu/drm/panthor/panthor_heap.c +3 -0
index 143fa35f2e74..8728c9bb76e4 100644
@@ -281,6 +281,9 @@ int panthor_heap_create(struct panthor_heap_pool *pool,
 	if (initial_chunk_count == 0)
 		return -EINVAL;
 
+	if (initial_chunk_count < max_chunks)
+		return -EINVAL;
+
 	if (hweight32(chunk_size) != 1 ||
 	    chunk_size < SZ_256K || chunk_size > SZ_2M)
 		return -EINVAL;
include/uapi/drm/panthor_drm.h +6 -2
index dadb05ab1235..5db80a0682d5 100644
@@ -895,13 +895,17 @@ struct drm_panthor_tiler_heap_create {
 	/** @vm_id: VM ID the tiler heap should be mapped to */
 	__u32 vm_id;
 
-	/** @initial_chunk_count: Initial number of chunks to allocate. */
+	/** @initial_chunk_count: Initial number of chunks to allocate. Must be at least one. */
 	__u32 initial_chunk_count;
 
 	/** @chunk_size: Chunk size. Must be a power of two at least 256KB large. */
 	__u32 chunk_size;
 
-	/** @max_chunks: Maximum number of chunks that can be allocated. */
+	/**
+	 * @max_chunks: Maximum number of chunks that can be allocated.
+	 *
+	 * Must be at least @initial_chunk_count.
+	 */
 	__u32 max_chunks;
 
 	/**
maintainer
#6 lore raw

Re: [PATCH 2/3] drm/panthor: Make sure the tiler initial/max chunks are consistent

Quoted 11 lines · Steven Price
Quoted 8 lines · Boris Brezillon

It doesn't make sense to have a maximum number of chunks smaller than the initial number of chunks attached to the context.

Fix the uAPI header to reflect the new constraint, and mention the undocumented "initial_chunk_count > 0" constraint while at it.

Fixes 9cca48fa4f89 ("drm/panthor: Add the heap logical block")

Ok, I'll take that back... I've rebased (and fixed up all the out of tree patches) and this doesn't work when I actually test it!

Quoted 14 lines
Quoted 14 lines
---
 drivers/gpu/drm/panthor/panthor_heap.c | 3 +++
 include/uapi/drm/panthor_drm.h         | 8 ++++++--
 2 files changed, 9 insertions(+), 2 deletions(-)
drivers/gpu/drm/panthor/panthor_heap.c +0 -0
index 143fa35f2e74..8728c9bb76e4 100644
@@ -281,6 +281,9 @@ int panthor_heap_create(struct panthor_heap_pool *pool,
 	if (initial_chunk_count == 0)
 		return -EINVAL;
 
Quoted 1 line

drivers/gpu/drm/panthor/panthor_heap.c int panthor_heap_create(struct panthor_heap_pool *pool, · line 284

Quoted 1 line
drivers/gpu/drm/panthor/panthor_heap.c +1 -0
+	if (initial_chunk_count < max_chunks)

This should be initial_chunk_count > max_chunks. Otherwise you're requiring the initial chunk count to be equal *or greater* than the max chunks which makes no sense!

Steve

Quoted 30 lines
Quoted 29 lines
drivers/gpu/drm/panthor/panthor_heap.c +2 -0
+		return -EINVAL;
+
 	if (hweight32(chunk_size) != 1 ||
 	    chunk_size < SZ_256K || chunk_size > SZ_2M)
 		return -EINVAL;
include/uapi/drm/panthor_drm.h +6 -2
index dadb05ab1235..5db80a0682d5 100644
@@ -895,13 +895,17 @@ struct drm_panthor_tiler_heap_create {
 	/** @vm_id: VM ID the tiler heap should be mapped to */
 	__u32 vm_id;
 
-	/** @initial_chunk_count: Initial number of chunks to allocate. */
+	/** @initial_chunk_count: Initial number of chunks to allocate. Must be at least one. */
 	__u32 initial_chunk_count;
 
 	/** @chunk_size: Chunk size. Must be a power of two at least 256KB large. */
 	__u32 chunk_size;
 
-	/** @max_chunks: Maximum number of chunks that can be allocated. */
+	/**
+	 * @max_chunks: Maximum number of chunks that can be allocated.
+	 *
+	 * Must be at least @initial_chunk_count.
+	 */
 	__u32 max_chunks;
 
 	/**
author maintainer
#7 lore raw

Re: [PATCH 2/3] drm/panthor: Make sure the tiler initial/max chunks are consistent

Quoted 30 lines · On Thu, 25 Apr 2024 11:43:39 +0100 Steven Price <[email protected]> wrote:
Quoted 11 lines · Steven Price
Quoted 8 lines · Boris Brezillon

It doesn't make sense to have a maximum number of chunks smaller than the initial number of chunks attached to the context.

Fix the uAPI header to reflect the new constraint, and mention the undocumented "initial_chunk_count > 0" constraint while at it.

Fixes 9cca48fa4f89 ("drm/panthor: Add the heap logical block")

Ok, I'll take that back... I've rebased (and fixed up all the out of tree patches) and this doesn't work when I actually test it!

Quoted 14 lines
Quoted 14 lines
  
---
 drivers/gpu/drm/panthor/panthor_heap.c | 3 +++
 include/uapi/drm/panthor_drm.h         | 8 ++++++--
 2 files changed, 9 insertions(+), 2 deletions(-)
drivers/gpu/drm/panthor/panthor_heap.c +0 -0
index 143fa35f2e74..8728c9bb76e4 100644
@@ -281,6 +281,9 @@ int panthor_heap_create(struct panthor_heap_pool *pool,
 	if (initial_chunk_count == 0)
 		return -EINVAL;
 
Quoted 5 lines
Quoted 1 line

drivers/gpu/drm/panthor/panthor_heap.c int panthor_heap_create(struct panthor_heap_pool *pool, · line 284

Quoted 1 line
drivers/gpu/drm/panthor/panthor_heap.c +1 -0
+	if (initial_chunk_count < max_chunks)  

This should be initial_chunk_count > max_chunks. Otherwise you're requiring the initial chunk count to be equal *or greater* than the max chunks which makes no sense!

Damn it, here's what happens when you think your changes are too trivial to be wrong...

But I swear I would have tested the whole thing before pushing to drm-misc. :P

author maintainer
#8 lore raw

PATCH 3/3 drm/panthor: Relax the check on the tiler chunk size

In reply to Boris Brezillon

The field used to store the chunk size if 12 bits wide, and the encoding is chunk_size = chunk_header.chunk_size << 12, which gives us a theoretical [4k:8M] range. This range is further limited by implementation constraints, but those shouldn't be enforced kernel side.

Fixes 9cca48fa4f89 ("drm/panthor: Add the heap logical block")
---
 drivers/gpu/drm/panthor/panthor_heap.c | 2 +-
 include/uapi/drm/panthor_drm.h         | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
drivers/gpu/drm/panthor/panthor_heap.c +1 -1
index 8728c9bb76e4..146ea2f57764 100644
@@ -285,7 +285,7 @@ int panthor_heap_create(struct panthor_heap_pool *pool,
 		return -EINVAL;
 
 	if (hweight32(chunk_size) != 1 ||
-	    chunk_size < SZ_256K || chunk_size > SZ_2M)
+	    chunk_size < SZ_4K || chunk_size > SZ_8M)
 		return -EINVAL;
 
 	down_read(&pool->lock);
include/uapi/drm/panthor_drm.h +1 -1
index 5db80a0682d5..80c0716361b9 100644
@@ -898,7 +898,7 @@ struct drm_panthor_tiler_heap_create {
 	/** @initial_chunk_count: Initial number of chunks to allocate. Must be at least one. */
 	__u32 initial_chunk_count;
 
-	/** @chunk_size: Chunk size. Must be a power of two at least 256KB large. */
+	/** @chunk_size: Chunk size. Must be a power of two and lie in the [4k:8M] range. */
 	__u32 chunk_size;
 
 	/**
Signature · 1 line
2.44.0
maintainer
#9 lore raw

Re: [PATCH 3/3] drm/panthor: Relax the check on the tiler chunk size

Quoted 1 line · Boris Brezillon

The field used to store the chunk size if 12 bits wide, and the encoding

NIT: ^^ is

Quoted 6 lines

is chunk_size = chunk_header.chunk_size << 12, which gives us a theoretical [4k:8M] range. This range is further limited by implementation constraints, but those shouldn't be enforced kernel side.

Fixes 9cca48fa4f89 ("drm/panthor: Add the heap logical block")

There's also a kerneldoc comment above panthor_heap_create that needs updating too:

Quoted 7 lines

/**
* panthor_heap_create() - Create a heap context
* @pool: Pool to instantiate the heap context from.
* @initial_chunk_count: Number of chunk allocated at initialization time.
* Must be at least 1.
* @chunk_size: The size of each chunk. Must be a power of two between 256k
* and 2M.

I'm also a little unsure on whether this is the right change. The "implementation defined" min/max in the hardware docs say 128KiB to 8MiB. I'm not convinced it makes sense to increase the range beyond that.

As far as I'm aware the "must be a power of 2" isn't actually a requirement (it's certainly not a requirement of the storage format) so the kernel is already being more restrictive than necessary.

It seems like a good idea to keep the uAPI requirements stricter than necessary and relax them in the future if we have a good reason (e.g. new hardware supports a wider range). But matching the existing hardware range of 128KB-8MB would obviously make sense now.

Steve

Quoted 31 lines
---
 drivers/gpu/drm/panthor/panthor_heap.c | 2 +-
 include/uapi/drm/panthor_drm.h         | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
drivers/gpu/drm/panthor/panthor_heap.c +1 -1
index 8728c9bb76e4..146ea2f57764 100644
@@ -285,7 +285,7 @@ int panthor_heap_create(struct panthor_heap_pool *pool,
 		return -EINVAL;
 
 	if (hweight32(chunk_size) != 1 ||
-	    chunk_size < SZ_256K || chunk_size > SZ_2M)
+	    chunk_size < SZ_4K || chunk_size > SZ_8M)
 		return -EINVAL;
 
 	down_read(&pool->lock);
include/uapi/drm/panthor_drm.h +1 -1
index 5db80a0682d5..80c0716361b9 100644
@@ -898,7 +898,7 @@ struct drm_panthor_tiler_heap_create {
 	/** @initial_chunk_count: Initial number of chunks to allocate. Must be at least one. */
 	__u32 initial_chunk_count;
 
-	/** @chunk_size: Chunk size. Must be a power of two at least 256KB large. */
+	/** @chunk_size: Chunk size. Must be a power of two and lie in the [4k:8M] range. */
 	__u32 chunk_size;
 
 	/**
author maintainer
#10 lore raw

Re: [PATCH 3/3] drm/panthor: Relax the check on the tiler chunk size

Quoted 22 lines · On Thu, 25 Apr 2024 10:28:56 +0100 Steven Price <[email protected]> wrote:
Quoted 1 line · Boris Brezillon

The field used to store the chunk size if 12 bits wide, and the encoding

NIT: ^^ is

Quoted 6 lines

is chunk_size = chunk_header.chunk_size << 12, which gives us a theoretical [4k:8M] range. This range is further limited by implementation constraints, but those shouldn't be enforced kernel side.

Fixes 9cca48fa4f89 ("drm/panthor: Add the heap logical block")

There's also a kerneldoc comment above panthor_heap_create that needs updating too:

Quoted 7 lines

/**
* panthor_heap_create() - Create a heap context
* @pool: Pool to instantiate the heap context from.
* @initial_chunk_count: Number of chunk allocated at initialization time.
* Must be at least 1.
* @chunk_size: The size of each chunk. Must be a power of two between 256k
* and 2M.

Quoted 7 lines

I'm also a little unsure on whether this is the right change. The "implementation defined" min/max in the hardware docs say 128KiB to 8MiB. I'm not convinced it makes sense to increase the range beyond that.

As far as I'm aware the "must be a power of 2" isn't actually a requirement (it's certainly not a requirement of the storage format) so the kernel is already being more restrictive than necessary.

Ah, I got that wrong because v9 has this must-be-a-power-of-two constraint (which is also where the erroneous 256k:2M range came from BTW).

Chris, I guess you'd prefer to have the power-of-two constraint relaxed too, so we can fine-tune the chunk size.

Quoted 5 lines

It seems like a good idea to keep the uAPI requirements stricter than necessary and relax them in the future if we have a good reason (e.g. new hardware supports a wider range). But matching the existing hardware range of 128KB-8MB would obviously make sense now.

Sure, I'll restrict the range to 128k:8M as you suggest.