vm: implement lazy-attach cow-duplication of vm-objects attached to a controller

This commit is contained in:
2026-04-19 20:16:19 +01:00
parent b3be4c541b
commit 4a9e907a75
9 changed files with 332 additions and 72 deletions
+102 -43
View File
@@ -9,13 +9,12 @@
#define VM_CONTROLLER_CAST(p) \
OBJECT_C_CAST(struct vm_controller, vc_base, &vm_controller_type, p)
BTREE_DEFINE_SIMPLE_INSERT(struct vm_object, vo_ctrl_node, vo_key, put_object)
BTREE_DEFINE_SIMPLE_GET(
struct vm_object,
equeue_key_t,
vo_ctrl_node,
vo_key,
get_object)
struct vm_request,
uint64_t,
req_node,
req_id,
get_request)
static struct object_type vm_controller_type = {
.ob_name = "vm-controller",
@@ -23,14 +22,14 @@ static struct object_type vm_controller_type = {
.ob_header_offset = offsetof(struct vm_controller, vc_base),
};
static struct vm_cache page_request_cache = {
.c_name = "page-request",
.c_obj_size = sizeof(struct page_request),
static struct vm_cache vm_request_cache = {
.c_name = "vm-request",
.c_obj_size = sizeof(struct vm_request),
};
kern_status_t vm_controller_type_init(void)
{
vm_cache_init(&page_request_cache);
vm_cache_init(&vm_request_cache);
return object_type_register(&vm_controller_type);
}
@@ -51,19 +50,19 @@ struct vm_controller *vm_controller_create(void)
return ctrl;
}
static struct page_request *get_next_request(struct vm_controller *ctrl)
static struct vm_request *get_next_request(struct vm_controller *ctrl)
{
struct btree_node *cur = btree_first(&ctrl->vc_requests);
while (cur) {
struct page_request *req
= BTREE_CONTAINER(struct page_request, req_node, cur);
struct vm_request *req
= BTREE_CONTAINER(struct vm_request, req_node, cur);
spin_lock(&req->req_lock);
switch (req->req_status) {
case PAGE_REQUEST_PENDING:
req->req_status = PAGE_REQUEST_IN_PROGRESS;
case VM_REQUEST_PENDING:
req->req_status = VM_REQUEST_IN_PROGRESS;
ctrl->vc_requests_waiting--;
return req;
case PAGE_REQUEST_ASYNC:
case VM_REQUEST_ASYNC:
btree_delete(&ctrl->vc_requests, &req->req_node);
ctrl->vc_requests_waiting--;
return req;
@@ -78,7 +77,7 @@ static struct page_request *get_next_request(struct vm_controller *ctrl)
return NULL;
}
static kern_status_t try_enqueue(struct btree *tree, struct page_request *req)
static kern_status_t try_enqueue(struct btree *tree, struct vm_request *req)
{
if (!tree->b_root) {
tree->b_root = &req->req_node;
@@ -88,8 +87,8 @@ static kern_status_t try_enqueue(struct btree *tree, struct page_request *req)
struct btree_node *cur = tree->b_root;
while (1) {
struct page_request *cur_node
= BTREE_CONTAINER(struct page_request, req_node, cur);
struct vm_request *cur_node
= BTREE_CONTAINER(struct vm_request, req_node, cur);
struct btree_node *next = NULL;
if (req->req_id > cur_node->req_id) {
@@ -119,7 +118,7 @@ static kern_status_t try_enqueue(struct btree *tree, struct page_request *req)
static kern_status_t send_request_async(
struct vm_controller *ctrl,
struct page_request *req)
struct vm_request *req)
{
fill_random(&req->req_id, sizeof req->req_id);
while (!try_enqueue(&ctrl->vc_requests, req)) {
@@ -136,9 +135,9 @@ static kern_status_t send_request_async(
kern_status_t vm_controller_recv(
struct vm_controller *ctrl,
equeue_packet_page_request_t *out)
equeue_packet_vm_request_t *out)
{
struct page_request *req = NULL;
struct vm_request *req = NULL;
req = get_next_request(ctrl);
if (!req) {
@@ -151,16 +150,30 @@ kern_status_t vm_controller_recv(
VM_CONTROLLER_SIGNAL_REQUEST_RECEIVED);
}
out->req_vmo = req->req_object;
vm_object_lock(req->req_object);
out->req_id = req->req_id;
out->req_vmo = req->req_object->vo_key;
out->req_type = req->req_type;
out->req_offset = req->req_offset;
out->req_length = req->req_length;
switch (req->req_type) {
case VM_REQUEST_READ:
case VM_REQUEST_DIRTY:
out->req_offset = req->req_offset;
out->req_length = req->req_length;
break;
case VM_REQUEST_ATTACH:
out->req_src_vmo = req->req_object->vo_src_key;
break;
default:
break;
}
vm_object_unlock(req->req_object);
spin_unlock(&req->req_lock);
if (req->req_status == PAGE_REQUEST_ASYNC) {
if (req->req_status == VM_REQUEST_ASYNC) {
put_current_thread(req->req_sender);
vm_cache_free(&page_request_cache, req);
vm_cache_free(&vm_request_cache, req);
}
return KERN_OK;
@@ -208,6 +221,53 @@ kern_status_t vm_controller_create_object(
return KERN_OK;
}
kern_status_t vm_controller_prepare_attach(
struct vm_controller *ctrl,
uint64_t req_id,
struct vm_object **out_vmo)
{
struct vm_request *req = get_request(&ctrl->vc_requests, req_id);
if (!req) {
return KERN_INVALID_ARGUMENT;
}
spin_lock(&req->req_lock);
req->req_status = VM_REQUEST_IN_PROGRESS;
*out_vmo = req->req_object;
spin_unlock(&req->req_lock);
return KERN_OK;
}
kern_status_t vm_controller_finish_attach(
struct vm_controller *ctrl,
uint64_t req_id,
equeue_key_t new_key)
{
struct vm_request *req = get_request(&ctrl->vc_requests, req_id);
if (!req) {
return KERN_INVALID_ARGUMENT;
}
spin_lock(&req->req_lock);
struct vm_object *vmo = req->req_object;
spin_unlock(&req->req_lock);
vm_object_lock(vmo);
vmo->vo_key = new_key;
vmo->vo_src_key = 0;
vmo->vo_flags &= ~VMO_LAZY_ATTACH;
vm_object_unlock(vmo);
spin_lock(&req->req_lock);
req->req_status = VM_REQUEST_COMPLETE;
req->req_result = KERN_OK;
thread_awaken(req->req_sender);
spin_unlock(&req->req_lock);
return KERN_OK;
}
kern_status_t vm_controller_detach_object(
struct vm_controller *ctrl,
struct vm_object *vmo)
@@ -216,7 +276,7 @@ kern_status_t vm_controller_detach_object(
return KERN_INVALID_ARGUMENT;
}
if (vmo->vo_key == 0) {
if (vmo->vo_flags & VMO_LAZY_ATTACH) {
/* this vmo isn't actually attached to this controller yet.
* this can happen if a controller-attached vmo was duplicated
* via copy-on-write, and the duplicate vmo has not yet been
@@ -225,16 +285,14 @@ kern_status_t vm_controller_detach_object(
return KERN_OK;
}
struct page_request *req
= vm_cache_alloc(&page_request_cache, VM_NORMAL);
req->req_type = PAGE_REQUEST_DETACH;
req->req_status = PAGE_REQUEST_ASYNC;
req->req_object = vmo->vo_key;
struct vm_request *req = vm_cache_alloc(&vm_request_cache, VM_NORMAL);
req->req_type = VM_REQUEST_DETACH;
req->req_status = VM_REQUEST_ASYNC;
req->req_object = vmo;
req->req_sender = get_current_thread();
send_request_async(ctrl, req);
vmo->vo_ctrl = NULL;
vmo->vo_key = 0;
object_unref(&ctrl->vc_base);
return KERN_OK;
@@ -242,7 +300,7 @@ kern_status_t vm_controller_detach_object(
static void wait_for_reply(
struct vm_controller *ctrl,
struct page_request *req,
struct vm_request *req,
unsigned long *lock_flags)
{
struct wait_item waiter;
@@ -251,7 +309,7 @@ static void wait_for_reply(
wait_item_init(&waiter, self);
for (;;) {
self->tr_state = THREAD_SLEEPING;
if (req->req_status == PAGE_REQUEST_COMPLETE) {
if (req->req_status == VM_REQUEST_COMPLETE) {
break;
}
@@ -264,7 +322,7 @@ static void wait_for_reply(
put_current_thread(self);
}
static void fulfill_requests(
void vm_controller_fulfill_requests(
struct vm_controller *ctrl,
equeue_key_t object,
off_t offset,
@@ -274,8 +332,8 @@ static void fulfill_requests(
off_t limit = offset + length - 1;
struct btree_node *cur = btree_first(&ctrl->vc_requests);
while (cur) {
struct page_request *req
= BTREE_CONTAINER(struct page_request, req_node, cur);
struct vm_request *req
= BTREE_CONTAINER(struct vm_request, req_node, cur);
spin_lock(&req->req_lock);
bool match = false;
off_t req_base = req->req_offset;
@@ -287,12 +345,14 @@ static void fulfill_requests(
match = true;
}
if (req->req_object != object) {
vm_object_lock(req->req_object);
if (req->req_object->vo_key != object) {
match = false;
}
vm_object_unlock(req->req_object);
if (match) {
req->req_status = PAGE_REQUEST_COMPLETE;
req->req_status = VM_REQUEST_COMPLETE;
req->req_result = result;
thread_awaken(req->req_sender);
}
@@ -325,14 +385,13 @@ kern_status_t vm_controller_supply_pages(
src_offset,
count,
NULL);
fulfill_requests(ctrl, dst->vo_key, dst_offset, count, status);
return status;
}
kern_status_t vm_controller_send_request(
struct vm_controller *ctrl,
struct page_request *req,
struct vm_request *req,
unsigned long *irq_flags)
{
fill_random(&req->req_id, sizeof req->req_id);