Skip to contentSkip to conversation

igt_kms: don't leak the mmap used for cairo

PATCH In review Paulo Zanoni 3 participants 12 messages Dec 30, 2013 – Jan 7, 2014 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/6 igt_kms: don't leak the mmap used for cairo

From: Paulo Zanoni <[email protected]>

When we call kmstest_get_cairo_ctx() and create a context, we do a gem_mmap. The problem is that we lose the mmap pointer, so we leak it. This patch stores the pointer and frees it at kmstest_remove_fb.

Huge test suites like kms_flip do this operation thousands of times, which makes the virtual memory size increase until the test suite gets killed. Today, without this patch, we can't even run 50% of the kms_flip tests due to this problem. To test this, just "./kms_flip", then run "top" and sort it by the VIRT column.

---
 lib/igt_kms.c | 15 ++++++++++-----
 lib/igt_kms.h |  1 +
 2 files changed, 11 insertions(+), 5 deletions(-)
lib/igt_kms.c +10 -5
index 57795b1..f032cd3 100644
@@ -303,6 +303,7 @@ unsigned int kmstest_create_fb(int fd, int width, int height, int bpp,
 	fb->height = height;
 	fb->tiling = tiled;
 	fb->drm_format = bpp_depth_to_drm_format(bpp, depth);
+	fb->fb_ptr = NULL;
 
 	return fb->fb_id;
 }
@@ -353,6 +354,7 @@ unsigned int kmstest_create_fb2(int fd, int width, int height, uint32_t format,
 	fb->tiling = tiled;
 	fb->drm_format = format;
 	fb->fb_id = fb_id;
+	fb->fb_ptr = NULL;
 
 	return fb_id;
 }
@@ -372,13 +374,14 @@ static cairo_surface_t *create_image_surface(int fd, struct kmstest_fb *fb)
 {
 	cairo_surface_t *surface;
 	cairo_format_t cformat;
-	void *fb_ptr;
 
 	cformat = drm_format_to_cairo(fb->drm_format);
-	fb_ptr = gem_mmap(fd, fb->gem_handle, fb->size, PROT_READ | PROT_WRITE);
-	surface = cairo_image_surface_create_for_data((unsigned char *)fb_ptr,
-						   cformat, fb->width,
-						   fb->height, fb->stride);
+	fb->fb_ptr = gem_mmap(fd, fb->gem_handle, fb->size,
+			      PROT_READ | PROT_WRITE);
+	surface = cairo_image_surface_create_for_data(
+						(unsigned char *)fb->fb_ptr,
+						cformat, fb->width,
+						fb->height, fb->stride);
 	assert(surface);
 
 	return surface;
@@ -423,6 +426,8 @@ void kmstest_remove_fb(int fd, struct kmstest_fb *fb)
 {
 	if (fb->cairo_ctx)
 		cairo_destroy(fb->cairo_ctx);
+	if (fb->fb_ptr)
+		munmap(fb->fb_ptr, fb->size);
 	do_or_die(drmModeRmFB(fd, fb->fb_id));
 	gem_close(fd, fb->gem_handle);
 }
lib/igt_kms.h +1 -0
index f61f8e5..f9494d0 100644
@@ -55,6 +55,7 @@ struct kmstest_fb {
 	unsigned tiling;
 	unsigned size;
 	cairo_t *cairo_ctx;
+	void *fb_ptr;
 };
 
 enum kmstest_text_align {
Signature · 1 line
1.8.3.1
#1 lore raw

PATCH 2/6 drmtest: don't leak memory when parsing the arguments

From: Paulo Zanoni <[email protected]>

Found this while investigating memory leaks on kms_flip. Detected by Valgrind.

---
 lib/drmtest.c | 2 ++
 1 file changed, 2 insertions(+)
lib/drmtest.c +2 -0
index 3d79a4d..7275b7f 100644
@@ -911,6 +911,8 @@ int igt_subtest_init_parse_opts(int argc, char **argv,
 	oom_adjust_for_doom();
 
 out:
+	free(short_opts);
+	free(combined_opts);
 	print_version();
 
 	return ret;
Signature · 1 line
1.8.3.1
#2 lore raw

PATCH 3/6 tests/kms_flip: don't leak the connector when setting DPMS

In reply to Paulo Zanoni

From: Paulo Zanoni <[email protected]>

Caught by Valgrind.

---
 tests/kms_flip.c | 2 ++
 1 file changed, 2 insertions(+)
tests/kms_flip.c +2 -0
index eaa42b5..80f4d76 100644
@@ -295,6 +295,8 @@ static void dpms_off_other_outputs(struct test_output *o)
 
 		set_connector_dpms(connector,  DRM_MODE_DPMS_ON);
 		set_connector_dpms(connector,  DRM_MODE_DPMS_OFF);
+
+		drmModeFreeConnector(connector);
 next:
 		;
 	}
Signature · 1 line
1.8.3.1
#3 lore raw

PATCH 4/6 tests/kms_flip: don't leak the CRTC

In reply to Paulo Zanoni

From: Paulo Zanoni <[email protected]>

The kms_flip program calls kmstest_get_connector_config, which returns a struct containing some allocated variables, including a pointer to the CRTC. The problem is that we copy the values returned by this structure to the test_output struct, but we ignore the CRTC pointer. So free the CRTC pointer instead of leaking it.

Caught by Valgrind.

---
 tests/kms_flip.c | 5 +++++
 1 file changed, 5 insertions(+)
tests/kms_flip.c +5 -0
index 80f4d76..78e179e 100644
@@ -921,6 +921,8 @@ static void connector_find_preferred_mode(uint32_t connector_id, int crtc_idx,
 
 	o->fb_width = o->kmode[0].hdisplay;
 	o->fb_height = o->kmode[0].vdisplay;
+
+	drmModeFreeCrtc(config.crtc);
 }
 
 static bool mode_compatible(const drmModeModeInfo *a, const drmModeModeInfo *b)
@@ -986,6 +988,9 @@ found:
 	o->kencoder[1] = config[1].encoder;
 	o->_crtc[1] = config[1].crtc->crtc_id;
 	o->kmode[1] = *mode[1];
+
+	drmModeFreeCrtc(config[0].crtc);
+	drmModeFreeCrtc(config[1].crtc);
 }
 
 static void paint_flip_mode(struct kmstest_fb *fb, bool odd_frame)
Signature · 1 line
1.8.3.1
#4 lore raw

PATCH 5/6 tests/kms_flip: don't leak the connector_config struct

In reply to Paulo Zanoni

From: Paulo Zanoni <[email protected]>

... in case we can't find a compatible mode. We already have config[0], we can't return without freeing it first.

Caught by Valgrind.

---
 tests/kms_flip.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
tests/kms_flip.c +3 -1
index 78e179e..c43a26f 100644
@@ -954,8 +954,10 @@ static void connector_find_compatible_mode(int crtc_idx0, int crtc_idx1,
 		return;
 
 	if (kmstest_get_connector_config(drm_fd, o->_connector[1],
-					 1 << crtc_idx1, &config[1]) < 0)
+					 1 << crtc_idx1, &config[1]) < 0) {
+		kmstest_free_connector_config(&config[0]);
 		return;
+	}
 
 	mode[0] = &config[0].default_mode;
 	mode[1] = &config[1].default_mode;
Signature · 1 line
1.8.3.1
#5 lore raw

PATCH 6/6 tests/kms_flip: free the test_output struct when counting modes

In reply to Paulo Zanoni

From: Paulo Zanoni <[email protected]>

Looks like we have to do a lot of work just to count the number of modes...

Caught by Valgrind.

---
 tests/kms_flip.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)
tests/kms_flip.c +13 -4
index c43a26f..7e5e355 100644
@@ -1129,6 +1129,16 @@ static unsigned event_loop(struct test_output *o, unsigned duration_ms)
 	return end - start;
 }
 
+static void free_test_output(struct test_output *o)
+{
+	int i;
+
+	for (i = 0; i < o->count; i++) {
+		drmModeFreeEncoder(o->kencoder[i]);
+		drmModeFreeConnector(o->kconnector[i]);
+	}
+}
+
 static void run_test_on_crtc_set(struct test_output *o, int *crtc_idxs,
 				 int crtc_count, int duration_ms)
 {
@@ -1231,10 +1241,7 @@ out:
 
 	last_connector = NULL;
 
-	for (i = 0; i < o->count; i++) {
-		drmModeFreeEncoder(o->kencoder[i]);
-		drmModeFreeConnector(o->kconnector[i]);
-	}
+	free_test_output(o);
 }
 
 static int run_test(int duration, int flags)
@@ -1261,6 +1268,7 @@ static int run_test(int duration, int flags)
 			if (o.mode_valid)
 				modes++;
 
+			free_test_output(&o);
 		}
 	}
 
@@ -1318,6 +1326,7 @@ static int run_pair(int duration, int flags)
 					if (o.mode_valid)
 						modes++;
 
+					free_test_output(&o);
 				}
 			}
 		}
Signature · 1 line
1.8.3.1
#6 lore raw

Re: [PATCH 6/6] tests/kms_flip: free the test_output struct when counting modes

Quoted 8 lines · Paulo Zanoni

From: Paulo Zanoni <[email protected]>

Looks like we have to do a lot of work just to count the number of modes...

Caught by Valgrind.

Nice set of patches, please push. I agree that the mode/output config handling in our kms tests is still awful and too verbose, but I didn't come up yet with a good idea ...
-Daniel

Quoted 60 lines
---
 tests/kms_flip.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)
tests/kms_flip.c +13 -4
index c43a26f..7e5e355 100644
@@ -1129,6 +1129,16 @@ static unsigned event_loop(struct test_output *o, unsigned duration_ms)
 	return end - start;
 }
 
+static void free_test_output(struct test_output *o)
+{
+	int i;
+
+	for (i = 0; i < o->count; i++) {
+		drmModeFreeEncoder(o->kencoder[i]);
+		drmModeFreeConnector(o->kconnector[i]);
+	}
+}
+
 static void run_test_on_crtc_set(struct test_output *o, int *crtc_idxs,
 				 int crtc_count, int duration_ms)
 {
@@ -1231,10 +1241,7 @@ out:
 
 	last_connector = NULL;
 
-	for (i = 0; i < o->count; i++) {
-		drmModeFreeEncoder(o->kencoder[i]);
-		drmModeFreeConnector(o->kconnector[i]);
-	}
+	free_test_output(o);
 }
 
 static int run_test(int duration, int flags)
@@ -1261,6 +1268,7 @@ static int run_test(int duration, int flags)
 			if (o.mode_valid)
 				modes++;
 
+			free_test_output(&o);
 		}
 	}
 
@@ -1318,6 +1326,7 @@ static int run_pair(int duration, int flags)
 					if (o.mode_valid)
 						modes++;
 
+					free_test_output(&o);
 				}
 			}
 		}
Signature · 6 lines
1.8.3.1

_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx
Signature · 3 lines
Daniel Vetter
Software Engineer, Intel Corporation
+41 (0) 79 365 57 48 - http://blog.ffwll.ch
#7 lore raw
In reply to Paulo Zanoni
Quoted 11 lines · Paulo Zanoni

From: Paulo Zanoni <[email protected]>

When we call kmstest_get_cairo_ctx() and create a context, we do a gem_mmap. The problem is that we lose the mmap pointer, so we leak it. This patch stores the pointer and frees it at kmstest_remove_fb.

Huge test suites like kms_flip do this operation thousands of times, which makes the virtual memory size increase until the test suite gets killed. Today, without this patch, we can't even run 50% of the kms_flip tests due to this problem. To test this, just "./kms_flip", then run "top" and sort it by the VIRT column.

Bleh. kmstest is a bad example of how to handle cairo object lifetimes and this patch just makes it worse.
-Chris

Signature · 1 line
Chris Wilson, Intel Open Source Technology Centre
#8 lore raw

2013/12/30 Chris Wilson <[email protected]>:

Quoted 7 lines · Paulo Zanoni
Quoted 6 lines

From: Paulo Zanoni <[email protected]>

When we call kmstest_get_cairo_ctx() and create a context, we do a gem_mmap. The problem is that we lose the mmap pointer, so we leak it. This patch stores the pointer and frees it at kmstest_remove_fb.

Quoted 8 lines
Quoted 5 lines

Huge test suites like kms_flip do this operation thousands of times, which makes the virtual memory size increase until the test suite gets killed. Today, without this patch, we can't even run 50% of the kms_flip tests due to this problem. To test this, just "./kms_flip", then run "top" and sort it by the VIRT column.

Bleh. kmstest is a bad example of how to handle cairo object lifetimes and this patch just makes it worse.

I definitely can't say I love the current code, but we need a fix somehow. Do you have any specific suggestions that I could try?

Quoted 4 lines

-Chris

--
Chris Wilson, Intel Open Source Technology Centre

Signature · 1 line
Paulo Zanoni
#9 lore raw
Quoted 14 lines · Paulo Zanoni

2013/12/30 Chris Wilson <[email protected]>:

Quoted 13 lines · Paulo Zanoni
Quoted 11 lines

From: Paulo Zanoni <[email protected]>

When we call kmstest_get_cairo_ctx() and create a context, we do a gem_mmap. The problem is that we lose the mmap pointer, so we leak it. This patch stores the pointer and frees it at kmstest_remove_fb.

Huge test suites like kms_flip do this operation thousands of times, which makes the virtual memory size increase until the test suite gets killed. Today, without this patch, we can't even run 50% of the kms_flip tests due to this problem. To test this, just "./kms_flip", then run "top" and sort it by the VIRT column.

Quoted 5 lines
Quoted 2 lines

Bleh. kmstest is a bad example of how to handle cairo object lifetimes and this patch just makes it worse.

I definitely can't say I love the current code, but we need a fix somehow. Do you have any specific suggestions that I could try?

I have something I can push in the near future.
-Chris

Signature · 1 line
Chris Wilson, Intel Open Source Technology Centre
#10 lore raw
Quoted 15 lines · Chris Wilson
Quoted 14 lines · Paulo Zanoni

2013/12/30 Chris Wilson <[email protected]>:

Quoted 13 lines · Paulo Zanoni
Quoted 11 lines

From: Paulo Zanoni <[email protected]>

When we call kmstest_get_cairo_ctx() and create a context, we do a gem_mmap. The problem is that we lose the mmap pointer, so we leak it. This patch stores the pointer and frees it at kmstest_remove_fb.

Huge test suites like kms_flip do this operation thousands of times, which makes the virtual memory size increase until the test suite gets killed. Today, without this patch, we can't even run 50% of the kms_flip tests due to this problem. To test this, just "./kms_flip", then run "top" and sort it by the VIRT column.

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

Bleh. kmstest is a bad example of how to handle cairo object lifetimes and this patch just makes it worse.

I definitely can't say I love the current code, but we need a fix somehow. Do you have any specific suggestions that I could try?

I have something I can push in the near future.

I've taken care of this GEM bo leak, thanks.
-Chris

Signature · 1 line
Chris Wilson, Intel Open Source Technology Centre
#11 lore raw

2013/12/31 Chris Wilson <[email protected]>:

Quoted 19 lines · Chris Wilson
Quoted 18 lines · Paulo Zanoni
Quoted 17 lines

2013/12/30 Chris Wilson <[email protected]>:

Quoted 15 lines · Paulo Zanoni
Quoted 11 lines

From: Paulo Zanoni <[email protected]>

When we call kmstest_get_cairo_ctx() and create a context, we do a gem_mmap. The problem is that we lose the mmap pointer, so we leak it. This patch stores the pointer and frees it at kmstest_remove_fb.

Huge test suites like kms_flip do this operation thousands of times, which makes the virtual memory size increase until the test suite gets killed. Today, without this patch, we can't even run 50% of the kms_flip tests due to this problem. To test this, just "./kms_flip", then run "top" and sort it by the VIRT column.

Bleh. kmstest is a bad example of how to handle cairo object lifetimes and this patch just makes it worse.

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

I definitely can't say I love the current code, but we need a fix somehow. Do you have any specific suggestions that I could try?

I have something I can push in the near future.

I've taken care of this GEM bo leak, thanks.

Yay \o/

I confirm your patch fixes the leak. Thanks!!

Quoted 4 lines

-Chris

--
Chris Wilson, Intel Open Source Technology Centre

Signature · 1 line
Paulo Zanoni