Skip to contentSkip to conversation

usb: Check for DMA capable buffer sanity

PATCH In review Florian Fainelli 4 participants 11 messages Apr 26, 2017 – May 31, 2017

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

v3 cover usb: Check for DMA capable buffer sanity

Changes in v3:

- added check in usb_gadget_map_request_by_dev (Felipe), new patch
- improved commit message description (Clemens)
- added additiona checks for urb->setup_packet (Alan)

Changes in v2:

- moved the check from usb_start_wait_urb() to usb_hcd_map_urb_for_dma()

Florian Fainelli (2):
usb: core: Check URB setup_packet and transfer_buffer sanity
usb: udc: core: Error if req->buf is either from vmalloc or stack

 drivers/usb/core/hcd.c        | 12 ++++++++++++
 drivers/usb/gadget/udc/core.c |  9 +++++++++
 2 files changed, 21 insertions(+)
Signature · 1 line
2.9.3
#1 lore raw

PATCH v3 1/2 usb: core: Check URB setup_packet and transfer_buffer sanity

Update usb_hcd_map_urb_for_dma() to check for an URB's setup_packet and transfer_buffer sanity. We first check that urb->setup_packet is neither coming from vmalloc space nor is an on stack buffer, and if that's the case, produce a warning and return an error. For urb->transfer_buffer there is an existing is_vmalloc_addr() check so we just supplement that with an object_is_on_stack() check, produce a warning if that is the case and also return an error.

---
 drivers/usb/core/hcd.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
drivers/usb/core/hcd.c +12 -0
index 49550790a3cb..26d710eec7da 100644
@@ -26,6 +26,7 @@
 #include <linux/module.h>
 #include <linux/version.h>
 #include <linux/kernel.h>
+#include <linux/sched/task_stack.h>
 #include <linux/slab.h>
 #include <linux/completion.h>
 #include <linux/utsname.h>
@@ -1523,6 +1524,14 @@ int usb_hcd_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
 		if (hcd->self.uses_pio_for_control)
 			return ret;
 		if (IS_ENABLED(CONFIG_HAS_DMA) && hcd->self.uses_dma) {
+			if (is_vmalloc_addr(urb->setup_packet)) {
+				WARN_ONCE(1, "setup packet is not dma capable\n");
+				return -EAGAIN;
+			} else if (object_is_on_stack(urb->setup_packet)) {
+				WARN_ONCE(1, "setup packet is on stack\n");
+				return -EAGAIN;
+			}
+
 			urb->setup_dma = dma_map_single(
 					hcd->self.sysdev,
 					urb->setup_packet,
@@ -1587,6 +1596,9 @@ int usb_hcd_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
 			} else if (is_vmalloc_addr(urb->transfer_buffer)) {
 				WARN_ONCE(1, "transfer buffer not dma capable\n");
 				ret = -EAGAIN;
+			} else if (object_is_on_stack(urb->transfer_buffer)) {
+				WARN_ONCE(1, "transfer buffer is on stack\n");
+				ret = -EAGAIN;
 			} else {
 				urb->transfer_dma = dma_map_single(
 						hcd->self.sysdev,
Signature · 1 line
2.9.3
#2 lore raw

PATCH v3 2/2 usb: udc: core: Error if req->buf is either from vmalloc or stack

In reply to Florian Fainelli

Check that req->buf is a valid DMA capable address, produce a warning and return an error if it's either coming from vmalloc space or is an on stack buffer.

---
 drivers/usb/gadget/udc/core.c | 9 +++++++++
 1 file changed, 9 insertions(+)
drivers/usb/gadget/udc/core.c +9 -0
index efce68e9a8e0..a03aec574f64 100644
@@ -23,6 +23,7 @@
 #include <linux/list.h>
 #include <linux/err.h>
 #include <linux/dma-mapping.h>
+#include <linux/sched/task_stack.h>
 #include <linux/workqueue.h>
 
 #include <linux/usb/ch9.h>
@@ -798,6 +799,14 @@ int usb_gadget_map_request_by_dev(struct device *dev,
 
 		req->num_mapped_sgs = mapped;
 	} else {
+		if (is_vmalloc_addr(req->buf)) {
+			dev_err(dev, "buffer is not dma capable\n");
+			return -EFAULT;
+		} else if (object_is_on_stack(req->buf)) {
+			dev_err(dev, "buffer is on stack\n");
+			return -EFAULT;
+		}
+
 		req->dma = dma_map_single(dev, req->buf, req->length,
 				is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
 
Signature · 1 line
2.9.3
#3 lore raw
In reply to Florian Fainelli
Quoted 9 lines · Florian Fainelli

Changes in v3:

- added check in usb_gadget_map_request_by_dev (Felipe), new patch
- improved commit message description (Clemens)
- added additiona checks for urb->setup_packet (Alan)

Changes in v2:

- moved the check from usb_start_wait_urb() to usb_hcd_map_urb_for_dma()

Is this version looking good now? Thanks!

Quoted 9 lines

Florian Fainelli (2):
usb: core: Check URB setup_packet and transfer_buffer sanity
usb: udc: core: Error if req->buf is either from vmalloc or stack

 drivers/usb/core/hcd.c        | 12 ++++++++++++
 drivers/usb/gadget/udc/core.c |  9 +++++++++
 2 files changed, 21 insertions(+)
Signature · 1 line
Florian
#4 lore raw
Quoted 7 lines · Florian Fainelli
Quoted 6 lines · Florian Fainelli

Changes in v3:

- added check in usb_gadget_map_request_by_dev (Felipe), new patch
- improved commit message description (Clemens)
- added additiona checks for urb->setup_packet (Alan)

Quoted 5 lines
Quoted 3 lines

Changes in v2:

- moved the check from usb_start_wait_urb() to usb_hcd_map_urb_for_dma()

Is this version looking good now? Thanks!

So, it seems I am in a similar situation with the I2C subsystem right now. I need to check the message buffers if they are DMA capable.

Because you have basically the same checks in 3 different places, and I need something similar for I2C, I wondered about a generic place to put these checks. Especially since we want future improvements to these checks applied everywhere immediately. Here is a small diff on what I have now:

===

dma-mapping(?): introduce helper to check for DMA capable addresses

Introduce a helper to check if an address is DMA capable. Such a check is subtle, so it is good to have a centralized place for it.

Note: I am absolutely not sure if dma-mapping.h is a good place for such a function. I just couldn't think of a better one for now.

Second note: I am not even sure the checks are complete (kmapped mem?). I am not an MM expert. But that just strengthens the argument of having on centralized place IMO.

drivers/usb/core/hcd.c +1 -1
index 5dea98358c05c4..777a37b395ff19 100644
@@ -1584,7 +1584,7 @@ int usb_hcd_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
 					ret = -EAGAIN;
 				else
 					urb->transfer_flags |= URB_DMA_MAP_PAGE;
-			} else if (is_vmalloc_addr(urb->transfer_buffer)) {
+			} else if (!is_dma_capable_addr(urb->transfer_buffer)) {
 				WARN_ONCE(1, "transfer buffer not dma capable\n");
 				ret = -EAGAIN;
 			} else {
include/linux/dma-mapping.h +8 -0
index 4f3eecedca2d7c..da8c1230302505 100644
@@ -10,6 +10,8 @@
 #include <linux/scatterlist.h>
 #include <linux/kmemcheck.h>
 #include <linux/bug.h>
+#include <linux/mm.h>
+#include <linux/sched/task_stack.h>
 
 /**
  * List of possible attributes associated with a DMA mapping. The semantics
@@ -818,4 +820,10 @@ static inline int dma_mmap_wc(struct device *dev,
 #define dma_unmap_len_set(PTR, LEN_NAME, VAL)    do { } while (0)
 #endif
 
+/* only works in process context because of stack detection */
+static inline bool is_dma_capable_addr(void *addr)
+{
+	return !(is_vmalloc_or_module_addr(addr) ||
+		 object_is_on_stack(addr));
+}
 #endif
mm/vmalloc.c +1 -0
index 34a1c3e46ed725..47de0c0a700e7c 100644
@@ -261,6 +261,7 @@ int is_vmalloc_or_module_addr(const void *x)
 #endif
 	return is_vmalloc_addr(x);
 }
+EXPORT_SYMBOL_GPL(is_vmalloc_or_module_addr);
 
 /*
  * Walk a vmap address to the struct page it maps.

===

The WIP branch containing also the I2C parts can be found here:

git://git.kernel.org/pub/scm/linux/kernel/git/wsa/linux.git renesas/topic/i2c-core-dma

I think the whole series needs 1 or 2 days more before I send out an RFC, but I thought I'll let you know about my idea already.

Thanks and kind regards,

Wolfram

#5 lore raw

From: Wolfram Sang

Quoted 24 lines

Sent: 28 May 2017 17:04

Quoted 7 lines · Florian Fainelli
Quoted 6 lines · Florian Fainelli

Changes in v3:

- added check in usb_gadget_map_request_by_dev (Felipe), new patch
- improved commit message description (Clemens)
- added additiona checks for urb->setup_packet (Alan)

Quoted 5 lines
Quoted 3 lines

Changes in v2:

- moved the check from usb_start_wait_urb() to usb_hcd_map_urb_for_dma()

Is this version looking good now? Thanks!

So, it seems I am in a similar situation with the I2C subsystem right now. I need to check the message buffers if they are DMA capable.

Because you have basically the same checks in 3 different places, and I need something similar for I2C, I wondered about a generic place to put these checks. Especially since we want future improvements to these checks applied everywhere immediately. Here is a small diff on what I have now:

...

drivers/usb/core/hcd.c int usb_hcd_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb, · line 1587

Quoted 1 line
drivers/usb/core/hcd.c +1 -0
+			} else if (!is_dma_capable_addr(urb->transfer_buffer)) {

For a generic function I'd pass the length as well.
It might be that buffers that don't cross page boundaries might be deemed 'dma-able'.

Possibly more useful would be a variant of (IIRC) dma_map_for_device() that will allocate a suitable bounce buffer for non-dma memory. I think it can already do so for memory that is outside the address range that the device can address (eg for a 32bit PCIe master in 64bit system).

David

#6 lore raw
Quoted 32 lines · David Laight

From: Wolfram Sang

Quoted 24 lines

Sent: 28 May 2017 17:04

Quoted 7 lines · Florian Fainelli
Quoted 6 lines · Florian Fainelli

Changes in v3:

- added check in usb_gadget_map_request_by_dev (Felipe), new patch
- improved commit message description (Clemens)
- added additiona checks for urb->setup_packet (Alan)

Quoted 5 lines
Quoted 3 lines

Changes in v2:

- moved the check from usb_start_wait_urb() to usb_hcd_map_urb_for_dma()

Is this version looking good now? Thanks!

So, it seems I am in a similar situation with the I2C subsystem right now. I need to check the message buffers if they are DMA capable.

Because you have basically the same checks in 3 different places, and I need something similar for I2C, I wondered about a generic place to put these checks. Especially since we want future improvements to these checks applied everywhere immediately. Here is a small diff on what I have now:

...

drivers/usb/core/hcd.c int usb_hcd_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb, · line 1587

Quoted 1 line
drivers/usb/core/hcd.c +1 -0
+			} else if (!is_dma_capable_addr(urb->transfer_buffer)) {

For a generic function I'd pass the length as well.
It might be that buffers that don't cross page boundaries might be deemed 'dma-able'.

Quoted 6 lines

Possibly more useful would be a variant of (IIRC) dma_map_for_device() that will allocate a suitable bounce buffer for non-dma memory. I think it can already do so for memory that is outside the address range that the device can address (eg for a 32bit PCIe master in 64bit system).

Such generic DMA API would be greatly useful!

I tried adding bounce buffers support to handle vmalloc'd buffers in MTD/SPI subsystem. But, there was a need felt for generic DMA API that can allocate bounce buffer for non-dma'able buffers that all drivers can make use of[1][2]

[1] https://lkml.org/lkml/2017/3/1/488
[2] https://lkml.org/lkml/2017/4/25/278

Signature · 2 lines
Regards
Vignesh
#7 lore raw
Quoted 8 lines
Quoted 7 lines

Possibly more useful would be a variant of (IIRC) dma_map_for_device() that will allocate a suitable bounce buffer for non-dma memory. I think it can already do so for memory that is outside the address range that the device can address (eg for a 32bit PCIe master in 64bit system).

Quoted 6 lines

Such generic DMA API would be greatly useful!

I tried adding bounce buffers support to handle vmalloc'd buffers in MTD/SPI subsystem. But, there was a need felt for generic DMA API that can allocate bounce buffer for non-dma'able buffers that all drivers can make use of[1][2]

Yes, I see this DMA API would make sense for subsystems like SPI, MTD or USB. For I2C, I don't think it makes a lot of sense because DMA is rarely used there. Most hardware doesn't even have DMA support and if so, the drivers apply a threshold (say 8 bytes) because most I2C transfers are smaller and setting up DMA for that simply doesn't pay off. And we are still talking of a mostly 100 or 400 kHz bus here.

So, I'd prefer a lightweight helper function telling if DMA is possible/feasible for a given I2C message. If so, do it. If not, falling back to PIO might be good enough for now. We can implement bounce buffer support in the above helper function later. I don't really want to enforce DMA capable buffers for I2C transactions when DMA is so rarely needed there.

So, if I2C is a bit different, then it might simply make sense to keep the function local for I2C now? This seems like a sensible start to me meanwhile.

Thanks to all for the helpful input here!

#8 lore raw
Quoted 4 lines

So, if I2C is a bit different, then it might simply make sense to keep the function local for I2C now? This seems like a sensible start to me meanwhile.

Then again, the DMA API would be for drivers. If the USB core wants to check for capable buffers, such a helper might be nice nonetheless. Especially if the USB core needs the check in at least 3 places.

#9 lore raw
In reply to Wolfram Sang
Quoted 67 lines · Wolfram Sang
Quoted 7 lines · Florian Fainelli
Quoted 6 lines · Florian Fainelli

Changes in v3:

- added check in usb_gadget_map_request_by_dev (Felipe), new patch
- improved commit message description (Clemens)
- added additiona checks for urb->setup_packet (Alan)

Quoted 5 lines
Quoted 3 lines

Changes in v2:

- moved the check from usb_start_wait_urb() to usb_hcd_map_urb_for_dma()

Is this version looking good now? Thanks!

So, it seems I am in a similar situation with the I2C subsystem right now. I need to check the message buffers if they are DMA capable.

Because you have basically the same checks in 3 different places, and I need something similar for I2C, I wondered about a generic place to put these checks. Especially since we want future improvements to these checks applied everywhere immediately. Here is a small diff on what I have now:

===

dma-mapping(?): introduce helper to check for DMA capable addresses

Introduce a helper to check if an address is DMA capable. Such a check is subtle, so it is good to have a centralized place for it.

Note: I am absolutely not sure if dma-mapping.h is a good place for such a function. I just couldn't think of a better one for now.

Second note: I am not even sure the checks are complete (kmapped mem?). I am not an MM expert. But that just strengthens the argument of having on centralized place IMO.

drivers/usb/core/hcd.c +1 -1
index 5dea98358c05c4..777a37b395ff19 100644
@@ -1584,7 +1584,7 @@ int usb_hcd_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
 					ret = -EAGAIN;
 				else
 					urb->transfer_flags |= URB_DMA_MAP_PAGE;
-			} else if (is_vmalloc_addr(urb->transfer_buffer)) {
+			} else if (!is_dma_capable_addr(urb->transfer_buffer)) {
 				WARN_ONCE(1, "transfer buffer not dma capable\n");
 				ret = -EAGAIN;
 			} else {
include/linux/dma-mapping.h +2 -0
index 4f3eecedca2d7c..da8c1230302505 100644
@@ -10,6 +10,8 @@
 #include <linux/scatterlist.h>
 #include <linux/kmemcheck.h>
 #include <linux/bug.h>
+#include <linux/mm.h>
+#include <linux/sched/task_stack.h>
 
 /**
  * List of possible attributes associated with a DMA mapping. The semantics

include/linux/dma-mapping.h static inline int dma_mmap_wc(struct device *dev, · line 827

Quoted 8 lines
include/linux/dma-mapping.h +5 -0
@@ -818,4 +820,10 @@ static inline int dma_mmap_wc(struct device *dev,
 #define dma_unmap_len_set(PTR, LEN_NAME, VAL)    do { } while (0)
 #endif
 
+/* only works in process context because of stack detection */
+static inline bool is_dma_capable_addr(void *addr)
+{
+	return !(is_vmalloc_or_module_addr(addr) ||
+		 object_is_on_stack(addr));

This does not catch kmap'ed buffers which are not directly DMA'able. I would suggest to use virt_addr_valid() instead. Something like:

	return (virt_addr_valid(addr) && !object_is_on_stack(addr));
Quoted 28 lines
include/linux/dma-mapping.h +1 -0
+}
 #endif
mm/vmalloc.c +1 -0
index 34a1c3e46ed725..47de0c0a700e7c 100644
@@ -261,6 +261,7 @@ int is_vmalloc_or_module_addr(const void *x)
 #endif
 	return is_vmalloc_addr(x);
 }
+EXPORT_SYMBOL_GPL(is_vmalloc_or_module_addr);
 
 /*
  * Walk a vmap address to the struct page it maps.

===

The WIP branch containing also the I2C parts can be found here:

git://git.kernel.org/pub/scm/linux/kernel/git/wsa/linux.git renesas/topic/i2c-core-dma

I think the whole series needs 1 or 2 days more before I send out an RFC, but I thought I'll let you know about my idea already.

Thanks and kind regards,

Wolfram

Signature · 2 lines
Regards
Vignesh
#10 lore raw
Quoted 11 lines

include/linux/dma-mapping.h static inline int dma_mmap_wc(struct device *dev, · line 827

Quoted 6 lines
include/linux/dma-mapping.h +5 -0
+/* only works in process context because of stack detection */
+static inline bool is_dma_capable_addr(void *addr)
+{
+	return !(is_vmalloc_or_module_addr(addr) ||
+		 object_is_on_stack(addr));

This does not catch kmap'ed buffers which are not directly DMA'able. I would suggest to use virt_addr_valid() instead. Something like:

	return (virt_addr_valid(addr) && !object_is_on_stack(addr));

Hehe, here is the part of the commit message I have for this code:

===

Second note: I am not even sure the checks complete (kmapped mem?). But that just strengthens the argument of having on centralized place IMO :)

===

So, thanks for the heads up!