[PATCH v3 2/3] staging: media: atomisp: use kvmalloc_objs() for overflow-safe allocation
From: Rodrigo Gobbi
Date: Tue Jun 23 2026 - 18:12:17 EST
From: Feng Ning <feng@xxxxxxxxx>
Replace open-coded width * height * sizeof() multiplications with
kvmalloc_objs() and array_size() to prevent integer overflow in buffer
allocations.
The atomisp driver computes DVS and statistics buffer sizes using
unchecked arithmetic. When dimensions are large, the product can
silently wrap, causing kvmalloc() to allocate an undersized buffer.
kvmalloc_objs() uses size_mul() internally, which saturates to SIZE_MAX
on overflow, so kvmalloc() returns NULL instead of succeeding with too
few bytes. array_size() provides the same overflow protection for the
two-factor dimension products.
Suggested-by: Andy Shevchenko <andy.shevchenko@xxxxxxxxx>
Signed-off-by: Feng Ning <feng@xxxxxxxxx>
[rodrigo: rebased; convert only the sites left open-coded after
commit d178c7ca8fef]
Signed-off-by: Rodrigo Gobbi <rodrigo.gobbi.7@xxxxxxxxx>
---
.../staging/media/atomisp/pci/sh_css_params.c | 101 +++++++-----------
1 file changed, 36 insertions(+), 65 deletions(-)
diff --git a/drivers/staging/media/atomisp/pci/sh_css_params.c b/drivers/staging/media/atomisp/pci/sh_css_params.c
index 8420a22fd8f0..adc329be8b0b 100644
--- a/drivers/staging/media/atomisp/pci/sh_css_params.c
+++ b/drivers/staging/media/atomisp/pci/sh_css_params.c
@@ -6,6 +6,7 @@
#include <linux/overflow.h>
#include <linux/math.h>
+#include <linux/slab.h>
#include "gdc_device.h" /* gdc_lut_store(), ... */
#include "isp.h" /* ISP_VEC_ELEMBITS */
@@ -4151,7 +4152,7 @@ struct ia_css_3a_statistics *
ia_css_3a_statistics_allocate(const struct ia_css_3a_grid_info *grid)
{
struct ia_css_3a_statistics *me;
- int grid_size;
+ size_t grid_size;
IA_CSS_ENTER("grid=%p", grid);
@@ -4162,8 +4163,8 @@ ia_css_3a_statistics_allocate(const struct ia_css_3a_grid_info *grid)
goto err;
me->grid = *grid;
- grid_size = grid->width * grid->height;
- me->data = kvmalloc(grid_size * sizeof(*me->data), GFP_KERNEL);
+ grid_size = array_size(grid->width, grid->height);
+ me->data = kvmalloc_objs(*me->data, grid_size);
if (!me->data)
goto err;
/* No weighted histogram, no structure, treat the histogram data as a byte dump in a byte array */
@@ -4236,6 +4237,7 @@ struct ia_css_dvs_coefficients *
ia_css_dvs_coefficients_allocate(const struct ia_css_dvs_grid_info *grid)
{
struct ia_css_dvs_coefficients *me;
+ size_t cnt;
assert(grid);
@@ -4245,15 +4247,13 @@ ia_css_dvs_coefficients_allocate(const struct ia_css_dvs_grid_info *grid)
me->grid = *grid;
- me->hor_coefs = kvmalloc(grid->num_hor_coefs *
- IA_CSS_DVS_NUM_COEF_TYPES *
- sizeof(*me->hor_coefs), GFP_KERNEL);
+ cnt = array_size(grid->num_hor_coefs, IA_CSS_DVS_NUM_COEF_TYPES);
+ me->hor_coefs = kvmalloc_objs(*me->hor_coefs, cnt);
if (!me->hor_coefs)
goto err;
- me->ver_coefs = kvmalloc(grid->num_ver_coefs *
- IA_CSS_DVS_NUM_COEF_TYPES *
- sizeof(*me->ver_coefs), GFP_KERNEL);
+ cnt = array_size(grid->num_ver_coefs, IA_CSS_DVS_NUM_COEF_TYPES);
+ me->ver_coefs = kvmalloc_objs(*me->ver_coefs, cnt);
if (!me->ver_coefs)
goto err;
@@ -4277,6 +4277,7 @@ struct ia_css_dvs2_statistics *
ia_css_dvs2_statistics_allocate(const struct ia_css_dvs_grid_info *grid)
{
struct ia_css_dvs2_statistics *me;
+ size_t cnt;
assert(grid);
@@ -4286,59 +4287,37 @@ ia_css_dvs2_statistics_allocate(const struct ia_css_dvs_grid_info *grid)
me->grid = *grid;
- me->hor_prod.odd_real = kvmalloc(grid->aligned_width *
- grid->aligned_height *
- sizeof(*me->hor_prod.odd_real),
- GFP_KERNEL);
+ cnt = array_size(grid->aligned_width, grid->aligned_height);
+
+ me->hor_prod.odd_real = kvmalloc_objs(*me->hor_prod.odd_real, cnt);
if (!me->hor_prod.odd_real)
goto err;
- me->hor_prod.odd_imag = kvmalloc(grid->aligned_width *
- grid->aligned_height *
- sizeof(*me->hor_prod.odd_imag),
- GFP_KERNEL);
+ me->hor_prod.odd_imag = kvmalloc_objs(*me->hor_prod.odd_imag, cnt);
if (!me->hor_prod.odd_imag)
goto err;
- me->hor_prod.even_real = kvmalloc(grid->aligned_width *
- grid->aligned_height *
- sizeof(*me->hor_prod.even_real),
- GFP_KERNEL);
+ me->hor_prod.even_real = kvmalloc_objs(*me->hor_prod.even_real, cnt);
if (!me->hor_prod.even_real)
goto err;
- me->hor_prod.even_imag = kvmalloc(grid->aligned_width *
- grid->aligned_height *
- sizeof(*me->hor_prod.even_imag),
- GFP_KERNEL);
+ me->hor_prod.even_imag = kvmalloc_objs(*me->hor_prod.even_imag, cnt);
if (!me->hor_prod.even_imag)
goto err;
- me->ver_prod.odd_real = kvmalloc(grid->aligned_width *
- grid->aligned_height *
- sizeof(*me->ver_prod.odd_real),
- GFP_KERNEL);
+ me->ver_prod.odd_real = kvmalloc_objs(*me->ver_prod.odd_real, cnt);
if (!me->ver_prod.odd_real)
goto err;
- me->ver_prod.odd_imag = kvmalloc(grid->aligned_width *
- grid->aligned_height *
- sizeof(*me->ver_prod.odd_imag),
- GFP_KERNEL);
+ me->ver_prod.odd_imag = kvmalloc_objs(*me->ver_prod.odd_imag, cnt);
if (!me->ver_prod.odd_imag)
goto err;
- me->ver_prod.even_real = kvmalloc(grid->aligned_width *
- grid->aligned_height *
- sizeof(*me->ver_prod.even_real),
- GFP_KERNEL);
+ me->ver_prod.even_real = kvmalloc_objs(*me->ver_prod.even_real, cnt);
if (!me->ver_prod.even_real)
goto err;
- me->ver_prod.even_imag = kvmalloc(grid->aligned_width *
- grid->aligned_height *
- sizeof(*me->ver_prod.even_imag),
- GFP_KERNEL);
+ me->ver_prod.even_imag = kvmalloc_objs(*me->ver_prod.even_imag, cnt);
if (!me->ver_prod.even_imag)
goto err;
@@ -4377,51 +4356,43 @@ ia_css_dvs2_coefficients_allocate(const struct ia_css_dvs_grid_info *grid)
me->grid = *grid;
- me->hor_coefs.odd_real = kvmalloc(grid->num_hor_coefs *
- sizeof(*me->hor_coefs.odd_real),
- GFP_KERNEL);
+ me->hor_coefs.odd_real = kvmalloc_objs(*me->hor_coefs.odd_real,
+ grid->num_hor_coefs);
if (!me->hor_coefs.odd_real)
goto err;
- me->hor_coefs.odd_imag = kvmalloc(grid->num_hor_coefs *
- sizeof(*me->hor_coefs.odd_imag),
- GFP_KERNEL);
+ me->hor_coefs.odd_imag = kvmalloc_objs(*me->hor_coefs.odd_imag,
+ grid->num_hor_coefs);
if (!me->hor_coefs.odd_imag)
goto err;
- me->hor_coefs.even_real = kvmalloc(grid->num_hor_coefs *
- sizeof(*me->hor_coefs.even_real),
- GFP_KERNEL);
+ me->hor_coefs.even_real = kvmalloc_objs(*me->hor_coefs.even_real,
+ grid->num_hor_coefs);
if (!me->hor_coefs.even_real)
goto err;
- me->hor_coefs.even_imag = kvmalloc(grid->num_hor_coefs *
- sizeof(*me->hor_coefs.even_imag),
- GFP_KERNEL);
+ me->hor_coefs.even_imag = kvmalloc_objs(*me->hor_coefs.even_imag,
+ grid->num_hor_coefs);
if (!me->hor_coefs.even_imag)
goto err;
- me->ver_coefs.odd_real = kvmalloc(grid->num_ver_coefs *
- sizeof(*me->ver_coefs.odd_real),
- GFP_KERNEL);
+ me->ver_coefs.odd_real = kvmalloc_objs(*me->ver_coefs.odd_real,
+ grid->num_ver_coefs);
if (!me->ver_coefs.odd_real)
goto err;
- me->ver_coefs.odd_imag = kvmalloc(grid->num_ver_coefs *
- sizeof(*me->ver_coefs.odd_imag),
- GFP_KERNEL);
+ me->ver_coefs.odd_imag = kvmalloc_objs(*me->ver_coefs.odd_imag,
+ grid->num_ver_coefs);
if (!me->ver_coefs.odd_imag)
goto err;
- me->ver_coefs.even_real = kvmalloc(grid->num_ver_coefs *
- sizeof(*me->ver_coefs.even_real),
- GFP_KERNEL);
+ me->ver_coefs.even_real = kvmalloc_objs(*me->ver_coefs.even_real,
+ grid->num_ver_coefs);
if (!me->ver_coefs.even_real)
goto err;
- me->ver_coefs.even_imag = kvmalloc(grid->num_ver_coefs *
- sizeof(*me->ver_coefs.even_imag),
- GFP_KERNEL);
+ me->ver_coefs.even_imag = kvmalloc_objs(*me->ver_coefs.even_imag,
+ grid->num_ver_coefs);
if (!me->ver_coefs.even_imag)
goto err;
--
2.48.1