Skip to content

Commit 157c69c

Browse files
committed
asustor_main.c: Avoid leaking struct gpio_device
turns out that the gpio_device* returned by gpio_device_find_by_label() must be "freed" (or the refcounter decremented) by calling gpio_device_put(dev). While fixing that, I refactored the code for getting the GPIO chip's base number a bit so we don't have to use the (deprecated) gpio_device_get_chip() anymore.
1 parent 4d305a5 commit 157c69c

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

asustor_main.c

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -391,21 +391,30 @@ static struct platform_device *__init asustor_create_pdev(const char *name,
391391
static int gpiochip_match_name(struct gpio_chip *chip, void *data)
392392
{
393393
const char *name = data;
394-
395394
return !strcmp(chip->label, name);
396395
}
397396

398-
static struct gpio_chip *find_chip_by_name(const char *name)
397+
// -1 means "not found"
398+
static int get_gpio_base_for_chipname(const char *name)
399399
{
400-
return gpiochip_find((void *)name, gpiochip_match_name);
400+
struct gpio_chip *gc = gpiochip_find((void *)name, gpiochip_match_name);
401+
return (gc != NULL) ? gc->base : -1;
401402
}
402403
#else
403404
// DG: kernel 6.7 removed gpiochip_find() and introduced gpio_device_find()
404405
// and friends instead
405-
static struct gpio_chip *find_chip_by_name(const char *name)
406+
407+
// -1 means "not found"
408+
static int get_gpio_base_for_chipname(const char *name)
406409
{
410+
int ret = -1;
407411
struct gpio_device *dev = gpio_device_find_by_label(name);
408-
return (dev != NULL) ? gpio_device_get_chip(dev) : NULL;
412+
if (dev != NULL) {
413+
ret = gpio_device_get_base(dev);
414+
// make sure to decrement the reference count of dev
415+
gpio_device_put(dev);
416+
}
417+
return ret;
409418
}
410419
#endif
411420

@@ -549,12 +558,12 @@ static int __init asustor_init(void)
549558
for (; keys_table->key != NULL; keys_table++) {
550559
if (i == keys_table->idx) {
551560
// add the GPIO chip's base, so we get the absolute (global) gpio number
552-
struct gpio_chip *chip =
553-
find_chip_by_name(keys_table->key);
554-
if (chip == NULL)
561+
const char *cn = keys_table->key;
562+
int gpio_base = get_gpio_base_for_chipname(cn);
563+
if (gpio_base == -1)
555564
continue;
556565
asustor_gpio_keys_table[i].gpio =
557-
chip->base + keys_table->chip_hwnum;
566+
gpio_base + keys_table->chip_hwnum;
558567
}
559568
}
560569
}

0 commit comments

Comments
 (0)