Received: by 2002:ac0:a594:0:0:0:0:0 with SMTP id m20-v6csp249197imm; Tue, 15 May 2018 00:47:56 -0700 (PDT) X-Google-Smtp-Source: AB8JxZqOS7ss6SV95if+Pr8nOVe+OEKWpVTvqYE4W3Y7fR5MpBTr1S0KwX2yIJU25ZRS0LOKXupG X-Received: by 2002:a63:6f8a:: with SMTP id k132-v6mr3248740pgc.200.1526370476513; Tue, 15 May 2018 00:47:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1526370476; cv=none; d=google.com; s=arc-20160816; b=eNG7d0z2IAloGocXPeOwXavWXxAGAVSiWFyh/o5tQNozhky6RMIMiKB1yVtnhhM39P 0AZLOyoA+BENSPdbAd5OFrSmGvhVd/jgpdbLD88Pd1tPeDZtjOYHVuFQqXWsDHIQpDfM 8DMfN4sjV3Bg6JaJwHtprFwWFokfihGcQcGzN4+rbZ3I+7OSgMfVdEmXtUyEurp5aasD YM7WOIqJqUWZl9kY1iRz2RsqWJoWkJgFm84A4521p69nQyPO/OwyYZvBibr/2wK9X24K fmCRNWgt0HueB6lPmYEyPTvME44B/eApDNPjWd+9jZ+14QSJxugZbkuXb/xoUPJxq44o nFxQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:cc:to:subject :message-id:date:from:references:in-reply-to:mime-version :dkim-signature:arc-authentication-results; bh=34IV/9TJKLpPf2RAETbIYSu4E5PE1Kwu0aOyMq9Hf6c=; b=ltfeQTHU2AVKLAaaUW+mjC+JqolZouHpqjwgSRkZXU0dKhoLe4A0WauyRIFkchPwzq q1PWw8BcxbYVO7gsodZYwsf+xANfy90oEFlukko9q8jcUY0hbuXRLkrDptvJGzBFav6d I+ElfDXR5WHeFzilX2Qop8YRBkV8FSEMc/K4zlmJ6Q1RhEbtUlxkVJxPi2gfy/h3vXeh uw3s5c+jx4k9wqnEhEr6bSaK88VkjfkhpvyFzwxxInpYuAdCgAEC33jCOD7HK7OJANol kQuZbVa5NkusT1gQb59UIhNbEgMuweCco/DHzTNvoHin4iSf8QQI0ijk3gaY2lSn2PX8 UEdA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@android.com header.s=20161025 header.b=mDMVhP3K; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=android.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id f59-v6si11234683plb.106.2018.05.15.00.47.42; Tue, 15 May 2018 00:47:56 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@android.com header.s=20161025 header.b=mDMVhP3K; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=android.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752378AbeEOHqP (ORCPT + 99 others); Tue, 15 May 2018 03:46:15 -0400 Received: from mail-it0-f67.google.com ([209.85.214.67]:35889 "EHLO mail-it0-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752090AbeEOHqC (ORCPT ); Tue, 15 May 2018 03:46:02 -0400 Received: by mail-it0-f67.google.com with SMTP id e20-v6so17892948itc.1 for ; Tue, 15 May 2018 00:46:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=android.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=34IV/9TJKLpPf2RAETbIYSu4E5PE1Kwu0aOyMq9Hf6c=; b=mDMVhP3K46Nlf0jmig8HhMwKjQwRslrD7iON2N68z2aqYUgU8mT09kGJ06yMG7/DnI NpRWYUy8EeoYQbeIx8D8L149sEaS7Ek9seJ1vFzWt/McNK9rNnb35uoVY1nh72LWlU54 o83e4qlWdZh/S/Vnsm4A74lEbtDgDA6WAD/OAafWTW3i8MP6Jg5z9tJUDIUgZn4dkEuC C1ZT1jGoPzHWHmbEH9sqNOHh0mliaoRfVADm3qIEgpDAousD6nVtYvI0/6cewY0AXwdZ OIXp+FzTVK9dZL+N9adPIcqgwZBtJ08oiLGD7ijxkOkXIpHuihpgLlxXyxlXpn7/r5M0 tdpQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=34IV/9TJKLpPf2RAETbIYSu4E5PE1Kwu0aOyMq9Hf6c=; b=QxxlkexUrdceiGtldOnT80dSZHn8JSw9JokkgKvtxw390debVP06lovjjK4KPEv7aI x1socZMDTtRp2G6HYQOKUvYZq/XP3aKJ//zaIIJQu42FMgPc6ALI4MOvUnedaWeZrr+J 7mPtFW7XxbjrJ7zOeO4g/mZLF2ko/mfYojkfzQVE+pjlYA8DUtk7UCxRhLJxCdVpSOKm tpbAu/PWubE2UrFagOtQYVmIZR/QQvJk5z6hrIwbUb+wnBmRnCoDaX7S2J/VboyKwKVu urZdlwyJmbNLjH30Y4jjzYYqpWks9XfCZG6+sZVc+SDxDbdc+I8/+6FmVwWJ7+Ay0NuF JvXQ== X-Gm-Message-State: ALKqPwcHFx+65cvhUI1Tt+NL1pGDIapNhgtLwws2ZpspoXje1PyaETVj VBB2hNQqv+PtREcQz2CHhBysHa9DMt02Vxp92yDdFQ== X-Received: by 2002:a6b:b74b:: with SMTP id h72-v6mr14044301iof.201.1526370361987; Tue, 15 May 2018 00:46:01 -0700 (PDT) MIME-Version: 1.0 Received: by 2002:a4f:e043:0:0:0:0:0 with HTTP; Tue, 15 May 2018 00:46:01 -0700 (PDT) In-Reply-To: <20180515062732.GA183759@rodete-desktop-imager.corp.google.com> References: <20180507141537.4855-1-minchan@kernel.org> <20180507172829.GA66161@joelaf.mtv.corp.google.com> <20180508105101.GB8209@rodete-desktop-imager.corp.google.com> <20180508230813.GA102094@joelaf.mtv.corp.google.com> <20180509064023.GD8209@rodete-desktop-imager.corp.google.com> <20180509231941.GA4790@joelaf.mtv.corp.google.com> <20180515062732.GA183759@rodete-desktop-imager.corp.google.com> From: Martijn Coenen Date: Tue, 15 May 2018 09:46:01 +0200 Message-ID: Subject: Re: [PATCH v6] ANDROID: binder: change down_write to down_read To: Minchan Kim Cc: Joel Fernandes , LKML , Ganesh Mahendran , Joe Perches , =?UTF-8?B?QXJ2ZSBIasO4bm5ldsOlZw==?= , Todd Kjos , Greg Kroah-Hartman Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, May 15, 2018 at 8:27 AM, Minchan Kim wro= te: > Hi Joel, > > Sorry for the late response. I was off. > > On Wed, May 09, 2018 at 04:19:41PM -0700, Joel Fernandes wrote: >> On Wed, May 09, 2018 at 03:40:23PM +0900, Minchan Kim wrote: >> > On Tue, May 08, 2018 at 04:08:13PM -0700, Joel Fernandes wrote: >> > > On Tue, May 08, 2018 at 07:51:01PM +0900, Minchan Kim wrote: >> > > > On Mon, May 07, 2018 at 10:28:29AM -0700, Joel Fernandes wrote: >> > > > > On Mon, May 07, 2018 at 11:15:37PM +0900, Minchan Kim wrote: >> > > > > > binder_update_page_range needs down_write of mmap_sem because >> > > > > > vm_insert_page need to change vma->vm_flags to VM_MIXEDMAP unl= ess >> > > > > > it is set. However, when I profile binder working, it seems >> > > > > > every binder buffers should be mapped in advance by binder_mma= p. >> > > > > > It means we could set VM_MIXEDMAP in binder_mmap time which is >> > > > > > already hold a mmap_sem as down_write so binder_update_page_ra= nge >> > > > > > doesn't need to hold a mmap_sem as down_write. >> > > > > > Please use proper API down_read. It would help mmap_sem conten= tion >> > > > > > problem as well as fixing down_write abuse. >> > > > > > >> > > > > > Ganesh Mahendran tested app launching and binder throughput te= st >> > > > > > and he said he couldn't find any problem and I did binder late= ncy >> > > > > > test per Greg KH request(Thanks Martijn to teach me how I can = do) >> > > > > > I cannot find any problem, too. >> > > > > > >> > > > > > Cc: Ganesh Mahendran >> > > > > > Cc: Joe Perches >> > > > > > Cc: Arve Hj=C3=B8nnev=C3=A5g >> > > > > > Cc: Todd Kjos >> > > > > > Cc: Greg Kroah-Hartman >> > > > > > Reviewed-by: Martijn Coenen >> > > > > > Signed-off-by: Minchan Kim >> > > > > > --- >> > > > > > drivers/android/binder.c | 4 +++- >> > > > > > drivers/android/binder_alloc.c | 6 +++--- >> > > > > > 2 files changed, 6 insertions(+), 4 deletions(-) >> > > > > > >> > > > > > diff --git a/drivers/android/binder.c b/drivers/android/binder= .c >> > > > > > index 4eab5be3d00f..7b8e96f60719 100644 >> > > > > > --- a/drivers/android/binder.c >> > > > > > +++ b/drivers/android/binder.c >> > > > > > @@ -4730,7 +4730,9 @@ static int binder_mmap(struct file *filp= , struct vm_area_struct *vma) >> > > > > > failure_string =3D "bad vm_flags"; >> > > > > > goto err_bad_arg; >> > > > > > } >> > > > > > - vma->vm_flags =3D (vma->vm_flags | VM_DONTCOPY) & ~VM_MAYW= RITE; >> > > > > > + vma->vm_flags |=3D VM_DONTCOPY | VM_MIXEDMAP; >> > > > > > + vma->vm_flags &=3D ~VM_MAYWRITE; >> > > > > > + >> > > > > > vma->vm_ops =3D &binder_vm_ops; >> > > > > > vma->vm_private_data =3D proc; >> > > > > > >> > > > > > diff --git a/drivers/android/binder_alloc.c b/drivers/android/= binder_alloc.c >> > > > > > index 5a426c877dfb..4f382d51def1 100644 >> > > > > > --- a/drivers/android/binder_alloc.c >> > > > > > +++ b/drivers/android/binder_alloc.c >> > > > > > @@ -219,7 +219,7 @@ static int binder_update_page_range(struct= binder_alloc *alloc, int allocate, >> > > > > > mm =3D alloc->vma_vm_mm; >> > > > > > >> > > > > > if (mm) { >> > > > > > - down_write(&mm->mmap_sem); >> > > > > > + down_read(&mm->mmap_sem); >> > > > > >> > > > > >> > > > > Nice. Is there a need to hold the reader-lock at all here? Just = curious what >> > > > > else is it protecting (here or in vm_insert_page). >> > > > >> > > > It should protect vm_area_struct. IOW, when we try insert page int= o virtual address area, >> > > > vma shouldn't be changed(ie, unmap/collapse/split). >> > > >> > > When you say unmap, are you talking about pages being unmapped from = the VMA >> > > or something else? >> > >> > I mean to destroy vm_area_struct itself as well as unmap pages. >> > >> > > >> > > For the collapse/split part, the binder VMA (vm_area_struct) itself = isn't >> > > changed after the initial mmap (AFAIK) so I don't see a need for pro= tection >> > > there. >> > >> > There is no way to unmap in runtime? What happens if some buggy applic= ations >> > do unmap by mistake? >> > Cannot we access those B's vma from A context? >> > If B process is exiting, the VMA would be gone while A is accessing. >> >> If the VMA is gone, then why is it a good idea to map pages into it anyw= ay? > > Hmm, sorry about that. I couldn't understand your point. I think Joel may have meant that if the VMA is gone (forex because the process unmaps or dies), we shouldn't allocate pages into that VMA anymore. I don't think we do that currently - we check if the vma is still valid before allocating. > >> >> About the unmap at runtime part, your commit message was a bit confusing= . You >> said "every binder buffers should be mapped in advance by binder_mmap." = but I >> think the new binder shrinker mechanism doesn't make that true anymore. > > It's good point. I didn't know know that. > When I see binder_vm_fault, it emits SIGBUS. That means shrinker cannot z= ap pages > process is using, I think. IOW, every pages for binder are mapped at mmap= time > and is never mapped in runtime by page fault. Right? Right - the address range is allocated once, and an initial amount of pages is mapped into it. For every transaction into that process, we will see if there's enough pages, and if not allocate so that we have enough of them - so this is not done by page fault. The shrinker won't touch pages for which a transaction is in progress. Of course a process itself could still try to read from an unallocated address, but in that case returning SIGBUS and having that process crash seems fine. I'm also not sure the read lock is needed, but I would need to read a whole lot more code to convince myself it's not. > > Thanks. > >> >> The unmap by mistake point is a valid one I guess. >> >> thanks, >> >> - Joel >>