Received: by 10.223.164.221 with SMTP id h29csp2292456wrb; Thu, 2 Nov 2017 08:41:31 -0700 (PDT) X-Google-Smtp-Source: ABhQp+TmIfdRaRdOT2HHO6iiLlxlO5zb2eiGrE3w+LdTNgTrFMmItyZsl+pNqWL6ZImxydXnnN98 X-Received: by 10.101.76.195 with SMTP id n3mr4178351pgt.120.1509637291176; Thu, 02 Nov 2017 08:41:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1509637291; cv=none; d=google.com; s=arc-20160816; b=mLu/dPiQSg1awnSZ0fRSDlIRhJu0/reAf2PbMmaPYHzVZvBT+LUJLSpTRy/DzXYmIq aI1jGWpRtuBn/3slDRjcgwtUjhBG+LPqK3cQvLTGFQnGPsf9v5s/IK0f4p2Lx7cuVj5J 7uNaMTNCVM9263kv2OEjtjBgIPo0Qrq19Y9f5gqxeRHNVZ5St7C29nSLxNHMwBmhtjXE xgJsa9TQs8Yysf58iXW/ZSOhC3nat5Mx2Mzsn66J4SHZOwnd/TpRrglnB6EE7VGk2O1c L0sH/vtEu2Qy9LzFJ7kZ/8nKR96ByDElF+nylRfRkTIWcUXDRnGj3UOl1EHPQfLQ1gaz JvVw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:message-id:in-reply-to :subject:cc:to:from:date:arc-authentication-results; bh=ookrNR2oY1b+bqal7g8oR6ujHJgnIhfv6uE+RmbEnBE=; b=XXKej40vpRYFqYuKOiV7YsTRJUV5b8oONOGxdtRjcO1gU97515J0MbKMLSrAKRaLa1 mWNU196VjtyKqc2jmEptRGTB2j0uPu1BcZO87FU3oYFDRxeg/gw6meD2Fd/gqUz+oXdf /WTZJwTNF1D+6SZfo4wX/eks0jQLXd+X1x0rx2DNWH/Gwl+/zdAiYnxizCpiKtEPxVcZ YLJzQSCnEYj32I+vfBAarQtbT2sxVYHm/hN5eAO9Xiedsg1TW2Lu7f4b+X6iU+GkkJ90 +F7c79DMTpG2iWxm3LAaknJcYT1t8/rjnYVuoGhRMqfVhmu0Etp0owuYEa+Bbug/pm6s NxRA== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=harvard.edu Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id f126si4204044pfg.410.2017.11.02.08.41.16; Thu, 02 Nov 2017 08:41:31 -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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=harvard.edu Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933932AbdKBPki (ORCPT + 97 others); Thu, 2 Nov 2017 11:40:38 -0400 Received: from iolanthe.rowland.org ([192.131.102.54]:32778 "HELO iolanthe.rowland.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with SMTP id S933567AbdKBPkg (ORCPT ); Thu, 2 Nov 2017 11:40:36 -0400 Received: (qmail 2308 invoked by uid 2102); 2 Nov 2017 11:40:35 -0400 Received: from localhost (sendmail-bs@127.0.0.1) by localhost with SMTP; 2 Nov 2017 11:40:35 -0400 Date: Thu, 2 Nov 2017 11:40:35 -0400 (EDT) From: Alan Stern X-X-Sender: stern@iolanthe.rowland.org To: Peter Zijlstra cc: "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 , , , , Subject: Re: [PATCH] refcount: provide same memory ordering guarantees as in atomic_t In-Reply-To: <20171102135742.7o4urtltgvhr6dku@hirez.programming.kicks-ass.net> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 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 they allow things to pass by in one direction. Example: C C-refcount-vs-atomic-dec-and-lock { } P0(int *x, int *y, refcount_t *r) { refcount_set(r, 1); WRITE_ONCE(*x, 1); smp_wmb(); WRITE_ONCE(*y, 1); } P1(int *x, int *y, refcount_t *r, spinlock_t *s) { int rx, ry; bool r1; ry = READ_ONCE(*y); r1 = refcount_dec_and_lock(r, s); if (r1) rx = READ_ONCE(*x); } exists (1:ry=1 /\ 1:r1=1 /\ 1:rx=0) This is allowed. The idea is that the CPU can take: Read y Acquire Release Read x and execute the first read after the Acquire and the second read before the Release: Acquire Read y Read x Release and then the CPU can reorder the reads: Acquire Read x Read y Release If the program had used atomic_dec_and_lock() instead, which provides a full smp_mb barrier, this outcome would not be possible. Alan Stern From 1582962996636725884@xxx Thu Nov 02 13:59:13 +0000 2017 X-GM-THRID: 1582046402032606032 X-Gmail-Labels: Inbox,Category Forums,HistoricalUnread