nfscache.c 16 KB

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