One instance of UMDF device

Hello All,

I have a virtual device developed using UMDF framework. I want to have only one instance of virtual device running at any point of time. Can I use a simple mutex object in Dllmain() to prevent another instance from running or is there any better alternative?

Thanks and Regards,
Madhukar

I’m not sure what you’d do with a Mutex… hold it the entire time?

But, in any case, just create a global value that’s initialized to zero, acquire the appropriate locks check to see if it’s zero… if it is, increment it and drop the lock and continue to load. if it’s not zero, drop the lock and refuse to load.

Simple, no?

Peter
OSR
@OSRDrivers

> But, in any case, just create a global value that’s initialized to zero

Where this global should be placed in user mode?

Creating a named object (event is maybe the simplest) in the Ob’s namespace is one of the simplest ways.


Maxim S. Shatskih
Microsoft MVP on File System And Storage
xxxxx@storagecraft.com
http://www.storagecraft.com

Thanks for the replies.

If I use a named object, then probably I have use it at device creation level and return failure from creating second device.

Is it advisable to take help of registry and maintain the counter for number of device instances.

Regards,
Madhukar

What am I missing? This is a UMDF driver. Its instance is loaded into a single host process. Why is the answer not to simply use a global LONG?

You can do that, but consider the race conditions. You’ll have to lock appropriately.

Mr. Shatskih’s suggestion of creating a named object is a clever one… If the object already exists, you know that you already have an instance of your driver running.

Peter
OSR
@OSRDrivers

And what happens if some other binary squats on that name?

d

Bent from my phone


From: xxxxx@osr.commailto:xxxxx
Sent: ?12/?2/?2014 6:17 AM
To: Windows System Software Devs Interest Listmailto:xxxxx
Subject: RE:[ntdev] One instance of UMDF device



What am I missing? This is a UMDF driver. Its instance is loaded into a single host process. Why is the answer not to simply use a global LONG?



You can do that, but consider the race conditions. You’ll have to lock appropriately.

Mr. Shatskih’s suggestion of creating a named object is a clever one… If the object already exists, you know that you already have an instance of your driver running.

Peter
OSR
@OSRDrivers


NTDEV is sponsored by OSR

Visit the list at: http://www.osronline.com/showlists.cfm?list=ntdev

OSR is HIRING!! See http://www.osr.com/careers

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer</mailto:xxxxx></mailto:xxxxx>

The name can contain a specially invented GUID
“Doron Holan” wrote in message news:xxxxx@ntdev…
And what happens if some other binary squats on that name?

d

Bent from my phone

------------------------------------------------------------------------------
From: xxxxx@osr.com
Sent: ?12/?2/?2014 6:17 AM
To: Windows System Software Devs Interest List
Subject: RE:[ntdev] One instance of UMDF device



What am I missing? This is a UMDF driver. Its instance is loaded into a single host process. Why is the answer not to simply use a global LONG?



You can do that, but consider the race conditions. You’ll have to lock appropriately.

Mr. Shatskih’s suggestion of creating a named object is a clever one… If the object already exists, you know that you already have an instance of your driver running.

Peter
OSR
@OSRDrivers


NTDEV is sponsored by OSR

Visit the list at: http://www.osronline.com/showlists.cfm?list=ntdev

OSR is HIRING!! See http://www.osr.com/careers

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer

xxxxx@hotmail.com wrote:

If I use a named object, then probably I have use it at device creation level and return failure from creating second device.

Is it advisable to take help of registry and maintain the counter for number of device instances.

The problem with the registry is that it persists across boots. That’s
the advantage of a global variable.


Tim Roberts, xxxxx@probo.com
Providenza & Boekelheide, Inc.

> The problem with the registry is that it persists across boots.

HKLM\HARDWARE is IIRC not such

But anyway I need to apologize: I’ve forgot that UMDF hosts are started per driver, not per device.


Maxim S. Shatskih
Microsoft MVP on File System And Storage
xxxxx@storagecraft.com
http://www.storagecraft.com

Put it under a volatile key

-----Original Message-----
From: xxxxx@lists.osr.com [mailto:xxxxx@lists.osr.com] On Behalf Of Tim Roberts
Sent: Tuesday, December 2, 2014 9:40 AM
To: Windows System Software Devs Interest List
Subject: Re: [ntdev] One instance of UMDF device

xxxxx@hotmail.com wrote:

If I use a named object, then probably I have use it at device creation level and return failure from creating second device.

Is it advisable to take help of registry and maintain the counter for number of device instances.

The problem with the registry is that it persists across boots. That’s the advantage of a global variable.


Tim Roberts, xxxxx@probo.com
Providenza & Boekelheide, Inc.


NTDEV is sponsored by OSR

Visit the list at: http://www.osronline.com/showlists.cfm?list=ntdev

OSR is HIRING!! See http://www.osr.com/careers

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer

No - do not store transient information like the number of instances of your device in the registry. Use that for persistent settings that you want to allow the administrator to edit.

What happens when you write the counter, get the hive flushed to disk, and then the system reboots unexpectedly. ON the next boot you’ll fail to load, and you’ll probably never load again unless the user knows what registry information to clean up.

-p
-----Original Message-----
From: xxxxx@lists.osr.com [mailto:xxxxx@lists.osr.com] On Behalf Of xxxxx@hotmail.com
Sent: Tuesday, December 2, 2014 12:45 AM
To: Windows System Software Devs Interest List
Subject: RE:[ntdev] One instance of UMDF device

Thanks for the replies.

If I use a named object, then probably I have use it at device creation level and return failure from creating second device.

Is it advisable to take help of registry and maintain the counter for number of device instances.

Regards,
Madhukar


NTDEV is sponsored by OSR

Visit the list at: http://www.osronline.com/showlists.cfm?list=ntdev

OSR is HIRING!! See http://www.osr.com/careers

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer

Note that global variable wouldn’t work because UMDF driver instances may
live in separate host processes. See the link below.


http://msdn.microsoft.com/en-us/library/windows/hardware/ff540807(v=vs.85).aspx

Note: A UMDF driver runs in a user-mode host process, while a KMDF driver
runs in kernel mode in a system process. The framework might load multiple
instances of a UMDF driver into separate instances of the host process. As a
result:
• The framework might call a UMDF driver’s DriverEntry routine multiple
times if it loads instances of the driver in different host processes. In
contrast, the framework calls a KMDF driver’s DriverEntry routine only once.
• If a UMDF driver creates a global variable in its DriverEntry routine, the
variable might not be available to all instances of the driver. However, a
global variable that a KMDF driver creates in its DriverEntry routine is
available to all instances of the driver.


-kumar

“Maxim S. Shatskih” wrote in message news:xxxxx@ntdev…

The problem with the registry is that it persists across boots.

HKLM\HARDWARE is IIRC not such

But anyway I need to apologize: I’ve forgot that UMDF hosts are started per
driver
, not per device.


Maxim S. Shatskih
Microsoft MVP on File System And Storage
xxxxx@storagecraft.com
http://www.storagecraft.com

Now two separate UMDF instances loaded for the same device, but in different host processes. THAT’s something I hadn’t considered. Thanks Mr. Rajeev.

Peter
OSR
@OSRDrivers

That’s not what he’s saying Peter. Devices with the same driver may run in a shared host, or in separate hosts. You will (today) never end up with multiple drivers for the same device running in different hosts. UMDF doesn’t have the necessary support to shuttle requests between hosts today.

The solution is to create a symbolic link with WdfDeviceCreateSymbolicLink() (in UMDF 2) or IWdfDevice2::CreateSymbolicLinkWithReferenceString with a well known, but unlikely to collide name. I’d suggest putting a GUID (determined at compile time, not runtime) in the name. You can add a reference string if you’d like to specially handle anyone who does attempt to open your device through that symbolic link.

If the link already exists then you know there’s a running instance somewhere and can fail in AddDevice.

The advantage of using a UMDF managed symbolic link is that UMDF will clean this up for you when the device is removed, or when the host process terminates unexpectedly. With registry keys and other more permanent objects you could end up in a weird state where the reference you’ve created persists beyond the life of the host that created it.

-p

-----Original Message-----
From: xxxxx@lists.osr.com [mailto:xxxxx@lists.osr.com] On Behalf Of xxxxx@osr.com
Sent: Wednesday, December 3, 2014 9:05 AM
To: Windows System Software Devs Interest List
Subject: RE:[ntdev] One instance of UMDF device

Now two separate UMDF instances loaded for the same device, but in different host processes. THAT’s something I hadn’t considered. Thanks Mr. Rajeev.

Peter
OSR
@OSRDrivers


NTDEV is sponsored by OSR

Visit the list at: http://www.osronline.com/showlists.cfm?list=ntdev

OSR is HIRING!! See http://www.osr.com/careers

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer

If not then a simple global variable will work and everybody’s good to go, no?

He’s got a driver for a virtual device. It presumably supports one device. If it can never be loaded into separate host processes then “lock, increment, test, unlock” on a global variable (using a global lock) is certainly the easiest solution, no?

I mean… put this in the Driver Context and be done with it.

I feel like I keep missing some supreme subtlety in this thread. 13 replies for a situation that can be solved by incrementing a global variable?

Peter
OSR
@OSRDrivers

The subtlety is driver vs. device.

Two device nodes: a & b. Each load driver D.

D could load into one host and be added to both A & B within that host. Or D could be loaded into two hosts, one for A and one for B.

What will never happen (and this is how I parsed your interpretation) is that D will load in two hosts, both of which will share some control over device A only.

-p

-----Original Message-----
From: xxxxx@lists.osr.com [mailto:xxxxx@lists.osr.com] On Behalf Of xxxxx@osr.com
Sent: Wednesday, December 3, 2014 11:16 AM
To: Windows System Software Devs Interest List
Subject: RE:[ntdev] One instance of UMDF device

If not then a simple global variable will work and everybody’s good to go, no?

He’s got a driver for a virtual device. It presumably supports one device. If it can never be loaded into separate host processes then “lock, increment, test, unlock” on a global variable (using a global lock) is certainly the easiest solution, no?

I mean… put this in the Driver Context and be done with it.

I feel like I keep missing some supreme subtlety in this thread. 13 replies for a situation that can be solved by incrementing a global variable?

Peter
OSR
@OSRDrivers


NTDEV is sponsored by OSR

Visit the list at: http://www.osronline.com/showlists.cfm?list=ntdev

OSR is HIRING!! See http://www.osr.com/careers

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer

Thanks for taking the time to clarify. I certainly appreciate it. I think it’s pretty clear that the driver for a single device instance can’t load into two separate host processes. Sorry if *I* wasn’t clear in my reply to Mr. Rajeev.

But now I *am* starting to wonder. So, in UMDF 2, if I call WdfDriverCreate and specify a context structure… there’s no guarantee that will be global to all running instances of my driver. It *might* be global to some instances and not others. Do I now have the correct?

If that’s the case, that is indeed news to me… and I think a *lot* of people would find that pretty surprising.

Peter
OSR
@OSRDrivers

> But now I *am* starting to wonder. So, in UMDF 2, if I call WdfDriverCreate and specify a context

In other words: is the running WUDFHost instance per-driver? or per-device?


Maxim S. Shatskih
Microsoft MVP on File System And Storage
xxxxx@storagecraft.com
http://www.storagecraft.com

See “Using Device Pooling in UMDF Drivers”, particularly the 2nd section which explains what happens in the pooled and non-pooled cases: http://msdn.microsoft.com/en-us/library/windows/hardware/hh463993(v=vs.85).aspx

So, in UMDF 2, if I call WdfDriverCreate and specify a context structure… there’s no guarantee that will be global to all running instances of my driver. It *might* be global to some instances and not others. Do I now have the correct?

Correct, it is only guaranteed to be global for all devices that use that driver and that are pooled in the same host process. Object/context memory is allocated from the host process heap.

Calling the host per-driver isn’t meaningful since you can have multiple drivers loaded on a device.

Prior to UMDF 1.11 (Windows 8) each device node got its own host. Your driver would regularly be split across hosts. We’ve received few complaints about this from anyone who wasn’t looking at memory usage of UMDF devices.

Starting with Windows 8 we pool devices into a single host by default. This is to reduce footprint as we get more user-mode drivers on smaller systems. Because this means a bad driver can take down more devices, we have a process for isolating a crashing device off into its own host that involves splitting the pool up and then coalescing it again over time (as hosts naturally restart).

When debugging and/or deploying through VS, our default behavior is to run the drivers being deployed in their own host so as to avoid noise, and to keep you from losing stable drivers when your driver under test crashes.

So the common case in Windows 8 is that every instance of your driver will load in a single host. But there are cases where it won’t, so relying on that is poor form.

You can override the default pooling behavior if you must. See http://msdn.microsoft.com/en-us/library/windows/hardware/hh463993(v=vs.85).aspx. This means that you’ll be increasing the memory utilization for the system, which may upset your customers if they’re running on, or selling, low-spec devices.

-p

-----Original Message-----
From: xxxxx@lists.osr.com [mailto:xxxxx@lists.osr.com] On Behalf Of Maxim S. Shatskih
Sent: Wednesday, December 3, 2014 12:01 PM
To: Windows System Software Devs Interest List
Subject: Re:[ntdev] One instance of UMDF device

But now I *am* starting to wonder. So, in UMDF 2, if I call
WdfDriverCreate and specify a context

In other words: is the running WUDFHost instance per-driver? or per-device?


Maxim S. Shatskih
Microsoft MVP on File System And Storage xxxxx@storagecraft.com http://www.storagecraft.com


NTDEV is sponsored by OSR

Visit the list at: http://www.osronline.com/showlists.cfm?list=ntdev

OSR is HIRING!! See http://www.osr.com/careers

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer