Skip to contentSkip to conversation

Fix memory leak in binder

PATCH In review Tu, Xiaobing 3 participants 20 messages Oct 15, 2012 – Oct 23, 2012 Staging

lore
Share & tools
lore
#0 lore raw

-----Original Message-----
From: Tu, Xiaobing
Sent: Monday, October 15, 2012 3:03 PM
To: '[email protected]'; '[email protected]'; '[email protected]'; '[email protected]'; '[email protected]'; '[email protected]'; '[email protected]'; '[email protected]'
Cc: Zhang, Di; Ma, Xindong
Subject: Fix memory leak in binder

After enabling kmemleak and run monkey, following memleak is reported:
unreferenced object 0xeed27f80 (size 64):
comm "Binder_8", pid 641, jiffies 4294946341 (age 2275.810s) hex dump (first 32 bytes):

    4f dd 00 00 84 7f d2 ee 84 7f d2 ee 01 00 00 00  O...............
    00 00 00 00 00 00 00 00 00 aa 4c d7 00 00 00 00  ..........L.....
  backtrace:
    [<c184fabc>] kmemleak_alloc+0x3c/0xa0
    [<c12f391e>] kmem_cache_alloc_trace+0x9e/0x180
    [<c1668bb5>] binder_thread_write+0xcf5/0x23a0
    [<c166b091>] binder_ioctl+0x1f1/0x530
    [<c130dcf6>] do_vfs_ioctl+0x86/0x5e0
    [<c130e282>] sys_ioctl+0x32/0x60
    [<c1872e01>] syscall_call+0x7/0xb
    [<ffffffff>] 0xffffffff

The work item in async_todo list is not freed when binder released. Also the async transaction should also be freed in binder_release_work.

---
 drivers/staging/android/binder.c |    7 +++++++
 1 files changed, 7 insertions(+), 0 deletions(-)
drivers/staging/android/binder.c +7 -0
index 7df2a89..d23de68 100644
@@ -1281,6 +1281,7 @@ static void binder_send_failed_reply(struct binder_transaction *t,
 					"already\n", target_thread->proc->pid,
 					target_thread->pid,
 					target_thread->return_error);
+				kfree(t);
 			}
 			return;
 		} else {
@@ -2509,6 +2510,11 @@ static void binder_release_work(struct list_head *list)
 			t = container_of(w, struct binder_transaction, work);
 			if (t->buffer->target_node && !(t->flags & TF_ONE_WAY))
 				binder_send_failed_reply(t, BR_DEAD_REPLY);
+			else {
+				t->buffer->transaction = NULL;
+				kfree(t);
+				binder_stats_deleted(BINDER_STAT_TRANSACTION);
+			}
 		} break;
 		case BINDER_WORK_TRANSACTION_COMPLETE: {
 			kfree(w);
@@ -2982,6 +2988,7 @@ static void binder_deferred_release(struct binder_proc *proc)
 
 		nodes++;
 		rb_erase(&node->rb_node, &proc->nodes);
+		binder_release_work(&node->async_todo);
 		list_del_init(&node->work.entry);
 		if (hlist_empty(&node->refs)) {
 			kfree(node);
Signature · 5 lines
1.7.6

Br
XiaoBing Tu
PSI@System Integration Shanghai
#1 lore raw

Fix memory leak in binder--version2

After enabling kmemleak and run monkey, following memleak is reported:
unreferenced object 0xeed27f80 (size 64):
comm "Binder_8", pid 641, jiffies 4294946341 (age 2275.810s) hex dump (first 32 bytes):

    4f dd 00 00 84 7f d2 ee 84 7f d2 ee 01 00 00 00  O...............
    00 00 00 00 00 00 00 00 00 aa 4c d7 00 00 00 00  ..........L.....
  backtrace:
    [<c184fabc>] kmemleak_alloc+0x3c/0xa0
    [<c12f391e>] kmem_cache_alloc_trace+0x9e/0x180
    [<c1668bb5>] binder_thread_write+0xcf5/0x23a0
    [<c166b091>] binder_ioctl+0x1f1/0x530
    [<c130dcf6>] do_vfs_ioctl+0x86/0x5e0
    [<c130e282>] sys_ioctl+0x32/0x60
    [<c1872e01>] syscall_call+0x7/0xb
    [<ffffffff>] 0xffffffff

The work item in async_todo list is not freed when binder released. Also the async transaction should also be freed in binder_release_work.

---
 drivers/staging/android/binder.c |    6 ++++++
 1 files changed, 6 insertions(+), 0 deletions(-)
drivers/staging/android/binder.c +6 -0
index 7df2a89..022c9f8 100644
@@ -2509,6 +2509,11 @@ static void binder_release_work(struct list_head *list)
 			t = container_of(w, struct binder_transaction, work);
 			if (t->buffer->target_node && !(t->flags & TF_ONE_WAY))
 				binder_send_failed_reply(t, BR_DEAD_REPLY);
+			else {
+				t->buffer->transaction = NULL;
+				kfree(t);
+				binder_stats_deleted(BINDER_STAT_TRANSACTION);
+			}
 		} break;
 		case BINDER_WORK_TRANSACTION_COMPLETE: {
 			kfree(w);
@@ -2982,6 +2987,7 @@ static void binder_deferred_release(struct binder_proc *proc)
 
 		nodes++;
 		rb_erase(&node->rb_node, &proc->nodes);
+		binder_release_work(&node->async_todo);
 		list_del_init(&node->work.entry);
 		if (hlist_empty(&node->refs)) {
 			kfree(node);
Signature · 1 line
1.7.6
maintainer
#2 lore raw

Re: Fix memory leak in binder--version2

Quoted 16 lines · Xiaobing

After enabling kmemleak and run monkey, following memleak is reported:
unreferenced object 0xeed27f80 (size 64):
comm "Binder_8", pid 641, jiffies 4294946341 (age 2275.810s) hex dump (first 32 bytes):

    4f dd 00 00 84 7f d2 ee 84 7f d2 ee 01 00 00 00  O...............
    00 00 00 00 00 00 00 00 00 aa 4c d7 00 00 00 00  ..........L.....
  backtrace:
    [<c184fabc>] kmemleak_alloc+0x3c/0xa0
    [<c12f391e>] kmem_cache_alloc_trace+0x9e/0x180
    [<c1668bb5>] binder_thread_write+0xcf5/0x23a0
    [<c166b091>] binder_ioctl+0x1f1/0x530
    [<c130dcf6>] do_vfs_ioctl+0x86/0x5e0
    [<c130e282>] sys_ioctl+0x32/0x60
    [<c1872e01>] syscall_call+0x7/0xb
    [<ffffffff>] 0xffffffff
Quoted 8 lines

The work item in async_todo list is not freed when binder released. Also the async transaction should also be freed in binder_release_work.

---
 drivers/staging/android/binder.c |    6 ++++++
 1 files changed, 6 insertions(+), 0 deletions(-)

Nice fix, but next time can you at least use scripts/get_maintainer.pl to figure out who to send this to? I'll queue it up soon, but it took akpm to point me at this for me to notice it.

thanks,

greg k-h

#3 lore raw

Re: Fix memory leak in binder--version2

Quoted 26 lines · Greg KH
Quoted 16 lines · Xiaobing

After enabling kmemleak and run monkey, following memleak is reported:
unreferenced object 0xeed27f80 (size 64):
comm "Binder_8", pid 641, jiffies 4294946341 (age 2275.810s) hex dump (first 32 bytes):

    4f dd 00 00 84 7f d2 ee 84 7f d2 ee 01 00 00 00  O...............
    00 00 00 00 00 00 00 00 00 aa 4c d7 00 00 00 00  ..........L.....
  backtrace:
    [<c184fabc>] kmemleak_alloc+0x3c/0xa0
    [<c12f391e>] kmem_cache_alloc_trace+0x9e/0x180
    [<c1668bb5>] binder_thread_write+0xcf5/0x23a0
    [<c166b091>] binder_ioctl+0x1f1/0x530
    [<c130dcf6>] do_vfs_ioctl+0x86/0x5e0
    [<c130e282>] sys_ioctl+0x32/0x60
    [<c1872e01>] syscall_call+0x7/0xb
    [<ffffffff>] 0xffffffff
Quoted 8 lines

The work item in async_todo list is not freed when binder released. Also the async transaction should also be freed in binder_release_work.

---
 drivers/staging/android/binder.c |    6 ++++++
 1 files changed, 6 insertions(+), 0 deletions(-)
Quoted 3 lines

Nice fix, but next time can you at least use scripts/get_maintainer.pl to figure out who to send this to? I'll queue it up soon, but it took akpm to point me at this for me to notice it.

This patch was just pointer out to me as well. I have a similar fix queued up at https://android-review.googlesource.com/#/c/43801/ that is still being tested. It fixes this leak and a theoretical leak of death notification objects.

Signature · 1 line
Arve Hjønnevåg
maintainer
#4 lore raw

Re: Fix memory leak in binder--version2

Quoted 27 lines · Arve Hjønnevåg
Quoted 26 lines · Greg KH
Quoted 24 lines · Xiaobing

After enabling kmemleak and run monkey, following memleak is reported:
unreferenced object 0xeed27f80 (size 64):
comm "Binder_8", pid 641, jiffies 4294946341 (age 2275.810s) hex dump (first 32 bytes):

    4f dd 00 00 84 7f d2 ee 84 7f d2 ee 01 00 00 00  O...............
    00 00 00 00 00 00 00 00 00 aa 4c d7 00 00 00 00  ..........L.....
  backtrace:
    [<c184fabc>] kmemleak_alloc+0x3c/0xa0
    [<c12f391e>] kmem_cache_alloc_trace+0x9e/0x180
    [<c1668bb5>] binder_thread_write+0xcf5/0x23a0
    [<c166b091>] binder_ioctl+0x1f1/0x530
    [<c130dcf6>] do_vfs_ioctl+0x86/0x5e0
    [<c130e282>] sys_ioctl+0x32/0x60
    [<c1872e01>] syscall_call+0x7/0xb
    [<ffffffff>] 0xffffffff

The work item in async_todo list is not freed when binder released. Also the async transaction should also be freed in binder_release_work.

---
 drivers/staging/android/binder.c |    6 ++++++
 1 files changed, 6 insertions(+), 0 deletions(-)
Quoted 8 lines
Quoted 3 lines

Nice fix, but next time can you at least use scripts/get_maintainer.pl to figure out who to send this to? I'll queue it up soon, but it took akpm to point me at this for me to notice it.

This patch was just pointer out to me as well. I have a similar fix queued up at https://android-review.googlesource.com/#/c/43801/ that is still being tested. It fixes this leak and a theoretical leak of death notification objects.

Ok, should I hold off applying this patch and wait for your patch instead?

thanks,

greg k-h

#5 lore raw

PATCH 1/2 Staging: android: binder: Fix memory leak on thread/process exit

If a thread or process exited while a reply, one-way transaction or death notification was pending, the struct holding the pending work was leaked.

Change-Id I2eaafaba1c0ecda3ec0872d449dc16d0721c21e7
---
 drivers/staging/android/binder.c |   28 +++++++++++++++++++++++++++-
 1 files changed, 27 insertions(+), 1 deletions(-)
drivers/staging/android/binder.c +27 -1
index 85ed6cf..b89799a 100644
@@ -2548,14 +2548,38 @@ static void binder_release_work(struct list_head *list)
 			struct binder_transaction *t;
 
 			t = container_of(w, struct binder_transaction, work);
-			if (t->buffer->target_node && !(t->flags & TF_ONE_WAY))
+			if (t->buffer->target_node &&
+			    !(t->flags & TF_ONE_WAY)) {
 				binder_send_failed_reply(t, BR_DEAD_REPLY);
+			} else {
+				binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
+					"binder: undelivered transaction %d\n",
+					t->debug_id);
+				t->buffer->transaction = NULL;
+				kfree(t);
+				binder_stats_deleted(BINDER_STAT_TRANSACTION);
+			}
 		} break;
 		case BINDER_WORK_TRANSACTION_COMPLETE: {
+			binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
+				"binder: undelivered TRANSACTION_COMPLETE\n");
 			kfree(w);
 			binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
 		} break;
+		case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
+		case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
+			struct binder_ref_death *death;
+
+			death = container_of(w, struct binder_ref_death, work);
+			binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
+				"binder: undelivered death notification, %p\n",
+				death->cookie);
+			kfree(death);
+			binder_stats_deleted(BINDER_STAT_DEATH);
+		} break;
 		default:
+			pr_err("binder: unexpected work type, %d, not freed\n",
+			       w->type);
 			break;
 		}
 	}
@@ -3036,6 +3060,7 @@ static void binder_deferred_release(struct binder_proc *proc)
 		nodes++;
 		rb_erase(&node->rb_node, &proc->nodes);
 		list_del_init(&node->work.entry);
+		binder_release_work(&node->async_todo);
 		if (hlist_empty(&node->refs)) {
 			kfree(node);
 			binder_stats_deleted(BINDER_STAT_NODE);
@@ -3074,6 +3099,7 @@ static void binder_deferred_release(struct binder_proc *proc)
 		binder_delete_ref(ref);
 	}
 	binder_release_work(&proc->todo);
+	binder_release_work(&proc->delivered_death);
 	buffers = 0;
 
 	while ((n = rb_first(&proc->allocated_buffers))) {
Signature · 1 line
1.7.7.3
#6 lore raw

PATCH 2/2 Staging: android: binder: Allow using highmem for binder buffers

Change-Id I19be5fd89c194abcb8b041e2dd6c4d71ae7b1cc2
---
 drivers/staging/android/binder.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
drivers/staging/android/binder.c +1 -1
index b89799a..c831dcc 100644
@@ -671,7 +671,7 @@ static int binder_update_page_range(struct binder_proc *proc, int allocate,
 		page = &proc->pages[(page_addr - proc->buffer) / PAGE_SIZE];
 
 		BUG_ON(*page);
-		*page = alloc_page(GFP_KERNEL | __GFP_ZERO);
+		*page = alloc_page(GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO);
 		if (*page == NULL) {
 			printk(KERN_ERR "binder: %d: binder_alloc_buf failed "
 			       "for page at %p\n", proc->pid, page_addr);
Signature · 1 line
1.7.7.3
maintainer
#7 lore raw

Re: [PATCH 2/2] Staging: android: binder: Allow using highmem for binder buffers

Quoted 1 line · Arve Hjønnevåg

Change-Id: I19be5fd89c194abcb8b041e2dd6c4d71ae7b1cc2

Again with the change-id field :(

And please provide some information here, _why_ do we want to allow highmem for binder buffers? What does this solve? Is it a bug or just a feature enhancement? Depending on what it is depends on what release it should go into.

thanks,

greg k-h

maintainer
#8 lore raw

Re: [PATCH 1/2] Staging: android: binder: Fix memory leak on thread/process exit

In reply to Arve Hjønnevåg
Quoted 5 lines · Arve Hjønnevåg

If a thread or process exited while a reply, one-way transaction or death notification was pending, the struct holding the pending work was leaked.

Change-Id: I2eaafaba1c0ecda3ec0872d449dc16d0721c21e7

What is this field? Please don't include this in kernel patches, it forces me to edit the patch by hand :(

Also, this should go to older kernels, right?

greg k-h

#9 lore raw

Re: [PATCH 1/2] Staging: android: binder: Fix memory leak on thread/process exit

Quoted 10 lines · Greg KH
Quoted 5 lines · Arve Hjønnevåg

If a thread or process exited while a reply, one-way transaction or death notification was pending, the struct holding the pending work was leaked.

Change-Id: I2eaafaba1c0ecda3ec0872d449dc16d0721c21e7

What is this field? Please don't include this in kernel patches, it forces me to edit the patch by hand :(

It is a tag generated a git hook to uniquely identify a change through multiple revisions of that change. If the tag does not already exist the hook adds it. I removed it from the reposted patches as you requested, but have you considered leaving it in? It is a useful search token both for finding different revisions of a patch and for finding which branch a change has been cherry-picked into.

Quoted 1 line

Also, this should go to older kernels, right?

It can, but the leak is relatively slow so I don't think it is critical. I looked at one device that had been running for a while and it had leaked around 16k per month.

Signature · 1 line
Arve Hjønnevåg
maintainer
#10 lore raw

Re: [PATCH 1/2] Staging: android: binder: Fix memory leak on thread/process exit

Quoted 12 lines · Arve Hjønnevåg
Quoted 10 lines · Greg KH
Quoted 5 lines · Arve Hjønnevåg

If a thread or process exited while a reply, one-way transaction or death notification was pending, the struct holding the pending work was leaked.

Change-Id: I2eaafaba1c0ecda3ec0872d449dc16d0721c21e7

What is this field? Please don't include this in kernel patches, it forces me to edit the patch by hand :(

Quoted 6 lines

It is a tag generated a git hook to uniquely identify a change through multiple revisions of that change. If the tag does not already exist the hook adds it. I removed it from the reposted patches as you requested, but have you considered leaving it in? It is a useful search token both for finding different revisions of a patch and for finding which branch a change has been cherry-picked into.

Sorry, I was being facetious, I knew what it really was, but the point was, why would I?

As has been stated before, you can include information like this in your patch, but you MUST reference it properly so that others can be able to figure out what you mean. A random Change-Id value means nothing given that lots of different groups use gerrit. You need a url, or a pointer to which gerrit instance this refers to before you can include it.

Same goes for bugzilla entries, if you look in the kernel changelog, we reference lots of different ones, but we use urls to determine which one we are talking about (suse, red hat, kernel.org, etc.)

thanks,

greg k-h

#11 lore raw

Re: [PATCH 1/2] Staging: android: binder: Fix memory leak on thread/process exit

Quoted 20 lines · Greg KH
Quoted 12 lines · Arve Hjønnevåg
Quoted 10 lines · Greg KH
Quoted 5 lines · Arve Hjønnevåg

If a thread or process exited while a reply, one-way transaction or death notification was pending, the struct holding the pending work was leaked.

Change-Id: I2eaafaba1c0ecda3ec0872d449dc16d0721c21e7

What is this field? Please don't include this in kernel patches, it forces me to edit the patch by hand :(

Quoted 6 lines

It is a tag generated a git hook to uniquely identify a change through multiple revisions of that change. If the tag does not already exist the hook adds it. I removed it from the reposted patches as you requested, but have you considered leaving it in? It is a useful search token both for finding different revisions of a patch and for finding which branch a change has been cherry-picked into.

Quoted 3 lines

Sorry, I was being facetious, I knew what it really was, but the point was, why would I?

So you can use it. If, for instance, you want to see which branches you cherry-picked this change into you can run "git rev-list --all --grep I2eaafaba1c0ecda3ec0872d449dc16d0721c21e7 | xargs -rl git branch -a --contains". If it was more widely used, it would also be useful for finding mailing list discussion of multiple versions of patches (even when the title of the patch changes).

Quoted 6 lines

As has been stated before, you can include information like this in your patch, but you MUST reference it properly so that others can be able to figure out what you mean. A random Change-Id value means nothing given that lots of different groups use gerrit. You need a url, or a pointer to which gerrit instance this refers to before you can include it.

There is no single url. The tag was generated by a local git hook before it was uploaded anywhere. While gerrit looks for this tag and use it when it is there, the same change can be uploaded to different gerrit instances. While it would be useful to also include urls to the gerrit instance(s) that a patch was discussed on, the Change-Id is useful by itself.

Quoted 4 lines

Same goes for bugzilla entries, if you look in the kernel changelog, we reference lots of different ones, but we use urls to determine which one we are talking about (suse, red hat, kernel.org, etc.)

Those IDs appear to be local to a specific server, so the id without the url would not be very useful.

Signature · 1 line
Arve Hjønnevåg
maintainer
#12 lore raw

Re: [PATCH 1/2] Staging: android: binder: Fix memory leak on thread/process exit

Quoted 25 lines · Arve Hj?nnev?g
Quoted 20 lines · Greg KH
Quoted 12 lines · Arve Hj?nnev?g
Quoted 10 lines · Greg KH
Quoted 5 lines · Arve Hj?nnev?g

If a thread or process exited while a reply, one-way transaction or death notification was pending, the struct holding the pending work was leaked.

Change-Id: I2eaafaba1c0ecda3ec0872d449dc16d0721c21e7

What is this field? Please don't include this in kernel patches, it forces me to edit the patch by hand :(

Quoted 6 lines

It is a tag generated a git hook to uniquely identify a change through multiple revisions of that change. If the tag does not already exist the hook adds it. I removed it from the reposted patches as you requested, but have you considered leaving it in? It is a useful search token both for finding different revisions of a patch and for finding which branch a change has been cherry-picked into.

Quoted 3 lines

Sorry, I was being facetious, I knew what it really was, but the point was, why would I?

Quoted 6 lines

So you can use it. If, for instance, you want to see which branches you cherry-picked this change into you can run "git rev-list --all --grep I2eaafaba1c0ecda3ec0872d449dc16d0721c21e7 | xargs -rl git branch -a --contains". If it was more widely used, it would also be useful for finding mailing list discussion of multiple versions of patches (even when the title of the patch changes).

Yes, if you want a unique identifier for a patch in order to try to track it through trees, you can do something like that (hint, see Alan Stern's patches, he has his own identifier for that.) But don't use the gerrit id for this, it's not useful and it is confusing. We have discussed this in the past, see the lkml archives for details.

Quoted 14 lines
Quoted 6 lines

As has been stated before, you can include information like this in your patch, but you MUST reference it properly so that others can be able to figure out what you mean. A random Change-Id value means nothing given that lots of different groups use gerrit. You need a url, or a pointer to which gerrit instance this refers to before you can include it.

There is no single url. The tag was generated by a local git hook before it was uploaded anywhere. While gerrit looks for this tag and use it when it is there, the same change can be uploaded to different gerrit instances. While it would be useful to also include urls to the gerrit instance(s) that a patch was discussed on, the Change-Id is useful by itself.

Quoted 7 lines
Quoted 4 lines

Same goes for bugzilla entries, if you look in the kernel changelog, we reference lots of different ones, but we use urls to determine which one we are talking about (suse, red hat, kernel.org, etc.)

Those IDs appear to be local to a specific server, so the id without the url would not be very useful.

Ok, then I don't recommend it be used at all, as we have a unique identifier, the git commit id in Linus's tree. That id does follow the patch around if it goes into any stable kernel tree, so let's stick with it.

thanks,

greg k-h

#13 lore raw

Re: Fix memory leak in binder--version2

In reply to Greg KH
Quoted 32 lines · Greg KH
Quoted 31 lines · Arve Hjønnevåg
Quoted 29 lines · Greg KH
Quoted 16 lines · Xiaobing

After enabling kmemleak and run monkey, following memleak is reported:
unreferenced object 0xeed27f80 (size 64):
comm "Binder_8", pid 641, jiffies 4294946341 (age 2275.810s) hex dump (first 32 bytes):

    4f dd 00 00 84 7f d2 ee 84 7f d2 ee 01 00 00 00  O...............
    00 00 00 00 00 00 00 00 00 aa 4c d7 00 00 00 00  ..........L.....
  backtrace:
    [<c184fabc>] kmemleak_alloc+0x3c/0xa0
    [<c12f391e>] kmem_cache_alloc_trace+0x9e/0x180
    [<c1668bb5>] binder_thread_write+0xcf5/0x23a0
    [<c166b091>] binder_ioctl+0x1f1/0x530
    [<c130dcf6>] do_vfs_ioctl+0x86/0x5e0
    [<c130e282>] sys_ioctl+0x32/0x60
    [<c1872e01>] syscall_call+0x7/0xb
    [<ffffffff>] 0xffffffff
Quoted 8 lines

The work item in async_todo list is not freed when binder released. Also the async transaction should also be freed in binder_release_work.

---
 drivers/staging/android/binder.c |    6 ++++++
 1 files changed, 6 insertions(+), 0 deletions(-)

Nice fix, but next time can you at least use scripts/get_maintainer.pl to figure out who to send this to? I'll queue it up soon, but it took akpm to point me at this for me to notice it.

Quoted 8 lines
Quoted 4 lines

This patch was just pointer out to me as well. I have a similar fix queued up at https://android-review.googlesource.com/#/c/43801/ that is still being tested. It fixes this leak and a theoretical leak of death notification objects.

Ok, should I hold off applying this patch and wait for your patch instead?

I just sent two patches, but they are not fully tested yet.

Signature · 1 line
Arve Hjønnevåg
maintainer
#14 lore raw

Re: Fix memory leak in binder--version2

Quoted 38 lines · Arve Hjønnevåg
Quoted 37 lines · Greg KH
Quoted 35 lines · Arve Hjønnevåg
Quoted 26 lines · Greg KH
Quoted 16 lines · Xiaobing

After enabling kmemleak and run monkey, following memleak is reported:
unreferenced object 0xeed27f80 (size 64):
comm "Binder_8", pid 641, jiffies 4294946341 (age 2275.810s) hex dump (first 32 bytes):

    4f dd 00 00 84 7f d2 ee 84 7f d2 ee 01 00 00 00  O...............
    00 00 00 00 00 00 00 00 00 aa 4c d7 00 00 00 00  ..........L.....
  backtrace:
    [<c184fabc>] kmemleak_alloc+0x3c/0xa0
    [<c12f391e>] kmem_cache_alloc_trace+0x9e/0x180
    [<c1668bb5>] binder_thread_write+0xcf5/0x23a0
    [<c166b091>] binder_ioctl+0x1f1/0x530
    [<c130dcf6>] do_vfs_ioctl+0x86/0x5e0
    [<c130e282>] sys_ioctl+0x32/0x60
    [<c1872e01>] syscall_call+0x7/0xb
    [<ffffffff>] 0xffffffff
Quoted 8 lines

The work item in async_todo list is not freed when binder released. Also the async transaction should also be freed in binder_release_work.

---
 drivers/staging/android/binder.c |    6 ++++++
 1 files changed, 6 insertions(+), 0 deletions(-)
Quoted 3 lines

Nice fix, but next time can you at least use scripts/get_maintainer.pl to figure out who to send this to? I'll queue it up soon, but it took akpm to point me at this for me to notice it.

This patch was just pointer out to me as well. I have a similar fix queued up at https://android-review.googlesource.com/#/c/43801/ that is still being tested. It fixes this leak and a theoretical leak of death notification objects.

Quoted 5 lines
Quoted 3 lines

Ok, should I hold off applying this patch and wait for your patch instead?

I just sent two patches, but they are not fully tested yet.

Ok, care to test them and after that, resend them with the changes I noted in the changelog section of the patch?

thanks,

greg k-h

#15 lore raw

PATCH 1/4 Staging: android: binder: Add some missing binder_stat_br calls

Cached thread return errors, death notifications and new looper requests were not included in the stats.

---
 drivers/staging/android/binder.c |    4 ++++
 1 files changed, 4 insertions(+), 0 deletions(-)
drivers/staging/android/binder.c +4 -0
index 7b0ba92..9e852d0 100644
@@ -2135,6 +2135,7 @@ retry:
 			if (put_user(thread->return_error2, (uint32_t __user *)ptr))
 				return -EFAULT;
 			ptr += sizeof(uint32_t);
+			binder_stat_br(proc, thread, thread->return_error2);
 			if (ptr == end)
 				goto done;
 			thread->return_error2 = BR_OK;
@@ -2142,6 +2143,7 @@ retry:
 		if (put_user(thread->return_error, (uint32_t __user *)ptr))
 			return -EFAULT;
 		ptr += sizeof(uint32_t);
+		binder_stat_br(proc, thread, thread->return_error);
 		thread->return_error = BR_OK;
 		goto done;
 	}
@@ -2297,6 +2299,7 @@ retry:
 			if (put_user(death->cookie, (void * __user *)ptr))
 				return -EFAULT;
 			ptr += sizeof(void *);
+			binder_stat_br(proc, thread, cmd);
 			binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
 				     "binder: %d:%d %s %p\n",
 				      proc->pid, thread->pid,
@@ -2404,6 +2407,7 @@ done:
 			     proc->pid, thread->pid);
 		if (put_user(BR_SPAWN_LOOPER, (uint32_t __user *)buffer))
 			return -EFAULT;
+		binder_stat_br(proc, thread, BR_SPAWN_LOOPER);
 	}
 	return 0;
 }
Signature · 1 line
1.7.7.3
#16 lore raw

PATCH 2/4 Staging: android: binder: Add some tracepoints

Add tracepoints:
- ioctl entry and exit
- Main binder lock: lock, locked and unlock
- Command and return buffer opcodes
- Transaction: create and receive
- Transaction buffer: create and free
- Object and file descriptor transfer
- binder_update_page_range

---
 drivers/staging/android/Makefile       |    2 +
 drivers/staging/android/binder.c       |   91 +++++++--
 drivers/staging/android/binder_trace.h |  327 ++++++++++++++++++++++++++++++++
 3 files changed, 400 insertions(+), 20 deletions(-)
 create mode 100644 drivers/staging/android/binder_trace.h
drivers/staging/android/Makefile +2 -0
index e16fcd5..b35a631 100644
@@ -1,3 +1,5 @@
+ccflags-y += -I$(src)			# needed for trace events
+
 obj-$(CONFIG_ANDROID_BINDER_IPC)	+= binder.o
 obj-$(CONFIG_ASHMEM)			+= ashmem.o
 obj-$(CONFIG_ANDROID_LOGGER)		+= logger.o
drivers/staging/android/binder.c +71 -20

299 lines not on this page · raw message

drivers/staging/android/binder_trace.h new file +327 -0

327 lines not on this page · raw message

Signature · 1 line
1.7.7.3
#17 lore raw

PATCH 3/4 Staging: android: binder: Fix memory leak on thread/process exit

In reply to Arve Hjønnevåg

If a thread or process exited while a reply, one-way transaction or death notification was pending, the struct holding the pending work was leaked.

---
 drivers/staging/android/binder.c |   28 +++++++++++++++++++++++++++-
 1 files changed, 27 insertions(+), 1 deletions(-)
drivers/staging/android/binder.c +27 -1
index b7cfcdb..91ce956 100644
@@ -2460,14 +2460,38 @@ static void binder_release_work(struct list_head *list)
 			struct binder_transaction *t;
 
 			t = container_of(w, struct binder_transaction, work);
-			if (t->buffer->target_node && !(t->flags & TF_ONE_WAY))
+			if (t->buffer->target_node &&
+			    !(t->flags & TF_ONE_WAY)) {
 				binder_send_failed_reply(t, BR_DEAD_REPLY);
+			} else {
+				binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
+					"binder: undelivered transaction %d\n",
+					t->debug_id);
+				t->buffer->transaction = NULL;
+				kfree(t);
+				binder_stats_deleted(BINDER_STAT_TRANSACTION);
+			}
 		} break;
 		case BINDER_WORK_TRANSACTION_COMPLETE: {
+			binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
+				"binder: undelivered TRANSACTION_COMPLETE\n");
 			kfree(w);
 			binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
 		} break;
+		case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
+		case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
+			struct binder_ref_death *death;
+
+			death = container_of(w, struct binder_ref_death, work);
+			binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
+				"binder: undelivered death notification, %p\n",
+				death->cookie);
+			kfree(death);
+			binder_stats_deleted(BINDER_STAT_DEATH);
+		} break;
 		default:
+			pr_err("binder: unexpected work type, %d, not freed\n",
+			       w->type);
 			break;
 		}
 	}
@@ -2951,6 +2975,7 @@ static void binder_deferred_release(struct binder_proc *proc)
 		nodes++;
 		rb_erase(&node->rb_node, &proc->nodes);
 		list_del_init(&node->work.entry);
+		binder_release_work(&node->async_todo);
 		if (hlist_empty(&node->refs)) {
 			kfree(node);
 			binder_stats_deleted(BINDER_STAT_NODE);
@@ -2989,6 +3014,7 @@ static void binder_deferred_release(struct binder_proc *proc)
 		binder_delete_ref(ref);
 	}
 	binder_release_work(&proc->todo);
+	binder_release_work(&proc->delivered_death);
 	buffers = 0;
 
 	while ((n = rb_first(&proc->allocated_buffers))) {
Signature · 1 line
1.7.7.3
#18 lore raw

PATCH 4/4 Staging: android: binder: Allow using highmem for binder buffers

In reply to Arve Hjønnevåg

The default kernel mapping for the pages allocated for the binder buffers is never used. Set the __GFP_HIGHMEM flag when allocating these pages so we don't needlessly use low memory pages that may be required elsewhere.

---
 drivers/staging/android/binder.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
drivers/staging/android/binder.c +1 -1
index 91ce956..9fbc06e 100644
@@ -583,7 +583,7 @@ static int binder_update_page_range(struct binder_proc *proc, int allocate,
 		page = &proc->pages[(page_addr - proc->buffer) / PAGE_SIZE];
 
 		BUG_ON(*page);
-		*page = alloc_page(GFP_KERNEL | __GFP_ZERO);
+		*page = alloc_page(GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO);
 		if (*page == NULL) {
 			pr_err("binder: %d: binder_alloc_buf failed "
 			       "for page at %p\n", proc->pid, page_addr);
Signature · 1 line
1.7.7.3
#19 lore raw

Re: Fix memory leak in binder--version2

In reply to Greg KH
Quoted 39 lines · Greg KH
Quoted 38 lines · Arve Hjønnevåg
Quoted 37 lines · Greg KH
Quoted 35 lines · Arve Hjønnevåg
Quoted 26 lines · Greg KH
Quoted 16 lines · Xiaobing

After enabling kmemleak and run monkey, following memleak is reported:
unreferenced object 0xeed27f80 (size 64):
comm "Binder_8", pid 641, jiffies 4294946341 (age 2275.810s) hex dump (first 32 bytes):

    4f dd 00 00 84 7f d2 ee 84 7f d2 ee 01 00 00 00  O...............
    00 00 00 00 00 00 00 00 00 aa 4c d7 00 00 00 00  ..........L.....
  backtrace:
    [<c184fabc>] kmemleak_alloc+0x3c/0xa0
    [<c12f391e>] kmem_cache_alloc_trace+0x9e/0x180
    [<c1668bb5>] binder_thread_write+0xcf5/0x23a0
    [<c166b091>] binder_ioctl+0x1f1/0x530
    [<c130dcf6>] do_vfs_ioctl+0x86/0x5e0
    [<c130e282>] sys_ioctl+0x32/0x60
    [<c1872e01>] syscall_call+0x7/0xb
    [<ffffffff>] 0xffffffff
Quoted 8 lines

The work item in async_todo list is not freed when binder released. Also the async transaction should also be freed in binder_release_work.

---
 drivers/staging/android/binder.c |    6 ++++++
 1 files changed, 6 insertions(+), 0 deletions(-)
Quoted 3 lines

Nice fix, but next time can you at least use scripts/get_maintainer.pl to figure out who to send this to? I'll queue it up soon, but it took akpm to point me at this for me to notice it.

This patch was just pointer out to me as well. I have a similar fix queued up at https://android-review.googlesource.com/#/c/43801/ that is still being tested. It fixes this leak and a theoretical leak of death notification objects.

Quoted 9 lines
Quoted 5 lines
Quoted 3 lines

Ok, should I hold off applying this patch and wait for your patch instead?

I just sent two patches, but they are not fully tested yet.

Ok, care to test them and after that, resend them with the changes I noted in the changelog section of the patch?

All the new cleanup code has been tested now. I also included two older patches that are not in mainline.

Signature · 1 line
Arve Hjønnevåg