All perfect praise be to Allah, the Lord of the worlds. I testify that there is none worthy of worship except Allah and that Muhammad, sallallahu ‘alayhi wa sallam, is His slave and Messenger.
Rinsing the mouth is valid if the person swallows the water or some of it after rinsing. Ibn Qudaamah said, "Exaggeration in rinsing the mouth is to turn the water around in the depth of the mouth and its far sides; and if he swallows it, it is permissible because the washing of the mouth was achieved." [Al-Mughni]
Accordingly, if you swallow the water while rinsing the mouth or some of it deliberately or forgetfully, this does not invalidate the rinsing and you are not obliged to repeat it whether you are performing Ghusl or ablution.
Allah knows best.