public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH]: In kernel hibernation, suspend to both: draft
@ 2012-05-07  8:37 Bojan Smojver
  2012-05-07 20:14 ` Rafael J. Wysocki
  0 siblings, 1 reply; 6+ messages in thread
From: Bojan Smojver @ 2012-05-07  8:37 UTC (permalink / raw)
  To: Rafael J. Wysocki; +Cc: Linux PM list, linux-kernel

Hi Rafael,

Was just playing with it and this is what I came up with. This is just a
rough draft (I didn't even think of memory leaks and such here, to be
honest). Let me know what you think.

------------------------------
diff --git a/Documentation/power/swsusp.txt b/Documentation/power/swsusp.txt
index ac190cf..92341b8 100644
--- a/Documentation/power/swsusp.txt
+++ b/Documentation/power/swsusp.txt
@@ -33,6 +33,11 @@ echo shutdown > /sys/power/disk; echo disk > /sys/power/state
 
 echo platform > /sys/power/disk; echo disk > /sys/power/state
 
+. If you would like to write hibernation image to swap and then suspend
+to RAM (provided your platform supports it), you can try
+
+echo suspend > /sys/power/disk; echo disk > /sys/power/state
+
 . If you have SATA disks, you'll need recent kernels with SATA suspend
 support. For suspend and resume to work, make sure your disk drivers
 are built into kernel -- not modules. [There's way to make
diff --git a/kernel/power/hibernate.c b/kernel/power/hibernate.c
index e09dfbf..3b5726f 100644
--- a/kernel/power/hibernate.c
+++ b/kernel/power/hibernate.c
@@ -5,6 +5,7 @@
  * Copyright (c) 2003 Open Source Development Lab
  * Copyright (c) 2004 Pavel Machek <pavel@ucw.cz>
  * Copyright (c) 2009 Rafael J. Wysocki, Novell Inc.
+ * Copyright (C) 2012 Bojan Smojver <bojan@rexursive.com>
  *
  * This file is released under the GPLv2.
  */
@@ -44,6 +45,9 @@ enum {
 	HIBERNATION_PLATFORM,
 	HIBERNATION_SHUTDOWN,
 	HIBERNATION_REBOOT,
+#ifdef CONFIG_SUSPEND
+	HIBERNATION_SUSPEND,
+#endif
 	/* keep last */
 	__HIBERNATION_AFTER_LAST
 };
@@ -572,6 +576,10 @@ int hibernation_platform_enter(void)
  */
 static void power_down(void)
 {
+#ifdef CONFIG_SUSPEND
+	int error;
+#endif
+
 	switch (hibernation_mode) {
 	case HIBERNATION_REBOOT:
 		kernel_restart(NULL);
@@ -581,6 +589,25 @@ static void power_down(void)
 	case HIBERNATION_SHUTDOWN:
 		kernel_power_off();
 		break;
+#ifdef CONFIG_SUSPEND
+	case HIBERNATION_SUSPEND:
+		error = suspend_devices_and_enter(PM_SUSPEND_MEM);
+		if (error) {
+			if (hibernation_ops)
+				hibernation_mode = HIBERNATION_PLATFORM;
+			else
+				hibernation_mode = HIBERNATION_SHUTDOWN;
+			power_down();
+		}
+		/*
+		 * Restore swap signature.
+		 */
+		error = swsusp_unmark();
+		if (error)
+			printk(KERN_ERR "PM: Swap will be unusable! "
+			                "Try swapon -a.\n");
+		return;
+#endif
 	}
 	kernel_halt();
 	/*
@@ -814,6 +841,9 @@ static const char * const hibernation_modes[] = {
 	[HIBERNATION_PLATFORM]	= "platform",
 	[HIBERNATION_SHUTDOWN]	= "shutdown",
 	[HIBERNATION_REBOOT]	= "reboot",
+#ifdef CONFIG_SUSPEND
+	[HIBERNATION_SUSPEND]	= "suspend",
+#endif
 };
 
 /*
@@ -854,6 +884,9 @@ static ssize_t disk_show(struct kobject *kobj, struct kobj_attribute *attr,
 		switch (i) {
 		case HIBERNATION_SHUTDOWN:
 		case HIBERNATION_REBOOT:
+#ifdef CONFIG_SUSPEND
+		case HIBERNATION_SUSPEND:
+#endif
 			break;
 		case HIBERNATION_PLATFORM:
 			if (hibernation_ops)
@@ -894,6 +927,9 @@ static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr,
 		switch (mode) {
 		case HIBERNATION_SHUTDOWN:
 		case HIBERNATION_REBOOT:
+#ifdef CONFIG_SUSPEND
+		case HIBERNATION_SUSPEND:
+#endif
 			hibernation_mode = mode;
 			break;
 		case HIBERNATION_PLATFORM:
diff --git a/kernel/power/power.h b/kernel/power/power.h
index 98f3622..07b4a68 100644
--- a/kernel/power/power.h
+++ b/kernel/power/power.h
@@ -156,6 +156,9 @@ extern void swsusp_free(void);
 extern int swsusp_read(unsigned int *flags_p);
 extern int swsusp_write(unsigned int flags);
 extern void swsusp_close(fmode_t);
+#ifdef CONFIG_SUSPEND
+extern int swsusp_unmark(void);
+#endif
 
 /* kernel/power/block_io.c */
 extern struct block_device *hib_resume_bdev;
diff --git a/kernel/power/swap.c b/kernel/power/swap.c
index eef311a..1027da8 100644
--- a/kernel/power/swap.c
+++ b/kernel/power/swap.c
@@ -1456,6 +1456,34 @@ void swsusp_close(fmode_t mode)
 	blkdev_put(hib_resume_bdev, mode);
 }
 
+/**
+ *      swsusp_unmark - Unmark swsusp signature in the resume device
+ */
+
+#ifdef CONFIG_SUSPEND
+int swsusp_unmark(void)
+{
+	int error;
+
+	hib_bio_read_page(swsusp_resume_block, swsusp_header, NULL);
+	if (!memcmp(HIBERNATE_SIG,swsusp_header->sig, 10)) {
+		memcpy(swsusp_header->sig,swsusp_header->orig_sig, 10);
+		error = hib_bio_write_page(swsusp_resume_block,
+					swsusp_header, NULL);
+	} else {
+		printk(KERN_ERR "PM: Cannot find swsusp signature!\n");
+		error = -ENODEV;
+	}
+
+	/*
+	 * We just returned from suspend, we don't need the image any more.
+	 */
+	free_all_swap_pages(root_swap);
+
+	return error;
+}
+#endif
+
 static int swsusp_header_init(void)
 {
 	swsusp_header = (struct swsusp_header*) __get_free_page(GFP_KERNEL);
------------------------------

-- 
Bojan


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH]: In kernel hibernation, suspend to both: draft
  2012-05-07  8:37 [PATCH]: In kernel hibernation, suspend to both: draft Bojan Smojver
@ 2012-05-07 20:14 ` Rafael J. Wysocki
  2012-05-08  3:15   ` Bojan Smojver
  0 siblings, 1 reply; 6+ messages in thread
From: Rafael J. Wysocki @ 2012-05-07 20:14 UTC (permalink / raw)
  To: Bojan Smojver; +Cc: Linux PM list, linux-kernel

Hi,

On Monday, May 07, 2012, Bojan Smojver wrote:
> Hi Rafael,
> 
> Was just playing with it and this is what I came up with. This is just a
> rough draft (I didn't even think of memory leaks and such here, to be
> honest). Let me know what you think.

Looks reasonable at first sight.

Thanks,
Rafael


> ------------------------------
> diff --git a/Documentation/power/swsusp.txt b/Documentation/power/swsusp.txt
> index ac190cf..92341b8 100644
> --- a/Documentation/power/swsusp.txt
> +++ b/Documentation/power/swsusp.txt
> @@ -33,6 +33,11 @@ echo shutdown > /sys/power/disk; echo disk > /sys/power/state
>  
>  echo platform > /sys/power/disk; echo disk > /sys/power/state
>  
> +. If you would like to write hibernation image to swap and then suspend
> +to RAM (provided your platform supports it), you can try
> +
> +echo suspend > /sys/power/disk; echo disk > /sys/power/state
> +
>  . If you have SATA disks, you'll need recent kernels with SATA suspend
>  support. For suspend and resume to work, make sure your disk drivers
>  are built into kernel -- not modules. [There's way to make
> diff --git a/kernel/power/hibernate.c b/kernel/power/hibernate.c
> index e09dfbf..3b5726f 100644
> --- a/kernel/power/hibernate.c
> +++ b/kernel/power/hibernate.c
> @@ -5,6 +5,7 @@
>   * Copyright (c) 2003 Open Source Development Lab
>   * Copyright (c) 2004 Pavel Machek <pavel@ucw.cz>
>   * Copyright (c) 2009 Rafael J. Wysocki, Novell Inc.
> + * Copyright (C) 2012 Bojan Smojver <bojan@rexursive.com>
>   *
>   * This file is released under the GPLv2.
>   */
> @@ -44,6 +45,9 @@ enum {
>  	HIBERNATION_PLATFORM,
>  	HIBERNATION_SHUTDOWN,
>  	HIBERNATION_REBOOT,
> +#ifdef CONFIG_SUSPEND
> +	HIBERNATION_SUSPEND,
> +#endif
>  	/* keep last */
>  	__HIBERNATION_AFTER_LAST
>  };
> @@ -572,6 +576,10 @@ int hibernation_platform_enter(void)
>   */
>  static void power_down(void)
>  {
> +#ifdef CONFIG_SUSPEND
> +	int error;
> +#endif
> +
>  	switch (hibernation_mode) {
>  	case HIBERNATION_REBOOT:
>  		kernel_restart(NULL);
> @@ -581,6 +589,25 @@ static void power_down(void)
>  	case HIBERNATION_SHUTDOWN:
>  		kernel_power_off();
>  		break;
> +#ifdef CONFIG_SUSPEND
> +	case HIBERNATION_SUSPEND:
> +		error = suspend_devices_and_enter(PM_SUSPEND_MEM);
> +		if (error) {
> +			if (hibernation_ops)
> +				hibernation_mode = HIBERNATION_PLATFORM;
> +			else
> +				hibernation_mode = HIBERNATION_SHUTDOWN;
> +			power_down();
> +		}
> +		/*
> +		 * Restore swap signature.
> +		 */
> +		error = swsusp_unmark();
> +		if (error)
> +			printk(KERN_ERR "PM: Swap will be unusable! "
> +			                "Try swapon -a.\n");
> +		return;
> +#endif
>  	}
>  	kernel_halt();
>  	/*
> @@ -814,6 +841,9 @@ static const char * const hibernation_modes[] = {
>  	[HIBERNATION_PLATFORM]	= "platform",
>  	[HIBERNATION_SHUTDOWN]	= "shutdown",
>  	[HIBERNATION_REBOOT]	= "reboot",
> +#ifdef CONFIG_SUSPEND
> +	[HIBERNATION_SUSPEND]	= "suspend",
> +#endif
>  };
>  
>  /*
> @@ -854,6 +884,9 @@ static ssize_t disk_show(struct kobject *kobj, struct kobj_attribute *attr,
>  		switch (i) {
>  		case HIBERNATION_SHUTDOWN:
>  		case HIBERNATION_REBOOT:
> +#ifdef CONFIG_SUSPEND
> +		case HIBERNATION_SUSPEND:
> +#endif
>  			break;
>  		case HIBERNATION_PLATFORM:
>  			if (hibernation_ops)
> @@ -894,6 +927,9 @@ static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr,
>  		switch (mode) {
>  		case HIBERNATION_SHUTDOWN:
>  		case HIBERNATION_REBOOT:
> +#ifdef CONFIG_SUSPEND
> +		case HIBERNATION_SUSPEND:
> +#endif
>  			hibernation_mode = mode;
>  			break;
>  		case HIBERNATION_PLATFORM:
> diff --git a/kernel/power/power.h b/kernel/power/power.h
> index 98f3622..07b4a68 100644
> --- a/kernel/power/power.h
> +++ b/kernel/power/power.h
> @@ -156,6 +156,9 @@ extern void swsusp_free(void);
>  extern int swsusp_read(unsigned int *flags_p);
>  extern int swsusp_write(unsigned int flags);
>  extern void swsusp_close(fmode_t);
> +#ifdef CONFIG_SUSPEND
> +extern int swsusp_unmark(void);
> +#endif
>  
>  /* kernel/power/block_io.c */
>  extern struct block_device *hib_resume_bdev;
> diff --git a/kernel/power/swap.c b/kernel/power/swap.c
> index eef311a..1027da8 100644
> --- a/kernel/power/swap.c
> +++ b/kernel/power/swap.c
> @@ -1456,6 +1456,34 @@ void swsusp_close(fmode_t mode)
>  	blkdev_put(hib_resume_bdev, mode);
>  }
>  
> +/**
> + *      swsusp_unmark - Unmark swsusp signature in the resume device
> + */
> +
> +#ifdef CONFIG_SUSPEND
> +int swsusp_unmark(void)
> +{
> +	int error;
> +
> +	hib_bio_read_page(swsusp_resume_block, swsusp_header, NULL);
> +	if (!memcmp(HIBERNATE_SIG,swsusp_header->sig, 10)) {
> +		memcpy(swsusp_header->sig,swsusp_header->orig_sig, 10);
> +		error = hib_bio_write_page(swsusp_resume_block,
> +					swsusp_header, NULL);
> +	} else {
> +		printk(KERN_ERR "PM: Cannot find swsusp signature!\n");
> +		error = -ENODEV;
> +	}
> +
> +	/*
> +	 * We just returned from suspend, we don't need the image any more.
> +	 */
> +	free_all_swap_pages(root_swap);
> +
> +	return error;
> +}
> +#endif
> +
>  static int swsusp_header_init(void)
>  {
>  	swsusp_header = (struct swsusp_header*) __get_free_page(GFP_KERNEL);
> ------------------------------
> 
> 


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH]: In kernel hibernation, suspend to both: draft
  2012-05-07 20:14 ` Rafael J. Wysocki
@ 2012-05-08  3:15   ` Bojan Smojver
  2012-05-08 21:26     ` Rafael J. Wysocki
  0 siblings, 1 reply; 6+ messages in thread
From: Bojan Smojver @ 2012-05-08  3:15 UTC (permalink / raw)
  To: Rafael J. Wysocki; +Cc: Linux PM list, linux-kernel

On Mon, 2012-05-07 at 22:14 +0200, Rafael J. Wysocki wrote:
> Looks reasonable at first sight.

OK, thanks for having a look! I'll check to see whether memory for
hibernation is been released properly and then I may submit this for
real. BTW, the patch actually worked on my ThinkPad T510. The miracles
never stop. :-)

Just to be sure - I should prepare a patch against your linux-next
branch, correct? This patch was against mainline, which is still
somewhat behind.

-- 
Bojan


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH]: In kernel hibernation, suspend to both: draft
  2012-05-08  3:15   ` Bojan Smojver
@ 2012-05-08 21:26     ` Rafael J. Wysocki
  2012-05-09  7:24       ` Borislav Petkov
  0 siblings, 1 reply; 6+ messages in thread
From: Rafael J. Wysocki @ 2012-05-08 21:26 UTC (permalink / raw)
  To: Bojan Smojver; +Cc: Linux PM list, linux-kernel

On Tuesday, May 08, 2012, Bojan Smojver wrote:
> On Mon, 2012-05-07 at 22:14 +0200, Rafael J. Wysocki wrote:
> > Looks reasonable at first sight.
> 
> OK, thanks for having a look! I'll check to see whether memory for
> hibernation is been released properly and then I may submit this for
> real. BTW, the patch actually worked on my ThinkPad T510. The miracles
> never stop. :-)
> 
> Just to be sure - I should prepare a patch against your linux-next
> branch, correct?

Yup.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH]: In kernel hibernation, suspend to both: draft
  2012-05-08 21:26     ` Rafael J. Wysocki
@ 2012-05-09  7:24       ` Borislav Petkov
  2012-05-09  7:41         ` Bojan Smojver
  0 siblings, 1 reply; 6+ messages in thread
From: Borislav Petkov @ 2012-05-09  7:24 UTC (permalink / raw)
  To: Rafael J. Wysocki; +Cc: Bojan Smojver, Linux PM list, linux-kernel

On Tue, May 08, 2012 at 11:26:42PM +0200, Rafael J. Wysocki wrote:
> On Tuesday, May 08, 2012, Bojan Smojver wrote:
> > On Mon, 2012-05-07 at 22:14 +0200, Rafael J. Wysocki wrote:
> > > Looks reasonable at first sight.
> > 
> > OK, thanks for having a look! I'll check to see whether memory for
> > hibernation is been released properly and then I may submit this for
> > real. BTW, the patch actually worked on my ThinkPad T510. The miracles
> > never stop. :-)
> > 
> > Just to be sure - I should prepare a patch against your linux-next
> > branch, correct?
> 
> Yup.

Oh goody, that's what the apples do - suspend to ram and disk. When you
have the patch ready, you'll have another tester, just send it on my way
too pls :-).

Thanks.

-- 
Regards/Gruss,
    Boris.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH]: In kernel hibernation, suspend to both: draft
  2012-05-09  7:24       ` Borislav Petkov
@ 2012-05-09  7:41         ` Bojan Smojver
  0 siblings, 0 replies; 6+ messages in thread
From: Bojan Smojver @ 2012-05-09  7:41 UTC (permalink / raw)
  To: Borislav Petkov; +Cc: Rafael J. Wysocki, Linux PM list, linux-kernel

On Wed, 2012-05-09 at 09:24 +0200, Borislav Petkov wrote:
> Oh goody, that's what the apples do - suspend to ram and disk. When
> you have the patch ready, you'll have another tester, just send it on
> my way too pls :-).

Pretty much the same thing submitted to Rafael here:

http://marc.info/?l=linux-kernel&m=133651588229850&w=2

It worked on my ThinkPad, but YMMV. :-)

-- 
Bojan


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2012-05-09  7:41 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-05-07  8:37 [PATCH]: In kernel hibernation, suspend to both: draft Bojan Smojver
2012-05-07 20:14 ` Rafael J. Wysocki
2012-05-08  3:15   ` Bojan Smojver
2012-05-08 21:26     ` Rafael J. Wysocki
2012-05-09  7:24       ` Borislav Petkov
2012-05-09  7:41         ` Bojan Smojver

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox