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=-5.0 required=3.0 tests=DKIM_INVALID,DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,USER_AGENT_SANE_2 autolearn=ham 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 57AC2C34026 for ; Tue, 18 Feb 2020 16:46:41 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 07D15208C4 for ; Tue, 18 Feb 2020 16:46:40 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=lca.pw header.i=@lca.pw header.b="rIWSKd/B" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 07D15208C4 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=lca.pw Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 8F5896B0003; Tue, 18 Feb 2020 11:46:40 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 8A7596B0006; Tue, 18 Feb 2020 11:46:40 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7BB7E6B0007; Tue, 18 Feb 2020 11:46:40 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0102.hostedemail.com [216.40.44.102]) by kanga.kvack.org (Postfix) with ESMTP id 63A9C6B0003 for ; Tue, 18 Feb 2020 11:46:40 -0500 (EST) Received: from smtpin19.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 17EAB824805A for ; Tue, 18 Feb 2020 16:46:40 +0000 (UTC) X-FDA: 76503826560.19.burn02_3a1c52b30be0c X-HE-Tag: burn02_3a1c52b30be0c X-Filterd-Recvd-Size: 7927 Received: from mail-qt1-f194.google.com (mail-qt1-f194.google.com [209.85.160.194]) by imf50.hostedemail.com (Postfix) with ESMTP for ; Tue, 18 Feb 2020 16:46:39 +0000 (UTC) Received: by mail-qt1-f194.google.com with SMTP id v25so14964592qto.7 for ; Tue, 18 Feb 2020 08:46:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=lca.pw; s=google; h=message-id:subject:from:to:cc:date:in-reply-to:references :mime-version:content-transfer-encoding; bh=T8bF+pORiY8PeSjCtC7UrykFxDxF7KoVLB+X2H57+r8=; b=rIWSKd/B0aB55Dekd8pjLuev1MFl1RfX1mliLmvbPkCvo+JLAZEL2J2dLtEf783Z/Z 26EVH0WjKIHmq1nL02rV9crhpo67vDdn3gztNN+jGQ14DQIE8Q/XOpg6B75SYoeCHjBl a3U9qFj8M6ZqIF2lsDXu0Jgs68cptuAqM26s4Go3hy/2pS5rwgkooU86VOaU5bh7ZSfX CS8b7zm6IYngDIimj58tDJpjwpWQoes56Bf1knLqLRzvgtEJeA9zvkEr4SHRH30sM9wz qMjksu2NFsvtGUtCRN0K9JFGKN9dB7S7WEepbkdCZN/Fpunz5G6/qooYvXJrKrJ8xMm/ dhXA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:message-id:subject:from:to:cc:date:in-reply-to :references:mime-version:content-transfer-encoding; bh=T8bF+pORiY8PeSjCtC7UrykFxDxF7KoVLB+X2H57+r8=; b=JZOWoZbEuzT0CqhxpSxVhKPtoLc+njvU9FaYFwRWBcbyKY5IRCcgAOhmJrpeysPz7B I2oAd86j0XO/Q8rwc+s0zC0OYwT+qmr9FMbtffnwA2R0p0E8vm7I98MGP5kBpa5gdeU4 4yp4u1KHOa+Gp8VCJI2QHIm2K6YTKSEe347WDFFqSSCJShJ9z2DSvuZhUZCStLLTVKRL tj8MWk1hoWt13cmOpTL/ObGIp5c7c3N1oju32XER49q9qtelqik3K8wsNnhzk5Nu7GKs Wo1GbiV5sQzi2824I5RAU6RsbYKL9O4dky1UGce9QnxdzzLweVSTnz9eeiS0TZPX2ME2 ZKTQ== X-Gm-Message-State: APjAAAXK6wU38oy2LvCXgrBombi4ZzAvPYe6VBNKVC+zfmTr7iBRjN9G NhrcyQoEUuk/o9Pqp+nan3TeNw== X-Google-Smtp-Source: APXvYqy0PMHgd6DS8SURJABmhNCWT9jAkeYA+IlfYABnhpIgMJOcH/r3wU+tuj7gEABHol3f593SBQ== X-Received: by 2002:ac8:6bce:: with SMTP id b14mr17799998qtt.200.1582044398605; Tue, 18 Feb 2020 08:46:38 -0800 (PST) Received: from dhcp-41-57.bos.redhat.com (nat-pool-bos-t.redhat.com. [66.187.233.206]) by smtp.gmail.com with ESMTPSA id m54sm2098569qtf.67.2020.02.18.08.46.37 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 18 Feb 2020 08:46:38 -0800 (PST) Message-ID: <1582044396.7365.94.camel@lca.pw> Subject: Re: [PATCH -next] fork: annotate a data race in vm_area_dup() From: Qian Cai To: "Kirill A. Shutemov" Cc: Marco Elver , Andrew Morton , Linux Memory Management List , LKML , Peter Zijlstra , syzbot , syzkaller-bugs Date: Tue, 18 Feb 2020 11:46:36 -0500 In-Reply-To: <20200218151813.3yzzb2hzlmtbf5xg@box> References: <20200218103002.6rtjreyqjepo3yxe@box> <93E6B243-9A0F-410C-8EE4-9D57E28AF5AF@lca.pw> <1582038035.7365.93.camel@lca.pw> <20200218151813.3yzzb2hzlmtbf5xg@box> Content-Type: text/plain; charset="UTF-8" X-Mailer: Evolution 3.22.6 (3.22.6-10.el7) Mime-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Bogosity: Ham, tests=bogofilter, spamicity=0.000056, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Tue, 2020-02-18 at 18:18 +0300, Kirill A. Shutemov wrote: > On Tue, Feb 18, 2020 at 10:00:35AM -0500, Qian Cai wrote: > > On Tue, 2020-02-18 at 15:09 +0100, Marco Elver wrote: > > > On Tue, 18 Feb 2020 at 13:40, Qian Cai wrote: > > > >=20 > > > >=20 > > > >=20 > > > > > On Feb 18, 2020, at 5:29 AM, Kirill A. Shutemov wrote: > > > > >=20 > > > > > I think I've got this: > > > > >=20 > > > > > vm_area_dup() blindly copies all fields of orignal VMA to the n= ew one. > > > > > This includes coping vm_area_struct::shared.rb which is normall= y protected > > > > > by i_mmap_lock. But this is fine because the read value will be > > > > > overwritten on the following __vma_link_file() under proper pro= tectection. > > > >=20 > > > > Right, multiple processes could share the same file-based address= space where those vma have been linked into address_space::i_mmap via vm= _area_struct::shared.rb. Thus, the reader could see its shared.rb linkage= pointers got updated by other processes. > > > >=20 > > > > >=20 > > > > > So the fix is correct, but justificaiton is lacking. > > > > >=20 > > > > > Also, I would like to more fine-grained annotation: marking wit= h > > > > > data_race() 200 bytes copy may hide other issues. > > > >=20 > > > > That is the harder part where I don=E2=80=99t think we have anyth= ing for that today. Macro, any suggestions? ASSERT_IGNORE_FIELD()? > > >=20 > > > There is no nice interface I can think of. All options will just ca= use > > > more problems, inconsistencies, or annoyances. > > >=20 > > > Ideally, to not introduce more types of macros and keep it consiste= nt, > > > ASSERT_EXCLUSIVE_FIELDS_EXCEPT(var, ...) maybe what you're after: > > > "Check no concurrent writers to struct, except ignore the provided > > > fields". > > >=20 > > > This option doesn't quite work, unless you just restrict it to 1 fi= eld > > > (we can't use ranges, because e.g. vm_area_struct has > > > __randomize_layout). The next time around, you'll want 2 fields, an= d > > > it won't work. Also, do we know that 'shared.rb' is the only thing = we > > > want to ignore? > > >=20 > > > If you wanted something similar to ASSERT_EXCLUSIVE_BITS, it'd have= to > > > be ASSERT_EXCLUSIVE_FIELDS(var, ...), however, this is quite annoyi= ng > > > for structs with many fields as you'd have to list all of them. It'= s > > > similar to what you can already do currently (but I also don't > > > recommend because it's unmaintainable): > > >=20 > > > ASSERT_EXCLUSIVE_WRITER(orig->vm_start); > > > ASSERT_EXCLUSIVE_WRITER(orig->vm_end); > > > ASSERT_EXCLUSIVE_WRITER(orig->vm_next); > > > ASSERT_EXCLUSIVE_WRITER(orig->vm_prev); > > > ... and so on ... > > > *new =3D data_race(*orig); > > >=20 > > > Also note that vm_area_struct has __randomize_layout, which makes > > > using ranges impossible. All in all, I don't see a terribly nice > > > option. > > >=20 > > > If, however, you knew that there are 1 or 2 fields only that you wa= nt > > > to make sure are not modified concurrently, ASSERT_EXCLUSIVE_WRITER= + > > > data_race() would probably work well (or even ASSERT_EXCLUSIVE_ACCE= SS > > > if you want to make sure there are no writers nor _readers_). > >=20 > > I am testing an idea that just do, > >=20 > > lockdep_assert_held_write(&orig->vm_mm->mmap_sem); > > *new =3D data_race(*orig); > >=20 > > The idea is that as long as we have the exclusive mmap_sem held in al= l paths > > (auditing indicated so), no writer should be able to mess up our vm_a= rea_struct > > except the "shared.rb" field which has no harm. >=20 > Well, some fields protected by page_table_lock and can be written to > without exclusive mmap_sem. Probably even without any mmap_sem: pin > mm_struct + page_table_lock should be enough. >=20 How about this? diff --git a/kernel/fork.c b/kernel/fork.c index cb2ae49e497e..68f273e0ebf4 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -359,7 +359,10 @@ struct vm_area_struct *vm_area_dup(struct vm_area_st= ruct *orig) =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0struct vm_area_struct *ne= w =3D kmem_cache_alloc(vm_area_cachep, GFP_KERNEL); =C2=A0 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0if (new) { -=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0*new =3D *orig; +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0ASSERT_EXCLUSIVE_WRITER(orig->vm_flags); +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0ASSERT_EXCLUSIVE_WRITER(orig->vm_file); + +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0*new =3D data_race(*orig); =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0INIT_LIST_HEAD(&new->anon_vma_chain); =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0new->vm_next =3D new->vm_prev =3D NULL; =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0}