Skip to contentSkip to conversation

firmware: vpd: do not leave freed section attributes to the list

PATCH In review Dmitry Torokhov 4 participants 22 messages May 24, 2017 – May 25, 2017

lore
Share & tools
lore
#0 lore raw

PATCH 1/8 firmware: vpd: do not leave freed section attributes to the list

We should only add section attribute to the list of section attributes if we successfully created corresponding sysfs attribute.

Fixes 049a59db34eb ("firmware: Google VPD sysfs driver")
---
 drivers/firmware/google/vpd.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/firmware/google/vpd.c +1 -1
index 1e7860f02f4f..23a24a6d02c2 100644
@@ -136,12 +136,12 @@ static int vpd_section_attrib_add(const u8 *key, s32 key_len,
 	info->value = value;
 
 	INIT_LIST_HEAD(&info->list);
-	list_add_tail(&info->list, &sec->attribs);
 
 	ret = sysfs_create_bin_file(sec->kobj, &info->bin_attr);
 	if (ret)
 		goto free_info_key;
 
+	list_add_tail(&info->list, &sec->attribs);
 	return 0;
 
 free_info_key:
Signature · 1 line
2.13.0.219.gdb65acc882-goog
#1 lore raw

PATCH 2/8 firmware: vpd: use kdtrndup when copying section key

Instead of open-coding kstrndup with kzalloc + memcpy, let's use the helper.

---
 drivers/firmware/google/vpd.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/firmware/google/vpd.c +2 -3
index 23a24a6d02c2..acf3f3cdc3f8 100644
@@ -118,14 +118,13 @@ static int vpd_section_attrib_add(const u8 *key, s32 key_len,
 	info = kzalloc(sizeof(*info), GFP_KERNEL);
 	if (!info)
 		return -ENOMEM;
-	info->key = kzalloc(key_len + 1, GFP_KERNEL);
+
+	info->key = kstrndup(key, key_len, GFP_KERNEL);
 	if (!info->key) {
 		ret = -ENOMEM;
 		goto free_info;
 	}
 
-	memcpy(info->key, key, key_len);
-
 	sysfs_bin_attr_init(&info->bin_attr);
 	info->bin_attr.attr.name = info->key;
 	info->bin_attr.attr.mode = 0444;
Signature · 1 line
2.13.0.219.gdb65acc882-goog
Reviewed-by on v1 2/8
#2 lore raw

Re: [PATCH 2/8] firmware: vpd: use kdtrndup when copying section key

Quoted 4 lines · Dmitry Torokhov

Instead of open-coding kstrndup with kzalloc + memcpy, let's use the helper.

Quoted 28 lines
---
 drivers/firmware/google/vpd.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/firmware/google/vpd.c +2 -4
index 23a24a6d02c2..acf3f3cdc3f8 100644
@@ -118,14 +118,13 @@ static int vpd_section_attrib_add(const u8 *key, s32 key_len,
        info = kzalloc(sizeof(*info), GFP_KERNEL);
        if (!info)
                return -ENOMEM;
-       info->key = kzalloc(key_len + 1, GFP_KERNEL);
+
+       info->key = kstrndup(key, key_len, GFP_KERNEL);
        if (!info->key) {
                ret = -ENOMEM;
                goto free_info;
        }
 
-       memcpy(info->key, key, key_len);
-
        sysfs_bin_attr_init(&info->bin_attr);
        info->bin_attr.attr.name = info->key;
        info->bin_attr.attr.mode = 0444;
--

2.13.0.219.gdb65acc882-goog

#3 lore raw

PATCH 3/8 firmware: vpd: avoid potential use-after-free when destroying section

In reply to Dmitry Torokhov

We should not free info->key before we remove sysfs attribute that uses this data as its name.

Fixes 049a59db34eb ("firmware: Google VPD sysfs driver")
---
 drivers/firmware/google/vpd.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/firmware/google/vpd.c +1 -1
index acf3f3cdc3f8..b3a6e918418b 100644
@@ -157,8 +157,8 @@ static void vpd_section_attrib_destroy(struct vpd_section *sec)
 	struct vpd_attrib_info *temp;
 
 	list_for_each_entry_safe(info, temp, &sec->attribs, list) {
-		kfree(info->key);
 		sysfs_remove_bin_file(sec->kobj, &info->bin_attr);
+		kfree(info->key);
 		kfree(info);
 	}
 }
Signature · 1 line
2.13.0.219.gdb65acc882-goog
Reviewed-by on v1 3/8
#4 lore raw

Re: [PATCH 3/8] firmware: vpd: avoid potential use-after-free when destroying section

Quoted 5 lines · Dmitry Torokhov

We should not free info->key before we remove sysfs attribute that uses this data as its name.

Fixes 049a59db34eb ("firmware: Google VPD sysfs driver")
Quoted 21 lines
---
 drivers/firmware/google/vpd.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/firmware/google/vpd.c +1 -2
index acf3f3cdc3f8..b3a6e918418b 100644
@@ -157,8 +157,8 @@ static void vpd_section_attrib_destroy(struct vpd_section *sec)
        struct vpd_attrib_info *temp;
 
        list_for_each_entry_safe(info, temp, &sec->attribs, list) {
-               kfree(info->key);
                sysfs_remove_bin_file(sec->kobj, &info->bin_attr);
+               kfree(info->key);
                kfree(info);
        }
 }
--

2.13.0.219.gdb65acc882-goog

#5 lore raw

PATCH 4/8 firmware: vpd: do not leak kobjects

In reply to Dmitry Torokhov

kobject_del() only unlinks kobject, we need to use kobject_put() to make sure kobject will go away completely.

Fixes 049a59db34eb ("firmware: Google VPD sysfs driver")
---
 drivers/firmware/google/vpd.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/firmware/google/vpd.c +2 -2
index b3a6e918418b..cb07a2984ece 100644
@@ -243,7 +243,7 @@ static int vpd_section_destroy(struct vpd_section *sec)
 {
 	if (sec->enabled) {
 		vpd_section_attrib_destroy(sec);
-		kobject_del(sec->kobj);
+		kobject_put(sec->kobj);
 		sysfs_remove_bin_file(vpd_kobj, &sec->bin_attr);
 		kfree(sec->raw_name);
 		iounmap(sec->baseaddr);
@@ -330,7 +330,7 @@ static void __exit vpd_platform_exit(void)
 {
 	vpd_section_destroy(&ro_vpd);
 	vpd_section_destroy(&rw_vpd);
-	kobject_del(vpd_kobj);
+	kobject_put(vpd_kobj);
 }
 
 module_init(vpd_platform_init);
Signature · 1 line
2.13.0.219.gdb65acc882-goog
Reviewed-by on v1 4/8
#6 lore raw

Re: [PATCH 4/8] firmware: vpd: do not leak kobjects

Quoted 5 lines · Dmitry Torokhov

kobject_del() only unlinks kobject, we need to use kobject_put() to make sure kobject will go away completely.

Fixes 049a59db34eb ("firmware: Google VPD sysfs driver")
Quoted 29 lines
---
 drivers/firmware/google/vpd.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/firmware/google/vpd.c +2 -3
index b3a6e918418b..cb07a2984ece 100644
@@ -243,7 +243,7 @@ static int vpd_section_destroy(struct vpd_section *sec)
 {
        if (sec->enabled) {
                vpd_section_attrib_destroy(sec);
-               kobject_del(sec->kobj);
+               kobject_put(sec->kobj);
                sysfs_remove_bin_file(vpd_kobj, &sec->bin_attr);
                kfree(sec->raw_name);
                iounmap(sec->baseaddr);
@@ -330,7 +330,7 @@ static void __exit vpd_platform_exit(void)
 {
        vpd_section_destroy(&ro_vpd);
        vpd_section_destroy(&rw_vpd);
-       kobject_del(vpd_kobj);
+       kobject_put(vpd_kobj);
 }
 
 module_init(vpd_platform_init);
--

2.13.0.219.gdb65acc882-goog

#7 lore raw

PATCH 5/8 firmware: vpd: use kasprintf() when forming name of 'raw' attribute

In reply to Dmitry Torokhov

When creating name for the "raw" attribute, let's switch to using kaspeintf() instead of doing it by hand. Also make sure we handle errors.

---
 drivers/firmware/google/vpd.c | 31 +++++++++++++++----------------
 1 file changed, 15 insertions(+), 16 deletions(-)
drivers/firmware/google/vpd.c +15 -16
index cb07a2984ece..3463b8037ed8 100644
@@ -190,8 +190,7 @@ static int vpd_section_create_attribs(struct vpd_section *sec)
 static int vpd_section_init(const char *name, struct vpd_section *sec,
 			    phys_addr_t physaddr, size_t size)
 {
-	int ret;
-	int raw_len;
+	int err;
 
 	sec->baseaddr = memremap(physaddr, size, MEMREMAP_WB);
 	if (!sec->baseaddr)
@@ -200,10 +199,11 @@ static int vpd_section_init(const char *name, struct vpd_section *sec,
 	sec->name = name;
 
 	/* We want to export the raw partion with name ${name}_raw */
-	raw_len = strlen(name) + 5;
-	sec->raw_name = kzalloc(raw_len, GFP_KERNEL);
-	strncpy(sec->raw_name, name, raw_len);
-	strncat(sec->raw_name, "_raw", raw_len);
+	sec->raw_name = kasprintf(GFP_KERNEL, "%s_raw", name);
+	if (!sec->raw_name) {
+		err = -ENOMEM;
+		goto err_iounmap;
+	}
 
 	sysfs_bin_attr_init(&sec->bin_attr);
 	sec->bin_attr.attr.name = sec->raw_name;
@@ -212,14 +212,14 @@ static int vpd_section_init(const char *name, struct vpd_section *sec,
 	sec->bin_attr.read = vpd_section_read;
 	sec->bin_attr.private = sec;
 
-	ret = sysfs_create_bin_file(vpd_kobj, &sec->bin_attr);
-	if (ret)
-		goto free_sec;
+	err = sysfs_create_bin_file(vpd_kobj, &sec->bin_attr);
+	if (err)
+		goto err_free_raw_name;
 
 	sec->kobj = kobject_create_and_add(name, vpd_kobj);
 	if (!sec->kobj) {
-		ret = -EINVAL;
-		goto sysfs_remove;
+		err = -EINVAL;
+		goto err_sysfs_remove;
 	}
 
 	INIT_LIST_HEAD(&sec->attribs);
@@ -229,14 +229,13 @@ static int vpd_section_init(const char *name, struct vpd_section *sec,
 
 	return 0;
 
-sysfs_remove:
+err_sysfs_remove:
 	sysfs_remove_bin_file(vpd_kobj, &sec->bin_attr);
-
-free_sec:
+err_free_raw_name:
 	kfree(sec->raw_name);
+err_iounmap:
 	iounmap(sec->baseaddr);
-
-	return ret;
+	return err;
 }
 
 static int vpd_section_destroy(struct vpd_section *sec)
Signature · 1 line
2.13.0.219.gdb65acc882-goog
Reviewed-by on v1 5/8
#8 lore raw

Re: [PATCH 5/8] firmware: vpd: use kasprintf() when forming name of 'raw' attribute

Quoted 5 lines · Dmitry Torokhov

When creating name for the "raw" attribute, let's switch to using kaspeintf() instead of doing it by hand. Also make sure we handle errors.

Quoted 76 lines
---
 drivers/firmware/google/vpd.c | 31 +++++++++++++++----------------
 1 file changed, 15 insertions(+), 16 deletions(-)
drivers/firmware/google/vpd.c +15 -17
index cb07a2984ece..3463b8037ed8 100644
@@ -190,8 +190,7 @@ static int vpd_section_create_attribs(struct vpd_section *sec)
 static int vpd_section_init(const char *name, struct vpd_section *sec,
                            phys_addr_t physaddr, size_t size)
 {
-       int ret;
-       int raw_len;
+       int err;
 
        sec->baseaddr = memremap(physaddr, size, MEMREMAP_WB);
        if (!sec->baseaddr)
@@ -200,10 +199,11 @@ static int vpd_section_init(const char *name, struct vpd_section *sec,
        sec->name = name;
 
        /* We want to export the raw partion with name ${name}_raw */
-       raw_len = strlen(name) + 5;
-       sec->raw_name = kzalloc(raw_len, GFP_KERNEL);
-       strncpy(sec->raw_name, name, raw_len);
-       strncat(sec->raw_name, "_raw", raw_len);
+       sec->raw_name = kasprintf(GFP_KERNEL, "%s_raw", name);
+       if (!sec->raw_name) {
+               err = -ENOMEM;
+               goto err_iounmap;
+       }
 
        sysfs_bin_attr_init(&sec->bin_attr);
        sec->bin_attr.attr.name = sec->raw_name;
@@ -212,14 +212,14 @@ static int vpd_section_init(const char *name, struct vpd_section *sec,
        sec->bin_attr.read = vpd_section_read;
        sec->bin_attr.private = sec;
 
-       ret = sysfs_create_bin_file(vpd_kobj, &sec->bin_attr);
-       if (ret)
-               goto free_sec;
+       err = sysfs_create_bin_file(vpd_kobj, &sec->bin_attr);
+       if (err)
+               goto err_free_raw_name;
 
        sec->kobj = kobject_create_and_add(name, vpd_kobj);
        if (!sec->kobj) {
-               ret = -EINVAL;
-               goto sysfs_remove;
+               err = -EINVAL;
+               goto err_sysfs_remove;
        }
 
        INIT_LIST_HEAD(&sec->attribs);
@@ -229,14 +229,13 @@ static int vpd_section_init(const char *name, struct vpd_section *sec,
 
        return 0;
 
-sysfs_remove:
+err_sysfs_remove:
        sysfs_remove_bin_file(vpd_kobj, &sec->bin_attr);
-
-free_sec:
+err_free_raw_name:
        kfree(sec->raw_name);
+err_iounmap:
        iounmap(sec->baseaddr);
-
-       return ret;
+       return err;
 }
 
 static int vpd_section_destroy(struct vpd_section *sec)
--

2.13.0.219.gdb65acc882-goog

#9 lore raw

PATCH 6/8 firmware: vpd: do not clear statically allocated data

In reply to Dmitry Torokhov

ro_vpd and rw_vpd are static module-scope variables that are guaranteed to be initialized with zeroes, there is no need for explicit memset().

---
 drivers/firmware/google/vpd.c | 3 ---
 1 file changed, 3 deletions(-)
drivers/firmware/google/vpd.c +0 -3
index 3463b8037ed8..78945729388e 100644
@@ -317,9 +317,6 @@ static int __init vpd_platform_init(void)
 	if (!vpd_kobj)
 		return -ENOMEM;
 
-	memset(&ro_vpd, 0, sizeof(ro_vpd));
-	memset(&rw_vpd, 0, sizeof(rw_vpd));
-
 	platform_driver_register(&vpd_driver);
 
 	return 0;
Signature · 1 line
2.13.0.219.gdb65acc882-goog
Reviewed-by on v1 6/8
#10 lore raw

Re: [PATCH 6/8] firmware: vpd: do not clear statically allocated data

Quoted 4 lines · Dmitry Torokhov

ro_vpd and rw_vpd are static module-scope variables that are guaranteed to be initialized with zeroes, there is no need for explicit memset().

Quoted 21 lines
---
 drivers/firmware/google/vpd.c | 3 ---
 1 file changed, 3 deletions(-)
drivers/firmware/google/vpd.c +0 -4
index 3463b8037ed8..78945729388e 100644
@@ -317,9 +317,6 @@ static int __init vpd_platform_init(void)
        if (!vpd_kobj)
                return -ENOMEM;
 
-       memset(&ro_vpd, 0, sizeof(ro_vpd));
-       memset(&rw_vpd, 0, sizeof(rw_vpd));
-
        platform_driver_register(&vpd_driver);
 
        return 0;
--

2.13.0.219.gdb65acc882-goog

#11 lore raw

PATCH 7/8 firmware: vpd: remove platform driver

In reply to Dmitry Torokhov

There is no reason why VPD should register platform device and driver, given that we do not use their respective kobjects to attach attributes, nor do we need suspend/resume hooks, or any other features of device core.

---
 drivers/firmware/google/vpd.c | 44 ++++++++++++++++---------------------------
 1 file changed, 16 insertions(+), 28 deletions(-)
drivers/firmware/google/vpd.c +16 -28
index 78945729388e..f4766bf6785a 100644
@@ -22,8 +22,6 @@
 #include <linux/kobject.h>
 #include <linux/list.h>
 #include <linux/module.h>
-#include <linux/of_address.h>
-#include <linux/platform_device.h>
 #include <linux/slab.h>
 #include <linux/sysfs.h>
 
@@ -279,47 +277,37 @@ static int vpd_sections_init(phys_addr_t physaddr)
 		ret = vpd_section_init("rw", &rw_vpd,
 				       physaddr + sizeof(struct vpd_cbmem) +
 				       header.ro_size, header.rw_size);
-		if (ret)
+		if (ret) {
+			vpd_section_destroy(&ro_vpd);
 			return ret;
+		}
 	}
 
 	return 0;
 }
 
-static int vpd_probe(struct platform_device *pdev)
-{
-	int ret;
-	struct lb_cbmem_ref entry;
-
-	ret = coreboot_table_find(CB_TAG_VPD, &entry, sizeof(entry));
-	if (ret)
-		return ret;
-
-	return vpd_sections_init(entry.cbmem_addr);
-}
-
-static struct platform_driver vpd_driver = {
-	.probe = vpd_probe,
-	.driver = {
-		.name = "vpd",
-	},
-};
-
 static int __init vpd_platform_init(void)
 {
-	struct platform_device *pdev;
-
-	pdev = platform_device_register_simple("vpd", -1, NULL, 0);
-	if (IS_ERR(pdev))
-		return PTR_ERR(pdev);
+	struct lb_cbmem_ref entry;
+	int err;
 
 	vpd_kobj = kobject_create_and_add("vpd", firmware_kobj);
 	if (!vpd_kobj)
 		return -ENOMEM;
 
-	platform_driver_register(&vpd_driver);
+	err = coreboot_table_find(CB_TAG_VPD, &entry, sizeof(entry));
+	if (err)
+		goto err_kobject_put;
+
+	err = vpd_sections_init(entry.cbmem_addr);
+	if (err)
+		goto err_kobject_put;
 
 	return 0;
+
+err_kobject_put:
+	kobject_put(vpd_kobj);
+	return err;
 }
 
 static void __exit vpd_platform_exit(void)
Signature · 1 line
2.13.0.219.gdb65acc882-goog
Reviewed-by on v1 7/8
#12 lore raw

Re: [PATCH 7/8] firmware: vpd: remove platform driver

Quoted 6 lines · Dmitry Torokhov

There is no reason why VPD should register platform device and driver, given that we do not use their respective kobjects to attach attributes, nor do we need suspend/resume hooks, or any other features of device core.

One reason might be to use devm_ functions for memory allocations to simplify error handling and cleanup. Without that, I agree.

Quoted 84 lines
---
 drivers/firmware/google/vpd.c | 44 ++++++++++++++++---------------------------
 1 file changed, 16 insertions(+), 28 deletions(-)
drivers/firmware/google/vpd.c +16 -29
index 78945729388e..f4766bf6785a 100644
@@ -22,8 +22,6 @@
 #include <linux/kobject.h>
 #include <linux/list.h>
 #include <linux/module.h>
-#include <linux/of_address.h>
-#include <linux/platform_device.h>
 #include <linux/slab.h>
 #include <linux/sysfs.h>
 
@@ -279,47 +277,37 @@ static int vpd_sections_init(phys_addr_t physaddr)
                ret = vpd_section_init("rw", &rw_vpd,
                                       physaddr + sizeof(struct vpd_cbmem) +
                                       header.ro_size, header.rw_size);
-               if (ret)
+               if (ret) {
+                       vpd_section_destroy(&ro_vpd);
                        return ret;
+               }
        }
 
        return 0;
 }
 
-static int vpd_probe(struct platform_device *pdev)
-{
-       int ret;
-       struct lb_cbmem_ref entry;
-
-       ret = coreboot_table_find(CB_TAG_VPD, &entry, sizeof(entry));
-       if (ret)
-               return ret;
-
-       return vpd_sections_init(entry.cbmem_addr);
-}
-
-static struct platform_driver vpd_driver = {
-       .probe = vpd_probe,
-       .driver = {
-               .name = "vpd",
-       },
-};
-
 static int __init vpd_platform_init(void)
 {
-       struct platform_device *pdev;
-
-       pdev = platform_device_register_simple("vpd", -1, NULL, 0);
-       if (IS_ERR(pdev))
-               return PTR_ERR(pdev);
+       struct lb_cbmem_ref entry;
+       int err;
 
        vpd_kobj = kobject_create_and_add("vpd", firmware_kobj);
        if (!vpd_kobj)
                return -ENOMEM;
 
-       platform_driver_register(&vpd_driver);
+       err = coreboot_table_find(CB_TAG_VPD, &entry, sizeof(entry));
+       if (err)
+               goto err_kobject_put;
+
+       err = vpd_sections_init(entry.cbmem_addr);
+       if (err)
+               goto err_kobject_put;
 
        return 0;
+
+err_kobject_put:
+       kobject_put(vpd_kobj);
+       return err;
 }
 
 static void __exit vpd_platform_exit(void)
--

2.13.0.219.gdb65acc882-goog

#13 lore raw

Re: [PATCH 7/8] firmware: vpd: remove platform driver

I'm not a kernel expert so maybe I don't understand this right, but... I think this might have been done this way to ensure that the driver can get initialized correctly regardless of probe ordering. coreboot_table_find() may fail with -EPROBE_DEFER if the coreboot_table driver and its dependent (coreboot_table-acpi or coreboot_table-of) haven't been probed yet. In that case we want the VPD driver to wait and try again later, after they've been probed. I believe with the way this used to be written the driver core did that automatically for us, but with your change I'm not sure if it still does.

Quoted 98 lines · Guenter Roeck
Quoted 6 lines · Dmitry Torokhov

There is no reason why VPD should register platform device and driver, given that we do not use their respective kobjects to attach attributes, nor do we need suspend/resume hooks, or any other features of device core.

One reason might be to use devm_ functions for memory allocations to simplify error handling and cleanup. Without that, I agree.

Quoted 84 lines
---
 drivers/firmware/google/vpd.c | 44 ++++++++++++++++---------------------------
 1 file changed, 16 insertions(+), 28 deletions(-)
drivers/firmware/google/vpd.c +16 -29
index 78945729388e..f4766bf6785a 100644
@@ -22,8 +22,6 @@
 #include <linux/kobject.h>
 #include <linux/list.h>
 #include <linux/module.h>
-#include <linux/of_address.h>
-#include <linux/platform_device.h>
 #include <linux/slab.h>
 #include <linux/sysfs.h>
 
@@ -279,47 +277,37 @@ static int vpd_sections_init(phys_addr_t physaddr)
                ret = vpd_section_init("rw", &rw_vpd,
                                       physaddr + sizeof(struct vpd_cbmem) +
                                       header.ro_size, header.rw_size);
-               if (ret)
+               if (ret) {
+                       vpd_section_destroy(&ro_vpd);
                        return ret;
+               }
        }
 
        return 0;
 }
 
-static int vpd_probe(struct platform_device *pdev)
-{
-       int ret;
-       struct lb_cbmem_ref entry;
-
-       ret = coreboot_table_find(CB_TAG_VPD, &entry, sizeof(entry));
-       if (ret)
-               return ret;
-
-       return vpd_sections_init(entry.cbmem_addr);
-}
-
-static struct platform_driver vpd_driver = {
-       .probe = vpd_probe,
-       .driver = {
-               .name = "vpd",
-       },
-};
-
 static int __init vpd_platform_init(void)
 {
-       struct platform_device *pdev;
-
-       pdev = platform_device_register_simple("vpd", -1, NULL, 0);
-       if (IS_ERR(pdev))
-               return PTR_ERR(pdev);
+       struct lb_cbmem_ref entry;
+       int err;
 
        vpd_kobj = kobject_create_and_add("vpd", firmware_kobj);
        if (!vpd_kobj)
                return -ENOMEM;
 
-       platform_driver_register(&vpd_driver);
+       err = coreboot_table_find(CB_TAG_VPD, &entry, sizeof(entry));
+       if (err)
+               goto err_kobject_put;
+
+       err = vpd_sections_init(entry.cbmem_addr);
+       if (err)
+               goto err_kobject_put;
 
        return 0;
+
+err_kobject_put:
+       kobject_put(vpd_kobj);
+       return err;
 }
 
 static void __exit vpd_platform_exit(void)
--

2.13.0.219.gdb65acc882-goog

#14 lore raw

Re: [PATCH 7/8] firmware: vpd: remove platform driver

Quoted 10 lines · Julius Werner

I'm not a kernel expert so maybe I don't understand this right, but... I think this might have been done this way to ensure that the driver can get initialized correctly regardless of probe ordering. coreboot_table_find() may fail with -EPROBE_DEFER if the coreboot_table driver and its dependent (coreboot_table-acpi or coreboot_table-of) haven't been probed yet. In that case we want the VPD driver to wait and try again later, after they've been probed. I believe with the way this used to be written the driver core did that automatically for us, but with your change I'm not sure if it still does.

Hmm, I missed that. OK, I think we have a maze of platform drivers there, I'll see what can be done.

Quoted 100 lines · Guenter Roeck
Quoted 98 lines
Quoted 6 lines · Dmitry Torokhov

There is no reason why VPD should register platform device and driver, given that we do not use their respective kobjects to attach attributes, nor do we need suspend/resume hooks, or any other features of device core.

One reason might be to use devm_ functions for memory allocations to simplify error handling and cleanup. Without that, I agree.

Quoted 84 lines
---
 drivers/firmware/google/vpd.c | 44 ++++++++++++++++---------------------------
 1 file changed, 16 insertions(+), 28 deletions(-)
drivers/firmware/google/vpd.c +16 -29
index 78945729388e..f4766bf6785a 100644
@@ -22,8 +22,6 @@
 #include <linux/kobject.h>
 #include <linux/list.h>
 #include <linux/module.h>
-#include <linux/of_address.h>
-#include <linux/platform_device.h>
 #include <linux/slab.h>
 #include <linux/sysfs.h>
 
@@ -279,47 +277,37 @@ static int vpd_sections_init(phys_addr_t physaddr)
                ret = vpd_section_init("rw", &rw_vpd,
                                       physaddr + sizeof(struct vpd_cbmem) +
                                       header.ro_size, header.rw_size);
-               if (ret)
+               if (ret) {
+                       vpd_section_destroy(&ro_vpd);
                        return ret;
+               }
        }
 
        return 0;
 }
 
-static int vpd_probe(struct platform_device *pdev)
-{
-       int ret;
-       struct lb_cbmem_ref entry;
-
-       ret = coreboot_table_find(CB_TAG_VPD, &entry, sizeof(entry));
-       if (ret)
-               return ret;
-
-       return vpd_sections_init(entry.cbmem_addr);
-}
-
-static struct platform_driver vpd_driver = {
-       .probe = vpd_probe,
-       .driver = {
-               .name = "vpd",
-       },
-};
-
 static int __init vpd_platform_init(void)
 {
-       struct platform_device *pdev;
-
-       pdev = platform_device_register_simple("vpd", -1, NULL, 0);
-       if (IS_ERR(pdev))
-               return PTR_ERR(pdev);
+       struct lb_cbmem_ref entry;
+       int err;
 
        vpd_kobj = kobject_create_and_add("vpd", firmware_kobj);
        if (!vpd_kobj)
                return -ENOMEM;
 
-       platform_driver_register(&vpd_driver);
+       err = coreboot_table_find(CB_TAG_VPD, &entry, sizeof(entry));
+       if (err)
+               goto err_kobject_put;
+
+       err = vpd_sections_init(entry.cbmem_addr);
+       if (err)
+               goto err_kobject_put;
 
        return 0;
+
+err_kobject_put:
+       kobject_put(vpd_kobj);
+       return err;
 }
 
 static void __exit vpd_platform_exit(void)
--

2.13.0.219.gdb65acc882-goog

Signature · 1 line
Dmitry
#15 lore raw

PATCH 8/8 firmware: vpd: fix confusion between memremap and iounmap

In reply to Dmitry Torokhov

We should not be using iounmap to unmap memory mapped with memremap.

This fixes following warnings generated by sparse in response to incorrect type annotations:

CHECK drivers/firmware/google/vpd.c
drivers/firmware/google/vpd.c:235:20: warning: incorrect type in argument 1 (different address spaces)
drivers/firmware/google/vpd.c:235:20: expected void volatile [noderef] <asn:2>*addr
drivers/firmware/google/vpd.c:235:20: got char *baseaddr
drivers/firmware/google/vpd.c:246:28: warning: incorrect type in argument 1 (different address spaces)
drivers/firmware/google/vpd.c:246:28: expected void volatile [noderef] <asn:2>*addr
drivers/firmware/google/vpd.c:246:28: got char *baseaddr
drivers/firmware/google/vpd.c:258:14: warning: incorrect type in assignment (different address spaces)
drivers/firmware/google/vpd.c:258:14: expected struct vpd_cbmem [noderef] <asn:2>*temp
drivers/firmware/google/vpd.c:258:14: got void *

Fixes 049a59db34eb ("firmware: Google VPD sysfs driver")
---
 drivers/firmware/google/vpd.c | 39 ++++++++++++++++++++-------------------
 1 file changed, 20 insertions(+), 19 deletions(-)
drivers/firmware/google/vpd.c +20 -19
index f4766bf6785a..b076210e38a2 100644
@@ -200,7 +200,7 @@ static int vpd_section_init(const char *name, struct vpd_section *sec,
 	sec->raw_name = kasprintf(GFP_KERNEL, "%s_raw", name);
 	if (!sec->raw_name) {
 		err = -ENOMEM;
-		goto err_iounmap;
+		goto err_unmap;
 	}
 
 	sysfs_bin_attr_init(&sec->bin_attr);
@@ -231,8 +231,8 @@ static int vpd_section_init(const char *name, struct vpd_section *sec,
 	sysfs_remove_bin_file(vpd_kobj, &sec->bin_attr);
 err_free_raw_name:
 	kfree(sec->raw_name);
-err_iounmap:
-	iounmap(sec->baseaddr);
+err_unmap:
+	memunmap(sec->baseaddr);
 	return err;
 }
 
@@ -243,7 +243,7 @@ static int vpd_section_destroy(struct vpd_section *sec)
 		kobject_put(sec->kobj);
 		sysfs_remove_bin_file(vpd_kobj, &sec->bin_attr);
 		kfree(sec->raw_name);
-		iounmap(sec->baseaddr);
+		memunmap(sec->baseaddr);
 	}
 
 	return 0;
@@ -251,38 +251,39 @@ static int vpd_section_destroy(struct vpd_section *sec)
 
 static int vpd_sections_init(phys_addr_t physaddr)
 {
-	struct vpd_cbmem __iomem *temp;
-	struct vpd_cbmem header;
+	struct vpd_cbmem *header;
 	int ret = 0;
 
-	temp = memremap(physaddr, sizeof(struct vpd_cbmem), MEMREMAP_WB);
-	if (!temp)
+	header = memremap(physaddr, sizeof(struct vpd_cbmem), MEMREMAP_WB);
+	if (!header)
 		return -ENOMEM;
 
-	memcpy_fromio(&header, temp, sizeof(struct vpd_cbmem));
-	iounmap(temp);
-
-	if (header.magic != VPD_CBMEM_MAGIC)
-		return -ENODEV;
+	if (header->magic != VPD_CBMEM_MAGIC) {
+		ret = -ENODEV;
+		goto out;
+	}
 
-	if (header.ro_size) {
+	if (header->ro_size) {
 		ret = vpd_section_init("ro", &ro_vpd,
 				       physaddr + sizeof(struct vpd_cbmem),
-				       header.ro_size);
+				       header->ro_size);
 		if (ret)
-			return ret;
+			goto out;
 	}
 
-	if (header.rw_size) {
+	if (header->rw_size) {
 		ret = vpd_section_init("rw", &rw_vpd,
 				       physaddr + sizeof(struct vpd_cbmem) +
-				       header.ro_size, header.rw_size);
+						header->ro_size,
+				       header->rw_size);
 		if (ret) {
 			vpd_section_destroy(&ro_vpd);
-			return ret;
+			goto out;
 		}
 	}
 
+out:
+	memunmap(header);
 	return 0;
 }
 
Signature · 1 line
2.13.0.219.gdb65acc882-goog
Reviewed-by on v1 8/8
#16 lore raw

Re: [PATCH 8/8] firmware: vpd: fix confusion between memremap and iounmap

Quoted 16 lines · Dmitry Torokhov

We should not be using iounmap to unmap memory mapped with memremap.

This fixes following warnings generated by sparse in response to incorrect type annotations:

CHECK drivers/firmware/google/vpd.c
drivers/firmware/google/vpd.c:235:20: warning: incorrect type in argument 1 (different address spaces)
drivers/firmware/google/vpd.c:235:20: expected void volatile [noderef] <asn:2>*addr
drivers/firmware/google/vpd.c:235:20: got char *baseaddr
drivers/firmware/google/vpd.c:246:28: warning: incorrect type in argument 1 (different address spaces)
drivers/firmware/google/vpd.c:246:28: expected void volatile [noderef] <asn:2>*addr
drivers/firmware/google/vpd.c:246:28: got char *baseaddr
drivers/firmware/google/vpd.c:258:14: warning: incorrect type in assignment (different address spaces)
drivers/firmware/google/vpd.c:258:14: expected struct vpd_cbmem [noderef] <asn:2>*temp
drivers/firmware/google/vpd.c:258:14: got void *

Quoted 2 lines
Fixes 049a59db34eb ("firmware: Google VPD sysfs driver")
Quoted 95 lines
---
 drivers/firmware/google/vpd.c | 39 ++++++++++++++++++++-------------------
 1 file changed, 20 insertions(+), 19 deletions(-)
drivers/firmware/google/vpd.c +20 -20
index f4766bf6785a..b076210e38a2 100644
@@ -200,7 +200,7 @@ static int vpd_section_init(const char *name, struct vpd_section *sec,
        sec->raw_name = kasprintf(GFP_KERNEL, "%s_raw", name);
        if (!sec->raw_name) {
                err = -ENOMEM;
-               goto err_iounmap;
+               goto err_unmap;
        }
 
        sysfs_bin_attr_init(&sec->bin_attr);
@@ -231,8 +231,8 @@ static int vpd_section_init(const char *name, struct vpd_section *sec,
        sysfs_remove_bin_file(vpd_kobj, &sec->bin_attr);
 err_free_raw_name:
        kfree(sec->raw_name);
-err_iounmap:
-       iounmap(sec->baseaddr);
+err_unmap:
+       memunmap(sec->baseaddr);
        return err;
 }
 
@@ -243,7 +243,7 @@ static int vpd_section_destroy(struct vpd_section *sec)
                kobject_put(sec->kobj);
                sysfs_remove_bin_file(vpd_kobj, &sec->bin_attr);
                kfree(sec->raw_name);
-               iounmap(sec->baseaddr);
+               memunmap(sec->baseaddr);
        }
 
        return 0;
@@ -251,38 +251,39 @@ static int vpd_section_destroy(struct vpd_section *sec)
 
 static int vpd_sections_init(phys_addr_t physaddr)
 {
-       struct vpd_cbmem __iomem *temp;
-       struct vpd_cbmem header;
+       struct vpd_cbmem *header;
        int ret = 0;
 
-       temp = memremap(physaddr, sizeof(struct vpd_cbmem), MEMREMAP_WB);
-       if (!temp)
+       header = memremap(physaddr, sizeof(struct vpd_cbmem), MEMREMAP_WB);
+       if (!header)
                return -ENOMEM;
 
-       memcpy_fromio(&header, temp, sizeof(struct vpd_cbmem));
-       iounmap(temp);
-
-       if (header.magic != VPD_CBMEM_MAGIC)
-               return -ENODEV;
+       if (header->magic != VPD_CBMEM_MAGIC) {
+               ret = -ENODEV;
+               goto out;
+       }
 
-       if (header.ro_size) {
+       if (header->ro_size) {
                ret = vpd_section_init("ro", &ro_vpd,
                                       physaddr + sizeof(struct vpd_cbmem),
-                                      header.ro_size);
+                                      header->ro_size);
                if (ret)
-                       return ret;
+                       goto out;
        }
 
-       if (header.rw_size) {
+       if (header->rw_size) {
                ret = vpd_section_init("rw", &rw_vpd,
                                       physaddr + sizeof(struct vpd_cbmem) +
-                                      header.ro_size, header.rw_size);
+                                               header->ro_size,
+                                      header->rw_size);
                if (ret) {
                        vpd_section_destroy(&ro_vpd);
-                       return ret;
+                       goto out;
                }
        }
 
+out:
+       memunmap(header);
        return 0;
 }
 
--

2.13.0.219.gdb65acc882-goog

#17 lore raw

Re: [PATCH 8/8] firmware: vpd: fix confusion between memremap and iounmap

In reply to Dmitry Torokhov
Quoted 16 lines · Dmitry Torokhov

We should not be using iounmap to unmap memory mapped with memremap.

This fixes following warnings generated by sparse in response to incorrect type annotations:

CHECK drivers/firmware/google/vpd.c
drivers/firmware/google/vpd.c:235:20: warning: incorrect type in argument 1 (different address spaces)
drivers/firmware/google/vpd.c:235:20: expected void volatile [noderef] <asn:2>*addr
drivers/firmware/google/vpd.c:235:20: got char *baseaddr
drivers/firmware/google/vpd.c:246:28: warning: incorrect type in argument 1 (different address spaces)
drivers/firmware/google/vpd.c:246:28: expected void volatile [noderef] <asn:2>*addr
drivers/firmware/google/vpd.c:246:28: got char *baseaddr
drivers/firmware/google/vpd.c:258:14: warning: incorrect type in assignment (different address spaces)
drivers/firmware/google/vpd.c:258:14: expected struct vpd_cbmem [noderef] <asn:2>*temp
drivers/firmware/google/vpd.c:258:14: got void *

Quoted 3 lines
Fixes 049a59db34eb ("firmware: Google VPD sysfs driver")

This patch doesn't seem to apply to my tree :(

Reviewed-by on v1 1/8
#18 lore raw
In reply to Dmitry Torokhov
Quoted 5 lines · Dmitry Torokhov

We should only add section attribute to the list of section attributes if we successfully created corresponding sysfs attribute.

Fixes 049a59db34eb ("firmware: Google VPD sysfs driver")
Quoted 25 lines
---
 drivers/firmware/google/vpd.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/firmware/google/vpd.c +1 -2
index 1e7860f02f4f..23a24a6d02c2 100644
@@ -136,12 +136,12 @@ static int vpd_section_attrib_add(const u8 *key, s32 key_len,
        info->value = value;
 
        INIT_LIST_HEAD(&info->list);
-       list_add_tail(&info->list, &sec->attribs);
 
        ret = sysfs_create_bin_file(sec->kobj, &info->bin_attr);
        if (ret)
                goto free_info_key;
 
+       list_add_tail(&info->list, &sec->attribs);
        return 0;
 
 free_info_key:
--

2.13.0.219.gdb65acc882-goog

#19 lore raw
In reply to Dmitry Torokhov
Quoted 9 lines · Dmitry Torokhov

We should only add section attribute to the list of section attributes if we successfully created corresponding sysfs attribute.

Fixes 049a59db34eb ("firmware: Google VPD sysfs driver")
---
 drivers/firmware/google/vpd.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Next time, can you split this up into 2 series, one for the current kernel, and the rest for the "next" release? I've tried to split them up myself here, hopefully it works...

thanks,

greg k-h

#20 lore raw
Quoted 11 lines · Greg Kroah-Hartman
Quoted 9 lines · Dmitry Torokhov

We should only add section attribute to the list of section attributes if we successfully created corresponding sysfs attribute.

Fixes 049a59db34eb ("firmware: Google VPD sysfs driver")
---
 drivers/firmware/google/vpd.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 3 lines

Next time, can you split this up into 2 series, one for the current kernel, and the rest for the "next" release? I've tried to split them up myself here, hopefully it works...

OK, I will. It is just I did not consider either of issues serious enough so they could not wait for next release: failure to allocate tiny amounts of memory is impossible to trigger with current kernels. Same goes for the other patches. For example, one needs to not only manage to get sysfs attribute creation to fail, but also then unload the driver, to trigger the issue. Unlikely to happen in real life.

Thanks.

Signature · 1 line
Dmitry
#21 lore raw
Quoted 16 lines · Dmitry Torokhov
Quoted 11 lines · Greg Kroah-Hartman
Quoted 9 lines · Dmitry Torokhov

We should only add section attribute to the list of section attributes if we successfully created corresponding sysfs attribute.

Fixes 049a59db34eb ("firmware: Google VPD sysfs driver")
---
 drivers/firmware/google/vpd.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 3 lines

Next time, can you split this up into 2 series, one for the current kernel, and the rest for the "next" release? I've tried to split them up myself here, hopefully it works...

Quoted 6 lines

OK, I will. It is just I did not consider either of issues serious enough so they could not wait for next release: failure to allocate tiny amounts of memory is impossible to trigger with current kernels. Same goes for the other patches. For example, one needs to not only manage to get sysfs attribute creation to fail, but also then unload the driver, to trigger the issue. Unlikely to happen in real life.

Ah, ok, that would have been good to know too :)