git: 7b0fe2d405ae - main - vm_domainset: Ensure round-robin works properly

From: Olivier Certner <olce_at_FreeBSD.org>
Date: Fri, 17 Oct 2025 15:19:11 UTC
The branch main has been updated by olce:

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

commit 7b0fe2d405ae09b1247bccc6fa45a6d2755cbe4c
Author:     Olivier Certner <olce@FreeBSD.org>
AuthorDate: 2025-09-25 14:40:30 +0000
Commit:     Olivier Certner <olce@FreeBSD.org>
CommitDate: 2025-10-17 15:18:53 +0000

    vm_domainset: Ensure round-robin works properly
    
    All iterators that rely on an object's 'struct domainset_ref' (field
    'domain' on 'struct vm_object'), which is the case for page allocations
    with objects, are used with the corresponding object locked for writing,
    so cannot lose concurrent iterator index's increases even if those are
    made without atomic operations.  The only offender was thread stack
    allocation, which has just been fixed in commit 3b9b64457676 ("vm: Fix
    iterator usage in vm_thread_stack_create()").
    
    However, the interleaved policy would still reset the iterator index
    when restarting, losing track of the next domain to allocate from when
    applying round-robin, which all allocation policies do if allocation
    from the first domain fails.
    
    Fix this last round-robin problem by not resetting the shared index at
    iterator's phase init on DOMAINSET_POLICY_INTERLEAVE.
    
    Add an assertion to check that, when passed, an object is write-locked
    in order to prevent the problem mentioned in the first paragraph from
    reappearing.
    
    Reviewed by:    markj
    MFC after:      1 week
    Sponsored by:   The FreeBSD Foundation
    Differential Revision:  https://reviews.freebsd.org/D52733
---
 sys/vm/vm_domainset.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/sys/vm/vm_domainset.c b/sys/vm/vm_domainset.c
index 9fa17da954f7..c25ed0cc2267 100644
--- a/sys/vm/vm_domainset.c
+++ b/sys/vm/vm_domainset.c
@@ -113,7 +113,6 @@ vm_domainset_iter_interleave(struct vm_domainset_iter *di, int *domain)
 	int d;
 
 	d = di->di_offset % di->di_domain->ds_cnt;
-	*di->di_iter = d;
 	*domain = di->di_domain->ds_order[d];
 }
 
@@ -260,9 +259,14 @@ vm_domainset_iter_page_init(struct vm_domainset_iter *di, struct vm_object *obj,
 	 * are immutable and unsynchronized.  Updates can race but pointer
 	 * loads are assumed to be atomic.
 	 */
-	if (obj != NULL && obj->domain.dr_policy != NULL)
+	if (obj != NULL && obj->domain.dr_policy != NULL) {
+		/*
+		 * This write lock protects non-atomic increments of the
+		 * iterator index in vm_domainset_iter_rr().
+		 */
+		VM_OBJECT_ASSERT_WLOCKED(obj);
 		dr = &obj->domain;
-	else
+	} else
 		dr = &curthread->td_domain;
 
 	vm_domainset_iter_init(di, dr->dr_policy, &dr->dr_iter, obj, pindex);