aboutsummaryrefslogtreecommitdiffstats
path: root/fs/nfsd
diff options
context:
space:
mode:
authorJeff Layton <jlayton@redhat.com>2013-02-04 08:18:02 -0500
committerJ. Bruce Fields <bfields@redhat.com>2013-02-04 17:19:10 -0500
commit0338dd157282c19696d3c32614a748d0ba814b12 (patch)
tree4a42757386eab303fd723bcfbcadf843c921ebc8 /fs/nfsd
parent0ee0bf7ee5b55f232b645c4af0b0c37d4e115a32 (diff)
nfsd: dynamically allocate DRC entries
The existing code keeps a fixed-size cache of 1024 entries. This is much too small for a busy server, and wastes memory on an idle one. This patch changes the code to dynamically allocate and free these cache entries. A cap on the number of entries is retained, but it's much larger than the existing value and now scales with the amount of low memory in the machine. Signed-off-by: Jeff Layton <jlayton@redhat.com> Signed-off-by: J. Bruce Fields <bfields@redhat.com>
Diffstat (limited to 'fs/nfsd')
-rw-r--r--fs/nfsd/nfscache.c125
1 files changed, 75 insertions, 50 deletions
diff --git a/fs/nfsd/nfscache.c b/fs/nfsd/nfscache.c
index c0c58471eb45..d213e6e69e46 100644
--- a/fs/nfsd/nfscache.c
+++ b/fs/nfsd/nfscache.c
@@ -10,17 +10,13 @@
10 10
11#include <linux/slab.h> 11#include <linux/slab.h>
12#include <linux/sunrpc/clnt.h> 12#include <linux/sunrpc/clnt.h>
13#include <linux/highmem.h>
13 14
14#include "nfsd.h" 15#include "nfsd.h"
15#include "cache.h" 16#include "cache.h"
16 17
17/* Size of reply cache. Common values are: 18#define NFSDDBG_FACILITY NFSDDBG_REPCACHE
18 * 4.3BSD: 128 19
19 * 4.4BSD: 256
20 * Solaris2: 1024
21 * DEC Unix: 512-4096
22 */
23#define CACHESIZE 1024
24#define HASHSIZE 64 20#define HASHSIZE 64
25 21
26static struct hlist_head * cache_hash; 22static struct hlist_head * cache_hash;
@@ -28,6 +24,7 @@ static struct list_head lru_head;
28static int cache_disabled = 1; 24static int cache_disabled = 1;
29static struct kmem_cache *drc_slab; 25static struct kmem_cache *drc_slab;
30static unsigned int num_drc_entries; 26static unsigned int num_drc_entries;
27static unsigned int max_drc_entries;
31 28
32/* 29/*
33 * Calculate the hash index from an XID. 30 * Calculate the hash index from an XID.
@@ -48,6 +45,34 @@ static int nfsd_cache_append(struct svc_rqst *rqstp, struct kvec *vec);
48 */ 45 */
49static DEFINE_SPINLOCK(cache_lock); 46static DEFINE_SPINLOCK(cache_lock);
50 47
48/*
49 * Put a cap on the size of the DRC based on the amount of available
50 * low memory in the machine.
51 *
52 * 64MB: 8192
53 * 128MB: 11585
54 * 256MB: 16384
55 * 512MB: 23170
56 * 1GB: 32768
57 * 2GB: 46340
58 * 4GB: 65536
59 * 8GB: 92681
60 * 16GB: 131072
61 *
62 * ...with a hard cap of 256k entries. In the worst case, each entry will be
63 * ~1k, so the above numbers should give a rough max of the amount of memory
64 * used in k.
65 */
66static unsigned int
67nfsd_cache_size_limit(void)
68{
69 unsigned int limit;
70 unsigned long low_pages = totalram_pages - totalhigh_pages;
71
72 limit = (16 * int_sqrt(low_pages)) << (PAGE_SHIFT-10);
73 return min_t(unsigned int, limit, 256*1024);
74}
75
51static struct svc_cacherep * 76static struct svc_cacherep *
52nfsd_reply_cache_alloc(void) 77nfsd_reply_cache_alloc(void)
53{ 78{
@@ -68,6 +93,7 @@ nfsd_reply_cache_free_locked(struct svc_cacherep *rp)
68{ 93{
69 if (rp->c_type == RC_REPLBUFF) 94 if (rp->c_type == RC_REPLBUFF)
70 kfree(rp->c_replvec.iov_base); 95 kfree(rp->c_replvec.iov_base);
96 hlist_del(&rp->c_hash);
71 list_del(&rp->c_lru); 97 list_del(&rp->c_lru);
72 --num_drc_entries; 98 --num_drc_entries;
73 kmem_cache_free(drc_slab, rp); 99 kmem_cache_free(drc_slab, rp);
@@ -75,30 +101,18 @@ nfsd_reply_cache_free_locked(struct svc_cacherep *rp)
75 101
76int nfsd_reply_cache_init(void) 102int nfsd_reply_cache_init(void)
77{ 103{
78 int i;
79 struct svc_cacherep *rp;
80
81 drc_slab = kmem_cache_create("nfsd_drc", sizeof(struct svc_cacherep), 104 drc_slab = kmem_cache_create("nfsd_drc", sizeof(struct svc_cacherep),
82 0, 0, NULL); 105 0, 0, NULL);
83 if (!drc_slab) 106 if (!drc_slab)
84 goto out_nomem; 107 goto out_nomem;
85 108
86 INIT_LIST_HEAD(&lru_head); 109 cache_hash = kcalloc(HASHSIZE, sizeof(struct hlist_head), GFP_KERNEL);
87 i = CACHESIZE;
88 num_drc_entries = 0;
89 while (i) {
90 rp = nfsd_reply_cache_alloc();
91 if (!rp)
92 goto out_nomem;
93 ++num_drc_entries;
94 list_add(&rp->c_lru, &lru_head);
95 i--;
96 }
97
98 cache_hash = kcalloc (HASHSIZE, sizeof(struct hlist_head), GFP_KERNEL);
99 if (!cache_hash) 110 if (!cache_hash)
100 goto out_nomem; 111 goto out_nomem;
101 112
113 INIT_LIST_HEAD(&lru_head);
114 max_drc_entries = nfsd_cache_size_limit();
115 num_drc_entries = 0;
102 cache_disabled = 0; 116 cache_disabled = 0;
103 return 0; 117 return 0;
104out_nomem: 118out_nomem:
@@ -191,7 +205,7 @@ nfsd_cache_search(struct svc_rqst *rqstp)
191int 205int
192nfsd_cache_lookup(struct svc_rqst *rqstp) 206nfsd_cache_lookup(struct svc_rqst *rqstp)
193{ 207{
194 struct svc_cacherep *rp; 208 struct svc_cacherep *rp, *found;
195 __be32 xid = rqstp->rq_xid; 209 __be32 xid = rqstp->rq_xid;
196 u32 proto = rqstp->rq_prot, 210 u32 proto = rqstp->rq_prot,
197 vers = rqstp->rq_vers, 211 vers = rqstp->rq_vers,
@@ -210,38 +224,48 @@ nfsd_cache_lookup(struct svc_rqst *rqstp)
210 rtn = RC_DOIT; 224 rtn = RC_DOIT;
211 225
212 rp = nfsd_cache_search(rqstp); 226 rp = nfsd_cache_search(rqstp);
213 if (rp) { 227 if (rp)
214 nfsdstats.rchits++;
215 goto found_entry; 228 goto found_entry;
229
230 /* Try to use the first entry on the LRU */
231 if (!list_empty(&lru_head)) {
232 rp = list_first_entry(&lru_head, struct svc_cacherep, c_lru);
233 if (nfsd_cache_entry_expired(rp) ||
234 num_drc_entries >= max_drc_entries)
235 goto setup_entry;
216 } 236 }
217 nfsdstats.rcmisses++;
218 237
219 /* This loop shouldn't take more than a few iterations normally */ 238 spin_unlock(&cache_lock);
220 { 239 rp = nfsd_reply_cache_alloc();
221 int safe = 0; 240 if (!rp) {
222 list_for_each_entry(rp, &lru_head, c_lru) { 241 dprintk("nfsd: unable to allocate DRC entry!\n");
223 if (rp->c_state != RC_INPROG) 242 return RC_DOIT;
224 break;
225 if (safe++ > CACHESIZE) {
226 printk("nfsd: loop in repcache LRU list\n");
227 cache_disabled = 1;
228 goto out;
229 }
230 } 243 }
244 spin_lock(&cache_lock);
245 ++num_drc_entries;
246
247 /*
248 * Must search again just in case someone inserted one
249 * after we dropped the lock above.
250 */
251 found = nfsd_cache_search(rqstp);
252 if (found) {
253 nfsd_reply_cache_free_locked(rp);
254 rp = found;
255 goto found_entry;
231 } 256 }
232 257
233 /* All entries on the LRU are in-progress. This should not happen */ 258 /*
234 if (&rp->c_lru == &lru_head) { 259 * We're keeping the one we just allocated. Are we now over the
235 static int complaints; 260 * limit? Prune one off the tip of the LRU in trade for the one we
236 261 * just allocated if so.
237 printk(KERN_WARNING "nfsd: all repcache entries locked!\n"); 262 */
238 if (++complaints > 5) { 263 if (num_drc_entries >= max_drc_entries)
239 printk(KERN_WARNING "nfsd: disabling repcache.\n"); 264 nfsd_reply_cache_free_locked(list_first_entry(&lru_head,
240 cache_disabled = 1; 265 struct svc_cacherep, c_lru));
241 }
242 goto out;
243 }
244 266
267setup_entry:
268 nfsdstats.rcmisses++;
245 rqstp->rq_cacherep = rp; 269 rqstp->rq_cacherep = rp;
246 rp->c_state = RC_INPROG; 270 rp->c_state = RC_INPROG;
247 rp->c_xid = xid; 271 rp->c_xid = xid;
@@ -265,6 +289,7 @@ nfsd_cache_lookup(struct svc_rqst *rqstp)
265 return rtn; 289 return rtn;
266 290
267found_entry: 291found_entry:
292 nfsdstats.rchits++;
268 /* We found a matching entry which is either in progress or done. */ 293 /* We found a matching entry which is either in progress or done. */
269 age = jiffies - rp->c_timestamp; 294 age = jiffies - rp->c_timestamp;
270 lru_put_end(rp); 295 lru_put_end(rp);
@@ -295,7 +320,7 @@ found_entry:
295 break; 320 break;
296 default: 321 default:
297 printk(KERN_WARNING "nfsd: bad repcache type %d\n", rp->c_type); 322 printk(KERN_WARNING "nfsd: bad repcache type %d\n", rp->c_type);
298 rp->c_state = RC_UNUSED; 323 nfsd_reply_cache_free_locked(rp);
299 } 324 }
300 325
301 goto out; 326 goto out;