The Ramban in Devarim 22:6 states that the mitzvos are for our own good. Hashem gains nothing from any of them.
אבל אלו ההגדות אשר נתקשו על הרב כפי דעתי ענין אחר להם, שרצו לומר שאין התועלת במצות להקב״ה בעצמו ית׳, אבל התועלת באדם עצמו, למנוע ממנו נזק, או אמונה רעה, או מדה מגונה, או לזכור הנסים ונפלאות הבורא יתברך, לדעת את י״י. וזהו לצרוף בהן – שיהיו ככסף צרוף, כי הצורף הכסף אין מעשהו בלא טעם אבל להוציא ממנו כל סיג. וכן המצות להוציא מלבנו כל אמונה רעה, ולהודיענו האמת ולזכרו תמיד.
...They have no benefit for Hashem himself, they are for the benefit of man himself: to stop him from being damaged, bad beliefs, bad qualities, to remind him of the miracles Hashem did, to know Hashem. They purify him [the person], like one who refines silver by melting it to remove the impurities, so too the mitzvos remove our impurities. And also the mitzvot remove bad beliefs and teach us truth to remember always.
Therefore not only do we do harm to others when we sin, but we actually distance and destroy our connection with Hashem. We damage ourselves too! Hashem is constantly trying to shower us with all good, but we distance ourselves with our free choice. (See Ramchal Derech Hashem Chapter 4:10)
Gehinom is there to purify us from this harm.
Please see Chagiga 15b. Acher a sage sins horribly, he may not enter the world to come because of the sin, he may not enter punishment because of his Torah. Rebbi Meir mentions it would be better for Acher to face judgement (ie: Gehhna smoke pours from Acher's Kever) so that he should be able to enter the world to come. And that is precisely what Rebbi Meir causes to happen. We see a clear example (imo) that the "punishment" is for our benefit as well so that we may enter the world to come. Acher was not allowed to enter that world since he sinned, but after his punishment he is. The sin has been purified.
Also the punishment is limited in duration (12 months) See Sefer HaIkarim 4:38.
Also a number of commentators state explicitly that Hashem does things in an infinite way, gehinom (punishment) is limited because it precisely does not come from Hashem. Hashem does NOT punish us. We cause the punishment to come upon ourselves. (See 1st Perek of Tomer Devorah, and Rav Yaakov Mlisa on Eicha: לא תצא מפיו הרעות והטוב)
In theory Hashem is kind and allows this purification, had he not, that imo would be much more cruel.