There's quite a few Keith Richards riffs that require barring most or all strings on a fret while doing a two-finger hammer on. I can do these, but can only play the some of the songs two or three times before my fingers just can't do it anymore. Sometimes the hammer-on fingers just get too cramped to move halfway to the strings.

Is there any exercise that would help, or is just continuing to play them the best exercise?

I was watching Richards in a Youtube video last night, and his fingers look almost relaxed. Asshole.
try playing with thumb over the neck like hendrix, or john frusciante, that's how i play those little melodies while playing chords
F*ck Chuck Norris ... SpongeBob grills under water
I've tried the "baseball bat" grip, but it's more difficult for me than having my thumb on the back of the neck, opposite the barring (index) finger.
Try shooting up some heroine first..
Part of the reason it is easy for Richards is because his body can no longer feel pain.

In all seriousness though, it sounds like you just need to do it more often. Practice should get you over that in time.