The kernel does not avoid deadlocks of user-space locks (because often it doesn't even know about them).
Deadlocks of kernel locks are avoided by writing code that is correct. This is greatly helped by lockdep, which can prove the correctness of locking operations.
(The lockdep code has been ported to user space, but it helps only for programs that bother to use it.)