Received: by 10.223.164.221 with SMTP id h29csp2427789wrb; Thu, 2 Nov 2017 10:46:09 -0700 (PDT) X-Google-Smtp-Source: ABhQp+QyixYAETZQ8axovqwgqnsKqq/n/fQMFwdstTNv0JUYA60depai75K5F0iyEp6w5AZs2nFu X-Received: by 10.98.68.8 with SMTP id r8mr4642211pfa.161.1509644769431; Thu, 02 Nov 2017 10:46:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1509644769; cv=none; d=google.com; s=arc-20160816; b=tSUSc7PPS/WlhDqADm0w4I+15KolyXpQrji2WUIq06pa66EwS16/Wg+nM7rIm5XcDc 1ns+wY2bg7WCYD3y6z8WLIuGhtbqkdu3gIUfDMcucPxYVySM0ZNwIqnTPzXYhefpF5xC UtHGexsbOu8SEA60AaAaULmKo4+JyP01Y3tsyoJW46GoK++wLX+gyHQ4ucpFMQt5d7vC 0KfpLgjQgQBTChelnudUC8pk5xFcBRWS33H8tHmgcxuz0wzZaZAv24aF7CDVSKRvFJQT +nEVIP3fDPahKp+/cnNRuAxWSSqpvJmndWNIKPmOoAAnvVSgE69T1lgEZ9p4XwqP1bG4 +Qwg== 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:arc-authentication-results; bh=2HFqITiseJ+pVCeed5JCjNlubh8yT49aCrcHski3SJg=; b=ORi1wtSfO6q3Hk563EvBEh1k/5LE8tSOpC5m2Ytqe/MWw1ntbWp2djHUz93QAwWntZ rLpFKcTfdFxHvF3ykPEX6o+7ilsFsnNWJWtaYAYO9guS0ZkRGj79f/y+vNWOh+g4esbD 7yLvgPjZuhQvYOSiIRDUFx6C2U+Yj9jXIV+FXcUjyix+7FKHLudjkW53jXD6GstRVRtK m5bk/qIGfo8dLFYYWllTU3F89//F4qrF763TS1eYb2lPuj7hxTPyCP2jO6u6UoA+ewdJ gwhp7EQHVTS5cS3NvMdiDbzR+5lto7QcqfBi4bqzRExmuS3qCiKMSkCx/yXnUaPaCeqx 08WA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=XE90Exc/; 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=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id m26si4078968pgc.466.2017.11.02.10.45.56; Thu, 02 Nov 2017 10:46:09 -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=@gmail.com header.s=20161025 header.b=XE90Exc/; 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=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755865AbdKBRpQ (ORCPT + 97 others); Thu, 2 Nov 2017 13:45:16 -0400 Received: from mail-wr0-f196.google.com ([209.85.128.196]:49256 "EHLO mail-wr0-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755781AbdKBRpN (ORCPT ); Thu, 2 Nov 2017 13:45:13 -0400 Received: by mail-wr0-f196.google.com with SMTP id g90so314751wrd.6 for ; Thu, 02 Nov 2017 10:45:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=2HFqITiseJ+pVCeed5JCjNlubh8yT49aCrcHski3SJg=; b=XE90Exc/E+saLeKNDOzCAjgzQOavT3Si7d1u8kbb463jIv/P6Oc5PD+WrPH62n262q 8vCvnjzb3dpyN4ekFVMLg/dr3f04WhIifbEA1D8KSYzCmIs8bi+ZRbuiIgCphvnz5mOj +8OeAU9/fHByeZDSP9i7WtOgCbVHNXhsMZPuDO7Q526zQOowNZ1cdITOqzTBNnITLYwR utG+hY4Ly1lgvNw+TBURgZXs3QIsDL3KptJ9iH5mYSujc0sztLN951CXbETEy81+YZBc y/oHFFh7lshU5c+T3doWo7d6g8f3Q8YpqPVw95n5sSjOnS4PqQ6lH7c9nuE1kf96+kC3 Zyzg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=2HFqITiseJ+pVCeed5JCjNlubh8yT49aCrcHski3SJg=; b=tr2lTxrVm3gQBZ1eWzls3yzsCtfa0JQP5NSopnmAMG8Z+4R1uYxTjE6hshUxUzFRyp dcMD0GjVCR5EAjFI/Xj/iSdOXdP1xWhJa+w3BX4El7c2xm7W8iRjLeDHa6Mgyd1Bp5Xt 6rDNFRZI2G4BZqtC/xlEuIwwCIR+MjbsLb1Pr3PPjXEnghtQEVJvpGWj9Tr0pvnFc2OG ffQnvjAasBm5UNGfeJUb8NOpmOUME69RVZgT6RpC6ZNGi6I8YekRV8MsFd2Px2q4snuX v6J/v1zfqV627cGYVs1kaN4aI/0zXQfNMzUbk0VkMvJE8ltrUbzETYbB9nXp+xyfd0vK O6XA== X-Gm-Message-State: AMCzsaVDl0kELwtbpKmFXTbpedMOJj2VlPXJxYDBNK3kaHZGa6vzzbcB abE+aIDorsmsyLbOppfKbhk= X-Received: by 10.223.153.234 with SMTP id y97mr3581559wrb.165.1509644712453; Thu, 02 Nov 2017 10:45:12 -0700 (PDT) Received: from andrea (host10-10-dynamic.49-82-r.retail.telecomitalia.it. [82.49.10.10]) by smtp.gmail.com with ESMTPSA id m64sm195701wmb.10.2017.11.02.10.45.10 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 02 Nov 2017 10:45:11 -0700 (PDT) Date: Thu, 2 Nov 2017 18:45:04 +0100 From: Andrea Parri To: Alan Stern Cc: Peter Zijlstra , "Reshetova, Elena" , "linux-kernel@vger.kernel.org" , "gregkh@linuxfoundation.org" , "keescook@chromium.org" , "tglx@linutronix.de" , "mingo@redhat.com" , "ishkamiel@gmail.com" , Will Deacon , Paul McKenney , boqun.feng@gmail.com, dhowells@redhat.com, david@fromorbit.com Subject: Re: [PATCH] refcount: provide same memory ordering guarantees as in atomic_t Message-ID: <20171102174504.GA19833@andrea> References: <20171102160237.t2xkryg6joskf77y@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.5.24 (2015-08-30) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Nov 02, 2017 at 01:08:52PM -0400, Alan Stern wrote: > On Thu, 2 Nov 2017, Peter Zijlstra wrote: > > > On Thu, Nov 02, 2017 at 11:40:35AM -0400, Alan Stern wrote: > > > On Thu, 2 Nov 2017, Peter Zijlstra wrote: > > > > > > > > Lock functions such as refcount_dec_and_lock() & > > > > > refcount_dec_and_mutex_lock() Provide exactly the same guarantees as > > > > > they atomic counterparts. > > > > > > > > Nope. The atomic_dec_and_lock() provides smp_mb() while > > > > refcount_dec_and_lock() merely orders all prior load/store's against all > > > > later load/store's. > > > > > > In fact there is no guaranteed ordering when refcount_dec_and_lock() > > > returns false; > > > > It should provide a release: > > > > - if !=1, dec_not_one will provide release > > - if ==1, dec_not_one will no-op, but then we'll acquire the lock and > > dec_and_test will provide the release, even if the test fails and we > > unlock again it should still dec. > > > > The one exception is when the counter is saturated, but in that case > > we'll never free the object and the ordering is moot in any case. > > Also if the counter is 0, but that will never happen if the > refcounting is correct. > > > > it provides ordering only if the return value is true. > > > In which case it provides acquire ordering (thanks to the spin_lock), > > > and both release ordering and a control dependency (thanks to the > > > refcount_dec_and_test). > > > > > > > The difference is subtle and involves at least 3 CPUs. I can't seem to > > > > write up anything simple, keeps turning into monsters :/ Will, Paul, > > > > have you got anything simple around? > > > > > > The combination of acquire + release is not the same as smp_mb, because > > > > acquire+release is nothing, its release+acquire that I meant which > > should order things locally, but now that you've got me looking at it > > again, we don't in fact do that. > > > > So refcount_dec_and_lock() will provide a release, irrespective of the > > return value (assuming we're not saturated). If it returns true, it also > > does an acquire for the lock. > > > > But combined they're acquire+release, which is unfortunate.. it means > > the lock section and the refcount stuff overlaps, but I don't suppose > > that's actually a problem. Need to consider more. > > Right. To address your point: release + acquire isn't the same as a > full barrier either. The SB pattern illustrates the difference: > > P0 P1 > Write x=1 Write y=1 > Release a smp_mb > Acquire b Read x=0 > Read y=0 > > This would not be allowed if the release + acquire sequence was > replaced by smp_mb. But as it stands, this is allowed because nothing > prevents the CPU from interchanging the order of the release and the > acquire -- and then you're back to the acquire + release case. > > However, there is one circumstance where this interchange isn't > allowed: when the release and acquire access the same memory > location. Thus: > > P0(int *x, int *y, int *a) > { > int r0; > > WRITE_ONCE(*x, 1); > smp_store_release(a, 1); > smp_load_acquire(a); > r0 = READ_ONCE(*y); > } > > P1(int *x, int *y) > { > int r1; > > WRITE_ONCE(*y, 1); > smp_mb(); > r1 = READ_ONCE(*x); > } > > exists (0:r0=0 /\ 1:r1=0) > > This is forbidden. It would remain forbidden even if the smp_mb in P1 > were replaced by a similar release/acquire pair for the same memory > location. Hopefully, the LKMM does not agree with this assessment... ;-) > > To see the difference between smp_mb and release/acquire requires three > threads: > > P0 P1 P2 > Write x=1 Read y=1 Read z=1 > Release a data dep. smp_rmb > Acquire a Write z=1 Read x=0 > Write y=1 > > The Linux Kernel Memory Model allows this execution, although as far as > I know, no existing hardware will do it. But with smp_mb in P0, the > execution would be forbidden. Here's a two-threads example showing that "(w)mb is _not_ rfi-rel-acq": C rfi-rel-acq-is-not-mb {} P0(int *x, int *y, int *a) { WRITE_ONCE(*x, 1); smp_store_release(a, 1); r1 = smp_load_acquire(a); WRITE_ONCE(*y, 1); } P1(int *x, int *y) { int r0; int r1; r0 = READ_ONCE(*y); smp_rmb(); r1 = READ_ONCE(*x); } exists (1:r0=1 /\ 1:r1=0) Andrea > > None of this should be a problem for refcount_dec_and_lock, assuming it > is used purely for reference counting. > > Alan Stern > From 1582976102125776643@xxx Thu Nov 02 17:27:32 +0000 2017 X-GM-THRID: 1582046402032606032 X-Gmail-Labels: Inbox,Category Forums,HistoricalUnread