drm/tegra: Fix error handling cleanup
PATCH 1/3 drm/tegra: Fix error handling cleanup
From: Thierry Reding <[email protected]>
The DRM driver's ->load() implementation didn't do a good job (no job at all really) cleaning up on failure. Fix that by undoing any prior setup when an error occurs. This requires a bit of rework to make it possible to clean up fbdev midway.
This was tested by injecting errors at various points during the initialization sequence and verifying that error cleanup didn't crash and no memory leaked (using kmemleak).
--- drivers/gpu/drm/tegra/drm.c | 20 ++++++++++++++++---- drivers/gpu/drm/tegra/drm.h | 1 + drivers/gpu/drm/tegra/fb.c | 20 +++++++++++++++++--- 3 files changed, 34 insertions(+), 7 deletions(-)
drivers/gpu/drm/tegra/drm.c +16 -4
@@ -42,13 +42,13 @@ static int tegra_drm_load(struct drm_device *drm, unsigned long flags) err =tegra_drm_fb_prepare (drm);if (err <0 ) -return err; + goto config;drm_kms_helper_poll_init (drm); err =host1x_device_init (device);if (err <0 ) -return err; + goto fbdev;/* * We don't use the drm_irq_install() helpers provided by the DRM
@@ -59,13 +59,25 @@ static int tegra_drm_load(struct drm_device *drm, unsigned long flags) err =drm_vblank_init (drm, drm->mode_config.num_crtc);if (err <0 ) -return err; + goto device; err =tegra_drm_fb_init (drm);if (err <0 ) -return err; + goto vblank;return 0 ; + +vblank: +drm_vblank_cleanup (drm); +device: +host1x_device_exit (device); +fbdev: +drm_kms_helper_poll_fini (drm); +tegra_drm_fb_free (drm); +config: +drm_mode_config_cleanup (drm); +kfree (tegra); +return err; }static int tegra_drm_unload (struct drm_device *drm)
drivers/gpu/drm/tegra/drm.h +1 -0
@@ -288,6 +288,7 @@ bool tegra_fb_is_bottom_up(struct drm_framebuffer *framebuffer);int tegra_fb_get_tiling (struct drm_framebuffer *framebuffer,struct tegra_bo_tiling *tiling);int tegra_drm_fb_prepare (struct drm_device *drm); +void tegra_drm_fb_free (struct drm_device *drm);int tegra_drm_fb_init (struct drm_device *drm);void tegra_drm_fb_exit (struct drm_device *drm);#ifdef CONFIG_DRM_TEGRA_FBDEV
drivers/gpu/drm/tegra/fb.c +17 -3
@@ -289,6 +289,11 @@ static struct tegra_fbdev *tegra_fbdev_create(struct drm_device *drm)return fbdev; } +static void tegra_fbdev_free (struct tegra_fbdev *fbdev) +{ +kfree (fbdev); +} + static int tegra_fbdev_init(struct tegra_fbdev *fbdev, unsigned int preferred_bpp, unsigned int num_crtc,
@@ -322,7 +327,7 @@ fini:return err; } -static void tegra_fbdev_free (struct tegra_fbdev *fbdev) +static void tegra_fbdev_exit (struct tegra_fbdev *fbdev) {struct fb_info *info = fbdev->base.fbdev;
@@ -345,7 +350,7 @@ static void tegra_fbdev_free(struct tegra_fbdev *fbdev) }drm_fb_helper_fini (&fbdev->base); -kfree (fbdev); +tegra_fbdev_free (fbdev); }void tegra_fbdev_restore_mode (struct tegra_fbdev *fbdev)
@@ -393,6 +398,15 @@ int tegra_drm_fb_prepare(struct drm_device *drm)return 0 ; } +void tegra_drm_fb_free (struct drm_device *drm) +{ +#ifdef CONFIG_DRM_TEGRA_FBDEV +struct tegra_drm *tegra = drm->dev_private; + +tegra_fbdev_free (tegra->fbdev); +#endif +} +int tegra_drm_fb_init (struct drm_device *drm) {#ifdef CONFIG_DRM_TEGRA_FBDEV
@@ -413,6 +427,6 @@ void tegra_drm_fb_exit(struct drm_device *drm)#ifdef CONFIG_DRM_TEGRA_FBDEV struct tegra_drm *tegra = drm->dev_private; -tegra_fbdev_free (tegra->fbdev); +tegra_fbdev_exit (tegra->fbdev);#endif }
Signature · 6 lines
2.1.3 _______________________________________________ dri-devel mailing list [email protected] http://lists.freedesktop.org/mailman/listinfo/dri-devel
PATCH v6 2/3 drm/tegra: Add IOMMU support
From: Thierry Reding <[email protected]>
When an IOMMU device is available on the platform bus, allocate an IOMMU domain and attach the display controllers to it. The display controllers can then scan out non-contiguous buffers by mapping them through the IOMMU.
---
Changes in v6:
- clean up IOMMU resources on initialization failure
Changes in v5:
- fix PRIME export of physically non-contiguous buffers
- refactor and fix error cleanup paths
- fix checking for failed allocations
- fix build warnings on 64-bit
Changes in v4:
- error out if IOMMU attachment fails
Changes in v3:
- create write-combined mappings for mmap()
- keep non-IOMMU fallback path working
- no longer rely on iommu_attach()
Changes in v2:
- don't pass GFP_KERNEL to drm_gem_get_pages()
drivers/gpu/drm/tegra/dc.c | 17 +++ drivers/gpu/drm/tegra/drm.c | 24 ++++ drivers/gpu/drm/tegra/drm.h | 5 + drivers/gpu/drm/tegra/fb.c | 16 ++- drivers/gpu/drm/tegra/gem.c | 277 ++++++++++++++++++++++++++++++++++++++------ drivers/gpu/drm/tegra/gem.h | 6 + 6 files changed, 308 insertions(+), 37 deletions(-)
drivers/gpu/drm/tegra/dc.c +17 -0
@@ -9,6 +9,7 @@#include <linux/clk.h> #include <linux/debugfs.h> +#include <linux/iommu.h> #include <linux/reset.h> #include <soc/tegra/pmc.h>
@@ -1289,6 +1290,17 @@ static int tegra_dc_init(struct host1x_client *client)struct tegra_drm *tegra = drm->dev_private;int err; +if (tegra->domain) { + err =iommu_attach_device (tegra->domain, dc->dev); +if (err <0 ) { +dev_err (dc->dev,"failed to attach to domain: %d\n" , + err); +return err; + } + + dc->domain = tegra->domain; + } +drm_crtc_init (drm, &dc->base, &tegra_crtc_funcs);drm_mode_crtc_set_gamma_size (&dc->base,256 );drm_crtc_helper_add (&dc->base, &tegra_crtc_helper_funcs);
@@ -1346,6 +1358,11 @@ static int tegra_dc_exit(struct host1x_client *client)return err; } +if (dc->domain) { +iommu_detach_device (dc->domain, dc->dev); + dc->domain =NULL ; + } +return 0 ; }
drivers/gpu/drm/tegra/drm.c +24 -0
@@ -8,6 +8,7 @@*/ #include <linux/host1x.h> +#include <linux/iommu.h> #include "drm.h" #include "gem.h"
@@ -33,6 +34,17 @@ static int tegra_drm_load(struct drm_device *drm, unsigned long flags)if (!tegra)return -ENOMEM ; +if (iommu_present (&platform_bus_type)) { + tegra->domain =iommu_domain_alloc (&platform_bus_type); +if (IS_ERR (tegra->domain)) { + err =PTR_ERR (tegra->domain); + goto free; + } + +DRM_DEBUG ("IOMMU context initialized\n" ); +drm_mm_init (&tegra->mm,0 ,SZ_2G ); + } +mutex_init (&tegra->clients_lock);INIT_LIST_HEAD (&tegra->clients); drm->dev_private = tegra;
@@ -76,6 +88,12 @@ fbdev:tegra_drm_fb_free (drm); config:drm_mode_config_cleanup (drm); + +if (tegra->domain) { +iommu_domain_free (tegra->domain); +drm_mm_takedown (&tegra->mm); + } +free:kfree (tegra);return err; }
@@ -83,6 +101,7 @@ config:static int tegra_drm_unload (struct drm_device *drm) {struct host1x_device *device =to_host1x_device (drm->dev); +struct tegra_drm *tegra = drm->dev_private;int err;drm_kms_helper_poll_fini (drm);
@@ -94,6 +113,11 @@ static int tegra_drm_unload(struct drm_device *drm)if (err <0 )return err; +if (tegra->domain) { +iommu_domain_free (tegra->domain); +drm_mm_takedown (&tegra->mm); + } +return 0 ; }
drivers/gpu/drm/tegra/drm.h +5 -0
@@ -39,6 +39,9 @@ struct tegra_fbdev {struct tegra_drm {struct drm_device *drm; +struct iommu_domain *domain; +struct drm_mm mm; +struct mutex clients_lock;struct list_head clients;
@@ -121,6 +124,8 @@ struct tegra_dc {struct drm_pending_vblank_event *event;const struct tegra_dc_soc_info *soc; + +struct iommu_domain *domain; }; static inline struct tegra_dc *
drivers/gpu/drm/tegra/fb.c +15 -1
@@ -65,8 +65,12 @@ static void tegra_fb_destroy(struct drm_framebuffer *framebuffer)for (i =0 ; i < fb->num_planes; i++) {struct tegra_bo *bo = fb->planes[i]; -if (bo) +if (bo) { +if (bo->pages && bo->vaddr) +vunmap (bo->vaddr); +drm_gem_object_unreference_unlocked (&bo->gem); + } }drm_framebuffer_cleanup (framebuffer);
@@ -254,6 +258,16 @@ static int tegra_fbdev_probe(struct drm_fb_helper *helper, offset = info->var.xoffset * bytes_per_pixel + info->var.yoffset * fb->pitches[0 ]; +if (bo->pages) { + bo->vaddr =vmap (bo->pages, bo->num_pages,VM_MAP , +pgprot_writecombine (PAGE_KERNEL )); +if (!bo->vaddr) { +dev_err (drm->dev,"failed to vmap() framebuffer\n" ); + err = -ENOMEM ; + goto destroy; + } + } + drm->mode_config.fb_base = (resource_size_t )bo->paddr; info->screen_base = (void __iomem *)bo->vaddr + offset; info->screen_size = size;
drivers/gpu/drm/tegra/gem.c +241 -36
373 lines not on this page · raw message
drivers/gpu/drm/tegra/gem.h +6 -0
@@ -38,6 +38,12 @@ struct tegra_bo {dma_addr_t paddr;void *vaddr; +struct drm_mm_node *mm; +unsigned long num_pages; +struct page **pages; +/* size of IOMMU mapping */ +size_t size; +struct tegra_bo_tiling tiling; };
Signature · 6 lines
2.1.3 _______________________________________________ dri-devel mailing list [email protected] http://lists.freedesktop.org/mailman/listinfo/dri-devel
PATCH 3/3 drm/tegra: gem: Check before freeing CMA memory
In reply to Thierry RedingFrom: Thierry Reding <[email protected]>
dma_free_writecombine() must not be called on a buffer that couldn't be allocated. Check for a valid virtual address before attempting to free the memory to avoid a crash.
--- drivers/gpu/drm/tegra/gem.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/tegra/gem.c +1 -1
@@ -210,7 +210,7 @@ static void tegra_bo_free(struct drm_device *drm, struct tegra_bo *bo)drm_gem_put_pages (&bo->gem, bo->pages, true, true);sg_free_table (bo->sgt);kfree (bo->sgt); - }else { + }else if (bo->vaddr) {dma_free_writecombine (drm->dev, bo->gem.size, bo->vaddr, bo->paddr); }
Signature · 6 lines
2.1.3 _______________________________________________ dri-devel mailing list [email protected] http://lists.freedesktop.org/mailman/listinfo/dri-devel
Re: [PATCH 3/3] drm/tegra: gem: Check before freeing CMA memory
Quoted 7 lines · Thierry Reding
From: Thierry Reding <[email protected]>
dma_free_writecombine()must not be called on a buffer that couldn't be allocated. Check for a valid virtual address before attempting to free the memory to avoid a crash.Reported-by Sean Paul <[email protected]>
nit: I'd prefer to use [email protected] instead of my google.com address
Quoted 1 line
Signed-off-by Thierry Reding <[email protected]>
Quoted 20 lines
--- drivers/gpu/drm/tegra/gem.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/gpu/drm/tegra/gem.c +1
-2@@ -210,7 +210,7 @@ static void tegra_bo_free(struct drm_device *drm, struct tegra_bo *bo)drm_gem_put_pages (&bo->gem, bo->pages, true, true);sg_free_table (bo->sgt);kfree (bo->sgt); - }else { + }else if (bo->vaddr) {dma_free_writecombine (drm->dev, bo->gem.size, bo->vaddr, bo->paddr); } --2.1.3
Quoted 4 lines
_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel
_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel
Quoted 7 lines · Thierry Reding
From: Thierry Reding <[email protected]>
The DRM driver's ->load() implementation didn't do a good job (no job at all really) cleaning up on failure. Fix that by undoing any prior setup when an error occurs. This requires a bit of rework to make it possible to clean up fbdev midway.
Quoted 7 lines
This was tested by injecting errors at various points during the initialization sequence and verifying that error cleanup didn't crash and no memory leaked (using kmemleak).
Reported-by Stéphane Marchesin <[email protected]>Reported-by Sean Paul <[email protected]>Signed-off-by Thierry Reding <[email protected]>
Looks much better :-)
Quoted 131 lines
--- drivers/gpu/drm/tegra/drm.c | 20 ++++++++++++++++---- drivers/gpu/drm/tegra/drm.h | 1 + drivers/gpu/drm/tegra/fb.c | 20 +++++++++++++++++--- 3 files changed, 34 insertions(+), 7 deletions(-)drivers/gpu/drm/tegra/drm.c +16
-4@@ -42,13 +42,13 @@ static int tegra_drm_load(struct drm_device *drm, unsigned long flags) err =tegra_drm_fb_prepare (drm);if (err <0 ) -return err; + goto config;drm_kms_helper_poll_init (drm); err =host1x_device_init (device);if (err <0 ) -return err; + goto fbdev;/* * We don't use the drm_irq_install() helpers provided by the DRM @@ -59,13 +59,25 @@ static int tegra_drm_load(struct drm_device *drm, unsigned long flags) err =drm_vblank_init (drm, drm->mode_config.num_crtc);if (err <0 ) -return err; + goto device; err =tegra_drm_fb_init (drm);if (err <0 ) -return err; + goto vblank;return 0 ; + +vblank: +drm_vblank_cleanup (drm); +device: +host1x_device_exit (device); +fbdev: +drm_kms_helper_poll_fini (drm); +tegra_drm_fb_free (drm); +config: +drm_mode_config_cleanup (drm); +kfree (tegra); +return err; }static int tegra_drm_unload (struct drm_device *drm)drivers/gpu/drm/tegra/drm.h +1
-0@@ -288,6 +288,7 @@ bool tegra_fb_is_bottom_up(struct drm_framebuffer *framebuffer);int tegra_fb_get_tiling (struct drm_framebuffer *framebuffer,struct tegra_bo_tiling *tiling);int tegra_drm_fb_prepare (struct drm_device *drm); +void tegra_drm_fb_free (struct drm_device *drm);int tegra_drm_fb_init (struct drm_device *drm);void tegra_drm_fb_exit (struct drm_device *drm);#ifdef CONFIG_DRM_TEGRA_FBDEV drivers/gpu/drm/tegra/fb.c +17
-3@@ -289,6 +289,11 @@ static struct tegra_fbdev *tegra_fbdev_create(struct drm_device *drm)return fbdev; } +static void tegra_fbdev_free (struct tegra_fbdev *fbdev) +{ +kfree (fbdev); +} + static int tegra_fbdev_init(struct tegra_fbdev *fbdev, unsigned int preferred_bpp, unsigned int num_crtc,@@ -322,7 +327,7 @@ fini:return err; } -static void tegra_fbdev_free (struct tegra_fbdev *fbdev) +static void tegra_fbdev_exit (struct tegra_fbdev *fbdev) {struct fb_info *info = fbdev->base.fbdev;@@ -345,7 +350,7 @@ static void tegra_fbdev_free(struct tegra_fbdev *fbdev) }drm_fb_helper_fini (&fbdev->base); -kfree (fbdev); +tegra_fbdev_free (fbdev); }void tegra_fbdev_restore_mode (struct tegra_fbdev *fbdev)@@ -393,6 +398,15 @@ int tegra_drm_fb_prepare(struct drm_device *drm)return 0 ; } +void tegra_drm_fb_free (struct drm_device *drm) +{ +#ifdef CONFIG_DRM_TEGRA_FBDEV +struct tegra_drm *tegra = drm->dev_private; + +tegra_fbdev_free (tegra->fbdev); +#endif +} +int tegra_drm_fb_init (struct drm_device *drm) {#ifdef CONFIG_DRM_TEGRA_FBDEV @@ -413,6 +427,6 @@ void tegra_drm_fb_exit(struct drm_device *drm)#ifdef CONFIG_DRM_TEGRA_FBDEV struct tegra_drm *tegra = drm->dev_private; -tegra_fbdev_free (tegra->fbdev); +tegra_fbdev_exit (tegra->fbdev);#endif }Signature · 6 lines
2.1.3 _______________________________________________ dri-devel mailing list [email protected] http://lists.freedesktop.org/mailman/listinfo/dri-devel
_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel