Files
cool_fox_newer/patches/wineserver-view-tree.patch
Hasusmogus 71871a6a64 patches/: the wineserver view-tree patch docs/linux.md describes
Against GE-Proton11-14's wine (ValveSoftware/wine a47c73f with GE's
protonprep wine section applied): a process's views in a tree by base
as well as the list, so map_view's overlap check and unmap_view's
lookup no longer walk all of them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-26 22:23:29 +01:00

212 lines
8.0 KiB
Diff

diff --git a/server/file.h b/server/file.h
index c64ac913..fec7711c 100644
--- a/server/file.h
+++ b/server/file.h
@@ -184,6 +184,7 @@ struct memory_view;
extern void init_memory(void);
extern int grow_file( int unix_fd, file_pos_t new_size );
extern void free_map_addr( client_ptr_t base, mem_size_t size );
+extern void init_process_view_tree( struct process *process );
extern struct memory_view *find_mapped_view( struct process *process, client_ptr_t base );
extern struct memory_view *get_exe_view( struct process *process );
extern struct file *get_view_file( const struct memory_view *view, unsigned int access, unsigned int sharing );
diff --git a/server/mapping.c b/server/mapping.c
index a3f54ac5..3da293cd 100644
--- a/server/mapping.c
+++ b/server/mapping.c
@@ -131,6 +131,8 @@ static struct list shared_map_list = LIST_INIT( shared_map_list );
struct memory_view
{
struct list entry; /* entry in per-process view list */
+ struct rb_entry tree_entry; /* entry in per-process view tree, by base */
+ int in_tree; /* whether tree_entry is in the tree */
struct fd *fd; /* fd for mapped file */
struct ranges *committed; /* list of committed ranges in this mapping */
struct shared_map *shared; /* temp file for shared PE mapping */
@@ -431,11 +433,61 @@ static int create_temp_file( file_pos_t size )
return fd;
}
+/* The views of a process are also kept in a tree by base address: with
+ * the list alone every map searched all of them for an overlap, and every
+ * unmap for the view; a process with tens of thousands of views (a console
+ * title's physical page views, mapped and unmapped as it streams) spent a
+ * millisecond in each, while its client held the virtual memory lock. Views
+ * never overlap (every add is checked first), so the view with the greatest
+ * base at or below an address is the only one that can hold it. */
+static int view_tree_compare( const void *key, const struct rb_entry *entry )
+{
+ client_ptr_t base = *(const client_ptr_t *)key;
+ const struct memory_view *view = RB_ENTRY_VALUE( entry, const struct memory_view, tree_entry );
+
+ if (base < view->base) return -1;
+ if (base > view->base) return 1;
+ return 0;
+}
+
+void init_process_view_tree( struct process *process )
+{
+ rb_init( &process->view_tree, view_tree_compare );
+ process->view_tree_bad = 0;
+}
+
+/* the view with the greatest base not above addr, from the tree */
+static struct memory_view *view_at_or_below( struct process *process, client_ptr_t addr )
+{
+ struct rb_entry *entry = process->view_tree.root;
+ struct memory_view *best = NULL;
+
+ while (entry)
+ {
+ struct memory_view *view = RB_ENTRY_VALUE( entry, struct memory_view, tree_entry );
+ if (view->base <= addr)
+ {
+ best = view;
+ entry = entry->right;
+ }
+ else entry = entry->left;
+ }
+ return best;
+}
+
/* find a memory view from its base address */
struct memory_view *find_mapped_view( struct process *process, client_ptr_t base )
{
struct memory_view *view;
+ if (!process->view_tree_bad)
+ {
+ struct rb_entry *entry = rb_get( &process->view_tree, &base );
+ if (entry) return RB_ENTRY_VALUE( entry, struct memory_view, tree_entry );
+ set_error( STATUS_NOT_MAPPED_VIEW );
+ return NULL;
+ }
+
LIST_FOR_EACH_ENTRY( view, &process->views, struct memory_view, entry )
if (view->base == base) return view;
@@ -448,6 +500,14 @@ static struct memory_view *find_mapped_addr( struct process *process, client_ptr
{
struct memory_view *view;
+ if (!process->view_tree_bad)
+ {
+ view = view_at_or_below( process, addr );
+ if (view && addr < view->base + view->size) return view;
+ set_error( STATUS_NOT_MAPPED_VIEW );
+ return NULL;
+ }
+
LIST_FOR_EACH_ENTRY( view, &process->views, struct memory_view, entry )
if (addr >= view->base && addr < view->base + view->size) return view;
@@ -464,6 +524,13 @@ static int is_valid_view_addr( struct process *process, client_ptr_t addr, mem_s
if (addr & host_page_mask) return 0;
if (addr + size < addr) return 0; /* overflow */
+ if (!process->view_tree_bad)
+ {
+ /* of the views beginning below the range's end, the last ends last */
+ view = view_at_or_below( process, addr + size - 1 );
+ return !view || view->base + view->size <= addr;
+ }
+
/* check for overlapping view */
LIST_FOR_EACH_ENTRY( view, &process->views, struct memory_view, entry )
{
@@ -474,6 +541,19 @@ static int is_valid_view_addr( struct process *process, client_ptr_t addr, mem_s
return 1;
}
+static void view_tree_add( struct process *process, struct memory_view *view )
+{
+ view->in_tree = 0;
+ if (process->view_tree_bad) return;
+ if (rb_put( &process->view_tree, &view->base, &view->tree_entry ))
+ {
+ /* two views at one base: the list answers from now on */
+ process->view_tree_bad = 1;
+ return;
+ }
+ view->in_tree = 1;
+}
+
/* get the main exe memory view */
struct memory_view *get_exe_view( struct process *process )
{
@@ -509,19 +589,22 @@ static int add_process_view( struct thread *thread, struct memory_view *view )
process->imagelen = name.len;
process->image_info = view->image;
list_add_head( &process->views, &view->entry );
+ view_tree_add( process, view );
return 1;
}
}
list_add_tail( &process->views, &view->entry );
+ view_tree_add( process, view );
return 0;
}
-static void free_memory_view( struct memory_view *view )
+static void free_memory_view( struct process *process, struct memory_view *view )
{
if (view->fd) release_object( view->fd );
if (view->committed) release_object( view->committed );
if (view->shared) release_object( view->shared );
list_remove( &view->entry );
+ if (view->in_tree) rb_remove( &process->view_tree, &view->tree_entry );
free( view );
}
@@ -531,7 +614,7 @@ void free_mapped_views( struct process *process )
struct list *ptr;
while ((ptr = list_head( &process->views )))
- free_memory_view( LIST_ENTRY( ptr, struct memory_view, entry ));
+ free_memory_view( process, LIST_ENTRY( ptr, struct memory_view, entry ));
}
/* find the shared PE mapping for a given mapping */
@@ -1859,7 +1942,7 @@ DECL_HANDLER(unmap_view)
if (!view) return;
generate_dll_event( current, DbgUnloadDllStateChange, view );
- free_memory_view( view );
+ free_memory_view( current->process, view );
}
/* get information about a mapped image view */
diff --git a/server/process.c b/server/process.c
index 7cf792de..dfb14f3e 100644
--- a/server/process.c
+++ b/server/process.c
@@ -715,6 +715,7 @@ struct process *create_process( int fd, struct process *parent, unsigned int fla
list_init( &process->asyncs );
list_init( &process->classes );
list_init( &process->views );
+ init_process_view_tree( process );
process->end_time = 0;
diff --git a/server/process.h b/server/process.h
index 2244f4d4..87f1d6d1 100644
--- a/server/process.h
+++ b/server/process.h
@@ -22,6 +22,7 @@
#define __WINE_SERVER_PROCESS_H
#include "object.h"
+#include "wine/rbtree.h"
struct atom_table;
struct handle_table;
@@ -79,6 +80,8 @@ struct process
obj_handle_t desktop; /* handle to desktop to use for new threads */
struct token *token; /* security token associated with this process */
struct list views; /* list of memory views */
+ struct rb_tree view_tree; /* the same views by base address */
+ int view_tree_bad; /* a view could not be put in the tree: use the list */
client_ptr_t peb; /* PEB address in client address space */
struct dir_cache *dir_cache; /* map of client-side directory cache */
unsigned int trace_data; /* opaque data used by the process tracing mechanism */