Skip to contentSkip to conversation

nfsd: Clean up nfs4_make_rec_clidname()

PATCH In review Eric Biggers 4 participants 6 messages Aug 4, 2025 – Jun 3

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v2 cover nfsd: Clean up nfs4_make_rec_clidname()

Two cleanups for the MD5 calculation and formatting in nfs4_make_rec_clidname().

Eric Biggers (2):
nfsd: Replace open-coded conversion of bytes to hex
nfsd: Eliminate an allocation in nfs4_make_rec_clidname()

 fs/nfsd/nfs4recover.c | 31 +++++--------------------------
 fs/nfsd/state.h       |  4 +++-
 2 files changed, 8 insertions(+), 27 deletions(-)

base-commit: 35a813e010b99894bb4706c56c16a580bf7959c2

Signature · 1 line
2.50.1.565.gc32cd1483b-goog
#1 lore raw

PATCH v2 1/2 nfsd: Replace open-coded conversion of bytes to hex

Since the Linux kernel's sprintf() has conversion to hex built-in via "%*phN", delete md5_to_hex() and just use that. Also add an explicit array bound to the dname parameter of nfs4_make_rec_clidname() to make its size clear. No functional change.

---
 fs/nfsd/nfs4recover.c | 18 ++----------------
 1 file changed, 2 insertions(+), 16 deletions(-)
fs/nfsd/nfs4recover.c +2 -16
index 2231192ec33f..54f5e5392ef9 100644
@@ -90,26 +90,12 @@ static void
 nfs4_reset_creds(const struct cred *original)
 {
 	put_cred(revert_creds(original));
 }
 
-static void
-md5_to_hex(char *out, char *md5)
-{
-	int i;
-
-	for (i=0; i<16; i++) {
-		unsigned char c = md5[i];
-
-		*out++ = '0' + ((c&0xf0)>>4) + (c>=0xa0)*('a'-'9'-1);
-		*out++ = '0' + (c&0x0f) + ((c&0x0f)>=0x0a)*('a'-'9'-1);
-	}
-	*out = '\0';
-}
-
 static int
-nfs4_make_rec_clidname(char *dname, const struct xdr_netobj *clname)
+nfs4_make_rec_clidname(char dname[HEXDIR_LEN], const struct xdr_netobj *clname)
 {
 	struct xdr_netobj cksum;
 	struct crypto_shash *tfm;
 	int status;
 
@@ -131,11 +117,11 @@ nfs4_make_rec_clidname(char *dname, const struct xdr_netobj *clname)
 	status = crypto_shash_tfm_digest(tfm, clname->data, clname->len,
 					 cksum.data);
 	if (status)
 		goto out;
 
-	md5_to_hex(dname, cksum.data);
+	sprintf(dname, "%*phN", 16, cksum.data);
 
 	status = 0;
 out:
 	kfree(cksum.data);
 	crypto_free_shash(tfm);
Signature · 1 line
2.50.1.565.gc32cd1483b-goog
#2 lore raw

Re: [PATCH v2 1/2] nfsd: Replace open-coded conversion of bytes to hex

Quoted 4 lines · Eric Biggers

Since the Linux kernel's sprintf() has conversion to hex built-in via "%*phN", delete md5_to_hex() and just use that. Also add an explicit array bound to the dname parameter of nfs4_make_rec_clidname() to make its size clear. No functional change.

...

fs/nfsd/nfs4recover.c nfs4_make_rec_clidname(char *dname, const struct xdr_netobj *clname) · line 122

Quoted 1 line
fs/nfsd/nfs4recover.c +1 -0
+	sprintf(dname, "%*phN", 16, cksum.data);

FWIW, since the length is static no need to use stack for that:

	sprintf(dname, "%16phN", cksum.data);

will be better.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#3 lore raw

PATCH v2 2/2 nfsd: Eliminate an allocation in nfs4_make_rec_clidname()

In reply to Eric Biggers

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.

---
 fs/nfsd/nfs4recover.c | 15 ++++-----------
 fs/nfsd/state.h       |  4 +++-
 2 files changed, 7 insertions(+), 12 deletions(-)
fs/nfsd/nfs4recover.c +4 -11
index 54f5e5392ef9..e2b9472e5c78 100644
@@ -93,11 +93,11 @@ nfs4_reset_creds(const struct cred *original)
 }
 
 static int
 nfs4_make_rec_clidname(char dname[HEXDIR_LEN], const struct xdr_netobj *clname)
 {
-	struct xdr_netobj cksum;
+	u8 digest[MD5_DIGEST_SIZE];
 	struct crypto_shash *tfm;
 	int status;
 
 	dprintk("NFSD: nfs4_make_rec_clidname for %.*s\n",
 			clname->len, clname->data);
@@ -105,27 +105,20 @@ nfs4_make_rec_clidname(char dname[HEXDIR_LEN], const struct xdr_netobj *clname)
 	if (IS_ERR(tfm)) {
 		status = PTR_ERR(tfm);
 		goto out_no_tfm;
 	}
 
-	cksum.len = crypto_shash_digestsize(tfm);
-	cksum.data = kmalloc(cksum.len, GFP_KERNEL);
-	if (cksum.data == NULL) {
-		status = -ENOMEM;
- 		goto out;
-	}
-
 	status = crypto_shash_tfm_digest(tfm, clname->data, clname->len,
-					 cksum.data);
+					 digest);
 	if (status)
 		goto out;
 
-	sprintf(dname, "%*phN", 16, cksum.data);
+	static_assert(HEXDIR_LEN == 2 * MD5_DIGEST_SIZE + 1);
+	sprintf(dname, "%*phN", MD5_DIGEST_SIZE, digest);
 
 	status = 0;
 out:
-	kfree(cksum.data);
 	crypto_free_shash(tfm);
 out_no_tfm:
 	return status;
 }
 
fs/nfsd/state.h +3 -1
index 8adc2550129e..b7d4c6d6f3d4 100644
@@ -33,10 +33,11 @@
  */
 
 #ifndef _NFSD4_STATE_H
 #define _NFSD4_STATE_H
 
+#include <crypto/md5.h>
 #include <linux/idr.h>
 #include <linux/refcount.h>
 #include <linux/sunrpc/svc_xprt.h>
 #include "nfsfh.h"
 #include "nfsd.h"
@@ -379,11 +380,12 @@ struct nfsd4_sessionid {
 	clientid_t	clientid;
 	u32		sequence;
 	u32		reserved;
 };
 
-#define HEXDIR_LEN     33 /* hex version of 16 byte md5 of cl_name plus '\0' */
+/* Length of MD5 digest as hex, plus terminating '\0' */
+#define HEXDIR_LEN	(2 * MD5_DIGEST_SIZE + 1)
 
 /*
  *       State                Meaning                  Where set
  * --------------------------------------------------------------------------
  * | NFSD4_ACTIVE      | Confirmed, active    | Default                     |
Signature · 1 line
2.50.1.565.gc32cd1483b-goog
maintainer Reviewed-by on v2 2/2
#4 lore raw

Re: [PATCH v2 2/2] nfsd: Eliminate an allocation in nfs4_make_rec_clidname()

Quoted 79 lines · Eric Biggers

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.

---
 fs/nfsd/nfs4recover.c | 15 ++++-----------
 fs/nfsd/state.h       |  4 +++-
 2 files changed, 7 insertions(+), 12 deletions(-)
fs/nfsd/nfs4recover.c +4 -11
index 54f5e5392ef9..e2b9472e5c78 100644
@@ -93,11 +93,11 @@ nfs4_reset_creds(const struct cred *original)
 }
 
 static int
 nfs4_make_rec_clidname(char dname[HEXDIR_LEN], const struct xdr_netobj *clname)
 {
-	struct xdr_netobj cksum;
+	u8 digest[MD5_DIGEST_SIZE];
 	struct crypto_shash *tfm;
 	int status;
 
 	dprintk("NFSD: nfs4_make_rec_clidname for %.*s\n",
 			clname->len, clname->data);
@@ -105,27 +105,20 @@ nfs4_make_rec_clidname(char dname[HEXDIR_LEN], const struct xdr_netobj *clname)
 	if (IS_ERR(tfm)) {
 		status = PTR_ERR(tfm);
 		goto out_no_tfm;
 	}
 
-	cksum.len = crypto_shash_digestsize(tfm);
-	cksum.data = kmalloc(cksum.len, GFP_KERNEL);
-	if (cksum.data == NULL) {
-		status = -ENOMEM;
- 		goto out;
-	}
-
 	status = crypto_shash_tfm_digest(tfm, clname->data, clname->len,
-					 cksum.data);
+					 digest);
 	if (status)
 		goto out;
 
-	sprintf(dname, "%*phN", 16, cksum.data);
+	static_assert(HEXDIR_LEN == 2 * MD5_DIGEST_SIZE + 1);
+	sprintf(dname, "%*phN", MD5_DIGEST_SIZE, digest);
 
 	status = 0;
 out:
-	kfree(cksum.data);
 	crypto_free_shash(tfm);
 out_no_tfm:
 	return status;
 }
 
fs/nfsd/state.h +1 -0
index 8adc2550129e..b7d4c6d6f3d4 100644
@@ -33,10 +33,11 @@
  */
 
 #ifndef _NFSD4_STATE_H
 #define _NFSD4_STATE_H
 
+#include <crypto/md5.h>
 #include <linux/idr.h>
 #include <linux/refcount.h>
 #include <linux/sunrpc/svc_xprt.h>
 #include "nfsfh.h"
 #include "nfsd.h"
@@ -379,11 +380,12 @@ struct nfsd4_sessionid {
 	clientid_t	clientid;
 	u32		sequence;
 	u32		reserved;

fs/nfsd/state.h struct nfsd4_sessionid { · line 391

Quoted 10 lines
fs/nfsd/state.h +2 -1
 };
 
-#define HEXDIR_LEN     33 /* hex version of 16 byte md5 of cl_name plus '\0' */
+/* Length of MD5 digest as hex, plus terminating '\0' */
+#define HEXDIR_LEN	(2 * MD5_DIGEST_SIZE + 1)
 
 /*
  *       State                Meaning                  Where set
  * --------------------------------------------------------------------------
  * | NFSD4_ACTIVE      | Confirmed, active    | Default                     |
maintainer
#5 lore raw
In reply to Eric Biggers

From: Chuck Lever <[email protected]>

Quoted 8 lines · Eric Biggers

Two cleanups for the MD5 calculation and formatting in nfs4_make_rec_clidname().

Eric Biggers (2):
nfsd: Replace open-coded conversion of bytes to hex
nfsd: Eliminate an allocation in nfs4_make_rec_clidname()

[...]

Applied v2 to nfsd-testing, thanks!

[1/2] nfsd: Replace open-coded conversion of bytes to hex

      commit: 9093f8308442fc5096e973a46107f8623e2b336b
[2/2] nfsd: Eliminate an allocation in nfs4_make_rec_clidname()
      commit: b0a26370a4129a7e3b477f80c39b08ed5faf1422

--
Chuck Lever