AMD-GFX Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] drm/buddy: fix issue that force_merge cannot free all roots
@ 2024-08-08  6:38 Lin.Cao
  2024-08-09 10:09 ` Matthew Auld
  0 siblings, 1 reply; 4+ messages in thread
From: Lin.Cao @ 2024-08-08  6:38 UTC (permalink / raw)
  To: dri-devel, amd-gfx, matthew.auld, alexander.deucher,
	christian.koenig, Arunpravin.PaneerSelvam
  Cc: lincao12, Horace.Chen

If buddy manager have more than one roots and each root have sub-block
need to be free. When drm_buddy_fini called, the first loop of
force_merge will merge and free all of the sub block of first root,
which offset is 0x0 and size is biggest(more than have of the mm size).
In subsequent force_merge rounds, if we use 0 as start and use remaining
mm size as end, the block of other roots will be skipped in
__force_merge function. It will cause the other roots can not be freed.

Solution: use roots' offset as the start could fix this issue.

Signed-off-by: Lin.Cao <lincao12@amd.com>
---
 drivers/gpu/drm/drm_buddy.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/drivers/gpu/drm/drm_buddy.c b/drivers/gpu/drm/drm_buddy.c
index 94f8c34fc293..5379687552bc 100644
--- a/drivers/gpu/drm/drm_buddy.c
+++ b/drivers/gpu/drm/drm_buddy.c
@@ -327,12 +327,14 @@ void drm_buddy_fini(struct drm_buddy *mm)
 	u64 root_size, size;
 	unsigned int order;
 	int i;
+	u64 start = 0;
 
 	size = mm->size;
 
 	for (i = 0; i < mm->n_roots; ++i) {
 		order = ilog2(size) - ilog2(mm->chunk_size);
-		__force_merge(mm, 0, size, order);
+		start = drm_buddy_block_offset(mm->roots[i]);
+		__force_merge(mm, start, start + size, order);
 
 		WARN_ON(!drm_buddy_block_is_free(mm->roots[i]));
 		drm_block_free(mm, mm->roots[i]);
-- 
2.45.2


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] drm/buddy: fix issue that force_merge cannot free all roots
  2024-08-08  6:38 Lin.Cao
@ 2024-08-09 10:09 ` Matthew Auld
  0 siblings, 0 replies; 4+ messages in thread
From: Matthew Auld @ 2024-08-09 10:09 UTC (permalink / raw)
  To: Lin.Cao, dri-devel, amd-gfx, alexander.deucher, christian.koenig,
	Arunpravin.PaneerSelvam
  Cc: Horace.Chen

Hi,

On 08/08/2024 07:38, Lin.Cao wrote:
> If buddy manager have more than one roots and each root have sub-block
> need to be free. When drm_buddy_fini called, the first loop of
> force_merge will merge and free all of the sub block of first root,
> which offset is 0x0 and size is biggest(more than have of the mm size).
> In subsequent force_merge rounds, if we use 0 as start and use remaining
> mm size as end, the block of other roots will be skipped in
> __force_merge function. It will cause the other roots can not be freed.
> 
> Solution: use roots' offset as the start could fix this issue.
> 
> Signed-off-by: Lin.Cao <lincao12@amd.com>

Nice catch.

> ---
>   drivers/gpu/drm/drm_buddy.c | 4 +++-
>   1 file changed, 3 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/gpu/drm/drm_buddy.c b/drivers/gpu/drm/drm_buddy.c
> index 94f8c34fc293..5379687552bc 100644
> --- a/drivers/gpu/drm/drm_buddy.c
> +++ b/drivers/gpu/drm/drm_buddy.c
> @@ -327,12 +327,14 @@ void drm_buddy_fini(struct drm_buddy *mm)
>   	u64 root_size, size;
>   	unsigned int order;
>   	int i;
> +	u64 start = 0;

Nit: We could maybe move this into root_size, size or even into the loop 
body below? Also no need to init.

>   
>   	size = mm->size;
>   
>   	for (i = 0; i < mm->n_roots; ++i) {
>   		order = ilog2(size) - ilog2(mm->chunk_size);
> -		__force_merge(mm, 0, size, order);
> +		start = drm_buddy_block_offset(mm->roots[i]);
> +		__force_merge(mm, start, start + size, order);
>   
>   		WARN_ON(!drm_buddy_block_is_free(mm->roots[i]));

We do seem to have a testcase for this at the bottom of 
drm_test_buddy_alloc_clear(), so either it is not triggering the 
WARN_ON() here in which case we should maybe improve that. Or it is, but 
kunit doesn't treat that as a test failure? Maybe we can call something 
like kunit_fail_current_test() here if that WARN_ON is triggered?

For reference our CI is just running all drm selftests with:

/kernel/tools/testing/kunit/kunit.py run --kunitconfig 
/kernel/drivers/gpu/drm/tests/.kunitconfig

>   		drm_block_free(mm, mm->roots[i]);

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH] drm/buddy: fix issue that force_merge cannot free all roots
@ 2024-08-13  9:44 Lin.Cao
  2024-08-14 14:31 ` Matthew Auld
  0 siblings, 1 reply; 4+ messages in thread
From: Lin.Cao @ 2024-08-13  9:44 UTC (permalink / raw)
  To: dri-devel, amd-gfx, matthew.auld, alexander.deucher,
	christian.koenig, Arunpravin.PaneerSelvam
  Cc: lincao12, Horace.Chen

If buddy manager have more than one roots and each root have sub-block
need to be free. When drm_buddy_fini called, the first loop of
force_merge will merge and free all of the sub block of first root,
which offset is 0x0 and size is biggest(more than have of the mm size).
In subsequent force_merge rounds, if we use 0 as start and use remaining
mm size as end, the block of other roots will be skipped in
__force_merge function. It will cause the other roots can not be freed.

Solution: use roots' offset as the start could fix this issue.

Signed-off-by: Lin.Cao <lincao12@amd.com>
---
 drivers/gpu/drm/drm_buddy.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/drivers/gpu/drm/drm_buddy.c b/drivers/gpu/drm/drm_buddy.c
index 94f8c34fc293..b3f0dd652088 100644
--- a/drivers/gpu/drm/drm_buddy.c
+++ b/drivers/gpu/drm/drm_buddy.c
@@ -324,7 +324,7 @@ EXPORT_SYMBOL(drm_buddy_init);
  */
 void drm_buddy_fini(struct drm_buddy *mm)
 {
-	u64 root_size, size;
+	u64 root_size, size, start;
 	unsigned int order;
 	int i;
 
@@ -332,7 +332,8 @@ void drm_buddy_fini(struct drm_buddy *mm)
 
 	for (i = 0; i < mm->n_roots; ++i) {
 		order = ilog2(size) - ilog2(mm->chunk_size);
-		__force_merge(mm, 0, size, order);
+		start = drm_buddy_block_offset(mm->roots[i]);
+		__force_merge(mm, start, start + size, order);
 
 		WARN_ON(!drm_buddy_block_is_free(mm->roots[i]));
 		drm_block_free(mm, mm->roots[i]);
-- 
2.45.2


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] drm/buddy: fix issue that force_merge cannot free all roots
  2024-08-13  9:44 [PATCH] drm/buddy: fix issue that force_merge cannot free all roots Lin.Cao
@ 2024-08-14 14:31 ` Matthew Auld
  0 siblings, 0 replies; 4+ messages in thread
From: Matthew Auld @ 2024-08-14 14:31 UTC (permalink / raw)
  To: Lin.Cao, dri-devel, amd-gfx, alexander.deucher, christian.koenig,
	Arunpravin.PaneerSelvam
  Cc: Horace.Chen

On 13/08/2024 10:44, Lin.Cao wrote:
> If buddy manager have more than one roots and each root have sub-block
> need to be free. When drm_buddy_fini called, the first loop of
> force_merge will merge and free all of the sub block of first root,
> which offset is 0x0 and size is biggest(more than have of the mm size).
> In subsequent force_merge rounds, if we use 0 as start and use remaining
> mm size as end, the block of other roots will be skipped in
> __force_merge function. It will cause the other roots can not be freed.
> 
> Solution: use roots' offset as the start could fix this issue.

Were you able to take a look at the test side for this? See previous 
reply: 
https://lore.kernel.org/dri-devel/3fdd9175-832a-4113-8aaa-6039925c5a4d@intel.com/

Patch itself is good, but would be good to understand why the test for 
this is not failing, and try to improve that also.

> 
> Signed-off-by: Lin.Cao <lincao12@amd.com>
> ---
>   drivers/gpu/drm/drm_buddy.c | 5 +++--
>   1 file changed, 3 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/gpu/drm/drm_buddy.c b/drivers/gpu/drm/drm_buddy.c
> index 94f8c34fc293..b3f0dd652088 100644
> --- a/drivers/gpu/drm/drm_buddy.c
> +++ b/drivers/gpu/drm/drm_buddy.c
> @@ -324,7 +324,7 @@ EXPORT_SYMBOL(drm_buddy_init);
>    */
>   void drm_buddy_fini(struct drm_buddy *mm)
>   {
> -	u64 root_size, size;
> +	u64 root_size, size, start;
>   	unsigned int order;
>   	int i;
>   
> @@ -332,7 +332,8 @@ void drm_buddy_fini(struct drm_buddy *mm)
>   
>   	for (i = 0; i < mm->n_roots; ++i) {
>   		order = ilog2(size) - ilog2(mm->chunk_size);
> -		__force_merge(mm, 0, size, order);
> +		start = drm_buddy_block_offset(mm->roots[i]);
> +		__force_merge(mm, start, start + size, order);
>   
>   		WARN_ON(!drm_buddy_block_is_free(mm->roots[i]));
>   		drm_block_free(mm, mm->roots[i]);

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2024-08-14 14:31 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-08-13  9:44 [PATCH] drm/buddy: fix issue that force_merge cannot free all roots Lin.Cao
2024-08-14 14:31 ` Matthew Auld
  -- strict thread matches above, loose matches on Subject: below --
2024-08-08  6:38 Lin.Cao
2024-08-09 10:09 ` Matthew Auld

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox