driverdev-devel.linuxdriverproject.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v4 00/79] Address some issues with PM runtime at media subsystem
@ 2021-04-28 14:51 Mauro Carvalho Chehab
  2021-04-28 14:51 ` [PATCH v4 20/79] staging: media: rkvdec: fix pm_runtime_get_sync() usage count Mauro Carvalho Chehab
                   ` (9 more replies)
  0 siblings, 10 replies; 28+ messages in thread
From: Mauro Carvalho Chehab @ 2021-04-28 14:51 UTC (permalink / raw)
  Cc: Shawn Tu, Ricardo Ribalda, Dafna Hirschfeld, Heiko Stuebner,
	linuxarm, Todor Tomov, Bjorn Andersson, Andrzej Hajda, Lad,
	Prabhakar, Thierry Reding, Pengutronix Kernel Team,
	Dmitry Osipenko, linux-stm32, Andrzej Pietrasiewicz, Leon Luo,
	Paul Kocialkowski, Mauro Carvalho Chehab, Dave Stevenson,
	Mauro Carvalho Chehab, Matt Ranostay, Krzysztof Kozlowski,
	Jonathan Hunter, linux-rockchip, Chen-Yu Tsai, Andy Gross,
	Matthias Brugger, Dongchun Zhu, Sakari Ailus, Bingbu Cao,
	Marek Szyprowski, Shunqian Zheng, Tianshu Qiu, NXP Linux Team,
	Philipp Zabel, devel, Jacopo Mondi, Sylwester Nawrocki,
	linux-tegra, Alexandre Torgue, Wenyou Yang,
	Manivannan Sadhasivam, linux-arm-msm, Sascha Hauer,
	Steve Longerbeam, linux-media, Maxime Ripard, Stanimir Varbanov,
	Benoit Parrot, Helen Koike, linux-samsung-soc, linux-mediatek,
	Jacek Anaszewski, mauro.chehab, Sylwester Nawrocki,
	Paul J. Murphy, Ezequiel Garcia, Daniele Alessandrelli,
	Chiranjeevi Rapolu, linux-arm-kernel, Jacob Chen, Jernej Skrabec,
	Hyungwoo Yang, linux-kernel, Robert Foss, Dan Scally,
	Sowjanya Komatineni, Maxime Coquelin, linux-renesas-soc,
	Yong Zhi, Shawn Guo

During the review of the patches from unm.edu, one of the patterns
I noticed is the amount of patches trying to fix pm_runtime_get_sync()
calls.

After analyzing the feedback from version 1 of this series, I noticed
a few other weird behaviors at the PM runtime resume code. So, this
series start addressing some bugs and issues at the current code.
Then, it gets rid of pm_runtime_get_sync() at the media subsystem
(with 2 exceptions).

It should be noticed that
Commit dd8088d5a896 ("PM: runtime: Add pm_runtime_resume_and_get to deal with usage counter")
added a new method to does a pm_runtime get, which increments
the usage count only on success.

The rationale of getting rid of pm_runtime_get_sync() is:

1. despite its name, this is actually a PM runtime resume call,
   but some developers didn't seem to realize that, as I got this
   pattern on some drivers:

        pm_runtime_get_sync(&client->dev);
        pm_runtime_disable(&client->dev);
        pm_runtime_set_suspended(&client->dev);
        pm_runtime_put_noidle(&client->dev);

   It makes no sense to resume PM just to suspend it again ;-)

2. Usual *_get() methods only increment their use count on success,
   but pm_runtime_get_sync() increments it unconditionally. Due to
   that, several drivers were mistakenly not calling
   pm_runtime_put_noidle() when it fails;

3. The name of the new variant is a lot clearer:
	pm_runtime_resume_and_get()
    As its same clearly says that this is a PM runtime resume function,
    that also increments the usage counter on success;

4. Consistency: we did similar changes subsystem wide with
   for instance strlcpy() and strcpy() that got replaced by
   strscpy(). Having all drivers using the same known-to-be-safe
   methods is a good thing;

5. Prevent newer drivers to copy-and-paste a code that it would
   be easier to break if they don't truly understand what's behind
   the scenes.

This series replace places  pm_runtime_get_sync(), by calling
pm_runtime_resume_and_get() instead.

This should help to avoid future mistakes like that, as people
tend to use the existing drivers as examples for newer ones.

compile-tested only.

Patches 1 to 7 fix some issues that already exists at the current
PM runtime code;

patches 8 to 20 fix some usage_count problems that still exists
at the media subsystem;

patches 21 to 78 repaces pm_runtime_get_sync() by 
pm_runtime_resume_and_get();

Patch 79 (and a hunk on patch 78) documents the two exceptions
where pm_runtime_get_sync() will still be used for now.

---

v4:
    - Added a couple of additional fixes at existing PM runtime code;
    - Some patches are now more conservative in order to avoid causing
     regressions.
v3:
    - fix a compilation error;
v2:
    - addressed pointed issues and fixed a few other PM issues.


Mauro Carvalho Chehab (79):
  media: venus: fix PM runtime logic at venus_sys_error_handler()
  media: s6p_cec: decrement usage count if disabled
  media: i2c: ccs-core: return the right error code at suspend
  media: i2c: ov7740: don't resume at remove time
  media: i2c: video-i2c: don't resume at remove time
  media: i2c: imx334: fix the pm runtime get logic
  media: exynos-gsc: don't resume at remove time
  media: atmel: properly get pm_runtime
  media: marvel-ccic: fix some issues when getting pm_runtime
  media: mdk-mdp: fix pm_runtime_get_sync() usage count
  media: rcar_fdp1: fix pm_runtime_get_sync() usage count
  media: renesas-ceu: Properly check for PM errors
  media: s5p: fix pm_runtime_get_sync() usage count
  media: am437x: fix pm_runtime_get_sync() usage count
  media: sh_vou: fix pm_runtime_get_sync() usage count
  media: mtk-vcodec: fix pm_runtime_get_sync() usage count
  media: s5p-jpeg: fix pm_runtime_get_sync() usage count
  media: sti/delta: fix pm_runtime_get_sync() usage count
  media: sunxi: fix pm_runtime_get_sync() usage count
  staging: media: rkvdec: fix pm_runtime_get_sync() usage count
  staging: media: atomisp: use pm_runtime_resume_and_get()
  staging: media: imx7-mipi-csis: use pm_runtime_resume_and_get()
  staging: media: ipu3: use pm_runtime_resume_and_get()
  staging: media: cedrus_video: use pm_runtime_resume_and_get()
  staging: media: tegra-vde: use pm_runtime_resume_and_get()
  staging: media: tegra-video: use pm_runtime_resume_and_get()
  media: i2c: ak7375: use pm_runtime_resume_and_get()
  media: i2c: ccs-core: use pm_runtime_resume_and_get()
  media: i2c: dw9714: use pm_runtime_resume_and_get()
  media: i2c: dw9768: use pm_runtime_resume_and_get()
  media: i2c: dw9807-vcm: use pm_runtime_resume_and_get()
  media: i2c: hi556: use pm_runtime_resume_and_get()
  media: i2c: imx214: use pm_runtime_resume_and_get()
  media: i2c: imx219: use pm_runtime_resume_and_get()
  media: i2c: imx258: use pm_runtime_resume_and_get()
  media: i2c: imx274: use pm_runtime_resume_and_get()
  media: i2c: imx290: use pm_runtime_resume_and_get()
  media: i2c: imx319: use pm_runtime_resume_and_get()
  media: i2c: imx355: use pm_runtime_resume_and_get()
  media: i2c: mt9m001: use pm_runtime_resume_and_get()
  media: i2c: ov02a10: use pm_runtime_resume_and_get()
  media: i2c: ov13858: use pm_runtime_resume_and_get()
  media: i2c: ov2659: use pm_runtime_resume_and_get()
  media: i2c: ov2685: use pm_runtime_resume_and_get()
  media: i2c: ov2740: use pm_runtime_resume_and_get()
  media: i2c: ov5647: use pm_runtime_resume_and_get()
  media: i2c: ov5648: use pm_runtime_resume_and_get()
  media: i2c: ov5670: use pm_runtime_resume_and_get()
  media: i2c: ov5675: use pm_runtime_resume_and_get()
  media: i2c: ov5695: use pm_runtime_resume_and_get()
  media: i2c: ov7740: use pm_runtime_resume_and_get()
  media: i2c: ov8856: use pm_runtime_resume_and_get()
  media: i2c: ov8865: use pm_runtime_resume_and_get()
  media: i2c: ov9734: use pm_runtime_resume_and_get()
  media: i2c: tvp5150: use pm_runtime_resume_and_get()
  media: i2c: video-i2c: use pm_runtime_resume_and_get()
  media: rockchip/rga: use pm_runtime_resume_and_get()
  media: sti/hva: use pm_runtime_resume_and_get()
  media: sti/bdisp: use pm_runtime_resume_and_get()
  media: ipu3: use pm_runtime_resume_and_get()
  media: coda: use pm_runtime_resume_and_get()
  media: exynos4-is: use pm_runtime_resume_and_get()
  media: exynos-gsc: use pm_runtime_resume_and_get()
  media: mtk-jpeg: use pm_runtime_resume_and_get()
  media: camss: use pm_runtime_resume_and_get()
  media: venus: use pm_runtime_resume_and_get()
  media: venus: vdec: use pm_runtime_resume_and_get()
  media: venus: venc: use pm_runtime_resume_and_get()
  media: rcar-fcp: use pm_runtime_resume_and_get()
  media: rkisp1: use pm_runtime_resume_and_get()
  media: s3c-camif: use pm_runtime_resume_and_get()
  media: s5p-mfc: use pm_runtime_resume_and_get()
  media: stm32: use pm_runtime_resume_and_get()
  media: sunxi: use pm_runtime_resume_and_get()
  media: ti-vpe: use pm_runtime_resume_and_get()
  media: vsp1: use pm_runtime_resume_and_get()
  media: rcar-vin: use pm_runtime_resume_and_get()
  media: hantro: use pm_runtime_resume_and_get()
  media: hantro: do a PM resume earlier

 drivers/media/cec/platform/s5p/s5p_cec.c      |  7 +++--
 drivers/media/i2c/ak7375.c                    | 10 +------
 drivers/media/i2c/ccs/ccs-core.c              | 18 +++++-------
 drivers/media/i2c/dw9714.c                    | 10 +------
 drivers/media/i2c/dw9768.c                    | 10 +------
 drivers/media/i2c/dw9807-vcm.c                | 10 +------
 drivers/media/i2c/hi556.c                     |  3 +-
 drivers/media/i2c/imx214.c                    |  6 ++--
 drivers/media/i2c/imx219.c                    |  6 ++--
 drivers/media/i2c/imx258.c                    |  6 ++--
 drivers/media/i2c/imx274.c                    |  3 +-
 drivers/media/i2c/imx290.c                    |  6 ++--
 drivers/media/i2c/imx319.c                    |  6 ++--
 drivers/media/i2c/imx334.c                    |  7 +++--
 drivers/media/i2c/imx355.c                    |  6 ++--
 drivers/media/i2c/mt9m001.c                   |  9 ++++--
 drivers/media/i2c/ov02a10.c                   |  6 ++--
 drivers/media/i2c/ov13858.c                   |  6 ++--
 drivers/media/i2c/ov2659.c                    |  6 ++--
 drivers/media/i2c/ov2685.c                    |  7 ++---
 drivers/media/i2c/ov2740.c                    |  6 ++--
 drivers/media/i2c/ov5647.c                    |  9 +++---
 drivers/media/i2c/ov5648.c                    |  6 ++--
 drivers/media/i2c/ov5670.c                    |  6 ++--
 drivers/media/i2c/ov5675.c                    |  3 +-
 drivers/media/i2c/ov5695.c                    |  6 ++--
 drivers/media/i2c/ov7740.c                    |  8 ++---
 drivers/media/i2c/ov8856.c                    |  3 +-
 drivers/media/i2c/ov8865.c                    |  6 ++--
 drivers/media/i2c/ov9734.c                    |  3 +-
 drivers/media/i2c/tvp5150.c                   | 16 ++--------
 drivers/media/i2c/video-i2c.c                 | 14 +++------
 drivers/media/pci/intel/ipu3/ipu3-cio2-main.c |  3 +-
 drivers/media/platform/am437x/am437x-vpfe.c   | 22 ++++++++++----
 drivers/media/platform/atmel/atmel-isc-base.c | 27 ++++++++++++-----
 drivers/media/platform/atmel/atmel-isi.c      | 19 +++++++++---
 drivers/media/platform/coda/coda-common.c     |  7 +++--
 drivers/media/platform/exynos-gsc/gsc-core.c  | 11 +++----
 drivers/media/platform/exynos-gsc/gsc-m2m.c   |  2 +-
 .../media/platform/exynos4-is/fimc-capture.c  |  6 ++--
 drivers/media/platform/exynos4-is/fimc-is.c   |  4 +--
 .../platform/exynos4-is/fimc-isp-video.c      |  3 +-
 drivers/media/platform/exynos4-is/fimc-isp.c  |  7 ++---
 drivers/media/platform/exynos4-is/fimc-lite.c |  5 ++--
 drivers/media/platform/exynos4-is/fimc-m2m.c  |  2 +-
 drivers/media/platform/exynos4-is/media-dev.c |  8 ++---
 drivers/media/platform/exynos4-is/mipi-csis.c |  8 ++---
 .../media/platform/marvell-ccic/mcam-core.c   |  9 ++++--
 .../media/platform/mtk-jpeg/mtk_jpeg_core.c   |  4 +--
 drivers/media/platform/mtk-mdp/mtk_mdp_m2m.c  |  6 ++--
 .../platform/mtk-vcodec/mtk_vcodec_dec_pm.c   |  4 +--
 .../media/platform/qcom/camss/camss-csid.c    |  6 ++--
 .../media/platform/qcom/camss/camss-csiphy.c  |  6 ++--
 .../media/platform/qcom/camss/camss-ispif.c   |  6 ++--
 drivers/media/platform/qcom/camss/camss-vfe.c |  5 ++--
 drivers/media/platform/qcom/venus/core.c      | 28 +++++++++++-------
 .../media/platform/qcom/venus/pm_helpers.c    | 10 +++----
 drivers/media/platform/qcom/venus/vdec.c      |  4 +--
 drivers/media/platform/qcom/venus/venc.c      |  5 ++--
 drivers/media/platform/rcar-fcp.c             |  6 ++--
 drivers/media/platform/rcar-vin/rcar-csi2.c   | 15 ++++++++--
 drivers/media/platform/rcar-vin/rcar-dma.c    |  6 ++--
 drivers/media/platform/rcar-vin/rcar-v4l2.c   |  6 ++--
 drivers/media/platform/rcar_fdp1.c            | 12 ++++++--
 drivers/media/platform/renesas-ceu.c          |  4 +--
 drivers/media/platform/rockchip/rga/rga-buf.c |  3 +-
 drivers/media/platform/rockchip/rga/rga.c     |  4 ++-
 .../platform/rockchip/rkisp1/rkisp1-capture.c |  3 +-
 .../media/platform/s3c-camif/camif-capture.c  |  2 +-
 drivers/media/platform/s3c-camif/camif-core.c |  5 ++--
 drivers/media/platform/s5p-jpeg/jpeg-core.c   |  2 +-
 drivers/media/platform/s5p-mfc/s5p_mfc_pm.c   |  6 ++--
 drivers/media/platform/sh_vou.c               |  6 +++-
 drivers/media/platform/sti/bdisp/bdisp-v4l2.c |  7 +++--
 drivers/media/platform/sti/delta/delta-v4l2.c |  4 +--
 drivers/media/platform/sti/hva/hva-hw.c       | 17 ++++++-----
 drivers/media/platform/stm32/stm32-dcmi.c     |  5 ++--
 .../platform/sunxi/sun4i-csi/sun4i_v4l2.c     |  6 ++--
 .../sunxi/sun8i-rotate/sun8i_rotate.c         |  2 +-
 drivers/media/platform/ti-vpe/cal-video.c     |  4 ++-
 drivers/media/platform/ti-vpe/cal.c           |  8 +++--
 drivers/media/platform/ti-vpe/vpe.c           |  4 +--
 drivers/media/platform/vsp1/vsp1_drv.c        |  6 ++--
 .../staging/media/atomisp/pci/atomisp_fops.c  |  6 ++--
 drivers/staging/media/hantro/hantro_drv.c     | 29 ++++++++++++-------
 drivers/staging/media/imx/imx7-mipi-csis.c    |  7 ++---
 drivers/staging/media/ipu3/ipu3.c             |  3 +-
 drivers/staging/media/rkvdec/rkvdec.c         |  2 +-
 .../staging/media/sunxi/cedrus/cedrus_video.c |  6 ++--
 drivers/staging/media/tegra-vde/vde.c         | 19 ++++++++++--
 drivers/staging/media/tegra-video/csi.c       |  3 +-
 drivers/staging/media/tegra-video/vi.c        |  3 +-
 92 files changed, 335 insertions(+), 347 deletions(-)

-- 
2.30.2


_______________________________________________
devel mailing list
devel@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel

^ permalink raw reply	[flat|nested] 28+ messages in thread

* [PATCH v4 20/79] staging: media: rkvdec: fix pm_runtime_get_sync() usage count
  2021-04-28 14:51 [PATCH v4 00/79] Address some issues with PM runtime at media subsystem Mauro Carvalho Chehab
@ 2021-04-28 14:51 ` Mauro Carvalho Chehab
  2021-04-28 15:09   ` Johan Hovold
  2021-04-28 14:51 ` [PATCH v4 21/79] staging: media: atomisp: use pm_runtime_resume_and_get() Mauro Carvalho Chehab
                   ` (8 subsequent siblings)
  9 siblings, 1 reply; 28+ messages in thread
From: Mauro Carvalho Chehab @ 2021-04-28 14:51 UTC (permalink / raw)
  Cc: devel, Mauro Carvalho Chehab, Greg Kroah-Hartman, linuxarm,
	linux-kernel, linux-rockchip, mauro.chehab,
	Mauro Carvalho Chehab, Ezequiel Garcia, linux-media

The pm_runtime_get_sync() internally increments the
dev->power.usage_count without decrementing it, even on errors.
Replace it by the new pm_runtime_resume_and_get(), introduced by:
commit dd8088d5a896 ("PM: runtime: Add pm_runtime_resume_and_get to deal with usage counter")
in order to properly decrement the usage counter and avoid memory
leaks.

Reviewed-by: Ezequiel Garcia <ezequiel@collabora.com>
Signed-off-by: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>
---
 drivers/staging/media/rkvdec/rkvdec.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/staging/media/rkvdec/rkvdec.c b/drivers/staging/media/rkvdec/rkvdec.c
index d821661d30f3..8c17615f3a7a 100644
--- a/drivers/staging/media/rkvdec/rkvdec.c
+++ b/drivers/staging/media/rkvdec/rkvdec.c
@@ -658,7 +658,7 @@ static void rkvdec_device_run(void *priv)
 	if (WARN_ON(!desc))
 		return;
 
-	ret = pm_runtime_get_sync(rkvdec->dev);
+	ret = pm_runtime_resume_and_get(rkvdec->dev);
 	if (ret < 0) {
 		rkvdec_job_finish_no_pm(ctx, VB2_BUF_STATE_ERROR);
 		return;
-- 
2.30.2

_______________________________________________
devel mailing list
devel@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel

^ permalink raw reply related	[flat|nested] 28+ messages in thread

* [PATCH v4 21/79] staging: media: atomisp: use pm_runtime_resume_and_get()
  2021-04-28 14:51 [PATCH v4 00/79] Address some issues with PM runtime at media subsystem Mauro Carvalho Chehab
  2021-04-28 14:51 ` [PATCH v4 20/79] staging: media: rkvdec: fix pm_runtime_get_sync() usage count Mauro Carvalho Chehab
@ 2021-04-28 14:51 ` Mauro Carvalho Chehab
  2021-04-30 16:59   ` Jonathan Cameron
  2021-04-28 14:51 ` [PATCH v4 22/79] staging: media: imx7-mipi-csis: " Mauro Carvalho Chehab
                   ` (7 subsequent siblings)
  9 siblings, 1 reply; 28+ messages in thread
From: Mauro Carvalho Chehab @ 2021-04-28 14:51 UTC (permalink / raw)
  Cc: devel, Mauro Carvalho Chehab, Greg Kroah-Hartman, linuxarm,
	linux-kernel, Sakari Ailus, mauro.chehab, Mauro Carvalho Chehab,
	linux-media

Commit dd8088d5a896 ("PM: runtime: Add pm_runtime_resume_and_get to deal with usage counter")
added pm_runtime_resume_and_get() in order to automatically handle
dev->power.usage_count decrement on errors.

Use the new API, in order to cleanup the error check logic.

Signed-off-by: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>
---
 drivers/staging/media/atomisp/pci/atomisp_fops.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/drivers/staging/media/atomisp/pci/atomisp_fops.c b/drivers/staging/media/atomisp/pci/atomisp_fops.c
index f1e6b2597853..26d05474a035 100644
--- a/drivers/staging/media/atomisp/pci/atomisp_fops.c
+++ b/drivers/staging/media/atomisp/pci/atomisp_fops.c
@@ -837,7 +837,7 @@ static int atomisp_open(struct file *file)
 	}
 
 	/* runtime power management, turn on ISP */
-	ret = pm_runtime_get_sync(vdev->v4l2_dev->dev);
+	ret = pm_runtime_resume_and_get(vdev->v4l2_dev->dev);
 	if (ret < 0) {
 		dev_err(isp->dev, "Failed to power on device\n");
 		goto error;
@@ -881,9 +881,9 @@ static int atomisp_open(struct file *file)
 
 css_error:
 	atomisp_css_uninit(isp);
-error:
-	hmm_pool_unregister(HMM_POOL_TYPE_DYNAMIC);
 	pm_runtime_put(vdev->v4l2_dev->dev);
+error:
+	hmm_pool_unregister(HMM_POOL_TYPE_DYNAMIC);
 	rt_mutex_unlock(&isp->mutex);
 	return ret;
 }
-- 
2.30.2

_______________________________________________
devel mailing list
devel@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel

^ permalink raw reply related	[flat|nested] 28+ messages in thread

* [PATCH v4 22/79] staging: media: imx7-mipi-csis: use pm_runtime_resume_and_get()
  2021-04-28 14:51 [PATCH v4 00/79] Address some issues with PM runtime at media subsystem Mauro Carvalho Chehab
  2021-04-28 14:51 ` [PATCH v4 20/79] staging: media: rkvdec: fix pm_runtime_get_sync() usage count Mauro Carvalho Chehab
  2021-04-28 14:51 ` [PATCH v4 21/79] staging: media: atomisp: use pm_runtime_resume_and_get() Mauro Carvalho Chehab
@ 2021-04-28 14:51 ` Mauro Carvalho Chehab
  2021-04-28 14:51 ` [PATCH v4 23/79] staging: media: ipu3: " Mauro Carvalho Chehab
                   ` (6 subsequent siblings)
  9 siblings, 0 replies; 28+ messages in thread
From: Mauro Carvalho Chehab @ 2021-04-28 14:51 UTC (permalink / raw)
  Cc: devel, Philipp Zabel, Mauro Carvalho Chehab, Greg Kroah-Hartman,
	Sascha Hauer, linuxarm, linux-kernel, NXP Linux Team,
	Pengutronix Kernel Team, Steve Longerbeam, mauro.chehab,
	Shawn Guo, Mauro Carvalho Chehab, linux-arm-kernel, linux-media

Commit dd8088d5a896 ("PM: runtime: Add pm_runtime_resume_and_get to deal with usage counter")
added pm_runtime_resume_and_get() in order to automatically handle
dev->power.usage_count decrement on errors.

Use the new API, in order to cleanup the error check logic.

Acked-by: Rui Miguel Silva <rmfrfs@gmail.com>
Signed-off-by: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>
---
 drivers/staging/media/imx/imx7-mipi-csis.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/drivers/staging/media/imx/imx7-mipi-csis.c b/drivers/staging/media/imx/imx7-mipi-csis.c
index 025fdc488bd6..1dc680d94a46 100644
--- a/drivers/staging/media/imx/imx7-mipi-csis.c
+++ b/drivers/staging/media/imx/imx7-mipi-csis.c
@@ -695,11 +695,10 @@ static int mipi_csis_s_stream(struct v4l2_subdev *mipi_sd, int enable)
 
 		mipi_csis_clear_counters(state);
 
-		ret = pm_runtime_get_sync(&state->pdev->dev);
-		if (ret < 0) {
-			pm_runtime_put_noidle(&state->pdev->dev);
+		ret = pm_runtime_resume_and_get(&state->pdev->dev);
+		if (ret < 0)
 			return ret;
-		}
+
 		ret = v4l2_subdev_call(state->src_sd, core, s_power, 1);
 		if (ret < 0 && ret != -ENOIOCTLCMD)
 			goto done;
-- 
2.30.2

_______________________________________________
devel mailing list
devel@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel

^ permalink raw reply related	[flat|nested] 28+ messages in thread

* [PATCH v4 23/79] staging: media: ipu3: use pm_runtime_resume_and_get()
  2021-04-28 14:51 [PATCH v4 00/79] Address some issues with PM runtime at media subsystem Mauro Carvalho Chehab
                   ` (2 preceding siblings ...)
  2021-04-28 14:51 ` [PATCH v4 22/79] staging: media: imx7-mipi-csis: " Mauro Carvalho Chehab
@ 2021-04-28 14:51 ` Mauro Carvalho Chehab
  2021-04-30 17:03   ` Jonathan Cameron
  2021-04-28 14:51 ` [PATCH v4 24/79] staging: media: cedrus_video: " Mauro Carvalho Chehab
                   ` (5 subsequent siblings)
  9 siblings, 1 reply; 28+ messages in thread
From: Mauro Carvalho Chehab @ 2021-04-28 14:51 UTC (permalink / raw)
  Cc: devel, Mauro Carvalho Chehab, Greg Kroah-Hartman, linuxarm,
	linux-kernel, Sakari Ailus, mauro.chehab, Bingbu Cao,
	Mauro Carvalho Chehab, Tianshu Qiu, linux-media

Commit dd8088d5a896 ("PM: runtime: Add pm_runtime_resume_and_get to deal with usage counter")
added pm_runtime_resume_and_get() in order to automatically handle
dev->power.usage_count decrement on errors.

Use the new API, in order to cleanup the error check logic.

Signed-off-by: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>
---
 drivers/staging/media/ipu3/ipu3.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/drivers/staging/media/ipu3/ipu3.c b/drivers/staging/media/ipu3/ipu3.c
index ee1bba6bdcac..8e1e9e46e604 100644
--- a/drivers/staging/media/ipu3/ipu3.c
+++ b/drivers/staging/media/ipu3/ipu3.c
@@ -392,10 +392,9 @@ int imgu_s_stream(struct imgu_device *imgu, int enable)
 	}
 
 	/* Set Power */
-	r = pm_runtime_get_sync(dev);
+	r = pm_runtime_resume_and_get(dev);
 	if (r < 0) {
 		dev_err(dev, "failed to set imgu power\n");
-		pm_runtime_put(dev);
 		return r;
 	}
 
-- 
2.30.2

_______________________________________________
devel mailing list
devel@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel

^ permalink raw reply related	[flat|nested] 28+ messages in thread

* [PATCH v4 24/79] staging: media: cedrus_video: use pm_runtime_resume_and_get()
  2021-04-28 14:51 [PATCH v4 00/79] Address some issues with PM runtime at media subsystem Mauro Carvalho Chehab
                   ` (3 preceding siblings ...)
  2021-04-28 14:51 ` [PATCH v4 23/79] staging: media: ipu3: " Mauro Carvalho Chehab
@ 2021-04-28 14:51 ` Mauro Carvalho Chehab
  2021-04-30 17:05   ` Jonathan Cameron
  2021-04-28 14:51 ` [PATCH v4 25/79] staging: media: tegra-vde: " Mauro Carvalho Chehab
                   ` (4 subsequent siblings)
  9 siblings, 1 reply; 28+ messages in thread
From: Mauro Carvalho Chehab @ 2021-04-28 14:51 UTC (permalink / raw)
  Cc: devel, Jernej Skrabec, Mauro Carvalho Chehab, Greg Kroah-Hartman,
	linuxarm, Maxime Ripard, linux-kernel, Paul Kocialkowski,
	Chen-Yu Tsai, mauro.chehab, Mauro Carvalho Chehab,
	linux-arm-kernel, linux-media

Commit dd8088d5a896 ("PM: runtime: Add pm_runtime_resume_and_get to deal with usage counter")
added pm_runtime_resume_and_get() in order to automatically handle
dev->power.usage_count decrement on errors.

Use the new API, in order to cleanup the error check logic.

Signed-off-by: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>
---
 drivers/staging/media/sunxi/cedrus/cedrus_video.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)

diff --git a/drivers/staging/media/sunxi/cedrus/cedrus_video.c b/drivers/staging/media/sunxi/cedrus/cedrus_video.c
index b62eb8e84057..9ddd789d0b1f 100644
--- a/drivers/staging/media/sunxi/cedrus/cedrus_video.c
+++ b/drivers/staging/media/sunxi/cedrus/cedrus_video.c
@@ -490,11 +490,9 @@ static int cedrus_start_streaming(struct vb2_queue *vq, unsigned int count)
 	}
 
 	if (V4L2_TYPE_IS_OUTPUT(vq->type)) {
-		ret = pm_runtime_get_sync(dev->dev);
-		if (ret < 0) {
-			pm_runtime_put_noidle(dev->dev);
+		ret = pm_runtime_resume_and_get(dev->dev);
+		if (ret < 0)
 			goto err_cleanup;
-		}
 
 		if (dev->dec_ops[ctx->current_codec]->start) {
 			ret = dev->dec_ops[ctx->current_codec]->start(ctx);
-- 
2.30.2

_______________________________________________
devel mailing list
devel@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel

^ permalink raw reply related	[flat|nested] 28+ messages in thread

* [PATCH v4 25/79] staging: media: tegra-vde: use pm_runtime_resume_and_get()
  2021-04-28 14:51 [PATCH v4 00/79] Address some issues with PM runtime at media subsystem Mauro Carvalho Chehab
                   ` (4 preceding siblings ...)
  2021-04-28 14:51 ` [PATCH v4 24/79] staging: media: cedrus_video: " Mauro Carvalho Chehab
@ 2021-04-28 14:51 ` Mauro Carvalho Chehab
  2021-04-29 12:38   ` Dmitry Osipenko
  2021-04-30 17:08   ` Jonathan Cameron
  2021-04-28 14:51 ` [PATCH v4 26/79] staging: media: tegra-video: " Mauro Carvalho Chehab
                   ` (3 subsequent siblings)
  9 siblings, 2 replies; 28+ messages in thread
From: Mauro Carvalho Chehab @ 2021-04-28 14:51 UTC (permalink / raw)
  Cc: devel, Mauro Carvalho Chehab, Greg Kroah-Hartman, linuxarm,
	Jonathan Hunter, linux-tegra, Thierry Reding, mauro.chehab,
	Dmitry Osipenko, Mauro Carvalho Chehab, linux-kernel,
	linux-media

Commit dd8088d5a896 ("PM: runtime: Add pm_runtime_resume_and_get to deal with usage counter")
added pm_runtime_resume_and_get() in order to automatically handle
dev->power.usage_count decrement on errors.

Use the new API, in order to cleanup the error check logic.

Signed-off-by: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>
---
 drivers/staging/media/tegra-vde/vde.c | 19 ++++++++++++++++---
 1 file changed, 16 insertions(+), 3 deletions(-)

diff --git a/drivers/staging/media/tegra-vde/vde.c b/drivers/staging/media/tegra-vde/vde.c
index 28845b5bafaf..1cdacb3f781c 100644
--- a/drivers/staging/media/tegra-vde/vde.c
+++ b/drivers/staging/media/tegra-vde/vde.c
@@ -775,9 +775,9 @@ static int tegra_vde_ioctl_decode_h264(struct tegra_vde *vde,
 	if (ret)
 		goto release_dpb_frames;
 
-	ret = pm_runtime_get_sync(dev);
+	ret = pm_runtime_resume_and_get(dev);
 	if (ret < 0)
-		goto put_runtime_pm;
+		goto unlock;
 
 	/*
 	 * We rely on the VDE registers reset value, otherwise VDE
@@ -843,6 +843,8 @@ static int tegra_vde_ioctl_decode_h264(struct tegra_vde *vde,
 put_runtime_pm:
 	pm_runtime_mark_last_busy(dev);
 	pm_runtime_put_autosuspend(dev);
+
+unlock:
 	mutex_unlock(&vde->lock);
 
 release_dpb_frames:
@@ -1069,11 +1071,17 @@ static int tegra_vde_probe(struct platform_device *pdev)
 	 * power-cycle it in order to put hardware into a predictable lower
 	 * power state.
 	 */
-	pm_runtime_get_sync(dev);
+	if (pm_runtime_resume_and_get(dev) < 0)
+		goto err_pm_runtime;
+
 	pm_runtime_put(dev);
 
 	return 0;
 
+err_pm_runtime:
+	pm_runtime_dont_use_autosuspend(dev);
+	pm_runtime_disable(dev);
+
 err_deinit_iommu:
 	tegra_vde_iommu_deinit(vde);
 
@@ -1089,7 +1097,12 @@ static int tegra_vde_remove(struct platform_device *pdev)
 	struct tegra_vde *vde = platform_get_drvdata(pdev);
 	struct device *dev = &pdev->dev;
 
+	/*
+	 * As it increments RPM usage_count even on errors, we don't need to
+	 * check the returned code here.
+	 */
 	pm_runtime_get_sync(dev);
+
 	pm_runtime_dont_use_autosuspend(dev);
 	pm_runtime_disable(dev);
 
-- 
2.30.2

_______________________________________________
devel mailing list
devel@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel

^ permalink raw reply related	[flat|nested] 28+ messages in thread

* [PATCH v4 26/79] staging: media: tegra-video: use pm_runtime_resume_and_get()
  2021-04-28 14:51 [PATCH v4 00/79] Address some issues with PM runtime at media subsystem Mauro Carvalho Chehab
                   ` (5 preceding siblings ...)
  2021-04-28 14:51 ` [PATCH v4 25/79] staging: media: tegra-vde: " Mauro Carvalho Chehab
@ 2021-04-28 14:51 ` Mauro Carvalho Chehab
  2021-04-30 17:13   ` Jonathan Cameron
  2021-04-28 14:52 ` [PATCH v4 78/79] media: hantro: " Mauro Carvalho Chehab
                   ` (2 subsequent siblings)
  9 siblings, 1 reply; 28+ messages in thread
From: Mauro Carvalho Chehab @ 2021-04-28 14:51 UTC (permalink / raw)
  Cc: devel, Mauro Carvalho Chehab, Greg Kroah-Hartman, linuxarm,
	Jonathan Hunter, linux-tegra, Thierry Reding,
	Sowjanya Komatineni, mauro.chehab, Mauro Carvalho Chehab,
	linux-kernel, linux-media

Commit dd8088d5a896 ("PM: runtime: Add pm_runtime_resume_and_get to deal with usage counter")
added pm_runtime_resume_and_get() in order to automatically handle
dev->power.usage_count decrement on errors.

Use the new API, in order to cleanup the error check logic.

Signed-off-by: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>
---
 drivers/staging/media/tegra-video/csi.c | 3 +--
 drivers/staging/media/tegra-video/vi.c  | 3 +--
 2 files changed, 2 insertions(+), 4 deletions(-)

diff --git a/drivers/staging/media/tegra-video/csi.c b/drivers/staging/media/tegra-video/csi.c
index 033a6935c26d..e938bf4c48b6 100644
--- a/drivers/staging/media/tegra-video/csi.c
+++ b/drivers/staging/media/tegra-video/csi.c
@@ -298,10 +298,9 @@ static int tegra_csi_enable_stream(struct v4l2_subdev *subdev)
 	struct tegra_csi *csi = csi_chan->csi;
 	int ret, err;
 
-	ret = pm_runtime_get_sync(csi->dev);
+	ret = pm_runtime_resume_and_get(csi->dev);
 	if (ret < 0) {
 		dev_err(csi->dev, "failed to get runtime PM: %d\n", ret);
-		pm_runtime_put_noidle(csi->dev);
 		return ret;
 	}
 
diff --git a/drivers/staging/media/tegra-video/vi.c b/drivers/staging/media/tegra-video/vi.c
index 7a09061cda57..1298740a9c6c 100644
--- a/drivers/staging/media/tegra-video/vi.c
+++ b/drivers/staging/media/tegra-video/vi.c
@@ -297,10 +297,9 @@ static int tegra_channel_start_streaming(struct vb2_queue *vq, u32 count)
 	struct tegra_vi_channel *chan = vb2_get_drv_priv(vq);
 	int ret;
 
-	ret = pm_runtime_get_sync(chan->vi->dev);
+	ret = pm_runtime_resume_and_get(chan->vi->dev);
 	if (ret < 0) {
 		dev_err(chan->vi->dev, "failed to get runtime PM: %d\n", ret);
-		pm_runtime_put_noidle(chan->vi->dev);
 		return ret;
 	}
 
-- 
2.30.2

_______________________________________________
devel mailing list
devel@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel

^ permalink raw reply related	[flat|nested] 28+ messages in thread

* [PATCH v4 78/79] media: hantro: use pm_runtime_resume_and_get()
  2021-04-28 14:51 [PATCH v4 00/79] Address some issues with PM runtime at media subsystem Mauro Carvalho Chehab
                   ` (6 preceding siblings ...)
  2021-04-28 14:51 ` [PATCH v4 26/79] staging: media: tegra-video: " Mauro Carvalho Chehab
@ 2021-04-28 14:52 ` Mauro Carvalho Chehab
  2021-04-28 17:14   ` Ezequiel Garcia
  2021-04-30 18:09   ` Jonathan Cameron
  2021-04-28 14:52 ` [PATCH v4 79/79] media: hantro: do a PM resume earlier Mauro Carvalho Chehab
  2021-04-28 15:50 ` [PATCH v4 00/79] Address some issues with PM runtime at media subsystem Johan Hovold
  9 siblings, 2 replies; 28+ messages in thread
From: Mauro Carvalho Chehab @ 2021-04-28 14:52 UTC (permalink / raw)
  Cc: devel, Mauro Carvalho Chehab, Greg Kroah-Hartman, linuxarm,
	linux-kernel, linux-rockchip, Philipp Zabel, mauro.chehab,
	Mauro Carvalho Chehab, Ezequiel Garcia, linux-media

Commit dd8088d5a896 ("PM: runtime: Add pm_runtime_resume_and_get to deal with usage counter")
added pm_runtime_resume_and_get() in order to automatically handle
dev->power.usage_count decrement on errors.

While there's nothing wrong with the current usage on this driver,
as we're getting rid of the pm_runtime_get_sync() call all over
the media subsystem, let's remove the last occurrence on this
driver.

Signed-off-by: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>
---
 drivers/staging/media/hantro/hantro_drv.c | 23 ++++++++++++++++-------
 1 file changed, 16 insertions(+), 7 deletions(-)

diff --git a/drivers/staging/media/hantro/hantro_drv.c b/drivers/staging/media/hantro/hantro_drv.c
index 595e82a82728..25fa36e7e773 100644
--- a/drivers/staging/media/hantro/hantro_drv.c
+++ b/drivers/staging/media/hantro/hantro_drv.c
@@ -56,14 +56,12 @@ dma_addr_t hantro_get_ref(struct hantro_ctx *ctx, u64 ts)
 	return hantro_get_dec_buf_addr(ctx, buf);
 }
 
-static void hantro_job_finish(struct hantro_dev *vpu,
-			      struct hantro_ctx *ctx,
-			      enum vb2_buffer_state result)
+static void hantro_job_finish_no_pm(struct hantro_dev *vpu,
+				    struct hantro_ctx *ctx,
+				    enum vb2_buffer_state result)
 {
 	struct vb2_v4l2_buffer *src, *dst;
 
-	pm_runtime_mark_last_busy(vpu->dev);
-	pm_runtime_put_autosuspend(vpu->dev);
 	clk_bulk_disable(vpu->variant->num_clocks, vpu->clocks);
 
 	src = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
@@ -81,6 +79,16 @@ static void hantro_job_finish(struct hantro_dev *vpu,
 					 result);
 }
 
+static void hantro_job_finish(struct hantro_dev *vpu,
+			      struct hantro_ctx *ctx,
+			      enum vb2_buffer_state result)
+{
+	pm_runtime_mark_last_busy(vpu->dev);
+	pm_runtime_put_autosuspend(vpu->dev);
+
+	hantro_job_finish_no_pm(vpu, ctx, result);
+}
+
 void hantro_irq_done(struct hantro_dev *vpu,
 		     enum vb2_buffer_state result)
 {
@@ -155,7 +163,8 @@ static void device_run(void *priv)
 	ret = clk_bulk_enable(ctx->dev->variant->num_clocks, ctx->dev->clocks);
 	if (ret)
 		goto err_cancel_job;
-	ret = pm_runtime_get_sync(ctx->dev->dev);
+
+	ret = pm_runtime_resume_and_get(ctx->dev->dev);
 	if (ret < 0)
 		goto err_cancel_job;
 
@@ -165,7 +174,7 @@ static void device_run(void *priv)
 	return;
 
 err_cancel_job:
-	hantro_job_finish(ctx->dev, ctx, VB2_BUF_STATE_ERROR);
+	hantro_job_finish_no_pm(ctx->dev, ctx, VB2_BUF_STATE_ERROR);
 }
 
 static struct v4l2_m2m_ops vpu_m2m_ops = {
-- 
2.30.2

_______________________________________________
devel mailing list
devel@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel

^ permalink raw reply related	[flat|nested] 28+ messages in thread

* [PATCH v4 79/79] media: hantro: do a PM resume earlier
  2021-04-28 14:51 [PATCH v4 00/79] Address some issues with PM runtime at media subsystem Mauro Carvalho Chehab
                   ` (7 preceding siblings ...)
  2021-04-28 14:52 ` [PATCH v4 78/79] media: hantro: " Mauro Carvalho Chehab
@ 2021-04-28 14:52 ` Mauro Carvalho Chehab
  2021-04-28 17:17   ` Ezequiel Garcia
  2021-04-28 15:50 ` [PATCH v4 00/79] Address some issues with PM runtime at media subsystem Johan Hovold
  9 siblings, 1 reply; 28+ messages in thread
From: Mauro Carvalho Chehab @ 2021-04-28 14:52 UTC (permalink / raw)
  Cc: devel, Mauro Carvalho Chehab, Greg Kroah-Hartman, linuxarm,
	linux-kernel, linux-rockchip, Philipp Zabel, mauro.chehab,
	Mauro Carvalho Chehab, Ezequiel Garcia, linux-media

The device_run() first enables the clock and then
tries to resume PM runtime, checking for errors.

Well, if for some reason the pm_runtime can not resume,
it would be better to detect it beforehand.

So, change the order inside device_run().

Signed-off-by: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>
---
 drivers/staging/media/hantro/hantro_drv.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/drivers/staging/media/hantro/hantro_drv.c b/drivers/staging/media/hantro/hantro_drv.c
index 25fa36e7e773..67de6b15236d 100644
--- a/drivers/staging/media/hantro/hantro_drv.c
+++ b/drivers/staging/media/hantro/hantro_drv.c
@@ -160,14 +160,14 @@ static void device_run(void *priv)
 	src = hantro_get_src_buf(ctx);
 	dst = hantro_get_dst_buf(ctx);
 
-	ret = clk_bulk_enable(ctx->dev->variant->num_clocks, ctx->dev->clocks);
-	if (ret)
-		goto err_cancel_job;
-
 	ret = pm_runtime_resume_and_get(ctx->dev->dev);
 	if (ret < 0)
 		goto err_cancel_job;
 
+	ret = clk_bulk_enable(ctx->dev->variant->num_clocks, ctx->dev->clocks);
+	if (ret)
+		goto err_cancel_job;
+
 	v4l2_m2m_buf_copy_metadata(src, dst, true);
 
 	ctx->codec_ops->run(ctx);
-- 
2.30.2

_______________________________________________
devel mailing list
devel@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel

^ permalink raw reply related	[flat|nested] 28+ messages in thread

* Re: [PATCH v4 20/79] staging: media: rkvdec: fix pm_runtime_get_sync() usage count
  2021-04-28 14:51 ` [PATCH v4 20/79] staging: media: rkvdec: fix pm_runtime_get_sync() usage count Mauro Carvalho Chehab
@ 2021-04-28 15:09   ` Johan Hovold
  2021-04-29  7:38     ` Mauro Carvalho Chehab
  0 siblings, 1 reply; 28+ messages in thread
From: Johan Hovold @ 2021-04-28 15:09 UTC (permalink / raw)
  To: Mauro Carvalho Chehab
  Cc: devel, Greg Kroah-Hartman, linuxarm, linux-kernel,
	linux-rockchip, mauro.chehab, Mauro Carvalho Chehab,
	Ezequiel Garcia, linux-media

On Wed, Apr 28, 2021 at 04:51:41PM +0200, Mauro Carvalho Chehab wrote:
> The pm_runtime_get_sync() internally increments the
> dev->power.usage_count without decrementing it, even on errors.
> Replace it by the new pm_runtime_resume_and_get(), introduced by:
> commit dd8088d5a896 ("PM: runtime: Add pm_runtime_resume_and_get to deal with usage counter")
> in order to properly decrement the usage counter and avoid memory
> leaks.

Again, there is no memory leak here either. Just a potential PM usage
counter leak.

> Reviewed-by: Ezequiel Garcia <ezequiel@collabora.com>
> Signed-off-by: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>
> ---
>  drivers/staging/media/rkvdec/rkvdec.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/staging/media/rkvdec/rkvdec.c b/drivers/staging/media/rkvdec/rkvdec.c
> index d821661d30f3..8c17615f3a7a 100644
> --- a/drivers/staging/media/rkvdec/rkvdec.c
> +++ b/drivers/staging/media/rkvdec/rkvdec.c
> @@ -658,7 +658,7 @@ static void rkvdec_device_run(void *priv)
>  	if (WARN_ON(!desc))
>  		return;
>  
> -	ret = pm_runtime_get_sync(rkvdec->dev);
> +	ret = pm_runtime_resume_and_get(rkvdec->dev);
>  	if (ret < 0) {
>  		rkvdec_job_finish_no_pm(ctx, VB2_BUF_STATE_ERROR);
>  		return;

Johan
_______________________________________________
devel mailing list
devel@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel

^ permalink raw reply	[flat|nested] 28+ messages in thread

* Re: [PATCH v4 00/79] Address some issues with PM runtime at media subsystem
  2021-04-28 14:51 [PATCH v4 00/79] Address some issues with PM runtime at media subsystem Mauro Carvalho Chehab
                   ` (8 preceding siblings ...)
  2021-04-28 14:52 ` [PATCH v4 79/79] media: hantro: do a PM resume earlier Mauro Carvalho Chehab
@ 2021-04-28 15:50 ` Johan Hovold
  2021-04-29 10:18   ` Mauro Carvalho Chehab
  9 siblings, 1 reply; 28+ messages in thread
From: Johan Hovold @ 2021-04-28 15:50 UTC (permalink / raw)
  To: Mauro Carvalho Chehab
  Cc: Ricardo Ribalda, Dafna Hirschfeld, Heiko Stuebner, mauro.chehab,
	linuxarm, Todor Tomov, Bjorn Andersson, Andrzej Hajda, Lad,
	Prabhakar, Thierry Reding, Robert Foss, Sylwester Nawrocki,
	Dmitry Osipenko, linux-stm32, Andrzej Pietrasiewicz, Leon Luo,
	Dan Scally, linux-samsung-soc, Dave Stevenson,
	Manivannan Sadhasivam, Chen-Yu Tsai, Krzysztof Kozlowski,
	Jonathan Hunter, linux-rockchip, Matt Ranostay, Andy Gross,
	Dongchun Zhu, Maxime Coquelin, Steve Longerbeam, Bingbu Cao,
	Marek Szyprowski, Shunqian Zheng, Tianshu Qiu, NXP Linux Team,
	Shawn Tu, devel, Jacopo Mondi, linux-tegra, Sakari Ailus,
	Wenyou Yang, Alexandre Torgue, linux-arm-msm, Sascha Hauer,
	linux-renesas-soc, Maxime Ripard, Stanimir Varbanov,
	Benoit Parrot, Helen Koike, Yong Zhi, linux-mediatek,
	Jacek Anaszewski, Matthias Brugger, Sylwester Nawrocki,
	Mauro Carvalho Chehab, Ezequiel Garcia, Daniele Alessandrelli,
	Chiranjeevi Rapolu, linux-arm-kernel, Jacob Chen, Jernej Skrabec,
	Philipp Zabel, Hyungwoo Yang, linux-kernel, Paul J. Murphy,
	Paul Kocialkowski, Sowjanya Komatineni, Pengutronix Kernel Team,
	linux-media, Shawn Guo

On Wed, Apr 28, 2021 at 04:51:21PM +0200, Mauro Carvalho Chehab wrote:
> During the review of the patches from unm.edu, one of the patterns
> I noticed is the amount of patches trying to fix pm_runtime_get_sync()
> calls.
> 
> After analyzing the feedback from version 1 of this series, I noticed
> a few other weird behaviors at the PM runtime resume code. So, this
> series start addressing some bugs and issues at the current code.
> Then, it gets rid of pm_runtime_get_sync() at the media subsystem
> (with 2 exceptions).
> 
> It should be noticed that
> Commit dd8088d5a896 ("PM: runtime: Add pm_runtime_resume_and_get to deal with usage counter")
> added a new method to does a pm_runtime get, which increments
> the usage count only on success.
> 
> The rationale of getting rid of pm_runtime_get_sync() is:
> 
> 1. despite its name, this is actually a PM runtime resume call,
>    but some developers didn't seem to realize that, as I got this
>    pattern on some drivers:
> 
>         pm_runtime_get_sync(&client->dev);
>         pm_runtime_disable(&client->dev);
>         pm_runtime_set_suspended(&client->dev);
>         pm_runtime_put_noidle(&client->dev);
> 
>    It makes no sense to resume PM just to suspend it again ;-)

This is perfectly alright. Take a look at ov7740_remove() for example:

	pm_runtime_get_sync(&client->dev);
	pm_runtime_disable(&client->dev);
	pm_runtime_set_suspended(&client->dev);
	pm_runtime_put_noidle(&client->dev);
	
	ov7740_set_power(ov7740, 0);

There's an explicit power-off after balancing the PM count and this will
work regardless of the power state when entering this function.

So this has nothing to do with pm_runtime_get_sync() per se.

> 2. Usual *_get() methods only increment their use count on success,
>    but pm_runtime_get_sync() increments it unconditionally. Due to
>    that, several drivers were mistakenly not calling
>    pm_runtime_put_noidle() when it fails;

Sure, but pm_runtime_get_async() also works this way. You just won't be
notified if the async resume fails.

> 3. The name of the new variant is a lot clearer:
> 	pm_runtime_resume_and_get()
>     As its same clearly says that this is a PM runtime resume function,
>     that also increments the usage counter on success;

It also introduced an inconsistency in the API and does not pair as well
with the pm_runtime_put variants.

> 4. Consistency: we did similar changes subsystem wide with
>    for instance strlcpy() and strcpy() that got replaced by
>    strscpy(). Having all drivers using the same known-to-be-safe
>    methods is a good thing;

It's not known to be safe; there are ways to get also this interface
wrong as for example this series has shown.

> 5. Prevent newer drivers to copy-and-paste a code that it would
>    be easier to break if they don't truly understand what's behind
>    the scenes.

Cargo-cult programming always runs that risk.

> This series replace places  pm_runtime_get_sync(), by calling
> pm_runtime_resume_and_get() instead.
> 
> This should help to avoid future mistakes like that, as people
> tend to use the existing drivers as examples for newer ones.

The only valid point about and use for pm_runtime_resume_and_get() is to
avoid leaking a PM usage count reference in the unlikely case that
resume fails (something which hardly any driver implements recovery
from anyway).

It's a convenience wrapper that saves you from writing one extra line in
some cases (depending on how you implement runtime-pm support) and not a
silver bullet against bugs.
 
> compile-tested only.
> 
> Patches 1 to 7 fix some issues that already exists at the current
> PM runtime code;
> 
> patches 8 to 20 fix some usage_count problems that still exists
> at the media subsystem;
> 
> patches 21 to 78 repaces pm_runtime_get_sync() by 
> pm_runtime_resume_and_get();
> 
> Patch 79 (and a hunk on patch 78) documents the two exceptions
> where pm_runtime_get_sync() will still be used for now.
> 
> ---
> 
> v4:
>     - Added a couple of additional fixes at existing PM runtime code;
>     - Some patches are now more conservative in order to avoid causing
>      regressions.
> v3:
>     - fix a compilation error;
> v2:
>     - addressed pointed issues and fixed a few other PM issues.

This really doesn't say much more than "changed stuff" so kinda hard to
track if review feedback has been taken into account for example.

Johan
_______________________________________________
devel mailing list
devel@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel

^ permalink raw reply	[flat|nested] 28+ messages in thread

* Re: [PATCH v4 78/79] media: hantro: use pm_runtime_resume_and_get()
  2021-04-28 14:52 ` [PATCH v4 78/79] media: hantro: " Mauro Carvalho Chehab
@ 2021-04-28 17:14   ` Ezequiel Garcia
  2021-04-30 18:09   ` Jonathan Cameron
  1 sibling, 0 replies; 28+ messages in thread
From: Ezequiel Garcia @ 2021-04-28 17:14 UTC (permalink / raw)
  To: Mauro Carvalho Chehab
  Cc: devel, Greg Kroah-Hartman, linuxarm, linux-kernel,
	linux-rockchip, Philipp Zabel, mauro.chehab,
	Mauro Carvalho Chehab, linux-media

On Wed, 2021-04-28 at 16:52 +0200, Mauro Carvalho Chehab wrote:
> Commit dd8088d5a896 ("PM: runtime: Add pm_runtime_resume_and_get to deal with usage counter")
> added pm_runtime_resume_and_get() in order to automatically handle
> dev->power.usage_count decrement on errors.
> 
> While there's nothing wrong with the current usage on this driver,
> as we're getting rid of the pm_runtime_get_sync() call all over
> the media subsystem, let's remove the last occurrence on this
> driver.
> 
> Signed-off-by: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>

Looks good.

Reviewed-by: Ezequiel Garcia <ezequiel@collabora.com>

Thanks,
Ezequiel

> ---
>  drivers/staging/media/hantro/hantro_drv.c | 23 ++++++++++++++++-------
>  1 file changed, 16 insertions(+), 7 deletions(-)
> 
> diff --git a/drivers/staging/media/hantro/hantro_drv.c b/drivers/staging/media/hantro/hantro_drv.c
> index 595e82a82728..25fa36e7e773 100644
> --- a/drivers/staging/media/hantro/hantro_drv.c
> +++ b/drivers/staging/media/hantro/hantro_drv.c
> @@ -56,14 +56,12 @@ dma_addr_t hantro_get_ref(struct hantro_ctx *ctx, u64 ts)
>         return hantro_get_dec_buf_addr(ctx, buf);
>  }
>  
> -static void hantro_job_finish(struct hantro_dev *vpu,
> -                             struct hantro_ctx *ctx,
> -                             enum vb2_buffer_state result)
> +static void hantro_job_finish_no_pm(struct hantro_dev *vpu,
> +                                   struct hantro_ctx *ctx,
> +                                   enum vb2_buffer_state result)
>  {
>         struct vb2_v4l2_buffer *src, *dst;
>  
> -       pm_runtime_mark_last_busy(vpu->dev);
> -       pm_runtime_put_autosuspend(vpu->dev);
>         clk_bulk_disable(vpu->variant->num_clocks, vpu->clocks);
>  
>         src = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
> @@ -81,6 +79,16 @@ static void hantro_job_finish(struct hantro_dev *vpu,
>                                          result);
>  }
>  
> +static void hantro_job_finish(struct hantro_dev *vpu,
> +                             struct hantro_ctx *ctx,
> +                             enum vb2_buffer_state result)
> +{
> +       pm_runtime_mark_last_busy(vpu->dev);
> +       pm_runtime_put_autosuspend(vpu->dev);
> +
> +       hantro_job_finish_no_pm(vpu, ctx, result);
> +}
> +
>  void hantro_irq_done(struct hantro_dev *vpu,
>                      enum vb2_buffer_state result)
>  {
> @@ -155,7 +163,8 @@ static void device_run(void *priv)
>         ret = clk_bulk_enable(ctx->dev->variant->num_clocks, ctx->dev->clocks);
>         if (ret)
>                 goto err_cancel_job;
> -       ret = pm_runtime_get_sync(ctx->dev->dev);
> +
> +       ret = pm_runtime_resume_and_get(ctx->dev->dev);
>         if (ret < 0)
>                 goto err_cancel_job;
>  
> @@ -165,7 +174,7 @@ static void device_run(void *priv)
>         return;
>  
>  err_cancel_job:
> -       hantro_job_finish(ctx->dev, ctx, VB2_BUF_STATE_ERROR);
> +       hantro_job_finish_no_pm(ctx->dev, ctx, VB2_BUF_STATE_ERROR);
>  }
>  
>  static struct v4l2_m2m_ops vpu_m2m_ops = {


_______________________________________________
devel mailing list
devel@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel

^ permalink raw reply	[flat|nested] 28+ messages in thread

* Re: [PATCH v4 79/79] media: hantro: do a PM resume earlier
  2021-04-28 14:52 ` [PATCH v4 79/79] media: hantro: do a PM resume earlier Mauro Carvalho Chehab
@ 2021-04-28 17:17   ` Ezequiel Garcia
  2021-04-29  7:13     ` Mauro Carvalho Chehab
  0 siblings, 1 reply; 28+ messages in thread
From: Ezequiel Garcia @ 2021-04-28 17:17 UTC (permalink / raw)
  To: Mauro Carvalho Chehab
  Cc: devel, Greg Kroah-Hartman, linuxarm, linux-kernel,
	linux-rockchip, Philipp Zabel, mauro.chehab,
	Mauro Carvalho Chehab, linux-media

Hi Mauro,

Thanks a lot for taking care of this.

On Wed, 2021-04-28 at 16:52 +0200, Mauro Carvalho Chehab wrote:
> The device_run() first enables the clock and then
> tries to resume PM runtime, checking for errors.
> 
> Well, if for some reason the pm_runtime can not resume,
> it would be better to detect it beforehand.
> 
> So, change the order inside device_run().
> 
> Signed-off-by: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>

Clocks could be behind power-domains, IIRC, so this change
is fixing that.

However, this has ever been a problem for this driver,
so I don't think it makes sense to bother with Fixes tag.

Reviewed-by: Ezequiel Garcia <ezequiel@collabora.com>

Thanks,
Ezequiel

> ---
>  drivers/staging/media/hantro/hantro_drv.c | 8 ++++----
>  1 file changed, 4 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/staging/media/hantro/hantro_drv.c b/drivers/staging/media/hantro/hantro_drv.c
> index 25fa36e7e773..67de6b15236d 100644
> --- a/drivers/staging/media/hantro/hantro_drv.c
> +++ b/drivers/staging/media/hantro/hantro_drv.c
> @@ -160,14 +160,14 @@ static void device_run(void *priv)
>         src = hantro_get_src_buf(ctx);
>         dst = hantro_get_dst_buf(ctx);
>  
> -       ret = clk_bulk_enable(ctx->dev->variant->num_clocks, ctx->dev->clocks);
> -       if (ret)
> -               goto err_cancel_job;
> -
>         ret = pm_runtime_resume_and_get(ctx->dev->dev);
>         if (ret < 0)
>                 goto err_cancel_job;
>  
> +       ret = clk_bulk_enable(ctx->dev->variant->num_clocks, ctx->dev->clocks);
> +       if (ret)
> +               goto err_cancel_job;
> +
>         v4l2_m2m_buf_copy_metadata(src, dst, true);
>  
>         ctx->codec_ops->run(ctx);


_______________________________________________
devel mailing list
devel@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel

^ permalink raw reply	[flat|nested] 28+ messages in thread

* Re: [PATCH v4 79/79] media: hantro: do a PM resume earlier
  2021-04-28 17:17   ` Ezequiel Garcia
@ 2021-04-29  7:13     ` Mauro Carvalho Chehab
  0 siblings, 0 replies; 28+ messages in thread
From: Mauro Carvalho Chehab @ 2021-04-29  7:13 UTC (permalink / raw)
  To: Ezequiel Garcia
  Cc: devel, Greg Kroah-Hartman, linuxarm, linux-kernel,
	linux-rockchip, Philipp Zabel, mauro.chehab,
	Mauro Carvalho Chehab, linux-media

Em Wed, 28 Apr 2021 14:17:50 -0300
Ezequiel Garcia <ezequiel@collabora.com> escreveu:

> Hi Mauro,
> 
> Thanks a lot for taking care of this.
> 
> On Wed, 2021-04-28 at 16:52 +0200, Mauro Carvalho Chehab wrote:
> > The device_run() first enables the clock and then
> > tries to resume PM runtime, checking for errors.
> > 
> > Well, if for some reason the pm_runtime can not resume,
> > it would be better to detect it beforehand.
> > 
> > So, change the order inside device_run().
> > 
> > Signed-off-by: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>  
> 
> Clocks could be behind power-domains, IIRC, so this change
> is fixing that.
> 
> However, this has ever been a problem for this driver,
> so I don't think it makes sense to bother with Fixes tag.

I would prefer to move this patch to the first part of this
series, together with other fixes, rebasing it to apply cleanly
before the pm_runtime_resume_and_get() patch, with:

    Fixes: 775fec69008d ("media: add Rockchip VPU JPEG encoder driver")

This way, people that could be interested on backporting it will be
capable to apply it as is to stable Kernel releases that came
with this driver.

> 
> Reviewed-by: Ezequiel Garcia <ezequiel@collabora.com>
> 
> Thanks,
> Ezequiel
> 
> > ---
> >  drivers/staging/media/hantro/hantro_drv.c | 8 ++++----
> >  1 file changed, 4 insertions(+), 4 deletions(-)
> > 
> > diff --git a/drivers/staging/media/hantro/hantro_drv.c b/drivers/staging/media/hantro/hantro_drv.c
> > index 25fa36e7e773..67de6b15236d 100644
> > --- a/drivers/staging/media/hantro/hantro_drv.c
> > +++ b/drivers/staging/media/hantro/hantro_drv.c
> > @@ -160,14 +160,14 @@ static void device_run(void *priv)
> >         src = hantro_get_src_buf(ctx);
> >         dst = hantro_get_dst_buf(ctx);
> >  
> > -       ret = clk_bulk_enable(ctx->dev->variant->num_clocks, ctx->dev->clocks);
> > -       if (ret)
> > -               goto err_cancel_job;
> > -
> >         ret = pm_runtime_resume_and_get(ctx->dev->dev);
> >         if (ret < 0)
> >                 goto err_cancel_job;
> >  
> > +       ret = clk_bulk_enable(ctx->dev->variant->num_clocks, ctx->dev->clocks);
> > +       if (ret)
> > +               goto err_cancel_job;
> > +
> >         v4l2_m2m_buf_copy_metadata(src, dst, true);
> >  
> >         ctx->codec_ops->run(ctx);  
> 
> 



Thanks,
Mauro
_______________________________________________
devel mailing list
devel@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel

^ permalink raw reply	[flat|nested] 28+ messages in thread

* Re: [PATCH v4 20/79] staging: media: rkvdec: fix pm_runtime_get_sync() usage count
  2021-04-28 15:09   ` Johan Hovold
@ 2021-04-29  7:38     ` Mauro Carvalho Chehab
  0 siblings, 0 replies; 28+ messages in thread
From: Mauro Carvalho Chehab @ 2021-04-29  7:38 UTC (permalink / raw)
  To: Johan Hovold
  Cc: devel, Greg Kroah-Hartman, linuxarm, linux-kernel,
	linux-rockchip, mauro.chehab, Mauro Carvalho Chehab,
	Ezequiel Garcia, linux-media

Em Wed, 28 Apr 2021 17:09:57 +0200
Johan Hovold <johan@kernel.org> escreveu:

> On Wed, Apr 28, 2021 at 04:51:41PM +0200, Mauro Carvalho Chehab wrote:
> > The pm_runtime_get_sync() internally increments the
> > dev->power.usage_count without decrementing it, even on errors.
> > Replace it by the new pm_runtime_resume_and_get(), introduced by:
> > commit dd8088d5a896 ("PM: runtime: Add pm_runtime_resume_and_get to deal with usage counter")
> > in order to properly decrement the usage counter and avoid memory
> > leaks.  
> 
> Again, there is no memory leak here either. Just a potential PM usage
> counter leak.

True. Will fix it at the entire series with:

	FILTER_BRANCH_SQUELCH_WARNING=1 git filter-branch -f --msg-filter "cat|perl -0pe 's/ and avoid memory\n\s*leaks./, avoiding\na potential PM usage counter leak./igs'" BASE..

Regards,
Mauro
_______________________________________________
devel mailing list
devel@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel

^ permalink raw reply	[flat|nested] 28+ messages in thread

* Re: [PATCH v4 00/79] Address some issues with PM runtime at media subsystem
  2021-04-28 15:50 ` [PATCH v4 00/79] Address some issues with PM runtime at media subsystem Johan Hovold
@ 2021-04-29 10:18   ` Mauro Carvalho Chehab
  2021-04-29 12:33     ` Dmitry Osipenko
  2021-04-29 15:17     ` Johan Hovold
  0 siblings, 2 replies; 28+ messages in thread
From: Mauro Carvalho Chehab @ 2021-04-29 10:18 UTC (permalink / raw)
  To: Johan Hovold
  Cc: Ricardo Ribalda, Dafna Hirschfeld, Heiko Stuebner, mauro.chehab,
	linuxarm, Todor Tomov, Bjorn Andersson, Andrzej Hajda, Lad,
	Prabhakar, Thierry Reding, Robert Foss, Sylwester Nawrocki,
	Dmitry Osipenko, linux-stm32, Andrzej Pietrasiewicz, Leon Luo,
	Dan Scally, linux-samsung-soc, Dave Stevenson,
	Manivannan Sadhasivam, Chen-Yu Tsai, Krzysztof Kozlowski,
	Jonathan Hunter, linux-rockchip, Matt Ranostay, Andy Gross,
	Dongchun Zhu, Maxime Coquelin, Steve Longerbeam, Bingbu Cao,
	Marek Szyprowski, Shunqian Zheng, Tianshu Qiu, NXP Linux Team,
	Shawn Tu, devel, Jacopo Mondi, linux-tegra, Sakari Ailus,
	Wenyou Yang, Alexandre Torgue, linux-arm-msm, Sascha Hauer,
	linux-renesas-soc, Maxime Ripard, Stanimir Varbanov,
	Benoit Parrot, Helen Koike, Yong Zhi, linux-mediatek,
	Jacek Anaszewski, Matthias Brugger, Sylwester Nawrocki,
	Mauro Carvalho Chehab, Ezequiel Garcia, Daniele Alessandrelli,
	Chiranjeevi Rapolu, linux-arm-kernel, Jacob Chen, Jernej Skrabec,
	Philipp Zabel, Hyungwoo Yang, linux-kernel, Paul J. Murphy,
	Paul Kocialkowski, Sowjanya Komatineni, Pengutronix Kernel Team,
	linux-media, Shawn Guo

Em Wed, 28 Apr 2021 17:50:08 +0200
Johan Hovold <johan@kernel.org> escreveu:

> On Wed, Apr 28, 2021 at 04:51:21PM +0200, Mauro Carvalho Chehab wrote:

> > 1. despite its name, this is actually a PM runtime resume call,
> >    but some developers didn't seem to realize that, as I got this
> >    pattern on some drivers:
> > 
> >         pm_runtime_get_sync(&client->dev);
> >         pm_runtime_disable(&client->dev);
> >         pm_runtime_set_suspended(&client->dev);
> >         pm_runtime_put_noidle(&client->dev);
> > 
> >    It makes no sense to resume PM just to suspend it again ;-)  
> 
> This is perfectly alright. Take a look at ov7740_remove() for example:
> 
> 	pm_runtime_get_sync(&client->dev);
> 	pm_runtime_disable(&client->dev);
> 	pm_runtime_set_suspended(&client->dev);
> 	pm_runtime_put_noidle(&client->dev);
> 	
> 	ov7740_set_power(ov7740, 0);
> 
> There's an explicit power-off after balancing the PM count and this will
> work regardless of the power state when entering this function.

Ok, but this should equally work:

 	pm_runtime_disable(&client->dev);
 	pm_runtime_set_suspended(&client->dev);
 	
 	ov7740_set_power(ov7740, 0);

as there's no additional cleanup made on this particular driver
between pm_runtime_get_sync() and pm_runtime_put_noidle().

> So this has nothing to do with pm_runtime_get_sync() per se.

Yes, but some patches on this series are cleaning up the driver release
logic.

> 
> > 2. Usual *_get() methods only increment their use count on success,
> >    but pm_runtime_get_sync() increments it unconditionally. Due to
> >    that, several drivers were mistakenly not calling
> >    pm_runtime_put_noidle() when it fails;  
> 
> Sure, but pm_runtime_get_async() also works this way. You just won't be
> notified if the async resume fails.

Granted, it makes sense along the pm_runtime kAPI.

It is inconsistent with the behavior of kobject_get*() and other
*_get*() methods that are based or inspired on it, as, on those, the
operations are atomic: either everything succeeds and it doesn't return
an error, or the usage counter is not incremented and the object
state doesn't change after the call.

> > 3. The name of the new variant is a lot clearer:
> > 	pm_runtime_resume_and_get()
> >     As its same clearly says that this is a PM runtime resume function,
> >     that also increments the usage counter on success;  
> 
> It also introduced an inconsistency in the API and does not pair as well
> with the pm_runtime_put variants.

Agreed. A name that would be more consistent with PM runtime would
probably be:

	pm_runtime_resume_if_get()

as there are already:

	pm_runtime_get_if_in_use()
	pm_runtime_get_if_active()

But any such discussions are out of the scope of this patchset ;-)

> 
> > 4. Consistency: we did similar changes subsystem wide with
> >    for instance strlcpy() and strcpy() that got replaced by
> >    strscpy(). Having all drivers using the same known-to-be-safe
> >    methods is a good thing;  
> 
> It's not known to be safe; there are ways to get also this interface
> wrong as for example this series has shown.

Very true. Yet, it is a lot simpler to use functions that won't change
the state of the objects when returning an error, as this is by far
the most common pattern within the Kernel.

Human brains are trained to identify certain patterns. When there's
something using a similar pattern, but with a different behavior, 
our brains are more subject to fail identifying problems.

> > 5. Prevent newer drivers to copy-and-paste a code that it would
> >    be easier to break if they don't truly understand what's behind
> >    the scenes.  
> 
> Cargo-cult programming always runs that risk.

True.

> > This series replace places  pm_runtime_get_sync(), by calling
> > pm_runtime_resume_and_get() instead.
> > 
> > This should help to avoid future mistakes like that, as people
> > tend to use the existing drivers as examples for newer ones.  
> 
> The only valid point about and use for pm_runtime_resume_and_get() is to
> avoid leaking a PM usage count reference in the unlikely case that
> resume fails (something which hardly any driver implements recovery
> from anyway).
> 
> It's a convenience wrapper that saves you from writing one extra line in
> some cases (depending on how you implement runtime-pm support) and not a
> silver bullet against bugs.
>  
> > compile-tested only.
> > 
> > Patches 1 to 7 fix some issues that already exists at the current
> > PM runtime code;
> > 
> > patches 8 to 20 fix some usage_count problems that still exists
> > at the media subsystem;
> > 
> > patches 21 to 78 repaces pm_runtime_get_sync() by 
> > pm_runtime_resume_and_get();
> > 
> > Patch 79 (and a hunk on patch 78) documents the two exceptions
> > where pm_runtime_get_sync() will still be used for now.
> > 
> > ---
> > 
> > v4:
> >     - Added a couple of additional fixes at existing PM runtime code;
> >     - Some patches are now more conservative in order to avoid causing
> >      regressions.
> > v3:
> >     - fix a compilation error;
> > v2:
> >     - addressed pointed issues and fixed a few other PM issues.  
> 
> This really doesn't say much more than "changed stuff" so kinda hard to
> track if review feedback has been taken into account for example.

I addressed all review feedback I got (as far as I'm aware), and added
all received reviewed-by/acked-by.

Yeah, I could have written a more comprehensive changes description
there.

Thanks,
Mauro
_______________________________________________
devel mailing list
devel@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel

^ permalink raw reply	[flat|nested] 28+ messages in thread

* Re: [PATCH v4 00/79] Address some issues with PM runtime at media subsystem
  2021-04-29 10:18   ` Mauro Carvalho Chehab
@ 2021-04-29 12:33     ` Dmitry Osipenko
  2021-04-29 15:17     ` Johan Hovold
  1 sibling, 0 replies; 28+ messages in thread
From: Dmitry Osipenko @ 2021-04-29 12:33 UTC (permalink / raw)
  To: Mauro Carvalho Chehab, Johan Hovold
  Cc: Ricardo Ribalda, Dafna Hirschfeld, Heiko Stuebner, mauro.chehab,
	linuxarm, Todor Tomov, Bjorn Andersson, Andrzej Hajda, Lad,
	Prabhakar, Thierry Reding, Robert Foss, Sylwester Nawrocki,
	linux-stm32, Andrzej Pietrasiewicz, Leon Luo, Dan Scally,
	linux-samsung-soc, Dave Stevenson, Manivannan Sadhasivam,
	Chen-Yu Tsai, Krzysztof Kozlowski, Jonathan Hunter,
	linux-rockchip, Matt Ranostay, Andy Gross, Dongchun Zhu,
	Maxime Coquelin, Steve Longerbeam, Bingbu Cao, Marek Szyprowski,
	Shunqian Zheng, Tianshu Qiu, NXP Linux Team, Shawn Tu, devel,
	Jacopo Mondi, linux-tegra, Sakari Ailus, Wenyou Yang,
	Alexandre Torgue, linux-arm-msm, Sascha Hauer, linux-renesas-soc,
	Maxime Ripard, Stanimir Varbanov, Benoit Parrot, Helen Koike,
	Yong Zhi, linux-mediatek, Jacek Anaszewski, Matthias Brugger,
	Sylwester Nawrocki, Mauro Carvalho Chehab, Ezequiel Garcia,
	Daniele Alessandrelli, Chiranjeevi Rapolu, linux-arm-kernel,
	Jacob Chen, Jernej Skrabec, Philipp Zabel, Hyungwoo Yang,
	linux-kernel, Paul J. Murphy, Paul Kocialkowski,
	Sowjanya Komatineni, Pengutronix Kernel Team, linux-media,
	Shawn Guo

29.04.2021 13:18, Mauro Carvalho Chehab пишет:
>> This is perfectly alright. Take a look at ov7740_remove() for example:
>>
>> 	pm_runtime_get_sync(&client->dev);
>> 	pm_runtime_disable(&client->dev);
>> 	pm_runtime_set_suspended(&client->dev);
>> 	pm_runtime_put_noidle(&client->dev);
>> 	
>> 	ov7740_set_power(ov7740, 0);
>>
>> There's an explicit power-off after balancing the PM count and this will
>> work regardless of the power state when entering this function.
> Ok, but this should equally work:
> 
>  	pm_runtime_disable(&client->dev);
>  	pm_runtime_set_suspended(&client->dev);
>  	
>  	ov7740_set_power(ov7740, 0);
> 
> as there's no additional cleanup made on this particular driver
> between pm_runtime_get_sync() and pm_runtime_put_noidle().
> 

The pm_runtime_get_sync() turns hardware ON by invoking
ov7740_set_power(ov7740, 1), and thus, the ON->OFF is kept balanced in
both RPM-available and RPM-unavailable cases. The RPM state of device
should be reset after driver removal.

It doesn't look like any additional cleanups are needed by that ov7740
driver. The driver removal is opposite to the probe, hence it should be
correct as-is.
_______________________________________________
devel mailing list
devel@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel

^ permalink raw reply	[flat|nested] 28+ messages in thread

* Re: [PATCH v4 25/79] staging: media: tegra-vde: use pm_runtime_resume_and_get()
  2021-04-28 14:51 ` [PATCH v4 25/79] staging: media: tegra-vde: " Mauro Carvalho Chehab
@ 2021-04-29 12:38   ` Dmitry Osipenko
  2021-04-30 17:08   ` Jonathan Cameron
  1 sibling, 0 replies; 28+ messages in thread
From: Dmitry Osipenko @ 2021-04-29 12:38 UTC (permalink / raw)
  To: Mauro Carvalho Chehab
  Cc: devel, Greg Kroah-Hartman, linuxarm, Jonathan Hunter,
	linux-tegra, Thierry Reding, mauro.chehab, Mauro Carvalho Chehab,
	linux-kernel, linux-media

28.04.2021 17:51, Mauro Carvalho Chehab пишет:
> @@ -1069,11 +1071,17 @@ static int tegra_vde_probe(struct platform_device *pdev)
>  	 * power-cycle it in order to put hardware into a predictable lower
>  	 * power state.
>  	 */
> -	pm_runtime_get_sync(dev);
> +	if (pm_runtime_resume_and_get(dev) < 0)
> +		goto err_pm_runtime;
> +
>  	pm_runtime_put(dev);
>  
>  	return 0;
>  
> +err_pm_runtime:
> +	pm_runtime_dont_use_autosuspend(dev);
> +	pm_runtime_disable(dev);
> +
>  err_deinit_iommu:
>  	tegra_vde_iommu_deinit(vde);

This is incorrect error unwinding, the miscdev isn't unregistered.
_______________________________________________
devel mailing list
devel@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel

^ permalink raw reply	[flat|nested] 28+ messages in thread

* Re: [PATCH v4 00/79] Address some issues with PM runtime at media subsystem
  2021-04-29 10:18   ` Mauro Carvalho Chehab
  2021-04-29 12:33     ` Dmitry Osipenko
@ 2021-04-29 15:17     ` Johan Hovold
  1 sibling, 0 replies; 28+ messages in thread
From: Johan Hovold @ 2021-04-29 15:17 UTC (permalink / raw)
  To: Mauro Carvalho Chehab
  Cc: Ricardo Ribalda, Dafna Hirschfeld, Heiko Stuebner, mauro.chehab,
	linuxarm, Todor Tomov, Bjorn Andersson, Andrzej Hajda, Lad,
	Prabhakar, Thierry Reding, Robert Foss, Sylwester Nawrocki,
	Dmitry Osipenko, linux-stm32, Andrzej Pietrasiewicz, Leon Luo,
	Dan Scally, linux-samsung-soc, Dave Stevenson,
	Manivannan Sadhasivam, Chen-Yu Tsai, Krzysztof Kozlowski,
	Jonathan Hunter, linux-rockchip, Matt Ranostay, Andy Gross,
	Dongchun Zhu, Maxime Coquelin, Steve Longerbeam, Bingbu Cao,
	Marek Szyprowski, Shunqian Zheng, Tianshu Qiu, NXP Linux Team,
	Shawn Tu, devel, Jacopo Mondi, linux-tegra, Sakari Ailus,
	Wenyou Yang, Alexandre Torgue, linux-arm-msm, Sascha Hauer,
	linux-renesas-soc, Maxime Ripard, Stanimir Varbanov,
	Benoit Parrot, Helen Koike, Yong Zhi, linux-mediatek,
	Jacek Anaszewski, Matthias Brugger, Sylwester Nawrocki,
	Mauro Carvalho Chehab, Ezequiel Garcia, Daniele Alessandrelli,
	Chiranjeevi Rapolu, linux-arm-kernel, Jacob Chen, Jernej Skrabec,
	Philipp Zabel, Hyungwoo Yang, linux-kernel, Paul J. Murphy,
	Paul Kocialkowski, Sowjanya Komatineni, Pengutronix Kernel Team,
	linux-media, Shawn Guo

On Thu, Apr 29, 2021 at 12:18:16PM +0200, Mauro Carvalho Chehab wrote:
> Em Wed, 28 Apr 2021 17:50:08 +0200
> Johan Hovold <johan@kernel.org> escreveu:
> 
> > On Wed, Apr 28, 2021 at 04:51:21PM +0200, Mauro Carvalho Chehab wrote:
> 
> > > 1. despite its name, this is actually a PM runtime resume call,
> > >    but some developers didn't seem to realize that, as I got this
> > >    pattern on some drivers:
> > > 
> > >         pm_runtime_get_sync(&client->dev);
> > >         pm_runtime_disable(&client->dev);
> > >         pm_runtime_set_suspended(&client->dev);
> > >         pm_runtime_put_noidle(&client->dev);
> > > 
> > >    It makes no sense to resume PM just to suspend it again ;-)  
> > 
> > This is perfectly alright. Take a look at ov7740_remove() for example:
> > 
> > 	pm_runtime_get_sync(&client->dev);
> > 	pm_runtime_disable(&client->dev);
> > 	pm_runtime_set_suspended(&client->dev);
> > 	pm_runtime_put_noidle(&client->dev);
> > 	
> > 	ov7740_set_power(ov7740, 0);
> > 
> > There's an explicit power-off after balancing the PM count and this will
> > work regardless of the power state when entering this function.
> 
> Ok, but this should equally work:
> 
>  	pm_runtime_disable(&client->dev);
>  	pm_runtime_set_suspended(&client->dev);
>  	
>  	ov7740_set_power(ov7740, 0);
> 
> as there's no additional cleanup made on this particular driver
> between pm_runtime_get_sync() and pm_runtime_put_noidle().

No, that would break the driver as I pointed out to you yesterday:

	https://lore.kernel.org/r/YImG1klSPkFSaS3a@hovoldconsulting.com

If the device is already suspended when remove is called then you'll
end up with an unbalanced call to ov7740_set_power() that will try to
disable an already disabled clock.

> > So this has nothing to do with pm_runtime_get_sync() per se.
> 
> Yes, but some patches on this series are cleaning up the driver release
> logic.

You mentioned this example as an argument against using
pm_runtime_get_sync(), which I don't think makes sense.

> > > 2. Usual *_get() methods only increment their use count on success,
> > >    but pm_runtime_get_sync() increments it unconditionally. Due to
> > >    that, several drivers were mistakenly not calling
> > >    pm_runtime_put_noidle() when it fails;  
> > 
> > Sure, but pm_runtime_get_async() also works this way. You just won't be
> > notified if the async resume fails.
> 
> Granted, it makes sense along the pm_runtime kAPI.
> 
> It is inconsistent with the behavior of kobject_get*() and other
> *_get*() methods that are based or inspired on it, as, on those, the
> operations are atomic: either everything succeeds and it doesn't return
> an error, or the usage counter is not incremented and the object
> state doesn't change after the call.

Right, and I'm aware that some people have overlooked this. But its not
the end of the world since hardly any driver can handle resume failures
properly anyway. 

This is mostly just an exercise to shut up static checkers.

> > > 3. The name of the new variant is a lot clearer:
> > > 	pm_runtime_resume_and_get()
> > >     As its same clearly says that this is a PM runtime resume function,
> > >     that also increments the usage counter on success;  
> > 
> > It also introduced an inconsistency in the API and does not pair as well
> > with the pm_runtime_put variants.
> 
> Agreed. A name that would be more consistent with PM runtime would
> probably be:
> 
> 	pm_runtime_resume_if_get()

Naw, since the get part always succeeds.

It should start with pm_runtime_get, but pm_runtime_get_sync() is
unfortunately taken.

> as there are already:
> 
> 	pm_runtime_get_if_in_use()
> 	pm_runtime_get_if_active()
> 
> But any such discussions are out of the scope of this patchset ;-)

Right.

> > > 4. Consistency: we did similar changes subsystem wide with
> > >    for instance strlcpy() and strcpy() that got replaced by
> > >    strscpy(). Having all drivers using the same known-to-be-safe
> > >    methods is a good thing;  
> > 
> > It's not known to be safe; there are ways to get also this interface
> > wrong as for example this series has shown.
> 
> Very true. Yet, it is a lot simpler to use functions that won't change
> the state of the objects when returning an error, as this is by far
> the most common pattern within the Kernel.

A resume failure does change the state (and needs to be recovered from),
but I get what you're saying.

> Human brains are trained to identify certain patterns. When there's
> something using a similar pattern, but with a different behavior, 
> our brains are more subject to fail identifying problems.

Sure. But I'm not sure that having two interfaces with different
semantics to do the job is doing us any favours here. But again, that
discussion has already been had.

And I realise that this is partly also your motive here (even if the old
interface isn't going to go away).

> > > compile-tested only.
> > > Patches 1 to 7 fix some issues that already exists at the current
> > > PM runtime code;
> > > 
> > > patches 8 to 20 fix some usage_count problems that still exists
> > > at the media subsystem;
> > > 
> > > patches 21 to 78 repaces pm_runtime_get_sync() by 
> > > pm_runtime_resume_and_get();
> > > 
> > > Patch 79 (and a hunk on patch 78) documents the two exceptions
> > > where pm_runtime_get_sync() will still be used for now.

80 patches in one series (posted to lkml) is a bit excessive. Perhaps
you can break it up in a fixes part and one or more cleanups parts?

Johan
_______________________________________________
devel mailing list
devel@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel

^ permalink raw reply	[flat|nested] 28+ messages in thread

* Re: [PATCH v4 21/79] staging: media: atomisp: use pm_runtime_resume_and_get()
  2021-04-28 14:51 ` [PATCH v4 21/79] staging: media: atomisp: use pm_runtime_resume_and_get() Mauro Carvalho Chehab
@ 2021-04-30 16:59   ` Jonathan Cameron
  0 siblings, 0 replies; 28+ messages in thread
From: Jonathan Cameron @ 2021-04-30 16:59 UTC (permalink / raw)
  To: Mauro Carvalho Chehab
  Cc: devel, Greg Kroah-Hartman, linuxarm, linux-kernel, Sakari Ailus,
	mauro.chehab, Mauro Carvalho Chehab, linux-media

On Wed, 28 Apr 2021 16:51:42 +0200
Mauro Carvalho Chehab <mchehab+huawei@kernel.org> wrote:

> Commit dd8088d5a896 ("PM: runtime: Add pm_runtime_resume_and_get to deal with usage counter")
> added pm_runtime_resume_and_get() in order to automatically handle
> dev->power.usage_count decrement on errors.
> 
> Use the new API, in order to cleanup the error check logic.

I'd mention that the origin error handling order was wrong and you've
also fixed that by moving hm_pool_unregister() later.

> 
> Signed-off-by: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>
> ---
>  drivers/staging/media/atomisp/pci/atomisp_fops.c | 6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/staging/media/atomisp/pci/atomisp_fops.c b/drivers/staging/media/atomisp/pci/atomisp_fops.c
> index f1e6b2597853..26d05474a035 100644
> --- a/drivers/staging/media/atomisp/pci/atomisp_fops.c
> +++ b/drivers/staging/media/atomisp/pci/atomisp_fops.c
> @@ -837,7 +837,7 @@ static int atomisp_open(struct file *file)
>  	}
>  
>  	/* runtime power management, turn on ISP */
> -	ret = pm_runtime_get_sync(vdev->v4l2_dev->dev);
> +	ret = pm_runtime_resume_and_get(vdev->v4l2_dev->dev);
>  	if (ret < 0) {
>  		dev_err(isp->dev, "Failed to power on device\n");
>  		goto error;
> @@ -881,9 +881,9 @@ static int atomisp_open(struct file *file)
>  

>  css_error:
>  	atomisp_css_uninit(isp);
> -error:
> -	hmm_pool_unregister(HMM_POOL_TYPE_DYNAMIC);
>  	pm_runtime_put(vdev->v4l2_dev->dev);
> +error:
> +	hmm_pool_unregister(HMM_POOL_TYPE_DYNAMIC);
>  	rt_mutex_unlock(&isp->mutex);
>  	return ret;
>  }

_______________________________________________
devel mailing list
devel@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel

^ permalink raw reply	[flat|nested] 28+ messages in thread

* Re: [PATCH v4 23/79] staging: media: ipu3: use pm_runtime_resume_and_get()
  2021-04-28 14:51 ` [PATCH v4 23/79] staging: media: ipu3: " Mauro Carvalho Chehab
@ 2021-04-30 17:03   ` Jonathan Cameron
  2021-05-03  9:57     ` Johan Hovold
  0 siblings, 1 reply; 28+ messages in thread
From: Jonathan Cameron @ 2021-04-30 17:03 UTC (permalink / raw)
  To: Mauro Carvalho Chehab
  Cc: devel, Greg Kroah-Hartman, linuxarm, linux-kernel, Sakari Ailus,
	mauro.chehab, Bingbu Cao, Mauro Carvalho Chehab, Tianshu Qiu,
	linux-media

On Wed, 28 Apr 2021 16:51:44 +0200
Mauro Carvalho Chehab <mchehab+huawei@kernel.org> wrote:

> Commit dd8088d5a896 ("PM: runtime: Add pm_runtime_resume_and_get to deal with usage counter")
> added pm_runtime_resume_and_get() in order to automatically handle
> dev->power.usage_count decrement on errors.
> 
> Use the new API, in order to cleanup the error check logic.
> 
> Signed-off-by: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>
Could add that pm_runtime_put() should have been pm_runtime_put_noidle()
inorder to not potentially result in a call to runtime suspend when
we never resumed in the first place.

Otherwise reasonable cleanup.

Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>

> ---
>  drivers/staging/media/ipu3/ipu3.c | 3 +--
>  1 file changed, 1 insertion(+), 2 deletions(-)
> 
> diff --git a/drivers/staging/media/ipu3/ipu3.c b/drivers/staging/media/ipu3/ipu3.c
> index ee1bba6bdcac..8e1e9e46e604 100644
> --- a/drivers/staging/media/ipu3/ipu3.c
> +++ b/drivers/staging/media/ipu3/ipu3.c
> @@ -392,10 +392,9 @@ int imgu_s_stream(struct imgu_device *imgu, int enable)
>  	}
>  
>  	/* Set Power */
> -	r = pm_runtime_get_sync(dev);
> +	r = pm_runtime_resume_and_get(dev);
>  	if (r < 0) {
>  		dev_err(dev, "failed to set imgu power\n");
> -		pm_runtime_put(dev);
>  		return r;
>  	}
>  

_______________________________________________
devel mailing list
devel@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel

^ permalink raw reply	[flat|nested] 28+ messages in thread

* Re: [PATCH v4 24/79] staging: media: cedrus_video: use pm_runtime_resume_and_get()
  2021-04-28 14:51 ` [PATCH v4 24/79] staging: media: cedrus_video: " Mauro Carvalho Chehab
@ 2021-04-30 17:05   ` Jonathan Cameron
  0 siblings, 0 replies; 28+ messages in thread
From: Jonathan Cameron @ 2021-04-30 17:05 UTC (permalink / raw)
  To: Mauro Carvalho Chehab
  Cc: devel, Jernej Skrabec, Greg Kroah-Hartman, linuxarm,
	Maxime Ripard, linux-kernel, Paul Kocialkowski, Chen-Yu Tsai,
	mauro.chehab, Mauro Carvalho Chehab, linux-arm-kernel,
	linux-media

On Wed, 28 Apr 2021 16:51:45 +0200
Mauro Carvalho Chehab <mchehab+huawei@kernel.org> wrote:

> Commit dd8088d5a896 ("PM: runtime: Add pm_runtime_resume_and_get to deal with usage counter")
> added pm_runtime_resume_and_get() in order to automatically handle
> dev->power.usage_count decrement on errors.
> 
> Use the new API, in order to cleanup the error check logic.
> 
> Signed-off-by: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>
Acked-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>

> ---
>  drivers/staging/media/sunxi/cedrus/cedrus_video.c | 6 ++----
>  1 file changed, 2 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/staging/media/sunxi/cedrus/cedrus_video.c b/drivers/staging/media/sunxi/cedrus/cedrus_video.c
> index b62eb8e84057..9ddd789d0b1f 100644
> --- a/drivers/staging/media/sunxi/cedrus/cedrus_video.c
> +++ b/drivers/staging/media/sunxi/cedrus/cedrus_video.c
> @@ -490,11 +490,9 @@ static int cedrus_start_streaming(struct vb2_queue *vq, unsigned int count)
>  	}
>  
>  	if (V4L2_TYPE_IS_OUTPUT(vq->type)) {
> -		ret = pm_runtime_get_sync(dev->dev);
> -		if (ret < 0) {
> -			pm_runtime_put_noidle(dev->dev);
> +		ret = pm_runtime_resume_and_get(dev->dev);
> +		if (ret < 0)
>  			goto err_cleanup;
> -		}
>  
>  		if (dev->dec_ops[ctx->current_codec]->start) {
>  			ret = dev->dec_ops[ctx->current_codec]->start(ctx);

_______________________________________________
devel mailing list
devel@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel

^ permalink raw reply	[flat|nested] 28+ messages in thread

* Re: [PATCH v4 25/79] staging: media: tegra-vde: use pm_runtime_resume_and_get()
  2021-04-28 14:51 ` [PATCH v4 25/79] staging: media: tegra-vde: " Mauro Carvalho Chehab
  2021-04-29 12:38   ` Dmitry Osipenko
@ 2021-04-30 17:08   ` Jonathan Cameron
  2021-04-30 17:11     ` Jonathan Cameron
  1 sibling, 1 reply; 28+ messages in thread
From: Jonathan Cameron @ 2021-04-30 17:08 UTC (permalink / raw)
  To: Mauro Carvalho Chehab
  Cc: devel, Greg Kroah-Hartman, linuxarm, Jonathan Hunter,
	linux-tegra, Thierry Reding, mauro.chehab, Dmitry Osipenko,
	Mauro Carvalho Chehab, linux-kernel, linux-media

On Wed, 28 Apr 2021 16:51:46 +0200
Mauro Carvalho Chehab <mchehab+huawei@kernel.org> wrote:

> Commit dd8088d5a896 ("PM: runtime: Add pm_runtime_resume_and_get to deal with usage counter")
> added pm_runtime_resume_and_get() in order to automatically handle
> dev->power.usage_count decrement on errors.
> 
> Use the new API, in order to cleanup the error check logic.
> 
> Signed-off-by: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>
LGTM

Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>

> ---
>  drivers/staging/media/tegra-vde/vde.c | 19 ++++++++++++++++---
>  1 file changed, 16 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/staging/media/tegra-vde/vde.c b/drivers/staging/media/tegra-vde/vde.c
> index 28845b5bafaf..1cdacb3f781c 100644
> --- a/drivers/staging/media/tegra-vde/vde.c
> +++ b/drivers/staging/media/tegra-vde/vde.c
> @@ -775,9 +775,9 @@ static int tegra_vde_ioctl_decode_h264(struct tegra_vde *vde,
>  	if (ret)
>  		goto release_dpb_frames;
>  
> -	ret = pm_runtime_get_sync(dev);
> +	ret = pm_runtime_resume_and_get(dev);
>  	if (ret < 0)
> -		goto put_runtime_pm;
> +		goto unlock;
>  
>  	/*
>  	 * We rely on the VDE registers reset value, otherwise VDE
> @@ -843,6 +843,8 @@ static int tegra_vde_ioctl_decode_h264(struct tegra_vde *vde,
>  put_runtime_pm:
>  	pm_runtime_mark_last_busy(dev);
>  	pm_runtime_put_autosuspend(dev);
> +
> +unlock:
>  	mutex_unlock(&vde->lock);
>  
>  release_dpb_frames:
> @@ -1069,11 +1071,17 @@ static int tegra_vde_probe(struct platform_device *pdev)
>  	 * power-cycle it in order to put hardware into a predictable lower
>  	 * power state.
>  	 */
> -	pm_runtime_get_sync(dev);
> +	if (pm_runtime_resume_and_get(dev) < 0)
> +		goto err_pm_runtime;
> +
>  	pm_runtime_put(dev);
>  
>  	return 0;
>  
> +err_pm_runtime:
> +	pm_runtime_dont_use_autosuspend(dev);
> +	pm_runtime_disable(dev);
> +
>  err_deinit_iommu:
>  	tegra_vde_iommu_deinit(vde);
>  
> @@ -1089,7 +1097,12 @@ static int tegra_vde_remove(struct platform_device *pdev)
>  	struct tegra_vde *vde = platform_get_drvdata(pdev);
>  	struct device *dev = &pdev->dev;
>  
> +	/*
> +	 * As it increments RPM usage_count even on errors, we don't need to
> +	 * check the returned code here.
> +	 */
>  	pm_runtime_get_sync(dev);
> +
>  	pm_runtime_dont_use_autosuspend(dev);
>  	pm_runtime_disable(dev);
>  

_______________________________________________
devel mailing list
devel@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel

^ permalink raw reply	[flat|nested] 28+ messages in thread

* Re: [PATCH v4 25/79] staging: media: tegra-vde: use pm_runtime_resume_and_get()
  2021-04-30 17:08   ` Jonathan Cameron
@ 2021-04-30 17:11     ` Jonathan Cameron
  0 siblings, 0 replies; 28+ messages in thread
From: Jonathan Cameron @ 2021-04-30 17:11 UTC (permalink / raw)
  To: Mauro Carvalho Chehab
  Cc: devel, Greg Kroah-Hartman, linuxarm, Jonathan Hunter,
	linux-tegra, Thierry Reding, mauro.chehab, Dmitry Osipenko,
	Mauro Carvalho Chehab, linux-kernel, linux-media

On Fri, 30 Apr 2021 18:08:36 +0100
Jonathan Cameron <Jonathan.Cameron@Huawei.com> wrote:

> On Wed, 28 Apr 2021 16:51:46 +0200
> Mauro Carvalho Chehab <mchehab+huawei@kernel.org> wrote:
> 
> > Commit dd8088d5a896 ("PM: runtime: Add pm_runtime_resume_and_get to deal with usage counter")
> > added pm_runtime_resume_and_get() in order to automatically handle
> > dev->power.usage_count decrement on errors.
> > 
> > Use the new API, in order to cleanup the error check logic.
> > 
> > Signed-off-by: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>
> LGTM
> 
> Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
drop that.  I missed the misc unwind thing caught in the other review.
Too many patches without a break :(
> 
> > ---
> >  drivers/staging/media/tegra-vde/vde.c | 19 ++++++++++++++++---
> >  1 file changed, 16 insertions(+), 3 deletions(-)
> > 
> > diff --git a/drivers/staging/media/tegra-vde/vde.c b/drivers/staging/media/tegra-vde/vde.c
> > index 28845b5bafaf..1cdacb3f781c 100644
> > --- a/drivers/staging/media/tegra-vde/vde.c
> > +++ b/drivers/staging/media/tegra-vde/vde.c
> > @@ -775,9 +775,9 @@ static int tegra_vde_ioctl_decode_h264(struct tegra_vde *vde,
> >  	if (ret)
> >  		goto release_dpb_frames;
> >  
> > -	ret = pm_runtime_get_sync(dev);
> > +	ret = pm_runtime_resume_and_get(dev);
> >  	if (ret < 0)
> > -		goto put_runtime_pm;
> > +		goto unlock;
> >  
> >  	/*
> >  	 * We rely on the VDE registers reset value, otherwise VDE
> > @@ -843,6 +843,8 @@ static int tegra_vde_ioctl_decode_h264(struct tegra_vde *vde,
> >  put_runtime_pm:
> >  	pm_runtime_mark_last_busy(dev);
> >  	pm_runtime_put_autosuspend(dev);
> > +
> > +unlock:
> >  	mutex_unlock(&vde->lock);
> >  
> >  release_dpb_frames:
> > @@ -1069,11 +1071,17 @@ static int tegra_vde_probe(struct platform_device *pdev)
> >  	 * power-cycle it in order to put hardware into a predictable lower
> >  	 * power state.
> >  	 */
> > -	pm_runtime_get_sync(dev);
> > +	if (pm_runtime_resume_and_get(dev) < 0)
> > +		goto err_pm_runtime;
> > +
> >  	pm_runtime_put(dev);
> >  
> >  	return 0;
> >  
> > +err_pm_runtime:
> > +	pm_runtime_dont_use_autosuspend(dev);
> > +	pm_runtime_disable(dev);
> > +
> >  err_deinit_iommu:
> >  	tegra_vde_iommu_deinit(vde);
> >  
> > @@ -1089,7 +1097,12 @@ static int tegra_vde_remove(struct platform_device *pdev)
> >  	struct tegra_vde *vde = platform_get_drvdata(pdev);
> >  	struct device *dev = &pdev->dev;
> >  
> > +	/*
> > +	 * As it increments RPM usage_count even on errors, we don't need to
> > +	 * check the returned code here.
> > +	 */
> >  	pm_runtime_get_sync(dev);
> > +
> >  	pm_runtime_dont_use_autosuspend(dev);
> >  	pm_runtime_disable(dev);
> >  
> 

_______________________________________________
devel mailing list
devel@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel

^ permalink raw reply	[flat|nested] 28+ messages in thread

* Re: [PATCH v4 26/79] staging: media: tegra-video: use pm_runtime_resume_and_get()
  2021-04-28 14:51 ` [PATCH v4 26/79] staging: media: tegra-video: " Mauro Carvalho Chehab
@ 2021-04-30 17:13   ` Jonathan Cameron
  0 siblings, 0 replies; 28+ messages in thread
From: Jonathan Cameron @ 2021-04-30 17:13 UTC (permalink / raw)
  To: Mauro Carvalho Chehab
  Cc: devel, Greg Kroah-Hartman, linuxarm, Jonathan Hunter,
	linux-tegra, Thierry Reding, Sowjanya Komatineni, mauro.chehab,
	Mauro Carvalho Chehab, linux-kernel, linux-media

On Wed, 28 Apr 2021 16:51:47 +0200
Mauro Carvalho Chehab <mchehab+huawei@kernel.org> wrote:

> Commit dd8088d5a896 ("PM: runtime: Add pm_runtime_resume_and_get to deal with usage counter")
> added pm_runtime_resume_and_get() in order to automatically handle
> dev->power.usage_count decrement on errors.
> 
> Use the new API, in order to cleanup the error check logic.
> 
> Signed-off-by: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>
NOP patch so 
Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
> ---
>  drivers/staging/media/tegra-video/csi.c | 3 +--
>  drivers/staging/media/tegra-video/vi.c  | 3 +--
>  2 files changed, 2 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/staging/media/tegra-video/csi.c b/drivers/staging/media/tegra-video/csi.c
> index 033a6935c26d..e938bf4c48b6 100644
> --- a/drivers/staging/media/tegra-video/csi.c
> +++ b/drivers/staging/media/tegra-video/csi.c
> @@ -298,10 +298,9 @@ static int tegra_csi_enable_stream(struct v4l2_subdev *subdev)
>  	struct tegra_csi *csi = csi_chan->csi;
>  	int ret, err;
>  
> -	ret = pm_runtime_get_sync(csi->dev);
> +	ret = pm_runtime_resume_and_get(csi->dev);
>  	if (ret < 0) {
>  		dev_err(csi->dev, "failed to get runtime PM: %d\n", ret);
> -		pm_runtime_put_noidle(csi->dev);
>  		return ret;
>  	}
>  
> diff --git a/drivers/staging/media/tegra-video/vi.c b/drivers/staging/media/tegra-video/vi.c
> index 7a09061cda57..1298740a9c6c 100644
> --- a/drivers/staging/media/tegra-video/vi.c
> +++ b/drivers/staging/media/tegra-video/vi.c
> @@ -297,10 +297,9 @@ static int tegra_channel_start_streaming(struct vb2_queue *vq, u32 count)
>  	struct tegra_vi_channel *chan = vb2_get_drv_priv(vq);
>  	int ret;
>  
> -	ret = pm_runtime_get_sync(chan->vi->dev);
> +	ret = pm_runtime_resume_and_get(chan->vi->dev);
>  	if (ret < 0) {
>  		dev_err(chan->vi->dev, "failed to get runtime PM: %d\n", ret);
> -		pm_runtime_put_noidle(chan->vi->dev);
>  		return ret;
>  	}
>  

_______________________________________________
devel mailing list
devel@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel

^ permalink raw reply	[flat|nested] 28+ messages in thread

* Re: [PATCH v4 78/79] media: hantro: use pm_runtime_resume_and_get()
  2021-04-28 14:52 ` [PATCH v4 78/79] media: hantro: " Mauro Carvalho Chehab
  2021-04-28 17:14   ` Ezequiel Garcia
@ 2021-04-30 18:09   ` Jonathan Cameron
  1 sibling, 0 replies; 28+ messages in thread
From: Jonathan Cameron @ 2021-04-30 18:09 UTC (permalink / raw)
  To: Mauro Carvalho Chehab
  Cc: devel, Greg Kroah-Hartman, linuxarm, linux-kernel,
	linux-rockchip, Philipp Zabel, mauro.chehab,
	Mauro Carvalho Chehab, Ezequiel Garcia, linux-media

On Wed, 28 Apr 2021 16:52:39 +0200
Mauro Carvalho Chehab <mchehab+huawei@kernel.org> wrote:

> Commit dd8088d5a896 ("PM: runtime: Add pm_runtime_resume_and_get to deal with usage counter")
> added pm_runtime_resume_and_get() in order to automatically handle
> dev->power.usage_count decrement on errors.
> 
> While there's nothing wrong with the current usage on this driver,
> as we're getting rid of the pm_runtime_get_sync() call all over
> the media subsystem, let's remove the last occurrence on this
> driver.

Not sure there is nothing wrong in here before your patch...


> 
> Signed-off-by: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>
> ---
>  drivers/staging/media/hantro/hantro_drv.c | 23 ++++++++++++++++-------
>  1 file changed, 16 insertions(+), 7 deletions(-)
> 
> diff --git a/drivers/staging/media/hantro/hantro_drv.c b/drivers/staging/media/hantro/hantro_drv.c
> index 595e82a82728..25fa36e7e773 100644
> --- a/drivers/staging/media/hantro/hantro_drv.c
> +++ b/drivers/staging/media/hantro/hantro_drv.c
> @@ -56,14 +56,12 @@ dma_addr_t hantro_get_ref(struct hantro_ctx *ctx, u64 ts)
>  	return hantro_get_dec_buf_addr(ctx, buf);
>  }
>  
> -static void hantro_job_finish(struct hantro_dev *vpu,
> -			      struct hantro_ctx *ctx,
> -			      enum vb2_buffer_state result)
> +static void hantro_job_finish_no_pm(struct hantro_dev *vpu,
> +				    struct hantro_ctx *ctx,
> +				    enum vb2_buffer_state result)
>  {
>  	struct vb2_v4l2_buffer *src, *dst;
>  
> -	pm_runtime_mark_last_busy(vpu->dev);
> -	pm_runtime_put_autosuspend(vpu->dev);
>  	clk_bulk_disable(vpu->variant->num_clocks, vpu->clocks);
>  
>  	src = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
> @@ -81,6 +79,16 @@ static void hantro_job_finish(struct hantro_dev *vpu,
>  					 result);
>  }
>  
> +static void hantro_job_finish(struct hantro_dev *vpu,
> +			      struct hantro_ctx *ctx,
> +			      enum vb2_buffer_state result)
> +{
> +	pm_runtime_mark_last_busy(vpu->dev);
> +	pm_runtime_put_autosuspend(vpu->dev);
> +
> +	hantro_job_finish_no_pm(vpu, ctx, result);
> +}
> +
>  void hantro_irq_done(struct hantro_dev *vpu,
>  		     enum vb2_buffer_state result)
>  {
> @@ -155,7 +163,8 @@ static void device_run(void *priv)
>  	ret = clk_bulk_enable(ctx->dev->variant->num_clocks, ctx->dev->clocks);
>  	if (ret)
>  		goto err_cancel_job;

Before your patch, if this error condition happened, we'd call runtime_put
before the related runtime_get...  You fixed that, but the patch description
doesn't call it out.


> -	ret = pm_runtime_get_sync(ctx->dev->dev);
> +
> +	ret = pm_runtime_resume_and_get(ctx->dev->dev);
>  	if (ret < 0)
>  		goto err_cancel_job;
>  
> @@ -165,7 +174,7 @@ static void device_run(void *priv)
>  	return;
>  
>  err_cancel_job:
> -	hantro_job_finish(ctx->dev, ctx, VB2_BUF_STATE_ERROR);
> +	hantro_job_finish_no_pm(ctx->dev, ctx, VB2_BUF_STATE_ERROR);
>  }
>  
>  static struct v4l2_m2m_ops vpu_m2m_ops = {

_______________________________________________
devel mailing list
devel@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel

^ permalink raw reply	[flat|nested] 28+ messages in thread

* Re: [PATCH v4 23/79] staging: media: ipu3: use pm_runtime_resume_and_get()
  2021-04-30 17:03   ` Jonathan Cameron
@ 2021-05-03  9:57     ` Johan Hovold
  0 siblings, 0 replies; 28+ messages in thread
From: Johan Hovold @ 2021-05-03  9:57 UTC (permalink / raw)
  To: Jonathan Cameron
  Cc: devel, Mauro Carvalho Chehab, Greg Kroah-Hartman, linuxarm,
	linux-kernel, Sakari Ailus, mauro.chehab, Bingbu Cao,
	Mauro Carvalho Chehab, Tianshu Qiu, linux-media

On Fri, Apr 30, 2021 at 06:03:38PM +0100, Jonathan Cameron wrote:
> On Wed, 28 Apr 2021 16:51:44 +0200
> Mauro Carvalho Chehab <mchehab+huawei@kernel.org> wrote:
> 
> > Commit dd8088d5a896 ("PM: runtime: Add pm_runtime_resume_and_get to deal with usage counter")
> > added pm_runtime_resume_and_get() in order to automatically handle
> > dev->power.usage_count decrement on errors.
> > 
> > Use the new API, in order to cleanup the error check logic.
> > 
> > Signed-off-by: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>
> Could add that pm_runtime_put() should have been pm_runtime_put_noidle()
> inorder to not potentially result in a call to runtime suspend when
> we never resumed in the first place.

No, that would never happen anyway and any pm_runtime_put() will do
even if pm_runtime_put_noidle() is the natural choice in this case to
balance the counter.

> Otherwise reasonable cleanup.
> 
> Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
> 
> > ---
> >  drivers/staging/media/ipu3/ipu3.c | 3 +--
> >  1 file changed, 1 insertion(+), 2 deletions(-)
> > 
> > diff --git a/drivers/staging/media/ipu3/ipu3.c b/drivers/staging/media/ipu3/ipu3.c
> > index ee1bba6bdcac..8e1e9e46e604 100644
> > --- a/drivers/staging/media/ipu3/ipu3.c
> > +++ b/drivers/staging/media/ipu3/ipu3.c
> > @@ -392,10 +392,9 @@ int imgu_s_stream(struct imgu_device *imgu, int enable)
> >  	}
> >  
> >  	/* Set Power */
> > -	r = pm_runtime_get_sync(dev);
> > +	r = pm_runtime_resume_and_get(dev);
> >  	if (r < 0) {
> >  		dev_err(dev, "failed to set imgu power\n");
> > -		pm_runtime_put(dev);
> >  		return r;
> >  	}

Johan
_______________________________________________
devel mailing list
devel@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel

^ permalink raw reply	[flat|nested] 28+ messages in thread

end of thread, other threads:[~2021-05-03  9:57 UTC | newest]

Thread overview: 28+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-28 14:51 [PATCH v4 00/79] Address some issues with PM runtime at media subsystem Mauro Carvalho Chehab
2021-04-28 14:51 ` [PATCH v4 20/79] staging: media: rkvdec: fix pm_runtime_get_sync() usage count Mauro Carvalho Chehab
2021-04-28 15:09   ` Johan Hovold
2021-04-29  7:38     ` Mauro Carvalho Chehab
2021-04-28 14:51 ` [PATCH v4 21/79] staging: media: atomisp: use pm_runtime_resume_and_get() Mauro Carvalho Chehab
2021-04-30 16:59   ` Jonathan Cameron
2021-04-28 14:51 ` [PATCH v4 22/79] staging: media: imx7-mipi-csis: " Mauro Carvalho Chehab
2021-04-28 14:51 ` [PATCH v4 23/79] staging: media: ipu3: " Mauro Carvalho Chehab
2021-04-30 17:03   ` Jonathan Cameron
2021-05-03  9:57     ` Johan Hovold
2021-04-28 14:51 ` [PATCH v4 24/79] staging: media: cedrus_video: " Mauro Carvalho Chehab
2021-04-30 17:05   ` Jonathan Cameron
2021-04-28 14:51 ` [PATCH v4 25/79] staging: media: tegra-vde: " Mauro Carvalho Chehab
2021-04-29 12:38   ` Dmitry Osipenko
2021-04-30 17:08   ` Jonathan Cameron
2021-04-30 17:11     ` Jonathan Cameron
2021-04-28 14:51 ` [PATCH v4 26/79] staging: media: tegra-video: " Mauro Carvalho Chehab
2021-04-30 17:13   ` Jonathan Cameron
2021-04-28 14:52 ` [PATCH v4 78/79] media: hantro: " Mauro Carvalho Chehab
2021-04-28 17:14   ` Ezequiel Garcia
2021-04-30 18:09   ` Jonathan Cameron
2021-04-28 14:52 ` [PATCH v4 79/79] media: hantro: do a PM resume earlier Mauro Carvalho Chehab
2021-04-28 17:17   ` Ezequiel Garcia
2021-04-29  7:13     ` Mauro Carvalho Chehab
2021-04-28 15:50 ` [PATCH v4 00/79] Address some issues with PM runtime at media subsystem Johan Hovold
2021-04-29 10:18   ` Mauro Carvalho Chehab
2021-04-29 12:33     ` Dmitry Osipenko
2021-04-29 15:17     ` Johan Hovold

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).