[PATCH 0/5] platform: fix reference leak on failed platform_device_register()

Vastargazing posted 5 patches an hour ago
drivers/mfd/sm501.c                    | 4 +++-
drivers/misc/eeprom/digsy_mtc_eeprom.c | 4 +++-
drivers/mtd/maps/physmap-core.c        | 4 +++-
drivers/perf/arm_pmu_acpi.c            | 4 +++-
drivers/platform/chrome/cros_ec_lpc.c  | 1 +
5 files changed, 13 insertions(+), 4 deletions(-)
[PATCH 0/5] platform: fix reference leak on failed platform_device_register()
Posted by Vastargazing an hour ago
platform_device_register() calls device_initialize() before
platform_device_add(). If platform_device_add() fails, device_initialize()
has already set the struct device kref to 1, and the kref must be released
via platform_device_put(). The kernel-doc for platform_device_register()
states this explicitly:

  NOTE: _Never_ directly free @pdev after calling this function, even if
  it returned an error! Always use platform_device_put() to give up the
  reference initialised in this function instead.

This series fixes five drivers that call platform_device_register() and
return the error code without calling platform_device_put() on the failure
path. The pattern was identified by Coccinelle static analysis, confirmed
by manual inspection, and is analogous to the recently merged fix in
commit 4ff036f95238 ("ALSA: pcmtest: fix reference leak on failed device
registration").

Sites fixed:
  drivers/misc/eeprom/digsy_mtc_eeprom.c
  drivers/platform/chrome/cros_ec_lpc.c
  drivers/mtd/maps/physmap-core.c
  drivers/perf/arm_pmu_acpi.c
  drivers/mfd/sm501.c

Build tested on x86 defconfig:
  - digsy_mtc_eeprom.c: compiled cleanly
  - cros_ec_lpc.c:       compiled cleanly
  - sm501.c:             compiled cleanly
  - physmap-core.c:      pre-existing Werror in map.h header (unrelated)
  - arm_pmu_acpi.c:      ARM-only, no asm/cputype.h on x86

Vastargazing (5):
  misc: eeprom: digsy_mtc: fix reference leak on failed device
    registration
  platform/chrome: cros_ec_lpc: fix reference leak on failed device
    registration
  mtd: maps: physmap: fix reference leak on failed device registration
  perf: arm: pmu: fix reference leak on failed device registration
  mfd: sm501: fix reference leak on failed device registration

 drivers/mfd/sm501.c                    | 4 +++-
 drivers/misc/eeprom/digsy_mtc_eeprom.c | 4 +++-
 drivers/mtd/maps/physmap-core.c        | 4 +++-
 drivers/perf/arm_pmu_acpi.c            | 4 +++-
 drivers/platform/chrome/cros_ec_lpc.c  | 1 +
 5 files changed, 13 insertions(+), 4 deletions(-)

-- 
2.51.0