From 385301db2f646faa18cf4125f177897b182f0137 Mon Sep 17 00:00:00 2001 From: Erik Faye-Lund Date: Thu, 14 Nov 2024 17:51:14 +0100 Subject: [PATCH] panfrost: try to survive start-up alloc fails If we fail to allocate memory this early on, we would otherwise fall over and die. This propagates the errors up to the caller. Reviewed-by: Eric R. Smith Part-of: --- src/gallium/drivers/panfrost/pan_device.c | 22 +++++++++++++++++----- src/gallium/drivers/panfrost/pan_device.h | 2 +- src/gallium/drivers/panfrost/pan_screen.c | 6 +++++- 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/src/gallium/drivers/panfrost/pan_device.c b/src/gallium/drivers/panfrost/pan_device.c index 63d6ca52dba..7bd310db42a 100644 --- a/src/gallium/drivers/panfrost/pan_device.c +++ b/src/gallium/drivers/panfrost/pan_device.c @@ -50,7 +50,7 @@ panfrost_supports_compressed_format(struct panfrost_device *dev, return dev->compressed_formats & BITFIELD_BIT(texfeat_bit); } -void +int panfrost_open_device(void *memctx, int fd, struct panfrost_device *dev) { dev->memctx = memctx; @@ -58,7 +58,7 @@ panfrost_open_device(void *memctx, int fd, struct panfrost_device *dev) dev->kmod.dev = pan_kmod_dev_create(fd, PAN_KMOD_DEV_FLAG_OWNS_FD, NULL); if (!dev->kmod.dev) { close(fd); - return; + return -1; } pan_kmod_dev_query_props(dev->kmod.dev, &dev->kmod.props); @@ -120,7 +120,8 @@ panfrost_open_device(void *memctx, int fd, struct panfrost_device *dev) if (dev->arch < 10) { dev->tiler_heap = panfrost_bo_create( dev, 128 * 1024 * 1024, PAN_BO_INVISIBLE | PAN_BO_GROWABLE, "Tiler heap"); - assert(dev->tiler_heap); + if (!dev->tiler_heap) + goto err_free_kmod_dev; } pthread_mutex_init(&dev->submit_lock, NULL); @@ -128,14 +129,25 @@ panfrost_open_device(void *memctx, int fd, struct panfrost_device *dev) /* Done once on init */ dev->sample_positions = panfrost_bo_create( dev, panfrost_sample_positions_buffer_size(), 0, "Sample positions"); - assert(dev->sample_positions); + if (!dev->sample_positions) + goto err_free_kmod_dev; panfrost_upload_sample_positions(dev->sample_positions->ptr.cpu); - return; + return 0; err_free_kmod_dev: + if (dev->decode_ctx) + pandecode_destroy_context(dev->decode_ctx); + + panfrost_bo_unreference(dev->tiler_heap); + panfrost_bo_unreference(dev->sample_positions); + + if (dev->kmod.vm) + pan_kmod_vm_destroy(dev->kmod.vm); + pan_kmod_dev_destroy(dev->kmod.dev); dev->kmod.dev = NULL; + return -1; } void diff --git a/src/gallium/drivers/panfrost/pan_device.h b/src/gallium/drivers/panfrost/pan_device.h index b985b05ea13..4795fdba1b7 100644 --- a/src/gallium/drivers/panfrost/pan_device.h +++ b/src/gallium/drivers/panfrost/pan_device.h @@ -209,7 +209,7 @@ panfrost_device_kmod_version_minor(const struct panfrost_device *dev) return dev->kmod.dev->driver.version.minor; } -void panfrost_open_device(void *memctx, int fd, struct panfrost_device *dev); +int panfrost_open_device(void *memctx, int fd, struct panfrost_device *dev); void panfrost_close_device(struct panfrost_device *dev); diff --git a/src/gallium/drivers/panfrost/pan_screen.c b/src/gallium/drivers/panfrost/pan_screen.c index 4baeec9bdfa..210b340fb5e 100644 --- a/src/gallium/drivers/panfrost/pan_screen.c +++ b/src/gallium/drivers/panfrost/pan_screen.c @@ -950,7 +950,11 @@ panfrost_create_screen(int fd, const struct pipe_screen_config *config, debug_get_flags_option("PAN_MESA_DEBUG", panfrost_debug_options, 0); screen->max_afbc_packing_ratio = debug_get_num_option( "PAN_MAX_AFBC_PACKING_RATIO", DEFAULT_MAX_AFBC_PACKING_RATIO); - panfrost_open_device(screen, fd, dev); + + if (panfrost_open_device(screen, fd, dev)) { + ralloc_free(screen); + return NULL; + } if (dev->debug & PAN_DBG_NO_AFBC) dev->has_afbc = false;