dma-buf/sync_file: Always increment refcount when merging fences.
Subsystems (2)
PATCH 1/1 dma-buf/sync_file: Always increment refcount when merging fences.
The refcount of a fence should be increased whenever it is added to a merged fence, since it will later be decreased when the merged fence is destroyed. Failing to do so will cause the original fence to be freed if the merged fence gets freed, but other places still referencing won't know about it.
This patch fixes a kernel panic that can be triggered by creating a fence that is expired (or increasing the timeline until it expires), then creating a merged fence out of it, and deleting the merged fence. This will make the original expired fence's refcount go to zero.
---
Sample code to trigger the mentioned kernel panic (might need to be executed a couple times before it actually breaks everything):
static void test_sync_expired_merge(void)
{
int iterations = 1 << 20;
int timeline;
int i;
int fence_expired, fence_merged;
timeline = sw_sync_timeline_create();
sw_sync_timeline_inc(timeline, 100);
fence_expired = sw_sync_fence_create(timeline, 1);
fence_merged = sw_sync_merge(fence_expired, fence_expired);
sw_sync_fence_destroy(fence_merged);
for (i = 0; i < iterations; i++) {
int fence = sw_sync_merge(fence_expired, fence_expired);
igt_assert_f(sw_sync_wait(fence, -1) > 0,
"Failure waiting on fence\n");
sw_sync_fence_destroy(fence);
}
sw_sync_fence_destroy(fence_expired);
}drivers/dma-buf/sync_file.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-)
drivers/dma-buf/sync_file.c +2 -5
@@ -178,11 +178,8 @@ static struct fence **get_fences(struct sync_file *sync_file, int *num_fences)static void add_fence (struct fence **fences,int *i,struct fence *fence) { fences[*i] = fence; - -if (!fence_is_signaled (fence)) { -fence_get (fence); - (*i)++; - } +fence_get (fence); + (*i)++; }/**
Signature · 6 lines
2.7.4 _______________________________________________ dri-devel mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/dri-devel
Hi Rafael,
2016-09-13 Rafael Antognolli <[email protected]>:
Quoted 41 lines
The refcount of a fence should be increased whenever it is added to a merged fence, since it will later be decreased when the merged fence is destroyed. Failing to do so will cause the original fence to be freed if the merged fence gets freed, but other places still referencing won't know about it.
This patch fixes a kernel panic that can be triggered by creating a fence that is expired (or increasing the timeline until it expires), then creating a merged fence out of it, and deleting the merged fence. This will make the original expired fence's refcount go to zero.
Signed-off-by Rafael Antognolli <[email protected]>---
Sample code to trigger the mentioned kernel panic (might need to be executed a couple times before it actually breaks everything):
static void
test_sync_expired_merge(void){ int iterations = 1 << 20; int timeline; int i; int fence_expired, fence_merged; timeline = sw_sync_timeline_create(); sw_sync_timeline_inc(timeline, 100); fence_expired = sw_sync_fence_create(timeline, 1); fence_merged = sw_sync_merge(fence_expired, fence_expired); sw_sync_fence_destroy(fence_merged); for (i = 0; i < iterations; i++) { int fence = sw_sync_merge(fence_expired, fence_expired); igt_assert_f(sw_sync_wait(fence, -1) > 0, "Failure waiting on fence\n"); sw_sync_fence_destroy(fence); } sw_sync_fence_destroy(fence_expired); }
Quoted 2 lines
drivers/dma-buf/sync_file.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-)
Thanks for spotting this.
Gustavo
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
Quoted 50 lines · Rafael Antognolli
The refcount of a fence should be increased whenever it is added to a merged fence, since it will later be decreased when the merged fence is destroyed. Failing to do so will cause the original fence to be freed if the merged fence gets freed, but other places still referencing won't know about it.
This patch fixes a kernel panic that can be triggered by creating a fence that is expired (or increasing the timeline until it expires), then creating a merged fence out of it, and deleting the merged fence. This will make the original expired fence's refcount go to zero.
Signed-off-by Rafael Antognolli <[email protected]>---
Sample code to trigger the mentioned kernel panic (might need to be executed a couple times before it actually breaks everything):
static void
test_sync_expired_merge(void){ int iterations = 1 << 20; int timeline; int i; int fence_expired, fence_merged; timeline = sw_sync_timeline_create(); sw_sync_timeline_inc(timeline, 100); fence_expired = sw_sync_fence_create(timeline, 1); fence_merged = sw_sync_merge(fence_expired, fence_expired); sw_sync_fence_destroy(fence_merged); for (i = 0; i < iterations; i++) { int fence = sw_sync_merge(fence_expired, fence_expired); igt_assert_f(sw_sync_wait(fence, -1) > 0, "Failure waiting on fence\n"); sw_sync_fence_destroy(fence); } sw_sync_fence_destroy(fence_expired); }drivers/dma-buf/sync_file.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-)drivers/dma-buf/sync_file.c +0
-0@@ -178,11 +178,8 @@ static struct fence **get_fences(struct sync_file *sync_file, int *num_fences)static void add_fence (struct fence **fences,int *i,struct fence *fence)
drivers/dma-buf/sync_file.c static struct fence **get_fences(struct sync_file *sync_file, int *num_fences) · line 183
Quoted 10 lines
drivers/dma-buf/sync_file.c +2
-5{ fences[*i] = fence; - -if (!fence_is_signaled (fence)) { -fence_get (fence); - (*i)++; - } +fence_get (fence); + (*i)++; }
I think you'll find it's the caller:
if (i == 0) {
add_fence(fences, &i, a_fences[0]);
i++;
}that does the unexpected.
This should be
if (i == 0)
fences[i++] = fence_get(a_fences[0]);
That ensures the sync_file inherits the signaled status without having to keep all fences.
I think there still seems to be a memory leak when calling sync_file_set_fence() here with i == 1.
-Chris
Signature · 5 lines
Chris Wilson, Intel Open Source Technology Centre _______________________________________________ dri-devel mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/dri-devel
2016-09-14 Chris Wilson <[email protected]>:
Quoted 69 lines · Rafael Antognolli
Quoted 50 lines
The refcount of a fence should be increased whenever it is added to a merged fence, since it will later be decreased when the merged fence is destroyed. Failing to do so will cause the original fence to be freed if the merged fence gets freed, but other places still referencing won't know about it.
This patch fixes a kernel panic that can be triggered by creating a fence that is expired (or increasing the timeline until it expires), then creating a merged fence out of it, and deleting the merged fence. This will make the original expired fence's refcount go to zero.
Signed-off-by Rafael Antognolli <[email protected]>---
Sample code to trigger the mentioned kernel panic (might need to be executed a couple times before it actually breaks everything):
static void
test_sync_expired_merge(void){ int iterations = 1 << 20; int timeline; int i; int fence_expired, fence_merged; timeline = sw_sync_timeline_create(); sw_sync_timeline_inc(timeline, 100); fence_expired = sw_sync_fence_create(timeline, 1); fence_merged = sw_sync_merge(fence_expired, fence_expired); sw_sync_fence_destroy(fence_merged); for (i = 0; i < iterations; i++) { int fence = sw_sync_merge(fence_expired, fence_expired); igt_assert_f(sw_sync_wait(fence, -1) > 0, "Failure waiting on fence\n"); sw_sync_fence_destroy(fence); } sw_sync_fence_destroy(fence_expired); }drivers/dma-buf/sync_file.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-)drivers/dma-buf/sync_file.c +0
-0@@ -178,11 +178,8 @@ static struct fence **get_fences(struct sync_file *sync_file, int *num_fences)static void add_fence (struct fence **fences,int *i,struct fence *fence)drivers/dma-buf/sync_file.c static struct fence **get_fences(struct sync_file *sync_file, int *num_fences) · line 183
Quoted 10 lines
drivers/dma-buf/sync_file.c +2
-5{ fences[*i] = fence; - -if (!fence_is_signaled (fence)) { -fence_get (fence); - (*i)++; - } +fence_get (fence); + (*i)++; }I think you'll find it's the caller:
if (i == 0) { add_fence(fences, &i, a_fences[0]); i++; }
Quoted 6 lines
that does the unexpected.
This should be
if (i == 0)
fences[i++] = fence_get(a_fences[0]);
You are right. Otherwise we would miss the extra reference. i == 0 here means that all fences are signalled.
Quoted 7 lines
That ensures the
sync_fileinherits the signaled status without having to keep all fences.I think there still seems to be a memory leak when calling
sync_file_set_fence()here with i == 1.
I think that is something we discussed already, we don't hold an extra ref there for i == 1 because it would leak the fence.
Gustavo
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
Quoted 73 lines · Gustavo Padovan
2016-09-14 Chris Wilson <[email protected]>:
Quoted 71 lines · Rafael Antognolli
Quoted 50 lines
The refcount of a fence should be increased whenever it is added to a merged fence, since it will later be decreased when the merged fence is destroyed. Failing to do so will cause the original fence to be freed if the merged fence gets freed, but other places still referencing won't know about it.
This patch fixes a kernel panic that can be triggered by creating a fence that is expired (or increasing the timeline until it expires), then creating a merged fence out of it, and deleting the merged fence. This will make the original expired fence's refcount go to zero.
Signed-off-by Rafael Antognolli <[email protected]>---
Sample code to trigger the mentioned kernel panic (might need to be executed a couple times before it actually breaks everything):
static void
test_sync_expired_merge(void){ int iterations = 1 << 20; int timeline; int i; int fence_expired, fence_merged; timeline = sw_sync_timeline_create(); sw_sync_timeline_inc(timeline, 100); fence_expired = sw_sync_fence_create(timeline, 1); fence_merged = sw_sync_merge(fence_expired, fence_expired); sw_sync_fence_destroy(fence_merged); for (i = 0; i < iterations; i++) { int fence = sw_sync_merge(fence_expired, fence_expired); igt_assert_f(sw_sync_wait(fence, -1) > 0, "Failure waiting on fence\n"); sw_sync_fence_destroy(fence); } sw_sync_fence_destroy(fence_expired); }drivers/dma-buf/sync_file.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-)drivers/dma-buf/sync_file.c +0
-0@@ -178,11 +178,8 @@ static struct fence **get_fences(struct sync_file *sync_file, int *num_fences)static void add_fence (struct fence **fences,int *i,struct fence *fence)drivers/dma-buf/sync_file.c static struct fence **get_fences(struct sync_file *sync_file, int *num_fences) · line 183
Quoted 10 lines
drivers/dma-buf/sync_file.c +2
-5{ fences[*i] = fence; - -if (!fence_is_signaled (fence)) { -fence_get (fence); - (*i)++; - } +fence_get (fence); + (*i)++; }I think you'll find it's the caller:
if (i == 0) { add_fence(fences, &i, a_fences[0]); i++; }that does the unexpected.
Quoted 12 lines
Quoted 4 lines
This should be
if (i == 0)
fences[i++] = fence_get(a_fences[0]);You are right. Otherwise we would miss the extra reference. i == 0 here means that all fences are signalled.
Quoted 4 lines
That ensures the
sync_fileinherits the signaled status without having to keep all fences.
OK, so if you are building a merged fence out of several signaled fences, then you only keep one of them instead? I haven't noticed that was the intention.
Quoted 6 lines
Quoted 2 lines
I think there still seems to be a memory leak when calling
sync_file_set_fence()here with i == 1.I think that is something we discussed already, we don't hold an extra ref there for i == 1 because it would leak the fence.
I agree, we already increase the reference count in add_fence anyway (or here, assuming we do what Chris suggested. But I also believe that's the cause of confusion.
IMHO it would be better to call fence_get() inside fence_array_create(), so it would match the fence_put in fence_array_release(). And if we have only one fence, fence_get it inside sync_file_set_fence().
Rafael
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
2016-09-14 Rafael Antognolli <[email protected]>:
Quoted 98 lines · Gustavo Padovan
Quoted 73 lines
2016-09-14 Chris Wilson <[email protected]>:
Quoted 71 lines · Rafael Antognolli
Quoted 50 lines
The refcount of a fence should be increased whenever it is added to a merged fence, since it will later be decreased when the merged fence is destroyed. Failing to do so will cause the original fence to be freed if the merged fence gets freed, but other places still referencing won't know about it.
This patch fixes a kernel panic that can be triggered by creating a fence that is expired (or increasing the timeline until it expires), then creating a merged fence out of it, and deleting the merged fence. This will make the original expired fence's refcount go to zero.
Signed-off-by Rafael Antognolli <[email protected]>---
Sample code to trigger the mentioned kernel panic (might need to be executed a couple times before it actually breaks everything):
static void
test_sync_expired_merge(void){ int iterations = 1 << 20; int timeline; int i; int fence_expired, fence_merged; timeline = sw_sync_timeline_create(); sw_sync_timeline_inc(timeline, 100); fence_expired = sw_sync_fence_create(timeline, 1); fence_merged = sw_sync_merge(fence_expired, fence_expired); sw_sync_fence_destroy(fence_merged); for (i = 0; i < iterations; i++) { int fence = sw_sync_merge(fence_expired, fence_expired); igt_assert_f(sw_sync_wait(fence, -1) > 0, "Failure waiting on fence\n"); sw_sync_fence_destroy(fence); } sw_sync_fence_destroy(fence_expired); }drivers/dma-buf/sync_file.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-)drivers/dma-buf/sync_file.c +0
-0@@ -178,11 +178,8 @@ static struct fence **get_fences(struct sync_file *sync_file, int *num_fences)static void add_fence (struct fence **fences,int *i,struct fence *fence)drivers/dma-buf/sync_file.c static struct fence **get_fences(struct sync_file *sync_file, int *num_fences) · line 183
Quoted 10 lines
drivers/dma-buf/sync_file.c +2
-5{ fences[*i] = fence; - -if (!fence_is_signaled (fence)) { -fence_get (fence); - (*i)++; - } +fence_get (fence); + (*i)++; }I think you'll find it's the caller:
if (i == 0) { add_fence(fences, &i, a_fences[0]); i++; }that does the unexpected.
Quoted 12 lines
Quoted 4 lines
This should be
if (i == 0)
fences[i++] = fence_get(a_fences[0]);You are right. Otherwise we would miss the extra reference. i == 0 here means that all fences are signalled.
Quoted 4 lines
That ensures the
sync_fileinherits the signaled status without having to keep all fences.OK, so if you are building a merged fence out of several signaled fences, then you only keep one of them instead? I haven't noticed that was the intention.
Quoted 6 lines
Quoted 2 lines
I think there still seems to be a memory leak when calling
sync_file_set_fence()here with i == 1.I think that is something we discussed already, we don't hold an extra ref there for i == 1 because it would leak the fence.
Quoted 7 lines
I agree, we already increase the reference count in
add_fenceanyway (or here, assuming we do what Chris suggested. But I also believe that's the cause of confusion.IMHO it would be better to call
fence_get()insidefence_array_create(), so it would match thefence_putinfence_array_release(). And if we have only one fence,fence_getit insidesync_file_set_fence().
sync_file is not the only user of fence_array, so I would not push this behaviour changes to others users. The current behaviour is well documented inside sync_file_set_fence, not sure we need to change it.
Gustavo
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
Quoted 6 lines · Gustavo Padovan
2016-09-14 Chris Wilson <[email protected]>:
Quoted 2 lines
I think there still seems to be a memory leak when calling
sync_file_set_fence()here with i == 1.I think that is something we discussed already, we don't hold an extra ref there for i == 1 because it would leak the fence.
That's fine. I'm worried about the array we kmalloc here but sync_file->fence = fences[0]. Where does the array go?
I *think* we need a if (i == 1) kfree(fences);
-Chris
Signature · 5 lines
Chris Wilson, Intel Open Source Technology Centre _______________________________________________ dri-devel mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/dri-devel