google_battery, google_bms: plug memleak: case 2.2.2: for_each_child_of_node() (v6.12 prep)

Case 2.2.2 from below bug:

    If returning or breaking early from within
    for_each_child_of_node(), a reference to the current device_node
    remains and needs to be dropped, either right away, or later if
    used past that loop to avoid a memleak.

These drivers call gbms_batt_id_node() at various times, and each time
either a new node (via for_each_child_of_node()) or the same node is
returned. If a new node is returned, it must be free'd, if the same
node is returned, it must not be free'd. Currently, new nodes returned
are never free'd, causing a memleak each time.

Since a new node must be free'd and the existing node doesn't, and
since callers don't necessarily know which type of node was returned,
the correct approach to plug this leak here is to always return a node
pointer that can safely be free'd. This can be achieved by simply
adding an extra reference to the existing node if applicable. This way,
callers can unconditionally free the returned node.

Do so, and use scoped cleanup to simplify all cleanup paths.

This commit plugs memleaks in normal code paths.

Test: TH
Bug: 370679843
Change-Id: I5317821c2299a175fb16f6458ba8ee770c31211f
Signed-off-by: AndrĂ© Draszik <[email protected]>
Signed-off-by: Will McVicker <[email protected]>
2 files changed
tree: abc752f1de5e3b7f6b35e933d9343442d7d1baf0
  1. Documentation/
  2. misc/
  3. .checkpatch.conf
  4. BUILD.bazel
  5. gbms_power_supply.h
  6. gbms_storage.c
  7. gbms_storage.h
  8. google_battery.c
  9. google_bms.c
  10. google_bms.h
  11. google_ccd.c
  12. google_charger.c
  13. google_cpm.c
  14. google_dc_pps.c
  15. google_dc_pps.h
  16. google_dock.c
  17. google_dual_batt_gauge.c
  18. google_eeprom.c
  19. google_eeprom_01.c
  20. google_psy.h
  21. google_ttf.c
  22. Kconfig
  23. ln8411_A1.h
  24. ln8411_charger.h
  25. ln8411_driver.c
  26. ln8411_gbms_pps.c
  27. ln8411_regs.h
  28. Makefile
  29. max1720x.h
  30. max1720x_battery.c
  31. max1720x_battery.h
  32. max1720x_outliers.c
  33. max20339.c
  34. max77729_charger.c
  35. max77729_pmic.c
  36. max77729_uic.c
  37. max77759.h
  38. max77759_A1_regs.h
  39. max77759_charger.c
  40. max77759_charger.h
  41. max77759_maxq.c
  42. max77759_maxq.h
  43. max77759_regs.h
  44. max77759_usecase.c
  45. max77759_usecase.h
  46. max77759_v0p65_121919_regs.h
  47. max77779.h
  48. max77779_0227_v2.h
  49. max77779_charger.c
  50. max77779_charger.h
  51. max77779_charger_i2c.c
  52. max77779_fg.c
  53. max77779_fg.h
  54. max77779_fg_i2c.c
  55. max77779_fg_model.c
  56. max77779_fwupdate.c
  57. max77779_i2cm.c
  58. max77779_i2cm.h
  59. max77779_i2cm_i2c.c
  60. max77779_pmic.c
  61. max77779_pmic.h
  62. max77779_pmic_i2c.c
  63. max77779_pmic_irq.c
  64. max77779_pmic_pinctrl.c
  65. max77779_pmic_sgpio.c
  66. max77779_regs.h
  67. max77779_regs_1211.h
  68. max77779_sp.c
  69. max77779_sp.h
  70. max77779_sp_i2c.c
  71. max77779_usecase.c
  72. max77779_usecase.h
  73. max77779_v20240115_regs.h
  74. max77779_vimon.c
  75. max77779_vimon.h
  76. max77779_vimon_i2c.c
  77. max777x9_bcl.h
  78. max_m5.c
  79. max_m5.h
  80. max_m5_reg.h
  81. maxfg_common.c
  82. maxfg_common.h
  83. OWNERS
  84. p9221-dt-bindings.h
  85. p9221_charger.c
  86. p9221_charger.h
  87. p9221_chip.c
  88. pca9468_charger.c
  89. pca9468_charger.h
  90. pca9468_gbms_pps.c
  91. pca9468_regs.h
  92. pmic-voter-compat.c
  93. pmic-voter.h
  94. PREUPLOAD.cfg
  95. qmath.h
  96. rt9471_charger.c
  97. rt9471_charger.h