linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/2] dmi-id: export oem strings to sysfs
@ 2016-07-15  9:42 Allen Hung
  2016-07-15  9:42 ` [PATCH 1/2] dmi-id: don't free dev structure after calling device_register Allen Hung
  2016-07-15  9:42 ` [PATCH 2/2] dmi-id: add dmi/id/oem group for exporting oem strings to sysfs Allen Hung
  0 siblings, 2 replies; 5+ messages in thread
From: Allen Hung @ 2016-07-15  9:42 UTC (permalink / raw)
  To: Jean Delvare, linux-kernel; +Cc: Mario Limonciello, Allen Hung

The oem strings in DMI BIOS have been parsed and stored as dmi devices
in dmi_scan.c but they are not exported to userspace via sysfs.

The series of patches 1/2) fix the error exit code which may cause kernel
panic if error happened during adding the oem strings to sysfs, and (2/2)
export oem strings to a new group "oem" under sysfs device 
/sys/class/dmi/id.

Alle Hung (2):
  dmi-id: don't free dev structure after calling device_register
  dmi-id: add dmi/id/oem group for exporting oem strings to sysfs

 drivers/firmware/dmi-id.c | 116 ++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 112 insertions(+), 4 deletions(-)

-- 
2.7.4

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

* [PATCH 1/2] dmi-id: don't free dev structure after calling device_register
  2016-07-15  9:42 [PATCH 0/2] dmi-id: export oem strings to sysfs Allen Hung
@ 2016-07-15  9:42 ` Allen Hung
  2016-07-15  9:42 ` [PATCH 2/2] dmi-id: add dmi/id/oem group for exporting oem strings to sysfs Allen Hung
  1 sibling, 0 replies; 5+ messages in thread
From: Allen Hung @ 2016-07-15  9:42 UTC (permalink / raw)
  To: Jean Delvare, linux-kernel; +Cc: Mario Limonciello, Allen Hung

dmi_dev is freed in error exit code but, according to the document
of device_register, it should never directly free device structure
after calling this function, even if it returned an error! Use
put_device() instead.

Signed-off-by: Allen Hung <allen_hung@dell.com>
---
 drivers/firmware/dmi-id.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/drivers/firmware/dmi-id.c b/drivers/firmware/dmi-id.c
index 94a58a0..44c0139 100644
--- a/drivers/firmware/dmi-id.c
+++ b/drivers/firmware/dmi-id.c
@@ -229,14 +229,14 @@ static int __init dmi_id_init(void)
 
 	ret = device_register(dmi_dev);
 	if (ret)
-		goto fail_free_dmi_dev;
+		goto fail_put_dmi_dev;
 
 	return 0;
 
-fail_free_dmi_dev:
-	kfree(dmi_dev);
-fail_class_unregister:
+fail_put_dmi_dev:
+	put_device(dmi_dev);
 
+fail_class_unregister:
 	class_unregister(&dmi_class);
 
 	return ret;
-- 
2.7.4

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

* [PATCH 2/2] dmi-id: add dmi/id/oem group for exporting oem strings to sysfs
  2016-07-15  9:42 [PATCH 0/2] dmi-id: export oem strings to sysfs Allen Hung
  2016-07-15  9:42 ` [PATCH 1/2] dmi-id: don't free dev structure after calling device_register Allen Hung
@ 2016-07-15  9:42 ` Allen Hung
  1 sibling, 0 replies; 5+ messages in thread
From: Allen Hung @ 2016-07-15  9:42 UTC (permalink / raw)
  To: Jean Delvare, linux-kernel; +Cc: Mario Limonciello, Allen Hung

The oem strings in DMI system identification information of the BIOS have
been parsed and stored as dmi devices in dmi_scan.c but they are not
exported to userspace via sysfs.

The patch intends to export oem strings to sysfs device /sys/class/dmi/id.
As the number of oem strings are dynamic, a group "oem" is added to the
device and the strings will be added to the group as string1, string2, ...,
and stringN.

Signed-off-by: Allen Hung <allen_hung@dell.com>
---
 drivers/firmware/dmi-id.c | 108 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 108 insertions(+)

diff --git a/drivers/firmware/dmi-id.c b/drivers/firmware/dmi-id.c
index 44c0139..503b635 100644
--- a/drivers/firmware/dmi-id.c
+++ b/drivers/firmware/dmi-id.c
@@ -58,6 +58,44 @@ DEFINE_DMI_ATTR_WITH_SHOW(chassis_version,	0444, DMI_CHASSIS_VERSION);
 DEFINE_DMI_ATTR_WITH_SHOW(chassis_serial,	0400, DMI_CHASSIS_SERIAL);
 DEFINE_DMI_ATTR_WITH_SHOW(chassis_asset_tag,	0444, DMI_CHASSIS_ASSET_TAG);
 
+static struct attribute *dmi_oem_attrs[] = {
+	NULL,
+};
+
+static const char oem_group[] = "oem";
+
+static struct attribute_group dmi_oem_attr_group = {
+	.attrs = dmi_oem_attrs,
+	.name  = oem_group,
+};
+
+static LIST_HEAD(dmi_oem_attrs_list);
+
+struct dmi_oem_attribute {
+	struct device_attribute dev_attr;
+	const char *oem_string;
+	char buf[32];
+	bool is_added:1;
+	struct list_head list;
+};
+
+#define to_dmi_oem_attr(_dev_attr) \
+	container_of(_dev_attr, struct dmi_oem_attribute, dev_attr)
+
+static ssize_t sys_dmi_oem_show(struct device *dev,
+				  struct device_attribute *attr,
+				  char *page)
+{
+	struct dmi_oem_attribute *oa = to_dmi_oem_attr(attr);
+	ssize_t len;
+
+	strlcpy(page, oa->oem_string, PAGE_SIZE-1);
+	len = strlen(page);
+	page[len++] = '\n';
+	page[len] = 0;
+	return len;
+}
+
 static void ascii_filter(char *d, const char *s)
 {
 	/* Filter out characters we don't want to see in the modalias string */
@@ -204,6 +242,69 @@ static void __init dmi_id_init_attr_table(void)
 	sys_dmi_attributes[i++] = &sys_dmi_modalias_attr.attr;
 }
 
+static int __init dmi_id_init_oem_attr_group(void)
+{
+	int i, ret;
+	const struct dmi_device *dev;
+	struct dmi_oem_attribute *oa, *tmp;
+	struct device_attribute dev_attr_tmpl =
+		__ATTR(, 0444, sys_dmi_oem_show, NULL);
+
+	ret = sysfs_create_group(&dmi_dev->kobj, &dmi_oem_attr_group);
+	if (ret)
+		return ret;
+
+	/* All devices with type=DMI_DEV_TYPE_OEM_STRING will be found in
+	 * the reverse order of what they were parsed in dmi_scan.c. However,
+	 * we do want to expose the OEM strings to sysfs in the same order as
+	 * what they were originally parsed. A linked list with 2-pass method
+	 * is used here to reverse the reserved order.
+	 *
+	 * Pass 1: find out all "OEM string" devices and add each "oem string"
+	 * to a linked list.
+	 */
+	dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, NULL);
+	while (dev)  {
+		oa = kzalloc(sizeof(*oa), GFP_KERNEL);
+		if (!oa) {
+			ret = -ENOMEM;
+			goto failed;
+		}
+		oa->dev_attr = dev_attr_tmpl;
+		oa->oem_string = dev->name;
+		list_add(&oa->list, &dmi_oem_attrs_list);
+		dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev);
+	}
+
+	/* Pass 2: traverse the list and add each string as a file to "oem"
+	 * group
+	 */
+	i = 0;
+	list_for_each_entry(oa, &dmi_oem_attrs_list, list) {
+		snprintf(oa->buf, sizeof(oa->buf), "string%d", ++i);
+		oa->dev_attr.attr.name = oa->buf;
+		ret = sysfs_add_file_to_group(
+			&dmi_dev->kobj, &oa->dev_attr.attr, oem_group);
+		if (ret)
+			goto failed;
+		oa->is_added = 1;
+	}
+
+	return 0;
+
+failed:
+	list_for_each_entry_safe(oa, tmp, &dmi_oem_attrs_list, list) {
+		if (oa->is_added)
+			sysfs_remove_file_from_group(
+				&dmi_dev->kobj,	&oa->dev_attr.attr, oem_group);
+		list_del(&oa->list);
+		kfree(oa);
+	}
+	sysfs_remove_group(&dmi_dev->kobj, &dmi_oem_attr_group);
+
+	return ret;
+}
+
 static int __init dmi_id_init(void)
 {
 	int ret;
@@ -231,8 +332,15 @@ static int __init dmi_id_init(void)
 	if (ret)
 		goto fail_put_dmi_dev;
 
+	ret = dmi_id_init_oem_attr_group();
+	if (ret)
+		goto fail_dev_unregister;
+
 	return 0;
 
+fail_dev_unregister:
+	device_unregister(dmi_dev);
+
 fail_put_dmi_dev:
 	put_device(dmi_dev);
 
-- 
2.7.4

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

* Re: [PATCH 1/2] dmi-id: don't free dev structure after calling device_register
  2016-07-14  8:01 ` [PATCH 1/2] dmi-id: don't free dev structure after calling device_register Allen Hung
@ 2016-07-18 17:09   ` Jean Delvare
  0 siblings, 0 replies; 5+ messages in thread
From: Jean Delvare @ 2016-07-18 17:09 UTC (permalink / raw)
  To: Allen Hung; +Cc: linux-kernel, Mario Limonciello

On Thu, 14 Jul 2016 16:01:22 +0800, Allen Hung wrote:
> dmi_dev is freed in error exit code but, according to the document
> of device_register, it should never directly free device structure
> after calling this function, even if it returned an error! Use
> put_device() instead.
> 
> Signed-off-by: Allen Hung <allen_hung@dell.com>
> ---
>  drivers/firmware/dmi-id.c | 8 ++++----
>  1 file changed, 4 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/firmware/dmi-id.c b/drivers/firmware/dmi-id.c
> index 94a58a0..44c0139 100644
> --- a/drivers/firmware/dmi-id.c
> +++ b/drivers/firmware/dmi-id.c
> @@ -229,14 +229,14 @@ static int __init dmi_id_init(void)
>  
>  	ret = device_register(dmi_dev);
>  	if (ret)
> -		goto fail_free_dmi_dev;
> +		goto fail_put_dmi_dev;
>  
>  	return 0;
>  
> -fail_free_dmi_dev:
> -	kfree(dmi_dev);
> -fail_class_unregister:
> +fail_put_dmi_dev:
> +	put_device(dmi_dev);
>  
> +fail_class_unregister:
>  	class_unregister(&dmi_class);
>  
>  	return ret;

Good catch. Applied, thanks.

-- 
Jean Delvare
SUSE L3 Support

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

* [PATCH 1/2] dmi-id: don't free dev structure after calling device_register
  2016-07-14  8:01 [PATCH 0/2] dmi-id: export " Allen Hung
@ 2016-07-14  8:01 ` Allen Hung
  2016-07-18 17:09   ` Jean Delvare
  0 siblings, 1 reply; 5+ messages in thread
From: Allen Hung @ 2016-07-14  8:01 UTC (permalink / raw)
  To: Jean Delvare, linux-kernel; +Cc: Mario Limonciello, Allen Hung

dmi_dev is freed in error exit code but, according to the document
of device_register, it should never directly free device structure
after calling this function, even if it returned an error! Use
put_device() instead.

Signed-off-by: Allen Hung <allen_hung@dell.com>
---
 drivers/firmware/dmi-id.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/drivers/firmware/dmi-id.c b/drivers/firmware/dmi-id.c
index 94a58a0..44c0139 100644
--- a/drivers/firmware/dmi-id.c
+++ b/drivers/firmware/dmi-id.c
@@ -229,14 +229,14 @@ static int __init dmi_id_init(void)
 
 	ret = device_register(dmi_dev);
 	if (ret)
-		goto fail_free_dmi_dev;
+		goto fail_put_dmi_dev;
 
 	return 0;
 
-fail_free_dmi_dev:
-	kfree(dmi_dev);
-fail_class_unregister:
+fail_put_dmi_dev:
+	put_device(dmi_dev);
 
+fail_class_unregister:
 	class_unregister(&dmi_class);
 
 	return ret;
-- 
2.7.4

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

end of thread, other threads:[~2016-07-18 17:09 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-07-15  9:42 [PATCH 0/2] dmi-id: export oem strings to sysfs Allen Hung
2016-07-15  9:42 ` [PATCH 1/2] dmi-id: don't free dev structure after calling device_register Allen Hung
2016-07-15  9:42 ` [PATCH 2/2] dmi-id: add dmi/id/oem group for exporting oem strings to sysfs Allen Hung
  -- strict thread matches above, loose matches on Subject: below --
2016-07-14  8:01 [PATCH 0/2] dmi-id: export " Allen Hung
2016-07-14  8:01 ` [PATCH 1/2] dmi-id: don't free dev structure after calling device_register Allen Hung
2016-07-18 17:09   ` Jean Delvare

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).