nfscache.c 16 KB

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