From 9e43258438a48eb23c6e65ad91ce88518d6a617a Mon Sep 17 00:00:00 2001 From: Alex Waterman Date: Fri, 5 Aug 2016 16:47:17 -0700 Subject: gpu: nvgpu: Add checking in allocator functions Add checks to make sure function pointers are valid before attempting to call said function. Also, ensure that any allocator created defines the following 3 functions at minimum: alloc() free() fini() Bug 1799159 Change-Id: I4cd3d5746ccb721c723a161c9487564846027572 Signed-off-by: Alex Waterman Reviewed-on: http://git-master/r/1200059 (cherry picked from commit e26557a49d7ca6629ada24f12a3be396b0ae22cd) Reviewed-on: http://git-master/r/1208476 Reviewed-by: Automatic_Commit_Validation_User GVS: Gerrit_Virtual_Submit Reviewed-by: Terje Bergstrom --- drivers/gpu/nvgpu/gk20a/gk20a_allocator.c | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) (limited to 'drivers/gpu/nvgpu/gk20a/gk20a_allocator.c') diff --git a/drivers/gpu/nvgpu/gk20a/gk20a_allocator.c b/drivers/gpu/nvgpu/gk20a/gk20a_allocator.c index 25d15d0f..d9e38200 100644 --- a/drivers/gpu/nvgpu/gk20a/gk20a_allocator.c +++ b/drivers/gpu/nvgpu/gk20a/gk20a_allocator.c @@ -29,17 +29,23 @@ static struct dentry *gk20a_alloc_debugfs_root; u64 gk20a_alloc_length(struct gk20a_allocator *a) { - return a->ops->length(a); + if (a->ops->length) + return a->ops->length(a); + + return 0; } u64 gk20a_alloc_base(struct gk20a_allocator *a) { - return a->ops->base(a); + if (a->ops->base) + return a->ops->base(a); + + return 0; } u64 gk20a_alloc_initialized(struct gk20a_allocator *a) { - if (!a->ops) + if (!a->ops || !a->ops->inited) return 0; return a->ops->inited(a); @@ -47,7 +53,10 @@ u64 gk20a_alloc_initialized(struct gk20a_allocator *a) u64 gk20a_alloc_end(struct gk20a_allocator *a) { - return a->ops->end(a); + if (a->ops->end) + return a->ops->end(a); + + return 0; } u64 gk20a_alloc(struct gk20a_allocator *a, u64 len) @@ -62,7 +71,10 @@ void gk20a_free(struct gk20a_allocator *a, u64 addr) u64 gk20a_alloc_fixed(struct gk20a_allocator *a, u64 base, u64 len) { - return a->ops->alloc_fixed(a, base, len); + if (a->ops->alloc_fixed) + return a->ops->alloc_fixed(a, base, len); + + return 0; } void gk20a_free_fixed(struct gk20a_allocator *a, u64 base, u64 len) @@ -92,6 +104,13 @@ int __gk20a_alloc_common_init(struct gk20a_allocator *a, if (!ops) return -EINVAL; + /* + * This is the bare minimum operations required for a sensible + * allocator. + */ + if (!ops->alloc || !ops->free || !ops->fini) + return -EINVAL; + a->ops = ops; a->priv = priv; a->debug = dbg; -- cgit v1.2.2