Skip to content

Commit a0c2baa

Browse files
Sherry Yanggregkh
authored andcommitted
android: binder: Don't get mm from task
Use binder_alloc struct's mm_struct rather than getting a reference to the mm struct through get_task_mm to avoid a potential deadlock between lru lock, task lock and dentry lock, since a thread can be holding the task lock and the dentry lock while trying to acquire the lru lock. Acked-by: Arve Hjønnevåg <arve@android.com> Signed-off-by: Sherry Yang <sherryy@android.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 9d35593 commit a0c2baa

File tree

2 files changed

+9
-14
lines changed

2 files changed

+9
-14
lines changed

drivers/android/binder_alloc.c

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -215,17 +215,12 @@ static int binder_update_page_range(struct binder_alloc *alloc, int allocate,
215215
}
216216
}
217217

218-
if (!vma && need_mm)
219-
mm = get_task_mm(alloc->tsk);
218+
if (!vma && need_mm && mmget_not_zero(alloc->vma_vm_mm))
219+
mm = alloc->vma_vm_mm;
220220

221221
if (mm) {
222222
down_write(&mm->mmap_sem);
223223
vma = alloc->vma;
224-
if (vma && mm != alloc->vma_vm_mm) {
225-
pr_err("%d: vma mm and task mm mismatch\n",
226-
alloc->pid);
227-
vma = NULL;
228-
}
229224
}
230225

231226
if (!vma && need_mm) {
@@ -720,6 +715,7 @@ int binder_alloc_mmap_handler(struct binder_alloc *alloc,
720715
barrier();
721716
alloc->vma = vma;
722717
alloc->vma_vm_mm = vma->vm_mm;
718+
mmgrab(alloc->vma_vm_mm);
723719

724720
return 0;
725721

@@ -795,6 +791,8 @@ void binder_alloc_deferred_release(struct binder_alloc *alloc)
795791
vfree(alloc->buffer);
796792
}
797793
mutex_unlock(&alloc->mutex);
794+
if (alloc->vma_vm_mm)
795+
mmdrop(alloc->vma_vm_mm);
798796

799797
binder_alloc_debug(BINDER_DEBUG_OPEN_CLOSE,
800798
"%s: %d buffers %d, pages %d\n",
@@ -889,7 +887,6 @@ int binder_alloc_get_allocated_count(struct binder_alloc *alloc)
889887
void binder_alloc_vma_close(struct binder_alloc *alloc)
890888
{
891889
WRITE_ONCE(alloc->vma, NULL);
892-
WRITE_ONCE(alloc->vma_vm_mm, NULL);
893890
}
894891

895892
/**
@@ -926,9 +923,9 @@ enum lru_status binder_alloc_free_page(struct list_head *item,
926923
page_addr = (uintptr_t)alloc->buffer + index * PAGE_SIZE;
927924
vma = alloc->vma;
928925
if (vma) {
929-
mm = get_task_mm(alloc->tsk);
930-
if (!mm)
931-
goto err_get_task_mm_failed;
926+
if (!mmget_not_zero(alloc->vma_vm_mm))
927+
goto err_mmget;
928+
mm = alloc->vma_vm_mm;
932929
if (!down_write_trylock(&mm->mmap_sem))
933930
goto err_down_write_mmap_sem_failed;
934931
}
@@ -963,7 +960,7 @@ enum lru_status binder_alloc_free_page(struct list_head *item,
963960

964961
err_down_write_mmap_sem_failed:
965962
mmput_async(mm);
966-
err_get_task_mm_failed:
963+
err_mmget:
967964
err_page_already_freed:
968965
mutex_unlock(&alloc->mutex);
969966
err_get_alloc_mutex_failed:
@@ -1002,7 +999,6 @@ struct shrinker binder_shrinker = {
1002999
*/
10031000
void binder_alloc_init(struct binder_alloc *alloc)
10041001
{
1005-
alloc->tsk = current->group_leader;
10061002
alloc->pid = current->group_leader->pid;
10071003
mutex_init(&alloc->mutex);
10081004
INIT_LIST_HEAD(&alloc->buffers);

drivers/android/binder_alloc.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ struct binder_lru_page {
100100
*/
101101
struct binder_alloc {
102102
struct mutex mutex;
103-
struct task_struct *tsk;
104103
struct vm_area_struct *vma;
105104
struct mm_struct *vma_vm_mm;
106105
void *buffer;

0 commit comments

Comments
 (0)