Skip to content

Commit 7973642

Browse files
andy-shevBartosz Golaszewski
authored andcommitted
gpiolib: Replace open coded krealloc()
gpiod_get_array() does a new allocation in some cases, followed by copying previously allocated placeholder for the descriptors. Replace that with krealloc(__GFP_ZERO), since it was kzalloc() originally. Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com> Reviewed-by: Linus Walleij <linus.walleij@linaro.org> Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
1 parent 2093bcd commit 7973642

1 file changed

Lines changed: 8 additions & 9 deletions

File tree

drivers/gpio/gpiolib.c

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4277,12 +4277,14 @@ struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
42774277
struct gpio_array *array_info = NULL;
42784278
struct gpio_chip *gc;
42794279
int count, bitmap_size;
4280+
size_t descs_size;
42804281

42814282
count = gpiod_count(dev, con_id);
42824283
if (count < 0)
42834284
return ERR_PTR(count);
42844285

4285-
descs = kzalloc(struct_size(descs, desc, count), GFP_KERNEL);
4286+
descs_size = struct_size(descs, desc, count);
4287+
descs = kzalloc(descs_size, GFP_KERNEL);
42864288
if (!descs)
42874289
return ERR_PTR(-ENOMEM);
42884290

@@ -4306,20 +4308,17 @@ struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
43064308
bitmap_size = BITS_TO_LONGS(gc->ngpio > count ?
43074309
gc->ngpio : count);
43084310

4309-
array = kzalloc(struct_size(descs, desc, count) +
4310-
struct_size(array_info, invert_mask,
4311-
3 * bitmap_size), GFP_KERNEL);
4311+
array = krealloc(descs, descs_size +
4312+
struct_size(array_info, invert_mask, 3 * bitmap_size),
4313+
GFP_KERNEL | __GFP_ZERO);
43124314
if (!array) {
43134315
gpiod_put_array(descs);
43144316
return ERR_PTR(-ENOMEM);
43154317
}
43164318

4317-
memcpy(array, descs,
4318-
struct_size(descs, desc, descs->ndescs + 1));
4319-
kfree(descs);
4320-
43214319
descs = array;
4322-
array_info = (void *)(descs->desc + count);
4320+
4321+
array_info = (void *)descs + descs_size;
43234322
array_info->get_mask = array_info->invert_mask +
43244323
bitmap_size;
43254324
array_info->set_mask = array_info->get_mask +

0 commit comments

Comments
 (0)