[PATCH v2] drm: Add checks for NULL drm_*_helper_funcs

Haneen Mohammed hamohammed.sa at gmail.com
Wed May 30 00:42:59 UTC 2018


This patch add checks for NULL drm_[connector/crtc/plane]_helper_funcs
pointers before derefrencing the variable to avoid NULL pointer
dereference and make the helper functions optional in the following
cases:

1) when using drm_helper_probe_single_connector_modes for fill_modes
callback in the drm_connector_funcs.
2) using drm_atomic_helper_[check/commit] for atomic_[check/commit]
callbacks in the drm_mode_config_funcs.

The dependency is as follow:

drm_connector_funcs:
* fill_modes = drm_helper_probe_single_connector_modes
	b) drm_helper_probe_detect -> drm_connector_helper_funcs

drm_mode_config_funcs:
* atomic_check = drm_atomic_helper_check
	a) drm_atomic_helper_check_modeset -> drm_connector helper_funcs
	a) handle_conflicting_encoders -> drm_connector_helper_funcs
	b) mode_fixup -> drm_crtc_helper_funcs
	c) update_connector_routing -> drm_connectot_helper_funcs

* atomic_commit = drm_atomic_helper_commit
	a) drm_atomic_helper_prepare_planes -> drm_plane_helper_funcs
	b) drm_atomic_helper_cleanup_planes -> drm_plane_helper_funcs

Signed-off-by: Haneen Mohammed <hamohammed.sa at gmail.com>
---
Chanes in v2:
- add the least amount of checks to make the helpers optional for vkms

 drivers/gpu/drm/drm_atomic_helper.c | 16 ++++++++--------
 drivers/gpu/drm/drm_probe_helper.c  |  5 +++--
 2 files changed, 11 insertions(+), 10 deletions(-)

diff --git a/drivers/gpu/drm/drm_atomic_helper.c b/drivers/gpu/drm/drm_atomic_helper.c
index 2a7ceca82b9e..ff3684c4a83c 100644
--- a/drivers/gpu/drm/drm_atomic_helper.c
+++ b/drivers/gpu/drm/drm_atomic_helper.c
@@ -112,9 +112,9 @@ static int handle_conflicting_encoders(struct drm_atomic_state *state,
 		if (!new_conn_state->crtc)
 			continue;
 
-		if (funcs->atomic_best_encoder)
+		if (funcs && funcs->atomic_best_encoder)
 			new_encoder = funcs->atomic_best_encoder(connector, new_conn_state);
-		else if (funcs->best_encoder)
+		else if (funcs && funcs->best_encoder)
 			new_encoder = funcs->best_encoder(connector);
 		else
 			new_encoder = drm_atomic_helper_best_encoder(connector);
@@ -308,10 +308,10 @@ update_connector_routing(struct drm_atomic_state *state,
 
 	funcs = connector->helper_private;
 
-	if (funcs->atomic_best_encoder)
+	if (funcs && funcs->atomic_best_encoder)
 		new_encoder = funcs->atomic_best_encoder(connector,
 							 new_connector_state);
-	else if (funcs->best_encoder)
+	else if (funcs && funcs->best_encoder)
 		new_encoder = funcs->best_encoder(connector);
 	else
 		new_encoder = drm_atomic_helper_best_encoder(connector);
@@ -438,7 +438,7 @@ mode_fixup(struct drm_atomic_state *state)
 			continue;
 
 		funcs = crtc->helper_private;
-		if (!funcs->mode_fixup)
+		if (!funcs || !funcs->mode_fixup)
 			continue;
 
 		ret = funcs->mode_fixup(crtc, &new_crtc_state->mode,
@@ -639,7 +639,7 @@ drm_atomic_helper_check_modeset(struct drm_device *dev,
 				new_crtc_state->connectors_changed = true;
 		}
 
-		if (funcs->atomic_check)
+		if (funcs && funcs->atomic_check)
 			ret = funcs->atomic_check(connector, new_connector_state);
 		if (ret)
 			return ret;
@@ -2117,7 +2117,7 @@ int drm_atomic_helper_prepare_planes(struct drm_device *dev,
 
 		funcs = plane->helper_private;
 
-		if (funcs->prepare_fb) {
+		if (funcs && funcs->prepare_fb) {
 			ret = funcs->prepare_fb(plane, new_plane_state);
 			if (ret)
 				goto fail;
@@ -2412,7 +2412,7 @@ void drm_atomic_helper_cleanup_planes(struct drm_device *dev,
 
 		funcs = plane->helper_private;
 
-		if (funcs->cleanup_fb)
+		if (funcs && funcs->cleanup_fb)
 			funcs->cleanup_fb(plane, plane_state);
 	}
 }
diff --git a/drivers/gpu/drm/drm_probe_helper.c b/drivers/gpu/drm/drm_probe_helper.c
index 527743394150..2fea54d16ebb 100644
--- a/drivers/gpu/drm/drm_probe_helper.c
+++ b/drivers/gpu/drm/drm_probe_helper.c
@@ -320,7 +320,7 @@ drm_helper_probe_detect(struct drm_connector *connector,
 	if (ret)
 		return ret;
 
-	if (funcs->detect_ctx)
+	if (funcs && funcs->detect_ctx)
 		return funcs->detect_ctx(connector, ctx, force);
 	else if (connector->funcs->detect)
 		return connector->funcs->detect(connector, force);
@@ -480,7 +480,8 @@ int drm_helper_probe_single_connector_modes(struct drm_connector *connector,
 		goto prune;
 	}
 
-	count = (*connector_funcs->get_modes)(connector);
+	if (connector_funcs && connector_funcs->get_modes)
+		count = (*connector_funcs->get_modes)(connector);
 
 	if (count == 0 && connector->status == connector_status_connected)
 		count = drm_add_modes_noedid(connector, 1024, 768);
-- 
2.17.0



More information about the dri-devel mailing list