The impotence of evil - SPOILERS
Karl killing Doyle isn't vengeance or even entirely to protect Linda and Frank from harm - it's a sacrifice. He knows someone's going to have to kill Doyle sooner or later and he does it himself to spare others the necessity. When he says goodbye to Vaughn he says that although the bible says two men ought not lie together, he doesn't believe God would send him to Hades for THAT. What he's alluding to is that if he does nothing and it falls to Vaughn to kill Doyle, it would mean Vaughn's soul - and since his own is already lost for killing his mother, he takes it upon himself.
Like everyone else in the story he's spent his lifetime being bullied. He's presented as Christ-like with his piety, his suffering, etc. (his books are the Bible, one on Christmas, ans some carpentry books) and so on. Back at the asylum, there's JT Walsh whispering obscenities to him again, the devil at his ear. Finally he has the confidence to dismiss him and warn him to never speak to him again. And like that the devil is dispelled. Karl's smile at the end is the realization not only that Linda, Vaughn, and Frank will be safe, but that when Edmund Burke said: "The only thing necessary for the triumph of evil is for good men to do nothing.", the corollary is that if good men do take action, evil has no power.