Optimize ksm

Based from link below, would not merge clean
https://github.com/jimsth/vanir_hammerhead/commit/b3a0e90c4830f0f5f3678258c9375b7cc9793032