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

sparc: fix error handling in scan_one_device()

Once of_device_register() failed, we should call put_device() to
decrement reference count for cleanup. Or it could cause memory leak.
So fix this by calling put_device(), then the name can be freed in
kobject_cleanup().

Calling path: of_device_register() -> of_device_add() -> device_add().
As comment of device_add() says, 'if device_add() succeeds, you should
call device_del() when you want to get rid of it. If device_add() has
not succeeded, use only put_device() to drop the reference count'.

Found by code review.

Cc: stable@vger.kernel.org
Fixes: cf44bbc26cf1 ("[SPARC]: Beginnings of generic of_device framework.")
Signed-off-by: Ma Ke <make24@iscas.ac.cn>
Reviewed-by: Andreas Larsson <andreas@gaisler.com>
Signed-off-by: Andreas Larsson <andreas@gaisler.com>

authored by

Ma Ke and committed by
Andreas Larsson
302c0411 936fb512

+2
+1
arch/sparc/kernel/of_device_32.c
··· 387 387 388 388 if (of_device_register(op)) { 389 389 printk("%pOF: Could not register of device.\n", dp); 390 + put_device(&op->dev); 390 391 kfree(op); 391 392 op = NULL; 392 393 }
+1
arch/sparc/kernel/of_device_64.c
··· 677 677 678 678 if (of_device_register(op)) { 679 679 printk("%pOF: Could not register of device.\n", dp); 680 + put_device(&op->dev); 680 681 kfree(op); 681 682 op = NULL; 682 683 }