On 21 Nov 2011, at 06:03, sheetal phirke wrote:
> I am writing IOKit driver with Vnode & File listener.
> Is it safe for me to take mutex lock while IOMalloc / IOFree?
I think that would be a very bad idea. Listeners in the vnode scope are potentially on the page out path (because the pager might need to create a new swap file), which makes allocating memory in your code potentially dangerous, regardless of whether you're holding a mutex or not.
Share and Enjoy
--
Quinn "The Eskimo!" <http://www.apple.com/developer/>
Apple Developer Relations, Developer Technical Support, Core OS/Hardware
For the KAuth callback
static int VNodeListener (
kauth_cred_t KAuthCred,
void * pvData,
kauth_action_t KAuthAction,
uintptr_t puiArg0,
uintptr_t puiArg1,
uintptr_t puiArg2,
uintptr_t puiArg3
);
surely this is only true if kauth_cred_getuid (KAuthCred) == 0?
In this case, the KAuth VNode callback has been called for a VFS thread owned by root, because the VNode pager (dynamic_pager) and all asynchronous I/O (AIO) kernel threads run as root. So a simple check at the start of the callback for a root-owned thread can prevent this problem. (The KAuth technical note advises strongly in favour of this anyway to prevent deadlock with other system daemons.) Of course this means that you can't monitor root's I/O events, which may be a problem for some applications, but not if you're only interested in the users' file I/O (except AIO for the reason above...although the documentation says that AIO doesn't cause this problem because it won't actually generate the callbacks in the first place!)
For non-root threads, it should be safe to call IOMalloc()/IOFree() and to acquire a mutex, right because the VNode pager cannot have called KAuth VNode callback? In fact, there should not be any special caveats for these threads, other than remembering that you're in the middle of a VFS thread so you don't want to waste time because you'll slow down I/O.
It would help if the KAuth system were able to provide the PID of the calling thread (or 0 for kernel threads) as well as the UID & GID in the callback's arguments. The KAuth documentation suggests that using proc_self() doesn't help you with AIO threads, which is a real shame. Process-level rather than user/group-level filtering would be much more efficient and useful in some circumstances.
Regards,