nfscache.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  1. /*
  2. * Request reply cache. This is currently a global cache, but this may
  3. * change in the future and be a per-client cache.
  4. *
  5. * This code is heavily inspired by the 44BSD implementation, although
  6. * it does things a bit differently.
  7. *
  8. * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
  9. */
  10. #include <linux/slab.h>
  11. #include <linux/sunrpc/addr.h>
  12. #include <linux/highmem.h>
  13. #include <linux/log2.h>
  14. #include <linux/hash.h>
  15. #include <net/checksum.h>
  16. #include "nfsd.h"
  17. #include "cache.h"
  18. #define NFSDDBG_FACILITY NFSDDBG_REPCACHE
  19. /*
  20. * We use this value to determine the number of hash buckets from the max
  21. * cache size, the idea being that when the cache is at its maximum number
  22. * of entries, then this should be the average number of entries per bucket.
  23. */
  24. #define TARGET_BUCKET_SIZE 64
  25. struct nfsd_drc_bucket {
  26. struct list_head lru_head;
  27. spinlock_t cache_lock;
  28. };
  29. static struct nfsd_drc_bucket *drc_hashtbl;
  30. static struct kmem_cache *drc_slab;
  31. /* max number of entries allowed in the cache */
  32. static unsigned int max_drc_entries;
  33. /* number of significant bits in the hash value */
  34. static unsigned int maskbits;
  35. static unsigned int drc_hashsize;
  36. /*
  37. * Stats and other tracking of on the duplicate reply cache. All of these and
  38. * the "rc" fields in nfsdstats are protected by the cache_lock
  39. */
  40. /* total number of entries */
  41. static atomic_t num_drc_entries;
  42. /* cache misses due only to checksum comparison failures */
  43. static unsigned int payload_misses;
  44. /* amount of memory (in bytes) currently consumed by the DRC */
  45. static unsigned int drc_mem_usage;
  46. /* longest hash chain seen */
  47. static unsigned int longest_chain;
  48. /* size of cache when we saw the longest hash chain */
  49. static unsigned int longest_chain_cachesize;
  50. static int nfsd_cache_append(struct svc_rqst *rqstp, struct kvec *vec);
  51. static unsigned long nfsd_reply_cache_count(struct shrinker *shrink,
  52. struct shrink_control *sc);
  53. static unsigned long nfsd_reply_cache_scan(struct shrinker *shrink,
  54. struct shrink_control *sc);
  55. static struct shrinker nfsd_reply_cache_shrinker = {
  56. .scan_objects = nfsd_reply_cache_scan,
  57. .count_objects = nfsd_reply_cache_count,
  58. .seeks = 1,
  59. };
  60. /*
  61. * Put a cap on the size of the DRC based on the amount of available
  62. * low memory in the machine.
  63. *
  64. * 64MB: 8192
  65. * 128MB: 11585
  66. * 256MB: 16384
  67. * 512MB: 23170
  68. * 1GB: 32768
  69. * 2GB: 46340
  70. * 4GB: 65536
  71. * 8GB: 92681
  72. * 16GB: 131072
  73. *
  74. * ...with a hard cap of 256k entries. In the worst case, each entry will be
  75. * ~1k, so the above numbers should give a rough max of the amount of memory
  76. * used in k.
  77. */
  78. static unsigned int
  79. nfsd_cache_size_limit(void)
  80. {
  81. unsigned int limit;
  82. unsigned long low_pages = totalram_pages - totalhigh_pages;
  83. limit = (16 * int_sqrt(low_pages)) << (PAGE_SHIFT-10);
  84. return min_t(unsigned int, limit, 256*1024);
  85. }
  86. /*
  87. * Compute the number of hash buckets we need. Divide the max cachesize by
  88. * the "target" max bucket size, and round up to next power of two.
  89. */
  90. static unsigned int
  91. nfsd_hashsize(unsigned int limit)
  92. {
  93. return roundup_pow_of_two(limit / TARGET_BUCKET_SIZE);
  94. }
  95. static u32
  96. nfsd_cache_hash(__be32 xid)
  97. {
  98. return hash_32(be32_to_cpu(xid), maskbits);
  99. }
  100. static struct svc_cacherep *
  101. nfsd_reply_cache_alloc(void)
  102. {
  103. struct svc_cacherep *rp;
  104. rp = kmem_cache_alloc(drc_slab, GFP_KERNEL);
  105. if (rp) {
  106. rp->c_state = RC_UNUSED;
  107. rp->c_type = RC_NOCACHE;
  108. INIT_LIST_HEAD(&rp->c_lru);
  109. }
  110. return rp;
  111. }
  112. static void
  113. nfsd_reply_cache_free_locked(struct svc_cacherep *rp)
  114. {
  115. if (rp->c_type == RC_REPLBUFF && rp->c_replvec.iov_base) {
  116. drc_mem_usage -= rp->c_replvec.iov_len;
  117. kfree(rp->c_replvec.iov_base);
  118. }
  119. list_del(&rp->c_lru);
  120. atomic_dec(&num_drc_entries);
  121. drc_mem_usage -= sizeof(*rp);
  122. kmem_cache_free(drc_slab, rp);
  123. }
  124. static void
  125. nfsd_reply_cache_free(struct nfsd_drc_bucket *b, struct svc_cacherep *rp)
  126. {
  127. spin_lock(&b->cache_lock);
  128. nfsd_reply_cache_free_locked(rp);
  129. spin_unlock(&b->cache_lock);
  130. }
  131. int nfsd_reply_cache_init(void)
  132. {
  133. unsigned int hashsize;
  134. unsigned int i;
  135. int status = 0;
  136. max_drc_entries = nfsd_cache_size_limit();
  137. atomic_set(&num_drc_entries, 0);
  138. hashsize = nfsd_hashsize(max_drc_entries);
  139. maskbits = ilog2(hashsize);
  140. status = register_shrinker(&nfsd_reply_cache_shrinker);
  141. if (status)
  142. return status;
  143. drc_slab = kmem_cache_create("nfsd_drc", sizeof(struct svc_cacherep),
  144. 0, 0, NULL);
  145. if (!drc_slab)
  146. goto out_nomem;
  147. drc_hashtbl = kcalloc(hashsize, sizeof(*drc_hashtbl), GFP_KERNEL);
  148. if (!drc_hashtbl)
  149. goto out_nomem;
  150. for (i = 0; i < hashsize; i++) {
  151. INIT_LIST_HEAD(&drc_hashtbl[i].lru_head);
  152. spin_lock_init(&drc_hashtbl[i].cache_lock);
  153. }
  154. drc_hashsize = hashsize;
  155. return 0;
  156. out_nomem:
  157. printk(KERN_ERR "nfsd: failed to allocate reply cache\n");
  158. nfsd_reply_cache_shutdown();
  159. return -ENOMEM;
  160. }
  161. void nfsd_reply_cache_shutdown(void)
  162. {
  163. struct svc_cacherep *rp;
  164. unsigned int i;
  165. unregister_shrinker(&nfsd_reply_cache_shrinker);
  166. for (i = 0; i < drc_hashsize; i++) {
  167. struct list_head *head = &drc_hashtbl[i].lru_head;
  168. while (!list_empty(head)) {
  169. rp = list_first_entry(head, struct svc_cacherep, c_lru);
  170. nfsd_reply_cache_free_locked(rp);
  171. }
  172. }
  173. kfree (drc_hashtbl);
  174. drc_hashtbl = NULL;
  175. drc_hashsize = 0;
  176. kmem_cache_destroy(drc_slab);
  177. drc_slab = NULL;
  178. }
  179. /*
  180. * Move cache entry to end of LRU list, and queue the cleaner to run if it's
  181. * not already scheduled.
  182. */
  183. static void
  184. lru_put_end(struct nfsd_drc_bucket *b, struct svc_cacherep *rp)
  185. {
  186. rp->c_timestamp = jiffies;
  187. list_move_tail(&rp->c_lru, &b->lru_head);
  188. }
  189. static long
  190. prune_bucket(struct nfsd_drc_bucket *b)
  191. {
  192. struct svc_cacherep *rp, *tmp;
  193. long freed = 0;
  194. list_for_each_entry_safe(rp, tmp, &b->lru_head, c_lru) {
  195. /*
  196. * Don't free entries attached to calls that are still
  197. * in-progress, but do keep scanning the list.
  198. */
  199. if (rp->c_state == RC_INPROG)
  200. continue;
  201. if (atomic_read(&num_drc_entries) <= max_drc_entries &&
  202. time_before(jiffies, rp->c_timestamp + RC_EXPIRE))
  203. break;
  204. nfsd_reply_cache_free_locked(rp);
  205. freed++;
  206. }
  207. return freed;
  208. }
  209. /*
  210. * Walk the LRU list and prune off entries that are older than RC_EXPIRE.
  211. * Also prune the oldest ones when the total exceeds the max number of entries.
  212. */
  213. static long
  214. prune_cache_entries(void)
  215. {
  216. unsigned int i;
  217. long freed = 0;
  218. for (i = 0; i < drc_hashsize; i++) {
  219. struct nfsd_drc_bucket *b = &drc_hashtbl[i];
  220. if (list_empty(&b->lru_head))
  221. continue;
  222. spin_lock(&b->cache_lock);
  223. freed += prune_bucket(b);
  224. spin_unlock(&b->cache_lock);
  225. }
  226. return freed;
  227. }
  228. static unsigned long
  229. nfsd_reply_cache_count(struct shrinker *shrink, struct shrink_control *sc)
  230. {
  231. return atomic_read(&num_drc_entries);
  232. }
  233. static unsigned long
  234. nfsd_reply_cache_scan(struct shrinker *shrink, struct shrink_control *sc)
  235. {
  236. return prune_cache_entries();
  237. }
  238. /*
  239. * Walk an xdr_buf and get a CRC for at most the first RC_CSUMLEN bytes
  240. */
  241. static __wsum
  242. nfsd_cache_csum(struct svc_rqst *rqstp)
  243. {
  244. int idx;
  245. unsigned int base;
  246. __wsum csum;
  247. struct xdr_buf *buf = &rqstp->rq_arg;
  248. const unsigned char *p = buf->head[0].iov_base;
  249. size_t csum_len = min_t(size_t, buf->head[0].iov_len + buf->page_len,
  250. RC_CSUMLEN);
  251. size_t len = min(buf->head[0].iov_len, csum_len);
  252. /* rq_arg.head first */
  253. csum = csum_partial(p, len, 0);
  254. csum_len -= len;
  255. /* Continue into page array */
  256. idx = buf->page_base / PAGE_SIZE;
  257. base = buf->page_base & ~PAGE_MASK;
  258. while (csum_len) {
  259. p = page_address(buf->pages[idx]) + base;
  260. len = min_t(size_t, PAGE_SIZE - base, csum_len);
  261. csum = csum_partial(p, len, csum);
  262. csum_len -= len;
  263. base = 0;
  264. ++idx;
  265. }
  266. return csum;
  267. }
  268. static bool
  269. nfsd_cache_match(struct svc_rqst *rqstp, __wsum csum, struct svc_cacherep *rp)
  270. {
  271. /* Check RPC XID first */
  272. if (rqstp->rq_xid != rp->c_xid)
  273. return false;
  274. /* compare checksum of NFS data */
  275. if (csum != rp->c_csum) {
  276. ++payload_misses;
  277. return false;
  278. }
  279. /* Other discriminators */
  280. if (rqstp->rq_proc != rp->c_proc ||
  281. rqstp->rq_prot != rp->c_prot ||
  282. rqstp->rq_vers != rp->c_vers ||
  283. rqstp->rq_arg.len != rp->c_len ||
  284. !rpc_cmp_addr(svc_addr(rqstp), (struct sockaddr *)&rp->c_addr) ||
  285. rpc_get_port(svc_addr(rqstp)) != rpc_get_port((struct sockaddr *)&rp->c_addr))
  286. return false;
  287. return true;
  288. }
  289. /*
  290. * Search the request hash for an entry that matches the given rqstp.
  291. * Must be called with cache_lock held. Returns the found entry or
  292. * NULL on failure.
  293. */
  294. static struct svc_cacherep *
  295. nfsd_cache_search(struct nfsd_drc_bucket *b, struct svc_rqst *rqstp,
  296. __wsum csum)
  297. {
  298. struct svc_cacherep *rp, *ret = NULL;
  299. struct list_head *rh = &b->lru_head;
  300. unsigned int entries = 0;
  301. list_for_each_entry(rp, rh, c_lru) {
  302. ++entries;
  303. if (nfsd_cache_match(rqstp, csum, rp)) {
  304. ret = rp;
  305. break;
  306. }
  307. }
  308. /* tally hash chain length stats */
  309. if (entries > longest_chain) {
  310. longest_chain = entries;
  311. longest_chain_cachesize = atomic_read(&num_drc_entries);
  312. } else if (entries == longest_chain) {
  313. /* prefer to keep the smallest cachesize possible here */
  314. longest_chain_cachesize = min_t(unsigned int,
  315. longest_chain_cachesize,
  316. atomic_read(&num_drc_entries));
  317. }
  318. return ret;
  319. }
  320. /*
  321. * Try to find an entry matching the current call in the cache. When none
  322. * is found, we try to grab the oldest expired entry off the LRU list. If
  323. * a suitable one isn't there, then drop the cache_lock and allocate a
  324. * new one, then search again in case one got inserted while this thread
  325. * didn't hold the lock.
  326. */
  327. int
  328. nfsd_cache_lookup(struct svc_rqst *rqstp)
  329. {
  330. struct svc_cacherep *rp, *found;
  331. __be32 xid = rqstp->rq_xid;
  332. u32 proto = rqstp->rq_prot,
  333. vers = rqstp->rq_vers,
  334. proc = rqstp->rq_proc;
  335. __wsum csum;
  336. u32 hash = nfsd_cache_hash(xid);
  337. struct nfsd_drc_bucket *b = &drc_hashtbl[hash];
  338. unsigned long age;
  339. int type = rqstp->rq_cachetype;
  340. int rtn = RC_DOIT;
  341. rqstp->rq_cacherep = NULL;
  342. if (type == RC_NOCACHE) {
  343. nfsdstats.rcnocache++;
  344. return rtn;
  345. }
  346. csum = nfsd_cache_csum(rqstp);
  347. /*
  348. * Since the common case is a cache miss followed by an insert,
  349. * preallocate an entry.
  350. */
  351. rp = nfsd_reply_cache_alloc();
  352. spin_lock(&b->cache_lock);
  353. if (likely(rp)) {
  354. atomic_inc(&num_drc_entries);
  355. drc_mem_usage += sizeof(*rp);
  356. }
  357. /* go ahead and prune the cache */
  358. prune_bucket(b);
  359. found = nfsd_cache_search(b, rqstp, csum);
  360. if (found) {
  361. if (likely(rp))
  362. nfsd_reply_cache_free_locked(rp);
  363. rp = found;
  364. goto found_entry;
  365. }
  366. if (!rp) {
  367. dprintk("nfsd: unable to allocate DRC entry!\n");
  368. goto out;
  369. }
  370. nfsdstats.rcmisses++;
  371. rqstp->rq_cacherep = rp;
  372. rp->c_state = RC_INPROG;
  373. rp->c_xid = xid;
  374. rp->c_proc = proc;
  375. rpc_copy_addr((struct sockaddr *)&rp->c_addr, svc_addr(rqstp));
  376. rpc_set_port((struct sockaddr *)&rp->c_addr, rpc_get_port(svc_addr(rqstp)));
  377. rp->c_prot = proto;
  378. rp->c_vers = vers;
  379. rp->c_len = rqstp->rq_arg.len;
  380. rp->c_csum = csum;
  381. lru_put_end(b, rp);
  382. /* release any buffer */
  383. if (rp->c_type == RC_REPLBUFF) {
  384. drc_mem_usage -= rp->c_replvec.iov_len;
  385. kfree(rp->c_replvec.iov_base);
  386. rp->c_replvec.iov_base = NULL;
  387. }
  388. rp->c_type = RC_NOCACHE;
  389. out:
  390. spin_unlock(&b->cache_lock);
  391. return rtn;
  392. found_entry:
  393. nfsdstats.rchits++;
  394. /* We found a matching entry which is either in progress or done. */
  395. age = jiffies - rp->c_timestamp;
  396. lru_put_end(b, rp);
  397. rtn = RC_DROPIT;
  398. /* Request being processed or excessive rexmits */
  399. if (rp->c_state == RC_INPROG || age < RC_DELAY)
  400. goto out;
  401. /* From the hall of fame of impractical attacks:
  402. * Is this a user who tries to snoop on the cache? */
  403. rtn = RC_DOIT;
  404. if (!test_bit(RQ_SECURE, &rqstp->rq_flags) && rp->c_secure)
  405. goto out;
  406. /* Compose RPC reply header */
  407. switch (rp->c_type) {
  408. case RC_NOCACHE:
  409. break;
  410. case RC_REPLSTAT:
  411. svc_putu32(&rqstp->rq_res.head[0], rp->c_replstat);
  412. rtn = RC_REPLY;
  413. break;
  414. case RC_REPLBUFF:
  415. if (!nfsd_cache_append(rqstp, &rp->c_replvec))
  416. goto out; /* should not happen */
  417. rtn = RC_REPLY;
  418. break;
  419. default:
  420. printk(KERN_WARNING "nfsd: bad repcache type %d\n", rp->c_type);
  421. nfsd_reply_cache_free_locked(rp);
  422. }
  423. goto out;
  424. }
  425. /*
  426. * Update a cache entry. This is called from nfsd_dispatch when
  427. * the procedure has been executed and the complete reply is in
  428. * rqstp->rq_res.
  429. *
  430. * We're copying around data here rather than swapping buffers because
  431. * the toplevel loop requires max-sized buffers, which would be a waste
  432. * of memory for a cache with a max reply size of 100 bytes (diropokres).
  433. *
  434. * If we should start to use different types of cache entries tailored
  435. * specifically for attrstat and fh's, we may save even more space.
  436. *
  437. * Also note that a cachetype of RC_NOCACHE can legally be passed when
  438. * nfsd failed to encode a reply that otherwise would have been cached.
  439. * In this case, nfsd_cache_update is called with statp == NULL.
  440. */
  441. void
  442. nfsd_cache_update(struct svc_rqst *rqstp, int cachetype, __be32 *statp)
  443. {
  444. struct svc_cacherep *rp = rqstp->rq_cacherep;
  445. struct kvec *resv = &rqstp->rq_res.head[0], *cachv;
  446. u32 hash;
  447. struct nfsd_drc_bucket *b;
  448. int len;
  449. size_t bufsize = 0;
  450. if (!rp)
  451. return;
  452. hash = nfsd_cache_hash(rp->c_xid);
  453. b = &drc_hashtbl[hash];
  454. len = resv->iov_len - ((char*)statp - (char*)resv->iov_base);
  455. len >>= 2;
  456. /* Don't cache excessive amounts of data and XDR failures */
  457. if (!statp || len > (256 >> 2)) {
  458. nfsd_reply_cache_free(b, rp);
  459. return;
  460. }
  461. switch (cachetype) {
  462. case RC_REPLSTAT:
  463. if (len != 1)
  464. printk("nfsd: RC_REPLSTAT/reply len %d!\n",len);
  465. rp->c_replstat = *statp;
  466. break;
  467. case RC_REPLBUFF:
  468. cachv = &rp->c_replvec;
  469. bufsize = len << 2;
  470. cachv->iov_base = kmalloc(bufsize, GFP_KERNEL);
  471. if (!cachv->iov_base) {
  472. nfsd_reply_cache_free(b, rp);
  473. return;
  474. }
  475. cachv->iov_len = bufsize;
  476. memcpy(cachv->iov_base, statp, bufsize);
  477. break;
  478. case RC_NOCACHE:
  479. nfsd_reply_cache_free(b, rp);
  480. return;
  481. }
  482. spin_lock(&b->cache_lock);
  483. drc_mem_usage += bufsize;
  484. lru_put_end(b, rp);
  485. rp->c_secure = test_bit(RQ_SECURE, &rqstp->rq_flags);
  486. rp->c_type = cachetype;
  487. rp->c_state = RC_DONE;
  488. spin_unlock(&b->cache_lock);
  489. return;
  490. }
  491. /*
  492. * Copy cached reply to current reply buffer. Should always fit.
  493. * FIXME as reply is in a page, we should just attach the page, and
  494. * keep a refcount....
  495. */
  496. static int
  497. nfsd_cache_append(struct svc_rqst *rqstp, struct kvec *data)
  498. {
  499. struct kvec *vec = &rqstp->rq_res.head[0];
  500. if (vec->iov_len + data->iov_len > PAGE_SIZE) {
  501. printk(KERN_WARNING "nfsd: cached reply too large (%Zd).\n",
  502. data->iov_len);
  503. return 0;
  504. }
  505. memcpy((char*)vec->iov_base + vec->iov_len, data->iov_base, data->iov_len);
  506. vec->iov_len += data->iov_len;
  507. return 1;
  508. }
  509. /*
  510. * Note that fields may be added, removed or reordered in the future. Programs
  511. * scraping this file for info should test the labels to ensure they're
  512. * getting the correct field.
  513. */
  514. static int nfsd_reply_cache_stats_show(struct seq_file *m, void *v)
  515. {
  516. seq_printf(m, "max entries: %u\n", max_drc_entries);
  517. seq_printf(m, "num entries: %u\n",
  518. atomic_read(&num_drc_entries));
  519. seq_printf(m, "hash buckets: %u\n", 1 << maskbits);
  520. seq_printf(m, "mem usage: %u\n", drc_mem_usage);
  521. seq_printf(m, "cache hits: %u\n", nfsdstats.rchits);
  522. seq_printf(m, "cache misses: %u\n", nfsdstats.rcmisses);
  523. seq_printf(m, "not cached: %u\n", nfsdstats.rcnocache);
  524. seq_printf(m, "payload misses: %u\n", payload_misses);
  525. seq_printf(m, "longest chain len: %u\n", longest_chain);
  526. seq_printf(m, "cachesize at longest: %u\n", longest_chain_cachesize);
  527. return 0;
  528. }
  529. int nfsd_reply_cache_stats_open(struct inode *inode, struct file *file)
  530. {
  531. return single_open(file, nfsd_reply_cache_stats_show, NULL);
  532. }