use a stable clock reference in vdso vgetns
PATCH 1/1 use a stable clock reference in vdso vgetns
When using vdso services for clock_gettime, we test for the ability of a fine-grained measurement through the existance of a vread() function.
However, from the time we test it, to the time we use it, vread() reference may not be valid anymore. It happens, for example, when we change the current clocksource from one that provides vread (say tsc) to one that lacks it (say acpi_pm), in the middle of clock_gettime routine.
seqlock does not really protect us, since readers here won't stop the writers to change references. The proposed solution is to grab a copy of the clock structure early, and use it as a stable reference onwards.
Since we don't free parts of memory associated with old clocksources, we merely stop using it for a while, this is a safe thing to do.
--- arch/x86/include/asm/vgtod.h | 2 +- arch/x86/vdso/vclock_gettime.c | 51 +++++++++++++++++++++++++++------------ 2 files changed, 36 insertions(+), 17 deletions(-)
arch/x86/include/asm/vgtod.h +1 -1
@@ -13,7 +13,7 @@ struct vsyscall_gtod_data {int sysctl_enabled;struct timezone sys_tz; -struct {/* extract of a clocksource struct */ +struct vclock {/* extract of a clocksource struct */ cycle_t (*vread)(void );cycle_t cycle_last;cycle_t mask;
arch/x86/vdso/vclock_gettime.c +35 -16
104 lines not on this page · raw message
Signature · 1 line
1.7.0.1
Quoted 11 lines · Glauber Costa
When using vdso services for
clock_gettime, we test for the ability of a fine-grained measurement through the existance of avread()function.However, from the time we test it, to the time we use it,
vread()reference may not be valid anymore. It happens, for example, when we change the current clocksource from one that provides vread (say tsc) to one that lacks it (sayacpi_pm), in the middle ofclock_gettimeroutine.seqlock does not really protect us, since readers here won't stop the writers to change references. The proposed solution is to grab a copy of the clock structure early, and use it as a stable reference onwards.
Ah. Good find! The fix looks reasonable to me. However, its likely the similar code in arch/x86/kernel/vsyscall_64.c will need a similar fix.
Awhile back there was some motivation to merge the two vdso/vsyscall implementations to avoid the duplication, but my memory is failing on why that didn't happen. I feel like it had to do with complication with the way the two implementations are mapped out to userland. Even so, it seems a shared forced inline method would resolve the issue, so maybe it just fell off the todo list?
thanks
-john
Quoted 16 lines · john stultz
Quoted 11 lines · Glauber Costa
When using vdso services for
clock_gettime, we test for the ability of a fine-grained measurement through the existance of avread()function.However, from the time we test it, to the time we use it,
vread()reference may not be valid anymore. It happens, for example, when we change the current clocksource from one that provides vread (say tsc) to one that lacks it (sayacpi_pm), in the middle ofclock_gettimeroutine.seqlock does not really protect us, since readers here won't stop the writers to change references. The proposed solution is to grab a copy of the clock structure early, and use it as a stable reference onwards.
Ah. Good find! The fix looks reasonable to me. However, its likely the similar code in
arch/x86/kernel/vsyscall_64.cwill need a similar fix.
Quoted 6 lines
Awhile back there was some motivation to merge the two vdso/vsyscall implementations to avoid the duplication, but my memory is failing on why that didn't happen. I feel like it had to do with complication with the way the two implementations are mapped out to userland. Even so, it seems a shared forced inline method would resolve the issue, so maybe it just fell off the todo list?
Actually vsyscall updates seem to be covered by update_vsyscall() already. I tried hard and can't reproduce this behaviour.
Now that I am thinking, maybe we could come up something similar to vsyscall?
If we start by making vsyscall data point to the same object, we may get it for free. Or am I missing something ?
Quoted 16 lines · john stultz
Quoted 11 lines · Glauber Costa
When using vdso services for
clock_gettime, we test for the ability of a fine-grained measurement through the existance of avread()function.However, from the time we test it, to the time we use it,
vread()reference may not be valid anymore. It happens, for example, when we change the current clocksource from one that provides vread (say tsc) to one that lacks it (sayacpi_pm), in the middle ofclock_gettimeroutine.seqlock does not really protect us, since readers here won't stop the writers to change references. The proposed solution is to grab a copy of the clock structure early, and use it as a stable reference onwards.
Ah. Good find! The fix looks reasonable to me. However, its likely the similar code in
arch/x86/kernel/vsyscall_64.cwill need a similar fix.
Quoted 6 lines
Awhile back there was some motivation to merge the two vdso/vsyscall implementations to avoid the duplication, but my memory is failing on why that didn't happen. I feel like it had to do with complication with the way the two implementations are mapped out to userland. Even so, it seems a shared forced inline method would resolve the issue, so maybe it just fell off the todo list?
Ok, I just stated that vsyscall is safe because of update_vsyscall. This is just not the case. Update vsyscall will update the timer structures but leave us with the problem that it will do it regardless of any readers. What makes vsyscall "safe", is this:
vread = __vsyscall_gtod_data.clock.vread;
^
|--- saves reference to vread uses --.
if (unlikely(!__vsyscall_gtod_data.sysctl_enabled || !vread)) {
gettimeofday(tv,NULL);
return;
}
now = vread(); <=== uses againThis was, actually, my first idea on how to solve the problem for vdso. If we are always reading this inside the seqlock, it should be okay.
In the way vdso's clock_gettime is structured, however, it would involve passing down the expected seq + vread pointer down the function call chain, or wrapping all callees inside the lock. Honestly, I believe the solution I posted is cleaner than both, even having a slight difference wrt vsyscall's today implementation.
What do you think ?
Quoted 16 lines · john stultz
Quoted 11 lines · Glauber Costa
When using vdso services for
clock_gettime, we test for the ability of a fine-grained measurement through the existance of avread()function.However, from the time we test it, to the time we use it,
vread()reference may not be valid anymore. It happens, for example, when we change the current clocksource from one that provides vread (say tsc) to one that lacks it (sayacpi_pm), in the middle ofclock_gettimeroutine.seqlock does not really protect us, since readers here won't stop the writers to change references. The proposed solution is to grab a copy of the clock structure early, and use it as a stable reference onwards.
Ah. Good find! The fix looks reasonable to me. However, its likely the similar code in
arch/x86/kernel/vsyscall_64.cwill need a similar fix.
Quoted 6 lines
Awhile back there was some motivation to merge the two vdso/vsyscall implementations to avoid the duplication, but my memory is failing on why that didn't happen. I feel like it had to do with complication with the way the two implementations are mapped out to userland. Even so, it seems a shared forced inline method would resolve the issue, so maybe it just fell off the todo list?
News here?
Quoted 17 lines · Glauber Costa
Quoted 16 lines · john stultz
Quoted 11 lines · Glauber Costa
When using vdso services for
clock_gettime, we test for the ability of a fine-grained measurement through the existance of avread()function.However, from the time we test it, to the time we use it,
vread()reference may not be valid anymore. It happens, for example, when we change the current clocksource from one that provides vread (say tsc) to one that lacks it (sayacpi_pm), in the middle ofclock_gettimeroutine.seqlock does not really protect us, since readers here won't stop the writers to change references. The proposed solution is to grab a copy of the clock structure early, and use it as a stable reference onwards.
Ah. Good find! The fix looks reasonable to me. However, its likely the similar code in
arch/x86/kernel/vsyscall_64.cwill need a similar fix.
Quoted 7 lines
Quoted 6 lines
Awhile back there was some motivation to merge the two vdso/vsyscall implementations to avoid the duplication, but my memory is failing on why that didn't happen. I feel like it had to do with complication with the way the two implementations are mapped out to userland. Even so, it seems a shared forced inline method would resolve the issue, so maybe it just fell off the todo list?
News here?
Errr.. Sorry, are you waiting for me to implement this? Or did you want a comment on your earlier mail? (Apologies, I've been a bit scattered here).
I think trying to unify the two implementations would be nice if you're up to trying, but if not, go ahead and push your patch and that will fix the bug until I can get around to looking at the unification.
I don't think the unification would be *that* complicated, since its just a matter of grabbing the right reference to the mapped out data (either the __vsyscall_gtod_data or the vdso_vsyscall_gtod_data) and then just passing a pointer to the right reference to the common inlined functions).
thanks
-john
Quoted 24 lines · john stultz
Quoted 23 lines · Glauber Costa
Quoted 22 lines · john stultz
Quoted 11 lines · Glauber Costa
When using vdso services for
clock_gettime, we test for the ability of a fine-grained measurement through the existance of avread()function.However, from the time we test it, to the time we use it,
vread()reference may not be valid anymore. It happens, for example, when we change the current clocksource from one that provides vread (say tsc) to one that lacks it (sayacpi_pm), in the middle ofclock_gettimeroutine.seqlock does not really protect us, since readers here won't stop the writers to change references. The proposed solution is to grab a copy of the clock structure early, and use it as a stable reference onwards.
Ah. Good find! The fix looks reasonable to me. However, its likely the similar code in
arch/x86/kernel/vsyscall_64.cwill need a similar fix.Awhile back there was some motivation to merge the two vdso/vsyscall implementations to avoid the duplication, but my memory is failing on why that didn't happen. I feel like it had to do with complication with the way the two implementations are mapped out to userland. Even so, it seems a shared forced inline method would resolve the issue, so maybe it just fell off the todo list?
Quoted 6 lines
Quoted 1 line
News here?
Errr.. Sorry, are you waiting for me to implement this? Or did you want a comment on your earlier mail? (Apologies, I've been a bit scattered here).
np. Comments on the earlier e-mail.
I tried a bit more with that, and sent two other e-mails arguing why I thought that was the right approach. (well, the second best, just after unification)
Quoted 3 lines
I think trying to unify the two implementations would be nice if you're up to trying, but if not, go ahead and push your patch and that will fix the bug until I can get around to looking at the unification.
Unfortunately, I have other things under my radar now, so if people don't see a problem, I'd like to have this particular problem solved. The patch seems right anyway.
Quoted 35 lines · Glauber Costa
Quoted 24 lines · john stultz
Quoted 23 lines · Glauber Costa
Quoted 22 lines · john stultz
Quoted 11 lines · Glauber Costa
When using vdso services for
clock_gettime, we test for the ability of a fine-grained measurement through the existance of avread()function.However, from the time we test it, to the time we use it,
vread()reference may not be valid anymore. It happens, for example, when we change the current clocksource from one that provides vread (say tsc) to one that lacks it (sayacpi_pm), in the middle ofclock_gettimeroutine.seqlock does not really protect us, since readers here won't stop the writers to change references. The proposed solution is to grab a copy of the clock structure early, and use it as a stable reference onwards.
Ah. Good find! The fix looks reasonable to me. However, its likely the similar code in
arch/x86/kernel/vsyscall_64.cwill need a similar fix.Awhile back there was some motivation to merge the two vdso/vsyscall implementations to avoid the duplication, but my memory is failing on why that didn't happen. I feel like it had to do with complication with the way the two implementations are mapped out to userland. Even so, it seems a shared forced inline method would resolve the issue, so maybe it just fell off the todo list?
Quoted 6 lines
Quoted 1 line
News here?
Errr.. Sorry, are you waiting for me to implement this? Or did you want a comment on your earlier mail? (Apologies, I've been a bit scattered here).
np. Comments on the earlier e-mail.
I tried a bit more with that, and sent two other e-mails arguing why I thought that was the right approach. (well, the second best, just after unification)
Quoted 6 lines
Quoted 3 lines
I think trying to unify the two implementations would be nice if you're up to trying, but if not, go ahead and push your patch and that will fix the bug until I can get around to looking at the unification.
Unfortunately, I have other things under my radar now, so if people don't see a problem, I'd like to have this particular problem solved. The patch seems right anyway.
Ok. Then go ahead and push it in and I'll try to get the unification done later.
thanks
-john
Quoted 39 lines · john stultz
Quoted 38 lines · Glauber Costa
Quoted 24 lines · john stultz
Quoted 23 lines · Glauber Costa
Quoted 22 lines · john stultz
Quoted 11 lines · Glauber Costa
When using vdso services for
clock_gettime, we test for the ability of a fine-grained measurement through the existance of avread()function.However, from the time we test it, to the time we use it,
vread()reference may not be valid anymore. It happens, for example, when we change the current clocksource from one that provides vread (say tsc) to one that lacks it (sayacpi_pm), in the middle ofclock_gettimeroutine.seqlock does not really protect us, since readers here won't stop the writers to change references. The proposed solution is to grab a copy of the clock structure early, and use it as a stable reference onwards.
Ah. Good find! The fix looks reasonable to me. However, its likely the similar code in
arch/x86/kernel/vsyscall_64.cwill need a similar fix.Awhile back there was some motivation to merge the two vdso/vsyscall implementations to avoid the duplication, but my memory is failing on why that didn't happen. I feel like it had to do with complication with the way the two implementations are mapped out to userland. Even so, it seems a shared forced inline method would resolve the issue, so maybe it just fell off the todo list?
Quoted 6 lines
Quoted 1 line
News here?
Errr.. Sorry, are you waiting for me to implement this? Or did you want a comment on your earlier mail? (Apologies, I've been a bit scattered here).
np. Comments on the earlier e-mail.
I tried a bit more with that, and sent two other e-mails arguing why I thought that was the right approach. (well, the second best, just after unification)Quoted 3 lines
I think trying to unify the two implementations would be nice if you're up to trying, but if not, go ahead and push your patch and that will fix the bug until I can get around to looking at the unification.
Quoted 6 lines
Quoted 3 lines
Unfortunately, I have other things under my radar now, so if people don't see a problem, I'd like to have this particular problem solved. The patch seems right anyway.
Ok. Then go ahead and push it in and I'll try to get the unification done later.
Also, if we're integrating this, I'd expect the code in vdso to be the end result, not the other way around. This makes this patch applicable as-is, anyway.
So, as for pushing it, I don't expect any changes to it. Maintainers?