Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp134207imu; Tue, 22 Jan 2019 15:24:24 -0800 (PST) X-Google-Smtp-Source: ALg8bN5EGyu/Vfwg9XzAhFwpLhBngyB27IwW7+rqAsOYgilrLnsagHvmVztQ20drbnqjeMopfJtP X-Received: by 2002:a62:1542:: with SMTP id 63mr35533515pfv.230.1548199464742; Tue, 22 Jan 2019 15:24:24 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1548199464; cv=none; d=google.com; s=arc-20160816; b=o6UIgAkVX87bMmaUokYYi5hfCAgsUl/GxrpT5p9EZDJySgvlSA669YUTitPLad4+HH F9pxkhhdlgg7mQGX7E3z2T/i2ivMFl8tzwPSqRINJF1pZN5lMQYsoFXXRx3cmXa8sRIZ GxJno5J7IuOJJDSzYgK9q9HleM3hHgWKuuLYaSjP+rllEFKWsgBenMYoSF8/wuNaFXdI YO9cqnSy+/ijPrs+Vt1WRx3IDtT6UzOKJS64a99tsbOh2wY7y1npTqR0iIBsroTBO/wP HDFhF3r+eA7jOx3WUoQVIo42DXnvfX+zemrn/cnQEnPwfWE1m4BmcFSt8jw6GDvxAvEZ hb+A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=EpXWdqvCkEPWa6yAUiNF/XWYwIM8qsTQoY3KGOXsen0=; b=MsOlDibhTLVbk90aGFW0iDKw5JDBJjy7qYLNZwYeljYrmt45STfjMnCbS9V2+UqeKN dvcOJhNfa8plkOpfHZlFHRCyLCqoyJUdyky0LwRjDCyGwgnYksGX+MXyPhuq7DJazu1n YBG2cZB5GgQEdmgFZqnjtNk5dE+jww8bImwuECGYPQbPkFQOzlVJCvdgWyPSO9/f5HNm 0EF1HKktpcO3sAo3U615VTIGT0yxHLtANETS+gXmOF/5uzK/XyXoxPvmZP0ACN6Pbqpf 9Mue9mtRs5K7+vmqSrNSeOkpLSWOpCY+2r5UjsMldauGOVVczESAyNATReF1DH/TYnCd DH1A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=OesFrvNz; 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=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id a17si52054pfn.213.2019.01.22.15.24.08; Tue, 22 Jan 2019 15:24:24 -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=@chromium.org header.s=google header.b=OesFrvNz; 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=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726851AbfAVXXD (ORCPT + 99 others); Tue, 22 Jan 2019 18:23:03 -0500 Received: from mail-ua1-f67.google.com ([209.85.222.67]:36812 "EHLO mail-ua1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726765AbfAVXXD (ORCPT ); Tue, 22 Jan 2019 18:23:03 -0500 Received: by mail-ua1-f67.google.com with SMTP id j3so108768uap.3 for ; Tue, 22 Jan 2019 15:23:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=EpXWdqvCkEPWa6yAUiNF/XWYwIM8qsTQoY3KGOXsen0=; b=OesFrvNz3hNtpWMRMyd2qHuPWNXfed0smM4b4u9SdyeH4XYud4eGfRs5UJK9JZ4mZg y4f0NWsbM29zXGUQvIhxWFN3i0iOKFwbAYKgYEyecbb0VJvsgoC6n3ARxFJKSWBAPDI4 q2CGJPiNtlg/YRMU4dYa4p7ujIEnhi5DdyCso= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=EpXWdqvCkEPWa6yAUiNF/XWYwIM8qsTQoY3KGOXsen0=; b=Vv6JBBaEorvEtyso0EFYGyC5lxr/Ztf8tZlVDMq0m+cMvHNDQwBnklMSdQkPQS4mYh bJnDjsawbqpezSTjdO28fWi5x9gHJzQAae8EhWgWWLSxll/WiJp5adFpCm6pYbxIjh7B DySt7kd7Kb7y41u0XfNCDN/whsTI0N2YTD4cJdz47NN7+a1ltRgOSEDIm6TE+Q5EJxmH WSsxIwvK7HfHf5hkCm1aare+rOiHgktvXYp+bFsVS6VsV6qY7BPnnCtvXcJjjDXHvwgM 0TYHW6OWcoAI2ylnDz6+ZeA/HUmdnRhIOJ7guj5H3276tpCJ9Juq+qgMZfo1gcRSQfd/ jHTA== X-Gm-Message-State: AJcUukcQpGizJ0BtG8Pz1anlCgYNt2Oe0DzhXeMXWivqjk/CvyqycDD2 JwhuRdG5mqZ8Vte/Vv2c2ImiAv4SLHo= X-Received: by 2002:ab0:30b5:: with SMTP id b21mr5921905uam.69.1548199381432; Tue, 22 Jan 2019 15:23:01 -0800 (PST) Received: from mail-vs1-f46.google.com (mail-vs1-f46.google.com. [209.85.217.46]) by smtp.gmail.com with ESMTPSA id e67sm25267903vsd.32.2019.01.22.15.22.57 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 22 Jan 2019 15:22:58 -0800 (PST) Received: by mail-vs1-f46.google.com with SMTP id n13so198600vsk.4 for ; Tue, 22 Jan 2019 15:22:57 -0800 (PST) X-Received: by 2002:a67:848a:: with SMTP id g132mr14558030vsd.222.1548199377420; Tue, 22 Jan 2019 15:22:57 -0800 (PST) MIME-Version: 1.0 References: <20190121131816.GC17749@hirez.programming.kicks-ass.net> <20190122090456.GE13777@hirez.programming.kicks-ass.net> In-Reply-To: <20190122090456.GE13777@hirez.programming.kicks-ass.net> From: Kees Cook Date: Wed, 23 Jan 2019 12:22:44 +1300 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH] kcov: convert kcov.refcount to refcount_t To: Peter Zijlstra Cc: Alan Stern , Dmitry Vyukov , Elena Reshetova , Andrea Parri , "Paul E. McKenney" , Will Deacon , Andrew Morton , Andrey Ryabinin , Anders Roxell , Mark Rutland , LKML Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Jan 22, 2019 at 10:05 PM Peter Zijlstra wrote: > > 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). In the end, arm and arm64 chose to use REFCOUNT_FULL unconditionally, so there's no arch-specific implementation. > This isn't compiled, and obviously needs comment/documentation updates > to go along with it. Elena, can you do the doc updates? > > --- > 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); > Thanks for this! -- Kees Cook