Module Name:    src
Committed By:   thorpej
Date:           Thu Jan  2 15:42:27 UTC 2020

Modified Files:
        src/sys/compat/common: kern_info_43.c kern_time_50.c
        src/sys/compat/netbsd32: netbsd32_sysctl.c
        src/sys/external/bsd/drm2/include/linux: ktime.h
        src/sys/fs/nfs/common: nfs_lock.c
        src/sys/kern: init_main.c init_sysctl.c kern_rndq.c kern_tc.c
            kern_time.c
        src/sys/miscfs/fdesc: fdesc_vnops.c
        src/sys/miscfs/kernfs: kernfs.h kernfs_vnops.c
        src/sys/miscfs/procfs: procfs_linux.c
        src/sys/nfs: nfs_serv.c
        src/sys/rump/librump/rumpkern: cons.c emul.c rump.c
        src/sys/sys: kernel.h timevar.h

Log Message:
- Eliminate the global "boottime" variable, which was being accessed
  without any synchronization against changes by e.g. clock_settime().
- Replace with new getbinboottime() / getnanoboottime() / getmicroboottime()
  functions (naming mirrors that of other time access functions in kern_tc.c).
  It returns the (maybe-converted) value of timebasebin, which also tracks
  our estimate of when the system was booted (i.e. the legacy "boottime" was
  redundant).

XXX There needs to be a lockless synchronization mechanism for reading
timebasebin, but this is a problem in kern_tc.c that pre-existed these
"boottime" changes.  At least now the problem is centralized in one location.


To generate a diff of this commit:
cvs rdiff -u -r1.37 -r1.38 src/sys/compat/common/kern_info_43.c
cvs rdiff -u -r1.33 -r1.34 src/sys/compat/common/kern_time_50.c
cvs rdiff -u -r1.40 -r1.41 src/sys/compat/netbsd32/netbsd32_sysctl.c
cvs rdiff -u -r1.7 -r1.8 src/sys/external/bsd/drm2/include/linux/ktime.h
cvs rdiff -u -r1.2 -r1.3 src/sys/fs/nfs/common/nfs_lock.c
cvs rdiff -u -r1.516 -r1.517 src/sys/kern/init_main.c
cvs rdiff -u -r1.222 -r1.223 src/sys/kern/init_sysctl.c
cvs rdiff -u -r1.95 -r1.96 src/sys/kern/kern_rndq.c
cvs rdiff -u -r1.53 -r1.54 src/sys/kern/kern_tc.c
cvs rdiff -u -r1.202 -r1.203 src/sys/kern/kern_time.c
cvs rdiff -u -r1.130 -r1.131 src/sys/miscfs/fdesc/fdesc_vnops.c
cvs rdiff -u -r1.40 -r1.41 src/sys/miscfs/kernfs/kernfs.h
cvs rdiff -u -r1.161 -r1.162 src/sys/miscfs/kernfs/kernfs_vnops.c
cvs rdiff -u -r1.79 -r1.80 src/sys/miscfs/procfs/procfs_linux.c
cvs rdiff -u -r1.177 -r1.178 src/sys/nfs/nfs_serv.c
cvs rdiff -u -r1.8 -r1.9 src/sys/rump/librump/rumpkern/cons.c
cvs rdiff -u -r1.194 -r1.195 src/sys/rump/librump/rumpkern/emul.c
cvs rdiff -u -r1.338 -r1.339 src/sys/rump/librump/rumpkern/rump.c
cvs rdiff -u -r1.31 -r1.32 src/sys/sys/kernel.h
cvs rdiff -u -r1.39 -r1.40 src/sys/sys/timevar.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/compat/common/kern_info_43.c
diff -u src/sys/compat/common/kern_info_43.c:1.37 src/sys/compat/common/kern_info_43.c:1.38
--- src/sys/compat/common/kern_info_43.c:1.37	Sun Jan 27 02:08:39 2019
+++ src/sys/compat/common/kern_info_43.c	Thu Jan  2 15:42:26 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_info_43.c,v 1.37 2019/01/27 02:08:39 pgoyette Exp $	*/
+/*	$NetBSD: kern_info_43.c,v 1.38 2020/01/02 15:42:26 thorpej Exp $	*/
 
 /*
  * Copyright (c) 1982, 1986, 1991, 1993
@@ -32,7 +32,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: kern_info_43.c,v 1.37 2019/01/27 02:08:39 pgoyette Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_info_43.c,v 1.38 2020/01/02 15:42:26 thorpej Exp $");
 
 #if defined(_KERNEL_OPT)
 #include "opt_compat_netbsd.h"
@@ -283,7 +283,7 @@ compat_43_sys_getkerninfo(struct lwp *l,
 			ksi.open_max = OPEN_MAX;
 			ksi.child_max = CHILD_MAX;
 
-			TIMESPEC_TO_TIMEVAL(&tv, &boottime);
+			getmicroboottime(&tv);
 			timeval_to_timeval50(&tv, &ksi.boottime);
 			COPY(hostname);
 

Index: src/sys/compat/common/kern_time_50.c
diff -u src/sys/compat/common/kern_time_50.c:1.33 src/sys/compat/common/kern_time_50.c:1.34
--- src/sys/compat/common/kern_time_50.c:1.33	Sun Jan 27 02:08:39 2019
+++ src/sys/compat/common/kern_time_50.c	Thu Jan  2 15:42:26 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_time_50.c,v 1.33 2019/01/27 02:08:39 pgoyette Exp $	*/
+/*	$NetBSD: kern_time_50.c,v 1.34 2020/01/02 15:42:26 thorpej Exp $	*/
 
 /*-
  * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
@@ -29,7 +29,7 @@
  * POSSIBILITY OF SUCH DAMAGE.
  */
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: kern_time_50.c,v 1.33 2019/01/27 02:08:39 pgoyette Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_time_50.c,v 1.34 2020/01/02 15:42:26 thorpej Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_compat_netbsd.h"
@@ -585,7 +585,7 @@ compat_sysctl_time(struct sysctllog **cl
 {
 	struct timeval tv;
 
-	TIMESPEC_TO_TIMEVAL(&tv, &boottime);
+	getmicroboottime(&tv);
 	timeval_to_timeval50(&tv, &boottime50);
 
 	sysctl_createv(clog, 0, NULL, NULL,

Index: src/sys/compat/netbsd32/netbsd32_sysctl.c
diff -u src/sys/compat/netbsd32/netbsd32_sysctl.c:1.40 src/sys/compat/netbsd32/netbsd32_sysctl.c:1.41
--- src/sys/compat/netbsd32/netbsd32_sysctl.c:1.40	Sat May 26 21:07:47 2018
+++ src/sys/compat/netbsd32/netbsd32_sysctl.c	Thu Jan  2 15:42:26 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: netbsd32_sysctl.c,v 1.40 2018/05/26 21:07:47 kamil Exp $	*/
+/*	$NetBSD: netbsd32_sysctl.c,v 1.41 2020/01/02 15:42:26 thorpej Exp $	*/
 
 /*
  * Copyright (c) 2003 The NetBSD Foundation, Inc.
@@ -32,7 +32,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: netbsd32_sysctl.c,v 1.40 2018/05/26 21:07:47 kamil Exp $");
+__KERNEL_RCSID(0, "$NetBSD: netbsd32_sysctl.c,v 1.41 2020/01/02 15:42:26 thorpej Exp $");
 
 #if defined(_KERNEL_OPT)
 #include "opt_ddb.h"
@@ -80,8 +80,10 @@ netbsd32_sysctl_kern_boottime(SYSCTLFN_A
 {
 	struct sysctlnode node;
 	struct netbsd32_timespec bt32;
+	struct timespec ts;
 
-	netbsd32_from_timespec(&boottime, &bt32);
+	getnanoboottime(&ts);
+	netbsd32_from_timespec(&ts, &bt32);
 
 	node = *rnode;
 	node.sysctl_data = &bt32;

Index: src/sys/external/bsd/drm2/include/linux/ktime.h
diff -u src/sys/external/bsd/drm2/include/linux/ktime.h:1.7 src/sys/external/bsd/drm2/include/linux/ktime.h:1.8
--- src/sys/external/bsd/drm2/include/linux/ktime.h:1.7	Mon Aug 27 13:57:38 2018
+++ src/sys/external/bsd/drm2/include/linux/ktime.h	Thu Jan  2 15:42:26 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: ktime.h,v 1.7 2018/08/27 13:57:38 riastradh Exp $	*/
+/*	$NetBSD: ktime.h,v 1.8 2020/01/02 15:42:26 thorpej Exp $	*/
 
 /*-
  * Copyright (c) 2013 The NetBSD Foundation, Inc.
@@ -131,15 +131,21 @@ ktime_get_raw_ns(void)
 static inline ktime_t
 ktime_get_monotonic_offset(void)
 {
-	return timespec_to_ktime(boottime);
+	struct timespec ts;
+
+	getnanoboottime(&ts);
+
+	return timespec_to_ktime(ts);
 }
 
 static inline ktime_t
 ktime_mono_to_real(ktime_t kt)
 {
 	struct timespec ts = ktime_to_timespec(kt);
+	struct timespec bts;
 
-	timespecadd(&ts, &boottime, &ts);
+	getnanoboottime(&bts);
+	timespecadd(&ts, &bts, &ts);
 
 	return timespec_to_ktime(ts);
 }

Index: src/sys/fs/nfs/common/nfs_lock.c
diff -u src/sys/fs/nfs/common/nfs_lock.c:1.2 src/sys/fs/nfs/common/nfs_lock.c:1.3
--- src/sys/fs/nfs/common/nfs_lock.c:1.2	Tue Dec 13 22:41:46 2016
+++ src/sys/fs/nfs/common/nfs_lock.c	Thu Jan  2 15:42:26 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: nfs_lock.c,v 1.2 2016/12/13 22:41:46 pgoyette Exp $	*/
+/*	$NetBSD: nfs_lock.c,v 1.3 2020/01/02 15:42:26 thorpej Exp $	*/
 /*-
  * Copyright (c) 1997 Berkeley Software Design, Inc. All rights reserved.
  *
@@ -31,7 +31,7 @@
 
 #include <sys/cdefs.h>
 /* __FBSDID("FreeBSD: head/sys/nfs/nfs_lock.c 303382 2016-07-27 11:08:59Z kib "); */
-__RCSID("$NetBSD: nfs_lock.c,v 1.2 2016/12/13 22:41:46 pgoyette Exp $");
+__RCSID("$NetBSD: nfs_lock.c,v 1.3 2020/01/02 15:42:26 thorpej Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -243,7 +243,7 @@ nfs_dolock(struct vop_advlock_args *ap)
 	struct flock *fl;
 	struct proc *p;
 	struct nfsmount *nmp;
-	struct timeval boottime;
+	struct timeval btv;
 
 	td = curthread;
 	p = td->td_proc;
@@ -287,8 +287,8 @@ nfs_dolock(struct vop_advlock_args *ap)
 		p->p_nlminfo = malloc(sizeof(struct nlminfo),
 		    M_NLMINFO, M_WAITOK | M_ZERO);
 		p->p_nlminfo->pid_start = p->p_stats->p_start;
-		getboottime(&boottime);
-		timevaladd(&p->p_nlminfo->pid_start, &boottime);
+		getmicroboottime(&btv);
+		timevaladd(&p->p_nlminfo->pid_start, &btv);
 	}
 	msg.lm_msg_ident.pid_start = p->p_nlminfo->pid_start;
 	msg.lm_msg_ident.msg_seq = ++(p->p_nlminfo->msg_seq);

Index: src/sys/kern/init_main.c
diff -u src/sys/kern/init_main.c:1.516 src/sys/kern/init_main.c:1.517
--- src/sys/kern/init_main.c:1.516	Wed Jan  1 22:57:17 2020
+++ src/sys/kern/init_main.c	Thu Jan  2 15:42:27 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: init_main.c,v 1.516 2020/01/01 22:57:17 thorpej Exp $	*/
+/*	$NetBSD: init_main.c,v 1.517 2020/01/02 15:42:27 thorpej Exp $	*/
 
 /*-
  * Copyright (c) 2008, 2009, 2019 The NetBSD Foundation, Inc.
@@ -97,7 +97,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: init_main.c,v 1.516 2020/01/01 22:57:17 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: init_main.c,v 1.517 2020/01/02 15:42:27 thorpej Exp $");
 
 #include "opt_ddb.h"
 #include "opt_inet.h"
@@ -242,7 +242,6 @@ struct	vnode *rootvp, *swapdev_vp;
 int	boothowto;
 int	cold __read_mostly = 1;		/* still working on startup */
 int	shutting_down __read_mostly;	/* system is shutting down */
-struct timespec boottime;	        /* time at system startup - will only follow settime deltas */
 
 int	start_init_exec;		/* semaphore for start_init() */
 
@@ -688,16 +687,6 @@ main(void)
 	 * munched in mi_switch() after the time got set.
 	 */
 	getnanotime(&time);
-	{
-		struct timespec ut;
-		/*
-		 * was:
-		 *	boottime = time;
-		 * but we can do better
-		 */
-		nanouptime(&ut);
-		timespecsub(&time, &ut, &boottime);
-	}
 
 	mutex_enter(proc_lock);
 	LIST_FOREACH(p, &allproc, p_list) {

Index: src/sys/kern/init_sysctl.c
diff -u src/sys/kern/init_sysctl.c:1.222 src/sys/kern/init_sysctl.c:1.223
--- src/sys/kern/init_sysctl.c:1.222	Tue Jan 15 07:11:23 2019
+++ src/sys/kern/init_sysctl.c	Thu Jan  2 15:42:27 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: init_sysctl.c,v 1.222 2019/01/15 07:11:23 mrg Exp $ */
+/*	$NetBSD: init_sysctl.c,v 1.223 2020/01/02 15:42:27 thorpej Exp $ */
 
 /*-
  * Copyright (c) 2003, 2007, 2008, 2009 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: init_sysctl.c,v 1.222 2019/01/15 07:11:23 mrg Exp $");
+__KERNEL_RCSID(0, "$NetBSD: init_sysctl.c,v 1.223 2020/01/02 15:42:27 thorpej Exp $");
 
 #include "opt_sysv.h"
 #include "opt_compat_netbsd.h"
@@ -110,6 +110,7 @@ dcopyout(struct lwp *l, const void *kadd
 
 static int sysctl_kern_maxvnodes(SYSCTLFN_PROTO);
 static int sysctl_kern_messages(SYSCTLFN_PROTO);
+static int sysctl_kern_boottime(SYSCTLFN_PROTO);
 static int sysctl_kern_rtc_offset(SYSCTLFN_PROTO);
 static int sysctl_kern_maxproc(SYSCTLFN_PROTO);
 static int sysctl_kern_hostid(SYSCTLFN_PROTO);
@@ -235,7 +236,7 @@ SYSCTL_SETUP(sysctl_kern_setup, "sysctl 
 		       CTLFLAG_PERMANENT,
 		       CTLTYPE_STRUCT, "boottime",
 		       SYSCTL_DESCR("System boot time"),
-		       NULL, 0, &boottime, sizeof(boottime),
+		       sysctl_kern_boottime, 0, NULL, sizeof(struct timespec),
 		       CTL_KERN, KERN_BOOTTIME, CTL_EOL);
 	sysctl_createv(clog, 0, NULL, NULL,
 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
@@ -803,6 +804,21 @@ sysctl_kern_messages(SYSCTLFN_ARGS)
 }
 
 /*
+ * sysctl helper routine for the kern.boottime node
+ */
+static int
+sysctl_kern_boottime(SYSCTLFN_ARGS)
+{
+	struct sysctlnode node;
+	struct timespec ts;
+
+	getnanoboottime(&ts);
+	node = *rnode;
+	node.sysctl_data = &ts;
+	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
+}
+
+/*
  * sysctl helper routine for rtc_offset - set time after changes
  */
 static int

Index: src/sys/kern/kern_rndq.c
diff -u src/sys/kern/kern_rndq.c:1.95 src/sys/kern/kern_rndq.c:1.96
--- src/sys/kern/kern_rndq.c:1.95	Sun Sep 29 12:07:52 2019
+++ src/sys/kern/kern_rndq.c	Thu Jan  2 15:42:27 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_rndq.c,v 1.95 2019/09/29 12:07:52 rhialto Exp $	*/
+/*	$NetBSD: kern_rndq.c,v 1.96 2020/01/02 15:42:27 thorpej Exp $	*/
 
 /*-
  * Copyright (c) 1997-2013 The NetBSD Foundation, Inc.
@@ -32,7 +32,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: kern_rndq.c,v 1.95 2019/09/29 12:07:52 rhialto Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_rndq.c,v 1.96 2020/01/02 15:42:27 thorpej Exp $");
 
 #include <sys/param.h>
 #include <sys/atomic.h>
@@ -1241,9 +1241,11 @@ rnd_extract_data(void *p, uint32_t len, 
 
 	mutex_spin_enter(&rnd_global.lock);
 	if (__predict_false(!timed_in)) {
-		if (boottime.tv_sec) {
-			rndpool_add_data(&rnd_global.pool, &boottime,
-			    sizeof(boottime), 0);
+		struct timespec tv;
+		getnanoboottime(&tv);
+		if (tv.tv_sec) {
+			rndpool_add_data(&rnd_global.pool, &tv,
+			    sizeof(tv), 0);
 		}
 		timed_in++;
 	}

Index: src/sys/kern/kern_tc.c
diff -u src/sys/kern/kern_tc.c:1.53 src/sys/kern/kern_tc.c:1.54
--- src/sys/kern/kern_tc.c:1.53	Fri Dec 27 09:25:58 2019
+++ src/sys/kern/kern_tc.c	Thu Jan  2 15:42:27 2020
@@ -1,4 +1,4 @@
-/* $NetBSD: kern_tc.c,v 1.53 2019/12/27 09:25:58 msaitoh Exp $ */
+/* $NetBSD: kern_tc.c,v 1.54 2020/01/02 15:42:27 thorpej Exp $ */
 
 /*-
  * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
@@ -40,7 +40,7 @@
 
 #include <sys/cdefs.h>
 /* __FBSDID("$FreeBSD: src/sys/kern/kern_tc.c,v 1.166 2005/09/19 22:16:31 andre Exp $"); */
-__KERNEL_RCSID(0, "$NetBSD: kern_tc.c,v 1.53 2019/12/27 09:25:58 msaitoh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_tc.c,v 1.54 2020/01/02 15:42:27 thorpej Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_ntp.h"
@@ -519,6 +519,35 @@ getmicrotime(struct timeval *tvp)
 	} while (gen == 0 || gen != th->th_generation);
 }
 
+void
+getnanoboottime(struct timespec *tsp)
+{
+	struct bintime bt;
+
+	getbinboottime(&bt);
+	bintime2timespec(&bt, tsp);
+}
+
+void
+getmicroboottime(struct timeval *tvp)
+{
+	struct bintime bt;
+
+	getbinboottime(&bt);
+	bintime2timeval(&bt, tvp);
+}
+
+void
+getbinboottime(struct bintime *bt)
+{
+
+	/*
+	 * XXX Need lockless read synchronization around timebasebin
+	 * (and not just here).
+	 */
+	*bt = timebasebin;
+}
+
 /*
  * Initialize a new timecounter and possibly use it.
  */

Index: src/sys/kern/kern_time.c
diff -u src/sys/kern/kern_time.c:1.202 src/sys/kern/kern_time.c:1.203
--- src/sys/kern/kern_time.c:1.202	Wed Jan  1 17:28:17 2020
+++ src/sys/kern/kern_time.c	Thu Jan  2 15:42:27 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_time.c,v 1.202 2020/01/01 17:28:17 thorpej Exp $	*/
+/*	$NetBSD: kern_time.c,v 1.203 2020/01/02 15:42:27 thorpej Exp $	*/
 
 /*-
  * Copyright (c) 2000, 2004, 2005, 2007, 2008, 2009 The NetBSD Foundation, Inc.
@@ -61,7 +61,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: kern_time.c,v 1.202 2020/01/01 17:28:17 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_time.c,v 1.203 2020/01/02 15:42:27 thorpej Exp $");
 
 #include <sys/param.h>
 #include <sys/resourcevar.h>
@@ -155,8 +155,6 @@ settime1(struct proc *p, const struct ti
 
 	tc_setclock(ts);
 
-	timespecadd(&boottime, &delta, &boottime);
-
 	resettodr();
 
 	return (0);

Index: src/sys/miscfs/fdesc/fdesc_vnops.c
diff -u src/sys/miscfs/fdesc/fdesc_vnops.c:1.130 src/sys/miscfs/fdesc/fdesc_vnops.c:1.131
--- src/sys/miscfs/fdesc/fdesc_vnops.c:1.130	Mon Sep  3 16:29:35 2018
+++ src/sys/miscfs/fdesc/fdesc_vnops.c	Thu Jan  2 15:42:27 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: fdesc_vnops.c,v 1.130 2018/09/03 16:29:35 riastradh Exp $	*/
+/*	$NetBSD: fdesc_vnops.c,v 1.131 2020/01/02 15:42:27 thorpej Exp $	*/
 
 /*
  * Copyright (c) 1992, 1993
@@ -41,13 +41,12 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: fdesc_vnops.c,v 1.130 2018/09/03 16:29:35 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: fdesc_vnops.c,v 1.131 2020/01/02 15:42:27 thorpej Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
 #include <sys/time.h>
 #include <sys/proc.h>
-#include <sys/kernel.h>	/* boottime */
 #include <sys/resourcevar.h>
 #include <sys/socketvar.h>
 #include <sys/filedesc.h>
@@ -412,6 +411,7 @@ fdesc_getattr(void *v)
 	struct vattr *vap = ap->a_vap;
 	unsigned fd;
 	int error = 0;
+	struct timeval tv;
 
 	switch (VTOFDESC(vp)->fd_type) {
 	case Froot:
@@ -454,7 +454,8 @@ fdesc_getattr(void *v)
 		vap->va_gid = 0;
 		vap->va_fsid = vp->v_mount->mnt_stat.f_fsidx.__fsid_val[0];
 		vap->va_blocksize = DEV_BSIZE;
-		vap->va_atime.tv_sec = boottime.tv_sec;
+		getmicroboottime(&tv);
+		vap->va_atime.tv_sec = tv.tv_sec;
 		vap->va_atime.tv_nsec = 0;
 		vap->va_mtime = vap->va_atime;
 		vap->va_ctime = vap->va_mtime;

Index: src/sys/miscfs/kernfs/kernfs.h
diff -u src/sys/miscfs/kernfs/kernfs.h:1.40 src/sys/miscfs/kernfs/kernfs.h:1.41
--- src/sys/miscfs/kernfs/kernfs.h:1.40	Sun Jul 20 13:58:04 2014
+++ src/sys/miscfs/kernfs/kernfs.h	Thu Jan  2 15:42:27 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: kernfs.h,v 1.40 2014/07/20 13:58:04 hannken Exp $	*/
+/*	$NetBSD: kernfs.h,v 1.41 2020/01/02 15:42:27 thorpej Exp $	*/
 
 /*
  * Copyright (c) 1992, 1993
@@ -48,10 +48,11 @@ typedef enum {
 	KFSkern,		/* the filesystem itself (.) */
 	KFSroot,		/* the filesystem root (..) */
 	KFSnull,		/* none aplicable */
-	KFStime,		/* boottime */
+	KFStime,		/* time */
+	KFSboottime,		/* boottime */
 	KFSint,			/* integer */
 	KFSstring,		/* string */
-	KFShostname,	/* hostname */
+	KFShostname,		/* hostname */
 	KFSavenrun,		/* loadavg */
 	KFSdevice,		/* device file (rootdev/rrootdev) */
 	KFSmsgbuf,		/* msgbuf */

Index: src/sys/miscfs/kernfs/kernfs_vnops.c
diff -u src/sys/miscfs/kernfs/kernfs_vnops.c:1.161 src/sys/miscfs/kernfs/kernfs_vnops.c:1.162
--- src/sys/miscfs/kernfs/kernfs_vnops.c:1.161	Thu Aug 29 06:43:13 2019
+++ src/sys/miscfs/kernfs/kernfs_vnops.c	Thu Jan  2 15:42:27 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: kernfs_vnops.c,v 1.161 2019/08/29 06:43:13 hannken Exp $	*/
+/*	$NetBSD: kernfs_vnops.c,v 1.162 2020/01/02 15:42:27 thorpej Exp $	*/
 
 /*
  * Copyright (c) 1992, 1993
@@ -39,7 +39,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: kernfs_vnops.c,v 1.161 2019/08/29 06:43:13 hannken Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kernfs_vnops.c,v 1.162 2020/01/02 15:42:27 thorpej Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -78,7 +78,7 @@ const struct kern_target kern_targets[] 
      /*        name            data          tag           type  ro/rw */
      { DT_DIR, N("."),         0,            KFSkern,        VDIR, DIR_MODE   },
      { DT_DIR, N(".."),        0,            KFSroot,        VDIR, DIR_MODE   },
-     { DT_REG, N("boottime"),  &boottime.tv_sec, KFSint,     VREG, READ_MODE  },
+     { DT_REG, N("boottime"),  0,            KFSboottime,    VREG, READ_MODE  },
 			/* XXXUNCONST */
      { DT_REG, N("copyright"), __UNCONST(copyright),
      					     KFSstring,      VREG, READ_MODE  },
@@ -362,6 +362,17 @@ kernfs_xread(struct kernfs_node *kfs, in
 		break;
 	}
 
+	case KFSboottime: {
+		struct timeval tv;
+
+		/*
+		 * Historically, /kern/boottime only contained seconds.
+		 */
+		getmicroboottime(&tv);
+		snprintf(*bufp, len, "%lld\n", (long long)tv.tv_sec);
+		break;
+	}
+
 	case KFSint: {
 		int *ip = kt->kt_data;
 
@@ -639,7 +650,7 @@ kernfs_getattr(void *v)
 	/* Make all times be current TOD, except for the "boottime" node. */
 	if (kfs->kfs_kt->kt_namlen == 8 &&
 	    !memcmp(kfs->kfs_kt->kt_name, "boottime", 8)) {
-		vap->va_ctime = boottime;
+		getnanoboottime(&vap->va_ctime);
 	} else {
 		getnanotime(&vap->va_ctime);
 	}
@@ -673,6 +684,7 @@ kernfs_getattr(void *v)
 
 	case KFSnull:
 	case KFStime:
+	case KFSboottime:
 	case KFSint:
 	case KFSstring:
 	case KFShostname:

Index: src/sys/miscfs/procfs/procfs_linux.c
diff -u src/sys/miscfs/procfs/procfs_linux.c:1.79 src/sys/miscfs/procfs/procfs_linux.c:1.80
--- src/sys/miscfs/procfs/procfs_linux.c:1.79	Tue Dec 31 13:07:13 2019
+++ src/sys/miscfs/procfs/procfs_linux.c	Thu Jan  2 15:42:27 2020
@@ -1,4 +1,4 @@
-/*      $NetBSD: procfs_linux.c,v 1.79 2019/12/31 13:07:13 ad Exp $      */
+/*      $NetBSD: procfs_linux.c,v 1.80 2020/01/02 15:42:27 thorpej Exp $      */
 
 /*
  * Copyright (c) 2001 Wasabi Systems, Inc.
@@ -36,7 +36,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: procfs_linux.c,v 1.79 2019/12/31 13:07:13 ad Exp $");
+__KERNEL_RCSID(0, "$NetBSD: procfs_linux.c,v 1.80 2020/01/02 15:42:27 thorpej Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -297,6 +297,9 @@ procfs_docpustat(struct lwp *curl, struc
 
 	cpu_count_sync_all();
 
+	struct timeval btv;
+	getmicroboottime(&btv);
+
 	len += snprintf(&bf[len], LBFSZ - len,
 			"disk 0 0 0 0\n"
 			"page %u %u\n"
@@ -308,7 +311,7 @@ procfs_docpustat(struct lwp *curl, struc
 			uvmexp.pgswapin, uvmexp.pgswapout,
 			cpu_count_get(CPU_COUNT_NINTR),
 			cpu_count_get(CPU_COUNT_NSWTCH),
-			boottime.tv_sec);
+			btv.tv_sec);
 	if (len >= LBFSZ)
 		goto out;
 

Index: src/sys/nfs/nfs_serv.c
diff -u src/sys/nfs/nfs_serv.c:1.177 src/sys/nfs/nfs_serv.c:1.178
--- src/sys/nfs/nfs_serv.c:1.177	Wed Feb 20 10:05:20 2019
+++ src/sys/nfs/nfs_serv.c	Thu Jan  2 15:42:27 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: nfs_serv.c,v 1.177 2019/02/20 10:05:20 hannken Exp $	*/
+/*	$NetBSD: nfs_serv.c,v 1.178 2020/01/02 15:42:27 thorpej Exp $	*/
 
 /*
  * Copyright (c) 1989, 1993
@@ -55,7 +55,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: nfs_serv.c,v 1.177 2019/02/20 10:05:20 hannken Exp $");
+__KERNEL_RCSID(0, "$NetBSD: nfs_serv.c,v 1.178 2020/01/02 15:42:27 thorpej Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -1003,8 +1003,10 @@ nfsrv_write(struct nfsrv_descript *nfsd,
 		 * but it may make the values more human readable,
 		 * for debugging purposes.
 		 */
-		*tl++ = txdr_unsigned(boottime.tv_sec);
-		*tl = txdr_unsigned(boottime.tv_nsec / 1000);
+		struct timeval btv;
+		getmicroboottime(&btv);
+		*tl++ = txdr_unsigned(btv.tv_sec);
+		*tl = txdr_unsigned(btv.tv_usec);
 	} else {
 		nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
 		nfsm_srvfillattr(&va, fp);
@@ -1304,8 +1306,10 @@ loop1:
 			     * but it may make the values more human readable,
 			     * for debugging purposes.
 			     */
-			    *tl++ = txdr_unsigned(boottime.tv_sec);
-			    *tl = txdr_unsigned(boottime.tv_nsec / 1000);
+			    struct timeval btv;
+			    getmicroboottime(&btv);
+			    *tl++ = txdr_unsigned(btv.tv_sec);
+			    *tl = txdr_unsigned(btv.tv_usec);
 			} else {
 			    nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
 			    nfsm_srvfillattr(&va, fp);
@@ -3319,8 +3323,10 @@ nfsrv_commit(struct nfsrv_descript *nfsd
 	nfsm_srvwcc_data(for_ret, &bfor, aft_ret, &aft);
 	if (!error) {
 		nfsm_build(tl, u_int32_t *, NFSX_V3WRITEVERF);
-		*tl++ = txdr_unsigned(boottime.tv_sec);
-		*tl = txdr_unsigned(boottime.tv_nsec / 1000);
+		struct timeval btv;
+		getmicroboottime(&btv);
+		*tl++ = txdr_unsigned(btv.tv_sec);
+		*tl = txdr_unsigned(btv.tv_usec);
 	} else {
 		return (0);
 	}

Index: src/sys/rump/librump/rumpkern/cons.c
diff -u src/sys/rump/librump/rumpkern/cons.c:1.8 src/sys/rump/librump/rumpkern/cons.c:1.9
--- src/sys/rump/librump/rumpkern/cons.c:1.8	Mon Sep  3 16:29:37 2018
+++ src/sys/rump/librump/rumpkern/cons.c	Thu Jan  2 15:42:27 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: cons.c,v 1.8 2018/09/03 16:29:37 riastradh Exp $	*/
+/*	$NetBSD: cons.c,v 1.9 2020/01/02 15:42:27 thorpej Exp $	*/
 
 /*
  * Copyright (c) 2013 Antti Kantee.  All Rights Reserved.
@@ -36,7 +36,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: cons.c,v 1.8 2018/09/03 16:29:37 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: cons.c,v 1.9 2020/01/02 15:42:27 thorpej Exp $");
 
 #include <sys/param.h>
 #include <sys/file.h>
@@ -137,11 +137,14 @@ rumpcons_ioctl(struct file *fp, u_long c
 static int
 rumpcons_stat(struct file *fp, struct stat *sb)
 {
+	struct timespec ts;
+
+	getnanoboottime(&ts);
 
 	memset(sb, 0, sizeof(*sb));
 	sb->st_mode = 0600 | _S_IFCHR;
-	sb->st_atimespec = sb->st_mtimespec = sb->st_ctimespec = boottime;
-	sb->st_birthtimespec = boottime;
+	sb->st_atimespec = sb->st_mtimespec = sb->st_ctimespec = ts;
+	sb->st_birthtimespec = ts;
 
 	return 0;
 }

Index: src/sys/rump/librump/rumpkern/emul.c
diff -u src/sys/rump/librump/rumpkern/emul.c:1.194 src/sys/rump/librump/rumpkern/emul.c:1.195
--- src/sys/rump/librump/rumpkern/emul.c:1.194	Thu Jan  2 08:49:10 2020
+++ src/sys/rump/librump/rumpkern/emul.c	Thu Jan  2 15:42:27 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: emul.c,v 1.194 2020/01/02 08:49:10 martin Exp $	*/
+/*	$NetBSD: emul.c,v 1.195 2020/01/02 15:42:27 thorpej Exp $	*/
 
 /*
  * Copyright (c) 2007-2011 Antti Kantee.  All Rights Reserved.
@@ -26,7 +26,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: emul.c,v 1.194 2020/01/02 08:49:10 martin Exp $");
+__KERNEL_RCSID(0, "$NetBSD: emul.c,v 1.195 2020/01/02 15:42:27 thorpej Exp $");
 
 #include <sys/param.h>
 #include <sys/cprng.h>
@@ -64,9 +64,8 @@ struct vnode *rootvp;
 dev_t rootdev = NODEV;
 
 const int schedppq = 1;
-int	shutting_down __read_mostly;	/* system is shutting down */
-struct timespec boottime;
 int cold = 1;
+int shutting_down;
 int boothowto = AB_SILENT;
 struct tty *constty;
 

Index: src/sys/rump/librump/rumpkern/rump.c
diff -u src/sys/rump/librump/rumpkern/rump.c:1.338 src/sys/rump/librump/rumpkern/rump.c:1.339
--- src/sys/rump/librump/rumpkern/rump.c:1.338	Sun Dec 15 14:21:34 2019
+++ src/sys/rump/librump/rumpkern/rump.c	Thu Jan  2 15:42:27 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: rump.c,v 1.338 2019/12/15 14:21:34 pgoyette Exp $	*/
+/*	$NetBSD: rump.c,v 1.339 2020/01/02 15:42:27 thorpej Exp $	*/
 
 /*
  * Copyright (c) 2007-2011 Antti Kantee.  All Rights Reserved.
@@ -26,7 +26,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: rump.c,v 1.338 2019/12/15 14:21:34 pgoyette Exp $");
+__KERNEL_RCSID(0, "$NetBSD: rump.c,v 1.339 2020/01/02 15:42:27 thorpej Exp $");
 
 #include <sys/systm.h>
 #define ELFSIZE ARCH_ELFSIZE
@@ -229,7 +229,7 @@ int
 rump_init(void)
 {
 	char buf[256];
-	struct timespec ts;
+	struct timespec bts;
 	int64_t sec;
 	long nsec;
 	struct lwp *l, *initlwp;
@@ -276,8 +276,8 @@ rump_init(void)
 	rump_cpus_bootstrap(&numcpu);
 
 	rumpuser_clock_gettime(RUMPUSER_CLOCK_RELWALL, &sec, &nsec);
-	boottime.tv_sec = sec;
-	boottime.tv_nsec = nsec;
+	bts.tv_sec = sec;
+	bts.tv_nsec = nsec;
 
 	initmsgbuf(rump_msgbuf, sizeof(rump_msgbuf));
 	aprint_verbose("%s%s", copyright, version);
@@ -369,8 +369,7 @@ rump_init(void)
 	ktrinit();
 #endif
 
-	ts = boottime;
-	tc_setclock(&ts);
+	tc_setclock(&bts);
 
 	extern krwlock_t exec_lock;
 	rw_init(&exec_lock);

Index: src/sys/sys/kernel.h
diff -u src/sys/sys/kernel.h:1.31 src/sys/sys/kernel.h:1.32
--- src/sys/sys/kernel.h:1.31	Wed Jan  1 22:57:17 2020
+++ src/sys/sys/kernel.h	Thu Jan  2 15:42:27 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: kernel.h,v 1.31 2020/01/01 22:57:17 thorpej Exp $	*/
+/*	$NetBSD: kernel.h,v 1.32 2020/01/02 15:42:27 thorpej Exp $	*/
 
 /*-
  * Copyright (c) 1990, 1993
@@ -50,8 +50,6 @@ extern int hostnamelen;
 extern char domainname[MAXHOSTNAMELEN];
 extern int domainnamelen;
 
-extern struct timespec boottime;
-
 extern int rtc_offset;		/* offset of rtc from UTC in minutes */
 
 extern int cold;		/* still working on startup */

Index: src/sys/sys/timevar.h
diff -u src/sys/sys/timevar.h:1.39 src/sys/sys/timevar.h:1.40
--- src/sys/sys/timevar.h:1.39	Tue Aug  6 15:47:55 2019
+++ src/sys/sys/timevar.h	Thu Jan  2 15:42:27 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: timevar.h,v 1.39 2019/08/06 15:47:55 riastradh Exp $	*/
+/*	$NetBSD: timevar.h,v 1.40 2020/01/02 15:42:27 thorpej Exp $	*/
 
 /*
  *  Copyright (c) 2005, 2008 The NetBSD Foundation.
@@ -146,6 +146,10 @@ void	getbintime(struct bintime *);
 void	getnanotime(struct timespec *);
 void	getmicrotime(struct timeval *);
 
+void	getbinboottime(struct bintime *);
+void	getnanoboottime(struct timespec *);
+void	getmicroboottime(struct timeval *);
+
 /* Other functions */
 int	ts2timo(clockid_t, int, struct timespec *, int *, struct timespec *);
 void	adjtime1(const struct timeval *, struct timeval *, struct proc *);

Reply via email to