From 14d527e97ac3ad1f1ed96e70bc34e1f60e5b3cb5 Mon Sep 17 00:00:00 2001 From: Vedashree Vidwans Date: Tue, 23 Aug 2022 14:27:43 -0700 Subject: [PATCH] tegra: hwpm: fix reported coverity violations - Declare and initialize timeout and mem_bytes_kernel_u32 variables. - alist_map NULL pointer check is done after alist_map structure references. Modify update_allowlist ioctl and release alist_map functions to check alist_map NULL pointer before it's use. - Checking if unsigned _IOC_NR(cmd) is less than zero has no effect. Remove this check. - Correct printf format specifier for mem_bytes_addr. Bug 3461002 Change-Id: I79a97942e667c173d112bcaafb14ddcb8dd7d47f Signed-off-by: Vedashree Vidwans Reviewed-on: https://git-master.nvidia.com/r/c/linux-nvidia/+/2765676 Reviewed-by: Vasuki Shankar Reviewed-by: Seema Khowala GVS: Gerrit_Virtual_Submit --- common/allowlist.c | 26 +++++++++++++++++++------ hal/t234/t234_mem_mgmt.c | 2 +- os/linux/ioctl.c | 20 ++++++++----------- os/linux/mem_mgmt_utils.c | 41 +++++++++++++++++++++------------------ 4 files changed, 51 insertions(+), 38 deletions(-) diff --git a/common/allowlist.c b/common/allowlist.c index c2fb42a..ce6cdf5 100644 --- a/common/allowlist.c +++ b/common/allowlist.c @@ -22,14 +22,28 @@ int tegra_hwpm_get_allowlist_size(struct tegra_soc_hwpm *hwpm) { int ret = 0; - hwpm->alist_map->full_alist_size = 0ULL; - tegra_hwpm_fn(hwpm, " "); - ret = tegra_hwpm_func_all_ip(hwpm, NULL, TEGRA_HWPM_GET_ALIST_SIZE); - if (ret != 0) { - tegra_hwpm_err(hwpm, "get_alist_size failed"); - return ret; + if (hwpm->alist_map == NULL) { + /* Allocate tegra_hwpm_allowlist_map */ + hwpm->alist_map = tegra_hwpm_kzalloc(hwpm, + sizeof(struct tegra_hwpm_allowlist_map)); + if (!hwpm->alist_map) { + tegra_hwpm_err(NULL, + "Couldn't allocate allowlist map structure"); + return -ENOMEM; + } + hwpm->alist_map->full_alist_size = 0ULL; + } + + if (hwpm->alist_map->full_alist_size == 0) { + /* Full alist size is not computed yet */ + ret = tegra_hwpm_func_all_ip(hwpm, NULL, + TEGRA_HWPM_GET_ALIST_SIZE); + if (ret != 0) { + tegra_hwpm_err(hwpm, "get_alist_size failed"); + return ret; + } } return 0; diff --git a/hal/t234/t234_mem_mgmt.c b/hal/t234/t234_mem_mgmt.c index 0f1f28f..55ea05a 100644 --- a/hal/t234/t234_mem_mgmt.c +++ b/hal/t234/t234_mem_mgmt.c @@ -119,7 +119,7 @@ int t234_hwpm_enable_mem_mgmt(struct tegra_soc_hwpm *hwpm) return err; } tegra_hwpm_dbg(hwpm, hwpm_verbose, - "MEM_BYTES_ADDR = 0x%x", mem_bytes_addr); + "MEM_BYTES_ADDR = 0x%llx", mem_bytes_addr); err = tegra_hwpm_writel(hwpm, pma_perfmux, pmasys_channel_mem_block_r(0), diff --git a/os/linux/ioctl.c b/os/linux/ioctl.c index 4e2fe6c..40cceba 100644 --- a/os/linux/ioctl.c +++ b/os/linux/ioctl.c @@ -191,14 +191,11 @@ static int tegra_hwpm_query_allowlist_ioctl(struct tegra_soc_hwpm *hwpm, if (query_allowlist->allowlist == NULL) { /* Userspace is querying allowlist size only */ - if (hwpm->alist_map->full_alist_size == 0) { - /*Full alist size is not computed yet */ - ret = tegra_hwpm_get_allowlist_size(hwpm); - if (ret != 0) { - tegra_hwpm_err(hwpm, - "failed to get alist_size"); - return ret; - } + ret = tegra_hwpm_get_allowlist_size(hwpm); + if (ret != 0) { + tegra_hwpm_err(hwpm, + "failed to get alist_size"); + return ret; } query_allowlist->allowlist_size = hwpm->alist_map->full_alist_size; @@ -248,12 +245,11 @@ static long tegra_hwpm_ioctl(struct file *file, { int ret = 0; struct tegra_soc_hwpm *hwpm = NULL; - u8 *buf; + u8 *buf = NULL; if ((_IOC_TYPE(cmd) != TEGRA_SOC_HWPM_IOC_MAGIC) || - (_IOC_NR(cmd) < 0) || - (_IOC_NR(cmd) >= TERGA_SOC_HWPM_NUM_IOCTLS) || - (_IOC_SIZE(cmd) > TEGRA_SOC_HWPM_MAX_ARG_SIZE)) { + (_IOC_NR(cmd) >= TERGA_SOC_HWPM_NUM_IOCTLS) || + (_IOC_SIZE(cmd) > TEGRA_SOC_HWPM_MAX_ARG_SIZE)) { tegra_hwpm_err(hwpm, "Invalid IOCTL call"); ret = -EINVAL; goto fail; diff --git a/os/linux/mem_mgmt_utils.c b/os/linux/mem_mgmt_utils.c index e31e842..fc32369 100644 --- a/os/linux/mem_mgmt_utils.c +++ b/os/linux/mem_mgmt_utils.c @@ -265,7 +265,9 @@ int tegra_hwpm_clear_mem_pipeline(struct tegra_soc_hwpm *hwpm) u32 *mem_bytes_kernel_u32 = (u32 *)(hwpm->mem_mgmt->mem_bytes_kernel); u32 sleep_msecs = 100; - struct tegra_hwpm_timeout timeout; + struct tegra_hwpm_timeout timeout = {0}; + + *mem_bytes_kernel_u32 = TEGRA_HWPM_MEM_BYTES_INVALID; do { ret = hwpm->active_chip->stream_mem_bytes(hwpm); @@ -372,11 +374,6 @@ int tegra_hwpm_map_update_allowlist(struct tegra_soc_hwpm *hwpm, tegra_hwpm_fn(hwpm, " "); - if (hwpm->alist_map->full_alist_size == 0ULL) { - tegra_hwpm_err(hwpm, "Invalid allowlist size"); - return -EINVAL; - } - if (hwpm->alist_map == NULL) { /* Allocate tegra_hwpm_allowlist_map */ hwpm->alist_map = tegra_hwpm_kzalloc(hwpm, @@ -388,6 +385,12 @@ int tegra_hwpm_map_update_allowlist(struct tegra_soc_hwpm *hwpm, } } + if (hwpm->alist_map->full_alist_size == 0ULL) { + tegra_hwpm_err(hwpm, + "Allowlist size should be computed before"); + return -EINVAL; + } + alist_buf_size = tegra_hwpm_safe_mult_u64(hwpm->alist_map->full_alist_size, hwpm->active_chip->get_alist_buf_size(hwpm)); @@ -448,20 +451,20 @@ void tegra_hwpm_release_alist_map(struct tegra_soc_hwpm *hwpm) { u64 idx = 0U; - if (hwpm->alist_map->full_alist_map) { - vunmap(hwpm->alist_map->full_alist_map); - } - - for (idx = 0ULL; idx < hwpm->alist_map->num_pages; idx++) { - set_page_dirty(hwpm->alist_map->pages[idx]); - put_page(hwpm->alist_map->pages[idx]); - } - - if (hwpm->alist_map->pages) { - tegra_hwpm_kfree(hwpm, hwpm->alist_map->pages); - } - if (hwpm->alist_map) { + if (hwpm->alist_map->full_alist_map) { + vunmap(hwpm->alist_map->full_alist_map); + } + + for (idx = 0ULL; idx < hwpm->alist_map->num_pages; idx++) { + set_page_dirty(hwpm->alist_map->pages[idx]); + put_page(hwpm->alist_map->pages[idx]); + } + + if (hwpm->alist_map->pages) { + tegra_hwpm_kfree(hwpm, hwpm->alist_map->pages); + } + tegra_hwpm_kfree(hwpm, hwpm->alist_map); } }