Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp876167ybv; Wed, 19 Feb 2020 11:04:46 -0800 (PST) X-Google-Smtp-Source: APXvYqzFM97h4lBbzhJpFRv5wtoIXPK9giPSd/7jbucmczWrxRuTtUkopKevpu+NelWmFtxgeXtA X-Received: by 2002:a9d:7304:: with SMTP id e4mr19057926otk.99.1582139086100; Wed, 19 Feb 2020 11:04:46 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1582139086; cv=none; d=google.com; s=arc-20160816; b=wQ/Afe82OTmWDE5JcdhmOQJgc90sSDPtEOBIA8BZVKgUvkcBM7MgthibVAOuzpDMiq zgpOzk/5xBHp8i9LeMC8eEz9If4qBJ4Fqc1SH3Pf7x/wlrC7lWPZ0RVbtknqFAI3isyt sclkgAdSVTPim/YAzbi5r7yvrMEjpxCxu3Rlg4NKVP2VXpndpsiG56SZu5JUzeyxv5N2 O0qUchVwS+52DgAKJWpHjZQbuRQj+M50lh+4aOesLl/U8UeLR3JcwkNptIGIQUWnQgFk hCLAZgt5KM4cjDrVJHXGooVtD5TiqfqpMRNDHeCJJKc/4Q8iWl2ptddj4HH9IpgXrNGe nKiA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:reply-to:message-id :subject:cc:to:from:date:dkim-signature; bh=VDXAmsdWl4PpjnnChgO+PNQx1Wo+XnGCqN1ezWhkqkg=; b=Y6GNUr01gXPQUa+IFdzL1SGlPg7Etted7qyzbmgRk+m4hPWgEYgm150Hcegio1VOh4 LGxBMiKRQZF986KVLO+mq8a2LRXoRsp75Wi5gCCiaEvXIdj8u0il8Dg2Tz2P+ie8p+Np xqqkFQZnSknd4HPNMs1DEfuDTHwvNFm2DhCawjabXuYB8pGECqyJTDMaAtnsCYXlCQki sVe1R4VQd/8qbHu6YcLUqYUmFMLh6I7B7eo/oMN8/Yx/M4UcMqUPkZRvNLXqNLRqiHLu Bkq/nkjo2pKTK2Vm6jhtpOZNKsrHumJ9ovrqsCPY4bqZ+MURhThU9Do9FBll5jhJXM6F +BRA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=PFi6l+Ns; 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=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 6si303213otv.224.2020.02.19.11.04.34; Wed, 19 Feb 2020 11:04:46 -0800 (PST) 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=@kernel.org header.s=default header.b=PFi6l+Ns; 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=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726777AbgBSTEZ (ORCPT + 99 others); Wed, 19 Feb 2020 14:04:25 -0500 Received: from mail.kernel.org ([198.145.29.99]:41478 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726643AbgBSTEZ (ORCPT ); Wed, 19 Feb 2020 14:04:25 -0500 Received: from paulmck-ThinkPad-P72.home (50-39-105-78.bvtn.or.frontiernet.net [50.39.105.78]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id B85A7206DB; Wed, 19 Feb 2020 19:04:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1582139063; bh=mezGysAMCe50aogHaA5sl++kWa73c/LuEVAzNwLfPVQ=; h=Date:From:To:Cc:Subject:Reply-To:References:In-Reply-To:From; b=PFi6l+Ns2YnYnF3DJ3EE/KGh6WUog0ugjnK0XQt/MjpfflUUNyfO8/AK8xKptXr1o mvEKmKSzQyOIUIbSvexr/dZVL6VRcSRXXmywgferlUkefCORd5jCByI560cUCmBlWI kYgvy2kBxq7GYvSyeV7e4YWYswiCNRtrcxAonH6Q= Received: by paulmck-ThinkPad-P72.home (Postfix, from userid 1000) id 8FB263520BB6; Wed, 19 Feb 2020 11:04:23 -0800 (PST) Date: Wed, 19 Feb 2020 11:04:23 -0800 From: "Paul E. McKenney" To: Qian Cai Cc: akpm@linux-foundation.org, kirill@shutemov.name, elver@google.com, peterz@infradead.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH -next v2] fork: annotate a data race in vm_area_dup() Message-ID: <20200219190423.GN2935@paulmck-ThinkPad-P72> Reply-To: paulmck@kernel.org References: <1582122495-12885-1-git-send-email-cai@lca.pw> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1582122495-12885-1-git-send-email-cai@lca.pw> User-Agent: Mutt/1.9.4 (2018-02-28) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Feb 19, 2020 at 09:28:15AM -0500, Qian Cai wrote: > struct vm_area_struct could be accessed concurrently as noticed by > KCSAN, > > write to 0xffff9cf8bba08ad8 of 8 bytes by task 14263 on cpu 35: > vma_interval_tree_insert+0x101/0x150: > rb_insert_augmented_cached at include/linux/rbtree_augmented.h:58 > (inlined by) vma_interval_tree_insert at mm/interval_tree.c:23 > __vma_link_file+0x6e/0xe0 > __vma_link_file at mm/mmap.c:629 > vma_link+0xa2/0x120 > mmap_region+0x753/0xb90 > do_mmap+0x45c/0x710 > vm_mmap_pgoff+0xc0/0x130 > ksys_mmap_pgoff+0x1d1/0x300 > __x64_sys_mmap+0x33/0x40 > do_syscall_64+0x91/0xc44 > entry_SYSCALL_64_after_hwframe+0x49/0xbe > > read to 0xffff9cf8bba08a80 of 200 bytes by task 14262 on cpu 122: > vm_area_dup+0x6a/0xe0 > vm_area_dup at kernel/fork.c:362 > __split_vma+0x72/0x2a0 > __split_vma at mm/mmap.c:2661 > split_vma+0x5a/0x80 > mprotect_fixup+0x368/0x3f0 > do_mprotect_pkey+0x263/0x420 > __x64_sys_mprotect+0x51/0x70 > do_syscall_64+0x91/0xc44 > entry_SYSCALL_64_after_hwframe+0x49/0xbe > > vm_area_dup() blindly copies all fields of original VMA to the new one. > This includes coping vm_area_struct::shared.rb which is normally > protected by i_mmap_lock. But this is fine because the read value will > be overwritten on the following __vma_link_file() under proper > protection. Thus, mark it as an intentional data race and insert a few > assertions for the fields that should not be modified concurrently. > > Signed-off-by: Qian Cai Queued for safekeeping on -rcu. I had to adjust a bit to get it to apply on -rcu, please see below. In my experience, git should have no trouble figuring it out. ;-) Thanx, Paul ------------------------------------------------------------------------ commit 1228aca56f2a25b67876d8a819437b620a6e1cee Author: Qian Cai Date: Wed Feb 19 11:00:54 2020 -0800 fork: Annotate a data race in vm_area_dup() struct vm_area_struct could be accessed concurrently as noticed by KCSAN, write to 0xffff9cf8bba08ad8 of 8 bytes by task 14263 on cpu 35: vma_interval_tree_insert+0x101/0x150: rb_insert_augmented_cached at include/linux/rbtree_augmented.h:58 (inlined by) vma_interval_tree_insert at mm/interval_tree.c:23 __vma_link_file+0x6e/0xe0 __vma_link_file at mm/mmap.c:629 vma_link+0xa2/0x120 mmap_region+0x753/0xb90 do_mmap+0x45c/0x710 vm_mmap_pgoff+0xc0/0x130 ksys_mmap_pgoff+0x1d1/0x300 __x64_sys_mmap+0x33/0x40 do_syscall_64+0x91/0xc44 entry_SYSCALL_64_after_hwframe+0x49/0xbe read to 0xffff9cf8bba08a80 of 200 bytes by task 14262 on cpu 122: vm_area_dup+0x6a/0xe0 vm_area_dup at kernel/fork.c:362 __split_vma+0x72/0x2a0 __split_vma at mm/mmap.c:2661 split_vma+0x5a/0x80 mprotect_fixup+0x368/0x3f0 do_mprotect_pkey+0x263/0x420 __x64_sys_mprotect+0x51/0x70 do_syscall_64+0x91/0xc44 entry_SYSCALL_64_after_hwframe+0x49/0xbe vm_area_dup() blindly copies all fields of original VMA to the new one. This includes coping vm_area_struct::shared.rb which is normally protected by i_mmap_lock. But this is fine because the read value will be overwritten on the following __vma_link_file() under proper protection. Thus, mark it as an intentional data race and insert a few assertions for the fields that should not be modified concurrently. Signed-off-by: Qian Cai Signed-off-by: Paul E. McKenney diff --git a/kernel/fork.c b/kernel/fork.c index 60a1295..e592e6f 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -359,7 +359,13 @@ struct vm_area_struct *vm_area_dup(struct vm_area_struct *orig) struct vm_area_struct *new = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL); if (new) { - *new = *orig; + ASSERT_EXCLUSIVE_WRITER(orig->vm_flags); + ASSERT_EXCLUSIVE_WRITER(orig->vm_file); + /* + * orig->shared.rb may be modified concurrently, but the clone + * will be reinitialized. + */ + *new = data_race(*orig); INIT_LIST_HEAD(&new->anon_vma_chain); } return new;