nfscache.c 15 KB

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