diff mbox

[07/25] serial: sh-sci: Convert from clk_get() to devm_clk_get()

Message ID 1447958344-836-8-git-send-email-geert+renesas@glider.be (mailing list archive)
State Superseded
Delegated to: Geert Uytterhoeven
Headers show

Commit Message

Geert Uytterhoeven Nov. 19, 2015, 6:38 p.m. UTC
Transfer clock cleanup handling to the core device management code.

Signed-off-by: Geert Uytterhoeven <geert+renesas@glider.be>
---
 drivers/tty/serial/sh-sci.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)

Comments

Laurent Pinchart Nov. 19, 2015, 8:38 p.m. UTC | #1
Hi Geert,

Thank for the patch.

On Thursday 19 November 2015 19:38:46 Geert Uytterhoeven wrote:
> Transfer clock cleanup handling to the core device management code.
> 
> Signed-off-by: Geert Uytterhoeven <geert+renesas@glider.be>
> ---
>  drivers/tty/serial/sh-sci.c | 8 +++-----
>  1 file changed, 3 insertions(+), 5 deletions(-)
> 
> diff --git a/drivers/tty/serial/sh-sci.c b/drivers/tty/serial/sh-sci.c
> index cba51da604253db6..9442961a198378c7 100644
> --- a/drivers/tty/serial/sh-sci.c
> +++ b/drivers/tty/serial/sh-sci.c
> @@ -2216,7 +2216,7 @@ static struct uart_ops sci_uart_ops = {
>  static int sci_init_clocks(struct sci_port *sci_port, struct device *dev)
>  {
>  	/* Get the SCI functional clock. It's called "fck" on ARM. */
> -	sci_port->fclk = clk_get(dev, "fck");
> +	sci_port->fclk = devm_clk_get(dev, "fck");

Have you tested what happens if you unbind the device from the driver while 
userspace has the serial port open ?

>  	if (PTR_ERR(sci_port->fclk) == -EPROBE_DEFER)
>  		return -EPROBE_DEFER;
>  	if (!IS_ERR(sci_port->fclk))
> @@ -2226,7 +2226,7 @@ static int sci_init_clocks(struct sci_port *sci_port,
> struct device *dev) * But it used to be called "sci_ick", and we need to
> maintain DT * backward compatibility.
>  	 */
> -	sci_port->fclk = clk_get(dev, "sci_ick");
> +	sci_port->fclk = devm_clk_get(dev, "sci_ick");
>  	if (PTR_ERR(sci_port->fclk) == -EPROBE_DEFER)
>  		return -EPROBE_DEFER;
>  	if (!IS_ERR(sci_port->fclk))
> @@ -2236,7 +2236,7 @@ static int sci_init_clocks(struct sci_port *sci_port,
> struct device *dev) * Not all SH platforms declare a clock lookup entry for
> SCI devices, * in which case we need to get the global "peripheral_clk"
> clock. */
> -	sci_port->fclk = clk_get(dev, "peripheral_clk");
> +	sci_port->fclk = devm_clk_get(dev, "peripheral_clk");
>  	if (!IS_ERR(sci_port->fclk))
>  		return 0;
> 
> @@ -2395,8 +2395,6 @@ static int sci_init_single(struct platform_device
> *dev,
> 
>  static void sci_cleanup_single(struct sci_port *port)
>  {
> -	clk_put(port->fclk);
> -
>  	pm_runtime_disable(port->port.dev);
>  }
Geert Uytterhoeven Dec. 10, 2015, 12:30 p.m. UTC | #2
Hi Laurent,

On Thu, Nov 19, 2015 at 9:38 PM, Laurent Pinchart
<laurent.pinchart@ideasonboard.com> wrote:
> On Thursday 19 November 2015 19:38:46 Geert Uytterhoeven wrote:
>> Transfer clock cleanup handling to the core device management code.
>>
>> Signed-off-by: Geert Uytterhoeven <geert+renesas@glider.be>
>> ---
>>  drivers/tty/serial/sh-sci.c | 8 +++-----
>>  1 file changed, 3 insertions(+), 5 deletions(-)
>>
>> diff --git a/drivers/tty/serial/sh-sci.c b/drivers/tty/serial/sh-sci.c
>> index cba51da604253db6..9442961a198378c7 100644
>> --- a/drivers/tty/serial/sh-sci.c
>> +++ b/drivers/tty/serial/sh-sci.c
>> @@ -2216,7 +2216,7 @@ static struct uart_ops sci_uart_ops = {
>>  static int sci_init_clocks(struct sci_port *sci_port, struct device *dev)
>>  {
>>       /* Get the SCI functional clock. It's called "fck" on ARM. */
>> -     sci_port->fclk = clk_get(dev, "fck");
>> +     sci_port->fclk = devm_clk_get(dev, "fck");
>
> Have you tested what happens if you unbind the device from the driver while
> userspace has the serial port open ?

Yes I have. And I didn't notice any user-visible behavioral differences.

There is a small ordering difference, though: with clk_get() and clk_put(), the
explicit clk_put() is done before removing the device from its PM Domain
(which involces another call to clk_put() on the module clock):

    device_release_driver
        __device_release_driver
            platform_drv_remove
                sci_remove
                    sci_cleanup_single
                        __clk_put
                genpd_dev_pm_detach
                    pm_genpd_remove_device
                        pm_clk_destroy
                            __pm_clk_remove
                                __clk_put

When using devm_clk_get(), the managed cleanup is done after the device has
been removed from its PM Domain:

    device_release_driver
        __device_release_driver
            platform_drv_remove
                genpd_dev_pm_detach
                    pm_genpd_remove_device
                        pm_clk_destroy
                            __pm_clk_remove
                                __clk_put
            release_nodes
                __clk_put

This shouldn't make a difference, and applies to all other drivers using
devm_*(), and devices that are part of a PM Domain.

Gr{oetje,eeting}s,

                        Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds
--
To unsubscribe from this list: send the line "unsubscribe linux-sh" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Laurent Pinchart Dec. 13, 2015, 7:39 p.m. UTC | #3
Hi Geert,

On Thursday 10 December 2015 13:30:18 Geert Uytterhoeven wrote:
> On Thu, Nov 19, 2015 at 9:38 PM, Laurent Pinchart wrote:
> > On Thursday 19 November 2015 19:38:46 Geert Uytterhoeven wrote:
> >> Transfer clock cleanup handling to the core device management code.
> >> 
> >> Signed-off-by: Geert Uytterhoeven <geert+renesas@glider.be>
> >> ---
> >> 
> >>  drivers/tty/serial/sh-sci.c | 8 +++-----
> >>  1 file changed, 3 insertions(+), 5 deletions(-)
> >> 
> >> diff --git a/drivers/tty/serial/sh-sci.c b/drivers/tty/serial/sh-sci.c
> >> index cba51da604253db6..9442961a198378c7 100644
> >> --- a/drivers/tty/serial/sh-sci.c
> >> +++ b/drivers/tty/serial/sh-sci.c
> >> @@ -2216,7 +2216,7 @@ static struct uart_ops sci_uart_ops = {
> >> 
> >>  static int sci_init_clocks(struct sci_port *sci_port, struct device
> >>  *dev)
> >>  {
> >>  
> >>       /* Get the SCI functional clock. It's called "fck" on ARM. */
> >> 
> >> -     sci_port->fclk = clk_get(dev, "fck");
> >> +     sci_port->fclk = devm_clk_get(dev, "fck");
> > 
> > Have you tested what happens if you unbind the device from the driver
> > while userspace has the serial port open ?
> 
> Yes I have. And I didn't notice any user-visible behavioral differences.
> 
> There is a small ordering difference, though: with clk_get() and clk_put(),
> the explicit clk_put() is done before removing the device from its PM
> Domain (which involces another call to clk_put() on the module clock):
> 
>     device_release_driver
>         __device_release_driver
>             platform_drv_remove
>                 sci_remove
>                     sci_cleanup_single
>                         __clk_put
>                 genpd_dev_pm_detach
>                     pm_genpd_remove_device
>                         pm_clk_destroy
>                             __pm_clk_remove
>                                 __clk_put
> 
> When using devm_clk_get(), the managed cleanup is done after the device has
> been removed from its PM Domain:
> 
>     device_release_driver
>         __device_release_driver
>             platform_drv_remove
>                 genpd_dev_pm_detach
>                     pm_genpd_remove_device
>                         pm_clk_destroy
>                             __pm_clk_remove
>                                 __clk_put
>             release_nodes
>                 __clk_put
> 
> This shouldn't make a difference, and applies to all other drivers using
> devm_*(), and devices that are part of a PM Domain.

I was more concerned about the clk_disable_unprepare() calls. The function is 
called through the uart pm handler which is called when closing the uart 
(uart_close()). It's a bit difficult to follow the dynamic code paths as I'm 
not very familiar with the framework, but I was concerned that the would end 
up calling clk_disable_unprepare() on a clock for which we have no reference 
anymore. What prevents clk_disable_unprepare() from being called in the close 
path after the device has been unbound ? And what ensures that the clocks get 
unprepared when unbinding the device if the port is held open by a userspace 
process ?
Geert Uytterhoeven Dec. 14, 2015, 10:34 a.m. UTC | #4
Hi Laurent,

On Sun, Dec 13, 2015 at 8:39 PM, Laurent Pinchart
<laurent.pinchart@ideasonboard.com> wrote:
> On Thursday 10 December 2015 13:30:18 Geert Uytterhoeven wrote:
>> On Thu, Nov 19, 2015 at 9:38 PM, Laurent Pinchart wrote:
>> > On Thursday 19 November 2015 19:38:46 Geert Uytterhoeven wrote:
>> >> Transfer clock cleanup handling to the core device management code.
>> >>
>> >> Signed-off-by: Geert Uytterhoeven <geert+renesas@glider.be>
>> >> ---
>> >>
>> >>  drivers/tty/serial/sh-sci.c | 8 +++-----
>> >>  1 file changed, 3 insertions(+), 5 deletions(-)
>> >>
>> >> diff --git a/drivers/tty/serial/sh-sci.c b/drivers/tty/serial/sh-sci.c
>> >> index cba51da604253db6..9442961a198378c7 100644
>> >> --- a/drivers/tty/serial/sh-sci.c
>> >> +++ b/drivers/tty/serial/sh-sci.c
>> >> @@ -2216,7 +2216,7 @@ static struct uart_ops sci_uart_ops = {
>> >>
>> >>  static int sci_init_clocks(struct sci_port *sci_port, struct device
>> >>  *dev)
>> >>  {
>> >>
>> >>       /* Get the SCI functional clock. It's called "fck" on ARM. */
>> >>
>> >> -     sci_port->fclk = clk_get(dev, "fck");
>> >> +     sci_port->fclk = devm_clk_get(dev, "fck");
>> >
>> > Have you tested what happens if you unbind the device from the driver
>> > while userspace has the serial port open ?
>>
>> Yes I have. And I didn't notice any user-visible behavioral differences.
>>
>> There is a small ordering difference, though: with clk_get() and clk_put(),
>> the explicit clk_put() is done before removing the device from its PM
>> Domain (which involces another call to clk_put() on the module clock):
>>
>>     device_release_driver
>>         __device_release_driver
>>             platform_drv_remove
>>                 sci_remove
>>                     sci_cleanup_single
>>                         __clk_put
>>                 genpd_dev_pm_detach
>>                     pm_genpd_remove_device
>>                         pm_clk_destroy
>>                             __pm_clk_remove
>>                                 __clk_put
>>
>> When using devm_clk_get(), the managed cleanup is done after the device has
>> been removed from its PM Domain:
>>
>>     device_release_driver
>>         __device_release_driver
>>             platform_drv_remove
>>                 genpd_dev_pm_detach
>>                     pm_genpd_remove_device
>>                         pm_clk_destroy
>>                             __pm_clk_remove
>>                                 __clk_put
>>             release_nodes
>>                 __clk_put
>>
>> This shouldn't make a difference, and applies to all other drivers using
>> devm_*(), and devices that are part of a PM Domain.
>
> I was more concerned about the clk_disable_unprepare() calls. The function is
> called through the uart pm handler which is called when closing the uart
> (uart_close()). It's a bit difficult to follow the dynamic code paths as I'm
> not very familiar with the framework, but I was concerned that the would end
> up calling clk_disable_unprepare() on a clock for which we have no reference
> anymore. What prevents clk_disable_unprepare() from being called in the close
> path after the device has been unbound ? And what ensures that the clocks get
> unprepared when unbinding the device if the port is held open by a userspace
> process ?

Both questions are answered by uart_hangup():
  - uart_hangup() sets port->count to zero, hence uart_close() returns early
    (second return statement), long before it would call the uart pm handler.
  - uart_hangup() calls uart_change_pm(state, UART_PM_STATE_OFF);

Gr{oetje,eeting}s,

                        Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds
--
To unsubscribe from this list: send the line "unsubscribe linux-sh" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Laurent Pinchart Dec. 14, 2015, 3:47 p.m. UTC | #5
Hi Geert,

On Monday 14 December 2015 11:34:11 Geert Uytterhoeven wrote:
> On Sun, Dec 13, 2015 at 8:39 PM, Laurent Pinchart wrote:
> > On Thursday 10 December 2015 13:30:18 Geert Uytterhoeven wrote:
> >> On Thu, Nov 19, 2015 at 9:38 PM, Laurent Pinchart wrote:
> >> > On Thursday 19 November 2015 19:38:46 Geert Uytterhoeven wrote:
> >> >> Transfer clock cleanup handling to the core device management code.
> >> >> 
> >> >> Signed-off-by: Geert Uytterhoeven <geert+renesas@glider.be>
> >> >> ---
> >> >> 
> >> >>  drivers/tty/serial/sh-sci.c | 8 +++-----
> >> >>  1 file changed, 3 insertions(+), 5 deletions(-)
> >> >> 
> >> >> diff --git a/drivers/tty/serial/sh-sci.c b/drivers/tty/serial/sh-sci.c
> >> >> index cba51da604253db6..9442961a198378c7 100644
> >> >> --- a/drivers/tty/serial/sh-sci.c
> >> >> +++ b/drivers/tty/serial/sh-sci.c
> >> >> @@ -2216,7 +2216,7 @@ static struct uart_ops sci_uart_ops = {
> >> >> 
> >> >>  static int sci_init_clocks(struct sci_port *sci_port, struct device
> >> >>  *dev)
> >> >>  {
> >> >>       /* Get the SCI functional clock. It's called "fck" on ARM. */
> >> >> -     sci_port->fclk = clk_get(dev, "fck");
> >> >> +     sci_port->fclk = devm_clk_get(dev, "fck");
> >> > 
> >> > Have you tested what happens if you unbind the device from the driver
> >> > while userspace has the serial port open ?
> >> 
> >> Yes I have. And I didn't notice any user-visible behavioral differences.
> >> 
> >> There is a small ordering difference, though: with clk_get() and
> >> clk_put(), the explicit clk_put() is done before removing the device from
> >> its PM
> >> 
> >> Domain (which involces another call to clk_put() on the module clock):
> >>     device_release_driver
> >>         __device_release_driver
> >>             platform_drv_remove
> >>                 sci_remove
> >>                     sci_cleanup_single
> >>                         __clk_put
> >>                 genpd_dev_pm_detach
> >>                     pm_genpd_remove_device
> >>                         pm_clk_destroy
> >>                             __pm_clk_remove
> >>                                 __clk_put
> >> 
> >> When using devm_clk_get(), the managed cleanup is done after the device
> >> has been removed from its PM Domain:
> >>     device_release_driver
> >>         __device_release_driver
> >>             platform_drv_remove
> >>                 genpd_dev_pm_detach
> >>                     pm_genpd_remove_device
> >>                         pm_clk_destroy
> >>                             __pm_clk_remove
> >>                                 __clk_put
> >>             release_nodes
> >>                 __clk_put
> >> 
> >> This shouldn't make a difference, and applies to all other drivers using
> >> devm_*(), and devices that are part of a PM Domain.
> > 
> > I was more concerned about the clk_disable_unprepare() calls. The function
> > is called through the uart pm handler which is called when closing the
> > uart (uart_close()). It's a bit difficult to follow the dynamic code
> > paths as I'm not very familiar with the framework, but I was concerned
> > that the would end up calling clk_disable_unprepare() on a clock for
> > which we have no reference anymore. What prevents clk_disable_unprepare()
> > from being called in the close path after the device has been unbound ?
> > And what ensures that the clocks get unprepared when unbinding the device
> > if the port is held open by a userspace process ?
> 
> Both questions are answered by uart_hangup():
>   - uart_hangup() sets port->count to zero, hence uart_close() returns early
> (second return statement), long before it would call the uart pm handler.
> - uart_hangup() calls uart_change_pm(state, UART_PM_STATE_OFF);

Thank you for making me slightly wiser :-)
diff mbox

Patch

diff --git a/drivers/tty/serial/sh-sci.c b/drivers/tty/serial/sh-sci.c
index cba51da604253db6..9442961a198378c7 100644
--- a/drivers/tty/serial/sh-sci.c
+++ b/drivers/tty/serial/sh-sci.c
@@ -2216,7 +2216,7 @@  static struct uart_ops sci_uart_ops = {
 static int sci_init_clocks(struct sci_port *sci_port, struct device *dev)
 {
 	/* Get the SCI functional clock. It's called "fck" on ARM. */
-	sci_port->fclk = clk_get(dev, "fck");
+	sci_port->fclk = devm_clk_get(dev, "fck");
 	if (PTR_ERR(sci_port->fclk) == -EPROBE_DEFER)
 		return -EPROBE_DEFER;
 	if (!IS_ERR(sci_port->fclk))
@@ -2226,7 +2226,7 @@  static int sci_init_clocks(struct sci_port *sci_port, struct device *dev)
 	 * But it used to be called "sci_ick", and we need to maintain DT
 	 * backward compatibility.
 	 */
-	sci_port->fclk = clk_get(dev, "sci_ick");
+	sci_port->fclk = devm_clk_get(dev, "sci_ick");
 	if (PTR_ERR(sci_port->fclk) == -EPROBE_DEFER)
 		return -EPROBE_DEFER;
 	if (!IS_ERR(sci_port->fclk))
@@ -2236,7 +2236,7 @@  static int sci_init_clocks(struct sci_port *sci_port, struct device *dev)
 	 * Not all SH platforms declare a clock lookup entry for SCI devices,
 	 * in which case we need to get the global "peripheral_clk" clock.
 	 */
-	sci_port->fclk = clk_get(dev, "peripheral_clk");
+	sci_port->fclk = devm_clk_get(dev, "peripheral_clk");
 	if (!IS_ERR(sci_port->fclk))
 		return 0;
 
@@ -2395,8 +2395,6 @@  static int sci_init_single(struct platform_device *dev,
 
 static void sci_cleanup_single(struct sci_port *port)
 {
-	clk_put(port->fclk);
-
 	pm_runtime_disable(port->port.dev);
 }