Skip to content

Commit 3b17e8f

Browse files
committed
usb: hub: Add sychronization for FDO's child list access.
As FDO's child list is getting changed not from PnP dispatcher but from DeviceStatusChangeThread(), we need to sychronize access for it. svn path=/branches/GSoC_2016/USB/; revision=72386
1 parent 4d09920 commit 3b17e8f

File tree

4 files changed

+50
-21
lines changed

4 files changed

+50
-21
lines changed

drivers/usb/usbhub/fdo.c

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,6 +1094,7 @@ DestroyUsbChildDeviceObject(
10941094
PDEVICE_OBJECT ChildDeviceObject = NULL;
10951095
ULONG Index = 0;
10961096

1097+
KeAcquireGuardedMutex(&HubDeviceExtension->HubMutexLock);
10971098
for (Index = 0; Index < USB_MAXCHILDREN; Index++)
10981099
{
10991100
if (HubDeviceExtension->ChildDeviceObject[Index])
@@ -1114,15 +1115,17 @@ DestroyUsbChildDeviceObject(
11141115
if (!ChildDeviceObject)
11151116
{
11161117
DPRINT1("Removal request for non-existant device!\n");
1118+
KeReleaseGuardedMutex(&HubDeviceExtension->HubMutexLock);
11171119
return STATUS_UNSUCCESSFUL;
11181120
}
11191121

11201122
DPRINT("Removing device on port %d (Child index: %d)\n", PortId, Index);
11211123

11221124
/* Remove the device from the table */
1123-
// is lock needed?
11241125
HubDeviceExtension->ChildDeviceObject[Index] = NULL;
11251126

1127+
KeReleaseGuardedMutex(&HubDeviceExtension->HubMutexLock);
1128+
11261129
/* Invalidate device relations for the root hub */
11271130
IoInvalidateDeviceRelations(HubDeviceExtension->RootHubPhysicalDeviceObject, BusRelations);
11281131

@@ -1153,26 +1156,6 @@ CreateUsbChildDeviceObject(
11531156
HubInterface = &HubDeviceExtension->HubInterface;
11541157
RootHubDeviceObject = HubDeviceExtension->RootHubPhysicalDeviceObject;
11551158
HubInterfaceBusContext = HubDeviceExtension->UsbDInterface.BusContext;
1156-
//
1157-
// Find an empty slot in the child device array
1158-
//
1159-
for (ChildDeviceCount = 0; ChildDeviceCount < USB_MAXCHILDREN; ChildDeviceCount++)
1160-
{
1161-
if (HubDeviceExtension->ChildDeviceObject[ChildDeviceCount] == NULL)
1162-
{
1163-
DPRINT("Found unused entry at %d\n", ChildDeviceCount);
1164-
break;
1165-
}
1166-
}
1167-
1168-
//
1169-
// Check if the limit has been reached for maximum usb devices
1170-
//
1171-
if (ChildDeviceCount == USB_MAXCHILDREN)
1172-
{
1173-
DPRINT1("USBHUB: Too many child devices!\n");
1174-
return STATUS_UNSUCCESSFUL;
1175-
}
11761159

11771160
while (TRUE)
11781161
{
@@ -1341,8 +1324,35 @@ CreateUsbChildDeviceObject(
13411324

13421325
UsbChildExtension->IsRemovePending = FALSE;
13431326

1327+
KeAcquireGuardedMutex(&HubDeviceExtension->HubMutexLock);
1328+
1329+
//
1330+
// Find an empty slot in the child device array
1331+
//
1332+
for (ChildDeviceCount = 0; ChildDeviceCount < USB_MAXCHILDREN; ChildDeviceCount++)
1333+
{
1334+
if (HubDeviceExtension->ChildDeviceObject[ChildDeviceCount] == NULL)
1335+
{
1336+
DPRINT("Found unused entry at %d\n", ChildDeviceCount);
1337+
break;
1338+
}
1339+
}
1340+
1341+
//
1342+
// Check if the limit has been reached for maximum usb devices
1343+
//
1344+
if (ChildDeviceCount == USB_MAXCHILDREN)
1345+
{
1346+
DPRINT1("USBHUB: Too many child devices!\n");
1347+
Status = STATUS_UNSUCCESSFUL;
1348+
KeReleaseGuardedMutex(&HubDeviceExtension->HubMutexLock);
1349+
UsbChildExtension->DeviceInterface.InterfaceDereference(UsbChildExtension->DeviceInterface.BusContext);
1350+
goto Cleanup;
1351+
}
1352+
13441353
HubDeviceExtension->ChildDeviceObject[ChildDeviceCount] = NewChildDeviceObject;
13451354
HubDeviceExtension->InstanceCount++;
1355+
KeReleaseGuardedMutex(&HubDeviceExtension->HubMutexLock);
13461356

13471357
IoInvalidateDeviceRelations(RootHubDeviceObject, BusRelations);
13481358
return STATUS_SUCCESS;
@@ -1398,6 +1408,8 @@ USBHUB_FdoQueryBusRelations(
13981408

13991409
HubDeviceExtension = (PHUB_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
14001410

1411+
KeAcquireGuardedMutex(&HubDeviceExtension->HubMutexLock);
1412+
14011413
//
14021414
// Count the number of children
14031415
//
@@ -1418,6 +1430,7 @@ USBHUB_FdoQueryBusRelations(
14181430
{
14191431
// We have nothing to add
14201432
*pDeviceRelations = RelationsFromTop;
1433+
KeReleaseGuardedMutex(&HubDeviceExtension->HubMutexLock);
14211434
return STATUS_SUCCESS;
14221435
}
14231436
}
@@ -1432,6 +1445,7 @@ USBHUB_FdoQueryBusRelations(
14321445

14331446
if (!DeviceRelations)
14341447
{
1448+
KeReleaseGuardedMutex(&HubDeviceExtension->HubMutexLock);
14351449
if (!RelationsFromTop)
14361450
return STATUS_INSUFFICIENT_RESOURCES;
14371451
else
@@ -1461,6 +1475,8 @@ USBHUB_FdoQueryBusRelations(
14611475
}
14621476
}
14631477

1478+
KeReleaseGuardedMutex(&HubDeviceExtension->HubMutexLock);
1479+
14641480
// We should do this, because replaced this with our's one
14651481
if (RelationsFromTop)
14661482
ExFreePool(RelationsFromTop);
@@ -2198,6 +2214,7 @@ USBHUB_FdoHandleDeviceControl(
21982214
// sanity checks
21992215
ASSERT(NodeConnectionInfo);
22002216

2217+
KeAcquireGuardedMutex(&HubDeviceExtension->HubMutexLock);
22012218
for(Index = 0; Index < USB_MAXCHILDREN; Index++)
22022219
{
22032220
if (HubDeviceExtension->ChildDeviceObject[Index] == NULL)
@@ -2224,6 +2241,7 @@ USBHUB_FdoHandleDeviceControl(
22242241
}
22252242
break;
22262243
}
2244+
KeReleaseGuardedMutex(&HubDeviceExtension->HubMutexLock);
22272245
// done
22282246
Irp->IoStatus.Information = sizeof(USB_NODE_INFORMATION);
22292247
Status = STATUS_SUCCESS;
@@ -2244,6 +2262,7 @@ USBHUB_FdoHandleDeviceControl(
22442262
// sanity checks
22452263
ASSERT(NodeKey);
22462264

2265+
KeAcquireGuardedMutex(&HubDeviceExtension->HubMutexLock);
22472266
for(Index = 0; Index < USB_MAXCHILDREN; Index++)
22482267
{
22492268
if (HubDeviceExtension->ChildDeviceObject[Index] == NULL)
@@ -2283,6 +2302,7 @@ USBHUB_FdoHandleDeviceControl(
22832302
NodeKey->ActualLength = Length + sizeof(USB_NODE_CONNECTION_DRIVERKEY_NAME);
22842303
break;
22852304
}
2305+
KeReleaseGuardedMutex(&HubDeviceExtension->HubMutexLock);
22862306
}
22872307
}
22882308
else if (IoStack->Parameters.DeviceIoControl.IoControlCode == IOCTL_USB_GET_NODE_CONNECTION_NAME)

drivers/usb/usbhub/pdo.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,14 +153,18 @@ IsValidPDO(
153153

154154
HubDeviceExtension = (PHUB_DEVICE_EXTENSION)ChildDeviceExtension->ParentDeviceObject->DeviceExtension;
155155

156+
KeAcquireGuardedMutex(&HubDeviceExtension->HubMutexLock);
156157
for(Index = 0; Index < USB_MAXCHILDREN; Index++)
157158
{
158159
if (HubDeviceExtension->ChildDeviceObject[Index] == DeviceObject)
159160
{
161+
KeReleaseGuardedMutex(&HubDeviceExtension->HubMutexLock);
162+
160163
/* PDO exists */
161164
return TRUE;
162165
}
163166
}
167+
KeReleaseGuardedMutex(&HubDeviceExtension->HubMutexLock);
164168

165169
/* invalid pdo */
166170
return FALSE;

drivers/usb/usbhub/usbhub.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ USBHUB_AddDevice(
9292
HubDeviceExtension->Common.IsFDO = TRUE;
9393
DeviceObject->Flags |= DO_POWER_PAGABLE;
9494

95+
// initialize mutex
96+
KeInitializeGuardedMutex(&HubDeviceExtension->HubMutexLock);
97+
9598
//
9699
// initialize reset complete event
97100
//

drivers/usb/usbhub/usbhub.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ typedef struct _HUB_DEVICE_EXTENSION
7373
PDEVICE_OBJECT RootHubPhysicalDeviceObject;
7474
PDEVICE_OBJECT RootHubFunctionalDeviceObject;
7575

76+
KGUARDED_MUTEX HubMutexLock;
77+
7678
ULONG NumberOfHubs;
7779
KEVENT ResetComplete;
7880

0 commit comments

Comments
 (0)