2017-12-31 11:40:53

by Xiongwei Song

[permalink] [raw]
Subject: [PATCH] drm/ttm: optimize errors checking and free _manager when finishing

In the function ttm_page_alloc_init, kzalloc call is made for variable
_manager, we need to check its return value, it may return NULL.

In the function ttm_page_alloc_fini, we need to call kfree for variable
_manager, instead of make _manager NULL directly.

Signed-off-by: Xiongwei Song <[email protected]>
---
drivers/gpu/drm/ttm/ttm_page_alloc.c | 13 +++++++++++--
1 file changed, 11 insertions(+), 2 deletions(-)

diff --git a/drivers/gpu/drm/ttm/ttm_page_alloc.c b/drivers/gpu/drm/ttm/ttm_page_alloc.c
index b5ba6441489f..e20a0b8e352b 100644
--- a/drivers/gpu/drm/ttm/ttm_page_alloc.c
+++ b/drivers/gpu/drm/ttm/ttm_page_alloc.c
@@ -1007,6 +1007,10 @@ int ttm_page_alloc_init(struct ttm_mem_global *glob, unsigned max_pages)
pr_info("Initializing pool allocator\n");

_manager = kzalloc(sizeof(*_manager), GFP_KERNEL);
+ if (!_manager) {
+ ret = -ENOMEM;
+ goto out;
+ }

ttm_page_pool_init_locked(&_manager->wc_pool, GFP_HIGHUSER, "wc", 0);

@@ -1034,13 +1038,17 @@ int ttm_page_alloc_init(struct ttm_mem_global *glob, unsigned max_pages)
&glob->kobj, "pool");
if (unlikely(ret != 0)) {
kobject_put(&_manager->kobj);
- _manager = NULL;
- return ret;
+ goto out_free_mgr;
}

ttm_pool_mm_shrink_init(_manager);

return 0;
+out_free_mgr:
+ kfree(_manager);
+ _manager = NULL;
+out:
+ return ret;
}

void ttm_page_alloc_fini(void)
@@ -1055,6 +1063,7 @@ void ttm_page_alloc_fini(void)
ttm_page_pool_free(&_manager->pools[i], FREE_ALL_PAGES, true);

kobject_put(&_manager->kobj);
+ kfree(_manager);
_manager = NULL;
}

--
2.15.1


2018-01-02 03:05:47

by He, Roger

[permalink] [raw]
Subject: RE: [PATCH] drm/ttm: optimize errors checking and free _manager when finishing



-----Original Message-----
From: Xiongwei Song [mailto:[email protected]]
Sent: Sunday, December 31, 2017 7:40 PM
To: Koenig, Christian <[email protected]>; He, Roger <[email protected]>; [email protected]
Cc: [email protected]; [email protected]
Subject: [PATCH] drm/ttm: optimize errors checking and free _manager when finishing

In the function ttm_page_alloc_init, kzalloc call is made for variable _manager, we need to check its return value, it may return NULL.

In the function ttm_page_alloc_fini, we need to call kfree for variable _manager, instead of make _manager NULL directly.

Signed-off-by: Xiongwei Song <[email protected]>
---
drivers/gpu/drm/ttm/ttm_page_alloc.c | 13 +++++++++++--
1 file changed, 11 insertions(+), 2 deletions(-)

diff --git a/drivers/gpu/drm/ttm/ttm_page_alloc.c b/drivers/gpu/drm/ttm/ttm_page_alloc.c
index b5ba6441489f..e20a0b8e352b 100644
--- a/drivers/gpu/drm/ttm/ttm_page_alloc.c
+++ b/drivers/gpu/drm/ttm/ttm_page_alloc.c
@@ -1007,6 +1007,10 @@ int ttm_page_alloc_init(struct ttm_mem_global *glob, unsigned max_pages)
pr_info("Initializing pool allocator\n");

_manager = kzalloc(sizeof(*_manager), GFP_KERNEL);
+ if (!_manager) {
+ ret = -ENOMEM;
+ goto out;
+ }
Seems we only need above here for this patch I think.
The rest is no need, because ttm_pool_kobj_release will kfree _manager.


Thanks
Roger(Hongbo.He)

ttm_page_pool_init_locked(&_manager->wc_pool, GFP_HIGHUSER, "wc", 0);

@@ -1034,13 +1038,17 @@ int ttm_page_alloc_init(struct ttm_mem_global *glob, unsigned max_pages)
&glob->kobj, "pool");
if (unlikely(ret != 0)) {
kobject_put(&_manager->kobj);
- _manager = NULL;
- return ret;
+ goto out_free_mgr;
}

ttm_pool_mm_shrink_init(_manager);

return 0;
+out_free_mgr:
+ kfree(_manager);
+ _manager = NULL;
+out:
+ return ret;
}

void ttm_page_alloc_fini(void)
@@ -1055,6 +1063,7 @@ void ttm_page_alloc_fini(void)
ttm_page_pool_free(&_manager->pools[i], FREE_ALL_PAGES, true);

kobject_put(&_manager->kobj);
+ kfree(_manager);
_manager = NULL;
}

--
2.15.1

2018-01-02 09:30:20

by Christian König

[permalink] [raw]
Subject: Re: [PATCH] drm/ttm: optimize errors checking and free _manager when finishing

Am 02.01.2018 um 04:05 schrieb He, Roger:
>
> -----Original Message-----
> From: Xiongwei Song [mailto:[email protected]]
> Sent: Sunday, December 31, 2017 7:40 PM
> To: Koenig, Christian <[email protected]>; He, Roger <[email protected]>; [email protected]
> Cc: [email protected]; [email protected]
> Subject: [PATCH] drm/ttm: optimize errors checking and free _manager when finishing
>
> In the function ttm_page_alloc_init, kzalloc call is made for variable _manager, we need to check its return value, it may return NULL.
>
> In the function ttm_page_alloc_fini, we need to call kfree for variable _manager, instead of make _manager NULL directly.
>
> Signed-off-by: Xiongwei Song <[email protected]>
> ---
> drivers/gpu/drm/ttm/ttm_page_alloc.c | 13 +++++++++++--
> 1 file changed, 11 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/gpu/drm/ttm/ttm_page_alloc.c b/drivers/gpu/drm/ttm/ttm_page_alloc.c
> index b5ba6441489f..e20a0b8e352b 100644
> --- a/drivers/gpu/drm/ttm/ttm_page_alloc.c
> +++ b/drivers/gpu/drm/ttm/ttm_page_alloc.c
> @@ -1007,6 +1007,10 @@ int ttm_page_alloc_init(struct ttm_mem_global *glob, unsigned max_pages)
> pr_info("Initializing pool allocator\n");
>
> _manager = kzalloc(sizeof(*_manager), GFP_KERNEL);
> + if (!_manager) {
> + ret = -ENOMEM;
> + goto out;
> + }
> Seems we only need above here for this patch I think.
> The rest is no need, because ttm_pool_kobj_release will kfree _manager.

Yes, agree. Freeing it again in ttm_page_alloc_fini will actually double
free the memory and probably cause some problems.

Please refine the patch with only the additional error handling.

Regards,
Christian.

>
>
> Thanks
> Roger(Hongbo.He)
>
> ttm_page_pool_init_locked(&_manager->wc_pool, GFP_HIGHUSER, "wc", 0);
>
> @@ -1034,13 +1038,17 @@ int ttm_page_alloc_init(struct ttm_mem_global *glob, unsigned max_pages)
> &glob->kobj, "pool");
> if (unlikely(ret != 0)) {
> kobject_put(&_manager->kobj);
> - _manager = NULL;
> - return ret;
> + goto out_free_mgr;
> }
>
> ttm_pool_mm_shrink_init(_manager);
>
> return 0;
> +out_free_mgr:
> + kfree(_manager);
> + _manager = NULL;
> +out:
> + return ret;
> }
>
> void ttm_page_alloc_fini(void)
> @@ -1055,6 +1063,7 @@ void ttm_page_alloc_fini(void)
> ttm_page_pool_free(&_manager->pools[i], FREE_ALL_PAGES, true);
>
> kobject_put(&_manager->kobj);
> + kfree(_manager);
> _manager = NULL;
> }
>
> --
> 2.15.1
>