Message ID | 20230128094146.205858-2-zhongjinghua@huawei.com (mailing list archive) |
---|---|
State | Rejected |
Headers | show |
Series | scsi, driver core: fix iscsi rescan fails to create block device | expand |
On Sat, Jan 28, 2023 at 05:41:45PM +0800, Zhong Jinghua wrote: > When the dev reference count is 0, calling get_device will go from 0 to 1, You can NOT have a device reference count that is 0. If you do, you are doing something really really wrong, and there's a bug somewhere else. > which will cause errors in some place of the kernel. It's already an error in the kernel that tries to increment a reference count of 0 as that device is already freed and you are working with memory that is not present. > So introduce a > get_devcie_unless_zero method that returns NULL when the dev reference > count is 0. No, this is not ok, sorry, please never do this. Fix the caller. thanks, greg k-h
diff --git a/drivers/base/core.c b/drivers/base/core.c index d02501933467..6f17a93a3443 100644 --- a/drivers/base/core.c +++ b/drivers/base/core.c @@ -3613,6 +3613,14 @@ struct device *get_device(struct device *dev) } EXPORT_SYMBOL_GPL(get_device); +struct device __must_check *get_device_unless_zero(struct device *dev) +{ + if (!dev || !kobject_get_unless_zero(&dev->kobj)) + return NULL; + return dev; +} +EXPORT_SYMBOL_GPL(get_device_unless_zero); + /** * put_device - decrement reference count. * @dev: device in question. diff --git a/include/linux/device.h b/include/linux/device.h index 424b55df0272..c63bac6d51c8 100644 --- a/include/linux/device.h +++ b/include/linux/device.h @@ -1069,6 +1069,7 @@ extern int (*platform_notify_remove)(struct device *dev); * */ struct device *get_device(struct device *dev); +struct device __must_check *get_device_unless_zero(struct device *dev); void put_device(struct device *dev); bool kill_device(struct device *dev);
When the dev reference count is 0, calling get_device will go from 0 to 1, which will cause errors in some place of the kernel. So introduce a get_devcie_unless_zero method that returns NULL when the dev reference count is 0. Signed-off-by: Zhong Jinghua <zhongjinghua@huawei.com> --- drivers/base/core.c | 8 ++++++++ include/linux/device.h | 1 + 2 files changed, 9 insertions(+)