mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Thierry Reding <thierry.reding@gmail.com>
To: Mariusz Skamra <mariuszx.skamra@intel.com>
Cc: linux-kernel@vger.kernel.org, tglx@linutronix.de
Subject: Re: [PATCH v2] ktime: Simplify ktime_compare implementation
Date: Mon, 26 Jun 2017 08:52:04 +0200	[thread overview]
Message-ID: <20170626065204.GA31609@ulmo.fritz.box> (raw)
In-Reply-To: <1495803647-9504-1-git-send-email-mariuszx.skamra@intel.com>

[-- Attachment #1: Type: text/plain, Size: 2620 bytes --]

On Fri, May 26, 2017 at 03:00:47PM +0200, Mariusz Skamra wrote:
> ktime_sub can be used here instread of two conditional checks.
> 
> Signed-off-by: Mariusz Skamra <mariuszx.skamra@intel.com>
> Acked-by: Kuppuswamy Sathyanarayanan <sathyanarayanan.kuppuswamy@intel.com>
> ---
>  include/linux/ktime.h | 6 +-----
>  1 file changed, 1 insertion(+), 5 deletions(-)
> 
> diff --git a/include/linux/ktime.h b/include/linux/ktime.h
> index 0c8bd45..04817b1 100644
> --- a/include/linux/ktime.h
> +++ b/include/linux/ktime.h
> @@ -108,11 +108,7 @@ static inline ktime_t timeval_to_ktime(struct timeval tv)
>   */
>  static inline int ktime_compare(const ktime_t cmp1, const ktime_t cmp2)
>  {
> -	if (cmp1 < cmp2)
> -		return -1;
> -	if (cmp1 > cmp2)
> -		return 1;
> -	return 0;
> +	return ktime_sub(cmp1, cmp2);
>  }
>  
>  /**
> -- 
> 2.1.4

Boot is broken on Tegra186 on next-20170623. Bisection points at this
commit and I've confirmed that reverting it also fixes the problem.

Another fix is to change the return value of ktime_compare() to ktime_t,
though I'm not sure that'd be a good alternative because it breaks with
the traditional return type of int (-1, 0, +1) for comparison functions.

It looks like the issue is with the truncation from s64 to int, as
demonstrated by this minimal test case:

--- >8 ---
#include <stdint.h>
#include <stdio.h>

#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))

typedef int64_t s64;
typedef s64 ktime_t;

#define ktime_sub(lhs, rhs) ((lhs) - (rhs))

static inline int ktime_compare(const ktime_t cmp1, const ktime_t cmp2)
{
	return ktime_sub(cmp1, cmp2);
}

int main(int argc, char *argv[])
{
	static const ktime_t values[][2] = {
		{ 0, 1 },
		{ 1, 0 },
		{ 0, INT64_MAX },
		{ INT64_MAX, 0 },
	};
	unsigned int i;

	for (i = 0; i < ARRAY_SIZE(values); i++)
		printf("%ld - %ld = %ld (%d)\n", values[i][0], values[i][1],
		       ktime_sub(values[i][0], values[i][1]),
		       ktime_compare(values[i][0], values[i][1]));

	return 0;
}
--- >8 ---

and the following results on x86_64:

	$ gcc -O2 -g -Wall -Werror -o test test.c && ./test
	0 - 1 = -1 (-1)
	1 - 0 = 1 (1)
	0 - 9223372036854775807 = -9223372036854775807 (1)
	9223372036854775807 - 0 = 9223372036854775807 (-1)

and AArch64:

	$ aarch64-unknown-linux-gnu-gcc -O2 -g -Wall -Werror -static -o test test.c && qemu-aarch64 ./test
	0 - 1 = -1 (-1)
	1 - 0 = 1 (1)
	0 - 9223372036854775807 = -9223372036854775807 (1)
	9223372036854775807 - 0 = 9223372036854775807 (-1)

Perhaps it would be best to drop this patch?

Thierry

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

  parent reply	other threads:[~2017-06-26  6:52 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-05-26 13:00 Mariusz Skamra
2017-06-12  7:04 ` [lkp-robot] [ktime] 02c180b3d0: INFO:ltp-pan_reported_some_tests_FAIL kernel test robot
2017-06-20 19:38 ` [tip:timers/core] ktime: Simplify ktime_compare implementation tip-bot for Mariusz Skamra
2017-06-26  6:52 ` Thierry Reding [this message]
2017-06-26  8:45   ` [PATCH v2] " Thomas Gleixner

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170626065204.GA31609@ulmo.fritz.box \
    --to=thierry.reding@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mariuszx.skamra@intel.com \
    --cc=tglx@linutronix.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®