Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

bcachefs: Fix negative timespecs

This fixes two problems in the handling of negative times:

• rem is signed, but the rem * c->sb.nsec_per_time_unit operation
produced a bogus unsigned result, because s32 * u32 = u32.

• The timespec was not normalized (it could contain more than a
billion nanoseconds).

For example, { .tv_sec = -14245441, .tv_nsec = 750000000 }, after
being round tripped through timespec_to_bch2_time and then
bch2_time_to_timespec would come back as
{ .tv_sec = -14245440, .tv_nsec = 4044967296 } (more than 4 billion
nanoseconds).

Cc: stable@vger.kernel.org
Fixes: 595c1e9bab7f ("bcachefs: Fix time handling")
Closes: https://github.com/koverstreet/bcachefs/issues/743
Co-developed-by: Erin Shepherd <erin.shepherd@e43.eu>
Signed-off-by: Erin Shepherd <erin.shepherd@e43.eu>
Co-developed-by: Ryan Lahfa <ryan@lahfa.xyz>
Signed-off-by: Ryan Lahfa <ryan@lahfa.xyz>
Signed-off-by: Alyssa Ross <hi@alyssa.is>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>

authored by

Alyssa Ross and committed by
Kent Overstreet
a3ed1cc4 16005147

+5 -2
+5 -2
fs/bcachefs/bcachefs.h
··· 1195 1195 static inline struct timespec64 bch2_time_to_timespec(const struct bch_fs *c, s64 time) 1196 1196 { 1197 1197 struct timespec64 t; 1198 + s64 sec; 1198 1199 s32 rem; 1199 1200 1200 1201 time += c->sb.time_base_lo; 1201 1202 1202 - t.tv_sec = div_s64_rem(time, c->sb.time_units_per_sec, &rem); 1203 - t.tv_nsec = rem * c->sb.nsec_per_time_unit; 1203 + sec = div_s64_rem(time, c->sb.time_units_per_sec, &rem); 1204 + 1205 + set_normalized_timespec64(&t, sec, rem * (s64)c->sb.nsec_per_time_unit); 1206 + 1204 1207 return t; 1205 1208 } 1206 1209