Skip to contentSkip to conversation

driver-core: dev_to_node() should handle NULL pointers

PATCH In review Dan Carpenter 2 participants 4 messages Jul 20, 2012 – Aug 16, 2012

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 driver-core: dev_to_node() should handle NULL pointers

What prompted this patch is that in dma_pool_create() we call dev_to_node() before checking whether "dev" is NULL. It looks like there are places which call dma_pool_create() with a NULL pointer. An example is in drivers/usb/gadget/amd5536udc.c.

---
Static checker fix.

include/linux/device.h +3 -1
index aa7b3b4..c80e7a8d 100644
@@ -714,7 +714,9 @@ int dev_set_name(struct device *dev, const char *name, ...);
 #ifdef CONFIG_NUMA
 static inline int dev_to_node(struct device *dev)
 {
-	return dev->numa_node;
+	if (dev)
+		return dev->numa_node;
+	return -1;
 }
 static inline void set_dev_node(struct device *dev, int node)
 {
#1 lore raw
Quoted 14 lines · Dan Carpenter

What prompted this patch is that in dma_pool_create() we call dev_to_node() before checking whether "dev" is NULL. It looks like there are places which call dma_pool_create() with a NULL pointer. An example is in drivers/usb/gadget/amd5536udc.c.

---
Static checker fix.

include/linux/device.h +0 -0
index aa7b3b4..c80e7a8d 100644

include/linux/device.h int dev_set_name(struct device *dev, const char *name, ...); · line 719

Quoted 7 lines
include/linux/device.h +3 -1
@@ -714,7 +714,9 @@ int dev_set_name(struct device *dev, const char *name, ...);
 #ifdef CONFIG_NUMA
 static inline int dev_to_node(struct device *dev)
 {
-	return dev->numa_node;
+	if (dev)
+		return dev->numa_node;
+	return -1;

What happens if this function returns -1? Can the callers properly handle this?

thanks,

greg k-h

#2 lore raw
Quoted 10 lines · Greg Kroah-Hartman
Quoted 9 lines · Dan Carpenter

What prompted this patch is that in dma_pool_create() we call dev_to_node() before checking whether "dev" is NULL. It looks like there are places which call dma_pool_create() with a NULL pointer. An example is in drivers/usb/gadget/amd5536udc.c.

---
Static checker fix.

Quoted 16 lines

include/linux/device.h int dev_set_name(struct device *dev, const char *name, ...); · line 719

Quoted 12 lines
include/linux/device.h +3 -1
index aa7b3b4..c80e7a8d 100644
@@ -714,7 +714,9 @@ int dev_set_name(struct device *dev, const char *name, ...);
 #ifdef CONFIG_NUMA
 static inline int dev_to_node(struct device *dev)
 {
-	return dev->numa_node;
+	if (dev)
+		return dev->numa_node;
+	return -1;

What happens if this function returns -1? Can the callers properly handle this?

Gar. Now I'm not sure any more.

-1 means no affinity and it's what the dev_to_node() returns if NUMA is disabled. But now I think probably it's important to get the NUMA node correct in dma_pool_create() so this isn't the right answer.

dma_pool_create() is not correct. It has code to handle a NULL "dev" pointer, but the dev_to_node() dereference will cause an oops before we reach it. I'm think this is a real issue that affects a couple drivers. Maybe those people compile without NUMA?

I'm not sure the right fix now.

regards,
dan carpenter

#3 lore raw
Quoted 35 lines · Dan Carpenter
Quoted 10 lines · Greg Kroah-Hartman
Quoted 9 lines · Dan Carpenter

What prompted this patch is that in dma_pool_create() we call dev_to_node() before checking whether "dev" is NULL. It looks like there are places which call dma_pool_create() with a NULL pointer. An example is in drivers/usb/gadget/amd5536udc.c.

---
Static checker fix.

Quoted 16 lines

include/linux/device.h int dev_set_name(struct device *dev, const char *name, ...); · line 719

Quoted 12 lines
include/linux/device.h +3 -1
index aa7b3b4..c80e7a8d 100644
@@ -714,7 +714,9 @@ int dev_set_name(struct device *dev, const char *name, ...);
 #ifdef CONFIG_NUMA
 static inline int dev_to_node(struct device *dev)
 {
-	return dev->numa_node;
+	if (dev)
+		return dev->numa_node;
+	return -1;

What happens if this function returns -1? Can the callers properly handle this?

Gar. Now I'm not sure any more.

-1 means no affinity and it's what the dev_to_node() returns if NUMA is disabled. But now I think probably it's important to get the NUMA node correct in dma_pool_create() so this isn't the right answer.

Quoted 6 lines

dma_pool_create() is not correct. It has code to handle a NULL "dev" pointer, but the dev_to_node() dereference will cause an oops before we reach it. I'm think this is a real issue that affects a couple drivers. Maybe those people compile without NUMA?

I'm not sure the right fix now.

Ok, I'll drop this one then.

greg k-h