mm: bdi: fix race in bdi_class device creation

There is a race from when a device is created with device_create() and
then the drvdata is set with a call to dev_set_drvdata() in which a
sysfs file could be open, yet the drvdata will be NULL, causing all
sorts of bad things to happen.

This patch fixes the problem by using the new function,
device_create_vargs().

Many thanks to Arthur Jones <ajones@riverbed.com> for reporting the bug,
and testing patches out.

Cc: Kay Sievers <kay.sievers@vrfy.org>
Cc: Arthur Jones <ajones@riverbed.com>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Miklos Szeredi <mszeredi@suse.cz>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
This commit is contained in:
Greg Kroah-Hartman 2008-05-15 13:44:08 -07:00
parent 8882b39421
commit 19051c5035

View file

@ -172,30 +172,22 @@ postcore_initcall(bdi_class_init);
int bdi_register(struct backing_dev_info *bdi, struct device *parent, int bdi_register(struct backing_dev_info *bdi, struct device *parent,
const char *fmt, ...) const char *fmt, ...)
{ {
char *name;
va_list args; va_list args;
int ret = 0; int ret = 0;
struct device *dev; struct device *dev;
va_start(args, fmt); va_start(args, fmt);
name = kvasprintf(GFP_KERNEL, fmt, args); dev = device_create_vargs(bdi_class, parent, MKDEV(0, 0), bdi, fmt, args);
va_end(args); va_end(args);
if (!name)
return -ENOMEM;
dev = device_create(bdi_class, parent, MKDEV(0, 0), name);
if (IS_ERR(dev)) { if (IS_ERR(dev)) {
ret = PTR_ERR(dev); ret = PTR_ERR(dev);
goto exit; goto exit;
} }
bdi->dev = dev; bdi->dev = dev;
dev_set_drvdata(bdi->dev, bdi); bdi_debug_register(bdi, dev_name(dev));
bdi_debug_register(bdi, name);
exit: exit:
kfree(name);
return ret; return ret;
} }
EXPORT_SYMBOL(bdi_register); EXPORT_SYMBOL(bdi_register);