linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Yejune Deng <yejune.deng@gmail.com>
To: jlayton@kernel.org, idryomov@gmail.com
Cc: ceph-devel@vger.kernel.org, linux-kernel@vger.kernel.org,
	Yejune Deng <yejune.deng@gmail.com>
Subject: [PATCH] ceph: convert {n}ref from atomic_t to refcount_t
Date: Wed, 24 Mar 2021 18:26:25 +0800	[thread overview]
Message-ID: <20210324102625.112640-1-yejune.deng@gmail.com> (raw)

refcount_t type should be used instead of atomic_t when the variable
is used as a reference counter. This is because the implementation of
refcount_t can prevent overflows and detect possible use-after-free.

Signed-off-by: Yejune Deng <yejune.deng@gmail.com>
---
 fs/ceph/mds_client.h |  2 +-
 fs/ceph/snap.c       | 27 +++++++++++++++------------
 fs/ceph/super.h      |  2 +-
 3 files changed, 17 insertions(+), 14 deletions(-)

diff --git a/fs/ceph/mds_client.h b/fs/ceph/mds_client.h
index eaa7c5422116..bf99c5ba47fc 100644
--- a/fs/ceph/mds_client.h
+++ b/fs/ceph/mds_client.h
@@ -351,7 +351,7 @@ struct ceph_pool_perm {
 struct ceph_snapid_map {
 	struct rb_node node;
 	struct list_head lru;
-	atomic_t ref;
+	refcount_t ref;
 	u64 snap;
 	dev_t dev;
 	unsigned long last_used;
diff --git a/fs/ceph/snap.c b/fs/ceph/snap.c
index 0728b01d4d43..c0fbbb56b259 100644
--- a/fs/ceph/snap.c
+++ b/fs/ceph/snap.c
@@ -66,14 +66,15 @@ void ceph_get_snap_realm(struct ceph_mds_client *mdsc,
 			 struct ceph_snap_realm *realm)
 {
 	dout("get_realm %p %d -> %d\n", realm,
-	     atomic_read(&realm->nref), atomic_read(&realm->nref)+1);
+	     refcount_read(&realm->nref), refcount_read(&realm->nref)+1);
 	/*
 	 * since we _only_ increment realm refs or empty the empty
 	 * list with snap_rwsem held, adjusting the empty list here is
 	 * safe.  we do need to protect against concurrent empty list
 	 * additions, however.
 	 */
-	if (atomic_inc_return(&realm->nref) == 1) {
+	refcount_inc(&realm->nref);
+	if (refcount_read(&realm->nref) == 1) {
 		spin_lock(&mdsc->snap_empty_lock);
 		list_del_init(&realm->empty_item);
 		spin_unlock(&mdsc->snap_empty_lock);
@@ -117,7 +118,7 @@ static struct ceph_snap_realm *ceph_create_snap_realm(
 	if (!realm)
 		return ERR_PTR(-ENOMEM);
 
-	atomic_set(&realm->nref, 1);    /* for caller */
+	refcount_set(&realm->nref, 1);    /* for caller */
 	realm->ino = ino;
 	INIT_LIST_HEAD(&realm->children);
 	INIT_LIST_HEAD(&realm->child_item);
@@ -199,8 +200,8 @@ static void __put_snap_realm(struct ceph_mds_client *mdsc,
 			     struct ceph_snap_realm *realm)
 {
 	dout("__put_snap_realm %llx %p %d -> %d\n", realm->ino, realm,
-	     atomic_read(&realm->nref), atomic_read(&realm->nref)-1);
-	if (atomic_dec_and_test(&realm->nref))
+	     refcount_read(&realm->nref), refcount_read(&realm->nref)-1);
+	if (refcount_dec_and_test(&realm->nref))
 		__destroy_snap_realm(mdsc, realm);
 }
 
@@ -211,8 +212,8 @@ void ceph_put_snap_realm(struct ceph_mds_client *mdsc,
 			 struct ceph_snap_realm *realm)
 {
 	dout("put_snap_realm %llx %p %d -> %d\n", realm->ino, realm,
-	     atomic_read(&realm->nref), atomic_read(&realm->nref)-1);
-	if (!atomic_dec_and_test(&realm->nref))
+	     refcount_read(&realm->nref), refcount_read(&realm->nref)-1);
+	if (!refcount_dec_and_test(&realm->nref))
 		return;
 
 	if (down_write_trylock(&mdsc->snap_rwsem)) {
@@ -1034,7 +1035,8 @@ struct ceph_snapid_map* ceph_get_snapid_map(struct ceph_mds_client *mdsc,
 		} else if (snap < exist->snap) {
 			p = &(*p)->rb_right;
 		} else {
-			if (atomic_inc_return(&exist->ref) == 1)
+			refcount_inc(&exist->ref);
+			if (refcount_read(&exist->ref) == 1)
 				list_del_init(&exist->lru);
 			break;
 		}
@@ -1057,7 +1059,7 @@ struct ceph_snapid_map* ceph_get_snapid_map(struct ceph_mds_client *mdsc,
 	}
 
 	INIT_LIST_HEAD(&sm->lru);
-	atomic_set(&sm->ref, 1);
+	refcount_set(&sm->ref, 1);
 	sm->snap = snap;
 
 	exist = NULL;
@@ -1076,7 +1078,8 @@ struct ceph_snapid_map* ceph_get_snapid_map(struct ceph_mds_client *mdsc,
 		exist = NULL;
 	}
 	if (exist) {
-		if (atomic_inc_return(&exist->ref) == 1)
+		refcount_inc(&exist->ref);
+		if (refcount_read(&exist->ref) == 1)
 			list_del_init(&exist->lru);
 	} else {
 		rb_link_node(&sm->node, parent, p);
@@ -1099,7 +1102,7 @@ void ceph_put_snapid_map(struct ceph_mds_client* mdsc,
 {
 	if (!sm)
 		return;
-	if (atomic_dec_and_lock(&sm->ref, &mdsc->snapid_map_lock)) {
+	if (refcount_dec_and_lock(&sm->ref, &mdsc->snapid_map_lock)) {
 		if (!RB_EMPTY_NODE(&sm->node)) {
 			sm->last_used = jiffies;
 			list_add_tail(&sm->lru, &mdsc->snapid_map_lru);
@@ -1161,7 +1164,7 @@ void ceph_cleanup_snapid_map(struct ceph_mds_client *mdsc)
 		sm = list_first_entry(&to_free, struct ceph_snapid_map, lru);
 		list_del(&sm->lru);
 		free_anon_bdev(sm->dev);
-		if (WARN_ON_ONCE(atomic_read(&sm->ref))) {
+		if (WARN_ON_ONCE(refcount_read(&sm->ref))) {
 			pr_err("snapid map %llx -> %x still in use\n",
 			       sm->snap, sm->dev);
 		}
diff --git a/fs/ceph/super.h b/fs/ceph/super.h
index c48bb30c8d70..062123a73ef1 100644
--- a/fs/ceph/super.h
+++ b/fs/ceph/super.h
@@ -835,7 +835,7 @@ struct ceph_readdir_cache_control {
 struct ceph_snap_realm {
 	u64 ino;
 	struct inode *inode;
-	atomic_t nref;
+	refcount_t nref;
 	struct rb_node node;
 
 	u64 created, seq;
-- 
2.29.0


             reply	other threads:[~2021-03-24 10:27 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-24 10:26 Yejune Deng [this message]
2021-03-24 15:04 ` [PATCH] ceph: convert {n}ref from atomic_t to refcount_t Jeff Layton
2021-04-19 19:41 ` Jeff Layton

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210324102625.112640-1-yejune.deng@gmail.com \
    --to=yejune.deng@gmail.com \
    --cc=ceph-devel@vger.kernel.org \
    --cc=idryomov@gmail.com \
    --cc=jlayton@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).