linux-um archives
 help / color / mirror / Atom feed
From: benjamin@sipsolutions.net
To: linux-um@lists.infradead.org
Cc: Benjamin Berg <benjamin.berg@intel.com>
Subject: [PATCH 3/4] um: chan_user: retry partial writes
Date: Wed, 18 Oct 2023 14:36:42 +0200	[thread overview]
Message-ID: <20231018123643.1255813-3-benjamin@sipsolutions.net> (raw)
In-Reply-To: <20231018123643.1255813-1-benjamin@sipsolutions.net>

From: Benjamin Berg <benjamin.berg@intel.com>

In the next commit, we are going to set the output FD to be blocking.
Once that is done, the write() may be short if an interrupt happens
while it is writing out data. As such, to properly catch an EINTR error,
we need to retry the write.

Signed-off-by: Benjamin Berg <benjamin.berg@intel.com>
---
 arch/um/drivers/chan_user.c | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)

diff --git a/arch/um/drivers/chan_user.c b/arch/um/drivers/chan_user.c
index 29c68820522a..339aae56b6f1 100644
--- a/arch/um/drivers/chan_user.c
+++ b/arch/um/drivers/chan_user.c
@@ -37,11 +37,23 @@ int generic_read(int fd, char *c_out, void *unused)
 
 int generic_write(int fd, const char *buf, int n, void *unused)
 {
+	int written = 0;
 	int err;
 
-	CATCH_EINTR(err = write(fd, buf, n));
-	if (err > 0)
-		return err;
+	/* The FD may be in blocking mode, as such, need to retry short writes,
+	 * they may have been interrupted by a signal.
+	 */
+	do {
+		errno = 0;
+		err = write(fd, buf + written, n - written);
+		if (err > 0) {
+			written += err;
+			continue;
+		}
+	} while (err < 0 && errno == EINTR);
+
+	if (written > 0)
+		return written;
 	else if (errno == EAGAIN)
 		return 0;
 	else if (err == 0)
-- 
2.41.0


_______________________________________________
linux-um mailing list
linux-um@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-um

  parent reply	other threads:[~2023-10-18 12:37 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-10-18 12:36 [PATCH 1/4] um: irqs: process outstanding IRQs when unblocking signals benjamin
2023-10-18 12:36 ` [PATCH 2/4] um: chan_user: catch EINTR when reading and writing benjamin
2023-10-18 12:36 ` benjamin [this message]
2023-10-18 12:36 ` [PATCH 4/4] um: chan: use blocking IO for console output for time-travel benjamin
2023-10-20  9:15 ` [PATCH 1/4] um: irqs: process outstanding IRQs when unblocking signals Benjamin Beichler
2023-10-20  9:26   ` Anton Ivanov
2023-10-20 10:33     ` Benjamin Beichler
2023-10-20  9:59   ` Benjamin Berg
2023-10-20 10:38     ` Benjamin Beichler
2023-10-20 11:39       ` Johannes Berg
2023-10-20 12:06         ` Benjamin Beichler
2023-10-20 12:20           ` Johannes Berg
2023-10-20 12:23             ` Johannes Berg
2023-10-20 12:43               ` Benjamin Beichler
2023-10-20 12:58                 ` Johannes Berg
2023-10-20 12:58             ` Benjamin Beichler
2023-10-20 13:39               ` Johannes Berg
2023-10-20 15:47                 ` Benjamin Beichler
2023-10-20 15:51                   ` Johannes Berg

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231018123643.1255813-3-benjamin@sipsolutions.net \
    --to=benjamin@sipsolutions.net \
    --cc=benjamin.berg@intel.com \
    --cc=linux-um@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox