All of lore.kernel.org
 help / color / mirror / Atom feed
From: Paulo Marques <pmarques@grupopie.com>
To: Linus Torvalds <torvalds@osdl.org>
Cc: James Bottomley <James.Bottomley@steeleye.com>,
	Andrew Morton <akpm@osdl.org>,
	johnstultz@us.ibm.com,
	Linux Kernel <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH] fix get_jiffies_64 to work on voyager
Date: Tue, 06 Jan 2004 18:29:14 +0000	[thread overview]
Message-ID: <3FFAFE7A.7030404@grupopie.com> (raw)
In-Reply-To: Pine.LNX.4.58.0401060819000.2653@home.osdl.org

Linus Torvalds wrote:

> 
> On Tue, 6 Jan 2004, James Bottomley wrote:
> 
>>			 however, there is also no need to get
>>the xtime sequence lock every time we do a jiffies_64 read, since the
>>only unstable time is when we may be updating both halves of it
>>non-atomically.  Thus, we only need the sequence lock when the bottom
>>half is zero.  This should improve the fast path of get_jiffies_64() for
>>all x86 arch's.
>>
> 
> This is wrong. There is nothing that guarantees that the read has read the 
> high bits first. 
> 


It seems to me that even if the the high bits get read first, the 64 value can 
go "backwards" in time, since it can read X from the high 32 bits, then the 
clock advance to X+1, and the low bits will read a value close to zero, 
resulting in ((X<<32)|0).


> It might have read the low bits first (and gotten 0xffffffff), and then on
> another CPU the contents were updated, and now the high bits are one
> bigger, and when you read the high bits, you get a value that is off by a
> _lot_.
> 
> And it's not just 0 and 0xffffffff in the low bits that can be problematic 
> either: if the CPU that does the "get_jiffies64()" gets an interrupt, the 
> thing may be off by more than a count of one.
> 
> IF this optimization is really worth it, the code should be something like 
> this:
> 
> 	#define JIFFY_SLOP (3)
> 
> 	u64 ret = jiffies_64;

> 	u32 low32 = ret;
> 
> 	if ((low+JIFFY_SLOP) <= JIFFY_SLOP*2) {
> 		... do the seqlock thing ...
> 	}
> 
> instead.
> 


What about this instead? I don't like very much this kind of construction, but 
it seems that it would prevent the lock altogether:


	u32 jiff_high1, jiff_high2, jiff_low

	do {
		jiff_high1 = ((volatile) jiffies_64) >> 32;
		jiff_low = ((volatile) jiffies_64);
		jiff_high2 = ((volatile) jiffies_64) >> 32;
	}
	while(jiff_high1 != jiff_high2);

	return (jiff_high1<<32) | jiff_low;

If there is anyway to avoid the volatiles there, it would be much cleaner.


Please don't beat me too hard if there is an obvious problem with this 
implementation.

This is an old trick that was used on a brain dead DOS API, where there were 2 
functions to read date and time, and there was hard to get a real timestamp :)


-- 
Paulo Marques - www.grupopie.com

"In a world without walls and fences who needs windows and gates?"


  reply	other threads:[~2004-01-06 18:31 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-01-06 16:04 [PATCH] fix get_jiffies_64 to work on voyager James Bottomley
2004-01-06 16:19 ` Andrew Morton
2004-01-06 16:26   ` James Bottomley
2004-01-06 17:05     ` Andrew Morton
2004-01-06 18:53       ` James Bottomley
2004-01-06 16:29   ` Linus Torvalds
2004-01-06 17:05     ` Andrew Morton
2004-01-06 17:11     ` Tim Schmielau
2004-01-06 17:22       ` Andrew Morton
2004-01-06 17:37         ` Tim Schmielau
2004-01-06 18:02       ` Linus Torvalds
2004-01-06 16:30   ` Tim Schmielau
2004-01-06 16:26 ` Linus Torvalds
2004-01-06 18:29   ` Paulo Marques [this message]
2004-01-06 18:46     ` Linus Torvalds
2004-01-06 19:04       ` Paulo Marques

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=3FFAFE7A.7030404@grupopie.com \
    --to=pmarques@grupopie.com \
    --cc=James.Bottomley@steeleye.com \
    --cc=akpm@osdl.org \
    --cc=johnstultz@us.ibm.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=torvalds@osdl.org \
    /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 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.