All of lore.kernel.org
 help / color / mirror / Atom feed
From: Lucas Stach <l.stach@pengutronix.de>
To: Maciej Purski <m.purski@samsung.com>,
	Mark Brown <broonie@kernel.org>, Tony Lindgren <tony@atomide.com>
Cc: linux-kernel@vger.kernel.org, Carlos Hernandez <ceh@ti.com>,
	linux-omap@vger.kernel.org, linux-arm-kernel@lists.infradead.org,
	Marek Szyprowski <m.szyprowski@samsung.com>
Subject: Re: [PATCH 5/7] regulator: core: Lock dependent regulators
Date: Mon, 04 Jun 2018 16:20:47 +0200	[thread overview]
Message-ID: <1528122047.2728.10.camel@pengutronix.de> (raw)
In-Reply-To: <1528120764-14316-6-git-send-email-m.purski@samsung.com>

Hi Maciej,

Am Montag, den 04.06.2018, 15:59 +0200 schrieb Maciej Purski:
> Implementing coupled regulators adds a new dependency between
> regulators. Therefore, the current locking model should be changed.
> Coupled regulators should be locked with regulator's supplies at the
> same time.
> 
> Add new function regulator_lock_dependent(), which locks all regulators
> related with the one, that is being changed.

Sort of high level comment, but this doesn't look right: With dependent
regulators you don't strictly lock the regulators in the direction of
the tree root, but also siblings at the same level. This is prone with
deadlocks, as you can't control the order of the regulator locks being
taken by different tasks. This really needs a ww_mutex to be
implemented in a robust way.

Regards,
Lucas

> Signed-off-by: Maciej Purski <m.purski@samsung.com>
> ---
>  drivers/regulator/core.c | 75 +++++++++++++++++++++++++++++++++---------------
>  1 file changed, 52 insertions(+), 23 deletions(-)
> 
> diff --git a/drivers/regulator/core.c b/drivers/regulator/core.c
> index 0b366c5..7c57268 100644
> --- a/drivers/regulator/core.c
> +++ b/drivers/regulator/core.c
> @@ -201,38 +201,67 @@ static void regulator_unlock(struct regulator_dev *rdev)
> >  	}
>  }
>  
> -/**
> - * regulator_lock_supply - lock a regulator and its supplies
> - * @rdev:         regulator source
> - */
> -static void regulator_lock_supply(struct regulator_dev *rdev)
> +static int regulator_lock_recursive(struct regulator_dev *rdev,
> > +				    unsigned int subclass)
>  {
> > +	struct regulator_dev *c_rdev;
> >  	int i;
>  
> > -	for (i = 0; rdev; rdev = rdev_get_supply(rdev), i++)
> > -		regulator_lock_nested(rdev, i);
> > +	for (i = 0; i < rdev->coupling_desc.n_coupled; i++) {
> > +		c_rdev = rdev->coupling_desc.coupled_rdevs[i];
> +
> > +		if (!c_rdev)
> > +			continue;
> +
> > +		regulator_lock_nested(c_rdev, subclass++);
> +
> > +		if (c_rdev->supply)
> > +			subclass =
> > +				regulator_lock_recursive(c_rdev->supply->rdev,
> > +							 subclass);
> > +	}
> +
> > +	return subclass;
>  }
>  
>  /**
> - * regulator_unlock_supply - unlock a regulator and its supplies
> - * @rdev:         regulator source
> + * regulator_unlock_dependent - unlock regulator's suppliers and coupled
> > + *				regulators
> > + * @rdev:			regulator source
> + *
> + * Unlock all regulators related with rdev by coupling or suppling.
>   */
> -static void regulator_unlock_supply(struct regulator_dev *rdev)
> +static void regulator_unlock_dependent(struct regulator_dev *rdev)
>  {
> > -	struct regulator *supply;
> > +	struct regulator_dev *c_rdev;
> > +	int i;
>  
> > -	while (1) {
> > -		regulator_unlock(rdev);
> > -		supply = rdev->supply;
> > +	for (i = 0; i < rdev->coupling_desc.n_coupled; i++) {
> > +		c_rdev = rdev->coupling_desc.coupled_rdevs[i];
>  
> > -		if (!rdev->supply)
> > -			return;
> > +		if (!c_rdev)
> > +			continue;
> +
> > +		regulator_unlock(c_rdev);
>  
> > -		rdev = supply->rdev;
> > +		if (c_rdev->supply)
> > +			regulator_unlock_dependent(c_rdev->supply->rdev);
> >  	}
>  }
>  
>  /**
> + * regulator_lock_dependent - lock regulator's suppliers and coupled regulators
> > + * @rdev:			regulator source
> + *
> + * This function as a wrapper on regulator_lock_recursive(), which locks
> + * all regulators related with rdev by coupling or suppling.
> + */
> +static inline void regulator_lock_dependent(struct regulator_dev *rdev)
> +{
> > +	regulator_lock_recursive(rdev, 0);
> +}
> +
> +/**
>   * of_get_regulator - get a regulator device node based on supply name
>   * @dev: Device pointer for the consumer (of regulator) device
>   * @supply: regulator supply name
> @@ -3332,12 +3361,12 @@ int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
> >  	int ret = 0;
>  
> >  	pr_err("%s: %d\n", __func__, __LINE__);
> > -	regulator_lock_supply(regulator->rdev);
> > +	regulator_lock_dependent(regulator->rdev);
>  
> >  	ret = regulator_set_voltage_unlocked(regulator, min_uV, max_uV,
> >  					     PM_SUSPEND_ON);
>  
> > -	regulator_unlock_supply(regulator->rdev);
> > +	regulator_unlock_dependent(regulator->rdev);
>  
> >  	return ret;
>  }
> @@ -3415,12 +3444,12 @@ int regulator_set_suspend_voltage(struct regulator *regulator, int min_uV,
> >  	if (regulator_check_states(state) || state == PM_SUSPEND_ON)
> >  		return -EINVAL;
>  
> > -	regulator_lock_supply(regulator->rdev);
> > +	regulator_lock_dependent(regulator->rdev);
>  
> >  	ret = _regulator_set_suspend_voltage(regulator, min_uV,
> >  					     max_uV, state);
>  
> > -	regulator_unlock_supply(regulator->rdev);
> > +	regulator_unlock_dependent(regulator->rdev);
>  
> >  	return ret;
>  }
> @@ -3612,11 +3641,11 @@ int regulator_get_voltage(struct regulator *regulator)
>  {
> >  	int ret;
>  
> > -	regulator_lock_supply(regulator->rdev);
> > +	regulator_lock_dependent(regulator->rdev);
>  
> >  	ret = _regulator_get_voltage(regulator->rdev);
>  
> > -	regulator_unlock_supply(regulator->rdev);
> > +	regulator_unlock_dependent(regulator->rdev);
>  
> >  	return ret;
>  }

WARNING: multiple messages have this Message-ID (diff)
From: l.stach@pengutronix.de (Lucas Stach)
To: linux-arm-kernel@lists.infradead.org
Subject: [PATCH 5/7] regulator: core: Lock dependent regulators
Date: Mon, 04 Jun 2018 16:20:47 +0200	[thread overview]
Message-ID: <1528122047.2728.10.camel@pengutronix.de> (raw)
In-Reply-To: <1528120764-14316-6-git-send-email-m.purski@samsung.com>

Hi Maciej,

Am Montag, den 04.06.2018, 15:59 +0200 schrieb Maciej Purski:
> Implementing coupled regulators adds a new dependency between
> regulators. Therefore, the current locking model should be changed.
> Coupled regulators should be locked with regulator's supplies at the
> same time.
> 
> Add new function regulator_lock_dependent(), which locks all regulators
> related with the one, that is being changed.

Sort of high level comment, but this doesn't look right: With dependent
regulators you don't strictly lock the regulators in the direction of
the tree root, but also siblings at the same level. This is prone with
deadlocks, as you can't control the order of the regulator locks being
taken by different tasks. This really needs a ww_mutex to be
implemented in a robust way.

Regards,
Lucas

> Signed-off-by: Maciej Purski <m.purski@samsung.com>
> ---
> ?drivers/regulator/core.c | 75 +++++++++++++++++++++++++++++++++---------------
> ?1 file changed, 52 insertions(+), 23 deletions(-)
> 
> diff --git a/drivers/regulator/core.c b/drivers/regulator/core.c
> index 0b366c5..7c57268 100644
> --- a/drivers/regulator/core.c
> +++ b/drivers/regulator/core.c
> @@ -201,38 +201,67 @@ static void regulator_unlock(struct regulator_dev *rdev)
> > ?	}
> ?}
> ?
> -/**
> - * regulator_lock_supply - lock a regulator and its supplies
> - * @rdev:?????????regulator source
> - */
> -static void regulator_lock_supply(struct regulator_dev *rdev)
> +static int regulator_lock_recursive(struct regulator_dev *rdev,
> > +				????unsigned int subclass)
> ?{
> > +	struct regulator_dev *c_rdev;
> > ?	int i;
> ?
> > -	for (i = 0; rdev; rdev = rdev_get_supply(rdev), i++)
> > -		regulator_lock_nested(rdev, i);
> > +	for (i = 0; i < rdev->coupling_desc.n_coupled; i++) {
> > +		c_rdev = rdev->coupling_desc.coupled_rdevs[i];
> +
> > +		if (!c_rdev)
> > +			continue;
> +
> > +		regulator_lock_nested(c_rdev, subclass++);
> +
> > +		if (c_rdev->supply)
> > +			subclass =
> > +				regulator_lock_recursive(c_rdev->supply->rdev,
> > +							?subclass);
> > +	}
> +
> > +	return subclass;
> ?}
> ?
> ?/**
> - * regulator_unlock_supply - unlock a regulator and its supplies
> - * @rdev:?????????regulator source
> + * regulator_unlock_dependent - unlock regulator's suppliers and coupled
> > + *				regulators
> > + * @rdev:			regulator source
> + *
> + * Unlock all regulators related with rdev by coupling or suppling.
> ? */
> -static void regulator_unlock_supply(struct regulator_dev *rdev)
> +static void regulator_unlock_dependent(struct regulator_dev *rdev)
> ?{
> > -	struct regulator *supply;
> > +	struct regulator_dev *c_rdev;
> > +	int i;
> ?
> > -	while (1) {
> > -		regulator_unlock(rdev);
> > -		supply = rdev->supply;
> > +	for (i = 0; i < rdev->coupling_desc.n_coupled; i++) {
> > +		c_rdev = rdev->coupling_desc.coupled_rdevs[i];
> ?
> > -		if (!rdev->supply)
> > -			return;
> > +		if (!c_rdev)
> > +			continue;
> +
> > +		regulator_unlock(c_rdev);
> ?
> > -		rdev = supply->rdev;
> > +		if (c_rdev->supply)
> > +			regulator_unlock_dependent(c_rdev->supply->rdev);
> > ?	}
> ?}
> ?
> ?/**
> + * regulator_lock_dependent - lock regulator's suppliers and coupled regulators
> > + * @rdev:			regulator source
> + *
> + * This function as a wrapper on regulator_lock_recursive(), which locks
> + * all regulators related with rdev by coupling or suppling.
> + */
> +static inline void regulator_lock_dependent(struct regulator_dev *rdev)
> +{
> > +	regulator_lock_recursive(rdev, 0);
> +}
> +
> +/**
> ? * of_get_regulator - get a regulator device node based on supply name
> ? * @dev: Device pointer for the consumer (of regulator) device
> ? * @supply: regulator supply name
> @@ -3332,12 +3361,12 @@ int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
> > ?	int ret = 0;
> ?
> > ?	pr_err("%s: %d\n", __func__, __LINE__);
> > -	regulator_lock_supply(regulator->rdev);
> > +	regulator_lock_dependent(regulator->rdev);
> ?
> > ?	ret = regulator_set_voltage_unlocked(regulator, min_uV, max_uV,
> > ?					?????PM_SUSPEND_ON);
> ?
> > -	regulator_unlock_supply(regulator->rdev);
> > +	regulator_unlock_dependent(regulator->rdev);
> ?
> > ?	return ret;
> ?}
> @@ -3415,12 +3444,12 @@ int regulator_set_suspend_voltage(struct regulator *regulator, int min_uV,
> > ?	if (regulator_check_states(state) || state == PM_SUSPEND_ON)
> > ?		return -EINVAL;
> ?
> > -	regulator_lock_supply(regulator->rdev);
> > +	regulator_lock_dependent(regulator->rdev);
> ?
> > ?	ret = _regulator_set_suspend_voltage(regulator, min_uV,
> > ?					?????max_uV, state);
> ?
> > -	regulator_unlock_supply(regulator->rdev);
> > +	regulator_unlock_dependent(regulator->rdev);
> ?
> > ?	return ret;
> ?}
> @@ -3612,11 +3641,11 @@ int regulator_get_voltage(struct regulator *regulator)
> ?{
> > ?	int ret;
> ?
> > -	regulator_lock_supply(regulator->rdev);
> > +	regulator_lock_dependent(regulator->rdev);
> ?
> > ?	ret = _regulator_get_voltage(regulator->rdev);
> ?
> > -	regulator_unlock_supply(regulator->rdev);
> > +	regulator_unlock_dependent(regulator->rdev);
> ?
> > ?	return ret;
> ?}

  reply	other threads:[~2018-06-04 14:20 UTC|newest]

Thread overview: 81+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-05-29 22:15 Regression in Linux next again Tony Lindgren
2018-05-29 22:15 ` Tony Lindgren
2018-05-30  9:13 ` Mark Brown
2018-05-30  9:13   ` Mark Brown
2018-05-30 14:03   ` Maciej Purski
2018-05-30 14:03     ` Maciej Purski
2018-05-30 14:33     ` Mark Brown
2018-05-30 14:33       ` Mark Brown
2018-05-30 14:45       ` Tony Lindgren
2018-05-30 14:45         ` Tony Lindgren
     [not found]         ` <CGME20180604135952eucas1p292f7bcec405e6a1a6261031df36cad32@eucas1p2.samsung.com>
2018-06-04 13:59           ` Maciej Purski
2018-06-04 13:59             ` Maciej Purski
     [not found]             ` <CGME20180604135952eucas1p2d76b6aa5d8fc9912113d519b48f7e99a@eucas1p2.samsung.com>
2018-06-04 13:59               ` [PATCH 1/7] regulator: core: Add debug messages Maciej Purski
2018-06-04 13:59                 ` Maciej Purski
     [not found]             ` <CGME20180604135952eucas1p2e3fdb68bf31e32b7c9557051671885a9@eucas1p2.samsung.com>
2018-06-04 13:59               ` [PATCH 2/7] regulator: core: Add regulator_set_voltage_rdev() Maciej Purski
2018-06-04 13:59                 ` Maciej Purski
     [not found]             ` <CGME20180604135953eucas1p2f2c9dd9581cd114d323c3d64afe5c308@eucas1p2.samsung.com>
2018-06-04 13:59               ` [PATCH 3/7] regulator: core: Use re-entrant locks Maciej Purski
2018-06-04 13:59                 ` Maciej Purski
     [not found]             ` <CGME20180604135953eucas1p2ec281df0793bc73e79f3000837abcb04@eucas1p2.samsung.com>
2018-06-04 13:59               ` [PATCH 4/7] regulator: core: Implement voltage balancing algorithm Maciej Purski
2018-06-04 13:59                 ` Maciej Purski
2018-06-04 13:59                 ` Maciej Purski
     [not found]             ` <CGME20180604135954eucas1p2156fed3300b5514a4efa2baf9e7b9bc5@eucas1p2.samsung.com>
2018-06-04 13:59               ` [PATCH 5/7] regulator: core: Lock dependent regulators Maciej Purski
2018-06-04 13:59                 ` Maciej Purski
2018-06-04 13:59                 ` Maciej Purski
2018-06-04 14:20                 ` Lucas Stach [this message]
2018-06-04 14:20                   ` Lucas Stach
2018-06-18 12:37                   ` Maciej Purski
2018-06-18 12:37                     ` Maciej Purski
     [not found]             ` <CGME20180604135954eucas1p2eeb77ada3ca97fecc6caec20d7e8397a@eucas1p2.samsung.com>
2018-06-04 13:59               ` [PATCH 6/7] regulator: core: Lock dependent regulators on regulator_enable() Maciej Purski
2018-06-04 13:59                 ` Maciej Purski
     [not found]             ` <CGME20180604135954eucas1p2bebd1c4970401bb957da228056f9a662@eucas1p2.samsung.com>
2018-06-04 13:59               ` [PATCH 7/7] regulator: core: Enable voltage balancing Maciej Purski
2018-06-04 13:59                 ` Maciej Purski
2018-06-04 23:13                 ` kbuild test robot
2018-06-04 23:13                   ` kbuild test robot
2018-06-04 23:13                   ` kbuild test robot
2018-06-04 23:54                 ` kbuild test robot
2018-06-04 23:54                   ` kbuild test robot
2018-06-04 23:54                   ` kbuild test robot
2018-06-05  4:45             ` Regression in Linux next again Tony Lindgren
2018-06-05  4:45               ` Tony Lindgren
     [not found]               ` <CGME20180613103622eucas1p1778ba2c2e5dd85ccb4c488bd0a38386d@eucas1p1.samsung.com>
2018-06-13 10:33                 ` [PATCH v2] regulator: core: Enable voltage balancing Maciej Purski
2018-06-13 10:33                   ` Maciej Purski
2018-06-15 11:29                   ` Tony Lindgren
2018-06-15 11:29                     ` Tony Lindgren
2018-06-18 13:17                     ` Maciej Purski
2018-06-18 13:17                       ` Maciej Purski
     [not found]                     ` <CGME20180618140856eucas1p281619f9bf003655a3c2eac356216ab25@eucas1p2.samsung.com>
2018-06-18 14:08                       ` [PATCH] regulator: core: Pass max_uV value to regulator_set_voltage_rdev Maciej Purski
2018-06-18 14:08                         ` Maciej Purski
2018-07-02  8:05                         ` Tony Lindgren
2018-07-02  8:05                           ` Tony Lindgren
2018-09-28 20:09                           ` Dmitry Osipenko
2018-09-28 20:09                             ` Dmitry Osipenko
2018-09-28 20:09                             ` Dmitry Osipenko
2018-09-28 20:09                             ` Dmitry Osipenko
2018-09-28 20:22                             ` Tony Lindgren
2018-09-28 20:22                               ` Tony Lindgren
2018-09-28 20:36                               ` Dmitry Osipenko
2018-09-28 20:36                                 ` Dmitry Osipenko
2018-09-28 22:26                               ` Dmitry Osipenko
2018-09-28 22:26                                 ` Dmitry Osipenko
2018-09-28 22:26                                 ` Dmitry Osipenko
2018-09-28 22:41                                 ` Tony Lindgren
2018-09-28 22:41                                   ` Tony Lindgren
2018-09-28 23:17                                   ` Dmitry Osipenko
2018-09-28 23:17                                     ` Dmitry Osipenko
2018-09-28 23:17                                     ` Dmitry Osipenko
2018-09-28 23:51                                     ` Dmitry Osipenko
2018-09-28 23:51                                       ` Dmitry Osipenko
2018-09-28 23:51                                       ` Dmitry Osipenko
2018-09-29  0:27                                       ` Tony Lindgren
2018-09-29  0:27                                         ` Tony Lindgren
2018-09-29  0:44                                         ` Dmitry Osipenko
2018-09-29  0:44                                           ` Dmitry Osipenko
2018-10-01  7:25                                           ` Maciej Purski
2018-10-01  7:25                                             ` Maciej Purski
2018-10-01 13:34                                             ` Dmitry Osipenko
2018-10-01 13:34                                               ` Dmitry Osipenko
2018-05-30 14:53       ` Regression in Linux next again Naresh Kamboju
2018-05-30 14:53         ` Naresh Kamboju
2018-05-31  5:44         ` Naresh Kamboju
2018-05-31  5:44           ` Naresh Kamboju

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1528122047.2728.10.camel@pengutronix.de \
    --to=l.stach@pengutronix.de \
    --cc=broonie@kernel.org \
    --cc=ceh@ti.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-omap@vger.kernel.org \
    --cc=m.purski@samsung.com \
    --cc=m.szyprowski@samsung.com \
    --cc=tony@atomide.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.