Skip to contentSkip to conversation

23222f8f8dce6: acpi, nfit: Add function to look up nvdimm device and provide SMBIOS handle

PATCH In review Jane Chu 3 participants 8 messages Jan 11, 2019

Subsystems (2)
lore
Share & tools
lore

Jane Chu

author
#0 lore raw

Hi, Tony,

I happen to be looking at the above patch, and noticed below change in drivers/acpi/nfit/core.c

+ mutex_lock(&acpi_desc_lock);
+ list_for_each_entry(acpi_desc, &acpi_descs, list) {
+ mutex_lock(&acpi_desc->init_mutex);
+ list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
+ memdev = __to_nfit_memdev(nfit_mem);
+ if (memdev->device_handle == device_handle) {

+                               mutex_unlock(&acpi_desc->init_mutex);
+                               mutex_unlock(&acpi_desc_lock);
+                               *flags = memdev->flags;   <-----
+                               return memdev->physical_id;

+ }
+ }
+ mutex_unlock(&acpi_desc->init_mutex);
+ }
+ mutex_unlock(&acpi_desc_lock);

Is there a reason to retrieve the memdev->flags value after releasing the locks?

Thanks!
-jane

_______________________________________________
Linux-nvdimm mailing list
[email protected]
https://lists.01.org/mailman/listinfo/linux-nvdimm

#1 lore raw

Jane,

It does look a bit fishy. But if there is an issue with “memdev” changing once we release the locks, then it will only help a little to move the “*flags = memdev->flags” back before we release the locks. Note that we access memdev one more time with the “return memdev->physical_id”

But I’m not at all sure what the possible races are here. I hope someone from the linx-nvdimm list can comment. I think I just cut & pasted the bulk of those nested loops from elsewhere. As far as I know we don’t currently have any support for hotplug NFIT devices, so I suspect there is currently no race here.

-Tony

From: Jane Chu [mailto:[email protected]]
Sent: Friday, January 11, 2019 1:43 PM
To: Luck, Tony <[email protected]>
Cc: linux-nvdimm <[email protected]>
Subject: 23222f8f8dce6: acpi, nfit: Add function to look up nvdimm device and provide SMBIOS handle

Hi, Tony,

I happen to be looking at the above patch, and noticed below change in

drivers/acpi/nfit/core.c

+ mutex_lock(&acpi_desc_lock);

+ list_for_each_entry(acpi_desc, &acpi_descs, list) {

+ mutex_lock(&acpi_desc->init_mutex);

+ list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {

+ memdev = __to_nfit_memdev(nfit_mem);

+ if (memdev->device_handle == device_handle) {

+ mutex_unlock(&acpi_desc->init_mutex);

+ mutex_unlock(&acpi_desc_lock);

+ *flags = memdev->flags; <-----

+ return memdev->physical_id;

+ }

+ }

+ mutex_unlock(&acpi_desc->init_mutex);

+ }

+ mutex_unlock(&acpi_desc_lock);

Is there a reason to retrieve the memdev->flags value after releasing

the locks?

Thanks!

-jane

_______________________________________________
Linux-nvdimm mailing list
[email protected]
https://lists.01.org/mailman/listinfo/linux-nvdimm

author
#2 lore raw
Quoted 8 lines · Tony

Jane,

It does look a bit fishy.  But if there is an issue with “memdev” changing once we release the locks,

then it will only help a little to move the “*flags = memdev->flags” back before we release the locks.

Note that we access memdev one more time with the “return memdev->physical_id”

Indeed, that line too.

Quoted 8 lines

But I’m not at all sure what the possible races are here. I hope someone from the linx-nvdimm list

can comment. I think I just cut & pasted the bulk of those nested loops from elsewhere. As far as I

know we don’t currently have any support for hotplug NFIT devices, so I suspect there is currently

no race here.

I don't understand the bulk of the code, just trying to follow what you said here. So if we *ever* decide to support hotplug NFIT device, what would remind
us to close the race here? Is there any harm to reference memdev while we
still have the locks?

Thanks!
-jane

Quoted 22 lines

-Tony

*From:*Jane Chu [mailto:[email protected]]
*Sent:* Friday, January 11, 2019 1:43 PM
*To:* Luck, Tony <[email protected]>
*Cc:* linux-nvdimm <[email protected]>
*Subject:* 23222f8f8dce6: acpi, nfit: Add function to look up nvdimm device and provide SMBIOS handle

Hi, Tony,
I happen to be looking at the above patch, and noticed below change in drivers/acpi/nfit/core.c
+       mutex_lock(&acpi_desc_lock);
+       list_for_each_entry(acpi_desc, &acpi_descs, list) { +               mutex_lock(&acpi_desc->init_mutex);
+               list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) { +                       memdev = __to_nfit_memdev(nfit_mem); +                       if (memdev->device_handle == device_handle) { +                               mutex_unlock(&acpi_desc->init_mutex); +                               mutex_unlock(&acpi_desc_lock); +                               *flags = memdev->flags;   <----- +                               return memdev->physical_id; +                       }

Quoted 8 lines

+               }
+               mutex_unlock(&acpi_desc->init_mutex);
+       }
+       mutex_unlock(&acpi_desc_lock);
Is there a reason to retrieve the memdev->flags value after releasing the locks?
Thanks!
-jane

_______________________________________________
Linux-nvdimm mailing list
[email protected]
https://lists.01.org/mailman/listinfo/linux-nvdimm

#3 lore raw
Quoted 25 lines · Jane Chu
Quoted 11 lines · Tony

Jane,

It does look a bit fishy. But if there is an issue with “memdev” changing once we release the locks,

then it will only help a little to move the “*flags = memdev->flags” back before we release the locks.

Note that we access memdev one more time with the “return memdev->physical_id”

Indeed, that line too.

Quoted 9 lines

But I’m not at all sure what the possible races are here. I hope someone from the linx-nvdimm list

can comment. I think I just cut & pasted the bulk of those nested loops from elsewhere. As far as I

know we don’t currently have any support for hotplug NFIT devices, so I suspect there is currently

Quoted 6 lines
Quoted 2 lines

no race here.

I don't understand the bulk of the code, just trying to follow what you said here. So if we *ever* decide to support hotplug NFIT device, what would remind
us to close the race here? Is there any harm to reference memdev while we
still have the locks?

No harm in fixing it up, I'd take that patch if you wrote it up.

There is potentially a race today if the kernel was calling this routine, but some other thread was doing:

echo "ACPI0012:00" > /sys/bus/acpi/drivers/nfit/unbind
_______________________________________________
Linux-nvdimm mailing list [email protected]
https://lists.01.org/mailman/listinfo/linux-nvdimm

#4 lore raw
Quoted 1 line · Dan Williams

No harm in fixing it up, I'd take that patch if you wrote it up.

Something like this?

drivers/acpi/nfit/core.c +4 -2
index 011d3db19c80..22945bf803c8 100644
@@ -721,6 +721,7 @@ int nfit_get_smbios_id(u32 device_handle, u16 *flags)
 	struct acpi_nfit_memory_map *memdev;
 	struct acpi_nfit_desc *acpi_desc;
 	struct nfit_mem *nfit_mem;
+	u16 physical_id;
 
 	mutex_lock(&acpi_desc_lock);
 	list_for_each_entry(acpi_desc, &acpi_descs, list) {
@@ -728,10 +729,11 @@ int nfit_get_smbios_id(u32 device_handle, u16 *flags)
 		list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
 			memdev = __to_nfit_memdev(nfit_mem);
 			if (memdev->device_handle == device_handle) {
+				*flags = memdev->flags;
+				physical_id = memdev->physical_id;
 				mutex_unlock(&acpi_desc->init_mutex);
 				mutex_unlock(&acpi_desc_lock);
-				*flags = memdev->flags;
-				return memdev->physical_id;
+				return physical_id;
 			}
 		}
 		mutex_unlock(&acpi_desc->init_mutex);

_______________________________________________
Linux-nvdimm mailing list
[email protected]
https://lists.01.org/mailman/listinfo/linux-nvdimm

#5 lore raw
Quoted 23 lines · Tony
Quoted 1 line · Dan Williams

No harm in fixing it up, I'd take that patch if you wrote it up.

Something like this?

drivers/acpi/nfit/core.c +1 -0
index 011d3db19c80..22945bf803c8 100644
@@ -721,6 +721,7 @@ int nfit_get_smbios_id(u32 device_handle, u16 *flags)
        struct acpi_nfit_memory_map *memdev;
        struct acpi_nfit_desc *acpi_desc;
        struct nfit_mem *nfit_mem;
+       u16 physical_id;
 
        mutex_lock(&acpi_desc_lock);
        list_for_each_entry(acpi_desc, &acpi_descs, list) {
@@ -728,10 +729,11 @@ int nfit_get_smbios_id(u32 device_handle, u16 *flags)
                list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
                        memdev = __to_nfit_memdev(nfit_mem);
                        if (memdev->device_handle == device_handle) {

drivers/acpi/nfit/core.c int nfit_get_smbios_id(u32 device_handle, u16 *flags) · line 739

Quoted 10 lines
drivers/acpi/nfit/core.c +3 -2
+                               *flags = memdev->flags;
+                               physical_id = memdev->physical_id;
                                mutex_unlock(&acpi_desc->init_mutex);
                                mutex_unlock(&acpi_desc_lock);
-                               *flags = memdev->flags;
-                               return memdev->physical_id;
+                               return physical_id;
                        }
                }
                mutex_unlock(&acpi_desc->init_mutex);

Yup, looks good.
_______________________________________________
Linux-nvdimm mailing list
[email protected]
https://lists.01.org/mailman/listinfo/linux-nvdimm

#6 lore raw

PATCH 1/1 acpi/nfit: Fix race accessing memdev in nfit_get_smbios_id()

Possible race accessing memdev structures after dropping the mutex. Dan Williams says this could race against another thread that is doing:

 # echo "ACPI0012:00" > /sys/bus/acpi/drivers/nfit/unbind

---

This time with a commit message and credit to Jane for finding it.

 drivers/acpi/nfit/core.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/acpi/nfit/core.c +4 -2
index 011d3db19c80..22945bf803c8 100644
@@ -721,6 +721,7 @@ int nfit_get_smbios_id(u32 device_handle, u16 *flags)
 	struct acpi_nfit_memory_map *memdev;
 	struct acpi_nfit_desc *acpi_desc;
 	struct nfit_mem *nfit_mem;
+	u16 physical_id;
 
 	mutex_lock(&acpi_desc_lock);
 	list_for_each_entry(acpi_desc, &acpi_descs, list) {
@@ -728,10 +729,11 @@ int nfit_get_smbios_id(u32 device_handle, u16 *flags)
 		list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
 			memdev = __to_nfit_memdev(nfit_mem);
 			if (memdev->device_handle == device_handle) {
+				*flags = memdev->flags;
+				physical_id = memdev->physical_id;
 				mutex_unlock(&acpi_desc->init_mutex);
 				mutex_unlock(&acpi_desc_lock);
-				*flags = memdev->flags;
-				return memdev->physical_id;
+				return physical_id;
 			}
 		}
 		mutex_unlock(&acpi_desc->init_mutex);
Signature · 6 lines
2.19.1

_______________________________________________
Linux-nvdimm mailing list
[email protected]
https://lists.01.org/mailman/listinfo/linux-nvdimm
#7 lore raw

Re: [PATCH] acpi/nfit: Fix race accessing memdev in nfit_get_smbios_id()

Quoted 6 lines · Tony

Possible race accessing memdev structures after dropping the mutex. Dan Williams says this could race against another thread that is doing:

Quoted 7 lines
 # echo "ACPI0012:00" > /sys/bus/acpi/drivers/nfit/unbind

---

This time with a commit message and credit to Jane for finding it.

Thanks, applied.
_______________________________________________
Linux-nvdimm mailing list
[email protected]
https://lists.01.org/mailman/listinfo/linux-nvdimm