Index: vm_map.c =================================================================== --- vm_map.c +++ vm_map.c @@ -956,53 +956,66 @@ /* * vm_map_entry_set_max_free: * - * Set the max_free field in a vm_map_entry. + * Compute the size of the largest free gap between two entries, + * one the root of a tree and the other the ancestor of that root + * that is the least or greatest ancestor found on the search path. */ -static inline void -vm_map_entry_set_max_free(vm_map_entry_t entry) +static inline vm_size_t +vm_map_entry_max_free_left(vm_map_entry_t root, vm_map_entry_t left_ancestor) { - vm_map_entry_t child; - vm_size_t max_left, max_right; - child = entry->left; - max_left = (child != NULL) ? child->max_free : - entry->start - entry->prev->end; - child = entry->right; - max_right = (child != NULL) ? child->max_free : - entry->next->start - entry->end; - entry->max_free = MAX(max_left, max_right); + return (root->left != NULL ? + root->left->max_free : root->start - left_ancestor->end); } -#define SPLAY_LEFT_STEP(root, y, rlist, test) do { \ - y = root->left; \ - if (y != NULL && (test)) { \ - /* Rotate right and make y root. */ \ - root->left = y->right; \ - y->right = root; \ - vm_map_entry_set_max_free(root); \ - root = y; \ - y = root->left; \ - } \ - /* Put root on rlist. */ \ - root->left = rlist; \ - rlist = root; \ - root = y; \ +static inline vm_size_t +vm_map_entry_max_free_right(vm_map_entry_t root, vm_map_entry_t right_ancestor) +{ + + return (root->right != NULL ? + root->right->max_free : right_ancestor->start - root->end); +} + +#define SPLAY_LEFT_STEP(root, y, rlist, test) do { \ + y = root->left; \ + root->max_free = vm_map_entry_max_free_right(root, rlist); \ + if (y != NULL && (test)) { \ + /* Rotate right and make y root. */ \ + root->left = y->right; \ + y->right = root; \ + y->max_free = vm_map_entry_max_free_left(root, y); \ + if (y->max_free > root->max_free) \ + root->max_free = y->max_free; \ + else \ + y->max_free = root->max_free; \ + root = y; \ + y = root->left; \ + } \ + /* Put root on rlist. */ \ + root->left = rlist; \ + rlist = root; \ + root = y; \ } while (0) -#define SPLAY_RIGHT_STEP(root, y, llist, test) do { \ - y = root->right; \ - if (y != NULL && (test)) { \ - /* Rotate left and make y root. */ \ - root->right = y->left; \ - y->left = root; \ - vm_map_entry_set_max_free(root); \ - root = y; \ - y = root->right; \ - } \ - /* Put root on llist. */ \ - root->right = llist; \ - llist = root; \ - root = y; \ +#define SPLAY_RIGHT_STEP(root, y, llist, test) do { \ + y = root->right; \ + root->max_free = vm_map_entry_max_free_left(root, llist); \ + if (y != NULL && (test)) { \ + /* Rotate left and make y root. */ \ + root->right = y->left; \ + y->left = root; \ + y->max_free = vm_map_entry_max_free_right(root, y); \ + if (y->max_free > root->max_free) \ + root->max_free = y->max_free; \ + else \ + y->max_free = root->max_free; \ + root = y; \ + y = root->right; \ + } \ + /* Put root on llist. */ \ + root->right = llist; \ + llist = root; \ + root = y; \ } while (0) /* @@ -1015,13 +1028,13 @@ */ static vm_map_entry_t vm_map_splay_split(vm_offset_t addr, vm_size_t length, - vm_map_entry_t root, vm_map_entry_t *out_llist, vm_map_entry_t *out_rlist) + vm_map_entry_t root, vm_map_entry_t *io_llist, vm_map_entry_t *io_rlist) { vm_map_entry_t llist, rlist; vm_map_entry_t y; - llist = NULL; - rlist = NULL; + llist = *io_llist; + rlist = *io_rlist; while (root != NULL && root->max_free >= length) { if (addr < root->start) { SPLAY_LEFT_STEP(root, y, rlist, @@ -1032,8 +1045,8 @@ } else break; } - *out_llist = llist; - *out_rlist = rlist; + *io_llist = llist; + *io_rlist = rlist; return (root); } @@ -1065,40 +1078,48 @@ * Walk back up the two spines, flip the pointers and set max_free. The * subtrees of the root go at the bottom of llist and rlist. */ -static vm_map_entry_t -vm_map_splay_merge(vm_map_entry_t root, - vm_map_entry_t llist, vm_map_entry_t rlist, - vm_map_entry_t ltree, vm_map_entry_t rtree) +static void +vm_map_splay_merge(vm_map_entry_t root, vm_map_entry_t header, + vm_map_entry_t llist, vm_map_entry_t rlist) { - vm_map_entry_t y; + vm_map_entry_t tree, y; + vm_size_t max_free; - while (llist != NULL) { + tree = root->left; + if (llist != header) + max_free = vm_map_entry_max_free_left(root, llist); + while (llist != header) { y = llist->right; - llist->right = ltree; - vm_map_entry_set_max_free(llist); - ltree = llist; + llist->right = tree; + if (llist->max_free < max_free) + llist->max_free = max_free; + else + max_free = llist->max_free; + tree = llist; llist = y; } - while (rlist != NULL) { + root->left = tree; + tree = root->right; + if (rlist != header) + max_free = vm_map_entry_max_free_right(root, rlist); + while (rlist != header) { y = rlist->left; - rlist->left = rtree; - vm_map_entry_set_max_free(rlist); - rtree = rlist; + rlist->left = tree; + if (rlist->max_free < max_free) + rlist->max_free = max_free; + else + max_free = rlist->max_free; + tree = rlist; rlist = y; } - - /* - * Final assembly: add ltree and rtree as subtrees of root. - */ - root->left = ltree; - root->right = rtree; - vm_map_entry_set_max_free(root); - - return (root); + root->right = tree; + root->max_free = MAX( + vm_map_entry_max_free_left(root, llist), + vm_map_entry_max_free_right(root, rlist)); } /* - * vm_map_entry_splay: + * vm_map_splay: * * The Sleator and Tarjan top-down splay algorithm with the * following variation. Max_free must be computed bottom-up, so @@ -1115,14 +1136,15 @@ * Returns: the new root. */ static vm_map_entry_t -vm_map_entry_splay(vm_offset_t addr, vm_map_entry_t root) +vm_map_splay(vm_offset_t addr, vm_map_t map) { - vm_map_entry_t llist, rlist; + vm_map_entry_t llist, rlist, root; - root = vm_map_splay_split(addr, 0, root, &llist, &rlist); + llist = rlist = &map->header; + root = vm_map_splay_split(addr, 0, map->root, &llist, &rlist); if (root != NULL) { /* do nothing */ - } else if (llist != NULL) { + } else if (llist != &map->header) { /* * Recover the greatest node in the left * subtree and make it the root. @@ -1130,7 +1152,7 @@ root = llist; llist = root->right; root->right = NULL; - } else if (rlist != NULL) { + } else if (rlist != &map->header) { /* * Recover the least node in the right * subtree and make it the root. @@ -1142,8 +1164,10 @@ /* There is no root. */ return (NULL); } - return (vm_map_splay_merge(root, llist, rlist, - root->left, root->right)); + vm_map_splay_merge(root, &map->header, llist, rlist); + map->root = root; + VM_MAP_ASSERT_CONSISTENT(map); + return (root); } /* @@ -1162,14 +1186,15 @@ map->nentries, entry); VM_MAP_ASSERT_LOCKED(map); map->nentries++; - root = map->root; - root = vm_map_splay_split(entry->start, 0, root, &llist, &rlist); + llist = rlist = &map->header; + root = vm_map_splay_split(entry->start, 0, map->root, &llist, &rlist); KASSERT(root == NULL, ("vm_map_entry_link: link object already mapped")); - entry->prev = (llist == NULL) ? &map->header : llist; - entry->next = (rlist == NULL) ? &map->header : rlist; - entry->prev->next = entry->next->prev = entry; - root = vm_map_splay_merge(entry, llist, rlist, NULL, NULL); + entry->prev = llist; + entry->next = rlist; + llist->next = rlist->prev = entry; + entry->left = entry->right = NULL; + vm_map_splay_merge(entry, &map->header, llist, rlist); map->root = entry; VM_MAP_ASSERT_CONSISTENT(map); } @@ -1188,12 +1213,8 @@ vm_map_entry_t llist, rlist, root, y; VM_MAP_ASSERT_LOCKED(map); - llist = entry->prev; - rlist = entry->next; - llist->next = rlist; - rlist->prev = llist; - root = map->root; - root = vm_map_splay_split(entry->start, 0, root, &llist, &rlist); + llist = rlist = &map->header; + root = vm_map_splay_split(entry->start, 0, map->root, &llist, &rlist); KASSERT(root != NULL, ("vm_map_entry_unlink: unlink object not mapped")); @@ -1218,11 +1239,11 @@ case UNLINK_MERGE_NONE: vm_map_splay_findprev(root, &llist); vm_map_splay_findnext(root, &rlist); - if (llist != NULL) { + if (llist != &map->header) { root = llist; llist = root->right; root->right = NULL; - } else if (rlist != NULL) { + } else if (rlist != &map->header) { root = rlist; rlist = root->left; root->left = NULL; @@ -1230,9 +1251,11 @@ root = NULL; break; } + y = entry->next; + y->prev = entry->prev; + y->prev->next = y; if (root != NULL) - root = vm_map_splay_merge(root, llist, rlist, - root->left, root->right); + vm_map_splay_merge(root, &map->header, llist, rlist); map->root = root; VM_MAP_ASSERT_CONSISTENT(map); map->nentries--; @@ -1254,15 +1277,15 @@ vm_map_entry_t llist, rlist, root; VM_MAP_ASSERT_LOCKED(map); - root = map->root; - root = vm_map_splay_split(entry->start, 0, root, &llist, &rlist); + llist = rlist = &map->header; + root = vm_map_splay_split(entry->start, 0, map->root, &llist, &rlist); KASSERT(root != NULL, ("%s: resize object not mapped", __func__)); vm_map_splay_findnext(root, &rlist); root->right = NULL; entry->end += grow_amount; - map->root = vm_map_splay_merge(root, llist, rlist, - root->left, root->right); + vm_map_splay_merge(root, &map->header, llist, rlist); + map->root = root; VM_MAP_ASSERT_CONSISTENT(map); CTR4(KTR_VM, "%s: map %p, nentries %d, entry %p", __func__, map, map->nentries, entry); @@ -1308,8 +1331,7 @@ * change the map. Thus, the map's timestamp need not change * on a temporary upgrade. */ - map->root = cur = vm_map_entry_splay(address, cur); - VM_MAP_ASSERT_CONSISTENT(map); + cur = vm_map_splay(address, map); if (!locked) sx_downgrade(&map->lock); @@ -1596,11 +1618,11 @@ * After splay, if start comes before root node, then there * must be a gap from start to the root. */ - root = vm_map_splay_split(start, length, map->root, - &llist, &rlist); + llist = rlist = &map->header; + root = vm_map_splay_split(start, length, map->root, &llist, &rlist); if (root != NULL) start = root->end; - else if (rlist != NULL) { + else if (rlist != &map->header) { root = rlist; rlist = root->left; root->left = NULL; @@ -1609,8 +1631,8 @@ llist = root->right; root->right = NULL; } - map->root = vm_map_splay_merge(root, llist, rlist, - root->left, root->right); + vm_map_splay_merge(root, &map->header, llist, rlist); + map->root = root; VM_MAP_ASSERT_CONSISTENT(map); if (start + length <= root->start) return (start); @@ -1631,39 +1653,33 @@ /* * Splay for the least large-enough gap in the right subtree. */ - llist = NULL; - rlist = NULL; + llist = rlist = &map->header; for (left_length = 0; ; - left_length = root->left != NULL ? - root->left->max_free : root->start - llist->end) { + left_length = vm_map_entry_max_free_left(root, llist)) { if (length <= left_length) SPLAY_LEFT_STEP(root, y, rlist, - length <= (y->left != NULL ? - y->left->max_free : y->start - llist->end)); + length <= vm_map_entry_max_free_left(y, llist)); else SPLAY_RIGHT_STEP(root, y, llist, - length > (y->left != NULL ? - y->left->max_free : y->start - root->end)); + length > vm_map_entry_max_free_left(y, root)); if (root == NULL) break; } root = llist; llist = root->right; - if ((y = rlist) == NULL) + if (rlist == &map->header) root->right = NULL; else { + y = rlist; rlist = y->left; + vm_map_splay_merge(y, &map->header, &map->header, rlist); y->left = NULL; - root->right = y->right; - } - root = vm_map_splay_merge(root, llist, rlist, - root->left, root->right); - if (y != NULL) { - y->right = root->right; - vm_map_entry_set_max_free(y); + y->max_free = MAX( + vm_map_entry_max_free_left(y, root), + vm_map_entry_max_free_right(y, &map->header)); root->right = y; - vm_map_entry_set_max_free(root); } + vm_map_splay_merge(root, &map->header, llist, &map->header); map->root = root; VM_MAP_ASSERT_CONSISTENT(map); return (root->end);