From nobody Sat Sep 26 22:00:01 2026 Received: from mail-wm2-f12.google.com (mail-wm2-f12.google.com [74.125.225.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id ED01F3B4EB3 for ; Fri, 25 Sep 2026 16:57:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.225.140 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790355447; cv=none; b=up8yMvRSzl4eVF0tXN/xIsrtbvgZiKfNbdAADlRDMDtG7M4rUj6ytKyE1qBab+IqrpWqyR6KwGbuZgtkc70YsPzcTpq4epVeQNm+t9ch3CMPpEMvadkXhco7Ypg+FOvIVuCNqhtiQc7vgXoAznRducmNzgukREhG13H2ZhCmS6A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790355447; c=relaxed/simple; bh=s4Bhqa/9o2Mg9C+InyGA1v5tFMHsLYRgIw+LAOI5JvI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=iEdgovX0mw8ChQQf4hskRo9NWF5qnwUxDoh5yd17x4Ec19Cvq60NlmyOJSV3nXxloIdVF9hYyCLr4TZvIfhw8CxS2qIHqKTWmJEqyeiAcKuUhNQ8W6k34fk0DTvHGYyWB8T5C2nvNY/QalZQNTovK3tjV/BkpT+9JLvWl/0lPjo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=Ea7QIUSz; arc=none smtp.client-ip=74.125.225.140 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Ea7QIUSz" Received: by mail-wm2-f12.google.com with SMTP id 5b1f17b1804b1-49b912d822dso8347375e9.2 for ; Fri, 25 Sep 2026 09:57:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1790355436; x=1790960236; darn=vger.kernel.org; h=content-transfer-encoding:content-type:mime-version:references :in-reply-to:message-id:date:subject:cc:to:from:from:to:cc:subject :date:message-id:reply-to:content-type; bh=M37SYtPCvo9ogl7cjsynA5M6S0SGzDYIDrhu9A1R+So=; b=Ea7QIUSzIl7t6yQ7/xrb2PT+8hFD1HCbJgCdVLIadBXhOky0gp2mxag8zup1n0UEHz XtKxv5duXknFquIA1FmfOQrE6tHoCTUbOSS+DjLk72oFwq6UbrMDf5qXiFs1/kz9UKsz Q8SHKLDsi7WoM08VT1Iu1HT2J+tVcN75G5fWFcu72IEIua6vf/8UZmJXiarETPUOLjDX L2K/N63UNhjuCOGjIG8g3/Pjpzfh0tIbyYfnAevG+J7k59cKnXxcjWGTWA7jlO8qCVNt iwu0LdP5gL8ve9MGz/M1RyN13j85F/3fZRp4eebmpz3McBPj42qazgyoQ7BNR27FihHg nkig== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1790355436; x=1790960236; h=content-transfer-encoding:content-type:mime-version:references :in-reply-to:message-id:date:subject:cc:to:from:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=M37SYtPCvo9ogl7cjsynA5M6S0SGzDYIDrhu9A1R+So=; b=rUtg89SUYXB2zwiRHOwu1hTOJEHnpAdPJu3bDdsjcyThlfoQkwnAEC/vCBdjkFzV6w yrf1j6cwhlipb0qHYv3IVvqXek/4zWVK0uXUFr2FTRm2O5pYRkO3H3nq5N8FqXHJyrvd AnKjGuAscY5AyIycK9sxKK1K0U041HBGvYNR/UBgwW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OdNvQa54b1s dWSatLfQ3NbHuWGql X-Received: by 2002:a05:600c:4f54:b0:49c:fa21:1c89 with SMTP id 5b1f17b1804b1-49fe7bd0003mr110164735e9.30.1790355435863; Fri, 25 Sep 2026 09:57:15 -0700 (PDT) Received: from MacBook-Pro-von-Karl.localdomain (dynamic-2a02-3100-adc6-9c01-89ae-b145-8960-cf19.310.pool.telefonica.de. [2a02:3100:adc6:9c01:89ae:b145:8960:cf19]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4887a36189bsm8020363f8f.21.2026.09.25.09.57.14 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Fri, 25 Sep 2026 09:57:15 -0700 (PDT) From: Karl Mehltretter To: John Stultz , Thomas Gleixner Cc: Karl Mehltretter , Stephen Boyd , Miroslav Lichvar , Deepa Dinamani , Cassio Neri , =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= , David Laight , linux-kernel@vger.kernel.org Subject: [PATCH v2 1/2] time: Prevent time64_to_tm() day truncation on 32-bit Date: Fri, 25 Sep 2026 18:57:01 +0200 Message-Id: <20260925165702.51677-2-kmehltretter@gmail.com> X-Mailer: git-send-email 2.39.5 (Apple Git-154) In-Reply-To: <20260925165702.51677-1-kmehltretter@gmail.com> References: <20260925165702.51677-1-kmehltretter@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable time64_to_tm() accepts a 64-bit seconds value, but stores the quotient in long. On a 32-bit kernel the day count therefore wraps when it reaches 2^31, even though the corresponding year remains representable in struct tm. This is reachable when formatting externally supplied timestamps. For example, NILFS recovery copies the little-endian 64-bit ss_create field from an on-disk segment summary into time64_t. Its sysfs attributes then print that value with %ptTs, whose formatter calls time64_to_tm(). A corrupted image can consequently produce an architecture-dependent printed date. Keep the day quotient in s64 while leaving the seconds-within-day remainder as long. Use div_s64_rem() for the weekday calculation so 32-bit builds do not require compiler runtime division helpers. Fixes: e6c2682a1da3 ("time: Add time64_to_tm()") Assisted-by: LLM Reviewed-by: Thomas Wei=C3=9Fschuh Signed-off-by: Karl Mehltretter --- kernel/time/timeconv.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/kernel/time/timeconv.c b/kernel/time/timeconv.c index 59b922c826e7..f1cb61e0b114 100644 --- a/kernel/time/timeconv.c +++ b/kernel/time/timeconv.c @@ -49,7 +49,8 @@ void time64_to_tm(time64_t totalsecs, int offset, struct = tm *result) u32 u32tmp, day_of_century, year_of_century, day_of_year, month, day; u64 u64tmp, udays, century, year; bool is_Jan_or_Feb, is_leap_year; - long days, rem; + long rem; + s64 days; int remainder; =20 days =3D div_s64_rem(totalsecs, SECS_PER_DAY, &remainder); @@ -70,7 +71,8 @@ void time64_to_tm(time64_t totalsecs, int offset, struct = tm *result) result->tm_sec =3D rem % 60; =20 /* January 1, 1970 was a Thursday. */ - result->tm_wday =3D (4 + days) % 7; + div_s64_rem(days + 4, 7, &remainder); + result->tm_wday =3D remainder; if (result->tm_wday < 0) result->tm_wday +=3D 7; =20 --=20 2.39.5 (Apple Git-154) From nobody Sat Sep 26 22:00:01 2026 Received: from mail-wr2-f34.google.com (mail-wr2-f34.google.com [74.125.225.98]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5669D3B38BC for ; Fri, 25 Sep 2026 16:57:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.225.98 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790355446; cv=none; b=lGZjLrp4ZRoPvKodLB8H8Ozy7vHqg3du99rQXFdGrpkO0E1IWRCLiioDyvhjN3TNvgNHBOIAqirRQZC6gppvLmTfg3nuthuf1l9cmDq0yFf6FO4KVz8lVfuqr2tQuFpA8BrTWuuSw7pl7IwMBaNQ0ZsXDdkyXTxPl0n/M6X9nTs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790355446; c=relaxed/simple; bh=bnR6oveAJAqKO07GD5pI21LiZ7qQfCjA4aDACb15Zvk=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=rNo37HhNG3YCP1gZxxyWnt9k+MKtj6fohl/HU5HoYX5bfEcgrVAqQwJmQoqb1lwZ+9B2aJczeIVGJZRItpLiZw7XzQTc/EOWkiv7LQ/bagWBlZL+PDbg/etETI8VtCGPMWB4GytaPwCgoHq31h+4jEAsAqD1DyEd8VTPCzUC90E= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=D4iSbiwP; arc=none smtp.client-ip=74.125.225.98 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="D4iSbiwP" Received: by mail-wr2-f34.google.com with SMTP id ffacd0b85a97d-48884b021dbso69292f8f.3 for ; Fri, 25 Sep 2026 09:57:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1790355437; x=1790960237; darn=vger.kernel.org; h=content-transfer-encoding:content-type:mime-version:references :in-reply-to:message-id:date:subject:cc:to:from:from:to:cc:subject :date:message-id:reply-to:content-type; bh=/8+c8IwhstQAab6428QQht/BXHi81y8rJ08Qk2bhaG8=; b=D4iSbiwPHB62xbGM/xv2tvc5+r0rvtS8b8UCvDL/dlxZdgo9EFM7ghIqoyXXr+wUXn ODBgvO7qL6ju9CMPG1Fl256/05MOw3eHKbdnqCimLEe0hzQPC36aVHwVuNskKBklKme2 KyRrxKFBGGw3u0DdSI0XRxzdSNqXcavd3JPV9wftqPJVapu0lJHqlkdTTi0TDDwmEd0K jxEkr0NpUBu6wjdcQlXJjlwyGmB1qt2aR4+Au7cMHTv2wfI5Qo4foTKdB1zNncicvELL mIdyROgHaGFkNHdXUxG0hA3fw24cmqHSQucczAQOtukxx7jDuquGNJr/TQo+TYYJ/p4z 0h7Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1790355437; x=1790960237; h=content-transfer-encoding:content-type:mime-version:references :in-reply-to:message-id:date:subject:cc:to:from:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=/8+c8IwhstQAab6428QQht/BXHi81y8rJ08Qk2bhaG8=; b=UCh0Gz3eTrIeHCAOmN/OBRZ4PZsTH/F0qSm+EhSAY0M5fjCjoqDeP2B+5xYapaoEnj 4BU+YILfwotTNlW44dikqYoSDsD5BJnJfI0nlTQhRUPGklbB8JAbo+mwAVV3MhIEv5kR 2sooq7YSBg5NJOc/FMoZlGNg2xUDvIv3XK9D1ZodH5tT6MzmWLeXzzKFO4tKcELaf+nH WztUMIWq3ycuZg/jpiKYxdJh/8Onw5XuYZM3ip3KpDUFsCeYu0jNTN3sURzKv7zy2Byo hSAjGNQyMEEAC4t/jGGJefdJyGwzmQBsapi2bobjkFQr0N0wsJUcf+SKgL8VaDnG0oXk doew== X-Forwarded-Encrypted: i=1; AKwUvBxLLOwKKScq/SQTAri2ZVThB+QEVRJEhY0fc5mCP199dKCxkHtJ0rl+Tg+mvCmiTE8j1gm1h2eVckXJor0=@vger.kernel.org X-Gm-Message-State: AFuF++noF0G3fbxR8aczCSur2uak8oQzoRLD2TJwTimEgUcm6D1Ak1p6 CTV1JitekFZfGOLueJyNoVyTnnmw3OrGA3ZP+xqBRCLu91JXZ5MhWyTT X-Gm-Gg: AYBFou1R6GCNtuzzBhFCnfjxFUPZQjSmkPeqVj4M3s6faXoYQzjYSZdmfKvQevw89+7 pg4lQ2xher9bwJsofZn9bmFgDGX5cfcgjNY9VABHeUIgWuv0ql4psSjdgTkA4i/a0JlClKbWqom CtxM01eUvFXnTgDqudV/ERPyrWzEbulqwtwYGKvk6cjJ+fPQZbDFwUnw8qF1pdNl8QXmh9zcrzT BOWuDXdA366pMgoy0zrOS1HQi/VlB0fjP0oShZYY2Dkae0cLT1oGQr8+xxmzDUDBur61U7koP47 NcKRZSxbLAV3ltbN73Q46dEhRxmsKikpCGaW/cmmvL8plpsFD5GL9MHrdkMPW8nz64TfdokypLl Ulbm1Mgt2dmNe6rbWZdaLZ4YyhFlz/gZ6L6BbXeLab4tBZIoey+pDP+qSep1imupjCAYdYEFZhS JziKlkcERT84ubwX/iMTcZe1q0BVzgzpEn+LqtEKvVYKNB5Lmsu4+V2U9HKZp4XO0dNTFCAMoSm 9nk9uGVCOj8hViY0rhlmAn44e854jKPCtmi8yywfdz9nn/hrNdttgBke0vwClpGimh73lQ5SvJo GevMz/sIPb6bzwsuHt/t2OtQefFkaPNbN1KjiRvNzzvz7I6fuE0GknpUL+pFz6azMdZjpLLaftX NwnMPW5Y9+2UqrtHp X-Received: by 2002:a05:600d:8653:10b0:49f:e701:51c8 with SMTP id 5b1f17b1804b1-49fe70151ebmr83676225e9.9.1790355436835; Fri, 25 Sep 2026 09:57:16 -0700 (PDT) Received: from MacBook-Pro-von-Karl.localdomain (dynamic-2a02-3100-adc6-9c01-89ae-b145-8960-cf19.310.pool.telefonica.de. [2a02:3100:adc6:9c01:89ae:b145:8960:cf19]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4887a36189bsm8020363f8f.21.2026.09.25.09.57.15 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Fri, 25 Sep 2026 09:57:16 -0700 (PDT) From: Karl Mehltretter To: John Stultz , Thomas Gleixner Cc: Karl Mehltretter , Stephen Boyd , Miroslav Lichvar , Deepa Dinamani , Cassio Neri , =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= , David Laight , linux-kernel@vger.kernel.org Subject: [PATCH v2 2/2] time/kunit: Add time64_to_tm() case beyond 32-bit day count Date: Fri, 25 Sep 2026 18:57:02 +0200 Message-Id: <20260925165702.51677-3-kmehltretter@gmail.com> X-Mailer: git-send-email 2.39.5 (Apple Git-154) In-Reply-To: <20260925165702.51677-1-kmehltretter@gmail.com> References: <20260925165702.51677-1-kmehltretter@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable The existing date range test covers +/-80,000 years around the epoch, far below the day count at which a 32-bit long wraps (2^31 days, roughly 5.88 million years). Add a case at exactly 2^31 days after the epoch. Without the preceding fix a 32-bit kernel returns tm_year=3D-5879541 and wrong month, day, day-of-year and weekday; with it the result matches 64-bit kernels. Assisted-by: LLM Reviewed-by: Thomas Wei=C3=9Fschuh Signed-off-by: Karl Mehltretter --- kernel/time/time_test.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/kernel/time/time_test.c b/kernel/time/time_test.c index 1b99180da288..8b718767b3ba 100644 --- a/kernel/time/time_test.c +++ b/kernel/time/time_test.c @@ -87,8 +87,24 @@ static void time64_to_tm_test_date_range(struct kunit *t= est) } } =20 +static void time64_to_tm_test_wide_day_count(struct kunit *test) +{ + /* 2^31 days: the first count that does not fit in a 32-bit long. */ + time64_t timestamp =3D (1LL << 31) * 86400; + struct tm result; + + time64_to_tm(timestamp, 0, &result); + + KUNIT_EXPECT_EQ(test, result.tm_year, 5879680); + KUNIT_EXPECT_EQ(test, result.tm_mon, 6); + KUNIT_EXPECT_EQ(test, result.tm_mday, 12); + KUNIT_EXPECT_EQ(test, result.tm_yday, 193); + KUNIT_EXPECT_EQ(test, result.tm_wday, 6); +} + static struct kunit_case time_test_cases[] =3D { KUNIT_CASE_SLOW(time64_to_tm_test_date_range), + KUNIT_CASE(time64_to_tm_test_wide_day_count), {} }; =20 --=20 2.39.5 (Apple Git-154)