From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751078AbdE2MXc (ORCPT ); Mon, 29 May 2017 08:23:32 -0400 Received: from bombadil.infradead.org ([65.50.211.133]:46689 "EHLO bombadil.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750925AbdE2MXa (ORCPT ); Mon, 29 May 2017 08:23:30 -0400 Date: Mon, 29 May 2017 14:23:16 +0200 From: Peter Zijlstra To: "Eric W. Biederman" Cc: Christoph Hellwig , Kees Cook , Andrew Morton , Elena Reshetova , Greg KH , Ingo Molnar , Alexey Dobriyan , "Serge E. Hallyn" , arozansk@redhat.com, Davidlohr Bueso , Manfred Spraul , "axboe@kernel.dk" , James Bottomley , "x86@kernel.org" , Ingo Molnar , Arnd Bergmann , "David S. Miller" , Rik van Riel , linux-arch , "kernel-hardening@lists.openwall.com" , LKML Subject: Re: [PATCH 0/3] ipc subsystem refcounter conversions Message-ID: <20170529122316.giceifwjnuz6djr4@hirez.programming.kicks-ass.net> References: <1487590189-18151-1-git-send-email-elena.reshetova@intel.com> <20170303162352.b6af1c0c3115b3f5f1e7aed3@linux-foundation.org> <20170529083903.GA17735@infradead.org> <87h904xc26.fsf@xmission.com> <20170529102442.gerbzxzixllen46q@hirez.programming.kicks-ass.net> <87a85wvsxa.fsf@xmission.com> <87o9ubvr0t.fsf@xmission.com> <8760gjvqm7.fsf@xmission.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <8760gjvqm7.fsf@xmission.com> User-Agent: NeoMutt/20170113 (1.7.2) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, May 29, 2017 at 06:39:44AM -0500, Eric W. Biederman wrote: > I failed to see that there is a refcount_inc. Too much noise in > the header file I suppose. > > But implementing refcount_inc in terms of refcount_inc_not_zero is > totally broken. The two operations are not the same and the go to > different assumptions the code is making. > > That explains why you think refcount_inc_not_zero should lie because > you are implementing refcount_inc with it. They are semantically very > different operations. Please separate them. There has been much debate about this. And the best I'll do is add a comment and/or retain these exact semantics. What is done is: refcount_inc() := WARN_ON(!refcount_inc_not_zero()) Because incrementing a zero reference count is a use-after-free and something we should not do ever. This is where the whole usage count vs reference count pain comes from. Once there are no more _references_ to an object, a reference count frees the object. Therefore a zero reference count means a dead object and incrementing from that is fail. The usage count model otoh counts how many (active) users there are of an object, and no active users is a good and expected situation. But it is very explicitly not a reference count. Because even in the no users case do we have a reference to the object (we've not leaked it after all, we just don't track all references). Similarly, refcount_dec() is implemented using dec_and_test() and will WARN when it hits 0, because this is a leak and we don't want those either. A usage count variant otoh would be fine with hitting 0.