Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp7155599imu; Tue, 22 Jan 2019 01:06:42 -0800 (PST) X-Google-Smtp-Source: ALg8bN6lDu+PcJhNvGJt4knBVo8n0l1p4BfOhonoB5N/oZ+5HvWrxJM3yT4LQoJULLMBC3B+f/au X-Received: by 2002:a63:5723:: with SMTP id l35mr30094243pgb.228.1548148001970; Tue, 22 Jan 2019 01:06:41 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1548148001; cv=none; d=google.com; s=arc-20160816; b=y58pfrmnJekkntOrbhQOosyq1PJMTnVcHMc81dyM87HqtQ9bcttoHjo0FUZtq5KqZV tUqbGko8dw/S52edBOA5ivLP5A/GSOzrLHi4FuyYOemKvbocvIH0/vK5HTUCmaOlNQbC Hcpe43dof0t1zPEX3Dhrd0sRZNonvRVTJ8z6BokoPDWLTtemUXjZI/Hl8GhvAsvWbvar to+pA4ebWNb4TzUx1a0yufnXEt52JaNxcl0zU6WAO1ndWB1b7toTGioDI2n8Mz07wGqk azubpWrfipxGqpCZOEri7sQpiIV9wxxmQ51jRWayURr0PHGk0uuIYR+EA4mNWBNYKI/L LNuQ== 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=nEOchColTM/3kdo5tv9rkLbmsX1DYi0ROeJ5vx0DwLg=; b=Ytgq5TXvvr18phM3YTssBxZU3mfsk2u87RXSPaK29ROpJy2Yk1COumUqTb1xuAEN+a K5gK7Rt9YUm55yfXIUiTQWL/LqBphig+jf+84+FcurtSvn5dAQSmEC/rVb+491AG2h9V +XZBpi/LFSwSUBYvmVSJ7PD+9RLGQgzBE/+0aW3GWX31HwSpgnP2eHuknNactWa7ROfA XVobFgEYzsveg8v2gVRkfTMasxhFJFJjLktjPQ25z4DNAKvU+Eh8DU6FGYhlILAiqdne FG9bYtqgJlT2CgE309J8QAoHqZFEGGQ3Mg1cw0bkRwWcV0Iars9YgA4C/2n+BrBsfyha liXg== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=jxRLfJeW; 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 d66si14638777pfg.36.2019.01.22.01.06.26; Tue, 22 Jan 2019 01:06:41 -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=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=jxRLfJeW; 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 S1727542AbfAVJFE (ORCPT + 99 others); Tue, 22 Jan 2019 04:05:04 -0500 Received: from bombadil.infradead.org ([198.137.202.133]:50422 "EHLO bombadil.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727299AbfAVJFE (ORCPT ); Tue, 22 Jan 2019 04:05:04 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20170209; h=In-Reply-To:Content-Type:MIME-Version :References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=nEOchColTM/3kdo5tv9rkLbmsX1DYi0ROeJ5vx0DwLg=; b=jxRLfJeWQs5cm40aTmQ/pQ/hp A9UCxHRXRilBxmDkUNQHugVDyVf1zK29MnFohA7uTF7TP2uCdIOEmb27zp/qBhotGSCl1NjkRESpi EA7tWbyJ40puJ3KiCQfeyrmmx0A81bBoEX/9h2sebtOmqpHpr3KXH85aqYlM3/R58vngiTD+7+do3 8+pppJHz1TqhVqCjgOiQwpVBfyzcXkjoprbBc8uCEsGLYaK5JdWOuWDLTjo40QiNF1ueMzL35NN1w JxqTJKvj1XjnAOWh1hsiKZEltEWJnxBQ2e5fyhe61BPu+xTsz08XoTslaw9xeULGH4CNkWVm8fBB8 jTjP/LfKw==; Received: from j217100.upc-j.chello.nl ([24.132.217.100] helo=hirez.programming.kicks-ass.net) by bombadil.infradead.org with esmtpsa (Exim 4.90_1 #2 (Red Hat Linux)) id 1glrzO-0000oN-65; Tue, 22 Jan 2019 09:04:58 +0000 Received: by hirez.programming.kicks-ass.net (Postfix, from userid 1000) id 84E4B20D15814; Tue, 22 Jan 2019 10:04:56 +0100 (CET) Date: Tue, 22 Jan 2019 10:04:56 +0100 From: Peter Zijlstra To: Alan Stern Cc: Dmitry Vyukov , Elena Reshetova , Andrea Parri , Kees Cook , "Paul E. McKenney" , Will Deacon , Andrew Morton , Andrey Ryabinin , Anders Roxell , Mark Rutland , LKML Subject: Re: [PATCH] kcov: convert kcov.refcount to refcount_t Message-ID: <20190122090456.GE13777@hirez.programming.kicks-ass.net> References: <20190121131816.GC17749@hirez.programming.kicks-ass.net> 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 Mon, Jan 21, 2019 at 11:05:03AM -0500, Alan Stern wrote: > On Mon, 21 Jan 2019, Peter Zijlstra wrote: > > Any additional ordering; like the one you have above; are not strictly > > required for the proper functioning of the refcount. Rather, you rely on > > additional ordering and will need to provide this explicitly: > > > > > > if (refcount_dec_and_text(&x->rc)) { > > /* > > * Comment that explains what we order against.... > > */ > > smp_mb__after_atomic(); > > BUG_ON(!x->done*); > > free(x); > > } > > > > > > Also; these patches explicitly mention that refcount_t is weaker, > > specifically to make people aware of this difference. > > > > A full smp_mb() (or two) would also be much more expensive on a number > > of platforms and in the vast majority of the cases it is not required. > > How about adding smp_rmb() into refcount_dec_and_test()? That would > give acq+rel semantics, which seems to be what people will expect. And > it wouldn't be nearly as expensive as a full smp_mb(). Yes, that's a very good suggestion. I suppose we can add smp_acquire__after_ctrl_dep() on the true branch. Then it reall does become rel_acq. A wee something like so (I couldn't find an arm64 refcount, even though I have distinct memories of talk about it). This isn't compiled, and obviously needs comment/documentation updates to go along with it. --- arch/x86/include/asm/refcount.h | 9 ++++++++- lib/refcount.c | 7 ++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/arch/x86/include/asm/refcount.h b/arch/x86/include/asm/refcount.h index dbaed55c1c24..6f7a1eb345b4 100644 --- a/arch/x86/include/asm/refcount.h +++ b/arch/x86/include/asm/refcount.h @@ -74,9 +74,16 @@ bool refcount_sub_and_test(unsigned int i, refcount_t *r) static __always_inline __must_check bool refcount_dec_and_test(refcount_t *r) { - return GEN_UNARY_SUFFIXED_RMWcc(LOCK_PREFIX "decl", + bool ret = GEN_UNARY_SUFFIXED_RMWcc(LOCK_PREFIX "decl", REFCOUNT_CHECK_LT_ZERO, r->refs.counter, e, "cx"); + + if (ret) { + smp_acquire__after_ctrl_dep(); + return true; + } + + return false; } static __always_inline __must_check diff --git a/lib/refcount.c b/lib/refcount.c index ebcf8cd49e05..8276ad349d48 100644 --- a/lib/refcount.c +++ b/lib/refcount.c @@ -190,7 +190,12 @@ bool refcount_sub_and_test_checked(unsigned int i, refcount_t *r) } while (!atomic_try_cmpxchg_release(&r->refs, &val, new)); - return !new; + if (!new) { + smp_acquire__after_ctrl_dep(); + return true; + } + + return false; } EXPORT_SYMBOL(refcount_sub_and_test_checked);