[Devel] [PATCH RHEL9 COMMIT] kmsg: Virtualize timestamps in /dev/ksmg output

Konstantin Khorenko khorenko at virtuozzo.com
Tue Feb 21 16:47:05 MSK 2023


The commit is pushed to "branch-rh9-5.14.0-162.6.1.vz9.18.x-ovz" and will appear at https://src.openvz.org/scm/ovz/vzkernel.git
after rh9-5.14.0-162.6.1.vz9.18.11
------>
commit 95f395f7cdb0b6d61ac19841d464f73ca717ce9b
Author: Konstantin Khorenko <khorenko at virtuozzo.com>
Date:   Tue Feb 7 20:37:42 2023 +0300

    kmsg: Virtualize timestamps in /dev/ksmg output
    
    Internally log messages contain the timestamps relative to the Host boot
    time, while Container could start much later than the Host boots and the
    timestamps should be shifted accordingly.
    
    Otherwise "dmesg -H" reports data from the future.
    
    Note:
     * time_ns->offsets.boottime is (ve_uptime - host_uptime),
       i.e. negative for Containers created on this Host
     * ring buffer contains timestamps relative to the Host boot
    
     => we have to add the .boottime offset to the timestamp of Host
        to get the msg entry timestamp relative to Container boottime.
    
    Note 2: we cannot use timespec64_to_ns() in ve_timens_sub_boottime_ns()
            to convert .boottime to nsec because timespec64_to_ns() is not
            designed to handle negative offsets, it tries to avoid the
            overflow and returns KTIME_MAX instead for negative offsets.
    
    Based on the vz7 commit
      e20414197aef ("kmsg: Virtualize timestamps in /dev/ksmg output")
    
    https://jira.sw.ru/browse/PSBM-145313
    
    Signed-off-by: Konstantin Khorenko <khorenko at virtuozzo.com>
    Reviewed-by: Alexander Atanasov <alexander.atanasov at virtuozzo.com>
    
    Feature: time: per-CT virtualization enhancements
---
 kernel/printk/printk.c | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)

diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
index b61fcb03f968..4db25f4e3e8c 100644
--- a/kernel/printk/printk.c
+++ b/kernel/printk/printk.c
@@ -600,6 +600,25 @@ static void append_char(char **pp, char *e, char c)
 		*(*pp)++ = c;
 }
 
+static inline u64 ve_timens_add_boottime_ns(u64 nsec)
+{
+	struct time_namespace *ve_time_ns;
+	struct timens_offsets *ns_offsets;
+
+	ve_time_ns = ve_get_time_ns(get_exec_env());
+	if (unlikely(!ve_time_ns)) {
+		/* container not yet started */
+		return nsec;
+	}
+
+	ns_offsets = &ve_time_ns->offsets;
+	nsec += ns_offsets->boottime.tv_sec * NSEC_PER_SEC;
+	nsec += ns_offsets->boottime.tv_nsec;
+	put_time_ns(ve_time_ns);
+
+	return nsec;
+}
+
 static ssize_t info_print_ext_header(char *buf, size_t size,
 				     struct printk_info *info)
 {
@@ -614,6 +633,8 @@ static ssize_t info_print_ext_header(char *buf, size_t size,
 	caller[0] = '\0';
 #endif
 
+	/* shift the timestamp on the Container uptime value */
+	ts_usec = ve_timens_add_boottime_ns(ts_usec);
 	do_div(ts_usec, 1000);
 
 	return scnprintf(buf, size, "%u,%llu,%llu,%c%s;",


More information about the Devel mailing list