Skip to content

Commit 9a6e8c7

Browse files
committed
Merge tag 'drm-fixes-2024-12-07' of https://gitlab.freedesktop.org/drm/kernel
Pull drm fixes from Dave Airlie: "Pretty quiet week which is probably expected after US holidays, the dma-fence and displayport MST message handling fixes make up the bulk of this, along with a couple of minor xe and other driver fixes. dma-fence: - Fix reference leak on fence-merge failure path - Simplify fence merging with kernel's sort() - Fix dma_fence_array_signaled() to ensure forward progress dp_mst: - Fix MST sideband message body length check - Fix a bunch of locking/state handling with DP MST msgs sti: - Add __iomem for mixer_dbg_mxn()'s parameter xe: - Missing init value and 64-bit write-order check - Fix a memory allocation issue causing lockdep violation v3d: - Performance counter fix" * tag 'drm-fixes-2024-12-07' of https://gitlab.freedesktop.org/drm/kernel: drm/v3d: Enable Performance Counters before clearing them drm/dp_mst: Use reset_msg_rx_state() instead of open coding it drm/dp_mst: Reset message rx state after OOM in drm_dp_mst_handle_up_req() drm/dp_mst: Ensure mst_primary pointer is valid in drm_dp_mst_handle_up_req() drm/dp_mst: Fix down request message timeout handling drm/dp_mst: Simplify error path in drm_dp_mst_handle_down_rep() drm/dp_mst: Verify request type in the corresponding down message reply drm/dp_mst: Fix resetting msg rx state after topology removal drm/xe: Move the coredump registration to the worker thread drm/xe/guc: Fix missing init value and add register order check drm/sti: Add __iomem for mixer_dbg_mxn's parameter drm/dp_mst: Fix MST sideband message body length check dma-buf: fix dma_fence_array_signaled v4 dma-fence: Use kernel's sort for merging fences dma-fence: Fix reference leak on fence merge failure path
2 parents 2b90dcd + 471f3a2 commit 9a6e8c7

File tree

8 files changed

+285
-137
lines changed

8 files changed

+285
-137
lines changed

drivers/dma-buf/dma-fence-array.c

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,36 @@ static bool dma_fence_array_enable_signaling(struct dma_fence *fence)
103103
static bool dma_fence_array_signaled(struct dma_fence *fence)
104104
{
105105
struct dma_fence_array *array = to_dma_fence_array(fence);
106+
int num_pending;
107+
unsigned int i;
106108

107-
if (atomic_read(&array->num_pending) > 0)
109+
/*
110+
* We need to read num_pending before checking the enable_signal bit
111+
* to avoid racing with the enable_signaling() implementation, which
112+
* might decrement the counter, and cause a partial check.
113+
* atomic_read_acquire() pairs with atomic_dec_and_test() in
114+
* dma_fence_array_enable_signaling()
115+
*
116+
* The !--num_pending check is here to account for the any_signaled case
117+
* if we race with enable_signaling(), that means the !num_pending check
118+
* in the is_signalling_enabled branch might be outdated (num_pending
119+
* might have been decremented), but that's fine. The user will get the
120+
* right value when testing again later.
121+
*/
122+
num_pending = atomic_read_acquire(&array->num_pending);
123+
if (test_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT, &array->base.flags)) {
124+
if (num_pending <= 0)
125+
goto signal;
108126
return false;
127+
}
128+
129+
for (i = 0; i < array->num_fences; ++i) {
130+
if (dma_fence_is_signaled(array->fences[i]) && !--num_pending)
131+
goto signal;
132+
}
133+
return false;
109134

135+
signal:
110136
dma_fence_array_clear_pending_error(array);
111137
return true;
112138
}

drivers/dma-buf/dma-fence-unwrap.c

Lines changed: 61 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <linux/dma-fence-chain.h>
1313
#include <linux/dma-fence-unwrap.h>
1414
#include <linux/slab.h>
15+
#include <linux/sort.h>
1516

1617
/* Internal helper to start new array iteration, don't use directly */
1718
static struct dma_fence *
@@ -59,6 +60,25 @@ struct dma_fence *dma_fence_unwrap_next(struct dma_fence_unwrap *cursor)
5960
}
6061
EXPORT_SYMBOL_GPL(dma_fence_unwrap_next);
6162

63+
64+
static int fence_cmp(const void *_a, const void *_b)
65+
{
66+
struct dma_fence *a = *(struct dma_fence **)_a;
67+
struct dma_fence *b = *(struct dma_fence **)_b;
68+
69+
if (a->context < b->context)
70+
return -1;
71+
else if (a->context > b->context)
72+
return 1;
73+
74+
if (dma_fence_is_later(b, a))
75+
return 1;
76+
else if (dma_fence_is_later(a, b))
77+
return -1;
78+
79+
return 0;
80+
}
81+
6282
/* Implementation for the dma_fence_merge() marco, don't use directly */
6383
struct dma_fence *__dma_fence_unwrap_merge(unsigned int num_fences,
6484
struct dma_fence **fences,
@@ -67,8 +87,7 @@ struct dma_fence *__dma_fence_unwrap_merge(unsigned int num_fences,
6787
struct dma_fence_array *result;
6888
struct dma_fence *tmp, **array;
6989
ktime_t timestamp;
70-
unsigned int i;
71-
size_t count;
90+
int i, j, count;
7291

7392
count = 0;
7493
timestamp = ns_to_ktime(0);
@@ -96,78 +115,55 @@ struct dma_fence *__dma_fence_unwrap_merge(unsigned int num_fences,
96115
if (!array)
97116
return NULL;
98117

99-
/*
100-
* This trashes the input fence array and uses it as position for the
101-
* following merge loop. This works because the dma_fence_merge()
102-
* wrapper macro is creating this temporary array on the stack together
103-
* with the iterators.
104-
*/
105-
for (i = 0; i < num_fences; ++i)
106-
fences[i] = dma_fence_unwrap_first(fences[i], &iter[i]);
107-
108118
count = 0;
109-
do {
110-
unsigned int sel;
111-
112-
restart:
113-
tmp = NULL;
114-
for (i = 0; i < num_fences; ++i) {
115-
struct dma_fence *next;
116-
117-
while (fences[i] && dma_fence_is_signaled(fences[i]))
118-
fences[i] = dma_fence_unwrap_next(&iter[i]);
119-
120-
next = fences[i];
121-
if (!next)
122-
continue;
123-
124-
/*
125-
* We can't guarantee that inpute fences are ordered by
126-
* context, but it is still quite likely when this
127-
* function is used multiple times. So attempt to order
128-
* the fences by context as we pass over them and merge
129-
* fences with the same context.
130-
*/
131-
if (!tmp || tmp->context > next->context) {
132-
tmp = next;
133-
sel = i;
134-
135-
} else if (tmp->context < next->context) {
136-
continue;
137-
138-
} else if (dma_fence_is_later(tmp, next)) {
139-
fences[i] = dma_fence_unwrap_next(&iter[i]);
140-
goto restart;
119+
for (i = 0; i < num_fences; ++i) {
120+
dma_fence_unwrap_for_each(tmp, &iter[i], fences[i]) {
121+
if (!dma_fence_is_signaled(tmp)) {
122+
array[count++] = dma_fence_get(tmp);
141123
} else {
142-
fences[sel] = dma_fence_unwrap_next(&iter[sel]);
143-
goto restart;
124+
ktime_t t = dma_fence_timestamp(tmp);
125+
126+
if (ktime_after(t, timestamp))
127+
timestamp = t;
144128
}
145129
}
130+
}
146131

147-
if (tmp) {
148-
array[count++] = dma_fence_get(tmp);
149-
fences[sel] = dma_fence_unwrap_next(&iter[sel]);
150-
}
151-
} while (tmp);
132+
if (count == 0 || count == 1)
133+
goto return_fastpath;
152134

153-
if (count == 0) {
154-
tmp = dma_fence_allocate_private_stub(ktime_get());
155-
goto return_tmp;
156-
}
135+
sort(array, count, sizeof(*array), fence_cmp, NULL);
157136

158-
if (count == 1) {
159-
tmp = array[0];
160-
goto return_tmp;
137+
/*
138+
* Only keep the most recent fence for each context.
139+
*/
140+
j = 0;
141+
for (i = 1; i < count; i++) {
142+
if (array[i]->context == array[j]->context)
143+
dma_fence_put(array[i]);
144+
else
145+
array[++j] = array[i];
161146
}
162-
163-
result = dma_fence_array_create(count, array,
164-
dma_fence_context_alloc(1),
165-
1, false);
166-
if (!result) {
167-
tmp = NULL;
168-
goto return_tmp;
147+
count = ++j;
148+
149+
if (count > 1) {
150+
result = dma_fence_array_create(count, array,
151+
dma_fence_context_alloc(1),
152+
1, false);
153+
if (!result) {
154+
for (i = 0; i < count; i++)
155+
dma_fence_put(array[i]);
156+
tmp = NULL;
157+
goto return_tmp;
158+
}
159+
return &result->base;
169160
}
170-
return &result->base;
161+
162+
return_fastpath:
163+
if (count == 0)
164+
tmp = dma_fence_allocate_private_stub(timestamp);
165+
else
166+
tmp = array[0];
171167

172168
return_tmp:
173169
kfree(array);

0 commit comments

Comments
 (0)