tty: serial: convert atomic_* to refcount_* APIs
Subsystems (3)
PATCH 1/1 tty: serial: convert atomic_* to refcount_* APIs
The refcount_* APIs are designed to address known issues with the atomic_t APIs for reference counting. They protect the reference counters from overflow/underflow, use-after-free errors, provide improved memory ordering guarantee schemes, are 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, ...)
---
Note: The patch is compile tested using dec_station.defconfig for
MIPS architecture.
drivers/tty/serial/dz.c | 34 +++++++++++++--------------------- drivers/tty/serial/zs.c | 11 ++++------- drivers/tty/serial/zs.h | 2 +- 3 files changed, 18 insertions(+), 29 deletions(-)
drivers/tty/serial/dz.c +13 -21
@@ -46,7 +46,7 @@#include <linux/tty.h> #include <linux/tty_flip.h> -#include <linux/atomic.h> +#include <linux/refcount.h> #include <linux/io.h> #include <asm/bootinfo.h>
@@ -75,8 +75,8 @@ struct dz_port {struct dz_mux {struct dz_port dport[DZ_NB_PORT ]; -atomic_t map_guard; -atomic_t irq_guard; +refcount_t map_guard; +refcount_t irq_guard;int initialised; };
@@ -399,18 +399,17 @@ static int dz_startup(struct uart_port *uport)struct dz_port *dport =to_dport (uport);struct dz_mux *mux = dport->mux;unsigned long flags; -int irq_guard;int ret;u16 tmp; - irq_guard =atomic_add_return (1 , &mux->irq_guard); -if (irq_guard !=1 ) +refcount_inc (&mux->irq_guard); +if (refcount_read (&mux->irq_guard) !=1 )return 0 ; ret =request_irq (dport->port.irq, dz_interrupt,IRQF_SHARED ,"dz" , mux);if (ret) { -atomic_add (-1 , &mux->irq_guard); +refcount_dec (&mux->irq_guard);printk (KERN_ERR "dz: Cannot get IRQ %d!\n" , dport->port.irq);return ret; }
@@ -440,15 +439,13 @@ static void dz_shutdown(struct uart_port *uport)struct dz_port *dport =to_dport (uport);struct dz_mux *mux = dport->mux;unsigned long flags; -int irq_guard;u16 tmp;spin_lock_irqsave (&dport->port.lock, flags);dz_stop_tx (&dport->port);spin_unlock_irqrestore (&dport->port.lock, flags); - irq_guard =atomic_add_return (-1 , &mux->irq_guard); -if (!irq_guard) { +if (refcount_dec_and_test (&mux->irq_guard)) {/* Disable interrupts. */ tmp =dz_in (dport,DZ_CSR ); tmp &= ~(DZ_RIE |DZ_TIE );
@@ -662,13 +659,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,14 +682,12 @@ 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); +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 ;
@@ -702,8 +695,7 @@ static int dz_request_port(struct uart_port *uport) } 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; }
drivers/tty/serial/zs.c +4 -7
@@ -753,15 +753,14 @@ 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); +refcount_dec (&scc->irq_guard);printk (KERN_ERR "zs: can't get irq %d\n" , zport->port.irq);return ret;
@@ -806,7 +805,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 +814,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
@@ -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
Greg Kroah-Hartman
maintainerQuoted 9 lines · Deepak R Varma
The refcount_* APIs are designed to address known issues with the
atomic_tAPIs for reference counting. They protect the reference counters from overflow/underflow, use-after-free errors, provide improved memory ordering guarantee schemes, are neater and safer. Hence, replace the atomic_* APIs by their equivalentrefcount_tAPI functions.This patch proposal address the following warnings generated by the
atomic_as_refcounter.coccicoccinelle script
Quoted 7 lines
atomic_add_return(-1, ...)Signed-off-by Deepak R Varma <[email protected]>---
Note: The patch is compile tested using dec_station.defconfig for
MIPS architecture.
Do you have this hardware? If not, please just do one-variable-at-a-time so that if there are real problems, we can revert the offending change easier. And it makes it simpler to review.
But, are you sure this is correct:
drivers/tty/serial/dz.c static int dz_startup(struct uart_port *uport) · line 406
Quoted 4 lines
drivers/tty/serial/dz.c +2
-2- irq_guard =atomic_add_return (1 , &mux->irq_guard); -if (irq_guard !=1 ) +refcount_inc (&mux->irq_guard); +if (refcount_read (&mux->irq_guard) !=1 )
That is now different logic than before, why? Are you sure this is ok?
I stopped reviewing here...
thanks,
greg k-h
Quoted 13 lines · Greg Kroah-Hartman
Quoted 12 lines · Deepak R Varma
The refcount_* APIs are designed to address known issues with the
atomic_tAPIs for reference counting. They protect the reference counters from overflow/underflow, use-after-free errors, provide improved memory ordering guarantee schemes, are neater and safer. Hence, replace the atomic_* APIs by their equivalentrefcount_tAPI functions.This patch proposal address the following warnings generated by the
atomic_as_refcounter.coccicoccinelle scriptatomic_add_return(-1, ...)
Quoted 8 lines
Quoted 4 lines
Signed-off-by Deepak R Varma <[email protected]>---
Note: The patch is compile tested using dec_station.defconfig for
MIPS architecture.Do you have this hardware? If not, please just do one-variable-at-a-time so that if there are real problems, we can revert the offending change easier. And it makes it simpler to review.
Hi Greg,
I do not have the hardware. I will make a one variable per patch change as suggest and send in a patch set.
Quoted 9 lines
But, are you sure this is correct:
drivers/tty/serial/dz.c static int dz_startup(struct uart_port *uport) · line 406
Quoted 4 lines
drivers/tty/serial/dz.c +2
-2- irq_guard =atomic_add_return (1 , &mux->irq_guard); -if (irq_guard !=1 ) +refcount_inc (&mux->irq_guard); +if (refcount_read (&mux->irq_guard) !=1 )That is now different logic than before, why? Are you sure this is ok?
This is not correct. I read it wrong. I will correct this in the patch set.
Quoted 2 lines
I stopped reviewing here...
Thanks. I will review the rest of the changes per your advise and make the corrections.
regards,
./drv
Quoted 4 lines
thanks,
greg k-h