Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp5031558ybb; Tue, 24 Mar 2020 09:40:49 -0700 (PDT) X-Google-Smtp-Source: ADFU+vsq44Jhw+qdPareUzdSl8VMnSkFIPomTVt48dAJLYmEUb8drJLxnjmEPJbTWd7cojqHpj0d X-Received: by 2002:aca:52d0:: with SMTP id g199mr4125702oib.59.1585068049827; Tue, 24 Mar 2020 09:40:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1585068049; cv=none; d=google.com; s=arc-20160816; b=yHL1OvwmviXb8Za0WrsJu4t3oJCI06A5PyecwTGA5oRXFW7LYxIVY3d1fMDIwfcyDp QLQ4xHf45puG5d4H6PWx/p6gzd3VQzb56n5l0h2blC0xIYnfkVKQZ6X6LyTHIXYWhoXb 2K/I2YNTct9NuRYsqJicGiflB5xY2H8/dCuryV6+LiKKRKvir6IsMiQTiU0zVr4hfz/0 gARb/PrsA8wLFk+umgH7BnXNlA4QhNC92y4UkmYOo4hoqTq4CpPemwvBuUjqnPOVAYkn JbxRpC/n6IuDHwzO+bZHPgOVppEMlCMjJReFaPK7NGzux8m2WN9pe541ZeJV3BGE2xxU gazQ== 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=ZzkCQpPbdj2OFyiXSSy0aeIz5QRMq8qWJOYRKQeOTcc=; b=mCO1/5ZQeHmM69YqXNew7ajLR6NX9EKUGWvQjNoJ5R34oBYhAYUOpKoyQqfMkcU2pE 9bcS9O9/ed0981y9EEkDzl0hvHVSHBk/HEgOMd7BplT1bqQEY0vE+HbPc4gCgi/cG3mW ZmwObOgpwp7Usu6TcQ/9RjAijVwnCg1QkvXpPJm1hH+Cup6yOS2IY4nbZK1ImO72FkE1 T1wYd8VTX8vyCHUV5WU4fVgVdLApTFsJEarJezkS/XfYwrbq/hor9b/ty3YlWCxbpbKD mUyUGr38RcNYiQGEvH8s/rzneztrBzyEOUA23NeR/6XBSX6LHaUh1FzoIrZrd3GkcpM/ pDFA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=SqvC2kgm; 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 h10si8803960oih.231.2020.03.24.09.40.36; Tue, 24 Mar 2020 09:40:49 -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=SqvC2kgm; 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 S1727341AbgCXQjA (ORCPT + 99 others); Tue, 24 Mar 2020 12:39:00 -0400 Received: from mail-lj1-f196.google.com ([209.85.208.196]:36025 "EHLO mail-lj1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727161AbgCXQi7 (ORCPT ); Tue, 24 Mar 2020 12:38:59 -0400 Received: by mail-lj1-f196.google.com with SMTP id g12so19317470ljj.3 for ; Tue, 24 Mar 2020 09:38:58 -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=ZzkCQpPbdj2OFyiXSSy0aeIz5QRMq8qWJOYRKQeOTcc=; b=SqvC2kgmwRB6Tw4XKDcWiHvMeyI2C3KLquP94TtLNSUWZkmtCtSc82qScnzWqUkjtu GJ/VmxWlx42XrPVn7bvv1addkWv0DUTK/VlBqLttdTB8Ev+Y7bQtN0Gm7mqH89KBS50I zntcKww3mWjZjjU40cMN4+YEC8bzOjJOOr5U0cyL724nHMiBGyxXilBSE9P0iXs1uDDg EmzZHjPWcMIx86jXLdVgZY5v9fo/Lxos9aq8AFFYJqhPb/jr6fCGvpLL6BJe75NDiDag iVS/80LexsuCyT0i3tOMeoID5FSGXKrUONpM5mO/OBdn0IYRzTiegfj+htQoeOnBLp97 +A9Q== 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=ZzkCQpPbdj2OFyiXSSy0aeIz5QRMq8qWJOYRKQeOTcc=; b=bOrYU3tWSEk+waaYAWn8scwLUHu3CWqJdI8g33jupya5y01cXNoLP531oTMprgjan7 vtJ1aVlJTC8yyxs1mHKSuHdTt44pXlstUJiZEu9ld7gZK8kgaV/7/T81YL2MPijsXU6+ +ADLKkhpusQ4LBG499pWjYwCEfVwrw/XVVHX+bFD3PKOKGl2BkySPx6Ym6Sq4Vfx5D3k Tme80VGW53SL5UXfIf8bkC8u3DFz789KaQSYWvfooH4DIlXl/P9pnndPQFh17lEYiYeQ KSToWFWWNVhCfx5HTb6FvJCmZwAkkWCit4JZ0L8piuszH51fgFTrFn+jWYpRRT3FEJOG vE/A== X-Gm-Message-State: ANhLgQ33uqiG25Ppd+aFgfvBzx07cZpL9dGSBrzQbPQrU3fnIF/Soc+N h3a6cdiA38H0fkO7nAYgCHq6FgN6UayNiAxjG9zFTg== X-Received: by 2002:a2e:b5d1:: with SMTP id g17mr4517795ljn.139.1585067937520; Tue, 24 Mar 2020 09:38:57 -0700 (PDT) MIME-Version: 1.0 References: <20200324153643.15527-1-will@kernel.org> <20200324153643.15527-4-will@kernel.org> <20200324162652.GA2518046@kroah.com> In-Reply-To: <20200324162652.GA2518046@kroah.com> From: Jann Horn Date: Tue, 24 Mar 2020 17:38:30 +0100 Message-ID: Subject: Re: [RFC PATCH 03/21] list: Annotate lockless list primitives with data_race() To: Greg KH Cc: Will Deacon , kernel list , Eric Dumazet , Kees Cook , Maddie Stone , Marco Elver , "Paul E . McKenney" , Peter Zijlstra , Thomas Gleixner , kernel-team , Kernel Hardening 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, Mar 24, 2020 at 5:26 PM Greg KH wrote: > On Tue, Mar 24, 2020 at 05:20:45PM +0100, Jann Horn wrote: > > On Tue, Mar 24, 2020 at 4:37 PM Will Deacon wrote: > > > Some list predicates can be used locklessly even with the non-RCU list > > > implementations, since they effectively boil down to a test against > > > NULL. For example, checking whether or not a list is empty is safe even > > > in the presence of a concurrent, tearing write to the list head pointer. > > > Similarly, checking whether or not an hlist node has been hashed is safe > > > as well. > > > > > > Annotate these lockless list predicates with data_race() and READ_ONCE() > > > so that KCSAN and the compiler are aware of what's going on. The writer > > > side can then avoid having to use WRITE_ONCE() in the non-RCU > > > implementation. > > [...] > > > static inline int list_empty(const struct list_head *head) > > > { > > > - return READ_ONCE(head->next) == head; > > > + return data_race(READ_ONCE(head->next) == head); > > > } > > [...] > > > static inline int hlist_unhashed(const struct hlist_node *h) > > > { > > > - return !READ_ONCE(h->pprev); > > > + return data_race(!READ_ONCE(h->pprev)); > > > } > > > > This is probably valid in practice for hlist_unhashed(), which > > compares with NULL, as long as the most significant byte of all kernel > > pointers is non-zero; but I think list_empty() could realistically > > return false positives in the presence of a concurrent tearing store? > > This could break the following code pattern: > > > > /* optimistic lockless check */ > > if (!list_empty(&some_list)) { > > /* slowpath */ > > mutex_lock(&some_mutex); > > list_for_each(tmp, &some_list) { > > ... > > } > > mutex_unlock(&some_mutex); > > } > > > > (I'm not sure whether patterns like this appear commonly though.) > > > I would hope not as the list could go "empty" before the lock is > grabbed. That pattern would be wrong. If the list becomes empty in between, the loop just iterates over nothing, and the effect is no different from what you'd get if you had bailed out before. But sure, you have to be aware that that can happen.