Module Name:    src
Committed By:   kamil
Date:           Sun Feb 24 21:14:43 UTC 2019

Modified Files:
        src/sys/sys: kcov.h
        src/tests/modules: t_kcov.c

Log Message:
Add KCOV_LOAD() and KCOV_STORE() - new helper macros

New macros prefer 64-bit atomic operations whenever accessible.

As a fallback they use volatile move operations that are not known
to have negative effect in KCOV even if interrupted in the middle of
operation.

Enable kcov_basic and kcov_thread tests on targets without
__HAVE_ATOMIC64_OPS.


To generate a diff of this commit:
cvs rdiff -u -r1.1 -r1.2 src/sys/sys/kcov.h
cvs rdiff -u -r1.2 -r1.3 src/tests/modules/t_kcov.c

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

Modified files:

Index: src/sys/sys/kcov.h
diff -u src/sys/sys/kcov.h:1.1 src/sys/sys/kcov.h:1.2
--- src/sys/sys/kcov.h:1.1	Sat Feb 23 03:10:06 2019
+++ src/sys/sys/kcov.h	Sun Feb 24 21:14:43 2019
@@ -1,4 +1,4 @@
-/*      $NetBSD: kcov.h,v 1.1 2019/02/23 03:10:06 kamil Exp $        */
+/*      $NetBSD: kcov.h,v 1.2 2019/02/24 21:14:43 kamil Exp $        */
 
 /*
  * Copyright (c) 2019 The NetBSD Foundation, Inc.
@@ -32,6 +32,10 @@
 #ifndef _SYS_KCOV_H_
 #define _SYS_KCOV_H_
 
+#include <sys/param.h>
+#include <sys/types.h>
+#include <sys/atomic.h>
+
 #define KCOV_IOC_SETBUFSIZE	_IOW('K', 1, uint64_t)
 #define KCOV_IOC_ENABLE		_IO('K', 2)
 #define KCOV_IOC_DISABLE	_IO('K', 3)
@@ -39,4 +43,19 @@
 typedef volatile uint64_t kcov_int_t;
 #define KCOV_ENTRY_SIZE sizeof(kcov_int_t)
 
+/*
+ * Always prefer 64-bit atomic operations whenever accessible.
+ *
+ * As a fallback keep regular volatile move operation that it's not known
+ * to have negative effect in KCOV even if interrupted in the middle of
+ * operation.
+ */
+#ifdef __HAVE_ATOMIC64_OPS
+#define KCOV_STORE(x,v)	__atomic_store_n(&(x), (v), __ATOMIC_RELAXED)
+#define KCOV_LOAD(x)	__atomic_load_n(&(x), __ATOMIC_RELAXED)
+#else
+#define KCOV_STORE(x,v)	(x) = (y)
+#define KCOV_LOAD(x)	(x)
+#endif
+
 #endif /* !_SYS_KCOV_H_ */

Index: src/tests/modules/t_kcov.c
diff -u src/tests/modules/t_kcov.c:1.2 src/tests/modules/t_kcov.c:1.3
--- src/tests/modules/t_kcov.c:1.2	Sat Feb 23 08:03:24 2019
+++ src/tests/modules/t_kcov.c	Sun Feb 24 21:14:43 2019
@@ -254,10 +254,10 @@ ATF_TC_BODY(kcov_basic, tc)
 	ATF_REQUIRE_MSG(ioctl(fd, KCOV_IOC_ENABLE) == 0,
 	    "Unable to enable kcov ");
 
-	__atomic_store_n(&buf[0], 0 , __ATOMIC_RELAXED);
+	KCOV_STORE(&buf[0], 0);
 
 	sleep(0);
-	ATF_REQUIRE_MSG(__atomic_load_n(&buf[0], __ATOMIC_RELAXED) != 0, "No records found");
+	ATF_REQUIRE_MSG(KCOV_LOAD(&buf[0]) != 0, "No records found");
 
 	ATF_REQUIRE_MSG(ioctl(fd, KCOV_IOC_DISABLE) == 0,
 	    "Unable to disable kcov");
@@ -270,9 +270,9 @@ thread_test_helper(void *ptr)
 {
 	kcov_int_t *buf = ptr;
 
-	__atomic_store_n(&buf[0], 0, __ATOMIC_RELAXED);
+	KCOV_STORE(&buf[0], 0);
 	sleep(0);
-	ATF_REQUIRE_MSG(__atomic_load_n(&buf[0], __ATOMIC_RELAXED) == 0,
+	ATF_REQUIRE_MSG(KCOV_LOAD(&buf[0]) == 0,
 	    "Records changed in blocked thread");
 
 	return NULL;
@@ -311,9 +311,7 @@ ATF_TP_ADD_TCS(tp)
 	ATF_TP_ADD_TC(tp, kcov_enable_no_disable);
 	ATF_TP_ADD_TC(tp, kcov_enable_no_disable_no_close);
 	ATF_TP_ADD_TC(tp, kcov_mmap_enable_thread_close);
-#ifdef __HAVE_ATOMIC64_OPS
 	ATF_TP_ADD_TC(tp, kcov_basic);
 	ATF_TP_ADD_TC(tp, kcov_thread);
-#endif
 	return atf_no_error();
 }

Reply via email to