Skip to contentSkip to conversation

time/fs - file's time race with vgettimeofday

PATCH In review Jiri Olsa 3 participants 10 messages Jul 2, 2010 – Jul 7, 2010

Subsystems (4)
lore
Share & tools
lore

Jiri Olsa

author
#0 lore raw

PATCH 1/1 time/fs - file's time race with vgettimeofday

hi,

there's a race among calling gettimeofday(2) and a file's time updates. Following test program expose the race.

run it in the while loop

	while [ 1 ]; do ./test1 || break; done

--- SNIP ---
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>

int main (void)

{
	struct stat st;
	struct timeval tv;

	unlink("./file");

	gettimeofday(&tv, NULL);

	if (-1 == creat("./file", O_RDWR)) {
		perror("creat");
		return -1;
	}

	if (stat("./file", &st) != 0) {
		perror("stat");
		return -1;
	}

	printf("USER gtod: %ld\n", (long)tv.tv_sec);
	printf("USER file: %ld.%09u\n",
			(long) st.st_mtime,
			(unsigned) st.st_mtim.tv_nsec);

	return tv.tv_sec <= st.st_mtime ? 0 : -1;
}

--- SNIP ---

The point is that the stat call returns time that
sometime precedes time from gettimeofday.

The reason follows.

- inode's time is initialized by CURRENT_TIME_SEC macro, which returns tv_sec portion of xtime variable
- the xtime is updated by update_wall_time being called each tick (not that often for NO_HZ)
- vgettimeofday reads the actuall clocksource tick and computes the time

Thus while the inode's time is based on the xtime update by the update_wall_time function, the vgettimeofday computes the time correctly each time it's called.

Thus the race is triggered within 2 update_wall_time updates, when in between the gettimeofday and creat calls happened.

ticks CPU update_wall_time executed
-------------------------------------------------------------------------------
t1
update 1
(xtime is computed based on tick t1)

t2

       |         gettimeofday           |
       | (returns time based on tick 2) |
       |                                |
       |            creat               |
       |   (set time based on tick 1)   |

update 2
(xtime is computed based on tick t2)
t3
-------------------------------------------------------------------------------

This issue was described in the BZ 244697

	Time goes backward - gettimeofday() vs. rename()
	https://bugzilla.redhat.com/show_bug.cgi?id=244697

It's not just issue of the creat but few others like rename.

The following patch will prevent the race by adding the CURRENT_TIME_SEC_REAL macro, which will return seconds from the getnstimeofday call, ensuring it's computed on current tick. It fixes the 'creat' case for ext4.

I'm not sure how much trouble is having this race unfixed compared to the performance impact the fix might have. Maybe there're better ways to fix this.

thanks for any ideas,
jirka

---

fs/ext2/ialloc.c +1 -1
index 938dbc7..7a0a2fc 100644
@@ -558,7 +558,7 @@ got:
 
 	inode->i_ino = ino;
 	inode->i_blocks = 0;
-	inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC;
+	inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC_REAL;
 	memset(ei->i_data, 0, sizeof(ei->i_data));
 	ei->i_flags =
 		ext2_mask_flags(mode, EXT2_I(dir)->i_flags & EXT2_FL_INHERITED);
fs/ext4/ext4.h +1 -1
index 19a4de5..2c2925c 100644
@@ -1157,7 +1157,7 @@ static inline struct ext4_inode_info *EXT4_I(struct inode *inode)
 static inline struct timespec ext4_current_time(struct inode *inode)
 {
 	return (inode->i_sb->s_time_gran < NSEC_PER_SEC) ?
-		current_fs_time(inode->i_sb) : CURRENT_TIME_SEC;
+		current_fs_time(inode->i_sb) : CURRENT_TIME_SEC_REAL;
 }
 
 static inline int ext4_valid_inum(struct super_block *sb, unsigned long ino)
include/linux/time.h +2 -0
index ea3559f..f390687 100644
@@ -109,12 +109,14 @@ void timekeeping_init(void);
 extern int timekeeping_suspended;
 
 unsigned long get_seconds(void);
+unsigned long get_seconds_real(void);
 struct timespec current_kernel_time(void);
 struct timespec __current_kernel_time(void); /* does not hold xtime_lock */
 struct timespec get_monotonic_coarse(void);
 
 #define CURRENT_TIME		(current_kernel_time())
 #define CURRENT_TIME_SEC	((struct timespec) { get_seconds(), 0 })
+#define CURRENT_TIME_SEC_REAL	((struct timespec) { get_seconds_real(), 0 })
 
 /* Some architectures do not supply their own clocksource.
  * This is mainly the case in architectures that get their
kernel/time.c +2 -1
index 848b1c2..ce10dae 100644
@@ -227,7 +227,8 @@ SYSCALL_DEFINE1(adjtimex, struct timex __user *, txc_p)
  */
 struct timespec current_fs_time(struct super_block *sb)
 {
-	struct timespec now = current_kernel_time();
+	struct timespec now;
+	getnstimeofday(&now);
 	return timespec_trunc(now, sb->s_time_gran);
 }
 EXPORT_SYMBOL(current_fs_time);
kernel/time/timekeeping.c +8 -0
index caf8d4d..7ebfe23 100644
@@ -897,6 +897,14 @@ unsigned long get_seconds(void)
 }
 EXPORT_SYMBOL(get_seconds);
 
+unsigned long get_seconds_real(void)
+{
+	struct timespec ts;
+	getnstimeofday(&ts);
+	return ts.tv_sec;
+}
+EXPORT_SYMBOL(get_seconds_real);
+
 struct timespec __current_kernel_time(void)
 {
 	return xtime;
#1 lore raw

In no way I can review this patch, but I am curious and have the questions. Also, I think it makes sense to cc the fs/ developers, I've added Al.

Quoted 31 lines · Jiri Olsa

there's a race among calling gettimeofday(2) and a file's time updates. Following test program expose the race.

run it in the while loop

	while [ 1 ]; do ./test1 || break; done

--- SNIP ---
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>

int main (void)

{
	struct stat st;
	struct timeval tv;

	unlink("./file");

	gettimeofday(&tv, NULL);

	if (-1 == creat("./file", O_RDWR)) {
		perror("creat");
		return -1;
	}

	if (stat("./file", &st) != 0) {
		perror("stat");
		return -1;
	}
Quoted 7 lines
	printf("USER gtod: %ld\n", (long)tv.tv_sec);
	printf("USER file: %ld.%09u\n",
			(long) st.st_mtime,
			(unsigned) st.st_mtim.tv_nsec);

	return tv.tv_sec <= st.st_mtime ? 0 : -1;
}

Interesting. To the point, I actually compiled this code and yes, it triggers the problem on ext3 ;)

Quoted 4 lines

The following patch will prevent the race by adding the CURRENT_TIME_SEC_REAL macro, which will return seconds from the getnstimeofday call, ensuring it's computed on current tick. It fixes the 'creat' case for ext4.

What about other filesystems? Perhaps it makes sense to change CURRENT_TIME_SEC instead of adding CURRENT_TIME_SEC_REAL?

Once again, I am asking. It is not that I suggest to change your patch.

But there is something I can't understand.

We have

	#define CURRENT_TIME_SEC	((struct timespec) { get_seconds(), 0 })

and your patch adds

	#define CURRENT_TIME_SEC_REAL	((struct timespec) { get_seconds_real(), 0 })

which fixes the problem for ext4.

But, get_seconds() is also used by sys_time(), and we should have the same problem with another trivial test-case:

	#include <stdio.h>
	#include <sys/time.h>
	#include <time.h>

	int main(void)
	{
		struct timeval tv;
		int sec;

		do {
			gettimeofday(&tv, NULL);
			sec = time(NULL);
		} while (tv.tv_sec <= sec);

		printf("gtod: %ld.%06ld\n", tv.tv_sec, tv.tv_usec);
		printf("time: %d.000000\n", sec);
		return 0;
	}

However, this test-case can't trigger the problem. Confused.

Oleg.

Quoted 91 lines

I'm not sure how much trouble is having this race unfixed compared to the performance impact the fix might have. Maybe there're better ways to fix this.

thanks for any ideas,
jirka

---

fs/ext2/ialloc.c +1 -1
index 938dbc7..7a0a2fc 100644
@@ -558,7 +558,7 @@ got:
 
 	inode->i_ino = ino;
 	inode->i_blocks = 0;
-	inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC;
+	inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC_REAL;
 	memset(ei->i_data, 0, sizeof(ei->i_data));
 	ei->i_flags =
 		ext2_mask_flags(mode, EXT2_I(dir)->i_flags & EXT2_FL_INHERITED);
fs/ext4/ext4.h +1 -1
index 19a4de5..2c2925c 100644
@@ -1157,7 +1157,7 @@ static inline struct ext4_inode_info *EXT4_I(struct inode *inode)
 static inline struct timespec ext4_current_time(struct inode *inode)
 {
 	return (inode->i_sb->s_time_gran < NSEC_PER_SEC) ?
-		current_fs_time(inode->i_sb) : CURRENT_TIME_SEC;
+		current_fs_time(inode->i_sb) : CURRENT_TIME_SEC_REAL;
 }
 
 static inline int ext4_valid_inum(struct super_block *sb, unsigned long ino)
include/linux/time.h +2 -0
index ea3559f..f390687 100644
@@ -109,12 +109,14 @@ void timekeeping_init(void);
 extern int timekeeping_suspended;
 
 unsigned long get_seconds(void);
+unsigned long get_seconds_real(void);
 struct timespec current_kernel_time(void);
 struct timespec __current_kernel_time(void); /* does not hold xtime_lock */
 struct timespec get_monotonic_coarse(void);
 
 #define CURRENT_TIME		(current_kernel_time())
 #define CURRENT_TIME_SEC	((struct timespec) { get_seconds(), 0 })
+#define CURRENT_TIME_SEC_REAL	((struct timespec) { get_seconds_real(), 0 })
 
 /* Some architectures do not supply their own clocksource.
  * This is mainly the case in architectures that get their
kernel/time.c +2 -1
index 848b1c2..ce10dae 100644
@@ -227,7 +227,8 @@ SYSCALL_DEFINE1(adjtimex, struct timex __user *, txc_p)
  */
 struct timespec current_fs_time(struct super_block *sb)
 {
-	struct timespec now = current_kernel_time();
+	struct timespec now;
+	getnstimeofday(&now);
 	return timespec_trunc(now, sb->s_time_gran);
 }
 EXPORT_SYMBOL(current_fs_time);
kernel/time/timekeeping.c +8 -0
index caf8d4d..7ebfe23 100644
@@ -897,6 +897,14 @@ unsigned long get_seconds(void)
 }
 EXPORT_SYMBOL(get_seconds);
 
+unsigned long get_seconds_real(void)
+{
+	struct timespec ts;
+	getnstimeofday(&ts);
+	return ts.tv_sec;
+}
+EXPORT_SYMBOL(get_seconds_real);
+
 struct timespec __current_kernel_time(void)
 {
 	return xtime;
#2 lore raw
Quoted 23 lines · Oleg Nesterov

But, get_seconds() is also used by sys_time(), and we should have the same problem with another trivial test-case:

	#include <stdio.h>
	#include <sys/time.h>
	#include <time.h>

	int main(void)
	{
		struct timeval tv;
		int sec;

		do {
			gettimeofday(&tv, NULL);
			sec = time(NULL);
		} while (tv.tv_sec <= sec);

		printf("gtod: %ld.%06ld\n", tv.tv_sec, tv.tv_usec);
		printf("time: %d.000000\n", sec);
		return 0;
	}
Quoted 1 line

However, this test-case can't trigger the problem. Confused.

Aha. libc's time() doesn't use sys_time(), it uses __vsyscall(1) vtime()->do_vgettimeofday().

This one quickly triggers the "time goes backward" case.

	#include <stdio.h>
	#include <sys/time.h>
	#include <time.h>
	#include <unistd.h>
	#include <sys/syscall.h>

	int main(void)
	{
		struct timeval tv;
		int sec;

		do {
			gettimeofday(&tv, NULL);
			sec = syscall(__NR_time, NULL);
		} while (tv.tv_sec <= sec);

		printf("gtod: %ld.%06ld\n", tv.tv_sec, tv.tv_usec);
		printf("time: %d.000000\n", sec);
		return 0;
	}

Not that I think this "problem" should be fixed, just curious.

Oleg.

author
#3 lore raw
In reply to Oleg Nesterov
Quoted 2 lines · Oleg Nesterov

In no way I can review this patch, but I am curious and have the questions. Also, I think it makes sense to cc the fs/ developers, I've added Al.

thanks

Quoted 49 lines · Jiri Olsa
Quoted 31 lines

there's a race among calling gettimeofday(2) and a file's time updates. Following test program expose the race.

run it in the while loop

	while [ 1 ]; do ./test1 || break; done

--- SNIP ---
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>

int main (void)

{
	struct stat st;
	struct timeval tv;

	unlink("./file");

	gettimeofday(&tv, NULL);

	if (-1 == creat("./file", O_RDWR)) {
		perror("creat");
		return -1;
	}

	if (stat("./file", &st) != 0) {
		perror("stat");
		return -1;
	}
Quoted 7 lines
	printf("USER gtod: %ld\n", (long)tv.tv_sec);
	printf("USER file: %ld.%09u\n",
			(long) st.st_mtime,
			(unsigned) st.st_mtim.tv_nsec);

	return tv.tv_sec <= st.st_mtime ? 0 : -1;
}

Interesting. To the point, I actually compiled this code and yes, it triggers the problem on ext3 ;)

Quoted 4 lines

The following patch will prevent the race by adding the CURRENT_TIME_SEC_REAL macro, which will return seconds from the getnstimeofday call, ensuring it's computed on current tick. It fixes the 'creat' case for ext4.

Quoted 4 lines

What about other filesystems? Perhaps it makes sense to change CURRENT_TIME_SEC instead of adding CURRENT_TIME_SEC_REAL?

Once again, I am asking. It is not that I suggest to change your patch.

well, the patch is more or less to prove the problem exists and it could be fixed :) also I'm not sure that all the places using CURRENT_TIME_SEC suffer the same issue..

I'm currenty looking to the code and trying to come up with better solution.. any ideas are welcome :)

jirka

#4 lore raw
In reply to Jiri Olsa
Quoted 101 lines · Jiri Olsa

hi,

there's a race among calling gettimeofday(2) and a file's time updates.  Following test program expose the race.

run it in the while loop
       while [ 1 ]; do ./test1 || break; done

--- SNIP ---
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>

int main (void)
{
       struct stat st;
       struct timeval tv;

       unlink("./file");

       gettimeofday(&tv, NULL);

       if (-1 == creat("./file", O_RDWR)) {
               perror("creat");
               return -1;
       }

       if (stat("./file", &st) != 0) {
               perror("stat");
               return -1;
       }

       printf("USER gtod: %ld\n", (long)tv.tv_sec); printf("USER file: %ld.%09u\n",
                       (long) st.st_mtime, (unsigned) st.st_mtim.tv_nsec);

       return tv.tv_sec <= st.st_mtime ? 0 : -1;
}
--- SNIP ---

The point is that the stat call returns time that
sometime precedes time from gettimeofday.

The reason follows.

       - inode's time is initialized by CURRENT_TIME_SEC macro, which returns tv_sec portion of xtime variable
       - the xtime is updated by update_wall_time being called each tick (not that often for NO_HZ)
       - vgettimeofday reads the actuall clocksource tick and computes the time

Thus while the inode's time is based on the xtime update by the update_wall_time function, the vgettimeofday computes the time correctly each time it's called.

Thus the race is triggered within 2 update_wall_time updates, when in between the gettimeofday and creat calls happened.

ticks                   CPU                   update_wall_time executed
-------------------------------------------------------------------------------
 t1
                                                       update 1 (xtime is computed based on tick t1)

 t2

      |         gettimeofday           |
      | (returns time based on tick 2) |
      |                                |
      |            creat               |
      |   (set time based on tick 1)   |

                                                       update 2 (xtime is computed based on tick t2) t3
-------------------------------------------------------------------------------

This issue was described in the BZ 244697

       Time goes backward - gettimeofday() vs. rename()
       https://bugzilla.redhat.com/show_bug.cgi?id=244697

It's not just issue of the creat but few others like rename.

The following patch will prevent the race by adding the CURRENT_TIME_SEC_REAL macro, which will return seconds from the getnstimeofday call, ensuring it's computed on current tick. It fixes the 'creat' case for ext4.

Quoted 3 lines

I'm not sure how much trouble is having this race unfixed compared to the performance impact the fix might have. Maybe there're better ways to fix this.

I do worry that your patch will have too much of a performance hit. I think the right fix would be in vtime().

Test patch to follow shortly.

thanks
-john

#5 lore raw
Quoted 16 lines · john stultz
Quoted 15 lines · Jiri Olsa

This issue was described in the BZ 244697

Time goes backward - gettimeofday() vs. rename()
https://bugzilla.redhat.com/show_bug.cgi?id=244697

It's not just issue of the creat but few others like rename.

The following patch will prevent the race by adding the CURRENT_TIME_SEC_REAL macro, which will return seconds from the getnstimeofday call, ensuring it's computed on current tick. It fixes the 'creat' case for ext4.

Quoted 8 lines
Quoted 3 lines

I'm not sure how much trouble is having this race unfixed compared to the performance impact the fix might have. Maybe there're better ways to fix this.

I do worry that your patch will have too much of a performance hit. I think the right fix would be in vtime().

Test patch to follow shortly.

So the following (untested) patch _should_ resolve this in mainline on x86. Please let me know if it works for you.

However, for older kernels, this patch won't be sufficient, as it depends on update_vsyscall getting the time at the last tick in its wall_time, and kernels that don't have the logarithmic accumulation code & remove xtime_cache patches can't be fixed so easily.

Once we get this upstream, let me know if you have any questions about how to backport this to older kernels.

thanks
-john

[PATCH] x86: Fix vtime/file timestamp inconsistencies

Due to vtime calling vgettimeofday(), its possible that an application could call time();create("stuff",O_RDRW); only to see the file's creation timestamp to be before the value returned by time.

The proper fix is to make vtime use the same time value as current_kernel_time() (which is exported via update_vsyscall) instead of vgettime().

arch/x86/kernel/vsyscall_64.c +8 -3
index 1c0c6ab..ce9a4fa 100644
@@ -169,13 +169,18 @@ int __vsyscall(0) vgettimeofday(struct timeval * tv, struct timezone * tz)
  * unlikely */
 time_t __vsyscall(1) vtime(time_t *t)
 {
-	struct timeval tv;
+	unsigned seq;
 	time_t result;
 	if (unlikely(!__vsyscall_gtod_data.sysctl_enabled))
 		return time_syscall(t);
 
-	vgettimeofday(&tv, NULL);
-	result = tv.tv_sec;
+	do {
+		seq = read_seqbegin(&__vsyscall_gtod_data.lock);
+
+		result = vsyscall_gtod_data.wall_time_sec;
+
+	} while (read_seqretry(&__vsyscall_gtod_data.lock, seq));
+
 	if (t)
 		*t = result;
 	return result;
author
#6 lore raw
Quoted 44 lines · john stultz
Quoted 16 lines · john stultz
Quoted 15 lines · Jiri Olsa

This issue was described in the BZ 244697

Time goes backward - gettimeofday() vs. rename()
https://bugzilla.redhat.com/show_bug.cgi?id=244697

It's not just issue of the creat but few others like rename.

The following patch will prevent the race by adding the CURRENT_TIME_SEC_REAL macro, which will return seconds from the getnstimeofday call, ensuring it's computed on current tick. It fixes the 'creat' case for ext4.

Quoted 8 lines
Quoted 3 lines

I'm not sure how much trouble is having this race unfixed compared to the performance impact the fix might have. Maybe there're better ways to fix this.

I do worry that your patch will have too much of a performance hit. I think the right fix would be in vtime().

Test patch to follow shortly.

So the following (untested) patch _should_ resolve this in mainline on x86. Please let me know if it works for you.

However, for older kernels, this patch won't be sufficient, as it depends on update_vsyscall getting the time at the last tick in its wall_time, and kernels that don't have the logarithmic accumulation code & remove xtime_cache patches can't be fixed so easily.

Once we get this upstream, let me know if you have any questions about how to backport this to older kernels.

thanks
-john

[PATCH] x86: Fix vtime/file timestamp inconsistencies

Quoted 7 lines

Due to vtime calling vgettimeofday(), its possible that an application could call time();create("stuff",O_RDRW); only to see the file's creation timestamp to be before the value returned by time.

The proper fix is to make vtime use the same time value as current_kernel_time() (which is exported via update_vsyscall) instead of vgettime().

hm, this would be solution for the time() call.

But I think the issue still stays for gettimeofday and clock_gettime (CLOCK_REALTIME/CLOCK_MONOTONIC) because they call vread from clocksource to get the real time.

Thats where I'm not sure if this race is that "bad", compared either to performance hit or inaccuracy of user time calls.. which are possible solutions..

jirka

Quoted 34 lines
arch/x86/kernel/vsyscall_64.c +8 -3
index 1c0c6ab..ce9a4fa 100644
@@ -169,13 +169,18 @@ int __vsyscall(0) vgettimeofday(struct timeval * tv, struct timezone * tz)
  * unlikely */
 time_t __vsyscall(1) vtime(time_t *t)
 {
-	struct timeval tv;
+	unsigned seq;
 	time_t result;
 	if (unlikely(!__vsyscall_gtod_data.sysctl_enabled))
 		return time_syscall(t);
 
-	vgettimeofday(&tv, NULL);
-	result = tv.tv_sec;
+	do {
+		seq = read_seqbegin(&__vsyscall_gtod_data.lock);
+
+		result = vsyscall_gtod_data.wall_time_sec;
+
+	} while (read_seqretry(&__vsyscall_gtod_data.lock, seq));
+
 	if (t)
 		*t = result;
 	return result;
#7 lore raw
Quoted 13 lines · Jiri Olsa
Quoted 9 lines · john stultz

[PATCH] x86: Fix vtime/file timestamp inconsistencies

Due to vtime calling vgettimeofday(), its possible that an application could call time();create("stuff",O_RDRW); only to see the file's creation timestamp to be before the value returned by time.

The proper fix is to make vtime use the same time value as current_kernel_time() (which is exported via update_vsyscall) instead of vgettime().

hm, this would be solution for the time() call.

Quoted 7 lines

But I think the issue still stays for gettimeofday and clock_gettime (CLOCK_REALTIME/CLOCK_MONOTONIC) because they call vread from clocksource to get the real time.

Thats where I'm not sure if this race is that "bad", compared either to performance hit or inaccuracy of user time calls.. which are possible solutions..

Right, so as long as the file timestamps are tick-granular (like other tick-granular interfaces: current_kernel_time(), time(), CLOCK_REALTIME_COARSE) you will have the possibility of inconsistencies against the clocksource resolution interfaces (gettimeofday(), CLOCK_REALTIME, etc).

But that is to be expected as a constraint of the granularity. So I don't really see this as an issue.

Folks may want to increase the granularity of filesystem timestamps, but that will come at the possibly very expensive cost of reading the clocksource hardware (which can have different access latencies between architectures and even machines of the same arch). I suspect its not worth it.

The concerning issue here that you pointed out are the inconsistencies could be seen between vsyscall time() and time() (or filesystem timestamps). That is a problem, and my patch should resolve that one.

thanks
-john

author
#8 lore raw
In reply to john stultz
Quoted 75 lines · john stultz
Quoted 16 lines · john stultz
Quoted 15 lines · Jiri Olsa

This issue was described in the BZ 244697

Time goes backward - gettimeofday() vs. rename()
https://bugzilla.redhat.com/show_bug.cgi?id=244697

It's not just issue of the creat but few others like rename.

The following patch will prevent the race by adding the CURRENT_TIME_SEC_REAL macro, which will return seconds from the getnstimeofday call, ensuring it's computed on current tick. It fixes the 'creat' case for ext4.

Quoted 8 lines
Quoted 3 lines

I'm not sure how much trouble is having this race unfixed compared to the performance impact the fix might have. Maybe there're better ways to fix this.

I do worry that your patch will have too much of a performance hit. I think the right fix would be in vtime().

Test patch to follow shortly.

So the following (untested) patch _should_ resolve this in mainline on x86. Please let me know if it works for you.

However, for older kernels, this patch won't be sufficient, as it depends on update_vsyscall getting the time at the last tick in its wall_time, and kernels that don't have the logarithmic accumulation code & remove xtime_cache patches can't be fixed so easily.

Once we get this upstream, let me know if you have any questions about how to backport this to older kernels.

thanks
-john

[PATCH] x86: Fix vtime/file timestamp inconsistencies

Due to vtime calling vgettimeofday(), its possible that an application could call time();create("stuff",O_RDRW); only to see the file's creation timestamp to be before the value returned by time.

The proper fix is to make vtime use the same time value as current_kernel_time() (which is exported via update_vsyscall) instead of vgettime().

arch/x86/kernel/vsyscall_64.c +1 -3
index 1c0c6ab..ce9a4fa 100644
@@ -169,13 +169,18 @@ int __vsyscall(0) vgettimeofday(struct timeval * tv, struct timezone * tz)
  * unlikely */
 time_t __vsyscall(1) vtime(time_t *t)
 {
-	struct timeval tv;
+	unsigned seq;
 	time_t result;
 	if (unlikely(!__vsyscall_gtod_data.sysctl_enabled))
 		return time_syscall(t);
 
-	vgettimeofday(&tv, NULL);
-	result = tv.tv_sec;

arch/x86/kernel/vsyscall_64.c int __vsyscall(0) vgettimeofday(struct timeval * tv, struct timezone * tz) · line 186

Quoted 10 lines
arch/x86/kernel/vsyscall_64.c +7 -0
+	do {
+		seq = read_seqbegin(&__vsyscall_gtod_data.lock);
+
+		result = vsyscall_gtod_data.wall_time_sec;
+
+	} while (read_seqretry(&__vsyscall_gtod_data.lock, seq));
+
 	if (t)
 		*t = result;
 	return result;

I think there's a typo in using "vsyscall_gtod_data" inside the vtime call it should be "__vsyscall_gtod_data" intead.

attaching changed patch

wbr,
jirka

---
[PATCH] x86: Fix vtime/file timestamp inconsistencies

Due to vtime calling vgettimeofday(), its possible that an application could call time();create("stuff",O_RDRW); only to see the file's creation timestamp to be before the value returned by time.

The proper fix is to make vtime use the same time value as current_kernel_time() (which is exported via update_vsyscall) instead of vgettime().

---

arch/x86/kernel/vsyscall_64.c +8 -3
index 1c0c6ab..dce0c3c 100644
@@ -169,13 +169,18 @@ int __vsyscall(0) vgettimeofday(struct timeval * tv, struct timezone * tz)
  * unlikely */
 time_t __vsyscall(1) vtime(time_t *t)
 {
-	struct timeval tv;
+	unsigned seq;
 	time_t result;
 	if (unlikely(!__vsyscall_gtod_data.sysctl_enabled))
 		return time_syscall(t);
 
-	vgettimeofday(&tv, NULL);
-	result = tv.tv_sec;
+	do {
+		seq = read_seqbegin(&__vsyscall_gtod_data.lock);
+
+		result = __vsyscall_gtod_data.wall_time_sec;
+
+	} while (read_seqretry(&__vsyscall_gtod_data.lock, seq));
+
 	if (t)
 		*t = result;
 	return result;
#9 lore raw
Quoted 10 lines · Jiri Olsa
Quoted 9 lines · john stultz
arch/x86/kernel/vsyscall_64.c +7 -2
-	vgettimeofday(&tv, NULL);
-	result = tv.tv_sec;
+	do {
+		seq = read_seqbegin(&__vsyscall_gtod_data.lock);
+
+		result = vsyscall_gtod_data.wall_time_sec;
+
+	} while (read_seqretry(&__vsyscall_gtod_data.lock, seq));
+
Quoted 8 lines

arch/x86/kernel/vsyscall_64.c int __vsyscall(0) vgettimeofday(struct timeval * tv, struct timezone * tz) · line 186

Quoted 5 lines
arch/x86/kernel/vsyscall_64.c +0 -0
 	if (t)
 		*t = result;
 	return result;

I think there's a typo in using "vsyscall_gtod_data" inside the vtime call it should be "__vsyscall_gtod_data" intead.

Quite right! Thanks for catching it!
-john