Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp930410ybl; Fri, 31 Jan 2020 10:33:53 -0800 (PST) X-Google-Smtp-Source: APXvYqwIUWQijUg9bluInTqdeppLcG2+aJWyvAIkqueeTv+ayhGZg+Eu0tR0Uvb0qh4BFpuemdE3 X-Received: by 2002:aca:be57:: with SMTP id o84mr7300829oif.138.1580495633315; Fri, 31 Jan 2020 10:33:53 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1580495633; cv=none; d=google.com; s=arc-20160816; b=BVHMS/aVyNzaEzL1tPuXur7dA1lnLmSH6KQ1kH71MlJLAnConClyKgdjYysm2KO+qU Iw0pPMFzEhcD+a3w1xFq1iWno7Oyc15zPyMOc8IkFgKDLTIP68s0s9oy+wClV2IoEoqj vJ61cyQHSYwioYPtgHCNuKNk5bI9iwmOI+RtUo/bDcFSFobTfRP2yBoFwRy3RIpjiWFt lGhC//riHAHtE2NgROIVkmBIQgytmTzW5mMlL3pjc3GmF5wOpa6fPeCgJXtbj/bVNBva q3lK1k6HQHivGRW8ayCt4+5UE4JwBsLECQ4atKXlQn4CD553U+LAkMsZWeNG9sGnv2V+ ROXg== 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:message-id:subject:cc :to:from:date:dkim-signature; bh=u4qq+g/DO8Gz9ix2J+r2fE6vVlq1YIPwVCUNlGd86FI=; b=Y03xk4GXoKrNgTvV1Ra2DO0qOACSHaEy+fdFq7qFWTMACHi7i+P/xKPOWJcO+axUKl Z+J/DaxkiwPzH8bbwBi5BepSGDxmQ5GZaRAwTBiQgB6ETKP3OFAjqbKZLbSXQkBvrwlV m0xqeozmDpLhsKymeccB4Np6/RIcH09B0UFdO3mnziLm/7ff4HF5Bd2n8OZswgElAHp8 YLzKlBOj0iGneU5pu/Mcti8/pLFLd/LPSxlADZ+1m3uR9AmkeUqV2YQg8UKs9eURECGz S7mty0yT6sbhE4FuOS2iOXkZ/n7R8HuFGmJIuNVoNRUs9ZhZ/fpH4M8pdAx64UEwnBp4 1rNA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=JsGaYS3M; 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 u191si4647408oia.86.2020.01.31.10.33.40; Fri, 31 Jan 2020 10:33:53 -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=JsGaYS3M; 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 S1726105AbgAaScn (ORCPT + 99 others); Fri, 31 Jan 2020 13:32:43 -0500 Received: from mail.kernel.org ([198.145.29.99]:41068 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725907AbgAaScm (ORCPT ); Fri, 31 Jan 2020 13:32:42 -0500 Received: from willie-the-truck (236.31.169.217.in-addr.arpa [217.169.31.236]) (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 748AF206F0; Fri, 31 Jan 2020 18:32:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1580495561; bh=aSo9PawRW0VeIAlFXUcKr/vu2eg7cH600lHWwdQZ2lI=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=JsGaYS3MSHO6IuCIbex/e9imcWyDfmuFfV8T5mtCsLP0UqaDJSsK2xF6CmQ3AJ6eK sRxSbZrdYGzJWJMfpccThuoGk4WAv08QsqveX/DlMjJ0tJxaP0QMnsqL6GkVhSjZNb QKBbSNOLUU1Rnd2PDXFIHVjwFoKI8YFhLdi4orSE= Date: Fri, 31 Jan 2020 18:32:37 +0000 From: Will Deacon To: Eric Dumazet Cc: Thomas Gleixner , "Paul E. McKenney" , the arch/x86 maintainers , LKML , Peter Zijlstra Subject: Re: Confused about hlist_unhashed_lockless() Message-ID: <20200131183236.GA6159@willie-the-truck> References: <20200131164308.GA5175@willie-the-truck> <20200131165718.GA5517@willie-the-truck> <20200131172058.GB5517@willie-the-truck> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.10.1 (2018-07-13) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Jan 31, 2020 at 09:33:55AM -0800, Eric Dumazet wrote: > On Fri, Jan 31, 2020 at 9:21 AM Will Deacon wrote: > > On Fri, Jan 31, 2020 at 09:06:27AM -0800, Eric Dumazet wrote: > > > Sorry, but timer_pending() requires no serialization. > > > > Then we should update the comment! > > Which one ? > > It seems KCSAN does not read the comments :) Haha, that would be nice wouldn't it? > > Without serialisation, timer_pending() as currently implemented does > > not reliably tell you whether the timer is in the hlist. Is that not a > > problem? > > No it is not a problem. > > However some callers might have incorrect assumptions, I have not > audited all the code. Of course not, I'm just interested in what correct callers are allowed to assume. > Using an RCU hlist does not introduce serialisation, but does > > at least rule out the case where timer_pending() returns false for a > > timer that /is/ reachable in the list by another CPU. > > > > > The only thing we need is a READ_ONCE() so that compiler is not allowed > > > to optimize out stuff like > > > > > > loop() { > > > if (timer_pending()) > > > something; > > > > If that was the case, then you wouldn't need to touch hlist_add_before() > > at all so there's got to be more to it than that or we can revert that > > part of the patch. > > Sorry, I do not get your point. It would help if you provide a patch > or something. Ok, diff below. If I've understood you correctly (big if ;), then I think it's sufficient. The idea is that you have to explicitly annotate your code as a data race if you're trying to use the non-RCU list implementation in a lock-free fashion. Will --->8 diff --git a/include/linux/list.h b/include/linux/list.h index 884216db3246..74c78017332e 100644 --- a/include/linux/list.h +++ b/include/linux/list.h @@ -810,7 +810,7 @@ static inline void __hlist_del(struct hlist_node *n) WRITE_ONCE(*pprev, next); if (next) - WRITE_ONCE(next->pprev, pprev); + next->pprev = pprev; } /** @@ -852,11 +852,11 @@ static inline void hlist_del_init(struct hlist_node *n) static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h) { struct hlist_node *first = h->first; - WRITE_ONCE(n->next, first); + n->next = first; if (first) - WRITE_ONCE(first->pprev, &n->next); + first->pprev = &n->next; WRITE_ONCE(h->first, n); - WRITE_ONCE(n->pprev, &h->first); + n->pprev = &h->first; } /** @@ -867,9 +867,9 @@ static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h) static inline void hlist_add_before(struct hlist_node *n, struct hlist_node *next) { - WRITE_ONCE(n->pprev, next->pprev); - WRITE_ONCE(n->next, next); - WRITE_ONCE(next->pprev, &n->next); + n->pprev = next->pprev; + n->next = next; + next->pprev = &n->next; WRITE_ONCE(*(n->pprev), n); } @@ -881,12 +881,12 @@ static inline void hlist_add_before(struct hlist_node *n, static inline void hlist_add_behind(struct hlist_node *n, struct hlist_node *prev) { - WRITE_ONCE(n->next, prev->next); - WRITE_ONCE(prev->next, n); - WRITE_ONCE(n->pprev, &prev->next); + n->next = prev->next; + prev->next = n; + n->pprev = &prev->next; if (n->next) - WRITE_ONCE(n->next->pprev, &n->next); + n->next->pprev = &n->next; } /** diff --git a/include/linux/timer.h b/include/linux/timer.h index 1e6650ed066d..53c3b3bc00f0 100644 --- a/include/linux/timer.h +++ b/include/linux/timer.h @@ -158,13 +158,14 @@ static inline void destroy_timer_on_stack(struct timer_list *timer) { } * * timer_pending will tell whether a given timer is currently pending, * or not. Callers must ensure serialization wrt. other operations done - * to this timer, eg. interrupt contexts, or other CPUs on SMP. + * to this timer, eg. interrupt contexts, or other CPUs on SMP if they + * cannot tolerate spurious results. * * return value: 1 if the timer is pending, 0 if not. */ static inline int timer_pending(const struct timer_list * timer) { - return timer->entry.pprev != NULL; + return !data_race(hlist_unhashed_lockless(&timer->entry)); } extern void add_timer_on(struct timer_list *timer, int cpu);