Received: by 2002:a25:b794:0:0:0:0:0 with SMTP id n20csp3040820ybh; Mon, 5 Aug 2019 10:56:07 -0700 (PDT) X-Google-Smtp-Source: APXvYqyrXmb1sQ/5ziNb7QYmiiG/8a+1S8f39ubWfPxgzdBcSQpdaSiASg6Pt8xZcvEX9voSQx9C X-Received: by 2002:a63:5648:: with SMTP id g8mr20904188pgm.81.1565027767711; Mon, 05 Aug 2019 10:56:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1565027767; cv=none; d=google.com; s=arc-20160816; b=HSUMaLS9j3lvO27xp8FjREZ6bIQsGPbqCOrjlIJ/nB2LwvC1RpyqFqARCCNVcsfstU WGI2oa39ENzvXXPXOHpQCPdNCYSWqZbcsN04g6DYR1/TjVpG4iqpcTjvmyLqNtIVQmE6 6OIrvFgM8Ie5ysY13hfL+ztihAjIAhSuqzMdLjhyUNiOt/YdfI5cmybZKf7pqJ6kIiry tFTteILmS3U5ASgP+pCwgP8x6NidQRAMNgE4uJpBQ0jr2k905tOJ0o5kJuDpf/P9jw+y /UVWj/zLQ5r1NqXSbQAcgGmzf3ELesEduEhJZax5Uo7NhjrkRaZMJK39kk3yJ7iMrnCq WTig== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:from:subject:cc:to:message-id:date; bh=fp5jPYRNjdaOp5fkVvvT2gFfGIBNcEaVGTt31fxHejg=; b=UgBDQjH8mzlGZWmXfkODnxgblNRNijj5hKE+FtNqtm068RWyvSU1zxb1rVk/9CPbPP Al2Z9QVgXZfJoskaGGv5iCV3H+N30nrxvbHTmGXqE4L6+tqQtQU+c7XyUevZloS+8g4E kEC+g3tW32/Z0i3CXu2hwqJP5w/JwNvXkInOvTLFPT6oGs2yObX0HsAJrIWyC6msFBpE VsB144Xwi2k3PWeA837s0tFNQzsQCll2LMAndQKwZh1sWdlOI9RXi7xf5bGTi0fwro0t BMUgbbTYbTduJfFr2PKVOIqP3iPNzDv+UPpc70Bex8F734Zwh49qsk2U264aqwJjvC2c RMLw== 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id a17si45057292pfk.246.2019.08.05.10.55.51; Mon, 05 Aug 2019 10:56:07 -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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729765AbfHERzB (ORCPT + 99 others); Mon, 5 Aug 2019 13:55:01 -0400 Received: from shards.monkeyblade.net ([23.128.96.9]:59882 "EHLO shards.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726559AbfHERzB (ORCPT ); Mon, 5 Aug 2019 13:55:01 -0400 Received: from localhost (unknown [IPv6:2601:601:9f80:35cd::d71]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) (Authenticated sender: davem-davemloft) by shards.monkeyblade.net (Postfix) with ESMTPSA id A05B51540A46F; Mon, 5 Aug 2019 10:55:00 -0700 (PDT) Date: Mon, 05 Aug 2019 10:55:00 -0700 (PDT) Message-Id: <20190805.105500.1555481916904502971.davem@davemloft.net> To: hslester96@gmail.com Cc: vishal@chelsio.com, netdev@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v2 2/2] cxgb4: smt: Use refcount_t for refcount From: David Miller In-Reply-To: <20190802083547.12657-1-hslester96@gmail.com> References: <20190802083547.12657-1-hslester96@gmail.com> X-Mailer: Mew version 6.8 on Emacs 26.1 Mime-Version: 1.0 Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Greylist: Sender succeeded SMTP AUTH, not delayed by milter-greylist-4.5.12 (shards.monkeyblade.net [149.20.54.216]); Mon, 05 Aug 2019 10:55:00 -0700 (PDT) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Chuhong Yuan Date: Fri, 2 Aug 2019 16:35:47 +0800 > refcount_t is better for reference counters since its > implementation can prevent overflows. > So convert atomic_t ref counters to refcount_t. > > Signed-off-by: Chuhong Yuan > --- > Changes in v2: > - Convert refcount from 0-base to 1-base. The existing code is buggy and should be fixed before you start making conversions to it. > @@ -111,7 +111,7 @@ static void t4_smte_free(struct smt_entry *e) > */ > void cxgb4_smt_release(struct smt_entry *e) > { > - if (atomic_dec_and_test(&e->refcnt)) > + if (refcount_dec_and_test(&e->refcnt)) > t4_smte_free(e); This runs without any locking and therefore: > if (e) { > spin_lock(&e->lock); > - if (!atomic_read(&e->refcnt)) { > - atomic_set(&e->refcnt, 1); > + if (refcount_read(&e->refcnt) == 1) { > + refcount_set(&e->refcnt, 2); This test is not safe, since the reference count can asynchronously decrement to zero above outside of any locks. Then you'll need to add locking, and as a result the need to an atomic counter goes away and just a normal int can be used.