Skip to content

Commit 9c71a2e

Browse files
avaginKAGA-KOKO
authored andcommitted
posix-clocks: Introduce clock_get_ktime() callback
The callsite in common_timer_get() has already a comment: /* * The timespec64 based conversion is suboptimal, but it's not * worth to implement yet another callback. */ kc->clock_get(timr->it_clock, &ts64); now = timespec64_to_ktime(ts64); The upcoming support for time namespaces requires to have access to: - The time in a task's time namespace for sys_clock_gettime() - The time in the root name space for common_timer_get() That adds a valid reason to finally implement a separate callback which returns the time in ktime_t format. Suggested-by: Thomas Gleixner <[email protected]> Co-developed-by: Dmitry Safonov <[email protected]> Signed-off-by: Andrei Vagin <[email protected]> Signed-off-by: Dmitry Safonov <[email protected]> Signed-off-by: Thomas Gleixner <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 2f58bf9 commit 9c71a2e

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

kernel/time/alarmtimer.c

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -663,7 +663,7 @@ static int alarm_clock_getres(const clockid_t which_clock, struct timespec64 *tp
663663
* @which_clock: clockid
664664
* @tp: timespec to fill.
665665
*
666-
* Provides the underlying alarm base time.
666+
* Provides the underlying alarm base time in a tasks time namespace.
667667
*/
668668
static int alarm_clock_get_timespec(clockid_t which_clock, struct timespec64 *tp)
669669
{
@@ -677,6 +677,22 @@ static int alarm_clock_get_timespec(clockid_t which_clock, struct timespec64 *tp
677677
return 0;
678678
}
679679

680+
/**
681+
* alarm_clock_get_ktime - posix clock_get_ktime interface
682+
* @which_clock: clockid
683+
*
684+
* Provides the underlying alarm base time in the root namespace.
685+
*/
686+
static ktime_t alarm_clock_get_ktime(clockid_t which_clock)
687+
{
688+
struct alarm_base *base = &alarm_bases[clock2alarm(which_clock)];
689+
690+
if (!alarmtimer_get_rtcdev())
691+
return -EINVAL;
692+
693+
return base->get_ktime();
694+
}
695+
680696
/**
681697
* alarm_timer_create - posix timer_create interface
682698
* @new_timer: k_itimer pointer to manage
@@ -840,6 +856,7 @@ static int alarm_timer_nsleep(const clockid_t which_clock, int flags,
840856

841857
const struct k_clock alarm_clock = {
842858
.clock_getres = alarm_clock_getres,
859+
.clock_get_ktime = alarm_clock_get_ktime,
843860
.clock_get_timespec = alarm_clock_get_timespec,
844861
.timer_create = alarm_timer_create,
845862
.timer_set = common_timer_set,

kernel/time/posix-timers.c

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,11 @@ static int posix_get_realtime_timespec(clockid_t which_clock, struct timespec64
171171
return 0;
172172
}
173173

174+
static ktime_t posix_get_realtime_ktime(clockid_t which_clock)
175+
{
176+
return ktime_get_real();
177+
}
178+
174179
/* Set clock_realtime */
175180
static int posix_clock_realtime_set(const clockid_t which_clock,
176181
const struct timespec64 *tp)
@@ -193,6 +198,11 @@ static int posix_get_monotonic_timespec(clockid_t which_clock, struct timespec64
193198
return 0;
194199
}
195200

201+
static ktime_t posix_get_monotonic_ktime(clockid_t which_clock)
202+
{
203+
return ktime_get();
204+
}
205+
196206
/*
197207
* Get monotonic-raw time for posix timers
198208
*/
@@ -228,12 +238,22 @@ static int posix_get_boottime_timespec(const clockid_t which_clock, struct times
228238
return 0;
229239
}
230240

241+
static ktime_t posix_get_boottime_ktime(const clockid_t which_clock)
242+
{
243+
return ktime_get_boottime();
244+
}
245+
231246
static int posix_get_tai_timespec(clockid_t which_clock, struct timespec64 *tp)
232247
{
233248
ktime_get_clocktai_ts64(tp);
234249
return 0;
235250
}
236251

252+
static ktime_t posix_get_tai_ktime(clockid_t which_clock)
253+
{
254+
return ktime_get_clocktai();
255+
}
256+
237257
static int posix_get_hrtimer_res(clockid_t which_clock, struct timespec64 *tp)
238258
{
239259
tp->tv_sec = 0;
@@ -781,7 +801,7 @@ static void common_hrtimer_arm(struct k_itimer *timr, ktime_t expires,
781801
* Posix magic: Relative CLOCK_REALTIME timers are not affected by
782802
* clock modifications, so they become CLOCK_MONOTONIC based under the
783803
* hood. See hrtimer_init(). Update timr->kclock, so the generic
784-
* functions which use timr->kclock->clock_get_timespec() work.
804+
* functions which use timr->kclock->clock_get_*() work.
785805
*
786806
* Note: it_clock stays unmodified, because the next timer_set() might
787807
* use ABSTIME, so it needs to switch back.
@@ -1262,6 +1282,7 @@ SYSCALL_DEFINE4(clock_nanosleep_time32, clockid_t, which_clock, int, flags,
12621282
static const struct k_clock clock_realtime = {
12631283
.clock_getres = posix_get_hrtimer_res,
12641284
.clock_get_timespec = posix_get_realtime_timespec,
1285+
.clock_get_ktime = posix_get_realtime_ktime,
12651286
.clock_set = posix_clock_realtime_set,
12661287
.clock_adj = posix_clock_realtime_adj,
12671288
.nsleep = common_nsleep,
@@ -1280,6 +1301,7 @@ static const struct k_clock clock_realtime = {
12801301
static const struct k_clock clock_monotonic = {
12811302
.clock_getres = posix_get_hrtimer_res,
12821303
.clock_get_timespec = posix_get_monotonic_timespec,
1304+
.clock_get_ktime = posix_get_monotonic_ktime,
12831305
.nsleep = common_nsleep,
12841306
.timer_create = common_timer_create,
12851307
.timer_set = common_timer_set,
@@ -1310,6 +1332,7 @@ static const struct k_clock clock_monotonic_coarse = {
13101332

13111333
static const struct k_clock clock_tai = {
13121334
.clock_getres = posix_get_hrtimer_res,
1335+
.clock_get_ktime = posix_get_tai_ktime,
13131336
.clock_get_timespec = posix_get_tai_timespec,
13141337
.nsleep = common_nsleep,
13151338
.timer_create = common_timer_create,
@@ -1326,6 +1349,7 @@ static const struct k_clock clock_tai = {
13261349

13271350
static const struct k_clock clock_boottime = {
13281351
.clock_getres = posix_get_hrtimer_res,
1352+
.clock_get_ktime = posix_get_boottime_ktime,
13291353
.clock_get_timespec = posix_get_boottime_timespec,
13301354
.nsleep = common_nsleep,
13311355
.timer_create = common_timer_create,

kernel/time/posix-timers.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@ struct k_clock {
66
struct timespec64 *tp);
77
int (*clock_set)(const clockid_t which_clock,
88
const struct timespec64 *tp);
9+
/* Returns the clock value in the current time namespace. */
910
int (*clock_get_timespec)(const clockid_t which_clock,
1011
struct timespec64 *tp);
12+
/* Returns the clock value in the root time namespace. */
13+
ktime_t (*clock_get_ktime)(const clockid_t which_clock);
1114
int (*clock_adj)(const clockid_t which_clock, struct __kernel_timex *tx);
1215
int (*timer_create)(struct k_itimer *timer);
1316
int (*nsleep)(const clockid_t which_clock, int flags,

0 commit comments

Comments
 (0)