From: NeilBrown Date: Wed, 17 Feb 2010 16:53:34 +1100 Subject: [PATCH] sunrpc: use monotonic time in expiry cache Patch-mainline: Submitted for 2.6.34 References: bnc#578668 this protects us from confusion when the wallclock time changes. We convert to and from wallclock when setting or reading expiry times. Signed-off-by: NeilBrown Acked-by: NeilBrown --- fs/nfs/dns_resolve.c | 6 +++--- fs/nfsd/export.c | 9 +++------ include/linux/sunrpc/cache.h | 23 ++++++++++++++++++++++- net/sunrpc/cache.c | 35 +++++++++++++++++++---------------- 4 files changed, 47 insertions(+), 26 deletions(-) --- a/fs/nfs/dns_resolve.c +++ b/fs/nfs/dns_resolve.c @@ -143,7 +143,7 @@ static int nfs_dns_show(struct seq_file return 0; } item = container_of(h, struct nfs_dns_ent, h); - ttl = (long)item->h.expiry_time - (long)get_seconds(); + ttl = (long)item->h.expiry_time - (long)monotonic_seconds(); if (ttl < 0) ttl = 0; @@ -215,7 +215,7 @@ static int nfs_dns_parse(struct cache_de ttl = get_expiry(&buf); if (ttl == 0) goto out; - key.h.expiry_time = ttl + get_seconds(); + key.h.expiry_time = ttl + monotonic_seconds(); ret = -ENOMEM; item = nfs_dns_lookup(cd, &key); @@ -277,7 +277,7 @@ static int do_cache_lookup_nowait(struct goto out_err; ret = -ETIMEDOUT; if (!test_bit(CACHE_VALID, &(*item)->h.flags) - || (*item)->h.expiry_time < get_seconds() + || (*item)->h.expiry_time < monotonic_seconds() || cd->flush_time > (*item)->h.last_refresh) goto out_put; ret = -ENOENT; --- a/fs/nfsd/export.c +++ b/fs/nfsd/export.c @@ -946,10 +946,9 @@ static void exp_fsid_unhash(struct svc_e ek = exp_get_fsid_key(exp->ex_client, exp->ex_fsid); if (!IS_ERR(ek)) { - ek->h.expiry_time = get_seconds()-1; + sunrpc_invalidate(&ek->h, &svc_expkey_cache); cache_put(&ek->h, &svc_expkey_cache); } - svc_expkey_cache.nextcheck = get_seconds(); } static int exp_fsid_hash(svc_client *clp, struct svc_export *exp) @@ -984,10 +983,9 @@ static void exp_unhash(struct svc_export ek = exp_get_key(exp->ex_client, inode->i_sb->s_dev, inode->i_ino); if (!IS_ERR(ek)) { - ek->h.expiry_time = get_seconds()-1; + sunrpc_invalidate(&ek->h, &svc_expkey_cache); cache_put(&ek->h, &svc_expkey_cache); } - svc_expkey_cache.nextcheck = get_seconds(); } /* @@ -1108,8 +1106,7 @@ out: static void exp_do_unexport(svc_export *unexp) { - unexp->h.expiry_time = get_seconds()-1; - svc_export_cache.nextcheck = get_seconds(); + sunrpc_invalidate(&unexp->h, &svc_export_cache); exp_unhash(unexp); exp_fsid_unhash(unexp); } --- a/include/linux/sunrpc/cache.h +++ b/include/linux/sunrpc/cache.h @@ -220,14 +220,35 @@ static inline int get_int(char **bpp, in return 0; } +/* + * timestamps kept in the cache are expressed in seconds + * since boot. This is the best for measuring differences in + * real time. + */ +static inline unsigned long monotonic_seconds(void) +{ + struct timespec boot; + getboottime(&boot); + return get_seconds() - boot.tv_sec; +} + static inline time_t get_expiry(char **bpp) { int rv; + struct timespec boot; + if (get_int(bpp, &rv)) return 0; if (rv < 0) return 0; - return rv; + getboottime(&boot); + return rv - boot.tv_sec; } +static inline void sunrpc_invalidate(struct cache_head *h, + struct cache_detail *detail) +{ + h->expiry_time = monotonic_seconds() - 1; + detail->nextcheck = monotonic_seconds(); +} #endif /* _LINUX_SUNRPC_CACHE_H_ */ --- a/net/sunrpc/cache.c +++ b/net/sunrpc/cache.c @@ -41,7 +41,7 @@ static void cache_revisit_request(struct static void cache_init(struct cache_head *h) { - time_t now = get_seconds(); + time_t now = monotonic_seconds(); h->next = NULL; h->flags = 0; kref_init(&h->ref); @@ -108,7 +108,7 @@ static void cache_dequeue(struct cache_d static void cache_fresh_locked(struct cache_head *head, time_t expiry) { head->expiry_time = expiry; - head->last_refresh = get_seconds(); + head->last_refresh = monotonic_seconds(); set_bit(CACHE_VALID, &head->flags); } @@ -184,7 +184,7 @@ static int cache_make_upcall(struct cach static inline int cache_is_valid(struct cache_detail *detail, struct cache_head *h) { if (!test_bit(CACHE_VALID, &h->flags) || - h->expiry_time < get_seconds()) + h->expiry_time < monotonic_seconds()) return -EAGAIN; else if (detail->flush_time > h->last_refresh) return -EAGAIN; @@ -222,7 +222,7 @@ int cache_check(struct cache_detail *det /* now see if we want to start an upcall */ refresh_age = (h->expiry_time - h->last_refresh); - age = get_seconds() - h->last_refresh; + age = monotonic_seconds() - h->last_refresh; if (rqstp == NULL) { if (rv == -EAGAIN) @@ -237,7 +237,7 @@ int cache_check(struct cache_detail *det cache_revisit_request(h); if (rv == -EAGAIN) { set_bit(CACHE_NEGATIVE, &h->flags); - cache_fresh_locked(h, get_seconds()+CACHE_NEW_EXPIRY); + cache_fresh_locked(h, monotonic_seconds()+CACHE_NEW_EXPIRY); cache_fresh_unlocked(h, detail); rv = -ENOENT; } @@ -372,11 +372,11 @@ static int cache_clean(void) return -1; } current_detail = list_entry(next, struct cache_detail, others); - if (current_detail->nextcheck > get_seconds()) + if (current_detail->nextcheck > monotonic_seconds()) current_index = current_detail->hash_size; else { current_index = 0; - current_detail->nextcheck = get_seconds()+30*60; + current_detail->nextcheck = monotonic_seconds()+30*60; } } @@ -401,7 +401,7 @@ static int cache_clean(void) for (; ch; cp= & ch->next, ch= *cp) { if (current_detail->nextcheck > ch->expiry_time) current_detail->nextcheck = ch->expiry_time+1; - if (ch->expiry_time >= get_seconds() && + if (ch->expiry_time >= monotonic_seconds() && ch->last_refresh >= current_detail->flush_time) continue; if (test_and_clear_bit(CACHE_PENDING, &ch->flags)) @@ -465,7 +465,7 @@ EXPORT_SYMBOL_GPL(cache_flush); void cache_purge(struct cache_detail *detail) { detail->flush_time = LONG_MAX; - detail->nextcheck = get_seconds(); + detail->nextcheck = monotonic_seconds(); cache_flush(); detail->flush_time = 1; } @@ -1249,7 +1249,8 @@ static int c_show(struct seq_file *m, vo ifdebug(CACHE) seq_printf(m, "# expiry=%ld refcnt=%d flags=%lx\n", - cp->expiry_time, atomic_read(&cp->ref.refcount), cp->flags); + cp->expiry_time - monotonic_seconds() + get_seconds(), + atomic_read(&cp->ref.refcount), cp->flags); cache_get(cp); if (cache_check(cd, cp, NULL)) /* cache_check does a cache_put on failure */ @@ -1313,7 +1314,8 @@ static ssize_t read_flush(struct file *f unsigned long p = *ppos; size_t len; - sprintf(tbuf, "%lu\n", cd->flush_time); + sprintf(tbuf, "%lu\n", (cd->flush_time - monotonic_seconds() + + get_seconds())); len = strlen(tbuf); if (p >= len) return 0; @@ -1331,19 +1333,20 @@ static ssize_t write_flush(struct file * struct cache_detail *cd) { char tbuf[20]; - char *ep; - long flushtime; + char *bp, *ep; + if (*ppos || count > sizeof(tbuf)-1) return -EINVAL; if (copy_from_user(tbuf, buf, count)) return -EFAULT; tbuf[count] = 0; - flushtime = simple_strtoul(tbuf, &ep, 0); + simple_strtoul(tbuf, &ep, 0); if (*ep && *ep != '\n') return -EINVAL; - cd->flush_time = flushtime; - cd->nextcheck = get_seconds(); + bp = tbuf; + cd->flush_time = get_expiry(&bp); + cd->nextcheck = monotonic_seconds(); cache_flush(); *ppos += count;