diff --git a/sys/vm/vm_map.c b/sys/vm/vm_map.c index 6bcebd543096..ea9bb17c57f4 100644 --- a/sys/vm/vm_map.c +++ b/sys/vm/vm_map.c @@ -1655,6 +1655,7 @@ _vm_map_clip_start(vm_map_t map, vm_map_entry_t entry, vm_offset_t start) vm_map_entry_t new_entry; VM_MAP_ASSERT_LOCKED(map); + KASSERT(entry->end > start, ("invalid clip of entry %p", entry)); /* * Split off the front portion -- note that we must insert the new @@ -1739,6 +1740,7 @@ _vm_map_clip_end(vm_map_t map, vm_map_entry_t entry, vm_offset_t end) vm_map_entry_t new_entry; VM_MAP_ASSERT_LOCKED(map); + KASSERT(entry->start < end, ("invalid clip of entry %p", entry)); /* * If there is no object backing this entry, we might as well create @@ -1960,9 +1962,16 @@ vm_map_protect(vm_map_t map, vm_offset_t start, vm_offset_t end, return (KERN_SUCCESS); vm_map_lock(map); - VM_MAP_RANGE_CHECK(map, start, end); + /* + * Ensure that we are not concurrently wiring pages. vm_map_wire() may + * need to fault pages into the map and will drop the map lock while + * doing so, and the VM object may end up in an inconsistent state if we + * update the protection on the map entry in between faults. + */ + vm_map_wait_busy(map); + if (vm_map_lookup_entry(map, start, &entry)) { vm_map_clip_start(map, entry, start); } else { @@ -1972,8 +1981,8 @@ vm_map_protect(vm_map_t map, vm_offset_t start, vm_offset_t end, /* * Make a first pass to check for protection violations. */ - current = entry; - while ((current != &map->header) && (current->start < end)) { + for (current = entry; current != &map->header && current->start < end; + current = current->next) { if (current->eflags & MAP_ENTRY_IS_SUB_MAP) { vm_map_unlock(map); return (KERN_INVALID_ARGUMENT); @@ -1982,17 +1991,15 @@ vm_map_protect(vm_map_t map, vm_offset_t start, vm_offset_t end, vm_map_unlock(map); return (KERN_PROTECTION_FAILURE); } - current = current->next; } - /* * Do an accounting pass for private read-only mappings that * now will do cow due to allowed write (e.g. debugger sets * breakpoint on text segment) */ - for (current = entry; (current != &map->header) && - (current->start < end); current = current->next) { + for (current = entry; current != &map->header && current->start < end; + current = current->next) { vm_map_clip_end(map, current, end); @@ -2045,8 +2052,8 @@ vm_map_protect(vm_map_t map, vm_offset_t start, vm_offset_t end, * Go back and fix up protections. [Note that clipping is not * necessary the second time.] */ - current = entry; - while ((current != &map->header) && (current->start < end)) { + for (current = entry; current != &map->header && current->start < end; + current = current->next) { old_prot = current->protection; if (set_max) @@ -2080,7 +2087,6 @@ vm_map_protect(vm_map_t map, vm_offset_t start, vm_offset_t end, #undef MASK } vm_map_simplify_entry(map, current); - current = current->next; } vm_map_unlock(map); return (KERN_SUCCESS);