kmod/core: ensure page is RO before changing its permissions

Make sure we leave the page's execution permissions in the same state we found them.

Also do a little bit of code cleanup.