Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 26 Oct 2024 12:59:59 GMT
From:      Mark Johnston <markj@FreeBSD.org>
To:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org
Subject:   git: b947b53f0f9f - stable/14 - vm_page: Fix a logic bug in vm_page_unwire_managed()
Message-ID:  <202410261259.49QCxxA8062610@gitrepo.freebsd.org>

next in thread | raw e-mail | index | archive | help
The branch stable/14 has been updated by markj:

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

commit b947b53f0f9fc7a9ab265dfaf1548e1a1d96e2f3
Author:     Mark Johnston <markj@FreeBSD.org>
AuthorDate: 2024-10-07 20:50:49 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2024-10-26 12:58:50 +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
    
    (cherry picked from commit c59166e5b4e8821556a3d23af7bd17ca556f2e22)
---
 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 7c3083de42de..3713fe17dd13 100644
--- a/sys/vm/vm_page.c
+++ b/sys/vm/vm_page.c
@@ -4090,10 +4090,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
@@ -4102,7 +4105,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
@@ -4111,7 +4114,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));
@@ -4387,6 +4390,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));



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202410261259.49QCxxA8062610>