Author: kib
Date: Sat Jun 23 09:33:06 2012
New Revision: 237474
URL: http://svn.freebsd.org/changeset/base/237474

Log:
  Stop updating the struct vdso_timehands from even handler executed in
  the scheduled task from tc_windup(). Do it directly from tc_windup in
  interrupt context [1].
  
  Establish the permanent mapping of the shared page into the kernel
  address space, avoiding the potential need to sleep waiting for
  allocation of sf buffer during vdso_timehands update. As a
  consequence, shared_page_write_start() and shared_page_write_end()
  functions are not needed anymore.
  
  Guess and memorize the pointers to native host and compat32 sysentvec
  during initialization, to avoid the need to get shared_page_alloc_sx
  lock during the update.
  
  In tc_fill_vdso_timehands(), do not loop waiting for timehands
  generation to stabilize, since vdso_timehands is written in the same
  interrupt context which wrote timehands.
  
  Requested by:   mav [1]
  MFC after:      29 days

Modified:
  head/sys/kern/kern_exec.c
  head/sys/kern/kern_tc.c
  head/sys/sys/sysent.h
  head/sys/sys/vdso.h

Modified: head/sys/kern/kern_exec.c
==============================================================================
--- head/sys/kern/kern_exec.c   Sat Jun 23 08:36:49 2012        (r237473)
+++ head/sys/kern/kern_exec.c   Sat Jun 23 09:33:06 2012        (r237474)
@@ -1517,42 +1517,13 @@ exec_unregister(execsw_arg)
 static struct sx shared_page_alloc_sx;
 static vm_object_t shared_page_obj;
 static int shared_page_free;
-
-struct sf_buf *
-shared_page_write_start(int base)
-{
-       vm_page_t m;
-       struct sf_buf *s;
-
-       VM_OBJECT_LOCK(shared_page_obj);
-       m = vm_page_grab(shared_page_obj, OFF_TO_IDX(base), VM_ALLOC_RETRY);
-       VM_OBJECT_UNLOCK(shared_page_obj);
-       s = sf_buf_alloc(m, SFB_DEFAULT);
-       return (s);
-}
-
-void
-shared_page_write_end(struct sf_buf *sf)
-{
-       vm_page_t m;
-
-       m = sf_buf_page(sf);
-       sf_buf_free(sf);
-       VM_OBJECT_LOCK(shared_page_obj);
-       vm_page_wakeup(m);
-       VM_OBJECT_UNLOCK(shared_page_obj);
-}
+char *shared_page_mapping;
 
 void
 shared_page_write(int base, int size, const void *data)
 {
-       struct sf_buf *sf;
-       vm_offset_t sk;
 
-       sf = shared_page_write_start(base);
-       sk = sf_buf_kva(sf);
-       bcopy(data, (void *)(sk + (base & PAGE_MASK)), size);
-       shared_page_write_end(sf);
+       bcopy(data, shared_page_mapping + base, size);
 }
 
 static int
@@ -1596,6 +1567,7 @@ static void
 shared_page_init(void *dummy __unused)
 {
        vm_page_t m;
+       vm_offset_t addr;
 
        sx_init(&shared_page_alloc_sx, "shpsx");
        shared_page_obj = vm_pager_allocate(OBJT_PHYS, 0, PAGE_SIZE,
@@ -1605,25 +1577,24 @@ shared_page_init(void *dummy __unused)
            VM_ALLOC_ZERO);
        m->valid = VM_PAGE_BITS_ALL;
        VM_OBJECT_UNLOCK(shared_page_obj);
+       addr = kmem_alloc_nofault(kernel_map, PAGE_SIZE);
+       pmap_qenter(addr, &m, 1);
+       shared_page_mapping = (char *)addr;
 }
 
 SYSINIT(shp, SI_SUB_EXEC, SI_ORDER_FIRST, (sysinit_cfunc_t)shared_page_init,
     NULL);
 
 static void
-timehands_update(void *arg)
+timehands_update(struct sysentvec *sv)
 {
-       struct sysentvec *sv;
-       struct sf_buf *sf;
        struct vdso_timehands th;
        struct vdso_timekeep *tk;
        uint32_t enabled, idx;
 
-       sv = arg;
-       sx_xlock(&shared_page_alloc_sx);
        enabled = tc_fill_vdso_timehands(&th);
-       sf = shared_page_write_start(sv->sv_timekeep_off);
-       tk = (void *)(sf_buf_kva(sf) + (sv->sv_timekeep_off & PAGE_MASK));
+       tk = (struct vdso_timekeep *)(shared_page_mapping +
+           sv->sv_timekeep_off);
        idx = sv->sv_timekeep_curr;
        atomic_store_rel_32(&tk->tk_th[idx].th_gen, 0);
        if (++idx >= VDSO_TH_NUM)
@@ -1637,25 +1608,19 @@ timehands_update(void *arg)
        tk->tk_enabled = enabled;
        atomic_store_rel_32(&tk->tk_th[idx].th_gen, sv->sv_timekeep_gen);
        tk->tk_current = idx;
-       shared_page_write_end(sf);
-       sx_xunlock(&shared_page_alloc_sx);
 }
 
 #ifdef COMPAT_FREEBSD32
 static void
-timehands_update32(void *arg)
+timehands_update32(struct sysentvec *sv)
 {
-       struct sysentvec *sv;
-       struct sf_buf *sf;
        struct vdso_timekeep32 *tk;
        struct vdso_timehands32 th;
        uint32_t enabled, idx;
 
-       sv = arg;
-       sx_xlock(&shared_page_alloc_sx);
        enabled = tc_fill_vdso_timehands32(&th);
-       sf = shared_page_write_start(sv->sv_timekeep_off);
-       tk = (void *)(sf_buf_kva(sf) + (sv->sv_timekeep_off & PAGE_MASK));
+       tk = (struct vdso_timekeep32 *)(shared_page_mapping +
+           sv->sv_timekeep_off);
        idx = sv->sv_timekeep_curr;
        atomic_store_rel_32(&tk->tk_th[idx].th_gen, 0);
        if (++idx >= VDSO_TH_NUM)
@@ -1669,11 +1634,32 @@ timehands_update32(void *arg)
        tk->tk_enabled = enabled;
        atomic_store_rel_32(&tk->tk_th[idx].th_gen, sv->sv_timekeep_gen);
        tk->tk_current = idx;
-       shared_page_write_end(sf);
-       sx_xunlock(&shared_page_alloc_sx);
 }
 #endif
 
+/*
+ * This is hackish, but easiest way to avoid creating list structures
+ * that needs to be iterated over from the hardclock interrupt
+ * context.
+ */
+static struct sysentvec *host_sysentvec;
+#ifdef COMPAT_FREEBSD32
+static struct sysentvec *compat32_sysentvec;
+#endif
+
+void
+timekeep_push_vdso(void)
+{
+
+       if (host_sysentvec != NULL && host_sysentvec->sv_timekeep_base != 0)
+               timehands_update(host_sysentvec);
+#ifdef COMPAT_FREEBSD32
+       if (compat32_sysentvec != NULL &&
+           compat32_sysentvec->sv_timekeep_base != 0)
+               timehands_update32(compat32_sysentvec);
+#endif
+}
+
 void
 exec_sysvec_init(void *param)
 {
@@ -1688,29 +1674,32 @@ exec_sysvec_init(void *param)
        sv->sv_shared_page_obj = shared_page_obj;
        sv->sv_sigcode_base = sv->sv_shared_page_base +
            shared_page_fill(*(sv->sv_szsigcode), 16, sv->sv_sigcode);
+       if ((sv->sv_flags & SV_ABI_MASK) != SV_ABI_FREEBSD)
+               return;
        tk_ver = VDSO_TK_VER_CURR;
 #ifdef COMPAT_FREEBSD32
        if ((sv->sv_flags & SV_ILP32) != 0) {
                tk_base = shared_page_alloc(sizeof(struct vdso_timekeep32) +
                    sizeof(struct vdso_timehands32) * VDSO_TH_NUM, 16);
                KASSERT(tk_base != -1, ("tk_base -1 for 32bit"));
-               EVENTHANDLER_REGISTER(tc_windup, timehands_update32, sv,
-                   EVENTHANDLER_PRI_ANY);
                shared_page_write(tk_base + offsetof(struct vdso_timekeep32,
                    tk_ver), sizeof(uint32_t), &tk_ver);
+               KASSERT(compat32_sysentvec == 0,
+                   ("Native compat32 already registered"));
+               compat32_sysentvec = sv;
        } else {
 #endif
                tk_base = shared_page_alloc(sizeof(struct vdso_timekeep) +
                    sizeof(struct vdso_timehands) * VDSO_TH_NUM, 16);
                KASSERT(tk_base != -1, ("tk_base -1 for native"));
-               EVENTHANDLER_REGISTER(tc_windup, timehands_update, sv,
-                   EVENTHANDLER_PRI_ANY);
                shared_page_write(tk_base + offsetof(struct vdso_timekeep,
                    tk_ver), sizeof(uint32_t), &tk_ver);
+               KASSERT(host_sysentvec == 0, ("Native already registered"));
+               host_sysentvec = sv;
 #ifdef COMPAT_FREEBSD32
        }
 #endif
        sv->sv_timekeep_base = sv->sv_shared_page_base + tk_base;
        sv->sv_timekeep_off = tk_base;
-       EVENTHANDLER_INVOKE(tc_windup);
+       timekeep_push_vdso();
 }

Modified: head/sys/kern/kern_tc.c
==============================================================================
--- head/sys/kern/kern_tc.c     Sat Jun 23 08:36:49 2012        (r237473)
+++ head/sys/kern/kern_tc.c     Sat Jun 23 09:33:06 2012        (r237474)
@@ -31,7 +31,6 @@ __FBSDID("$FreeBSD$");
 #include <sys/systm.h>
 #include <sys/timeffc.h>
 #include <sys/timepps.h>
-#include <sys/taskqueue.h>
 #include <sys/timetc.h>
 #include <sys/timex.h>
 #include <sys/vdso.h>
@@ -121,12 +120,8 @@ SYSCTL_INT(_kern_timecounter, OID_AUTO, 
     &timestepwarnings, 0, "Log time steps");
 
 static void tc_windup(void);
-static void tc_windup_push_vdso(void *ctx, int pending);
 static void cpu_tick_calibrate(int);
 
-static struct task tc_windup_push_vdso_task = TASK_INITIALIZER(0,
-    tc_windup_push_vdso,  0);
-
 static int
 sysctl_kern_boottime(SYSCTL_HANDLER_ARGS)
 {
@@ -1367,7 +1362,7 @@ tc_windup(void)
 #endif
 
        timehands = th;
-       taskqueue_enqueue_fast(taskqueue_fast, &tc_windup_push_vdso_task);
+       timekeep_push_vdso();
 }
 
 /* Report or change the active timecounter hardware. */
@@ -1394,7 +1389,7 @@ sysctl_kern_timecounter_hardware(SYSCTL_
                (void)newtc->tc_get_timecount(newtc);
 
                timecounter = newtc;
-               EVENTHANDLER_INVOKE(tc_windup);
+               timekeep_push_vdso();
                return (0);
        }
        return (EINVAL);
@@ -1865,7 +1860,7 @@ sysctl_fast_gettime(SYSCTL_HANDLER_ARGS)
        if (error != 0)
                return (error);
        vdso_th_enable = old_vdso_th_enable;
-       EVENTHANDLER_INVOKE(tc_windup);
+       timekeep_push_vdso();
        return (0);
 }
 SYSCTL_PROC(_kern_timecounter, OID_AUTO, fast_gettime,
@@ -1877,19 +1872,15 @@ tc_fill_vdso_timehands(struct vdso_timeh
 {
        struct timehands *th;
        uint32_t enabled;
-       int gen;
 
-       do {
-               th = timehands;
-               gen = th->th_generation;
-               vdso_th->th_algo = VDSO_TH_ALGO_1;
-               vdso_th->th_scale = th->th_scale;
-               vdso_th->th_offset_count = th->th_offset_count;
-               vdso_th->th_counter_mask = th->th_counter->tc_counter_mask;
-               vdso_th->th_offset = th->th_offset;
-               vdso_th->th_boottime = boottimebin;
-               enabled = cpu_fill_vdso_timehands(vdso_th);
-       } while (gen == 0 || timehands->th_generation != gen);
+       th = timehands;
+       vdso_th->th_algo = VDSO_TH_ALGO_1;
+       vdso_th->th_scale = th->th_scale;
+       vdso_th->th_offset_count = th->th_offset_count;
+       vdso_th->th_counter_mask = th->th_counter->tc_counter_mask;
+       vdso_th->th_offset = th->th_offset;
+       vdso_th->th_boottime = boottimebin;
+       enabled = cpu_fill_vdso_timehands(vdso_th);
        if (!vdso_th_enable)
                enabled = 0;
        return (enabled);
@@ -1901,30 +1892,19 @@ tc_fill_vdso_timehands32(struct vdso_tim
 {
        struct timehands *th;
        uint32_t enabled;
-       int gen;
 
-       do {
-               th = timehands;
-               gen = th->th_generation;
-               vdso_th32->th_algo = VDSO_TH_ALGO_1;
-               *(uint64_t *)&vdso_th32->th_scale[0] = th->th_scale;
-               vdso_th32->th_offset_count = th->th_offset_count;
-               vdso_th32->th_counter_mask = th->th_counter->tc_counter_mask;
-               vdso_th32->th_offset.sec = th->th_offset.sec;
-               *(uint64_t *)&vdso_th32->th_offset.frac[0] = th->th_offset.frac;
-               vdso_th32->th_boottime.sec = boottimebin.sec;
-               *(uint64_t *)&vdso_th32->th_boottime.frac[0] = boottimebin.frac;
-               enabled = cpu_fill_vdso_timehands32(vdso_th32);
-       } while (gen == 0 || timehands->th_generation != gen);
+       th = timehands;
+       vdso_th32->th_algo = VDSO_TH_ALGO_1;
+       *(uint64_t *)&vdso_th32->th_scale[0] = th->th_scale;
+       vdso_th32->th_offset_count = th->th_offset_count;
+       vdso_th32->th_counter_mask = th->th_counter->tc_counter_mask;
+       vdso_th32->th_offset.sec = th->th_offset.sec;
+       *(uint64_t *)&vdso_th32->th_offset.frac[0] = th->th_offset.frac;
+       vdso_th32->th_boottime.sec = boottimebin.sec;
+       *(uint64_t *)&vdso_th32->th_boottime.frac[0] = boottimebin.frac;
+       enabled = cpu_fill_vdso_timehands32(vdso_th32);
        if (!vdso_th_enable)
                enabled = 0;
        return (enabled);
 }
 #endif
-
-static void
-tc_windup_push_vdso(void *ctx, int pending)
-{
-
-       EVENTHANDLER_INVOKE(tc_windup);
-}

Modified: head/sys/sys/sysent.h
==============================================================================
--- head/sys/sys/sysent.h       Sat Jun 23 08:36:49 2012        (r237473)
+++ head/sys/sys/sysent.h       Sat Jun 23 09:33:06 2012        (r237474)
@@ -265,8 +265,6 @@ int shared_page_alloc(int size, int alig
 int shared_page_fill(int size, int align, const void *data);
 void shared_page_write(int base, int size, const void *data);
 void exec_sysvec_init(void *param);
-struct sf_buf *shared_page_write_start(int base);
-void shared_page_write_end(struct sf_buf *sf);
 
 #define INIT_SYSENTVEC(name, sv)                                       \
     SYSINIT(name, SI_SUB_EXEC, SI_ORDER_ANY,                           \

Modified: head/sys/sys/vdso.h
==============================================================================
--- head/sys/sys/vdso.h Sat Jun 23 08:36:49 2012        (r237473)
+++ head/sys/sys/vdso.h Sat Jun 23 09:33:06 2012        (r237474)
@@ -29,7 +29,6 @@
 #define        _SYS_VDSO_H
 
 #include <sys/types.h>
-#include <sys/eventhandler.h>
 #include <machine/vdso.h>
 
 struct vdso_timehands {
@@ -74,6 +73,8 @@ u_int __vdso_gettc(const struct vdso_tim
 
 #ifdef _KERNEL
 
+void timekeep_push_vdso(void);
+
 uint32_t tc_fill_vdso_timehands(struct vdso_timehands *vdso_th);
 
 /*
@@ -86,9 +87,6 @@ uint32_t tc_fill_vdso_timehands(struct v
  */
 uint32_t cpu_fill_vdso_timehands(struct vdso_timehands *vdso_th);
 
-typedef void (*tc_windup_fn)(void *);
-EVENTHANDLER_DECLARE(tc_windup, tc_windup_fn);
-
 #define        VDSO_TH_NUM     4
 
 #ifdef COMPAT_FREEBSD32
_______________________________________________
svn-src-head@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/svn-src-head
To unsubscribe, send any mail to "svn-src-head-unsubscr...@freebsd.org"

Reply via email to