git: ae13829ddce0 - main - vm_addr_ok: add power2 invariant check

From: Doug Moore <dougm_at_FreeBSD.org>
Date: Mon, 10 Jan 2022 07:22:09 UTC
The branch main has been updated by dougm:

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

commit ae13829ddce0b5fbf12f2b240a26414b41def8ba
Author:     Doug Moore <dougm@FreeBSD.org>
AuthorDate: 2022-01-10 07:17:25 +0000
Commit:     Doug Moore <dougm@FreeBSD.org>
CommitDate: 2022-01-10 07:17:25 +0000

    vm_addr_ok: add power2 invariant check
    
    With INVARIANTS defined, have vm_addr_align_ok and vm_addr_bound_ok
    panic when passed an alignment/boundary parameter that is not a power
    of two.
    
    Reviewed by:    alc
    Suggested by:   kib, se
    Differential Revision:  https://reviews.freebsd.org/D33725
---
 sys/vm/vm_extern.h | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/sys/vm/vm_extern.h b/sys/vm/vm_extern.h
index 4e03bba66eeb..9fac3403f787 100644
--- a/sys/vm/vm_extern.h
+++ b/sys/vm/vm_extern.h
@@ -140,6 +140,11 @@ u_int vm_wait_count(void);
 static inline bool
 vm_addr_align_ok(vm_paddr_t pa, u_long alignment)
 {
+#ifdef INVARIANTS
+	if (!powerof2(alignment))
+		panic("%s: alignment is not a power of 2: %#lx",
+		    __func__, alignment);
+#endif
 	return ((pa & (alignment - 1)) == 0);
 }
 
@@ -150,6 +155,11 @@ vm_addr_align_ok(vm_paddr_t pa, u_long alignment)
 static inline bool
 vm_addr_bound_ok(vm_paddr_t pa, vm_paddr_t size, vm_paddr_t boundary)
 {
+#ifdef INVARIANTS
+	if (!powerof2(boundary))
+		panic("%s: boundary is not a power of 2: %#jx",
+		    __func__, (uintmax_t)boundary);
+#endif
 	return (((pa ^ (pa + size - 1)) & -boundary) == 0);
 }