Skip to content

Commit 9ebcd02

Browse files
Eric Biggerschucklever
authored andcommitted
nfsd: Eliminate an allocation in nfs4_make_rec_clidname()
Since MD5 digests are fixed-size, make nfs4_make_rec_clidname() store the digest in a stack buffer instead of a dynamically allocated buffer. Use MD5_DIGEST_SIZE instead of a hard-coded value, both in nfs4_make_rec_clidname() and in the definition of HEXDIR_LEN. Signed-off-by: Eric Biggers <[email protected]> Reviewed-by: Jeff Layton <[email protected]> Signed-off-by: Chuck Lever <[email protected]>
1 parent 17695d7 commit 9ebcd02

File tree

2 files changed

+7
-12
lines changed

2 files changed

+7
-12
lines changed

fs/nfsd/nfs4recover.c

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ nfs4_reset_creds(const struct cred *original)
9595
static int
9696
nfs4_make_rec_clidname(char dname[HEXDIR_LEN], const struct xdr_netobj *clname)
9797
{
98-
struct xdr_netobj cksum;
98+
u8 digest[MD5_DIGEST_SIZE];
9999
struct crypto_shash *tfm;
100100
int status;
101101

@@ -107,23 +107,16 @@ nfs4_make_rec_clidname(char dname[HEXDIR_LEN], const struct xdr_netobj *clname)
107107
goto out_no_tfm;
108108
}
109109

110-
cksum.len = crypto_shash_digestsize(tfm);
111-
cksum.data = kmalloc(cksum.len, GFP_KERNEL);
112-
if (cksum.data == NULL) {
113-
status = -ENOMEM;
114-
goto out;
115-
}
116-
117110
status = crypto_shash_tfm_digest(tfm, clname->data, clname->len,
118-
cksum.data);
111+
digest);
119112
if (status)
120113
goto out;
121114

122-
sprintf(dname, "%*phN", 16, cksum.data);
115+
static_assert(HEXDIR_LEN == 2 * MD5_DIGEST_SIZE + 1);
116+
sprintf(dname, "%*phN", MD5_DIGEST_SIZE, digest);
123117

124118
status = 0;
125119
out:
126-
kfree(cksum.data);
127120
crypto_free_shash(tfm);
128121
out_no_tfm:
129122
return status;

fs/nfsd/state.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#ifndef _NFSD4_STATE_H
3636
#define _NFSD4_STATE_H
3737

38+
#include <crypto/md5.h>
3839
#include <linux/idr.h>
3940
#include <linux/refcount.h>
4041
#include <linux/sunrpc/svc_xprt.h>
@@ -391,7 +392,8 @@ struct nfsd4_sessionid {
391392
u32 reserved;
392393
};
393394

394-
#define HEXDIR_LEN 33 /* hex version of 16 byte md5 of cl_name plus '\0' */
395+
/* Length of MD5 digest as hex, plus terminating '\0' */
396+
#define HEXDIR_LEN (2 * MD5_DIGEST_SIZE + 1)
395397

396398
/*
397399
* State Meaning Where set

0 commit comments

Comments
 (0)