On Thu, Apr 01, 2021 at 12:00:39PM +0300, Dan Carpenter wrote: > Hi Keith, > > I've been trying to figure out ways Smatch can check for device managed > resources. Like adding rules that if we call dev_set_name(&foo->bar) > then it's device managaged and if there is a kfree(foo) without calling > device_put(&foo->bar) then that's a resource leak. > > Of course one of the rules is that if you call device_register(dev) then > you can't kfree(dev), it has to released with device_put(dev) and that's > true even if the register fails. But this code here feels very > intentional so maybe there is an exception to the rule? Thanks for the notice. There was no intentional use of this, so I think it was a mistake. The proposal in the follow on replies looks much better. > The patch acc02a109b04: "node: Add memory-side caching attributes" > from Mar 11, 2019, leads to the following static checker warning: > > drivers/base/node.c:285 node_init_cache_dev() > error: kfree after device_register(): 'dev' > > drivers/base/node.c > 263 static void node_init_cache_dev(struct node *node) > 264 { > 265 struct device *dev; > 266 > 267 dev = kzalloc(sizeof(*dev), GFP_KERNEL); > 268 if (!dev) > 269 return; > 270 > 271 dev->parent = &node->dev; > 272 dev->release = node_cache_release; > 273 if (dev_set_name(dev, "memory_side_cache")) > 274 goto free_dev; > 275 > 276 if (device_register(dev)) > ^^^^^^^^^^^^^^^^^^^ > 277 goto free_name; > 278 > 279 pm_runtime_no_callbacks(dev); > 280 node->cache_dev = dev; > 281 return; > 282 free_name: > 283 kfree_const(dev->kobj.name); > 284 free_dev: > 285 kfree(dev); > ^^^^^^^^^^ > 286 } > > regards, > dan carpenter