If a process holds some spinlocks or semaphores, and exit accidently(e.g., killed by linux), would linux release these locks correctly? If linux doesn't do this work, why?
It depends on the type of lock you're talking about.
If you're talking about any kind of kernel internal lock, they will be released as appropriate (as your system would soon crash otherwise). In general these kind of locks are not owned by the process itself, but rather by some internal kernel workflow, and usually don't remain locked after the process returns to userspace anyway.
Note, however, that if the kernel is already deadlocked when you issue the kill, chances are the process won't be killed. Process killing is performed as part of the signal handling path, which is invoked from the kernel-to-userspace return transition code. If the process is waiting for a kernel spinlock, you'll never make it to the return code and so the process won't exit.
Additionally, if the process is killed because of a kernel OOPS, then all bets are off - the kernel is already in an inconsistent state, and the OOPS exit code doesn't try very hard to clean up any locks the kernel thread may have been holding at the time.
If you're talking about any sort of userspace spinlock or semaphore (including the
If you're talking about the
If you're talking about a process-local
If you're talking about a shared