From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-11.2 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY, SPF_HELO_NONE,SPF_PASS,UNPARSEABLE_RELAY,URIBL_BLOCKED,USER_AGENT_SANE_1 autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id A2896C433E5 for ; Fri, 10 Jul 2020 17:36:10 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 4CE3C2078D for ; Fri, 10 Jul 2020 17:36:10 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 4CE3C2078D Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=linux.alibaba.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id BD8348D0003; Fri, 10 Jul 2020 13:36:09 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B895B8D0001; Fri, 10 Jul 2020 13:36:09 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A77678D0003; Fri, 10 Jul 2020 13:36:09 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0128.hostedemail.com [216.40.44.128]) by kanga.kvack.org (Postfix) with ESMTP id 91D5F8D0001 for ; Fri, 10 Jul 2020 13:36:09 -0400 (EDT) Received: from smtpin10.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 3A59C349B for ; Fri, 10 Jul 2020 17:36:09 +0000 (UTC) X-FDA: 77022869658.10.jump83_10002f026ed0 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin10.hostedemail.com (Postfix) with ESMTP id 0CED116A047 for ; Fri, 10 Jul 2020 17:36:09 +0000 (UTC) X-HE-Tag: jump83_10002f026ed0 X-Filterd-Recvd-Size: 4337 Received: from out30-43.freemail.mail.aliyun.com (out30-43.freemail.mail.aliyun.com [115.124.30.43]) by imf16.hostedemail.com (Postfix) with ESMTP for ; Fri, 10 Jul 2020 17:36:07 +0000 (UTC) X-Alimail-AntiSpam:AC=PASS;BC=-1|-1;BR=01201311R181e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=e01e07425;MF=yang.shi@linux.alibaba.com;NM=1;PH=DS;RN=9;SR=0;TI=SMTPD_---0U2JRgj0_1594402558; Received: from US-143344MP.local(mailfrom:yang.shi@linux.alibaba.com fp:SMTPD_---0U2JRgj0_1594402558) by smtp.aliyun-inc.com(127.0.0.1); Sat, 11 Jul 2020 01:36:00 +0800 Subject: Re: [PATCH] mm: Close race between munmap() and expand_upwards()/downwards() To: "Kirill A. Shutemov" , Andrew Morton Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Jann Horn , stable@vger.kernel.org, Vlastimil Babka , Oleg Nesterov , Matthew Wilcox References: <20200709105309.42495-1-kirill.shutemov@linux.intel.com> From: Yang Shi Message-ID: Date: Fri, 10 Jul 2020 10:35:50 -0700 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:52.0) Gecko/20100101 Thunderbird/52.7.0 MIME-Version: 1.0 In-Reply-To: <20200709105309.42495-1-kirill.shutemov@linux.intel.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Content-Language: en-US X-Rspamd-Queue-Id: 0CED116A047 X-Spamd-Result: default: False [0.00 / 100.00] X-Rspamd-Server: rspam02 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On 7/9/20 3:53 AM, Kirill A. Shutemov wrote: > VMA with VM_GROWSDOWN or VM_GROWSUP flag set can change their size under > mmap_read_lock(). It can lead to race with __do_munmap(): > > Thread A Thread B > __do_munmap() > detach_vmas_to_be_unmapped() > mmap_write_downgrade() > expand_downwards() > vma->vm_start = address; > // The VMA now overlaps with > // VMAs detached by the Thread A > // page fault populates expanded part > // of the VMA > unmap_region() > // Zaps pagetables partly > // populated by Thread B > > Similar race exists for expand_upwards(). > > The fix is to avoid downgrading mmap_lock in __do_munmap() if detached > VMAs are next to VM_GROWSDOWN or VM_GROWSUP VMA. Thanks for catching this. The fix makes sense to me. Reviewed-by: Yang Shi > > Signed-off-by: Kirill A. Shutemov > Reported-by: Jann Horn > Fixes: dd2283f2605e ("mm: mmap: zap pages with read mmap_sem in munmap") > Cc: # 4.20 > Cc: Yang Shi > Cc: Vlastimil Babka > Cc: Oleg Nesterov > Cc: Matthew Wilcox > --- > mm/mmap.c | 16 ++++++++++++++-- > 1 file changed, 14 insertions(+), 2 deletions(-) > > diff --git a/mm/mmap.c b/mm/mmap.c > index 59a4682ebf3f..71df4b36b42a 100644 > --- a/mm/mmap.c > +++ b/mm/mmap.c > @@ -2620,7 +2620,7 @@ static void unmap_region(struct mm_struct *mm, > * Create a list of vma's touched by the unmap, removing them from the mm's > * vma list as we go.. > */ > -static void > +static bool > detach_vmas_to_be_unmapped(struct mm_struct *mm, struct vm_area_struct *vma, > struct vm_area_struct *prev, unsigned long end) > { > @@ -2645,6 +2645,17 @@ detach_vmas_to_be_unmapped(struct mm_struct *mm, struct vm_area_struct *vma, > > /* Kill the cache */ > vmacache_invalidate(mm); > + > + /* > + * Do not downgrade mmap_sem if we are next to VM_GROWSDOWN or > + * VM_GROWSUP VMA. Such VMAs can change their size under > + * down_read(mmap_sem) and collide with the VMA we are about to unmap. > + */ > + if (vma && (vma->vm_flags & VM_GROWSDOWN)) > + return false; > + if (prev && (prev->vm_flags & VM_GROWSUP)) > + return false; > + return true; > } > > /* > @@ -2825,7 +2836,8 @@ int __do_munmap(struct mm_struct *mm, unsigned long start, size_t len, > } > > /* Detach vmas from rbtree */ > - detach_vmas_to_be_unmapped(mm, vma, prev, end); > + if (!detach_vmas_to_be_unmapped(mm, vma, prev, end)) > + downgrade = false; > > if (downgrade) > mmap_write_downgrade(mm);