Skip to contentSkip to conversation

sysfs: add explicit sysfs_cache object

PATCH Applied Alan Jenkins 1 participant 2 messages Sep 4, 2008 – Sep 9, 2008

lore
Share & tools
lore
#0 lore raw

PATCH 3/3 sysfs: add explicit sysfs_cache object

The implicit caching in sysfs is unsuitable for long-lived programs; it would fill up with stale data. Fortunately libudev doesn't use the relevant functions, but this is not very clear.

Add an explicit sysfs_cache parameter to the relevant functions (sysfs_attr_get_value, sysfs_device_get and variants). The parameter can be NULL for no caching.

sysfs_device objects returned by these functions must now be explicitly released by calling sysfs_device_cleanup().

This makes traversing parent devices quite hairy, so we hide the details in udev_device_parent_iter_*.

extras/usb_id/usb_id.c +32 -23
index fd4d2f2..f8e38f0 100644
@@ -228,33 +228,34 @@ static int usb_id(const char *devpath)
 	char if_subclass[NAME_SIZE];
 	int if_class_num;
 	int protocol = 0;
+	int retval = 1;
 	int status;
 
 	dbg("devpath %s\n", devpath);
 
 	/* get all usb specific information: dev_interface, if_class, dev_usb */
-	dev = sysfs_device_get(devpath);
+	dev = sysfs_device_get(NULL, devpath);
 	if (dev = NULL) {
 		err("unable to access '%s'\n", devpath);
-		return 1;
+		goto out;
 	}
 
 	/* usb interface directory */
-	dev_interface = sysfs_device_get_parent_with_subsystem(dev, "usb");
+	dev_interface = sysfs_device_get_parent_with_subsystem(NULL, dev, "usb");
 	if (dev_interface = NULL) {
 		info("unable to access usb_interface device of '%s'\n", devpath);
-		return 1;
+		goto out;
 	}
 
-	status = sysfs_attr_get_value(dev_interface->devpath, "bInterfaceClass",
+	status = sysfs_attr_get_value(NULL, dev_interface->devpath, "bInterfaceClass",
 				      if_class, sizeof(if_class));
 	if (!status) {
 		info("%s: cannot get bInterfaceClass attribute\n", dev_interface->kernel);
-		return 1;
+		goto out;
 	}
 	if_class_num = strtoul(if_class, NULL, 16);
 	if (if_class_num = 8) {
-		status = sysfs_attr_get_value(dev_interface->devpath, "bInterfaceSubClass",
+		status = sysfs_attr_get_value(NULL, dev_interface->devpath, "bInterfaceSubClass",
 					      if_subclass, sizeof(if_subclass));
 		if (!status)
 			protocol = set_usb_mass_storage_ifsubtype(type_str, if_subclass, sizeof(type_str)-1);
@@ -264,10 +265,10 @@ static int usb_id(const char *devpath)
 	info("%s: if_class %d protocol %d\n", dev_interface->devpath, if_class_num, protocol);
 
 	/* usb device directory */
-	dev_usb = sysfs_device_get_parent_with_subsystem(dev_interface, "usb");
+	dev_usb = sysfs_device_get_parent_with_subsystem(NULL, dev_interface, "usb");
 	if (!dev_usb) {
 		info("unable to find parent 'usb' device of '%s'\n", devpath);
-		return 1;
+		goto out;
 	}
 
 	/* mass storage */
@@ -280,7 +281,7 @@ static int usb_id(const char *devpath)
 		int host, bus, target, lun;
 
 		/* get scsi device */
-		dev_scsi = sysfs_device_get_parent_with_subsystem(dev, "scsi");
+		dev_scsi = sysfs_device_get_parent_with_subsystem(NULL, dev, "scsi");
 		if (dev_scsi = NULL) {
 			info("unable to find parent 'scsi' device of '%s'\n", devpath);
 			goto fallback;
@@ -291,7 +292,7 @@ static int usb_id(const char *devpath)
 		}
 
 		/* Generic SPC-2 device */
-		status = sysfs_attr_get_value(dev_scsi->devpath, "vendor",
+		status = sysfs_attr_get_value(NULL, dev_scsi->devpath, "vendor",
 					      scsi_vendor, sizeof(scsi_vendor));
 		if (!status) {
 			info("%s: cannot get SCSI vendor attribute\n", dev_scsi->kernel);
@@ -299,7 +300,7 @@ static int usb_id(const char *devpath)
 		}
 		set_str(vendor_str, scsi_vendor, sizeof(vendor_str)-1);
 
-		status = sysfs_attr_get_value(dev_scsi->devpath, "model",
+		status = sysfs_attr_get_value(NULL, dev_scsi->devpath, "model",
 					      scsi_model, sizeof(scsi_model));
 		if (!status) {
 			info("%s: cannot get SCSI model attribute\n", dev_scsi->kernel);
@@ -307,7 +308,7 @@ static int usb_id(const char *devpath)
 		}
 		set_str(model_str, scsi_model, sizeof(model_str)-1);
 
-		status = sysfs_attr_get_value(dev_scsi->devpath, "type",
+		status = sysfs_attr_get_value(NULL, dev_scsi->devpath, "type",
 					      scsi_type, sizeof(scsi_type));
 		if (!status) {
 			info("%s: cannot get SCSI type attribute\n", dev_scsi->kernel);
@@ -315,7 +316,7 @@ static int usb_id(const char *devpath)
 		}
 		set_scsi_type(type_str, scsi_type, sizeof(type_str)-1);
 
-		status = sysfs_attr_get_value(dev_scsi->devpath, "rev",
+		status = sysfs_attr_get_value(NULL, dev_scsi->devpath, "rev",
 					      scsi_rev, sizeof(scsi_rev));
 		if (!status) {
 			info("%s: cannot get SCSI revision attribute\n", dev_scsi->kernel);
@@ -337,16 +338,16 @@ fallback:
 
 		status = 0;
 		if (!use_num_info)
-			status = sysfs_attr_get_value(dev_usb->devpath, "manufacturer",
+			status = sysfs_attr_get_value(NULL, dev_usb->devpath, "manufacturer",
 						      usb_vendor, sizeof(usb_vendor));
 
 		if (!status)
-			status = sysfs_attr_get_value(dev_usb->devpath, "idVendor",
+			status = sysfs_attr_get_value(NULL, dev_usb->devpath, "idVendor",
 						      usb_vendor, sizeof(usb_vendor));
 
 		if (!status) {
 			info("No USB vendor information available\n");
-			return 1;
+			goto out;
 		}
 		set_str(vendor_str, usb_vendor, sizeof(vendor_str)-1);
 	}
@@ -356,16 +357,16 @@ fallback:
 
 		status = 0;
 		if (!use_num_info)
-			status = sysfs_attr_get_value(dev_usb->devpath, "product",
+			status = sysfs_attr_get_value(NULL, dev_usb->devpath, "product",
 						      usb_model, sizeof(usb_model));
 
 		if (!status)
-			status = sysfs_attr_get_value(dev_usb->devpath, "idProduct",
+			status = sysfs_attr_get_value(NULL, dev_usb->devpath, "idProduct",
 						      usb_model, sizeof(usb_model));
 
 		if (!status) {
 			dbg("No USB model information available\n");
-			return 1;
+			goto out;
 		}
 		set_str(model_str, usb_model, sizeof(model_str)-1);
 	}
@@ -373,7 +374,7 @@ fallback:
 	if (revision_str[0] = '\0') {
 		char usb_rev[NAME_SIZE];
 
-		status = sysfs_attr_get_value(dev_usb->devpath, "bcdDevice",
+		status = sysfs_attr_get_value(NULL, dev_usb->devpath, "bcdDevice",
 					      usb_rev, sizeof(usb_rev));
 		if (status)
 			set_str(revision_str, usb_rev, sizeof(revision_str)-1);
@@ -382,12 +383,20 @@ fallback:
 	if (serial_str[0] = '\0') {
 		char usb_serial[NAME_SIZE];
 
-		status = sysfs_attr_get_value(dev_usb->devpath, "serial",
+		status = sysfs_attr_get_value(NULL, dev_usb->devpath, "serial",
 					      usb_serial, sizeof(usb_serial));
 		if (status)
 			set_str(serial_str, usb_serial, sizeof(serial_str)-1);
 	}
-	return 0;
+
+	/* success */
+	retval = 0;
+
+out:
+	sysfs_device_cleanup(dev);
+	sysfs_device_cleanup(dev_interface);
+	sysfs_device_cleanup(dev_usb);
+	return retval;
 }
 
 int main(int argc, char **argv)
udev/test-udev.c +13 -9
index 07628f7..878c477 100644
@@ -62,7 +62,6 @@ static void asmlinkage sig_handler(int signum)
 
 int main(int argc, char *argv[])
 {
-	struct sysfs_device *dev;
 	struct udevice *udev;
 	const char *maj, *min;
 	struct udev_rules rules;
@@ -133,18 +132,23 @@ int main(int argc, char *argv[])
 	sysfs_init();
 	udev_rules_init(&rules, 0);
 
-	dev = sysfs_device_get(devpath);
-	if (dev = NULL) {
-		info("unable to open '%s'\n", devpath);
-		goto fail;
-	}
-
 	udev = udev_device_init();
 	if (udev = NULL)
 		goto fail;
 
+	/* valgrind can only verify sysfs_device_cleanup() calls if cache is disabled */
+#if 0
+	udev->cache = sysfs_cache_init();
+	if (udev->cache = NULL)
+		goto fail;
+#endif
+
 	/* override built-in sysfs device */
-	udev->dev = dev;
+	udev->dev = sysfs_device_get(udev->cache, devpath);
+	if (udev->dev = NULL) {
+		info("unable to open '%s'\n", devpath);
+		goto fail;
+	}
 	strlcpy(udev->action, action, sizeof(udev->action));
 
 	/* get dev_t from environment, which is needed for "remove" to work, "add" works also from sysfs */
@@ -164,8 +168,8 @@ int main(int argc, char *argv[])
 	if (retval = 0 && !udev->ignore_device && udev_run)
 		udev_rules_run(udev);
 
-	udev_device_cleanup(udev);
 fail:
+	udev_device_cleanup(udev);
 	udev_rules_cleanup(&rules);
 	sysfs_cleanup();
 	selinux_exit();
udev/udev.h +18 -5
index 28b59db..1037734 100644
@@ -48,9 +48,13 @@
 
 struct udev_rules;
 
+struct sysfs_cache {
+	struct list_head dev_list;		/* device cache */
+	struct list_head attr_list;		/* attribute value cache */
+};
+
 struct sysfs_device {
 	struct list_head node;			/* for device cache */
-	struct sysfs_device *parent;		/* already cached parent*/
 	char devpath[PATH_SIZE];
 	char subsystem[NAME_SIZE];		/* $class, $bus, drivers, module */
 	char kernel[NAME_SIZE];			/* device instance name */
@@ -63,6 +67,7 @@ struct udevice {
 	struct sysfs_device *dev;		/* points to dev_local by default */
 	struct sysfs_device dev_local;
 	struct sysfs_device *dev_parent;	/* current parent device used for matching */
+	struct sysfs_cache *cache;
 	char action[NAME_SIZE];
 	char *devpath_old;
 
@@ -103,6 +108,9 @@ extern void udev_config_init(void);
 /* udev_device.c */
 extern struct udevice *udev_device_init(void);
 extern void udev_device_cleanup(struct udevice *udev);
+extern void udev_device_parents_iter_init(struct udevice *udev, struct sysfs_device **iter);
+extern void udev_device_parents_iter_next(struct udevice *udev, struct sysfs_device **iter);
+extern void udev_device_parents_iter_cleanup(struct udevice *udev, struct sysfs_device **iter);
 extern dev_t udev_device_get_devt(struct udevice *udev);
 
 /* udev_device_event.c */
@@ -112,12 +120,17 @@ extern int udev_device_event(struct udev_rules *rules, struct udevice *udev);
 extern char sysfs_path[PATH_SIZE];
 extern int sysfs_init(void);
 extern void sysfs_cleanup(void);
+extern struct sysfs_cache *sysfs_cache_init(void);
+extern void sysfs_cache_cleanup(struct sysfs_cache *cache);
 extern void sysfs_device_set_values(struct sysfs_device *dev, const char *devpath,
 				    const char *subsystem, const char *driver);
-extern struct sysfs_device *sysfs_device_get(const char *devpath);
-extern struct sysfs_device *sysfs_device_get_parent(struct sysfs_device *dev);
-extern struct sysfs_device *sysfs_device_get_parent_with_subsystem(struct sysfs_device *dev, const char *subsystem);
-extern int sysfs_attr_get_value(const char *devpath, const char *attr_name, char *value, size_t len);
+extern struct sysfs_device *sysfs_device_get(struct sysfs_cache *cache, const char *devpath);
+extern struct sysfs_device *sysfs_device_get_parent(struct sysfs_cache *cache, struct sysfs_device *dev);
+extern struct sysfs_device *sysfs_device_get_parent_with_subsystem(struct sysfs_cache *cache,
+								   struct sysfs_device *dev, const char *subsystem);
+extern void sysfs_device_cleanup(struct sysfs_device *dev);
+int sysfs_attr_get_value(struct sysfs_cache *cache, const char *devpath, const char *attr_name,
+			 char *value, size_t len);
 extern int sysfs_resolve_link(char *path, size_t size);
 extern int sysfs_lookup_devpath_by_subsys_id(char *devpath, size_t len, const char *subsystem, const char *id);
 
udev/udev_device.c +28 -1
index 13000b0..a81d80e 100644
@@ -63,19 +63,46 @@ void udev_device_cleanup(struct udevice *udev)
 {
 	if (udev = NULL)
 		return;
+	if (udev->dev != &udev->dev_local)
+		sysfs_device_cleanup(udev->dev);
+	udev_device_parents_iter_cleanup(udev, &udev->dev_parent);
+	sysfs_cache_cleanup(udev->cache);
 	name_list_cleanup(&udev->symlink_list);
 	name_list_cleanup(&udev->run_list);
 	name_list_cleanup(&udev->env_list);
 	free(udev);
 }
 
+void udev_device_parents_iter_init(struct udevice *udev, struct sysfs_device **iter)
+{
+	*iter = udev->dev;
+}
+
+void udev_device_parents_iter_next(struct udevice *udev, struct sysfs_device **iter)
+{
+	struct sysfs_device *dev;
+
+	dev = *iter;
+	*iter = sysfs_device_get_parent(udev->cache, dev);
+
+	if (dev != udev->dev)
+		sysfs_device_cleanup(dev);
+}
+
+void udev_device_parents_iter_cleanup(struct udevice *udev, struct sysfs_device **iter)
+{
+	if (*iter != udev->dev)
+		sysfs_device_cleanup(*iter);
+	*iter = NULL;
+}
+
 dev_t udev_device_get_devt(struct udevice *udev)
 {
 	char attr[NAME_SIZE];
 	unsigned int maj, min;
 
 	/* read it from sysfs  */
-	if (sysfs_attr_get_value(udev->dev->devpath, "dev", attr, sizeof(attr))) {
+	if (sysfs_attr_get_value(udev->cache, udev->dev->devpath, "dev", attr, sizeof(attr))) {
 		if (sscanf(attr, "%u:%u", &maj, &min) = 2)
 			return makedev(maj, min);
 	}
udev/udev_node.c +1 -1
index a29c4b7..f489b67 100644
@@ -380,7 +380,7 @@ int udev_node_add(struct udevice *udev)
 		int range;
 
 		/* take the maximum registered minor range */
-		if (sysfs_attr_get_value(udev->dev->devpath, "range", attr, sizeof(attr))) {
+		if (sysfs_attr_get_value(udev->cache, udev->dev->devpath, "range", attr, sizeof(attr))) {
 			range = atoi(attr);
 			if (range > 1)
 				udev->partitions = range-1;
udev/udev_rules.c +52 -44

172 lines not on this page · raw message

udev/udev_sysfs.c +69 -34

219 lines not on this page · raw message

udev/udevinfo.c +10 -4
index b76d11d..50d1bef 100644
@@ -66,7 +66,7 @@ static void print_all_attributes(const char *devpath, const char *key)
 			if (S_ISLNK(statbuf.st_mode))
 				continue;
 
-			if (!sysfs_attr_get_value(devpath, dent->d_name, value, sizeof(value)))
+			if (!sysfs_attr_get_value(NULL, devpath, dent->d_name, value, sizeof(value)))
 				continue;
 			len = strlen(value);
 			dbg("attr '%s'='%s'(%zi)\n", dent->d_name, value, len);
@@ -89,7 +89,7 @@ static int print_device_chain(const char *devpath)
 {
 	struct sysfs_device *dev;
 
-	dev = sysfs_device_get(devpath);
+	dev = sysfs_device_get(NULL, devpath);
 	if (dev = NULL)
 		return -1;
 
@@ -109,9 +109,14 @@ static int print_device_chain(const char *devpath)
 
 	/* walk up the chain of devices */
 	while (1) {
-		dev = sysfs_device_get_parent(dev);
-		if (dev = NULL)
+		struct sysfs_device *dev_parent;
+
+		dev_parent = sysfs_device_get_parent(NULL, dev);
+		if (dev_parent = NULL)
 			break;
+		sysfs_device_cleanup(dev);
+		dev = dev_parent;
+
 		printf("  looking at parent device '%s':\n", dev->devpath);
 		printf("    KERNELS=\"%s\"\n", dev->kernel);
 		printf("    SUBSYSTEMS=\"%s\"\n", dev->subsystem);
@@ -120,6 +125,7 @@ static int print_device_chain(const char *devpath)
 		print_all_attributes(dev->devpath, "ATTRS");
 	}
 
+	sysfs_device_cleanup(dev);
 	return 0;
 }
 
udev/udevtest.c +15 -11
index 63603aa..cf96019 100644
@@ -78,7 +78,6 @@ int udevtest(int argc, char *argv[])
 	const char *subsystem = NULL;
 	const char *devpath = NULL;
 	struct udevice *udev;
-	struct sysfs_device *dev;
 	struct udev_rules rules = {};
 	int retval;
 	int rc = 0;
@@ -150,25 +149,30 @@ int udevtest(int argc, char *argv[])
 	if (strncmp(devpath, sysfs_path, strlen(sysfs_path)) = 0)
 		devpath = &devpath[strlen(sysfs_path)];
 
-	dev = sysfs_device_get(devpath);
-	if (dev = NULL) {
-		fprintf(stderr, "unable to open device '%s'\n", devpath);
+	udev = udev_device_init();
+	if (udev = NULL) {
+		fprintf(stderr, "error initializing device\n");
 		rc = 2;
 		goto exit;
 	}
 
-	udev = udev_device_init();
-	if (udev = NULL) {
-		fprintf(stderr, "error initializing device\n");
+	udev->cache = sysfs_cache_init();
+	if (udev->cache = NULL) {
+		fprintf(stderr, "error initializing sysfs cache\n");
 		rc = 3;
 		goto exit;
 	}
 
+	/* override built-in sysfs device */
+	udev->dev = sysfs_device_get(udev->cache, devpath);
+	if (udev->dev = NULL) {
+		fprintf(stderr, "unable to open device '%s'\n", devpath);
+		rc = 4;
+		goto exit;
+	}
 	if (subsystem != NULL)
-		strlcpy(dev->subsystem, subsystem, sizeof(dev->subsystem));
+		strlcpy(udev->dev->subsystem, subsystem, sizeof(udev->dev->subsystem));
 
-	/* override built-in sysfs device */
-	udev->dev = dev;
 	strlcpy(udev->action, action, sizeof(udev->action));
 	udev->devt = udev_device_get_devt(udev);
 
@@ -198,9 +202,9 @@ int udevtest(int argc, char *argv[])
 			info("run: '%s'\n", program);
 		}
 	}
-	udev_device_cleanup(udev);
 
 exit:
+	udev_device_cleanup(udev);
 	udev_rules_cleanup(&rules);
 	sysfs_cleanup();
 	return rc;
#1 lore raw
Quoted 23 lines · Alan Jenkins
udev/test-udev.c +0 -7
index 07628f7..878c477 100644
@@ -62,7 +62,6 @@ static void asmlinkage sig_handler(int signum)
 
 int main(int argc, char *argv[])
 {
-	struct sysfs_device *dev;
 	struct udevice *udev;
 	const char *maj, *min;
 	struct udev_rules rules;
@@ -133,18 +132,23 @@ int main(int argc, char *argv[])
 	sysfs_init();
 	udev_rules_init(&rules, 0);
 
-	dev = sysfs_device_get(devpath);
-	if (dev = NULL) {
-		info("unable to open '%s'\n", devpath);
-		goto fail;
-	}
-
 	udev = udev_device_init();

udev/test-udev.c int main(int argc, char *argv[]) · line 145

Quoted 10 lines
udev/test-udev.c +6 -0
 	if (udev = NULL)
 		goto fail;
 
+	/* valgrind can only verify sysfs_device_cleanup() calls if cache is disabled */
+#if 0
+	udev->cache = sysfs_cache_init();
+	if (udev->cache = NULL)
+		goto fail;
+#endif
 

Sorry, I didn't test these patches properly for performance. It's right to disable the cache in udev-test, but then I forgot to enable the cache in udevd :-(.

Here's the missing code:

udev/udevd.c +5 -0
index 68cf264..b5e5810 100644
@@ -129,6 +129,11 @@ static int udev_event_process(struct udevd_uevent_msg *msg)
 	udev = udev_device_init();
 	if (udev = NULL)
 		return -1;
+
+	udev->cache = sysfs_cache_init();
+	if (udev->cache = NULL)
+		return -1;
+
 	strlcpy(udev->action, msg->action, sizeof(udev->action));
 	sysfs_device_set_values(udev->dev, msg->devpath, msg->subsystem, msg->driver);
 	udev->devpath_old = msg->devpath_old;