All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/2] Fix for ADJ_SETOFFSET w/ ADJ_NANO
@ 2016-01-21 23:03 John Stultz
  2016-01-21 23:03 ` [PATCH 1/2] ntp: Fix ADJ_SETOFFSET being used " John Stultz
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: John Stultz @ 2016-01-21 23:03 UTC (permalink / raw)
  To: lkml
  Cc: John Stultz, Sasha Levin, Richard Cochran, Thomas Gleixner,
	Prarit Bhargava, Harald Hoyer, Kay Sievers, David Herrmann,
	Shuah Khan

David Herrmann mailed me pointing out that one of the
changes that landed in 4.5-rc broke users of ADJ_SETOFFSET
when used with ADJ_NANO.

I've implemented a fix to this issue and also introduced
more unit tests to validate these going forward.

Thomas: Can you queue the first patch for tip/timers/urgent?

Shuah: The kselftests patch can wait to the next merge window
if you'd prefer.

Let me know if you have any thoughts or objections!

thanks
-john


Cc: Sasha Levin <sasha.levin@oracle.com>
Cc: Richard Cochran <richardcochran@gmail.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Prarit Bhargava <prarit@redhat.com>
Cc: Harald Hoyer <harald@redhat.com>
Cc: Kay Sievers <kay@vrfy.org>
Cc: David Herrmann <dh.herrmann@gmail.com>
Cc: Shuah Khan <shuahkh@osg.samsung.com>

John Stultz (2):
  ntp: Fix ADJ_SETOFFSET being used w/ ADJ_NANO
  kselftests: timers: Add adjtimex SETOFFSET validity tests

 kernel/time/ntp.c                               |  14 ++-
 tools/testing/selftests/timers/valid-adjtimex.c | 139 +++++++++++++++++++++++-
 2 files changed, 150 insertions(+), 3 deletions(-)

-- 
1.9.1

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

* [PATCH 1/2] ntp: Fix ADJ_SETOFFSET being used w/ ADJ_NANO
  2016-01-21 23:03 [PATCH 0/2] Fix for ADJ_SETOFFSET w/ ADJ_NANO John Stultz
@ 2016-01-21 23:03 ` John Stultz
  2016-01-22 11:06   ` David Herrmann
  2016-01-22 11:06   ` [tip:timers/urgent] " tip-bot for John Stultz
  2016-01-21 23:03 ` [PATCH 2/2] kselftests: timers: Add adjtimex SETOFFSET validity tests John Stultz
  2016-01-21 23:16 ` [PATCH 0/2] Fix for ADJ_SETOFFSET w/ ADJ_NANO Shuah Khan
  2 siblings, 2 replies; 9+ messages in thread
From: John Stultz @ 2016-01-21 23:03 UTC (permalink / raw)
  To: lkml
  Cc: John Stultz, Sasha Levin, Richard Cochran, Thomas Gleixner,
	Prarit Bhargava, Harald Hoyer, Kay Sievers, David Herrmann

Recently, in commit 37cf4dc3370f
("time: Verify time values in adjtimex ADJ_SETOFFSET to avoid overflow")
I forgot to check if the timeval being passed was actually a
timespec (as is signaled with ADJ_NANO).

This resulted in that patch breaking ADJ_SETOFFSET users who set
ADJ_NANO, by rejecting valid timespecs that were compared with
valid timeval ranges.

This patch addresses this by checking for the ADJ_NANO flag and
using the timepsec check instead in that case.

Cc: Sasha Levin <sasha.levin@oracle.com>
Cc: Richard Cochran <richardcochran@gmail.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Prarit Bhargava <prarit@redhat.com>
Cc: Harald Hoyer <harald@redhat.com>
Cc: Kay Sievers <kay@vrfy.org>
Cc: David Herrmann <dh.herrmann@gmail.com>
Reported-by: Harald Hoyer <harald@redhat.com>
Reported-by: Kay Sievers <kay@vrfy.org>
Signed-off-by: John Stultz <john.stultz@linaro.org>
---
 kernel/time/ntp.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

diff --git a/kernel/time/ntp.c b/kernel/time/ntp.c
index 36f2ca0..6df8927 100644
--- a/kernel/time/ntp.c
+++ b/kernel/time/ntp.c
@@ -685,8 +685,18 @@ int ntp_validate_timex(struct timex *txc)
 		if (!capable(CAP_SYS_TIME))
 			return -EPERM;
 
-		if (!timeval_inject_offset_valid(&txc->time))
-			return -EINVAL;
+		if (txc->modes & ADJ_NANO) {
+			struct timespec ts;
+
+			ts.tv_sec = txc->time.tv_sec;
+			ts.tv_nsec = txc->time.tv_usec;
+			if (!timespec_inject_offset_valid(&ts))
+				return -EINVAL;
+
+		} else {
+			if (!timeval_inject_offset_valid(&txc->time))
+				return -EINVAL;
+		}
 	}
 
 	/*
-- 
1.9.1

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

* [PATCH 2/2] kselftests: timers: Add adjtimex SETOFFSET validity tests
  2016-01-21 23:03 [PATCH 0/2] Fix for ADJ_SETOFFSET w/ ADJ_NANO John Stultz
  2016-01-21 23:03 ` [PATCH 1/2] ntp: Fix ADJ_SETOFFSET being used " John Stultz
@ 2016-01-21 23:03 ` John Stultz
  2016-01-26 15:39   ` [tip:timers/urgent] " tip-bot for John Stultz
  2016-01-21 23:16 ` [PATCH 0/2] Fix for ADJ_SETOFFSET w/ ADJ_NANO Shuah Khan
  2 siblings, 1 reply; 9+ messages in thread
From: John Stultz @ 2016-01-21 23:03 UTC (permalink / raw)
  To: lkml
  Cc: John Stultz, Sasha Levin, Richard Cochran, Thomas Gleixner,
	Prarit Bhargava, Harald Hoyer, Kay Sievers, David Herrmann,
	Shuah Khan

Add some simple tests to check both valid and invalid
offsets when using adjtimex's ADJ_SETOFFSET method.

Cc: Sasha Levin <sasha.levin@oracle.com>
Cc: Richard Cochran <richardcochran@gmail.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Prarit Bhargava <prarit@redhat.com>
Cc: Harald Hoyer <harald@redhat.com>
Cc: Kay Sievers <kay@vrfy.org>
Cc: David Herrmann <dh.herrmann@gmail.com>
Cc: Shuah Khan <shuahkh@osg.samsung.com>
Signed-off-by: John Stultz <john.stultz@linaro.org>
---
 tools/testing/selftests/timers/valid-adjtimex.c | 139 +++++++++++++++++++++++-
 1 file changed, 138 insertions(+), 1 deletion(-)

diff --git a/tools/testing/selftests/timers/valid-adjtimex.c b/tools/testing/selftests/timers/valid-adjtimex.c
index e86d937..60fe3c5 100644
--- a/tools/testing/selftests/timers/valid-adjtimex.c
+++ b/tools/testing/selftests/timers/valid-adjtimex.c
@@ -45,7 +45,17 @@ static inline int ksft_exit_fail(void)
 }
 #endif
 
-#define NSEC_PER_SEC 1000000000L
+#define NSEC_PER_SEC 1000000000LL
+#define USEC_PER_SEC 1000000LL
+
+#define ADJ_SETOFFSET 0x0100
+
+#include <sys/syscall.h>
+static int clock_adjtime(clockid_t id, struct timex *tx)
+{
+	return syscall(__NR_clock_adjtime, id, tx);
+}
+
 
 /* clear NTP time_status & time_state */
 int clear_time_state(void)
@@ -193,10 +203,137 @@ out:
 }
 
 
+int set_offset(long long offset, int use_nano)
+{
+	struct timex tmx = {};
+	int ret;
+
+	tmx.modes = ADJ_SETOFFSET;
+	if (use_nano) {
+		tmx.modes |= ADJ_NANO;
+
+		tmx.time.tv_sec = offset / NSEC_PER_SEC;
+		tmx.time.tv_usec = offset % NSEC_PER_SEC;
+
+		if (offset < 0 && tmx.time.tv_usec) {
+			tmx.time.tv_sec -= 1;
+			tmx.time.tv_usec += NSEC_PER_SEC;
+		}
+	} else {
+		tmx.time.tv_sec = offset / USEC_PER_SEC;
+		tmx.time.tv_usec = offset % USEC_PER_SEC;
+
+		if (offset < 0 && tmx.time.tv_usec) {
+			tmx.time.tv_sec -= 1;
+			tmx.time.tv_usec += USEC_PER_SEC;
+		}
+	}
+
+	ret = clock_adjtime(CLOCK_REALTIME, &tmx);
+	if (ret < 0) {
+		printf("(sec: %ld  usec: %ld) ", tmx.time.tv_sec, tmx.time.tv_usec);
+		printf("[FAIL]\n");
+		return -1;
+	}
+	return 0;
+}
+
+int set_bad_offset(long sec, long usec, int use_nano)
+{
+	struct timex tmx = {};
+	int ret;
+
+	tmx.modes = ADJ_SETOFFSET;
+	if (use_nano)
+		tmx.modes |= ADJ_NANO;
+
+	tmx.time.tv_sec = sec;
+	tmx.time.tv_usec = usec;
+	ret = clock_adjtime(CLOCK_REALTIME, &tmx);
+	if (ret >= 0) {
+		printf("Invalid (sec: %ld  usec: %ld) did not fail! ", tmx.time.tv_sec, tmx.time.tv_usec);
+		printf("[FAIL]\n");
+		return -1;
+	}
+	return 0;
+}
+
+int validate_set_offset(void)
+{
+	printf("Testing ADJ_SETOFFSET... ");
+
+	/* Test valid values */
+	if (set_offset(NSEC_PER_SEC - 1, 1))
+		return -1;
+
+	if (set_offset(-NSEC_PER_SEC + 1, 1))
+		return -1;
+
+	if (set_offset(-NSEC_PER_SEC - 1, 1))
+		return -1;
+
+	if (set_offset(5 * NSEC_PER_SEC, 1))
+		return -1;
+
+	if (set_offset(-5 * NSEC_PER_SEC, 1))
+		return -1;
+
+	if (set_offset(5 * NSEC_PER_SEC + NSEC_PER_SEC / 2, 1))
+		return -1;
+
+	if (set_offset(-5 * NSEC_PER_SEC - NSEC_PER_SEC / 2, 1))
+		return -1;
+
+	if (set_offset(USEC_PER_SEC - 1, 0))
+		return -1;
+
+	if (set_offset(-USEC_PER_SEC + 1, 0))
+		return -1;
+
+	if (set_offset(-USEC_PER_SEC - 1, 0))
+		return -1;
+
+	if (set_offset(5 * USEC_PER_SEC, 0))
+		return -1;
+
+	if (set_offset(-5 * USEC_PER_SEC, 0))
+		return -1;
+
+	if (set_offset(5 * USEC_PER_SEC + USEC_PER_SEC / 2, 0))
+		return -1;
+
+	if (set_offset(-5 * USEC_PER_SEC - USEC_PER_SEC / 2, 0))
+		return -1;
+
+	/* Test invalid values */
+	if (set_bad_offset(0, -1, 1))
+		return -1;
+	if (set_bad_offset(0, -1, 0))
+		return -1;
+	if (set_bad_offset(0, 2 * NSEC_PER_SEC, 1))
+		return -1;
+	if (set_bad_offset(0, 2 * USEC_PER_SEC, 0))
+		return -1;
+	if (set_bad_offset(0, NSEC_PER_SEC, 1))
+		return -1;
+	if (set_bad_offset(0, USEC_PER_SEC, 0))
+		return -1;
+	if (set_bad_offset(0, -NSEC_PER_SEC, 1))
+		return -1;
+	if (set_bad_offset(0, -USEC_PER_SEC, 0))
+		return -1;
+
+	printf("[OK]\n");
+	return 0;
+}
+
 int main(int argc, char **argv)
 {
 	if (validate_freq())
 		return ksft_exit_fail();
 
+	if (validate_set_offset())
+		return ksft_exit_fail();
+
 	return ksft_exit_pass();
 }
-- 
1.9.1

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

* Re: [PATCH 0/2] Fix for ADJ_SETOFFSET w/ ADJ_NANO
  2016-01-21 23:03 [PATCH 0/2] Fix for ADJ_SETOFFSET w/ ADJ_NANO John Stultz
  2016-01-21 23:03 ` [PATCH 1/2] ntp: Fix ADJ_SETOFFSET being used " John Stultz
  2016-01-21 23:03 ` [PATCH 2/2] kselftests: timers: Add adjtimex SETOFFSET validity tests John Stultz
@ 2016-01-21 23:16 ` Shuah Khan
  2016-01-22  7:54   ` Thomas Gleixner
  2 siblings, 1 reply; 9+ messages in thread
From: Shuah Khan @ 2016-01-21 23:16 UTC (permalink / raw)
  To: John Stultz, lkml
  Cc: Sasha Levin, Richard Cochran, Thomas Gleixner, Prarit Bhargava,
	Harald Hoyer, Kay Sievers, David Herrmann, Shuah Khan

On 01/21/2016 04:03 PM, John Stultz wrote:
> David Herrmann mailed me pointing out that one of the
> changes that landed in 4.5-rc broke users of ADJ_SETOFFSET
> when used with ADJ_NANO.
> 
> I've implemented a fix to this issue and also introduced
> more unit tests to validate these going forward.
> 
> Thomas: Can you queue the first patch for tip/timers/urgent?
> 
> Shuah: The kselftests patch can wait to the next merge window
> if you'd prefer.

Yeah. Probably it has to wait until the next merge window as
this is a new test. I can pull this into linux-kselftest next
after merge window closes.

thanks,
-- Shuah


-- 
Shuah Khan
Sr. Linux Kernel Developer
Open Source Innovation Group
Samsung Research America (Silicon Valley)
shuahkh@osg.samsung.com | (970) 217-8978

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

* Re: [PATCH 0/2] Fix for ADJ_SETOFFSET w/ ADJ_NANO
  2016-01-21 23:16 ` [PATCH 0/2] Fix for ADJ_SETOFFSET w/ ADJ_NANO Shuah Khan
@ 2016-01-22  7:54   ` Thomas Gleixner
  2016-01-22 14:56     ` Shuah Khan
  0 siblings, 1 reply; 9+ messages in thread
From: Thomas Gleixner @ 2016-01-22  7:54 UTC (permalink / raw)
  To: Shuah Khan
  Cc: John Stultz, lkml, Sasha Levin, Richard Cochran, Prarit Bhargava,
	Harald Hoyer, Kay Sievers, David Herrmann

On Thu, 21 Jan 2016, Shuah Khan wrote:
> On 01/21/2016 04:03 PM, John Stultz wrote:
> > David Herrmann mailed me pointing out that one of the
> > changes that landed in 4.5-rc broke users of ADJ_SETOFFSET
> > when used with ADJ_NANO.
> > 
> > I've implemented a fix to this issue and also introduced
> > more unit tests to validate these going forward.
> > 
> > Thomas: Can you queue the first patch for tip/timers/urgent?
> > 
> > Shuah: The kselftests patch can wait to the next merge window
> > if you'd prefer.
> 
> Yeah. Probably it has to wait until the next merge window as
> this is a new test. I can pull this into linux-kselftest next
> after merge window closes.

We really should not delay selftests, especially if they have been written
along with a fix for a recently detected problem.

Thanks,

	tglx

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

* Re: [PATCH 1/2] ntp: Fix ADJ_SETOFFSET being used w/ ADJ_NANO
  2016-01-21 23:03 ` [PATCH 1/2] ntp: Fix ADJ_SETOFFSET being used " John Stultz
@ 2016-01-22 11:06   ` David Herrmann
  2016-01-22 11:06   ` [tip:timers/urgent] " tip-bot for John Stultz
  1 sibling, 0 replies; 9+ messages in thread
From: David Herrmann @ 2016-01-22 11:06 UTC (permalink / raw)
  To: John Stultz
  Cc: lkml, Sasha Levin, Richard Cochran, Thomas Gleixner,
	Prarit Bhargava, Harald Hoyer, Kay Sievers

Hi

On Fri, Jan 22, 2016 at 12:03 AM, John Stultz <john.stultz@linaro.org> wrote:
> Recently, in commit 37cf4dc3370f
> ("time: Verify time values in adjtimex ADJ_SETOFFSET to avoid overflow")
> I forgot to check if the timeval being passed was actually a
> timespec (as is signaled with ADJ_NANO).
>
> This resulted in that patch breaking ADJ_SETOFFSET users who set
> ADJ_NANO, by rejecting valid timespecs that were compared with
> valid timeval ranges.
>
> This patch addresses this by checking for the ADJ_NANO flag and
> using the timepsec check instead in that case.
>
> Cc: Sasha Levin <sasha.levin@oracle.com>
> Cc: Richard Cochran <richardcochran@gmail.com>
> Cc: Thomas Gleixner <tglx@linutronix.de>
> Cc: Prarit Bhargava <prarit@redhat.com>
> Cc: Harald Hoyer <harald@redhat.com>
> Cc: Kay Sievers <kay@vrfy.org>
> Cc: David Herrmann <dh.herrmann@gmail.com>
> Reported-by: Harald Hoyer <harald@redhat.com>
> Reported-by: Kay Sievers <kay@vrfy.org>
> Signed-off-by: John Stultz <john.stultz@linaro.org>

Thanks for picking this up. Looks good to me:

Reviewed-by: David Herrmann <dh.herrmann@gmail.com>

Thanks
David

> ---
>  kernel/time/ntp.c | 14 ++++++++++++--
>  1 file changed, 12 insertions(+), 2 deletions(-)
>
> diff --git a/kernel/time/ntp.c b/kernel/time/ntp.c
> index 36f2ca0..6df8927 100644
> --- a/kernel/time/ntp.c
> +++ b/kernel/time/ntp.c
> @@ -685,8 +685,18 @@ int ntp_validate_timex(struct timex *txc)
>                 if (!capable(CAP_SYS_TIME))
>                         return -EPERM;
>
> -               if (!timeval_inject_offset_valid(&txc->time))
> -                       return -EINVAL;
> +               if (txc->modes & ADJ_NANO) {
> +                       struct timespec ts;
> +
> +                       ts.tv_sec = txc->time.tv_sec;
> +                       ts.tv_nsec = txc->time.tv_usec;
> +                       if (!timespec_inject_offset_valid(&ts))
> +                               return -EINVAL;
> +
> +               } else {
> +                       if (!timeval_inject_offset_valid(&txc->time))
> +                               return -EINVAL;
> +               }
>         }
>
>         /*
> --
> 1.9.1
>

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

* [tip:timers/urgent] ntp: Fix ADJ_SETOFFSET being used w/ ADJ_NANO
  2016-01-21 23:03 ` [PATCH 1/2] ntp: Fix ADJ_SETOFFSET being used " John Stultz
  2016-01-22 11:06   ` David Herrmann
@ 2016-01-22 11:06   ` tip-bot for John Stultz
  1 sibling, 0 replies; 9+ messages in thread
From: tip-bot for John Stultz @ 2016-01-22 11:06 UTC (permalink / raw)
  To: linux-tip-commits
  Cc: hpa, sasha.levin, prarit, tglx, john.stultz, mingo, kay,
	linux-kernel, dh.herrmann, richardcochran, harald

Commit-ID:  dd4e17ab704269bce71402285f5e8b9ac24b1eff
Gitweb:     http://git.kernel.org/tip/dd4e17ab704269bce71402285f5e8b9ac24b1eff
Author:     John Stultz <john.stultz@linaro.org>
AuthorDate: Thu, 21 Jan 2016 15:03:34 -0800
Committer:  Thomas Gleixner <tglx@linutronix.de>
CommitDate: Fri, 22 Jan 2016 12:01:42 +0100

ntp: Fix ADJ_SETOFFSET being used w/ ADJ_NANO

Recently, in commit 37cf4dc3370f I forgot to check if the timeval being passed
was actually a timespec (as is signaled with ADJ_NANO).

This resulted in that patch breaking ADJ_SETOFFSET users who set
ADJ_NANO, by rejecting valid timespecs that were compared with
valid timeval ranges.

This patch addresses this by checking for the ADJ_NANO flag and
using the timepsec check instead in that case.

Reported-by: Harald Hoyer <harald@redhat.com>
Reported-by: Kay Sievers <kay@vrfy.org>
Fixes: 37cf4dc3370f "time: Verify time values in adjtimex ADJ_SETOFFSET to avoid overflow"
Signed-off-by: John Stultz <john.stultz@linaro.org>
Cc: Sasha Levin <sasha.levin@oracle.com>
Cc: Richard Cochran <richardcochran@gmail.com>
Cc: Prarit Bhargava <prarit@redhat.com>
Cc: David Herrmann <dh.herrmann@gmail.com>
Link: http://lkml.kernel.org/r/1453417415-19110-2-git-send-email-john.stultz@linaro.org
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
---
 kernel/time/ntp.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

diff --git a/kernel/time/ntp.c b/kernel/time/ntp.c
index 36f2ca0..6df8927 100644
--- a/kernel/time/ntp.c
+++ b/kernel/time/ntp.c
@@ -685,8 +685,18 @@ int ntp_validate_timex(struct timex *txc)
 		if (!capable(CAP_SYS_TIME))
 			return -EPERM;
 
-		if (!timeval_inject_offset_valid(&txc->time))
-			return -EINVAL;
+		if (txc->modes & ADJ_NANO) {
+			struct timespec ts;
+
+			ts.tv_sec = txc->time.tv_sec;
+			ts.tv_nsec = txc->time.tv_usec;
+			if (!timespec_inject_offset_valid(&ts))
+				return -EINVAL;
+
+		} else {
+			if (!timeval_inject_offset_valid(&txc->time))
+				return -EINVAL;
+		}
 	}
 
 	/*

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

* Re: [PATCH 0/2] Fix for ADJ_SETOFFSET w/ ADJ_NANO
  2016-01-22  7:54   ` Thomas Gleixner
@ 2016-01-22 14:56     ` Shuah Khan
  0 siblings, 0 replies; 9+ messages in thread
From: Shuah Khan @ 2016-01-22 14:56 UTC (permalink / raw)
  To: Thomas Gleixner
  Cc: John Stultz, lkml, Sasha Levin, Richard Cochran, Prarit Bhargava,
	Harald Hoyer, Kay Sievers, David Herrmann

On 01/22/2016 12:54 AM, Thomas Gleixner wrote:
> On Thu, 21 Jan 2016, Shuah Khan wrote:
>> On 01/21/2016 04:03 PM, John Stultz wrote:
>>> David Herrmann mailed me pointing out that one of the
>>> changes that landed in 4.5-rc broke users of ADJ_SETOFFSET
>>> when used with ADJ_NANO.
>>>
>>> I've implemented a fix to this issue and also introduced
>>> more unit tests to validate these going forward.
>>>
>>> Thomas: Can you queue the first patch for tip/timers/urgent?
>>>
>>> Shuah: The kselftests patch can wait to the next merge window
>>> if you'd prefer.
>>
>> Yeah. Probably it has to wait until the next merge window as
>> this is a new test. I can pull this into linux-kselftest next
>> after merge window closes.
> 
> We really should not delay selftests, especially if they have been written
> along with a fix for a recently detected problem.
> 

Thomas,

Yes. That is why I have "probably" in my response.
Could you please fold this test in with your urgent
fix, so they can go in together.

Acked-by: Shuah Khan <shuahkh@osg.samsung.com>

thanks,
-- Shuah

-- 
Shuah Khan
Sr. Linux Kernel Developer
Open Source Innovation Group
Samsung Research America (Silicon Valley)
shuahkh@osg.samsung.com | (970) 217-8978

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

* [tip:timers/urgent] kselftests: timers: Add adjtimex SETOFFSET validity tests
  2016-01-21 23:03 ` [PATCH 2/2] kselftests: timers: Add adjtimex SETOFFSET validity tests John Stultz
@ 2016-01-26 15:39   ` tip-bot for John Stultz
  0 siblings, 0 replies; 9+ messages in thread
From: tip-bot for John Stultz @ 2016-01-26 15:39 UTC (permalink / raw)
  To: linux-tip-commits
  Cc: richardcochran, linux-kernel, dh.herrmann, sasha.levin,
	john.stultz, harald, kay, prarit, tglx, hpa, mingo, shuahkh

Commit-ID:  e03a58c320e1103ebe97bda8ebdfcc5c9829c53f
Gitweb:     http://git.kernel.org/tip/e03a58c320e1103ebe97bda8ebdfcc5c9829c53f
Author:     John Stultz <john.stultz@linaro.org>
AuthorDate: Thu, 21 Jan 2016 15:03:35 -0800
Committer:  Thomas Gleixner <tglx@linutronix.de>
CommitDate: Tue, 26 Jan 2016 16:26:06 +0100

kselftests: timers: Add adjtimex SETOFFSET validity tests

Add some simple tests to check both valid and invalid
offsets when using adjtimex's ADJ_SETOFFSET method.

Signed-off-by: John Stultz <john.stultz@linaro.org>
Acked-by: Shuah Khan <shuahkh@osg.samsung.com>
Cc: Sasha Levin <sasha.levin@oracle.com>
Cc: Richard Cochran <richardcochran@gmail.com>
Cc: Prarit Bhargava <prarit@redhat.com>
Cc: Harald Hoyer <harald@redhat.com>
Cc: Kay Sievers <kay@vrfy.org>
Cc: David Herrmann <dh.herrmann@gmail.com>
Link: http://lkml.kernel.org/r/1453417415-19110-3-git-send-email-john.stultz@linaro.org
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
---
 tools/testing/selftests/timers/valid-adjtimex.c | 139 +++++++++++++++++++++++-
 1 file changed, 138 insertions(+), 1 deletion(-)

diff --git a/tools/testing/selftests/timers/valid-adjtimex.c b/tools/testing/selftests/timers/valid-adjtimex.c
index e86d937..60fe3c5 100644
--- a/tools/testing/selftests/timers/valid-adjtimex.c
+++ b/tools/testing/selftests/timers/valid-adjtimex.c
@@ -45,7 +45,17 @@ static inline int ksft_exit_fail(void)
 }
 #endif
 
-#define NSEC_PER_SEC 1000000000L
+#define NSEC_PER_SEC 1000000000LL
+#define USEC_PER_SEC 1000000LL
+
+#define ADJ_SETOFFSET 0x0100
+
+#include <sys/syscall.h>
+static int clock_adjtime(clockid_t id, struct timex *tx)
+{
+	return syscall(__NR_clock_adjtime, id, tx);
+}
+
 
 /* clear NTP time_status & time_state */
 int clear_time_state(void)
@@ -193,10 +203,137 @@ out:
 }
 
 
+int set_offset(long long offset, int use_nano)
+{
+	struct timex tmx = {};
+	int ret;
+
+	tmx.modes = ADJ_SETOFFSET;
+	if (use_nano) {
+		tmx.modes |= ADJ_NANO;
+
+		tmx.time.tv_sec = offset / NSEC_PER_SEC;
+		tmx.time.tv_usec = offset % NSEC_PER_SEC;
+
+		if (offset < 0 && tmx.time.tv_usec) {
+			tmx.time.tv_sec -= 1;
+			tmx.time.tv_usec += NSEC_PER_SEC;
+		}
+	} else {
+		tmx.time.tv_sec = offset / USEC_PER_SEC;
+		tmx.time.tv_usec = offset % USEC_PER_SEC;
+
+		if (offset < 0 && tmx.time.tv_usec) {
+			tmx.time.tv_sec -= 1;
+			tmx.time.tv_usec += USEC_PER_SEC;
+		}
+	}
+
+	ret = clock_adjtime(CLOCK_REALTIME, &tmx);
+	if (ret < 0) {
+		printf("(sec: %ld  usec: %ld) ", tmx.time.tv_sec, tmx.time.tv_usec);
+		printf("[FAIL]\n");
+		return -1;
+	}
+	return 0;
+}
+
+int set_bad_offset(long sec, long usec, int use_nano)
+{
+	struct timex tmx = {};
+	int ret;
+
+	tmx.modes = ADJ_SETOFFSET;
+	if (use_nano)
+		tmx.modes |= ADJ_NANO;
+
+	tmx.time.tv_sec = sec;
+	tmx.time.tv_usec = usec;
+	ret = clock_adjtime(CLOCK_REALTIME, &tmx);
+	if (ret >= 0) {
+		printf("Invalid (sec: %ld  usec: %ld) did not fail! ", tmx.time.tv_sec, tmx.time.tv_usec);
+		printf("[FAIL]\n");
+		return -1;
+	}
+	return 0;
+}
+
+int validate_set_offset(void)
+{
+	printf("Testing ADJ_SETOFFSET... ");
+
+	/* Test valid values */
+	if (set_offset(NSEC_PER_SEC - 1, 1))
+		return -1;
+
+	if (set_offset(-NSEC_PER_SEC + 1, 1))
+		return -1;
+
+	if (set_offset(-NSEC_PER_SEC - 1, 1))
+		return -1;
+
+	if (set_offset(5 * NSEC_PER_SEC, 1))
+		return -1;
+
+	if (set_offset(-5 * NSEC_PER_SEC, 1))
+		return -1;
+
+	if (set_offset(5 * NSEC_PER_SEC + NSEC_PER_SEC / 2, 1))
+		return -1;
+
+	if (set_offset(-5 * NSEC_PER_SEC - NSEC_PER_SEC / 2, 1))
+		return -1;
+
+	if (set_offset(USEC_PER_SEC - 1, 0))
+		return -1;
+
+	if (set_offset(-USEC_PER_SEC + 1, 0))
+		return -1;
+
+	if (set_offset(-USEC_PER_SEC - 1, 0))
+		return -1;
+
+	if (set_offset(5 * USEC_PER_SEC, 0))
+		return -1;
+
+	if (set_offset(-5 * USEC_PER_SEC, 0))
+		return -1;
+
+	if (set_offset(5 * USEC_PER_SEC + USEC_PER_SEC / 2, 0))
+		return -1;
+
+	if (set_offset(-5 * USEC_PER_SEC - USEC_PER_SEC / 2, 0))
+		return -1;
+
+	/* Test invalid values */
+	if (set_bad_offset(0, -1, 1))
+		return -1;
+	if (set_bad_offset(0, -1, 0))
+		return -1;
+	if (set_bad_offset(0, 2 * NSEC_PER_SEC, 1))
+		return -1;
+	if (set_bad_offset(0, 2 * USEC_PER_SEC, 0))
+		return -1;
+	if (set_bad_offset(0, NSEC_PER_SEC, 1))
+		return -1;
+	if (set_bad_offset(0, USEC_PER_SEC, 0))
+		return -1;
+	if (set_bad_offset(0, -NSEC_PER_SEC, 1))
+		return -1;
+	if (set_bad_offset(0, -USEC_PER_SEC, 0))
+		return -1;
+
+	printf("[OK]\n");
+	return 0;
+}
+
 int main(int argc, char **argv)
 {
 	if (validate_freq())
 		return ksft_exit_fail();
 
+	if (validate_set_offset())
+		return ksft_exit_fail();
+
 	return ksft_exit_pass();
 }

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

end of thread, other threads:[~2016-01-26 15:40 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-01-21 23:03 [PATCH 0/2] Fix for ADJ_SETOFFSET w/ ADJ_NANO John Stultz
2016-01-21 23:03 ` [PATCH 1/2] ntp: Fix ADJ_SETOFFSET being used " John Stultz
2016-01-22 11:06   ` David Herrmann
2016-01-22 11:06   ` [tip:timers/urgent] " tip-bot for John Stultz
2016-01-21 23:03 ` [PATCH 2/2] kselftests: timers: Add adjtimex SETOFFSET validity tests John Stultz
2016-01-26 15:39   ` [tip:timers/urgent] " tip-bot for John Stultz
2016-01-21 23:16 ` [PATCH 0/2] Fix for ADJ_SETOFFSET w/ ADJ_NANO Shuah Khan
2016-01-22  7:54   ` Thomas Gleixner
2016-01-22 14:56     ` Shuah Khan

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.