Reputation: 8335
I tried to compile some kernel modules from Pleora eBUS SDK on a Linux Ubuntu 19.04 machine with Kernel version 5.0.0-38-generic, but got the compilation error:
error: implicit declaration of function ‘current_kernel_time’; did you mean ‘core_kernel_text’? [-Werror=implicit-function-declaration]
lTime = current_kernel_time();
and another (same issue) for function do_gettimeofday
.
I noticed that current_kernel_time
and current_kernel_time64
were still available in kernel v4.15 (used by my other machine with Ubuntu 18.04 for example), last seen in linux v4.19.97. In time.h header of my kernel version there is a function get_timespec64
which seems to do something similar, however it accepts 2 arguments and it doesn't seem correct to me (because in the sources I saw that there is a copy_from_user
invoked) to pass some uninitialized const struct __kernel_timespec __user *uts
as a second argument. Can someone give me some hint how to replace calls to current_kernel_time
in my newer kernel version?
Example code that needs to be modified:
OS_UINT64 OS_TimeGetUS( OS_VOID )
{
struct timespec64 lTime;
lTime = current_kernel_time();
return ( lTime.tv_sec * 1000000 + OS_DIV64( lTime.tv_nsec, 1000 ) );
}
Upvotes: 3
Views: 5334
Reputation: 69477
The current_kernel_time()
function got deprecated and moved to timekeeping32.h
in v4.15 (commit) and then completely removed in v4.20 (commit). Newer timekeeping functions were introduced in v4.18 (commit).
The new exported functions are (source):
extern void ktime_get_raw_ts64(struct timespec64 *ts);
extern void ktime_get_ts64(struct timespec64 *ts);
extern void ktime_get_real_ts64(struct timespec64 *tv);
extern void ktime_get_coarse_ts64(struct timespec64 *ts);
extern void ktime_get_coarse_real_ts64(struct timespec64 *ts);
See also the relevant kernel documentation page, which states:
Deprecated time interfaces
[...]
struct timespec current_kernel_time(void) struct timespec64 current_kernel_time64(void) struct timespec get_monotonic_coarse(void) struct timespec64 get_monotonic_coarse64(void)
These are replaced by
ktime_get_coarse_real_ts64()
andktime_get_coarse_ts64()
. However, A lot of code that wants coarse-grained times can use the simple ‘jiffies’ instead, while some drivers may actually want the higher resolution accessors these days.
You can rewrite your code as:
OS_UINT64 OS_TimeGetUS( OS_VOID )
{
struct timespec64 lTime;
ktime_get_coarse_real_ts64(&lTime);
return (lTime.tv_sec * 1000000 + OS_DIV64( lTime.tv_nsec, 1000 ) );
}
However I see that you are returning the time in microseconds. In such case, you might want to directly use ktime_t ktime_get(void)
or the equivalent u64 ktime_get_ns(void)
. The code snippet you provide could then be rewritten as:
OS_UINT64 OS_TimeGetUS( OS_VOID )
{
return OS_DIV64(ktime_get_ns(), 1000);
}
Upvotes: 7