Skip to content

Commit 27836b6

Browse files
dma-buf: remove shared fence staging in reservation object
No need for that any more. Just replace the list when there isn't enough room any more for the additional fence. Signed-off-by: Christian König <christian.koenig@amd.com> Reviewed-by: Junwei Zhang <Jerry.Zhang@amd.com> Reviewed-by: Huang Rui <ray.huang@amd.com> Link: https://patchwork.kernel.org/patch/10626143/
1 parent 9d32bdb commit 27836b6

2 files changed

Lines changed: 58 additions & 124 deletions

File tree

‎drivers/dma-buf/reservation.c‎

Lines changed: 58 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -68,152 +68,69 @@ EXPORT_SYMBOL(reservation_seqcount_string);
6868
*/
6969
int reservation_object_reserve_shared(struct reservation_object *obj)
7070
{
71-
struct reservation_object_list *fobj, *old;
72-
u32 max;
71+
struct reservation_object_list *old, *new;
72+
unsigned int i, j, k, max;
7373

7474
old = reservation_object_get_list(obj);
7575

7676
if (old && old->shared_max) {
77-
if (old->shared_count < old->shared_max) {
78-
/* perform an in-place update */
79-
kfree(obj->staged);
80-
obj->staged = NULL;
77+
if (old->shared_count < old->shared_max)
8178
return 0;
82-
} else
79+
else
8380
max = old->shared_max * 2;
84-
} else
85-
max = 4;
86-
87-
/*
88-
* resize obj->staged or allocate if it doesn't exist,
89-
* noop if already correct size
90-
*/
91-
fobj = krealloc(obj->staged, offsetof(typeof(*fobj), shared[max]),
92-
GFP_KERNEL);
93-
if (!fobj)
94-
return -ENOMEM;
95-
96-
obj->staged = fobj;
97-
fobj->shared_max = max;
98-
return 0;
99-
}
100-
EXPORT_SYMBOL(reservation_object_reserve_shared);
101-
102-
static void
103-
reservation_object_add_shared_inplace(struct reservation_object *obj,
104-
struct reservation_object_list *fobj,
105-
struct dma_fence *fence)
106-
{
107-
struct dma_fence *signaled = NULL;
108-
u32 i, signaled_idx;
109-
110-
dma_fence_get(fence);
111-
112-
preempt_disable();
113-
write_seqcount_begin(&obj->seq);
114-
115-
for (i = 0; i < fobj->shared_count; ++i) {
116-
struct dma_fence *old_fence;
117-
118-
old_fence = rcu_dereference_protected(fobj->shared[i],
119-
reservation_object_held(obj));
120-
121-
if (old_fence->context == fence->context) {
122-
/* memory barrier is added by write_seqcount_begin */
123-
RCU_INIT_POINTER(fobj->shared[i], fence);
124-
write_seqcount_end(&obj->seq);
125-
preempt_enable();
126-
127-
dma_fence_put(old_fence);
128-
return;
129-
}
130-
131-
if (!signaled && dma_fence_is_signaled(old_fence)) {
132-
signaled = old_fence;
133-
signaled_idx = i;
134-
}
135-
}
136-
137-
/*
138-
* memory barrier is added by write_seqcount_begin,
139-
* fobj->shared_count is protected by this lock too
140-
*/
141-
if (signaled) {
142-
RCU_INIT_POINTER(fobj->shared[signaled_idx], fence);
14381
} else {
144-
BUG_ON(fobj->shared_count >= fobj->shared_max);
145-
RCU_INIT_POINTER(fobj->shared[fobj->shared_count], fence);
146-
fobj->shared_count++;
82+
max = 4;
14783
}
14884

149-
write_seqcount_end(&obj->seq);
150-
preempt_enable();
151-
152-
dma_fence_put(signaled);
153-
}
154-
155-
static void
156-
reservation_object_add_shared_replace(struct reservation_object *obj,
157-
struct reservation_object_list *old,
158-
struct reservation_object_list *fobj,
159-
struct dma_fence *fence)
160-
{
161-
unsigned i, j, k;
162-
163-
dma_fence_get(fence);
164-
165-
if (!old) {
166-
RCU_INIT_POINTER(fobj->shared[0], fence);
167-
fobj->shared_count = 1;
168-
goto done;
169-
}
85+
new = kmalloc(offsetof(typeof(*new), shared[max]), GFP_KERNEL);
86+
if (!new)
87+
return -ENOMEM;
17088

17189
/*
17290
* no need to bump fence refcounts, rcu_read access
17391
* requires the use of kref_get_unless_zero, and the
17492
* references from the old struct are carried over to
17593
* the new.
17694
*/
177-
for (i = 0, j = 0, k = fobj->shared_max; i < old->shared_count; ++i) {
178-
struct dma_fence *check;
95+
for (i = 0, j = 0, k = max; i < (old ? old->shared_count : 0); ++i) {
96+
struct dma_fence *fence;
17997

180-
check = rcu_dereference_protected(old->shared[i],
181-
reservation_object_held(obj));
182-
183-
if (check->context == fence->context ||
184-
dma_fence_is_signaled(check))
185-
RCU_INIT_POINTER(fobj->shared[--k], check);
98+
fence = rcu_dereference_protected(old->shared[i],
99+
reservation_object_held(obj));
100+
if (dma_fence_is_signaled(fence))
101+
RCU_INIT_POINTER(new->shared[--k], fence);
186102
else
187-
RCU_INIT_POINTER(fobj->shared[j++], check);
103+
RCU_INIT_POINTER(new->shared[j++], fence);
188104
}
189-
fobj->shared_count = j;
190-
RCU_INIT_POINTER(fobj->shared[fobj->shared_count], fence);
191-
fobj->shared_count++;
105+
new->shared_count = j;
106+
new->shared_max = max;
192107

193-
done:
194108
preempt_disable();
195109
write_seqcount_begin(&obj->seq);
196110
/*
197111
* RCU_INIT_POINTER can be used here,
198112
* seqcount provides the necessary barriers
199113
*/
200-
RCU_INIT_POINTER(obj->fence, fobj);
114+
RCU_INIT_POINTER(obj->fence, new);
201115
write_seqcount_end(&obj->seq);
202116
preempt_enable();
203117

204118
if (!old)
205-
return;
119+
return 0;
206120

207121
/* Drop the references to the signaled fences */
208-
for (i = k; i < fobj->shared_max; ++i) {
209-
struct dma_fence *f;
122+
for (i = k; i < new->shared_max; ++i) {
123+
struct dma_fence *fence;
210124

211-
f = rcu_dereference_protected(fobj->shared[i],
212-
reservation_object_held(obj));
213-
dma_fence_put(f);
125+
fence = rcu_dereference_protected(new->shared[i],
126+
reservation_object_held(obj));
127+
dma_fence_put(fence);
214128
}
215129
kfree_rcu(old, rcu);
130+
131+
return 0;
216132
}
133+
EXPORT_SYMBOL(reservation_object_reserve_shared);
217134

218135
/**
219136
* reservation_object_add_shared_fence - Add a fence to a shared slot
@@ -226,15 +143,39 @@ reservation_object_add_shared_replace(struct reservation_object *obj,
226143
void reservation_object_add_shared_fence(struct reservation_object *obj,
227144
struct dma_fence *fence)
228145
{
229-
struct reservation_object_list *old, *fobj = obj->staged;
146+
struct reservation_object_list *fobj;
147+
unsigned int i;
230148

231-
old = reservation_object_get_list(obj);
232-
obj->staged = NULL;
149+
dma_fence_get(fence);
150+
151+
fobj = reservation_object_get_list(obj);
233152

234-
if (!fobj)
235-
reservation_object_add_shared_inplace(obj, old, fence);
236-
else
237-
reservation_object_add_shared_replace(obj, old, fobj, fence);
153+
preempt_disable();
154+
write_seqcount_begin(&obj->seq);
155+
156+
for (i = 0; i < fobj->shared_count; ++i) {
157+
struct dma_fence *old_fence;
158+
159+
old_fence = rcu_dereference_protected(fobj->shared[i],
160+
reservation_object_held(obj));
161+
if (old_fence->context == fence->context ||
162+
dma_fence_is_signaled(old_fence)) {
163+
dma_fence_put(old_fence);
164+
goto replace;
165+
}
166+
}
167+
168+
BUG_ON(fobj->shared_count >= fobj->shared_max);
169+
fobj->shared_count++;
170+
171+
replace:
172+
/*
173+
* memory barrier is added by write_seqcount_begin,
174+
* fobj->shared_count is protected by this lock too
175+
*/
176+
RCU_INIT_POINTER(fobj->shared[i], fence);
177+
write_seqcount_end(&obj->seq);
178+
preempt_enable();
238179
}
239180
EXPORT_SYMBOL(reservation_object_add_shared_fence);
240181

@@ -343,9 +284,6 @@ int reservation_object_copy_fences(struct reservation_object *dst,
343284
new = dma_fence_get_rcu_safe(&src->fence_excl);
344285
rcu_read_unlock();
345286

346-
kfree(dst->staged);
347-
dst->staged = NULL;
348-
349287
src_list = reservation_object_get_list(dst);
350288
old = reservation_object_get_excl(dst);
351289

‎include/linux/reservation.h‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,13 @@ struct reservation_object_list {
6868
* @seq: sequence count for managing RCU read-side synchronization
6969
* @fence_excl: the exclusive fence, if there is one currently
7070
* @fence: list of current shared fences
71-
* @staged: staged copy of shared fences for RCU updates
7271
*/
7372
struct reservation_object {
7473
struct ww_mutex lock;
7574
seqcount_t seq;
7675

7776
struct dma_fence __rcu *fence_excl;
7877
struct reservation_object_list __rcu *fence;
79-
struct reservation_object_list *staged;
8078
};
8179

8280
#define reservation_object_held(obj) lockdep_is_held(&(obj)->lock.base)
@@ -95,7 +93,6 @@ reservation_object_init(struct reservation_object *obj)
9593
__seqcount_init(&obj->seq, reservation_seqcount_string, &reservation_seqcount_class);
9694
RCU_INIT_POINTER(obj->fence, NULL);
9795
RCU_INIT_POINTER(obj->fence_excl, NULL);
98-
obj->staged = NULL;
9996
}
10097

10198
/**
@@ -124,7 +121,6 @@ reservation_object_fini(struct reservation_object *obj)
124121

125122
kfree(fobj);
126123
}
127-
kfree(obj->staged);
128124

129125
ww_mutex_destroy(&obj->lock);
130126
}

0 commit comments

Comments
 (0)