Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754637AbcKUUMg (ORCPT ); Mon, 21 Nov 2016 15:12:36 -0500 Received: from mail-ua0-f196.google.com ([209.85.217.196]:35692 "EHLO mail-ua0-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753809AbcKUUMf (ORCPT ); Mon, 21 Nov 2016 15:12:35 -0500 MIME-Version: 1.0 In-Reply-To: <2236FBA76BA1254E88B949DDB74E612B41C156AE@IRSMSX102.ger.corp.intel.com> References: <20161117085342.GB3142@twins.programming.kicks-ass.net> <20161117161937.GA46515@ast-mbp.thefacebook.com> <2236FBA76BA1254E88B949DDB74E612B41C14BB4@IRSMSX102.ger.corp.intel.com> <2236FBA76BA1254E88B949DDB74E612B41C15583@IRSMSX102.ger.corp.intel.com> <20161121154915.GB3124@twins.programming.kicks-ass.net> <20161121160059.GB3174@twins.programming.kicks-ass.net> <2236FBA76BA1254E88B949DDB74E612B41C156AE@IRSMSX102.ger.corp.intel.com> From: David Windsor Date: Mon, 21 Nov 2016 15:12:33 -0500 Message-ID: Subject: Re: [RFC][PATCH 2/7] kref: Add kref_read() To: "Reshetova, Elena" Cc: Peter Zijlstra , Alexei Starovoitov , Kees Cook , Greg KH , Will Deacon , Arnd Bergmann , Thomas Gleixner , Ingo Molnar , "H. Peter Anvin" , LKML , Daniel Borkmann Content-Type: text/plain; charset=UTF-8 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 1414 Lines: 39 On Mon, Nov 21, 2016 at 2:27 PM, Reshetova, Elena wrote: >> On Mon, Nov 21, 2016 at 04:49:15PM +0100, Peter Zijlstra wrote: >> > > Speaking of non-fitting patterns. This one is quite common in >> > > networking code for refcounters: >> > > >> > > if (atomic_cmpxchg(&cur->refcnt, 1, 0) == 1) {} This is from >> > > net/netfilter/nfnetlink_acct.c, but there are similar ones in other >> > > places. >> > >> > Cute, but weird it doesn't actually decrement if not 1. >> >> Hurgh.. creative refcounting that. The question is how much of that do >> we want to support? It really must not decrement there. > > And one more creative usage: > > http://lxr.free-electrons.com/source/net/ipv4/udp.c#L1940 > > if (!sk || !atomic_inc_not_zero_hint(&sk->sk_refcnt, 2)) > return; > > I didn't even guess anyone is using atomic_inc_not_zero_hint... > But network code keeps surprising me today :) > So, yes, I guess the question is what to do with these cases really? Many of the calls to non-supported functions can be decomposed into calls to supported functions. The ones that may prove interesting are ones like atomic_cmpxchg(), in which some sort of external locking is going to be required to achieve the same atomicity guarantees provided by cmpxchg, like so: mutex_lock(lock); cnt = refcount_read(ref); if (cnt == val1) { refcount_set(ref, val2); } mutex_unlock(lock); return cnt;