Message ID | 20230125233554.153109-2-surenb@google.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp542548wrn; Wed, 25 Jan 2023 15:39:26 -0800 (PST) X-Google-Smtp-Source: AMrXdXtYEznD+Qpa8Al8xGjss/H7FSxfoiZXlW8P32aq2a3DFHjgg4kKWmjoU5g4JRv1PQXhLsT1 X-Received: by 2002:a05:6a00:1a15:b0:581:73c4:f0bb with SMTP id g21-20020a056a001a1500b0058173c4f0bbmr39946644pfv.2.1674689966048; Wed, 25 Jan 2023 15:39:26 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674689966; cv=none; d=google.com; s=arc-20160816; b=fDba6evP1n43zupaFOm/kxfZq3UIXuE/3Tqn5sRH+CNHX8vFN7xdYdtZN3YRFEP4kw wSsIVnbDM3wYmAKr8dBv8ZpWCdZPy39b7R7sD5Om73Pi0jQ9uzrXEtDJsAYp0aMh0PGo pVbjXPdnEaPvVNcrrMPt6w7+Re3AQUYEEIo+U1F4n/a7/AmWmdB/YjNpnUBFGmloRrA8 S7dkAmnb6mYeN2U1fVjDb3Ia9wzIilszUZNyk33+KjtTTFXvVmzkw9cS9u5GZKCWxRCh 1kAgr0sgoVZePogA043EYTcUEquj9M1jllgR0ARsY87i0UworICI+vRuvnLTEUAoikmD j88Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=Mu3p5HiJtTPZuGN+bqO+yFOC5Hz9ZieUjpaHjc/iiFw=; b=M7qEZgG53UAK0g4w1HoGkAca5lzdaElXyJQZBdqjJ5frYDaz+8oH6/2MiAcx0ggCPM NriaUH2cWribcJaPiIM9UQPUTJoO2QsJ7IWnz/dLhsJ4P3c9JtCxCmK3ZKfSOm3lCm8G 0+TTl7U2XiXZ73eDadmS2NY3caCNC0/oftL2LuP3lChHMWk/h4gB+myNdiZ+5QTEh6+U 94QlBlt79X9Vd56yFdszWCoT6TDQRVSbVYdWxzFzemXCpU51vpTeTZPgWrYgXNpA26na LCBzqqAq7hq6r0Ujl36DsKejr3KjGIMkUQy4OYOwjodviovXykvFPX7y/A3NMqN5c77/ pLgA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b="O3Dw/3Yf"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id d6-20020a056a0024c600b005907636f4bfsi1425419pfv.48.2023.01.25.15.39.13; Wed, 25 Jan 2023 15:39:26 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b="O3Dw/3Yf"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235925AbjAYXgD (ORCPT <rfc822;lekhanya01809@gmail.com> + 99 others); Wed, 25 Jan 2023 18:36:03 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36842 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235876AbjAYXgC (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Wed, 25 Jan 2023 18:36:02 -0500 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7D4652915D for <linux-kernel@vger.kernel.org>; Wed, 25 Jan 2023 15:36:01 -0800 (PST) Received: by mail-yb1-xb4a.google.com with SMTP id a14-20020a5b0ace000000b007bf99065fcbso132735ybr.2 for <linux-kernel@vger.kernel.org>; Wed, 25 Jan 2023 15:36:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=Mu3p5HiJtTPZuGN+bqO+yFOC5Hz9ZieUjpaHjc/iiFw=; b=O3Dw/3Yf4QCHpXxfNvbShJeQEuFeZuaCO84YhsKbrfT9gihwXHPbAFRN/aHZ1lsYAC qgvLA/2wUiu0psYz4A7EZ32N7vcrezUtVg4gluDiovAd73nSCoLCjue3jqIRvyM8XHNi SclRHbL02JwJ/8QyQNyO8kpVKn5KS/kWq1y8oT8WTssLAAs6ZeUTA2CZ8CZH/ArlHruv /5QEuWLIWZWNh2Nma31HKz5RSg245aOjanktJB6bqUAOqyB+fKcWWKlMYW8vP7U0C0Fl poBIPYC0lSSLPSIW0BCqHBXJQ4Xga5iRXWZxkJmdRhnHZTAHGwylJAWWG4x9W7mFvX74 pJgA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=Mu3p5HiJtTPZuGN+bqO+yFOC5Hz9ZieUjpaHjc/iiFw=; b=qRvlLfTZWhWj/LsFq7+IxAeAhAMnr2ncm5Bql6eeEh3qdn1cxGKfjpEJiaXvaGcsF0 oGCFWcABXyIb9MsmF8Ke0c2NtsRU8ny6WMd0nFK6lYlE6c0THYTNmsY3N4O8nbWUJvjz lQY32vYiyY6HAOP+AhyEvUnqdNiOVTVGvu87tA8kDdSfCTbwfsa6S2bDoeCFMLThHsto Xx48YLrosXCbPC/H7aMJq8yb4T1tjORC1S3/vAX+jpRSCnN5Fkc9RUZFutGaFpf4BEeT l5SgD5cX96Ay5pSiHl1J+96ZaeQoMHNDabwF250IKTpzhkyG3VQd57Knfgm3Y/f/TULr OaXA== X-Gm-Message-State: AFqh2kqPpc/na2Ujor0RqvbBj1vfpUMq42SBKRun2xbDivldZbIcGEl8 nSvAad/EA3CZPoWxLacWF4AkK6MdOiM= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:200:f7b0:20e8:ce66:f98]) (user=surenb job=sendgmr) by 2002:a81:9148:0:b0:4fd:417b:7b8a with SMTP id i69-20020a819148000000b004fd417b7b8amr2939691ywg.404.1674689760727; Wed, 25 Jan 2023 15:36:00 -0800 (PST) Date: Wed, 25 Jan 2023 15:35:48 -0800 In-Reply-To: <20230125233554.153109-1-surenb@google.com> Mime-Version: 1.0 References: <20230125233554.153109-1-surenb@google.com> X-Mailer: git-send-email 2.39.1.456.gfc5497dd1b-goog Message-ID: <20230125233554.153109-2-surenb@google.com> Subject: [PATCH v3 1/7] kernel/fork: convert vma assignment to a memcpy From: Suren Baghdasaryan <surenb@google.com> To: akpm@linux-foundation.org Cc: michel@lespinasse.org, jglisse@google.com, mhocko@suse.com, vbabka@suse.cz, hannes@cmpxchg.org, mgorman@techsingularity.net, dave@stgolabs.net, willy@infradead.org, liam.howlett@oracle.com, peterz@infradead.org, ldufour@linux.ibm.com, paulmck@kernel.org, mingo@redhat.com, will@kernel.org, luto@kernel.org, songliubraving@fb.com, peterx@redhat.com, david@redhat.com, dhowells@redhat.com, hughd@google.com, bigeasy@linutronix.de, kent.overstreet@linux.dev, punit.agrawal@bytedance.com, lstoakes@gmail.com, peterjung1337@gmail.com, rientjes@google.com, axelrasmussen@google.com, joelaf@google.com, minchan@google.com, jannh@google.com, shakeelb@google.com, tatashin@google.com, edumazet@google.com, gthelen@google.com, gurua@google.com, arjunroy@google.com, soheil@google.com, hughlynch@google.com, leewalsh@google.com, posk@google.com, linux-mm@kvack.org, linux-arm-kernel@lists.infradead.org, linuxppc-dev@lists.ozlabs.org, x86@kernel.org, linux-kernel@vger.kernel.org, kernel-team@android.com, surenb@google.com Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1756039705692832189?= X-GMAIL-MSGID: =?utf-8?q?1756039705692832189?= |
Series |
introduce vm_flags modifier functions
|
|
Commit Message
Suren Baghdasaryan
Jan. 25, 2023, 11:35 p.m. UTC
Convert vma assignment in vm_area_dup() to a memcpy() to prevent compiler
errors when we add a const modifier to vma->vm_flags.
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
---
kernel/fork.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
Comments
On Wed, 25 Jan 2023 15:35:48 -0800 Suren Baghdasaryan <surenb@google.com> wrote: > Convert vma assignment in vm_area_dup() to a memcpy() to prevent compiler > errors when we add a const modifier to vma->vm_flags. > > ... > > --- a/kernel/fork.c > +++ b/kernel/fork.c > @@ -482,7 +482,7 @@ struct vm_area_struct *vm_area_dup(struct vm_area_struct *orig) > * orig->shared.rb may be modified concurrently, but the clone > * will be reinitialized. > */ > - *new = data_race(*orig); > + memcpy(new, orig, sizeof(*new)); The data_race() removal is unchangelogged? > INIT_LIST_HEAD(&new->anon_vma_chain); > dup_anon_vma_name(orig, new); > }
On Wed, Jan 25, 2023 at 4:22 PM Andrew Morton <akpm@linux-foundation.org> wrote: > > On Wed, 25 Jan 2023 15:35:48 -0800 Suren Baghdasaryan <surenb@google.com> wrote: > > > Convert vma assignment in vm_area_dup() to a memcpy() to prevent compiler > > errors when we add a const modifier to vma->vm_flags. > > > > ... > > > > --- a/kernel/fork.c > > +++ b/kernel/fork.c > > @@ -482,7 +482,7 @@ struct vm_area_struct *vm_area_dup(struct vm_area_struct *orig) > > * orig->shared.rb may be modified concurrently, but the clone > > * will be reinitialized. > > */ > > - *new = data_race(*orig); > > + memcpy(new, orig, sizeof(*new)); > > The data_race() removal is unchangelogged? True. I'll add a note in the changelog about that. Ideally I would like to preserve it but I could not find a way to do that. > > > INIT_LIST_HEAD(&new->anon_vma_chain); > > dup_anon_vma_name(orig, new); > > } >
On Wed, 25 Jan 2023 16:50:01 -0800 Suren Baghdasaryan <surenb@google.com> wrote: > On Wed, Jan 25, 2023 at 4:22 PM Andrew Morton <akpm@linux-foundation.org> wrote: > > > > On Wed, 25 Jan 2023 15:35:48 -0800 Suren Baghdasaryan <surenb@google.com> wrote: > > > > > Convert vma assignment in vm_area_dup() to a memcpy() to prevent compiler > > > errors when we add a const modifier to vma->vm_flags. > > > > > > ... > > > > > > --- a/kernel/fork.c > > > +++ b/kernel/fork.c > > > @@ -482,7 +482,7 @@ struct vm_area_struct *vm_area_dup(struct vm_area_struct *orig) > > > * orig->shared.rb may be modified concurrently, but the clone > > > * will be reinitialized. > > > */ > > > - *new = data_race(*orig); > > > + memcpy(new, orig, sizeof(*new)); > > > > The data_race() removal is unchangelogged? > > True. I'll add a note in the changelog about that. Ideally I would > like to preserve it but I could not find a way to do that. > Perhaps Paul can comment? I wonder if KCSAN knows how to detect this race, given that it's now in a memcpy. I assume so.
On Wed, Jan 25, 2023 at 05:34:49PM -0800, Andrew Morton wrote: > On Wed, 25 Jan 2023 16:50:01 -0800 Suren Baghdasaryan <surenb@google.com> wrote: > > > On Wed, Jan 25, 2023 at 4:22 PM Andrew Morton <akpm@linux-foundation.org> wrote: > > > > > > On Wed, 25 Jan 2023 15:35:48 -0800 Suren Baghdasaryan <surenb@google.com> wrote: > > > > > > > Convert vma assignment in vm_area_dup() to a memcpy() to prevent compiler > > > > errors when we add a const modifier to vma->vm_flags. > > > > > > > > ... > > > > > > > > --- a/kernel/fork.c > > > > +++ b/kernel/fork.c > > > > @@ -482,7 +482,7 @@ struct vm_area_struct *vm_area_dup(struct vm_area_struct *orig) > > > > * orig->shared.rb may be modified concurrently, but the clone > > > > * will be reinitialized. > > > > */ > > > > - *new = data_race(*orig); > > > > + memcpy(new, orig, sizeof(*new)); > > > > > > The data_race() removal is unchangelogged? > > > > True. I'll add a note in the changelog about that. Ideally I would > > like to preserve it but I could not find a way to do that. > > > > Perhaps Paul can comment? > > I wonder if KCSAN knows how to detect this race, given that it's now in > a memcpy. I assume so. data_race() is just wrapping an expression around __kcsan_[en|dis]able_current and ensuring the expression is evaluated once and returning the correct type. I believe the following should be sufficient. diff --git a/kernel/fork.c b/kernel/fork.c index 9f7fe3541897..1b30ee568e02 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -472,7 +472,7 @@ struct vm_area_struct *vm_area_dup(struct vm_area_struct *orig) * orig->shared.rb may be modified concurrently, but the clone * will be reinitialized. */ - *new = data_race(*orig); + data_race(memcpy(new, orig, sizeof(*new))); INIT_LIST_HEAD(&new->anon_vma_chain); dup_anon_vma_name(orig, new); } I don't see how memcpy could automagically figure out whether the memcpy is prone to races or not in an arbitrary context. Assuming using data_race this way is ok then Acked-by: Mel Gorman <mgorman@techsingularity.net>
On Thu, Jan 26, 2023 at 3:52 AM Mel Gorman <mgorman@techsingularity.net> wrote: > > On Wed, Jan 25, 2023 at 05:34:49PM -0800, Andrew Morton wrote: > > On Wed, 25 Jan 2023 16:50:01 -0800 Suren Baghdasaryan <surenb@google.com> wrote: > > > > > On Wed, Jan 25, 2023 at 4:22 PM Andrew Morton <akpm@linux-foundation.org> wrote: > > > > > > > > On Wed, 25 Jan 2023 15:35:48 -0800 Suren Baghdasaryan <surenb@google.com> wrote: > > > > > > > > > Convert vma assignment in vm_area_dup() to a memcpy() to prevent compiler > > > > > errors when we add a const modifier to vma->vm_flags. > > > > > > > > > > ... > > > > > > > > > > --- a/kernel/fork.c > > > > > +++ b/kernel/fork.c > > > > > @@ -482,7 +482,7 @@ struct vm_area_struct *vm_area_dup(struct vm_area_struct *orig) > > > > > * orig->shared.rb may be modified concurrently, but the clone > > > > > * will be reinitialized. > > > > > */ > > > > > - *new = data_race(*orig); > > > > > + memcpy(new, orig, sizeof(*new)); > > > > > > > > The data_race() removal is unchangelogged? > > > > > > True. I'll add a note in the changelog about that. Ideally I would > > > like to preserve it but I could not find a way to do that. > > > > > > > Perhaps Paul can comment? > > > > I wonder if KCSAN knows how to detect this race, given that it's now in > > a memcpy. I assume so. > > data_race() is just wrapping an expression around > __kcsan_[en|dis]able_current and ensuring the expression is evaluated once > and returning the correct type. I believe the following should be sufficient. Thanks for the suggestion, Mel! I'll try that. > > diff --git a/kernel/fork.c b/kernel/fork.c > index 9f7fe3541897..1b30ee568e02 100644 > --- a/kernel/fork.c > +++ b/kernel/fork.c > @@ -472,7 +472,7 @@ struct vm_area_struct *vm_area_dup(struct vm_area_struct *orig) > * orig->shared.rb may be modified concurrently, but the clone > * will be reinitialized. > */ > - *new = data_race(*orig); > + data_race(memcpy(new, orig, sizeof(*new))); > INIT_LIST_HEAD(&new->anon_vma_chain); > dup_anon_vma_name(orig, new); > } > > I don't see how memcpy could automagically figure out whether the memcpy > is prone to races or not in an arbitrary context. > > Assuming using data_race this way is ok then > > Acked-by: Mel Gorman <mgorman@techsingularity.net> Thanks! > > -- > Mel Gorman > SUSE Labs
On Wed, Jan 25, 2023 at 05:34:49PM -0800, Andrew Morton wrote: > On Wed, 25 Jan 2023 16:50:01 -0800 Suren Baghdasaryan <surenb@google.com> wrote: > > > On Wed, Jan 25, 2023 at 4:22 PM Andrew Morton <akpm@linux-foundation.org> wrote: > > > > > > On Wed, 25 Jan 2023 15:35:48 -0800 Suren Baghdasaryan <surenb@google.com> wrote: > > > > > > > Convert vma assignment in vm_area_dup() to a memcpy() to prevent compiler > > > > errors when we add a const modifier to vma->vm_flags. > > > > > > > > ... > > > > > > > > --- a/kernel/fork.c > > > > +++ b/kernel/fork.c > > > > @@ -482,7 +482,7 @@ struct vm_area_struct *vm_area_dup(struct vm_area_struct *orig) > > > > * orig->shared.rb may be modified concurrently, but the clone > > > > * will be reinitialized. > > > > */ > > > > - *new = data_race(*orig); > > > > + memcpy(new, orig, sizeof(*new)); > > > > > > The data_race() removal is unchangelogged? > > > > True. I'll add a note in the changelog about that. Ideally I would > > like to preserve it but I could not find a way to do that. > > Perhaps Paul can comment? > > I wonder if KCSAN knows how to detect this race, given that it's now in > a memcpy. I assume so. I ran an experiment memcpy()ing between a static array and an onstack array, and KCSAN did not complain. But maybe I was setting it up wrong. This is what I did: long myid = (long)arg; /* different value for each task */ static unsigned long z1[10] = { 0 }; unsigned long z2[10]; ... memcpy(z1, z2, ARRAY_SIZE(z1) * sizeof(z1[0])); for (zi = 0; zi < ARRAY_SIZE(z1); zi++) z2[zi] += myid; memcpy(z2, z1, ARRAY_SIZE(z1) * sizeof(z1[0])); Adding Marco on CC for his thoughts. Thanx, Paul
On Thu, Jan 26, 2023 at 09:27AM -0800, Paul E. McKenney wrote: > On Wed, Jan 25, 2023 at 05:34:49PM -0800, Andrew Morton wrote: > > On Wed, 25 Jan 2023 16:50:01 -0800 Suren Baghdasaryan <surenb@google.com> wrote: > > > > > On Wed, Jan 25, 2023 at 4:22 PM Andrew Morton <akpm@linux-foundation.org> wrote: > > > > > > > > On Wed, 25 Jan 2023 15:35:48 -0800 Suren Baghdasaryan <surenb@google.com> wrote: > > > > > > > > > Convert vma assignment in vm_area_dup() to a memcpy() to prevent compiler > > > > > errors when we add a const modifier to vma->vm_flags. > > > > > > > > > > ... > > > > > > > > > > --- a/kernel/fork.c > > > > > +++ b/kernel/fork.c > > > > > @@ -482,7 +482,7 @@ struct vm_area_struct *vm_area_dup(struct vm_area_struct *orig) > > > > > * orig->shared.rb may be modified concurrently, but the clone > > > > > * will be reinitialized. > > > > > */ > > > > > - *new = data_race(*orig); > > > > > + memcpy(new, orig, sizeof(*new)); > > > > > > > > The data_race() removal is unchangelogged? > > > > > > True. I'll add a note in the changelog about that. Ideally I would > > > like to preserve it but I could not find a way to do that. > > > > Perhaps Paul can comment? > > > > I wonder if KCSAN knows how to detect this race, given that it's now in > > a memcpy. I assume so. > > I ran an experiment memcpy()ing between a static array and an onstack > array, and KCSAN did not complain. But maybe I was setting it up wrong. > > This is what I did: > > long myid = (long)arg; /* different value for each task */ > static unsigned long z1[10] = { 0 }; > unsigned long z2[10]; > > ... > > memcpy(z1, z2, ARRAY_SIZE(z1) * sizeof(z1[0])); > for (zi = 0; zi < ARRAY_SIZE(z1); zi++) > z2[zi] += myid; > memcpy(z2, z1, ARRAY_SIZE(z1) * sizeof(z1[0])); > > Adding Marco on CC for his thoughts. ( Sorry for not seeing it earlier - just saw this by chance. ) memcpy() data races will be detected as of (given a relatively recent Clang compiler): https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=7c201739beef Also beware that the compiler is free to "optimize" things by either inlining memcpy() (turning an explicit memcpy() into just a bunch of loads/stores), or outline plain assignments into memcpy() calls. So the only way to be sure what ends up there is to look at the disassembled code. The data_race() was introduced by: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=cda099b37d716 It says: "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." And as far as I can tell this hasn't changed. Thanks, -- Marco
On Tue, Feb 7, 2023 at 9:16 AM Marco Elver <elver@google.com> wrote: > > On Thu, Jan 26, 2023 at 09:27AM -0800, Paul E. McKenney wrote: > > On Wed, Jan 25, 2023 at 05:34:49PM -0800, Andrew Morton wrote: > > > On Wed, 25 Jan 2023 16:50:01 -0800 Suren Baghdasaryan <surenb@google.com> wrote: > > > > > > > On Wed, Jan 25, 2023 at 4:22 PM Andrew Morton <akpm@linux-foundation.org> wrote: > > > > > > > > > > On Wed, 25 Jan 2023 15:35:48 -0800 Suren Baghdasaryan <surenb@google.com> wrote: > > > > > > > > > > > Convert vma assignment in vm_area_dup() to a memcpy() to prevent compiler > > > > > > errors when we add a const modifier to vma->vm_flags. > > > > > > > > > > > > ... > > > > > > > > > > > > --- a/kernel/fork.c > > > > > > +++ b/kernel/fork.c > > > > > > @@ -482,7 +482,7 @@ struct vm_area_struct *vm_area_dup(struct vm_area_struct *orig) > > > > > > * orig->shared.rb may be modified concurrently, but the clone > > > > > > * will be reinitialized. > > > > > > */ > > > > > > - *new = data_race(*orig); > > > > > > + memcpy(new, orig, sizeof(*new)); > > > > > > > > > > The data_race() removal is unchangelogged? > > > > > > > > True. I'll add a note in the changelog about that. Ideally I would > > > > like to preserve it but I could not find a way to do that. > > > > > > Perhaps Paul can comment? > > > > > > I wonder if KCSAN knows how to detect this race, given that it's now in > > > a memcpy. I assume so. > > > > I ran an experiment memcpy()ing between a static array and an onstack > > array, and KCSAN did not complain. But maybe I was setting it up wrong. > > > > This is what I did: > > > > long myid = (long)arg; /* different value for each task */ > > static unsigned long z1[10] = { 0 }; > > unsigned long z2[10]; > > > > ... > > > > memcpy(z1, z2, ARRAY_SIZE(z1) * sizeof(z1[0])); > > for (zi = 0; zi < ARRAY_SIZE(z1); zi++) > > z2[zi] += myid; > > memcpy(z2, z1, ARRAY_SIZE(z1) * sizeof(z1[0])); > > > > Adding Marco on CC for his thoughts. > > ( Sorry for not seeing it earlier - just saw this by chance. ) > > memcpy() data races will be detected as of (given a relatively recent > Clang compiler): > > https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=7c201739beef > > Also beware that the compiler is free to "optimize" things by either > inlining memcpy() (turning an explicit memcpy() into just a bunch of > loads/stores), or outline plain assignments into memcpy() calls. So the > only way to be sure what ends up there is to look at the disassembled > code. > > The data_race() was introduced by: > > https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=cda099b37d716 > > It says: > "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." > > And as far as I can tell this hasn't changed. Thanks for the feedback, Marco! So, IIUC Mel's proposal to use data_race(memcpy(new, orig, sizeof(*new))); is fine in this case, right? Thanks, Suren. > > Thanks, > -- Marco
On Tue, 7 Feb 2023 at 18:24, Suren Baghdasaryan <surenb@google.com> wrote: > > On Tue, Feb 7, 2023 at 9:16 AM Marco Elver <elver@google.com> wrote: > > > > On Thu, Jan 26, 2023 at 09:27AM -0800, Paul E. McKenney wrote: > > > On Wed, Jan 25, 2023 at 05:34:49PM -0800, Andrew Morton wrote: > > > > On Wed, 25 Jan 2023 16:50:01 -0800 Suren Baghdasaryan <surenb@google.com> wrote: > > > > > > > > > On Wed, Jan 25, 2023 at 4:22 PM Andrew Morton <akpm@linux-foundation.org> wrote: > > > > > > > > > > > > On Wed, 25 Jan 2023 15:35:48 -0800 Suren Baghdasaryan <surenb@google.com> wrote: > > > > > > > > > > > > > Convert vma assignment in vm_area_dup() to a memcpy() to prevent compiler > > > > > > > errors when we add a const modifier to vma->vm_flags. > > > > > > > > > > > > > > ... > > > > > > > > > > > > > > --- a/kernel/fork.c > > > > > > > +++ b/kernel/fork.c > > > > > > > @@ -482,7 +482,7 @@ struct vm_area_struct *vm_area_dup(struct vm_area_struct *orig) > > > > > > > * orig->shared.rb may be modified concurrently, but the clone > > > > > > > * will be reinitialized. > > > > > > > */ > > > > > > > - *new = data_race(*orig); > > > > > > > + memcpy(new, orig, sizeof(*new)); > > > > > > > > > > > > The data_race() removal is unchangelogged? > > > > > > > > > > True. I'll add a note in the changelog about that. Ideally I would > > > > > like to preserve it but I could not find a way to do that. > > > > > > > > Perhaps Paul can comment? > > > > > > > > I wonder if KCSAN knows how to detect this race, given that it's now in > > > > a memcpy. I assume so. > > > > > > I ran an experiment memcpy()ing between a static array and an onstack > > > array, and KCSAN did not complain. But maybe I was setting it up wrong. > > > > > > This is what I did: > > > > > > long myid = (long)arg; /* different value for each task */ > > > static unsigned long z1[10] = { 0 }; > > > unsigned long z2[10]; > > > > > > ... > > > > > > memcpy(z1, z2, ARRAY_SIZE(z1) * sizeof(z1[0])); > > > for (zi = 0; zi < ARRAY_SIZE(z1); zi++) > > > z2[zi] += myid; > > > memcpy(z2, z1, ARRAY_SIZE(z1) * sizeof(z1[0])); > > > > > > Adding Marco on CC for his thoughts. > > > > ( Sorry for not seeing it earlier - just saw this by chance. ) > > > > memcpy() data races will be detected as of (given a relatively recent > > Clang compiler): > > > > https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=7c201739beef > > > > Also beware that the compiler is free to "optimize" things by either > > inlining memcpy() (turning an explicit memcpy() into just a bunch of > > loads/stores), or outline plain assignments into memcpy() calls. So the > > only way to be sure what ends up there is to look at the disassembled > > code. > > > > The data_race() was introduced by: > > > > https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=cda099b37d716 > > > > It says: > > "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." > > > > And as far as I can tell this hasn't changed. > > Thanks for the feedback, Marco! > So, IIUC Mel's proposal to use data_race(memcpy(new, orig, > sizeof(*new))); is fine in this case, right? Yes, that'd work.
diff --git a/kernel/fork.c b/kernel/fork.c index 6683c1b0f460..a531901859d9 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -482,7 +482,7 @@ struct vm_area_struct *vm_area_dup(struct vm_area_struct *orig) * orig->shared.rb may be modified concurrently, but the clone * will be reinitialized. */ - *new = data_race(*orig); + memcpy(new, orig, sizeof(*new)); INIT_LIST_HEAD(&new->anon_vma_chain); dup_anon_vma_name(orig, new); }