mirror of
git://nv-tegra.nvidia.com/linux-nvgpu.git
synced 2025-12-22 09:12:24 +03:00
gpu: nvgpu: Fix Division by zero defect
Fix following Coverity Defect: profile.c : Division or modulo by zero CID 10061399 Bug 3460991 Signed-off-by: Jinesh Parakh <jparakh@nvidia.com> Change-Id: I03979af4ab105f659cf0fe3eac8d21946dfca950 Reviewed-on: https://git-master.nvidia.com/r/c/linux-nvgpu/+/2695362 Reviewed-by: svc-mobile-coverity <svc-mobile-coverity@nvidia.com> Reviewed-by: svc-mobile-cert <svc-mobile-cert@nvidia.com> Reviewed-by: svc-mobile-misra <svc-mobile-misra@nvidia.com> Reviewed-by: Sagar Kamble <skamble@nvidia.com> Reviewed-by: Sachin Nikam <snikam@nvidia.com> GVS: Gerrit_Virtual_Submit
This commit is contained in:
committed by
mobile promotions
parent
5b38f48227
commit
131933d528
@@ -447,17 +447,33 @@ static u32 nvgpu_swprofile_subsample_basic_stats(struct gk20a *g,
|
|||||||
/* With the sorted list of samples we can easily compute the median. */
|
/* With the sorted list of samples we can easily compute the median. */
|
||||||
sort(storage, samples, sizeof(u64), profile_cmp, NULL);
|
sort(storage, samples, sizeof(u64), profile_cmp, NULL);
|
||||||
|
|
||||||
|
if (samples == 0U) {
|
||||||
|
return 0U;
|
||||||
|
}
|
||||||
|
|
||||||
mean = sum / samples;
|
mean = sum / samples;
|
||||||
median = storage[samples / 2];
|
median = storage[samples / 2];
|
||||||
|
|
||||||
/* Compute the sample variance (i.e sigma squared). */
|
/*
|
||||||
for (i = 0U; i < samples; i++) {
|
* If only 1 sample is found, the min, max, median and mean would be
|
||||||
sigma_2 += storage[i] * storage[i];
|
* the value of that one observation (storage[0]). The variance in this
|
||||||
}
|
* case would be 0.
|
||||||
|
* The need for this special case is because in the original implementation,
|
||||||
|
* sigma_2 is divided by samples-1 which is 0 in our case, causing a divide
|
||||||
|
* by zero error.
|
||||||
|
*/
|
||||||
|
if (samples == 1U) {
|
||||||
|
sigma_2 = 0U;
|
||||||
|
} else {
|
||||||
|
/* Compute the sample variance (i.e sigma squared). */
|
||||||
|
for (i = 0U; i < samples; i++) {
|
||||||
|
sigma_2 += storage[i] * storage[i];
|
||||||
|
}
|
||||||
|
|
||||||
/* Remember: _sample_ variance. */
|
/* Remember: _sample_ variance. */
|
||||||
sigma_2 /= (samples - 1U);
|
sigma_2 /= (samples - 1U);
|
||||||
sigma_2 -= (mean * mean);
|
sigma_2 -= (mean * mean);
|
||||||
|
}
|
||||||
|
|
||||||
results[0] = min;
|
results[0] = min;
|
||||||
results[1] = max;
|
results[1] = max;
|
||||||
@@ -509,9 +525,12 @@ void nvgpu_swprofile_print_basic_stats(struct gk20a *g,
|
|||||||
samples = nvgpu_swprofile_subsample_basic_stats(g, p, i,
|
samples = nvgpu_swprofile_subsample_basic_stats(g, p, i,
|
||||||
results, storage);
|
results, storage);
|
||||||
|
|
||||||
|
if (samples == 0U) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
gk20a_debug_output(o, fmt_output, p->col_names[i],
|
gk20a_debug_output(o, fmt_output, p->col_names[i],
|
||||||
results[0], results[1],
|
results[0], results[1],
|
||||||
results[2], results[3], results[4]);
|
results[2], results[3], results[4]);
|
||||||
}
|
}
|
||||||
|
|
||||||
gk20a_debug_output(o, "Number of samples: %u\n", samples);
|
gk20a_debug_output(o, "Number of samples: %u\n", samples);
|
||||||
|
|||||||
Reference in New Issue
Block a user