Skip to content

Commit d202cce

Browse files
neilbrownJ. Bruce Fields
authored andcommitted
sunrpc: never return expired entries in sunrpc_cache_lookup
If sunrpc_cache_lookup finds an expired entry, remove it from the cache and return a freshly created non-VALID entry instead. This ensures that we only ever get a usable entry, or an entry that will become usable once an update arrives. i.e. we will never need to repeat the lookup. This allows us to remove the 'is_expired' test from cache_check (i.e. from cache_is_valid). cache_check should never get an expired entry as 'lookup' will never return one. If it does happen - due to inconvenient timing - then just accept it as still valid, it won't be very much past it's use-by date. Signed-off-by: NeilBrown <neilb@suse.de> Signed-off-by: J. Bruce Fields <bfields@citi.umich.edu>
1 parent 2f50d8b commit d202cce

1 file changed

Lines changed: 14 additions & 3 deletions

File tree

‎net/sunrpc/cache.c‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ struct cache_head *sunrpc_cache_lookup(struct cache_detail *detail,
5959
struct cache_head *key, int hash)
6060
{
6161
struct cache_head **head, **hp;
62-
struct cache_head *new = NULL;
62+
struct cache_head *new = NULL, *freeme = NULL;
6363

6464
head = &detail->hash_table[hash];
6565

@@ -68,6 +68,9 @@ struct cache_head *sunrpc_cache_lookup(struct cache_detail *detail,
6868
for (hp=head; *hp != NULL ; hp = &(*hp)->next) {
6969
struct cache_head *tmp = *hp;
7070
if (detail->match(tmp, key)) {
71+
if (cache_is_expired(detail, tmp))
72+
/* This entry is expired, we will discard it. */
73+
break;
7174
cache_get(tmp);
7275
read_unlock(&detail->hash_lock);
7376
return tmp;
@@ -92,6 +95,13 @@ struct cache_head *sunrpc_cache_lookup(struct cache_detail *detail,
9295
for (hp=head; *hp != NULL ; hp = &(*hp)->next) {
9396
struct cache_head *tmp = *hp;
9497
if (detail->match(tmp, key)) {
98+
if (cache_is_expired(detail, tmp)) {
99+
*hp = tmp->next;
100+
tmp->next = NULL;
101+
detail->entries --;
102+
freeme = tmp;
103+
break;
104+
}
95105
cache_get(tmp);
96106
write_unlock(&detail->hash_lock);
97107
cache_put(new, detail);
@@ -104,6 +114,8 @@ struct cache_head *sunrpc_cache_lookup(struct cache_detail *detail,
104114
cache_get(new);
105115
write_unlock(&detail->hash_lock);
106116

117+
if (freeme)
118+
cache_put(freeme, detail);
107119
return new;
108120
}
109121
EXPORT_SYMBOL_GPL(sunrpc_cache_lookup);
@@ -189,8 +201,7 @@ static int cache_make_upcall(struct cache_detail *cd, struct cache_head *h)
189201

190202
static inline int cache_is_valid(struct cache_detail *detail, struct cache_head *h)
191203
{
192-
if (!test_bit(CACHE_VALID, &h->flags) ||
193-
cache_is_expired(detail, h))
204+
if (!test_bit(CACHE_VALID, &h->flags))
194205
return -EAGAIN;
195206
else {
196207
/* entry is valid */

0 commit comments

Comments
 (0)