2828
2929static const struct file_operations sync_file_fops ;
3030
31- static struct sync_file * sync_file_alloc (int size )
31+ static struct sync_file * sync_file_alloc (void )
3232{
3333 struct sync_file * sync_file ;
3434
35- sync_file = kzalloc (size , GFP_KERNEL );
35+ sync_file = kzalloc (sizeof ( * sync_file ) , GFP_KERNEL );
3636 if (!sync_file )
3737 return NULL ;
3838
@@ -45,6 +45,8 @@ static struct sync_file *sync_file_alloc(int size)
4545
4646 init_waitqueue_head (& sync_file -> wq );
4747
48+ INIT_LIST_HEAD (& sync_file -> cb .node );
49+
4850 return sync_file ;
4951
5052err :
@@ -54,14 +56,11 @@ static struct sync_file *sync_file_alloc(int size)
5456
5557static void fence_check_cb_func (struct fence * f , struct fence_cb * cb )
5658{
57- struct sync_file_cb * check ;
5859 struct sync_file * sync_file ;
5960
60- check = container_of (cb , struct sync_file_cb , cb );
61- sync_file = check -> sync_file ;
61+ sync_file = container_of (cb , struct sync_file , cb );
6262
63- if (atomic_dec_and_test (& sync_file -> status ))
64- wake_up_all (& sync_file -> wq );
63+ wake_up_all (& sync_file -> wq );
6564}
6665
6766/**
@@ -76,22 +75,18 @@ struct sync_file *sync_file_create(struct fence *fence)
7675{
7776 struct sync_file * sync_file ;
7877
79- sync_file = sync_file_alloc (offsetof( struct sync_file , cbs [ 1 ]) );
78+ sync_file = sync_file_alloc ();
8079 if (!sync_file )
8180 return NULL ;
8281
83- sync_file -> num_fences = 1 ;
84- atomic_set ( & sync_file -> status , 1 );
82+ sync_file -> fence = fence ;
83+
8584 snprintf (sync_file -> name , sizeof (sync_file -> name ), "%s-%s%llu-%d" ,
8685 fence -> ops -> get_driver_name (fence ),
8786 fence -> ops -> get_timeline_name (fence ), fence -> context ,
8887 fence -> seqno );
8988
90- sync_file -> cbs [0 ].fence = fence ;
91- sync_file -> cbs [0 ].sync_file = sync_file ;
92- if (fence_add_callback (fence , & sync_file -> cbs [0 ].cb ,
93- fence_check_cb_func ))
94- atomic_dec (& sync_file -> status );
89+ fence_add_callback (fence , & sync_file -> cb , fence_check_cb_func );
9590
9691 return sync_file ;
9792}
@@ -121,14 +116,49 @@ static struct sync_file *sync_file_fdget(int fd)
121116 return NULL ;
122117}
123118
124- static void sync_file_add_pt (struct sync_file * sync_file , int * i ,
125- struct fence * fence )
119+ static int sync_file_set_fence (struct sync_file * sync_file ,
120+ struct fence * * fences , int num_fences )
126121{
127- sync_file -> cbs [* i ].fence = fence ;
128- sync_file -> cbs [* i ].sync_file = sync_file ;
122+ struct fence_array * array ;
123+
124+ /*
125+ * The reference for the fences in the new sync_file and held
126+ * in add_fence() during the merge procedure, so for num_fences == 1
127+ * we already own a new reference to the fence. For num_fence > 1
128+ * we own the reference of the fence_array creation.
129+ */
130+ if (num_fences == 1 ) {
131+ sync_file -> fence = fences [0 ];
132+ } else {
133+ array = fence_array_create (num_fences , fences ,
134+ fence_context_alloc (1 ), 1 , false);
135+ if (!array )
136+ return - ENOMEM ;
137+
138+ sync_file -> fence = & array -> base ;
139+ }
129140
130- if (!fence_add_callback (fence , & sync_file -> cbs [* i ].cb ,
131- fence_check_cb_func )) {
141+ return 0 ;
142+ }
143+
144+ static struct fence * * get_fences (struct sync_file * sync_file , int * num_fences )
145+ {
146+ if (fence_is_array (sync_file -> fence )) {
147+ struct fence_array * array = to_fence_array (sync_file -> fence );
148+
149+ * num_fences = array -> num_fences ;
150+ return array -> fences ;
151+ }
152+
153+ * num_fences = 1 ;
154+ return & sync_file -> fence ;
155+ }
156+
157+ static void add_fence (struct fence * * fences , int * i , struct fence * fence )
158+ {
159+ fences [* i ] = fence ;
160+
161+ if (!fence_is_signaled (fence )) {
132162 fence_get (fence );
133163 (* i )++ ;
134164 }
@@ -147,16 +177,24 @@ static void sync_file_add_pt(struct sync_file *sync_file, int *i,
147177static struct sync_file * sync_file_merge (const char * name , struct sync_file * a ,
148178 struct sync_file * b )
149179{
150- int num_fences = a -> num_fences + b -> num_fences ;
151180 struct sync_file * sync_file ;
152- int i , i_a , i_b ;
153- unsigned long size = offsetof( struct sync_file , cbs [ num_fences ]) ;
181+ struct fence * * fences , * * nfences , * * a_fences , * * b_fences ;
182+ int i , i_a , i_b , num_fences , a_num_fences , b_num_fences ;
154183
155- sync_file = sync_file_alloc (size );
184+ sync_file = sync_file_alloc ();
156185 if (!sync_file )
157186 return NULL ;
158187
159- atomic_set (& sync_file -> status , num_fences );
188+ a_fences = get_fences (a , & a_num_fences );
189+ b_fences = get_fences (b , & b_num_fences );
190+ if (a_num_fences > INT_MAX - b_num_fences )
191+ return NULL ;
192+
193+ num_fences = a_num_fences + b_num_fences ;
194+
195+ fences = kcalloc (num_fences , sizeof (* fences ), GFP_KERNEL );
196+ if (!fences )
197+ goto err ;
160198
161199 /*
162200 * Assume sync_file a and b are both ordered and have no
@@ -165,55 +203,73 @@ static struct sync_file *sync_file_merge(const char *name, struct sync_file *a,
165203 * If a sync_file can only be created with sync_file_merge
166204 * and sync_file_create, this is a reasonable assumption.
167205 */
168- for (i = i_a = i_b = 0 ; i_a < a -> num_fences && i_b < b -> num_fences ; ) {
169- struct fence * pt_a = a -> cbs [i_a ]. fence ;
170- struct fence * pt_b = b -> cbs [i_b ]. fence ;
206+ for (i = i_a = i_b = 0 ; i_a < a_num_fences && i_b < b_num_fences ; ) {
207+ struct fence * pt_a = a_fences [i_a ];
208+ struct fence * pt_b = b_fences [i_b ];
171209
172210 if (pt_a -> context < pt_b -> context ) {
173- sync_file_add_pt ( sync_file , & i , pt_a );
211+ add_fence ( fences , & i , pt_a );
174212
175213 i_a ++ ;
176214 } else if (pt_a -> context > pt_b -> context ) {
177- sync_file_add_pt ( sync_file , & i , pt_b );
215+ add_fence ( fences , & i , pt_b );
178216
179217 i_b ++ ;
180218 } else {
181219 if (pt_a -> seqno - pt_b -> seqno <= INT_MAX )
182- sync_file_add_pt ( sync_file , & i , pt_a );
220+ add_fence ( fences , & i , pt_a );
183221 else
184- sync_file_add_pt ( sync_file , & i , pt_b );
222+ add_fence ( fences , & i , pt_b );
185223
186224 i_a ++ ;
187225 i_b ++ ;
188226 }
189227 }
190228
191- for (; i_a < a -> num_fences ; i_a ++ )
192- sync_file_add_pt (sync_file , & i , a -> cbs [i_a ].fence );
229+ for (; i_a < a_num_fences ; i_a ++ )
230+ add_fence (fences , & i , a_fences [i_a ]);
231+
232+ for (; i_b < b_num_fences ; i_b ++ )
233+ add_fence (fences , & i , b_fences [i_b ]);
234+
235+ if (i == 0 ) {
236+ add_fence (fences , & i , a_fences [0 ]);
237+ i ++ ;
238+ }
193239
194- for (; i_b < b -> num_fences ; i_b ++ )
195- sync_file_add_pt (sync_file , & i , b -> cbs [i_b ].fence );
240+ if (num_fences > i ) {
241+ nfences = krealloc (fences , i * sizeof (* fences ),
242+ GFP_KERNEL );
243+ if (!nfences )
244+ goto err ;
245+
246+ fences = nfences ;
247+ }
248+
249+ if (sync_file_set_fence (sync_file , fences , i ) < 0 ) {
250+ kfree (fences );
251+ goto err ;
252+ }
196253
197- if (num_fences > i )
198- atomic_sub (num_fences - i , & sync_file -> status );
199- sync_file -> num_fences = i ;
254+ fence_add_callback (sync_file -> fence , & sync_file -> cb ,
255+ fence_check_cb_func );
200256
201257 strlcpy (sync_file -> name , name , sizeof (sync_file -> name ));
202258 return sync_file ;
259+
260+ err :
261+ fput (sync_file -> file );
262+ return NULL ;
263+
203264}
204265
205266static void sync_file_free (struct kref * kref )
206267{
207268 struct sync_file * sync_file = container_of (kref , struct sync_file ,
208269 kref );
209- int i ;
210-
211- for (i = 0 ; i < sync_file -> num_fences ; ++ i ) {
212- fence_remove_callback (sync_file -> cbs [i ].fence ,
213- & sync_file -> cbs [i ].cb );
214- fence_put (sync_file -> cbs [i ].fence );
215- }
216270
271+ fence_remove_callback (sync_file -> fence , & sync_file -> cb );
272+ fence_put (sync_file -> fence );
217273 kfree (sync_file );
218274}
219275
@@ -232,9 +288,9 @@ static unsigned int sync_file_poll(struct file *file, poll_table *wait)
232288
233289 poll_wait (file , & sync_file -> wq , wait );
234290
235- status = atomic_read ( & sync_file -> status );
291+ status = fence_is_signaled ( sync_file -> fence );
236292
237- if (! status )
293+ if (status )
238294 return POLLIN ;
239295 if (status < 0 )
240296 return POLLERR ;
@@ -315,15 +371,18 @@ static long sync_file_ioctl_fence_info(struct sync_file *sync_file,
315371{
316372 struct sync_file_info info ;
317373 struct sync_fence_info * fence_info = NULL ;
374+ struct fence * * fences ;
318375 __u32 size ;
319- int ret , i ;
376+ int num_fences , ret , i ;
320377
321378 if (copy_from_user (& info , (void __user * )arg , sizeof (info )))
322379 return - EFAULT ;
323380
324381 if (info .flags || info .pad )
325382 return - EINVAL ;
326383
384+ fences = get_fences (sync_file , & num_fences );
385+
327386 /*
328387 * Passing num_fences = 0 means that userspace doesn't want to
329388 * retrieve any sync_fence_info. If num_fences = 0 we skip filling
@@ -333,16 +392,16 @@ static long sync_file_ioctl_fence_info(struct sync_file *sync_file,
333392 if (!info .num_fences )
334393 goto no_fences ;
335394
336- if (info .num_fences < sync_file -> num_fences )
395+ if (info .num_fences < num_fences )
337396 return - EINVAL ;
338397
339- size = sync_file -> num_fences * sizeof (* fence_info );
398+ size = num_fences * sizeof (* fence_info );
340399 fence_info = kzalloc (size , GFP_KERNEL );
341400 if (!fence_info )
342401 return - ENOMEM ;
343402
344- for (i = 0 ; i < sync_file -> num_fences ; ++ i )
345- sync_fill_fence_info (sync_file -> cbs [i ]. fence , & fence_info [i ]);
403+ for (i = 0 ; i < num_fences ; i ++ )
404+ sync_fill_fence_info (fences [i ], & fence_info [i ]);
346405
347406 if (copy_to_user (u64_to_user_ptr (info .sync_fence_info ), fence_info ,
348407 size )) {
@@ -352,11 +411,8 @@ static long sync_file_ioctl_fence_info(struct sync_file *sync_file,
352411
353412no_fences :
354413 strlcpy (info .name , sync_file -> name , sizeof (info .name ));
355- info .status = atomic_read (& sync_file -> status );
356- if (info .status >= 0 )
357- info .status = !info .status ;
358-
359- info .num_fences = sync_file -> num_fences ;
414+ info .status = fence_is_signaled (sync_file -> fence );
415+ info .num_fences = num_fences ;
360416
361417 if (copy_to_user ((void __user * )arg , & info , sizeof (info )))
362418 ret = - EFAULT ;
0 commit comments