From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-3.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id A8A9FC4727E for ; Thu, 1 Oct 2020 16:37:04 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 624912072E for ; Thu, 1 Oct 2020 16:37:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732026AbgJAQhE (ORCPT ); Thu, 1 Oct 2020 12:37:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49418 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731917AbgJAQhD (ORCPT ); Thu, 1 Oct 2020 12:37:03 -0400 Received: from ZenIV.linux.org.uk (zeniv.linux.org.uk [IPv6:2002:c35c:fd02::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BF433C0613D0; Thu, 1 Oct 2020 09:37:03 -0700 (PDT) Received: from viro by ZenIV.linux.org.uk with local (Exim 4.92.3 #3 (Red Hat Linux)) id 1kO1ZW-009vym-B9; Thu, 01 Oct 2020 16:36:46 +0000 Date: Thu, 1 Oct 2020 17:36:46 +0100 From: Al Viro To: Alan Stern Cc: "Paul E. McKenney" , parri.andrea@gmail.com, will@kernel.org, peterz@infradead.org, boqun.feng@gmail.com, npiggin@gmail.com, dhowells@redhat.com, j.alglave@ucl.ac.uk, luc.maranget@inria.fr, akiyks@gmail.com, dlustig@nvidia.com, joel@joelfernandes.org, linux-kernel@vger.kernel.org, linux-arch@vger.kernel.org Subject: Re: Litmus test for question from Al Viro Message-ID: <20201001163646.GG3421308@ZenIV.linux.org.uk> References: <20201001045116.GA5014@paulmck-ThinkPad-P72> <20201001161529.GA251468@rowland.harvard.edu> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20201001161529.GA251468@rowland.harvard.edu> Sender: Al Viro Precedence: bulk List-ID: X-Mailing-List: linux-arch@vger.kernel.org On Thu, Oct 01, 2020 at 12:15:29PM -0400, Alan Stern wrote: > > CPU1: > > to_free = NULL > > spin_lock(&LOCK) > > if (!smp_load_acquire(&V->B)) > > to_free = V > > V->A = 0 > > spin_unlock(&LOCK) > > kfree(to_free) > > > > CPU2: > > to_free = V; > > if (READ_ONCE(V->A)) { > > spin_lock(&LOCK) > > if (V->A) > > to_free = NULL > > smp_store_release(&V->B, 0); > > spin_unlock(&LOCK) > > } > > kfree(to_free); > > 1) is it guaranteed that V will be freed exactly once and that > > no accesses to *V will happen after freeing it? > > 2) do we need smp_store_release() there? I.e. will anything > > break if it's replaced with plain V->B = 0? > > Here are my answers to Al's questions: > > 1) It is guaranteed that V will be freed exactly once. It is not > guaranteed that no accesses to *V will occur after it is freed, because > the test contains a data race. CPU1's plain "V->A = 0" write races with > CPU2's READ_ONCE; What will that READ_ONCE() yield in that case? If it's non-zero, we should be fine - we won't get to kfree() until after we are done with the spinlock. And if it's zero... What will CPU1 do with *V accesses _after_ it has issued the store to V->A? Confused... > if the plain write were replaced with > "WRITE_ONCE(V->A, 0)" then the guarantee would hold. Equally well, > CPU1's smp_load_acquire could be replaced with a plain read while the > plain write is replaced with smp_store_release. Er... Do you mean the write to ->A on CPU1?