svn commit: r357091 - head/sys/vm

Konstantin Belousov kib at FreeBSD.org
Fri Jan 24 19:42:54 UTC 2020


Author: kib
Date: Fri Jan 24 19:42:53 2020
New Revision: 357091
URL: https://svnweb.freebsd.org/changeset/base/357091

Log:
  Handle a race of collapse with a retrying fault.
  
  Both vm_object_scan_all_shadowed() and vm_object_collapse_scan() might
  observe an invalid page left in the default backing object by the
  fault handler that retried.  Check for the condition and refuse to collapse.
  
  Reported and tested by:	pho
  Reviewed by:	jeff
  Sponsored by:	The FreeBSD Foundation
  Differential revision:	https://reviews.freebsd.org/D23331

Modified:
  head/sys/vm/vm_object.c

Modified: head/sys/vm/vm_object.c
==============================================================================
--- head/sys/vm/vm_object.c	Fri Jan 24 17:24:02 2020	(r357090)
+++ head/sys/vm/vm_object.c	Fri Jan 24 19:42:53 2020	(r357091)
@@ -1700,14 +1700,25 @@ vm_object_scan_all_shadowed(vm_object_t object)
 		if (new_pindex >= object->size)
 			break;
 
-		/*
-		 * If the backing object page is busy a grandparent or older
-		 * page may still be undergoing CoW.  It is not safe to
-		 * collapse the backing object until it is quiesced.
-		 */
-		if (p != NULL && vm_page_busied(p))
-			return (false);
+		if (p != NULL) {
+			/*
+			 * If the backing object page is busy a
+			 * grandparent or older page may still be
+			 * undergoing CoW.  It is not safe to collapse
+			 * the backing object until it is quiesced.
+			 */
+			if (vm_page_tryxbusy(p) == 0)
+				return (false);
 
+			/*
+			 * We raced with the fault handler that left
+			 * newly allocated invalid page on the object
+			 * queue and retried.
+			 */
+			if (!vm_page_all_valid(p))
+				goto unbusy_ret;
+		}
+
 		/*
 		 * See if the parent has the page or if the parent's object
 		 * pager has the page.  If the parent has the page but the page
@@ -1717,15 +1728,24 @@ vm_object_scan_all_shadowed(vm_object_t object)
 		 * object and we might as well give up now.
 		 */
 		pp = vm_page_lookup(object, new_pindex);
+
 		/*
-		 * The valid check here is stable due to object lock being
-		 * required to clear valid and initiate paging.
+		 * The valid check here is stable due to object lock
+		 * being required to clear valid and initiate paging.
+		 * Busy of p disallows fault handler to validate pp.
 		 */
 		if ((pp == NULL || vm_page_none_valid(pp)) &&
 		    !vm_pager_has_page(object, new_pindex, NULL, NULL))
-			return (false);
+			goto unbusy_ret;
+		if (p != NULL)
+			vm_page_xunbusy(p);
 	}
 	return (true);
+
+unbusy_ret:
+	if (p != NULL)
+		vm_page_xunbusy(p);
+	return (false);
 }
 
 static void
@@ -1769,6 +1789,14 @@ vm_object_collapse_scan(vm_object_t object)
 				swap_pager_freespace(backing_object, p->pindex,
 				    1);
 
+			KASSERT(!pmap_page_is_mapped(p),
+			    ("freeing mapped page %p", p));
+			if (vm_page_remove(p))
+				vm_page_free(p);
+			continue;
+		}
+
+		if (!vm_page_all_valid(p)) {
 			KASSERT(!pmap_page_is_mapped(p),
 			    ("freeing mapped page %p", p));
 			if (vm_page_remove(p))


More information about the svn-src-all mailing list