.. | .. |
---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
---|
1 | 2 | /* |
---|
2 | 3 | * net/sunrpc/cache.c |
---|
3 | 4 | * |
---|
.. | .. |
---|
5 | 6 | * used by sunrpc clients and servers. |
---|
6 | 7 | * |
---|
7 | 8 | * Copyright (C) 2002 Neil Brown <neilb@cse.unsw.edu.au> |
---|
8 | | - * |
---|
9 | | - * Released under terms in GPL version 2. See COPYING. |
---|
10 | | - * |
---|
11 | 9 | */ |
---|
12 | 10 | |
---|
13 | 11 | #include <linux/types.h> |
---|
.. | .. |
---|
34 | 32 | #include <linux/sunrpc/cache.h> |
---|
35 | 33 | #include <linux/sunrpc/stats.h> |
---|
36 | 34 | #include <linux/sunrpc/rpc_pipe_fs.h> |
---|
| 35 | +#include <trace/events/sunrpc.h> |
---|
37 | 36 | #include "netns.h" |
---|
38 | 37 | |
---|
39 | 38 | #define RPCDBG_FACILITY RPCDBG_CACHE |
---|
.. | .. |
---|
43 | 42 | |
---|
44 | 43 | static void cache_init(struct cache_head *h, struct cache_detail *detail) |
---|
45 | 44 | { |
---|
46 | | - time_t now = seconds_since_boot(); |
---|
| 45 | + time64_t now = seconds_since_boot(); |
---|
47 | 46 | INIT_HLIST_NODE(&h->cache_list); |
---|
48 | 47 | h->flags = 0; |
---|
49 | 48 | kref_init(&h->ref); |
---|
.. | .. |
---|
57 | 56 | static void cache_fresh_unlocked(struct cache_head *head, |
---|
58 | 57 | struct cache_detail *detail); |
---|
59 | 58 | |
---|
60 | | -struct cache_head *sunrpc_cache_lookup(struct cache_detail *detail, |
---|
61 | | - struct cache_head *key, int hash) |
---|
| 59 | +static struct cache_head *sunrpc_cache_find_rcu(struct cache_detail *detail, |
---|
| 60 | + struct cache_head *key, |
---|
| 61 | + int hash) |
---|
62 | 62 | { |
---|
63 | | - struct cache_head *new = NULL, *freeme = NULL, *tmp = NULL; |
---|
64 | | - struct hlist_head *head; |
---|
| 63 | + struct hlist_head *head = &detail->hash_table[hash]; |
---|
| 64 | + struct cache_head *tmp; |
---|
65 | 65 | |
---|
66 | | - head = &detail->hash_table[hash]; |
---|
67 | | - |
---|
68 | | - read_lock(&detail->hash_lock); |
---|
69 | | - |
---|
70 | | - hlist_for_each_entry(tmp, head, cache_list) { |
---|
71 | | - if (detail->match(tmp, key)) { |
---|
72 | | - if (cache_is_expired(detail, tmp)) |
---|
73 | | - /* This entry is expired, we will discard it. */ |
---|
74 | | - break; |
---|
75 | | - cache_get(tmp); |
---|
76 | | - read_unlock(&detail->hash_lock); |
---|
77 | | - return tmp; |
---|
78 | | - } |
---|
| 66 | + rcu_read_lock(); |
---|
| 67 | + hlist_for_each_entry_rcu(tmp, head, cache_list) { |
---|
| 68 | + if (!detail->match(tmp, key)) |
---|
| 69 | + continue; |
---|
| 70 | + if (test_bit(CACHE_VALID, &tmp->flags) && |
---|
| 71 | + cache_is_expired(detail, tmp)) |
---|
| 72 | + continue; |
---|
| 73 | + tmp = cache_get_rcu(tmp); |
---|
| 74 | + rcu_read_unlock(); |
---|
| 75 | + return tmp; |
---|
79 | 76 | } |
---|
80 | | - read_unlock(&detail->hash_lock); |
---|
81 | | - /* Didn't find anything, insert an empty entry */ |
---|
| 77 | + rcu_read_unlock(); |
---|
| 78 | + return NULL; |
---|
| 79 | +} |
---|
| 80 | + |
---|
| 81 | +static void sunrpc_begin_cache_remove_entry(struct cache_head *ch, |
---|
| 82 | + struct cache_detail *cd) |
---|
| 83 | +{ |
---|
| 84 | + /* Must be called under cd->hash_lock */ |
---|
| 85 | + hlist_del_init_rcu(&ch->cache_list); |
---|
| 86 | + set_bit(CACHE_CLEANED, &ch->flags); |
---|
| 87 | + cd->entries --; |
---|
| 88 | +} |
---|
| 89 | + |
---|
| 90 | +static void sunrpc_end_cache_remove_entry(struct cache_head *ch, |
---|
| 91 | + struct cache_detail *cd) |
---|
| 92 | +{ |
---|
| 93 | + cache_fresh_unlocked(ch, cd); |
---|
| 94 | + cache_put(ch, cd); |
---|
| 95 | +} |
---|
| 96 | + |
---|
| 97 | +static struct cache_head *sunrpc_cache_add_entry(struct cache_detail *detail, |
---|
| 98 | + struct cache_head *key, |
---|
| 99 | + int hash) |
---|
| 100 | +{ |
---|
| 101 | + struct cache_head *new, *tmp, *freeme = NULL; |
---|
| 102 | + struct hlist_head *head = &detail->hash_table[hash]; |
---|
82 | 103 | |
---|
83 | 104 | new = detail->alloc(); |
---|
84 | 105 | if (!new) |
---|
.. | .. |
---|
90 | 111 | cache_init(new, detail); |
---|
91 | 112 | detail->init(new, key); |
---|
92 | 113 | |
---|
93 | | - write_lock(&detail->hash_lock); |
---|
| 114 | + spin_lock(&detail->hash_lock); |
---|
94 | 115 | |
---|
95 | 116 | /* check if entry appeared while we slept */ |
---|
96 | | - hlist_for_each_entry(tmp, head, cache_list) { |
---|
97 | | - if (detail->match(tmp, key)) { |
---|
98 | | - if (cache_is_expired(detail, tmp)) { |
---|
99 | | - hlist_del_init(&tmp->cache_list); |
---|
100 | | - detail->entries --; |
---|
101 | | - freeme = tmp; |
---|
102 | | - break; |
---|
103 | | - } |
---|
104 | | - cache_get(tmp); |
---|
105 | | - write_unlock(&detail->hash_lock); |
---|
106 | | - cache_put(new, detail); |
---|
107 | | - return tmp; |
---|
| 117 | + hlist_for_each_entry_rcu(tmp, head, cache_list, |
---|
| 118 | + lockdep_is_held(&detail->hash_lock)) { |
---|
| 119 | + if (!detail->match(tmp, key)) |
---|
| 120 | + continue; |
---|
| 121 | + if (test_bit(CACHE_VALID, &tmp->flags) && |
---|
| 122 | + cache_is_expired(detail, tmp)) { |
---|
| 123 | + sunrpc_begin_cache_remove_entry(tmp, detail); |
---|
| 124 | + trace_cache_entry_expired(detail, tmp); |
---|
| 125 | + freeme = tmp; |
---|
| 126 | + break; |
---|
108 | 127 | } |
---|
| 128 | + cache_get(tmp); |
---|
| 129 | + spin_unlock(&detail->hash_lock); |
---|
| 130 | + cache_put(new, detail); |
---|
| 131 | + return tmp; |
---|
109 | 132 | } |
---|
110 | 133 | |
---|
111 | | - hlist_add_head(&new->cache_list, head); |
---|
| 134 | + hlist_add_head_rcu(&new->cache_list, head); |
---|
112 | 135 | detail->entries++; |
---|
113 | 136 | cache_get(new); |
---|
114 | | - write_unlock(&detail->hash_lock); |
---|
| 137 | + spin_unlock(&detail->hash_lock); |
---|
115 | 138 | |
---|
116 | | - if (freeme) { |
---|
117 | | - cache_fresh_unlocked(freeme, detail); |
---|
118 | | - cache_put(freeme, detail); |
---|
119 | | - } |
---|
| 139 | + if (freeme) |
---|
| 140 | + sunrpc_end_cache_remove_entry(freeme, detail); |
---|
120 | 141 | return new; |
---|
121 | 142 | } |
---|
122 | | -EXPORT_SYMBOL_GPL(sunrpc_cache_lookup); |
---|
123 | 143 | |
---|
| 144 | +struct cache_head *sunrpc_cache_lookup_rcu(struct cache_detail *detail, |
---|
| 145 | + struct cache_head *key, int hash) |
---|
| 146 | +{ |
---|
| 147 | + struct cache_head *ret; |
---|
| 148 | + |
---|
| 149 | + ret = sunrpc_cache_find_rcu(detail, key, hash); |
---|
| 150 | + if (ret) |
---|
| 151 | + return ret; |
---|
| 152 | + /* Didn't find anything, insert an empty entry */ |
---|
| 153 | + return sunrpc_cache_add_entry(detail, key, hash); |
---|
| 154 | +} |
---|
| 155 | +EXPORT_SYMBOL_GPL(sunrpc_cache_lookup_rcu); |
---|
124 | 156 | |
---|
125 | 157 | static void cache_dequeue(struct cache_detail *detail, struct cache_head *ch); |
---|
126 | 158 | |
---|
127 | | -static void cache_fresh_locked(struct cache_head *head, time_t expiry, |
---|
| 159 | +static void cache_fresh_locked(struct cache_head *head, time64_t expiry, |
---|
128 | 160 | struct cache_detail *detail) |
---|
129 | 161 | { |
---|
130 | | - time_t now = seconds_since_boot(); |
---|
| 162 | + time64_t now = seconds_since_boot(); |
---|
131 | 163 | if (now <= detail->flush_time) |
---|
132 | 164 | /* ensure it isn't immediately treated as expired */ |
---|
133 | 165 | now = detail->flush_time + 1; |
---|
.. | .. |
---|
146 | 178 | } |
---|
147 | 179 | } |
---|
148 | 180 | |
---|
| 181 | +static void cache_make_negative(struct cache_detail *detail, |
---|
| 182 | + struct cache_head *h) |
---|
| 183 | +{ |
---|
| 184 | + set_bit(CACHE_NEGATIVE, &h->flags); |
---|
| 185 | + trace_cache_entry_make_negative(detail, h); |
---|
| 186 | +} |
---|
| 187 | + |
---|
| 188 | +static void cache_entry_update(struct cache_detail *detail, |
---|
| 189 | + struct cache_head *h, |
---|
| 190 | + struct cache_head *new) |
---|
| 191 | +{ |
---|
| 192 | + if (!test_bit(CACHE_NEGATIVE, &new->flags)) { |
---|
| 193 | + detail->update(h, new); |
---|
| 194 | + trace_cache_entry_update(detail, h); |
---|
| 195 | + } else { |
---|
| 196 | + cache_make_negative(detail, h); |
---|
| 197 | + } |
---|
| 198 | +} |
---|
| 199 | + |
---|
149 | 200 | struct cache_head *sunrpc_cache_update(struct cache_detail *detail, |
---|
150 | 201 | struct cache_head *new, struct cache_head *old, int hash) |
---|
151 | 202 | { |
---|
.. | .. |
---|
156 | 207 | struct cache_head *tmp; |
---|
157 | 208 | |
---|
158 | 209 | if (!test_bit(CACHE_VALID, &old->flags)) { |
---|
159 | | - write_lock(&detail->hash_lock); |
---|
| 210 | + spin_lock(&detail->hash_lock); |
---|
160 | 211 | if (!test_bit(CACHE_VALID, &old->flags)) { |
---|
161 | | - if (test_bit(CACHE_NEGATIVE, &new->flags)) |
---|
162 | | - set_bit(CACHE_NEGATIVE, &old->flags); |
---|
163 | | - else |
---|
164 | | - detail->update(old, new); |
---|
| 212 | + cache_entry_update(detail, old, new); |
---|
165 | 213 | cache_fresh_locked(old, new->expiry_time, detail); |
---|
166 | | - write_unlock(&detail->hash_lock); |
---|
| 214 | + spin_unlock(&detail->hash_lock); |
---|
167 | 215 | cache_fresh_unlocked(old, detail); |
---|
168 | 216 | return old; |
---|
169 | 217 | } |
---|
170 | | - write_unlock(&detail->hash_lock); |
---|
| 218 | + spin_unlock(&detail->hash_lock); |
---|
171 | 219 | } |
---|
172 | 220 | /* We need to insert a new entry */ |
---|
173 | 221 | tmp = detail->alloc(); |
---|
.. | .. |
---|
178 | 226 | cache_init(tmp, detail); |
---|
179 | 227 | detail->init(tmp, old); |
---|
180 | 228 | |
---|
181 | | - write_lock(&detail->hash_lock); |
---|
182 | | - if (test_bit(CACHE_NEGATIVE, &new->flags)) |
---|
183 | | - set_bit(CACHE_NEGATIVE, &tmp->flags); |
---|
184 | | - else |
---|
185 | | - detail->update(tmp, new); |
---|
| 229 | + spin_lock(&detail->hash_lock); |
---|
| 230 | + cache_entry_update(detail, tmp, new); |
---|
186 | 231 | hlist_add_head(&tmp->cache_list, &detail->hash_table[hash]); |
---|
187 | 232 | detail->entries++; |
---|
188 | 233 | cache_get(tmp); |
---|
189 | 234 | cache_fresh_locked(tmp, new->expiry_time, detail); |
---|
190 | 235 | cache_fresh_locked(old, 0, detail); |
---|
191 | | - write_unlock(&detail->hash_lock); |
---|
| 236 | + spin_unlock(&detail->hash_lock); |
---|
192 | 237 | cache_fresh_unlocked(tmp, detail); |
---|
193 | 238 | cache_fresh_unlocked(old, detail); |
---|
194 | 239 | cache_put(old, detail); |
---|
195 | 240 | return tmp; |
---|
196 | 241 | } |
---|
197 | 242 | EXPORT_SYMBOL_GPL(sunrpc_cache_update); |
---|
198 | | - |
---|
199 | | -static int cache_make_upcall(struct cache_detail *cd, struct cache_head *h) |
---|
200 | | -{ |
---|
201 | | - if (cd->cache_upcall) |
---|
202 | | - return cd->cache_upcall(cd, h); |
---|
203 | | - return sunrpc_cache_pipe_upcall(cd, h); |
---|
204 | | -} |
---|
205 | 243 | |
---|
206 | 244 | static inline int cache_is_valid(struct cache_head *h) |
---|
207 | 245 | { |
---|
.. | .. |
---|
228 | 266 | { |
---|
229 | 267 | int rv; |
---|
230 | 268 | |
---|
231 | | - write_lock(&detail->hash_lock); |
---|
| 269 | + spin_lock(&detail->hash_lock); |
---|
232 | 270 | rv = cache_is_valid(h); |
---|
233 | 271 | if (rv == -EAGAIN) { |
---|
234 | | - set_bit(CACHE_NEGATIVE, &h->flags); |
---|
| 272 | + cache_make_negative(detail, h); |
---|
235 | 273 | cache_fresh_locked(h, seconds_since_boot()+CACHE_NEW_EXPIRY, |
---|
236 | 274 | detail); |
---|
237 | 275 | rv = -ENOENT; |
---|
238 | 276 | } |
---|
239 | | - write_unlock(&detail->hash_lock); |
---|
| 277 | + spin_unlock(&detail->hash_lock); |
---|
240 | 278 | cache_fresh_unlocked(h, detail); |
---|
241 | 279 | return rv; |
---|
242 | 280 | } |
---|
.. | .. |
---|
259 | 297 | struct cache_head *h, struct cache_req *rqstp) |
---|
260 | 298 | { |
---|
261 | 299 | int rv; |
---|
262 | | - long refresh_age, age; |
---|
| 300 | + time64_t refresh_age, age; |
---|
263 | 301 | |
---|
264 | 302 | /* First decide return status as best we can */ |
---|
265 | 303 | rv = cache_is_valid(h); |
---|
.. | .. |
---|
273 | 311 | rv = -ENOENT; |
---|
274 | 312 | } else if (rv == -EAGAIN || |
---|
275 | 313 | (h->expiry_time != 0 && age > refresh_age/2)) { |
---|
276 | | - dprintk("RPC: Want update, refage=%ld, age=%ld\n", |
---|
| 314 | + dprintk("RPC: Want update, refage=%lld, age=%lld\n", |
---|
277 | 315 | refresh_age, age); |
---|
278 | | - if (!test_and_set_bit(CACHE_PENDING, &h->flags)) { |
---|
279 | | - switch (cache_make_upcall(detail, h)) { |
---|
280 | | - case -EINVAL: |
---|
281 | | - rv = try_to_negate_entry(detail, h); |
---|
282 | | - break; |
---|
283 | | - case -EAGAIN: |
---|
284 | | - cache_fresh_unlocked(h, detail); |
---|
285 | | - break; |
---|
286 | | - } |
---|
| 316 | + switch (detail->cache_upcall(detail, h)) { |
---|
| 317 | + case -EINVAL: |
---|
| 318 | + rv = try_to_negate_entry(detail, h); |
---|
| 319 | + break; |
---|
| 320 | + case -EAGAIN: |
---|
| 321 | + cache_fresh_unlocked(h, detail); |
---|
| 322 | + break; |
---|
287 | 323 | } |
---|
288 | 324 | } |
---|
289 | 325 | |
---|
.. | .. |
---|
346 | 382 | |
---|
347 | 383 | void sunrpc_init_cache_detail(struct cache_detail *cd) |
---|
348 | 384 | { |
---|
349 | | - rwlock_init(&cd->hash_lock); |
---|
| 385 | + spin_lock_init(&cd->hash_lock); |
---|
350 | 386 | INIT_LIST_HEAD(&cd->queue); |
---|
351 | 387 | spin_lock(&cache_list_lock); |
---|
352 | 388 | cd->nextcheck = 0; |
---|
353 | 389 | cd->entries = 0; |
---|
354 | | - atomic_set(&cd->readers, 0); |
---|
| 390 | + atomic_set(&cd->writers, 0); |
---|
355 | 391 | cd->last_close = 0; |
---|
356 | 392 | cd->last_warn = -1; |
---|
357 | 393 | list_add(&cd->others, &cache_list); |
---|
.. | .. |
---|
366 | 402 | { |
---|
367 | 403 | cache_purge(cd); |
---|
368 | 404 | spin_lock(&cache_list_lock); |
---|
369 | | - write_lock(&cd->hash_lock); |
---|
| 405 | + spin_lock(&cd->hash_lock); |
---|
370 | 406 | if (current_detail == cd) |
---|
371 | 407 | current_detail = NULL; |
---|
372 | 408 | list_del_init(&cd->others); |
---|
373 | | - write_unlock(&cd->hash_lock); |
---|
| 409 | + spin_unlock(&cd->hash_lock); |
---|
374 | 410 | spin_unlock(&cache_list_lock); |
---|
375 | 411 | if (list_empty(&cache_list)) { |
---|
376 | 412 | /* module must be being unloaded so its safe to kill the worker */ |
---|
.. | .. |
---|
427 | 463 | struct hlist_head *head; |
---|
428 | 464 | struct hlist_node *tmp; |
---|
429 | 465 | |
---|
430 | | - write_lock(¤t_detail->hash_lock); |
---|
| 466 | + spin_lock(¤t_detail->hash_lock); |
---|
431 | 467 | |
---|
432 | 468 | /* Ok, now to clean this strand */ |
---|
433 | 469 | |
---|
.. | .. |
---|
438 | 474 | if (!cache_is_expired(current_detail, ch)) |
---|
439 | 475 | continue; |
---|
440 | 476 | |
---|
441 | | - hlist_del_init(&ch->cache_list); |
---|
442 | | - current_detail->entries--; |
---|
| 477 | + sunrpc_begin_cache_remove_entry(ch, current_detail); |
---|
| 478 | + trace_cache_entry_expired(current_detail, ch); |
---|
443 | 479 | rv = 1; |
---|
444 | 480 | break; |
---|
445 | 481 | } |
---|
446 | 482 | |
---|
447 | | - write_unlock(¤t_detail->hash_lock); |
---|
| 483 | + spin_unlock(¤t_detail->hash_lock); |
---|
448 | 484 | d = current_detail; |
---|
449 | 485 | if (!ch) |
---|
450 | 486 | current_index ++; |
---|
451 | 487 | spin_unlock(&cache_list_lock); |
---|
452 | | - if (ch) { |
---|
453 | | - set_bit(CACHE_CLEANED, &ch->flags); |
---|
454 | | - cache_fresh_unlocked(ch, d); |
---|
455 | | - cache_put(ch, d); |
---|
456 | | - } |
---|
| 488 | + if (ch) |
---|
| 489 | + sunrpc_end_cache_remove_entry(ch, d); |
---|
457 | 490 | } else |
---|
458 | 491 | spin_unlock(&cache_list_lock); |
---|
459 | 492 | |
---|
.. | .. |
---|
465 | 498 | */ |
---|
466 | 499 | static void do_cache_clean(struct work_struct *work) |
---|
467 | 500 | { |
---|
468 | | - int delay = 5; |
---|
469 | | - if (cache_clean() == -1) |
---|
470 | | - delay = round_jiffies_relative(30*HZ); |
---|
| 501 | + int delay; |
---|
471 | 502 | |
---|
472 | 503 | if (list_empty(&cache_list)) |
---|
473 | | - delay = 0; |
---|
| 504 | + return; |
---|
474 | 505 | |
---|
475 | | - if (delay) |
---|
476 | | - queue_delayed_work(system_power_efficient_wq, |
---|
477 | | - &cache_cleaner, delay); |
---|
| 506 | + if (cache_clean() == -1) |
---|
| 507 | + delay = round_jiffies_relative(30*HZ); |
---|
| 508 | + else |
---|
| 509 | + delay = 5; |
---|
| 510 | + |
---|
| 511 | + queue_delayed_work(system_power_efficient_wq, &cache_cleaner, delay); |
---|
478 | 512 | } |
---|
479 | 513 | |
---|
480 | 514 | |
---|
.. | .. |
---|
496 | 530 | { |
---|
497 | 531 | struct cache_head *ch = NULL; |
---|
498 | 532 | struct hlist_head *head = NULL; |
---|
499 | | - struct hlist_node *tmp = NULL; |
---|
500 | 533 | int i = 0; |
---|
501 | 534 | |
---|
502 | | - write_lock(&detail->hash_lock); |
---|
| 535 | + spin_lock(&detail->hash_lock); |
---|
503 | 536 | if (!detail->entries) { |
---|
504 | | - write_unlock(&detail->hash_lock); |
---|
| 537 | + spin_unlock(&detail->hash_lock); |
---|
505 | 538 | return; |
---|
506 | 539 | } |
---|
507 | 540 | |
---|
508 | 541 | dprintk("RPC: %d entries in %s cache\n", detail->entries, detail->name); |
---|
509 | 542 | for (i = 0; i < detail->hash_size; i++) { |
---|
510 | 543 | head = &detail->hash_table[i]; |
---|
511 | | - hlist_for_each_entry_safe(ch, tmp, head, cache_list) { |
---|
512 | | - hlist_del_init(&ch->cache_list); |
---|
513 | | - detail->entries--; |
---|
514 | | - |
---|
515 | | - set_bit(CACHE_CLEANED, &ch->flags); |
---|
516 | | - write_unlock(&detail->hash_lock); |
---|
517 | | - cache_fresh_unlocked(ch, detail); |
---|
518 | | - cache_put(ch, detail); |
---|
519 | | - write_lock(&detail->hash_lock); |
---|
| 544 | + while (!hlist_empty(head)) { |
---|
| 545 | + ch = hlist_entry(head->first, struct cache_head, |
---|
| 546 | + cache_list); |
---|
| 547 | + sunrpc_begin_cache_remove_entry(ch, detail); |
---|
| 548 | + spin_unlock(&detail->hash_lock); |
---|
| 549 | + sunrpc_end_cache_remove_entry(ch, detail); |
---|
| 550 | + spin_lock(&detail->hash_lock); |
---|
520 | 551 | } |
---|
521 | 552 | } |
---|
522 | | - write_unlock(&detail->hash_lock); |
---|
| 553 | + spin_unlock(&detail->hash_lock); |
---|
523 | 554 | } |
---|
524 | 555 | EXPORT_SYMBOL_GPL(cache_purge); |
---|
525 | 556 | |
---|
.. | .. |
---|
878 | 909 | static ssize_t cache_slow_downcall(const char __user *buf, |
---|
879 | 910 | size_t count, struct cache_detail *cd) |
---|
880 | 911 | { |
---|
881 | | - static char write_buf[8192]; /* protected by queue_io_mutex */ |
---|
| 912 | + static char write_buf[32768]; /* protected by queue_io_mutex */ |
---|
882 | 913 | ssize_t ret = -EINVAL; |
---|
883 | 914 | |
---|
884 | 915 | if (count >= sizeof(write_buf)) |
---|
.. | .. |
---|
1007 | 1038 | } |
---|
1008 | 1039 | rp->offset = 0; |
---|
1009 | 1040 | rp->q.reader = 1; |
---|
1010 | | - atomic_inc(&cd->readers); |
---|
| 1041 | + |
---|
1011 | 1042 | spin_lock(&queue_lock); |
---|
1012 | 1043 | list_add(&rp->q.list, &cd->queue); |
---|
1013 | 1044 | spin_unlock(&queue_lock); |
---|
1014 | 1045 | } |
---|
| 1046 | + if (filp->f_mode & FMODE_WRITE) |
---|
| 1047 | + atomic_inc(&cd->writers); |
---|
1015 | 1048 | filp->private_data = rp; |
---|
1016 | 1049 | return 0; |
---|
1017 | 1050 | } |
---|
.. | .. |
---|
1040 | 1073 | filp->private_data = NULL; |
---|
1041 | 1074 | kfree(rp); |
---|
1042 | 1075 | |
---|
| 1076 | + } |
---|
| 1077 | + if (filp->f_mode & FMODE_WRITE) { |
---|
| 1078 | + atomic_dec(&cd->writers); |
---|
1043 | 1079 | cd->last_close = seconds_since_boot(); |
---|
1044 | | - atomic_dec(&cd->readers); |
---|
1045 | 1080 | } |
---|
1046 | 1081 | module_put(cd->owner); |
---|
1047 | 1082 | return 0; |
---|
.. | .. |
---|
1149 | 1184 | |
---|
1150 | 1185 | static bool cache_listeners_exist(struct cache_detail *detail) |
---|
1151 | 1186 | { |
---|
1152 | | - if (atomic_read(&detail->readers)) |
---|
| 1187 | + if (atomic_read(&detail->writers)) |
---|
1153 | 1188 | return true; |
---|
1154 | 1189 | if (detail->last_close == 0) |
---|
1155 | 1190 | /* This cache was never opened */ |
---|
.. | .. |
---|
1170 | 1205 | * |
---|
1171 | 1206 | * Each request is at most one page long. |
---|
1172 | 1207 | */ |
---|
1173 | | -int sunrpc_cache_pipe_upcall(struct cache_detail *detail, struct cache_head *h) |
---|
| 1208 | +static int cache_pipe_upcall(struct cache_detail *detail, struct cache_head *h) |
---|
1174 | 1209 | { |
---|
1175 | | - |
---|
1176 | 1210 | char *buf; |
---|
1177 | 1211 | struct cache_request *crq; |
---|
1178 | 1212 | int ret = 0; |
---|
1179 | 1213 | |
---|
1180 | | - if (!detail->cache_request) |
---|
1181 | | - return -EINVAL; |
---|
1182 | | - |
---|
1183 | | - if (!cache_listeners_exist(detail)) { |
---|
1184 | | - warn_no_listener(detail); |
---|
1185 | | - return -EINVAL; |
---|
1186 | | - } |
---|
1187 | 1214 | if (test_bit(CACHE_CLEANED, &h->flags)) |
---|
1188 | 1215 | /* Too late to make an upcall */ |
---|
1189 | 1216 | return -EAGAIN; |
---|
.. | .. |
---|
1206 | 1233 | if (test_bit(CACHE_PENDING, &h->flags)) { |
---|
1207 | 1234 | crq->item = cache_get(h); |
---|
1208 | 1235 | list_add_tail(&crq->q.list, &detail->queue); |
---|
| 1236 | + trace_cache_entry_upcall(detail, h); |
---|
1209 | 1237 | } else |
---|
1210 | 1238 | /* Lost a race, no longer PENDING, so don't enqueue */ |
---|
1211 | 1239 | ret = -EAGAIN; |
---|
.. | .. |
---|
1217 | 1245 | } |
---|
1218 | 1246 | return ret; |
---|
1219 | 1247 | } |
---|
| 1248 | + |
---|
| 1249 | +int sunrpc_cache_pipe_upcall(struct cache_detail *detail, struct cache_head *h) |
---|
| 1250 | +{ |
---|
| 1251 | + if (test_and_set_bit(CACHE_PENDING, &h->flags)) |
---|
| 1252 | + return 0; |
---|
| 1253 | + return cache_pipe_upcall(detail, h); |
---|
| 1254 | +} |
---|
1220 | 1255 | EXPORT_SYMBOL_GPL(sunrpc_cache_pipe_upcall); |
---|
| 1256 | + |
---|
| 1257 | +int sunrpc_cache_pipe_upcall_timeout(struct cache_detail *detail, |
---|
| 1258 | + struct cache_head *h) |
---|
| 1259 | +{ |
---|
| 1260 | + if (!cache_listeners_exist(detail)) { |
---|
| 1261 | + warn_no_listener(detail); |
---|
| 1262 | + trace_cache_entry_no_listener(detail, h); |
---|
| 1263 | + return -EINVAL; |
---|
| 1264 | + } |
---|
| 1265 | + return sunrpc_cache_pipe_upcall(detail, h); |
---|
| 1266 | +} |
---|
| 1267 | +EXPORT_SYMBOL_GPL(sunrpc_cache_pipe_upcall_timeout); |
---|
1221 | 1268 | |
---|
1222 | 1269 | /* |
---|
1223 | 1270 | * parse a message from user-space and pass it |
---|
.. | .. |
---|
1294 | 1341 | * get a header, then pass each real item in the cache |
---|
1295 | 1342 | */ |
---|
1296 | 1343 | |
---|
1297 | | -void *cache_seq_start(struct seq_file *m, loff_t *pos) |
---|
1298 | | - __acquires(cd->hash_lock) |
---|
| 1344 | +static void *__cache_seq_start(struct seq_file *m, loff_t *pos) |
---|
1299 | 1345 | { |
---|
1300 | 1346 | loff_t n = *pos; |
---|
1301 | 1347 | unsigned int hash, entry; |
---|
1302 | 1348 | struct cache_head *ch; |
---|
1303 | 1349 | struct cache_detail *cd = m->private; |
---|
1304 | 1350 | |
---|
1305 | | - read_lock(&cd->hash_lock); |
---|
1306 | 1351 | if (!n--) |
---|
1307 | 1352 | return SEQ_START_TOKEN; |
---|
1308 | 1353 | hash = n >> 32; |
---|
1309 | 1354 | entry = n & ((1LL<<32) - 1); |
---|
1310 | 1355 | |
---|
1311 | | - hlist_for_each_entry(ch, &cd->hash_table[hash], cache_list) |
---|
| 1356 | + hlist_for_each_entry_rcu(ch, &cd->hash_table[hash], cache_list) |
---|
1312 | 1357 | if (!entry--) |
---|
1313 | 1358 | return ch; |
---|
1314 | 1359 | n &= ~((1LL<<32) - 1); |
---|
.. | .. |
---|
1320 | 1365 | if (hash >= cd->hash_size) |
---|
1321 | 1366 | return NULL; |
---|
1322 | 1367 | *pos = n+1; |
---|
1323 | | - return hlist_entry_safe(cd->hash_table[hash].first, |
---|
| 1368 | + return hlist_entry_safe(rcu_dereference_raw( |
---|
| 1369 | + hlist_first_rcu(&cd->hash_table[hash])), |
---|
1324 | 1370 | struct cache_head, cache_list); |
---|
1325 | 1371 | } |
---|
1326 | | -EXPORT_SYMBOL_GPL(cache_seq_start); |
---|
1327 | 1372 | |
---|
1328 | | -void *cache_seq_next(struct seq_file *m, void *p, loff_t *pos) |
---|
| 1373 | +static void *cache_seq_next(struct seq_file *m, void *p, loff_t *pos) |
---|
1329 | 1374 | { |
---|
1330 | 1375 | struct cache_head *ch = p; |
---|
1331 | 1376 | int hash = (*pos >> 32); |
---|
.. | .. |
---|
1338 | 1383 | *pos += 1LL<<32; |
---|
1339 | 1384 | } else { |
---|
1340 | 1385 | ++*pos; |
---|
1341 | | - return hlist_entry_safe(ch->cache_list.next, |
---|
| 1386 | + return hlist_entry_safe(rcu_dereference_raw( |
---|
| 1387 | + hlist_next_rcu(&ch->cache_list)), |
---|
1342 | 1388 | struct cache_head, cache_list); |
---|
1343 | 1389 | } |
---|
1344 | 1390 | *pos &= ~((1LL<<32) - 1); |
---|
.. | .. |
---|
1350 | 1396 | if (hash >= cd->hash_size) |
---|
1351 | 1397 | return NULL; |
---|
1352 | 1398 | ++*pos; |
---|
1353 | | - return hlist_entry_safe(cd->hash_table[hash].first, |
---|
| 1399 | + return hlist_entry_safe(rcu_dereference_raw( |
---|
| 1400 | + hlist_first_rcu(&cd->hash_table[hash])), |
---|
1354 | 1401 | struct cache_head, cache_list); |
---|
1355 | 1402 | } |
---|
1356 | | -EXPORT_SYMBOL_GPL(cache_seq_next); |
---|
1357 | 1403 | |
---|
1358 | | -void cache_seq_stop(struct seq_file *m, void *p) |
---|
1359 | | - __releases(cd->hash_lock) |
---|
| 1404 | +void *cache_seq_start_rcu(struct seq_file *m, loff_t *pos) |
---|
| 1405 | + __acquires(RCU) |
---|
1360 | 1406 | { |
---|
1361 | | - struct cache_detail *cd = m->private; |
---|
1362 | | - read_unlock(&cd->hash_lock); |
---|
| 1407 | + rcu_read_lock(); |
---|
| 1408 | + return __cache_seq_start(m, pos); |
---|
1363 | 1409 | } |
---|
1364 | | -EXPORT_SYMBOL_GPL(cache_seq_stop); |
---|
| 1410 | +EXPORT_SYMBOL_GPL(cache_seq_start_rcu); |
---|
| 1411 | + |
---|
| 1412 | +void *cache_seq_next_rcu(struct seq_file *file, void *p, loff_t *pos) |
---|
| 1413 | +{ |
---|
| 1414 | + return cache_seq_next(file, p, pos); |
---|
| 1415 | +} |
---|
| 1416 | +EXPORT_SYMBOL_GPL(cache_seq_next_rcu); |
---|
| 1417 | + |
---|
| 1418 | +void cache_seq_stop_rcu(struct seq_file *m, void *p) |
---|
| 1419 | + __releases(RCU) |
---|
| 1420 | +{ |
---|
| 1421 | + rcu_read_unlock(); |
---|
| 1422 | +} |
---|
| 1423 | +EXPORT_SYMBOL_GPL(cache_seq_stop_rcu); |
---|
1365 | 1424 | |
---|
1366 | 1425 | static int c_show(struct seq_file *m, void *p) |
---|
1367 | 1426 | { |
---|
.. | .. |
---|
1372 | 1431 | return cd->cache_show(m, cd, NULL); |
---|
1373 | 1432 | |
---|
1374 | 1433 | ifdebug(CACHE) |
---|
1375 | | - seq_printf(m, "# expiry=%ld refcnt=%d flags=%lx\n", |
---|
| 1434 | + seq_printf(m, "# expiry=%lld refcnt=%d flags=%lx\n", |
---|
1376 | 1435 | convert_to_wallclock(cp->expiry_time), |
---|
1377 | 1436 | kref_read(&cp->ref), cp->flags); |
---|
1378 | 1437 | cache_get(cp); |
---|
1379 | 1438 | if (cache_check(cd, cp, NULL)) |
---|
1380 | 1439 | /* cache_check does a cache_put on failure */ |
---|
1381 | | - seq_printf(m, "# "); |
---|
| 1440 | + seq_puts(m, "# "); |
---|
1382 | 1441 | else { |
---|
1383 | 1442 | if (cache_is_expired(cd, cp)) |
---|
1384 | | - seq_printf(m, "# "); |
---|
| 1443 | + seq_puts(m, "# "); |
---|
1385 | 1444 | cache_put(cp, cd); |
---|
1386 | 1445 | } |
---|
1387 | 1446 | |
---|
.. | .. |
---|
1389 | 1448 | } |
---|
1390 | 1449 | |
---|
1391 | 1450 | static const struct seq_operations cache_content_op = { |
---|
1392 | | - .start = cache_seq_start, |
---|
1393 | | - .next = cache_seq_next, |
---|
1394 | | - .stop = cache_seq_stop, |
---|
| 1451 | + .start = cache_seq_start_rcu, |
---|
| 1452 | + .next = cache_seq_next_rcu, |
---|
| 1453 | + .stop = cache_seq_stop_rcu, |
---|
1395 | 1454 | .show = c_show, |
---|
1396 | 1455 | }; |
---|
1397 | 1456 | |
---|
.. | .. |
---|
1445 | 1504 | char tbuf[22]; |
---|
1446 | 1505 | size_t len; |
---|
1447 | 1506 | |
---|
1448 | | - len = snprintf(tbuf, sizeof(tbuf), "%lu\n", |
---|
| 1507 | + len = snprintf(tbuf, sizeof(tbuf), "%llu\n", |
---|
1449 | 1508 | convert_to_wallclock(cd->flush_time)); |
---|
1450 | 1509 | return simple_read_from_buffer(buf, count, ppos, tbuf, len); |
---|
1451 | 1510 | } |
---|
.. | .. |
---|
1456 | 1515 | { |
---|
1457 | 1516 | char tbuf[20]; |
---|
1458 | 1517 | char *ep; |
---|
1459 | | - time_t now; |
---|
| 1518 | + time64_t now; |
---|
1460 | 1519 | |
---|
1461 | 1520 | if (*ppos || count > sizeof(tbuf)-1) |
---|
1462 | 1521 | return -EINVAL; |
---|
.. | .. |
---|
1485 | 1544 | cd->flush_time = now; |
---|
1486 | 1545 | cd->nextcheck = now; |
---|
1487 | 1546 | cache_flush(); |
---|
| 1547 | + |
---|
| 1548 | + if (cd->flush) |
---|
| 1549 | + cd->flush(); |
---|
1488 | 1550 | |
---|
1489 | 1551 | *ppos += count; |
---|
1490 | 1552 | return count; |
---|
.. | .. |
---|
1536 | 1598 | return cache_release(inode, filp, cd); |
---|
1537 | 1599 | } |
---|
1538 | 1600 | |
---|
1539 | | -static const struct file_operations cache_file_operations_procfs = { |
---|
1540 | | - .owner = THIS_MODULE, |
---|
1541 | | - .llseek = no_llseek, |
---|
1542 | | - .read = cache_read_procfs, |
---|
1543 | | - .write = cache_write_procfs, |
---|
1544 | | - .poll = cache_poll_procfs, |
---|
1545 | | - .unlocked_ioctl = cache_ioctl_procfs, /* for FIONREAD */ |
---|
1546 | | - .open = cache_open_procfs, |
---|
1547 | | - .release = cache_release_procfs, |
---|
| 1601 | +static const struct proc_ops cache_channel_proc_ops = { |
---|
| 1602 | + .proc_lseek = no_llseek, |
---|
| 1603 | + .proc_read = cache_read_procfs, |
---|
| 1604 | + .proc_write = cache_write_procfs, |
---|
| 1605 | + .proc_poll = cache_poll_procfs, |
---|
| 1606 | + .proc_ioctl = cache_ioctl_procfs, /* for FIONREAD */ |
---|
| 1607 | + .proc_open = cache_open_procfs, |
---|
| 1608 | + .proc_release = cache_release_procfs, |
---|
1548 | 1609 | }; |
---|
1549 | 1610 | |
---|
1550 | 1611 | static int content_open_procfs(struct inode *inode, struct file *filp) |
---|
.. | .. |
---|
1561 | 1622 | return content_release(inode, filp, cd); |
---|
1562 | 1623 | } |
---|
1563 | 1624 | |
---|
1564 | | -static const struct file_operations content_file_operations_procfs = { |
---|
1565 | | - .open = content_open_procfs, |
---|
1566 | | - .read = seq_read, |
---|
1567 | | - .llseek = seq_lseek, |
---|
1568 | | - .release = content_release_procfs, |
---|
| 1625 | +static const struct proc_ops content_proc_ops = { |
---|
| 1626 | + .proc_open = content_open_procfs, |
---|
| 1627 | + .proc_read = seq_read, |
---|
| 1628 | + .proc_lseek = seq_lseek, |
---|
| 1629 | + .proc_release = content_release_procfs, |
---|
1569 | 1630 | }; |
---|
1570 | 1631 | |
---|
1571 | 1632 | static int open_flush_procfs(struct inode *inode, struct file *filp) |
---|
.. | .. |
---|
1599 | 1660 | return write_flush(filp, buf, count, ppos, cd); |
---|
1600 | 1661 | } |
---|
1601 | 1662 | |
---|
1602 | | -static const struct file_operations cache_flush_operations_procfs = { |
---|
1603 | | - .open = open_flush_procfs, |
---|
1604 | | - .read = read_flush_procfs, |
---|
1605 | | - .write = write_flush_procfs, |
---|
1606 | | - .release = release_flush_procfs, |
---|
1607 | | - .llseek = no_llseek, |
---|
| 1663 | +static const struct proc_ops cache_flush_proc_ops = { |
---|
| 1664 | + .proc_open = open_flush_procfs, |
---|
| 1665 | + .proc_read = read_flush_procfs, |
---|
| 1666 | + .proc_write = write_flush_procfs, |
---|
| 1667 | + .proc_release = release_flush_procfs, |
---|
| 1668 | + .proc_lseek = no_llseek, |
---|
1608 | 1669 | }; |
---|
1609 | 1670 | |
---|
1610 | 1671 | static void remove_cache_proc_entries(struct cache_detail *cd) |
---|
.. | .. |
---|
1627 | 1688 | goto out_nomem; |
---|
1628 | 1689 | |
---|
1629 | 1690 | p = proc_create_data("flush", S_IFREG | 0600, |
---|
1630 | | - cd->procfs, &cache_flush_operations_procfs, cd); |
---|
| 1691 | + cd->procfs, &cache_flush_proc_ops, cd); |
---|
1631 | 1692 | if (p == NULL) |
---|
1632 | 1693 | goto out_nomem; |
---|
1633 | 1694 | |
---|
1634 | 1695 | if (cd->cache_request || cd->cache_parse) { |
---|
1635 | 1696 | p = proc_create_data("channel", S_IFREG | 0600, cd->procfs, |
---|
1636 | | - &cache_file_operations_procfs, cd); |
---|
| 1697 | + &cache_channel_proc_ops, cd); |
---|
1637 | 1698 | if (p == NULL) |
---|
1638 | 1699 | goto out_nomem; |
---|
1639 | 1700 | } |
---|
1640 | 1701 | if (cd->cache_show) { |
---|
1641 | 1702 | p = proc_create_data("content", S_IFREG | 0400, cd->procfs, |
---|
1642 | | - &content_file_operations_procfs, cd); |
---|
| 1703 | + &content_proc_ops, cd); |
---|
1643 | 1704 | if (p == NULL) |
---|
1644 | 1705 | goto out_nomem; |
---|
1645 | 1706 | } |
---|
.. | .. |
---|
1849 | 1910 | |
---|
1850 | 1911 | void sunrpc_cache_unhash(struct cache_detail *cd, struct cache_head *h) |
---|
1851 | 1912 | { |
---|
1852 | | - write_lock(&cd->hash_lock); |
---|
| 1913 | + spin_lock(&cd->hash_lock); |
---|
1853 | 1914 | if (!hlist_unhashed(&h->cache_list)){ |
---|
1854 | | - hlist_del_init(&h->cache_list); |
---|
1855 | | - cd->entries--; |
---|
1856 | | - write_unlock(&cd->hash_lock); |
---|
1857 | | - cache_put(h, cd); |
---|
| 1915 | + sunrpc_begin_cache_remove_entry(h, cd); |
---|
| 1916 | + spin_unlock(&cd->hash_lock); |
---|
| 1917 | + sunrpc_end_cache_remove_entry(h, cd); |
---|
1858 | 1918 | } else |
---|
1859 | | - write_unlock(&cd->hash_lock); |
---|
| 1919 | + spin_unlock(&cd->hash_lock); |
---|
1860 | 1920 | } |
---|
1861 | 1921 | EXPORT_SYMBOL_GPL(sunrpc_cache_unhash); |
---|