2021-03-09 05:28:40

by Sanket Parmar

[permalink] [raw]
Subject: [PATCH 1/2] usb: cdns3: Use dma_pool_* api to alloc trb pool

Allocation of DMA coherent memory in atomic context using
dma_alloc_coherent() might fail on platforms with smaller
DMA region.

To fix it, dma_alloc_coherent() is replaced with dma_pool
API to allocate a smaller chunk of DMA coherent memory for
TRB rings.

Fixes: 7733f6c32e36 ("usb: cdns3: Add Cadence USB3 DRD Driver")
Reported-by: Aswath Govindraju <[email protected]>
Signed-off-by: Sanket Parmar <[email protected]>
---
drivers/usb/cdns3/cdns3-gadget.c | 42 +++++++++++++++++--------------------
drivers/usb/cdns3/cdns3-gadget.h | 1 +
2 files changed, 20 insertions(+), 23 deletions(-)

diff --git a/drivers/usb/cdns3/cdns3-gadget.c b/drivers/usb/cdns3/cdns3-gadget.c
index 582bfec..5f51215 100644
--- a/drivers/usb/cdns3/cdns3-gadget.c
+++ b/drivers/usb/cdns3/cdns3-gadget.c
@@ -59,6 +59,7 @@
#include <linux/dma-mapping.h>
#include <linux/usb/gadget.h>
#include <linux/module.h>
+#include <linux/dmapool.h>
#include <linux/iopoll.h>

#include "core.h"
@@ -190,29 +191,13 @@ dma_addr_t cdns3_trb_virt_to_dma(struct cdns3_endpoint *priv_ep,
return priv_ep->trb_pool_dma + offset;
}

-static int cdns3_ring_size(struct cdns3_endpoint *priv_ep)
-{
- switch (priv_ep->type) {
- case USB_ENDPOINT_XFER_ISOC:
- return TRB_ISO_RING_SIZE;
- case USB_ENDPOINT_XFER_CONTROL:
- return TRB_CTRL_RING_SIZE;
- default:
- if (priv_ep->use_streams)
- return TRB_STREAM_RING_SIZE;
- else
- return TRB_RING_SIZE;
- }
-}
-
static void cdns3_free_trb_pool(struct cdns3_endpoint *priv_ep)
{
struct cdns3_device *priv_dev = priv_ep->cdns3_dev;

if (priv_ep->trb_pool) {
- dma_free_coherent(priv_dev->sysdev,
- cdns3_ring_size(priv_ep),
- priv_ep->trb_pool, priv_ep->trb_pool_dma);
+ dma_pool_free(priv_dev->eps_dma_pool,
+ priv_ep->trb_pool, priv_ep->trb_pool_dma);
priv_ep->trb_pool = NULL;
}
}
@@ -226,7 +211,7 @@ static void cdns3_free_trb_pool(struct cdns3_endpoint *priv_ep)
int cdns3_allocate_trb_pool(struct cdns3_endpoint *priv_ep)
{
struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
- int ring_size = cdns3_ring_size(priv_ep);
+ int ring_size = TRB_RING_SIZE;
int num_trbs = ring_size / TRB_SIZE;
struct cdns3_trb *link_trb;

@@ -234,10 +219,10 @@ int cdns3_allocate_trb_pool(struct cdns3_endpoint *priv_ep)
cdns3_free_trb_pool(priv_ep);

if (!priv_ep->trb_pool) {
- priv_ep->trb_pool = dma_alloc_coherent(priv_dev->sysdev,
- ring_size,
- &priv_ep->trb_pool_dma,
- GFP_DMA32 | GFP_ATOMIC);
+ priv_ep->trb_pool = dma_pool_alloc(priv_dev->eps_dma_pool,
+ GFP_DMA32 | GFP_ATOMIC,
+ &priv_ep->trb_pool_dma);
+
if (!priv_ep->trb_pool)
return -ENOMEM;

@@ -3113,6 +3098,7 @@ static void cdns3_gadget_exit(struct cdns *cdns)

dma_free_coherent(priv_dev->sysdev, 8, priv_dev->setup_buf,
priv_dev->setup_dma);
+ dma_pool_destroy(priv_dev->eps_dma_pool);

kfree(priv_dev->zlp_buf);
usb_put_gadget(&priv_dev->gadget);
@@ -3185,6 +3171,14 @@ static int cdns3_gadget_start(struct cdns *cdns)
/* initialize endpoint container */
INIT_LIST_HEAD(&priv_dev->gadget.ep_list);
INIT_LIST_HEAD(&priv_dev->aligned_buf_list);
+ priv_dev->eps_dma_pool = dma_pool_create("cdns3_eps_dma_pool",
+ priv_dev->sysdev,
+ TRB_RING_SIZE, 8, 0);
+ if (!priv_dev->eps_dma_pool) {
+ dev_err(priv_dev->dev, "Failed to create TRB dma pool\n");
+ ret = -ENOMEM;
+ goto err1;
+ }

ret = cdns3_init_eps(priv_dev);
if (ret) {
@@ -3235,6 +3229,8 @@ static int cdns3_gadget_start(struct cdns *cdns)
err2:
cdns3_free_all_eps(priv_dev);
err1:
+ dma_pool_destroy(priv_dev->eps_dma_pool);
+
usb_put_gadget(&priv_dev->gadget);
cdns->gadget_dev = NULL;
return ret;
diff --git a/drivers/usb/cdns3/cdns3-gadget.h b/drivers/usb/cdns3/cdns3-gadget.h
index 21fa461..ecf9b91 100644
--- a/drivers/usb/cdns3/cdns3-gadget.h
+++ b/drivers/usb/cdns3/cdns3-gadget.h
@@ -1298,6 +1298,7 @@ struct cdns3_device {

struct cdns3_usb_regs __iomem *regs;

+ struct dma_pool *eps_dma_pool;
struct usb_ctrlrequest *setup_buf;
dma_addr_t setup_dma;
void *zlp_buf;
--
1.7.1


2021-03-14 02:52:37

by Peter Chen

[permalink] [raw]
Subject: Re: [PATCH 1/2] usb: cdns3: Use dma_pool_* api to alloc trb pool

On 21-03-09 06:19:39, Sanket Parmar wrote:
> Allocation of DMA coherent memory in atomic context using
> dma_alloc_coherent() might fail on platforms with smaller
> DMA region.
>
> To fix it, dma_alloc_coherent() is replaced with dma_pool
> API to allocate a smaller chunk of DMA coherent memory for
> TRB rings.
>
> Fixes: 7733f6c32e36 ("usb: cdns3: Add Cadence USB3 DRD Driver")

The patch is ok, but I don't think it is a bug-fix, it is an
improvement for smaller DMA region use case.

I will apply it with deletion of above Fixes tag if you have no
more opinion.

Peter
> Reported-by: Aswath Govindraju <[email protected]>
> Signed-off-by: Sanket Parmar <[email protected]>
> ---
> drivers/usb/cdns3/cdns3-gadget.c | 42 +++++++++++++++++--------------------
> drivers/usb/cdns3/cdns3-gadget.h | 1 +
> 2 files changed, 20 insertions(+), 23 deletions(-)
>
> diff --git a/drivers/usb/cdns3/cdns3-gadget.c b/drivers/usb/cdns3/cdns3-gadget.c
> index 582bfec..5f51215 100644
> --- a/drivers/usb/cdns3/cdns3-gadget.c
> +++ b/drivers/usb/cdns3/cdns3-gadget.c
> @@ -59,6 +59,7 @@
> #include <linux/dma-mapping.h>
> #include <linux/usb/gadget.h>
> #include <linux/module.h>
> +#include <linux/dmapool.h>
> #include <linux/iopoll.h>
>
> #include "core.h"
> @@ -190,29 +191,13 @@ dma_addr_t cdns3_trb_virt_to_dma(struct cdns3_endpoint *priv_ep,
> return priv_ep->trb_pool_dma + offset;
> }
>
> -static int cdns3_ring_size(struct cdns3_endpoint *priv_ep)
> -{
> - switch (priv_ep->type) {
> - case USB_ENDPOINT_XFER_ISOC:
> - return TRB_ISO_RING_SIZE;
> - case USB_ENDPOINT_XFER_CONTROL:
> - return TRB_CTRL_RING_SIZE;
> - default:
> - if (priv_ep->use_streams)
> - return TRB_STREAM_RING_SIZE;
> - else
> - return TRB_RING_SIZE;
> - }
> -}
> -
> static void cdns3_free_trb_pool(struct cdns3_endpoint *priv_ep)
> {
> struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
>
> if (priv_ep->trb_pool) {
> - dma_free_coherent(priv_dev->sysdev,
> - cdns3_ring_size(priv_ep),
> - priv_ep->trb_pool, priv_ep->trb_pool_dma);
> + dma_pool_free(priv_dev->eps_dma_pool,
> + priv_ep->trb_pool, priv_ep->trb_pool_dma);
> priv_ep->trb_pool = NULL;
> }
> }
> @@ -226,7 +211,7 @@ static void cdns3_free_trb_pool(struct cdns3_endpoint *priv_ep)
> int cdns3_allocate_trb_pool(struct cdns3_endpoint *priv_ep)
> {
> struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
> - int ring_size = cdns3_ring_size(priv_ep);
> + int ring_size = TRB_RING_SIZE;
> int num_trbs = ring_size / TRB_SIZE;
> struct cdns3_trb *link_trb;
>
> @@ -234,10 +219,10 @@ int cdns3_allocate_trb_pool(struct cdns3_endpoint *priv_ep)
> cdns3_free_trb_pool(priv_ep);
>
> if (!priv_ep->trb_pool) {
> - priv_ep->trb_pool = dma_alloc_coherent(priv_dev->sysdev,
> - ring_size,
> - &priv_ep->trb_pool_dma,
> - GFP_DMA32 | GFP_ATOMIC);
> + priv_ep->trb_pool = dma_pool_alloc(priv_dev->eps_dma_pool,
> + GFP_DMA32 | GFP_ATOMIC,
> + &priv_ep->trb_pool_dma);
> +
> if (!priv_ep->trb_pool)
> return -ENOMEM;
>
> @@ -3113,6 +3098,7 @@ static void cdns3_gadget_exit(struct cdns *cdns)
>
> dma_free_coherent(priv_dev->sysdev, 8, priv_dev->setup_buf,
> priv_dev->setup_dma);
> + dma_pool_destroy(priv_dev->eps_dma_pool);
>
> kfree(priv_dev->zlp_buf);
> usb_put_gadget(&priv_dev->gadget);
> @@ -3185,6 +3171,14 @@ static int cdns3_gadget_start(struct cdns *cdns)
> /* initialize endpoint container */
> INIT_LIST_HEAD(&priv_dev->gadget.ep_list);
> INIT_LIST_HEAD(&priv_dev->aligned_buf_list);
> + priv_dev->eps_dma_pool = dma_pool_create("cdns3_eps_dma_pool",
> + priv_dev->sysdev,
> + TRB_RING_SIZE, 8, 0);
> + if (!priv_dev->eps_dma_pool) {
> + dev_err(priv_dev->dev, "Failed to create TRB dma pool\n");
> + ret = -ENOMEM;
> + goto err1;
> + }
>
> ret = cdns3_init_eps(priv_dev);
> if (ret) {
> @@ -3235,6 +3229,8 @@ static int cdns3_gadget_start(struct cdns *cdns)
> err2:
> cdns3_free_all_eps(priv_dev);
> err1:
> + dma_pool_destroy(priv_dev->eps_dma_pool);
> +
> usb_put_gadget(&priv_dev->gadget);
> cdns->gadget_dev = NULL;
> return ret;
> diff --git a/drivers/usb/cdns3/cdns3-gadget.h b/drivers/usb/cdns3/cdns3-gadget.h
> index 21fa461..ecf9b91 100644
> --- a/drivers/usb/cdns3/cdns3-gadget.h
> +++ b/drivers/usb/cdns3/cdns3-gadget.h
> @@ -1298,6 +1298,7 @@ struct cdns3_device {
>
> struct cdns3_usb_regs __iomem *regs;
>
> + struct dma_pool *eps_dma_pool;
> struct usb_ctrlrequest *setup_buf;
> dma_addr_t setup_dma;
> void *zlp_buf;
> --
> 1.7.1
>

--

Thanks,
Peter Chen

2021-03-15 06:13:17

by Sanket Parmar

[permalink] [raw]
Subject: RE: [PATCH 1/2] usb: cdns3: Use dma_pool_* api to alloc trb pool

Hi Peter,
>
> On 21-03-09 06:19:39, Sanket Parmar wrote:
> > Allocation of DMA coherent memory in atomic context using
> > dma_alloc_coherent() might fail on platforms with smaller
> > DMA region.
> >
> > To fix it, dma_alloc_coherent() is replaced with dma_pool
> > API to allocate a smaller chunk of DMA coherent memory for
> > TRB rings.
> >
> > Fixes: 7733f6c32e36 ("usb: cdns3: Add Cadence USB3 DRD Driver")
>
> The patch is ok, but I don't think it is a bug-fix, it is an
> improvement for smaller DMA region use case.
>
> I will apply it with deletion of above Fixes tag if you have no
> more opinion.
>

Please go ahead.

Thanks,
Sanket

> Peter
> > Reported-by: Aswath Govindraju <[email protected]>
> > Signed-off-by: Sanket Parmar <[email protected]>
> > ---
> > drivers/usb/cdns3/cdns3-gadget.c | 42 +++++++++++++++++---------------
> -----
> > drivers/usb/cdns3/cdns3-gadget.h | 1 +
> > 2 files changed, 20 insertions(+), 23 deletions(-)
> >
> > diff --git a/drivers/usb/cdns3/cdns3-gadget.c b/drivers/usb/cdns3/cdns3-
> gadget.c
> > index 582bfec..5f51215 100644
> > --- a/drivers/usb/cdns3/cdns3-gadget.c
> > +++ b/drivers/usb/cdns3/cdns3-gadget.c
> > @@ -59,6 +59,7 @@
> > #include <linux/dma-mapping.h>
> > #include <linux/usb/gadget.h>
> > #include <linux/module.h>
> > +#include <linux/dmapool.h>
> > #include <linux/iopoll.h>
> >
> > #include "core.h"
> > @@ -190,29 +191,13 @@ dma_addr_t cdns3_trb_virt_to_dma(struct
> cdns3_endpoint *priv_ep,
> > return priv_ep->trb_pool_dma + offset;
> > }
> >
> > -static int cdns3_ring_size(struct cdns3_endpoint *priv_ep)
> > -{
> > - switch (priv_ep->type) {
> > - case USB_ENDPOINT_XFER_ISOC:
> > - return TRB_ISO_RING_SIZE;
> > - case USB_ENDPOINT_XFER_CONTROL:
> > - return TRB_CTRL_RING_SIZE;
> > - default:
> > - if (priv_ep->use_streams)
> > - return TRB_STREAM_RING_SIZE;
> > - else
> > - return TRB_RING_SIZE;
> > - }
> > -}
> > -
> > static void cdns3_free_trb_pool(struct cdns3_endpoint *priv_ep)
> > {
> > struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
> >
> > if (priv_ep->trb_pool) {
> > - dma_free_coherent(priv_dev->sysdev,
> > - cdns3_ring_size(priv_ep),
> > - priv_ep->trb_pool, priv_ep-
> >trb_pool_dma);
> > + dma_pool_free(priv_dev->eps_dma_pool,
> > + priv_ep->trb_pool, priv_ep->trb_pool_dma);
> > priv_ep->trb_pool = NULL;
> > }
> > }
> > @@ -226,7 +211,7 @@ static void cdns3_free_trb_pool(struct
> cdns3_endpoint *priv_ep)
> > int cdns3_allocate_trb_pool(struct cdns3_endpoint *priv_ep)
> > {
> > struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
> > - int ring_size = cdns3_ring_size(priv_ep);
> > + int ring_size = TRB_RING_SIZE;
> > int num_trbs = ring_size / TRB_SIZE;
> > struct cdns3_trb *link_trb;
> >
> > @@ -234,10 +219,10 @@ int cdns3_allocate_trb_pool(struct
> cdns3_endpoint *priv_ep)
> > cdns3_free_trb_pool(priv_ep);
> >
> > if (!priv_ep->trb_pool) {
> > - priv_ep->trb_pool = dma_alloc_coherent(priv_dev->sysdev,
> > - ring_size,
> > - &priv_ep-
> >trb_pool_dma,
> > - GFP_DMA32 |
> GFP_ATOMIC);
> > + priv_ep->trb_pool = dma_pool_alloc(priv_dev-
> >eps_dma_pool,
> > + GFP_DMA32 |
> GFP_ATOMIC,
> > + &priv_ep->trb_pool_dma);
> > +
> > if (!priv_ep->trb_pool)
> > return -ENOMEM;
> >
> > @@ -3113,6 +3098,7 @@ static void cdns3_gadget_exit(struct cdns *cdns)
> >
> > dma_free_coherent(priv_dev->sysdev, 8, priv_dev->setup_buf,
> > priv_dev->setup_dma);
> > + dma_pool_destroy(priv_dev->eps_dma_pool);
> >
> > kfree(priv_dev->zlp_buf);
> > usb_put_gadget(&priv_dev->gadget);
> > @@ -3185,6 +3171,14 @@ static int cdns3_gadget_start(struct cdns *cdns)
> > /* initialize endpoint container */
> > INIT_LIST_HEAD(&priv_dev->gadget.ep_list);
> > INIT_LIST_HEAD(&priv_dev->aligned_buf_list);
> > + priv_dev->eps_dma_pool =
> dma_pool_create("cdns3_eps_dma_pool",
> > + priv_dev->sysdev,
> > + TRB_RING_SIZE, 8, 0);
> > + if (!priv_dev->eps_dma_pool) {
> > + dev_err(priv_dev->dev, "Failed to create TRB dma pool\n");
> > + ret = -ENOMEM;
> > + goto err1;
> > + }
> >
> > ret = cdns3_init_eps(priv_dev);
> > if (ret) {
> > @@ -3235,6 +3229,8 @@ static int cdns3_gadget_start(struct cdns *cdns)
> > err2:
> > cdns3_free_all_eps(priv_dev);
> > err1:
> > + dma_pool_destroy(priv_dev->eps_dma_pool);
> > +
> > usb_put_gadget(&priv_dev->gadget);
> > cdns->gadget_dev = NULL;
> > return ret;
> > diff --git a/drivers/usb/cdns3/cdns3-gadget.h b/drivers/usb/cdns3/cdns3-
> gadget.h
> > index 21fa461..ecf9b91 100644
> > --- a/drivers/usb/cdns3/cdns3-gadget.h
> > +++ b/drivers/usb/cdns3/cdns3-gadget.h
> > @@ -1298,6 +1298,7 @@ struct cdns3_device {
> >
> > struct cdns3_usb_regs __iomem *regs;
> >
> > + struct dma_pool *eps_dma_pool;
> > struct usb_ctrlrequest *setup_buf;
> > dma_addr_t setup_dma;
> > void *zlp_buf;
> > --
> > 1.7.1
> >
>
> --
>
> Thanks,
> Peter Chen