Skip to contentSkip to conversation

ANDROID: binder: change down_write to down_read

PATCH Superseded Minchan Kim 4 participants 6 messages Mar 29, 2018 – Mar 30, 2018 Android drivers

v3 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 ANDROID: binder: change down_write to down_read

binder_update_page_range needs down_write of mmap_sem because vm_insert_page need to change vma->vm_flags to VM_MIXEDMAP unless it is set. However, when I profile binder working, it seems every binder buffers should be mapped in advance by binder_mmap. It means we could set VM_MIXEDMAP in bider_mmap time which is already hold a mmap_sem as down_write so binder_update_page_range doesn't need to hold a mmap_sem as down_write.

Android suffers from mmap_sem contention so let's reduce mmap_sem down_write.

Cc Todd Kjos <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc Arve Hjønnevåg <[email protected]>

---

Quoted 1 line

From v2:

* Fix vma->flag setting - Arve

Quoted 1 line

From v1:

* remove WARN_ON_ONCE - Greg
* add reviewed-by - Martijn

Martijn, I took your LGTM of v1 as Reviewed-by. If you don't like it or want to change it to acked-by, please, tell me.

 drivers/android/binder.c       | 3 ++-
 drivers/android/binder_alloc.c | 6 +++---
 2 files changed, 5 insertions(+), 4 deletions(-)
drivers/android/binder.c +2 -1
index 764b63a5aade..875ab21fa2e0 100644
@@ -4722,7 +4722,8 @@ static int binder_mmap(struct file *filp, struct vm_area_struct *vma)
 		failure_string = "bad vm_flags";
 		goto err_bad_arg;
 	}
-	vma->vm_flags = (vma->vm_flags | VM_DONTCOPY) & ~VM_MAYWRITE;
+	vma->vm_flags = (vma->flags | VM_DONTCOPY | VM_MIXEDMAP) &
+							~VM_MAYWRITE;
 	vma->vm_ops = &binder_vm_ops;
 	vma->vm_private_data = proc;
 
drivers/android/binder_alloc.c +3 -3
index 5a426c877dfb..4f382d51def1 100644
@@ -219,7 +219,7 @@ static int binder_update_page_range(struct binder_alloc *alloc, int allocate,
 		mm = alloc->vma_vm_mm;
 
 	if (mm) {
-		down_write(&mm->mmap_sem);
+		down_read(&mm->mmap_sem);
 		vma = alloc->vma;
 	}
 
@@ -288,7 +288,7 @@ static int binder_update_page_range(struct binder_alloc *alloc, int allocate,
 		/* vm_insert_page does not seem to increment the refcount */
 	}
 	if (mm) {
-		up_write(&mm->mmap_sem);
+		up_read(&mm->mmap_sem);
 		mmput(mm);
 	}
 	return 0;
@@ -321,7 +321,7 @@ static int binder_update_page_range(struct binder_alloc *alloc, int allocate,
 	}
 err_no_vma:
 	if (mm) {
-		up_write(&mm->mmap_sem);
+		up_read(&mm->mmap_sem);
 		mmput(mm);
 	}
 	return vma ? -ENOMEM : -ESRCH;
Signature · 1 line
2.17.0.rc1.321.gba9d0f2565-goog
author Reviewed-by Martijn Coenen on v3 1/1
#1 lore raw
Quoted 8 lines · Minchan Kim

binder_update_page_range needs down_write of mmap_sem because vm_insert_page need to change vma->vm_flags to VM_MIXEDMAP unless it is set. However, when I profile binder working, it seems every binder buffers should be mapped in advance by binder_mmap. It means we could set VM_MIXEDMAP in bider_mmap time which is already hold a mmap_sem as down_write so binder_update_page_range doesn't need to hold a mmap_sem as down_write.

Quoted 8 lines

Android suffers from mmap_sem contention so let's reduce mmap_sem down_write.

Cc Todd Kjos <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc Arve Hjønnevåg <[email protected]>

Sent wrong version. Sorry for that. Please ignore this and take it.

Quoted 1 line

From 480e992d4a650fb98e1397114d75dea7af8e6d0c Mon Sep 17 00:00:00 2001

From: Minchan Kim <[email protected]>
Date: Wed, 28 Mar 2018 11:32:42 +0900
Subject: [PATCH v3] ANDROID: binder: change down_write to down_read
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

binder_update_page_range needs down_write of mmap_sem because vm_insert_page need to change vma->vm_flags to VM_MIXEDMAP unless it is set. However, when I profile binder working, it seems every binder buffers should be mapped in advance by binder_mmap. It means we could set VM_MIXEDMAP in bider_mmap time which is already hold a mmap_sem as down_write so binder_update_page_range doesn't need to hold a mmap_sem as down_write.

Android suffers from mmap_sem contention so let's reduce mmap_sem down_write.

Cc Arve Hjønnevåg <[email protected]>
Cc Todd Kjos <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>

---

Quoted 1 line

From v2:

* Fix vma->flag setting - Arve

Quoted 1 line

From v1:

* remove WARN_ON_ONCE - Greg
* add reviewed-by - Martijn

Martijn, I took your LGTM of v1 as Reviewed-by. If you don't like it or want to change it to acked-by, please, tell me.

 drivers/android/binder.c       | 3 ++-
 drivers/android/binder_alloc.c | 6 +++---
 2 files changed, 5 insertions(+), 4 deletions(-)
drivers/android/binder.c +2 -1
index 764b63a5aade..fe62be7d7113 100644
@@ -4722,7 +4722,8 @@ static int binder_mmap(struct file *filp, struct vm_area_struct *vma)
 		failure_string = "bad vm_flags";
 		goto err_bad_arg;
 	}
-	vma->vm_flags = (vma->vm_flags | VM_DONTCOPY) & ~VM_MAYWRITE;
+	vma->vm_flags = (vma->vm_flags | VM_DONTCOPY | VM_MIXEDMAP) &
+							~VM_MAYWRITE;
 	vma->vm_ops = &binder_vm_ops;
 	vma->vm_private_data = proc;
 
drivers/android/binder_alloc.c +3 -3
index 5a426c877dfb..4f382d51def1 100644
@@ -219,7 +219,7 @@ static int binder_update_page_range(struct binder_alloc *alloc, int allocate,
 		mm = alloc->vma_vm_mm;
 
 	if (mm) {
-		down_write(&mm->mmap_sem);
+		down_read(&mm->mmap_sem);
 		vma = alloc->vma;
 	}
 
@@ -288,7 +288,7 @@ static int binder_update_page_range(struct binder_alloc *alloc, int allocate,
 		/* vm_insert_page does not seem to increment the refcount */
 	}
 	if (mm) {
-		up_write(&mm->mmap_sem);
+		up_read(&mm->mmap_sem);
 		mmput(mm);
 	}
 	return 0;
@@ -321,7 +321,7 @@ static int binder_update_page_range(struct binder_alloc *alloc, int allocate,
 	}
 err_no_vma:
 	if (mm) {
-		up_write(&mm->mmap_sem);
+		up_read(&mm->mmap_sem);
 		mmput(mm);
 	}
 	return vma ? -ENOMEM : -ESRCH;
Signature · 1 line
2.17.0.rc1.321.gba9d0f2565-goog
#2 lore raw
Quoted 21 lines · Minchan Kim
Quoted 8 lines · Minchan Kim

binder_update_page_range needs down_write of mmap_sem because vm_insert_page need to change vma->vm_flags to VM_MIXEDMAP unless it is set. However, when I profile binder working, it seems every binder buffers should be mapped in advance by binder_mmap. It means we could set VM_MIXEDMAP in bider_mmap time which is already hold a mmap_sem as down_write so binder_update_page_range doesn't need to hold a mmap_sem as down_write.

Quoted 8 lines

Android suffers from mmap_sem contention so let's reduce mmap_sem down_write.

Cc Todd Kjos <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc Arve Hjønnevåg <[email protected]>

Sent wrong version. Sorry for that. Please ignore this and take it.

Quoted 7 lines
Quoted 1 line

From 480e992d4a650fb98e1397114d75dea7af8e6d0c Mon Sep 17 00:00:00 2001

From: Minchan Kim <[email protected]>
Date: Wed, 28 Mar 2018 11:32:42 +0900
Subject: [PATCH v3] ANDROID: binder: change down_write to down_read
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

<snip>

I really don't want to hand-edit patches, please just send v4.

thanks,

greg k-h

#3 lore raw
In reply to Minchan Kim
I love your patch! Yet something to improve:

Hi Minchan,

I love your patch! Yet something to improve:

[auto build test ERROR on staging/staging-testing]
[also build test ERROR on v4.16-rc7 next-20180329]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url: https://github.com/0day-ci/linux/commits/Minchan-Kim/ANDROID-binder-change-down_write-to-down_read/20180330-043057
config: x86_64-randconfig-x014-201812 (attached as .config)
compiler: gcc-7 (Debian 7.3.0-1) 7.3.0
reproduce:

        # save the attached .config to linux build tree
        make ARCH=x86_64 

All errors (new ones prefixed by >>):

drivers//android/binder.c: In function 'binder_mmap':

>> drivers//android/binder.c:4725:24: error: 'struct vm_area_struct' has no member named 'flags'; did you mean 'vm_flags'?
     vma->vm_flags = (vma->flags | VM_DONTCOPY | VM_MIXEDMAP) &
                           ^~~~~
                           vm_flags

vim +4725 drivers//android/binder.c

4701
4702 static int binder_mmap(struct file *filp, struct vm_area_struct *vma)
4703 {
4704 int ret;
4705 struct binder_proc *proc = filp->private_data;
4706 const char *failure_string;
4707
4708 if (proc->tsk != current->group_leader)
4709 return -EINVAL;
4710
4711 if ((vma->vm_end - vma->vm_start) > SZ_4M)
4712 vma->vm_end = vma->vm_start + SZ_4M;
4713
4714 binder_debug(BINDER_DEBUG_OPEN_CLOSE,

  4715			     "%s: %d %lx-%lx (%ld K) vma %lx pagep %lx\n",
  4716			     __func__, proc->pid, vma->vm_start, vma->vm_end,
  4717			     (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
  4718			     (unsigned long)pgprot_val(vma->vm_page_prot));
  4719	
  4720		if (vma->vm_flags & FORBIDDEN_MMAP_FLAGS) {
  4721			ret = -EPERM;
  4722			failure_string = "bad vm_flags";
  4723			goto err_bad_arg;
  4724		}
> 4725		vma->vm_flags = (vma->flags | VM_DONTCOPY | VM_MIXEDMAP) &

4726 ~VM_MAYWRITE;
4727 vma->vm_ops = &binder_vm_ops;
4728 vma->vm_private_data = proc;
4729
4730 ret = binder_alloc_mmap_handler(&proc->alloc, vma);
4731 if (ret)
4732 return ret;
4733 mutex_lock(&proc->files_lock);
4734 proc->files = get_files_struct(current);
4735 mutex_unlock(&proc->files_lock);
4736 return 0;
4737
4738 err_bad_arg:
4739 pr_err("%s: %d %lx-%lx %s failed %d\n", __func__,
4740 proc->pid, vma->vm_start, vma->vm_end, failure_string, ret);
4741 return ret;
4742 }
4743

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation

#4 lore raw
Quoted 3 lines · kbuild test robot

Hi Minchan,

I love your patch! Yet something to improve:

Glad to hear.
It's first time someone loves my patch. ;-)

Quoted 12 lines

[auto build test ERROR on staging/staging-testing]
[also build test ERROR on v4.16-rc7 next-20180329]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url: https://github.com/0day-ci/linux/commits/Minchan-Kim/ANDROID-binder-change-down_write-to-down_read/20180330-043057
config: x86_64-randconfig-x014-201812 (attached as .config)
compiler: gcc-7 (Debian 7.3.0-1) 7.3.0
reproduce:

        # save the attached .config to linux build tree
        make ARCH=x86_64 
Quoted 7 lines

All errors (new ones prefixed by >>):

drivers//android/binder.c: In function 'binder_mmap':

Quoted 1 line
Quoted 1 line

drivers//android/binder.c:4725:24: error: 'struct vm_area_struct' has no member named 'flags'; did you mean 'vm_flags'?

     vma->vm_flags = (vma->flags | VM_DONTCOPY | VM_MIXEDMAP) &
                           ^~~~~
                           vm_flags

http://lkml.kernel.org/r/<[email protected]>

This time, I was little bit fast. :)

#5 lore raw
Quoted 7 lines · Minchan Kim
Quoted 3 lines · kbuild test robot

Hi Minchan,

I love your patch! Yet something to improve:

Glad to hear.
It's first time someone loves my patch. ;-)

FYI, that message originates from Linus. :-)

Quoted 14 lines
Quoted 14 lines

[auto build test ERROR on staging/staging-testing]
[also build test ERROR on v4.16-rc7 next-20180329]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url: https://github.com/0day-ci/linux/commits/Minchan-Kim/ANDROID-binder-change-down_write-to-down_read/20180330-043057
config: x86_64-randconfig-x014-201812 (attached as .config)
compiler: gcc-7 (Debian 7.3.0-1) 7.3.0
reproduce:

        # save the attached .config to linux build tree
        make ARCH=x86_64

All errors (new ones prefixed by >>):

drivers//android/binder.c: In function 'binder_mmap':

Quoted 8 lines
Quoted 4 lines
Quoted 1 line
Quoted 1 line

drivers//android/binder.c:4725:24: error: 'struct vm_area_struct' has no member named 'flags'; did you mean 'vm_flags'?

     vma->vm_flags = (vma->flags | VM_DONTCOPY | VM_MIXEDMAP) &
                           ^~~~~
                           vm_flags

http://lkml.kernel.org/r/<[email protected]>

This time, I was little bit fast. :)

Hurrah, quick hands! :)

Cheers,
Fengguang