Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp441738ybt; Fri, 10 Jul 2020 04:01:09 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwMWQ4CbZHx9vnhs1yvx1sAU/JwxrpuWJ7J6tebOFHlQOkJpbgVKHL1vkPhj3Zi2w65TC8P X-Received: by 2002:a17:906:9244:: with SMTP id c4mr59673262ejx.60.1594378869021; Fri, 10 Jul 2020 04:01:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1594378869; cv=none; d=google.com; s=arc-20160816; b=cNooTSDl7245y5Vq/5YMQEtx8UYGSNS/5PxiG2rPey8xzlVcO+0vhjDBTK9F7DchjH ZsLPqyIiBidf+BFRrPewqbMn5aUC3ZdWtOOuvOjXSgBDgIgpJaLf5U6f3ZHwggFBm5Ge HP57W/vcIRQwK74QN/ZYrYrAz0IQ3AXry7yPhTU8nMiydOdgszfIQVvlux97osAgCKgG Ue4nNvUPraxiXh0y3lKYBZVApSjf+ldVd/LjGgRtHl/QNwlCL6wINlAfttJ933N7F2OG fSKcTKYx8HT0PPHW+qioJMy0EEQjnZ6hVYs/njSQgNaDM+nFFMdLzo2sGZaRv1MtcLbm uoCw== 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 :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject; bh=YVzfrexM5WYb6cUl0ZTJ8eIucqMPalSdO1Z0Rvmma7c=; b=NBvEjhaEXUx3BenYS+mZ+8xlL5G/6aZ01IXd0yvl7sKZKNML2kSGgepX8dUlh/7M2a gUkn63DlxRpwHr1QOxSyLyZOHCTzMWkYzLNPpSjtB/HsdVfydcyGXGxnAn2CWs/yFaWk KWrEyvib4wSlTQjpyfqPuV0WptnXtn9BzQRd7+B55V/NLNLpu4++fCwjXvsdoulQHrru iItTErqAOnDA4YGrCIKGoemfphW4dpIV7jTSc4m2qdDzmaMKZgLbqnggr62gZNyywoyx Kt+v55m3xQHJP7D0zO/cvVUL+I1lw5MA2yB+Fl18AGL5be7DyCARnaAlj82bIsI6HS8U fZJQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id d15si3811044ejt.185.2020.07.10.04.00.45; Fri, 10 Jul 2020 04:01:09 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726816AbgGJK6d (ORCPT + 99 others); Fri, 10 Jul 2020 06:58:33 -0400 Received: from mx2.suse.de ([195.135.220.15]:40754 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726369AbgGJK6c (ORCPT ); Fri, 10 Jul 2020 06:58:32 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 7A06BAD76; Fri, 10 Jul 2020 10:58:30 +0000 (UTC) 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, Yang Shi , Oleg Nesterov , Matthew Wilcox References: <20200709105309.42495-1-kirill.shutemov@linux.intel.com> From: Vlastimil Babka Message-ID: <6f96a9ed-3a24-2478-b84d-5e213baa16d4@suse.cz> Date: Fri, 10 Jul 2020 12:58:29 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.9.0 MIME-Version: 1.0 In-Reply-To: <20200709105309.42495-1-kirill.shutemov@linux.intel.com> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 7/9/20 12:53 PM, 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. > > 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 Acked-by: Vlastimil Babka Thanks! > --- > 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); >