linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/2] w1: remove race with sysfs file creation
@ 2013-08-21 20:24 Greg Kroah-Hartman
  2013-08-21 20:24 ` [PATCH 2/2] w1: use default attribute groups for w1 slave devices Greg Kroah-Hartman
  0 siblings, 1 reply; 4+ messages in thread
From: Greg Kroah-Hartman @ 2013-08-21 20:24 UTC (permalink / raw)
  To: linux-kernel; +Cc: Greg Kroah-Hartman, Evgeniy Polyakov

W1 slave sysfs files are created _after_ userspace is notified that the
device has been added to the system.  Fix that race by moving the
creation/remove of the files to the bus notifier that is there for doing
this type of thing.

Cc: Evgeniy Polyakov <zbr@ioremap.net>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
---
 drivers/w1/w1.c | 111 +++++++++++++++++++++++++++++++++++++-------------------
 1 file changed, 73 insertions(+), 38 deletions(-)

diff --git a/drivers/w1/w1.c b/drivers/w1/w1.c
index 0459df84..3c798db0 100644
--- a/drivers/w1/w1.c
+++ b/drivers/w1/w1.c
@@ -587,6 +587,73 @@ end:
 	return err;
 }
 
+/*
+ * Handle sysfs file creation and removal here, before userspace is told that
+ * the device is added / removed from the system
+ */
+static int w1_bus_notify(struct notifier_block *nb, unsigned long action,
+			 void *data)
+{
+	struct device *dev = data;
+	struct w1_slave *sl;
+	int err;
+
+	/*
+	 * Only care about slave devices at the moment.  Yes, we should use a
+	 * separate "type" for this, but for now, look at the release function
+	 * to know which type it is...
+	 */
+	if (dev->release != w1_slave_release)
+		return 0;
+
+	sl = dev_to_w1_slave(dev);
+
+	switch (action) {
+	case BUS_NOTIFY_ADD_DEVICE:
+		/* Create our sysfs files before userspace is told about it */
+		/* Create "name" entry */
+		err = device_create_file(&sl->dev, &w1_slave_attr_name);
+		if (err < 0) {
+			dev_err(&sl->dev,
+				"sysfs file creation for [%s] failed. err=%d\n",
+				dev_name(&sl->dev), err);
+			return err;
+		}
+
+		/* Create "id" entry */
+		err = device_create_file(&sl->dev, &w1_slave_attr_id);
+		if (err < 0) {
+			dev_err(&sl->dev,
+				"sysfs file creation for [%s] failed. err=%d\n",
+				dev_name(&sl->dev), err);
+			return err;
+		}
+
+		/* if the family driver needs to initialize something... */
+		if (sl->family->fops && sl->family->fops->add_slave &&
+		    ((err = sl->family->fops->add_slave(sl)) < 0)) {
+			dev_err(&sl->dev,
+				"sysfs file creation for [%s] failed. err=%d\n",
+				dev_name(&sl->dev), err);
+			return err;
+		}
+
+		break;
+	case BUS_NOTIFY_DEL_DEVICE:
+		/* Remove our sysfs files */
+		if (sl->family->fops && sl->family->fops->remove_slave)
+			sl->family->fops->remove_slave(sl);
+		device_remove_file(&sl->dev, &w1_slave_attr_id);
+		device_remove_file(&sl->dev, &w1_slave_attr_name);
+		break;
+	}
+	return 0;
+}
+
+static struct notifier_block w1_bus_nb = {
+	.notifier_call = w1_bus_notify,
+};
+
 static int __w1_attach_slave_device(struct w1_slave *sl)
 {
 	int err;
@@ -615,44 +682,13 @@ static int __w1_attach_slave_device(struct w1_slave *sl)
 		return err;
 	}
 
-	/* Create "name" entry */
-	err = device_create_file(&sl->dev, &w1_slave_attr_name);
-	if (err < 0) {
-		dev_err(&sl->dev,
-			"sysfs file creation for [%s] failed. err=%d\n",
-			dev_name(&sl->dev), err);
-		goto out_unreg;
-	}
-
-	/* Create "id" entry */
-	err = device_create_file(&sl->dev, &w1_slave_attr_id);
-	if (err < 0) {
-		dev_err(&sl->dev,
-			"sysfs file creation for [%s] failed. err=%d\n",
-			dev_name(&sl->dev), err);
-		goto out_rem1;
-	}
 
-	/* if the family driver needs to initialize something... */
-	if (sl->family->fops && sl->family->fops->add_slave &&
-	    ((err = sl->family->fops->add_slave(sl)) < 0)) {
-		dev_err(&sl->dev,
-			"sysfs file creation for [%s] failed. err=%d\n",
-			dev_name(&sl->dev), err);
-		goto out_rem2;
-	}
+	dev_set_uevent_suppress(&sl->dev, false);
+	kobject_uevent(&sl->dev.kobj, KOBJ_ADD);
 
 	list_add_tail(&sl->w1_slave_entry, &sl->master->slist);
 
 	return 0;
-
-out_rem2:
-	device_remove_file(&sl->dev, &w1_slave_attr_id);
-out_rem1:
-	device_remove_file(&sl->dev, &w1_slave_attr_name);
-out_unreg:
-	device_unregister(&sl->dev);
-	return err;
 }
 
 static int w1_attach_slave_device(struct w1_master *dev, struct w1_reg_num *rn)
@@ -723,16 +759,11 @@ void w1_slave_detach(struct w1_slave *sl)
 
 	list_del(&sl->w1_slave_entry);
 
-	if (sl->family->fops && sl->family->fops->remove_slave)
-		sl->family->fops->remove_slave(sl);
-
 	memset(&msg, 0, sizeof(msg));
 	memcpy(msg.id.id, &sl->reg_num, sizeof(msg.id));
 	msg.type = W1_SLAVE_REMOVE;
 	w1_netlink_send(sl->master, &msg);
 
-	device_remove_file(&sl->dev, &w1_slave_attr_id);
-	device_remove_file(&sl->dev, &w1_slave_attr_name);
 	device_unregister(&sl->dev);
 
 	wait_for_completion(&sl->released);
@@ -1017,6 +1048,10 @@ static int __init w1_init(void)
 		goto err_out_exit_init;
 	}
 
+	retval = bus_register_notifier(&w1_bus_type, &w1_bus_nb);
+	if (retval)
+		goto err_out_bus_unregister;
+
 	retval = driver_register(&w1_master_driver);
 	if (retval) {
 		printk(KERN_ERR
-- 
1.8.3.rc0.20.gb99dd2e


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 2/2] w1: use default attribute groups for w1 slave devices
  2013-08-21 20:24 [PATCH 1/2] w1: remove race with sysfs file creation Greg Kroah-Hartman
@ 2013-08-21 20:24 ` Greg Kroah-Hartman
  2013-08-22  0:02   ` Evgeniy Polyakov
  0 siblings, 1 reply; 4+ messages in thread
From: Greg Kroah-Hartman @ 2013-08-21 20:24 UTC (permalink / raw)
  To: linux-kernel; +Cc: Greg Kroah-Hartman, Evgeniy Polyakov

As we have 2 sysfs files for the w1 slave devices, let the driver core
create / destroy them automatically by setting the default attribute
group for them, saving code and housekeeping logic.

Cc: Evgeniy Polyakov <zbr@ioremap.net>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
---
 drivers/w1/w1.c | 38 +++++++++++---------------------------
 1 file changed, 11 insertions(+), 27 deletions(-)

diff --git a/drivers/w1/w1.c b/drivers/w1/w1.c
index 3c798db0..52b6c2f0 100644
--- a/drivers/w1/w1.c
+++ b/drivers/w1/w1.c
@@ -96,14 +96,15 @@ static void w1_slave_release(struct device *dev)
 	complete(&sl->released);
 }
 
-static ssize_t w1_slave_read_name(struct device *dev, struct device_attribute *attr, char *buf)
+static ssize_t name_show(struct device *dev, struct device_attribute *attr, char *buf)
 {
 	struct w1_slave *sl = dev_to_w1_slave(dev);
 
 	return sprintf(buf, "%s\n", sl->name);
 }
+static DEVICE_ATTR_RO(name);
 
-static ssize_t w1_slave_read_id(struct device *dev,
+static ssize_t id_show(struct device *dev,
 	struct device_attribute *attr, char *buf)
 {
 	struct w1_slave *sl = dev_to_w1_slave(dev);
@@ -112,11 +113,14 @@ static ssize_t w1_slave_read_id(struct device *dev,
 	memcpy(buf, (u8 *)&sl->reg_num, count);
 	return count;
 }
+static DEVICE_ATTR_RO(id);
 
-static struct device_attribute w1_slave_attr_name =
-	__ATTR(name, S_IRUGO, w1_slave_read_name, NULL);
-static struct device_attribute w1_slave_attr_id =
-	__ATTR(id, S_IRUGO, w1_slave_read_id, NULL);
+static struct attribute *w1_slave_attrs[] = {
+	&dev_attr_name.attr,
+	&dev_attr_id.attr,
+	NULL,
+};
+ATTRIBUTE_GROUPS(w1_slave);
 
 /* Default family */
 
@@ -610,25 +614,6 @@ static int w1_bus_notify(struct notifier_block *nb, unsigned long action,
 
 	switch (action) {
 	case BUS_NOTIFY_ADD_DEVICE:
-		/* Create our sysfs files before userspace is told about it */
-		/* Create "name" entry */
-		err = device_create_file(&sl->dev, &w1_slave_attr_name);
-		if (err < 0) {
-			dev_err(&sl->dev,
-				"sysfs file creation for [%s] failed. err=%d\n",
-				dev_name(&sl->dev), err);
-			return err;
-		}
-
-		/* Create "id" entry */
-		err = device_create_file(&sl->dev, &w1_slave_attr_id);
-		if (err < 0) {
-			dev_err(&sl->dev,
-				"sysfs file creation for [%s] failed. err=%d\n",
-				dev_name(&sl->dev), err);
-			return err;
-		}
-
 		/* if the family driver needs to initialize something... */
 		if (sl->family->fops && sl->family->fops->add_slave &&
 		    ((err = sl->family->fops->add_slave(sl)) < 0)) {
@@ -643,8 +628,6 @@ static int w1_bus_notify(struct notifier_block *nb, unsigned long action,
 		/* Remove our sysfs files */
 		if (sl->family->fops && sl->family->fops->remove_slave)
 			sl->family->fops->remove_slave(sl);
-		device_remove_file(&sl->dev, &w1_slave_attr_id);
-		device_remove_file(&sl->dev, &w1_slave_attr_name);
 		break;
 	}
 	return 0;
@@ -662,6 +645,7 @@ static int __w1_attach_slave_device(struct w1_slave *sl)
 	sl->dev.driver = &w1_slave_driver;
 	sl->dev.bus = &w1_bus_type;
 	sl->dev.release = &w1_slave_release;
+	sl->dev.groups = w1_slave_groups;
 
 	dev_set_name(&sl->dev, "%02x-%012llx",
 		 (unsigned int) sl->reg_num.family,
-- 
1.8.3.rc0.20.gb99dd2e


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH 2/2] w1: use default attribute groups for w1 slave devices
  2013-08-21 20:24 ` [PATCH 2/2] w1: use default attribute groups for w1 slave devices Greg Kroah-Hartman
@ 2013-08-22  0:02   ` Evgeniy Polyakov
  2013-08-22 17:54     ` Greg Kroah-Hartman
  0 siblings, 1 reply; 4+ messages in thread
From: Evgeniy Polyakov @ 2013-08-22  0:02 UTC (permalink / raw)
  To: Greg Kroah-Hartman, linux-kernel@vger.kernel.org

hi Greg

22.08.2013, 00:24, "Greg Kroah-Hartman" <gregkh@linuxfoundation.org>:
> As we have 2 sysfs files for the w1 slave devices, let the driver core
> create / destroy them automatically by setting the default attribute
> group for them, saving code and housekeeping logic.
>
> Cc: Evgeniy Polyakov <zbr@ioremap.net>
> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

Both patches look good, thanks a lot
Acked-by: Evgeniy Polyakov <zbr@ioremap.net>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH 2/2] w1: use default attribute groups for w1 slave devices
  2013-08-22  0:02   ` Evgeniy Polyakov
@ 2013-08-22 17:54     ` Greg Kroah-Hartman
  0 siblings, 0 replies; 4+ messages in thread
From: Greg Kroah-Hartman @ 2013-08-22 17:54 UTC (permalink / raw)
  To: Evgeniy Polyakov; +Cc: linux-kernel@vger.kernel.org

On Thu, Aug 22, 2013 at 04:02:39AM +0400, Evgeniy Polyakov wrote:
> hi Greg
> 
> 22.08.2013, 00:24, "Greg Kroah-Hartman" <gregkh@linuxfoundation.org>:
> > As we have 2 sysfs files for the w1 slave devices, let the driver core
> > create / destroy them automatically by setting the default attribute
> > group for them, saving code and housekeeping logic.
> >
> > Cc: Evgeniy Polyakov <zbr@ioremap.net>
> > Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
> 
> Both patches look good, thanks a lot
> Acked-by: Evgeniy Polyakov <zbr@ioremap.net>

Great, thanks for the review.

greg k-h

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2013-08-22 17:54 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-08-21 20:24 [PATCH 1/2] w1: remove race with sysfs file creation Greg Kroah-Hartman
2013-08-21 20:24 ` [PATCH 2/2] w1: use default attribute groups for w1 slave devices Greg Kroah-Hartman
2013-08-22  0:02   ` Evgeniy Polyakov
2013-08-22 17:54     ` Greg Kroah-Hartman

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).