MOD_INC_USE_COUNT/MOD_DEC_USE_COUNT include/linux/module.h

These manipulate the module usage count, to protect against removal (a module also can't be removed if another module uses one of its exported symbols: see below). Every reference to the module from user context should be reflected by this counter (e.g. for every data structure or socket) before the function sleeps. To quote Tim Waugh:

/* THIS IS BAD */
foo_open (...)
{
        stuff..
        if (fail)
                return -EBUSY;
        sleep.. (might get unloaded here)
        stuff..
        MOD_INC_USE_COUNT;
        return 0;
}

/* THIS IS GOOD /
foo_open (...)
{
        MOD_INC_USE_COUNT;
        stuff..
        if (fail) {
                MOD_DEC_USE_COUNT;
                return -EBUSY;
        }
        sleep.. (safe now)
        stuff..
        return 0;
}
   

You can often avoid having to deal with these problems by using the owner field of the file_operations structure. Set this field as the macro THIS_MODULE.

For more complicated module unload locking requirements, you can set the can_unload function pointer to your own routine, which should return 0 if the module is unloadable, or -EBUSY otherwise.