From mboxrd@z Thu Jan 1 00:00:00 1970 From: Eric Dumazet Subject: Re: [PATCH v2 2/3] net: Add BUG_ON() to get_net() Date: Tue, 09 Jan 2018 10:52:22 -0800 Message-ID: <1515523942.131759.9.camel@gmail.com> References: <151551001420.4318.12500370110778093679.stgit@localhost.localdomain> <151551003602.4318.8752010178458260165.stgit@localhost.localdomain> Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 7bit Cc: ebiederm@xmission.com To: Kirill Tkhai , netdev@vger.kernel.org, davem@davemloft.net Return-path: Received: from mail-pg0-f68.google.com ([74.125.83.68]:43083 "EHLO mail-pg0-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S934975AbeAISwY (ORCPT ); Tue, 9 Jan 2018 13:52:24 -0500 Received: by mail-pg0-f68.google.com with SMTP id f14so6015149pga.10 for ; Tue, 09 Jan 2018 10:52:24 -0800 (PST) In-Reply-To: <151551003602.4318.8752010178458260165.stgit@localhost.localdomain> Sender: netdev-owner@vger.kernel.org List-ID: On Tue, 2018-01-09 at 18:00 +0300, Kirill Tkhai wrote: > Since people may mistakenly obtain destroying net > from net_namespace_list and from net::netns_ids > without checking for its net::counter, let's protect > against such situations and insert BUG_ON() to stop > move on after this. > > Panic is better, than memory corruption and undefined > behavior. > > Signed-off-by: Kirill Tkhai > --- > include/net/net_namespace.h | 2 +- > 1 file changed, 1 insertion(+), 1 deletion(-) > > diff --git a/include/net/net_namespace.h b/include/net/net_namespace.h > index 10f99dafd5ac..ff0e47471d5b 100644 > --- a/include/net/net_namespace.h > +++ b/include/net/net_namespace.h > @@ -195,7 +195,7 @@ void __put_net(struct net *net); > > static inline struct net *get_net(struct net *net) > { > - atomic_inc(&net->count); > + BUG_ON(atomic_inc_return(&net->count) <= 1); > return net; > } Why not simply use refcount_t instead of duplicating its logic ?