@@ -113,13 +113,15 @@ typedef struct user_service_struct {
113113 VCHIQ_SERVICE_T * service ;
114114 void * userdata ;
115115 VCHIQ_INSTANCE_T instance ;
116- int is_vchi ;
117- int dequeue_pending ;
116+ char is_vchi ;
117+ char dequeue_pending ;
118+ char close_pending ;
118119 int message_available_pos ;
119120 int msg_insert ;
120121 int msg_remove ;
121122 struct semaphore insert_event ;
122123 struct semaphore remove_event ;
124+ struct semaphore close_event ;
123125 VCHIQ_HEADER_T * msg_queue [MSG_QUEUE_SIZE ];
124126} USER_SERVICE_T ;
125127
@@ -142,6 +144,7 @@ struct vchiq_instance_struct {
142144 int closing ;
143145 int pid ;
144146 int mark ;
147+ int use_close_delivered ;
145148
146149 struct list_head bulk_waiter_list ;
147150 struct mutex bulk_waiter_list_mutex ;
@@ -179,7 +182,9 @@ static const char *const ioctl_names[] = {
179182 "USE_SERVICE" ,
180183 "RELEASE_SERVICE" ,
181184 "SET_SERVICE_OPTION" ,
182- "DUMP_PHYS_MEM"
185+ "DUMP_PHYS_MEM" ,
186+ "LIB_VERSION" ,
187+ "CLOSE_DELIVERED"
183188};
184189
185190vchiq_static_assert ((sizeof (ioctl_names )/sizeof (ioctl_names [0 ])) ==
@@ -231,10 +236,13 @@ add_completion(VCHIQ_INSTANCE_T instance, VCHIQ_REASON_T reason,
231236 completion -> service_userdata = user_service -> service ;
232237 completion -> bulk_userdata = bulk_userdata ;
233238
234- if (reason == VCHIQ_SERVICE_CLOSED )
239+ if (reason == VCHIQ_SERVICE_CLOSED ) {
235240 /* Take an extra reference, to be held until
236241 this CLOSED notification is delivered. */
237242 lock_service (user_service -> service );
243+ if (instance -> use_close_delivered )
244+ user_service -> close_pending = 1 ;
245+ }
238246
239247 /* A write barrier is needed here to ensure that the entire completion
240248 record is written out before the insert point. */
@@ -281,10 +289,10 @@ service_callback(VCHIQ_REASON_T reason, VCHIQ_HEADER_T *header,
281289 return VCHIQ_SUCCESS ;
282290
283291 vchiq_log_trace (vchiq_arm_log_level ,
284- "service_callback - service %lx(%d), reason %d, header %lx, "
292+ "service_callback - service %lx(%d,%p ), reason %d, header %lx, "
285293 "instance %lx, bulk_userdata %lx" ,
286294 (unsigned long )user_service ,
287- service -> localport ,
295+ service -> localport , user_service -> userdata ,
288296 reason , (unsigned long )header ,
289297 (unsigned long )instance , (unsigned long )bulk_userdata );
290298
@@ -371,10 +379,31 @@ user_service_free(void *userdata)
371379
372380/****************************************************************************
373381*
374- * vchiq_ioctl
382+ * close_delivered
375383*
376384***************************************************************************/
385+ static void close_delivered (USER_SERVICE_T * user_service )
386+ {
387+ vchiq_log_info (vchiq_arm_log_level ,
388+ "close_delivered(handle=%x)" ,
389+ user_service -> service -> handle );
390+
391+ WARN_ON (user_service -> close_pending == 0 );
377392
393+ /* Allow the underlying service to be culled */
394+ unlock_service (user_service -> service );
395+
396+ /* Wake the user-thread blocked in close_ or remove_service */
397+ up (& user_service -> close_event );
398+
399+ user_service -> close_pending = 0 ;
400+ }
401+
402+ /****************************************************************************
403+ *
404+ * vchiq_ioctl
405+ *
406+ ***************************************************************************/
378407static long
379408vchiq_ioctl (struct file * file , unsigned int cmd , unsigned long arg )
380409{
@@ -485,14 +514,16 @@ vchiq_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
485514 user_service -> service = service ;
486515 user_service -> userdata = userdata ;
487516 user_service -> instance = instance ;
488- user_service -> is_vchi = args .is_vchi ;
517+ user_service -> is_vchi = ( args .is_vchi != 0 ) ;
489518 user_service -> dequeue_pending = 0 ;
519+ user_service -> close_pending = 0 ;
490520 user_service -> message_available_pos =
491521 instance -> completion_remove - 1 ;
492522 user_service -> msg_insert = 0 ;
493523 user_service -> msg_remove = 0 ;
494524 sema_init (& user_service -> insert_event , 0 );
495525 sema_init (& user_service -> remove_event , 0 );
526+ sema_init (& user_service -> close_event , 0 );
496527
497528 if (args .is_open ) {
498529 status = vchiq_open_service_internal
@@ -526,8 +557,24 @@ vchiq_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
526557 VCHIQ_SERVICE_HANDLE_T handle = (VCHIQ_SERVICE_HANDLE_T )arg ;
527558
528559 service = find_service_for_instance (instance , handle );
529- if (service != NULL )
530- status = vchiq_close_service (service -> handle );
560+ if (service != NULL ) {
561+ USER_SERVICE_T * user_service =
562+ (USER_SERVICE_T * )service -> base .userdata ;
563+ /* close_pending is false on first entry, and when the
564+ wait in vchiq_close_service has been interrupted. */
565+ if (!user_service -> close_pending ) {
566+ status = vchiq_close_service (service -> handle );
567+ if (status != VCHIQ_SUCCESS )
568+ break ;
569+ }
570+
571+ /* close_pending is true once the underlying service
572+ has been closed until the client library calls the
573+ CLOSE_DELIVERED ioctl, signalling close_event. */
574+ if (user_service -> close_pending &&
575+ down_interruptible (& user_service -> close_event ))
576+ status = VCHIQ_RETRY ;
577+ }
531578 else
532579 ret = - EINVAL ;
533580 } break ;
@@ -536,8 +583,24 @@ vchiq_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
536583 VCHIQ_SERVICE_HANDLE_T handle = (VCHIQ_SERVICE_HANDLE_T )arg ;
537584
538585 service = find_service_for_instance (instance , handle );
539- if (service != NULL )
540- status = vchiq_remove_service (service -> handle );
586+ if (service != NULL ) {
587+ USER_SERVICE_T * user_service =
588+ (USER_SERVICE_T * )service -> base .userdata ;
589+ /* close_pending is false on first entry, and when the
590+ wait in vchiq_close_service has been interrupted. */
591+ if (!user_service -> close_pending ) {
592+ status = vchiq_remove_service (service -> handle );
593+ if (status != VCHIQ_SUCCESS )
594+ break ;
595+ }
596+
597+ /* close_pending is true once the underlying service
598+ has been closed until the client library calls the
599+ CLOSE_DELIVERED ioctl, signalling close_event. */
600+ if (user_service -> close_pending &&
601+ down_interruptible (& user_service -> close_event ))
602+ status = VCHIQ_RETRY ;
603+ }
541604 else
542605 ret = - EINVAL ;
543606 } break ;
@@ -804,8 +867,9 @@ vchiq_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
804867 completion -> header = msgbuf ;
805868 }
806869
807- if (completion -> reason ==
808- VCHIQ_SERVICE_CLOSED )
870+ if ((completion -> reason ==
871+ VCHIQ_SERVICE_CLOSED ) &&
872+ !instance -> use_close_delivered )
809873 unlock_service (service );
810874
811875 if (copy_to_user ((void __user * )(
@@ -983,6 +1047,28 @@ vchiq_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
9831047 dump_phys_mem (args .virt_addr , args .num_bytes );
9841048 } break ;
9851049
1050+ case VCHIQ_IOC_LIB_VERSION : {
1051+ unsigned int lib_version = (unsigned int )arg ;
1052+
1053+ if (lib_version < VCHIQ_VERSION_MIN )
1054+ ret = - EINVAL ;
1055+ else if (lib_version >= VCHIQ_VERSION_CLOSE_DELIVERED )
1056+ instance -> use_close_delivered = 1 ;
1057+ } break ;
1058+
1059+ case VCHIQ_IOC_CLOSE_DELIVERED : {
1060+ VCHIQ_SERVICE_HANDLE_T handle = (VCHIQ_SERVICE_HANDLE_T )arg ;
1061+
1062+ service = find_closed_service_for_instance (instance , handle );
1063+ if (service != NULL ) {
1064+ USER_SERVICE_T * user_service =
1065+ (USER_SERVICE_T * )service -> base .userdata ;
1066+ close_delivered (user_service );
1067+ }
1068+ else
1069+ ret = - EINVAL ;
1070+ } break ;
1071+
9861072 default :
9871073 ret = - ENOTTY ;
9881074 break ;
@@ -1170,7 +1256,15 @@ vchiq_release(struct inode *inode, struct file *file)
11701256 (MAX_COMPLETIONS - 1 )];
11711257 service = completion -> service_userdata ;
11721258 if (completion -> reason == VCHIQ_SERVICE_CLOSED )
1259+ {
1260+ USER_SERVICE_T * user_service =
1261+ service -> base .userdata ;
1262+
1263+ /* Wake any blocked user-thread */
1264+ if (instance -> use_close_delivered )
1265+ up (& user_service -> close_event );
11731266 unlock_service (service );
1267+ }
11741268 instance -> completion_remove ++ ;
11751269 }
11761270
0 commit comments