diff mbox

[3/3] PM / hibernate / memory hotplug: Rework mutual exclusion

Message ID 1562298.ZjRvhqQzT7@vostro.rjw.lan (mailing list archive)
State Accepted, archived
Headers show

Commit Message

Rafael Wysocki Aug. 29, 2013, 9:18 p.m. UTC
From: Rafael J. Wysocki <rafael.j.wysocki@intel.com>

Since all of the memory hotplug operations have to be carried out
under device_hotplug_lock, they won't need to acquire pm_mutex if
device_hotplug_lock is held around hibernation.

For this reason, make the hibernation code acquire
device_hotplug_lock after freezing user space processes and
release it before thawing them.  At the same tim drop the
lock_system_sleep() and unlock_system_sleep() calls from
lock_memory_hotplug() and unlock_memory_hotplug(), respectively.

Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
---
 kernel/power/hibernate.c |    4 ++++
 kernel/power/user.c      |    2 ++
 mm/memory_hotplug.c      |    4 ----
 3 files changed, 6 insertions(+), 4 deletions(-)


--
To unsubscribe from this list: send the line "unsubscribe linux-pm" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Comments

Toshi Kani Aug. 31, 2013, 12:23 a.m. UTC | #1
On Thu, 2013-08-29 at 23:18 +0200, Rafael J. Wysocki wrote:
> From: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
> 
> Since all of the memory hotplug operations have to be carried out
> under device_hotplug_lock, they won't need to acquire pm_mutex if
> device_hotplug_lock is held around hibernation.
> 
> For this reason, make the hibernation code acquire
> device_hotplug_lock after freezing user space processes and
> release it before thawing them.  At the same tim drop the
> lock_system_sleep() and unlock_system_sleep() calls from
> lock_memory_hotplug() and unlock_memory_hotplug(), respectively.
> 
> Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
> ---
>  kernel/power/hibernate.c |    4 ++++
>  kernel/power/user.c      |    2 ++
>  mm/memory_hotplug.c      |    4 ----
>  3 files changed, 6 insertions(+), 4 deletions(-)
> 
> Index: linux-pm/kernel/power/hibernate.c
> ===================================================================
> --- linux-pm.orig/kernel/power/hibernate.c
> +++ linux-pm/kernel/power/hibernate.c
> @@ -652,6 +652,7 @@ int hibernate(void)
>  	if (error)
>  		goto Exit;
>  
> +	lock_device_hotplug();

Since hibernate() can be called from sysfs, do you think the tool may
see this as a circular dependency with p_active again?  This shouldn't
be a problem in practice, though.

Thanks,
-Toshi

--
To unsubscribe from this list: send the line "unsubscribe linux-pm" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Toshi Kani Aug. 31, 2013, 12:35 a.m. UTC | #2
On Sat, 2013-08-31 at 02:39 +0200, Rafael J. Wysocki wrote:
> On Friday, August 30, 2013 06:23:19 PM Toshi Kani wrote:
> > On Thu, 2013-08-29 at 23:18 +0200, Rafael J. Wysocki wrote:
> > > From: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
> > > 
> > > Since all of the memory hotplug operations have to be carried out
> > > under device_hotplug_lock, they won't need to acquire pm_mutex if
> > > device_hotplug_lock is held around hibernation.
> > > 
> > > For this reason, make the hibernation code acquire
> > > device_hotplug_lock after freezing user space processes and
> > > release it before thawing them.  At the same tim drop the
> > > lock_system_sleep() and unlock_system_sleep() calls from
> > > lock_memory_hotplug() and unlock_memory_hotplug(), respectively.
> > > 
> > > Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
> > > ---
> > >  kernel/power/hibernate.c |    4 ++++
> > >  kernel/power/user.c      |    2 ++
> > >  mm/memory_hotplug.c      |    4 ----
> > >  3 files changed, 6 insertions(+), 4 deletions(-)
> > > 
> > > Index: linux-pm/kernel/power/hibernate.c
> > > ===================================================================
> > > --- linux-pm.orig/kernel/power/hibernate.c
> > > +++ linux-pm/kernel/power/hibernate.c
> > > @@ -652,6 +652,7 @@ int hibernate(void)
> > >  	if (error)
> > >  		goto Exit;
> > >  
> > > +	lock_device_hotplug();
> > 
> > Since hibernate() can be called from sysfs, do you think the tool may
> > see this as a circular dependency with p_active again?  This shouldn't
> > be a problem in practice, though.
> 
> /sys/power/state isn't a device attribute even and is never removed, so it
> would be very sad and disappointing if lockdep reported that as a circular
> dependency.  The deadlock is surely not possible here anyway.

Agreed.  The code looks good otherwise, and this is a nice cleanup.  If
it is OK to ignore the possible warning from the tool (which I do not
know the rule here), feel free to add my ack to patch 2/3 and 3/3 as
well.

Thanks,
-Toshi


--
To unsubscribe from this list: send the line "unsubscribe linux-pm" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Rafael Wysocki Aug. 31, 2013, 12:39 a.m. UTC | #3
On Friday, August 30, 2013 06:23:19 PM Toshi Kani wrote:
> On Thu, 2013-08-29 at 23:18 +0200, Rafael J. Wysocki wrote:
> > From: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
> > 
> > Since all of the memory hotplug operations have to be carried out
> > under device_hotplug_lock, they won't need to acquire pm_mutex if
> > device_hotplug_lock is held around hibernation.
> > 
> > For this reason, make the hibernation code acquire
> > device_hotplug_lock after freezing user space processes and
> > release it before thawing them.  At the same tim drop the
> > lock_system_sleep() and unlock_system_sleep() calls from
> > lock_memory_hotplug() and unlock_memory_hotplug(), respectively.
> > 
> > Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
> > ---
> >  kernel/power/hibernate.c |    4 ++++
> >  kernel/power/user.c      |    2 ++
> >  mm/memory_hotplug.c      |    4 ----
> >  3 files changed, 6 insertions(+), 4 deletions(-)
> > 
> > Index: linux-pm/kernel/power/hibernate.c
> > ===================================================================
> > --- linux-pm.orig/kernel/power/hibernate.c
> > +++ linux-pm/kernel/power/hibernate.c
> > @@ -652,6 +652,7 @@ int hibernate(void)
> >  	if (error)
> >  		goto Exit;
> >  
> > +	lock_device_hotplug();
> 
> Since hibernate() can be called from sysfs, do you think the tool may
> see this as a circular dependency with p_active again?  This shouldn't
> be a problem in practice, though.

/sys/power/state isn't a device attribute even and is never removed, so it
would be very sad and disappointing if lockdep reported that as a circular
dependency.  The deadlock is surely not possible here anyway.

Thanks,
Rafael

--
To unsubscribe from this list: send the line "unsubscribe linux-pm" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Rafael Wysocki Aug. 31, 2013, 12:50 a.m. UTC | #4
On Friday, August 30, 2013 06:35:12 PM Toshi Kani wrote:
> On Sat, 2013-08-31 at 02:39 +0200, Rafael J. Wysocki wrote:
> > On Friday, August 30, 2013 06:23:19 PM Toshi Kani wrote:
> > > On Thu, 2013-08-29 at 23:18 +0200, Rafael J. Wysocki wrote:
> > > > From: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
> > > > 
> > > > Since all of the memory hotplug operations have to be carried out
> > > > under device_hotplug_lock, they won't need to acquire pm_mutex if
> > > > device_hotplug_lock is held around hibernation.
> > > > 
> > > > For this reason, make the hibernation code acquire
> > > > device_hotplug_lock after freezing user space processes and
> > > > release it before thawing them.  At the same tim drop the
> > > > lock_system_sleep() and unlock_system_sleep() calls from
> > > > lock_memory_hotplug() and unlock_memory_hotplug(), respectively.
> > > > 
> > > > Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
> > > > ---
> > > >  kernel/power/hibernate.c |    4 ++++
> > > >  kernel/power/user.c      |    2 ++
> > > >  mm/memory_hotplug.c      |    4 ----
> > > >  3 files changed, 6 insertions(+), 4 deletions(-)
> > > > 
> > > > Index: linux-pm/kernel/power/hibernate.c
> > > > ===================================================================
> > > > --- linux-pm.orig/kernel/power/hibernate.c
> > > > +++ linux-pm/kernel/power/hibernate.c
> > > > @@ -652,6 +652,7 @@ int hibernate(void)
> > > >  	if (error)
> > > >  		goto Exit;
> > > >  
> > > > +	lock_device_hotplug();
> > > 
> > > Since hibernate() can be called from sysfs, do you think the tool may
> > > see this as a circular dependency with p_active again?  This shouldn't
> > > be a problem in practice, though.
> > 
> > /sys/power/state isn't a device attribute even and is never removed, so it
> > would be very sad and disappointing if lockdep reported that as a circular
> > dependency.  The deadlock is surely not possible here anyway.
> 
> Agreed.  The code looks good otherwise, and this is a nice cleanup.  If
> it is OK to ignore the possible warning from the tool (which I do not
> know the rule here),

Well, if it complains, we'll just need to add some annotations to this.
The code is correct I believe.

> feel free to add my ack to patch 2/3 and 3/3 as well.

I will, thanks!
diff mbox

Patch

Index: linux-pm/kernel/power/hibernate.c
===================================================================
--- linux-pm.orig/kernel/power/hibernate.c
+++ linux-pm/kernel/power/hibernate.c
@@ -652,6 +652,7 @@  int hibernate(void)
 	if (error)
 		goto Exit;
 
+	lock_device_hotplug();
 	/* Allocate memory management structures */
 	error = create_basic_memory_bitmaps();
 	if (error)
@@ -685,6 +686,7 @@  int hibernate(void)
  Free_bitmaps:
 	free_basic_memory_bitmaps();
  Thaw:
+	unlock_device_hotplug();
 	thaw_processes();
 
 	/* Don't bother checking whether freezer_test_done is true */
@@ -814,6 +816,7 @@  static int software_resume(void)
 
 	pr_debug("PM: Loading hibernation image.\n");
 
+	lock_device_hotplug();
 	error = create_basic_memory_bitmaps();
 	if (error)
 		goto Thaw;
@@ -827,6 +830,7 @@  static int software_resume(void)
 	swsusp_free();
 	free_basic_memory_bitmaps();
  Thaw:
+	unlock_device_hotplug();
 	thaw_processes();
  Finish:
 	pm_notifier_call_chain(PM_POST_RESTORE);
Index: linux-pm/kernel/power/user.c
===================================================================
--- linux-pm.orig/kernel/power/user.c
+++ linux-pm/kernel/power/user.c
@@ -201,6 +201,7 @@  static long snapshot_ioctl(struct file *
 	if (!mutex_trylock(&pm_mutex))
 		return -EBUSY;
 
+	lock_device_hotplug();
 	data = filp->private_data;
 
 	switch (cmd) {
@@ -373,6 +374,7 @@  static long snapshot_ioctl(struct file *
 
 	}
 
+	unlock_device_hotplug();
 	mutex_unlock(&pm_mutex);
 
 	return error;
Index: linux-pm/mm/memory_hotplug.c
===================================================================
--- linux-pm.orig/mm/memory_hotplug.c
+++ linux-pm/mm/memory_hotplug.c
@@ -51,14 +51,10 @@  DEFINE_MUTEX(mem_hotplug_mutex);
 void lock_memory_hotplug(void)
 {
 	mutex_lock(&mem_hotplug_mutex);
-
-	/* for exclusive hibernation if CONFIG_HIBERNATION=y */
-	lock_system_sleep();
 }
 
 void unlock_memory_hotplug(void)
 {
-	unlock_system_sleep();
 	mutex_unlock(&mem_hotplug_mutex);
 }