Commit ec851b2
gpiolib: fix memory leak in gpiochip_setup_dev()
Here is a backtrace report about memory leak detected in
gpiochip_setup_dev():
unreferenced object 0xffff88810b406400 (size 512):
comm "python3", pid 1682, jiffies 4295346908 (age 24.090s)
backtrace:
kmalloc_trace
device_add device_private_init at drivers/base/core.c:3361
(inlined by) device_add at drivers/base/core.c:3411
cdev_device_add
gpiolib_cdev_register
gpiochip_setup_dev
gpiochip_add_data_with_key
gcdev_register() & gcdev_unregister() would call device_add() &
device_del() (no matter CONFIG_GPIO_CDEV is enabled or not) to
register/unregister device.
However, if device_add() succeeds, some resource (like
struct device_private allocated by device_private_init())
is not released by device_del().
Therefore, after device_add() succeeds by gcdev_register(), it
needs to call put_device() to release resource in the error handle
path.
Here we move forward the register of release function, and let it
release every piece of resource by put_device() instead of kfree().
While at it, fix another subtle issue, i.e. when gc->ngpio is equal
to 0, we still call kcalloc() and, in case of further error, kfree()
on the ZERO_PTR pointer, which is not NULL. It's not a bug per se,
but rather waste of the resources and potentially wrong expectation
about contents of the gdev->descs variable.
Fixes: 159f3cd ("gpiolib: Defer gpio device setup until after gpiolib initialization")
Signed-off-by: Zeng Heng <[email protected]>
Co-developed-by: Andy Shevchenko <[email protected]>
Signed-off-by: Andy Shevchenko <[email protected]>
Signed-off-by: Bartosz Golaszewski <[email protected]>1 parent b7b275e commit ec851b2
1 file changed
+26
-16
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
526 | 526 | | |
527 | 527 | | |
528 | 528 | | |
| 529 | + | |
| 530 | + | |
| 531 | + | |
529 | 532 | | |
530 | 533 | | |
531 | 534 | | |
532 | 535 | | |
533 | | - | |
534 | | - | |
535 | 536 | | |
536 | 537 | | |
537 | 538 | | |
| |||
597 | 598 | | |
598 | 599 | | |
599 | 600 | | |
600 | | - | |
601 | 601 | | |
| 602 | + | |
| 603 | + | |
602 | 604 | | |
603 | | - | |
604 | 605 | | |
605 | 606 | | |
606 | 607 | | |
| |||
647 | 648 | | |
648 | 649 | | |
649 | 650 | | |
650 | | - | |
651 | | - | |
652 | | - | |
653 | | - | |
654 | | - | |
655 | | - | |
656 | 651 | | |
657 | 652 | | |
658 | 653 | | |
659 | 654 | | |
660 | | - | |
| 655 | + | |
| 656 | + | |
661 | 657 | | |
662 | 658 | | |
663 | 659 | | |
| |||
668 | 664 | | |
669 | 665 | | |
670 | 666 | | |
671 | | - | |
| 667 | + | |
672 | 668 | | |
673 | 669 | | |
674 | 670 | | |
675 | 671 | | |
676 | 672 | | |
677 | 673 | | |
678 | 674 | | |
679 | | - | |
| 675 | + | |
680 | 676 | | |
681 | 677 | | |
682 | 678 | | |
683 | 679 | | |
684 | 680 | | |
685 | 681 | | |
| 682 | + | |
| 683 | + | |
| 684 | + | |
| 685 | + | |
| 686 | + | |
| 687 | + | |
686 | 688 | | |
687 | 689 | | |
688 | 690 | | |
| |||
701 | 703 | | |
702 | 704 | | |
703 | 705 | | |
| 706 | + | |
704 | 707 | | |
705 | 708 | | |
706 | 709 | | |
707 | | - | |
708 | 710 | | |
| 711 | + | |
| 712 | + | |
709 | 713 | | |
710 | 714 | | |
711 | 715 | | |
| |||
816 | 820 | | |
817 | 821 | | |
818 | 822 | | |
| 823 | + | |
| 824 | + | |
| 825 | + | |
| 826 | + | |
| 827 | + | |
819 | 828 | | |
820 | 829 | | |
821 | 830 | | |
| |||
829 | 838 | | |
830 | 839 | | |
831 | 840 | | |
| 841 | + | |
| 842 | + | |
832 | 843 | | |
833 | 844 | | |
834 | 845 | | |
835 | | - | |
| 846 | + | |
836 | 847 | | |
837 | 848 | | |
838 | | - | |
839 | 849 | | |
840 | 850 | | |
841 | 851 | | |
| |||
0 commit comments