Use ZwCreateFile while a mutex is aqcuired

Hi,

Is it valid to use:

ExAqcuireFastMutex( &MyMutex );
ZwCreateFile(…);
ZwReadFile(…);
ZwClose(…);
ExReleaseFastMutex( &MyMutex );

In the above case, the MyMutex is a private mutex…


Bartjan.

Absolutely not. Fast mutexes raise the IRQL to APC_LEVEL. It is invalid to call NT file API’s at APC_LEVEL due to the fact that the IO completion APC cannot run if the FSD pends the request.

-----Original Message-----
From: Bartjan Wattel [mailto:xxxxx@zeelandnet.nl]
Sent: Monday, April 15, 2002 9:54 AM
To: File Systems Developers
Subject: [ntfsd] Use ZwCreateFile while a mutex is aqcuired

Hi,

Is it valid to use:

ExAqcuireFastMutex( &MyMutex );
ZwCreateFile(…);
ZwReadFile(…);
ZwClose(…);
ExReleaseFastMutex( &MyMutex );

In the above case, the MyMutex is a private mutex…


Bartjan.


You are currently subscribed to ntfsd as: xxxxx@inin.com
To unsubscribe send a blank email to %%email.unsub%%

No. You may not call ANY Zw I/O routine at APC_LEVEL. Acquiring a fast
mutex raises to APC level.

You CAN use “unsafe” fast mutexes if you manage the kernel APCs yourself.
Thus the rules are just like ERESOURCE objects - KeEnterCriticalRegion (or
FsRtlEnterFileSystem) then acquire the unsafe fast mutex, then perform the
operation then exit the critical region, then drop the unsafe fast mutex.

But calling an I/O routine at APC_LEVEL will hang if anything in the stack
returns STATUS_PENDING and expects it to work - I/O completion requires an
APC normally and the APC won’t be delivered so long as the thread is at
APC_LEVEL.

Tony

Tony Mason
Consulting Partner
OSR Open Systems Resources, Inc.
http://www.osr.com

Hope to see you at the next OSR file systems class October 7, 2002!

-----Original Message-----
From: Bartjan Wattel [mailto:xxxxx@zeelandnet.nl]
Sent: Monday, April 15, 2002 10:54 AM
To: File Systems Developers
Subject: [ntfsd] Use ZwCreateFile while a mutex is aqcuired

Hi,

Is it valid to use:

ExAqcuireFastMutex( &MyMutex );
ZwCreateFile(…);
ZwReadFile(…);
ZwClose(…);
ExReleaseFastMutex( &MyMutex );

In the above case, the MyMutex is a private mutex…


Bartjan.


You are currently subscribed to ntfsd as: xxxxx@osr.com
To unsubscribe send a blank email to %%email.unsub%%

It doesn’t matter if the mutex is private or not.
The main problem with your code is that the fast mutext
package raises IRQl to APC_LEVEL on acquire.
And calling ZwXxxxFile routines on such an IRQL can
lead to the deadlock if the FSD decides to post the request
to the worker thread. The IoCompleteRequest will then queue
an APC to the original thread, but it will never be delivered,
because the thread is waiting on APC_LEVEL.

So, I think it is not good idea to do it.

Paul

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com]On Behalf Of Bartjan Wattel
Sent: Monday, April 15, 2002 4:54 PM
To: File Systems Developers
Subject: [ntfsd] Use ZwCreateFile while a mutex is aqcuired

Hi,

Is it valid to use:

ExAqcuireFastMutex( &MyMutex );
ZwCreateFile(…);
ZwReadFile(…);
ZwClose(…);
ExReleaseFastMutex( &MyMutex );

In the above case, the MyMutex is a private mutex…


Bartjan.


You are currently subscribed to ntfsd as: xxxxx@compelson.com
To unsubscribe send a blank email to %%email.unsub%%

> Is it valid to use:

ExAqcuireFastMutex( &MyMutex );
ZwCreateFile(…);
ZwReadFile(…);
ZwClose(…);
ExReleaseFastMutex( &MyMutex );

I don’t think you can do Zwxxx from APC_LEVEL, and fast mutexes raise to it.

Max