nfscache.c 16 KB

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