Received: by 2002:a25:e7d8:0:0:0:0:0 with SMTP id e207csp725827ybh; Tue, 10 Mar 2020 07:11:20 -0700 (PDT) X-Google-Smtp-Source: ADFU+vtAgSQfFjJ9pJQH+1qjQvMnjRrO8FXypEMCcH/wBZAwCUHYVFsvi7WTGBO7hOzle205Umme X-Received: by 2002:a9d:21b4:: with SMTP id s49mr16420293otb.294.1583849480838; Tue, 10 Mar 2020 07:11:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1583849480; cv=none; d=google.com; s=arc-20160816; b=QkUjJBzDGts+MqL/03/2pCpigTlBRfSnK/lrhS5KFMkZeUQT/+Fcu8E0qFckns/V5B HbNSCTUIzado7IoDJAHkpE83X5g87MHPsF49QruUqR6tJv9orUoZg5ZOpO8gVolDHaj6 qpwRR95t0gq8wMIh0X0OXjhgGe9miau1hV/nW19aHliiS9Oq5RKj4eDyO3v6SidfcWMU Cmo5yVp8Dby75V1bvawhLWogfj6HfbLoMybNirvof3EE8ihvf+VhL6yz0lpXq9NEPyMP GuAdaiE7DjWWjZR+FzDQFBFJP3bHHzETlvUdmy/92IuN4FocPRAIZ6zdylxVp988h4Fo lIwQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=UH/kbp/wpXOAAE3tRNnXeOhnV8pFJCL150z+9pntZ4A=; b=YaELiMlRCsWHjq/4lvpz2fkwXIMwglqoOYI1bvPE+10CWVWGeFF29a4vnEeSyrgxRE wMW54fSK6nCRU7CMEfGlSPM0kln4DT8M1aJ7j/aC9IBk52pmLBF7qptBTv0rsu88+BJX MXuLdB/F57L56LsFhR0fQNV8D86yjeND3KEqucn7tIY/tZqNhD9sB0AJR2OmzfeMBb87 vDwi1bf1FSYiknNiPZ9iJg/qAZ7vvCsv8WdcLXDBCqaBALg5S81ejc2M+nHSTlElhyB4 HrWUxRloeMHXcAOhJu2rGuIjEywr6ksmjFMCPHd4P2m1mtrbfR1cgHSKKha6uPKXxaeB dUHQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b="RWOmkRG/"; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id l19si5302331oii.54.2020.03.10.07.10.57; Tue, 10 Mar 2020 07:11:20 -0700 (PDT) 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=@google.com header.s=20161025 header.b="RWOmkRG/"; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726766AbgCJOKI (ORCPT + 99 others); Tue, 10 Mar 2020 10:10:08 -0400 Received: from mail-ot1-f65.google.com ([209.85.210.65]:42393 "EHLO mail-ot1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726426AbgCJOKH (ORCPT ); Tue, 10 Mar 2020 10:10:07 -0400 Received: by mail-ot1-f65.google.com with SMTP id 66so13216897otd.9 for ; Tue, 10 Mar 2020 07:10:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=UH/kbp/wpXOAAE3tRNnXeOhnV8pFJCL150z+9pntZ4A=; b=RWOmkRG/j7y49Wh3SB9PdFhimY5gdbbMOjfJjQXm6WmxbtGRhWUGE2xUdZ7PzL4/Y/ Fpvm/Of6YjafgVW2nYRKdQxF+l0LSXglzjiU8dCzWmTqyOm/9m5a04/5C3CTbomPV+Wx BBf/GYHEx8y8vA0rDW3muZe3lQoMsG18tsNePYHtL7TDDTpKvQi5TzkvH3txUaUX7kfP 2KUzZjvsiJ3MWXs49B++XTqiVQaLOz2Z44p2Yzy4G/nxGS8NZ52+OtdnD9qHt90rTzJ5 fI7NhweRPwmp3LbgL1FaXgg0jWQqVtfHFWmrLzAxSRCbsZG2BT4OfAzKLfOe8C9Z8uc/ z5CA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=UH/kbp/wpXOAAE3tRNnXeOhnV8pFJCL150z+9pntZ4A=; b=mGXRBy40aHcfwpQnXniBFU8r5N+/C5I8PKBd5on8FkmDe7gxygbRfJ1BaNRxdxsgLB KheXAGS5/IlgggZjAUbIv3w7w03gXLAKjUCFWAkKBbGEDJxMU65t68g7j4olBKhk9ers Y9vASLU+drwq1jjxhBucwq1iGnkatp0uEEAq4g1VtQQva5j9kwLfOwy9hxdyV9/E9gvU KhSFNtkQ/chO3bjQcAgiQbdAet99Ob6DDXlKcPB/nmmsViT2qaTz3e9kV61l8rsPaSmB uyd4c85c6D0X3vdLnwSSl3YkIbTEgtv/0WMJI/eCLGOA6nQnbwmo4wHVwIX+ZMM2pHhm ujLg== X-Gm-Message-State: ANhLgQ1Cg82V0OpeShU8Desn8EFeNYlgLjYFAMW5yBs7ubt0nH8CBbhr ubGTq6ka/8Uy4hTxWyHEHhMsbjsk2d90/OCSLRIsqA== X-Received: by 2002:a05:6830:1213:: with SMTP id r19mr9393949otp.17.1583849406243; Tue, 10 Mar 2020 07:10:06 -0700 (PDT) MIME-Version: 1.0 References: <20200310092119.14965-1-chris@chris-wilson.co.uk> <2e936d8fd2c445beb08e6dd3ee1f3891@AcuMS.aculab.com> <158384100886.16414.15741589015363013386@build.alporthouse.com> <723d527a4ad349b78bf11d52eba97c0e@AcuMS.aculab.com> <20200310125031.GY2935@paulmck-ThinkPad-P72> In-Reply-To: <20200310125031.GY2935@paulmck-ThinkPad-P72> From: Marco Elver Date: Tue, 10 Mar 2020 15:09:54 +0100 Message-ID: Subject: Re: [PATCH] list: Prevent compiler reloads inside 'safe' list iteration To: "Paul E. McKenney" Cc: David Laight , Chris Wilson , "linux-kernel@vger.kernel.org" , "intel-gfx@lists.freedesktop.org" , Andrew Morton , Randy Dunlap , "stable@vger.kernel.org" Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, 10 Mar 2020 at 13:50, Paul E. McKenney wrote: > > On Tue, Mar 10, 2020 at 12:23:34PM +0000, David Laight wrote: > > From: Chris Wilson > > > Sent: 10 March 2020 11:50 > > > > > > Quoting David Laight (2020-03-10 11:36:41) > > > > From: Chris Wilson > > > > > Sent: 10 March 2020 09:21 > > > > > Instruct the compiler to read the next element in the list iteration > > > > > once, and that it is not allowed to reload the value from the stale > > > > > element later. This is important as during the course of the safe > > > > > iteration, the stale element may be poisoned (unbeknownst to the > > > > > compiler). > > > > > > > > Eh? > > > > I thought any function call will stop the compiler being allowed > > > > to reload the value. > > > > The 'safe' loop iterators are only 'safe' against called > > > > code removing the current item from the list. > > > > > > > > > This helps prevent kcsan warnings over 'unsafe' conduct in releasing the > > > > > list elements during list_for_each_entry_safe() and friends. > > > > > > > > Sounds like kcsan is buggy ???? > > Adding Marco on CC for his thoughts. I'd have to see a stack-trace with line-numbers. But keep in mind what KCSAN does, which is report "data races". If the KCSAN report showed 2 accesses, where one of them was a *plain* read (and the other a write), then it's a valid data race (per LKMM's definition). It seems this was the case here. As mentioned, the compiler is free to transform plain accesses in various concurrency-unfriendly ways. FWIW, for writes we're already being quite generous, in that plain aligned writes up to word-size are assumed to be "atomic" with the default (conservative) config, i.e. marking such writes is optional. Although, that's a generous assumption that is not always guaranteed to hold (https://lore.kernel.org/lkml/20190821103200.kpufwtviqhpbuv2n@willie-the-truck/). If there is code for which you prefer not to see KCSAN reports at all, you are free to disable them with KCSAN_SANITIZE_file.o := n Thanks, -- Marco > > > The warning kcsan gave made sense (a strange case where the emptying the > > > list from inside the safe iterator would allow that list to be taken > > > under a global mutex and have one extra request added to it. The > > > list_for_each_entry_safe() should be ok in this scenario, so long as the > > > next element is read before this element is dropped, and the compiler is > > > instructed not to reload the element. > > > > Normally the loop iteration code has to hold the mutex. > > I guess it can be released inside the loop provided no other > > code can ever delete entries. > > > > > kcsan is a little more insistent on having that annotation :) > > > > > > In this instance I would say it was a false positive from kcsan, but I > > > can see why it would complain and suspect that given a sufficiently > > > aggressive compiler, we may be caught out by a late reload of the next > > > element. > > > > If you have: > > for (; p; p = next) { > > next = p->next; > > external_function_call(void); > > } > > the compiler must assume that the function call > > can change 'p->next' and read it before the call. > > That "must assume" is a statement of current compiler technology. > Given the progress over the past forty years, I would not expect this > restriction to hold forever. Yes, we can and probably will get the > compiler implementers to give us command-line flags to suppress global > analysis. But given the progress in compilers that I have seen over > the past 4+ decades, I would expect that the day will come when we won't > want to be using those command-line flags. > > But if you want to ignore KCSAN's warnings, you are free to do so. > > > Is this a list with strange locking rules? > > The only deletes are from within the loop. > > Adds and deletes are locked. > > The list traversal isn't locked. > > > > I suspect kcsan bleats because it doesn't assume the compiler > > will use a single instruction/memory operation to read p->next. > > That is just stupid. > > Heh! If I am still around, I will ask you for your evaluation of the > above statement in 40 years. Actually, 10 years will likely suffice. ;-) > > Thanx, Paul