Re: [RFC] time: xtime_lock is held too long

Eric Dumazet
2011-05-06T17:42:54Z

Le vendredi 06 mai 2011 à 18:59 +0200, Andi Kleen a écrit :

> If you have a better way to make it faster please share it.

Ideally we could use RCU :)

Have whatever state hold in one structure (possibly big, it doesnt
matter) and switch pointer in writer once everything is setup in new
structure.

struct {
struct timespec xtime;
struct timespec wall_to_monotonic;
...
} time_keep_blob;

struct time_keep_blob __rcu *xtime_cur;

ktime_t ktime_get(void)
{
const struct time_keep_blob *xp;
s64 secs, nsecs;

rcu_read_lock();
xp = rcu_dereference(xtime_cur);
secs = xp->xtime.tv_sec + xp->wall_to_monotonic.tv_sec;
nsecs = xp->xtime.tv_nsec + xp->wall_to_monotonic.tv_nsec;
nsecs += timekeeping_get_ns(xp);
rcu_read_unlock();

return ktime_add_ns(ktime_set(secs, 0), nsecs);
}

I dont know timekeeping details, maybe its necessary to loop if
xtime_cur changes :

ktime_t ktime_get(void)
{
const struct time_keep_blob *xp;
s64 secs, nsecs;

rcu_read_lock();
do {
xp = rcu_dereference(xtime_cur);
secs = xp->xtime.tv_sec + xp->wall_to_monotonic.tv_sec;
nsecs = xp->xtime.tv_nsec + xp->wall_to_monotonic.tv_nsec;
nsecs += timekeeping_get_ns(xp);
} while (rcu_dereference(xtime_cur) != xp);
rcu_read_unlock();
return ktime_add_ns(ktime_set(secs, 0), nsecs);
}