Ways to block IRP on IOCTL call

Topics:
Forums
3
17 Sep, 21
70
3
2

Lets say we are writing Windows Driver where we have two different ioctl calls.
One ioctl (Read) will be called and it will be blocked until global variable have been changed to non zero number.

Second ioctl (write) will get the data from user space and will edit this global variable.

How would I implement this. Currently I do a spinlock on Read IRP dispatch until global variable is non zero

Pseudocode:

global var = 0

dispatch_iotcl_read(Irp){

    while True{

        if(var is not 0){
            break;
        }

        sleep(sometime);

    }
    buf = var;

    completeio(Irp);

}


distpach_ioctl_write(Irp){
    var = irp.user_input;

    completeio(Irp);
}


I would like to not use spinlock and do something else.

 

Also, i would assume global var would need mutex since its been shared by two different ioctl. Or does it? Is it ever possible for ioctl to be called concurrently?

2 Comments

17 Sep, 21

Yes, I think you’ll need a global mutex (KeInitializeMutex) to guard access to the global, and an event (KeInitializeEvent) that’s waited on by readers and signalled after a write to let the readers know data is available.

And yep, it’s possible for an IOCTL to be called concurrently.

Instead of handling the read/writes by IOCTL, have you considered splitting them out into IRP_MJ_READ and IRP_MJ_WRITE?

21 Sep, 21

Hey Jase, 

Sorry for slow response. That was the answer. Using mutex for global var and event instead of spinlock thanks!