Skip to contentSkip to conversation

tty: serial: zs: convert atomic_* to refcount_* APIs for irq_guard

PATCH In review Deepak R Varma 2 participants 2 messages Dec 24, 2022 – Jan 20, 2023

Subsystems (2)

v2 of 3 In review Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 tty: serial: zs: convert atomic_* to refcount_* APIs for irq_guard

The refcount_* APIs are designed to address known issues with the atomic_t APIs for reference counting. They provide following distinct advantages:
- protect the reference counters from overflow/underflow
- avoid use-after-free errors
- provide improved memory ordering guarantee schemes
- neater and safer.
Hence, replace the atomic_* APIs by their equivalent refcount_t API functions.

This patch proposal address the following warnings generated by the atomic_as_refcounter.cocci coccinelle script atomic_add_return(-1, ...)

---

Changes in v2:
1. Separate the combined change into one variable per patch as suggested by [email protected]
2. There was additional feedback on validating the change as it appeared to modify the existing logic. However, I found that the logic does not change with the proposed refcount_* APIs used in this change. Hence that feedback is not applied in this version.

Please Note:
The patch is compile tested using dec_station.defconfig for MIPS architecture.

 drivers/tty/serial/zs.c | 14 +++++---------
 drivers/tty/serial/zs.h |  2 +-
 2 files changed, 6 insertions(+), 10 deletions(-)
drivers/tty/serial/zs.c +5 -9
index 730c648e32ff..6be9933eff5c 100644
@@ -753,17 +753,15 @@ static int zs_startup(struct uart_port *uport)
 	struct zs_port *zport = to_zport(uport);
 	struct zs_scc *scc = zport->scc;
 	unsigned long flags;
-	int irq_guard;
 	int ret;
 
-	irq_guard = atomic_add_return(1, &scc->irq_guard);
-	if (irq_guard == 1) {
+	refcount_inc(&scc->irq_guard);
+	if (refcount_read(&scc->irq_guard) == 1) {
 		ret = request_irq(zport->port.irq, zs_interrupt,
 				  IRQF_SHARED, "scc", scc);
 		if (ret) {
-			atomic_add(-1, &scc->irq_guard);
-			printk(KERN_ERR "zs: can't get irq %d\n",
-			       zport->port.irq);
+			refcount_dec(&scc->irq_guard);
+			printk(KERN_ERR "zs: can't get irq %d\n", zport->port.irq);
 			return ret;
 		}
 	}
@@ -806,7 +804,6 @@ static void zs_shutdown(struct uart_port *uport)
 	struct zs_port *zport = to_zport(uport);
 	struct zs_scc *scc = zport->scc;
 	unsigned long flags;
-	int irq_guard;
 
 	spin_lock_irqsave(&scc->zlock, flags);
 
@@ -816,8 +813,7 @@ static void zs_shutdown(struct uart_port *uport)
 
 	spin_unlock_irqrestore(&scc->zlock, flags);
 
-	irq_guard = atomic_add_return(-1, &scc->irq_guard);
-	if (!irq_guard)
+	if (refcount_dec_and_test(&scc->irq_guard))
 		free_irq(zport->port.irq, scc);
 }
 
drivers/tty/serial/zs.h +1 -1
index 26ef8eafa1c1..bd97b73d7e16 100644
@@ -40,7 +40,7 @@ struct zs_port {
 struct zs_scc {
 	struct zs_port	zport[2];
 	spinlock_t	zlock;
-	atomic_t	irq_guard;
+	refcount_t	irq_guard;
 	int		initialised;
 };
 

--
2.34.1

#1 lore raw
Quoted 37 lines · Deepak R Varma

The refcount_* APIs are designed to address known issues with the atomic_t APIs for reference counting. They provide following distinct advantages:
- protect the reference counters from overflow/underflow
- avoid use-after-free errors
- provide improved memory ordering guarantee schemes
- neater and safer.
Hence, replace the atomic_* APIs by their equivalent refcount_t API functions.

This patch proposal address the following warnings generated by the atomic_as_refcounter.cocci coccinelle script atomic_add_return(-1, ...)

---

Changes in v2:
1. Separate the combined change into one variable per patch as suggested by [email protected]
2. There was additional feedback on validating the change as it appeared to modify the existing logic. However, I found that the logic does not change with the proposed refcount_* APIs used in this change. Hence that feedback is not applied in this version.

Please Note:
The patch is compile tested using dec_station.defconfig for MIPS architecture.

 drivers/tty/serial/zs.c | 14 +++++---------
 drivers/tty/serial/zs.h |  2 +-
 2 files changed, 6 insertions(+), 10 deletions(-)
drivers/tty/serial/zs.c +0 -0
index 730c648e32ff..6be9933eff5c 100644

drivers/tty/serial/zs.c static int zs_startup(struct uart_port *uport) · line 759

Quoted 10 lines
drivers/tty/serial/zs.c +2 -3
@@ -753,17 +753,15 @@ static int zs_startup(struct uart_port *uport)
 	struct zs_port *zport = to_zport(uport);
 	struct zs_scc *scc = zport->scc;
 	unsigned long flags;
-	int irq_guard;
 	int ret;
 
-	irq_guard = atomic_add_return(1, &scc->irq_guard);
-	if (irq_guard == 1) {
+	refcount_inc(&scc->irq_guard);
+	if (refcount_read(&scc->irq_guard) == 1) {

This conversion is wrong :(