Received: by 2002:a25:31c3:0:0:0:0:0 with SMTP id x186csp980353ybx; Wed, 6 Nov 2019 11:17:32 -0800 (PST) X-Google-Smtp-Source: APXvYqz4NOjqT3SZNu473f3yTcFYXMUvFV2UE+1W3Q7ze/HjypRLhk3qEMvmcCfLpEiQyPvPM64t X-Received: by 2002:a50:eb49:: with SMTP id z9mr4561786edp.31.1573067852625; Wed, 06 Nov 2019 11:17:32 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1573067852; cv=none; d=google.com; s=arc-20160816; b=RR5ERusyhq8y8jexjjeRQ6yJhfsza+jpXiJ/vT8g/AiWH5i7yJVvSOt8xW/5I86Ssg OYUvmL0IZOaFhrnIJDQhb8DMJ9AErzL5U9q+egu8OBAUNPTIK0T6kb+iy7ezkZys0Wtl T4qJffha5dCL8vQyIC77sxcEyW0xW3dkoJ0w/NCkcyGqNKCPjmjIBrDQtC1QYnvZd5y/ z1tb9ZYS9rHchk7Pt0NAKgMqVieA/oWndlhoTlXPIl+sZYzcrtw21zUIakgjqSxsgTHb TZYM0XytNT90QAK/dzjOS5IP9R/CFc6606viJHUVdS0TgwWsDEUsGaHe5paW7d+XZWYR Fgeg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :message-id:in-reply-to:subject:cc:to:from:date; bh=Yi18O/J+O2XjKmXAOA+Ysomsr5fXgQdHF5epTcnip7Y=; b=plFpp0mYA+lgGjEPuqevdr76XyQQP3gzhA+L6gA5T7kcWCHAh5tvDUUMBkFZf25sdo 6g1Feat2OXy17j4a9j8pbh4vZlqjQqh9QhJv4/AxqvxjXOMsVU9ApPUx9bOocKppYkLp TnWdly9Nn0ogS/1SW9ZR5qlVejR+Upi6H6ch4i3iXAGEI2uuLuMz/ETGHL9VvOHeq5Cw x0h5xlyHV3gKRDeuVtjdhH7Tp7WV5cyg/S8SRrHaueD+p8B65KToaOfPxp3tZMvGHBAu IYoSl50m80shaqZtqzHmlE2QA45p5fOMf+JMg+Na0rxamajmnN2wuWxoMC8Ty3XJDEA1 /DaA== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id h19si7884198ejx.34.2019.11.06.11.17.09; Wed, 06 Nov 2019 11:17:32 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731977AbfKFTP3 (ORCPT + 99 others); Wed, 6 Nov 2019 14:15:29 -0500 Received: from Galois.linutronix.de ([193.142.43.55]:45099 "EHLO Galois.linutronix.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727208AbfKFTP3 (ORCPT ); Wed, 6 Nov 2019 14:15:29 -0500 Received: from p5b06da22.dip0.t-ipconnect.de ([91.6.218.34] helo=nanos) by Galois.linutronix.de with esmtpsa (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1iSQm6-0001pl-Sj; Wed, 06 Nov 2019 20:15:27 +0100 Date: Wed, 6 Nov 2019 20:15:25 +0100 (CET) From: Thomas Gleixner To: Eric Dumazet cc: linux-kernel , Eric Dumazet , syzbot Subject: Re: [PATCH] hrtimer: Annotate lockless access to timer->state In-Reply-To: Message-ID: References: <20191106174804.74723-1-edumazet@google.com> User-Agent: Alpine 2.21 (DEB 202 2017-01-01) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII X-Linutronix-Spam-Score: -1.0 X-Linutronix-Spam-Level: - X-Linutronix-Spam-Status: No , -1.0 points, 5.0 required, ALL_TRUSTED=-1,SHORTCIRCUIT=-0.0001 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, 6 Nov 2019, Eric Dumazet wrote: > On Wed, Nov 6, 2019 at 10:09 AM Thomas Gleixner wrote: > > > > On Wed, 6 Nov 2019, Eric Dumazet wrote: > > > @@ -1013,8 +1013,9 @@ static void __remove_hrtimer(struct hrtimer *timer, > > > static inline int > > > remove_hrtimer(struct hrtimer *timer, struct hrtimer_clock_base *base, bool restart) > > > { > > > - if (hrtimer_is_queued(timer)) { > > > - u8 state = timer->state; > > > + u8 state = timer->state; > > > > Shouldn't that be a read once then at least for consistency sake? > > We own the lock here, this is not really needed ? > > Note they are other timer->state reads I chose to leave unchanged. > > But no big deal if you prefer I can add a READ_ONCE() Nah. I can add it myself if at all. I know that the READ_ONCE() is not required there, but I really hate to twist my brain when staring at code why some places use it and some not. So at least some commentry helps to avoid that. Something like the below. If you have no objections I just queue the patch with this folded in. Thanks, tglx 8<------------- --- a/include/linux/hrtimer.h +++ b/include/linux/hrtimer.h @@ -456,12 +456,18 @@ extern u64 hrtimer_next_event_without(co extern bool hrtimer_active(const struct hrtimer *timer); -/* - * Helper function to check, whether the timer is on one of the queues +/** + * hrtimer_is_queued = check, whether the timer is on one of the queues + * @timer: Timer to check + * + * Returns: True if the timer is queued, false otherwise + * + * The function can be used lockless, but it gives only a momentary snapshot. */ -static inline int hrtimer_is_queued(struct hrtimer *timer) +static inline bool hrtimer_is_queued(struct hrtimer *timer) { - return READ_ONCE(timer->state) & HRTIMER_STATE_ENQUEUED; + /* The READ_ONCE pairs with the update functions of timer->state */ + return !!READ_ONCE(timer->state) & HRTIMER_STATE_ENQUEUED; } /* --- a/kernel/time/hrtimer.c +++ b/kernel/time/hrtimer.c @@ -966,6 +966,7 @@ static int enqueue_hrtimer(struct hrtime base->cpu_base->active_bases |= 1 << base->index; + /* Pairs with the lockless read in hrtimer_is_queued() */ WRITE_ONCE(timer->state, HRTIMER_STATE_ENQUEUED); return timerqueue_add(&base->active, &timer->node); @@ -988,6 +989,7 @@ static void __remove_hrtimer(struct hrti struct hrtimer_cpu_base *cpu_base = base->cpu_base; u8 state = timer->state; + /* Pairs with the lockless read in hrtimer_is_queued() */ WRITE_ONCE(timer->state, newstate); if (!(state & HRTIMER_STATE_ENQUEUED)) return;