Skip to contentSkip to conversation

cifs: avoid a kmalloc in smb2_send_recv/SendReceive2 for the common case

PATCH In review Ronnie Sahlberg 2 participants 3 messages Nov 21, 2017 – Nov 22, 2017 2 messages not in the archive

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

PATCH 1/1 cifs: avoid a kmalloc in smb2_send_recv/SendReceive2 for the common case

In both functions, use an array of 8 (arbitrary but should be big enough for all current uses) iov and avoid having to kmalloc the array for the common case.

If 8 is too small, then fall back to the original behaviour and use kmalloc/kfree.

This should not change any behaviour but should save us a tiny amount of cpu cycles.

---
 fs/cifs/transport.c | 33 +++++++++++++++++++++++----------
 1 file changed, 23 insertions(+), 10 deletions(-)
fs/cifs/transport.c +23 -10
index e678307bb7a0..510f41a435c8 100644
@@ -38,6 +38,9 @@
 #include "cifsproto.h"
 #include "cifs_debug.h"
 
+/* Max number of iovectors we can use off the stack when sending requests. */
+#define CIFS_MAX_IOV_SIZE 8
+
 void
 cifs_wake_up_task(struct mid_q_entry *mid)
 {
@@ -803,12 +806,16 @@ SendReceive2(const unsigned int xid, struct cifs_ses *ses,
 	     const int flags, struct kvec *resp_iov)
 {
 	struct smb_rqst rqst;
-	struct kvec *new_iov;
+	struct kvec s_iov[CIFS_MAX_IOV_SIZE], *new_iov;
 	int rc;
 
-	new_iov = kmalloc(sizeof(struct kvec) * (n_vec + 1), GFP_KERNEL);
-	if (!new_iov)
-		return -ENOMEM;
+	if (n_vec + 1 > CIFS_MAX_IOV_SIZE) {
+		new_iov = kmalloc(sizeof(struct kvec) * (n_vec + 1),
+				  GFP_KERNEL);
+		if (!new_iov)
+			return -ENOMEM;
+	} else
+		new_iov = s_iov;
 
 	/* 1st iov is a RFC1001 length followed by the rest of the packet */
 	memcpy(new_iov + 1, iov, (sizeof(struct kvec) * n_vec));
@@ -823,7 +830,8 @@ SendReceive2(const unsigned int xid, struct cifs_ses *ses,
 	rqst.rq_nvec = n_vec + 1;
 
 	rc = cifs_send_recv(xid, ses, &rqst, resp_buf_type, flags, resp_iov);
-	kfree(new_iov);
+	if (n_vec + 1 > CIFS_MAX_IOV_SIZE)
+		kfree(new_iov);
 	return rc;
 }
 
@@ -834,15 +842,19 @@ smb2_send_recv(const unsigned int xid, struct cifs_ses *ses,
 	       const int flags, struct kvec *resp_iov)
 {
 	struct smb_rqst rqst;
-	struct kvec *new_iov;
+	struct kvec s_iov[CIFS_MAX_IOV_SIZE], *new_iov;
 	int rc;
 	int i;
 	__u32 count;
 	__be32 rfc1002_marker;
 
-	new_iov = kmalloc(sizeof(struct kvec) * (n_vec + 1), GFP_KERNEL);
-	if (!new_iov)
-		return -ENOMEM;
+	if (n_vec + 1 > CIFS_MAX_IOV_SIZE) {
+		new_iov = kmalloc(sizeof(struct kvec) * (n_vec + 1),
+				  GFP_KERNEL);
+		if (!new_iov)
+			return -ENOMEM;
+	} else
+		new_iov = s_iov;
 
 	/* 1st iov is an RFC1002 Session Message length */
 	memcpy(new_iov + 1, iov, (sizeof(struct kvec) * n_vec));
@@ -861,7 +873,8 @@ smb2_send_recv(const unsigned int xid, struct cifs_ses *ses,
 	rqst.rq_nvec = n_vec + 1;
 
 	rc = cifs_send_recv(xid, ses, &rqst, resp_buf_type, flags, resp_iov);
-	kfree(new_iov);
+	if (n_vec + 1 > CIFS_MAX_IOV_SIZE)
+		kfree(new_iov);
 	return rc;
 }
 
Signature · 1 line
2.13.3

A message that is not in the archive (sent off-list or not yet received): 20171121040807.8364-1-lsahlber-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org

Reviewed-by
#2 lore raw

2017-11-20 20:08 GMT-08:00 Ronnie Sahlberg <[email protected]>:

Quoted 97 lines

In both functions, use an array of 8 (arbitrary but should be big enough for all current uses) iov and avoid having to kmalloc the array for the common case.

If 8 is too small, then fall back to the original behaviour and use kmalloc/kfree.

This should not change any behaviour but should save us a tiny amount of cpu cycles.

---
 fs/cifs/transport.c | 33 +++++++++++++++++++++++----------
 1 file changed, 23 insertions(+), 10 deletions(-)
fs/cifs/transport.c +23 -11
index e678307bb7a0..510f41a435c8 100644
@@ -38,6 +38,9 @@
 #include "cifsproto.h"
 #include "cifs_debug.h"
 
+/* Max number of iovectors we can use off the stack when sending requests. */
+#define CIFS_MAX_IOV_SIZE 8
+
 void
 cifs_wake_up_task(struct mid_q_entry *mid)
 {
@@ -803,12 +806,16 @@ SendReceive2(const unsigned int xid, struct cifs_ses *ses,
             const int flags, struct kvec *resp_iov)
 {
        struct smb_rqst rqst;
-       struct kvec *new_iov;
+       struct kvec s_iov[CIFS_MAX_IOV_SIZE], *new_iov;
        int rc;
 
-       new_iov = kmalloc(sizeof(struct kvec) * (n_vec + 1), GFP_KERNEL);
-       if (!new_iov)
-               return -ENOMEM;
+       if (n_vec + 1 > CIFS_MAX_IOV_SIZE) {
+               new_iov = kmalloc(sizeof(struct kvec) * (n_vec + 1),
+                                 GFP_KERNEL);
+               if (!new_iov)
+                       return -ENOMEM;
+       } else
+               new_iov = s_iov;
 
        /* 1st iov is a RFC1001 length followed by the rest of the packet */
        memcpy(new_iov + 1, iov, (sizeof(struct kvec) * n_vec));
@@ -823,7 +830,8 @@ SendReceive2(const unsigned int xid, struct cifs_ses *ses,
        rqst.rq_nvec = n_vec + 1;
 
        rc = cifs_send_recv(xid, ses, &rqst, resp_buf_type, flags, resp_iov);
-       kfree(new_iov);
+       if (n_vec + 1 > CIFS_MAX_IOV_SIZE)
+               kfree(new_iov);
        return rc;
 }
 
@@ -834,15 +842,19 @@ smb2_send_recv(const unsigned int xid, struct cifs_ses *ses,
               const int flags, struct kvec *resp_iov)
 {
        struct smb_rqst rqst;
-       struct kvec *new_iov;
+       struct kvec s_iov[CIFS_MAX_IOV_SIZE], *new_iov;
        int rc;
        int i;
        __u32 count;
        __be32 rfc1002_marker;
 
-       new_iov = kmalloc(sizeof(struct kvec) * (n_vec + 1), GFP_KERNEL);
-       if (!new_iov)
-               return -ENOMEM;
+       if (n_vec + 1 > CIFS_MAX_IOV_SIZE) {
+               new_iov = kmalloc(sizeof(struct kvec) * (n_vec + 1),
+                                 GFP_KERNEL);
+               if (!new_iov)
+                       return -ENOMEM;
+       } else
+               new_iov = s_iov;
 
        /* 1st iov is an RFC1002 Session Message length */
        memcpy(new_iov + 1, iov, (sizeof(struct kvec) * n_vec));
@@ -861,7 +873,8 @@ smb2_send_recv(const unsigned int xid, struct cifs_ses *ses,
        rqst.rq_nvec = n_vec + 1;
 
        rc = cifs_send_recv(xid, ses, &rqst, resp_buf_type, flags, resp_iov);
-       kfree(new_iov);
+       if (n_vec + 1 > CIFS_MAX_IOV_SIZE)
+               kfree(new_iov);
        return rc;
 }
 
--

2.13.3

Quoted 4 lines

--
To unsubscribe from this list: send the line "unsubscribe linux-cifs" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

Thanks for the patch!

Should we split it into two and mark the 1st part for stable? This would address possible performance degradation in previous kernels because SendReceive2() was changed to use kmalloc() several kernels ago. I didn't measure a performance difference, so not sure if it is worth the effort.

In both cases (as one patch or two), you can add my

--
Best regards,
Pavel Shilovsky

A message that is not in the archive (sent off-list or not yet received): CY4PR21MB013522818F6E904053FA08D4B6200-kUhI0YP1syr0Ny/[email protected]

#4 lore raw

----- Original Message -----

Quoted 121 lines

From: "Pavel Shilovskiy" <[email protected]>
To: "Ronnie Sahlberg" <[email protected]>, "Steve French" <[email protected]>
Cc: "linux-cifs" <[email protected]>
Sent: Wednesday, 22 November, 2017 11:06:38 AM
Subject: RE: [PATCH] cifs: avoid a kmalloc in smb2_send_recv/SendReceive2 for the common case

2017-11-20 20:08 GMT-08:00 Ronnie Sahlberg <[email protected]>:

Quoted 102 lines

In both functions, use an array of 8 (arbitrary but should be big enough for all current uses) iov and avoid having to kmalloc the array for the common case.

If 8 is too small, then fall back to the original behaviour and use kmalloc/kfree.

This should not change any behaviour but should save us a tiny amount of cpu cycles.

---
 fs/cifs/transport.c | 33 +++++++++++++++++++++++----------
 1 file changed, 23 insertions(+), 10 deletions(-)
fs/cifs/transport.c +3 -0
index e678307bb7a0..510f41a435c8 100644
@@ -38,6 +38,9 @@
 #include "cifsproto.h"
 #include "cifs_debug.h"
 
+/* Max number of iovectors we can use off the stack when sending requests.
 */
+#define CIFS_MAX_IOV_SIZE 8
+
 void
 cifs_wake_up_task(struct mid_q_entry *mid)

{

fs/cifs/transport.c +8 -4
@@ -803,12 +806,16 @@ SendReceive2(const unsigned int xid, struct cifs_ses
 *ses,
             const int flags, struct kvec *resp_iov)
 {
        struct smb_rqst rqst;
-       struct kvec *new_iov;
+       struct kvec s_iov[CIFS_MAX_IOV_SIZE], *new_iov;
        int rc;
 
-       new_iov = kmalloc(sizeof(struct kvec) * (n_vec + 1), GFP_KERNEL);
-       if (!new_iov)
-               return -ENOMEM;
+       if (n_vec + 1 > CIFS_MAX_IOV_SIZE) {
+               new_iov = kmalloc(sizeof(struct kvec) * (n_vec + 1),
+                                 GFP_KERNEL);
+               if (!new_iov)
+                       return -ENOMEM;
+       } else
+               new_iov = s_iov;
 
        /* 1st iov is a RFC1001 length followed by the rest of the packet
        */
        memcpy(new_iov + 1, iov, (sizeof(struct kvec) * n_vec));
fs/cifs/transport.c +2 -1
@@ -823,7 +830,8 @@ SendReceive2(const unsigned int xid, struct cifs_ses
 *ses,
        rqst.rq_nvec = n_vec + 1;
 
        rc = cifs_send_recv(xid, ses, &rqst, resp_buf_type, flags,
        resp_iov);
-       kfree(new_iov);
+       if (n_vec + 1 > CIFS_MAX_IOV_SIZE)
+               kfree(new_iov);
        return rc;

}

fs/cifs/transport.c +8 -4
@@ -834,15 +842,19 @@ smb2_send_recv(const unsigned int xid, struct
 cifs_ses *ses,
               const int flags, struct kvec *resp_iov)
 {
        struct smb_rqst rqst;
-       struct kvec *new_iov;
+       struct kvec s_iov[CIFS_MAX_IOV_SIZE], *new_iov;
        int rc;
        int i;
        __u32 count;
        __be32 rfc1002_marker;
 
-       new_iov = kmalloc(sizeof(struct kvec) * (n_vec + 1), GFP_KERNEL);
-       if (!new_iov)
-               return -ENOMEM;
+       if (n_vec + 1 > CIFS_MAX_IOV_SIZE) {
+               new_iov = kmalloc(sizeof(struct kvec) * (n_vec + 1),
+                                 GFP_KERNEL);
+               if (!new_iov)
+                       return -ENOMEM;
+       } else
+               new_iov = s_iov;
 
        /* 1st iov is an RFC1002 Session Message length */
        memcpy(new_iov + 1, iov, (sizeof(struct kvec) * n_vec));
fs/cifs/transport.c +2 -1
@@ -861,7 +873,8 @@ smb2_send_recv(const unsigned int xid, struct cifs_ses
 *ses,
        rqst.rq_nvec = n_vec + 1;
 
        rc = cifs_send_recv(xid, ses, &rqst, resp_buf_type, flags,
        resp_iov);
-       kfree(new_iov);
+       if (n_vec + 1 > CIFS_MAX_IOV_SIZE)
+               kfree(new_iov);
        return rc;

}

Quoted 7 lines

--
2.13.3

--
To unsubscribe from this list: send the line "unsubscribe linux-cifs" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

Thanks for the patch!

Quoted 4 lines

Should we split it into two and mark the 1st part for stable? This would address possible performance degradation in previous kernels because SendReceive2() was changed to use kmalloc() several kernels ago. I didn't measure a performance difference, so not sure if it is worth the effort.

Don't know. I leave that to Steve.
It is low risk and probably improves performance a little bit, but it is not a bug so ...

Quoted 9 lines

In both cases (as one patch or two), you can add my

--
Best regards,
Pavel Shilovsky