git: c59166e5b4e8 - main - vm_page: Fix a logic bug in vm_page_unwire_managed()

From: Mark Johnston <markj_at_FreeBSD.org>
Date: Mon, 07 Oct 2024 20:52:32 UTC
The branch main has been updated by markj:

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

commit c59166e5b4e8821556a3d23af7bd17ca556f2e22
Author:     Mark Johnston <markj@FreeBSD.org>
AuthorDate: 2024-10-07 20:50:49 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2024-10-07 20:52:15 +0000

    vm_page: Fix a logic bug in vm_page_unwire_managed()
    
    When releasing a page reference, we have logic for various cases, based
    on the value of the counter.  But, the implementation fails to take into
    account the possibility that the VPRC_BLOCKED flag is set, which is ORed
    into the counter for short windows when removing mappings of a page.  If
    the flag is set while the last reference is being released, we may fail
    to add the page to a page queue when the last wiring reference is
    released.
    
    Fix the problem by performing comparisons with VPRC_BLOCKED masked off.
    While here, add a related assertion.
    
    Reviewed by:    dougm, kib
    Tested by:      pho
    MFC after:      2 weeks
    Differential Revision:  https://reviews.freebsd.org/D46944
---
 sys/vm/vm_page.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/sys/vm/vm_page.c b/sys/vm/vm_page.c
index 3a8d8d137dc1..1f0b364dbde2 100644
--- a/sys/vm/vm_page.c
+++ b/sys/vm/vm_page.c
@@ -4275,10 +4275,13 @@ vm_page_unwire_managed(vm_page_t m, uint8_t nqueue, bool noreuse)
 	 */
 	old = atomic_load_int(&m->ref_count);
 	do {
+		u_int count;
+
 		KASSERT(VPRC_WIRE_COUNT(old) > 0,
 		    ("vm_page_unwire: wire count underflow for page %p", m));
 
-		if (old > VPRC_OBJREF + 1) {
+		count = old & ~VPRC_BLOCKED;
+		if (count > VPRC_OBJREF + 1) {
 			/*
 			 * The page has at least one other wiring reference.  An
 			 * earlier iteration of this loop may have called
@@ -4287,7 +4290,7 @@ vm_page_unwire_managed(vm_page_t m, uint8_t nqueue, bool noreuse)
 			 */
 			if ((vm_page_astate_load(m).flags & PGA_DEQUEUE) == 0)
 				vm_page_aflag_set(m, PGA_DEQUEUE);
-		} else if (old == VPRC_OBJREF + 1) {
+		} else if (count == VPRC_OBJREF + 1) {
 			/*
 			 * This is the last wiring.  Clear PGA_DEQUEUE and
 			 * update the page's queue state to reflect the
@@ -4296,7 +4299,7 @@ vm_page_unwire_managed(vm_page_t m, uint8_t nqueue, bool noreuse)
 			 * clear leftover queue state.
 			 */
 			vm_page_release_toq(m, nqueue, noreuse);
-		} else if (old == 1) {
+		} else if (count == 1) {
 			vm_page_aflag_clear(m, PGA_DEQUEUE);
 		}
 	} while (!atomic_fcmpset_rel_int(&m->ref_count, &old, old - 1));
@@ -4572,6 +4575,8 @@ vm_page_try_blocked_op(vm_page_t m, void (*op)(vm_page_t))
 	do {
 		KASSERT(old != 0,
 		    ("vm_page_try_blocked_op: page %p has no references", m));
+		KASSERT((old & VPRC_BLOCKED) == 0,
+		    ("vm_page_try_blocked_op: page %p blocks wirings", m));
 		if (VPRC_WIRE_COUNT(old) != 0)
 			return (false);
 	} while (!atomic_fcmpset_int(&m->ref_count, &old, old | VPRC_BLOCKED));