Skip to content

Commit 18a8f0a

Browse files
Phil Elwellpopcornmix
authored andcommitted
VCHIQ: Make service closure fully synchronous (drv)
This is one half of a two-part patch, the other half of which is to the vchiq_lib user library. With these patches, calls to vchiq_close_service and vchiq_remove_service won't return until any associated callbacks have been delivered to the callback thread.
1 parent 388939b commit 18a8f0a

5 files changed

Lines changed: 149 additions & 17 deletions

File tree

‎drivers/misc/vc04_services/interface/vchiq_arm/vchiq_arm.c‎

Lines changed: 108 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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

185190
vchiq_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+
***************************************************************************/
378407
static long
379408
vchiq_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

‎drivers/misc/vc04_services/interface/vchiq_arm/vchiq_cfg.h‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright (c) 2010-2012 Broadcom. All rights reserved.
2+
* Copyright (c) 2010-2014 Broadcom. All rights reserved.
33
*
44
* Redistribution and use in source and binary forms, with or without
55
* modification, are permitted provided that the following conditions
@@ -36,11 +36,17 @@
3636

3737
#define VCHIQ_MAGIC VCHIQ_MAKE_FOURCC('V', 'C', 'H', 'I')
3838
/* The version of VCHIQ - change with any non-trivial change */
39-
#define VCHIQ_VERSION 6
39+
#define VCHIQ_VERSION 7
4040
/* The minimum compatible version - update to match VCHIQ_VERSION with any
4141
** incompatible change */
4242
#define VCHIQ_VERSION_MIN 3
4343

44+
/* The version that introduced the VCHIQ_IOC_LIB_VERSION ioctl */
45+
#define VCHIQ_VERSION_LIB_VERSION 7
46+
47+
/* The version that introduced the VCHIQ_IOC_CLOSE_DELIVERED ioctl */
48+
#define VCHIQ_VERSION_CLOSE_DELIVERED 7
49+
4450
#define VCHIQ_MAX_STATES 1
4551
#define VCHIQ_MAX_SERVICES 4096
4652
#define VCHIQ_MAX_SLOTS 128

‎drivers/misc/vc04_services/interface/vchiq_arm/vchiq_core.c‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,31 @@ find_service_for_instance(VCHIQ_INSTANCE_T instance,
223223
return service;
224224
}
225225

226+
VCHIQ_SERVICE_T *
227+
find_closed_service_for_instance(VCHIQ_INSTANCE_T instance,
228+
VCHIQ_SERVICE_HANDLE_T handle) {
229+
VCHIQ_SERVICE_T *service;
230+
231+
spin_lock(&service_spinlock);
232+
service = handle_to_service(handle);
233+
if (service &&
234+
((service->srvstate == VCHIQ_SRVSTATE_FREE) ||
235+
(service->srvstate == VCHIQ_SRVSTATE_CLOSED)) &&
236+
(service->handle == handle) &&
237+
(service->instance == instance)) {
238+
BUG_ON(service->ref_count == 0);
239+
service->ref_count++;
240+
} else
241+
service = NULL;
242+
spin_unlock(&service_spinlock);
243+
244+
if (!service)
245+
vchiq_log_info(vchiq_core_log_level,
246+
"Invalid service handle 0x%x", handle);
247+
248+
return service;
249+
}
250+
226251
VCHIQ_SERVICE_T *
227252
next_service_by_instance(VCHIQ_STATE_T *state, VCHIQ_INSTANCE_T instance,
228253
int *pidx)
@@ -2632,6 +2657,7 @@ vchiq_open_service_internal(VCHIQ_SERVICE_T *service, int client_id)
26322657
VCHIQ_MAKE_MSG(VCHIQ_MSG_OPEN, service->localport, 0),
26332658
&body, 1, sizeof(payload), 1);
26342659
if (status == VCHIQ_SUCCESS) {
2660+
/* Wait for the ACK/NAK */
26352661
if (down_interruptible(&service->remove_event) != 0) {
26362662
status = VCHIQ_RETRY;
26372663
vchiq_release_service_internal(service);

‎drivers/misc/vc04_services/interface/vchiq_arm/vchiq_core.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,10 @@ extern VCHIQ_SERVICE_T *
604604
find_service_for_instance(VCHIQ_INSTANCE_T instance,
605605
VCHIQ_SERVICE_HANDLE_T handle);
606606

607+
extern VCHIQ_SERVICE_T *
608+
find_closed_service_for_instance(VCHIQ_INSTANCE_T instance,
609+
VCHIQ_SERVICE_HANDLE_T handle);
610+
607611
extern VCHIQ_SERVICE_T *
608612
next_service_by_instance(VCHIQ_STATE_T *state, VCHIQ_INSTANCE_T instance,
609613
int *pidx);

‎drivers/misc/vc04_services/interface/vchiq_arm/vchiq_ioctl.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ typedef struct {
124124
_IOW(VCHIQ_IOC_MAGIC, 14, VCHIQ_SET_SERVICE_OPTION_T)
125125
#define VCHIQ_IOC_DUMP_PHYS_MEM \
126126
_IOW(VCHIQ_IOC_MAGIC, 15, VCHIQ_DUMP_MEM_T)
127-
#define VCHIQ_IOC_MAX 15
127+
#define VCHIQ_IOC_LIB_VERSION _IO(VCHIQ_IOC_MAGIC, 16)
128+
#define VCHIQ_IOC_CLOSE_DELIVERED _IO(VCHIQ_IOC_MAGIC, 17)
129+
#define VCHIQ_IOC_MAX 17
128130

129131
#endif

0 commit comments

Comments
 (0)