diff mbox series

[v2] tty: serial: dz: convert atomic_* to refcount_* APIs

Message ID Y6at4MyYmIuy2Ctc@qemulion
State New
Headers show
Series [v2] tty: serial: dz: convert atomic_* to refcount_* APIs | expand

Commit Message

Deepak R Varma Dec. 24, 2022, 7:44 a.m. UTC
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, ...)

Signed-off-by: Deepak R Varma <drv@mailo.com>
---

Changes in v2:
   1. Separate the combined change into one variable per patch as
      suggested by gregkh@linuxfoundation.org
   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.


 drivers/tty/serial/dz.c | 23 +++++++++--------------
 1 file changed, 9 insertions(+), 14 deletions(-)

--
2.34.1
diff mbox series

Patch

diff --git a/drivers/tty/serial/dz.c b/drivers/tty/serial/dz.c
index 6b7ed7f2f3ca..b70edc248f8b 100644
--- a/drivers/tty/serial/dz.c
+++ b/drivers/tty/serial/dz.c
@@ -47,6 +47,7 @@ 
 #include <linux/tty_flip.h>

 #include <linux/atomic.h>
+#include <linux/refcount.h>
 #include <linux/io.h>
 #include <asm/bootinfo.h>

@@ -75,7 +76,7 @@  struct dz_port {

 struct dz_mux {
 	struct dz_port		dport[DZ_NB_PORT];
-	atomic_t		map_guard;
+	refcount_t		map_guard;
 	atomic_t		irq_guard;
 	int			initialised;
 };
@@ -662,13 +663,11 @@  static const char *dz_type(struct uart_port *uport)
 static void dz_release_port(struct uart_port *uport)
 {
 	struct dz_mux *mux = to_dport(uport)->mux;
-	int map_guard;

 	iounmap(uport->membase);
 	uport->membase = NULL;

-	map_guard = atomic_add_return(-1, &mux->map_guard);
-	if (!map_guard)
+	if (refcount_dec_and_test(&mux->map_guard))
 		release_mem_region(uport->mapbase, dec_kn_slot_size);
 }

@@ -687,23 +686,19 @@  static int dz_map_port(struct uart_port *uport)
 static int dz_request_port(struct uart_port *uport)
 {
 	struct dz_mux *mux = to_dport(uport)->mux;
-	int map_guard;
 	int ret;

-	map_guard = atomic_add_return(1, &mux->map_guard);
-	if (map_guard == 1) {
-		if (!request_mem_region(uport->mapbase, dec_kn_slot_size,
-					"dz")) {
-			atomic_add(-1, &mux->map_guard);
-			printk(KERN_ERR
-			       "dz: Unable to reserve MMIO resource\n");
+	refcount_inc(&mux->map_guard);
+	if (refcount_read(&mux->map_guard) == 1) {
+		if (!request_mem_region(uport->mapbase, dec_kn_slot_size, "dz")) {
+			refcount_dec(&mux->map_guard);
+			printk(KERN_ERR "dz: Unable to reserve MMIO resource\n");
 			return -EBUSY;
 		}
 	}
 	ret = dz_map_port(uport);
 	if (ret) {
-		map_guard = atomic_add_return(-1, &mux->map_guard);
-		if (!map_guard)
+		if (refcount_dec_and_test(&mux->map_guard))
 			release_mem_region(uport->mapbase, dec_kn_slot_size);
 		return ret;
 	}