ingenic_drm_bind() calls drm_atomic_private_obj_init() late in the
bind sequence, without checking its return value.

drm_atomic_private_obj_init() registers the object on the
drm->mode_config.privobj_list and then allocates its initial state
through the atomic_create_state hook. If that allocation fails, it
returns an error but leaves the object registered with a NULL state.
Since the return value isn't checked, the probe would continue and
the first atomic commit would then crash in
drm_atomic_get_private_obj_state() -> ingenic_drm_duplicate_state(),
which does a kmemdup() of that NULL state.

Check the drm_atomic_private_obj_init() return value, and move it and
its drmm cleanup action before drm_mode_config_reset().

This will also be needed when drm_mode_config_reset() gets replaced
by drm_mode_config_create_initial_state(), which creates the initial
state of the drm_private_obj instances registered so far: the private
object will already have its state by then, so
drm_mode_config_create_initial_state() will skip it.

Fixes: 8e46b1ebf3704 ("drm/ingenic: Switch private_obj initialization to 
atomic_create_state")
Reported-by: [email protected]
Link: https://lore.kernel.org/r/[email protected]
Signed-off-by: Maxime Ripard <[email protected]>
---
 drivers/gpu/drm/ingenic/ingenic-drm-drv.c | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)

diff --git a/drivers/gpu/drm/ingenic/ingenic-drm-drv.c 
b/drivers/gpu/drm/ingenic/ingenic-drm-drv.c
index 625ec76cf04d..6acb7d2f25b9 100644
--- a/drivers/gpu/drm/ingenic/ingenic-drm-drv.c
+++ b/drivers/gpu/drm/ingenic/ingenic-drm-drv.c
@@ -1352,10 +1352,20 @@ static int ingenic_drm_bind(struct device *dev, bool 
has_components)
        if (ret) {
                dev_err(dev, "Failed calling drm_vblank_init()\n");
                goto err_drvdata;
        }
 
+       ret = drm_atomic_private_obj_init(drm, &priv->private_obj,
+                                         &ingenic_drm_private_state_funcs);
+       if (ret)
+               goto err_drvdata;
+
+       ret = drmm_add_action_or_reset(drm, ingenic_drm_atomic_private_obj_fini,
+                                      &priv->private_obj);
+       if (ret)
+               goto err_drvdata;
+
        drm_mode_config_reset(drm);
 
        ret = clk_prepare_enable(priv->pix_clk);
        if (ret) {
                dev_err(dev, "Unable to start pixel clock\n");
@@ -1399,18 +1409,10 @@ static int ingenic_drm_bind(struct device *dev, bool 
has_components)
        if (ret) {
                dev_err(dev, "Unable to register clock notifier\n");
                goto err_devclk_disable;
        }
 
-       drm_atomic_private_obj_init(drm, &priv->private_obj,
-                                   &ingenic_drm_private_state_funcs);
-
-       ret = drmm_add_action_or_reset(drm, ingenic_drm_atomic_private_obj_fini,
-                                      &priv->private_obj);
-       if (ret)
-               goto err_clk_notifier_unregister;
-
        ret = drm_dev_register(drm, 0);
        if (ret) {
                dev_err(dev, "Failed to register DRM driver\n");
                goto err_clk_notifier_unregister;
        }

-- 
2.56.1

Reply via email to