usb: chipidea: Tegra USB DMA fix + cleanups
cover usb: chipidea: Tegra USB DMA fix + cleanups
This series contains a fix for a DMA buffer overflow in Tegra USB driver followed by a cleanup of the DMA bounce buffer code and another cleanup to make probe() consistently use dev_err_probe() for error printk()s.
Michał Mirosław (3):
usb: chipidea: Fix DMA overwrite for Tegra
usb: chipidea: Simplify Tegra DMA alignment code
usb: chipidea: tegra: Consistently use dev_err_probe()
drivers/usb/chipidea/ci_hdrc_tegra.c | 16 ++++------ drivers/usb/chipidea/host.c | 48 ++++++++++++---------------- 2 files changed, 27 insertions(+), 37 deletions(-)
Signature · 1 line
2.39.2
PATCH 3/3 usb: chipidea: tegra: Consistently use dev_err_probe()
Convert all error exits from probe() to dev_err_probe().
--- drivers/usb/chipidea/ci_hdrc_tegra.c | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-)
drivers/usb/chipidea/ci_hdrc_tegra.c +7 -9
@@ -293,14 +293,12 @@ static int tegra_usb_probe(struct platform_device *pdev) usb->phy =devm_usb_get_phy_by_phandle (&pdev->dev,"nvidia,phy" ,0 );if (IS_ERR (usb->phy))return dev_err_probe (&pdev->dev,PTR_ERR (usb->phy), -"failed to get PHY\n" ); +"failed to get PHY" ); usb->clk =devm_clk_get (&pdev->dev,NULL ); -if (IS_ERR (usb->clk)) { - err =PTR_ERR (usb->clk); -dev_err (&pdev->dev,"failed to get clock: %d\n" , err); -return err; - } +if (IS_ERR (usb->clk)) +return dev_err_probe (&pdev->dev,PTR_ERR (usb->clk), +"failed to get clock" ); err =devm_tegra_core_dev_init_opp_table_common (&pdev->dev);if (err)
@@ -316,7 +314,7 @@ static int tegra_usb_probe(struct platform_device *pdev) err =tegra_usb_reset_controller (&pdev->dev);if (err) { -dev_err (&pdev->dev,"failed to reset controller: %d\n" , err); +dev_err_probe (&pdev->dev, err,"failed to reset controller" ); goto fail_power_off; }
@@ -347,8 +345,8 @@ static int tegra_usb_probe(struct platform_device *pdev) usb->dev =ci_hdrc_add_device (&pdev->dev, pdev->resource, pdev->num_resources, &usb->data);if (IS_ERR (usb->dev)) { - err =PTR_ERR (usb->dev); -dev_err (&pdev->dev,"failed to add HDRC device: %d\n" , err); + err =dev_err_probe (&pdev->dev,PTR_ERR (usb->dev), +"failed to add HDRC device" ); goto phy_shutdown; }
Signature · 1 line
2.39.2
PATCH 2/3 usb: chipidea: Simplify Tegra DMA alignment code
In reply to Michał MirosławThe USB host on Tegra3 works with DWORD alignment. Previous code tried to align the buffer, but it did align the wrapper struct instead, so the buffer was at a constant offset of 8 bytes (two pointers) from assumed alignment. Since kmalloc() guarantees at least 8-byte alignment, the alignment-extending is removed.
--- drivers/usb/chipidea/host.c | 45 +++++++++++++++---------------------- 1 file changed, 18 insertions(+), 27 deletions(-)
drivers/usb/chipidea/host.c +18 -27
@@ -30,8 +30,7 @@ struct ehci_ci_priv { };struct ci_hdrc_dma_aligned_buffer { -void *kmalloc_ptr; -void *old_xfer_buffer; +void *original_buffer;u8 data[]; };
@@ -380,60 +379,52 @@ static int ci_ehci_bus_suspend(struct usb_hcd *hcd)return 0 ; } -static void ci_hdrc_free_dma_aligned_buffer (struct urb *urb) +static void ci_hdrc_free_dma_aligned_buffer (struct urb *urb,bool copy_back) {struct ci_hdrc_dma_aligned_buffer *temp; -size_t length;if (!(urb->transfer_flags &URB_ALIGNED_TEMP_BUFFER ))return ; + urb->transfer_flags &= ~URB_ALIGNED_TEMP_BUFFER ; temp =container_of (urb->transfer_buffer, struct ci_hdrc_dma_aligned_buffer, data); + urb->transfer_buffer = temp->original_buffer; + +if (copy_back &&usb_urb_dir_in (urb)) { +size_t length; -if (usb_urb_dir_in (urb)) {if (usb_pipeisoc (urb->pipe)) length = urb->transfer_buffer_length;else length = urb->actual_length; -memcpy (temp->old_xfer_buffer, temp->data, length); +memcpy (temp->original_buffer, temp->data, length); } - urb->transfer_buffer = temp->old_xfer_buffer; -kfree (temp->kmalloc_ptr); - urb->transfer_flags &= ~URB_ALIGNED_TEMP_BUFFER ; +kfree (temp); }static int ci_hdrc_alloc_dma_aligned_buffer (struct urb *urb,gfp_t mem_flags) { -struct ci_hdrc_dma_aligned_buffer *temp, *kmalloc_ptr; -const unsigned int ci_hdrc_usb_dma_align =32 ; -size_t kmalloc_size; +struct ci_hdrc_dma_aligned_buffer *temp;if (urb->num_sgs || urb->sg || urb->transfer_buffer_length ==0 )return 0 ; -if (!((uintptr_t )urb->transfer_buffer & (ci_hdrc_usb_dma_align -1 )) && !(urb->transfer_buffer_length &3 )) +if (IS_ALIGNED ((uintptr_t )urb->transfer_buffer,4 ) + &&IS_ALIGNED (urb->transfer_buffer_length,4 ))return 0 ; -/* Allocate a buffer with enough padding for alignment */ - kmalloc_size =ALIGN (urb->transfer_buffer_length,4 ) + -sizeof (struct ci_hdrc_dma_aligned_buffer ) + - ci_hdrc_usb_dma_align -1 ; - - kmalloc_ptr =kmalloc (kmalloc_size, mem_flags); -if (!kmalloc_ptr) + temp =kmalloc (sizeof (*temp) +ALIGN (urb->transfer_buffer_length,4 ), mem_flags); +if (!temp)return -ENOMEM ; -/* Position our struct dma_aligned_buffer such that data is aligned */ - temp =PTR_ALIGN (kmalloc_ptr +1 , ci_hdrc_usb_dma_align) -1 ; - temp->kmalloc_ptr = kmalloc_ptr; - temp->old_xfer_buffer = urb->transfer_buffer;if (usb_urb_dir_out (urb))memcpy (temp->data, urb->transfer_buffer, urb->transfer_buffer_length); + + temp->original_buffer = urb->transfer_buffer; urb->transfer_buffer = temp->data; - urb->transfer_flags |=URB_ALIGNED_TEMP_BUFFER ;return 0 ;
@@ -450,7 +441,7 @@ static int ci_hdrc_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb, ret =usb_hcd_map_urb_for_dma (hcd, urb, mem_flags);if (ret) -ci_hdrc_free_dma_aligned_buffer (urb); +ci_hdrc_free_dma_aligned_buffer (urb, false);return ret; }
@@ -458,7 +449,7 @@ static int ci_hdrc_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,static void ci_hdrc_unmap_urb_for_dma (struct usb_hcd *hcd,struct urb *urb) {usb_hcd_unmap_urb_for_dma (hcd, urb); -ci_hdrc_free_dma_aligned_buffer (urb); +ci_hdrc_free_dma_aligned_buffer (urb, true); }#ifdef CONFIG_PM_SLEEP
Signature · 1 line
2.39.2
PATCH 1/3 usb: chipidea: Fix DMA overwrite for Tegra
In reply to Michał MirosławTegra USB controllers seem to issue DMA in doubleword-sized chunks and thus may write past the buffer provided. This is detected by SLUB:
=============================================================================
BUG kmalloc-64 (Tainted: G B ): kmalloc Redzone overwritten
-----------------------------------------------------------------------------
0x8555cd02-0x8555cd03 @offset=3330. First byte 0x0 instead of 0xcc Allocated in usb_get_status+0x2b/0xac age=1 cpu=3 pid=41
__kmem_cache_alloc_node+0x12f/0x1e4 __kmalloc+0x33/0x8c usb_get_status+0x2b/0xac hub_probe+0x5e9/0xcec usb_probe_interface+0xbf/0x21c really_probe+0xa5/0x2c4 __driver_probe_device+0x75/0x174 driver_probe_device+0x31/0x94 __device_attach_driver+0x65/0xc0 bus_for_each_drv+0x4b/0x74 __device_attach+0x69/0x120 bus_probe_device+0x65/0x6c device_add+0x48b/0x5f8 usb_set_configuration+0x37b/0x6b4 usb_generic_driver_probe+0x37/0x68 usb_probe_device+0x35/0xb4
Slab 0xbf622b80 objects=21 used=18 fp=0x8555cdc0 flags=0x800(slab|zone=0) Object 0x8555cd00 @offset=3328 fp=0x00000000
Redzone 8555ccc0: cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc ................ Redzone 8555ccd0: cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc ................ Redzone 8555cce0: cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc ................ Redzone 8555ccf0: cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc ................
Object 8555cd00: 01 00 00 00 cc cc cc cc cc cc cc cc cc cc cc cc ................
Object 8555cd10: cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc ................
Object 8555cd20: cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc ................
Object 8555cd30: cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc ................
Redzone 8555cd40: cc cc cc cc ....
Padding 8555cd74: 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a ZZZZZZZZZZZZ
CPU: 3 PID: 41 Comm: kworker/3:1 Tainted: G B 6.6.0-rc1mq-00118-g59786f827ea1 #1115 Hardware name: NVIDIA Tegra SoC (Flattened Device Tree) Workqueue: usb_hub_wq hub_event [<8010ca28>] (unwind_backtrace) from [<801090a5>] (show_stack+0x11/0x14) [<801090a5>] (show_stack) from [<805da2fb>] (dump_stack_lvl+0x4d/0x7c) [<805da2fb>] (dump_stack_lvl) from [<8026464f>] (check_bytes_and_report+0xb3/0xe4) [<8026464f>] (check_bytes_and_report) from [<802648e1>] (check_object+0x261/0x290) [<802648e1>] (check_object) from [<802671b1>] (free_to_partial_list+0x105/0x3f8) [<802671b1>] (free_to_partial_list) from [<80268613>] (__kmem_cache_free+0x103/0x128) [<80268613>] (__kmem_cache_free) from [<80425a67>] (usb_get_status+0x73/0xac) [<80425a67>] (usb_get_status) from [<80421b31>] (hub_probe+0x5e9/0xcec) [<80421b31>] (hub_probe) from [<80428bbb>] (usb_probe_interface+0xbf/0x21c) [<80428bbb>] (usb_probe_interface) from [<803ee13d>] (really_probe+0xa5/0x2c4) [<803ee13d>] (really_probe) from [<803ee3d1>] (__driver_probe_device+0x75/0x174) [<803ee3d1>] (__driver_probe_device) from [<803ee501>] (driver_probe_device+0x31/0x94)
usb 1-1: device descriptor read/8, error -71
--- drivers/usb/chipidea/host.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/usb/chipidea/host.c +4 -3
@@ -411,12 +411,13 @@ static int ci_hdrc_alloc_dma_aligned_buffer(struct urb *urb, gfp_t mem_flags)const unsigned int ci_hdrc_usb_dma_align =32 ;size_t kmalloc_size; -if (urb->num_sgs || urb->sg || urb->transfer_buffer_length ==0 || - !((uintptr_t )urb->transfer_buffer & (ci_hdrc_usb_dma_align -1 ))) +if (urb->num_sgs || urb->sg || urb->transfer_buffer_length ==0 ) +return 0 ; +if (!((uintptr_t )urb->transfer_buffer & (ci_hdrc_usb_dma_align -1 )) && !(urb->transfer_buffer_length &3 ))return 0 ;/* Allocate a buffer with enough padding for alignment */ - kmalloc_size = urb->transfer_buffer_length + + kmalloc_size =ALIGN (urb->transfer_buffer_length,4 ) +sizeof (struct ci_hdrc_dma_aligned_buffer ) + ci_hdrc_usb_dma_align -1 ;
Signature · 1 line
2.39.2
Peter Chen
maintainerRe: [PATCH 1/3] usb: chipidea: Fix DMA overwrite for Tegra
Quoted 72 lines · Michał Mirosław
Tegra USB controllers seem to issue DMA in doubleword-sized chunks and thus may write past the buffer provided. This is detected by SLUB:
=============================================================================
BUG kmalloc-64 (Tainted: G B ): kmalloc Redzone overwritten
-----------------------------------------------------------------------------0x8555cd02-0x8555cd03 @offset=3330. First byte
0x0instead of0xccAllocated inusb_get_status+0x2b/0xacage=1 cpu=3 pid=41__kmem_cache_alloc_node+0x12f/0x1e4 __kmalloc+0x33/0x8c usb_get_status+0x2b/0xac hub_probe+0x5e9/0xcec usb_probe_interface+0xbf/0x21c really_probe+0xa5/0x2c4 __driver_probe_device+0x75/0x174 driver_probe_device+0x31/0x94 __device_attach_driver+0x65/0xc0 bus_for_each_drv+0x4b/0x74 __device_attach+0x69/0x120 bus_probe_device+0x65/0x6c device_add+0x48b/0x5f8 usb_set_configuration+0x37b/0x6b4 usb_generic_driver_probe+0x37/0x68 usb_probe_device+0x35/0xb4Slab
0xbf622b80objects=21 used=18 fp=0x8555cdc0 flags=0x800(slab|zone=0) Object0x8555cd00@offset=3328 fp=0x00000000Redzone
8555ccc0: cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc ................ Redzone8555ccd0: cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc ................ Redzone8555cce0: cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc ................ Redzone8555ccf0: cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc ................
Object8555cd00: 01 00 00 00 cc cc cc cc cc cc cc cc cc cc cc cc ................
Object8555cd10: cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc ................
Object8555cd20: cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc ................
Object8555cd30: cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc ................
Redzone8555cd40: cc cc cc cc ....
Padding8555cd74: 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a ZZZZZZZZZZZZCPU: 3 PID: 41 Comm: kworker/3:1 Tainted: G B 6.6.0-rc1mq-00118-g59786f827ea1 #1115 Hardware name: NVIDIA Tegra SoC (Flattened Device Tree) Workqueue: usb_hub_wq hub_event [<8010ca28>] (unwind_backtrace) from [<801090a5>] (show_stack+0x11/0x14) [<801090a5>] (show_stack) from [<805da2fb>] (dump_stack_lvl+0x4d/0x7c) [<805da2fb>] (dump_stack_lvl) from [<8026464f>] (check_bytes_and_report+0xb3/0xe4) [<8026464f>] (check_bytes_and_report) from [<802648e1>] (check_object+0x261/0x290) [<802648e1>] (check_object) from [<802671b1>] (free_to_partial_list+0x105/0x3f8) [<802671b1>] (free_to_partial_list) from [<80268613>] (__kmem_cache_free+0x103/0x128) [<80268613>] (__kmem_cache_free) from [<80425a67>] (usb_get_status+0x73/0xac) [<80425a67>] (usb_get_status) from [<80421b31>] (hub_probe+0x5e9/0xcec) [<80421b31>] (hub_probe) from [<80428bbb>] (usb_probe_interface+0xbf/0x21c) [<80428bbb>] (usb_probe_interface) from [<803ee13d>] (really_probe+0xa5/0x2c4) [<803ee13d>] (really_probe) from [<803ee3d1>] (__driver_probe_device+0x75/0x174) [<803ee3d1>] (__driver_probe_device) from [<803ee501>] (driver_probe_device+0x31/0x94)usb 1-1: device descriptor read/8, error -71
Fixes fc53d5279094 ("usb: chipidea: tegra: Support host mode")Signed-off-by Michał Mirosław <[email protected]>--- drivers/usb/chipidea/host.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)drivers/usb/chipidea/host.c +1
-2@@ -411,12 +411,13 @@ static int ci_hdrc_alloc_dma_aligned_buffer(struct urb *urb, gfp_t mem_flags)const unsigned int ci_hdrc_usb_dma_align =32 ;size_t kmalloc_size; -if (urb->num_sgs || urb->sg || urb->transfer_buffer_length ==0 || - !((uintptr_t )urb->transfer_buffer & (ci_hdrc_usb_dma_align -1 ))) +if (urb->num_sgs || urb->sg || urb->transfer_buffer_length ==0 )
drivers/usb/chipidea/host.c static int ci_hdrc_alloc_dma_aligned_buffer(struct urb *urb, gfp_t mem_flags) · line 423
Quoted 10 lines
drivers/usb/chipidea/host.c +3
-1+return 0 ; +if (!((uintptr_t )urb->transfer_buffer & (ci_hdrc_usb_dma_align -1 )) && !(urb->transfer_buffer_length &3 ))return 0 ;/* Allocate a buffer with enough padding for alignment */ - kmalloc_size = urb->transfer_buffer_length + + kmalloc_size =ALIGN (urb->transfer_buffer_length,4 ) +sizeof (struct ci_hdrc_dma_aligned_buffer ) + ci_hdrc_usb_dma_align -1 ;
Would you please explain why you make these changes?
Signature · 2 lines
Thanks, Peter Chen
Re: [PATCH 3/3] usb: chipidea: tegra: Consistently use dev_err_probe()
In reply to Michał MirosławQuoted 3 lines · Michał Mirosław
Convert all error exits from
probe()todev_err_probe().Signed-off-by Michał Mirosław <[email protected]>
Peter
Quoted 50 lines
--- drivers/usb/chipidea/ci_hdrc_tegra.c | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-)drivers/usb/chipidea/ci_hdrc_tegra.c +7
-9@@ -293,14 +293,12 @@ static int tegra_usb_probe(struct platform_device *pdev) usb->phy =devm_usb_get_phy_by_phandle (&pdev->dev,"nvidia,phy" ,0 );if (IS_ERR (usb->phy))return dev_err_probe (&pdev->dev,PTR_ERR (usb->phy), -"failed to get PHY\n" ); +"failed to get PHY" ); usb->clk =devm_clk_get (&pdev->dev,NULL ); -if (IS_ERR (usb->clk)) { - err =PTR_ERR (usb->clk); -dev_err (&pdev->dev,"failed to get clock: %d\n" , err); -return err; - } +if (IS_ERR (usb->clk)) +return dev_err_probe (&pdev->dev,PTR_ERR (usb->clk), +"failed to get clock" ); err =devm_tegra_core_dev_init_opp_table_common (&pdev->dev);if (err)@@ -316,7 +314,7 @@ static int tegra_usb_probe(struct platform_device *pdev) err =tegra_usb_reset_controller (&pdev->dev);if (err) { -dev_err (&pdev->dev,"failed to reset controller: %d\n" , err); +dev_err_probe (&pdev->dev, err,"failed to reset controller" ); goto fail_power_off; }@@ -347,8 +345,8 @@ static int tegra_usb_probe(struct platform_device *pdev) usb->dev =ci_hdrc_add_device (&pdev->dev, pdev->resource, pdev->num_resources, &usb->data);if (IS_ERR (usb->dev)) { - err =PTR_ERR (usb->dev); -dev_err (&pdev->dev,"failed to add HDRC device: %d\n" , err); + err =dev_err_probe (&pdev->dev,PTR_ERR (usb->dev), +"failed to add HDRC device" ); goto phy_shutdown; }Signature · 1 line
2.39.2
Signature · 2 lines
Thanks, Peter Chen
Re: [PATCH 1/3] usb: chipidea: Fix DMA overwrite for Tegra
In reply to Peter ChenQuoted 77 lines · Peter Chen
Quoted 76 lines · Michał Mirosław
Tegra USB controllers seem to issue DMA in doubleword-sized chunks and thus may write past the buffer provided. This is detected by SLUB:
=============================================================================
BUG kmalloc-64 (Tainted: G B ): kmalloc Redzone overwritten
-----------------------------------------------------------------------------0x8555cd02-0x8555cd03 @offset=3330. First byte
0x0instead of0xccAllocated inusb_get_status+0x2b/0xacage=1 cpu=3 pid=41__kmem_cache_alloc_node+0x12f/0x1e4 __kmalloc+0x33/0x8c usb_get_status+0x2b/0xac hub_probe+0x5e9/0xcec usb_probe_interface+0xbf/0x21c really_probe+0xa5/0x2c4 __driver_probe_device+0x75/0x174 driver_probe_device+0x31/0x94 __device_attach_driver+0x65/0xc0 bus_for_each_drv+0x4b/0x74 __device_attach+0x69/0x120 bus_probe_device+0x65/0x6c device_add+0x48b/0x5f8 usb_set_configuration+0x37b/0x6b4 usb_generic_driver_probe+0x37/0x68 usb_probe_device+0x35/0xb4Slab
0xbf622b80objects=21 used=18 fp=0x8555cdc0 flags=0x800(slab|zone=0) Object0x8555cd00@offset=3328 fp=0x00000000Redzone
8555ccc0: cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc ................ Redzone8555ccd0: cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc ................ Redzone8555cce0: cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc ................ Redzone8555ccf0: cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc ................
Object8555cd00: 01 00 00 00 cc cc cc cc cc cc cc cc cc cc cc cc ................
Object8555cd10: cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc ................
Object8555cd20: cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc ................
Object8555cd30: cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc ................
Redzone8555cd40: cc cc cc cc ....
Padding8555cd74: 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a ZZZZZZZZZZZZCPU: 3 PID: 41 Comm: kworker/3:1 Tainted: G B 6.6.0-rc1mq-00118-g59786f827ea1 #1115 Hardware name: NVIDIA Tegra SoC (Flattened Device Tree) Workqueue: usb_hub_wq hub_event [<8010ca28>] (unwind_backtrace) from [<801090a5>] (show_stack+0x11/0x14) [<801090a5>] (show_stack) from [<805da2fb>] (dump_stack_lvl+0x4d/0x7c) [<805da2fb>] (dump_stack_lvl) from [<8026464f>] (check_bytes_and_report+0xb3/0xe4) [<8026464f>] (check_bytes_and_report) from [<802648e1>] (check_object+0x261/0x290) [<802648e1>] (check_object) from [<802671b1>] (free_to_partial_list+0x105/0x3f8) [<802671b1>] (free_to_partial_list) from [<80268613>] (__kmem_cache_free+0x103/0x128) [<80268613>] (__kmem_cache_free) from [<80425a67>] (usb_get_status+0x73/0xac) [<80425a67>] (usb_get_status) from [<80421b31>] (hub_probe+0x5e9/0xcec) [<80421b31>] (hub_probe) from [<80428bbb>] (usb_probe_interface+0xbf/0x21c) [<80428bbb>] (usb_probe_interface) from [<803ee13d>] (really_probe+0xa5/0x2c4) [<803ee13d>] (really_probe) from [<803ee3d1>] (__driver_probe_device+0x75/0x174) [<803ee3d1>] (__driver_probe_device) from [<803ee501>] (driver_probe_device+0x31/0x94)usb 1-1: device descriptor read/8, error -71
Fixes fc53d5279094 ("usb: chipidea: tegra: Support host mode")Signed-off-by Michał Mirosław <[email protected]>--- drivers/usb/chipidea/host.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)drivers/usb/chipidea/host.c +3
-2@@ -411,12 +411,13 @@ static int ci_hdrc_alloc_dma_aligned_buffer(struct urb *urb, gfp_t mem_flags)const unsigned int ci_hdrc_usb_dma_align =32 ;size_t kmalloc_size; -if (urb->num_sgs || urb->sg || urb->transfer_buffer_length ==0 || - !((uintptr_t )urb->transfer_buffer & (ci_hdrc_usb_dma_align -1 ))) +if (urb->num_sgs || urb->sg || urb->transfer_buffer_length ==0 ) +return 0 ; +if (!((uintptr_t )urb->transfer_buffer & (ci_hdrc_usb_dma_align -1 )) && !(urb->transfer_buffer_length &3 ))return 0 ;
Quoted 8 lines
drivers/usb/chipidea/host.c static int ci_hdrc_alloc_dma_aligned_buffer(struct urb *urb, gfp_t mem_flags) · line 423
Quoted 6 lines
drivers/usb/chipidea/host.c +1
-1/* Allocate a buffer with enough padding for alignment */ - kmalloc_size = urb->transfer_buffer_length + + kmalloc_size =ALIGN (urb->transfer_buffer_length,4 ) +sizeof (struct ci_hdrc_dma_aligned_buffer ) + ci_hdrc_usb_dma_align -1 ;Would you please explain why you make these changes?
Can you point out what's unclear in the commit message? This is to fix a buffer overflow by DMA from the USB controller as it seems to write data 32-bit word at a time. What the patch does is extend the workaround code to account for the extra room needed at the tail of the buffer.
Best Regards
Michał Mirosław
Peter Chen
maintainerRe: [PATCH 1/3] usb: chipidea: Fix DMA overwrite for Tegra
Quoted 87 lines · Michał Mirosław
Quoted 77 lines · Peter Chen
Quoted 76 lines · Michał Mirosław
Tegra USB controllers seem to issue DMA in doubleword-sized chunks and thus may write past the buffer provided. This is detected by SLUB:
=============================================================================
BUG kmalloc-64 (Tainted: G B ): kmalloc Redzone overwritten
-----------------------------------------------------------------------------0x8555cd02-0x8555cd03 @offset=3330. First byte
0x0instead of0xccAllocated inusb_get_status+0x2b/0xacage=1 cpu=3 pid=41__kmem_cache_alloc_node+0x12f/0x1e4 __kmalloc+0x33/0x8c usb_get_status+0x2b/0xac hub_probe+0x5e9/0xcec usb_probe_interface+0xbf/0x21c really_probe+0xa5/0x2c4 __driver_probe_device+0x75/0x174 driver_probe_device+0x31/0x94 __device_attach_driver+0x65/0xc0 bus_for_each_drv+0x4b/0x74 __device_attach+0x69/0x120 bus_probe_device+0x65/0x6c device_add+0x48b/0x5f8 usb_set_configuration+0x37b/0x6b4 usb_generic_driver_probe+0x37/0x68 usb_probe_device+0x35/0xb4Slab
0xbf622b80objects=21 used=18 fp=0x8555cdc0 flags=0x800(slab|zone=0) Object0x8555cd00@offset=3328 fp=0x00000000Redzone
8555ccc0: cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc ................ Redzone8555ccd0: cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc ................ Redzone8555cce0: cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc ................ Redzone8555ccf0: cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc ................
Object8555cd00: 01 00 00 00 cc cc cc cc cc cc cc cc cc cc cc cc ................
Object8555cd10: cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc ................
Object8555cd20: cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc ................
Object8555cd30: cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc ................
Redzone8555cd40: cc cc cc cc ....
Padding8555cd74: 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a ZZZZZZZZZZZZCPU: 3 PID: 41 Comm: kworker/3:1 Tainted: G B 6.6.0-rc1mq-00118-g59786f827ea1 #1115 Hardware name: NVIDIA Tegra SoC (Flattened Device Tree) Workqueue: usb_hub_wq hub_event [<8010ca28>] (unwind_backtrace) from [<801090a5>] (show_stack+0x11/0x14) [<801090a5>] (show_stack) from [<805da2fb>] (dump_stack_lvl+0x4d/0x7c) [<805da2fb>] (dump_stack_lvl) from [<8026464f>] (check_bytes_and_report+0xb3/0xe4) [<8026464f>] (check_bytes_and_report) from [<802648e1>] (check_object+0x261/0x290) [<802648e1>] (check_object) from [<802671b1>] (free_to_partial_list+0x105/0x3f8) [<802671b1>] (free_to_partial_list) from [<80268613>] (__kmem_cache_free+0x103/0x128) [<80268613>] (__kmem_cache_free) from [<80425a67>] (usb_get_status+0x73/0xac) [<80425a67>] (usb_get_status) from [<80421b31>] (hub_probe+0x5e9/0xcec) [<80421b31>] (hub_probe) from [<80428bbb>] (usb_probe_interface+0xbf/0x21c) [<80428bbb>] (usb_probe_interface) from [<803ee13d>] (really_probe+0xa5/0x2c4) [<803ee13d>] (really_probe) from [<803ee3d1>] (__driver_probe_device+0x75/0x174) [<803ee3d1>] (__driver_probe_device) from [<803ee501>] (driver_probe_device+0x31/0x94)usb 1-1: device descriptor read/8, error -71
Fixes fc53d5279094 ("usb: chipidea: tegra: Support host mode")Signed-off-by Michał Mirosław <[email protected]>--- drivers/usb/chipidea/host.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)drivers/usb/chipidea/host.c +3
-2@@ -411,12 +411,13 @@ static int ci_hdrc_alloc_dma_aligned_buffer(struct urb *urb, gfp_t mem_flags)const unsigned int ci_hdrc_usb_dma_align =32 ;size_t kmalloc_size; -if (urb->num_sgs || urb->sg || urb->transfer_buffer_length ==0 || - !((uintptr_t )urb->transfer_buffer & (ci_hdrc_usb_dma_align -1 ))) +if (urb->num_sgs || urb->sg || urb->transfer_buffer_length ==0 ) +return 0 ; +if (!((uintptr_t )urb->transfer_buffer & (ci_hdrc_usb_dma_align -1 )) && !(urb->transfer_buffer_length &3 ))return 0 ;Quoted 8 lines
drivers/usb/chipidea/host.c static int ci_hdrc_alloc_dma_aligned_buffer(struct urb *urb, gfp_t mem_flags) · line 423
Quoted 6 lines
drivers/usb/chipidea/host.c +1
-1/* Allocate a buffer with enough padding for alignment */ - kmalloc_size = urb->transfer_buffer_length + + kmalloc_size =ALIGN (urb->transfer_buffer_length,4 ) +sizeof (struct ci_hdrc_dma_aligned_buffer ) + ci_hdrc_usb_dma_align -1 ;Would you please explain why you make these changes?
Quoted 1 line
Can you point out what's unclear in the commit message?
You may appended below words to commit log, current commit log looks too simple.
Quoted 7 lines
This is to fix
a buffer overflow by DMA from the USB controller as it seems to write data 32-bit word at a time. What the patch does is extend the workaround code to account for the extra room needed at the tail of the buffer.Best Regards
Michał Mirosław
Signature · 2 lines
Thanks, Peter Chen