Skip to contentSkip to conversation

cputime: Fix timeval-->cputime conversion

PATCH In review zengtao 4 participants 9 messages Jan 28, 2016 – Feb 2, 2016 Generic include/asm header files

lore
Share & tools
lore

zengtao

author
#0 lore raw

PATCH 1/1 cputime: Fix timeval-->cputime conversion

The structure:

struct timeval {
	__kernel_time_t		tv_sec;		/* seconds */
	__kernel_suseconds_t	tv_usec;	/* microseconds */
};

both __kernel_time_t and __kernel_suseconds_t are short than u64 when it is 32bit platform, so force u64 conversion here.

---
 include/asm-generic/cputime_nsecs.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
include/asm-generic/cputime_nsecs.h +2 -1
index 0419485..e2f7ff9 100644
@@ -91,7 +91,8 @@ static inline void cputime_to_timespec(const cputime_t ct, struct timespec *val)
  */
 static inline cputime_t timeval_to_cputime(const struct timeval *val)
 {
-	u64 ret = val->tv_sec * NSEC_PER_SEC + val->tv_usec * NSEC_PER_USEC;
+	u64 ret = (u64)val->tv_sec * NSEC_PER_SEC +
+			val->tv_usec * NSEC_PER_USEC;
 	return (__force cputime_t) ret;
 }
 static inline void cputime_to_timeval(const cputime_t ct, struct timeval *val)
Signature · 1 line
1.9.1
Reviewed-by on v1 1/1
#1 lore raw

Cc'ing Arnd

Quoted 9 lines · zengtao

The structure:

struct timeval {
	__kernel_time_t		tv_sec;		/* seconds */
	__kernel_suseconds_t	tv_usec;	/* microseconds */
};

both __kernel_time_t and __kernel_suseconds_t are short than u64 when it is 32bit platform, so force u64 conversion here.

Quoted 22 lines
---
 include/asm-generic/cputime_nsecs.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
include/asm-generic/cputime_nsecs.h +2 -1
index 0419485..e2f7ff9 100644
@@ -91,7 +91,8 @@ static inline void cputime_to_timespec(const cputime_t ct, struct timespec *val)
  */
 static inline cputime_t timeval_to_cputime(const struct timeval *val)
 {
-	u64 ret = val->tv_sec * NSEC_PER_SEC + val->tv_usec * NSEC_PER_USEC;
+	u64 ret = (u64)val->tv_sec * NSEC_PER_SEC +
+			val->tv_usec * NSEC_PER_USEC;
 	return (__force cputime_t) ret;
 }
 static inline void cputime_to_timeval(const cputime_t ct, struct timeval *val)
Signature · 1 line
1.9.1
maintainer
#2 lore raw
Quoted 12 lines · On Thursday 28 January 2016 09:22:04 Thomas Gleixner wrote:

Cc'ing Arnd

Quoted 8 lines · zengtao

The structure:
struct timeval {

      __kernel_time_t         tv_sec;         /* seconds */
      __kernel_suseconds_t    tv_usec;        /* microseconds */

};
both __kernel_time_t and __kernel_suseconds_t are short than u64 when it is 32bit platform, so force u64 conversion here.

Quoted 3 lines
Quoted 1 line

This seems to miss timespec_to_cputime(), which has the same problem, so only setitimer() is fixed, but not nanosleep() or timer_settime().

There should probably be some explanation in which cases this happens, my reading is that can only occur on MIPS32 and ARM32 with "Full dynticks CPU time accounting" enabled, which is required for CONFIG_NO_HZ_FULL, so we need this backported to any kernel that includes 31c1fc818715 ("ARM: Kconfig: allow full nohz CPU accounting"), i.e. v3.13 or higher, correct?

Arnd

Quoted 21 lines
Quoted 21 lines
 include/asm-generic/cputime_nsecs.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
include/asm-generic/cputime_nsecs.h +2 -1
index 0419485..e2f7ff9 100644
@@ -91,7 +91,8 @@ static inline void cputime_to_timespec(const cputime_t ct, struct timespec *val)
  */
 static inline cputime_t timeval_to_cputime(const struct timeval *val)
 {
-     u64 ret = val->tv_sec * NSEC_PER_SEC + val->tv_usec * NSEC_PER_USEC;
+     u64 ret = (u64)val->tv_sec * NSEC_PER_SEC +
+                     val->tv_usec * NSEC_PER_USEC;
      return (__force cputime_t) ret;
 }
 static inline void cputime_to_timeval(const cputime_t ct, struct timeval *val)
Signature · 1 line
1.9.1
#3 lore raw
Quoted 20 lines

-----Original Message-----
From: Arnd Bergmann [mailto:[email protected]]
Sent: Thursday, January 28, 2016 7:52 PM
To: Thomas Gleixner
Cc: Zengtao (B); LKML; Frederic Weisbecker
Subject: Re: [PATCH] cputime: Fix timeval-->cputime conversion

Quoted 12 lines · On Thursday 28 January 2016 09:22:04 Thomas Gleixner wrote:

Cc'ing Arnd

Quoted 8 lines · zengtao

The structure:
struct timeval {

      __kernel_time_t         tv_sec;         /* seconds */
      __kernel_suseconds_t    tv_usec;        /* microseconds */

};
both __kernel_time_t and __kernel_suseconds_t are short than u64 when it is 32bit platform, so force u64 conversion here.

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

This seems to miss timespec_to_cputime(), which has the same problem, so only setitimer() is fixed, but not nanosleep() or timer_settime().

Yes, I have checked the code just now, the timespec_to_cputime() has the same problem.I found the origin issue through setitimer().And I think the timespec_to_cputime() only affects timer_settime(),by which means it affects nanosleep?

Quoted 7 lines

There should probably be some explanation in which cases this happens, my reading is that can only occur on MIPS32 and ARM32 with "Full dynticks CPU time accounting" enabled, which is required for CONFIG_NO_HZ_FULL, so we need this backported to any kernel that includes 31c1fc818715 ("ARM: Kconfig: allow full nohz CPU accounting"), i.e. v3.13 or higher, correct?

Yes.

Quoted 28 lines

Arnd

Quoted 4 lines

Review

Quoted 4 lines
 include/asm-generic/cputime_nsecs.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
(quoted diff) +0 -0

b/include/asm-generic/cputime_nsecs.h

Quoted 4 lines

include/asm-generic/cputime_nsecs.h static inline void cputime_to_timespec(const

Quoted 4 lines

index 0419485..e2f7ff9 100644

include/asm-generic/cputime_nsecs.h +0 -0
@@ -91,7 +91,8 @@ static inline void cputime_to_timespec(const

cputime_t ct, struct timespec *val)

Quoted 4 lines

include/asm-generic/cputime_nsecs.h static inline void cputime_to_timespec(const cputime_t ct, struct timespec *val)

Quoted 4 lines
include/asm-generic/cputime_nsecs.h +0 -1
  */
 static inline cputime_t timeval_to_cputime(const struct timeval *val)
 {
-     u64 ret = val->tv_sec * NSEC_PER_SEC + val->tv_usec *

NSEC_PER_USEC;

Quoted 5 lines

include/asm-generic/cputime_nsecs.h static inline void cputime_to_timespec(const cputime_t ct, struct timespec *val)

Quoted 5 lines
include/asm-generic/cputime_nsecs.h +2 -0
+     u64 ret = (u64)val->tv_sec * NSEC_PER_SEC +
+                     val->tv_usec * NSEC_PER_USEC;
      return (__force cputime_t) ret;
 }
 static inline void cputime_to_timeval(const cputime_t ct, struct timeval

*val)

Quoted 4 lines
Quoted 4 lines

--
1.9.1

maintainer
#4 lore raw
Quoted 22 lines · On Friday 29 January 2016 03:12:37 Zengtao wrote:
Quoted 22 lines

-----Original Message-----
From: Arnd Bergmann [mailto:[email protected]]
Sent: Thursday, January 28, 2016 7:52 PM
To: Thomas Gleixner
Cc: Zengtao (B); LKML; Frederic Weisbecker
Subject: Re: [PATCH] cputime: Fix timeval-->cputime conversion

Quoted 14 lines · On Thursday 28 January 2016 09:22:04 Thomas Gleixner wrote:

Cc'ing Arnd

Quoted 9 lines · zengtao

The structure:
struct timeval {

      __kernel_time_t         tv_sec;         /* seconds */
      __kernel_suseconds_t    tv_usec;        /* microseconds */

};
both __kernel_time_t and __kernel_suseconds_t are short than u64 when it is 32bit platform, so force u64 conversion here.

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

This seems to miss timespec_to_cputime(), which has the same problem, so only setitimer() is fixed, but not nanosleep() or timer_settime().

Yes, I have checked the code just now, the timespec_to_cputime() has the same problem.I found the origin issue through setitimer().And I think the timespec_to_cputime() only affects timer_settime(),by which means it affects nanosleep?

Reading that code again, I think it does not affect sys_nanosleep, but it does affect sys_clock_nanosleep(CLOCK_PROCESS_CPUTIME_ID, ...) along with timer_create/timer_settime with CLOCK_PROCESS_CPUTIME_ID.

Arnd

#5 lore raw
Quoted 38 lines

-----Original Message-----
From: Arnd Bergmann [mailto:[email protected]]
Sent: Friday, January 29, 2016 4:46 PM
To: Zengtao (B)
Cc: Thomas Gleixner; LKML; Frederic Weisbecker
Subject: Re: [PATCH] cputime: Fix timeval-->cputime conversion

Quoted 8 lines · On Friday 29 January 2016 03:12:37 Zengtao wrote:
Quoted 8 lines

-----Original Message-----
From: Arnd Bergmann [mailto:[email protected]]
Sent: Thursday, January 28, 2016 7:52 PM
To: Thomas Gleixner
Cc: Zengtao (B); LKML; Frederic Weisbecker
Subject: Re: [PATCH] cputime: Fix timeval-->cputime conversion

On Thursday 28 January 2016 09:22:04 Thomas Gleixner wrote:

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

Cc'ing Arnd

Quoted 4 lines · zengtao

The structure:
struct timeval {

      __kernel_time_t         tv_sec;         /* seconds */
      __kernel_suseconds_t    tv_usec;        /* microseconds

*/

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines

};
both __kernel_time_t and __kernel_suseconds_t are short than u64 when it is 32bit platform, so force u64 conversion here.

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

This seems to miss timespec_to_cputime(), which has the same problem, so only setitimer() is fixed, but not nanosleep() or timer_settime().

Yes, I have checked the code just now, the timespec_to_cputime() has the same problem.I found the origin issue through setitimer().And I think the timespec_to_cputime() only affects timer_settime(),by which means it

Quoted 7 lines

affects

Quoted 1 line

nanosleep?

Reading that code again, I think it does not affect sys_nanosleep, but it does affect sys_clock_nanosleep(CLOCK_PROCESS_CPUTIME_ID, ...) along with timer_create/timer_settime with CLOCK_PROCESS_CPUTIME_ID.

Got it, I will fix the timespec_to_cputime and resend the patch later.

Quoted 1 line

Arnd

#6 lore raw
In reply to Arnd Bergmann

Hi Arnd:
I have got a new idea about the problem:
In include/linux/time64.h
#define NSEC_PER_SEC 1000000000L
I think we should change it to
#define NSEC_PER_SEC 1000000000LL

My reason is :
1. when it is used in a multiplication, it will easily get overflow.
2. when it don't get overflow, the change has no side affect.

Thanks.
Zengtao

Quoted 54 lines

-----Original Message-----
From: Zengtao (B)
Sent: Saturday, January 30, 2016 10:31 AM
To: 'Arnd Bergmann'
Cc: Thomas Gleixner; LKML; Frederic Weisbecker
Subject: RE: [PATCH] cputime: Fix timeval-->cputime conversion

Quoted 38 lines

-----Original Message-----
From: Arnd Bergmann [mailto:[email protected]]
Sent: Friday, January 29, 2016 4:46 PM
To: Zengtao (B)
Cc: Thomas Gleixner; LKML; Frederic Weisbecker
Subject: Re: [PATCH] cputime: Fix timeval-->cputime conversion

Quoted 8 lines · On Friday 29 January 2016 03:12:37 Zengtao wrote:
Quoted 8 lines

-----Original Message-----
From: Arnd Bergmann [mailto:[email protected]]
Sent: Thursday, January 28, 2016 7:52 PM
To: Thomas Gleixner
Cc: Zengtao (B); LKML; Frederic Weisbecker
Subject: Re: [PATCH] cputime: Fix timeval-->cputime conversion

On Thursday 28 January 2016 09:22:04 Thomas Gleixner wrote:

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

Cc'ing Arnd

Quoted 4 lines · zengtao

The structure:
struct timeval {

      __kernel_time_t         tv_sec;         /* seconds */
      __kernel_suseconds_t    tv_usec;        /* microseconds

*/

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines

};
both __kernel_time_t and __kernel_suseconds_t are short than u64 when it is 32bit platform, so force u64 conversion here.

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

This seems to miss timespec_to_cputime(), which has the same problem, so only setitimer() is fixed, but not nanosleep() or timer_settime().

Yes, I have checked the code just now, the timespec_to_cputime() has the same problem.I found the origin issue through setitimer().And I think the timespec_to_cputime() only affects timer_settime(),by which means it

Quoted 7 lines

affects

Quoted 1 line

nanosleep?

Reading that code again, I think it does not affect sys_nanosleep, but it does affect sys_clock_nanosleep(CLOCK_PROCESS_CPUTIME_ID, ...) along with timer_create/timer_settime with CLOCK_PROCESS_CPUTIME_ID.

Got it, I will fix the timespec_to_cputime and resend the patch later.

Quoted 1 line

Arnd

#7 lore raw
Quoted 10 lines · Zengtao (B)

Hi Arnd:
I have got a new idea about the problem:
In include/linux/time64.h
#define NSEC_PER_SEC 1000000000L
I think we should change it to
#define NSEC_PER_SEC 1000000000LL

My reason is :
1. when it is used in a multiplication, it will easily get overflow.
2. when it don't get overflow, the change has no side affect.

That's not a good idea. NSEC_PER_SEC is used in lots of places with 32bit storage. I really don't want to mop up all the fallout.

Thanks,

tglx

#8 lore raw
Quoted 16 lines

-----Original Message-----
From: Thomas Gleixner [mailto:[email protected]]
Sent: Monday, February 01, 2016 4:43 PM
To: Zengtao (B)
Cc: Arnd Bergmann; LKML; Frederic Weisbecker
Subject: RE: [PATCH] cputime: Fix timeval-->cputime conversion

Quoted 7 lines · Zengtao (B)

Hi Arnd:
I have got a new idea about the problem:
In include/linux/time64.h
#define NSEC_PER_SEC 1000000000L
I think we should change it to
#define NSEC_PER_SEC 1000000000LL

Quoted 6 lines
Quoted 3 lines

My reason is :
1. when it is used in a multiplication, it will easily get overflow.
2. when it don't get overflow, the change has no side affect.

That's not a good idea. NSEC_PER_SEC is used in lots of places with 32bit storage. I really don't want to mop up all the fallout.

Yes, agree, a lot of places it has been used as 32bit value. Beyond that, I think it is more reasonable to use 64bit.

Quoted 5 lines

Thanks,

tglx