Skip to content

Commit 1a04080

Browse files
Matthew Hailspopcornmix
authored andcommitted
VCHIQ: Fix mem leak of USER_SERVICE_T objects.
The userdata for VCHIQ services created through the ioctl API is a kmalloced structure. These objects were getting leaked, most notably in vchiq_release(), where the service could be closed, freed and removed from the service list before the wait-to-die loop was entered. This change adds a userdata termination callback, and implements it in the case where USER_SERVICE_T is used for the service userdata.
1 parent fa56067 commit 1a04080

4 files changed

Lines changed: 26 additions & 12 deletions

File tree

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

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,17 @@ service_callback(VCHIQ_REASON_T reason, VCHIQ_HEADER_T *header,
357357
bulk_userdata);
358358
}
359359

360+
/****************************************************************************
361+
*
362+
* user_service_free
363+
*
364+
***************************************************************************/
365+
static void
366+
user_service_free(void *userdata)
367+
{
368+
kfree(userdata);
369+
}
370+
360371
/****************************************************************************
361372
*
362373
* vchiq_ioctl
@@ -467,7 +478,7 @@ vchiq_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
467478
service = vchiq_add_service_internal(
468479
instance->state,
469480
&args.params, srvstate,
470-
instance);
481+
instance, user_service_free);
471482

472483
if (service != NULL) {
473484
user_service->service = service;
@@ -490,8 +501,6 @@ vchiq_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
490501
service = NULL;
491502
ret = (status == VCHIQ_RETRY) ?
492503
-EINTR : -EIO;
493-
user_service->service = NULL;
494-
user_service->instance = NULL;
495504
break;
496505
}
497506
}
@@ -503,7 +512,6 @@ vchiq_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
503512
sizeof(service->handle)) != 0) {
504513
ret = -EFAULT;
505514
vchiq_remove_service(service->handle);
506-
kfree(user_service);
507515
}
508516

509517
service = NULL;
@@ -796,10 +804,8 @@ vchiq_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
796804
}
797805

798806
if (completion->reason ==
799-
VCHIQ_SERVICE_CLOSED) {
807+
VCHIQ_SERVICE_CLOSED)
800808
unlock_service(service);
801-
kfree(user_service);
802-
}
803809

804810
if (copy_to_user((void __user *)(
805811
(size_t)args.buf +
@@ -1151,7 +1157,6 @@ vchiq_release(struct inode *inode, struct file *file)
11511157
spin_unlock(&msg_queue_spinlock);
11521158

11531159
unlock_service(service);
1154-
kfree(user_service);
11551160
}
11561161

11571162
/* Release any closed services */

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,9 @@ unlock_service(VCHIQ_SERVICE_T *service)
273273
}
274274
spin_unlock(&service_spinlock);
275275

276+
if (service && service->userdata_term)
277+
service->userdata_term(service->base.userdata);
278+
276279
kfree(service);
277280
}
278281

@@ -2476,7 +2479,7 @@ vchiq_init_state(VCHIQ_STATE_T *state, VCHIQ_SLOT_ZERO_T *slot_zero,
24762479
VCHIQ_SERVICE_T *
24772480
vchiq_add_service_internal(VCHIQ_STATE_T *state,
24782481
const VCHIQ_SERVICE_PARAMS_T *params, int srvstate,
2479-
VCHIQ_INSTANCE_T instance)
2482+
VCHIQ_INSTANCE_T instance, VCHIQ_USERDATA_TERM_T userdata_term)
24802483
{
24812484
VCHIQ_SERVICE_T *service;
24822485

@@ -2488,6 +2491,7 @@ vchiq_add_service_internal(VCHIQ_STATE_T *state,
24882491
service->handle = VCHIQ_SERVICE_HANDLE_INVALID;
24892492
service->ref_count = 1;
24902493
service->srvstate = VCHIQ_SRVSTATE_FREE;
2494+
service->userdata_term = userdata_term;
24912495
service->localport = VCHIQ_PORT_FREE;
24922496
service->remoteport = VCHIQ_PORT_FREE;
24932497

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,8 @@ typedef enum {
237237
VCHIQ_BULK_RECEIVE
238238
} VCHIQ_BULK_DIR_T;
239239

240+
typedef void (*VCHIQ_USERDATA_TERM_T)(void *userdata);
241+
240242
typedef struct vchiq_bulk_struct {
241243
short mode;
242244
short dir;
@@ -284,6 +286,7 @@ typedef struct vchiq_service_struct {
284286
VCHIQ_SERVICE_HANDLE_T handle;
285287
unsigned int ref_count;
286288
int srvstate;
289+
VCHIQ_USERDATA_TERM_T userdata_term;
287290
unsigned int localport;
288291
unsigned int remoteport;
289292
int public_fourcc;
@@ -534,7 +537,7 @@ vchiq_connect_internal(VCHIQ_STATE_T *state, VCHIQ_INSTANCE_T instance);
534537
extern VCHIQ_SERVICE_T *
535538
vchiq_add_service_internal(VCHIQ_STATE_T *state,
536539
const VCHIQ_SERVICE_PARAMS_T *params, int srvstate,
537-
VCHIQ_INSTANCE_T instance);
540+
VCHIQ_INSTANCE_T instance, VCHIQ_USERDATA_TERM_T userdata_term);
538541

539542
extern VCHIQ_STATUS_T
540543
vchiq_open_service_internal(VCHIQ_SERVICE_T *service, int client_id);

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,8 @@ VCHIQ_STATUS_T vchiq_add_service(
241241
state,
242242
params,
243243
srvstate,
244-
instance);
244+
instance,
245+
NULL);
245246

246247
if (service) {
247248
*phandle = service->handle;
@@ -282,7 +283,8 @@ VCHIQ_STATUS_T vchiq_open_service(
282283
service = vchiq_add_service_internal(state,
283284
params,
284285
VCHIQ_SRVSTATE_OPENING,
285-
instance);
286+
instance,
287+
NULL);
286288

287289
if (service) {
288290
status = vchiq_open_service_internal(service, current->pid);

0 commit comments

Comments
 (0)