Source-Changes-HG archive

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

[src/netbsd-8]: src/sys Pull up following revision(s) (requested by ozaki-r i...



details:   https://anonhg.NetBSD.org/src/rev/cb8777dc89c8
branches:  netbsd-8
changeset: 434454:cb8777dc89c8
user:      martin <martin%NetBSD.org@localhost>
date:      Thu Nov 30 14:31:04 2017 +0000

description:
Pull up following revision(s) (requested by ozaki-r in ticket #404):
        sys/sys/localcount.h: revision 1.5
        sys/kern/subr_localcount.c: revision 1.7
Implement a debugging facility (overflow/underflow detection) for localcount
We cannot get an accurate count from a localcount instance because it consists
of per-cpu counters and we have no way to sum them up atomically. So we cannot
detect counter overflow/underflow as we can do on a normal refcount.
The facility adds an atomic counter to each localcount instance to enable the
validations. The counter ups and downs in synchronization with the per-CPU
counters. The counter is used iff both DEBUG and LOCKDEBUG are enabled in the
kernel.
Discussed on tech-kern@

diffstat:

 sys/kern/subr_localcount.c |  32 ++++++++++++++++++++++++++++++--
 sys/sys/localcount.h       |  10 +++++++---
 2 files changed, 37 insertions(+), 5 deletions(-)

diffs (95 lines):

diff -r 8a051ceccffb -r cb8777dc89c8 sys/kern/subr_localcount.c
--- a/sys/kern/subr_localcount.c        Thu Nov 30 14:23:12 2017 +0000
+++ b/sys/kern/subr_localcount.c        Thu Nov 30 14:31:04 2017 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: subr_localcount.c,v 1.4 2017/06/02 00:32:12 chs Exp $  */
+/*     $NetBSD: subr_localcount.c,v 1.4.2.1 2017/11/30 14:31:04 martin Exp $   */
 
 /*-
  * Copyright (c) 2016 The NetBSD Foundation, Inc.
@@ -44,7 +44,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: subr_localcount.c,v 1.4 2017/06/02 00:32:12 chs Exp $");
+__KERNEL_RCSID(0, "$NetBSD: subr_localcount.c,v 1.4.2.1 2017/11/30 14:31:04 martin Exp $");
 
 #include <sys/param.h>
 #include <sys/localcount.h>
@@ -54,6 +54,9 @@
 #include <sys/mutex.h>
 #include <sys/percpu.h>
 #include <sys/xcall.h>
+#if defined(DEBUG) && defined(LOCKDEBUG)
+#include <sys/atomic.h>
+#endif
 
 /*
  * localcount_init(lc)
@@ -203,6 +206,10 @@
 
        KASSERT(lc->lc_totalp == NULL);
        localcount_adjust(lc, +1);
+#if defined(DEBUG) && defined(LOCKDEBUG)
+       if (atomic_inc_32_nv(&lc->lc_refcnt) == 0)
+               panic("counter overflow");
+#endif
 }
 
 /*
@@ -247,5 +254,26 @@
        }
 
        localcount_adjust(lc, -1);
+#if defined(DEBUG) && defined(LOCKDEBUG)
+       if (atomic_dec_32_nv(&lc->lc_refcnt) == UINT_MAX)
+               panic("counter underflow");
+#endif
  out:  kpreempt_enable();
 }
+
+/*
+ * localcount_debug_refcnt(lc)
+ *
+ *     Return a total reference count of lc.  It returns a correct value
+ *     only if DEBUG and LOCKDEBUG enabled.  Otherwise always return 0.
+ */
+uint32_t
+localcount_debug_refcnt(const struct localcount *lc)
+{
+
+#if defined(DEBUG) && defined(LOCKDEBUG)
+       return lc->lc_refcnt;
+#else
+       return 0;
+#endif
+}
diff -r 8a051ceccffb -r cb8777dc89c8 sys/sys/localcount.h
--- a/sys/sys/localcount.h      Thu Nov 30 14:23:12 2017 +0000
+++ b/sys/sys/localcount.h      Thu Nov 30 14:31:04 2017 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: localcount.h,v 1.4 2017/06/02 00:32:12 chs Exp $       */
+/*     $NetBSD: localcount.h,v 1.4.2.1 2017/11/30 14:31:04 martin Exp $        */
 
 /*-
  * Copyright (c) 2016 The NetBSD Foundation, Inc.
@@ -43,8 +43,9 @@
 struct percpu;
 
 struct localcount {
-       int64_t         *lc_totalp;
-       struct percpu   *lc_percpu; /* int64_t */
+       int64_t                 *lc_totalp;
+       struct percpu           *lc_percpu; /* int64_t */
+       volatile uint32_t       lc_refcnt; /* only for debugging */
 };
 
 void   localcount_init(struct localcount *);
@@ -55,4 +56,7 @@
 void   localcount_release(struct localcount *, struct kcondvar *,
            struct kmutex *);
 
+uint32_t
+       localcount_debug_refcnt(const struct localcount *);
+
 #endif /* _SYS_LOCALCOUNT_H */



Home | Main Index | Thread Index | Old Index