From dce49c9b2b6bf3ee4747b404f6693a3aeabd5909 Mon Sep 17 00:00:00 2001 From: Scott Long Date: Tue, 15 Jan 2019 14:59:44 -0800 Subject: [PATCH] gpu: nvgpu: container_of() changes to sema code The container_of() macro used in nvgpu produces the following set of MISRA required rule violations: * Rule 11.3 : A cast shall not be performed between a pointer to object type and a pointer to a different object type. * Rule 11.8 : A cast shall not remove any const or volatile qualification from the type pointed to be a pointer. * Rule 20.7 : Expressions resulting from the expansion of macro parameters shall be enclosed in parentheses. Using the same modified implementation of container_of() as that used in the nvgpu_list_node/nvgpu_rbtree_node routines eliminates the Rule 11.8 and Rule 20.7 violations and exchanges the Rule 11.3 violation with an advisory Rule 11.4 violation. This patch uses that same equivalent implementation in two new (static) functions that are used to replace the references to container_of() in common semaphore code: * nvgpu_semaphore_pool_from_ref * nvgpu_semaphore_from_ref It should be noted that replacement functions still contain potentially dangerous (and non-MISRA compliant code) and that it is expected that deviation requests will be filed for the new advisory rule violations accordingly. JIRA NVGPU-782 Change-Id: I79c0b6fc4fa819c92985f2e2239e9d1d7137618d Signed-off-by: Scott Long Reviewed-on: https://git-master.nvidia.com/r/1995937 Reviewed-by: svc-mobile-coverity Reviewed-by: svc-mobile-misra Reviewed-by: Automatic_Commit_Validation_User Reviewed-by: svc-misra-checker GVS: Gerrit_Virtual_Submit Reviewed-by: Alex Waterman Reviewed-by: mobile promotions Tested-by: mobile promotions --- drivers/gpu/nvgpu/common/semaphore.c | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/drivers/gpu/nvgpu/common/semaphore.c b/drivers/gpu/nvgpu/common/semaphore.c index 148cd4c31..656e005e4 100644 --- a/drivers/gpu/nvgpu/common/semaphore.c +++ b/drivers/gpu/nvgpu/common/semaphore.c @@ -313,14 +313,20 @@ void nvgpu_semaphore_pool_unmap(struct nvgpu_semaphore_pool *p, "Unmapped semaphore pool! (idx=%llu)", p->page_idx); } +static struct nvgpu_semaphore_pool * +nvgpu_semaphore_pool_from_ref(struct nvgpu_ref *ref) +{ + return (struct nvgpu_semaphore_pool *) + ((uintptr_t)ref - offsetof(struct nvgpu_semaphore_pool, ref)); +} + /* * Completely free a semaphore_pool. You should make sure this pool is not * mapped otherwise there's going to be a memory leak. */ static void nvgpu_semaphore_pool_free(struct nvgpu_ref *ref) { - struct nvgpu_semaphore_pool *p = - container_of(ref, struct nvgpu_semaphore_pool, ref); + struct nvgpu_semaphore_pool *p = nvgpu_semaphore_pool_from_ref(ref); struct nvgpu_semaphore_sea *s = p->sema_sea; /* Freeing a mapped pool is a bad idea. */ @@ -469,10 +475,15 @@ struct nvgpu_semaphore *nvgpu_semaphore_alloc(struct channel_gk20a *ch) return s; } +static struct nvgpu_semaphore *nvgpu_semaphore_from_ref(struct nvgpu_ref *ref) +{ + return (struct nvgpu_semaphore *) + ((uintptr_t)ref - offsetof(struct nvgpu_semaphore, ref)); +} + static void nvgpu_semaphore_free(struct nvgpu_ref *ref) { - struct nvgpu_semaphore *s = - container_of(ref, struct nvgpu_semaphore, ref); + struct nvgpu_semaphore *s = nvgpu_semaphore_from_ref(ref); nvgpu_semaphore_pool_put(s->location.pool);