Chuck Ebbert 121bfe2
diff --git a/kernel/resource.c b/kernel/resource.c
Chuck Ebbert 121bfe2
index ace2269..1a2a40e 100644
Chuck Ebbert 121bfe2
--- a/kernel/resource.c
Chuck Ebbert 121bfe2
+++ b/kernel/resource.c
Chuck Ebbert 121bfe2
@@ -358,6 +358,20 @@ int __weak page_is_ram(unsigned long pfn)
Chuck Ebbert 121bfe2
 }
Chuck Ebbert 121bfe2
 
Chuck Ebbert 121bfe2
 /*
Chuck Ebbert 121bfe2
+ * Find the resource before "child" in the sibling list of "root" children.
Chuck Ebbert 121bfe2
+ */
Chuck Ebbert 121bfe2
+static struct resource *find_sibling_prev(struct resource *root, struct resource *child)
Chuck Ebbert 121bfe2
+{
Chuck Ebbert 121bfe2
+	struct resource *this;
Chuck Ebbert 121bfe2
+
Chuck Ebbert 121bfe2
+	for (this = root->child; this; this = this->sibling)
Chuck Ebbert 121bfe2
+		if (this->sibling == child)
Chuck Ebbert 121bfe2
+			return this;
Chuck Ebbert 121bfe2
+
Chuck Ebbert 121bfe2
+	return NULL;
Chuck Ebbert 121bfe2
+}
Chuck Ebbert 121bfe2
+
Chuck Ebbert 121bfe2
+/*
Chuck Ebbert 121bfe2
  * Find empty slot in the resource tree given range and alignment.
Chuck Ebbert 121bfe2
  */
Chuck Ebbert 121bfe2
 static int find_resource(struct resource *root, struct resource *new,
Chuck Ebbert 121bfe2
@@ -369,24 +383,18 @@ static int find_resource(struct resource *root, struct resource *new,
Chuck Ebbert 121bfe2
 						   resource_size_t),
Chuck Ebbert 121bfe2
 			 void *alignf_data)
Chuck Ebbert 121bfe2
 {
Chuck Ebbert 121bfe2
-	struct resource *this = root->child;
Chuck Ebbert 121bfe2
+	struct resource *this;
Chuck Ebbert 121bfe2
 	struct resource tmp = *new;
Chuck Ebbert 121bfe2
 	resource_size_t start;
Chuck Ebbert 121bfe2
 
Chuck Ebbert 121bfe2
-	tmp.start = root->start;
Chuck Ebbert 121bfe2
-	/*
Chuck Ebbert 121bfe2
-	 * Skip past an allocated resource that starts at 0, since the assignment
Chuck Ebbert 121bfe2
-	 * of this->start - 1 to tmp->end below would cause an underflow.
Chuck Ebbert 121bfe2
-	 */
Chuck Ebbert 121bfe2
-	if (this && this->start == 0) {
Chuck Ebbert 121bfe2
-		tmp.start = this->end + 1;
Chuck Ebbert 121bfe2
-		this = this->sibling;
Chuck Ebbert 121bfe2
-	}
Chuck Ebbert 121bfe2
-	for(;;) {
Chuck Ebbert 121bfe2
+	tmp.end = root->end;
Chuck Ebbert 121bfe2
+
Chuck Ebbert 121bfe2
+	this = find_sibling_prev(root, NULL);
Chuck Ebbert 121bfe2
+	for (;;) {
Chuck Ebbert 121bfe2
 		if (this)
Chuck Ebbert 121bfe2
-			tmp.end = this->start - 1;
Chuck Ebbert 121bfe2
+			tmp.start = this->end + 1;
Chuck Ebbert 121bfe2
 		else
Chuck Ebbert 121bfe2
-			tmp.end = root->end;
Chuck Ebbert 121bfe2
+			tmp.start = root->start;
Chuck Ebbert 121bfe2
 		if (tmp.start < min)
Chuck Ebbert 121bfe2
 			tmp.start = min;
Chuck Ebbert 121bfe2
 		if (tmp.end > max)
Chuck Ebbert 121bfe2
@@ -404,10 +412,10 @@ static int find_resource(struct resource *root, struct resource *new,
Chuck Ebbert 121bfe2
 			new->end = tmp.start + size - 1;
Chuck Ebbert 121bfe2
 			return 0;
Chuck Ebbert 121bfe2
 		}
Chuck Ebbert 121bfe2
-		if (!this)
Chuck Ebbert 121bfe2
+		if (!this || this->start == root->start)
Chuck Ebbert 121bfe2
 			break;
Chuck Ebbert 121bfe2
-		tmp.start = this->end + 1;
Chuck Ebbert 121bfe2
-		this = this->sibling;
Chuck Ebbert 121bfe2
+		tmp.end = this->start - 1;
Chuck Ebbert 121bfe2
+		this = find_sibling_prev(root, this);
Chuck Ebbert 121bfe2
 	}
Chuck Ebbert 121bfe2
 	return -EBUSY;
Chuck Ebbert 121bfe2
 }