git: 5e04571cf3cf - main - sys/bitset.h: reduce visibility of BIT_* macros

From: Stefan Eßer <se_at_FreeBSD.org>
Date: Sun, 05 Dec 2021 22:00:52 UTC
The branch main has been updated by se:

URL: https://cgit.FreeBSD.org/src/commit/?id=5e04571cf3cf4024be926976a6abf19626df30be

commit 5e04571cf3cf4024be926976a6abf19626df30be
Author:     Stefan Eßer <se@FreeBSD.org>
AuthorDate: 2021-12-05 21:27:33 +0000
Commit:     Stefan Eßer <se@FreeBSD.org>
CommitDate: 2021-12-05 22:00:25 +0000

    sys/bitset.h: reduce visibility of BIT_* macros
    
    Add two underscore characters "__" to names of BIT_* and BITSET_*
    macros to move them to the implementation name space and to prevent
    a name space pollution due to BIT_* macros in 3rd party programs with
    conflicting parameter signatures.
    
    These prefixed macro names are used in kernel header files to define
    macros in e.g. sched.h, sys/cpuset.h and sys/domainset.h.
    
    If C programs are built with either -D_KERNEL (automatically passed
    when building a kernel or kernel modules) or -D_WANT_FREENBSD_BITSET
    (or this macros is defined in the source code before including the
    bitset macros), then all macros are made visible with their previous
    names, too. E.g., both __BIT_SET() and BIT_SET() are visible with
    either of _KERNEL or _WANT_FREEBSD_BITSET defined.
    
    The main reason for this change is that some 3rd party sources
    including sched.h have been found to contain conflicting BIT_*
    macros.
    
    As a work-around, parts of shed.h have been made conditional and
    depend on _WITH_CPU_SET_T being set when sched.h is included.
    Ports that expect the full functionality provided by sched.h need
    to be built with -D_WITH_CPU_SET_T. But this leads to conflicts if
    BIT_* macros are defined in that program, too.
    
    This patch set makes all of sched.h visible again without this
    parameter being passed and without any name space pollution due
    to BIT_* macros becoming visible when sched.h is included.
    
    This patch set will be backported to the STABLE branches, but ports
    will need to use -D_WITH_CPU_SET_T as long as there are supported
    releases that do not contain these patches.
    
    Reviewed by:    kib, markj
    MFC after:      1 month
    Relnotes:       yes
    Differential Revision:  https://reviews.freebsd.org/D33235
---
 include/sched.h              |   4 --
 lib/libmemstat/memstat_uma.c |   2 +
 sbin/pfctl/pfctl_altq.c      |   1 +
 sbin/pfctl/pfctl_parser.h    |   2 +-
 share/man/man9/bitset.9      |  25 +++++++++
 sys/sys/_bitset.h            |  13 +++--
 sys/sys/_cpuset.h            |   2 +-
 sys/sys/_domainset.h         |   2 +-
 sys/sys/bitset.h             | 130 +++++++++++++++++++++++++++++--------------
 sys/sys/cpuset.h             |  58 +++++++++----------
 sys/sys/domainset.h          |  52 ++++++++---------
 tests/sys/sys/bitset_test.c  |   2 +
 usr.bin/cpuset/cpuset.c      |   2 +
 13 files changed, 186 insertions(+), 109 deletions(-)

diff --git a/include/sched.h b/include/sched.h
index 460d3bfed154..2d4f301c9718 100644
--- a/include/sched.h
+++ b/include/sched.h
@@ -33,20 +33,16 @@
 #include <sys/types.h>
 #include <sys/sched.h>
 #if __BSD_VISIBLE
-#ifdef _WITH_CPU_SET_T
 #include <sys/cpuset.h>
 struct _cpuset;
 typedef struct _cpuset cpu_set_t;
-#endif /* _WITH_CPU_SET_T */
 #endif /* __BSD_VISIBLE */
 
 __BEGIN_DECLS
 #if __BSD_VISIBLE
-#ifdef _WITH_CPU_SET_T
 int sched_getaffinity(pid_t pid, size_t cpusetsz, cpuset_t *cpuset);
 int sched_setaffinity(int pid, size_t cpusetsz, const cpuset_t *cpuset);
 int sched_getcpu(void);
-#endif /* _WITH_CPU_SET_T */
 #endif /* __BSD_VISIBLE */
 __END_DECLS
 
diff --git a/lib/libmemstat/memstat_uma.c b/lib/libmemstat/memstat_uma.c
index b416cbc63297..980a674ab76e 100644
--- a/lib/libmemstat/memstat_uma.c
+++ b/lib/libmemstat/memstat_uma.c
@@ -28,6 +28,8 @@
  * $FreeBSD$
  */
 
+#define _WANT_FREEBSD_BITSET
+
 #include <sys/param.h>
 #include <sys/counter.h>
 #include <sys/cpuset.h>
diff --git a/sbin/pfctl/pfctl_altq.c b/sbin/pfctl/pfctl_altq.c
index 304bf69d7c7a..6d35ae77240f 100644
--- a/sbin/pfctl/pfctl_altq.c
+++ b/sbin/pfctl/pfctl_altq.c
@@ -22,6 +22,7 @@
 __FBSDID("$FreeBSD$");
 
 #define PFIOC_USE_LATEST
+#define _WANT_FREEBSD_BITSET
 
 #include <sys/types.h>
 #include <sys/bitset.h>
diff --git a/sbin/pfctl/pfctl_parser.h b/sbin/pfctl/pfctl_parser.h
index 4e144b97567b..0cd19a560f8d 100644
--- a/sbin/pfctl/pfctl_parser.h
+++ b/sbin/pfctl/pfctl_parser.h
@@ -184,7 +184,7 @@ struct node_queue_opt {
 };
 
 #define QPRI_BITSET_SIZE	256
-BITSET_DEFINE(qpri_bitset, QPRI_BITSET_SIZE);
+__BITSET_DEFINE(qpri_bitset, QPRI_BITSET_SIZE);
 LIST_HEAD(gen_sc, segment);
 
 struct pfctl_altq {
diff --git a/share/man/man9/bitset.9 b/share/man/man9/bitset.9
index 1a5ec05b01c6..e6ce3d9b4f58 100644
--- a/share/man/man9/bitset.9
+++ b/share/man/man9/bitset.9
@@ -164,6 +164,7 @@
 .Fo BIT_COPY_STORE_REL
 .Fa "const SETSIZE" "struct STRUCTNAME *from" "struct STRUCTNAME *to"
 .Fc
+.Fd #define _WANT_FREEBSD_BITSET
 .Sh DESCRIPTION
 The
 .Nm
@@ -179,6 +180,16 @@ One example use of
 is
 .In sys/cpuset.h .
 .Pp
+These macros are meant to be used in the kernel and are visible if
+.Dv _KERNEL is defined when
+.In sys/_bitset.h
+or
+.In sys/bitset.h
+are included in a program.
+Userland programs must define 
+.Dv _WANT_FREEBSD_BITSET
+before including these files to make the macros visible.
+.Pp
 The
 .Fn BITSET_DEFINE
 macro defines a bitset struct
@@ -584,3 +595,17 @@ Unlike every other reference to individual set members, which are zero-indexed,
 and
 .Fn BIT_FLS
 return a one-indexed result (or zero if the set is empty).
+.Pp
+In order to use the macros defined in
+.In sys/bitset.h
+and
+.In sys/_bitset.h
+in userland programs,
+.Dv _WANT_FREEBSD_BITSET
+has to be defined before including the header files.
+This requirements exists to prevent a name space pollution due to macros defined in
+.Nm
+in programs that include
+.In sys/cpuset.h
+or
+.In sched.h .
diff --git a/sys/sys/_bitset.h b/sys/sys/_bitset.h
index e54f04cbeee7..1c167daf3f09 100644
--- a/sys/sys/_bitset.h
+++ b/sys/sys/_bitset.h
@@ -44,8 +44,8 @@
 
 #define	__bitset_words(_s)	(__howmany(_s, _BITSET_BITS))
 
-#define	BITSET_DEFINE(t, _s)						\
-struct t {								\
+#define	__BITSET_DEFINE(_t, _s)						\
+struct _t {								\
         long    __bits[__bitset_words((_s))];				\
 }
 
@@ -55,12 +55,17 @@ struct t {								\
  * Sadly we cannot declare a bitset struct with '__bits[]', because it's
  * the only member of the struct and the compiler complains.
  */
-#define BITSET_DEFINE_VAR(t)	BITSET_DEFINE(t, 1)
+#define __BITSET_DEFINE_VAR(_t)	__BITSET_DEFINE(_t, 1)
 
 /*
  * Define a default type that can be used while manually specifying size
  * to every call.
  */
-BITSET_DEFINE(bitset, 1);
+__BITSET_DEFINE(bitset, 1);
+
+#if defined(_KERNEL) || defined(_WANT_FREEBSD_BITSET)
+#define	BITSET_DEFINE(_t, _s)	__BITSET_DEFINE(_t, _s)
+#define	BITSET_DEFINE_VAR(_t)	__BITSET_DEFINE_VAR(_t)
+#endif
 
 #endif /* !_SYS__BITSET_H_ */
diff --git a/sys/sys/_cpuset.h b/sys/sys/_cpuset.h
index cb3c0f9d586e..9033acb9ba14 100644
--- a/sys/sys/_cpuset.h
+++ b/sys/sys/_cpuset.h
@@ -46,7 +46,7 @@
 #define	CPU_SETSIZE	CPU_MAXSIZE
 #endif
 
-BITSET_DEFINE(_cpuset, CPU_SETSIZE);
+__BITSET_DEFINE(_cpuset, CPU_SETSIZE);
 typedef struct _cpuset cpuset_t;
 
 #endif /* !_SYS__CPUSET_H_ */
diff --git a/sys/sys/_domainset.h b/sys/sys/_domainset.h
index 5685d532a9e3..443c68fd331b 100644
--- a/sys/sys/_domainset.h
+++ b/sys/sys/_domainset.h
@@ -43,7 +43,7 @@
 #define	DOMAINSET_SETSIZE	DOMAINSET_MAXSIZE
 #endif
 
-BITSET_DEFINE(_domainset, DOMAINSET_SETSIZE);
+__BITSET_DEFINE(_domainset, DOMAINSET_SETSIZE);
 typedef struct _domainset domainset_t;
 
 /*
diff --git a/sys/sys/bitset.h b/sys/sys/bitset.h
index 1c154d5601ab..91a5e6e37db3 100644
--- a/sys/sys/bitset.h
+++ b/sys/sys/bitset.h
@@ -48,36 +48,36 @@
 	(__constexpr_cond(__bitset_words((_s)) == 1) ?			\
 	 0 : ((n) / _BITSET_BITS))
 
-#define	BIT_CLR(_s, n, p)						\
+#define	__BIT_CLR(_s, n, p)						\
 	((p)->__bits[__bitset_word(_s, n)] &= ~__bitset_mask((_s), (n)))
 
-#define	BIT_COPY(_s, f, t)	(void)(*(t) = *(f))
+#define	__BIT_COPY(_s, f, t)	(void)(*(t) = *(f))
 
-#define	BIT_ISSET(_s, n, p)						\
+#define	__BIT_ISSET(_s, n, p)						\
 	((((p)->__bits[__bitset_word(_s, n)] & __bitset_mask((_s), (n))) != 0))
 
-#define	BIT_SET(_s, n, p)						\
+#define	__BIT_SET(_s, n, p)						\
 	((p)->__bits[__bitset_word(_s, n)] |= __bitset_mask((_s), (n)))
 
-#define	BIT_ZERO(_s, p) do {						\
+#define	__BIT_ZERO(_s, p) do {						\
 	__size_t __i;							\
 	for (__i = 0; __i < __bitset_words((_s)); __i++)		\
 		(p)->__bits[__i] = 0L;					\
 } while (0)
 
-#define	BIT_FILL(_s, p) do {						\
+#define	__BIT_FILL(_s, p) do {						\
 	__size_t __i;							\
 	for (__i = 0; __i < __bitset_words((_s)); __i++)		\
 		(p)->__bits[__i] = -1L;					\
 } while (0)
 
-#define	BIT_SETOF(_s, n, p) do {					\
-	BIT_ZERO(_s, p);						\
+#define	__BIT_SETOF(_s, n, p) do {					\
+	__BIT_ZERO(_s, p);						\
 	(p)->__bits[__bitset_word(_s, n)] = __bitset_mask((_s), (n));	\
 } while (0)
 
 /* Is p empty. */
-#define	BIT_EMPTY(_s, p) __extension__ ({				\
+#define	__BIT_EMPTY(_s, p) __extension__ ({				\
 	__size_t __i;							\
 	for (__i = 0; __i < __bitset_words((_s)); __i++)		\
 		if ((p)->__bits[__i])					\
@@ -86,7 +86,7 @@
 })
 
 /* Is p full set. */
-#define	BIT_ISFULLSET(_s, p) __extension__ ({				\
+#define	__BIT_ISFULLSET(_s, p) __extension__ ({				\
 	__size_t __i;							\
 	for (__i = 0; __i < __bitset_words((_s)); __i++)		\
 		if ((p)->__bits[__i] != (long)-1)			\
@@ -95,7 +95,7 @@
 })
 
 /* Is c a subset of p. */
-#define	BIT_SUBSET(_s, p, c) __extension__ ({				\
+#define	__BIT_SUBSET(_s, p, c) __extension__ ({				\
 	__size_t __i;							\
 	for (__i = 0; __i < __bitset_words((_s)); __i++)		\
 		if (((c)->__bits[__i] &					\
@@ -106,7 +106,7 @@
 })
 
 /* Are there any common bits between b & c? */
-#define	BIT_OVERLAP(_s, p, c) __extension__ ({				\
+#define	__BIT_OVERLAP(_s, p, c) __extension__ ({	       		\
 	__size_t __i;							\
 	for (__i = 0; __i < __bitset_words((_s)); __i++)		\
 		if (((c)->__bits[__i] &					\
@@ -116,7 +116,7 @@
 })
 
 /* Compare two sets, returns 0 if equal 1 otherwise. */
-#define	BIT_CMP(_s, p, c) __extension__ ({				\
+#define	__BIT_CMP(_s, p, c) __extension__ ({				\
 	__size_t __i;							\
 	for (__i = 0; __i < __bitset_words((_s)); __i++)		\
 		if (((c)->__bits[__i] !=				\
@@ -125,49 +125,49 @@
 	__i != __bitset_words((_s));					\
 })
 
-#define	BIT_OR(_s, d, s) do {						\
+#define	__BIT_OR(_s, d, s) do {						\
 	__size_t __i;							\
 	for (__i = 0; __i < __bitset_words((_s)); __i++)		\
 		(d)->__bits[__i] |= (s)->__bits[__i];			\
 } while (0)
 
-#define	BIT_OR2(_s, d, s1, s2) do {					\
+#define	__BIT_OR2(_s, d, s1, s2) do {					\
 	__size_t __i;							\
 	for (__i = 0; __i < __bitset_words((_s)); __i++)		\
 		(d)->__bits[__i] = (s1)->__bits[__i] | (s2)->__bits[__i];\
 } while (0)
 
-#define	BIT_AND(_s, d, s) do {						\
+#define	__BIT_AND(_s, d, s) do {					\
 	__size_t __i;							\
 	for (__i = 0; __i < __bitset_words((_s)); __i++)		\
 		(d)->__bits[__i] &= (s)->__bits[__i];			\
 } while (0)
 
-#define	BIT_AND2(_s, d, s1, s2) do {					\
+#define	__BIT_AND2(_s, d, s1, s2) do {					\
 	__size_t __i;							\
 	for (__i = 0; __i < __bitset_words((_s)); __i++)		\
 		(d)->__bits[__i] = (s1)->__bits[__i] & (s2)->__bits[__i];\
 } while (0)
 
-#define	BIT_ANDNOT(_s, d, s) do {					\
+#define	__BIT_ANDNOT(_s, d, s) do {					\
 	__size_t __i;							\
 	for (__i = 0; __i < __bitset_words((_s)); __i++)		\
 		(d)->__bits[__i] &= ~(s)->__bits[__i];			\
 } while (0)
 
-#define	BIT_ANDNOT2(_s, d, s1, s2) do {					\
+#define	__BIT_ANDNOT2(_s, d, s1, s2) do {				\
 	__size_t __i;							\
 	for (__i = 0; __i < __bitset_words((_s)); __i++)		\
 		(d)->__bits[__i] = (s1)->__bits[__i] & ~(s2)->__bits[__i];\
 } while (0)
 
-#define	BIT_XOR(_s, d, s) do {						\
+#define	__BIT_XOR(_s, d, s) do {					\
 	__size_t __i;							\
 	for (__i = 0; __i < __bitset_words((_s)); __i++)		\
 		(d)->__bits[__i] ^= (s)->__bits[__i];			\
 } while (0)
 
-#define	BIT_XOR2(_s, d, s1, s2) do {					\
+#define	__BIT_XOR2(_s, d, s1, s2) do {					\
 	__size_t __i;							\
 	for (__i = 0; __i < __bitset_words((_s)); __i++)		\
 		(d)->__bits[__i] = (s1)->__bits[__i] ^ (s2)->__bits[__i];\
@@ -179,42 +179,42 @@
  * or a bit index.
  */
 
-#define	BIT_CLR_ATOMIC(_s, n, p)					\
+#define	__BIT_CLR_ATOMIC(_s, n, p)					\
 	atomic_clear_long(&(p)->__bits[__bitset_word(_s, n)],		\
 	    __bitset_mask((_s), n))
 
-#define	BIT_SET_ATOMIC(_s, n, p)					\
+#define	__BIT_SET_ATOMIC(_s, n, p)					\
 	atomic_set_long(&(p)->__bits[__bitset_word(_s, n)],		\
 	    __bitset_mask((_s), n))
 
-#define	BIT_SET_ATOMIC_ACQ(_s, n, p)					\
+#define	__BIT_SET_ATOMIC_ACQ(_s, n, p)					\
 	atomic_set_acq_long(&(p)->__bits[__bitset_word(_s, n)],		\
 	    __bitset_mask((_s), n))
 
-#define	BIT_TEST_CLR_ATOMIC(_s, n, p)					\
+#define	__BIT_TEST_CLR_ATOMIC(_s, n, p)					\
 	(atomic_testandclear_long(					\
 	    &(p)->__bits[__bitset_word((_s), (n))], (n)) != 0)
 
-#define	BIT_TEST_SET_ATOMIC(_s, n, p)					\
+#define	__BIT_TEST_SET_ATOMIC(_s, n, p)					\
 	(atomic_testandset_long(					\
 	    &(p)->__bits[__bitset_word((_s), (n))], (n)) != 0)
 
 /* Convenience functions catering special cases. */
-#define	BIT_AND_ATOMIC(_s, d, s) do {					\
+#define	__BIT_AND_ATOMIC(_s, d, s) do {					\
 	__size_t __i;							\
 	for (__i = 0; __i < __bitset_words((_s)); __i++)		\
 		atomic_clear_long(&(d)->__bits[__i],			\
 		    ~(s)->__bits[__i]);					\
 } while (0)
 
-#define	BIT_OR_ATOMIC(_s, d, s) do {					\
+#define	__BIT_OR_ATOMIC(_s, d, s) do {					\
 	__size_t __i;							\
 	for (__i = 0; __i < __bitset_words((_s)); __i++)		\
 		atomic_set_long(&(d)->__bits[__i],			\
 		    (s)->__bits[__i]);					\
 } while (0)
 
-#define	BIT_COPY_STORE_REL(_s, f, t) do {				\
+#define	__BIT_COPY_STORE_REL(_s, f, t) do {				\
 	__size_t __i;							\
 	for (__i = 0; __i < __bitset_words((_s)); __i++)		\
 		atomic_store_rel_long(&(t)->__bits[__i],		\
@@ -222,10 +222,10 @@
 } while (0)
 
 /*
- * Note that `start` and the returned value from BIT_FFS_AT are
+ * Note that `start` and the returned value from __BIT_FFS_AT are
  * 1-based bit indices.
  */
-#define	BIT_FFS_AT(_s, p, start) __extension__ ({			\
+#define	__BIT_FFS_AT(_s, p, start) __extension__ ({			\
 	__size_t __i;							\
 	long __bit, __mask;						\
 									\
@@ -244,9 +244,9 @@
 	__bit;								\
 })
 
-#define	BIT_FFS(_s, p) BIT_FFS_AT((_s), (p), 0)
+#define	__BIT_FFS(_s, p) __BIT_FFS_AT((_s), (p), 0)
 
-#define	BIT_FLS(_s, p) __extension__ ({					\
+#define	__BIT_FLS(_s, p) __extension__ ({			       	\
 	__size_t __i;							\
 	long __bit;							\
 									\
@@ -261,7 +261,7 @@
 	__bit;								\
 })
 
-#define	BIT_COUNT(_s, p) __extension__ ({				\
+#define	__BIT_COUNT(_s, p) __extension__ ({				\
 	__size_t __i;							\
 	long __count;							\
 									\
@@ -271,7 +271,7 @@
 	__count;							\
 })
 
-#define	_BIT_FOREACH_ADVANCE(_s, i, p, op) __extension__ ({		\
+#define	__BIT_FOREACH_ADVANCE(_s, i, p, op) __extension__ ({		\
 	int __found;							\
 	for (;;) {							\
 		if (__bits != 0) {					\
@@ -293,24 +293,68 @@
 /*
  * Non-destructively loop over all set or clear bits in the set.
  */
-#define _BIT_FOREACH(_s, i, p, op)					\
+#define __BIT_FOREACH(_s, i, p, op)					\
 	for (long __i = -1, __bits = 0;					\
-	    _BIT_FOREACH_ADVANCE(_s, i, p, op); )
+	    __BIT_FOREACH_ADVANCE(_s, i, p, op); )
 
-#define	BIT_FOREACH_ISSET(_s, i, p)	_BIT_FOREACH(_s, i, p, )
-#define	BIT_FOREACH_ISCLR(_s, i, p)	_BIT_FOREACH(_s, i, p, ~)
+#define	__BIT_FOREACH_ISSET(_s, i, p)	__BIT_FOREACH(_s, i, p, )
+#define	__BIT_FOREACH_ISCLR(_s, i, p)	__BIT_FOREACH(_s, i, p, ~)
 
-#define	BITSET_T_INITIALIZER(x)						\
+#define	__BITSET_T_INITIALIZER(x)					\
 	{ .__bits = { x } }
 
-#define	BITSET_FSET(n)							\
+#define	__BITSET_FSET(n)						\
 	[ 0 ... ((n) - 1) ] = (-1L)
 
-#define	BITSET_SIZE(_s)	(__bitset_words((_s)) * sizeof(long))
+#define	__BITSET_SIZE(_s)	(__bitset_words((_s)) * sizeof(long))
 
 /*
  * Dynamically allocate a bitset.
  */
-#define BITSET_ALLOC(_s, mt, mf)	malloc(BITSET_SIZE((_s)), mt, (mf))
+#define __BITSET_ALLOC(_s, mt, mf) malloc(__BITSET_SIZE((_s)), mt, (mf))
+
+#if defined(_KERNEL) || defined(_WANT_FREEBSD_BITSET)
+#define	BIT_AND(_s, d, s)			__BIT_AND(_s, d, s)
+#define	BIT_AND2(_s, d, s1, s2)			__BIT_AND2(_s, d, s1, s2)
+#define	BIT_ANDNOT(_s, d, s)			__BIT_ANDNOT(_s, d, s)
+#define	BIT_ANDNOT2(_s, d, s1, s2)		__BIT_ANDNOT2(_s, d, s1, s2)
+#define	BIT_AND_ATOMIC(_s, d, s)		__BIT_AND_ATOMIC(_s, d, s)
+#define	BIT_CLR(_s, n, p)			__BIT_CLR(_s, n, p)
+#define	BIT_CLR_ATOMIC(_s, n, p)		__BIT_CLR_ATOMIC(_s, n, p)
+#define	BIT_CMP(_s, p, c)			__BIT_CMP(_s, p, c)
+#define	BIT_COPY(_s, f, t)			__BIT_COPY(_s, f, t)
+#define	BIT_COPY_STORE_REL(_s, f, t)		__BIT_COPY_STORE_REL(_s, f, t)
+#define	BIT_COUNT(_s, p)			__BIT_COUNT(_s, p)
+#define	BIT_EMPTY(_s, p)			__BIT_EMPTY(_s, p)
+#define	BIT_FFS(_s, p)				__BIT_FFS(_s, p)
+#define	BIT_FFS_AT(_s, p, start)		__BIT_FFS_AT(_s, p, start)
+#define	BIT_FILL(_s, p)				__BIT_FILL(_s, p)
+#define	BIT_FLS(_s, p)				__BIT_FLS(_s, p)
+#define BIT_FOREACH(_s, i, p, op)		__BIT_FOREACH(_s, i, p, op)
+#define	BIT_FOREACH_ADVANCE(_s, i, p, op)	__BIT_FOREACH_ADVANCE(_s, i, p, op)
+#define	BIT_FOREACH_ISCLR(_s, i, p)		__BIT_FOREACH_ISCLR(_s, i, p)
+#define	BIT_FOREACH_ISSET(_s, i, p)		__BIT_FOREACH_ISSET(_s, i, p)
+#define	BIT_ISFULLSET(_s, p)			__BIT_ISFULLSET(_s, p)
+#define	BIT_ISSET(_s, n, p)			__BIT_ISSET(_s, n, p)
+#define	BIT_OR(_s, d, s)			__BIT_OR(_s, d, s)
+#define	BIT_OR2(_s, d, s1, s2)			__BIT_OR2(_s, d, s1, s2)
+#define	BIT_OR_ATOMIC(_s, d, s)			__BIT_OR_ATOMIC(_s, d, s)
+#define	BIT_OVERLAP(_s, p, c)			__BIT_OVERLAP(_s, p, c)
+#define	BIT_SET(_s, n, p)			__BIT_SET(_s, n, p)
+#define	BIT_SETOF(_s, n, p)			__BIT_SETOF(_s, n, p)
+#define	BIT_SET_ATOMIC(_s, n, p)		__BIT_SET_ATOMIC(_s, n, p)
+#define	BIT_SET_ATOMIC_ACQ(_s, n, p)		__BIT_SET_ATOMIC_ACQ(_s, n, p)
+#define	BIT_SUBSET(_s, p, c)			__BIT_SUBSET(_s, p, c)
+#define	BIT_TEST_CLR_ATOMIC(_s, n, p)		__BIT_TEST_CLR_ATOMIC(_s, n, p)
+#define	BIT_TEST_SET_ATOMIC(_s, n, p)		__BIT_TEST_SET_ATOMIC(_s, n, p)
+#define	BIT_XOR(_s, d, s)			__BIT_XOR(_s, d, s)
+#define	BIT_XOR2(_s, d, s1, s2)			__BIT_XOR2(_s, d, s1, s2)
+#define	BIT_ZERO(_s, p)				__BIT_ZERO(_s, p)
+
+#define BITSET_ALLOC(_s, mt, mf)		__BITSET_ALLOC(_s, mt, mf)
+#define	BITSET_FSET(n)				__BITSET_FSET(n)
+#define	BITSET_SIZE(_s)				__BITSET_SIZE(_s)
+#define	BITSET_T_INITIALIZER(x)			__BITSET_T_INITIALIZER(x)
+#endif
 
 #endif /* !_SYS_BITSET_H_ */
diff --git a/sys/sys/cpuset.h b/sys/sys/cpuset.h
index 602f73fbe5e4..71317670c593 100644
--- a/sys/sys/cpuset.h
+++ b/sys/sys/cpuset.h
@@ -43,35 +43,35 @@
 
 #define	CPUSETBUFSIZ	((2 + sizeof(long) * 2) * _NCPUWORDS)
 
-#define	CPU_CLR(n, p)			BIT_CLR(CPU_SETSIZE, n, p)
-#define	CPU_COPY(f, t)			BIT_COPY(CPU_SETSIZE, f, t)
-#define	CPU_ISSET(n, p)			BIT_ISSET(CPU_SETSIZE, n, p)
-#define	CPU_SET(n, p)			BIT_SET(CPU_SETSIZE, n, p)
-#define	CPU_ZERO(p) 			BIT_ZERO(CPU_SETSIZE, p)
-#define	CPU_FILL(p) 			BIT_FILL(CPU_SETSIZE, p)
-#define	CPU_SETOF(n, p)			BIT_SETOF(CPU_SETSIZE, n, p)
-#define	CPU_EQUAL(p, c)			(BIT_CMP(CPU_SETSIZE, p, c) == 0)
-#define	CPU_EMPTY(p)			BIT_EMPTY(CPU_SETSIZE, p)
-#define	CPU_ISFULLSET(p)		BIT_ISFULLSET(CPU_SETSIZE, p)
-#define	CPU_SUBSET(p, c)		BIT_SUBSET(CPU_SETSIZE, p, c)
-#define	CPU_OVERLAP(p, c)		BIT_OVERLAP(CPU_SETSIZE, p, c)
-#define	CPU_CMP(p, c)			BIT_CMP(CPU_SETSIZE, p, c)
-#define	CPU_OR(d, s)			BIT_OR(CPU_SETSIZE, d, s)
-#define	CPU_AND(d, s)			BIT_AND(CPU_SETSIZE, d, s)
-#define	CPU_ANDNOT(d, s)		BIT_ANDNOT(CPU_SETSIZE, d, s)
-#define	CPU_CLR_ATOMIC(n, p)		BIT_CLR_ATOMIC(CPU_SETSIZE, n, p)
-#define	CPU_SET_ATOMIC(n, p)		BIT_SET_ATOMIC(CPU_SETSIZE, n, p)
-#define	CPU_SET_ATOMIC_ACQ(n, p)	BIT_SET_ATOMIC_ACQ(CPU_SETSIZE, n, p)
-#define	CPU_AND_ATOMIC(n, p)		BIT_AND_ATOMIC(CPU_SETSIZE, n, p)
-#define	CPU_OR_ATOMIC(d, s)		BIT_OR_ATOMIC(CPU_SETSIZE, d, s)
-#define	CPU_COPY_STORE_REL(f, t)	BIT_COPY_STORE_REL(CPU_SETSIZE, f, t)
-#define	CPU_FFS(p)			BIT_FFS(CPU_SETSIZE, p)
-#define	CPU_FLS(p)			BIT_FLS(CPU_SETSIZE, p)
-#define	CPU_FOREACH_ISSET(i, p)		BIT_FOREACH_ISSET(CPU_SETSIZE, i, p)
-#define	CPU_FOREACH_ISCLR(i, p)		BIT_FOREACH_ISCLR(CPU_SETSIZE, i, p)
-#define	CPU_COUNT(p)			((int)BIT_COUNT(CPU_SETSIZE, p))
-#define	CPUSET_FSET			BITSET_FSET(_NCPUWORDS)
-#define	CPUSET_T_INITIALIZER		BITSET_T_INITIALIZER
+#define	CPU_CLR(n, p)			__BIT_CLR(CPU_SETSIZE, n, p)
+#define	CPU_COPY(f, t)			__BIT_COPY(CPU_SETSIZE, f, t)
+#define	CPU_ISSET(n, p)			__BIT_ISSET(CPU_SETSIZE, n, p)
+#define	CPU_SET(n, p)			__BIT_SET(CPU_SETSIZE, n, p)
+#define	CPU_ZERO(p) 			__BIT_ZERO(CPU_SETSIZE, p)
+#define	CPU_FILL(p) 			__BIT_FILL(CPU_SETSIZE, p)
+#define	CPU_SETOF(n, p)			__BIT_SETOF(CPU_SETSIZE, n, p)
+#define	CPU_EQUAL(p, c)			(__BIT_CMP(CPU_SETSIZE, p, c) == 0)
+#define	CPU_EMPTY(p)			__BIT_EMPTY(CPU_SETSIZE, p)
+#define	CPU_ISFULLSET(p)		__BIT_ISFULLSET(CPU_SETSIZE, p)
+#define	CPU_SUBSET(p, c)		__BIT_SUBSET(CPU_SETSIZE, p, c)
+#define	CPU_OVERLAP(p, c)		__BIT_OVERLAP(CPU_SETSIZE, p, c)
+#define	CPU_CMP(p, c)			__BIT_CMP(CPU_SETSIZE, p, c)
+#define	CPU_OR(d, s)			__BIT_OR(CPU_SETSIZE, d, s)
+#define	CPU_AND(d, s)			__BIT_AND(CPU_SETSIZE, d, s)
+#define	CPU_ANDNOT(d, s)		__BIT_ANDNOT(CPU_SETSIZE, d, s)
+#define	CPU_CLR_ATOMIC(n, p)		__BIT_CLR_ATOMIC(CPU_SETSIZE, n, p)
+#define	CPU_SET_ATOMIC(n, p)		__BIT_SET_ATOMIC(CPU_SETSIZE, n, p)
+#define	CPU_SET_ATOMIC_ACQ(n, p)	__BIT_SET_ATOMIC_ACQ(CPU_SETSIZE, n, p)
+#define	CPU_AND_ATOMIC(n, p)		__BIT_AND_ATOMIC(CPU_SETSIZE, n, p)
+#define	CPU_OR_ATOMIC(d, s)		__BIT_OR_ATOMIC(CPU_SETSIZE, d, s)
+#define	CPU_COPY_STORE_REL(f, t)	__BIT_COPY_STORE_REL(CPU_SETSIZE, f, t)
+#define	CPU_FFS(p)			__BIT_FFS(CPU_SETSIZE, p)
+#define	CPU_FLS(p)			__BIT_FLS(CPU_SETSIZE, p)
+#define	CPU_FOREACH_ISSET(i, p)		__BIT_FOREACH_ISSET(CPU_SETSIZE, i, p)
+#define	CPU_FOREACH_ISCLR(i, p)		__BIT_FOREACH_ISCLR(CPU_SETSIZE, i, p)
+#define	CPU_COUNT(p)			((int)__BIT_COUNT(CPU_SETSIZE, p))
+#define	CPUSET_FSET			__BITSET_FSET(_NCPUWORDS)
+#define	CPUSET_T_INITIALIZER(x)		__BITSET_T_INITIALIZER(x)
 
 /*
  * Valid cpulevel_t values.
diff --git a/sys/sys/domainset.h b/sys/sys/domainset.h
index 2113196d8d26..51b5011fcb4e 100644
--- a/sys/sys/domainset.h
+++ b/sys/sys/domainset.h
@@ -43,34 +43,34 @@
 	    sizeof("::") + sizeof(__XSTRING(DOMAINSET_POLICY_MAX)) +	\
 	    sizeof(__XSTRING(MAXMEMDOM)))
 
-#define	DOMAINSET_CLR(n, p)		BIT_CLR(DOMAINSET_SETSIZE, n, p)
-#define	DOMAINSET_COPY(f, t)		BIT_COPY(DOMAINSET_SETSIZE, f, t)
-#define	DOMAINSET_ISSET(n, p)		BIT_ISSET(DOMAINSET_SETSIZE, n, p)
-#define	DOMAINSET_SET(n, p)		BIT_SET(DOMAINSET_SETSIZE, n, p)
-#define	DOMAINSET_ZERO(p) 		BIT_ZERO(DOMAINSET_SETSIZE, p)
-#define	DOMAINSET_FILL(p) 		BIT_FILL(DOMAINSET_SETSIZE, p)
-#define	DOMAINSET_SETOF(n, p)		BIT_SETOF(DOMAINSET_SETSIZE, n, p)
-#define	DOMAINSET_EMPTY(p)		BIT_EMPTY(DOMAINSET_SETSIZE, p)
-#define	DOMAINSET_ISFULLSET(p)		BIT_ISFULLSET(DOMAINSET_SETSIZE, p)
-#define	DOMAINSET_SUBSET(p, c)		BIT_SUBSET(DOMAINSET_SETSIZE, p, c)
-#define	DOMAINSET_OVERLAP(p, c)		BIT_OVERLAP(DOMAINSET_SETSIZE, p, c)
-#define	DOMAINSET_CMP(p, c)		BIT_CMP(DOMAINSET_SETSIZE, p, c)
-#define	DOMAINSET_OR(d, s)		BIT_OR(DOMAINSET_SETSIZE, d, s)
-#define	DOMAINSET_AND(d, s)		BIT_AND(DOMAINSET_SETSIZE, d, s)
-#define	DOMAINSET_ANDNOT(d, s)		BIT_ANDNOT(DOMAINSET_SETSIZE, d, s)
-#define	DOMAINSET_CLR_ATOMIC(n, p)	BIT_CLR_ATOMIC(DOMAINSET_SETSIZE, n, p)
-#define	DOMAINSET_SET_ATOMIC(n, p)	BIT_SET_ATOMIC(DOMAINSET_SETSIZE, n, p)
+#define	DOMAINSET_CLR(n, p)		__BIT_CLR(DOMAINSET_SETSIZE, n, p)
+#define	DOMAINSET_COPY(f, t)		__BIT_COPY(DOMAINSET_SETSIZE, f, t)
+#define	DOMAINSET_ISSET(n, p)		__BIT_ISSET(DOMAINSET_SETSIZE, n, p)
+#define	DOMAINSET_SET(n, p)		__BIT_SET(DOMAINSET_SETSIZE, n, p)
+#define	DOMAINSET_ZERO(p) 		__BIT_ZERO(DOMAINSET_SETSIZE, p)
+#define	DOMAINSET_FILL(p) 		__BIT_FILL(DOMAINSET_SETSIZE, p)
+#define	DOMAINSET_SETOF(n, p)		__BIT_SETOF(DOMAINSET_SETSIZE, n, p)
+#define	DOMAINSET_EMPTY(p)		__BIT_EMPTY(DOMAINSET_SETSIZE, p)
+#define	DOMAINSET_ISFULLSET(p)		__BIT_ISFULLSET(DOMAINSET_SETSIZE, p)
+#define	DOMAINSET_SUBSET(p, c)		__BIT_SUBSET(DOMAINSET_SETSIZE, p, c)
+#define	DOMAINSET_OVERLAP(p, c)		__BIT_OVERLAP(DOMAINSET_SETSIZE, p, c)
+#define	DOMAINSET_CMP(p, c)		__BIT_CMP(DOMAINSET_SETSIZE, p, c)
+#define	DOMAINSET_OR(d, s)		__BIT_OR(DOMAINSET_SETSIZE, d, s)
+#define	DOMAINSET_AND(d, s)		__BIT_AND(DOMAINSET_SETSIZE, d, s)
+#define	DOMAINSET_ANDNOT(d, s)		__BIT_ANDNOT(DOMAINSET_SETSIZE, d, s)
+#define	DOMAINSET_CLR_ATOMIC(n, p)	__BIT_CLR_ATOMIC(DOMAINSET_SETSIZE, n, p)
+#define	DOMAINSET_SET_ATOMIC(n, p)	__BIT_SET_ATOMIC(DOMAINSET_SETSIZE, n, p)
 #define	DOMAINSET_SET_ATOMIC_ACQ(n, p)					\
-	    BIT_SET_ATOMIC_ACQ(DOMAINSET_SETSIZE, n, p)
-#define	DOMAINSET_AND_ATOMIC(n, p)	BIT_AND_ATOMIC(DOMAINSET_SETSIZE, n, p)
-#define	DOMAINSET_OR_ATOMIC(d, s)	BIT_OR_ATOMIC(DOMAINSET_SETSIZE, d, s)
+	    __BIT_SET_ATOMIC_ACQ(DOMAINSET_SETSIZE, n, p)
+#define	DOMAINSET_AND_ATOMIC(n, p)	__BIT_AND_ATOMIC(DOMAINSET_SETSIZE, n, p)
+#define	DOMAINSET_OR_ATOMIC(d, s)	__BIT_OR_ATOMIC(DOMAINSET_SETSIZE, d, s)
 #define	DOMAINSET_COPY_STORE_REL(f, t)					\
-	    BIT_COPY_STORE_REL(DOMAINSET_SETSIZE, f, t)
-#define	DOMAINSET_FFS(p)		BIT_FFS(DOMAINSET_SETSIZE, p)
-#define	DOMAINSET_FLS(p)		BIT_FLS(DOMAINSET_SETSIZE, p)
-#define	DOMAINSET_COUNT(p)		((int)BIT_COUNT(DOMAINSET_SETSIZE, p))
-#define	DOMAINSET_FSET			BITSET_FSET(_NDOMAINSETWORDS)
-#define	DOMAINSET_T_INITIALIZER		BITSET_T_INITIALIZER
+	    __BIT_COPY_STORE_REL(DOMAINSET_SETSIZE, f, t)
+#define	DOMAINSET_FFS(p)		__BIT_FFS(DOMAINSET_SETSIZE, p)
+#define	DOMAINSET_FLS(p)		__BIT_FLS(DOMAINSET_SETSIZE, p)
+#define	DOMAINSET_COUNT(p)		((int)__BIT_COUNT(DOMAINSET_SETSIZE, p))
+#define	DOMAINSET_FSET			__BITSET_FSET(_NDOMAINSETWORDS)
+#define	DOMAINSET_T_INITIALIZER(x)	__BITSET_T_INITIALIZER(x)
 
 #define	DOMAINSET_POLICY_INVALID	0
 #define	DOMAINSET_POLICY_ROUNDROBIN	1
diff --git a/tests/sys/sys/bitset_test.c b/tests/sys/sys/bitset_test.c
index 781b523dae97..d193d2e3014c 100644
--- a/tests/sys/sys/bitset_test.c
+++ b/tests/sys/sys/bitset_test.c
@@ -7,6 +7,8 @@
  * the FreeBSD Foundation.
  */
 
+#define _WANT_FREEBSD_BITSET
+
 #include <sys/types.h>
 #include <sys/_bitset.h>
 #include <sys/bitset.h>
diff --git a/usr.bin/cpuset/cpuset.c b/usr.bin/cpuset/cpuset.c
index e039030415bb..79c6c2b6ca79 100644
--- a/usr.bin/cpuset/cpuset.c
+++ b/usr.bin/cpuset/cpuset.c
@@ -32,6 +32,8 @@
 #include <sys/cdefs.h>
 __FBSDID("$FreeBSD$");
 
+#define _WANT_FREEBSD_BITSET
+
 #include <sys/param.h>
 #include <sys/types.h>
 #include <sys/time.h>