From: Peter Hurley Date: Mon, 10 Feb 2014 01:59:04 +0000 (-0500) Subject: tty: Fix ref counting for port krefs X-Git-Tag: v5.15~17544^2~11^2~7^2~45^2~232 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=c0fdfb80382e4901473ce0e31d1e7833c1d297be;p=platform%2Fkernel%2Flinux-starfive.git tty: Fix ref counting for port krefs The tty core supports two models for handling tty_port lifetimes; the tty_port can use the kref supplied by tty_port (which will automatically destruct the tty_port when the ref count drops to zero) or it can destruct the tty_port manually. For tty drivers that choose to use the port kref to manage the tty_port lifetime, it is not possible to safely acquire a port reference conditionally. If the last reference is released after evaluating the condition but before acquiring the reference, a bogus reference will be held while the tty_port destruction commences. Rather, only acquire a port reference if the ref count is non-zero and allow the caller to distinguish if a reference has successfully been acquired. Cc: Jiri Slaby Signed-off-by: Peter Hurley Acked-by: Greg Kroah-Hartman Tested-By: Alexander Holler Signed-off-by: Marcel Holtmann --- diff --git a/include/linux/tty.h b/include/linux/tty.h index 90b4fdc..4781d7b 100644 --- a/include/linux/tty.h +++ b/include/linux/tty.h @@ -518,9 +518,9 @@ extern void tty_port_put(struct tty_port *port); static inline struct tty_port *tty_port_get(struct tty_port *port) { - if (port) - kref_get(&port->kref); - return port; + if (port && kref_get_unless_zero(&port->kref)) + return port; + return NULL; } /* If the cts flow control is enabled, return true. */