Received: by 2002:a25:8b12:0:0:0:0:0 with SMTP id i18csp1210753ybl; Fri, 16 Aug 2019 10:43:16 -0700 (PDT) X-Google-Smtp-Source: APXvYqxcwUbqjDStWTAiyu07sKkKNKZVxyYyefLUulqREzZ9Hm4H1l7Z7hTad9TUMa3qMrTzTJhA X-Received: by 2002:a65:621a:: with SMTP id d26mr8823716pgv.153.1565977396249; Fri, 16 Aug 2019 10:43:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1565977396; cv=none; d=google.com; s=arc-20160816; b=jNGlwUAt1/IEAXdCsd7oZ90LMH8bb8S/gS7McHl6gYRoxo9lmYyZSKcvXLzmUomvbK Er9Wm8ah0+pwRwxiIxA8HNlaQwzZw4igVtJ5dGoHgkSWlpcEDF2zeHn8zP4m+JBqRFJS YtI/opO0M6PyFQOVAwGz4XdHEhS9H4fj9UJBvWn0ffWi0EPmXVogBI8uAw4KehEXvfT7 gkmjyHHGkH22NbyLZo/Mexo/ysdzWgh4Msd0BjiYfMYFbES6pOaOxx0J1KT6ftq4wiuR rsXJj8ClFrfVWnIsorDwTyi3ffnYFEW8gqDysFwSrMx/G1w0s/ILgiZW3sXV4gFPeIFg DBhw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:thread-index:thread-topic :content-transfer-encoding:mime-version:subject:references :in-reply-to:message-id:cc:to:from:date:dkim-signature:dkim-filter; bh=j/iWpJC7S2VTPE4TQ/7wBReGyb0P4Sv1rymnA4u2WTs=; b=suhmqPSpRGi9pUi0yiRs+ojJcCTY58+8Jv92FvahH7Q1KuIlQDN6caTv61cTDJQjvT J8xPbhOpqzT4Dr7g90tMEeLc325P67VJ0TJYmaYXtJt3uBixkBCvGAU1OTzkpu++al4M 6DeoA2DbbnHRxZEFvoBAy+TzEmQ7fbNix6lIDBKW6nWG2dkm2xDONV7JbYp1pAGwHDcO qvwYTX0X/y70OrOgyKNRQ9iFLHeWsE/0wanwxnp5mLKpaAyYOy5y8Y1TwilN2ZI3v2y6 qrv1PWLkgB6jKSeX6S3XQuFj59Ig2SolBk91T9x4WIgmDZx1fvNo5Yj5Ez7RicwBcWIE LPHQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@efficios.com header.s=default header.b=m1Np0DLy; 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=efficios.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id a186si4149416pge.365.2019.08.16.10.43.00; Fri, 16 Aug 2019 10:43:16 -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=@efficios.com header.s=default header.b=m1Np0DLy; 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=efficios.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727513AbfHPRmD (ORCPT + 99 others); Fri, 16 Aug 2019 13:42:03 -0400 Received: from mail.efficios.com ([167.114.142.138]:52614 "EHLO mail.efficios.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727067AbfHPRmD (ORCPT ); Fri, 16 Aug 2019 13:42:03 -0400 Received: from localhost (ip6-localhost [IPv6:::1]) by mail.efficios.com (Postfix) with ESMTP id B21A22C7427; Fri, 16 Aug 2019 13:42:00 -0400 (EDT) Received: from mail.efficios.com ([IPv6:::1]) by localhost (mail02.efficios.com [IPv6:::1]) (amavisd-new, port 10032) with ESMTP id Kdg3i7hBaWqV; Fri, 16 Aug 2019 13:42:00 -0400 (EDT) Received: from localhost (ip6-localhost [IPv6:::1]) by mail.efficios.com (Postfix) with ESMTP id 235012C741F; Fri, 16 Aug 2019 13:42:00 -0400 (EDT) DKIM-Filter: OpenDKIM Filter v2.10.3 mail.efficios.com 235012C741F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=efficios.com; s=default; t=1565977320; bh=j/iWpJC7S2VTPE4TQ/7wBReGyb0P4Sv1rymnA4u2WTs=; h=Date:From:To:Message-ID:MIME-Version; b=m1Np0DLyMdwFFVexRh7mu3VN3GrvXx/9SfeyH1JzeVm+G57bXooAFfIS43FN58inQ 2crWB3W60dPjlmznACIlDzaOjQlKIMEG7be2v8yepgdkmBY7wB0vsiIM13DVskK63d swUaLc2qUGu0lEQ5n4jgyNOUFzMR+GBhCXwAuzuM6yoBfJhusQBai+43JfpovM0QUz fHWo27uydZwQVxAIcajRH+hCO0ycJkpVlM/duVDtnX0sSgv3slKwz8FQP7jef0Vcyf BHU1JSpvUbguUqXNKxfQNzYPgdwt9TbpHhN8GQ20Jo6CRP7ItGyoFklgE18zAL9Qux m8BUHQC7Wp/7w== X-Virus-Scanned: amavisd-new at efficios.com Received: from mail.efficios.com ([IPv6:::1]) by localhost (mail02.efficios.com [IPv6:::1]) (amavisd-new, port 10026) with ESMTP id dKBcm3YeFfeT; Fri, 16 Aug 2019 13:42:00 -0400 (EDT) Received: from mail02.efficios.com (mail02.efficios.com [167.114.142.138]) by mail.efficios.com (Postfix) with ESMTP id 088382C740F; Fri, 16 Aug 2019 13:42:00 -0400 (EDT) Date: Fri, 16 Aug 2019 13:41:59 -0400 (EDT) From: Mathieu Desnoyers To: rostedt Cc: Valentin Schneider , linux-kernel , "Joel Fernandes, Google" , Peter Zijlstra , Thomas Gleixner , paulmck , Boqun Feng , Will Deacon , David Howells , Alan Stern , Linus Torvalds Message-ID: <241506096.21688.1565977319832.JavaMail.zimbra@efficios.com> In-Reply-To: <20190816130440.07cc0a30@oasis.local.home> References: <00000000000076ecf3059030d3f1@google.com> <20190816142643.13758-1-mathieu.desnoyers@efficios.com> <20190816122539.34fada7b@oasis.local.home> <28afb801-6b76-f86b-9e1b-09488fb7c8ce@arm.com> <20190816130440.07cc0a30@oasis.local.home> Subject: Re: [PATCH 1/1] Fix: trace sched switch start/stop racy updates MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-Originating-IP: [167.114.142.138] X-Mailer: Zimbra 8.8.15_GA_3829 (ZimbraWebClient - FF68 (Linux)/8.8.15_GA_3829) Thread-Topic: trace sched switch start/stop racy updates Thread-Index: tCTIhLRfwsNJzC/XdZY3OMmGz0773Q== Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org ----- On Aug 16, 2019, at 1:04 PM, rostedt rostedt@goodmis.org wrote: > On Fri, 16 Aug 2019 17:48:59 +0100 > Valentin Schneider wrote: > >> On 16/08/2019 17:25, Steven Rostedt wrote: >> >> Also, write and read to/from those variables should be done with >> >> WRITE_ONCE() and READ_ONCE(), given that those are read within tracing >> >> probes without holding the sched_register_mutex. >> >> >> > >> > I understand the READ_ONCE() but is the WRITE_ONCE() truly necessary? >> > It's done while holding the mutex. It's not that critical of a path, >> > and makes the code look ugly. >> > >> >> I seem to recall something like locking primitives don't protect you from >> store tearing / invented stores, so if you can have concurrent readers >> using READ_ONCE(), there should be a WRITE_ONCE() on the writer side, even >> if it's done in a critical section. > > But for this, it really doesn't matter. The READ_ONCE() is for going > from 0->1 or 1->0 any other change is the same as 1. Let's consider this "invented store" scenario (even though as I noted in my earlier email, I suspect this particular instance of the code does not appear to fit the requirements to generate this in the wild with current compilers): intial state: sched_tgid_ref = 10; Thread A Thread B registration tracepoint probe sched_tgid_ref++ - compiler decides to invent a store: sched_tgid_ref = 0 READ_ONCE(sched_tgid_ref) -> observes 0, but should really see either 10 or 11. - compiler stores 11 into sched_tgid_ref This kind of scenario could cause spurious missing data in the middle of a trace caused by another user trying to increment the reference count, which is really unexpected. A similar scenario can happen for "store tearing" if the compiler decides to break the store into many stores close to the 16-bit overflow value when updating a 32-bit reference count. Spurious 1 -> 0 -> 1 transitions could be observed by readers. > When we enable trace events, we start recording the tasks comms such > that we can possibly map them to the pids. When we disable trace > events, we stop recording the comms so that we don't overwrite the > cache when not needed. Note, if more than the max cache of tasks are > recorded during a session, we are likely to miss comms anyway. > > Thinking about this more, the READ_ONCE() and WRTIE_ONCE() are not even > needed, because this is just a best effort anyway. If you choose not to use READ_ONCE(), then the "load tearing" issue can cause similar spurious 1 -> 0 -> 1 transitions near 16-bit counter overflow as described above. The "Invented load" also becomes an issue, because the compiler could use the loaded value for a branch, and re-load that value between two branches which are expected to use the same value, effectively generating a corrupted state. I think we need a statement about whether READ_ONCE/WRITE_ONCE should be used in this kind of situation, or if we are fine dealing with the awkward compiler side-effects when they will occur. Thanks, Mathieu > > The only real fix was to move the check into the mutex protect area, > because that can cause a real bug if there was a race. > > { > - bool sched_register = (!sched_cmdline_ref && !sched_tgid_ref); > + bool sched_register; > + > mutex_lock(&sched_register_mutex); > + sched_register = (!sched_cmdline_ref && !sched_tgid_ref); > > Thus, I'd like to see a v2 of this patch without the READ_ONCE() or > WRITE_ONCE() added. > > -- Steve -- Mathieu Desnoyers EfficiOS Inc. http://www.efficios.com