lkml.org 
[lkml]   [2022]   [Dec]   [24]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
Patch in this message
/
Date
From
Subject[PATCH v2] tty: serial: dz: convert atomic_* to refcount_* APIs
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(-)

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;
}
--
2.34.1


\
 
 \ /
  Last update: 2023-03-26 23:19    [W:0.020 / U:0.132 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site