Skip to contentSkip to conversation

dma-buf/reservation: shouldn't kfree staged when slot available

PATCH In review Monk Liu 3 participants 10 messages Feb 28, 2018 – Mar 5, 2018 DMA buffer sharing framework

lore
Share & tools
lore

Monk Liu

author
#0 lore raw

PATCH 1/1 dma-buf/reservation: shouldn't kfree staged when slot available

under below scenario the obj->fence would refer to a wild pointer:

1,call reservation_object_reserved_shared
2,call reservation_object_add_shared_fence
3,call reservation_object_reserved_shared
4,call reservation_object_add_shared_fence

in step 1, staged is allocated,

in step 2, code path will go reservation_object_add_shared_replace() and obj->fence would be assigned as staged (through RCU_INIT_POINTER)

in step 3, obj->staged will be freed(by simple kfree), which make obj->fence point to a wild pointer...

in step 4, code path will go reservation_object_add_shared_inplace() and inside it the @fobj (which equals to @obj->staged, set by above steps) is already a wild pointer

should remov the kfree on staged in reservation_object_reserve_shared()

Change-Id If7c01f1b4be3d3d8a81efa90216841f79ab1fc1c
---
 drivers/dma-buf/reservation.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
drivers/dma-buf/reservation.c +2 -5
index 375de41..b473ccc 100644
@@ -74,12 +74,9 @@ int reservation_object_reserve_shared(struct reservation_object *obj)
 	old = reservation_object_get_list(obj);
 
 	if (old && old->shared_max) {
-		if (old->shared_count < old->shared_max) {
-			/* perform an in-place update */
-			kfree(obj->staged);
-			obj->staged = NULL;
+		if (old->shared_count < old->shared_max)
 			return 0;
-		} else
+		else
 			max = old->shared_max * 2;
 	} else
 		max = 4;
Signature · 6 lines
2.7.4

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

Am 28.02.2018 um 07:44 schrieb Monk Liu:

Quoted 7 lines

under below scenario the obj->fence would refer to a wild pointer:

1,call reservation_object_reserved_shared
2,call reservation_object_add_shared_fence
3,call reservation_object_reserved_shared
4,call reservation_object_add_shared_fence

Quoted 7 lines

in step 1, staged is allocated,

in step 2, code path will go reservation_object_add_shared_replace() and obj->fence would be assigned as staged (through RCU_INIT_POINTER)

in step 3, obj->staged will be freed(by simple kfree), which make obj->fence point to a wild pointer...

Well that explanation is still nonsense. See reservation_object_add_shared_fence:

Quoted 1 line

        obj->staged = NULL;

Among the first things reservation_object_add_shared_fence() does is it sets obj->staged to NULL.

So step 3 will not free anything and we never have a wild pointer.

Regards,
Christian.

Quoted 22 lines

in step 4, code path will go reservation_object_add_shared_inplace() and inside it the @fobj (which equals to @obj->staged, set by above steps) is already a wild pointer

should remov the kfree on staged in reservation_object_reserve_shared()

Change-Id If7c01f1b4be3d3d8a81efa90216841f79ab1fc1c
---
  drivers/dma-buf/reservation.c | 7 ++-----
  1 file changed, 2 insertions(+), 5 deletions(-)
drivers/dma-buf/reservation.c +0 -1
index 375de41..b473ccc 100644
@@ -74,12 +74,9 @@ int reservation_object_reserve_shared(struct reservation_object *obj)
  	old = reservation_object_get_list(obj);
 
  	if (old && old->shared_max) {
-		if (old->shared_count < old->shared_max) {

drivers/dma-buf/reservation.c int reservation_object_reserve_shared(struct reservation_object *obj) · line 82

Quoted 10 lines
drivers/dma-buf/reservation.c +2 -4
-			/* perform an in-place update */
-			kfree(obj->staged);
-			obj->staged = NULL;
+		if (old->shared_count < old->shared_max)
  			return 0;
-		} else
+		else
  			max = old->shared_max * 2;
  	} else
  		max = 4;

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

author
#2 lore raw

Hi Christian

You are right on that part of obj-staged is set to NULL in add_fence,
So my following question will be why we kfree(obj->staged) in reserve_shared() if staged is always NULL in that point ?

Thanks
/Monk

-----Original Message-----
From: Christian König [mailto:[email protected]]
Sent: 2018年2月28日 16:27
To: Liu, Monk <[email protected]>; [email protected]; [email protected]
Subject: Re: [PATCH] dma-buf/reservation: shouldn't kfree staged when slot available

Am 28.02.2018 um 07:44 schrieb Monk Liu:

Quoted 7 lines

under below scenario the obj->fence would refer to a wild pointer:

1,call reservation_object_reserved_shared
2,call reservation_object_add_shared_fence
3,call reservation_object_reserved_shared
4,call reservation_object_add_shared_fence

Quoted 7 lines

in step 1, staged is allocated,

in step 2, code path will go reservation_object_add_shared_replace() and obj->fence would be assigned as staged (through RCU_INIT_POINTER)

in step 3, obj->staged will be freed(by simple kfree), which make obj->fence point to a wild pointer...

Well that explanation is still nonsense. See
reservation_object_add_shared_fence:

Quoted 1 line

        obj->staged = NULL;

Among the first things reservation_object_add_shared_fence() does is it sets obj->staged to NULL.

So step 3 will not free anything and we never have a wild pointer.

Regards,
Christian.

Quoted 22 lines

in step 4, code path will go reservation_object_add_shared_inplace() and inside it the @fobj (which equals to @obj->staged, set by above steps) is already a wild pointer

should remov the kfree on staged in reservation_object_reserve_shared()

Change-Id If7c01f1b4be3d3d8a81efa90216841f79ab1fc1c
---
  drivers/dma-buf/reservation.c | 7 ++-----
  1 file changed, 2 insertions(+), 5 deletions(-)
drivers/dma-buf/reservation.c +0 -1
index 375de41..b473ccc 100644
@@ -74,12 +74,9 @@ int reservation_object_reserve_shared(struct reservation_object *obj)
  	old = reservation_object_get_list(obj);
 
  	if (old && old->shared_max) {
-		if (old->shared_count < old->shared_max) {

drivers/dma-buf/reservation.c int reservation_object_reserve_shared(struct reservation_object *obj) · line 82

Quoted 10 lines
drivers/dma-buf/reservation.c +2 -4
-			/* perform an in-place update */
-			kfree(obj->staged);
-			obj->staged = NULL;
+		if (old->shared_count < old->shared_max)
  			return 0;
-		} else
+		else
  			max = old->shared_max * 2;
  	} else
  		max = 4;

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

#3 lore raw

Am 05.03.2018 um 08:55 schrieb Liu, Monk:

Quoted 4 lines

Hi Christian

You are right on that part of obj-staged is set to NULL in add_fence,
So my following question will be why we kfree(obj->staged) in reserve_shared() if staged is always NULL in that point ?

Good question, I haven't wrote code that so I can't fully answer.

Maybe Chris or Maarten know more about that.

Christian.

Quoted 72 lines

Thanks
/Monk

-----Original Message-----
From: Christian König [mailto:[email protected]]
Sent: 2018年2月28日 16:27
To: Liu, Monk <[email protected]>; [email protected]; [email protected]
Subject: Re: [PATCH] dma-buf/reservation: shouldn't kfree staged when slot available

Am 28.02.2018 um 07:44 schrieb Monk Liu:

Quoted 7 lines

under below scenario the obj->fence would refer to a wild pointer:

1,call reservation_object_reserved_shared
2,call reservation_object_add_shared_fence
3,call reservation_object_reserved_shared
4,call reservation_object_add_shared_fence

Quoted 7 lines

in step 1, staged is allocated,

in step 2, code path will go reservation_object_add_shared_replace() and obj->fence would be assigned as staged (through RCU_INIT_POINTER)

in step 3, obj->staged will be freed(by simple kfree), which make obj->fence point to a wild pointer...

Well that explanation is still nonsense. See
reservation_object_add_shared_fence:

Quoted 1 line

        obj->staged = NULL;

Among the first things reservation_object_add_shared_fence() does is it sets obj->staged to NULL.

So step 3 will not free anything and we never have a wild pointer.

Regards,
Christian.

Quoted 21 lines

in step 4, code path will go reservation_object_add_shared_inplace() and inside it the @fobj (which equals to @obj->staged, set by above steps) is already a wild pointer

should remov the kfree on staged in reservation_object_reserve_shared()

Change-Id If7c01f1b4be3d3d8a81efa90216841f79ab1fc1c
---
   drivers/dma-buf/reservation.c | 7 ++-----
   1 file changed, 2 insertions(+), 5 deletions(-)
drivers/dma-buf/reservation.c +0 -1
index 375de41..b473ccc 100644
@@ -74,12 +74,9 @@ int reservation_object_reserve_shared(struct reservation_object *obj)
   	old = reservation_object_get_list(obj);
 
   	if (old && old->shared_max) {
-		if (old->shared_count < old->shared_max) {

drivers/dma-buf/reservation.c int reservation_object_reserve_shared(struct reservation_object *obj) · line 82

Quoted 10 lines
drivers/dma-buf/reservation.c +2 -4
-			/* perform an in-place update */
-			kfree(obj->staged);
-			obj->staged = NULL;
+		if (old->shared_count < old->shared_max)
   			return 0;
-		} else
+		else
   			max = old->shared_max * 2;
   	} else
   		max = 4;

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

author
#4 lore raw

And by the way, I add "if (staged!=NULL) BUG();" prior to "kfree(obj->staged)" in reserve_shared() routine, and this BUG() is actually hit, The stack dump shows it is hit during the vm_bo_update() in gem_va_update()...

Besides, the whole reservation logic still looks a little weired to me ... especially this staged part ...

Thanks

/Monk

-----Original Message-----
From: Christian König [mailto:[email protected]]
Sent: 2018年3月5日 19:22
To: Liu, Monk <[email protected]>; Koenig, Christian <[email protected]>; [email protected]; [email protected]
Subject: Re: [PATCH] dma-buf/reservation: shouldn't kfree staged when slot available

Am 05.03.2018 um 08:55 schrieb Liu, Monk:

Quoted 4 lines

Hi Christian

You are right on that part of obj-staged is set to NULL in add_fence,
So my following question will be why we kfree(obj->staged) in reserve_shared() if staged is always NULL in that point ?

Good question, I haven't wrote code that so I can't fully answer.

Maybe Chris or Maarten know more about that.

Christian.

Quoted 59 lines

Thanks
/Monk

-----Original Message-----
From: Christian König [mailto:[email protected]]
Sent: 2018年2月28日 16:27
To: Liu, Monk <[email protected]>; [email protected]; [email protected]
Subject: Re: [PATCH] dma-buf/reservation: shouldn't kfree staged when slot available

Am 28.02.2018 um 07:44 schrieb Monk Liu:

Quoted 7 lines

under below scenario the obj->fence would refer to a wild pointer:

1,call reservation_object_reserved_shared
2,call reservation_object_add_shared_fence
3,call reservation_object_reserved_shared
4,call reservation_object_add_shared_fence

Quoted 7 lines

in step 1, staged is allocated,

in step 2, code path will go reservation_object_add_shared_replace() and obj->fence would be assigned as staged (through RCU_INIT_POINTER)

in step 3, obj->staged will be freed(by simple kfree), which make obj->fence point to a wild pointer...

Well that explanation is still nonsense. See
reservation_object_add_shared_fence:

Quoted 1 line

        obj->staged = NULL;

Among the first things reservation_object_add_shared_fence() does is it sets obj->staged to NULL.

So step 3 will not free anything and we never have a wild pointer.

Regards,
Christian.

Quoted 20 lines

in step 4, code path will go reservation_object_add_shared_inplace() and inside it the @fobj (which equals to @obj->staged, set by above steps) is already a wild pointer

should remov the kfree on staged in
reservation_object_reserve_shared()

Change-Id If7c01f1b4be3d3d8a81efa90216841f79ab1fc1c
---
   drivers/dma-buf/reservation.c | 7 ++-----
   1 file changed, 2 insertions(+), 5 deletions(-)
(quoted diff) +0 -0

b/drivers/dma-buf/reservation.c index 375de41..b473ccc 100644

drivers/dma-buf/reservation.c +0 -0
@@ -74,12 +74,9 @@ int reservation_object_reserve_shared(struct reservation_object *obj)
   	old = reservation_object_get_list(obj);
 
Quoted 16 lines

drivers/dma-buf/reservation.c int reservation_object_reserve_shared(struct reservation_object *obj) · line 82

Quoted 12 lines
drivers/dma-buf/reservation.c +2 -5
   	if (old && old->shared_max) {
-		if (old->shared_count < old->shared_max) {
-			/* perform an in-place update */
-			kfree(obj->staged);
-			obj->staged = NULL;
+		if (old->shared_count < old->shared_max)
   			return 0;
-		} else
+		else
   			max = old->shared_max * 2;
   	} else
   		max = 4;

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

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

maintainer
#5 lore raw

Am 05.03.2018 um 12:25 schrieb Liu, Monk:

Quoted 2 lines

And by the way, I add "if (staged!=NULL) BUG();" prior to "kfree(obj->staged)" in reserve_shared() routine, and this BUG() is actually hit, The stack dump shows it is hit during the vm_bo_update() in gem_va_update()...

That is expected. The staged handling just makes sure that there is room available, it doesn't guarantee that it is actually used.

E.g. we can end up reserving a fence slot, but then find that we actually don't need it.

Christian.

Quoted 16 lines

Besides, the whole reservation logic still looks a little weired to me ... especially this staged part ...

Thanks

/Monk

-----Original Message-----
From: Christian König [mailto:[email protected]]
Sent: 2018年3月5日 19:22
To: Liu, Monk <[email protected]>; Koenig, Christian <[email protected]>; [email protected]; [email protected]
Subject: Re: [PATCH] dma-buf/reservation: shouldn't kfree staged when slot available

Am 05.03.2018 um 08:55 schrieb Liu, Monk:

Quoted 2 lines

Hi Christian

Quoted 81 lines
Quoted 2 lines

You are right on that part of obj-staged is set to NULL in add_fence,
So my following question will be why we kfree(obj->staged) in reserve_shared() if staged is always NULL in that point ?

Good question, I haven't wrote code that so I can't fully answer.

Maybe Chris or Maarten know more about that.

Christian.

Quoted 73 lines

Thanks
/Monk

-----Original Message-----
From: Christian König [mailto:[email protected]]
Sent: 2018年2月28日 16:27
To: Liu, Monk <[email protected]>; [email protected]; [email protected]
Subject: Re: [PATCH] dma-buf/reservation: shouldn't kfree staged when slot available

Am 28.02.2018 um 07:44 schrieb Monk Liu:

Quoted 7 lines

under below scenario the obj->fence would refer to a wild pointer:

1,call reservation_object_reserved_shared
2,call reservation_object_add_shared_fence
3,call reservation_object_reserved_shared
4,call reservation_object_add_shared_fence

Quoted 7 lines

in step 1, staged is allocated,

in step 2, code path will go reservation_object_add_shared_replace() and obj->fence would be assigned as staged (through RCU_INIT_POINTER)

in step 3, obj->staged will be freed(by simple kfree), which make obj->fence point to a wild pointer...

Well that explanation is still nonsense. See
reservation_object_add_shared_fence:

Quoted 1 line
          obj->staged = NULL;

Among the first things reservation_object_add_shared_fence() does is it sets obj->staged to NULL.

So step 3 will not free anything and we never have a wild pointer.

Regards,
Christian.

Quoted 22 lines

in step 4, code path will go reservation_object_add_shared_inplace() and inside it the @fobj (which equals to @obj->staged, set by above steps) is already a wild pointer

should remov the kfree on staged in
reservation_object_reserve_shared()

Change-Id If7c01f1b4be3d3d8a81efa90216841f79ab1fc1c
---
    drivers/dma-buf/reservation.c | 7 ++-----
    1 file changed, 2 insertions(+), 5 deletions(-)
(quoted diff) +0 -0

b/drivers/dma-buf/reservation.c index 375de41..b473ccc 100644

drivers/dma-buf/reservation.c +0 -1
@@ -74,12 +74,9 @@ int reservation_object_reserve_shared(struct reservation_object *obj)
    	old = reservation_object_get_list(obj);
 
    	if (old && old->shared_max) {
-		if (old->shared_count < old->shared_max) {

drivers/dma-buf/reservation.c int reservation_object_reserve_shared(struct reservation_object *obj) · line 82

Quoted 10 lines
drivers/dma-buf/reservation.c +2 -4
-			/* perform an in-place update */
-			kfree(obj->staged);
-			obj->staged = NULL;
+		if (old->shared_count < old->shared_max)
    			return 0;
-		} else
+		else
    			max = old->shared_max * 2;
    	} else
    		max = 4;

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

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

author
#6 lore raw

But the thing confuse me is according to the design, if driver keep calling reserve_shared() prior to add_fence(), and with lock held of cause, That BUG() shouldn't hit, so there are two things in face looks weired to me:
1) by design in reserve_shared(), obj->staged should be already NULL, so why we kfree on it
2) in fact, amdgpu can hit the case that obj->staged is not NULL in reserved_shared(), don't know how it lead here

Any thought ?

/Monk

-----Original Message-----
From: Koenig, Christian
Sent: 2018年3月5日 19:29
To: Liu, Monk <[email protected]>; [email protected]; [email protected]
Subject: Re: [PATCH] dma-buf/reservation: shouldn't kfree staged when slot available

Am 05.03.2018 um 12:25 schrieb Liu, Monk:

Quoted 2 lines

And by the way, I add "if (staged!=NULL) BUG();" prior to
"kfree(obj->staged)" in reserve_shared() routine, and this BUG() is actually hit, The stack dump shows it is hit during the vm_bo_update() in gem_va_update()...

That is expected. The staged handling just makes sure that there is room available, it doesn't guarantee that it is actually used.

E.g. we can end up reserving a fence slot, but then find that we actually don't need it.

Christian.

Quoted 19 lines

Besides, the whole reservation logic still looks a little weired to me ... especially this staged part ...

Thanks

/Monk

-----Original Message-----
From: Christian König [mailto:[email protected]]
Sent: 2018年3月5日 19:22
To: Liu, Monk <[email protected]>; Koenig, Christian <[email protected]>; [email protected]; [email protected]
Subject: Re: [PATCH] dma-buf/reservation: shouldn't kfree staged when slot available

Am 05.03.2018 um 08:55 schrieb Liu, Monk:

Quoted 2 lines

Hi Christian

Quoted 82 lines
Quoted 2 lines

You are right on that part of obj-staged is set to NULL in add_fence,
So my following question will be why we kfree(obj->staged) in reserve_shared() if staged is always NULL in that point ?

Good question, I haven't wrote code that so I can't fully answer.

Maybe Chris or Maarten know more about that.

Christian.

Quoted 74 lines

Thanks
/Monk

-----Original Message-----
From: Christian König [mailto:[email protected]]
Sent: 2018年2月28日 16:27
To: Liu, Monk <[email protected]>; [email protected]; [email protected]
Subject: Re: [PATCH] dma-buf/reservation: shouldn't kfree staged when slot available

Am 28.02.2018 um 07:44 schrieb Monk Liu:

Quoted 7 lines

under below scenario the obj->fence would refer to a wild pointer:

1,call reservation_object_reserved_shared
2,call reservation_object_add_shared_fence
3,call reservation_object_reserved_shared
4,call reservation_object_add_shared_fence

Quoted 8 lines

in step 1, staged is allocated,

in step 2, code path will go reservation_object_add_shared_replace() and obj->fence would be assigned as staged (through RCU_INIT_POINTER)

in step 3, obj->staged will be freed(by simple kfree), which make obj->fence point to a wild pointer...

Well that explanation is still nonsense. See
reservation_object_add_shared_fence:

Quoted 1 line
          obj->staged = NULL;

Among the first things reservation_object_add_shared_fence() does is it sets obj->staged to NULL.

So step 3 will not free anything and we never have a wild pointer.

Regards,
Christian.

Quoted 22 lines

in step 4, code path will go reservation_object_add_shared_inplace() and inside it the @fobj (which equals to @obj->staged, set by above steps) is already a wild pointer

should remov the kfree on staged in
reservation_object_reserve_shared()

Change-Id If7c01f1b4be3d3d8a81efa90216841f79ab1fc1c
---
    drivers/dma-buf/reservation.c | 7 ++-----
    1 file changed, 2 insertions(+), 5 deletions(-)
(quoted diff) +0 -0

b/drivers/dma-buf/reservation.c index 375de41..b473ccc 100644

drivers/dma-buf/reservation.c +0 -1
@@ -74,12 +74,9 @@ int reservation_object_reserve_shared(struct reservation_object *obj)
    	old = reservation_object_get_list(obj);
 
    	if (old && old->shared_max) {
-		if (old->shared_count < old->shared_max) {

drivers/dma-buf/reservation.c int reservation_object_reserve_shared(struct reservation_object *obj) · line 82

Quoted 10 lines
drivers/dma-buf/reservation.c +2 -4
-			/* perform an in-place update */
-			kfree(obj->staged);
-			obj->staged = NULL;
+		if (old->shared_count < old->shared_max)
    			return 0;
-		} else
+		else
    			max = old->shared_max * 2;
    	} else
    		max = 4;

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

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

maintainer
#7 lore raw

Am 05.03.2018 um 12:37 schrieb Liu, Monk:

Quoted 3 lines

But the thing confuse me is according to the design, if driver keep calling reserve_shared() prior to add_fence(), and with lock held of cause, That BUG() shouldn't hit, so there are two things in face looks weired to me:
1) by design in reserve_shared(), obj->staged should be already NULL, so why we kfree on it

No, that is not correct.

Quoted 1 line

2) in fact, amdgpu can hit the case that obj->staged is not NULL in reserved_shared(), don't know how it lead here

We reserved a fence slot without using it, so it is still there when reserve_shared() is called.

Christian.

Quoted 121 lines

Any thought ?

/Monk

-----Original Message-----
From: Koenig, Christian
Sent: 2018年3月5日 19:29
To: Liu, Monk <[email protected]>; [email protected]; [email protected]
Subject: Re: [PATCH] dma-buf/reservation: shouldn't kfree staged when slot available

Am 05.03.2018 um 12:25 schrieb Liu, Monk:

Quoted 2 lines

And by the way, I add "if (staged!=NULL) BUG();" prior to
"kfree(obj->staged)" in reserve_shared() routine, and this BUG() is actually hit, The stack dump shows it is hit during the vm_bo_update() in gem_va_update()...

That is expected. The staged handling just makes sure that there is room available, it doesn't guarantee that it is actually used.

E.g. we can end up reserving a fence slot, but then find that we actually don't need it.

Christian.

Quoted 100 lines

Besides, the whole reservation logic still looks a little weired to me ... especially this staged part ...

Thanks

/Monk

-----Original Message-----
From: Christian König [mailto:[email protected]]
Sent: 2018年3月5日 19:22
To: Liu, Monk <[email protected]>; Koenig, Christian <[email protected]>; [email protected]; [email protected]
Subject: Re: [PATCH] dma-buf/reservation: shouldn't kfree staged when slot available

Am 05.03.2018 um 08:55 schrieb Liu, Monk:

Quoted 4 lines

Hi Christian

You are right on that part of obj-staged is set to NULL in add_fence,
So my following question will be why we kfree(obj->staged) in reserve_shared() if staged is always NULL in that point ?

Good question, I haven't wrote code that so I can't fully answer.

Maybe Chris or Maarten know more about that.

Christian.

Quoted 74 lines

Thanks
/Monk

-----Original Message-----
From: Christian König [mailto:[email protected]]
Sent: 2018年2月28日 16:27
To: Liu, Monk <[email protected]>; [email protected]; [email protected]
Subject: Re: [PATCH] dma-buf/reservation: shouldn't kfree staged when slot available

Am 28.02.2018 um 07:44 schrieb Monk Liu:

Quoted 7 lines

under below scenario the obj->fence would refer to a wild pointer:

1,call reservation_object_reserved_shared
2,call reservation_object_add_shared_fence
3,call reservation_object_reserved_shared
4,call reservation_object_add_shared_fence

Quoted 8 lines

in step 1, staged is allocated,

in step 2, code path will go reservation_object_add_shared_replace() and obj->fence would be assigned as staged (through RCU_INIT_POINTER)

in step 3, obj->staged will be freed(by simple kfree), which make obj->fence point to a wild pointer...

Well that explanation is still nonsense. See
reservation_object_add_shared_fence:

Quoted 1 line
           obj->staged = NULL;

Among the first things reservation_object_add_shared_fence() does is it sets obj->staged to NULL.

So step 3 will not free anything and we never have a wild pointer.

Regards,
Christian.

Quoted 22 lines

in step 4, code path will go reservation_object_add_shared_inplace() and inside it the @fobj (which equals to @obj->staged, set by above steps) is already a wild pointer

should remov the kfree on staged in
reservation_object_reserve_shared()

Change-Id If7c01f1b4be3d3d8a81efa90216841f79ab1fc1c
---
     drivers/dma-buf/reservation.c | 7 ++-----
     1 file changed, 2 insertions(+), 5 deletions(-)
(quoted diff) +0 -0

b/drivers/dma-buf/reservation.c index 375de41..b473ccc 100644

drivers/dma-buf/reservation.c +0 -1
@@ -74,12 +74,9 @@ int reservation_object_reserve_shared(struct reservation_object *obj)
     	old = reservation_object_get_list(obj);
 
     	if (old && old->shared_max) {
-		if (old->shared_count < old->shared_max) {

drivers/dma-buf/reservation.c int reservation_object_reserve_shared(struct reservation_object *obj) · line 82

Quoted 10 lines
drivers/dma-buf/reservation.c +2 -4
-			/* perform an in-place update */
-			kfree(obj->staged);
-			obj->staged = NULL;
+		if (old->shared_count < old->shared_max)
     			return 0;
-		} else
+		else
     			max = old->shared_max * 2;
     	} else
     		max = 4;

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

author
#8 lore raw

Can you give more details ? thanks

/Monk

-----Original Message-----
From: Koenig, Christian
Sent: 2018年3月5日 19:39
To: Liu, Monk <[email protected]>; [email protected]; [email protected]
Subject: Re: [PATCH] dma-buf/reservation: shouldn't kfree staged when slot available

Am 05.03.2018 um 12:37 schrieb Liu, Monk:

Quoted 4 lines

But the thing confuse me is according to the design, if driver keep
calling reserve_shared() prior to add_fence(), and with lock held of cause, That BUG() shouldn't hit, so there are two things in face looks weired to me:
1) by design in reserve_shared(), obj->staged should be already NULL,
so why we kfree on it

No, that is not correct.

Quoted 2 lines

2) in fact, amdgpu can hit the case that obj->staged is not NULL in reserved_shared(), don't know how it lead here

We reserved a fence slot without using it, so it is still there when reserve_shared() is called.

Christian.

Quoted 14 lines

Any thought ?

/Monk

-----Original Message-----
From: Koenig, Christian
Sent: 2018年3月5日 19:29
To: Liu, Monk <[email protected]>; [email protected]; [email protected]
Subject: Re: [PATCH] dma-buf/reservation: shouldn't kfree staged when slot available

Quoted 111 lines

Am 05.03.2018 um 12:25 schrieb Liu, Monk:

Quoted 2 lines

And by the way, I add "if (staged!=NULL) BUG();" prior to
"kfree(obj->staged)" in reserve_shared() routine, and this BUG() is actually hit, The stack dump shows it is hit during the vm_bo_update() in gem_va_update()...

That is expected. The staged handling just makes sure that there is room available, it doesn't guarantee that it is actually used.

E.g. we can end up reserving a fence slot, but then find that we actually don't need it.

Christian.

Quoted 102 lines

Besides, the whole reservation logic still looks a little weired to me ... especially this staged part ...

Thanks

/Monk

-----Original Message-----
From: Christian König [mailto:[email protected]]
Sent: 2018年3月5日 19:22
To: Liu, Monk <[email protected]>; Koenig, Christian <[email protected]>; [email protected]; [email protected]
Subject: Re: [PATCH] dma-buf/reservation: shouldn't kfree staged when slot available

Am 05.03.2018 um 08:55 schrieb Liu, Monk:

Quoted 4 lines

Hi Christian

You are right on that part of obj-staged is set to NULL in
add_fence, So my following question will be why we kfree(obj->staged) in reserve_shared() if staged is always NULL in that point ?

Good question, I haven't wrote code that so I can't fully answer.

Maybe Chris or Maarten know more about that.

Christian.

Quoted 76 lines

Thanks
/Monk

-----Original Message-----
From: Christian König [mailto:[email protected]]
Sent: 2018年2月28日 16:27
To: Liu, Monk <[email protected]>; [email protected]; [email protected]
Subject: Re: [PATCH] dma-buf/reservation: shouldn't kfree staged when slot available

Am 28.02.2018 um 07:44 schrieb Monk Liu:

Quoted 9 lines

under below scenario the obj->fence would refer to a wild pointer:

1,call reservation_object_reserved_shared
2,call reservation_object_add_shared_fence
3,call reservation_object_reserved_shared
4,call reservation_object_add_shared_fence

in step 1, staged is allocated,

Quoted 7 lines

in step 2, code path will go reservation_object_add_shared_replace()
and obj->fence would be assigned as staged (through RCU_INIT_POINTER)

in step 3, obj->staged will be freed(by simple kfree), which make obj->fence point to a wild pointer...

Well that explanation is still nonsense. See
reservation_object_add_shared_fence:

Quoted 1 line
           obj->staged = NULL;

Among the first things reservation_object_add_shared_fence() does is it sets obj->staged to NULL.

So step 3 will not free anything and we never have a wild pointer.

Regards,
Christian.

Quoted 23 lines

in step 4, code path will go reservation_object_add_shared_inplace()
and inside it the @fobj (which equals to @obj->staged, set by above steps) is already a wild pointer

should remov the kfree on staged in
reservation_object_reserve_shared()

Change-Id If7c01f1b4be3d3d8a81efa90216841f79ab1fc1c
---
     drivers/dma-buf/reservation.c | 7 ++-----
     1 file changed, 2 insertions(+), 5 deletions(-)
(quoted diff) +0 -0

b/drivers/dma-buf/reservation.c index 375de41..b473ccc 100644

drivers/dma-buf/reservation.c +0 -1
@@ -74,12 +74,9 @@ int reservation_object_reserve_shared(struct reservation_object *obj)
     	old = reservation_object_get_list(obj);
 
     	if (old && old->shared_max) {
-		if (old->shared_count < old->shared_max) {

drivers/dma-buf/reservation.c int reservation_object_reserve_shared(struct reservation_object *obj) · line 82

Quoted 10 lines
drivers/dma-buf/reservation.c +2 -4
-			/* perform an in-place update */
-			kfree(obj->staged);
-			obj->staged = NULL;
+		if (old->shared_count < old->shared_max)
     			return 0;
-		} else
+		else
     			max = old->shared_max * 2;
     	} else
     		max = 4;

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

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

#9 lore raw

Well, not really.

It's just that reservation_object_reserve_shared() is called multiple times without actually adding fences.

That is a perfectly normal use case, so nothing special here.

Christian.

Am 05.03.2018 um 12:47 schrieb Liu, Monk:

Quoted 148 lines

Can you give more details ? thanks

/Monk

-----Original Message-----
From: Koenig, Christian
Sent: 2018年3月5日 19:39
To: Liu, Monk <[email protected]>; [email protected]; [email protected]
Subject: Re: [PATCH] dma-buf/reservation: shouldn't kfree staged when slot available

Am 05.03.2018 um 12:37 schrieb Liu, Monk:

Quoted 4 lines

But the thing confuse me is according to the design, if driver keep
calling reserve_shared() prior to add_fence(), and with lock held of cause, That BUG() shouldn't hit, so there are two things in face looks weired to me:
1) by design in reserve_shared(), obj->staged should be already NULL,
so why we kfree on it

No, that is not correct.

Quoted 2 lines

2) in fact, amdgpu can hit the case that obj->staged is not NULL in reserved_shared(), don't know how it lead here

We reserved a fence slot without using it, so it is still there when reserve_shared() is called.

Christian.

Quoted 13 lines

Any thought ?

/Monk

-----Original Message-----
From: Koenig, Christian
Sent: 2018年3月5日 19:29
To: Liu, Monk <[email protected]>; [email protected]; [email protected]
Subject: Re: [PATCH] dma-buf/reservation: shouldn't kfree staged when slot available

Quoted 111 lines

Am 05.03.2018 um 12:25 schrieb Liu, Monk:

Quoted 2 lines

And by the way, I add "if (staged!=NULL) BUG();" prior to
"kfree(obj->staged)" in reserve_shared() routine, and this BUG() is actually hit, The stack dump shows it is hit during the vm_bo_update() in gem_va_update()...

That is expected. The staged handling just makes sure that there is room available, it doesn't guarantee that it is actually used.

E.g. we can end up reserving a fence slot, but then find that we actually don't need it.

Christian.

Quoted 102 lines

Besides, the whole reservation logic still looks a little weired to me ... especially this staged part ...

Thanks

/Monk

-----Original Message-----
From: Christian König [mailto:[email protected]]
Sent: 2018年3月5日 19:22
To: Liu, Monk <[email protected]>; Koenig, Christian <[email protected]>; [email protected]; [email protected]
Subject: Re: [PATCH] dma-buf/reservation: shouldn't kfree staged when slot available

Am 05.03.2018 um 08:55 schrieb Liu, Monk:

Quoted 4 lines

Hi Christian

You are right on that part of obj-staged is set to NULL in
add_fence, So my following question will be why we kfree(obj->staged) in reserve_shared() if staged is always NULL in that point ?

Good question, I haven't wrote code that so I can't fully answer.

Maybe Chris or Maarten know more about that.

Christian.

Quoted 76 lines

Thanks
/Monk

-----Original Message-----
From: Christian König [mailto:[email protected]]
Sent: 2018年2月28日 16:27
To: Liu, Monk <[email protected]>; [email protected]; [email protected]
Subject: Re: [PATCH] dma-buf/reservation: shouldn't kfree staged when slot available

Am 28.02.2018 um 07:44 schrieb Monk Liu:

Quoted 9 lines

under below scenario the obj->fence would refer to a wild pointer:

1,call reservation_object_reserved_shared
2,call reservation_object_add_shared_fence
3,call reservation_object_reserved_shared
4,call reservation_object_add_shared_fence

in step 1, staged is allocated,

Quoted 7 lines

in step 2, code path will go reservation_object_add_shared_replace()
and obj->fence would be assigned as staged (through RCU_INIT_POINTER)

in step 3, obj->staged will be freed(by simple kfree), which make obj->fence point to a wild pointer...

Well that explanation is still nonsense. See
reservation_object_add_shared_fence:

Quoted 1 line
            obj->staged = NULL;

Among the first things reservation_object_add_shared_fence() does is it sets obj->staged to NULL.

So step 3 will not free anything and we never have a wild pointer.

Regards,
Christian.

Quoted 23 lines

in step 4, code path will go reservation_object_add_shared_inplace()
and inside it the @fobj (which equals to @obj->staged, set by above steps) is already a wild pointer

should remov the kfree on staged in
reservation_object_reserve_shared()

Change-Id If7c01f1b4be3d3d8a81efa90216841f79ab1fc1c
---
      drivers/dma-buf/reservation.c | 7 ++-----
      1 file changed, 2 insertions(+), 5 deletions(-)
(quoted diff) +0 -0

b/drivers/dma-buf/reservation.c index 375de41..b473ccc 100644

drivers/dma-buf/reservation.c +0 -1
@@ -74,12 +74,9 @@ int reservation_object_reserve_shared(struct reservation_object *obj)
      	old = reservation_object_get_list(obj);
 
      	if (old && old->shared_max) {
-		if (old->shared_count < old->shared_max) {

drivers/dma-buf/reservation.c int reservation_object_reserve_shared(struct reservation_object *obj) · line 82

Quoted 10 lines
drivers/dma-buf/reservation.c +2 -4
-			/* perform an in-place update */
-			kfree(obj->staged);
-			obj->staged = NULL;
+		if (old->shared_count < old->shared_max)
      			return 0;
-		} else
+		else
      			max = old->shared_max * 2;
      	} else
      		max = 4;

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

Quoted 4 lines

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

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