Patchwork ipv4: Handle very small SO_SNDTIMEOs

login
register
mail settings
Submitter Andi Kleen
Date Nov. 8, 2012, 3:51 p.m.
Message ID <1352389908-28587-1-git-send-email-andi@firstfloor.org>
Download mbox | patch
Permalink /patch/197852/
State Changes Requested
Delegated to: David Miller
Headers show

Comments

Andi Kleen - Nov. 8, 2012, 3:51 p.m.
From: Andi Kleen <ak@linux.intel.com>

When the SO_SNDTIMEO timeout is short enough it may round down to zero
jiffies.   This causes unexpected behaviour because the socket
essentially becomes always non blocking.

Round up the timeout to at least two jiffies, so that
we guarantee waiting for at least some time. With one jiffie
we could potentially still wait zero time because we might
be just at the edge of the jiffie, and if it flips over
in the middle of the check it could be still zero.

This has been observed in a real application.

Signed-off-by: Andi Kleen <ak@linux.intel.com>
---
 net/core/sock.c |    9 ++++++++-
 1 files changed, 8 insertions(+), 1 deletions(-)
David Miller - Nov. 9, 2012, 9:58 p.m.
From: Andi Kleen <andi@firstfloor.org>
Date: Thu,  8 Nov 2012 07:51:48 -0800

> From: Andi Kleen <ak@linux.intel.com>
> 
> When the SO_SNDTIMEO timeout is short enough it may round down to zero
> jiffies.   This causes unexpected behaviour because the socket
> essentially becomes always non blocking.
> 
> Round up the timeout to at least two jiffies, so that
> we guarantee waiting for at least some time. With one jiffie
> we could potentially still wait zero time because we might
> be just at the edge of the jiffie, and if it flips over
> in the middle of the check it could be still zero.
> 
> This has been observed in a real application.
> 
> Signed-off-by: Andi Kleen <ak@linux.intel.com>

Please use usecs_to_jiffies() and, if necessary, implement the policy
there.

I'm sure that things like poll() et al. have similar if not identical
issues.  Therefore it's better to have some generic spot deal with
this problem generically.
--
To unsubscribe from this list: send the line "unsubscribe netdev" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Patch

diff --git a/net/core/sock.c b/net/core/sock.c
index a6000fb..93c0060 100644
--- a/net/core/sock.c
+++ b/net/core/sock.c
@@ -362,8 +362,15 @@  static int sock_set_timeout(long *timeo_p, char __user *optval, int optlen)
 	*timeo_p = MAX_SCHEDULE_TIMEOUT;
 	if (tv.tv_sec == 0 && tv.tv_usec == 0)
 		return 0;
-	if (tv.tv_sec < (MAX_SCHEDULE_TIMEOUT/HZ - 1))
+	if (tv.tv_sec < (MAX_SCHEDULE_TIMEOUT/HZ - 1)) {
 		*timeo_p = tv.tv_sec*HZ + (tv.tv_usec+(1000000/HZ-1))/(1000000/HZ);
+		/* Do at least two jiffies to ensure we wait at all.
+		 * Otherwise we may be on the edge of the next jiffie already,
+		 * and then essentially non block.
+		 */
+		if ((unsigned long)*timeo_p < 2)
+			*timeo_p = 2;
+	}
 	return 0;
 }