I noticed that the lock/unlock order done by functions SeLockSubjectContext/SeUnlockSubjectContext is incorrect:
SeLockSubjectContext locks in the following order:
SeUnlockSubjectContext unlocks in the following order:
As you can see in SeUnlockSubjectContext there is a problem with lock order, because it should be inversed:
The general rule of thumb to prevent deadlocks is:
1. Use locks in the same order when multiple locks are involved
2. When unlocking unlock them in inverse order, for example
The attached patch fixes the problem. I've tested it in debugger to make sure that the locks execute in the correct order and there are no deadlocks present.