Commit 01a7decf authored by Jeff Layton's avatar Jeff Layton Committed by J. Bruce Fields

nfsd: keep a checksum of the first 256 bytes of request

Now that we're allowing more DRC entries, it becomes a lot easier to hit
problems with XID collisions. In order to mitigate those, calculate a
checksum of up to the first 256 bytes of each request coming in and store
that in the cache entry, along with the total length of the request.

This initially used crc32, but Chuck Lever and Jim Rees pointed out that
crc32 is probably more heavyweight than we really need for generating
these checksums, and recommended looking at using the same routines that
are used to generate checksums for IP packets.

On an x86_64 KVM guest measurements with ftrace showed ~800ns to use
csum_partial vs ~1750ns for crc32.  The difference probably isn't
terribly significant, but for now we may as well use csum_partial.
Signed-off-by: default avatarJeff Layton <jlayton@redhat.com>
Stones-thrown-by: default avatarChuck Lever <chuck.lever@oracle.com>
Signed-off-by: default avatarJ. Bruce Fields <bfields@redhat.com>
parent 4c190e2f
...@@ -29,6 +29,8 @@ struct svc_cacherep { ...@@ -29,6 +29,8 @@ struct svc_cacherep {
u32 c_prot; u32 c_prot;
u32 c_proc; u32 c_proc;
u32 c_vers; u32 c_vers;
unsigned int c_len;
__wsum c_csum;
unsigned long c_timestamp; unsigned long c_timestamp;
union { union {
struct kvec u_vec; struct kvec u_vec;
...@@ -73,6 +75,9 @@ enum { ...@@ -73,6 +75,9 @@ enum {
/* Cache entries expire after this time period */ /* Cache entries expire after this time period */
#define RC_EXPIRE (120 * HZ) #define RC_EXPIRE (120 * HZ)
/* Checksum this amount of the request */
#define RC_CSUMLEN (256U)
int nfsd_reply_cache_init(void); int nfsd_reply_cache_init(void);
void nfsd_reply_cache_shutdown(void); void nfsd_reply_cache_shutdown(void);
int nfsd_cache_lookup(struct svc_rqst *); int nfsd_cache_lookup(struct svc_rqst *);
......
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
#include <linux/slab.h> #include <linux/slab.h>
#include <linux/sunrpc/addr.h> #include <linux/sunrpc/addr.h>
#include <linux/highmem.h> #include <linux/highmem.h>
#include <net/checksum.h>
#include "nfsd.h" #include "nfsd.h"
#include "cache.h" #include "cache.h"
...@@ -130,6 +131,7 @@ int nfsd_reply_cache_init(void) ...@@ -130,6 +131,7 @@ int nfsd_reply_cache_init(void)
INIT_LIST_HEAD(&lru_head); INIT_LIST_HEAD(&lru_head);
max_drc_entries = nfsd_cache_size_limit(); max_drc_entries = nfsd_cache_size_limit();
num_drc_entries = 0; num_drc_entries = 0;
return 0; return 0;
out_nomem: out_nomem:
printk(KERN_ERR "nfsd: failed to allocate reply cache\n"); printk(KERN_ERR "nfsd: failed to allocate reply cache\n");
...@@ -237,13 +239,46 @@ nfsd_reply_cache_shrink(struct shrinker *shrink, struct shrink_control *sc) ...@@ -237,13 +239,46 @@ nfsd_reply_cache_shrink(struct shrinker *shrink, struct shrink_control *sc)
return num; return num;
} }
/*
* Walk an xdr_buf and get a CRC for at most the first RC_CSUMLEN bytes
*/
static __wsum
nfsd_cache_csum(struct svc_rqst *rqstp)
{
int idx;
unsigned int base;
__wsum csum;
struct xdr_buf *buf = &rqstp->rq_arg;
const unsigned char *p = buf->head[0].iov_base;
size_t csum_len = min_t(size_t, buf->head[0].iov_len + buf->page_len,
RC_CSUMLEN);
size_t len = min(buf->head[0].iov_len, csum_len);
/* rq_arg.head first */
csum = csum_partial(p, len, 0);
csum_len -= len;
/* Continue into page array */
idx = buf->page_base / PAGE_SIZE;
base = buf->page_base & ~PAGE_MASK;
while (csum_len) {
p = page_address(buf->pages[idx]) + base;
len = min(PAGE_SIZE - base, csum_len);
csum = csum_partial(p, len, csum);
csum_len -= len;
base = 0;
++idx;
}
return csum;
}
/* /*
* Search the request hash for an entry that matches the given rqstp. * Search the request hash for an entry that matches the given rqstp.
* Must be called with cache_lock held. Returns the found entry or * Must be called with cache_lock held. Returns the found entry or
* NULL on failure. * NULL on failure.
*/ */
static struct svc_cacherep * static struct svc_cacherep *
nfsd_cache_search(struct svc_rqst *rqstp) nfsd_cache_search(struct svc_rqst *rqstp, __wsum csum)
{ {
struct svc_cacherep *rp; struct svc_cacherep *rp;
struct hlist_node *hn; struct hlist_node *hn;
...@@ -257,6 +292,7 @@ nfsd_cache_search(struct svc_rqst *rqstp) ...@@ -257,6 +292,7 @@ nfsd_cache_search(struct svc_rqst *rqstp)
hlist_for_each_entry(rp, hn, rh, c_hash) { hlist_for_each_entry(rp, hn, rh, c_hash) {
if (xid == rp->c_xid && proc == rp->c_proc && if (xid == rp->c_xid && proc == rp->c_proc &&
proto == rp->c_prot && vers == rp->c_vers && proto == rp->c_prot && vers == rp->c_vers &&
rqstp->rq_arg.len == rp->c_len && csum == rp->c_csum &&
rpc_cmp_addr(svc_addr(rqstp), (struct sockaddr *)&rp->c_addr) && rpc_cmp_addr(svc_addr(rqstp), (struct sockaddr *)&rp->c_addr) &&
rpc_get_port(svc_addr(rqstp)) == rpc_get_port((struct sockaddr *)&rp->c_addr)) rpc_get_port(svc_addr(rqstp)) == rpc_get_port((struct sockaddr *)&rp->c_addr))
return rp; return rp;
...@@ -277,6 +313,7 @@ nfsd_cache_lookup(struct svc_rqst *rqstp) ...@@ -277,6 +313,7 @@ nfsd_cache_lookup(struct svc_rqst *rqstp)
u32 proto = rqstp->rq_prot, u32 proto = rqstp->rq_prot,
vers = rqstp->rq_vers, vers = rqstp->rq_vers,
proc = rqstp->rq_proc; proc = rqstp->rq_proc;
__wsum csum;
unsigned long age; unsigned long age;
int type = rqstp->rq_cachetype; int type = rqstp->rq_cachetype;
int rtn; int rtn;
...@@ -287,10 +324,12 @@ nfsd_cache_lookup(struct svc_rqst *rqstp) ...@@ -287,10 +324,12 @@ nfsd_cache_lookup(struct svc_rqst *rqstp)
return RC_DOIT; return RC_DOIT;
} }
csum = nfsd_cache_csum(rqstp);
spin_lock(&cache_lock); spin_lock(&cache_lock);
rtn = RC_DOIT; rtn = RC_DOIT;
rp = nfsd_cache_search(rqstp); rp = nfsd_cache_search(rqstp, csum);
if (rp) if (rp)
goto found_entry; goto found_entry;
...@@ -318,7 +357,7 @@ nfsd_cache_lookup(struct svc_rqst *rqstp) ...@@ -318,7 +357,7 @@ nfsd_cache_lookup(struct svc_rqst *rqstp)
* Must search again just in case someone inserted one * Must search again just in case someone inserted one
* after we dropped the lock above. * after we dropped the lock above.
*/ */
found = nfsd_cache_search(rqstp); found = nfsd_cache_search(rqstp, csum);
if (found) { if (found) {
nfsd_reply_cache_free_locked(rp); nfsd_reply_cache_free_locked(rp);
rp = found; rp = found;
...@@ -344,6 +383,8 @@ nfsd_cache_lookup(struct svc_rqst *rqstp) ...@@ -344,6 +383,8 @@ nfsd_cache_lookup(struct svc_rqst *rqstp)
rpc_set_port((struct sockaddr *)&rp->c_addr, rpc_get_port(svc_addr(rqstp))); rpc_set_port((struct sockaddr *)&rp->c_addr, rpc_get_port(svc_addr(rqstp)));
rp->c_prot = proto; rp->c_prot = proto;
rp->c_vers = vers; rp->c_vers = vers;
rp->c_len = rqstp->rq_arg.len;
rp->c_csum = csum;
hash_refile(rp); hash_refile(rp);
lru_put_end(rp); lru_put_end(rp);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment