Skip to content

Commit 381d96c

Browse files
t-8chKAGA-KOKO
authored andcommitted
vdso/gettimeofday: Introduce vdso_set_timespec()
This code is duplicated and with the introduction of auxiliary clocks will be duplicated even more. Introduce a helper. Signed-off-by: Thomas Weißschuh <[email protected]> Signed-off-by: Thomas Gleixner <[email protected]> Link: https://lore.kernel.org/all/[email protected]
1 parent 1a1cd5f commit 381d96c

File tree

1 file changed

+14
-18
lines changed

1 file changed

+14
-18
lines changed

lib/vdso/gettimeofday.c

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,16 @@ static __always_inline bool vdso_clockid_valid(clockid_t clock)
7777
return likely((u32) clock < MAX_CLOCKS);
7878
}
7979

80+
/*
81+
* Must not be invoked within the sequence read section as a race inside
82+
* that loop could result in __iter_div_u64_rem() being extremely slow.
83+
*/
84+
static __always_inline void vdso_set_timespec(struct __kernel_timespec *ts, u64 sec, u64 ns)
85+
{
86+
ts->tv_sec = sec + __iter_div_u64_rem(ns, NSEC_PER_SEC, &ns);
87+
ts->tv_nsec = ns;
88+
}
89+
8090
#ifdef CONFIG_TIME_NS
8191

8292
#ifdef CONFIG_GENERIC_VDSO_DATA_STORE
@@ -122,12 +132,7 @@ bool do_hres_timens(const struct vdso_time_data *vdns, const struct vdso_clock *
122132
sec += offs->sec;
123133
ns += offs->nsec;
124134

125-
/*
126-
* Do this outside the loop: a race inside the loop could result
127-
* in __iter_div_u64_rem() being extremely slow.
128-
*/
129-
ts->tv_sec = sec + __iter_div_u64_rem(ns, NSEC_PER_SEC, &ns);
130-
ts->tv_nsec = ns;
135+
vdso_set_timespec(ts, sec, ns);
131136

132137
return true;
133138
}
@@ -188,12 +193,7 @@ bool do_hres(const struct vdso_time_data *vd, const struct vdso_clock *vc,
188193
sec = vdso_ts->sec;
189194
} while (unlikely(vdso_read_retry(vc, seq)));
190195

191-
/*
192-
* Do this outside the loop: a race inside the loop could result
193-
* in __iter_div_u64_rem() being extremely slow.
194-
*/
195-
ts->tv_sec = sec + __iter_div_u64_rem(ns, NSEC_PER_SEC, &ns);
196-
ts->tv_nsec = ns;
196+
vdso_set_timespec(ts, sec, ns);
197197

198198
return true;
199199
}
@@ -223,12 +223,8 @@ bool do_coarse_timens(const struct vdso_time_data *vdns, const struct vdso_clock
223223
sec += offs->sec;
224224
nsec += offs->nsec;
225225

226-
/*
227-
* Do this outside the loop: a race inside the loop could result
228-
* in __iter_div_u64_rem() being extremely slow.
229-
*/
230-
ts->tv_sec = sec + __iter_div_u64_rem(nsec, NSEC_PER_SEC, &nsec);
231-
ts->tv_nsec = nsec;
226+
vdso_set_timespec(ts, sec, nsec);
227+
232228
return true;
233229
}
234230
#else

0 commit comments

Comments
 (0)