2022-09-30 11:09:35

by Moudy Ho (何宗原)

[permalink] [raw]
Subject: [PATCH v1 0/2] Fix error handling for MDP3

Hi,
This series fixes some error handling in MDP3 in response to the bug report
submitted by Dan Carpenter (Message ID = YxB1E00e8D6P4W2e@kili)

Moudy Ho (2):
media: platform: mtk-mdp3: fix error handling in mdp_cmdq_send()
media: platform: mtk-mdp3: fix error handling about components
clock_on

.../platform/mediatek/mdp3/mtk-mdp3-cmdq.c | 51 ++++++++++---------
.../platform/mediatek/mdp3/mtk-mdp3-comp.c | 24 ++++++---
2 files changed, 45 insertions(+), 30 deletions(-)

--
2.18.0


2022-09-30 11:15:43

by Moudy Ho (何宗原)

[permalink] [raw]
Subject: [PATCH v1 1/2] media: platform: mtk-mdp3: fix error handling in mdp_cmdq_send()

Increase and refine the goto label in mdp_cmdq_send() to avoid
double free and facilitate traceability.
Also, remove redundant work queue event in blocking function
mdp_cmdq_send().

Fixes: 61890ccaefaf ("media: platform: mtk-mdp3: add MediaTek MDP3 driver")
Signed-off-by: Moudy Ho <[email protected]>
---
.../platform/mediatek/mdp3/mtk-mdp3-cmdq.c | 47 ++++++++++---------
1 file changed, 26 insertions(+), 21 deletions(-)

diff --git a/drivers/media/platform/mediatek/mdp3/mtk-mdp3-cmdq.c b/drivers/media/platform/mediatek/mdp3/mtk-mdp3-cmdq.c
index 86c054600a08..e194dec8050a 100644
--- a/drivers/media/platform/mediatek/mdp3/mtk-mdp3-cmdq.c
+++ b/drivers/media/platform/mediatek/mdp3/mtk-mdp3-cmdq.c
@@ -252,10 +252,9 @@ static int mdp_cmdq_pkt_create(struct cmdq_client *client, struct cmdq_pkt *pkt,
dma_addr_t dma_addr;

pkt->va_base = kzalloc(size, GFP_KERNEL);
- if (!pkt->va_base) {
- kfree(pkt);
+ if (!pkt->va_base)
return -ENOMEM;
- }
+
pkt->buf_size = size;
pkt->cl = (void *)client;

@@ -368,25 +367,30 @@ int mdp_cmdq_send(struct mdp_dev *mdp, struct mdp_cmdq_param *param)
cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
if (!cmd) {
ret = -ENOMEM;
- goto err_cmdq_data;
+ goto err_cancel_job;
}

- if (mdp_cmdq_pkt_create(mdp->cmdq_clt, &cmd->pkt, SZ_16K)) {
- ret = -ENOMEM;
- goto err_cmdq_data;
- }
+ ret = mdp_cmdq_pkt_create(mdp->cmdq_clt, &cmd->pkt, SZ_16K);
+ if (ret)
+ goto err_free_cmd;

comps = kcalloc(param->config->num_components, sizeof(*comps),
GFP_KERNEL);
if (!comps) {
ret = -ENOMEM;
- goto err_cmdq_data;
+ goto err_destroy_pkt;
}

path = kzalloc(sizeof(*path), GFP_KERNEL);
if (!path) {
ret = -ENOMEM;
- goto err_cmdq_data;
+ goto err_free_comps;
+ }
+
+ ret = mtk_mutex_prepare(mdp->mdp_mutex[MDP_PIPE_RDMA0]);
+ if (ret) {
+ dev_err(dev, "Fail to enable mutex clk\n");
+ goto err_free_path;
}

path->mdp_dev = mdp;
@@ -406,15 +410,13 @@ int mdp_cmdq_send(struct mdp_dev *mdp, struct mdp_cmdq_param *param)
ret = mdp_path_ctx_init(mdp, path);
if (ret) {
dev_err(dev, "mdp_path_ctx_init error\n");
- goto err_cmdq_data;
+ goto err_free_path;
}

- mtk_mutex_prepare(mdp->mdp_mutex[MDP_PIPE_RDMA0]);
-
ret = mdp_path_config(mdp, cmd, path);
if (ret) {
dev_err(dev, "mdp_path_config error\n");
- goto err_cmdq_data;
+ goto err_free_path;
}
cmdq_pkt_finalize(&cmd->pkt);

@@ -433,7 +435,7 @@ int mdp_cmdq_send(struct mdp_dev *mdp, struct mdp_cmdq_param *param)
ret = mdp_comp_clocks_on(&mdp->pdev->dev, cmd->comps, cmd->num_comps);
if (ret) {
dev_err(dev, "comp %d failed to enable clock!\n", ret);
- goto err_clock_off;
+ goto err_free_path;
}

dma_sync_single_for_device(mdp->cmdq_clt->chan->mbox->dev,
@@ -450,17 +452,20 @@ int mdp_cmdq_send(struct mdp_dev *mdp, struct mdp_cmdq_param *param)
return 0;

err_clock_off:
- mtk_mutex_unprepare(mdp->mdp_mutex[MDP_PIPE_RDMA0]);
mdp_comp_clocks_off(&mdp->pdev->dev, cmd->comps,
cmd->num_comps);
-err_cmdq_data:
+err_free_path:
+ mtk_mutex_unprepare(mdp->mdp_mutex[MDP_PIPE_RDMA0]);
kfree(path);
- atomic_dec(&mdp->job_count);
- wake_up(&mdp->callback_wq);
- if (cmd && cmd->pkt.buf_size > 0)
- mdp_cmdq_pkt_destroy(&cmd->pkt);
+err_free_comps:
kfree(comps);
+err_destroy_pkt:
+ mdp_cmdq_pkt_destroy(&cmd->pkt);
+err_free_cmd:
kfree(cmd);
+err_cancel_job:
+ atomic_dec(&mdp->job_count);
+
return ret;
}
EXPORT_SYMBOL_GPL(mdp_cmdq_send);
--
2.18.0

2022-09-30 11:29:21

by Moudy Ho (何宗原)

[permalink] [raw]
Subject: [PATCH v1 2/2] media: platform: mtk-mdp3: fix error handling about components clock_on

Add goto statement in mdp_comp_clock_on() to avoid error code not being
propagated or returning positive values.
This change also performs a well-timed clock_off when an error occurs, and
reduces unnecessary error logging in mdp_cmdq_send().

Fixes: 61890ccaefaf ("media: platform: mtk-mdp3: add MediaTek MDP3 driver")
Signed-off-by: Moudy Ho <[email protected]>
---
.../platform/mediatek/mdp3/mtk-mdp3-cmdq.c | 4 +---
.../platform/mediatek/mdp3/mtk-mdp3-comp.c | 24 ++++++++++++++-----
2 files changed, 19 insertions(+), 9 deletions(-)

diff --git a/drivers/media/platform/mediatek/mdp3/mtk-mdp3-cmdq.c b/drivers/media/platform/mediatek/mdp3/mtk-mdp3-cmdq.c
index e194dec8050a..124c1b96e96b 100644
--- a/drivers/media/platform/mediatek/mdp3/mtk-mdp3-cmdq.c
+++ b/drivers/media/platform/mediatek/mdp3/mtk-mdp3-cmdq.c
@@ -433,10 +433,8 @@ int mdp_cmdq_send(struct mdp_dev *mdp, struct mdp_cmdq_param *param)
cmd->mdp_ctx = param->mdp_ctx;

ret = mdp_comp_clocks_on(&mdp->pdev->dev, cmd->comps, cmd->num_comps);
- if (ret) {
- dev_err(dev, "comp %d failed to enable clock!\n", ret);
+ if (ret)
goto err_free_path;
- }

dma_sync_single_for_device(mdp->cmdq_clt->chan->mbox->dev,
cmd->pkt.pa_base, cmd->pkt.cmd_buf_size,
diff --git a/drivers/media/platform/mediatek/mdp3/mtk-mdp3-comp.c b/drivers/media/platform/mediatek/mdp3/mtk-mdp3-comp.c
index d3eaf8884412..fe6a39315e88 100644
--- a/drivers/media/platform/mediatek/mdp3/mtk-mdp3-comp.c
+++ b/drivers/media/platform/mediatek/mdp3/mtk-mdp3-comp.c
@@ -699,12 +699,22 @@ int mdp_comp_clock_on(struct device *dev, struct mdp_comp *comp)
dev_err(dev,
"Failed to enable clk %d. type:%d id:%d\n",
i, comp->type, comp->id);
- pm_runtime_put(comp->comp_dev);
- return ret;
+ goto err_unwind;
}
}

return 0;
+
+err_unwind:
+ while (--i >= 0) {
+ if (IS_ERR_OR_NULL(comp->clks[i]))
+ continue;
+ clk_disable_unprepare(comp->clks[i]);
+ }
+ if (comp->comp_dev)
+ pm_runtime_put_sync(comp->comp_dev);
+
+ return ret;
}

void mdp_comp_clock_off(struct device *dev, struct mdp_comp *comp)
@@ -723,11 +733,13 @@ void mdp_comp_clock_off(struct device *dev, struct mdp_comp *comp)

int mdp_comp_clocks_on(struct device *dev, struct mdp_comp *comps, int num)
{
- int i;
+ int i, ret;

- for (i = 0; i < num; i++)
- if (mdp_comp_clock_on(dev, &comps[i]) != 0)
- return ++i;
+ for (i = 0; i < num; i++) {
+ ret = mdp_comp_clock_on(dev, &comps[i]);
+ if (ret)
+ return ret;
+ }

return 0;
}
--
2.18.0

Subject: Re: [PATCH v1 1/2] media: platform: mtk-mdp3: fix error handling in mdp_cmdq_send()

Il 30/09/22 12:23, Moudy Ho ha scritto:
> Increase and refine the goto label in mdp_cmdq_send() to avoid
> double free and facilitate traceability.
> Also, remove redundant work queue event in blocking function
> mdp_cmdq_send().
>
> Fixes: 61890ccaefaf ("media: platform: mtk-mdp3: add MediaTek MDP3 driver")
> Signed-off-by: Moudy Ho <[email protected]>

Reviewed-by: AngeloGioacchino Del Regno <[email protected]>


Subject: Re: [PATCH v1 2/2] media: platform: mtk-mdp3: fix error handling about components clock_on

Il 30/09/22 12:23, Moudy Ho ha scritto:
> Add goto statement in mdp_comp_clock_on() to avoid error code not being
> propagated or returning positive values.
> This change also performs a well-timed clock_off when an error occurs, and
> reduces unnecessary error logging in mdp_cmdq_send().
>
> Fixes: 61890ccaefaf ("media: platform: mtk-mdp3: add MediaTek MDP3 driver")
> Signed-off-by: Moudy Ho <[email protected]>
> ---
> .../platform/mediatek/mdp3/mtk-mdp3-cmdq.c | 4 +---
> .../platform/mediatek/mdp3/mtk-mdp3-comp.c | 24 ++++++++++++++-----
> 2 files changed, 19 insertions(+), 9 deletions(-)
>
> diff --git a/drivers/media/platform/mediatek/mdp3/mtk-mdp3-cmdq.c b/drivers/media/platform/mediatek/mdp3/mtk-mdp3-cmdq.c
> index e194dec8050a..124c1b96e96b 100644
> --- a/drivers/media/platform/mediatek/mdp3/mtk-mdp3-cmdq.c
> +++ b/drivers/media/platform/mediatek/mdp3/mtk-mdp3-cmdq.c
> @@ -433,10 +433,8 @@ int mdp_cmdq_send(struct mdp_dev *mdp, struct mdp_cmdq_param *param)
> cmd->mdp_ctx = param->mdp_ctx;
>
> ret = mdp_comp_clocks_on(&mdp->pdev->dev, cmd->comps, cmd->num_comps);
> - if (ret) {
> - dev_err(dev, "comp %d failed to enable clock!\n", ret);
> + if (ret)
> goto err_free_path;
> - }
>
> dma_sync_single_for_device(mdp->cmdq_clt->chan->mbox->dev,
> cmd->pkt.pa_base, cmd->pkt.cmd_buf_size,
> diff --git a/drivers/media/platform/mediatek/mdp3/mtk-mdp3-comp.c b/drivers/media/platform/mediatek/mdp3/mtk-mdp3-comp.c
> index d3eaf8884412..fe6a39315e88 100644
> --- a/drivers/media/platform/mediatek/mdp3/mtk-mdp3-comp.c
> +++ b/drivers/media/platform/mediatek/mdp3/mtk-mdp3-comp.c
> @@ -699,12 +699,22 @@ int mdp_comp_clock_on(struct device *dev, struct mdp_comp *comp)
> dev_err(dev,
> "Failed to enable clk %d. type:%d id:%d\n",
> i, comp->type, comp->id);
> - pm_runtime_put(comp->comp_dev);
> - return ret;
> + goto err_unwind;
> }
> }
>
> return 0;
> +
> +err_unwind:

For this label to be clearer, I would rename it as `err_revert`, or
`clocks_off`, or even simply `err`, as the `unwind` word may create
some confusion here.

> + while (--i >= 0) {
> + if (IS_ERR_OR_NULL(comp->clks[i]))
> + continue;
> + clk_disable_unprepare(comp->clks[i]);
> + }
> + if (comp->comp_dev)
> + pm_runtime_put_sync(comp->comp_dev);
> +
> + return ret;
> }
>
> void mdp_comp_clock_off(struct device *dev, struct mdp_comp *comp)
> @@ -723,11 +733,13 @@ void mdp_comp_clock_off(struct device *dev, struct mdp_comp *comp)
>
> int mdp_comp_clocks_on(struct device *dev, struct mdp_comp *comps, int num)
> {
> - int i;
> + int i, ret;
>
> - for (i = 0; i < num; i++)
> - if (mdp_comp_clock_on(dev, &comps[i]) != 0)
> - return ++i;
> + for (i = 0; i < num; i++) {
> + ret = mdp_comp_clock_on(dev, &comps[i]);
> + if (ret)
> + return ret;
> + }
>
> return 0;
> }
>

2022-10-03 03:00:18

by Moudy Ho (何宗原)

[permalink] [raw]
Subject: Re: [PATCH v1 2/2] media: platform: mtk-mdp3: fix error handling about components clock_on

On Fri, 2022-09-30 at 14:12 +0200, AngeloGioacchino Del Regno wrote:
> Il 30/09/22 12:23, Moudy Ho ha scritto:
> > Add goto statement in mdp_comp_clock_on() to avoid error code not
> > being
> > propagated or returning positive values.
> > This change also performs a well-timed clock_off when an error
> > occurs, and
> > reduces unnecessary error logging in mdp_cmdq_send().
> >
> > Fixes: 61890ccaefaf ("media: platform: mtk-mdp3: add MediaTek MDP3
> > driver")
> > Signed-off-by: Moudy Ho <[email protected]>
> > ---
> > .../platform/mediatek/mdp3/mtk-mdp3-cmdq.c | 4 +---
> > .../platform/mediatek/mdp3/mtk-mdp3-comp.c | 24
> > ++++++++++++++-----
> > 2 files changed, 19 insertions(+), 9 deletions(-)
> >
> > diff --git a/drivers/media/platform/mediatek/mdp3/mtk-mdp3-cmdq.c
> > b/drivers/media/platform/mediatek/mdp3/mtk-mdp3-cmdq.c
> > index e194dec8050a..124c1b96e96b 100644
> > --- a/drivers/media/platform/mediatek/mdp3/mtk-mdp3-cmdq.c
> > +++ b/drivers/media/platform/mediatek/mdp3/mtk-mdp3-cmdq.c
> > @@ -433,10 +433,8 @@ int mdp_cmdq_send(struct mdp_dev *mdp, struct
> > mdp_cmdq_param *param)
> > cmd->mdp_ctx = param->mdp_ctx;
> >
> > ret = mdp_comp_clocks_on(&mdp->pdev->dev, cmd->comps, cmd-
> > >num_comps);
> > - if (ret) {
> > - dev_err(dev, "comp %d failed to enable clock!\n", ret);
> > + if (ret)
> > goto err_free_path;
> > - }
> >
> > dma_sync_single_for_device(mdp->cmdq_clt->chan->mbox->dev,
> > cmd->pkt.pa_base, cmd-
> > >pkt.cmd_buf_size,
> > diff --git a/drivers/media/platform/mediatek/mdp3/mtk-mdp3-comp.c
> > b/drivers/media/platform/mediatek/mdp3/mtk-mdp3-comp.c
> > index d3eaf8884412..fe6a39315e88 100644
> > --- a/drivers/media/platform/mediatek/mdp3/mtk-mdp3-comp.c
> > +++ b/drivers/media/platform/mediatek/mdp3/mtk-mdp3-comp.c
> > @@ -699,12 +699,22 @@ int mdp_comp_clock_on(struct device *dev,
> > struct mdp_comp *comp)
> > dev_err(dev,
> > "Failed to enable clk %d. type:%d
> > id:%d\n",
> > i, comp->type, comp->id);
> > - pm_runtime_put(comp->comp_dev);
> > - return ret;
> > + goto err_unwind;
> > }
> > }
> >
> > return 0;
> > +
> > +err_unwind:
>
> For this label to be clearer, I would rename it as `err_revert`, or
> `clocks_off`, or even simply `err`, as the `unwind` word may create
> some confusion here.
>

Hi Angelo,

Thanks for the suggestion to rename to "err_revert" and make the flow
more readable.
I will correct it in the next version.

Regards,
Moudy

> > + while (--i >= 0) {
> > + if (IS_ERR_OR_NULL(comp->clks[i]))
> > + continue;
> > + clk_disable_unprepare(comp->clks[i]);
> > + }
> > + if (comp->comp_dev)
> > + pm_runtime_put_sync(comp->comp_dev);
> > +
> > + return ret;
> > }
> >
> > void mdp_comp_clock_off(struct device *dev, struct mdp_comp
> > *comp)
> > @@ -723,11 +733,13 @@ void mdp_comp_clock_off(struct device *dev,
> > struct mdp_comp *comp)
> >
> > int mdp_comp_clocks_on(struct device *dev, struct mdp_comp
> > *comps, int num)
> > {
> > - int i;
> > + int i, ret;
> >
> > - for (i = 0; i < num; i++)
> > - if (mdp_comp_clock_on(dev, &comps[i]) != 0)
> > - return ++i;
> > + for (i = 0; i < num; i++) {
> > + ret = mdp_comp_clock_on(dev, &comps[i]);
> > + if (ret)
> > + return ret;
> > + }
> >
> > return 0;
> > }
> >