Skip to contentSkip to conversation

niu: Use refcount_t for refcount

PATCH Superseded Chuhong Yuan 1 participant 2 messages Aug 2, 2019

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 niu: Use refcount_t for refcount

refcount_t is better for reference counters since its implementation can prevent overflows.
So convert atomic_t ref counters to refcount_t.

Also convert refcount from 0-based to 1-based.

---
 drivers/net/ethernet/sun/niu.c | 6 +++---
 drivers/net/ethernet/sun/niu.h | 2 +-
 2 files changed, 4 insertions(+), 4 deletions(-)
drivers/net/ethernet/sun/niu.c +3 -3
index 0bc5863bffeb..5bf096e51db7 100644
@@ -9464,7 +9464,7 @@ static struct niu_parent *niu_new_parent(struct niu *np,
 	memcpy(&p->id, id, sizeof(*id));
 	p->plat_type = ptype;
 	INIT_LIST_HEAD(&p->list);
-	atomic_set(&p->refcnt, 0);
+	refcount_set(&p->refcnt, 1);
 	list_add(&p->list, &niu_parent_list);
 	spin_lock_init(&p->lock);
 
@@ -9524,7 +9524,7 @@ static struct niu_parent *niu_get_parent(struct niu *np,
 					port_name);
 		if (!err) {
 			p->ports[port] = np;
-			atomic_inc(&p->refcnt);
+			refcount_inc(&p->refcnt);
 		}
 	}
 	mutex_unlock(&niu_parent_lock);
@@ -9552,7 +9552,7 @@ static void niu_put_parent(struct niu *np)
 	p->ports[port] = NULL;
 	np->parent = NULL;
 
-	if (atomic_dec_and_test(&p->refcnt)) {
+	if (refcount_dec_and_test(&p->refcnt)) {
 		list_del(&p->list);
 		platform_device_unregister(p->plat_dev);
 	}
drivers/net/ethernet/sun/niu.h +1 -1
index 04c215f91fc0..755e6dd4c903 100644
@@ -3071,7 +3071,7 @@ struct niu_parent {
 
 	struct niu		*ports[NIU_MAX_PORTS];
 
-	atomic_t		refcnt;
+	refcount_t		refcnt;
 	struct list_head	list;
 
 	spinlock_t		lock;
Signature · 1 line
2.20.1
#1 lore raw
Quoted 6 lines · Chuhong Yuan <[email protected]> 于2019年8月2日周五 下午8:57写道:

refcount_t is better for reference counters since its implementation can prevent overflows.
So convert atomic_t ref counters to refcount_t.

Also convert refcount from 0-based to 1-based.

It seems that directly converting refcount from 0-based to 1-based is infeasible.
I am sorry for this mistake.

Regards,
Chuhong

Quoted 54 lines
---
 drivers/net/ethernet/sun/niu.c | 6 +++---
 drivers/net/ethernet/sun/niu.h | 2 +-
 2 files changed, 4 insertions(+), 4 deletions(-)
drivers/net/ethernet/sun/niu.c +3 -3
index 0bc5863bffeb..5bf096e51db7 100644
@@ -9464,7 +9464,7 @@ static struct niu_parent *niu_new_parent(struct niu *np,
        memcpy(&p->id, id, sizeof(*id));
        p->plat_type = ptype;
        INIT_LIST_HEAD(&p->list);
-       atomic_set(&p->refcnt, 0);
+       refcount_set(&p->refcnt, 1);
        list_add(&p->list, &niu_parent_list);
        spin_lock_init(&p->lock);
 
@@ -9524,7 +9524,7 @@ static struct niu_parent *niu_get_parent(struct niu *np,
                                        port_name);
                if (!err) {
                        p->ports[port] = np;
-                       atomic_inc(&p->refcnt);
+                       refcount_inc(&p->refcnt);
                }
        }
        mutex_unlock(&niu_parent_lock);
@@ -9552,7 +9552,7 @@ static void niu_put_parent(struct niu *np)
        p->ports[port] = NULL;
        np->parent = NULL;
 
-       if (atomic_dec_and_test(&p->refcnt)) {
+       if (refcount_dec_and_test(&p->refcnt)) {
                list_del(&p->list);
                platform_device_unregister(p->plat_dev);
        }
drivers/net/ethernet/sun/niu.h +1 -2
index 04c215f91fc0..755e6dd4c903 100644
@@ -3071,7 +3071,7 @@ struct niu_parent {
 
        struct niu              *ports[NIU_MAX_PORTS];
 
-       atomic_t                refcnt;
+       refcount_t              refcnt;
        struct list_head        list;
 
        spinlock_t              lock;
--

2.20.1