The Gemara in brachos 5a says.
אָמַר רָבָא, וְאִיתֵּימָא רַב חִסְדָּא: אִם רוֹאֶה אָדָם שֶׁיִּסּוּרִין בָּאִין עָלָיו — יְפַשְׁפֵּשׁ בְּמַעֲשָׂיו, שֶׁנֶּאֱמַר: ״נַחְפְּשָׂה דְרָכֵינוּ וְנַחְקֹרָה וְנָשׁוּבָה עַד ה׳״. פִּשְׁפֵּשׁ וְלֹא מָצָא — יִתְלֶה בְּבִטּוּל תּוֹרָה, שֶׁנֶּאֱמַר: ״אַשְׁרֵי הַגֶּבֶר אֲשֶׁר תְּיַסְּרֶנּוּ יָּהּ וּמִתּוֹרָתְךָ תְלַמְּדֶנּוּ״.
Rava, and some say Rav Ḥisda, said: If a person sees that suffering has befallen him, he should examine his actions. Generally, suffering comes about as punishment for one’s transgressions, as it is stated: “We will search and examine our ways, and return to God” (Lamentations 3:40). If he examined his ways and found no transgression for which that suffering is appropriate, he may attribute his suffering to dereliction in the study of Torah. God punishes an individual for dereliction in the study of Torah in order to emphasize the gravity of the issue, as it is stated: “Happy is the man whom You punish, Lord, and teach out of Your law” (Psalms 94:12). This verse teaches us that his suffering will cause him to return to Your law.
וְאִם תָּלָה וְלֹא מָצָא — בְּיָדוּעַ שֶׁיִּסּוּרִין שֶׁל אַהֲבָה הֵם, שֶׁנֶּאֱמַר: ״כִּי אֶת אֲשֶׁר יֶאֱהַב ה׳ יוֹכִיחַ״.
And if he did attribute his suffering to dereliction in the study of Torah, and did not find this to be so, he may be confident that these are afflictions of love, as it is stated: “For whom the Lord loves, He rebukes, as does a father the son in whom he delights” (Proverbs 3:12).
https://www.sefaria.org/Berakhot.5a.10
Pretty much this gemara implies all sufferings are from sin. Except afflictions of love.
Afflictions of love is a machlokes, what it means.
A basic pshat from the Ben Yehoyada there, is that rightous suffer to atone for others, or to give them more time to repent. Which they would get rewarded for in the world to come. As rashi there says.
Children suffering comes from the sins of the parents. What did the children do wrong, for that I heard you have to say they're a gilgul, that needs a tikun. (Don't have source inside for that one)