diff
    int i, ret = 0;
    __u32 version;
+   bool monitor_pages_allocated = false;

    // ... (existing code)

    vmbus_connection.monitor_pages[0] = hv_alloc_hyperv_page();
    vmbus_connection.monitor_pages[1] = hv_alloc_hyperv_page();
    if ((vmbus_connection.monitor_pages[0] == NULL) ||
        (vmbus_connection.monitor_pages[1] == NULL)) {
        ret = -ENOMEM;
        goto cleanup;
    }
+   monitor_pages_allocated = true;

    // ... (rest of the function)

cleanup:
    pr_err("Unable to connect to host\n");
    vmbus_connection.conn_state = DISCONNECTED;
+   if (monitor_pages_allocated) {
+       hv_free_hyperv_page(vmbus_connection.monitor_pages[0]);
+       hv_free_hyperv_page(vmbus_connection.monitor_pages[1]);
+       vmbus_connection.monitor_pages[0] = NULL;
+       vmbus_connection.monitor_pages[1] = NULL;
+   }
    vmbus_disconnect();
