Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

gpio: bcm-kona: reduce the number of memory allocations

Simplify allocation by using a flexible array member.

Use __counted_by for extra runtime analysis.

Shuffle some code as __counted_by requires the counting variable to be
set right after allocation.

Signed-off-by: Rosen Penev <rosenp@gmail.com>
Link: https://patch.msgid.link/20260311003431.31881-1-rosenp@gmail.com
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>

authored by

Rosen Penev and committed by
Bartosz Golaszewski
0258fe87 8f0aecf2

+16 -21
+16 -21
drivers/gpio/gpio-bcm-kona.c
··· 58 58 #define LOCK_CODE 0xffffffff 59 59 #define UNLOCK_CODE 0x00000000 60 60 61 - struct bcm_kona_gpio { 62 - void __iomem *reg_base; 63 - int num_bank; 64 - raw_spinlock_t lock; 65 - struct gpio_chip gpio_chip; 66 - struct irq_domain *irq_domain; 67 - struct bcm_kona_gpio_bank *banks; 68 - }; 69 - 70 61 struct bcm_kona_gpio_bank { 71 62 int id; 72 63 int irq; ··· 79 88 u8 gpio_unlock_count[GPIO_PER_BANK]; 80 89 /* Used in the interrupt handler */ 81 90 struct bcm_kona_gpio *kona_gpio; 91 + }; 92 + 93 + struct bcm_kona_gpio { 94 + void __iomem *reg_base; 95 + int num_bank; 96 + raw_spinlock_t lock; 97 + struct gpio_chip gpio_chip; 98 + struct irq_domain *irq_domain; 99 + struct bcm_kona_gpio_bank banks[] __counted_by(num_bank); 82 100 }; 83 101 84 102 static inline void bcm_kona_gpio_write_lock_regs(void __iomem *reg_base, ··· 584 584 int ret; 585 585 int i; 586 586 587 - kona_gpio = devm_kzalloc(dev, sizeof(*kona_gpio), GFP_KERNEL); 588 - if (!kona_gpio) 589 - return -ENOMEM; 590 - 591 - kona_gpio->gpio_chip = template_chip; 592 - chip = &kona_gpio->gpio_chip; 593 587 ret = platform_irq_count(pdev); 594 588 if (!ret) { 595 589 dev_err(dev, "Couldn't determine # GPIO banks\n"); ··· 591 597 } else if (ret < 0) { 592 598 return dev_err_probe(dev, ret, "Couldn't determine GPIO banks\n"); 593 599 } 600 + 601 + kona_gpio = devm_kzalloc(dev, struct_size(kona_gpio, banks, ret), GFP_KERNEL); 602 + if (!kona_gpio) 603 + return -ENOMEM; 604 + 594 605 kona_gpio->num_bank = ret; 595 606 596 607 if (kona_gpio->num_bank > GPIO_MAX_BANK_NUM) { ··· 603 604 GPIO_MAX_BANK_NUM); 604 605 return -ENXIO; 605 606 } 606 - kona_gpio->banks = devm_kcalloc(dev, 607 - kona_gpio->num_bank, 608 - sizeof(*kona_gpio->banks), 609 - GFP_KERNEL); 610 - if (!kona_gpio->banks) 611 - return -ENOMEM; 612 607 608 + kona_gpio->gpio_chip = template_chip; 609 + chip = &kona_gpio->gpio_chip; 613 610 chip->parent = dev; 614 611 chip->ngpio = kona_gpio->num_bank * GPIO_PER_BANK; 615 612