[PATCH] for smbd: High CPU usage (sendfile() EAGAIN spin)
Jeremy Allison
jra at samba.org
Thu Jul 19 17:13:05 UTC 2018
On Thu, Jul 19, 2018 at 07:40:27AM +0200, Stefan Metzmacher wrote:
>
> What about changing to blocking mode only when we actually got EAGAIN?
Here is a version that does that for all platforms.
If you or Volker could take a look I'd appreciate it !
Please review and push if happy :-).
Cheers,
Jeremy
-------------- next part --------------
From 062d10b7d1cef84985557fa977a79dab94745f6a Mon Sep 17 00:00:00 2001
From: Jeremy Allison <jra at samba.org>
Date: Wed, 18 Jul 2018 13:32:49 -0700
Subject: [PATCH 1/5] s3: smbd: Fix Linux sendfile() for SMB2. Ensure we don't
spin on EAGAIN.
For SMB2 the socket is set non-blocking. Ensure sendfile()
calls complete if they return EAGAIN by saving the socket state,
setting it blocking, doing the sendfile until completion and then
restoring the socket state.
BUG: https://bugzilla.samba.org/show_bug.cgi?id=13537
Signed-off-by: Jeremy Allison <jra at samba.org>
---
source3/lib/sendfile.c | 84 ++++++++++++++++++++++++++++++++++++++----
1 file changed, 77 insertions(+), 7 deletions(-)
diff --git a/source3/lib/sendfile.c b/source3/lib/sendfile.c
index 3d457bd6f13..f1b0ef91850 100644
--- a/source3/lib/sendfile.c
+++ b/source3/lib/sendfile.c
@@ -24,6 +24,7 @@
*/
#include "includes.h"
+#include "system/filesys.h"
#if defined(LINUX_SENDFILE_API)
@@ -36,8 +37,11 @@
ssize_t sys_sendfile(int tofd, int fromfd, const DATA_BLOB *header, off_t offset, size_t count)
{
size_t total=0;
- ssize_t ret;
+ ssize_t ret = -1;
size_t hdr_len = 0;
+ int saved_errno = 0;
+ int old_flags = 0;
+ bool socket_flags_changed = false;
/*
* Send the header first.
@@ -48,8 +52,25 @@ ssize_t sys_sendfile(int tofd, int fromfd, const DATA_BLOB *header, off_t offset
hdr_len = header->length;
while (total < hdr_len) {
ret = sys_send(tofd, header->data + total,hdr_len - total, MSG_MORE);
- if (ret == -1)
- return -1;
+ if (ret == -1) {
+ if (errno == EAGAIN || errno == EWOULDBLOCK) {
+ /*
+ * send() must complete before we can
+ * send any other outgoing data on the
+ * socket. Ensure socket is in blocking
+ * mode. For SMB2 by default the socket
+ * is in non-blocking mode.
+ */
+ old_flags = fcntl(tofd, F_GETFL, 0);
+ ret = set_blocking(tofd, true);
+ if (ret == -1) {
+ goto out;
+ }
+ socket_flags_changed = true;
+ continue;
+ }
+ goto out;
+ }
total += ret;
}
}
@@ -59,8 +80,34 @@ ssize_t sys_sendfile(int tofd, int fromfd, const DATA_BLOB *header, off_t offset
ssize_t nwritten;
do {
nwritten = sendfile(tofd, fromfd, &offset, total);
- } while (nwritten == -1 && (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK));
+ } while (nwritten == -1 && errno == EINTR);
if (nwritten == -1) {
+ if (errno == EAGAIN || errno == EWOULDBLOCK) {
+ if (socket_flags_changed) {
+ /*
+ * We're already in blocking
+ * mode. This is an error.
+ */
+ ret = -1;
+ goto out;
+ }
+
+ /*
+ * Sendfile must complete before we can
+ * send any other outgoing data on the socket.
+ * Ensure socket is in blocking mode.
+ * For SMB2 by default the socket is in
+ * non-blocking mode.
+ */
+ old_flags = fcntl(tofd, F_GETFL, 0);
+ ret = set_blocking(tofd, true);
+ if (ret == -1) {
+ goto out;
+ }
+ socket_flags_changed = true;
+ continue;
+ }
+
if (errno == ENOSYS || errno == EINVAL) {
/* Ok - we're in a world of pain here. We just sent
* the header, but the sendfile failed. We have to
@@ -72,17 +119,40 @@ ssize_t sys_sendfile(int tofd, int fromfd, const DATA_BLOB *header, off_t offset
*/
errno = EINTR; /* Normally we can never return this. */
}
- return -1;
+ ret = -1;
+ goto out;
}
if (nwritten == 0) {
/*
* EOF, return a short read
*/
- return hdr_len + (count - total);
+ ret = hdr_len + (count - total);
+ goto out;
}
total -= nwritten;
}
- return count + hdr_len;
+
+ ret = count + hdr_len;
+
+ out:
+
+ if (ret == -1) {
+ saved_errno = errno;
+ }
+
+ if (socket_flags_changed) {
+ /* Restore the old state of the socket. */
+ int err = fcntl(tofd, F_SETFL, old_flags);
+ if (err == -1) {
+ return -1;
+ }
+ }
+
+ if (ret == -1) {
+ errno = saved_errno;
+ }
+
+ return ret;
}
#elif defined(SOLARIS_SENDFILE_API)
--
2.18.0.203.gfac676dfb9-goog
From 1ae8242d26d81c663061c789a64edde4e0cbb887 Mon Sep 17 00:00:00 2001
From: Jeremy Allison <jra at samba.org>
Date: Wed, 18 Jul 2018 15:29:37 -0700
Subject: [PATCH 2/5] s3: smbd: Fix Solaris sendfile() for SMB2. Ensure we
don't spin on EAGAIN.
For SMB2 the socket is set non-blocking. Ensure sendfile()
calls complete if they return EAGAIN by saving the socket state,
setting it blocking, doing the sendfile until completion and then
restoring the socket state.
BUG: https://bugzilla.samba.org/show_bug.cgi?id=13537
Signed-off-by: Jeremy Allison <jra at samba.org>
---
source3/lib/sendfile.c | 56 +++++++++++++++++++++++++++++++++++++-----
1 file changed, 50 insertions(+), 6 deletions(-)
diff --git a/source3/lib/sendfile.c b/source3/lib/sendfile.c
index f1b0ef91850..e2d2d8689c6 100644
--- a/source3/lib/sendfile.c
+++ b/source3/lib/sendfile.c
@@ -169,6 +169,10 @@ ssize_t sys_sendfile(int tofd, int fromfd, const DATA_BLOB *header, off_t offset
size_t total, xferred;
struct sendfilevec vec[2];
ssize_t hdr_len = 0;
+ int saved_errno = 0;
+ int old_flags = 0;
+ ssize_t ret = -1;
+ bool socket_flags_changed = false;
if (header) {
sfvcnt = 2;
@@ -205,17 +209,37 @@ ssize_t sys_sendfile(int tofd, int fromfd, const DATA_BLOB *header, off_t offset
xferred = 0;
nwritten = sendfilev(tofd, vec, sfvcnt, &xferred);
- if (nwritten == -1 && (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK)) {
+ if (nwritten == -1 && errno == EINTR) {
if (xferred == 0)
continue; /* Nothing written yet. */
else
nwritten = xferred;
}
- if (nwritten == -1)
- return -1;
- if (nwritten == 0)
- return -1; /* I think we're at EOF here... */
+ if (nwritten == -1) {
+ if (errno == EAGAIN || errno == EWOULDBLOCK) {
+ /*
+ * Sendfile must complete before we can
+ * send any other outgoing data on the socket.
+ * Ensure socket is in blocking mode.
+ * For SMB2 by default the socket is in
+ * non-blocking mode.
+ */
+ old_flags = fcntl(tofd, F_GETFL, 0);
+ ret = set_blocking(tofd, true);
+ if (ret == -1) {
+ goto out;
+ }
+ socket_flags_changed = true;
+ continue;
+ }
+ ret = -1;
+ goto out;
+ }
+ if (nwritten == 0) {
+ ret = -1;
+ goto out; /* I think we're at EOF here... */
+ }
/*
* If this was a short (signal interrupted) write we may need
@@ -237,7 +261,27 @@ ssize_t sys_sendfile(int tofd, int fromfd, const DATA_BLOB *header, off_t offset
}
total -= nwritten;
}
- return count + hdr_len;
+ ret = count + hdr_len;
+
+ out:
+
+ if (ret == -1) {
+ saved_errno = errno;
+ }
+
+ if (socket_flags_changed) {
+ /* Restore the old state of the socket. */
+ int err = fcntl(tofd, F_SETFL, old_flags);
+ if (err == -1) {
+ return -1;
+ }
+ }
+
+ if (ret == -1) {
+ errno = saved_errno;
+ }
+
+ return ret;
}
#elif defined(HPUX_SENDFILE_API)
--
2.18.0.203.gfac676dfb9-goog
From 6846948fabfb372ca56ef32c9e3bf0852d9acf62 Mon Sep 17 00:00:00 2001
From: Jeremy Allison <jra at samba.org>
Date: Wed, 18 Jul 2018 15:36:47 -0700
Subject: [PATCH 3/5] s3: smbd: Fix HPUX sendfile() for SMB2. Ensure we don't
spin on EAGAIN.
For SMB2 the socket is set non-blocking. Ensure sendfile()
calls complete if they return EAGAIN by saving the socket state,
setting it blocking, doing the sendfile until completion and then
restoring the socket state.
BUG: https://bugzilla.samba.org/show_bug.cgi?id=13537
Signed-off-by: Jeremy Allison <jra at samba.org>
---
source3/lib/sendfile.c | 56 +++++++++++++++++++++++++++++++++++++-----
1 file changed, 50 insertions(+), 6 deletions(-)
diff --git a/source3/lib/sendfile.c b/source3/lib/sendfile.c
index e2d2d8689c6..be253b8b48d 100644
--- a/source3/lib/sendfile.c
+++ b/source3/lib/sendfile.c
@@ -294,6 +294,10 @@ ssize_t sys_sendfile(int tofd, int fromfd, const DATA_BLOB *header, off_t offset
size_t total=0;
struct iovec hdtrl[2];
size_t hdr_len = 0;
+ int saved_errno = 0;
+ int old_flags = 0;
+ ssize_t ret = -1;
+ bool socket_flags_changed = false;
if (header) {
/* Set up the header/trailer iovec. */
@@ -319,11 +323,31 @@ ssize_t sys_sendfile(int tofd, int fromfd, const DATA_BLOB *header, off_t offset
do {
nwritten = sendfile(tofd, fromfd, offset, total, &hdtrl[0], 0);
- } while (nwritten == -1 && (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK));
- if (nwritten == -1)
- return -1;
- if (nwritten == 0)
- return -1; /* I think we're at EOF here... */
+ } while (nwritten == -1 && errno == EINTR);
+ if (nwritten == -1) {
+ if (errno == EAGAIN || errno == EWOULDBLOCK) {
+ /*
+ * Sendfile must complete before we can
+ * send any other outgoing data on the socket.
+ * Ensure socket is in blocking mode.
+ * For SMB2 by default the socket is in
+ * non-blocking mode.
+ */
+ old_flags = fcntl(tofd, F_GETFL, 0);
+ ret = set_blocking(tofd, true);
+ if (ret == -1) {
+ goto out;
+ }
+ socket_flags_changed = true;
+ continue;
+ }
+ ret = -1;
+ goto out;
+ }
+ if (nwritten == 0) {
+ ret = -1; /* I think we're at EOF here... */
+ goto out;
+ }
/*
* If this was a short (signal interrupted) write we may need
@@ -347,7 +371,27 @@ ssize_t sys_sendfile(int tofd, int fromfd, const DATA_BLOB *header, off_t offset
total -= nwritten;
offset += nwritten;
}
- return count + hdr_len;
+ ret = count + hdr_len;
+
+ out:
+
+ if (ret == -1) {
+ saved_errno = errno;
+ }
+
+ if (socket_flags_changed) {
+ /* Restore the old state of the socket. */
+ int err = fcntl(tofd, F_SETFL, old_flags);
+ if (err == -1) {
+ return -1;
+ }
+ }
+
+ if (ret == -1) {
+ errno = saved_errno;
+ }
+
+ return ret;
}
#elif defined(FREEBSD_SENDFILE_API) || defined(DARWIN_SENDFILE_API)
--
2.18.0.203.gfac676dfb9-goog
From 46da3348d8518a64d435a781f40e2c6b6af9901c Mon Sep 17 00:00:00 2001
From: Jeremy Allison <jra at samba.org>
Date: Wed, 18 Jul 2018 15:44:34 -0700
Subject: [PATCH 4/5] s3: smbd: Fix FreeBSD sendfile() for SMB2. Ensure we
don't spin on EAGAIN.
For SMB2 the socket is set non-blocking. Ensure sendfile()
calls complete if they return EAGAIN by saving the socket state,
setting it blocking, doing the sendfile until completion and then
restoring the socket state.
BUG: https://bugzilla.samba.org/show_bug.cgi?id=13537
Signed-off-by: Jeremy Allison <jra at samba.org>
---
source3/lib/sendfile.c | 48 ++++++++++++++++++++++++++++++++++++++----
1 file changed, 44 insertions(+), 4 deletions(-)
diff --git a/source3/lib/sendfile.c b/source3/lib/sendfile.c
index be253b8b48d..2ac5a1c5764 100644
--- a/source3/lib/sendfile.c
+++ b/source3/lib/sendfile.c
@@ -405,9 +405,12 @@ ssize_t sys_sendfile(int tofd, int fromfd,
{
struct sf_hdtr sf_header = {0};
struct iovec io_header = {0};
+ int saved_errno = 0;
+ int old_flags = 0;
off_t nwritten;
- int ret;
+ ssize_t ret = -1;
+ bool socket_flags_changed = false;
if (header) {
sf_header.headers = &io_header;
@@ -428,9 +431,26 @@ ssize_t sys_sendfile(int tofd, int fromfd,
#else
ret = sendfile(fromfd, tofd, offset, count, &sf_header, &nwritten, 0);
#endif
- if (ret == -1 && errno != EINTR && errno != EAGAIN && errno != EWOULDBLOCK) {
+ if (ret == -1 && errno != EINTR) {
+ if (errno == EAGAIN || errno == EWOULDBLOCK) {
+ /*
+ * Sendfile must complete before we can
+ * send any other outgoing data on the socket.
+ * Ensure socket is in blocking mode.
+ * For SMB2 by default the socket is in
+ * non-blocking mode.
+ */
+ old_flags = fcntl(tofd, F_GETFL, 0);
+ ret = set_blocking(tofd, true);
+ if (ret == -1) {
+ goto out;
+ }
+ socket_flags_changed = true;
+ continue;
+ }
/* Send failed, we are toast. */
- return -1;
+ ret = -1;
+ goto out;
}
if (nwritten == 0) {
@@ -457,7 +477,27 @@ ssize_t sys_sendfile(int tofd, int fromfd,
count -= nwritten;
}
- return nwritten;
+ ret = nwritten;
+
+ out:
+
+ if (ret == -1) {
+ saved_errno = errno;
+ }
+
+ if (socket_flags_changed) {
+ /* Restore the old state of the socket. */
+ int err = fcntl(tofd, F_SETFL, old_flags);
+ if (err == -1) {
+ return -1;
+ }
+ }
+
+ if (ret == -1) {
+ errno = saved_errno;
+ }
+
+ return ret;
}
#elif defined(AIX_SENDFILE_API)
--
2.18.0.203.gfac676dfb9-goog
From ada965e8682ede2577f6844be290f53bb40a3dc9 Mon Sep 17 00:00:00 2001
From: Jeremy Allison <jra at samba.org>
Date: Wed, 18 Jul 2018 15:49:29 -0700
Subject: [PATCH 5/5] s3: smbd: Fix AIX sendfile() for SMB2. Ensure we don't
spin on EAGAIN.
For SMB2 the socket is set non-blocking. Ensure sendfile()
calls complete if they return EAGAIN by saving the socket state,
setting it blocking, doing the sendfile until completion and then
restoring the socket state.
BUG: https://bugzilla.samba.org/show_bug.cgi?id=13537
Signed-off-by: Jeremy Allison <jra at samba.org>
---
source3/lib/sendfile.c | 49 +++++++++++++++++++++++++++++++++++++-----
1 file changed, 44 insertions(+), 5 deletions(-)
diff --git a/source3/lib/sendfile.c b/source3/lib/sendfile.c
index 2ac5a1c5764..9e1ee50cd5a 100644
--- a/source3/lib/sendfile.c
+++ b/source3/lib/sendfile.c
@@ -510,6 +510,10 @@ ssize_t sys_sendfile(int tofd, int fromfd,
ssize_t sys_sendfile(int tofd, int fromfd, const DATA_BLOB *header, off_t offset, size_t count)
{
struct sf_parms hdtrl;
+ int saved_errno = 0;
+ int old_flags = 0;
+ ssize_t ret = -1;
+ bool socket_flags_changed = false;
/* Set up the header/trailer struct params. */
if (header) {
@@ -527,8 +531,6 @@ ssize_t sys_sendfile(int tofd, int fromfd, const DATA_BLOB *header, off_t offset
hdtrl.file_bytes = count;
while ( hdtrl.file_bytes + hdtrl.header_length ) {
- ssize_t ret;
-
/*
Return Value
@@ -546,12 +548,49 @@ ssize_t sys_sendfile(int tofd, int fromfd, const DATA_BLOB *header, off_t offset
*/
do {
ret = send_file(&tofd, &hdtrl, 0);
- } while ((ret == 1) || (ret == -1 && (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK)));
- if ( ret == -1 )
+ } while ((ret == 1) || (ret == -1 && errno == EINTR));
+ if ( ret == -1 ) {
+ if (errno == EAGAIN || errno == EWOULDBLOCK) {
+ /*
+ * Sendfile must complete before we can
+ * send any other outgoing data on the socket.
+ * Ensure socket is in blocking mode.
+ * For SMB2 by default the socket is in
+ * non-blocking mode.
+ */
+ old_flags = fcntl(tofd, F_GETFL, 0);
+ ret = set_blocking(tofd, true);
+ if (ret == -1) {
+ goto out;
+ }
+ socket_flags_changed = true;
+ continue;
+ }
+ goto out;
+ }
+ }
+
+ ret = count + header->length;
+
+ out:
+
+ if (ret == -1) {
+ saved_errno = errno;
+ }
+
+ if (socket_flags_changed) {
+ /* Restore the old state of the socket. */
+ int err = fcntl(tofd, F_SETFL, old_flags);
+ if (err == -1) {
return -1;
+ }
}
- return count + header->length;
+ if (ret == -1) {
+ errno = saved_errno;
+ }
+
+ return ret;
}
/* END AIX SEND_FILE */
--
2.18.0.203.gfac676dfb9-goog
More information about the samba-technical
mailing list