Skip to contentSkip to conversation

PM / Hibernate: Migrate to ktime_t

PATCH In review Tina Ruchandani 4 participants 4 messages Oct 30, 2014 – Nov 8, 2014

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 PM / Hibernate: Migrate to ktime_t

This patch migrates swsusp_show_speed and its callers to using ktime_t instead of 'struct timeval' which suffers from the y2038 problem.

Changes to swsusp_show_speed:
- use ktime_t for start and stop times
- pass start and stop times by value
Calling functions affected:
- load_image
- load_image_lzo
- save_image
- save_image_lzo
- hibernate_preallocate_memory
Design decisions:
- use ktime_t to preserve same granularity of reporting as before
- use centisecs logic as before to avoid 'div by zero' issues caused by using seconds and nanoseconds directly
- use monotonic time (ktime_get()) since we only care about elapsed time.

--
Changes in v4:
- Rebased patch off of 3.18-rc1
Changes in v3:
- Use monotonic clock (ktime_get())
instead of ktime_get_real()
- Fix incorrect denominator in ktime_divns
Changes in v2:
- Incorporate whitespace fixes suggested by
Arnd Bergmann <[email protected]>
- Use ktime_divns instead of ktime_to_ns and
then do_div to get centisecs
- Explain reason for changing elapsed_centisecs
to u64.

---
 kernel/power/hibernate.c | 14 ++++++--------
 kernel/power/power.h     |  3 +--
 kernel/power/snapshot.c  |  9 +++++----
 kernel/power/swap.c      | 41 +++++++++++++++++++++--------------------
 4 files changed, 33 insertions(+), 34 deletions(-)
kernel/power/hibernate.c +6 -8
index a9dfa79..6e6cb96 100644
@@ -28,6 +28,7 @@
 #include <linux/syscore_ops.h>
 #include <linux/ctype.h>
 #include <linux/genhd.h>
+#include <linux/ktime.h>
 #include <trace/events/power.h>
 
 #include "power.h"
@@ -232,20 +233,17 @@ static void platform_recover(int platform_mode)
  * @nr_pages: Number of memory pages processed between @start and @stop.
  * @msg: Additional diagnostic message to print.
  */
-void swsusp_show_speed(struct timeval *start, struct timeval *stop,
-			unsigned nr_pages, char *msg)
+void swsusp_show_speed(ktime_t start, ktime_t stop,
+		      unsigned nr_pages, char *msg)
 {
+	ktime_t diff;
 	u64 elapsed_centisecs64;
 	unsigned int centisecs;
 	unsigned int k;
 	unsigned int kps;
 
-	elapsed_centisecs64 = timeval_to_ns(stop) - timeval_to_ns(start);
-	/*
-	 * If "(s64)elapsed_centisecs64 < 0", it will print long elapsed time,
-	 * it is obvious enough for what went wrong.
-	 */
-	do_div(elapsed_centisecs64, NSEC_PER_SEC / 100);
+	diff = ktime_sub(stop, start);
+	elapsed_centisecs64 = ktime_divns(diff, 10*NSEC_PER_MSEC);
 	centisecs = elapsed_centisecs64;
 	if (centisecs == 0)
 		centisecs = 1;	/* avoid div-by-zero */
kernel/power/power.h +1 -2
index 2df883a..ce9b832 100644
@@ -174,8 +174,7 @@ extern int hib_wait_on_bio_chain(struct bio **bio_chain);
 
 struct timeval;
 /* kernel/power/swsusp.c */
-extern void swsusp_show_speed(struct timeval *, struct timeval *,
-				unsigned int, char *);
+extern void swsusp_show_speed(ktime_t, ktime_t, unsigned int, char *);
 
 #ifdef CONFIG_SUSPEND
 /* kernel/power/suspend.c */
kernel/power/snapshot.c +5 -4
index 791a618..0c40c16 100644
@@ -28,6 +28,7 @@
 #include <linux/list.h>
 #include <linux/slab.h>
 #include <linux/compiler.h>
+#include <linux/ktime.h>
 
 #include <asm/uaccess.h>
 #include <asm/mmu_context.h>
@@ -1576,11 +1577,11 @@ int hibernate_preallocate_memory(void)
 	struct zone *zone;
 	unsigned long saveable, size, max_size, count, highmem, pages = 0;
 	unsigned long alloc, save_highmem, pages_highmem, avail_normal;
-	struct timeval start, stop;
+	ktime_t start, stop;
 	int error;
 
 	printk(KERN_INFO "PM: Preallocating image memory... ");
-	do_gettimeofday(&start);
+	start = ktime_get();
 
 	error = memory_bm_create(&orig_bm, GFP_IMAGE, PG_ANY);
 	if (error)
@@ -1709,9 +1710,9 @@ int hibernate_preallocate_memory(void)
 	free_unnecessary_pages();
 
  out:
-	do_gettimeofday(&stop);
+	stop = ktime_get();
 	printk(KERN_CONT "done (allocated %lu pages)\n", pages);
-	swsusp_show_speed(&start, &stop, pages, "Allocated");
+	swsusp_show_speed(start, stop, pages, "Allocated");
 
 	return 0;
 
kernel/power/swap.c +21 -20

139 lines not on this page · raw message

Signature · 1 line
2.1.0.rc2.206.gedb03e5
Acked-by on v1 1/1
#1 lore raw
Quoted 18 lines · Tina Ruchandani

This patch migrates swsusp_show_speed and its callers to using ktime_t instead of 'struct timeval' which suffers from the y2038 problem.

Changes to swsusp_show_speed:
- use ktime_t for start and stop times
- pass start and stop times by value
Calling functions affected:
- load_image
- load_image_lzo
- save_image
- save_image_lzo
- hibernate_preallocate_memory
Design decisions:
- use ktime_t to preserve same granularity of reporting as before
- use centisecs logic as before to avoid 'div by zero' issues caused by using seconds and nanoseconds directly
- use monotonic time (ktime_get()) since we only care about elapsed time.

Quoted 2 lines
Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
Reviewed-by on v1 1/1
#2 lore raw
In reply to Tina Ruchandani
Quoted 18 lines · On Thursday 30 October 2014 11:04:53 Tina Ruchandani wrote:

This patch migrates swsusp_show_speed and its callers to using ktime_t instead of 'struct timeval' which suffers from the y2038 problem.

Changes to swsusp_show_speed:
- use ktime_t for start and stop times
- pass start and stop times by value
Calling functions affected:
- load_image
- load_image_lzo
- save_image
- save_image_lzo
- hibernate_preallocate_memory
Design decisions:
- use ktime_t to preserve same granularity of reporting as before
- use centisecs logic as before to avoid 'div by zero' issues caused by using seconds and nanoseconds directly
- use monotonic time (ktime_get()) since we only care about elapsed time.

Quoted 2 lines
Quoted 5 lines

--
Changes in v4:
- Rebased patch off of 3.18-rc1
Changes in v3:

Note: you need three '-' characters to separate the patch description from any extra information that you want in the mail but not in the changelog.

Arnd

#3 lore raw
Quoted 19 lines · Arnd Bergmann
Quoted 18 lines · On Thursday 30 October 2014 11:04:53 Tina Ruchandani wrote:

This patch migrates swsusp_show_speed and its callers to using ktime_t instead of 'struct timeval' which suffers from the y2038 problem.

Changes to swsusp_show_speed:
- use ktime_t for start and stop times
- pass start and stop times by value
Calling functions affected:
- load_image
- load_image_lzo
- save_image
- save_image_lzo
- hibernate_preallocate_memory
Design decisions:
- use ktime_t to preserve same granularity of reporting as before
- use centisecs logic as before to avoid 'div by zero' issues caused by using seconds and nanoseconds directly
- use monotonic time (ktime_get()) since we only care about elapsed time.

Quoted 4 lines
Quoted 2 lines

Patch queued up for 3.19-rc1, thanks!

Signature · 2 lines
I speak only for myself.
Rafael J. Wysocki, Intel Open Source Technology Center.