Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/sys - Eliminate the global "boottime" variable, which wa...



details:   https://anonhg.NetBSD.org/src/rev/26942812521b
branches:  trunk
changeset: 1006125:26942812521b
user:      thorpej <thorpej%NetBSD.org@localhost>
date:      Thu Jan 02 15:42:26 2020 +0000

description:
- 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.

diffstat:

 sys/compat/common/kern_info_43.c            |   6 ++--
 sys/compat/common/kern_time_50.c            |   6 ++--
 sys/compat/netbsd32/netbsd32_sysctl.c       |   8 ++++--
 sys/external/bsd/drm2/include/linux/ktime.h |  12 +++++++--
 sys/fs/nfs/common/nfs_lock.c                |  10 ++++----
 sys/kern/init_main.c                        |  15 +-----------
 sys/kern/init_sysctl.c                      |  22 ++++++++++++++++--
 sys/kern/kern_rndq.c                        |  12 ++++++----
 sys/kern/kern_tc.c                          |  33 +++++++++++++++++++++++++++-
 sys/kern/kern_time.c                        |   6 +---
 sys/miscfs/fdesc/fdesc_vnops.c              |   9 ++++---
 sys/miscfs/kernfs/kernfs.h                  |   7 +++--
 sys/miscfs/kernfs/kernfs_vnops.c            |  20 ++++++++++++++---
 sys/miscfs/procfs/procfs_linux.c            |   9 +++++--
 sys/nfs/nfs_serv.c                          |  22 ++++++++++++-------
 sys/rump/librump/rumpkern/cons.c            |  11 ++++++---
 sys/rump/librump/rumpkern/emul.c            |   7 ++---
 sys/rump/librump/rumpkern/rump.c            |  13 +++++------
 sys/sys/kernel.h                            |   4 +--
 sys/sys/timevar.h                           |   6 ++++-
 20 files changed, 153 insertions(+), 85 deletions(-)

diffs (truncated from 740 to 300 lines):

diff -r c421863ab434 -r 26942812521b sys/compat/common/kern_info_43.c
--- a/sys/compat/common/kern_info_43.c  Thu Jan 02 14:33:55 2020 +0000
+++ b/sys/compat/common/kern_info_43.c  Thu Jan 02 15:42:26 2020 +0000
@@ -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 @@
                        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);
 
diff -r c421863ab434 -r 26942812521b sys/compat/common/kern_time_50.c
--- a/sys/compat/common/kern_time_50.c  Thu Jan 02 14:33:55 2020 +0000
+++ b/sys/compat/common/kern_time_50.c  Thu Jan 02 15:42:26 2020 +0000
@@ -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 @@
 {
        struct timeval tv;
 
-       TIMESPEC_TO_TIMEVAL(&tv, &boottime);
+       getmicroboottime(&tv);
        timeval_to_timeval50(&tv, &boottime50);
 
        sysctl_createv(clog, 0, NULL, NULL,
diff -r c421863ab434 -r 26942812521b sys/compat/netbsd32/netbsd32_sysctl.c
--- a/sys/compat/netbsd32/netbsd32_sysctl.c     Thu Jan 02 14:33:55 2020 +0000
+++ b/sys/compat/netbsd32/netbsd32_sysctl.c     Thu Jan 02 15:42:26 2020 +0000
@@ -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 @@
 {
        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;
diff -r c421863ab434 -r 26942812521b sys/external/bsd/drm2/include/linux/ktime.h
--- a/sys/external/bsd/drm2/include/linux/ktime.h       Thu Jan 02 14:33:55 2020 +0000
+++ b/sys/external/bsd/drm2/include/linux/ktime.h       Thu Jan 02 15:42:26 2020 +0000
@@ -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 @@
 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);
 }
diff -r c421863ab434 -r 26942812521b sys/fs/nfs/common/nfs_lock.c
--- a/sys/fs/nfs/common/nfs_lock.c      Thu Jan 02 14:33:55 2020 +0000
+++ b/sys/fs/nfs/common/nfs_lock.c      Thu Jan 02 15:42:26 2020 +0000
@@ -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 @@
        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 @@
                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);
diff -r c421863ab434 -r 26942812521b sys/kern/init_main.c
--- a/sys/kern/init_main.c      Thu Jan 02 14:33:55 2020 +0000
+++ b/sys/kern/init_main.c      Thu Jan 02 15:42:26 2020 +0000
@@ -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 @@
 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 @@
         * 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) {
diff -r c421863ab434 -r 26942812521b sys/kern/init_sysctl.c
--- a/sys/kern/init_sysctl.c    Thu Jan 02 14:33:55 2020 +0000
+++ b/sys/kern/init_sysctl.c    Thu Jan 02 15:42:26 2020 +0000
@@ -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 @@
 
 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 @@
                       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 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
diff -r c421863ab434 -r 26942812521b sys/kern/kern_rndq.c
--- a/sys/kern/kern_rndq.c      Thu Jan 02 14:33:55 2020 +0000
+++ b/sys/kern/kern_rndq.c      Thu Jan 02 15:42:26 2020 +0000
@@ -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 @@
 
        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++;
        }
diff -r c421863ab434 -r 26942812521b sys/kern/kern_tc.c
--- a/sys/kern/kern_tc.c        Thu Jan 02 14:33:55 2020 +0000
+++ b/sys/kern/kern_tc.c        Thu Jan 02 15:42:26 2020 +0000
@@ -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>



Home | Main Index | Thread Index | Old Index