Skip to content

Commit 01679b5

Browse files
committed
posix-timers: Document sys_clock_getres() correctly
The decades old comment about Posix clock resolution is confusing at best. Remove it and add a proper explanation to sys_clock_getres(). Signed-off-by: Thomas Gleixner <tglx@linutronix.de> Reviewed-by: Frederic Weisbecker <frederic@kernel.org> Link: https://lore.kernel.org/r/20230425183313.356427330@linutronix.de
1 parent 8cc96ca commit 01679b5

File tree

1 file changed

+73
-8
lines changed

1 file changed

+73
-8
lines changed

kernel/time/posix-timers.c

Lines changed: 73 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,6 @@ static const struct k_clock clock_realtime, clock_monotonic;
6767
* to implement others. This structure defines the various
6868
* clocks.
6969
*
70-
* RESOLUTION: Clock resolution is used to round up timer and interval
71-
* times, NOT to report clock times, which are reported with as
72-
* much resolution as the system can muster. In some cases this
73-
* resolution may depend on the underlying clock hardware and
74-
* may not be quantifiable until run time, and only then is the
75-
* necessary code is written. The standard says we should say
76-
* something about this issue in the documentation...
77-
*
7870
* FUNCTIONS: The CLOCKs structure defines possible functions to
7971
* handle various clock functions.
8072
*
@@ -1198,6 +1190,79 @@ SYSCALL_DEFINE2(clock_adjtime, const clockid_t, which_clock,
11981190
return err;
11991191
}
12001192

1193+
/**
1194+
* sys_clock_getres - Get the resolution of a clock
1195+
* @which_clock: The clock to get the resolution for
1196+
* @tp: Pointer to a a user space timespec64 for storage
1197+
*
1198+
* POSIX defines:
1199+
*
1200+
* "The clock_getres() function shall return the resolution of any
1201+
* clock. Clock resolutions are implementation-defined and cannot be set by
1202+
* a process. If the argument res is not NULL, the resolution of the
1203+
* specified clock shall be stored in the location pointed to by res. If
1204+
* res is NULL, the clock resolution is not returned. If the time argument
1205+
* of clock_settime() is not a multiple of res, then the value is truncated
1206+
* to a multiple of res."
1207+
*
1208+
* Due to the various hardware constraints the real resolution can vary
1209+
* wildly and even change during runtime when the underlying devices are
1210+
* replaced. The kernel also can use hardware devices with different
1211+
* resolutions for reading the time and for arming timers.
1212+
*
1213+
* The kernel therefore deviates from the POSIX spec in various aspects:
1214+
*
1215+
* 1) The resolution returned to user space
1216+
*
1217+
* For CLOCK_REALTIME, CLOCK_MONOTONIC, CLOCK_BOOTTIME, CLOCK_TAI,
1218+
* CLOCK_REALTIME_ALARM, CLOCK_BOOTTIME_ALAREM and CLOCK_MONOTONIC_RAW
1219+
* the kernel differentiates only two cases:
1220+
*
1221+
* I) Low resolution mode:
1222+
*
1223+
* When high resolution timers are disabled at compile or runtime
1224+
* the resolution returned is nanoseconds per tick, which represents
1225+
* the precision at which timers expire.
1226+
*
1227+
* II) High resolution mode:
1228+
*
1229+
* When high resolution timers are enabled the resolution returned
1230+
* is always one nanosecond independent of the actual resolution of
1231+
* the underlying hardware devices.
1232+
*
1233+
* For CLOCK_*_ALARM the actual resolution depends on system
1234+
* state. When system is running the resolution is the same as the
1235+
* resolution of the other clocks. During suspend the actual
1236+
* resolution is the resolution of the underlying RTC device which
1237+
* might be way less precise than the clockevent device used during
1238+
* running state.
1239+
*
1240+
* For CLOCK_REALTIME_COARSE and CLOCK_MONOTONIC_COARSE the resolution
1241+
* returned is always nanoseconds per tick.
1242+
*
1243+
* For CLOCK_PROCESS_CPUTIME and CLOCK_THREAD_CPUTIME the resolution
1244+
* returned is always one nanosecond under the assumption that the
1245+
* underlying scheduler clock has a better resolution than nanoseconds
1246+
* per tick.
1247+
*
1248+
* For dynamic POSIX clocks (PTP devices) the resolution returned is
1249+
* always one nanosecond.
1250+
*
1251+
* 2) Affect on sys_clock_settime()
1252+
*
1253+
* The kernel does not truncate the time which is handed in to
1254+
* sys_clock_settime(). The kernel internal timekeeping is always using
1255+
* nanoseconds precision independent of the clocksource device which is
1256+
* used to read the time from. The resolution of that device only
1257+
* affects the presicion of the time returned by sys_clock_gettime().
1258+
*
1259+
* Returns:
1260+
* 0 Success. @tp contains the resolution
1261+
* -EINVAL @which_clock is not a valid clock ID
1262+
* -EFAULT Copying the resolution to @tp faulted
1263+
* -ENODEV Dynamic POSIX clock is not backed by a device
1264+
* -EOPNOTSUPP Dynamic POSIX clock does not support getres()
1265+
*/
12011266
SYSCALL_DEFINE2(clock_getres, const clockid_t, which_clock,
12021267
struct __kernel_timespec __user *, tp)
12031268
{

0 commit comments

Comments
 (0)