Skip to contentSkip to conversation

Fix memory leak with drmModeGetConnectorCurrent()

PATCH In review [email protected] 2 participants 4 messages Dec 15, 2015 – Jan 7, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Fix memory leak with drmModeGetConnectorCurrent()

From: Ville Syrjälä <[email protected]>

drmModeGetConnectorCurrent() must provide temporary storage for the kernel to fill in at least one mode (asking for !=0 modes is how you prevent the heavyweight probe in the kernel). Currently we malloc that temp storage but we fail to free it before overwriting the pointer with the address of the actual storage we use to store the real mode list we get from the kernel in the second ioctl call.

Let's just keep the temporary storage on the stack and thus we avoid the leak and also eliminate some pointless mallocs.

Cc Chris Wilson <[email protected]>
Fixes 5ed5fa10600f ("mode: Retrieve only the current information for a Connector")
---
 xf86drmMode.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
xf86drmMode.c +7 -4
index ab6b5195e8d3..7710061865ee 100644
@@ -475,12 +475,13 @@ _drmModeGetConnector(int fd, uint32_t connector_id, int probe)
 {
 	struct drm_mode_get_connector conn, counts;
 	drmModeConnectorPtr r = NULL;
+	struct drm_mode_modeinfo stack_mode;
 
 	memclear(conn);
 	conn.connector_id = connector_id;
 	if (!probe) {
 		conn.count_modes = 1;
-		conn.modes_ptr = VOID2U64(drmMalloc(sizeof(struct drm_mode_modeinfo)));
+		conn.modes_ptr = VOID2U64(&stack_mode);
 	}
 
 	if (drmIoctl(fd, DRM_IOCTL_MODE_GETCONNECTOR, &conn))
@@ -504,7 +505,7 @@ retry:
 			goto err_allocs;
 	} else {
 		conn.count_modes = 1;
-		conn.modes_ptr = VOID2U64(drmMalloc(sizeof(struct drm_mode_modeinfo)));
+		conn.modes_ptr = VOID2U64(&stack_mode);
 	}
 
 	if (conn.count_encoders) {
@@ -525,7 +526,8 @@ retry:
 	    counts.count_encoders < conn.count_encoders) {
 		drmFree(U642VOID(conn.props_ptr));
 		drmFree(U642VOID(conn.prop_values_ptr));
-		drmFree(U642VOID(conn.modes_ptr));
+		if (U642VOID(conn.modes_ptr) != &stack_mode)
+			drmFree(U642VOID(conn.modes_ptr));
 		drmFree(U642VOID(conn.encoders_ptr));
 
 		goto retry;
@@ -567,7 +569,8 @@ retry:
 err_allocs:
 	drmFree(U642VOID(conn.prop_values_ptr));
 	drmFree(U642VOID(conn.props_ptr));
-	drmFree(U642VOID(conn.modes_ptr));
+	if (U642VOID(conn.modes_ptr) != &stack_mode)
+		drmFree(U642VOID(conn.modes_ptr));
 	drmFree(U642VOID(conn.encoders_ptr));
 
 	return r;
Signature · 6 lines
2.4.10

_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel
Reviewed-by on v1 1/1
#1 lore raw
Quoted 26 lines · On Tue, Dec 15, 2015 at 03:59:28PM +0200, [email protected] wrote:

From: Ville Syrjälä <[email protected]>

drmModeGetConnectorCurrent() must provide temporary storage for the kernel to fill in at least one mode (asking for !=0 modes is how you prevent the heavyweight probe in the kernel). Currently we malloc that temp storage but we fail to free it before overwriting the pointer with the address of the actual storage we use to store the real mode list we get from the kernel in the second ioctl call.

Let's just keep the temporary storage on the stack and thus we avoid the leak and also eliminate some pointless mallocs.

Cc Chris Wilson <[email protected]>
Fixes 5ed5fa10600f ("mode: Retrieve only the current information for a Connector")
---
 xf86drmMode.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
xf86drmMode.c +0 -0
index ab6b5195e8d3..7710061865ee 100644
@@ -475,12 +475,13 @@ _drmModeGetConnector(int fd, uint32_t connector_id, int probe)
 {
 	struct drm_mode_get_connector conn, counts;

xf86drmMode.c _drmModeGetConnector(int fd, uint32_t connector_id, int probe) · line 485

Quoted 10 lines
xf86drmMode.c +2 -1
 	drmModeConnectorPtr r = NULL;
+	struct drm_mode_modeinfo stack_mode;
 
 	memclear(conn);
 	conn.connector_id = connector_id;
 	if (!probe) {
 		conn.count_modes = 1;
-		conn.modes_ptr = VOID2U64(drmMalloc(sizeof(struct drm_mode_modeinfo)));
+		conn.modes_ptr = VOID2U64(&stack_mode);
 	}

If you just made this change, we wouldn't need the hunks below (and I wouln't have to look at so much shouting).

Either way,

-Chris

Signature · 5 lines
Chris Wilson, Intel Open Source Technology Centre
_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel
#2 lore raw
Quoted 30 lines · Chris Wilson
Quoted 29 lines · On Tue, Dec 15, 2015 at 03:59:28PM +0200, [email protected] wrote:

From: Ville Syrjälä <[email protected]>

drmModeGetConnectorCurrent() must provide temporary storage for the kernel to fill in at least one mode (asking for !=0 modes is how you prevent the heavyweight probe in the kernel). Currently we malloc that temp storage but we fail to free it before overwriting the pointer with the address of the actual storage we use to store the real mode list we get from the kernel in the second ioctl call.

Let's just keep the temporary storage on the stack and thus we avoid the leak and also eliminate some pointless mallocs.

Cc Chris Wilson <[email protected]>
Fixes 5ed5fa10600f ("mode: Retrieve only the current information for a Connector")
---
 xf86drmMode.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
xf86drmMode.c +1 -0
index ab6b5195e8d3..7710061865ee 100644
@@ -475,12 +475,13 @@ _drmModeGetConnector(int fd, uint32_t connector_id, int probe)
 {
 	struct drm_mode_get_connector conn, counts;
 	drmModeConnectorPtr r = NULL;
+	struct drm_mode_modeinfo stack_mode;
 
Quoted 10 lines

xf86drmMode.c _drmModeGetConnector(int fd, uint32_t connector_id, int probe) · line 485

Quoted 7 lines
xf86drmMode.c +1 -1
 	memclear(conn);
 	conn.connector_id = connector_id;
 	if (!probe) {
 		conn.count_modes = 1;
-		conn.modes_ptr = VOID2U64(drmMalloc(sizeof(struct drm_mode_modeinfo)));
+		conn.modes_ptr = VOID2U64(&stack_mode);
 	}

If you just made this change, we wouldn't need the hunks below (and I wouln't have to look at so much shouting).

That was my initial plan, but then I figured we could also skip the other malloc for disconnected connectors.

Quoted 7 lines

Either way,

-Chris

Signature · 1 line
Chris Wilson, Intel Open Source Technology Centre
Signature · 6 lines
Ville Syrjälä
Intel OTC
_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel
author Applied
#3 lore raw
In reply to Chris Wilson
Quoted 41 lines · Chris Wilson
Quoted 26 lines · On Tue, Dec 15, 2015 at 03:59:28PM +0200, [email protected] wrote:

From: Ville Syrjälä <[email protected]>

drmModeGetConnectorCurrent() must provide temporary storage for the kernel to fill in at least one mode (asking for !=0 modes is how you prevent the heavyweight probe in the kernel). Currently we malloc that temp storage but we fail to free it before overwriting the pointer with the address of the actual storage we use to store the real mode list we get from the kernel in the second ioctl call.

Let's just keep the temporary storage on the stack and thus we avoid the leak and also eliminate some pointless mallocs.

Cc Chris Wilson <[email protected]>
Fixes 5ed5fa10600f ("mode: Retrieve only the current information for a Connector")
---
 xf86drmMode.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
xf86drmMode.c +0 -0
index ab6b5195e8d3..7710061865ee 100644
@@ -475,12 +475,13 @@ _drmModeGetConnector(int fd, uint32_t connector_id, int probe)
 {
 	struct drm_mode_get_connector conn, counts;

xf86drmMode.c _drmModeGetConnector(int fd, uint32_t connector_id, int probe) · line 485

Quoted 10 lines
xf86drmMode.c +2 -1
 	drmModeConnectorPtr r = NULL;
+	struct drm_mode_modeinfo stack_mode;
 
 	memclear(conn);
 	conn.connector_id = connector_id;
 	if (!probe) {
 		conn.count_modes = 1;
-		conn.modes_ptr = VOID2U64(drmMalloc(sizeof(struct drm_mode_modeinfo)));
+		conn.modes_ptr = VOID2U64(&stack_mode);
 	}

If you just made this change, we wouldn't need the hunks below (and I wouln't have to look at so much shouting).

Quoted 2 lines

Either way,

Pushed to master. Thanks for the review.

Signature · 6 lines
Ville Syrjälä
Intel OTC
_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel