vc4/simulator: avoid free simulator memory on destroy

The vc4 simulator only allows the memory to be initialized
once, as it would assert if simpenrose_init_hardware_supply_mem
is called twice.

So if a screen_create is called after all previous screens have been
destroyed this raises the assertion, and this happens with CTS
when using a surfaceless target.

To avoid the simulator assertion, we need to guarantee that
simpenrose_init_hardware_supply_mem is only called once
so we never can free the supplied memory to the simulator on
the screen_destroy of the last screen using the simulator.

It can be assumed that the simulator memory will be freed at
the end of the program execution.

Reviewed-by: Juan A. Suarez <jasuarez@igalia.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/36899>
This commit is contained in:
Jose Maria Casanova Crespo 2025-08-20 15:14:57 +02:00 committed by Marge Bot
parent 99a5006a38
commit ecf8aa2700

View file

@ -80,6 +80,7 @@ static struct vc4_simulator_state {
int refcount;
} sim_state = {
.mutex = SIMPLE_MTX_INITIALIZER,
.mem = NULL,
};
enum gem_type {
@ -704,16 +705,23 @@ vc4_simulator_init_global(void)
return;
}
sim_state.mem_size = 256 * 1024 * 1024;
sim_state.mem = calloc(sim_state.mem_size, 1);
if (!sim_state.mem)
abort();
sim_state.heap = u_mmInit(0, sim_state.mem_size);
/* We supply our own memory so that we can have more aperture
* available (256MB instead of simpenrose's default 64MB).
/* We only allocate once the memory supplied to the simulator, and we
* will never free it unless execution ends. So if the simulator is
* initialized after being destroyed, it doesn't assert because of
* memory being re-initialized.
*/
simpenrose_init_hardware_supply_mem(sim_state.mem, sim_state.mem_size);
if (!sim_state.mem) {
sim_state.mem_size = 256 * 1024 * 1024;
sim_state.mem = calloc(sim_state.mem_size, 1);
if (!sim_state.mem)
abort();
/* We supply our own memory so that we can have more aperture
* available (256MB instead of simpenrose's default 64MB).
*/
simpenrose_init_hardware_supply_mem(sim_state.mem, sim_state.mem_size);
}
sim_state.heap = u_mmInit(0, sim_state.mem_size);
/* Carve out low memory for tile allocation overflow. The kernel
* should be automatically handling overflow memory setup on real
@ -773,7 +781,7 @@ vc4_simulator_destroy(struct vc4_simulator_file *sim_file)
if (!--sim_state.refcount) {
_mesa_hash_table_destroy(sim_state.fd_map, NULL);
u_mmDestroy(sim_state.heap);
free(sim_state.mem);
/* We don't free the simulator allocated memory */
/* No memsetting it, because it contains the mutex. */
}
ralloc_free(sim_file);